Skip to content

Commit 5509f2c

Browse files
committed
Update other plugins to warn when minified asset is missing
1 parent be0401c commit 5509f2c

File tree

7 files changed

+116
-5
lines changed

7 files changed

+116
-5
lines changed

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,39 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis
7676
$video_visitor = new Image_Prioritizer_Video_Tag_Visitor();
7777
$registry->register( 'image-prioritizer/video', $video_visitor );
7878
}
79+
80+
/**
81+
* Gets the path to a script or stylesheet.
82+
*
83+
* @since n.e.x.t
84+
*
85+
* @param string $src_path Source path, relative to plugin root.
86+
* @param string|null $min_path Minified path. If not supplied, then '.min' is injected before the file extension in the source path.
87+
* @return string URL to script or stylesheet.
88+
*/
89+
function image_prioritizer_get_asset_path( string $src_path, ?string $min_path = null ): string {
90+
if ( null === $min_path ) {
91+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
92+
$min_path = (string) preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
93+
}
94+
95+
$force_src = false;
96+
if ( WP_DEBUG && ! file_exists( trailingslashit( __DIR__ ) . $min_path ) ) {
97+
$force_src = true;
98+
wp_trigger_error(
99+
__FUNCTION__,
100+
sprintf(
101+
/* translators: %s is the minified asset path */
102+
__( 'Minified asset has not been built: %s', 'image-prioritizer' ),
103+
$min_path
104+
),
105+
E_USER_WARNING
106+
);
107+
}
108+
109+
if ( SCRIPT_DEBUG || $force_src ) {
110+
return $src_path;
111+
}
112+
113+
return $min_path;
114+
}

plugins/image-prioritizer/hooks.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* @since 0.2.0
2323
*/
2424
function image_prioritizer_get_lazy_load_script(): string {
25-
$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.
25+
$path = image_prioritizer_get_asset_path( 'lazy-load.js' );
26+
$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.
2627

2728
if ( false === $script ) {
2829
return '';

plugins/optimization-detective/detection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static function ( OD_URL_Metric_Group $group ): array {
112112
return wp_get_inline_script_tag(
113113
sprintf(
114114
'import detect from %s; detect( %s );',
115-
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . sprintf( 'detect%s.js', wp_scripts_get_suffix() ) ) ),
115+
wp_json_encode( add_query_arg( 'ver', OPTIMIZATION_DETECTIVE_VERSION, plugin_dir_url( __FILE__ ) . od_get_asset_path( 'detect.js' ) ) ),
116116
wp_json_encode( $detect_args )
117117
),
118118
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ function perflab_get_asset_path( string $src_path, ?string $min_path = null ): s
229229
}
230230

231231
$force_src = false;
232-
if ( WP_DEBUG && ! file_exists( PERFLAB_PLUGIN_DIR_PATH . $min_path ) ) {
232+
if ( WP_DEBUG && ! file_exists( trailingslashit( PERFLAB_PLUGIN_DIR_PATH ) . $min_path ) ) {
233233
$force_src = true;
234234
wp_trigger_error(
235235
__FUNCTION__,

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)