PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-search / src / dashboard / class-dashboard.php

class-dashboard.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-search/src/dashboard/class-dashboard.php

222 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A class that adds a search dashboard to wp-admin.
4 *
5 * @package automattic/jetpack
6 */
7
8 namespace Automattic\Jetpack\Search;
9
10 use Automattic\Jetpack\Admin_UI\Admin_Menu;
11 use Automattic\Jetpack\Assets;
12 use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
13 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
14 use Automattic\Jetpack\Status;
15 use Automattic\Jetpack\Tracking;
16 /**
17 * Responsible for adding a search dashboard to wp-admin.
18 *
19 * @package Automattic\Jetpack\Search
20 */
21 class Dashboard {
22 /**
23 * Whether the class has been initialized
24 *
25 * @var boolean
26 */
27 private static $initialized = false;
28 /**
29 * Plan instance
30 *
31 * @var \Automattic\Jetpack\Search\Plan
32 */
33 protected $plan;
34
35 /**
36 * Connection manager instance
37 *
38 * @var \Automattic\Jetpack\Connection\Manager
39 */
40 protected $connection_manager;
41
42 /**
43 * Module_Control instance
44 *
45 * @var \Automattic\Jetpack\Search\Module_Control
46 */
47 protected $module_control;
48
49 /**
50 * Priority for the dashboard menu
51 * For Jetpack sites: Akismet uses 4, so we use 1 to ensure both menus are added when only they exist.
52 * For Simple sites: the value is overriden in a child class with value 100000 to wait for all menus to be registered.
53 *
54 * @var int
55 */
56 protected $search_menu_priority = 1;
57
58 /**
59 * Contructor
60 *
61 * @param \Automattic\Jetpack\Search\Plan $plan - Plan instance.
62 * @param \Automattic\Jetpack\Connection\Manager $connection_manager - Connection Manager instance.
63 * @param \Automattic\Jetpack\Search\Module_Control $module_control - Module_Control instance.
64 */
65 public function __construct( $plan = null, $connection_manager = null, $module_control = null ) {
66 $this->plan = $plan ? $plan : new Plan();
67 $this->connection_manager = $connection_manager ? $connection_manager : new Connection_Manager( Package::SLUG );
68 $this->module_control = $module_control ? $module_control : new Module_Control( $this->plan );
69 $this->plan->init_hooks();
70 }
71
72 /**
73 * Initialise hooks.
74 *
75 * We use the `config` package to initialize the search package, which ensures the package is
76 * only initialized once. However earlier versions of Jetpack would still forcely initialize the
77 * dashboard. As a result, there would be two `Search` submenus if we don't ensure the dashboard
78 * is initialized only once. So we use `$initialized` to ensure the class is only initialized once.
79 *
80 * Ref: https://github.com/Automattic/jetpack/pull/21888/files#diff-aae7d66951585fc55053a4d53b68552a41864d2c69aee900574ef4404b7ad5f7L42
81 */
82 public function init_hooks() {
83 if ( ! self::$initialized ) {
84 self::$initialized = true;
85 add_action( 'admin_menu', array( $this, 'add_wp_admin_submenu' ), $this->search_menu_priority );
86 // Check if the site plan changed and deactivate module accordingly.
87 add_action( 'current_screen', array( $this, 'check_plan_deactivate_search_module' ) );
88 }
89 }
90
91 /**
92 * The page to be added to submenu
93 */
94 public function add_wp_admin_submenu() {
95 // Jetpack of version <= 10.5 would register `jetpack-search` submenu with its built-in search module.
96 $this->remove_search_submenu_if_exists();
97
98 if ( $this->should_add_search_submenu() ) {
99 $page_suffix = Admin_Menu::add_menu(
100 /** "Search" is a product name, do not translate. */
101 'Jetpack Search',
102 'Search',
103 'manage_options',
104 'jetpack-search',
105 array( $this, 'render' )
106 );
107 } else {
108 // always add the page, but hide it from the menu.
109 $page_suffix = add_submenu_page(
110 '',
111 /** "Search" is a product name, do not translate. */
112 'Jetpack Search',
113 'Search',
114 'manage_options',
115 'jetpack-search',
116 array( $this, 'render' )
117 );
118 }
119
120 if ( $page_suffix ) {
121 add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
122 }
123 }
124
125 /**
126 * Override render funtion
127 */
128 public function render() {
129 ?>
130 <div id="jp-search-dashboard" class="jp-search-dashboard">
131 <div class="hide-if-js"><?php esc_html_e( 'Your Jetpack Search dashboard requires JavaScript to function properly.', 'jetpack-search-pkg' ); ?></div>
132 </div>
133 <?php
134 }
135
136 /**
137 * Test whether we should show Search menu.
138 *
139 * @return boolean Show search sub menu or not.
140 */
141 protected function should_add_search_submenu() {
142 /**
143 * The filter allows to ommit adding a submenu item for Jetpack Search.
144 *
145 * @since 0.11.2
146 *
147 * @param boolean $should_add_search_submenu Default value is true.
148 */
149 return apply_filters( 'jetpack_search_should_add_search_submenu', current_user_can( 'manage_options' ) );
150 }
151
152 /**
153 * Remove `jetpack-search` submenu page
154 */
155 protected function remove_search_submenu_if_exists() {
156 remove_submenu_page( 'jetpack', 'jetpack-search' );
157 }
158
159 /**
160 * Initialize the admin resources.
161 */
162 public function admin_init() {
163 add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ) );
164 }
165
166 /**
167 * Enqueue admin scripts.
168 */
169 public function load_admin_scripts() {
170 if ( $this->should_enqueue_tracking_script() ) {
171 // Required for Analytics.
172 Tracking::register_tracks_functions_scripts( true );
173 }
174
175 Assets::register_script(
176 'jp-search-dashboard',
177 '../../build/dashboard/jp-search-dashboard.js',
178 __FILE__,
179 array(
180 'in_footer' => true,
181 'textdomain' => 'jetpack-search-pkg',
182 )
183 );
184 Assets::enqueue_script( 'jp-search-dashboard' );
185
186 // Add objects to be passed to the initial state of the app.
187 // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
188 wp_add_inline_script(
189 'jp-search-dashboard',
190 ( new Initial_State() )->render(),
191 'before'
192 );
193
194 // Connection initial state.
195 Connection_Initial_State::render_script( 'jp-search-dashboard' );
196 }
197
198 /**
199 * Check if we should enqueue the tracking script.
200 */
201 protected function should_enqueue_tracking_script() {
202 return ! ( new Status() )->is_offline_mode() && $this->connection_manager->is_connected();
203 }
204
205 /**
206 * Deactivate search module if plan doesn't support search.
207 *
208 * @param \WP_Screen $current_screen Creent screen object.
209 */
210 public function check_plan_deactivate_search_module( $current_screen ) {
211 // Only run on Jetpack admin pages.
212 // The first two checks for current screen are cheap to run on every page.
213 if (
214 property_exists( $current_screen, 'base' ) &&
215 strpos( $current_screen->base, 'jetpack_page_' ) !== false &&
216 ( ! $this->plan->supports_search() || $this->plan->must_upgrade() )
217 ) {
218 $this->module_control->deactivate();
219 }
220 }
221 }
222