| 1 |
<?php |
| 2 |
/** |
| 3 |
* Theme Builder template repository. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons\Theme_Builder; |
| 9 |
|
| 10 |
use WP_Query; |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Retrieves and caches Theme Builder templates. |
| 18 |
*/ |
| 19 |
class Repository |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Cache key for transient. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
private string $cache_key = 'king_addons_theme_builder_templates'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Retrieve active templates. |
| 30 |
* |
| 31 |
* @return array<int,array<string,mixed>> |
| 32 |
*/ |
| 33 |
public function get_active_templates(): array |
| 34 |
{ |
| 35 |
$cached = get_transient($this->cache_key); |
| 36 |
if (is_array($cached)) { |
| 37 |
return $cached; |
| 38 |
} |
| 39 |
|
| 40 |
// Query for templates that have EITHER ENABLED OR LOCATION meta key. |
| 41 |
// This ensures templates created via Theme Builder are shown, |
| 42 |
// even if they were created before the ENABLED key was set or if they |
| 43 |
// have only the LOCATION key set. |
| 44 |
$query = new WP_Query([ |
| 45 |
'post_type' => 'elementor_library', |
| 46 |
'post_status' => ['publish', 'draft', 'pending', 'future'], |
| 47 |
'posts_per_page' => -1, |
| 48 |
'meta_query' => [ |
| 49 |
'relation' => 'OR', |
| 50 |
[ |
| 51 |
'key' => Meta_Keys::ENABLED, |
| 52 |
'compare' => 'EXISTS', |
| 53 |
], |
| 54 |
[ |
| 55 |
'key' => Meta_Keys::LOCATION, |
| 56 |
'compare' => 'EXISTS', |
| 57 |
], |
| 58 |
], |
| 59 |
'fields' => 'ids', |
| 60 |
'no_found_rows' => true, |
| 61 |
]); |
| 62 |
|
| 63 |
$templates = []; |
| 64 |
|
| 65 |
foreach ($query->posts as $post_id) { |
| 66 |
$location = get_post_meta($post_id, Meta_Keys::LOCATION, true) ?: ''; |
| 67 |
|
| 68 |
// Skip templates without location - they're not Theme Builder templates |
| 69 |
if (empty($location)) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$sub_location = get_post_meta($post_id, Meta_Keys::SUB_LOCATION, true) ?: ''; |
| 74 |
$conditions_meta = get_post_meta($post_id, Meta_Keys::CONDITIONS, true); |
| 75 |
$conditions = Conditions::normalize($conditions_meta); |
| 76 |
$priority = (int) get_post_meta($post_id, Meta_Keys::PRIORITY, true); |
| 77 |
$is_pro_only = (bool) get_post_meta($post_id, Meta_Keys::IS_PRO_ONLY, true); |
| 78 |
|
| 79 |
// Check if ENABLED meta exists |
| 80 |
$enabled_meta = get_post_meta($post_id, Meta_Keys::ENABLED, true); |
| 81 |
// If ENABLED meta doesn't exist but LOCATION does, default to enabled |
| 82 |
// If ENABLED meta exists, use its value |
| 83 |
$enabled = ($enabled_meta === '') ? true : ((string) $enabled_meta === '1'); |
| 84 |
|
| 85 |
$templates[] = [ |
| 86 |
'id' => (int) $post_id, |
| 87 |
'location' => $location, |
| 88 |
'sub_location' => $sub_location, |
| 89 |
'conditions' => $conditions, |
| 90 |
'priority' => $priority ?: 10, |
| 91 |
'is_pro_only' => $is_pro_only || Conditions::is_pro_only($conditions), |
| 92 |
'enabled' => $enabled, |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
set_transient($this->cache_key, $templates, HOUR_IN_SECONDS); |
| 97 |
|
| 98 |
return $templates; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Delete cached templates. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function clear_cache(): void |
| 107 |
{ |
| 108 |
delete_transient($this->cache_key); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get templates that match request location. |
| 113 |
* |
| 114 |
* @param array<string,mixed> $context Request context. |
| 115 |
* |
| 116 |
* @return array<int,array<string,mixed>> |
| 117 |
*/ |
| 118 |
public function get_location_candidates(array $context): array |
| 119 |
{ |
| 120 |
$templates = $this->get_active_templates(); |
| 121 |
$candidates = []; |
| 122 |
foreach ($templates as $template) { |
| 123 |
if (empty($template['enabled'])) { |
| 124 |
continue; |
| 125 |
} |
| 126 |
if ($this->matches_location($template, $context)) { |
| 127 |
$candidates[] = $template; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $candidates; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Check if template location matches context. |
| 136 |
* |
| 137 |
* @param array<string,mixed> $template Template data. |
| 138 |
* @param array<string,mixed> $context Request context. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
*/ |
| 142 |
private function matches_location(array $template, array $context): bool |
| 143 |
{ |
| 144 |
$location = $template['location'] ?? ''; |
| 145 |
$sub_location = $template['sub_location'] ?? ''; |
| 146 |
$type = $context['type'] ?? ''; |
| 147 |
|
| 148 |
if ('not_found' === $type) { |
| 149 |
return 'not_found' === $sub_location || 'not_found' === $location; |
| 150 |
} |
| 151 |
|
| 152 |
if ('search' === $type) { |
| 153 |
return 'search_results' === $sub_location || 'search' === $location; |
| 154 |
} |
| 155 |
|
| 156 |
if ('author' === $type) { |
| 157 |
return in_array($sub_location, ['author_all', 'author_specific'], true); |
| 158 |
} |
| 159 |
|
| 160 |
if ('single' === $type) { |
| 161 |
$post_type = $context['post_type'] ?? ''; |
| 162 |
if ('single' === $location) { |
| 163 |
if ('single_post' === $sub_location && 'post' === $post_type) { |
| 164 |
return true; |
| 165 |
} |
| 166 |
if ('single_page' === $sub_location && 'page' === $post_type) { |
| 167 |
return true; |
| 168 |
} |
| 169 |
if ('single_cpt' === $sub_location && !in_array($post_type, ['post', 'page'], true)) { |
| 170 |
return true; |
| 171 |
} |
| 172 |
if (!empty($post_type) && ('single_' . $post_type) === $sub_location) { |
| 173 |
return true; |
| 174 |
} |
| 175 |
return empty($sub_location); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ('archive' === $type) { |
| 180 |
$taxonomy = $context['taxonomy'] ?? ''; |
| 181 |
$is_blog = !empty($context['is_blog_page']); |
| 182 |
|
| 183 |
if ('archive' === $location) { |
| 184 |
if ($is_blog && 'archive_blog' === $sub_location) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
if ('archive_category' === $sub_location && 'category' === $taxonomy) { |
| 188 |
return true; |
| 189 |
} |
| 190 |
if ('archive_tag' === $sub_location && 'post_tag' === $taxonomy) { |
| 191 |
return true; |
| 192 |
} |
| 193 |
if ('archive_tax' === $sub_location && !empty($taxonomy)) { |
| 194 |
return true; |
| 195 |
} |
| 196 |
if (!empty($taxonomy) && ('archive_' . $taxonomy) === $sub_location) { |
| 197 |
return true; |
| 198 |
} |
| 199 |
return empty($sub_location); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return false; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|