1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\ACF\Migrations; |
4
|
|
|
|
5
|
|
|
class OptionPage implements HasMetaData |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
private $id; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string $id |
14
|
|
|
*/ |
15
|
|
|
public function __construct($id) |
16
|
|
|
{ |
17
|
|
|
$this->setId($id); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getMetaData($key) |
21
|
|
|
{ |
22
|
|
|
return get_option($this->getOptionPageKey($key)); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getAllMetaData() |
26
|
|
|
{ |
27
|
|
|
global $wpdb; |
|
|
|
|
28
|
|
|
|
29
|
|
|
$query = $wpdb->prepare( |
30
|
|
|
"SELECT option_name, option_value FROM $wpdb->options WHERE option_name REGEXP '%s'", |
31
|
|
|
"_?{$this->getId()}_.+" |
32
|
|
|
); |
33
|
|
|
$results = $wpdb->get_results($query, OBJECT); |
34
|
|
|
|
35
|
|
|
$options = []; |
36
|
|
|
foreach ($results as $o) { |
37
|
|
|
$options[$this->stripOptionId($o->option_name)] = $o->option_value; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $options; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $metaValue |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function hasMetaValue($metaValue) |
48
|
|
|
{ |
49
|
|
|
global $wpdb; |
|
|
|
|
50
|
|
|
|
51
|
|
|
$query = $wpdb->prepare("SELECT option_name, option_value FROM $wpdb->options WHERE option_name REGEXP '%s' and option_value = '%s'", |
52
|
|
|
"_?{$this->getId()}_.+", $metaValue |
53
|
|
|
); |
54
|
|
|
$results = $wpdb->get_results($query); |
55
|
|
|
|
56
|
|
|
return $results !== []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function updateMetaData($fieldName, $value, $oldValue = '') |
60
|
|
|
{ |
61
|
|
|
return update_option($this->getOptionPageKey($fieldName), $value); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function deleteMetaData($fieldName) |
65
|
|
|
{ |
66
|
|
|
return delete_option($this->getOptionPageKey($fieldName)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $key |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getOptionPageKey($key) |
74
|
|
|
{ |
75
|
|
|
$pre = ''; |
76
|
|
|
if ($key[0] === '_') { |
77
|
|
|
$pre = '_'; |
78
|
|
|
$key = substr($key, 1); |
79
|
|
|
} |
80
|
|
|
if ($this->getId()) { |
81
|
|
|
return $pre . $this->getId() . '_' . $key; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $pre . $key; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getId() |
91
|
|
|
{ |
92
|
|
|
return $this->id; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $id |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function setId($id) |
100
|
|
|
{ |
101
|
|
|
$this->id = $id; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $fieldName |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
private function stripOptionId($fieldName) |
111
|
|
|
{ |
112
|
|
|
return str_replace($this->getId(), '', $fieldName); |
113
|
|
|
} |
114
|
|
|
} |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state