• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 6987097851

25 Nov 2023 04:49AM UTC coverage: 49.206% (-0.1%) from 49.302%
6987097851

push

github

web-flow
Merge pull request #20878 from Yoast/JRF/ghactions-minor-tweak

GH Actions: update a few links in inline comments

15305 of 31104 relevant lines covered (49.21%)

4.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.03
/admin/tracking/class-tracking.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin\Tracking
6
 */
7

8
use Yoast\WP\SEO\Analytics\Application\Missing_Indexables_Collector;
9
use Yoast\WP\SEO\Analytics\Application\To_Be_Cleaned_Indexables_Collector;
10

11
/**
12
 * This class handles the tracking routine.
13
 */
14
class WPSEO_Tracking implements WPSEO_WordPress_Integration {
15

16
        /**
17
         * The tracking option name.
18
         *
19
         * @var string
20
         */
21
        protected $option_name = 'wpseo_tracking_last_request';
22

23
        /**
24
         * The limit for the option.
25
         *
26
         * @var int
27
         */
28
        protected $threshold = 0;
29

30
        /**
31
         * The endpoint to send the data to.
32
         *
33
         * @var string
34
         */
35
        protected $endpoint = '';
36

37
        /**
38
         * The current time.
39
         *
40
         * @var int
41
         */
42
        private $current_time;
43

44
        /**
45
         * WPSEO_Tracking constructor.
46
         *
47
         * @param string $endpoint  The endpoint to send the data to.
48
         * @param int    $threshold The limit for the option.
49
         */
50
        public function __construct( $endpoint, $threshold ) {
4✔
51
                if ( ! $this->tracking_enabled() ) {
4✔
52
                        return;
2✔
53
                }
54

55
                $this->endpoint     = $endpoint;
2✔
56
                $this->threshold    = $threshold;
2✔
57
                $this->current_time = time();
2✔
58
        }
1✔
59

60
        /**
61
         * Registers all hooks to WordPress.
62
         */
63
        public function register_hooks() {
×
64
                if ( ! $this->tracking_enabled() ) {
×
65
                        return;
×
66
                }
67

68
                // Send tracking data on `admin_init`.
69
                add_action( 'admin_init', [ $this, 'send' ], 1 );
×
70

71
                // Add an action hook that will be triggered at the specified time by `wp_schedule_single_event()`.
72
                add_action( 'wpseo_send_tracking_data_after_core_update', [ $this, 'send' ] );
×
73
                // Call `wp_schedule_single_event()` after a WordPress core update.
74
                add_action( 'upgrader_process_complete', [ $this, 'schedule_tracking_data_sending' ], 10, 2 );
×
75
        }
76

77
        /**
78
         * Schedules a new sending of the tracking data after a WordPress core update.
79
         *
80
         * @param bool|WP_Upgrader $upgrader Optional. WP_Upgrader instance or false.
81
         *                                   Depending on context, it might be a Theme_Upgrader,
82
         *                                   Plugin_Upgrader, Core_Upgrade, or Language_Pack_Upgrader.
83
         *                                   instance. Default false.
84
         * @param array            $data     Array of update data.
85
         *
86
         * @return void
87
         */
88
        public function schedule_tracking_data_sending( $upgrader = false, $data = [] ) {
×
89
                // Return if it's not a WordPress core update.
90
                if ( ! $upgrader || ! isset( $data['type'] ) || $data['type'] !== 'core' ) {
×
91
                        return;
×
92
                }
93

94
                /*
95
                 * To uniquely identify the scheduled cron event, `wp_next_scheduled()`
96
                 * needs to receive the same arguments as those used when originally
97
                 * scheduling the event otherwise it will always return false.
98
                 */
99
                if ( ! wp_next_scheduled( 'wpseo_send_tracking_data_after_core_update', [ true ] ) ) {
×
100
                        /*
101
                         * Schedule sending of data tracking 6 hours after a WordPress core
102
                         * update. Pass a `true` parameter for the callback `$force` argument.
103
                         */
104
                        wp_schedule_single_event( ( time() + ( HOUR_IN_SECONDS * 6 ) ), 'wpseo_send_tracking_data_after_core_update', [ true ] );
×
105
                }
106
        }
107

108
        /**
109
         * Sends the tracking data.
110
         *
111
         * @param bool $force Whether to send the tracking data ignoring the two
112
         *                    weeks time threshold. Default false.
113
         */
114
        public function send( $force = false ) {
×
115
                if ( ! $this->should_send_tracking( $force ) ) {
×
116
                        return;
×
117
                }
118

119
                // Set a 'content-type' header of 'application/json'.
120
                $tracking_request_args = [
121
                        'headers' => [
×
122
                                'content-type:' => 'application/json',
123
                        ],
124
                ];
125

126
                $collector = $this->get_collector();
×
127

128
                $request = new WPSEO_Remote_Request( $this->endpoint, $tracking_request_args );
×
129
                $request->set_body( $collector->get_as_json() );
×
130
                $request->send();
×
131

132
                update_option( $this->option_name, $this->current_time, 'yes' );
×
133
        }
134

135
        /**
136
         * Determines whether to send the tracking data.
137
         *
138
         * Returns false if tracking is disabled or the current page is one of the
139
         * admin plugins pages. Returns true when there's no tracking data stored or
140
         * the data was sent more than two weeks ago. The two weeks interval is set
141
         * when instantiating the class.
142
         *
143
         * @param bool $ignore_time_treshhold Whether to send the tracking data ignoring
144
         *                                    the two weeks time treshhold. Default false.
145
         *
146
         * @return bool True when tracking data should be sent.
147
         */
148
        protected function should_send_tracking( $ignore_time_treshhold = false ) {
×
149
                global $pagenow;
×
150

151
                // Only send tracking on the main site of a multi-site instance. This returns true on non-multisite installs.
152
                if ( is_network_admin() || ! is_main_site() ) {
×
153
                        return false;
×
154
                }
155

156
                // Because we don't want to possibly block plugin actions with our routines.
157
                if ( in_array( $pagenow, [ 'plugins.php', 'plugin-install.php', 'plugin-editor.php' ], true ) ) {
×
158
                        return false;
×
159
                }
160

161
                $last_time = get_option( $this->option_name );
×
162

163
                // When tracking data haven't been sent yet or when sending data is forced.
164
                if ( ! $last_time || $ignore_time_treshhold ) {
×
165
                        return true;
×
166
                }
167

168
                return $this->exceeds_treshhold( $this->current_time - $last_time );
×
169
        }
170

171
        /**
172
         * Checks if the given amount of seconds exceeds the set threshold.
173
         *
174
         * @param int $seconds The amount of seconds to check.
175
         *
176
         * @return bool True when seconds is bigger than threshold.
177
         */
178
        protected function exceeds_treshhold( $seconds ) {
×
179
                return ( $seconds > $this->threshold );
×
180
        }
181

182
        /**
183
         * Returns the collector for collecting the data.
184
         *
185
         * @return WPSEO_Collector The instance of the collector.
186
         */
187
        public function get_collector() {
2✔
188
                $collector = new WPSEO_Collector();
2✔
189
                $collector->add_collection( new WPSEO_Tracking_Default_Data() );
2✔
190
                $collector->add_collection( new WPSEO_Tracking_Server_Data() );
2✔
191
                $collector->add_collection( new WPSEO_Tracking_Theme_Data() );
2✔
192
                $collector->add_collection( new WPSEO_Tracking_Plugin_Data() );
2✔
193
                $collector->add_collection( new WPSEO_Tracking_Settings_Data() );
2✔
194
                $collector->add_collection( new WPSEO_Tracking_Addon_Data() );
2✔
195
                $collector->add_collection( YoastSEO()->classes->get( Missing_Indexables_Collector::class ) );
2✔
196
                $collector->add_collection( YoastSEO()->classes->get( To_Be_Cleaned_Indexables_Collector::class ) );
2✔
197

198
                return $collector;
2✔
199
        }
200

201
        /**
202
         * See if we should run tracking at all.
203
         *
204
         * @return bool True when we can track, false when we can't.
205
         */
206
        private function tracking_enabled() {
×
207
                // Check if we're allowing tracking.
208
                $tracking = WPSEO_Options::get( 'tracking' );
×
209

210
                if ( $tracking === false ) {
×
211
                        return false;
×
212
                }
213

214
                // Save this state.
215
                if ( $tracking === null ) {
×
216
                        /**
217
                         * Filter: 'wpseo_enable_tracking' - Enables the data tracking of Yoast SEO Premium and add-ons.
218
                         *
219
                         * @api string $is_enabled The enabled state. Default is false.
220
                         */
221
                        $tracking = apply_filters( 'wpseo_enable_tracking', false );
×
222

223
                        WPSEO_Options::set( 'tracking', $tracking );
×
224
                }
225

226
                if ( $tracking === false ) {
×
227
                        return false;
×
228
                }
229

230
                if ( ! YoastSEO()->helpers->environment->is_production_mode() ) {
×
231
                        return false;
×
232
                }
233

234
                return true;
×
235
        }
236
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc