|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Wraps an array (meta data for now) and tells if there was any changes. |
|
4
|
|
|
* |
|
5
|
|
|
* The main idea behind this class is to avoid doing unneeded |
|
6
|
|
|
* SQL updates if nothing changed. |
|
7
|
|
|
* |
|
8
|
|
|
* @version 1.0.19 |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
defined( 'ABSPATH' ) || exit; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Meta data class. |
|
15
|
|
|
*/ |
|
16
|
|
|
class GetPaid_Meta_Data implements JsonSerializable { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Current data for metadata |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.0.19 |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $current_data; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Metadata data |
|
28
|
|
|
* |
|
29
|
|
|
* @since 1.0.19 |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $data; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $meta Data to wrap behind this function. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct( $meta = array() ) { |
|
40
|
|
|
$this->current_data = $meta; |
|
41
|
|
|
$this->apply_changes(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* When converted to JSON. |
|
46
|
|
|
* |
|
47
|
|
|
* @return object|array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function jsonSerialize() { |
|
50
|
|
|
return $this->get_data(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Merge changes with data and clear. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function apply_changes() { |
|
57
|
|
|
$this->data = $this->current_data; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Creates or updates a property in the metadata object. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $key Key to set. |
|
64
|
|
|
* @param mixed $value Value to set. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __set( $key, $value ) { |
|
67
|
|
|
$this->current_data[ $key ] = $value; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks if a given key exists in our data. This is called internally |
|
72
|
|
|
* by `empty` and `isset`. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $key Key to check if set. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function __isset( $key ) { |
|
79
|
|
|
return array_key_exists( $key, $this->current_data ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the value of any property. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key Key to get. |
|
86
|
|
|
* @return mixed Property value or NULL if it does not exists |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __get( $key ) { |
|
89
|
|
|
if ( array_key_exists( $key, $this->current_data ) ) { |
|
90
|
|
|
return $this->current_data[ $key ]; |
|
91
|
|
|
} |
|
92
|
|
|
return null; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return data changes only. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function get_changes() { |
|
101
|
|
|
$changes = array(); |
|
102
|
|
|
foreach ( $this->current_data as $id => $value ) { |
|
103
|
|
|
if ( ! array_key_exists( $id, $this->data ) || $value !== $this->data[ $id ] ) { |
|
104
|
|
|
$changes[ $id ] = $value; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
return $changes; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Return all data as an array. |
|
112
|
|
|
* |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public function get_data() { |
|
116
|
|
|
return $this->data; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|