PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.7.1
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.7.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-part-creator.php
superb-blocks / src / data / utils / wizard Last commit date
wizard-items 5 months ago class-wizard-constants.php 5 months ago class-wizard-creation-util.php 5 months ago class-wizard-exception.php 5 months ago class-wizard-menu-creator.php 5 months ago class-wizard-page-creator.php 5 months ago class-wizard-part-creator.php 5 months ago class-wizard-stage-util.php 5 months ago class-wizard-template-provider.php 5 months ago
class-wizard-part-creator.php
188 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 // Sanitize
35 $template_part_data['slug'] = sanitize_text_field($template_part_data['slug']);
36 $template_part_data['package'] = sanitize_text_field($template_part_data['package']);
37 $template_part_data['type'] = sanitize_text_field($template_part_data['type']);
38
39 $template_part_area = $stage_type === WizardStageTypes::HEADER_STAGE ? 'header' : 'footer';
40 // Get all block template parts
41 $specificUpdatePart = isset($template_part_data['updatedTemplatePart']) && $template_part_data['updatedTemplatePart'] !== 'all' ? sanitize_text_field($template_part_data['updatedTemplatePart']) : false;
42 $templates = get_block_templates(['area' => $template_part_area], 'wp_template_part');
43 foreach ($templates as $template) {
44 if ($specificUpdatePart && !empty($specificUpdatePart) && $template->slug !== $specificUpdatePart) {
45 // If we only want to update a specific part, skip the others.
46 continue;
47 }
48 if ($template->area !== $template_part_area) {
49 // Sometimes get_block_templates returns parts from other areas, skip those.
50 continue;
51 }
52 self::InitCreateTemplatePartProcess($template, $template_part_data, $template_part_area);
53 }
54 }
55
56 return true;
57 }
58
59 private static function InitCreateTemplatePartProcess($template, $template_part_data, $template_part_area)
60 {
61 $restoration_point = WizardRestorationPointController::CreateTemplateRestorationPoint($template->slug, WizardItemTypes::WP_TEMPLATE_PART, $template_part_area);
62 if (!$restoration_point) {
63 throw new WizardException(esc_html__('Template part restoration point could not be created. If the issue persists, please contact support.', 'superb-blocks'));
64 }
65 $part_created = self::CreateTemplatePart($template->slug, $template_part_data['slug'], $template_part_data['package'], $template_part_data['type'], $template_part_area);
66 if (!$part_created) {
67 throw new WizardException(esc_html__('Template part could not be created. If the issue persists, please contact support.', 'superb-blocks'));
68 }
69 }
70
71 private static function ValidateTemplatePartDataOrThrow($data)
72 {
73 if (
74 !isset($data['slug'])
75 || !isset($data['package'])
76 || !isset($data['type'])
77 || empty($data['slug'])
78 || empty($data['package'])
79 || empty($data['type'])
80 ) {
81 throw new WizardException(esc_html__('Missing template part selection data. If the issue persists, please contact support.', 'superb-blocks'));
82 }
83 }
84
85 private static function CreateTemplatePart($slug, $template_id, $package, $template_type, $category)
86 {
87 // Slug is the same as the template id, no need to create a new template part.
88 if ($template_type === WizardItemTypes::WP_TEMPLATE_PART && $slug === $template_id) {
89 return true;
90 }
91
92 // Do not create if template part does not exist in theme.
93 if (!get_block_template(get_stylesheet() . "//" . $slug, WizardItemTypes::WP_TEMPLATE_PART)) {
94 return false;
95 }
96
97 $current_template_part_post_id = WizardCreationUtil::GetTemplatePostID($slug, WizardItemTypes::WP_TEMPLATE_PART);
98
99 $template_content = false;
100 if ($template_type === WizardItemTypes::WP_TEMPLATE_PART) {
101 $is_file_template = WizardCreationUtil::IsFileTemplate($template_id, $template_type, WizardItemTypes::WP_TEMPLATE_PART);
102 $is_restoration_point = WizardCreationUtil::IsRestorationPoint($template_id, $template_type, WizardItemTypes::WP_TEMPLATE_PART);
103 if ($is_file_template) {
104 $template_content = self::GetFileTemplateContent($template_id);
105 } elseif ($is_restoration_point) {
106 $template_content = self::GetRestorationPointContent($template_id);
107 } else {
108 $template_content = self::GetTemplateContent($template_id);
109 }
110 } else {
111 $template_content = self::GetSelectedAddonsTemplateContent($template_id, $package);
112 }
113
114 if (!$template_content) {
115 return false;
116 }
117
118 if ($category === 'header') {
119 $template_content = self::InjectCurrentHeaderNavigationId($template_content);
120 }
121
122 return $current_template_part_post_id
123 ? WizardCreationUtil::UpdateTemplatePost($current_template_part_post_id, $template_content)
124 : WizardCreationUtil::CreateNewTemplatePartPost($slug, $template_content);
125 }
126
127 private static function GetFileTemplateContent($template_id)
128 {
129 $template = get_block_file_template(get_stylesheet() . '//' . $template_id, WizardItemTypes::WP_TEMPLATE_PART);
130 if ($template->theme !== get_stylesheet() || empty($template->content)) {
131 return false;
132 }
133 return $template->content;
134 }
135
136 private static function GetTemplateContent($template_id)
137 {
138 $template = get_block_template(get_stylesheet() . '//' . $template_id, WizardItemTypes::WP_TEMPLATE_PART);
139 if ($template->theme !== get_stylesheet() || empty($template->content)) {
140 return false;
141 }
142 return $template->content;
143 }
144
145 private static function GetRestorationPointContent($template_id)
146 {
147 $restoration_point = WizardRestorationPointController::GetTemplateRestorationPoint($template_id);
148 if (!$restoration_point) {
149 return false;
150 }
151 return $restoration_point['content'];
152 }
153
154 private static function GetSelectedAddonsTemplateContent($template_id, $package)
155 {
156 $data = LibraryRequestController::GetInsertData(
157 [
158 'id' => $template_id,
159 'package' => $package,
160 ],
161 LibraryRequestController::GUTENBERG_ENDPOINT_BASE,
162 LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS
163 );
164
165 if (!$data || !isset($data['content']) || isset($data['access_failed'])) {
166 return false;
167 }
168
169 $data = GutenbergController::GutenbergDataImportAction($data);
170
171 return $data['content'];
172 }
173
174 private static function InjectCurrentHeaderNavigationId($template_content)
175 {
176 if (!has_block('core/navigation', $template_content)) {
177 return $template_content;
178 }
179
180 $navigation_id = WizardCreationUtil::GetNavigationTemplatePartMenuId();
181 if (!$navigation_id) {
182 return $template_content;
183 }
184
185 return WizardCreationUtil::UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_content, $navigation_id);
186 }
187 }
188