Skip to content

Commit 1ca7c77

Browse files
committed
Introduce perflab_get_asset_src()
1 parent cc3b95f commit 1ca7c77

File tree

1 file changed

+43
-1
lines changed
  • plugins/performance-lab/includes/admin

1 file changed

+43
-1
lines changed

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

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,48 @@ 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 URL 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_src( string $src_path, ?string $min_path = null ): string {
226+
$dir_url = plugin_dir_url( PERFLAB_MAIN_FILE );
227+
228+
if ( null === $min_path ) {
229+
// Note: wp_scripts_get_suffix() is not used here because we need access to both the source and minified paths.
230+
$min_path = preg_replace( '/(?=\.\w+$)/', '.min', $src_path );
231+
}
232+
233+
$force_src = false;
234+
if (
235+
( WP_DEBUG || wp_get_environment_type() === 'local' )
236+
&&
237+
! file_exists( PERFLAB_PLUGIN_DIR_PATH . $min_path )
238+
) {
239+
$force_src = true;
240+
wp_trigger_error(
241+
__FUNCTION__,
242+
sprintf(
243+
/* translators: %s is the minified asset path */
244+
__( 'Minified asset has not been built: %s', 'performance-lab' ),
245+
$min_path
246+
),
247+
E_USER_WARNING
248+
);
249+
}
250+
251+
if ( SCRIPT_DEBUG || $force_src ) {
252+
return $dir_url . $src_path;
253+
}
254+
255+
return $dir_url . $min_path;
256+
}
257+
216258
/**
217259
* Callback function to handle admin scripts.
218260
*
@@ -228,7 +270,7 @@ function perflab_enqueue_features_page_scripts(): void {
228270
// Enqueue plugin activate AJAX script and localize script data.
229271
wp_enqueue_script(
230272
'perflab-plugin-activate-ajax',
231-
plugin_dir_url( PERFLAB_MAIN_FILE ) . 'includes/admin/plugin-activate-ajax' . wp_scripts_get_suffix() . '.js',
273+
perflab_get_asset_src( 'includes/admin/plugin-activate-ajax.js' ),
232274
array( 'wp-i18n', 'wp-a11y', 'wp-api-fetch' ),
233275
PERFLAB_VERSION,
234276
true

0 commit comments

Comments
 (0)