@@ -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 | } |
@@ -6,124 +6,124 @@ |
||
| 6 | 6 | |
| 7 | 7 | class SiteReviewsBlock 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 | - 'count' => [ |
|
| 28 | - 'default' => 5, |
|
| 29 | - 'type' => 'number', |
|
| 30 | - ], |
|
| 31 | - 'hide' => [ |
|
| 32 | - 'default' => '', |
|
| 33 | - 'type' => 'string', |
|
| 34 | - ], |
|
| 35 | - 'id' => [ |
|
| 36 | - 'default' => '', |
|
| 37 | - 'type' => 'string', |
|
| 38 | - ], |
|
| 39 | - 'pagination' => [ |
|
| 40 | - 'default' => '', |
|
| 41 | - 'type' => 'string', |
|
| 42 | - ], |
|
| 43 | - 'post_id' => [ |
|
| 44 | - 'default' => '', |
|
| 45 | - 'type' => 'string', |
|
| 46 | - ], |
|
| 47 | - 'rating' => [ |
|
| 48 | - 'default' => 1, |
|
| 49 | - 'type' => 'number', |
|
| 50 | - ], |
|
| 51 | - 'schema' => [ |
|
| 52 | - 'default' => false, |
|
| 53 | - 'type' => 'boolean', |
|
| 54 | - ], |
|
| 55 | - 'type' => [ |
|
| 56 | - 'default' => 'local', |
|
| 57 | - 'type' => 'string', |
|
| 58 | - ], |
|
| 59 | - ]; |
|
| 60 | - } |
|
| 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 | + 'count' => [ |
|
| 28 | + 'default' => 5, |
|
| 29 | + 'type' => 'number', |
|
| 30 | + ], |
|
| 31 | + 'hide' => [ |
|
| 32 | + 'default' => '', |
|
| 33 | + 'type' => 'string', |
|
| 34 | + ], |
|
| 35 | + 'id' => [ |
|
| 36 | + 'default' => '', |
|
| 37 | + 'type' => 'string', |
|
| 38 | + ], |
|
| 39 | + 'pagination' => [ |
|
| 40 | + 'default' => '', |
|
| 41 | + 'type' => 'string', |
|
| 42 | + ], |
|
| 43 | + 'post_id' => [ |
|
| 44 | + 'default' => '', |
|
| 45 | + 'type' => 'string', |
|
| 46 | + ], |
|
| 47 | + 'rating' => [ |
|
| 48 | + 'default' => 1, |
|
| 49 | + 'type' => 'number', |
|
| 50 | + ], |
|
| 51 | + 'schema' => [ |
|
| 52 | + 'default' => false, |
|
| 53 | + 'type' => 'boolean', |
|
| 54 | + ], |
|
| 55 | + 'type' => [ |
|
| 56 | + 'default' => 'local', |
|
| 57 | + 'type' => 'string', |
|
| 58 | + ], |
|
| 59 | + ]; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return string |
|
| 64 | - */ |
|
| 65 | - public function render(array $attributes) |
|
| 66 | - { |
|
| 67 | - $attributes['class'] = $attributes['className']; |
|
| 68 | - $shortcode = glsr(Shortcode::class); |
|
| 69 | - if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
| 70 | - $attributes = $this->normalize($attributes); |
|
| 71 | - $this->filterReviewLinks(); |
|
| 72 | - $this->filterShortcodeClass(); |
|
| 73 | - $this->filterShowMoreLinks('content'); |
|
| 74 | - $this->filterShowMoreLinks('response'); |
|
| 75 | - if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
| 76 | - $this->filterInterpolation(); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - return $shortcode->buildShortcode($attributes); |
|
| 80 | - } |
|
| 62 | + /** |
|
| 63 | + * @return string |
|
| 64 | + */ |
|
| 65 | + public function render(array $attributes) |
|
| 66 | + { |
|
| 67 | + $attributes['class'] = $attributes['className']; |
|
| 68 | + $shortcode = glsr(Shortcode::class); |
|
| 69 | + if ('edit' == filter_input(INPUT_GET, 'context')) { |
|
| 70 | + $attributes = $this->normalize($attributes); |
|
| 71 | + $this->filterReviewLinks(); |
|
| 72 | + $this->filterShortcodeClass(); |
|
| 73 | + $this->filterShowMoreLinks('content'); |
|
| 74 | + $this->filterShowMoreLinks('response'); |
|
| 75 | + if (!$this->hasVisibleFields($shortcode, $attributes)) { |
|
| 76 | + $this->filterInterpolation(); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + return $shortcode->buildShortcode($attributes); |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @return void |
|
| 84 | - */ |
|
| 85 | - protected function filterInterpolation() |
|
| 86 | - { |
|
| 87 | - add_filter('site-reviews/interpolate/reviews', function ($context) { |
|
| 88 | - $context['class'] = 'glsr-default glsr-block-disabled'; |
|
| 89 | - $context['reviews'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
| 90 | - return $context; |
|
| 91 | - }); |
|
| 92 | - } |
|
| 82 | + /** |
|
| 83 | + * @return void |
|
| 84 | + */ |
|
| 85 | + protected function filterInterpolation() |
|
| 86 | + { |
|
| 87 | + add_filter('site-reviews/interpolate/reviews', function ($context) { |
|
| 88 | + $context['class'] = 'glsr-default glsr-block-disabled'; |
|
| 89 | + $context['reviews'] = __('You have hidden all of the fields for this block.', 'site-reviews'); |
|
| 90 | + return $context; |
|
| 91 | + }); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - /** |
|
| 95 | - * @return void |
|
| 96 | - */ |
|
| 97 | - protected function filterReviewLinks() |
|
| 98 | - { |
|
| 99 | - add_filter('site-reviews/rendered/template/reviews', function ($template) { |
|
| 100 | - return str_replace('<a', '<a tabindex="-1"', $template); |
|
| 101 | - }); |
|
| 102 | - } |
|
| 94 | + /** |
|
| 95 | + * @return void |
|
| 96 | + */ |
|
| 97 | + protected function filterReviewLinks() |
|
| 98 | + { |
|
| 99 | + add_filter('site-reviews/rendered/template/reviews', function ($template) { |
|
| 100 | + return str_replace('<a', '<a tabindex="-1"', $template); |
|
| 101 | + }); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * @return void |
|
| 106 | - */ |
|
| 107 | - protected function filterShortcodeClass() |
|
| 108 | - { |
|
| 109 | - add_filter('site-reviews/style', function () { |
|
| 110 | - return 'default'; |
|
| 111 | - }); |
|
| 112 | - } |
|
| 104 | + /** |
|
| 105 | + * @return void |
|
| 106 | + */ |
|
| 107 | + protected function filterShortcodeClass() |
|
| 108 | + { |
|
| 109 | + add_filter('site-reviews/style', function () { |
|
| 110 | + return 'default'; |
|
| 111 | + }); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * @param string $field |
|
| 116 | - * @return void |
|
| 117 | - */ |
|
| 118 | - protected function filterShowMoreLinks($field) |
|
| 119 | - { |
|
| 120 | - add_filter('site-reviews/review/wrap/'.$field, function ($value) { |
|
| 121 | - $value = preg_replace( |
|
| 122 | - '/(.*)(<span class="glsr-hidden)(.*)(<\/span>)(.*)/s', |
|
| 123 | - '$1... <a href="#" class="glsr-read-more" tabindex="-1">'.__('Show more', 'site-reviews').'</a>$5', |
|
| 124 | - $value |
|
| 125 | - ); |
|
| 126 | - return $value; |
|
| 127 | - }); |
|
| 128 | - } |
|
| 114 | + /** |
|
| 115 | + * @param string $field |
|
| 116 | + * @return void |
|
| 117 | + */ |
|
| 118 | + protected function filterShowMoreLinks($field) |
|
| 119 | + { |
|
| 120 | + add_filter('site-reviews/review/wrap/'.$field, function ($value) { |
|
| 121 | + $value = preg_replace( |
|
| 122 | + '/(.*)(<span class="glsr-hidden)(.*)(<\/span>)(.*)/s', |
|
| 123 | + '$1... <a href="#" class="glsr-read-more" tabindex="-1">'.__('Show more', 'site-reviews').'</a>$5', |
|
| 124 | + $value |
|
| 125 | + ); |
|
| 126 | + return $value; |
|
| 127 | + }); |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -7,342 +7,342 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Helper |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @param string $name |
|
| 12 | - * @param string $path |
|
| 13 | - * @return string |
|
| 14 | - */ |
|
| 15 | - public function buildClassName($name, $path = '') |
|
| 16 | - { |
|
| 17 | - $className = $this->camelCase($name); |
|
| 18 | - $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
|
| 19 | - return !empty($path) |
|
| 20 | - ? __NAMESPACE__.'\\'.$path.'\\'.$className |
|
| 21 | - : $className; |
|
| 22 | - } |
|
| 10 | + /** |
|
| 11 | + * @param string $name |
|
| 12 | + * @param string $path |
|
| 13 | + * @return string |
|
| 14 | + */ |
|
| 15 | + public function buildClassName($name, $path = '') |
|
| 16 | + { |
|
| 17 | + $className = $this->camelCase($name); |
|
| 18 | + $path = ltrim(str_replace(__NAMESPACE__, '', $path), '\\'); |
|
| 19 | + return !empty($path) |
|
| 20 | + ? __NAMESPACE__.'\\'.$path.'\\'.$className |
|
| 21 | + : $className; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param string $name |
|
| 26 | - * @param string $prefix |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - public function buildMethodName($name, $prefix = '') |
|
| 30 | - { |
|
| 31 | - return lcfirst($prefix.$this->buildClassName($name)); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * @param string $name |
|
| 26 | + * @param string $prefix |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + public function buildMethodName($name, $prefix = '') |
|
| 30 | + { |
|
| 31 | + return lcfirst($prefix.$this->buildClassName($name)); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param string $name |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - public function buildPropertyName($name) |
|
| 39 | - { |
|
| 40 | - return lcfirst($this->buildClassName($name)); |
|
| 41 | - } |
|
| 34 | + /** |
|
| 35 | + * @param string $name |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + public function buildPropertyName($name) |
|
| 39 | + { |
|
| 40 | + return lcfirst($this->buildClassName($name)); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param string $string |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - public function camelCase($string) |
|
| 48 | - { |
|
| 49 | - $string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
|
| 50 | - return str_replace(' ', '', $string); |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * @param string $string |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + public function camelCase($string) |
|
| 48 | + { |
|
| 49 | + $string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
|
| 50 | + return str_replace(' ', '', $string); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return bool |
|
| 55 | - */ |
|
| 56 | - public function compareArrays(array $arr1, array $arr2) |
|
| 57 | - { |
|
| 58 | - sort($arr1); |
|
| 59 | - sort($arr2); |
|
| 60 | - return $arr1 == $arr2; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * @return bool |
|
| 55 | + */ |
|
| 56 | + public function compareArrays(array $arr1, array $arr2) |
|
| 57 | + { |
|
| 58 | + sort($arr1); |
|
| 59 | + sort($arr2); |
|
| 60 | + return $arr1 == $arr2; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @param mixed $array |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public function consolidateArray($array) |
|
| 68 | - { |
|
| 69 | - return is_array($array) || is_object($array) |
|
| 70 | - ? (array) $array |
|
| 71 | - : []; |
|
| 72 | - } |
|
| 63 | + /** |
|
| 64 | + * @param mixed $array |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public function consolidateArray($array) |
|
| 68 | + { |
|
| 69 | + return is_array($array) || is_object($array) |
|
| 70 | + ? (array) $array |
|
| 71 | + : []; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - /** |
|
| 75 | - * @return array |
|
| 76 | - */ |
|
| 77 | - public function convertDotNotationArray(array $array) |
|
| 78 | - { |
|
| 79 | - $results = []; |
|
| 80 | - foreach ($array as $path => $value) { |
|
| 81 | - $results = $this->dataSet($results, $path, $value); |
|
| 82 | - } |
|
| 83 | - return $results; |
|
| 84 | - } |
|
| 74 | + /** |
|
| 75 | + * @return array |
|
| 76 | + */ |
|
| 77 | + public function convertDotNotationArray(array $array) |
|
| 78 | + { |
|
| 79 | + $results = []; |
|
| 80 | + foreach ($array as $path => $value) { |
|
| 81 | + $results = $this->dataSet($results, $path, $value); |
|
| 82 | + } |
|
| 83 | + return $results; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * @param string $name |
|
| 88 | - * @return string |
|
| 89 | - */ |
|
| 90 | - public function convertPathToId($path, $prefix = '') |
|
| 91 | - { |
|
| 92 | - return str_replace(['[', ']'], ['-', ''], $this->convertPathToName($path, $prefix)); |
|
| 93 | - } |
|
| 86 | + /** |
|
| 87 | + * @param string $name |
|
| 88 | + * @return string |
|
| 89 | + */ |
|
| 90 | + public function convertPathToId($path, $prefix = '') |
|
| 91 | + { |
|
| 92 | + return str_replace(['[', ']'], ['-', ''], $this->convertPathToName($path, $prefix)); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @param string $path |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - public function convertPathToName($path, $prefix = '') |
|
| 100 | - { |
|
| 101 | - $levels = explode('.', $path); |
|
| 102 | - return array_reduce($levels, function ($result, $value) { |
|
| 103 | - return $result .= '['.$value.']'; |
|
| 104 | - }, $prefix); |
|
| 105 | - } |
|
| 95 | + /** |
|
| 96 | + * @param string $path |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + public function convertPathToName($path, $prefix = '') |
|
| 100 | + { |
|
| 101 | + $levels = explode('.', $path); |
|
| 102 | + return array_reduce($levels, function ($result, $value) { |
|
| 103 | + return $result .= '['.$value.']'; |
|
| 104 | + }, $prefix); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - /** |
|
| 108 | - * @param string $string |
|
| 109 | - * @param mixed $callback |
|
| 110 | - * @return array |
|
| 111 | - */ |
|
| 112 | - public function convertStringToArray($string, $callback = null) |
|
| 113 | - { |
|
| 114 | - $array = array_map('trim', explode(',', $string)); |
|
| 115 | - return $callback |
|
| 116 | - ? array_filter($array, $callback) |
|
| 117 | - : array_filter($array); |
|
| 118 | - } |
|
| 107 | + /** |
|
| 108 | + * @param string $string |
|
| 109 | + * @param mixed $callback |
|
| 110 | + * @return array |
|
| 111 | + */ |
|
| 112 | + public function convertStringToArray($string, $callback = null) |
|
| 113 | + { |
|
| 114 | + $array = array_map('trim', explode(',', $string)); |
|
| 115 | + return $callback |
|
| 116 | + ? array_filter($array, $callback) |
|
| 117 | + : array_filter($array); |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * @param string $string |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public function dashCase($string) |
|
| 125 | - { |
|
| 126 | - return str_replace('_', '-', $this->snakeCase($string)); |
|
| 127 | - } |
|
| 120 | + /** |
|
| 121 | + * @param string $string |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public function dashCase($string) |
|
| 125 | + { |
|
| 126 | + return str_replace('_', '-', $this->snakeCase($string)); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * Get a value from an array of values using a dot-notation path as reference. |
|
| 131 | - * @param array $data |
|
| 132 | - * @param string $path |
|
| 133 | - * @param mixed $fallback |
|
| 134 | - * @return mixed |
|
| 135 | - */ |
|
| 136 | - public function dataGet($data, $path = '', $fallback = '') |
|
| 137 | - { |
|
| 138 | - $data = $this->consolidateArray($data); |
|
| 139 | - $keys = explode('.', $path); |
|
| 140 | - foreach ($keys as $key) { |
|
| 141 | - if (!isset($data[$key])) { |
|
| 142 | - return $fallback; |
|
| 143 | - } |
|
| 144 | - $data = $data[$key]; |
|
| 145 | - } |
|
| 146 | - return $data; |
|
| 147 | - } |
|
| 129 | + /** |
|
| 130 | + * Get a value from an array of values using a dot-notation path as reference. |
|
| 131 | + * @param array $data |
|
| 132 | + * @param string $path |
|
| 133 | + * @param mixed $fallback |
|
| 134 | + * @return mixed |
|
| 135 | + */ |
|
| 136 | + public function dataGet($data, $path = '', $fallback = '') |
|
| 137 | + { |
|
| 138 | + $data = $this->consolidateArray($data); |
|
| 139 | + $keys = explode('.', $path); |
|
| 140 | + foreach ($keys as $key) { |
|
| 141 | + if (!isset($data[$key])) { |
|
| 142 | + return $fallback; |
|
| 143 | + } |
|
| 144 | + $data = $data[$key]; |
|
| 145 | + } |
|
| 146 | + return $data; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Set a value to an array of values using a dot-notation path as reference. |
|
| 151 | - * @param string $path |
|
| 152 | - * @param mixed $value |
|
| 153 | - * @return array |
|
| 154 | - */ |
|
| 155 | - public function dataSet(array $data, $path = '', $value) |
|
| 156 | - { |
|
| 157 | - $token = strtok($path, '.'); |
|
| 158 | - $ref = &$data; |
|
| 159 | - while (false !== $token) { |
|
| 160 | - $ref = $this->consolidateArray($ref); |
|
| 161 | - $ref = &$ref[$token]; |
|
| 162 | - $token = strtok('.'); |
|
| 163 | - } |
|
| 164 | - $ref = $value; |
|
| 165 | - return $data; |
|
| 166 | - } |
|
| 149 | + /** |
|
| 150 | + * Set a value to an array of values using a dot-notation path as reference. |
|
| 151 | + * @param string $path |
|
| 152 | + * @param mixed $value |
|
| 153 | + * @return array |
|
| 154 | + */ |
|
| 155 | + public function dataSet(array $data, $path = '', $value) |
|
| 156 | + { |
|
| 157 | + $token = strtok($path, '.'); |
|
| 158 | + $ref = &$data; |
|
| 159 | + while (false !== $token) { |
|
| 160 | + $ref = $this->consolidateArray($ref); |
|
| 161 | + $ref = &$ref[$token]; |
|
| 162 | + $token = strtok('.'); |
|
| 163 | + } |
|
| 164 | + $ref = $value; |
|
| 165 | + return $data; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * @param string $needle |
|
| 170 | - * @param string $haystack |
|
| 171 | - * @return bool |
|
| 172 | - */ |
|
| 173 | - public function endsWith($needle, $haystack) |
|
| 174 | - { |
|
| 175 | - $length = strlen($needle); |
|
| 176 | - return 0 != $length |
|
| 177 | - ? substr($haystack, -$length) === $needle |
|
| 178 | - : true; |
|
| 179 | - } |
|
| 168 | + /** |
|
| 169 | + * @param string $needle |
|
| 170 | + * @param string $haystack |
|
| 171 | + * @return bool |
|
| 172 | + */ |
|
| 173 | + public function endsWith($needle, $haystack) |
|
| 174 | + { |
|
| 175 | + $length = strlen($needle); |
|
| 176 | + return 0 != $length |
|
| 177 | + ? substr($haystack, -$length) === $needle |
|
| 178 | + : true; |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - /** |
|
| 182 | - * @param string $key |
|
| 183 | - * @return mixed |
|
| 184 | - */ |
|
| 185 | - public function filterInput($key, array $request = []) |
|
| 186 | - { |
|
| 187 | - if (isset($request[$key])) { |
|
| 188 | - return $request[$key]; |
|
| 189 | - } |
|
| 190 | - $variable = filter_input(INPUT_POST, $key); |
|
| 191 | - if (is_null($variable) && isset($_POST[$key])) { |
|
| 192 | - $variable = $_POST[$key]; |
|
| 193 | - } |
|
| 194 | - return $variable; |
|
| 195 | - } |
|
| 181 | + /** |
|
| 182 | + * @param string $key |
|
| 183 | + * @return mixed |
|
| 184 | + */ |
|
| 185 | + public function filterInput($key, array $request = []) |
|
| 186 | + { |
|
| 187 | + if (isset($request[$key])) { |
|
| 188 | + return $request[$key]; |
|
| 189 | + } |
|
| 190 | + $variable = filter_input(INPUT_POST, $key); |
|
| 191 | + if (is_null($variable) && isset($_POST[$key])) { |
|
| 192 | + $variable = $_POST[$key]; |
|
| 193 | + } |
|
| 194 | + return $variable; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * @param string $key |
|
| 199 | - * @return array |
|
| 200 | - */ |
|
| 201 | - public function filterInputArray($key) |
|
| 202 | - { |
|
| 203 | - $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 204 | - if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
|
| 205 | - $variable = $_POST[$key]; |
|
| 206 | - } |
|
| 207 | - return (array) $variable; |
|
| 208 | - } |
|
| 197 | + /** |
|
| 198 | + * @param string $key |
|
| 199 | + * @return array |
|
| 200 | + */ |
|
| 201 | + public function filterInputArray($key) |
|
| 202 | + { |
|
| 203 | + $variable = filter_input(INPUT_POST, $key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
| 204 | + if (empty($variable) && !empty($_POST[$key]) && is_array($_POST[$key])) { |
|
| 205 | + $variable = $_POST[$key]; |
|
| 206 | + } |
|
| 207 | + return (array) $variable; |
|
| 208 | + } |
|
| 209 | 209 | |
| 210 | - /** |
|
| 211 | - * @param bool $flattenValue |
|
| 212 | - * @param string $prefix |
|
| 213 | - * @return array |
|
| 214 | - */ |
|
| 215 | - public function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
| 216 | - { |
|
| 217 | - $result = []; |
|
| 218 | - foreach ($array as $key => $value) { |
|
| 219 | - $newKey = ltrim($prefix.'.'.$key, '.'); |
|
| 220 | - if ($this->isIndexedFlatArray($value)) { |
|
| 221 | - if ($flattenValue) { |
|
| 222 | - $value = '['.implode(', ', $value).']'; |
|
| 223 | - } |
|
| 224 | - } elseif (is_array($value)) { |
|
| 225 | - $result = array_merge($result, $this->flattenArray($value, $flattenValue, $newKey)); |
|
| 226 | - continue; |
|
| 227 | - } |
|
| 228 | - $result[$newKey] = $value; |
|
| 229 | - } |
|
| 230 | - return $result; |
|
| 231 | - } |
|
| 210 | + /** |
|
| 211 | + * @param bool $flattenValue |
|
| 212 | + * @param string $prefix |
|
| 213 | + * @return array |
|
| 214 | + */ |
|
| 215 | + public function flattenArray(array $array, $flattenValue = false, $prefix = '') |
|
| 216 | + { |
|
| 217 | + $result = []; |
|
| 218 | + foreach ($array as $key => $value) { |
|
| 219 | + $newKey = ltrim($prefix.'.'.$key, '.'); |
|
| 220 | + if ($this->isIndexedFlatArray($value)) { |
|
| 221 | + if ($flattenValue) { |
|
| 222 | + $value = '['.implode(', ', $value).']'; |
|
| 223 | + } |
|
| 224 | + } elseif (is_array($value)) { |
|
| 225 | + $result = array_merge($result, $this->flattenArray($value, $flattenValue, $newKey)); |
|
| 226 | + continue; |
|
| 227 | + } |
|
| 228 | + $result[$newKey] = $value; |
|
| 229 | + } |
|
| 230 | + return $result; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * @return string |
|
| 235 | - */ |
|
| 236 | - public function getIpAddress() |
|
| 237 | - { |
|
| 238 | - $cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
|
| 239 | - $ipv6 = defined('AF_INET6') |
|
| 240 | - ? $cloudflareIps['v6'] |
|
| 241 | - : []; |
|
| 242 | - $whitelist = apply_filters('site-reviews/whip/whitelist', [ |
|
| 243 | - Whip::CLOUDFLARE_HEADERS => [ |
|
| 244 | - Whip::IPV4 => $cloudflareIps['v4'], |
|
| 245 | - Whip::IPV6 => $ipv6, |
|
| 246 | - ], |
|
| 247 | - ]); |
|
| 248 | - $whip = new Whip(Whip::ALL_METHODS, $whitelist); |
|
| 249 | - do_action_ref_array('site-reviews/whip', [&$whip]); |
|
| 250 | - return (string) $whip->getValidIpAddress(); |
|
| 251 | - } |
|
| 233 | + /** |
|
| 234 | + * @return string |
|
| 235 | + */ |
|
| 236 | + public function getIpAddress() |
|
| 237 | + { |
|
| 238 | + $cloudflareIps = glsr(Cache::class)->getCloudflareIps(); |
|
| 239 | + $ipv6 = defined('AF_INET6') |
|
| 240 | + ? $cloudflareIps['v6'] |
|
| 241 | + : []; |
|
| 242 | + $whitelist = apply_filters('site-reviews/whip/whitelist', [ |
|
| 243 | + Whip::CLOUDFLARE_HEADERS => [ |
|
| 244 | + Whip::IPV4 => $cloudflareIps['v4'], |
|
| 245 | + Whip::IPV6 => $ipv6, |
|
| 246 | + ], |
|
| 247 | + ]); |
|
| 248 | + $whip = new Whip(Whip::ALL_METHODS, $whitelist); |
|
| 249 | + do_action_ref_array('site-reviews/whip', [&$whip]); |
|
| 250 | + return (string) $whip->getValidIpAddress(); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * @param string $key |
|
| 255 | - * @param string $position |
|
| 256 | - * @return array |
|
| 257 | - */ |
|
| 258 | - public function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
| 259 | - { |
|
| 260 | - $keyPosition = intval(array_search($key, array_keys($array))); |
|
| 261 | - if ('after' == $position) { |
|
| 262 | - ++$keyPosition; |
|
| 263 | - } |
|
| 264 | - if (false !== $keyPosition) { |
|
| 265 | - $result = array_slice($array, 0, $keyPosition); |
|
| 266 | - $result = array_merge($result, $insert); |
|
| 267 | - return array_merge($result, array_slice($array, $keyPosition)); |
|
| 268 | - } |
|
| 269 | - return array_merge($array, $insert); |
|
| 270 | - } |
|
| 253 | + /** |
|
| 254 | + * @param string $key |
|
| 255 | + * @param string $position |
|
| 256 | + * @return array |
|
| 257 | + */ |
|
| 258 | + public function insertInArray(array $array, array $insert, $key, $position = 'before') |
|
| 259 | + { |
|
| 260 | + $keyPosition = intval(array_search($key, array_keys($array))); |
|
| 261 | + if ('after' == $position) { |
|
| 262 | + ++$keyPosition; |
|
| 263 | + } |
|
| 264 | + if (false !== $keyPosition) { |
|
| 265 | + $result = array_slice($array, 0, $keyPosition); |
|
| 266 | + $result = array_merge($result, $insert); |
|
| 267 | + return array_merge($result, array_slice($array, $keyPosition)); |
|
| 268 | + } |
|
| 269 | + return array_merge($array, $insert); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - /** |
|
| 273 | - * @param mixed $array |
|
| 274 | - * @return bool |
|
| 275 | - */ |
|
| 276 | - public function isIndexedFlatArray($array) |
|
| 277 | - { |
|
| 278 | - if (!is_array($array) || array_filter($array, 'is_array')) { |
|
| 279 | - return false; |
|
| 280 | - } |
|
| 281 | - return wp_is_numeric_array($array); |
|
| 282 | - } |
|
| 272 | + /** |
|
| 273 | + * @param mixed $array |
|
| 274 | + * @return bool |
|
| 275 | + */ |
|
| 276 | + public function isIndexedFlatArray($array) |
|
| 277 | + { |
|
| 278 | + if (!is_array($array) || array_filter($array, 'is_array')) { |
|
| 279 | + return false; |
|
| 280 | + } |
|
| 281 | + return wp_is_numeric_array($array); |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | - /** |
|
| 285 | - * @param string $string |
|
| 286 | - * @param string $prefix |
|
| 287 | - * @return string |
|
| 288 | - */ |
|
| 289 | - public function prefixString($string, $prefix = '') |
|
| 290 | - { |
|
| 291 | - return $prefix.str_replace($prefix, '', trim($string)); |
|
| 292 | - } |
|
| 284 | + /** |
|
| 285 | + * @param string $string |
|
| 286 | + * @param string $prefix |
|
| 287 | + * @return string |
|
| 288 | + */ |
|
| 289 | + public function prefixString($string, $prefix = '') |
|
| 290 | + { |
|
| 291 | + return $prefix.str_replace($prefix, '', trim($string)); |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * @return array |
|
| 296 | - */ |
|
| 297 | - public function removeEmptyArrayValues(array $array) |
|
| 298 | - { |
|
| 299 | - $result = []; |
|
| 300 | - foreach ($array as $key => $value) { |
|
| 301 | - if (!$value) { |
|
| 302 | - continue; |
|
| 303 | - } |
|
| 304 | - $result[$key] = is_array($value) |
|
| 305 | - ? $this->removeEmptyArrayValues($value) |
|
| 306 | - : $value; |
|
| 307 | - } |
|
| 308 | - return $result; |
|
| 309 | - } |
|
| 294 | + /** |
|
| 295 | + * @return array |
|
| 296 | + */ |
|
| 297 | + public function removeEmptyArrayValues(array $array) |
|
| 298 | + { |
|
| 299 | + $result = []; |
|
| 300 | + foreach ($array as $key => $value) { |
|
| 301 | + if (!$value) { |
|
| 302 | + continue; |
|
| 303 | + } |
|
| 304 | + $result[$key] = is_array($value) |
|
| 305 | + ? $this->removeEmptyArrayValues($value) |
|
| 306 | + : $value; |
|
| 307 | + } |
|
| 308 | + return $result; |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 312 | - * @param string $prefix |
|
| 313 | - * @param string $text |
|
| 314 | - * @return string |
|
| 315 | - */ |
|
| 316 | - public function removePrefix($prefix, $text) |
|
| 317 | - { |
|
| 318 | - return 0 === strpos($text, $prefix) |
|
| 319 | - ? substr($text, strlen($prefix)) |
|
| 320 | - : $text; |
|
| 321 | - } |
|
| 311 | + /** |
|
| 312 | + * @param string $prefix |
|
| 313 | + * @param string $text |
|
| 314 | + * @return string |
|
| 315 | + */ |
|
| 316 | + public function removePrefix($prefix, $text) |
|
| 317 | + { |
|
| 318 | + return 0 === strpos($text, $prefix) |
|
| 319 | + ? substr($text, strlen($prefix)) |
|
| 320 | + : $text; |
|
| 321 | + } |
|
| 322 | 322 | |
| 323 | - /** |
|
| 324 | - * @param string $string |
|
| 325 | - * @return string |
|
| 326 | - */ |
|
| 327 | - public function snakeCase($string) |
|
| 328 | - { |
|
| 329 | - if (!ctype_lower($string)) { |
|
| 330 | - $string = preg_replace('/\s+/u', '', $string); |
|
| 331 | - $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
| 332 | - $string = function_exists('mb_strtolower') |
|
| 333 | - ? mb_strtolower($string, 'UTF-8') |
|
| 334 | - : strtolower($string); |
|
| 335 | - } |
|
| 336 | - return str_replace('-', '_', $string); |
|
| 337 | - } |
|
| 323 | + /** |
|
| 324 | + * @param string $string |
|
| 325 | + * @return string |
|
| 326 | + */ |
|
| 327 | + public function snakeCase($string) |
|
| 328 | + { |
|
| 329 | + if (!ctype_lower($string)) { |
|
| 330 | + $string = preg_replace('/\s+/u', '', $string); |
|
| 331 | + $string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
| 332 | + $string = function_exists('mb_strtolower') |
|
| 333 | + ? mb_strtolower($string, 'UTF-8') |
|
| 334 | + : strtolower($string); |
|
| 335 | + } |
|
| 336 | + return str_replace('-', '_', $string); |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * @param string $needle |
|
| 341 | - * @param string $haystack |
|
| 342 | - * @return bool |
|
| 343 | - */ |
|
| 344 | - public function startsWith($needle, $haystack) |
|
| 345 | - { |
|
| 346 | - return substr($haystack, 0, strlen($needle)) === $needle; |
|
| 347 | - } |
|
| 339 | + /** |
|
| 340 | + * @param string $needle |
|
| 341 | + * @param string $haystack |
|
| 342 | + * @return bool |
|
| 343 | + */ |
|
| 344 | + public function startsWith($needle, $haystack) |
|
| 345 | + { |
|
| 346 | + return substr($haystack, 0, strlen($needle)) === $needle; |
|
| 347 | + } |
|
| 348 | 348 | } |
@@ -9,194 +9,194 @@ |
||
| 9 | 9 | |
| 10 | 10 | abstract class Container |
| 11 | 11 | { |
| 12 | - const PROTECTED_PROPERTIES = [ |
|
| 13 | - 'instance', |
|
| 14 | - 'services', |
|
| 15 | - 'storage', |
|
| 16 | - ]; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var static |
|
| 20 | - */ |
|
| 21 | - protected static $instance; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The container's bound services. |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - protected $services = []; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * The container's storage items. |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $storage = []; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @return static |
|
| 37 | - */ |
|
| 38 | - public static function load() |
|
| 39 | - { |
|
| 40 | - if (empty(static::$instance)) { |
|
| 41 | - static::$instance = new static(); |
|
| 42 | - } |
|
| 43 | - return static::$instance; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param string $property |
|
| 48 | - * @return mixed |
|
| 49 | - */ |
|
| 50 | - public function __get($property) |
|
| 51 | - { |
|
| 52 | - if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
| 53 | - return $this->$property; |
|
| 54 | - } |
|
| 55 | - $constant = 'static::'.strtoupper($this->make(Helper::class)->snakeCase($property)); |
|
| 56 | - if (defined($constant)) { |
|
| 57 | - return constant($constant); |
|
| 58 | - } |
|
| 59 | - return glsr_get($this->storage, $property, null); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param string $property |
|
| 64 | - * @param string $value |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function __set($property, $value) |
|
| 68 | - { |
|
| 69 | - if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
| 70 | - $this->storage[$property] = $value; |
|
| 71 | - } elseif (!isset($this->$property)) { |
|
| 72 | - $this->$property = $value; |
|
| 73 | - } else { |
|
| 74 | - throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Bind a service to the container. |
|
| 80 | - * @param string $alias |
|
| 81 | - * @param mixed $concrete |
|
| 82 | - * @return mixed |
|
| 83 | - */ |
|
| 84 | - public function bind($alias, $concrete) |
|
| 85 | - { |
|
| 86 | - $this->services[$alias] = $concrete; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Request a service from the container. |
|
| 91 | - * @param mixed $abstract |
|
| 92 | - * @return mixed |
|
| 93 | - */ |
|
| 94 | - public function make($abstract) |
|
| 95 | - { |
|
| 96 | - if (!isset($this->services[$abstract])) { |
|
| 97 | - $abstract = $this->addNamespace($abstract); |
|
| 98 | - } |
|
| 99 | - if (isset($this->services[$abstract])) { |
|
| 100 | - $abstract = $this->services[$abstract]; |
|
| 101 | - } |
|
| 102 | - if (is_callable($abstract)) { |
|
| 103 | - return call_user_func_array($abstract, [$this]); |
|
| 104 | - } |
|
| 105 | - if (is_object($abstract)) { |
|
| 106 | - return $abstract; |
|
| 107 | - } |
|
| 108 | - return $this->resolve($abstract); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Bind a singleton instance to the container. |
|
| 113 | - * @param string $alias |
|
| 114 | - * @param callable|string|null $binding |
|
| 115 | - * @return void |
|
| 116 | - */ |
|
| 117 | - public function singleton($alias, $binding) |
|
| 118 | - { |
|
| 119 | - $this->bind($alias, $this->make($binding)); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Prefix the current namespace to the abstract if absent. |
|
| 124 | - * @param string $abstract |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - protected function addNamespace($abstract) |
|
| 128 | - { |
|
| 129 | - if (false === strpos($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
| 130 | - $abstract = __NAMESPACE__.'\\'.$abstract; |
|
| 131 | - } |
|
| 132 | - return $abstract; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Resolve a service from the container. |
|
| 137 | - * @param mixed $concrete |
|
| 138 | - * @return mixed |
|
| 139 | - * @throws Exception |
|
| 140 | - */ |
|
| 141 | - protected function resolve($concrete) |
|
| 142 | - { |
|
| 143 | - if ($concrete instanceof Closure) { |
|
| 144 | - return $concrete($this); |
|
| 145 | - } |
|
| 146 | - $reflector = new ReflectionClass($concrete); |
|
| 147 | - if (!$reflector->isInstantiable()) { |
|
| 148 | - throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
| 149 | - } |
|
| 150 | - $constructor = $reflector->getConstructor(); |
|
| 151 | - if (empty($constructor)) { |
|
| 152 | - return new $concrete(); |
|
| 153 | - } |
|
| 154 | - return $reflector->newInstanceArgs( |
|
| 155 | - $this->resolveDependencies($constructor->getParameters()) |
|
| 156 | - ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Resolve a class based dependency from the container. |
|
| 161 | - * @return mixed |
|
| 162 | - * @throws Exception |
|
| 163 | - */ |
|
| 164 | - protected function resolveClass(ReflectionParameter $parameter) |
|
| 165 | - { |
|
| 166 | - try { |
|
| 167 | - return $this->make($parameter->getClass()->name); |
|
| 168 | - } catch (Exception $error) { |
|
| 169 | - if ($parameter->isOptional()) { |
|
| 170 | - return $parameter->getDefaultValue(); |
|
| 171 | - } |
|
| 172 | - throw $error; |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Resolve all of the dependencies from the ReflectionParameters. |
|
| 178 | - * @return array |
|
| 179 | - */ |
|
| 180 | - protected function resolveDependencies(array $dependencies) |
|
| 181 | - { |
|
| 182 | - $results = []; |
|
| 183 | - foreach ($dependencies as $dependency) { |
|
| 184 | - $results[] = !is_null($class = $dependency->getClass()) |
|
| 185 | - ? $this->resolveClass($dependency) |
|
| 186 | - : $this->resolveDependency($dependency); |
|
| 187 | - } |
|
| 188 | - return $results; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Resolve a single ReflectionParameter dependency. |
|
| 193 | - * @return array|null |
|
| 194 | - */ |
|
| 195 | - protected function resolveDependency(ReflectionParameter $parameter) |
|
| 196 | - { |
|
| 197 | - if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
| 198 | - return $parameter->getDefaultValue(); |
|
| 199 | - } |
|
| 200 | - return null; |
|
| 201 | - } |
|
| 12 | + const PROTECTED_PROPERTIES = [ |
|
| 13 | + 'instance', |
|
| 14 | + 'services', |
|
| 15 | + 'storage', |
|
| 16 | + ]; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var static |
|
| 20 | + */ |
|
| 21 | + protected static $instance; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The container's bound services. |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + protected $services = []; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * The container's storage items. |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $storage = []; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @return static |
|
| 37 | + */ |
|
| 38 | + public static function load() |
|
| 39 | + { |
|
| 40 | + if (empty(static::$instance)) { |
|
| 41 | + static::$instance = new static(); |
|
| 42 | + } |
|
| 43 | + return static::$instance; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param string $property |
|
| 48 | + * @return mixed |
|
| 49 | + */ |
|
| 50 | + public function __get($property) |
|
| 51 | + { |
|
| 52 | + if (property_exists($this, $property) && !in_array($property, static::PROTECTED_PROPERTIES)) { |
|
| 53 | + return $this->$property; |
|
| 54 | + } |
|
| 55 | + $constant = 'static::'.strtoupper($this->make(Helper::class)->snakeCase($property)); |
|
| 56 | + if (defined($constant)) { |
|
| 57 | + return constant($constant); |
|
| 58 | + } |
|
| 59 | + return glsr_get($this->storage, $property, null); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param string $property |
|
| 64 | + * @param string $value |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function __set($property, $value) |
|
| 68 | + { |
|
| 69 | + if (!property_exists($this, $property) || in_array($property, static::PROTECTED_PROPERTIES)) { |
|
| 70 | + $this->storage[$property] = $value; |
|
| 71 | + } elseif (!isset($this->$property)) { |
|
| 72 | + $this->$property = $value; |
|
| 73 | + } else { |
|
| 74 | + throw new Exception(sprintf('The "%s" property cannot be changed once set.', $property)); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Bind a service to the container. |
|
| 80 | + * @param string $alias |
|
| 81 | + * @param mixed $concrete |
|
| 82 | + * @return mixed |
|
| 83 | + */ |
|
| 84 | + public function bind($alias, $concrete) |
|
| 85 | + { |
|
| 86 | + $this->services[$alias] = $concrete; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Request a service from the container. |
|
| 91 | + * @param mixed $abstract |
|
| 92 | + * @return mixed |
|
| 93 | + */ |
|
| 94 | + public function make($abstract) |
|
| 95 | + { |
|
| 96 | + if (!isset($this->services[$abstract])) { |
|
| 97 | + $abstract = $this->addNamespace($abstract); |
|
| 98 | + } |
|
| 99 | + if (isset($this->services[$abstract])) { |
|
| 100 | + $abstract = $this->services[$abstract]; |
|
| 101 | + } |
|
| 102 | + if (is_callable($abstract)) { |
|
| 103 | + return call_user_func_array($abstract, [$this]); |
|
| 104 | + } |
|
| 105 | + if (is_object($abstract)) { |
|
| 106 | + return $abstract; |
|
| 107 | + } |
|
| 108 | + return $this->resolve($abstract); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Bind a singleton instance to the container. |
|
| 113 | + * @param string $alias |
|
| 114 | + * @param callable|string|null $binding |
|
| 115 | + * @return void |
|
| 116 | + */ |
|
| 117 | + public function singleton($alias, $binding) |
|
| 118 | + { |
|
| 119 | + $this->bind($alias, $this->make($binding)); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Prefix the current namespace to the abstract if absent. |
|
| 124 | + * @param string $abstract |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + protected function addNamespace($abstract) |
|
| 128 | + { |
|
| 129 | + if (false === strpos($abstract, __NAMESPACE__) && !class_exists($abstract)) { |
|
| 130 | + $abstract = __NAMESPACE__.'\\'.$abstract; |
|
| 131 | + } |
|
| 132 | + return $abstract; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Resolve a service from the container. |
|
| 137 | + * @param mixed $concrete |
|
| 138 | + * @return mixed |
|
| 139 | + * @throws Exception |
|
| 140 | + */ |
|
| 141 | + protected function resolve($concrete) |
|
| 142 | + { |
|
| 143 | + if ($concrete instanceof Closure) { |
|
| 144 | + return $concrete($this); |
|
| 145 | + } |
|
| 146 | + $reflector = new ReflectionClass($concrete); |
|
| 147 | + if (!$reflector->isInstantiable()) { |
|
| 148 | + throw new Exception('Target ['.$concrete.'] is not instantiable.'); |
|
| 149 | + } |
|
| 150 | + $constructor = $reflector->getConstructor(); |
|
| 151 | + if (empty($constructor)) { |
|
| 152 | + return new $concrete(); |
|
| 153 | + } |
|
| 154 | + return $reflector->newInstanceArgs( |
|
| 155 | + $this->resolveDependencies($constructor->getParameters()) |
|
| 156 | + ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Resolve a class based dependency from the container. |
|
| 161 | + * @return mixed |
|
| 162 | + * @throws Exception |
|
| 163 | + */ |
|
| 164 | + protected function resolveClass(ReflectionParameter $parameter) |
|
| 165 | + { |
|
| 166 | + try { |
|
| 167 | + return $this->make($parameter->getClass()->name); |
|
| 168 | + } catch (Exception $error) { |
|
| 169 | + if ($parameter->isOptional()) { |
|
| 170 | + return $parameter->getDefaultValue(); |
|
| 171 | + } |
|
| 172 | + throw $error; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Resolve all of the dependencies from the ReflectionParameters. |
|
| 178 | + * @return array |
|
| 179 | + */ |
|
| 180 | + protected function resolveDependencies(array $dependencies) |
|
| 181 | + { |
|
| 182 | + $results = []; |
|
| 183 | + foreach ($dependencies as $dependency) { |
|
| 184 | + $results[] = !is_null($class = $dependency->getClass()) |
|
| 185 | + ? $this->resolveClass($dependency) |
|
| 186 | + : $this->resolveDependency($dependency); |
|
| 187 | + } |
|
| 188 | + return $results; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Resolve a single ReflectionParameter dependency. |
|
| 193 | + * @return array|null |
|
| 194 | + */ |
|
| 195 | + protected function resolveDependency(ReflectionParameter $parameter) |
|
| 196 | + { |
|
| 197 | + if ($parameter->isArray() && $parameter->isDefaultValueAvailable()) { |
|
| 198 | + return $parameter->getDefaultValue(); |
|
| 199 | + } |
|
| 200 | + return null; |
|
| 201 | + } |
|
| 202 | 202 | } |
@@ -11,175 +11,175 @@ |
||
| 11 | 11 | |
| 12 | 12 | class Review implements \ArrayAccess |
| 13 | 13 | { |
| 14 | - public $assigned_to; |
|
| 15 | - public $author; |
|
| 16 | - public $avatar; |
|
| 17 | - public $content; |
|
| 18 | - public $custom; |
|
| 19 | - public $date; |
|
| 20 | - public $email; |
|
| 21 | - public $ID; |
|
| 22 | - public $ip_address; |
|
| 23 | - public $modified; |
|
| 24 | - public $pinned; |
|
| 25 | - public $rating; |
|
| 26 | - public $response; |
|
| 27 | - public $review_id; |
|
| 28 | - public $review_type; |
|
| 29 | - public $status; |
|
| 30 | - public $term_ids; |
|
| 31 | - public $title; |
|
| 32 | - public $url; |
|
| 33 | - public $user_id; |
|
| 14 | + public $assigned_to; |
|
| 15 | + public $author; |
|
| 16 | + public $avatar; |
|
| 17 | + public $content; |
|
| 18 | + public $custom; |
|
| 19 | + public $date; |
|
| 20 | + public $email; |
|
| 21 | + public $ID; |
|
| 22 | + public $ip_address; |
|
| 23 | + public $modified; |
|
| 24 | + public $pinned; |
|
| 25 | + public $rating; |
|
| 26 | + public $response; |
|
| 27 | + public $review_id; |
|
| 28 | + public $review_type; |
|
| 29 | + public $status; |
|
| 30 | + public $term_ids; |
|
| 31 | + public $title; |
|
| 32 | + public $url; |
|
| 33 | + public $user_id; |
|
| 34 | 34 | |
| 35 | - public function __construct(WP_Post $post) |
|
| 36 | - { |
|
| 37 | - if (Application::POST_TYPE != $post->post_type) { |
|
| 38 | - return; |
|
| 39 | - } |
|
| 40 | - $this->content = $post->post_content; |
|
| 41 | - $this->date = $post->post_date; |
|
| 42 | - $this->ID = intval($post->ID); |
|
| 43 | - $this->status = $post->post_status; |
|
| 44 | - $this->title = $post->post_title; |
|
| 45 | - $this->user_id = intval($post->post_author); |
|
| 46 | - $this->setProperties($post); |
|
| 47 | - $this->setTermIds($post); |
|
| 48 | - } |
|
| 35 | + public function __construct(WP_Post $post) |
|
| 36 | + { |
|
| 37 | + if (Application::POST_TYPE != $post->post_type) { |
|
| 38 | + return; |
|
| 39 | + } |
|
| 40 | + $this->content = $post->post_content; |
|
| 41 | + $this->date = $post->post_date; |
|
| 42 | + $this->ID = intval($post->ID); |
|
| 43 | + $this->status = $post->post_status; |
|
| 44 | + $this->title = $post->post_title; |
|
| 45 | + $this->user_id = intval($post->post_author); |
|
| 46 | + $this->setProperties($post); |
|
| 47 | + $this->setTermIds($post); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return mixed |
|
| 52 | - */ |
|
| 53 | - public function __get($key) |
|
| 54 | - { |
|
| 55 | - return $this->offsetGet($key); |
|
| 56 | - } |
|
| 50 | + /** |
|
| 51 | + * @return mixed |
|
| 52 | + */ |
|
| 53 | + public function __get($key) |
|
| 54 | + { |
|
| 55 | + return $this->offsetGet($key); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * @return string |
|
| 60 | - */ |
|
| 61 | - public function __toString() |
|
| 62 | - { |
|
| 63 | - return (string) $this->build(); |
|
| 64 | - } |
|
| 58 | + /** |
|
| 59 | + * @return string |
|
| 60 | + */ |
|
| 61 | + public function __toString() |
|
| 62 | + { |
|
| 63 | + return (string) $this->build(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @return ReviewHtml |
|
| 68 | - */ |
|
| 69 | - public function build(array $args = []) |
|
| 70 | - { |
|
| 71 | - if (empty($this->ID)) { |
|
| 72 | - return new ReviewHtml($this); |
|
| 73 | - } |
|
| 74 | - $partial = glsr(SiteReviewsPartial::class); |
|
| 75 | - $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
| 76 | - $partial->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
| 77 | - return $partial->buildReview($this); |
|
| 78 | - } |
|
| 66 | + /** |
|
| 67 | + * @return ReviewHtml |
|
| 68 | + */ |
|
| 69 | + public function build(array $args = []) |
|
| 70 | + { |
|
| 71 | + if (empty($this->ID)) { |
|
| 72 | + return new ReviewHtml($this); |
|
| 73 | + } |
|
| 74 | + $partial = glsr(SiteReviewsPartial::class); |
|
| 75 | + $partial->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
| 76 | + $partial->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
| 77 | + return $partial->buildReview($this); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @param mixed $key |
|
| 82 | - * @return bool |
|
| 83 | - */ |
|
| 84 | - public function offsetExists($key) |
|
| 85 | - { |
|
| 86 | - return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
| 87 | - } |
|
| 80 | + /** |
|
| 81 | + * @param mixed $key |
|
| 82 | + * @return bool |
|
| 83 | + */ |
|
| 84 | + public function offsetExists($key) |
|
| 85 | + { |
|
| 86 | + return property_exists($this, $key) || array_key_exists($key, (array) $this->custom); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param mixed $key |
|
| 91 | - * @return mixed |
|
| 92 | - */ |
|
| 93 | - public function offsetGet($key) |
|
| 94 | - { |
|
| 95 | - if (property_exists($this, $key)) { |
|
| 96 | - return $this->{$key}; |
|
| 97 | - } |
|
| 98 | - return array_key_exists($key, (array) $this->custom) |
|
| 99 | - ? $this->custom[$key] |
|
| 100 | - : null; |
|
| 101 | - } |
|
| 89 | + /** |
|
| 90 | + * @param mixed $key |
|
| 91 | + * @return mixed |
|
| 92 | + */ |
|
| 93 | + public function offsetGet($key) |
|
| 94 | + { |
|
| 95 | + if (property_exists($this, $key)) { |
|
| 96 | + return $this->{$key}; |
|
| 97 | + } |
|
| 98 | + return array_key_exists($key, (array) $this->custom) |
|
| 99 | + ? $this->custom[$key] |
|
| 100 | + : null; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param mixed $key |
|
| 105 | - * @param mixed $value |
|
| 106 | - * @return void |
|
| 107 | - */ |
|
| 108 | - public function offsetSet($key, $value) |
|
| 109 | - { |
|
| 110 | - if (property_exists($this, $key)) { |
|
| 111 | - $this->{$key} = $value; |
|
| 112 | - return; |
|
| 113 | - } |
|
| 114 | - if (!is_array($this->custom)) { |
|
| 115 | - $this->custom = array_filter((array) $this->custom); |
|
| 116 | - } |
|
| 117 | - $this->custom[$key] = $value; |
|
| 118 | - } |
|
| 103 | + /** |
|
| 104 | + * @param mixed $key |
|
| 105 | + * @param mixed $value |
|
| 106 | + * @return void |
|
| 107 | + */ |
|
| 108 | + public function offsetSet($key, $value) |
|
| 109 | + { |
|
| 110 | + if (property_exists($this, $key)) { |
|
| 111 | + $this->{$key} = $value; |
|
| 112 | + return; |
|
| 113 | + } |
|
| 114 | + if (!is_array($this->custom)) { |
|
| 115 | + $this->custom = array_filter((array) $this->custom); |
|
| 116 | + } |
|
| 117 | + $this->custom[$key] = $value; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * @param mixed $key |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - public function offsetUnset($key) |
|
| 125 | - { |
|
| 126 | - $this->offsetSet($key, null); |
|
| 127 | - } |
|
| 120 | + /** |
|
| 121 | + * @param mixed $key |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + public function offsetUnset($key) |
|
| 125 | + { |
|
| 126 | + $this->offsetSet($key, null); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - /** |
|
| 130 | - * @return void |
|
| 131 | - */ |
|
| 132 | - public function render() |
|
| 133 | - { |
|
| 134 | - echo $this->build(); |
|
| 135 | - } |
|
| 129 | + /** |
|
| 130 | + * @return void |
|
| 131 | + */ |
|
| 132 | + public function render() |
|
| 133 | + { |
|
| 134 | + echo $this->build(); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - protected function isModified(array $properties) |
|
| 141 | - { |
|
| 142 | - return $this->date != $properties['date'] |
|
| 143 | - || $this->content != $properties['content'] |
|
| 144 | - || $this->title != $properties['title']; |
|
| 145 | - } |
|
| 137 | + /** |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + protected function isModified(array $properties) |
|
| 141 | + { |
|
| 142 | + return $this->date != $properties['date'] |
|
| 143 | + || $this->content != $properties['content'] |
|
| 144 | + || $this->title != $properties['title']; |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * @return void |
|
| 149 | - */ |
|
| 150 | - protected function setProperties(WP_Post $post) |
|
| 151 | - { |
|
| 152 | - $defaults = [ |
|
| 153 | - 'author' => __('Anonymous', 'site-reviews'), |
|
| 154 | - 'date' => '', |
|
| 155 | - 'review_id' => '', |
|
| 156 | - 'review_type' => 'local', |
|
| 157 | - ]; |
|
| 158 | - $meta = array_filter( |
|
| 159 | - array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
| 160 | - 'strlen' |
|
| 161 | - ); |
|
| 162 | - $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
| 163 | - $this->modified = $this->isModified($properties); |
|
| 164 | - array_walk($properties, function ($value, $key) { |
|
| 165 | - if (!property_exists($this, $key) || isset($this->$key)) { |
|
| 166 | - return; |
|
| 167 | - } |
|
| 168 | - $this->$key = maybe_unserialize($value); |
|
| 169 | - }); |
|
| 170 | - } |
|
| 147 | + /** |
|
| 148 | + * @return void |
|
| 149 | + */ |
|
| 150 | + protected function setProperties(WP_Post $post) |
|
| 151 | + { |
|
| 152 | + $defaults = [ |
|
| 153 | + 'author' => __('Anonymous', 'site-reviews'), |
|
| 154 | + 'date' => '', |
|
| 155 | + 'review_id' => '', |
|
| 156 | + 'review_type' => 'local', |
|
| 157 | + ]; |
|
| 158 | + $meta = array_filter( |
|
| 159 | + array_map('array_shift', array_filter((array) get_post_meta($post->ID))), |
|
| 160 | + 'strlen' |
|
| 161 | + ); |
|
| 162 | + $properties = glsr(CreateReviewDefaults::class)->restrict(array_merge($defaults, $meta)); |
|
| 163 | + $this->modified = $this->isModified($properties); |
|
| 164 | + array_walk($properties, function ($value, $key) { |
|
| 165 | + if (!property_exists($this, $key) || isset($this->$key)) { |
|
| 166 | + return; |
|
| 167 | + } |
|
| 168 | + $this->$key = maybe_unserialize($value); |
|
| 169 | + }); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * @return void |
|
| 174 | - */ |
|
| 175 | - protected function setTermIds(WP_Post $post) |
|
| 176 | - { |
|
| 177 | - $this->term_ids = []; |
|
| 178 | - if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
| 179 | - return; |
|
| 180 | - } |
|
| 181 | - foreach ($terms as $term) { |
|
| 182 | - $this->term_ids[] = $term->term_id; |
|
| 183 | - } |
|
| 184 | - } |
|
| 172 | + /** |
|
| 173 | + * @return void |
|
| 174 | + */ |
|
| 175 | + protected function setTermIds(WP_Post $post) |
|
| 176 | + { |
|
| 177 | + $this->term_ids = []; |
|
| 178 | + if (!is_array($terms = get_the_terms($post, Application::TAXONOMY))) { |
|
| 179 | + return; |
|
| 180 | + } |
|
| 181 | + foreach ($terms as $term) { |
|
| 182 | + $this->term_ids[] = $term->term_id; |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | 185 | } |
@@ -7,55 +7,55 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SiteReviewsFormWidget extends Widget |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @param array $instance |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function form($instance) |
|
| 15 | - { |
|
| 16 | - $this->widgetArgs = glsr(SiteReviewsFormShortcode::class)->normalize($instance); |
|
| 17 | - $terms = glsr(Database::class)->getTerms(); |
|
| 18 | - $this->renderField('text', [ |
|
| 19 | - 'class' => 'widefat', |
|
| 20 | - 'label' => __('Title', 'site-reviews'), |
|
| 21 | - 'name' => 'title', |
|
| 22 | - ]); |
|
| 23 | - $this->renderField('textarea', [ |
|
| 24 | - 'class' => 'widefat', |
|
| 25 | - 'label' => __('Description', 'site-reviews'), |
|
| 26 | - 'name' => 'description', |
|
| 27 | - ]); |
|
| 28 | - $this->renderField('select', [ |
|
| 29 | - 'class' => 'widefat', |
|
| 30 | - 'label' => __('Automatically assign a category', 'site-reviews'), |
|
| 31 | - 'name' => 'category', |
|
| 32 | - 'options' => ['' => __('Do not assign a category', 'site-reviews')] + $terms, |
|
| 33 | - ]); |
|
| 34 | - $this->renderField('text', [ |
|
| 35 | - 'class' => 'widefat', |
|
| 36 | - 'default' => '', |
|
| 37 | - 'description' => sprintf(__('You may also enter %s to assign to the current post.', 'site-reviews'), '<code>post_id</code>'), |
|
| 38 | - 'label' => __('Assign reviews to a custom page/post ID', 'site-reviews'), |
|
| 39 | - 'name' => 'assign_to', |
|
| 40 | - ]); |
|
| 41 | - $this->renderField('text', [ |
|
| 42 | - 'class' => 'widefat', |
|
| 43 | - 'label' => __('Enter any custom CSS classes here', 'site-reviews'), |
|
| 44 | - 'name' => 'class', |
|
| 45 | - ]); |
|
| 46 | - $this->renderField('checkbox', [ |
|
| 47 | - 'name' => 'hide', |
|
| 48 | - 'options' => glsr(SiteReviewsFormShortcode::class)->getHideOptions(), |
|
| 49 | - ]); |
|
| 50 | - } |
|
| 10 | + /** |
|
| 11 | + * @param array $instance |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function form($instance) |
|
| 15 | + { |
|
| 16 | + $this->widgetArgs = glsr(SiteReviewsFormShortcode::class)->normalize($instance); |
|
| 17 | + $terms = glsr(Database::class)->getTerms(); |
|
| 18 | + $this->renderField('text', [ |
|
| 19 | + 'class' => 'widefat', |
|
| 20 | + 'label' => __('Title', 'site-reviews'), |
|
| 21 | + 'name' => 'title', |
|
| 22 | + ]); |
|
| 23 | + $this->renderField('textarea', [ |
|
| 24 | + 'class' => 'widefat', |
|
| 25 | + 'label' => __('Description', 'site-reviews'), |
|
| 26 | + 'name' => 'description', |
|
| 27 | + ]); |
|
| 28 | + $this->renderField('select', [ |
|
| 29 | + 'class' => 'widefat', |
|
| 30 | + 'label' => __('Automatically assign a category', 'site-reviews'), |
|
| 31 | + 'name' => 'category', |
|
| 32 | + 'options' => ['' => __('Do not assign a category', 'site-reviews')] + $terms, |
|
| 33 | + ]); |
|
| 34 | + $this->renderField('text', [ |
|
| 35 | + 'class' => 'widefat', |
|
| 36 | + 'default' => '', |
|
| 37 | + 'description' => sprintf(__('You may also enter %s to assign to the current post.', 'site-reviews'), '<code>post_id</code>'), |
|
| 38 | + 'label' => __('Assign reviews to a custom page/post ID', 'site-reviews'), |
|
| 39 | + 'name' => 'assign_to', |
|
| 40 | + ]); |
|
| 41 | + $this->renderField('text', [ |
|
| 42 | + 'class' => 'widefat', |
|
| 43 | + 'label' => __('Enter any custom CSS classes here', 'site-reviews'), |
|
| 44 | + 'name' => 'class', |
|
| 45 | + ]); |
|
| 46 | + $this->renderField('checkbox', [ |
|
| 47 | + 'name' => 'hide', |
|
| 48 | + 'options' => glsr(SiteReviewsFormShortcode::class)->getHideOptions(), |
|
| 49 | + ]); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * @param array $args |
|
| 54 | - * @param array $instance |
|
| 55 | - * @return void |
|
| 56 | - */ |
|
| 57 | - public function widget($args, $instance) |
|
| 58 | - { |
|
| 59 | - echo glsr(SiteReviewsFormShortcode::class)->build($instance, $args, 'widget'); |
|
| 60 | - } |
|
| 52 | + /** |
|
| 53 | + * @param array $args |
|
| 54 | + * @param array $instance |
|
| 55 | + * @return void |
|
| 56 | + */ |
|
| 57 | + public function widget($args, $instance) |
|
| 58 | + { |
|
| 59 | + echo glsr(SiteReviewsFormShortcode::class)->build($instance, $args, 'widget'); |
|
| 60 | + } |
|
| 61 | 61 | } |
@@ -7,92 +7,92 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SiteReviewsWidget extends Widget |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @param array $instance |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function form($instance) |
|
| 15 | - { |
|
| 16 | - $this->widgetArgs = glsr(SiteReviewsShortcode::class)->normalize($instance); |
|
| 17 | - $terms = glsr(Database::class)->getTerms(); |
|
| 18 | - $this->renderField('text', [ |
|
| 19 | - 'class' => 'widefat', |
|
| 20 | - 'label' => __('Title', 'site-reviews'), |
|
| 21 | - 'name' => 'title', |
|
| 22 | - ]); |
|
| 23 | - $this->renderField('number', [ |
|
| 24 | - 'class' => 'small-text', |
|
| 25 | - 'default' => 5, |
|
| 26 | - 'label' => __('How many reviews would you like to display?', 'site-reviews'), |
|
| 27 | - 'max' => 100, |
|
| 28 | - 'name' => 'count', |
|
| 29 | - ]); |
|
| 30 | - $this->renderField('select', [ |
|
| 31 | - 'label' => __('What is the minimum rating to display?', 'site-reviews'), |
|
| 32 | - 'name' => 'rating', |
|
| 33 | - 'options' => [ |
|
| 34 | - '5' => sprintf(_n('%s star', '%s stars', 5, 'site-reviews'), 5), |
|
| 35 | - '4' => sprintf(_n('%s star', '%s stars', 4, 'site-reviews'), 4), |
|
| 36 | - '3' => sprintf(_n('%s star', '%s stars', 3, 'site-reviews'), 3), |
|
| 37 | - '2' => sprintf(_n('%s star', '%s stars', 2, 'site-reviews'), 2), |
|
| 38 | - '1' => sprintf(_n('%s star', '%s stars', 1, 'site-reviews'), 1), |
|
| 39 | - ], |
|
| 40 | - ]); |
|
| 41 | - if (count(glsr()->reviewTypes) > 1) { |
|
| 42 | - $this->renderField('select', [ |
|
| 43 | - 'class' => 'widefat', |
|
| 44 | - 'label' => __('Which type of review would you like to display?', 'site-reviews'), |
|
| 45 | - 'name' => 'type', |
|
| 46 | - 'options' => ['' => __('All Reviews', 'site-reviews')] + glsr()->reviewTypes, |
|
| 47 | - ]); |
|
| 48 | - } |
|
| 49 | - if (!empty($terms)) { |
|
| 50 | - $this->renderField('select', [ |
|
| 51 | - 'class' => 'widefat', |
|
| 52 | - 'label' => __('Limit reviews to this category', 'site-reviews'), |
|
| 53 | - 'name' => 'category', |
|
| 54 | - 'options' => ['' => __('All Categories', 'site-reviews')] + $terms, |
|
| 55 | - ]); |
|
| 56 | - } |
|
| 57 | - $this->renderField('text', [ |
|
| 58 | - 'class' => 'widefat', |
|
| 59 | - 'default' => '', |
|
| 60 | - 'description' => sprintf(__("Separate multiple ID's with a comma. You may also enter %s to automatically represent the current page/post ID.", 'site-reviews'), '<code>post_id</code>'), |
|
| 61 | - 'label' => __('Limit reviews to those assigned to this page/post ID', 'site-reviews'), |
|
| 62 | - 'name' => 'assigned_to', |
|
| 63 | - ]); |
|
| 64 | - $this->renderField('text', [ |
|
| 65 | - 'class' => 'widefat', |
|
| 66 | - 'label' => __('Enter any custom CSS classes here', 'site-reviews'), |
|
| 67 | - 'name' => 'class', |
|
| 68 | - ]); |
|
| 69 | - $this->renderField('checkbox', [ |
|
| 70 | - 'name' => 'hide', |
|
| 71 | - 'options' => glsr(SiteReviewsShortcode::class)->getHideOptions(), |
|
| 72 | - ]); |
|
| 73 | - } |
|
| 10 | + /** |
|
| 11 | + * @param array $instance |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function form($instance) |
|
| 15 | + { |
|
| 16 | + $this->widgetArgs = glsr(SiteReviewsShortcode::class)->normalize($instance); |
|
| 17 | + $terms = glsr(Database::class)->getTerms(); |
|
| 18 | + $this->renderField('text', [ |
|
| 19 | + 'class' => 'widefat', |
|
| 20 | + 'label' => __('Title', 'site-reviews'), |
|
| 21 | + 'name' => 'title', |
|
| 22 | + ]); |
|
| 23 | + $this->renderField('number', [ |
|
| 24 | + 'class' => 'small-text', |
|
| 25 | + 'default' => 5, |
|
| 26 | + 'label' => __('How many reviews would you like to display?', 'site-reviews'), |
|
| 27 | + 'max' => 100, |
|
| 28 | + 'name' => 'count', |
|
| 29 | + ]); |
|
| 30 | + $this->renderField('select', [ |
|
| 31 | + 'label' => __('What is the minimum rating to display?', 'site-reviews'), |
|
| 32 | + 'name' => 'rating', |
|
| 33 | + 'options' => [ |
|
| 34 | + '5' => sprintf(_n('%s star', '%s stars', 5, 'site-reviews'), 5), |
|
| 35 | + '4' => sprintf(_n('%s star', '%s stars', 4, 'site-reviews'), 4), |
|
| 36 | + '3' => sprintf(_n('%s star', '%s stars', 3, 'site-reviews'), 3), |
|
| 37 | + '2' => sprintf(_n('%s star', '%s stars', 2, 'site-reviews'), 2), |
|
| 38 | + '1' => sprintf(_n('%s star', '%s stars', 1, 'site-reviews'), 1), |
|
| 39 | + ], |
|
| 40 | + ]); |
|
| 41 | + if (count(glsr()->reviewTypes) > 1) { |
|
| 42 | + $this->renderField('select', [ |
|
| 43 | + 'class' => 'widefat', |
|
| 44 | + 'label' => __('Which type of review would you like to display?', 'site-reviews'), |
|
| 45 | + 'name' => 'type', |
|
| 46 | + 'options' => ['' => __('All Reviews', 'site-reviews')] + glsr()->reviewTypes, |
|
| 47 | + ]); |
|
| 48 | + } |
|
| 49 | + if (!empty($terms)) { |
|
| 50 | + $this->renderField('select', [ |
|
| 51 | + 'class' => 'widefat', |
|
| 52 | + 'label' => __('Limit reviews to this category', 'site-reviews'), |
|
| 53 | + 'name' => 'category', |
|
| 54 | + 'options' => ['' => __('All Categories', 'site-reviews')] + $terms, |
|
| 55 | + ]); |
|
| 56 | + } |
|
| 57 | + $this->renderField('text', [ |
|
| 58 | + 'class' => 'widefat', |
|
| 59 | + 'default' => '', |
|
| 60 | + 'description' => sprintf(__("Separate multiple ID's with a comma. You may also enter %s to automatically represent the current page/post ID.", 'site-reviews'), '<code>post_id</code>'), |
|
| 61 | + 'label' => __('Limit reviews to those assigned to this page/post ID', 'site-reviews'), |
|
| 62 | + 'name' => 'assigned_to', |
|
| 63 | + ]); |
|
| 64 | + $this->renderField('text', [ |
|
| 65 | + 'class' => 'widefat', |
|
| 66 | + 'label' => __('Enter any custom CSS classes here', 'site-reviews'), |
|
| 67 | + 'name' => 'class', |
|
| 68 | + ]); |
|
| 69 | + $this->renderField('checkbox', [ |
|
| 70 | + 'name' => 'hide', |
|
| 71 | + 'options' => glsr(SiteReviewsShortcode::class)->getHideOptions(), |
|
| 72 | + ]); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @param array $newInstance |
|
| 77 | - * @param array $oldInstance |
|
| 78 | - * @return array |
|
| 79 | - */ |
|
| 80 | - public function update($newInstance, $oldInstance) |
|
| 81 | - { |
|
| 82 | - if (!is_numeric($newInstance['count'])) { |
|
| 83 | - $newInstance['count'] = 10; |
|
| 84 | - } |
|
| 85 | - $newInstance['count'] = min(50, max(0, intval($newInstance['count']))); |
|
| 86 | - return parent::update($newInstance, $oldInstance); |
|
| 87 | - } |
|
| 75 | + /** |
|
| 76 | + * @param array $newInstance |
|
| 77 | + * @param array $oldInstance |
|
| 78 | + * @return array |
|
| 79 | + */ |
|
| 80 | + public function update($newInstance, $oldInstance) |
|
| 81 | + { |
|
| 82 | + if (!is_numeric($newInstance['count'])) { |
|
| 83 | + $newInstance['count'] = 10; |
|
| 84 | + } |
|
| 85 | + $newInstance['count'] = min(50, max(0, intval($newInstance['count']))); |
|
| 86 | + return parent::update($newInstance, $oldInstance); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * @param array $args |
|
| 91 | - * @param array $instance |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - public function widget($args, $instance) |
|
| 95 | - { |
|
| 96 | - echo glsr(SiteReviewsShortcode::class)->build($instance, $args, 'widget'); |
|
| 97 | - } |
|
| 89 | + /** |
|
| 90 | + * @param array $args |
|
| 91 | + * @param array $instance |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + public function widget($args, $instance) |
|
| 95 | + { |
|
| 96 | + echo glsr(SiteReviewsShortcode::class)->build($instance, $args, 'widget'); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -7,60 +7,60 @@ |
||
| 7 | 7 | |
| 8 | 8 | class SiteReviewsSummaryWidget extends Widget |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @param array $instance |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function form($instance) |
|
| 15 | - { |
|
| 16 | - $this->widgetArgs = glsr(SiteReviewsSummaryShortcode::class)->normalize($instance); |
|
| 17 | - $terms = glsr(Database::class)->getTerms(); |
|
| 18 | - $this->renderField('text', [ |
|
| 19 | - 'class' => 'widefat', |
|
| 20 | - 'label' => __('Title', 'site-reviews'), |
|
| 21 | - 'name' => 'title', |
|
| 22 | - ]); |
|
| 23 | - if (count(glsr()->reviewTypes) > 1) { |
|
| 24 | - $this->renderField('select', [ |
|
| 25 | - 'class' => 'widefat', |
|
| 26 | - 'label' => __('Which type of review would you like to use?', 'site-reviews'), |
|
| 27 | - 'name' => 'type', |
|
| 28 | - 'options' => ['' => __('All review types', 'site-reviews')] + glsr()->reviewTypes, |
|
| 29 | - ]); |
|
| 30 | - } |
|
| 31 | - if (!empty($terms)) { |
|
| 32 | - $this->renderField('select', [ |
|
| 33 | - 'class' => 'widefat', |
|
| 34 | - 'label' => __('Limit summary to this category', 'site-reviews'), |
|
| 35 | - 'name' => 'category', |
|
| 36 | - 'options' => ['' => __('All Categories', 'site-reviews')] + $terms, |
|
| 37 | - ]); |
|
| 38 | - } |
|
| 39 | - $this->renderField('text', [ |
|
| 40 | - 'class' => 'widefat', |
|
| 41 | - 'default' => '', |
|
| 42 | - 'description' => sprintf(__("Separate multiple ID's with a comma. You may also enter %s to automatically represent the current page/post ID.", 'site-reviews'), '<code>post_id</code>'), |
|
| 43 | - 'label' => __('Limit summary to reviews assigned to a page/post ID', 'site-reviews'), |
|
| 44 | - 'name' => 'assigned_to', |
|
| 45 | - ]); |
|
| 46 | - $this->renderField('text', [ |
|
| 47 | - 'class' => 'widefat', |
|
| 48 | - 'label' => __('Enter any custom CSS classes here', 'site-reviews'), |
|
| 49 | - 'name' => 'class', |
|
| 50 | - ]); |
|
| 51 | - $this->renderField('checkbox', [ |
|
| 52 | - 'name' => 'hide', |
|
| 53 | - 'options' => glsr(SiteReviewsSummaryShortcode::class)->getHideOptions(), |
|
| 54 | - ]); |
|
| 55 | - } |
|
| 10 | + /** |
|
| 11 | + * @param array $instance |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function form($instance) |
|
| 15 | + { |
|
| 16 | + $this->widgetArgs = glsr(SiteReviewsSummaryShortcode::class)->normalize($instance); |
|
| 17 | + $terms = glsr(Database::class)->getTerms(); |
|
| 18 | + $this->renderField('text', [ |
|
| 19 | + 'class' => 'widefat', |
|
| 20 | + 'label' => __('Title', 'site-reviews'), |
|
| 21 | + 'name' => 'title', |
|
| 22 | + ]); |
|
| 23 | + if (count(glsr()->reviewTypes) > 1) { |
|
| 24 | + $this->renderField('select', [ |
|
| 25 | + 'class' => 'widefat', |
|
| 26 | + 'label' => __('Which type of review would you like to use?', 'site-reviews'), |
|
| 27 | + 'name' => 'type', |
|
| 28 | + 'options' => ['' => __('All review types', 'site-reviews')] + glsr()->reviewTypes, |
|
| 29 | + ]); |
|
| 30 | + } |
|
| 31 | + if (!empty($terms)) { |
|
| 32 | + $this->renderField('select', [ |
|
| 33 | + 'class' => 'widefat', |
|
| 34 | + 'label' => __('Limit summary to this category', 'site-reviews'), |
|
| 35 | + 'name' => 'category', |
|
| 36 | + 'options' => ['' => __('All Categories', 'site-reviews')] + $terms, |
|
| 37 | + ]); |
|
| 38 | + } |
|
| 39 | + $this->renderField('text', [ |
|
| 40 | + 'class' => 'widefat', |
|
| 41 | + 'default' => '', |
|
| 42 | + 'description' => sprintf(__("Separate multiple ID's with a comma. You may also enter %s to automatically represent the current page/post ID.", 'site-reviews'), '<code>post_id</code>'), |
|
| 43 | + 'label' => __('Limit summary to reviews assigned to a page/post ID', 'site-reviews'), |
|
| 44 | + 'name' => 'assigned_to', |
|
| 45 | + ]); |
|
| 46 | + $this->renderField('text', [ |
|
| 47 | + 'class' => 'widefat', |
|
| 48 | + 'label' => __('Enter any custom CSS classes here', 'site-reviews'), |
|
| 49 | + 'name' => 'class', |
|
| 50 | + ]); |
|
| 51 | + $this->renderField('checkbox', [ |
|
| 52 | + 'name' => 'hide', |
|
| 53 | + 'options' => glsr(SiteReviewsSummaryShortcode::class)->getHideOptions(), |
|
| 54 | + ]); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @param array $args |
|
| 59 | - * @param array $instance |
|
| 60 | - * @return void |
|
| 61 | - */ |
|
| 62 | - public function widget($args, $instance) |
|
| 63 | - { |
|
| 64 | - echo glsr(SiteReviewsSummaryShortcode::class)->build($instance, $args, 'widget'); |
|
| 65 | - } |
|
| 57 | + /** |
|
| 58 | + * @param array $args |
|
| 59 | + * @param array $instance |
|
| 60 | + * @return void |
|
| 61 | + */ |
|
| 62 | + public function widget($args, $instance) |
|
| 63 | + { |
|
| 64 | + echo glsr(SiteReviewsSummaryShortcode::class)->build($instance, $args, 'widget'); |
|
| 65 | + } |
|
| 66 | 66 | } |
@@ -7,54 +7,54 @@ |
||
| 7 | 7 | |
| 8 | 8 | abstract class Widget extends WP_Widget |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var array |
|
| 12 | - */ |
|
| 13 | - protected $widgetArgs; |
|
| 10 | + /** |
|
| 11 | + * @var array |
|
| 12 | + */ |
|
| 13 | + protected $widgetArgs; |
|
| 14 | 14 | |
| 15 | - public function __construct($idBase, $name, $values) |
|
| 16 | - { |
|
| 17 | - $controlOptions = $widgetOptions = []; |
|
| 18 | - if (isset($values['class'])) { |
|
| 19 | - $widgetOptions['classname'] = $values['class']; |
|
| 20 | - } |
|
| 21 | - if (isset($values['description'])) { |
|
| 22 | - $widgetOptions['description'] = $values['description']; |
|
| 23 | - } |
|
| 24 | - if (isset($values['width'])) { |
|
| 25 | - $controlOptions['width'] = $values['width']; |
|
| 26 | - } |
|
| 27 | - parent::__construct($idBase, $name, $widgetOptions, $controlOptions); |
|
| 28 | - } |
|
| 15 | + public function __construct($idBase, $name, $values) |
|
| 16 | + { |
|
| 17 | + $controlOptions = $widgetOptions = []; |
|
| 18 | + if (isset($values['class'])) { |
|
| 19 | + $widgetOptions['classname'] = $values['class']; |
|
| 20 | + } |
|
| 21 | + if (isset($values['description'])) { |
|
| 22 | + $widgetOptions['description'] = $values['description']; |
|
| 23 | + } |
|
| 24 | + if (isset($values['width'])) { |
|
| 25 | + $controlOptions['width'] = $values['width']; |
|
| 26 | + } |
|
| 27 | + parent::__construct($idBase, $name, $widgetOptions, $controlOptions); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param string $tag |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - protected function renderField($tag, array $args = []) |
|
| 35 | - { |
|
| 36 | - $args = $this->normalizeFieldAttributes($tag, $args); |
|
| 37 | - $field = glsr(Builder::class)->{$tag}($args['name'], $args); |
|
| 38 | - echo glsr(Builder::class)->div($field, [ |
|
| 39 | - 'class' => 'glsr-field', |
|
| 40 | - ]); |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * @param string $tag |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + protected function renderField($tag, array $args = []) |
|
| 35 | + { |
|
| 36 | + $args = $this->normalizeFieldAttributes($tag, $args); |
|
| 37 | + $field = glsr(Builder::class)->{$tag}($args['name'], $args); |
|
| 38 | + echo glsr(Builder::class)->div($field, [ |
|
| 39 | + 'class' => 'glsr-field', |
|
| 40 | + ]); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param string $tag |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - protected function normalizeFieldAttributes($tag, array $args) |
|
| 48 | - { |
|
| 49 | - if (empty($args['value'])) { |
|
| 50 | - $args['value'] = $this->widgetArgs[$args['name']]; |
|
| 51 | - } |
|
| 52 | - if (empty($this->widgetArgs['options']) && in_array($tag, ['checkbox', 'radio'])) { |
|
| 53 | - $args['checked'] = in_array($args['value'], (array) $this->widgetArgs[$args['name']]); |
|
| 54 | - } |
|
| 55 | - $args['id'] = $this->get_field_id($args['name']); |
|
| 56 | - $args['name'] = $this->get_field_name($args['name']); |
|
| 57 | - $args['is_widget'] = true; |
|
| 58 | - return $args; |
|
| 59 | - } |
|
| 43 | + /** |
|
| 44 | + * @param string $tag |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + protected function normalizeFieldAttributes($tag, array $args) |
|
| 48 | + { |
|
| 49 | + if (empty($args['value'])) { |
|
| 50 | + $args['value'] = $this->widgetArgs[$args['name']]; |
|
| 51 | + } |
|
| 52 | + if (empty($this->widgetArgs['options']) && in_array($tag, ['checkbox', 'radio'])) { |
|
| 53 | + $args['checked'] = in_array($args['value'], (array) $this->widgetArgs[$args['name']]); |
|
| 54 | + } |
|
| 55 | + $args['id'] = $this->get_field_id($args['name']); |
|
| 56 | + $args['name'] = $this->get_field_name($args['name']); |
|
| 57 | + $args['is_widget'] = true; |
|
| 58 | + return $args; |
|
| 59 | + } |
|
| 60 | 60 | } |