Manager.php
2 weeks ago
Shortcode_Abstract.php
2 weeks ago
Shortcode_Interface.php
2 weeks ago
Utils.php
2 weeks ago
Shortcode_Abstract.php
255 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tribe\Shortcode; |
| 4 | |
| 5 | use Tribe__Utils__Array as Arr; |
| 6 | |
| 7 | /** |
| 8 | * The abstract all shortcodes should implement. |
| 9 | * |
| 10 | * @package Tribe\Shortcode |
| 11 | * |
| 12 | * @since 4.12.0 |
| 13 | */ |
| 14 | abstract class Shortcode_Abstract implements Shortcode_Interface { |
| 15 | /** |
| 16 | * Slug of the current shortcode. |
| 17 | * |
| 18 | * @since 4.12.0 |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $slug; |
| 23 | |
| 24 | /** |
| 25 | * Default arguments to be merged into final arguments of the shortcode. |
| 26 | * |
| 27 | * @since 4.12.0 |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | protected $default_arguments = []; |
| 32 | |
| 33 | /** |
| 34 | * Array map allowing aliased shortcode arguments. |
| 35 | * |
| 36 | * The array keys are aliases of the array values (i.e. the "real" shortcode attributes to parse). |
| 37 | * Example array: [ 'alias' => 'canonical', 'from' => 'to', 'that' => 'becomes_this' ] |
| 38 | * Example shortcode usage: [some_tag alias=17 to='Fred'] will be parsed as [some_tag canonical=17 to='Fred'] |
| 39 | * |
| 40 | * @since 4.12.2 |
| 41 | * |
| 42 | * @var array<string,string> |
| 43 | */ |
| 44 | protected $aliased_arguments = []; |
| 45 | |
| 46 | /** |
| 47 | * Array of callbacks for arguments validation. |
| 48 | * |
| 49 | * @since 4.12.0 |
| 50 | * |
| 51 | * @var array |
| 52 | */ |
| 53 | protected $validate_arguments_map = []; |
| 54 | |
| 55 | /** |
| 56 | * Arguments of the current shortcode. |
| 57 | * |
| 58 | * @since 4.12.0 |
| 59 | * |
| 60 | * @var array |
| 61 | */ |
| 62 | protected $arguments; |
| 63 | |
| 64 | /** |
| 65 | * Content of the current shortcode. |
| 66 | * |
| 67 | * @since 4.12.0 |
| 68 | * |
| 69 | * @var string |
| 70 | */ |
| 71 | protected $content; |
| 72 | |
| 73 | /** |
| 74 | * {@inheritDoc} |
| 75 | */ |
| 76 | public function setup( $arguments, $content ) { |
| 77 | $this->arguments = $this->parse_arguments( (array) $arguments ); |
| 78 | $this->content = $content; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * {@inheritDoc} |
| 83 | */ |
| 84 | public function set_aliased_arguments( array $alias_map ) { |
| 85 | $this->aliased_arguments = Arr::filter_to_flat_scalar_associative_array( (array) $alias_map ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * {@inheritDoc} |
| 90 | */ |
| 91 | public function get_aliased_arguments() { |
| 92 | return $this->aliased_arguments; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * {@inheritDoc} |
| 97 | */ |
| 98 | public function parse_arguments( array $arguments ) { |
| 99 | $arguments = Arr::parse_associative_array_alias( (array) $arguments, (array) $this->get_aliased_arguments() ); |
| 100 | $arguments = shortcode_atts( $this->get_default_arguments(), $arguments, $this->slug ); |
| 101 | |
| 102 | return $this->validate_arguments( $arguments ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * {@inheritDoc} |
| 107 | */ |
| 108 | public function validate_arguments( array $arguments ) { |
| 109 | $validate_arguments_map = $this->filter_validated_arguments_map( $this->get_validated_arguments_map() ); |
| 110 | foreach ( $validate_arguments_map as $key => $callback ) { |
| 111 | $arguments[ $key ] = $callback( isset( $arguments[ $key ] ) ? $arguments[ $key ] : null ); |
| 112 | } |
| 113 | |
| 114 | return $arguments; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | public function get_registration_slug() { |
| 121 | return $this->slug; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * {@inheritDoc} |
| 126 | */ |
| 127 | public function get_validated_arguments_map() { |
| 128 | return $this->validate_arguments_map; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * {@inheritDoc} |
| 133 | */ |
| 134 | public function filter_validated_arguments_map( $validate_arguments_map ) { |
| 135 | /** |
| 136 | * Applies a filter to instance arguments validation callbacks. |
| 137 | * |
| 138 | * @since 4.12.0 |
| 139 | * |
| 140 | * @param array $validate_arguments_map Current set of callbacks for arguments. |
| 141 | * @param static $instance Which instance of shortcode we are dealing with. |
| 142 | */ |
| 143 | $validate_arguments_map = apply_filters( 'tribe_shortcode_validate_arguments_map', $validate_arguments_map, $this ); |
| 144 | |
| 145 | $registration_slug = $this->get_registration_slug(); |
| 146 | |
| 147 | /** |
| 148 | * Applies a filter to instance arguments validation callbacks based on the registration slug of the shortcode. |
| 149 | * |
| 150 | * @since 4.12.0 |
| 151 | * |
| 152 | * @param array $validate_arguments_map Current set of callbacks for arguments. |
| 153 | * @param static $instance Which instance of shortcode we are dealing with. |
| 154 | */ |
| 155 | $validate_arguments_map = apply_filters( "tribe__shortcode_{$registration_slug}_validate_arguments_map", $validate_arguments_map, $this ); |
| 156 | |
| 157 | return $validate_arguments_map; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * {@inheritDoc} |
| 162 | */ |
| 163 | public function get_arguments() { |
| 164 | /** |
| 165 | * Applies a filter to instance arguments. |
| 166 | * |
| 167 | * @since 4.12.0 |
| 168 | * |
| 169 | * @param array $arguments Current set of arguments. |
| 170 | * @param static $instance Which instance of shortcode we are dealing with. |
| 171 | */ |
| 172 | $arguments = apply_filters( 'tribe_shortcode_arguments', $this->arguments, $this ); |
| 173 | |
| 174 | $registration_slug = $this->get_registration_slug(); |
| 175 | |
| 176 | /** |
| 177 | * Applies a filter to instance arguments based on the registration slug of the shortcode. |
| 178 | * |
| 179 | * @since 4.12.0 |
| 180 | * |
| 181 | * @param array $arguments Current set of arguments. |
| 182 | * @param static $instance Which instance of shortcode we are dealing with. |
| 183 | */ |
| 184 | $arguments = apply_filters( "tribe_shortcode_{$registration_slug}_arguments", $arguments, $this ); |
| 185 | |
| 186 | return $arguments; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * {@inheritDoc} |
| 191 | */ |
| 192 | public function get_argument( $index, $default = null ) { |
| 193 | $arguments = $this->get_arguments(); |
| 194 | $argument = Arr::get( $arguments, $index, $default ); |
| 195 | |
| 196 | /** |
| 197 | * Applies a filter to a specific shortcode argument, catch all for all shortcodes. |
| 198 | * |
| 199 | * @since 4.12.0 |
| 200 | * |
| 201 | * @param mixed $argument The argument. |
| 202 | * @param array $index Which index we indent to fetch from the arguments. |
| 203 | * @param array $default Default value if it doesn't exist. |
| 204 | * @param static $instance Which instance of shortcode we are dealing with. |
| 205 | */ |
| 206 | $argument = apply_filters( 'tribe_shortcode_argument', $argument, $index, $default, $this ); |
| 207 | |
| 208 | $registration_slug = $this->get_registration_slug(); |
| 209 | |
| 210 | /** |
| 211 | * Applies a filter to a specific shortcode argument, to a particular registration slug. |
| 212 | * |
| 213 | * @since 4.12.0 |
| 214 | * |
| 215 | * @param mixed $argument The argument value. |
| 216 | * @param array $index Which index we indent to fetch from the arguments. |
| 217 | * @param array $default Default value if it doesn't exist. |
| 218 | * @param static $instance Which instance of shortcode we are dealing with. |
| 219 | */ |
| 220 | $argument = apply_filters( "tribe_shortcode_{$registration_slug}_argument", $argument, $index, $default, $this ); |
| 221 | |
| 222 | return $argument; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * {@inheritDoc} |
| 227 | */ |
| 228 | public function get_default_arguments() { |
| 229 | /** |
| 230 | * Applies a filter to instance default arguments. |
| 231 | * |
| 232 | * @since 4.12.0 |
| 233 | * |
| 234 | * @param array $default_arguments Current set of default arguments. |
| 235 | * @param static $instance Which instance of shortcode we are dealing with. |
| 236 | */ |
| 237 | $default_arguments = apply_filters( 'tribe_shortcode_default_arguments', $this->default_arguments, $this ); |
| 238 | |
| 239 | $registration_slug = $this->get_registration_slug(); |
| 240 | |
| 241 | /** |
| 242 | * Applies a filter to instance default arguments based on the registration slug of the shortcode. |
| 243 | * |
| 244 | * @since 4.12.0 |
| 245 | * |
| 246 | * @param array $default_arguments Current set of default arguments. |
| 247 | * @param static $instance Which instance of shortcode we are dealing with. |
| 248 | */ |
| 249 | $default_arguments = apply_filters( "tribe_shortcode_{$registration_slug}_default_arguments", $default_arguments, $this ); |
| 250 | |
| 251 | return $default_arguments; |
| 252 | } |
| 253 | |
| 254 | } |
| 255 |