PluginProbe
Customify / 2.3.0
Customify v2.3.0
2.10.9 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.7.1 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.0.1 1.6.5 1.7.0 1.7.1 All 77 releases
customify / features / class-Customify_Importer.php

class-Customify_Importer.php in Customify 2.3.0, at features/class-Customify_Importer.php

621 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 final class Customify_Importer_Controller {
4
5 protected $customify;
6 protected static $steps = array();
7
8 function init() {
9 add_action( 'rest_api_init', array( $this, 'register_routes' ) );
10 }
11
12 /**
13 * Register the oEmbed REST API route.
14 *
15 * @since 4.4.0
16 */
17 public function register_routes() {
18
19 register_rest_route( 'customify/1.0', 'import', array(
20 array(
21 'methods' => WP_REST_Server::CREATABLE,
22 'callback' => array( $this, 'import_step' ),
23 'args' => array(
24 'action' => array(
25 'required' => true,
26 'sanitize_callback' => 'esc_url_raw',
27 ),
28 'option_key' => array(
29 'required' => true,
30 'sanitize_callback' => 'esc_url_raw',
31 ),
32 'step_id' => array(
33 'required' => true,
34 ),
35 ),
36 ),
37 ) );
38 }
39
40 /**
41 * Callback for the API endpoint.
42 *
43 * Returns the JSON object for the post.
44 *
45 * @since 4.4.0
46 *
47 * @param WP_REST_Request $request Full data about the request.
48 *
49 */
50 public function import_step( $request ) {
51 if ( ! isset( $_POST['option_key'] ) ) {
52 wp_send_json_error( esc_html__( 'Missing option key', 'customify' ) );
53 }
54
55 $option_key = $_POST['option_key'];
56 if ( ! isset( $_POST['step_id'] ) ) {
57 wp_send_json_error( esc_html__( 'Missing step id', 'customify' ) );
58 }
59
60 $step_id = $_POST['step_id'];
61 $import_step = $this->get_customify_field_data( $option_key, $step_id );
62
63 if ( isset( $_POST['step_type'] ) && ! empty( $_POST['step_type'] ) && in_array( $_POST['step_type'], array(
64 'wp_option',
65 'recall',
66 'remote'
67 ) )
68 ) {
69 $import_step['type'] = $_POST['step_type'];
70 }
71
72 switch ( $import_step['type'] ) {
73
74 case 'wp_option' : {
75 $value = $import_step['value'];
76
77 if ( isset( $import_step['base64_encoded'] ) && $import_step['base64_encoded'] ) {
78 $value = base64_decode( $value );
79 $value = json_decode( $value, true );
80 if ( empty( $value ) ) {
81 wp_send_json_error( esc_html__( 'Wrong value, I cannot decode', 'customify' ) );
82 }
83 }
84
85 // first check if the value actually changes
86 $current_value = get_option( $step_id );
87 if ( $current_value === $value ) {
88 wp_send_json_success( esc_html__( 'This option is already here', 'customify' ) );
89 }
90
91 $updated = update_option( $step_id, $value );
92
93 if ( ! $updated ) {
94 wp_send_json_error( esc_html__( 'I can\'t import this!', 'customify' ) );
95 } else {
96 wp_send_json_success( 'awesome' );
97 }
98
99 break;
100 }
101
102 case 'widgets' : {
103
104 if ( isset( $import_step['base64_encoded'] ) && $import_step['base64_encoded'] ) {
105 $decoded = base64_decode( $import_step['value'] );
106 $decoded = json_decode( $decoded, true );
107 if ( empty( $decoded ) ) {
108 wp_send_json_error( esc_html__( 'Wrong value, I cannot decode', 'customify' ) );
109 }
110
111 $updated = $this->import_widget_data( $decoded );
112 } else {
113 $updated = $this->import_widget_data( $import_step['value'] );
114 }
115
116 if ( ! $updated ) {
117 wp_send_json_error( esc_html__( 'I can\'t import this!', 'customify' ) );
118 } else {
119 wp_send_json_success( 'awesome' );
120 }
121
122 break;
123 }
124
125 case 'xml' : {
126 if ( ! isset( $import_step['file'] ) || ! file_exists( $import_step['file'] ) ) {
127 wp_send_json_error( esc_html__( 'No file', 'customify' ) );
128 }
129 break;
130 }
131
132 case 'remote' : {
133 if ( ! isset( $import_step['discover_url'] ) ) {
134 wp_send_json_error( esc_html__( 'No url', 'customify' ) );
135 }
136 $data = wp_remote_get( $import_step['discover_url'] );
137 $data = wp_remote_retrieve_body( $data );
138
139 $data = json_decode( $data, true );
140
141 if ( $data['success'] && ! empty( $data['data'] ) ) {
142 $data = $data['data'];
143 } else {
144 wp_send_json_error( 'i don\'t evan kno\'' );
145 }
146
147 $data = $this->process_remote_data( $data );
148 break;
149 }
150
151 case 'recall' : {
152
153 if ( ! isset( $_POST['recall_type'] ) ) {
154 wp_send_json( array(
155 'success' => false,
156 'code' => 'no_type',
157 'message' => esc_html__( 'No recall type', 'customify' )
158 ) );
159 }
160
161 if ( ! isset( $_POST['recall_data'] ) ) {
162 wp_send_json( array(
163 'success' => false,
164 'code' => 'no_data',
165 'message' => esc_html__( 'No recall data', 'customify' )
166 ) );
167 }
168
169 switch ( $_POST['recall_type'] ) {
170
171 case 'post_type' : {
172 wp_send_json( $_POST );
173 $this->import_post_types( $_POST['recall_type'], $_POST['recall_data'] );
174 break;
175 }
176
177 case 'taxonomy' : {
178 $this->import_taxonomies( $_POST['recall_type'], $_POST['recall_data'] );
179 break;
180 }
181
182 case 'wp_option' : {
183 $wp_options = $_POST['recall_data'];
184 $already_imported = false;
185
186 if ( ! empty( $wp_options ) ) {
187
188 foreach ( $wp_options as $value ) {
189 // first check if the value actually changes
190 $current_value = get_option( $step_id );
191 if ( $current_value === $value ) {
192 $already_imported = true;
193 continue;
194 }
195
196 $updated = update_option( $step_id, $value );
197 }
198 }
199
200 if ( $already_imported ) {
201 wp_send_json( array(
202 'success' => false,
203 'code' => 'already_imported',
204 'message' => esc_html__( 'This is already imported', 'customify' )
205 ) );
206 }
207 wp_send_json( array(
208 'success' => true,
209 'code' => 'imported',
210 'message' => esc_html__( 'Done', 'customify' )
211 ) );
212 break;
213 }
214
215 default : {
216 wp_send_json( array(
217 'success' => false,
218 'code' => 'wrong_recall_type',
219 'message' => esc_html__( 'wrong recall type', 'customify' )
220 ) );
221 break;
222 }
223 }
224
225 wp_send_json( array(
226 'success' => false,
227 'code' => 'what',
228 'message' => esc_html__( 'I dont think i should be here', 'customify' )
229 ) );
230 break;
231 }
232
233 default : {
234 wp_send_json( array(
235 'success' => false,
236 'code' => 'wrong_type',
237 'message' => esc_html__( 'Wrong import type', 'customify' )
238 ) );
239 break;
240 }
241 }
242
243 // look for this step
244 return 'you should be here';
245 }
246
247 protected function import_post_types( $post_type, $data ) {
248 wp_send_json( $post_type );
249 $result = array();
250 if ( is_array( $data ) && isset( $data['results'] ) && ! empty( $data['results'] ) ) {
251 $result[ $post_type ] = array();
252 $post_exists = false;
253 if ( post_type_exists( $post_type ) && is_array( $data['results'] ) && ! empty( $data['results'] ) ) {
254
255 foreach ( $data['results'] as $id => $post_args ) {
256
257 $post_exists = get_page_by_title( $post_args['post_title'] );
258
259 if ( $post_exists ) {
260 continue;
261 }
262
263 $args = array_intersect_key( (array) $post_args
264 , array(
265 'post_author' => 0,
266 'post_date' => 0,
267 'post_date_gmt' => 0,
268 'post_content' => 0,
269 'post_content_filtered' => 0,
270 'post_title' => 0,
271 'post_excerpt' => 0,
272 'post_status' => 'published',
273 'post_type' => 'post',
274 'ping_status' => 'closed',
275 'post_password' => '',
276 'post_name' => '',
277 'to_ping' => '',
278 'pinged' => '',
279 'post_modified' => 0,
280 'post_modified_gmt' => 0,
281 'post_parent' => 0,
282 'menu_order' => '',
283 'post_mime_type' => '',
284 ) );
285
286 /**
287 * ID
288 * comment_count
289 * comment_status
290 * filter
291 * guid */
292
293 /**
294 * @TODO Still needed
295 * 'guid'
296 * (string) Global Unique ID for referencing the post. Default empty.
297 * 'tax_input'
298 * (array) Array of taxonomy terms keyed by their taxonomy name. Default empty.
299 * 'meta_input'
300 *
301 * All metadata
302 */
303
304 $result[ $post_type ][ $id ] = wp_insert_post( $args );
305 }
306
307
308 wp_send_json($data);
309
310 if ( ! empty( $post_exists ) ) {
311 wp_send_json( array(
312 'success' => false,
313 'code' => 'exists',
314 'message' => esc_html__( 'They are already here', 'customify' )
315 ) );
316 } elseif ( empty( $result[ $post_type ] ) ) {
317 wp_send_json( array(
318 'success' => false,
319 'code' => 'empty',
320 'message' => esc_html__( 'Nothing to import', 'customify' )
321 ) );
322 }
323 }
324 }
325 }
326
327 protected function import_taxonomies( $tax, $data ) {
328 $result = array();
329
330 if ( is_array( $data ) && isset( $data['results'] ) && ! empty( $data['results'] ) ) {
331
332 $result[ $tax ] = array();
333 $term_exists = false;
334
335 if ( taxonomy_exists( $tax ) && is_array( $data['results'] ) && ! empty( $data['results'] ) ) {
336 foreach ( $data['results'] as $id => $term_args ) {
337
338 $term_exists = term_exists( $term_args['name'], $tax );
339 if ( $term_exists !== 0 && $term_exists !== null ) {
340 continue;
341 }
342
343 $args = array_intersect_key( (array) $term_args
344 , array(
345 'description' => '',
346 'parent' => 0,
347 'slug' => '',
348 ) );
349
350 $result[ $tax ][ $id ] = wp_insert_term( $term_args['name'], $tax, $args );
351 }
352 }
353
354 wp_send_json( $result[ $tax ] );
355
356 if ( ! empty( $term_exists ) ) {
357 wp_send_json( array(
358 'success' => false,
359 'code' => 'exists',
360 'message' => esc_html__( 'They are already here', 'customify' )
361 ) );
362 } elseif ( empty( $result[ $tax ] ) ) {
363 wp_send_json( array(
364 'success' => false,
365 'code' => 'empty',
366 'message' => esc_html__( 'Nothing to import', 'customify' )
367 ) );
368 }
369 }
370
371
372 wp_send_json( $result );
373 }
374
375 protected function process_remote_data( $data ) {
376
377 if ( isset( $data['wp_options'] ) && ! empty( $data['wp_options'] ) ) {
378 $this->add_step( 'wp_options', 'wp_option', $data['wp_options'] );
379 }
380
381 if ( isset( $data['widgets'] ) && ! empty( $data['widgets'] ) ) {
382 $this->add_step( 'widgets', 'widgets', $data['widgets'] );
383 }
384
385 if ( isset( $data['widgets'] ) && ! empty( $data['widgets'] ) ) {}
386
387 // select what you can get from the export
388 if ( isset( $data['taxonomies'] ) && ! empty( $data['taxonomies'] ) ) {
389
390 foreach ( $data['taxonomies'] as $tax => $term_args ) {
391
392 if ( ! taxonomy_exists( $tax ) ) {
393 unset( $data['taxonomies'][ $tax ] );
394 continue;
395 }
396
397 $term_exists = false;
398
399 if ( ! isset( $term_args['results'] ) || empty( $term_args['results'] ) ) {
400 $this->add_step( $tax, 'taxonomy', array( 'error' => 'empty' ) );
401 continue;
402 }
403
404 foreach ( $term_args['results'] as $term_id => $term ) {
405 $term_exists = term_exists( $term['name'], $tax );
406 if ( $term_exists !== 0 && $term_exists !== null ) {
407 unset( $data['taxonomies'][ $tax ]['results'][ $term_id ] );
408 }
409 }
410
411 // now if after the check if this is still empty .. .sry for ya
412 if ( empty( $data['taxonomies'][ $tax ] ) && ! empty( $term_exists ) ) {
413 $data['taxonomies'][ $tax ] = 'already_imported';
414 }
415
416 $this->add_step( $tax, 'taxonomy', $data['taxonomies'][ $tax ] );
417 }
418 }
419
420 if ( isset( $data['post_types'] ) && ! empty( $data['post_types'] ) ) {
421 foreach ( $data['post_types'] as $post_type => $post_type_args ) {
422
423 if ( ! post_type_exists( $post_type ) || empty( $post_type_args['results'] ) ) {
424 continue;
425 }
426
427 $post_exists = false;
428
429 if ( ! isset( $post_type_args['results'] ) || empty( $post_type_args['results'] ) ) {
430 $this->add_step( $post_type, 'post_type', array( 'error' => 'empty' ) );
431 continue;
432 }
433
434 foreach ( $post_type_args['results'] as $post_id => $post ) {
435 $post_exists = get_page_by_title( $post['post_title'], OBJECT, $post_type );
436 if ( ! empty( $post_exists ) ) {
437 unset( $data['post_types'][ $post_type ]['results'][ $post_id ] );
438 }
439 }
440
441 // if still empty good by
442 if ( empty( $data['post_types'][ $post_type ] ) && ! empty( $post_exists ) ) {
443 $data['post_types'][ $post_type ] = 'already_imported';
444 }
445
446 $this->add_step( $post_type, 'post_type', $data['post_types'][ $post_type ] );
447 }
448
449 unset( $data['post_types'] );
450 }
451
452 wp_send_json_success( $this->get_steps() );
453
454 return $data;
455 }
456
457 protected function add_step( $id, $type, $data ) {
458 self::$steps[] = array(
459 'id' => $id,
460 'type' => $type,
461 'data' => $data
462 );
463 }
464
465 protected function get_steps( ) {
466 return self::$steps;
467 }
468
469 protected function get_customify_field_data( $option_key, $step_id ) {
470
471 $options = PixCustomifyPlugin()->get_options_configs();
472
473 if ( ! isset( $options[ $option_key ] ) ) {
474 wp_send_json_error( 'inexistent key' );
475 }
476
477 $option_config = $options[ $option_key ];
478
479 if ( ! isset( $options[ $option_key ]['imports'] ) ) {
480 wp_send_json_error( 'where is imports????' );
481 }
482
483 $imports = $options[ $option_key ]['imports'];
484
485 if ( isset( $imports[ $step_id ] ) ) {
486 return $imports[ $step_id ];
487 }
488
489 return false;
490 }
491
492
493 /**
494 * Parse JSON import file and load data - pass to import
495 */
496 function import_widget_data( $json_data ) {
497
498 if ( empty( $json_data ) ) {
499 return false;
500 }
501
502 //first let's remove all the widgets in the sidebars to avoid a big mess
503 $sidebars_widgets = wp_get_sidebars_widgets();
504 foreach ( $sidebars_widgets as $sidebarID => $widgets ) {
505 if ( $sidebarID != 'wp_inactive_widgets' ) {
506 $sidebars_widgets[ $sidebarID ] = array();
507 }
508 }
509 wp_set_sidebars_widgets( $sidebars_widgets );
510
511 $sidebar_data = $json_data[0];
512 $widget_data = $json_data[1];
513
514 foreach ( $sidebar_data as $title => $sidebar ) {
515 $count = count( $sidebar );
516 for ( $i = 0; $i < $count; $i ++ ) {
517 $widget = array();
518 $widget['type'] = trim( substr( $sidebar[ $i ], 0, strrpos( $sidebar[ $i ], '-' ) ) );
519 $widget['type-index'] = trim( substr( $sidebar[ $i ], strrpos( $sidebar[ $i ], '-' ) + 1 ) );
520 if ( ! isset( $widget_data[ $widget['type'] ][ $widget['type-index'] ] ) ) {
521 unset( $sidebar_data[ $title ][ $i ] );
522 }
523 }
524 $sidebar_data[ $title ] = array_values( $sidebar_data[ $title ] );
525 }
526
527 $sidebar_data = array( array_filter( $sidebar_data ), $widget_data );
528
529 if ( ! self::parse_import_data( $sidebar_data ) ) {
530 return false;
531 }
532
533 return true;
534 }
535
536 /**
537 * Import widgets
538 */
539 public static function parse_import_data( $import_array ) {
540 $sidebars_data = $import_array[0];
541 $widget_data = $import_array[1];
542
543 $current_sidebars = get_option( 'sidebars_widgets' );
544 $new_widgets = array();
545
546 foreach ( $sidebars_data as $import_sidebar => $import_widgets ) :
547 $current_sidebars[ $import_sidebar ] = array();
548 foreach ( $import_widgets as $import_widget ) :
549
550 //if the sidebar exists
551 //if ( isset( $current_sidebars[$import_sidebar] ) ) :
552 $title = trim( substr( $import_widget, 0, strrpos( $import_widget, '-' ) ) );
553 $index = trim( substr( $import_widget, strrpos( $import_widget, '-' ) + 1 ) );
554 $current_widget_data = get_option( 'widget_' . $title );
555 $new_widget_name = self::get_new_widget_name( $title, $index );
556 $new_index = trim( substr( $new_widget_name, strrpos( $new_widget_name, '-' ) + 1 ) );
557
558 if ( ! empty( $new_widgets[ $title ] ) && is_array( $new_widgets[ $title ] ) ) {
559 while ( array_key_exists( $new_index, $new_widgets[ $title ] ) ) {
560 $new_index ++;
561 }
562 }
563 $current_sidebars[ $import_sidebar ][] = $title . '-' . $new_index;
564 if ( array_key_exists( $title, $new_widgets ) ) {
565 $new_widgets[ $title ][ $new_index ] = $widget_data[ $title ][ $index ];
566 if ( ! empty( $new_widgets[ $title ]['_multiwidget'] ) ) {
567 $multiwidget = $new_widgets[ $title ]['_multiwidget'];
568 unset( $new_widgets[ $title ]['_multiwidget'] );
569 $new_widgets[ $title ]['_multiwidget'] = $multiwidget;
570 } else {
571 $new_widgets[ $title ]['_multiwidget'] = null;
572 }
573 } else {
574 $current_widget_data[ $new_index ] = $widget_data[ $title ][ $index ];
575 if ( ! empty( $current_widget_data['_multiwidget'] ) ) {
576 $current_multiwidget = $current_widget_data['_multiwidget'];
577 $new_multiwidget = $widget_data[ $title ]['_multiwidget'];
578 $multiwidget = ( $current_multiwidget != $new_multiwidget ) ? $current_multiwidget : 1;
579 unset( $current_widget_data['_multiwidget'] );
580 $current_widget_data['_multiwidget'] = $multiwidget;
581 } else {
582 $current_widget_data['_multiwidget'] = null;
583 }
584 $new_widgets[ $title ] = $current_widget_data;
585 }
586
587 //endif;
588 endforeach;
589 endforeach;
590
591 if ( isset( $new_widgets ) && isset( $current_sidebars ) ) {
592 update_option( 'sidebars_widgets', $current_sidebars );
593
594 foreach ( $new_widgets as $title => $content ) {
595 update_option( 'widget_' . $title, $content );
596 }
597
598 return true;
599 }
600
601 return false;
602 }
603
604 public static function get_new_widget_name( $widget_name, $widget_index ) {
605 $current_sidebars = get_option( 'sidebars_widgets' );
606 $all_widget_array = array();
607 foreach ( $current_sidebars as $sidebar => $widgets ) {
608 if ( ! empty( $widgets ) && is_array( $widgets ) && $sidebar != 'wp_inactive_widgets' ) {
609 foreach ( $widgets as $widget ) {
610 $all_widget_array[] = $widget;
611 }
612 }
613 }
614 while ( in_array( $widget_name . '-' . $widget_index, $all_widget_array ) ) {
615 $widget_index ++;
616 }
617 $new_widget_name = $widget_name . '-' . $widget_index;
618
619 return $new_widget_name;
620 }
621 }