PluginProbe
Performance Lab / 1.8.0
Performance Lab v1.8.0
trunk 1.0.0 1.0.0-beta.1 1.0.0-beta.2 1.0.0-beta.3 1.0.0-rc.1 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 2.1.0 2.2.0 2.3.0 2.4.0 2.5.0 2.6.0 2.6.1 2.7.0 2.8.0 All 44 releases
performance-lab / admin / load.php

load.php in Performance Lab 1.8.0, at admin/load.php

490 lines 16.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin integration file
4 *
5 * @package performance-lab
6 */
7
8 /**
9 * Adds the modules page to the Settings menu.
10 *
11 * @since 1.0.0
12 */
13 function perflab_add_modules_page() {
14 // Don't add a page if active modules are controlled programmatically.
15 if ( has_filter( 'perflab_active_modules' ) ) {
16 return false;
17 }
18
19 $hook_suffix = add_options_page(
20 __( 'Performance Modules', 'performance-lab' ),
21 __( 'Performance', 'performance-lab' ),
22 'manage_options',
23 PERFLAB_MODULES_SCREEN,
24 'perflab_render_modules_page'
25 );
26
27 // Add the following hooks only if the screen was successfully added.
28 if ( false !== $hook_suffix ) {
29 add_action( "load-{$hook_suffix}", 'perflab_load_modules_page', 10, 0 );
30 add_action( 'plugin_action_links_' . plugin_basename( PERFLAB_MAIN_FILE ), 'perflab_plugin_action_links_add_settings' );
31 }
32
33 return $hook_suffix;
34 }
35 add_action( 'admin_menu', 'perflab_add_modules_page' );
36
37 /**
38 * Initializes settings sections and fields for the modules page.
39 *
40 * @global array $wp_settings_sections Registered WordPress settings sections.
41 *
42 * @since 1.0.0
43 *
44 * @param array|null $modules Associative array of available module data, keyed by module slug. By default, this
45 * will rely on {@see perflab_get_modules()}.
46 * @param array|null $focus_areas Associative array of focus area data, keyed by focus area slug. By default, this will
47 * rely on {@see perflab_get_focus_areas()}.
48 */
49 function perflab_load_modules_page( $modules = null, $focus_areas = null ) {
50 global $wp_settings_sections;
51
52 // Register sections for all focus areas, plus 'Other'.
53 if ( ! is_array( $focus_areas ) ) {
54 $focus_areas = perflab_get_focus_areas();
55 }
56 $sections = $focus_areas;
57 $sections['other'] = array( 'name' => __( 'Other', 'performance-lab' ) );
58 foreach ( $sections as $section_slug => $section_data ) {
59 add_settings_section(
60 $section_slug,
61 $section_data['name'],
62 null,
63 PERFLAB_MODULES_SCREEN
64 );
65 }
66
67 // Register fields for all modules.
68 if ( ! is_array( $modules ) ) {
69 $modules = perflab_get_modules();
70 }
71 $settings = perflab_get_module_settings();
72 foreach ( $modules as $module_slug => $module_data ) {
73 $module_settings = isset( $settings[ $module_slug ] ) ? $settings[ $module_slug ] : array();
74 $module_section = isset( $sections[ $module_data['focus'] ] ) ? $module_data['focus'] : 'other';
75
76 // Mark this module's section as added.
77 $sections[ $module_section ]['added'] = true;
78
79 add_settings_field(
80 $module_slug,
81 $module_data['name'],
82 function() use ( $module_slug, $module_data, $module_settings ) {
83 perflab_render_modules_page_field( $module_slug, $module_data, $module_settings );
84 },
85 PERFLAB_MODULES_SCREEN,
86 $module_section
87 );
88 }
89
90 // Remove all sections for which there are no modules.
91 foreach ( $sections as $section_slug => $section_data ) {
92 if ( empty( $section_data['added'] ) ) {
93 unset( $wp_settings_sections[ PERFLAB_MODULES_SCREEN ][ $section_slug ] );
94 }
95 }
96 }
97
98 /**
99 * Renders the modules page.
100 *
101 * @since 1.0.0
102 */
103 function perflab_render_modules_page() {
104 ?>
105 <div class="wrap">
106 <h1>
107 <?php esc_html_e( 'Performance Modules', 'performance-lab' ); ?>
108 </h1>
109
110 <form action="options.php" method="post" novalidate="novalidate">
111 <?php settings_fields( PERFLAB_MODULES_SCREEN ); ?>
112 <?php do_settings_sections( PERFLAB_MODULES_SCREEN ); ?>
113 <?php submit_button(); ?>
114 </form>
115 </div>
116 <?php
117 }
118
119 /**
120 * Renders fields for a given module on the modules page.
121 *
122 * @since 1.0.0
123 *
124 * @param string $module_slug Slug of the module.
125 * @param array $module_data Associative array of the module's parsed data.
126 * @param array $module_settings Associative array of the module's current settings.
127 */
128 function perflab_render_modules_page_field( $module_slug, $module_data, $module_settings ) {
129 $base_id = sprintf( 'module_%s', $module_slug );
130 $base_name = sprintf( '%1$s[%2$s]', PERFLAB_MODULES_SETTING, $module_slug );
131 $enabled = isset( $module_settings['enabled'] ) && $module_settings['enabled'];
132 ?>
133 <fieldset>
134 <legend class="screen-reader-text">
135 <?php echo esc_html( $module_data['name'] ); ?>
136 </legend>
137 <label for="<?php echo esc_attr( "{$base_id}_enabled" ); ?>">
138 <?php if ( perflab_can_load_module( $module_slug ) ) { ?>
139 <input type="checkbox" id="<?php echo esc_attr( "{$base_id}_enabled" ); ?>" name="<?php echo esc_attr( "{$base_name}[enabled]" ); ?>" aria-describedby="<?php echo esc_attr( "{$base_id}_description" ); ?>" value="1"<?php checked( $enabled ); ?>>
140 <?php
141 if ( $module_data['experimental'] ) {
142 printf(
143 /* translators: %s: module name */
144 __( 'Enable %s <strong>(experimental)</strong>', 'performance-lab' ),
145 esc_html( $module_data['name'] )
146 );
147 } else {
148 printf(
149 /* translators: %s: module name */
150 __( 'Enable %s', 'performance-lab' ),
151 esc_html( $module_data['name'] )
152 );
153 }
154 ?>
155 <?php } else { ?>
156 <input type="checkbox" id="<?php echo esc_attr( "{$base_id}_enabled" ); ?>" aria-describedby="<?php echo esc_attr( "{$base_id}_description" ); ?>" disabled>
157 <input type="hidden" name="<?php echo esc_attr( "{$base_name}[enabled]" ); ?>" value="<?php echo $enabled ? '1' : '0'; ?>">
158 <?php
159 if ( 'database/sqlite' === $module_slug && file_exists( WP_CONTENT_DIR . '/db.php' ) && ! defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) {
160 printf(
161 /* translators: %s: db.php drop-in path */
162 esc_html__( 'The SQLite module cannot be activated because a different %s drop-in already exists.', 'performance-lab' ),
163 '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '/db.php</code>'
164 );
165 } elseif ( 'database/sqlite' === $module_slug && ! wp_is_writable( WP_CONTENT_DIR ) ) {
166 printf(
167 /* translators: %s: db.php drop-in path */
168 esc_html__( 'The SQLite module cannot be activated because the %s directory is not writable.', 'performance-lab' ),
169 '<code>' . esc_html( basename( WP_CONTENT_DIR ) ) . '</code>'
170 );
171 } elseif ( 'database/sqlite' === $module_slug && ! class_exists( 'SQLite3' ) ) {
172 esc_html_e( 'The SQLite module cannot be activated because the SQLite extension is not loaded.', 'performance-lab' );
173 } else {
174 printf(
175 /* translators: %s: module name */
176 __( '%s is already part of your WordPress version and therefore cannot be loaded as part of the plugin.', 'performance-lab' ),
177 esc_html( $module_data['name'] )
178 );
179 }
180 ?>
181 <?php } ?>
182 </label>
183 <p id="<?php echo esc_attr( "{$base_id}_description" ); ?>" class="description">
184 <?php echo esc_html( $module_data['description'] ); ?>
185 </p>
186 <?php if ( 'database/sqlite' === $module_slug ) : ?>
187 <?php if ( $enabled ) : ?>
188 <?php if ( defined( 'PERFLAB_SQLITE_DB_DROPIN_VERSION' ) ) : ?>
189 <?php // Don't use the WP notice classes here, as that makes them move to the top of the page. ?>
190 <p class="notice notice-warning" style="padding:1em;max-width:50em;">
191 <?php esc_html_e( 'Your site is currently using an SQLite database. You can disable this module to get back to your previous MySQL database, with all your previous data intact.', 'performance-lab' ); ?>
192 </p>
193 <?php endif; ?>
194 <?php else : ?>
195 <?php // Don't use the WP notice classes here, as that makes them move to the top of the page. ?>
196 <p class="notice notice-warning" style="padding:1em;max-width:50em;">
197 <?php esc_html_e( 'Enabling this module will switch to a separate database and install WordPress in it. You will need to reconfigure your site, and start with a fresh site. Disabling the module you will get back to your previous MySQL database, with all your previous data intact. Warning: Do not activate if your site is implemented in a multi-server environment.', 'performance-lab' ); ?>
198 </p>
199 <?php endif; ?>
200 <?php endif; ?>
201 </fieldset>
202 <?php
203 }
204
205 /**
206 * Gets all available focus areas.
207 *
208 * @since 1.0.0
209 *
210 * @return array Associative array of focus area data, keyed by focus area slug. Fields for every focus area include
211 * 'name'.
212 */
213 function perflab_get_focus_areas() {
214 return array(
215 'images' => array(
216 'name' => __( 'Images', 'performance-lab' ),
217 ),
218 'js-and-css' => array(
219 'name' => __( 'JS & CSS', 'performance-lab' ),
220 ),
221 'database' => array(
222 'name' => __( 'Database', 'performance-lab' ),
223 ),
224 'measurement' => array(
225 'name' => __( 'Measurement', 'performance-lab' ),
226 ),
227 'object-cache' => array(
228 'name' => __( 'Object Cache', 'performance-lab' ),
229 ),
230 );
231 }
232
233 /**
234 * Gets all available modules.
235 *
236 * This function iterates through the modules directory and therefore should only be called on the modules page.
237 * It searches all modules, similar to how plugins are searched in the WordPress core function `get_plugins()`.
238 *
239 * @since 1.0.0
240 *
241 * @param string $modules_root Modules root directory to look for modules in. Default is the `/modules` directory
242 * in the plugin's root.
243 * @return array Associative array of parsed module data, keyed by module slug. Fields for every module include
244 * 'name', 'description', 'focus', and 'experimental'.
245 */
246 function perflab_get_modules( $modules_root = null ) {
247 if ( null === $modules_root ) {
248 $modules_root = dirname( __DIR__ ) . '/modules';
249 }
250
251 $modules = array();
252 $module_files = array();
253 $modules_dir = @opendir( $modules_root );
254
255 // Modules are organized as {focus}/{module-slug} in the modules folder.
256 if ( $modules_dir ) {
257 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
258 while ( ( $focus = readdir( $modules_dir ) ) !== false ) {
259 if ( '.' === substr( $focus, 0, 1 ) ) {
260 continue;
261 }
262
263 // Each focus area must be a directory.
264 if ( ! is_dir( $modules_root . '/' . $focus ) ) {
265 continue;
266 }
267
268 $focus_dir = @opendir( $modules_root . '/' . $focus );
269 if ( $focus_dir ) {
270 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
271 while ( ( $file = readdir( $focus_dir ) ) !== false ) {
272 // Unlike plugins, modules must be in a directory.
273 if ( ! is_dir( $modules_root . '/' . $focus . '/' . $file ) ) {
274 continue;
275 }
276
277 $module_dir = @opendir( $modules_root . '/' . $focus . '/' . $file );
278 if ( $module_dir ) {
279 // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
280 while ( ( $subfile = readdir( $module_dir ) ) !== false ) {
281 if ( '.' === substr( $subfile, 0, 1 ) ) {
282 continue;
283 }
284
285 // Unlike plugins, module main files must be called `load.php`.
286 if ( 'load.php' !== $subfile ) {
287 continue;
288 }
289
290 $module_files[] = "$focus/$file/$subfile";
291 }
292
293 closedir( $module_dir );
294 }
295 }
296
297 closedir( $focus_dir );
298 }
299 }
300
301 closedir( $modules_dir );
302 }
303
304 foreach ( $module_files as $module_file ) {
305 if ( ! is_readable( "$modules_root/$module_file" ) ) {
306 continue;
307 }
308 $module_dir = dirname( $module_file );
309 $module_data = perflab_get_module_data( "$modules_root/$module_file" );
310 if ( ! $module_data ) {
311 continue;
312 }
313
314 $modules[ $module_dir ] = $module_data;
315 }
316
317 uasort(
318 $modules,
319 function( $a, $b ) {
320 return strnatcasecmp( $a['name'], $b['name'] );
321 }
322 );
323
324 return $modules;
325 }
326
327 /**
328 * Parses the module main file to get the module's metadata.
329 *
330 * This is similar to how plugin data is parsed in the WordPress core function `get_plugin_data()`.
331 * The user-facing strings will be translated.
332 *
333 * @since 1.0.0
334 *
335 * @param string $module_file Absolute path to the main module file.
336 * @return array|bool Associative array of parsed module data, or false on failure. Fields for every module include
337 * 'name', 'description', 'focus', and 'experimental'.
338 */
339 function perflab_get_module_data( $module_file ) {
340 // Extract the module dir in the form {focus}/{module-slug}.
341 preg_match( '/.*\/(.*\/.*)\/load\.php$/i', $module_file, $matches );
342 $module_dir = $matches[1];
343
344 $default_headers = array(
345 'name' => 'Module Name',
346 'description' => 'Description',
347 'experimental' => 'Experimental',
348 );
349
350 $module_data = get_file_data( $module_file, $default_headers, 'perflab_module' );
351
352 // Module name and description are the minimum requirements.
353 if ( ! $module_data['name'] || ! $module_data['description'] ) {
354 return false;
355 }
356
357 // Experimental should be a boolean.
358 if ( 'yes' === strtolower( trim( $module_data['experimental'] ) ) ) {
359 $module_data['experimental'] = true;
360 } else {
361 $module_data['experimental'] = false;
362 }
363
364 // Extract the module focus from the module directory.
365 if ( strpos( $module_dir, '/' ) ) {
366 list( $focus, $slug ) = explode( '/', $module_dir );
367 $module_data['focus'] = $focus;
368 $module_data['slug'] = $slug;
369 }
370
371 // Translate fields using low-level function since they come from PHP comments, including the necessary context for
372 // `_x()`. This must match how these are translated in the generated `/module-i18n.php` file.
373 $translatable_fields = array(
374 'name' => 'module name',
375 'description' => 'module description',
376 );
377 foreach ( $translatable_fields as $field => $context ) {
378 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralText
379 $module_data[ $field ] = translate_with_gettext_context( $module_data[ $field ], $context, 'performance-lab' );
380 }
381
382 return $module_data;
383 }
384
385 /**
386 * Initialise Admin Pointer
387 *
388 * Handles the bootstrapping of the admin pointer.
389 * Mainly jQuery code that is self-initialising.
390 *
391 * @param string $hook_suffix The current admin page.
392 * @since 1.0.0
393 */
394 function perflab_admin_pointer( $hook_suffix ) {
395 if ( ! in_array( $hook_suffix, array( 'index.php', 'plugins.php' ), true ) ) {
396 return;
397 }
398
399 // Do not show admin pointer in multisite Network admin or User admin UI.
400 if ( is_network_admin() || is_user_admin() ) {
401 return;
402 }
403
404 $current_user = get_current_user_id();
405 $dismissed = explode( ',', (string) get_user_meta( $current_user, 'dismissed_wp_pointers', true ) );
406
407 if ( in_array( 'perflab-admin-pointer', $dismissed, true ) ) {
408 return;
409 }
410
411 // Enqueue pointer CSS and JS.
412 wp_enqueue_style( 'wp-pointer' );
413 wp_enqueue_script( 'wp-pointer' );
414 add_action( 'admin_print_footer_scripts', 'perflab_render_pointer' );
415 }
416 add_action( 'admin_enqueue_scripts', 'perflab_admin_pointer' );
417
418 /**
419 * Renders the Admin Pointer.
420 *
421 * Handles the rendering of the admin pointer.
422 *
423 * @since 1.0.0
424 */
425 function perflab_render_pointer() {
426 $heading = __( 'Performance Lab', 'performance-lab' );
427 $wp_kses_options = array(
428 'a' => array(
429 'href' => array(),
430 ),
431 );
432
433 $content = sprintf(
434 /* translators: %s: settings page link */
435 __( 'You can now test upcoming WordPress performance features. Open %s to individually toggle the performance features included in the plugin.', 'performance-lab' ),
436 '<a href="' . esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ) . '">' . __( 'Settings > Performance', 'performance-lab' ) . '</a>'
437 );
438
439 ?>
440 <script id="perflab-admin-pointer" type="text/javascript">
441 jQuery( function() {
442 // Pointer Options.
443 var options = {
444 content: '<h3><?php echo esc_js( $heading ); ?></h3><p><?php echo wp_kses( $content, $wp_kses_options ); ?></p>',
445 position: {
446 edge: 'left',
447 align: 'right',
448 },
449 pointerClass: 'wp-pointer arrow-top',
450 pointerWidth: 420,
451 close: function() {
452 jQuery.post(
453 window.ajaxurl,
454 {
455 pointer: 'perflab-admin-pointer',
456 action: 'dismiss-wp-pointer',
457 }
458 );
459 }
460 };
461
462 jQuery( '#menu-settings' ).pointer( options ).pointer( 'open' );
463 } );
464 </script>
465 <?php
466 }
467
468 /**
469 * Adds a link to the modules page to the plugin's entry in the plugins list table.
470 *
471 * This function is only used if the modules page exists and is accessible.
472 *
473 * @since 1.0.0
474 * @see perflab_add_modules_page()
475 *
476 * @param array $links List of plugin action links HTML.
477 * @return array Modified list of plugin action links HTML.
478 */
479 function perflab_plugin_action_links_add_settings( $links ) {
480 // Add link as the first plugin action link.
481 $settings_link = sprintf(
482 '<a href="%s">%s</a>',
483 esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ),
484 esc_html__( 'Settings', 'performance-lab' )
485 );
486 array_unshift( $links, $settings_link );
487
488 return $links;
489 }
490