| 1 |
<?php |
| 2 |
/** |
| 3 |
* BuilderFns class |
| 4 |
* |
| 5 |
* The builder. |
| 6 |
* |
| 7 |
* @package RadiusTheme\SB |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace RadiusTheme\SB\Helpers; |
| 12 |
|
| 13 |
// Do not allow directly accessing this file. |
| 14 |
use RadiusTheme\SB\Models\TemplateSettings; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit( 'This script cannot be accessed directly.' ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* BuilderFns class |
| 22 |
*/ |
| 23 |
class BuilderFns { |
| 24 |
/** |
| 25 |
* Local Cache. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private static $cache = []; |
| 30 |
|
| 31 |
/** |
| 32 |
* Template builder post type |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
public static $post_type_tb = 'rtsb_builder'; |
| 37 |
/** |
| 38 |
* Template builder |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public static $template_meta = 'rtsb_tb_template'; |
| 43 |
/** |
| 44 |
* Template builder |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public static $product_template_meta = 'rtsb_selected_product_template'; |
| 49 |
/** |
| 50 |
* Template builder |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
public static $view_template_for_products_meta = 'rtsb_template_for_selected_products'; |
| 55 |
/** |
| 56 |
* Template builder |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
public static $view_template_for_archive_meta = 'rtsb_template_for_selected_category'; |
| 61 |
|
| 62 |
/** |
| 63 |
* Template builder |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
public static $categories_product_page_template = 'rtsb_categories_product_page_template'; |
| 68 |
/** |
| 69 |
* Template builder |
| 70 |
* |
| 71 |
* @var string |
| 72 |
*/ |
| 73 |
public static $tags_product_page_template = 'rtsb_tags_product_page_template'; |
| 74 |
/** |
| 75 |
* Page builder id. |
| 76 |
* |
| 77 |
* @return int |
| 78 |
*/ |
| 79 |
public static function builder_page_id_by_type( $type ) { |
| 80 |
global $post; |
| 81 |
|
| 82 |
if ( ! array_key_exists( $type, self::builder_page_types() ) ) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
// Wpml Supported. |
| 86 |
$options = self::option_name( $type ); |
| 87 |
$post_id = TemplateSettings::instance()->get_option( $options ); |
| 88 |
if ( empty( $post ) ) { |
| 89 |
return 0; |
| 90 |
} |
| 91 |
if ( empty( $post_id ) ) { |
| 92 |
$options = self::option_name( $type, true ); |
| 93 |
$post_id = TemplateSettings::instance()->get_option( $options ); |
| 94 |
} |
| 95 |
|
| 96 |
$cache_key = 'rtsb_builder_page_id_by_type_' . $type; |
| 97 |
if ( isset( self::$cache[ $cache_key ] ) ) { |
| 98 |
return self::$cache[ $cache_key ]; |
| 99 |
} |
| 100 |
|
| 101 |
switch ( $type ) { |
| 102 |
case 'product': |
| 103 |
if ( 'product' !== $post->post_type ) { |
| 104 |
break; |
| 105 |
} |
| 106 |
$cache_key_product = 'template_for_product_page_' . get_the_ID(); |
| 107 |
$template = get_transient( $cache_key_product ); |
| 108 |
if ( $template && 'publish' === get_post_status( $template ) ) { |
| 109 |
$post_id = $template; |
| 110 |
break; |
| 111 |
} |
| 112 |
Cache::set_transient_cache_key( $cache_key_product ); |
| 113 |
$builder_id = get_post_meta( get_the_ID(), self::$view_template_for_products_meta, true ); |
| 114 |
|
| 115 |
if ( self::get_specific_product_as_default( $builder_id ) && 'publish' === get_post_status( $builder_id ) ) { |
| 116 |
$post_id = $builder_id; |
| 117 |
set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours. |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
$terms = get_the_terms( get_the_ID(), 'product_cat' ); |
| 122 |
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) { |
| 123 |
foreach ( $terms as $term ) { |
| 124 |
$template_id = get_term_meta( $term->term_id, self::$categories_product_page_template, true ); |
| 125 |
if ( empty( $template_id ) || 'publish' !== get_post_status( $template_id ) ) { |
| 126 |
continue; |
| 127 |
} |
| 128 |
$set_default_option_name = self::option_name_product_page_specific_cat_set_default( $template_id ); |
| 129 |
if ( TemplateSettings::instance()->get_option( $set_default_option_name ) ) { |
| 130 |
$post_id = $template_id; |
| 131 |
set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours. |
| 132 |
break 2; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
$tags = get_the_terms( get_the_ID(), 'product_tag' ); |
| 137 |
if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) { |
| 138 |
foreach ( $tags as $term ) { |
| 139 |
$template_id = get_term_meta( $term->term_id, self::$tags_product_page_template, true ); |
| 140 |
if ( empty( $template_id ) || 'publish' !== get_post_status( $template_id ) ) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
$set_default_option_name = self::option_name_product_page_specific_tag_set_default( $template_id ); |
| 144 |
if ( TemplateSettings::instance()->get_option( $set_default_option_name ) ) { |
| 145 |
$post_id = $template_id; |
| 146 |
set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours. |
| 147 |
break 2; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
set_transient( $cache_key_product, $post_id, 12 * HOUR_IN_SECONDS ); |
| 152 |
break; |
| 153 |
case 'archive': |
| 154 |
$queried_object = get_queried_object(); |
| 155 |
if ( ! empty( $queried_object->taxonomy ) && 'product_cat' === $queried_object->taxonomy ) { |
| 156 |
$cache_key_archive = 'template_for_archive_page_' . $queried_object->term_id; |
| 157 |
$template = get_transient( $cache_key_archive ); |
| 158 |
if ( $template && 'publish' === get_post_status( $template ) ) { |
| 159 |
$post_id = $template; |
| 160 |
break; |
| 161 |
} |
| 162 |
Cache::set_transient_cache_key( $cache_key_archive ); |
| 163 |
$builder_id = get_term_meta( $queried_object->term_id, self::$view_template_for_archive_meta, true ); |
| 164 |
if ( self::get_specific_category_as_default( $builder_id ) && 'publish' === get_post_status( $builder_id ) ) { |
| 165 |
$post_id = $builder_id; |
| 166 |
set_transient( $cache_key_archive, $post_id, 12 * HOUR_IN_SECONDS ); // Cache duration: 12 hours. |
| 167 |
} |
| 168 |
} |
| 169 |
break; |
| 170 |
|
| 171 |
default: |
| 172 |
} |
| 173 |
if ( 'publish' === get_post_status( $post_id ) && self::builder_type( $post_id ) === $type ) { |
| 174 |
self::$cache[ $cache_key ] = $post_id; |
| 175 |
return $post_id; |
| 176 |
} |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Page builder id. |
| 183 |
* |
| 184 |
* @return init |
| 185 |
*/ |
| 186 |
public static function builder_page_id_by_page() { |
| 187 |
$type = apply_filters( 'rtsb/builder/set/current/page/type', '' ); |
| 188 |
if ( ! $type ) { |
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
return self::builder_page_id_by_type( $type ); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Page builder Page for. |
| 197 |
* |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
public static function builder_page_types() { |
| 201 |
$page_types = [ |
| 202 |
'shop' => esc_html__( 'Shop', 'shopbuilder' ), |
| 203 |
'archive' => esc_html__( 'Category Archive', 'shopbuilder' ), |
| 204 |
'product' => esc_html__( 'Product Page', 'shopbuilder' ), |
| 205 |
'cart' => esc_html__( 'Cart', 'shopbuilder' ), |
| 206 |
'checkout' => esc_html__( 'Checkout', 'shopbuilder' ), |
| 207 |
]; |
| 208 |
|
| 209 |
return apply_filters( 'rtsb/builder/register/page/types', $page_types ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Option name. |
| 214 |
* |
| 215 |
* @param string $type Builder type. |
| 216 |
* |
| 217 |
* @return string |
| 218 |
*/ |
| 219 |
public static function option_name( $type, $default_lang = false ) { |
| 220 |
|
| 221 |
$type = str_replace( [ '-', ' ' ], '_', $type ); |
| 222 |
$suffix = $type; |
| 223 |
|
| 224 |
if ( $default_lang ) { |
| 225 |
return self::$template_meta . '_default_' . $suffix; |
| 226 |
} |
| 227 |
$current_lang = Fns::wpml_current_language(); |
| 228 |
|
| 229 |
if ( ! empty( $current_lang ) ) { |
| 230 |
$suffix = $type . '_' . $current_lang; |
| 231 |
} |
| 232 |
|
| 233 |
return self::$template_meta . '_default_' . $suffix; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @param $post_id |
| 238 |
* |
| 239 |
* @return string|void |
| 240 |
*/ |
| 241 |
public static function option_name_for_specific_product_set_default( $post_id ) { |
| 242 |
if ( ! $post_id ) { |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
return 'rtsb_template_specific_product_set_default_' . $post_id; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* @param $post_id |
| 251 |
* |
| 252 |
* @return false|mixed|void|null |
| 253 |
*/ |
| 254 |
public static function get_specific_product_as_default( $post_id ) { |
| 255 |
if ( ! $post_id ) { |
| 256 |
return; |
| 257 |
} |
| 258 |
$options = self::option_name_for_specific_product_set_default( $post_id ); |
| 259 |
|
| 260 |
return TemplateSettings::instance()->get_option( $options ); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* @param $post_id |
| 265 |
* |
| 266 |
* @return string|void |
| 267 |
*/ |
| 268 |
public static function option_name_for_specific_category_set_default( $post_id ) { |
| 269 |
if ( ! $post_id ) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
return 'rtsb_template_specific_category_set_default_' . $post_id; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* @param $post_id |
| 278 |
* |
| 279 |
* @return false|mixed|void|null |
| 280 |
*/ |
| 281 |
public static function get_specific_category_as_default( $post_id ) { |
| 282 |
if ( ! $post_id ) { |
| 283 |
return; |
| 284 |
} |
| 285 |
$options = self::option_name_for_specific_category_set_default( $post_id ); |
| 286 |
|
| 287 |
return TemplateSettings::instance()->get_option( $options ); // get_option( $options ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* @param $template_id |
| 292 |
* |
| 293 |
* @return string |
| 294 |
*/ |
| 295 |
public static function option_name_by_template_id( $template_id ) { |
| 296 |
$_suff = null; |
| 297 |
if ( $template_id ) { |
| 298 |
$_suff = '_' . $template_id; |
| 299 |
} |
| 300 |
|
| 301 |
return self::$view_template_for_products_meta . $_suff; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* @param $template_id |
| 306 |
* |
| 307 |
* @return string |
| 308 |
*/ |
| 309 |
public static function option_name_product_page_selected_cat( $template_id ) { |
| 310 |
$_suff = null; |
| 311 |
if ( $template_id ) { |
| 312 |
$_suff = '_' . $template_id; |
| 313 |
} |
| 314 |
|
| 315 |
return self::$categories_product_page_template . $_suff; |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* @param $post_id |
| 320 |
* |
| 321 |
* @return string|void |
| 322 |
*/ |
| 323 |
public static function option_name_product_page_specific_cat_set_default( $post_id ) { |
| 324 |
if ( ! $post_id ) { |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
return 'rtsb_template_product_page_specific_category_set_default_' . $post_id; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* @param $template_id |
| 333 |
* |
| 334 |
* @return string |
| 335 |
*/ |
| 336 |
public static function option_name_product_page_selected_tag( $template_id ) { |
| 337 |
$_suff = null; |
| 338 |
if ( $template_id ) { |
| 339 |
$_suff = '_' . $template_id; |
| 340 |
} |
| 341 |
|
| 342 |
return self::$tags_product_page_template . $_suff; |
| 343 |
} |
| 344 |
/** |
| 345 |
* @param $post_id |
| 346 |
* |
| 347 |
* @return string|void |
| 348 |
*/ |
| 349 |
public static function option_name_product_page_specific_tag_set_default( $post_id ) { |
| 350 |
if ( ! $post_id ) { |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
return 'rtsb_template_product_page_specific_tag_set_default_' . $post_id; |
| 355 |
} |
| 356 |
/** |
| 357 |
* @param $template_id |
| 358 |
* |
| 359 |
* @return string |
| 360 |
*/ |
| 361 |
public static function archive_option_name_by_template_id( $template_id ) { |
| 362 |
$_suff = null; |
| 363 |
if ( $template_id ) { |
| 364 |
$_suff = '_' . $template_id; |
| 365 |
} |
| 366 |
|
| 367 |
return self::$view_template_for_archive_meta . $_suff; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Template builder |
| 372 |
* |
| 373 |
* @var string |
| 374 |
*/ |
| 375 |
public static function template_type_meta_key() { |
| 376 |
return self::$template_meta . '_type'; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Get builder type. |
| 381 |
* |
| 382 |
* @param [type] $post_id Post id. |
| 383 |
* |
| 384 |
* @return string |
| 385 |
*/ |
| 386 |
public static function builder_type( $post_id ) { |
| 387 |
$post_id = Fns::wpml_object_id( $post_id, self::$post_type_tb, 'default' ); |
| 388 |
|
| 389 |
if ( ! $post_id ) { |
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
$cache_key = 'rtsb_template_type_' . $post_id; |
| 394 |
if ( ! empty( self::$cache[ $cache_key ] ) ) { |
| 395 |
return self::$cache[ $cache_key ]; |
| 396 |
} |
| 397 |
|
| 398 |
$type = get_post_meta( $post_id, self::template_type_meta_key(), true ); |
| 399 |
|
| 400 |
// Cache the results in a static variable for the next call. |
| 401 |
self::$cache[ $cache_key ] = $type; |
| 402 |
|
| 403 |
return $type; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Template builder |
| 408 |
* |
| 409 |
* @var boolean |
| 410 |
*/ |
| 411 |
public static function is_builder_preview() { |
| 412 |
return is_singular( self::$post_type_tb ); |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Template builder |
| 417 |
* |
| 418 |
* @var boolean |
| 419 |
*/ |
| 420 |
public static function is_archive() { |
| 421 |
return self::is_page_builder( 'archive', is_product_taxonomy() ); |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Template builder |
| 426 |
* |
| 427 |
* @var boolean |
| 428 |
*/ |
| 429 |
public static function is_shop() { |
| 430 |
return self::is_page_builder( 'shop', is_shop() ); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Template builder |
| 435 |
* |
| 436 |
* @var boolean |
| 437 |
*/ |
| 438 |
public static function is_cart() { |
| 439 |
return self::is_page_builder( 'cart', is_cart() ); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Template builder |
| 444 |
* |
| 445 |
* @var boolean |
| 446 |
*/ |
| 447 |
public static function is_checkout() { |
| 448 |
return self::is_page_builder( 'checkout', is_checkout() && ! is_wc_endpoint_url() && ! is_order_received_page() ); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Single Page builder Detection |
| 453 |
* |
| 454 |
* @return boolean |
| 455 |
*/ |
| 456 |
public static function is_product() { |
| 457 |
return self::is_page_builder( 'product', is_product() ); |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* @param $is_quick_view |
| 462 |
* |
| 463 |
* @return bool |
| 464 |
*/ |
| 465 |
public static function is_quick_views_page( $is_quick_view = false ) { |
| 466 |
return rtsb()->has_pro() && self::is_page_builder( 'quick-view', $is_quick_view ); |
| 467 |
} |
| 468 |
|
| 469 |
/** |
| 470 |
* Builder page detector. |
| 471 |
* |
| 472 |
* @param string $type builder Page type. |
| 473 |
* @param boolean $is_page page status. |
| 474 |
* |
| 475 |
* @return boolean |
| 476 |
*/ |
| 477 |
public static function is_page_builder( $type, $is_page, $is_require_auth = false ) { |
| 478 |
if ( self::builder_type( get_the_ID() ) === $type ) { |
| 479 |
return true; |
| 480 |
} |
| 481 |
|
| 482 |
if ( $is_require_auth && ! is_user_logged_in() ) { |
| 483 |
$type = 'myaccount-auth'; |
| 484 |
} |
| 485 |
|
| 486 |
$builder_id = self::builder_page_id_by_type( $type ); |
| 487 |
if ( ! $builder_id ) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
|
| 491 |
$builder_type = self::builder_type( $builder_id ); |
| 492 |
if ( $type === $builder_type && $is_page ) { |
| 493 |
return true; |
| 494 |
} |
| 495 |
|
| 496 |
return false; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Get builder content function |
| 501 |
* |
| 502 |
* @param [type] $template_id builder Template id. |
| 503 |
* @param boolean $with_css with css. |
| 504 |
* |
| 505 |
* @return mixed |
| 506 |
*/ |
| 507 |
public static function get_builder_content( $template_id, $with_css = false ) { |
| 508 |
// Don't Use cache: tickets ID -> 75376. |
| 509 |
return \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, $with_css ); |
| 510 |
} |
| 511 |
} |
| 512 |
|