PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.1.1
Gutenify – Visual Site Builder Blocks & Starter Templates v1.1.1
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 / inc / class-template-kits.php

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

393 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 if ( ! empty( $demo_page['custom_css'] ) ) {
115 update_post_meta( $id, 'gutenify_custom_css', gutenify_minimize_css_simple( $demo_page['custom_css'] ) );
116 }
117 }
118 }
119 }
120 update_post_meta( $kit_id, 'kit_templates', wp_json_encode( $demo_page_data ) );
121 return $demo_page_data;
122 }
123
124 public function add_template( $template, $parts = array() ) {
125 if ( ! empty( $template ) ) {
126 $template_data = get_block_templates(
127 array(
128 'slug__in' => array( $template['slug'] ),
129 )
130 );
131
132 $content = html_entity_decode( $template['content'] );
133
134 if ( ! empty( $template['has_header'] ) ) {
135 $header_slug = ! empty( $template['header_slug'] ) && ! empty( $parts[ $template['header_slug'] ] ) ? $parts[ $template['header_slug'] ]['wp_slug'] : 'header';
136 if ( ! empty( $header_slug ) ) {
137 $content = '<!-- wp:template-part {"slug":"' . $header_slug . '","theme":"' . $this->current_stylesheet . '","tagName":"header"} /-->' . $content;
138 }
139 }
140
141 if ( ! empty( $template['has_footer'] ) ) {
142 $footer_slug = ! empty( $template['footer_slug'] ) && ! empty( $parts[ $template['footer_slug'] ] ) ? $parts[ $template['footer_slug'] ]['wp_slug'] : 'footer';
143
144 if ( ! empty( $footer_slug ) ) {
145 $content = $content . '<!-- wp:template-part {"slug":"' . $footer_slug . '","theme":"' . $this->current_stylesheet . '","tagName":"footer"} /-->';
146 }
147 }
148
149 $post_data = array(
150 'post_title' => $template['title'] ? wp_strip_all_tags( $template['title'] ) : '',
151 'post_name' => $template['slug'],
152 'post_status' => 'publish',
153 'post_type' => 'wp_template',
154 'post_content' => $content,
155 'tax_input' => array(
156 'wp_theme' => $this->current_stylesheet,
157 ),
158 );
159
160 if ( ! empty( $template_data ) ) {
161 $post_data['post_title'] = $template_data[0]->title;
162 if ( null !== $template_data[0]->wp_id ) {
163 $post_data['ID'] = $template_data[0]->wp_id;
164 }
165 }
166
167 if ( ! empty( $template['wp_id'] ) && ! empty( get_post( $template['wp_id'] ) ) ) {
168 $post_data['ID'] = $template['wp_id'];
169 }
170
171 $id = wp_insert_post( wp_slash( (array) $post_data ), true );
172 if ( ! is_wp_error( $id ) ) {
173 $post = get_post( $id );
174 $template['wp_id'] = $id;
175 $template['wp_slug'] = $post->post_name;
176 $template['content'] = htmlentities( $template['content'] );
177 }
178 }
179 return $template;
180 }
181
182 public function add_templates( $items, $kit_id ) {
183 $metadata = get_post_meta( $kit_id );
184 $_parts = array();
185 if ( ! empty( $metadata['kit_parts'][0] ) ) {
186 $_parts = json_decode( $metadata['kit_parts'][0] );
187 }
188 $parts = array();
189 if ( ! empty( $_parts ) ) {
190 foreach ( $_parts as $part ) {
191 $parts[ $part->slug ] = (array) $part;
192 }
193 }
194
195 if ( ! empty( $metadata['kit_templates'][0] ) ) {
196 $db_items = json_decode( $metadata['kit_templates'][0] );
197 if ( ! empty( $db_items ) ) {
198 foreach ( $db_items as $db_item ) {
199 $db_item = (array) $db_item;
200 if ( ! empty( $items ) ) {
201 foreach ( $items as $key => $_item ) {
202 if ( $_item['slug'] === $db_item['slug'] ) {
203 $items[ $key ]['wp_id'] = $db_item['wp_id'];
204 $items[ $key ]['wp_slug'] = $db_item['wp_slug'];
205 }
206 }
207 }
208 }
209 }
210 }
211
212 if ( ! empty( $items ) ) {
213 $items_data = array();
214 foreach ( $items as $item ) {
215 $item = (array) $item;
216 $item = $this->add_template( (array) $item, (array) $parts );
217 $items_data[] = $item;
218 $local_template_parts[ $item['slug'] ] = $item;
219 }
220 update_post_meta( $kit_id, 'kit_templates', wp_json_encode( $items_data ) );
221 }
222 return $items_data;
223 }
224
225 public function add_template_part( $part, $navigations ) {
226 if ( ! empty( $part ) ) {
227 $content = html_entity_decode( $part['content'] );
228
229 if ( ! empty( $navigations ) ) {
230 foreach ( $navigations as $nav_slug => $navigation ) {
231 $content = str_replace( '[nav-ref:' . $nav_slug . ']', '"ref":' . $navigation['wp_id'], $content );
232 }
233 }
234
235 $part_data = array(
236 'post_title' => wp_strip_all_tags( $part['title'] ),
237 'post_name' => $part['slug'],
238 'post_status' => 'publish',
239 'post_type' => 'wp_template_part',
240 'post_content' => $content,
241 'tax_input' => array(
242 'wp_template_part_area' => _filter_block_template_part_area( $part['area'] ),
243 'wp_theme' => $this->current_stylesheet,
244 ),
245 );
246
247 if ( ! empty( $part['wp_id'] ) && ! empty( get_post( $part['wp_id'] ) ) ) {
248 $part_data['ID'] = $part['wp_id'];
249 }
250
251 $id = wp_insert_post( wp_slash( (array) $part_data ), true );
252 if ( ! is_wp_error( $id ) ) {
253 $post = get_post( $id );
254 $part['wp_id'] = $id;
255 $part['wp_slug'] = $post->post_name;
256 $part['content'] = htmlentities( $part['content'] );
257 }
258 return $part;
259 }
260 }
261
262 public function add_template_parts( $selected_parts, $kit_id ) {
263 $metadata = get_post_meta( $kit_id );
264
265 $_navigations = array();
266 if ( ! empty( $metadata['kit_navigations'][0] ) ) {
267 $_navigations = json_decode( $metadata['kit_navigations'][0] );
268 }
269 $navigations = array();
270 if ( ! empty( $_navigations ) ) {
271 foreach ( $_navigations as $navigation ) {
272 $navigations[ $navigation->slug ] = (array) $navigation;
273 }
274 }
275
276 $parts = $selected_parts;
277 if ( ! empty( $metadata['kit_parts'][0] ) ) {
278 $db_parts = json_decode( $metadata['kit_parts'][0] );
279 if ( ! empty( $db_parts ) ) {
280 foreach ( $db_parts as $db_part ) {
281 $db_part = (array) $db_part;
282 if ( ! empty( $parts ) ) {
283 foreach ( $parts as $key => $_part ) {
284 if ( $_part['slug'] === $db_part['slug'] ) {
285 $parts[ $key ]['wp_id'] = $db_part['wp_id'];
286 $parts[ $key ]['wp_slug'] = $db_part['wp_slug'];
287 }
288 }
289 }
290 }
291 }
292 }
293
294 $parts_data = array();
295 if ( ! empty( $parts ) ) {
296 foreach ( $parts as $part ) {
297 $part = $this->add_template_part( (array) $part, $navigations );
298 $parts_data[] = $part;
299 $local_template_parts[ $part['slug'] ] = $part;
300 }
301 }
302 update_post_meta( $kit_id, 'kit_parts', wp_json_encode( $parts_data ) );
303 return $parts_data;
304 }
305
306
307 public function add_navigation( $item, $parts = array() ) {
308 if ( ! empty( $item ) ) {
309 $item_data = get_block_templates(
310 array(
311 'slug__in' => array( $item['slug'] ),
312 )
313 );
314
315 $content = html_entity_decode( $item['content'] );
316
317 $post_data = array(
318 'post_title' => $item['title'] ? wp_strip_all_tags( $item['title'] ) : '',
319 'post_name' => $item['slug'],
320 'post_status' => 'publish',
321 'post_type' => 'wp_navigation',
322 'post_content' => $content,
323 'tax_input' => array(
324 'wp_theme' => $this->current_stylesheet,
325 ),
326 );
327
328 if ( ! empty( $item_data ) ) {
329 $post_data['post_title'] = $item_data[0]->title;
330 if ( null !== $item_data[0]->wp_id ) {
331 $post_data['ID'] = $item_data[0]->wp_id;
332 }
333 }
334
335 if ( ! empty( $item['wp_id'] ) && ! empty( get_post( $item['wp_id'] ) ) ) {
336 $post_data['ID'] = $item['wp_id'];
337 }
338
339 $id = wp_insert_post( wp_slash( (array) $post_data ), true );
340 if ( ! is_wp_error( $id ) ) {
341 $post = get_post( $id );
342 $item['wp_id'] = $id;
343 $item['wp_slug'] = $post->post_name;
344 $item['content'] = htmlentities( $item['content'] );
345 }
346 }
347 return $item;
348 }
349
350 public function add_navigations( $selected_navigations, $kit_id ) {
351 $metadata = get_post_meta( $kit_id );
352 $items = $selected_navigations;
353 if ( ! empty( $metadata['kit_navigations'][0] ) ) {
354 $db_items = json_decode( $metadata['kit_navigations'][0] );
355 if ( ! empty( $db_items ) ) {
356 foreach ( $db_items as $item ) {
357 $item = (array) $item;
358 if ( ! empty( $items ) ) {
359 foreach ( $items as $key => $_item ) {
360 if ( $_item['slug'] === $item['slug'] ) {
361 $items[ $key ]['wp_id'] = $item['wp_id'];
362 $items[ $key ]['wp_slug'] = $item['wp_slug'];
363 }
364 }
365 }
366 }
367 }
368 }
369
370 $parts_data = array();
371 if ( ! empty( $items ) ) {
372 foreach ( $items as $item ) {
373 $item = $this->add_navigation( (array) $item );
374 $items_data[] = $item;
375 }
376 }
377 update_post_meta( $kit_id, 'kit_navigations', wp_json_encode( $items_data ) );
378 return $items_data;
379 }
380
381 public function add_static_homepage( $slug, $kit_id, $demo_pages = array() ) {
382 if ( ! empty( $demo_pages ) ) {
383 foreach( $demo_pages as $page ) {
384 if ( $page['slug'] === $slug && '' !== $page['wp_id'] ) {
385 update_option( 'page_on_front', $page['wp_id'] );
386 update_option( 'show_on_front', 'page' );
387 return $page;
388 }
389 }
390 }
391 }
392 }
393