PluginProbe
Performance Lab / 1.2.0
Performance Lab v1.2.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 / load.php

load.php in Performance Lab 1.2.0, at load.php

203 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Performance Lab
4 * Plugin URI: https://github.com/WordPress/performance
5 * Description: Performance plugin from the WordPress Performance Group, which is a collection of standalone performance modules.
6 * Requires at least: 5.8
7 * Requires PHP: 5.6
8 * Version: 1.2.0
9 * Author: WordPress Performance Group
10 * Author URI: https://make.wordpress.org/core/tag/performance/
11 * License: GPLv2 or later
12 * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
13 * Text Domain: performance-lab
14 *
15 * @package performance-lab
16 */
17
18 define( 'PERFLAB_VERSION', '1.2.0' );
19 define( 'PERFLAB_MAIN_FILE', __FILE__ );
20 define( 'PERFLAB_MODULES_SETTING', 'perflab_modules_settings' );
21 define( 'PERFLAB_MODULES_SCREEN', 'perflab-modules' );
22
23 /**
24 * Registers the performance modules setting.
25 *
26 * @since 1.0.0
27 */
28 function perflab_register_modules_setting() {
29 register_setting(
30 PERFLAB_MODULES_SCREEN,
31 PERFLAB_MODULES_SETTING,
32 array(
33 'type' => 'object',
34 'sanitize_callback' => 'perflab_sanitize_modules_setting',
35 'default' => perflab_get_modules_setting_default(),
36 )
37 );
38 }
39 add_action( 'init', 'perflab_register_modules_setting' );
40
41 /**
42 * Gets the default value for the performance modules setting.
43 *
44 * @since 1.0.0
45 */
46 function perflab_get_modules_setting_default() {
47 // Since the default relies on some minimal logic that includes requiring an additional file,
48 // the result is "cached" in a static variable.
49 static $default_option = null;
50
51 if ( null === $default_option ) {
52 // To set the default value for which modules are enabled, rely on this generated file.
53 $default_enabled_modules = require plugin_dir_path( __FILE__ ) . 'default-enabled-modules.php';
54 $default_option = array_reduce(
55 $default_enabled_modules,
56 function( $module_settings, $module_dir ) {
57 $module_settings[ $module_dir ] = array( 'enabled' => true );
58 return $module_settings;
59 },
60 array()
61 );
62 }
63
64 return $default_option;
65 }
66
67 /**
68 * Sanitizes the performance modules setting.
69 *
70 * @since 1.0.0
71 *
72 * @param mixed $value Modules setting value.
73 * @return array Sanitized modules setting value.
74 */
75 function perflab_sanitize_modules_setting( $value ) {
76 if ( ! is_array( $value ) ) {
77 return array();
78 }
79
80 // Ensure that every element is an array with an 'enabled' key.
81 return array_filter(
82 array_map(
83 function( $module_settings ) {
84 if ( ! is_array( $module_settings ) ) {
85 return array();
86 }
87 return array_merge(
88 array( 'enabled' => false ),
89 $module_settings
90 );
91 },
92 $value
93 )
94 );
95 }
96
97 /**
98 * Gets the performance module settings.
99 *
100 * @since 1.0.0
101 *
102 * @return array Associative array of module settings keyed by module slug.
103 */
104 function perflab_get_module_settings() {
105 // Even though a default value is registered for this setting, the default must be explicitly
106 // passed here, to support scenarios where this function is called before the 'init' action,
107 // for example when loading the active modules.
108 return (array) get_option( PERFLAB_MODULES_SETTING, perflab_get_modules_setting_default() );
109 }
110
111 /**
112 * Gets the active performance modules.
113 *
114 * @since 1.0.0
115 *
116 * @return array List of active module slugs.
117 */
118 function perflab_get_active_modules() {
119 $modules = array_keys(
120 array_filter(
121 perflab_get_module_settings(),
122 function( $module_settings ) {
123 return isset( $module_settings['enabled'] ) && $module_settings['enabled'];
124 }
125 )
126 );
127
128 /**
129 * Filters active modules to allow programmatically control which modules are active.
130 *
131 * @since 1.0.0
132 *
133 * @param array An array of the currently active modules.
134 */
135 $modules = apply_filters( 'perflab_active_modules', $modules );
136
137 return $modules;
138 }
139
140 /**
141 * Gets the content attribute for the generator tag for the Performance Lab plugin.
142 *
143 * This attribute is then used in {@see perflab_render_generator()}.
144 *
145 * @since 1.1.0
146 */
147 function perflab_get_generator_content() {
148 $active_modules = perflab_get_active_modules();
149
150 return sprintf(
151 'Performance Lab %1$s; modules: %2$s',
152 PERFLAB_VERSION,
153 implode( ', ', $active_modules )
154 );
155 }
156
157 /**
158 * Displays the HTML generator tag for the Performance Lab plugin.
159 *
160 * See {@see 'wp_head'}.
161 *
162 * @since 1.1.0
163 */
164 function perflab_render_generator() {
165 $content = perflab_get_generator_content();
166
167 echo '<meta name="generator" content="' . esc_attr( $content ) . '">' . "\n";
168 }
169 add_action( 'wp_head', 'perflab_render_generator' );
170
171 /**
172 * Loads the active performance modules.
173 *
174 * @since 1.0.0
175 */
176 function perflab_load_active_modules() {
177 $active_modules = perflab_get_active_modules();
178
179 if ( empty( $active_modules ) ) {
180 return;
181 }
182
183 foreach ( $active_modules as $module ) {
184 // Do not load module if it no longer exists.
185 $module_file = plugin_dir_path( __FILE__ ) . 'modules/' . $module . '/load.php';
186 if ( ! file_exists( $module_file ) ) {
187 continue;
188 }
189
190 require_once $module_file;
191 }
192 }
193
194 perflab_load_active_modules();
195
196 // Only load admin integration when in admin.
197 if ( is_admin() ) {
198 require_once plugin_dir_path( __FILE__ ) . 'admin/load.php';
199 }
200
201 // Polyfills.
202 require_once plugin_dir_path( __FILE__ ) . 'polyfills.php';
203