| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { exit; } |
| 3 |
if(!class_exists('wpSmartImportQuery')) { |
| 4 |
class wpSmartImportQuery { |
| 5 |
|
| 6 |
public function wpsi_insert($table, $data, $format = null) { |
| 7 |
global $wpdb; |
| 8 |
$table = $wpdb->prefix.$table; |
| 9 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 10 |
$res = $wpdb->insert($table, $data, $format); |
| 11 |
return $res == false ? 0 : $wpdb->insert_id; |
| 12 |
} |
| 13 |
|
| 14 |
public function wpsi_update($table, $data, $where, $format = null, $where_format = null) { |
| 15 |
global $wpdb; |
| 16 |
$table = $wpdb->prefix.$table; |
| 17 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 18 |
$res = $wpdb->update($table, $data, $where, $format, $where_format); |
| 19 |
return $res == false ? 0 : $res; |
| 20 |
} |
| 21 |
|
| 22 |
public function wpsi_getRow($table_name, $id) { |
| 23 |
global $wpdb; |
| 24 |
$table = $wpdb->prefix.$table_name; |
| 25 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 26 |
$ret = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `$table` WHERE id = %d", $id ) ); |
| 27 |
if (empty($ret)) { |
| 28 |
$ret = array('error'=>"No Data found"); |
| 29 |
} |
| 30 |
return $ret; |
| 31 |
} |
| 32 |
|
| 33 |
public function retrieve_posts($import_id) { |
| 34 |
global $wpdb; |
| 35 |
$table = $wpdb->prefix."wpsi_posts"; |
| 36 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 37 |
$posts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE import_id = %d", $import_id ) ); |
| 38 |
return $posts; |
| 39 |
} |
| 40 |
|
| 41 |
public function wpsi_saveFile($post_data) { |
| 42 |
global $wpdb; |
| 43 |
$table = $wpdb->prefix."wpsi_files"; |
| 44 |
$res = 0; |
| 45 |
$data = array( |
| 46 |
"name" => $post_data['file_name'], |
| 47 |
"file_name" => basename($post_data['file_path']), |
| 48 |
"file_path" => $post_data['file_path'] , |
| 49 |
"folder_name" => wpSmartImport::getVar('folder_name'), |
| 50 |
"created_at" => current_time('mysql'), |
| 51 |
"updated_at" => current_time('mysql') |
| 52 |
); |
| 53 |
$format = array('%s', '%s','%s', '%s', '%s'); |
| 54 |
if (self::file_record_exist($post_data['file_path'])) { |
| 55 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 56 |
$res = $wpdb->insert($table, $data, $format); |
| 57 |
$res = $wpdb->insert_id; |
| 58 |
} |
| 59 |
return $res; |
| 60 |
} |
| 61 |
|
| 62 |
static public function retrieve_files() { |
| 63 |
global $wpdb; |
| 64 |
$table = esc_sql( $wpdb->prefix . "wpsi_files" ); |
| 65 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 66 |
$querystr = $wpdb->prepare( "SELECT * FROM {$table}" ); |
| 67 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 68 |
$files = $wpdb->get_results( $querystr, ARRAY_A ); |
| 69 |
return $files; |
| 70 |
} |
| 71 |
|
| 72 |
public function delete_record($table_name, $where, $format) { |
| 73 |
global $wpdb; |
| 74 |
$table = $wpdb->prefix.$table_name; |
| 75 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 76 |
return $wpdb->delete($table, $where, $format); |
| 77 |
} |
| 78 |
|
| 79 |
public function delete_import($import_id) { |
| 80 |
if(empty($import_id) || absint($import_id) < 1 ) return; |
| 81 |
global $wpdb; |
| 82 |
$table = $wpdb->prefix."wpsi_imports"; |
| 83 |
do_action( 'wpsi_delete_import', $import_id ); |
| 84 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 85 |
return $wpdb->delete($table, array('id' => $import_id), array('%d')); |
| 86 |
} |
| 87 |
|
| 88 |
public function delete_file_by($field, $value) { |
| 89 |
$format = is_numeric($value) ? '%d' : '%s'; |
| 90 |
global $wpdb; |
| 91 |
$table = $wpdb->prefix."wpsi_files"; |
| 92 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 93 |
return $wpdb->delete($table, array($field => $value), array($format)); |
| 94 |
} |
| 95 |
|
| 96 |
public function delete_post($post_id, $import_id) { |
| 97 |
global $wpdb; |
| 98 |
$table = $wpdb->prefix."wpsi_posts"; |
| 99 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 100 |
return $wpdb->delete($table, array('post_id' => $post_id,'import_id' => $import_id), array( '%d','%d')); |
| 101 |
} |
| 102 |
|
| 103 |
public function check_unique_key($value) { |
| 104 |
global $wpdb; |
| 105 |
$table = $wpdb->prefix."wpsi_imports"; |
| 106 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 107 |
$a = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE unique_key = %s", $value ) ); |
| 108 |
if (count($a) > 0) { |
| 109 |
return false; |
| 110 |
} else { |
| 111 |
return true; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
public function check_post_exist($post_id, $unique_key) { |
| 116 |
global $wpdb; |
| 117 |
$table = $wpdb->prefix."wpsi_posts"; |
| 118 |
$id = 0; |
| 119 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 120 |
$id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table WHERE `post_id` = %d AND `unique_key` = %s", $post_id, $unique_key ) ); |
| 121 |
return empty($id) ? false : true; |
| 122 |
} |
| 123 |
|
| 124 |
public function file_record_exist($value) { |
| 125 |
global $wpdb; |
| 126 |
$table = $wpdb->prefix."wpsi_files"; |
| 127 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 128 |
$results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table WHERE file_path = %s", $value ) ); |
| 129 |
if (count($results) > 0) { |
| 130 |
return false; |
| 131 |
} else { |
| 132 |
return true; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
static function wpsi_file_name_check() { |
| 137 |
global $wpdb; |
| 138 |
if ( isset( $_REQUEST['_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_nonce'] ) ), 'wpsi_nonce')) { |
| 139 |
|
| 140 |
$table = $wpdb->prefix."wpsi_files"; |
| 141 |
$name = isset( $_POST['name'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['name'] ) ) ) : ''; |
| 142 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 143 |
$result = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $table WHERE `name` = %s", $name ) ); |
| 144 |
if(empty($result)) { |
| 145 |
$msg = "You Can use this name"; |
| 146 |
$response = 'success'; |
| 147 |
} else { |
| 148 |
$msg = "Name Already Exist <br> Note : USE Unique name for finding a file Easily"; |
| 149 |
$response = 'error'; |
| 150 |
} |
| 151 |
echo json_encode(array('msg' => $msg, 'response' => $response)); |
| 152 |
|
| 153 |
}else{ |
| 154 |
echo json_encode( array('msg' => 'Nonce verification failed!', 'response' => 'error' ) ); |
| 155 |
} |
| 156 |
wp_die(); |
| 157 |
} |
| 158 |
} |
| 159 |
new wpSmartImportQuery; |
| 160 |
} |