Passed
Push — master ( 9c19c1...8479fb )
by Paul
14:24 queued 07:17
created
plugin/Modules/Schema.php 1 patch
Spacing   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -30,21 +30,21 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
     /**
@@ -283,8 +283,8 @@  discard block
 block discarded – undo
283 283
         $excerpt = has_excerpt()
284 284
             ? get_the_excerpt()
285 285
             : get_the_content();
286
-        $description = strip_shortcodes(wp_strip_all_tags($excerpt));
287
-        return wp_trim_words($description, apply_filters('excerpt_length', 55));
286
+        $description = strip_shortcodes( wp_strip_all_tags( $excerpt ) );
287
+        return wp_trim_words( $description, apply_filters( 'excerpt_length', 55 ) );
288 288
     }
289 289
 
290 290
     /**
@@ -292,7 +292,7 @@  discard block
 block discarded – undo
292 292
      */
293 293
     protected function getThingImage()
294 294
     {
295
-        return (string) get_the_post_thumbnail_url(null, 'large');
295
+        return (string)get_the_post_thumbnail_url( null, 'large' );
296 296
     }
297 297
 
298 298
     /**
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
      */
309 309
     protected function getThingUrl()
310 310
     {
311
-        return (string) get_the_permalink();
311
+        return (string)get_the_permalink();
312 312
     }
313 313
 
314 314
     /**
@@ -316,7 +316,7 @@  discard block
 block discarded – undo
316 316
      * @param string $value
317 317
      * @return string
318 318
      */
319
-    protected function setAndGetKeyValue($option, $value)
319
+    protected function setAndGetKeyValue( $option, $value )
320 320
     {
321 321
         $this->keyValues[$option] = $value;
322 322
         return $value;
Please login to merge, or discard this patch.