| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'WPINC' ) ) { |
| 4 |
die; |
| 5 |
} |
| 6 |
|
| 7 |
use Elementor\Controls_Manager; |
| 8 |
use Elementor\Plugin; |
| 9 |
use UltimateStoreKit\Includes\Builder\Builder_Template_Helper; |
| 10 |
use UltimateStoreKit\Base\Singleton; |
| 11 |
use UltimateStoreKit\Includes\Builder\Meta; |
| 12 |
use UltimateStoreKit\Includes\Controls\SelectInput\Dynamic_Select; |
| 13 |
|
| 14 |
|
| 15 |
class Builder_Integration { |
| 16 |
|
| 17 |
use Singleton; |
| 18 |
|
| 19 |
private $current_template = null; |
| 20 |
public $current_template_id = null; |
| 21 |
|
| 22 |
function __construct() { |
| 23 |
add_filter( 'template_include', [ $this, 'set_builder_template' ], 9999 ); |
| 24 |
add_action( 'elementor/editor/init', [ $this, 'set_sample_post' ], 999 ); |
| 25 |
|
| 26 |
add_action( 'print_default_editor_scripts', array( $this, 'my_custom_fonts' ) ); |
| 27 |
|
| 28 |
add_action( 'elementor/documents/register_controls', [ $this, 'register_document_controls' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
public function my_custom_fonts() { |
| 32 |
if ( is_admin() && Plugin::instance()->editor->is_edit_mode() ) { |
| 33 |
if ( isset( $_REQUEST['usk-template'] ) ) { |
| 34 |
wp_register_style( 'usk-template-builder-hide-preview-btn-inline', false ); // phpcs:ignore |
| 35 |
wp_enqueue_style( 'usk-template-builder-hide-preview-btn-inline' ); |
| 36 |
wp_add_inline_style( |
| 37 |
'usk-template-builder-hide-preview-btn-inline', |
| 38 |
'#elementor-panel-footer-saver-preview {display:none!important}' |
| 39 |
); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
function set_sample_post() { |
| 44 |
if ( Builder_Template_Helper::isTemplateEditMode() ) { |
| 45 |
$object = \UltimateStoreKit\Includes\Builder\Builder_Post_Singleton::instance(); |
| 46 |
$object::set_sample_post(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
function register_document_controls( $document ) { |
| 51 |
if ( ! $document instanceof \Elementor\Core\DocumentTypes\PageBase |
| 52 |
|| ! $document::get_property( 'has_elements' ) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
if ( Plugin::instance()->preview->is_preview_mode() ) |
| 57 |
return; |
| 58 |
|
| 59 |
if ( ! Builder_Template_Helper::isTemplateEditMode() ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
global $post; |
| 64 |
|
| 65 |
if ( ! isset( $post->ID ) ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
$meta = get_post_meta( $post->ID ); |
| 69 |
|
| 70 |
$templateMeta = optional( $meta )[ Meta::TEMPLATE_TYPE ]; |
| 71 |
if ( ! isset( $templateMeta[0] ) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
$postMeta = $templateMeta[0]; |
| 75 |
$postMeta = explode( '|', $postMeta ); |
| 76 |
$postType = $postMeta[0]; |
| 77 |
|
| 78 |
if ( $postMeta[1] != 'single' ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$document->start_controls_section( |
| 83 |
'usk_page_setting_preview', |
| 84 |
[ |
| 85 |
'label' => esc_html__( 'Builder Settings', 'ultimate-store-kit' ), |
| 86 |
'tab' => Controls_Manager::TAB_SETTINGS, |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
$document->add_control( |
| 91 |
'usk_builder_sample_post_id', |
| 92 |
[ |
| 93 |
'label' => __( 'Builder Post', 'ultimate-store-kit' ), |
| 94 |
'type' => Dynamic_Select::TYPE, |
| 95 |
'multiple' => false, |
| 96 |
'label_block' => true, |
| 97 |
'query_args' => [ |
| 98 |
'post_type' => $postType |
| 99 |
], |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
$document->add_control( |
| 104 |
'usk_builder_sample_apply_preview', |
| 105 |
[ |
| 106 |
'type' => Controls_Manager::BUTTON, |
| 107 |
'label' => esc_html__( 'Apply & Preview', 'ultimate-store-kit' ), |
| 108 |
'label_block' => true, |
| 109 |
'show_label' => false, |
| 110 |
'text' => esc_html__( 'Apply & Preview', 'ultimate-store-kit' ), |
| 111 |
'separator' => 'none', |
| 112 |
'event' => 'ultimateStoreKitBuilderSetting:applySinglePagePostOnPreview', |
| 113 |
] |
| 114 |
); |
| 115 |
|
| 116 |
$document->end_controls_section(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Rewrite default template |
| 121 |
* |
| 122 |
*/ |
| 123 |
function set_builder_template( $template ) { |
| 124 |
if ( Builder_Template_Helper::isTemplateEditMode() ) { |
| 125 |
return $this->setBackendTemplate( $template ); |
| 126 |
} else { |
| 127 |
return $this->setFrontendTemplate( $template ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
protected function setBackendTemplate( $template ) { |
| 133 |
return $template; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
protected function setFrontendTemplate( $template ) { |
| 138 |
|
| 139 |
if ( get_post_type() == 'product' ) { |
| 140 |
global $product; |
| 141 |
$product = wc_get_product(); |
| 142 |
} |
| 143 |
|
| 144 |
if ( defined( 'ELEMENTOR_PATH' ) ) { |
| 145 |
$elementorTem = ELEMENTOR_PATH . "modules/page-templates/templates/"; |
| 146 |
$elementorTem = explode( $elementorTem, $template ); |
| 147 |
if ( count( $elementorTem ) == 2 ) { |
| 148 |
return $template; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) || is_product_taxonomy() ) { |
| 153 |
if ( $custom_template = $this->get_template_id( 'shop' ) ) { |
| 154 |
$this->current_template_id = $custom_template; |
| 155 |
return $this->getTemplatePath( 'woocommerce/archive-product', $template ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if ( is_cart() ) { |
| 160 |
if ( $custom_template = $this->get_template_id( 'cart', 'product' ) ) { |
| 161 |
$this->current_template_id = $custom_template; |
| 162 |
return $this->getTemplatePath( 'woocommerce/cart', $template ); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if ( is_order_received_page() ) { |
| 167 |
if ( $custom_template = $this->get_template_id( 'order-received', 'product' ) ) { |
| 168 |
$this->current_template_id = $custom_template; |
| 169 |
return $this->getTemplatePath( 'woocommerce/order-received', $template ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if ( is_checkout() ) { |
| 174 |
if ( $custom_template = $this->get_template_id( 'checkout', 'product' ) ) { |
| 175 |
$this->current_template_id = $custom_template; |
| 176 |
return $this->getTemplatePath( 'woocommerce/checkout', $template ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if ( is_single() && get_post_type() == 'product' ) { |
| 181 |
if ( $custom_template = $this->get_template_id( 'single', 'product' ) ) { |
| 182 |
$this->current_template_id = $custom_template; |
| 183 |
return $this->getTemplatePath( 'woocommerce/single-product', $template ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if ( is_account_page() ) { |
| 188 |
global $wp; |
| 189 |
$query_vars = WC()->query->get_query_vars(); |
| 190 |
|
| 191 |
/** |
| 192 |
* Add wishlist endpoint |
| 193 |
*/ |
| 194 |
$query_vars['wishlist'] = 'wishlist'; |
| 195 |
|
| 196 |
if ( $endpoint = array_intersect_key( $wp->query_vars, $query_vars ) ) { |
| 197 |
$endpoint = array_key_first( $endpoint ); |
| 198 |
|
| 199 |
if ( $endpoint && $custom_template = $this->get_template_id( $endpoint, 'account' ) ) { |
| 200 |
$this->current_template_id = $custom_template; |
| 201 |
|
| 202 |
if ( $newTemplate = $this->getTemplatePath( "woocommerce/{$endpoint}" ) ) { |
| 203 |
return $newTemplate; |
| 204 |
} |
| 205 |
|
| 206 |
return $this->getTemplatePath( "woocommerce/my-account", '' ); |
| 207 |
} |
| 208 |
|
| 209 |
if ( $custom_template = $this->get_template_id( 'myaccount-orders', 'account' ) ) { |
| 210 |
$this->current_template_id = $custom_template; |
| 211 |
return $this->getTemplatePath( 'woocommerce/my-account', $template ); |
| 212 |
} |
| 213 |
|
| 214 |
} else { |
| 215 |
if ( $custom_template = $this->get_template_id( 'myaccount', 'account' ) ) { |
| 216 |
$this->current_template_id = $custom_template; |
| 217 |
return $this->getTemplatePath( 'woocommerce/my-account', $template ); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
if ( is_single() ) { |
| 224 |
if ( $custom_template = $this->get_template_id( 'single', 'post' ) ) { |
| 225 |
$this->current_template_id = $custom_template; |
| 226 |
return $this->getTemplatePath( 'single-post', $template ); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if ( is_archive() ) { |
| 231 |
if ( $custom_template = $this->get_template_id( 'archive', 'post' ) ) { |
| 232 |
$this->current_template_id = $custom_template; |
| 233 |
return $this->getTemplatePath( 'archive-post', $template ); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if ( is_home() ) { |
| 238 |
if ( $custom_template = $this->get_template_id( 'home', 'post' ) ) { |
| 239 |
$this->current_template_id = $custom_template; |
| 240 |
return $this->getTemplatePath( 'home', $template ); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
if ( $page_Id = intval( get_option( 'bdt_usk_compare_products_page_id' ) ) ) { |
| 245 |
if ( is_page( $page_Id ) ) { |
| 246 |
if ( $custom_template = $this->get_template_id( 'compare-products', 'product' ) ) { |
| 247 |
$this->current_template_id = $custom_template; |
| 248 |
return $this->getTemplatePath( 'home', $template ); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
return $template; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
public function getThemeTemplatePath( $slug ) { |
| 258 |
|
| 259 |
$fullPath = get_template_directory() . "/ultimate-store-kit/$slug"; |
| 260 |
if ( file_exists( $fullPath ) ) { |
| 261 |
return $fullPath; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
public function getPluginTemplatePath( $slug ) { |
| 266 |
|
| 267 |
$fullPath = BDTUSK_PATH . "includes/builder/templates/$slug"; |
| 268 |
if ( file_exists( $fullPath ) ) { |
| 269 |
return $fullPath; |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* Get Template Path ID |
| 276 |
* |
| 277 |
* @param $slug |
| 278 |
* @param $postType |
| 279 |
* |
| 280 |
* @return mixed|void|null |
| 281 |
*/ |
| 282 |
public function get_template_id( $slug, $postType = false ) { |
| 283 |
|
| 284 |
if ( null !== $this->current_template_id ) { |
| 285 |
return $this->current_template_id; |
| 286 |
} |
| 287 |
|
| 288 |
$templateId = Builder_Template_Helper::getTemplate( $slug, $postType ); |
| 289 |
$this->current_template_id = apply_filters( 'ultimate-store-kit-builder/custom-shop-template', $templateId ); |
| 290 |
|
| 291 |
return $this->current_template_id; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
/** |
| 296 |
* Get Template Path |
| 297 |
* |
| 298 |
* @param $slug |
| 299 |
* @param $default |
| 300 |
* |
| 301 |
* @return mixed|string|void |
| 302 |
*/ |
| 303 |
protected function getTemplatePath( $slug, $default = '' ) { |
| 304 |
$phpSlug = "{$slug}.php"; |
| 305 |
|
| 306 |
if ( $template = $this->getThemeTemplatePath( $phpSlug ) ) { |
| 307 |
return $template; |
| 308 |
} |
| 309 |
|
| 310 |
if ( $template = $this->getPluginTemplatePath( $phpSlug ) ) { |
| 311 |
return $template; |
| 312 |
} |
| 313 |
|
| 314 |
return $default; |
| 315 |
} |
| 316 |
|
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
Builder_Integration::instance(); |
| 321 |
|
| 322 |
|
| 323 |
|