abstract-ad.php
1 week ago
abstract-admin-list-table.php
1 week ago
abstract-data.php
1 week ago
abstract-factory.php
1 week ago
abstract-group.php
2 days ago
abstract-placement-type.php
1 week ago
abstract-placement.php
3 months ago
abstract-screen.php
3 months ago
abstract-types.php
1 week ago
index.php
2 years ago
abstract-data.php
378 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This class is serving as the base for metadata service. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Abstracts; |
| 11 | |
| 12 | use AdvancedAds\Framework\Utilities\Arr; |
| 13 | use Exception; |
| 14 | use WP_Error; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Data. |
| 20 | */ |
| 21 | abstract class Data { |
| 22 | |
| 23 | /** |
| 24 | * ID for this object. |
| 25 | * |
| 26 | * @var int |
| 27 | */ |
| 28 | protected $id = 0; |
| 29 | |
| 30 | /** |
| 31 | * Core data for this object. Name value pairs (name + default value). |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | protected $data = []; |
| 36 | |
| 37 | /** |
| 38 | * Core data changes for this object. |
| 39 | * |
| 40 | * @var array |
| 41 | */ |
| 42 | protected $changes = []; |
| 43 | |
| 44 | /** |
| 45 | * Set to _data on construct so we can track and reset data if needed. |
| 46 | * |
| 47 | * @var array |
| 48 | */ |
| 49 | protected $default_data = []; |
| 50 | |
| 51 | /** |
| 52 | * This is false until the object is read from the DB. |
| 53 | * |
| 54 | * @var bool |
| 55 | */ |
| 56 | protected $object_read = false; |
| 57 | |
| 58 | /** |
| 59 | * This is the object type. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $object_type = 'post'; |
| 64 | |
| 65 | /** |
| 66 | * Contains a reference to the data store for this class. |
| 67 | * |
| 68 | * @var object |
| 69 | */ |
| 70 | protected $data_store; |
| 71 | |
| 72 | /** |
| 73 | * Stores temp data. |
| 74 | * |
| 75 | * @var array |
| 76 | */ |
| 77 | protected $temp_data = []; |
| 78 | |
| 79 | /** |
| 80 | * Change data to JSON format. |
| 81 | * |
| 82 | * @return string Data in JSON format. |
| 83 | */ |
| 84 | public function __toString() { |
| 85 | return wp_json_encode( $this->get_data() ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Default constructor. |
| 90 | * |
| 91 | * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
| 92 | */ |
| 93 | public function __construct( $read = 0 ) { // phpcs:ignore |
| 94 | $this->default_data = $this->data; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Save should create or update based on object existence. |
| 99 | * |
| 100 | * @return int ID. |
| 101 | */ |
| 102 | public function save(): int { |
| 103 | // Early bail!! |
| 104 | if ( ! $this->data_store ) { |
| 105 | return $this->get_id(); |
| 106 | } |
| 107 | |
| 108 | if ( $this->get_id() ) { |
| 109 | $this->data_store->update( $this ); |
| 110 | } else { |
| 111 | $this->data_store->create( $this ); |
| 112 | } |
| 113 | |
| 114 | return $this->get_id(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Delete an object, set the ID to 0, and return result. |
| 119 | * |
| 120 | * @param bool $force_delete Should the date be deleted permanently. |
| 121 | * |
| 122 | * @return bool |
| 123 | */ |
| 124 | public function delete( $force_delete = false ): bool { |
| 125 | if ( $this->data_store ) { |
| 126 | $this->data_store->delete( $this, $force_delete ); |
| 127 | $this->set_id( 0 ); |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Merge changes with data and clear. |
| 137 | * |
| 138 | * @return void |
| 139 | */ |
| 140 | public function apply_changes(): void { |
| 141 | $this->data = array_replace_recursive( $this->data, $this->changes ); |
| 142 | $this->changes = []; |
| 143 | } |
| 144 | |
| 145 | /* Getter ------------------- */ |
| 146 | |
| 147 | /** |
| 148 | * Returns the unique ID for this object. |
| 149 | * |
| 150 | * @return int |
| 151 | */ |
| 152 | public function get_id(): int { |
| 153 | return $this->id; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Get the data store. |
| 158 | * |
| 159 | * @return object |
| 160 | */ |
| 161 | public function get_data_store() { |
| 162 | return $this->data_store; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get object read property. |
| 167 | * |
| 168 | * @return bool |
| 169 | */ |
| 170 | public function get_object_read(): bool { |
| 171 | return (bool) $this->object_read; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Returns all data for this object. |
| 176 | * |
| 177 | * @return array |
| 178 | */ |
| 179 | public function get_data(): array { |
| 180 | return array_merge( |
| 181 | [ 'id' => $this->get_id() ], |
| 182 | $this->data |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Returns array of expected data keys for this object. |
| 188 | * |
| 189 | * @return array |
| 190 | */ |
| 191 | public function get_data_keys() { |
| 192 | $keys = array_keys( $this->data ); |
| 193 | |
| 194 | return array_values( array_diff( $keys, [ 'title', 'content', 'status' ] ) ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Return data changes only. |
| 199 | * |
| 200 | * @return array |
| 201 | */ |
| 202 | public function get_changes(): array { |
| 203 | return $this->changes; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Prefix for action and filter hooks on data. |
| 208 | * |
| 209 | * @return string |
| 210 | */ |
| 211 | protected function get_hook_prefix(): string { |
| 212 | return 'advanced-ads-' . $this->object_type . '-get-'; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Gets a prop for a getter method. |
| 217 | * |
| 218 | * Gets the value from either current pending changes, or the data itself. |
| 219 | * Context controls what happens to the value before it's returned. |
| 220 | * |
| 221 | * @param string $prop Name of prop to get. |
| 222 | * @param string $context What the value is for. Valid values are view and edit. |
| 223 | * |
| 224 | * @return mixed |
| 225 | */ |
| 226 | public function get_prop( $prop, $context = 'view' ) { |
| 227 | $value = null; |
| 228 | |
| 229 | if ( Arr::has( $this->temp_data, $prop ) ) { |
| 230 | $value = Arr::get( $this->temp_data, $prop ); |
| 231 | } elseif ( Arr::has( $this->data, $prop ) ) { |
| 232 | $value = Arr::has( $this->changes, $prop ) |
| 233 | ? Arr::get( $this->changes, $prop ) |
| 234 | : Arr::get( $this->data, $prop ); |
| 235 | } |
| 236 | |
| 237 | if ( 'view' === $context ) { |
| 238 | /** |
| 239 | * Filter the option value retrieved for $field. |
| 240 | * `$field` parameter makes dynamic hook portion. |
| 241 | * |
| 242 | * @var mixed $value The option value (may be set to default). |
| 243 | * @var Ad $this The current Ad instance. |
| 244 | */ |
| 245 | $value = apply_filters_deprecated( |
| 246 | "advanced-ads-ad-option-{$prop}", |
| 247 | [ $value, $this ], |
| 248 | '2.0.0', |
| 249 | $this->get_hook_prefix() . $prop, |
| 250 | 'This filter is deprecated. Use ' . $this->get_hook_prefix() . $prop . ' instead.' |
| 251 | ); |
| 252 | |
| 253 | $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
| 254 | } |
| 255 | |
| 256 | return $value; |
| 257 | } |
| 258 | |
| 259 | /* Setter ------------------- */ |
| 260 | |
| 261 | /** |
| 262 | * Set ID. |
| 263 | * |
| 264 | * @param int $id ID. |
| 265 | * |
| 266 | * @return void |
| 267 | */ |
| 268 | public function set_id( $id ): void { |
| 269 | $this->id = absint( $id ); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Set object read property. |
| 274 | * |
| 275 | * @param bool $read Should read?. |
| 276 | * |
| 277 | * @return void |
| 278 | */ |
| 279 | public function set_object_read( $read = true ): void { |
| 280 | $this->object_read = (bool) $read; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Set all props to default values. |
| 285 | * |
| 286 | * @return void |
| 287 | */ |
| 288 | public function set_defaults(): void { |
| 289 | $this->data = $this->default_data; |
| 290 | $this->changes = []; |
| 291 | $this->set_object_read( false ); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Set a collection of props in one go, collect any errors, and return the result. |
| 296 | * Only sets using public methods. |
| 297 | * |
| 298 | * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
| 299 | * |
| 300 | * @return bool|WP_Error |
| 301 | */ |
| 302 | public function set_props( $props ) { |
| 303 | $errors = false; |
| 304 | |
| 305 | foreach ( $props as $prop => $value ) { |
| 306 | try { |
| 307 | /** |
| 308 | * Checks if the prop being set is allowed, and the value is not null. |
| 309 | */ |
| 310 | if ( is_null( $value ) ) { |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | $setter = 'set_' . str_replace( '-', '_', $prop ); |
| 315 | |
| 316 | if ( is_callable( [ $this, $setter ] ) ) { |
| 317 | $this->{$setter}( $value ); |
| 318 | } else { |
| 319 | $this->set_prop( $prop, $value ); |
| 320 | } |
| 321 | } catch ( Exception $e ) { |
| 322 | if ( ! $errors ) { |
| 323 | $errors = new WP_Error(); |
| 324 | } |
| 325 | $errors->add( $e->getCode(), $e->getMessage(), [ 'property_name' => $prop ] ); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Sets a prop for a setter method. |
| 334 | * |
| 335 | * @param string $prop Name of prop to set. |
| 336 | * @param mixed $value Value of the prop. |
| 337 | * |
| 338 | * @return void |
| 339 | */ |
| 340 | public function set_prop( $prop, $value ): void { |
| 341 | if ( Arr::has( $this->data, $prop ) && true === $this->object_read ) { |
| 342 | if ( Arr::get( $this->data, $prop ) !== $value ) { |
| 343 | Arr::set( $this->changes, $prop, $value ); |
| 344 | } |
| 345 | } else { |
| 346 | Arr::set( $this->data, $prop, $value ); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Sets a prop temporary. |
| 352 | * |
| 353 | * @param string $prop Name of prop to set. |
| 354 | * @param mixed $value Value of the prop. |
| 355 | * |
| 356 | * @return void |
| 357 | */ |
| 358 | public function set_prop_temp( $prop, $value ): void { |
| 359 | $this->temp_data[ $prop ] = $value; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Unset a prop. |
| 364 | * |
| 365 | * @param string $prop Name of prop to unset. |
| 366 | * |
| 367 | * @return void |
| 368 | */ |
| 369 | public function unset_prop( $prop ): void { |
| 370 | if ( array_key_exists( $prop, $this->changes ) ) { |
| 371 | unset( $this->changes[ $prop ] ); |
| 372 | return; |
| 373 | } |
| 374 | |
| 375 | $this->data[ $prop ] = null; |
| 376 | } |
| 377 | } |
| 378 |