Last active
August 10, 2016 15:29
-
-
Save igmoweb/6ce4c50f1d3402c99bd240e9bb372013 to your computer and use it in GitHub Desktop.
Carga ciertos módulos de Jetpack sin activar Jetpack y oculta Jetpack a los usuarios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Jetpack_Mandatory_Modules { | |
public function __construct() { | |
add_action( 'plugins_loaded', array( $this, 'load_base_jetpack' ) ); | |
// Este hook marcará los módulos obligatorios como activados | |
// aquellos obligatorios que nosotros queremos | |
add_filter( 'pre_option_jetpack_active_modules', array( $this, 'mandatory_modules' ), 500 ); | |
// Cargamos sólo los módulos obligatorios. Jetpack no tendrá en cuenta los demás | |
add_filter( 'jetpack_get_available_modules', array( $this, 'mandatory_modules' ), 500 ); | |
// Ponemos Jetpack en modo Developer. | |
add_filter( 'jetpack_development_mode', '__return_true' ); | |
// Y escondemos los menús de administración de Jetpack | |
add_action( 'admin_init', array( $this, 'remove_jetpack_admin_menus' ), 500 ); | |
// Y que no aparezca Jetpack en la lista de plugins | |
add_filter( 'all_plugins', array( $this, 'remove_jetpack_from_plugins_list' ) ); | |
// Como medida preventiva, desactivaremos Jetpack en caso de estar ya activado | |
add_action( 'admin_init', array( $this, 'maybe_deactivate_jetpack' ) ); | |
} | |
/** | |
* Quita Jetpack de la lista de plugins | |
* | |
* @param $plugins | |
* | |
* @return mixed | |
*/ | |
public function remove_jetpack_from_plugins_list( $plugins ) { | |
if ( isset( $plugins['jetpack/jetpack.php'] ) ) { | |
unset( $plugins['jetpack/jetpack.php'] ); | |
} | |
return $plugins; | |
} | |
/** | |
* Carga el código base de Jetpack | |
*/ | |
public function load_base_jetpack() { | |
$jetpack_dir = WP_CONTENT_DIR . '/plugins/jetpack'; | |
include_once( $jetpack_dir . '/jetpack.php' ); | |
} | |
/** | |
* Módulos que siempre queremos tener cargados | |
* | |
* @return array | |
*/ | |
public function mandatory_modules() { | |
return array( | |
'custom-css', | |
'widget-visibility', | |
'shortcodes' | |
); | |
} | |
/** | |
* Carga los módulos obligatorios | |
*/ | |
public function load_mandatory_modules() { | |
$modules = self::mandatory_modules(); | |
foreach ( $modules as $module ) { | |
if ( ! @include_once( Jetpack::get_module_path( $module ) ) ) { | |
continue; | |
} | |
} | |
} | |
/** | |
* Elimina el menú de Jetpack del escritorio de WordPress | |
*/ | |
public function remove_jetpack_admin_menus() { | |
remove_menu_page( 'jetpack' ); | |
remove_submenu_page( 'jetpack', 'jetpack' ); | |
remove_submenu_page( 'jetpack', 'jetpack_modules' ); | |
} | |
/** | |
* Desactiva Jetpack si ya está activado | |
*/ | |
public function maybe_deactivate_jetpack() { | |
$active_plugins = get_option( 'active_plugins' ); | |
if ( in_array( 'jetpack/jetpack.php', $active_plugins ) ) { | |
deactivate_plugins( array( 'jetpack/jetpack.php' ), true ); | |
} | |
} | |
} | |
// Instanciamos la clase en un hook temprano | |
// Ojo. las funciones anónimas no funcionan en PHP > 5.3 | |
add_action( 'muplugins_loaded', function() { | |
// Nueva instancia de nuestra clase | |
new Jetpack_Mandatory_Modules(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment