PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.6.5
Gutenify – Visual Site Builder Blocks & Starter Templates v1.6.5
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / core / inc / rest-api / class-template-kits.php

class-template-kits.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.6.5, at core/inc/rest-api/class-template-kits.php

392 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Template Kits functions.
4 *
5 * @package Gutenify
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Class Gutenify_Rest
14 */
15 class Gutenify_Template_Kits {
16 public $current_stylesheet = '';
17
18 function __construct() {
19 $this->current_stylesheet = wp_get_theme()->get_stylesheet();
20 }
21
22 public function add_kit( $kit ) {
23 $response = false;
24 if ( ! empty( $kit['name'] ) ) {
25 $posts = get_posts(
26 array(
27 'post_type' => 'gutenify_kit',
28 'meta_query' => array(
29 array(
30 'key' => 'kit_name',
31 'value' => $kit['name'],
32 ),
33 ),
34 )
35 );
36 if ( empty( $posts ) ) {
37 $my_post = array(
38 'post_title' => wp_strip_all_tags( $kit['title'] ),
39 'post_status' => 'publish',
40 'post_type' => 'gutenify_kit',
41 );
42 $post_id = wp_insert_post( $my_post );
43 $local_template_parts = array();
44 if ( ! is_wp_error( $post_id ) ) {
45 update_post_meta( $post_id, 'kit_name', $kit['name'] );
46 }
47 } else {
48 $post_id = $posts[0]->ID;
49 }
50
51 if ( $post_id > 0 ) {
52 $selected_navigations = ! empty( $kit['navigations'] ) ? $kit['navigations'] : array();
53 $selected_parts = ! empty( $kit['template_parts'] ) ? $kit['template_parts'] : array();
54 $selected_templates = ! empty( $kit['templates'] ) ? $kit['templates'] : array();
55 $selected_demo_pages = ! empty( $kit['demo_pages'] ) ? $kit['demo_pages'] : array();
56 $selected_home_page = ! empty( $kit['staticHomePage'] ) ? $kit['staticHomePage'] : false;
57
58 $response['navigations'] = $this->add_navigations( $selected_navigations, $post_id );
59 $response['template_parts'] = $this->add_template_parts( $selected_parts, $post_id );
60 $response['templates'] = $this->add_templates( $selected_templates, $post_id );
61 $response['demo_pages'] = $this->add_demo_pages( $selected_demo_pages, $post_id );
62 if ( $selected_home_page ) {
63 $response['static_homepage'] = $this->add_static_homepage( $selected_home_page, $post_id, $response['demo_pages'] );
64 }
65 }
66
67 return $response;
68 }
69
70 return $response;
71 }
72
73 public function add_demo_pages( $items, $kit_id ) {
74 // Demo pages.
75 $demo_page_data = array();
76 if ( ! empty( $items ) ) {
77
78 $metadata = get_post_meta( $kit_id );
79 $_templates = array();
80 if ( ! empty( $metadata['kit_templates'][0] ) ) {
81 $_templates = json_decode( $metadata['kit_templates'][0] );
82 }
83 $templates = array();
84 if ( ! empty( $_templates ) ) {
85 foreach ( $_templates as $template ) {
86 $templates[ $template->slug ] = (array) $template;
87 }
88 }
89 foreach ( $items as $demo_page ) {
90
91 $template = ! empty( $templates[ $demo_page['page_template'] ]['wp_slug'] ) ? $templates[ $demo_page['page_template'] ]['wp_slug'] : $demo_page['page_template'];
92
93 $post_data = array(
94 'post_title' => $demo_page['title'] ? wp_strip_all_tags( $demo_page['title'] ) : '',
95 'post_name' => $demo_page['slug'],
96 'post_status' => 'publish',
97 'post_type' => ! empty( $demo_page['post_type'] ) ? $demo_page['post_type'] : 'page',
98 'post_content' => $demo_page['content'],
99 );
100
101 $id = wp_insert_post( wp_slash( (array) $post_data ), true );
102 if ( ! is_wp_error( $id ) ) {
103 $post = get_post( $id );
104 $demo_page['wp_id'] = $id;
105 $demo_page['wp_slug'] = $post->post_name;
106 $demo_page['content'] = htmlentities( $demo_page['content'] );
107
108 $demo_page_data[] = $demo_page;
109
110 if ( ! empty( $template ) ) {
111 update_post_meta( $id, '_wp_page_template', $template );
112 }
113 }
114 }
115 }
116 update_post_meta( $kit_id, 'kit_templates', wp_json_encode( $demo_page_data ) );
117 return $demo_page_data;
118 }
119
120 public function add_template( $template, $parts = array() ) {
121 if ( ! empty( $template ) ) {
122 $template_data = get_block_templates(
123 array(
124 'slug__in' => array( $template['slug'] ),
125 )
126 );
127
128 $theme = wp_get_theme();
129 $slug = $theme->template;
130 $content = html_entity_decode( $template['content'] );
131 $content = str_replace( '[current-theme-slug]', $slug, $content );
132
133 if ( ! empty( $template['has_header'] ) ) {
134 $header_slug = ! empty( $template['header_slug'] ) && ! empty( $parts[ $template['header_slug'] ] ) ? $parts[ $template['header_slug'] ]['wp_slug'] : 'header';
135 if ( ! empty( $header_slug ) ) {
136 $content = '<!-- wp:template-part {"slug":"' . $header_slug . '","theme":"' . $this->current_stylesheet . '","tagName":"header"} /-->' . $content;
137 }
138 }
139
140 if ( ! empty( $template['has_footer'] ) ) {
141 $footer_slug = ! empty( $template['footer_slug'] ) && ! empty( $parts[ $template['footer_slug'] ] ) ? $parts[ $template['footer_slug'] ]['wp_slug'] : 'footer';
142
143 if ( ! empty( $footer_slug ) ) {
144 $content = $content . '<!-- wp:template-part {"slug":"' . $footer_slug . '","theme":"' . $this->current_stylesheet . '","tagName":"footer"} /-->';
145 }
146 }
147
148 $post_data = array(
149 'post_title' => $template['title'] ? wp_strip_all_tags( $template['title'] ) : '',
150 'post_name' => $template['slug'],
151 'post_status' => 'publish',
152 'post_type' => 'wp_template',
153 'post_content' => $content,
154 'tax_input' => array(
155 'wp_theme' => $this->current_stylesheet,
156 ),
157 );
158
159 if ( ! empty( $template_data ) ) {
160 $post_data['post_title'] = $template_data[0]->title;
161 if ( null !== $template_data[0]->wp_id ) {
162 $post_data['ID'] = $template_data[0]->wp_id;
163 }
164 }
165
166 if ( ! empty( $template['wp_id'] ) && ! empty( get_post( $template['wp_id'] ) ) ) {
167 $post_data['ID'] = $template['wp_id'];
168 }
169
170 $id = wp_insert_post( wp_slash( (array) $post_data ), true );
171 if ( ! is_wp_error( $id ) ) {
172 $post = get_post( $id );
173 $template['wp_id'] = $id;
174 $template['wp_slug'] = $post->post_name;
175 $template['content'] = htmlentities( $template['content'] );
176 }
177 }
178 return $template;
179 }
180
181 public function add_templates( $items, $kit_id ) {
182 $metadata = get_post_meta( $kit_id );
183 $_parts = array();
184 if ( ! empty( $metadata['kit_parts'][0] ) ) {
185 $_parts = json_decode( $metadata['kit_parts'][0] );
186 }
187 $parts = array();
188 if ( ! empty( $_parts ) ) {
189 foreach ( $_parts as $part ) {
190 $parts[ $part->slug ] = (array) $part;
191 }
192 }
193
194 if ( ! empty( $metadata['kit_templates'][0] ) ) {
195 $db_items = json_decode( $metadata['kit_templates'][0] );
196 if ( ! empty( $db_items ) ) {
197 foreach ( $db_items as $db_item ) {
198 $db_item = (array) $db_item;
199 if ( ! empty( $items ) ) {
200 foreach ( $items as $key => $_item ) {
201 if ( $_item['slug'] === $db_item['slug'] ) {
202 $items[ $key ]['wp_id'] = $db_item['wp_id'];
203 $items[ $key ]['wp_slug'] = $db_item['wp_slug'];
204 }
205 }
206 }
207 }
208 }
209 }
210
211 if ( ! empty( $items ) ) {
212 $items_data = array();
213 foreach ( $items as $item ) {
214 $item = (array) $item;
215 $item = $this->add_template( (array) $item, (array) $parts );
216 $items_data[] = $item;
217 $local_template_parts[ $item['slug'] ] = $item;
218 }
219 update_post_meta( $kit_id, 'kit_templates', wp_json_encode( $items_data ) );
220 }
221 return $items_data;
222 }
223
224 public function add_template_part( $part, $navigations ) {
225 if ( ! empty( $part ) ) {
226 $content = html_entity_decode( $part['content'] );
227
228 if ( ! empty( $navigations ) ) {
229 foreach ( $navigations as $nav_slug => $navigation ) {
230 $content = str_replace( '[nav-ref:' . $nav_slug . ']', '"ref":' . $navigation['wp_id'], $content );
231 }
232 }
233
234 $part_data = array(
235 'post_title' => wp_strip_all_tags( $part['title'] ),
236 'post_name' => $part['slug'],
237 'post_status' => 'publish',
238 'post_type' => 'wp_template_part',
239 'post_content' => $content,
240 'tax_input' => array(
241 'wp_template_part_area' => _filter_block_template_part_area( $part['area'] ),
242 'wp_theme' => $this->current_stylesheet,
243 ),
244 );
245
246 if ( ! empty( $part['wp_id'] ) && ! empty( get_post( $part['wp_id'] ) ) ) {
247 $part_data['ID'] = $part['wp_id'];
248 }
249
250 $id = wp_insert_post( wp_slash( (array) $part_data ), true );
251 if ( ! is_wp_error( $id ) ) {
252 $post = get_post( $id );
253 $part['wp_id'] = $id;
254 $part['wp_slug'] = $post->post_name;
255 $part['content'] = htmlentities( $part['content'] );
256 }
257 return $part;
258 }
259 }
260
261 public function add_template_parts( $selected_parts, $kit_id ) {
262 $metadata = get_post_meta( $kit_id );
263
264 $_navigations = array();
265 if ( ! empty( $metadata['kit_navigations'][0] ) ) {
266 $_navigations = json_decode( $metadata['kit_navigations'][0] );
267 }
268 $navigations = array();
269 if ( ! empty( $_navigations ) ) {
270 foreach ( $_navigations as $navigation ) {
271 $navigations[ $navigation->slug ] = (array) $navigation;
272 }
273 }
274
275 $parts = $selected_parts;
276 if ( ! empty( $metadata['kit_parts'][0] ) ) {
277 $db_parts = json_decode( $metadata['kit_parts'][0] );
278 if ( ! empty( $db_parts ) ) {
279 foreach ( $db_parts as $db_part ) {
280 $db_part = (array) $db_part;
281 if ( ! empty( $parts ) ) {
282 foreach ( $parts as $key => $_part ) {
283 if ( $_part['slug'] === $db_part['slug'] ) {
284 $parts[ $key ]['wp_id'] = $db_part['wp_id'];
285 $parts[ $key ]['wp_slug'] = $db_part['wp_slug'];
286 }
287 }
288 }
289 }
290 }
291 }
292
293 $parts_data = array();
294 if ( ! empty( $parts ) ) {
295 foreach ( $parts as $part ) {
296 $part = $this->add_template_part( (array) $part, $navigations );
297 $parts_data[] = $part;
298 $local_template_parts[ $part['slug'] ] = $part;
299 }
300 }
301 update_post_meta( $kit_id, 'kit_parts', wp_json_encode( $parts_data ) );
302 return $parts_data;
303 }
304
305
306 public function add_navigation( $item, $parts = array() ) {
307 if ( ! empty( $item ) ) {
308 $item_data = get_block_templates(
309 array(
310 'slug__in' => array( $item['slug'] ),
311 )
312 );
313
314 $content = html_entity_decode( $item['content'] );
315
316 $post_data = array(
317 'post_title' => $item['title'] ? wp_strip_all_tags( $item['title'] ) : '',
318 'post_name' => $item['slug'],
319 'post_status' => 'publish',
320 'post_type' => 'wp_navigation',
321 'post_content' => $content,
322 'tax_input' => array(
323 'wp_theme' => $this->current_stylesheet,
324 ),
325 );
326
327 if ( ! empty( $item_data ) ) {
328 $post_data['post_title'] = $item_data[0]->title;
329 if ( null !== $item_data[0]->wp_id ) {
330 $post_data['ID'] = $item_data[0]->wp_id;
331 }
332 }
333
334 if ( ! empty( $item['wp_id'] ) && ! empty( get_post( $item['wp_id'] ) ) ) {
335 $post_data['ID'] = $item['wp_id'];
336 }
337
338 $id = wp_insert_post( wp_slash( (array) $post_data ), true );
339 if ( ! is_wp_error( $id ) ) {
340 $post = get_post( $id );
341 $item['wp_id'] = $id;
342 $item['wp_slug'] = $post->post_name;
343 $item['content'] = htmlentities( $item['content'] );
344 }
345 }
346 return $item;
347 }
348
349 public function add_navigations( $selected_navigations, $kit_id ) {
350 $metadata = get_post_meta( $kit_id );
351 $items = $selected_navigations;
352 if ( ! empty( $metadata['kit_navigations'][0] ) ) {
353 $db_items = json_decode( $metadata['kit_navigations'][0] );
354 if ( ! empty( $db_items ) ) {
355 foreach ( $db_items as $item ) {
356 $item = (array) $item;
357 if ( ! empty( $items ) ) {
358 foreach ( $items as $key => $_item ) {
359 if ( $_item['slug'] === $item['slug'] ) {
360 $items[ $key ]['wp_id'] = $item['wp_id'];
361 $items[ $key ]['wp_slug'] = $item['wp_slug'];
362 }
363 }
364 }
365 }
366 }
367 }
368
369 $parts_data = array();
370 if ( ! empty( $items ) ) {
371 foreach ( $items as $item ) {
372 $item = $this->add_navigation( (array) $item );
373 $items_data[] = $item;
374 }
375 }
376 update_post_meta( $kit_id, 'kit_navigations', wp_json_encode( $items_data ) );
377 return $items_data;
378 }
379
380 public function add_static_homepage( $slug, $kit_id, $demo_pages = array() ) {
381 if ( ! empty( $demo_pages ) ) {
382 foreach( $demo_pages as $page ) {
383 if ( $page['slug'] === $slug && '' !== $page['wp_id'] ) {
384 update_option( 'page_on_front', $page['wp_id'] );
385 update_option( 'show_on_front', 'page' );
386 return $page;
387 }
388 }
389 }
390 }
391 }
392