@@ -4,38 +4,38 @@ |
||
| 4 | 4 | |
| 5 | 5 | class YesNo extends Field |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return string|void |
|
| 9 | - */ |
|
| 10 | - public function build() |
|
| 11 | - { |
|
| 12 | - $this->builder->tag = 'input'; |
|
| 13 | - $this->mergeFieldArgs(); |
|
| 14 | - return $this->builder->getTag(); |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * @return string|void |
|
| 9 | + */ |
|
| 10 | + public function build() |
|
| 11 | + { |
|
| 12 | + $this->builder->tag = 'input'; |
|
| 13 | + $this->mergeFieldArgs(); |
|
| 14 | + return $this->builder->getTag(); |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - public static function defaults() |
|
| 21 | - { |
|
| 22 | - return [ |
|
| 23 | - 'class' => 'inline', |
|
| 24 | - ]; |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + public static function defaults() |
|
| 21 | + { |
|
| 22 | + return [ |
|
| 23 | + 'class' => 'inline', |
|
| 24 | + ]; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @return array |
|
| 29 | - */ |
|
| 30 | - public static function required() |
|
| 31 | - { |
|
| 32 | - return [ |
|
| 33 | - 'is_multi' => true, |
|
| 34 | - 'options' => [ |
|
| 35 | - 'no' => __('No', 'site-reviews'), |
|
| 36 | - 'yes' => __('Yes', 'site-reviews'), |
|
| 37 | - ], |
|
| 38 | - 'type' => 'radio', |
|
| 39 | - ]; |
|
| 40 | - } |
|
| 27 | + /** |
|
| 28 | + * @return array |
|
| 29 | + */ |
|
| 30 | + public static function required() |
|
| 31 | + { |
|
| 32 | + return [ |
|
| 33 | + 'is_multi' => true, |
|
| 34 | + 'options' => [ |
|
| 35 | + 'no' => __('No', 'site-reviews'), |
|
| 36 | + 'yes' => __('Yes', 'site-reviews'), |
|
| 37 | + ], |
|
| 38 | + 'type' => 'radio', |
|
| 39 | + ]; |
|
| 40 | + } |
|
| 41 | 41 | } |
@@ -32,8 +32,8 @@ |
||
| 32 | 32 | return [ |
| 33 | 33 | 'is_multi' => true, |
| 34 | 34 | 'options' => [ |
| 35 | - 'no' => __('No', 'site-reviews'), |
|
| 36 | - 'yes' => __('Yes', 'site-reviews'), |
|
| 35 | + 'no' => __( 'No', 'site-reviews' ), |
|
| 36 | + 'yes' => __( 'Yes', 'site-reviews' ), |
|
| 37 | 37 | ], |
| 38 | 38 | 'type' => 'radio', |
| 39 | 39 | ]; |
@@ -4,13 +4,13 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Number extends Field |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return array |
|
| 9 | - */ |
|
| 10 | - public static function defaults() |
|
| 11 | - { |
|
| 12 | - return [ |
|
| 13 | - 'class' => 'small-text', |
|
| 14 | - ]; |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * @return array |
|
| 9 | + */ |
|
| 10 | + public static function defaults() |
|
| 11 | + { |
|
| 12 | + return [ |
|
| 13 | + 'class' => 'small-text', |
|
| 14 | + ]; |
|
| 15 | + } |
|
| 16 | 16 | } |
@@ -4,23 +4,23 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Checkbox extends Field |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @return array |
|
| 9 | - */ |
|
| 10 | - public static function defaults() |
|
| 11 | - { |
|
| 12 | - return [ |
|
| 13 | - 'value' => 1, |
|
| 14 | - ]; |
|
| 15 | - } |
|
| 7 | + /** |
|
| 8 | + * @return array |
|
| 9 | + */ |
|
| 10 | + public static function defaults() |
|
| 11 | + { |
|
| 12 | + return [ |
|
| 13 | + 'value' => 1, |
|
| 14 | + ]; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return array |
|
| 19 | - */ |
|
| 20 | - public static function required() |
|
| 21 | - { |
|
| 22 | - return [ |
|
| 23 | - 'is_multi' => true, |
|
| 24 | - ]; |
|
| 25 | - } |
|
| 17 | + /** |
|
| 18 | + * @return array |
|
| 19 | + */ |
|
| 20 | + public static function required() |
|
| 21 | + { |
|
| 22 | + return [ |
|
| 23 | + 'is_multi' => true, |
|
| 24 | + ]; |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -6,29 +6,29 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Rating extends Field |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return string|void |
|
| 11 | - */ |
|
| 12 | - public function build() |
|
| 13 | - { |
|
| 14 | - $this->builder->tag = 'select'; |
|
| 15 | - $this->mergeFieldArgs(); |
|
| 16 | - return $this->builder->getTag(); |
|
| 17 | - } |
|
| 9 | + /** |
|
| 10 | + * @return string|void |
|
| 11 | + */ |
|
| 12 | + public function build() |
|
| 13 | + { |
|
| 14 | + $this->builder->tag = 'select'; |
|
| 15 | + $this->mergeFieldArgs(); |
|
| 16 | + return $this->builder->getTag(); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @return array |
|
| 21 | - */ |
|
| 22 | - public static function required() |
|
| 23 | - { |
|
| 24 | - $options = ['' => __('Select a Rating', 'site-reviews')]; |
|
| 25 | - foreach (range(glsr()->constant('MAX_RATING', RatingModule::class), 1) as $rating) { |
|
| 26 | - $options[$rating] = sprintf(_n('%s Star', '%s Stars', $rating, 'site-reviews'), $rating); |
|
| 27 | - } |
|
| 28 | - return [ |
|
| 29 | - 'class' => 'glsr-star-rating', |
|
| 30 | - 'options' => $options, |
|
| 31 | - 'type' => 'select', |
|
| 32 | - ]; |
|
| 33 | - } |
|
| 19 | + /** |
|
| 20 | + * @return array |
|
| 21 | + */ |
|
| 22 | + public static function required() |
|
| 23 | + { |
|
| 24 | + $options = ['' => __('Select a Rating', 'site-reviews')]; |
|
| 25 | + foreach (range(glsr()->constant('MAX_RATING', RatingModule::class), 1) as $rating) { |
|
| 26 | + $options[$rating] = sprintf(_n('%s Star', '%s Stars', $rating, 'site-reviews'), $rating); |
|
| 27 | + } |
|
| 28 | + return [ |
|
| 29 | + 'class' => 'glsr-star-rating', |
|
| 30 | + 'options' => $options, |
|
| 31 | + 'type' => 'select', |
|
| 32 | + ]; |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -21,9 +21,9 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | public static function required() |
| 23 | 23 | { |
| 24 | - $options = ['' => __('Select a Rating', 'site-reviews')]; |
|
| 25 | - foreach (range(glsr()->constant('MAX_RATING', RatingModule::class), 1) as $rating) { |
|
| 26 | - $options[$rating] = sprintf(_n('%s Star', '%s Stars', $rating, 'site-reviews'), $rating); |
|
| 24 | + $options = ['' => __( 'Select a Rating', 'site-reviews' )]; |
|
| 25 | + foreach( range( glsr()->constant( 'MAX_RATING', RatingModule::class ), 1 ) as $rating ) { |
|
| 26 | + $options[$rating] = sprintf( _n( '%s Star', '%s Stars', $rating, 'site-reviews' ), $rating ); |
|
| 27 | 27 | } |
| 28 | 28 | return [ |
| 29 | 29 | 'class' => 'glsr-star-rating', |
@@ -4,36 +4,36 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Form |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @param string $id |
|
| 9 | - * @return string |
|
| 10 | - */ |
|
| 11 | - public function buildFields($id) |
|
| 12 | - { |
|
| 13 | - return array_reduce($this->getFields($id), function ($carry, $field) { |
|
| 14 | - return $carry.$field; |
|
| 15 | - }); |
|
| 16 | - } |
|
| 7 | + /** |
|
| 8 | + * @param string $id |
|
| 9 | + * @return string |
|
| 10 | + */ |
|
| 11 | + public function buildFields($id) |
|
| 12 | + { |
|
| 13 | + return array_reduce($this->getFields($id), function ($carry, $field) { |
|
| 14 | + return $carry.$field; |
|
| 15 | + }); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @param string $id |
|
| 20 | - * @return array |
|
| 21 | - */ |
|
| 22 | - public function getFields($id) |
|
| 23 | - { |
|
| 24 | - $fields = []; |
|
| 25 | - foreach (glsr()->config('forms/'.$id) as $name => $field) { |
|
| 26 | - $fields[] = new Field(wp_parse_args($field, ['name' => $name])); |
|
| 27 | - } |
|
| 28 | - return $fields; |
|
| 29 | - } |
|
| 18 | + /** |
|
| 19 | + * @param string $id |
|
| 20 | + * @return array |
|
| 21 | + */ |
|
| 22 | + public function getFields($id) |
|
| 23 | + { |
|
| 24 | + $fields = []; |
|
| 25 | + foreach (glsr()->config('forms/'.$id) as $name => $field) { |
|
| 26 | + $fields[] = new Field(wp_parse_args($field, ['name' => $name])); |
|
| 27 | + } |
|
| 28 | + return $fields; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param string $id |
|
| 33 | - * @return void |
|
| 34 | - */ |
|
| 35 | - public function renderFields($id) |
|
| 36 | - { |
|
| 37 | - echo $this->buildFields($id); |
|
| 38 | - } |
|
| 31 | + /** |
|
| 32 | + * @param string $id |
|
| 33 | + * @return void |
|
| 34 | + */ |
|
| 35 | + public function renderFields($id) |
|
| 36 | + { |
|
| 37 | + echo $this->buildFields($id); |
|
| 38 | + } |
|
| 39 | 39 | } |
@@ -8,9 +8,9 @@ discard block |
||
| 8 | 8 | * @param string $id |
| 9 | 9 | * @return string |
| 10 | 10 | */ |
| 11 | - public function buildFields($id) |
|
| 11 | + public function buildFields( $id ) |
|
| 12 | 12 | { |
| 13 | - return array_reduce($this->getFields($id), function ($carry, $field) { |
|
| 13 | + return array_reduce( $this->getFields( $id ), function( $carry, $field ) { |
|
| 14 | 14 | return $carry.$field; |
| 15 | 15 | }); |
| 16 | 16 | } |
@@ -19,11 +19,11 @@ discard block |
||
| 19 | 19 | * @param string $id |
| 20 | 20 | * @return array |
| 21 | 21 | */ |
| 22 | - public function getFields($id) |
|
| 22 | + public function getFields( $id ) |
|
| 23 | 23 | { |
| 24 | 24 | $fields = []; |
| 25 | - foreach (glsr()->config('forms/'.$id) as $name => $field) { |
|
| 26 | - $fields[] = new Field(wp_parse_args($field, ['name' => $name])); |
|
| 25 | + foreach( glsr()->config( 'forms/'.$id ) as $name => $field ) { |
|
| 26 | + $fields[] = new Field( wp_parse_args( $field, ['name' => $name] ) ); |
|
| 27 | 27 | } |
| 28 | 28 | return $fields; |
| 29 | 29 | } |
@@ -32,8 +32,8 @@ discard block |
||
| 32 | 32 | * @param string $id |
| 33 | 33 | * @return void |
| 34 | 34 | */ |
| 35 | - public function renderFields($id) |
|
| 35 | + public function renderFields( $id ) |
|
| 36 | 36 | { |
| 37 | - echo $this->buildFields($id); |
|
| 37 | + echo $this->buildFields( $id ); |
|
| 38 | 38 | } |
| 39 | 39 | } |
@@ -6,17 +6,17 @@ |
||
| 6 | 6 | |
| 7 | 7 | class StarRating implements PartialContract |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @return void|string |
|
| 11 | - */ |
|
| 12 | - public function build(array $args = []) |
|
| 13 | - { |
|
| 14 | - require_once ABSPATH.'wp-admin/includes/template.php'; |
|
| 15 | - ob_start(); |
|
| 16 | - wp_star_rating($args); |
|
| 17 | - $stars = ob_get_clean(); |
|
| 18 | - return !glsr()->isAdmin() |
|
| 19 | - ? str_replace(['star-rating', 'star star'], ['glsr-stars', 'glsr-star glsr-star'], $stars) |
|
| 20 | - : $stars; |
|
| 21 | - } |
|
| 9 | + /** |
|
| 10 | + * @return void|string |
|
| 11 | + */ |
|
| 12 | + public function build(array $args = []) |
|
| 13 | + { |
|
| 14 | + require_once ABSPATH.'wp-admin/includes/template.php'; |
|
| 15 | + ob_start(); |
|
| 16 | + wp_star_rating($args); |
|
| 17 | + $stars = ob_get_clean(); |
|
| 18 | + return !glsr()->isAdmin() |
|
| 19 | + ? str_replace(['star-rating', 'star star'], ['glsr-stars', 'glsr-star glsr-star'], $stars) |
|
| 20 | + : $stars; |
|
| 21 | + } |
|
| 22 | 22 | } |
@@ -9,14 +9,14 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * @return void|string |
| 11 | 11 | */ |
| 12 | - public function build(array $args = []) |
|
| 12 | + public function build( array $args = [] ) |
|
| 13 | 13 | { |
| 14 | 14 | require_once ABSPATH.'wp-admin/includes/template.php'; |
| 15 | 15 | ob_start(); |
| 16 | - wp_star_rating($args); |
|
| 16 | + wp_star_rating( $args ); |
|
| 17 | 17 | $stars = ob_get_clean(); |
| 18 | 18 | return !glsr()->isAdmin() |
| 19 | - ? str_replace(['star-rating', 'star star'], ['glsr-stars', 'glsr-star glsr-star'], $stars) |
|
| 19 | + ? str_replace( ['star-rating', 'star star'], ['glsr-stars', 'glsr-star glsr-star'], $stars ) |
|
| 20 | 20 | : $stars; |
| 21 | 21 | } |
| 22 | 22 | } |
@@ -10,191 +10,191 @@ |
||
| 10 | 10 | |
| 11 | 11 | class SiteReviewsSummary |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - protected $args; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var float |
|
| 20 | - */ |
|
| 21 | - protected $averageRating; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $ratingCounts; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @return void|string |
|
| 30 | - */ |
|
| 31 | - public function build(array $args = []) |
|
| 32 | - { |
|
| 33 | - $this->args = $args; |
|
| 34 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
| 35 | - if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
| 36 | - return; |
|
| 37 | - } |
|
| 38 | - $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
| 39 | - $this->generateSchema(); |
|
| 40 | - return glsr(Template::class)->build('templates/reviews-summary', [ |
|
| 41 | - 'context' => [ |
|
| 42 | - 'assigned_to' => $this->args['assigned_to'], |
|
| 43 | - 'category' => $this->args['category'], |
|
| 44 | - 'class' => $this->getClass(), |
|
| 45 | - 'id' => $this->args['id'], |
|
| 46 | - 'percentages' => $this->buildPercentage(), |
|
| 47 | - 'rating' => $this->buildRating(), |
|
| 48 | - 'stars' => $this->buildStars(), |
|
| 49 | - 'text' => $this->buildText(), |
|
| 50 | - ], |
|
| 51 | - ]); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return void|string |
|
| 56 | - */ |
|
| 57 | - protected function buildPercentage() |
|
| 58 | - { |
|
| 59 | - if ($this->isHidden('bars')) { |
|
| 60 | - return; |
|
| 61 | - } |
|
| 62 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
| 63 | - $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
| 64 | - $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
| 65 | - $background = $this->buildPercentageBackground($percentages[$level]); |
|
| 66 | - $count = apply_filters('site-reviews/summary/counts', |
|
| 67 | - $percentages[$level], |
|
| 68 | - $this->ratingCounts[$level] |
|
| 69 | - ); |
|
| 70 | - $percent = $this->buildPercentageCount($count); |
|
| 71 | - return $carry.glsr(Builder::class)->div($label.$background.$percent, [ |
|
| 72 | - 'class' => 'glsr-bar', |
|
| 73 | - ]); |
|
| 74 | - }); |
|
| 75 | - return $this->wrap('percentage', $bars); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param string $percent |
|
| 80 | - * @return string |
|
| 81 | - */ |
|
| 82 | - protected function buildPercentageBackground($percent) |
|
| 83 | - { |
|
| 84 | - $backgroundPercent = glsr(Builder::class)->span([ |
|
| 85 | - 'class' => 'glsr-bar-background-percent', |
|
| 86 | - 'style' => 'width:'.$percent, |
|
| 87 | - ]); |
|
| 88 | - return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param string $count |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - protected function buildPercentageCount($count) |
|
| 96 | - { |
|
| 97 | - return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param string $label |
|
| 102 | - * @return string |
|
| 103 | - */ |
|
| 104 | - protected function buildPercentageLabel($label) |
|
| 105 | - { |
|
| 106 | - return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return void|string |
|
| 111 | - */ |
|
| 112 | - protected function buildRating() |
|
| 113 | - { |
|
| 114 | - if ($this->isHidden('rating')) { |
|
| 115 | - return; |
|
| 116 | - } |
|
| 117 | - return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return void|string |
|
| 122 | - */ |
|
| 123 | - protected function buildStars() |
|
| 124 | - { |
|
| 125 | - if ($this->isHidden('stars')) { |
|
| 126 | - return; |
|
| 127 | - } |
|
| 128 | - $stars = glsr_star_rating($this->averageRating); |
|
| 129 | - return $this->wrap('stars', $stars); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return void|string |
|
| 134 | - */ |
|
| 135 | - protected function buildText() |
|
| 136 | - { |
|
| 137 | - if ($this->isHidden('summary')) { |
|
| 138 | - return; |
|
| 139 | - } |
|
| 140 | - $count = intval(array_sum($this->ratingCounts)); |
|
| 141 | - if (empty($this->args['text'])) { |
|
| 142 | - // @todo document this change |
|
| 143 | - $this->args['text'] = _nx( |
|
| 144 | - '{rating} out of {max} stars (based on {num} review)', |
|
| 145 | - '{rating} out of {max} stars (based on {num} reviews)', |
|
| 146 | - $count, |
|
| 147 | - 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
| 148 | - 'site-reviews' |
|
| 149 | - ); |
|
| 150 | - } |
|
| 151 | - $summary = str_replace( |
|
| 152 | - ['{rating}', '{max}', '{num}'], |
|
| 153 | - [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
| 154 | - $this->args['text'] |
|
| 155 | - ); |
|
| 156 | - return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return void |
|
| 161 | - */ |
|
| 162 | - protected function generateSchema() |
|
| 163 | - { |
|
| 164 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
| 165 | - return; |
|
| 166 | - } |
|
| 167 | - glsr(Schema::class)->store( |
|
| 168 | - glsr(Schema::class)->buildSummary($this->args) |
|
| 169 | - ); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @return string |
|
| 174 | - */ |
|
| 175 | - protected function getClass() |
|
| 176 | - { |
|
| 177 | - return trim('glsr-summary glsr-default '.$this->args['class']); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param string $key |
|
| 182 | - * @return bool |
|
| 183 | - */ |
|
| 184 | - protected function isHidden($key) |
|
| 185 | - { |
|
| 186 | - return in_array($key, $this->args['hide']); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @param string $key |
|
| 191 | - * @param string $value |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - protected function wrap($key, $value) |
|
| 195 | - { |
|
| 196 | - return glsr(Builder::class)->div($value, [ |
|
| 197 | - 'class' => 'glsr-summary-'.$key, |
|
| 198 | - ]); |
|
| 199 | - } |
|
| 13 | + /** |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + protected $args; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var float |
|
| 20 | + */ |
|
| 21 | + protected $averageRating; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $ratingCounts; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @return void|string |
|
| 30 | + */ |
|
| 31 | + public function build(array $args = []) |
|
| 32 | + { |
|
| 33 | + $this->args = $args; |
|
| 34 | + $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
| 35 | + if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
| 36 | + return; |
|
| 37 | + } |
|
| 38 | + $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
| 39 | + $this->generateSchema(); |
|
| 40 | + return glsr(Template::class)->build('templates/reviews-summary', [ |
|
| 41 | + 'context' => [ |
|
| 42 | + 'assigned_to' => $this->args['assigned_to'], |
|
| 43 | + 'category' => $this->args['category'], |
|
| 44 | + 'class' => $this->getClass(), |
|
| 45 | + 'id' => $this->args['id'], |
|
| 46 | + 'percentages' => $this->buildPercentage(), |
|
| 47 | + 'rating' => $this->buildRating(), |
|
| 48 | + 'stars' => $this->buildStars(), |
|
| 49 | + 'text' => $this->buildText(), |
|
| 50 | + ], |
|
| 51 | + ]); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return void|string |
|
| 56 | + */ |
|
| 57 | + protected function buildPercentage() |
|
| 58 | + { |
|
| 59 | + if ($this->isHidden('bars')) { |
|
| 60 | + return; |
|
| 61 | + } |
|
| 62 | + $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
| 63 | + $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
| 64 | + $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
| 65 | + $background = $this->buildPercentageBackground($percentages[$level]); |
|
| 66 | + $count = apply_filters('site-reviews/summary/counts', |
|
| 67 | + $percentages[$level], |
|
| 68 | + $this->ratingCounts[$level] |
|
| 69 | + ); |
|
| 70 | + $percent = $this->buildPercentageCount($count); |
|
| 71 | + return $carry.glsr(Builder::class)->div($label.$background.$percent, [ |
|
| 72 | + 'class' => 'glsr-bar', |
|
| 73 | + ]); |
|
| 74 | + }); |
|
| 75 | + return $this->wrap('percentage', $bars); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param string $percent |
|
| 80 | + * @return string |
|
| 81 | + */ |
|
| 82 | + protected function buildPercentageBackground($percent) |
|
| 83 | + { |
|
| 84 | + $backgroundPercent = glsr(Builder::class)->span([ |
|
| 85 | + 'class' => 'glsr-bar-background-percent', |
|
| 86 | + 'style' => 'width:'.$percent, |
|
| 87 | + ]); |
|
| 88 | + return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param string $count |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + protected function buildPercentageCount($count) |
|
| 96 | + { |
|
| 97 | + return '<span class="glsr-bar-percent">'.$count.'</span>'; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param string $label |
|
| 102 | + * @return string |
|
| 103 | + */ |
|
| 104 | + protected function buildPercentageLabel($label) |
|
| 105 | + { |
|
| 106 | + return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return void|string |
|
| 111 | + */ |
|
| 112 | + protected function buildRating() |
|
| 113 | + { |
|
| 114 | + if ($this->isHidden('rating')) { |
|
| 115 | + return; |
|
| 116 | + } |
|
| 117 | + return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return void|string |
|
| 122 | + */ |
|
| 123 | + protected function buildStars() |
|
| 124 | + { |
|
| 125 | + if ($this->isHidden('stars')) { |
|
| 126 | + return; |
|
| 127 | + } |
|
| 128 | + $stars = glsr_star_rating($this->averageRating); |
|
| 129 | + return $this->wrap('stars', $stars); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @return void|string |
|
| 134 | + */ |
|
| 135 | + protected function buildText() |
|
| 136 | + { |
|
| 137 | + if ($this->isHidden('summary')) { |
|
| 138 | + return; |
|
| 139 | + } |
|
| 140 | + $count = intval(array_sum($this->ratingCounts)); |
|
| 141 | + if (empty($this->args['text'])) { |
|
| 142 | + // @todo document this change |
|
| 143 | + $this->args['text'] = _nx( |
|
| 144 | + '{rating} out of {max} stars (based on {num} review)', |
|
| 145 | + '{rating} out of {max} stars (based on {num} reviews)', |
|
| 146 | + $count, |
|
| 147 | + 'Do not translate {rating}, {max}, and {num}, they are template tags.', |
|
| 148 | + 'site-reviews' |
|
| 149 | + ); |
|
| 150 | + } |
|
| 151 | + $summary = str_replace( |
|
| 152 | + ['{rating}', '{max}', '{num}'], |
|
| 153 | + [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
| 154 | + $this->args['text'] |
|
| 155 | + ); |
|
| 156 | + return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return void |
|
| 161 | + */ |
|
| 162 | + protected function generateSchema() |
|
| 163 | + { |
|
| 164 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
| 165 | + return; |
|
| 166 | + } |
|
| 167 | + glsr(Schema::class)->store( |
|
| 168 | + glsr(Schema::class)->buildSummary($this->args) |
|
| 169 | + ); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @return string |
|
| 174 | + */ |
|
| 175 | + protected function getClass() |
|
| 176 | + { |
|
| 177 | + return trim('glsr-summary glsr-default '.$this->args['class']); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param string $key |
|
| 182 | + * @return bool |
|
| 183 | + */ |
|
| 184 | + protected function isHidden($key) |
|
| 185 | + { |
|
| 186 | + return in_array($key, $this->args['hide']); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @param string $key |
|
| 191 | + * @param string $value |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + protected function wrap($key, $value) |
|
| 195 | + { |
|
| 196 | + return glsr(Builder::class)->div($value, [ |
|
| 197 | + 'class' => 'glsr-summary-'.$key, |
|
| 198 | + ]); |
|
| 199 | + } |
|
| 200 | 200 | } |
@@ -28,16 +28,16 @@ discard block |
||
| 28 | 28 | /** |
| 29 | 29 | * @return void|string |
| 30 | 30 | */ |
| 31 | - public function build(array $args = []) |
|
| 31 | + public function build( array $args = [] ) |
|
| 32 | 32 | { |
| 33 | 33 | $this->args = $args; |
| 34 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($args); |
|
| 35 | - if (!array_sum($this->ratingCounts) && $this->isHidden('if_empty')) { |
|
| 34 | + $this->ratingCounts = glsr( ReviewManager::class )->getRatingCounts( $args ); |
|
| 35 | + if( !array_sum( $this->ratingCounts ) && $this->isHidden( 'if_empty' ) ) { |
|
| 36 | 36 | return; |
| 37 | 37 | } |
| 38 | - $this->averageRating = glsr(Rating::class)->getAverage($this->ratingCounts); |
|
| 38 | + $this->averageRating = glsr( Rating::class )->getAverage( $this->ratingCounts ); |
|
| 39 | 39 | $this->generateSchema(); |
| 40 | - return glsr(Template::class)->build('templates/reviews-summary', [ |
|
| 40 | + return glsr( Template::class )->build( 'templates/reviews-summary', [ |
|
| 41 | 41 | 'context' => [ |
| 42 | 42 | 'assigned_to' => $this->args['assigned_to'], |
| 43 | 43 | 'category' => $this->args['category'], |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | 'stars' => $this->buildStars(), |
| 49 | 49 | 'text' => $this->buildText(), |
| 50 | 50 | ], |
| 51 | - ]); |
|
| 51 | + ] ); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
@@ -56,35 +56,35 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | protected function buildPercentage() |
| 58 | 58 | { |
| 59 | - if ($this->isHidden('bars')) { |
|
| 59 | + if( $this->isHidden( 'bars' ) ) { |
|
| 60 | 60 | return; |
| 61 | 61 | } |
| 62 | - $percentages = preg_filter('/$/', '%', glsr(Rating::class)->getPercentages($this->ratingCounts)); |
|
| 63 | - $bars = array_reduce(range(glsr()->constant('MAX_RATING', Rating::class), 1), function ($carry, $level) use ($percentages) { |
|
| 64 | - $label = $this->buildPercentageLabel($this->args['labels'][$level]); |
|
| 65 | - $background = $this->buildPercentageBackground($percentages[$level]); |
|
| 66 | - $count = apply_filters('site-reviews/summary/counts', |
|
| 62 | + $percentages = preg_filter( '/$/', '%', glsr( Rating::class )->getPercentages( $this->ratingCounts ) ); |
|
| 63 | + $bars = array_reduce( range( glsr()->constant( 'MAX_RATING', Rating::class ), 1 ), function( $carry, $level ) use ($percentages) { |
|
| 64 | + $label = $this->buildPercentageLabel( $this->args['labels'][$level] ); |
|
| 65 | + $background = $this->buildPercentageBackground( $percentages[$level] ); |
|
| 66 | + $count = apply_filters( 'site-reviews/summary/counts', |
|
| 67 | 67 | $percentages[$level], |
| 68 | 68 | $this->ratingCounts[$level] |
| 69 | 69 | ); |
| 70 | - $percent = $this->buildPercentageCount($count); |
|
| 71 | - return $carry.glsr(Builder::class)->div($label.$background.$percent, [ |
|
| 70 | + $percent = $this->buildPercentageCount( $count ); |
|
| 71 | + return $carry.glsr( Builder::class )->div( $label.$background.$percent, [ |
|
| 72 | 72 | 'class' => 'glsr-bar', |
| 73 | - ]); |
|
| 73 | + ] ); |
|
| 74 | 74 | }); |
| 75 | - return $this->wrap('percentage', $bars); |
|
| 75 | + return $this->wrap( 'percentage', $bars ); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * @param string $percent |
| 80 | 80 | * @return string |
| 81 | 81 | */ |
| 82 | - protected function buildPercentageBackground($percent) |
|
| 82 | + protected function buildPercentageBackground( $percent ) |
|
| 83 | 83 | { |
| 84 | - $backgroundPercent = glsr(Builder::class)->span([ |
|
| 84 | + $backgroundPercent = glsr( Builder::class )->span( [ |
|
| 85 | 85 | 'class' => 'glsr-bar-background-percent', |
| 86 | 86 | 'style' => 'width:'.$percent, |
| 87 | - ]); |
|
| 87 | + ] ); |
|
| 88 | 88 | return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
| 89 | 89 | } |
| 90 | 90 | |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * @param string $count |
| 93 | 93 | * @return string |
| 94 | 94 | */ |
| 95 | - protected function buildPercentageCount($count) |
|
| 95 | + protected function buildPercentageCount( $count ) |
|
| 96 | 96 | { |
| 97 | 97 | return '<span class="glsr-bar-percent">'.$count.'</span>'; |
| 98 | 98 | } |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | * @param string $label |
| 102 | 102 | * @return string |
| 103 | 103 | */ |
| 104 | - protected function buildPercentageLabel($label) |
|
| 104 | + protected function buildPercentageLabel( $label ) |
|
| 105 | 105 | { |
| 106 | 106 | return '<span class="glsr-bar-label">'.$label.'</span>'; |
| 107 | 107 | } |
@@ -111,10 +111,10 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | protected function buildRating() |
| 113 | 113 | { |
| 114 | - if ($this->isHidden('rating')) { |
|
| 114 | + if( $this->isHidden( 'rating' ) ) { |
|
| 115 | 115 | return; |
| 116 | 116 | } |
| 117 | - return $this->wrap('rating', '<span>'.$this->averageRating.'</span>'); |
|
| 117 | + return $this->wrap( 'rating', '<span>'.$this->averageRating.'</span>' ); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -122,11 +122,11 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | protected function buildStars() |
| 124 | 124 | { |
| 125 | - if ($this->isHidden('stars')) { |
|
| 125 | + if( $this->isHidden( 'stars' ) ) { |
|
| 126 | 126 | return; |
| 127 | 127 | } |
| 128 | - $stars = glsr_star_rating($this->averageRating); |
|
| 129 | - return $this->wrap('stars', $stars); |
|
| 128 | + $stars = glsr_star_rating( $this->averageRating ); |
|
| 129 | + return $this->wrap( 'stars', $stars ); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -134,11 +134,11 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | protected function buildText() |
| 136 | 136 | { |
| 137 | - if ($this->isHidden('summary')) { |
|
| 137 | + if( $this->isHidden( 'summary' ) ) { |
|
| 138 | 138 | return; |
| 139 | 139 | } |
| 140 | - $count = intval(array_sum($this->ratingCounts)); |
|
| 141 | - if (empty($this->args['text'])) { |
|
| 140 | + $count = intval( array_sum( $this->ratingCounts ) ); |
|
| 141 | + if( empty($this->args['text']) ) { |
|
| 142 | 142 | // @todo document this change |
| 143 | 143 | $this->args['text'] = _nx( |
| 144 | 144 | '{rating} out of {max} stars (based on {num} review)', |
@@ -150,10 +150,10 @@ discard block |
||
| 150 | 150 | } |
| 151 | 151 | $summary = str_replace( |
| 152 | 152 | ['{rating}', '{max}', '{num}'], |
| 153 | - [$this->averageRating, glsr()->constant('MAX_RATING', Rating::class), $count], |
|
| 153 | + [$this->averageRating, glsr()->constant( 'MAX_RATING', Rating::class ), $count], |
|
| 154 | 154 | $this->args['text'] |
| 155 | 155 | ); |
| 156 | - return $this->wrap('text', '<span>'.$summary.'</span>'); |
|
| 156 | + return $this->wrap( 'text', '<span>'.$summary.'</span>' ); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -161,11 +161,11 @@ discard block |
||
| 161 | 161 | */ |
| 162 | 162 | protected function generateSchema() |
| 163 | 163 | { |
| 164 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
| 164 | + if( !wp_validate_boolean( $this->args['schema'] ) ) { |
|
| 165 | 165 | return; |
| 166 | 166 | } |
| 167 | - glsr(Schema::class)->store( |
|
| 168 | - glsr(Schema::class)->buildSummary($this->args) |
|
| 167 | + glsr( Schema::class )->store( |
|
| 168 | + glsr( Schema::class )->buildSummary( $this->args ) |
|
| 169 | 169 | ); |
| 170 | 170 | } |
| 171 | 171 | |
@@ -174,16 +174,16 @@ discard block |
||
| 174 | 174 | */ |
| 175 | 175 | protected function getClass() |
| 176 | 176 | { |
| 177 | - return trim('glsr-summary glsr-default '.$this->args['class']); |
|
| 177 | + return trim( 'glsr-summary glsr-default '.$this->args['class'] ); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
| 181 | 181 | * @param string $key |
| 182 | 182 | * @return bool |
| 183 | 183 | */ |
| 184 | - protected function isHidden($key) |
|
| 184 | + protected function isHidden( $key ) |
|
| 185 | 185 | { |
| 186 | - return in_array($key, $this->args['hide']); |
|
| 186 | + return in_array( $key, $this->args['hide'] ); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | /** |
@@ -191,10 +191,10 @@ discard block |
||
| 191 | 191 | * @param string $value |
| 192 | 192 | * @return string |
| 193 | 193 | */ |
| 194 | - protected function wrap($key, $value) |
|
| 194 | + protected function wrap( $key, $value ) |
|
| 195 | 195 | { |
| 196 | - return glsr(Builder::class)->div($value, [ |
|
| 196 | + return glsr( Builder::class )->div( $value, [ |
|
| 197 | 197 | 'class' => 'glsr-summary-'.$key, |
| 198 | - ]); |
|
| 198 | + ] ); |
|
| 199 | 199 | } |
| 200 | 200 | } |
@@ -302,7 +302,8 @@ |
||
| 302 | 302 | } |
| 303 | 303 | if (in_array($field->field['type'], ['radio', 'checkbox'])) { |
| 304 | 304 | $field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
| 305 | - } else { |
|
| 305 | + } |
|
| 306 | + else { |
|
| 306 | 307 | $field->field['value'] = $this->values[$field->field['path']]; |
| 307 | 308 | } |
| 308 | 309 | } |
@@ -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 | } |
@@ -40,20 +40,20 @@ discard block |
||
| 40 | 40 | /** |
| 41 | 41 | * @return void|string |
| 42 | 42 | */ |
| 43 | - public function build(array $args = []) |
|
| 43 | + public function build( array $args = [] ) |
|
| 44 | 44 | { |
| 45 | 45 | $this->args = $args; |
| 46 | - if (!is_user_logged_in() && glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
| 46 | + if( !is_user_logged_in() && glsr( OptionManager::class )->getBool( 'settings.general.require.login' ) ) { |
|
| 47 | 47 | return $this->buildLoginRegister(); |
| 48 | 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) { |
|
| 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 | 54 | return $carry.$field; |
| 55 | 55 | }); |
| 56 | - return glsr(Template::class)->build('templates/reviews-form', [ |
|
| 56 | + return glsr( Template::class )->build( 'templates/reviews-form', [ |
|
| 57 | 57 | 'args' => $args, |
| 58 | 58 | 'context' => [ |
| 59 | 59 | 'class' => $this->getClass(), |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | 'response' => $this->buildResponse(), |
| 63 | 63 | 'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(), |
| 64 | 64 | ], |
| 65 | - ]); |
|
| 65 | + ] ); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -70,11 +70,11 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | protected function buildLoginRegister() |
| 72 | 72 | { |
| 73 | - return glsr(Template::class)->build('templates/login-register', [ |
|
| 73 | + return glsr( Template::class )->build( 'templates/login-register', [ |
|
| 74 | 74 | 'context' => [ |
| 75 | - 'text' => trim($this->getLoginText().' '.$this->getRegisterText()), |
|
| 75 | + 'text' => trim( $this->getLoginText().' '.$this->getRegisterText() ), |
|
| 76 | 76 | ], |
| 77 | - ]); |
|
| 77 | + ] ); |
|
| 78 | 78 | } |
| 79 | 79 | |
| 80 | 80 | /** |
@@ -82,15 +82,15 @@ discard block |
||
| 82 | 82 | */ |
| 83 | 83 | protected function buildRecaptcha() |
| 84 | 84 | { |
| 85 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
| 85 | + if( !glsr( OptionManager::class )->isRecaptchaEnabled() ) { |
|
| 86 | 86 | return; |
| 87 | 87 | } |
| 88 | - return glsr(Builder::class)->div([ |
|
| 88 | + return glsr( Builder::class )->div( [ |
|
| 89 | 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')), |
|
| 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 | 92 | 'data-size' => 'invisible', |
| 93 | - ]); |
|
| 93 | + ] ); |
|
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | /** |
@@ -99,15 +99,15 @@ discard block |
||
| 99 | 99 | protected function buildResponse() |
| 100 | 100 | { |
| 101 | 101 | $classes = !empty($this->errors) |
| 102 | - ? glsr(StyleValidationDefaults::class)->defaults()['message_error_class'] |
|
| 102 | + ? glsr( StyleValidationDefaults::class )->defaults()['message_error_class'] |
|
| 103 | 103 | : ''; |
| 104 | - return glsr(Template::class)->build('templates/form/response', [ |
|
| 104 | + return glsr( Template::class )->build( 'templates/form/response', [ |
|
| 105 | 105 | 'context' => [ |
| 106 | 106 | 'class' => $classes, |
| 107 | - 'message' => wpautop($this->message), |
|
| 107 | + 'message' => wpautop( $this->message ), |
|
| 108 | 108 | ], |
| 109 | 109 | 'has_errors' => !empty($this->errors), |
| 110 | - ]); |
|
| 110 | + ] ); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
@@ -115,11 +115,11 @@ discard block |
||
| 115 | 115 | */ |
| 116 | 116 | protected function buildSubmitButton() |
| 117 | 117 | { |
| 118 | - return glsr(Template::class)->build('templates/form/submit-button', [ |
|
| 118 | + return glsr( Template::class )->build( 'templates/form/submit-button', [ |
|
| 119 | 119 | 'context' => [ |
| 120 | - 'text' => __('Submit your review', 'site-reviews'), |
|
| 120 | + 'text' => __( 'Submit your review', 'site-reviews' ), |
|
| 121 | 121 | ], |
| 122 | - ]); |
|
| 122 | + ] ); |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /** |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | */ |
| 128 | 128 | protected function getClass() |
| 129 | 129 | { |
| 130 | - return trim('glsr-form glsr-'.glsr(Style::class)->get().' '.$this->args['class']); |
|
| 130 | + return trim( 'glsr-form glsr-'.glsr( Style::class )->get().' '.$this->args['class'] ); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -137,18 +137,18 @@ discard block |
||
| 137 | 137 | { |
| 138 | 138 | $hiddenFields = $this->getHiddenFields(); |
| 139 | 139 | $hiddenFields[] = $this->getHoneypotField(); |
| 140 | - $fields = $this->normalizeFields(glsr(Form::class)->getFields('submission-form')); |
|
| 141 | - $paths = array_map(function ($obj) { |
|
| 140 | + $fields = $this->normalizeFields( glsr( Form::class )->getFields( 'submission-form' ) ); |
|
| 141 | + $paths = array_map( function( $obj ) { |
|
| 142 | 142 | return $obj->field['path']; |
| 143 | - }, $hiddenFields); |
|
| 144 | - foreach ($fields as $field) { |
|
| 145 | - $index = array_search($field->field['path'], $paths); |
|
| 146 | - if (false === $index) { |
|
| 143 | + }, $hiddenFields ); |
|
| 144 | + foreach( $fields as $field ) { |
|
| 145 | + $index = array_search( $field->field['path'], $paths ); |
|
| 146 | + if( false === $index ) { |
|
| 147 | 147 | continue; |
| 148 | 148 | } |
| 149 | 149 | unset($hiddenFields[$index]); |
| 150 | 150 | } |
| 151 | - return array_merge($hiddenFields, $fields); |
|
| 151 | + return array_merge( $hiddenFields, $fields ); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -156,11 +156,11 @@ discard block |
||
| 156 | 156 | */ |
| 157 | 157 | protected function getLoginText() |
| 158 | 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); |
|
| 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 | 164 | } |
| 165 | 165 | |
| 166 | 166 | /** |
@@ -168,14 +168,14 @@ discard block |
||
| 168 | 168 | */ |
| 169 | 169 | protected function getRegisterText() |
| 170 | 170 | { |
| 171 | - if (!get_option('users_can_register') || !glsr(OptionManager::class)->getBool('settings.general.require.login')) { |
|
| 171 | + if( !get_option( 'users_can_register' ) || !glsr( OptionManager::class )->getBool( 'settings.general.require.login' ) ) { |
|
| 172 | 172 | return; |
| 173 | 173 | } |
| 174 | - $registerLink = glsr(Builder::class)->a([ |
|
| 174 | + $registerLink = glsr( Builder::class )->a( [ |
|
| 175 | 175 | 'href' => wp_registration_url(), |
| 176 | - 'text' => __('register', 'site-reviews'), |
|
| 177 | - ]); |
|
| 178 | - return sprintf(__('You may also %s for an account.', 'site-reviews'), $registerLink); |
|
| 176 | + 'text' => __( 'register', 'site-reviews' ), |
|
| 177 | + ] ); |
|
| 178 | + return sprintf( __( 'You may also %s for an account.', 'site-reviews' ), $registerLink ); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | /** |
@@ -190,13 +190,13 @@ discard block |
||
| 190 | 190 | 'name' => '_counter', |
| 191 | 191 | ], [ |
| 192 | 192 | 'name' => '_nonce', |
| 193 | - 'value' => wp_create_nonce('submit-review'), |
|
| 193 | + 'value' => wp_create_nonce( 'submit-review' ), |
|
| 194 | 194 | ], [ |
| 195 | 195 | 'name' => '_post_id', |
| 196 | 196 | 'value' => get_the_ID(), |
| 197 | 197 | ], [ |
| 198 | 198 | 'name' => '_referer', |
| 199 | - 'value' => wp_unslash(filter_input(INPUT_SERVER, 'REQUEST_URI')), |
|
| 199 | + 'value' => wp_unslash( filter_input( INPUT_SERVER, 'REQUEST_URI' ) ), |
|
| 200 | 200 | ], [ |
| 201 | 201 | 'name' => 'assign_to', |
| 202 | 202 | 'value' => $this->args['assign_to'], |
@@ -210,9 +210,9 @@ discard block |
||
| 210 | 210 | 'name' => 'form_id', |
| 211 | 211 | 'value' => $this->args['id'], |
| 212 | 212 | ]]; |
| 213 | - return array_map(function ($field) { |
|
| 214 | - return new Field(wp_parse_args($field, ['type' => 'hidden'])); |
|
| 215 | - }, $fields); |
|
| 213 | + return array_map( function( $field ) { |
|
| 214 | + return new Field( wp_parse_args( $field, ['type' => 'hidden'] ) ); |
|
| 215 | + }, $fields ); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
@@ -220,40 +220,40 @@ discard block |
||
| 220 | 220 | */ |
| 221 | 221 | protected function getHoneypotField() |
| 222 | 222 | { |
| 223 | - return new Field([ |
|
| 223 | + return new Field( [ |
|
| 224 | 224 | 'name' => 'gotcha', |
| 225 | 225 | 'type' => 'honeypot', |
| 226 | - ]); |
|
| 226 | + ] ); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | /** |
| 230 | 230 | * @return void |
| 231 | 231 | */ |
| 232 | - protected function normalizeFieldId(Field &$field) |
|
| 232 | + protected function normalizeFieldId( Field &$field ) |
|
| 233 | 233 | { |
| 234 | - if (empty($this->args['id']) || empty($field->field['id'])) { |
|
| 234 | + if( empty($this->args['id']) || empty($field->field['id']) ) { |
|
| 235 | 235 | return; |
| 236 | 236 | } |
| 237 | - $field->field['id'].= '-'.$this->args['id']; |
|
| 237 | + $field->field['id'] .= '-'.$this->args['id']; |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
| 241 | 241 | * @return void |
| 242 | 242 | */ |
| 243 | - protected function normalizeFieldClass(Field &$field) |
|
| 243 | + protected function normalizeFieldClass( Field &$field ) |
|
| 244 | 244 | { |
| 245 | - if (!isset($field->field['class'])) { |
|
| 245 | + if( !isset($field->field['class']) ) { |
|
| 246 | 246 | $field->field['class'] = ''; |
| 247 | 247 | } |
| 248 | - $field->field['class'] = trim($field->field['class'].' glsr-field-control'); |
|
| 248 | + $field->field['class'] = trim( $field->field['class'].' glsr-field-control' ); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | /** |
| 252 | 252 | * @return void |
| 253 | 253 | */ |
| 254 | - protected function normalizeFieldErrors(Field &$field) |
|
| 254 | + protected function normalizeFieldErrors( Field &$field ) |
|
| 255 | 255 | { |
| 256 | - if (!array_key_exists($field->field['path'], $this->errors)) { |
|
| 256 | + if( !array_key_exists( $field->field['path'], $this->errors ) ) { |
|
| 257 | 257 | return; |
| 258 | 258 | } |
| 259 | 259 | $field->field['errors'] = $this->errors[$field->field['path']]; |
@@ -262,9 +262,9 @@ discard block |
||
| 262 | 262 | /** |
| 263 | 263 | * @return void |
| 264 | 264 | */ |
| 265 | - protected function normalizeFieldRequired(Field &$field) |
|
| 265 | + protected function normalizeFieldRequired( Field &$field ) |
|
| 266 | 266 | { |
| 267 | - if (!in_array($field->field['path'], $this->required)) { |
|
| 267 | + if( !in_array( $field->field['path'], $this->required ) ) { |
|
| 268 | 268 | return; |
| 269 | 269 | } |
| 270 | 270 | $field->field['required'] = true; |
@@ -273,19 +273,19 @@ discard block |
||
| 273 | 273 | /** |
| 274 | 274 | * @return array |
| 275 | 275 | */ |
| 276 | - protected function normalizeFields($fields) |
|
| 276 | + protected function normalizeFields( $fields ) |
|
| 277 | 277 | { |
| 278 | 278 | $normalizedFields = []; |
| 279 | - foreach ($fields as $field) { |
|
| 280 | - if (in_array($field->field['path'], $this->args['hide'])) { |
|
| 279 | + foreach( $fields as $field ) { |
|
| 280 | + if( in_array( $field->field['path'], $this->args['hide'] ) ) { |
|
| 281 | 281 | continue; |
| 282 | 282 | } |
| 283 | 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); |
|
| 284 | + $this->normalizeFieldClass( $field ); |
|
| 285 | + $this->normalizeFieldErrors( $field ); |
|
| 286 | + $this->normalizeFieldRequired( $field ); |
|
| 287 | + $this->normalizeFieldValue( $field ); |
|
| 288 | + $this->normalizeFieldId( $field ); |
|
| 289 | 289 | $normalizedFields[] = $field; |
| 290 | 290 | } |
| 291 | 291 | return $normalizedFields; |
@@ -294,12 +294,12 @@ discard block |
||
| 294 | 294 | /** |
| 295 | 295 | * @return void |
| 296 | 296 | */ |
| 297 | - protected function normalizeFieldValue(Field &$field) |
|
| 297 | + protected function normalizeFieldValue( Field &$field ) |
|
| 298 | 298 | { |
| 299 | - if (!array_key_exists($field->field['path'], $this->values)) { |
|
| 299 | + if( !array_key_exists( $field->field['path'], $this->values ) ) { |
|
| 300 | 300 | return; |
| 301 | 301 | } |
| 302 | - if (in_array($field->field['type'], ['radio', 'checkbox'])) { |
|
| 302 | + if( in_array( $field->field['type'], ['radio', 'checkbox'] ) ) { |
|
| 303 | 303 | $field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']]; |
| 304 | 304 | } else { |
| 305 | 305 | $field->field['value'] = $this->values[$field->field['path']]; |
@@ -73,7 +73,8 @@ discard block |
||
| 73 | 73 | foreach ($potEntries as $key => $entry) { |
| 74 | 74 | $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
| 75 | 75 | } |
| 76 | - } catch (Exception $e) { |
|
| 76 | + } |
|
| 77 | + catch (Exception $e) { |
|
| 77 | 78 | glsr_log()->error($e->getMessage()); |
| 78 | 79 | } |
| 79 | 80 | return $entries; |
@@ -196,7 +197,8 @@ discard block |
||
| 196 | 197 | if (in_array($needle, [$single, $plural])) { |
| 197 | 198 | $this->results[$key] = $entry; |
| 198 | 199 | } |
| 199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 200 | + } |
|
| 201 | + elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 200 | 202 | $this->results[$key] = $entry; |
| 201 | 203 | } |
| 202 | 204 | } |
@@ -242,7 +244,8 @@ discard block |
||
| 242 | 244 | foreach ($keys as $key) { |
| 243 | 245 | try { |
| 244 | 246 | $entry = $this->normalizeEntryString($entry, $key); |
| 245 | - } catch (\TypeError $error) { |
|
| 247 | + } |
|
| 248 | + catch (\TypeError $error) { |
|
| 246 | 249 | glsr_log()->once('error', 'Translation/normalize', $error); |
| 247 | 250 | glsr_log()->once('debug', 'Translation/normalize', $entry); |
| 248 | 251 | } |
@@ -10,272 +10,272 @@ |
||
| 10 | 10 | |
| 11 | 11 | class Translation |
| 12 | 12 | { |
| 13 | - const SEARCH_THRESHOLD = 3; |
|
| 13 | + const SEARCH_THRESHOLD = 3; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $entries; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $entries; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $results; |
|
| 20 | + /** |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $results; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Returns all saved custom translations with translation context. |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function all() |
|
| 30 | - { |
|
| 31 | - $translations = $this->translations(); |
|
| 32 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
| 33 | - array_walk($translations, function (&$entry) use ($entries) { |
|
| 34 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 35 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 36 | - : ''; |
|
| 37 | - }); |
|
| 38 | - return $translations; |
|
| 39 | - } |
|
| 25 | + /** |
|
| 26 | + * Returns all saved custom translations with translation context. |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function all() |
|
| 30 | + { |
|
| 31 | + $translations = $this->translations(); |
|
| 32 | + $entries = $this->filter($translations, $this->entries())->results(); |
|
| 33 | + array_walk($translations, function (&$entry) use ($entries) { |
|
| 34 | + $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 35 | + ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 36 | + : ''; |
|
| 37 | + }); |
|
| 38 | + return $translations; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return array |
|
| 43 | - */ |
|
| 44 | - public function entries() |
|
| 45 | - { |
|
| 46 | - if (!isset($this->entries)) { |
|
| 47 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 48 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 49 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
| 50 | - $this->entries = $entries; |
|
| 51 | - } |
|
| 52 | - return $this->entries; |
|
| 53 | - } |
|
| 41 | + /** |
|
| 42 | + * @return array |
|
| 43 | + */ |
|
| 44 | + public function entries() |
|
| 45 | + { |
|
| 46 | + if (!isset($this->entries)) { |
|
| 47 | + $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 48 | + $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 49 | + $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
| 50 | + $this->entries = $entries; |
|
| 51 | + } |
|
| 52 | + return $this->entries; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @param array|null $entriesToExclude |
|
| 57 | - * @param array|null $entries |
|
| 58 | - * @return static |
|
| 59 | - */ |
|
| 60 | - public function exclude($entriesToExclude = null, $entries = null) |
|
| 61 | - { |
|
| 62 | - return $this->filter($entriesToExclude, $entries, false); |
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * @param array|null $entriesToExclude |
|
| 57 | + * @param array|null $entries |
|
| 58 | + * @return static |
|
| 59 | + */ |
|
| 60 | + public function exclude($entriesToExclude = null, $entries = null) |
|
| 61 | + { |
|
| 62 | + return $this->filter($entriesToExclude, $entries, false); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @param string $potFile |
|
| 67 | - * @return array |
|
| 68 | - */ |
|
| 69 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 70 | - { |
|
| 71 | - try { |
|
| 72 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 73 | - foreach ($potEntries as $key => $entry) { |
|
| 74 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 75 | - } |
|
| 76 | - } catch (Exception $e) { |
|
| 77 | - glsr_log()->error($e->getMessage()); |
|
| 78 | - } |
|
| 79 | - return $entries; |
|
| 80 | - } |
|
| 65 | + /** |
|
| 66 | + * @param string $potFile |
|
| 67 | + * @return array |
|
| 68 | + */ |
|
| 69 | + public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 70 | + { |
|
| 71 | + try { |
|
| 72 | + $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 73 | + foreach ($potEntries as $key => $entry) { |
|
| 74 | + $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 75 | + } |
|
| 76 | + } catch (Exception $e) { |
|
| 77 | + glsr_log()->error($e->getMessage()); |
|
| 78 | + } |
|
| 79 | + return $entries; |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - /** |
|
| 83 | - * @param array|null $filterWith |
|
| 84 | - * @param array|null $entries |
|
| 85 | - * @param bool $intersect |
|
| 86 | - * @return static |
|
| 87 | - */ |
|
| 88 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 89 | - { |
|
| 90 | - if (!is_array($entries)) { |
|
| 91 | - $entries = $this->results; |
|
| 92 | - } |
|
| 93 | - if (!is_array($filterWith)) { |
|
| 94 | - $filterWith = $this->translations(); |
|
| 95 | - } |
|
| 96 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
| 97 | - $this->results = $intersect |
|
| 98 | - ? array_intersect_key($entries, $keys) |
|
| 99 | - : array_diff_key($entries, $keys); |
|
| 100 | - return $this; |
|
| 101 | - } |
|
| 82 | + /** |
|
| 83 | + * @param array|null $filterWith |
|
| 84 | + * @param array|null $entries |
|
| 85 | + * @param bool $intersect |
|
| 86 | + * @return static |
|
| 87 | + */ |
|
| 88 | + public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 89 | + { |
|
| 90 | + if (!is_array($entries)) { |
|
| 91 | + $entries = $this->results; |
|
| 92 | + } |
|
| 93 | + if (!is_array($filterWith)) { |
|
| 94 | + $filterWith = $this->translations(); |
|
| 95 | + } |
|
| 96 | + $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
| 97 | + $this->results = $intersect |
|
| 98 | + ? array_intersect_key($entries, $keys) |
|
| 99 | + : array_diff_key($entries, $keys); |
|
| 100 | + return $this; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @param string $template |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - public function render($template, array $entry) |
|
| 108 | - { |
|
| 109 | - $data = array_combine( |
|
| 110 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 111 | - $entry |
|
| 112 | - ); |
|
| 113 | - $data['data.class'] = $data['data.error'] = ''; |
|
| 114 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
| 115 | - $data['data.class'] = 'is-invalid'; |
|
| 116 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
| 117 | - } |
|
| 118 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 119 | - 'context' => array_map('esc_html', $data), |
|
| 120 | - ]); |
|
| 121 | - } |
|
| 103 | + /** |
|
| 104 | + * @param string $template |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + public function render($template, array $entry) |
|
| 108 | + { |
|
| 109 | + $data = array_combine( |
|
| 110 | + array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 111 | + $entry |
|
| 112 | + ); |
|
| 113 | + $data['data.class'] = $data['data.error'] = ''; |
|
| 114 | + if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
| 115 | + $data['data.class'] = 'is-invalid'; |
|
| 116 | + $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
| 117 | + } |
|
| 118 | + return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 119 | + 'context' => array_map('esc_html', $data), |
|
| 120 | + ]); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Returns a rendered string of all saved custom translations with translation context. |
|
| 125 | - * @return string |
|
| 126 | - */ |
|
| 127 | - public function renderAll() |
|
| 128 | - { |
|
| 129 | - $rendered = ''; |
|
| 130 | - foreach ($this->all() as $index => $entry) { |
|
| 131 | - $entry['index'] = $index; |
|
| 132 | - $entry['prefix'] = OptionManager::databaseKey(); |
|
| 133 | - $rendered.= $this->render($entry['type'], $entry); |
|
| 134 | - } |
|
| 135 | - return $rendered; |
|
| 136 | - } |
|
| 123 | + /** |
|
| 124 | + * Returns a rendered string of all saved custom translations with translation context. |
|
| 125 | + * @return string |
|
| 126 | + */ |
|
| 127 | + public function renderAll() |
|
| 128 | + { |
|
| 129 | + $rendered = ''; |
|
| 130 | + foreach ($this->all() as $index => $entry) { |
|
| 131 | + $entry['index'] = $index; |
|
| 132 | + $entry['prefix'] = OptionManager::databaseKey(); |
|
| 133 | + $rendered.= $this->render($entry['type'], $entry); |
|
| 134 | + } |
|
| 135 | + return $rendered; |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * @param bool $resetAfterRender |
|
| 140 | - * @return string |
|
| 141 | - */ |
|
| 142 | - public function renderResults($resetAfterRender = true) |
|
| 143 | - { |
|
| 144 | - $rendered = ''; |
|
| 145 | - foreach ($this->results as $id => $entry) { |
|
| 146 | - $data = [ |
|
| 147 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 148 | - 'id' => $id, |
|
| 149 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 150 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 151 | - ]; |
|
| 152 | - $text = !empty($data['p1']) |
|
| 153 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 154 | - : $data['s1']; |
|
| 155 | - $rendered.= $this->render('result', [ |
|
| 156 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 157 | - 'text' => wp_strip_all_tags($text), |
|
| 158 | - ]); |
|
| 159 | - } |
|
| 160 | - if ($resetAfterRender) { |
|
| 161 | - $this->reset(); |
|
| 162 | - } |
|
| 163 | - return $rendered; |
|
| 164 | - } |
|
| 138 | + /** |
|
| 139 | + * @param bool $resetAfterRender |
|
| 140 | + * @return string |
|
| 141 | + */ |
|
| 142 | + public function renderResults($resetAfterRender = true) |
|
| 143 | + { |
|
| 144 | + $rendered = ''; |
|
| 145 | + foreach ($this->results as $id => $entry) { |
|
| 146 | + $data = [ |
|
| 147 | + 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 148 | + 'id' => $id, |
|
| 149 | + 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 150 | + 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 151 | + ]; |
|
| 152 | + $text = !empty($data['p1']) |
|
| 153 | + ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 154 | + : $data['s1']; |
|
| 155 | + $rendered.= $this->render('result', [ |
|
| 156 | + 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 157 | + 'text' => wp_strip_all_tags($text), |
|
| 158 | + ]); |
|
| 159 | + } |
|
| 160 | + if ($resetAfterRender) { |
|
| 161 | + $this->reset(); |
|
| 162 | + } |
|
| 163 | + return $rendered; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * @return void |
|
| 168 | - */ |
|
| 169 | - public function reset() |
|
| 170 | - { |
|
| 171 | - $this->results = []; |
|
| 172 | - } |
|
| 166 | + /** |
|
| 167 | + * @return void |
|
| 168 | + */ |
|
| 169 | + public function reset() |
|
| 170 | + { |
|
| 171 | + $this->results = []; |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * @return array |
|
| 176 | - */ |
|
| 177 | - public function results() |
|
| 178 | - { |
|
| 179 | - $results = $this->results; |
|
| 180 | - $this->reset(); |
|
| 181 | - return $results; |
|
| 182 | - } |
|
| 174 | + /** |
|
| 175 | + * @return array |
|
| 176 | + */ |
|
| 177 | + public function results() |
|
| 178 | + { |
|
| 179 | + $results = $this->results; |
|
| 180 | + $this->reset(); |
|
| 181 | + return $results; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @param string $needle |
|
| 186 | - * @return static |
|
| 187 | - */ |
|
| 188 | - public function search($needle = '') |
|
| 189 | - { |
|
| 190 | - $this->reset(); |
|
| 191 | - $needle = trim(strtolower($needle)); |
|
| 192 | - foreach ($this->entries() as $key => $entry) { |
|
| 193 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 194 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 195 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 196 | - if (in_array($needle, [$single, $plural])) { |
|
| 197 | - $this->results[$key] = $entry; |
|
| 198 | - } |
|
| 199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 200 | - $this->results[$key] = $entry; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - return $this; |
|
| 204 | - } |
|
| 184 | + /** |
|
| 185 | + * @param string $needle |
|
| 186 | + * @return static |
|
| 187 | + */ |
|
| 188 | + public function search($needle = '') |
|
| 189 | + { |
|
| 190 | + $this->reset(); |
|
| 191 | + $needle = trim(strtolower($needle)); |
|
| 192 | + foreach ($this->entries() as $key => $entry) { |
|
| 193 | + $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 194 | + $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 195 | + if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 196 | + if (in_array($needle, [$single, $plural])) { |
|
| 197 | + $this->results[$key] = $entry; |
|
| 198 | + } |
|
| 199 | + } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 200 | + $this->results[$key] = $entry; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + return $this; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Store the translations to avoid unnecessary loops. |
|
| 208 | - * @return array |
|
| 209 | - */ |
|
| 210 | - public function translations() |
|
| 211 | - { |
|
| 212 | - static $translations; |
|
| 213 | - if (empty($translations)) { |
|
| 214 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
| 215 | - $translations = isset($settings['strings']) |
|
| 216 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
| 217 | - : []; |
|
| 218 | - } |
|
| 219 | - return $translations; |
|
| 220 | - } |
|
| 206 | + /** |
|
| 207 | + * Store the translations to avoid unnecessary loops. |
|
| 208 | + * @return array |
|
| 209 | + */ |
|
| 210 | + public function translations() |
|
| 211 | + { |
|
| 212 | + static $translations; |
|
| 213 | + if (empty($translations)) { |
|
| 214 | + $settings = glsr(OptionManager::class)->get('settings'); |
|
| 215 | + $translations = isset($settings['strings']) |
|
| 216 | + ? $this->normalizeSettings((array) $settings['strings']) |
|
| 217 | + : []; |
|
| 218 | + } |
|
| 219 | + return $translations; |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - /** |
|
| 223 | - * @param string $key |
|
| 224 | - * @return string |
|
| 225 | - */ |
|
| 226 | - protected function getEntryString(array $entry, $key) |
|
| 227 | - { |
|
| 228 | - return isset($entry[$key]) |
|
| 229 | - ? implode('', (array) $entry[$key]) |
|
| 230 | - : ''; |
|
| 231 | - } |
|
| 222 | + /** |
|
| 223 | + * @param string $key |
|
| 224 | + * @return string |
|
| 225 | + */ |
|
| 226 | + protected function getEntryString(array $entry, $key) |
|
| 227 | + { |
|
| 228 | + return isset($entry[$key]) |
|
| 229 | + ? implode('', (array) $entry[$key]) |
|
| 230 | + : ''; |
|
| 231 | + } |
|
| 232 | 232 | |
| 233 | - /** |
|
| 234 | - * @return array |
|
| 235 | - */ |
|
| 236 | - protected function normalize(array $entries) |
|
| 237 | - { |
|
| 238 | - $keys = [ |
|
| 239 | - 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
| 240 | - ]; |
|
| 241 | - array_walk($entries, function (&$entry) use ($keys) { |
|
| 242 | - foreach ($keys as $key) { |
|
| 243 | - try { |
|
| 244 | - $entry = $this->normalizeEntryString($entry, $key); |
|
| 245 | - } catch (\TypeError $error) { |
|
| 246 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 247 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 248 | - } |
|
| 249 | - } |
|
| 250 | - }); |
|
| 251 | - return $entries; |
|
| 252 | - } |
|
| 233 | + /** |
|
| 234 | + * @return array |
|
| 235 | + */ |
|
| 236 | + protected function normalize(array $entries) |
|
| 237 | + { |
|
| 238 | + $keys = [ |
|
| 239 | + 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
|
| 240 | + ]; |
|
| 241 | + array_walk($entries, function (&$entry) use ($keys) { |
|
| 242 | + foreach ($keys as $key) { |
|
| 243 | + try { |
|
| 244 | + $entry = $this->normalizeEntryString($entry, $key); |
|
| 245 | + } catch (\TypeError $error) { |
|
| 246 | + glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 247 | + glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 248 | + } |
|
| 249 | + } |
|
| 250 | + }); |
|
| 251 | + return $entries; |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * @param string $key |
|
| 256 | - * @return array |
|
| 257 | - */ |
|
| 258 | - protected function normalizeEntryString(array $entry, $key) |
|
| 259 | - { |
|
| 260 | - if (isset($entry[$key])) { |
|
| 261 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
| 262 | - } |
|
| 263 | - return $entry; |
|
| 264 | - } |
|
| 254 | + /** |
|
| 255 | + * @param string $key |
|
| 256 | + * @return array |
|
| 257 | + */ |
|
| 258 | + protected function normalizeEntryString(array $entry, $key) |
|
| 259 | + { |
|
| 260 | + if (isset($entry[$key])) { |
|
| 261 | + $entry[$key] = $this->getEntryString($entry, $key); |
|
| 262 | + } |
|
| 263 | + return $entry; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * @return array |
|
| 268 | - */ |
|
| 269 | - protected function normalizeSettings(array $strings) |
|
| 270 | - { |
|
| 271 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 272 | - $strings = array_filter($strings, 'is_array'); |
|
| 273 | - foreach ($strings as &$string) { |
|
| 274 | - $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
| 275 | - $string = wp_parse_args($string, $defaultString); |
|
| 276 | - } |
|
| 277 | - return array_filter($strings, function ($string) { |
|
| 278 | - return !empty($string['id']); |
|
| 279 | - }); |
|
| 280 | - } |
|
| 266 | + /** |
|
| 267 | + * @return array |
|
| 268 | + */ |
|
| 269 | + protected function normalizeSettings(array $strings) |
|
| 270 | + { |
|
| 271 | + $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 272 | + $strings = array_filter($strings, 'is_array'); |
|
| 273 | + foreach ($strings as &$string) { |
|
| 274 | + $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
|
| 275 | + $string = wp_parse_args($string, $defaultString); |
|
| 276 | + } |
|
| 277 | + return array_filter($strings, function ($string) { |
|
| 278 | + return !empty($string['id']); |
|
| 279 | + }); |
|
| 280 | + } |
|
| 281 | 281 | } |
@@ -29,10 +29,10 @@ discard block |
||
| 29 | 29 | public function all() |
| 30 | 30 | { |
| 31 | 31 | $translations = $this->translations(); |
| 32 | - $entries = $this->filter($translations, $this->entries())->results(); |
|
| 33 | - array_walk($translations, function (&$entry) use ($entries) { |
|
| 34 | - $entry['desc'] = array_key_exists($entry['id'], $entries) |
|
| 35 | - ? $this->getEntryString($entries[$entry['id']], 'msgctxt') |
|
| 32 | + $entries = $this->filter( $translations, $this->entries() )->results(); |
|
| 33 | + array_walk( $translations, function( &$entry ) use ($entries) { |
|
| 34 | + $entry['desc'] = array_key_exists( $entry['id'], $entries ) |
|
| 35 | + ? $this->getEntryString( $entries[$entry['id']], 'msgctxt' ) |
|
| 36 | 36 | : ''; |
| 37 | 37 | }); |
| 38 | 38 | return $translations; |
@@ -43,10 +43,10 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function entries() |
| 45 | 45 | { |
| 46 | - if (!isset($this->entries)) { |
|
| 47 | - $potFile = glsr()->path(glsr()->languages.'/'.Application::ID.'.pot'); |
|
| 48 | - $entries = $this->extractEntriesFromPotFile($potFile); |
|
| 49 | - $entries = apply_filters('site-reviews/translation/entries', $entries); |
|
| 46 | + if( !isset($this->entries) ) { |
|
| 47 | + $potFile = glsr()->path( glsr()->languages.'/'.Application::ID.'.pot' ); |
|
| 48 | + $entries = $this->extractEntriesFromPotFile( $potFile ); |
|
| 49 | + $entries = apply_filters( 'site-reviews/translation/entries', $entries ); |
|
| 50 | 50 | $this->entries = $entries; |
| 51 | 51 | } |
| 52 | 52 | return $this->entries; |
@@ -57,24 +57,24 @@ discard block |
||
| 57 | 57 | * @param array|null $entries |
| 58 | 58 | * @return static |
| 59 | 59 | */ |
| 60 | - public function exclude($entriesToExclude = null, $entries = null) |
|
| 60 | + public function exclude( $entriesToExclude = null, $entries = null ) |
|
| 61 | 61 | { |
| 62 | - return $this->filter($entriesToExclude, $entries, false); |
|
| 62 | + return $this->filter( $entriesToExclude, $entries, false ); |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * @param string $potFile |
| 67 | 67 | * @return array |
| 68 | 68 | */ |
| 69 | - public function extractEntriesFromPotFile($potFile, array $entries = []) |
|
| 69 | + public function extractEntriesFromPotFile( $potFile, array $entries = [] ) |
|
| 70 | 70 | { |
| 71 | 71 | try { |
| 72 | - $potEntries = $this->normalize(Parser::parseFile($potFile)->getEntries()); |
|
| 73 | - foreach ($potEntries as $key => $entry) { |
|
| 74 | - $entries[html_entity_decode($key, ENT_COMPAT, 'UTF-8')] = $entry; |
|
| 72 | + $potEntries = $this->normalize( Parser::parseFile( $potFile )->getEntries() ); |
|
| 73 | + foreach( $potEntries as $key => $entry ) { |
|
| 74 | + $entries[html_entity_decode( $key, ENT_COMPAT, 'UTF-8' )] = $entry; |
|
| 75 | 75 | } |
| 76 | - } catch (Exception $e) { |
|
| 77 | - glsr_log()->error($e->getMessage()); |
|
| 76 | + } catch( Exception $e ) { |
|
| 77 | + glsr_log()->error( $e->getMessage() ); |
|
| 78 | 78 | } |
| 79 | 79 | return $entries; |
| 80 | 80 | } |
@@ -85,18 +85,18 @@ discard block |
||
| 85 | 85 | * @param bool $intersect |
| 86 | 86 | * @return static |
| 87 | 87 | */ |
| 88 | - public function filter($filterWith = null, $entries = null, $intersect = true) |
|
| 88 | + public function filter( $filterWith = null, $entries = null, $intersect = true ) |
|
| 89 | 89 | { |
| 90 | - if (!is_array($entries)) { |
|
| 90 | + if( !is_array( $entries ) ) { |
|
| 91 | 91 | $entries = $this->results; |
| 92 | 92 | } |
| 93 | - if (!is_array($filterWith)) { |
|
| 93 | + if( !is_array( $filterWith ) ) { |
|
| 94 | 94 | $filterWith = $this->translations(); |
| 95 | 95 | } |
| 96 | - $keys = array_flip(glsr_array_column($filterWith, 'id')); |
|
| 96 | + $keys = array_flip( glsr_array_column( $filterWith, 'id' ) ); |
|
| 97 | 97 | $this->results = $intersect |
| 98 | - ? array_intersect_key($entries, $keys) |
|
| 99 | - : array_diff_key($entries, $keys); |
|
| 98 | + ? array_intersect_key( $entries, $keys ) |
|
| 99 | + : array_diff_key( $entries, $keys ); |
|
| 100 | 100 | return $this; |
| 101 | 101 | } |
| 102 | 102 | |
@@ -104,20 +104,20 @@ discard block |
||
| 104 | 104 | * @param string $template |
| 105 | 105 | * @return string |
| 106 | 106 | */ |
| 107 | - public function render($template, array $entry) |
|
| 107 | + public function render( $template, array $entry ) |
|
| 108 | 108 | { |
| 109 | 109 | $data = array_combine( |
| 110 | - array_map(function ($key) { return 'data.'.$key; }, array_keys($entry)), |
|
| 110 | + array_map( function( $key ) { return 'data.'.$key; }, array_keys( $entry ) ), |
|
| 111 | 111 | $entry |
| 112 | 112 | ); |
| 113 | 113 | $data['data.class'] = $data['data.error'] = ''; |
| 114 | - if (false === array_search($entry['s1'], glsr_array_column($this->entries(), 'msgid'))) { |
|
| 114 | + if( false === array_search( $entry['s1'], glsr_array_column( $this->entries(), 'msgid' ) ) ) { |
|
| 115 | 115 | $data['data.class'] = 'is-invalid'; |
| 116 | - $data['data.error'] = __('This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews'); |
|
| 116 | + $data['data.error'] = __( 'This custom translation is no longer valid as the original text has been changed or removed.', 'site-reviews' ); |
|
| 117 | 117 | } |
| 118 | - return glsr(Template::class)->build('partials/translations/'.$template, [ |
|
| 119 | - 'context' => array_map('esc_html', $data), |
|
| 120 | - ]); |
|
| 118 | + return glsr( Template::class )->build( 'partials/translations/'.$template, [ |
|
| 119 | + 'context' => array_map( 'esc_html', $data ), |
|
| 120 | + ] ); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -127,10 +127,10 @@ discard block |
||
| 127 | 127 | public function renderAll() |
| 128 | 128 | { |
| 129 | 129 | $rendered = ''; |
| 130 | - foreach ($this->all() as $index => $entry) { |
|
| 130 | + foreach( $this->all() as $index => $entry ) { |
|
| 131 | 131 | $entry['index'] = $index; |
| 132 | 132 | $entry['prefix'] = OptionManager::databaseKey(); |
| 133 | - $rendered.= $this->render($entry['type'], $entry); |
|
| 133 | + $rendered .= $this->render( $entry['type'], $entry ); |
|
| 134 | 134 | } |
| 135 | 135 | return $rendered; |
| 136 | 136 | } |
@@ -139,25 +139,25 @@ discard block |
||
| 139 | 139 | * @param bool $resetAfterRender |
| 140 | 140 | * @return string |
| 141 | 141 | */ |
| 142 | - public function renderResults($resetAfterRender = true) |
|
| 142 | + public function renderResults( $resetAfterRender = true ) |
|
| 143 | 143 | { |
| 144 | 144 | $rendered = ''; |
| 145 | - foreach ($this->results as $id => $entry) { |
|
| 145 | + foreach( $this->results as $id => $entry ) { |
|
| 146 | 146 | $data = [ |
| 147 | - 'desc' => $this->getEntryString($entry, 'msgctxt'), |
|
| 147 | + 'desc' => $this->getEntryString( $entry, 'msgctxt' ), |
|
| 148 | 148 | 'id' => $id, |
| 149 | - 'p1' => $this->getEntryString($entry, 'msgid_plural'), |
|
| 150 | - 's1' => $this->getEntryString($entry, 'msgid'), |
|
| 149 | + 'p1' => $this->getEntryString( $entry, 'msgid_plural' ), |
|
| 150 | + 's1' => $this->getEntryString( $entry, 'msgid' ), |
|
| 151 | 151 | ]; |
| 152 | 152 | $text = !empty($data['p1']) |
| 153 | - ? sprintf('%s | %s', $data['s1'], $data['p1']) |
|
| 153 | + ? sprintf( '%s | %s', $data['s1'], $data['p1'] ) |
|
| 154 | 154 | : $data['s1']; |
| 155 | - $rendered.= $this->render('result', [ |
|
| 156 | - 'entry' => json_encode($data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), |
|
| 157 | - 'text' => wp_strip_all_tags($text), |
|
| 158 | - ]); |
|
| 155 | + $rendered .= $this->render( 'result', [ |
|
| 156 | + 'entry' => json_encode( $data, JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ), |
|
| 157 | + 'text' => wp_strip_all_tags( $text ), |
|
| 158 | + ] ); |
|
| 159 | 159 | } |
| 160 | - if ($resetAfterRender) { |
|
| 160 | + if( $resetAfterRender ) { |
|
| 161 | 161 | $this->reset(); |
| 162 | 162 | } |
| 163 | 163 | return $rendered; |
@@ -185,18 +185,18 @@ discard block |
||
| 185 | 185 | * @param string $needle |
| 186 | 186 | * @return static |
| 187 | 187 | */ |
| 188 | - public function search($needle = '') |
|
| 188 | + public function search( $needle = '' ) |
|
| 189 | 189 | { |
| 190 | 190 | $this->reset(); |
| 191 | - $needle = trim(strtolower($needle)); |
|
| 192 | - foreach ($this->entries() as $key => $entry) { |
|
| 193 | - $single = strtolower($this->getEntryString($entry, 'msgid')); |
|
| 194 | - $plural = strtolower($this->getEntryString($entry, 'msgid_plural')); |
|
| 195 | - if (strlen($needle) < static::SEARCH_THRESHOLD) { |
|
| 196 | - if (in_array($needle, [$single, $plural])) { |
|
| 191 | + $needle = trim( strtolower( $needle ) ); |
|
| 192 | + foreach( $this->entries() as $key => $entry ) { |
|
| 193 | + $single = strtolower( $this->getEntryString( $entry, 'msgid' ) ); |
|
| 194 | + $plural = strtolower( $this->getEntryString( $entry, 'msgid_plural' ) ); |
|
| 195 | + if( strlen( $needle ) < static::SEARCH_THRESHOLD ) { |
|
| 196 | + if( in_array( $needle, [$single, $plural] ) ) { |
|
| 197 | 197 | $this->results[$key] = $entry; |
| 198 | 198 | } |
| 199 | - } elseif (false !== strpos(sprintf('%s %s', $single, $plural), $needle)) { |
|
| 199 | + } elseif( false !== strpos( sprintf( '%s %s', $single, $plural ), $needle ) ) { |
|
| 200 | 200 | $this->results[$key] = $entry; |
| 201 | 201 | } |
| 202 | 202 | } |
@@ -210,10 +210,10 @@ discard block |
||
| 210 | 210 | public function translations() |
| 211 | 211 | { |
| 212 | 212 | static $translations; |
| 213 | - if (empty($translations)) { |
|
| 214 | - $settings = glsr(OptionManager::class)->get('settings'); |
|
| 213 | + if( empty($translations) ) { |
|
| 214 | + $settings = glsr( OptionManager::class )->get( 'settings' ); |
|
| 215 | 215 | $translations = isset($settings['strings']) |
| 216 | - ? $this->normalizeSettings((array) $settings['strings']) |
|
| 216 | + ? $this->normalizeSettings( (array)$settings['strings'] ) |
|
| 217 | 217 | : []; |
| 218 | 218 | } |
| 219 | 219 | return $translations; |
@@ -223,28 +223,28 @@ discard block |
||
| 223 | 223 | * @param string $key |
| 224 | 224 | * @return string |
| 225 | 225 | */ |
| 226 | - protected function getEntryString(array $entry, $key) |
|
| 226 | + protected function getEntryString( array $entry, $key ) |
|
| 227 | 227 | { |
| 228 | 228 | return isset($entry[$key]) |
| 229 | - ? implode('', (array) $entry[$key]) |
|
| 229 | + ? implode( '', (array)$entry[$key] ) |
|
| 230 | 230 | : ''; |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | /** |
| 234 | 234 | * @return array |
| 235 | 235 | */ |
| 236 | - protected function normalize(array $entries) |
|
| 236 | + protected function normalize( array $entries ) |
|
| 237 | 237 | { |
| 238 | 238 | $keys = [ |
| 239 | 239 | 'msgctxt', 'msgid', 'msgid_plural', 'msgstr', 'msgstr[0]', 'msgstr[1]', |
| 240 | 240 | ]; |
| 241 | - array_walk($entries, function (&$entry) use ($keys) { |
|
| 242 | - foreach ($keys as $key) { |
|
| 241 | + array_walk( $entries, function( &$entry ) use ($keys) { |
|
| 242 | + foreach( $keys as $key ) { |
|
| 243 | 243 | try { |
| 244 | - $entry = $this->normalizeEntryString($entry, $key); |
|
| 245 | - } catch (\TypeError $error) { |
|
| 246 | - glsr_log()->once('error', 'Translation/normalize', $error); |
|
| 247 | - glsr_log()->once('debug', 'Translation/normalize', $entry); |
|
| 244 | + $entry = $this->normalizeEntryString( $entry, $key ); |
|
| 245 | + } catch( \TypeError $error ) { |
|
| 246 | + glsr_log()->once( 'error', 'Translation/normalize', $error ); |
|
| 247 | + glsr_log()->once( 'debug', 'Translation/normalize', $entry ); |
|
| 248 | 248 | } |
| 249 | 249 | } |
| 250 | 250 | }); |
@@ -255,10 +255,10 @@ discard block |
||
| 255 | 255 | * @param string $key |
| 256 | 256 | * @return array |
| 257 | 257 | */ |
| 258 | - protected function normalizeEntryString(array $entry, $key) |
|
| 258 | + protected function normalizeEntryString( array $entry, $key ) |
|
| 259 | 259 | { |
| 260 | - if (isset($entry[$key])) { |
|
| 261 | - $entry[$key] = $this->getEntryString($entry, $key); |
|
| 260 | + if( isset($entry[$key]) ) { |
|
| 261 | + $entry[$key] = $this->getEntryString( $entry, $key ); |
|
| 262 | 262 | } |
| 263 | 263 | return $entry; |
| 264 | 264 | } |
@@ -266,15 +266,15 @@ discard block |
||
| 266 | 266 | /** |
| 267 | 267 | * @return array |
| 268 | 268 | */ |
| 269 | - protected function normalizeSettings(array $strings) |
|
| 269 | + protected function normalizeSettings( array $strings ) |
|
| 270 | 270 | { |
| 271 | - $defaultString = array_fill_keys(['id', 's1', 's2', 'p1', 'p2'], ''); |
|
| 272 | - $strings = array_filter($strings, 'is_array'); |
|
| 273 | - foreach ($strings as &$string) { |
|
| 271 | + $defaultString = array_fill_keys( ['id', 's1', 's2', 'p1', 'p2'], '' ); |
|
| 272 | + $strings = array_filter( $strings, 'is_array' ); |
|
| 273 | + foreach( $strings as &$string ) { |
|
| 274 | 274 | $string['type'] = isset($string['p1']) ? 'plural' : 'single'; |
| 275 | - $string = wp_parse_args($string, $defaultString); |
|
| 275 | + $string = wp_parse_args( $string, $defaultString ); |
|
| 276 | 276 | } |
| 277 | - return array_filter($strings, function ($string) { |
|
| 277 | + return array_filter( $strings, function( $string ) { |
|
| 278 | 278 | return !empty($string['id']); |
| 279 | 279 | }); |
| 280 | 280 | } |