LLMS_Abstract_Database_Store

WPDB database interactions abstract class


Source Source

File: includes/abstracts/llms.abstract.database.store.php

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
abstract class LLMS_Abstract_Database_Store {
 
    /**
     * The Database ID of the record
     *
     * @var int
     */
    protected $id = null;
 
    /**
     * Object properties
     *
     * @var array
     */
    private $data = array();
 
    /**
     * Column name of the record's "created" date
     *
     * This can be set to an empty string if the extending
     * class does not utilize or require created date storage.
     *
     * @var string
     */
    protected $date_created = 'created';
 
    /**
     * Column name of the record's "updated" date
     *
     * This can be set to an empty string if the extending
     * class does not utilize or require updated date storage.
     *
     * @var string
     */
    protected $date_updated = 'updated';
 
    /**
     * Array of table column name => format
     *
     * @var array
     */
    protected $columns = array();
 
    /**
     * Primary Key column name => format
     *
     * @var array
     */
    protected $primary_key = array(
        'id' => '%d',
    );
 
    /**
     * Database Table Name
     *
     * @var string
     */
    protected $table = '';
 
    /**
     * Database Table Prefix
     *
     * @var string
     */
    protected $table_prefix = 'lifterlms_';
 
    /**
     * The record type
     *
     * Used for filters/actions.
     *
     * This is a placeholder which should be redefined in any extending classes.
     *
     * @var string
     */
    protected $type = '_db_record_';
 
    /**
     * Constructor
     *
     * @since 3.14.0
     * @since 3.21.0 Unknown.
     *
     * @return void
     */
    public function __construct() {
 
        if ( ! $this->id ) {
 
            // If created dates supported, add current time to the data on construction.
            if ( $this->date_created ) {
                $this->set( $this->date_created, llms_current_time( 'mysql' ), false );
            }
 
            if ( $this->date_updated ) {
                $this->set( $this->date_updated, llms_current_time( 'mysql' ), false );
            }
        }
 
    }
 
    /**
     * Get object data
     *
     * @since 3.14.0
     *
     * @param string $key Key to retrieve.
     * @return mixed
     */
    public function __get( $key ) {
        return $this->data[ $key ];
    }
 
    /**
     * Determine if the item exists in the database
     *
     * @since 3.14.7
     * @since 3.15.0 Unknown.
     *
     * @return boolean
     */
    public function exists() {
 
        if ( $this->primary_key ) {
            return $this->read( $this->get_primary_key() ) ? true : false;
        }
 
        return false;
 
    }
 
    /**
     * Get object data
     *
     * @since 3.14.0
     * @since 3.16.0 Unknown.
     * @since 3.36.0 Prevent undefined index error when attempting to retrieve an unset value from an unsaved object.
     *
     * @param string  $key   Key to retrieve.
     * @param boolean $cache If true, save data to to the object for future gets.
     * @return mixed
     */
    public function get( $key, $cache = true ) {
 
        $key_exists = isset( $this->data[ $key ] );
        if ( ! $key_exists && $this->id ) {
            $res = $this->read( $key )[ $key ];
            if ( $cache ) {
                $this->set( $key, $res );
            }
            return $res;
        }
        return $key_exists ? $this->$key : null;
 
    }
 
    /**
     * Set object data
     *
     * @since 3.14.0
     *
     * @param string $key Column name.
     * @param mixed  $val Column value.
     * @return void
     */
    public function __set( $key, $val ) {
        $this->data[ $key ] = $val;
    }
 
    /**
     * General setter
     *
     * @since 3.14.0
     * @since 3.21.0 Unknown.
     *
     * @param string  $key  Column name.
     * @param mixed   $val  Column value.
     * @param boolean $save If true, immediately persists to database.
     * @return LLMS_Abstract_Database_Store Instance of the current object, useful for chaining.
     */
    public function set( $key, $val, $save = false ) {
 
        $this->$key = $val;
        if ( $save ) {
            $update = array(
                $key => $val,
            );
            // If update date supported, add an updated date.
            if ( $this->date_updated ) {
                $update[ $this->date_updated ] = llms_current_time( 'mysql' );
            }
            $this->update( $update );
        }
 
        return $this;
 
    }
 
    /**
     * Setup an object with an array of data
     *
     * @since 3.14.0
     * @since 3.33.0 Return self for chaining instead of void.
     *
     * @param array $data key => val
     * @return LLMS_Abstract_Database_Store Instance of the current object, useful for chaining.
     */
    public function setup( $data ) {
 
        foreach ( $data as $key => $val ) {
            $this->set( $key, $val, false );
        }
 
        return $this;
 
    }
 
    /**
     * Create the item in the database
     *
     * @since 3.14.0
     * @since 3.24.0 Unknown.
     * @since 4.3.0 Added deprecated hook call to `llms__created` action to preserve backwards compatibility.
     * @since 6.0.0 Removed deprecated `llms__created` action hook.
     *
     * @return int|false Record ID on success, false on error or when there's nothing to save.
     */
    private function create() {
 
        if ( ! $this->data ) {
            return false;
        }
 
        global $wpdb;
        $format = array_map( array( $this, 'get_column_format' ), array_keys( $this->data ) );
        $res    = $wpdb->insert( $this->get_table(), $this->data, $format ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        if ( 1 === $res ) {
 
            $this->id = $wpdb->insert_id;
 
            /**
             * Fires when a new database record is created.
             *
             * The dynamic portion of this hook, `$this->type`, refers to the record type.
             *
             * @since Unknown.
             *
             * @param int                          $id  Record ID.
             * @param LLMS_Abstract_Database_Store $obj Instance of the record object.
             */
            do_action( "llms_{$this->type}_created", $this->id, $this );
 
            return $this->id;
        }
        return false;
 
    }
 
    /**
     * Delete the object from the database
     *
     * @since 3.14.0
     * @since 3.24.0 Unknown.
     * @since 4.3.0 Added deprecated hook call to `llms__deleted` action to preserve backwards compatibility.
     * @since 6.0.0 Removed deprecated `llms__deleted` action hook.
     *
     * @return boolean `true` on success, `false` otherwise.
     */
    public function delete() {
 
        if ( ! $this->id ) {
            return false;
        }
 
        $id = $this->id;
        global $wpdb;
        $where = array_combine( array_keys( $this->primary_key ), array( $this->id ) );
        $res   = $wpdb->delete( $this->get_table(), $where, array_values( $this->primary_key ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        if ( $res ) {
            $this->id   = null;
            $this->data = array();
 
            /**
             * Fires when a new database record is created.
             *
             * The dynamic portion of this hook, `$this->type`, refers to the record type.
             *
             * @since Unknown.
             *
             * @param int                          $id  Record ID.
             * @param LLMS_Abstract_Database_Store $obj Instance of the record object.
             */
            do_action( "llms_{$this->type}_deleted", $id, $this );
 
            return true;
        }
        return false;
 
    }
 
    /**
     * Read object data from the database
     *
     * @since 3.14.0
     *
     * @param string[]|string $keys Key name (or array of keys) to retrieve from the database.
     * @return array|false Returns a key=>val array of data or `false` when record not found.
     */
    private function read( $keys ) {
 
        global $wpdb;
        if ( is_array( $keys ) ) {
            $keys = implode( ', ', $keys );
        }
        $res = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            $wpdb->prepare( "SELECT {$keys} FROM {$this->get_table()} WHERE {$this->get_primary_key()} = %d", $this->id ), // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- This query is safe.
            ARRAY_A
        );
        return ! $res ? false : $res;
 
    }
 
    /**
     * Update object data in the database
     *
     * @since 3.14.0
     * @since 3.24.0 Unknown.
     * @since 4.3.0 Added deprecated hook call to `llms__updated` action to preserve backwards compatibility.
     * @since 6.0.0 Removed deprecated `llms__updated` action hook.
     *
     * @param array $data Data to update as key=>val.
     * @return boolean
     */
    private function update( $data ) {
 
        global $wpdb;
        $format = array_map( array( $this, 'get_column_format' ), array_keys( $data ) );
        $where  = array_combine( array_keys( $this->primary_key ), array( $this->id ) );
        $res    = $wpdb->update( $this->get_table(), $data, $where, $format, array_values( $this->primary_key ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        if ( $res ) {
 
            /**
             * Fires when a new database record is updated.
             *
             * The dynamic portion of this hook, `$this->type`, refers to the record type.
             *
             * @since Unknown.
             *
             * @param int                          $id  Record ID.
             * @param LLMS_Abstract_Database_Store $obj Instance of the record object.
             */
            do_action( "llms_{$this->type}_updated", $this->id, $this );
 
            return true;
        }
        return false;
 
    }
 
    /**
     * Load the whole object from the database
     *
     * @since 3.14.0
     *
     * @return LLMS_Abstract_Database_Store instance of the current object, useful for chaining.
     */
    protected function hydrate() {
 
        if ( $this->id ) {
            $res = $this->read( array_keys( $this->columns ) );
            if ( $res ) {
                $this->data = array_merge( $this->data, $res );
            }
        }
 
        return $this;
 
    }
 
    /**
     * Save object to the database
     *
     * Creates it if doesn't already exist, updates if it does.
     *
     * @since 3.14.0
     * @since 3.24.0 Unknown.
     *
     * @return boolean
     */
    public function save() {
 
        if ( ! $this->id ) {
            $id = $this->create();
            if ( $id ) {
                return true;
            }
            return false;
        } else {
            return $this->update( $this->data );
        }
 
    }
 
    /**
     * Retrieve the format for a column
     *
     * @since 3.14.0
     *
     * @param string $key Column name.
     * @return string
     */
    private function get_column_format( $key ) {
 
        if ( isset( $this->columns[ $key ] ) ) {
            return $this->columns[ $key ];
        }
        return '%s';
 
    }
 
    /**
     * Retrieve the primary key column name
     *
     * @since 3.15.0
     *
     * @return string
     */
    protected function get_primary_key() {
        $primary_key = array_keys( $this->primary_key );
        return preg_replace( '/[^a-zA-Z0-9_]/', '', $primary_key[0] );
    }
 
    /**
     * Get the ID of the object
     *
     * @since 3.14.0
     *
     * @return int
     */
    public function get_id() {
        return $this->id;
    }
 
    /**
     * Get the table Name
     *
     * @since 3.14.0
     *
     * @return string
     */
    private function get_table() {
 
        global $wpdb;
        return $wpdb->prefix . $this->table_prefix . $this->table;
 
    }
 
    /**
     * Retrieve object as an array
     *
     * @since 3.14.0
     * @since 3.34.0 Return the item ID instead of item format as the value of the primary key.
     * @since 3.36.0 Hydrate before returning the array.
     *
     * @return array
     */
    public function to_array() {
 
        $this->hydrate();
        return array_merge( array_combine( array_keys( $this->primary_key ), array( $this->id ) ), $this->data );
 
    }
 
}


Top ↑

Methods Methods

  • __construct — Constructor
  • __get — Get object data
  • __set — Set object data
  • create — Create the item in the database
  • delete — Delete the object from the database
  • exists — Determine if the item exists in the database
  • get — Get object data
  • get_column_format — Retrieve the format for a column
  • get_id — Get the ID of the object
  • get_primary_key — Retrieve the primary key column name
  • get_table — Get the table Name
  • hydrate — Load the whole object from the database
  • read — Read object data from the database
  • save — Save object to the database
  • set — General setter
  • setup — Setup an object with an array of data
  • to_array — Retrieve object as an array
  • update — Update object data in the database

Top ↑

Changelog Changelog

Changelog
Version Description
4.3.0 Add deprecated hook calls to preserve backwards compatibility for extending classes which have no $type property declaration. Updated the $type property to have a default placeholder value.
3.36.0 Prevent undefined index error when attempting to retrieve an unset value from an unsaved object. Hydrate before returning an array via the to_array() method.
3.34.0 to_array() method returns value of the primary key instead of the format.
3.33.0 setup() method returns self instead of void.
3.14.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.