@@ -9,55 +9,55 @@ |
||
| 9 | 9 | |
| 10 | 10 | class Pagination implements PartialContract |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected $args; |
|
| 12 | + /** |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected $args; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @return void|string |
|
| 19 | - */ |
|
| 20 | - public function build(array $args = []) |
|
| 21 | - { |
|
| 22 | - $this->args = $this->normalize($args); |
|
| 23 | - if ($this->args['total'] < 2) { |
|
| 24 | - return; |
|
| 25 | - } |
|
| 26 | - return glsr(Template::class)->build('templates/pagination', [ |
|
| 27 | - 'context' => [ |
|
| 28 | - 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
| 29 | - 'loader' => '<div class="glsr-loader"></div>', |
|
| 30 | - 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
| 31 | - ], |
|
| 32 | - ]); |
|
| 33 | - } |
|
| 17 | + /** |
|
| 18 | + * @return void|string |
|
| 19 | + */ |
|
| 20 | + public function build(array $args = []) |
|
| 21 | + { |
|
| 22 | + $this->args = $this->normalize($args); |
|
| 23 | + if ($this->args['total'] < 2) { |
|
| 24 | + return; |
|
| 25 | + } |
|
| 26 | + return glsr(Template::class)->build('templates/pagination', [ |
|
| 27 | + 'context' => [ |
|
| 28 | + 'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
| 29 | + 'loader' => '<div class="glsr-loader"></div>', |
|
| 30 | + 'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
| 31 | + ], |
|
| 32 | + ]); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @return string |
|
| 37 | - */ |
|
| 38 | - protected function buildLinks() |
|
| 39 | - { |
|
| 40 | - $args = glsr(Style::class)->paginationArgs($this->args); |
|
| 41 | - if (is_front_page()) { |
|
| 42 | - unset($args['format']); |
|
| 43 | - } |
|
| 44 | - if ('array' == $args['type']) { |
|
| 45 | - $args['type'] = 'plain'; |
|
| 46 | - } |
|
| 47 | - return paginate_links($args); |
|
| 48 | - } |
|
| 35 | + /** |
|
| 36 | + * @return string |
|
| 37 | + */ |
|
| 38 | + protected function buildLinks() |
|
| 39 | + { |
|
| 40 | + $args = glsr(Style::class)->paginationArgs($this->args); |
|
| 41 | + if (is_front_page()) { |
|
| 42 | + unset($args['format']); |
|
| 43 | + } |
|
| 44 | + if ('array' == $args['type']) { |
|
| 45 | + $args['type'] = 'plain'; |
|
| 46 | + } |
|
| 47 | + return paginate_links($args); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - protected function normalize(array $args) |
|
| 54 | - { |
|
| 55 | - if ($baseUrl = glsr_get($args, 'baseUrl')) { |
|
| 56 | - $args['base'] = $baseUrl.'%_%'; |
|
| 57 | - } |
|
| 58 | - return wp_parse_args(array_filter($args), [ |
|
| 59 | - 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
| 60 | - 'total' => 1, |
|
| 61 | - ]); |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + protected function normalize(array $args) |
|
| 54 | + { |
|
| 55 | + if ($baseUrl = glsr_get($args, 'baseUrl')) { |
|
| 56 | + $args['base'] = $baseUrl.'%_%'; |
|
| 57 | + } |
|
| 58 | + return wp_parse_args(array_filter($args), [ |
|
| 59 | + 'current' => glsr(QueryBuilder::class)->getPaged(), |
|
| 60 | + 'total' => 1, |
|
| 61 | + ]); |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -19,376 +19,376 @@ |
||
| 19 | 19 | |
| 20 | 20 | class SiteReviews |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - public $args; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + public $args; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @var Review |
|
| 29 | - */ |
|
| 30 | - public $current; |
|
| 27 | + /** |
|
| 28 | + * @var Review |
|
| 29 | + */ |
|
| 30 | + public $current; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - public $options; |
|
| 32 | + /** |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + public $options; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @var Reviews |
|
| 39 | - */ |
|
| 40 | - protected $reviews; |
|
| 37 | + /** |
|
| 38 | + * @var Reviews |
|
| 39 | + */ |
|
| 40 | + protected $reviews; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param Reviews|null $reviews |
|
| 44 | - * @return ReviewsHtml |
|
| 45 | - */ |
|
| 46 | - public function build(array $args = [], $reviews = null) |
|
| 47 | - { |
|
| 48 | - $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
| 49 | - $this->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
| 50 | - $this->reviews = $reviews instanceof Reviews |
|
| 51 | - ? $reviews |
|
| 52 | - : glsr(ReviewManager::class)->get($args); |
|
| 53 | - $this->generateSchema(); |
|
| 54 | - return $this->buildReviews(); |
|
| 55 | - } |
|
| 42 | + /** |
|
| 43 | + * @param Reviews|null $reviews |
|
| 44 | + * @return ReviewsHtml |
|
| 45 | + */ |
|
| 46 | + public function build(array $args = [], $reviews = null) |
|
| 47 | + { |
|
| 48 | + $this->args = glsr(SiteReviewsDefaults::class)->merge($args); |
|
| 49 | + $this->options = glsr(Helper::class)->flattenArray(glsr(OptionManager::class)->all()); |
|
| 50 | + $this->reviews = $reviews instanceof Reviews |
|
| 51 | + ? $reviews |
|
| 52 | + : glsr(ReviewManager::class)->get($args); |
|
| 53 | + $this->generateSchema(); |
|
| 54 | + return $this->buildReviews(); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return ReviewHtml |
|
| 59 | - */ |
|
| 60 | - public function buildReview(Review $review) |
|
| 61 | - { |
|
| 62 | - $review = apply_filters('site-reviews/review/build/before', $review); |
|
| 63 | - $this->current = $review; |
|
| 64 | - $renderedFields = []; |
|
| 65 | - foreach ($review as $key => $value) { |
|
| 66 | - $method = glsr(Helper::class)->buildMethodName($key, 'buildOption'); |
|
| 67 | - $field = method_exists($this, $method) |
|
| 68 | - ? $this->$method($key, $value) |
|
| 69 | - : apply_filters('site-reviews/review/build/'.$key, false, $value, $this, $review); |
|
| 70 | - if (false === $field) { |
|
| 71 | - continue; |
|
| 72 | - } |
|
| 73 | - $renderedFields[$key] = $field; |
|
| 74 | - } |
|
| 75 | - $this->wrap($renderedFields, $review); |
|
| 76 | - $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review); |
|
| 77 | - $this->current = null; |
|
| 78 | - return new ReviewHtml($review, (array) $renderedFields); |
|
| 79 | - } |
|
| 57 | + /** |
|
| 58 | + * @return ReviewHtml |
|
| 59 | + */ |
|
| 60 | + public function buildReview(Review $review) |
|
| 61 | + { |
|
| 62 | + $review = apply_filters('site-reviews/review/build/before', $review); |
|
| 63 | + $this->current = $review; |
|
| 64 | + $renderedFields = []; |
|
| 65 | + foreach ($review as $key => $value) { |
|
| 66 | + $method = glsr(Helper::class)->buildMethodName($key, 'buildOption'); |
|
| 67 | + $field = method_exists($this, $method) |
|
| 68 | + ? $this->$method($key, $value) |
|
| 69 | + : apply_filters('site-reviews/review/build/'.$key, false, $value, $this, $review); |
|
| 70 | + if (false === $field) { |
|
| 71 | + continue; |
|
| 72 | + } |
|
| 73 | + $renderedFields[$key] = $field; |
|
| 74 | + } |
|
| 75 | + $this->wrap($renderedFields, $review); |
|
| 76 | + $renderedFields = apply_filters('site-reviews/review/build/after', $renderedFields, $review); |
|
| 77 | + $this->current = null; |
|
| 78 | + return new ReviewHtml($review, (array) $renderedFields); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * @return ReviewsHtml |
|
| 83 | - */ |
|
| 84 | - public function buildReviews() |
|
| 85 | - { |
|
| 86 | - $renderedReviews = []; |
|
| 87 | - foreach ($this->reviews as $index => $review) { |
|
| 88 | - $renderedReviews[] = $this->buildReview($review); |
|
| 89 | - } |
|
| 90 | - return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
| 91 | - } |
|
| 81 | + /** |
|
| 82 | + * @return ReviewsHtml |
|
| 83 | + */ |
|
| 84 | + public function buildReviews() |
|
| 85 | + { |
|
| 86 | + $renderedReviews = []; |
|
| 87 | + foreach ($this->reviews as $index => $review) { |
|
| 88 | + $renderedReviews[] = $this->buildReview($review); |
|
| 89 | + } |
|
| 90 | + return new ReviewsHtml($renderedReviews, $this->reviews->max_num_pages, $this->args); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * @return void |
|
| 95 | - */ |
|
| 96 | - public function generateSchema() |
|
| 97 | - { |
|
| 98 | - if (!wp_validate_boolean($this->args['schema'])) { |
|
| 99 | - return; |
|
| 100 | - } |
|
| 101 | - glsr(Schema::class)->store( |
|
| 102 | - glsr(Schema::class)->build($this->args) |
|
| 103 | - ); |
|
| 104 | - } |
|
| 93 | + /** |
|
| 94 | + * @return void |
|
| 95 | + */ |
|
| 96 | + public function generateSchema() |
|
| 97 | + { |
|
| 98 | + if (!wp_validate_boolean($this->args['schema'])) { |
|
| 99 | + return; |
|
| 100 | + } |
|
| 101 | + glsr(Schema::class)->store( |
|
| 102 | + glsr(Schema::class)->build($this->args) |
|
| 103 | + ); |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * @param string $key |
|
| 108 | - * @param string $path |
|
| 109 | - * @return bool |
|
| 110 | - */ |
|
| 111 | - public function isHidden($key, $path = '') |
|
| 112 | - { |
|
| 113 | - $isOptionEnabled = !empty($path) |
|
| 114 | - ? $this->isOptionEnabled($path) |
|
| 115 | - : true; |
|
| 116 | - return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
| 117 | - } |
|
| 106 | + /** |
|
| 107 | + * @param string $key |
|
| 108 | + * @param string $path |
|
| 109 | + * @return bool |
|
| 110 | + */ |
|
| 111 | + public function isHidden($key, $path = '') |
|
| 112 | + { |
|
| 113 | + $isOptionEnabled = !empty($path) |
|
| 114 | + ? $this->isOptionEnabled($path) |
|
| 115 | + : true; |
|
| 116 | + return in_array($key, $this->args['hide']) || !$isOptionEnabled; |
|
| 117 | + } |
|
| 118 | 118 | |
| 119 | - /** |
|
| 120 | - * @param string $key |
|
| 121 | - * @param string $value |
|
| 122 | - * @return void|string |
|
| 123 | - */ |
|
| 124 | - protected function buildOptionAssignedTo($key, $value) |
|
| 125 | - { |
|
| 126 | - if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
| 127 | - return; |
|
| 128 | - } |
|
| 129 | - $post = glsr(Polylang::class)->getPost($value); |
|
| 130 | - if (!($post instanceof WP_Post)) { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
| 134 | - 'href' => get_the_permalink($post->ID), |
|
| 135 | - ]); |
|
| 136 | - $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
| 137 | - return '<span>'.$assignedTo.'</span>'; |
|
| 138 | - } |
|
| 119 | + /** |
|
| 120 | + * @param string $key |
|
| 121 | + * @param string $value |
|
| 122 | + * @return void|string |
|
| 123 | + */ |
|
| 124 | + protected function buildOptionAssignedTo($key, $value) |
|
| 125 | + { |
|
| 126 | + if ($this->isHidden($key, 'settings.reviews.assigned_links')) { |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | + $post = glsr(Polylang::class)->getPost($value); |
|
| 130 | + if (!($post instanceof WP_Post)) { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + $permalink = glsr(Builder::class)->a(get_the_title($post->ID), [ |
|
| 134 | + 'href' => get_the_permalink($post->ID), |
|
| 135 | + ]); |
|
| 136 | + $assignedTo = sprintf(__('Review of %s', 'site-reviews'), $permalink); |
|
| 137 | + return '<span>'.$assignedTo.'</span>'; |
|
| 138 | + } |
|
| 139 | 139 | |
| 140 | - /** |
|
| 141 | - * @param string $key |
|
| 142 | - * @param string $value |
|
| 143 | - * @return void|string |
|
| 144 | - */ |
|
| 145 | - protected function buildOptionAuthor($key, $value) |
|
| 146 | - { |
|
| 147 | - if !($this->isHidden($key)) { |
|
| 148 | - return '<span>'.$value.'</span>'; |
|
| 149 | - } |
|
| 150 | - } |
|
| 140 | + /** |
|
| 141 | + * @param string $key |
|
| 142 | + * @param string $value |
|
| 143 | + * @return void|string |
|
| 144 | + */ |
|
| 145 | + protected function buildOptionAuthor($key, $value) |
|
| 146 | + { |
|
| 147 | + if !($this->isHidden($key)) { |
|
| 148 | + return '<span>'.$value.'</span>'; |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * @param string $key |
|
| 154 | - * @param string $value |
|
| 155 | - * @return void|string |
|
| 156 | - */ |
|
| 157 | - protected function buildOptionAvatar($key, $value) |
|
| 158 | - { |
|
| 159 | - if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
| 160 | - return; |
|
| 161 | - } |
|
| 162 | - $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
| 163 | - return glsr(Builder::class)->img([ |
|
| 164 | - 'height' => $size, |
|
| 165 | - 'src' => $this->generateAvatar($value), |
|
| 166 | - 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
| 167 | - 'width' => $size, |
|
| 168 | - ]); |
|
| 169 | - } |
|
| 152 | + /** |
|
| 153 | + * @param string $key |
|
| 154 | + * @param string $value |
|
| 155 | + * @return void|string |
|
| 156 | + */ |
|
| 157 | + protected function buildOptionAvatar($key, $value) |
|
| 158 | + { |
|
| 159 | + if ($this->isHidden($key, 'settings.reviews.avatars')) { |
|
| 160 | + return; |
|
| 161 | + } |
|
| 162 | + $size = $this->getOption('settings.reviews.avatars_size', 40); |
|
| 163 | + return glsr(Builder::class)->img([ |
|
| 164 | + 'height' => $size, |
|
| 165 | + 'src' => $this->generateAvatar($value), |
|
| 166 | + 'style' => sprintf('width:%1$spx; height:%1$spx;', $size), |
|
| 167 | + 'width' => $size, |
|
| 168 | + ]); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * @param string $key |
|
| 173 | - * @param string $value |
|
| 174 | - * @return void|string |
|
| 175 | - */ |
|
| 176 | - protected function buildOptionContent($key, $value) |
|
| 177 | - { |
|
| 178 | - $text = $this->normalizeText($value); |
|
| 179 | - if (!$this->isHiddenOrEmpty($key, $text)) { |
|
| 180 | - return '<p>'.$text.'</p>'; |
|
| 181 | - } |
|
| 182 | - } |
|
| 171 | + /** |
|
| 172 | + * @param string $key |
|
| 173 | + * @param string $value |
|
| 174 | + * @return void|string |
|
| 175 | + */ |
|
| 176 | + protected function buildOptionContent($key, $value) |
|
| 177 | + { |
|
| 178 | + $text = $this->normalizeText($value); |
|
| 179 | + if (!$this->isHiddenOrEmpty($key, $text)) { |
|
| 180 | + return '<p>'.$text.'</p>'; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @param string $key |
|
| 186 | - * @param string $value |
|
| 187 | - * @return void|string |
|
| 188 | - */ |
|
| 189 | - protected function buildOptionDate($key, $value) |
|
| 190 | - { |
|
| 191 | - if ($this->isHidden($key)) { |
|
| 192 | - return; |
|
| 193 | - } |
|
| 194 | - $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
| 195 | - if ('relative' == $dateFormat) { |
|
| 196 | - $date = glsr(Date::class)->relative($value); |
|
| 197 | - } else { |
|
| 198 | - $format = 'custom' == $dateFormat |
|
| 199 | - ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
| 200 | - : (string) get_option('date_format'); |
|
| 201 | - $date = date_i18n($format, strtotime($value)); |
|
| 202 | - } |
|
| 203 | - return '<span>'.$date.'</span>'; |
|
| 204 | - } |
|
| 184 | + /** |
|
| 185 | + * @param string $key |
|
| 186 | + * @param string $value |
|
| 187 | + * @return void|string |
|
| 188 | + */ |
|
| 189 | + protected function buildOptionDate($key, $value) |
|
| 190 | + { |
|
| 191 | + if ($this->isHidden($key)) { |
|
| 192 | + return; |
|
| 193 | + } |
|
| 194 | + $dateFormat = $this->getOption('settings.reviews.date.format', 'default'); |
|
| 195 | + if ('relative' == $dateFormat) { |
|
| 196 | + $date = glsr(Date::class)->relative($value); |
|
| 197 | + } else { |
|
| 198 | + $format = 'custom' == $dateFormat |
|
| 199 | + ? $this->getOption('settings.reviews.date.custom', 'M j, Y') |
|
| 200 | + : (string) get_option('date_format'); |
|
| 201 | + $date = date_i18n($format, strtotime($value)); |
|
| 202 | + } |
|
| 203 | + return '<span>'.$date.'</span>'; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * @param string $key |
|
| 208 | - * @param string $value |
|
| 209 | - * @return void|string |
|
| 210 | - */ |
|
| 211 | - protected function buildOptionRating($key, $value) |
|
| 212 | - { |
|
| 213 | - if (!$this->isHiddenOrEmpty($key, $value)) { |
|
| 214 | - return glsr_star_rating($value); |
|
| 215 | - } |
|
| 216 | - } |
|
| 206 | + /** |
|
| 207 | + * @param string $key |
|
| 208 | + * @param string $value |
|
| 209 | + * @return void|string |
|
| 210 | + */ |
|
| 211 | + protected function buildOptionRating($key, $value) |
|
| 212 | + { |
|
| 213 | + if (!$this->isHiddenOrEmpty($key, $value)) { |
|
| 214 | + return glsr_star_rating($value); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * @param string $key |
|
| 220 | - * @param string $value |
|
| 221 | - * @return void|string |
|
| 222 | - */ |
|
| 223 | - protected function buildOptionResponse($key, $value) |
|
| 224 | - { |
|
| 225 | - if ($this->isHiddenOrEmpty($key, $value)) { |
|
| 226 | - return; |
|
| 227 | - } |
|
| 228 | - $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
| 229 | - $text = $this->normalizeText($value); |
|
| 230 | - $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
| 231 | - $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
| 232 | - $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
| 233 | - return $response.$background; |
|
| 234 | - } |
|
| 218 | + /** |
|
| 219 | + * @param string $key |
|
| 220 | + * @param string $value |
|
| 221 | + * @return void|string |
|
| 222 | + */ |
|
| 223 | + protected function buildOptionResponse($key, $value) |
|
| 224 | + { |
|
| 225 | + if ($this->isHiddenOrEmpty($key, $value)) { |
|
| 226 | + return; |
|
| 227 | + } |
|
| 228 | + $title = sprintf(__('Response from %s', 'site-reviews'), get_bloginfo('name')); |
|
| 229 | + $text = $this->normalizeText($value); |
|
| 230 | + $text = '<p><strong>'.$title.'</strong></p><p>'.$text.'</p>'; |
|
| 231 | + $response = glsr(Builder::class)->div($text, ['class' => 'glsr-review-response-inner']); |
|
| 232 | + $background = glsr(Builder::class)->div(['class' => 'glsr-review-response-background']); |
|
| 233 | + return $response.$background; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - /** |
|
| 237 | - * @param string $key |
|
| 238 | - * @param string $value |
|
| 239 | - * @return void|string |
|
| 240 | - */ |
|
| 241 | - protected function buildOptionTitle($key, $value) |
|
| 242 | - { |
|
| 243 | - if ($this->isHidden($key)) { |
|
| 244 | - return; |
|
| 245 | - } |
|
| 246 | - if (empty($value)) { |
|
| 247 | - $value = __('No Title', 'site-reviews'); |
|
| 248 | - } |
|
| 249 | - return '<h3>'.$value.'</h3>'; |
|
| 250 | - } |
|
| 236 | + /** |
|
| 237 | + * @param string $key |
|
| 238 | + * @param string $value |
|
| 239 | + * @return void|string |
|
| 240 | + */ |
|
| 241 | + protected function buildOptionTitle($key, $value) |
|
| 242 | + { |
|
| 243 | + if ($this->isHidden($key)) { |
|
| 244 | + return; |
|
| 245 | + } |
|
| 246 | + if (empty($value)) { |
|
| 247 | + $value = __('No Title', 'site-reviews'); |
|
| 248 | + } |
|
| 249 | + return '<h3>'.$value.'</h3>'; |
|
| 250 | + } |
|
| 251 | 251 | |
| 252 | - /** |
|
| 253 | - * @param string $avatarUrl |
|
| 254 | - * @return string |
|
| 255 | - */ |
|
| 256 | - protected function generateAvatar($avatarUrl) |
|
| 257 | - { |
|
| 258 | - if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
| 259 | - return $avatarUrl; |
|
| 260 | - } |
|
| 261 | - $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
| 262 | - if (empty($authorIdOrEmail)) { |
|
| 263 | - $authorIdOrEmail = $this->current->email; |
|
| 264 | - } |
|
| 265 | - if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
| 266 | - return $newAvatar; |
|
| 267 | - } |
|
| 268 | - return $avatarUrl; |
|
| 269 | - } |
|
| 252 | + /** |
|
| 253 | + * @param string $avatarUrl |
|
| 254 | + * @return string |
|
| 255 | + */ |
|
| 256 | + protected function generateAvatar($avatarUrl) |
|
| 257 | + { |
|
| 258 | + if (!$this->isOptionEnabled('settings.reviews.avatars_regenerate') || 'local' != $this->current->review_type) { |
|
| 259 | + return $avatarUrl; |
|
| 260 | + } |
|
| 261 | + $authorIdOrEmail = get_the_author_meta('ID', $this->current->user_id); |
|
| 262 | + if (empty($authorIdOrEmail)) { |
|
| 263 | + $authorIdOrEmail = $this->current->email; |
|
| 264 | + } |
|
| 265 | + if ($newAvatar = get_avatar_url($authorIdOrEmail)) { |
|
| 266 | + return $newAvatar; |
|
| 267 | + } |
|
| 268 | + return $avatarUrl; |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * @param string $text |
|
| 273 | - * @return string |
|
| 274 | - */ |
|
| 275 | - protected function getExcerpt($text) |
|
| 276 | - { |
|
| 277 | - $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
| 278 | - $split = extension_loaded('intl') |
|
| 279 | - ? $this->getExcerptIntlSplit($text, $limit) |
|
| 280 | - : $this->getExcerptSplit($text, $limit); |
|
| 281 | - $hiddenText = substr($text, $split); |
|
| 282 | - if (!empty($hiddenText)) { |
|
| 283 | - $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
| 284 | - 'class' => 'glsr-hidden glsr-hidden-text', |
|
| 285 | - 'data-show-less' => __('Show less', 'site-reviews'), |
|
| 286 | - 'data-show-more' => __('Show more', 'site-reviews'), |
|
| 287 | - ]); |
|
| 288 | - $text = ltrim(substr($text, 0, $split)).$showMore; |
|
| 289 | - } |
|
| 290 | - return $text; |
|
| 291 | - } |
|
| 271 | + /** |
|
| 272 | + * @param string $text |
|
| 273 | + * @return string |
|
| 274 | + */ |
|
| 275 | + protected function getExcerpt($text) |
|
| 276 | + { |
|
| 277 | + $limit = intval($this->getOption('settings.reviews.excerpts_length', 55)); |
|
| 278 | + $split = extension_loaded('intl') |
|
| 279 | + ? $this->getExcerptIntlSplit($text, $limit) |
|
| 280 | + : $this->getExcerptSplit($text, $limit); |
|
| 281 | + $hiddenText = substr($text, $split); |
|
| 282 | + if (!empty($hiddenText)) { |
|
| 283 | + $showMore = glsr(Builder::class)->span($hiddenText, [ |
|
| 284 | + 'class' => 'glsr-hidden glsr-hidden-text', |
|
| 285 | + 'data-show-less' => __('Show less', 'site-reviews'), |
|
| 286 | + 'data-show-more' => __('Show more', 'site-reviews'), |
|
| 287 | + ]); |
|
| 288 | + $text = ltrim(substr($text, 0, $split)).$showMore; |
|
| 289 | + } |
|
| 290 | + return $text; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * @param string $text |
|
| 295 | - * @param int $limit |
|
| 296 | - * @return int |
|
| 297 | - */ |
|
| 298 | - protected function getExcerptIntlSplit($text, $limit) |
|
| 299 | - { |
|
| 300 | - $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
| 301 | - $words->setText($text); |
|
| 302 | - $count = 0; |
|
| 303 | - foreach ($words as $offset) { |
|
| 304 | - if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
| 305 | - continue; |
|
| 306 | - } |
|
| 307 | - ++$count; |
|
| 308 | - if ($count != $limit) { |
|
| 309 | - continue; |
|
| 310 | - } |
|
| 311 | - return $offset; |
|
| 312 | - } |
|
| 313 | - return strlen($text); |
|
| 314 | - } |
|
| 293 | + /** |
|
| 294 | + * @param string $text |
|
| 295 | + * @param int $limit |
|
| 296 | + * @return int |
|
| 297 | + */ |
|
| 298 | + protected function getExcerptIntlSplit($text, $limit) |
|
| 299 | + { |
|
| 300 | + $words = IntlRuleBasedBreakIterator::createWordInstance(''); |
|
| 301 | + $words->setText($text); |
|
| 302 | + $count = 0; |
|
| 303 | + foreach ($words as $offset) { |
|
| 304 | + if (IntlRuleBasedBreakIterator::WORD_NONE === $words->getRuleStatus()) { |
|
| 305 | + continue; |
|
| 306 | + } |
|
| 307 | + ++$count; |
|
| 308 | + if ($count != $limit) { |
|
| 309 | + continue; |
|
| 310 | + } |
|
| 311 | + return $offset; |
|
| 312 | + } |
|
| 313 | + return strlen($text); |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - /** |
|
| 317 | - * @param string $text |
|
| 318 | - * @param int $limit |
|
| 319 | - * @return int |
|
| 320 | - */ |
|
| 321 | - protected function getExcerptSplit($text, $limit) |
|
| 322 | - { |
|
| 323 | - if (str_word_count($text, 0) > $limit) { |
|
| 324 | - $words = array_keys(str_word_count($text, 2)); |
|
| 325 | - return $words[$limit]; |
|
| 326 | - } |
|
| 327 | - return strlen($text); |
|
| 328 | - } |
|
| 316 | + /** |
|
| 317 | + * @param string $text |
|
| 318 | + * @param int $limit |
|
| 319 | + * @return int |
|
| 320 | + */ |
|
| 321 | + protected function getExcerptSplit($text, $limit) |
|
| 322 | + { |
|
| 323 | + if (str_word_count($text, 0) > $limit) { |
|
| 324 | + $words = array_keys(str_word_count($text, 2)); |
|
| 325 | + return $words[$limit]; |
|
| 326 | + } |
|
| 327 | + return strlen($text); |
|
| 328 | + } |
|
| 329 | 329 | |
| 330 | - /** |
|
| 331 | - * @param string $path |
|
| 332 | - * @param mixed $fallback |
|
| 333 | - * @return mixed |
|
| 334 | - */ |
|
| 335 | - protected function getOption($path, $fallback = '') |
|
| 336 | - { |
|
| 337 | - if (array_key_exists($path, $this->options)) { |
|
| 338 | - return $this->options[$path]; |
|
| 339 | - } |
|
| 340 | - return $fallback; |
|
| 341 | - } |
|
| 330 | + /** |
|
| 331 | + * @param string $path |
|
| 332 | + * @param mixed $fallback |
|
| 333 | + * @return mixed |
|
| 334 | + */ |
|
| 335 | + protected function getOption($path, $fallback = '') |
|
| 336 | + { |
|
| 337 | + if (array_key_exists($path, $this->options)) { |
|
| 338 | + return $this->options[$path]; |
|
| 339 | + } |
|
| 340 | + return $fallback; |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - /** |
|
| 344 | - * @param string $key |
|
| 345 | - * @param string $value |
|
| 346 | - * @return bool |
|
| 347 | - */ |
|
| 348 | - protected function isHiddenOrEmpty($key, $value) |
|
| 349 | - { |
|
| 350 | - return $this->isHidden($key) || empty($value); |
|
| 351 | - } |
|
| 343 | + /** |
|
| 344 | + * @param string $key |
|
| 345 | + * @param string $value |
|
| 346 | + * @return bool |
|
| 347 | + */ |
|
| 348 | + protected function isHiddenOrEmpty($key, $value) |
|
| 349 | + { |
|
| 350 | + return $this->isHidden($key) || empty($value); |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * @param string $path |
|
| 355 | - * @return bool |
|
| 356 | - */ |
|
| 357 | - protected function isOptionEnabled($path) |
|
| 358 | - { |
|
| 359 | - return 'yes' == $this->getOption($path); |
|
| 360 | - } |
|
| 353 | + /** |
|
| 354 | + * @param string $path |
|
| 355 | + * @return bool |
|
| 356 | + */ |
|
| 357 | + protected function isOptionEnabled($path) |
|
| 358 | + { |
|
| 359 | + return 'yes' == $this->getOption($path); |
|
| 360 | + } |
|
| 361 | 361 | |
| 362 | - /** |
|
| 363 | - * @param string $text |
|
| 364 | - * @return string |
|
| 365 | - */ |
|
| 366 | - protected function normalizeText($text) |
|
| 367 | - { |
|
| 368 | - $text = wp_kses($text, wp_kses_allowed_html()); |
|
| 369 | - $text = convert_smilies(strip_shortcodes($text)); |
|
| 370 | - $text = str_replace(']]>', ']]>', $text); |
|
| 371 | - $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
| 372 | - if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
| 373 | - $text = $this->getExcerpt($text); |
|
| 374 | - } |
|
| 375 | - return wptexturize(nl2br($text)); |
|
| 376 | - } |
|
| 362 | + /** |
|
| 363 | + * @param string $text |
|
| 364 | + * @return string |
|
| 365 | + */ |
|
| 366 | + protected function normalizeText($text) |
|
| 367 | + { |
|
| 368 | + $text = wp_kses($text, wp_kses_allowed_html()); |
|
| 369 | + $text = convert_smilies(strip_shortcodes($text)); |
|
| 370 | + $text = str_replace(']]>', ']]>', $text); |
|
| 371 | + $text = preg_replace('/(\R){2,}/', '$1', $text); |
|
| 372 | + if ($this->isOptionEnabled('settings.reviews.excerpts')) { |
|
| 373 | + $text = $this->getExcerpt($text); |
|
| 374 | + } |
|
| 375 | + return wptexturize(nl2br($text)); |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * @return void |
|
| 380 | - */ |
|
| 381 | - protected function wrap(array &$renderedFields, Review $review) |
|
| 382 | - { |
|
| 383 | - $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review); |
|
| 384 | - array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
| 385 | - $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
| 386 | - if (empty($value)) { |
|
| 387 | - return; |
|
| 388 | - } |
|
| 389 | - $value = glsr(Builder::class)->div($value, [ |
|
| 390 | - 'class' => 'glsr-review-'.$key, |
|
| 391 | - ]); |
|
| 392 | - }); |
|
| 393 | - } |
|
| 378 | + /** |
|
| 379 | + * @return void |
|
| 380 | + */ |
|
| 381 | + protected function wrap(array &$renderedFields, Review $review) |
|
| 382 | + { |
|
| 383 | + $renderedFields = apply_filters('site-reviews/review/wrap', $renderedFields, $review); |
|
| 384 | + array_walk($renderedFields, function (&$value, $key) use ($review) { |
|
| 385 | + $value = apply_filters('site-reviews/review/wrap/'.$key, $value, $review); |
|
| 386 | + if (empty($value)) { |
|
| 387 | + return; |
|
| 388 | + } |
|
| 389 | + $value = glsr(Builder::class)->div($value, [ |
|
| 390 | + 'class' => 'glsr-review-'.$key, |
|
| 391 | + ]); |
|
| 392 | + }); |
|
| 393 | + } |
|
| 394 | 394 | } |
@@ -7,48 +7,48 @@ |
||
| 7 | 7 | |
| 8 | 8 | class ReviewHtml extends ArrayObject |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var Review |
|
| 12 | - */ |
|
| 13 | - public $review; |
|
| 10 | + /** |
|
| 11 | + * @var Review |
|
| 12 | + */ |
|
| 13 | + public $review; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - public $values; |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + public $values; |
|
| 19 | 19 | |
| 20 | - public function __construct(Review $review, array $values = []) |
|
| 21 | - { |
|
| 22 | - $this->review = $review; |
|
| 23 | - $this->values = $values; |
|
| 24 | - parent::__construct($values, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
| 25 | - } |
|
| 20 | + public function __construct(Review $review, array $values = []) |
|
| 21 | + { |
|
| 22 | + $this->review = $review; |
|
| 23 | + $this->values = $values; |
|
| 24 | + parent::__construct($values, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @return string|void |
|
| 29 | - */ |
|
| 30 | - public function __toString() |
|
| 31 | - { |
|
| 32 | - if (empty($this->values)) { |
|
| 33 | - return; |
|
| 34 | - } |
|
| 35 | - return glsr(Template::class)->build('templates/review', [ |
|
| 36 | - 'context' => $this->values, |
|
| 37 | - 'review' => $this->review, |
|
| 38 | - ]); |
|
| 39 | - } |
|
| 27 | + /** |
|
| 28 | + * @return string|void |
|
| 29 | + */ |
|
| 30 | + public function __toString() |
|
| 31 | + { |
|
| 32 | + if (empty($this->values)) { |
|
| 33 | + return; |
|
| 34 | + } |
|
| 35 | + return glsr(Template::class)->build('templates/review', [ |
|
| 36 | + 'context' => $this->values, |
|
| 37 | + 'review' => $this->review, |
|
| 38 | + ]); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param mixed $key |
|
| 43 | - * @return mixed |
|
| 44 | - */ |
|
| 45 | - public function offsetGet($key) |
|
| 46 | - { |
|
| 47 | - if (property_exists($this, $key)) { |
|
| 48 | - return $this->{$key}; |
|
| 49 | - } |
|
| 50 | - return array_key_exists($key, $this->values) |
|
| 51 | - ? $this->values[$key] |
|
| 52 | - : null; |
|
| 53 | - } |
|
| 41 | + /** |
|
| 42 | + * @param mixed $key |
|
| 43 | + * @return mixed |
|
| 44 | + */ |
|
| 45 | + public function offsetGet($key) |
|
| 46 | + { |
|
| 47 | + if (property_exists($this, $key)) { |
|
| 48 | + return $this->{$key}; |
|
| 49 | + } |
|
| 50 | + return array_key_exists($key, $this->values) |
|
| 51 | + ? $this->values[$key] |
|
| 52 | + : null; |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -7,135 +7,135 @@ |
||
| 7 | 7 | |
| 8 | 8 | class ReviewsHtml extends ArrayObject |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var array |
|
| 12 | - */ |
|
| 13 | - public $args; |
|
| 10 | + /** |
|
| 11 | + * @var array |
|
| 12 | + */ |
|
| 13 | + public $args; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var int |
|
| 17 | - */ |
|
| 18 | - public $max_num_pages; |
|
| 15 | + /** |
|
| 16 | + * @var int |
|
| 17 | + */ |
|
| 18 | + public $max_num_pages; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @var string |
|
| 22 | - */ |
|
| 23 | - public $pagination; |
|
| 20 | + /** |
|
| 21 | + * @var string |
|
| 22 | + */ |
|
| 23 | + public $pagination; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - public $reviews; |
|
| 25 | + /** |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + public $reviews; |
|
| 29 | 29 | |
| 30 | - public function __construct(array $reviews, $maxPageCount, array $args) |
|
| 31 | - { |
|
| 32 | - $this->args = $args; |
|
| 33 | - $this->max_num_pages = $maxPageCount; |
|
| 34 | - $this->reviews = $reviews; |
|
| 35 | - $this->pagination = $this->buildPagination(); |
|
| 36 | - parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
| 37 | - } |
|
| 30 | + public function __construct(array $reviews, $maxPageCount, array $args) |
|
| 31 | + { |
|
| 32 | + $this->args = $args; |
|
| 33 | + $this->max_num_pages = $maxPageCount; |
|
| 34 | + $this->reviews = $reviews; |
|
| 35 | + $this->pagination = $this->buildPagination(); |
|
| 36 | + parent::__construct($reviews, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function __toString() |
|
| 43 | - { |
|
| 44 | - return glsr(Template::class)->build('templates/reviews', [ |
|
| 45 | - 'args' => $this->args, |
|
| 46 | - 'context' => [ |
|
| 47 | - 'assigned_to' => $this->args['assigned_to'], |
|
| 48 | - 'category' => $this->args['category'], |
|
| 49 | - 'class' => $this->getClass(), |
|
| 50 | - 'id' => $this->args['id'], |
|
| 51 | - 'pagination' => $this->getPagination(), |
|
| 52 | - 'reviews' => $this->getReviews(), |
|
| 53 | - ], |
|
| 54 | - ]); |
|
| 55 | - } |
|
| 39 | + /** |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function __toString() |
|
| 43 | + { |
|
| 44 | + return glsr(Template::class)->build('templates/reviews', [ |
|
| 45 | + 'args' => $this->args, |
|
| 46 | + 'context' => [ |
|
| 47 | + 'assigned_to' => $this->args['assigned_to'], |
|
| 48 | + 'category' => $this->args['category'], |
|
| 49 | + 'class' => $this->getClass(), |
|
| 50 | + 'id' => $this->args['id'], |
|
| 51 | + 'pagination' => $this->getPagination(), |
|
| 52 | + 'reviews' => $this->getReviews(), |
|
| 53 | + ], |
|
| 54 | + ]); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * @return string |
|
| 59 | - */ |
|
| 60 | - public function getPagination() |
|
| 61 | - { |
|
| 62 | - return wp_validate_boolean($this->args['pagination']) |
|
| 63 | - ? $this->pagination |
|
| 64 | - : ''; |
|
| 65 | - } |
|
| 57 | + /** |
|
| 58 | + * @return string |
|
| 59 | + */ |
|
| 60 | + public function getPagination() |
|
| 61 | + { |
|
| 62 | + return wp_validate_boolean($this->args['pagination']) |
|
| 63 | + ? $this->pagination |
|
| 64 | + : ''; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @return string |
|
| 69 | - */ |
|
| 70 | - public function getReviews() |
|
| 71 | - { |
|
| 72 | - $html = empty($this->reviews) |
|
| 73 | - ? $this->getReviewsFallback() |
|
| 74 | - : implode(PHP_EOL, $this->reviews); |
|
| 75 | - $wrapper = '<div class="glsr-reviews">%s</div>'; |
|
| 76 | - $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper); |
|
| 77 | - return sprintf($wrapper, $html); |
|
| 78 | - } |
|
| 67 | + /** |
|
| 68 | + * @return string |
|
| 69 | + */ |
|
| 70 | + public function getReviews() |
|
| 71 | + { |
|
| 72 | + $html = empty($this->reviews) |
|
| 73 | + ? $this->getReviewsFallback() |
|
| 74 | + : implode(PHP_EOL, $this->reviews); |
|
| 75 | + $wrapper = '<div class="glsr-reviews">%s</div>'; |
|
| 76 | + $wrapper = apply_filters('site-reviews/reviews/reviews-wrapper', $wrapper); |
|
| 77 | + return sprintf($wrapper, $html); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @param mixed $key |
|
| 82 | - * @return mixed |
|
| 83 | - */ |
|
| 84 | - public function offsetGet($key) |
|
| 85 | - { |
|
| 86 | - if ('navigation' == $key) { |
|
| 87 | - glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
| 88 | - return $this->pagination; |
|
| 89 | - } |
|
| 90 | - if (property_exists($this, $key)) { |
|
| 91 | - return $this->{$key}; |
|
| 92 | - } |
|
| 93 | - return array_key_exists($key, $this->reviews) |
|
| 94 | - ? $this->reviews[$key] |
|
| 95 | - : null; |
|
| 96 | - } |
|
| 80 | + /** |
|
| 81 | + * @param mixed $key |
|
| 82 | + * @return mixed |
|
| 83 | + */ |
|
| 84 | + public function offsetGet($key) |
|
| 85 | + { |
|
| 86 | + if ('navigation' == $key) { |
|
| 87 | + glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.'; |
|
| 88 | + return $this->pagination; |
|
| 89 | + } |
|
| 90 | + if (property_exists($this, $key)) { |
|
| 91 | + return $this->{$key}; |
|
| 92 | + } |
|
| 93 | + return array_key_exists($key, $this->reviews) |
|
| 94 | + ? $this->reviews[$key] |
|
| 95 | + : null; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * @return string |
|
| 100 | - */ |
|
| 101 | - protected function buildPagination() |
|
| 102 | - { |
|
| 103 | - $html = glsr(Partial::class)->build('pagination', [ |
|
| 104 | - 'baseUrl' => glsr_get($this->args, 'pagedUrl'), |
|
| 105 | - 'current' => glsr_get($this->args, 'paged'), |
|
| 106 | - 'total' => $this->max_num_pages, |
|
| 107 | - ]); |
|
| 108 | - $html .= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']); |
|
| 109 | - $wrapper = '<div class="glsr-pagination">%s</div>'; |
|
| 110 | - $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper); |
|
| 111 | - return sprintf($wrapper, $html); |
|
| 112 | - } |
|
| 98 | + /** |
|
| 99 | + * @return string |
|
| 100 | + */ |
|
| 101 | + protected function buildPagination() |
|
| 102 | + { |
|
| 103 | + $html = glsr(Partial::class)->build('pagination', [ |
|
| 104 | + 'baseUrl' => glsr_get($this->args, 'pagedUrl'), |
|
| 105 | + 'current' => glsr_get($this->args, 'paged'), |
|
| 106 | + 'total' => $this->max_num_pages, |
|
| 107 | + ]); |
|
| 108 | + $html .= sprintf('<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json']); |
|
| 109 | + $wrapper = '<div class="glsr-pagination">%s</div>'; |
|
| 110 | + $wrapper = apply_filters('site-reviews/reviews/pagination-wrapper', $wrapper); |
|
| 111 | + return sprintf($wrapper, $html); |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * @return string |
|
| 116 | - */ |
|
| 117 | - protected function getClass() |
|
| 118 | - { |
|
| 119 | - $defaults = [ |
|
| 120 | - 'glsr-default', |
|
| 121 | - ]; |
|
| 122 | - if ('ajax' == $this->args['pagination']) { |
|
| 123 | - $defaults[] = 'glsr-ajax-pagination'; |
|
| 124 | - } |
|
| 125 | - $classes = explode(' ', $this->args['class']); |
|
| 126 | - $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
| 127 | - return implode(' ', $classes); |
|
| 128 | - } |
|
| 114 | + /** |
|
| 115 | + * @return string |
|
| 116 | + */ |
|
| 117 | + protected function getClass() |
|
| 118 | + { |
|
| 119 | + $defaults = [ |
|
| 120 | + 'glsr-default', |
|
| 121 | + ]; |
|
| 122 | + if ('ajax' == $this->args['pagination']) { |
|
| 123 | + $defaults[] = 'glsr-ajax-pagination'; |
|
| 124 | + } |
|
| 125 | + $classes = explode(' ', $this->args['class']); |
|
| 126 | + $classes = array_unique(array_merge($defaults, array_filter($classes))); |
|
| 127 | + return implode(' ', $classes); |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - protected function getReviewsFallback() |
|
| 134 | - { |
|
| 135 | - if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
| 136 | - $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
| 137 | - } |
|
| 138 | - $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
| 139 | - return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
| 140 | - } |
|
| 130 | + /** |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + protected function getReviewsFallback() |
|
| 134 | + { |
|
| 135 | + if (empty($this->args['fallback']) && glsr(OptionManager::class)->getBool('settings.reviews.fallback')) { |
|
| 136 | + $this->args['fallback'] = __('There are no reviews yet. Be the first one to write one.', 'site-reviews'); |
|
| 137 | + } |
|
| 138 | + $fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>'; |
|
| 139 | + return apply_filters('site-reviews/reviews/fallback', $fallback, $this->args); |
|
| 140 | + } |
|
| 141 | 141 | } |
@@ -8,237 +8,237 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Field |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - public $field; |
|
| 15 | - |
|
| 16 | - public function __construct(array $field = []) |
|
| 17 | - { |
|
| 18 | - $this->field = wp_parse_args($field, [ |
|
| 19 | - 'errors' => false, |
|
| 20 | - 'is_hidden' => false, |
|
| 21 | - 'is_multi' => false, |
|
| 22 | - 'is_public' => false, |
|
| 23 | - 'is_raw' => false, |
|
| 24 | - 'is_setting' => false, |
|
| 25 | - 'is_valid' => true, |
|
| 26 | - 'is_widget' => false, |
|
| 27 | - 'path' => '', |
|
| 28 | - ]); |
|
| 29 | - $this->normalize(); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @return string |
|
| 34 | - */ |
|
| 35 | - public function __toString() |
|
| 36 | - { |
|
| 37 | - return (string) $this->build(); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return void|string |
|
| 42 | - */ |
|
| 43 | - public function build() |
|
| 44 | - { |
|
| 45 | - if (!$this->field['is_valid']) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - if ($this->field['is_raw']) { |
|
| 49 | - return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
| 50 | - } |
|
| 51 | - if (!$this->field['is_setting']) { |
|
| 52 | - return $this->buildField(); |
|
| 53 | - } |
|
| 54 | - if (!$this->field['is_multi']) { |
|
| 55 | - return $this->buildSettingField(); |
|
| 56 | - } |
|
| 57 | - return $this->buildSettingMultiField(); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @return void |
|
| 62 | - */ |
|
| 63 | - public function render() |
|
| 64 | - { |
|
| 65 | - echo $this->build(); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @return string |
|
| 70 | - */ |
|
| 71 | - protected function buildField() |
|
| 72 | - { |
|
| 73 | - $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
| 74 | - 'context' => [ |
|
| 75 | - 'class' => $this->getFieldClass(), |
|
| 76 | - 'errors' => $this->getFieldErrors(), |
|
| 77 | - 'field' => glsr(Builder::class)->raw($this->field), |
|
| 78 | - 'label' => glsr(Builder::class)->label([ |
|
| 79 | - 'class' => 'glsr-'.$this->field['type'].'-label', |
|
| 80 | - 'for' => $this->field['id'], |
|
| 81 | - 'is_public' => $this->field['is_public'], |
|
| 82 | - 'text' => $this->field['label'].'<span></span>', |
|
| 83 | - 'type' => $this->field['type'], |
|
| 84 | - ]), |
|
| 85 | - ], |
|
| 86 | - ]); |
|
| 87 | - return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @return string |
|
| 92 | - */ |
|
| 93 | - protected function buildSettingField() |
|
| 94 | - { |
|
| 95 | - return glsr(Template::class)->build('partials/form/table-row', [ |
|
| 96 | - 'context' => [ |
|
| 97 | - 'class' => $this->getFieldClass(), |
|
| 98 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 99 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 100 | - ], |
|
| 101 | - ]); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @return string |
|
| 106 | - */ |
|
| 107 | - protected function buildSettingMultiField() |
|
| 108 | - { |
|
| 109 | - $dependsOn = $this->getFieldDependsOn(); |
|
| 110 | - unset($this->field['data-depends']); |
|
| 111 | - return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
| 112 | - 'context' => [ |
|
| 113 | - 'class' => $this->getFieldClass(), |
|
| 114 | - 'depends_on' => $dependsOn, |
|
| 115 | - 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 116 | - 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 117 | - 'legend' => $this->field['legend'], |
|
| 118 | - ], |
|
| 119 | - ]); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @return string |
|
| 124 | - */ |
|
| 125 | - protected function getFieldClass() |
|
| 126 | - { |
|
| 127 | - $classes = []; |
|
| 128 | - if (!empty($this->field['errors'])) { |
|
| 129 | - $classes[] = 'glsr-has-error'; |
|
| 130 | - } |
|
| 131 | - if ($this->field['is_hidden']) { |
|
| 132 | - $classes[] = 'hidden'; |
|
| 133 | - } |
|
| 134 | - if (!empty($this->field['required'])) { |
|
| 135 | - $classes[] = 'glsr-required'; |
|
| 136 | - } |
|
| 137 | - $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
| 138 | - return implode(' ', $classes); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return string |
|
| 143 | - */ |
|
| 144 | - protected function getFieldDependsOn() |
|
| 145 | - { |
|
| 146 | - return !empty($this->field['data-depends']) |
|
| 147 | - ? $this->field['data-depends'] |
|
| 148 | - : ''; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @return void|string |
|
| 153 | - */ |
|
| 154 | - protected function getFieldErrors() |
|
| 155 | - { |
|
| 156 | - if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
| 157 | - return; |
|
| 158 | - } |
|
| 159 | - $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
| 160 | - return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
| 161 | - }); |
|
| 162 | - return glsr(Template::class)->build('templates/form/field-errors', [ |
|
| 163 | - 'context' => [ |
|
| 164 | - 'errors' => $errors, |
|
| 165 | - ], |
|
| 166 | - ]); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return string |
|
| 171 | - */ |
|
| 172 | - protected function getFieldPrefix() |
|
| 173 | - { |
|
| 174 | - return $this->field['is_setting'] |
|
| 175 | - ? OptionManager::databaseKey() |
|
| 176 | - : Application::ID; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @return bool |
|
| 181 | - */ |
|
| 182 | - protected function isFieldValid() |
|
| 183 | - { |
|
| 184 | - $missingValues = []; |
|
| 185 | - $requiredValues = [ |
|
| 186 | - 'name', 'type', |
|
| 187 | - ]; |
|
| 188 | - foreach ($requiredValues as $value) { |
|
| 189 | - if (isset($this->field[$value])) { |
|
| 190 | - continue; |
|
| 191 | - } |
|
| 192 | - $missingValues[] = $value; |
|
| 193 | - $this->field['is_valid'] = false; |
|
| 194 | - } |
|
| 195 | - if (!empty($missingValues)) { |
|
| 196 | - glsr_log() |
|
| 197 | - ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
| 198 | - ->debug($this->field); |
|
| 199 | - } |
|
| 200 | - return $this->field['is_valid']; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @return void |
|
| 205 | - */ |
|
| 206 | - protected function normalize() |
|
| 207 | - { |
|
| 208 | - if (!$this->isFieldValid()) { |
|
| 209 | - return; |
|
| 210 | - } |
|
| 211 | - $this->field['path'] = $this->field['name']; |
|
| 212 | - $className = glsr(Helper::class)->buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
| 213 | - if (class_exists($className)) { |
|
| 214 | - $this->field = $className::merge($this->field); |
|
| 215 | - } |
|
| 216 | - $this->normalizeFieldId(); |
|
| 217 | - $this->normalizeFieldName(); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * @return void |
|
| 222 | - */ |
|
| 223 | - protected function normalizeFieldId() |
|
| 224 | - { |
|
| 225 | - if (isset($this->field['id']) || $this->field['is_raw']) { |
|
| 226 | - return; |
|
| 227 | - } |
|
| 228 | - $this->field['id'] = glsr(Helper::class)->convertPathToId( |
|
| 229 | - $this->field['path'], |
|
| 230 | - $this->getFieldPrefix() |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @return void |
|
| 236 | - */ |
|
| 237 | - protected function normalizeFieldName() |
|
| 238 | - { |
|
| 239 | - $this->field['name'] = glsr(Helper::class)->convertPathToName( |
|
| 240 | - $this->field['path'], |
|
| 241 | - $this->getFieldPrefix() |
|
| 242 | - ); |
|
| 243 | - } |
|
| 11 | + /** |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + public $field; |
|
| 15 | + |
|
| 16 | + public function __construct(array $field = []) |
|
| 17 | + { |
|
| 18 | + $this->field = wp_parse_args($field, [ |
|
| 19 | + 'errors' => false, |
|
| 20 | + 'is_hidden' => false, |
|
| 21 | + 'is_multi' => false, |
|
| 22 | + 'is_public' => false, |
|
| 23 | + 'is_raw' => false, |
|
| 24 | + 'is_setting' => false, |
|
| 25 | + 'is_valid' => true, |
|
| 26 | + 'is_widget' => false, |
|
| 27 | + 'path' => '', |
|
| 28 | + ]); |
|
| 29 | + $this->normalize(); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @return string |
|
| 34 | + */ |
|
| 35 | + public function __toString() |
|
| 36 | + { |
|
| 37 | + return (string) $this->build(); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return void|string |
|
| 42 | + */ |
|
| 43 | + public function build() |
|
| 44 | + { |
|
| 45 | + if (!$this->field['is_valid']) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + if ($this->field['is_raw']) { |
|
| 49 | + return glsr(Builder::class)->{$this->field['type']}($this->field); |
|
| 50 | + } |
|
| 51 | + if (!$this->field['is_setting']) { |
|
| 52 | + return $this->buildField(); |
|
| 53 | + } |
|
| 54 | + if (!$this->field['is_multi']) { |
|
| 55 | + return $this->buildSettingField(); |
|
| 56 | + } |
|
| 57 | + return $this->buildSettingMultiField(); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @return void |
|
| 62 | + */ |
|
| 63 | + public function render() |
|
| 64 | + { |
|
| 65 | + echo $this->build(); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @return string |
|
| 70 | + */ |
|
| 71 | + protected function buildField() |
|
| 72 | + { |
|
| 73 | + $field = glsr(Template::class)->build('templates/form/field_'.$this->field['type'], [ |
|
| 74 | + 'context' => [ |
|
| 75 | + 'class' => $this->getFieldClass(), |
|
| 76 | + 'errors' => $this->getFieldErrors(), |
|
| 77 | + 'field' => glsr(Builder::class)->raw($this->field), |
|
| 78 | + 'label' => glsr(Builder::class)->label([ |
|
| 79 | + 'class' => 'glsr-'.$this->field['type'].'-label', |
|
| 80 | + 'for' => $this->field['id'], |
|
| 81 | + 'is_public' => $this->field['is_public'], |
|
| 82 | + 'text' => $this->field['label'].'<span></span>', |
|
| 83 | + 'type' => $this->field['type'], |
|
| 84 | + ]), |
|
| 85 | + ], |
|
| 86 | + ]); |
|
| 87 | + return apply_filters('site-reviews/rendered/field', $field, $this->field['type'], $this->field); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @return string |
|
| 92 | + */ |
|
| 93 | + protected function buildSettingField() |
|
| 94 | + { |
|
| 95 | + return glsr(Template::class)->build('partials/form/table-row', [ |
|
| 96 | + 'context' => [ |
|
| 97 | + 'class' => $this->getFieldClass(), |
|
| 98 | + 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 99 | + 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 100 | + ], |
|
| 101 | + ]); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @return string |
|
| 106 | + */ |
|
| 107 | + protected function buildSettingMultiField() |
|
| 108 | + { |
|
| 109 | + $dependsOn = $this->getFieldDependsOn(); |
|
| 110 | + unset($this->field['data-depends']); |
|
| 111 | + return glsr(Template::class)->build('partials/form/table-row-multiple', [ |
|
| 112 | + 'context' => [ |
|
| 113 | + 'class' => $this->getFieldClass(), |
|
| 114 | + 'depends_on' => $dependsOn, |
|
| 115 | + 'field' => glsr(Builder::class)->{$this->field['type']}($this->field), |
|
| 116 | + 'label' => glsr(Builder::class)->label($this->field['legend'], ['for' => $this->field['id']]), |
|
| 117 | + 'legend' => $this->field['legend'], |
|
| 118 | + ], |
|
| 119 | + ]); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @return string |
|
| 124 | + */ |
|
| 125 | + protected function getFieldClass() |
|
| 126 | + { |
|
| 127 | + $classes = []; |
|
| 128 | + if (!empty($this->field['errors'])) { |
|
| 129 | + $classes[] = 'glsr-has-error'; |
|
| 130 | + } |
|
| 131 | + if ($this->field['is_hidden']) { |
|
| 132 | + $classes[] = 'hidden'; |
|
| 133 | + } |
|
| 134 | + if (!empty($this->field['required'])) { |
|
| 135 | + $classes[] = 'glsr-required'; |
|
| 136 | + } |
|
| 137 | + $classes = apply_filters('site-reviews/rendered/field/classes', $classes, $this->field); |
|
| 138 | + return implode(' ', $classes); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return string |
|
| 143 | + */ |
|
| 144 | + protected function getFieldDependsOn() |
|
| 145 | + { |
|
| 146 | + return !empty($this->field['data-depends']) |
|
| 147 | + ? $this->field['data-depends'] |
|
| 148 | + : ''; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @return void|string |
|
| 153 | + */ |
|
| 154 | + protected function getFieldErrors() |
|
| 155 | + { |
|
| 156 | + if (empty($this->field['errors']) || !is_array($this->field['errors'])) { |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | + $errors = array_reduce($this->field['errors'], function ($carry, $error) { |
|
| 160 | + return $carry.glsr(Builder::class)->span($error, ['class' => 'glsr-field-error']); |
|
| 161 | + }); |
|
| 162 | + return glsr(Template::class)->build('templates/form/field-errors', [ |
|
| 163 | + 'context' => [ |
|
| 164 | + 'errors' => $errors, |
|
| 165 | + ], |
|
| 166 | + ]); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return string |
|
| 171 | + */ |
|
| 172 | + protected function getFieldPrefix() |
|
| 173 | + { |
|
| 174 | + return $this->field['is_setting'] |
|
| 175 | + ? OptionManager::databaseKey() |
|
| 176 | + : Application::ID; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @return bool |
|
| 181 | + */ |
|
| 182 | + protected function isFieldValid() |
|
| 183 | + { |
|
| 184 | + $missingValues = []; |
|
| 185 | + $requiredValues = [ |
|
| 186 | + 'name', 'type', |
|
| 187 | + ]; |
|
| 188 | + foreach ($requiredValues as $value) { |
|
| 189 | + if (isset($this->field[$value])) { |
|
| 190 | + continue; |
|
| 191 | + } |
|
| 192 | + $missingValues[] = $value; |
|
| 193 | + $this->field['is_valid'] = false; |
|
| 194 | + } |
|
| 195 | + if (!empty($missingValues)) { |
|
| 196 | + glsr_log() |
|
| 197 | + ->warning('Field is missing: '.implode(', ', $missingValues)) |
|
| 198 | + ->debug($this->field); |
|
| 199 | + } |
|
| 200 | + return $this->field['is_valid']; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @return void |
|
| 205 | + */ |
|
| 206 | + protected function normalize() |
|
| 207 | + { |
|
| 208 | + if (!$this->isFieldValid()) { |
|
| 209 | + return; |
|
| 210 | + } |
|
| 211 | + $this->field['path'] = $this->field['name']; |
|
| 212 | + $className = glsr(Helper::class)->buildClassName($this->field['type'], __NAMESPACE__.'\Fields'); |
|
| 213 | + if (class_exists($className)) { |
|
| 214 | + $this->field = $className::merge($this->field); |
|
| 215 | + } |
|
| 216 | + $this->normalizeFieldId(); |
|
| 217 | + $this->normalizeFieldName(); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * @return void |
|
| 222 | + */ |
|
| 223 | + protected function normalizeFieldId() |
|
| 224 | + { |
|
| 225 | + if (isset($this->field['id']) || $this->field['is_raw']) { |
|
| 226 | + return; |
|
| 227 | + } |
|
| 228 | + $this->field['id'] = glsr(Helper::class)->convertPathToId( |
|
| 229 | + $this->field['path'], |
|
| 230 | + $this->getFieldPrefix() |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @return void |
|
| 236 | + */ |
|
| 237 | + protected function normalizeFieldName() |
|
| 238 | + { |
|
| 239 | + $this->field['name'] = glsr(Helper::class)->convertPathToName( |
|
| 240 | + $this->field['path'], |
|
| 241 | + $this->getFieldPrefix() |
|
| 242 | + ); |
|
| 243 | + } |
|
| 244 | 244 | } |
@@ -6,78 +6,78 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Template |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @param string $templatePath |
|
| 11 | - * @return void|string |
|
| 12 | - */ |
|
| 13 | - public function build($templatePath, array $data = []) |
|
| 14 | - { |
|
| 15 | - $data = $this->normalize($data); |
|
| 16 | - ob_start(); |
|
| 17 | - glsr()->render($templatePath, $data); |
|
| 18 | - $template = ob_get_clean(); |
|
| 19 | - $path = glsr(Helper::class)->removePrefix('templates/', $templatePath); |
|
| 20 | - $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
| 21 | - $template = $this->interpolate($template, $data['context'], $path); |
|
| 22 | - $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
| 23 | - $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
| 24 | - return $template; |
|
| 25 | - } |
|
| 9 | + /** |
|
| 10 | + * @param string $templatePath |
|
| 11 | + * @return void|string |
|
| 12 | + */ |
|
| 13 | + public function build($templatePath, array $data = []) |
|
| 14 | + { |
|
| 15 | + $data = $this->normalize($data); |
|
| 16 | + ob_start(); |
|
| 17 | + glsr()->render($templatePath, $data); |
|
| 18 | + $template = ob_get_clean(); |
|
| 19 | + $path = glsr(Helper::class)->removePrefix('templates/', $templatePath); |
|
| 20 | + $template = apply_filters('site-reviews/build/template/'.$path, $template, $data); |
|
| 21 | + $template = $this->interpolate($template, $data['context'], $path); |
|
| 22 | + $template = apply_filters('site-reviews/rendered/template', $template, $templatePath, $data); |
|
| 23 | + $template = apply_filters('site-reviews/rendered/template/'.$path, $template, $data); |
|
| 24 | + return $template; |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Interpolate context values into template placeholders. |
|
| 29 | - * @param string $template |
|
| 30 | - * @param string $templatePath |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function interpolate($template, array $context = [], $templatePath) |
|
| 34 | - { |
|
| 35 | - $context = $this->normalizeContext($context); |
|
| 36 | - $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template); |
|
| 37 | - foreach ($context as $key => $value) { |
|
| 38 | - $template = strtr( |
|
| 39 | - $template, |
|
| 40 | - array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
| 41 | - ); |
|
| 42 | - } |
|
| 43 | - return trim($template); |
|
| 44 | - } |
|
| 27 | + /** |
|
| 28 | + * Interpolate context values into template placeholders. |
|
| 29 | + * @param string $template |
|
| 30 | + * @param string $templatePath |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function interpolate($template, array $context = [], $templatePath) |
|
| 34 | + { |
|
| 35 | + $context = $this->normalizeContext($context); |
|
| 36 | + $context = apply_filters('site-reviews/interpolate/'.$templatePath, $context, $template); |
|
| 37 | + foreach ($context as $key => $value) { |
|
| 38 | + $template = strtr( |
|
| 39 | + $template, |
|
| 40 | + array_fill_keys(['{'.$key.'}', '{{ '.$key.' }}'], $value) |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | + return trim($template); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * @param string $templatePath |
|
| 48 | - * @return void|string |
|
| 49 | - */ |
|
| 50 | - public function render($templatePath, array $data = []) |
|
| 51 | - { |
|
| 52 | - echo $this->build($templatePath, $data); |
|
| 53 | - } |
|
| 46 | + /** |
|
| 47 | + * @param string $templatePath |
|
| 48 | + * @return void|string |
|
| 49 | + */ |
|
| 50 | + public function render($templatePath, array $data = []) |
|
| 51 | + { |
|
| 52 | + echo $this->build($templatePath, $data); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @return array |
|
| 57 | - */ |
|
| 58 | - protected function normalize(array $data) |
|
| 59 | - { |
|
| 60 | - $arrayKeys = ['context', 'globals']; |
|
| 61 | - $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
| 62 | - foreach ($arrayKeys as $key) { |
|
| 63 | - if (is_array($data[$key])) { |
|
| 64 | - continue; |
|
| 65 | - } |
|
| 66 | - $data[$key] = []; |
|
| 67 | - } |
|
| 68 | - return $data; |
|
| 69 | - } |
|
| 55 | + /** |
|
| 56 | + * @return array |
|
| 57 | + */ |
|
| 58 | + protected function normalize(array $data) |
|
| 59 | + { |
|
| 60 | + $arrayKeys = ['context', 'globals']; |
|
| 61 | + $data = wp_parse_args($data, array_fill_keys($arrayKeys, [])); |
|
| 62 | + foreach ($arrayKeys as $key) { |
|
| 63 | + if (is_array($data[$key])) { |
|
| 64 | + continue; |
|
| 65 | + } |
|
| 66 | + $data[$key] = []; |
|
| 67 | + } |
|
| 68 | + return $data; |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - protected function normalizeContext(array $context) |
|
| 75 | - { |
|
| 76 | - $context = array_filter($context, function ($value) { |
|
| 77 | - return !is_array($value) && !is_object($value); |
|
| 78 | - }); |
|
| 79 | - return array_map(function ($value) { |
|
| 80 | - return (string) $value; |
|
| 81 | - }, $context); |
|
| 82 | - } |
|
| 71 | + /** |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + protected function normalizeContext(array $context) |
|
| 75 | + { |
|
| 76 | + $context = array_filter($context, function ($value) { |
|
| 77 | + return !is_array($value) && !is_object($value); |
|
| 78 | + }); |
|
| 79 | + return array_map(function ($value) { |
|
| 80 | + return (string) $value; |
|
| 81 | + }, $context); |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -8,114 +8,114 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Notice |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * @param string $type |
|
| 13 | - * @param string|array|WP_Error $message |
|
| 14 | - * @return void |
|
| 15 | - */ |
|
| 16 | - public function add($type, $message, array $args = []) |
|
| 17 | - { |
|
| 18 | - if (empty(array_filter([$message, $type]))) { |
|
| 19 | - return; |
|
| 20 | - } |
|
| 21 | - $args['message'] = $message; |
|
| 22 | - $args['type'] = $type; |
|
| 23 | - add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
| 24 | - } |
|
| 11 | + /** |
|
| 12 | + * @param string $type |
|
| 13 | + * @param string|array|WP_Error $message |
|
| 14 | + * @return void |
|
| 15 | + */ |
|
| 16 | + public function add($type, $message, array $args = []) |
|
| 17 | + { |
|
| 18 | + if (empty(array_filter([$message, $type]))) { |
|
| 19 | + return; |
|
| 20 | + } |
|
| 21 | + $args['message'] = $message; |
|
| 22 | + $args['type'] = $type; |
|
| 23 | + add_settings_error(Application::ID, '', json_encode($this->normalize($args))); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @param string|array|WP_Error $message |
|
| 28 | - * @return void |
|
| 29 | - */ |
|
| 30 | - public function addError($message, array $args = []) |
|
| 31 | - { |
|
| 32 | - $this->add('error', $message, $args); |
|
| 33 | - } |
|
| 26 | + /** |
|
| 27 | + * @param string|array|WP_Error $message |
|
| 28 | + * @return void |
|
| 29 | + */ |
|
| 30 | + public function addError($message, array $args = []) |
|
| 31 | + { |
|
| 32 | + $this->add('error', $message, $args); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param string|array|WP_Error $message |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function addSuccess($message, array $args = []) |
|
| 40 | - { |
|
| 41 | - $this->add('success', $message, $args); |
|
| 42 | - } |
|
| 35 | + /** |
|
| 36 | + * @param string|array|WP_Error $message |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function addSuccess($message, array $args = []) |
|
| 40 | + { |
|
| 41 | + $this->add('success', $message, $args); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @param string|array|WP_Error $message |
|
| 46 | - * @return void |
|
| 47 | - */ |
|
| 48 | - public function addWarning($message, array $args = []) |
|
| 49 | - { |
|
| 50 | - $this->add('warning', $message, $args); |
|
| 51 | - } |
|
| 44 | + /** |
|
| 45 | + * @param string|array|WP_Error $message |
|
| 46 | + * @return void |
|
| 47 | + */ |
|
| 48 | + public function addWarning($message, array $args = []) |
|
| 49 | + { |
|
| 50 | + $this->add('warning', $message, $args); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * @return static |
|
| 55 | - */ |
|
| 56 | - public function clear() |
|
| 57 | - { |
|
| 58 | - global $wp_settings_errors; |
|
| 59 | - $wp_settings_errors = []; |
|
| 60 | - delete_transient('settings_errors'); |
|
| 61 | - return $this; |
|
| 62 | - } |
|
| 53 | + /** |
|
| 54 | + * @return static |
|
| 55 | + */ |
|
| 56 | + public function clear() |
|
| 57 | + { |
|
| 58 | + global $wp_settings_errors; |
|
| 59 | + $wp_settings_errors = []; |
|
| 60 | + delete_transient('settings_errors'); |
|
| 61 | + return $this; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @return string |
|
| 66 | - */ |
|
| 67 | - public function get() |
|
| 68 | - { |
|
| 69 | - $notices = array_map('unserialize', |
|
| 70 | - array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
| 71 | - ); |
|
| 72 | - if (empty($notices)) { |
|
| 73 | - return; |
|
| 74 | - } |
|
| 75 | - return array_reduce($notices, function ($carry, $notice) { |
|
| 76 | - return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
| 77 | - }); |
|
| 78 | - } |
|
| 64 | + /** |
|
| 65 | + * @return string |
|
| 66 | + */ |
|
| 67 | + public function get() |
|
| 68 | + { |
|
| 69 | + $notices = array_map('unserialize', |
|
| 70 | + array_unique(array_map('serialize', get_settings_errors(Application::ID))) |
|
| 71 | + ); |
|
| 72 | + if (empty($notices)) { |
|
| 73 | + return; |
|
| 74 | + } |
|
| 75 | + return array_reduce($notices, function ($carry, $notice) { |
|
| 76 | + return $carry.$this->buildNotice(json_decode($notice['message'], true)); |
|
| 77 | + }); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @return string |
|
| 82 | - */ |
|
| 83 | - protected function buildNotice(array $args) |
|
| 84 | - { |
|
| 85 | - $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
| 86 | - return $carry.glsr(Builder::class)->p($message); |
|
| 87 | - }); |
|
| 88 | - $class = 'notice notice-'.$args['type']; |
|
| 89 | - if ($args['inline']) { |
|
| 90 | - $class .= ' inline'; |
|
| 91 | - } |
|
| 92 | - if ($args['dismissible']) { |
|
| 93 | - $class .= ' is-dismissible'; |
|
| 94 | - } |
|
| 95 | - return glsr(Builder::class)->div($messages, [ |
|
| 96 | - 'class' => $class, |
|
| 97 | - ]); |
|
| 98 | - } |
|
| 80 | + /** |
|
| 81 | + * @return string |
|
| 82 | + */ |
|
| 83 | + protected function buildNotice(array $args) |
|
| 84 | + { |
|
| 85 | + $messages = array_reduce($args['messages'], function ($carry, $message) { |
|
| 86 | + return $carry.glsr(Builder::class)->p($message); |
|
| 87 | + }); |
|
| 88 | + $class = 'notice notice-'.$args['type']; |
|
| 89 | + if ($args['inline']) { |
|
| 90 | + $class .= ' inline'; |
|
| 91 | + } |
|
| 92 | + if ($args['dismissible']) { |
|
| 93 | + $class .= ' is-dismissible'; |
|
| 94 | + } |
|
| 95 | + return glsr(Builder::class)->div($messages, [ |
|
| 96 | + 'class' => $class, |
|
| 97 | + ]); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @return array |
|
| 102 | - */ |
|
| 103 | - protected function normalize(array $args) |
|
| 104 | - { |
|
| 105 | - $defaults = [ |
|
| 106 | - 'dismissible' => true, |
|
| 107 | - 'inline' => true, |
|
| 108 | - 'message' => '', |
|
| 109 | - 'type' => '', |
|
| 110 | - ]; |
|
| 111 | - $args = shortcode_atts($defaults, $args); |
|
| 112 | - if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
| 113 | - $args['type'] = 'success'; |
|
| 114 | - } |
|
| 115 | - $args['messages'] = is_wp_error($args['message']) |
|
| 116 | - ? (array) $args['message']->get_error_message() |
|
| 117 | - : (array) $args['message']; |
|
| 118 | - unset($args['message']); |
|
| 119 | - return $args; |
|
| 120 | - } |
|
| 100 | + /** |
|
| 101 | + * @return array |
|
| 102 | + */ |
|
| 103 | + protected function normalize(array $args) |
|
| 104 | + { |
|
| 105 | + $defaults = [ |
|
| 106 | + 'dismissible' => true, |
|
| 107 | + 'inline' => true, |
|
| 108 | + 'message' => '', |
|
| 109 | + 'type' => '', |
|
| 110 | + ]; |
|
| 111 | + $args = shortcode_atts($defaults, $args); |
|
| 112 | + if (!in_array($args['type'], ['error', 'warning', 'success'])) { |
|
| 113 | + $args['type'] = 'success'; |
|
| 114 | + } |
|
| 115 | + $args['messages'] = is_wp_error($args['message']) |
|
| 116 | + ? (array) $args['message']->get_error_message() |
|
| 117 | + : (array) $args['message']; |
|
| 118 | + unset($args['message']); |
|
| 119 | + return $args; |
|
| 120 | + } |
|
| 121 | 121 | } |
@@ -7,82 +7,82 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Akismet |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @return bool |
|
| 12 | - */ |
|
| 13 | - public function isSpam(array $review) |
|
| 14 | - { |
|
| 15 | - if (!$this->isActive()) { |
|
| 16 | - return false; |
|
| 17 | - } |
|
| 18 | - $submission = [ |
|
| 19 | - 'blog' => get_option('home'), |
|
| 20 | - 'blog_charset' => get_option('blog_charset'), |
|
| 21 | - 'blog_lang' => get_locale(), |
|
| 22 | - 'comment_author' => $review['name'], |
|
| 23 | - 'comment_author_email' => $review['email'], |
|
| 24 | - 'comment_content' => $review['title']."\n\n".$review['content'], |
|
| 25 | - 'comment_type' => 'review', |
|
| 26 | - 'referrer' => filter_input(INPUT_SERVER, 'HTTP_REFERER'), |
|
| 27 | - 'user_agent' => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), |
|
| 28 | - 'user_ip' => $review['ip_address'], |
|
| 29 | - // 'user_role' => 'administrator', |
|
| 30 | - // 'is_test' => 1, |
|
| 31 | - ]; |
|
| 32 | - foreach ($_SERVER as $key => $value) { |
|
| 33 | - if (is_array($value) || in_array($key, ['HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW'])) { |
|
| 34 | - continue; |
|
| 35 | - } |
|
| 36 | - $submission[$key] = $value; |
|
| 37 | - } |
|
| 38 | - return $this->check(apply_filters('site-reviews/akismet/submission', $submission, $review)); |
|
| 39 | - } |
|
| 10 | + /** |
|
| 11 | + * @return bool |
|
| 12 | + */ |
|
| 13 | + public function isSpam(array $review) |
|
| 14 | + { |
|
| 15 | + if (!$this->isActive()) { |
|
| 16 | + return false; |
|
| 17 | + } |
|
| 18 | + $submission = [ |
|
| 19 | + 'blog' => get_option('home'), |
|
| 20 | + 'blog_charset' => get_option('blog_charset'), |
|
| 21 | + 'blog_lang' => get_locale(), |
|
| 22 | + 'comment_author' => $review['name'], |
|
| 23 | + 'comment_author_email' => $review['email'], |
|
| 24 | + 'comment_content' => $review['title']."\n\n".$review['content'], |
|
| 25 | + 'comment_type' => 'review', |
|
| 26 | + 'referrer' => filter_input(INPUT_SERVER, 'HTTP_REFERER'), |
|
| 27 | + 'user_agent' => filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), |
|
| 28 | + 'user_ip' => $review['ip_address'], |
|
| 29 | + // 'user_role' => 'administrator', |
|
| 30 | + // 'is_test' => 1, |
|
| 31 | + ]; |
|
| 32 | + foreach ($_SERVER as $key => $value) { |
|
| 33 | + if (is_array($value) || in_array($key, ['HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW'])) { |
|
| 34 | + continue; |
|
| 35 | + } |
|
| 36 | + $submission[$key] = $value; |
|
| 37 | + } |
|
| 38 | + return $this->check(apply_filters('site-reviews/akismet/submission', $submission, $review)); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - protected function check(array $submission) |
|
| 45 | - { |
|
| 46 | - $response = AkismetPlugin::http_post($this->buildQuery($submission), 'comment-check'); |
|
| 47 | - return apply_filters('site-reviews/akismet/is-spam', |
|
| 48 | - 'true' == $response[1], |
|
| 49 | - $submission, |
|
| 50 | - $response |
|
| 51 | - ); |
|
| 52 | - } |
|
| 41 | + /** |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + protected function check(array $submission) |
|
| 45 | + { |
|
| 46 | + $response = AkismetPlugin::http_post($this->buildQuery($submission), 'comment-check'); |
|
| 47 | + return apply_filters('site-reviews/akismet/is-spam', |
|
| 48 | + 'true' == $response[1], |
|
| 49 | + $submission, |
|
| 50 | + $response |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - protected function buildQuery(array $data) |
|
| 58 | - { |
|
| 59 | - $query = []; |
|
| 60 | - foreach ($data as $key => $value) { |
|
| 61 | - if (is_array($value) || is_object($value)) { |
|
| 62 | - continue; |
|
| 63 | - } |
|
| 64 | - if (false === $value) { |
|
| 65 | - $value = '0'; |
|
| 66 | - } |
|
| 67 | - $value = trim($value); |
|
| 68 | - if (!strlen($value)) { |
|
| 69 | - continue; |
|
| 70 | - } |
|
| 71 | - $query[] = urlencode($key).'='.urlencode($value); |
|
| 72 | - } |
|
| 73 | - return implode('&', $query); |
|
| 74 | - } |
|
| 54 | + /** |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + protected function buildQuery(array $data) |
|
| 58 | + { |
|
| 59 | + $query = []; |
|
| 60 | + foreach ($data as $key => $value) { |
|
| 61 | + if (is_array($value) || is_object($value)) { |
|
| 62 | + continue; |
|
| 63 | + } |
|
| 64 | + if (false === $value) { |
|
| 65 | + $value = '0'; |
|
| 66 | + } |
|
| 67 | + $value = trim($value); |
|
| 68 | + if (!strlen($value)) { |
|
| 69 | + continue; |
|
| 70 | + } |
|
| 71 | + $query[] = urlencode($key).'='.urlencode($value); |
|
| 72 | + } |
|
| 73 | + return implode('&', $query); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * @return bool |
|
| 78 | - */ |
|
| 79 | - protected function isActive() |
|
| 80 | - { |
|
| 81 | - $check = !glsr(OptionManager::class)->getBool('settings.submissions.akismet') |
|
| 82 | - || !is_callable(['Akismet', 'get_api_key']) |
|
| 83 | - || !is_callable(['Akismet', 'http_post']) |
|
| 84 | - ? false |
|
| 85 | - : (bool) AkismetPlugin::get_api_key(); |
|
| 86 | - return apply_filters('site-reviews/akismet/is-active', $check); |
|
| 87 | - } |
|
| 76 | + /** |
|
| 77 | + * @return bool |
|
| 78 | + */ |
|
| 79 | + protected function isActive() |
|
| 80 | + { |
|
| 81 | + $check = !glsr(OptionManager::class)->getBool('settings.submissions.akismet') |
|
| 82 | + || !is_callable(['Akismet', 'get_api_key']) |
|
| 83 | + || !is_callable(['Akismet', 'http_post']) |
|
| 84 | + ? false |
|
| 85 | + : (bool) AkismetPlugin::get_api_key(); |
|
| 86 | + return apply_filters('site-reviews/akismet/is-active', $check); |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -10,216 +10,216 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Session |
| 12 | 12 | { |
| 13 | - const DELIMITER = '__'; |
|
| 14 | - |
|
| 15 | - const SESSION_COOKIE = '_glsr_session'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var int |
|
| 19 | - */ |
|
| 20 | - protected $expiryTimestamp; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var int |
|
| 24 | - */ |
|
| 25 | - protected $expiryTimestampReset; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var array |
|
| 29 | - */ |
|
| 30 | - protected $sessionData; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 35 | - protected $sessionId; |
|
| 36 | - |
|
| 37 | - public function __construct() |
|
| 38 | - { |
|
| 39 | - $cookieId = filter_input(INPUT_COOKIE, static::SESSION_COOKIE); |
|
| 40 | - if (false !== strpos($cookieId, static::DELIMITER)) { |
|
| 41 | - $cookie = explode(static::DELIMITER, stripslashes($cookieId)); |
|
| 42 | - $this->sessionId = preg_replace('/[^A-Za-z0-9_]/', '', $cookie[0]); |
|
| 43 | - $this->expiryTimestamp = absint($cookie[1]); |
|
| 44 | - $this->expiryTimestampReset = absint($cookie[2]); |
|
| 45 | - if (time() > $this->expiryTimestampReset) { |
|
| 46 | - $this->setCookieExpiration(); |
|
| 47 | - } |
|
| 48 | - } else { |
|
| 49 | - $this->sessionId = $this->generateSessionId(); |
|
| 50 | - $this->setCookieExpiration(); |
|
| 51 | - } |
|
| 52 | - $this->getSessionData(); |
|
| 53 | - $this->setCookie(); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function clear() |
|
| 60 | - { |
|
| 61 | - $this->setCookieExpiration(); |
|
| 62 | - $this->regenerateSessionId('and delete session!'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return int|false |
|
| 67 | - */ |
|
| 68 | - public function deleteAllSessions() |
|
| 69 | - { |
|
| 70 | - return glsr(SqlQueries::class)->deleteAllSessions(static::SESSION_COOKIE); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param int $limit |
|
| 75 | - * @return void |
|
| 76 | - */ |
|
| 77 | - public function deleteExpiredSessions($limit = 1000) |
|
| 78 | - { |
|
| 79 | - if ($expiredSessions = implode("','", $this->getExpiredSessions($limit))) { |
|
| 80 | - glsr(SqlQueries::class)->deleteExpiredSessions($expiredSessions); |
|
| 81 | - } |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $key |
|
| 86 | - * @param string|array $fallback |
|
| 87 | - * @param bool|string $unset |
|
| 88 | - * @return string|array |
|
| 89 | - */ |
|
| 90 | - public function get($key, $fallback = '', $unset = false) |
|
| 91 | - { |
|
| 92 | - $key = sanitize_key($key); |
|
| 93 | - $value = isset($this->sessionData[$key]) |
|
| 94 | - ? maybe_unserialize($this->sessionData[$key]) |
|
| 95 | - : $fallback; |
|
| 96 | - if (isset($this->sessionData[$key]) && $unset) { |
|
| 97 | - unset($this->sessionData[$key]); |
|
| 98 | - $this->updateSession(); |
|
| 99 | - } |
|
| 100 | - return $value; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param string $key |
|
| 105 | - * @param mixed $value |
|
| 106 | - * @return mixed |
|
| 107 | - */ |
|
| 108 | - public function set($key, $value) |
|
| 109 | - { |
|
| 110 | - $key = sanitize_key($key); |
|
| 111 | - $this->sessionData[$key] = maybe_serialize($value); |
|
| 112 | - $this->updateSession(); |
|
| 113 | - return $this->sessionData[$key]; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * @return void |
|
| 118 | - */ |
|
| 119 | - protected function createSession() |
|
| 120 | - { |
|
| 121 | - add_option($this->getSessionId(), $this->sessionData, '', false); |
|
| 122 | - add_option($this->getSessionId('expires'), $this->expiryTimestamp, '', false); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @return void |
|
| 127 | - */ |
|
| 128 | - protected function deleteSession() |
|
| 129 | - { |
|
| 130 | - delete_option($this->getSessionId()); |
|
| 131 | - delete_option($this->getSessionId('expires')); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * @return string |
|
| 136 | - */ |
|
| 137 | - protected function generateSessionId() |
|
| 138 | - { |
|
| 139 | - return md5(( new PasswordHash(8, false))->get_random_bytes(32)); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param int $limit |
|
| 144 | - * @return array |
|
| 145 | - */ |
|
| 146 | - protected function getExpiredSessions($limit) |
|
| 147 | - { |
|
| 148 | - $expiredSessions = []; |
|
| 149 | - $sessions = glsr(SqlQueries::class)->getExpiredSessions(static::SESSION_COOKIE, absint($limit)); |
|
| 150 | - if (!empty($sessions)) { |
|
| 151 | - $now = time(); |
|
| 152 | - foreach ($sessions as $session) { |
|
| 153 | - if ($now <= $session->expiration) { |
|
| 154 | - continue; |
|
| 155 | - } |
|
| 156 | - $expiredSessions[] = $session->name; |
|
| 157 | - $expiredSessions[] = str_replace('_expires_', '_', $session->name); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - return $expiredSessions; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param string $separator |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - protected function getSessionId($separator = '') |
|
| 168 | - { |
|
| 169 | - return implode('_', array_filter([static::SESSION_COOKIE, $separator, $this->sessionId])); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @return array |
|
| 174 | - */ |
|
| 175 | - protected function getSessionData() |
|
| 176 | - { |
|
| 177 | - return $this->sessionData = (array) get_option($this->getSessionId(), []); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param bool|string $deleteOld |
|
| 182 | - * @return void |
|
| 183 | - */ |
|
| 184 | - protected function regenerateSessionId($deleteOld = false) |
|
| 185 | - { |
|
| 186 | - if ($deleteOld) { |
|
| 187 | - $this->deleteSession(); |
|
| 188 | - } |
|
| 189 | - $this->sessionId = $this->generateSessionId(); |
|
| 190 | - $this->setCookie(); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @return void |
|
| 195 | - */ |
|
| 196 | - protected function setCookie() |
|
| 197 | - { |
|
| 198 | - if (headers_sent()) { |
|
| 199 | - return; |
|
| 200 | - } |
|
| 201 | - $cookie = $this->sessionId.static::DELIMITER.$this->expiryTimestamp.static::DELIMITER.$this->expiryTimestampReset; |
|
| 202 | - $cookiePath = preg_replace('|https?://[^/]+|i', '', trailingslashit((string) get_option('home'))); |
|
| 203 | - setcookie(static::SESSION_COOKIE, $cookie, $this->expiryTimestamp, $cookiePath); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @return void |
|
| 208 | - */ |
|
| 209 | - protected function setCookieExpiration() |
|
| 210 | - { |
|
| 211 | - $this->expiryTimestampReset = time() + (24 * 60); // 24 minutes |
|
| 212 | - $this->expiryTimestamp = time() + (30 * 60); // 30 minutes |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @return void |
|
| 217 | - */ |
|
| 218 | - protected function updateSession() |
|
| 219 | - { |
|
| 220 | - if (false === get_option($this->getSessionId())) { |
|
| 221 | - return $this->createSession(); |
|
| 222 | - } |
|
| 223 | - update_option($this->getSessionId(), $this->sessionData, false); |
|
| 224 | - } |
|
| 13 | + const DELIMITER = '__'; |
|
| 14 | + |
|
| 15 | + const SESSION_COOKIE = '_glsr_session'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var int |
|
| 19 | + */ |
|
| 20 | + protected $expiryTimestamp; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var int |
|
| 24 | + */ |
|
| 25 | + protected $expiryTimestampReset; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var array |
|
| 29 | + */ |
|
| 30 | + protected $sessionData; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | + protected $sessionId; |
|
| 36 | + |
|
| 37 | + public function __construct() |
|
| 38 | + { |
|
| 39 | + $cookieId = filter_input(INPUT_COOKIE, static::SESSION_COOKIE); |
|
| 40 | + if (false !== strpos($cookieId, static::DELIMITER)) { |
|
| 41 | + $cookie = explode(static::DELIMITER, stripslashes($cookieId)); |
|
| 42 | + $this->sessionId = preg_replace('/[^A-Za-z0-9_]/', '', $cookie[0]); |
|
| 43 | + $this->expiryTimestamp = absint($cookie[1]); |
|
| 44 | + $this->expiryTimestampReset = absint($cookie[2]); |
|
| 45 | + if (time() > $this->expiryTimestampReset) { |
|
| 46 | + $this->setCookieExpiration(); |
|
| 47 | + } |
|
| 48 | + } else { |
|
| 49 | + $this->sessionId = $this->generateSessionId(); |
|
| 50 | + $this->setCookieExpiration(); |
|
| 51 | + } |
|
| 52 | + $this->getSessionData(); |
|
| 53 | + $this->setCookie(); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function clear() |
|
| 60 | + { |
|
| 61 | + $this->setCookieExpiration(); |
|
| 62 | + $this->regenerateSessionId('and delete session!'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return int|false |
|
| 67 | + */ |
|
| 68 | + public function deleteAllSessions() |
|
| 69 | + { |
|
| 70 | + return glsr(SqlQueries::class)->deleteAllSessions(static::SESSION_COOKIE); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param int $limit |
|
| 75 | + * @return void |
|
| 76 | + */ |
|
| 77 | + public function deleteExpiredSessions($limit = 1000) |
|
| 78 | + { |
|
| 79 | + if ($expiredSessions = implode("','", $this->getExpiredSessions($limit))) { |
|
| 80 | + glsr(SqlQueries::class)->deleteExpiredSessions($expiredSessions); |
|
| 81 | + } |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $key |
|
| 86 | + * @param string|array $fallback |
|
| 87 | + * @param bool|string $unset |
|
| 88 | + * @return string|array |
|
| 89 | + */ |
|
| 90 | + public function get($key, $fallback = '', $unset = false) |
|
| 91 | + { |
|
| 92 | + $key = sanitize_key($key); |
|
| 93 | + $value = isset($this->sessionData[$key]) |
|
| 94 | + ? maybe_unserialize($this->sessionData[$key]) |
|
| 95 | + : $fallback; |
|
| 96 | + if (isset($this->sessionData[$key]) && $unset) { |
|
| 97 | + unset($this->sessionData[$key]); |
|
| 98 | + $this->updateSession(); |
|
| 99 | + } |
|
| 100 | + return $value; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param string $key |
|
| 105 | + * @param mixed $value |
|
| 106 | + * @return mixed |
|
| 107 | + */ |
|
| 108 | + public function set($key, $value) |
|
| 109 | + { |
|
| 110 | + $key = sanitize_key($key); |
|
| 111 | + $this->sessionData[$key] = maybe_serialize($value); |
|
| 112 | + $this->updateSession(); |
|
| 113 | + return $this->sessionData[$key]; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * @return void |
|
| 118 | + */ |
|
| 119 | + protected function createSession() |
|
| 120 | + { |
|
| 121 | + add_option($this->getSessionId(), $this->sessionData, '', false); |
|
| 122 | + add_option($this->getSessionId('expires'), $this->expiryTimestamp, '', false); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @return void |
|
| 127 | + */ |
|
| 128 | + protected function deleteSession() |
|
| 129 | + { |
|
| 130 | + delete_option($this->getSessionId()); |
|
| 131 | + delete_option($this->getSessionId('expires')); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * @return string |
|
| 136 | + */ |
|
| 137 | + protected function generateSessionId() |
|
| 138 | + { |
|
| 139 | + return md5(( new PasswordHash(8, false))->get_random_bytes(32)); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param int $limit |
|
| 144 | + * @return array |
|
| 145 | + */ |
|
| 146 | + protected function getExpiredSessions($limit) |
|
| 147 | + { |
|
| 148 | + $expiredSessions = []; |
|
| 149 | + $sessions = glsr(SqlQueries::class)->getExpiredSessions(static::SESSION_COOKIE, absint($limit)); |
|
| 150 | + if (!empty($sessions)) { |
|
| 151 | + $now = time(); |
|
| 152 | + foreach ($sessions as $session) { |
|
| 153 | + if ($now <= $session->expiration) { |
|
| 154 | + continue; |
|
| 155 | + } |
|
| 156 | + $expiredSessions[] = $session->name; |
|
| 157 | + $expiredSessions[] = str_replace('_expires_', '_', $session->name); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + return $expiredSessions; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param string $separator |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + protected function getSessionId($separator = '') |
|
| 168 | + { |
|
| 169 | + return implode('_', array_filter([static::SESSION_COOKIE, $separator, $this->sessionId])); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @return array |
|
| 174 | + */ |
|
| 175 | + protected function getSessionData() |
|
| 176 | + { |
|
| 177 | + return $this->sessionData = (array) get_option($this->getSessionId(), []); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param bool|string $deleteOld |
|
| 182 | + * @return void |
|
| 183 | + */ |
|
| 184 | + protected function regenerateSessionId($deleteOld = false) |
|
| 185 | + { |
|
| 186 | + if ($deleteOld) { |
|
| 187 | + $this->deleteSession(); |
|
| 188 | + } |
|
| 189 | + $this->sessionId = $this->generateSessionId(); |
|
| 190 | + $this->setCookie(); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @return void |
|
| 195 | + */ |
|
| 196 | + protected function setCookie() |
|
| 197 | + { |
|
| 198 | + if (headers_sent()) { |
|
| 199 | + return; |
|
| 200 | + } |
|
| 201 | + $cookie = $this->sessionId.static::DELIMITER.$this->expiryTimestamp.static::DELIMITER.$this->expiryTimestampReset; |
|
| 202 | + $cookiePath = preg_replace('|https?://[^/]+|i', '', trailingslashit((string) get_option('home'))); |
|
| 203 | + setcookie(static::SESSION_COOKIE, $cookie, $this->expiryTimestamp, $cookiePath); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @return void |
|
| 208 | + */ |
|
| 209 | + protected function setCookieExpiration() |
|
| 210 | + { |
|
| 211 | + $this->expiryTimestampReset = time() + (24 * 60); // 24 minutes |
|
| 212 | + $this->expiryTimestamp = time() + (30 * 60); // 30 minutes |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @return void |
|
| 217 | + */ |
|
| 218 | + protected function updateSession() |
|
| 219 | + { |
|
| 220 | + if (false === get_option($this->getSessionId())) { |
|
| 221 | + return $this->createSession(); |
|
| 222 | + } |
|
| 223 | + update_option($this->getSessionId(), $this->sessionData, false); |
|
| 224 | + } |
|
| 225 | 225 | } |