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