PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.0
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.0
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Traits / MediaRowTrait.php

MediaRowTrait.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.0, at classes/Traits/MediaRowTrait.php

121 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Traits;
3
4 /**
5 * Trait to use to connect medias and database.
6 * It also cache the results.
7 * Classes using that trait must define a protected property $db_class_name (string) containing the media SQL DB class name.
8 *
9 * @since 1.9
10 */
11 trait MediaRowTrait {
12
13 /**
14 * The media SQL data row.
15 *
16 * @var ?array
17 * @since 1.9
18 */
19 protected $row;
20
21 /**
22 * The media ID.
23 *
24 * @var int
25 * @since 1.9
26 */
27 protected $id;
28
29 /**
30 * Get the row.
31 *
32 * @since 1.9
33 *
34 * @return array
35 */
36 public function get_row() {
37 if ( isset( $this->row ) ) {
38 return $this->row;
39 }
40
41 if ( ! $this->db_class_name || $this->id <= 0 ) {
42 return $this->invalidate_row();
43 }
44
45 $this->row = $this->get_row_db_instance()->get( $this->id );
46
47 if ( ! $this->row ) {
48 return $this->invalidate_row();
49 }
50
51 return $this->row;
52 }
53
54 /**
55 * Update the row.
56 *
57 * @since 1.9
58 *
59 * @param array $data The data to update.
60 */
61 public function update_row( $data ) {
62 if ( ! $this->db_class_name || $this->id <= 0 ) {
63 return;
64 }
65
66 $this->get_row_db_instance()->update( $this->id, $data );
67
68 $this->reset_row_cache();
69 }
70
71 /**
72 * Delete the row.
73 *
74 * @since 1.9
75 */
76 public function delete_row() {
77 if ( ! $this->db_class_name || $this->id <= 0 ) {
78 return;
79 }
80
81 $this->get_row_db_instance()->delete( $this->id );
82
83 $this->invalidate_row();
84 }
85
86 /**
87 * Shorthand to get the DB table instance.
88 *
89 * @since 1.9
90 *
91 * @return \Imagify\DB\DBInterface The DB table instance.
92 */
93 public function get_row_db_instance() {
94 return call_user_func( [ $this->db_class_name, 'get_instance' ] );
95 }
96
97 /**
98 * Invalidate the row, by setting it to an empty array.
99 *
100 * @since 1.9
101 *
102 * @return array The row.
103 */
104 public function invalidate_row() {
105 $this->row = [];
106 return $this->row;
107 }
108
109 /**
110 * Reset the row cache.
111 *
112 * @since 1.9
113 *
114 * @return null The row.
115 */
116 public function reset_row_cache() {
117 $this->row = null;
118 return $this->row;
119 }
120 }
121