PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.24
Advanced Ads – Ad Manager & AdSense v2.0.24
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 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