PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 4.0.7
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v4.0.7
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 days ago class-wizard-constants.php 5 days ago class-wizard-creation-util.php 5 days ago class-wizard-exception.php 5 days ago class-wizard-menu-creator.php 5 days ago class-wizard-page-creator.php 5 days ago class-wizard-part-creator.php 5 days ago class-wizard-stage-util.php 5 days ago class-wizard-template-provider.php 5 days ago
class-wizard-page-creator.php
351 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 aren't in the page content, they're part of the template: open the page settings sidebar, click the template name under \"Template\", and choose \"Edit 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::GetInsertDataV2(
210 array(
211 'id' => $selection['slug'],
212 'package' => $selection['package'],
213 ),
214 LibraryRequestController::GUTENBERG_TYPE_PAGE
215 );
216 if (!$data || !isset($data['content']) || isset($data['access_failed'])) {
217 return [false, false];
218 }
219
220 $data = GutenbergController::GutenbergDataImportAction($data);
221
222 $template_slug = AddonsPageTemplateUtil::TEMPLATE_ID;
223 $template_content = $data['content'];
224
225 return [$template_content, $template_slug];
226 }
227
228 private static function HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $template_object, $template_content, $include_addons_template_content)
229 {
230 $restoration_point = WizardRestorationPointController::CreateTemplateRestorationPoint($template_object->slug, WizardItemTypes::WP_TEMPLATE);
231 if (!$restoration_point) {
232 throw new WizardException(esc_html__('Template restoration point could not be created. If the issue persists, please contact support.', 'superb-blocks'));
233 }
234
235 $template_content = $include_addons_template_content ? AddonsPageTemplateUtil::GetAddonsPageTemplateContent($template_content) : $template_content;
236 $current_template_post_id = WizardCreationUtil::GetTemplatePostID($template_post_slug, WizardItemTypes::WP_TEMPLATE);
237 if ($current_template_post_id) {
238 wp_update_post(
239 array(
240 'ID' => $current_template_post_id,
241 // wp_update_post calls wp_unslash internally; slash so real
242 // backslashes in imported block markup survive intact.
243 'post_content' => wp_slash($template_content)
244 )
245 );
246 } else {
247 wp_insert_post(
248 array(
249 'post_title' => $template_object->title,
250 'post_excerpt' => $template_object->description,
251 'post_name' => $template_post_slug,
252 // wp_insert_post calls wp_unslash internally; slash so real
253 // backslashes in imported block markup survive intact.
254 'post_content' => wp_slash($template_content),
255 'post_status' => 'publish',
256 'post_type' => WizardItemTypes::WP_TEMPLATE,
257 'comment_status' => 'closed',
258 'ping_status' => 'closed',
259 'tax_input' => array(
260 'wp_theme' => array(get_stylesheet())
261 )
262 )
263 );
264 }
265 }
266
267 private static function HandleFrontPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
268 {
269 $front_page_template = get_block_template(get_stylesheet() . "//front-page");
270 $is_front_page_and_has_front_page_template = $front_page_template && isset($front_page_template->id);
271
272 if ($is_front_page_and_has_front_page_template) {
273 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
274 self::HandleTemplatePostRevisionCreationOrUpdate('front-page', $front_page_template, $template_content, $include_addons_template_content);
275
276 if (get_option('show_on_front') === 'page' && get_option('page_on_front')) {
277 return get_option('page_on_front');
278 }
279
280 $template_slug = false;
281
282 $template_content = '<!-- wp:paragraph -->
283 <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>
284 <!-- /wp:paragraph -->';
285 }
286
287 return self::InsertPage($selection, $template_content, $template_slug);
288 }
289
290 private static function HandleBlogPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
291 {
292 $template_post_slug = 'home';
293 $blog_page_template = get_block_template(get_stylesheet() . "//home");
294 if (!$blog_page_template || !isset($blog_page_template->id)) {
295 $blog_page_template = get_block_template(get_stylesheet() . "//index");
296 $template_post_slug = 'index';
297 }
298 $is_blog_page_and_has_blog_page_template = $blog_page_template && isset($blog_page_template->id);
299
300 if ($is_blog_page_and_has_blog_page_template) {
301 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
302 self::HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $blog_page_template, $template_content, $include_addons_template_content);
303
304 if (get_option('show_on_front') === 'page' && get_option('page_for_posts')) {
305 return get_option('page_for_posts');
306 }
307
308 $template_slug = false;
309
310 $template_content = '<!-- wp:paragraph -->
311 <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>
312 <!-- /wp:paragraph -->';
313 }
314
315 return self::InsertPage($selection, $template_content, $template_slug);
316 }
317
318 private static function InsertPage($selection, $template_content, $template_slug)
319 {
320 $post_data = array(
321 'post_title' => esc_html(isset($selection['customTitle']) ? $selection['customTitle'] : $selection['title']),
322 // wp_insert_post calls wp_unslash internally; slash so real
323 // backslashes in imported block markup survive intact.
324 'post_content' => wp_slash($template_content),
325 'post_status' => 'publish',
326 'post_type' => 'page'
327 );
328 if ($template_slug) {
329 $post_data['page_template'] = $template_slug;
330 }
331
332 $created_page_id = wp_insert_post($post_data);
333
334 if ($created_page_id instanceof WP_Error) {
335 return false;
336 }
337
338 return $created_page_id;
339 }
340
341 private static function GetMenuItemArr($id, $selection)
342 {
343 $url = $id === 0 ? get_home_url() : get_permalink($id);
344 return [
345 'id' => absint($id),
346 'title' => esc_attr(isset($selection['customTitle']) ? $selection['customTitle'] : $selection['title']),
347 'url' => esc_url($url)
348 ];
349 }
350 }
351