Passed
Push — master ( 71e49e...43cd94 )
by zyt
01:52
created
src/GoogleFontsOptimizer.php 2 patches
Spacing   +80 added lines, -80 removed lines patch added patch discarded remove patch
@@ -13,9 +13,9 @@  discard block
 block discarded – undo
13 13
     const FILTER_OB_CLEANER  = 'zwf_gfo_clean_ob';
14 14
     const DEFAULT_OB_CLEANER = false;
15 15
 
16
-    protected $candidates = [];
16
+    protected $candidates = [ ];
17 17
 
18
-    protected $enqueued = [];
18
+    protected $enqueued = [ ];
19 19
 
20 20
     /**
21 21
      * Like the wind.
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
                  * Scan and optimize requests found in the markup (uses more
34 34
                  * memory but works on [almost] any theme)
35 35
                  */
36
-                add_action('template_redirect', [$this, 'startBuffering'], 11);
36
+                add_action('template_redirect', [ $this, 'startBuffering' ], 11);
37 37
                 break;
38 38
 
39 39
             case self::DEFAULT_OPERATION_MODE:
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
                  * Scan only things added via wp_enqueue_style (uses slightly
43 43
                  * less memory usually, but requires a decently coded theme)
44 44
                  */
45
-                add_filter('print_styles_array', [$this, 'processStylesHandles']);
45
+                add_filter('print_styles_array', [ $this, 'processStylesHandles' ]);
46 46
                 break;
47 47
         }
48 48
     }
@@ -54,7 +54,7 @@  discard block
 block discarded – undo
54 54
      *
55 55
      * @return bool
56 56
      */
57
-    protected function hasEnoughElements(array $candidates = [])
57
+    protected function hasEnoughElements(array $candidates = [ ])
58 58
     {
59 59
         $enough = true;
60 60
 
@@ -82,25 +82,25 @@  discard block
 block discarded – undo
82 82
         $candidate_handles = $this->findCandidateHandles($handles);
83 83
 
84 84
         // Bail if we don't have anything that makes sense for us to continue
85
-        if (! $this->hasEnoughElements($candidate_handles)) {
85
+        if (!$this->hasEnoughElements($candidate_handles)) {
86 86
             return $handles;
87 87
         }
88 88
 
89 89
         $fonts_array = $this->getFontsArray($this->getCandidates());
90
-        if (isset($fonts_array['complete'])) {
90
+        if (isset($fonts_array[ 'complete' ])) {
91 91
             $combined_font_url = $this->buildGoogleFontsUrlFromFontsArray($fonts_array);
92 92
             $handle_name       = 'zwf-gfo-combined';
93 93
             $this->enqueueStyle($handle_name, $combined_font_url);
94
-            $handles[] = $handle_name;
94
+            $handles[ ] = $handle_name;
95 95
         }
96 96
 
97
-        if (isset($fonts_array['partial'])) {
97
+        if (isset($fonts_array[ 'partial' ])) {
98 98
             $cnt = 0;
99
-            foreach ($fonts_array['partial']['url'] as $url) {
99
+            foreach ($fonts_array[ 'partial' ][ 'url' ] as $url) {
100 100
                 $cnt++;
101 101
                 $handle_name = 'zwf-gfo-combined-txt-' . $cnt;
102 102
                 $this->enqueueStyle($handle_name, $url);
103
-                $handles[] = $handle_name;
103
+                $handles[ ] = $handle_name;
104 104
             }
105 105
         }
106 106
 
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
     public function findCandidateHandles(array $handles)
129 129
     {
130 130
         $handler           = \wp_styles();
131
-        $candidate_handles = [];
131
+        $candidate_handles = [ ];
132 132
 
133 133
         foreach ($handles as $handle) {
134 134
             // $url = $handler->registered[ $handle ]->src;
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
                 $url = $dep->src;
138 138
                 if ($this->isGoogleWebFontUrl($url)) {
139 139
                     $this->addCandidate($url);
140
-                    $candidate_handles[$handle] = $url;
140
+                    $candidate_handles[ $handle ] = $url;
141 141
                 }
142 142
             }
143 143
         }
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
                 \wp_dequeue_style($handle);
164 164
             }
165 165
             // @codeCoverageIgnoreEnd
166
-            unset($this->enqueued[$handle]);
166
+            unset($this->enqueued[ $handle ]);
167 167
         }
168 168
     }
169 169
 
@@ -178,7 +178,7 @@  discard block
 block discarded – undo
178 178
      *
179 179
      * @return void
180 180
      */
181
-    public function enqueueStyle($handle, $url, $deps = [], $version = null)
181
+    public function enqueueStyle($handle, $url, $deps = [ ], $version = null)
182 182
     {
183 183
         // @codeCoverageIgnoreStart
184 184
         // \wp_register_style($handle, $url, $deps, $version);
@@ -187,7 +187,7 @@  discard block
 block discarded – undo
187 187
         }
188 188
         // @codeCoverageIgnoreEnd
189 189
 
190
-        $this->enqueued[$handle] = $url;
190
+        $this->enqueued[ $handle ] = $url;
191 191
     }
192 192
 
193 193
     /**
@@ -201,8 +201,8 @@  discard block
 block discarded – undo
201 201
     {
202 202
         $data = $this->enqueued;
203 203
 
204
-        if ($handle && isset($this->enqueued[$handle])) {
205
-            $data = $this->enqueued[$handle];
204
+        if ($handle && isset($this->enqueued[ $handle ])) {
205
+            $data = $this->enqueued[ $handle ];
206 206
         }
207 207
 
208 208
         return $data;
@@ -230,7 +230,7 @@  discard block
 block discarded – undo
230 230
             $rel  = null;
231 231
             $type = null;
232 232
             $href = null;
233
-            $find = ['rel', 'type', 'href'];
233
+            $find = [ 'rel', 'type', 'href' ];
234 234
             foreach ($find as $attr) {
235 235
                 if ($link_node->hasAttribute($attr)) {
236 236
                     $$attr = $link_node->getAttribute($attr);
@@ -246,14 +246,14 @@  discard block
 block discarded – undo
246 246
         $candidates = $this->getCandidates();
247 247
 
248 248
         // Bail and return original markup unmodified if we don't have things to do
249
-        if (! $this->hasEnoughElements($candidates)) {
249
+        if (!$this->hasEnoughElements($candidates)) {
250 250
             return $markup;
251 251
         }
252 252
 
253 253
         // Process what we found and modify original markup with our replacement
254 254
         $fonts_array = $this->getFontsArray($candidates);
255 255
         $font_markup = $this->buildFontsMarkup($fonts_array);
256
-        $markup      = $this->modifyMarkup($markup, $font_markup, $fonts_array['links']);
256
+        $markup      = $this->modifyMarkup($markup, $font_markup, $fonts_array[ 'links' ]);
257 257
 
258 258
         return $markup;
259 259
     }
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
             }*/
306 306
 
307 307
             // Start our own buffering
308
-            $started = ob_start([$this, 'endBuffering']);
308
+            $started = ob_start([ $this, 'endBuffering' ]);
309 309
         }
310 310
 
311 311
         return $started;
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
     public function endBuffering($markup)
315 315
     {
316 316
         // Bail early on things we don't want to parse
317
-        if (! $this->isMarkupDoable($markup)) {
317
+        if (!$this->isMarkupDoable($markup)) {
318 318
             return $markup;
319 319
         }
320 320
 
@@ -399,7 +399,7 @@  discard block
 block discarded – undo
399 399
             $is_amp_markup = $this->isAmpMarkup($content);
400 400
         }
401 401
 
402
-        if ($has_no_html_tag && ! $has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet) {
402
+        if ($has_no_html_tag && !$has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet) {
403 403
             $doable = false;
404 404
         }
405 405
 
@@ -432,7 +432,7 @@  discard block
 block discarded – undo
432 432
     /**
433 433
      * @param array $urls
434 434
      */
435
-    public function setCandidates(array $urls = [])
435
+    public function setCandidates(array $urls = [ ])
436 436
     {
437 437
         $this->candidates = $urls;
438 438
     }
@@ -442,7 +442,7 @@  discard block
 block discarded – undo
442 442
      */
443 443
     public function addCandidate($url)
444 444
     {
445
-        $this->candidates[] = $url;
445
+        $this->candidates[ ] = $url;
446 446
     }
447 447
 
448 448
     /**
@@ -484,11 +484,11 @@  discard block
 block discarded – undo
484 484
      *
485 485
      * @return null|string
486 486
      */
487
-    public function buildGoogleFontsUrl(array $fonts, $subsets = [])
487
+    public function buildGoogleFontsUrl(array $fonts, $subsets = [ ])
488 488
     {
489 489
         $base_url  = 'https://fonts.googleapis.com/css';
490
-        $font_args = [];
491
-        $family    = [];
490
+        $font_args = [ ];
491
+        $family    = [ ];
492 492
         $url       = null;
493 493
 
494 494
         foreach ($fonts as $font_name => $font_weight) {
@@ -496,17 +496,17 @@  discard block
 block discarded – undo
496 496
                 $font_weight = implode(',', $font_weight);
497 497
             }
498 498
             // Trimming end colon handles edge case of being given an empty $font_weight
499
-            $family[] = trim(trim($font_name) . ':' . trim($font_weight), ':');
499
+            $family[ ] = trim(trim($font_name) . ':' . trim($font_weight), ':');
500 500
         }
501 501
 
502
-        $font_args['family'] = implode('|', $family);
502
+        $font_args[ 'family' ] = implode('|', $family);
503 503
 
504
-        if (! empty($subsets)) {
504
+        if (!empty($subsets)) {
505 505
             if (is_array($subsets)) {
506 506
                 $subsets = array_unique($subsets);
507 507
                 $subsets = implode(',', $subsets);
508 508
             }
509
-            $font_args['subset'] = trim($subsets);
509
+            $font_args[ 'subset' ] = trim($subsets);
510 510
         }
511 511
 
512 512
         $url = $base_url . '?' . http_build_query($font_args);
@@ -522,26 +522,26 @@  discard block
 block discarded – undo
522 522
      *
523 523
      * @return array
524 524
      */
525
-    protected function getFontsArray(array $candidates = [])
525
+    protected function getFontsArray(array $candidates = [ ])
526 526
     {
527
-        $fonts = [];
527
+        $fonts = [ ];
528 528
 
529 529
         foreach ($candidates as $candidate) {
530
-            $fonts['links'][] = $candidate;
530
+            $fonts[ 'links' ][ ] = $candidate;
531 531
 
532
-            $params = [];
532
+            $params = [ ];
533 533
             parse_str(parse_url($candidate, PHP_URL_QUERY), $params);
534 534
 
535
-            if (isset($params['text'])) {
535
+            if (isset($params[ 'text' ])) {
536 536
                 // Fonts with character limitations are segregated into
537 537
                 // under 'partial' (when `text` query param is used)
538
-                $font_family                = explode(':', $params['family']);
539
-                $fonts['partial']['name'][] = $font_family[0];
540
-                $fonts['partial']['url'][]  = $this->httpsify($candidate);
538
+                $font_family                = explode(':', $params[ 'family' ]);
539
+                $fonts[ 'partial' ][ 'name' ][ ] = $font_family[ 0 ];
540
+                $fonts[ 'partial' ][ 'url' ][ ]  = $this->httpsify($candidate);
541 541
             } else {
542 542
                 $fontstrings = $this->buildFontStringsFromQueryParams($params);
543 543
                 foreach ($fontstrings as $font) {
544
-                    $fonts['complete'][] = $font;
544
+                    $fonts[ 'complete' ][ ] = $font;
545 545
                 }
546 546
             }
547 547
         }
@@ -560,29 +560,29 @@  discard block
 block discarded – undo
560 560
      */
561 561
     protected function buildFontStringsFromQueryParams(array $params)
562 562
     {
563
-        $fonts = [];
563
+        $fonts = [ ];
564 564
 
565
-        foreach (explode('|', $params['family']) as $families) {
565
+        foreach (explode('|', $params[ 'family' ]) as $families) {
566 566
             $font_family = explode(':', $families);
567 567
             $subset      = false;
568
-            if (isset($params['subset'])) {
568
+            if (isset($params[ 'subset' ])) {
569 569
                 // Use the found subset parameter
570
-                $subset = $params['subset'];
571
-            } elseif (isset($font_family[2])) {
570
+                $subset = $params[ 'subset' ];
571
+            } elseif (isset($font_family[ 2 ])) {
572 572
                 // Use the subset in the family string
573
-                $subset = $font_family[2];
573
+                $subset = $font_family[ 2 ];
574 574
             }
575 575
 
576 576
             // We need to have a name and a size
577
-            if (strlen($font_family[0]) > 0 && strlen($font_family[1]) > 0) {
577
+            if (strlen($font_family[ 0 ]) > 0 && strlen($font_family[ 1 ]) > 0) {
578 578
                 $parts = [
579
-                    $font_family[0],
580
-                    $font_family[1]
579
+                    $font_family[ 0 ],
580
+                    $font_family[ 1 ]
581 581
                 ];
582 582
                 if ($subset) {
583
-                    $parts[] = $subset;
583
+                    $parts[ ] = $subset;
584 584
                 }
585
-                $fonts[] = implode(':', $parts);
585
+                $fonts[ ] = implode(':', $parts);
586 586
             }
587 587
         }
588 588
 
@@ -616,37 +616,37 @@  discard block
 block discarded – undo
616 616
      */
617 617
     protected function consolidateFontsArray(array $fonts_array)
618 618
     {
619
-        $fonts         = [];
620
-        $subsets       = [];
621
-        $fonts_to_subs = [];
619
+        $fonts         = [ ];
620
+        $subsets       = [ ];
621
+        $fonts_to_subs = [ ];
622 622
 
623
-        foreach ($fonts_array['complete'] as $font_string) {
623
+        foreach ($fonts_array[ 'complete' ] as $font_string) {
624 624
             $parts = explode(':', $font_string);
625
-            $name  = $parts[0];
626
-            $size  = $parts[1];
625
+            $name  = $parts[ 0 ];
626
+            $size  = $parts[ 1 ];
627 627
 
628
-            if (isset($fonts[$name])) {
628
+            if (isset($fonts[ $name ])) {
629 629
                 // If a name already exists, append the new size
630
-                $fonts[$name] .= ',' . $size;
630
+                $fonts[ $name ] .= ',' . $size;
631 631
             } else {
632 632
                 // Create a new key for the name and size
633
-                $fonts[$name] = $size;
633
+                $fonts[ $name ] = $size;
634 634
             }
635 635
 
636 636
             // Check if subset is specified as the third element
637
-            if (isset($parts[2])) {
638
-                $subset = $parts[2];
637
+            if (isset($parts[ 2 ])) {
638
+                $subset = $parts[ 2 ];
639 639
                 // Collect all the subsets defined into a single array
640 640
                 $elements = explode(',', $subset);
641 641
                 foreach ($elements as $sub) {
642
-                    $subsets[] = $sub;
642
+                    $subsets[ ] = $sub;
643 643
                 }
644 644
                 // Keeping a separate map of names => requested subsets for
645 645
                 // webfontloader purposes
646
-                if (isset($fonts_to_subs[$name])) {
647
-                    $fonts_to_subs[$name] .= ',' . $subset;
646
+                if (isset($fonts_to_subs[ $name ])) {
647
+                    $fonts_to_subs[ $name ] .= ',' . $subset;
648 648
                 } else {
649
-                    $fonts_to_subs[$name] = $subset;
649
+                    $fonts_to_subs[ $name ] = $subset;
650 650
                 }
651 651
             }
652 652
         }
@@ -663,7 +663,7 @@  discard block
 block discarded – undo
663 663
         // Sanitize and de-dup $fonts_to_subs mapping
664 664
         $fonts_to_subs = $this->dedupValues($fonts_to_subs, false); // no sort
665 665
 
666
-        return [$fonts, $subsets, $fonts_to_subs];
666
+        return [ $fonts, $subsets, $fonts_to_subs ];
667 667
     }
668 668
 
669 669
     /**
@@ -688,7 +688,7 @@  discard block
 block discarded – undo
688 688
             if (false !== $sort) {
689 689
                 sort($parts, $sort);
690 690
             }
691
-            $data[$key] = $parts;
691
+            $data[ $key ] = $parts;
692 692
         }
693 693
 
694 694
         return $data;
@@ -750,9 +750,9 @@  discard block
 block discarded – undo
750 750
             $href     = $this->encodeUnencodedAmpersands($font_url);
751 751
             $markup   = '<link rel="stylesheet" type="text/css" href="' . $href . '">';
752 752
 
753
-            if (isset($fonts_array['partial'])) {
754
-                if (is_array($fonts_array['partial']['url'])) {
755
-                    foreach ($fonts_array['partial']['url'] as $other) {
753
+            if (isset($fonts_array[ 'partial' ])) {
754
+                if (is_array($fonts_array[ 'partial' ][ 'url' ])) {
755
+                    foreach ($fonts_array[ 'partial' ][ 'url' ] as $other) {
756 756
                         $markup .= '<link rel="stylesheet" type="text/css"';
757 757
                         $markup .= ' href="' . $this->encodeUnencodedAmpersands($other) . '">';
758 758
                     }
@@ -760,24 +760,24 @@  discard block
 block discarded – undo
760 760
             }
761 761
         } else {
762 762
             // Bulding WebFont script loader
763
-            $families_array = [];
763
+            $families_array = [ ];
764 764
 
765 765
             list($fonts, $subsets, $mapping) = $this->consolidateFontsArray($fonts_array);
766 766
             foreach ($fonts as $name => $sizes) {
767 767
                 $family = $name . ':' . implode(',', $sizes);
768
-                if (isset($mapping[$name])) {
769
-                    $family .= ':' . implode(',', $mapping[$name]);
768
+                if (isset($mapping[ $name ])) {
769
+                    $family .= ':' . implode(',', $mapping[ $name ]);
770 770
                 }
771
-                $families_array[] = $family;
771
+                $families_array[ ] = $family;
772 772
             }
773 773
             $families = "'" . implode("', '", $families_array) . "'";
774 774
 
775 775
             // Load 'text' requests with the "custom" module
776 776
             $custom = '';
777
-            if (isset($fonts_array['partial'])) {
777
+            if (isset($fonts_array[ 'partial' ])) {
778 778
                 $custom  = ",\n    custom: {\n";
779
-                $custom .= "        families: [ '" . implode("', '", $fonts_array['partial']['name']) . "' ],\n";
780
-                $custom .= "        urls: [ '" . implode("', '", $fonts_array['partial']['url']) . "' ]\n";
779
+                $custom .= "        families: [ '" . implode("', '", $fonts_array[ 'partial' ][ 'name' ]) . "' ],\n";
780
+                $custom .= "        urls: [ '" . implode("', '", $fonts_array[ 'partial' ][ 'url' ]) . "' ]\n";
781 781
                 $custom .= '    }';
782 782
             }
783 783
 
Please login to merge, or discard this patch.
Upper-Lower-Casing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -796,7 +796,7 @@
 block discarded – undo
796 796
     s.parentNode.insertBefore(wf, s);
797 797
 })();
798 798
 </script>
799
-HTML;
799
+html;
800 800
         }
801 801
 
802 802
         return $markup;
Please login to merge, or discard this patch.