PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
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 / inc / health-check.php

health-check.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.0, at inc/health-check.php

216 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Internals
6 */
7
8 /**
9 * Represents the abstract class for the health check.
10 */
11 abstract class WPSEO_Health_Check {
12
13 /**
14 * The health check section in which 'good' results should be shown.
15 *
16 * @var string
17 */
18 const STATUS_GOOD = 'good';
19
20 /**
21 * The health check section in which 'recommended' results should be shown.
22 *
23 * @var string
24 */
25 const STATUS_RECOMMENDED = 'recommended';
26
27 /**
28 * The health check section in which 'critical' results should be shown.
29 *
30 * @var string
31 */
32 const STATUS_CRITICAL = 'critical';
33
34 /**
35 * The value of the section header in the Health check.
36 *
37 * @var string
38 */
39 protected $label = '';
40
41 /**
42 * Section the result should be displayed in.
43 *
44 * @var string
45 */
46 protected $status = '';
47
48 /**
49 * What the badge should say with a color.
50 *
51 * @var array
52 */
53 protected $badge = [
54 'label' => '',
55 'color' => '',
56 ];
57
58 /**
59 * Additional details about the results of the test.
60 *
61 * @var string
62 */
63 protected $description = '';
64
65 /**
66 * A link or button to allow the end user to take action on the result.
67 *
68 * @var string
69 */
70 protected $actions = '';
71
72 /**
73 * The name of the test.
74 *
75 * @var string
76 */
77 protected $test = '';
78
79 /**
80 * Whether or not the test should be ran on AJAX as well.
81 *
82 * @var bool True when is async, default false.
83 */
84 protected $async = false;
85
86 /**
87 * Runs the test and returns the result.
88 */
89 abstract public function run();
90
91 /**
92 * Registers the test to WordPress.
93 */
94 public function register_test() {
95 if ( $this->is_async() ) {
96 add_filter( 'site_status_tests', [ $this, 'add_async_test' ] );
97
98 add_action( 'wp_ajax_health-check-' . $this->get_test_name(), [ $this, 'get_async_test_result' ] );
99
100 return;
101 }
102
103 add_filter( 'site_status_tests', [ $this, 'add_test' ] );
104 }
105
106 /**
107 * Runs the test.
108 *
109 * @param array $tests Array with the current tests.
110 *
111 * @return array The extended array.
112 */
113 public function add_test( $tests ) {
114 $tests['direct'][ $this->get_test_name() ] = [
115 'test' => [ $this, 'get_test_result' ],
116 ];
117
118 return $tests;
119 }
120
121 /**
122 * Runs the test in async mode.
123 *
124 * @param array $tests Array with the current tests.
125 *
126 * @return array The extended array.
127 */
128 public function add_async_test( $tests ) {
129 $tests['async'][ $this->get_test_name() ] = [
130 'test' => $this->get_test_name(),
131 ];
132
133 return $tests;
134 }
135
136 /**
137 * Formats the test result as an array.
138 *
139 * @return array The formatted test result.
140 */
141 public function get_test_result() {
142 $this->run();
143 $this->add_yoast_signature();
144
145 return [
146 'label' => $this->label,
147 'status' => $this->status,
148 'badge' => $this->get_badge(),
149 'description' => $this->description,
150 'actions' => $this->actions,
151 'test' => $this->test,
152 ];
153 }
154
155 /**
156 * Formats the test result as JSON as response to an AJAX request.
157 */
158 public function get_async_test_result() {
159 $result = $this->get_test_result();
160 wp_send_json_success( $result );
161 }
162
163 /**
164 * Retrieves the badge and ensure usable values are set.
165 *
166 * @return array The proper formatted badge.
167 */
168 protected function get_badge() {
169 if ( ! is_array( $this->badge ) ) {
170 $this->badge = [];
171 }
172
173 if ( empty( $this->badge['label'] ) ) {
174 $this->badge['label'] = __( 'SEO', 'wordpress-seo' );
175 }
176
177 if ( empty( $this->badge['color'] ) ) {
178 $this->badge['color'] = 'green';
179 }
180
181 return $this->badge;
182 }
183
184 /**
185 * WordPress converts the underscores to dashes. To prevent issues we have
186 * to do it as well.
187 *
188 * @return string The formatted testname.
189 */
190 protected function get_test_name() {
191 return str_replace( '_', '-', $this->test );
192 }
193
194 /**
195 * Checks if the health check is async.
196 *
197 * @return bool True when check is async.
198 */
199 protected function is_async() {
200 return ! empty( $this->async );
201 }
202
203 /**
204 * Adds a text to the bottom of the Site Health check to indicate it is a Yoast SEO Site Health Check.
205 */
206 protected function add_yoast_signature() {
207 $this->actions .= sprintf(
208 /* translators: 1: Start of a paragraph beginning with the Yoast icon, 2: Expands to 'Yoast SEO', 3: Paragraph closing tag. */
209 esc_html__( '%1$sThis was reported by the %2$s plugin%3$s', 'wordpress-seo' ),
210 '<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">',
211 'Yoast SEO',
212 '</p>'
213 );
214 }
215 }
216