@@ -4,21 +4,21 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Text extends Base |
| 6 | 6 | { |
| 7 | - protected $element = 'input'; |
|
| 7 | + protected $element = 'input'; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @return string |
|
| 11 | - */ |
|
| 12 | - public function render(array $defaults = []) |
|
| 13 | - { |
|
| 14 | - $defaults = wp_parse_args($defaults, [ |
|
| 15 | - 'class' => 'regular-text', |
|
| 16 | - 'type' => 'text', |
|
| 17 | - ]); |
|
| 9 | + /** |
|
| 10 | + * @return string |
|
| 11 | + */ |
|
| 12 | + public function render(array $defaults = []) |
|
| 13 | + { |
|
| 14 | + $defaults = wp_parse_args($defaults, [ |
|
| 15 | + 'class' => 'regular-text', |
|
| 16 | + 'type' => 'text', |
|
| 17 | + ]); |
|
| 18 | 18 | |
| 19 | - return sprintf('<input %s/>%s', |
|
| 20 | - $this->implodeAttributes($defaults), |
|
| 21 | - $this->generateDescription() |
|
| 22 | - ); |
|
| 23 | - } |
|
| 19 | + return sprintf('<input %s/>%s', |
|
| 20 | + $this->implodeAttributes($defaults), |
|
| 21 | + $this->generateDescription() |
|
| 22 | + ); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -9,15 +9,15 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * @return string |
| 11 | 11 | */ |
| 12 | - public function render(array $defaults = []) |
|
| 12 | + public function render( array $defaults = [] ) |
|
| 13 | 13 | { |
| 14 | - $defaults = wp_parse_args($defaults, [ |
|
| 14 | + $defaults = wp_parse_args( $defaults, [ |
|
| 15 | 15 | 'class' => 'regular-text', |
| 16 | 16 | 'type' => 'text', |
| 17 | - ]); |
|
| 17 | + ] ); |
|
| 18 | 18 | |
| 19 | - return sprintf('<input %s/>%s', |
|
| 20 | - $this->implodeAttributes($defaults), |
|
| 19 | + return sprintf( '<input %s/>%s', |
|
| 20 | + $this->implodeAttributes( $defaults ), |
|
| 21 | 21 | $this->generateDescription() |
| 22 | 22 | ); |
| 23 | 23 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Text extends Base |
|
| 6 | -{ |
|
| 5 | +class Text extends Base { |
|
| 7 | 6 | protected $element = 'input'; |
| 8 | 7 | |
| 9 | 8 | /** |
@@ -4,14 +4,14 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Url extends Text |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return string |
|
| 9 | - */ |
|
| 10 | - public function render() |
|
| 11 | - { |
|
| 12 | - return parent::render([ |
|
| 13 | - 'class' => 'regular-text code', |
|
| 14 | - 'type' => 'url', |
|
| 15 | - ]); |
|
| 16 | - } |
|
| 7 | + /** |
|
| 8 | + * @return string |
|
| 9 | + */ |
|
| 10 | + public function render() |
|
| 11 | + { |
|
| 12 | + return parent::render([ |
|
| 13 | + 'class' => 'regular-text code', |
|
| 14 | + 'type' => 'url', |
|
| 15 | + ]); |
|
| 16 | + } |
|
| 17 | 17 | } |
@@ -9,9 +9,9 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | public function render() |
| 11 | 11 | { |
| 12 | - return parent::render([ |
|
| 12 | + return parent::render( [ |
|
| 13 | 13 | 'class' => 'regular-text code', |
| 14 | 14 | 'type' => 'url', |
| 15 | - ]); |
|
| 15 | + ] ); |
|
| 16 | 16 | } |
| 17 | 17 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Url extends Text |
|
| 6 | -{ |
|
| 5 | +class Url extends Text { |
|
| 7 | 6 | /** |
| 8 | 7 | * @return string |
| 9 | 8 | */ |
@@ -7,357 +7,357 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Base |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var array |
|
| 12 | - */ |
|
| 13 | - protected $args; |
|
| 14 | - |
|
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $dependencies = []; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Whether the field has multiple values. |
|
| 22 | - * |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - protected $multi = false; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Whether the field is rendered outside of the form table. |
|
| 29 | - * |
|
| 30 | - * @var bool |
|
| 31 | - */ |
|
| 32 | - protected $outside = false; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The field element tag (i.e. "input"). |
|
| 36 | - * |
|
| 37 | - * @var string |
|
| 38 | - */ |
|
| 39 | - protected $element; |
|
| 40 | - |
|
| 41 | - public function __construct(array $args = []) |
|
| 42 | - { |
|
| 43 | - $this->args = $args; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param string $property |
|
| 48 | - * |
|
| 49 | - * @return mixed |
|
| 50 | - * @throws Exception |
|
| 51 | - */ |
|
| 52 | - public function __get($property) |
|
| 53 | - { |
|
| 54 | - switch ($property) { |
|
| 55 | - case 'args': |
|
| 56 | - case 'dependencies': |
|
| 57 | - case 'element': |
|
| 58 | - case 'multi': |
|
| 59 | - case 'outside': |
|
| 60 | - return $this->$property; |
|
| 61 | - } |
|
| 62 | - throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Generate the field description. |
|
| 67 | - * |
|
| 68 | - * @param bool $paragraph |
|
| 69 | - * |
|
| 70 | - * @return string|null |
|
| 71 | - */ |
|
| 72 | - public function generateDescription($paragraph = true) |
|
| 73 | - { |
|
| 74 | - if (!isset($this->args['desc']) || !$this->args['desc']) { |
|
| 75 | - return; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $tag = ((bool) $paragraph || 'p' == $paragraph) ? 'p' : 'span'; |
|
| 79 | - |
|
| 80 | - return sprintf('<%1$s class="description">%2$s</%1$s>', $tag, $this->args['desc']); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Generate the field label. |
|
| 85 | - * |
|
| 86 | - * @return string|null |
|
| 87 | - */ |
|
| 88 | - public function generateLabel() |
|
| 89 | - { |
|
| 90 | - if (empty($this->args['label'])) { |
|
| 91 | - return; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $for = (bool) $this->args['id'] |
|
| 95 | - ? " for=\"{$this->args['id']}\"" |
|
| 96 | - : ''; |
|
| 97 | - |
|
| 98 | - return sprintf('<label%s>%s</label>', $for, $this->args['label']); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Render this field type. |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - abstract public function render(); |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Convert a value to camel case. |
|
| 110 | - * |
|
| 111 | - * @param string $value |
|
| 112 | - * |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - protected function camelCase($value) |
|
| 116 | - { |
|
| 117 | - $value = ucwords(str_replace(['-', '_'], ' ', $value)); |
|
| 118 | - |
|
| 119 | - return lcfirst(str_replace(' ', '', $value)); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Implode the field attributes. |
|
| 124 | - * |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - protected function implodeAttributes($defaults = []) |
|
| 128 | - { |
|
| 129 | - return $this->normalize($defaults, 'implode'); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Implode multi-field items. |
|
| 134 | - * |
|
| 135 | - * @return string|null |
|
| 136 | - */ |
|
| 137 | - protected function implodeOptions($method = 'select_option', $default = null) |
|
| 138 | - { |
|
| 139 | - $this->args['default'] ?: $this->args['default'] = $default; |
|
| 140 | - |
|
| 141 | - $method = $this->camelCase($method); |
|
| 142 | - |
|
| 143 | - $method = method_exists($this, $method) |
|
| 144 | - ? $method |
|
| 145 | - : 'selectOption'; |
|
| 146 | - |
|
| 147 | - $i = 0; |
|
| 148 | - |
|
| 149 | - if ('singleInput' === $method) { |
|
| 150 | - if (!isset($this->args['options']) || empty($this->args['options'])) { |
|
| 151 | - return; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - // hack to make sure unset single checkbox values start at 1 instead of 0 |
|
| 155 | - if (0 === key($this->args['options'])) { |
|
| 156 | - $options = ['1' => $this->args['options'][0]]; |
|
| 157 | - $this->args['options'] = $options; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - return $this->singleInput(); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - return array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$i, $method) { |
|
| 164 | - return $carry .= $this->$method($key, $i++); |
|
| 165 | - }); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Normalize attributes for this specific field type. |
|
| 170 | - * |
|
| 171 | - * @param bool|string $implode |
|
| 172 | - * |
|
| 173 | - * @return array|string |
|
| 174 | - */ |
|
| 175 | - protected function normalize(array $defaults = [], $implode = false) |
|
| 176 | - { |
|
| 177 | - $args = $this->mergeAttributesWith($defaults); |
|
| 178 | - |
|
| 179 | - $normalize = new Normalizer(); |
|
| 180 | - |
|
| 181 | - return ($this->element && method_exists($normalize, $this->element)) |
|
| 182 | - ? $normalize->{$this->element}($args, $implode) |
|
| 183 | - : ((bool) $implode ? '' : []); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Merge and overwrite empty $this->args values with $defaults. |
|
| 188 | - * |
|
| 189 | - * @return array |
|
| 190 | - */ |
|
| 191 | - protected function mergeAttributesWith(array $defaults) |
|
| 192 | - { |
|
| 193 | - // similar to array_merge except overwrite empty values |
|
| 194 | - foreach ($defaults as $key => $value) { |
|
| 195 | - if (isset($this->args[$key]) && !empty($this->args[$key])) { |
|
| 196 | - continue; |
|
| 197 | - } |
|
| 198 | - $this->args[$key] = $value; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $attributes = $this->args['attributes']; |
|
| 202 | - |
|
| 203 | - // prioritize $attributes over $this->args, don't worry about duplicates |
|
| 204 | - return array_merge($this->args, $attributes); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Generate checkboxes and radios. |
|
| 209 | - * |
|
| 210 | - * @param string $optionKey |
|
| 211 | - * @param string $number |
|
| 212 | - * @param string $type |
|
| 213 | - * |
|
| 214 | - * @return string|null |
|
| 215 | - */ |
|
| 216 | - protected function multiInput($optionKey, $number, $type = 'radio') |
|
| 217 | - { |
|
| 218 | - $args = $this->multiInputArgs($type, $optionKey, $number); |
|
| 219 | - |
|
| 220 | - if (!$args) { |
|
| 221 | - return; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $attributes = ''; |
|
| 225 | - |
|
| 226 | - foreach ($args['attributes'] as $key => $val) { |
|
| 227 | - $attributes .= sprintf('%s="%s" ', $key, $val); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return sprintf('<li><label for="%s"><input %s%s/> %s</label></li>', |
|
| 231 | - $args['attributes']['id'], |
|
| 232 | - $attributes, |
|
| 233 | - checked($args['value'], $args['attributes']['value'], false), |
|
| 234 | - $args['label'] |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Build the checkbox/radio args. |
|
| 240 | - * |
|
| 241 | - * @param string $type |
|
| 242 | - * @param string $optionName |
|
| 243 | - * @param string $number |
|
| 244 | - * |
|
| 245 | - * @return array|null |
|
| 246 | - */ |
|
| 247 | - protected function multiInputArgs($type, $optionName, $number) |
|
| 248 | - { |
|
| 249 | - $defaults = [ |
|
| 250 | - 'class' => '', |
|
| 251 | - 'name' => '', |
|
| 252 | - 'type' => $type, |
|
| 253 | - 'value' => '', |
|
| 254 | - ]; |
|
| 255 | - |
|
| 256 | - $args = []; |
|
| 257 | - |
|
| 258 | - $value = $this->args['options'][$optionName]; |
|
| 259 | - |
|
| 260 | - if (is_array($value)) { |
|
| 261 | - $args = $value; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - if (is_string($value)) { |
|
| 265 | - $label = $value; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - isset($args['name']) ?: $args['name'] = $optionName; |
|
| 269 | - isset($args['value']) ?: $args['value'] = $optionName; |
|
| 270 | - |
|
| 271 | - $args = wp_parse_args($args, $defaults); |
|
| 272 | - |
|
| 273 | - if (!isset($label) || '' === $args['name']) { |
|
| 274 | - return; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - $args['id'] = $this->args['id']."-{$number}"; |
|
| 278 | - $args['name'] = $this->args['name'].('checkbox' === $type && $this->multi ? '[]' : ''); |
|
| 279 | - |
|
| 280 | - $args = array_filter($args, function ($value) { |
|
| 281 | - return '' !== $value; |
|
| 282 | - }); |
|
| 283 | - |
|
| 284 | - if (is_array($this->args['value'])) { |
|
| 285 | - if (in_array($args['value'], $this->args['value'])) { |
|
| 286 | - $this->args['default'] = $args['value']; |
|
| 287 | - } |
|
| 288 | - } elseif ($this->args['value']) { |
|
| 289 | - $this->args['default'] = $this->args['value']; |
|
| 290 | - } elseif ('radio' == $type && !$this->args['default']) { |
|
| 291 | - $this->args['default'] = 0; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return [ |
|
| 295 | - 'attributes' => $args, |
|
| 296 | - 'label' => $label, |
|
| 297 | - 'value' => $this->args['default'], |
|
| 298 | - ]; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Generate checkboxes. |
|
| 303 | - * |
|
| 304 | - * @param string $optionKey |
|
| 305 | - * @param string $number |
|
| 306 | - * |
|
| 307 | - * @return string|null |
|
| 308 | - */ |
|
| 309 | - protected function multiInputCheckbox($optionKey, $number) |
|
| 310 | - { |
|
| 311 | - return $this->multiInput($optionKey, $number, 'checkbox'); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Generate select options. |
|
| 316 | - * |
|
| 317 | - * @param string $optionKey |
|
| 318 | - * |
|
| 319 | - * @return string |
|
| 320 | - */ |
|
| 321 | - protected function selectOption($optionKey) |
|
| 322 | - { |
|
| 323 | - return sprintf('<option value="%s"%s>%s</option>', |
|
| 324 | - $optionKey, |
|
| 325 | - selected($this->args['value'], $optionKey, false), |
|
| 326 | - $this->args['options'][$optionKey] |
|
| 327 | - ); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Generate a single checkbox. |
|
| 332 | - * |
|
| 333 | - * @param string $type |
|
| 334 | - * |
|
| 335 | - * @return string|null |
|
| 336 | - */ |
|
| 337 | - protected function singleInput($type = 'checkbox') |
|
| 338 | - { |
|
| 339 | - $optionKey = key($this->args['options']); |
|
| 340 | - |
|
| 341 | - $args = $this->multiInputArgs($type, $optionKey, 1); |
|
| 342 | - |
|
| 343 | - if (!$args) { |
|
| 344 | - return; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $atts = $this->normalize(); |
|
| 348 | - $atts = wp_parse_args($args['attributes'], $atts); |
|
| 349 | - |
|
| 350 | - $attributes = ''; |
|
| 351 | - |
|
| 352 | - foreach ($atts as $key => $val) { |
|
| 353 | - $attributes .= sprintf('%s="%s" ', $key, $val); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - return sprintf('<label for="%s"><input %s%s/> %s</label>', |
|
| 357 | - $atts['id'], |
|
| 358 | - $attributes, |
|
| 359 | - checked($args['value'], $atts['value'], false), |
|
| 360 | - $args['label'] |
|
| 361 | - ); |
|
| 362 | - } |
|
| 10 | + /** |
|
| 11 | + * @var array |
|
| 12 | + */ |
|
| 13 | + protected $args; |
|
| 14 | + |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $dependencies = []; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Whether the field has multiple values. |
|
| 22 | + * |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + protected $multi = false; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Whether the field is rendered outside of the form table. |
|
| 29 | + * |
|
| 30 | + * @var bool |
|
| 31 | + */ |
|
| 32 | + protected $outside = false; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The field element tag (i.e. "input"). |
|
| 36 | + * |
|
| 37 | + * @var string |
|
| 38 | + */ |
|
| 39 | + protected $element; |
|
| 40 | + |
|
| 41 | + public function __construct(array $args = []) |
|
| 42 | + { |
|
| 43 | + $this->args = $args; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param string $property |
|
| 48 | + * |
|
| 49 | + * @return mixed |
|
| 50 | + * @throws Exception |
|
| 51 | + */ |
|
| 52 | + public function __get($property) |
|
| 53 | + { |
|
| 54 | + switch ($property) { |
|
| 55 | + case 'args': |
|
| 56 | + case 'dependencies': |
|
| 57 | + case 'element': |
|
| 58 | + case 'multi': |
|
| 59 | + case 'outside': |
|
| 60 | + return $this->$property; |
|
| 61 | + } |
|
| 62 | + throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Generate the field description. |
|
| 67 | + * |
|
| 68 | + * @param bool $paragraph |
|
| 69 | + * |
|
| 70 | + * @return string|null |
|
| 71 | + */ |
|
| 72 | + public function generateDescription($paragraph = true) |
|
| 73 | + { |
|
| 74 | + if (!isset($this->args['desc']) || !$this->args['desc']) { |
|
| 75 | + return; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $tag = ((bool) $paragraph || 'p' == $paragraph) ? 'p' : 'span'; |
|
| 79 | + |
|
| 80 | + return sprintf('<%1$s class="description">%2$s</%1$s>', $tag, $this->args['desc']); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Generate the field label. |
|
| 85 | + * |
|
| 86 | + * @return string|null |
|
| 87 | + */ |
|
| 88 | + public function generateLabel() |
|
| 89 | + { |
|
| 90 | + if (empty($this->args['label'])) { |
|
| 91 | + return; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $for = (bool) $this->args['id'] |
|
| 95 | + ? " for=\"{$this->args['id']}\"" |
|
| 96 | + : ''; |
|
| 97 | + |
|
| 98 | + return sprintf('<label%s>%s</label>', $for, $this->args['label']); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Render this field type. |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + abstract public function render(); |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Convert a value to camel case. |
|
| 110 | + * |
|
| 111 | + * @param string $value |
|
| 112 | + * |
|
| 113 | + * @return string |
|
| 114 | + */ |
|
| 115 | + protected function camelCase($value) |
|
| 116 | + { |
|
| 117 | + $value = ucwords(str_replace(['-', '_'], ' ', $value)); |
|
| 118 | + |
|
| 119 | + return lcfirst(str_replace(' ', '', $value)); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Implode the field attributes. |
|
| 124 | + * |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + protected function implodeAttributes($defaults = []) |
|
| 128 | + { |
|
| 129 | + return $this->normalize($defaults, 'implode'); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Implode multi-field items. |
|
| 134 | + * |
|
| 135 | + * @return string|null |
|
| 136 | + */ |
|
| 137 | + protected function implodeOptions($method = 'select_option', $default = null) |
|
| 138 | + { |
|
| 139 | + $this->args['default'] ?: $this->args['default'] = $default; |
|
| 140 | + |
|
| 141 | + $method = $this->camelCase($method); |
|
| 142 | + |
|
| 143 | + $method = method_exists($this, $method) |
|
| 144 | + ? $method |
|
| 145 | + : 'selectOption'; |
|
| 146 | + |
|
| 147 | + $i = 0; |
|
| 148 | + |
|
| 149 | + if ('singleInput' === $method) { |
|
| 150 | + if (!isset($this->args['options']) || empty($this->args['options'])) { |
|
| 151 | + return; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + // hack to make sure unset single checkbox values start at 1 instead of 0 |
|
| 155 | + if (0 === key($this->args['options'])) { |
|
| 156 | + $options = ['1' => $this->args['options'][0]]; |
|
| 157 | + $this->args['options'] = $options; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + return $this->singleInput(); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + return array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$i, $method) { |
|
| 164 | + return $carry .= $this->$method($key, $i++); |
|
| 165 | + }); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Normalize attributes for this specific field type. |
|
| 170 | + * |
|
| 171 | + * @param bool|string $implode |
|
| 172 | + * |
|
| 173 | + * @return array|string |
|
| 174 | + */ |
|
| 175 | + protected function normalize(array $defaults = [], $implode = false) |
|
| 176 | + { |
|
| 177 | + $args = $this->mergeAttributesWith($defaults); |
|
| 178 | + |
|
| 179 | + $normalize = new Normalizer(); |
|
| 180 | + |
|
| 181 | + return ($this->element && method_exists($normalize, $this->element)) |
|
| 182 | + ? $normalize->{$this->element}($args, $implode) |
|
| 183 | + : ((bool) $implode ? '' : []); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Merge and overwrite empty $this->args values with $defaults. |
|
| 188 | + * |
|
| 189 | + * @return array |
|
| 190 | + */ |
|
| 191 | + protected function mergeAttributesWith(array $defaults) |
|
| 192 | + { |
|
| 193 | + // similar to array_merge except overwrite empty values |
|
| 194 | + foreach ($defaults as $key => $value) { |
|
| 195 | + if (isset($this->args[$key]) && !empty($this->args[$key])) { |
|
| 196 | + continue; |
|
| 197 | + } |
|
| 198 | + $this->args[$key] = $value; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $attributes = $this->args['attributes']; |
|
| 202 | + |
|
| 203 | + // prioritize $attributes over $this->args, don't worry about duplicates |
|
| 204 | + return array_merge($this->args, $attributes); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Generate checkboxes and radios. |
|
| 209 | + * |
|
| 210 | + * @param string $optionKey |
|
| 211 | + * @param string $number |
|
| 212 | + * @param string $type |
|
| 213 | + * |
|
| 214 | + * @return string|null |
|
| 215 | + */ |
|
| 216 | + protected function multiInput($optionKey, $number, $type = 'radio') |
|
| 217 | + { |
|
| 218 | + $args = $this->multiInputArgs($type, $optionKey, $number); |
|
| 219 | + |
|
| 220 | + if (!$args) { |
|
| 221 | + return; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $attributes = ''; |
|
| 225 | + |
|
| 226 | + foreach ($args['attributes'] as $key => $val) { |
|
| 227 | + $attributes .= sprintf('%s="%s" ', $key, $val); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return sprintf('<li><label for="%s"><input %s%s/> %s</label></li>', |
|
| 231 | + $args['attributes']['id'], |
|
| 232 | + $attributes, |
|
| 233 | + checked($args['value'], $args['attributes']['value'], false), |
|
| 234 | + $args['label'] |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Build the checkbox/radio args. |
|
| 240 | + * |
|
| 241 | + * @param string $type |
|
| 242 | + * @param string $optionName |
|
| 243 | + * @param string $number |
|
| 244 | + * |
|
| 245 | + * @return array|null |
|
| 246 | + */ |
|
| 247 | + protected function multiInputArgs($type, $optionName, $number) |
|
| 248 | + { |
|
| 249 | + $defaults = [ |
|
| 250 | + 'class' => '', |
|
| 251 | + 'name' => '', |
|
| 252 | + 'type' => $type, |
|
| 253 | + 'value' => '', |
|
| 254 | + ]; |
|
| 255 | + |
|
| 256 | + $args = []; |
|
| 257 | + |
|
| 258 | + $value = $this->args['options'][$optionName]; |
|
| 259 | + |
|
| 260 | + if (is_array($value)) { |
|
| 261 | + $args = $value; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + if (is_string($value)) { |
|
| 265 | + $label = $value; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + isset($args['name']) ?: $args['name'] = $optionName; |
|
| 269 | + isset($args['value']) ?: $args['value'] = $optionName; |
|
| 270 | + |
|
| 271 | + $args = wp_parse_args($args, $defaults); |
|
| 272 | + |
|
| 273 | + if (!isset($label) || '' === $args['name']) { |
|
| 274 | + return; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + $args['id'] = $this->args['id']."-{$number}"; |
|
| 278 | + $args['name'] = $this->args['name'].('checkbox' === $type && $this->multi ? '[]' : ''); |
|
| 279 | + |
|
| 280 | + $args = array_filter($args, function ($value) { |
|
| 281 | + return '' !== $value; |
|
| 282 | + }); |
|
| 283 | + |
|
| 284 | + if (is_array($this->args['value'])) { |
|
| 285 | + if (in_array($args['value'], $this->args['value'])) { |
|
| 286 | + $this->args['default'] = $args['value']; |
|
| 287 | + } |
|
| 288 | + } elseif ($this->args['value']) { |
|
| 289 | + $this->args['default'] = $this->args['value']; |
|
| 290 | + } elseif ('radio' == $type && !$this->args['default']) { |
|
| 291 | + $this->args['default'] = 0; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return [ |
|
| 295 | + 'attributes' => $args, |
|
| 296 | + 'label' => $label, |
|
| 297 | + 'value' => $this->args['default'], |
|
| 298 | + ]; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Generate checkboxes. |
|
| 303 | + * |
|
| 304 | + * @param string $optionKey |
|
| 305 | + * @param string $number |
|
| 306 | + * |
|
| 307 | + * @return string|null |
|
| 308 | + */ |
|
| 309 | + protected function multiInputCheckbox($optionKey, $number) |
|
| 310 | + { |
|
| 311 | + return $this->multiInput($optionKey, $number, 'checkbox'); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Generate select options. |
|
| 316 | + * |
|
| 317 | + * @param string $optionKey |
|
| 318 | + * |
|
| 319 | + * @return string |
|
| 320 | + */ |
|
| 321 | + protected function selectOption($optionKey) |
|
| 322 | + { |
|
| 323 | + return sprintf('<option value="%s"%s>%s</option>', |
|
| 324 | + $optionKey, |
|
| 325 | + selected($this->args['value'], $optionKey, false), |
|
| 326 | + $this->args['options'][$optionKey] |
|
| 327 | + ); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Generate a single checkbox. |
|
| 332 | + * |
|
| 333 | + * @param string $type |
|
| 334 | + * |
|
| 335 | + * @return string|null |
|
| 336 | + */ |
|
| 337 | + protected function singleInput($type = 'checkbox') |
|
| 338 | + { |
|
| 339 | + $optionKey = key($this->args['options']); |
|
| 340 | + |
|
| 341 | + $args = $this->multiInputArgs($type, $optionKey, 1); |
|
| 342 | + |
|
| 343 | + if (!$args) { |
|
| 344 | + return; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $atts = $this->normalize(); |
|
| 348 | + $atts = wp_parse_args($args['attributes'], $atts); |
|
| 349 | + |
|
| 350 | + $attributes = ''; |
|
| 351 | + |
|
| 352 | + foreach ($atts as $key => $val) { |
|
| 353 | + $attributes .= sprintf('%s="%s" ', $key, $val); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + return sprintf('<label for="%s"><input %s%s/> %s</label>', |
|
| 357 | + $atts['id'], |
|
| 358 | + $attributes, |
|
| 359 | + checked($args['value'], $atts['value'], false), |
|
| 360 | + $args['label'] |
|
| 361 | + ); |
|
| 362 | + } |
|
| 363 | 363 | } |
@@ -38,7 +38,7 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | protected $element; |
| 40 | 40 | |
| 41 | - public function __construct(array $args = []) |
|
| 41 | + public function __construct( array $args = [] ) |
|
| 42 | 42 | { |
| 43 | 43 | $this->args = $args; |
| 44 | 44 | } |
@@ -49,9 +49,9 @@ discard block |
||
| 49 | 49 | * @return mixed |
| 50 | 50 | * @throws Exception |
| 51 | 51 | */ |
| 52 | - public function __get($property) |
|
| 52 | + public function __get( $property ) |
|
| 53 | 53 | { |
| 54 | - switch ($property) { |
|
| 54 | + switch( $property ) { |
|
| 55 | 55 | case 'args': |
| 56 | 56 | case 'dependencies': |
| 57 | 57 | case 'element': |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | case 'outside': |
| 60 | 60 | return $this->$property; |
| 61 | 61 | } |
| 62 | - throw new Exception(sprintf('Invalid %s property: %s', __CLASS__, $property)); |
|
| 62 | + throw new Exception( sprintf( 'Invalid %s property: %s', __CLASS__, $property ) ); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
@@ -69,15 +69,15 @@ discard block |
||
| 69 | 69 | * |
| 70 | 70 | * @return string|null |
| 71 | 71 | */ |
| 72 | - public function generateDescription($paragraph = true) |
|
| 72 | + public function generateDescription( $paragraph = true ) |
|
| 73 | 73 | { |
| 74 | - if (!isset($this->args['desc']) || !$this->args['desc']) { |
|
| 74 | + if( !isset( $this->args['desc'] ) || !$this->args['desc'] ) { |
|
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - $tag = ((bool) $paragraph || 'p' == $paragraph) ? 'p' : 'span'; |
|
| 78 | + $tag = ( (bool) $paragraph || 'p' == $paragraph ) ? 'p' : 'span'; |
|
| 79 | 79 | |
| 80 | - return sprintf('<%1$s class="description">%2$s</%1$s>', $tag, $this->args['desc']); |
|
| 80 | + return sprintf( '<%1$s class="description">%2$s</%1$s>', $tag, $this->args['desc'] ); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function generateLabel() |
| 89 | 89 | { |
| 90 | - if (empty($this->args['label'])) { |
|
| 90 | + if( empty( $this->args['label'] ) ) { |
|
| 91 | 91 | return; |
| 92 | 92 | } |
| 93 | 93 | |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | ? " for=\"{$this->args['id']}\"" |
| 96 | 96 | : ''; |
| 97 | 97 | |
| 98 | - return sprintf('<label%s>%s</label>', $for, $this->args['label']); |
|
| 98 | + return sprintf( '<label%s>%s</label>', $for, $this->args['label'] ); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
@@ -112,11 +112,11 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @return string |
| 114 | 114 | */ |
| 115 | - protected function camelCase($value) |
|
| 115 | + protected function camelCase( $value ) |
|
| 116 | 116 | { |
| 117 | - $value = ucwords(str_replace(['-', '_'], ' ', $value)); |
|
| 117 | + $value = ucwords( str_replace( ['-', '_'], ' ', $value ) ); |
|
| 118 | 118 | |
| 119 | - return lcfirst(str_replace(' ', '', $value)); |
|
| 119 | + return lcfirst( str_replace( ' ', '', $value ) ); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -124,9 +124,9 @@ discard block |
||
| 124 | 124 | * |
| 125 | 125 | * @return string |
| 126 | 126 | */ |
| 127 | - protected function implodeAttributes($defaults = []) |
|
| 127 | + protected function implodeAttributes( $defaults = [] ) |
|
| 128 | 128 | { |
| 129 | - return $this->normalize($defaults, 'implode'); |
|
| 129 | + return $this->normalize( $defaults, 'implode' ); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -134,25 +134,25 @@ discard block |
||
| 134 | 134 | * |
| 135 | 135 | * @return string|null |
| 136 | 136 | */ |
| 137 | - protected function implodeOptions($method = 'select_option', $default = null) |
|
| 137 | + protected function implodeOptions( $method = 'select_option', $default = null ) |
|
| 138 | 138 | { |
| 139 | 139 | $this->args['default'] ?: $this->args['default'] = $default; |
| 140 | 140 | |
| 141 | - $method = $this->camelCase($method); |
|
| 141 | + $method = $this->camelCase( $method ); |
|
| 142 | 142 | |
| 143 | - $method = method_exists($this, $method) |
|
| 143 | + $method = method_exists( $this, $method ) |
|
| 144 | 144 | ? $method |
| 145 | 145 | : 'selectOption'; |
| 146 | 146 | |
| 147 | 147 | $i = 0; |
| 148 | 148 | |
| 149 | - if ('singleInput' === $method) { |
|
| 150 | - if (!isset($this->args['options']) || empty($this->args['options'])) { |
|
| 149 | + if( 'singleInput' === $method ) { |
|
| 150 | + if( !isset( $this->args['options'] ) || empty( $this->args['options'] ) ) { |
|
| 151 | 151 | return; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | // hack to make sure unset single checkbox values start at 1 instead of 0 |
| 155 | - if (0 === key($this->args['options'])) { |
|
| 155 | + if( 0 === key( $this->args['options'] ) ) { |
|
| 156 | 156 | $options = ['1' => $this->args['options'][0]]; |
| 157 | 157 | $this->args['options'] = $options; |
| 158 | 158 | } |
@@ -160,8 +160,8 @@ discard block |
||
| 160 | 160 | return $this->singleInput(); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - return array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$i, $method) { |
|
| 164 | - return $carry .= $this->$method($key, $i++); |
|
| 163 | + return array_reduce( array_keys( $this->args['options'] ), function( $carry, $key ) use ( &$i, $method ) { |
|
| 164 | + return $carry .= $this->$method( $key, $i++ ); |
|
| 165 | 165 | }); |
| 166 | 166 | } |
| 167 | 167 | |
@@ -172,15 +172,15 @@ discard block |
||
| 172 | 172 | * |
| 173 | 173 | * @return array|string |
| 174 | 174 | */ |
| 175 | - protected function normalize(array $defaults = [], $implode = false) |
|
| 175 | + protected function normalize( array $defaults = [], $implode = false ) |
|
| 176 | 176 | { |
| 177 | - $args = $this->mergeAttributesWith($defaults); |
|
| 177 | + $args = $this->mergeAttributesWith( $defaults ); |
|
| 178 | 178 | |
| 179 | 179 | $normalize = new Normalizer(); |
| 180 | 180 | |
| 181 | - return ($this->element && method_exists($normalize, $this->element)) |
|
| 182 | - ? $normalize->{$this->element}($args, $implode) |
|
| 183 | - : ((bool) $implode ? '' : []); |
|
| 181 | + return ( $this->element && method_exists( $normalize, $this->element ) ) |
|
| 182 | + ? $normalize->{$this->element}( $args, $implode ) |
|
| 183 | + : ( (bool) $implode ? '' : [] ); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | /** |
@@ -188,11 +188,11 @@ discard block |
||
| 188 | 188 | * |
| 189 | 189 | * @return array |
| 190 | 190 | */ |
| 191 | - protected function mergeAttributesWith(array $defaults) |
|
| 191 | + protected function mergeAttributesWith( array $defaults ) |
|
| 192 | 192 | { |
| 193 | 193 | // similar to array_merge except overwrite empty values |
| 194 | - foreach ($defaults as $key => $value) { |
|
| 195 | - if (isset($this->args[$key]) && !empty($this->args[$key])) { |
|
| 194 | + foreach( $defaults as $key => $value ) { |
|
| 195 | + if( isset( $this->args[$key] ) && !empty( $this->args[$key] ) ) { |
|
| 196 | 196 | continue; |
| 197 | 197 | } |
| 198 | 198 | $this->args[$key] = $value; |
@@ -201,7 +201,7 @@ discard block |
||
| 201 | 201 | $attributes = $this->args['attributes']; |
| 202 | 202 | |
| 203 | 203 | // prioritize $attributes over $this->args, don't worry about duplicates |
| 204 | - return array_merge($this->args, $attributes); |
|
| 204 | + return array_merge( $this->args, $attributes ); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | /** |
@@ -213,24 +213,24 @@ discard block |
||
| 213 | 213 | * |
| 214 | 214 | * @return string|null |
| 215 | 215 | */ |
| 216 | - protected function multiInput($optionKey, $number, $type = 'radio') |
|
| 216 | + protected function multiInput( $optionKey, $number, $type = 'radio' ) |
|
| 217 | 217 | { |
| 218 | - $args = $this->multiInputArgs($type, $optionKey, $number); |
|
| 218 | + $args = $this->multiInputArgs( $type, $optionKey, $number ); |
|
| 219 | 219 | |
| 220 | - if (!$args) { |
|
| 220 | + if( !$args ) { |
|
| 221 | 221 | return; |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | $attributes = ''; |
| 225 | 225 | |
| 226 | - foreach ($args['attributes'] as $key => $val) { |
|
| 227 | - $attributes .= sprintf('%s="%s" ', $key, $val); |
|
| 226 | + foreach( $args['attributes'] as $key => $val ) { |
|
| 227 | + $attributes .= sprintf( '%s="%s" ', $key, $val ); |
|
| 228 | 228 | } |
| 229 | 229 | |
| 230 | - return sprintf('<li><label for="%s"><input %s%s/> %s</label></li>', |
|
| 230 | + return sprintf( '<li><label for="%s"><input %s%s/> %s</label></li>', |
|
| 231 | 231 | $args['attributes']['id'], |
| 232 | 232 | $attributes, |
| 233 | - checked($args['value'], $args['attributes']['value'], false), |
|
| 233 | + checked( $args['value'], $args['attributes']['value'], false ), |
|
| 234 | 234 | $args['label'] |
| 235 | 235 | ); |
| 236 | 236 | } |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | * |
| 245 | 245 | * @return array|null |
| 246 | 246 | */ |
| 247 | - protected function multiInputArgs($type, $optionName, $number) |
|
| 247 | + protected function multiInputArgs( $type, $optionName, $number ) |
|
| 248 | 248 | { |
| 249 | 249 | $defaults = [ |
| 250 | 250 | 'class' => '', |
@@ -257,37 +257,37 @@ discard block |
||
| 257 | 257 | |
| 258 | 258 | $value = $this->args['options'][$optionName]; |
| 259 | 259 | |
| 260 | - if (is_array($value)) { |
|
| 260 | + if( is_array( $value ) ) { |
|
| 261 | 261 | $args = $value; |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | - if (is_string($value)) { |
|
| 264 | + if( is_string( $value ) ) { |
|
| 265 | 265 | $label = $value; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | - isset($args['name']) ?: $args['name'] = $optionName; |
|
| 269 | - isset($args['value']) ?: $args['value'] = $optionName; |
|
| 268 | + isset( $args['name'] ) ?: $args['name'] = $optionName; |
|
| 269 | + isset( $args['value'] ) ?: $args['value'] = $optionName; |
|
| 270 | 270 | |
| 271 | - $args = wp_parse_args($args, $defaults); |
|
| 271 | + $args = wp_parse_args( $args, $defaults ); |
|
| 272 | 272 | |
| 273 | - if (!isset($label) || '' === $args['name']) { |
|
| 273 | + if( !isset( $label ) || '' === $args['name'] ) { |
|
| 274 | 274 | return; |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | $args['id'] = $this->args['id']."-{$number}"; |
| 278 | - $args['name'] = $this->args['name'].('checkbox' === $type && $this->multi ? '[]' : ''); |
|
| 278 | + $args['name'] = $this->args['name'].( 'checkbox' === $type && $this->multi ? '[]' : '' ); |
|
| 279 | 279 | |
| 280 | - $args = array_filter($args, function ($value) { |
|
| 280 | + $args = array_filter( $args, function( $value ) { |
|
| 281 | 281 | return '' !== $value; |
| 282 | 282 | }); |
| 283 | 283 | |
| 284 | - if (is_array($this->args['value'])) { |
|
| 285 | - if (in_array($args['value'], $this->args['value'])) { |
|
| 284 | + if( is_array( $this->args['value'] ) ) { |
|
| 285 | + if( in_array( $args['value'], $this->args['value'] ) ) { |
|
| 286 | 286 | $this->args['default'] = $args['value']; |
| 287 | 287 | } |
| 288 | - } elseif ($this->args['value']) { |
|
| 288 | + } elseif( $this->args['value'] ) { |
|
| 289 | 289 | $this->args['default'] = $this->args['value']; |
| 290 | - } elseif ('radio' == $type && !$this->args['default']) { |
|
| 290 | + } elseif( 'radio' == $type && !$this->args['default'] ) { |
|
| 291 | 291 | $this->args['default'] = 0; |
| 292 | 292 | } |
| 293 | 293 | |
@@ -306,9 +306,9 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @return string|null |
| 308 | 308 | */ |
| 309 | - protected function multiInputCheckbox($optionKey, $number) |
|
| 309 | + protected function multiInputCheckbox( $optionKey, $number ) |
|
| 310 | 310 | { |
| 311 | - return $this->multiInput($optionKey, $number, 'checkbox'); |
|
| 311 | + return $this->multiInput( $optionKey, $number, 'checkbox' ); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | /** |
@@ -318,11 +318,11 @@ discard block |
||
| 318 | 318 | * |
| 319 | 319 | * @return string |
| 320 | 320 | */ |
| 321 | - protected function selectOption($optionKey) |
|
| 321 | + protected function selectOption( $optionKey ) |
|
| 322 | 322 | { |
| 323 | - return sprintf('<option value="%s"%s>%s</option>', |
|
| 323 | + return sprintf( '<option value="%s"%s>%s</option>', |
|
| 324 | 324 | $optionKey, |
| 325 | - selected($this->args['value'], $optionKey, false), |
|
| 325 | + selected( $this->args['value'], $optionKey, false ), |
|
| 326 | 326 | $this->args['options'][$optionKey] |
| 327 | 327 | ); |
| 328 | 328 | } |
@@ -334,29 +334,29 @@ discard block |
||
| 334 | 334 | * |
| 335 | 335 | * @return string|null |
| 336 | 336 | */ |
| 337 | - protected function singleInput($type = 'checkbox') |
|
| 337 | + protected function singleInput( $type = 'checkbox' ) |
|
| 338 | 338 | { |
| 339 | - $optionKey = key($this->args['options']); |
|
| 339 | + $optionKey = key( $this->args['options'] ); |
|
| 340 | 340 | |
| 341 | - $args = $this->multiInputArgs($type, $optionKey, 1); |
|
| 341 | + $args = $this->multiInputArgs( $type, $optionKey, 1 ); |
|
| 342 | 342 | |
| 343 | - if (!$args) { |
|
| 343 | + if( !$args ) { |
|
| 344 | 344 | return; |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | $atts = $this->normalize(); |
| 348 | - $atts = wp_parse_args($args['attributes'], $atts); |
|
| 348 | + $atts = wp_parse_args( $args['attributes'], $atts ); |
|
| 349 | 349 | |
| 350 | 350 | $attributes = ''; |
| 351 | 351 | |
| 352 | - foreach ($atts as $key => $val) { |
|
| 353 | - $attributes .= sprintf('%s="%s" ', $key, $val); |
|
| 352 | + foreach( $atts as $key => $val ) { |
|
| 353 | + $attributes .= sprintf( '%s="%s" ', $key, $val ); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | - return sprintf('<label for="%s"><input %s%s/> %s</label>', |
|
| 356 | + return sprintf( '<label for="%s"><input %s%s/> %s</label>', |
|
| 357 | 357 | $atts['id'], |
| 358 | 358 | $attributes, |
| 359 | - checked($args['value'], $atts['value'], false), |
|
| 359 | + checked( $args['value'], $atts['value'], false ), |
|
| 360 | 360 | $args['label'] |
| 361 | 361 | ); |
| 362 | 362 | } |
@@ -5,8 +5,7 @@ discard block |
||
| 5 | 5 | use Exception; |
| 6 | 6 | use GeminiLabs\Castor\Services\Normalizer; |
| 7 | 7 | |
| 8 | -abstract class Base |
|
| 9 | -{ |
|
| 8 | +abstract class Base { |
|
| 10 | 9 | /** |
| 11 | 10 | * @var array |
| 12 | 11 | */ |
@@ -285,9 +284,11 @@ discard block |
||
| 285 | 284 | if (in_array($args['value'], $this->args['value'])) { |
| 286 | 285 | $this->args['default'] = $args['value']; |
| 287 | 286 | } |
| 288 | - } elseif ($this->args['value']) { |
|
| 287 | + } |
|
| 288 | + elseif ($this->args['value']) { |
|
| 289 | 289 | $this->args['default'] = $this->args['value']; |
| 290 | - } elseif ('radio' == $type && !$this->args['default']) { |
|
| 290 | + } |
|
| 291 | + elseif ('radio' == $type && !$this->args['default']) { |
|
| 291 | 292 | $this->args['default'] = 0; |
| 292 | 293 | } |
| 293 | 294 | |
@@ -4,15 +4,15 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Number extends Text |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return string |
|
| 9 | - */ |
|
| 10 | - public function render() |
|
| 11 | - { |
|
| 12 | - return parent::render([ |
|
| 13 | - 'class' => 'small-text', |
|
| 14 | - 'min' => '0', |
|
| 15 | - 'type' => 'number', |
|
| 16 | - ]); |
|
| 17 | - } |
|
| 7 | + /** |
|
| 8 | + * @return string |
|
| 9 | + */ |
|
| 10 | + public function render() |
|
| 11 | + { |
|
| 12 | + return parent::render([ |
|
| 13 | + 'class' => 'small-text', |
|
| 14 | + 'min' => '0', |
|
| 15 | + 'type' => 'number', |
|
| 16 | + ]); |
|
| 17 | + } |
|
| 18 | 18 | } |
@@ -9,10 +9,10 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | public function render() |
| 11 | 11 | { |
| 12 | - return parent::render([ |
|
| 12 | + return parent::render( [ |
|
| 13 | 13 | 'class' => 'small-text', |
| 14 | 14 | 'min' => '0', |
| 15 | 15 | 'type' => 'number', |
| 16 | - ]); |
|
| 16 | + ] ); |
|
| 17 | 17 | } |
| 18 | 18 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Number extends Text |
|
| 6 | -{ |
|
| 5 | +class Number extends Text { |
|
| 7 | 6 | /** |
| 8 | 7 | * @return string |
| 9 | 8 | */ |
@@ -4,18 +4,18 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Submit extends Text |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return string |
|
| 9 | - */ |
|
| 10 | - public function render() |
|
| 11 | - { |
|
| 12 | - if (isset($this->args['name'])) { |
|
| 13 | - $this->args['name'] = 'submit'; |
|
| 14 | - } |
|
| 7 | + /** |
|
| 8 | + * @return string |
|
| 9 | + */ |
|
| 10 | + public function render() |
|
| 11 | + { |
|
| 12 | + if (isset($this->args['name'])) { |
|
| 13 | + $this->args['name'] = 'submit'; |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - return parent::render([ |
|
| 17 | - 'class' => 'button button-primary', |
|
| 18 | - 'type' => 'submit', |
|
| 19 | - ]); |
|
| 20 | - } |
|
| 16 | + return parent::render([ |
|
| 17 | + 'class' => 'button button-primary', |
|
| 18 | + 'type' => 'submit', |
|
| 19 | + ]); |
|
| 20 | + } |
|
| 21 | 21 | } |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | public function render() |
| 11 | 11 | { |
| 12 | - if (isset($this->args['name'])) { |
|
| 12 | + if( isset( $this->args['name'] ) ) { |
|
| 13 | 13 | $this->args['name'] = 'submit'; |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - return parent::render([ |
|
| 16 | + return parent::render( [ |
|
| 17 | 17 | 'class' => 'button button-primary', |
| 18 | 18 | 'type' => 'submit', |
| 19 | - ]); |
|
| 19 | + ] ); |
|
| 20 | 20 | } |
| 21 | 21 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Submit extends Text |
|
| 6 | -{ |
|
| 5 | +class Submit extends Text { |
|
| 7 | 6 | /** |
| 8 | 7 | * @return string |
| 9 | 8 | */ |
@@ -4,23 +4,23 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Textarea extends Base |
| 6 | 6 | { |
| 7 | - protected $element = 'textarea'; |
|
| 7 | + protected $element = 'textarea'; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @return string |
|
| 11 | - */ |
|
| 12 | - public function render(array $defaults = []) |
|
| 13 | - { |
|
| 14 | - $defaults = wp_parse_args($defaults, [ |
|
| 15 | - 'class' => 'large-text', |
|
| 16 | - 'rows' => 3, |
|
| 17 | - 'type' => 'textarea', |
|
| 18 | - ]); |
|
| 9 | + /** |
|
| 10 | + * @return string |
|
| 11 | + */ |
|
| 12 | + public function render(array $defaults = []) |
|
| 13 | + { |
|
| 14 | + $defaults = wp_parse_args($defaults, [ |
|
| 15 | + 'class' => 'large-text', |
|
| 16 | + 'rows' => 3, |
|
| 17 | + 'type' => 'textarea', |
|
| 18 | + ]); |
|
| 19 | 19 | |
| 20 | - return sprintf('<textarea %s>%s</textarea>%s', |
|
| 21 | - $this->implodeAttributes($defaults), |
|
| 22 | - $this->args['value'], |
|
| 23 | - $this->generateDescription() |
|
| 24 | - ); |
|
| 25 | - } |
|
| 20 | + return sprintf('<textarea %s>%s</textarea>%s', |
|
| 21 | + $this->implodeAttributes($defaults), |
|
| 22 | + $this->args['value'], |
|
| 23 | + $this->generateDescription() |
|
| 24 | + ); |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -9,16 +9,16 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * @return string |
| 11 | 11 | */ |
| 12 | - public function render(array $defaults = []) |
|
| 12 | + public function render( array $defaults = [] ) |
|
| 13 | 13 | { |
| 14 | - $defaults = wp_parse_args($defaults, [ |
|
| 14 | + $defaults = wp_parse_args( $defaults, [ |
|
| 15 | 15 | 'class' => 'large-text', |
| 16 | 16 | 'rows' => 3, |
| 17 | 17 | 'type' => 'textarea', |
| 18 | - ]); |
|
| 18 | + ] ); |
|
| 19 | 19 | |
| 20 | - return sprintf('<textarea %s>%s</textarea>%s', |
|
| 21 | - $this->implodeAttributes($defaults), |
|
| 20 | + return sprintf( '<textarea %s>%s</textarea>%s', |
|
| 21 | + $this->implodeAttributes( $defaults ), |
|
| 22 | 22 | $this->args['value'], |
| 23 | 23 | $this->generateDescription() |
| 24 | 24 | ); |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Textarea extends Base |
|
| 6 | -{ |
|
| 5 | +class Textarea extends Base { |
|
| 7 | 6 | protected $element = 'textarea'; |
| 8 | 7 | |
| 9 | 8 | /** |
@@ -4,26 +4,26 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Hidden extends Text |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return string |
|
| 9 | - */ |
|
| 10 | - public function render() |
|
| 11 | - { |
|
| 12 | - if (isset($this->args['label'])) { |
|
| 13 | - unset($this->args['label']); |
|
| 14 | - } |
|
| 7 | + /** |
|
| 8 | + * @return string |
|
| 9 | + */ |
|
| 10 | + public function render() |
|
| 11 | + { |
|
| 12 | + if (isset($this->args['label'])) { |
|
| 13 | + unset($this->args['label']); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - if (isset($this->args['desc'])) { |
|
| 17 | - unset($this->args['desc']); |
|
| 18 | - } |
|
| 16 | + if (isset($this->args['desc'])) { |
|
| 17 | + unset($this->args['desc']); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - if (isset($this->args['id'])) { |
|
| 21 | - unset($this->args['id']); |
|
| 22 | - } |
|
| 20 | + if (isset($this->args['id'])) { |
|
| 21 | + unset($this->args['id']); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - return parent::render([ |
|
| 25 | - 'class' => '', |
|
| 26 | - 'type' => 'hidden', |
|
| 27 | - ]); |
|
| 28 | - } |
|
| 24 | + return parent::render([ |
|
| 25 | + 'class' => '', |
|
| 26 | + 'type' => 'hidden', |
|
| 27 | + ]); |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -9,21 +9,21 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | public function render() |
| 11 | 11 | { |
| 12 | - if (isset($this->args['label'])) { |
|
| 13 | - unset($this->args['label']); |
|
| 12 | + if( isset( $this->args['label'] ) ) { |
|
| 13 | + unset( $this->args['label'] ); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - if (isset($this->args['desc'])) { |
|
| 17 | - unset($this->args['desc']); |
|
| 16 | + if( isset( $this->args['desc'] ) ) { |
|
| 17 | + unset( $this->args['desc'] ); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - if (isset($this->args['id'])) { |
|
| 21 | - unset($this->args['id']); |
|
| 20 | + if( isset( $this->args['id'] ) ) { |
|
| 21 | + unset( $this->args['id'] ); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - return parent::render([ |
|
| 24 | + return parent::render( [ |
|
| 25 | 25 | 'class' => '', |
| 26 | 26 | 'type' => 'hidden', |
| 27 | - ]); |
|
| 27 | + ] ); |
|
| 28 | 28 | } |
| 29 | 29 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Hidden extends Text |
|
| 6 | -{ |
|
| 5 | +class Hidden extends Text { |
|
| 7 | 6 | /** |
| 8 | 7 | * @return string |
| 9 | 8 | */ |
@@ -4,21 +4,21 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Select extends Base |
| 6 | 6 | { |
| 7 | - protected $element = 'select'; |
|
| 7 | + protected $element = 'select'; |
|
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * @return string |
|
| 11 | - */ |
|
| 12 | - public function render(array $defaults = []) |
|
| 13 | - { |
|
| 14 | - $defaults = wp_parse_args($defaults, [ |
|
| 15 | - 'type' => 'select', |
|
| 16 | - ]); |
|
| 9 | + /** |
|
| 10 | + * @return string |
|
| 11 | + */ |
|
| 12 | + public function render(array $defaults = []) |
|
| 13 | + { |
|
| 14 | + $defaults = wp_parse_args($defaults, [ |
|
| 15 | + 'type' => 'select', |
|
| 16 | + ]); |
|
| 17 | 17 | |
| 18 | - return sprintf('<select %s>%s</select>%s', |
|
| 19 | - $this->implodeAttributes($defaults), |
|
| 20 | - $this->implodeOptions('select_option'), |
|
| 21 | - $this->generateDescription() |
|
| 22 | - ); |
|
| 23 | - } |
|
| 18 | + return sprintf('<select %s>%s</select>%s', |
|
| 19 | + $this->implodeAttributes($defaults), |
|
| 20 | + $this->implodeOptions('select_option'), |
|
| 21 | + $this->generateDescription() |
|
| 22 | + ); |
|
| 23 | + } |
|
| 24 | 24 | } |
@@ -9,15 +9,15 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * @return string |
| 11 | 11 | */ |
| 12 | - public function render(array $defaults = []) |
|
| 12 | + public function render( array $defaults = [] ) |
|
| 13 | 13 | { |
| 14 | - $defaults = wp_parse_args($defaults, [ |
|
| 14 | + $defaults = wp_parse_args( $defaults, [ |
|
| 15 | 15 | 'type' => 'select', |
| 16 | - ]); |
|
| 16 | + ] ); |
|
| 17 | 17 | |
| 18 | - return sprintf('<select %s>%s</select>%s', |
|
| 19 | - $this->implodeAttributes($defaults), |
|
| 20 | - $this->implodeOptions('select_option'), |
|
| 18 | + return sprintf( '<select %s>%s</select>%s', |
|
| 19 | + $this->implodeAttributes( $defaults ), |
|
| 20 | + $this->implodeOptions( 'select_option' ), |
|
| 21 | 21 | $this->generateDescription() |
| 22 | 22 | ); |
| 23 | 23 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Select extends Base |
|
| 6 | -{ |
|
| 5 | +class Select extends Base { |
|
| 7 | 6 | protected $element = 'select'; |
| 8 | 7 | |
| 9 | 8 | /** |
@@ -4,35 +4,35 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Checkbox extends Base |
| 6 | 6 | { |
| 7 | - protected $element = 'input'; |
|
| 7 | + protected $element = 'input'; |
|
| 8 | 8 | |
| 9 | - public function __construct(array $args = []) |
|
| 10 | - { |
|
| 11 | - parent::__construct($args); |
|
| 9 | + public function __construct(array $args = []) |
|
| 10 | + { |
|
| 11 | + parent::__construct($args); |
|
| 12 | 12 | |
| 13 | - if (count($args['options']) > 1) { |
|
| 14 | - $this->multi = true; |
|
| 15 | - } |
|
| 16 | - } |
|
| 13 | + if (count($args['options']) > 1) { |
|
| 14 | + $this->multi = true; |
|
| 15 | + } |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @return string |
|
| 20 | - */ |
|
| 21 | - public function render() |
|
| 22 | - { |
|
| 23 | - $inline = $this->args['inline'] ? ' class="inline"' : ''; |
|
| 18 | + /** |
|
| 19 | + * @return string |
|
| 20 | + */ |
|
| 21 | + public function render() |
|
| 22 | + { |
|
| 23 | + $inline = $this->args['inline'] ? ' class="inline"' : ''; |
|
| 24 | 24 | |
| 25 | - if ($this->multi) { |
|
| 26 | - return sprintf('<ul%s>%s</ul>%s', |
|
| 27 | - $inline, |
|
| 28 | - $this->implodeOptions('multi_input_checkbox'), |
|
| 29 | - $this->generateDescription() |
|
| 30 | - ); |
|
| 31 | - } |
|
| 25 | + if ($this->multi) { |
|
| 26 | + return sprintf('<ul%s>%s</ul>%s', |
|
| 27 | + $inline, |
|
| 28 | + $this->implodeOptions('multi_input_checkbox'), |
|
| 29 | + $this->generateDescription() |
|
| 30 | + ); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - return sprintf('%s%s', |
|
| 34 | - $this->implodeOptions('single_input'), |
|
| 35 | - $this->generateDescription() |
|
| 36 | - ); |
|
| 37 | - } |
|
| 33 | + return sprintf('%s%s', |
|
| 34 | + $this->implodeOptions('single_input'), |
|
| 35 | + $this->generateDescription() |
|
| 36 | + ); |
|
| 37 | + } |
|
| 38 | 38 | } |
@@ -6,11 +6,11 @@ discard block |
||
| 6 | 6 | { |
| 7 | 7 | protected $element = 'input'; |
| 8 | 8 | |
| 9 | - public function __construct(array $args = []) |
|
| 9 | + public function __construct( array $args = [] ) |
|
| 10 | 10 | { |
| 11 | - parent::__construct($args); |
|
| 11 | + parent::__construct( $args ); |
|
| 12 | 12 | |
| 13 | - if (count($args['options']) > 1) { |
|
| 13 | + if( count( $args['options'] ) > 1 ) { |
|
| 14 | 14 | $this->multi = true; |
| 15 | 15 | } |
| 16 | 16 | } |
@@ -22,16 +22,16 @@ discard block |
||
| 22 | 22 | { |
| 23 | 23 | $inline = $this->args['inline'] ? ' class="inline"' : ''; |
| 24 | 24 | |
| 25 | - if ($this->multi) { |
|
| 26 | - return sprintf('<ul%s>%s</ul>%s', |
|
| 25 | + if( $this->multi ) { |
|
| 26 | + return sprintf( '<ul%s>%s</ul>%s', |
|
| 27 | 27 | $inline, |
| 28 | - $this->implodeOptions('multi_input_checkbox'), |
|
| 28 | + $this->implodeOptions( 'multi_input_checkbox' ), |
|
| 29 | 29 | $this->generateDescription() |
| 30 | 30 | ); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - return sprintf('%s%s', |
|
| 34 | - $this->implodeOptions('single_input'), |
|
| 33 | + return sprintf( '%s%s', |
|
| 34 | + $this->implodeOptions( 'single_input' ), |
|
| 35 | 35 | $this->generateDescription() |
| 36 | 36 | ); |
| 37 | 37 | } |
@@ -2,8 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace GeminiLabs\Castor\Forms\Fields; |
| 4 | 4 | |
| 5 | -class Checkbox extends Base |
|
| 6 | -{ |
|
| 5 | +class Checkbox extends Base { |
|
| 7 | 6 | protected $element = 'input'; |
| 8 | 7 | |
| 9 | 8 | public function __construct(array $args = []) |