PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / services / health-check / report-builder.php

report-builder.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.2, at src/services/health-check/report-builder.php

208 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Services\Health_Check;
4
5 /**
6 * Provides an interface to build WordPress-friendly health check results.
7 */
8 class Report_Builder {
9
10 /**
11 * Passed health check.
12 */
13 const STATUS_GOOD = 'good';
14
15 /**
16 * Changes are recommended but not necessary.
17 */
18 const STATUS_RECOMMENDED = 'recommended';
19
20 /**
21 * Significant issues that the user should consider fixing.
22 */
23 const STATUS_CRITICAL = 'critical';
24
25 /**
26 * The user-facing label.
27 *
28 * @var string
29 */
30 private $label = '';
31
32 /**
33 * The identifier that WordPress uses for the health check.
34 *
35 * @var string
36 */
37 private $test_identifier = '';
38
39 /**
40 * The test status (good, recommended, critical).
41 *
42 * @var string
43 */
44 private $status = '';
45
46 /**
47 * The short description for the result.
48 *
49 * @var string
50 */
51 private $description = '';
52
53 /**
54 * Actions that the user can take to solve the health check result.
55 *
56 * @var string
57 */
58 private $actions = '';
59
60 /**
61 * Sets the label for the health check that the user can see.
62 *
63 * @param string $label The label that the user can see.
64 * @return Report_Builder This builder.
65 */
66 public function set_label( $label ) {
67 $this->label = $label;
68 return $this;
69 }
70
71 /**
72 * Sets the name for the test that the plugin uses to identify the test.
73 *
74 * @param string $test_identifier The identifier for the health check.
75 * @return Report_Builder This builder.
76 */
77 public function set_test_identifier( $test_identifier ) {
78 $this->test_identifier = $test_identifier;
79 return $this;
80 }
81
82 /**
83 * Sets the status of the test result to GOOD (green label).
84 *
85 * @return Report_Builder This builder.
86 */
87 public function set_status_good() {
88 $this->status = self::STATUS_GOOD;
89 return $this;
90 }
91
92 /**
93 * Sets the status of the test result to RECOMMENDED (orange label).
94 *
95 * @return Report_Builder This builder.
96 */
97 public function set_status_recommended() {
98 $this->status = self::STATUS_RECOMMENDED;
99 return $this;
100 }
101
102 /**
103 * Sets the status of the test result to CRITICAL (red label).
104 *
105 * @return Report_Builder This builder.
106 */
107 public function set_status_critical() {
108 $this->status = self::STATUS_CRITICAL;
109 return $this;
110 }
111
112 /**
113 * Sets a description for the test result. This will be the heading for the result in the user interface.
114 *
115 * @param string $description The description for the test result.
116 * @return Report_Builder This builder.
117 */
118 public function set_description( $description ) {
119 $this->description = $description;
120 return $this;
121 }
122
123 /**
124 * Sets a text that describes how the user can solve the failed health check.
125 *
126 * @param string $actions The descriptive text.
127 * @return Report_Builder This builder.
128 */
129 public function set_actions( $actions ) {
130 $this->actions = $actions;
131 return $this;
132 }
133
134 /**
135 * Builds an array of strings in the format that WordPress uses to display health checks (https://developer.wordpress.org/reference/hooks/site_status_test_result/).
136 *
137 * @return string[] The report in WordPress' site status report format.
138 */
139 public function build() {
140 return [
141 'label' => $this->label,
142 'status' => $this->status,
143 'badge' => $this->get_badge(),
144 'description' => $this->description,
145 'actions' => $this->get_actions_with_signature(),
146 'test' => $this->test_identifier,
147 ];
148 }
149
150 /**
151 * Generates a badge that the user can see.
152 *
153 * @return string[] The badge.
154 */
155 private function get_badge() {
156 return [
157 'label' => $this->get_badge_label(),
158 'color' => $this->get_badge_color(),
159 ];
160 }
161
162 /**
163 * Generates the label for a badge.
164 *
165 * @return string The badge label.
166 */
167 private function get_badge_label() {
168 return __( 'SEO', 'wordpress-seo' );
169 }
170
171 /**
172 * Generates the color for the badge using the current status.
173 *
174 * @return string The color for the badge's outline.
175 */
176 private function get_badge_color() {
177 if ( $this->status === self::STATUS_CRITICAL || $this->status === self::STATUS_RECOMMENDED ) {
178 return 'red';
179 }
180
181 return 'blue';
182 }
183
184 /**
185 * Concatenates the set actions with Yoast's signature.
186 *
187 * @return string A string containing the set actions and Yoast's signature.
188 */
189 private function get_actions_with_signature() {
190 return $this->actions . $this->get_signature();
191 }
192
193 /**
194 * Generates Yoast's signature that's displayed at the bottom of the health check result.
195 *
196 * @return string Yoast's signature as an HTML string.
197 */
198 private function get_signature() {
199 return sprintf(
200 /* translators: 1: Start of a paragraph beginning with the Yoast icon, 2: Expands to 'Yoast SEO', 3: Paragraph closing tag. */
201 esc_html__( '%1$sThis was reported by the %2$s plugin%3$s', 'wordpress-seo' ),
202 '<p class="yoast-site-health__signature"><img src="' . esc_url( plugin_dir_url( WPSEO_FILE ) . 'packages/js/images/Yoast_SEO_Icon.svg' ) . '" alt="" height="20" width="20" class="yoast-site-health__signature-icon">',
203 'Yoast SEO',
204 '</p>'
205 );
206 }
207 }
208