| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Modules\VariationSwatches; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\Fns; |
| 6 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 7 |
|
| 8 |
// Do not allow directly accessing this file. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit( 'This script cannot be accessed directly.' ); |
| 11 |
} |
| 12 |
/** |
| 13 |
* Variation Swatches |
| 14 |
*/ |
| 15 |
final class VariationSwatches { |
| 16 |
|
| 17 |
/** |
| 18 |
* SingleTon |
| 19 |
*/ |
| 20 |
use SingletonTrait; |
| 21 |
|
| 22 |
/** |
| 23 |
* Asset Handle |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $handle = 'rtsb-variation-swatches'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
private function __construct() { |
| 35 |
$already_active = Fns::check_plugin_active( 'woo-product-variation-swatches/woo-product-variation-swatches.php' ); |
| 36 |
if ( ! $already_active ) { |
| 37 |
$this->scripts(); |
| 38 |
$this->init(); |
| 39 |
do_action( 'rtsb/variation/swatches/init' ); |
| 40 |
} |
| 41 |
} |
| 42 |
/** |
| 43 |
* Initialize Functionality. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
private function scripts() { |
| 48 |
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_assets' ], 99 ); |
| 49 |
add_action( 'admin_enqueue_scripts', [ $this, 'register_backend_assets' ], 1 ); |
| 50 |
} |
| 51 |
/** |
| 52 |
* Initialize Functionality. |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
private function init() { |
| 57 |
SwatchesHooks::instance(); |
| 58 |
SwatchesTerms::instance(); |
| 59 |
} |
| 60 |
/** |
| 61 |
* Assets. |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function frontend_assets() { |
| 66 |
// Enqueue assets. |
| 67 |
$this->handle = Fns::enqueue_module_assets( |
| 68 |
$this->handle, |
| 69 |
'variation-swatches', |
| 70 |
[ |
| 71 |
'context' => ( function_exists( 'rtsbpro' ) && rtsb()->has_pro() ) ? rtsbpro() : rtsb(), |
| 72 |
'version' => rtsb()->has_pro() ? RTSBPRO_VERSION : RTSB_VERSION, |
| 73 |
] |
| 74 |
); |
| 75 |
|
| 76 |
$this->frontend_dynamic_css(); |
| 77 |
wp_localize_script( |
| 78 |
$this->handle, |
| 79 |
'rtsbVsParams', |
| 80 |
apply_filters( |
| 81 |
'rtsb/vs/js/object', |
| 82 |
[ |
| 83 |
'is_product_page' => is_product(), |
| 84 |
'ajax_url' => WC()->ajax_url(), |
| 85 |
'hasPro' => rtsb()->has_pro(), |
| 86 |
rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ), |
| 87 |
'attr_beside_label' => 'on' === SwatchesFns::get_options( 'show_term_name_beside_label' ), |
| 88 |
// In the future it also come from settings. |
| 89 |
'enable_variation_url' => 'on' === SwatchesFns::get_options( 'enable_variation_url' ), |
| 90 |
'url_link_selector' => 'a.woocommerce-LoopProduct-link, a.add_to_cart_button', |
| 91 |
'has_wc_bundles' => false, |
| 92 |
] |
| 93 |
) |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @return void |
| 99 |
*/ |
| 100 |
private function frontend_dynamic_css() { |
| 101 |
$options = SwatchesFns::get_options(); |
| 102 |
$dynamic_css = ':root{'; |
| 103 |
if ( ! empty( $options['details_page_attr_label_font_size'] ) ) { |
| 104 |
$dynamic_css .= '--details-page-attr-label-font-size:' . $options['details_page_attr_label_font_size'] . 'px;'; |
| 105 |
} |
| 106 |
if ( ! empty( $options['details_page_attr_label_color'] ) ) { |
| 107 |
$dynamic_css .= '--details-page-attr-label-color:' . $options['details_page_attr_label_color'] . ';'; |
| 108 |
} |
| 109 |
|
| 110 |
if ( ! empty( $options['attribute_border_color'] ) ) { |
| 111 |
$dynamic_css .= '--attribute-border-color:' . $options['attribute_border_color'] . ';'; |
| 112 |
} |
| 113 |
if ( ! empty( $options['attribute_border_color_hover'] ) ) { |
| 114 |
$dynamic_css .= '--attribute-border-color-hover:' . $options['attribute_border_color_hover'] . ';'; |
| 115 |
} |
| 116 |
// Checkmark. |
| 117 |
if ( ! empty( $options['attribute_checkmark_width'] ) ) { |
| 118 |
$dynamic_css .= '--checkmark-width:' . $options['attribute_checkmark_width'] . 'px;'; |
| 119 |
} |
| 120 |
if ( ! empty( $options['attribute_checkmark_height'] ) ) { |
| 121 |
$dynamic_css .= '--checkmark-height:' . $options['attribute_checkmark_height'] . 'px;'; |
| 122 |
} |
| 123 |
if ( ! empty( $options['attribute_checkmark_icon_size'] ) ) { |
| 124 |
$dynamic_css .= '--checkmark-font-size:' . $options['attribute_checkmark_icon_size'] . 'px;'; |
| 125 |
} |
| 126 |
if ( ! empty( $options['checkmark_bg_color'] ) ) { |
| 127 |
$dynamic_css .= '--checkmark-bg-color:' . $options['checkmark_bg_color'] . ';'; |
| 128 |
} |
| 129 |
|
| 130 |
// Tooltip. |
| 131 |
if ( ! empty( $options['tooltip_bg_color'] ) ) { |
| 132 |
$dynamic_css .= '--tooltip-bg-color:' . $options['tooltip_bg_color'] . ';'; |
| 133 |
} |
| 134 |
if ( ! empty( $options['tooltip_image_bg_color'] ) ) { |
| 135 |
$dynamic_css .= '--tooltip-image-bg-color:' . $options['tooltip_image_bg_color'] . ';'; |
| 136 |
} |
| 137 |
|
| 138 |
if ( ! empty( $options['tooltip_text_color'] ) ) { |
| 139 |
$dynamic_css .= '--tooltip-text-color:' . $options['tooltip_text_color'] . ';'; |
| 140 |
} |
| 141 |
|
| 142 |
// Product Page. |
| 143 |
if ( ! empty( $options['details_page_attribute_width'] ) ) { |
| 144 |
$dynamic_css .= '--details-page-attr-width:' . $options['details_page_attribute_width'] . 'px;'; |
| 145 |
} |
| 146 |
if ( ! empty( $options['details_page_attribute_height'] ) ) { |
| 147 |
$dynamic_css .= '--details-page-attr-height:' . $options['details_page_attribute_height'] . 'px;'; |
| 148 |
} |
| 149 |
if ( ! empty( $options['details_page_attribute_font_size'] ) ) { |
| 150 |
$dynamic_css .= '--details-page-attr-font-size:' . $options['details_page_attribute_font_size'] . 'px;'; |
| 151 |
} |
| 152 |
// Showcase. |
| 153 |
if ( ! empty( $options['showcase_attribute_width'] ) ) { |
| 154 |
$dynamic_css .= '--showcase-attr-width:' . $options['showcase_attribute_width'] . 'px;'; |
| 155 |
} |
| 156 |
if ( ! empty( $options['showcase_attribute_height'] ) ) { |
| 157 |
$dynamic_css .= '--showcase-attr-height:' . $options['showcase_attribute_height'] . 'px;'; |
| 158 |
} |
| 159 |
if ( ! empty( $options['showcase_attribute_font_size'] ) ) { |
| 160 |
$dynamic_css .= '--showcase-attr-font-size:' . $options['showcase_attribute_font_size'] . 'px;'; |
| 161 |
} |
| 162 |
// Tooltips. |
| 163 |
if ( ! empty( $options['tooltip_images_padding'] ) ) { |
| 164 |
$dynamic_css .= '--tooltip-image-padding:' . $options['tooltip_images_padding'] . 'px;'; |
| 165 |
} |
| 166 |
// Tooltips. |
| 167 |
if ( ! empty( $options['attribute_border_radius'] ) ) { |
| 168 |
$dynamic_css .= '--variation-attr-border-radius:' . absint( $options['attribute_border_radius'] ) . 'px;'; |
| 169 |
} |
| 170 |
|
| 171 |
$dynamic_css .= '}'; |
| 172 |
if ( ! empty( $dynamic_css ) ) { |
| 173 |
$dynamic_css = apply_filters( 'rtsb/variation/swatches/dynamic/css', $dynamic_css, $options ); |
| 174 |
wp_add_inline_style( $this->handle, $dynamic_css ); |
| 175 |
} |
| 176 |
} |
| 177 |
/** |
| 178 |
* Assets. |
| 179 |
* |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public function register_backend_assets() { |
| 183 |
global $post; |
| 184 |
$current_screen = get_current_screen(); |
| 185 |
$screen_id = $current_screen ? $current_screen->id : ''; |
| 186 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 187 |
if ( in_array( 'sitepress-multilingual-cms/sitepress.php', get_option( 'active_plugins' ) ) ) { |
| 188 |
$ajaxurl = admin_url( 'admin-ajax.php?lang=' . ICL_LANGUAGE_CODE ); |
| 189 |
} else { |
| 190 |
$ajaxurl = admin_url( 'admin-ajax.php' ); |
| 191 |
} |
| 192 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 193 |
if ( ( 'product' === sanitize_text_field( wp_unslash( $_GET['post_type'] ?? '' ) ) && isset( $_GET['taxonomy'] ) ) || 'product' === $screen_id ) { |
| 194 |
wp_register_script( 'rtsb-variation-swatch-admin', rtsb()->get_assets_uri( 'js/backend/variation-swatch-admin.js' ), '', '1.0', true ); |
| 195 |
wp_localize_script( |
| 196 |
'rtsb-variation-swatch-admin', |
| 197 |
'rtsbVsParams', |
| 198 |
[ |
| 199 |
'ajaxurl' => esc_url( $ajaxurl ), |
| 200 |
'media_title' => esc_html__( 'Choose an Image', 'shopbuilder' ), |
| 201 |
'button_title' => esc_html__( 'Use Image', 'shopbuilder' ), |
| 202 |
'add_media' => esc_html__( 'Add Media', 'shopbuilder' ), |
| 203 |
'reset_notice' => esc_html__( 'Are you sure to reset', 'shopbuilder' ), |
| 204 |
rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ), |
| 205 |
'post_id' => 'product' === $screen_id ? $post->ID : null, |
| 206 |
'attribute_types' => SwatchesFns::get_available_attributes_types(), |
| 207 |
] |
| 208 |
); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|