| 1 |
<?php |
| 2 |
|
| 3 |
namespace RadiusTheme\SB\Controllers; |
| 4 |
|
| 5 |
use RadiusTheme\SB\Helpers\BuilderFns; |
| 6 |
use RadiusTheme\SB\Helpers\Fns; |
| 7 |
use RadiusTheme\SB\Models\Settings; |
| 8 |
use RadiusTheme\SB\Traits\SingletonTrait; |
| 9 |
|
| 10 |
// Do not allow directly accessing this file. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit( 'This script cannot be accessed directly.' ); |
| 13 |
} |
| 14 |
|
| 15 |
class AssetsController { |
| 16 |
|
| 17 |
use SingletonTrait; |
| 18 |
|
| 19 |
/** |
| 20 |
* Plugin version |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $version; |
| 25 |
|
| 26 |
/** |
| 27 |
* Ajax URL |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $ajaxurl; |
| 32 |
|
| 33 |
/** |
| 34 |
* Styles. |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
private $styles = []; |
| 39 |
|
| 40 |
/** |
| 41 |
* Scripts. |
| 42 |
* |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
private $scripts = []; |
| 46 |
|
| 47 |
/** |
| 48 |
* Class Constructor |
| 49 |
*/ |
| 50 |
public function __construct() { |
| 51 |
$this->version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : RTSB_VERSION; |
| 52 |
if ( in_array( 'sitepress-multilingual-cms/sitepress.php', get_option( 'active_plugins' ) ) ) { |
| 53 |
$this->ajaxurl = admin_url( 'admin-ajax.php?lang=' . ICL_LANGUAGE_CODE ); |
| 54 |
} else { |
| 55 |
$this->ajaxurl = admin_url( 'admin-ajax.php' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Admin scripts. |
| 60 |
*/ |
| 61 |
add_action( 'admin_enqueue_scripts', [ $this, 'register_backend_assets' ], 1 ); |
| 62 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_backend_scripts' ] ); |
| 63 |
|
| 64 |
/** |
| 65 |
* Public scripts. |
| 66 |
*/ |
| 67 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_public_scripts' ], 15 ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get all frontend scripts. |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
private function get_public_assets() { |
| 76 |
$this->get_public_styles()->get_public_scripts(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get public styles. |
| 81 |
* |
| 82 |
* @return object |
| 83 |
*/ |
| 84 |
private function get_public_styles() { |
| 85 |
$this->styles[] = [ |
| 86 |
'handle' => 'rtsb-fonts', |
| 87 |
'src' => rtsb()->get_assets_uri( 'fonts/rtsbfont.css' ), |
| 88 |
]; |
| 89 |
|
| 90 |
$this->styles[] = [ |
| 91 |
'handle' => 'rtsb-frontend', |
| 92 |
'src' => rtsb()->get_assets_uri( 'css/frontend/frontend.css' ), |
| 93 |
]; |
| 94 |
|
| 95 |
$this->styles[] = [ |
| 96 |
'handle' => 'swiper', |
| 97 |
'src' => rtsb()->get_assets_uri( 'vendor/swiper/css/swiper-bundle.min.css' ), |
| 98 |
]; |
| 99 |
|
| 100 |
if ( BuilderFns::is_builder_preview() && 'elementor' == Fns::page_edit_with( get_the_ID() ) ) { |
| 101 |
$this->styles[] = [ |
| 102 |
'handle' => 'photoswipe', |
| 103 |
'src' => plugins_url( 'assets/css/photoswipe/photoswipe.min.css', WC_PLUGIN_FILE ), |
| 104 |
]; |
| 105 |
|
| 106 |
$this->styles[] = [ |
| 107 |
'handle' => 'photoswipe-default-skin', |
| 108 |
'src' => plugins_url( 'assets/css/photoswipe/default-skin/default-skin.min.css', WC_PLUGIN_FILE ), |
| 109 |
]; |
| 110 |
|
| 111 |
// Load only for elementor editor page and fix some issue. |
| 112 |
$this->styles[] = [ |
| 113 |
'handle' => 'elementor-editor-style-fix', |
| 114 |
'src' => rtsb()->get_assets_uri( 'css/backend/elementor-editor-style-fix.css' ), |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
return $this; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get public scripts. |
| 123 |
* |
| 124 |
* @return object |
| 125 |
*/ |
| 126 |
private function get_public_scripts() { |
| 127 |
$this->scripts[] = [ |
| 128 |
'handle' => 'swiper', |
| 129 |
'src' => rtsb()->get_assets_uri( 'vendor/swiper/js/swiper-bundle.min.js' ), |
| 130 |
'deps' => [ 'jquery' ], |
| 131 |
'footer' => true, |
| 132 |
]; |
| 133 |
|
| 134 |
$this->scripts[] = [ |
| 135 |
'handle' => 'rtsb-imagesloaded', |
| 136 |
'src' => rtsb()->get_assets_uri( 'vendor/isotope/imagesloaded.pkgd.min.js' ), |
| 137 |
'deps' => [ 'jquery' ], |
| 138 |
'footer' => true, |
| 139 |
]; |
| 140 |
|
| 141 |
$this->scripts[] = [ |
| 142 |
'handle' => 'rtsb-tipsy', |
| 143 |
'src' => rtsb()->get_assets_uri( 'vendor/tipsy/tipsy.min.js' ), |
| 144 |
'deps' => [ 'jquery' ], |
| 145 |
'footer' => true, |
| 146 |
]; |
| 147 |
|
| 148 |
if ( BuilderFns::is_builder_preview() && 'elementor' == Fns::page_edit_with( get_the_ID() ) ) { |
| 149 |
|
| 150 |
$this->scripts[] = [ |
| 151 |
'handle' => 'flexslider', |
| 152 |
'src' => plugins_url( 'assets/js/flexslider/jquery.flexslider.js', WC_PLUGIN_FILE ), |
| 153 |
'deps' => [ 'jquery' ], |
| 154 |
'footer' => true, |
| 155 |
]; |
| 156 |
|
| 157 |
$this->scripts[] = [ |
| 158 |
'handle' => 'photoswipe', |
| 159 |
'src' => plugins_url( 'assets/js/photoswipe/photoswipe.js', WC_PLUGIN_FILE ), |
| 160 |
'deps' => [ 'jquery' ], |
| 161 |
'footer' => true, |
| 162 |
]; |
| 163 |
|
| 164 |
$this->scripts[] = [ |
| 165 |
'handle' => 'zoom', |
| 166 |
'src' => plugins_url( 'assets/js/zoom/jquery.zoom.js', WC_PLUGIN_FILE ), |
| 167 |
'deps' => [ 'jquery' ], |
| 168 |
'footer' => true, |
| 169 |
]; |
| 170 |
|
| 171 |
$this->scripts[] = [ |
| 172 |
'handle' => 'photoswipe-ui-default', |
| 173 |
'src' => plugins_url( 'assets/js/photoswipe/photoswipe-ui-default.js', WC_PLUGIN_FILE ), |
| 174 |
'deps' => [ 'jquery', 'photoswipe' ], |
| 175 |
'footer' => true, |
| 176 |
]; |
| 177 |
|
| 178 |
$this->scripts[] = [ |
| 179 |
'handle' => 'wc-single-product', |
| 180 |
'src' => plugins_url( 'assets/js/frontend/single-product.js', WC_PLUGIN_FILE ), |
| 181 |
'deps' => [ 'jquery', 'flexslider', 'photoswipe', 'photoswipe-ui-default', 'zoom' ], |
| 182 |
'footer' => true, |
| 183 |
]; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
$this->scripts[] = [ |
| 188 |
'handle' => 'rtsb-public', |
| 189 |
'src' => rtsb()->get_assets_uri( 'js/frontend/frontend.js' ), |
| 190 |
'deps' => [ 'jquery', 'rtsb-imagesloaded', 'swiper' ], |
| 191 |
'footer' => true, |
| 192 |
]; |
| 193 |
|
| 194 |
return $this; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Register public scripts. |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
public function register_public_scripts() { |
| 203 |
$this->get_public_assets(); |
| 204 |
|
| 205 |
// Register public styles. |
| 206 |
foreach ( $this->styles as $style ) { |
| 207 |
wp_register_style( $style['handle'], $style['src'], '', $this->version ); |
| 208 |
} |
| 209 |
|
| 210 |
// Register public scripts. |
| 211 |
foreach ( $this->scripts as $script ) { |
| 212 |
wp_register_script( $script['handle'], $script['src'], $script['deps'], $this->version, $script['footer'] ); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Enqueues public scripts. |
| 218 |
* |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
public function enqueue_public_scripts() { |
| 222 |
/** |
| 223 |
* Register scripts. |
| 224 |
*/ |
| 225 |
$this->register_public_scripts(); |
| 226 |
/** |
| 227 |
* Enqueue scripts. |
| 228 |
*/ |
| 229 |
if ( BuilderFns::is_builder_preview() && 'elementor' === Fns::page_edit_with( get_the_ID() ) ) { |
| 230 |
/** |
| 231 |
* Styles. |
| 232 |
*/ |
| 233 |
wp_enqueue_style( 'swiper' ); |
| 234 |
wp_enqueue_style( 'photoswipe' ); |
| 235 |
wp_enqueue_style( 'photoswipe-default-skin' ); |
| 236 |
wp_enqueue_style( 'elementor-editor-style-fix' ); |
| 237 |
// TODO: Need to remove 'woocommerce-general' if conflict arises. |
| 238 |
wp_enqueue_style( 'woocommerce-general' ); |
| 239 |
|
| 240 |
/** |
| 241 |
* Scripts. |
| 242 |
*/ |
| 243 |
wp_enqueue_script( 'flexslider' ); |
| 244 |
wp_enqueue_script( 'wc-single-product' ); |
| 245 |
wp_dequeue_script( 'rtsb-public' ); |
| 246 |
wp_enqueue_script( 'swiper' ); |
| 247 |
wp_enqueue_script( 'rtsb-public' ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Styles. |
| 252 |
*/ |
| 253 |
wp_enqueue_style( 'rtsb-fonts' ); |
| 254 |
wp_enqueue_style( 'rtsb-frontend' ); |
| 255 |
|
| 256 |
/** |
| 257 |
* Scripts. |
| 258 |
*/ |
| 259 |
wp_enqueue_script( 'rtsb-imagesloaded' ); |
| 260 |
wp_enqueue_script( 'rtsb-tipsy' ); |
| 261 |
wp_enqueue_script( 'rtsb-public' ); |
| 262 |
|
| 263 |
wp_localize_script( |
| 264 |
'rtsb-public', |
| 265 |
'rtsbPublicParams', |
| 266 |
[ |
| 267 |
'ajaxUrl' => esc_url( $this->ajaxurl ), |
| 268 |
'homeurl' => home_url(), |
| 269 |
'wcCartUrl' => wc_get_cart_url(), |
| 270 |
'addedToCartText' => esc_html__( 'Product Added', 'shopbuilder' ), |
| 271 |
'browseCartText' => esc_html__( 'Browse Cart', 'shopbuilder' ), |
| 272 |
rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ), |
| 273 |
] |
| 274 |
); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Registers Admin scripts. |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
public function register_backend_assets() { |
| 283 |
/** |
| 284 |
* Styles. |
| 285 |
*/ |
| 286 |
wp_register_style( 'rtsb-admin-app', rtsb()->get_assets_uri( 'css/backend/admin-settings.css' ), '', $this->version ); |
| 287 |
wp_register_style( 'rtsb-admin-global', rtsb()->get_assets_uri( 'css/backend/admin-global.css' ), '', $this->version ); |
| 288 |
wp_register_style( 'rtsb-templatebuilder', rtsb()->get_assets_uri( 'css/backend/template-builder.css' ), '', $this->version ); |
| 289 |
|
| 290 |
/** |
| 291 |
* Scripts. |
| 292 |
*/ |
| 293 |
wp_register_script( 'rtsb-admin-app', rtsb()->get_assets_uri( 'js/backend/admin-settings.js' ), '', $this->version, true ); |
| 294 |
wp_register_script( 'rtsb-templatebuilder', rtsb()->get_assets_uri( 'js/backend/template-builder.js' ), '', $this->version, true ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Enqueues admin scripts. |
| 299 |
* |
| 300 |
* @param string $hook Hooks. |
| 301 |
* |
| 302 |
* @return void |
| 303 |
*/ |
| 304 |
public function enqueue_backend_scripts( $hook ) { |
| 305 |
|
| 306 |
wp_enqueue_style( 'rtsb-admin-global' ); |
| 307 |
|
| 308 |
if ( 'shopbuilder_page_rtsb-settings' === $hook ) { |
| 309 |
/** |
| 310 |
* Styles. |
| 311 |
*/ |
| 312 |
wp_enqueue_style( 'rtsb-admin-app' ); |
| 313 |
// add_thickbox(); |
| 314 |
// wp_enqueue_script( 'plugin-install' ); |
| 315 |
wp_enqueue_script( 'updates' ); |
| 316 |
|
| 317 |
/** |
| 318 |
* Scripts. |
| 319 |
*/ |
| 320 |
wp_enqueue_script( 'rtsb-admin-app' ); |
| 321 |
wp_localize_script( |
| 322 |
'rtsb-admin-app', |
| 323 |
'rtsbParams', |
| 324 |
[ |
| 325 |
'ajaxurl' => esc_url( $this->ajaxurl ), |
| 326 |
'homeurl' => home_url(), |
| 327 |
'nonce' => wp_create_nonce( rtsb()->nonceText ), |
| 328 |
'sections' => Settings::instance()->get_sections(), |
| 329 |
'pages' => Fns::get_pages(), |
| 330 |
] |
| 331 |
); |
| 332 |
} else { |
| 333 |
$current_screen = get_current_screen(); |
| 334 |
|
| 335 |
if ( 'edit-rtsb_builder' === $current_screen->id ) { |
| 336 |
/** |
| 337 |
* Styles. |
| 338 |
*/ |
| 339 |
wp_enqueue_style( 'rtsb-templatebuilder' ); |
| 340 |
|
| 341 |
/** |
| 342 |
* Scripts. |
| 343 |
*/ |
| 344 |
wp_enqueue_script( 'rtsb-templatebuilder' ); |
| 345 |
wp_localize_script( |
| 346 |
'rtsb-templatebuilder', |
| 347 |
'rtsbParams', |
| 348 |
[ |
| 349 |
'ajaxurl' => esc_url( $this->ajaxurl ), |
| 350 |
'homeurl' => home_url(), |
| 351 |
rtsb()->nonceId => wp_create_nonce( rtsb()->nonceText ), |
| 352 |
] |
| 353 |
); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|