| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
class WPPB_ImpEx_Export { |
| 6 |
|
| 7 |
protected $args_to_export; |
| 8 |
|
| 9 |
/** |
| 10 |
* this will take custom options and posttypes that will be exported from database. |
| 11 |
* |
| 12 |
* @param array $args_to_export custom options and posttypes to export. |
| 13 |
*/ |
| 14 |
function __construct( $args_to_export ) { |
| 15 |
$this->args_to_export = $args_to_export; |
| 16 |
} |
| 17 |
|
| 18 |
/* function to export from database */ |
| 19 |
private function export_array( $nonce ) { |
| 20 |
if( !wp_verify_nonce( $nonce, 'wppb_export_settings' ) ) |
| 21 |
return array(); |
| 22 |
|
| 23 |
/* export options from database */ |
| 24 |
$option_values = array(); |
| 25 |
foreach( $this->args_to_export['options'] as $option ) { |
| 26 |
$get_option = get_option( $option ); |
| 27 |
if( $get_option !== false ) { |
| 28 |
$option_values[$option] = $get_option; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/* export custom posts from database */ |
| 33 |
$all_custom_posts = array(); |
| 34 |
foreach( $this->args_to_export['cpts'] as $post_type ) { |
| 35 |
$all_custom_posts[$post_type] = get_posts( "post_type=$post_type&posts_per_page=-1" ); |
| 36 |
foreach( $all_custom_posts[$post_type] as $key => $value ) { |
| 37 |
$all_custom_posts[$post_type][$key]->postmeta = get_post_custom( $value->ID ); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
/* create and return array for export */ |
| 42 |
$all_for_export = array( |
| 43 |
"options" => $option_values, |
| 44 |
"posts" => $all_custom_posts |
| 45 |
); |
| 46 |
|
| 47 |
return $all_for_export; |
| 48 |
} |
| 49 |
|
| 50 |
/* export to json file */ |
| 51 |
public function download_to_json_format( $prefix ) { |
| 52 |
|
| 53 |
if( isset( $_POST['cozmos-export'] ) && isset( $_POST['wppb_nonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['wppb_nonce'] ), 'wppb_export_settings' ) ) { |
| 54 |
|
| 55 |
$all_for_export = $this->export_array( sanitize_text_field( $_POST['wppb_nonce'] ) ); |
| 56 |
$json = json_encode( $all_for_export ); |
| 57 |
$filename = $prefix . date( 'Y-m-d_h.i.s', time() ); |
| 58 |
$filename .= '.json'; |
| 59 |
header( "Content-Disposition: attachment; filename=$filename" ); |
| 60 |
header( 'Content-type: application/json' ); |
| 61 |
header( 'Content-Length: ' . mb_strlen( $json ) ); |
| 62 |
header( 'Connection: close' ); |
| 63 |
echo $json; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 64 |
exit; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
} |
| 69 |
} |
| 70 |
|