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.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-menu-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-menu-creator.php
146 lines
1 <?php
2
3 namespace SuperbAddons\Data\Utils\Wizard;
4
5 use WP_Post;
6
7 defined('ABSPATH') || exit();
8
9 class WizardMenuCreator
10 {
11 public static function MaybeUpdateMenu($selection_data, $menu_items)
12 {
13 if (!isset($selection_data[WizardStageTypes::NAVIGATION_MENU_STAGE])) {
14 return;
15 }
16
17 $navigation_selections = $selection_data[WizardStageTypes::NAVIGATION_MENU_STAGE];
18 if (self::ShouldSkipMenu($navigation_selections)) {
19 return;
20 }
21
22 // Create navigation menu content
23 $menu_content = self::CreateMenuContent($menu_items);
24 $navigation_process = $navigation_selections[0]['slug'];
25
26 if ($navigation_process === WizardNavigationMenuOptions::CREATE_NEW_MENU) {
27 $menu_post_id = self::CreateOrUpdateMenu($menu_content);
28 } else if ($navigation_process === WizardNavigationMenuOptions::APPEND_EXISTING_MENU) {
29 $menu_post_id = self::AppendToExistingMenu($menu_content);
30 }
31
32 // Update the known navigation template parts with the new navigation menu id.
33 $header_template_parts = get_block_templates(['area' => 'header'], 'wp_template_part');
34 foreach ($header_template_parts as $template) {
35 self::UpdateNavigationTemplatePart($menu_post_id, $template->slug);
36 }
37 }
38
39 private static function ShouldSkipMenu($navigation_selections)
40 {
41 return empty($navigation_selections) || !isset($navigation_selections[0]['slug']) || $navigation_selections[0]['slug'] === WizardNavigationMenuOptions::SKIP_MENU;
42 }
43
44 private static function CreateMenuContent($menu_items)
45 {
46 $menu_content = '';
47 foreach ($menu_items as $menu_item) {
48 $menu_content .= self::CreateMenuItem($menu_item);
49 }
50 return $menu_content;
51 }
52
53 private static function CreateMenuItem($menu_item)
54 {
55 if ($menu_item['id'] === 0) {
56 return "<!-- wp:home-link {\"label\":\"{$menu_item['title']}\"} /-->";
57 }
58 return "<!-- wp:navigation-link {\"label\":\"{$menu_item['title']}\",\"type\":\"page\",\"id\":{$menu_item['id']},\"url\":\"{$menu_item['url']}\",\"kind\":\"post-type\",\"isTopLevelLink\":true} /-->";
59 }
60
61 private static function CreateOrUpdateMenu($menu_content)
62 {
63 $menu_post_id = self::GetWizardNavigationPostID();
64 if ($menu_post_id) {
65 $menu_post_id = self::UpdateExistingMenuPost($menu_post_id, $menu_content);
66 }
67
68 if (!$menu_post_id) {
69 $menu_post_id = self::CreateNewMenuPost($menu_content);
70 self::SetWizardNavigationPostID($menu_post_id);
71 }
72
73 return $menu_post_id;
74 }
75
76 private static function UpdateExistingMenuPost($menu_post_id, $menu_content, $append = false)
77 {
78 $menu_post = get_post($menu_post_id);
79 if (!$menu_post instanceof WP_Post || $menu_post->post_type !== WizardItemTypes::WP_NAVIGATION) {
80 return false;
81 }
82
83 if ($append) {
84 $menu_content = $menu_post->post_content . $menu_content;
85 }
86 WizardCreationUtil::UpdateTemplatePost($menu_post_id, $menu_content);
87
88 return $menu_post_id;
89 }
90
91 private static function CreateNewMenuPost($menu_content)
92 {
93 return wp_insert_post(
94 array(
95 'post_title' => esc_html__("Navigation - Superb Addons - Theme Designer", "superb-blocks"),
96 'post_content' => $menu_content,
97 'post_status' => 'publish',
98 'post_type' => WizardItemTypes::WP_NAVIGATION,
99 'comment_status' => 'closed',
100 'ping_status' => 'closed'
101 )
102 );
103 }
104
105 private static function AppendToExistingMenu($menu_content)
106 {
107 $menu_post_id = WizardCreationUtil::GetNavigationTemplatePartMenuId();
108 if (!$menu_post_id) {
109 return false;
110 }
111
112 self::UpdateExistingMenuPost($menu_post_id, $menu_content, true);
113 return $menu_post_id;
114 }
115
116 private static function SetWizardNavigationPostID($post_id)
117 {
118 return update_option('superbaddons_wizard_navigation_post_id', $post_id, false);
119 }
120
121 private static function GetWizardNavigationPostID()
122 {
123 return get_option('superbaddons_wizard_navigation_post_id', false);
124 }
125
126 private static function UpdateNavigationTemplatePart($navigation_id, $template_part_slug)
127 {
128 $template_data = WizardCreationUtil::GetNavigationTemplateData($template_part_slug);
129 if (!$template_data) {
130 return;
131 }
132
133 $new_content = WizardCreationUtil::UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_data['content'], $navigation_id);
134
135 // If the template part has a post id, custom changes were made to the template part.
136 if (isset($template_data['post_id']) && $template_data['post_id'] > 0) {
137 // Update the template part post.
138 WizardCreationUtil::UpdateTemplatePost($template_data['post_id'], $new_content);
139 return;
140 }
141
142 // No post id, create a new template part post with the change.
143 WizardCreationUtil::CreateNewTemplatePartPost($template_part_slug, $new_content);
144 }
145 }
146