PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.5.7
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.5.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 1 year ago class-wizard-constants.php 1 year ago class-wizard-creation-util.php 1 year ago class-wizard-exception.php 1 year ago class-wizard-menu-creator.php 1 year ago class-wizard-page-creator.php 1 year ago class-wizard-part-creator.php 1 year ago class-wizard-stage-util.php 1 year ago class-wizard-template-provider.php 1 year ago
class-wizard-page-creator.php
354 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 === 'front-page' && self::HasStaticFrontPage()) {
89 return [self::GetMenuItemArr(0, $selection)];
90 }
91
92 if ($datatype === 'static') {
93 return [self::GetMenuItemArr(0, $selection)];
94 }
95
96 if ($datatype === 'blog') {
97 update_option('show_on_front', 'posts');
98 return [self::GetMenuItemArr(0, $selection)];
99 }
100
101 $created_page_id = self::CreateTemplatePage($selection, $available_templates, 'front-page');
102 if (!$created_page_id) return [];
103
104 update_option('show_on_front', 'page');
105 update_option('page_on_front', $created_page_id);
106 return [self::GetMenuItemArr(0, $selection)];
107 }
108
109 private static function HasStaticFrontPage()
110 {
111 return get_option('show_on_front') === 'page' && get_option('page_on_front');
112 }
113
114 private static function HandleBlogPageCreation($selection, $available_templates)
115 {
116 $datatype = $selection['type'];
117 // Set basic page type for creation.
118 $selection['type'] = $selection['type'] === WizardItemTypes::PAGE ? WizardItemTypes::PAGE : WizardItemTypes::WP_TEMPLATE;
119
120 if (!boolval($selection['isChanged']) || $datatype === 'static') {
121 $blog_page_id = get_option('page_for_posts');
122 if ($blog_page_id) {
123 return [self::GetMenuItemArr($blog_page_id, $selection)];
124 }
125 }
126
127 $created_page_id = self::CreateTemplatePage($selection, $available_templates, 'blog');
128 if (!$created_page_id) return [];
129
130 update_option('page_for_posts', $created_page_id);
131 return [self::GetMenuItemArr($created_page_id, $selection)];
132 }
133
134 private static function HandleTemplatePageCreation($selections, $available_templates)
135 {
136 $menu_items = array();
137 foreach ($selections as $selection) {
138 $created_page_id = self::CreateTemplatePage($selection, $available_templates, 'page');
139 if (!$created_page_id) {
140 continue;
141 }
142 $menu_items[] = self::GetMenuItemArr($created_page_id, $selection);
143 }
144 return $menu_items;
145 }
146
147 private static function CreateTemplatePage($selection, $available_templates, $page_type)
148 {
149 $template_content = false;
150 $template_slug = false;
151
152 if ($selection['type'] === WizardItemTypes::WP_TEMPLATE) {
153 list($template_content, $template_slug) = self::GetWPTemplateDataAndSlug($selection, $available_templates);
154 } elseif ($selection['type'] === WizardItemTypes::PAGE) {
155 list($template_content, $template_slug) = self::GetAddonsTemplateDataAndSlug($selection);
156 }
157
158 if (empty($template_content) || empty($template_slug)) {
159 return false;
160 }
161 if ($page_type === 'front-page') {
162 return self::HandleFrontPageTemplate($selection, $template_content, $template_slug, $selection['type'] === WizardItemTypes::PAGE);
163 }
164 if ($page_type === 'blog') {
165 return self::HandleBlogPageTemplate($selection, $template_content, $template_slug, $selection['type'] === WizardItemTypes::PAGE);
166 }
167
168 return self::InsertPage($selection, $template_content, $template_slug);
169 }
170
171
172 private static function GetWPTemplateDataAndSlug($selection, $available_templates)
173 {
174 $template_content = false;
175 $template_slug = false;
176
177 $is_restoration_point = WizardCreationUtil::IsRestorationPoint($selection['slug'], $selection['type'], WizardItemTypes::WP_TEMPLATE);
178 $is_file_template = WizardCreationUtil::IsFileTemplate($selection['slug'], $selection['type'], WizardItemTypes::WP_TEMPLATE);
179 $is_theme_template_page = $selection['slug'] !== "front-page" && $selection['slug'] !== "home" && $selection['slug'] !== "index";
180 if ($is_theme_template_page) {
181 if (!in_array($selection['slug'], $available_templates)) {
182 return [false, false];
183 }
184
185 $template_slug = $selection['slug'];
186 $template_content = '<!-- wp:paragraph -->
187 <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>
188 <!-- /wp:paragraph -->';
189 } else if ($is_file_template) {
190 $template_slug = $selection['slug'];
191 $file_template = get_block_file_template(get_stylesheet() . '//' . $template_slug, WizardItemTypes::WP_TEMPLATE);
192 if (!$file_template) {
193 return [false, false];
194 }
195 $template_content = $file_template->content;
196 } else if ($is_restoration_point) {
197 $template_slug = $selection['slug'];
198 $restoration_point = WizardRestorationPointController::GetTemplateRestorationPoint($template_slug, WizardItemTypes::WP_TEMPLATE);
199 if (!$restoration_point) {
200 return [false, false];
201 }
202 $template_content = $restoration_point['content'];
203 } else {
204 $template_slug = $selection['slug'];
205 $block_template = get_block_template(get_stylesheet() . '//' . $template_slug, WizardItemTypes::WP_TEMPLATE);
206 if (!$block_template) {
207 return [false, false];
208 }
209 $template_content = $block_template->content;
210 }
211
212 return [$template_content, $template_slug];
213 }
214
215 private static function GetAddonsTemplateDataAndSlug($selection)
216 {
217 $data = LibraryRequestController::GetInsertData(
218 [
219 'id' => $selection['slug'],
220 'package' => $selection['package'],
221 ],
222 LibraryRequestController::GUTENBERG_ENDPOINT_BASE,
223 LibraryRequestController::GUTENBERG_ROUTE_TYPE_PAGES
224 );
225 if (!$data || !isset($data['content']) || isset($data['access_failed'])) {
226 return [false, false];
227 }
228
229 $data = GutenbergController::GutenbergDataImportAction($data);
230
231 $template_slug = AddonsPageTemplateUtil::TEMPLATE_ID;
232 $template_content = $data['content'];
233
234 return [$template_content, $template_slug];
235 }
236
237 private static function HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $template_object, $template_content, $include_addons_template_content)
238 {
239 $restoration_point = WizardRestorationPointController::CreateTemplateRestorationPoint($template_object->slug, WizardItemTypes::WP_TEMPLATE);
240 if (!$restoration_point) {
241 throw new WizardException(esc_html__('Template restoration point could not be created. If the issue persists, please contact support.', 'superb-blocks'));
242 }
243
244 $template_content = $include_addons_template_content ? AddonsPageTemplateUtil::GetAddonsPageTemplateContent($template_content) : $template_content;
245 $current_template_post_id = WizardCreationUtil::GetTemplatePostID($template_post_slug, WizardItemTypes::WP_TEMPLATE);
246 if ($current_template_post_id) {
247 wp_update_post(
248 array(
249 'ID' => $current_template_post_id,
250 'post_content' => $template_content
251 )
252 );
253 } else {
254 wp_insert_post(
255 array(
256 'post_title' => $template_object->title,
257 'post_excerpt' => $template_object->description,
258 'post_name' => $template_post_slug,
259 'post_content' => $template_content,
260 'post_status' => 'publish',
261 'post_type' => WizardItemTypes::WP_TEMPLATE,
262 'comment_status' => 'closed',
263 'ping_status' => 'closed',
264 'tax_input' => array(
265 'wp_theme' => array(get_stylesheet())
266 )
267 )
268 );
269 }
270 }
271
272 private static function HandleFrontPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
273 {
274 $front_page_template = get_block_template(get_stylesheet() . "//front-page");
275 $is_front_page_and_has_front_page_template = $front_page_template && isset($front_page_template->id);
276
277 if ($is_front_page_and_has_front_page_template) {
278 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
279 self::HandleTemplatePostRevisionCreationOrUpdate('front-page', $front_page_template, $template_content, $include_addons_template_content);
280
281 if (get_option('show_on_front') === 'page' && get_option('page_on_front')) {
282 return get_option('page_on_front');
283 }
284
285 $template_slug = false;
286
287 $template_content = '<!-- wp:paragraph -->
288 <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>
289 <!-- /wp:paragraph -->';
290 }
291
292 return self::InsertPage($selection, $template_content, $template_slug);
293 }
294
295 private static function HandleBlogPageTemplate($selection, $template_content, $template_slug, $include_addons_template_content)
296 {
297 $template_post_slug = 'home';
298 $blog_page_template = get_block_template(get_stylesheet() . "//home");
299 if (!$blog_page_template || !isset($blog_page_template->id)) {
300 $blog_page_template = get_block_template(get_stylesheet() . "//index");
301 $template_post_slug = 'index';
302 }
303 $is_blog_page_and_has_blog_page_template = $blog_page_template && isset($blog_page_template->id);
304
305 if ($is_blog_page_and_has_blog_page_template) {
306 $template_content = WizardCreationUtil::MaybeUpdateQueryLoopBlockInherit($template_content);
307 self::HandleTemplatePostRevisionCreationOrUpdate($template_post_slug, $blog_page_template, $template_content, $include_addons_template_content);
308
309 if (get_option('show_on_front') === 'page' && get_option('page_for_posts')) {
310 return get_option('page_for_posts');
311 }
312
313 $template_slug = false;
314
315 $template_content = '<!-- wp:paragraph -->
316 <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>
317 <!-- /wp:paragraph -->';
318 }
319
320 return self::InsertPage($selection, $template_content, $template_slug);
321 }
322
323 private static function InsertPage($selection, $template_content, $template_slug)
324 {
325 $post_data = array(
326 'post_title' => esc_html($selection['customTitle'] ?? $selection['title']),
327 'post_content' => $template_content,
328 'post_status' => 'publish',
329 'post_type' => 'page'
330 );
331 if ($template_slug) {
332 $post_data['page_template'] = $template_slug;
333 }
334
335 $created_page_id = wp_insert_post($post_data);
336
337 if ($created_page_id instanceof WP_Error) {
338 return false;
339 }
340
341 return $created_page_id;
342 }
343
344 private static function GetMenuItemArr($id, $selection)
345 {
346 $url = $id === 0 ? get_home_url() : get_permalink($id);
347 return [
348 'id' => absint($id),
349 'title' => esc_attr($selection['customTitle'] ?? $selection['title']),
350 'url' => esc_url($url)
351 ];
352 }
353 }
354