| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_RAG_Training_File' ) ) : |
| 7 |
|
| 8 |
final class WPSC_RAG_Training_File { |
| 9 |
|
| 10 |
/** |
| 11 |
* Object data in key => val pair. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $data = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Set whether or not current object properties modified |
| 19 |
* |
| 20 |
* @var boolean |
| 21 |
*/ |
| 22 |
private $is_modified = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* Schema for this model |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
public static $schema; |
| 30 |
|
| 31 |
/** |
| 32 |
* Prevent fields to modify |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
public static $prevent_modify; |
| 37 |
|
| 38 |
/** |
| 39 |
* DB object caching |
| 40 |
* |
| 41 |
* @var array |
| 42 |
*/ |
| 43 |
private static $cache = array(); |
| 44 |
|
| 45 |
/** |
| 46 |
* Initialize this class |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public static function init() { |
| 51 |
|
| 52 |
// Apply schema for this model. |
| 53 |
add_action( 'init', array( __CLASS__, 'apply_schema' ), 2 ); |
| 54 |
|
| 55 |
// Get object of this class. |
| 56 |
add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Apply schema for this model |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public static function apply_schema() { |
| 65 |
|
| 66 |
$schema = array( |
| 67 |
'id' => array( |
| 68 |
'has_ref' => false, |
| 69 |
'ref_class' => '', |
| 70 |
'has_multiple_val' => false, |
| 71 |
), |
| 72 |
'status' => array( |
| 73 |
'has_ref' => false, |
| 74 |
'ref_class' => '', |
| 75 |
'has_multiple_val' => false, |
| 76 |
), |
| 77 |
'provider' => array( |
| 78 |
'has_ref' => false, |
| 79 |
'ref_class' => '', |
| 80 |
'has_multiple_val' => false, |
| 81 |
), |
| 82 |
'source' => array( |
| 83 |
'has_ref' => false, |
| 84 |
'ref_class' => '', |
| 85 |
'has_multiple_val' => false, |
| 86 |
), |
| 87 |
'source_id' => array( |
| 88 |
'has_ref' => false, |
| 89 |
'ref_class' => '', |
| 90 |
'has_multiple_val' => false, |
| 91 |
), |
| 92 |
'doc_source' => array( |
| 93 |
'has_ref' => false, |
| 94 |
'ref_class' => '', |
| 95 |
'has_multiple_val' => false, |
| 96 |
), |
| 97 |
'name' => array( |
| 98 |
'has_ref' => false, |
| 99 |
'ref_class' => '', |
| 100 |
'has_multiple_val' => false, |
| 101 |
), |
| 102 |
'file_path' => array( |
| 103 |
'has_ref' => false, |
| 104 |
'ref_class' => '', |
| 105 |
'has_multiple_val' => false, |
| 106 |
), |
| 107 |
'provider_file_id' => array( |
| 108 |
'has_ref' => false, |
| 109 |
'ref_class' => '', |
| 110 |
'has_multiple_val' => false, |
| 111 |
), |
| 112 |
'meta_data' => array( |
| 113 |
'has_ref' => false, |
| 114 |
'ref_class' => '', |
| 115 |
'has_multiple_val' => false, |
| 116 |
), |
| 117 |
'post_updated_on' => array( |
| 118 |
'has_ref' => true, |
| 119 |
'ref_class' => 'datetime', |
| 120 |
'has_multiple_val' => false, |
| 121 |
), |
| 122 |
'date_updated' => array( |
| 123 |
'has_ref' => true, |
| 124 |
'ref_class' => 'datetime', |
| 125 |
'has_multiple_val' => false, |
| 126 |
), |
| 127 |
'date_created' => array( |
| 128 |
'has_ref' => true, |
| 129 |
'ref_class' => 'datetime', |
| 130 |
'has_multiple_val' => false, |
| 131 |
), |
| 132 |
); |
| 133 |
|
| 134 |
self::$schema = $schema; |
| 135 |
|
| 136 |
// Prevent modify. |
| 137 |
$prevent_modify = array( 'id' ); |
| 138 |
self::$prevent_modify = apply_filters( 'wpsc_ai_training_prevent_modify', $prevent_modify ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Constructor function |
| 143 |
* |
| 144 |
* @param int $id - optional record id. |
| 145 |
*/ |
| 146 |
public function __construct( $id = 0 ) { |
| 147 |
|
| 148 |
global $wpdb; |
| 149 |
|
| 150 |
$id = intval( $id ); |
| 151 |
|
| 152 |
if ( isset( self::$cache[ $id ] ) ) { |
| 153 |
$this->data = self::$cache[ $id ]->data; |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
if ( $id > 0 ) { |
| 158 |
|
| 159 |
$ai_training = $wpdb->get_row( |
| 160 |
$wpdb->prepare( |
| 161 |
"SELECT * FROM {$wpdb->prefix}psmsc_ai_training WHERE id = %d", |
| 162 |
$id |
| 163 |
), |
| 164 |
ARRAY_A |
| 165 |
); |
| 166 |
if ( ! is_array( $ai_training ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
foreach ( $ai_training as $key => $val ) { |
| 171 |
$this->data[ $key ] = $val !== null ? $val : ''; |
| 172 |
} |
| 173 |
|
| 174 |
self::$cache[ $id ] = $this; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Magic get function to use with object arrow function |
| 180 |
* |
| 181 |
* @param mixed $var_name - (Required) property slug. |
| 182 |
* @return mixed |
| 183 |
*/ |
| 184 |
public function __get( $var_name ) { |
| 185 |
|
| 186 |
if ( ! isset( $this->data[ $var_name ] ) || |
| 187 |
$this->data[ $var_name ] == null || |
| 188 |
$this->data[ $var_name ] == '' |
| 189 |
) { |
| 190 |
return self::$schema[ $var_name ]['has_multiple_val'] ? array() : ''; |
| 191 |
} |
| 192 |
|
| 193 |
if ( self::$schema[ $var_name ]['has_multiple_val'] ) { |
| 194 |
|
| 195 |
$response = array(); |
| 196 |
$values = $this->data[ $var_name ] ? explode( '|', $this->data[ $var_name ] ) : array(); |
| 197 |
foreach ( $values as $val ) { |
| 198 |
$response[] = self::$schema[ $var_name ]['has_ref'] ? |
| 199 |
WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $val ) : |
| 200 |
$val; |
| 201 |
} |
| 202 |
return $response; |
| 203 |
|
| 204 |
} else { |
| 205 |
|
| 206 |
return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ? |
| 207 |
WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) : |
| 208 |
$this->data[ $var_name ]; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Magic function to use setting object field with arrow function |
| 214 |
* |
| 215 |
* @param mixed $var_name - (Required) property slug. |
| 216 |
* @param mixed $value - (Required) value to set for a property. |
| 217 |
* @return void |
| 218 |
*/ |
| 219 |
public function __set( $var_name, $value ) { |
| 220 |
|
| 221 |
if ( |
| 222 |
! isset( $this->data[ $var_name ] ) || |
| 223 |
in_array( $var_name, self::$prevent_modify ) |
| 224 |
) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
$data_val = ''; |
| 229 |
if ( self::$schema[ $var_name ]['has_multiple_val'] && is_array( $value ) ) { |
| 230 |
|
| 231 |
$data_vals = array_map( |
| 232 |
fn( $val ) => is_object( $val ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $val ) : $val, |
| 233 |
$value |
| 234 |
); |
| 235 |
|
| 236 |
$data_val = $data_vals ? implode( '|', $data_vals ) : ''; |
| 237 |
|
| 238 |
} else { |
| 239 |
|
| 240 |
$data_val = is_object( $value ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) : $value; |
| 241 |
} |
| 242 |
|
| 243 |
if ( $this->data[ $var_name ] == $data_val ) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
$this->data[ $var_name ] = $data_val; |
| 248 |
$this->is_modified = true; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Save changes made |
| 253 |
* |
| 254 |
* @return boolean |
| 255 |
*/ |
| 256 |
public function save() { |
| 257 |
|
| 258 |
global $wpdb; |
| 259 |
|
| 260 |
if ( ! $this->is_modified ) { |
| 261 |
return true; |
| 262 |
} |
| 263 |
|
| 264 |
if ( empty( $this->data['id'] ) ) { |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
$data = $this->data; |
| 269 |
|
| 270 |
unset( $data['id'] ); |
| 271 |
$success = $wpdb->update( |
| 272 |
$wpdb->prefix . 'psmsc_ai_training', |
| 273 |
$data, |
| 274 |
array( 'id' => $this->data['id'] ) |
| 275 |
); |
| 276 |
|
| 277 |
$this->is_modified = false; |
| 278 |
self::$cache[ $this->id ] = $this; |
| 279 |
return $success ? true : false; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Insert new record |
| 284 |
* |
| 285 |
* @param array $data - insert data. |
| 286 |
* @return WPSC_RAG_Training_File |
| 287 |
*/ |
| 288 |
public static function insert( $data ) { |
| 289 |
|
| 290 |
global $wpdb; |
| 291 |
|
| 292 |
$success = $wpdb->insert( |
| 293 |
$wpdb->prefix . 'psmsc_ai_training', |
| 294 |
$data |
| 295 |
); |
| 296 |
|
| 297 |
if ( ! $success ) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
|
| 301 |
$cl = new WPSC_RAG_Training_File( $wpdb->insert_id ); |
| 302 |
return $cl; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Delete record of given ID |
| 307 |
* |
| 308 |
* @param WPSC_RAG_Training_File $ai_training - AI training object. |
| 309 |
* @return boolean |
| 310 |
*/ |
| 311 |
public static function destroy( $ai_training ) { |
| 312 |
|
| 313 |
global $wpdb; |
| 314 |
|
| 315 |
$success = $wpdb->delete( |
| 316 |
$wpdb->prefix . 'psmsc_ai_training', |
| 317 |
array( 'id' => $ai_training->id ) |
| 318 |
); |
| 319 |
if ( ! $success ) { |
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
unset( self::$cache[ $ai_training->id ] ); |
| 324 |
return true; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Set data to create new object using direct data. Used in find method |
| 329 |
* |
| 330 |
* @param array $data - object data to set. |
| 331 |
* @return void |
| 332 |
*/ |
| 333 |
private function set_data( $data ) { |
| 334 |
|
| 335 |
foreach ( $data as $var_name => $val ) { |
| 336 |
$this->data[ $var_name ] = $val !== null ? $val : ''; |
| 337 |
} |
| 338 |
self::$cache[ $this->id ] = $this; |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Find records based on given filters |
| 343 |
* |
| 344 |
* @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc. |
| 345 |
* @param boolean $is_object - return data as array or object. Default object. |
| 346 |
* @return mixed |
| 347 |
*/ |
| 348 |
public static function find( $filter = array(), $is_object = true ) { |
| 349 |
|
| 350 |
global $wpdb; |
| 351 |
|
| 352 |
$filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 0; |
| 353 |
$filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 1; |
| 354 |
$filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'id'; |
| 355 |
$filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC'; |
| 356 |
|
| 357 |
$sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_ai_training ait '; |
| 358 |
$where = self::get_where( $filter ); |
| 359 |
|
| 360 |
$order = WPSC_Functions::parse_order( $filter ); |
| 361 |
$limit = WPSC_Functions::parse_limit( $filter ); |
| 362 |
|
| 363 |
$sql = $sql . $where . $order . $limit; |
| 364 |
$results = $wpdb->get_results( $sql, ARRAY_A ); |
| 365 |
|
| 366 |
// total results. |
| 367 |
$sql = 'SELECT count(DISTINCT ait.id) FROM ' . $wpdb->prefix . 'psmsc_ai_training ait '; |
| 368 |
$total_items = $wpdb->get_var( $sql . $where ); |
| 369 |
|
| 370 |
$response = WPSC_Functions::parse_response( $results, $total_items, $filter ); |
| 371 |
|
| 372 |
// Return array. |
| 373 |
if ( ! $is_object ) { |
| 374 |
return $response; |
| 375 |
} |
| 376 |
|
| 377 |
// create and return array of objects. |
| 378 |
$temp_results = array(); |
| 379 |
foreach ( $response['results'] as $cl ) { |
| 380 |
|
| 381 |
$ob = new WPSC_RAG_Training_File(); |
| 382 |
$data = array(); |
| 383 |
foreach ( $cl as $key => $val ) { |
| 384 |
$data[ $key ] = $val; |
| 385 |
} |
| 386 |
$ob->set_data( $data ); |
| 387 |
$temp_results[] = $ob; |
| 388 |
} |
| 389 |
$response['results'] = $temp_results; |
| 390 |
|
| 391 |
return $response; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Get where for find method |
| 396 |
* |
| 397 |
* @param array $filter - user filter. |
| 398 |
* @return array |
| 399 |
*/ |
| 400 |
private static function get_where( $filter ) { |
| 401 |
$where = array( '1=1' ); |
| 402 |
|
| 403 |
// Load meta filters. |
| 404 |
$meta_query = isset( $filter['meta_query'] ) ? $filter['meta_query'] : array(); |
| 405 |
if ( $meta_query ) { |
| 406 |
$where[] = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query ); |
| 407 |
} |
| 408 |
|
| 409 |
global $wpdb; |
| 410 |
$search = WPSC_Functions::get_filter_search_str( $filter ); |
| 411 |
if ( $search ) { |
| 412 |
$escaped_search = esc_sql( $wpdb->esc_like( $search ) ); |
| 413 |
$like = '%' . $escaped_search . '%'; |
| 414 |
|
| 415 |
$search_query = array(); |
| 416 |
|
| 417 |
// AI training table cols. |
| 418 |
foreach ( self::$schema as $slug => $schema ) { |
| 419 |
if ( ! $schema['has_ref'] && $slug != 'id' ) { |
| 420 |
$search_query[] = $wpdb->prepare( 'CONVERT(ait.' . $slug . ' USING utf8) LIKE %s', $like ); |
| 421 |
} |
| 422 |
} |
| 423 |
$where[] = implode( ' OR ', $search_query ); |
| 424 |
} |
| 425 |
|
| 426 |
return 'WHERE (' . implode( ') AND (', $where ) . ') '; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Load current class to ref classes |
| 431 |
* |
| 432 |
* @param array $classes - ref classes array. |
| 433 |
* @return array |
| 434 |
*/ |
| 435 |
public static function load_ref_class( $classes ) { |
| 436 |
|
| 437 |
$classes['wpsc_ai_training'] = array( |
| 438 |
'class' => __CLASS__, |
| 439 |
'save-key' => 'id', |
| 440 |
); |
| 441 |
return $classes; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Count total records based on given filters |
| 446 |
* |
| 447 |
* @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc. |
| 448 |
* @return int |
| 449 |
*/ |
| 450 |
public static function count( $filter = array() ) { |
| 451 |
|
| 452 |
global $wpdb; |
| 453 |
|
| 454 |
$sql = 'SELECT COUNT(DISTINCT ait.id) FROM ' . $wpdb->prefix . 'psmsc_ai_training ait '; |
| 455 |
$where = self::get_where( $filter ); |
| 456 |
|
| 457 |
$sql = $sql . $where; |
| 458 |
|
| 459 |
return (int) $wpdb->get_var( $sql ); |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Pluck specific column values based on given filters |
| 464 |
* |
| 465 |
* @param string $column - column name to pluck. |
| 466 |
* @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc. |
| 467 |
* @return array |
| 468 |
*/ |
| 469 |
public static function pluck( $column, $filter = array() ) { |
| 470 |
|
| 471 |
global $wpdb; |
| 472 |
|
| 473 |
if ( empty( $column ) ) { |
| 474 |
return array(); |
| 475 |
} |
| 476 |
|
| 477 |
// Validate column using schema. |
| 478 |
if ( ! array_key_exists( $column, self::$schema ) ) { |
| 479 |
return array(); |
| 480 |
} |
| 481 |
|
| 482 |
$table = $wpdb->prefix . 'psmsc_ai_training'; |
| 483 |
|
| 484 |
$filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'id'; |
| 485 |
$filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC'; |
| 486 |
|
| 487 |
$sql = "SELECT DISTINCT {$column} FROM {$table} ait "; |
| 488 |
$where = self::get_where( $filter ); |
| 489 |
$order = WPSC_Functions::parse_order( $filter ); |
| 490 |
$limit = WPSC_Functions::parse_limit( $filter ); |
| 491 |
|
| 492 |
$sql = $sql . $where . $order . $limit; |
| 493 |
|
| 494 |
return $wpdb->get_col( $sql ); |
| 495 |
} |
| 496 |
|
| 497 |
/** |
| 498 |
* Safe delete record of given ID. If provider file id is available then update status to delete otherwise hard delete. |
| 499 |
* |
| 500 |
* @param WPSC_RAG_Training_File $ai_training_data - AI training object. |
| 501 |
* @return boolean |
| 502 |
*/ |
| 503 |
public static function safe_delete( $ai_training_data ) { |
| 504 |
|
| 505 |
if ( ! $ai_training_data->provider_file_id ) { |
| 506 |
return self::destroy( $ai_training_data ); |
| 507 |
} else { |
| 508 |
$ai_training_data->status = WPSC_PS_AIT_Status::DELETE; |
| 509 |
$ai_training_data->save(); |
| 510 |
return true; |
| 511 |
} |
| 512 |
return false; |
| 513 |
} |
| 514 |
} |
| 515 |
endif; |
| 516 |
WPSC_RAG_Training_File::init(); |
| 517 |
|