| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\Templating; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
//use FluentCart\App\Hooks\Handlers\ShortCodes\Buttons\AddToCartShortcode; |
| 7 |
use FluentCart\App\Hooks\Handlers\ShortCodes\SingleProductShortCode; |
| 8 |
use FluentCart\App\Modules\Templating\BlockTemplates\ProductCategoryTemplate; |
| 9 |
use FluentCart\App\Modules\Templating\BlockTemplates\ProductPageTemplate; |
| 10 |
|
| 11 |
class TemplateLoader |
| 12 |
{ |
| 13 |
|
| 14 |
private static $supportedTheme = false; |
| 15 |
|
| 16 |
public static $currentRenderingPageType = ''; |
| 17 |
|
| 18 |
public static $currentTaxonomy = null; |
| 19 |
|
| 20 |
public static function init() |
| 21 |
{ |
| 22 |
static $loaded; |
| 23 |
if ($loaded) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$loaded = true; |
| 28 |
|
| 29 |
self::$supportedTheme = self::hasThemeSupport(); |
| 30 |
|
| 31 |
if (self::$supportedTheme) { |
| 32 |
add_filter('template_include', [self::class, 'loadTemplate'], 10); |
| 33 |
} else { |
| 34 |
add_action('template_redirect', array(__CLASS__, 'initUnsupportedTheme'), 1); |
| 35 |
} |
| 36 |
|
| 37 |
// Handling Post Type Archive redirection or simulation to the shop page |
| 38 |
add_action('template_redirect', function () { |
| 39 |
if (is_post_type_archive('fluent-products')) { |
| 40 |
global $wp; |
| 41 |
// we will redirect to shop page if its set |
| 42 |
$storeSettings = new StoreSettings(); |
| 43 |
$shopPage = trim($storeSettings->getShopPage(), '/'); |
| 44 |
$requestUrl = trim(home_url($wp->request), '/'); |
| 45 |
|
| 46 |
if ($shopPage && $shopPage !== $requestUrl) { |
| 47 |
wp_redirect($shopPage, 301); |
| 48 |
exit; |
| 49 |
} |
| 50 |
} |
| 51 |
}, 1); |
| 52 |
|
| 53 |
add_action('fluent_cart/render_products_archive', [__CLASS__, 'renderProductsArchive']); |
| 54 |
|
| 55 |
add_filter('fluent_cart/shop_app_product_query_taxonomy_filters', function ($taxFilters, $args) { |
| 56 |
|
| 57 |
return $taxFilters; |
| 58 |
|
| 59 |
$currentTaxonomy = TemplateLoader::$currentTaxonomy; |
| 60 |
if ($currentTaxonomy && $args['is_main_query']) { |
| 61 |
unset($taxFilters[$currentTaxonomy->taxonomy]); |
| 62 |
$taxFilters[$currentTaxonomy->taxonomy] = [$currentTaxonomy->term_id]; |
| 63 |
} |
| 64 |
|
| 65 |
return $taxFilters; |
| 66 |
}, 10, 2); |
| 67 |
|
| 68 |
(new TemplateActions())->register(); |
| 69 |
|
| 70 |
AssetLoader::register(); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
public static function registerBlockParts() |
| 75 |
{ |
| 76 |
if (!self::supportsBlockTemplates('wp_template')) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
(new ProductCategoryTemplate())->init(); |
| 81 |
(new ProductPageTemplate())->init(); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
public static function loadTemplate($template) |
| 86 |
{ |
| 87 |
if (is_embed()) { |
| 88 |
return $template; |
| 89 |
} |
| 90 |
|
| 91 |
$default_file = self::getDefaultTemplateFile(); |
| 92 |
|
| 93 |
if (!$default_file) { |
| 94 |
return $template; |
| 95 |
} |
| 96 |
|
| 97 |
$search_files = self::getTemplateLoaderFiles($default_file); |
| 98 |
|
| 99 |
$locatedFile = locate_template($search_files); |
| 100 |
|
| 101 |
if ($locatedFile) { |
| 102 |
return $locatedFile; |
| 103 |
} |
| 104 |
|
| 105 |
return $template; |
| 106 |
} |
| 107 |
|
| 108 |
public static function initUnsupportedTheme() |
| 109 |
{ |
| 110 |
$isTaxPages = is_tax(get_object_taxonomies('fluent-products')); |
| 111 |
|
| 112 |
if ($isTaxPages) { |
| 113 |
if (apply_filters('fluent_cart/template/disable_taxonomy_fallback', false)) { |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
add_filter('template_include', array(__CLASS__, 'loadGenericFallbackTemplate'), 9999); |
| 118 |
} else if (is_singular('fluent-products')) { |
| 119 |
(new TemplateActions())->initSingleProductHooks(); |
| 120 |
} |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
private static function simulateProductsArchive() |
| 125 |
{ |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
public static function loadGenericFallbackTemplate($template) |
| 130 |
{ |
| 131 |
return __DIR__ . '/fallback-generic-template.php'; |
| 132 |
} |
| 133 |
|
| 134 |
public static function renderProductsArchive($args = []) |
| 135 |
{ |
| 136 |
echo do_shortcode('[fluent_cart_products]'); |
| 137 |
} |
| 138 |
|
| 139 |
public static function hasThemeSupport() |
| 140 |
{ |
| 141 |
return (bool)current_theme_supports('fluent_cart') || wp_is_block_theme(); |
| 142 |
} |
| 143 |
|
| 144 |
private static function getDefaultTemplateFile() |
| 145 |
{ |
| 146 |
if (is_singular('fluent-products') && !self::hasBlockTemplate('single-fluent-products') |
| 147 |
) { |
| 148 |
$default_file = 'single-fluent-products.php'; |
| 149 |
} elseif (is_tax(get_object_taxonomies('fluent-products'))) { |
| 150 |
$object = get_queried_object(); |
| 151 |
self::$currentRenderingPageType = $object->taxonomy; |
| 152 |
self::$currentTaxonomy = $object; |
| 153 |
|
| 154 |
if (self::taxonomyHasBlockTemplate($object)) { |
| 155 |
$default_file = ''; |
| 156 |
} elseif (is_tax('product-categories') || is_tax('product-brands')) { |
| 157 |
$default_file = 'taxonomy-' . $object->taxonomy . '.php'; |
| 158 |
} elseif (!self::hasBlockTemplate('archive-fluent-products')) { |
| 159 |
$default_file = 'archive-fluent-products.php'; |
| 160 |
} else { |
| 161 |
$default_file = ''; |
| 162 |
} |
| 163 |
} else { |
| 164 |
$default_file = ''; |
| 165 |
} |
| 166 |
|
| 167 |
return $default_file; |
| 168 |
} |
| 169 |
|
| 170 |
private static function hasBlockTemplate($template_name) |
| 171 |
{ |
| 172 |
if (!$template_name) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
$has_template = false; |
| 177 |
$template_filename = $template_name . '.html'; |
| 178 |
|
| 179 |
$filepath = DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template_filename; |
| 180 |
$possible_paths = array( |
| 181 |
get_stylesheet_directory() . $filepath, |
| 182 |
get_template_directory() . $filepath, |
| 183 |
); |
| 184 |
|
| 185 |
// Check the first matching one. |
| 186 |
foreach ($possible_paths as $path) { |
| 187 |
if (is_readable($path)) { |
| 188 |
$has_template = true; |
| 189 |
break; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
return (bool)apply_filters('fluent_cart/has_block_template', $has_template, [ |
| 194 |
'template_name' => $template_name |
| 195 |
]); |
| 196 |
} |
| 197 |
|
| 198 |
private static function taxonomyHasBlockTemplate($taxonomy): bool |
| 199 |
{ |
| 200 |
$template_name = 'taxonomy-' . $taxonomy->taxonomy; |
| 201 |
return self::hasBlockTemplate($template_name); |
| 202 |
} |
| 203 |
|
| 204 |
private static function getTemplateLoaderFiles($defaultFile) |
| 205 |
{ |
| 206 |
$templates = apply_filters('fluent_cart/template_loader_files', array(), [ |
| 207 |
'default_file' => $defaultFile, |
| 208 |
]); |
| 209 |
|
| 210 |
$templates[] = 'fluent-cart.php'; |
| 211 |
|
| 212 |
if (is_page_template()) { |
| 213 |
$page_template = get_page_template_slug(); |
| 214 |
if ($page_template) { |
| 215 |
$validated_file = validate_file($page_template); |
| 216 |
if (0 === $validated_file) { |
| 217 |
$templates[] = $page_template; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if (is_singular('fluent-products')) { |
| 223 |
$object = get_queried_object(); |
| 224 |
$name_decoded = urldecode($object->post_name); |
| 225 |
if ($name_decoded !== $object->post_name) { |
| 226 |
$templates[] = "single-fluent-products-{$name_decoded}.php"; |
| 227 |
} |
| 228 |
$templates[] = "single-fluent-products-{$object->post_name}.php"; |
| 229 |
} |
| 230 |
|
| 231 |
if (is_tax(get_object_taxonomies('fluent-products'))) { |
| 232 |
$object = get_queried_object(); |
| 233 |
$templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php'; |
| 234 |
$templates[] = self::templatePath() . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php'; |
| 235 |
$templates[] = 'taxonomy-' . $object->taxonomy . '.php'; |
| 236 |
$templates[] = self::templatePath() . 'taxonomy-' . $object->taxonomy . '.php'; |
| 237 |
} |
| 238 |
|
| 239 |
$templates[] = $defaultFile; |
| 240 |
|
| 241 |
$templates[] = self::templatePath() . $defaultFile; |
| 242 |
|
| 243 |
return array_unique($templates); |
| 244 |
} |
| 245 |
|
| 246 |
public static function templatePath() |
| 247 |
{ |
| 248 |
return apply_filters('fluent_cart/template_path', 'fluent-cart/'); |
| 249 |
} |
| 250 |
|
| 251 |
public static function supportsBlockTemplates($templateType = 'wp_template') |
| 252 |
{ |
| 253 |
|
| 254 |
return ($templateType === 'wp_template_part' && |
| 255 |
(wp_is_block_theme() || current_theme_supports('block-template-parts')) |
| 256 |
) |
| 257 |
|| ($templateType == 'wp_template' && wp_is_block_theme()); |
| 258 |
} |
| 259 |
} |
| 260 |
|