1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Defaults; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
abstract class DefaultsAbstract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $callable = [ |
14
|
|
|
'defaults', 'filter', 'merge', 'restrict', |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $guarded = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $name |
24
|
|
|
* @return void|array |
25
|
|
|
*/ |
26
|
7 |
|
public function __call( $name, array $args = [] ) |
27
|
|
|
{ |
28
|
7 |
|
if( !method_exists( $this, $name ) || !in_array( $name, $this->callable ))return; |
29
|
7 |
|
$defaults = call_user_func_array( [$this, $name], $args ); |
30
|
7 |
|
$hookName = (new ReflectionClass( $this ))->getShortName(); |
31
|
7 |
|
$hookName = str_replace( 'Defaults', '', $hookName ); |
32
|
7 |
|
$hookName = glsr( Helper::class )->dashCase( $hookName ); |
33
|
7 |
|
return apply_filters( 'site-reviews/defaults/'.$hookName, $defaults, $name ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
abstract protected function defaults(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
protected function filter( array $values = [] ) |
45
|
|
|
{ |
46
|
|
|
return $this->normalize( $this->merge( array_filter( $values )), $values ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
7 |
|
protected function filteredJson( array $values = [] ) |
53
|
|
|
{ |
54
|
7 |
|
$defaults = $this->flattenArray( |
55
|
7 |
|
array_diff_key( $this->defaults(), array_flip( $this->guarded )) |
56
|
|
|
); |
57
|
7 |
|
$values = $this->flattenArray( |
58
|
7 |
|
shortcode_atts( $defaults, $values ) |
59
|
|
|
); |
60
|
7 |
|
$filtered = array_filter( array_diff_assoc( $values, $defaults ), function( $value ) { |
61
|
1 |
|
return !$this->isEmpty( $value ); |
62
|
7 |
|
}); |
63
|
7 |
|
return json_encode( $filtered, JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_TAG|JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function flattenArray( array $values ) |
70
|
|
|
{ |
71
|
7 |
|
array_walk( $values, function( &$value ) { |
72
|
7 |
|
if( !is_array( $value ))return; |
73
|
1 |
|
$value = implode( ',', $value ); |
74
|
7 |
|
}); |
75
|
7 |
|
return $values; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $var |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
1 |
|
protected function isEmpty( $var ) |
83
|
|
|
{ |
84
|
1 |
|
return !is_numeric( $var ) && !is_bool( $var ) && empty( $var ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function merge( array $values = [] ) |
91
|
|
|
{ |
92
|
|
|
return $this->normalize( wp_parse_args( $values, $this->defaults() ), $values ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
7 |
|
protected function normalize( array $values, array $originalValues ) |
99
|
|
|
{ |
100
|
7 |
|
$values['json'] = $this->filteredJson( $originalValues ); |
101
|
7 |
|
return $values; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
7 |
|
protected function restrict( array $values = [] ) |
108
|
|
|
{ |
109
|
7 |
|
return $this->normalize( shortcode_atts( $this->defaults(), $values ), $values ); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|