| @@ -13,447 +13,482 @@ | ||
| 13 | 13 | use WP_Error; |
| 14 | 14 | |
| 15 | 15 | class Model |
| 16 | 16 | { |
| 17 | - protected static $table; | |
| 18 | - protected static $primary_key; | |
| 19 | - protected $app_db; | |
| 20 | - protected $table_name; | |
| 21 | - protected $db_response; | |
| 22 | - /** | |
| 23 | - * Undocumented function | |
| 24 | - */ | |
| 25 | - public function __construct() | |
| 26 | - { | |
| 27 | - global $wpdb; | |
| 28 | - $this->app_db = $wpdb; | |
| 29 | - $this->table_name = $wpdb->prefix . static::$table; | |
| 17 | + protected static $table; | |
| 18 | + protected static $primary_key; | |
| 19 | + protected $app_db; | |
| 20 | + protected $table_name; | |
| 21 | + protected $db_response; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Undocumented function | |
| 25 | + */ | |
| 26 | + public function __construct() | |
| 27 | + { | |
| 28 | + global $wpdb; | |
| 29 | + $this->app_db = $wpdb; | |
| 30 | + $this->table_name = $wpdb->prefix . static::$table; | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Undocumented function | |
| 35 | + * | |
| 36 | + * @return void | |
| 37 | + */ | |
| 38 | + public function insert($data = []) | |
| 39 | + { | |
| 40 | + if (is_null($data)) { | |
| 41 | + return new WP_Error('empty_data', __('Form data is empty', 'bit-form')); | |
| 30 | 42 | } |
| 31 | - /** | |
| 32 | - * Undocumented function | |
| 33 | - * | |
| 34 | - * @return void | |
| 35 | - */ | |
| 36 | - public function insert($data = array()) | |
| 37 | - { | |
| 38 | - if (is_null($data)) { | |
| 39 | - return new WP_Error('empty_data', __('Form data is empty', 'bitform')); | |
| 40 | - } | |
| 41 | - $result = $this->app_db->insert( | |
| 42 | - $this->table_name, | |
| 43 | - $data | |
| 44 | - ); | |
| 45 | - return $this->getResult($result); | |
| 43 | + $result = $this->app_db->insert( | |
| 44 | + $this->table_name, | |
| 45 | + $data | |
| 46 | + ); | |
| 47 | + return $this->getResult($result); | |
| 48 | + } | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * Undocumented function | |
| 52 | + * | |
| 53 | + * @param string $item | |
| 54 | + * @param array $condition | |
| 55 | + * | |
| 56 | + * @return mixed | |
| 57 | + */ | |
| 58 | + public function get($item = '*', $condition = [], $limit = null, $offset = null, $order_by = null, $order_follow = null) | |
| 59 | + { | |
| 60 | + $checkIfTableExists = $this->app_db->get_var( | |
| 61 | + $this->app_db->prepare( | |
| 62 | + 'SHOW TABLES LIKE %s', | |
| 63 | + $this->table_name | |
| 64 | + ) | |
| 65 | + ); | |
| 66 | + if (is_null($checkIfTableExists)) { | |
| 67 | + return []; | |
| 46 | 68 | } |
| 47 | - /** | |
| 48 | - * Undocumented function | |
| 49 | - * | |
| 50 | - * @param string $item | |
| 51 | - * @param array $condition | |
| 52 | - * | |
| 53 | - * @return void | |
| 54 | - */ | |
| 55 | - public function get($item = "*", $condition = [], $limit = null, $offset = null, $order_by = null, $order_follow = null) | |
| 56 | - { | |
| 57 | - if (\is_array($item)) { | |
| 58 | - $column_to_select = implode(",", $item); | |
| 59 | - } else { | |
| 60 | - $column_to_select = $item; | |
| 61 | - } | |
| 62 | - $checkCondition = $this->checkCondition($condition); | |
| 63 | - if (is_wp_error($checkCondition)) { | |
| 64 | - return $checkCondition; | |
| 65 | - } | |
| 66 | - $order = null; | |
| 67 | - if (!\is_null($order_by)) { | |
| 68 | - $order_follow = \is_null($order_follow) ? 'ASC' : $order_follow; | |
| 69 | - $order .= " ORDER BY $order_by $order_follow"; | |
| 70 | - } | |
| 71 | - $paginate = null; | |
| 72 | - if (!\is_null($limit)) { | |
| 73 | - $limit = \intval($limit); | |
| 74 | - $paginate .= " LIMIT $limit "; | |
| 75 | - } | |
| 76 | - if (!\is_null($offset)) { | |
| 77 | - $offset = \intval($offset); | |
| 78 | - $paginate .= " OFFSET $offset "; | |
| 79 | - } | |
| 80 | - if (empty($condition)) { | |
| 81 | - $sql = "SELECT $column_to_select FROM `$this->table_name` $order $paginate"; | |
| 82 | - $all_values = null; | |
| 83 | - } else { | |
| 84 | - $formatted_conditions = $this->getFormatedCondition($condition); | |
| 85 | - if ($formatted_conditions) { | |
| 86 | - $condition_to_check = $formatted_conditions['conditions']; | |
| 87 | - $all_values = $formatted_conditions['values']; | |
| 88 | - } else { | |
| 89 | - $condition_to_check = null; | |
| 90 | - $all_values = null; | |
| 91 | - } | |
| 92 | - $sql = "SELECT $column_to_select FROM `$this->table_name`" | |
| 93 | - . $condition_to_check . $order . $paginate; | |
| 94 | - } | |
| 95 | - // var_dump($sql); | |
| 96 | - return $this->execute($sql, $all_values)->getResult(); | |
| 69 | + if (\is_array($item)) { | |
| 70 | + $column_to_select = implode(',', $item); | |
| 71 | + } else { | |
| 72 | + $column_to_select = $item; | |
| 97 | 73 | } |
| 98 | - /** | |
| 99 | - * Undocumented function | |
| 100 | - * | |
| 101 | - * @param string $item | |
| 102 | - * @param array $condition | |
| 103 | - * | |
| 104 | - * @return void | |
| 105 | - */ | |
| 106 | - public function count($condition = null) | |
| 107 | - { | |
| 108 | - $checkCondition = $this->checkCondition($condition); | |
| 109 | - if (is_wp_error($checkCondition)) { | |
| 110 | - return $checkCondition; | |
| 111 | - } | |
| 112 | - if (empty($condition)) { | |
| 113 | - $result = $this->app_db->query( | |
| 114 | - "SELECT COUNT(*) FROM `$this->table_name`" | |
| 115 | - ); | |
| 116 | - } else { | |
| 117 | - $formatted_conditions = $this->getFormatedCondition($condition); | |
| 118 | - if ($formatted_conditions) { | |
| 119 | - $condition_to_check = $formatted_conditions['conditions']; | |
| 120 | - $all_values = $formatted_conditions['values']; | |
| 121 | - } else { | |
| 122 | - $condition_to_check = null; | |
| 123 | - $all_values = null; | |
| 124 | - } | |
| 125 | - $result = $this->app_db->query( | |
| 126 | - $this->app_db->prepare( | |
| 127 | - "SELECT COUNT(*) as count FROM `$this->table_name`" | |
| 128 | - . $condition_to_check, | |
| 129 | - $all_values | |
| 130 | - ) | |
| 131 | - ); | |
| 132 | - } | |
| 133 | - if (!$result) { | |
| 134 | - if ($this->app_db->last_error) { | |
| 135 | - return new WP_Error('db_error', $this->app_db->last_error); | |
| 136 | - } | |
| 137 | - return new WP_Error('db_error', __("Result is empty", "bitform")); | |
| 138 | - } else { | |
| 139 | - return $this->app_db->last_result; | |
| 140 | - } | |
| 74 | + $checkCondition = $this->checkCondition($condition); | |
| 75 | + if (is_wp_error($checkCondition)) { | |
| 76 | + return $checkCondition; | |
| 141 | 77 | } |
| 142 | - /** | |
| 143 | - * Undocumented function | |
| 144 | - * | |
| 145 | - * @param array $data_to_update | |
| 146 | - * @param array $condition | |
| 147 | - * | |
| 148 | - * @return void | |
| 149 | - */ | |
| 150 | - public function update(array $data, array $condition) | |
| 151 | - { | |
| 152 | - if ( | |
| 153 | - !\is_null($data) | |
| 154 | - && \is_array($data) | |
| 155 | - && array_keys($data) !== range(0, count($data) - 1) | |
| 156 | - ) { | |
| 157 | - $data_to_update = $data; | |
| 158 | - } else { | |
| 159 | - return new WP_Error( | |
| 160 | - 'update_error', | |
| 161 | - __('Nothing to update', 'bitform') | |
| 162 | - ); | |
| 163 | - } | |
| 164 | - $update_condition = (!\is_null($condition) && | |
| 165 | - array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 166 | - $result = $this->app_db->update( | |
| 167 | - $this->table_name, | |
| 168 | - $data_to_update, | |
| 169 | - $update_condition | |
| 170 | - ); | |
| 171 | - return $this->getResult($result); | |
| 78 | + $order = null; | |
| 79 | + if (!\is_null($order_by)) { | |
| 80 | + $order_follow = \is_null($order_follow) ? 'ASC' : $order_follow; | |
| 81 | + $order .= " ORDER BY $order_by $order_follow"; | |
| 172 | 82 | } |
| 173 | - /** | |
| 174 | - * Undocumented function | |
| 175 | - * | |
| 176 | - * @param array $data_to_update | |
| 177 | - * @param array $condition | |
| 178 | - * | |
| 179 | - * @return void | |
| 180 | - */ | |
| 181 | - public function bulkUpdate(array $data = null, array $condition = null) | |
| 182 | - { | |
| 183 | - if ( | |
| 184 | - !\is_null($data) | |
| 185 | - && \is_array($data) | |
| 186 | - && array_keys($data) !== range(0, count($data) - 1) | |
| 187 | - ) { | |
| 188 | - $data_to_update = $data; | |
| 189 | - } else { | |
| 190 | - return new WP_Error( | |
| 191 | - 'update_error', | |
| 192 | - __('Nothing to update', 'bitform') | |
| 193 | - ); | |
| 194 | - } | |
| 83 | + $paginate = null; | |
| 84 | + if (!\is_null($limit)) { | |
| 85 | + $limit = \intval($limit); | |
| 86 | + $paginate .= " LIMIT $limit "; | |
| 87 | + } | |
| 88 | + if (!\is_null($offset)) { | |
| 89 | + $offset = \intval($offset); | |
| 90 | + $paginate .= " OFFSET $offset "; | |
| 91 | + } | |
| 92 | + if (empty($condition)) { | |
| 93 | + $sql = "SELECT $column_to_select FROM `$this->table_name` $order $paginate"; | |
| 94 | + $all_values = null; | |
| 95 | + } else { | |
| 96 | + $formatted_conditions = $this->getFormatedCondition($condition); | |
| 97 | + if ($formatted_conditions) { | |
| 98 | + $condition_to_check = $formatted_conditions['conditions']; | |
| 99 | + $all_values = $formatted_conditions['values']; | |
| 100 | + } else { | |
| 101 | + $condition_to_check = null; | |
| 102 | + $all_values = null; | |
| 103 | + } | |
| 104 | + $sql = "SELECT $column_to_select FROM `$this->table_name`" | |
| 105 | + . $condition_to_check . $order . $paginate; | |
| 106 | + } | |
| 107 | + return $this->execute($sql, $all_values)->getResult(); | |
| 108 | + } | |
| 195 | 109 | |
| 196 | - $update_fields = ''; | |
| 197 | - $all_values = array(); | |
| 198 | - $index_checker = 0; | |
| 199 | - $data_count = count($data_to_update) - 1; | |
| 200 | - foreach ($data_to_update as $field_name => $field_value) { | |
| 201 | - $update_fields .= $field_name . ' = ' . $this->getFieldFormat($field_value); | |
| 202 | - if ($index_checker < $data_count) { | |
| 203 | - $update_fields .= ','; | |
| 204 | - } | |
| 205 | - $index_checker = $index_checker + 1; | |
| 206 | - $all_values[] = $field_value; | |
| 207 | - } | |
| 208 | - $update_condition = (!\is_null($condition) && | |
| 209 | - array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 210 | - $formatted_conditions = $this->getFormatedCondition($update_condition); | |
| 211 | - if ($formatted_conditions) { | |
| 212 | - $condition_to_check = $formatted_conditions['conditions']; | |
| 213 | - $all_values = array_merge($all_values, $formatted_conditions['values']); | |
| 214 | - } else { | |
| 215 | - $condition_to_check = null; | |
| 216 | - } | |
| 217 | - $result = $this->app_db->query( | |
| 218 | - $this->app_db->prepare( | |
| 219 | - "UPDATE $this->table_name SET $update_fields $condition_to_check", | |
| 220 | - $all_values | |
| 221 | - ) | |
| 222 | - ); | |
| 223 | - return $this->getResult($result); | |
| 110 | + /** | |
| 111 | + * Undocumented function | |
| 112 | + * | |
| 113 | + * @param string $item | |
| 114 | + * @param array $condition | |
| 115 | + * | |
| 116 | + * @return void | |
| 117 | + */ | |
| 118 | + public function count($condition = null) | |
| 119 | + { | |
| 120 | + $checkCondition = $this->checkCondition($condition); | |
| 121 | + if (is_wp_error($checkCondition)) { | |
| 122 | + return $checkCondition; | |
| 224 | 123 | } |
| 225 | - /** | |
| 226 | - * Duplicate's row | |
| 227 | - * | |
| 228 | - * @param array $data_to_update | |
| 229 | - * @param array $condition | |
| 230 | - * | |
| 231 | - * @return void | |
| 232 | - */ | |
| 233 | - public function duplicate(array $columns, array $duplicate, array $condition) | |
| 234 | - { | |
| 235 | - if (!(!\is_null($columns) | |
| 236 | - && \is_array($columns) | |
| 237 | - && array_keys($columns) === range(0, count($columns) - 1) | |
| 238 | - && !\is_null($duplicate) | |
| 239 | - && \is_array($duplicate) | |
| 240 | - && array_keys($duplicate) === range(0, count($duplicate) - 1))) { | |
| 241 | - return new WP_Error( | |
| 242 | - 'duplicate_error', | |
| 243 | - __('Nothing to duplicate', 'bitform') | |
| 244 | - ); | |
| 245 | - } | |
| 124 | + if (empty($condition)) { | |
| 125 | + $result = $this->app_db->query( | |
| 126 | + "SELECT COUNT(*) FROM `$this->table_name`" | |
| 127 | + ); | |
| 128 | + } else { | |
| 129 | + $formatted_conditions = $this->getFormatedCondition($condition); | |
| 130 | + if ($formatted_conditions) { | |
| 131 | + $condition_to_check = $formatted_conditions['conditions']; | |
| 132 | + $all_values = $formatted_conditions['values']; | |
| 133 | + } else { | |
| 134 | + $condition_to_check = null; | |
| 135 | + $all_values = null; | |
| 136 | + } | |
| 137 | + $result = $this->app_db->query( | |
| 138 | + $this->app_db->prepare( | |
| 139 | + "SELECT COUNT(*) as count FROM `$this->table_name`" | |
| 140 | + . $condition_to_check, | |
| 141 | + $all_values | |
| 142 | + ) | |
| 143 | + ); | |
| 144 | + } | |
| 145 | + if (!$result) { | |
| 146 | + if ($this->app_db->last_error) { | |
| 147 | + return new WP_Error('db_error', $this->app_db->last_error); | |
| 148 | + } | |
| 149 | + return new WP_Error('db_error', __('Result is empty', 'bit-form')); | |
| 150 | + } else { | |
| 151 | + return $this->app_db->last_result; | |
| 152 | + } | |
| 153 | + } | |
| 246 | 154 | |
| 247 | - $dupCol = ''; | |
| 248 | - $insCol = \implode(',', $columns); | |
| 249 | - $all_values = array(); | |
| 250 | - $data_count = count($duplicate) - 1; | |
| 251 | - foreach ($duplicate as $dupKey => $dupColName) { | |
| 252 | - if (in_array($dupColName, $columns)) { | |
| 253 | - $dupCol .= $dupColName; | |
| 254 | - } else { | |
| 255 | - $dupCol .= $this->getFieldFormat($dupColName); | |
| 256 | - $all_values[] = $dupColName; | |
| 257 | - } | |
| 258 | - if ($dupKey < $data_count) { | |
| 259 | - $dupCol .= ','; | |
| 260 | - } | |
| 261 | - } | |
| 262 | - $condition_to_check = null; | |
| 263 | - $update_condition = (!\is_null($condition) && | |
| 264 | - array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 265 | - $formatted_conditions = $this->getFormatedCondition($update_condition); | |
| 266 | - if ($formatted_conditions) { | |
| 267 | - $condition_to_check = $formatted_conditions['conditions']; | |
| 268 | - $all_values = array_merge($all_values, $formatted_conditions['values']); | |
| 269 | - } | |
| 270 | - $query = "INSERT INTO $this->table_name ($insCol) | |
| 155 | + /** | |
| 156 | + * Undocumented function | |
| 157 | + * | |
| 158 | + * @param array $data_to_update | |
| 159 | + * @param array $condition | |
| 160 | + * | |
| 161 | + * @return void | |
| 162 | + */ | |
| 163 | + public function update(array $data, array $condition) | |
| 164 | + { | |
| 165 | + if ( | |
| 166 | + !\is_null($data) | |
| 167 | + && \is_array($data) | |
| 168 | + && array_keys($data) !== range(0, count($data) - 1) | |
| 169 | + ) { | |
| 170 | + $data_to_update = $data; | |
| 171 | + } else { | |
| 172 | + return new WP_Error( | |
| 173 | + 'update_error', | |
| 174 | + __('Nothing to update', 'bit-form') | |
| 175 | + ); | |
| 176 | + } | |
| 177 | + $update_condition = (!\is_null($condition) && | |
| 178 | + array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 179 | + $result = $this->app_db->update( | |
| 180 | + $this->table_name, | |
| 181 | + $data_to_update, | |
| 182 | + $update_condition | |
| 183 | + ); | |
| 184 | + return $this->getResult($result); | |
| 185 | + } | |
| 186 | + | |
| 187 | + /** | |
| 188 | + * Undocumented function | |
| 189 | + * | |
| 190 | + * @param array $data_to_update | |
| 191 | + * @param array $condition | |
| 192 | + * | |
| 193 | + * @return void | |
| 194 | + */ | |
| 195 | + public function bulkUpdate(array $data = null, array $condition = null) | |
| 196 | + { | |
| 197 | + if ( | |
| 198 | + !\is_null($data) | |
| 199 | + && \is_array($data) | |
| 200 | + && array_keys($data) !== range(0, count($data) - 1) | |
| 201 | + ) { | |
| 202 | + $data_to_update = $data; | |
| 203 | + } else { | |
| 204 | + return new WP_Error( | |
| 205 | + 'update_error', | |
| 206 | + __('Nothing to update', 'bit-form') | |
| 207 | + ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + $update_fields = ''; | |
| 211 | + $all_values = []; | |
| 212 | + $index_checker = 0; | |
| 213 | + $data_count = count($data_to_update) - 1; | |
| 214 | + foreach ($data_to_update as $field_name => $field_value) { | |
| 215 | + $update_fields .= $field_name . ' = ' . $this->getFieldFormat($field_value); | |
| 216 | + if ($index_checker < $data_count) { | |
| 217 | + $update_fields .= ','; | |
| 218 | + } | |
| 219 | + $index_checker = $index_checker + 1; | |
| 220 | + $all_values[] = $field_value; | |
| 221 | + } | |
| 222 | + $update_condition = (!\is_null($condition) && | |
| 223 | + array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 224 | + $formatted_conditions = $this->getFormatedCondition($update_condition); | |
| 225 | + if ($formatted_conditions) { | |
| 226 | + $condition_to_check = $formatted_conditions['conditions']; | |
| 227 | + $all_values = array_merge($all_values, $formatted_conditions['values']); | |
| 228 | + } else { | |
| 229 | + $condition_to_check = null; | |
| 230 | + } | |
| 231 | + $result = $this->app_db->query( | |
| 232 | + $this->app_db->prepare( | |
| 233 | + "UPDATE $this->table_name SET $update_fields $condition_to_check", | |
| 234 | + $all_values | |
| 235 | + ) | |
| 236 | + ); | |
| 237 | + return $this->getResult($result); | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Duplicate's row | |
| 242 | + * | |
| 243 | + * @param array $data_to_update | |
| 244 | + * @param array $condition | |
| 245 | + * | |
| 246 | + * @return void | |
| 247 | + */ | |
| 248 | + public function duplicate(array $columns, array $duplicate, array $condition) | |
| 249 | + { | |
| 250 | + if (!(!\is_null($columns) | |
| 251 | + && \is_array($columns) | |
| 252 | + && array_keys($columns) === range(0, count($columns) - 1) | |
| 253 | + && !\is_null($duplicate) | |
| 254 | + && \is_array($duplicate) | |
| 255 | + && array_keys($duplicate) === range(0, count($duplicate) - 1))) { | |
| 256 | + return new WP_Error( | |
| 257 | + 'duplicate_error', | |
| 258 | + __('Nothing to duplicate', 'bit-form') | |
| 259 | + ); | |
| 260 | + } | |
| 261 | + | |
| 262 | + $dupCol = ''; | |
| 263 | + $insCol = \implode(',', $columns); | |
| 264 | + $all_values = []; | |
| 265 | + $data_count = count($duplicate) - 1; | |
| 266 | + foreach ($duplicate as $dupKey => $dupColName) { | |
| 267 | + if (in_array($dupColName, $columns)) { | |
| 268 | + $dupCol .= $dupColName; | |
| 269 | + } else { | |
| 270 | + $dupCol .= $this->getFieldFormat($dupColName); | |
| 271 | + $all_values[] = $dupColName; | |
| 272 | + } | |
| 273 | + if ($dupKey < $data_count) { | |
| 274 | + $dupCol .= ','; | |
| 275 | + } | |
| 276 | + } | |
| 277 | + $condition_to_check = null; | |
| 278 | + $update_condition = (!\is_null($condition) && | |
| 279 | + array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 280 | + $formatted_conditions = $this->getFormatedCondition($update_condition); | |
| 281 | + if ($formatted_conditions) { | |
| 282 | + $condition_to_check = $formatted_conditions['conditions']; | |
| 283 | + $all_values = array_merge($all_values, $formatted_conditions['values']); | |
| 284 | + } | |
| 285 | + $query = "INSERT INTO $this->table_name ($insCol) | |
| 271 | 286 | SELECT $dupCol FROM $this->table_name $condition_to_check"; |
| 272 | - $this->execute($query, $all_values); | |
| 273 | - return $this->getResult($result); | |
| 287 | + $this->execute($query, $all_values); | |
| 288 | + return $this->getResult(); | |
| 289 | + } | |
| 290 | + | |
| 291 | + public function trash(array $condition = null) | |
| 292 | + { | |
| 293 | + if ( | |
| 294 | + !\is_null($condition) | |
| 295 | + && \is_array($condition) | |
| 296 | + && array_keys($condition) !== range(0, count($condition) - 1) | |
| 297 | + ) { | |
| 298 | + $delete_condition = $condition; | |
| 299 | + } else { | |
| 300 | + return new WP_Error( | |
| 301 | + 'deletion_error', | |
| 302 | + __('At least 1 condition needed', 'bit-form') | |
| 303 | + ); | |
| 274 | 304 | } |
| 275 | - public function trash(array $condition = null) | |
| 276 | - { | |
| 277 | - if ( | |
| 278 | - !\is_null($condition) | |
| 279 | - && \is_array($condition) | |
| 280 | - && array_keys($condition) !== range(0, count($condition) - 1) | |
| 281 | - ) { | |
| 282 | - $delete_condition = $condition; | |
| 283 | - } else { | |
| 284 | - return new WP_Error( | |
| 285 | - 'deletion_error', | |
| 286 | - __('At least 1 condition needed', 'bitform') | |
| 287 | - ); | |
| 288 | - } | |
| 289 | - $update_condition = (!\is_null($condition) && | |
| 290 | - array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 291 | - $result = $this->app_db->update( | |
| 292 | - $this->table_name, | |
| 293 | - $data_to_update, | |
| 294 | - $update_condition | |
| 295 | - ); | |
| 296 | - return $this->getResult($result); | |
| 305 | + $update_condition = (!\is_null($condition) && | |
| 306 | + array_keys($condition) !== range(0, count($condition) - 1)) ? $condition : null; | |
| 307 | + $result = $this->app_db->update( | |
| 308 | + $this->table_name, | |
| 309 | + $data_to_update, | |
| 310 | + $update_condition | |
| 311 | + ); | |
| 312 | + return $this->getResult($result); | |
| 313 | + } | |
| 314 | + | |
| 315 | + public function delete(array $condition = null) | |
| 316 | + { | |
| 317 | + if ( | |
| 318 | + !\is_null($condition) | |
| 319 | + && \is_array($condition) | |
| 320 | + && array_keys($condition) !== range(0, count($condition) - 1) | |
| 321 | + ) { | |
| 322 | + $delete_condition = $condition; | |
| 323 | + } else { | |
| 324 | + return new WP_Error( | |
| 325 | + 'deletion_error', | |
| 326 | + __('At least 1 condition needed', 'bit-form') | |
| 327 | + ); | |
| 297 | 328 | } |
| 298 | - public function delete(array $condition = null) | |
| 299 | - { | |
| 300 | - if ( | |
| 301 | - !\is_null($condition) | |
| 302 | - && \is_array($condition) | |
| 303 | - && array_keys($condition) !== range(0, count($condition) - 1) | |
| 304 | - ) { | |
| 305 | - $delete_condition = $condition; | |
| 306 | - } else { | |
| 307 | - return new WP_Error( | |
| 308 | - 'deletion_error', | |
| 309 | - __('At least 1 condition needed', 'bitform') | |
| 310 | - ); | |
| 311 | - } | |
| 312 | - $result = $this->app_db->delete( | |
| 313 | - $this->table_name, | |
| 314 | - $delete_condition | |
| 315 | - ); | |
| 316 | - return $this->getResult($result); | |
| 329 | + $result = $this->app_db->delete( | |
| 330 | + $this->table_name, | |
| 331 | + $delete_condition | |
| 332 | + ); | |
| 333 | + return $this->getResult($result); | |
| 334 | + } | |
| 335 | + | |
| 336 | + public function bulkDelete(array $condition = null) | |
| 337 | + { | |
| 338 | + if ( | |
| 339 | + !\is_null($condition) | |
| 340 | + && \is_array($condition) | |
| 341 | + && array_keys($condition) !== range(0, count($condition) - 1) | |
| 342 | + ) { | |
| 343 | + $delete_condition = $condition; | |
| 344 | + } else { | |
| 345 | + return new WP_Error( | |
| 346 | + 'deletion_error', | |
| 347 | + __('At least 1 condition needed', 'bit-form') | |
| 348 | + ); | |
| 317 | 349 | } |
| 318 | - public function bulkDelete(array $condition = null) | |
| 319 | - { | |
| 320 | - if ( | |
| 321 | - !\is_null($condition) | |
| 322 | - && \is_array($condition) | |
| 323 | - && array_keys($condition) !== range(0, count($condition) - 1) | |
| 324 | - ) { | |
| 325 | - $delete_condition = $condition; | |
| 350 | + // $formatted_conditions = $this->getFormatedCondition($delete_condition, $check_operator); | |
| 351 | + $formatted_conditions = $this->getFormatedCondition($delete_condition); | |
| 352 | + if ($formatted_conditions) { | |
| 353 | + $condition_to_check = $formatted_conditions['conditions']; | |
| 354 | + $all_values = $formatted_conditions['values']; | |
| 355 | + } else { | |
| 356 | + $condition_to_check = null; | |
| 357 | + return new WP_Error( | |
| 358 | + 'deletion_error', | |
| 359 | + __('At least 1 condition needed', 'bit-form') | |
| 360 | + ); | |
| 361 | + } | |
| 362 | + $result = $this->app_db->query( | |
| 363 | + $this->app_db->prepare( | |
| 364 | + "DELETE FROM $this->table_name $condition_to_check", | |
| 365 | + $all_values | |
| 366 | + ) | |
| 367 | + ); | |
| 368 | + return $this->getResult($result); | |
| 369 | + } | |
| 370 | + | |
| 371 | + protected function getFieldFormat($value) | |
| 372 | + { | |
| 373 | + return ('integer' === gettype($value)) ? | |
| 374 | + '%d' : (('double' === gettype($value)) ? '%f' : '%s'); | |
| 375 | + } | |
| 376 | + | |
| 377 | + protected function getFormatedCondition($condition, $check_operator = null, $join_operator = ' AND ') | |
| 378 | + { | |
| 379 | + if (\is_null($condition)) { | |
| 380 | + return false; | |
| 381 | + } | |
| 382 | + $no_condition = count($condition); | |
| 383 | + $index_checker = 0; | |
| 384 | + $condition_to_check = ' WHERE '; | |
| 385 | + $all_values = []; | |
| 386 | + foreach ($condition as $key => $value) { | |
| 387 | + $value_type = ''; | |
| 388 | + if (is_array($value)) { | |
| 389 | + if (isset($value['operator'])) { | |
| 390 | + $set_check_operator = $value['operator']; | |
| 391 | + $value_type .= $this->getFieldFormat($value['value']); | |
| 392 | + $all_values[] = $value['value']; | |
| 326 | 393 | } else { |
| 327 | - return new WP_Error( | |
| 328 | - 'deletion_error', | |
| 329 | - __('At least 1 condition needed', 'bitform') | |
| 330 | - ); | |
| 394 | + $set_check_operator = \is_null($check_operator) ? 'in' : $check_operator; | |
| 395 | + $value_type .= ' ( '; | |
| 396 | + $value_index_checker = 0; | |
| 397 | + $value_count = count($value) - 1; | |
| 398 | + foreach ($value as $condKey => $condValue) { | |
| 399 | + $value_type .= $this->getFieldFormat($condValue); | |
| 400 | + $all_values[] = $condValue; | |
| 401 | + if ($value_index_checker < $value_count) { | |
| 402 | + $value_type .= ', '; | |
| 403 | + } | |
| 404 | + $value_index_checker = $value_index_checker + 1; | |
| 405 | + } | |
| 406 | + $value_type .= ' )'; | |
| 331 | 407 | } |
| 332 | - $formatted_conditions = $this->getFormatedCondition($delete_condition); | |
| 333 | - if ($formatted_conditions) { | |
| 334 | - $condition_to_check = $formatted_conditions['conditions']; | |
| 335 | - $all_values = $formatted_conditions['values']; | |
| 336 | - } else { | |
| 337 | - $condition_to_check = null; | |
| 338 | - return new WP_Error( | |
| 339 | - 'deletion_error', | |
| 340 | - __('At least 1 condition needed', 'bitform') | |
| 341 | - ); | |
| 342 | - } | |
| 343 | - $result = $this->app_db->query( | |
| 344 | - $this->app_db->prepare( | |
| 345 | - "DELETE FROM $this->table_name $condition_to_check", | |
| 346 | - $all_values | |
| 347 | - ) | |
| 348 | - ); | |
| 349 | - return $this->getResult($result); | |
| 408 | + } else { | |
| 409 | + $set_check_operator = \is_null($check_operator) ? '=' : $check_operator; | |
| 410 | + $value_type .= $this->getFieldFormat($value); | |
| 411 | + $all_values[] = $value; | |
| 412 | + } | |
| 413 | + $condition_to_check = $condition_to_check . $key . " $set_check_operator " . $value_type; | |
| 414 | + if ($index_checker < $no_condition - 1) { | |
| 415 | + $condition_to_check = $condition_to_check . " $join_operator "; | |
| 416 | + } | |
| 417 | + $index_checker = $index_checker + 1; | |
| 350 | 418 | } |
| 419 | + return [ | |
| 420 | + 'conditions' => $condition_to_check, | |
| 421 | + 'values' => $all_values | |
| 422 | + ]; | |
| 423 | + } | |
| 351 | 424 | |
| 352 | - protected function getFieldFormat($value) | |
| 353 | - { | |
| 354 | - return (gettype($value) == 'integer') ? | |
| 355 | - '%d' : ((gettype($value) == 'double') ? '%f' : '%s'); | |
| 425 | + protected function checkCondition(array $condition) | |
| 426 | + { | |
| 427 | + if (!is_null($condition) && array_keys($condition) === range(0, count($condition) - 1)) { | |
| 428 | + return new WP_Error( | |
| 429 | + 'get_condition', | |
| 430 | + 'Require ASSOC_ARRAY but found N_ARRAY' | |
| 431 | + ); | |
| 356 | 432 | } |
| 433 | + return true; | |
| 434 | + } | |
| 357 | 435 | |
| 358 | - protected function getFormatedCondition($condition, $check_operator = null, $join_operator = ' AND ') | |
| 359 | - { | |
| 360 | - if (\is_null($condition)) { | |
| 361 | - return false; | |
| 362 | - } | |
| 363 | - $no_condition = count($condition); | |
| 364 | - $index_checker = 0; | |
| 365 | - $condition_to_check = ' WHERE '; | |
| 366 | - $all_values = array(); | |
| 367 | - foreach ($condition as $key => $value) { | |
| 368 | - $value_type = ''; | |
| 369 | - if (is_array($value)) { | |
| 370 | - if (isset($value['operator'])) { | |
| 371 | - $set_check_operator = $value['operator']; | |
| 372 | - $value_type .= $this->getFieldFormat($value['value']); | |
| 373 | - $all_values[] = $value['value']; | |
| 374 | - } else { | |
| 375 | - $set_check_operator = \is_null($check_operator) ? 'in' : $check_operator; | |
| 376 | - $value_type .= ' ( '; | |
| 377 | - $value_index_checker = 0; | |
| 378 | - $value_count = count($value) - 1; | |
| 379 | - foreach ($value as $condKey => $condValue) { | |
| 380 | - $value_type .= $this->getFieldFormat($condValue); | |
| 381 | - $all_values[] = $condValue; | |
| 382 | - if ($value_index_checker < $value_count) { | |
| 383 | - $value_type .= ', '; | |
| 384 | - } | |
| 385 | - $value_index_checker = $value_index_checker + 1; | |
| 386 | - } | |
| 387 | - $value_type .= ' )'; | |
| 388 | - } | |
| 389 | - } else { | |
| 390 | - $set_check_operator = \is_null($check_operator) ? '=' : $check_operator; | |
| 391 | - $value_type .= $this->getFieldFormat($value); | |
| 392 | - $all_values[] = $value; | |
| 393 | - } | |
| 394 | - $condition_to_check = $condition_to_check . $key . " $set_check_operator " . $value_type; | |
| 395 | - if ($index_checker < $no_condition - 1) { | |
| 396 | - $condition_to_check = $condition_to_check . " $join_operator "; | |
| 397 | - } | |
| 398 | - $index_checker = $index_checker + 1; | |
| 399 | - } | |
| 400 | - return array( | |
| 401 | - 'conditions' => $condition_to_check, | |
| 402 | - 'values' => $all_values | |
| 403 | - ); | |
| 436 | + protected function execute($sql, $values = null) | |
| 437 | + { | |
| 438 | + if (is_null($values)) { | |
| 439 | + $preparedQuery = $sql; | |
| 440 | + } else { | |
| 441 | + $preparedQuery = $this->app_db->prepare($sql, $values); | |
| 404 | 442 | } |
| 443 | + // echo " Q S " . $preparedQuery . " Q EE"; | |
| 444 | + if (empty($preparedQuery)) { | |
| 445 | + $this->db_response = new WP_Error('null_query', __('prepared query is empty', 'bit-form')); | |
| 446 | + } else { | |
| 447 | + $this->db_response = false !== stripos($preparedQuery, 'DELETE') ? $this->app_db->query($preparedQuery) | |
| 448 | + : $this->app_db->get_results($preparedQuery, OBJECT_K); | |
| 449 | + } | |
| 450 | + // print_r($this->app_db->last_query); | |
| 451 | + return $this; | |
| 452 | + } | |
| 405 | 453 | |
| 406 | - protected function checkCondition(array $condition) | |
| 407 | - { | |
| 408 | - if (!is_null($condition) && array_keys($condition) === range(0, count($condition) - 1)) { | |
| 409 | - return new WP_Error( | |
| 410 | - 'get_condition', | |
| 411 | - 'Require ASSOC_ARRAY but found N_ARRAY' | |
| 412 | - ); | |
| 413 | - } | |
| 414 | - return true; | |
| 454 | + protected function getResult($db_response = null) | |
| 455 | + { | |
| 456 | + $db_response = !empty($this->db_response) ? $this->db_response : $db_response; | |
| 457 | + if (!empty($this->app_db->last_error)) { | |
| 458 | + return new WP_Error('db_error', $this->app_db->last_error); | |
| 415 | 459 | } |
| 416 | - | |
| 417 | - protected function execute($sql, $values = null) | |
| 418 | - { | |
| 419 | - if (is_null($values)) { | |
| 420 | - $preparedQuery = $this->app_db->prepare($sql); | |
| 421 | - } else { | |
| 422 | - $preparedQuery = $this->app_db->prepare($sql, $values); | |
| 423 | - } | |
| 424 | - // echo " Q S " . $preparedQuery . " Q EE"; | |
| 425 | - if (empty($preparedQuery)) { | |
| 426 | - $this->db_response = new WP_Error('null_query', __("prepared query is empty", "bitform")); | |
| 427 | - } else { | |
| 428 | - $this->db_response = stripos($preparedQuery, 'DELETE') !== false ? $this->app_db->query($preparedQuery) | |
| 429 | - : $this->app_db->get_results($preparedQuery, OBJECT_K); | |
| 430 | - } | |
| 431 | - // print_r($this->app_db->last_query); | |
| 432 | - return $this; | |
| 460 | + if (!$db_response) { | |
| 461 | + if ($this->app_db->num_rows > 0) { | |
| 462 | + $response = $this->app_db->num_rows; | |
| 463 | + } | |
| 464 | + if (is_wp_error($db_response)) { | |
| 465 | + $response = $db_response; | |
| 466 | + } | |
| 467 | + $response = new WP_Error('result_empty', __('Result is empty', 'bit-form')); | |
| 468 | + } elseif (is_array($this->app_db->last_result) && !empty($this->app_db->last_result)) { | |
| 469 | + $response = $this->app_db->last_result; | |
| 470 | + } elseif ($this->app_db->insert_id) { | |
| 471 | + $response = $this->app_db->insert_id; | |
| 472 | + } else { | |
| 473 | + $response = $db_response; | |
| 433 | 474 | } |
| 475 | + $this->app_db->flush(); | |
| 476 | + return $response; | |
| 477 | + } | |
| 434 | 478 | |
| 435 | - protected function getResult($db_response = null) | |
| 436 | - { | |
| 437 | - $db_response = !empty($this->db_response) ? $this->db_response : $db_response; | |
| 438 | - if (!empty($this->app_db->last_error)) { | |
| 439 | - return new WP_Error('db_error', $this->app_db->last_error); | |
| 440 | - } | |
| 441 | - if (!$db_response) { | |
| 442 | - if ($this->app_db->num_rows > 0) { | |
| 443 | - $response = $this->app_db->num_rows; | |
| 444 | - } | |
| 445 | - if (is_wp_error($db_response)) { | |
| 446 | - $response = $db_response; | |
| 447 | - } | |
| 448 | - $response = new WP_Error('result_empty', __("Result is empty", "bitform")); | |
| 449 | - } elseif (is_array($this->app_db->last_result) && !empty($this->app_db->last_result)) { | |
| 450 | - $response = $this->app_db->last_result; | |
| 451 | - } elseif ($this->app_db->insert_id) { | |
| 452 | - $response = $this->app_db->insert_id; | |
| 453 | - } else { | |
| 454 | - $response = $db_response; | |
| 455 | - } | |
| 456 | - $this->app_db->flush(); | |
| 457 | - return $response; | |
| 479 | + /** | |
| 480 | + * Get last inserted id | |
| 481 | + * | |
| 482 | + * @return int | |
| 483 | + */ | |
| 484 | + public function lastId() | |
| 485 | + { | |
| 486 | + $sql = "SELECT id FROM {$this->table_name} | |
| 487 | + ORDER BY id DESC LIMIT 1"; | |
| 488 | + $result = $this->execute($sql)->getResult(); | |
| 489 | + if (is_wp_error($result)) { | |
| 490 | + return 0; | |
| 458 | 491 | } |
| 492 | + return $result[0]->id; | |
| 493 | + } | |
| 459 | 494 | } |