PluginProbe
Elementor Website Builder – more than just a page builder / 3.2.3
Elementor Website Builder – more than just a page builder v3.2.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / tracker.php

tracker.php in Elementor Website Builder – more than just a page builder 3.2.3, at includes/tracker.php

379 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor tracker.
10 *
11 * Elementor tracker handler class is responsible for sending non-sensitive plugin
12 * data to Elementor servers for users that actively allowed data tracking.
13 *
14 * @since 1.0.0
15 */
16 class Tracker {
17
18 /**
19 * API URL.
20 *
21 * Holds the URL of the Tracker API.
22 *
23 * @since 1.0.0
24 * @access private
25 *
26 * @var string API URL.
27 */
28 private static $_api_url = 'https://my.elementor.com/api/v1/tracker/';
29
30 private static $notice_shown = false;
31
32 /**
33 * Init.
34 *
35 * Initialize Elementor tracker.
36 *
37 * @since 1.0.0
38 * @access public
39 * @static
40 */
41 public static function init() {
42 add_action( 'elementor/tracker/send_event', [ __CLASS__, 'send_tracking_data' ] );
43 add_action( 'admin_init', [ __CLASS__, 'handle_tracker_actions' ] );
44 }
45
46 /**
47 * Check for settings opt-in.
48 *
49 * Checks whether the site admin has opted-in for data tracking, or not.
50 *
51 * @since 1.0.0
52 * @access public
53 * @static
54 *
55 * @param string $new_value Allowed tracking value.
56 *
57 * @return string Return `yes` if tracking allowed, `no` otherwise.
58 */
59 public static function check_for_settings_optin( $new_value ) {
60 $old_value = get_option( 'elementor_allow_tracking', 'no' );
61 if ( $old_value !== $new_value && 'yes' === $new_value ) {
62 self::send_tracking_data( true );
63 }
64
65 if ( empty( $new_value ) ) {
66 $new_value = 'no';
67 }
68 return $new_value;
69 }
70
71 /**
72 * Send tracking data.
73 *
74 * Decide whether to send tracking data, or not.
75 *
76 * @since 1.0.0
77 * @access public
78 * @static
79 *
80 * @param bool $override
81 */
82 public static function send_tracking_data( $override = false ) {
83 // Don't trigger this on AJAX Requests.
84 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
85 return;
86 }
87
88 if ( ! self::is_allow_track() ) {
89 return;
90 }
91
92 $last_send = self::get_last_send_time();
93
94 /**
95 * Tracker override send.
96 *
97 * Filters whether to override sending tracking data or not.
98 *
99 * @since 1.0.0
100 *
101 * @param bool $override Whether to override default setting or not.
102 */
103 $override = apply_filters( 'elementor/tracker/send_override', $override );
104
105 if ( ! $override ) {
106 $last_send_interval = strtotime( '-1 week' );
107
108 /**
109 * Tracker last send interval.
110 *
111 * Filters the interval of between two tracking requests.
112 *
113 * @since 1.0.0
114 *
115 * @param int $last_send_interval A date/time string. Default is `strtotime( '-1 week' )`.
116 */
117 $last_send_interval = apply_filters( 'elementor/tracker/last_send_interval', $last_send_interval );
118
119 // Send a maximum of once per week by default.
120 if ( $last_send && $last_send > $last_send_interval ) {
121 return;
122 }
123 } else {
124 // Make sure there is at least a 1 hour delay between override sends, we dont want duplicate calls due to double clicking links.
125 if ( $last_send && $last_send > strtotime( '-1 hours' ) ) {
126 return;
127 }
128 }
129
130 // Update time first before sending to ensure it is set.
131 update_option( 'elementor_tracker_last_send', time() );
132
133 $params = self::get_tracking_data( empty( $last_send ) );
134
135 add_filter( 'https_ssl_verify', '__return_false' );
136
137 wp_safe_remote_post(
138 self::$_api_url,
139 [
140 'timeout' => 25,
141 'blocking' => false,
142 // 'sslverify' => false,
143 'body' => [
144 'data' => wp_json_encode( $params ),
145 ],
146 ]
147 );
148 }
149
150 /**
151 * Is allow track.
152 *
153 * Checks whether the site admin has opted-in for data tracking, or not.
154 *
155 * @since 1.0.0
156 * @access public
157 * @static
158 */
159 public static function is_allow_track() {
160 return 'yes' === get_option( 'elementor_allow_tracking', 'no' );
161 }
162
163 /**
164 * Handle tracker actions.
165 *
166 * Check if the user opted-in or opted-out and update the database.
167 *
168 * Fired by `admin_init` action.
169 *
170 * @since 1.0.0
171 * @access public
172 * @static
173 */
174 public static function handle_tracker_actions() {
175 if ( ! isset( $_GET['elementor_tracker'] ) ) {
176 return;
177 }
178
179 if ( 'opt_into' === $_GET['elementor_tracker'] ) {
180 check_admin_referer( 'opt_into' );
181
182 self::set_opt_in( true );
183 }
184
185 if ( 'opt_out' === $_GET['elementor_tracker'] ) {
186 check_admin_referer( 'opt_out' );
187
188 self::set_opt_in( false );
189 }
190
191 wp_redirect( remove_query_arg( 'elementor_tracker' ) );
192 exit;
193 }
194
195 /**
196 * @since 2.2.0
197 * @access public
198 * @static
199 */
200 public static function is_notice_shown() {
201 return self::$notice_shown;
202 }
203
204 public static function set_opt_in( $value ) {
205 if ( $value ) {
206 update_option( 'elementor_allow_tracking', 'yes' );
207 self::send_tracking_data( true );
208 } else {
209 update_option( 'elementor_allow_tracking', 'no' );
210 update_option( 'elementor_tracker_notice', '1' );
211 }
212 }
213
214 /**
215 * Get system reports data.
216 *
217 * Retrieve the data from system reports.
218 *
219 * @since 2.0.0
220 * @access private
221 * @static
222 *
223 * @return array The data from system reports.
224 */
225 private static function get_system_reports_data() {
226 $reports = Plugin::$instance->system_info->load_reports( System_Info\Main::get_allowed_reports() );
227
228 $system_reports = [];
229 foreach ( $reports as $report_key => $report_details ) {
230 $system_reports[ $report_key ] = [];
231 foreach ( $report_details['report'] as $sub_report_key => $sub_report_details ) {
232 $system_reports[ $report_key ][ $sub_report_key ] = $sub_report_details['value'];
233 }
234 }
235 return $system_reports;
236 }
237
238 /**
239 * Get last send time.
240 *
241 * Retrieve the last time tracking data was sent.
242 *
243 * @since 2.0.0
244 * @access private
245 * @static
246 *
247 * @return int|false The last time tracking data was sent, or false if
248 * tracking data never sent.
249 */
250 private static function get_last_send_time() {
251 $last_send_time = get_option( 'elementor_tracker_last_send', false );
252
253 /**
254 * Tracker last send time.
255 *
256 * Filters the last time tracking data was sent.
257 *
258 * @since 1.0.0
259 *
260 * @param int|false $last_send_time The last time tracking data was sent,
261 * or false if tracking data never sent.
262 */
263 $last_send_time = apply_filters( 'elementor/tracker/last_send_time', $last_send_time );
264
265 return $last_send_time;
266 }
267
268 /**
269 * Get posts usage.
270 *
271 * Retrieve the number of posts using Elementor.
272 *
273 * @since 2.0.0
274 * @access public
275 * @static
276 *
277 * @return array The number of posts using Elementor grouped by post types
278 * and post status.
279 */
280 public static function get_posts_usage() {
281 global $wpdb;
282
283 $usage = [];
284
285 $results = $wpdb->get_results(
286 "SELECT `post_type`, `post_status`, COUNT(`ID`) `hits`
287 FROM {$wpdb->posts} `p`
288 LEFT JOIN {$wpdb->postmeta} `pm` ON(`p`.`ID` = `pm`.`post_id`)
289 WHERE `post_type` != 'elementor_library'
290 AND `meta_key` = '_elementor_edit_mode' AND `meta_value` = 'builder'
291 GROUP BY `post_type`, `post_status`;"
292 );
293
294 if ( $results ) {
295 foreach ( $results as $result ) {
296 $usage[ $result->post_type ][ $result->post_status ] = $result->hits;
297 }
298 }
299
300 return $usage;
301
302 }
303
304 /**
305 * Get library usage.
306 *
307 * Retrieve the number of Elementor library items saved.
308 *
309 * @since 2.0.0
310 * @access public
311 * @static
312 *
313 * @return array The number of Elementor library items grouped by post types
314 * and meta value.
315 */
316 public static function get_library_usage() {
317 global $wpdb;
318
319 $usage = [];
320
321 $results = $wpdb->get_results(
322 "SELECT `meta_value`, COUNT(`ID`) `hits`
323 FROM {$wpdb->posts} `p`
324 LEFT JOIN {$wpdb->postmeta} `pm` ON(`p`.`ID` = `pm`.`post_id`)
325 WHERE `post_type` = 'elementor_library'
326 AND `meta_key` = '_elementor_template_type'
327 GROUP BY `post_type`, `meta_value`;"
328 );
329
330 if ( $results ) {
331 foreach ( $results as $result ) {
332 $usage[ $result->meta_value ] = $result->hits;
333 }
334 }
335
336 return $usage;
337
338 }
339
340 /**
341 * Get the tracking data
342 *
343 * Retrieve tracking data and apply filter
344 *
345 * @access public
346 * @static
347 *
348 * @param bool $is_first_time
349 *
350 * @return array
351 */
352 public static function get_tracking_data( $is_first_time = false ) {
353 $params = [
354 'system' => self::get_system_reports_data(),
355 'site_lang' => get_bloginfo( 'language' ),
356 'email' => get_option( 'admin_email' ),
357 'usages' => [
358 'posts' => self::get_posts_usage(),
359 'library' => self::get_library_usage(),
360 ],
361 'is_first_time' => $is_first_time,
362 ];
363
364 /**
365 * Tracker send tracking data params.
366 *
367 * Filters the data parameters when sending tracking request.
368 *
369 * @param array $params Variable to encode as JSON.
370 *
371 * @since 1.0.0
372 *
373 */
374 $params = apply_filters( 'elementor/tracker/send_tracking_data_params', $params );
375
376 return $params;
377 }
378 }
379