Nessuna descrizione

class-jetpack-search-customberg.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * A class that adds a search customization interface to wp-admin.
  4. *
  5. * @package automattic/jetpack
  6. */
  7. namespace Automattic\Jetpack\Search;
  8. use Automattic\Jetpack\Status;
  9. use Automattic\Jetpack\Tracking;
  10. use Jetpack;
  11. use Jetpack_Plan;
  12. use Jetpack_Search_Helpers;
  13. /**
  14. * Responsible for adding a search customization interface to wp-admin.
  15. *
  16. * @package Automattic\Jetpack\Search
  17. */
  18. class Jetpack_Search_Customberg {
  19. /**
  20. * The singleton instance of this class.
  21. *
  22. * @var Jetpack_Search_Customberg
  23. */
  24. protected static $instance;
  25. /**
  26. * Get the singleton instance of the class.
  27. *
  28. * @return Jetpack_Search_Customberg
  29. */
  30. public static function instance() {
  31. if ( ! isset( self::$instance ) ) {
  32. self::$instance = new static();
  33. self::$instance->init_hooks();
  34. }
  35. return self::$instance;
  36. }
  37. /**
  38. * Adds action hooks.
  39. */
  40. public function init_hooks() {
  41. add_action( 'admin_menu', array( $this, 'add_wp_admin_page' ), 999 );
  42. }
  43. /**
  44. * Adds a wp-admin page without adding a sidebar submenu item.
  45. */
  46. public function add_wp_admin_page() {
  47. if ( ! $this->should_add_page() ) {
  48. return;
  49. }
  50. // Intentionally omits adding a submenu via the first null argument.
  51. $hook = add_submenu_page(
  52. null,
  53. __( 'Search Settings', 'jetpack' ),
  54. __( 'Search', 'jetpack' ),
  55. 'manage_options', // Must be an admin.
  56. 'jetpack-search-configure',
  57. array( $this, 'jetpack_search_admin_page' )
  58. );
  59. // Only load assets if Customberg is supported.
  60. if ( $this->wp_supports_customberg() ) {
  61. add_action( "admin_print_scripts-$hook", array( $this, 'load_assets' ) );
  62. add_action( 'admin_footer', array( 'Jetpack_Search_Helpers', 'print_instant_search_sidebar' ) );
  63. } else {
  64. add_action( "admin_print_scripts-$hook", array( $this, 'add_redirect_if_necessary' ) );
  65. }
  66. }
  67. /**
  68. * Prints the dashboard container.
  69. */
  70. public function jetpack_search_admin_page() {
  71. // TODO: Spin this function off into a static helper function in a helper class for code reuse.
  72. $static_url = apply_filters( 'jetpack_static_url', '//en.wordpress.com/i/loading/loading-64.gif' );
  73. ?>
  74. <div id="jp-search-configure" class="jp-search-configure-dashboard" style="height: calc(100vh - 100px);">
  75. <div class="hide-if-no-js" style="height: 100%;">
  76. <img class="jp-search-loader" width="32" height="32" alt="<?php esc_attr_e( 'Loading&hellip;', 'jetpack' ); ?>" src="<?php echo esc_url( $static_url ); ?>" style="
  77. position: absolute;
  78. left: 50%;
  79. top: 50%;
  80. "/>
  81. </div>
  82. <div class="hide-if-js"><?php esc_html_e( 'Your Search customization page requires JavaScript to function properly.', 'jetpack' ); ?></div>
  83. </div>
  84. <?php
  85. }
  86. /**
  87. * Redirects to the Customizer if Customberg is not supported by the current host.
  88. */
  89. public function add_redirect_if_necessary() {
  90. // Add a JS redirect if Customberg is not supported.
  91. if ( ! $this->wp_supports_customberg() ) {
  92. ?>
  93. <script>
  94. window.location.href="<?php echo esc_url( admin_url( 'customize.php?autofocus[section]=jetpack_search' ) ); ?>";
  95. </script>
  96. <?php
  97. }
  98. }
  99. /**
  100. * Loads assets for the customization experience.
  101. */
  102. public function load_assets() {
  103. $this->load_assets_with_parameters( '', JETPACK__PLUGIN_FILE );
  104. }
  105. /**
  106. * Loads script and style assets according to parameters provided.
  107. *
  108. * @param string $path_prefix - Path prefix for built assets.
  109. * @param string $plugin_base_path - Base path for plugin files.
  110. */
  111. public function load_assets_with_parameters( $path_prefix, $plugin_base_path ) {
  112. $style_relative_path = $path_prefix . '_inc/build/instant-search/jp-search-configure-main.min.css';
  113. $manifest_relative_path = $path_prefix . '_inc/build/instant-search/jp-search-configure-main.min.asset.php';
  114. $script_relative_path = $path_prefix . '_inc/build/instant-search/jp-search-configure-main.min.js';
  115. //
  116. // Load styles.
  117. \Jetpack_Admin_Page::load_wrapper_styles();
  118. wp_enqueue_style(
  119. 'jp-search-configure',
  120. plugins_url( $style_relative_path, $plugin_base_path ),
  121. array(
  122. 'wp-components',
  123. 'wp-block-editor',
  124. ),
  125. JETPACK__VERSION
  126. );
  127. //
  128. // Load scripts.
  129. $manifest_path = plugin_dir_path( $plugin_base_path ) . $manifest_relative_path;
  130. $script_dependencies = array();
  131. if ( file_exists( $manifest_path ) ) {
  132. $asset_manifest = include $manifest_path;
  133. $script_dependencies = $asset_manifest['dependencies'];
  134. }
  135. Tracking::register_tracks_functions_scripts( true );
  136. wp_enqueue_script(
  137. 'jp-search-configure',
  138. plugins_url( $script_relative_path, $plugin_base_path ),
  139. $script_dependencies,
  140. JETPACK__VERSION,
  141. true
  142. );
  143. wp_set_script_translations( 'jp-search-configure', 'jetpack' );
  144. // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
  145. wp_add_inline_script( 'jp-search-configure', 'var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( Jetpack_Search_Helpers::generate_initial_javascript_state() ) ) . '"));', 'before' );
  146. wp_add_inline_script(
  147. 'jp-search-configure',
  148. "window.jetpackSearchConfigureInit( 'jp-search-configure' )"
  149. );
  150. }
  151. /**
  152. * Determine if the current version of WordPress supports Customberg.
  153. *
  154. * @return boolean
  155. */
  156. protected function wp_supports_customberg() {
  157. // Must be WP 5.8 or greater.
  158. global $wp_version;
  159. return version_compare( $wp_version, '5.8', '>=' );
  160. }
  161. /**
  162. * Determine if the requisite page should be added to wp-admin.
  163. *
  164. * @return boolean
  165. */
  166. protected function should_add_page() {
  167. $is_offline_mode = ( new Status() )->is_offline_mode();
  168. return (
  169. ! $is_offline_mode && // Must be online.
  170. Jetpack::is_connection_ready() && // Must be connected.
  171. method_exists( 'Jetpack_Plan', 'supports' ) && Jetpack_Plan::supports( 'search' ) // Must have plan supporting Jetpack (Instant) Search.
  172. );
  173. }
  174. }