1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux\MetaBox; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Settings\Settings; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* SiteMeta::all(); |
9
|
|
|
* SiteMeta::global(); |
10
|
|
|
* SiteMeta::global('seo_enabled','fallback'); |
11
|
|
|
* SiteMeta::get('global'); |
12
|
|
|
* SiteMeta::get('global','seo_enabled','fallback'); |
13
|
|
|
*/ |
14
|
|
|
class SiteMetaManager |
15
|
|
|
{ |
16
|
|
|
protected $options; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->options = get_option( Settings::id(), [] ); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $group |
25
|
|
|
* @return object|array|null |
26
|
|
|
*/ |
27
|
|
|
public function __call( $group, $args ) |
28
|
|
|
{ |
29
|
|
|
$args = array_pad( $args, 2, null ); |
30
|
|
|
$group = $this->$group; |
31
|
|
|
if( is_object( $group )) { |
32
|
|
|
return $group; |
33
|
|
|
} |
34
|
|
|
return $this->get( $group, $args[0], $args[1] ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $group |
39
|
|
|
* @return object|array|null |
40
|
|
|
*/ |
41
|
|
|
public function __get( $group ) |
42
|
|
|
{ |
43
|
|
|
if( $group == 'all' ) { |
44
|
|
|
return (object) $this->options; |
45
|
|
|
} |
46
|
|
|
if( empty( $group )) { |
47
|
|
|
$group = $this->getDefaultGroup(); |
48
|
|
|
} |
49
|
|
|
return isset( $this->options[$group] ) |
50
|
|
|
? $this->options[$group] |
51
|
|
|
: null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $group |
56
|
|
|
* @param string|null $key |
57
|
|
|
* @param mixed $fallback |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function get( $group = '', $key = '', $fallback = null ) |
61
|
|
|
{ |
62
|
|
|
if( func_num_args() < 1 ) { |
63
|
|
|
return $this->all; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
if( is_string( $group )) { |
66
|
|
|
$group = $this->$group; |
67
|
|
|
} |
68
|
|
|
if( !is_array( $group )) { |
69
|
|
|
return $fallback; |
70
|
|
|
} |
71
|
|
|
if( is_null( $key )) { |
72
|
|
|
return $group; |
73
|
|
|
} |
74
|
|
|
return $this->getValue( $group, $key, $fallback ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
protected function getDefaultGroup() |
81
|
|
|
{ |
82
|
|
|
return ''; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $key |
87
|
|
|
* @param mixed $fallback |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
protected function getValue( array $group, $key, $fallback ) |
91
|
|
|
{ |
92
|
|
|
if( !array_key_exists( $key, $group )) { |
93
|
|
|
return $fallback; |
94
|
|
|
} |
95
|
|
|
return empty( $group[$key] ) && !is_null( $fallback ) |
96
|
|
|
? $fallback |
97
|
|
|
: $group[$key]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.