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

Commit

Permalink
Mini Cart: Remove deprecated print_inline_script() (#10165)
Browse files Browse the repository at this point in the history
* Mini Cart: Replace the deprecated print_inline_script() with supported get_inline_script_data(). Fixes #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
  • Loading branch information
danieldudzic authored Jul 14, 2023
1 parent 73cb0ce commit b75f00e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
use Automattic\WooCommerce\Blocks\Utils\Utils;

/**
* Mini-Cart class.
Expand Down Expand Up @@ -322,11 +323,19 @@ protected function append_script_and_deps_src( $script ) {

$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(
'src' => preg_match( '|^(https?:)?//|', $script->src ) ? $script->src : $site_url . $script->src,
'version' => $script->ver,
'before' => $wp_scripts->print_inline_script( $script->handle, 'before', false ),
'after' => $wp_scripts->print_inline_script( $script->handle, 'after', false ),
'before' => $script_before,
'after' => $script_after,
'translations' => $wp_scripts->print_translations( $script->handle, false ),
);
}
Expand Down
24 changes: 24 additions & 0 deletions src/Utils/Utils.php
Original file line number Diff line number Diff line change
@@ -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 );
}
}

0 comments on commit b75f00e

Please sign in to comment.