400 ] );
}
$import_file = $_FILES['import_file']['tmp_name'];
$settings = json_decode( file_get_contents( $import_file ), false );
$columns = DBManager::get_columns();
$update = ! empty( $_POST['wowp_import_update'] ) ? '1' : '';
foreach ( $settings as $key => $val ) {
$data = [];
$formats = [];
foreach ( $columns as $column ) {
$name = $column->Field;
$data[ $name ] = ! empty( $val->$name ) ? $val->$name : '';
if ( $name === 'id' || $name === 'status' || $name === 'mode' ) {
$formats[] = '%d';
} else {
$formats[] = '%s';
}
}
$check_row = DBManager::check_row( $data['id'] );
if ( ! empty( $update ) && ! empty( $check_row ) ) {
$where = [
'id' => absint( $data['id'] ),
];
$index = array_search( 'id', array_keys( $data ), true );
unset( $data['id'], $formats[ $index ] );
DBManager::update( $data, $where, $formats );
} elseif ( ! empty( $check_row ) ) {
$index = array_search( 'id', array_keys( $data ), true );
unset( $data['id'], $formats[ $index ] );
DBManager::insert( $data, $formats );
} else {
DBManager::insert( $data, $formats );
}
}
$redirect_link = add_query_arg( [
'page' => WPCoder::SLUG,
], admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_link );
exit;
}
private static function get_file_extension( $str ) {
$parts = explode( '.', $str );
return end( $parts );
}
public static function export_item( $id = 0, $action = '' ): bool {
$page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
$action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : $action;
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) {
return false;
}
$data = DBManager::get_data_by_id( $id );
if ( ! $data ) {
return false;
}
$name = trim( $data->title );
$name = str_replace( ' ', '-', $name );
$file_name = WPCoder::info( 'data' ) . '-' . $name . '-id-' . $id . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
self::export( $file_name, [ $data ] );
return true;
}
public static function export_items(): bool {
if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) {
return false;
}
if ( !isset( $_REQUEST['action'] ) || 'export' !== $_REQUEST['action'] ) {
return false;
}
$name = WPCoder::PREFIX . '_list_action';
$nonce_action = WPCoder::PREFIX . '_nonce';
if( ! isset( $_POST[ $name ] ) || ! wp_verify_nonce( $_POST[ $name ], $nonce_action ) || ! WPCoder::user_can_manage() ) {
return false;
}
$ids = isset( $_POST['ID'] ) ? ( map_deep( $_POST['ID'], 'absint' ) ) : false;
if ( empty( $ids ) || ! is_array( $ids ) ) {
return false;
}
$data = DBManager::get_data_by_ids( $ids );
if ( ! $data ) {
return false;
}
$file_name = WPCoder::info( 'data' ) . '-items-' . implode( '-', array_slice( $ids, 0, 3 ) ) . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
self::export( $file_name, $data );
return true;
}
public static function export_data(): bool {
$tag = isset( $_POST['tag'] ) ? sanitize_text_field( wp_unslash( $_POST['tag'] ) ) : '';
if ( ! empty( $tag ) ) {
$file_name = WPCoder::info( 'data' ) . '-tag-' . esc_attr( $tag ) . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
$data = DBManager::get_data_by_tag( $tag );
} else {
$file_name = WPCoder::info( 'data' ) . '-database-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
$data = DBManager::get_all_data();
}
if ( empty( $data ) ) {
return false;
}
self::export( $file_name, $data );
return true;
}
private static function export( $file_name, $data ): void {
ignore_user_abort( true );
nocache_headers();
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename=' . $file_name );
header( "Expires: 0" );
echo json_encode( $data );
exit;
}
}