| 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 |
|
| 13 |
|
| 14 |
public static function form_export(): void { |
| 15 |
?> |
| 16 |
<form method="post"> |
| 17 |
<p/> |
| 18 |
<?php |
| 19 |
$tags = DBManager::get_tags_from_table(); |
| 20 |
|
| 21 |
$tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : ''; |
| 22 |
$tag_search = ( $tag_search === 'all' ) ? '' : $tag_search; |
| 23 |
|
| 24 |
echo '<div class="actions"><label for="filter-by-tag" class="screen-reader-text">' . esc_attr__( 'Filter by tag', |
| 25 |
'wp-coder' ) . '</label>'; |
| 26 |
echo '<select name="tag" id="filter-by-tag">'; |
| 27 |
echo '<option value="all"' . selected( 'all', $tag_search, false ) . '>' . esc_attr__( 'All', |
| 28 |
'wp-coder' ) . '</option>'; |
| 29 |
|
| 30 |
if ( ! empty( $tags ) ) { |
| 31 |
foreach ( $tags as $tag ) { |
| 32 |
if ( empty( $tag['tag'] ) ) { |
| 33 |
continue; |
| 34 |
} |
| 35 |
echo '<option value="' . esc_attr( trim( $tag['tag'] ) ) . '"' . selected( $tag['tag'], $tag_search, |
| 36 |
false ) . '>' . esc_html( $tag['tag'] ) . '</option>'; |
| 37 |
} |
| 38 |
} |
| 39 |
echo '</select>'; |
| 40 |
submit_button( __( 'Export Data', 'wp-coder' ), 'secondary', 'submit', false ); |
| 41 |
echo '</div>'; |
| 42 |
wp_nonce_field( WPCoder::PREFIX . '_nonce', WPCoder::PREFIX . '_export_data' ); |
| 43 |
?> |
| 44 |
</form> |
| 45 |
|
| 46 |
<?php |
| 47 |
} |
| 48 |
|
| 49 |
public static function form_import(): void { |
| 50 |
?> |
| 51 |
<form method="post" enctype="multipart/form-data" action=""> |
| 52 |
<p> |
| 53 |
<span class="wowp-file"> |
| 54 |
<input type="file" name="import_file" accept="*.json"/> |
| 55 |
</span> |
| 56 |
<br> |
| 57 |
Upload a valid WP Coder Pro .json file exported from another site. |
| 58 |
</p> |
| 59 |
<p> |
| 60 |
<label> |
| 61 |
<input type="checkbox" name="wowp_import_update" value="1"> |
| 62 |
<?php esc_attr_e( 'Update item if item already exists.', 'wp-coder' ); ?> |
| 63 |
</label> |
| 64 |
|
| 65 |
</p> |
| 66 |
|
| 67 |
<p> |
| 68 |
<?php submit_button( __( 'Import Settings', 'wp-coder' ), 'secondary', 'submit', false ); ?> |
| 69 |
<?php wp_nonce_field( WPCoder::PREFIX . '_nonce', WPCoder::PREFIX . '_import_data' ); ?> |
| 70 |
</p> |
| 71 |
</form> |
| 72 |
|
| 73 |
<?php |
| 74 |
} |
| 75 |
|
| 76 |
public static function import_data(): void { |
| 77 |
|
| 78 |
if ( self::get_file_extension( $_FILES['import_file']['name'] ) != 'json' ) { |
| 79 |
wp_die( esc_attr__( 'Please upload a valid .json file', 'wp-coder' ), esc_attr__( 'Error', 'wp-coder' ), |
| 80 |
[ 'response' => 400 ] ); |
| 81 |
} |
| 82 |
|
| 83 |
$import_file = $_FILES['import_file']['tmp_name']; |
| 84 |
$settings = json_decode( file_get_contents( $import_file ), false ); |
| 85 |
|
| 86 |
$columns = DBManager::get_columns(); |
| 87 |
|
| 88 |
$update = ! empty( $_POST['wowp_import_update'] ) ? '1' : ''; |
| 89 |
|
| 90 |
foreach ( $settings as $key => $val ) { |
| 91 |
|
| 92 |
$data = []; |
| 93 |
$formats = []; |
| 94 |
|
| 95 |
foreach ( $columns as $column ) { |
| 96 |
$name = $column->Field; |
| 97 |
$data[ $name ] = ! empty( $val->$name ) ? $val->$name : ''; |
| 98 |
if ( $name === 'id' || $name === 'status' || $name === 'mode' ) { |
| 99 |
$formats[] = '%d'; |
| 100 |
} else { |
| 101 |
$formats[] = '%s'; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$check_row = DBManager::check_row( $data['id'] ); |
| 106 |
|
| 107 |
if ( ! empty( $update ) && ! empty( $check_row ) ) { |
| 108 |
|
| 109 |
$where = [ |
| 110 |
'id' => absint( $data['id'] ), |
| 111 |
]; |
| 112 |
$index = array_search( 'id', array_keys( $data ), true ); |
| 113 |
unset( $data['id'], $formats[ $index ] ); |
| 114 |
|
| 115 |
DBManager::update( $data, $where, $formats ); |
| 116 |
} elseif ( ! empty( $check_row ) ) { |
| 117 |
$index = array_search( 'id', array_keys( $data ), true ); |
| 118 |
unset( $data['id'], $formats[ $index ] ); |
| 119 |
|
| 120 |
DBManager::insert( $data, $formats ); |
| 121 |
} else { |
| 122 |
DBManager::insert( $data, $formats ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$redirect_link = add_query_arg( [ |
| 127 |
'page' => WPCoder::SLUG, |
| 128 |
], admin_url( 'admin.php' ) ); |
| 129 |
|
| 130 |
wp_safe_redirect( $redirect_link ); |
| 131 |
exit; |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
private static function get_file_extension( $str ) { |
| 136 |
$parts = explode( '.', $str ); |
| 137 |
|
| 138 |
return end( $parts ); |
| 139 |
} |
| 140 |
|
| 141 |
public static function export_item( $id = 0, $action = '' ): bool { |
| 142 |
|
| 143 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : ''; |
| 144 |
$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : $action; |
| 145 |
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id; |
| 146 |
|
| 147 |
if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
$data = DBManager::get_data_by_id( $id ); |
| 152 |
if ( ! $data ) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
$name = trim( $data->title ); |
| 157 |
$name = str_replace( ' ', '-', $name ); |
| 158 |
$file_name = WPCoder::info( 'data' ) . '-' . $name . '-id-' . $id . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json'; |
| 159 |
self::export( $file_name, [ $data ] ); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
public static function export_items(): bool { |
| 165 |
|
| 166 |
if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
if ( !isset( $_REQUEST['action'] ) || 'export' !== $_REQUEST['action'] ) { |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
$name = WPCoder::PREFIX . '_list_action'; |
| 175 |
$nonce_action = WPCoder::PREFIX . '_nonce'; |
| 176 |
|
| 177 |
if( ! isset( $_POST[ $name ] ) || ! wp_verify_nonce( $_POST[ $name ], $nonce_action ) || ! current_user_can( 'unfiltered_html' ) ) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
$ids = isset( $_POST['ID'] ) ? ( map_deep( $_POST['ID'], 'absint' ) ) : false; |
| 182 |
|
| 183 |
|
| 184 |
if ( empty( $ids ) || ! is_array( $ids ) ) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
$data = DBManager::get_data_by_ids( $ids ); |
| 189 |
if ( ! $data ) { |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
$file_name = WPCoder::info( 'data' ) . '-items-' . implode( '-', array_slice( $ids, 0, 3 ) ) . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json'; |
| 194 |
self::export( $file_name, $data ); |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
public static function export_data(): bool { |
| 200 |
$tag = isset( $_POST['tag'] ) ? sanitize_text_field( wp_unslash( $_POST['tag'] ) ) : ''; |
| 201 |
if ( ! empty( $tag ) ) { |
| 202 |
$file_name = WPCoder::info( 'data' ) . '-tag-' . esc_attr( $tag ) . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json'; |
| 203 |
$data = DBManager::get_data_by_tag( $tag ); |
| 204 |
} else { |
| 205 |
$file_name = WPCoder::info( 'data' ) . '-database-' . gmdate( 'Y-m-d-H-i-s' ) . '.json'; |
| 206 |
$data = DBManager::get_all_data(); |
| 207 |
} |
| 208 |
|
| 209 |
if ( empty( $data ) ) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
self::export( $file_name, $data ); |
| 213 |
|
| 214 |
return true; |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
private static function export( $file_name, $data ): void { |
| 219 |
|
| 220 |
ignore_user_abort( true ); |
| 221 |
nocache_headers(); |
| 222 |
header( 'Content-Type: application/json; charset=utf-8' ); |
| 223 |
header( 'Content-Disposition: attachment; filename=' . $file_name ); |
| 224 |
header( "Expires: 0" ); |
| 225 |
|
| 226 |
echo json_encode( $data ); |
| 227 |
exit; |
| 228 |
} |
| 229 |
|
| 230 |
} |