PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.4.1
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 / admin / ryte / class-ryte.php

class-ryte.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4.1, at admin/ryte/class-ryte.php

231 lines 4.8 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\Admin
6 */
7
8 /**
9 * Handles the request for getting the Ryte status.
10 */
11 class WPSEO_Ryte implements WPSEO_WordPress_Integration {
12
13 /**
14 * Is the request started by pressing the fetch button.
15 *
16 * @var bool
17 */
18 private $is_manual_request = false;
19
20 /**
21 * Holds the Ryte API response.
22 *
23 * @var array
24 */
25 private $ryte_response = null;
26
27 /**
28 * Constructs the object.
29 */
30 public function __construct() {
31 $this->maybe_add_weekly_schedule();
32 }
33
34 /**
35 * Sets up the hooks.
36 *
37 * @return void
38 */
39 public function register_hooks() {
40 if ( ! self::is_active() ) {
41 return;
42 }
43
44 // Sets the action for the Ryte fetch cron job.
45 add_action( 'wpseo_ryte_fetch', [ $this, 'fetch_from_ryte' ] );
46 }
47
48 /**
49 * Determines if we can use the functionality.
50 *
51 * @return bool True if this functionality can be used.
52 */
53 public static function is_active() {
54 if ( wp_doing_ajax() ) {
55 return false;
56 }
57
58 if ( ! WPSEO_Options::get( 'ryte_indexability' ) ) {
59 return false;
60 }
61
62 return true;
63 }
64
65 /**
66 * Hooks to run on plugin activation.
67 */
68 public function activate_hooks() {
69 if ( $this->get_option()->is_enabled() ) {
70 $this->schedule_cron();
71
72 return;
73 }
74
75 $this->unschedule_cron();
76 }
77
78 /**
79 * Determines whether to add a custom cron weekly schedule.
80 *
81 * @return void
82 */
83 public function maybe_add_weekly_schedule() {
84 // If there's no default cron weekly schedule, add a custom one.
85 add_filter( 'cron_schedules', [ $this, 'add_weekly_schedule' ] );
86 }
87
88 /**
89 * Adds a custom weekly cron schedule.
90 *
91 * @param array $schedules The existing custom cron schedules.
92 *
93 * @return array Enriched list of custom cron schedules.
94 */
95 public function add_weekly_schedule( $schedules ) {
96 if ( ! is_array( $schedules ) ) {
97 $schedules = [];
98 }
99
100 /*
101 * Starting with version 5.4, WordPress does have a default weekly cron
102 * schedule. See https://core.trac.wordpress.org/changeset/47062.
103 * We need to add a custom one only if the default one doesn't exist.
104 */
105 if ( isset( $schedules['weekly'] ) ) {
106 return $schedules;
107 }
108
109 $schedules['weekly'] = [
110 'interval' => WEEK_IN_SECONDS,
111 'display' => __( 'Once Weekly', 'wordpress-seo' ),
112 ];
113
114 return $schedules;
115 }
116
117 /**
118 * Fetches the data from Ryte.
119 *
120 * @return bool|null Whether the request ran.
121 */
122 public function fetch_from_ryte() {
123 // Don't do anything when the WordPress environment type isn't "production".
124 if ( wp_get_environment_type() !== 'production' ) {
125 return;
126 }
127
128 $ryte_option = $this->get_option();
129 if ( ! $ryte_option->should_be_fetched() ) {
130 return false;
131 }
132
133 $new_status = $this->request_indexability();
134
135 // Updates the timestamp in the option.
136 $ryte_option->set_last_fetch( time() );
137
138 $ryte_option->set_status( $new_status );
139 $ryte_option->save_option();
140
141 return true;
142 }
143
144 /**
145 * Retrieves the option to use.
146 *
147 * @return WPSEO_Ryte_Option The option.
148 */
149 protected function get_option() {
150 return new WPSEO_Ryte_Option();
151 }
152
153 /**
154 * Sends a request to Ryte to get the indexability status.
155 *
156 * @return int The indexability status value.
157 */
158 protected function request_indexability() {
159 $parameters = [];
160 if ( $this->wordfence_protection_enabled() ) {
161 $parameters['wf_strict'] = 1;
162 }
163
164 $request = new WPSEO_Ryte_Request();
165 $response = $request->do_request( get_option( 'home' ), $parameters );
166
167 // Populate the ryte_response property.
168 $this->ryte_response = $response;
169
170 // It's a valid Ryte response because the array contains an `is_indexable` element.
171 if ( isset( $response['is_indexable'] ) ) {
172 return (int) $response['is_indexable'];
173 }
174
175 // It's not a valid Ryte response.
176 return WPSEO_Ryte_Option::CANNOT_FETCH;
177 }
178
179 /**
180 * Schedules the cronjob to get the new indexability status.
181 *
182 * @return void
183 */
184 private function schedule_cron() {
185 if ( wp_next_scheduled( 'wpseo_ryte_fetch' ) ) {
186 return;
187 }
188
189 wp_schedule_event( time(), 'weekly', 'wpseo_ryte_fetch' );
190 }
191
192 /**
193 * Unschedules the cronjob to get the new indexability status.
194 *
195 * @return void
196 */
197 private function unschedule_cron() {
198 if ( ! wp_next_scheduled( 'wpseo_ryte_fetch' ) ) {
199 return;
200 }
201
202 wp_clear_scheduled_hook( 'wpseo_ryte_fetch' );
203 }
204
205 /**
206 * Checks if WordFence protects the site against 'fake' Google crawlers.
207 *
208 * @return bool True if WordFence protects the site.
209 */
210 private function wordfence_protection_enabled() {
211 if ( ! class_exists( 'wfConfig' ) ) {
212 return false;
213 }
214
215 if ( ! method_exists( 'wfConfig', 'get' ) ) {
216 return false;
217 }
218
219 return (bool) wfConfig::get( 'blockFakeBots' );
220 }
221
222 /**
223 * Retrieves the Ryte API response property.
224 *
225 * @return array|WP_Error The response or WP_Error on failure.
226 */
227 public function get_response() {
228 return $this->ryte_response;
229 }
230 }
231