PluginProbe ʕ •ᴥ•ʔ
Jetpack – WP Security, Backup, Speed, & Growth / 16.1-beta.3
Jetpack – WP Security, Backup, Speed, & Growth v16.1-beta.3
16.1.2 16.2-a.1 16.1.1 16.1 16.1-beta 16.1-beta.2 16.1-beta.3 16.1-a.5 16.1-a.3 16.0.1 16.1-a.1 16.0 16.0-beta 16.0-a.7 16.0-a.5 15.9.1 16.0-a.3 16.0-a.1 15.9 15.9-beta 15.9-a.7 15.9-a.5 15.9-a.3 15.9-a.1 15.8 15.8-beta 15.8-a.7 15.8-a.5 5.2.5 5.3.4 5.4.4 5.5.5 5.6.5 5.7.5 5.8.4 5.9.4 6.0.4 6.1 6.1.1 6.1.2 6.1.3 6.1.4 6.1.5 6.2 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.3 6.3.1 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.4 6.4.1 6.4.2 6.4.3 6.4.4 6.4.5 6.4.6 6.5 6.5.1 6.5.2 6.5.3 6.5.4 6.6 6.6.1 6.6.2 6.6.3 6.6.4 6.6.5 6.7 6.7.1 6.7.2 6.7.3 6.7.4 6.8 6.8.1 6.8.2 6.8.3 6.8.4 6.8.5 6.9 6.9.1 6.9.2 6.9.3 6.9.4 7.0 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1 7.1.1 7.1.2 7.1.3 7.1.4 7.1.5 7.2 7.2.1 7.2.1.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3 7.3.0.1 7.3.1 7.3.1.1 7.3.2 7.3.3 7.3.4 7.3.5 7.4 7.4.1 7.4.2 7.4.3 7.4.4 7.4.5 7.5 7.5.0.1 7.5.1 7.5.2 7.5.3 7.5.4 7.5.5 7.5.6 7.5.7 7.6 7.6.1 7.6.2 7.6.3 7.6.4 7.7 7.7.1 7.7.2 7.7.3 7.7.4 7.7.5 7.7.6 7.8 7.8.1 7.8.2 7.8.3 7.8.4 7.9 7.9.1 7.9.2 7.9.3 7.9.4 8.0 8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11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / jetpack_vendor / automattic / jetpack-stats / src / class-main.php
jetpack / jetpack_vendor / automattic / jetpack-stats / src Last commit date
abilities 2 months ago class-main.php 1 month ago class-options.php 4 weeks ago class-package-version.php 4 weeks ago class-rest-provider.php 2 years ago class-tracking-pixel.php 4 weeks ago class-transient-cleanup.php 6 months ago class-wpcom-stats.php 8 months ago class-xmlrpc-provider.php 2 years ago
class-main.php
267 lines
1 <?php
2 /**
3 * Stats Main
4 *
5 * @package automattic/jetpack-stats
6 */
7
8 namespace Automattic\Jetpack\Stats;
9
10 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11 use Automattic\Jetpack\Constants;
12 use Automattic\Jetpack\Modules;
13 use Automattic\Jetpack\Stats\Abilities\Stats_Abilities;
14 use Automattic\Jetpack\Status;
15 use Automattic\Jetpack\Status\Visitor;
16 use WP_User;
17
18 /**
19 * Stats Main class.
20 *
21 * Entrypoint for Stats.
22 *
23 * @since 0.1.0
24 */
25 class Main {
26 /**
27 * Stats version.
28 * Mostly needed for backwards compatibility.
29 */
30 const STATS_VERSION = '9';
31
32 /**
33 * Singleton Main instance.
34 *
35 * @var Main
36 **/
37 private static $instance = null;
38
39 /**
40 * Initializer.
41 * Used to configure the stats package, eg when called via the Config package.
42 *
43 * @return object
44 */
45 public static function init() {
46 if ( null === self::$instance ) {
47 self::$instance = new Main();
48 }
49
50 return self::$instance;
51 }
52
53 /**
54 * Class constructor.
55 *
56 * @return void
57 */
58 private function __construct() {
59 /**
60 * This avoids conflicts when running Stats package with older versions of the Jetpack plugin.
61 *
62 * On JP version 11.5-a.2 the hooks below were removed from the Jetpack plugin and it is safe
63 * to register them in the Stats package.
64 */
65 $jp_plugin_version = Constants::get_constant( 'JETPACK__VERSION' );
66 if ( $jp_plugin_version && version_compare( $jp_plugin_version, '11.5-a.2', '<' ) ) {
67 return;
68 }
69 // Generate the tracking code after wp() has queried for posts.
70 add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ), 1 );
71
72 add_action( 'wp_head', array( __CLASS__, 'hide_smile_css' ) );
73 add_action( 'embed_head', array( __CLASS__, 'hide_smile_css' ) );
74
75 // Map stats caps.
76 add_filter( 'map_meta_cap', array( __CLASS__, 'map_meta_caps' ), 10, 3 );
77
78 XMLRPC_Provider::init();
79
80 /*
81 * REST_Provider only registers its routes on REST init, so defer
82 * constructing it (and autoloading the class) until a REST request is
83 * served. A closure is used because rest_api_init passes the REST server
84 * to callbacks, which would otherwise be read as REST_Provider::init()'s
85 * $new_instance argument.
86 */
87 add_action(
88 'rest_api_init',
89 static function () {
90 REST_Provider::init();
91 },
92 0
93 );
94 Transient_Cleanup::init();
95
96 // Clean up transient cron on module deactivation.
97 add_action( 'jetpack_deactivate_module_stats', array( Transient_Cleanup::class, 'unschedule_cleanup' ) );
98
99 // Set up package version hook.
100 add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
101
102 // Register WP Abilities API surface. Gated behind the
103 // `jetpack_wp_abilities_enabled` filter inside Registrar::init(),
104 // which defaults to false — so this call is safe to make unconditionally
105 // and still opt-in per-site until the flag is flipped.
106 Stats_Abilities::init();
107 }
108
109 /**
110 * Checks if filter is set and dnt is enabled.
111 *
112 * @return bool
113 */
114 public static function jetpack_is_dnt_enabled() {
115 /**
116 * Filter the option which decides honor DNT or not.
117 *
118 * @module stats
119 * @since-jetpack 6.1.0
120 *
121 * @param bool false Honors DNT for clients who don't want to be tracked. Defaults to false. Set to true to enable.
122 */
123 if ( false === apply_filters( 'jetpack_honor_dnt_header_for_stats', false ) ) {
124 return false;
125 }
126
127 foreach ( $_SERVER as $name => $value ) {
128 if ( 'http_dnt' === strtolower( $name ) && 1 === (int) $value ) {
129 return true;
130 }
131 }
132
133 return false;
134 }
135
136 /**
137 * Maps view_stats cap to read cap as needed.
138 *
139 * @access public
140 * @param mixed $caps Caps.
141 * @param mixed $cap Cap.
142 * @param mixed $user_id User ID.
143 * @return array Possibly mapped capabilities for meta capability.
144 */
145 public static function map_meta_caps( $caps, $cap, $user_id ) {
146 // Map view_stats to exists.
147 if ( 'view_stats' === $cap ) {
148 $user = new WP_User( $user_id );
149 // WordPress 6.9 introduced lazy-loading of some WP_User properties, including `roles`.
150 // It also made said properties protected, so we can't modify keys directly.
151 $user_roles = $user->roles;
152 $user_role = array_shift( $user_roles ); // Work with the copy
153 $stats_roles = Options::get_option( 'roles' );
154
155 // Is the users role in the available stats roles?
156 if ( is_array( $stats_roles ) && in_array( $user_role, $stats_roles, true ) ) {
157 $caps = array( 'read' );
158 }
159 }
160
161 return $caps;
162 }
163
164 /**
165 * Stats Template Redirect.
166 *
167 * @access public
168 * @return void
169 */
170 public static function template_redirect() {
171 if ( ! self::should_track() ) {
172 return;
173 }
174
175 add_action( 'wp_enqueue_scripts', array( Tracking_Pixel::class, 'enqueue_stats_script' ), 101 );
176 add_action( 'wp_footer', array( Tracking_Pixel::class, 'add_amp_pixel' ), 101 );
177 add_action( 'web_stories_print_analytics', array( Tracking_Pixel::class, 'add_amp_pixel' ), 101 );
178 }
179
180 /**
181 * CSS to hide the tracking pixel smiley.
182 * It is now hidden for everyone (used to be visible if you had set the hide_smile option).
183 *
184 * @access public
185 * @return void
186 */
187 public static function hide_smile_css() {
188 if ( ! self::should_track() ) {
189 return;
190 }
191 ?>
192 <style>img#wpstats{display:none}</style>
193 <?php
194 }
195
196 /**
197 * Whether we should add the tracking pixel.
198 *
199 * @return bool
200 */
201 public static function should_track() {
202 global $current_user;
203
204 // Not connected sites should not generate tracking stats.
205 if ( ! ( new Connection_Manager() )->is_connected() ) {
206 return false;
207 }
208
209 // If the stats module is disabled we should not generate tracking stats.
210 if ( ! ( new Modules() )->is_active( 'stats' ) ) {
211 return false;
212 }
213
214 // Do not generate tracking stats for feeds, robots, embeds, previews
215 // or to honour the DNT headers.
216 if (
217 is_feed()
218 || is_robots()
219 || is_embed()
220 || is_trackback()
221 || is_preview()
222 || self::jetpack_is_dnt_enabled()
223 ) {
224 return false;
225 }
226
227 // Sites in Safe Mode should not generate tracking stats.
228 $status = new Status();
229 if ( $status->in_safe_mode() ) {
230 return false;
231 }
232
233 // Should we be counting this user's views?
234 if ( ! empty( $current_user->ID ) ) {
235 $count_roles = Options::get_option( 'count_roles' );
236 if ( ! is_array( $count_roles ) || ! array_intersect( $current_user->roles, $count_roles ) ) {
237 return false;
238 }
239 }
240
241 /**
242 * Allow excluding specific IP addresses from being tracked in Stats.
243 * Note: for this to work well, visitors' IP addresses must:
244 * - be stored and returned properly in IP address headers;
245 * - not be impacted by any caching setup on your site.
246 *
247 * @module stats
248 *
249 * @since-jetpack 10.6
250 *
251 * @param array $excluded_ips An array of IP address strings to exclude from tracking.
252 */
253 $excluded_ips = (array) apply_filters( 'jetpack_stats_excluded_ips', array() );
254
255 // Should we be counting views for this IP address?
256 $current_user_ip = ( new Visitor() )->get_ip( true );
257 if (
258 ! empty( $excluded_ips )
259 && in_array( $current_user_ip, $excluded_ips, true )
260 ) {
261 return false;
262 }
263
264 return true;
265 }
266 }
267