| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Modules\VariationGallery; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 6 |
use RadiusTheme\SB\Helpers\Fns; |
| 7 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 8 |
|
| 9 |
// Do not allow directly accessing this file. |
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit( 'This script cannot be accessed directly.' ); |
| 12 |
} |
| 13 |
/** |
| 14 |
* Variation Swatches |
| 15 |
*/ |
| 16 |
final class VariationGalleryInit { |
| 17 |
|
| 18 |
/** |
| 19 |
* SingleTon |
| 20 |
*/ |
| 21 |
use SingletonTrait; |
| 22 |
|
| 23 |
/** |
| 24 |
* Asset Handle |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $handle = 'rtsb-variation-gallery'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
private function __construct() { |
| 36 |
$already_active = Fns::check_plugin_active( 'woo-product-variation-gallery/woo-product-variation-gallery.php' ); |
| 37 |
if ( ! $already_active ) { |
| 38 |
$this->scripts(); |
| 39 |
$this->init(); |
| 40 |
do_action( 'rtsb/variation/gallery/init' ); |
| 41 |
} |
| 42 |
} |
| 43 |
/** |
| 44 |
* Initialize Functionality. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
private function scripts() { |
| 49 |
add_filter( 'rtsb/optimizer/scripts/deps', [ $this, 'extend_shared_dependencies' ] ); |
| 50 |
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_assets' ], 99 ); |
| 51 |
add_action( 'admin_enqueue_scripts', [ $this, 'register_backend_assets' ], 1 ); |
| 52 |
} |
| 53 |
/** |
| 54 |
* Initialize Functionality. |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
private function init() { |
| 59 |
GalleryAdmin::instance(); |
| 60 |
GalleryFrontEnd::instance(); |
| 61 |
} |
| 62 |
/** |
| 63 |
* Shared dependencies. |
| 64 |
* |
| 65 |
* @param array $deps Dependencies. |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function extend_shared_dependencies( $deps ) { |
| 70 |
if ( is_product() || BuilderFns::is_product() ) { |
| 71 |
$deps[] = 'swiper'; |
| 72 |
} |
| 73 |
return $deps; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Assets. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function frontend_assets() { |
| 82 |
wp_dequeue_script( 'wc-flexslider' ); |
| 83 |
// Enqueue assets. |
| 84 |
$this->handle = Fns::enqueue_module_assets( |
| 85 |
$this->handle, |
| 86 |
'variation-gallery', |
| 87 |
[ |
| 88 |
'context' => ( function_exists( 'rtsbpro' ) && rtsb()->has_pro() ) ? rtsbpro() : rtsb(), |
| 89 |
'version' => rtsb()->has_pro() ? RTSBPRO_VERSION : RTSB_VERSION, |
| 90 |
'type' => 'css', |
| 91 |
] |
| 92 |
); |
| 93 |
$this->handle = Fns::enqueue_module_assets( |
| 94 |
$this->handle, |
| 95 |
'variation-gallery', |
| 96 |
[ |
| 97 |
'context' => rtsb(), |
| 98 |
'version' => RTSB_VERSION, |
| 99 |
'type' => 'js', |
| 100 |
] |
| 101 |
); |
| 102 |
$this->frontend_dynamic_css(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
private function frontend_dynamic_css() { |
| 109 |
$options = GalleryFns::get_options(); |
| 110 |
$dynamic_css = ':root{'; |
| 111 |
$alignment = 'on' === ( $options['main_slider_alignment'] ?? '' ) ? 'center' : 'flex-start'; |
| 112 |
$col = GalleryFns::get_options( 'thumbnails_columns', 3 ); |
| 113 |
$dynamic_css .= '--vg-main-slider-v-alignment:' . $alignment . ';'; |
| 114 |
$dynamic_css .= '--vg-grid-column:' . absint( $col ) . ';'; |
| 115 |
$dynamic_css .= '--vg-main-slider-border-color:' . ( ! empty( $options['main_image_border_color'] ) ? $options['main_image_border_color'] : 'transparent' ) . ';'; |
| 116 |
$dynamic_css .= '--vg-thumb-border-color:' . ( ! empty( $options['thumbnail_item_border_color'] ) ? $options['thumbnail_item_border_color'] : '#eee' ) . ';'; |
| 117 |
$dynamic_css .= '--vg-thumb-item-inner-padding:' . ( ! empty( $options['thumbnail_item_inner_padding'] ) ? $options['thumbnail_item_inner_padding'] : '8' ) . 'px;'; |
| 118 |
$dynamic_css .= '--vg-thumb-border-radius:' . ( ! empty( $options['thumbnail_item_border_radius'] ) ? absint( $options['thumbnail_item_border_radius'] ) : '3' ) . 'px;'; |
| 119 |
$dynamic_css .= '--vg-thumb-gap:' . ( ! empty( $options['thumbnails_gap'] ) ? absint( $options['thumbnails_gap'] ) : '10' ) . 'px;'; |
| 120 |
if ( ! empty( $options['main_image_section_width'] ) && absint( $options['image_section_height'] ) ) { |
| 121 |
$dynamic_css .= '--vg-image-slider-height:' . absint( $options['image_section_height'] ) . 'px;'; |
| 122 |
} |
| 123 |
if ( ! empty( $options['thumb_image_section_width'] ) && absint( $options['thumb_image_section_width'] ) ) { |
| 124 |
$dynamic_css .= '--vg-thumb-slider-width:' . absint( $options['thumb_image_section_width'] ) . 'px;'; |
| 125 |
} |
| 126 |
$dynamic_css .= '}'; |
| 127 |
if ( ! empty( $dynamic_css ) ) { |
| 128 |
$dynamic_css = apply_filters( 'rtsb/variation/gallery/dynamic/css', $dynamic_css, $options ); |
| 129 |
wp_add_inline_style( $this->handle, $dynamic_css ); |
| 130 |
} |
| 131 |
} |
| 132 |
/** |
| 133 |
* Assets. |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function register_backend_assets() { |
| 138 |
global $post; |
| 139 |
$current_screen = get_current_screen(); |
| 140 |
$screen_id = $current_screen ? $current_screen->id : ''; |
| 141 |
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 142 |
if ( in_array( 'sitepress-multilingual-cms/sitepress.php', get_option( 'active_plugins' ) ) ) { |
| 143 |
$ajaxurl = admin_url( 'admin-ajax.php?lang=' . ICL_LANGUAGE_CODE ); |
| 144 |
} else { |
| 145 |
$ajaxurl = admin_url( 'admin-ajax.php' ); |
| 146 |
} |
| 147 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 148 |
if ( ( 'product' === sanitize_text_field( wp_unslash( $_GET['post_type'] ?? '' ) ) && isset( $_GET['taxonomy'] ) ) || 'product' === $screen_id ) { |
| 149 |
wp_register_style( 'rtsb-variation-gallery-admin', rtsb()->get_assets_uri( 'css/backend/variation-gallery-admin.css' ), '', '1.0' ); |
| 150 |
wp_register_script( 'rtsb-variation-gallery-admin', rtsb()->get_assets_uri( 'js/backend/variation-gallery-admin.js' ), '', '1.0', true ); |
| 151 |
wp_enqueue_script( 'rtsb-variation-gallery-admin' ); |
| 152 |
wp_enqueue_style( 'rtsb-variation-gallery-admin' ); |
| 153 |
wp_localize_script( |
| 154 |
'rtsb-variation-gallery-admin', |
| 155 |
'rtsbVgParams', |
| 156 |
[ |
| 157 |
'ajaxurl' => esc_url( $ajaxurl ), |
| 158 |
'admin_url' => esc_url( admin_url() ), |
| 159 |
'pro_version' => defined( 'RTSBPRO_VERSION' ) ? RTSBPRO_VERSION : false, |
| 160 |
'nonce' => wp_create_nonce( 'rtsb_vg_nonce' ), |
| 161 |
'media_title' => esc_html__( 'Choose an Image', 'shopbuilder' ), |
| 162 |
'button_title' => esc_html__( 'Use Image', 'shopbuilder' ), |
| 163 |
'add_media' => esc_html__( 'Add Media', 'shopbuilder' ), |
| 164 |
'add_video' => esc_html__( 'Add Video', 'shopbuilder' ), |
| 165 |
'sure_txt' => esc_html__( 'Are you sure to delete?', 'shopbuilder' ), |
| 166 |
] |
| 167 |
); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|