PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.3
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 / tracking / class-tracking.php

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

231 lines 6.4 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\Tracking
6 */
7
8 /**
9 * This class handles the tracking routine.
10 */
11 class WPSEO_Tracking implements WPSEO_WordPress_Integration {
12
13 /**
14 * The tracking option name.
15 *
16 * @var string
17 */
18 protected $option_name = 'wpseo_tracking_last_request';
19
20 /**
21 * The limit for the option.
22 *
23 * @var int
24 */
25 protected $threshold = 0;
26
27 /**
28 * The endpoint to send the data to.
29 *
30 * @var string
31 */
32 protected $endpoint = '';
33
34 /**
35 * The current time.
36 *
37 * @var int
38 */
39 private $current_time;
40
41 /**
42 * WPSEO_Tracking constructor.
43 *
44 * @param string $endpoint The endpoint to send the data to.
45 * @param int $threshold The limit for the option.
46 */
47 public function __construct( $endpoint, $threshold ) {
48 if ( ! $this->tracking_enabled() ) {
49 return;
50 }
51
52 $this->endpoint = $endpoint;
53 $this->threshold = $threshold;
54 $this->current_time = time();
55 }
56
57 /**
58 * Registers all hooks to WordPress.
59 */
60 public function register_hooks() {
61 if ( ! $this->tracking_enabled() ) {
62 return;
63 }
64
65 // Send tracking data on `admin_init`.
66 add_action( 'admin_init', [ $this, 'send' ], 1 );
67
68 // Add an action hook that will be triggered at the specified time by `wp_schedule_single_event()`.
69 add_action( 'wpseo_send_tracking_data_after_core_update', [ $this, 'send' ] );
70 // Call `wp_schedule_single_event()` after a WordPress core update.
71 add_action( 'upgrader_process_complete', [ $this, 'schedule_tracking_data_sending' ], 10, 2 );
72 }
73
74 /**
75 * Schedules a new sending of the tracking data after a WordPress core update.
76 *
77 * @param bool|WP_Upgrader $upgrader Optional. WP_Upgrader instance or false.
78 * Depending on context, it might be a Theme_Upgrader,
79 * Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader.
80 * instance. Default false.
81 * @param array $data Array of update data.
82 *
83 * @return void
84 */
85 public function schedule_tracking_data_sending( $upgrader = false, $data = [] ) {
86 // Return if it's not a WordPress core update.
87 if ( ! $upgrader || ! isset( $data['type'] ) || $data['type'] !== 'core' ) {
88 return;
89 }
90
91 /*
92 * To uniquely identify the scheduled cron event, `wp_next_scheduled()`
93 * needs to receive the same arguments as those used when originally
94 * scheduling the event otherwise it will always return false.
95 */
96 if ( ! wp_next_scheduled( 'wpseo_send_tracking_data_after_core_update', true ) ) {
97 /*
98 * Schedule sending of data tracking 6 hours after a WordPress core
99 * update. Pass a `true` parameter for the callback `$force` argument.
100 */
101 wp_schedule_single_event( ( time() + ( HOUR_IN_SECONDS * 6 ) ), 'wpseo_send_tracking_data_after_core_update', true );
102 }
103 }
104
105 /**
106 * Sends the tracking data.
107 *
108 * @param bool $force Whether to send the tracking data ignoring the two
109 * weeks time treshhold. Default false.
110 */
111 public function send( $force = false ) {
112 if ( ! $this->should_send_tracking( $force ) ) {
113 return;
114 }
115
116 // Set a 'content-type' header of 'application/json'.
117 $tracking_request_args = [
118 'headers' => [
119 'content-type:' => 'application/json',
120 ],
121 ];
122
123 $collector = $this->get_collector();
124
125 $request = new WPSEO_Remote_Request( $this->endpoint, $tracking_request_args );
126 $request->set_body( $collector->get_as_json() );
127 $request->send();
128
129 update_option( $this->option_name, $this->current_time, 'yes' );
130 }
131
132 /**
133 * Determines whether to send the tracking data.
134 *
135 * Returns false if tracking is disabled or the current page is one of the
136 * admin plugins pages. Returns true when there's no tracking data stored or
137 * the data was sent more than two weeks ago. The two weeks interval is set
138 * when instantiating the class.
139 *
140 * @param bool $ignore_time_treshhold Whether to send the tracking data ignoring
141 * the two weeks time treshhold. Default false.
142 *
143 * @return bool True when tracking data should be sent.
144 */
145 protected function should_send_tracking( $ignore_time_treshhold = false ) {
146 global $pagenow;
147
148 // Only send tracking on the main site of a multi-site instance. This returns true on non-multisite installs.
149 if ( is_network_admin() || ! is_main_site() ) {
150 return false;
151 }
152
153 // Because we don't want to possibly block plugin actions with our routines.
154 if ( in_array( $pagenow, [ 'plugins.php', 'plugin-install.php', 'plugin-editor.php' ], true ) ) {
155 return false;
156 }
157
158 $last_time = get_option( $this->option_name );
159
160 // When tracking data haven't been sent yet or when sending data is forced.
161 if ( ! $last_time || $ignore_time_treshhold ) {
162 return true;
163 }
164
165 return $this->exceeds_treshhold( $this->current_time - $last_time );
166 }
167
168 /**
169 * Checks if the given amount of seconds exceeds the set threshold.
170 *
171 * @param int $seconds The amount of seconds to check.
172 *
173 * @return bool True when seconds is bigger than threshold.
174 */
175 protected function exceeds_treshhold( $seconds ) {
176 return ( $seconds > $this->threshold );
177 }
178
179 /**
180 * Returns the collector for collecting the data.
181 *
182 * @return WPSEO_Collector The instance of the collector.
183 */
184 public function get_collector() {
185 $collector = new WPSEO_Collector();
186 $collector->add_collection( new WPSEO_Tracking_Default_Data() );
187 $collector->add_collection( new WPSEO_Tracking_Server_Data() );
188 $collector->add_collection( new WPSEO_Tracking_Theme_Data() );
189 $collector->add_collection( new WPSEO_Tracking_Plugin_Data() );
190 $collector->add_collection( new WPSEO_Tracking_Settings_Data() );
191
192 return $collector;
193 }
194
195 /**
196 * See if we should run tracking at all.
197 *
198 * @return bool True when we can track, false when we can't.
199 */
200 private function tracking_enabled() {
201 // Check if we're allowing tracking.
202 $tracking = WPSEO_Options::get( 'tracking' );
203
204 if ( $tracking === false ) {
205 return false;
206 }
207
208 // Save this state.
209 if ( $tracking === null ) {
210 /**
211 * Filter: 'wpseo_enable_tracking' - Enables the data tracking of Yoast SEO Premium and add-ons.
212 *
213 * @api string $is_enabled The enabled state. Default is false.
214 */
215 $tracking = apply_filters( 'wpseo_enable_tracking', false );
216
217 WPSEO_Options::set( 'tracking', $tracking );
218 }
219
220 if ( $tracking === false ) {
221 return false;
222 }
223
224 if ( wp_get_environment_type() !== 'production' ) {
225 return false;
226 }
227
228 return true;
229 }
230 }
231