__( 'Other', 'performance-lab' ) );
foreach ( $sections as $section_slug => $section_data ) {
add_settings_section(
$section_slug,
$section_data['name'],
null,
PERFLAB_MODULES_SCREEN
);
}
// Register fields for all modules.
if ( ! is_array( $modules ) ) {
$modules = perflab_get_modules();
}
$settings = perflab_get_module_settings();
foreach ( $modules as $module_slug => $module_data ) {
$module_settings = isset( $settings[ $module_slug ] ) ? $settings[ $module_slug ] : array();
$module_section = isset( $sections[ $module_data['focus'] ] ) ? $module_data['focus'] : 'other';
// Mark this module's section as added.
$sections[ $module_section ]['added'] = true;
add_settings_field(
$module_slug,
$module_data['name'],
function() use ( $module_slug, $module_data, $module_settings ) {
perflab_render_modules_page_field( $module_slug, $module_data, $module_settings );
},
PERFLAB_MODULES_SCREEN,
$module_section
);
}
// Remove all sections for which there are no modules.
foreach ( $sections as $section_slug => $section_data ) {
if ( empty( $section_data['added'] ) ) {
unset( $wp_settings_sections[ PERFLAB_MODULES_SCREEN ][ $section_slug ] );
}
}
}
/**
* Renders the modules page.
*
* @since 1.0.0
*/
function perflab_render_modules_page() {
?>
array(
'name' => __( 'Images', 'performance-lab' ),
),
'javascript' => array(
'name' => __( 'JavaScript', 'performance-lab' ),
),
'site-health' => array(
'name' => __( 'Site Health', 'performance-lab' ),
),
'measurement' => array(
'name' => __( 'Measurement', 'performance-lab' ),
),
'object-cache' => array(
'name' => __( 'Object Cache', 'performance-lab' ),
),
);
}
/**
* Gets all available modules.
*
* This function iterates through the modules directory and therefore should only be called on the modules page.
* It searches all modules, similar to how plugins are searched in the WordPress core function `get_plugins()`.
*
* @since 1.0.0
*
* @param string $modules_root Modules root directory to look for modules in. Default is the `/modules` directory
* in the plugin's root.
* @return array Associative array of parsed module data, keyed by module slug. Fields for every module include
* 'name', 'description', 'focus', and 'experimental'.
*/
function perflab_get_modules( $modules_root = null ) {
if ( null === $modules_root ) {
$modules_root = dirname( __DIR__ ) . '/modules';
}
$modules = array();
$module_files = array();
$modules_dir = @opendir( $modules_root );
// Modules are organized as {focus}/{module-slug} in the modules folder.
if ( $modules_dir ) {
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( ( $focus = readdir( $modules_dir ) ) !== false ) {
if ( '.' === substr( $focus, 0, 1 ) ) {
continue;
}
// Each focus area must be a directory.
if ( ! is_dir( $modules_root . '/' . $focus ) ) {
continue;
}
$focus_dir = @opendir( $modules_root . '/' . $focus );
if ( $focus_dir ) {
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( ( $file = readdir( $focus_dir ) ) !== false ) {
// Unlike plugins, modules must be in a directory.
if ( ! is_dir( $modules_root . '/' . $focus . '/' . $file ) ) {
continue;
}
$module_dir = @opendir( $modules_root . '/' . $focus . '/' . $file );
if ( $module_dir ) {
// phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( ( $subfile = readdir( $module_dir ) ) !== false ) {
if ( '.' === substr( $subfile, 0, 1 ) ) {
continue;
}
// Unlike plugins, module main files must be called `load.php`.
if ( 'load.php' !== $subfile ) {
continue;
}
$module_files[] = "$focus/$file/$subfile";
}
closedir( $module_dir );
}
}
closedir( $focus_dir );
}
}
closedir( $modules_dir );
}
foreach ( $module_files as $module_file ) {
if ( ! is_readable( "$modules_root/$module_file" ) ) {
continue;
}
$module_dir = dirname( $module_file );
$module_data = perflab_get_module_data( "$modules_root/$module_file" );
if ( ! $module_data ) {
continue;
}
$modules[ $module_dir ] = $module_data;
}
uasort(
$modules,
function( $a, $b ) {
return strnatcasecmp( $a['name'], $b['name'] );
}
);
return $modules;
}
/**
* Parses the module main file to get the module's metadata.
*
* This is similar to how plugin data is parsed in the WordPress core function `get_plugin_data()`.
* The user-facing strings will be translated.
*
* @since 1.0.0
*
* @param string $module_file Absolute path to the main module file.
* @return array|bool Associative array of parsed module data, or false on failure. Fields for every module include
* 'name', 'description', 'focus', and 'experimental'.
*/
function perflab_get_module_data( $module_file ) {
// Extract the module dir in the form {focus}/{module-slug}.
preg_match( '/.*\/(.*\/.*)\/load\.php$/i', $module_file, $matches );
$module_dir = $matches[1];
$default_headers = array(
'name' => 'Module Name',
'description' => 'Description',
'experimental' => 'Experimental',
);
$module_data = get_file_data( $module_file, $default_headers, 'perflab_module' );
// Module name and description are the minimum requirements.
if ( ! $module_data['name'] || ! $module_data['description'] ) {
return false;
}
// Experimental should be a boolean.
if ( 'yes' === strtolower( trim( $module_data['experimental'] ) ) ) {
$module_data['experimental'] = true;
} else {
$module_data['experimental'] = false;
}
// Extract the module focus from the module directory.
if ( strpos( $module_dir, '/' ) ) {
list( $focus, $slug ) = explode( '/', $module_dir );
$module_data['focus'] = $focus;
$module_data['slug'] = $slug;
}
// Translate fields using low-level function since they come from PHP comments, including the necessary context for
// `_x()`. This must match how these are translated in the generated `/module-i18n.php` file.
$translatable_fields = array(
'name' => 'module name',
'description' => 'module description',
);
foreach ( $translatable_fields as $field => $context ) {
// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralContext,WordPress.WP.I18n.NonSingularStringLiteralText
$module_data[ $field ] = translate_with_gettext_context( $module_data[ $field ], $context, 'performance-lab' );
}
return $module_data;
}
/**
* Initialise Admin Pointer
*
* Handles the bootstrapping of the admin pointer.
* Mainly jQuery code that is self-initialising.
*
* @param string $hook_suffix The current admin page.
* @since 1.0.0
*/
function perflab_admin_pointer( $hook_suffix ) {
if ( ! in_array( $hook_suffix, array( 'index.php', 'plugins.php' ), true ) ) {
return;
}
// Do not show admin pointer in multisite Network admin or User admin UI.
if ( is_network_admin() || is_user_admin() ) {
return;
}
$current_user = get_current_user_id();
$dismissed = explode( ',', (string) get_user_meta( $current_user, 'dismissed_wp_pointers', true ) );
if ( in_array( 'perflab-admin-pointer', $dismissed, true ) ) {
return;
}
// Enqueue pointer CSS and JS.
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
add_action( 'admin_print_footer_scripts', 'perflab_render_pointer' );
}
add_action( 'admin_enqueue_scripts', 'perflab_admin_pointer' );
/**
* Renders the Admin Pointer.
*
* Handles the rendering of the admin pointer.
*
* @since 1.0.0
*/
function perflab_render_pointer() {
$heading = __( 'Performance Lab', 'performance-lab' );
$wp_kses_options = array(
'a' => array(
'href' => array(),
),
);
$content = sprintf(
/* translators: %s: settings page link */
__( 'You can now test upcoming WordPress performance features. Open %s to individually toggle the performance features included in the plugin.', 'performance-lab' ),
'' . __( 'Settings > Performance', 'performance-lab' ) . ''
);
?>
%s',
esc_url( add_query_arg( 'page', PERFLAB_MODULES_SCREEN, admin_url( 'options-general.php' ) ) ),
esc_html__( 'Settings', 'performance-lab' )
);
array_unshift( $links, $settings_link );
return $links;
}