@@ -328,7 +328,8 @@ |
||
| 328 | 328 | } |
| 329 | 329 | if (is_array($params[0])) { |
| 330 | 330 | $this->args += $params[0]; |
| 331 | - } elseif (is_array($params[1])) { |
|
| 331 | + } |
|
| 332 | + elseif (is_array($params[1])) { |
|
| 332 | 333 | $this->args += $params[1]; |
| 333 | 334 | } |
| 334 | 335 | if (!isset($this->args['is_public'])) { |
@@ -18,346 +18,346 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class Builder |
| 20 | 20 | { |
| 21 | - const INPUT_TYPES = [ |
|
| 22 | - 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
| 23 | - 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
| 24 | - 'url', 'week', |
|
| 25 | - ]; |
|
| 26 | - |
|
| 27 | - const TAGS_FORM = [ |
|
| 28 | - 'input', 'select', 'textarea', |
|
| 29 | - ]; |
|
| 30 | - |
|
| 31 | - const TAGS_SINGLE = [ |
|
| 32 | - 'img', |
|
| 33 | - ]; |
|
| 34 | - |
|
| 35 | - const TAGS_STRUCTURE = [ |
|
| 36 | - 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
| 37 | - ]; |
|
| 38 | - |
|
| 39 | - const TAGS_TEXT = [ |
|
| 40 | - 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
| 41 | - 'small', 'span', |
|
| 42 | - ]; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @var array |
|
| 46 | - */ |
|
| 47 | - public $args = []; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * @var bool |
|
| 51 | - */ |
|
| 52 | - public $render = false; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 57 | - public $tag; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @param string $method |
|
| 61 | - * @param array $args |
|
| 62 | - * @return string|void |
|
| 63 | - */ |
|
| 64 | - public function __call($method, $args) |
|
| 65 | - { |
|
| 66 | - $instance = new static(); |
|
| 67 | - $instance->setTagFromMethod($method); |
|
| 68 | - call_user_func_array([$instance, 'normalize'], $args += ['', '']); |
|
| 69 | - $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
| 70 | - do_action_ref_array('site-reviews/builder', [$instance]); |
|
| 71 | - $generatedTag = in_array($instance->tag, $tags) |
|
| 72 | - ? $instance->buildTag() |
|
| 73 | - : $instance->buildCustomField(); |
|
| 74 | - $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
| 75 | - if (!$this->render) { |
|
| 76 | - return $generatedTag; |
|
| 77 | - } |
|
| 78 | - echo $generatedTag; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param string $property |
|
| 83 | - * @param mixed $value |
|
| 84 | - * @return void |
|
| 85 | - */ |
|
| 86 | - public function __set($property, $value) |
|
| 87 | - { |
|
| 88 | - $properties = [ |
|
| 89 | - 'args' => 'is_array', |
|
| 90 | - 'render' => 'is_bool', |
|
| 91 | - 'tag' => 'is_string', |
|
| 92 | - ]; |
|
| 93 | - if (!isset($properties[$property]) |
|
| 94 | - || empty(array_filter([$value], $properties[$property])) |
|
| 95 | - ) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 98 | - $this->$property = $value; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return void|string |
|
| 103 | - */ |
|
| 104 | - public function getClosingTag() |
|
| 105 | - { |
|
| 106 | - if (empty($this->tag)) { |
|
| 107 | - return; |
|
| 108 | - } |
|
| 109 | - return '</'.$this->tag.'>'; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * @return void|string |
|
| 114 | - */ |
|
| 115 | - public function getOpeningTag() |
|
| 116 | - { |
|
| 117 | - if (empty($this->tag)) { |
|
| 118 | - return; |
|
| 119 | - } |
|
| 120 | - $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
| 121 | - return '<'.trim($this->tag.' '.$attributes).'>'; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return void|string |
|
| 126 | - */ |
|
| 127 | - public function getTag() |
|
| 128 | - { |
|
| 129 | - if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
| 130 | - return $this->getOpeningTag(); |
|
| 131 | - } |
|
| 132 | - if (!in_array($this->tag, static::TAGS_FORM)) { |
|
| 133 | - return $this->buildDefaultTag(); |
|
| 134 | - } |
|
| 135 | - return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * @return string |
|
| 140 | - */ |
|
| 141 | - public function raw(array $field) |
|
| 142 | - { |
|
| 143 | - unset($field['label']); |
|
| 144 | - return $this->{$field['type']}($field); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @return string|void |
|
| 149 | - */ |
|
| 150 | - protected function buildCustomField() |
|
| 151 | - { |
|
| 152 | - $className = $this->getCustomFieldClassName(); |
|
| 153 | - if (class_exists($className)) { |
|
| 154 | - return (new $className($this))->build(); |
|
| 155 | - } |
|
| 156 | - glsr_log()->error('Field missing: '.$className); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return string|void |
|
| 161 | - */ |
|
| 162 | - protected function buildDefaultTag($text = '') |
|
| 163 | - { |
|
| 164 | - if (empty($text)) { |
|
| 165 | - $text = $this->args['text']; |
|
| 166 | - } |
|
| 167 | - return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @return string|void |
|
| 172 | - */ |
|
| 173 | - protected function buildFieldDescription() |
|
| 174 | - { |
|
| 175 | - if (empty($this->args['description'])) { |
|
| 176 | - return; |
|
| 177 | - } |
|
| 178 | - if ($this->args['is_widget']) { |
|
| 179 | - return $this->small($this->args['description']); |
|
| 180 | - } |
|
| 181 | - return $this->p($this->args['description'], ['class' => 'description']); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return string|void |
|
| 186 | - */ |
|
| 187 | - protected function buildFormInput() |
|
| 188 | - { |
|
| 189 | - if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
| 190 | - if (isset($this->args['multiple'])) { |
|
| 191 | - $this->args['name'].= '[]'; |
|
| 192 | - } |
|
| 193 | - return $this->buildFormLabel().$this->getOpeningTag(); |
|
| 194 | - } |
|
| 195 | - return empty($this->args['options']) |
|
| 196 | - ? $this->buildFormInputChoice() |
|
| 197 | - : $this->buildFormInputMultiChoice(); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * @return string|void |
|
| 202 | - */ |
|
| 203 | - protected function buildFormInputChoice() |
|
| 204 | - { |
|
| 205 | - if (!empty($this->args['text'])) { |
|
| 206 | - $this->args['label'] = $this->args['text']; |
|
| 207 | - } |
|
| 208 | - if (!$this->args['is_public']) { |
|
| 209 | - return $this->buildFormLabel([ |
|
| 210 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 211 | - 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
| 212 | - ]); |
|
| 213 | - } |
|
| 214 | - return $this->getOpeningTag().$this->buildFormLabel([ |
|
| 215 | - 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 216 | - 'text' => $this->args['label'].'<span></span>', |
|
| 217 | - ]); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @return string|void |
|
| 222 | - */ |
|
| 223 | - protected function buildFormInputMultiChoice() |
|
| 224 | - { |
|
| 225 | - if ('checkbox' == $this->args['type']) { |
|
| 226 | - $this->args['name'].= '[]'; |
|
| 227 | - } |
|
| 228 | - $index = 0; |
|
| 229 | - $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
| 230 | - return $carry.$this->li($this->{$this->args['type']}([ |
|
| 231 | - 'checked' => in_array($key, (array) $this->args['value']), |
|
| 232 | - 'id' => $this->args['id'].'-'.$index++, |
|
| 233 | - 'name' => $this->args['name'], |
|
| 234 | - 'text' => $this->args['options'][$key], |
|
| 235 | - 'value' => $key, |
|
| 236 | - ])); |
|
| 237 | - }); |
|
| 238 | - return $this->ul($options, [ |
|
| 239 | - 'class' => $this->args['class'], |
|
| 240 | - 'id' => $this->args['id'], |
|
| 241 | - ]); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * @return void|string |
|
| 246 | - */ |
|
| 247 | - protected function buildFormLabel(array $customArgs = []) |
|
| 248 | - { |
|
| 249 | - if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
| 250 | - return; |
|
| 251 | - } |
|
| 252 | - return $this->label(wp_parse_args($customArgs, [ |
|
| 253 | - 'for' => $this->args['id'], |
|
| 254 | - 'is_public' => $this->args['is_public'], |
|
| 255 | - 'text' => $this->args['label'], |
|
| 256 | - 'type' => $this->args['type'], |
|
| 257 | - ])); |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @return string|void |
|
| 262 | - */ |
|
| 263 | - protected function buildFormSelect() |
|
| 264 | - { |
|
| 265 | - return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * @return string|void |
|
| 270 | - */ |
|
| 271 | - protected function buildFormSelectOptions() |
|
| 272 | - { |
|
| 273 | - return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
| 274 | - return $carry.$this->option([ |
|
| 275 | - 'selected' => $this->args['value'] === (string) $key, |
|
| 276 | - 'text' => $this->args['options'][$key], |
|
| 277 | - 'value' => $key, |
|
| 278 | - ]); |
|
| 279 | - }); |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @return string|void |
|
| 284 | - */ |
|
| 285 | - protected function buildFormTextarea() |
|
| 286 | - { |
|
| 287 | - return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * @return string|void |
|
| 292 | - */ |
|
| 293 | - protected function buildTag() |
|
| 294 | - { |
|
| 295 | - $this->mergeArgsWithRequiredDefaults(); |
|
| 296 | - return $this->getTag(); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * @return string |
|
| 301 | - */ |
|
| 302 | - protected function getCustomFieldClassName() |
|
| 303 | - { |
|
| 304 | - $classname = Helper::buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
| 305 | - return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * @return void |
|
| 310 | - */ |
|
| 311 | - protected function mergeArgsWithRequiredDefaults() |
|
| 312 | - { |
|
| 313 | - $className = $this->getCustomFieldClassName(); |
|
| 314 | - if (class_exists($className)) { |
|
| 315 | - $this->args = $className::merge($this->args); |
|
| 316 | - } |
|
| 317 | - $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * @param string|array ...$params |
|
| 322 | - * @return void |
|
| 323 | - */ |
|
| 324 | - protected function normalize(...$params) |
|
| 325 | - { |
|
| 326 | - if (is_string($params[0]) || is_numeric($params[0])) { |
|
| 327 | - $this->setNameOrTextAttributeForTag($params[0]); |
|
| 328 | - } |
|
| 329 | - if (is_array($params[0])) { |
|
| 330 | - $this->args += $params[0]; |
|
| 331 | - } elseif (is_array($params[1])) { |
|
| 332 | - $this->args += $params[1]; |
|
| 333 | - } |
|
| 334 | - if (!isset($this->args['is_public'])) { |
|
| 335 | - $this->args['is_public'] = false; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * @param string $value |
|
| 341 | - * @return void |
|
| 342 | - */ |
|
| 343 | - protected function setNameOrTextAttributeForTag($value) |
|
| 344 | - { |
|
| 345 | - $attribute = in_array($this->tag, static::TAGS_FORM) |
|
| 346 | - ? 'name' |
|
| 347 | - : 'text'; |
|
| 348 | - $this->args[$attribute] = $value; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @param string $method |
|
| 353 | - * @return void |
|
| 354 | - */ |
|
| 355 | - protected function setTagFromMethod($method) |
|
| 356 | - { |
|
| 357 | - $this->tag = strtolower($method); |
|
| 358 | - if (in_array($this->tag, static::INPUT_TYPES)) { |
|
| 359 | - $this->args['type'] = $this->tag; |
|
| 360 | - $this->tag = 'input'; |
|
| 361 | - } |
|
| 362 | - } |
|
| 21 | + const INPUT_TYPES = [ |
|
| 22 | + 'checkbox', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', |
|
| 23 | + 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', |
|
| 24 | + 'url', 'week', |
|
| 25 | + ]; |
|
| 26 | + |
|
| 27 | + const TAGS_FORM = [ |
|
| 28 | + 'input', 'select', 'textarea', |
|
| 29 | + ]; |
|
| 30 | + |
|
| 31 | + const TAGS_SINGLE = [ |
|
| 32 | + 'img', |
|
| 33 | + ]; |
|
| 34 | + |
|
| 35 | + const TAGS_STRUCTURE = [ |
|
| 36 | + 'div', 'form', 'nav', 'ol', 'section', 'ul', |
|
| 37 | + ]; |
|
| 38 | + |
|
| 39 | + const TAGS_TEXT = [ |
|
| 40 | + 'a', 'button', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'i', 'label', 'li', 'option', 'p', 'pre', |
|
| 41 | + 'small', 'span', |
|
| 42 | + ]; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @var array |
|
| 46 | + */ |
|
| 47 | + public $args = []; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * @var bool |
|
| 51 | + */ |
|
| 52 | + public $render = false; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | + public $tag; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @param string $method |
|
| 61 | + * @param array $args |
|
| 62 | + * @return string|void |
|
| 63 | + */ |
|
| 64 | + public function __call($method, $args) |
|
| 65 | + { |
|
| 66 | + $instance = new static(); |
|
| 67 | + $instance->setTagFromMethod($method); |
|
| 68 | + call_user_func_array([$instance, 'normalize'], $args += ['', '']); |
|
| 69 | + $tags = array_merge(static::TAGS_FORM, static::TAGS_SINGLE, static::TAGS_STRUCTURE, static::TAGS_TEXT); |
|
| 70 | + do_action_ref_array('site-reviews/builder', [$instance]); |
|
| 71 | + $generatedTag = in_array($instance->tag, $tags) |
|
| 72 | + ? $instance->buildTag() |
|
| 73 | + : $instance->buildCustomField(); |
|
| 74 | + $generatedTag = apply_filters('site-reviews/builder/result', $generatedTag, $instance); |
|
| 75 | + if (!$this->render) { |
|
| 76 | + return $generatedTag; |
|
| 77 | + } |
|
| 78 | + echo $generatedTag; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param string $property |
|
| 83 | + * @param mixed $value |
|
| 84 | + * @return void |
|
| 85 | + */ |
|
| 86 | + public function __set($property, $value) |
|
| 87 | + { |
|
| 88 | + $properties = [ |
|
| 89 | + 'args' => 'is_array', |
|
| 90 | + 'render' => 'is_bool', |
|
| 91 | + 'tag' => 'is_string', |
|
| 92 | + ]; |
|
| 93 | + if (!isset($properties[$property]) |
|
| 94 | + || empty(array_filter([$value], $properties[$property])) |
|
| 95 | + ) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + $this->$property = $value; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return void|string |
|
| 103 | + */ |
|
| 104 | + public function getClosingTag() |
|
| 105 | + { |
|
| 106 | + if (empty($this->tag)) { |
|
| 107 | + return; |
|
| 108 | + } |
|
| 109 | + return '</'.$this->tag.'>'; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * @return void|string |
|
| 114 | + */ |
|
| 115 | + public function getOpeningTag() |
|
| 116 | + { |
|
| 117 | + if (empty($this->tag)) { |
|
| 118 | + return; |
|
| 119 | + } |
|
| 120 | + $attributes = glsr(Attributes::class)->{$this->tag}($this->args)->toString(); |
|
| 121 | + return '<'.trim($this->tag.' '.$attributes).'>'; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return void|string |
|
| 126 | + */ |
|
| 127 | + public function getTag() |
|
| 128 | + { |
|
| 129 | + if (in_array($this->tag, static::TAGS_SINGLE)) { |
|
| 130 | + return $this->getOpeningTag(); |
|
| 131 | + } |
|
| 132 | + if (!in_array($this->tag, static::TAGS_FORM)) { |
|
| 133 | + return $this->buildDefaultTag(); |
|
| 134 | + } |
|
| 135 | + return call_user_func([$this, 'buildForm'.ucfirst($this->tag)]).$this->buildFieldDescription(); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * @return string |
|
| 140 | + */ |
|
| 141 | + public function raw(array $field) |
|
| 142 | + { |
|
| 143 | + unset($field['label']); |
|
| 144 | + return $this->{$field['type']}($field); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @return string|void |
|
| 149 | + */ |
|
| 150 | + protected function buildCustomField() |
|
| 151 | + { |
|
| 152 | + $className = $this->getCustomFieldClassName(); |
|
| 153 | + if (class_exists($className)) { |
|
| 154 | + return (new $className($this))->build(); |
|
| 155 | + } |
|
| 156 | + glsr_log()->error('Field missing: '.$className); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return string|void |
|
| 161 | + */ |
|
| 162 | + protected function buildDefaultTag($text = '') |
|
| 163 | + { |
|
| 164 | + if (empty($text)) { |
|
| 165 | + $text = $this->args['text']; |
|
| 166 | + } |
|
| 167 | + return $this->getOpeningTag().$text.$this->getClosingTag(); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @return string|void |
|
| 172 | + */ |
|
| 173 | + protected function buildFieldDescription() |
|
| 174 | + { |
|
| 175 | + if (empty($this->args['description'])) { |
|
| 176 | + return; |
|
| 177 | + } |
|
| 178 | + if ($this->args['is_widget']) { |
|
| 179 | + return $this->small($this->args['description']); |
|
| 180 | + } |
|
| 181 | + return $this->p($this->args['description'], ['class' => 'description']); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return string|void |
|
| 186 | + */ |
|
| 187 | + protected function buildFormInput() |
|
| 188 | + { |
|
| 189 | + if (!in_array($this->args['type'], ['checkbox', 'radio'])) { |
|
| 190 | + if (isset($this->args['multiple'])) { |
|
| 191 | + $this->args['name'].= '[]'; |
|
| 192 | + } |
|
| 193 | + return $this->buildFormLabel().$this->getOpeningTag(); |
|
| 194 | + } |
|
| 195 | + return empty($this->args['options']) |
|
| 196 | + ? $this->buildFormInputChoice() |
|
| 197 | + : $this->buildFormInputMultiChoice(); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * @return string|void |
|
| 202 | + */ |
|
| 203 | + protected function buildFormInputChoice() |
|
| 204 | + { |
|
| 205 | + if (!empty($this->args['text'])) { |
|
| 206 | + $this->args['label'] = $this->args['text']; |
|
| 207 | + } |
|
| 208 | + if (!$this->args['is_public']) { |
|
| 209 | + return $this->buildFormLabel([ |
|
| 210 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 211 | + 'text' => $this->getOpeningTag().' '.$this->args['label'].'<span></span>', |
|
| 212 | + ]); |
|
| 213 | + } |
|
| 214 | + return $this->getOpeningTag().$this->buildFormLabel([ |
|
| 215 | + 'class' => 'glsr-'.$this->args['type'].'-label', |
|
| 216 | + 'text' => $this->args['label'].'<span></span>', |
|
| 217 | + ]); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @return string|void |
|
| 222 | + */ |
|
| 223 | + protected function buildFormInputMultiChoice() |
|
| 224 | + { |
|
| 225 | + if ('checkbox' == $this->args['type']) { |
|
| 226 | + $this->args['name'].= '[]'; |
|
| 227 | + } |
|
| 228 | + $index = 0; |
|
| 229 | + $options = array_reduce(array_keys($this->args['options']), function ($carry, $key) use (&$index) { |
|
| 230 | + return $carry.$this->li($this->{$this->args['type']}([ |
|
| 231 | + 'checked' => in_array($key, (array) $this->args['value']), |
|
| 232 | + 'id' => $this->args['id'].'-'.$index++, |
|
| 233 | + 'name' => $this->args['name'], |
|
| 234 | + 'text' => $this->args['options'][$key], |
|
| 235 | + 'value' => $key, |
|
| 236 | + ])); |
|
| 237 | + }); |
|
| 238 | + return $this->ul($options, [ |
|
| 239 | + 'class' => $this->args['class'], |
|
| 240 | + 'id' => $this->args['id'], |
|
| 241 | + ]); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * @return void|string |
|
| 246 | + */ |
|
| 247 | + protected function buildFormLabel(array $customArgs = []) |
|
| 248 | + { |
|
| 249 | + if (empty($this->args['label']) || 'hidden' == $this->args['type']) { |
|
| 250 | + return; |
|
| 251 | + } |
|
| 252 | + return $this->label(wp_parse_args($customArgs, [ |
|
| 253 | + 'for' => $this->args['id'], |
|
| 254 | + 'is_public' => $this->args['is_public'], |
|
| 255 | + 'text' => $this->args['label'], |
|
| 256 | + 'type' => $this->args['type'], |
|
| 257 | + ])); |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @return string|void |
|
| 262 | + */ |
|
| 263 | + protected function buildFormSelect() |
|
| 264 | + { |
|
| 265 | + return $this->buildFormLabel().$this->buildDefaultTag($this->buildFormSelectOptions()); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * @return string|void |
|
| 270 | + */ |
|
| 271 | + protected function buildFormSelectOptions() |
|
| 272 | + { |
|
| 273 | + return array_reduce(array_keys($this->args['options']), function ($carry, $key) { |
|
| 274 | + return $carry.$this->option([ |
|
| 275 | + 'selected' => $this->args['value'] === (string) $key, |
|
| 276 | + 'text' => $this->args['options'][$key], |
|
| 277 | + 'value' => $key, |
|
| 278 | + ]); |
|
| 279 | + }); |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @return string|void |
|
| 284 | + */ |
|
| 285 | + protected function buildFormTextarea() |
|
| 286 | + { |
|
| 287 | + return $this->buildFormLabel().$this->buildDefaultTag($this->args['value']); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * @return string|void |
|
| 292 | + */ |
|
| 293 | + protected function buildTag() |
|
| 294 | + { |
|
| 295 | + $this->mergeArgsWithRequiredDefaults(); |
|
| 296 | + return $this->getTag(); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * @return string |
|
| 301 | + */ |
|
| 302 | + protected function getCustomFieldClassName() |
|
| 303 | + { |
|
| 304 | + $classname = Helper::buildClassName($this->tag, __NAMESPACE__.'\Fields'); |
|
| 305 | + return apply_filters('site-reviews/builder/field/'.$this->tag, $classname); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * @return void |
|
| 310 | + */ |
|
| 311 | + protected function mergeArgsWithRequiredDefaults() |
|
| 312 | + { |
|
| 313 | + $className = $this->getCustomFieldClassName(); |
|
| 314 | + if (class_exists($className)) { |
|
| 315 | + $this->args = $className::merge($this->args); |
|
| 316 | + } |
|
| 317 | + $this->args = glsr(BuilderDefaults::class)->merge($this->args); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * @param string|array ...$params |
|
| 322 | + * @return void |
|
| 323 | + */ |
|
| 324 | + protected function normalize(...$params) |
|
| 325 | + { |
|
| 326 | + if (is_string($params[0]) || is_numeric($params[0])) { |
|
| 327 | + $this->setNameOrTextAttributeForTag($params[0]); |
|
| 328 | + } |
|
| 329 | + if (is_array($params[0])) { |
|
| 330 | + $this->args += $params[0]; |
|
| 331 | + } elseif (is_array($params[1])) { |
|
| 332 | + $this->args += $params[1]; |
|
| 333 | + } |
|
| 334 | + if (!isset($this->args['is_public'])) { |
|
| 335 | + $this->args['is_public'] = false; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * @param string $value |
|
| 341 | + * @return void |
|
| 342 | + */ |
|
| 343 | + protected function setNameOrTextAttributeForTag($value) |
|
| 344 | + { |
|
| 345 | + $attribute = in_array($this->tag, static::TAGS_FORM) |
|
| 346 | + ? 'name' |
|
| 347 | + : 'text'; |
|
| 348 | + $this->args[$attribute] = $value; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @param string $method |
|
| 353 | + * @return void |
|
| 354 | + */ |
|
| 355 | + protected function setTagFromMethod($method) |
|
| 356 | + { |
|
| 357 | + $this->tag = strtolower($method); |
|
| 358 | + if (in_array($this->tag, static::INPUT_TYPES)) { |
|
| 359 | + $this->args['type'] = $this->tag; |
|
| 360 | + $this->tag = 'input'; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | 363 | } |
@@ -4,9 +4,9 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface ShortcodeContract |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @params string|array $atts |
|
| 9 | - * @return string |
|
| 10 | - */ |
|
| 11 | - public function buildShortcode($atts = []); |
|
| 7 | + /** |
|
| 8 | + * @params string|array $atts |
|
| 9 | + * @return string |
|
| 10 | + */ |
|
| 11 | + public function buildShortcode($atts = []); |
|
| 12 | 12 | } |
@@ -6,83 +6,83 @@ |
||
| 6 | 6 | |
| 7 | 7 | class SiteReviewsSummaryBlock extends BlockGenerator |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function attributes() |
|
| 13 | - { |
|
| 14 | - return [ |
|
| 15 | - 'assigned_to' => [ |
|
| 16 | - 'default' => '', |
|
| 17 | - 'type' => 'string', |
|
| 18 | - ], |
|
| 19 | - 'category' => [ |
|
| 20 | - 'default' => '', |
|
| 21 | - 'type' => 'string', |
|
| 22 | - ], |
|
| 23 | - 'className' => [ |
|
| 24 | - 'default' => '', |
|
| 25 | - 'type' => 'string', |
|
| 26 | - ], |
|
| 27 | - 'hide' => [ |
|
| 28 | - 'default' => '', |
|
| 29 | - 'type' => 'string', |
|
| 30 | - ], |
|
| 31 | - 'post_id' => [ |
|
| 32 | - 'default' => '', |
|
| 33 | - 'type' => 'string', |
|
| 34 | - ], |
|
| 35 | - 'rating' => [ |
|
| 36 | - 'default' => '1', |
|
| 37 | - 'type' => 'number', |
|
| 38 | - ], |
|
| 39 | - 'schema' => [ |
|
| 40 | - 'default' => false, |
|
| 41 | - 'type' => 'boolean', |
|
| 42 | - ], |
|
| 43 | - 'type' => [ |
|
| 44 | - 'default' => 'local', |
|
| 45 | - 'type' => 'string', |
|
| 46 | - ], |
|
| 47 | - ]; |
|
| 48 | - } |
|
| 9 | + /** |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function attributes() |
|
| 13 | + { |
|
| 14 | + return [ |
|
| 15 | + 'assigned_to' => [ |
|
| 16 | + 'default' => '', |
|
| 17 | + 'type' => 'string', |
|
| 18 | + ], |
|
| 19 | + 'category' => [ |
|
| 20 | + 'default' => '', |
|
| 21 | + 'type' => 'string', |
|
| 22 | + ], |
|
| 23 | + 'className' => [ |
|
| 24 | + 'default' => '', |
|
| 25 | + 'type' => 'string', |
|
| 26 | + ], |
|
| 27 | + 'hide' => [ |
|
| 28 | + 'default' => '', |
|
| 29 | + 'type' => 'string', |
|
| 30 | + ], |
|
| 31 | + 'post_id' => [ |
|
| 32 | + 'default' => '', |
|
| 33 | + 'type' => 'string', |
|
| 34 | + ], |
|
| 35 | + 'rating' => [ |
|
| 36 | + 'default' => '1', |
|
| 37 | + 'type' => 'number', |
|
| 38 | + ], |
|
| 39 | + 'schema' => [ |
|
| 40 | + 'default' => false, |
|
| 41 | + 'type' => 'boolean', |
|
| 42 | + ], |
|
| 43 | + 'type' => [ |
|
| 44 | + 'default' => 'local', |
|
| 45 | + 'type' => 'string', |
|
| 46 | + ], |
|
| 47 | + ]; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return string |
|
| 52 | - */ |
|
| 53 | - public function render(array $attributes) |
|
| 54 | - { |
|
| 55 | - $attributes['class'] = $attributes['className']; |
|
| 56 | - $shortcode = glsr(Shortcode::class); |
|
| 57 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
| 58 | - $attributes = $this->normalize($attributes); |
|
| 59 | - $this->filterShortcodeClass(); |
|
| 60 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
| 61 | - $this->filterInterpolation(); |
|
| 62 | - } |
|
| 63 | - } |
|
| 64 | - return $shortcode->buildShortcode($attributes); |
|
| 65 | - } |
|
| 50 | + /** |
|
| 51 | + * @return string |
|
| 52 | + */ |
|
| 53 | + public function render(array $attributes) |
|
| 54 | + { |
|
| 55 | + $attributes['class'] = $attributes['className']; |
|
| 56 | + $shortcode = glsr(Shortcode::class); |
|
| 57 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
| 58 | + $attributes = $this->normalize($attributes); |
|
| 59 | + $this->filterShortcodeClass(); |
|
| 60 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
| 61 | + $this->filterInterpolation(); |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | + return $shortcode->buildShortcode($attributes); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @return void |
|
| 69 | - */ |
|
| 70 | - protected function filterInterpolation() |
|
| 71 | - { |
|
| 72 | - add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
| 73 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
| 74 | - $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
| 75 | - return $context; |
|
| 76 | - }); |
|
| 77 | - } |
|
| 67 | + /** |
|
| 68 | + * @return void |
|
| 69 | + */ |
|
| 70 | + protected function filterInterpolation() |
|
| 71 | + { |
|
| 72 | + add_filter('site-reviews/interpolate/reviews-summary', function ($context) { |
|
| 73 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
| 74 | + $context['text'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
| 75 | + return $context; |
|
| 76 | + }); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * @return void |
|
| 81 | - */ |
|
| 82 | - protected function filterShortcodeClass() |
|
| 83 | - { |
|
| 84 | - add_filter('site-reviews/style', function () { |
|
| 85 | - return 'default'; |
|
| 86 | - }); |
|
| 87 | - } |
|
| 79 | + /** |
|
| 80 | + * @return void |
|
| 81 | + */ |
|
| 82 | + protected function filterShortcodeClass() |
|
| 83 | + { |
|
| 84 | + add_filter('site-reviews/style', function () { |
|
| 85 | + return 'default'; |
|
| 86 | + }); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -6,65 +6,65 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BlockGenerator |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function attributes() |
|
| 13 | - { |
|
| 14 | - return []; |
|
| 15 | - } |
|
| 9 | + /** |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function attributes() |
|
| 13 | + { |
|
| 14 | + return []; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - public function normalize(array $attributes) |
|
| 21 | - { |
|
| 22 | - $hide = array_flip(explode(',', $attributes['hide'])); |
|
| 23 | - unset($hide['if_empty']); |
|
| 24 | - $attributes['hide'] = implode(',', array_keys($hide)); |
|
| 25 | - if (!isset($attributes['assigned_to'])) { |
|
| 26 | - return $attributes; |
|
| 27 | - } |
|
| 28 | - if ('post_id' == $attributes['assigned_to']) { |
|
| 29 | - $attributes['assigned_to'] = $attributes['post_id']; |
|
| 30 | - } elseif ('parent_id' == $attributes['assigned_to']) { |
|
| 31 | - $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
| 32 | - } |
|
| 33 | - return $attributes; |
|
| 34 | - } |
|
| 17 | + /** |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + public function normalize(array $attributes) |
|
| 21 | + { |
|
| 22 | + $hide = array_flip(explode(',', $attributes['hide'])); |
|
| 23 | + unset($hide['if_empty']); |
|
| 24 | + $attributes['hide'] = implode(',', array_keys($hide)); |
|
| 25 | + if (!isset($attributes['assigned_to'])) { |
|
| 26 | + return $attributes; |
|
| 27 | + } |
|
| 28 | + if ('post_id' == $attributes['assigned_to']) { |
|
| 29 | + $attributes['assigned_to'] = $attributes['post_id']; |
|
| 30 | + } elseif ('parent_id' == $attributes['assigned_to']) { |
|
| 31 | + $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
|
| 32 | + } |
|
| 33 | + return $attributes; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function register($block) |
|
| 40 | - { |
|
| 41 | - if (!function_exists('register_block_type')) { |
|
| 42 | - return; |
|
| 43 | - } |
|
| 44 | - register_block_type(Application::ID.'/'.$block, [ |
|
| 45 | - 'attributes' => $this->attributes(), |
|
| 46 | - 'editor_script' => Application::ID.'/blocks', |
|
| 47 | - 'editor_style' => Application::ID.'/blocks', |
|
| 48 | - 'render_callback' => [$this, 'render'], |
|
| 49 | - 'style' => Application::ID, |
|
| 50 | - ]); |
|
| 51 | - } |
|
| 36 | + /** |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function register($block) |
|
| 40 | + { |
|
| 41 | + if (!function_exists('register_block_type')) { |
|
| 42 | + return; |
|
| 43 | + } |
|
| 44 | + register_block_type(Application::ID.'/'.$block, [ |
|
| 45 | + 'attributes' => $this->attributes(), |
|
| 46 | + 'editor_script' => Application::ID.'/blocks', |
|
| 47 | + 'editor_style' => Application::ID.'/blocks', |
|
| 48 | + 'render_callback' => [$this, 'render'], |
|
| 49 | + 'style' => Application::ID, |
|
| 50 | + ]); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return void |
|
| 55 | - */ |
|
| 56 | - abstract public function render(array $attributes); |
|
| 53 | + /** |
|
| 54 | + * @return void |
|
| 55 | + */ |
|
| 56 | + abstract public function render(array $attributes); |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @param mixed $shortcode |
|
| 60 | - * @return bool |
|
| 61 | - */ |
|
| 62 | - protected function hasVisibleFields($shortcode, array $attributes) |
|
| 63 | - { |
|
| 64 | - $args = $shortcode->normalizeAtts($attributes); |
|
| 65 | - $defaults = $shortcode->getHideOptions(); |
|
| 66 | - $hide = array_flip($args['hide']); |
|
| 67 | - unset($defaults['if_empty'], $hide['if_empty']); |
|
| 68 | - return !empty(array_diff_key($defaults, $hide)); |
|
| 69 | - } |
|
| 58 | + /** |
|
| 59 | + * @param mixed $shortcode |
|
| 60 | + * @return bool |
|
| 61 | + */ |
|
| 62 | + protected function hasVisibleFields($shortcode, array $attributes) |
|
| 63 | + { |
|
| 64 | + $args = $shortcode->normalizeAtts($attributes); |
|
| 65 | + $defaults = $shortcode->getHideOptions(); |
|
| 66 | + $hide = array_flip($args['hide']); |
|
| 67 | + unset($defaults['if_empty'], $hide['if_empty']); |
|
| 68 | + return !empty(array_diff_key($defaults, $hide)); |
|
| 69 | + } |
|
| 70 | 70 | } |
@@ -27,7 +27,8 @@ |
||
| 27 | 27 | } |
| 28 | 28 | if ('post_id' == $attributes['assigned_to']) { |
| 29 | 29 | $attributes['assigned_to'] = $attributes['post_id']; |
| 30 | - } elseif ('parent_id' == $attributes['assigned_to']) { |
|
| 30 | + } |
|
| 31 | + elseif ('parent_id' == $attributes['assigned_to']) { |
|
| 31 | 32 | $attributes['assigned_to'] = wp_get_post_parent_id($attributes['post_id']); |
| 32 | 33 | } |
| 33 | 34 | return $attributes; |
@@ -4,10 +4,10 @@ |
||
| 4 | 4 | |
| 5 | 5 | class RegisterWidgets |
| 6 | 6 | { |
| 7 | - public $widgets; |
|
| 7 | + public $widgets; |
|
| 8 | 8 | |
| 9 | - public function __construct($input) |
|
| 10 | - { |
|
| 11 | - $this->widgets = $input; |
|
| 12 | - } |
|
| 9 | + public function __construct($input) |
|
| 10 | + { |
|
| 11 | + $this->widgets = $input; |
|
| 12 | + } |
|
| 13 | 13 | } |
@@ -1,7 +1,7 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | - 'fields' => [ |
|
| 5 | - 'label' => 'et_pb_contact_form_label', |
|
| 6 | - ], |
|
| 4 | + 'fields' => [ |
|
| 5 | + 'label' => 'et_pb_contact_form_label', |
|
| 6 | + ], |
|
| 7 | 7 | ]; |
@@ -12,297 +12,297 @@ |
||
| 12 | 12 | |
| 13 | 13 | class SiteReviewsForm |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $args; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $args; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $errors; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $errors; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $message; |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $message; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $required; |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $required; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $values; |
|
| 35 | + /** |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $values; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return void|string |
|
| 42 | - */ |
|
| 43 | - public function build(array $args = []) |
|
| 44 | - { |
|
| 45 | - $this->args = $args; |
|
| 46 | - if (!is_user_logged_in() && glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
| 47 | - return $this->buildLoginRegister(); |
|
| 48 | - } |
|
| 49 | - $this->errors = glsr()->sessionGet($args['id'].'errors', []); |
|
| 50 | - $this->message = glsr()->sessionGet($args['id'].'message', ''); |
|
| 51 | - $this->required = glsr(OptionManager::class)->get('settings.submissions.required', []); |
|
| 52 | - $this->values = glsr()->sessionGet($args['id'].'values', []); |
|
| 53 | - $fields = array_reduce($this->getFields(), function ($carry, $field) { |
|
| 54 | - return $carry.$field; |
|
| 55 | - }); |
|
| 56 | - return glsr(Template::class)->build('templates/reviews-form', [ |
|
| 57 | - 'args' => $args, |
|
| 58 | - 'context' => [ |
|
| 59 | - 'class' => $this->getClass(), |
|
| 60 | - 'fields' => $fields, |
|
| 61 | - 'id' => $this->args['id'], |
|
| 62 | - 'response' => $this->buildResponse(), |
|
| 63 | - 'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(), |
|
| 64 | - ], |
|
| 65 | - ]); |
|
| 66 | - } |
|
| 40 | + /** |
|
| 41 | + * @return void|string |
|
| 42 | + */ |
|
| 43 | + public function build(array $args = []) |
|
| 44 | + { |
|
| 45 | + $this->args = $args; |
|
| 46 | + if (!is_user_logged_in() && glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
| 47 | + return $this->buildLoginRegister(); |
|
| 48 | + } |
|
| 49 | + $this->errors = glsr()->sessionGet($args['id'].'errors', []); |
|
| 50 | + $this->message = glsr()->sessionGet($args['id'].'message', ''); |
|
| 51 | + $this->required = glsr(OptionManager::class)->get('settings.submissions.required', []); |
|
| 52 | + $this->values = glsr()->sessionGet($args['id'].'values', []); |
|
| 53 | + $fields = array_reduce($this->getFields(), function ($carry, $field) { |
|
| 54 | + return $carry.$field; |
|
| 55 | + }); |
|
| 56 | + return glsr(Template::class)->build('templates/reviews-form', [ |
|
| 57 | + 'args' => $args, |
|
| 58 | + 'context' => [ |
|
| 59 | + 'class' => $this->getClass(), |
|
| 60 | + 'fields' => $fields, |
|
| 61 | + 'id' => $this->args['id'], |
|
| 62 | + 'response' => $this->buildResponse(), |
|
| 63 | + 'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(), |
|
| 64 | + ], |
|
| 65 | + ]); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - protected function buildLoginRegister() |
|
| 72 | - { |
|
| 73 | - return glsr(Template::class)->build('templates/login-register', [ |
|
| 74 | - 'context' => [ |
|
| 75 | - 'text' => trim($this->getLoginText().' '.$this->getRegisterText()), |
|
| 76 | - ], |
|
| 77 | - ]); |
|
| 78 | - } |
|
| 68 | + /** |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + protected function buildLoginRegister() |
|
| 72 | + { |
|
| 73 | + return glsr(Template::class)->build('templates/login-register', [ |
|
| 74 | + 'context' => [ |
|
| 75 | + 'text' => trim($this->getLoginText().' '.$this->getRegisterText()), |
|
| 76 | + ], |
|
| 77 | + ]); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @return void|string |
|
| 82 | - */ |
|
| 83 | - protected function buildRecaptcha() |
|
| 84 | - { |
|
| 85 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
| 86 | - return; |
|
| 87 | - } |
|
| 88 | - return glsr(Builder::class)->div([ |
|
| 89 | - 'class' => 'glsr-recaptcha-holder', |
|
| 90 | - 'data-badge' => glsr(OptionManager::class)->get('settings.submissions.recaptcha.position'), |
|
| 91 | - 'data-sitekey' => sanitize_text_field(glsr(OptionManager::class)->get('settings.submissions.recaptcha.key')), |
|
| 92 | - 'data-size' => 'invisible', |
|
| 93 | - ]); |
|
| 94 | - } |
|
| 80 | + /** |
|
| 81 | + * @return void|string |
|
| 82 | + */ |
|
| 83 | + protected function buildRecaptcha() |
|
| 84 | + { |
|
| 85 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
| 86 | + return; |
|
| 87 | + } |
|
| 88 | + return glsr(Builder::class)->div([ |
|
| 89 | + 'class' => 'glsr-recaptcha-holder', |
|
| 90 | + 'data-badge' => glsr(OptionManager::class)->get('settings.submissions.recaptcha.position'), |
|
| 91 | + 'data-sitekey' => sanitize_text_field(glsr(OptionManager::class)->get('settings.submissions.recaptcha.key')), |
|
| 92 | + 'data-size' => 'invisible', |
|
| 93 | + ]); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - protected function buildResponse() |
|
| 100 | - { |
|
| 101 | - $classes = !empty($this->errors) |
|
| 102 | - ? glsr(StyleValidationDefaults::class)->defaults()['message_error_class'] |
|
| 103 | - : ''; |
|
| 104 | - return glsr(Template::class)->build('templates/form/response', [ |
|
| 105 | - 'context' => [ |
|
| 106 | - 'class' => $classes, |
|
| 107 | - 'message' => wpautop($this->message), |
|
| 108 | - ], |
|
| 109 | - 'has_errors' => !empty($this->errors), |
|
| 110 | - ]); |
|
| 111 | - } |
|
| 96 | + /** |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + protected function buildResponse() |
|
| 100 | + { |
|
| 101 | + $classes = !empty($this->errors) |
|
| 102 | + ? glsr(StyleValidationDefaults::class)->defaults()['message_error_class'] |
|
| 103 | + : ''; |
|
| 104 | + return glsr(Template::class)->build('templates/form/response', [ |
|
| 105 | + 'context' => [ |
|
| 106 | + 'class' => $classes, |
|
| 107 | + 'message' => wpautop($this->message), |
|
| 108 | + ], |
|
| 109 | + 'has_errors' => !empty($this->errors), |
|
| 110 | + ]); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - protected function buildSubmitButton() |
|
| 117 | - { |
|
| 118 | - return glsr(Template::class)->build('templates/form/submit-button', [ |
|
| 119 | - 'context' => [ |
|
| 120 | - 'text' => __('Submit your review', 'site-reviews'), |
|
| 121 | - ], |
|
| 122 | - ]); |
|
| 123 | - } |
|
| 113 | + /** |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + protected function buildSubmitButton() |
|
| 117 | + { |
|
| 118 | + return glsr(Template::class)->build('templates/form/submit-button', [ |
|
| 119 | + 'context' => [ |
|
| 120 | + 'text' => __('Submit your review', 'site-reviews'), |
|
| 121 | + ], |
|
| 122 | + ]); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @return string |
|
| 127 | - */ |
|
| 128 | - protected function getClass() |
|
| 129 | - { |
|
| 130 | - return trim('glsr-form glsr-'.glsr(Style::class)->get().' '.$this->args['class']); |
|
| 131 | - } |
|
| 125 | + /** |
|
| 126 | + * @return string |
|
| 127 | + */ |
|
| 128 | + protected function getClass() |
|
| 129 | + { |
|
| 130 | + return trim('glsr-form glsr-'.glsr(Style::class)->get().' '.$this->args['class']); |
|
| 131 | + } |
|
| 132 | 132 | |
| 133 | - /** |
|
| 134 | - * @return array |
|
| 135 | - */ |
|
| 136 | - protected function getFields() |
|
| 137 | - { |
|
| 138 | - $hiddenFields = $this->getHiddenFields(); |
|
| 139 | - $hiddenFields[] = $this->getHoneypotField(); |
|
| 140 | - $fields = $this->normalizeFields(glsr(Form::class)->getFields('submission-form')); |
|
| 141 | - $paths = array_map(function ($obj) { |
|
| 142 | - return $obj->field['path']; |
|
| 143 | - }, $hiddenFields); |
|
| 144 | - foreach ($fields as $field) { |
|
| 145 | - $index = array_search($field->field['path'], $paths); |
|
| 146 | - if (false === $index) { |
|
| 147 | - continue; |
|
| 148 | - } |
|
| 149 | - unset($hiddenFields[$index]); |
|
| 150 | - } |
|
| 151 | - return array_merge($hiddenFields, $fields); |
|
| 152 | - } |
|
| 133 | + /** |
|
| 134 | + * @return array |
|
| 135 | + */ |
|
| 136 | + protected function getFields() |
|
| 137 | + { |
|
| 138 | + $hiddenFields = $this->getHiddenFields(); |
|
| 139 | + $hiddenFields[] = $this->getHoneypotField(); |
|
| 140 | + $fields = $this->normalizeFields(glsr(Form::class)->getFields('submission-form')); |
|
| 141 | + $paths = array_map(function ($obj) { |
|
| 142 | + return $obj->field['path']; |
|
| 143 | + }, $hiddenFields); |
|
| 144 | + foreach ($fields as $field) { |
|
| 145 | + $index = array_search($field->field['path'], $paths); |
|
| 146 | + if (false === $index) { |
|
| 147 | + continue; |
|
| 148 | + } |
|
| 149 | + unset($hiddenFields[$index]); |
|
| 150 | + } |
|
| 151 | + return array_merge($hiddenFields, $fields); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - protected function getLoginText() |
|
| 158 | - { |
|
| 159 | - $loginLink = glsr(Builder::class)->a([ |
|
| 160 | - 'href' => wp_login_url(strval(get_permalink())), |
|
| 161 | - 'text' => __('logged in', 'site-reviews'), |
|
| 162 | - ]); |
|
| 163 | - return sprintf(__('You must be %s to submit a review.', 'site-reviews'), $loginLink); |
|
| 164 | - } |
|
| 154 | + /** |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + protected function getLoginText() |
|
| 158 | + { |
|
| 159 | + $loginLink = glsr(Builder::class)->a([ |
|
| 160 | + 'href' => wp_login_url(strval(get_permalink())), |
|
| 161 | + 'text' => __('logged in', 'site-reviews'), |
|
| 162 | + ]); |
|
| 163 | + return sprintf(__('You must be %s to submit a review.', 'site-reviews'), $loginLink); |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * @return void|string |
|
| 168 | - */ |
|
| 169 | - protected function getRegisterText() |
|
| 170 | - { |
|
| 171 | - if (!get_option('users_can_register') || !glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
| 172 | - return; |
|
| 173 | - } |
|
| 174 | - $registerLink = glsr(Builder::class)->a([ |
|
| 175 | - 'href' => wp_registration_url(), |
|
| 176 | - 'text' => __('register', 'site-reviews'), |
|
| 177 | - ]); |
|
| 178 | - return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
|
| 179 | - } |
|
| 166 | + /** |
|
| 167 | + * @return void|string |
|
| 168 | + */ |
|
| 169 | + protected function getRegisterText() |
|
| 170 | + { |
|
| 171 | + if (!get_option('users_can_register') || !glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
| 172 | + return; |
|
| 173 | + } |
|
| 174 | + $registerLink = glsr(Builder::class)->a([ |
|
| 175 | + 'href' => wp_registration_url(), |
|
| 176 | + 'text' => __('register', 'site-reviews'), |
|
| 177 | + ]); |
|
| 178 | + return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * @return array |
|
| 183 | - */ |
|
| 184 | - protected function getHiddenFields() |
|
| 185 | - { |
|
| 186 | - $fields = [[ |
|
| 187 | - 'name' => '_action', |
|
| 188 | - 'value' => 'submit-review', |
|
| 189 | - ], [ |
|
| 190 | - 'name' => '_counter', |
|
| 191 | - ], [ |
|
| 192 | - 'name' => '_nonce', |
|
| 193 | - 'value' => wp_create_nonce('submit-review'), |
|
| 194 | - ], [ |
|
| 195 | - 'name' => '_post_id', |
|
| 196 | - 'value' => get_the_ID(), |
|
| 197 | - ], [ |
|
| 198 | - 'name' => '_referer', |
|
| 199 | - 'value' => wp_unslash(filter_input(INPUT_SERVER, 'REQUEST_URI')), |
|
| 200 | - ], [ |
|
| 201 | - 'name' => 'assign_to', |
|
| 202 | - 'value' => $this->args['assign_to'], |
|
| 203 | - ], [ |
|
| 204 | - 'name' => 'category', |
|
| 205 | - 'value' => $this->args['category'], |
|
| 206 | - ], [ |
|
| 207 | - 'name' => 'excluded', |
|
| 208 | - 'value' => $this->args['hide'], |
|
| 209 | - ], [ |
|
| 210 | - 'name' => 'form_id', |
|
| 211 | - 'value' => $this->args['id'], |
|
| 212 | - ]]; |
|
| 213 | - return array_map(function ($field) { |
|
| 214 | - return new Field(wp_parse_args($field, ['type' => 'hidden'])); |
|
| 215 | - }, $fields); |
|
| 216 | - } |
|
| 181 | + /** |
|
| 182 | + * @return array |
|
| 183 | + */ |
|
| 184 | + protected function getHiddenFields() |
|
| 185 | + { |
|
| 186 | + $fields = [[ |
|
| 187 | + 'name' => '_action', |
|
| 188 | + 'value' => 'submit-review', |
|
| 189 | + ], [ |
|
| 190 | + 'name' => '_counter', |
|
| 191 | + ], [ |
|
| 192 | + 'name' => '_nonce', |
|
| 193 | + 'value' => wp_create_nonce('submit-review'), |
|
| 194 | + ], [ |
|
| 195 | + 'name' => '_post_id', |
|
| 196 | + 'value' => get_the_ID(), |
|
| 197 | + ], [ |
|
| 198 | + 'name' => '_referer', |
|
| 199 | + 'value' => wp_unslash(filter_input(INPUT_SERVER, 'REQUEST_URI')), |
|
| 200 | + ], [ |
|
| 201 | + 'name' => 'assign_to', |
|
| 202 | + 'value' => $this->args['assign_to'], |
|
| 203 | + ], [ |
|
| 204 | + 'name' => 'category', |
|
| 205 | + 'value' => $this->args['category'], |
|
| 206 | + ], [ |
|
| 207 | + 'name' => 'excluded', |
|
| 208 | + 'value' => $this->args['hide'], |
|
| 209 | + ], [ |
|
| 210 | + 'name' => 'form_id', |
|
| 211 | + 'value' => $this->args['id'], |
|
| 212 | + ]]; |
|
| 213 | + return array_map(function ($field) { |
|
| 214 | + return new Field(wp_parse_args($field, ['type' => 'hidden'])); |
|
| 215 | + }, $fields); |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * @return Field |
|
| 220 | - */ |
|
| 221 | - protected function getHoneypotField() |
|
| 222 | - { |
|
| 223 | - return new Field([ |
|
| 224 | - 'name' => 'gotcha', |
|
| 225 | - 'type' => 'honeypot', |
|
| 226 | - ]); |
|
| 227 | - } |
|
| 218 | + /** |
|
| 219 | + * @return Field |
|
| 220 | + */ |
|
| 221 | + protected function getHoneypotField() |
|
| 222 | + { |
|
| 223 | + return new Field([ |
|
| 224 | + 'name' => 'gotcha', |
|
| 225 | + 'type' => 'honeypot', |
|
| 226 | + ]); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - /** |
|
| 230 | - * @return void |
|
| 231 | - */ |
|
| 232 | - protected function normalizeFieldId(Field &$field) |
|
| 233 | - { |
|
| 234 | - if (empty($this->args['id']) || empty($field->field['id'])) { |
|
| 235 | - return; |
|
| 236 | - } |
|
| 237 | - $field->field['id'].= '-'.$this->args['id']; |
|
| 238 | - } |
|
| 229 | + /** |
|
| 230 | + * @return void |
|
| 231 | + */ |
|
| 232 | + protected function normalizeFieldId(Field &$field) |
|
| 233 | + { |
|
| 234 | + if (empty($this->args['id']) || empty($field->field['id'])) { |
|
| 235 | + return; |
|
| 236 | + } |
|
| 237 | + $field->field['id'].= '-'.$this->args['id']; |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * @return void |
|
| 242 | - */ |
|
| 243 | - protected function normalizeFieldClass(Field &$field) |
|
| 244 | - { |
|
| 245 | - if (!isset($field->field['class'])) { |
|
| 246 | - $field->field['class'] = ''; |
|
| 247 | - } |
|
| 248 | - $field->field['class'] = trim($field->field['class'].' glsr-field-control'); |
|
| 249 | - } |
|
| 240 | + /** |
|
| 241 | + * @return void |
|
| 242 | + */ |
|
| 243 | + protected function normalizeFieldClass(Field &$field) |
|
| 244 | + { |
|
| 245 | + if (!isset($field->field['class'])) { |
|
| 246 | + $field->field['class'] = ''; |
|
| 247 | + } |
|
| 248 | + $field->field['class'] = trim($field->field['class'].' glsr-field-control'); |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | - /** |
|
| 252 | - * @return void |
|
| 253 | - */ |
|
| 254 | - protected function normalizeFieldErrors(Field &$field) |
|
| 255 | - { |
|
| 256 | - if (!array_key_exists($field->field['path'], $this->errors)) { |
|
| 257 | - return; |
|
| 258 | - } |
|
| 259 | - $field->field['errors'] = $this->errors[$field->field['path']]; |
|
| 260 | - } |
|
| 251 | + /** |
|
| 252 | + * @return void |
|
| 253 | + */ |
|
| 254 | + protected function normalizeFieldErrors(Field &$field) |
|
| 255 | + { |
|
| 256 | + if (!array_key_exists($field->field['path'], $this->errors)) { |
|
| 257 | + return; |
|
| 258 | + } |
|
| 259 | + $field->field['errors'] = $this->errors[$field->field['path']]; |
|
| 260 | + } |
|
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * @return void |
|
| 264 | - */ |
|
| 265 | - protected function normalizeFieldRequired(Field &$field) |
|
| 266 | - { |
|
| 267 | - if (!in_array($field->field['path'], $this->required)) { |
|
| 268 | - return; |
|
| 269 | - } |
|
| 270 | - $field->field['required'] = true; |
|
| 271 | - } |
|
| 262 | + /** |
|
| 263 | + * @return void |
|
| 264 | + */ |
|
| 265 | + protected function normalizeFieldRequired(Field &$field) |
|
| 266 | + { |
|
| 267 | + if (!in_array($field->field['path'], $this->required)) { |
|
| 268 | + return; |
|
| 269 | + } |
|
| 270 | + $field->field['required'] = true; |
|
| 271 | + } |
|
| 272 | 272 | |
| 273 | - /** |
|
| 274 | - * @return array |
|
| 275 | - */ |
|
| 276 | - protected function normalizeFields($fields) |
|
| 277 | - { |
|
| 278 | - $normalizedFields = []; |
|
| 279 | - foreach ($fields as $field) { |
|
| 280 | - if (in_array($field->field['path'], $this->args['hide'])) { |
|
| 281 | - continue; |
|
| 282 | - } |
|
| 283 | - $field->field['is_public'] = true; |
|
| 284 | - $this->normalizeFieldClass($field); |
|
| 285 | - $this->normalizeFieldErrors($field); |
|
| 286 | - $this->normalizeFieldRequired($field); |
|
| 287 | - $this->normalizeFieldValue($field); |
|
| 288 | - $this->normalizeFieldId($field); |
|
| 289 | - $normalizedFields[] = $field; |
|
| 290 | - } |
|
| 291 | - return $normalizedFields; |
|
| 292 | - } |
|
| 273 | + /** |
|
| 274 | + * @return array |
|
| 275 | + */ |
|
| 276 | + protected function normalizeFields($fields) |
|
| 277 | + { |
|
| 278 | + $normalizedFields = []; |
|
| 279 | + foreach ($fields as $field) { |
|
| 280 | + if (in_array($field->field['path'], $this->args['hide'])) { |
|
| 281 | + continue; |
|
| 282 | + } |
|
| 283 | + $field->field['is_public'] = true; |
|
| 284 | + $this->normalizeFieldClass($field); |
|
| 285 | + $this->normalizeFieldErrors($field); |
|
| 286 | + $this->normalizeFieldRequired($field); |
|
| 287 | + $this->normalizeFieldValue($field); |
|
| 288 | + $this->normalizeFieldId($field); |
|
| 289 | + $normalizedFields[] = $field; |
|
| 290 | + } |
|
| 291 | + return $normalizedFields; |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * @return void |
|
| 296 | - */ |
|
| 297 | - protected function normalizeFieldValue(Field &$field) |
|
| 298 | - { |
|
| 299 | - if (!array_key_exists($field->field['path'], $this->values)) { |
|
| 300 | - return; |
|
| 301 | - } |
|
| 302 | - if (in_array($field->field['type'], ['radio', 'checkbox'])) { |
|
| 303 | - $field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
|
| 304 | - } else { |
|
| 305 | - $field->field['value'] = $this->values[$field->field['path']]; |
|
| 306 | - } |
|
| 307 | - } |
|
| 294 | + /** |
|
| 295 | + * @return void |
|
| 296 | + */ |
|
| 297 | + protected function normalizeFieldValue(Field &$field) |
|
| 298 | + { |
|
| 299 | + if (!array_key_exists($field->field['path'], $this->values)) { |
|
| 300 | + return; |
|
| 301 | + } |
|
| 302 | + if (in_array($field->field['type'], ['radio', 'checkbox'])) { |
|
| 303 | + $field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
|
| 304 | + } else { |
|
| 305 | + $field->field['value'] = $this->values[$field->field['path']]; |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | 308 | } |
@@ -64,7 +64,8 @@ |
||
| 64 | 64 | if ($flattenValue) { |
| 65 | 65 | $value = '['.implode(', ', $value).']'; |
| 66 | 66 | } |
| 67 | - } elseif (is_array($value)) { |
|
| 67 | + } |
|
| 68 | + elseif (is_array($value)) { |
|
| 68 | 69 | $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
| 69 | 70 | continue; |
| 70 | 71 | } |
@@ -4,215 +4,215 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Arr |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return bool |
|
| 9 | - */ |
|
| 10 | - public static function compareArrays(array $arr1, array $arr2) |
|
| 11 | - { |
|
| 12 | - sort($arr1); |
|
| 13 | - sort($arr2); |
|
| 14 | - return $arr1 == $arr2; |
|
| 15 | - } |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @param mixed $array |
|
| 19 | - * @return array |
|
| 20 | - */ |
|
| 21 | - public static function consolidateArray($array) |
|
| 22 | - { |
|
| 23 | - return is_array($array) || is_object($array) |
|
| 24 | - ? (array) $array |
|
| 25 | - : []; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @return array |
|
| 30 | - */ |
|
| 31 | - public static function convertDotNotationArray(array $array) |
|
| 32 | - { |
|
| 33 | - $results = []; |
|
| 34 | - foreach ($array as $path => $value) { |
|
| 35 | - $results = static::set($results, $path, $value); |
|
| 36 | - } |
|
| 37 | - return $results; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param string $string |
|
| 42 | - * @param mixed $callback |
|
| 43 | - * @return array |
|
| 44 | - */ |
|
| 45 | - public static function convertStringToArray($string, $callback = null) |
|
| 46 | - { |
|
| 47 | - $array = array_map('trim', explode(',', $string)); |
|
| 48 | - return $callback |
|
| 49 | - ? array_filter($array, $callback) |
|
| 50 | - : array_filter($array); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @param bool $flattenValue |
|
| 55 | - * @param string $prefix |
|
| 56 | - * @return array |
|
| 57 | - */ |
|
| 58 | - public static function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
| 59 | - { |
|
| 60 | - $result = []; |
|
| 61 | - foreach ($array as $key => $value) { |
|
| 62 | - $newKey = ltrim($prefix.'.'.$key, '.'); |
|
| 63 | - if (static::isIndexedFlatArray($value)) { |
|
| 64 | - if ($flattenValue) { |
|
| 65 | - $value = '['.implode(', ', $value).']'; |
|
| 66 | - } |
|
| 67 | - } elseif (is_array($value)) { |
|
| 68 | - $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
|
| 69 | - continue; |
|
| 70 | - } |
|
| 71 | - $result[$newKey] = $value; |
|
| 72 | - } |
|
| 73 | - return $result; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Get a value from an array of values using a dot-notation path as reference. |
|
| 78 | - * @param mixed $data |
|
| 79 | - * @param string $path |
|
| 80 | - * @param mixed $fallback |
|
| 81 | - * @return mixed |
|
| 82 | - */ |
|
| 83 | - public static function get($data, $path = '', $fallback = '') |
|
| 84 | - { |
|
| 85 | - $data = static::consolidateArray($data); |
|
| 86 | - $keys = explode('.', $path); |
|
| 87 | - foreach ($keys as $key) { |
|
| 88 | - if (!isset($data[$key])) { |
|
| 89 | - return $fallback; |
|
| 90 | - } |
|
| 91 | - $data = $data[$key]; |
|
| 92 | - } |
|
| 93 | - return $data; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param string $key |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - public static function insertAfter($key, array $array, array $insert) |
|
| 101 | - { |
|
| 102 | - return static::insertInArray($array, $insert, $key, 'after'); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @param string $key |
|
| 107 | - * @return array |
|
| 108 | - */ |
|
| 109 | - public static function insertBefore($key, array $array, array $insert) |
|
| 110 | - { |
|
| 111 | - return static::insertInArray($array, $insert, $key, 'before'); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param string $key |
|
| 116 | - * @param string $position |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public static function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
| 120 | - { |
|
| 121 | - $keyPosition = intval(array_search($key, array_keys($array))); |
|
| 122 | - if ('after' == $position) { |
|
| 123 | - ++$keyPosition; |
|
| 124 | - } |
|
| 125 | - if (false !== $keyPosition) { |
|
| 126 | - $result = array_slice($array, 0, $keyPosition); |
|
| 127 | - $result = array_merge($result, $insert); |
|
| 128 | - return array_merge($result, array_slice($array, $keyPosition)); |
|
| 129 | - } |
|
| 130 | - return array_merge($array, $insert); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @param mixed $array |
|
| 135 | - * @return bool |
|
| 136 | - */ |
|
| 137 | - public static function isIndexedFlatArray($array) |
|
| 138 | - { |
|
| 139 | - if (!is_array($array) || array_filter($array, 'is_array')) { |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - return wp_is_numeric_array($array); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * @param bool $prefixed |
|
| 147 | - * @return array |
|
| 148 | - */ |
|
| 149 | - public static function prefixArrayKeys(array $values, $prefixed = true) |
|
| 150 | - { |
|
| 151 | - $trim = '_'; |
|
| 152 | - $prefix = $prefixed |
|
| 153 | - ? $trim |
|
| 154 | - : ''; |
|
| 155 | - $prefixed = []; |
|
| 156 | - foreach ($values as $key => $value) { |
|
| 157 | - $key = trim($key); |
|
| 158 | - if (0 === strpos($key, $trim)) { |
|
| 159 | - $key = substr($key, strlen($trim)); |
|
| 160 | - } |
|
| 161 | - $prefixed[$prefix.$key] = $value; |
|
| 162 | - } |
|
| 163 | - return $prefixed; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public static function removeEmptyArrayValues(array $array) |
|
| 170 | - { |
|
| 171 | - $result = []; |
|
| 172 | - foreach ($array as $key => $value) { |
|
| 173 | - if (!$value) { |
|
| 174 | - continue; |
|
| 175 | - } |
|
| 176 | - $result[$key] = is_array($value) |
|
| 177 | - ? static::removeEmptyArrayValues($value) |
|
| 178 | - : $value; |
|
| 179 | - } |
|
| 180 | - return $result; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Set a value to an array of values using a dot-notation path as reference. |
|
| 186 | - * @param string $path |
|
| 187 | - * @param mixed $value |
|
| 188 | - * @return array |
|
| 189 | - */ |
|
| 190 | - public static function set(array $data, $path, $value) |
|
| 191 | - { |
|
| 192 | - $token = strtok($path, '.'); |
|
| 193 | - $ref = &$data; |
|
| 194 | - while (false !== $token) { |
|
| 195 | - $ref = static::consolidateArray($ref); |
|
| 196 | - $ref = &$ref[$token]; |
|
| 197 | - $token = strtok('.'); |
|
| 198 | - } |
|
| 199 | - $ref = $value; |
|
| 200 | - return $data; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @return array |
|
| 205 | - */ |
|
| 206 | - public static function unique(array $values) |
|
| 207 | - { |
|
| 208 | - return array_filter(array_unique($values)); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @return array |
|
| 213 | - */ |
|
| 214 | - public static function unprefixArrayKeys(array $values) |
|
| 215 | - { |
|
| 216 | - return static::prefixArrayKeys($values, false); |
|
| 217 | - } |
|
| 7 | + /** |
|
| 8 | + * @return bool |
|
| 9 | + */ |
|
| 10 | + public static function compareArrays(array $arr1, array $arr2) |
|
| 11 | + { |
|
| 12 | + sort($arr1); |
|
| 13 | + sort($arr2); |
|
| 14 | + return $arr1 == $arr2; |
|
| 15 | + } |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @param mixed $array |
|
| 19 | + * @return array |
|
| 20 | + */ |
|
| 21 | + public static function consolidateArray($array) |
|
| 22 | + { |
|
| 23 | + return is_array($array) || is_object($array) |
|
| 24 | + ? (array) $array |
|
| 25 | + : []; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @return array |
|
| 30 | + */ |
|
| 31 | + public static function convertDotNotationArray(array $array) |
|
| 32 | + { |
|
| 33 | + $results = []; |
|
| 34 | + foreach ($array as $path => $value) { |
|
| 35 | + $results = static::set($results, $path, $value); |
|
| 36 | + } |
|
| 37 | + return $results; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param string $string |
|
| 42 | + * @param mixed $callback |
|
| 43 | + * @return array |
|
| 44 | + */ |
|
| 45 | + public static function convertStringToArray($string, $callback = null) |
|
| 46 | + { |
|
| 47 | + $array = array_map('trim', explode(',', $string)); |
|
| 48 | + return $callback |
|
| 49 | + ? array_filter($array, $callback) |
|
| 50 | + : array_filter($array); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @param bool $flattenValue |
|
| 55 | + * @param string $prefix |
|
| 56 | + * @return array |
|
| 57 | + */ |
|
| 58 | + public static function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
| 59 | + { |
|
| 60 | + $result = []; |
|
| 61 | + foreach ($array as $key => $value) { |
|
| 62 | + $newKey = ltrim($prefix.'.'.$key, '.'); |
|
| 63 | + if (static::isIndexedFlatArray($value)) { |
|
| 64 | + if ($flattenValue) { |
|
| 65 | + $value = '['.implode(', ', $value).']'; |
|
| 66 | + } |
|
| 67 | + } elseif (is_array($value)) { |
|
| 68 | + $result = array_merge($result, static::flattenArray($value, $flattenValue, $newKey)); |
|
| 69 | + continue; |
|
| 70 | + } |
|
| 71 | + $result[$newKey] = $value; |
|
| 72 | + } |
|
| 73 | + return $result; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Get a value from an array of values using a dot-notation path as reference. |
|
| 78 | + * @param mixed $data |
|
| 79 | + * @param string $path |
|
| 80 | + * @param mixed $fallback |
|
| 81 | + * @return mixed |
|
| 82 | + */ |
|
| 83 | + public static function get($data, $path = '', $fallback = '') |
|
| 84 | + { |
|
| 85 | + $data = static::consolidateArray($data); |
|
| 86 | + $keys = explode('.', $path); |
|
| 87 | + foreach ($keys as $key) { |
|
| 88 | + if (!isset($data[$key])) { |
|
| 89 | + return $fallback; |
|
| 90 | + } |
|
| 91 | + $data = $data[$key]; |
|
| 92 | + } |
|
| 93 | + return $data; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param string $key |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + public static function insertAfter($key, array $array, array $insert) |
|
| 101 | + { |
|
| 102 | + return static::insertInArray($array, $insert, $key, 'after'); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @param string $key |
|
| 107 | + * @return array |
|
| 108 | + */ |
|
| 109 | + public static function insertBefore($key, array $array, array $insert) |
|
| 110 | + { |
|
| 111 | + return static::insertInArray($array, $insert, $key, 'before'); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param string $key |
|
| 116 | + * @param string $position |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public static function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
| 120 | + { |
|
| 121 | + $keyPosition = intval(array_search($key, array_keys($array))); |
|
| 122 | + if ('after' == $position) { |
|
| 123 | + ++$keyPosition; |
|
| 124 | + } |
|
| 125 | + if (false !== $keyPosition) { |
|
| 126 | + $result = array_slice($array, 0, $keyPosition); |
|
| 127 | + $result = array_merge($result, $insert); |
|
| 128 | + return array_merge($result, array_slice($array, $keyPosition)); |
|
| 129 | + } |
|
| 130 | + return array_merge($array, $insert); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @param mixed $array |
|
| 135 | + * @return bool |
|
| 136 | + */ |
|
| 137 | + public static function isIndexedFlatArray($array) |
|
| 138 | + { |
|
| 139 | + if (!is_array($array) || array_filter($array, 'is_array')) { |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + return wp_is_numeric_array($array); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * @param bool $prefixed |
|
| 147 | + * @return array |
|
| 148 | + */ |
|
| 149 | + public static function prefixArrayKeys(array $values, $prefixed = true) |
|
| 150 | + { |
|
| 151 | + $trim = '_'; |
|
| 152 | + $prefix = $prefixed |
|
| 153 | + ? $trim |
|
| 154 | + : ''; |
|
| 155 | + $prefixed = []; |
|
| 156 | + foreach ($values as $key => $value) { |
|
| 157 | + $key = trim($key); |
|
| 158 | + if (0 === strpos($key, $trim)) { |
|
| 159 | + $key = substr($key, strlen($trim)); |
|
| 160 | + } |
|
| 161 | + $prefixed[$prefix.$key] = $value; |
|
| 162 | + } |
|
| 163 | + return $prefixed; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public static function removeEmptyArrayValues(array $array) |
|
| 170 | + { |
|
| 171 | + $result = []; |
|
| 172 | + foreach ($array as $key => $value) { |
|
| 173 | + if (!$value) { |
|
| 174 | + continue; |
|
| 175 | + } |
|
| 176 | + $result[$key] = is_array($value) |
|
| 177 | + ? static::removeEmptyArrayValues($value) |
|
| 178 | + : $value; |
|
| 179 | + } |
|
| 180 | + return $result; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Set a value to an array of values using a dot-notation path as reference. |
|
| 186 | + * @param string $path |
|
| 187 | + * @param mixed $value |
|
| 188 | + * @return array |
|
| 189 | + */ |
|
| 190 | + public static function set(array $data, $path, $value) |
|
| 191 | + { |
|
| 192 | + $token = strtok($path, '.'); |
|
| 193 | + $ref = &$data; |
|
| 194 | + while (false !== $token) { |
|
| 195 | + $ref = static::consolidateArray($ref); |
|
| 196 | + $ref = &$ref[$token]; |
|
| 197 | + $token = strtok('.'); |
|
| 198 | + } |
|
| 199 | + $ref = $value; |
|
| 200 | + return $data; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @return array |
|
| 205 | + */ |
|
| 206 | + public static function unique(array $values) |
|
| 207 | + { |
|
| 208 | + return array_filter(array_unique($values)); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @return array |
|
| 213 | + */ |
|
| 214 | + public static function unprefixArrayKeys(array $values) |
|
| 215 | + { |
|
| 216 | + return static::prefixArrayKeys($values, false); |
|
| 217 | + } |
|
| 218 | 218 | } |
@@ -6,285 +6,285 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Attributes |
| 8 | 8 | { |
| 9 | - const ATTRIBUTES_A = [ |
|
| 10 | - 'download', 'href', 'hreflang', 'ping', 'referrerpolicy', 'rel', 'target', 'type', |
|
| 11 | - ]; |
|
| 9 | + const ATTRIBUTES_A = [ |
|
| 10 | + 'download', 'href', 'hreflang', 'ping', 'referrerpolicy', 'rel', 'target', 'type', |
|
| 11 | + ]; |
|
| 12 | 12 | |
| 13 | - const ATTRIBUTES_BUTTON = [ |
|
| 14 | - 'autofocus', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
| 15 | - 'formnovalidate', 'formtarget', 'name', 'type', 'value', |
|
| 16 | - ]; |
|
| 13 | + const ATTRIBUTES_BUTTON = [ |
|
| 14 | + 'autofocus', 'disabled', 'form', 'formaction', 'formenctype', 'formmethod', |
|
| 15 | + 'formnovalidate', 'formtarget', 'name', 'type', 'value', |
|
| 16 | + ]; |
|
| 17 | 17 | |
| 18 | - const ATTRIBUTES_FORM = [ |
|
| 19 | - 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', 'method', |
|
| 20 | - 'name', 'novalidate', 'target', |
|
| 21 | - ]; |
|
| 18 | + const ATTRIBUTES_FORM = [ |
|
| 19 | + 'accept', 'accept-charset', 'action', 'autocapitalize', 'autocomplete', 'enctype', 'method', |
|
| 20 | + 'name', 'novalidate', 'target', |
|
| 21 | + ]; |
|
| 22 | 22 | |
| 23 | - const ATTRIBUTES_IMG = [ |
|
| 24 | - 'alt', 'crossorigin', 'decoding', 'height', 'ismap', 'referrerpolicy', 'sizes', 'src', |
|
| 25 | - 'srcset', 'width', 'usemap', |
|
| 26 | - ]; |
|
| 23 | + const ATTRIBUTES_IMG = [ |
|
| 24 | + 'alt', 'crossorigin', 'decoding', 'height', 'ismap', 'referrerpolicy', 'sizes', 'src', |
|
| 25 | + 'srcset', 'width', 'usemap', |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - const ATTRIBUTES_INPUT = [ |
|
| 29 | - 'accept', 'autocomplete', 'autocorrect', 'autofocus', 'capture', 'checked', 'disabled', |
|
| 30 | - 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'height', |
|
| 31 | - 'incremental', 'inputmode', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple', |
|
| 32 | - 'name', 'pattern', 'placeholder', 'readonly', 'results', 'required', 'selectionDirection', |
|
| 33 | - 'selectionEnd', 'selectionStart', 'size', 'spellcheck', 'src', 'step', 'tabindex', 'type', |
|
| 34 | - 'value', 'webkitdirectory', 'width', |
|
| 35 | - ]; |
|
| 28 | + const ATTRIBUTES_INPUT = [ |
|
| 29 | + 'accept', 'autocomplete', 'autocorrect', 'autofocus', 'capture', 'checked', 'disabled', |
|
| 30 | + 'form', 'formaction', 'formenctype', 'formmethod', 'formnovalidate', 'formtarget', 'height', |
|
| 31 | + 'incremental', 'inputmode', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple', |
|
| 32 | + 'name', 'pattern', 'placeholder', 'readonly', 'results', 'required', 'selectionDirection', |
|
| 33 | + 'selectionEnd', 'selectionStart', 'size', 'spellcheck', 'src', 'step', 'tabindex', 'type', |
|
| 34 | + 'value', 'webkitdirectory', 'width', |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - const ATTRIBUTES_LABEL = [ |
|
| 38 | - 'for', |
|
| 39 | - ]; |
|
| 37 | + const ATTRIBUTES_LABEL = [ |
|
| 38 | + 'for', |
|
| 39 | + ]; |
|
| 40 | 40 | |
| 41 | - const ATTRIBUTES_OPTION = [ |
|
| 42 | - 'disabled', 'label', 'selected', 'value', |
|
| 43 | - ]; |
|
| 41 | + const ATTRIBUTES_OPTION = [ |
|
| 42 | + 'disabled', 'label', 'selected', 'value', |
|
| 43 | + ]; |
|
| 44 | 44 | |
| 45 | - const ATTRIBUTES_SELECT = [ |
|
| 46 | - 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
| 47 | - ]; |
|
| 45 | + const ATTRIBUTES_SELECT = [ |
|
| 46 | + 'autofocus', 'disabled', 'form', 'multiple', 'name', 'required', 'size', |
|
| 47 | + ]; |
|
| 48 | 48 | |
| 49 | - const ATTRIBUTES_TEXTAREA = [ |
|
| 50 | - 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
| 51 | - 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', |
|
| 52 | - ]; |
|
| 49 | + const ATTRIBUTES_TEXTAREA = [ |
|
| 50 | + 'autocapitalize', 'autocomplete', 'autofocus', 'cols', 'disabled', 'form', 'maxlength', |
|
| 51 | + 'minlength', 'name', 'placeholder', 'readonly', 'required', 'rows', 'spellcheck', 'wrap', |
|
| 52 | + ]; |
|
| 53 | 53 | |
| 54 | - const BOOLEAN_ATTRIBUTES = [ |
|
| 55 | - 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
| 56 | - 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
| 57 | - 'webkitdirectory', |
|
| 58 | - ]; |
|
| 54 | + const BOOLEAN_ATTRIBUTES = [ |
|
| 55 | + 'autofocus', 'capture', 'checked', 'disabled', 'draggable', 'formnovalidate', 'hidden', |
|
| 56 | + 'multiple', 'novalidate', 'readonly', 'required', 'selected', 'spellcheck', |
|
| 57 | + 'webkitdirectory', |
|
| 58 | + ]; |
|
| 59 | 59 | |
| 60 | - const GLOBAL_ATTRIBUTES = [ |
|
| 61 | - 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
| 62 | - 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
| 63 | - ]; |
|
| 60 | + const GLOBAL_ATTRIBUTES = [ |
|
| 61 | + 'accesskey', 'class', 'contenteditable', 'contextmenu', 'dir', 'draggable', 'dropzone', |
|
| 62 | + 'hidden', 'id', 'lang', 'spellcheck', 'style', 'tabindex', 'title', |
|
| 63 | + ]; |
|
| 64 | 64 | |
| 65 | - const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
| 66 | - 'aria-', 'data-', 'item', 'on', |
|
| 67 | - ]; |
|
| 65 | + const GLOBAL_WILDCARD_ATTRIBUTES = [ |
|
| 66 | + 'aria-', 'data-', 'item', 'on', |
|
| 67 | + ]; |
|
| 68 | 68 | |
| 69 | - const INPUT_TYPES = [ |
|
| 70 | - 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', |
|
| 71 | - 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', |
|
| 72 | - 'time', 'url', 'week', |
|
| 73 | - ]; |
|
| 69 | + const INPUT_TYPES = [ |
|
| 70 | + 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', |
|
| 71 | + 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', |
|
| 72 | + 'time', 'url', 'week', |
|
| 73 | + ]; |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @var array |
|
| 77 | - */ |
|
| 78 | - protected $attributes = []; |
|
| 75 | + /** |
|
| 76 | + * @var array |
|
| 77 | + */ |
|
| 78 | + protected $attributes = []; |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @param string $method |
|
| 82 | - * @param array $args |
|
| 83 | - * @return static |
|
| 84 | - */ |
|
| 85 | - public function __call($method, $args) |
|
| 86 | - { |
|
| 87 | - $args += [[], false]; |
|
| 88 | - $constant = 'static::ATTRIBUTES_'.strtoupper($method); |
|
| 89 | - $allowedAttributeKeys = defined($constant) |
|
| 90 | - ? constant($constant) |
|
| 91 | - : []; |
|
| 92 | - $this->normalize((array) $args[0], $allowedAttributeKeys); |
|
| 93 | - $this->normalizeInputType($method); |
|
| 94 | - return $this; |
|
| 95 | - } |
|
| 80 | + /** |
|
| 81 | + * @param string $method |
|
| 82 | + * @param array $args |
|
| 83 | + * @return static |
|
| 84 | + */ |
|
| 85 | + public function __call($method, $args) |
|
| 86 | + { |
|
| 87 | + $args += [[], false]; |
|
| 88 | + $constant = 'static::ATTRIBUTES_'.strtoupper($method); |
|
| 89 | + $allowedAttributeKeys = defined($constant) |
|
| 90 | + ? constant($constant) |
|
| 91 | + : []; |
|
| 92 | + $this->normalize((array) $args[0], $allowedAttributeKeys); |
|
| 93 | + $this->normalizeInputType($method); |
|
| 94 | + return $this; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - public function toArray() |
|
| 101 | - { |
|
| 102 | - return $this->attributes; |
|
| 103 | - } |
|
| 97 | + /** |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + public function toArray() |
|
| 101 | + { |
|
| 102 | + return $this->attributes; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function toString() |
|
| 109 | - { |
|
| 110 | - $attributes = []; |
|
| 111 | - foreach ($this->attributes as $attribute => $value) { |
|
| 112 | - $quote = $this->getQuoteChar($attribute); |
|
| 113 | - $attributes[] = in_array($attribute, static::BOOLEAN_ATTRIBUTES) |
|
| 114 | - ? $attribute |
|
| 115 | - : $attribute.'='.$quote.implode(',', (array) $value).$quote; |
|
| 116 | - } |
|
| 117 | - return implode(' ', $attributes); |
|
| 118 | - } |
|
| 105 | + /** |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function toString() |
|
| 109 | + { |
|
| 110 | + $attributes = []; |
|
| 111 | + foreach ($this->attributes as $attribute => $value) { |
|
| 112 | + $quote = $this->getQuoteChar($attribute); |
|
| 113 | + $attributes[] = in_array($attribute, static::BOOLEAN_ATTRIBUTES) |
|
| 114 | + ? $attribute |
|
| 115 | + : $attribute.'='.$quote.implode(',', (array) $value).$quote; |
|
| 116 | + } |
|
| 117 | + return implode(' ', $attributes); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * @return array |
|
| 122 | - */ |
|
| 123 | - protected function filterAttributes(array $allowedAttributeKeys) |
|
| 124 | - { |
|
| 125 | - return array_intersect_key($this->attributes, array_flip($allowedAttributeKeys)); |
|
| 126 | - } |
|
| 120 | + /** |
|
| 121 | + * @return array |
|
| 122 | + */ |
|
| 123 | + protected function filterAttributes(array $allowedAttributeKeys) |
|
| 124 | + { |
|
| 125 | + return array_intersect_key($this->attributes, array_flip($allowedAttributeKeys)); |
|
| 126 | + } |
|
| 127 | 127 | |
| 128 | - /** |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - protected function filterGlobalAttributes() |
|
| 132 | - { |
|
| 133 | - $globalAttributes = $this->filterAttributes(static::GLOBAL_ATTRIBUTES); |
|
| 134 | - $wildcards = []; |
|
| 135 | - foreach (static::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
| 136 | - $newWildcards = array_filter($this->attributes, function ($key) use ($wildcard) { |
|
| 137 | - return Str::startsWith($wildcard, $key); |
|
| 138 | - }, ARRAY_FILTER_USE_KEY); |
|
| 139 | - $wildcards = array_merge($wildcards, $newWildcards); |
|
| 140 | - } |
|
| 141 | - return array_merge($globalAttributes, $wildcards); |
|
| 142 | - } |
|
| 128 | + /** |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + protected function filterGlobalAttributes() |
|
| 132 | + { |
|
| 133 | + $globalAttributes = $this->filterAttributes(static::GLOBAL_ATTRIBUTES); |
|
| 134 | + $wildcards = []; |
|
| 135 | + foreach (static::GLOBAL_WILDCARD_ATTRIBUTES as $wildcard) { |
|
| 136 | + $newWildcards = array_filter($this->attributes, function ($key) use ($wildcard) { |
|
| 137 | + return Str::startsWith($wildcard, $key); |
|
| 138 | + }, ARRAY_FILTER_USE_KEY); |
|
| 139 | + $wildcards = array_merge($wildcards, $newWildcards); |
|
| 140 | + } |
|
| 141 | + return array_merge($globalAttributes, $wildcards); |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * @return array |
|
| 146 | - */ |
|
| 147 | - protected function getPermanentAttributes() |
|
| 148 | - { |
|
| 149 | - $permanentAttributes = []; |
|
| 150 | - if (array_key_exists('value', $this->attributes)) { |
|
| 151 | - $permanentAttributes['value'] = $this->attributes['value']; |
|
| 152 | - } |
|
| 153 | - return $permanentAttributes; |
|
| 154 | - } |
|
| 144 | + /** |
|
| 145 | + * @return array |
|
| 146 | + */ |
|
| 147 | + protected function getPermanentAttributes() |
|
| 148 | + { |
|
| 149 | + $permanentAttributes = []; |
|
| 150 | + if (array_key_exists('value', $this->attributes)) { |
|
| 151 | + $permanentAttributes['value'] = $this->attributes['value']; |
|
| 152 | + } |
|
| 153 | + return $permanentAttributes; |
|
| 154 | + } |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * @param string $attribute |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - protected function getQuoteChar($attribute) |
|
| 161 | - { |
|
| 162 | - return Str::startsWith('data-', $attribute) |
|
| 163 | - ? '\'' |
|
| 164 | - : '"'; |
|
| 165 | - } |
|
| 156 | + /** |
|
| 157 | + * @param string $attribute |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + protected function getQuoteChar($attribute) |
|
| 161 | + { |
|
| 162 | + return Str::startsWith('data-', $attribute) |
|
| 163 | + ? '\'' |
|
| 164 | + : '"'; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - /** |
|
| 168 | - * @param string $key |
|
| 169 | - * @param mixed $value |
|
| 170 | - * @return bool |
|
| 171 | - */ |
|
| 172 | - protected function isAttributeKeyNumeric($key, $value) |
|
| 173 | - { |
|
| 174 | - return is_string($value) |
|
| 175 | - && is_numeric($key) |
|
| 176 | - && !array_key_exists($value, $this->attributes); |
|
| 177 | - } |
|
| 167 | + /** |
|
| 168 | + * @param string $key |
|
| 169 | + * @param mixed $value |
|
| 170 | + * @return bool |
|
| 171 | + */ |
|
| 172 | + protected function isAttributeKeyNumeric($key, $value) |
|
| 173 | + { |
|
| 174 | + return is_string($value) |
|
| 175 | + && is_numeric($key) |
|
| 176 | + && !array_key_exists($value, $this->attributes); |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * @return void |
|
| 181 | - */ |
|
| 182 | - protected function normalize(array $args, array $allowedAttributeKeys) |
|
| 183 | - { |
|
| 184 | - $this->attributes = array_change_key_case($args, CASE_LOWER); |
|
| 185 | - $this->normalizeBooleanAttributes(); |
|
| 186 | - $this->normalizeDataAttributes(); |
|
| 187 | - $this->normalizeStringAttributes(); |
|
| 188 | - $this->removeEmptyAttributes(); |
|
| 189 | - $this->removeIndexedAttributes(); |
|
| 190 | - $this->attributes = array_merge( |
|
| 191 | - $this->filterGlobalAttributes(), |
|
| 192 | - $this->filterAttributes($allowedAttributeKeys) |
|
| 193 | - ); |
|
| 194 | - } |
|
| 179 | + /** |
|
| 180 | + * @return void |
|
| 181 | + */ |
|
| 182 | + protected function normalize(array $args, array $allowedAttributeKeys) |
|
| 183 | + { |
|
| 184 | + $this->attributes = array_change_key_case($args, CASE_LOWER); |
|
| 185 | + $this->normalizeBooleanAttributes(); |
|
| 186 | + $this->normalizeDataAttributes(); |
|
| 187 | + $this->normalizeStringAttributes(); |
|
| 188 | + $this->removeEmptyAttributes(); |
|
| 189 | + $this->removeIndexedAttributes(); |
|
| 190 | + $this->attributes = array_merge( |
|
| 191 | + $this->filterGlobalAttributes(), |
|
| 192 | + $this->filterAttributes($allowedAttributeKeys) |
|
| 193 | + ); |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * @return void |
|
| 198 | - */ |
|
| 199 | - protected function normalizeBooleanAttributes() |
|
| 200 | - { |
|
| 201 | - foreach ($this->attributes as $key => $value) { |
|
| 202 | - if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 203 | - $key = $value; |
|
| 204 | - $value = true; |
|
| 205 | - } |
|
| 206 | - if (!in_array($key, static::BOOLEAN_ATTRIBUTES)) { |
|
| 207 | - continue; |
|
| 208 | - } |
|
| 209 | - $this->attributes[$key] = wp_validate_boolean($value); |
|
| 210 | - } |
|
| 211 | - } |
|
| 196 | + /** |
|
| 197 | + * @return void |
|
| 198 | + */ |
|
| 199 | + protected function normalizeBooleanAttributes() |
|
| 200 | + { |
|
| 201 | + foreach ($this->attributes as $key => $value) { |
|
| 202 | + if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 203 | + $key = $value; |
|
| 204 | + $value = true; |
|
| 205 | + } |
|
| 206 | + if (!in_array($key, static::BOOLEAN_ATTRIBUTES)) { |
|
| 207 | + continue; |
|
| 208 | + } |
|
| 209 | + $this->attributes[$key] = wp_validate_boolean($value); |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * @return void |
|
| 215 | - */ |
|
| 216 | - protected function normalizeDataAttributes() |
|
| 217 | - { |
|
| 218 | - foreach ($this->attributes as $key => $value) { |
|
| 219 | - if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 220 | - $key = $value; |
|
| 221 | - $value = ''; |
|
| 222 | - } |
|
| 223 | - if (!Str::startsWith('data-', $key)) { |
|
| 224 | - continue; |
|
| 225 | - } |
|
| 226 | - if (is_array($value)) { |
|
| 227 | - $value = json_encode($value, JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
| 228 | - } |
|
| 229 | - $this->attributes[$key] = $value; |
|
| 230 | - } |
|
| 231 | - } |
|
| 213 | + /** |
|
| 214 | + * @return void |
|
| 215 | + */ |
|
| 216 | + protected function normalizeDataAttributes() |
|
| 217 | + { |
|
| 218 | + foreach ($this->attributes as $key => $value) { |
|
| 219 | + if ($this->isAttributeKeyNumeric($key, $value)) { |
|
| 220 | + $key = $value; |
|
| 221 | + $value = ''; |
|
| 222 | + } |
|
| 223 | + if (!Str::startsWith('data-', $key)) { |
|
| 224 | + continue; |
|
| 225 | + } |
|
| 226 | + if (is_array($value)) { |
|
| 227 | + $value = json_encode($value, JSON_HEX_APOS | JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
|
| 228 | + } |
|
| 229 | + $this->attributes[$key] = $value; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * @return void |
|
| 235 | - */ |
|
| 236 | - protected function normalizeStringAttributes() |
|
| 237 | - { |
|
| 238 | - foreach ($this->attributes as $key => $value) { |
|
| 239 | - if (!is_string($value)) { |
|
| 240 | - continue; |
|
| 241 | - } |
|
| 242 | - $this->attributes[$key] = trim($value); |
|
| 243 | - } |
|
| 244 | - } |
|
| 233 | + /** |
|
| 234 | + * @return void |
|
| 235 | + */ |
|
| 236 | + protected function normalizeStringAttributes() |
|
| 237 | + { |
|
| 238 | + foreach ($this->attributes as $key => $value) { |
|
| 239 | + if (!is_string($value)) { |
|
| 240 | + continue; |
|
| 241 | + } |
|
| 242 | + $this->attributes[$key] = trim($value); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * @param string $method |
|
| 248 | - * @return void |
|
| 249 | - */ |
|
| 250 | - protected function normalizeInputType($method) |
|
| 251 | - { |
|
| 252 | - if ('input' != $method) { |
|
| 253 | - return; |
|
| 254 | - } |
|
| 255 | - $attributes = wp_parse_args($this->attributes, ['type' => '']); |
|
| 256 | - if (!in_array($attributes['type'], static::INPUT_TYPES)) { |
|
| 257 | - $this->attributes['type'] = 'text'; |
|
| 258 | - } |
|
| 259 | - } |
|
| 246 | + /** |
|
| 247 | + * @param string $method |
|
| 248 | + * @return void |
|
| 249 | + */ |
|
| 250 | + protected function normalizeInputType($method) |
|
| 251 | + { |
|
| 252 | + if ('input' != $method) { |
|
| 253 | + return; |
|
| 254 | + } |
|
| 255 | + $attributes = wp_parse_args($this->attributes, ['type' => '']); |
|
| 256 | + if (!in_array($attributes['type'], static::INPUT_TYPES)) { |
|
| 257 | + $this->attributes['type'] = 'text'; |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - /** |
|
| 262 | - * @return void |
|
| 263 | - */ |
|
| 264 | - protected function removeEmptyAttributes() |
|
| 265 | - { |
|
| 266 | - $attributes = $this->attributes; |
|
| 267 | - $permanentAttributes = $this->getPermanentAttributes(); |
|
| 268 | - foreach ($this->attributes as $key => $value) { |
|
| 269 | - if (in_array($key, static::BOOLEAN_ATTRIBUTES) && !$value) { |
|
| 270 | - unset($attributes[$key]); |
|
| 271 | - } |
|
| 272 | - if (Str::startsWith('data-', $key)) { |
|
| 273 | - $permanentAttributes[$key] = $value; |
|
| 274 | - unset($attributes[$key]); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - $this->attributes = array_merge(array_filter($attributes), $permanentAttributes); |
|
| 278 | - } |
|
| 261 | + /** |
|
| 262 | + * @return void |
|
| 263 | + */ |
|
| 264 | + protected function removeEmptyAttributes() |
|
| 265 | + { |
|
| 266 | + $attributes = $this->attributes; |
|
| 267 | + $permanentAttributes = $this->getPermanentAttributes(); |
|
| 268 | + foreach ($this->attributes as $key => $value) { |
|
| 269 | + if (in_array($key, static::BOOLEAN_ATTRIBUTES) && !$value) { |
|
| 270 | + unset($attributes[$key]); |
|
| 271 | + } |
|
| 272 | + if (Str::startsWith('data-', $key)) { |
|
| 273 | + $permanentAttributes[$key] = $value; |
|
| 274 | + unset($attributes[$key]); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + $this->attributes = array_merge(array_filter($attributes), $permanentAttributes); |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * @return void |
|
| 282 | - */ |
|
| 283 | - protected function removeIndexedAttributes() |
|
| 284 | - { |
|
| 285 | - $this->attributes = array_diff_key( |
|
| 286 | - $this->attributes, |
|
| 287 | - array_filter($this->attributes, 'is_numeric', ARRAY_FILTER_USE_KEY) |
|
| 288 | - ); |
|
| 289 | - } |
|
| 280 | + /** |
|
| 281 | + * @return void |
|
| 282 | + */ |
|
| 283 | + protected function removeIndexedAttributes() |
|
| 284 | + { |
|
| 285 | + $this->attributes = array_diff_key( |
|
| 286 | + $this->attributes, |
|
| 287 | + array_filter($this->attributes, 'is_numeric', ARRAY_FILTER_USE_KEY) |
|
| 288 | + ); |
|
| 289 | + } |
|
| 290 | 290 | } |