@@ -121,7 +121,7 @@ function embed_optimizer_filter_extension_module_urls( $extension_module_urls ):
121
121
if ( ! is_array ( $ extension_module_urls ) ) {
122
122
$ extension_module_urls = array ();
123
123
}
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 ' ) );
125
125
return $ extension_module_urls ;
126
126
}
127
127
@@ -326,7 +326,7 @@ function embed_optimizer_lazy_load_scripts(): void {
326
326
* @since 0.2.0
327
327
*/
328
328
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.
330
330
331
331
if ( false === $ script ) {
332
332
return '' ;
@@ -424,3 +424,39 @@ function embed_optimizer_render_generator(): void {
424
424
// Use the plugin slug as it is immutable.
425
425
echo '<meta name="generator" content="embed-optimizer ' . esc_attr ( EMBED_OPTIMIZER_VERSION ) . '"> ' . "\n" ;
426
426
}
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
+ }
0 commit comments