PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.87
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.87
51.1.87 51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 All 41 releases
king-addons / includes / helpers / Theme_Builder / Repository.php

Repository.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.87, at includes/helpers/Theme_Builder/Repository.php

213 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if (empty($conditions['groups'])) {
77 $conditions = Conditions::defaults_for_sub_location($sub_location);
78 }
79 $priority = (int) get_post_meta($post_id, Meta_Keys::PRIORITY, true);
80 $is_pro_only = (bool) get_post_meta($post_id, Meta_Keys::IS_PRO_ONLY, true);
81
82 // Check if ENABLED meta exists
83 $enabled_meta = get_post_meta($post_id, Meta_Keys::ENABLED, true);
84 // If ENABLED meta doesn't exist but LOCATION does, default to enabled
85 // If ENABLED meta exists, use its value
86 $enabled = ($enabled_meta === '') ? true : ((string) $enabled_meta === '1');
87
88 $templates[] = [
89 'id' => (int) $post_id,
90 'location' => $location,
91 'sub_location' => $sub_location,
92 'conditions' => $conditions,
93 'priority' => $priority ?: 10,
94 'is_pro_only' => $is_pro_only || Conditions::is_pro_only($conditions),
95 'enabled' => $enabled,
96 ];
97 }
98
99 set_transient($this->cache_key, $templates, HOUR_IN_SECONDS);
100
101 return $templates;
102 }
103
104 /**
105 * Delete cached templates.
106 *
107 * @return void
108 */
109 public function clear_cache(): void
110 {
111 delete_transient($this->cache_key);
112 }
113
114 /**
115 * Get templates that match request location.
116 *
117 * @param array<string,mixed> $context Request context.
118 *
119 * @return array<int,array<string,mixed>>
120 */
121 public function get_location_candidates(array $context): array
122 {
123 $templates = $this->get_active_templates();
124 $candidates = [];
125 foreach ($templates as $template) {
126 if (empty($template['enabled'])) {
127 continue;
128 }
129 if ($this->matches_location($template, $context)) {
130 $candidates[] = $template;
131 }
132 }
133
134 return $candidates;
135 }
136
137 /**
138 * Check if template location matches context.
139 *
140 * @param array<string,mixed> $template Template data.
141 * @param array<string,mixed> $context Request context.
142 *
143 * @return bool
144 */
145 private function matches_location(array $template, array $context): bool
146 {
147 $location = $template['location'] ?? '';
148 $sub_location = $template['sub_location'] ?? '';
149 $type = $context['type'] ?? '';
150
151 if ('not_found' === $type) {
152 return 'not_found' === $sub_location || 'not_found' === $location;
153 }
154
155 if ('search' === $type) {
156 return 'search_results' === $sub_location || 'search' === $location;
157 }
158
159 if ('author' === $type) {
160 return in_array($sub_location, ['author_all', 'author_specific'], true);
161 }
162
163 if ('single' === $type) {
164 $post_type = $context['post_type'] ?? '';
165 if ('single' === $location) {
166 if ('single_post' === $sub_location && 'post' === $post_type) {
167 return true;
168 }
169 if ('single_page' === $sub_location && 'page' === $post_type) {
170 return true;
171 }
172 if ('single_cpt' === $sub_location && !in_array($post_type, ['post', 'page'], true)) {
173 return true;
174 }
175 if (!empty($post_type) && ('single_' . $post_type) === $sub_location) {
176 return true;
177 }
178 return empty($sub_location);
179 }
180 }
181
182 if ('archive' === $type) {
183 $taxonomy = $context['taxonomy'] ?? '';
184 $is_blog = !empty($context['is_blog_page']);
185
186 if ('archive' === $location) {
187 if ($is_blog && 'archive_blog' === $sub_location) {
188 return true;
189 }
190 if ('archive_category' === $sub_location && 'category' === $taxonomy) {
191 return true;
192 }
193 if ('archive_tag' === $sub_location && 'post_tag' === $taxonomy) {
194 return true;
195 }
196 if ('archive_tax' === $sub_location && !empty($taxonomy)) {
197 return true;
198 }
199 if (!empty($taxonomy) && ('archive_' . $taxonomy) === $sub_location) {
200 return true;
201 }
202 return empty($sub_location);
203 }
204 }
205
206 return false;
207 }
208 }
209
210
211
212
213