| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
class WPPB_ImpEx_Import { |
| 6 |
|
| 7 |
protected $args_to_import; |
| 8 |
public $import_messages = array(); |
| 9 |
private $j = '0'; |
| 10 |
|
| 11 |
/** |
| 12 |
* this will take custom options and posttypes that will be imported to database. |
| 13 |
* |
| 14 |
* @param array $args_to_import custom options and posttypes to import. |
| 15 |
*/ |
| 16 |
function __construct( $args_to_import ) { |
| 17 |
$this->args_to_import = $args_to_import; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* this will save imported json. |
| 22 |
* |
| 23 |
* @param string $json_content imported json. |
| 24 |
*/ |
| 25 |
private function json_to_db( $json_content, $nonce ) { |
| 26 |
|
| 27 |
if( !wp_verify_nonce( $nonce, 'wppb_import_setttings' ) ){ |
| 28 |
$this->import_messages[$this->j]['message'] = __( 'You are not allowed to do this!', 'profile-builder' ); |
| 29 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 30 |
$this->j++; |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
/* decode and put json to array */ |
| 35 |
$imported_array_from_json = json_decode( $json_content, true ); |
| 36 |
if ( $imported_array_from_json !== NULL ) { |
| 37 |
$imported_options = $imported_array_from_json['options']; |
| 38 |
$imported_posts = $imported_array_from_json['posts']; |
| 39 |
|
| 40 |
/* import options to database */ |
| 41 |
foreach( $imported_options as $key => $value ) { |
| 42 |
|
| 43 |
if( ! empty( $value ) && strpos( $key, 'wppb_' ) !== false ) |
| 44 |
update_option( $key, $value ); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/* import custom posts to database */ |
| 49 |
foreach( $this->args_to_import as $imported_post_type ) { |
| 50 |
|
| 51 |
/* there could be the possibility that the post type doesn't exist yet so we need to register it */ |
| 52 |
if ( !post_type_exists( $imported_post_type ) ) { |
| 53 |
register_post_type( $imported_post_type ); |
| 54 |
} |
| 55 |
|
| 56 |
$db_posts = get_posts( "post_type=$imported_post_type&posts_per_page=-1" ); |
| 57 |
if( ! empty( $imported_posts[$imported_post_type] ) ) { |
| 58 |
foreach( $imported_posts[$imported_post_type] as $imported_post ) { |
| 59 |
foreach( $db_posts as $db_post ) { |
| 60 |
if( $imported_post["post_title"] == $db_post->post_title && $imported_post["post_name"] == $db_post->post_name ) { |
| 61 |
wp_delete_post( $db_post->ID, $force_delete = true ); |
| 62 |
} |
| 63 |
} |
| 64 |
unset( $imported_post["ID"] ); |
| 65 |
$imported_post_id = wp_insert_post( $imported_post ); |
| 66 |
foreach( $imported_post["postmeta"] as $key => $value ) { |
| 67 |
foreach( $value as $value_key => $serialized_value ) { |
| 68 |
add_post_meta( $imported_post_id, $key, maybe_unserialize( $serialized_value ) ); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} else { |
| 75 |
$this->import_messages[$this->j]['message'] = __( 'Uploaded file is not valid json!', 'profile-builder' ); |
| 76 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 77 |
$this->j++; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/* upload json file function */ |
| 82 |
public function upload_json_file() { |
| 83 |
if( isset( $_POST['cozmos-import'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_import_setttings' ) ) { |
| 84 |
if( ( !is_multisite() && current_user_can( apply_filters( 'wppb_settings_import_user_capability', 'manage_options' ) ) ) || |
| 85 |
( is_multisite() && current_user_can( apply_filters( 'wppb_multi_settings_import_user_capability', 'manage_network' ) ) ) ) { |
| 86 |
if (!empty($_FILES['cozmos-upload']['tmp_name'])) { |
| 87 |
$json_content = file_get_contents($_FILES['cozmos-upload']['tmp_name']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 88 |
/* save uploaded file to server (for later versions). |
| 89 |
$target = dirname( plugin_dir_path( __FILE__ ) ) . '/upload/'; |
| 90 |
$target = $target . basename( $_FILES['cozmos-upload']['name'] ); |
| 91 |
move_uploaded_file( $_FILES['cozmos-upload']['tmp_name'], $target ); |
| 92 |
*/ |
| 93 |
$this->json_to_db( $json_content, sanitize_text_field( $_POST['wppb_nonce'] ) ); |
| 94 |
if (empty($this->pbie_import_messages)) { |
| 95 |
$this->import_messages[$this->j]['message'] = __('Import successfully!', 'profile-builder'); |
| 96 |
$this->import_messages[$this->j]['type'] = 'updated'; |
| 97 |
$this->j++; |
| 98 |
flush_rewrite_rules(false); |
| 99 |
} |
| 100 |
} else { |
| 101 |
$this->import_messages[$this->j]['message'] = __('Please select a .json file to import!', 'profile-builder'); |
| 102 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 103 |
$this->j++; |
| 104 |
} |
| 105 |
} else { |
| 106 |
$this->import_messages[$this->j]['message'] = __('You do not have the capabilities required to do this!', 'profile-builder'); |
| 107 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 108 |
$this->j++; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/* messages return function */ |
| 114 |
public function get_messages() { |
| 115 |
return $this->import_messages; |
| 116 |
} |
| 117 |
} |