| 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 |
printf( |
| 160 |
/* translators: %s: module name */ |
| 161 |
__( '%s is already part of your WordPress version and therefore cannot be loaded as part of the plugin.', 'performance-lab' ), |
| 162 |
esc_html( $module_data['name'] ) |
| 163 |
); |
| 164 |
?> |
| 165 |
<?php } ?> |
| 166 |
</label> |
| 167 |
<p id="<?php echo esc_attr( "{$base_id}_description" ); ?>" class="description"> |
| 168 |
<?php echo esc_html( $module_data['description'] ); ?> |
| 169 |
</p> |
| 170 |
</fieldset> |
| 171 |
<?php |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Gets all available focus areas. |
| 176 |
* |
| 177 |
* @since 1.0.0 |
| 178 |
* |
| 179 |
* @return array Associative array of focus area data, keyed by focus area slug. Fields for every focus area include |
| 180 |
* 'name'. |
| 181 |
*/ |
| 182 |
function perflab_get_focus_areas() { |
| 183 |
return array( |
| 184 |
'images' => array( |
| 185 |
'name' => __( 'Images', 'performance-lab' ), |
| 186 |
), |
| 187 |
'javascript' => array( |
| 188 |
'name' => __( 'JavaScript', 'performance-lab' ), |
| 189 |
), |
| 190 |
'site-health' => array( |
| 191 |
'name' => __( 'Site Health', 'performance-lab' ), |
| 192 |
), |
| 193 |
'measurement' => array( |
| 194 |
'name' => __( 'Measurement', 'performance-lab' ), |
| 195 |
), |
| 196 |
'object-cache' => array( |
| 197 |
'name' => __( 'Object Cache', 'performance-lab' ), |
| 198 |
), |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Gets all available modules. |
| 204 |
* |
| 205 |
* This function iterates through the modules directory and therefore should only be called on the modules page. |
| 206 |
* It searches all modules, similar to how plugins are searched in the WordPress core function `get_plugins()`. |
| 207 |
* |
| 208 |
* @since 1.0.0 |
| 209 |
* |
| 210 |
* @param string $modules_root Modules root directory to look for modules in. Default is the `/modules` directory |
| 211 |
* in the plugin's root. |
| 212 |
* @return array Associative array of parsed module data, keyed by module slug. Fields for every module include |
| 213 |
* 'name', 'description', 'focus', and 'experimental'. |
| 214 |
*/ |
| 215 |
function perflab_get_modules( $modules_root = null ) { |
| 216 |
if ( null === $modules_root ) { |
| 217 |
$modules_root = dirname( __DIR__ ) . '/modules'; |
| 218 |
} |
| 219 |
|
| 220 |
$modules = array(); |
| 221 |
$module_files = array(); |
| 222 |
$modules_dir = @opendir( $modules_root ); |
| 223 |
|
| 224 |
// Modules are organized as {focus}/{module-slug} in the modules folder. |
| 225 |
if ( $modules_dir ) { |
| 226 |
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
| 227 |
while ( ( $focus = readdir( $modules_dir ) ) !== false ) { |
| 228 |
if ( '.' === substr( $focus, 0, 1 ) ) { |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
// Each focus area must be a directory. |
| 233 |
if ( ! is_dir( $modules_root . '/' . $focus ) ) { |
| 234 |
continue; |
| 235 |
} |
| 236 |
|
| 237 |
$focus_dir = @opendir( $modules_root . '/' . $focus ); |
| 238 |
if ( $focus_dir ) { |
| 239 |
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
| 240 |
while ( ( $file = readdir( $focus_dir ) ) !== false ) { |
| 241 |
// Unlike plugins, modules must be in a directory. |
| 242 |
if ( ! is_dir( $modules_root . '/' . $focus . '/' . $file ) ) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
$module_dir = @opendir( $modules_root . '/' . $focus . '/' . $file ); |
| 247 |
if ( $module_dir ) { |
| 248 |
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition |
| 249 |
while ( ( $subfile = readdir( $module_dir ) ) !== false ) { |
| 250 |
if ( '.' === substr( $subfile, 0, 1 ) ) { |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
// Unlike plugins, module main files must be called `load.php`. |
| 255 |
if ( 'load.php' !== $subfile ) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
$module_files[] = "$focus/$file/$subfile"; |
| 260 |
} |
| 261 |
|
| 262 |
closedir( $module_dir ); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
closedir( $focus_dir ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
closedir( $modules_dir ); |
| 271 |
} |
| 272 |
|
| 273 |
foreach ( $module_files as $module_file ) { |
| 274 |
if ( ! is_readable( "$modules_root/$module_file" ) ) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
$module_dir = dirname( $module_file ); |
| 278 |
$module_data = perflab_get_module_data( "$modules_root/$module_file" ); |
| 279 |
if ( ! $module_data ) { |
| 280 |
continue; |
| 281 |
} |
| 282 |
|
| 283 |
$modules[ $module_dir ] = $module_data; |
| 284 |
} |
| 285 |
|
| 286 |
uasort( |
| 287 |
$modules, |
| 288 |
function( $a, $b ) { |
| 289 |
return strnatcasecmp( $a['name'], $b['name'] ); |
| 290 |
} |
| 291 |
); |
| 292 |
|
| 293 |
return $modules; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Parses the module main file to get the module's metadata. |
| 298 |
* |
| 299 |
* This is similar to how plugin data is parsed in the WordPress core function `get_plugin_data()`. |
| 300 |
* The user-facing strings will be translated. |
| 301 |
* |
| 302 |
* @since 1.0.0 |
| 303 |
* |
| 304 |
* @param string $module_file Absolute path to the main module file. |
| 305 |
* @return array|bool Associative array of parsed module data, or false on failure. Fields for every module include |
| 306 |
* 'name', 'description', 'focus', and 'experimental'. |
| 307 |
*/ |
| 308 |
function perflab_get_module_data( $module_file ) { |
| 309 |
// Extract the module dir in the form {focus}/{module-slug}. |
| 310 |
preg_match( '/.*\/(.*\/.*)\/load\.php$/i', $module_file, $matches ); |
| 311 |
$module_dir = $matches[1]; |
| 312 |
|
| 313 |
$default_headers = array( |
| 314 |
'name' => 'Module Name', |
| 315 |
'description' => 'Description', |
| 316 |
'experimental' => 'Experimental', |
| 317 |
); |
| 318 |
|
| 319 |
$module_data = get_file_data( $module_file, $default_headers, 'perflab_module' ); |
| 320 |
|
| 321 |
// Module name and description are the minimum requirements. |
| 322 |
if ( ! $module_data['name'] || ! $module_data['description'] ) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
// Experimental should be a boolean. |
| 327 |
if ( 'yes' === strtolower( trim( $module_data['experimental'] ) ) ) { |
| 328 |
$module_data['experimental'] = true; |
| 329 |
} else { |
| 330 |
$module_data['experimental'] = false; |
| 331 |
} |
| 332 |
|
| 333 |
// Extract the module focus from the module directory. |
| 334 |
if ( strpos( $module_dir, '/' ) ) { |
| 335 |
list( $focus, $slug ) = explode( '/', $module_dir ); |
| 336 |
$module_data['focus'] = $focus; |
| 337 |
$module_data['slug'] = $slug; |
| 338 |
} |
| 339 |
|
| 340 |
// Translate fields using low-level function since they come from PHP comments, including the necessary context for |
| 341 |
// `_x()`. This must match how these are translated in the generated `/module-i18n.php` file. |
| 342 |
$translatable_fields = array( |
| 343 |
'name' => 'module name', |
| 344 |
'description' => 'module description', |
| 345 |
); |
| 346 |
foreach ( $translatable_fields as $field => $context ) { |
| 347 |
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralText |
| 348 |
$module_data[ $field ] = translate_with_gettext_context( $module_data[ $field ], $context, 'performance-lab' ); |
| 349 |
} |
| 350 |
|
| 351 |
return $module_data; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Initialise Admin Pointer |
| 356 |
* |
| 357 |
* Handles the bootstrapping of the admin pointer. |
| 358 |
* Mainly jQuery code that is self-initialising. |
| 359 |
* |
| 360 |
* @param string $hook_suffix The current admin page. |
| 361 |
* @since 1.0.0 |
| 362 |
*/ |
| 363 |
function perflab_admin_pointer( $hook_suffix ) { |
| 364 |
if ( ! in_array( $hook_suffix, array( 'index.php', 'plugins.php' ), true ) ) { |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
// Do not show admin pointer in multisite Network admin or User admin UI. |
| 369 |
if ( is_network_admin() || is_user_admin() ) { |
| 370 |
return; |
| 371 |
} |
| 372 |
|
| 373 |
$current_user = get_current_user_id(); |
| 374 |
$dismissed = explode( ',', (string) get_user_meta( $current_user, 'dismissed_wp_pointers', true ) ); |
| 375 |
|
| 376 |
if ( in_array( 'perflab-admin-pointer', $dismissed, true ) ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
// Enqueue pointer CSS and JS. |
| 381 |
wp_enqueue_style( 'wp-pointer' ); |
| 382 |
wp_enqueue_script( 'wp-pointer' ); |
| 383 |
add_action( 'admin_print_footer_scripts', 'perflab_render_pointer' ); |
| 384 |
} |
| 385 |
add_action( 'admin_enqueue_scripts', 'perflab_admin_pointer' ); |
| 386 |
|
| 387 |
/** |
| 388 |
* Renders the Admin Pointer. |
| 389 |
* |
| 390 |
* Handles the rendering of the admin pointer. |
| 391 |
* |
| 392 |
* @since 1.0.0 |
| 393 |
*/ |
| 394 |
function perflab_render_pointer() { |
| 395 |
$heading = __( 'Performance Lab', 'performance-lab' ); |
| 396 |
$wp_kses_options = array( |
| 397 |
'a' => array( |
| 398 |
'href' => array(), |
| 399 |
), |
| 400 |
); |
| 401 |
|
| 402 |
$content = sprintf( |
| 403 |
/* translators: %s: settings page link */ |
| 404 |
__( 'You can now test upcoming WordPress performance features. Open %s to individually toggle the performance features included in the plugin.', 'performance-lab' ), |
| 405 |
'<a href="' . esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ) . '">' . __( 'Settings > Performance', 'performance-lab' ) . '</a>' |
| 406 |
); |
| 407 |
|
| 408 |
?> |
| 409 |
<script id="perflab-admin-pointer" type="text/javascript"> |
| 410 |
jQuery( function() { |
| 411 |
// Pointer Options. |
| 412 |
var options = { |
| 413 |
content: '<h3><?php echo esc_js( $heading ); ?></h3><p><?php echo wp_kses( $content, $wp_kses_options ); ?></p>', |
| 414 |
position: { |
| 415 |
edge: 'left', |
| 416 |
align: 'right', |
| 417 |
}, |
| 418 |
pointerClass: 'wp-pointer arrow-top', |
| 419 |
pointerWidth: 420, |
| 420 |
close: function() { |
| 421 |
jQuery.post( |
| 422 |
window.ajaxurl, |
| 423 |
{ |
| 424 |
pointer: 'perflab-admin-pointer', |
| 425 |
action: 'dismiss-wp-pointer', |
| 426 |
} |
| 427 |
); |
| 428 |
} |
| 429 |
}; |
| 430 |
|
| 431 |
jQuery( '#menu-settings' ).pointer( options ).pointer( 'open' ); |
| 432 |
} ); |
| 433 |
</script> |
| 434 |
<?php |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Adds a link to the modules page to the plugin's entry in the plugins list table. |
| 439 |
* |
| 440 |
* This function is only used if the modules page exists and is accessible. |
| 441 |
* |
| 442 |
* @since 1.0.0 |
| 443 |
* @see perflab_add_modules_page() |
| 444 |
* |
| 445 |
* @param array $links List of plugin action links HTML. |
| 446 |
* @return array Modified list of plugin action links HTML. |
| 447 |
*/ |
| 448 |
function perflab_plugin_action_links_add_settings( $links ) { |
| 449 |
// Add link as the first plugin action link. |
| 450 |
$settings_link = sprintf( |
| 451 |
'<a href="%s">%s</a>', |
| 452 |
esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ), |
| 453 |
esc_html__( 'Settings', 'performance-lab' ) |
| 454 |
); |
| 455 |
array_unshift( $links, $settings_link ); |
| 456 |
|
| 457 |
return $links; |
| 458 |
} |
| 459 |
|