Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Refactor register_block_script (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu committed Jul 29, 2019
1 parent 33e666a commit 2cf01d3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,35 +172,31 @@ protected static function get_file_version( $file ) {
* @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
* @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
* @param bool $has_i18n Optional. Whether to add a script translation call to this file. Default 'true'.
* @param bool $enqueue Optional. Whether the script should be enqueued instead of registered.
*/
protected static function register_script( $handle, $src, $deps = array(), $has_i18n = true, $enqueue = false ) {
protected static function register_script( $handle, $src, $deps = array(), $has_i18n = true ) {
$filename = str_replace( plugins_url( '/', __DIR__ ), '', $src );
$ver = self::get_file_version( $filename );
$deps_path = dirname( __DIR__ ) . '/' . str_replace( '.js', '.deps.json', $filename );
$dependencies = file_exists( $deps_path ) ? json_decode( file_get_contents( $deps_path ) ) : array(); // phpcs:ignore WordPress.WP.AlternativeFunctions
$dependencies = array_merge( $dependencies, $deps );

if ( $enqueue ) {
wp_enqueue_script( $handle, $src, $dependencies, $ver, true );
} else {
wp_register_script( $handle, $src, $dependencies, $ver, true );
}
wp_register_script( $handle, $src, $dependencies, $ver, true );
if ( $has_i18n && function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( $handle, 'woo-gutenberg-products-block', dirname( __DIR__ ) . '/languages' );
}
}

/**
* Queues a script when required.
* Queues a block script.
*
* @since 2.3.0
*
* @param string $name Name of the script used to identify the file inside build folder.
*/
public static function load_script_as_required( $name ) {
public static function register_block_script( $name ) {
$filename = 'build/' . $name . '.js';
self::register_script( 'wc-' . $name, plugins_url( $filename, __DIR__ ), array(), true, true );
self::register_script( 'wc-' . $name, plugins_url( $filename, __DIR__ ) );
wp_enqueue_script( 'wc-' . $name );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/BlockTypes/ProductCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function register_block_type() {
* @return string Rendered block type output.
*/
public function render( $attributes = array(), $content = '' ) {
\Automattic\WooCommerce\Blocks\Assets::load_script_as_required( 'frontend' );
\Automattic\WooCommerce\Blocks\Assets::register_block_script( 'frontend' );

return $content;
}
Expand Down

0 comments on commit 2cf01d3

Please sign in to comment.