Jetpack_React_Page

The WordPress Core Jetpack React Page class.

Defined (1)

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

/_inc/lib/admin-pages/class.jetpack-react-page.php  
  1. class Jetpack_React_Page extends Jetpack_Admin_Page { 
  2.  
  3. protected $dont_show_if_not_active = false; 
  4.  
  5. protected $is_redirecting = false; 
  6.  
  7. function get_page_hook() { 
  8. $title = _x( 'Jetpack', 'The menu item label', 'jetpack' ); 
  9.  
  10. // Add the main admin Jetpack menu 
  11. return add_menu_page( 'Jetpack', $title, 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' ); 
  12.  
  13. function add_page_actions( $hook ) { 
  14. /** This action is documented in class.jetpack.php */ 
  15. do_action( 'jetpack_admin_menu', $hook ); 
  16.  
  17. // Place the Jetpack menu item on top and others in the order they appear 
  18. add_filter( 'custom_menu_order', '__return_true' ); 
  19. add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) ); 
  20.  
  21. if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] || ! empty( $_GET['configure'] ) ) { 
  22. return; // No need to handle the fallback redirection if we are not on the Jetpack page 
  23.  
  24. // Adding a redirect meta tag for older WordPress versions or if the REST API is disabled 
  25. if ( $this->is_wp_version_too_old() || ! $this->is_rest_api_enabled() ) { 
  26. $this->is_redirecting = true; 
  27. add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) ); 
  28.  
  29. // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled 
  30. add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) ); 
  31.  
  32. // Adding a redirect tag wrapped in browser conditional comments 
  33. add_action( 'admin_head', array( $this, 'add_legacy_browsers_head_script' ) ); 
  34.  
  35. /** 
  36. * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active. 
  37. * Works in Dev Mode or when user is connected. 
  38. * @since 4.3.0 
  39. */ 
  40. function jetpack_add_dashboard_sub_nav_item() { 
  41. if ( Jetpack::is_development_mode() || Jetpack::is_active() ) { 
  42. global $submenu; 
  43. if ( current_user_can( 'jetpack_admin_page' ) ) { 
  44. $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' ); 
  45.  
  46. /** 
  47. * If user is allowed to see the Jetpack Admin, add Settings sub-link. 
  48. * @since 4.3.0 
  49. */ 
  50. function jetpack_add_settings_sub_nav_item() { 
  51. if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) { 
  52. global $submenu; 
  53. $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/settings' ); 
  54.  
  55. function add_fallback_head_meta() { 
  56. echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">'; 
  57.  
  58. function add_noscript_head_meta() { 
  59. echo '<noscript>'; 
  60. $this->add_fallback_head_meta(); 
  61. echo '</noscript>'; 
  62.  
  63. function add_legacy_browsers_head_script() { 
  64. echo 
  65. "<script type=\"text/javascript\">\n" 
  66. . "/*@cc_on\n" 
  67. . "if ( @_jscript_version <= 10) {\n" 
  68. . "window.location.href = '?page=jetpack_modules';\n" 
  69. . "}\n" 
  70. . "@*/\n" 
  71. . "</script>"; 
  72.  
  73. function jetpack_menu_order( $menu_order ) { 
  74. $jp_menu_order = array(); 
  75.  
  76. foreach ( $menu_order as $index => $item ) { 
  77. if ( $item != 'jetpack' ) 
  78. $jp_menu_order[] = $item; 
  79.  
  80. if ( $index == 0 ) 
  81. $jp_menu_order[] = 'jetpack'; 
  82.  
  83. return $jp_menu_order; 
  84.  
  85. // Render the configuration page for the module if it exists and an error 
  86. // screen if the module is not configurable 
  87. // @todo remove when real settings are in place 
  88. function render_nojs_configurable( $module_name ) { 
  89. $module_name = preg_replace( '/[^\da-z\-]+/', '', $_GET['configure'] ); 
  90.  
  91. include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' ); 
  92. echo '<div class="wrap configure-module">'; 
  93.  
  94. if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) { 
  95. Jetpack::admin_screen_configure_module( $module_name ); 
  96. } else { 
  97. echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>'; 
  98.  
  99. echo '</div><!-- /wrap -->'; 
  100.  
  101. function page_render() { 
  102. // Handle redirects to configuration pages 
  103. if ( ! empty( $_GET['configure'] ) ) { 
  104. return $this->render_nojs_configurable( $_GET['configure'] ); 
  105.  
  106. /** This action is already documented in views/admin/admin-page.php */ 
  107. do_action( 'jetpack_notices' ); 
  108.  
  109. // Try fetching by patch 
  110. $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' ); 
  111.  
  112. if ( false === $static_html ) { 
  113.  
  114. // If we still have nothing, display an error 
  115. echo '<p>'; 
  116. esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' ); 
  117. echo '<code>yarn distclean && yarn build</code>'; 
  118. echo '</p>'; 
  119. } else { 
  120.  
  121. // We got the static.html so let's display it 
  122. echo $static_html; 
  123.  
  124. function get_i18n_data() { 
  125.  
  126. $i18n_json = JETPACK__PLUGIN_DIR . 'languages/json/jetpack-' . jetpack_get_user_locale() . '.json'; 
  127.  
  128. if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) { 
  129. $locale_data = @file_get_contents( $i18n_json ); 
  130. if ( $locale_data ) { 
  131. return $locale_data; 
  132.  
  133. // Return empty if we have nothing to return so it doesn't fail when parsed in JS 
  134. return '{}'; 
  135.  
  136. /** 
  137. * Gets array of any Jetpack notices that have been dismissed. 
  138. * @since 4.0.1 
  139. * @return mixed|void 
  140. */ 
  141. function get_dismissed_jetpack_notices() { 
  142. $jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() ); 
  143. /** 
  144. * Array of notices that have been dismissed. 
  145. * @since 4.0.1 
  146. * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices. 
  147. */ 
  148. $dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices ); 
  149. return $dismissed_notices; 
  150.  
  151. function additional_styles() { 
  152. $rtl = is_rtl() ? '.rtl' : ''; 
  153.  
  154. wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); 
  155. wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION ); 
  156.  
  157. function page_admin_scripts() { 
  158. if ( $this->is_redirecting ) { 
  159. return; // No need for scripts on a fallback page 
  160.  
  161. $is_dev_mode = Jetpack::is_development_mode(); 
  162.  
  163. // Enqueue jp.js and localize it 
  164. wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true ); 
  165.  
  166. if ( ! $is_dev_mode ) { 
  167. // Required for Analytics 
  168. wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true ); 
  169.  
  170. // Collecting roles that can view site stats. 
  171. $stats_roles = array(); 
  172. $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' ); 
  173.  
  174. if ( ! function_exists( 'get_editable_roles' ) ) { 
  175. require_once ABSPATH . 'wp-admin/includes/user.php'; 
  176. foreach ( get_editable_roles() as $slug => $role ) { 
  177. $stats_roles[ $slug ] = array( 
  178. 'name' => translate_user_role( $role['name'] ),  
  179. 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false,  
  180. ); 
  181.  
  182. $response = rest_do_request( new WP_REST_Request( 'GET', '/jetpack/v4/module/all' ) ); 
  183. $modules = $response->get_data(); 
  184.  
  185. // Preparing translated fields for JSON encoding by transforming all HTML entities to 
  186. // respective characters. 
  187. foreach( $modules as $slug => $data ) { 
  188. $modules[ $slug ]['name'] = html_entity_decode( $data['name'] ); 
  189. $modules[ $slug ]['description'] = html_entity_decode( $data['description'] ); 
  190. $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] ); 
  191. $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] ); 
  192.  
  193. // Get last post, to build the link to Customizer in the Related Posts module. 
  194. $last_post = get_posts( array( 'posts_per_page' => 1 ) ); 
  195. $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post 
  196. ? get_permalink( $last_post[0]->ID ) 
  197. : get_home_url(); 
  198.  
  199. // Get information about current theme. 
  200. $current_theme = wp_get_theme(); 
  201.  
  202. // Get all themes that Infinite Scroll provides support for natively. 
  203. $inf_scr_support_themes = array(); 
  204. foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) { 
  205. if ( is_readable( $path ) ) { 
  206. $inf_scr_support_themes[] = basename( $path, '.php' ); 
  207.  
  208. // Add objects to be passed to the initial state of the app 
  209. wp_localize_script( 'react-plugin', 'Initial_State', array( 
  210. 'WP_API_root' => esc_url_raw( rest_url() ),  
  211. 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),  
  212. 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),  
  213. 'connectionStatus' => array( 
  214. 'isActive' => Jetpack::is_active(),  
  215. 'isStaging' => Jetpack::is_staging_site(),  
  216. 'devMode' => array( 
  217. 'isActive' => $is_dev_mode,  
  218. 'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,  
  219. 'url' => site_url() && false === strpos( site_url(), '.' ),  
  220. 'filter' => apply_filters( 'jetpack_development_mode', false ),  
  221. ),  
  222. 'isPublic' => '1' == get_option( 'blog_public' ),  
  223. 'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(),  
  224. ),  
  225. 'dismissedNotices' => $this->get_dismissed_jetpack_notices(),  
  226. 'isDevVersion' => Jetpack::is_development_version(),  
  227. 'currentVersion' => JETPACK__VERSION,  
  228. 'getModules' => $modules,  
  229. 'showJumpstart' => jetpack_show_jumpstart(),  
  230. 'showHolidaySnow' => function_exists( 'jetpack_show_holiday_snow_option' ) ? jetpack_show_holiday_snow_option() : false,  
  231. 'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),  
  232. 'adminUrl' => esc_url( admin_url() ),  
  233. 'stats' => array( 
  234. // data is populated asynchronously on page load 
  235. 'data' => array( 
  236. 'general' => false,  
  237. 'day' => false,  
  238. 'week' => false,  
  239. 'month' => false,  
  240. ),  
  241. 'roles' => $stats_roles,  
  242. ),  
  243. 'settings' => $this->get_flattened_settings( $modules ),  
  244. 'settingNames' => array( 
  245. 'jetpack_holiday_snow_enabled' => function_exists( 'jetpack_holiday_snow_option_name' ) ? jetpack_holiday_snow_option_name() : false,  
  246. ),  
  247. 'userData' => array( 
  248. // 'othersLinked' => Jetpack::get_other_linked_admins(),  
  249. 'currentUser' => jetpack_current_user_data(),  
  250. ),  
  251. 'siteData' => array( 
  252. 'icon' => has_site_icon() 
  253. ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) ) 
  254. : '',  
  255. 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ),  
  256. /** 
  257. * Whether promotions are visible or not. 
  258. * @since 4.8.0 
  259. * @param bool $are_promotions_active Status of promotions visibility. True by default. 
  260. */ 
  261. 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ),  
  262. 'isAutomatedTransfer' => jetpack_is_automated_transfer_site(),  
  263. ),  
  264. 'themeData' => array( 
  265. 'name' => $current_theme->get( 'Name' ),  
  266. 'hasUpdate' => (bool) get_theme_update_available( $current_theme ),  
  267. 'support' => array( 
  268. 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes ),  
  269. ),  
  270. ),  
  271. 'locale' => $this->get_i18n_data(),  
  272. 'localeSlug' => join( '-', explode( '_', jetpack_get_user_locale() ) ),  
  273. 'jetpackStateNotices' => array( 
  274. 'messageCode' => Jetpack::state( 'message' ),  
  275. 'errorCode' => Jetpack::state( 'error' ),  
  276. 'errorDescription' => Jetpack::state( 'error_description' ),  
  277. ),  
  278. 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),  
  279. 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false,  
  280. 'lastPostUrl' => esc_url( $last_post ),  
  281. ) ); 
  282.  
  283. /** 
  284. * Returns an array of modules and settings both as first class members of the object. 
  285. * @param array $modules the result of an API request to get all modules. 
  286. * @return array flattened settings with modules. 
  287. */ 
  288. function get_flattened_settings( $modules ) { 
  289. $core_api_endpoint = new Jetpack_Core_API_Data(); 
  290. $settings = $core_api_endpoint->get_all_options(); 
  291. return $settings->data;