abstract-ad.php
1 year ago
abstract-admin-list-table.php
1 year ago
abstract-data.php
1 year ago
abstract-factory.php
1 year ago
abstract-group.php
1 year ago
abstract-placement-type.php
1 year ago
abstract-placement.php
1 year ago
abstract-screen.php
1 year ago
abstract-types.php
1 year ago
index.php
2 years ago
abstract-data.php
441 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 WP_Error; |
| 13 | use Exception; |
| 14 | use AdvancedAds\Framework\Utilities\Arr; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Data. |
| 20 | */ |
| 21 | abstract class Data implements \ArrayAccess { |
| 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 | $keys = array_diff( $keys, [ 'title', 'content', 'status' ] ); |
| 194 | |
| 195 | return array_merge( $keys ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Return data changes only. |
| 200 | * |
| 201 | * @return array |
| 202 | */ |
| 203 | public function get_changes(): array { |
| 204 | return $this->changes; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Prefix for action and filter hooks on data. |
| 209 | * |
| 210 | * @return string |
| 211 | */ |
| 212 | protected function get_hook_prefix(): string { |
| 213 | return 'advanced-ads-' . $this->object_type . '-get-'; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Gets a prop for a getter method. |
| 218 | * |
| 219 | * Gets the value from either current pending changes, or the data itself. |
| 220 | * Context controls what happens to the value before it's returned. |
| 221 | * |
| 222 | * @param string $prop Name of prop to get. |
| 223 | * @param string $context What the value is for. Valid values are view and edit. |
| 224 | * |
| 225 | * @return mixed |
| 226 | */ |
| 227 | public function get_prop( $prop, $context = 'view' ) { |
| 228 | $value = null; |
| 229 | |
| 230 | if ( Arr::has( $this->temp_data, $prop ) ) { |
| 231 | $value = Arr::get( $this->temp_data, $prop ); |
| 232 | } elseif ( Arr::has( $this->data, $prop ) ) { |
| 233 | $value = Arr::has( $this->changes, $prop ) |
| 234 | ? Arr::get( $this->changes, $prop ) |
| 235 | : Arr::get( $this->data, $prop ); |
| 236 | } |
| 237 | |
| 238 | if ( 'view' === $context ) { |
| 239 | /** |
| 240 | * Filter the option value retrieved for $field. |
| 241 | * `$field` parameter makes dynamic hook portion. |
| 242 | * |
| 243 | * @var mixed $value The option value (may be set to default). |
| 244 | * @var Ad $this The current Ad instance. |
| 245 | */ |
| 246 | $value = apply_filters_deprecated( |
| 247 | "advanced-ads-ad-option-{$prop}", |
| 248 | [ $value, $this ], |
| 249 | '2.0.0', |
| 250 | $this->get_hook_prefix() . $prop, |
| 251 | 'This filter is deprecated. Use ' . $this->get_hook_prefix() . $prop . ' instead.' |
| 252 | ); |
| 253 | |
| 254 | $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
| 255 | } |
| 256 | |
| 257 | return $value; |
| 258 | } |
| 259 | |
| 260 | /* Setter ------------------- */ |
| 261 | |
| 262 | /** |
| 263 | * Set ID. |
| 264 | * |
| 265 | * @param int $id ID. |
| 266 | * |
| 267 | * @return void |
| 268 | */ |
| 269 | public function set_id( $id ): void { |
| 270 | $this->id = absint( $id ); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Set object read property. |
| 275 | * |
| 276 | * @param bool $read Should read?. |
| 277 | * |
| 278 | * @return void |
| 279 | */ |
| 280 | public function set_object_read( $read = true ): void { |
| 281 | $this->object_read = boolval( $read ); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Set all props to default values. |
| 286 | * |
| 287 | * @return void |
| 288 | */ |
| 289 | public function set_defaults(): void { |
| 290 | $this->data = $this->default_data; |
| 291 | $this->changes = []; |
| 292 | $this->set_object_read( false ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Set a collection of props in one go, collect any errors, and return the result. |
| 297 | * Only sets using public methods. |
| 298 | * |
| 299 | * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
| 300 | * |
| 301 | * @return bool|WP_Error |
| 302 | */ |
| 303 | public function set_props( $props ) { |
| 304 | $errors = false; |
| 305 | |
| 306 | foreach ( $props as $prop => $value ) { |
| 307 | try { |
| 308 | /** |
| 309 | * Checks if the prop being set is allowed, and the value is not null. |
| 310 | */ |
| 311 | if ( is_null( $value ) ) { |
| 312 | continue; |
| 313 | } |
| 314 | |
| 315 | $setter = 'set_' . str_replace( '-', '_', $prop ); |
| 316 | |
| 317 | if ( is_callable( [ $this, $setter ] ) ) { |
| 318 | $this->{$setter}( $value ); |
| 319 | } else { |
| 320 | $this->set_prop( $prop, $value ); |
| 321 | } |
| 322 | } catch ( Exception $e ) { |
| 323 | if ( ! $errors ) { |
| 324 | $errors = new WP_Error(); |
| 325 | } |
| 326 | $errors->add( $e->getErrorCode(), $e->getMessage(), [ 'property_name' => $prop ] ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Sets a prop for a setter method. |
| 335 | * |
| 336 | * @param string $prop Name of prop to set. |
| 337 | * @param mixed $value Value of the prop. |
| 338 | * |
| 339 | * @return void |
| 340 | */ |
| 341 | public function set_prop( $prop, $value ): void { |
| 342 | if ( Arr::has( $this->data, $prop ) && true === $this->object_read ) { |
| 343 | if ( Arr::get( $this->data, $prop ) !== $value ) { |
| 344 | Arr::set( $this->changes, $prop, $value ); |
| 345 | } |
| 346 | } else { |
| 347 | Arr::set( $this->data, $prop, $value ); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Sets a prop temporary. |
| 353 | * |
| 354 | * @param string $prop Name of prop to set. |
| 355 | * @param mixed $value Value of the prop. |
| 356 | * |
| 357 | * @return void |
| 358 | */ |
| 359 | public function set_prop_temp( $prop, $value ): void { |
| 360 | $this->temp_data[ $prop ] = $value; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Unset a prop. |
| 365 | * |
| 366 | * @param string $prop Name of prop to unset. |
| 367 | * |
| 368 | * @return void |
| 369 | */ |
| 370 | public function unset_prop( $prop ): void { |
| 371 | if ( array_key_exists( $prop, $this->changes ) ) { |
| 372 | unset( $this->changes ); |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | $this->data[ $prop ] = null; |
| 377 | } |
| 378 | |
| 379 | // ArrayAccess API -------------------. |
| 380 | |
| 381 | /** |
| 382 | * Sets the value at the specified offset. |
| 383 | * |
| 384 | * @param mixed $offset The offset to set the value at. |
| 385 | * @param mixed $value The value to set. |
| 386 | * |
| 387 | * @return void |
| 388 | */ |
| 389 | public function offsetSet( $offset, $value ): void { |
| 390 | $this->set_prop( $offset, $value ); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Checks if the given offset exists. |
| 395 | * |
| 396 | * @param mixed $offset The offset to check for existence. |
| 397 | * |
| 398 | * @return bool True if the offset exists, false otherwise. |
| 399 | */ |
| 400 | public function offsetExists( $offset ): bool { |
| 401 | $func = 'get_' . $offset; |
| 402 | if ( method_exists( $this, $func ) ) { |
| 403 | return null !== $this->$func(); |
| 404 | } |
| 405 | |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Unsets the property at the specified offset. |
| 411 | * |
| 412 | * @param mixed $offset The offset to unset. |
| 413 | * |
| 414 | * @return void |
| 415 | */ |
| 416 | public function offsetUnset( $offset ): void { |
| 417 | $this->unset_prop( $offset ); |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Retrieve the value at the specified offset. |
| 422 | * |
| 423 | * This method attempts to call a getter method based on the offset name. |
| 424 | * If a method named 'get_{offset}' exists, it will be called and its return value will be returned. |
| 425 | * If no such method exists, null will be returned. |
| 426 | * |
| 427 | * @param string $offset The offset to retrieve. |
| 428 | * |
| 429 | * @return mixed The value at the specified offset, or null if the method does not exist. |
| 430 | */ |
| 431 | #[\ReturnTypeWillChange] |
| 432 | public function offsetGet( $offset ) { |
| 433 | $func = 'get_' . $offset; |
| 434 | if ( method_exists( $this, $func ) ) { |
| 435 | return $this->$func(); |
| 436 | } |
| 437 | |
| 438 | return null; |
| 439 | } |
| 440 | } |
| 441 |