| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Dashboard; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use WPCoder\WPCoder; |
| 8 |
|
| 9 |
class ImporterExporter { |
| 10 |
private $settings; |
| 11 |
|
| 12 |
public static function form_export(): void { |
| 13 |
?> |
| 14 |
<form method="post"> |
| 15 |
<p></p> |
| 16 |
<p> |
| 17 |
<?php submit_button( __( 'Export All Data', 'wpcoder' ), 'secondary', 'submit', false ); ?> |
| 18 |
<?php wp_nonce_field( WPCoder::PREFIX . '_nonce', WPCoder::PREFIX . '_export_data' ); ?> |
| 19 |
</p> |
| 20 |
</form> |
| 21 |
|
| 22 |
<?php |
| 23 |
} |
| 24 |
|
| 25 |
public static function form_import(): void { |
| 26 |
?> |
| 27 |
<form method="post" enctype="multipart/form-data" action=""> |
| 28 |
<p> |
| 29 |
<span class="wowp-file"> |
| 30 |
<input type="file" name="import_file" accept="*.json"/> |
| 31 |
</span> |
| 32 |
</p> |
| 33 |
<p> |
| 34 |
<label> |
| 35 |
<input type="checkbox" name="wowp_import_update" value="1"> |
| 36 |
<?php esc_attr_e( 'Update item if item already exists.' . 'wpcoder' ); ?> |
| 37 |
</label> |
| 38 |
|
| 39 |
</p> |
| 40 |
|
| 41 |
<p> |
| 42 |
<?php submit_button( __( 'Import', 'wpcoder' ), 'secondary', 'submit', false ); ?> |
| 43 |
<?php wp_nonce_field( WPCoder::PREFIX . '_nonce', WPCoder::PREFIX . '_import_data' ); ?> |
| 44 |
</p> |
| 45 |
</form> |
| 46 |
|
| 47 |
<?php |
| 48 |
} |
| 49 |
|
| 50 |
public static function import_data(): void { |
| 51 |
|
| 52 |
if ( self::get_file_extension( $_FILES['import_file']['name'] ) != 'json' ) { |
| 53 |
wp_die( __( 'Please upload a valid .json file', 'wpcoder' ), __( 'Error', 'wpcoder' ), |
| 54 |
[ 'response' => 400 ] ); |
| 55 |
} |
| 56 |
|
| 57 |
$import_file = $_FILES['import_file']['tmp_name']; |
| 58 |
$settings = json_decode( file_get_contents( $import_file ), false ); |
| 59 |
|
| 60 |
$columns = DBManager::get_columns(); |
| 61 |
|
| 62 |
$update = ! empty( $_POST['wowp_import_update'] ) ? '1' : ''; |
| 63 |
|
| 64 |
foreach ( $settings as $key => $val ) { |
| 65 |
|
| 66 |
$data = []; |
| 67 |
$formats = []; |
| 68 |
|
| 69 |
foreach ( $columns as $column ) { |
| 70 |
$name = $column->Field; |
| 71 |
$data[ $name ] = ! empty( $val->$name ) ? $val->$name : ''; |
| 72 |
if ( $name === 'id' || $name === 'status' || $name === 'mode' ) { |
| 73 |
$formats[] = '%d'; |
| 74 |
} else { |
| 75 |
$formats[] = '%s'; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$check_row = DBManager::check_row( $data['id'] ); |
| 80 |
|
| 81 |
if ( ! empty( $update ) && ! empty( $check_row ) ) { |
| 82 |
|
| 83 |
$where = [ |
| 84 |
'id' => absint( $data['id'] ), |
| 85 |
]; |
| 86 |
$index = array_search( 'id', array_keys( $data ), true ); |
| 87 |
unset( $data['id'], $formats[ $index ] ); |
| 88 |
|
| 89 |
DBManager::update( $data, $where, $formats ); |
| 90 |
} elseif ( ! empty( $check_row ) ) { |
| 91 |
$index = array_search( 'id', array_keys( $data ), true ); |
| 92 |
unset( $data['id'], $formats[ $index ] ); |
| 93 |
|
| 94 |
DBManager::insert( $data, $formats ); |
| 95 |
} else { |
| 96 |
DBManager::insert( $data, $formats ); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$redirect_link = add_query_arg( [ |
| 101 |
'page' => WPCoder::SLUG, |
| 102 |
], admin_url( 'admin.php' ) ); |
| 103 |
|
| 104 |
wp_safe_redirect( $redirect_link ); |
| 105 |
exit; |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
private static function get_file_extension( $str ) { |
| 110 |
$parts = explode( '.', $str ); |
| 111 |
|
| 112 |
return end( $parts ); |
| 113 |
} |
| 114 |
|
| 115 |
public static function export_item($id = 0, $action = '') { |
| 116 |
|
| 117 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : ''; |
| 118 |
$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : $action; |
| 119 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id; |
| 120 |
|
| 121 |
if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
$data = DBManager::get_data_by_id( $id ); |
| 126 |
if ( ! $data ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
$name = trim( $data->title ); |
| 131 |
$name = str_replace( ' ', '-', $name ); |
| 132 |
$file_name = $name . '-database-' . date( 'm-d-Y' ) . '.json'; |
| 133 |
self::export( $file_name, [ $data ] ); |
| 134 |
|
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
public static function export_data(): bool { |
| 139 |
$file_name = WPCoder::SHORTCODE . '-database-' . date( 'm-d-Y' ) . '.json'; |
| 140 |
$data = DBManager::get_all_data(); |
| 141 |
if ( empty( $data ) ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
self::export( $file_name, $data ); |
| 145 |
|
| 146 |
return true; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
private static function export( $file_name, $data ): void { |
| 151 |
|
| 152 |
ignore_user_abort( true ); |
| 153 |
nocache_headers(); |
| 154 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 155 |
header( 'Content-Disposition: attachment; filename=' . $file_name ); |
| 156 |
header( "Expires: 0" ); |
| 157 |
|
| 158 |
echo json_encode( $data ); |
| 159 |
exit; |
| 160 |
} |
| 161 |
|
| 162 |
} |