| 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 |
* Page builder id. |
| 64 |
* |
| 65 |
* @return int |
| 66 |
*/ |
| 67 |
public static function builder_page_id_by_type( $type ) { |
| 68 |
global $post; |
| 69 |
|
| 70 |
if ( ! array_key_exists( $type, self::builder_page_types() ) ) { |
| 71 |
return 0; |
| 72 |
} |
| 73 |
|
| 74 |
$options = self::option_name( $type ); |
| 75 |
$post_id = TemplateSettings::instance()->get_option( $options ); |
| 76 |
|
| 77 |
if ( empty( $post ) ) { |
| 78 |
return $post_id; |
| 79 |
} |
| 80 |
|
| 81 |
$cache_key = 'rtsb_builder_page_id_by_type_' . $type; |
| 82 |
if ( isset( self::$cache[ $cache_key ] ) ) { |
| 83 |
return self::$cache[ $cache_key ]; |
| 84 |
} |
| 85 |
|
| 86 |
switch ( $type ) { |
| 87 |
case 'product': |
| 88 |
if ( 'product' === $post->post_type ) { |
| 89 |
$builder_id = get_post_meta( get_the_ID(), self::$view_template_for_products_meta, true ); |
| 90 |
$set_default = self::get_specific_product_as_default( $builder_id ); |
| 91 |
|
| 92 |
if ( 'publish' === $post->post_status ) { |
| 93 |
$post_id = $builder_id && $set_default ? $builder_id : $post_id; |
| 94 |
} |
| 95 |
} |
| 96 |
break; |
| 97 |
case 'archive': |
| 98 |
$queried_object = get_queried_object(); |
| 99 |
|
| 100 |
if ( ! empty( $queried_object->taxonomy ) && 'product_cat' === $queried_object->taxonomy ) { |
| 101 |
$builder_id = get_term_meta( $queried_object->term_id, self::$view_template_for_archive_meta, true ); |
| 102 |
$set_default = self::get_specific_category_as_default( $builder_id ); |
| 103 |
|
| 104 |
if ( 'publish' === $post->post_status ) { |
| 105 |
$post_id = $builder_id && $set_default ? $builder_id : $post_id; |
| 106 |
} |
| 107 |
} |
| 108 |
break; |
| 109 |
|
| 110 |
default: |
| 111 |
|
| 112 |
} |
| 113 |
if ( 'publish' === $post->post_status && self::builder_type( $post_id ) === $type ) { |
| 114 |
self::$cache[ $cache_key ] = $post_id; |
| 115 |
return $post_id; |
| 116 |
} |
| 117 |
|
| 118 |
return $post_id; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Page builder id. |
| 123 |
* |
| 124 |
* @return init |
| 125 |
*/ |
| 126 |
public static function builder_page_id_by_page() { |
| 127 |
$type = apply_filters( 'rtsb/builder/set/current/page/type', '' ); |
| 128 |
if ( ! $type ) { |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
return self::builder_page_id_by_type( $type ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Page builder Page for. |
| 137 |
* |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
public static function builder_page_types() { |
| 141 |
$page_types = [ |
| 142 |
'shop' => esc_html__( 'Shop', 'shopbuilder' ), |
| 143 |
'archive' => esc_html__( 'Category Archive', 'shopbuilder' ), |
| 144 |
'product' => esc_html__( 'Product Page', 'shopbuilder' ), |
| 145 |
'cart' => esc_html__( 'Cart', 'shopbuilder' ), |
| 146 |
'checkout' => esc_html__( 'Checkout', 'shopbuilder' ), |
| 147 |
]; |
| 148 |
|
| 149 |
return apply_filters( 'rtsb/builder/register/page/types', $page_types ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Option name. |
| 154 |
* |
| 155 |
* @param string $type Builder type. |
| 156 |
* |
| 157 |
* @return string |
| 158 |
*/ |
| 159 |
public static function option_name( $type ) { |
| 160 |
$type = str_replace( [ '-', ' ' ], '_', $type ); |
| 161 |
|
| 162 |
return self::$template_meta . '_default_' . $type; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* @param $post_id |
| 167 |
* |
| 168 |
* @return string|void |
| 169 |
*/ |
| 170 |
public static function option_name_for_specific_product_set_default( $post_id ) { |
| 171 |
if ( ! $post_id ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
return 'rtsb_template_specific_product_set_default_' . $post_id; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @param $post_id |
| 180 |
* |
| 181 |
* @return false|mixed|void|null |
| 182 |
*/ |
| 183 |
public static function get_specific_product_as_default( $post_id ) { |
| 184 |
if ( ! $post_id ) { |
| 185 |
return; |
| 186 |
} |
| 187 |
$options = self::option_name_for_specific_product_set_default( $post_id ); |
| 188 |
|
| 189 |
return TemplateSettings::instance()->get_option( $options ); // get_option( $options ); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @param $post_id |
| 194 |
* |
| 195 |
* @return string|void |
| 196 |
*/ |
| 197 |
public static function option_name_for_specific_category_set_default( $post_id ) { |
| 198 |
if ( ! $post_id ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
return 'rtsb_template_specific_category_set_default_' . $post_id; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* @param $post_id |
| 207 |
* |
| 208 |
* @return false|mixed|void|null |
| 209 |
*/ |
| 210 |
public static function get_specific_category_as_default( $post_id ) { |
| 211 |
if ( ! $post_id ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
$options = self::option_name_for_specific_category_set_default( $post_id ); |
| 215 |
|
| 216 |
return TemplateSettings::instance()->get_option( $options ); // get_option( $options ); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* @param $template_id |
| 221 |
* |
| 222 |
* @return string |
| 223 |
*/ |
| 224 |
public static function option_name_by_template_id( $template_id ) { |
| 225 |
$_suff = null; |
| 226 |
if ( $template_id ) { |
| 227 |
$_suff = '_' . $template_id; |
| 228 |
} |
| 229 |
|
| 230 |
return self::$view_template_for_products_meta . $_suff; |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* @param $template_id |
| 235 |
* |
| 236 |
* @return string |
| 237 |
*/ |
| 238 |
public static function archive_option_name_by_template_id( $template_id ) { |
| 239 |
$_suff = null; |
| 240 |
if ( $template_id ) { |
| 241 |
$_suff = '_' . $template_id; |
| 242 |
} |
| 243 |
|
| 244 |
return self::$view_template_for_archive_meta . $_suff; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Template builder |
| 249 |
* |
| 250 |
* @var string |
| 251 |
*/ |
| 252 |
public static function template_type_meta_key() { |
| 253 |
return self::$template_meta . '_type'; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Get builder type. |
| 258 |
* |
| 259 |
* @param [type] $post_id Post id. |
| 260 |
* |
| 261 |
* @return string |
| 262 |
*/ |
| 263 |
public static function builder_type( $post_id ) { |
| 264 |
if( ! $post_id ){ |
| 265 |
return ; |
| 266 |
} |
| 267 |
|
| 268 |
$cache_key = 'rtsb_template_type_' . $post_id; |
| 269 |
if ( isset( self::$cache[ $cache_key ] ) ) { |
| 270 |
return self::$cache[ $cache_key ]; |
| 271 |
} |
| 272 |
|
| 273 |
$type = get_post_meta( $post_id, self::template_type_meta_key(), true ); |
| 274 |
|
| 275 |
// Cache the results in a static variable for the next call. |
| 276 |
self::$cache[ $cache_key ] = $type; |
| 277 |
|
| 278 |
return $type; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Template builder |
| 283 |
* |
| 284 |
* @var boolean |
| 285 |
*/ |
| 286 |
public static function is_builder_preview() { |
| 287 |
return is_singular( self::$post_type_tb ); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Template builder |
| 292 |
* |
| 293 |
* @var boolean |
| 294 |
*/ |
| 295 |
public static function is_archive() { |
| 296 |
return self::is_page_builder( 'archive', is_product_taxonomy() ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Template builder |
| 301 |
* |
| 302 |
* @var boolean |
| 303 |
*/ |
| 304 |
public static function is_shop() { |
| 305 |
return self::is_page_builder( 'shop', is_shop() ); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Template builder |
| 310 |
* |
| 311 |
* @var boolean |
| 312 |
*/ |
| 313 |
public static function is_cart() { |
| 314 |
return self::is_page_builder( 'cart', is_cart() ); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Template builder |
| 319 |
* |
| 320 |
* @var boolean |
| 321 |
*/ |
| 322 |
public static function is_checkout() { |
| 323 |
return self::is_page_builder( 'checkout', is_checkout() && ! is_wc_endpoint_url() && ! is_order_received_page() ); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Single Page builder Detection |
| 328 |
* |
| 329 |
* @return boolean |
| 330 |
*/ |
| 331 |
public static function is_product() { |
| 332 |
return self::is_page_builder( 'product', is_product() ); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Builder page detector. |
| 337 |
* |
| 338 |
* @param string $type builder Page type. |
| 339 |
* @param boolean $is_page page status. |
| 340 |
* |
| 341 |
* @return boolean |
| 342 |
*/ |
| 343 |
public static function is_page_builder( $type, $is_page, $is_require_auth = false ) { |
| 344 |
if ( self::builder_type( get_the_ID() ) === $type ) { |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
if( $is_require_auth && ! is_user_logged_in() ){ |
| 349 |
$type = 'myaccount-auth'; |
| 350 |
} |
| 351 |
|
| 352 |
$builder_id = self::builder_page_id_by_type( $type ); |
| 353 |
if( ! $builder_id ){ |
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
$builder_type = self::builder_type( $builder_id ); |
| 358 |
if ( $type === $builder_type && $is_page ) { |
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
return false; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Get builder content function |
| 367 |
* |
| 368 |
* @param [type] $template_id builder Template id. |
| 369 |
* @param boolean $with_css with css. |
| 370 |
* |
| 371 |
* @return mixed |
| 372 |
*/ |
| 373 |
public static function get_builder_content( $template_id, $with_css = false ) { |
| 374 |
// Don't Use cache: tickets ID -> 75376. |
| 375 |
return \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $template_id, $with_css ); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
} |
| 380 |
|