page-templates.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Core\PageTemplates; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use ShopEngine\Traits\Singleton; |
| 8 | use ShopEngine\Widgets\Products; |
| 9 | |
| 10 | |
| 11 | class Page_Templates { |
| 12 | use Singleton; |
| 13 | |
| 14 | private $templateList = []; |
| 15 | private $listedCollected = false; |
| 16 | |
| 17 | public function init() { |
| 18 | |
| 19 | add_filter('elementor/document/urls/edit', function($url) { |
| 20 | if(is_single()) { |
| 21 | global $wp; |
| 22 | $query = $wp->query_vars; |
| 23 | if(isset($query['name'])){ |
| 24 | $product = get_page_by_path( $query['name'], OBJECT, 'product' ); |
| 25 | if(!empty($product->ID)) { |
| 26 | return $url . "&shopengine_product_id=" . $product->ID; |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | return $url; |
| 31 | }); |
| 32 | |
| 33 | $templates = $this->getTemplates(); |
| 34 | |
| 35 | foreach($templates as $key => $template) { |
| 36 | |
| 37 | if(isset($template['class']) && $template['class']) { |
| 38 | |
| 39 | new $template['class'](); |
| 40 | |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | public function getTemplates() { |
| 47 | |
| 48 | if(!$this->listedCollected) { |
| 49 | $this->templateList = apply_filters('shopengine/page_templates', $this->get_list()); |
| 50 | $this->listedCollected = true; |
| 51 | } |
| 52 | |
| 53 | return $this->templateList; |
| 54 | } |
| 55 | |
| 56 | public function getTemplate($slug) { |
| 57 | $page_templates = $this->getTemplates(); |
| 58 | |
| 59 | return $page_templates[$slug] ?? []; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | public function get_list() { |
| 64 | |
| 65 | $product_id = Products::instance()->get_preview_product(); |
| 66 | |
| 67 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Some other templates call it without nonce added. |
| 68 | if(isset($_GET['shopengine_product_id'])) { |
| 69 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Some other templates call it without nonce added. |
| 70 | $product_id = sanitize_text_field(wp_unslash($_GET['shopengine_product_id'])); |
| 71 | update_option('__shopengine_preview_product_id', $product_id); |
| 72 | } elseif(get_option('__shopengine_preview_product_id')) { |
| 73 | $product_id = get_option('__shopengine_preview_product_id'); |
| 74 | } |
| 75 | |
| 76 | $shop_url = get_permalink(wc_get_page_id('shop')); |
| 77 | $shop_url = (strpos($shop_url, '?page_id') !== false ? get_home_url() . '?post_type=product' : $shop_url); |
| 78 | |
| 79 | return [ |
| 80 | 'shop' => [ |
| 81 | 'title' => esc_html__('Shop', 'shopengine'), |
| 82 | 'package' => 'free', |
| 83 | 'class' => 'ShopEngine\Core\Page_Templates\Hooks\Shop', |
| 84 | 'opt_key' => 'shop', |
| 85 | 'css' => 'shop', |
| 86 | 'url' => $shop_url, |
| 87 | ], |
| 88 | 'archive' => [ |
| 89 | 'title' => esc_html__('Archive', 'shopengine'), |
| 90 | 'package' => 'free', |
| 91 | 'class' => 'ShopEngine\Core\Page_Templates\Hooks\Archive', |
| 92 | 'opt_key' => 'archive', |
| 93 | 'css' => 'archive', |
| 94 | 'url' => $shop_url, |
| 95 | ], |
| 96 | 'single' => [ |
| 97 | 'title' => esc_html__('Single', 'shopengine'), |
| 98 | 'package' => 'free', |
| 99 | 'class' => 'ShopEngine\Core\Page_Templates\Hooks\Single', |
| 100 | 'opt_key' => 'single', |
| 101 | 'css' => 'single', |
| 102 | 'url' => get_permalink($product_id), |
| 103 | ], |
| 104 | 'cart' => [ |
| 105 | 'title' => esc_html__('Cart', 'shopengine'), |
| 106 | 'package' => 'free', |
| 107 | 'class' => 'ShopEngine\Core\Page_Templates\Hooks\Cart', |
| 108 | 'opt_key' => 'cart', |
| 109 | 'css' => 'cart', |
| 110 | 'url' => get_permalink(wc_get_page_id('cart')), |
| 111 | ], |
| 112 | 'checkout' => [ |
| 113 | 'title' => esc_html__('Checkout', 'shopengine'), |
| 114 | 'package' => 'free', |
| 115 | 'class' => 'ShopEngine\Core\Page_Templates\Hooks\Checkout', |
| 116 | 'opt_key' => 'checkout', |
| 117 | 'css' => 'checkout', |
| 118 | 'url' => get_permalink(wc_get_page_id('checkout')), |
| 119 | ], |
| 120 | ]; |
| 121 | |
| 122 | |
| 123 | } |
| 124 | } |
| 125 |