Skip to content

Commit

Permalink
Woo version check before loading block compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisnissle committed Oct 23, 2023
1 parent 5087e22 commit 5f79c7d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 65 deletions.
13 changes: 8 additions & 5 deletions src/Blocks/BlockTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 63 additions & 0 deletions src/Blocks/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
64 changes: 4 additions & 60 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '>=' );
}
}

0 comments on commit 5f79c7d

Please sign in to comment.