PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.6.1
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.6.1
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / data / utils / wizard / class-wizard-template-provider.php
superb-blocks / src / data / utils / wizard Last commit date
wizard-items 11 months ago class-wizard-constants.php 11 months ago class-wizard-creation-util.php 11 months ago class-wizard-exception.php 11 months ago class-wizard-menu-creator.php 11 months ago class-wizard-page-creator.php 11 months ago class-wizard-part-creator.php 11 months ago class-wizard-stage-util.php 11 months ago class-wizard-template-provider.php 11 months ago
class-wizard-template-provider.php
422 lines
1 <?php
2
3 namespace SuperbAddons\Data\Utils\Wizard;
4
5 use SuperbAddons\Admin\Controllers\Wizard\WizardRestorationPointController;
6 use SuperbAddons\Data\Controllers\KeyController;
7 use SuperbAddons\Data\Controllers\RestController;
8 use SuperbAddons\Library\Controllers\LibraryRequestController;
9
10 use WP_REST_Request;
11
12 defined('ABSPATH') || exit();
13
14 class WizardTemplateProvider
15 {
16 private $headerTemplates;
17 private $footerTemplates;
18 private $blogTemplates;
19 private $homeTemplates;
20 private $indexTemplates;
21 private $frontPageTemplates;
22 private $templatePages;
23
24 private $hasFrontPageTemplate;
25 private $hasHomeTemplate;
26 private $hasIndexTemplate;
27
28 private $userIsPremium;
29
30 public function __construct()
31 {
32 $this->headerTemplates = array();
33 $this->footerTemplates = array();
34 $this->blogTemplates = array();
35 $this->homeTemplates = array();
36 $this->indexTemplates = array();
37 $this->frontPageTemplates = array();
38 $this->templatePages = array();
39
40 $this->hasFrontPageTemplate = false;
41 $this->hasHomeTemplate = false;
42 $this->hasIndexTemplate = false;
43 $this->userIsPremium = KeyController::HasValidPremiumKey();
44 }
45
46 private function HasModifiedTemplate($slug, $template_type)
47 {
48 $args = array(
49 'post_type' => $template_type,
50 'posts_per_page' => 1,
51 'post_name__in' => array($slug),
52 'tax_query' => array(
53 array(
54 'taxonomy' => 'wp_theme',
55 'field' => 'name',
56 'terms' => get_stylesheet()
57 )
58 )
59 );
60 if ($template_type === WizardItemTypes::WP_TEMPLATE_PART) {
61 $args['tax_query'][] = array(
62 'taxonomy' => 'wp_template_part_area',
63 'field' => 'name',
64 'terms' => $slug
65 );
66 $args['tax_query']['relation'] = 'AND';
67 }
68
69 $modifiedTemplatePost = get_posts($args);
70
71 return $modifiedTemplatePost && !empty($modifiedTemplatePost) && $modifiedTemplatePost[0]->post_name === $slug;
72 }
73
74 private function InitializePart($slugOrWizardItem, $type, $regularTitle, $modifiedTitle)
75 {
76 if ($slugOrWizardItem instanceof WizarditemRestorationPoint) {
77 $this->AddToAppropriateArray($slugOrWizardItem, $slugOrWizardItem->GetBaseSlug());
78 return;
79 }
80
81 $datatype = false;
82 if ($slugOrWizardItem instanceof WizardItem) {
83 $datatype = $slugOrWizardItem->datatype;
84 $slug = $slugOrWizardItem->GetBaseSlug();
85 } else {
86 $slug = $slugOrWizardItem;
87 }
88
89 $partTemplate = get_block_template(get_stylesheet() . '//' . $slug, $type);
90
91 $hasModifiedPart = $this->HasModifiedTemplate($slug, $type);
92 if ($partTemplate) {
93 $partItem = new WizardItemTemplate($partTemplate, $regularTitle, $modifiedTitle, $hasModifiedPart);
94 if ($datatype) {
95 $partItem->datatype = $datatype;
96 }
97
98 $this->AddToAppropriateArray($partItem, $slug);
99 }
100 if ($hasModifiedPart) {
101 // Get the original part template from the theme file
102 $themePartItem = new WizardItemFile($slug, $type, $regularTitle);
103 if ($datatype) {
104 $partItem->datatype = $datatype;
105 }
106 $this->AddToAppropriateArray($themePartItem, $slug);
107 }
108 }
109
110 public function InitializePatterns($required_plugin = false)
111 {
112 // Check if the theme has a modified header template part post
113 $this->InitializePart('header', WizardItemTypes::WP_TEMPLATE_PART, __("Header (Theme)", "superb-blocks"), __("Header (Current)", "superb-blocks"));
114 $this->InitializePart('footer', WizardItemTypes::WP_TEMPLATE_PART, __("Footer (Theme)", "superb-blocks"), __("Footer (Current)", "superb-blocks"));
115
116 try {
117 $request = new WP_REST_Request('GET', '/' . RestController::NAMESPACE . LibraryRequestController::GUTENBERG_LIST_ROUTE . 'patterns');
118 $patterns_response = rest_do_request($request);
119 if (!$patterns_response || is_wp_error($patterns_response) || $patterns_response->is_error() || !isset($patterns_response->data) || !isset($patterns_response->data->items)) {
120 throw new WizardException(__("An error occurred while fetching available patterns.", "superb-blocks"));
121 }
122
123 // Order free to the top of list array if the user is not premium
124 if (!$this->userIsPremium) {
125 $freePatterns = array();
126 $premiumPatterns = array();
127 foreach ($patterns_response->data->items as $pattern) {
128 if (isset($pattern->package) && $pattern->package === 'premium') {
129 $premiumPatterns[] = $pattern;
130 } else {
131 $freePatterns[] = $pattern;
132 }
133 }
134 $patterns_response->data->items = array_merge($freePatterns, $premiumPatterns);
135 }
136 // Get header and footer templates from patterns
137 foreach ($patterns_response->data->items as $pattern) {
138 if ($required_plugin && !isset($pattern->required_plugins) || $required_plugin && !in_array($required_plugin, $pattern->required_plugins)) {
139 continue;
140 }
141 $patternItem = new WizardItemPattern($pattern);
142 foreach ($pattern->categories as $category) {
143 if ($category->id === WizardDataCategory::NAVIGATION) {
144 $this->headerTemplates[] = $patternItem;
145 } else if ($category->id === WizardDataCategory::FOOTER) {
146 $this->footerTemplates[] = $patternItem;
147 }
148 }
149 }
150 } catch (WizardException $e) {
151 // Exception is automatically logged. Catch it so that the wizard can continue with the available theme templates.
152 }
153 }
154
155 public function InitalizePageTemplates()
156 {
157 $acceptedTemplates = $this->GetAcceptedTemplates();
158
159 // Initial sort templates by slug
160 $this->SortTemplatesIntoArraysBySlug($acceptedTemplates);
161
162 // Additional template logic
163 $this->HandleTemplateLogic();
164
165 // Add addons service templates
166 try {
167 $request = new WP_REST_Request('GET', '/' . RestController::NAMESPACE . LibraryRequestController::GUTENBERG_LIST_ROUTE . 'pages');
168 $pages_response = rest_do_request($request);
169 if (!$pages_response || is_wp_error($pages_response) || $pages_response->is_error() || !isset($pages_response->data) || !isset($pages_response->data->items)) {
170 throw new WizardException(__("An error occurred while fetching available pages.", "superb-blocks"));
171 }
172
173 // Order free to the top of list array if the user is not premium
174 if (!$this->userIsPremium) {
175 $freePages = array();
176 $premiumPages = array();
177 foreach ($pages_response->data->items as $page) {
178 if (isset($page->package) && $page->package === 'premium') {
179 $premiumPages[] = $page;
180 } else {
181 $freePages[] = $page;
182 }
183 }
184 $pages_response->data->items = array_merge($freePages, $premiumPages);
185 }
186
187 foreach ($pages_response->data->items as $page) {
188 $pageItem = new WizardItemPage($page);
189 $pageItem->use_custom_page_template_preview = 1;
190 $this->templatePages[] = $pageItem;
191
192 // Add landing page templates to the front page templates
193 foreach ($page->categories as $category) {
194 if ($category->id === WizardDataCategory::LANDING_PAGE) {
195 $landingPageItem = new WizardItemPage($page);
196 $this->frontPageTemplates[] = $landingPageItem;
197 }
198 if ($category->id === WizardDataCategory::BLOG) {
199 $this->blogTemplates[] = $pageItem;
200 }
201 }
202 }
203 } catch (WizardException $e) {
204 // Exception is automatically logged. Catch it so that the wizard can continue with the available theme templates.
205 }
206 }
207
208 private function HandleTemplateLogic($logicType = 'default')
209 {
210 // Check for static pages
211 $has_static_pages = get_option('show_on_front') === 'page';
212 $staticFrontPageID = get_option('page_on_front');
213 $hasStaticFrontPage = $has_static_pages && $staticFrontPageID && $staticFrontPageID !== 0;
214 $staticBlogPageID = get_option('page_for_posts');
215 $hasStaticBlogPage = $has_static_pages && $staticBlogPageID && $staticBlogPageID !== 0;
216
217 if ($logicType === "default") {
218 // Front page templates additional logic
219 if (!$this->hasFrontPageTemplate) {
220 // If no front page template exists, set regular theme templates
221 $this->frontPageTemplates = $this->templatePages;
222
223 if ($hasStaticFrontPage) {
224 $static_page = new WizardItemStatic(__("Static Homepage (Current)", "superb-blocks"), $staticFrontPageID);
225 $this->frontPageTemplates = array_merge(array($static_page), $this->frontPageTemplates);
226 }
227 }
228
229 // Add "No blog page" selection
230 $noBlogPage = array(new WizardItemIgnore(__("No Blog Page", "superb-blocks")));
231 // Blog templates logic
232 if ($this->hasHomeTemplate || $this->hasIndexTemplate) {
233 $this->blogTemplates = array_merge($noBlogPage, $this->homeTemplates, $this->indexTemplates, $this->blogTemplates);
234 } else {
235 if ($hasStaticBlogPage) {
236 $static_page = array(new WizardItemStatic(__("Static Blog Page (Current)", "superb-blocks"), $staticBlogPageID));
237 $this->blogTemplates = array_merge($noBlogPage, $static_page);
238 } else {
239 $this->blogTemplates = $noBlogPage;
240 }
241 }
242
243 // If home templates exist and no front page template exists, add the home templates to the front page templates
244 if (!empty($this->homeTemplates) && !$this->hasFrontPageTemplate) {
245 if ($hasStaticFrontPage) {
246 // Add the home templates after the first item
247 array_splice($this->frontPageTemplates, 1, 0, $this->homeTemplates);
248 } else {
249 // Add the home templates to the front of the front page templates
250 $this->frontPageTemplates = array_merge($this->homeTemplates, $this->frontPageTemplates);
251 }
252 }
253 }
254
255 if ($logicType === "restoration") {
256 if (!empty($this->headerTemplates)) {
257 $currentHeader = get_block_template(get_stylesheet() . '//' . 'header', WizardItemTypes::WP_TEMPLATE_PART);
258 $currentHeader->title = $currentHeader->title . __(" (Current)", "superb-blocks");
259 $currentHeader = new WizardItem($currentHeader);
260 $this->headerTemplates = array_merge(array($currentHeader), $this->headerTemplates);
261 }
262
263 if (!empty($this->footerTemplates)) {
264 $currentFooter = get_block_template(get_stylesheet() . '//' . 'footer', WizardItemTypes::WP_TEMPLATE_PART);
265 $currentFooter->title = $currentFooter->title . __(" (Current)", "superb-blocks");
266 $currentFooter = new WizardItem($currentFooter);
267 $this->footerTemplates = array_merge(array($currentFooter), $this->footerTemplates);
268 }
269
270
271 if (!empty($this->frontPageTemplates)) {
272 if ($this->hasFrontPageTemplate) {
273 $currentFrontPage = get_block_template(get_stylesheet() . '//' . 'front-page', WizardItemTypes::WP_TEMPLATE);
274 $currentFrontPage->title = $currentFrontPage->title . __(" (Current)", "superb-blocks");
275 $currentFrontPage = new WizardItem($currentFrontPage);
276 $this->frontPageTemplates = array_merge(array($currentFrontPage), $this->frontPageTemplates);
277 }
278 }
279
280 $this->blogTemplates = array_merge($this->homeTemplates, $this->indexTemplates, $this->blogTemplates);
281 if (!empty($this->blogTemplates)) {
282 if ($this->hasHomeTemplate) {
283 $currentBlogPage = get_block_template(get_stylesheet() . '//' . 'home', WizardItemTypes::WP_TEMPLATE);
284 } elseif ($this->hasIndexTemplate) {
285 $currentBlogPage = get_block_template(get_stylesheet() . '//' . 'index', WizardItemTypes::WP_TEMPLATE);
286 }
287
288 if ($currentBlogPage) {
289 $currentBlogPage->datatype = 'blog';
290 $currentBlogPage->title = $currentBlogPage->title . __(" (Current)", "superb-blocks");
291 $currentBlogPage = new WizardItem($currentBlogPage);
292 $this->blogTemplates = array_merge(array($currentBlogPage), $this->blogTemplates);
293 }
294 }
295 }
296 }
297
298 private function GetAcceptedTemplates()
299 {
300 $templates = get_block_templates();
301 $acceptedTemplates = array();
302 // Filter out templates that are not accepted for the current theme
303 foreach ($templates as $template) {
304 if ($template->status !== 'publish') continue;
305 if ($template->type !== WizardItemTypes::WP_TEMPLATE) continue;
306 if ($template->theme !== get_stylesheet()) continue;
307 if ($template->slug === AddonsPageTemplateUtil::TEMPLATE_ID) continue; // Do not include the wizard page creation template
308 if ($template->slug !== 'front-page' && $template->slug !== 'home' && $template->slug !== 'index') {
309 if (!$template->is_custom) continue;
310 if (!$template->post_types || !in_array('page', $template->post_types)) continue;
311 }
312 $acceptedTemplates[] = new WizardItem($template);
313 }
314 return $acceptedTemplates;
315 }
316
317 public function InitializeRestorationTemplates()
318 {
319 $theme_restoration = WizardRestorationPointController::GetThemeRestorationPoints();
320 if (!$theme_restoration) {
321 return;
322 }
323
324 $restoration_templates = [];
325 foreach ($theme_restoration as $restorationId => $restorationPointArray) {
326 $restoration_templates[] = new WizarditemRestorationPoint($restorationId, $restorationPointArray);
327 }
328
329 // Sort restoration templates by timestamp
330 usort($restoration_templates, function ($a, $b) {
331 return $a->timestamp <=> $b->timestamp;
332 });
333
334 $this->SortTemplatesIntoArraysBySlug($restoration_templates, "restoration");
335 $this->HandleTemplateLogic('restoration');
336 }
337
338 private function SortTemplatesIntoArraysBySlug($acceptedTemplates, $logicType = "default")
339 {
340 foreach ($acceptedTemplates as $template) {
341 $slug = $template->GetBaseSlug();
342
343 if ($slug === 'front-page' || $slug === 'home' || $slug === 'index') {
344 switch ($slug) {
345 case "front-page":
346 $base_label = __("Front Page", "superb-blocks");
347 break;
348 case "home":
349 $template->datatype = 'blog';
350 $base_label = __("Blog Home", "superb-blocks");
351 break;
352 case "index":
353 $template->datatype = 'blog';
354 $base_label = __("Index", "superb-blocks");
355 break;
356 }
357 $this->InitializePart($template, WizardItemTypes::WP_TEMPLATE, $base_label . __(" (Theme)", "superb-blocks"), $base_label . __(" (Current)", "superb-blocks"));
358 } else {
359 if ($logicType === "default") {
360 $template->title = $template->title . ' (' . __('Theme', 'superb-blocks') . ')';
361 }
362 $this->AddToAppropriateArray($template, $slug);
363 }
364 }
365 }
366
367 private function AddToAppropriateArray($item, $slug)
368 {
369 switch ($slug) {
370 case 'header':
371 $this->headerTemplates[] = $item;
372 break;
373 case 'footer':
374 $this->footerTemplates[] = $item;
375 break;
376 case 'front-page':
377 $this->hasFrontPageTemplate = true;
378 $this->frontPageTemplates[] = $item;
379 break;
380 case 'home':
381 $this->hasHomeTemplate = true;
382 $this->homeTemplates[] = $item;
383 break;
384 case 'index':
385 $this->hasIndexTemplate = true;
386 $this->indexTemplates[] = $item;
387 break;
388 default:
389 if (strpos($slug, 'blog') !== false) {
390 $this->blogTemplates[] = $item;
391 }
392 $this->templatePages[] = $item;
393 break;
394 }
395 }
396
397 public function GetHeaderTemplates()
398 {
399 return $this->headerTemplates;
400 }
401
402 public function GetFooterTemplates()
403 {
404 return $this->footerTemplates;
405 }
406
407 public function GetBlogTemplates()
408 {
409 return $this->blogTemplates;
410 }
411
412 public function GetFrontPageTemplates()
413 {
414 return $this->frontPageTemplates;
415 }
416
417 public function GetTemplatePages()
418 {
419 return $this->templatePages;
420 }
421 }
422