No Description

class-jetpack-search-dashboard-page.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * A class that adds a search dashboard to wp-admin.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. use Automattic\Jetpack\Status;
  8. /**
  9. * Requires files needed.
  10. */
  11. require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class.jetpack-admin-page.php';
  12. require_once JETPACK__PLUGIN_DIR . '_inc/lib/admin-pages/class-jetpack-redux-state-helper.php';
  13. /**
  14. * Responsible for adding a search dashboard to wp-admin.
  15. *
  16. * @package Automattic\Jetpack\Search
  17. */
  18. class Jetpack_Search_Dashboard_Page extends Jetpack_Admin_Page {
  19. /**
  20. * Show the settings page only when Jetpack is connected or in dev mode.
  21. *
  22. * @var bool If the page should be shown.
  23. */
  24. protected $dont_show_if_not_active = true;
  25. /**
  26. * Add page specific actions given the page hook.
  27. *
  28. * @param {object} $hook The page hook.
  29. */
  30. public function add_page_actions( $hook ) {}// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  31. /**
  32. * Create a menu item for the page and returns the hook.
  33. */
  34. public function get_page_hook() {
  35. if ( ! $this->should_add_sub_menu() ) {
  36. return;
  37. }
  38. return add_submenu_page(
  39. 'jetpack',
  40. __( 'Search Settings', 'jetpack' ),
  41. _x( 'Search', 'product name shown in menu', 'jetpack' ),
  42. 'manage_options',
  43. 'jetpack-search',
  44. array( $this, 'render' ),
  45. $this->get_link_offset()
  46. );
  47. }
  48. /**
  49. * Enqueue and localize page specific scripts
  50. */
  51. public function page_admin_scripts() {
  52. $this->load_admin_styles();
  53. $this->load_admin_scripts();
  54. }
  55. /**
  56. * Override render funtion
  57. */
  58. public function render() {
  59. $this->page_render();
  60. }
  61. /**
  62. * Render Search setting elements
  63. */
  64. public function page_render() {
  65. ?>
  66. <div id="jp-search-dashboard" class="jp-search-dashboard">
  67. <div class="hide-if-js"><?php esc_html_e( 'Your Search dashboard requires JavaScript to function properly.', 'jetpack' ); ?></div>
  68. </div>
  69. <?php
  70. }
  71. /**
  72. * Test whether we should show Search menu.
  73. *
  74. * @return {boolean} Show search sub menu or not.
  75. */
  76. protected function should_add_sub_menu() {
  77. return method_exists( 'Jetpack_Plan', 'supports' ) && Jetpack_Plan::supports( 'search' );
  78. }
  79. /**
  80. * Place the Jetpack Search menu item at the bottom of the Jetpack submenu.
  81. *
  82. * @return int Menu offset.
  83. */
  84. private function get_link_offset() {
  85. global $submenu;
  86. return count( $submenu['jetpack'] );
  87. }
  88. /**
  89. * Enqueue admin styles.
  90. */
  91. public function load_admin_styles() {
  92. \Jetpack_Admin_Page::load_wrapper_styles();
  93. wp_enqueue_style(
  94. 'jp-search-dashboard',
  95. plugins_url( '_inc/build/search-dashboard.css', JETPACK__PLUGIN_FILE ),
  96. array(),
  97. JETPACK__VERSION
  98. );
  99. }
  100. /**
  101. * Enqueue admin scripts.
  102. */
  103. public function load_admin_scripts() {
  104. $script_deps_path = JETPACK__PLUGIN_DIR . '_inc/build/search-dashboard.asset.php';
  105. $script_dependencies = array( 'react', 'react-dom', 'wp-polyfill' );
  106. if ( file_exists( $script_deps_path ) ) {
  107. $asset_manifest = include $script_deps_path;
  108. $script_dependencies = $asset_manifest['dependencies'];
  109. }
  110. if ( ! ( new Status() )->is_offline_mode() && Jetpack::is_connection_ready() ) {
  111. // Required for Analytics.
  112. Automattic\Jetpack\Tracking::register_tracks_functions_scripts( true );
  113. }
  114. wp_enqueue_script(
  115. 'jp-search-dashboard',
  116. plugins_url( '_inc/build/search-dashboard.js', JETPACK__PLUGIN_FILE ),
  117. $script_dependencies,
  118. JETPACK__VERSION,
  119. true
  120. );
  121. // Add objects to be passed to the initial state of the app.
  122. // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
  123. wp_add_inline_script(
  124. 'jp-search-dashboard',
  125. 'var Initial_State=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( \Jetpack_Redux_State_Helper::get_initial_state() ) ) . '"));',
  126. 'before'
  127. );
  128. wp_set_script_translations( 'jp-search-dashboard', 'jetpack' );
  129. }
  130. }