| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use StoreEngine\Utils\Template; |
| 10 |
use StoreEngine\Utils\Helper; |
| 11 |
|
| 12 |
class TemplateLoader { |
| 13 |
|
| 14 |
public function template_loader( $template ) { |
| 15 |
if ( is_embed() ) { |
| 16 |
return $template; |
| 17 |
} |
| 18 |
|
| 19 |
$default_file = $this->get_template_loader_default_file(); |
| 20 |
|
| 21 |
if ( $default_file ) { |
| 22 |
/** |
| 23 |
* Filter hook to choose which files to find before storeengine does it's own logic. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
$search_files = $this->get_template_loader_files( $default_file ); |
| 28 |
$template = locate_template( $search_files ); |
| 29 |
|
| 30 |
if ( ! $template ) { |
| 31 |
if ( false !== strpos( $default_file, Helper::PRODUCT_CATEGORY_TAXONOMY ) || false !== strpos( $default_file, Helper::PRODUCT_TAG_TAXONOMY ) ) { |
| 32 |
$cs_template = str_replace( '_', '-', $default_file ); |
| 33 |
$template = Template::plugin_path() . 'templates/' . $cs_template; |
| 34 |
} else { |
| 35 |
$template = Template::plugin_path() . 'templates/' . $default_file; |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return $template; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the default filename for a template. |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
private function get_template_loader_default_file(): string { |
| 49 |
$default_file = ''; |
| 50 |
if ( is_singular( Helper::PRODUCT_POST_TYPE ) ) { |
| 51 |
$default_file = 'single-product.php'; |
| 52 |
} elseif ( is_tax( get_object_taxonomies( Helper::PRODUCT_POST_TYPE ) ) ) { |
| 53 |
if ( is_tax( Helper::PRODUCT_CATEGORY_TAXONOMY ) ) { |
| 54 |
$default_file = 'taxonomy-product-category.php'; |
| 55 |
} elseif ( is_tax( Helper::PRODUCT_TAG_TAXONOMY ) ) { |
| 56 |
$default_file = 'taxonomy-product-tag.php'; |
| 57 |
} else { |
| 58 |
$default_file = 'archive-product.php'; |
| 59 |
} |
| 60 |
} elseif ( is_post_type_archive( Helper::PRODUCT_POST_TYPE ) ) { |
| 61 |
$default_file = 'archive-product.php'; |
| 62 |
} |
| 63 |
|
| 64 |
return $default_file; |
| 65 |
} |
| 66 |
|
| 67 |
private function get_template_loader_files( $default_file ) { |
| 68 |
$templates = apply_filters( 'storeengine\frontend\template\loader_files', array(), $default_file ); |
| 69 |
$templates[] = 'storeengine.php'; |
| 70 |
|
| 71 |
if ( is_page_template() ) { |
| 72 |
$page_template = get_page_template_slug(); |
| 73 |
|
| 74 |
if ( $page_template ) { |
| 75 |
$validated_file = validate_file( $page_template ); |
| 76 |
if ( 0 === $validated_file ) { |
| 77 |
$templates[] = $page_template; |
| 78 |
} else { |
| 79 |
Helper::log_error( "StoreEngine: Unable to validate template path: \"$page_template\". Error Code: $validated_file." ); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( is_singular( Helper::PRODUCT_POST_TYPE ) ) { |
| 85 |
$object = get_queried_object(); |
| 86 |
$name_decoded = urldecode( $object->post_name ); |
| 87 |
if ( $name_decoded !== $object->post_name ) { |
| 88 |
$templates[] = "single-product-{$name_decoded}.php"; |
| 89 |
} |
| 90 |
$templates[] = "single-product-{$object->post_name}.php"; |
| 91 |
} |
| 92 |
|
| 93 |
if ( is_tax( get_object_taxonomies( Helper::PRODUCT_CATEGORY_TAXONOMY ) ) ) { |
| 94 |
$object = get_queried_object(); |
| 95 |
if ( is_tax( Helper::PRODUCT_CATEGORY_TAXONOMY ) ) { |
| 96 |
$templates[] = 'taxonomy-product-category-' . $object->slug . '.php'; |
| 97 |
$templates[] = Template::template_path() . 'taxonomy-product-category-' . $object->slug . '.php'; |
| 98 |
$templates[] = 'taxonomy-product-category.php'; |
| 99 |
$templates[] = Template::template_path() . 'taxonomy-product-category.php'; |
| 100 |
} elseif ( is_tax( Helper::PRODUCT_TAG_TAXONOMY ) ) { |
| 101 |
$templates[] = 'taxonomy-product-tag-' . $object->slug . '.php'; |
| 102 |
$templates[] = Template::template_path() . 'taxonomy-product-tag-' . $object->slug . '.php'; |
| 103 |
$templates[] = 'taxonomy-product-tag.php'; |
| 104 |
$templates[] = Template::template_path() . 'taxonomy-product-tag.php'; |
| 105 |
} |
| 106 |
$cs_default = str_replace( '_', '-', $default_file ); |
| 107 |
$templates[] = $cs_default; |
| 108 |
} |
| 109 |
|
| 110 |
$templates[] = $default_file; |
| 111 |
|
| 112 |
if ( isset( $cs_default ) ) { |
| 113 |
$templates[] = Template::template_path() . $cs_default; |
| 114 |
} |
| 115 |
|
| 116 |
$templates[] = Template::template_path() . $default_file; |
| 117 |
|
| 118 |
return array_unique( $templates ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @deprecated use self::frontend_dashboard_template_redirect |
| 123 |
*/ |
| 124 |
public function frontend_dashboard_template( $template ) { |
| 125 |
global $wp_query; |
| 126 |
if ( get_queried_object_id() === (int) Helper::get_settings( 'dashboard_page' ) || ! empty( $wp_query->query['storeengine_dashboard_page'] ) ) { |
| 127 |
if ( ! is_user_logged_in() ) { |
| 128 |
wp_safe_redirect( storeengine_login_url( Helper::get_dashboard_url() ) ); |
| 129 |
exit; |
| 130 |
} |
| 131 |
|
| 132 |
return Template::plugin_path() . 'templates/shortcode/frontend-dashboard.php'; |
| 133 |
} |
| 134 |
|
| 135 |
return $template; |
| 136 |
} |
| 137 |
} |
| 138 |
|