| @@ -1,106 +1,144 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace UltimateStoreKit\Includes\Builder; | |
| 4 | - | |
| 5 | -class Builder_Template_Helper { | |
| 6 | - | |
| 7 | - public static function isTemplateEditMode() { | |
| 8 | - if (get_post_type() == Meta::POST_TYPE) { | |
| 9 | - return true; | |
| 10 | - } | |
| 11 | - | |
| 12 | - if (isset($_REQUEST[Meta::POST_TYPE])) { | |
| 13 | - return true; | |
| 14 | - } | |
| 15 | - } | |
| 16 | - | |
| 17 | - public static function separator() { | |
| 18 | - return '|'; | |
| 19 | - } | |
| 20 | - | |
| 21 | - public static function templates($single = false) { | |
| 22 | - $shopItem = [ | |
| 23 | - 'shop' => 'Shop Page', | |
| 24 | - 'archive' => 'Archive Page', | |
| 25 | - 'single' => 'Single Page', | |
| 26 | - 'cart' => 'Cart Page', | |
| 27 | - 'checkout' => 'Checkout', | |
| 28 | - 'myaccount' => 'My Account', | |
| 29 | - // 'wishlist' => 'Wishlist', | |
| 30 | - ]; | |
| 31 | - | |
| 32 | - if ($wcItems = WC()->query->get_query_vars()) { | |
| 33 | - array_walk($wcItems, function ($item, $key) use (&$shopItem) { | |
| 34 | - $shopItem[$key] = ucwords(str_replace('-', ' ', $key)); | |
| 35 | - }); | |
| 36 | - } | |
| 37 | - | |
| 38 | - $product = [ | |
| 39 | - 'product' => $shopItem, | |
| 40 | - ]; | |
| 41 | - | |
| 42 | - $templates = apply_filters( | |
| 43 | - 'ultimate_store_kit_builder_templates', | |
| 44 | - $product | |
| 45 | - ); | |
| 46 | - | |
| 47 | - if ($single) { | |
| 48 | - $separator = static::separator(); | |
| 49 | - $return = []; | |
| 50 | - if (is_array($templates) && !empty($templates)) { | |
| 51 | - foreach ($templates as $keys => $items) { | |
| 52 | - if (is_array($items)) { | |
| 53 | - foreach ($items as $itemKey => $item) { | |
| 54 | - $return["{$keys}{$separator}{$itemKey}"] = $item; | |
| 55 | - } | |
| 56 | - } | |
| 57 | - } | |
| 58 | - } | |
| 59 | - | |
| 60 | - return apply_filters( | |
| 61 | - 'ultimate_store_kit_builder_all_templates', | |
| 62 | - $return | |
| 63 | - ); | |
| 64 | - } | |
| 65 | - | |
| 66 | - return $templates; | |
| 67 | - } | |
| 68 | - | |
| 69 | - public static function templateForSelectDropdown() { | |
| 70 | - return static::templates(); | |
| 71 | - } | |
| 72 | - | |
| 73 | - public static function getTemplateByIndex($index) { | |
| 74 | - $index = trim($index); | |
| 75 | - $templates = static::templates(true); | |
| 76 | - | |
| 77 | - return array_key_exists($index, $templates) ? $templates[$index] : false; | |
| 78 | - } | |
| 79 | - | |
| 80 | - public static function getTemplatePostTypeByIndex($index) { | |
| 81 | - $index = trim($index); | |
| 82 | - if ($item = explode(static::separator(), $index)) { | |
| 83 | - return get_post_type_object($item[0]); | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - public static function is_elementor_active() { | |
| 88 | - return did_action('elementor/loaded'); | |
| 89 | - } | |
| 90 | - | |
| 91 | - public static function getTemplate($slug, $postType = false) { | |
| 92 | - if (!$postType) { | |
| 93 | - $postType = get_post_type(); | |
| 94 | - } | |
| 95 | - $separator = static::separator(); | |
| 96 | - $template = strtolower("{$postType}{$separator}{$slug}"); | |
| 97 | - $enabledTemplate = strtolower(Meta::TEMPLATE_ID . $template); | |
| 98 | - | |
| 99 | - return get_option($enabledTemplate); | |
| 100 | - } | |
| 101 | - | |
| 102 | - public static function getTemplateId($templateType) { | |
| 103 | - $metaIndex = strtolower(Meta::TEMPLATE_ID . $templateType); | |
| 104 | - return intval(get_option($metaIndex)); | |
| 105 | - } | |
| 106 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace UltimateStoreKit\Includes\Builder; | |
| 4 | + | |
| 5 | +if (! defined('ABSPATH')) { | |
| 6 | + exit; // Exit if accessed directly | |
| 7 | +} | |
| 8 | + | |
| 9 | +class Builder_Template_Helper { | |
| 10 | + | |
| 11 | + public static function isTemplateEditMode() { | |
| 12 | + if ( get_post_type() == Meta::POST_TYPE ) { | |
| 13 | + return true; | |
| 14 | + } | |
| 15 | + | |
| 16 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check on whether a builder template is being edited. | |
| 17 | + if ( isset( $_REQUEST[ Meta::POST_TYPE ] ) ) { | |
| 18 | + return true; | |
| 19 | + } | |
| 20 | + } | |
| 21 | + | |
| 22 | + public static function separator() { | |
| 23 | + return '|'; | |
| 24 | + } | |
| 25 | + | |
| 26 | + public static function templates( $single = false ) { | |
| 27 | + $shop_item = [ | |
| 28 | + 'shop' => 'Shop Page', | |
| 29 | + 'archive' => 'Archive Page', | |
| 30 | + 'single' => 'Single Page', | |
| 31 | + 'category' => 'Category Page', | |
| 32 | + 'tag' => 'Tag Page', | |
| 33 | + 'cart' => 'Cart Page', | |
| 34 | + 'checkout' => 'Checkout', | |
| 35 | + 'order-received' => 'Order Received (Thank You Page)', | |
| 36 | + ]; | |
| 37 | + | |
| 38 | + $my_account = [ | |
| 39 | + 'login' => 'Login & Register', | |
| 40 | + 'myaccount' => 'Dashboard', | |
| 41 | + 'orders' => 'Orders', | |
| 42 | + 'downloads' => 'Downloads', | |
| 43 | + 'edit-address' => 'Address', | |
| 44 | + 'edit-account' => 'Account Details', | |
| 45 | + 'wishlist' => 'Wishlist', | |
| 46 | + 'logout' => 'Customer Logout', | |
| 47 | + ]; | |
| 48 | + | |
| 49 | + // Endpoints that belong to the order/checkout flow rather than the | |
| 50 | + // authenticated account area. Everything else from WC()->query is treated | |
| 51 | + // as an account screen so the dropdown grouping matches the routing in | |
| 52 | + // Builder_Integration::setFrontendTemplate(), which looks endpoint | |
| 53 | + // templates up under the `account` post-type group. | |
| 54 | + $order_flow_endpoints = apply_filters( | |
| 55 | + 'ultimate_store_kit_builder_order_flow_endpoints', | |
| 56 | + [ 'order-pay', 'order-received' ] | |
| 57 | + ); | |
| 58 | + | |
| 59 | + if ( $wcItems = WC()->query->get_query_vars() ) { | |
| 60 | + foreach ( $wcItems as $key => $item ) { | |
| 61 | + $label = ucwords( str_replace( '-', ' ', $key ) ); | |
| 62 | + if ( in_array( $key, $order_flow_endpoints, true ) ) { | |
| 63 | + // Keep hand-written labels (e.g. order-received) intact. | |
| 64 | + if ( ! isset( $shop_item[ $key ] ) ) { | |
| 65 | + $shop_item[ $key ] = $label; | |
| 66 | + } | |
| 67 | + } else { | |
| 68 | + $my_account[ $key ] = $label; | |
| 69 | + } | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + $product = [ | |
| 74 | + 'product' => $shop_item, | |
| 75 | + 'account' => $my_account, | |
| 76 | + ]; | |
| 77 | + | |
| 78 | + $templates = apply_filters( | |
| 79 | + 'ultimate_store_kit_builder_templates', | |
| 80 | + $product | |
| 81 | + ); | |
| 82 | + | |
| 83 | + if ( $single ) { | |
| 84 | + $separator = static::separator(); | |
| 85 | + $return = []; | |
| 86 | + if ( is_array( $templates ) && ! empty( $templates ) ) { | |
| 87 | + foreach ( $templates as $keys => $items ) { | |
| 88 | + if ( is_array( $items ) ) { | |
| 89 | + foreach ( $items as $itemKey => $item ) { | |
| 90 | + $return[ "{$keys}{$separator}{$itemKey}" ] = $item; | |
| 91 | + } | |
| 92 | + } | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + return apply_filters( | |
| 97 | + 'ultimate_store_kit_builder_all_templates', | |
| 98 | + $return | |
| 99 | + ); | |
| 100 | + } | |
| 101 | + | |
| 102 | + return $templates; | |
| 103 | + } | |
| 104 | + | |
| 105 | + public static function templateForSelectDropdown() { | |
| 106 | + return static::templates(); | |
| 107 | + } | |
| 108 | + | |
| 109 | + public static function getTemplateByIndex( $index ) { | |
| 110 | + $index = trim( $index ); | |
| 111 | + $templates = static::templates( true ); | |
| 112 | + | |
| 113 | + return array_key_exists( $index, $templates ) ? $templates[ $index ] : false; | |
| 114 | + } | |
| 115 | + | |
| 116 | + public static function getTemplatePostTypeByIndex( $index ) { | |
| 117 | + $index = trim( $index ); | |
| 118 | + if ( $item = explode( static::separator(), $index ) ) { | |
| 119 | + return get_post_type_object( $item[0] ); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + public static function is_elementor_active() { | |
| 124 | + return did_action( 'elementor/loaded' ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + public static function getTemplate( $slug, $postType = false ) { | |
| 128 | + if ( ! $postType ) { | |
| 129 | + $postType = get_post_type(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + $separator = static::separator(); | |
| 133 | + $template = strtolower( "{$postType}{$separator}{$slug}" ); | |
| 134 | + /** | |
| 135 | + * important area for debugging | |
| 136 | + */ | |
| 137 | + | |
| 138 | + return Meta::get_template_option( $template ); | |
| 139 | + } | |
| 140 | + | |
| 141 | + public static function getTemplateId( $templateType ) { | |
| 142 | + return intval( Meta::get_template_option( $templateType ) ); | |
| 143 | + } | |
| 144 | +} | |