PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.4.2
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.4.2
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-creation-util.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-creation-util.php
200 lines
1 <?php
2
3 namespace SuperbAddons\Data\Utils\Wizard;
4
5 use WP_Block_Template;
6
7 defined('ABSPATH') || exit();
8
9 class WizardCreationUtil
10 {
11 public static function GetTemplatePostID($slug, $type = WizardItemTypes::WP_TEMPLATE_PART)
12 {
13 $current_template_part_post_id = false;
14
15 $template_part_posts = get_posts(
16 array(
17 'post_type' => $type,
18 'post_name' => $slug,
19 'posts_per_page' => -1,
20 'tax_query' => array(
21 array(
22 'taxonomy' => 'wp_theme',
23 'field' => 'slug',
24 'terms' => get_stylesheet()
25 )
26 )
27 )
28 );
29
30 if ($template_part_posts && !empty($template_part_posts)) {
31 foreach ($template_part_posts as $template_part_post) {
32 if ($template_part_post->post_name === $slug) {
33 $current_template_part_post_id = $template_part_post->ID;
34 break;
35 }
36 }
37 }
38
39 return $current_template_part_post_id;
40 }
41
42 public static function GetNavigationTemplateData($template_part_slug = 'header')
43 {
44 $navigation_template_part = get_block_template(get_stylesheet() . '//' . $template_part_slug, WizardItemTypes::WP_TEMPLATE_PART);
45 if (!$navigation_template_part || !$navigation_template_part instanceof WP_Block_Template) {
46 // Template part is not loaded correctly.
47 return false;
48 }
49
50 $template_content = $navigation_template_part->content;
51 if (!has_block('core/navigation', $template_content)) {
52 // We cannot update the navigation if no navigation block is present in the template part.
53 // Stop execution.
54 return false;
55 }
56
57 return ['post_id' => $navigation_template_part->wp_id, 'title' => $navigation_template_part->title, 'content' => $template_content];
58 }
59
60 public static function GetNavigationTemplatePartMenuId($template_part_slug = 'header')
61 {
62 $template_data = self::GetNavigationTemplateData($template_part_slug);
63 if (!$template_data) {
64 return false;
65 }
66
67 $parsed_blocks = parse_blocks($template_data['content']);
68 $flattened = _flatten_blocks($parsed_blocks);
69 foreach ($flattened as &$block) {
70 // Keep pointer reference to the navigation block.
71 if ('core/navigation' !== $block['blockName'])
72 continue;
73 // Return the navigation menu id.
74 if (!isset($block['attrs']['ref'])) {
75 return false;
76 }
77 return $block['attrs']['ref'];
78 }
79
80 return false;
81 }
82
83 public static function UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_content, $ref_id)
84 {
85 if (!$ref_id) {
86 return $template_content;
87 }
88
89 $parsed_blocks = parse_blocks($template_content);
90 $flattened = _flatten_blocks($parsed_blocks);
91 foreach ($flattened as &$block) {
92 // Keep pointer reference to the navigation block.
93 if ('core/navigation' !== $block['blockName'])
94 continue;
95
96 // Update the navigation menu id.
97 $block['attrs'] ??= [];
98 $block['attrs']['ref'] = absint($ref_id);
99
100 if (!isset($block['innerBlocks']) || !is_array($block['innerBlocks'])) {
101 break;
102 }
103
104 // Remove inner navigation links.
105 $block['innerBlocks'] = [];
106 $block['innerContent'] = [];
107
108 break;
109 }
110
111 // Serialize the blocks back to a string, keeping the updated navigation block pointer reference.
112 $new_content = serialize_blocks($parsed_blocks);
113 return $new_content;
114 }
115
116 public static function MaybeUpdateQueryLoopBlockInherit($template_content)
117 {
118 if (!has_block('core/query', $template_content)) {
119 return $template_content;
120 }
121
122 $parsed_blocks = parse_blocks($template_content);
123 $flattened = _flatten_blocks($parsed_blocks);
124 foreach ($flattened as &$block) {
125 // Keep pointer reference to the navigation block.
126 if ('core/query' !== $block['blockName'])
127 continue;
128
129 // if the query block does not have the specified class name, skip it.
130 if (!isset($block['attrs']['className']) || strpos($block['attrs']['className'], WizardSpecialBlockClassName::INHERIT_TEMPLATE_QUERY) === false) {
131 continue;
132 }
133
134 // Update the query inherit bool.
135 $block['attrs'] ??= [];
136 $block['attrs']['query'] ??= [];
137 $block['attrs']['query']['inherit'] = true;
138 }
139
140 // Serialize the blocks back to a string, keeping the updated navigation block pointer reference.
141 $new_content = serialize_blocks($parsed_blocks);
142 return $new_content;
143 }
144
145 public static function IsFileTemplate(&$template_id, $template_type, $required_type)
146 {
147 if ($template_type === $required_type) {
148 if (strpos($template_id, WizardItemIdAffix::FILE_TEMPLATE) !== false) {
149 // Remove the file template affix from the template id
150 $template_id = str_replace(WizardItemIdAffix::FILE_TEMPLATE, '', $template_id);
151 return true;
152 }
153 }
154 return false;
155 }
156
157 public static function IsRestorationPoint(&$template_id, $template_type, $required_type)
158 {
159 if ($template_type === $required_type) {
160 if (strpos($template_id, WizardItemIdAffix::RESTORATION_POINT) !== false) {
161 // Remove the file template affix from the template id
162 $template_id = str_replace(WizardItemIdAffix::RESTORATION_POINT, '', $template_id);
163 return true;
164 }
165 }
166 return false;
167 }
168
169 public static function CreateNewTemplatePartPost($slug, $content)
170 {
171 $block_template = get_block_template(get_stylesheet() . "//" . $slug, WizardItemTypes::WP_TEMPLATE_PART);
172 return wp_insert_post(
173 array(
174 'post_title' => $block_template->title,
175 'post_excerpt' => $block_template->description,
176 'post_name' => $slug,
177 'post_content' => $content,
178 'post_status' => 'publish',
179 'post_type' => WizardItemTypes::WP_TEMPLATE_PART,
180 'comment_status' => 'closed',
181 'ping_status' => 'closed',
182 'tax_input' => array(
183 'wp_theme' => get_stylesheet(),
184 'wp_template_part_area' => $slug
185 )
186 )
187 );
188 }
189
190 public static function UpdateTemplatePost($post_id, $content)
191 {
192 return wp_update_post(
193 array(
194 'ID' => $post_id,
195 'post_content' => $content
196 )
197 );
198 }
199 }
200