| 1 |
<?php |
| 2 |
|
| 3 |
class WPPB_LE_Import { |
| 4 |
|
| 5 |
protected $args_to_import; |
| 6 |
public $import_messages = array(); |
| 7 |
private $j = '0'; |
| 8 |
|
| 9 |
/** |
| 10 |
* this will take labels that will be imported to database. |
| 11 |
* |
| 12 |
* @param array $args_to_import labels to import. |
| 13 |
*/ |
| 14 |
function __construct( $args_to_import ) { |
| 15 |
$this->args_to_import = $args_to_import; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* this will save imported json. |
| 20 |
* |
| 21 |
* @param string $json_content imported json. |
| 22 |
*/ |
| 23 |
private function json_to_db( $json_content, $nonce ) { |
| 24 |
|
| 25 |
if( !wp_verify_nonce( $nonce, 'wppb_import_labels' ) ){ |
| 26 |
$this->import_messages[$this->j]['message'] = __( 'You are not allowed to do this!', 'profile-builder' ); |
| 27 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 28 |
$this->j++; |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
/* decode and put json to array */ |
| 33 |
$imported_array_from_json = json_decode( $json_content, true ); |
| 34 |
if ( $imported_array_from_json !== NULL ) { |
| 35 |
/* import labels to database */ |
| 36 |
foreach( $imported_array_from_json as $key => $value ) { |
| 37 |
if( $key == 'pble' && ! empty( $value ) ) { |
| 38 |
update_option( $key, $value ); |
| 39 |
} |
| 40 |
} |
| 41 |
} else { |
| 42 |
$this->import_messages[$this->j]['message'] = __( 'Uploaded file is not valid json!', 'profile-builder' ); |
| 43 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 44 |
$this->j++; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/* upload json file function */ |
| 49 |
public function upload_json_file() { |
| 50 |
if( isset( $_POST['pble-import'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_import_labels' ) ) { |
| 51 |
if( ! empty( $_FILES['pble-upload']['tmp_name'] ) ) { |
| 52 |
$json_content = file_get_contents( $_FILES['pble-upload']['tmp_name'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 53 |
$this->json_to_db( $json_content, sanitize_text_field( $_POST['wppb_nonce'] ) ); |
| 54 |
|
| 55 |
if( empty( $this->import_messages ) ) { |
| 56 |
$this->import_messages[$this->j]['message'] = __( 'Import successfully!', 'profile-builder' ) . "</p><p>" . __( 'Page will refresh in 3 seconds...', 'profile-builder' ) . '<META HTTP-EQUIV="refresh" CONTENT="3">'; |
| 57 |
$this->import_messages[$this->j]['type'] = 'updated'; |
| 58 |
$this->j++; |
| 59 |
flush_rewrite_rules( false ); |
| 60 |
} |
| 61 |
} else { |
| 62 |
$this->import_messages[$this->j]['message'] = __( 'Please select a .json file to import!', 'profile-builder' ); |
| 63 |
$this->import_messages[$this->j]['type'] = 'error'; |
| 64 |
$this->j++; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/* messages return function */ |
| 70 |
public function get_messages() { |
| 71 |
return $this->import_messages; |
| 72 |
} |
| 73 |
} |