Jetpack_Admin_Page

The WordPress Core Jetpack Admin Page class.

Defined (1)

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

/_inc/lib/admin-pages/class.jetpack-admin-page.php  
  1. abstract class Jetpack_Admin_Page { 
  2. // Add page specific actions given the page hook 
  3. abstract function add_page_actions( $hook ); 
  4.  
  5. // Create a menu item for the page and returns the hook 
  6. abstract function get_page_hook(); 
  7.  
  8. // Enqueue and localize page specific scripts 
  9. abstract function page_admin_scripts(); 
  10.  
  11. // Render page specific HTML 
  12. abstract function page_render(); 
  13.  
  14. /** 
  15. * Should we block the page rendering because the site is in IDC? 
  16. * @var bool 
  17. */ 
  18. static $block_page_rendering_for_idc; 
  19.  
  20. /** 
  21. * Flag to know if we already checked the plan. 
  22. * @since 4.4.0 
  23. * @var bool 
  24. */ 
  25. static $plan_checked = false; 
  26.  
  27. /** 
  28. * Function called after admin_styles to load any additional needed styles. 
  29. * @since 4.3.0 
  30. */ 
  31. function additional_styles() {} 
  32.  
  33. function __construct() { 
  34. $this->jetpack = Jetpack::init(); 
  35. self::$block_page_rendering_for_idc = ( 
  36. Jetpack::validate_sync_error_idc_option() && ! Jetpack_Options::get_option( 'safe_mode_confirmed' ) 
  37. ); 
  38.  
  39. function add_actions() { 
  40.  
  41. // If user is not an admin and site is in Dev Mode, don't do anything 
  42. if ( ! current_user_can( 'manage_options' ) && Jetpack::is_development_mode() ) { 
  43. return; 
  44.  
  45. // Don't add in the modules page unless modules are available! 
  46. if ( $this->dont_show_if_not_active && ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) { 
  47. return; 
  48.  
  49. // Initialize menu item for the page in the admin 
  50. $hook = $this->get_page_hook(); 
  51.  
  52. // Attach hooks common to all Jetpack admin pages based on the created 
  53. // hook 
  54. add_action( "load-$hook", array( $this, 'admin_help' ) ); 
  55. add_action( "load-$hook", array( $this, 'admin_page_load' ) ); 
  56. add_action( "admin_head-$hook", array( $this, 'admin_head' ) ); 
  57.  
  58. add_action( "admin_print_styles-$hook", array( $this, 'admin_styles' ) ); 
  59. add_action( "admin_print_scripts-$hook", array( $this, 'admin_scripts' ) ); 
  60.  
  61. if ( ! self::$block_page_rendering_for_idc ) { 
  62. add_action( "admin_print_styles-$hook", array( $this, 'additional_styles' ) ); 
  63.  
  64. // Check if the site plan changed and deactivate modules accordingly. 
  65. add_action( 'current_screen', array( $this, 'check_plan_deactivate_modules' ) ); 
  66.  
  67. // Attach page specific actions in addition to the above 
  68. $this->add_page_actions( $hook ); 
  69.  
  70. function admin_head() { 
  71. if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) { 
  72. /** 
  73. * Fires in the <head> of a particular Jetpack configuation page. 
  74. * The dynamic portion of the hook name, `$_GET['configure']`,  
  75. * refers to the slug of module, such as 'stats', 'sso', etc. 
  76. * A complete hook for the latter would be 
  77. * 'jetpack_module_configuation_head_sso'. 
  78. * @since 3.0.0 
  79. */ 
  80. do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] ); 
  81.  
  82. // Render the page with a common top and bottom part, and page specific content 
  83. function render() { 
  84. // We're in an IDC: we need a decision made before we show the UI again. 
  85. if ( self::$block_page_rendering_for_idc ) { 
  86. return; 
  87.  
  88. $this->page_render(); 
  89.  
  90. function admin_help() { 
  91. $this->jetpack->admin_help(); 
  92.  
  93. function admin_page_load() { 
  94. // This is big. For the moment, just call the existing one. 
  95. $this->jetpack->admin_page_load(); 
  96.  
  97. function admin_page_top() { 
  98. include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); 
  99.  
  100. function admin_page_bottom() { 
  101. include_once( JETPACK__PLUGIN_DIR . '_inc/footer.php' ); 
  102.  
  103. // Add page specific scripts and jetpack stats for all menu pages 
  104. function admin_scripts() { 
  105. $this->page_admin_scripts(); // Delegate to inheriting class 
  106. add_action( 'admin_footer', array( $this->jetpack, 'do_stats' ) ); 
  107.  
  108. // Enqueue the Jetpack admin stylesheet 
  109. function admin_styles() { 
  110. $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; 
  111.  
  112. wp_enqueue_style( 'jetpack-admin', plugins_url( "css/jetpack-admin{$min}.css", JETPACK__PLUGIN_FILE ), array( 'genericons' ), JETPACK__VERSION . '-20121016' ); 
  113. wp_style_add_data( 'jetpack-admin', 'rtl', 'replace' ); 
  114. wp_style_add_data( 'jetpack-admin', 'suffix', $min ); 
  115.  
  116. /** 
  117. * Checks if WordPress version is too old to have REST API. 
  118. * @since 4.3 
  119. * @return bool 
  120. */ 
  121. function is_wp_version_too_old() { 
  122. global $wp_version; 
  123. return ( ! function_exists( 'rest_api_init' ) || version_compare( $wp_version, '4.4-z', '<=' ) ); 
  124.  
  125. /** 
  126. * Checks if REST API is enabled. 
  127. * @since 4.4.2 
  128. * @return bool 
  129. */ 
  130. function is_rest_api_enabled() { 
  131. return 
  132. /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ 
  133. apply_filters( 'rest_enabled', true ) && 
  134. /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ 
  135. apply_filters( 'rest_jsonp_enabled', true ) && 
  136. /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */ 
  137. apply_filters( 'rest_authentication_errors', true ); 
  138.  
  139. /** 
  140. * Checks the site plan and deactivates modules that were active but are no longer included in the plan. 
  141. * @since 4.4.0 
  142. * @param $page 
  143. * @return bool|array 
  144. */ 
  145. function check_plan_deactivate_modules( $page ) { 
  146. if ( 
  147. Jetpack::is_development_mode() 
  148. || ! in_array( 
  149. $page->base,  
  150. array( 
  151. 'toplevel_page_jetpack',  
  152. 'admin_page_jetpack_modules',  
  153. 'jetpack_page_vaultpress',  
  154. 'jetpack_page_stats',  
  155. 'jetpack_page_akismet-key-config' 
  156. || true === self::$plan_checked 
  157. ) { 
  158. return false; 
  159.  
  160. self::$plan_checked = true; 
  161. $previous = get_option( 'jetpack_active_plan', '' ); 
  162. $response = rest_do_request( new WP_REST_Request( 'GET', '/jetpack/v4/site' ) ); 
  163.  
  164. if ( ! is_object( $response ) || $response->is_error() ) { 
  165.  
  166. // If we can't get information about the current plan we don't do anything 
  167. self::$plan_checked = true; 
  168. return; 
  169.  
  170. $current = $response->get_data(); 
  171. $current = json_decode( $current['data'] ); 
  172.  
  173. $to_deactivate = array(); 
  174. if ( isset( $current->plan->product_slug ) ) { 
  175. if ( 
  176. empty( $previous ) 
  177. || ! isset( $previous['product_slug'] ) 
  178. || $previous['product_slug'] !== $current->plan->product_slug 
  179. ) { 
  180. $active = Jetpack::get_active_modules(); 
  181. switch ( $current->plan->product_slug ) { 
  182. case 'jetpack_free': 
  183. $to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads' ); 
  184. break; 
  185. case 'jetpack_personal': 
  186. case 'jetpack_personal_monthly': 
  187. $to_deactivate = array( 'seo-tools', 'videopress', 'google-analytics', 'wordads' ); 
  188. break; 
  189. case 'jetpack_premium': 
  190. case 'jetpack_premium_monthly': 
  191. $to_deactivate = array( 'seo-tools', 'google-analytics' ); 
  192. break; 
  193. $to_deactivate = array_intersect( $active, $to_deactivate ); 
  194. if ( ! empty( $to_deactivate ) ) { 
  195. Jetpack::update_active_modules( array_filter( array_diff( $active, $to_deactivate ) ) ); 
  196. return array( 
  197. 'previous' => $previous,  
  198. 'current' => $current,  
  199. 'deactivate' => $to_deactivate 
  200. );