1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Bright Nucleus Shortcode Component. |
4
|
|
|
* |
5
|
|
|
* @package BrightNucleus\Shortcode |
6
|
|
|
* @author Alain Schlesser <[email protected]> |
7
|
|
|
* @license MIT |
8
|
|
|
* @link http://www.brightnucleus.com/ |
9
|
|
|
* @copyright 2015-2016 Alain Schlesser, Bright Nucleus |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BrightNucleus\Shortcode; |
13
|
|
|
|
14
|
|
|
use BrightNucleus\Config\ConfigInterface; |
15
|
|
|
use BrightNucleus\Config\ConfigTrait; |
16
|
|
|
use BrightNucleus\Exception\RuntimeException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Base implementation of the Shortcode Attributes Parser Interface. |
20
|
|
|
* |
21
|
|
|
* @since 0.1.0 |
22
|
|
|
* |
23
|
|
|
* @package BrightNucleus\Shortcode |
24
|
|
|
* @author Alain Schlesser <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ShortcodeAttsParser implements ShortcodeAttsParserInterface { |
27
|
|
|
|
28
|
|
|
use ConfigTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instantiate the shortcode attributes parser object |
32
|
|
|
* |
33
|
|
|
* @since 0.1.0 |
34
|
|
|
* |
35
|
|
|
* @param ConfigInterface $config Configuration array to |
36
|
|
|
* parametrize the shortcode |
37
|
|
|
* attributes. |
38
|
|
|
* @throws RuntimeException If the config could not be processed. |
39
|
|
|
*/ |
40
|
|
|
public function __construct( ConfigInterface $config ) { |
41
|
|
|
$this->processConfig( $config ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Parse and validate the shortcode's attributes. |
46
|
|
|
* |
47
|
|
|
* @since 0.1.0 |
48
|
|
|
* |
49
|
|
|
* @param array $atts Attributes passed to the shortcode. |
50
|
|
|
* @param string $tag Tag of the shortcode. |
51
|
|
|
* @return array Validated attributes of the shortcode. |
52
|
|
|
*/ |
53
|
|
|
public function parse_atts( $atts, $tag ) { |
54
|
|
|
$atts = \shortcode_atts( |
55
|
|
|
$this->default_atts(), |
56
|
|
|
$this->validated_atts( (array) $atts ), |
57
|
|
|
$tag |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return $atts; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return an array of default attributes read from the configuration array. |
65
|
|
|
* |
66
|
|
|
* @since 0.1.0 |
67
|
|
|
* |
68
|
|
|
* @return array Default attributes. |
69
|
|
|
*/ |
70
|
|
|
protected function default_atts() { |
71
|
|
|
|
72
|
|
|
$atts = array(); |
73
|
|
|
|
74
|
|
|
if ( ! $this->hasConfigKey( 'atts' ) ) { |
75
|
|
|
return $atts; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$atts_config = $this->getConfigKey( 'atts' ); |
79
|
|
|
array_walk( $atts_config, |
80
|
|
|
function ( $att_properties, $att_label ) use ( &$atts ) { |
81
|
|
|
$atts[ $att_label ] = $att_properties['default']; |
82
|
|
|
} |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
return $atts; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return an array of validated attributes checked against the |
90
|
|
|
* configuration array. |
91
|
|
|
* |
92
|
|
|
* @since 0.1.0 |
93
|
|
|
* |
94
|
|
|
* @param array $atts Attributes that were passed to the shortcode. |
95
|
|
|
* @return array Validated attributes. |
96
|
|
|
*/ |
97
|
|
|
protected function validated_atts( $atts ) { |
98
|
|
|
|
99
|
|
|
if ( ! $this->hasConfigKey( 'atts' ) ) { |
100
|
|
|
return $atts; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$atts_config = $this->getConfigKey( 'atts' ); |
104
|
|
|
array_walk( $atts_config, |
105
|
|
|
function ( $att_properties, $att_label ) use ( &$atts ) { |
106
|
|
|
if ( array_key_exists( $att_label, $atts ) ) { |
107
|
|
|
$validate_function = $att_properties['validate']; |
108
|
|
|
$atts[ $att_label ] = $validate_function( $atts[ $att_label ] ); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
return $atts; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|