PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 15.1
Jetpack – WP Security, Backup, Speed, & Growth v15.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.jetpack.php

class.jetpack.php in Jetpack – WP Security, Backup, Speed, & Growth 15.1, at class.jetpack.php

6,166 lines 203.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * The mega-class.
4 *
5 * This contains too much, so please think twice before adding more.
6 *
7 * @package automattic/jetpack
8 */
9
10 use Automattic\Jetpack\Assets;
11 use Automattic\Jetpack\Boost_Speed_Score\Speed_Score;
12 use Automattic\Jetpack\Config;
13 use Automattic\Jetpack\Connection\Authorize_Json_Api;
14 use Automattic\Jetpack\Connection\Client;
15 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
16 use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
17 use Automattic\Jetpack\Connection\Secrets;
18 use Automattic\Jetpack\Connection\Tokens;
19 use Automattic\Jetpack\Connection\Webhooks\Authorize_Redirect;
20 use Automattic\Jetpack\Constants;
21 use Automattic\Jetpack\CookieState;
22 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
23 use Automattic\Jetpack\Device_Detection\User_Agent_Info;
24 use Automattic\Jetpack\Errors;
25 use Automattic\Jetpack\Files;
26 use Automattic\Jetpack\Identity_Crisis;
27 use Automattic\Jetpack\Licensing;
28 use Automattic\Jetpack\Modules;
29 use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
30 use Automattic\Jetpack\Paths;
31 use Automattic\Jetpack\Plugin\Deprecate;
32 use Automattic\Jetpack\Plugin\Tracking as Plugin_Tracking;
33 use Automattic\Jetpack\Redirect;
34 use Automattic\Jetpack\Status;
35 use Automattic\Jetpack\Status\Host;
36 use Automattic\Jetpack\Status\Visitor;
37 use Automattic\Jetpack\Sync\Actions as Sync_Actions;
38 use Automattic\Jetpack\Sync\Health;
39 use Automattic\Jetpack\Sync\Sender;
40 use Automattic\Jetpack\Terms_Of_Service;
41 use Automattic\Jetpack\Tracking;
42 use Automattic\Woocommerce_Analytics;
43
44 if ( ! defined( 'ABSPATH' ) ) {
45 exit( 0 );
46 }
47
48 /*
49 Options:
50 jetpack_options (array)
51 An array of options.
52 @see Jetpack_Options::get_option_names()
53
54 jetpack_register (string)
55 Temporary verification secrets.
56
57 jetpack_activated (int)
58 1: the plugin was activated normally
59 2: the plugin was activated on this site because of a network-wide activation
60 3: the plugin was auto-installed
61 4: the plugin was manually disconnected (but is still installed)
62
63 jetpack_active_modules (array)
64 Array of active module slugs.
65
66 jetpack_do_activate (bool)
67 Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
68 */
69
70 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php';
71
72 /**
73 * The Jetpack class.
74 */
75 class Jetpack {
76 /**
77 * XMLRPC server instance.
78 *
79 * @var null|Jetpack_XMLRPC_Server XMLRPC server used by Jetpack.
80 */
81 public $xmlrpc_server = null;
82
83 /**
84 * Contains all assets that have had their URL rewritten to minified versions.
85 *
86 * @var array
87 */
88 public static $min_assets = array();
89
90 /**
91 * Plugins to deactivate.
92 *
93 * @var array Plugins to deactivate by module.
94 */
95 public $plugins_to_deactivate = array(
96 'contact-form' => array(
97 array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
98 array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
99 ),
100 'gravatar-hovercards' => array(
101 array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
102 ),
103 'latex' => array(
104 array( 'wp-latex/wp-latex.php', 'WP LaTeX' ),
105 ),
106 'sharedaddy' => array(
107 array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
108 array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),
109 ),
110 'shortlinks' => array(
111 array( 'stats/stats.php', 'WordPress.com Stats' ),
112 ),
113 'stats' => array(
114 array( 'stats/stats.php', 'WordPress.com Stats' ),
115 ),
116 'twitter-widget' => array(
117 array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
118 ),
119 'videopress' => array(
120 array( 'video/video.php', 'VideoPress' ),
121 ),
122 'widget-visibility' => array(
123 array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),
124 array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),
125 ),
126 );
127
128 /**
129 * Map of roles we care about, and their corresponding minimum capabilities.
130 *
131 * @deprecated 7.6 Use Automattic\Jetpack\Roles::$capability_translations instead.
132 *
133 * @access public
134 * @static
135 *
136 * @var array
137 */
138 public static $capability_translations = array(
139 'administrator' => 'manage_options',
140 'editor' => 'edit_others_posts',
141 'author' => 'publish_posts',
142 'contributor' => 'edit_posts',
143 'subscriber' => 'read',
144 );
145
146 /**
147 * Map of modules that have conflicts with plugins and should not be auto-activated
148 * if the plugins are active. Used by filter_default_modules
149 *
150 * Plugin Authors: If you'd like to prevent a single module from auto-activating,
151 * change `module-slug` and add this to your plugin:
152 *
153 * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' );
154 * function my_jetpack_get_default_modules( $modules ) {
155 * return array_diff( $modules, array( 'module-slug' ) );
156 * }
157 *
158 * @var array
159 */
160 private $conflicting_plugins = array(
161 'comments' => array(
162 'Intense Debate' => 'intensedebate/intensedebate.php',
163 'Disqus' => 'disqus-comment-system/disqus.php',
164 'Livefyre' => 'livefyre-comments/livefyre.php',
165 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php',
166 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php',
167 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php',
168 ),
169 'comment-likes' => array(
170 'Epoch' => 'epoch/plugincore.php',
171 ),
172 'latex' => array(
173 'LaTeX for WordPress' => 'latex/latex.php',
174 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php',
175 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php',
176 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php',
177 'Enable Latex' => 'enable-latex/enable-latex.php',
178 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php',
179 ),
180 'protect' => array(
181 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php',
182 'Captcha' => 'captcha/captcha.php',
183 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php',
184 'Login Security Solution' => 'login-security-solution/login-security-solution.php',
185 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php',
186 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php',
187 'SiteGuard WP Plugin' => 'siteguard/siteguard.php',
188 'Security-protection' => 'security-protection/security-protection.php',
189 'Login Security' => 'login-security/login-security.php',
190 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php',
191 'Wordfence Security' => 'wordfence/wordfence.php',
192 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php',
193 'iThemes Security' => 'better-wp-security/better-wp-security.php',
194 ),
195 'related-posts' => array(
196 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php',
197 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php',
198 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php',
199 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php',
200 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php',
201 'outbrain' => 'outbrain/outbrain.php',
202 'Shareaholic' => 'shareaholic/shareaholic.php',
203 'Sexybookmarks' => 'sexybookmarks/shareaholic.php',
204 ),
205 'sharedaddy' => array(
206 'AddThis' => 'addthis/addthis_social_widget.php',
207 'Add To Any' => 'add-to-any/add-to-any.php',
208 'ShareThis' => 'share-this/sharethis.php',
209 'Shareaholic' => 'shareaholic/shareaholic.php',
210 ),
211 'seo-tools' => array(
212 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
213 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
214 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
215 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
216 'The SEO Framework' => 'autodescription/autodescription.php',
217 'Rank Math' => 'seo-by-rank-math/rank-math.php',
218 'Slim SEO' => 'slim-seo/slim-seo.php',
219 'SEOKEY' => 'seo-key/seo-key.php',
220 'SEOKEY Pro' => 'seo-key-pro/seo-key.php',
221 'SEOPress' => 'wp-seopress/seopress.php',
222 'SEOPress Pro' => 'wp-seopress-pro/seopress-pro.php',
223 ),
224 'verification-tools' => array(
225 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
226 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
227 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
228 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
229 'The SEO Framework' => 'autodescription/autodescription.php',
230 'Rank Math' => 'seo-by-rank-math/rank-math.php',
231 'Slim SEO' => 'slim-seo/slim-seo.php',
232 ),
233 'widget-visibility' => array(
234 'Widget Logic' => 'widget-logic/widget_logic.php',
235 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php',
236 ),
237 'sitemaps' => array(
238 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php',
239 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php',
240 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php',
241 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php',
242 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php',
243 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
244 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
245 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
246 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
247 'The SEO Framework' => 'autodescription/autodescription.php',
248 'Sitemap' => 'sitemap/sitemap.php',
249 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php',
250 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php',
251 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php',
252 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php',
253 'Rank Math' => 'seo-by-rank-math/rank-math.php',
254 'Slim SEO' => 'slim-seo/slim-seo.php',
255 ),
256 );
257
258 /**
259 * Plugins for which we turn off our Facebook OG Tags implementation.
260 *
261 * Note: the following plugins automatically deactivate Jetpack's Open
262 * Graph tags via filter when their Social Meta modules are active:
263 *
264 * - All in One SEO Pack, All in one SEO Pack Pro
265 * - WordPress SEO by Yoast, WordPress SEO Premium by Yoast
266 *
267 * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter:
268 * add_filter( 'jetpack_enable_open_graph', '__return_false' );
269 *
270 * @var array Array of plugin slugs.
271 */
272 private $open_graph_conflicting_plugins = array(
273 '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php', // 2 Click Social Media Buttons.
274 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook.
275 'add-meta-tags/add-meta-tags.php', // Add Meta Tags.
276 'complete-open-graph/complete-open-graph.php', // Complete Open Graph.
277 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail.
278 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php', // Open Graph Meta Tags by Heateor.
279 'facebook/facebook.php', // Facebook (official plugin).
280 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one.
281 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php', // Facebook Featured Image & OG Meta Tags.
282 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags.
283 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php', // Facebook Open Graph Meta Tags for WordPress.
284 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag.
285 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer.
286 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php', // Fedmich's Facebook Open Graph Meta.
287 'network-publisher/networkpub.php', // Network Publisher.
288 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG.
289 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php', // NextScripts SNAP.
290 'og-tags/og-tags.php', // OG Tags.
291 'opengraph/opengraph.php', // Open Graph.
292 'open-graph-protocol-framework/open-graph-protocol-framework.php', // Open Graph Protocol Framework.
293 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments.
294 'seo-ultimate/seo-ultimate.php', // SEO Ultimate.
295 'sexybookmarks/sexy-bookmarks.php', // Shareaholic.
296 'shareaholic/sexy-bookmarks.php', // Shareaholic.
297 'sharepress/sharepress.php', // SharePress.
298 'simple-facebook-connect/sfc.php', // Simple Facebook Connect.
299 'social-discussions/social-discussions.php', // Social Discussions.
300 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit.
301 'socialize/socialize.php', // Socialize.
302 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™.
303 'only-tweet-like-share-and-google-1/tweet-like-plusone.php', // Tweet, Like, Google +1 and Share.
304 'wordbooker/wordbooker.php', // Wordbooker.
305 'wpsso/wpsso.php', // WordPress Social Sharing Optimization.
306 'wp-caregiver/wp-caregiver.php', // WP Caregiver.
307 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', // WP Facebook Like Send & Open Graph Meta.
308 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol.
309 'wp-ogp/wp-ogp.php', // WP-OGP.
310 'wp-seopress/seopress.php', // SEOPress.
311 'wp-seopress-pro/seopress-pro.php', // SEOPress Pro.
312 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin.
313 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button.
314 'open-graph-metabox/open-graph-metabox.php', // Open Graph Metabox.
315 'seo-by-rank-math/rank-math.php', // Rank Math.
316 'slim-seo/slim-seo.php', // Slim SEO.
317 );
318
319 /**
320 * Plugins for which we turn off our Twitter Cards Tags implementation.
321 *
322 * @var array Plugins that conflict with Twitter cards.
323 */
324 private $twitter_cards_conflicting_plugins = array(
325 // phpcs:ignore Squiz.PHP.CommentedOutCode.Found
326 // 'twitter/twitter.php', // The official one handles this on its own.
327 // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php
328 'eewee-twitter-card/index.php', // Eewee Twitter Card.
329 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards.
330 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards.
331 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php', // Pure Web Brilliant's Social Graph Twitter Cards Extension.
332 'twitter-cards/twitter-cards.php', // Twitter Cards.
333 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta.
334 'wp-to-twitter/wp-to-twitter.php', // WP to Twitter.
335 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards.
336 'seo-by-rank-math/rank-math.php', // Rank Math.
337 'slim-seo/slim-seo.php', // Slim SEO.
338 );
339
340 /**
341 * Message to display in admin_notice
342 *
343 * @var string
344 */
345 public $message = '';
346
347 /**
348 * Error to display in admin_notice
349 *
350 * @var string
351 */
352 public $error = '';
353
354 /**
355 * Modules that need more privacy description.
356 *
357 * @var string
358 */
359 public $privacy_checks = '';
360
361 /**
362 * Stats to record once the page loads
363 *
364 * @var array
365 */
366 public $stats = array();
367
368 /**
369 * Jetpack_Sync object
370 *
371 * @todo This is also seemingly unused.
372 *
373 * @var object
374 */
375 public $sync;
376
377 /**
378 * Verified data for JSON authorization request
379 *
380 * @deprecated 13.4
381 *
382 * @var array
383 */
384 public $json_api_authorization_request = array();
385
386 /**
387 * Connection manager.
388 *
389 * @var Automattic\Jetpack\Connection\Manager
390 */
391 protected $connection_manager;
392
393 /**
394 * Plugin lock key.
395 *
396 * @var string Transient key used to prevent multiple simultaneous plugin upgrades
397 */
398 public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock';
399
400 /**
401 * Holds an instance of Automattic\Jetpack\A8c_Mc_Stats
402 *
403 * @var Automattic\Jetpack\A8c_Mc_Stats
404 */
405 public $a8c_mc_stats_instance;
406
407 /**
408 * Constant for login redirect key.
409 *
410 * @var string
411 * @since 8.4.0
412 */
413 public static $jetpack_redirect_login = 'jetpack_connect_login_redirect';
414
415 /**
416 * Holds the singleton instance of this class
417 *
418 * @since 2.3.3
419 * @var Jetpack
420 */
421 public static $instance = false;
422
423 /**
424 * Singleton
425 *
426 * @static
427 */
428 public static function init() {
429 if ( ! self::$instance ) {
430 self::$instance = new Jetpack();
431 add_action( 'plugins_loaded', array( self::$instance, 'plugin_upgrade' ) );
432 add_action( 'jetpack_idc_disconnect', array( __CLASS__, 'on_idc_disconnect' ) );
433 }
434
435 return self::$instance;
436 }
437
438 /**
439 * Must never be called statically
440 */
441 public function plugin_upgrade() {
442 if ( self::is_connection_ready() ) {
443 list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
444 if ( JETPACK__VERSION !== $version ) {
445 // Prevent multiple upgrades at once - only a single process should trigger an upgrade to avoid stampedes.
446 if ( wp_using_ext_object_cache() ) {
447 if ( true !== wp_cache_add( self::$plugin_upgrade_lock_key, 1, 'transient', 10 ) ) {
448 return;
449 }
450 } else {
451 if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) {
452 return;
453 }
454
455 set_transient( self::$plugin_upgrade_lock_key, 1, 10 );
456 }
457
458 // check which active modules actually exist and remove others from active_modules list.
459 $unfiltered_modules = self::get_active_modules();
460 $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) );
461 if ( array_diff( $unfiltered_modules, $modules ) ) {
462 self::update_active_modules( $modules );
463 }
464
465 add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
466
467 // Upgrade to 4.3.0.
468 if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) {
469 Jetpack_Options::delete_option( 'identity_crisis_whitelist' );
470 }
471
472 // Make sure Markdown for posts gets turned back on.
473 if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) {
474 update_option( 'wpcom_publish_posts_with_markdown', true );
475 }
476
477 /*
478 * Minileven deprecation. 8.3.0.
479 * Only delete options if not using
480 * the replacement standalone Minileven plugin.
481 */
482 if (
483 ! self::is_plugin_active( 'minileven-master/minileven.php' )
484 && ! self::is_plugin_active( 'minileven/minileven.php' )
485 ) {
486 if ( get_option( 'wp_mobile_custom_css' ) ) {
487 delete_option( 'wp_mobile_custom_css' );
488 }
489 if ( get_option( 'wp_mobile_excerpt' ) ) {
490 delete_option( 'wp_mobile_excerpt' );
491 }
492 if ( get_option( 'wp_mobile_featured_images' ) ) {
493 delete_option( 'wp_mobile_featured_images' );
494 }
495 if ( get_option( 'wp_mobile_app_promos' ) ) {
496 delete_option( 'wp_mobile_app_promos' );
497 }
498 }
499
500 // Upgrade to 8.4.0.
501 if ( Jetpack_Options::get_option( 'ab_connect_banner_green_bar' ) ) {
502 Jetpack_Options::delete_option( 'ab_connect_banner_green_bar' );
503 }
504
505 // Update to 8.8.x (WordPress 5.5 Compatibility).
506 if ( Jetpack_Options::get_option( 'autoupdate_plugins' ) ) {
507 $updated = update_site_option(
508 'auto_update_plugins',
509 array_unique(
510 array_merge(
511 (array) Jetpack_Options::get_option( 'autoupdate_plugins', array() ),
512 (array) get_site_option( 'auto_update_plugins', array() )
513 )
514 )
515 );
516
517 if ( $updated ) {
518 Jetpack_Options::delete_option( 'autoupdate_plugins' );
519 } // Should we have some type of fallback if something fails here?
520 }
521
522 if ( did_action( 'wp_loaded' ) ) {
523 self::upgrade_on_load();
524 } else {
525 add_action(
526 'wp_loaded',
527 array( __CLASS__, 'upgrade_on_load' )
528 );
529 }
530 }
531 }
532 }
533
534 /**
535 * Runs upgrade routines that need to have modules loaded.
536 */
537 public static function upgrade_on_load() {
538
539 // Not attempting any upgrades if jetpack_modules_loaded did not fire.
540 // This can happen in case Jetpack has been just upgraded and is
541 // being initialized late during the page load. In this case we wait
542 // until the next proper admin page load with Jetpack active.
543 if ( ! did_action( 'jetpack_modules_loaded' ) ) {
544 delete_transient( self::$plugin_upgrade_lock_key );
545
546 return;
547 }
548
549 self::maybe_set_version_option();
550
551 if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) {
552 Jetpack_Widget_Conditions::migrate_post_type_rules();
553 }
554
555 if (
556 class_exists( 'Jetpack_Sitemap_Manager' )
557 ) {
558 do_action( 'jetpack_sitemaps_purge_data' );
559 }
560
561 // Delete old stats cache.
562 delete_option( 'jetpack_restapi_stats_cache' );
563
564 delete_transient( self::$plugin_upgrade_lock_key );
565 }
566
567 /**
568 * Saves all the currently active modules to options.
569 * Also fires Action hooks for each newly activated and deactivated module.
570 *
571 * @param array $modules Array of active modules to be saved in options.
572 *
573 * @return bool $success true for success, false for failure.
574 */
575 public static function update_active_modules( $modules ) {
576 return ( new Modules() )->update_active( $modules );
577 }
578
579 /**
580 * Remove all active modules.
581 *
582 * @return void
583 */
584 public static function delete_active_modules() {
585 self::update_active_modules( array() );
586 }
587
588 /**
589 * Adds a hook to plugins_loaded at a priority that's currently the earliest
590 * available.
591 */
592 public function add_configure_hook() {
593 global $wp_filter;
594
595 $current_priority = has_filter( 'plugins_loaded', array( $this, 'configure' ) );
596 if ( false !== $current_priority ) {
597 remove_action( 'plugins_loaded', array( $this, 'configure' ), $current_priority );
598 }
599
600 $taken_priorities = array_map( 'intval', array_keys( $wp_filter['plugins_loaded']->callbacks ) );
601 sort( $taken_priorities );
602
603 $first_priority = array_shift( $taken_priorities );
604
605 if ( $first_priority <= PHP_INT_MIN ) {
606 $new_priority = PHP_INT_MIN;
607 } else {
608 $new_priority = $first_priority - 1;
609 }
610
611 add_action( 'plugins_loaded', array( $this, 'configure' ), $new_priority );
612 }
613
614 /**
615 * Constructor. Initializes WordPress hooks
616 */
617 private function __construct() {
618 /*
619 * Check for and alert any deprecated hooks
620 */
621 add_action( 'init', array( $this, 'deprecated_hooks' ) );
622
623 // Note how this runs at an earlier plugin_loaded hook intentionally to accomodate for other plugins.
624 add_action( 'plugin_loaded', array( $this, 'add_configure_hook' ), 90 );
625 add_action( 'network_plugin_loaded', array( $this, 'add_configure_hook' ), 90 );
626 add_action( 'mu_plugin_loaded', array( $this, 'add_configure_hook' ), 90 );
627 add_action( 'plugins_loaded', array( $this, 'late_initialization' ), 90 );
628
629 add_action( 'jetpack_verify_signature_error', array( $this, 'track_xmlrpc_error' ) );
630
631 /**
632 * Prepare Gutenberg Editor functionality
633 *
634 * The hooks previously here have been moved to modules/blocks.php but leaving this here pending
635 * a longer investigation to see if code is expecting the Gutenberg class to always be available.
636 */
637 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
638 add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );
639
640 add_action( 'jetpack_event_log', array( 'Jetpack', 'log' ), 10, 2 );
641
642 add_filter( 'login_url', array( $this, 'login_url' ), 10, 2 );
643 add_action( 'login_init', array( $this, 'login_init' ) );
644
645 // Set up the REST authentication hooks.
646 Connection_Rest_Authentication::init();
647
648 add_action( 'admin_init', array( $this, 'admin_init' ) );
649 add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
650
651 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ), 20 );
652
653 // Filter the dashboard meta box order to swap the new one in in place of the old one.
654 add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) );
655
656 // WordPress dashboard widget.
657 require_once JETPACK__PLUGIN_DIR . 'class-jetpack-stats-dashboard-widget.php';
658 add_action( 'wp_dashboard_setup', array( new Jetpack_Stats_Dashboard_Widget(), 'init' ) );
659
660 // Returns HTTPS support status.
661 add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) );
662
663 add_action( 'wp_loaded', array( $this, 'register_assets' ) );
664
665 /**
666 * These actions run checks to load additional files.
667 * They check for external files or plugins, so they need to run as late as possible.
668 */
669 add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 );
670 add_action( 'web_stories_story_head', array( $this, 'check_open_graph' ), 1 );
671 add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 );
672 add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
673
674 add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 );
675 add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 );
676
677 add_filter( 'profile_update', array( 'Jetpack', 'user_meta_cleanup' ) );
678
679 add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
680 add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
681
682 add_action(
683 'plugins_loaded',
684 function () {
685 if ( User_Agent_Info::is_mobile_app() ) {
686 add_filter( 'get_edit_post_link', '__return_empty_string' );
687 }
688 }
689 );
690
691 // Update the site's Jetpack plan and products from API on heartbeats.
692 add_action( 'jetpack_heartbeat', array( Jetpack_Plan::class, 'refresh_from_wpcom' ) );
693
694 // Actually push the stats on shutdown.
695 if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
696 add_action( 'shutdown', array( $this, 'push_stats' ) );
697 }
698
699 // After a successful connection.
700 add_action( 'jetpack_site_registered', array( $this, 'activate_default_modules_on_site_register' ) );
701 add_action( 'jetpack_site_registered', array( $this, 'handle_unique_registrations_stats' ) );
702
703 // Actions for Manager::authorize().
704 add_action( 'jetpack_authorize_starting', array( $this, 'authorize_starting' ) );
705 add_action( 'jetpack_authorize_ending_linked', array( $this, 'authorize_ending_linked' ) );
706 add_action( 'jetpack_authorize_ending_authorized', array( $this, 'authorize_ending_authorized' ) );
707
708 Jetpack_Client_Server::init();
709 add_action( 'jetpack_client_authorize_error', array( Jetpack_Client_Server::class, 'client_authorize_error' ) );
710 add_filter( 'jetpack_client_authorize_already_authorized_url', array( Jetpack_Client_Server::class, 'client_authorize_already_authorized_url' ) );
711 add_action( 'jetpack_client_authorize_processing', array( Jetpack_Client_Server::class, 'client_authorize_processing' ) );
712 add_filter( 'jetpack_client_authorize_fallback_url', array( Jetpack_Client_Server::class, 'client_authorize_fallback_url' ) );
713
714 // Filters for the Manager::get_token() urls and request body.
715 add_filter( 'jetpack_token_redirect_url', array( Authorize_Redirect::class, 'filter_connect_redirect_url' ) );
716 add_filter( 'jetpack_token_request_body', array( __CLASS__, 'filter_token_request_body' ) );
717
718 // Filter for the `jetpack/v4/connection/data` API response.
719 add_filter( 'jetpack_current_user_connection_data', array( __CLASS__, 'filter_jetpack_current_user_connection_data' ) );
720
721 // Actions for successful reconnect.
722 add_action( 'jetpack_reconnection_completed', array( $this, 'reconnection_completed' ) );
723
724 // Actions for successful disconnect.
725 add_action( 'jetpack_site_disconnected', array( $this, 'jetpack_site_disconnected' ) );
726
727 // Actions for licensing.
728 Licensing::instance()->initialize();
729
730 // Filters for Sync Callables.
731 add_filter( 'jetpack_sync_callable_whitelist', array( $this, 'filter_sync_callable_whitelist' ), 10, 1 );
732 add_filter( 'jetpack_sync_multisite_callable_whitelist', array( $this, 'filter_sync_multisite_callable_whitelist' ), 10, 1 );
733
734 // Make resources use static domain when possible.
735 add_filter( 'jetpack_static_url', array( 'Automattic\\Jetpack\\Assets', 'staticize_subdomain' ) );
736
737 // Validate the domain names in Jetpack development versions.
738 add_action( 'jetpack_pre_register', array( static::class, 'registration_check_domains' ) );
739
740 // Register product descriptions for partner coupon usage.
741 add_filter( 'jetpack_partner_coupon_products', array( $this, 'get_partner_coupon_product_descriptions' ) );
742
743 // Actions for conditional recommendations.
744 add_action( 'plugins_loaded', array( 'Jetpack_Recommendations', 'init_conditional_recommendation_actions' ) );
745
746 // Add 5-star
747 add_filter( 'plugin_row_meta', array( $this, 'add_5_star_review_link' ), 10, 2 );
748 add_action( 'init', array( Deprecate::class, 'instance' ) );
749 }
750
751 /**
752 * Before everything else starts getting initalized, we need to initialize Jetpack using the
753 * Config object.
754 */
755 public function configure() {
756 $config = new Config();
757
758 foreach (
759 array(
760 'jitm',
761 'sync',
762 'account_protection',
763 'waf',
764 'videopress',
765 'stats',
766 'stats_admin',
767 'import',
768 )
769 as $feature
770 ) {
771 $config->ensure( $feature );
772 }
773
774 $config->ensure(
775 'connection',
776 array(
777 'slug' => 'jetpack',
778 'name' => 'Jetpack',
779 )
780 );
781
782 // Identity crisis package.
783 $config->ensure(
784 'identity_crisis',
785 array(
786 'slug' => 'jetpack',
787 'admin_page' => '/wp-admin/admin.php?page=jetpack',
788 )
789 );
790
791 $config->ensure( 'search' );
792
793 if ( ! $this->connection_manager ) {
794 $this->connection_manager = new Connection_Manager( 'jetpack' );
795 }
796
797 $modules = new Automattic\Jetpack\Modules();
798 if ( $modules->is_active( 'publicize' ) && $this->connection_manager->has_connected_user() ) {
799 $config->ensure( 'publicize' );
800 }
801
802 add_action( 'jetpack_initialize_tracking', array( $this, 'initialize_tracking' ) );
803
804 /*
805 * Load things that should only be in Network Admin.
806 *
807 * For now blow away everything else until a more full
808 * understanding of what is needed at the network level is
809 * available
810 */
811 if ( is_multisite() ) {
812 $network = Jetpack_Network::init();
813 $network->set_connection( $this->connection_manager );
814 }
815
816 $is_connection_ready = self::is_connection_ready();
817
818 if ( $is_connection_ready ) {
819 add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) );
820 $this->run_initialize_tracking_action();
821
822 Jetpack_Heartbeat::init();
823 if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) {
824 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
825 Jetpack_Search_Performance_Logger::init();
826 }
827 } else {
828 add_action( 'jetpack_agreed_to_terms_of_service', array( $this, 'run_initialize_tracking_action' ) );
829 add_action( 'rest_api_init', array( $this, 'run_initialize_tracking_action' ) );
830 add_filter(
831 'xmlrpc_methods',
832 function ( $methods ) {
833 $this->run_initialize_tracking_action();
834 return $methods;
835 },
836 1
837 );
838 }
839
840 // Initialize remote file upload request handlers.
841 $this->add_remote_request_handlers();
842
843 /*
844 * Enable enhanced handling of previewing sites in Calypso
845 */
846 if ( $is_connection_ready ) {
847 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
848 add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );
849 add_action( 'rest_api_init', array( $this, 'maybe_initialize_rest_jsonapi' ) );
850 }
851 }
852
853 /**
854 * Runs on plugins_loaded. Use this to add code that needs to be executed later than other
855 * initialization code.
856 *
857 * @action plugins_loaded
858 */
859 public function late_initialization() {
860 add_action( 'after_setup_theme', array( 'Jetpack', 'load_modules' ), -2 );
861 My_Jetpack_Initializer::init();
862
863 // Initialize Boost Speed Score
864 new Speed_Score( array(), 'jetpack-dashboard' );
865
866 /**
867 * Fires when Jetpack is fully loaded and ready. This is the point where it's safe
868 * to instantiate classes from packages and namespaces that are managed by the Jetpack Autoloader.
869 *
870 * @since 8.1.0
871 *
872 * @param Jetpack $jetpack the main plugin class object.
873 */
874 do_action( 'jetpack_loaded', $this );
875
876 add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 2 );
877 }
878
879 /**
880 * This is ported over from the manage module, which has been deprecated and baked in here.
881 */
882 public function add_wpcom_to_allowed_redirect_hosts() {
883 add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_domain' ) );
884 }
885
886 /**
887 * Return $domains, with 'wordpress.com' appended.
888 * This is ported over from the manage module, which has been deprecated and baked in here.
889 *
890 * @param array $domains Array of domains allowed for redirect.
891 *
892 * @return array
893 */
894 public function allow_wpcom_domain( $domains ) {
895 if ( empty( $domains ) ) {
896 $domains = array();
897 }
898 $domains[] = 'wordpress.com';
899 return array_unique( $domains );
900 }
901
902 /**
903 * Redirect edit post links to Calypso.
904 *
905 * @deprecated since 13.9
906 *
907 * @param string $default_url Post edit URL.
908 * @param int $post_id Post ID.
909 *
910 * @return string
911 */
912 public function point_edit_post_links_to_calypso( $default_url, $post_id ) {
913 _deprecated_function( __METHOD__, '13.9' );
914
915 $post = get_post( $post_id );
916
917 if ( empty( $post ) ) {
918 return $default_url;
919 }
920
921 $post_type = $post->post_type;
922
923 // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso.
924 // https://en.support.wordpress.com/custom-post-types/.
925 $allowed_post_types = array(
926 'post',
927 'page',
928 'jetpack-portfolio',
929 'jetpack-testimonial',
930 );
931
932 if ( ! in_array( $post_type, $allowed_post_types, true ) ) {
933 return $default_url;
934 }
935
936 return Redirect::get_url(
937 'calypso-edit-' . $post_type,
938 array(
939 'path' => $post_id,
940 )
941 );
942 }
943
944 /**
945 * Redirect edit comment links to Calypso.
946 *
947 * @deprecated since 13.9
948 *
949 * @param string $url Comment edit URL.
950 *
951 * @return string
952 */
953 public function point_edit_comment_links_to_calypso( $url ) {
954 _deprecated_function( __METHOD__, '13.9' );
955
956 // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID.
957 $query_args = null;
958 wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args );
959
960 return Redirect::get_url(
961 'calypso-edit-comment',
962 array(
963 'path' => $query_args['amp;c'],
964 )
965 );
966 }
967
968 /**
969 * Extend Sync callables with Jetpack Plugin functions.
970 *
971 * @param array $callables list of callables.
972 *
973 * @return array list of callables.
974 */
975 public function filter_sync_callable_whitelist( $callables ) {
976 // Jetpack Functions.
977 $jetpack_callables = array(
978 'single_user_site' => array( 'Jetpack', 'is_single_user_site' ),
979 'updates' => array( 'Jetpack', 'get_updates' ),
980 'available_jetpack_blocks' => array( 'Jetpack_Gutenberg', 'get_availability' ), // Includes both Gutenberg blocks *and* plugins.
981 );
982 return array_merge( $callables, $jetpack_callables );
983 }
984
985 /**
986 * Extend Sync multisite callables with Jetpack Plugin functions.
987 *
988 * @param array $callables list of callables.
989 *
990 * @return array list of callables.
991 */
992 public function filter_sync_multisite_callable_whitelist( $callables ) {
993
994 // Jetpack Funtions.
995 $jetpack_multisite_callables = array(
996 'network_name' => array( 'Jetpack', 'network_name' ),
997 'network_allow_new_registrations' => array( 'Jetpack', 'network_allow_new_registrations' ),
998 'network_add_new_users' => array( 'Jetpack', 'network_add_new_users' ),
999 'network_site_upload_space' => array( 'Jetpack', 'network_site_upload_space' ),
1000 'network_upload_file_types' => array( 'Jetpack', 'network_upload_file_types' ),
1001 'network_enable_administration_menus' => array( 'Jetpack', 'network_enable_administration_menus' ),
1002 );
1003 $callables = array_merge( $callables, $jetpack_multisite_callables );
1004
1005 return $callables;
1006 }
1007
1008 /**
1009 * If there are any stats that need to be pushed, but haven't been, push them now.
1010 */
1011 public function push_stats() {
1012 if ( ! empty( $this->stats ) ) {
1013 $this->do_stats( 'server_side' );
1014 }
1015 }
1016
1017 /**
1018 * Sets the Jetpack custom capabilities.
1019 *
1020 * @param string[] $caps Array of the user's capabilities.
1021 * @param string $cap Capability name.
1022 */
1023 public function jetpack_custom_caps( $caps, $cap ) {
1024 switch ( $cap ) {
1025 case 'jetpack_manage_autoupdates':
1026 $caps = array(
1027 'manage_options',
1028 'update_plugins',
1029 );
1030 break;
1031 case 'jetpack_network_admin_page':
1032 case 'jetpack_network_settings_page':
1033 $caps = array( 'manage_network_plugins' );
1034 break;
1035 case 'jetpack_network_sites_page':
1036 $caps = array( 'manage_sites' );
1037 break;
1038 case 'jetpack_admin_page':
1039 $is_offline_mode = ( new Status() )->is_offline_mode();
1040 if ( $is_offline_mode ) {
1041 $caps = array( 'manage_options' );
1042 break;
1043 } else {
1044 $caps = array( 'edit_posts' );
1045 }
1046 break;
1047 }
1048 return $caps;
1049 }
1050
1051 /**
1052 * Register assets for use in various modules and the Jetpack admin page.
1053 *
1054 * @uses wp_script_is, wp_register_script, plugins_url
1055 * @action wp_loaded
1056 * @return void
1057 */
1058 public function register_assets() {
1059 if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
1060 wp_register_script(
1061 'jetpack-gallery-settings',
1062 Assets::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ),
1063 array( 'media-views' ),
1064 '20121225',
1065 true
1066 );
1067 }
1068
1069 if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) {
1070 wp_register_script(
1071 'jetpack-twitter-timeline',
1072 Assets::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ),
1073 array(),
1074 '4.0.0',
1075 true
1076 );
1077 }
1078
1079 if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) {
1080 wp_register_script(
1081 'jetpack-facebook-embed',
1082 Assets::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ),
1083 array(),
1084 JETPACK__VERSION,
1085 true
1086 );
1087
1088 /** This filter is documented in modules/sharedaddy/sharing-sources.php */
1089 $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
1090 if ( ! is_numeric( $fb_app_id ) ) {
1091 $fb_app_id = '';
1092 }
1093 wp_localize_script(
1094 'jetpack-facebook-embed',
1095 'jpfbembed',
1096 array(
1097 'appid' => $fb_app_id,
1098 'locale' => $this->get_locale(),
1099 )
1100 );
1101 }
1102
1103 /**
1104 * As jetpack_register_genericons is by default fired off a hook,
1105 * the hook may have already fired by this point.
1106 * So, let's just trigger it manually.
1107 */
1108 require_once JETPACK__PLUGIN_DIR . '_inc/genericons.php';
1109 jetpack_register_genericons();
1110
1111 /**
1112 * Register the social logos
1113 */
1114 require_once JETPACK__PLUGIN_DIR . '_inc/social-logos.php';
1115 jetpack_register_social_logos();
1116 }
1117
1118 /**
1119 * Guess locale from language code.
1120 *
1121 * @param string $lang Language code.
1122 * @return string|bool
1123 */
1124 public function guess_locale_from_lang( $lang ) {
1125 if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) {
1126 return 'en_US';
1127 }
1128
1129 if ( ! class_exists( 'GP_Locales' ) ) {
1130 if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
1131 return false;
1132 }
1133
1134 require JETPACK__GLOTPRESS_LOCALES_PATH;
1135 }
1136
1137 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1138 // WP.com: get_locale() returns 'it'.
1139 $locale = GP_Locales::by_slug( $lang );
1140 } else {
1141 // Jetpack: get_locale() returns 'it_IT';.
1142 $locale = GP_Locales::by_field( 'facebook_locale', $lang );
1143 }
1144
1145 if ( ! $locale ) {
1146 return false;
1147 }
1148
1149 if ( empty( $locale->facebook_locale ) ) {
1150 if ( empty( $locale->wp_locale ) ) {
1151 return false;
1152 } else {
1153 // Facebook SDK is smart enough to fall back to en_US if a
1154 // locale isn't supported. Since supported Facebook locales
1155 // can fall out of sync, we'll attempt to use the known
1156 // wp_locale value and rely on said fallback.
1157 return $locale->wp_locale;
1158 }
1159 }
1160
1161 return $locale->facebook_locale;
1162 }
1163
1164 /**
1165 * Get the locale.
1166 *
1167 * @return string|bool
1168 */
1169 public function get_locale() {
1170 $locale = $this->guess_locale_from_lang( get_locale() );
1171
1172 if ( ! $locale ) {
1173 $locale = 'en_US';
1174 }
1175
1176 return $locale;
1177 }
1178
1179 /**
1180 * Return the network_site_url so that .com knows what network this site is a part of.
1181 *
1182 * @return string
1183 */
1184 public function jetpack_main_network_site_option() {
1185 return network_site_url();
1186 }
1187 /**
1188 * Network Name.
1189 */
1190 public static function network_name() {
1191 global $current_site;
1192 return $current_site->site_name;
1193 }
1194 /**
1195 * Does the network allow new user and site registrations.
1196 *
1197 * @return string
1198 */
1199 public static function network_allow_new_registrations() {
1200 return ( in_array( get_site_option( 'registration' ), array( 'none', 'user', 'blog', 'all' ), true ) ? get_site_option( 'registration' ) : 'none' );
1201 }
1202 /**
1203 * Does the network allow admins to add new users.
1204 *
1205 * @return bool
1206 */
1207 public static function network_add_new_users() {
1208 return (bool) get_site_option( 'add_new_users' );
1209 }
1210 /**
1211 * File upload psace left per site in MB.
1212 * -1 means NO LIMIT.
1213 *
1214 * @return int
1215 */
1216 public static function network_site_upload_space() {
1217 // value in MB.
1218 return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() );
1219 }
1220
1221 /**
1222 * Network allowed file types.
1223 *
1224 * @return string
1225 */
1226 public static function network_upload_file_types() {
1227 return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' );
1228 }
1229
1230 /**
1231 * Maximum file upload size set by the network.
1232 *
1233 * @return int
1234 */
1235 public static function network_max_upload_file_size() {
1236 // value in KB.
1237 return get_site_option( 'fileupload_maxk', 300 );
1238 }
1239
1240 /**
1241 * Lets us know if a site allows admins to manage the network.
1242 *
1243 * @return array
1244 */
1245 public static function network_enable_administration_menus() {
1246 return get_site_option( 'menu_items' );
1247 }
1248
1249 /**
1250 * If a user has been promoted to or demoted from admin, we need to clear the
1251 * jetpack_other_linked_admins transient.
1252 *
1253 * @since 4.3.2
1254 * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared.
1255 *
1256 * @param int $user_id The user ID whose role changed.
1257 * @param string $role The new role.
1258 * @param array $old_roles An array of the user's previous roles.
1259 */
1260 public function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) {
1261 if ( 'administrator' === $role
1262 || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles, true ) )
1263 || $old_roles === null
1264 ) {
1265 delete_transient( 'jetpack_other_linked_admins' );
1266 }
1267 }
1268
1269 /**
1270 * Checks to see if there are any other users available to become primary
1271 * Users must both:
1272 * - Be linked to wpcom
1273 * - Be an admin
1274 *
1275 * @return mixed False if no other users are linked, Int if there are.
1276 */
1277 public static function get_other_linked_admins() {
1278 $other_linked_users = get_transient( 'jetpack_other_linked_admins' );
1279
1280 if ( false === $other_linked_users ) {
1281 $admins = get_users( array( 'role' => 'administrator' ) );
1282 if ( count( $admins ) > 1 ) {
1283 $available = array();
1284 foreach ( $admins as $admin ) {
1285 if ( self::connection()->is_user_connected( $admin->ID ) ) {
1286 $available[] = $admin->ID;
1287 }
1288 }
1289
1290 $count_connected_admins = count( $available );
1291 if ( count( $available ) > 1 ) {
1292 $other_linked_users = $count_connected_admins;
1293 } else {
1294 $other_linked_users = 0;
1295 }
1296 } else {
1297 $other_linked_users = 0;
1298 }
1299
1300 set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS );
1301 }
1302
1303 return ( 0 === $other_linked_users ) ? false : $other_linked_users;
1304 }
1305
1306 /**
1307 * Return whether we are dealing with a multi network setup or not.
1308 * The reason we are type casting this is because we want to avoid the situation where
1309 * the result is false since when is_main_network_option return false it cases
1310 * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the
1311 * database which could be set to anything as opposed to what this function returns.
1312 *
1313 * @return boolean
1314 */
1315 public function is_main_network_option() {
1316 // returns either an '1' or an empty string.
1317 return (string) (bool) self::is_multi_network();
1318 }
1319
1320 /**
1321 * Return true if we are with multi-site or multi-network false if we are dealing with single site.
1322 *
1323 * @return string
1324 */
1325 public function is_multisite() {
1326 return (string) (bool) is_multisite();
1327 }
1328
1329 /**
1330 * Implemented since there is no core is multi network function
1331 * Right now there is no way to tell if we which network is the dominant network on the system
1332 *
1333 * @since 3.3
1334 * @return boolean
1335 */
1336 public static function is_multi_network() {
1337 global $wpdb;
1338
1339 // if we don't have a multi site setup no need to do any more.
1340 if ( ! is_multisite() ) {
1341 return false;
1342 }
1343
1344 $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
1345 if ( $num_sites > 1 ) {
1346 return true;
1347 } else {
1348 return false;
1349 }
1350 }
1351
1352 /**
1353 * Get back if the current site is single user site.
1354 *
1355 * @return bool
1356 */
1357 public static function is_single_user_site() {
1358 global $wpdb;
1359
1360 $some_users = get_transient( 'jetpack_is_single_user' );
1361 if ( false === $some_users ) {
1362 $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
1363 set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
1364 }
1365 return 1 === (int) $some_users;
1366 }
1367
1368 /**
1369 * Returns true if the site has file write access false otherwise.
1370 *
1371 * @return string ( '1' | '0' )
1372 **/
1373 public static function file_system_write_access() {
1374 if ( ! function_exists( 'get_filesystem_method' ) ) {
1375 require_once ABSPATH . 'wp-admin/includes/file.php';
1376 }
1377
1378 require_once ABSPATH . 'wp-admin/includes/template.php';
1379
1380 $filesystem_method = get_filesystem_method();
1381 if ( 'direct' === $filesystem_method ) {
1382 return 1;
1383 }
1384
1385 ob_start();
1386 $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
1387 ob_end_clean();
1388 if ( $filesystem_credentials_are_stored ) {
1389 return 1;
1390 }
1391 return 0;
1392 }
1393
1394 // phpcs:disable WordPress.WP.CapitalPDangit.MisspelledInComment
1395 /**
1396 * Gets updates and stores in jetpack_updates.
1397 *
1398 * The jetpack_updates option is saved in the following schema:
1399 *
1400 * array (
1401 * 'plugins' => (int) Number of plugin updates available.
1402 * 'themes' => (int) Number of theme updates available.
1403 * 'wordpress' => (int) Number of WordPress core updates available.
1404 * 'translations' => (int) Number of translation updates available.
1405 * 'total' => (int) Total of all available updates.
1406 * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed.
1407 * )
1408 *
1409 * @return array
1410 */
1411 public static function get_updates() {
1412 $updates = array();
1413 $update_data = wp_get_update_data();
1414
1415 // Stores the individual update counts as well as the total count.
1416 if ( isset( $update_data['counts'] ) ) {
1417 $updates = $update_data['counts'];
1418 }
1419
1420 // If we need to update WordPress core, let's find the latest version number.
1421 if ( ! empty( $updates['wordpress'] ) ) {
1422 $cur = get_preferred_from_update_core();
1423 if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
1424 $updates['wp_update_version'] = $cur->current;
1425 }
1426 }
1427 return $updates;
1428 }
1429 // phpcs:enable WordPress.WP.CapitalPDangit.MisspelledInComment
1430
1431 /**
1432 * Get update details for core, plugins, and themes.
1433 *
1434 * @return array
1435 */
1436 public static function get_update_details() {
1437 $update_details = array(
1438 'update_core' => get_site_transient( 'update_core' ),
1439 'update_plugins' => get_site_transient( 'update_plugins' ),
1440 'update_themes' => get_site_transient( 'update_themes' ),
1441 );
1442 return $update_details;
1443 }
1444
1445 /**
1446 * Is Jetpack active?
1447 * The method only checks if there's an existing token for the master user. It doesn't validate the token.
1448 *
1449 * This method is deprecated since 9.6.0. Please use one of the methods provided by the Manager class in the Connection package,
1450 * or Jetpack::is_connection_ready if you want to know when the Jetpack plugin starts considering the connection ready to be used.
1451 *
1452 * Since this method has a wide spread use, we decided not to throw any deprecation warnings for now.
1453 *
1454 * @deprecated 9.6.0
1455 *
1456 * @return bool
1457 */
1458 public static function is_active() {
1459 return self::connection()->has_connected_owner();
1460 }
1461
1462 /**
1463 * Returns true if the current site is connected to WordPress.com and has the minimum requirements to enable Jetpack UI
1464 *
1465 * This method was introduced just before the release of the possibility to use Jetpack without a user connection, while
1466 * it was available only when no_user_testing_mode was enabled. In the near future, this will return is_connected for all
1467 * users and this option will be available by default for everybody.
1468 *
1469 * @since 9.6.0
1470 * @since 9.7.0 returns is_connected in all cases and adds filter to the returned value
1471 *
1472 * @return bool is the site connection ready to be used?
1473 */
1474 public static function is_connection_ready() {
1475 /**
1476 * Allows filtering whether the connection is ready to be used. If true, this will enable the Jetpack UI and modules
1477 *
1478 * Modules will be enabled depending on the connection status and if the module requires a connection or user connection.
1479 *
1480 * @since 9.7.0
1481 *
1482 * @param bool $is_connection_ready Is the connection ready?
1483 * @param Automattic\Jetpack\Connection\Manager $connection_manager Instance of the Manager class, can be used to check the connection status.
1484 */
1485 return apply_filters( 'jetpack_is_connection_ready', self::connection()->is_connected(), self::connection() );
1486 }
1487
1488 /**
1489 * Determines reason for Jetpack offline mode.
1490 */
1491 public static function development_mode_trigger_text() {
1492 $status = new Status();
1493
1494 if ( ! $status->is_offline_mode() ) {
1495 return __( 'Jetpack is not in Offline Mode.', 'jetpack' );
1496 }
1497
1498 if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) {
1499 $notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' );
1500 } elseif ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
1501 $notice = __( 'The WP_LOCAL_DEV constant is defined in wp-config.php or elsewhere.', 'jetpack' );
1502 } elseif ( $status->is_local_site() ) {
1503 $notice = __( 'The site URL is a known local development environment URL (e.g. http://localhost).', 'jetpack' );
1504 /** This filter is documented in packages/status/src/class-status.php */
1505 } elseif ( has_filter( 'jetpack_development_mode' ) && apply_filters( 'jetpack_development_mode', false ) ) { // This is a deprecated filter name.
1506 $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' );
1507 } elseif ( get_option( 'jetpack_offline_mode' ) ) {
1508 $notice = __( 'The jetpack_offline_mode option is set to true.', 'jetpack' );
1509 } else {
1510 $notice = __( 'The jetpack_offline_mode filter is set to true.', 'jetpack' );
1511 }
1512
1513 return $notice;
1514 }
1515 /**
1516 * Get Jetpack offline mode notice text and notice class.
1517 *
1518 * Mirrors the checks made in Automattic\Jetpack\Status->is_offline_mode
1519 */
1520 public static function show_development_mode_notice() {
1521 if ( ( new Status() )->is_offline_mode() ) {
1522 $notice = sprintf(
1523 /* translators: %s is a URL */
1524 __( 'In <a href="%s" target="_blank">Offline Mode</a>:', 'jetpack' ),
1525 esc_url( Redirect::get_url( 'jetpack-support-development-mode' ) )
1526 );
1527
1528 $notice .= ' ' . self::development_mode_trigger_text();
1529
1530 echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All provided text.
1531 }
1532
1533 // Throw up a notice if using a development version and as for feedback.
1534 if ( self::is_development_version() ) {
1535 /* translators: %s is a URL */
1536 $notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), esc_url( Redirect::get_url( 'jetpack-contact-support-beta-group' ) ) );
1537
1538 echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All provided text.
1539 }
1540 }
1541
1542 /**
1543 * Whether Jetpack's version maps to a public release, or a development version.
1544 */
1545 public static function is_development_version() {
1546 /**
1547 * Allows filtering whether this is a development version of Jetpack.
1548 *
1549 * This filter is especially useful for tests.
1550 *
1551 * @since 4.3.0
1552 *
1553 * @param bool $development_version Is this a develoment version of Jetpack?
1554 */
1555 return (bool) apply_filters(
1556 'jetpack_development_version',
1557 ! preg_match( '/^\d+(\.\d+)+$/', Constants::get_constant( 'JETPACK__VERSION' ) )
1558 );
1559 }
1560
1561 /**
1562 * Get the wpcom email of the current|specified connected user.
1563 *
1564 * @param null|int $user_id User ID or will use get_current_user_id if null.
1565 */
1566 public static function get_connected_user_email( $user_id = null ) {
1567 if ( ! $user_id ) {
1568 $user_id = get_current_user_id();
1569 }
1570
1571 $xml = new Jetpack_IXR_Client(
1572 array(
1573 'user_id' => $user_id,
1574 )
1575 );
1576 $xml->query( 'wpcom.getUserEmail' );
1577 if ( ! $xml->isError() ) {
1578 return $xml->getResponse();
1579 }
1580 return false;
1581 }
1582
1583 /**
1584 * Get the wpcom email of the master user.
1585 */
1586 public static function get_master_user_email() {
1587 $master_user_id = Jetpack_Options::get_option( 'master_user' );
1588 if ( $master_user_id ) {
1589 return self::get_connected_user_email( $master_user_id );
1590 }
1591 return '';
1592 }
1593
1594 /**
1595 * Gets current user IP address.
1596 *
1597 * @param bool $check_all_headers Check all headers? Default is `false`.
1598 *
1599 * @deprecated Jetpack 10.6
1600 *
1601 * @return string Current user IP address.
1602 */
1603 public static function current_user_ip( $check_all_headers = false ) {
1604 _deprecated_function( __METHOD__, 'jetpack-10.6', 'Automattic\\Jetpack\\Status\\Visitor::get_ip' );
1605
1606 return ( new Visitor() )->get_ip( $check_all_headers );
1607 }
1608
1609 /**
1610 * Loads the currently active modules.
1611 */
1612 public static function load_modules() {
1613 $status = new Status();
1614
1615 if (
1616 ! self::is_connection_ready()
1617 && ! $status->is_offline_mode()
1618 && (
1619 ! is_multisite()
1620 || ! get_site_option( 'jetpack_protect_active' )
1621 )
1622 ) {
1623 return;
1624 }
1625
1626 $version = Jetpack_Options::get_option( 'version' );
1627 if ( ! $version ) {
1628 $version = JETPACK__VERSION . ':' . time();
1629 $old_version = $version;
1630 /** This action is documented in class.jetpack.php */
1631 do_action( 'updating_jetpack_version', $version, false );
1632 Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
1633 }
1634 list( $version ) = explode( ':', $version );
1635
1636 $modules = array_filter( self::get_active_modules(), array( 'Jetpack', 'is_module' ) );
1637
1638 $modules_data = array();
1639
1640 // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check.
1641 if ( version_compare( $version, JETPACK__VERSION, '<' ) ) {
1642 $updated_modules = array();
1643 foreach ( $modules as $module ) {
1644 $modules_data[ $module ] = self::get_module( $module );
1645 if ( ! isset( $modules_data[ $module ]['changed'] ) ) {
1646 continue;
1647 }
1648
1649 if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) {
1650 continue;
1651 }
1652
1653 $updated_modules[] = $module;
1654 }
1655
1656 $modules = array_diff( $modules, $updated_modules );
1657 }
1658
1659 $is_site_connection = false;
1660
1661 if ( method_exists( self::connection(), 'is_site_connection' ) ) {
1662 $is_site_connection = self::connection()->is_site_connection();
1663 }
1664
1665 foreach ( $modules as $index => $module ) {
1666 // If we're in offline/site-connection mode, disable modules requiring a connection/user connection.
1667 if ( $status->is_offline_mode() || $is_site_connection ) {
1668 // Prime the pump if we need to.
1669 if ( empty( $modules_data[ $module ] ) ) {
1670 $modules_data[ $module ] = self::get_module( $module );
1671 }
1672 // If the module requires a connection, but we're in local mode, don't include it.
1673 if ( $status->is_offline_mode() && $modules_data[ $module ]['requires_connection'] ) {
1674 continue;
1675 }
1676
1677 if ( $is_site_connection && $modules_data[ $module ]['requires_user_connection'] ) {
1678 continue;
1679 }
1680 }
1681
1682 if ( did_action( 'jetpack_module_loaded_' . $module ) ) {
1683 continue;
1684 }
1685
1686 if ( ! include_once self::get_module_path( $module ) ) { // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.NotAbsolutePath
1687 unset( $modules[ $index ] );
1688 self::update_active_modules( array_values( $modules ) );
1689 continue;
1690 }
1691
1692 /**
1693 * Fires when a specific module is loaded.
1694 * The dynamic part of the hook, $module, is the module slug.
1695 *
1696 * @since 1.1.0
1697 */
1698 do_action( 'jetpack_module_loaded_' . $module );
1699 }
1700
1701 /**
1702 * Fires when all the modules are loaded.
1703 *
1704 * @since 1.1.0
1705 */
1706 do_action( 'jetpack_modules_loaded' );
1707
1708 // Load module-specific code that is needed even when a module isn't active. Loaded here because code contained therein may need actions such as setup_theme.
1709 require_once JETPACK__PLUGIN_DIR . 'modules/module-extras.php';
1710 }
1711
1712 /**
1713 * Check if Jetpack's REST API compat file should be included
1714 *
1715 * @action plugins_loaded
1716 * @return void
1717 */
1718 public function check_rest_api_compat() {
1719 /**
1720 * Filters the list of REST API compat files to be included.
1721 *
1722 * @since 2.2.5
1723 *
1724 * @param array $args Array of REST API compat files to include.
1725 */
1726 $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() );
1727
1728 foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) {
1729 require_once $_jetpack_rest_api_compat_include;
1730 }
1731 }
1732
1733 /**
1734 * Gets all plugins currently active in values, regardless of whether they're
1735 * traditionally activated or network activated.
1736 *
1737 * @todo Store the result in core's object cache maybe?
1738 */
1739 public static function get_active_plugins() {
1740 $active_plugins = (array) get_option( 'active_plugins', array() );
1741
1742 if ( is_multisite() ) {
1743 // Due to legacy code, active_sitewide_plugins stores them in the keys,
1744 // whereas active_plugins stores them in the values.
1745 $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
1746 if ( $network_plugins ) {
1747 $active_plugins = array_merge( $active_plugins, $network_plugins );
1748 }
1749 }
1750
1751 sort( $active_plugins );
1752
1753 return array_unique( $active_plugins );
1754 }
1755
1756 /**
1757 * Gets and parses additional plugin data to send with the heartbeat data
1758 *
1759 * @since 3.8.1
1760 *
1761 * @return array Array of plugin data
1762 */
1763 public static function get_parsed_plugin_data() {
1764 if ( ! function_exists( 'get_plugins' ) ) {
1765 require_once ABSPATH . 'wp-admin/includes/plugin.php';
1766 }
1767 /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
1768 $all_plugins = apply_filters( 'all_plugins', get_plugins() );
1769 $active_plugins = self::get_active_plugins();
1770
1771 $plugins = array();
1772 foreach ( $all_plugins as $path => $plugin_data ) {
1773 $plugins[ $path ] = array(
1774 'is_active' => in_array( $path, $active_plugins, true ),
1775 'file' => $path,
1776 'name' => $plugin_data['Name'],
1777 'version' => $plugin_data['Version'],
1778 'author' => $plugin_data['Author'],
1779 );
1780 }
1781
1782 return $plugins;
1783 }
1784
1785 /**
1786 * Gets and parses theme data to send with the heartbeat data
1787 *
1788 * @since 3.8.1
1789 *
1790 * @return array Array of theme data
1791 */
1792 public static function get_parsed_theme_data() {
1793 $all_themes = wp_get_themes( array( 'allowed' => true ) );
1794 $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' );
1795
1796 $themes = array();
1797 foreach ( $all_themes as $slug => $theme_data ) {
1798 $theme_headers = array();
1799 foreach ( $header_keys as $header_key ) {
1800 $theme_headers[ $header_key ] = $theme_data->get( $header_key );
1801 }
1802
1803 $themes[ $slug ] = array(
1804 'is_active_theme' => wp_get_theme()->get_template() === $slug,
1805 'slug' => $slug,
1806 'theme_root' => $theme_data->get_theme_root_uri(),
1807 'parent' => $theme_data->parent(),
1808 'headers' => $theme_headers,
1809 );
1810 }
1811
1812 return $themes;
1813 }
1814
1815 /**
1816 * Checks whether a specific plugin is active.
1817 *
1818 * We don't want to store these in a static variable, in case
1819 * there are switch_to_blog() calls involved.
1820 *
1821 * @param string $plugin Plugin to check in 'folder/file.php` format.
1822 */
1823 public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) {
1824 return in_array( $plugin, self::get_active_plugins(), true );
1825 }
1826
1827 /**
1828 * Check if Jetpack's Open Graph tags should be used.
1829 * If certain plugins are active, Jetpack's og tags are suppressed.
1830 *
1831 * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
1832 * @action plugins_loaded
1833 * @return void
1834 */
1835 public function check_open_graph() {
1836 if ( in_array( 'publicize', self::get_active_modules(), true ) || in_array( 'sharedaddy', self::get_active_modules(), true ) ) {
1837 include_once JETPACK__PLUGIN_DIR . 'enhanced-open-graph.php';
1838 add_filter( 'jetpack_enable_open_graph', '__return_true', 0 );
1839 }
1840
1841 $active_plugins = self::get_active_plugins();
1842
1843 if ( ! empty( $active_plugins ) ) {
1844 foreach ( $this->open_graph_conflicting_plugins as $plugin ) {
1845 if ( in_array( $plugin, $active_plugins, true ) ) {
1846 add_filter( 'jetpack_enable_open_graph', '__return_false', 99 );
1847 break;
1848 }
1849 }
1850 }
1851
1852 /**
1853 * Allow the addition of Open Graph Meta Tags to all pages.
1854 *
1855 * @since 2.0.3
1856 *
1857 * @param bool false Should Open Graph Meta tags be added. Default to false.
1858 */
1859 if ( apply_filters( 'jetpack_enable_open_graph', false ) ) {
1860 require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php';
1861 }
1862 }
1863
1864 /**
1865 * Check if Jetpack's Twitter tags should be used.
1866 * If certain plugins are active, Jetpack's twitter tags are suppressed.
1867 *
1868 * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
1869 * @action plugins_loaded
1870 * @return void
1871 */
1872 public function check_twitter_tags() {
1873
1874 $active_plugins = self::get_active_plugins();
1875
1876 if ( ! empty( $active_plugins ) ) {
1877 foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) {
1878 if ( in_array( $plugin, $active_plugins, true ) ) {
1879 add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 );
1880 break;
1881 }
1882 }
1883 }
1884
1885 /**
1886 * Allow Twitter Card Meta tags to be disabled.
1887 *
1888 * @since 2.6.0
1889 *
1890 * @param bool true Should Twitter Card Meta tags be disabled. Default to true.
1891 */
1892 if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) {
1893 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
1894 }
1895 }
1896
1897 /* Jetpack Options API */
1898
1899 /**
1900 * Gets the option names from Jetpack_Options.
1901 *
1902 * @param string $type Jetpack option type.
1903 *
1904 * @return array
1905 */
1906 public static function get_option_names( $type = 'compact' ) {
1907 return Jetpack_Options::get_option_names( $type );
1908 }
1909
1910 /**
1911 * Returns the requested option.
1912 *
1913 * Looks in jetpack_options or jetpack_$name as appropriate.
1914 *
1915 * @param string $name Option name.
1916 * @param mixed $default Default value.
1917 */
1918 public static function get_option( $name, $default = false ) {
1919 return Jetpack_Options::get_option( $name, $default );
1920 }
1921
1922 /**
1923 * Returns an array of all PHP files in the specified absolute path.
1924 * Equivalent to glob( "$absolute_path/*.php" ).
1925 *
1926 * @param string $absolute_path The absolute path of the directory to search.
1927 * @return array Array of absolute paths to the PHP files.
1928 */
1929 public static function glob_php( $absolute_path ) {
1930 return ( new Files() )->glob_php( $absolute_path );
1931 }
1932
1933 /**
1934 * Activate new modules.
1935 *
1936 * @param bool $redirect Should this function redirect after activation.
1937 *
1938 * @return void
1939 */
1940 public static function activate_new_modules( $redirect = false ) {
1941 if ( ! self::is_connection_ready() && ! ( new Status() )->is_offline_mode() ) {
1942 return;
1943 }
1944
1945 $jetpack_old_version = Jetpack_Options::get_option( 'version' );
1946 if ( ! $jetpack_old_version ) {
1947 $old_version = '1.1:' . time();
1948 $version = $old_version;
1949 $jetpack_old_version = $version;
1950 /** This action is documented in class.jetpack.php */
1951 do_action( 'updating_jetpack_version', $version, false );
1952 Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
1953 }
1954
1955 list( $jetpack_version ) = explode( ':', $jetpack_old_version );
1956
1957 if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) {
1958 return;
1959 }
1960
1961 $active_modules = self::get_active_modules();
1962 $reactivate_modules = array();
1963 foreach ( $active_modules as $active_module ) {
1964 $module = self::get_module( $active_module );
1965 if ( ! isset( $module['changed'] ) ) {
1966 continue;
1967 }
1968
1969 if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) {
1970 continue;
1971 }
1972
1973 $reactivate_modules[] = $active_module;
1974 self::deactivate_module( $active_module );
1975 }
1976
1977 $new_version = JETPACK__VERSION . ':' . time();
1978 /** This action is documented in class.jetpack.php */
1979 do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version );
1980 Jetpack_Options::update_options(
1981 array(
1982 'version' => $new_version,
1983 'old_version' => $jetpack_old_version,
1984 )
1985 );
1986
1987 self::state( 'message', 'modules_activated' );
1988
1989 self::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules, $redirect );
1990
1991 if ( $redirect ) {
1992 $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page.
1993 if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we're not changing the site.
1994 $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we're not changing the site.
1995 }
1996 wp_safe_redirect( self::admin_url( 'page=' . rawurlencode( $page ) ) );
1997 exit( 0 );
1998 }
1999 }
2000
2001 /**
2002 * List available Jetpack modules. Simply lists .php files in /modules/.
2003 * Make sure to tuck away module "library" files in a sub-directory.
2004 *
2005 * @param bool|string $min_version Only return modules introduced in this version or later. Default is false, do not filter.
2006 * @param bool|string $max_version Only return modules introduced before this version. Default is false, do not filter.
2007 * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection.
2008 * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection.
2009 *
2010 * @return array $modules Array of module slugs
2011 */
2012 public static function get_available_modules( $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) {
2013 return ( new Modules() )->get_available( $min_version, $max_version, $requires_connection, $requires_user_connection );
2014 }
2015
2016 /**
2017 * Get default modules loaded on activation.
2018 *
2019 * @param bool|string $min_version Only return modules introduced in this version or later. Default is false, do not filter.
2020 * @param bool|string $max_version Only return modules introduced before this version. Default is false, do not filter.
2021 * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection.
2022 * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection.
2023 *
2024 * @return array $modules Array of module slugs
2025 */
2026 public static function get_default_modules( $min_version = false, $max_version = false, $requires_connection = null, $requires_user_connection = null ) {
2027 $return = array();
2028
2029 foreach ( self::get_available_modules( $min_version, $max_version, $requires_connection, $requires_user_connection ) as $module ) {
2030 $module_data = self::get_module( $module );
2031
2032 switch ( strtolower( $module_data['auto_activate'] ) ) {
2033 case 'yes':
2034 $return[] = $module;
2035 break;
2036 case 'public':
2037 if ( Jetpack_Options::get_option( 'public' ) ) {
2038 $return[] = $module;
2039 }
2040 break;
2041 case 'no':
2042 default:
2043 break;
2044 }
2045 }
2046 /**
2047 * Filters the array of default modules.
2048 *
2049 * @since 2.5.0
2050 *
2051 * @param array $return Array of default modules.
2052 * @param string $min_version Minimum version number required to use modules.
2053 * @param string $max_version Maximum version number required to use modules.
2054 * @param bool|null $requires_connection Value of the Requires Connection filter.
2055 * @param bool|null $requires_user_connection Value of the Requires User Connection filter.
2056 */
2057 return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version, $requires_connection, $requires_user_connection );
2058 }
2059
2060 /**
2061 * Checks activated modules during auto-activation to determine
2062 * if any of those modules are being deprecated. If so, close
2063 * them out, and add any replacement modules.
2064 *
2065 * Runs at priority 99 by default.
2066 *
2067 * This is run late, so that it can still activate a module if
2068 * the new module is a replacement for another that the user
2069 * currently has active, even if something at the normal priority
2070 * would kibosh everything.
2071 *
2072 * @since 2.6
2073 * @uses jetpack_get_default_modules filter
2074 * @param array $modules Array of Jetpack modules.
2075 * @return array
2076 */
2077 public function handle_deprecated_modules( $modules ) {
2078 $deprecated_modules = array(
2079 'debug' => null, // Closed out and moved to the debugger library.
2080 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2081 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support.
2082 'minileven' => null, // Closed out in 8.3 -- Responsive themes are common now, and so is AMP.
2083 'lazy-images' => null, // Closed out in 12.8 -- WordPress core now has native lazy loading.
2084 'enhanced-distribution' => null, // Closed out in 13.3 -- WP.com is winding down the firehose.
2085 );
2086
2087 // Don't activate SSO if they never completed activating WPCC.
2088 if ( self::is_module_active( 'wpcc' ) ) {
2089 $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' );
2090 if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) {
2091 $deprecated_modules['wpcc'] = null;
2092 }
2093 }
2094
2095 foreach ( $deprecated_modules as $module => $replacement ) {
2096 if ( self::is_module_active( $module ) ) {
2097 self::deactivate_module( $module );
2098 if ( $replacement ) {
2099 $modules[] = $replacement;
2100 }
2101 }
2102 }
2103
2104 return array_unique( $modules );
2105 }
2106
2107 /**
2108 * Checks activated plugins during auto-activation to determine
2109 * if any of those plugins are in the list with a corresponding module
2110 * that is not compatible with the plugin. The module will not be allowed
2111 * to auto-activate.
2112 *
2113 * @since 2.6
2114 * @uses jetpack_get_default_modules filter
2115 * @param array $modules Array of Jetpack modules.
2116 * @return array
2117 */
2118 public function filter_default_modules( $modules ) {
2119
2120 $active_plugins = self::get_active_plugins();
2121
2122 if ( ! empty( $active_plugins ) ) {
2123
2124 // For each module we'd like to auto-activate...
2125 foreach ( $modules as $key => $module ) {
2126 // If there are potential conflicts for it...
2127 if ( ! empty( $this->conflicting_plugins[ $module ] ) ) {
2128 // For each potential conflict...
2129 foreach ( $this->conflicting_plugins[ $module ] as $plugin ) {
2130 // If that conflicting plugin is active...
2131 if ( in_array( $plugin, $active_plugins, true ) ) {
2132 // Remove that item from being auto-activated.
2133 unset( $modules[ $key ] );
2134 }
2135 }
2136 }
2137 }
2138 }
2139
2140 // Special case to convert block setting to a block module.
2141 $block_key = array_search( 'blocks', $modules, true );
2142 if ( $block_key !== false ) { // Only care if 'blocks' made it through the previous filters.
2143 $block_option = get_option( 'jetpack_blocks_disabled', null );
2144 if ( $block_option ) {
2145 unset( $modules[ $block_key ] );
2146 }
2147 }
2148
2149 return $modules;
2150 }
2151
2152 /**
2153 * Extract a module's slug from its full path.
2154 *
2155 * @param string $file Full path to a file.
2156 *
2157 * @return string Module slug.
2158 */
2159 public static function get_module_slug( $file ) {
2160 return ( new Modules() )->get_slug( $file );
2161 }
2162
2163 /**
2164 * Generate a module's path from its slug.
2165 *
2166 * @param string $slug Module slug.
2167 */
2168 public static function get_module_path( $slug ) {
2169 return ( new Modules() )->get_path( $slug );
2170 }
2171
2172 /**
2173 * Load module data from module file. Headers differ from WordPress
2174 * plugin headers to avoid them being identified as standalone
2175 * plugins on the WordPress plugins page.
2176 *
2177 * @param string $module The module slug.
2178 */
2179 public static function get_module( $module ) {
2180 return ( new Modules() )->get( $module );
2181 }
2182
2183 /**
2184 * Like core's get_file_data implementation, but caches the result.
2185 *
2186 * @param string $file Absolute path to the file.
2187 * @param array $headers List of headers, in the format array( 'HeaderKey' => 'Header Name' ).
2188 */
2189 public static function get_file_data( $file, $headers ) {
2190 return ( new Modules() )->get_file_data( $file, $headers );
2191 }
2192
2193 /**
2194 * Return translated module tag.
2195 *
2196 * @param string $tag Tag as it appears in each module heading.
2197 *
2198 * @return mixed
2199 */
2200 public static function translate_module_tag( $tag ) {
2201 return jetpack_get_module_i18n_tag( $tag );
2202 }
2203
2204 /**
2205 * Return module name translation. Uses matching string created in modules/module-headings.php.
2206 *
2207 * @since 3.9.2
2208 *
2209 * @param array $modules Array of Jetpack modules.
2210 *
2211 * @return string|void
2212 */
2213 public static function get_translated_modules( $modules ) {
2214 foreach ( $modules as $index => $module ) {
2215 $i18n_module = jetpack_get_module_i18n( $module['module'] );
2216 if ( isset( $module['name'] ) ) {
2217 $modules[ $index ]['name'] = $i18n_module['name'];
2218 }
2219 if ( isset( $module['description'] ) ) {
2220 $modules[ $index ]['description'] = $i18n_module['description'];
2221 $modules[ $index ]['short_description'] = $i18n_module['description'];
2222 }
2223 if ( isset( $module['module_tags'] ) ) {
2224 $modules[ $index ]['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] );
2225 }
2226 }
2227 return $modules;
2228 }
2229
2230 /**
2231 * Get a list of activated modules as an array of module slugs.
2232 */
2233 public static function get_active_modules() {
2234 return ( new Modules() )->get_active();
2235 }
2236
2237 /**
2238 * Check whether or not a Jetpack module is active.
2239 *
2240 * @param string $module The slug of a Jetpack module.
2241 * @return bool
2242 *
2243 * @static
2244 */
2245 public static function is_module_active( $module ) {
2246 return ( new Modules() )->is_active( $module );
2247 }
2248
2249 /**
2250 * Is slug a valid module.
2251 *
2252 * @param string $module Module slug.
2253 *
2254 * @return bool
2255 */
2256 public static function is_module( $module ) {
2257 return ( new Modules() )->is_module( $module );
2258 }
2259
2260 /**
2261 * Catches PHP errors. Must be used in conjunction with output buffering.
2262 *
2263 * @deprecated since 13.5
2264 * @param bool $catch True to start catching, False to stop.
2265 *
2266 * @static
2267 * @deprecated 13.5
2268 * @see \Automattic\Jetpack\Errors
2269 */
2270 public static function catch_errors( $catch ) {
2271 _deprecated_function( __METHOD__, '13.5' );
2272 // @phan-suppress-next-line PhanDeprecatedClass
2273 return ( new Errors() )->catch_errors( $catch );
2274 }
2275
2276 /**
2277 * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
2278 *
2279 * @deprecated since 13.5
2280 */
2281 public static function catch_errors_on_shutdown() {
2282 _deprecated_function( __METHOD__, '13.5' );
2283 self::state( 'php_errors', self::alias_directories( ob_get_clean() ) );
2284 }
2285
2286 /**
2287 * Rewrite any string to make paths easier to read.
2288 *
2289 * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR
2290 * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR.
2291 *
2292 * @param string $string String to attempt rewrite.
2293 * @return mixed
2294 */
2295 public static function alias_directories( $string ) {
2296 // ABSPATH has a trailing slash.
2297 $string = str_replace( ABSPATH, 'ABSPATH/', $string );
2298 // WP_CONTENT_DIR does not have a trailing slash.
2299 $string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string );
2300
2301 return $string;
2302 }
2303
2304 /**
2305 * Activates default Jetpack modules.
2306 *
2307 * @param null|string $min_version Only return modules introduced in this version or later. Default is false, do not filter.
2308 * @param null|string $max_version Only return modules introduced before this version. Default is false, do not filter.
2309 * @param array $other_modules Other modules to activate.
2310 * @param null|bool $redirect Should there be a redirection after activation.
2311 * @param bool $send_state_messages If a state message should be sent.
2312 * @param bool|null $requires_connection Pass a boolean value to only return modules that require (or do not require) a connection.
2313 * @param bool|null $requires_user_connection Pass a boolean value to only return modules that require (or do not require) a user connection.
2314 *
2315 * @return void
2316 */
2317 public static function activate_default_modules(
2318 $min_version = false,
2319 $max_version = false,
2320 $other_modules = array(),
2321 $redirect = null,
2322 $send_state_messages = null,
2323 $requires_connection = null,
2324 $requires_user_connection = null
2325 ) {
2326 $jetpack = self::init();
2327
2328 if ( $redirect === null ) {
2329 if (
2330 ( defined( 'REST_REQUEST' ) && REST_REQUEST )
2331 ||
2332 ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
2333 ||
2334 ( defined( 'WP_CLI' ) && WP_CLI )
2335 ||
2336 ( defined( 'DOING_CRON' ) && DOING_CRON )
2337 ||
2338 ( defined( 'DOING_AJAX' ) && DOING_AJAX )
2339 ) {
2340 $redirect = false;
2341 } elseif ( is_admin() ) {
2342 $redirect = true;
2343 } else {
2344 $redirect = false;
2345 }
2346 }
2347
2348 if ( $send_state_messages === null ) {
2349 $send_state_messages = current_user_can( 'jetpack_activate_modules' );
2350 }
2351
2352 $modules = self::get_default_modules( $min_version, $max_version, $requires_connection, $requires_user_connection );
2353 $modules = array_merge( $other_modules, $modules );
2354
2355 // Look for standalone plugins and disable if active.
2356
2357 $to_deactivate = array();
2358 foreach ( $modules as $module ) {
2359 if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) {
2360 $to_deactivate[ $module ] = $jetpack->plugins_to_deactivate[ $module ];
2361 }
2362 }
2363
2364 $deactivated = array();
2365 foreach ( $to_deactivate as $module => $deactivate_us ) {
2366 foreach ( $deactivate_us as $i => $deactivate_me ) {
2367 list( $probable_file, $probable_title ) = $deactivate_me;
2368 if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) {
2369 $deactivated[] = "$module:$i";
2370 }
2371 }
2372 }
2373
2374 if ( $deactivated ) {
2375 if ( $send_state_messages ) {
2376 self::state( 'deactivated_plugins', implode( ',', $deactivated ) );
2377 }
2378
2379 if ( $redirect ) {
2380 $url = add_query_arg(
2381 array(
2382 'action' => 'activate_default_modules',
2383 '_wpnonce' => wp_create_nonce( 'activate_default_modules' ),
2384 ),
2385 add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), self::admin_url( 'page=jetpack' ) )
2386 );
2387 wp_safe_redirect( $url );
2388 exit( 0 );
2389 }
2390 }
2391
2392 /**
2393 * Fires before default modules are activated.
2394 *
2395 * @since 1.9.0
2396 *
2397 * @param string $min_version Minimum version number required to use modules.
2398 * @param string $max_version Maximum version number required to use modules.
2399 * @param array $other_modules Array of other modules to activate alongside the default modules.
2400 * @param bool|null $requires_connection Value of the Requires Connection filter.
2401 * @param bool|null $requires_user_connection Value of the Requires User Connection filter.
2402 */
2403 do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules, $requires_connection, $requires_user_connection );
2404
2405 // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating.
2406 if ( $send_state_messages ) {
2407 self::restate();
2408 }
2409
2410 $active = self::get_active_modules();
2411
2412 foreach ( $modules as $module ) {
2413 if ( did_action( "jetpack_module_loaded_$module" ) ) {
2414 $active[] = $module;
2415 self::update_active_modules( $active );
2416 continue;
2417 }
2418
2419 if ( $send_state_messages && in_array( $module, $active, true ) ) {
2420 $module_info = self::get_module( $module );
2421 if ( ! $module_info['deactivate'] ) {
2422 $state = in_array( $module, $other_modules, true ) ? 'reactivated_modules' : 'activated_modules';
2423 $active_state = self::state( $state );
2424 if ( $active_state ) {
2425 $active_state = explode( ',', $active_state );
2426 } else {
2427 $active_state = array();
2428 }
2429 $active_state[] = $module;
2430 self::state( $state, implode( ',', $active_state ) );
2431 }
2432 continue;
2433 }
2434
2435 $file = self::get_module_path( $module );
2436 if ( ! file_exists( $file ) ) {
2437 continue;
2438 }
2439
2440 // we'll override this later if the plugin can be included without fatal error.
2441 if ( $redirect ) {
2442 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
2443 }
2444
2445 if ( $send_state_messages ) {
2446 self::state( 'error', 'module_activation_failed' );
2447 self::state( 'module', $module );
2448 }
2449
2450 ob_start();
2451 require_once $file;
2452
2453 $active[] = $module;
2454
2455 if ( $send_state_messages ) {
2456
2457 $state = in_array( $module, $other_modules, true ) ? 'reactivated_modules' : 'activated_modules';
2458 $active_state = self::state( $state );
2459 if ( $active_state ) {
2460 $active_state = explode( ',', $active_state );
2461 } else {
2462 $active_state = array();
2463 }
2464 $active_state[] = $module;
2465 self::state( $state, implode( ',', $active_state ) );
2466 }
2467
2468 self::update_active_modules( $active );
2469
2470 ob_end_clean();
2471 }
2472
2473 if ( $send_state_messages ) {
2474 self::state( 'error', false );
2475 self::state( 'module', false );
2476 }
2477 /**
2478 * Fires when default modules are activated.
2479 *
2480 * @since 1.9.0
2481 *
2482 * @param string $min_version Minimum version number required to use modules.
2483 * @param string $max_version Maximum version number required to use modules.
2484 * @param array $other_modules Array of other modules to activate alongside the default modules.
2485 * @param bool|null $requires_connection Value of the Requires Connection filter.
2486 * @param bool|null $requires_user_connection Value of the Requires User Connection filter.
2487 */
2488 do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules, $requires_connection, $requires_user_connection );
2489 }
2490
2491 /**
2492 * Activate a module.
2493 *
2494 * @param string $module Module slug.
2495 * @param bool $exit Should exit be called after deactivation.
2496 * @param bool $redirect Should there be a redirection after activation.
2497 *
2498 * @return bool|void
2499 */
2500 public static function activate_module( $module, $exit = true, $redirect = true ) {
2501 return ( new Modules() )->activate( $module, $exit, $redirect );
2502 }
2503
2504 /**
2505 * Deactivate module.
2506 *
2507 * @param string $module Module slug.
2508 *
2509 * @return bool
2510 */
2511 public static function deactivate_module( $module ) {
2512 return ( new Modules() )->deactivate( $module );
2513 }
2514
2515 /**
2516 * Enable a configuable module.
2517 *
2518 * @param string $module Module slug.
2519 *
2520 * @return void
2521 */
2522 public static function enable_module_configurable( $module ) {
2523 $module = self::get_module_slug( $module );
2524 add_filter( 'jetpack_module_configurable_' . $module, '__return_true' );
2525 }
2526
2527 /**
2528 * Composes a module configure URL. It uses Jetpack settings search as default value
2529 * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter
2530 *
2531 * @param string $module Module slug.
2532 * @return string $url module configuration URL.
2533 */
2534 public static function module_configuration_url( $module ) {
2535 $module = self::get_module_slug( $module );
2536 $default_url = self::admin_url() . "#/settings?term=$module";
2537 /**
2538 * Allows to modify configure_url of specific module to be able to redirect to some custom location.
2539 *
2540 * @since 6.9.0
2541 *
2542 * @param string $default_url Default url, which redirects to jetpack settings page.
2543 */
2544 $url = apply_filters( 'jetpack_module_configuration_url_' . $module, $default_url );
2545
2546 return $url;
2547 }
2548
2549 /* Installation */
2550 /**
2551 * Bail on activation if there is an issue.
2552 *
2553 * @param string $message Error message.
2554 * @param bool $deactivate Deactivate Jetpack or not.
2555 *
2556 * @return never
2557 */
2558 public static function bail_on_activation( $message, $deactivate = true ) {
2559 ?>
2560 <!doctype html>
2561 <html>
2562 <head>
2563 <meta charset="<?php bloginfo( 'charset' ); ?>">
2564 <style>
2565 * {
2566 text-align: center;
2567 margin: 0;
2568 padding: 0;
2569 font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
2570 }
2571 p {
2572 margin-top: 1em;
2573 font-size: 18px;
2574 }
2575 </style>
2576 <body>
2577 <p><?php echo esc_html( $message ); ?></p>
2578 </body>
2579 </html>
2580 <?php
2581 if ( $deactivate ) {
2582 $plugins = get_option( 'active_plugins' );
2583 $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' );
2584 $update = false;
2585 foreach ( $plugins as $i => $plugin ) {
2586 if ( $plugin === $jetpack ) {
2587 $plugins[ $i ] = false;
2588 $update = true;
2589 }
2590 }
2591
2592 if ( $update ) {
2593 update_option( 'active_plugins', array_filter( $plugins ) );
2594 }
2595 }
2596 exit( 0 );
2597 }
2598
2599 /**
2600 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
2601 *
2602 * @param bool $network_wide Network-wide activation.
2603 */
2604 public static function plugin_activation( $network_wide ) {
2605 Jetpack_Options::update_option( 'activated', 1 );
2606
2607 if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
2608 /* translators: Jetpack version number. */
2609 self::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) );
2610 }
2611
2612 if ( $network_wide ) {
2613 self::state( 'network_nag', true );
2614 }
2615
2616 // For firing one-off events (notices) immediately after activation.
2617 set_transient( 'activated_jetpack', true, 0.1 * MINUTE_IN_SECONDS );
2618
2619 update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) );
2620
2621 Health::on_jetpack_activated();
2622
2623 if ( self::is_connection_ready() && method_exists( 'Automattic\Jetpack\Sync\Actions', 'do_only_first_initial_sync' ) ) {
2624 Sync_Actions::do_only_first_initial_sync();
2625 }
2626
2627 if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
2628 Woocommerce_Analytics::maybe_add_proxy_speed_module();
2629 }
2630
2631 self::plugin_initialize();
2632 }
2633
2634 /**
2635 * Returns the activation source.
2636 *
2637 * @param string $referer_url URL.
2638 *
2639 * @return array source_type, source_query.
2640 */
2641 public static function get_activation_source( $referer_url ) {
2642 if ( defined( 'WP_CLI' ) && WP_CLI ) {
2643 return array( 'wp-cli', null );
2644 }
2645
2646 $referer = wp_parse_url( $referer_url );
2647
2648 $source_type = 'unknown';
2649 $source_query = null;
2650
2651 if ( ! is_array( $referer ) || ! isset( $referer['path'] ) ) {
2652 return array( $source_type, $source_query );
2653 }
2654
2655 $plugins_path = wp_parse_url( admin_url( 'plugins.php' ), PHP_URL_PATH );
2656 $plugins_install_path = wp_parse_url( admin_url( 'plugin-install.php' ), PHP_URL_PATH );// /wp-admin/plugin-install.php
2657
2658 if ( isset( $referer['query'] ) ) {
2659 parse_str( $referer['query'], $query_parts );
2660 } else {
2661 $query_parts = array();
2662 }
2663
2664 if ( $plugins_path === $referer['path'] ) {
2665 $source_type = 'list';
2666 } elseif ( $plugins_install_path === $referer['path'] ) {
2667 $tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured';
2668 switch ( $tab ) {
2669 case 'popular':
2670 $source_type = 'popular';
2671 break;
2672 case 'recommended':
2673 $source_type = 'recommended';
2674 break;
2675 case 'favorites':
2676 $source_type = 'favorites';
2677 break;
2678 case 'search':
2679 $source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' );
2680 $source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null;
2681 break;
2682 default:
2683 $source_type = 'featured';
2684 }
2685 }
2686
2687 return array( $source_type, $source_query );
2688 }
2689
2690 /**
2691 * Runs before bumping version numbers up to a new version
2692 *
2693 * @param string $version Version:timestamp.
2694 * @param string $old_version Old Version:timestamp or false if not set yet.
2695 */
2696 public static function do_version_bump( $version, $old_version ) {
2697 if ( $old_version ) { // For existing Jetpack installations.
2698 add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
2699
2700 // If a front end page is visited after the update, the 'wp' action will fire.
2701 add_action( 'wp', 'Jetpack::set_update_modal_display' );
2702
2703 // If an admin page is visited after the update, the 'current_screen' action will fire.
2704 add_action( 'current_screen', 'Jetpack::set_update_modal_display' );
2705 }
2706 }
2707
2708 /**
2709 * Sets the display_update_modal state.
2710 */
2711 public static function set_update_modal_display() {
2712 self::state( 'display_update_modal', true );
2713 }
2714
2715 /**
2716 * Enqueues the block library styles.
2717 *
2718 * @param string $hook The current admin page.
2719 */
2720 public static function enqueue_block_style( $hook ) {
2721 if ( 'toplevel_page_jetpack' === $hook ) {
2722 wp_enqueue_style( 'wp-block-library' );
2723 }
2724 }
2725
2726 /**
2727 * Sets the internal version number and activation state.
2728 *
2729 * @static
2730 */
2731 public static function plugin_initialize() {
2732 if ( ! Jetpack_Options::get_option( 'activated' ) ) {
2733 Jetpack_Options::update_option( 'activated', 2 );
2734 }
2735
2736 if ( ! Jetpack_Options::get_option( 'version' ) ) {
2737 $old_version = JETPACK__VERSION . ':' . time();
2738 $version = $old_version;
2739 /** This action is documented in class.jetpack.php */
2740 do_action( 'updating_jetpack_version', $version, false );
2741 Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
2742 }
2743
2744 if ( self::is_connection_ready() ) {
2745 self::handle_default_module_activation( true );
2746 }
2747
2748 self::load_modules();
2749
2750 Jetpack_Options::delete_option( 'do_activate' );
2751 }
2752
2753 /**
2754 * Handles the activation of the default modules depending on the current state of the site:
2755 * - If the site already has the jetpack_active_modules option, activate those.
2756 * - If the site has a site-only connection, only activate the default modules that require only a site connection.
2757 * - If the site has a user connection, activate the default modules that require a user connection.
2758 *
2759 * @param bool $should_activate_user_modules Whether the status of the user connection should be checked and the default modules that
2760 * require a user connection activated.
2761 */
2762 private static function handle_default_module_activation( $should_activate_user_modules ) {
2763 $active_modules = Jetpack_Options::get_option( 'active_modules' );
2764 if ( $active_modules ) {
2765 self::delete_active_modules();
2766
2767 /**
2768 * Previously active modules could mean two things. First, it could mean
2769 * that Jetpack was previously active on the site. In this case we would like
2770 * to only activate the modules that were set to active.
2771 * Another case could be that the module option was set by a standalone
2772 * plugin. In that case the `active_modules_initalized` option will not
2773 * be set, so we need to enable default Jetpack modules as well.
2774 */
2775 if ( ! Jetpack_Options::get_option( 'active_modules_initialized' ) ) {
2776 $default_modules = self::get_default_modules();
2777 $active_modules = array_merge( $active_modules, $default_modules );
2778 Jetpack_Options::update_option( 'active_modules_initialized', true );
2779 }
2780
2781 self::activate_default_modules(
2782 999, // This version trick basically excludes every default module.
2783 1,
2784 $active_modules,
2785 false
2786 );
2787 } elseif ( $should_activate_user_modules && ( new Connection_Manager() )->get_connection_owner_id() ) { // Check for a user connection.
2788 self::activate_default_modules( false, false, array(), false, null, null, null );
2789 Jetpack_Options::update_option( 'active_modules_initialized', true );
2790 } else {
2791 self::activate_default_modules( false, false, array(), false, null, null, false );
2792 }
2793 }
2794
2795 /**
2796 * Removes all connection options
2797 *
2798 * @static
2799 */
2800 public static function plugin_deactivation() {
2801 require_once ABSPATH . '/wp-admin/includes/plugin.php';
2802 $tracking = new Tracking();
2803 $tracking->record_user_event( 'deactivate_plugin', array() );
2804 if ( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
2805 Jetpack_Network::init()->deactivate();
2806 } else {
2807 add_filter( 'jetpack_update_activated_state_on_disconnect', '__return_false' );
2808 self::disconnect();
2809 Jetpack_Options::delete_option( 'version' );
2810 }
2811
2812 if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
2813 Woocommerce_Analytics::maybe_remove_proxy_speed_module();
2814 }
2815 }
2816
2817 /**
2818 * Set activated option to 4 on jetpack_idc_disconnect action.
2819 */
2820 public static function on_idc_disconnect() {
2821 \Jetpack_Options::update_option( 'activated', 4 );
2822 }
2823
2824 /**
2825 * Disconnects from the Jetpack servers.
2826 * Forgets all connection details and tells the Jetpack servers to do the same.
2827 *
2828 * Will not disconnect if there are other plugins using the connection.
2829 *
2830 * @since 11.0 Do not disconnect if other plugins are using the connection.
2831 *
2832 * @static
2833 */
2834 public static function disconnect() {
2835
2836 $connection = self::connection();
2837
2838 // If the site is in an IDC because sync is not allowed,
2839 // let's make sure to not disconnect the production site.
2840 $connection->remove_connection( ! Identity_Crisis::validate_sync_error_idc_option() );
2841 }
2842
2843 /**
2844 * Happens after a successful disconnection.
2845 *
2846 * @static
2847 */
2848 public static function jetpack_site_disconnected() {
2849 Identity_Crisis::clear_all_idc_options();
2850
2851 // Delete all the sync related data. Since it could be taking up space.
2852 Sender::get_instance()->uninstall();
2853
2854 /**
2855 * Filters whether the Jetpack activated state should be updated after disconnecting.
2856 *
2857 * @since 10.0.0
2858 *
2859 * @param bool $update_activated_state Whether activated state should be updated after disconnecting, defaults to true.
2860 */
2861 $update_activated_state = apply_filters( 'jetpack_update_activated_state_on_disconnect', true );
2862
2863 if ( $update_activated_state ) {
2864 Jetpack_Options::update_option( 'activated', 4 );
2865 }
2866 }
2867
2868 /**
2869 * Disconnects the user.
2870 *
2871 * @deprecated 13.4
2872 * @see \Automattic\Jetpack\Connection\Manager::disconnect_user()
2873 *
2874 * @param int $user_id The user ID to disconnect.
2875 */
2876 public function disconnect_user( $user_id ) {
2877 $this->connection_manager->disconnect_user( $user_id );
2878 }
2879
2880 /**
2881 * Checking the domain names in beta versions.
2882 * If this is a development version, before attempting to connect, let's make sure that the domains are viable.
2883 *
2884 * @param null|\WP_Error $error The domain validation error, or `null` if everything's fine.
2885 *
2886 * @return null|\WP_Error The domain validation error, or `null` if everything's fine.
2887 */
2888 public static function registration_check_domains( $error ) {
2889 if ( static::is_development_version() ) {
2890 $domains_to_check = array_unique(
2891 array(
2892 'siteurl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
2893 'homeurl' => wp_parse_url( get_home_url(), PHP_URL_HOST ),
2894 )
2895 );
2896 foreach ( $domains_to_check as $domain ) {
2897 $result = static::connection()->is_usable_domain( $domain );
2898 if ( is_wp_error( $result ) ) {
2899 return $result;
2900 }
2901 }
2902 }
2903
2904 return $error;
2905 }
2906
2907 /**
2908 * Tracking an internal event log. Try not to put too much chaff in here.
2909 *
2910 * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA)
2911 *
2912 * @param mixed $code Error code to log.
2913 * @param mixed $data Data to log.
2914 */
2915 public static function log( $code, $data = null ) {
2916
2917 $raw_log = Jetpack_Options::get_option( 'log', array() );
2918 // This can be modified by the `jetpack_options` filter, so abort if we don't have an array.
2919 if ( ! is_array( $raw_log ) ) {
2920 return;
2921 }
2922
2923 // only grab the latest 200 entries.
2924 $log = array_slice( $raw_log, -199, 199 );
2925
2926 // Append our event to the log.
2927 $log_entry = array(
2928 'time' => time(),
2929 'user_id' => get_current_user_id(),
2930 'blog_id' => Jetpack_Options::get_option( 'id' ),
2931 'code' => $code,
2932 );
2933 // Don't bother storing it unless we've got some.
2934 if ( $data !== null ) {
2935 $log_entry['data'] = $data;
2936 }
2937 $log[] = $log_entry;
2938
2939 // Try add_option first, to make sure it's not autoloaded.
2940 // @todo: Add an add_option method to Jetpack_Options.
2941 if ( ! add_option( 'jetpack_log', $log, '', 'no' ) ) {
2942 Jetpack_Options::update_option( 'log', $log );
2943 }
2944
2945 /**
2946 * Fires when Jetpack logs an internal event.
2947 *
2948 * @since 3.0.0
2949 *
2950 * @param array $log_entry {
2951 * Array of details about the log entry.
2952 *
2953 * @param string time Time of the event.
2954 * @param int user_id ID of the user who trigerred the event.
2955 * @param int blog_id Jetpack Blog ID.
2956 * @param string code Unique name for the event.
2957 * @param string data Data about the event.
2958 * }
2959 */
2960 do_action( 'jetpack_log_entry', $log_entry );
2961 }
2962
2963 /**
2964 * Get the internal event log.
2965 *
2966 * @param string $event only return the specific log events.
2967 * @param int $num - get specific number of latest results, limited to 200.
2968 *
2969 * @return array of log events || WP_Error for invalid params
2970 */
2971 public static function get_log( $event = false, $num = false ) {
2972 if ( $event && ! is_string( $event ) ) {
2973 return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) );
2974 }
2975
2976 if ( $num && ! is_numeric( $num ) ) {
2977 return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) );
2978 }
2979
2980 $entire_log = Jetpack_Options::get_option( 'log', array() );
2981
2982 // If nothing set - act as it did before, otherwise let's start customizing the output.
2983 if ( ! $num && ! $event ) {
2984 return $entire_log;
2985 } else {
2986 $entire_log = array_reverse( $entire_log );
2987 }
2988
2989 $custom_log_output = array();
2990
2991 if ( $event ) {
2992 foreach ( $entire_log as $log_event ) {
2993 if ( $event === $log_event['code'] ) {
2994 $custom_log_output[] = $log_event;
2995 }
2996 }
2997 } else {
2998 $custom_log_output = $entire_log;
2999 }
3000
3001 if ( $num ) {
3002 $custom_log_output = array_slice( $custom_log_output, 0, $num );
3003 }
3004
3005 return $custom_log_output;
3006 }
3007
3008 /**
3009 * Log modification of important settings.
3010 *
3011 * @param string $option Option name.
3012 * @param string $old_value Old value of option.
3013 * @param string $value New value of option.
3014 */
3015 public static function log_settings_change( $option, $old_value, $value ) {
3016 switch ( $option ) {
3017 case 'jetpack_sync_non_public_post_stati':
3018 self::log( $option, $value );
3019 break;
3020 }
3021 }
3022
3023 /**
3024 * Return stat data for WPCOM sync.
3025 *
3026 * @param bool $encode JSON encode the result.
3027 * @param bool $extended Adds additional stats data.
3028 *
3029 * @return array|string Stats data. Array if $encode is false. JSON-encoded string is $encode is true.
3030 */
3031 public static function get_stat_data( $encode = true, $extended = true ) {
3032 $data = Jetpack_Heartbeat::generate_stats_array();
3033
3034 if ( $extended ) {
3035 $additional_data = self::get_additional_stat_data();
3036 $data = array_merge( $data, $additional_data );
3037 }
3038
3039 if ( $encode ) {
3040 return wp_json_encode( $data );
3041 }
3042
3043 return $data;
3044 }
3045
3046 /**
3047 * Get additional stat data to sync to WPCOM
3048 *
3049 * @param string $prefix Stats prefix.
3050 *
3051 * @return array stats values.
3052 */
3053 public static function get_additional_stat_data( $prefix = '' ) {
3054 $return = array();
3055 $return[ "{$prefix}themes" ] = self::get_parsed_theme_data();
3056 $return[ "{$prefix}plugins-extra" ] = self::get_parsed_plugin_data();
3057 $return[ "{$prefix}users" ] = (int) self::get_site_user_count();
3058 $return[ "{$prefix}site-count" ] = 0;
3059
3060 if ( function_exists( 'get_blog_count' ) ) {
3061 $return[ "{$prefix}site-count" ] = get_blog_count();
3062 }
3063 return $return;
3064 }
3065
3066 /**
3067 * Get current site's user count.
3068 *
3069 * @return int|string|null Number of users on the site. -1 for a large network.
3070 */
3071 private static function get_site_user_count() {
3072 global $wpdb;
3073
3074 if ( function_exists( 'wp_is_large_network' ) ) {
3075 if ( wp_is_large_network( 'users' ) ) {
3076 return -1; // Not a real value but should tell us that we are dealing with a large network.
3077 }
3078 }
3079 $user_count = get_transient( 'jetpack_site_user_count' );
3080 if ( false === ( $user_count ) ) {
3081 // It wasn't there, so regenerate the data and save the transient.
3082 $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities'" );
3083 set_transient( 'jetpack_site_user_count', $user_count, DAY_IN_SECONDS );
3084 }
3085 return $user_count;
3086 }
3087
3088 /* Admin Pages */
3089
3090 /**
3091 * Admin init function.
3092 *
3093 * Runs on admin_init hook.
3094 *
3095 * @return void
3096 */
3097 public function admin_init() {
3098 // If the plugin is not connected, display a connect message.
3099 if (
3100 // the plugin was auto-activated and needs its candy.
3101 Jetpack_Options::get_option_and_ensure_autoload( 'do_activate', '0' )
3102 ||
3103 // the plugin is active, but was never activated. Probably came from a site-wide network activation.
3104 ! Jetpack_Options::get_option( 'activated' )
3105 ) {
3106 self::plugin_initialize();
3107 }
3108
3109 $is_offline_mode = ( new Status() )->is_offline_mode();
3110 $fallback_no_verify_ssl_certs = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' );
3111 /** Already documented in automattic/jetpack-connection::src/class-client.php */
3112 $client_verify_ssl_certs = apply_filters( 'jetpack_client_verify_ssl_certs', false );
3113
3114 // Run post-activation actions if needed.
3115 $this->plugin_post_activation();
3116
3117 if ( ( self::is_connection_ready() || $is_offline_mode ) && false === $fallback_no_verify_ssl_certs && ! $client_verify_ssl_certs ) {
3118 // Upgrade: 1.1 -> 1.1.1
3119 // Check and see if host can verify the Jetpack servers' SSL certificate.
3120 $args = array();
3121 Client::_wp_remote_request( self::connection()->api_url( 'test' ), $args, true );
3122 }
3123
3124 if (
3125 current_user_can( 'manage_options' )
3126 && ! self::permit_ssl()
3127 && ! $is_offline_mode
3128 ) {
3129 add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
3130 }
3131
3132 add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
3133 add_action( 'load-plugins.php', array( $this, 'plugins_page_init_jetpack_state' ) );
3134
3135 if ( ! ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) && ! is_network_admin() ) ) {
3136 add_action( 'admin_enqueue_scripts', array( $this, 'deactivate_dialog' ) );
3137 }
3138
3139 if ( isset( $_COOKIE['jetpackState']['display_update_modal'] ) ) {
3140 add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
3141 }
3142
3143 add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) );
3144
3145 if ( self::is_connection_ready() || $is_offline_mode ) {
3146 // Artificially throw errors in certain specific cases during plugin activation.
3147 add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
3148 }
3149 }
3150
3151 /**
3152 * Adds body classes.
3153 *
3154 * @param string $admin_body_class Body classes.
3155 *
3156 * @return string
3157 */
3158 public function admin_body_class( $admin_body_class = '' ) {
3159 $classes = explode( ' ', trim( $admin_body_class ) );
3160
3161 $classes[] = self::is_connection_ready() ? 'jetpack-connected' : 'jetpack-disconnected';
3162
3163 $admin_body_class = implode( ' ', array_unique( $classes ) );
3164 return " $admin_body_class ";
3165 }
3166
3167 /**
3168 * Adds Jetpack Page styles by appending class to the admin body class.
3169 *
3170 * @param string $admin_body_class Existing admin body class string.
3171 *
3172 * @return string
3173 */
3174 public static function add_jetpack_pagestyles( $admin_body_class = '' ) {
3175 return $admin_body_class . ' jetpack-pagestyles ';
3176 }
3177
3178 /**
3179 * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
3180 * This function artificially throws errors for such cases (per a specific list).
3181 *
3182 * @param string $plugin The activated plugin.
3183 * @throws RuntimeException If a conflicting plugin is detected.
3184 */
3185 public function throw_error_on_activate_plugin( $plugin ) {
3186 $active_modules = self::get_active_modules();
3187
3188 // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks.
3189 if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules, true ) ) {
3190 $throw = false;
3191
3192 // Try and make sure it really was the stats plugin.
3193 if ( ! class_exists( 'ReflectionFunction' ) ) {
3194 if ( 'stats.php' === basename( $plugin ) ) {
3195 $throw = true;
3196 }
3197 } else {
3198 // @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- Checked above. See also https://github.com/phan/phan/issues/1204.
3199 $reflection = new ReflectionFunction( 'stats_get_api_key' );
3200 if ( basename( $plugin ) === basename( $reflection->getFileName() ) ) {
3201 $throw = true;
3202 }
3203 }
3204
3205 if ( $throw ) {
3206 /* translators: Plugin name to deactivate. */
3207 throw new RuntimeException( sprintf( __( 'Jetpack contains the most recent version of the old "%1$s" plugin.', 'jetpack' ), 'WordPress.com Stats' ) );
3208 }
3209 }
3210 }
3211
3212 /**
3213 * Call to Jetpack::state on the load-plugins.php hook.
3214 * In case the jetpackState cookie is populated, this call will read and re-set the cookie before HTTP headers are sent.
3215 */
3216 public function plugins_page_init_jetpack_state() {
3217 self::state( 'message' );
3218 }
3219
3220 /**
3221 * Adds the intercept action to the check_admin_referer hook.
3222 *
3223 * @return void
3224 */
3225 public function intercept_plugin_error_scrape_init() {
3226 add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 );
3227 }
3228
3229 /**
3230 * Detect if conflicting plugin is being deactivated.
3231 *
3232 * @param string $action The nonce action.
3233 * @param bool|int $result False if the nonce is invalid, 1 if the nonce is valid and generated between 0-12 hours ago, 2 if the nonce is valid and generated between 12-24 hours ago.
3234 *
3235 * @return void
3236 */
3237 public function intercept_plugin_error_scrape( $action, $result ) {
3238 if ( ! $result ) {
3239 return;
3240 }
3241
3242 foreach ( $this->plugins_to_deactivate as $deactivate_us ) {
3243 foreach ( $deactivate_us as $deactivate_me ) {
3244 if ( "plugin-activation-error_{$deactivate_me[0]}" === $action ) {
3245 /* translators: Plugin name to deactivate. */
3246 self::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), $deactivate_me[1] ), false );
3247 }
3248 }
3249 }
3250 }
3251
3252 /**
3253 * Register the remote file upload request handlers, if needed.
3254 *
3255 * @access public
3256 */
3257 public function add_remote_request_handlers() {
3258 // Remote file uploads are allowed only via AJAX requests.
3259 if ( ! is_admin() || ! Constants::get_constant( 'DOING_AJAX' ) ) {
3260 return;
3261 }
3262
3263 // Remote file uploads are allowed only for a set of specific AJAX actions.
3264 $remote_request_actions = array(
3265 'jetpack_upload_file',
3266 'jetpack_update_file',
3267 );
3268
3269 // phpcs:ignore WordPress.Security.NonceVerification
3270 if ( ! isset( $_POST['action'] ) || ! in_array( $_POST['action'], $remote_request_actions, true ) ) {
3271 return;
3272 }
3273
3274 // Require Jetpack authentication for the remote file upload AJAX requests.
3275 if ( ! $this->connection_manager ) {
3276 $this->connection_manager = new Connection_Manager();
3277 }
3278
3279 $this->connection_manager->require_jetpack_authentication();
3280
3281 // Register the remote file upload AJAX handlers.
3282 foreach ( $remote_request_actions as $action ) {
3283 add_action( "wp_ajax_nopriv_{$action}", array( $this, 'remote_request_handlers' ) );
3284 }
3285 }
3286
3287 /**
3288 * Handler for Jetpack remote file uploads.
3289 *
3290 * @access public
3291 * @return never
3292 */
3293 public function remote_request_handlers() {
3294 switch ( current_filter() ) {
3295 case 'wp_ajax_nopriv_jetpack_upload_file':
3296 $response = $this->upload_handler();
3297 break;
3298
3299 case 'wp_ajax_nopriv_jetpack_update_file':
3300 $response = $this->upload_handler( true );
3301 break;
3302 default:
3303 $response = new WP_Error( 'unknown_handler', 'Unknown Handler', 400 );
3304 break;
3305 }
3306
3307 if ( ! $response ) {
3308 $response = new WP_Error( 'unknown_error', 'Unknown Error', 400 );
3309 }
3310
3311 if ( is_wp_error( $response ) ) {
3312 $status_code = $response->get_error_data();
3313 $error = $response->get_error_code();
3314 $error_description = $response->get_error_message();
3315
3316 if ( ! is_int( $status_code ) ) {
3317 $status_code = 400;
3318 }
3319
3320 status_header( $status_code );
3321 die( wp_json_encode( (object) compact( 'error', 'error_description' ) ) );
3322 }
3323
3324 status_header( 200 );
3325 if ( true === $response ) {
3326 exit( 0 );
3327 }
3328
3329 die( wp_json_encode( (object) $response ) );
3330 }
3331
3332 /**
3333 * Uploads a file gotten from the global $_FILES.
3334 * If `$update_media_item` is true and `post_id` is defined
3335 * the attachment file of the media item (gotten through of the post_id)
3336 * will be updated instead of add a new one.
3337 *
3338 * @param boolean $update_media_item - update media attachment.
3339 * @return array|WP_Error - An array describing the uploading files process.
3340 */
3341 public function upload_handler( $update_media_item = false ) {
3342 if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' !== strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) ) {
3343 return new WP_Error( 405, get_status_header_desc( 405 ), 405 );
3344 }
3345
3346 $user = wp_authenticate( '', '' );
3347 if ( ! $user || is_wp_error( $user ) ) {
3348 return new WP_Error( 403, get_status_header_desc( 403 ), 403 );
3349 }
3350
3351 wp_set_current_user( $user->ID );
3352
3353 if ( ! current_user_can( 'upload_files' ) ) {
3354 return new WP_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 );
3355 }
3356
3357 if ( empty( $_FILES ) ) {
3358 return new WP_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 );
3359 }
3360
3361 foreach ( array_keys( $_FILES ) as $files_key ) {
3362 if ( ! isset( $_POST[ "_jetpack_file_hmac_{$files_key}" ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- no site changes here.
3363 return new WP_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 );
3364 }
3365 }
3366
3367 $media_keys = isset( $_FILES['media'] ) ? array_keys( $_FILES['media'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Unslash is not needed for `$_FILES`, the sniff is wrong. Sanitization should happen below.
3368
3369 $token = ( new Tokens() )->get_access_token( get_current_user_id() );
3370 if ( ! $token || is_wp_error( $token ) ) {
3371 return new WP_Error( 'unknown_token', 'Unknown Jetpack token', 403 );
3372 }
3373
3374 /**
3375 * Optionally block uploads processed through Jetpack's upload_handler().
3376 * The filter may return false or WP_Error to block this particular upload.
3377 *
3378 * @since 10.8
3379 *
3380 * @param bool|WP_Error $allowed If false or WP_Error, block the upload. If true, allow the upload.
3381 * @param mixed $_FILES The $_FILES attempting to be uploaded.
3382 */
3383 $can_upload = apply_filters( 'jetpack_upload_handler_can_upload', true, $_FILES );
3384 if ( ! $can_upload || is_wp_error( $can_upload ) ) {
3385 if ( is_wp_error( $can_upload ) ) {
3386 return $can_upload;
3387 }
3388 return new WP_Error( 'handler_cannot_upload', __( 'The upload handler cannot upload files', 'jetpack' ), 400 );
3389 }
3390
3391 $uploaded_files = array();
3392 $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
3393 unset( $GLOBALS['post'] );
3394 if ( empty( $_FILES['media']['name'] ) ) {
3395 // Nothing to process, just return.
3396 return $uploaded_files;
3397 }
3398 foreach ( $_FILES['media']['name'] as $index => $name ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- As above, unslash sniff is wrong. Validation should happen below.
3399 $file = array();
3400 foreach ( $media_keys as $media_key ) {
3401 $file[ $media_key ] = isset( $_FILES['media'][ $media_key ][ $index ] ) ? $_FILES['media'][ $media_key ][ $index ] : null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- As above, the unslash sniff is wrong.
3402 }
3403
3404 list( $hmac_provided, $salt ) = isset( $_POST['_jetpack_file_hmac_media'][ $index ] ) ? explode( ':', filter_var( wp_unslash( $_POST['_jetpack_file_hmac_media'][ $index ] ) ) ) : array( 'no', '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce should have been checked by the caller.
3405
3406 $hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret );
3407 if ( $hmac_provided !== $hmac_file ) {
3408 $uploaded_files[ $index ] = (object) array(
3409 'error' => 'invalid_hmac',
3410 'error_description' => 'The corresponding HMAC for this file does not match',
3411 );
3412 continue;
3413 }
3414
3415 $_FILES['.jetpack.upload.'] = $file;
3416 $post_id = isset( $_POST['post_id'][ $index ] ) ? absint( $_POST['post_id'][ $index ] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- caller should have checked a nonce.
3417 if ( ! current_user_can( 'edit_post', $post_id ) ) {
3418 $post_id = 0;
3419 }
3420
3421 if ( $update_media_item ) {
3422 if ( ! isset( $post_id ) || 0 === $post_id ) {
3423 return new WP_Error( 'invalid_input', 'Media ID must be defined.', 400 );
3424 }
3425
3426 $media_array = $_FILES['media']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
3427
3428 $file_array = array();
3429 $file_array['name'] = $media_array['name'][0];
3430 $file_array['type'] = $media_array['type'][0];
3431 $file_array['tmp_name'] = $media_array['tmp_name'][0];
3432 $file_array['error'] = $media_array['error'][0];
3433 $file_array['size'] = $media_array['size'][0];
3434
3435 $edited_media_item = Jetpack_Media::edit_media_file( $post_id, $file_array );
3436
3437 if ( is_wp_error( $edited_media_item ) ) {
3438 return $edited_media_item;
3439 }
3440
3441 $response = (object) array(
3442 'id' => (string) $post_id,
3443 'file' => (string) $edited_media_item->post_title,
3444 'url' => (string) wp_get_attachment_url( $post_id ),
3445 'type' => (string) $edited_media_item->post_mime_type,
3446 'meta' => (array) wp_get_attachment_metadata( $post_id ),
3447 );
3448
3449 return array( $response );
3450 }
3451
3452 $attachment_id = media_handle_upload(
3453 '.jetpack.upload.',
3454 $post_id,
3455 array(),
3456 array(
3457 'action' => 'jetpack_upload_file',
3458 )
3459 );
3460
3461 if ( ! $attachment_id ) {
3462 $uploaded_files[ $index ] = (object) array(
3463 'error' => 'unknown',
3464 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site',
3465 );
3466 } elseif ( is_wp_error( $attachment_id ) ) {
3467 $uploaded_files[ $index ] = (object) array(
3468 'error' => 'attachment_' . $attachment_id->get_error_code(),
3469 'error_description' => $attachment_id->get_error_message(),
3470 );
3471 } else {
3472 $attachment = get_post( $attachment_id );
3473 $uploaded_files[ $index ] = (object) array(
3474 'id' => (string) $attachment_id,
3475 'file' => $attachment->post_title,
3476 'url' => wp_get_attachment_url( $attachment_id ),
3477 'type' => $attachment->post_mime_type,
3478 'meta' => wp_get_attachment_metadata( $attachment_id ),
3479 );
3480 }
3481 }
3482 if ( $global_post !== null ) {
3483 $GLOBALS['post'] = $global_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
3484 }
3485
3486 return $uploaded_files;
3487 }
3488
3489 /**
3490 * Add action links for the Jetpack plugin.
3491 *
3492 * @param array $actions Plugin actions.
3493 *
3494 * @return array
3495 */
3496 public function plugin_action_links( $actions ) {
3497 if ( current_user_can( 'jetpack_manage_modules' ) && ( self::is_connection_ready() || ( new Status() )->is_offline_mode() ) ) {
3498 return array_merge(
3499 array( 'settings' => sprintf( '<a href="%s">%s</a>', esc_url( self::admin_url( 'page=jetpack#/settings' ) ), __( 'Settings', 'jetpack' ) ) ),
3500 $actions
3501 );
3502 }
3503
3504 return $actions;
3505 }
3506
3507 /**
3508 * Adds the deactivation warning modal for Jetpack.
3509 *
3510 * @param string $hook The current admin page.
3511 *
3512 * @return void
3513 */
3514 public function deactivate_dialog( $hook ) {
3515 if (
3516 'plugins.php' === $hook
3517 && self::is_connection_ready()
3518 ) {
3519
3520 // Register jp-tracks-functions dependency.
3521 Tracking::register_tracks_functions_scripts( true );
3522
3523 // add a deactivation script that will pick up deactivation actions for the Jetpack plugin.
3524 Assets::register_script(
3525 'jetpack-plugins-page-js',
3526 '_inc/build/plugins-page.js',
3527 JETPACK__PLUGIN_FILE,
3528 array(
3529 'in_footer' => true,
3530 'textdomain' => 'jetpack',
3531 )
3532 );
3533 Assets::enqueue_script( 'jetpack-plugins-page-js' );
3534
3535 // Add objects to be passed to the initial state of the app.
3536 // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
3537 wp_add_inline_script( 'jetpack-plugins-page-js', 'var Initial_State=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( Jetpack_Redux_State_Helper::get_minimal_state() ) ) . '"));', 'before' );
3538
3539 add_action( 'admin_footer', array( $this, 'jetpack_plugin_portal_containers' ) );
3540 }
3541 }
3542
3543 /**
3544 * Outputs the wrapper for the plugin modal
3545 * Contents are loaded by React script
3546 *
3547 * @return void
3548 */
3549 public function jetpack_plugin_portal_containers() {
3550 $this->load_view( 'admin/jetpack-plugin-portal-containers.php' );
3551 }
3552
3553 /**
3554 * Filters the login URL to include the registration flow in case the user isn't logged in.
3555 *
3556 * @param string $login_url The wp-login URL.
3557 * @param string $redirect URL to redirect users after logging in.
3558 * @since Jetpack 8.4
3559 * @return string
3560 */
3561 public function login_url( $login_url, $redirect ) {
3562 parse_str( (string) wp_parse_url( $redirect, PHP_URL_QUERY ), $redirect_parts );
3563 if ( ! empty( $redirect_parts[ self::$jetpack_redirect_login ] ) ) {
3564 $login_url = add_query_arg( self::$jetpack_redirect_login, 'true', $login_url );
3565 }
3566 return $login_url;
3567 }
3568
3569 /**
3570 * Redirects non-authenticated users to authenticate with Calypso if redirect flag is set.
3571 *
3572 * @since Jetpack 8.4
3573 */
3574 public function login_init() {
3575 // phpcs:ignore WordPress.Security.NonceVerification
3576 if ( ! empty( $_GET[ self::$jetpack_redirect_login ] ) ) {
3577 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
3578 wp_safe_redirect(
3579 add_query_arg(
3580 array(
3581 'forceInstall' => 1,
3582 'url' => rawurlencode( get_site_url() ),
3583 ),
3584 // @todo provide way to go to specific calypso env.
3585 self::get_calypso_host() . 'jetpack/connect'
3586 )
3587 );
3588 exit( 0 );
3589 }
3590 }
3591
3592 /*
3593 * Registration flow:
3594 * 1 - ::admin_page_load() action=register
3595 * 2 - ::try_registration()
3596 * 3 - ::register()
3597 * - Creates jetpack_register option containing two secrets and a timestamp
3598 * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with
3599 * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id
3600 * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's
3601 * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1
3602 * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2
3603 * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with
3604 * jetpack_id, jetpack_secret, jetpack_public
3605 * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret
3606 * 4 - redirect to https://wordpress.com/start/jetpack-connect
3607 * 5 - user logs in with WP.com account
3608 * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize
3609 * - Manager::authorize()
3610 * - Manager::get_token()
3611 * - GET https://jetpack.wordpress.com/jetpack.token/1/ with
3612 * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login
3613 * - which responds with access_token, token_type, scope
3614 * - Manager::authorize() stores jetpack_options: user_token => access_token.$user_id
3615 * - Jetpack::activate_default_modules()
3616 * - Deactivates deprecated plugins
3617 * - Activates all default modules
3618 * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users
3619 * 7 - For a new connection, user selects a Jetpack plan on wordpress.com
3620 * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized
3621 * Done!
3622 */
3623
3624 /**
3625 * Handles the page load events for the Jetpack admin page
3626 */
3627 public function admin_page_load() {
3628 $error = false;
3629
3630 // Make sure we have the right body class to hook stylings for subpages off of.
3631 add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ), 20 );
3632
3633 if ( ! empty( $_GET['jetpack_restate'] ) ) {
3634 // Should only be used in intermediate redirects to preserve state across redirects.
3635 self::restate();
3636 }
3637
3638 if ( isset( $_GET['action'] ) ) {
3639 switch ( $_GET['action'] ) {
3640 /**
3641 * Cases authorize and authorize_redirect are now handled by Connection package Webhooks
3642 */
3643 case 'authorize_redirect':
3644 case 'authorize':
3645 break;
3646 case 'register':
3647 if ( ! current_user_can( 'jetpack_connect' ) ) {
3648 $error = 'cheatin';
3649 break;
3650 }
3651 check_admin_referer( 'jetpack-register' );
3652 self::log( 'register' );
3653 self::maybe_set_version_option();
3654 $from = isset( $_GET['from'] ) ? sanitize_text_field( wp_unslash( $_GET['from'] ) ) : false;
3655 if ( $from ) {
3656 static::connection()->add_register_request_param( 'from', (string) $from );
3657 }
3658 $registered = static::connection()->try_registration();
3659 if ( is_wp_error( $registered ) ) {
3660 $error = $registered->get_error_code();
3661 self::state( 'error', $error );
3662 self::state( 'error', $registered->get_error_message() );
3663
3664 /**
3665 * Jetpack registration Error.
3666 *
3667 * @since 7.5.0
3668 *
3669 * @param string|int $error The error code.
3670 * @param \WP_Error $registered The error object.
3671 */
3672 do_action( 'jetpack_connection_register_fail', $error, $registered );
3673 break;
3674 }
3675
3676 $redirect = isset( $_GET['redirect'] ) ? wp_unslash( $_GET['redirect'] ) : false; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
3677
3678 /**
3679 * Jetpack registration Success.
3680 *
3681 * @since 7.5.0
3682 *
3683 * @param string $from 'from' GET parameter;
3684 */
3685 do_action( 'jetpack_connection_register_success', $from );
3686
3687 $url = $this->build_connect_url( true, $redirect, $from );
3688
3689 if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) {
3690 $url = add_query_arg( 'auth_approved', 'true', $url );
3691 }
3692
3693 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
3694 wp_safe_redirect( $url );
3695 exit( 0 );
3696 case 'activate':
3697 if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
3698 $error = 'cheatin';
3699 break;
3700 }
3701
3702 $module = isset( $_GET['module'] ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : '';
3703 check_admin_referer( "jetpack_activate-$module" );
3704 self::log( 'activate', $module );
3705 if ( ! self::activate_module( $module ) ) {
3706 /* translators: module/feature name */
3707 self::state( 'error', sprintf( __( 'Could not activate %s', 'jetpack' ), $module ) );
3708 }
3709 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
3710 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
3711 exit( 0 );
3712 case 'activate_default_modules':
3713 check_admin_referer( 'activate_default_modules' );
3714 self::log( 'activate_default_modules' );
3715 self::restate();
3716 $min_version = isset( $_GET['min_version'] ) ? sanitize_text_field( wp_unslash( $_GET['min_version'] ) ) : false;
3717 $max_version = isset( $_GET['max_version'] ) ? sanitize_text_field( wp_unslash( $_GET['max_version'] ) ) : false;
3718 $other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_GET['other_modules'] ) ) : array();
3719 self::activate_default_modules( $min_version, $max_version, $other_modules );
3720 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
3721 exit( 0 );
3722 case 'disconnect':
3723 if ( ! current_user_can( 'jetpack_disconnect' ) ) {
3724 $error = 'cheatin';
3725 break;
3726 }
3727
3728 check_admin_referer( 'jetpack-disconnect' );
3729 self::log( 'disconnect' );
3730 self::disconnect();
3731 wp_safe_redirect( self::admin_url( 'disconnected=true' ) );
3732 exit( 0 );
3733 case 'reconnect':
3734 if ( ! current_user_can( 'jetpack_reconnect' ) ) {
3735 $error = 'cheatin';
3736 break;
3737 }
3738
3739 check_admin_referer( 'jetpack-reconnect' );
3740 self::log( 'reconnect' );
3741 self::disconnect();
3742
3743 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
3744 wp_safe_redirect( $this->build_connect_url( true, false, 'reconnect' ) );
3745 exit( 0 );
3746 case 'deactivate':
3747 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
3748 $error = 'cheatin';
3749 break;
3750 }
3751
3752 $modules = sanitize_text_field( wp_unslash( $_GET['module'] ) );
3753 check_admin_referer( "jetpack_deactivate-$modules" );
3754 foreach ( explode( ',', $modules ) as $module ) {
3755 self::log( 'deactivate', $module );
3756 self::deactivate_module( $module );
3757 self::state( 'message', 'module_deactivated' );
3758 }
3759 self::state( 'module', $modules );
3760 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
3761 exit( 0 );
3762 case 'unlink':
3763 $redirect = isset( $_GET['redirect'] ) ? sanitize_text_field( wp_unslash( $_GET['redirect'] ) ) : '';
3764 check_admin_referer( 'jetpack-unlink' );
3765 self::log( 'unlink' );
3766 $this->connection_manager->disconnect_user();
3767 self::state( 'message', 'unlinked' );
3768 if ( 'sub-unlink' === $redirect ) {
3769 wp_safe_redirect( admin_url() );
3770 } else {
3771 wp_safe_redirect( self::admin_url( array( 'page' => rawurlencode( $redirect ) ) ) );
3772 }
3773 exit( 0 );
3774 default:
3775 /**
3776 * Fires when a Jetpack admin page is loaded with an unrecognized parameter.
3777 *
3778 * @since 2.6.0
3779 *
3780 * @param string sanitize_key( $_GET['action'] ) Unrecognized URL parameter.
3781 */
3782 do_action( 'jetpack_unrecognized_action', sanitize_key( $_GET['action'] ) );
3783 }
3784 }
3785
3786 $error = $error ? $error : self::state( 'error' );
3787 if ( ! $error ) {
3788 self::activate_new_modules( true );
3789 }
3790
3791 $activated_manage = false;
3792 $message_code = self::state( 'message' );
3793 if ( self::state( 'optin-manage' ) ) {
3794 $activated_manage = $message_code;
3795 $message_code = 'jetpack-manage';
3796 }
3797
3798 switch ( $message_code ) {
3799 case 'jetpack-manage':
3800 $sites_url = esc_url( Redirect::get_url( 'calypso-sites' ) );
3801 // translators: %s is the URL to the "Sites" panel on wordpress.com.
3802 $this->message = '<strong>' . sprintf( __( 'You are all set! Your site can now be managed from <a href="%s" target="_blank">wordpress.com/sites</a>.', 'jetpack' ), $sites_url ) . '</strong>';
3803 if ( $activated_manage ) {
3804 $this->message .= '<br /><strong>' . __( 'Manage has been activated for you!', 'jetpack' ) . '</strong>';
3805 }
3806 break;
3807
3808 }
3809
3810 $deactivated_plugins = self::state( 'deactivated_plugins' );
3811
3812 if ( ! empty( $deactivated_plugins ) ) {
3813 $deactivated_plugins = explode( ',', $deactivated_plugins );
3814 $deactivated_titles = array();
3815 foreach ( $deactivated_plugins as $deactivated_plugin ) {
3816 list( $module, $idx ) = explode( ':', $deactivated_plugin );
3817 if ( ! isset( $this->plugins_to_deactivate[ $module ][ $idx ] ) ) {
3818 continue;
3819 }
3820
3821 $deactivated_titles[] = '<strong>' . str_replace( ' ', '&nbsp;', $this->plugins_to_deactivate[ $module ][ $idx ][1] ) . '</strong>';
3822 }
3823
3824 if ( $deactivated_titles ) {
3825 if ( $this->message ) {
3826 $this->message .= "<br /><br />\n";
3827 }
3828
3829 $this->message .= wp_sprintf(
3830 _n(
3831 'Jetpack contains the most recent version of the old %l plugin.',
3832 'Jetpack contains the most recent versions of the old %l plugins.',
3833 count( $deactivated_titles ),
3834 'jetpack'
3835 ),
3836 $deactivated_titles
3837 );
3838
3839 $this->message .= "<br />\n";
3840
3841 $this->message .= _n(
3842 'The old version has been deactivated and can be removed from your site.',
3843 'The old versions have been deactivated and can be removed from your site.',
3844 count( $deactivated_titles ),
3845 'jetpack'
3846 );
3847 }
3848 }
3849
3850 $this->privacy_checks = self::state( 'privacy_checks' );
3851
3852 if ( $this->message || $this->error || $this->privacy_checks ) {
3853 add_action( 'jetpack_notices', array( $this, 'admin_notices' ) );
3854 }
3855
3856 add_filter( 'jetpack_short_module_description', 'wptexturize' );
3857 }
3858
3859 /**
3860 * Display admin notice upon error.
3861 *
3862 * @return void
3863 */
3864 public function admin_notices() {
3865
3866 if ( $this->error ) {
3867 ?>
3868 <div id="message" class="jetpack-message jetpack-err">
3869 <div class="squeezer">
3870 <h2>
3871 <?php
3872 echo wp_kses(
3873 $this->error,
3874 array(
3875 'a' => array( 'href' => array() ),
3876 'small' => true,
3877 'code' => true,
3878 'strong' => true,
3879 'br' => true,
3880 'b' => true,
3881 )
3882 );
3883 ?>
3884 </h2>
3885 <?php
3886 $desc = self::state( 'error_description' );
3887 if ( $desc ) :
3888 ?>
3889 <p><?php echo esc_html( stripslashes( $desc ) ); ?></p>
3890 <?php endif; ?>
3891 </div>
3892 </div>
3893 <?php
3894 }
3895
3896 if ( $this->message ) {
3897 ?>
3898 <div id="message" class="jetpack-message">
3899 <div class="squeezer">
3900 <h2>
3901 <?php
3902 echo wp_kses(
3903 $this->message,
3904 array(
3905 'strong' => array(),
3906 'a' => array( 'href' => true ),
3907 'br' => true,
3908 )
3909 );
3910 ?>
3911 </h2>
3912 </div>
3913 </div>
3914 <?php
3915 }
3916
3917 if ( $this->privacy_checks ) :
3918 $module_names = array();
3919 $module_slugs = array();
3920
3921 $privacy_checks = explode( ',', $this->privacy_checks );
3922 $privacy_checks = array_filter( $privacy_checks, array( 'Jetpack', 'is_module' ) );
3923 foreach ( $privacy_checks as $module_slug ) {
3924 $module = self::get_module( $module_slug );
3925 if ( ! $module ) {
3926 continue;
3927 }
3928
3929 $module_slugs[] = $module_slug;
3930 $module_names[] = "<strong>{$module['name']}</strong>";
3931 }
3932
3933 $module_slugs = implode( ',', $module_slugs );
3934 ?>
3935 <div id="message" class="jetpack-message jetpack-err">
3936 <div class="squeezer">
3937 <h2><strong><?php esc_html_e( 'Is this site private?', 'jetpack' ); ?></strong></h2><br />
3938 <p>
3939 <?php
3940 echo wp_kses(
3941 wptexturize(
3942 wp_sprintf(
3943 _nx(
3944 "Like your site's RSS feeds, %l allows access to your posts and other content to third parties.",
3945 "Like your site's RSS feeds, %l allow access to your posts and other content to third parties.",
3946 count( $privacy_checks ),
3947 '%l = list of Jetpack module/feature names',
3948 'jetpack'
3949 ),
3950 $module_names
3951 )
3952 ),
3953 array( 'strong' => true )
3954 );
3955
3956 echo "\n<br />\n";
3957
3958 echo wp_kses(
3959 sprintf(
3960 /* translators: URL to deactivate Jetpack features. */
3961 _nx(
3962 'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating this feature</a>.',
3963 'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating these features</a>.',
3964 count( $privacy_checks ),
3965 '%1$s = deactivation URL, %2$s = "Deactivate {list of Jetpack module/feature names}',
3966 'jetpack'
3967 ),
3968 esc_url(
3969 wp_nonce_url(
3970 self::admin_url(
3971 array(
3972 'page' => 'jetpack',
3973 'action' => 'deactivate',
3974 'module' => rawurlencode( $module_slugs ),
3975 )
3976 ),
3977 "jetpack_deactivate-$module_slugs"
3978 )
3979 ),
3980 esc_attr( wp_kses( wp_sprintf( _x( 'Deactivate %l', '%l = list of Jetpack module/feature names', 'jetpack' ), $module_names ), array() ) )
3981 ),
3982 array(
3983 'a' => array(
3984 'href' => true,
3985 'title' => true,
3986 ),
3987 )
3988 );
3989 ?>
3990 </p>
3991 </div>
3992 </div>
3993 <?php
3994 endif;
3995 }
3996
3997 /**
3998 * We can't always respond to a signed XML-RPC request with a
3999 * helpful error message. In some circumstances, doing so could
4000 * leak information.
4001 *
4002 * Instead, track that the error occurred via a Jetpack_Option,
4003 * and send that data back in the heartbeat.
4004 * All this does is increment a number, but it's enough to find
4005 * trends.
4006 *
4007 * @param WP_Error $xmlrpc_error The error produced during
4008 * signature validation.
4009 */
4010 public function track_xmlrpc_error( $xmlrpc_error ) {
4011 $code = is_wp_error( $xmlrpc_error )
4012 ? $xmlrpc_error->get_error_code()
4013 : 'should-not-happen';
4014
4015 $xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
4016 if ( isset( $xmlrpc_errors[ $code ] ) && $xmlrpc_errors[ $code ] ) {
4017 // No need to update the option if we already have
4018 // this code stored.
4019 return;
4020 }
4021 $xmlrpc_errors[ $code ] = true;
4022
4023 Jetpack_Options::update_option( 'xmlrpc_errors', $xmlrpc_errors, false );
4024 }
4025
4026 /**
4027 * Initialize the jetpack stats instance only when needed
4028 *
4029 * @return void
4030 */
4031 private function initialize_stats() {
4032 if ( $this->a8c_mc_stats_instance === null ) {
4033 $this->a8c_mc_stats_instance = new Automattic\Jetpack\A8c_Mc_Stats();
4034 }
4035 }
4036
4037 /**
4038 * Record a stat for later output. This will only currently output in the admin_footer.
4039 *
4040 * @param string $group Stats group.
4041 * @param string $detail Stats detail.
4042 */
4043 public function stat( $group, $detail ) {
4044 $this->initialize_stats();
4045 $this->a8c_mc_stats_instance->add( $group, $detail );
4046
4047 // Keep a local copy for backward compatibility (there are some direct checks on this).
4048 $this->stats = $this->a8c_mc_stats_instance->get_current_stats();
4049 }
4050
4051 /**
4052 * Load stats pixels. $group is auto-prefixed with "x_jetpack-"
4053 *
4054 * @param string $method Used to check if method is "server-side".
4055 */
4056 public function do_stats( $method = '' ) {
4057 $this->initialize_stats();
4058 if ( 'server_side' === $method ) {
4059 $this->a8c_mc_stats_instance->do_server_side_stats();
4060 } else {
4061 $this->a8c_mc_stats_instance->do_stats();
4062 }
4063
4064 // Keep a local copy for backward compatibility (there are some direct checks on this).
4065 $this->stats = array();
4066 }
4067
4068 /**
4069 * Runs stats code for a one-off, server-side.
4070 *
4071 * @param array|string $args The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store.
4072 *
4073 * @return bool If it worked.
4074 */
4075 public static function do_server_side_stat( $args ) {
4076 $url = self::build_stats_url( $args );
4077 $a8c_mc_stats_instance = new Automattic\Jetpack\A8c_Mc_Stats();
4078 return $a8c_mc_stats_instance->do_server_side_stat( $url );
4079 }
4080
4081 /**
4082 * Builds the stats url.
4083 *
4084 * @param array|string $args The arguments to append to the URL.
4085 *
4086 * @return string The URL to be pinged.
4087 */
4088 public static function build_stats_url( $args ) {
4089
4090 $a8c_mc_stats_instance = new Automattic\Jetpack\A8c_Mc_Stats();
4091 return $a8c_mc_stats_instance->build_stats_url( $args );
4092 }
4093
4094 /**
4095 * Builds a URL to the Jetpack connection auth page
4096 *
4097 * @since 3.9.5
4098 *
4099 * @param bool $raw If true, URL will not be escaped.
4100 * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection.
4101 * If string, will be a custom redirect.
4102 * @param bool|string $from If not false, adds 'from=$from' param to the connect URL.
4103 * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0.
4104 *
4105 * @return string Connect URL
4106 */
4107 public function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) {
4108 $site_id = Jetpack_Options::get_option( 'id' );
4109 $blog_token = ( new Tokens() )->get_access_token();
4110
4111 if ( $register || ! $blog_token || ! $site_id ) {
4112 $url = self::nonce_url_no_esc( self::admin_url( 'action=register' ), 'jetpack-register' );
4113
4114 if ( ! empty( $redirect ) ) {
4115 $url = add_query_arg(
4116 'redirect',
4117 rawurlencode( wp_validate_redirect( esc_url_raw( $redirect ) ) ),
4118 $url
4119 );
4120 }
4121
4122 if ( is_network_admin() ) {
4123 $url = add_query_arg( 'is_multisite', network_admin_url( 'admin.php?page=jetpack-settings' ), $url );
4124 }
4125
4126 $calypso_env = ( new Host() )->get_calypso_env();
4127
4128 if ( ! empty( $calypso_env ) ) {
4129 $url = add_query_arg( 'calypso_env', $calypso_env, $url );
4130 }
4131 } else {
4132
4133 // Let's check the existing blog token to see if we need to re-register. We only check once per minute
4134 // because otherwise this logic can get us in to a loop.
4135 $last_connect_url_check = (int) Jetpack_Options::get_raw_option( 'jetpack_last_connect_url_check' );
4136 if ( ! $last_connect_url_check || ( time() - $last_connect_url_check ) > MINUTE_IN_SECONDS ) {
4137 Jetpack_Options::update_raw_option( 'jetpack_last_connect_url_check', time() );
4138
4139 $response = Client::wpcom_json_api_request_as_blog(
4140 sprintf( '/sites/%d', $site_id ) . '?force=wpcom',
4141 '1.1'
4142 );
4143
4144 if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
4145
4146 // Generating a register URL instead to refresh the existing token.
4147 return $this->build_connect_url( $raw, $redirect, $from, true );
4148 }
4149 }
4150
4151 $url = ( new Authorize_Redirect( static::connection() ) )->build_authorize_url( $redirect );
4152 }
4153
4154 if ( $from ) {
4155 $url = add_query_arg( 'from', $from, $url );
4156 }
4157
4158 $url = $raw ? esc_url_raw( $url ) : esc_url( $url );
4159 /**
4160 * Filter the URL used when connecting a user to a WordPress.com account.
4161 *
4162 * @since 8.1.0
4163 *
4164 * @param string $url Connection URL.
4165 * @param bool $raw If true, URL will not be escaped.
4166 */
4167 return apply_filters( 'jetpack_build_connection_url', $url, $raw );
4168 }
4169
4170 /**
4171 * Create the Jetpack authorization URL.
4172 *
4173 * @param bool|string $redirect URL to redirect to.
4174 * @param null $deprecated Deprecated since Jetpack 10.9.
4175 *
4176 * @todo Update default value for redirect since the called function expects a string.
4177 *
4178 * @deprecated 13.4
4179 *
4180 * @return mixed|void
4181 */
4182 public static function build_authorize_url( $redirect = false, $deprecated = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
4183 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::build_authorize_url' );
4184
4185 return ( new Authorize_Redirect( static::connection() ) )->build_authorize_url( $redirect );
4186 }
4187
4188 /**
4189 * Filters the connection URL parameter array.
4190 *
4191 * @deprecated 13.4
4192 *
4193 * @param array $args default URL parameters used by the package.
4194 * @return array the modified URL arguments array.
4195 */
4196 public static function filter_connect_request_body( $args ) {
4197 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::filter_connect_request_body' );
4198
4199 return Authorize_Redirect::filter_connect_request_body( $args );
4200 }
4201
4202 /**
4203 * Filters the `jetpack/v4/connection/data` API response of the Connection package in order to
4204 * add Jetpack-the-plugin related permissions.
4205 *
4206 * @since 10.0
4207 *
4208 * @param array $current_user_connection_data An array containing the current user connection data.
4209 * @return array
4210 */
4211 public static function filter_jetpack_current_user_connection_data( $current_user_connection_data ) {
4212 $jetpack_permissions = array(
4213 'admin_page' => current_user_can( 'jetpack_admin_page' ),
4214 'manage_modules' => current_user_can( 'jetpack_manage_modules' ),
4215 'network_admin' => current_user_can( 'jetpack_network_admin_page' ),
4216 'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
4217 'edit_posts' => current_user_can( 'edit_posts' ),
4218 'publish_posts' => current_user_can( 'publish_posts' ),
4219 'manage_options' => current_user_can( 'manage_options' ),
4220 'view_stats' => current_user_can( 'view_stats' ),
4221 'manage_plugins' => current_user_can( 'install_plugins' )
4222 && current_user_can( 'activate_plugins' )
4223 && current_user_can( 'update_plugins' )
4224 && current_user_can( 'delete_plugins' ),
4225 );
4226
4227 if ( isset( $current_user_connection_data['permissions'] ) &&
4228 is_array( $current_user_connection_data['permissions'] ) ) {
4229 $current_user_connection_data['permissions'] = array_merge( $current_user_connection_data['permissions'], $jetpack_permissions );
4230 } else {
4231 $current_user_connection_data['permissions'] = $jetpack_permissions;
4232 }
4233
4234 return $current_user_connection_data;
4235 }
4236
4237 /**
4238 * Filters the redirection URL that is used for connect requests. The redirect
4239 * URL should return the user back to the Jetpack console.
4240 *
4241 * @deprecated 13.4
4242 *
4243 * @param String $redirect the default redirect URL used by the package.
4244 * @return String the modified URL.
4245 */
4246 public static function filter_connect_redirect_url( $redirect ) {
4247 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::filter_connect_redirect_url' );
4248 return Authorize_Redirect::filter_connect_redirect_url( $redirect );
4249 }
4250
4251 /**
4252 * This action fires at the beginning of the Manager::authorize method.
4253 */
4254 public static function authorize_starting() {
4255 $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' );
4256 // Checking if site has been active/connected previously before recording unique connection.
4257 if ( ! $jetpack_unique_connection ) {
4258 // jetpack_unique_connection option has never been set.
4259 $jetpack_unique_connection = array(
4260 'connected' => 0,
4261 'disconnected' => 0,
4262 'version' => '3.6.1',
4263 );
4264
4265 update_option( 'jetpack_unique_connection', $jetpack_unique_connection );
4266
4267 // Track unique connection.
4268 $jetpack = self::init();
4269
4270 $jetpack->stat( 'connections', 'unique-connection' );
4271 $jetpack->do_stats( 'server_side' );
4272 }
4273
4274 // Increment number of times connected.
4275 $jetpack_unique_connection['connected'] += 1;
4276 Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection );
4277 }
4278
4279 /**
4280 * This action fires when the site is registered (connected at a site level).
4281 */
4282 public function handle_unique_registrations_stats() {
4283 $jetpack_unique_registrations = Jetpack_Options::get_option( 'unique_registrations' );
4284 // Checking if site has been registered previously before recording unique connection.
4285 if ( ! $jetpack_unique_registrations ) {
4286
4287 $jetpack_unique_registrations = 0;
4288
4289 $this->stat( 'connections', 'unique-registrations' );
4290 $this->do_stats( 'server_side' );
4291 }
4292
4293 // Increment number of times connected.
4294 ++$jetpack_unique_registrations;
4295 Jetpack_Options::update_option( 'unique_registrations', $jetpack_unique_registrations );
4296 }
4297
4298 /**
4299 * This action fires at the end of the Manager::authorize method when a secondary user is
4300 * linked.
4301 */
4302 public static function authorize_ending_linked() {
4303 // Don't activate anything since we are just connecting a user.
4304 self::state( 'message', 'linked' );
4305 }
4306
4307 /**
4308 * This action fires at the end of the Manager::authorize method when the master user is
4309 * authorized.
4310 *
4311 * @param array $data The request data.
4312 */
4313 public static function authorize_ending_authorized( $data ) {
4314 // If redirect_uri is SSO, ensure SSO module is enabled.
4315 parse_str( wp_parse_url( $data['redirect_uri'], PHP_URL_QUERY ), $redirect_options );
4316
4317 /** This filter is documented in class.jetpack-cli.php */
4318 $jetpack_start_enable_sso = apply_filters( 'jetpack_start_enable_sso', true );
4319
4320 $activate_sso = (
4321 isset( $redirect_options['action'] ) &&
4322 'jetpack-sso' === $redirect_options['action'] &&
4323 $jetpack_start_enable_sso
4324 );
4325
4326 $do_redirect_on_error = ( 'client' === $data['auth_type'] );
4327
4328 self::handle_post_authorization_actions( $activate_sso, $do_redirect_on_error );
4329 }
4330
4331 /**
4332 * Fires on the jetpack_site_registered hook and acitvates default modules
4333 */
4334 public static function activate_default_modules_on_site_register() {
4335 self::handle_default_module_activation( false );
4336
4337 // Since this is a fresh connection, be sure to clear out IDC options.
4338 Identity_Crisis::clear_all_idc_options();
4339 }
4340
4341 /**
4342 * This action fires at the end of the REST_Connector connection_reconnect method when the
4343 * reconnect process is completed.
4344 * Note that this currently only happens when we don't need the user to re-authorize
4345 * their WP.com account, eg in cases where we are restoring a connection with
4346 * unhealthy blog token.
4347 */
4348 public static function reconnection_completed() {
4349 self::state( 'message', 'reconnection_completed' );
4350 }
4351
4352 /**
4353 * Apply activation source to a query string array.
4354 *
4355 * @param array $args Args used for a query string.
4356 *
4357 * @return void
4358 */
4359 public static function apply_activation_source_to_args( &$args ) {
4360 list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' );
4361
4362 if ( $activation_source_name ) {
4363 $args['_as'] = rawurlencode( $activation_source_name );
4364 }
4365
4366 if ( $activation_source_keyword ) {
4367 $args['_ak'] = rawurlencode( $activation_source_keyword );
4368 }
4369 }
4370
4371 /**
4372 * Returns the reconnection URL.
4373 *
4374 * @param bool $raw True to return an unescaped URL. False returns value after `esc_url`.
4375 *
4376 * @return string|null
4377 */
4378 public function build_reconnect_url( $raw = false ) {
4379 $url = wp_nonce_url( self::admin_url( 'action=reconnect' ), 'jetpack-reconnect' );
4380 return $raw ? $url : esc_url( $url );
4381 }
4382
4383 /**
4384 * Jetpack Admin URL.
4385 *
4386 * @param array $args Query string args.
4387 *
4388 * @return string Jetpack admin URL.
4389 */
4390 public static function admin_url( $args = null ) {
4391 return ( new Paths() )->admin_url( $args );
4392 }
4393
4394 /**
4395 * Creates a nonce from an URL.
4396 *
4397 * @param string $actionurl URL for action.
4398 * @param string $action Nonce action.
4399 * @param string $name Query arg name.
4400 *
4401 * @return string
4402 */
4403 public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) {
4404 $actionurl = str_replace( '&amp;', '&', $actionurl );
4405 return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
4406 }
4407
4408 /**
4409 * Dismiss Jetpack notices.
4410 *
4411 * @return void
4412 */
4413 public function dismiss_jetpack_notice() {
4414 if ( ! isset( $_GET['jetpack-notice'] ) ) {
4415 return;
4416 }
4417
4418 switch ( $_GET['jetpack-notice'] ) {
4419 case 'dismiss':
4420 if ( check_admin_referer( 'jetpack-deactivate' ) && ! is_plugin_active_for_network( plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ) ) ) {
4421
4422 require_once ABSPATH . 'wp-admin/includes/plugin.php';
4423 deactivate_plugins( JETPACK__PLUGIN_DIR . 'jetpack.php', false, false );
4424 wp_safe_redirect( admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s=' );
4425 }
4426 break;
4427 }
4428 }
4429
4430 /**
4431 * Determines which module has a higher sort order.
4432 *
4433 * @param array $a Modules array.
4434 * @param array $b Modules array.
4435 *
4436 * @return int 0 if the same sort or (+/-) to indicate which is greater.
4437 */
4438 public static function sort_modules( $a, $b ) {
4439 return $a['sort'] <=> $b['sort'];
4440 }
4441
4442 /**
4443 * Recheck SSL status for use via an AJAX call.
4444 *
4445 * Sends data back via `wp_send_json`.
4446 *
4447 * @return void
4448 */
4449 public function ajax_recheck_ssl() {
4450 check_ajax_referer( 'recheck-ssl', 'ajax-nonce' );
4451 $result = self::permit_ssl( true );
4452 wp_send_json(
4453 array(
4454 'enabled' => $result,
4455 'message' => get_transient( 'jetpack_https_test_message' ),
4456 )
4457 );
4458 }
4459
4460 /* Client API */
4461
4462 /**
4463 * Verify the onboarding token.
4464 *
4465 * @deprecated since 13.9
4466 *
4467 * @param array $token_data Token data.
4468 * @param string $token Token value.
4469 * @param string $request_data JSON-encoded request data.
4470 *
4471 * @return mixed
4472 */
4473 public static function verify_onboarding_token( $token_data, $token, $request_data ) {
4474 _deprecated_function( __METHOD__, '13.9' );
4475 // Default to a blog token.
4476 $token_type = 'blog';
4477
4478 // Let's see if this is onboarding. In such case, use user token type and the provided user id.
4479 if ( isset( $request_data ) || ! empty( $_GET['onboarding'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes, if caller is changing the site the nonce should be verified there.
4480 if ( ! empty( $_GET['onboarding'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
4481 $jpo = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
4482 } else {
4483 $jpo = json_decode( $request_data, true );
4484 }
4485
4486 $jpo_token = ! empty( $jpo['onboarding']['token'] ) ? $jpo['onboarding']['token'] : null;
4487 $jpo_user = ! empty( $jpo['onboarding']['jpUser'] ) ? $jpo['onboarding']['jpUser'] : null;
4488
4489 if (
4490 isset( $jpo_user )
4491 && isset( $jpo_token )
4492 && is_email( $jpo_user )
4493 && ctype_alnum( $jpo_token )
4494 && isset( $_GET['rest_route'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- no site changes.
4495 && self::validate_onboarding_token_action(
4496 $jpo_token,
4497 wp_unslash( $_GET['rest_route'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- no site changes.
4498 )
4499 ) {
4500 $jp_user = get_user_by( 'email', $jpo_user );
4501 if ( is_a( $jp_user, 'WP_User' ) ) {
4502 wp_set_current_user( $jp_user->ID );
4503 $user_can = is_multisite()
4504 ? current_user_can_for_site( get_current_blog_id(), 'manage_options' )
4505 : current_user_can( 'manage_options' );
4506 if ( $user_can ) {
4507 $token_type = 'user';
4508 $token->external_user_id = $jp_user->ID;
4509 }
4510 }
4511 }
4512
4513 $token_data['type'] = $token_type;
4514 $token_data['user_id'] = $token->external_user_id;
4515 }
4516
4517 return $token_data;
4518 }
4519
4520 /**
4521 * Create a random secret for validating onboarding payload
4522 *
4523 * @deprecated since 13.9
4524 * @return string Secret token
4525 */
4526 public static function create_onboarding_token() {
4527 _deprecated_function( __METHOD__, '13.9' );
4528 $token = Jetpack_Options::get_option( 'onboarding' );
4529 if ( false === ( $token ) ) {
4530 $token = wp_generate_password( 32, false );
4531 Jetpack_Options::update_option( 'onboarding', $token );
4532 }
4533
4534 return $token;
4535 }
4536
4537 /**
4538 * Remove the onboarding token
4539 *
4540 * @deprecated since 13.9
4541 * @return bool True on success, false on failure
4542 */
4543 public static function invalidate_onboarding_token() {
4544 _deprecated_function( __METHOD__, '13.9' );
4545 return Jetpack_Options::delete_option( 'onboarding' );
4546 }
4547
4548 /**
4549 * Validate an onboarding token for a specific action
4550 *
4551 * @deprecated since 13.9
4552 *
4553 * @param string $token Onboarding token.
4554 * @param string $action Action name.
4555 *
4556 * @return boolean True if token/action pair is accepted, false if not
4557 */
4558 public static function validate_onboarding_token_action( $token, $action ) {
4559 _deprecated_function( __METHOD__, '13.9' );
4560 // Compare tokens, bail if tokens do not match.
4561 if ( ! hash_equals( $token, Jetpack_Options::get_option( 'onboarding' ) ) ) {
4562 return false;
4563 }
4564
4565 // List of valid actions we can take.
4566 $valid_actions = array(
4567 '/jetpack/v4/settings',
4568 );
4569
4570 // Only allow valid actions.
4571 if ( ! in_array( $action, $valid_actions, true ) ) {
4572 return false;
4573 }
4574
4575 return true;
4576 }
4577
4578 /**
4579 * Checks to see if the URL is using SSL to connect with Jetpack
4580 *
4581 * @param bool $force_recheck Force SSL recheck.
4582 *
4583 * @return boolean
4584 * @since 2.3.3
4585 */
4586 public static function permit_ssl( $force_recheck = false ) {
4587 // Do some fancy tests to see if ssl is being supported.
4588 if ( ! $force_recheck ) {
4589 $ssl = get_transient( 'jetpack_https_test' );
4590 }
4591
4592 if ( $force_recheck || false === $ssl ) {
4593 $message = '';
4594 if ( ! str_starts_with( JETPACK__API_BASE, 'https' ) ) {
4595 $ssl = 0;
4596 } else {
4597 $ssl = 1;
4598
4599 if ( ! wp_http_supports( array( 'ssl' => true ) ) ) {
4600 $ssl = 0;
4601 $message = __( 'WordPress reports no SSL support', 'jetpack' );
4602 } else {
4603 $response = wp_remote_get( JETPACK__API_BASE . 'test/1/' );
4604 if ( is_wp_error( $response ) ) {
4605 $ssl = 0;
4606 $message = __( 'WordPress reports no SSL support', 'jetpack' );
4607 } elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) {
4608 $ssl = 0;
4609 $message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response );
4610 }
4611 }
4612 }
4613 set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS );
4614 set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS );
4615 }
4616
4617 return (bool) $ssl;
4618 }
4619
4620 /**
4621 * Displays an admin_notice, alerting the user that outbound SSL isn't working.
4622 */
4623 public function alert_auto_ssl_fail() {
4624 if ( ! current_user_can( 'manage_options' ) ) {
4625 return;
4626 }
4627
4628 $ajax_nonce = wp_create_nonce( 'recheck-ssl' );
4629 ?>
4630
4631 <div id="jetpack-ssl-warning" class="error jp-identity-crisis">
4632 <div class="jp-banner__content">
4633 <h2><?php esc_html_e( 'Outbound HTTPS not working', 'jetpack' ); ?></h2>
4634 <p><?php esc_html_e( 'Your site could not connect to WordPress.com via HTTPS. This could be due to any number of reasons, including faulty SSL certificates, misconfigured or missing SSL libraries, or network issues.', 'jetpack' ); ?></p>
4635 <p>
4636 <?php esc_html_e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?>
4637 <a href="#" id="jetpack-recheck-ssl-button"><?php esc_html_e( 'Try again', 'jetpack' ); ?></a>
4638 <span id="jetpack-recheck-ssl-output"><?php echo esc_html( get_transient( 'jetpack_https_test_message' ) ); ?></span>
4639 </p>
4640 <p>
4641 <?php
4642 printf(
4643 /* translators: Both are URLs. First for the connection debug tool and the second for a support page. */
4644 __( 'For more help, try our <a href="%1$s">connection debugger</a> or <a href="%2$s" target="_blank">troubleshooting tips</a>.', 'jetpack' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we're building known HTML.
4645 esc_url( self::admin_url( array( 'page' => 'jetpack-debugger' ) ) ),
4646 esc_url( Redirect::get_url( 'jetpack-support-getting-started-troubleshooting-tips' ) )
4647 );
4648 ?>
4649 </p>
4650 </div>
4651 </div>
4652 <style>
4653 #jetpack-recheck-ssl-output { margin-left: 5px; color: red; }
4654 </style>
4655 <script type="text/javascript">
4656 jQuery( document ).ready( function( $ ) {
4657 $( '#jetpack-recheck-ssl-button' ).click( function( e ) {
4658 var $this = $( this );
4659 $this.html( <?php echo wp_json_encode( __( 'Checking', 'jetpack' ) ); ?> );
4660 $( '#jetpack-recheck-ssl-output' ).html( '' );
4661 e.preventDefault();
4662 var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': <?php echo wp_json_encode( $ajax_nonce ); ?> };
4663 $.post( ajaxurl, data )
4664 .done( function( response ) {
4665 if ( response.enabled ) {
4666 $( '#jetpack-ssl-warning' ).hide();
4667 } else {
4668 this.html( <?php echo wp_json_encode( __( 'Try again', 'jetpack' ) ); ?> );
4669 $( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message );
4670 }
4671 }.bind( $this ) );
4672 } );
4673 } );
4674 </script>
4675
4676 <?php
4677 }
4678
4679 /**
4680 * Returns the connection manager object.
4681 *
4682 * @return Automattic\Jetpack\Connection\Manager
4683 */
4684 public static function connection() {
4685 $jetpack = static::init();
4686
4687 // If the connection manager hasn't been instantiated, do that now.
4688 if ( ! $jetpack->connection_manager ) {
4689 $jetpack->connection_manager = new Connection_Manager( 'jetpack' );
4690 }
4691
4692 return $jetpack->connection_manager;
4693 }
4694
4695 /**
4696 * Get verification secrets.
4697 *
4698 * @param string $action Action name.
4699 * @param int $user_id User ID.
4700 *
4701 * @return array|string|WP_Error
4702 */
4703 public static function get_secrets( $action, $user_id ) {
4704 $secrets = ( new Secrets() )->get( $action, $user_id );
4705
4706 if ( Secrets::SECRETS_MISSING === $secrets ) {
4707 return new WP_Error( 'verify_secrets_missing', 'Verification secrets not found' );
4708 }
4709
4710 if ( Secrets::SECRETS_EXPIRED === $secrets ) {
4711 return new WP_Error( 'verify_secrets_expired', 'Verification took too long' );
4712 }
4713
4714 return $secrets;
4715 }
4716
4717 /**
4718 * Filters the token request body to include tracking properties.
4719 *
4720 * @param array $properties Token request properties.
4721 *
4722 * @return array amended properties.
4723 */
4724 public static function filter_token_request_body( $properties ) {
4725 $tracking = new Tracking();
4726 $tracks_identity = $tracking->tracks_get_identity( get_current_user_id() );
4727
4728 return array_merge(
4729 $properties,
4730 array(
4731 '_ui' => $tracks_identity['_ui'],
4732 '_ut' => $tracks_identity['_ut'],
4733 )
4734 );
4735 }
4736
4737 /**
4738 * If the db version is showing something other that what we've got now, bump it to current.
4739 *
4740 * @return bool True if the option was incorrect and updated, false if nothing happened.
4741 */
4742 public static function maybe_set_version_option() {
4743 list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
4744 if ( JETPACK__VERSION !== $version ) {
4745 Jetpack_Options::update_option( 'version', JETPACK__VERSION . ':' . time() );
4746
4747 if ( version_compare( JETPACK__VERSION, $version, '>' ) ) {
4748 /** This action is documented in class.jetpack.php */
4749 do_action( 'updating_jetpack_version', JETPACK__VERSION, $version );
4750 }
4751
4752 return true;
4753 }
4754 return false;
4755 }
4756
4757 /* Client Server API */
4758
4759 /**
4760 * State is passed via cookies from one request to the next, but never to subsequent requests.
4761 * SET: state( $key, $value );
4762 * GET: $value = state( $key );
4763 *
4764 * @param string $key State key.
4765 * @param string $value Value.
4766 * @param bool $restate Reset the cookie (private).
4767 */
4768 public static function state( $key = null, $value = null, $restate = false ) {
4769 return ( new CookieState() )->state( $key, $value, $restate );
4770 }
4771
4772 /**
4773 * Set an empty state.
4774 *
4775 * @return void
4776 */
4777 public static function restate() {
4778 self::state( null, null, true );
4779 }
4780
4781 /**
4782 * Determines whether the jetpackState[$key] value should be added to the
4783 * cookie.
4784 *
4785 * @param string $key The state key.
4786 *
4787 * @return boolean Whether the value should be added to the cookie.
4788 */
4789 public static function should_set_cookie( $key ) {
4790 return ( new CookieState() )->should_set_cookie( $key );
4791 }
4792
4793 /**
4794 * Check if site is publicly accessible.
4795 *
4796 * @param string $file Module file.
4797 *
4798 * @return void
4799 */
4800 public static function check_privacy( $file ) {
4801 static $is_site_publicly_accessible = null;
4802
4803 if ( $is_site_publicly_accessible === null ) {
4804 $is_site_publicly_accessible = false;
4805
4806 $rpc = new Jetpack_IXR_Client();
4807
4808 $success = $rpc->query( 'jetpack.isSitePubliclyAccessible', home_url() );
4809 if ( $success ) {
4810 $response = $rpc->getResponse();
4811 if ( $response ) {
4812 $is_site_publicly_accessible = true;
4813 }
4814 }
4815
4816 Jetpack_Options::update_option( 'public', (int) $is_site_publicly_accessible );
4817 }
4818
4819 if ( $is_site_publicly_accessible ) {
4820 return;
4821 }
4822
4823 $module_slug = self::get_module_slug( $file );
4824
4825 $privacy_checks = self::state( 'privacy_checks' );
4826 if ( ! $privacy_checks ) {
4827 $privacy_checks = $module_slug;
4828 } else {
4829 $privacy_checks .= ",$module_slug";
4830 }
4831
4832 self::state( 'privacy_checks', $privacy_checks );
4833 }
4834
4835 /* JSON API Authorization */
4836
4837 /**
4838 * Handles the login action for Authorizing the JSON API
4839 */
4840 public function login_form_json_api_authorization() {
4841 $authorize_json_api = new Authorize_Json_Api();
4842 $authorize_json_api->verify_json_api_authorization_request();
4843
4844 add_action( 'wp_login', array( $authorize_json_api, 'store_json_api_authorization_token' ), 10, 2 );
4845
4846 add_action( 'login_message', array( $authorize_json_api, 'login_message_json_api_authorization' ) );
4847 add_action( 'login_form', array( $this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
4848 add_filter( 'site_url', array( $this, 'post_login_form_to_signed_url' ), 10, 3 );
4849 }
4850
4851 /**
4852 * Make sure the login form is POSTed to the signed URL so we can reverify the request.
4853 *
4854 * @param string $url Redirect URL.
4855 * @param string $path Path.
4856 * @param string $scheme URL Scheme.
4857 */
4858 public function post_login_form_to_signed_url( $url, $path, $scheme ) {
4859 if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) {
4860 return $url;
4861 }
4862 $query_string = isset( $_SERVER['QUERY_STRING'] ) ? wp_unslash( $_SERVER['QUERY_STRING'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
4863 $parsed_url = wp_parse_url( $url );
4864 $url = strtok( $url, '?' );
4865 $url = "$url?{$query_string}";
4866 if ( ! empty( $parsed_url['query'] ) ) {
4867 $url .= "&{$parsed_url['query']}";
4868 }
4869
4870 return $url;
4871 }
4872
4873 /**
4874 * Make sure the POSTed request is handled by the same action.
4875 */
4876 public function preserve_action_in_login_form_for_json_api_authorization() {
4877 $http_host = isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- escaped with esc_url below.
4878 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- escaped with esc_url below.
4879 echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n";
4880 echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $http_host . $request_uri ) ) . "' />\n";
4881 }
4882
4883 /**
4884 * If someone logs in to approve API access, store the Access Code in usermeta.
4885 *
4886 * @deprecated 13.4
4887 *
4888 * @param string $user_login Unused.
4889 * @param WP_User $user User logged in.
4890 */
4891 public function store_json_api_authorization_token( $user_login, $user ) {
4892 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::store_json_api_authorization_token' );
4893
4894 return ( new Authorize_Json_Api() )->store_json_api_authorization_token( $user_login, $user );
4895 }
4896
4897 /**
4898 * Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
4899 *
4900 * To be used with a filter of allowed domains for a redirect.
4901 *
4902 * @deprecated 13.4
4903 *
4904 * @param array $domains Allowed WP.com Environments.
4905 */
4906 public function allow_wpcom_public_api_domain( $domains ) {
4907 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Status\\Host::allow_wpcom_public_api_domain' );
4908
4909 return Host::allow_wpcom_public_api_domain( $domains );
4910 }
4911
4912 /**
4913 * Check if the redirect is encoded.
4914 *
4915 * @deprecated 13.4
4916 *
4917 * @param string $redirect_url Redirect URL.
4918 *
4919 * @return bool If redirect has been encoded.
4920 */
4921 public static function is_redirect_encoded( $redirect_url ) {
4922 _deprecated_function( __METHOD__, 'jetpack-13.4' );
4923 return preg_match( '/https?%3A%2F%2F/i', $redirect_url ) > 0;
4924 }
4925
4926 /**
4927 * Add all wordpress.com environments to the safe redirect allowed list.
4928 *
4929 * To be used with a filter of allowed domains for a redirect.
4930 *
4931 * @param array $domains Allowed WP.com Environments.
4932 *
4933 * @deprecated since 11.1
4934 */
4935 public function allow_wpcom_environments( $domains ) {
4936 return Host::allow_wpcom_environments( $domains );
4937 }
4938
4939 /**
4940 * Add the Access Code details to the public-api.wordpress.com redirect.
4941 *
4942 * @deprecated 13.4
4943 *
4944 * @param string $redirect_to URL.
4945 * @param string $original_redirect_to URL.
4946 * @param WP_User $user WP_User for the redirect.
4947 *
4948 * @return string
4949 */
4950 public function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
4951 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::add_token_to_login_redirect_json_api_authorization' );
4952
4953 return ( new Authorize_Json_Api() )->add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user );
4954 }
4955
4956 /**
4957 * Verifies the request by checking the signature
4958 *
4959 * @deprecated 13.4
4960 *
4961 * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow
4962 * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO.
4963 *
4964 * @param null|array $environment Value to override $_REQUEST.
4965 */
4966 public function verify_json_api_authorization_request( $environment = null ) {
4967 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::verify_json_api_authorization_request' );
4968
4969 return ( new Authorize_Json_Api() )->verify_json_api_authorization_request( $environment );
4970 }
4971
4972 /**
4973 * HTML for the JSON API authorization notice.
4974 *
4975 * @deprecated 13.4
4976 *
4977 * @return string
4978 */
4979 public function login_message_json_api_authorization() {
4980 _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::login_message_json_api_authorization' );
4981
4982 return ( new Authorize_Json_Api() )->login_message_json_api_authorization();
4983 }
4984
4985 /**
4986 * Get $content_width, but with a <s>twist</s> filter.
4987 */
4988 public static function get_content_width() {
4989 $content_width = ( isset( $GLOBALS['content_width'] ) && is_numeric( $GLOBALS['content_width'] ) )
4990 ? $GLOBALS['content_width']
4991 : false;
4992 /**
4993 * Filter the Content Width value.
4994 *
4995 * @since 2.2.3
4996 *
4997 * @param string $content_width Content Width value.
4998 */
4999 return apply_filters( 'jetpack_content_width', $content_width );
5000 }
5001
5002 /**
5003 * Pings the WordPress.com Mirror Site for the specified options.
5004 *
5005 * @param string|array $option_names The option names to request from the WordPress.com Mirror Site.
5006 *
5007 * @return array An associative array of the option values as stored in the WordPress.com Mirror Site
5008 */
5009 public function get_cloud_site_options( $option_names ) {
5010 $option_names = array_filter( (array) $option_names, 'is_string' );
5011
5012 $xml = new Jetpack_IXR_Client();
5013 $xml->query( 'jetpack.fetchSiteOptions', $option_names );
5014 if ( $xml->isError() ) {
5015 return array(
5016 'error_code' => $xml->getErrorCode(),
5017 'error_msg' => $xml->getErrorMessage(),
5018 );
5019 }
5020 $cloud_site_options = $xml->getResponse();
5021
5022 return $cloud_site_options;
5023 }
5024
5025 /**
5026 * Checks if the site is currently in an identity crisis.
5027 *
5028 * @return array|bool Array of options that are in a crisis, or false if everything is OK.
5029 */
5030 public static function check_identity_crisis() {
5031 if ( ! self::is_connection_ready() || ( new Status() )->is_offline_mode() || ! Identity_Crisis::validate_sync_error_idc_option() ) {
5032 return false;
5033 }
5034 return Jetpack_Options::get_option( 'sync_error_idc' );
5035 }
5036
5037 /**
5038 * Normalizes a url by doing three things:
5039 * - Strips protocol
5040 * - Strips www
5041 * - Adds a trailing slash
5042 *
5043 * @since 4.4.0
5044 * @param string $url URL.
5045 * @return WP_Error|string
5046 */
5047 public static function normalize_url_protocol_agnostic( $url ) {
5048 $parsed_url = wp_parse_url( trailingslashit( esc_url_raw( $url ) ) );
5049 if ( ! $parsed_url || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
5050 /* translators: URL string */
5051 return new WP_Error( 'cannot_parse_url', sprintf( esc_html__( 'Cannot parse URL %s', 'jetpack' ), $url ) );
5052 }
5053
5054 // Strip www and protocols.
5055 $url = preg_replace( '/^www\./i', '', $parsed_url['host'] . $parsed_url['path'] );
5056 return $url;
5057 }
5058
5059 /**
5060 * Maybe Use a .min.css stylesheet, maybe not.
5061 *
5062 * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args.
5063 *
5064 * @param string $url URL.
5065 * @param string $path File path.
5066 * @param string $plugin Plugin.
5067 *
5068 * @return mixed
5069 */
5070 public static function maybe_min_asset( $url, $path, $plugin ) {
5071 // Short out on things trying to find actual paths.
5072 if ( ! $path || empty( $plugin ) ) {
5073 return $url;
5074 }
5075
5076 $path = ltrim( $path, '/' );
5077
5078 // Strip out the abspath.
5079 $base = dirname( plugin_basename( $plugin ) );
5080
5081 // Short out on non-Jetpack assets.
5082 if ( ! str_starts_with( $base, 'jetpack/' ) ) {
5083 return $url;
5084 }
5085
5086 // File name parsing.
5087 $file = "{$base}/{$path}";
5088 $full_path = JETPACK__PLUGIN_DIR . substr( $file, 8 );
5089 $file_name = substr( $full_path, strrpos( $full_path, '/' ) + 1 );
5090 $file_name_parts_r = array_reverse( explode( '.', $file_name ) );
5091 $extension = array_shift( $file_name_parts_r );
5092
5093 if ( in_array( strtolower( $extension ), array( 'css', 'js' ), true ) ) {
5094 // Already pointing at the minified version.
5095 if ( 'min' === $file_name_parts_r[0] ) {
5096 return $url;
5097 }
5098
5099 $min_full_path = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $full_path );
5100 if ( file_exists( $min_full_path ) ) {
5101 $url = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $url );
5102 // If it's a CSS file, stash it so we can set the .min suffix for rtl-ing.
5103 if ( 'css' === $extension ) {
5104 $key = str_replace( JETPACK__PLUGIN_DIR, 'jetpack/', $min_full_path );
5105 self::$min_assets[ $key ] = $path;
5106 }
5107 }
5108 }
5109
5110 return $url;
5111 }
5112
5113 /**
5114 * If the asset is minified, let's flag .min as the suffix.
5115 *
5116 * Attached to `style_loader_src` filter.
5117 *
5118 * @param string $src source file.
5119 * @param string $handle The registered handle of the script in question.
5120 *
5121 * @return mixed
5122 */
5123 public static function set_suffix_on_min( $src, $handle ) {
5124 if ( ! str_contains( $src, '.min.css' ) ) {
5125 return $src;
5126 }
5127
5128 if ( ! empty( self::$min_assets ) ) {
5129 foreach ( self::$min_assets as $file => $path ) {
5130 if ( str_contains( $src, $file ) ) {
5131 wp_style_add_data( $handle, 'suffix', '.min' );
5132 return $src;
5133 }
5134 }
5135 }
5136
5137 return $src;
5138 }
5139
5140 /**
5141 * Maybe inlines a stylesheet.
5142 *
5143 * @deprecated since 11.7.
5144 *
5145 * @param string $tag The tag that would link to the external asset.
5146 * @param string $handle The registered handle of the script in question.
5147 *
5148 * @return string
5149 */
5150 public static function maybe_inline_style( $tag, $handle ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
5151 _deprecated_function( __METHOD__, '11.7', 'wp_maybe_inline_styles' );
5152
5153 return $tag;
5154 }
5155
5156 /**
5157 * Loads a view file from the views
5158 *
5159 * Data passed in with the $data parameter will be available in the
5160 * template file as $data['value']
5161 *
5162 * @html-template-var array $data
5163 *
5164 * @param string $template - Template file to load.
5165 * @param array $data - Any data to pass along to the template.
5166 * @return boolean - If template file was found.
5167 **/
5168 public function load_view( $template, $data = array() ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- This is used via the required files.
5169 $views_dir = JETPACK__PLUGIN_DIR . 'views/';
5170
5171 if ( file_exists( $views_dir . $template ) ) {
5172 require_once $views_dir . $template;
5173 return true;
5174 }
5175
5176 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
5177 trigger_error( sprintf( 'Jetpack: Unable to find view file: %s', esc_html( $views_dir . $template ) ), E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
5178 }
5179 return false;
5180 }
5181
5182 /**
5183 * Throws warnings for deprecated hooks to be removed from Jetpack that cannot remain in the original place in the code.
5184 */
5185 public function deprecated_hooks() {
5186 $filter_deprecated_list = array(
5187 'jetpack_bail_on_shortcode' => array(
5188 'replacement' => 'jetpack_shortcodes_to_include',
5189 'version' => 'jetpack-3.1.0',
5190 ),
5191 'wpl_sharing_2014_1' => array(
5192 'replacement' => null,
5193 'version' => 'jetpack-3.6.0',
5194 ),
5195 'jetpack-tools-to-include' => array(
5196 'replacement' => 'jetpack_tools_to_include',
5197 'version' => 'jetpack-3.9.0',
5198 ),
5199 'jetpack_identity_crisis_options_to_check' => array(
5200 'replacement' => null,
5201 'version' => 'jetpack-4.0.0',
5202 ),
5203 'update_option_jetpack_single_user_site' => array(
5204 'replacement' => null,
5205 'version' => 'jetpack-4.3.0',
5206 ),
5207 'audio_player_default_colors' => array(
5208 'replacement' => null,
5209 'version' => 'jetpack-4.3.0',
5210 ),
5211 'add_option_jetpack_featured_images_enabled' => array(
5212 'replacement' => null,
5213 'version' => 'jetpack-4.3.0',
5214 ),
5215 'add_option_jetpack_update_details' => array(
5216 'replacement' => null,
5217 'version' => 'jetpack-4.3.0',
5218 ),
5219 'add_option_jetpack_updates' => array(
5220 'replacement' => null,
5221 'version' => 'jetpack-4.3.0',
5222 ),
5223 'add_option_jetpack_network_name' => array(
5224 'replacement' => null,
5225 'version' => 'jetpack-4.3.0',
5226 ),
5227 'add_option_jetpack_network_allow_new_registrations' => array(
5228 'replacement' => null,
5229 'version' => 'jetpack-4.3.0',
5230 ),
5231 'add_option_jetpack_network_add_new_users' => array(
5232 'replacement' => null,
5233 'version' => 'jetpack-4.3.0',
5234 ),
5235 'add_option_jetpack_network_site_upload_space' => array(
5236 'replacement' => null,
5237 'version' => 'jetpack-4.3.0',
5238 ),
5239 'add_option_jetpack_network_upload_file_types' => array(
5240 'replacement' => null,
5241 'version' => 'jetpack-4.3.0',
5242 ),
5243 'add_option_jetpack_network_enable_administration_menus' => array(
5244 'replacement' => null,
5245 'version' => 'jetpack-4.3.0',
5246 ),
5247 'add_option_jetpack_is_multi_site' => array(
5248 'replacement' => null,
5249 'version' => 'jetpack-4.3.0',
5250 ),
5251 'add_option_jetpack_is_main_network' => array(
5252 'replacement' => null,
5253 'version' => 'jetpack-4.3.0',
5254 ),
5255 'add_option_jetpack_main_network_site' => array(
5256 'replacement' => null,
5257 'version' => 'jetpack-4.3.0',
5258 ),
5259 'jetpack_sync_all_registered_options' => array(
5260 'replacement' => null,
5261 'version' => 'jetpack-4.3.0',
5262 ),
5263 'jetpack_has_identity_crisis' => array(
5264 'replacement' => 'jetpack_sync_error_idc_validation',
5265 'version' => 'jetpack-4.4.0',
5266 ),
5267 'jetpack_is_post_mailable' => array(
5268 'replacement' => null,
5269 'version' => 'jetpack-4.4.0',
5270 ),
5271 'jetpack_seo_site_host' => array(
5272 'replacement' => null,
5273 'version' => 'jetpack-5.1.0',
5274 ),
5275 'jetpack_installed_plugin' => array(
5276 'replacement' => 'jetpack_plugin_installed',
5277 'version' => 'jetpack-6.0.0',
5278 ),
5279 'jetpack_holiday_snow_option_name' => array(
5280 'replacement' => null,
5281 'version' => 'jetpack-6.0.0',
5282 ),
5283 'jetpack_holiday_chance_of_snow' => array(
5284 'replacement' => null,
5285 'version' => 'jetpack-6.0.0',
5286 ),
5287 'jetpack_holiday_snow_js_url' => array(
5288 'replacement' => null,
5289 'version' => 'jetpack-6.0.0',
5290 ),
5291 'jetpack_is_holiday_snow_season' => array(
5292 'replacement' => null,
5293 'version' => 'jetpack-6.0.0',
5294 ),
5295 'jetpack_holiday_snow_option_updated' => array(
5296 'replacement' => null,
5297 'version' => 'jetpack-6.0.0',
5298 ),
5299 'jetpack_holiday_snowing' => array(
5300 'replacement' => null,
5301 'version' => 'jetpack-6.0.0',
5302 ),
5303 'jetpack_sso_auth_cookie_expirtation' => array(
5304 'replacement' => 'jetpack_sso_auth_cookie_expiration',
5305 'version' => 'jetpack-6.1.0',
5306 ),
5307 'jetpack_cache_plans' => array(
5308 'replacement' => null,
5309 'version' => 'jetpack-6.1.0',
5310 ),
5311 'jetpack_enable_site_verification' => array(
5312 'replacement' => null,
5313 'version' => 'jetpack-6.5.0',
5314 ),
5315 'can_display_jetpack_manage_notice' => array(
5316 'replacement' => null,
5317 'version' => 'jetpack-7.3.0',
5318 ),
5319 'atd_http_post_timeout' => array(
5320 'replacement' => null,
5321 'version' => 'jetpack-7.3.0',
5322 ),
5323 'atd_service_domain' => array(
5324 'replacement' => null,
5325 'version' => 'jetpack-7.3.0',
5326 ),
5327 'atd_load_scripts' => array(
5328 'replacement' => null,
5329 'version' => 'jetpack-7.3.0',
5330 ),
5331 'jetpack_widget_authors_exclude' => array(
5332 'replacement' => 'jetpack_widget_authors_params',
5333 'version' => 'jetpack-7.7.0',
5334 ),
5335 // Removed in Jetpack 7.9.0.
5336 'jetpack_pwa_manifest' => array(
5337 'replacement' => null,
5338 'version' => 'jetpack-7.9.0',
5339 ),
5340 'jetpack_pwa_background_color' => array(
5341 'replacement' => null,
5342 'version' => 'jetpack-7.9.0',
5343 ),
5344 'jetpack_check_mobile' => array(
5345 'replacement' => null,
5346 'version' => 'jetpack-8.3.0',
5347 ),
5348 'jetpack_mobile_stylesheet' => array(
5349 'replacement' => null,
5350 'version' => 'jetpack-8.3.0',
5351 ),
5352 'jetpack_mobile_template' => array(
5353 'replacement' => null,
5354 'version' => 'jetpack-8.3.0',
5355 ),
5356 'jetpack_mobile_theme_menu' => array(
5357 'replacement' => null,
5358 'version' => 'jetpack-8.3.0',
5359 ),
5360 'minileven_show_featured_images' => array(
5361 'replacement' => null,
5362 'version' => 'jetpack-8.3.0',
5363 ),
5364 'minileven_attachment_size' => array(
5365 'replacement' => null,
5366 'version' => 'jetpack-8.3.0',
5367 ),
5368 'instagram_cache_oembed_api_response_body' => array(
5369 'replacement' => null,
5370 'version' => 'jetpack-9.1.0',
5371 ),
5372 'jetpack_can_make_outbound_https' => array(
5373 'replacement' => null,
5374 'version' => 'jetpack-9.1.0',
5375 ),
5376 'sharing_email_can_send' => array(
5377 'replacement' => null,
5378 'version' => 'jetpack-11.0.0',
5379 ),
5380 'sharing_email_check' => array(
5381 'replacement' => null,
5382 'version' => 'jetpack-11.0.0',
5383 ),
5384 'sharing_services_email' => array(
5385 'replacement' => null,
5386 'version' => 'jetpack-11.0.0',
5387 ),
5388 'jetpack_dsp_promote_posts_enabled' => array(
5389 'replacement' => null,
5390 'version' => 'jetpack-11.8.0',
5391 ),
5392 'jetpack_are_blogging_prompts_enabled' => array(
5393 'replacement' => null,
5394 'version' => 'jetpack-11.8.0',
5395 ),
5396 'jetpack_subscriptions_modal_enabled' => array(
5397 'replacement' => null,
5398 'version' => 'jetpack-12.7.0',
5399 ),
5400 'jetpack_pre_connection_prompt_helpers' => array(
5401 'replacement' => null,
5402 'version' => 'jetpack-13.2.0',
5403 ),
5404 'jetpack_contact_form_use_package' => array(
5405 'replacement' => null,
5406 'version' => 'jetpack-13.4.0',
5407 ),
5408 // jetpack_implode_frontend_css has been removed, but is not listed here. The updated behavior is exactly the only use of the filter.
5409 // We can reassess formally deprecating it here later; for now, it would be noise with no functional difference.
5410 );
5411
5412 foreach ( $filter_deprecated_list as $tag => $args ) {
5413 if ( has_filter( $tag ) ) {
5414 apply_filters_deprecated( $tag, array( null ), $args['version'], $args['replacement'] );
5415 }
5416 }
5417
5418 $action_deprecated_list = array(
5419 'jetpack_updated_theme' => array(
5420 'replacement' => 'jetpack_updated_themes',
5421 'version' => 'jetpack-6.2.0',
5422 ),
5423 'atd_http_post_error' => array(
5424 'replacement' => null,
5425 'version' => 'jetpack-7.3.0',
5426 ),
5427 'mobile_reject_mobile' => array(
5428 'replacement' => null,
5429 'version' => 'jetpack-8.3.0',
5430 ),
5431 'mobile_force_mobile' => array(
5432 'replacement' => null,
5433 'version' => 'jetpack-8.3.0',
5434 ),
5435 'mobile_app_promo_download' => array(
5436 'replacement' => null,
5437 'version' => 'jetpack-8.3.0',
5438 ),
5439 'mobile_setup' => array(
5440 'replacement' => null,
5441 'version' => 'jetpack-8.3.0',
5442 ),
5443 'jetpack_mobile_footer_before' => array(
5444 'replacement' => null,
5445 'version' => 'jetpack-8.3.0',
5446 ),
5447 'wp_mobile_theme_footer' => array(
5448 'replacement' => null,
5449 'version' => 'jetpack-8.3.0',
5450 ),
5451 'minileven_credits' => array(
5452 'replacement' => null,
5453 'version' => 'jetpack-8.3.0',
5454 ),
5455 'jetpack_mobile_header_before' => array(
5456 'replacement' => null,
5457 'version' => 'jetpack-8.3.0',
5458 ),
5459 'jetpack_mobile_header_after' => array(
5460 'replacement' => null,
5461 'version' => 'jetpack-8.3.0',
5462 ),
5463 'sharing_email_dialog' => array(
5464 'replacement' => null,
5465 'version' => 'jetpack-11.0.0',
5466 ),
5467 'sharing_email_send_post' => array(
5468 'replacement' => null,
5469 'version' => 'jetpack-11.0.0',
5470 ),
5471 );
5472
5473 foreach ( $action_deprecated_list as $tag => $args ) {
5474 if ( has_action( $tag ) ) {
5475 do_action_deprecated( $tag, array(), $args['version'], $args['replacement'] );
5476 }
5477 }
5478 }
5479
5480 /**
5481 * Converts any url in a stylesheet, to the correct absolute url.
5482 *
5483 * Considerations:
5484 * - Normal, relative URLs `feh.png`
5485 * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==`
5486 * - Schema-agnostic URLs `//domain.com/feh.png`
5487 * - Absolute URLs `http://domain.com/feh.png`
5488 * - Domain root relative URLs `/feh.png`
5489 *
5490 * @param string $css The raw CSS -- should be read in directly from the file.
5491 * @param string $css_file_url The URL that the file can be accessed at, for calculating paths from.
5492 *
5493 * @return mixed|string
5494 */
5495 public static function absolutize_css_urls( $css, $css_file_url ) {
5496 $pattern = '#url\((?P<path>[^)]*)\)#i';
5497 $css_dir = dirname( $css_file_url );
5498 $p = wp_parse_url( $css_dir );
5499 $domain = sprintf(
5500 '%1$s//%2$s%3$s%4$s',
5501 isset( $p['scheme'] ) ? "{$p['scheme']}:" : '',
5502 isset( $p['user'], $p['pass'] ) ? "{$p['user']}:{$p['pass']}@" : '',
5503 $p['host'],
5504 isset( $p['port'] ) ? ":{$p['port']}" : ''
5505 );
5506
5507 if ( preg_match_all( $pattern, $css, $matches, PREG_SET_ORDER ) ) {
5508 $replace = array();
5509 $find = array();
5510 foreach ( $matches as $match ) {
5511 $url = trim( $match['path'], "'\" \t" );
5512
5513 // If this is a data url, we don't want to mess with it.
5514 if ( str_starts_with( $url, 'data:' ) ) {
5515 continue;
5516 }
5517
5518 // If this is an absolute or protocol-agnostic url,
5519 // we don't want to mess with it.
5520 if ( preg_match( '#^(https?:)?//#i', $url ) ) {
5521 continue;
5522 }
5523
5524 switch ( substr( $url, 0, 1 ) ) {
5525 case '/':
5526 $absolute = $domain . $url;
5527 break;
5528 default:
5529 $absolute = $css_dir . '/' . $url;
5530 }
5531
5532 $find[] = $match[0];
5533 $replace[] = sprintf( 'url("%s")', $absolute );
5534 }
5535 $css = str_replace( $find, $replace, $css );
5536 }
5537
5538 return $css;
5539 }
5540
5541 /**
5542 * Check the heartbeat data
5543 *
5544 * Organizes the heartbeat data by severity. For example, if the site
5545 * is in an ID crisis, it will be in the $filtered_data['bad'] array.
5546 *
5547 * Data will be added to "caution" array, if it either:
5548 * - Out of date Jetpack version
5549 * - Out of date WP version
5550 * - Out of date PHP version
5551 *
5552 * $return array $filtered_data
5553 */
5554 public static function jetpack_check_heartbeat_data() {
5555 $raw_data = Jetpack_Heartbeat::generate_stats_array();
5556
5557 $good = array();
5558 $caution = array();
5559 $bad = array();
5560
5561 foreach ( $raw_data as $stat => $value ) {
5562
5563 // Check jetpack version.
5564 if ( 'version' === $stat ) {
5565 if ( version_compare( $value, JETPACK__VERSION, '<' ) ) {
5566 $caution[ $stat ] = $value . ' - min supported is ' . JETPACK__VERSION;
5567 continue;
5568 }
5569 }
5570
5571 // Check WP version.
5572 if ( 'wp-version' === $stat ) {
5573 if ( version_compare( $value, JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
5574 $caution[ $stat ] = $value . ' - min supported is ' . JETPACK__MINIMUM_WP_VERSION;
5575 continue;
5576 }
5577 }
5578
5579 // Check PHP version.
5580 if ( 'php-version' === $stat ) {
5581 if ( version_compare( PHP_VERSION, JETPACK__MINIMUM_PHP_VERSION, '<' ) ) {
5582 $caution[ $stat ] = $value . ' - min supported is ' . JETPACK__MINIMUM_PHP_VERSION;
5583 continue;
5584 }
5585 }
5586
5587 // Check ID crisis.
5588 if ( 'identitycrisis' === $stat ) {
5589 if ( 'yes' === $value ) {
5590 $bad[ $stat ] = $value;
5591 continue;
5592 }
5593 }
5594
5595 // The rest are good :).
5596 $good[ $stat ] = $value;
5597 }
5598
5599 $filtered_data = array(
5600 'good' => $good,
5601 'caution' => $caution,
5602 'bad' => $bad,
5603 );
5604
5605 return $filtered_data;
5606 }
5607
5608 /**
5609 * This method is used to organize all options that can be reset
5610 * without disconnecting Jetpack.
5611 *
5612 * It is used in class.jetpack-cli.php to reset options
5613 *
5614 * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat.
5615 *
5616 * @return array of options to delete.
5617 */
5618 public static function get_jetpack_options_for_reset() {
5619 return Jetpack_Options::get_options_for_reset();
5620 }
5621
5622 /**
5623 * Get the user's meta box order.
5624 *
5625 * @param array $sorted Value for the user's option.
5626 * @return mixed
5627 */
5628 public function get_user_option_meta_box_order_dashboard( $sorted ) {
5629 if ( ! is_array( $sorted ) ) {
5630 return $sorted;
5631 }
5632
5633 foreach ( $sorted as $box_context => $ids ) {
5634 if ( ! str_contains( $ids, 'dashboard_stats' ) ) {
5635 // If the old id isn't anywhere in the ids, don't bother exploding and fail out.
5636 continue;
5637 }
5638
5639 $ids_array = explode( ',', $ids );
5640 $key = array_search( 'dashboard_stats', $ids_array, true );
5641
5642 if ( false !== $key ) {
5643 // If we've found that exact value in the option (and not `google_dashboard_stats` for example).
5644 $ids_array[ $key ] = 'jetpack_summary_widget';
5645 $sorted[ $box_context ] = implode( ',', $ids_array );
5646 // We've found it, stop searching, and just return.
5647 break;
5648 }
5649 }
5650
5651 return $sorted;
5652 }
5653 /**
5654 * Checks if Akismet is active and working.
5655 *
5656 * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key
5657 * that implied usage of methods present since more recent version.
5658 * See https://github.com/Automattic/jetpack/pull/9585
5659 *
5660 * @since 5.1.0
5661 *
5662 * @return bool True = Akismet available. False = Aksimet not available.
5663 */
5664 public static function is_akismet_active() {
5665 static $status = null;
5666
5667 if ( $status !== null ) {
5668 return $status;
5669 }
5670
5671 // Check if a modern version of Akismet is active.
5672 if ( ! method_exists( 'Akismet', 'http_post' ) ) {
5673 $status = false;
5674 return $status;
5675 }
5676
5677 // Make sure there is a key known to Akismet at all before verifying key.
5678 $akismet_key = Akismet::get_api_key();
5679 if ( ! $akismet_key ) {
5680 $status = false;
5681 return $status;
5682 }
5683
5684 // Possible values: valid, invalid, failure via Akismet. false if no status is cached.
5685 $akismet_key_state = get_transient( 'jetpack_akismet_key_is_valid' );
5686
5687 // Do not used the cache result in wp-admin or REST API requests if the key isn't valid, in case someone is actively renewing, etc.
5688 $recheck = ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) && 'valid' !== $akismet_key_state;
5689 // We cache the result of the Akismet key verification for ten minutes.
5690 if ( ! $akismet_key_state || $recheck ) {
5691 $akismet_key_state = Akismet::verify_key( $akismet_key );
5692 set_transient( 'jetpack_akismet_key_is_valid', $akismet_key_state, 10 * MINUTE_IN_SECONDS );
5693 }
5694
5695 $status = 'valid' === $akismet_key_state;
5696
5697 return $status;
5698 }
5699
5700 /**
5701 * Given a minified path, and a non-minified path, will return
5702 * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy.
5703 *
5704 * Both `$min_base` and `$non_min_base` are expected to be relative to the
5705 * root Jetpack directory.
5706 *
5707 * @since 5.6.0
5708 *
5709 * @param string $min_path Minimized path.
5710 * @param string $non_min_path Non-minimized path.
5711 * @return string The URL to the file
5712 */
5713 public static function get_file_url_for_environment( $min_path, $non_min_path ) {
5714 return Assets::get_file_url_for_environment( $min_path, $non_min_path );
5715 }
5716
5717 /**
5718 * Checks for whether Jetpack Backup is enabled.
5719 * Will return true if the state of Backup is anything except "unavailable".
5720 *
5721 * @return bool|int|mixed
5722 */
5723 public static function is_rewind_enabled() {
5724 // Rewind is a paid feature, therefore requires a user-level connection.
5725 if ( ! static::connection()->has_connected_owner() ) {
5726 return false;
5727 }
5728 $rewind_enabled = get_transient( 'jetpack_rewind_enabled' );
5729 $recheck = ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) && '0' === $rewind_enabled;
5730 if ( false === $rewind_enabled || $recheck ) {
5731 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.core-rest-api-endpoints.php';
5732 $rewind_data = (array) Jetpack_Core_Json_Api_Endpoints::rewind_data();
5733 $rewind_enabled = ( ! is_wp_error( $rewind_data )
5734 && ! empty( $rewind_data['state'] )
5735 && 'active' === $rewind_data['state'] )
5736 ? 1
5737 : 0;
5738 set_transient( 'jetpack_rewind_enabled', $rewind_enabled, 10 * MINUTE_IN_SECONDS );
5739 }
5740 return $rewind_enabled;
5741 }
5742
5743 /**
5744 * Return Calypso environment value; used for developing Jetpack and pairing
5745 * it with different Calypso enrionments, such as localhost.
5746 *
5747 * @deprecated 12.4 Moved to the Status package.
5748 *
5749 * @return string Calypso environment
5750 */
5751 public static function get_calypso_env() {
5752 _deprecated_function( __METHOD__, 'jetpack-12.4', '\Automattic\Jetpack\Status\Host->get_calypso_env' );
5753 return ( new Host() )->get_calypso_env();
5754 }
5755
5756 /**
5757 * Returns the hostname with protocol for Calypso.
5758 * Used for developing Jetpack with Calypso.
5759 *
5760 * @since 8.4.0
5761 *
5762 * @return string Calypso host.
5763 */
5764 public static function get_calypso_host() {
5765 $calypso_env = ( new Host() )->get_calypso_env();
5766 switch ( $calypso_env ) {
5767 case 'development':
5768 return 'http://calypso.localhost:3000/';
5769 case 'wpcalypso':
5770 return 'https://wpcalypso.wordpress.com/';
5771 case 'horizon':
5772 return 'https://horizon.wordpress.com/';
5773 default:
5774 return 'https://wordpress.com/';
5775 }
5776 }
5777
5778 /**
5779 * Handles activating default modules as well general cleanup for the new connection.
5780 *
5781 * @param boolean $activate_sso Whether to activate the SSO module when activating default modules.
5782 * @param boolean $redirect_on_activation_error Whether to redirect on activation error.
5783 * @param boolean $send_state_messages Whether to send state messages.
5784 * @return void
5785 */
5786 public static function handle_post_authorization_actions(
5787 $activate_sso = false,
5788 $redirect_on_activation_error = false,
5789 $send_state_messages = true
5790 ) {
5791 $other_modules = $activate_sso
5792 ? array( 'sso' )
5793 : array();
5794
5795 if ( Jetpack_Options::get_option( 'active_modules_initialized' ) ) {
5796 $active_modules = self::get_active_modules();
5797 self::delete_active_modules();
5798
5799 self::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), $redirect_on_activation_error, $send_state_messages );
5800 } else {
5801 // Default modules that don't require a user were already activated on site_register.
5802 // This time let's activate only those that require a user, this assures we don't reactivate manually deactivated modules while the site was connected only at a site level.
5803 self::activate_default_modules( false, false, $other_modules, $redirect_on_activation_error, $send_state_messages, null, true );
5804 Jetpack_Options::update_option( 'active_modules_initialized', true );
5805 }
5806
5807 // Since this is a fresh connection, be sure to clear out IDC options.
5808 Identity_Crisis::clear_all_idc_options();
5809
5810 if ( $send_state_messages ) {
5811 self::state( 'message', 'authorized' );
5812 }
5813 }
5814
5815 /**
5816 * Returns a boolean for whether backups UI should be displayed or not.
5817 *
5818 * @return bool Should backups UI be displayed?
5819 */
5820 public static function show_backups_ui() {
5821 /**
5822 * Whether UI for backups should be displayed.
5823 *
5824 * @since 6.5.0
5825 *
5826 * @param bool $show_backups Should UI for backups be displayed? True by default.
5827 */
5828 return self::is_plugin_active( 'vaultpress/vaultpress.php' ) || apply_filters( 'jetpack_show_backups', true );
5829 }
5830
5831 /**
5832 * Clean leftoveruser meta.
5833 *
5834 * Delete Jetpack-related user meta when it is no longer needed.
5835 *
5836 * @since 7.3.0
5837 *
5838 * @param int $user_id User ID being updated.
5839 */
5840 public static function user_meta_cleanup( $user_id ) {
5841 $meta_keys = array(
5842 // AtD removed from Jetpack 7.3.
5843 'AtD_options',
5844 'AtD_check_when',
5845 'AtD_guess_lang',
5846 'AtD_ignored_phrases',
5847 );
5848
5849 foreach ( $meta_keys as $meta_key ) {
5850 if ( get_user_meta( $user_id, $meta_key ) ) {
5851 delete_user_meta( $user_id, $meta_key );
5852 }
5853 }
5854 }
5855
5856 /**
5857 * Checks if a Jetpack site is both active and not in offline mode.
5858 *
5859 * This is a DRY function to avoid repeating `Jetpack::is_connection_ready && ! Automattic\Jetpack\Status->is_offline_mode`.
5860 *
5861 * @since 8.8.0
5862 *
5863 * @return bool True if Jetpack is active and not in offline mode.
5864 */
5865 public static function is_active_and_not_offline_mode() {
5866 if ( ! self::is_connection_ready() || ( new Status() )->is_offline_mode() ) {
5867 return false;
5868 }
5869 return true;
5870 }
5871
5872 /**
5873 * Returns the list of products that we have available for purchase.
5874 *
5875 * This method will not take current purchases or upgrades into account
5876 * but is instead a static list of products Jetpack offers with some
5877 * corresponding sales text/materials.
5878 *
5879 * @param bool $show_legacy Determine if we should include legacy product/plan details.
5880 * @return array
5881 */
5882 public static function get_products_for_purchase( $show_legacy = false ) {
5883 $products = array();
5884
5885 $products['backup'] = array(
5886 'title' => __( 'Jetpack Backup', 'jetpack' ),
5887 'slug' => 'jetpack_backup_t1_yearly',
5888 'description' => __( 'Never lose a word, image, page, or time worrying about your site with automated backups & one-click restores.', 'jetpack' ),
5889 'show_promotion' => true,
5890 'discount_percent' => 50,
5891 'included_in_plans' => array( 'security' ),
5892 'features' => array(
5893 _x( 'Real-time cloud backups', 'Backup Product Feature', 'jetpack' ),
5894 _x( '10GB of backup storage', 'Backup Product Feature', 'jetpack' ),
5895 _x( '30-day archive & activity log*', 'Backup Product Feature', 'jetpack' ),
5896 _x( 'One-click restores', 'Backup Product Feature', 'jetpack' ),
5897 ),
5898 'disclaimer' => array(
5899 'text' => __( '* Subject to your usage and storage limit.', 'jetpack' ),
5900 'link_text' => __( 'Learn more', 'jetpack' ),
5901 'url' => Redirect::get_url( 'jetpack-faq-backup-disclaimer' ),
5902 ),
5903 );
5904
5905 $products['creator'] = array(
5906 'title' => __( 'Jetpack Creator', 'jetpack' ),
5907 'slug' => 'jetpack_creator_yearly',
5908 'description' => __( 'Craft stunning content, boost your subscriber base, and monetize your online presence.', 'jetpack' ),
5909 'show_promotion' => true,
5910 'discount_percent' => 50,
5911 'included_in_plans' => array( 'complete' ),
5912 'features' => array(
5913 _x( 'Unlimited subscriber imports', 'Creator Product Feature', 'jetpack' ),
5914 _x( 'Earn more from your content', 'Creator Product Feature', 'jetpack' ),
5915 _x( 'Accept payments with PayPal', 'Creator Product Feature', 'jetpack' ),
5916 _x( 'Increase earnings with WordAds', 'Creator Product Feature', 'jetpack' ),
5917 ),
5918 );
5919
5920 $products['growth'] = array(
5921 'title' => __( 'Jetpack Growth', 'jetpack' ),
5922 'slug' => 'jetpack_growth_yearly',
5923 'description' => __( 'Essential tools to help you grow your audience, track visitor engagement, and turn leads into loyal customers and advocates.', 'jetpack' ),
5924 'show_promotion' => true,
5925 'discount_percent' => 50,
5926 'included_in_plans' => array( 'complete' ),
5927 'features' => array(
5928 _x( 'Jetpack Social', 'Growth Product Feature', 'jetpack' ),
5929 _x( 'Jetpack Stats (10K site views, upgradeable)', 'Growth Product Feature', 'jetpack' ),
5930 _x( 'Unlimited subscriber imports', 'Growth Product Feature', 'jetpack' ),
5931 _x( 'Earn more from your content', 'Growth Product Feature', 'jetpack' ),
5932 _x( 'Accept payments with PayPal', 'Growth Product Feature', 'jetpack' ),
5933 _x( 'Increase earnings with WordAds', 'Growth Product Feature', 'jetpack' ),
5934 ),
5935 );
5936
5937 $products['scan'] = array(
5938 'title' => __( 'Jetpack Scan', 'jetpack' ),
5939 'slug' => 'jetpack_scan',
5940 'description' => __( 'Automatic scanning and one-click fixes keep your site one step ahead of security threats and malware.', 'jetpack' ),
5941 'show_promotion' => true,
5942 'discount_percent' => 50,
5943 'included_in_plans' => array( 'security' ),
5944 'features' => array(
5945 _x( 'Automated daily scanning', 'Scan Product Feature', 'jetpack' ),
5946 _x( 'One-click fixes for most issues', 'Scan Product Feature', 'jetpack' ),
5947 _x( 'Instant email notifications', 'Scan Product Feature', 'jetpack' ),
5948 _x( 'Access to latest Firewall rules', 'Scan Product Feature', 'jetpack' ),
5949 ),
5950 );
5951
5952 $products['search'] = array(
5953 'title' => __( 'Jetpack Site Search', 'jetpack' ),
5954 'slug' => 'jetpack_search',
5955 'description' => __( 'Help your site visitors find answers instantly so they keep reading and buying. Great for sites with a lot of content.', 'jetpack' ),
5956 'show_promotion' => true,
5957 'discount_percent' => 50,
5958 'included_in_plans' => array(),
5959 'features' => array(
5960 _x( 'Instant search and indexing', 'Search Product Feature', 'jetpack' ),
5961 _x( 'Powerful filtering', 'Search Product Feature', 'jetpack' ),
5962 _x( 'Supports 38 languages', 'Search Product Feature', 'jetpack' ),
5963 _x( 'Spelling correction', 'Search Product Feature', 'jetpack' ),
5964 ),
5965 );
5966
5967 $products['akismet'] = array(
5968 'title' => __( 'Akismet Anti-spam', 'jetpack' ),
5969 'slug' => 'jetpack_anti_spam',
5970 'description' => __( 'Save time and get better responses by automatically blocking spam from your comments and forms.', 'jetpack' ),
5971 'show_promotion' => true,
5972 'discount_percent' => 50,
5973 'included_in_plans' => array( 'security' ),
5974 'features' => array(
5975 _x( 'Comment and form spam protection', 'Anti-Spam Product Feature', 'jetpack' ),
5976 _x( 'Block spam without CAPTCHAs', 'Anti-Spam Product Feature', 'jetpack' ),
5977 _x( 'Advanced stats', 'Anti-Spam Product Feature', 'jetpack' ),
5978 ),
5979 );
5980
5981 $products['security'] = array(
5982 'title' => _x( 'Security', 'Jetpack product name', 'jetpack' ),
5983 'slug' => 'jetpack_security_t1_yearly',
5984 'description' => __( 'Comprehensive site security, including Backup, Scan, and Anti-spam.', 'jetpack' ),
5985 'show_promotion' => true,
5986 'discount_percent' => 50,
5987 'included_in_plans' => array(),
5988 'features' => array(
5989 _x( 'Real-time cloud backups with 10GB storage', 'Security Tier 1 Feature', 'jetpack' ),
5990 _x( 'Automated real-time malware scan', 'Security Daily Plan Feature', 'jetpack' ),
5991 _x( 'One-click fixes for most threats', 'Security Daily Plan Feature', 'jetpack' ),
5992 _x( 'Comment & form spam protection', 'Security Daily Plan Feature', 'jetpack' ),
5993 ),
5994 );
5995
5996 $products['videopress'] = array(
5997 'title' => __( 'Jetpack VideoPress', 'jetpack' ),
5998 'slug' => 'jetpack_videopress',
5999 'description' => __( 'High-quality, ad-free video built specifically for WordPress.', 'jetpack' ),
6000 'show_promotion' => true,
6001 'discount_percent' => 50,
6002 'included_in_plans' => array(),
6003 'features' => array(
6004 _x( '1TB of storage', 'VideoPress Product Feature', 'jetpack' ),
6005 _x( 'Built into WordPress editor', 'VideoPress Product Feature', 'jetpack' ),
6006 _x( 'Ad-free and customizable player', 'VideoPress Product Feature', 'jetpack' ),
6007 _x( 'Unlimited users', 'VideoPress Product Feature', 'jetpack' ),
6008 ),
6009 );
6010
6011 if ( $show_legacy ) {
6012 $products['jetpack_backup_daily'] = array(
6013 'title' => __( 'Jetpack Backup', 'jetpack' ),
6014 'slug' => 'jetpack_backup_daily',
6015 'description' => __( 'Never lose a word, image, page, or time worrying about your site with automated backups & one-click restores.', 'jetpack' ),
6016 'show_promotion' => false,
6017 'discount_percent' => 0,
6018 'included_in_plans' => array(),
6019 'features' => array(
6020 _x( 'Automated daily backups (off-site)', 'Backup Product Feature', 'jetpack' ),
6021 _x( 'One-click restores', 'Backup Product Feature', 'jetpack' ),
6022 _x( 'Unlimited backup storage', 'Backup Product Feature', 'jetpack' ),
6023 ),
6024 );
6025 }
6026
6027 return $products;
6028 }
6029
6030 /**
6031 * Register product descriptions for partner coupon usage.
6032 *
6033 * @since 10.4.0
6034 *
6035 * @param array $products An array of registered products.
6036 *
6037 * @return array
6038 */
6039 public function get_partner_coupon_product_descriptions( $products ) {
6040 return array_merge( $products, self::get_products_for_purchase( true ) );
6041 }
6042
6043 /**
6044 * Determine if the current user is allowed to make Jetpack purchases without
6045 * a WordPress.com account
6046 *
6047 * @return boolean True if the user can make purchases, false if not
6048 */
6049 public static function current_user_can_purchase() {
6050
6051 // The site must be site-connected to Jetpack (no users connected).
6052 if ( ! self::connection()->is_site_connection() ) {
6053 return false;
6054 }
6055
6056 // Make sure only administrators can make purchases.
6057 if ( ! current_user_can( 'manage_options' ) ) {
6058 return false;
6059 }
6060
6061 return true;
6062 }
6063
6064 /**
6065 * Add 5-star review link on the Jetpack Plugin meta, in the plugins list table (on the plugins page).
6066 *
6067 * @param array $plugin_meta An array of the plugin's metadata.
6068 * @param string $plugin_file Path to the plugin file.
6069 *
6070 * @return array $plugin_meta An array of the plugin's metadata.
6071 */
6072 public function add_5_star_review_link( $plugin_meta, $plugin_file ) {
6073 if ( $plugin_file !== 'jetpack/jetpack.php' ) {
6074 return $plugin_meta;
6075 }
6076
6077 $u = get_current_user_id();
6078 $site = get_site_url();
6079
6080 $plugin_meta[] = '<a href="https://jetpack.com/redirect?source=jetpack-plugin-review&site=' . esc_attr( $site ) . '&u=' . esc_attr( $u ) . '" target="_blank" rel="noopener noreferrer" title="' . esc_attr__( 'Rate Jetpack on WordPress.org', 'jetpack' ) . '" style="color: #ffb900">'
6081 . str_repeat( '<span class="dashicons dashicons-star-filled" style="font-size: 16px; width:16px; height: 16px"></span>', 5 )
6082 . '</a>';
6083
6084 return $plugin_meta;
6085 }
6086
6087 /**
6088 * Lazy instantiation of the Plugin_Tracking object.
6089 *
6090 * @since 13.9
6091 *
6092 * @return void
6093 */
6094 public function initialize_tracking() {
6095 if ( did_action( 'jetpack_initialize_tracking' ) > 1 ) {
6096 // Only need to run once.
6097 return;
6098 }
6099
6100 if ( ( new Tracking( 'jetpack', $this->connection_manager ) )->should_enable_tracking( new Terms_Of_Service(), new Status() ) || static::is_connection_ready() ) {
6101 ( new Plugin_Tracking() )->init();
6102 }
6103 }
6104
6105 /**
6106 * Run the "initialize tracking" hook.
6107 *
6108 * @since 13.9
6109 */
6110 public function run_initialize_tracking_action() {
6111 /**
6112 * Fires when the tracking needs to be initialized.
6113 * Doesn't necessarily mean that will actually happen, depends if the 'jetpack_tos_agreed' option is set.
6114 *
6115 * @since 13.9
6116 */
6117 do_action( 'jetpack_initialize_tracking' );
6118 }
6119
6120 /**
6121 * Initialize REST jsonAPI if needed.
6122 *
6123 * @return void
6124 */
6125 public function maybe_initialize_rest_jsonapi() {
6126 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
6127 if ( ! empty( $_GET['jsonapi'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) ) {
6128 require_once ABSPATH . 'wp-admin/includes/admin.php'; // JSON API relies on WP functionality not autoloaded in REST.
6129
6130 define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
6131 require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
6132 }
6133 }
6134
6135 /**
6136 * Run plugin post-activation actions if we need to.
6137 *
6138 * @return void
6139 */
6140 private function plugin_post_activation() {
6141 if ( ( new Status() )->is_offline_mode() ) {
6142 return;
6143 }
6144
6145 if ( get_transient( 'activated_jetpack' ) ) {
6146 delete_transient( 'activated_jetpack' );
6147
6148 if ( ( new Host() )->is_woa_site() ) {
6149 $redirect_url = static::admin_url( 'page=jetpack' );
6150 } elseif ( is_network_admin() ) {
6151 $redirect_url = admin_url( 'network/admin.php?page=jetpack' );
6152 } elseif ( get_transient( 'my_jetpack_product_activated' ) ) {
6153 // don't redirect if this is an activation that just came from My Jetpack
6154 // My Jetpack has its own set of post-activation redirects
6155 return;
6156 } elseif ( My_Jetpack_Initializer::should_initialize() ) {
6157 $redirect_url = static::admin_url( 'page=my-jetpack' );
6158 } else {
6159 $redirect_url = static::admin_url( 'page=jetpack' );
6160 }
6161
6162 wp_safe_redirect( $redirect_url );
6163 }
6164 }
6165 }
6166