Jetpack_Admin

The Jetpack by WordPress.com Jetpack Admin class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-admin.php  
  1. class Jetpack_Admin { 
  2.  
  3. /** 
  4. * @var Jetpack_Admin 
  5. **/ 
  6. private static $instance = null; 
  7.  
  8. /** 
  9. * @var Jetpack 
  10. **/ 
  11. private $jetpack; 
  12.  
  13. static function init() { 
  14. if ( is_null( self::$instance ) ) { 
  15. self::$instance = new Jetpack_Admin; 
  16. return self::$instance; 
  17.  
  18. private function __construct() { 
  19. $this->jetpack = Jetpack::init(); 
  20.  
  21. jetpack_require_lib( 'admin-pages/class.jetpack-landing-page' ); 
  22. $this->landing_page = new Jetpack_Landing_Page; 
  23.  
  24. jetpack_require_lib( 'admin-pages/class.jetpack-settings-page' ); 
  25. $this->settings_page = new Jetpack_Settings_Page; 
  26.  
  27. jetpack_require_lib( 'admin-pages/class.jetpack-my-jetpack-page' ); 
  28. $this->my_jetpack_page = new Jetpack_My_Jetpack_Page; 
  29.  
  30. if ( isset( $_POST['jetpack-set-master-user'] ) ) { 
  31. add_action( 'init', array( $this->my_jetpack_page, 'jetpack_my_jetpack_change_user' ) ); 
  32.  
  33. // Add hooks for admin menus 
  34. add_action( 'admin_menu', array( $this->landing_page, 'add_actions' ), 998 ); 
  35. add_action( 'jetpack_admin_menu', array( $this, 'admin_menu_debugger' ) ); 
  36. add_action( 'jetpack_admin_menu', array( $this->settings_page, 'add_actions' ) ); 
  37. add_action( 'jetpack_admin_menu', array( $this->my_jetpack_page, 'add_actions' ) ); 
  38.  
  39.  
  40. // Add redirect to current page for activation/deactivation of modules 
  41. add_action( 'jetpack_pre_activate_module', array( $this, 'fix_redirect' ), 10, 2 ); 
  42. add_action( 'jetpack_pre_deactivate_module', array( $this, 'fix_redirect' ) ); 
  43.  
  44. // Add module bulk actions handler 
  45. add_action( 'jetpack_unrecognized_action', array( $this, 'handle_unrecognized_action' ) ); 
  46.  
  47. static function sort_requires_connection_last( $module1, $module2 ) { 
  48. if ( $module1['requires_connection'] == $module2['requires_connection'] ) { 
  49. return 0; 
  50. } elseif ( $module1['requires_connection'] ) { 
  51. return 1; 
  52. } elseif ( $module2['requires_connection'] ) { 
  53. return -1; 
  54.  
  55. return 0; 
  56.  
  57. // Produce JS understandable objects of modules containing information for 
  58. // presentation like description, name, configuration url, etc. 
  59. function get_modules() { 
  60. include_once( JETPACK__PLUGIN_DIR . 'modules/module-info.php' ); 
  61. $available_modules = $this->jetpack->get_available_modules(); 
  62. $active_modules = $this->jetpack->get_active_modules(); 
  63. $modules = array(); 
  64. $jetpack_active = Jetpack::is_active() || Jetpack::is_development_mode(); 
  65. foreach ( $available_modules as $module ) { 
  66. if ( $module_array = $this->jetpack->get_module( $module ) ) { 
  67. /** 
  68. * Filters each module's short description. 
  69. * @since 3.0.0 
  70. * @param string $module_array['description'] Module description. 
  71. * @param string $module Module slug. 
  72. */ 
  73. $short_desc = apply_filters( 'jetpack_short_module_description', $module_array['description'], $module ); 
  74. // Fix: correct multibyte strings truncate with checking for mbstring extension 
  75. $short_desc_trunc = ( function_exists( 'mb_strlen' ) ) 
  76. ? ( ( mb_strlen( $short_desc ) > 143 ) 
  77. ? mb_substr( $short_desc, 0, 140 ) . '...' 
  78. : $short_desc ) 
  79. : ( ( strlen( $short_desc ) > 143 ) 
  80. ? substr( $short_desc, 0, 140 ) . '...' 
  81. : $short_desc ); 
  82.  
  83. $module_array['module'] = $module; 
  84. $module_array['activated'] = ( $jetpack_active ? in_array( $module, $active_modules ) : false ); 
  85. $module_array['deactivate_nonce'] = wp_create_nonce( 'jetpack_deactivate-' . $module ); 
  86. $module_array['activate_nonce'] = wp_create_nonce( 'jetpack_activate-' . $module ); 
  87. $module_array['available'] = self::is_module_available( $module_array ); 
  88. $module_array['short_description'] = $short_desc_trunc; 
  89. $module_array['configure_url'] = Jetpack::module_configuration_url( $module ); 
  90.  
  91. ob_start(); 
  92. /** 
  93. * Allow the display of a "Learn More" button. 
  94. * The dynamic part of the action, $module, is the module slug. 
  95. * @since 3.0.0 
  96. */ 
  97. do_action( 'jetpack_learn_more_button_' . $module ); 
  98. $module_array['learn_more_button'] = ob_get_clean(); 
  99.  
  100. ob_start(); 
  101. if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $module ) ) { 
  102. /** 
  103. * Allow the display of information text when Jetpack is connected to WordPress.com. 
  104. * The dynamic part of the action, $module, is the module slug. 
  105. * @since 3.0.0 
  106. */ 
  107. do_action( 'jetpack_module_more_info_connected_' . $module ); 
  108. } else { 
  109. /** 
  110. * Allow the display of information text when Jetpack is connected to WordPress.com. 
  111. * The dynamic part of the action, $module, is the module slug. 
  112. * @since 3.0.0 
  113. */ 
  114. do_action( 'jetpack_module_more_info_' . $module ); 
  115.  
  116. /** 
  117. * Filter the long description of a module. 
  118. * @since 3.5.0 
  119. * @param string ob_get_clean() The module long description. 
  120. * @param string $module The module name. 
  121. */ 
  122. $module_array['long_description'] = apply_filters( 'jetpack_long_module_description', ob_get_clean(), $module ); 
  123.  
  124. ob_start(); 
  125. /** 
  126. * Filter the search terms for a module 
  127. * Search terms are typically added to the module headers, under "Additional Search Queries". 
  128. * Use syntax: 
  129. * function jetpack_$module_search_terms( $terms ) { 
  130. * $terms = _x( 'term 1, term 2', 'search terms', 'jetpack' ); 
  131. * return $terms; 
  132. * } 
  133. * add_filter( 'jetpack_search_terms_$module', 'jetpack_$module_search_terms' ); 
  134. * @since 3.5.0 
  135. * @param string The search terms (comma separated). 
  136. */ 
  137. echo apply_filters( 'jetpack_search_terms_' . $module, $module_array['additional_search_queries'] ); 
  138. $module_array['search_terms'] = ob_get_clean(); 
  139.  
  140. $module_array['configurable'] = false; 
  141. if ( 
  142. current_user_can( 'manage_options' ) && 
  143. /** 
  144. * Allow the display of a configuration link in the Jetpack Settings screen. 
  145. * @since 3.0.0 
  146. * @param string $module Module name. 
  147. * @param bool false Should the Configure module link be displayed? Default to false. 
  148. */ 
  149. apply_filters( 'jetpack_module_configurable_' . $module, false ) 
  150. ) { 
  151. $module_array['configurable'] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( Jetpack::module_configuration_url( $module ) ), __( 'Configure', 'jetpack' ) ); 
  152.  
  153. $modules[ $module ] = $module_array; 
  154.  
  155. uasort( $modules, array( $this->jetpack, 'sort_modules' ) ); 
  156.  
  157. if ( ! Jetpack::is_active() ) { 
  158. uasort( $modules, array( __CLASS__, 'sort_requires_connection_last' ) ); 
  159.  
  160. return $modules; 
  161.  
  162. static function is_module_available( $module ) { 
  163. if ( ! is_array( $module ) || empty( $module ) ) 
  164. return false; 
  165.  
  166. /** 
  167. * We never want to show VaultPress as activatable through Jetpack. 
  168. */ 
  169. if ( 'vaultpress' === $module['module'] ) { 
  170. return false; 
  171.  
  172. if ( Jetpack::is_development_mode() ) { 
  173. return ! ( $module['requires_connection'] ); 
  174. } else { 
  175. return Jetpack::is_active(); 
  176.  
  177. function handle_unrecognized_action( $action ) { 
  178. switch( $action ) { 
  179. case 'bulk-activate' : 
  180. if ( ! current_user_can( 'jetpack_activate_modules' ) ) { 
  181. break; 
  182.  
  183. $modules = (array) $_GET['modules']; 
  184. $modules = array_map( 'sanitize_key', $modules ); 
  185. check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); 
  186. foreach( $modules as $module ) { 
  187. Jetpack::log( 'activate', $module ); 
  188. Jetpack::activate_module( $module, false ); 
  189. // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end. 
  190. wp_safe_redirect( wp_get_referer() ); 
  191. exit; 
  192. case 'bulk-deactivate' : 
  193. if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) { 
  194. break; 
  195.  
  196. $modules = (array) $_GET['modules']; 
  197. $modules = array_map( 'sanitize_key', $modules ); 
  198. check_admin_referer( 'bulk-jetpack_page_jetpack_modules' ); 
  199. foreach ( $modules as $module ) { 
  200. Jetpack::log( 'deactivate', $module ); 
  201. Jetpack::deactivate_module( $module ); 
  202. Jetpack::state( 'message', 'module_deactivated' ); 
  203. Jetpack::state( 'module', $modules ); 
  204. wp_safe_redirect( wp_get_referer() ); 
  205. exit; 
  206. default: 
  207. return; 
  208.  
  209. function fix_redirect( $module, $redirect = true ) { 
  210. if ( ! $redirect ) { 
  211. return; 
  212. if ( wp_get_referer() ) { 
  213. add_filter( 'wp_redirect', 'wp_get_referer' ); 
  214.  
  215. function admin_menu_debugger() { 
  216. $debugger_hook = add_submenu_page( null, __( 'Jetpack Debugging Center', 'jetpack' ), '', 'manage_options', 'jetpack-debugger', array( $this, 'debugger_page' ) ); 
  217. add_action( "admin_head-$debugger_hook", array( 'Jetpack_Debugger', 'jetpack_debug_admin_head' ) ); 
  218.  
  219. function debugger_page() { 
  220. nocache_headers(); 
  221. if ( ! current_user_can( 'manage_options' ) ) { 
  222. die( '-1' ); 
  223. Jetpack_Debugger::jetpack_debug_display_handler();