PluginProbe
Jetpack Boost – Website Speed, Performance and Critical CSS / trunk
Jetpack Boost – Website Speed, Performance and Critical CSS vtrunk
4.7.0 4.7.0-beta 4.6.3 4.6.2 4.6.2-beta 3.12.0 3.12.1 3.13.0 3.13.1 3.2.0 3.2.1 3.2.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 All 124 releases
jetpack-boost / app / admin / class-admin.php

class-admin.php in Jetpack Boost – Website Speed, Performance and Critical CSS trunk, at app/admin/class-admin.php

257 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The admin-specific functionality of the plugin.
4 *
5 * @since 1.0.0
6 * @package automattic/jetpack-boost
7 */
8
9 namespace Automattic\Jetpack_Boost\Admin;
10
11 use Automattic\Jetpack\Admin_UI\Admin_Menu;
12 use Automattic\Jetpack\Assets;
13 use Automattic\Jetpack\Boost_Speed_Score\Speed_Score;
14 use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
15 use Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills;
16 use Automattic\Jetpack_Boost\Lib\Analytics;
17 use Automattic\Jetpack_Boost\Lib\Debug;
18 use Automattic\Jetpack_Boost\Lib\Environment_Change_Detector;
19 use Automattic\Jetpack_Boost\Lib\Premium_Features;
20 use Automattic\Jetpack_Boost\Modules\Modules_Setup;
21
22 class Admin {
23 /**
24 * Menu slug.
25 */
26 const MENU_SLUG = 'jetpack-boost';
27
28 /**
29 * Filter enabling the modern dashboard.
30 */
31 const MODERNIZATION_FILTER = 'rsm_jetpack_ui_modernization_boost';
32
33 /**
34 * Whether this request loaded the modern dashboard.
35 *
36 * @var bool
37 */
38 private $modern_dashboard_loaded = false;
39
40 public function init( Modules_Setup $modules ) {
41 Environment_Change_Detector::init();
42
43 // Initiate speed scores.
44 new Speed_Score( $modules->get_ready_active_optimization_modules(), 'boost-plugin' );
45
46 add_action( 'init', array( new Analytics(), 'init' ) );
47 add_filter( 'plugin_action_links_' . JETPACK_BOOST_PLUGIN_BASE, array( $this, 'plugin_page_settings_link' ) );
48 add_action( 'admin_menu', array( $this, 'handle_admin_menu' ), 1 ); // Akismet uses 4, so we use 1 to ensure both menus are added when only they exist.
49 }
50
51 public function handle_admin_menu() {
52 $this->maybe_load_wp_build();
53
54 /**
55 * Filters the number of problems shown in the Boost sidebar menu
56 *
57 * @param int $count the number of problems shown.
58 *
59 * @since 1.0.0
60 */
61 // Only report the count to users who can actually reach the Boost menu
62 // (added below with the 'manage_options' cap). Otherwise the central
63 // menu-badges total would include problems the current user can't see.
64 if ( current_user_can( 'manage_options' ) ) {
65 $total_problems = apply_filters( 'jetpack_boost_total_problem_count', 0 );
66 \Automattic\Jetpack\Menu_Badges\Menu_Badges::init(); // idempotent; wires the renderer.
67 \Automattic\Jetpack\Menu_Badges\Notification_Counts::register(
68 'jetpack-boost',
69 array(
70 'menu_slug' => JETPACK_BOOST_SLUG,
71 'count' => (int) $total_problems,
72 'type' => 'count',
73 )
74 );
75 }
76
77 $page_suffix = Admin_Menu::add_menu(
78 __( 'Jetpack Boost - Settings', 'jetpack-boost' ),
79 'Boost', // "Boost" is a product name, do not translate.
80 'manage_options',
81 JETPACK_BOOST_SLUG,
82 $this->modern_dashboard_loaded ? 'jetpack_boost_jetpack_boost_dashboard_wp_admin_render_page' : array( $this, 'render_settings' )
83 );
84 add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
85 }
86
87 /**
88 * Load the modern dashboard only on an opted-in Boost admin request.
89 */
90 private function maybe_load_wp_build() {
91 /**
92 * Enable the modern Boost dashboard.
93 *
94 * @since 4.7.1
95 * @param bool $enabled Whether to enable the modern dashboard. Default false.
96 */
97 if ( ! apply_filters( self::MODERNIZATION_FILTER, false ) || ! is_admin() ) {
98 return;
99 }
100
101 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
102 $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
103 if ( JETPACK_BOOST_SLUG !== $page || ! $this->dashboard_build_is_available() ) {
104 return;
105 }
106
107 WP_Build_Polyfills::register(
108 'jetpack-boost',
109 array_merge( WP_Build_Polyfills::SCRIPT_HANDLES, WP_Build_Polyfills::MODULE_IDS )
110 );
111
112 // wp_default_scripts has already fired by admin_menu, so register the init module now.
113 jetpack_boost_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Defined by the generated build and checked in dashboard_build_is_available().
114 add_action( 'current_screen', array( $this, 'alias_screen_id_for_wp_build' ) );
115 $this->modern_dashboard_loaded = true;
116 }
117
118 /**
119 * Load the generated dashboard, retaining the legacy page when assets are absent.
120 *
121 * @return bool Whether the generated dashboard is available.
122 */
123 private function dashboard_build_is_available() {
124 $build_file = JETPACK_BOOST_DIR_PATH . '/build/build.php';
125 if ( ! file_exists( $build_file ) ) {
126 Debug::log( 'Modern dashboard build is missing; loading the legacy dashboard.' );
127 return false;
128 }
129
130 require_once $build_file;
131
132 return function_exists( 'jetpack_boost_register_script_modules' )
133 && function_exists( 'jetpack_boost_jetpack_boost_dashboard_wp_admin_render_page' );
134 }
135
136 /**
137 * Match wp-build's enqueue screen without changing the Boost menu URL.
138 *
139 * @param \WP_Screen|null $screen Current screen.
140 */
141 public function alias_screen_id_for_wp_build( $screen ) {
142 if ( is_object( $screen ) ) {
143 $screen->id = 'jetpack-boost-dashboard';
144 }
145 }
146
147 /**
148 * Enqueue scripts and styles for the admin page.
149 */
150 public function admin_init() {
151 // Clear premium features cache when the plugin settings page is loaded.
152 Premium_Features::clear_cache();
153
154 add_action( 'admin_enqueue_scripts', array( My_Jetpack_Initializer::class, 'enqueue_scripts' ) );
155 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
156 }
157
158 /**
159 * Register the JavaScript for the admin area.
160 *
161 * @since 1.0.0
162 */
163 public function enqueue_scripts() {
164 /**
165 * Filters the internal path to the distributed assets used by the plugin
166 *
167 * @param string $path the path to the assets
168 */
169 $internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' );
170
171 $admin_js_handle = 'jetpack-boost-admin';
172
173 $admin_js_dependencies = array(
174 'wp-i18n',
175 'wp-components',
176 'my_jetpack_main_app',
177 );
178
179 Assets::register_script(
180 $admin_js_handle,
181 $internal_path . 'jetpack-boost.js',
182 JETPACK_BOOST_PATH,
183 array(
184 'dependencies' => $admin_js_dependencies,
185 'in_footer' => true,
186 'textdomain' => 'jetpack-boost',
187 'css_path' => $internal_path . 'jetpack-boost.css',
188 )
189 );
190
191 wp_localize_script(
192 $admin_js_handle,
193 'Jetpack_Boost',
194 ( new Config() )->constants()
195 );
196
197 Assets::enqueue_script( $admin_js_handle );
198
199 if ( $this->modern_dashboard_loaded ) {
200 $this->localize_api_settings( $admin_js_handle );
201 $i18n_loader_registered = wp_script_is( 'wp-jp-i18n-loader', 'registered' );
202 if ( $i18n_loader_registered ) {
203 wp_enqueue_script( 'wp-jp-i18n-loader' );
204 }
205
206 // The webpack handle carries Boost constants and DataSync bootstrap needed before modules run.
207 $prerequisites = wp_scripts()->query( 'jetpack-boost-dashboard-wp-admin-prerequisites', 'registered' );
208 if ( $prerequisites ) {
209 $prerequisites->deps[] = $admin_js_handle;
210 if ( $i18n_loader_registered ) {
211 $prerequisites->deps[] = 'wp-jp-i18n-loader';
212 }
213 } else {
214 Debug::log( 'Modern dashboard prerequisites are not registered; bootstrap dependencies could not be attached.' );
215 }
216 }
217 }
218
219 /**
220 * Get settings link.
221 *
222 * @param array $links the array of links.
223 */
224 public function plugin_page_settings_link( $links ) {
225 $settings_link = '<a href="' . admin_url( 'admin.php?page=jetpack-boost' ) . '">' . esc_html__( 'Settings', 'jetpack-boost' ) . '</a>';
226 array_unshift( $links, $settings_link );
227
228 return $links;
229 }
230
231 /**
232 * Localize the REST API settings for a script.
233 *
234 * @param string $handle Script handle.
235 */
236 private function localize_api_settings( $handle ) {
237 wp_localize_script(
238 $handle,
239 'wpApiSettings',
240 array(
241 'root' => esc_url_raw( rest_url() ),
242 'nonce' => wp_create_nonce( 'wp_rest' ),
243 )
244 );
245 }
246
247 /**
248 * Generate the settings page.
249 */
250 public function render_settings() {
251 $this->localize_api_settings( 'jetpack-boost-admin' );
252 ?>
253 <div id="jb-admin-settings"></div>
254 <?php
255 }
256 }
257