| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Priority' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Priority { |
| 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 = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Prevent fields to modify |
| 33 |
* |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
public static $prevent_modify = array(); |
| 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 |
'name' => array( |
| 73 |
'has_ref' => false, |
| 74 |
'ref_class' => '', |
| 75 |
'has_multiple_val' => false, |
| 76 |
), |
| 77 |
'color' => array( |
| 78 |
'has_ref' => false, |
| 79 |
'ref_class' => '', |
| 80 |
'has_multiple_val' => false, |
| 81 |
), |
| 82 |
'bg_color' => array( |
| 83 |
'has_ref' => false, |
| 84 |
'ref_class' => '', |
| 85 |
'has_multiple_val' => false, |
| 86 |
), |
| 87 |
'load_order' => array( |
| 88 |
'has_ref' => false, |
| 89 |
'ref_class' => '', |
| 90 |
'has_multiple_val' => false, |
| 91 |
), |
| 92 |
); |
| 93 |
self::$schema = apply_filters( 'wpsc_priority_schema', $schema ); |
| 94 |
|
| 95 |
// Prevent modify. |
| 96 |
$prevent_modify = array( 'id' ); |
| 97 |
self::$prevent_modify = apply_filters( 'wpsc_priority_prevent_modify', $prevent_modify ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Model constructor |
| 102 |
* |
| 103 |
* @param int $id - Optional. Data record id to retrive object for. |
| 104 |
*/ |
| 105 |
public function __construct( $id = 0 ) { |
| 106 |
|
| 107 |
global $wpdb; |
| 108 |
|
| 109 |
$id = intval( $id ); |
| 110 |
|
| 111 |
if ( isset( self::$cache[ $id ] ) ) { |
| 112 |
$this->data = self::$cache[ $id ]->data; |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
if ( $id > 0 ) { |
| 117 |
|
| 118 |
$priority = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}psmsc_priorities WHERE id = " . $id, ARRAY_A ); |
| 119 |
if ( ! is_array( $priority ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
foreach ( $priority as $key => $val ) { |
| 124 |
|
| 125 |
if ( $key == 'name' ) { |
| 126 |
|
| 127 |
$this->data[ $key ] = $val !== null ? WPSC_Translations::get( 'wpsc-priority-' . $priority['id'], stripslashes( $val ) ) : ''; |
| 128 |
|
| 129 |
} else { |
| 130 |
|
| 131 |
$this->data[ $key ] = $val !== null ? $val : ''; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
self::$cache[ $id ] = $this; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Convert object into an array |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
public function to_array() { |
| 145 |
|
| 146 |
return $this->data; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Magic get function to use with object arrow function |
| 151 |
* |
| 152 |
* @param string $var_name - variable name. |
| 153 |
* @return mixed |
| 154 |
*/ |
| 155 |
public function __get( $var_name ) { |
| 156 |
|
| 157 |
if ( ! isset( $this->data[ $var_name ] ) || |
| 158 |
$this->data[ $var_name ] == null || |
| 159 |
$this->data[ $var_name ] == '' |
| 160 |
) { |
| 161 |
return self::$schema[ $var_name ]['has_multiple_val'] ? array() : ''; |
| 162 |
} |
| 163 |
|
| 164 |
if ( self::$schema[ $var_name ]['has_multiple_val'] ) { |
| 165 |
|
| 166 |
$response = array(); |
| 167 |
$values = $this->data[ $var_name ] ? explode( '|', $this->data[ $var_name ] ) : array(); |
| 168 |
foreach ( $values as $val ) { |
| 169 |
$response[] = self::$schema[ $var_name ]['has_ref'] ? |
| 170 |
WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $val ) : |
| 171 |
$val; |
| 172 |
} |
| 173 |
return $response; |
| 174 |
|
| 175 |
} else { |
| 176 |
|
| 177 |
return self::$schema[ $var_name ]['has_ref'] && $this->data[ $var_name ] ? |
| 178 |
WPSC_Functions::get_object( self::$schema[ $var_name ]['ref_class'], $this->data[ $var_name ] ) : |
| 179 |
$this->data[ $var_name ]; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Magic function to use setting object field with arrow function |
| 185 |
* |
| 186 |
* @param string $var_name - (Required) property slug. |
| 187 |
* @param mixed $value - (Required) value to set for a property. |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function __set( $var_name, $value ) { |
| 191 |
|
| 192 |
if ( |
| 193 |
! isset( $this->data[ $var_name ] ) || |
| 194 |
in_array( $var_name, self::$prevent_modify ) |
| 195 |
) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
$data_val = ''; |
| 200 |
if ( self::$schema[ $var_name ]['has_multiple_val'] ) { |
| 201 |
|
| 202 |
$data_vals = array_map( |
| 203 |
fn( $val ) => is_object( $val ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $val ) : $val, |
| 204 |
$value |
| 205 |
); |
| 206 |
|
| 207 |
$data_val = $data_vals ? implode( '|', $data_vals ) : ''; |
| 208 |
|
| 209 |
} else { |
| 210 |
|
| 211 |
$data_val = is_object( $value ) ? WPSC_Functions::set_object( self::$schema[ $var_name ]['ref_class'], $value ) : $value; |
| 212 |
} |
| 213 |
|
| 214 |
if ( $this->data[ $var_name ] == $data_val ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
$this->data[ $var_name ] = $data_val; |
| 219 |
$this->is_modified = true; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Save changes made |
| 224 |
* |
| 225 |
* @return boolean |
| 226 |
*/ |
| 227 |
public function save() { |
| 228 |
|
| 229 |
global $wpdb; |
| 230 |
|
| 231 |
if ( ! $this->is_modified ) { |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
$data = $this->data; |
| 236 |
$success = true; |
| 237 |
|
| 238 |
if ( ! isset( $data['id'] ) ) { |
| 239 |
|
| 240 |
$pr = self::insert( $data ); |
| 241 |
if ( $pr ) { |
| 242 |
$this->data = $pr->data; |
| 243 |
$success = true; |
| 244 |
} else { |
| 245 |
$success = false; |
| 246 |
} |
| 247 |
} else { |
| 248 |
|
| 249 |
unset( $data['id'] ); |
| 250 |
$success = $wpdb->update( |
| 251 |
$wpdb->prefix . 'psmsc_priorities', |
| 252 |
$data, |
| 253 |
array( 'id' => $this->data['id'] ) |
| 254 |
); |
| 255 |
|
| 256 |
// Set string translation. |
| 257 |
WPSC_Translations::add( 'wpsc-priority-' . $this->data['id'], $this->data['name'] ); |
| 258 |
} |
| 259 |
$this->is_modified = false; |
| 260 |
self::$cache[ $this->id ] = $this; |
| 261 |
return $success ? true : false; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Insert new record |
| 266 |
* |
| 267 |
* @param array $data - insert data. |
| 268 |
* @return WPSC_Priority |
| 269 |
*/ |
| 270 |
public static function insert( $data ) { |
| 271 |
|
| 272 |
global $wpdb; |
| 273 |
|
| 274 |
if ( ! isset( $data['load_order'] ) ) { |
| 275 |
$max_order = $wpdb->get_var( "SELECT MAX(load_order) FROM {$wpdb->prefix}psmsc_priorities" ); |
| 276 |
$data['load_order'] = intval( $max_order ) + 1; |
| 277 |
} |
| 278 |
|
| 279 |
$success = $wpdb->insert( |
| 280 |
$wpdb->prefix . 'psmsc_priorities', |
| 281 |
$data |
| 282 |
); |
| 283 |
|
| 284 |
if ( ! $success ) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
$id = $wpdb->insert_id; |
| 289 |
|
| 290 |
// string translation. |
| 291 |
WPSC_Translations::add( 'wpsc-priority-' . $id, $data['name'] ); |
| 292 |
|
| 293 |
$priority = new WPSC_Priority( $id ); |
| 294 |
|
| 295 |
return $priority; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Delete record of given ID |
| 300 |
* |
| 301 |
* @param WPSC_Priority $priority - priority object. |
| 302 |
* @return boolean |
| 303 |
*/ |
| 304 |
public static function destroy( $priority ) { |
| 305 |
|
| 306 |
global $wpdb; |
| 307 |
|
| 308 |
$success = $wpdb->delete( |
| 309 |
$wpdb->prefix . 'psmsc_priorities', |
| 310 |
array( 'id' => $priority->id ) |
| 311 |
); |
| 312 |
if ( ! $success ) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
unset( self::$cache[ $priority->id ] ); |
| 317 |
|
| 318 |
// remove string translations. |
| 319 |
WPSC_Translations::remove( 'wpsc-priority-' . $priority->id ); |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Set data to create new object using direct data. Used in find method |
| 325 |
* |
| 326 |
* @param array $data - data to set for object. |
| 327 |
* @return void |
| 328 |
*/ |
| 329 |
private function set_data( $data ) { |
| 330 |
|
| 331 |
foreach ( $data as $var_name => $val ) { |
| 332 |
|
| 333 |
if ( $var_name == 'name' ) { |
| 334 |
|
| 335 |
$this->data[ $var_name ] = $val !== null ? WPSC_Translations::get( 'wpsc-priority-' . $data['id'], stripslashes( $val ) ) : ''; |
| 336 |
|
| 337 |
} else { |
| 338 |
|
| 339 |
$this->data[ $var_name ] = $val !== null ? $val : ''; |
| 340 |
} |
| 341 |
} |
| 342 |
self::$cache[ $this->id ] = $this; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Find records based on given filters |
| 347 |
* |
| 348 |
* @param array $filter - array containing array items like search, where, orderby, order, page_no, items_per_page, etc. |
| 349 |
* @param boolean $is_object - return data as array or object. Default object. |
| 350 |
* @return mixed |
| 351 |
*/ |
| 352 |
public static function find( $filter = array(), $is_object = true ) { |
| 353 |
|
| 354 |
global $wpdb; |
| 355 |
|
| 356 |
$sql = 'SELECT * FROM ' . $wpdb->prefix . 'psmsc_priorities '; |
| 357 |
$where = self::get_where( $filter ); |
| 358 |
|
| 359 |
$filter['items_per_page'] = isset( $filter['items_per_page'] ) ? $filter['items_per_page'] : 0; |
| 360 |
$filter['page_no'] = isset( $filter['page_no'] ) ? $filter['page_no'] : 0; |
| 361 |
$filter['orderby'] = isset( $filter['orderby'] ) ? $filter['orderby'] : 'load_order'; |
| 362 |
$filter['order'] = isset( $filter['order'] ) ? $filter['order'] : 'ASC'; |
| 363 |
|
| 364 |
$order = WPSC_Functions::parse_order( $filter ); |
| 365 |
|
| 366 |
$sql = $sql . $where . $order; |
| 367 |
$results = $wpdb->get_results( $sql, ARRAY_A ); |
| 368 |
|
| 369 |
// total results. |
| 370 |
$sql = 'SELECT count(id) FROM ' . $wpdb->prefix . 'psmsc_priorities '; |
| 371 |
$total_items = $wpdb->get_var( $sql . $where ); |
| 372 |
|
| 373 |
$response = WPSC_Functions::parse_response( $results, $total_items, $filter ); |
| 374 |
|
| 375 |
// Return array. |
| 376 |
if ( ! $is_object ) { |
| 377 |
return $response; |
| 378 |
} |
| 379 |
|
| 380 |
// create and return array of objects. |
| 381 |
$temp_results = array(); |
| 382 |
foreach ( $response['results'] as $priority ) { |
| 383 |
|
| 384 |
$ob = new WPSC_Priority(); |
| 385 |
$data = array(); |
| 386 |
foreach ( $priority as $key => $val ) { |
| 387 |
$data[ $key ] = $val; |
| 388 |
} |
| 389 |
$ob->set_data( $data ); |
| 390 |
$temp_results[] = $ob; |
| 391 |
} |
| 392 |
$response['results'] = $temp_results; |
| 393 |
|
| 394 |
return $response; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Get where for find method |
| 399 |
* |
| 400 |
* @param array $filter - user filter. |
| 401 |
* @return array |
| 402 |
*/ |
| 403 |
private static function get_where( $filter ) { |
| 404 |
|
| 405 |
$where = ''; |
| 406 |
|
| 407 |
// Set user defined filters. |
| 408 |
$meta_query = isset( $filter['meta_query'] ) && $filter['meta_query'] ? $filter['meta_query'] : array(); |
| 409 |
$meta_query = apply_filters( 'wpsc_priority_meta_query', $meta_query, $filter ); |
| 410 |
if ( $meta_query ) { |
| 411 |
|
| 412 |
$meta_query = WPSC_Functions::parse_user_filters( __CLASS__, $meta_query ); |
| 413 |
$where = $meta_query . ' '; |
| 414 |
} |
| 415 |
|
| 416 |
return $where ? 'WHERE ' . $where : ''; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Load current class to reference classes |
| 421 |
* |
| 422 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 423 |
* @return array |
| 424 |
*/ |
| 425 |
public static function load_ref_class( $classes ) { |
| 426 |
|
| 427 |
$classes['wpsc_priority'] = array( |
| 428 |
'class' => __CLASS__, |
| 429 |
'save-key' => 'id', |
| 430 |
); |
| 431 |
return $classes; |
| 432 |
} |
| 433 |
} |
| 434 |
endif; |
| 435 |
|
| 436 |
WPSC_Priority::init(); |
| 437 |
|