| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Blocksy theme compatibility layer |
| 8 |
*/ |
| 9 |
if ( ! class_exists( 'Merchant_Blocksy_Theme' ) ) { |
| 10 |
class Merchant_Blocksy_Theme { |
| 11 |
|
| 12 |
/** |
| 13 |
* Constructor. |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_action( 'wp_enqueue_scripts', array( $this, 'styles' ) ); |
| 17 |
add_action( 'init', array( $this, 'product_video_module' ) ); |
| 18 |
add_action( 'init', array( $this, 'product_audio_module' ) ); |
| 19 |
add_filter('theme_mod_woo_card_layout', array( $this, 'remove_theme_default_add_to_cart' ) ); |
| 20 |
add_filter('merchant_product_swatch_shop_catalog_add_to_cart_button_html', array( $this, 'fix_add_to_cart_button_structure' ) ); |
| 21 |
add_filter('woocommerce_loop_add_to_cart_link', array( $this, 'fix_add_to_cart_button_structure' ) ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Add a div to wrap the add to cart button. |
| 26 |
* |
| 27 |
* @param $button_html string add to cart button html. |
| 28 |
* |
| 29 |
* @return string The add to cart button html. |
| 30 |
*/ |
| 31 |
public function fix_add_to_cart_button_structure( $button_html ) { |
| 32 |
if ( ! merchant_is_blocksy_active() ) { |
| 33 |
return $button_html; |
| 34 |
} |
| 35 |
if ( ! Merchant_Modules::is_module_active( 'product-swatches' ) ) { |
| 36 |
return $button_html; |
| 37 |
} |
| 38 |
|
| 39 |
return '<div class="ct-woo-card-actions">' . $button_html . '</div>'; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Disable the theme's add to cart button in the shop loop while product swatches module is active. |
| 44 |
* |
| 45 |
* @param $layout_values array The layout values. |
| 46 |
* |
| 47 |
* @see https://developer.wordpress.org/reference/hooks/theme_mod_name/ |
| 48 |
* |
| 49 |
* @return array The layout values after disable the add to cart button. |
| 50 |
*/ |
| 51 |
public function remove_theme_default_add_to_cart( $layout_values ) { |
| 52 |
if ( ! merchant_is_blocksy_active() ) { |
| 53 |
return $layout_values; |
| 54 |
} |
| 55 |
if ( ! Merchant_Modules::is_module_active( 'product-swatches' ) ) { |
| 56 |
return $layout_values; |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! empty( $layout_values ) ) { |
| 60 |
|
| 61 |
foreach ( $layout_values as $key => $value ) { |
| 62 |
if ( $value['id'] === 'product_add_to_cart' ) { |
| 63 |
$layout_values[ $key ]['enabled'] = false; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $layout_values; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Load compatibility styles if the Blocksy theme is installed and active. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function styles() { |
| 77 |
if ( ! merchant_is_blocksy_active() ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
wp_enqueue_style( |
| 82 |
'merchant-blocksy-compatibility', |
| 83 |
MERCHANT_URI . 'assets/css/compatibility/blocksy/style.min.css', |
| 84 |
array(), |
| 85 |
MERCHANT_VERSION |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Handle with all required compatibility with 'Product Video' module. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function product_video_module() { |
| 95 |
if ( ! merchant_is_blocksy_active() ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! Merchant_Modules::is_module_active( 'product-video' ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
// Removes the theme's default product image on archive pages. |
| 104 |
add_filter( 'theme_mod_woo_card_layout', function($mod_settings){ |
| 105 |
global $post; |
| 106 |
|
| 107 |
if ( ! isset( $post ) ) { |
| 108 |
return $mod_settings; |
| 109 |
} |
| 110 |
|
| 111 |
$has_featured_video = get_post_meta( $post->ID, '_merchant_enable_featured_video', true ); |
| 112 |
if ( ! $has_featured_video ) { |
| 113 |
return $mod_settings; |
| 114 |
} |
| 115 |
|
| 116 |
foreach( $mod_settings as $index => $setting ) { |
| 117 |
if ( isset( $setting['id'] ) && $setting['id'] === 'product_image' ) { |
| 118 |
$mod_settings[$index]['enabled'] = false; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $mod_settings; |
| 123 |
} ); |
| 124 |
|
| 125 |
// Replaces the theme's default image gallery with the default from WooCommerce. |
| 126 |
add_filter( 'blocksy:woocommerce:product-view:use-default', '__return_true' ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Handle with all required compatibility with 'Product Audio' module. |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function product_audio_module() { |
| 135 |
if ( ! merchant_is_blocksy_active() ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
if ( ! Merchant_Modules::is_module_active( 'product-audio' ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
// Removes the theme's default product image on archive pages. |
| 144 |
add_filter( 'theme_mod_woo_card_layout', function($mod_settings){ |
| 145 |
global $post; |
| 146 |
|
| 147 |
if ( ! isset( $post ) ) { |
| 148 |
return $mod_settings; |
| 149 |
} |
| 150 |
|
| 151 |
$has_featured_audio = get_post_meta( $post->ID, '_merchant_enable_featured_audio', true ); |
| 152 |
if ( ! $has_featured_audio ) { |
| 153 |
return $mod_settings; |
| 154 |
} |
| 155 |
|
| 156 |
foreach( $mod_settings as $index => $setting ) { |
| 157 |
if ( isset( $setting['id'] ) && $setting['id'] === 'product_image' ) { |
| 158 |
$mod_settings[$index]['enabled'] = false; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return $mod_settings; |
| 163 |
} ); |
| 164 |
|
| 165 |
// Replaces the theme's default image gallery with the default from WooCommerce. |
| 166 |
add_filter( 'blocksy:woocommerce:product-view:use-default', '__return_true' ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* The class object can be accessed with "global $blocksy_compatibility", to allow removing actions. |
| 172 |
* Improving Third-party integrations. |
| 173 |
*/ |
| 174 |
$merchant_blocksy_compatibility = new Merchant_Blocksy_Theme(); |
| 175 |
} |
| 176 |
|