PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.6.2
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.6.2
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-part-creator.php
superb-blocks / src / data / utils / wizard Last commit date
wizard-items 10 months ago class-wizard-constants.php 10 months ago class-wizard-creation-util.php 10 months ago class-wizard-exception.php 10 months ago class-wizard-menu-creator.php 10 months ago class-wizard-page-creator.php 10 months ago class-wizard-part-creator.php 10 months ago class-wizard-stage-util.php 10 months ago class-wizard-template-provider.php 10 months ago
class-wizard-part-creator.php
165 lines
1 <?php
2
3 namespace SuperbAddons\Data\Utils\Wizard;
4
5 use SuperbAddons\Admin\Controllers\Wizard\WizardRestorationPointController;
6 use SuperbAddons\Gutenberg\Controllers\GutenbergController;
7 use SuperbAddons\Library\Controllers\LibraryRequestController;
8
9 defined('ABSPATH') || exit();
10
11 class WizardPartCreator
12 {
13 public static function CreateTemplateParts($selection_data, $wizardData)
14 {
15 foreach ($wizardData->GetStages() as $stage_type) {
16 if ($stage_type !== WizardStageTypes::HEADER_STAGE && $stage_type !== WizardStageTypes::FOOTER_STAGE) {
17 continue;
18 }
19
20 if (!isset($selection_data[$stage_type])) {
21 if ($wizardData->IsRestore()) {
22 // If the wizard is in restore mode, the template part stages may not be present in the selection data if there are no parts to restore.
23 continue;
24 }
25 throw new WizardException(esc_html__('Couldn\'t process template part selections. If the issue persists, please contact support.', 'superb-blocks'));
26 }
27
28 $template_part_data = isset($selection_data[$stage_type][0]) ? $selection_data[$stage_type][0] : [];
29 if (!boolval($template_part_data['isChanged'])) {
30 continue;
31 }
32
33 self::ValidateTemplatePartDataOrThrow($template_part_data);
34 $template_part_slug = $stage_type === WizardStageTypes::HEADER_STAGE ? 'header' : 'footer';
35 $restoration_point = WizardRestorationPointController::CreateTemplateRestorationPoint($template_part_slug, WizardItemTypes::WP_TEMPLATE_PART);
36 if (!$restoration_point) {
37 throw new WizardException(esc_html__('Template part restoration point could not be created. If the issue persists, please contact support.', 'superb-blocks'));
38 }
39 $part_created = self::CreateTemplatePart($template_part_slug, $template_part_data['slug'], $template_part_data['package'], $template_part_data['type']);
40 if (!$part_created) {
41 throw new WizardException(esc_html__('Template part could not be created. If the issue persists, please contact support.', 'superb-blocks'));
42 }
43 }
44
45 return true;
46 }
47
48 private static function ValidateTemplatePartDataOrThrow($data)
49 {
50 if (
51 !isset($data['slug'])
52 || !isset($data['package'])
53 || !isset($data['type'])
54 || empty($data['slug'])
55 || empty($data['package'])
56 || empty($data['type'])
57 ) {
58 throw new WizardException(esc_html__('Missing template part selection data. If the issue persists, please contact support.', 'superb-blocks'));
59 }
60 }
61
62 private static function CreateTemplatePart($slug, $template_id, $package, $template_type)
63 {
64 // Slug is the same as the template id, no need to create a new template part.
65 if ($template_type === WizardItemTypes::WP_TEMPLATE_PART && $slug === $template_id) {
66 return true;
67 }
68
69 // Do not create if template part does not exist in theme.
70 if (!get_block_template(get_stylesheet() . "//" . $slug, WizardItemTypes::WP_TEMPLATE_PART)) {
71 return false;
72 }
73
74 $current_template_part_post_id = WizardCreationUtil::GetTemplatePostID($slug, WizardItemTypes::WP_TEMPLATE_PART);
75
76 $template_content = false;
77 if ($template_type === WizardItemTypes::WP_TEMPLATE_PART) {
78 $is_file_template = WizardCreationUtil::IsFileTemplate($template_id, $template_type, WizardItemTypes::WP_TEMPLATE_PART);
79 $is_restoration_point = WizardCreationUtil::IsRestorationPoint($template_id, $template_type, WizardItemTypes::WP_TEMPLATE_PART);
80 if ($is_file_template) {
81 $template_content = self::GetFileTemplateContent($template_id);
82 } elseif ($is_restoration_point) {
83 $template_content = self::GetRestorationPointContent($template_id);
84 } else {
85 $template_content = self::GetTemplateContent($template_id);
86 }
87 } else {
88 $template_content = self::GetSelectedAddonsTemplateContent($template_id, $package);
89 }
90
91 if (!$template_content) {
92 return false;
93 }
94
95 if ($slug === 'header') {
96 $template_content = self::InjectCurrentHeaderNavigationId($template_content);
97 }
98
99 return $current_template_part_post_id
100 ? WizardCreationUtil::UpdateTemplatePost($current_template_part_post_id, $template_content)
101 : WizardCreationUtil::CreateNewTemplatePartPost($slug, $template_content);
102 }
103
104 private static function GetFileTemplateContent($template_id)
105 {
106 $template = get_block_file_template(get_stylesheet() . '//' . $template_id, WizardItemTypes::WP_TEMPLATE_PART);
107 if ($template->theme !== get_stylesheet() || empty($template->content)) {
108 return false;
109 }
110 return $template->content;
111 }
112
113 private static function GetTemplateContent($template_id)
114 {
115 $template = get_block_template(get_stylesheet() . '//' . $template_id, WizardItemTypes::WP_TEMPLATE_PART);
116 if ($template->theme !== get_stylesheet() || empty($template->content)) {
117 return false;
118 }
119 return $template->content;
120 }
121
122 private static function GetRestorationPointContent($template_id)
123 {
124 $restoration_point = WizardRestorationPointController::GetTemplateRestorationPoint($template_id);
125 if (!$restoration_point) {
126 return false;
127 }
128 return $restoration_point['content'];
129 }
130
131 private static function GetSelectedAddonsTemplateContent($template_id, $package)
132 {
133 $data = LibraryRequestController::GetInsertData(
134 [
135 'id' => $template_id,
136 'package' => $package,
137 ],
138 LibraryRequestController::GUTENBERG_ENDPOINT_BASE,
139 LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS
140 );
141
142 if (!$data || !isset($data['content']) || isset($data['access_failed'])) {
143 return false;
144 }
145
146 $data = GutenbergController::GutenbergDataImportAction($data);
147
148 return $data['content'];
149 }
150
151 private static function InjectCurrentHeaderNavigationId($template_content)
152 {
153 if (!has_block('core/navigation', $template_content)) {
154 return $template_content;
155 }
156
157 $navigation_id = WizardCreationUtil::GetNavigationTemplatePartMenuId();
158 if (!$navigation_id) {
159 return $template_content;
160 }
161
162 return WizardCreationUtil::UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_content, $navigation_id);
163 }
164 }
165