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