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-creation-util.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-creation-util.php
226 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 // Ensure we only get template parts for the current theme -> Tax query is used like in WP core
21 // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
22 'tax_query' => array(
23 array(
24 'taxonomy' => 'wp_theme',
25 'field' => 'name',
26 'terms' => get_stylesheet()
27 )
28 )
29 )
30 );
31
32 if ($template_part_posts && !empty($template_part_posts)) {
33 foreach ($template_part_posts as $template_part_post) {
34 if ($template_part_post->post_name === $slug) {
35 $current_template_part_post_id = $template_part_post->ID;
36 break;
37 }
38 }
39 }
40
41 return $current_template_part_post_id;
42 }
43
44 public static function GetNavigationTemplateData($template_part_slug = 'header')
45 {
46 $navigation_template_part = get_block_template(get_stylesheet() . '//' . $template_part_slug, WizardItemTypes::WP_TEMPLATE_PART);
47 if (!$navigation_template_part || !$navigation_template_part instanceof WP_Block_Template) {
48 // Template part is not loaded correctly.
49 return false;
50 }
51
52 $template_content = $navigation_template_part->content;
53 if (!has_block('core/navigation', $template_content)) {
54 // We cannot update the navigation if no navigation block is present in the template part.
55 // Stop execution.
56 return false;
57 }
58
59 return ['post_id' => $navigation_template_part->wp_id, 'title' => $navigation_template_part->title, 'content' => $template_content];
60 }
61
62 public static function HasNavigationTemplatePart()
63 {
64 $header_template_parts = get_block_templates(['area' => 'header'], 'wp_template_part');
65 if (empty($header_template_parts)) {
66 return false;
67 }
68 foreach ($header_template_parts as $template_part) {
69 $template_data = self::GetNavigationTemplateData($template_part->slug);
70 if ($template_data) {
71 return true;
72 }
73 }
74 return false;
75 }
76
77 public static function GetNavigationTemplatePartMenuId()
78 {
79 $header_template_parts = get_block_templates(['area' => 'header'], 'wp_template_part');
80 if (empty($header_template_parts)) {
81 return false;
82 }
83
84 foreach ($header_template_parts as $template_part) {
85 $template_data = self::GetNavigationTemplateData($template_part->slug);
86 if (!$template_data) {
87 continue;
88 }
89
90 $parsed_blocks = parse_blocks($template_data['content']);
91 $flattened = _flatten_blocks($parsed_blocks);
92 foreach ($flattened as &$block) {
93 // Keep pointer reference to the navigation block.
94 if ('core/navigation' !== $block['blockName'])
95 continue;
96 // Return the navigation menu id.
97 if (isset($block['attrs']['ref'])) {
98 return absint($block['attrs']['ref']);
99 }
100 }
101 }
102
103 return false;
104 }
105
106 public static function UpdateNavigationBlockContentRefAndRemoveInnerLinks($template_content, $ref_id)
107 {
108 if (!$ref_id) {
109 return $template_content;
110 }
111
112 $parsed_blocks = parse_blocks($template_content);
113 $flattened = _flatten_blocks($parsed_blocks);
114 foreach ($flattened as &$block) {
115 // Keep pointer reference to the navigation block.
116 if ('core/navigation' !== $block['blockName'])
117 continue;
118
119 // Update the navigation menu id.
120 $block['attrs'] = $block['attrs'] ?? [];
121 $block['attrs']['ref'] = absint($ref_id);
122
123 if (!isset($block['innerBlocks']) || !is_array($block['innerBlocks'])) {
124 break;
125 }
126
127 // Remove inner navigation links.
128 $block['innerBlocks'] = [];
129 $block['innerContent'] = [];
130
131 break;
132 }
133
134 // Serialize the blocks back to a string, keeping the updated navigation block pointer reference.
135 $new_content = serialize_blocks($parsed_blocks);
136 return $new_content;
137 }
138
139 public static function MaybeUpdateQueryLoopBlockInherit($template_content)
140 {
141 if (!has_block('core/query', $template_content)) {
142 return $template_content;
143 }
144
145 $parsed_blocks = parse_blocks($template_content);
146 $flattened = _flatten_blocks($parsed_blocks);
147 foreach ($flattened as &$block) {
148 // Keep pointer reference to the navigation block.
149 if ('core/query' !== $block['blockName'])
150 continue;
151
152 // if the query block does not have the specified class name, skip it.
153 if (!isset($block['attrs']['className']) || strpos($block['attrs']['className'], WizardSpecialBlockClassName::INHERIT_TEMPLATE_QUERY) === false) {
154 continue;
155 }
156
157 // Update the query inherit bool.
158 $block['attrs'] = $block['attrs'] ?? [];
159 $block['attrs']['query'] = $block['attrs']['query'] ?? [];
160 $block['attrs']['query']['inherit'] = true;
161 }
162
163 // Serialize the blocks back to a string, keeping the updated navigation block pointer reference.
164 $new_content = serialize_blocks($parsed_blocks);
165 return $new_content;
166 }
167
168 public static function IsFileTemplate(&$template_id, $template_type, $required_type)
169 {
170 if ($template_type === $required_type) {
171 if (strpos($template_id, WizardItemIdAffix::FILE_TEMPLATE) !== false) {
172 // Remove the file template affix from the template id
173 $template_id = str_replace(WizardItemIdAffix::FILE_TEMPLATE, '', $template_id);
174 return true;
175 }
176 }
177 return false;
178 }
179
180 public static function IsRestorationPoint(&$template_id, $template_type, $required_type)
181 {
182 if ($template_type === $required_type) {
183 if (strpos($template_id, WizardItemIdAffix::RESTORATION_POINT) !== false) {
184 // Remove the file template affix from the template id
185 $template_id = str_replace(WizardItemIdAffix::RESTORATION_POINT, '', $template_id);
186 return true;
187 }
188 }
189 return false;
190 }
191
192 public static function CreateNewTemplatePartPost($slug, $content)
193 {
194 $block_template = get_block_template(get_stylesheet() . "//" . $slug, WizardItemTypes::WP_TEMPLATE_PART);
195 if (!$block_template || !$block_template instanceof WP_Block_Template) {
196 return false;
197 }
198 return wp_insert_post(
199 array(
200 'post_title' => $block_template->title,
201 'post_excerpt' => $block_template->description,
202 'post_name' => $slug,
203 'post_content' => $content,
204 'post_status' => 'publish',
205 'post_type' => WizardItemTypes::WP_TEMPLATE_PART,
206 'comment_status' => 'closed',
207 'ping_status' => 'closed',
208 'tax_input' => array(
209 'wp_theme' => array(get_stylesheet()),
210 'wp_template_part_area' => array($block_template->area),
211 )
212 )
213 );
214 }
215
216 public static function UpdateTemplatePost($post_id, $content)
217 {
218 return wp_update_post(
219 array(
220 'ID' => $post_id,
221 'post_content' => $content
222 )
223 );
224 }
225 }
226