diff --git a/src/Blocks/BlockTypesController.php b/src/Blocks/BlockTypesController.php index 0be8bb00b..bf2ff7350 100644 --- a/src/Blocks/BlockTypesController.php +++ b/src/Blocks/BlockTypesController.php @@ -54,11 +54,14 @@ protected function get_block_types() { ); if ( \Vendidero\Germanized\Package::is_pro() ) { - $block_types = array_merge( $block_types, array( - 'ProductDeposit', - 'ProductDepositPackagingType', - 'ProductNutriScore', - ) ); + $block_types = array_merge( + $block_types, + array( + 'ProductDeposit', + 'ProductDepositPackagingType', + 'ProductNutriScore', + ) + ); } return $block_types; diff --git a/src/Blocks/Checkout.php b/src/Blocks/Checkout.php index 52d8b9d4a..3046ab8da 100644 --- a/src/Blocks/Checkout.php +++ b/src/Blocks/Checkout.php @@ -12,11 +12,74 @@ final class Checkout { public function __construct() { $this->adjust_checkout_block(); + $this->register_filters(); $this->register_integrations(); $this->register_endpoint_data(); $this->register_validation_and_storage(); } + private function register_filters() { + add_filter( + 'woocommerce_get_item_data', + function( $item_data, $item ) { + if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) || WC()->is_rest_api_request() ) { + $labels = wc_gzd_get_checkout_shopmarks(); + + if ( is_checkout() || has_block( 'woocommerce/checkout' ) ) { + $labels = wc_gzd_get_checkout_shopmarks(); + } elseif ( is_cart() || has_block( 'woocommerce/cart' ) ) { + $labels = wc_gzd_get_cart_shopmarks(); + } + + $label_item_data = array(); + + foreach ( $labels as $label ) { + $callback = $label->get_callback(); + $arg_count = $label->get_number_of_params(); + + if ( 'differential_taxation' === $label->get_type() ) { + add_filter( 'woocommerce_gzd_differential_taxation_notice_text_mark', '__return_false' ); + $callback = 'woocommerce_gzd_template_differential_taxation_notice_cart'; + $arg_count = 0; + } + + $args = array( '', $item, $item['key'] ); + + if ( 2 === $arg_count ) { + $args = array( $item, $item['key'] ); + } elseif ( 0 === $arg_count ) { + $args = array(); + } + + ob_start(); + if ( $label->get_is_action() ) { + call_user_func_array( $callback, $args ); + } else { + echo wp_kses_post( call_user_func_array( $callback, $args ) ); + } + $output = trim( ob_get_clean() ); + + if ( ! empty( $output ) ) { + $label_item_data[] = array( + 'key' => 'gzd-' . $label->get_type(), + 'value' => $output, + 'display' => '', + ); + } + } + + if ( ! empty( $label_item_data ) ) { + $item_data = array_merge( $label_item_data, $item_data ); + } + } + + return $item_data; + }, + 10000, + 2 + ); + } + private function adjust_checkout_block() { add_filter( 'render_block', diff --git a/src/Bootstrap.php b/src/Bootstrap.php index ba36081f6..6f7a36bad 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -41,6 +41,10 @@ public function __construct( $container ) { * Init the package - load the blocks library and define constants. */ protected function init() { + if ( ! Package::load_blocks() ) { + return false; + } + $this->register_dependencies(); $this->register_payment_methods(); @@ -78,66 +82,6 @@ function( $namespaces ) { } ); - add_filter( - 'woocommerce_get_item_data', - function( $item_data, $item ) { - if ( has_block( 'woocommerce/checkout' ) || has_block( 'woocommerce/cart' ) || WC()->is_rest_api_request() ) { - $labels = wc_gzd_get_checkout_shopmarks(); - - if ( is_checkout() || has_block( 'woocommerce/checkout' ) ) { - $labels = wc_gzd_get_checkout_shopmarks(); - } elseif ( is_cart() || has_block( 'woocommerce/cart' ) ) { - $labels = wc_gzd_get_cart_shopmarks(); - } - - $label_item_data = array(); - - foreach ( $labels as $label ) { - $callback = $label->get_callback(); - $arg_count = $label->get_number_of_params(); - - if ( 'differential_taxation' === $label->get_type() ) { - add_filter( 'woocommerce_gzd_differential_taxation_notice_text_mark', '__return_false' ); - $callback = 'woocommerce_gzd_template_differential_taxation_notice_cart'; - $arg_count = 0; - } - - $args = array( '', $item, $item['key'] ); - - if ( 2 === $arg_count ) { - $args = array( $item, $item['key'] ); - } elseif ( 0 === $arg_count ) { - $args = array(); - } - - ob_start(); - if ( $label->get_is_action() ) { - call_user_func_array( $callback, $args ); - } else { - echo wp_kses_post( call_user_func_array( $callback, $args ) ); - } - $output = trim( ob_get_clean() ); - - if ( ! empty( $output ) ) { - $label_item_data[] = array( - 'key' => 'gzd-' . $label->get_type(), - 'value' => $output, - 'display' => '', - ); - } - } - - if ( ! empty( $label_item_data ) ) { - $item_data = array_merge( $label_item_data, $item_data ); - } - } - - return $item_data; - }, - 10000, - 2 - ); - $this->container->get( BlockTypesController::class ); $this->container->get( Assets::class ); $this->container->get( Products::class ); diff --git a/src/Package.php b/src/Package.php index 46ccd209b..e71b6e50f 100644 --- a/src/Package.php +++ b/src/Package.php @@ -74,4 +74,10 @@ public static function get_language_path() { public static function is_pro() { return self::$gzd_instance->is_pro(); } + + public static function load_blocks() { + $woo_version = \Vendidero\Germanized\PluginsHelper::get_plugin_version( 'woocommerce' ); + + return version_compare( $woo_version, '8.2.0', '>=' ); + } }