|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Statamic\SeoPro\Reporting\Rules; |
| 4 | + |
| 5 | +use Statamic\SeoPro\Reporting\Rule; |
| 6 | + |
| 7 | +class IdealMetaDescriptionLength extends Rule |
| 8 | +{ |
| 9 | + use Concerns\FailsWhenPagesDontPass; |
| 10 | + |
| 11 | + protected $length; |
| 12 | + protected $warnings; |
| 13 | + |
| 14 | + public function actionablePill() |
| 15 | + { |
| 16 | + return __('seo-pro::messages.rules.meta_description_length_actionable_pill'); |
| 17 | + } |
| 18 | + |
| 19 | + public function siteDescription() |
| 20 | + { |
| 21 | + return __('seo-pro::messages.rules.meta_description_length_site'); |
| 22 | + } |
| 23 | + |
| 24 | + public function pageDescription() |
| 25 | + { |
| 26 | + if (! isset($this->length) || $this->length === 0) { |
| 27 | + return __('seo-pro::messages.rules.meta_description_length_page_failing_missing'); |
| 28 | + } |
| 29 | + |
| 30 | + $warnMin = config('statamic.seo-pro.reports.meta_description_length.warn_min', 120); |
| 31 | + $warnMax = config('statamic.seo-pro.reports.meta_description_length.warn_max', 240); |
| 32 | + |
| 33 | + if ($this->length < $warnMin) { |
| 34 | + return __('seo-pro::messages.rules.meta_description_length_page_failing_too_short', [ |
| 35 | + 'length' => $this->length, |
| 36 | + 'min' => $warnMin, |
| 37 | + ]); |
| 38 | + } |
| 39 | + |
| 40 | + if ($this->length > $warnMax) { |
| 41 | + return __('seo-pro::messages.rules.meta_description_length_page_failing_too_long', [ |
| 42 | + 'length' => $this->length, |
| 43 | + 'max' => $warnMax, |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + return __('seo-pro::messages.rules.meta_description_length_page'); |
| 48 | + } |
| 49 | + |
| 50 | + public function siteFailingComment() |
| 51 | + { |
| 52 | + return trans_choice( |
| 53 | + 'seo-pro::messages.rules.meta_description_length_site_failing', |
| 54 | + $this->failures, |
| 55 | + ['count' => $this->failures] |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function processSite() |
| 60 | + { |
| 61 | + $this->failures = $this->warnings = 0; |
| 62 | + |
| 63 | + $this->report->pages()->each(function ($page) { |
| 64 | + $rule = new static; |
| 65 | + |
| 66 | + $rule |
| 67 | + ->setPage($page) |
| 68 | + ->setReport($this->report) |
| 69 | + ->load($page->results()[$this->id()]); |
| 70 | + |
| 71 | + $status = $rule->status(); |
| 72 | + |
| 73 | + if ($status === 'fail') { |
| 74 | + $this->failures++; |
| 75 | + } elseif ($status === 'warning') { |
| 76 | + $this->warnings++; |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + public function siteStatus() |
| 82 | + { |
| 83 | + if ($this->failures > 0) { |
| 84 | + return 'fail'; |
| 85 | + } |
| 86 | + |
| 87 | + if ($this->warnings > 0) { |
| 88 | + return 'warning'; |
| 89 | + } |
| 90 | + |
| 91 | + return 'pass'; |
| 92 | + } |
| 93 | + |
| 94 | + public function saveSite() |
| 95 | + { |
| 96 | + return [ |
| 97 | + 'failures' => $this->failures, |
| 98 | + 'warnings' => $this->warnings, |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + public function loadSite($data) |
| 103 | + { |
| 104 | + if (! $data) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + $this->failures = $data['failures'] ?? 0; |
| 109 | + $this->warnings = $data['warnings'] ?? 0; |
| 110 | + } |
| 111 | + |
| 112 | + public function pageFailingComment() |
| 113 | + { |
| 114 | + $warnMax = config('statamic.seo-pro.reports.meta_description_length.warn_max', 240); |
| 115 | + |
| 116 | + if ($this->length === 0) { |
| 117 | + return __('seo-pro::messages.rules.meta_description_length_page_failing_missing'); |
| 118 | + } |
| 119 | + |
| 120 | + return __('seo-pro::messages.rules.meta_description_length_page_failing_too_long', [ |
| 121 | + 'length' => $this->length, |
| 122 | + 'max' => $warnMax, |
| 123 | + ]); |
| 124 | + } |
| 125 | + |
| 126 | + public function pagePassingComment() |
| 127 | + { |
| 128 | + return __('seo-pro::messages.rules.meta_description_length_page_passing', ['length' => $this->length]); |
| 129 | + } |
| 130 | + |
| 131 | + public function pageWarningComment() |
| 132 | + { |
| 133 | + return __('seo-pro::messages.rules.meta_description_length_page_warning', ['length' => $this->length]); |
| 134 | + } |
| 135 | + |
| 136 | + public function siteWarningComment() |
| 137 | + { |
| 138 | + return trans_choice( |
| 139 | + 'seo-pro::messages.rules.meta_description_length_site_warning', |
| 140 | + $this->warnings, |
| 141 | + ['count' => $this->warnings] |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + public function processPage() |
| 146 | + { |
| 147 | + $this->length = strlen($this->page->get('description', '')); |
| 148 | + } |
| 149 | + |
| 150 | + public function pageStatus() |
| 151 | + { |
| 152 | + $warnMin = config('statamic.seo-pro.reports.meta_description_length.warn_min', 120); |
| 153 | + $passMax = config('statamic.seo-pro.reports.meta_description_length.pass_max', 160); |
| 154 | + $warnMax = config('statamic.seo-pro.reports.meta_description_length.warn_max', 240); |
| 155 | + |
| 156 | + if ($this->length === 0) { |
| 157 | + return 'fail'; |
| 158 | + } |
| 159 | + |
| 160 | + if ($this->length < $warnMin || ($this->length > $passMax && $this->length <= $warnMax)) { |
| 161 | + return 'warning'; |
| 162 | + } |
| 163 | + |
| 164 | + if ($this->length > $warnMax) { |
| 165 | + return 'fail'; |
| 166 | + } |
| 167 | + |
| 168 | + return 'pass'; |
| 169 | + } |
| 170 | + |
| 171 | + public function savePage() |
| 172 | + { |
| 173 | + return $this->length; |
| 174 | + } |
| 175 | + |
| 176 | + public function loadPage($data) |
| 177 | + { |
| 178 | + $this->length = $data; |
| 179 | + } |
| 180 | +} |
0 commit comments