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