class.jetpack-react-page.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. include_once( 'class.jetpack-admin-page.php' );
  3. // Builds the landing page and its menu
  4. class Jetpack_React_Page extends Jetpack_Admin_Page {
  5. protected $dont_show_if_not_active = false;
  6. protected $is_redirecting = false;
  7. function get_page_hook() {
  8. $title = _x( 'Jetpack', 'The menu item label', 'jetpack' );
  9. // Add the main admin Jetpack menu
  10. return add_menu_page( 'Jetpack', $title, 'jetpack_admin_page', 'jetpack', array( $this, 'render' ), 'div' );
  11. }
  12. function add_page_actions( $hook ) {
  13. /** This action is documented in class.jetpack.php */
  14. do_action( 'jetpack_admin_menu', $hook );
  15. // Place the Jetpack menu item on top and others in the order they appear
  16. add_filter( 'custom_menu_order', '__return_true' );
  17. add_filter( 'menu_order', array( $this, 'jetpack_menu_order' ) );
  18. if ( ! isset( $_GET['page'] ) || 'jetpack' !== $_GET['page'] || ! empty( $_GET['configure'] ) ) {
  19. return; // No need to handle the fallback redirection if we are not on the Jetpack page
  20. }
  21. // Adding a redirect meta tag for older WordPress versions or if the REST API is disabled
  22. if ( $this->is_wp_version_too_old() || ! $this->is_rest_api_enabled() ) {
  23. $this->is_redirecting = true;
  24. add_action( 'admin_head', array( $this, 'add_fallback_head_meta' ) );
  25. }
  26. // Adding a redirect meta tag wrapped in noscript tags for all browsers in case they have JavaScript disabled
  27. add_action( 'admin_head', array( $this, 'add_noscript_head_meta' ) );
  28. // Adding a redirect tag wrapped in browser conditional comments
  29. add_action( 'admin_head', array( $this, 'add_legacy_browsers_head_script' ) );
  30. }
  31. /**
  32. * Add Jetpack Dashboard sub-link and point it to AAG if the user can view stats, manage modules or if Protect is active.
  33. *
  34. * Works in Dev Mode or when user is connected.
  35. *
  36. * @since 4.3.0
  37. */
  38. function jetpack_add_dashboard_sub_nav_item() {
  39. if ( Jetpack::is_development_mode() || Jetpack::is_active() ) {
  40. global $submenu;
  41. if ( current_user_can( 'jetpack_admin_page' ) ) {
  42. $submenu['jetpack'][] = array( __( 'Dashboard', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/dashboard' );
  43. }
  44. }
  45. }
  46. /**
  47. * If user is allowed to see the Jetpack Admin, add Settings sub-link.
  48. *
  49. * @since 4.3.0
  50. */
  51. function jetpack_add_settings_sub_nav_item() {
  52. if ( ( Jetpack::is_development_mode() || Jetpack::is_active() ) && current_user_can( 'jetpack_admin_page' ) && current_user_can( 'edit_posts' ) ) {
  53. global $submenu;
  54. $submenu['jetpack'][] = array( __( 'Settings', 'jetpack' ), 'jetpack_admin_page', 'admin.php?page=jetpack#/settings' );
  55. }
  56. }
  57. function add_fallback_head_meta() {
  58. echo '<meta http-equiv="refresh" content="0; url=?page=jetpack_modules">';
  59. }
  60. function add_noscript_head_meta() {
  61. echo '<noscript>';
  62. $this->add_fallback_head_meta();
  63. echo '</noscript>';
  64. }
  65. function add_legacy_browsers_head_script() {
  66. echo
  67. "<script type=\"text/javascript\">\n"
  68. . "/*@cc_on\n"
  69. . "if ( @_jscript_version <= 10) {\n"
  70. . "window.location.href = '?page=jetpack_modules';\n"
  71. . "}\n"
  72. . "@*/\n"
  73. . "</script>";
  74. }
  75. function jetpack_menu_order( $menu_order ) {
  76. $jp_menu_order = array();
  77. foreach ( $menu_order as $index => $item ) {
  78. if ( $item != 'jetpack' )
  79. $jp_menu_order[] = $item;
  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. include_once( JETPACK__PLUGIN_DIR . '_inc/header.php' );
  91. echo '<div class="wrap configure-module">';
  92. if ( Jetpack::is_module( $module_name ) && current_user_can( 'jetpack_configure_modules' ) ) {
  93. Jetpack::admin_screen_configure_module( $module_name );
  94. } else {
  95. echo '<h2>' . esc_html__( 'Error, bad module.', 'jetpack' ) . '</h2>';
  96. }
  97. echo '</div><!-- /wrap -->';
  98. }
  99. function page_render() {
  100. // Handle redirects to configuration pages
  101. if ( ! empty( $_GET['configure'] ) ) {
  102. return $this->render_nojs_configurable( $_GET['configure'] );
  103. }
  104. /** This action is already documented in views/admin/admin-page.php */
  105. do_action( 'jetpack_notices' );
  106. // Try fetching by patch
  107. $static_html = @file_get_contents( JETPACK__PLUGIN_DIR . '_inc/build/static.html' );
  108. if ( false === $static_html ) {
  109. // If we still have nothing, display an error
  110. echo '<p>';
  111. esc_html_e( 'Error fetching static.html. Try running: ', 'jetpack' );
  112. echo '<code>yarn distclean && yarn build</code>';
  113. echo '</p>';
  114. } else {
  115. // We got the static.html so let's display it
  116. echo $static_html;
  117. }
  118. }
  119. /**
  120. * Gets array of any Jetpack notices that have been dismissed.
  121. *
  122. * @since 4.0.1
  123. * @return mixed|void
  124. */
  125. function get_dismissed_jetpack_notices() {
  126. $jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
  127. /**
  128. * Array of notices that have been dismissed.
  129. *
  130. * @since 4.0.1
  131. *
  132. * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
  133. */
  134. $dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
  135. return $dismissed_notices;
  136. }
  137. function additional_styles() {
  138. $rtl = is_rtl() ? '.rtl' : '';
  139. wp_enqueue_style( 'dops-css', plugins_url( "_inc/build/admin.dops-style$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
  140. wp_enqueue_style( 'components-css', plugins_url( "_inc/build/style.min$rtl.css", JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
  141. }
  142. function page_admin_scripts() {
  143. if ( $this->is_redirecting ) {
  144. return; // No need for scripts on a fallback page
  145. }
  146. $is_dev_mode = Jetpack::is_development_mode();
  147. // Enqueue jp.js and localize it
  148. wp_enqueue_script( 'react-plugin', plugins_url( '_inc/build/admin.js', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION, true );
  149. if ( ! $is_dev_mode && Jetpack::is_active() ) {
  150. // Required for Analytics
  151. wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
  152. }
  153. // Collecting roles that can view site stats.
  154. $stats_roles = array();
  155. $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
  156. if ( ! function_exists( 'get_editable_roles' ) ) {
  157. require_once ABSPATH . 'wp-admin/includes/user.php';
  158. }
  159. foreach ( get_editable_roles() as $slug => $role ) {
  160. $stats_roles[ $slug ] = array(
  161. 'name' => translate_user_role( $role['name'] ),
  162. 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false,
  163. );
  164. }
  165. // Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page
  166. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php';
  167. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
  168. $moduleListEndpoint = new Jetpack_Core_API_Module_List_Endpoint();
  169. $modules = $moduleListEndpoint->get_modules();
  170. // Preparing translated fields for JSON encoding by transforming all HTML entities to
  171. // respective characters.
  172. foreach( $modules as $slug => $data ) {
  173. $modules[ $slug ]['name'] = html_entity_decode( $data['name'] );
  174. $modules[ $slug ]['description'] = html_entity_decode( $data['description'] );
  175. $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] );
  176. $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] );
  177. }
  178. // Get last post, to build the link to Customizer in the Related Posts module.
  179. $last_post = get_posts( array( 'posts_per_page' => 1 ) );
  180. $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post
  181. ? get_permalink( $last_post[0]->ID )
  182. : get_home_url();
  183. // Get information about current theme.
  184. $current_theme = wp_get_theme();
  185. // Get all themes that Infinite Scroll provides support for natively.
  186. $inf_scr_support_themes = array();
  187. foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) {
  188. if ( is_readable( $path ) ) {
  189. $inf_scr_support_themes[] = basename( $path, '.php' );
  190. }
  191. }
  192. // Add objects to be passed to the initial state of the app
  193. wp_localize_script( 'react-plugin', 'Initial_State', array(
  194. 'WP_API_root' => esc_url_raw( rest_url() ),
  195. 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
  196. 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
  197. 'connectionStatus' => array(
  198. 'isActive' => Jetpack::is_active(),
  199. 'isStaging' => Jetpack::is_staging_site(),
  200. 'devMode' => array(
  201. 'isActive' => $is_dev_mode,
  202. 'constant' => defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG,
  203. 'url' => site_url() && false === strpos( site_url(), '.' ),
  204. 'filter' => apply_filters( 'jetpack_development_mode', false ),
  205. ),
  206. 'isPublic' => '1' == get_option( 'blog_public' ),
  207. 'isInIdentityCrisis' => Jetpack::validate_sync_error_idc_option(),
  208. ),
  209. 'connectUrl' => Jetpack::init()->build_connect_url( true, false, false ),
  210. 'dismissedNotices' => $this->get_dismissed_jetpack_notices(),
  211. 'isDevVersion' => Jetpack::is_development_version(),
  212. 'currentVersion' => JETPACK__VERSION,
  213. 'getModules' => $modules,
  214. 'showJumpstart' => jetpack_show_jumpstart(),
  215. 'rawUrl' => Jetpack::build_raw_urls( get_home_url() ),
  216. 'adminUrl' => esc_url( admin_url() ),
  217. 'stats' => array(
  218. // data is populated asynchronously on page load
  219. 'data' => array(
  220. 'general' => false,
  221. 'day' => false,
  222. 'week' => false,
  223. 'month' => false,
  224. ),
  225. 'roles' => $stats_roles,
  226. ),
  227. 'settings' => $this->get_flattened_settings( $modules ),
  228. 'userData' => array(
  229. // 'othersLinked' => Jetpack::get_other_linked_admins(),
  230. 'currentUser' => jetpack_current_user_data(),
  231. ),
  232. 'siteData' => array(
  233. 'icon' => has_site_icon()
  234. ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) )
  235. : '',
  236. 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ),
  237. /**
  238. * Whether promotions are visible or not.
  239. *
  240. * @since 4.8.0
  241. *
  242. * @param bool $are_promotions_active Status of promotions visibility. True by default.
  243. */
  244. 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ),
  245. 'isAtomicSite' => jetpack_is_atomic_site(),
  246. 'plan' => Jetpack::get_active_plan(),
  247. 'showBackups' => Jetpack::show_backups_ui(),
  248. ),
  249. 'themeData' => array(
  250. 'name' => $current_theme->get( 'Name' ),
  251. 'hasUpdate' => (bool) get_theme_update_available( $current_theme ),
  252. 'support' => array(
  253. 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes ),
  254. ),
  255. ),
  256. 'locale' => Jetpack::get_i18n_data_json(),
  257. 'localeSlug' => join( '-', explode( '_', jetpack_get_user_locale() ) ),
  258. 'jetpackStateNotices' => array(
  259. 'messageCode' => Jetpack::state( 'message' ),
  260. 'errorCode' => Jetpack::state( 'error' ),
  261. 'errorDescription' => Jetpack::state( 'error_description' ),
  262. ),
  263. 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
  264. 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false,
  265. 'lastPostUrl' => esc_url( $last_post ),
  266. ) );
  267. }
  268. /**
  269. * Returns an array of modules and settings both as first class members of the object.
  270. *
  271. * @param array $modules the result of an API request to get all modules.
  272. *
  273. * @return array flattened settings with modules.
  274. */
  275. function get_flattened_settings( $modules ) {
  276. $core_api_endpoint = new Jetpack_Core_API_Data();
  277. $settings = $core_api_endpoint->get_all_options();
  278. return $settings->data;
  279. }
  280. }
  281. /*
  282. * Only show Jump Start on first activation.
  283. * Any option 'jumpstart' other than 'new connection' will hide it.
  284. *
  285. * The option can be of 4 things, and will be stored as such:
  286. * new_connection : Brand new connection - Show
  287. * jumpstart_activated : Jump Start has been activated - dismiss
  288. * jetpack_action_taken: Manual activation of a module already happened - dismiss
  289. * jumpstart_dismissed : Manual dismissal of Jump Start - dismiss
  290. *
  291. * @todo move to functions.global.php when available
  292. * @since 3.6
  293. * @return bool | show or hide
  294. */
  295. function jetpack_show_jumpstart() {
  296. if ( ! Jetpack::is_active() ) {
  297. return false;
  298. }
  299. $jumpstart_option = Jetpack_Options::get_option( 'jumpstart' );
  300. $hide_options = array(
  301. 'jumpstart_activated',
  302. 'jetpack_action_taken',
  303. 'jumpstart_dismissed'
  304. );
  305. if ( ! $jumpstart_option || in_array( $jumpstart_option, $hide_options ) ) {
  306. return false;
  307. }
  308. return true;
  309. }
  310. /**
  311. * Gather data about the current user.
  312. *
  313. * @since 4.1.0
  314. *
  315. * @return array
  316. */
  317. function jetpack_current_user_data() {
  318. $current_user = wp_get_current_user();
  319. $is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
  320. $dotcom_data = Jetpack::get_connected_user_data();
  321. // Add connected user gravatar to the returned dotcom_data.
  322. $dotcom_data['avatar'] = get_avatar_url( $dotcom_data['email'], array( 'size' => 64, 'default' => 'mysteryman' ) );
  323. $current_user_data = array(
  324. 'isConnected' => Jetpack::is_user_connected( $current_user->ID ),
  325. 'isMaster' => $is_master_user,
  326. 'username' => $current_user->user_login,
  327. 'id' => $current_user->ID,
  328. 'wpcomUser' => $dotcom_data,
  329. 'gravatar' => get_avatar( $current_user->ID, 40, 'mm', '', array( 'force_display' => true ) ),
  330. 'permissions' => array(
  331. 'admin_page' => current_user_can( 'jetpack_admin_page' ),
  332. 'connect' => current_user_can( 'jetpack_connect' ),
  333. 'disconnect' => current_user_can( 'jetpack_disconnect' ),
  334. 'manage_modules' => current_user_can( 'jetpack_manage_modules' ),
  335. 'network_admin' => current_user_can( 'jetpack_network_admin_page' ),
  336. 'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
  337. 'edit_posts' => current_user_can( 'edit_posts' ),
  338. 'publish_posts' => current_user_can( 'publish_posts' ),
  339. 'manage_options' => current_user_can( 'manage_options' ),
  340. 'view_stats' => current_user_can( 'view_stats' ),
  341. 'manage_plugins' => current_user_can( 'install_plugins' )
  342. && current_user_can( 'activate_plugins' )
  343. && current_user_can( 'update_plugins' )
  344. && current_user_can( 'delete_plugins' ),
  345. ),
  346. );
  347. return $current_user_data;
  348. }