PluginProbe
Customify / 2.5.3
Customify v2.5.3
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.5.3, at features/class-Customify_Importer.php

618 lines 16.9 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 $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 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 // select what you can get from the export
386 if ( isset( $data['taxonomies'] ) && ! empty( $data['taxonomies'] ) ) {
387
388 foreach ( $data['taxonomies'] as $tax => $term_args ) {
389
390 if ( ! taxonomy_exists( $tax ) ) {
391 unset( $data['taxonomies'][ $tax ] );
392 continue;
393 }
394
395 $term_exists = false;
396
397 if ( ! isset( $term_args['results'] ) || empty( $term_args['results'] ) ) {
398 $this->add_step( $tax, 'taxonomy', array( 'error' => 'empty' ) );
399 continue;
400 }
401
402 foreach ( $term_args['results'] as $term_id => $term ) {
403 $term_exists = term_exists( $term['name'], $tax );
404 if ( $term_exists !== 0 && $term_exists !== null ) {
405 unset( $data['taxonomies'][ $tax ]['results'][ $term_id ] );
406 }
407 }
408
409 // now if after the check if this is still empty .. .sry for ya
410 if ( empty( $data['taxonomies'][ $tax ] ) && ! empty( $term_exists ) ) {
411 $data['taxonomies'][ $tax ] = 'already_imported';
412 }
413
414 $this->add_step( $tax, 'taxonomy', $data['taxonomies'][ $tax ] );
415 }
416 }
417
418 if ( isset( $data['post_types'] ) && ! empty( $data['post_types'] ) ) {
419 foreach ( $data['post_types'] as $post_type => $post_type_args ) {
420
421 if ( ! post_type_exists( $post_type ) || empty( $post_type_args['results'] ) ) {
422 continue;
423 }
424
425 $post_exists = false;
426
427 if ( ! isset( $post_type_args['results'] ) || empty( $post_type_args['results'] ) ) {
428 $this->add_step( $post_type, 'post_type', array( 'error' => 'empty' ) );
429 continue;
430 }
431
432 foreach ( $post_type_args['results'] as $post_id => $post ) {
433 $post_exists = get_page_by_title( $post['post_title'], OBJECT, $post_type );
434 if ( ! empty( $post_exists ) ) {
435 unset( $data['post_types'][ $post_type ]['results'][ $post_id ] );
436 }
437 }
438
439 // if still empty good by
440 if ( empty( $data['post_types'][ $post_type ] ) && ! empty( $post_exists ) ) {
441 $data['post_types'][ $post_type ] = 'already_imported';
442 }
443
444 $this->add_step( $post_type, 'post_type', $data['post_types'][ $post_type ] );
445 }
446
447 unset( $data['post_types'] );
448 }
449
450 wp_send_json_success( $this->get_steps() );
451
452 return $data;
453 }
454
455 protected function add_step( $id, $type, $data ) {
456 self::$steps[] = array(
457 'id' => $id,
458 'type' => $type,
459 'data' => $data
460 );
461 }
462
463 protected function get_steps( ) {
464 return self::$steps;
465 }
466
467 protected function get_customify_field_data( $option_key, $step_id ) {
468
469 $options = PixCustomifyPlugin()->get_options_configs();
470
471 if ( ! isset( $options[ $option_key ] ) ) {
472 wp_send_json_error( 'nonexistent key' );
473 }
474
475 if ( ! isset( $options[ $option_key ]['imports'] ) ) {
476 wp_send_json_error( 'where is imports????' );
477 }
478
479 $imports = $options[ $option_key ]['imports'];
480
481 if ( isset( $imports[ $step_id ] ) ) {
482 return $imports[ $step_id ];
483 }
484
485 return false;
486 }
487
488
489 /**
490 * Parse JSON import file and load data - pass to import
491 */
492 function import_widget_data( $json_data ) {
493
494 if ( empty( $json_data ) ) {
495 return false;
496 }
497
498 //first let's remove all the widgets in the sidebars to avoid a big mess
499 $sidebars_widgets = wp_get_sidebars_widgets();
500 foreach ( $sidebars_widgets as $sidebarID => $widgets ) {
501 if ( $sidebarID != 'wp_inactive_widgets' ) {
502 $sidebars_widgets[ $sidebarID ] = array();
503 }
504 }
505 wp_set_sidebars_widgets( $sidebars_widgets );
506
507 $sidebar_data = $json_data[0];
508 $widget_data = $json_data[1];
509
510 foreach ( $sidebar_data as $title => $sidebar ) {
511 $count = count( $sidebar );
512 for ( $i = 0; $i < $count; $i ++ ) {
513 $widget = array();
514 $widget['type'] = trim( substr( $sidebar[ $i ], 0, strrpos( $sidebar[ $i ], '-' ) ) );
515 $widget['type-index'] = trim( substr( $sidebar[ $i ], strrpos( $sidebar[ $i ], '-' ) + 1 ) );
516 if ( ! isset( $widget_data[ $widget['type'] ][ $widget['type-index'] ] ) ) {
517 unset( $sidebar_data[ $title ][ $i ] );
518 }
519 }
520 $sidebar_data[ $title ] = array_values( $sidebar_data[ $title ] );
521 }
522
523 $sidebar_data = array( array_filter( $sidebar_data ), $widget_data );
524
525 if ( ! self::parse_import_data( $sidebar_data ) ) {
526 return false;
527 }
528
529 return true;
530 }
531
532 /**
533 * Import widgets
534 */
535 public static function parse_import_data( $import_array ) {
536 $sidebars_data = $import_array[0];
537 $widget_data = $import_array[1];
538
539 $current_sidebars = get_option( 'sidebars_widgets' );
540 $new_widgets = array();
541
542 foreach ( $sidebars_data as $import_sidebar => $import_widgets ) :
543 $current_sidebars[ $import_sidebar ] = array();
544 foreach ( $import_widgets as $import_widget ) :
545
546 //if the sidebar exists
547 //if ( isset( $current_sidebars[$import_sidebar] ) ) :
548 $title = trim( substr( $import_widget, 0, strrpos( $import_widget, '-' ) ) );
549 $index = trim( substr( $import_widget, strrpos( $import_widget, '-' ) + 1 ) );
550 $current_widget_data = get_option( 'widget_' . $title );
551 $new_widget_name = self::get_new_widget_name( $title, $index );
552 $new_index = trim( substr( $new_widget_name, strrpos( $new_widget_name, '-' ) + 1 ) );
553
554 if ( ! empty( $new_widgets[ $title ] ) && is_array( $new_widgets[ $title ] ) ) {
555 while ( array_key_exists( $new_index, $new_widgets[ $title ] ) ) {
556 $new_index ++;
557 }
558 }
559 $current_sidebars[ $import_sidebar ][] = $title . '-' . $new_index;
560 if ( array_key_exists( $title, $new_widgets ) ) {
561 $new_widgets[ $title ][ $new_index ] = $widget_data[ $title ][ $index ];
562 if ( ! empty( $new_widgets[ $title ]['_multiwidget'] ) ) {
563 $multiwidget = $new_widgets[ $title ]['_multiwidget'];
564 unset( $new_widgets[ $title ]['_multiwidget'] );
565 $new_widgets[ $title ]['_multiwidget'] = $multiwidget;
566 } else {
567 $new_widgets[ $title ]['_multiwidget'] = null;
568 }
569 } else {
570 $current_widget_data[ $new_index ] = $widget_data[ $title ][ $index ];
571 if ( ! empty( $current_widget_data['_multiwidget'] ) ) {
572 $current_multiwidget = $current_widget_data['_multiwidget'];
573 $new_multiwidget = $widget_data[ $title ]['_multiwidget'];
574 $multiwidget = ( $current_multiwidget != $new_multiwidget ) ? $current_multiwidget : 1;
575 unset( $current_widget_data['_multiwidget'] );
576 $current_widget_data['_multiwidget'] = $multiwidget;
577 } else {
578 $current_widget_data['_multiwidget'] = null;
579 }
580 $new_widgets[ $title ] = $current_widget_data;
581 }
582
583 //endif;
584 endforeach;
585 endforeach;
586
587 if ( isset( $new_widgets ) && isset( $current_sidebars ) ) {
588 update_option( 'sidebars_widgets', $current_sidebars );
589
590 foreach ( $new_widgets as $title => $content ) {
591 update_option( 'widget_' . $title, $content );
592 }
593
594 return true;
595 }
596
597 return false;
598 }
599
600 public static function get_new_widget_name( $widget_name, $widget_index ) {
601 $current_sidebars = get_option( 'sidebars_widgets' );
602 $all_widget_array = array();
603 foreach ( $current_sidebars as $sidebar => $widgets ) {
604 if ( ! empty( $widgets ) && is_array( $widgets ) && $sidebar != 'wp_inactive_widgets' ) {
605 foreach ( $widgets as $widget ) {
606 $all_widget_array[] = $widget;
607 }
608 }
609 }
610 while ( in_array( $widget_name . '-' . $widget_index, $all_widget_array ) ) {
611 $widget_index ++;
612 }
613 $new_widget_name = $widget_name . '-' . $widget_index;
614
615 return $new_widget_name;
616 }
617 }
618