@@ -12,322 +12,322 @@ |
||
12 | 12 | |
13 | 13 | class Schema |
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 $keyValues = []; |
|
20 | + /** |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $keyValues = []; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - protected $ratingCounts; |
|
25 | + /** |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + protected $ratingCounts; |
|
29 | 29 | |
30 | - /** |
|
31 | - * @return array |
|
32 | - */ |
|
33 | - public function build(array $args = []) |
|
34 | - { |
|
35 | - $this->args = $args; |
|
36 | - $schema = $this->buildSummary($args); |
|
37 | - $reviews = []; |
|
38 | - foreach (glsr(ReviewManager::class)->get($this->args) as $review) { |
|
39 | - // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
|
40 | - // @see https://developers.google.com/search/docs/data-types/review |
|
41 | - if ('local' != $review->review_type) { |
|
42 | - continue; |
|
43 | - } |
|
44 | - $reviews[] = $this->buildReview($review); |
|
45 | - } |
|
46 | - if (!empty($reviews)) { |
|
47 | - array_walk($reviews, function (&$review) { |
|
48 | - unset($review['@context']); |
|
49 | - unset($review['itemReviewed']); |
|
50 | - }); |
|
51 | - $schema['review'] = $reviews; |
|
52 | - } |
|
53 | - return $schema; |
|
54 | - } |
|
30 | + /** |
|
31 | + * @return array |
|
32 | + */ |
|
33 | + public function build(array $args = []) |
|
34 | + { |
|
35 | + $this->args = $args; |
|
36 | + $schema = $this->buildSummary($args); |
|
37 | + $reviews = []; |
|
38 | + foreach (glsr(ReviewManager::class)->get($this->args) as $review) { |
|
39 | + // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
|
40 | + // @see https://developers.google.com/search/docs/data-types/review |
|
41 | + if ('local' != $review->review_type) { |
|
42 | + continue; |
|
43 | + } |
|
44 | + $reviews[] = $this->buildReview($review); |
|
45 | + } |
|
46 | + if (!empty($reviews)) { |
|
47 | + array_walk($reviews, function (&$review) { |
|
48 | + unset($review['@context']); |
|
49 | + unset($review['itemReviewed']); |
|
50 | + }); |
|
51 | + $schema['review'] = $reviews; |
|
52 | + } |
|
53 | + return $schema; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * @param array|null $args |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function buildSummary($args = null) |
|
61 | - { |
|
62 | - if (is_array($args)) { |
|
63 | - $this->args = $args; |
|
64 | - } |
|
65 | - $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
66 | - $count = array_sum($this->getRatingCounts()); |
|
67 | - $schema = method_exists($this, $buildSummary) |
|
68 | - ? $this->$buildSummary() |
|
69 | - : $this->buildSummaryForCustom(); |
|
70 | - if (!empty($count)) { |
|
71 | - $schema->aggregateRating( |
|
72 | - $this->getSchemaType('AggregateRating') |
|
73 | - ->ratingValue($this->getRatingValue()) |
|
74 | - ->reviewCount($count) |
|
75 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
76 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
77 | - ); |
|
78 | - } |
|
79 | - $schema = $schema->toArray(); |
|
80 | - return apply_filters('site-reviews/schema/'.$schema['@type'], $schema, $args); |
|
81 | - } |
|
56 | + /** |
|
57 | + * @param array|null $args |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function buildSummary($args = null) |
|
61 | + { |
|
62 | + if (is_array($args)) { |
|
63 | + $this->args = $args; |
|
64 | + } |
|
65 | + $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
66 | + $count = array_sum($this->getRatingCounts()); |
|
67 | + $schema = method_exists($this, $buildSummary) |
|
68 | + ? $this->$buildSummary() |
|
69 | + : $this->buildSummaryForCustom(); |
|
70 | + if (!empty($count)) { |
|
71 | + $schema->aggregateRating( |
|
72 | + $this->getSchemaType('AggregateRating') |
|
73 | + ->ratingValue($this->getRatingValue()) |
|
74 | + ->reviewCount($count) |
|
75 | + ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
76 | + ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
77 | + ); |
|
78 | + } |
|
79 | + $schema = $schema->toArray(); |
|
80 | + return apply_filters('site-reviews/schema/'.$schema['@type'], $schema, $args); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * @return void |
|
85 | - */ |
|
86 | - public function render() |
|
87 | - { |
|
88 | - if (empty(glsr()->schemas)) { |
|
89 | - return; |
|
90 | - } |
|
91 | - printf('<script type="application/ld+json">%s</script>', json_encode( |
|
92 | - apply_filters('site-reviews/schema/all', glsr()->schemas), |
|
93 | - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
|
94 | - )); |
|
95 | - } |
|
83 | + /** |
|
84 | + * @return void |
|
85 | + */ |
|
86 | + public function render() |
|
87 | + { |
|
88 | + if (empty(glsr()->schemas)) { |
|
89 | + return; |
|
90 | + } |
|
91 | + printf('<script type="application/ld+json">%s</script>', json_encode( |
|
92 | + apply_filters('site-reviews/schema/all', glsr()->schemas), |
|
93 | + JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
|
94 | + )); |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function store(array $schema) |
|
101 | - { |
|
102 | - $schemas = glsr()->schemas; |
|
103 | - $schemas[] = $schema; |
|
104 | - glsr()->schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
105 | - } |
|
97 | + /** |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function store(array $schema) |
|
101 | + { |
|
102 | + $schemas = glsr()->schemas; |
|
103 | + $schemas[] = $schema; |
|
104 | + glsr()->schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * @param Review $review |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - protected function buildReview($review) |
|
112 | - { |
|
113 | - $schema = $this->getSchemaType('Review') |
|
114 | - ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
115 | - $schema->name($review->title); |
|
116 | - }) |
|
117 | - ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
118 | - $schema->reviewBody($review->content); |
|
119 | - }) |
|
120 | - ->datePublished((new DateTime($review->date))) |
|
121 | - ->author($this->getSchemaType('Person')->name($review->author)) |
|
122 | - ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
123 | - if (!empty($review->rating)) { |
|
124 | - $schema->reviewRating( |
|
125 | - $this->getSchemaType('Rating') |
|
126 | - ->ratingValue($review->rating) |
|
127 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
128 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
129 | - ); |
|
130 | - } |
|
131 | - return apply_filters('site-reviews/schema/review', $schema->toArray(), $review, $this->args); |
|
132 | - } |
|
107 | + /** |
|
108 | + * @param Review $review |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + protected function buildReview($review) |
|
112 | + { |
|
113 | + $schema = $this->getSchemaType('Review') |
|
114 | + ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
115 | + $schema->name($review->title); |
|
116 | + }) |
|
117 | + ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
118 | + $schema->reviewBody($review->content); |
|
119 | + }) |
|
120 | + ->datePublished((new DateTime($review->date))) |
|
121 | + ->author($this->getSchemaType('Person')->name($review->author)) |
|
122 | + ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
123 | + if (!empty($review->rating)) { |
|
124 | + $schema->reviewRating( |
|
125 | + $this->getSchemaType('Rating') |
|
126 | + ->ratingValue($review->rating) |
|
127 | + ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
128 | + ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
129 | + ); |
|
130 | + } |
|
131 | + return apply_filters('site-reviews/schema/review', $schema->toArray(), $review, $this->args); |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @param mixed $schema |
|
136 | - * @return mixed |
|
137 | - */ |
|
138 | - protected function buildSchemaValues($schema, array $values = []) |
|
139 | - { |
|
140 | - foreach ($values as $value) { |
|
141 | - $option = $this->getSchemaOptionValue($value); |
|
142 | - if (empty($option)) { |
|
143 | - continue; |
|
144 | - } |
|
145 | - $schema->$value($option); |
|
146 | - } |
|
147 | - return $schema; |
|
148 | - } |
|
134 | + /** |
|
135 | + * @param mixed $schema |
|
136 | + * @return mixed |
|
137 | + */ |
|
138 | + protected function buildSchemaValues($schema, array $values = []) |
|
139 | + { |
|
140 | + foreach ($values as $value) { |
|
141 | + $option = $this->getSchemaOptionValue($value); |
|
142 | + if (empty($option)) { |
|
143 | + continue; |
|
144 | + } |
|
145 | + $schema->$value($option); |
|
146 | + } |
|
147 | + return $schema; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * @return mixed |
|
152 | - */ |
|
153 | - protected function buildSummaryForCustom() |
|
154 | - { |
|
155 | - return $this->buildSchemaValues($this->getSchemaType(), [ |
|
156 | - 'description', 'image', 'name', 'url', |
|
157 | - ]); |
|
158 | - } |
|
150 | + /** |
|
151 | + * @return mixed |
|
152 | + */ |
|
153 | + protected function buildSummaryForCustom() |
|
154 | + { |
|
155 | + return $this->buildSchemaValues($this->getSchemaType(), [ |
|
156 | + 'description', 'image', 'name', 'url', |
|
157 | + ]); |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * @return mixed |
|
162 | - */ |
|
163 | - protected function buildSummaryForLocalBusiness() |
|
164 | - { |
|
165 | - return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
166 | - 'address', 'priceRange', 'telephone', |
|
167 | - ]); |
|
168 | - } |
|
160 | + /** |
|
161 | + * @return mixed |
|
162 | + */ |
|
163 | + protected function buildSummaryForLocalBusiness() |
|
164 | + { |
|
165 | + return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
166 | + 'address', 'priceRange', 'telephone', |
|
167 | + ]); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * @return mixed |
|
172 | - */ |
|
173 | - protected function buildSummaryForProduct() |
|
174 | - { |
|
175 | - $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
176 | - $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
177 | - 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
|
178 | - ]); |
|
179 | - return $this->buildSummaryForCustom() |
|
180 | - ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
181 | - $schema->offers($offers); |
|
182 | - }) |
|
183 | - ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
184 | - } |
|
170 | + /** |
|
171 | + * @return mixed |
|
172 | + */ |
|
173 | + protected function buildSummaryForProduct() |
|
174 | + { |
|
175 | + $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
176 | + $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
177 | + 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
|
178 | + ]); |
|
179 | + return $this->buildSummaryForCustom() |
|
180 | + ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
181 | + $schema->offers($offers); |
|
182 | + }) |
|
183 | + ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * @return array |
|
188 | - */ |
|
189 | - protected function getRatingCounts() |
|
190 | - { |
|
191 | - if (!isset($this->ratingCounts)) { |
|
192 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($this->args); |
|
193 | - } |
|
194 | - return $this->ratingCounts; |
|
195 | - } |
|
186 | + /** |
|
187 | + * @return array |
|
188 | + */ |
|
189 | + protected function getRatingCounts() |
|
190 | + { |
|
191 | + if (!isset($this->ratingCounts)) { |
|
192 | + $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($this->args); |
|
193 | + } |
|
194 | + return $this->ratingCounts; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * @return int|float |
|
199 | - */ |
|
200 | - protected function getRatingValue() |
|
201 | - { |
|
202 | - return glsr(Rating::class)->getAverage($this->getRatingCounts()); |
|
203 | - } |
|
197 | + /** |
|
198 | + * @return int|float |
|
199 | + */ |
|
200 | + protected function getRatingValue() |
|
201 | + { |
|
202 | + return glsr(Rating::class)->getAverage($this->getRatingCounts()); |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * @param string $option |
|
207 | - * @param string $fallback |
|
208 | - * @return string |
|
209 | - */ |
|
210 | - protected function getSchemaOption($option, $fallback) |
|
211 | - { |
|
212 | - $option = strtolower($option); |
|
213 | - if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
214 | - return $schemaOption; |
|
215 | - } |
|
216 | - $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
217 | - if (is_array($setting)) { |
|
218 | - return $this->getSchemaOptionDefault($setting, $fallback); |
|
219 | - } |
|
220 | - return !empty($setting) |
|
221 | - ? $setting |
|
222 | - : $fallback; |
|
223 | - } |
|
205 | + /** |
|
206 | + * @param string $option |
|
207 | + * @param string $fallback |
|
208 | + * @return string |
|
209 | + */ |
|
210 | + protected function getSchemaOption($option, $fallback) |
|
211 | + { |
|
212 | + $option = strtolower($option); |
|
213 | + if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
214 | + return $schemaOption; |
|
215 | + } |
|
216 | + $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
217 | + if (is_array($setting)) { |
|
218 | + return $this->getSchemaOptionDefault($setting, $fallback); |
|
219 | + } |
|
220 | + return !empty($setting) |
|
221 | + ? $setting |
|
222 | + : $fallback; |
|
223 | + } |
|
224 | 224 | |
225 | - /** |
|
226 | - * @param string $fallback |
|
227 | - * @return string |
|
228 | - */ |
|
229 | - protected function getSchemaOptionDefault(array $setting, $fallback) |
|
230 | - { |
|
231 | - $setting = wp_parse_args($setting, [ |
|
232 | - 'custom' => '', |
|
233 | - 'default' => $fallback, |
|
234 | - ]); |
|
235 | - return 'custom' != $setting['default'] |
|
236 | - ? $setting['default'] |
|
237 | - : $setting['custom']; |
|
238 | - } |
|
225 | + /** |
|
226 | + * @param string $fallback |
|
227 | + * @return string |
|
228 | + */ |
|
229 | + protected function getSchemaOptionDefault(array $setting, $fallback) |
|
230 | + { |
|
231 | + $setting = wp_parse_args($setting, [ |
|
232 | + 'custom' => '', |
|
233 | + 'default' => $fallback, |
|
234 | + ]); |
|
235 | + return 'custom' != $setting['default'] |
|
236 | + ? $setting['default'] |
|
237 | + : $setting['custom']; |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * @param string $option |
|
242 | - * @param string $fallback |
|
243 | - * @return void|string |
|
244 | - */ |
|
245 | - protected function getSchemaOptionValue($option, $fallback = 'post') |
|
246 | - { |
|
247 | - if (array_key_exists($option, $this->keyValues)) { |
|
248 | - return $this->keyValues[$option]; |
|
249 | - } |
|
250 | - $value = $this->getSchemaOption($option, $fallback); |
|
251 | - if ($value != $fallback) { |
|
252 | - return $this->setAndGetKeyValue($option, $value); |
|
253 | - } |
|
254 | - if (!is_single() && !is_page()) { |
|
255 | - return; |
|
256 | - } |
|
257 | - $method = Helper::buildMethodName($option, 'getThing'); |
|
258 | - if (method_exists($this, $method)) { |
|
259 | - return $this->setAndGetKeyValue($option, $this->$method()); |
|
260 | - } |
|
261 | - } |
|
240 | + /** |
|
241 | + * @param string $option |
|
242 | + * @param string $fallback |
|
243 | + * @return void|string |
|
244 | + */ |
|
245 | + protected function getSchemaOptionValue($option, $fallback = 'post') |
|
246 | + { |
|
247 | + if (array_key_exists($option, $this->keyValues)) { |
|
248 | + return $this->keyValues[$option]; |
|
249 | + } |
|
250 | + $value = $this->getSchemaOption($option, $fallback); |
|
251 | + if ($value != $fallback) { |
|
252 | + return $this->setAndGetKeyValue($option, $value); |
|
253 | + } |
|
254 | + if (!is_single() && !is_page()) { |
|
255 | + return; |
|
256 | + } |
|
257 | + $method = Helper::buildMethodName($option, 'getThing'); |
|
258 | + if (method_exists($this, $method)) { |
|
259 | + return $this->setAndGetKeyValue($option, $this->$method()); |
|
260 | + } |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * @param string|null $type |
|
265 | - * @return mixed |
|
266 | - */ |
|
267 | - protected function getSchemaType($type = null) |
|
268 | - { |
|
269 | - if (!is_string($type)) { |
|
270 | - $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
271 | - } |
|
272 | - $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
273 | - return class_exists($className) |
|
274 | - ? new $className() |
|
275 | - : new UnknownType($type); |
|
276 | - } |
|
263 | + /** |
|
264 | + * @param string|null $type |
|
265 | + * @return mixed |
|
266 | + */ |
|
267 | + protected function getSchemaType($type = null) |
|
268 | + { |
|
269 | + if (!is_string($type)) { |
|
270 | + $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
271 | + } |
|
272 | + $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
273 | + return class_exists($className) |
|
274 | + ? new $className() |
|
275 | + : new UnknownType($type); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * @return string |
|
280 | - */ |
|
281 | - protected function getThingDescription() |
|
282 | - { |
|
283 | - $post = get_post(); |
|
284 | - $text = Arr::get($post, 'post_excerpt'); |
|
285 | - if (empty($text)) { |
|
286 | - $text = Arr::get($post, 'post_content'); |
|
287 | - } |
|
288 | - if (function_exists('excerpt_remove_blocks')) { |
|
289 | - $text = excerpt_remove_blocks($text); |
|
290 | - } |
|
291 | - $text = strip_shortcodes($text); |
|
292 | - $text = wpautop($text); |
|
293 | - $text = wptexturize($text); |
|
294 | - $text = wp_strip_all_tags($text); |
|
295 | - $text = str_replace(']]>', ']]>', $text); |
|
296 | - return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
297 | - } |
|
278 | + /** |
|
279 | + * @return string |
|
280 | + */ |
|
281 | + protected function getThingDescription() |
|
282 | + { |
|
283 | + $post = get_post(); |
|
284 | + $text = Arr::get($post, 'post_excerpt'); |
|
285 | + if (empty($text)) { |
|
286 | + $text = Arr::get($post, 'post_content'); |
|
287 | + } |
|
288 | + if (function_exists('excerpt_remove_blocks')) { |
|
289 | + $text = excerpt_remove_blocks($text); |
|
290 | + } |
|
291 | + $text = strip_shortcodes($text); |
|
292 | + $text = wpautop($text); |
|
293 | + $text = wptexturize($text); |
|
294 | + $text = wp_strip_all_tags($text); |
|
295 | + $text = str_replace(']]>', ']]>', $text); |
|
296 | + return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - protected function getThingImage() |
|
303 | - { |
|
304 | - return (string) get_the_post_thumbnail_url(null, 'large'); |
|
305 | - } |
|
299 | + /** |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + protected function getThingImage() |
|
303 | + { |
|
304 | + return (string) get_the_post_thumbnail_url(null, 'large'); |
|
305 | + } |
|
306 | 306 | |
307 | - /** |
|
308 | - * @return string |
|
309 | - */ |
|
310 | - protected function getThingName() |
|
311 | - { |
|
312 | - return get_the_title(); |
|
313 | - } |
|
307 | + /** |
|
308 | + * @return string |
|
309 | + */ |
|
310 | + protected function getThingName() |
|
311 | + { |
|
312 | + return get_the_title(); |
|
313 | + } |
|
314 | 314 | |
315 | - /** |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - protected function getThingUrl() |
|
319 | - { |
|
320 | - return (string) get_the_permalink(); |
|
321 | - } |
|
315 | + /** |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + protected function getThingUrl() |
|
319 | + { |
|
320 | + return (string) get_the_permalink(); |
|
321 | + } |
|
322 | 322 | |
323 | - /** |
|
324 | - * @param string $option |
|
325 | - * @param string $value |
|
326 | - * @return string |
|
327 | - */ |
|
328 | - protected function setAndGetKeyValue($option, $value) |
|
329 | - { |
|
330 | - $this->keyValues[$option] = $value; |
|
331 | - return $value; |
|
332 | - } |
|
323 | + /** |
|
324 | + * @param string $option |
|
325 | + * @param string $value |
|
326 | + * @return string |
|
327 | + */ |
|
328 | + protected function setAndGetKeyValue($option, $value) |
|
329 | + { |
|
330 | + $this->keyValues[$option] = $value; |
|
331 | + return $value; |
|
332 | + } |
|
333 | 333 | } |
@@ -30,21 +30,21 @@ discard block |
||
30 | 30 | /** |
31 | 31 | * @return array |
32 | 32 | */ |
33 | - public function build(array $args = []) |
|
33 | + public function build( array $args = [] ) |
|
34 | 34 | { |
35 | 35 | $this->args = $args; |
36 | - $schema = $this->buildSummary($args); |
|
36 | + $schema = $this->buildSummary( $args ); |
|
37 | 37 | $reviews = []; |
38 | - foreach (glsr(ReviewManager::class)->get($this->args) as $review) { |
|
38 | + foreach( glsr( ReviewManager::class )->get( $this->args ) as $review ) { |
|
39 | 39 | // Only include critic reviews that have been directly produced by your site, not reviews from third-party sites or syndicated reviews. |
40 | 40 | // @see https://developers.google.com/search/docs/data-types/review |
41 | - if ('local' != $review->review_type) { |
|
41 | + if( 'local' != $review->review_type ) { |
|
42 | 42 | continue; |
43 | 43 | } |
44 | - $reviews[] = $this->buildReview($review); |
|
44 | + $reviews[] = $this->buildReview( $review ); |
|
45 | 45 | } |
46 | - if (!empty($reviews)) { |
|
47 | - array_walk($reviews, function (&$review) { |
|
46 | + if( !empty($reviews) ) { |
|
47 | + array_walk( $reviews, function( &$review ) { |
|
48 | 48 | unset($review['@context']); |
49 | 49 | unset($review['itemReviewed']); |
50 | 50 | }); |
@@ -57,27 +57,27 @@ discard block |
||
57 | 57 | * @param array|null $args |
58 | 58 | * @return array |
59 | 59 | */ |
60 | - public function buildSummary($args = null) |
|
60 | + public function buildSummary( $args = null ) |
|
61 | 61 | { |
62 | - if (is_array($args)) { |
|
62 | + if( is_array( $args ) ) { |
|
63 | 63 | $this->args = $args; |
64 | 64 | } |
65 | - $buildSummary = Helper::buildMethodName($this->getSchemaOptionValue('type'), 'buildSummaryFor'); |
|
66 | - $count = array_sum($this->getRatingCounts()); |
|
67 | - $schema = method_exists($this, $buildSummary) |
|
65 | + $buildSummary = Helper::buildMethodName( $this->getSchemaOptionValue( 'type' ), 'buildSummaryFor' ); |
|
66 | + $count = array_sum( $this->getRatingCounts() ); |
|
67 | + $schema = method_exists( $this, $buildSummary ) |
|
68 | 68 | ? $this->$buildSummary() |
69 | 69 | : $this->buildSummaryForCustom(); |
70 | - if (!empty($count)) { |
|
70 | + if( !empty($count) ) { |
|
71 | 71 | $schema->aggregateRating( |
72 | - $this->getSchemaType('AggregateRating') |
|
73 | - ->ratingValue($this->getRatingValue()) |
|
74 | - ->reviewCount($count) |
|
75 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
76 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
72 | + $this->getSchemaType( 'AggregateRating' ) |
|
73 | + ->ratingValue( $this->getRatingValue() ) |
|
74 | + ->reviewCount( $count ) |
|
75 | + ->bestRating( glsr()->constant( 'MAX_RATING', Rating::class ) ) |
|
76 | + ->worstRating( glsr()->constant( 'MIN_RATING', Rating::class ) ) |
|
77 | 77 | ); |
78 | 78 | } |
79 | 79 | $schema = $schema->toArray(); |
80 | - return apply_filters('site-reviews/schema/'.$schema['@type'], $schema, $args); |
|
80 | + return apply_filters( 'site-reviews/schema/'.$schema['@type'], $schema, $args ); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | /** |
@@ -85,64 +85,64 @@ discard block |
||
85 | 85 | */ |
86 | 86 | public function render() |
87 | 87 | { |
88 | - if (empty(glsr()->schemas)) { |
|
88 | + if( empty(glsr()->schemas) ) { |
|
89 | 89 | return; |
90 | 90 | } |
91 | - printf('<script type="application/ld+json">%s</script>', json_encode( |
|
92 | - apply_filters('site-reviews/schema/all', glsr()->schemas), |
|
91 | + printf( '<script type="application/ld+json">%s</script>', json_encode( |
|
92 | + apply_filters( 'site-reviews/schema/all', glsr()->schemas ), |
|
93 | 93 | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES |
94 | - )); |
|
94 | + ) ); |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
98 | 98 | * @return void |
99 | 99 | */ |
100 | - public function store(array $schema) |
|
100 | + public function store( array $schema ) |
|
101 | 101 | { |
102 | 102 | $schemas = glsr()->schemas; |
103 | 103 | $schemas[] = $schema; |
104 | - glsr()->schemas = array_map('unserialize', array_unique(array_map('serialize', $schemas))); |
|
104 | + glsr()->schemas = array_map( 'unserialize', array_unique( array_map( 'serialize', $schemas ) ) ); |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
108 | 108 | * @param Review $review |
109 | 109 | * @return array |
110 | 110 | */ |
111 | - protected function buildReview($review) |
|
111 | + protected function buildReview( $review ) |
|
112 | 112 | { |
113 | - $schema = $this->getSchemaType('Review') |
|
114 | - ->doIf(!in_array('title', $this->args['hide']), function ($schema) use ($review) { |
|
115 | - $schema->name($review->title); |
|
113 | + $schema = $this->getSchemaType( 'Review' ) |
|
114 | + ->doIf( !in_array( 'title', $this->args['hide'] ), function( $schema ) use ($review) { |
|
115 | + $schema->name( $review->title ); |
|
116 | 116 | }) |
117 | - ->doIf(!in_array('excerpt', $this->args['hide']), function ($schema) use ($review) { |
|
118 | - $schema->reviewBody($review->content); |
|
117 | + ->doIf( !in_array( 'excerpt', $this->args['hide'] ), function( $schema ) use ($review) { |
|
118 | + $schema->reviewBody( $review->content ); |
|
119 | 119 | }) |
120 | - ->datePublished((new DateTime($review->date))) |
|
121 | - ->author($this->getSchemaType('Person')->name($review->author)) |
|
122 | - ->itemReviewed($this->getSchemaType()->name($this->getSchemaOptionValue('name'))); |
|
123 | - if (!empty($review->rating)) { |
|
120 | + ->datePublished( (new DateTime( $review->date )) ) |
|
121 | + ->author( $this->getSchemaType( 'Person' )->name( $review->author ) ) |
|
122 | + ->itemReviewed( $this->getSchemaType()->name( $this->getSchemaOptionValue( 'name' ) ) ); |
|
123 | + if( !empty($review->rating) ) { |
|
124 | 124 | $schema->reviewRating( |
125 | - $this->getSchemaType('Rating') |
|
126 | - ->ratingValue($review->rating) |
|
127 | - ->bestRating(glsr()->constant('MAX_RATING', Rating::class)) |
|
128 | - ->worstRating(glsr()->constant('MIN_RATING', Rating::class)) |
|
125 | + $this->getSchemaType( 'Rating' ) |
|
126 | + ->ratingValue( $review->rating ) |
|
127 | + ->bestRating( glsr()->constant( 'MAX_RATING', Rating::class ) ) |
|
128 | + ->worstRating( glsr()->constant( 'MIN_RATING', Rating::class ) ) |
|
129 | 129 | ); |
130 | 130 | } |
131 | - return apply_filters('site-reviews/schema/review', $schema->toArray(), $review, $this->args); |
|
131 | + return apply_filters( 'site-reviews/schema/review', $schema->toArray(), $review, $this->args ); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | /** |
135 | 135 | * @param mixed $schema |
136 | 136 | * @return mixed |
137 | 137 | */ |
138 | - protected function buildSchemaValues($schema, array $values = []) |
|
138 | + protected function buildSchemaValues( $schema, array $values = [] ) |
|
139 | 139 | { |
140 | - foreach ($values as $value) { |
|
141 | - $option = $this->getSchemaOptionValue($value); |
|
142 | - if (empty($option)) { |
|
140 | + foreach( $values as $value ) { |
|
141 | + $option = $this->getSchemaOptionValue( $value ); |
|
142 | + if( empty($option) ) { |
|
143 | 143 | continue; |
144 | 144 | } |
145 | - $schema->$value($option); |
|
145 | + $schema->$value( $option ); |
|
146 | 146 | } |
147 | 147 | return $schema; |
148 | 148 | } |
@@ -152,9 +152,9 @@ discard block |
||
152 | 152 | */ |
153 | 153 | protected function buildSummaryForCustom() |
154 | 154 | { |
155 | - return $this->buildSchemaValues($this->getSchemaType(), [ |
|
155 | + return $this->buildSchemaValues( $this->getSchemaType(), [ |
|
156 | 156 | 'description', 'image', 'name', 'url', |
157 | - ]); |
|
157 | + ] ); |
|
158 | 158 | } |
159 | 159 | |
160 | 160 | /** |
@@ -162,9 +162,9 @@ discard block |
||
162 | 162 | */ |
163 | 163 | protected function buildSummaryForLocalBusiness() |
164 | 164 | { |
165 | - return $this->buildSchemaValues($this->buildSummaryForCustom(), [ |
|
165 | + return $this->buildSchemaValues( $this->buildSummaryForCustom(), [ |
|
166 | 166 | 'address', 'priceRange', 'telephone', |
167 | - ]); |
|
167 | + ] ); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | /** |
@@ -172,15 +172,15 @@ discard block |
||
172 | 172 | */ |
173 | 173 | protected function buildSummaryForProduct() |
174 | 174 | { |
175 | - $offerType = $this->getSchemaOption('offerType', 'AggregateOffer'); |
|
176 | - $offers = $this->buildSchemaValues($this->getSchemaType($offerType), [ |
|
175 | + $offerType = $this->getSchemaOption( 'offerType', 'AggregateOffer' ); |
|
176 | + $offers = $this->buildSchemaValues( $this->getSchemaType( $offerType ), [ |
|
177 | 177 | 'highPrice', 'lowPrice', 'price', 'priceCurrency', |
178 | - ]); |
|
178 | + ] ); |
|
179 | 179 | return $this->buildSummaryForCustom() |
180 | - ->doIf(!empty($offers->getProperties()), function ($schema) use ($offers) { |
|
181 | - $schema->offers($offers); |
|
180 | + ->doIf( !empty($offers->getProperties()), function( $schema ) use ($offers) { |
|
181 | + $schema->offers( $offers ); |
|
182 | 182 | }) |
183 | - ->setProperty('@id', $this->getSchemaOptionValue('url').'#product'); |
|
183 | + ->setProperty( '@id', $this->getSchemaOptionValue( 'url' ).'#product' ); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | /** |
@@ -188,8 +188,8 @@ discard block |
||
188 | 188 | */ |
189 | 189 | protected function getRatingCounts() |
190 | 190 | { |
191 | - if (!isset($this->ratingCounts)) { |
|
192 | - $this->ratingCounts = glsr(ReviewManager::class)->getRatingCounts($this->args); |
|
191 | + if( !isset($this->ratingCounts) ) { |
|
192 | + $this->ratingCounts = glsr( ReviewManager::class )->getRatingCounts( $this->args ); |
|
193 | 193 | } |
194 | 194 | return $this->ratingCounts; |
195 | 195 | } |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | */ |
200 | 200 | protected function getRatingValue() |
201 | 201 | { |
202 | - return glsr(Rating::class)->getAverage($this->getRatingCounts()); |
|
202 | + return glsr( Rating::class )->getAverage( $this->getRatingCounts() ); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | /** |
@@ -207,15 +207,15 @@ discard block |
||
207 | 207 | * @param string $fallback |
208 | 208 | * @return string |
209 | 209 | */ |
210 | - protected function getSchemaOption($option, $fallback) |
|
210 | + protected function getSchemaOption( $option, $fallback ) |
|
211 | 211 | { |
212 | - $option = strtolower($option); |
|
213 | - if ($schemaOption = trim((string) get_post_meta(intval(get_the_ID()), 'schema_'.$option, true))) { |
|
212 | + $option = strtolower( $option ); |
|
213 | + if( $schemaOption = trim( (string)get_post_meta( intval( get_the_ID() ), 'schema_'.$option, true ) ) ) { |
|
214 | 214 | return $schemaOption; |
215 | 215 | } |
216 | - $setting = glsr(OptionManager::class)->get('settings.schema.'.$option); |
|
217 | - if (is_array($setting)) { |
|
218 | - return $this->getSchemaOptionDefault($setting, $fallback); |
|
216 | + $setting = glsr( OptionManager::class )->get( 'settings.schema.'.$option ); |
|
217 | + if( is_array( $setting ) ) { |
|
218 | + return $this->getSchemaOptionDefault( $setting, $fallback ); |
|
219 | 219 | } |
220 | 220 | return !empty($setting) |
221 | 221 | ? $setting |
@@ -226,12 +226,12 @@ discard block |
||
226 | 226 | * @param string $fallback |
227 | 227 | * @return string |
228 | 228 | */ |
229 | - protected function getSchemaOptionDefault(array $setting, $fallback) |
|
229 | + protected function getSchemaOptionDefault( array $setting, $fallback ) |
|
230 | 230 | { |
231 | - $setting = wp_parse_args($setting, [ |
|
231 | + $setting = wp_parse_args( $setting, [ |
|
232 | 232 | 'custom' => '', |
233 | 233 | 'default' => $fallback, |
234 | - ]); |
|
234 | + ] ); |
|
235 | 235 | return 'custom' != $setting['default'] |
236 | 236 | ? $setting['default'] |
237 | 237 | : $setting['custom']; |
@@ -242,21 +242,21 @@ discard block |
||
242 | 242 | * @param string $fallback |
243 | 243 | * @return void|string |
244 | 244 | */ |
245 | - protected function getSchemaOptionValue($option, $fallback = 'post') |
|
245 | + protected function getSchemaOptionValue( $option, $fallback = 'post' ) |
|
246 | 246 | { |
247 | - if (array_key_exists($option, $this->keyValues)) { |
|
247 | + if( array_key_exists( $option, $this->keyValues ) ) { |
|
248 | 248 | return $this->keyValues[$option]; |
249 | 249 | } |
250 | - $value = $this->getSchemaOption($option, $fallback); |
|
251 | - if ($value != $fallback) { |
|
252 | - return $this->setAndGetKeyValue($option, $value); |
|
250 | + $value = $this->getSchemaOption( $option, $fallback ); |
|
251 | + if( $value != $fallback ) { |
|
252 | + return $this->setAndGetKeyValue( $option, $value ); |
|
253 | 253 | } |
254 | - if (!is_single() && !is_page()) { |
|
254 | + if( !is_single() && !is_page() ) { |
|
255 | 255 | return; |
256 | 256 | } |
257 | - $method = Helper::buildMethodName($option, 'getThing'); |
|
258 | - if (method_exists($this, $method)) { |
|
259 | - return $this->setAndGetKeyValue($option, $this->$method()); |
|
257 | + $method = Helper::buildMethodName( $option, 'getThing' ); |
|
258 | + if( method_exists( $this, $method ) ) { |
|
259 | + return $this->setAndGetKeyValue( $option, $this->$method() ); |
|
260 | 260 | } |
261 | 261 | } |
262 | 262 | |
@@ -264,15 +264,15 @@ discard block |
||
264 | 264 | * @param string|null $type |
265 | 265 | * @return mixed |
266 | 266 | */ |
267 | - protected function getSchemaType($type = null) |
|
267 | + protected function getSchemaType( $type = null ) |
|
268 | 268 | { |
269 | - if (!is_string($type)) { |
|
270 | - $type = $this->getSchemaOption('type', 'LocalBusiness'); |
|
269 | + if( !is_string( $type ) ) { |
|
270 | + $type = $this->getSchemaOption( 'type', 'LocalBusiness' ); |
|
271 | 271 | } |
272 | - $className = Helper::buildClassName($type, 'Modules\Schema'); |
|
273 | - return class_exists($className) |
|
272 | + $className = Helper::buildClassName( $type, 'Modules\Schema' ); |
|
273 | + return class_exists( $className ) |
|
274 | 274 | ? new $className() |
275 | - : new UnknownType($type); |
|
275 | + : new UnknownType( $type ); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | /** |
@@ -281,19 +281,19 @@ discard block |
||
281 | 281 | protected function getThingDescription() |
282 | 282 | { |
283 | 283 | $post = get_post(); |
284 | - $text = Arr::get($post, 'post_excerpt'); |
|
285 | - if (empty($text)) { |
|
286 | - $text = Arr::get($post, 'post_content'); |
|
284 | + $text = Arr::get( $post, 'post_excerpt' ); |
|
285 | + if( empty($text) ) { |
|
286 | + $text = Arr::get( $post, 'post_content' ); |
|
287 | 287 | } |
288 | - if (function_exists('excerpt_remove_blocks')) { |
|
289 | - $text = excerpt_remove_blocks($text); |
|
288 | + if( function_exists( 'excerpt_remove_blocks' ) ) { |
|
289 | + $text = excerpt_remove_blocks( $text ); |
|
290 | 290 | } |
291 | - $text = strip_shortcodes($text); |
|
292 | - $text = wpautop($text); |
|
293 | - $text = wptexturize($text); |
|
294 | - $text = wp_strip_all_tags($text); |
|
295 | - $text = str_replace(']]>', ']]>', $text); |
|
296 | - return wp_trim_words($text, apply_filters('excerpt_length', 55)); |
|
291 | + $text = strip_shortcodes( $text ); |
|
292 | + $text = wpautop( $text ); |
|
293 | + $text = wptexturize( $text ); |
|
294 | + $text = wp_strip_all_tags( $text ); |
|
295 | + $text = str_replace( ']]>', ']]>', $text ); |
|
296 | + return wp_trim_words( $text, apply_filters( 'excerpt_length', 55 ) ); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | */ |
302 | 302 | protected function getThingImage() |
303 | 303 | { |
304 | - return (string) get_the_post_thumbnail_url(null, 'large'); |
|
304 | + return (string)get_the_post_thumbnail_url( null, 'large' ); |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | /** |
@@ -317,7 +317,7 @@ discard block |
||
317 | 317 | */ |
318 | 318 | protected function getThingUrl() |
319 | 319 | { |
320 | - return (string) get_the_permalink(); |
|
320 | + return (string)get_the_permalink(); |
|
321 | 321 | } |
322 | 322 | |
323 | 323 | /** |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | * @param string $value |
326 | 326 | * @return string |
327 | 327 | */ |
328 | - protected function setAndGetKeyValue($option, $value) |
|
328 | + protected function setAndGetKeyValue( $option, $value ) |
|
329 | 329 | { |
330 | 330 | $this->keyValues[$option] = $value; |
331 | 331 | return $value; |
@@ -9,148 +9,148 @@ |
||
9 | 9 | |
10 | 10 | class EnqueuePublicAssets |
11 | 11 | { |
12 | - /** |
|
13 | - * @return void |
|
14 | - */ |
|
15 | - public function handle() |
|
16 | - { |
|
17 | - $this->enqueueAssets(); |
|
18 | - $this->enqueuePolyfillService(); |
|
19 | - $this->enqueueRecaptchaScript(); |
|
20 | - $this->inlineScript(); |
|
21 | - $this->inlineStyles(); |
|
22 | - } |
|
12 | + /** |
|
13 | + * @return void |
|
14 | + */ |
|
15 | + public function handle() |
|
16 | + { |
|
17 | + $this->enqueueAssets(); |
|
18 | + $this->enqueuePolyfillService(); |
|
19 | + $this->enqueueRecaptchaScript(); |
|
20 | + $this->inlineScript(); |
|
21 | + $this->inlineStyles(); |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * @return void |
|
26 | - */ |
|
27 | - public function enqueueAssets() |
|
28 | - { |
|
29 | - if (apply_filters('site-reviews/assets/css', true)) { |
|
30 | - wp_enqueue_style( |
|
31 | - Application::ID, |
|
32 | - $this->getStylesheet(), |
|
33 | - [], |
|
34 | - glsr()->version |
|
35 | - ); |
|
36 | - } |
|
37 | - if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | - $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39 | - ? [Application::ID.'/polyfill'] |
|
40 | - : []; |
|
41 | - $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42 | - wp_enqueue_script( |
|
43 | - Application::ID, |
|
44 | - glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45 | - $dependencies, |
|
46 | - glsr()->version, |
|
47 | - true |
|
48 | - ); |
|
49 | - } |
|
50 | - } |
|
24 | + /** |
|
25 | + * @return void |
|
26 | + */ |
|
27 | + public function enqueueAssets() |
|
28 | + { |
|
29 | + if (apply_filters('site-reviews/assets/css', true)) { |
|
30 | + wp_enqueue_style( |
|
31 | + Application::ID, |
|
32 | + $this->getStylesheet(), |
|
33 | + [], |
|
34 | + glsr()->version |
|
35 | + ); |
|
36 | + } |
|
37 | + if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | + $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
39 | + ? [Application::ID.'/polyfill'] |
|
40 | + : []; |
|
41 | + $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
42 | + wp_enqueue_script( |
|
43 | + Application::ID, |
|
44 | + glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
45 | + $dependencies, |
|
46 | + glsr()->version, |
|
47 | + true |
|
48 | + ); |
|
49 | + } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function enqueuePolyfillService() |
|
56 | - { |
|
57 | - if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58 | - return; |
|
59 | - } |
|
60 | - wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61 | - 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62 | - 'flags' => 'gated', |
|
63 | - ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64 | - } |
|
52 | + /** |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function enqueuePolyfillService() |
|
56 | + { |
|
57 | + if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
58 | + return; |
|
59 | + } |
|
60 | + wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
61 | + 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
|
62 | + 'flags' => 'gated', |
|
63 | + ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - public function enqueueRecaptchaScript() |
|
70 | - { |
|
71 | - // wpforms-recaptcha |
|
72 | - // google-recaptcha |
|
73 | - // nf-google-recaptcha |
|
74 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75 | - return; |
|
76 | - } |
|
77 | - $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | - wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79 | - 'hl' => $language, |
|
80 | - 'render' => 'explicit', |
|
81 | - ], 'https://www.google.com/recaptcha/api.js')); |
|
82 | - } |
|
66 | + /** |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + public function enqueueRecaptchaScript() |
|
70 | + { |
|
71 | + // wpforms-recaptcha |
|
72 | + // google-recaptcha |
|
73 | + // nf-google-recaptcha |
|
74 | + if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
75 | + return; |
|
76 | + } |
|
77 | + $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | + wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
79 | + 'hl' => $language, |
|
80 | + 'render' => 'explicit', |
|
81 | + ], 'https://www.google.com/recaptcha/api.js')); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function inlineScript() |
|
88 | - { |
|
89 | - $variables = [ |
|
90 | - 'action' => Application::PREFIX.'action', |
|
91 | - 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92 | - 'ajaxurl' => admin_url('admin-ajax.php'), |
|
93 | - 'nameprefix' => Application::ID, |
|
94 | - 'validationconfig' => glsr(Style::class)->validation, |
|
95 | - 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
96 | - ]; |
|
97 | - $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
98 | - wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
99 | - } |
|
84 | + /** |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function inlineScript() |
|
88 | + { |
|
89 | + $variables = [ |
|
90 | + 'action' => Application::PREFIX.'action', |
|
91 | + 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
|
92 | + 'ajaxurl' => admin_url('admin-ajax.php'), |
|
93 | + 'nameprefix' => Application::ID, |
|
94 | + 'validationconfig' => glsr(Style::class)->validation, |
|
95 | + 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
96 | + ]; |
|
97 | + $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
98 | + wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - public function inlineStyles() |
|
105 | - { |
|
106 | - $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
107 | - if (!apply_filters('site-reviews/assets/css', true)) { |
|
108 | - return; |
|
109 | - } |
|
110 | - if (!file_exists($inlineStylesheetPath)) { |
|
111 | - glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
112 | - return; |
|
113 | - } |
|
114 | - $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
115 | - $stylesheet = str_replace( |
|
116 | - array_keys($inlineStylesheetValues), |
|
117 | - array_values($inlineStylesheetValues), |
|
118 | - file_get_contents($inlineStylesheetPath) |
|
119 | - ); |
|
120 | - wp_add_inline_style(Application::ID, $stylesheet); |
|
121 | - } |
|
101 | + /** |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + public function inlineStyles() |
|
105 | + { |
|
106 | + $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
107 | + if (!apply_filters('site-reviews/assets/css', true)) { |
|
108 | + return; |
|
109 | + } |
|
110 | + if (!file_exists($inlineStylesheetPath)) { |
|
111 | + glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
112 | + return; |
|
113 | + } |
|
114 | + $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
115 | + $stylesheet = str_replace( |
|
116 | + array_keys($inlineStylesheetValues), |
|
117 | + array_values($inlineStylesheetValues), |
|
118 | + file_get_contents($inlineStylesheetPath) |
|
119 | + ); |
|
120 | + wp_add_inline_style(Application::ID, $stylesheet); |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * @return string |
|
125 | - */ |
|
126 | - protected function buildInlineScript(array $variables) |
|
127 | - { |
|
128 | - $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
129 | - foreach ($variables as $key => $value) { |
|
130 | - $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
131 | - } |
|
132 | - $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
133 | - $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
134 | - return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
135 | - } |
|
123 | + /** |
|
124 | + * @return string |
|
125 | + */ |
|
126 | + protected function buildInlineScript(array $variables) |
|
127 | + { |
|
128 | + $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
|
129 | + foreach ($variables as $key => $value) { |
|
130 | + $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
131 | + } |
|
132 | + $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
|
133 | + $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
134 | + return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
135 | + } |
|
136 | 136 | |
137 | - /** |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function getFixedSelectorsForPagination() |
|
141 | - { |
|
142 | - $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
143 | - return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
144 | - } |
|
137 | + /** |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function getFixedSelectorsForPagination() |
|
141 | + { |
|
142 | + $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
|
143 | + return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * @return string |
|
148 | - */ |
|
149 | - protected function getStylesheet() |
|
150 | - { |
|
151 | - $currentStyle = glsr(Style::class)->style; |
|
152 | - return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
153 | - ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
154 | - : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
155 | - } |
|
146 | + /** |
|
147 | + * @return string |
|
148 | + */ |
|
149 | + protected function getStylesheet() |
|
150 | + { |
|
151 | + $currentStyle = glsr(Style::class)->style; |
|
152 | + return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
153 | + ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
154 | + : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
155 | + } |
|
156 | 156 | } |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | */ |
27 | 27 | public function enqueueAssets() |
28 | 28 | { |
29 | - if (apply_filters('site-reviews/assets/css', true)) { |
|
29 | + if( apply_filters( 'site-reviews/assets/css', true ) ) { |
|
30 | 30 | wp_enqueue_style( |
31 | 31 | Application::ID, |
32 | 32 | $this->getStylesheet(), |
@@ -34,14 +34,14 @@ discard block |
||
34 | 34 | glsr()->version |
35 | 35 | ); |
36 | 36 | } |
37 | - if (apply_filters('site-reviews/assets/js', true)) { |
|
38 | - $dependencies = apply_filters('site-reviews/assets/polyfill', true) |
|
37 | + if( apply_filters( 'site-reviews/assets/js', true ) ) { |
|
38 | + $dependencies = apply_filters( 'site-reviews/assets/polyfill', true ) |
|
39 | 39 | ? [Application::ID.'/polyfill'] |
40 | 40 | : []; |
41 | - $dependencies = apply_filters('site-reviews/enqueue/public/dependencies', $dependencies); |
|
41 | + $dependencies = apply_filters( 'site-reviews/enqueue/public/dependencies', $dependencies ); |
|
42 | 42 | wp_enqueue_script( |
43 | 43 | Application::ID, |
44 | - glsr()->url('assets/scripts/'.Application::ID.'.js'), |
|
44 | + glsr()->url( 'assets/scripts/'.Application::ID.'.js' ), |
|
45 | 45 | $dependencies, |
46 | 46 | glsr()->version, |
47 | 47 | true |
@@ -54,13 +54,13 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function enqueuePolyfillService() |
56 | 56 | { |
57 | - if (!apply_filters('site-reviews/assets/polyfill', true)) { |
|
57 | + if( !apply_filters( 'site-reviews/assets/polyfill', true ) ) { |
|
58 | 58 | return; |
59 | 59 | } |
60 | - wp_enqueue_script(Application::ID.'/polyfill', add_query_arg([ |
|
60 | + wp_enqueue_script( Application::ID.'/polyfill', add_query_arg( [ |
|
61 | 61 | 'features' => 'Array.prototype.findIndex,CustomEvent,Element.prototype.closest,Element.prototype.dataset,Event,XMLHttpRequest,MutationObserver', |
62 | 62 | 'flags' => 'gated', |
63 | - ], 'https://polyfill.io/v3/polyfill.min.js')); |
|
63 | + ], 'https://polyfill.io/v3/polyfill.min.js' ) ); |
|
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
@@ -71,14 +71,14 @@ discard block |
||
71 | 71 | // wpforms-recaptcha |
72 | 72 | // google-recaptcha |
73 | 73 | // nf-google-recaptcha |
74 | - if (!glsr(OptionManager::class)->isRecaptchaEnabled()) { |
|
74 | + if( !glsr( OptionManager::class )->isRecaptchaEnabled() ) { |
|
75 | 75 | return; |
76 | 76 | } |
77 | - $language = apply_filters('site-reviews/recaptcha/language', get_locale()); |
|
78 | - wp_enqueue_script(Application::ID.'/google-recaptcha', add_query_arg([ |
|
77 | + $language = apply_filters( 'site-reviews/recaptcha/language', get_locale() ); |
|
78 | + wp_enqueue_script( Application::ID.'/google-recaptcha', add_query_arg( [ |
|
79 | 79 | 'hl' => $language, |
80 | 80 | 'render' => 'explicit', |
81 | - ], 'https://www.google.com/recaptcha/api.js')); |
|
81 | + ], 'https://www.google.com/recaptcha/api.js' ) ); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -89,13 +89,13 @@ discard block |
||
89 | 89 | $variables = [ |
90 | 90 | 'action' => Application::PREFIX.'action', |
91 | 91 | 'ajaxpagination' => $this->getFixedSelectorsForPagination(), |
92 | - 'ajaxurl' => admin_url('admin-ajax.php'), |
|
92 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
|
93 | 93 | 'nameprefix' => Application::ID, |
94 | - 'validationconfig' => glsr(Style::class)->validation, |
|
95 | - 'validationstrings' => glsr(ValidationStringsDefaults::class)->defaults(), |
|
94 | + 'validationconfig' => glsr( Style::class )->validation, |
|
95 | + 'validationstrings' => glsr( ValidationStringsDefaults::class )->defaults(), |
|
96 | 96 | ]; |
97 | - $variables = apply_filters('site-reviews/enqueue/public/localize', $variables); |
|
98 | - wp_add_inline_script(Application::ID, $this->buildInlineScript($variables), 'before'); |
|
97 | + $variables = apply_filters( 'site-reviews/enqueue/public/localize', $variables ); |
|
98 | + wp_add_inline_script( Application::ID, $this->buildInlineScript( $variables ), 'before' ); |
|
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
@@ -103,35 +103,35 @@ discard block |
||
103 | 103 | */ |
104 | 104 | public function inlineStyles() |
105 | 105 | { |
106 | - $inlineStylesheetPath = glsr()->path('assets/styles/inline-styles.css'); |
|
107 | - if (!apply_filters('site-reviews/assets/css', true)) { |
|
106 | + $inlineStylesheetPath = glsr()->path( 'assets/styles/inline-styles.css' ); |
|
107 | + if( !apply_filters( 'site-reviews/assets/css', true ) ) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | - if (!file_exists($inlineStylesheetPath)) { |
|
111 | - glsr_log()->error('Inline stylesheet is missing: '.$inlineStylesheetPath); |
|
110 | + if( !file_exists( $inlineStylesheetPath ) ) { |
|
111 | + glsr_log()->error( 'Inline stylesheet is missing: '.$inlineStylesheetPath ); |
|
112 | 112 | return; |
113 | 113 | } |
114 | - $inlineStylesheetValues = glsr()->config('inline-styles'); |
|
114 | + $inlineStylesheetValues = glsr()->config( 'inline-styles' ); |
|
115 | 115 | $stylesheet = str_replace( |
116 | - array_keys($inlineStylesheetValues), |
|
117 | - array_values($inlineStylesheetValues), |
|
118 | - file_get_contents($inlineStylesheetPath) |
|
116 | + array_keys( $inlineStylesheetValues ), |
|
117 | + array_values( $inlineStylesheetValues ), |
|
118 | + file_get_contents( $inlineStylesheetPath ) |
|
119 | 119 | ); |
120 | - wp_add_inline_style(Application::ID, $stylesheet); |
|
120 | + wp_add_inline_style( Application::ID, $stylesheet ); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | /** |
124 | 124 | * @return string |
125 | 125 | */ |
126 | - protected function buildInlineScript(array $variables) |
|
126 | + protected function buildInlineScript( array $variables ) |
|
127 | 127 | { |
128 | 128 | $script = 'window.hasOwnProperty("GLSR")||(window.GLSR={});'; |
129 | - foreach ($variables as $key => $value) { |
|
130 | - $script.= sprintf('GLSR.%s=%s;', $key, json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); |
|
129 | + foreach( $variables as $key => $value ) { |
|
130 | + $script .= sprintf( 'GLSR.%s=%s;', $key, json_encode( $value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); |
|
131 | 131 | } |
132 | 132 | $pattern = '/\"([^ \-\"]+)\"(:[{\[\"])/'; // removes unnecessary quotes surrounding object keys |
133 | - $optimizedScript = preg_replace($pattern, '$1$2', $script); |
|
134 | - return apply_filters('site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables); |
|
133 | + $optimizedScript = preg_replace( $pattern, '$1$2', $script ); |
|
134 | + return apply_filters( 'site-reviews/enqueue/public/inline-script', $optimizedScript, $script, $variables ); |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | /** |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | protected function getFixedSelectorsForPagination() |
141 | 141 | { |
142 | 142 | $selectors = ['#wpadminbar', '.site-navigation-fixed']; |
143 | - return apply_filters('site-reviews/enqueue/public/localize/ajax-pagination', $selectors); |
|
143 | + return apply_filters( 'site-reviews/enqueue/public/localize/ajax-pagination', $selectors ); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
@@ -148,9 +148,9 @@ discard block |
||
148 | 148 | */ |
149 | 149 | protected function getStylesheet() |
150 | 150 | { |
151 | - $currentStyle = glsr(Style::class)->style; |
|
152 | - return file_exists(glsr()->path('assets/styles/custom/'.$currentStyle.'.css')) |
|
153 | - ? glsr()->url('assets/styles/custom/'.$currentStyle.'.css') |
|
154 | - : glsr()->url('assets/styles/'.Application::ID.'.css'); |
|
151 | + $currentStyle = glsr( Style::class )->style; |
|
152 | + return file_exists( glsr()->path( 'assets/styles/custom/'.$currentStyle.'.css' ) ) |
|
153 | + ? glsr()->url( 'assets/styles/custom/'.$currentStyle.'.css' ) |
|
154 | + : glsr()->url( 'assets/styles/'.Application::ID.'.css' ); |
|
155 | 155 | } |
156 | 156 | } |
@@ -6,23 +6,23 @@ |
||
6 | 6 | |
7 | 7 | class StyleValidationDefaults extends Defaults |
8 | 8 | { |
9 | - /** |
|
10 | - * @return array |
|
11 | - */ |
|
12 | - protected function defaults() |
|
13 | - { |
|
14 | - return [ |
|
15 | - 'error_tag' => 'div', |
|
16 | - 'error_tag_class' => 'glsr-field-error', |
|
17 | - 'field_class' => 'glsr-field', |
|
18 | - 'field_error_class' => 'glsr-has-error', |
|
19 | - 'input_error_class' => 'glsr-is-invalid', |
|
20 | - 'input_valid_class' => 'glsr-is-valid', |
|
21 | - 'message_error_class' => 'glsr-has-errors', |
|
22 | - 'message_initial_class' => 'glsr-is-visible', |
|
23 | - 'message_success_class' => 'glsr-has-success', |
|
24 | - 'message_tag' => 'div', |
|
25 | - 'message_tag_class' => 'glsr-form-message', |
|
26 | - ]; |
|
27 | - } |
|
9 | + /** |
|
10 | + * @return array |
|
11 | + */ |
|
12 | + protected function defaults() |
|
13 | + { |
|
14 | + return [ |
|
15 | + 'error_tag' => 'div', |
|
16 | + 'error_tag_class' => 'glsr-field-error', |
|
17 | + 'field_class' => 'glsr-field', |
|
18 | + 'field_error_class' => 'glsr-has-error', |
|
19 | + 'input_error_class' => 'glsr-is-invalid', |
|
20 | + 'input_valid_class' => 'glsr-is-valid', |
|
21 | + 'message_error_class' => 'glsr-has-errors', |
|
22 | + 'message_initial_class' => 'glsr-is-visible', |
|
23 | + 'message_success_class' => 'glsr-has-success', |
|
24 | + 'message_tag' => 'div', |
|
25 | + 'message_tag_class' => 'glsr-form-message', |
|
26 | + ]; |
|
27 | + } |
|
28 | 28 | } |