| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main FilterHooks class. |
| 4 |
* |
| 5 |
* @package RadiusTheme\SB |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace RadiusTheme\SB\Modules\VariationSwatches; |
| 9 |
|
| 10 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 11 |
use RadiusTheme\SB\Helpers\Cache; |
| 12 |
use RadiusTheme\SB\Helpers\Fns; |
| 13 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Main FilterHooks class. |
| 19 |
*/ |
| 20 |
class SwatchesHooks { |
| 21 |
/** |
| 22 |
* Singleton Trait. |
| 23 |
*/ |
| 24 |
use SingletonTrait; |
| 25 |
|
| 26 |
/** |
| 27 |
* Module Class Constructor. |
| 28 |
*/ |
| 29 |
private function __construct() { |
| 30 |
add_filter( 'post_class', [ $this, 'product_loop_post_class' ], 25 ); |
| 31 |
add_filter( 'rtsb/builder/content/parent_class', [ $this, 'filter_parent_classes' ] ); |
| 32 |
|
| 33 |
add_filter( 'woocommerce_available_variation', [ $this, 'available_variation' ], 100, 3 ); |
| 34 |
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', [ $this, 'variation_attribute_options_html' ], 200, 2 ); |
| 35 |
if ( ! is_admin() ) { |
| 36 |
add_filter( 'woocommerce_ajax_variation_threshold', [ $this, 'ajax_variation_threshold' ], 99 ); |
| 37 |
} |
| 38 |
add_action( 'wp_ajax_rtsb_load_product_variation', [ $this, 'load_product_variation' ] ); |
| 39 |
add_action( 'wp_ajax_nopriv_rtsb_load_product_variation', [ $this, 'load_product_variation' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Filter the parent wrapper CSS classes. |
| 44 |
* |
| 45 |
* @param array $classes Existing classes. |
| 46 |
* @return array Modified classes. |
| 47 |
*/ |
| 48 |
public function filter_parent_classes( $classes ) { |
| 49 |
if ( ! BuilderFns::is_product() ) { |
| 50 |
return $classes; |
| 51 |
} |
| 52 |
$_product = Fns::get_product(); |
| 53 |
if ( $_product->is_type( 'variable' ) ) { |
| 54 |
$classes[] = 'rtsb-vs-product'; |
| 55 |
} |
| 56 |
return $classes; |
| 57 |
} |
| 58 |
/** |
| 59 |
* Available Variation |
| 60 |
* |
| 61 |
* @param array $variation variation. |
| 62 |
* @param \WC_Product $product product. |
| 63 |
* @param \WC_Product_Variable $variationObj variation object. |
| 64 |
*/ |
| 65 |
public function available_variation( $variation, $product, $variationObj ) { |
| 66 |
$attachment_id = $variationObj->get_image_id(); |
| 67 |
$default_size = 'woocommerce_thumbnail'; |
| 68 |
if ( ( ! is_admin() || wp_doing_ajax() ) && ! is_product() ) { |
| 69 |
$default_size = SwatchesFns::get_options( 'showcase_image_size', $default_size ); |
| 70 |
} |
| 71 |
$thumbnail_size = apply_filters( 'woocommerce_thumbnail_size', $default_size ); |
| 72 |
$image_Src = wp_get_attachment_image_src( $attachment_id, $default_size ); |
| 73 |
if ( isset( $variation['image']['thumb_src'] ) && ! empty( $variation['image']['thumb_src'] ) ) { |
| 74 |
$thumb_srcset = function_exists( 'wp_get_attachment_image_srcset' ) ? wp_get_attachment_image_srcset( $attachment_id, $thumbnail_size ) : false; |
| 75 |
$thumb_sizes = function_exists( 'wp_get_attachment_image_sizes' ) ? wp_get_attachment_image_sizes( $attachment_id, $thumbnail_size ) : false; |
| 76 |
$variation['image']['thumb_srcset'] = apply_filters( 'rtsb/thumb/srcset', $thumb_srcset, $variation, $product, $variationObj ); |
| 77 |
$variation['image']['thumb_sizes'] = apply_filters( 'rtsb/thumb/sizes', $thumb_sizes, $variation, $product, $variationObj ); |
| 78 |
} |
| 79 |
if ( ! empty( $image_Src[0] ) ) { |
| 80 |
$variation['image']['src'] = $image_Src[0]; |
| 81 |
$variation['image']['src_w'] = $image_Src[1] ?? ''; |
| 82 |
$variation['image']['src_h'] = $image_Src[2] ?? ''; |
| 83 |
$variation['image']['sizes'] = wp_get_attachment_image_sizes( $attachment_id, $thumbnail_size ); |
| 84 |
$variation['image']['srcset'] = wp_get_attachment_image_srcset( $attachment_id, $thumbnail_size ); |
| 85 |
} |
| 86 |
if ( 'on' === SwatchesFns::get_options( 'disable_out_of_stock' ) && ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ! is_admin() ) ) { |
| 87 |
return $variationObj->is_in_stock() ? $variation : false; |
| 88 |
} |
| 89 |
|
| 90 |
return $variation; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/** |
| 95 |
* Add Product Class |
| 96 |
* |
| 97 |
* @param string $classes product wrapper class. |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function product_loop_post_class( $classes ) { |
| 101 |
global $product; |
| 102 |
if ( ! $product instanceof \WC_Product ) { |
| 103 |
return $classes; |
| 104 |
} |
| 105 |
if ( wp_is_mobile() ) { |
| 106 |
$classes[] = 'rtsb-is-mobile'; |
| 107 |
} |
| 108 |
if ( $product->is_type( 'variable' ) ) { |
| 109 |
$classes[] = 'rtsb-vs-product'; |
| 110 |
$classes[] = 'rtsb-' . SwatchesFns::get_options( 'shape_style' ); |
| 111 |
$classes[] = 'rtsb-attribute-behavior-' . SwatchesFns::get_options( 'disabled_attribute_behavior' ); |
| 112 |
if ( 'on' === SwatchesFns::get_options( 'show_tooltip' ) ) { |
| 113 |
$classes[] = 'rtsb-tooltip'; |
| 114 |
} |
| 115 |
} |
| 116 |
return $classes; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Undocumented function |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function load_product_variation() { |
| 125 |
if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { |
| 126 |
wp_send_json_error( esc_html__( 'Something Went Wrong', 'shopbuilder' ) ); |
| 127 |
} |
| 128 |
$product_id = ! empty( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 129 |
$product = wc_get_product( $product_id ); |
| 130 |
$available_variations = $product->get_available_variations(); |
| 131 |
wp_send_json_success( $available_variations ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Ajax Variation Threshold. |
| 136 |
* |
| 137 |
* @return int |
| 138 |
*/ |
| 139 |
public function ajax_variation_threshold() { |
| 140 |
return 1; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Variation Attribute Options Html. |
| 145 |
* |
| 146 |
* @param string $html Html. |
| 147 |
* @param array $args Args. |
| 148 |
* |
| 149 |
* @return string |
| 150 |
*/ |
| 151 |
public function variation_attribute_options_html( $html, $args ) { |
| 152 |
if ( apply_filters( 'rtsb/default/variation/attribute/options/html', false, $args, $html ) ) { |
| 153 |
return $html; |
| 154 |
} |
| 155 |
// WooCommerce Product Bundle Fixing. |
| 156 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 157 |
if ( 'woocommerce_configure_bundle_order_item' === wp_unslash( $_POST['action'] ?? '' ) ) { |
| 158 |
return $html; |
| 159 |
} |
| 160 |
if ( empty( $args['attribute'] ) ) { |
| 161 |
return $html; |
| 162 |
} |
| 163 |
|
| 164 |
if ( empty( $args['product'] ) ) { |
| 165 |
return $html; |
| 166 |
} |
| 167 |
return SwatchesFns::generate_variation_attribute_option_html( apply_filters( 'rtsb/vs/variation/attribute/options/args', $args ), $html ); |
| 168 |
} |
| 169 |
} |
| 170 |
|