PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.4
Post Grid Gutenberg Blocks – PostX v5.0.4
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / classes / Importer.php

Importer.php in Post Grid Gutenberg Blocks – PostX 5.0.4, at classes/Importer.php

874 lines 29.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Importer System
5 *
6 * @package ULTP\Importer
7 * @since 4.0.0
8 */
9
10 namespace ULTP;
11
12 use ULTP\Includes\Durbin\DurbinClient;
13 use ULTP\Includes\Durbin\Xpo;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Importer class.
19 *
20 * Handles starter site import, plugin installation, and REST API endpoints for Ultimate Post.
21 *
22 * @package ULTP\Importer
23 * @since 4.0.0
24 */
25 class Importer {
26 /**
27 * Setup class.
28 *
29 * @since 4.0.0
30 * @return NULL No return value.
31 */
32 public function __construct() {
33 add_action( 'wp_ajax_install_required_plugin', array( $this, 'install_required_plugin_callback' ) );
34 add_action( 'rest_api_init', array( $this, 'get_starter_rest_endpoint_callback' ) );
35 }
36
37 /**
38 * Register REST API endpoints for starter import and related actions.
39 *
40 * @since 4.0.0
41 * @return void No return value.
42 */
43 public function get_starter_rest_endpoint_callback() {
44 register_rest_route(
45 'ultp/v3',
46 '/single_page_import/',
47 array(
48 array(
49 'methods' => 'POST',
50 'callback' => array( $this, 'single_page_import' ),
51 'permission_callback' => function () {
52 return current_user_can( 'edit_others_posts' );
53 },
54 'args' => array(),
55 ),
56 )
57 );
58 register_rest_route(
59 'ultp/v3',
60 '/deletepost_getnewsletters/',
61 array(
62 array(
63 'methods' => 'POST',
64 'callback' => array( $this, 'deletepost_getnewsletters' ),
65 'permission_callback' => function () {
66 return current_user_can( 'manage_options' );
67 },
68 'args' => array(),
69 ),
70 )
71 );
72 register_rest_route(
73 'ultp/v3',
74 '/starter_import_content/',
75 array(
76 array(
77 'methods' => 'POST',
78 'callback' => array( $this, 'starter_import_content_callback' ),
79 'permission_callback' => function () {
80 return current_user_can( 'manage_options' );
81 },
82 'args' => array(),
83 ),
84 )
85 );
86 register_rest_route(
87 'ultp/v3',
88 '/starter_dummy_post/',
89 array(
90 array(
91 'methods' => 'POST',
92 'callback' => array( $this, 'starter_dummy_post_callback' ),
93 'permission_callback' => function () {
94 return current_user_can( 'manage_options' );
95 },
96 'args' => array(),
97 ),
98 )
99 );
100 }
101
102 /**
103 * Handle plugin installation via AJAX.
104 *
105 * @since 4.0.0
106 * @return void No return value.
107 */
108 public function install_required_plugin_callback() {
109 if ( ! current_user_can( 'install_plugins' ) ) {
110 return wp_send_json_success( 'You are not allowed to install plugin' );
111 }
112 if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['wpnonce'] ) ), 'ultp-nonce' ) ) {
113 return '';
114 }
115 if ( ! current_user_can( 'install_plugins' ) ) {
116 return wp_send_json_success( 'You are not allowed to install plugin' );
117 }
118
119 if ( ! function_exists( 'get_plugins' ) ) {
120 require_once ABSPATH . 'wp-admin/includes/plugin.php';
121 }
122 if ( ! function_exists( 'plugins_api' ) ) {
123 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
124 }
125 if ( ! class_exists( 'WP_Upgrader' ) ) {
126 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
127 }
128
129 $msg = '';
130
131 $all_plugins = get_plugins();
132 $plugin = ultimate_post()->ultp_rest_sanitize_params( json_decode( stripslashes( $_POST['plugin'] ) ) );
133
134 if ( $plugin && isset( $all_plugins ) && is_array( $all_plugins ) && array_key_exists( $plugin->path, $all_plugins ) ) {
135 $activated_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
136 if ( is_array( $activated_plugins ) && in_array( $plugin->path, $activated_plugins ) ) {
137 $msg = '_already_installed_activated';
138 } else {
139 $activate = activate_plugin( $plugin->path, '', false );
140 $msg = is_wp_error( $activate ) ? '_only_activated_error' : '_only_activated_success';
141 }
142 } else {
143 $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() );
144 if ( isset( $plugin->download ) ) {
145 // Download.
146 $link = $api->download;
147 } else {
148 // From Org.
149 $api = plugins_api(
150 'plugin_information',
151 array(
152 'slug' => explode( '/', $plugin->path )[0],
153 'fields' => array(
154 'short_description' => false,
155 'sections' => false,
156 'requires' => false,
157 'rating' => false,
158 'ratings' => false,
159 'downloaded' => false,
160 'last_updated' => false,
161 'added' => false,
162 'tags' => false,
163 'compatibility' => false,
164 'homepage' => false,
165 'donate_link' => false,
166 ),
167 )
168 );
169 $link = $api->download_link;
170 }
171 $installed = $upgrader->install( $link );
172 $activate = activate_plugin( $plugin->path, '', false );
173
174 $msg = is_wp_error( $activate ) ? $link : '_download_activation_success';
175 }
176
177 if ( $msg == '_download_activation_error' || $msg == '_only_activated_error' ) {
178 wp_send_json_error( $msg );
179 } else {
180 wp_send_json_success( $msg );
181 }
182 }
183
184 /**
185 * Starter dummy post callback.
186 *
187 * @since 4.0.0
188 *
189 * @param \WP_REST_Request $server The REST request object.
190 */
191 function starter_dummy_post_callback( $server ) {
192 $post = $server->get_params();
193 $api_endpoint = isset( $post['api_endpoint'] ) ? sanitize_text_field( $post['api_endpoint'] ) : '';
194 $import_dummy = isset( $post['importDummy'] ) ? sanitize_text_field( $post['importDummy'] ) : '';
195
196 $response = wp_remote_get(
197 $api_endpoint . '/wp-json/importer/site_all_posts',
198 array(
199 'method' => 'POST',
200 'timeout' => 120,
201 'body' => array(
202 'type' => 'site_posts',
203 'license' => Xpo::get_lc_key(),
204 'ultp_ver' => ULTP_VER,
205 ),
206 )
207 );
208 if ( is_wp_error( $response ) ) {
209 return rest_ensure_response(
210 array(
211 'success' => false,
212 'response_data' => $response,
213 )
214 );
215 }
216 $response_data = json_decode( $response['body'] );
217 if ( ! $response_data->success ) {
218 return rest_ensure_response(
219 array(
220 'success' => false,
221 'returns' => $response_data,
222 )
223 );
224 }
225
226 // Dummy Post and Taxonomy creation.
227 $importable_posts = $response_data->posts;
228 $created_posts = $import_dummy != 'no' ? $this->starter_pack_dummy_post_creation( $importable_posts ) : array();
229 return rest_ensure_response(
230 array(
231 'success' => true,
232 'created_posts' => $created_posts,
233 )
234 );
235 }
236
237
238 function starter_import_content_callback( $server ) {
239 $post = $server->get_params();
240 $api_endpoint = isset( $post['api_endpoint'] ) ? sanitize_text_field( $post['api_endpoint'] ) : '';
241 $installed_plugin = isset( $post['installPlugin'] ) ? sanitize_text_field( $post['installPlugin'] ) : '';
242
243 // draft existing builder template.
244 $builder_parsed_args = array(
245 'post_type' => 'ultp_builder',
246 'post_status' => 'publish',
247 'posts_per_page' => -1,
248 );
249 $builder_posts = new \WP_Query( $builder_parsed_args );
250 if ( is_array( $builder_posts->posts ) && ! empty( $builder_posts->posts ) ) {
251 foreach ( $builder_posts->posts as $post ) {
252 wp_update_post(
253 array(
254 'ID' => $post->ID,
255 'post_status' => 'draft',
256 )
257 );
258 }
259 }
260
261 $response = wp_remote_get(
262 $api_endpoint . '/wp-json/importer/single',
263 array(
264 'method' => 'POST',
265 'timeout' => 120,
266 'body' => array(
267 'license' => Xpo::get_lc_key(),
268 'ultp_ver' => ULTP_VER,
269 ),
270 )
271 );
272 if ( is_wp_error( $response ) ) {
273 return rest_ensure_response(
274 array(
275 'success' => false,
276 'response_data' => $response,
277 )
278 );
279 }
280 $response_data = json_decode( $response['body'] );
281 if ( ! $response_data->success ) {
282 return rest_ensure_response(
283 array(
284 'success' => false,
285 'response_data' => $response_data,
286 )
287 );
288 }
289
290 // site logo handle.
291 if ( ! get_option( 'site_logo', '' ) && isset( $response_data->site_logo ) && $response_data->site_logo ) {
292 update_option( 'site_logo', $this->upload_post_cat_media( $response_data->site_logo, '', 'Site Logo' ) );
293
294 if ( isset( $response_data->dark_logo ) && $response_data->dark_logo ) {
295 $dark_logo = $this->upload_post_cat_media( $response_data->dark_logo, '', 'Site Dark Logo' );
296 update_option( 'ultp_site_dark_logo', wp_get_attachment_url( $dark_logo ) );
297 }
298 }
299
300 // Templates Insertion.
301 $inserted_meta = array();
302 $inserted_pages = array();
303 $inserted_header_footer = array();
304 $importable_pages = $response_data->content;
305
306 $exclude_post_type = is_object( $post ) ? ( $post->excludepages ?? null ) : ( $post['excludepages'] ?? null );
307 $excludepages = json_decode( ultimate_post()->ultp_rest_sanitize_params( stripslashes( $exclude_post_type ) ), true );
308
309 if ( ! empty( $importable_pages ) ) {
310 foreach ( $importable_pages as $key => $val ) {
311 $title = $val->name;
312 if ( is_array( $excludepages ) && ! in_array( $title, $excludepages ) ) {
313 $post_type = $val->type;
314 $p_content = str_replace( array( 'u0022', 'u002d' ), array( '\u0022', '\u002d' ), $val->content );
315 $p_content = str_replace( array( 'u003c', 'u003e', 'currentPostId' ), array( '<', '>', 'current_PostId' ), $p_content );
316 $post_id = wp_insert_post(
317 array(
318 'post_title' => $title,
319 'post_type' => $post_type,
320 'post_status' => 'publish',
321 'post_content' => $p_content,
322 )
323 );
324 if ( $post_id ) {
325 update_post_meta( $post_id, '__ultp_starter_pack_post', true );
326 $inserted_pages[ $post_id ] = $title;
327
328 if ( isset( $val->ultp_template ) && $val->ultp_template === 'ultp_page_template' ) {
329 update_post_meta( $post_id, '_wp_page_template', 'ultp_page_template' );
330 }
331 if ( isset( $val->home_page ) && $val->home_page == 'home_page' ) {
332 if ( get_option( 'show_on_front', true ) != 'page' ) {
333 update_option( 'show_on_front', 'page' );
334 }
335 update_option( 'page_on_front', $post_id );
336 $inserted_header_footer['home_page'] = $post_id;
337 }
338 if ( isset( $val->ultp_builder_type ) && ( $val->ultp_builder_type == 'header' || $val->ultp_builder_type == 'footer' ) ) {
339 $inserted_header_footer[] = $post_id;
340 }
341 if ( $post_type == 'ultp_builder' && $val->ultp_builder_type ) {
342 $conditions_settings = get_option( 'ultp_builder_conditions', array() );
343 $conditions = $response_data->conditions;
344 $ultp_builder_type = $val->ultp_builder_type;
345 $ultp_builder_id = $val->id;
346 $ultp_builder_conditions = $conditions->$ultp_builder_type;
347 $ultp_builder_conditions = $ultp_builder_conditions->$ultp_builder_id;
348 $conditions_settings[ $ultp_builder_type ][ $post_id ] = $ultp_builder_conditions;
349
350 update_post_meta( $post_id, '__ultp_builder_type', $val->ultp_builder_type );
351 update_option( 'ultp_builder_conditions', $conditions_settings );
352 }
353 if ( isset( $val->meta ) && is_array( $val->meta ) ) {
354 foreach ( $val->meta as $k => $v ) {
355 update_post_meta( $post_id, $k, str_replace( array( 'u0022', 'u002d' ), array( '\u0022', '\u002d' ), $v ) );
356 }
357 }
358 if ( isset( $val->_ultp_css ) ) {
359 $this->save_post_block_css( $post_id, str_replace( array( 'u0022', 'u002d' ), array( '\u0022', '\u002d' ), $val->_ultp_css ), '' );
360 }
361 $inserted_meta[ $post_id ] = $val->_ultp_css;
362 }
363 }
364 }
365 }
366
367 // Menu Creation.
368 $menuCreated = array();
369 $menu_item_parent = array();
370 $response_menu = $response_data->menu;
371 if ( ! empty( $response_menu ) && is_array( $response_menu ) ) {
372 foreach ( $response_menu as $key => $menu ) {
373 $menu_exists = wp_get_nav_menu_object( $menu->title );
374 if ( $menu_exists ) {
375 wp_delete_term( $menu_exists->term_id, $menu_exists->taxonomy );
376 }
377 $menu_id = wp_create_nav_menu( $menu->title );
378 if ( isset( $menu->items ) && $menu_id && ! empty( $menu->items ) ) {
379 foreach ( $menu->items as $key => $v ) {
380 $inserted = array_search( $v->title, $inserted_pages );
381 if ( $inserted || $v->type == 'custom' || $v->type == 'category' ) {
382 if (
383 ( $v->type == 'category' && get_term_by( 'name', $v->title, 'category' ) )
384 || $v->type != 'category'
385 ) {
386 $item = array(
387 'menu-item-title' => $v->title,
388 'menu-item-status' => 'publish',
389 );
390 $item['menu-item-object'] = $v->type;
391 $item['menu-item-type'] = $v->post_type;
392 if ( isset( $v->menu_item_parent ) && $v->menu_item_parent ) {
393 $parent_id = $this->find_parent_nav_item( wp_get_nav_menu_items( $menu_id ), $v->menu_item_parent );
394 $menu_item_parent[ $v->title ] = $parent_id;
395 $item['menu-item-parent-id'] = $parent_id;
396 }
397 if ( $v->type == 'custom' ) {
398 $item['menu-item-url'] = $v->url ? $v->url : home_url( '/' );
399 } elseif ( $v->type == 'category' ) {
400 $fetched_term = get_term_by( 'name', $v->title, 'category' );
401 if ( $fetched_term ) {
402 $item['menu-item-object-id'] = $fetched_term->term_id;
403 }
404 } else {
405 $item['menu-item-object-id'] = $inserted;
406 }
407 wp_update_nav_menu_item( $menu_id, 0, $item );
408 }
409 }
410 }
411 update_term_meta( $menu_id, '__ultp_starter_pack_term', 'postx_term' );
412 $menuCreated[ $menu_id ] = $item;
413 }
414 }
415 }
416
417 // Navigation Creation for Header Footer Builder.
418 $navCreated = array();
419 $navigation = $response_data->navigation; // returned from importer site.
420 if ( ! empty( $inserted_header_footer ) && ! empty( $navigation ) ) {
421 foreach ( $inserted_header_footer as $key => $builderID ) {
422 $builder_post = get_post( $builderID );
423 $builder_post_content = $builder_post->post_content;
424 foreach ( $navigation as $key => $v ) {
425 $site_navID = $v->id;
426 if ( strpos( $builder_post_content, 'wp:navigation' ) > -1 && ( strpos( $builder_post_content, '{"ref":' . $site_navID . ',' ) > -1 || strpos( $builder_post_content, '{"ref":' . $site_navID . '}' ) > -1 ) ) {
427
428 $current_parsed_args = array(
429 'post_type' => 'wp_navigation',
430 'post_status' => 'publish',
431 'orderby' => 'date',
432 'order' => 'ASC',
433 'posts_per_page' => -1,
434 );
435 $navigation_current_posts = new \WP_Query( $current_parsed_args );
436 $navigation_current_posts = $navigation_current_posts->posts;
437
438 // check for currently same navigation exist or not.
439 $new_navID = '';
440 if ( ! $new_navID ) {
441 $menu_exists = wp_get_nav_menu_object( $v->post_title );
442 if ( $menu_exists ) {
443 $menu_blocks = \WP_Classic_To_Block_Menu_Converter::convert( $menu_exists );
444 $new_navID = wp_insert_post(
445 array(
446 'post_content' => $menu_blocks,
447 'post_title' => $menu_exists->name,
448 'post_name' => $menu_exists->slug,
449 'post_status' => 'publish',
450 'post_type' => 'wp_navigation',
451 )
452 );
453 update_post_meta( $new_navID, '__ultp_starter_pack_post', true );
454 }
455 }
456 if ( strpos( $builder_post_content, '{"ref":' . $site_navID . ',' ) > -1 ) {
457 $builder_post_content = str_replace( '{"ref":' . $site_navID . ',', '{"ref":' . $new_navID . ',', $builder_post_content );
458 } elseif ( strpos( $builder_post_content, '{"ref":' . $site_navID . '}' ) ) {
459 $builder_post_content = str_replace( '{"ref":' . $site_navID . '}', '{"ref":' . $new_navID . '}', $builder_post_content );
460 }
461
462 wp_update_post(
463 array(
464 'ID' => $builderID,
465 'post_content' => str_replace( array( 'u0022', 'u002d' ), array( '\u0022', '\u002d' ), $builder_post_content ),
466 )
467 );
468 $navCreated[] = $new_navID;
469 }
470 }
471 }
472 }
473
474 // Other Plugin Content creation.
475 $other_plugin_content = $response_data->other_plugin_content;
476 $contact_forms = $other_plugin_content->contact_form7;
477 $mc4wp = $other_plugin_content->mc4wp;
478 $wow_optin_template_id = isset( $other_plugin_content->wow_optin->id ) ? $other_plugin_content->wow_optin->id : 0;
479
480 // contact form 7 post create.
481 if ( is_array( $contact_forms ) && ! empty( $contact_forms ) ) {
482 foreach ( $contact_forms as $post ) {
483 $post_id = wp_insert_post(
484 array(
485 'post_title' => $post->post_title,
486 'post_type' => 'wpcf7_contact_form',
487 'post_status' => 'publish',
488 'post_content' => str_replace( array( 'u002d', '<wordpress@postxkit.wpxpo.com>' ), array( '\u002d', '' ), $post->post_content ),
489 )
490 );
491 update_post_meta( $post_id, '_form', $post->_form );
492 update_post_meta( $post_id, '_hash', $post->_hash );
493 update_post_meta( $post_id, '__ultp_starter_pack_post', true );
494 }
495 }
496
497 // mailchimp post create.
498 if ( ! empty( $inserted_pages ) && ! empty( $mc4wp ) ) {
499 foreach ( $inserted_pages as $id => $p_t ) {
500 $current_post = get_post( $id );
501 $current_post_content = $current_post->post_content;
502 foreach ( $mc4wp as $key => $v ) {
503 $parsed_args = array(
504 'post_type' => 'mc4wp-form',
505 'post_status' => 'publish',
506 'posts_per_page' => -1,
507 );
508 $mc4wp_forms_data = new \WP_Query( $parsed_args );
509 $mc4wp_forms_current_posts = $mc4wp_forms_data->posts;
510
511 $site_ID = $v->id;
512 if ( strpos( $current_post_content, '[mc4wp_form id=' . $site_ID . ']' ) > -1 ) {
513 $new_ID = '';
514 // check for same mailchimp post.
515 if ( ! empty( $mc4wp_forms_current_posts ) ) {
516 foreach ( $mc4wp_forms_current_posts as $key => $val ) {
517 if ( $val->post_title == $v->post_title ) {
518 $new_ID = $val->ID;
519 }
520 }
521 }
522 if ( ! $new_ID ) {
523 $new_ID = wp_insert_post(
524 array(
525 'post_title' => $v->post_title,
526 'post_type' => 'mc4wp-form',
527 'post_status' => 'publish',
528 'post_content' => $v->post_content,
529 )
530 );
531 update_post_meta( $new_ID, '__ultp_starter_pack_post', true );
532 }
533
534 $current_post_content = str_replace( '[mc4wp_form id=' . $site_ID . ']', '[mc4wp_form id=' . $new_ID . ']', $current_post_content );
535 wp_update_post(
536 array(
537 'ID' => $id,
538 'post_content' => str_replace( array( 'u0022', 'u002d' ), array( '\u0022', '\u002d' ), $current_post_content ),
539 )
540 );
541 }
542 }
543 }
544 }
545 $wow_optin_template_import_status = 'successfully working';
546
547 $activated_plugins = $installed_plugin == "yes" ? apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) : array();
548 if ( in_array( 'optin/optin.php', $activated_plugins ) && $wow_optin_template_id && $installed_plugin == "yes" ) {
549 try {
550 if ( class_exists( '\OPTN\Includes\Db', true ) && method_exists( '\OPTN\Includes\Db', 'get_instance' ) ) {
551 $db_instance = \OPTN\Includes\Db::get_instance();
552 if ( is_object( $db_instance ) && is_callable( array( $db_instance, 'activate_recipe' ) ) ) {
553 $db_instance->activate_recipe( $wow_optin_template_id, array(
554 'disable_others' => true,
555 ) );
556 }
557 }
558 } catch ( \Throwable $e ) {
559 $wow_optin_template_import_status = 'have any problem to import Optin template.';
560 }
561 }
562 // $wow_optin_template_import_status
563 return rest_ensure_response(
564 array(
565 'success' => true,
566 'inserted_pages' => $inserted_pages,
567 'inserted_meta' => $inserted_meta,
568 'navCreated' => $navCreated,
569 'menuCreated' => $menuCreated,
570 'menu_item_parent' => $menu_item_parent,
571 'inserted_header_footer' => $inserted_header_footer,
572 'wow_optin_status' => $installed_plugin,
573 )
574 );
575 }
576
577
578 /**
579 * Find Menu Parent item
580 *
581 * @since 4.0.0
582 *
583 * @param array $menu_items Array of menu items.
584 * @param string $title Title of the parent menu item to find.
585 */
586 public function find_parent_nav_item( $menu_items, $title ) {
587 if ( $menu_items ) {
588 foreach ( $menu_items as $menu_item ) {
589 if ( $menu_item->title == $title ) {
590 return $menu_item->ID;
591 }
592 }
593 }
594 return 0;
595 }
596
597 /**
598 * Save CSS of pages
599 *
600 * Saves the provided CSS content for a specific post ID. Depending on the operation type,
601 * it can also handle deletion of the CSS.
602 *
603 * @since 4.0.0
604 *
605 * @param int $id The post ID.
606 * @param string $css The CSS content to save.
607 * @param string $type The operation type (e.g., 'delete').
608 * @throws Exception If the CSS cannot be saved or deleted.
609 */
610 public function save_post_block_css( $id, $css = '', $type = '' ) {
611 try {
612 global $wp_filesystem;
613 if ( ! $wp_filesystem ) {
614 require_once ABSPATH . 'wp-admin/includes/file.php';
615 }
616
617 $upload_dir_url = wp_upload_dir();
618 $dir = trailingslashit( $upload_dir_url['basedir'] ) . 'ultimate-post/';
619
620 $post_id = (int) $id;
621 $filename = "ultp-css-{$post_id}.css";
622 $ultp_block_css = $css;
623
624 if ( $ultp_block_css ) {
625 // Set Saving ID for Clean Cache.
626 ultimate_post()->set_setting( 'save_version', wp_rand( 1, 1000 ) );
627
628 update_post_meta( $post_id, '_ultp_active', 'yes' );
629
630 WP_Filesystem( false, $upload_dir_url['basedir'], true );
631 if ( ! $wp_filesystem->is_dir( $dir ) ) {
632 $wp_filesystem->mkdir( $dir );
633 }
634 if ( ! $wp_filesystem->put_contents( $dir . $filename, $ultp_block_css ) ) {
635 throw new Exception(__('CSS can not be saved due to permission!!!', 'ultimate-post')); //phpcs:ignore
636 }
637 update_post_meta( $post_id, '_ultp_css', $ultp_block_css );
638 return array(
639 'success' => true,
640 'message' => __( 'PostX css file has been updated.', 'ultimate-post' ),
641 );
642 }
643 if ( $type == 'delete' && file_exists( $dir . $filename ) ) {
644 wp_delete_file( $dir . $filename );
645 }
646 } catch ( Exception $e ) {
647 return array(
648 'success' => false,
649 'message' => $e->getMessage(),
650 );
651 }
652 }
653
654 /**
655 * Delete previos site import post/pages
656 *
657 * @since 4.0.0
658 *
659 * @param \WP_REST_Request $server The REST request object.
660 */
661 public function deletepost_getnewsletters( $server ) {
662 $post = $server->get_params();
663 $deletePrevious = isset( $post['deletePrevious'] ) ? sanitize_text_field( $post['deletePrevious'] ) : '';
664 $get_newsletter = isset( $post['get_newsletter'] ) ? sanitize_text_field( $post['get_newsletter'] ) : '';
665 $deleted_terms = array();
666 $deleted_posts = array();
667 if ( $deletePrevious == 'yes' ) {
668 $site_logo = get_option( 'site_logo', '' );
669 global $wpdb;
670 $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key='__ultp_starter_pack_post'" ) );
671 $terms = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM {$wpdb->termmeta} WHERE meta_key='__ultp_starter_pack_term'" ) );
672
673 if ( isset( $post_ids ) && is_array( $post_ids ) ) {
674 foreach ( $post_ids as $post_id ) {
675 if ( get_post_type( $post_id ) == 'ultp_builder' ) {
676 $conditions = get_option( 'ultp_builder_conditions', array() );
677 $builder_type = get_post_meta( $post_id, '__ultp_builder_type', true );
678 if ( isset( $conditions[ $builder_type ][ $post_id ] ) ) {
679 unset( $conditions[ $builder_type ][ $post_id ] );
680 update_option( 'ultp_builder_conditions', $conditions );
681 }
682 }
683 if ( $site_logo == $post_id ) {
684 update_option( 'site_logo', '' );
685 }
686 $deleted_posts[] = $post_id;
687 wp_delete_post( $post_id, true );
688 $this->save_post_block_css( $post_id, '', 'delete' );
689 }
690 }
691 if ( isset( $terms ) && is_array( $terms ) ) {
692 foreach ( $terms as $term_id ) {
693 $deleted_terms[] = $term_id;
694 $term = get_term( $term_id );
695 wp_delete_term( $term_id, $term->taxonomy );
696 }
697 }
698 }
699 if ( $get_newsletter == 'yes' ) {
700 DurbinClient::send( DurbinClient::ACTIVATE_ACTION );
701 }
702
703 return rest_ensure_response(
704 array(
705 'success' => true,
706 'term' => $deleted_terms,
707 'posts' => $deleted_posts,
708 )
709 );
710 }
711
712 /**
713 * Import Single Template
714 *
715 * @since 4.0.0
716 *
717 * @param \WP_REST_Request $server The REST request object.
718 */
719 public function single_page_import( $server ) {
720 $post = $server->get_params();
721 $id = isset( $post['ID'] ) ? sanitize_text_field( $post['ID'] ) : '';
722 $api_endpoint = isset( $post['api_endpoint'] ) ? sanitize_text_field( $post['api_endpoint'] ) : '';
723
724 if ( $id && $api_endpoint ) {
725 $import_single = array(
726 'id' => $id,
727 'type' => 'single',
728 'license' => Xpo::get_lc_key(),
729 'ultp_ver' => ULTP_VER,
730 );
731 $response = wp_remote_get(
732 $api_endpoint . '/wp-json/importer/single',
733 array(
734 'method' => 'POST',
735 'timeout' => 120,
736 'body' => $import_single,
737 )
738 );
739 $response_data = json_decode( $response['body'] );
740 if ( ! $response_data->success ) {
741 wp_send_json(
742 array(
743 'success' => false,
744 )
745 );
746 }
747 $content = $response_data->content[0];
748 return rest_ensure_response(
749 array(
750 'success' => true,
751 'content' => $content,
752 )
753 );
754 }
755 }
756
757 /**
758 * Create dummy posts
759 *
760 * @since 4.0.0
761 *
762 * @param array $importable_posts Array of importable post objects.
763 */
764 public function starter_pack_dummy_post_creation( $importable_posts ) {
765 $added_posts = array();
766 if ( is_array( $importable_posts ) && ! empty( $importable_posts ) ) {
767 foreach ( $importable_posts as $post ) {
768 $post_id = wp_insert_post(
769 array(
770 'post_title' => $post->post_title,
771 'post_type' => 'post',
772 'post_status' => 'publish',
773 'post_content' => str_replace( 'u002d', '\u002d', $post->post_content ),
774 )
775 );
776 $image_id = $this->upload_post_cat_media( $post->img_src, $post_id, $post->post_title );
777 set_post_thumbnail( $post_id, $image_id );
778
779 update_post_meta( $post_id, '__ultp_starter_pack_post', true );
780 if ( isset( $post->_ultp_css ) && $post->_ultp_css ) {
781 $this->save_post_block_css( $post_id, str_replace( array( 'u0022', 'u002d' ), array( '\u0022', '\u002d' ), $post->_ultp_css ), '' );
782 }
783 $post_category = $post->post_category;
784 $k = 0;
785 $cat_ids = array();
786 if ( is_array( $post_category ) && ! empty( $post_category ) ) {
787 foreach ( $post_category as $i => $cat ) {
788 $cat_id = $this->starter_pack_dummy_taxonomy_creation( $cat->name, $cat->slug, $cat->img_src, 'category' );
789 wp_set_post_terms( $post_id, $cat_id, 'category', $k == 0 ? false : true );
790 ++$k;
791 $cat_ids[] = $cat_id;
792 }
793 }
794 $post_tags = $post->post_tags;
795 $tag_ids = array();
796 $j = 0;
797 if ( is_array( $post_tags ) && ! empty( $post_tags ) ) {
798 foreach ( $post_tags as $i => $tag ) {
799 $tag_id = wp_set_post_terms( $post_id, $tag->slug, 'post_tag', $j == 0 ? false : true );
800 if ( ! is_wp_error( $tag_id ) && is_array( $tag_id ) && isset( $tag_id[0] ) ) {
801 update_term_meta( $tag_id[0], '__ultp_starter_pack_term', 'postx_term' );
802 $tag_ids[] = $tag_id[0];
803 }
804 ++$j;
805 }
806 }
807 $added_posts[ $post_id ] = array(
808 'title' => $post->post_title,
809 'cat_id' => $cat_ids,
810 'tag_ids' => $tag_ids,
811 );
812 }
813 }
814 return $added_posts;
815 }
816
817 /**
818 * Create dummy category of posts
819 *
820 * @since 4.0.0
821 *
822 * @param STRING $name The name of the taxonomy term.
823 * @param STRING $slug The slug for the taxonomy term.
824 * @param STRING $img_src (Optional) The image source URL for the taxonomy term.
825 * @param STRING $taxonomy (Optional) The taxonomy type.
826 */
827 public function starter_pack_dummy_taxonomy_creation( $name, $slug, $img_src = '', $taxonomy = '' ) {
828 $new_taxonomy = get_term_by( 'slug', $slug, $taxonomy );
829 if ( ! $new_taxonomy ) {
830 $new_term = wp_insert_term(
831 $name,
832 $taxonomy,
833 array(
834 'slug' => $slug,
835 )
836 );
837 if ( $new_term ) {
838 if ( $img_src ) {
839 $image_id = $this->upload_post_cat_media( $img_src, '', $name );
840 update_term_meta( $new_term['term_id'], 'ultp_category_image', $image_id );
841 }
842 update_term_meta( $new_term['term_id'], '__ultp_starter_pack_term', 'postx_term' );
843
844 return $new_term['term_id'];
845 }
846 } else {
847 return $new_taxonomy->term_id;
848 }
849 }
850
851 /**
852 * Upload image for post/category
853 *
854 * @since 4.0.0
855 *
856 * @param STRING $src The source URL of the image.
857 * @param INT $post_id The post ID to attach the image to.
858 * @param STRING $title The title for the image.
859 */
860 public function upload_post_cat_media( $src, $post_id, $title ) {
861 if ( ! $src ) {
862 return 0;
863 }
864
865 require_once ABSPATH . 'wp-admin/includes/media.php';
866 require_once ABSPATH . 'wp-admin/includes/file.php';
867 require_once ABSPATH . 'wp-admin/includes/image.php';
868
869 $image_id = media_sideload_image( $src, $post_id, $title, 'id' );
870 update_post_meta( $image_id, '__ultp_starter_pack_post', true );
871 return $image_id;
872 }
873 }
874