Skip to content

Commit bc81588

Browse files
committed
Merge branch 'trunk' of https://github.com/WordPress/performance into add/external-bg-preload
* 'trunk' of https://github.com/WordPress/performance: Explain why perflab_query_plugin_info() isn't used in perflab_install_and_activate_plugin() Explicitly request download_link and requires_plugins fields Bump wp-phpunit/wp-phpunit from 6.7.0 to 6.7.1 Remove download_link from data explicitly requested in perflab_query_plugin_info() Bump typescript from 5.6.3 to 5.7.2 Avoid transient cache for plugin installation to ensure latest version Update regex to handle version tags like `-rc` and `-beta` in plugin download links Remove plugin version number from download link Update other plugins to warn when minified asset is missing Eliminate local env check Change perflab_get_asset_url() to return path instead of URL Introduce perflab_get_asset_src() Add missing JS minification for new PL script
2 parents 94e527b + 638c1ed commit bc81588

File tree

11 files changed

+218
-20
lines changed

11 files changed

+218
-20
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"lodash": "4.17.21",
2525
"micromatch": "^4.0.8",
2626
"npm-run-all": "^4.1.5",
27-
"typescript": "^5.6.3",
27+
"typescript": "^5.7.2",
2828
"webpackbar": "^7.0.0"
2929
},
3030
"scripts": {

plugins/embed-optimizer/hooks.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function embed_optimizer_filter_extension_module_urls( $extension_module_urls ):
121121
if ( ! is_array( $extension_module_urls ) ) {
122122
$extension_module_urls = array();
123123
}
124-
$extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) );
124+
$extension_module_urls[] = add_query_arg( 'ver', EMBED_OPTIMIZER_VERSION, plugin_dir_url( __FILE__ ) . embed_optimizer_get_asset_path( 'detect.js' ) );
125125
return $extension_module_urls;
126126
}
127127

@@ -326,7 +326,7 @@ function embed_optimizer_lazy_load_scripts(): void {
326326
* @since 0.2.0
327327
*/
328328
function embed_optimizer_get_lazy_load_script(): string {
329-
$script = file_get_contents( __DIR__ . sprintf( '/lazy-load%s.js', wp_scripts_get_suffix() ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
329+
$script = file_get_contents( __DIR__ . '/' . embed_optimizer_get_asset_path( 'lazy-load.js' ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
330330

331331
if ( false === $script ) {
332332
return '';
@@ -424,3 +424,39 @@ function embed_optimizer_render_generator(): void {
424424
// Use the plugin slug as it is immutable.
425425
echo '<meta name="generator" content="embed-optimizer ' . esc_attr( EMBED_OPTIMIZER_VERSION ) . '">' . "\n";
426426
}
427+
428+
/**
429+
* Gets the path to a script or stylesheet.
430+
*
431+
* @since n.e.x.t
432+
*
433+
* @param string $src_path Source path, relative to plugin root.
434+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
435+
* @return string URL to script or stylesheet.
436+
*/
437+
function embed_optimizer_get_asset_path( string $src_path, ?string $min_path = null ): string {
438+
if ( null === $min_path ) {
439+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
440+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
441+
}
442+
443+
$force_src = false;
444+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
445+
$force_src = true;
446+
wp_trigger_error(
447+
__FUNCTION__,
448+
sprintf(
449+
/* translators: %s is the minified asset path */
450+
__( 'Minified asset has not been built: %s', 'embed-optimizer' ),
451+
$min_path
452+
),
453+
E_USER_WARNING
454+
);
455+
}
456+
457+
if ( SCRIPT_DEBUG || $force_src ) {
458+
return $src_path;
459+
}
460+
461+
return $min_path;
462+
}

plugins/image-prioritizer/helper.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis
8787
* @since 0.2.0
8888
*/
8989
function image_prioritizer_get_lazy_load_script(): string {
90-
$script = file_get_contents( __DIR__ . sprintf( '/lazy-load%s.js', wp_scripts_get_suffix() ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
90+
$path = image_prioritizer_get_asset_path( 'lazy-load.js' );
91+
$script = file_get_contents( __DIR__ . '/' . $path ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- It's a local filesystem path not a remote request.
9192

9293
if ( false === $script ) {
9394
return '';
@@ -108,7 +109,7 @@ function image_prioritizer_filter_extension_module_urls( $extension_module_urls
108109
if ( ! is_array( $extension_module_urls ) ) {
109110
$extension_module_urls = array();
110111
}
111-
$extension_module_urls[] = add_query_arg( 'ver', IMAGE_PRIORITIZER_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) );
112+
$extension_module_urls[] = add_query_arg( 'ver', IMAGE_PRIORITIZER_VERSION, plugin_dir_url( __FILE__ ) . image_prioritizer_get_asset_path( 'detect.js' ) );
112113
return $extension_module_urls;
113114
}
114115

@@ -211,3 +212,39 @@ function image_prioritizer_filter_store_url_metric_validity( $validity, OD_Stric
211212
// TODO: Check for the Content-Length and return invalid if it is gigantic?
212213
return $validity;
213214
}
215+
216+
/**
217+
* Gets the path to a script or stylesheet.
218+
*
219+
* @since n.e.x.t
220+
*
221+
* @param string $src_path Source path, relative to plugin root.
222+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
223+
* @return string URL to script or stylesheet.
224+
*/
225+
function image_prioritizer_get_asset_path( string $src_path, ?string $min_path = null ): string {
226+
if ( null === $min_path ) {
227+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
228+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
229+
}
230+
231+
$force_src = false;
232+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
233+
$force_src = true;
234+
wp_trigger_error(
235+
__FUNCTION__,
236+
sprintf(
237+
/* translators: %s is the minified asset path */
238+
__( 'Minified asset has not been built: %s', 'image-prioritizer' ),
239+
$min_path
240+
),
241+
E_USER_WARNING
242+
);
243+
}
244+
245+
if ( SCRIPT_DEBUG || $force_src ) {
246+
return $src_path;
247+
}
248+
249+
return $min_path;
250+
}

plugins/optimization-detective/detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static function ( OD_URL_Metric_Group $group ): array {
114114
return wp_get_inline_script_tag(
115115
sprintf(
116116
'import detect from %s; detect( %s );',
117-
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) ) ),
117+
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . od_get_asset_path( 'detect.js' ) ) ),
118118
wp_json_encode( $detect_args )
119119
),
120120
array( 'type' => 'module' )

plugins/optimization-detective/helper.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,39 @@ function od_render_generator_meta_tag(): void {
6464
// Use the plugin slug as it is immutable.
6565
echo '<meta name="generator" content="optimization-detective ' . esc_attr( OPTIMIZATION_DETECTIVE_VERSION ) . '">' . "\n";
6666
}
67+
68+
/**
69+
* Gets the path to a script or stylesheet.
70+
*
71+
* @since n.e.x.t
72+
*
73+
* @param string $src_path Source path, relative to plugin root.
74+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
75+
* @return string URL to script or stylesheet.
76+
*/
77+
function od_get_asset_path( string $src_path, ?string $min_path = null ): string {
78+
if ( null === $min_path ) {
79+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
80+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
81+
}
82+
83+
$force_src = false;
84+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
85+
$force_src = true;
86+
wp_trigger_error(
87+
__FUNCTION__,
88+
sprintf(
89+
/* translators: %s is the minified asset path */
90+
__( 'Minified asset has not been built: %s', 'optimization-detective' ),
91+
$min_path
92+
),
93+
E_USER_WARNING
94+
);
95+
}
96+
97+
if ( SCRIPT_DEBUG || $force_src ) {
98+
return $src_path;
99+
}
100+
101+
return $min_path;
102+
}

plugins/performance-lab/includes/admin/load.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,42 @@ function perflab_dismiss_wp_pointer_wrapper(): void {
213213
}
214214
add_action( 'wp_ajax_dismiss-wp-pointer', 'perflab_dismiss_wp_pointer_wrapper', 0 );
215215

216+
/**
217+
* Gets the path to a script or stylesheet.
218+
*
219+
* @since n.e.x.t
220+
*
221+
* @param string $src_path Source path.
222+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
223+
* @return string URL to script or stylesheet.
224+
*/
225+
function perflab_get_asset_path( string $src_path, ?string $min_path = null ): string {
226+
if ( null === $min_path ) {
227+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
228+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
229+
}
230+
231+
$force_src = false;
232+
if ( WP_DEBUG && ! file_exists( trailingslashit( PERFLAB_PLUGIN_DIR_PATH ) . $min_path ) ) {
233+
$force_src = true;
234+
wp_trigger_error(
235+
__FUNCTION__,
236+
sprintf(
237+
/* translators: %s is the minified asset path */
238+
__( 'Minified asset has not been built: %s', 'performance-lab' ),
239+
$min_path
240+
),
241+
E_USER_WARNING
242+
);
243+
}
244+
245+
if ( SCRIPT_DEBUG || $force_src ) {
246+
return $src_path;
247+
}
248+
249+
return $min_path;
250+
}
251+
216252
/**
217253
* Callback function to handle admin scripts.
218254
*
@@ -228,7 +264,7 @@ function perflab_enqueue_features_page_scripts(): void {
228264
// Enqueue plugin activate AJAX script and localize script data.
229265
wp_enqueue_script(
230266
'perflab-plugin-activate-ajax',
231-
plugin_dir_url( PERFLAB_MAIN_FILE ) . 'includes/admin/plugin-activate-ajax.js',
267+
plugin_dir_url( PERFLAB_MAIN_FILE ) . perflab_get_asset_path( 'includes/admin/plugin-activate-ajax.js' ),
232268
array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ),
233269
PERFLAB_VERSION,
234270
true

plugins/performance-lab/includes/admin/plugins.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @since 2.8.0
1717
*
1818
* @param string $plugin_slug The string identifier for the plugin in questions slug.
19-
* @return array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string, version: string}|WP_Error Array of plugin data or WP_Error if failed.
19+
* @return array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], version: string}|WP_Error Array of plugin data or WP_Error if failed.
2020
*/
2121
function perflab_query_plugin_info( string $plugin_slug ) {
2222
$transient_key = 'perflab_plugins_info';
@@ -40,7 +40,6 @@ function perflab_query_plugin_info( string $plugin_slug ) {
4040
'requires',
4141
'requires_php',
4242
'requires_plugins',
43-
'download_link',
4443
'version', // Needed by install_plugin_install_status().
4544
);
4645

@@ -113,7 +112,7 @@ function perflab_query_plugin_info( string $plugin_slug ) {
113112
/**
114113
* Validated (mostly) plugin data.
115114
*
116-
* @var array<string, array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], download_link: string, version: string}> $plugins
115+
* @var array<string, array{name: string, slug: string, short_description: string, requires: string|false, requires_php: string|false, requires_plugins: string[], version: string}> $plugins
117116
*/
118117
return $plugins[ $plugin_slug ];
119118
}
@@ -343,11 +342,27 @@ function perflab_install_and_activate_plugin( string $plugin_slug, array &$proce
343342
}
344343
$processed_plugins[] = $plugin_slug;
345344

346-
$plugin_data = perflab_query_plugin_info( $plugin_slug );
345+
// Get the freshest data (including the most recent download_link) as opposed what is cached by perflab_query_plugin_info().
346+
$plugin_data = plugins_api(
347+
'plugin_information',
348+
array(
349+
'slug' => $plugin_slug,
350+
'fields' => array(
351+
'download_link' => true,
352+
'requires_plugins' => true,
353+
'sections' => false, // Omit the bulk of the response which we don't need.
354+
),
355+
)
356+
);
357+
347358
if ( $plugin_data instanceof WP_Error ) {
348359
return $plugin_data;
349360
}
350361

362+
if ( is_object( $plugin_data ) ) {
363+
$plugin_data = (array) $plugin_data;
364+
}
365+
351366
// Add recommended plugins (soft dependencies) to the list of plugins installed and activated.
352367
if ( 'embed-optimizer' === $plugin_slug ) {
353368
$plugin_data['requires_plugins'][] = 'optimization-detective';

tools/phpstan/constants.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@
1717
define( 'IMAGE_PRIORITIZER_VERSION', '0.0.0' );
1818

1919
define( 'EMBED_OPTIMIZER_VERSION', '0.0.0' );
20+
21+
define( 'PERFLAB_PLUGIN_DIR_PATH', __DIR__ . '/../../plugins/performance-lab/' );

0 commit comments

Comments
 (0)