PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.44
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.44
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 51.1.45 All 40 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.44, at includes/helpers/Theme_Builder/Repository.php

190 lines 5.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 = new WP_Query([
41 'post_type' => 'elementor_library',
42 'post_status' => ['publish', 'draft', 'pending', 'future'],
43 'posts_per_page' => -1,
44 'meta_query' => [
45 [
46 'key' => Meta_Keys::ENABLED,
47 'compare' => 'EXISTS',
48 ],
49 ],
50 'fields' => 'ids',
51 'no_found_rows' => true,
52 ]);
53
54 $templates = [];
55
56 foreach ($query->posts as $post_id) {
57 $location = get_post_meta($post_id, Meta_Keys::LOCATION, true) ?: '';
58 $sub_location = get_post_meta($post_id, Meta_Keys::SUB_LOCATION, true) ?: '';
59 $conditions_meta = get_post_meta($post_id, Meta_Keys::CONDITIONS, true);
60 $conditions = Conditions::normalize($conditions_meta);
61 $priority = (int) get_post_meta($post_id, Meta_Keys::PRIORITY, true);
62 $is_pro_only = (bool) get_post_meta($post_id, Meta_Keys::IS_PRO_ONLY, true);
63 $enabled = (string) get_post_meta($post_id, Meta_Keys::ENABLED, true) === '1';
64
65 $templates[] = [
66 'id' => (int) $post_id,
67 'location' => $location,
68 'sub_location' => $sub_location,
69 'conditions' => $conditions,
70 'priority' => $priority ?: 10,
71 'is_pro_only' => $is_pro_only || Conditions::is_pro_only($conditions),
72 'enabled' => $enabled,
73 ];
74 }
75
76 set_transient($this->cache_key, $templates, HOUR_IN_SECONDS);
77
78 return $templates;
79 }
80
81 /**
82 * Delete cached templates.
83 *
84 * @return void
85 */
86 public function clear_cache(): void
87 {
88 delete_transient($this->cache_key);
89 }
90
91 /**
92 * Get templates that match request location.
93 *
94 * @param array<string,mixed> $context Request context.
95 *
96 * @return array<int,array<string,mixed>>
97 */
98 public function get_location_candidates(array $context): array
99 {
100 $templates = $this->get_active_templates();
101 $candidates = [];
102 foreach ($templates as $template) {
103 if (empty($template['enabled'])) {
104 continue;
105 }
106 if ($this->matches_location($template, $context)) {
107 $candidates[] = $template;
108 }
109 }
110
111 return $candidates;
112 }
113
114 /**
115 * Check if template location matches context.
116 *
117 * @param array<string,mixed> $template Template data.
118 * @param array<string,mixed> $context Request context.
119 *
120 * @return bool
121 */
122 private function matches_location(array $template, array $context): bool
123 {
124 $location = $template['location'] ?? '';
125 $sub_location = $template['sub_location'] ?? '';
126 $type = $context['type'] ?? '';
127
128 if ('not_found' === $type) {
129 return 'not_found' === $sub_location || 'not_found' === $location;
130 }
131
132 if ('search' === $type) {
133 return 'search_results' === $sub_location || 'search' === $location;
134 }
135
136 if ('author' === $type) {
137 return in_array($sub_location, ['author_all', 'author_specific'], true);
138 }
139
140 if ('single' === $type) {
141 $post_type = $context['post_type'] ?? '';
142 if ('single' === $location) {
143 if ('single_post' === $sub_location && 'post' === $post_type) {
144 return true;
145 }
146 if ('single_page' === $sub_location && 'page' === $post_type) {
147 return true;
148 }
149 if ('single_cpt' === $sub_location && !in_array($post_type, ['post', 'page'], true)) {
150 return true;
151 }
152 if (!empty($post_type) && ('single_' . $post_type) === $sub_location) {
153 return true;
154 }
155 return empty($sub_location);
156 }
157 }
158
159 if ('archive' === $type) {
160 $taxonomy = $context['taxonomy'] ?? '';
161 $is_blog = !empty($context['is_blog_page']);
162
163 if ('archive' === $location) {
164 if ($is_blog && 'archive_blog' === $sub_location) {
165 return true;
166 }
167 if ('archive_category' === $sub_location && 'category' === $taxonomy) {
168 return true;
169 }
170 if ('archive_tag' === $sub_location && 'post_tag' === $taxonomy) {
171 return true;
172 }
173 if ('archive_tax' === $sub_location && !empty($taxonomy)) {
174 return true;
175 }
176 if (!empty($taxonomy) && ('archive_' . $taxonomy) === $sub_location) {
177 return true;
178 }
179 return empty($sub_location);
180 }
181 }
182
183 return false;
184 }
185 }
186
187
188
189
190