PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 4.0.0
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v4.0.0
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 2 months ago class-wizard-constants.php 2 months ago class-wizard-creation-util.php 2 months ago class-wizard-exception.php 2 months ago class-wizard-menu-creator.php 2 months ago class-wizard-page-creator.php 2 months ago class-wizard-part-creator.php 2 months ago class-wizard-stage-util.php 2 months ago class-wizard-template-provider.php 2 months ago
class-wizard-page-creator.php
345 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 'post_content' => $template_content
242 )
243 );
244 } else {
245 wp_insert_post(
246 array(
247 'post_title' => $template_object->title,
248 'post_excerpt' => $template_object->description,
249 'post_name' => $template_post_slug,
250 'post_content' => $template_content,
251 'post_status' => 'publish',
252 'post_type' => WizardItemTypes::WP_TEMPLATE,
253 'comment_status' => 'closed',
254 'ping_status' => 'closed',
255 'tax_input' => array(
256 'wp_theme' => array(get_stylesheet())
257 )
258 )
259 );
260 }
261 }
262
263 private static function HandleFrontPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
264 {
265 $front_page_template = get_block_template(get_stylesheet() . "//front-page");
266 $is_front_page_and_has_front_page_template = $front_page_template && isset($front_page_template->id);
267
268 if ($is_front_page_and_has_front_page_template) {
269 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
270 self::HandleTemplatePostRevisionCreationOrUpdate('front-page', $front_page_template, $template_content, $include_addons_template_content);
271
272 if (get_option('show_on_front') === 'page' && get_option('page_on_front')) {
273 return get_option('page_on_front');
274 }
275
276 $template_slug = false;
277
278 $template_content = '<!-- wp:paragraph -->
279 <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>
280 <!-- /wp:paragraph -->';
281 }
282
283 return self::InsertPage($selection, $template_content, $template_slug);
284 }
285
286 private static function HandleBlogPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
287 {
288 $template_post_slug = 'home';
289 $blog_page_template = get_block_template(get_stylesheet() . "//home");
290 if (!$blog_page_template || !isset($blog_page_template->id)) {
291 $blog_page_template = get_block_template(get_stylesheet() . "//index");
292 $template_post_slug = 'index';
293 }
294 $is_blog_page_and_has_blog_page_template = $blog_page_template && isset($blog_page_template->id);
295
296 if ($is_blog_page_and_has_blog_page_template) {
297 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
298 self::HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $blog_page_template, $template_content, $include_addons_template_content);
299
300 if (get_option('show_on_front') === 'page' && get_option('page_for_posts')) {
301 return get_option('page_for_posts');
302 }
303
304 $template_slug = false;
305
306 $template_content = '<!-- wp:paragraph -->
307 <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>
308 <!-- /wp:paragraph -->';
309 }
310
311 return self::InsertPage($selection, $template_content, $template_slug);
312 }
313
314 private static function InsertPage($selection, $template_content, $template_slug)
315 {
316 $post_data = array(
317 'post_title' => esc_html(isset($selection['customTitle']) ? $selection['customTitle'] : $selection['title']),
318 'post_content' => $template_content,
319 'post_status' => 'publish',
320 'post_type' => 'page'
321 );
322 if ($template_slug) {
323 $post_data['page_template'] = $template_slug;
324 }
325
326 $created_page_id = wp_insert_post($post_data);
327
328 if ($created_page_id instanceof WP_Error) {
329 return false;
330 }
331
332 return $created_page_id;
333 }
334
335 private static function GetMenuItemArr($id, $selection)
336 {
337 $url = $id === 0 ? get_home_url() : get_permalink($id);
338 return [
339 'id' => absint($id),
340 'title' => esc_attr(isset($selection['customTitle']) ? $selection['customTitle'] : $selection['title']),
341 'url' => esc_url($url)
342 ];
343 }
344 }
345