PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.5
Advanced Ads – Ad Manager & AdSense v2.0.5
2.0.26 2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / includes / abstracts / abstract-data.php
advanced-ads / includes / abstracts Last commit date
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