Mini Cart: Remove deprecated print_inline_script() (https://github.com/woocommerce/woocommerce-blocks/pull/10165)

* Mini Cart: Replace the deprecated print_inline_script() with supported get_inline_script_data(). Fixes woocommerce/woocommerce-blocks#10004

* Mini Cart: Add version check for the new get_inline_script_data() function

* Update the variable names and fix a typo

* Mini Cart: Add regex to check for the WP version

* Abstract the WP version comparison regex to a separate Utils class
This commit is contained in:
Daniel Dudzic 2023-07-15 01:22:34 +02:00 committed by GitHub
parent e5b4bdf3a5
commit 2412e9dadb
2 changed files with 35 additions and 2 deletions

View File

@ -8,6 +8,7 @@ use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi;
use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry; use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils; use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils; use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
use Automattic\WooCommerce\Blocks\Utils\Utils;
/** /**
* Mini-Cart class. * Mini-Cart class.
@ -322,11 +323,19 @@ class MiniCart extends AbstractBlock {
$site_url = site_url() ?? wp_guess_url(); $site_url = site_url() ?? wp_guess_url();
if ( Utils::wp_version_compare( '6.3', '>=' ) ) {
$script_before = $wp_scripts->get_inline_script_data( $script->handle, 'before' );
$script_after = $wp_scripts->get_inline_script_data( $script->handle, 'after' );
} else {
$script_before = $wp_scripts->print_inline_script( $script->handle, 'before', false );
$script_after = $wp_scripts->print_inline_script( $script->handle, 'after', false );
}
$this->scripts_to_lazy_load[ $script->handle ] = array( $this->scripts_to_lazy_load[ $script->handle ] = array(
'src' => preg_match( '|^(https?:)?//|', $script->src ) ? $script->src : $site_url . $script->src, 'src' => preg_match( '|^(https?:)?//|', $script->src ) ? $script->src : $site_url . $script->src,
'version' => $script->ver, 'version' => $script->ver,
'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ), 'before' => $script_before,
'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ), 'after' => $script_after,
'translations' => $wp_scripts->print_translations( $script->handle, false ), 'translations' => $wp_scripts->print_translations( $script->handle, false ),
); );
} }

View File

@ -0,0 +1,24 @@
<?php
namespace Automattic\WooCommerce\Blocks\Utils;
/**
* Utils class
*/
class Utils {
/**
* Compare the current WordPress version with a given version.
*
* @param string $version The version to compare against.
* @param string|null $operator Optional. The comparison operator. Defaults to null.
* @return bool|int Returns true if the current WordPress version satisfies the comparison, false otherwise.
*/
public static function wp_version_compare( $version, $operator = null ) {
$current_wp_version = get_bloginfo( 'version' );
if ( preg_match( '/^([0-9]+\.[0-9]+)/', $current_wp_version, $matches ) ) {
$current_wp_version = (float) $matches[1];
}
return version_compare( $current_wp_version, $version, $operator );
}
}