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-page-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-page-creator.php
346 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 use WP_Error;
9
10 defined('ABSPATH') || exit();
11
12 class WizardPageCreator
13 {
14 public static function CreateTemplatePages($selection_data, $wizardData)
15 {
16 $theme_templates = get_block_templates();
17 if (empty($theme_templates) || empty(array_column($theme_templates, 'slug'))) {
18 return rest_ensure_response(['success' => false, 'text' => esc_html__("Theme templates could not be loaded.", "superb-blocks")]);
19 }
20 $theme_template_slugs = array_column($theme_templates, 'slug');
21
22 $page_stages = self::GetPageStages($wizardData);
23
24 if (empty($page_stages)) {
25 throw new WizardException(esc_html__('Something went wrong. No page stages found in selections.', 'superb-blocks'));
26 }
27
28 $menu_items = self::CreateAndGetPageMenuItems(
29 $page_stages,
30 $selection_data,
31 $theme_template_slugs
32 );
33
34 return $menu_items;
35 }
36
37 private static function GetPageStages($wizardData)
38 {
39 $page_stages = [];
40
41 foreach ($wizardData->GetStages() as $stage_type) {
42 if (in_array($stage_type, WizardStageTypes::PAGE_STAGES)) {
43 $page_stages[] = $stage_type;
44 }
45 }
46
47 return $page_stages;
48 }
49
50 private static function CreateAndGetPageMenuItems($stage_types, $selections, $available_templates)
51 {
52 $menu_items = array();
53 foreach ($stage_types as $stage_type) {
54 if (!isset($selections[$stage_type]) || empty($selections[$stage_type])) {
55 continue;
56 }
57 $created_menu_items = self::HandlePageCreation($stage_type, $selections[$stage_type], $available_templates);
58 $menu_items = array_merge($menu_items, $created_menu_items);
59 }
60
61 return $menu_items;
62 }
63
64 private static function HandlePageCreation($type, $selections, $available_templates)
65 {
66 switch ($type) {
67 case WizardStageTypes::FRONT_PAGE_STAGE:
68 return self::HandleFrontPageCreation($selections[0], $available_templates);
69 case WizardStageTypes::BLOG_PAGE_STAGE:
70 return self::HandleBlogPageCreation($selections[0], $available_templates);
71 case WizardStageTypes::TEMPLATE_PAGE_STAGE:
72 return self::HandleTemplatePageCreation($selections, $available_templates);
73 default:
74 return [];
75 }
76 }
77
78 private static function HandleFrontPageCreation($selection, $available_templates)
79 {
80 $datatype = $selection['type'];
81 // Set basic page type for creation.
82 $selection['type'] = $datatype === WizardItemTypes::PAGE ? WizardItemTypes::PAGE : WizardItemTypes::WP_TEMPLATE;
83
84 if (!boolval($selection['isChanged'])) {
85 return [self::GetMenuItemArr(0, $selection)];
86 }
87
88 if ($datatype === 'static') {
89 return [self::GetMenuItemArr(0, $selection)];
90 }
91
92 if ($datatype === 'blog') {
93 update_option('show_on_front', 'posts');
94 return [self::GetMenuItemArr(0, $selection)];
95 }
96
97 $created_page_id = self::CreateTemplatePage($selection, $available_templates, 'front-page');
98 if (!$created_page_id) return [];
99
100 update_option('show_on_front', 'page');
101 update_option('page_on_front', $created_page_id);
102 return [self::GetMenuItemArr(0, $selection)];
103 }
104
105 private static function HandleBlogPageCreation($selection, $available_templates)
106 {
107 $datatype = $selection['type'];
108 // Set basic page type for creation.
109 $selection['type'] = $selection['type'] === WizardItemTypes::PAGE ? WizardItemTypes::PAGE : WizardItemTypes::WP_TEMPLATE;
110
111 if (!boolval($selection['isChanged']) || $datatype === 'static') {
112 $blog_page_id = get_option('page_for_posts');
113 if ($blog_page_id) {
114 return [self::GetMenuItemArr($blog_page_id, $selection)];
115 }
116 }
117
118 $created_page_id = self::CreateTemplatePage($selection, $available_templates, 'blog');
119 if (!$created_page_id) return [];
120
121 update_option('page_for_posts', $created_page_id);
122 return [self::GetMenuItemArr($created_page_id, $selection)];
123 }
124
125 private static function HandleTemplatePageCreation($selections, $available_templates)
126 {
127 $menu_items = array();
128 foreach ($selections as $selection) {
129 $created_page_id = self::CreateTemplatePage($selection, $available_templates, 'page');
130 if (!$created_page_id) {
131 continue;
132 }
133 $menu_items[] = self::GetMenuItemArr($created_page_id, $selection);
134 }
135 return $menu_items;
136 }
137
138 private static function CreateTemplatePage($selection, $available_templates, $page_type)
139 {
140 $template_content = false;
141 $template_slug = false;
142
143 if ($selection['type'] === WizardItemTypes::WP_TEMPLATE) {
144 list($template_content, $template_slug) = self::GetWPTemplateDataAndSlug($selection, $available_templates);
145 } elseif ($selection['type'] === WizardItemTypes::PAGE) {
146 list($template_content, $template_slug) = self::GetAddonsTemplateDataAndSlug($selection);
147 }
148
149 if (empty($template_content) || empty($template_slug)) {
150 return false;
151 }
152 if ($page_type === 'front-page') {
153 return self::HandleFrontPageTemplate($selection, $template_content, $template_slug, $selection['type'] === WizardItemTypes::PAGE);
154 }
155 if ($page_type === 'blog') {
156 return self::HandleBlogPageTemplate($selection, $template_content, $template_slug, $selection['type'] === WizardItemTypes::PAGE);
157 }
158
159 return self::InsertPage($selection, $template_content, $template_slug);
160 }
161
162
163 private static function GetWPTemplateDataAndSlug($selection, $available_templates)
164 {
165 $template_content = false;
166 $template_slug = false;
167
168 $is_restoration_point = WizardCreationUtil::IsRestorationPoint($selection['slug'], $selection['type'], WizardItemTypes::WP_TEMPLATE);
169 $is_file_template = WizardCreationUtil::IsFileTemplate($selection['slug'], $selection['type'], WizardItemTypes::WP_TEMPLATE);
170 $is_theme_template_page = $selection['slug'] !== "front-page" && $selection['slug'] !== "home" && $selection['slug'] !== "index";
171
172 if ($is_restoration_point) {
173 $template_slug = $selection['slug'];
174 $restoration_point = WizardRestorationPointController::GetTemplateRestorationPoint($template_slug, WizardItemTypes::WP_TEMPLATE);
175 if (!$restoration_point) {
176 return [false, false];
177 }
178 $template_content = $restoration_point['content'];
179 } else if ($is_theme_template_page) {
180 if (!in_array($selection['slug'], $available_templates)) {
181 return [false, false];
182 }
183
184 $template_slug = $selection['slug'];
185 $template_content = '<!-- wp:paragraph -->
186 <p>' . esc_html__("This is a theme template page created by Superb Addons. You can edit this page's content and its template. If the blocks you want to edit can't be found in the page content, please edit the selected template.", "superb-blocks") . '</p>
187 <!-- /wp:paragraph -->';
188 } else if ($is_file_template) {
189 $template_slug = $selection['slug'];
190 $file_template = get_block_file_template(get_stylesheet() . '//' . $template_slug, WizardItemTypes::WP_TEMPLATE);
191 if (!$file_template) {
192 return [false, false];
193 }
194 $template_content = $file_template->content;
195 } else {
196 $template_slug = $selection['slug'];
197 $block_template = get_block_template(get_stylesheet() . '//' . $template_slug, WizardItemTypes::WP_TEMPLATE);
198 if (!$block_template) {
199 return [false, false];
200 }
201 $template_content = $block_template->content;
202 }
203
204 return [$template_content, $template_slug];
205 }
206
207 private static function GetAddonsTemplateDataAndSlug($selection)
208 {
209 $data = LibraryRequestController::GetInsertData(
210 [
211 'id' => $selection['slug'],
212 'package' => $selection['package'],
213 ],
214 LibraryRequestController::GUTENBERG_ENDPOINT_BASE,
215 LibraryRequestController::GUTENBERG_ROUTE_TYPE_PAGES
216 );
217 if (!$data || !isset($data['content']) || isset($data['access_failed'])) {
218 return [false, false];
219 }
220
221 $data = GutenbergController::GutenbergDataImportAction($data);
222
223 $template_slug = AddonsPageTemplateUtil::TEMPLATE_ID;
224 $template_content = $data['content'];
225
226 return [$template_content, $template_slug];
227 }
228
229 private static function HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $template_object, $template_content, $include_addons_template_content)
230 {
231 $restoration_point = WizardRestorationPointController::CreateTemplateRestorationPoint($template_object->slug, WizardItemTypes::WP_TEMPLATE);
232 if (!$restoration_point) {
233 throw new WizardException(esc_html__('Template restoration point could not be created. If the issue persists, please contact support.', 'superb-blocks'));
234 }
235
236 $template_content = $include_addons_template_content ? AddonsPageTemplateUtil::GetAddonsPageTemplateContent($template_content) : $template_content;
237 $current_template_post_id = WizardCreationUtil::GetTemplatePostID($template_post_slug, WizardItemTypes::WP_TEMPLATE);
238 if ($current_template_post_id) {
239 wp_update_post(
240 array(
241 'ID' => $current_template_post_id,
242 'post_content' => $template_content
243 )
244 );
245 } else {
246 wp_insert_post(
247 array(
248 'post_title' => $template_object->title,
249 'post_excerpt' => $template_object->description,
250 'post_name' => $template_post_slug,
251 'post_content' => $template_content,
252 'post_status' => 'publish',
253 'post_type' => WizardItemTypes::WP_TEMPLATE,
254 'comment_status' => 'closed',
255 'ping_status' => 'closed',
256 'tax_input' => array(
257 'wp_theme' => array(get_stylesheet())
258 )
259 )
260 );
261 }
262 }
263
264 private static function HandleFrontPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
265 {
266 $front_page_template = get_block_template(get_stylesheet() . "//front-page");
267 $is_front_page_and_has_front_page_template = $front_page_template && isset($front_page_template->id);
268
269 if ($is_front_page_and_has_front_page_template) {
270 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
271 self::HandleTemplatePostRevisionCreationOrUpdate('front-page', $front_page_template, $template_content, $include_addons_template_content);
272
273 if (get_option('show_on_front') === 'page' && get_option('page_on_front')) {
274 return get_option('page_on_front');
275 }
276
277 $template_slug = false;
278
279 $template_content = '<!-- wp:paragraph -->
280 <p>' . esc_html__("This is a front page template page created by Superb Addons. You can edit this page's content and its template. If the blocks you want to edit can't be found in the page content, please edit the front page template.", "superb-blocks") . '</p>
281 <!-- /wp:paragraph -->';
282 }
283
284 return self::InsertPage($selection, $template_content, $template_slug);
285 }
286
287 private static function HandleBlogPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
288 {
289 $template_post_slug = 'home';
290 $blog_page_template = get_block_template(get_stylesheet() . "//home");
291 if (!$blog_page_template || !isset($blog_page_template->id)) {
292 $blog_page_template = get_block_template(get_stylesheet() . "//index");
293 $template_post_slug = 'index';
294 }
295 $is_blog_page_and_has_blog_page_template = $blog_page_template && isset($blog_page_template->id);
296
297 if ($is_blog_page_and_has_blog_page_template) {
298 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
299 self::HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $blog_page_template, $template_content, $include_addons_template_content);
300
301 if (get_option('show_on_front') === 'page' && get_option('page_for_posts')) {
302 return get_option('page_for_posts');
303 }
304
305 $template_slug = false;
306
307 $template_content = '<!-- wp:paragraph -->
308 <p>' . esc_html__("This is a blog page template page created by Superb Addons. You can edit this page's content and its template. If the blocks you want to edit can't be found in the page content, please edit the blog template (home or index).", "superb-blocks") . '</p>
309 <!-- /wp:paragraph -->';
310 }
311
312 return self::InsertPage($selection, $template_content, $template_slug);
313 }
314
315 private static function InsertPage($selection, $template_content, $template_slug)
316 {
317 $post_data = array(
318 'post_title' => esc_html($selection['customTitle'] ?? $selection['title']),
319 'post_content' => $template_content,
320 'post_status' => 'publish',
321 'post_type' => 'page'
322 );
323 if ($template_slug) {
324 $post_data['page_template'] = $template_slug;
325 }
326
327 $created_page_id = wp_insert_post($post_data);
328
329 if ($created_page_id instanceof WP_Error) {
330 return false;
331 }
332
333 return $created_page_id;
334 }
335
336 private static function GetMenuItemArr($id, $selection)
337 {
338 $url = $id === 0 ? get_home_url() : get_permalink($id);
339 return [
340 'id' => absint($id),
341 'title' => esc_attr($selection['customTitle'] ?? $selection['title']),
342 'url' => esc_url($url)
343 ];
344 }
345 }
346