Completed
Push — rebecca/fix_12548 ( eedb5c )
by
unknown
11:14 queued 05:02
created

class.jetpack.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
4
use Automattic\Jetpack\Connection\Client;
5
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
6
use Automattic\Jetpack\Connection\REST_Connector as REST_Connector;
7
use Automattic\Jetpack\Connection\XMLRPC_Connector as XMLRPC_Connector;
8
use Automattic\Jetpack\Constants;
9
use Automattic\Jetpack\Roles;
10
use Automattic\Jetpack\Sync\Functions;
11
use Automattic\Jetpack\Sync\Sender;
12
use Automattic\Jetpack\Sync\Users;
13
use Automattic\Jetpack\Tracking;
14
use Automattic\Jetpack\Assets;
15
16
/*
17
Options:
18
jetpack_options (array)
19
	An array of options.
20
	@see Jetpack_Options::get_option_names()
21
22
jetpack_register (string)
23
	Temporary verification secrets.
24
25
jetpack_activated (int)
26
	1: the plugin was activated normally
27
	2: the plugin was activated on this site because of a network-wide activation
28
	3: the plugin was auto-installed
29
	4: the plugin was manually disconnected (but is still installed)
30
31
jetpack_active_modules (array)
32
	Array of active module slugs.
33
34
jetpack_do_activate (bool)
35
	Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
36
*/
37
38
require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php';
39
40
class Jetpack {
41
	public $xmlrpc_server = null;
42
43
	private $rest_authentication_status = null;
44
45
	public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA']
46
47
	private $tracking;
48
49
	/**
50
	 * @var array The handles of styles that are concatenated into jetpack.css.
51
	 *
52
	 * When making changes to that list, you must also update concat_list in tools/builder/frontend-css.js.
53
	 */
54
	public $concatenated_style_handles = array(
55
		'jetpack-carousel',
56
		'grunion.css',
57
		'the-neverending-homepage',
58
		'jetpack_likes',
59
		'jetpack_related-posts',
60
		'sharedaddy',
61
		'jetpack-slideshow',
62
		'presentations',
63
		'quiz',
64
		'jetpack-subscriptions',
65
		'jetpack-responsive-videos-style',
66
		'jetpack-social-menu',
67
		'tiled-gallery',
68
		'jetpack_display_posts_widget',
69
		'gravatar-profile-widget',
70
		'goodreads-widget',
71
		'jetpack_social_media_icons_widget',
72
		'jetpack-top-posts-widget',
73
		'jetpack_image_widget',
74
		'jetpack-my-community-widget',
75
		'jetpack-authors-widget',
76
		'wordads',
77
		'eu-cookie-law-style',
78
		'flickr-widget-style',
79
		'jetpack-search-widget',
80
		'jetpack-simple-payments-widget-style',
81
		'jetpack-widget-social-icons-styles',
82
	);
83
84
	/**
85
	 * The handles of scripts that can be loaded asynchronously.
86
	 *
87
	 * @var array
88
	 */
89
	public $async_script_handles = array(
90
		'woocommerce-analytics',
91
	);
92
93
	/**
94
	 * Contains all assets that have had their URL rewritten to minified versions.
95
	 *
96
	 * @var array
97
	 */
98
	static $min_assets = array();
99
100
	public $plugins_to_deactivate = array(
101
		'stats'               => array( 'stats/stats.php', 'WordPress.com Stats' ),
102
		'shortlinks'          => array( 'stats/stats.php', 'WordPress.com Stats' ),
103
		'sharedaddy'          => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
104
		'twitter-widget'      => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
105
		'contact-form'        => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
106
		'contact-form'        => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
107
		'custom-css'          => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
108
		'random-redirect'     => array( 'random-redirect/random-redirect.php', 'Random Redirect' ),
109
		'videopress'          => array( 'video/video.php', 'VideoPress' ),
110
		'widget-visibility'   => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),
111
		'widget-visibility'   => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),
112
		'sharedaddy'          => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),
113
		'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
114
		'latex'               => array( 'wp-latex/wp-latex.php', 'WP LaTeX' ),
115
	);
116
117
	/**
118
	 * Map of roles we care about, and their corresponding minimum capabilities.
119
	 *
120
	 * @deprecated 7.6 Use Automattic\Jetpack\Roles::$capability_translations instead.
121
	 *
122
	 * @access public
123
	 * @static
124
	 *
125
	 * @var array
126
	 */
127
	public static $capability_translations = array(
128
		'administrator' => 'manage_options',
129
		'editor'        => 'edit_others_posts',
130
		'author'        => 'publish_posts',
131
		'contributor'   => 'edit_posts',
132
		'subscriber'    => 'read',
133
	);
134
135
	/**
136
	 * Map of modules that have conflicts with plugins and should not be auto-activated
137
	 * if the plugins are active.  Used by filter_default_modules
138
	 *
139
	 * Plugin Authors: If you'd like to prevent a single module from auto-activating,
140
	 * change `module-slug` and add this to your plugin:
141
	 *
142
	 * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' );
143
	 * function my_jetpack_get_default_modules( $modules ) {
144
	 *     return array_diff( $modules, array( 'module-slug' ) );
145
	 * }
146
	 *
147
	 * @var array
148
	 */
149
	private $conflicting_plugins = array(
150
		'comments'           => array(
151
			'Intense Debate'                 => 'intensedebate/intensedebate.php',
152
			'Disqus'                         => 'disqus-comment-system/disqus.php',
153
			'Livefyre'                       => 'livefyre-comments/livefyre.php',
154
			'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php',
155
			'Google+ Comments'               => 'google-plus-comments/google-plus-comments.php',
156
			'WP-SpamShield Anti-Spam'        => 'wp-spamshield/wp-spamshield.php',
157
		),
158
		'comment-likes'      => array(
159
			'Epoch' => 'epoch/plugincore.php',
160
		),
161
		'contact-form'       => array(
162
			'Contact Form 7'           => 'contact-form-7/wp-contact-form-7.php',
163
			'Gravity Forms'            => 'gravityforms/gravityforms.php',
164
			'Contact Form Plugin'      => 'contact-form-plugin/contact_form.php',
165
			'Easy Contact Forms'       => 'easy-contact-forms/easy-contact-forms.php',
166
			'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php',
167
			'Ninja Forms'              => 'ninja-forms/ninja-forms.php',
168
		),
169
		'minileven'          => array(
170
			'WPtouch' => 'wptouch/wptouch.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
		'random-redirect'    => array(
196
			'Random Redirect 2' => 'random-redirect-2/random-redirect.php',
197
		),
198
		'related-posts'      => array(
199
			'YARPP'                       => 'yet-another-related-posts-plugin/yarpp.php',
200
			'WordPress Related Posts'     => 'wordpress-23-related-posts-plugin/wp_related_posts.php',
201
			'nrelate Related Content'     => 'nrelate-related-content/nrelate-related.php',
202
			'Contextual Related Posts'    => 'contextual-related-posts/contextual-related-posts.php',
203
			'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php',
204
			'outbrain'                    => 'outbrain/outbrain.php',
205
			'Shareaholic'                 => 'shareaholic/shareaholic.php',
206
			'Sexybookmarks'               => 'sexybookmarks/shareaholic.php',
207
		),
208
		'sharedaddy'         => array(
209
			'AddThis'     => 'addthis/addthis_social_widget.php',
210
			'Add To Any'  => 'add-to-any/add-to-any.php',
211
			'ShareThis'   => 'share-this/sharethis.php',
212
			'Shareaholic' => 'shareaholic/shareaholic.php',
213
		),
214
		'seo-tools'          => array(
215
			'WordPress SEO by Yoast'         => 'wordpress-seo/wp-seo.php',
216
			'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
217
			'All in One SEO Pack'            => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
218
			'All in One SEO Pack Pro'        => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
219
			'The SEO Framework'              => 'autodescription/autodescription.php',
220
		),
221
		'verification-tools' => array(
222
			'WordPress SEO by Yoast'         => 'wordpress-seo/wp-seo.php',
223
			'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
224
			'All in One SEO Pack'            => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
225
			'All in One SEO Pack Pro'        => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
226
			'The SEO Framework'              => 'autodescription/autodescription.php',
227
		),
228
		'widget-visibility'  => array(
229
			'Widget Logic'    => 'widget-logic/widget_logic.php',
230
			'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php',
231
		),
232
		'sitemaps'           => array(
233
			'Google XML Sitemaps'                  => 'google-sitemap-generator/sitemap.php',
234
			'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php',
235
			'Google XML Sitemaps for qTranslate'   => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php',
236
			'XML Sitemap & Google News feeds'      => 'xml-sitemap-feed/xml-sitemap.php',
237
			'Google Sitemap by BestWebSoft'        => 'google-sitemap-plugin/google-sitemap-plugin.php',
238
			'WordPress SEO by Yoast'               => 'wordpress-seo/wp-seo.php',
239
			'WordPress SEO Premium by Yoast'       => 'wordpress-seo-premium/wp-seo-premium.php',
240
			'All in One SEO Pack'                  => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
241
			'All in One SEO Pack Pro'              => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
242
			'The SEO Framework'                    => 'autodescription/autodescription.php',
243
			'Sitemap'                              => 'sitemap/sitemap.php',
244
			'Simple Wp Sitemap'                    => 'simple-wp-sitemap/simple-wp-sitemap.php',
245
			'Simple Sitemap'                       => 'simple-sitemap/simple-sitemap.php',
246
			'XML Sitemaps'                         => 'xml-sitemaps/xml-sitemaps.php',
247
			'MSM Sitemaps'                         => 'msm-sitemap/msm-sitemap.php',
248
		),
249
		'lazy-images'        => array(
250
			'Lazy Load'              => 'lazy-load/lazy-load.php',
251
			'BJ Lazy Load'           => 'bj-lazy-load/bj-lazy-load.php',
252
			'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php',
253
		),
254
	);
255
256
	/**
257
	 * Plugins for which we turn off our Facebook OG Tags implementation.
258
	 *
259
	 * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate
260
	 * Jetpack's Open Graph tags via filter when their Social Meta modules are active.
261
	 *
262
	 * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter:
263
	 * add_filter( 'jetpack_enable_open_graph', '__return_false' );
264
	 */
265
	private $open_graph_conflicting_plugins = array(
266
		'2-click-socialmedia-buttons/2-click-socialmedia-buttons.php',
267
		// 2 Click Social Media Buttons
268
		'add-link-to-facebook/add-link-to-facebook.php',         // Add Link to Facebook
269
		'add-meta-tags/add-meta-tags.php',                       // Add Meta Tags
270
		'easy-facebook-share-thumbnails/esft.php',               // Easy Facebook Share Thumbnail
271
		'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php',
272
		// Open Graph Meta Tags by Heateor
273
		'facebook/facebook.php',                                 // Facebook (official plugin)
274
		'facebook-awd/AWD_facebook.php',                         // Facebook AWD All in one
275
		'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php',
276
		// Facebook Featured Image & OG Meta Tags
277
		'facebook-meta-tags/facebook-metatags.php',              // Facebook Meta Tags
278
		'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php',
279
		// Facebook Open Graph Meta Tags for WordPress
280
		'facebook-revised-open-graph-meta-tag/index.php',        // Facebook Revised Open Graph Meta Tag
281
		'facebook-thumb-fixer/_facebook-thumb-fixer.php',        // Facebook Thumb Fixer
282
		'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php',
283
		// Fedmich's Facebook Open Graph Meta
284
		'network-publisher/networkpub.php',                      // Network Publisher
285
		'nextgen-facebook/nextgen-facebook.php',                 // NextGEN Facebook OG
286
		'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php',
287
		// NextScripts SNAP
288
		'og-tags/og-tags.php',                                   // OG Tags
289
		'opengraph/opengraph.php',                               // Open Graph
290
		'open-graph-protocol-framework/open-graph-protocol-framework.php',
291
		// Open Graph Protocol Framework
292
		'seo-facebook-comments/seofacebook.php',                 // SEO Facebook Comments
293
		'seo-ultimate/seo-ultimate.php',                         // SEO Ultimate
294
		'sexybookmarks/sexy-bookmarks.php',                      // Shareaholic
295
		'shareaholic/sexy-bookmarks.php',                        // Shareaholic
296
		'sharepress/sharepress.php',                             // SharePress
297
		'simple-facebook-connect/sfc.php',                       // Simple Facebook Connect
298
		'social-discussions/social-discussions.php',             // Social Discussions
299
		'social-sharing-toolkit/social_sharing_toolkit.php',     // Social Sharing Toolkit
300
		'socialize/socialize.php',                               // Socialize
301
		'squirrly-seo/squirrly.php',                             // SEO by SQUIRRLY™
302
		'only-tweet-like-share-and-google-1/tweet-like-plusone.php',
303
		// 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',
308
		// WP Facebook Like Send & Open Graph Meta
309
		'wp-facebook-open-graph-protocol/wp-facebook-ogp.php',   // WP Facebook Open Graph protocol
310
		'wp-ogp/wp-ogp.php',                                     // WP-OGP
311
		'zoltonorg-social-plugin/zosp.php',                      // Zolton.org Social Plugin
312
		'wp-fb-share-like-button/wp_fb_share-like_widget.php',   // WP Facebook Like Button
313
		'open-graph-metabox/open-graph-metabox.php',              // Open Graph Metabox
314
	);
315
316
	/**
317
	 * Plugins for which we turn off our Twitter Cards Tags implementation.
318
	 */
319
	private $twitter_cards_conflicting_plugins = array(
320
		// 'twitter/twitter.php',                       // The official one handles this on its own.
321
		// https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php
322
			'eewee-twitter-card/index.php',              // Eewee Twitter Card
323
		'ig-twitter-cards/ig-twitter-cards.php',     // IG:Twitter Cards
324
		'jm-twitter-cards/jm-twitter-cards.php',     // JM Twitter Cards
325
		'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php',
326
		// Pure Web Brilliant's Social Graph Twitter Cards Extension
327
		'twitter-cards/twitter-cards.php',           // Twitter Cards
328
		'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta
329
		'wp-to-twitter/wp-to-twitter.php',           // WP to Twitter
330
		'wp-twitter-cards/twitter_cards.php',        // WP Twitter Cards
331
	);
332
333
	/**
334
	 * Message to display in admin_notice
335
	 *
336
	 * @var string
337
	 */
338
	public $message = '';
339
340
	/**
341
	 * Error to display in admin_notice
342
	 *
343
	 * @var string
344
	 */
345
	public $error = '';
346
347
	/**
348
	 * Modules that need more privacy description.
349
	 *
350
	 * @var string
351
	 */
352
	public $privacy_checks = '';
353
354
	/**
355
	 * Stats to record once the page loads
356
	 *
357
	 * @var array
358
	 */
359
	public $stats = array();
360
361
	/**
362
	 * Jetpack_Sync object
363
	 */
364
	public $sync;
365
366
	/**
367
	 * Verified data for JSON authorization request
368
	 */
369
	public $json_api_authorization_request = array();
370
371
	/**
372
	 * @var Automattic\Jetpack\Connection\Manager
373
	 */
374
	protected $connection_manager;
375
376
	/**
377
	 * @var string Transient key used to prevent multiple simultaneous plugin upgrades
378
	 */
379
	public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock';
380
381
	/**
382
	 * Holds the singleton instance of this class
383
	 *
384
	 * @since 2.3.3
385
	 * @var Jetpack
386
	 */
387
	static $instance = false;
388
389
	/**
390
	 * Singleton
391
	 *
392
	 * @static
393
	 */
394
	public static function init() {
395
		if ( ! self::$instance ) {
396
			self::$instance = new Jetpack();
397
398
			self::$instance->plugin_upgrade();
399
		}
400
401
		return self::$instance;
402
	}
403
404
	/**
405
	 * Must never be called statically
406
	 */
407
	function plugin_upgrade() {
408
		if ( self::is_active() ) {
409
			list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
410
			if ( JETPACK__VERSION != $version ) {
411
				// Prevent multiple upgrades at once - only a single process should trigger
412
				// an upgrade to avoid stampedes
413
				if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) {
414
					return;
415
				}
416
417
				// Set a short lock to prevent multiple instances of the upgrade
418
				set_transient( self::$plugin_upgrade_lock_key, 1, 10 );
419
420
				// check which active modules actually exist and remove others from active_modules list
421
				$unfiltered_modules = self::get_active_modules();
422
				$modules            = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) );
423
				if ( array_diff( $unfiltered_modules, $modules ) ) {
424
					self::update_active_modules( $modules );
425
				}
426
427
				add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
428
429
				// Upgrade to 4.3.0
430
				if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) {
431
					Jetpack_Options::delete_option( 'identity_crisis_whitelist' );
432
				}
433
434
				// Make sure Markdown for posts gets turned back on
435
				if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) {
436
					update_option( 'wpcom_publish_posts_with_markdown', true );
437
				}
438
439
				if ( did_action( 'wp_loaded' ) ) {
440
					self::upgrade_on_load();
441
				} else {
442
					add_action(
443
						'wp_loaded',
444
						array( __CLASS__, 'upgrade_on_load' )
445
					);
446
				}
447
			}
448
		}
449
	}
450
451
	/**
452
	 * Runs upgrade routines that need to have modules loaded.
453
	 */
454
	static function upgrade_on_load() {
455
456
		// Not attempting any upgrades if jetpack_modules_loaded did not fire.
457
		// This can happen in case Jetpack has been just upgraded and is
458
		// being initialized late during the page load. In this case we wait
459
		// until the next proper admin page load with Jetpack active.
460
		if ( ! did_action( 'jetpack_modules_loaded' ) ) {
461
			delete_transient( self::$plugin_upgrade_lock_key );
462
463
			return;
464
		}
465
466
		self::maybe_set_version_option();
467
468
		if ( method_exists( 'Jetpack_Widget_Conditions', 'migrate_post_type_rules' ) ) {
469
			Jetpack_Widget_Conditions::migrate_post_type_rules();
470
		}
471
472
		if (
473
			class_exists( 'Jetpack_Sitemap_Manager' )
474
			&& version_compare( JETPACK__VERSION, '5.3', '>=' )
475
		) {
476
			do_action( 'jetpack_sitemaps_purge_data' );
477
		}
478
479
		// Delete old stats cache
480
		delete_option( 'jetpack_restapi_stats_cache' );
481
482
		delete_transient( self::$plugin_upgrade_lock_key );
483
	}
484
485
	/**
486
	 * Saves all the currently active modules to options.
487
	 * Also fires Action hooks for each newly activated and deactivated module.
488
	 *
489
	 * @param $modules Array Array of active modules to be saved in options.
490
	 *
491
	 * @return $success bool true for success, false for failure.
492
	 */
493
	static function update_active_modules( $modules ) {
494
		$current_modules      = Jetpack_Options::get_option( 'active_modules', array() );
495
		$active_modules       = self::get_active_modules();
496
		$new_active_modules   = array_diff( $modules, $current_modules );
497
		$new_inactive_modules = array_diff( $active_modules, $modules );
498
		$new_current_modules  = array_diff( array_merge( $current_modules, $new_active_modules ), $new_inactive_modules );
499
		$reindexed_modules    = array_values( $new_current_modules );
500
		$success              = Jetpack_Options::update_option( 'active_modules', array_unique( $reindexed_modules ) );
501
502
		foreach ( $new_active_modules as $module ) {
503
			/**
504
			 * Fires when a specific module is activated.
505
			 *
506
			 * @since 1.9.0
507
			 *
508
			 * @param string $module Module slug.
509
			 * @param boolean $success whether the module was activated. @since 4.2
510
			 */
511
			do_action( 'jetpack_activate_module', $module, $success );
512
			/**
513
			 * Fires when a module is activated.
514
			 * The dynamic part of the filter, $module, is the module slug.
515
			 *
516
			 * @since 1.9.0
517
			 *
518
			 * @param string $module Module slug.
519
			 */
520
			do_action( "jetpack_activate_module_$module", $module );
521
		}
522
523
		foreach ( $new_inactive_modules as $module ) {
524
			/**
525
			 * Fired after a module has been deactivated.
526
			 *
527
			 * @since 4.2.0
528
			 *
529
			 * @param string $module Module slug.
530
			 * @param boolean $success whether the module was deactivated.
531
			 */
532
			do_action( 'jetpack_deactivate_module', $module, $success );
533
			/**
534
			 * Fires when a module is deactivated.
535
			 * The dynamic part of the filter, $module, is the module slug.
536
			 *
537
			 * @since 1.9.0
538
			 *
539
			 * @param string $module Module slug.
540
			 */
541
			do_action( "jetpack_deactivate_module_$module", $module );
542
		}
543
544
		return $success;
545
	}
546
547
	static function delete_active_modules() {
548
		self::update_active_modules( array() );
549
	}
550
551
	/**
552
	 * Constructor.  Initializes WordPress hooks
553
	 */
554
	private function __construct() {
555
		/*
556
		 * Check for and alert any deprecated hooks
557
		 */
558
		add_action( 'init', array( $this, 'deprecated_hooks' ) );
559
560
		/*
561
		 * Enable enhanced handling of previewing sites in Calypso
562
		 */
563
		if ( self::is_active() ) {
564
			require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
565
			add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );
566
			require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php';
567
			add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 );
568
		}
569
570
		if ( self::jetpack_tos_agreed() ) {
571
			$tracking = new Automattic\Jetpack\Plugin\Tracking();
572
			add_action( 'init', array( $tracking, 'init' ) );
573
		}
574
575
		add_filter(
576
			'jetpack_connection_secret_generator',
577
			function( $callable ) {
578
				return function() {
579
					return wp_generate_password( 32, false );
580
				};
581
			}
582
		);
583
584
		add_action( 'jetpack_verify_signature_error', array( $this, 'track_xmlrpc_error' ) );
585
586
		$this->connection_manager = new Connection_Manager();
587
		$this->connection_manager->init();
588
589
		/*
590
		 * Load things that should only be in Network Admin.
591
		 *
592
		 * For now blow away everything else until a more full
593
		 * understanding of what is needed at the network level is
594
		 * available
595
		 */
596
		if ( is_multisite() ) {
597
			$network = Jetpack_Network::init();
598
			$network->set_connection( $this->connection_manager );
599
		}
600
601
		add_filter(
602
			'jetpack_signature_check_token',
603
			array( __CLASS__, 'verify_onboarding_token' ),
604
			10,
605
			3
606
		);
607
608
		/**
609
		 * Prepare Gutenberg Editor functionality
610
		 */
611
		require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
612
		Jetpack_Gutenberg::init();
613
		Jetpack_Gutenberg::load_independent_blocks();
614
		add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
615
616
		add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );
617
618
		// Unlink user before deleting the user from WP.com.
619
		add_action( 'deleted_user', array( 'Automattic\\Jetpack\\Connection\\Manager', 'disconnect_user' ), 10, 1 );
620
		add_action( 'remove_user_from_blog', array( 'Automattic\\Jetpack\\Connection\\Manager', 'disconnect_user' ), 10, 1 );
621
622
		// Initialize remote file upload request handlers.
623
		$this->add_remote_request_handlers();
624
625
		if ( self::is_active() ) {
626
			add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) );
627
628
			Jetpack_Heartbeat::init();
629
			if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) {
630
				require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
631
				Jetpack_Search_Performance_Logger::init();
632
			}
633
		}
634
635
		add_action( 'jetpack_event_log', array( 'Jetpack', 'log' ), 10, 2 );
636
637
		add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) );
638
		add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) );
639
640
		add_action( 'admin_init', array( $this, 'admin_init' ) );
641
		add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
642
643
		add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
644
645
		add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
646
		// Filter the dashboard meta box order to swap the new one in in place of the old one.
647
		add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) );
648
649
		// returns HTTPS support status
650
		add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) );
651
652
		// JITM AJAX callback function
653
		add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) );
654
655
		add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) );
656
657
		add_action( 'wp_loaded', array( $this, 'register_assets' ) );
658
		add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
659
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
660
		add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
661
662
		add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 );
663
664
		/**
665
		 * These actions run checks to load additional files.
666
		 * They check for external files or plugins, so they need to run as late as possible.
667
		 */
668
		add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 );
669
		add_action( 'amp_story_head', array( $this, 'check_open_graph' ), 1 );
670
		add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 );
671
		add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
672
673
		add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 );
674
		add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 );
675
		add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 );
676
677
		add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 );
678
		add_filter( 'profile_update', array( 'Jetpack', 'user_meta_cleanup' ) );
679
680
		add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
681
		add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
682
683
		// A filter to control all just in time messages
684
		add_filter( 'jetpack_just_in_time_msgs', array( $this, 'is_active_and_not_development_mode' ), 9 );
685
686
		add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9 );
687
688
		// If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin.
689
		// We should make sure to only do this for front end links.
690
		if ( self::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) {
691
			add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 );
692
			add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 );
693
694
			// we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions
695
			// so they point moderation links on emails to Calypso
696
			jetpack_require_lib( 'functions.wp-notify' );
697
		}
698
699
		// Hide edit post link if mobile app.
700
		if ( Jetpack_User_Agent_Info::is_mobile_app() ) {
701
			add_filter( 'edit_post_link', '__return_empty_string' );
702
		}
703
704
		// Update the Jetpack plan from API on heartbeats
705
		add_action( 'jetpack_heartbeat', array( 'Jetpack_Plan', 'refresh_from_wpcom' ) );
706
707
		/**
708
		 * This is the hack to concatenate all css files into one.
709
		 * For description and reasoning see the implode_frontend_css method
710
		 *
711
		 * Super late priority so we catch all the registered styles
712
		 */
713
		if ( ! is_admin() ) {
714
			add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first
715
			add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles`
716
		}
717
718
		/**
719
		 * These are sync actions that we need to keep track of for jitms
720
		 */
721
		add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 );
722
723
		// Actually push the stats on shutdown.
724
		if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
725
			add_action( 'shutdown', array( $this, 'push_stats' ) );
726
		}
727
728
		/*
729
		 * Load some scripts asynchronously.
730
		 */
731
		add_action( 'script_loader_tag', array( $this, 'script_add_async' ), 10, 3 );
732
	}
733
734
	/**
735
	 * Sets up the XMLRPC request handlers.
736
	 *
737
	 * @deprecated since 7.7.0
738
	 * @see Automattic\Jetpack\Connection\Manager::setup_xmlrpc_handlers()
739
	 *
740
	 * @param Array                 $request_params Incoming request parameters.
741
	 * @param Boolean               $is_active      Whether the connection is currently active.
742
	 * @param Boolean               $is_signed      Whether the signature check has been successful.
743
	 * @param Jetpack_XMLRPC_Server $xmlrpc_server  (optional) An instance of the server to use instead of instantiating a new one.
744
	 */
745
	public function setup_xmlrpc_handlers(
746
		$request_params,
747
		$is_active,
748
		$is_signed,
749
		Jetpack_XMLRPC_Server $xmlrpc_server = null
750
	) {
751
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::setup_xmlrpc_handlers' );
752
		return $this->connection_manager->setup_xmlrpc_handlers(
753
			$request_params,
754
			$is_active,
755
			$is_signed,
756
			$xmlrpc_server
757
		);
758
	}
759
760
	/**
761
	 * Initialize REST API registration connector.
762
	 *
763
	 * @deprecated since 7.7.0
764
	 * @see Automattic\Jetpack\Connection\Manager::initialize_rest_api_registration_connector()
765
	 */
766
	public function initialize_rest_api_registration_connector() {
767
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::initialize_rest_api_registration_connector' );
768
		$this->connection_manager->initialize_rest_api_registration_connector();
769
	}
770
771
	/**
772
	 * This is ported over from the manage module, which has been deprecated and baked in here.
773
	 *
774
	 * @param $domains
775
	 */
776
	function add_wpcom_to_allowed_redirect_hosts( $domains ) {
777
		add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_domain' ) );
778
	}
779
780
	/**
781
	 * Return $domains, with 'wordpress.com' appended.
782
	 * This is ported over from the manage module, which has been deprecated and baked in here.
783
	 *
784
	 * @param $domains
785
	 * @return array
786
	 */
787
	function allow_wpcom_domain( $domains ) {
788
		if ( empty( $domains ) ) {
789
			$domains = array();
790
		}
791
		$domains[] = 'wordpress.com';
792
		return array_unique( $domains );
793
	}
794
795
	function point_edit_post_links_to_calypso( $default_url, $post_id ) {
796
		$post = get_post( $post_id );
797
798
		if ( empty( $post ) ) {
799
			return $default_url;
800
		}
801
802
		$post_type = $post->post_type;
803
804
		// Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso.
805
		// https://en.support.wordpress.com/custom-post-types/
806
		$allowed_post_types = array(
807
			'post'                => 'post',
808
			'page'                => 'page',
809
			'jetpack-portfolio'   => 'edit/jetpack-portfolio',
810
			'jetpack-testimonial' => 'edit/jetpack-testimonial',
811
		);
812
813
		if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) {
814
			return $default_url;
815
		}
816
817
		$path_prefix = $allowed_post_types[ $post_type ];
818
819
		$site_slug = self::build_raw_urls( get_home_url() );
820
821
		return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) );
822
	}
823
824
	function point_edit_comment_links_to_calypso( $url ) {
825
		// Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID.
826
		wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args );
827
		return esc_url(
828
			sprintf(
829
				'https://wordpress.com/comment/%s/%d',
830
				self::build_raw_urls( get_home_url() ),
831
				$query_args['amp;c']
832
			)
833
		);
834
	}
835
836
	function jetpack_track_last_sync_callback( $params ) {
837
		/**
838
		 * Filter to turn off jitm caching
839
		 *
840
		 * @since 5.4.0
841
		 *
842
		 * @param bool false Whether to cache just in time messages
843
		 */
844
		if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) {
845
			return $params;
846
		}
847
848
		if ( is_array( $params ) && isset( $params[0] ) ) {
849
			$option = $params[0];
850
			if ( 'active_plugins' === $option ) {
851
				// use the cache if we can, but not terribly important if it gets evicted
852
				set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS );
853
			}
854
		}
855
856
		return $params;
857
	}
858
859
	function jetpack_connection_banner_callback() {
860
		check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' );
861
862
		if ( isset( $_REQUEST['dismissBanner'] ) ) {
863
			Jetpack_Options::update_option( 'dismissed_connection_banner', 1 );
864
			wp_send_json_success();
865
		}
866
867
		wp_die();
868
	}
869
870
	/**
871
	 * Removes all XML-RPC methods that are not `jetpack.*`.
872
	 * Only used in our alternate XML-RPC endpoint, where we want to
873
	 * ensure that Core and other plugins' methods are not exposed.
874
	 *
875
	 * @deprecated since 7.7.0
876
	 * @see Automattic\Jetpack\Connection\Manager::remove_non_jetpack_xmlrpc_methods()
877
	 *
878
	 * @param array $methods A list of registered WordPress XMLRPC methods.
879
	 * @return array Filtered $methods
880
	 */
881
	public function remove_non_jetpack_xmlrpc_methods( $methods ) {
882
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::remove_non_jetpack_xmlrpc_methods' );
883
		return $this->connection_manager->remove_non_jetpack_xmlrpc_methods( $methods );
884
	}
885
886
	/**
887
	 * Since a lot of hosts use a hammer approach to "protecting" WordPress sites,
888
	 * and just blanket block all requests to /xmlrpc.php, or apply other overly-sensitive
889
	 * security/firewall policies, we provide our own alternate XML RPC API endpoint
890
	 * which is accessible via a different URI. Most of the below is copied directly
891
	 * from /xmlrpc.php so that we're replicating it as closely as possible.
892
	 *
893
	 * @deprecated since 7.7.0
894
	 * @see Automattic\Jetpack\Connection\Manager::alternate_xmlrpc()
895
	 */
896
	public function alternate_xmlrpc() {
897
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::alternate_xmlrpc' );
898
		$this->connection_manager->alternate_xmlrpc();
899
	}
900
901
	/**
902
	 * The callback for the JITM ajax requests.
903
	 */
904
	function jetpack_jitm_ajax_callback() {
905
		// Check for nonce
906
		if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) {
907
			wp_die( 'Module activation failed due to lack of appropriate permissions' );
908
		}
909
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) {
910
			$module_slug = $_REQUEST['jitmModule'];
911
			self::log( 'activate', $module_slug );
912
			self::activate_module( $module_slug, false, false );
913
			self::state( 'message', 'no_message' );
914
915
			// A Jetpack module is being activated through a JITM, track it
916
			$this->stat( 'jitm', $module_slug . '-activated-' . JETPACK__VERSION );
917
			$this->do_stats( 'server_side' );
918
919
			wp_send_json_success();
920
		}
921
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) {
922
			// get the hide_jitm options array
923
			$jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
924
			$module_slug       = $_REQUEST['jitmModule'];
925
926
			if ( ! $jetpack_hide_jitm ) {
927
				$jetpack_hide_jitm = array(
928
					$module_slug => 'hide',
929
				);
930
			} else {
931
				$jetpack_hide_jitm[ $module_slug ] = 'hide';
932
			}
933
934
			Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm );
935
936
			// jitm is being dismissed forever, track it
937
			$this->stat( 'jitm', $module_slug . '-dismissed-' . JETPACK__VERSION );
938
			$this->do_stats( 'server_side' );
939
940
			wp_send_json_success();
941
		}
942 View Code Duplication
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) {
943
			$module_slug = $_REQUEST['jitmModule'];
944
945
			// User went to WordPress.com, track this
946
			$this->stat( 'jitm', $module_slug . '-wordpress-tools-' . JETPACK__VERSION );
947
			$this->do_stats( 'server_side' );
948
949
			wp_send_json_success();
950
		}
951 View Code Duplication
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) {
952
			$track = $_REQUEST['jitmModule'];
953
954
			// User is viewing JITM, track it.
955
			$this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION );
956
			$this->do_stats( 'server_side' );
957
958
			wp_send_json_success();
959
		}
960
	}
961
962
	/**
963
	 * If there are any stats that need to be pushed, but haven't been, push them now.
964
	 */
965
	function push_stats() {
966
		if ( ! empty( $this->stats ) ) {
967
			$this->do_stats( 'server_side' );
968
		}
969
	}
970
971
	function jetpack_custom_caps( $caps, $cap, $user_id, $args ) {
972
		switch ( $cap ) {
973
			case 'jetpack_connect':
974
			case 'jetpack_reconnect':
975
				if ( self::is_development_mode() ) {
976
					$caps = array( 'do_not_allow' );
977
					break;
978
				}
979
				/**
980
				 * Pass through. If it's not development mode, these should match disconnect.
981
				 * Let users disconnect if it's development mode, just in case things glitch.
982
				 */
983
			case 'jetpack_disconnect':
984
				/**
985
				 * In multisite, can individual site admins manage their own connection?
986
				 *
987
				 * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class.
988
				 */
989
				if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
990
					if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) {
991
						/**
992
						 * We need to update the option name -- it's terribly unclear which
993
						 * direction the override goes.
994
						 *
995
						 * @todo: Update the option name to `sub-sites-can-manage-own-connections`
996
						 */
997
						$caps = array( 'do_not_allow' );
998
						break;
999
					}
1000
				}
1001
1002
				$caps = array( 'manage_options' );
1003
				break;
1004
			case 'jetpack_manage_modules':
1005
			case 'jetpack_activate_modules':
1006
			case 'jetpack_deactivate_modules':
1007
				$caps = array( 'manage_options' );
1008
				break;
1009
			case 'jetpack_configure_modules':
1010
				$caps = array( 'manage_options' );
1011
				break;
1012
			case 'jetpack_manage_autoupdates':
1013
				$caps = array(
1014
					'manage_options',
1015
					'update_plugins',
1016
				);
1017
				break;
1018
			case 'jetpack_network_admin_page':
1019
			case 'jetpack_network_settings_page':
1020
				$caps = array( 'manage_network_plugins' );
1021
				break;
1022
			case 'jetpack_network_sites_page':
1023
				$caps = array( 'manage_sites' );
1024
				break;
1025
			case 'jetpack_admin_page':
1026
				if ( self::is_development_mode() ) {
1027
					$caps = array( 'manage_options' );
1028
					break;
1029
				} else {
1030
					$caps = array( 'read' );
1031
				}
1032
				break;
1033
			case 'jetpack_connect_user':
1034
				if ( self::is_development_mode() ) {
1035
					$caps = array( 'do_not_allow' );
1036
					break;
1037
				}
1038
				$caps = array( 'read' );
1039
				break;
1040
		}
1041
		return $caps;
1042
	}
1043
1044
	/**
1045
	 * Require a Jetpack authentication.
1046
	 *
1047
	 * @deprecated since 7.7.0
1048
	 * @see Automattic\Jetpack\Connection\Manager::require_jetpack_authentication()
1049
	 */
1050
	public function require_jetpack_authentication() {
1051
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::require_jetpack_authentication' );
1052
		$this->connection_manager->require_jetpack_authentication();
1053
	}
1054
1055
	/**
1056
	 * Load language files
1057
	 *
1058
	 * @action plugins_loaded
1059
	 */
1060
	public static function plugin_textdomain() {
1061
		// Note to self, the third argument must not be hardcoded, to account for relocated folders.
1062
		load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' );
1063
	}
1064
1065
	/**
1066
	 * Register assets for use in various modules and the Jetpack admin page.
1067
	 *
1068
	 * @uses wp_script_is, wp_register_script, plugins_url
1069
	 * @action wp_loaded
1070
	 * @return null
1071
	 */
1072
	public function register_assets() {
1073
		if ( ! wp_script_is( 'spin', 'registered' ) ) {
1074
			wp_register_script(
1075
				'spin',
1076
				Assets::get_file_url_for_environment( '_inc/build/spin.min.js', '_inc/spin.js' ),
1077
				false,
1078
				'1.3'
1079
			);
1080
		}
1081
1082 View Code Duplication
		if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) {
1083
			wp_register_script(
1084
				'jquery.spin',
1085
				Assets::get_file_url_for_environment( '_inc/build/jquery.spin.min.js', '_inc/jquery.spin.js' ),
1086
				array( 'jquery', 'spin' ),
1087
				'1.3'
1088
			);
1089
		}
1090
1091 View Code Duplication
		if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
1092
			wp_register_script(
1093
				'jetpack-gallery-settings',
1094
				Assets::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ),
1095
				array( 'media-views' ),
1096
				'20121225'
1097
			);
1098
		}
1099
1100 View Code Duplication
		if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) {
1101
			wp_register_script(
1102
				'jetpack-twitter-timeline',
1103
				Assets::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ),
1104
				array( 'jquery' ),
1105
				'4.0.0',
1106
				true
1107
			);
1108
		}
1109
1110
		if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) {
1111
			wp_register_script(
1112
				'jetpack-facebook-embed',
1113
				Assets::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ),
1114
				array( 'jquery' ),
1115
				null,
1116
				true
1117
			);
1118
1119
			/** This filter is documented in modules/sharedaddy/sharing-sources.php */
1120
			$fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
1121
			if ( ! is_numeric( $fb_app_id ) ) {
1122
				$fb_app_id = '';
1123
			}
1124
			wp_localize_script(
1125
				'jetpack-facebook-embed',
1126
				'jpfbembed',
1127
				array(
1128
					'appid'  => $fb_app_id,
1129
					'locale' => $this->get_locale(),
1130
				)
1131
			);
1132
		}
1133
1134
		/**
1135
		 * As jetpack_register_genericons is by default fired off a hook,
1136
		 * the hook may have already fired by this point.
1137
		 * So, let's just trigger it manually.
1138
		 */
1139
		require_once JETPACK__PLUGIN_DIR . '_inc/genericons.php';
1140
		jetpack_register_genericons();
1141
1142
		/**
1143
		 * Register the social logos
1144
		 */
1145
		require_once JETPACK__PLUGIN_DIR . '_inc/social-logos.php';
1146
		jetpack_register_social_logos();
1147
1148 View Code Duplication
		if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) {
1149
			wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION );
1150
		}
1151
	}
1152
1153
	/**
1154
	 * Guess locale from language code.
1155
	 *
1156
	 * @param string $lang Language code.
1157
	 * @return string|bool
1158
	 */
1159 View Code Duplication
	function guess_locale_from_lang( $lang ) {
1160
		if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) {
1161
			return 'en_US';
1162
		}
1163
1164
		if ( ! class_exists( 'GP_Locales' ) ) {
1165
			if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
1166
				return false;
1167
			}
1168
1169
			require JETPACK__GLOTPRESS_LOCALES_PATH;
1170
		}
1171
1172
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
1173
			// WP.com: get_locale() returns 'it'
1174
			$locale = GP_Locales::by_slug( $lang );
1175
		} else {
1176
			// Jetpack: get_locale() returns 'it_IT';
1177
			$locale = GP_Locales::by_field( 'facebook_locale', $lang );
1178
		}
1179
1180
		if ( ! $locale ) {
1181
			return false;
1182
		}
1183
1184
		if ( empty( $locale->facebook_locale ) ) {
1185
			if ( empty( $locale->wp_locale ) ) {
1186
				return false;
1187
			} else {
1188
				// Facebook SDK is smart enough to fall back to en_US if a
1189
				// locale isn't supported. Since supported Facebook locales
1190
				// can fall out of sync, we'll attempt to use the known
1191
				// wp_locale value and rely on said fallback.
1192
				return $locale->wp_locale;
1193
			}
1194
		}
1195
1196
		return $locale->facebook_locale;
1197
	}
1198
1199
	/**
1200
	 * Get the locale.
1201
	 *
1202
	 * @return string|bool
1203
	 */
1204
	function get_locale() {
1205
		$locale = $this->guess_locale_from_lang( get_locale() );
1206
1207
		if ( ! $locale ) {
1208
			$locale = 'en_US';
1209
		}
1210
1211
		return $locale;
1212
	}
1213
1214
	/**
1215
	 * Device Pixels support
1216
	 * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers.
1217
	 */
1218
	function devicepx() {
1219
		if ( self::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) {
1220
			wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true );
1221
		}
1222
	}
1223
1224
	/**
1225
	 * Return the network_site_url so that .com knows what network this site is a part of.
1226
	 *
1227
	 * @param  bool $option
1228
	 * @return string
1229
	 */
1230
	public function jetpack_main_network_site_option( $option ) {
1231
		return network_site_url();
1232
	}
1233
	/**
1234
	 * Network Name.
1235
	 */
1236
	static function network_name( $option = null ) {
1237
		global $current_site;
1238
		return $current_site->site_name;
1239
	}
1240
	/**
1241
	 * Does the network allow new user and site registrations.
1242
	 *
1243
	 * @return string
1244
	 */
1245
	static function network_allow_new_registrations( $option = null ) {
1246
		return ( in_array( get_site_option( 'registration' ), array( 'none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration' ) : 'none' );
1247
	}
1248
	/**
1249
	 * Does the network allow admins to add new users.
1250
	 *
1251
	 * @return boolian
1252
	 */
1253
	static function network_add_new_users( $option = null ) {
1254
		return (bool) get_site_option( 'add_new_users' );
1255
	}
1256
	/**
1257
	 * File upload psace left per site in MB.
1258
	 *  -1 means NO LIMIT.
1259
	 *
1260
	 * @return number
1261
	 */
1262
	static function network_site_upload_space( $option = null ) {
1263
		// value in MB
1264
		return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() );
1265
	}
1266
1267
	/**
1268
	 * Network allowed file types.
1269
	 *
1270
	 * @return string
1271
	 */
1272
	static function network_upload_file_types( $option = null ) {
1273
		return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' );
1274
	}
1275
1276
	/**
1277
	 * Maximum file upload size set by the network.
1278
	 *
1279
	 * @return number
1280
	 */
1281
	static function network_max_upload_file_size( $option = null ) {
1282
		// value in KB
1283
		return get_site_option( 'fileupload_maxk', 300 );
1284
	}
1285
1286
	/**
1287
	 * Lets us know if a site allows admins to manage the network.
1288
	 *
1289
	 * @return array
1290
	 */
1291
	static function network_enable_administration_menus( $option = null ) {
1292
		return get_site_option( 'menu_items' );
1293
	}
1294
1295
	/**
1296
	 * If a user has been promoted to or demoted from admin, we need to clear the
1297
	 * jetpack_other_linked_admins transient.
1298
	 *
1299
	 * @since 4.3.2
1300
	 * @since 4.4.0  $old_roles is null by default and if it's not passed, the transient is cleared.
1301
	 *
1302
	 * @param int    $user_id   The user ID whose role changed.
1303
	 * @param string $role      The new role.
1304
	 * @param array  $old_roles An array of the user's previous roles.
1305
	 */
1306
	function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) {
1307
		if ( 'administrator' == $role
1308
			|| ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) )
1309
			|| is_null( $old_roles )
1310
		) {
1311
			delete_transient( 'jetpack_other_linked_admins' );
1312
		}
1313
	}
1314
1315
	/**
1316
	 * Checks to see if there are any other users available to become primary
1317
	 * Users must both:
1318
	 * - Be linked to wpcom
1319
	 * - Be an admin
1320
	 *
1321
	 * @return mixed False if no other users are linked, Int if there are.
1322
	 */
1323
	static function get_other_linked_admins() {
1324
		$other_linked_users = get_transient( 'jetpack_other_linked_admins' );
1325
1326
		if ( false === $other_linked_users ) {
1327
			$admins = get_users( array( 'role' => 'administrator' ) );
1328
			if ( count( $admins ) > 1 ) {
1329
				$available = array();
1330
				foreach ( $admins as $admin ) {
1331
					if ( self::is_user_connected( $admin->ID ) ) {
1332
						$available[] = $admin->ID;
1333
					}
1334
				}
1335
1336
				$count_connected_admins = count( $available );
1337
				if ( count( $available ) > 1 ) {
1338
					$other_linked_users = $count_connected_admins;
1339
				} else {
1340
					$other_linked_users = 0;
1341
				}
1342
			} else {
1343
				$other_linked_users = 0;
1344
			}
1345
1346
			set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS );
1347
		}
1348
1349
		return ( 0 === $other_linked_users ) ? false : $other_linked_users;
1350
	}
1351
1352
	/**
1353
	 * Return whether we are dealing with a multi network setup or not.
1354
	 * The reason we are type casting this is because we want to avoid the situation where
1355
	 * the result is false since when is_main_network_option return false it cases
1356
	 * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the
1357
	 * database which could be set to anything as opposed to what this function returns.
1358
	 *
1359
	 * @param  bool $option
1360
	 *
1361
	 * @return boolean
1362
	 */
1363
	public function is_main_network_option( $option ) {
1364
		// return '1' or ''
1365
		return (string) (bool) self::is_multi_network();
1366
	}
1367
1368
	/**
1369
	 * Return true if we are with multi-site or multi-network false if we are dealing with single site.
1370
	 *
1371
	 * @param  string $option
1372
	 * @return boolean
1373
	 */
1374
	public function is_multisite( $option ) {
1375
		return (string) (bool) is_multisite();
1376
	}
1377
1378
	/**
1379
	 * Implemented since there is no core is multi network function
1380
	 * Right now there is no way to tell if we which network is the dominant network on the system
1381
	 *
1382
	 * @since  3.3
1383
	 * @return boolean
1384
	 */
1385 View Code Duplication
	public static function is_multi_network() {
1386
		global  $wpdb;
1387
1388
		// if we don't have a multi site setup no need to do any more
1389
		if ( ! is_multisite() ) {
1390
			return false;
1391
		}
1392
1393
		$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
1394
		if ( $num_sites > 1 ) {
1395
			return true;
1396
		} else {
1397
			return false;
1398
		}
1399
	}
1400
1401
	/**
1402
	 * Trigger an update to the main_network_site when we update the siteurl of a site.
1403
	 *
1404
	 * @return null
1405
	 */
1406
	function update_jetpack_main_network_site_option() {
1407
		_deprecated_function( __METHOD__, 'jetpack-4.2' );
1408
	}
1409
	/**
1410
	 * Triggered after a user updates the network settings via Network Settings Admin Page
1411
	 */
1412
	function update_jetpack_network_settings() {
1413
		_deprecated_function( __METHOD__, 'jetpack-4.2' );
1414
		// Only sync this info for the main network site.
1415
	}
1416
1417
	/**
1418
	 * Get back if the current site is single user site.
1419
	 *
1420
	 * @return bool
1421
	 */
1422 View Code Duplication
	public static function is_single_user_site() {
1423
		global $wpdb;
1424
1425
		if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) {
1426
			$some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
1427
			set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
1428
		}
1429
		return 1 === (int) $some_users;
1430
	}
1431
1432
	/**
1433
	 * Returns true if the site has file write access false otherwise.
1434
	 *
1435
	 * @return string ( '1' | '0' )
1436
	 **/
1437
	public static function file_system_write_access() {
1438
		if ( ! function_exists( 'get_filesystem_method' ) ) {
1439
			require_once ABSPATH . 'wp-admin/includes/file.php';
1440
		}
1441
1442
		require_once ABSPATH . 'wp-admin/includes/template.php';
1443
1444
		$filesystem_method = get_filesystem_method();
1445
		if ( $filesystem_method === 'direct' ) {
1446
			return 1;
1447
		}
1448
1449
		ob_start();
1450
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
1451
		ob_end_clean();
1452
		if ( $filesystem_credentials_are_stored ) {
1453
			return 1;
1454
		}
1455
		return 0;
1456
	}
1457
1458
	/**
1459
	 * Finds out if a site is using a version control system.
1460
	 *
1461
	 * @return string ( '1' | '0' )
1462
	 **/
1463
	public static function is_version_controlled() {
1464
		_deprecated_function( __METHOD__, 'jetpack-4.2', 'Functions::is_version_controlled' );
1465
		return (string) (int) Functions::is_version_controlled();
1466
	}
1467
1468
	/**
1469
	 * Determines whether the current theme supports featured images or not.
1470
	 *
1471
	 * @return string ( '1' | '0' )
1472
	 */
1473
	public static function featured_images_enabled() {
1474
		_deprecated_function( __METHOD__, 'jetpack-4.2' );
1475
		return current_theme_supports( 'post-thumbnails' ) ? '1' : '0';
1476
	}
1477
1478
	/**
1479
	 * Wrapper for core's get_avatar_url().  This one is deprecated.
1480
	 *
1481
	 * @deprecated 4.7 use get_avatar_url instead.
1482
	 * @param int|string|object $id_or_email A user ID,  email address, or comment object
1483
	 * @param int               $size Size of the avatar image
1484
	 * @param string            $default URL to a default image to use if no avatar is available
1485
	 * @param bool              $force_display Whether to force it to return an avatar even if show_avatars is disabled
1486
	 *
1487
	 * @return array
1488
	 */
1489
	public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) {
1490
		_deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' );
1491
		return get_avatar_url(
1492
			$id_or_email,
1493
			array(
1494
				'size'          => $size,
1495
				'default'       => $default,
1496
				'force_default' => $force_display,
1497
			)
1498
		);
1499
	}
1500
1501
	/**
1502
	 * jetpack_updates is saved in the following schema:
1503
	 *
1504
	 * array (
1505
	 *      'plugins'                       => (int) Number of plugin updates available.
1506
	 *      'themes'                        => (int) Number of theme updates available.
1507
	 *      'wordpress'                     => (int) Number of WordPress core updates available. // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
1508
	 *      'translations'                  => (int) Number of translation updates available.
1509
	 *      'total'                         => (int) Total of all available updates.
1510
	 *      'wp_update_version'             => (string) The latest available version of WordPress, only present if a WordPress update is needed.
1511
	 * )
1512
	 *
1513
	 * @return array
1514
	 */
1515
	public static function get_updates() {
1516
		$update_data = wp_get_update_data();
1517
1518
		// Stores the individual update counts as well as the total count.
1519
		if ( isset( $update_data['counts'] ) ) {
1520
			$updates = $update_data['counts'];
1521
		}
1522
1523
		// If we need to update WordPress core, let's find the latest version number.
1524 View Code Duplication
		if ( ! empty( $updates['wordpress'] ) ) {
1525
			$cur = get_preferred_from_update_core();
1526
			if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
1527
				$updates['wp_update_version'] = $cur->current;
1528
			}
1529
		}
1530
		return isset( $updates ) ? $updates : array();
1531
	}
1532
1533
	public static function get_update_details() {
1534
		$update_details = array(
1535
			'update_core'    => get_site_transient( 'update_core' ),
1536
			'update_plugins' => get_site_transient( 'update_plugins' ),
1537
			'update_themes'  => get_site_transient( 'update_themes' ),
1538
		);
1539
		return $update_details;
1540
	}
1541
1542
	public static function refresh_update_data() {
1543
		_deprecated_function( __METHOD__, 'jetpack-4.2' );
1544
1545
	}
1546
1547
	public static function refresh_theme_data() {
1548
		_deprecated_function( __METHOD__, 'jetpack-4.2' );
1549
	}
1550
1551
	/**
1552
	 * Is Jetpack active?
1553
	 */
1554
	public static function is_active() {
1555
		return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
1556
	}
1557
1558
	/**
1559
	 * Make an API call to WordPress.com for plan status
1560
	 *
1561
	 * @deprecated 7.2.0 Use Jetpack_Plan::refresh_from_wpcom.
1562
	 *
1563
	 * @return bool True if plan is updated, false if no update
1564
	 */
1565
	public static function refresh_active_plan_from_wpcom() {
1566
		_deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::refresh_from_wpcom' );
1567
		return Jetpack_Plan::refresh_from_wpcom();
1568
	}
1569
1570
	/**
1571
	 * Get the plan that this Jetpack site is currently using
1572
	 *
1573
	 * @deprecated 7.2.0 Use Jetpack_Plan::get.
1574
	 * @return array Active Jetpack plan details.
1575
	 */
1576
	public static function get_active_plan() {
1577
		_deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::get' );
1578
		return Jetpack_Plan::get();
1579
	}
1580
1581
	/**
1582
	 * Determine whether the active plan supports a particular feature
1583
	 *
1584
	 * @deprecated 7.2.0 Use Jetpack_Plan::supports.
1585
	 * @return bool True if plan supports feature, false if not.
1586
	 */
1587
	public static function active_plan_supports( $feature ) {
1588
		_deprecated_function( __METHOD__, 'jetpack-7.2.0', 'Jetpack_Plan::supports' );
1589
		return Jetpack_Plan::supports( $feature );
1590
	}
1591
1592
	/**
1593
	 * Is Jetpack in development (offline) mode?
1594
	 */
1595 View Code Duplication
	public static function is_development_mode() {
1596
		$development_mode = false;
1597
1598
		if ( defined( 'JETPACK_DEV_DEBUG' ) ) {
1599
			$development_mode = JETPACK_DEV_DEBUG;
1600
		} elseif ( $site_url = site_url() ) {
1601
			$development_mode = false === strpos( $site_url, '.' );
1602
		}
1603
1604
		/**
1605
		 * Filters Jetpack's development mode.
1606
		 *
1607
		 * @see https://jetpack.com/support/development-mode/
1608
		 *
1609
		 * @since 2.2.1
1610
		 *
1611
		 * @param bool $development_mode Is Jetpack's development mode active.
1612
		 */
1613
		$development_mode = (bool) apply_filters( 'jetpack_development_mode', $development_mode );
1614
		return $development_mode;
1615
	}
1616
1617
	/**
1618
	 * Whether the site is currently onboarding or not.
1619
	 * A site is considered as being onboarded if it currently has an onboarding token.
1620
	 *
1621
	 * @since 5.8
1622
	 *
1623
	 * @access public
1624
	 * @static
1625
	 *
1626
	 * @return bool True if the site is currently onboarding, false otherwise
1627
	 */
1628
	public static function is_onboarding() {
1629
		return Jetpack_Options::get_option( 'onboarding' ) !== false;
1630
	}
1631
1632
	/**
1633
	 * Determines reason for Jetpack development mode.
1634
	 */
1635
	public static function development_mode_trigger_text() {
1636
		if ( ! self::is_development_mode() ) {
1637
			return __( 'Jetpack is not in Development Mode.', 'jetpack' );
1638
		}
1639
1640
		if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) {
1641
			$notice = __( 'The JETPACK_DEV_DEBUG constant is defined in wp-config.php or elsewhere.', 'jetpack' );
1642
		} elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
1643
			$notice = __( 'The site URL lacking a dot (e.g. http://localhost).', 'jetpack' );
1644
		} else {
1645
			$notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' );
1646
		}
1647
1648
		return $notice;
1649
1650
	}
1651
	/**
1652
	 * Get Jetpack development mode notice text and notice class.
1653
	 *
1654
	 * Mirrors the checks made in Jetpack::is_development_mode
1655
	 */
1656
	public static function show_development_mode_notice() {
1657 View Code Duplication
		if ( self::is_development_mode() ) {
1658
			$notice = sprintf(
1659
				/* translators: %s is a URL */
1660
				__( 'In <a href="%s" target="_blank">Development Mode</a>:', 'jetpack' ),
1661
				'https://jetpack.com/support/development-mode/'
1662
			);
1663
1664
			$notice .= ' ' . self::development_mode_trigger_text();
1665
1666
			echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
1667
		}
1668
1669
		// Throw up a notice if using a development version and as for feedback.
1670
		if ( self::is_development_version() ) {
1671
			/* translators: %s is a URL */
1672
			$notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), 'https://jetpack.com/contact-support/beta-group/' );
1673
1674
			echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
1675
		}
1676
		// Throw up a notice if using staging mode
1677
		if ( self::is_staging_site() ) {
1678
			/* translators: %s is a URL */
1679
			$notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' );
1680
1681
			echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
1682
		}
1683
	}
1684
1685
	/**
1686
	 * Whether Jetpack's version maps to a public release, or a development version.
1687
	 */
1688
	public static function is_development_version() {
1689
		/**
1690
		 * Allows filtering whether this is a development version of Jetpack.
1691
		 *
1692
		 * This filter is especially useful for tests.
1693
		 *
1694
		 * @since 4.3.0
1695
		 *
1696
		 * @param bool $development_version Is this a develoment version of Jetpack?
1697
		 */
1698
		return (bool) apply_filters(
1699
			'jetpack_development_version',
1700
			! preg_match( '/^\d+(\.\d+)+$/', Constants::get_constant( 'JETPACK__VERSION' ) )
1701
		);
1702
	}
1703
1704
	/**
1705
	 * Is a given user (or the current user if none is specified) linked to a WordPress.com user?
1706
	 */
1707
	public static function is_user_connected( $user_id = false ) {
1708
		return self::connection()->is_user_connected( $user_id );
1709
	}
1710
1711
	/**
1712
	 * Get the wpcom user data of the current|specified connected user.
1713
	 */
1714 View Code Duplication
	public static function get_connected_user_data( $user_id = null ) {
1715
		// TODO: remove in favor of Connection_Manager->get_connected_user_data
1716
		if ( ! $user_id ) {
1717
			$user_id = get_current_user_id();
1718
		}
1719
1720
		$transient_key = "jetpack_connected_user_data_$user_id";
1721
1722
		if ( $cached_user_data = get_transient( $transient_key ) ) {
1723
			return $cached_user_data;
1724
		}
1725
1726
		$xml = new Jetpack_IXR_Client(
1727
			array(
1728
				'user_id' => $user_id,
1729
			)
1730
		);
1731
		$xml->query( 'wpcom.getUser' );
1732
		if ( ! $xml->isError() ) {
1733
			$user_data = $xml->getResponse();
1734
			set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS );
1735
			return $user_data;
1736
		}
1737
1738
		return false;
1739
	}
1740
1741
	/**
1742
	 * Get the wpcom email of the current|specified connected user.
1743
	 */
1744 View Code Duplication
	public static function get_connected_user_email( $user_id = null ) {
1745
		if ( ! $user_id ) {
1746
			$user_id = get_current_user_id();
1747
		}
1748
1749
		$xml = new Jetpack_IXR_Client(
1750
			array(
1751
				'user_id' => $user_id,
1752
			)
1753
		);
1754
		$xml->query( 'wpcom.getUserEmail' );
1755
		if ( ! $xml->isError() ) {
1756
			return $xml->getResponse();
1757
		}
1758
		return false;
1759
	}
1760
1761
	/**
1762
	 * Get the wpcom email of the master user.
1763
	 */
1764
	public static function get_master_user_email() {
1765
		$master_user_id = Jetpack_Options::get_option( 'master_user' );
1766
		if ( $master_user_id ) {
1767
			return self::get_connected_user_email( $master_user_id );
1768
		}
1769
		return '';
1770
	}
1771
1772
	/**
1773
	 * Whether the current user is the connection owner.
1774
	 *
1775
	 * @deprecated since 7.7
1776
	 *
1777
	 * @return bool Whether the current user is the connection owner.
1778
	 */
1779
	public function current_user_is_connection_owner() {
1780
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::is_connection_owner' );
1781
		return self::connection()->is_connection_owner();
1782
	}
1783
1784
	/**
1785
	 * Gets current user IP address.
1786
	 *
1787
	 * @param  bool $check_all_headers Check all headers? Default is `false`.
1788
	 *
1789
	 * @return string                  Current user IP address.
1790
	 */
1791
	public static function current_user_ip( $check_all_headers = false ) {
1792
		if ( $check_all_headers ) {
1793
			foreach ( array(
1794
				'HTTP_CF_CONNECTING_IP',
1795
				'HTTP_CLIENT_IP',
1796
				'HTTP_X_FORWARDED_FOR',
1797
				'HTTP_X_FORWARDED',
1798
				'HTTP_X_CLUSTER_CLIENT_IP',
1799
				'HTTP_FORWARDED_FOR',
1800
				'HTTP_FORWARDED',
1801
				'HTTP_VIA',
1802
			) as $key ) {
1803
				if ( ! empty( $_SERVER[ $key ] ) ) {
1804
					return $_SERVER[ $key ];
1805
				}
1806
			}
1807
		}
1808
1809
		return ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
1810
	}
1811
1812
	/**
1813
	 * Add any extra oEmbed providers that we know about and use on wpcom for feature parity.
1814
	 */
1815
	function extra_oembed_providers() {
1816
		// Cloudup: https://dev.cloudup.com/#oembed
1817
		wp_oembed_add_provider( 'https://cloudup.com/*', 'https://cloudup.com/oembed' );
1818
		wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' );
1819
		wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true );
1820
		wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true );
1821
		wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true );
1822
		wp_oembed_add_provider( '#https?://(www\.)?icloud\.com/keynote/.*#i', 'https://iwmb.icloud.com/iwmb/oembed', true );
1823
		wp_oembed_add_provider( 'https://song.link/*', 'https://song.link/oembed', false );
1824
	}
1825
1826
	/**
1827
	 * Synchronize connected user role changes
1828
	 */
1829
	function user_role_change( $user_id ) {
1830
		_deprecated_function( __METHOD__, 'jetpack-4.2', 'Users::user_role_change()' );
1831
		Users::user_role_change( $user_id );
1832
	}
1833
1834
	/**
1835
	 * Loads the currently active modules.
1836
	 */
1837
	public static function load_modules() {
1838
		if (
1839
			! self::is_active()
1840
			&& ! self::is_development_mode()
1841
			&& ! self::is_onboarding()
1842
			&& (
1843
				! is_multisite()
1844
				|| ! get_site_option( 'jetpack_protect_active' )
1845
			)
1846
		) {
1847
			return;
1848
		}
1849
1850
		$version = Jetpack_Options::get_option( 'version' );
1851 View Code Duplication
		if ( ! $version ) {
1852
			$version = $old_version = JETPACK__VERSION . ':' . time();
1853
			/** This action is documented in class.jetpack.php */
1854
			do_action( 'updating_jetpack_version', $version, false );
1855
			Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
1856
		}
1857
		list( $version ) = explode( ':', $version );
1858
1859
		$modules = array_filter( self::get_active_modules(), array( 'Jetpack', 'is_module' ) );
1860
1861
		$modules_data = array();
1862
1863
		// Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check.
1864
		if ( version_compare( $version, JETPACK__VERSION, '<' ) ) {
1865
			$updated_modules = array();
1866
			foreach ( $modules as $module ) {
1867
				$modules_data[ $module ] = self::get_module( $module );
1868
				if ( ! isset( $modules_data[ $module ]['changed'] ) ) {
1869
					continue;
1870
				}
1871
1872
				if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) {
1873
					continue;
1874
				}
1875
1876
				$updated_modules[] = $module;
1877
			}
1878
1879
			$modules = array_diff( $modules, $updated_modules );
1880
		}
1881
1882
		$is_development_mode = self::is_development_mode();
1883
1884
		foreach ( $modules as $index => $module ) {
1885
			// If we're in dev mode, disable modules requiring a connection
1886
			if ( $is_development_mode ) {
1887
				// Prime the pump if we need to
1888
				if ( empty( $modules_data[ $module ] ) ) {
1889
					$modules_data[ $module ] = self::get_module( $module );
1890
				}
1891
				// If the module requires a connection, but we're in local mode, don't include it.
1892
				if ( $modules_data[ $module ]['requires_connection'] ) {
1893
					continue;
1894
				}
1895
			}
1896
1897
			if ( did_action( 'jetpack_module_loaded_' . $module ) ) {
1898
				continue;
1899
			}
1900
1901
			if ( ! include_once self::get_module_path( $module ) ) {
1902
				unset( $modules[ $index ] );
1903
				self::update_active_modules( array_values( $modules ) );
1904
				continue;
1905
			}
1906
1907
			/**
1908
			 * Fires when a specific module is loaded.
1909
			 * The dynamic part of the hook, $module, is the module slug.
1910
			 *
1911
			 * @since 1.1.0
1912
			 */
1913
			do_action( 'jetpack_module_loaded_' . $module );
1914
		}
1915
1916
		/**
1917
		 * Fires when all the modules are loaded.
1918
		 *
1919
		 * @since 1.1.0
1920
		 */
1921
		do_action( 'jetpack_modules_loaded' );
1922
1923
		// 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.
1924
		require_once JETPACK__PLUGIN_DIR . 'modules/module-extras.php';
1925
	}
1926
1927
	/**
1928
	 * Check if Jetpack's REST API compat file should be included
1929
	 *
1930
	 * @action plugins_loaded
1931
	 * @return null
1932
	 */
1933
	public function check_rest_api_compat() {
1934
		/**
1935
		 * Filters the list of REST API compat files to be included.
1936
		 *
1937
		 * @since 2.2.5
1938
		 *
1939
		 * @param array $args Array of REST API compat files to include.
1940
		 */
1941
		$_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() );
1942
1943
		if ( function_exists( 'bbpress' ) ) {
1944
			$_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php';
1945
		}
1946
1947
		foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include ) {
1948
			require_once $_jetpack_rest_api_compat_include;
1949
		}
1950
	}
1951
1952
	/**
1953
	 * Gets all plugins currently active in values, regardless of whether they're
1954
	 * traditionally activated or network activated.
1955
	 *
1956
	 * @todo Store the result in core's object cache maybe?
1957
	 */
1958
	public static function get_active_plugins() {
1959
		$active_plugins = (array) get_option( 'active_plugins', array() );
1960
1961
		if ( is_multisite() ) {
1962
			// Due to legacy code, active_sitewide_plugins stores them in the keys,
1963
			// whereas active_plugins stores them in the values.
1964
			$network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
1965
			if ( $network_plugins ) {
1966
				$active_plugins = array_merge( $active_plugins, $network_plugins );
1967
			}
1968
		}
1969
1970
		sort( $active_plugins );
1971
1972
		return array_unique( $active_plugins );
1973
	}
1974
1975
	/**
1976
	 * Gets and parses additional plugin data to send with the heartbeat data
1977
	 *
1978
	 * @since 3.8.1
1979
	 *
1980
	 * @return array Array of plugin data
1981
	 */
1982
	public static function get_parsed_plugin_data() {
1983
		if ( ! function_exists( 'get_plugins' ) ) {
1984
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
1985
		}
1986
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
1987
		$all_plugins    = apply_filters( 'all_plugins', get_plugins() );
1988
		$active_plugins = self::get_active_plugins();
1989
1990
		$plugins = array();
1991
		foreach ( $all_plugins as $path => $plugin_data ) {
1992
			$plugins[ $path ] = array(
1993
				'is_active' => in_array( $path, $active_plugins ),
1994
				'file'      => $path,
1995
				'name'      => $plugin_data['Name'],
1996
				'version'   => $plugin_data['Version'],
1997
				'author'    => $plugin_data['Author'],
1998
			);
1999
		}
2000
2001
		return $plugins;
2002
	}
2003
2004
	/**
2005
	 * Gets and parses theme data to send with the heartbeat data
2006
	 *
2007
	 * @since 3.8.1
2008
	 *
2009
	 * @return array Array of theme data
2010
	 */
2011
	public static function get_parsed_theme_data() {
2012
		$all_themes  = wp_get_themes( array( 'allowed' => true ) );
2013
		$header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' );
2014
2015
		$themes = array();
2016
		foreach ( $all_themes as $slug => $theme_data ) {
2017
			$theme_headers = array();
2018
			foreach ( $header_keys as $header_key ) {
2019
				$theme_headers[ $header_key ] = $theme_data->get( $header_key );
2020
			}
2021
2022
			$themes[ $slug ] = array(
2023
				'is_active_theme' => $slug == wp_get_theme()->get_template(),
2024
				'slug'            => $slug,
2025
				'theme_root'      => $theme_data->get_theme_root_uri(),
2026
				'parent'          => $theme_data->parent(),
2027
				'headers'         => $theme_headers,
2028
			);
2029
		}
2030
2031
		return $themes;
2032
	}
2033
2034
	/**
2035
	 * Checks whether a specific plugin is active.
2036
	 *
2037
	 * We don't want to store these in a static variable, in case
2038
	 * there are switch_to_blog() calls involved.
2039
	 */
2040
	public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) {
2041
		return in_array( $plugin, self::get_active_plugins() );
2042
	}
2043
2044
	/**
2045
	 * Check if Jetpack's Open Graph tags should be used.
2046
	 * If certain plugins are active, Jetpack's og tags are suppressed.
2047
	 *
2048
	 * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
2049
	 * @action plugins_loaded
2050
	 * @return null
2051
	 */
2052
	public function check_open_graph() {
2053
		if ( in_array( 'publicize', self::get_active_modules() ) || in_array( 'sharedaddy', self::get_active_modules() ) ) {
2054
			add_filter( 'jetpack_enable_open_graph', '__return_true', 0 );
2055
		}
2056
2057
		$active_plugins = self::get_active_plugins();
2058
2059
		if ( ! empty( $active_plugins ) ) {
2060
			foreach ( $this->open_graph_conflicting_plugins as $plugin ) {
2061
				if ( in_array( $plugin, $active_plugins ) ) {
2062
					add_filter( 'jetpack_enable_open_graph', '__return_false', 99 );
2063
					break;
2064
				}
2065
			}
2066
		}
2067
2068
		/**
2069
		 * Allow the addition of Open Graph Meta Tags to all pages.
2070
		 *
2071
		 * @since 2.0.3
2072
		 *
2073
		 * @param bool false Should Open Graph Meta tags be added. Default to false.
2074
		 */
2075
		if ( apply_filters( 'jetpack_enable_open_graph', false ) ) {
2076
			require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php';
2077
		}
2078
	}
2079
2080
	/**
2081
	 * Check if Jetpack's Twitter tags should be used.
2082
	 * If certain plugins are active, Jetpack's twitter tags are suppressed.
2083
	 *
2084
	 * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
2085
	 * @action plugins_loaded
2086
	 * @return null
2087
	 */
2088
	public function check_twitter_tags() {
2089
2090
		$active_plugins = self::get_active_plugins();
2091
2092
		if ( ! empty( $active_plugins ) ) {
2093
			foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) {
2094
				if ( in_array( $plugin, $active_plugins ) ) {
2095
					add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 );
2096
					break;
2097
				}
2098
			}
2099
		}
2100
2101
		/**
2102
		 * Allow Twitter Card Meta tags to be disabled.
2103
		 *
2104
		 * @since 2.6.0
2105
		 *
2106
		 * @param bool true Should Twitter Card Meta tags be disabled. Default to true.
2107
		 */
2108
		if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) {
2109
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
2110
		}
2111
	}
2112
2113
	/**
2114
	 * Allows plugins to submit security reports.
2115
	 *
2116
	 * @param string $type         Report type (login_form, backup, file_scanning, spam)
2117
	 * @param string $plugin_file  Plugin __FILE__, so that we can pull plugin data
2118
	 * @param array  $args         See definitions above
2119
	 */
2120
	public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) {
2121
		_deprecated_function( __FUNCTION__, 'jetpack-4.2', null );
2122
	}
2123
2124
	/* Jetpack Options API */
2125
2126
	public static function get_option_names( $type = 'compact' ) {
2127
		return Jetpack_Options::get_option_names( $type );
2128
	}
2129
2130
	/**
2131
	 * Returns the requested option.  Looks in jetpack_options or jetpack_$name as appropriate.
2132
	 *
2133
	 * @param string $name    Option name
2134
	 * @param mixed  $default (optional)
2135
	 */
2136
	public static function get_option( $name, $default = false ) {
2137
		return Jetpack_Options::get_option( $name, $default );
2138
	}
2139
2140
	/**
2141
	 * Updates the single given option.  Updates jetpack_options or jetpack_$name as appropriate.
2142
	 *
2143
	 * @deprecated 3.4 use Jetpack_Options::update_option() instead.
2144
	 * @param string $name  Option name
2145
	 * @param mixed  $value Option value
2146
	 */
2147
	public static function update_option( $name, $value ) {
2148
		_deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' );
2149
		return Jetpack_Options::update_option( $name, $value );
2150
	}
2151
2152
	/**
2153
	 * Updates the multiple given options.  Updates jetpack_options and/or jetpack_$name as appropriate.
2154
	 *
2155
	 * @deprecated 3.4 use Jetpack_Options::update_options() instead.
2156
	 * @param array $array array( option name => option value, ... )
2157
	 */
2158
	public static function update_options( $array ) {
2159
		_deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' );
2160
		return Jetpack_Options::update_options( $array );
2161
	}
2162
2163
	/**
2164
	 * Deletes the given option.  May be passed multiple option names as an array.
2165
	 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
2166
	 *
2167
	 * @deprecated 3.4 use Jetpack_Options::delete_option() instead.
2168
	 * @param string|array $names
2169
	 */
2170
	public static function delete_option( $names ) {
2171
		_deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' );
2172
		return Jetpack_Options::delete_option( $names );
2173
	}
2174
2175
	/**
2176
	 * Enters a user token into the user_tokens option
2177
	 *
2178
	 * @param int    $user_id
2179
	 * @param string $token
2180
	 * return bool
2181
	 */
2182
	public static function update_user_token( $user_id, $token, $is_master_user ) {
2183
		// not designed for concurrent updates
2184
		$user_tokens = Jetpack_Options::get_option( 'user_tokens' );
2185
		if ( ! is_array( $user_tokens ) ) {
2186
			$user_tokens = array();
2187
		}
2188
		$user_tokens[ $user_id ] = $token;
2189
		if ( $is_master_user ) {
2190
			$master_user = $user_id;
2191
			$options     = compact( 'user_tokens', 'master_user' );
2192
		} else {
2193
			$options = compact( 'user_tokens' );
2194
		}
2195
		return Jetpack_Options::update_options( $options );
2196
	}
2197
2198
	/**
2199
	 * Returns an array of all PHP files in the specified absolute path.
2200
	 * Equivalent to glob( "$absolute_path/*.php" ).
2201
	 *
2202
	 * @param string $absolute_path The absolute path of the directory to search.
2203
	 * @return array Array of absolute paths to the PHP files.
2204
	 */
2205
	public static function glob_php( $absolute_path ) {
2206
		if ( function_exists( 'glob' ) ) {
2207
			return glob( "$absolute_path/*.php" );
2208
		}
2209
2210
		$absolute_path = untrailingslashit( $absolute_path );
2211
		$files         = array();
2212
		if ( ! $dir = @opendir( $absolute_path ) ) {
2213
			return $files;
2214
		}
2215
2216
		while ( false !== $file = readdir( $dir ) ) {
2217
			if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) {
2218
				continue;
2219
			}
2220
2221
			$file = "$absolute_path/$file";
2222
2223
			if ( ! is_file( $file ) ) {
2224
				continue;
2225
			}
2226
2227
			$files[] = $file;
2228
		}
2229
2230
		closedir( $dir );
2231
2232
		return $files;
2233
	}
2234
2235
	public static function activate_new_modules( $redirect = false ) {
2236
		if ( ! self::is_active() && ! self::is_development_mode() ) {
2237
			return;
2238
		}
2239
2240
		$jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic]
2241 View Code Duplication
		if ( ! $jetpack_old_version ) {
2242
			$jetpack_old_version = $version = $old_version = '1.1:' . time();
2243
			/** This action is documented in class.jetpack.php */
2244
			do_action( 'updating_jetpack_version', $version, false );
2245
			Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
2246
		}
2247
2248
		list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic]
2249
2250
		if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) {
2251
			return;
2252
		}
2253
2254
		$active_modules     = self::get_active_modules();
2255
		$reactivate_modules = array();
2256
		foreach ( $active_modules as $active_module ) {
2257
			$module = self::get_module( $active_module );
2258
			if ( ! isset( $module['changed'] ) ) {
2259
				continue;
2260
			}
2261
2262
			if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) {
2263
				continue;
2264
			}
2265
2266
			$reactivate_modules[] = $active_module;
2267
			self::deactivate_module( $active_module );
2268
		}
2269
2270
		$new_version = JETPACK__VERSION . ':' . time();
2271
		/** This action is documented in class.jetpack.php */
2272
		do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version );
2273
		Jetpack_Options::update_options(
2274
			array(
2275
				'version'     => $new_version,
2276
				'old_version' => $jetpack_old_version,
2277
			)
2278
		);
2279
2280
		self::state( 'message', 'modules_activated' );
2281
		self::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules, $redirect );
2282
2283
		if ( $redirect ) {
2284
			$page = 'jetpack'; // make sure we redirect to either settings or the jetpack page
2285
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) {
2286
				$page = $_GET['page'];
2287
			}
2288
2289
			wp_safe_redirect( self::admin_url( 'page=' . $page ) );
2290
			exit;
2291
		}
2292
	}
2293
2294
	/**
2295
	 * List available Jetpack modules. Simply lists .php files in /modules/.
2296
	 * Make sure to tuck away module "library" files in a sub-directory.
2297
	 */
2298
	public static function get_available_modules( $min_version = false, $max_version = false ) {
2299
		static $modules = null;
2300
2301
		if ( ! isset( $modules ) ) {
2302
			$available_modules_option = Jetpack_Options::get_option( 'available_modules', array() );
2303
			// Use the cache if we're on the front-end and it's available...
2304
			if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) {
2305
				$modules = $available_modules_option[ JETPACK__VERSION ];
2306
			} else {
2307
				$files = self::glob_php( JETPACK__PLUGIN_DIR . 'modules' );
2308
2309
				$modules = array();
2310
2311
				foreach ( $files as $file ) {
2312
					if ( ! $headers = self::get_module( $file ) ) {
2313
						continue;
2314
					}
2315
2316
					$modules[ self::get_module_slug( $file ) ] = $headers['introduced'];
2317
				}
2318
2319
				Jetpack_Options::update_option(
2320
					'available_modules',
2321
					array(
2322
						JETPACK__VERSION => $modules,
2323
					)
2324
				);
2325
			}
2326
		}
2327
2328
		/**
2329
		 * Filters the array of modules available to be activated.
2330
		 *
2331
		 * @since 2.4.0
2332
		 *
2333
		 * @param array $modules Array of available modules.
2334
		 * @param string $min_version Minimum version number required to use modules.
2335
		 * @param string $max_version Maximum version number required to use modules.
2336
		 */
2337
		$mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version );
2338
2339
		if ( ! $min_version && ! $max_version ) {
2340
			return array_keys( $mods );
2341
		}
2342
2343
		$r = array();
2344
		foreach ( $mods as $slug => $introduced ) {
2345
			if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) {
2346
				continue;
2347
			}
2348
2349
			if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) {
2350
				continue;
2351
			}
2352
2353
			$r[] = $slug;
2354
		}
2355
2356
		return $r;
2357
	}
2358
2359
	/**
2360
	 * Default modules loaded on activation.
2361
	 */
2362
	public static function get_default_modules( $min_version = false, $max_version = false ) {
2363
		$return = array();
2364
2365
		foreach ( self::get_available_modules( $min_version, $max_version ) as $module ) {
2366
			$module_data = self::get_module( $module );
2367
2368
			switch ( strtolower( $module_data['auto_activate'] ) ) {
2369
				case 'yes':
2370
					$return[] = $module;
2371
					break;
2372
				case 'public':
2373
					if ( Jetpack_Options::get_option( 'public' ) ) {
2374
						$return[] = $module;
2375
					}
2376
					break;
2377
				case 'no':
2378
				default:
2379
					break;
2380
			}
2381
		}
2382
		/**
2383
		 * Filters the array of default modules.
2384
		 *
2385
		 * @since 2.5.0
2386
		 *
2387
		 * @param array $return Array of default modules.
2388
		 * @param string $min_version Minimum version number required to use modules.
2389
		 * @param string $max_version Maximum version number required to use modules.
2390
		 */
2391
		return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version );
2392
	}
2393
2394
	/**
2395
	 * Checks activated modules during auto-activation to determine
2396
	 * if any of those modules are being deprecated.  If so, close
2397
	 * them out, and add any replacement modules.
2398
	 *
2399
	 * Runs at priority 99 by default.
2400
	 *
2401
	 * This is run late, so that it can still activate a module if
2402
	 * the new module is a replacement for another that the user
2403
	 * currently has active, even if something at the normal priority
2404
	 * would kibosh everything.
2405
	 *
2406
	 * @since 2.6
2407
	 * @uses jetpack_get_default_modules filter
2408
	 * @param array $modules
2409
	 * @return array
2410
	 */
2411
	function handle_deprecated_modules( $modules ) {
2412
		$deprecated_modules = array(
2413
			'debug'            => null,  // Closed out and moved to the debugger library.
2414
			'wpcc'             => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2415
			'gplus-authorship' => null,  // Closed out in 3.2 -- Google dropped support.
2416
		);
2417
2418
		// Don't activate SSO if they never completed activating WPCC.
2419
		if ( self::is_module_active( 'wpcc' ) ) {
2420
			$wpcc_options = Jetpack_Options::get_option( 'wpcc_options' );
2421
			if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) {
2422
				$deprecated_modules['wpcc'] = null;
2423
			}
2424
		}
2425
2426
		foreach ( $deprecated_modules as $module => $replacement ) {
2427
			if ( self::is_module_active( $module ) ) {
2428
				self::deactivate_module( $module );
2429
				if ( $replacement ) {
2430
					$modules[] = $replacement;
2431
				}
2432
			}
2433
		}
2434
2435
		return array_unique( $modules );
2436
	}
2437
2438
	/**
2439
	 * Checks activated plugins during auto-activation to determine
2440
	 * if any of those plugins are in the list with a corresponding module
2441
	 * that is not compatible with the plugin. The module will not be allowed
2442
	 * to auto-activate.
2443
	 *
2444
	 * @since 2.6
2445
	 * @uses jetpack_get_default_modules filter
2446
	 * @param array $modules
2447
	 * @return array
2448
	 */
2449
	function filter_default_modules( $modules ) {
2450
2451
		$active_plugins = self::get_active_plugins();
2452
2453
		if ( ! empty( $active_plugins ) ) {
2454
2455
			// For each module we'd like to auto-activate...
2456
			foreach ( $modules as $key => $module ) {
2457
				// If there are potential conflicts for it...
2458
				if ( ! empty( $this->conflicting_plugins[ $module ] ) ) {
2459
					// For each potential conflict...
2460
					foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) {
2461
						// If that conflicting plugin is active...
2462
						if ( in_array( $plugin, $active_plugins ) ) {
2463
							// Remove that item from being auto-activated.
2464
							unset( $modules[ $key ] );
2465
						}
2466
					}
2467
				}
2468
			}
2469
		}
2470
2471
		return $modules;
2472
	}
2473
2474
	/**
2475
	 * Extract a module's slug from its full path.
2476
	 */
2477
	public static function get_module_slug( $file ) {
2478
		return str_replace( '.php', '', basename( $file ) );
2479
	}
2480
2481
	/**
2482
	 * Generate a module's path from its slug.
2483
	 */
2484
	public static function get_module_path( $slug ) {
2485
		/**
2486
		 * Filters the path of a modules.
2487
		 *
2488
		 * @since 7.4.0
2489
		 *
2490
		 * @param array $return The absolute path to a module's root php file
2491
		 * @param string $slug The module slug
2492
		 */
2493
		return apply_filters( 'jetpack_get_module_path', JETPACK__PLUGIN_DIR . "modules/$slug.php", $slug );
2494
	}
2495
2496
	/**
2497
	 * Load module data from module file. Headers differ from WordPress
2498
	 * plugin headers to avoid them being identified as standalone
2499
	 * plugins on the WordPress plugins page.
2500
	 */
2501
	public static function get_module( $module ) {
2502
		$headers = array(
2503
			'name'                      => 'Module Name',
2504
			'description'               => 'Module Description',
2505
			'sort'                      => 'Sort Order',
2506
			'recommendation_order'      => 'Recommendation Order',
2507
			'introduced'                => 'First Introduced',
2508
			'changed'                   => 'Major Changes In',
2509
			'deactivate'                => 'Deactivate',
2510
			'free'                      => 'Free',
2511
			'requires_connection'       => 'Requires Connection',
2512
			'auto_activate'             => 'Auto Activate',
2513
			'module_tags'               => 'Module Tags',
2514
			'feature'                   => 'Feature',
2515
			'additional_search_queries' => 'Additional Search Queries',
2516
			'plan_classes'              => 'Plans',
2517
		);
2518
2519
		$file = self::get_module_path( self::get_module_slug( $module ) );
2520
2521
		$mod = self::get_file_data( $file, $headers );
2522
		if ( empty( $mod['name'] ) ) {
2523
			return false;
2524
		}
2525
2526
		$mod['sort']                 = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
2527
		$mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order'];
2528
		$mod['deactivate']           = empty( $mod['deactivate'] );
2529
		$mod['free']                 = empty( $mod['free'] );
2530
		$mod['requires_connection']  = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true;
2531
2532
		if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) {
2533
			$mod['auto_activate'] = 'No';
2534
		} else {
2535
			$mod['auto_activate'] = (string) $mod['auto_activate'];
2536
		}
2537
2538
		if ( $mod['module_tags'] ) {
2539
			$mod['module_tags'] = explode( ',', $mod['module_tags'] );
2540
			$mod['module_tags'] = array_map( 'trim', $mod['module_tags'] );
2541
			$mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] );
2542
		} else {
2543
			$mod['module_tags'] = array( self::translate_module_tag( 'Other' ) );
2544
		}
2545
2546 View Code Duplication
		if ( $mod['plan_classes'] ) {
2547
			$mod['plan_classes'] = explode( ',', $mod['plan_classes'] );
2548
			$mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) );
2549
		} else {
2550
			$mod['plan_classes'] = array( 'free' );
2551
		}
2552
2553 View Code Duplication
		if ( $mod['feature'] ) {
2554
			$mod['feature'] = explode( ',', $mod['feature'] );
2555
			$mod['feature'] = array_map( 'trim', $mod['feature'] );
2556
		} else {
2557
			$mod['feature'] = array( self::translate_module_tag( 'Other' ) );
2558
		}
2559
2560
		/**
2561
		 * Filters the feature array on a module.
2562
		 *
2563
		 * This filter allows you to control where each module is filtered: Recommended,
2564
		 * and the default "Other" listing.
2565
		 *
2566
		 * @since 3.5.0
2567
		 *
2568
		 * @param array   $mod['feature'] The areas to feature this module:
2569
		 *     'Recommended' shows on the main Jetpack admin screen.
2570
		 *     'Other' should be the default if no other value is in the array.
2571
		 * @param string  $module The slug of the module, e.g. sharedaddy.
2572
		 * @param array   $mod All the currently assembled module data.
2573
		 */
2574
		$mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod );
2575
2576
		/**
2577
		 * Filter the returned data about a module.
2578
		 *
2579
		 * This filter allows overriding any info about Jetpack modules. It is dangerous,
2580
		 * so please be careful.
2581
		 *
2582
		 * @since 3.6.0
2583
		 *
2584
		 * @param array   $mod    The details of the requested module.
2585
		 * @param string  $module The slug of the module, e.g. sharedaddy
2586
		 * @param string  $file   The path to the module source file.
2587
		 */
2588
		return apply_filters( 'jetpack_get_module', $mod, $module, $file );
2589
	}
2590
2591
	/**
2592
	 * Like core's get_file_data implementation, but caches the result.
2593
	 */
2594
	public static function get_file_data( $file, $headers ) {
2595
		// Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated
2596
		$file_name = basename( $file );
2597
2598
		$cache_key = 'jetpack_file_data_' . JETPACK__VERSION;
2599
2600
		$file_data_option = get_transient( $cache_key );
2601
2602
		if ( ! is_array( $file_data_option ) ) {
2603
			delete_transient( $cache_key );
2604
			$file_data_option = false;
2605
		}
2606
2607
		if ( false === $file_data_option ) {
2608
			$file_data_option = array();
2609
		}
2610
2611
		$key           = md5( $file_name . serialize( $headers ) );
2612
		$refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 );
2613
2614
		// If we don't need to refresh the cache, and already have the value, short-circuit!
2615
		if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) {
2616
			return $file_data_option[ $key ];
2617
		}
2618
2619
		$data = get_file_data( $file, $headers );
2620
2621
		$file_data_option[ $key ] = $data;
2622
2623
		set_transient( $cache_key, $file_data_option, 29 * DAY_IN_SECONDS );
2624
2625
		return $data;
2626
	}
2627
2628
2629
	/**
2630
	 * Return translated module tag.
2631
	 *
2632
	 * @param string $tag Tag as it appears in each module heading.
2633
	 *
2634
	 * @return mixed
2635
	 */
2636
	public static function translate_module_tag( $tag ) {
2637
		return jetpack_get_module_i18n_tag( $tag );
2638
	}
2639
2640
	/**
2641
	 * Get i18n strings as a JSON-encoded string
2642
	 *
2643
	 * @return string The locale as JSON
2644
	 */
2645
	public static function get_i18n_data_json() {
2646
2647
		// WordPress 5.0 uses md5 hashes of file paths to associate translation
2648
		// JSON files with the file they should be included for. This is an md5
2649
		// of '_inc/build/admin.js'.
2650
		$path_md5 = '1bac79e646a8bf4081a5011ab72d5807';
2651
2652
		$i18n_json =
2653
				   JETPACK__PLUGIN_DIR
2654
				   . 'languages/json/jetpack-'
2655
				   . get_user_locale()
2656
				   . '-'
2657
				   . $path_md5
2658
				   . '.json';
2659
2660
		if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
2661
			$locale_data = @file_get_contents( $i18n_json );
2662
			if ( $locale_data ) {
2663
				return $locale_data;
2664
			}
2665
		}
2666
2667
		// Return valid empty Jed locale
2668
		return '{ "locale_data": { "messages": { "": {} } } }';
2669
	}
2670
2671
	/**
2672
	 * Add locale data setup to wp-i18n
2673
	 *
2674
	 * Any Jetpack script that depends on wp-i18n should use this method to set up the locale.
2675
	 *
2676
	 * The locale setup depends on an adding inline script. This is error-prone and could easily
2677
	 * result in multiple additions of the same script when exactly 0 or 1 is desireable.
2678
	 *
2679
	 * This method provides a safe way to request the setup multiple times but add the script at
2680
	 * most once.
2681
	 *
2682
	 * @since 6.7.0
2683
	 *
2684
	 * @return void
2685
	 */
2686
	public static function setup_wp_i18n_locale_data() {
2687
		static $script_added = false;
2688
		if ( ! $script_added ) {
2689
			$script_added = true;
2690
			wp_add_inline_script(
2691
				'wp-i18n',
2692
				'wp.i18n.setLocaleData( ' . self::get_i18n_data_json() . ', \'jetpack\' );'
2693
			);
2694
		}
2695
	}
2696
2697
	/**
2698
	 * Return module name translation. Uses matching string created in modules/module-headings.php.
2699
	 *
2700
	 * @since 3.9.2
2701
	 *
2702
	 * @param array $modules
2703
	 *
2704
	 * @return string|void
2705
	 */
2706
	public static function get_translated_modules( $modules ) {
2707
		foreach ( $modules as $index => $module ) {
2708
			$i18n_module = jetpack_get_module_i18n( $module['module'] );
2709
			if ( isset( $module['name'] ) ) {
2710
				$modules[ $index ]['name'] = $i18n_module['name'];
2711
			}
2712
			if ( isset( $module['description'] ) ) {
2713
				$modules[ $index ]['description']       = $i18n_module['description'];
2714
				$modules[ $index ]['short_description'] = $i18n_module['description'];
2715
			}
2716
		}
2717
		return $modules;
2718
	}
2719
2720
	/**
2721
	 * Get a list of activated modules as an array of module slugs.
2722
	 */
2723
	public static function get_active_modules() {
2724
		$active = Jetpack_Options::get_option( 'active_modules' );
2725
2726
		if ( ! is_array( $active ) ) {
2727
			$active = array();
2728
		}
2729
2730
		if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) {
2731
			$active[] = 'vaultpress';
2732
		} else {
2733
			$active = array_diff( $active, array( 'vaultpress' ) );
2734
		}
2735
2736
		// If protect is active on the main site of a multisite, it should be active on all sites.
2737
		if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) {
2738
			$active[] = 'protect';
2739
		}
2740
2741
		/**
2742
		 * Allow filtering of the active modules.
2743
		 *
2744
		 * Gives theme and plugin developers the power to alter the modules that
2745
		 * are activated on the fly.
2746
		 *
2747
		 * @since 5.8.0
2748
		 *
2749
		 * @param array $active Array of active module slugs.
2750
		 */
2751
		$active = apply_filters( 'jetpack_active_modules', $active );
2752
2753
		return array_unique( $active );
2754
	}
2755
2756
	/**
2757
	 * Check whether or not a Jetpack module is active.
2758
	 *
2759
	 * @param string $module The slug of a Jetpack module.
2760
	 * @return bool
2761
	 *
2762
	 * @static
2763
	 */
2764
	public static function is_module_active( $module ) {
2765
		return in_array( $module, self::get_active_modules() );
2766
	}
2767
2768
	public static function is_module( $module ) {
2769
		return ! empty( $module ) && ! validate_file( $module, self::get_available_modules() );
2770
	}
2771
2772
	/**
2773
	 * Catches PHP errors.  Must be used in conjunction with output buffering.
2774
	 *
2775
	 * @param bool $catch True to start catching, False to stop.
2776
	 *
2777
	 * @static
2778
	 */
2779
	public static function catch_errors( $catch ) {
2780
		static $display_errors, $error_reporting;
2781
2782
		if ( $catch ) {
2783
			$display_errors  = @ini_set( 'display_errors', 1 );
2784
			$error_reporting = @error_reporting( E_ALL );
2785
			add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
2786
		} else {
2787
			@ini_set( 'display_errors', $display_errors );
2788
			@error_reporting( $error_reporting );
2789
			remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
2790
		}
2791
	}
2792
2793
	/**
2794
	 * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
2795
	 */
2796
	public static function catch_errors_on_shutdown() {
2797
		self::state( 'php_errors', self::alias_directories( ob_get_clean() ) );
2798
	}
2799
2800
	/**
2801
	 * Rewrite any string to make paths easier to read.
2802
	 *
2803
	 * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR
2804
	 * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR.
2805
	 *
2806
	 * @param $string
2807
	 * @return mixed
2808
	 */
2809
	public static function alias_directories( $string ) {
2810
		// ABSPATH has a trailing slash.
2811
		$string = str_replace( ABSPATH, 'ABSPATH/', $string );
2812
		// WP_CONTENT_DIR does not have a trailing slash.
2813
		$string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string );
2814
2815
		return $string;
2816
	}
2817
2818
	public static function activate_default_modules(
2819
		$min_version = false,
2820
		$max_version = false,
2821
		$other_modules = array(),
2822
		$redirect = null,
2823
		$send_state_messages = null
2824
	) {
2825
		$jetpack = self::init();
2826
2827
		if ( is_null( $redirect ) ) {
2828
			if (
2829
				( defined( 'REST_REQUEST' ) && REST_REQUEST )
2830
			||
2831
				( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
2832
			||
2833
				( defined( 'WP_CLI' ) && WP_CLI )
2834
			||
2835
				( defined( 'DOING_CRON' ) && DOING_CRON )
2836
			||
2837
				( defined( 'DOING_AJAX' ) && DOING_AJAX )
2838
			) {
2839
				$redirect = false;
2840
			} elseif ( is_admin() ) {
2841
				$redirect = true;
2842
			} else {
2843
				$redirect = false;
2844
			}
2845
		}
2846
2847
		if ( is_null( $send_state_messages ) ) {
2848
			$send_state_messages = current_user_can( 'jetpack_activate_modules' );
2849
		}
2850
2851
		$modules = self::get_default_modules( $min_version, $max_version );
2852
		$modules = array_merge( $other_modules, $modules );
2853
2854
		// Look for standalone plugins and disable if active.
2855
2856
		$to_deactivate = array();
2857
		foreach ( $modules as $module ) {
2858
			if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) {
2859
				$to_deactivate[ $module ] = $jetpack->plugins_to_deactivate[ $module ];
2860
			}
2861
		}
2862
2863
		$deactivated = array();
2864
		foreach ( $to_deactivate as $module => $deactivate_me ) {
2865
			list( $probable_file, $probable_title ) = $deactivate_me;
2866
			if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) {
2867
				$deactivated[] = $module;
2868
			}
2869
		}
2870
2871
		if ( $deactivated ) {
2872
			if ( $send_state_messages ) {
2873
				self::state( 'deactivated_plugins', join( ',', $deactivated ) );
2874
			}
2875
2876
			if ( $redirect ) {
2877
				$url = add_query_arg(
2878
					array(
2879
						'action'   => 'activate_default_modules',
2880
						'_wpnonce' => wp_create_nonce( 'activate_default_modules' ),
2881
					),
2882
					add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), self::admin_url( 'page=jetpack' ) )
2883
				);
2884
				wp_safe_redirect( $url );
2885
				exit;
2886
			}
2887
		}
2888
2889
		/**
2890
		 * Fires before default modules are activated.
2891
		 *
2892
		 * @since 1.9.0
2893
		 *
2894
		 * @param string $min_version Minimum version number required to use modules.
2895
		 * @param string $max_version Maximum version number required to use modules.
2896
		 * @param array $other_modules Array of other modules to activate alongside the default modules.
2897
		 */
2898
		do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules );
2899
2900
		// Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating
2901
		if ( $send_state_messages ) {
2902
			self::restate();
2903
			self::catch_errors( true );
2904
		}
2905
2906
		$active = self::get_active_modules();
2907
2908
		foreach ( $modules as $module ) {
2909
			if ( did_action( "jetpack_module_loaded_$module" ) ) {
2910
				$active[] = $module;
2911
				self::update_active_modules( $active );
2912
				continue;
2913
			}
2914
2915
			if ( $send_state_messages && in_array( $module, $active ) ) {
2916
				$module_info = self::get_module( $module );
2917 View Code Duplication
				if ( ! $module_info['deactivate'] ) {
2918
					$state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
2919
					if ( $active_state = self::state( $state ) ) {
2920
						$active_state = explode( ',', $active_state );
2921
					} else {
2922
						$active_state = array();
2923
					}
2924
					$active_state[] = $module;
2925
					self::state( $state, implode( ',', $active_state ) );
2926
				}
2927
				continue;
2928
			}
2929
2930
			$file = self::get_module_path( $module );
2931
			if ( ! file_exists( $file ) ) {
2932
				continue;
2933
			}
2934
2935
			// we'll override this later if the plugin can be included without fatal error
2936
			if ( $redirect ) {
2937
				wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
2938
			}
2939
2940
			if ( $send_state_messages ) {
2941
				self::state( 'error', 'module_activation_failed' );
2942
				self::state( 'module', $module );
2943
			}
2944
2945
			ob_start();
2946
			require_once $file;
2947
2948
			$active[] = $module;
2949
2950 View Code Duplication
			if ( $send_state_messages ) {
2951
2952
				$state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
2953
				if ( $active_state = self::state( $state ) ) {
2954
					$active_state = explode( ',', $active_state );
2955
				} else {
2956
					$active_state = array();
2957
				}
2958
				$active_state[] = $module;
2959
				self::state( $state, implode( ',', $active_state ) );
2960
			}
2961
2962
			self::update_active_modules( $active );
2963
2964
			ob_end_clean();
2965
		}
2966
2967
		if ( $send_state_messages ) {
2968
			self::state( 'error', false );
2969
			self::state( 'module', false );
2970
		}
2971
2972
		self::catch_errors( false );
2973
		/**
2974
		 * Fires when default modules are activated.
2975
		 *
2976
		 * @since 1.9.0
2977
		 *
2978
		 * @param string $min_version Minimum version number required to use modules.
2979
		 * @param string $max_version Maximum version number required to use modules.
2980
		 * @param array $other_modules Array of other modules to activate alongside the default modules.
2981
		 */
2982
		do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules );
2983
	}
2984
2985
	public static function activate_module( $module, $exit = true, $redirect = true ) {
2986
		/**
2987
		 * Fires before a module is activated.
2988
		 *
2989
		 * @since 2.6.0
2990
		 *
2991
		 * @param string $module Module slug.
2992
		 * @param bool $exit Should we exit after the module has been activated. Default to true.
2993
		 * @param bool $redirect Should the user be redirected after module activation? Default to true.
2994
		 */
2995
		do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect );
2996
2997
		$jetpack = self::init();
2998
2999
		if ( ! strlen( $module ) ) {
3000
			return false;
3001
		}
3002
3003
		if ( ! self::is_module( $module ) ) {
3004
			return false;
3005
		}
3006
3007
		// If it's already active, then don't do it again
3008
		$active = self::get_active_modules();
3009
		foreach ( $active as $act ) {
3010
			if ( $act == $module ) {
3011
				return true;
3012
			}
3013
		}
3014
3015
		$module_data = self::get_module( $module );
3016
3017
		if ( ! self::is_active() ) {
3018
			if ( ! self::is_development_mode() && ! self::is_onboarding() ) {
3019
				return false;
3020
			}
3021
3022
			// If we're not connected but in development mode, make sure the module doesn't require a connection
3023
			if ( self::is_development_mode() && $module_data['requires_connection'] ) {
3024
				return false;
3025
			}
3026
		}
3027
3028
		// Check and see if the old plugin is active
3029
		if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) {
3030
			// Deactivate the old plugin
3031
			if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) {
3032
				// If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module
3033
				// We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load.
3034
				self::state( 'deactivated_plugins', $module );
3035
				wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
3036
				exit;
3037
			}
3038
		}
3039
3040
		// Protect won't work with mis-configured IPs
3041
		if ( 'protect' === $module ) {
3042
			include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
3043
			if ( ! jetpack_protect_get_ip() ) {
3044
				self::state( 'message', 'protect_misconfigured_ip' );
3045
				return false;
3046
			}
3047
		}
3048
3049
		if ( ! Jetpack_Plan::supports( $module ) ) {
3050
			return false;
3051
		}
3052
3053
		// Check the file for fatal errors, a la wp-admin/plugins.php::activate
3054
		self::state( 'module', $module );
3055
		self::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error
3056
3057
		self::catch_errors( true );
3058
		ob_start();
3059
		require self::get_module_path( $module );
3060
		/** This action is documented in class.jetpack.php */
3061
		do_action( 'jetpack_activate_module', $module );
3062
		$active[] = $module;
3063
		self::update_active_modules( $active );
3064
3065
		self::state( 'error', false ); // the override
3066
		ob_end_clean();
3067
		self::catch_errors( false );
3068
3069
		if ( $redirect ) {
3070
			wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
3071
		}
3072
		if ( $exit ) {
3073
			exit;
3074
		}
3075
		return true;
3076
	}
3077
3078
	function activate_module_actions( $module ) {
3079
		_deprecated_function( __METHOD__, 'jetpack-4.2' );
3080
	}
3081
3082
	public static function deactivate_module( $module ) {
3083
		/**
3084
		 * Fires when a module is deactivated.
3085
		 *
3086
		 * @since 1.9.0
3087
		 *
3088
		 * @param string $module Module slug.
3089
		 */
3090
		do_action( 'jetpack_pre_deactivate_module', $module );
3091
3092
		$jetpack = self::init();
3093
3094
		$active = self::get_active_modules();
3095
		$new    = array_filter( array_diff( $active, (array) $module ) );
3096
3097
		return self::update_active_modules( $new );
3098
	}
3099
3100
	public static function enable_module_configurable( $module ) {
3101
		$module = self::get_module_slug( $module );
3102
		add_filter( 'jetpack_module_configurable_' . $module, '__return_true' );
3103
	}
3104
3105
	/**
3106
	 * Composes a module configure URL. It uses Jetpack settings search as default value
3107
	 * It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter
3108
	 *
3109
	 * @param string $module Module slug
3110
	 * @return string $url module configuration URL
3111
	 */
3112
	public static function module_configuration_url( $module ) {
3113
		$module      = self::get_module_slug( $module );
3114
		$default_url = self::admin_url() . "#/settings?term=$module";
3115
		/**
3116
		 * Allows to modify configure_url of specific module to be able to redirect to some custom location.
3117
		 *
3118
		 * @since 6.9.0
3119
		 *
3120
		 * @param string $default_url Default url, which redirects to jetpack settings page.
3121
		 */
3122
		$url = apply_filters( 'jetpack_module_configuration_url_' . $module, $default_url );
3123
3124
		return $url;
3125
	}
3126
3127
	/* Installation */
3128
	public static function bail_on_activation( $message, $deactivate = true ) {
3129
		?>
3130
<!doctype html>
3131
<html>
3132
<head>
3133
<meta charset="<?php bloginfo( 'charset' ); ?>">
3134
<style>
3135
* {
3136
	text-align: center;
3137
	margin: 0;
3138
	padding: 0;
3139
	font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
3140
}
3141
p {
3142
	margin-top: 1em;
3143
	font-size: 18px;
3144
}
3145
</style>
3146
<body>
3147
<p><?php echo esc_html( $message ); ?></p>
3148
</body>
3149
</html>
3150
		<?php
3151
		if ( $deactivate ) {
3152
			$plugins = get_option( 'active_plugins' );
3153
			$jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' );
3154
			$update  = false;
3155
			foreach ( $plugins as $i => $plugin ) {
3156
				if ( $plugin === $jetpack ) {
3157
					$plugins[ $i ] = false;
3158
					$update        = true;
3159
				}
3160
			}
3161
3162
			if ( $update ) {
3163
				update_option( 'active_plugins', array_filter( $plugins ) );
3164
			}
3165
		}
3166
		exit;
3167
	}
3168
3169
	/**
3170
	 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
3171
	 *
3172
	 * @static
3173
	 */
3174
	public static function plugin_activation( $network_wide ) {
3175
		Jetpack_Options::update_option( 'activated', 1 );
3176
3177
		if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
3178
			self::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) );
3179
		}
3180
3181
		if ( $network_wide ) {
3182
			self::state( 'network_nag', true );
3183
		}
3184
3185
		// For firing one-off events (notices) immediately after activation
3186
		set_transient( 'activated_jetpack', true, .1 * MINUTE_IN_SECONDS );
3187
3188
		update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) );
3189
3190
		self::plugin_initialize();
3191
	}
3192
3193
	public static function get_activation_source( $referer_url ) {
3194
3195
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
3196
			return array( 'wp-cli', null );
3197
		}
3198
3199
		$referer = parse_url( $referer_url );
3200
3201
		$source_type  = 'unknown';
3202
		$source_query = null;
3203
3204
		if ( ! is_array( $referer ) ) {
3205
			return array( $source_type, $source_query );
3206
		}
3207
3208
		$plugins_path         = parse_url( admin_url( 'plugins.php' ), PHP_URL_PATH );
3209
		$plugins_install_path = parse_url( admin_url( 'plugin-install.php' ), PHP_URL_PATH );// /wp-admin/plugin-install.php
3210
3211
		if ( isset( $referer['query'] ) ) {
3212
			parse_str( $referer['query'], $query_parts );
3213
		} else {
3214
			$query_parts = array();
3215
		}
3216
3217
		if ( $plugins_path === $referer['path'] ) {
3218
			$source_type = 'list';
3219
		} elseif ( $plugins_install_path === $referer['path'] ) {
3220
			$tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured';
3221
			switch ( $tab ) {
3222
				case 'popular':
3223
					$source_type = 'popular';
3224
					break;
3225
				case 'recommended':
3226
					$source_type = 'recommended';
3227
					break;
3228
				case 'favorites':
3229
					$source_type = 'favorites';
3230
					break;
3231
				case 'search':
3232
					$source_type  = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' );
3233
					$source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null;
3234
					break;
3235
				default:
3236
					$source_type = 'featured';
3237
			}
3238
		}
3239
3240
		return array( $source_type, $source_query );
3241
	}
3242
3243
	/**
3244
	 * Runs before bumping version numbers up to a new version
3245
	 *
3246
	 * @param  string $version    Version:timestamp
3247
	 * @param  string $old_version Old Version:timestamp or false if not set yet.
3248
	 * @return null              [description]
3249
	 */
3250
	public static function do_version_bump( $version, $old_version ) {
3251
		if ( ! $old_version ) { // For new sites
3252
			// There used to be stuff here, but this seems like it might  be useful to someone in the future...
3253
		}
3254
	}
3255
3256
	/**
3257
	 * Sets the internal version number and activation state.
3258
	 *
3259
	 * @static
3260
	 */
3261
	public static function plugin_initialize() {
3262
		if ( ! Jetpack_Options::get_option( 'activated' ) ) {
3263
			Jetpack_Options::update_option( 'activated', 2 );
3264
		}
3265
3266 View Code Duplication
		if ( ! Jetpack_Options::get_option( 'version' ) ) {
3267
			$version = $old_version = JETPACK__VERSION . ':' . time();
3268
			/** This action is documented in class.jetpack.php */
3269
			do_action( 'updating_jetpack_version', $version, false );
3270
			Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
3271
		}
3272
3273
		self::load_modules();
3274
3275
		Jetpack_Options::delete_option( 'do_activate' );
3276
		Jetpack_Options::delete_option( 'dismissed_connection_banner' );
3277
	}
3278
3279
	/**
3280
	 * Removes all connection options
3281
	 *
3282
	 * @static
3283
	 */
3284
	public static function plugin_deactivation() {
3285
		require_once ABSPATH . '/wp-admin/includes/plugin.php';
3286
		if ( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
3287
			Jetpack_Network::init()->deactivate();
3288
		} else {
3289
			self::disconnect( false );
3290
			// Jetpack_Heartbeat::init()->deactivate();
3291
		}
3292
	}
3293
3294
	/**
3295
	 * Disconnects from the Jetpack servers.
3296
	 * Forgets all connection details and tells the Jetpack servers to do the same.
3297
	 *
3298
	 * @static
3299
	 */
3300
	public static function disconnect( $update_activated_state = true ) {
3301
		wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
3302
		$connection = self::connection();
3303
		$connection->clean_nonces( true );
3304
3305
		// If the site is in an IDC because sync is not allowed,
3306
		// let's make sure to not disconnect the production site.
3307
		if ( ! self::validate_sync_error_idc_option() ) {
3308
			$tracking = new Tracking();
3309
			$tracking->record_user_event( 'disconnect_site', array() );
3310
3311
			$xml = new Jetpack_IXR_Client();
3312
			$xml->query( 'jetpack.deregister', get_current_user_id() );
3313
		}
3314
3315
		Jetpack_Options::delete_option(
3316
			array(
3317
				'blog_token',
3318
				'user_token',
3319
				'user_tokens',
3320
				'master_user',
3321
				'time_diff',
3322
				'fallback_no_verify_ssl_certs',
3323
			)
3324
		);
3325
3326
		Jetpack_IDC::clear_all_idc_options();
3327
		Jetpack_Options::delete_raw_option( 'jetpack_secrets' );
3328
3329
		if ( $update_activated_state ) {
3330
			Jetpack_Options::update_option( 'activated', 4 );
3331
		}
3332
3333
		if ( $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ) ) {
3334
			// Check then record unique disconnection if site has never been disconnected previously
3335
			if ( - 1 == $jetpack_unique_connection['disconnected'] ) {
3336
				$jetpack_unique_connection['disconnected'] = 1;
3337
			} else {
3338
				if ( 0 == $jetpack_unique_connection['disconnected'] ) {
3339
					// track unique disconnect
3340
					$jetpack = self::init();
3341
3342
					$jetpack->stat( 'connections', 'unique-disconnect' );
3343
					$jetpack->do_stats( 'server_side' );
3344
				}
3345
				// increment number of times disconnected
3346
				$jetpack_unique_connection['disconnected'] += 1;
3347
			}
3348
3349
			Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection );
3350
		}
3351
3352
		// Delete cached connected user data
3353
		$transient_key = 'jetpack_connected_user_data_' . get_current_user_id();
3354
		delete_transient( $transient_key );
3355
3356
		// Delete all the sync related data. Since it could be taking up space.
3357
		Sender::get_instance()->uninstall();
3358
3359
		// Disable the Heartbeat cron
3360
		Jetpack_Heartbeat::init()->deactivate();
3361
	}
3362
3363
	/**
3364
	 * Unlinks the current user from the linked WordPress.com user.
3365
	 *
3366
	 * @deprecated since 7.7
3367
	 * @see Automattic\Jetpack\Connection\Manager::disconnect_user()
3368
	 *
3369
	 * @param Integer $user_id the user identifier.
3370
	 * @return Boolean Whether the disconnection of the user was successful.
3371
	 */
3372
	public static function unlink_user( $user_id = null ) {
3373
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::disconnect_user' );
3374
		return Connection_Manager::disconnect_user( $user_id );
3375
	}
3376
3377
	/**
3378
	 * Attempts Jetpack registration.  If it fail, a state flag is set: @see ::admin_page_load()
3379
	 */
3380
	public static function try_registration() {
3381
		// The user has agreed to the TOS at some point by now.
3382
		Jetpack_Options::update_option( 'tos_agreed', true );
3383
3384
		// Let's get some testing in beta versions and such.
3385
		if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) {
3386
			// Before attempting to connect, let's make sure that the domains are viable.
3387
			$domains_to_check = array_unique(
3388
				array(
3389
					'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ),
3390
					'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ),
3391
				)
3392
			);
3393
			foreach ( $domains_to_check as $domain ) {
3394
				$result = self::connection()->is_usable_domain( $domain );
3395
				if ( is_wp_error( $result ) ) {
3396
					return $result;
3397
				}
3398
			}
3399
		}
3400
3401
		$result = self::register();
3402
3403
		// If there was an error with registration and the site was not registered, record this so we can show a message.
3404
		if ( ! $result || is_wp_error( $result ) ) {
3405
			return $result;
3406
		} else {
3407
			return true;
3408
		}
3409
	}
3410
3411
	/**
3412
	 * Tracking an internal event log. Try not to put too much chaff in here.
3413
	 *
3414
	 * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA)
3415
	 */
3416
	public static function log( $code, $data = null ) {
3417
		// only grab the latest 200 entries
3418
		$log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 );
3419
3420
		// Append our event to the log
3421
		$log_entry = array(
3422
			'time'    => time(),
3423
			'user_id' => get_current_user_id(),
3424
			'blog_id' => Jetpack_Options::get_option( 'id' ),
3425
			'code'    => $code,
3426
		);
3427
		// Don't bother storing it unless we've got some.
3428
		if ( ! is_null( $data ) ) {
3429
			$log_entry['data'] = $data;
3430
		}
3431
		$log[] = $log_entry;
3432
3433
		// Try add_option first, to make sure it's not autoloaded.
3434
		// @todo: Add an add_option method to Jetpack_Options
3435
		if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) {
3436
			Jetpack_Options::update_option( 'log', $log );
3437
		}
3438
3439
		/**
3440
		 * Fires when Jetpack logs an internal event.
3441
		 *
3442
		 * @since 3.0.0
3443
		 *
3444
		 * @param array $log_entry {
3445
		 *  Array of details about the log entry.
3446
		 *
3447
		 *  @param string time Time of the event.
3448
		 *  @param int user_id ID of the user who trigerred the event.
3449
		 *  @param int blog_id Jetpack Blog ID.
3450
		 *  @param string code Unique name for the event.
3451
		 *  @param string data Data about the event.
3452
		 * }
3453
		 */
3454
		do_action( 'jetpack_log_entry', $log_entry );
3455
	}
3456
3457
	/**
3458
	 * Get the internal event log.
3459
	 *
3460
	 * @param $event (string) - only return the specific log events
3461
	 * @param $num   (int)    - get specific number of latest results, limited to 200
3462
	 *
3463
	 * @return array of log events || WP_Error for invalid params
3464
	 */
3465
	public static function get_log( $event = false, $num = false ) {
3466
		if ( $event && ! is_string( $event ) ) {
3467
			return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) );
3468
		}
3469
3470
		if ( $num && ! is_numeric( $num ) ) {
3471
			return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) );
3472
		}
3473
3474
		$entire_log = Jetpack_Options::get_option( 'log', array() );
3475
3476
		// If nothing set - act as it did before, otherwise let's start customizing the output
3477
		if ( ! $num && ! $event ) {
3478
			return $entire_log;
3479
		} else {
3480
			$entire_log = array_reverse( $entire_log );
3481
		}
3482
3483
		$custom_log_output = array();
3484
3485
		if ( $event ) {
3486
			foreach ( $entire_log as $log_event ) {
3487
				if ( $event == $log_event['code'] ) {
3488
					$custom_log_output[] = $log_event;
3489
				}
3490
			}
3491
		} else {
3492
			$custom_log_output = $entire_log;
3493
		}
3494
3495
		if ( $num ) {
3496
			$custom_log_output = array_slice( $custom_log_output, 0, $num );
3497
		}
3498
3499
		return $custom_log_output;
3500
	}
3501
3502
	/**
3503
	 * Log modification of important settings.
3504
	 */
3505
	public static function log_settings_change( $option, $old_value, $value ) {
3506
		switch ( $option ) {
3507
			case 'jetpack_sync_non_public_post_stati':
3508
				self::log( $option, $value );
3509
				break;
3510
		}
3511
	}
3512
3513
	/**
3514
	 * Return stat data for WPCOM sync
3515
	 */
3516
	public static function get_stat_data( $encode = true, $extended = true ) {
3517
		$data = Jetpack_Heartbeat::generate_stats_array();
3518
3519
		if ( $extended ) {
3520
			$additional_data = self::get_additional_stat_data();
3521
			$data            = array_merge( $data, $additional_data );
3522
		}
3523
3524
		if ( $encode ) {
3525
			return json_encode( $data );
3526
		}
3527
3528
		return $data;
3529
	}
3530
3531
	/**
3532
	 * Get additional stat data to sync to WPCOM
3533
	 */
3534
	public static function get_additional_stat_data( $prefix = '' ) {
3535
		$return[ "{$prefix}themes" ]        = self::get_parsed_theme_data();
3536
		$return[ "{$prefix}plugins-extra" ] = self::get_parsed_plugin_data();
3537
		$return[ "{$prefix}users" ]         = (int) self::get_site_user_count();
3538
		$return[ "{$prefix}site-count" ]    = 0;
3539
3540
		if ( function_exists( 'get_blog_count' ) ) {
3541
			$return[ "{$prefix}site-count" ] = get_blog_count();
3542
		}
3543
		return $return;
3544
	}
3545
3546
	private static function get_site_user_count() {
3547
		global $wpdb;
3548
3549
		if ( function_exists( 'wp_is_large_network' ) ) {
3550
			if ( wp_is_large_network( 'users' ) ) {
3551
				return -1; // Not a real value but should tell us that we are dealing with a large network.
3552
			}
3553
		}
3554
		if ( false === ( $user_count = get_transient( 'jetpack_site_user_count' ) ) ) {
3555
			// It wasn't there, so regenerate the data and save the transient
3556
			$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities'" );
3557
			set_transient( 'jetpack_site_user_count', $user_count, DAY_IN_SECONDS );
3558
		}
3559
		return $user_count;
3560
	}
3561
3562
	/* Admin Pages */
3563
3564
	function admin_init() {
3565
		// If the plugin is not connected, display a connect message.
3566
		if (
3567
			// the plugin was auto-activated and needs its candy
3568
			Jetpack_Options::get_option_and_ensure_autoload( 'do_activate', '0' )
3569
		||
3570
			// the plugin is active, but was never activated.  Probably came from a site-wide network activation
3571
			! Jetpack_Options::get_option( 'activated' )
3572
		) {
3573
			self::plugin_initialize();
3574
		}
3575
3576
		if ( ! self::is_active() && ! self::is_development_mode() ) {
3577
			Jetpack_Connection_Banner::init();
3578
		} elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) {
3579
			// Upgrade: 1.1 -> 1.1.1
3580
			// Check and see if host can verify the Jetpack servers' SSL certificate
3581
			$args       = array();
3582
			$connection = self::connection();
3583
			Client::_wp_remote_request(
3584
				self::fix_url_for_bad_hosts( $connection->api_url( 'test' ) ),
3585
				$args,
3586
				true
3587
			);
3588
		}
3589
3590
		if ( current_user_can( 'manage_options' ) && 'AUTO' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) {
3591
			add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
3592
		}
3593
3594
		add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
3595
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
3596
		add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) );
3597
3598
		if ( self::is_active() || self::is_development_mode() ) {
3599
			// Artificially throw errors in certain whitelisted cases during plugin activation
3600
			add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
3601
		}
3602
3603
		// Add custom column in wp-admin/users.php to show whether user is linked.
3604
		add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) );
3605
		add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 );
3606
		add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) );
3607
	}
3608
3609
	function admin_body_class( $admin_body_class = '' ) {
3610
		$classes = explode( ' ', trim( $admin_body_class ) );
3611
3612
		$classes[] = self::is_active() ? 'jetpack-connected' : 'jetpack-disconnected';
3613
3614
		$admin_body_class = implode( ' ', array_unique( $classes ) );
3615
		return " $admin_body_class ";
3616
	}
3617
3618
	static function add_jetpack_pagestyles( $admin_body_class = '' ) {
3619
		return $admin_body_class . ' jetpack-pagestyles ';
3620
	}
3621
3622
	/**
3623
	 * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
3624
	 * This function artificially throws errors for such cases (whitelisted).
3625
	 *
3626
	 * @param string $plugin The activated plugin.
3627
	 */
3628
	function throw_error_on_activate_plugin( $plugin ) {
3629
		$active_modules = self::get_active_modules();
3630
3631
		// The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks.
3632
		if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) {
3633
			$throw = false;
3634
3635
			// Try and make sure it really was the stats plugin
3636
			if ( ! class_exists( 'ReflectionFunction' ) ) {
3637
				if ( 'stats.php' == basename( $plugin ) ) {
3638
					$throw = true;
3639
				}
3640
			} else {
3641
				$reflection = new ReflectionFunction( 'stats_get_api_key' );
3642
				if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) {
3643
					$throw = true;
3644
				}
3645
			}
3646
3647
			if ( $throw ) {
3648
				trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR );
3649
			}
3650
		}
3651
	}
3652
3653
	function intercept_plugin_error_scrape_init() {
3654
		add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 );
3655
	}
3656
3657
	function intercept_plugin_error_scrape( $action, $result ) {
3658
		if ( ! $result ) {
3659
			return;
3660
		}
3661
3662
		foreach ( $this->plugins_to_deactivate as $deactivate_me ) {
3663
			if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) {
3664
				self::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), $deactivate_me[1] ), false );
3665
			}
3666
		}
3667
	}
3668
3669
	/**
3670
	 * Register the remote file upload request handlers, if needed.
3671
	 *
3672
	 * @access public
3673
	 */
3674
	public function add_remote_request_handlers() {
3675
		// Remote file uploads are allowed only via AJAX requests.
3676
		if ( ! is_admin() || ! Constants::get_constant( 'DOING_AJAX' ) ) {
3677
			return;
3678
		}
3679
3680
		// Remote file uploads are allowed only for a set of specific AJAX actions.
3681
		$remote_request_actions = array(
3682
			'jetpack_upload_file',
3683
			'jetpack_update_file',
3684
		);
3685
3686
		// phpcs:ignore WordPress.Security.NonceVerification
3687
		if ( ! isset( $_POST['action'] ) || ! in_array( $_POST['action'], $remote_request_actions, true ) ) {
3688
			return;
3689
		}
3690
3691
		// Require Jetpack authentication for the remote file upload AJAX requests.
3692
		$this->connection_manager->require_jetpack_authentication();
3693
3694
		// Register the remote file upload AJAX handlers.
3695
		foreach ( $remote_request_actions as $action ) {
3696
			add_action( "wp_ajax_nopriv_{$action}", array( $this, 'remote_request_handlers' ) );
3697
		}
3698
	}
3699
3700
	/**
3701
	 * Handler for Jetpack remote file uploads.
3702
	 *
3703
	 * @access public
3704
	 */
3705
	public function remote_request_handlers() {
3706
		$action = current_filter();
3707
3708
		switch ( current_filter() ) {
3709
			case 'wp_ajax_nopriv_jetpack_upload_file':
3710
				$response = $this->upload_handler();
3711
				break;
3712
3713
			case 'wp_ajax_nopriv_jetpack_update_file':
3714
				$response = $this->upload_handler( true );
3715
				break;
3716
			default:
3717
				$response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 );
3718
				break;
3719
		}
3720
3721
		if ( ! $response ) {
3722
			$response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 );
3723
		}
3724
3725
		if ( is_wp_error( $response ) ) {
3726
			$status_code       = $response->get_error_data();
3727
			$error             = $response->get_error_code();
3728
			$error_description = $response->get_error_message();
3729
3730
			if ( ! is_int( $status_code ) ) {
3731
				$status_code = 400;
3732
			}
3733
3734
			status_header( $status_code );
3735
			die( json_encode( (object) compact( 'error', 'error_description' ) ) );
3736
		}
3737
3738
		status_header( 200 );
3739
		if ( true === $response ) {
3740
			exit;
3741
		}
3742
3743
		die( json_encode( (object) $response ) );
3744
	}
3745
3746
	/**
3747
	 * Uploads a file gotten from the global $_FILES.
3748
	 * If `$update_media_item` is true and `post_id` is defined
3749
	 * the attachment file of the media item (gotten through of the post_id)
3750
	 * will be updated instead of add a new one.
3751
	 *
3752
	 * @param  boolean $update_media_item - update media attachment
3753
	 * @return array - An array describing the uploadind files process
3754
	 */
3755
	function upload_handler( $update_media_item = false ) {
3756
		if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
3757
			return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 );
3758
		}
3759
3760
		$user = wp_authenticate( '', '' );
3761
		if ( ! $user || is_wp_error( $user ) ) {
3762
			return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 );
3763
		}
3764
3765
		wp_set_current_user( $user->ID );
3766
3767
		if ( ! current_user_can( 'upload_files' ) ) {
3768
			return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 );
3769
		}
3770
3771
		if ( empty( $_FILES ) ) {
3772
			return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 );
3773
		}
3774
3775
		foreach ( array_keys( $_FILES ) as $files_key ) {
3776
			if ( ! isset( $_POST[ "_jetpack_file_hmac_{$files_key}" ] ) ) {
3777
				return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 );
3778
			}
3779
		}
3780
3781
		$media_keys = array_keys( $_FILES['media'] );
3782
3783
		$token = Jetpack_Data::get_access_token( get_current_user_id() );
3784
		if ( ! $token || is_wp_error( $token ) ) {
3785
			return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 );
3786
		}
3787
3788
		$uploaded_files = array();
3789
		$global_post    = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
3790
		unset( $GLOBALS['post'] );
3791
		foreach ( $_FILES['media']['name'] as $index => $name ) {
3792
			$file = array();
3793
			foreach ( $media_keys as $media_key ) {
3794
				$file[ $media_key ] = $_FILES['media'][ $media_key ][ $index ];
3795
			}
3796
3797
			list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][ $index ] );
3798
3799
			$hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret );
3800
			if ( $hmac_provided !== $hmac_file ) {
3801
				$uploaded_files[ $index ] = (object) array(
3802
					'error'             => 'invalid_hmac',
3803
					'error_description' => 'The corresponding HMAC for this file does not match',
3804
				);
3805
				continue;
3806
			}
3807
3808
			$_FILES['.jetpack.upload.'] = $file;
3809
			$post_id                    = isset( $_POST['post_id'][ $index ] ) ? absint( $_POST['post_id'][ $index ] ) : 0;
3810
			if ( ! current_user_can( 'edit_post', $post_id ) ) {
3811
				$post_id = 0;
3812
			}
3813
3814
			if ( $update_media_item ) {
3815
				if ( ! isset( $post_id ) || $post_id === 0 ) {
3816
					return new Jetpack_Error( 'invalid_input', 'Media ID must be defined.', 400 );
3817
				}
3818
3819
				$media_array = $_FILES['media'];
3820
3821
				$file_array['name']     = $media_array['name'][0];
3822
				$file_array['type']     = $media_array['type'][0];
3823
				$file_array['tmp_name'] = $media_array['tmp_name'][0];
3824
				$file_array['error']    = $media_array['error'][0];
3825
				$file_array['size']     = $media_array['size'][0];
3826
3827
				$edited_media_item = Jetpack_Media::edit_media_file( $post_id, $file_array );
3828
3829
				if ( is_wp_error( $edited_media_item ) ) {
3830
					return $edited_media_item;
3831
				}
3832
3833
				$response = (object) array(
3834
					'id'   => (string) $post_id,
3835
					'file' => (string) $edited_media_item->post_title,
3836
					'url'  => (string) wp_get_attachment_url( $post_id ),
3837
					'type' => (string) $edited_media_item->post_mime_type,
3838
					'meta' => (array) wp_get_attachment_metadata( $post_id ),
3839
				);
3840
3841
				return (array) array( $response );
3842
			}
3843
3844
			$attachment_id = media_handle_upload(
3845
				'.jetpack.upload.',
3846
				$post_id,
3847
				array(),
3848
				array(
3849
					'action' => 'jetpack_upload_file',
3850
				)
3851
			);
3852
3853
			if ( ! $attachment_id ) {
3854
				$uploaded_files[ $index ] = (object) array(
3855
					'error'             => 'unknown',
3856
					'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site',
3857
				);
3858
			} elseif ( is_wp_error( $attachment_id ) ) {
3859
				$uploaded_files[ $index ] = (object) array(
3860
					'error'             => 'attachment_' . $attachment_id->get_error_code(),
3861
					'error_description' => $attachment_id->get_error_message(),
3862
				);
3863
			} else {
3864
				$attachment               = get_post( $attachment_id );
3865
				$uploaded_files[ $index ] = (object) array(
3866
					'id'   => (string) $attachment_id,
3867
					'file' => $attachment->post_title,
3868
					'url'  => wp_get_attachment_url( $attachment_id ),
3869
					'type' => $attachment->post_mime_type,
3870
					'meta' => wp_get_attachment_metadata( $attachment_id ),
3871
				);
3872
				// Zip files uploads are not supported unless they are done for installation purposed
3873
				// lets delete them in case something goes wrong in this whole process
3874
				if ( 'application/zip' === $attachment->post_mime_type ) {
3875
					// Schedule a cleanup for 2 hours from now in case of failed install.
3876
					wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $attachment_id ) );
3877
				}
3878
			}
3879
		}
3880
		if ( ! is_null( $global_post ) ) {
3881
			$GLOBALS['post'] = $global_post;
3882
		}
3883
3884
		return $uploaded_files;
3885
	}
3886
3887
	/**
3888
	 * Add help to the Jetpack page
3889
	 *
3890
	 * @since Jetpack (1.2.3)
3891
	 * @return false if not the Jetpack page
3892
	 */
3893
	function admin_help() {
3894
		$current_screen = get_current_screen();
3895
3896
		// Overview
3897
		$current_screen->add_help_tab(
3898
			array(
3899
				'id'      => 'home',
3900
				'title'   => __( 'Home', 'jetpack' ),
3901
				'content' =>
3902
					'<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' .
3903
					'<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' .
3904
					'<p>' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '</p>',
3905
			)
3906
		);
3907
3908
		// Screen Content
3909
		if ( current_user_can( 'manage_options' ) ) {
3910
			$current_screen->add_help_tab(
3911
				array(
3912
					'id'      => 'settings',
3913
					'title'   => __( 'Settings', 'jetpack' ),
3914
					'content' =>
3915
						'<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' .
3916
						'<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' .
3917
						'<ol>' .
3918
							'<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' .
3919
							'<li>' . __( 'Using the checkboxes next to each module, you can select multiple modules to toggle via the Bulk Actions menu at the top of the list.', 'jetpack' ) . '</li>' .
3920
						'</ol>' .
3921
						'<p>' . __( 'Using the tools on the right, you can search for specific modules, filter by module categories or which are active, or change the sorting order.', 'jetpack' ) . '</p>',
3922
				)
3923
			);
3924
		}
3925
3926
		// Help Sidebar
3927
		$current_screen->set_help_sidebar(
3928
			'<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' .
3929
			'<p><a href="https://jetpack.com/faq/" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' .
3930
			'<p><a href="https://jetpack.com/support/" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' .
3931
			'<p><a href="' . self::admin_url( array( 'page' => 'jetpack-debugger' ) ) . '">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>'
3932
		);
3933
	}
3934
3935
	function admin_menu_css() {
3936
		wp_enqueue_style( 'jetpack-icons' );
3937
	}
3938
3939
	function admin_menu_order() {
3940
		return true;
3941
	}
3942
3943 View Code Duplication
	function jetpack_menu_order( $menu_order ) {
3944
		$jp_menu_order = array();
3945
3946
		foreach ( $menu_order as $index => $item ) {
3947
			if ( $item != 'jetpack' ) {
3948
				$jp_menu_order[] = $item;
3949
			}
3950
3951
			if ( $index == 0 ) {
3952
				$jp_menu_order[] = 'jetpack';
3953
			}
3954
		}
3955
3956
		return $jp_menu_order;
3957
	}
3958
3959
	function admin_banner_styles() {
3960
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
3961
3962
		if ( ! wp_style_is( 'jetpack-dops-style' ) ) {
3963
			wp_register_style(
3964
				'jetpack-dops-style',
3965
				plugins_url( '_inc/build/admin.css', JETPACK__PLUGIN_FILE ),
3966
				array(),
3967
				JETPACK__VERSION
3968
			);
3969
		}
3970
3971
		wp_enqueue_style(
3972
			'jetpack',
3973
			plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ),
3974
			array( 'jetpack-dops-style' ),
3975
			JETPACK__VERSION . '-20121016'
3976
		);
3977
		wp_style_add_data( 'jetpack', 'rtl', 'replace' );
3978
		wp_style_add_data( 'jetpack', 'suffix', $min );
3979
	}
3980
3981
	function plugin_action_links( $actions ) {
3982
3983
		$jetpack_home = array( 'jetpack-home' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack' ), 'Jetpack' ) );
3984
3985
		if ( current_user_can( 'jetpack_manage_modules' ) && ( self::is_active() || self::is_development_mode() ) ) {
3986
			return array_merge(
3987
				$jetpack_home,
3988
				array( 'settings' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack#/settings' ), __( 'Settings', 'jetpack' ) ) ),
3989
				array( 'support' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack-debugger ' ), __( 'Support', 'jetpack' ) ) ),
3990
				$actions
3991
			);
3992
		}
3993
3994
		return array_merge( $jetpack_home, $actions );
3995
	}
3996
3997
	/*
3998
	 * Registration flow:
3999
	 * 1 - ::admin_page_load() action=register
4000
	 * 2 - ::try_registration()
4001
	 * 3 - ::register()
4002
	 *     - Creates jetpack_register option containing two secrets and a timestamp
4003
	 *     - Calls https://jetpack.wordpress.com/jetpack.register/1/ with
4004
	 *       siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id
4005
	 *     - That request to jetpack.wordpress.com does not immediately respond.  It first makes a request BACK to this site's
4006
	 *       xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1
4007
	 *     - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2
4008
	 *     - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with
4009
	 *       jetpack_id, jetpack_secret, jetpack_public
4010
	 *     - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret
4011
	 * 4 - redirect to https://wordpress.com/start/jetpack-connect
4012
	 * 5 - user logs in with WP.com account
4013
	 * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize
4014
	 *		- Jetpack_Client_Server::authorize()
4015
	 *		- Jetpack_Client_Server::get_token()
4016
	 *		- GET https://jetpack.wordpress.com/jetpack.token/1/ with
4017
	 *        client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login
4018
	 *			- which responds with access_token, token_type, scope
4019
	 *		- Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id
4020
	 *		- Jetpack::activate_default_modules()
4021
	 *     		- Deactivates deprecated plugins
4022
	 *     		- Activates all default modules
4023
	 *		- Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users
4024
	 * 7 - For a new connection, user selects a Jetpack plan on wordpress.com
4025
	 * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized
4026
	 *     Done!
4027
	 */
4028
4029
	/**
4030
	 * Handles the page load events for the Jetpack admin page
4031
	 */
4032
	function admin_page_load() {
4033
		$error = false;
4034
4035
		// Make sure we have the right body class to hook stylings for subpages off of.
4036
		add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ) );
4037
4038
		if ( ! empty( $_GET['jetpack_restate'] ) ) {
4039
			// Should only be used in intermediate redirects to preserve state across redirects
4040
			self::restate();
4041
		}
4042
4043
		if ( isset( $_GET['connect_url_redirect'] ) ) {
4044
			// @todo: Add validation against a known whitelist
4045
			$from = ! empty( $_GET['from'] ) ? $_GET['from'] : 'iframe';
4046
			// User clicked in the iframe to link their accounts
4047
			if ( ! self::is_user_connected() ) {
4048
				$redirect = ! empty( $_GET['redirect_after_auth'] ) ? $_GET['redirect_after_auth'] : false;
4049
4050
				add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) );
4051
				$connect_url = $this->build_connect_url( true, $redirect, $from );
4052
				remove_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) );
4053
4054
				if ( isset( $_GET['notes_iframe'] ) ) {
4055
					$connect_url .= '&notes_iframe';
4056
				}
4057
				wp_redirect( $connect_url );
4058
				exit;
4059
			} else {
4060
				if ( ! isset( $_GET['calypso_env'] ) ) {
4061
					self::state( 'message', 'already_authorized' );
4062
					wp_safe_redirect( self::admin_url() );
4063
					exit;
4064
				} else {
4065
					$connect_url  = $this->build_connect_url( true, false, $from );
4066
					$connect_url .= '&already_authorized=true';
4067
					wp_redirect( $connect_url );
4068
					exit;
4069
				}
4070
			}
4071
		}
4072
4073
		if ( isset( $_GET['action'] ) ) {
4074
			switch ( $_GET['action'] ) {
4075
				case 'authorize':
4076
					if ( self::is_active() && self::is_user_connected() ) {
4077
						self::state( 'message', 'already_authorized' );
4078
						wp_safe_redirect( self::admin_url() );
4079
						exit;
4080
					}
4081
					self::log( 'authorize' );
4082
					$client_server = new Jetpack_Client_Server();
4083
					$client_server->client_authorize();
4084
					exit;
4085
				case 'register':
4086
					if ( ! current_user_can( 'jetpack_connect' ) ) {
4087
						$error = 'cheatin';
4088
						break;
4089
					}
4090
					check_admin_referer( 'jetpack-register' );
4091
					self::log( 'register' );
4092
					self::maybe_set_version_option();
4093
					$registered = self::try_registration();
4094 View Code Duplication
					if ( is_wp_error( $registered ) ) {
4095
						$error = $registered->get_error_code();
4096
						self::state( 'error', $error );
4097
						self::state( 'error', $registered->get_error_message() );
4098
4099
						/**
4100
						 * Jetpack registration Error.
4101
						 *
4102
						 * @since 7.5.0
4103
						 *
4104
						 * @param string|int $error The error code.
4105
						 * @param \WP_Error $registered The error object.
4106
						 */
4107
						do_action( 'jetpack_connection_register_fail', $error, $registered );
4108
						break;
4109
					}
4110
4111
					$from     = isset( $_GET['from'] ) ? $_GET['from'] : false;
4112
					$redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : false;
4113
4114
					/**
4115
					 * Jetpack registration Success.
4116
					 *
4117
					 * @since 7.5.0
4118
					 *
4119
					 * @param string $from 'from' GET parameter;
4120
					 */
4121
					do_action( 'jetpack_connection_register_success', $from );
4122
4123
					$url = $this->build_connect_url( true, $redirect, $from );
4124
4125
					if ( ! empty( $_GET['onboarding'] ) ) {
4126
						$url = add_query_arg( 'onboarding', $_GET['onboarding'], $url );
4127
					}
4128
4129
					if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) {
4130
						$url = add_query_arg( 'auth_approved', 'true', $url );
4131
					}
4132
4133
					wp_redirect( $url );
4134
					exit;
4135
				case 'activate':
4136
					if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
4137
						$error = 'cheatin';
4138
						break;
4139
					}
4140
4141
					$module = stripslashes( $_GET['module'] );
4142
					check_admin_referer( "jetpack_activate-$module" );
4143
					self::log( 'activate', $module );
4144
					if ( ! self::activate_module( $module ) ) {
4145
						self::state( 'error', sprintf( __( 'Could not activate %s', 'jetpack' ), $module ) );
4146
					}
4147
					// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
4148
					wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4149
					exit;
4150
				case 'activate_default_modules':
4151
					check_admin_referer( 'activate_default_modules' );
4152
					self::log( 'activate_default_modules' );
4153
					self::restate();
4154
					$min_version   = isset( $_GET['min_version'] ) ? $_GET['min_version'] : false;
4155
					$max_version   = isset( $_GET['max_version'] ) ? $_GET['max_version'] : false;
4156
					$other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? $_GET['other_modules'] : array();
4157
					self::activate_default_modules( $min_version, $max_version, $other_modules );
4158
					wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4159
					exit;
4160
				case 'disconnect':
4161
					if ( ! current_user_can( 'jetpack_disconnect' ) ) {
4162
						$error = 'cheatin';
4163
						break;
4164
					}
4165
4166
					check_admin_referer( 'jetpack-disconnect' );
4167
					self::log( 'disconnect' );
4168
					self::disconnect();
4169
					wp_safe_redirect( self::admin_url( 'disconnected=true' ) );
4170
					exit;
4171
				case 'reconnect':
4172
					if ( ! current_user_can( 'jetpack_reconnect' ) ) {
4173
						$error = 'cheatin';
4174
						break;
4175
					}
4176
4177
					check_admin_referer( 'jetpack-reconnect' );
4178
					self::log( 'reconnect' );
4179
					$this->disconnect();
4180
					wp_redirect( $this->build_connect_url( true, false, 'reconnect' ) );
4181
					exit;
4182 View Code Duplication
				case 'deactivate':
4183
					if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
4184
						$error = 'cheatin';
4185
						break;
4186
					}
4187
4188
					$modules = stripslashes( $_GET['module'] );
4189
					check_admin_referer( "jetpack_deactivate-$modules" );
4190
					foreach ( explode( ',', $modules ) as $module ) {
4191
						self::log( 'deactivate', $module );
4192
						self::deactivate_module( $module );
4193
						self::state( 'message', 'module_deactivated' );
4194
					}
4195
					self::state( 'module', $modules );
4196
					wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4197
					exit;
4198
				case 'unlink':
4199
					$redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : '';
4200
					check_admin_referer( 'jetpack-unlink' );
4201
					self::log( 'unlink' );
4202
					Connection_Manager::disconnect_user();
4203
					self::state( 'message', 'unlinked' );
4204
					if ( 'sub-unlink' == $redirect ) {
4205
						wp_safe_redirect( admin_url() );
4206
					} else {
4207
						wp_safe_redirect( self::admin_url( array( 'page' => $redirect ) ) );
4208
					}
4209
					exit;
4210
				case 'onboard':
4211
					if ( ! current_user_can( 'manage_options' ) ) {
4212
						wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4213
					} else {
4214
						self::create_onboarding_token();
4215
						$url = $this->build_connect_url( true );
4216
4217
						if ( false !== ( $token = Jetpack_Options::get_option( 'onboarding' ) ) ) {
4218
							$url = add_query_arg( 'onboarding', $token, $url );
4219
						}
4220
4221
						$calypso_env = $this->get_calypso_env();
4222
						if ( ! empty( $calypso_env ) ) {
4223
							$url = add_query_arg( 'calypso_env', $calypso_env, $url );
4224
						}
4225
4226
						wp_redirect( $url );
4227
						exit;
4228
					}
4229
					exit;
4230
				default:
4231
					/**
4232
					 * Fires when a Jetpack admin page is loaded with an unrecognized parameter.
4233
					 *
4234
					 * @since 2.6.0
4235
					 *
4236
					 * @param string sanitize_key( $_GET['action'] ) Unrecognized URL parameter.
4237
					 */
4238
					do_action( 'jetpack_unrecognized_action', sanitize_key( $_GET['action'] ) );
4239
			}
4240
		}
4241
4242
		if ( ! $error = $error ? $error : self::state( 'error' ) ) {
4243
			self::activate_new_modules( true );
4244
		}
4245
4246
		$message_code = self::state( 'message' );
4247
		if ( self::state( 'optin-manage' ) ) {
4248
			$activated_manage = $message_code;
4249
			$message_code     = 'jetpack-manage';
4250
		}
4251
4252
		switch ( $message_code ) {
4253
			case 'jetpack-manage':
4254
				$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' ), 'https://wordpress.com/sites' ) . '</strong>';
4255
				if ( $activated_manage ) {
4256
					$this->message .= '<br /><strong>' . __( 'Manage has been activated for you!', 'jetpack' ) . '</strong>';
4257
				}
4258
				break;
4259
4260
		}
4261
4262
		$deactivated_plugins = self::state( 'deactivated_plugins' );
4263
4264
		if ( ! empty( $deactivated_plugins ) ) {
4265
			$deactivated_plugins = explode( ',', $deactivated_plugins );
4266
			$deactivated_titles  = array();
4267
			foreach ( $deactivated_plugins as $deactivated_plugin ) {
4268
				if ( ! isset( $this->plugins_to_deactivate[ $deactivated_plugin ] ) ) {
4269
					continue;
4270
				}
4271
4272
				$deactivated_titles[] = '<strong>' . str_replace( ' ', '&nbsp;', $this->plugins_to_deactivate[ $deactivated_plugin ][1] ) . '</strong>';
4273
			}
4274
4275
			if ( $deactivated_titles ) {
4276
				if ( $this->message ) {
4277
					$this->message .= "<br /><br />\n";
4278
				}
4279
4280
				$this->message .= wp_sprintf(
4281
					_n(
4282
						'Jetpack contains the most recent version of the old %l plugin.',
4283
						'Jetpack contains the most recent versions of the old %l plugins.',
4284
						count( $deactivated_titles ),
4285
						'jetpack'
4286
					),
4287
					$deactivated_titles
4288
				);
4289
4290
				$this->message .= "<br />\n";
4291
4292
				$this->message .= _n(
4293
					'The old version has been deactivated and can be removed from your site.',
4294
					'The old versions have been deactivated and can be removed from your site.',
4295
					count( $deactivated_titles ),
4296
					'jetpack'
4297
				);
4298
			}
4299
		}
4300
4301
		$this->privacy_checks = self::state( 'privacy_checks' );
4302
4303
		if ( $this->message || $this->error || $this->privacy_checks ) {
4304
			add_action( 'jetpack_notices', array( $this, 'admin_notices' ) );
4305
		}
4306
4307
		add_filter( 'jetpack_short_module_description', 'wptexturize' );
4308
	}
4309
4310
	function admin_notices() {
4311
4312
		if ( $this->error ) {
4313
			?>
4314
<div id="message" class="jetpack-message jetpack-err">
4315
	<div class="squeezer">
4316
		<h2>
4317
			<?php
4318
			echo wp_kses(
4319
				$this->error,
4320
				array(
4321
					'a'      => array( 'href' => array() ),
4322
					'small'  => true,
4323
					'code'   => true,
4324
					'strong' => true,
4325
					'br'     => true,
4326
					'b'      => true,
4327
				)
4328
			);
4329
			?>
4330
			</h2>
4331
			<?php	if ( $desc = self::state( 'error_description' ) ) : ?>
4332
		<p><?php echo esc_html( stripslashes( $desc ) ); ?></p>
4333
<?php	endif; ?>
4334
	</div>
4335
</div>
4336
			<?php
4337
		}
4338
4339
		if ( $this->message ) {
4340
			?>
4341
<div id="message" class="jetpack-message">
4342
	<div class="squeezer">
4343
		<h2>
4344
			<?php
4345
			echo wp_kses(
4346
				$this->message,
4347
				array(
4348
					'strong' => array(),
4349
					'a'      => array( 'href' => true ),
4350
					'br'     => true,
4351
				)
4352
			);
4353
			?>
4354
			</h2>
4355
	</div>
4356
</div>
4357
			<?php
4358
		}
4359
4360
		if ( $this->privacy_checks ) :
4361
			$module_names = $module_slugs = array();
4362
4363
			$privacy_checks = explode( ',', $this->privacy_checks );
4364
			$privacy_checks = array_filter( $privacy_checks, array( 'Jetpack', 'is_module' ) );
4365
			foreach ( $privacy_checks as $module_slug ) {
4366
				$module = self::get_module( $module_slug );
4367
				if ( ! $module ) {
4368
					continue;
4369
				}
4370
4371
				$module_slugs[] = $module_slug;
4372
				$module_names[] = "<strong>{$module['name']}</strong>";
4373
			}
4374
4375
			$module_slugs = join( ',', $module_slugs );
4376
			?>
4377
<div id="message" class="jetpack-message jetpack-err">
4378
	<div class="squeezer">
4379
		<h2><strong><?php esc_html_e( 'Is this site private?', 'jetpack' ); ?></strong></h2><br />
4380
		<p>
4381
			<?php
4382
			echo wp_kses(
4383
				wptexturize(
4384
					wp_sprintf(
4385
						_nx(
4386
							"Like your site's RSS feeds, %l allows access to your posts and other content to third parties.",
4387
							"Like your site's RSS feeds, %l allow access to your posts and other content to third parties.",
4388
							count( $privacy_checks ),
4389
							'%l = list of Jetpack module/feature names',
4390
							'jetpack'
4391
						),
4392
						$module_names
4393
					)
4394
				),
4395
				array( 'strong' => true )
4396
			);
4397
4398
			echo "\n<br />\n";
4399
4400
			echo wp_kses(
4401
				sprintf(
4402
					_nx(
4403
						'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating this feature</a>.',
4404
						'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating these features</a>.',
4405
						count( $privacy_checks ),
4406
						'%1$s = deactivation URL, %2$s = "Deactivate {list of Jetpack module/feature names}',
4407
						'jetpack'
4408
					),
4409
					wp_nonce_url(
4410
						self::admin_url(
4411
							array(
4412
								'page'   => 'jetpack',
4413
								'action' => 'deactivate',
4414
								'module' => urlencode( $module_slugs ),
4415
							)
4416
						),
4417
						"jetpack_deactivate-$module_slugs"
4418
					),
4419
					esc_attr( wp_kses( wp_sprintf( _x( 'Deactivate %l', '%l = list of Jetpack module/feature names', 'jetpack' ), $module_names ), array() ) )
4420
				),
4421
				array(
4422
					'a' => array(
4423
						'href'  => true,
4424
						'title' => true,
4425
					),
4426
				)
4427
			);
4428
			?>
4429
		</p>
4430
	</div>
4431
</div>
4432
			<?php
4433
endif;
4434
	}
4435
4436
	/**
4437
	 * We can't always respond to a signed XML-RPC request with a
4438
	 * helpful error message. In some circumstances, doing so could
4439
	 * leak information.
4440
	 *
4441
	 * Instead, track that the error occurred via a Jetpack_Option,
4442
	 * and send that data back in the heartbeat.
4443
	 * All this does is increment a number, but it's enough to find
4444
	 * trends.
4445
	 *
4446
	 * @param WP_Error $xmlrpc_error The error produced during
4447
	 *                               signature validation.
4448
	 */
4449
	function track_xmlrpc_error( $xmlrpc_error ) {
4450
		$code = is_wp_error( $xmlrpc_error )
4451
			? $xmlrpc_error->get_error_code()
4452
			: 'should-not-happen';
4453
4454
		$xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
4455
		if ( isset( $xmlrpc_errors[ $code ] ) && $xmlrpc_errors[ $code ] ) {
4456
			// No need to update the option if we already have
4457
			// this code stored.
4458
			return;
4459
		}
4460
		$xmlrpc_errors[ $code ] = true;
4461
4462
		Jetpack_Options::update_option( 'xmlrpc_errors', $xmlrpc_errors, false );
4463
	}
4464
4465
	/**
4466
	 * Record a stat for later output.  This will only currently output in the admin_footer.
4467
	 */
4468
	function stat( $group, $detail ) {
4469
		if ( ! isset( $this->stats[ $group ] ) ) {
4470
			$this->stats[ $group ] = array();
4471
		}
4472
		$this->stats[ $group ][] = $detail;
4473
	}
4474
4475
	/**
4476
	 * Load stats pixels. $group is auto-prefixed with "x_jetpack-"
4477
	 */
4478
	function do_stats( $method = '' ) {
4479
		if ( is_array( $this->stats ) && count( $this->stats ) ) {
4480
			foreach ( $this->stats as $group => $stats ) {
4481
				if ( is_array( $stats ) && count( $stats ) ) {
4482
					$args = array( "x_jetpack-{$group}" => implode( ',', $stats ) );
4483
					if ( 'server_side' === $method ) {
4484
						self::do_server_side_stat( $args );
4485
					} else {
4486
						echo '<img src="' . esc_url( self::build_stats_url( $args ) ) . '" width="1" height="1" style="display:none;" />';
4487
					}
4488
				}
4489
				unset( $this->stats[ $group ] );
4490
			}
4491
		}
4492
	}
4493
4494
	/**
4495
	 * Runs stats code for a one-off, server-side.
4496
	 *
4497
	 * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store.
4498
	 *
4499
	 * @return bool If it worked.
4500
	 */
4501
	static function do_server_side_stat( $args ) {
4502
		$response = wp_remote_get( esc_url_raw( self::build_stats_url( $args ) ) );
4503
		if ( is_wp_error( $response ) ) {
4504
			return false;
4505
		}
4506
4507
		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
4508
			return false;
4509
		}
4510
4511
		return true;
4512
	}
4513
4514
	/**
4515
	 * Builds the stats url.
4516
	 *
4517
	 * @param $args array|string The arguments to append to the URL.
4518
	 *
4519
	 * @return string The URL to be pinged.
4520
	 */
4521
	static function build_stats_url( $args ) {
4522
		$defaults = array(
4523
			'v'    => 'wpcom2',
4524
			'rand' => md5( mt_rand( 0, 999 ) . time() ),
4525
		);
4526
		$args     = wp_parse_args( $args, $defaults );
4527
		/**
4528
		 * Filter the URL used as the Stats tracking pixel.
4529
		 *
4530
		 * @since 2.3.2
4531
		 *
4532
		 * @param string $url Base URL used as the Stats tracking pixel.
4533
		 */
4534
		$base_url = apply_filters(
4535
			'jetpack_stats_base_url',
4536
			'https://pixel.wp.com/g.gif'
4537
		);
4538
		$url      = add_query_arg( $args, $base_url );
4539
		return $url;
4540
	}
4541
4542
	/**
4543
	 * Get the role of the current user.
4544
	 *
4545
	 * @deprecated 7.6 Use Automattic\Jetpack\Roles::translate_current_user_to_role() instead.
4546
	 *
4547
	 * @access public
4548
	 * @static
4549
	 *
4550
	 * @return string|boolean Current user's role, false if not enough capabilities for any of the roles.
4551
	 */
4552
	public static function translate_current_user_to_role() {
4553
		_deprecated_function( __METHOD__, 'jetpack-7.6.0' );
4554
4555
		$roles = new Roles();
4556
		return $roles->translate_current_user_to_role();
4557
	}
4558
4559
	/**
4560
	 * Get the role of a particular user.
4561
	 *
4562
	 * @deprecated 7.6 Use Automattic\Jetpack\Roles::translate_user_to_role() instead.
4563
	 *
4564
	 * @access public
4565
	 * @static
4566
	 *
4567
	 * @param \WP_User $user User object.
4568
	 * @return string|boolean User's role, false if not enough capabilities for any of the roles.
4569
	 */
4570
	public static function translate_user_to_role( $user ) {
4571
		_deprecated_function( __METHOD__, 'jetpack-7.6.0' );
4572
4573
		$roles = new Roles();
4574
		return $roles->translate_user_to_role( $user );
4575
	}
4576
4577
	/**
4578
	 * Get the minimum capability for a role.
4579
	 *
4580
	 * @deprecated 7.6 Use Automattic\Jetpack\Roles::translate_role_to_cap() instead.
4581
	 *
4582
	 * @access public
4583
	 * @static
4584
	 *
4585
	 * @param string $role Role name.
4586
	 * @return string|boolean Capability, false if role isn't mapped to any capabilities.
4587
	 */
4588
	public static function translate_role_to_cap( $role ) {
4589
		_deprecated_function( __METHOD__, 'jetpack-7.6.0' );
4590
4591
		$roles = new Roles();
4592
		return $roles->translate_role_to_cap( $role );
4593
	}
4594
4595
	/**
4596
	 * Sign a user role with the master access token.
4597
	 * If not specified, will default to the current user.
4598
	 *
4599
	 * @deprecated since 7.7
4600
	 * @see Automattic\Jetpack\Connection\Manager::sign_role()
4601
	 *
4602
	 * @access public
4603
	 * @static
4604
	 *
4605
	 * @param string $role    User role.
4606
	 * @param int    $user_id ID of the user.
4607
	 * @return string Signed user role.
4608
	 */
4609
	public static function sign_role( $role, $user_id = null ) {
4610
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::sign_role' );
4611
		return self::connection()->sign_role( $role, $user_id );
4612
	}
4613
4614
	/**
4615
	 * Builds a URL to the Jetpack connection auth page
4616
	 *
4617
	 * @since 3.9.5
4618
	 *
4619
	 * @param bool        $raw If true, URL will not be escaped.
4620
	 * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection.
4621
	 *                              If string, will be a custom redirect.
4622
	 * @param bool|string $from If not false, adds 'from=$from' param to the connect URL.
4623
	 * @param bool        $register If true, will generate a register URL regardless of the existing token, since 4.9.0
4624
	 *
4625
	 * @return string Connect URL
4626
	 */
4627
	function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) {
4628
		$site_id    = Jetpack_Options::get_option( 'id' );
4629
		$blog_token = Jetpack_Data::get_access_token();
4630
4631
		if ( $register || ! $blog_token || ! $site_id ) {
4632
			$url = self::nonce_url_no_esc( self::admin_url( 'action=register' ), 'jetpack-register' );
4633
4634
			if ( ! empty( $redirect ) ) {
4635
				$url = add_query_arg(
4636
					'redirect',
4637
					urlencode( wp_validate_redirect( esc_url_raw( $redirect ) ) ),
4638
					$url
4639
				);
4640
			}
4641
4642
			if ( is_network_admin() ) {
4643
				$url = add_query_arg( 'is_multisite', network_admin_url( 'admin.php?page=jetpack-settings' ), $url );
4644
			}
4645
4646
			$calypso_env = self::get_calypso_env();
4647
4648
			if ( ! empty( $calypso_env ) ) {
4649
				$url = add_query_arg( 'calypso_env', $calypso_env, $url );
4650
			}
4651
		} else {
4652
4653
			// Let's check the existing blog token to see if we need to re-register. We only check once per minute
4654
			// because otherwise this logic can get us in to a loop.
4655
			$last_connect_url_check = intval( Jetpack_Options::get_raw_option( 'jetpack_last_connect_url_check' ) );
4656
			if ( ! $last_connect_url_check || ( time() - $last_connect_url_check ) > MINUTE_IN_SECONDS ) {
4657
				Jetpack_Options::update_raw_option( 'jetpack_last_connect_url_check', time() );
4658
4659
				$response = Client::wpcom_json_api_request_as_blog(
4660
					sprintf( '/sites/%d', $site_id ) . '?force=wpcom',
4661
					'1.1'
4662
				);
4663
4664
				if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
4665
4666
					// Generating a register URL instead to refresh the existing token
4667
					return $this->build_connect_url( $raw, $redirect, $from, true );
4668
				}
4669
			}
4670
4671
			$url = $this->build_authorize_url( $redirect );
4672
		}
4673
4674
		if ( $from ) {
4675
			$url = add_query_arg( 'from', $from, $url );
4676
		}
4677
4678
		// Ensure that class to get the affiliate code is loaded
4679
		if ( ! class_exists( 'Jetpack_Affiliate' ) ) {
4680
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-affiliate.php';
4681
		}
4682
		// Get affiliate code and add it to the URL
4683
		$url = Jetpack_Affiliate::init()->add_code_as_query_arg( $url );
4684
4685
		return $raw ? esc_url_raw( $url ) : esc_url( $url );
4686
	}
4687
4688
	public static function build_authorize_url( $redirect = false, $iframe = false ) {
4689
		if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && include_once JETPACK__GLOTPRESS_LOCALES_PATH ) {
4690
			$gp_locale = GP_Locales::by_field( 'wp_locale', get_locale() );
4691
		}
4692
4693
		$roles       = new Roles();
4694
		$role        = $roles->translate_current_user_to_role();
4695
		$signed_role = self::connection()->sign_role( $role );
4696
4697
		$user = wp_get_current_user();
4698
4699
		$jetpack_admin_page = esc_url_raw( admin_url( 'admin.php?page=jetpack' ) );
4700
		$redirect           = $redirect
4701
			? wp_validate_redirect( esc_url_raw( $redirect ), $jetpack_admin_page )
4702
			: $jetpack_admin_page;
4703
4704
		if ( isset( $_REQUEST['is_multisite'] ) ) {
4705
			$redirect = Jetpack_Network::init()->get_url( 'network_admin_page' );
4706
		}
4707
4708
		$secrets = self::generate_secrets( 'authorize', false, 2 * HOUR_IN_SECONDS );
4709
4710
		/**
4711
		 * Filter the type of authorization.
4712
		 * 'calypso' completes authorization on wordpress.com/jetpack/connect
4713
		 * while 'jetpack' ( or any other value ) completes the authorization at jetpack.wordpress.com.
4714
		 *
4715
		 * @since 4.3.3
4716
		 *
4717
		 * @param string $auth_type Defaults to 'calypso', can also be 'jetpack'.
4718
		 */
4719
		$auth_type = apply_filters( 'jetpack_auth_type', 'calypso' );
4720
4721
		$tracks          = new Tracking();
4722
		$tracks_identity = $tracks->tracks_get_identity( get_current_user_id() );
4723
4724
		$args = urlencode_deep(
4725
			array(
4726
				'response_type' => 'code',
4727
				'client_id'     => Jetpack_Options::get_option( 'id' ),
4728
				'redirect_uri'  => add_query_arg(
4729
					array(
4730
						'action'   => 'authorize',
4731
						'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
4732
						'redirect' => urlencode( $redirect ),
4733
					),
4734
					esc_url( admin_url( 'admin.php?page=jetpack' ) )
4735
				),
4736
				'state'         => $user->ID,
4737
				'scope'         => $signed_role,
4738
				'user_email'    => $user->user_email,
4739
				'user_login'    => $user->user_login,
4740
				'is_active'     => self::is_active(),
4741
				'jp_version'    => JETPACK__VERSION,
4742
				'auth_type'     => $auth_type,
4743
				'secret'        => $secrets['secret_1'],
4744
				'locale'        => ( isset( $gp_locale ) && isset( $gp_locale->slug ) ) ? $gp_locale->slug : '',
4745
				'blogname'      => get_option( 'blogname' ),
4746
				'site_url'      => site_url(),
4747
				'home_url'      => home_url(),
4748
				'site_icon'     => get_site_icon_url(),
4749
				'site_lang'     => get_locale(),
4750
				'_ui'           => $tracks_identity['_ui'],
4751
				'_ut'           => $tracks_identity['_ut'],
4752
				'site_created'  => self::connection()->get_assumed_site_creation_date(),
4753
			)
4754
		);
4755
4756
		self::apply_activation_source_to_args( $args );
4757
4758
		$connection = self::connection();
4759
4760
		$calypso_env = self::get_calypso_env();
4761
4762
		if ( ! empty( $calypso_env ) ) {
4763
			$args['calypso_env'] = $calypso_env;
4764
		}
4765
4766
		$api_url = $iframe ? $connection->api_url( 'authorize_iframe' ) : $connection->api_url( 'authorize' );
4767
4768
		return add_query_arg( $args, $api_url );
4769
	}
4770
4771
	/**
4772
	 * Get our assumed site creation date.
4773
	 * Calculated based on the earlier date of either:
4774
	 * - Earliest admin user registration date.
4775
	 * - Earliest date of post of any post type.
4776
	 *
4777
	 * @since 7.2.0
4778
	 * @deprecated since 7.8.0
4779
	 *
4780
	 * @return string Assumed site creation date and time.
4781
	 */
4782
	public static function get_assumed_site_creation_date() {
4783
		_deprecated_function( __METHOD__, 'jetpack-7.8', 'Automattic\\Jetpack\\Connection\\Manager' );
4784
		return self::connection()->get_assumed_site_creation_date();
4785
	}
4786
4787 View Code Duplication
	public static function apply_activation_source_to_args( &$args ) {
4788
		list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' );
4789
4790
		if ( $activation_source_name ) {
4791
			$args['_as'] = urlencode( $activation_source_name );
4792
		}
4793
4794
		if ( $activation_source_keyword ) {
4795
			$args['_ak'] = urlencode( $activation_source_keyword );
4796
		}
4797
	}
4798
4799
	function build_reconnect_url( $raw = false ) {
4800
		$url = wp_nonce_url( self::admin_url( 'action=reconnect' ), 'jetpack-reconnect' );
4801
		return $raw ? $url : esc_url( $url );
4802
	}
4803
4804
	public static function admin_url( $args = null ) {
4805
		$args = wp_parse_args( $args, array( 'page' => 'jetpack' ) );
4806
		$url  = add_query_arg( $args, admin_url( 'admin.php' ) );
4807
		return $url;
4808
	}
4809
4810
	public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) {
4811
		$actionurl = str_replace( '&amp;', '&', $actionurl );
4812
		return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
4813
	}
4814
4815
	function dismiss_jetpack_notice() {
4816
4817
		if ( ! isset( $_GET['jetpack-notice'] ) ) {
4818
			return;
4819
		}
4820
4821
		switch ( $_GET['jetpack-notice'] ) {
4822
			case 'dismiss':
4823
				if ( check_admin_referer( 'jetpack-deactivate' ) && ! is_plugin_active_for_network( plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ) ) ) {
4824
4825
					require_once ABSPATH . 'wp-admin/includes/plugin.php';
4826
					deactivate_plugins( JETPACK__PLUGIN_DIR . 'jetpack.php', false, false );
4827
					wp_safe_redirect( admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s=' );
4828
				}
4829
				break;
4830
		}
4831
	}
4832
4833
	public static function sort_modules( $a, $b ) {
4834
		if ( $a['sort'] == $b['sort'] ) {
4835
			return 0;
4836
		}
4837
4838
		return ( $a['sort'] < $b['sort'] ) ? -1 : 1;
4839
	}
4840
4841
	function ajax_recheck_ssl() {
4842
		check_ajax_referer( 'recheck-ssl', 'ajax-nonce' );
4843
		$result = self::permit_ssl( true );
4844
		wp_send_json(
4845
			array(
4846
				'enabled' => $result,
4847
				'message' => get_transient( 'jetpack_https_test_message' ),
4848
			)
4849
		);
4850
	}
4851
4852
	/* Client API */
4853
4854
	/**
4855
	 * Returns the requested Jetpack API URL
4856
	 *
4857
	 * @deprecated since 7.7
4858
	 * @return string
4859
	 */
4860
	public static function api_url( $relative_url ) {
4861
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::api_url' );
4862
		$connection = self::connection();
4863
		return $connection->api_url( $relative_url );
4864
	}
4865
4866
	/**
4867
	 * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets
4868
	 */
4869
	public static function fix_url_for_bad_hosts( $url ) {
4870
		if ( 0 !== strpos( $url, 'https://' ) ) {
4871
			return $url;
4872
		}
4873
4874
		switch ( JETPACK_CLIENT__HTTPS ) {
4875
			case 'ALWAYS':
4876
				return $url;
4877
			case 'NEVER':
4878
				return set_url_scheme( $url, 'http' );
4879
			// default : case 'AUTO' :
4880
		}
4881
4882
		// we now return the unmodified SSL URL by default, as a security precaution
4883
		return $url;
4884
	}
4885
4886
	public static function verify_onboarding_token( $token_data, $token, $request_data ) {
4887
		// Default to a blog token.
4888
		$token_type = 'blog';
4889
4890
		// Let's see if this is onboarding. In such case, use user token type and the provided user id.
4891
		if ( isset( $request_data ) || ! empty( $_GET['onboarding'] ) ) {
4892
			if ( ! empty( $_GET['onboarding'] ) ) {
4893
				$jpo = $_GET;
4894
			} else {
4895
				$jpo = json_decode( $request_data, true );
4896
			}
4897
4898
			$jpo_token = ! empty( $jpo['onboarding']['token'] ) ? $jpo['onboarding']['token'] : null;
4899
			$jpo_user  = ! empty( $jpo['onboarding']['jpUser'] ) ? $jpo['onboarding']['jpUser'] : null;
4900
4901
			if (
4902
				isset( $jpo_user )
4903
				&& isset( $jpo_token )
4904
				&& is_email( $jpo_user )
4905
				&& ctype_alnum( $jpo_token )
4906
				&& isset( $_GET['rest_route'] )
4907
				&& self::validate_onboarding_token_action(
4908
					$jpo_token,
4909
					$_GET['rest_route']
4910
				)
4911
			) {
4912
				$jp_user = get_user_by( 'email', $jpo_user );
4913
				if ( is_a( $jp_user, 'WP_User' ) ) {
4914
					wp_set_current_user( $jp_user->ID );
4915
					$user_can = is_multisite()
4916
						? current_user_can_for_blog( get_current_blog_id(), 'manage_options' )
4917
						: current_user_can( 'manage_options' );
4918
					if ( $user_can ) {
4919
						$token_type              = 'user';
4920
						$token->external_user_id = $jp_user->ID;
4921
					}
4922
				}
4923
			}
4924
4925
			$token_data['type']    = $token_type;
4926
			$token_data['user_id'] = $token->external_user_id;
4927
		}
4928
4929
		return $token_data;
4930
	}
4931
4932
	/**
4933
	 * Create a random secret for validating onboarding payload
4934
	 *
4935
	 * @return string Secret token
4936
	 */
4937
	public static function create_onboarding_token() {
4938
		if ( false === ( $token = Jetpack_Options::get_option( 'onboarding' ) ) ) {
4939
			$token = wp_generate_password( 32, false );
4940
			Jetpack_Options::update_option( 'onboarding', $token );
4941
		}
4942
4943
		return $token;
4944
	}
4945
4946
	/**
4947
	 * Remove the onboarding token
4948
	 *
4949
	 * @return bool True on success, false on failure
4950
	 */
4951
	public static function invalidate_onboarding_token() {
4952
		return Jetpack_Options::delete_option( 'onboarding' );
4953
	}
4954
4955
	/**
4956
	 * Validate an onboarding token for a specific action
4957
	 *
4958
	 * @return boolean True if token/action pair is accepted, false if not
4959
	 */
4960
	public static function validate_onboarding_token_action( $token, $action ) {
4961
		// Compare tokens, bail if tokens do not match
4962
		if ( ! hash_equals( $token, Jetpack_Options::get_option( 'onboarding' ) ) ) {
4963
			return false;
4964
		}
4965
4966
		// List of valid actions we can take
4967
		$valid_actions = array(
4968
			'/jetpack/v4/settings',
4969
		);
4970
4971
		// Whitelist the action
4972
		if ( ! in_array( $action, $valid_actions ) ) {
4973
			return false;
4974
		}
4975
4976
		return true;
4977
	}
4978
4979
	/**
4980
	 * Checks to see if the URL is using SSL to connect with Jetpack
4981
	 *
4982
	 * @since 2.3.3
4983
	 * @return boolean
4984
	 */
4985
	public static function permit_ssl( $force_recheck = false ) {
4986
		// Do some fancy tests to see if ssl is being supported
4987
		if ( $force_recheck || false === ( $ssl = get_transient( 'jetpack_https_test' ) ) ) {
4988
			$message = '';
4989
			if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) {
4990
				$ssl = 0;
4991
			} else {
4992
				switch ( JETPACK_CLIENT__HTTPS ) {
4993
					case 'NEVER':
4994
						$ssl     = 0;
4995
						$message = __( 'JETPACK_CLIENT__HTTPS is set to NEVER', 'jetpack' );
4996
						break;
4997
					case 'ALWAYS':
4998
					case 'AUTO':
4999
					default:
5000
						$ssl = 1;
5001
						break;
5002
				}
5003
5004
				// If it's not 'NEVER', test to see
5005
				if ( $ssl ) {
5006
					if ( ! wp_http_supports( array( 'ssl' => true ) ) ) {
5007
						$ssl     = 0;
5008
						$message = __( 'WordPress reports no SSL support', 'jetpack' );
5009
					} else {
5010
						$response = wp_remote_get( JETPACK__API_BASE . 'test/1/' );
5011
						if ( is_wp_error( $response ) ) {
5012
							$ssl     = 0;
5013
							$message = __( 'WordPress reports no SSL support', 'jetpack' );
5014
						} elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) {
5015
							$ssl     = 0;
5016
							$message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response );
5017
						}
5018
					}
5019
				}
5020
			}
5021
			set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS );
5022
			set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS );
5023
		}
5024
5025
		return (bool) $ssl;
5026
	}
5027
5028
	/*
5029
	 * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working.
5030
	 */
5031
	public function alert_auto_ssl_fail() {
5032
		if ( ! current_user_can( 'manage_options' ) ) {
5033
			return;
5034
		}
5035
5036
		$ajax_nonce = wp_create_nonce( 'recheck-ssl' );
5037
		?>
5038
5039
		<div id="jetpack-ssl-warning" class="error jp-identity-crisis">
5040
			<div class="jp-banner__content">
5041
				<h2><?php _e( 'Outbound HTTPS not working', 'jetpack' ); ?></h2>
5042
				<p><?php _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>
5043
				<p>
5044
					<?php _e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?>
5045
					<a href="#" id="jetpack-recheck-ssl-button"><?php _e( 'Try again', 'jetpack' ); ?></a>
5046
					<span id="jetpack-recheck-ssl-output"><?php echo get_transient( 'jetpack_https_test_message' ); ?></span>
5047
				</p>
5048
				<p>
5049
					<?php
5050
					printf(
5051
						__( 'For more help, try our <a href="%1$s">connection debugger</a> or <a href="%2$s" target="_blank">troubleshooting tips</a>.', 'jetpack' ),
5052
						esc_url( self::admin_url( array( 'page' => 'jetpack-debugger' ) ) ),
5053
						esc_url( 'https://jetpack.com/support/getting-started-with-jetpack/troubleshooting-tips/' )
5054
					);
5055
					?>
5056
				</p>
5057
			</div>
5058
		</div>
5059
		<style>
5060
			#jetpack-recheck-ssl-output { margin-left: 5px; color: red; }
5061
		</style>
5062
		<script type="text/javascript">
5063
			jQuery( document ).ready( function( $ ) {
5064
				$( '#jetpack-recheck-ssl-button' ).click( function( e ) {
5065
					var $this = $( this );
5066
					$this.html( <?php echo json_encode( __( 'Checking', 'jetpack' ) ); ?> );
5067
					$( '#jetpack-recheck-ssl-output' ).html( '' );
5068
					e.preventDefault();
5069
					var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': '<?php echo $ajax_nonce; ?>' };
5070
					$.post( ajaxurl, data )
5071
					  .done( function( response ) {
5072
						  if ( response.enabled ) {
5073
							  $( '#jetpack-ssl-warning' ).hide();
5074
						  } else {
5075
							  this.html( <?php echo json_encode( __( 'Try again', 'jetpack' ) ); ?> );
5076
							  $( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message );
5077
						  }
5078
					  }.bind( $this ) );
5079
				} );
5080
			} );
5081
		</script>
5082
5083
		<?php
5084
	}
5085
5086
	/**
5087
	 * Returns the Jetpack XML-RPC API
5088
	 *
5089
	 * @return string
5090
	 */
5091
	public static function xmlrpc_api_url() {
5092
		$base = preg_replace( '#(https?://[^?/]+)(/?.*)?$#', '\\1', JETPACK__API_BASE );
5093
		return untrailingslashit( $base ) . '/xmlrpc.php';
5094
	}
5095
5096
	public static function connection() {
5097
		return self::init()->connection_manager;
5098
	}
5099
5100
	/**
5101
	 * Creates two secret tokens and the end of life timestamp for them.
5102
	 *
5103
	 * Note these tokens are unique per call, NOT static per site for connecting.
5104
	 *
5105
	 * @since 2.6
5106
	 * @return array
5107
	 */
5108
	public static function generate_secrets( $action, $user_id = false, $exp = 600 ) {
5109
		if ( false === $user_id ) {
5110
			$user_id = get_current_user_id();
5111
		}
5112
5113
		return self::connection()->generate_secrets( $action, $user_id, $exp );
5114
	}
5115
5116
	public static function get_secrets( $action, $user_id ) {
5117
		$secrets = self::connection()->get_secrets( $action, $user_id );
5118
5119
		if ( Connection_Manager::SECRETS_MISSING === $secrets ) {
5120
			return new WP_Error( 'verify_secrets_missing', 'Verification secrets not found' );
5121
		}
5122
5123
		if ( Connection_Manager::SECRETS_EXPIRED === $secrets ) {
5124
			return new WP_Error( 'verify_secrets_expired', 'Verification took too long' );
5125
		}
5126
5127
		return $secrets;
5128
	}
5129
5130
	/**
5131
	 * @deprecated 7.5 Use Connection_Manager instead.
5132
	 *
5133
	 * @param $action
5134
	 * @param $user_id
5135
	 */
5136
	public static function delete_secrets( $action, $user_id ) {
5137
		return self::connection()->delete_secrets( $action, $user_id );
5138
	}
5139
5140
	/**
5141
	 * Builds the timeout limit for queries talking with the wpcom servers.
5142
	 *
5143
	 * Based on local php max_execution_time in php.ini
5144
	 *
5145
	 * @since 2.6
5146
	 * @return int
5147
	 * @deprecated
5148
	 **/
5149
	public function get_remote_query_timeout_limit() {
5150
		_deprecated_function( __METHOD__, 'jetpack-5.4' );
5151
		return self::get_max_execution_time();
5152
	}
5153
5154
	/**
5155
	 * Builds the timeout limit for queries talking with the wpcom servers.
5156
	 *
5157
	 * Based on local php max_execution_time in php.ini
5158
	 *
5159
	 * @since 5.4
5160
	 * @return int
5161
	 **/
5162
	public static function get_max_execution_time() {
5163
		$timeout = (int) ini_get( 'max_execution_time' );
5164
5165
		// Ensure exec time set in php.ini
5166
		if ( ! $timeout ) {
5167
			$timeout = 30;
5168
		}
5169
		return $timeout;
5170
	}
5171
5172
	/**
5173
	 * Sets a minimum request timeout, and returns the current timeout
5174
	 *
5175
	 * @since 5.4
5176
	 **/
5177 View Code Duplication
	public static function set_min_time_limit( $min_timeout ) {
5178
		$timeout = self::get_max_execution_time();
5179
		if ( $timeout < $min_timeout ) {
5180
			$timeout = $min_timeout;
5181
			set_time_limit( $timeout );
5182
		}
5183
		return $timeout;
5184
	}
5185
5186
	/**
5187
	 * Takes the response from the Jetpack register new site endpoint and
5188
	 * verifies it worked properly.
5189
	 *
5190
	 * @since 2.6
5191
	 * @deprecated since 7.7.0
5192
	 * @see Automattic\Jetpack\Connection\Manager::validate_remote_register_response()
5193
	 **/
5194
	public function validate_remote_register_response() {
5195
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::validate_remote_register_response' );
5196
	}
5197
5198
	/**
5199
	 * @return bool|WP_Error
5200
	 */
5201
	public static function register() {
5202
		$tracking = new Tracking();
5203
		$tracking->record_user_event( 'jpc_register_begin' );
5204
5205
		add_filter( 'jetpack_register_request_body', array( __CLASS__, 'filter_register_request_body' ) );
5206
5207
		$connection   = self::connection();
5208
		$registration = $connection->register();
5209
5210
		remove_filter( 'jetpack_register_request_body', array( __CLASS__, 'filter_register_request_body' ) );
5211
5212
		if ( ! $registration || is_wp_error( $registration ) ) {
5213
			return $registration;
5214
		}
5215
5216
		return true;
5217
	}
5218
5219
	/**
5220
	 * Filters the registration request body to include tracking properties.
5221
	 *
5222
	 * @param Array $properties
5223
	 * @return Array amended properties.
5224
	 */
5225
	public static function filter_register_request_body( $properties ) {
5226
		$tracking        = new Tracking();
5227
		$tracks_identity = $tracking->tracks_get_identity( get_current_user_id() );
5228
5229
		return array_merge(
5230
			$properties,
5231
			array(
5232
				'_ui' => $tracks_identity['_ui'],
5233
				'_ut' => $tracks_identity['_ut'],
5234
			)
5235
		);
5236
	}
5237
5238
	/**
5239
	 * If the db version is showing something other that what we've got now, bump it to current.
5240
	 *
5241
	 * @return bool: True if the option was incorrect and updated, false if nothing happened.
5242
	 */
5243
	public static function maybe_set_version_option() {
5244
		list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
5245
		if ( JETPACK__VERSION != $version ) {
5246
			Jetpack_Options::update_option( 'version', JETPACK__VERSION . ':' . time() );
5247
5248
			if ( version_compare( JETPACK__VERSION, $version, '>' ) ) {
5249
				/** This action is documented in class.jetpack.php */
5250
				do_action( 'updating_jetpack_version', JETPACK__VERSION, $version );
5251
			}
5252
5253
			return true;
5254
		}
5255
		return false;
5256
	}
5257
5258
	/* Client Server API */
5259
5260
	/**
5261
	 * Loads the Jetpack XML-RPC client.
5262
	 * No longer necessary, as the XML-RPC client will be automagically loaded.
5263
	 *
5264
	 * @deprecated since 7.7.0
5265
	 */
5266
	public static function load_xml_rpc_client() {
5267
		// Removed the php notice that shows up in order to give time to Akismet and VaultPress time to update.
5268
		// _deprecated_function( __METHOD__, 'jetpack-7.7' );
5269
	}
5270
5271
	/**
5272
	 * Resets the saved authentication state in between testing requests.
5273
	 */
5274
	public function reset_saved_auth_state() {
5275
		$this->rest_authentication_status = null;
5276
		$this->connection_manager->reset_saved_auth_state();
5277
	}
5278
5279
	/**
5280
	 * Verifies the signature of the current request.
5281
	 *
5282
	 * @deprecated since 7.7.0
5283
	 * @see Automattic\Jetpack\Connection\Manager::verify_xml_rpc_signature()
5284
	 *
5285
	 * @return false|array
5286
	 */
5287
	public function verify_xml_rpc_signature() {
5288
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::verify_xml_rpc_signature' );
5289
		return self::connection()->verify_xml_rpc_signature();
5290
	}
5291
5292
	/**
5293
	 * Verifies the signature of the current request.
5294
	 *
5295
	 * This function has side effects and should not be used. Instead,
5296
	 * use the memoized version `->verify_xml_rpc_signature()`.
5297
	 *
5298
	 * @deprecated since 7.7.0
5299
	 * @see Automattic\Jetpack\Connection\Manager::internal_verify_xml_rpc_signature()
5300
	 * @internal
5301
	 */
5302
	private function internal_verify_xml_rpc_signature() {
5303
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::internal_verify_xml_rpc_signature' );
5304
	}
5305
5306
	/**
5307
	 * Authenticates XML-RPC and other requests from the Jetpack Server.
5308
	 *
5309
	 * @deprecated since 7.7.0
5310
	 * @see Automattic\Jetpack\Connection\Manager::authenticate_jetpack()
5311
	 *
5312
	 * @param \WP_User|mixed $user     User object if authenticated.
5313
	 * @param string         $username Username.
5314
	 * @param string         $password Password string.
5315
	 * @return \WP_User|mixed Authenticated user or error.
5316
	 */
5317
	public function authenticate_jetpack( $user, $username, $password ) {
5318
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::authenticate_jetpack' );
5319
		return $this->connection_manager->authenticate_jetpack( $user, $username, $password );
5320
	}
5321
5322
	// Authenticates requests from Jetpack server to WP REST API endpoints.
5323
	// Uses the existing XMLRPC request signing implementation.
5324
	function wp_rest_authenticate( $user ) {
5325
		if ( ! empty( $user ) ) {
5326
			// Another authentication method is in effect.
5327
			return $user;
5328
		}
5329
5330
		if ( ! isset( $_GET['_for'] ) || $_GET['_for'] !== 'jetpack' ) {
5331
			// Nothing to do for this authentication method.
5332
			return null;
5333
		}
5334
5335
		if ( ! isset( $_GET['token'] ) && ! isset( $_GET['signature'] ) ) {
5336
			// Nothing to do for this authentication method.
5337
			return null;
5338
		}
5339
5340
		// Ensure that we always have the request body available.  At this
5341
		// point, the WP REST API code to determine the request body has not
5342
		// run yet.  That code may try to read from 'php://input' later, but
5343
		// this can only be done once per request in PHP versions prior to 5.6.
5344
		// So we will go ahead and perform this read now if needed, and save
5345
		// the request body where both the Jetpack signature verification code
5346
		// and the WP REST API code can see it.
5347
		if ( ! isset( $GLOBALS['HTTP_RAW_POST_DATA'] ) ) {
5348
			$GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents( 'php://input' );
5349
		}
5350
		$this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA'];
5351
5352
		// Only support specific request parameters that have been tested and
5353
		// are known to work with signature verification.  A different method
5354
		// can be passed to the WP REST API via the '?_method=' parameter if
5355
		// needed.
5356
		if ( $_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
5357
			$this->rest_authentication_status = new WP_Error(
5358
				'rest_invalid_request',
5359
				__( 'This request method is not supported.', 'jetpack' ),
5360
				array( 'status' => 400 )
5361
			);
5362
			return null;
5363
		}
5364
		if ( $_SERVER['REQUEST_METHOD'] !== 'POST' && ! empty( $this->HTTP_RAW_POST_DATA ) ) {
5365
			$this->rest_authentication_status = new WP_Error(
5366
				'rest_invalid_request',
5367
				__( 'This request method does not support body parameters.', 'jetpack' ),
5368
				array( 'status' => 400 )
5369
			);
5370
			return null;
5371
		}
5372
5373
		$verified = $this->connection_manager->verify_xml_rpc_signature();
5374
5375
		if (
5376
			$verified &&
5377
			isset( $verified['type'] ) &&
5378
			'user' === $verified['type'] &&
5379
			! empty( $verified['user_id'] )
5380
		) {
5381
			// Authentication successful.
5382
			$this->rest_authentication_status = true;
5383
			return $verified['user_id'];
5384
		}
5385
5386
		// Something else went wrong.  Probably a signature error.
5387
		$this->rest_authentication_status = new WP_Error(
5388
			'rest_invalid_signature',
5389
			__( 'The request is not signed correctly.', 'jetpack' ),
5390
			array( 'status' => 400 )
5391
		);
5392
		return null;
5393
	}
5394
5395
	/**
5396
	 * Report authentication status to the WP REST API.
5397
	 *
5398
	 * @param  WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not
5399
	 * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication}
5400
	 */
5401
	public function wp_rest_authentication_errors( $value ) {
5402
		if ( $value !== null ) {
5403
			return $value;
5404
		}
5405
		return $this->rest_authentication_status;
5406
	}
5407
5408
	/**
5409
	 * Add our nonce to this request.
5410
	 *
5411
	 * @deprecated since 7.7.0
5412
	 * @see Automattic\Jetpack\Connection\Manager::add_nonce()
5413
	 *
5414
	 * @param int    $timestamp Timestamp of the request.
5415
	 * @param string $nonce     Nonce string.
5416
	 */
5417
	public function add_nonce( $timestamp, $nonce ) {
5418
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::add_nonce' );
5419
		return $this->connection_manager->add_nonce( $timestamp, $nonce );
5420
	}
5421
5422
	/**
5423
	 * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods.
5424
	 * Capture it here so we can verify the signature later.
5425
	 *
5426
	 * @deprecated since 7.7.0
5427
	 * @see Automattic\Jetpack\Connection\Manager::xmlrpc_methods()
5428
	 *
5429
	 * @param array $methods XMLRPC methods.
5430
	 * @return array XMLRPC methods, with the $HTTP_RAW_POST_DATA one.
5431
	 */
5432
	public function xmlrpc_methods( $methods ) {
5433
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::xmlrpc_methods' );
5434
		return $this->connection_manager->xmlrpc_methods( $methods );
5435
	}
5436
5437
	/**
5438
	 * Register additional public XMLRPC methods.
5439
	 *
5440
	 * @deprecated since 7.7.0
5441
	 * @see Automattic\Jetpack\Connection\Manager::public_xmlrpc_methods()
5442
	 *
5443
	 * @param array $methods Public XMLRPC methods.
5444
	 * @return array Public XMLRPC methods, with the getOptions one.
5445
	 */
5446
	public function public_xmlrpc_methods( $methods ) {
5447
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::public_xmlrpc_methods' );
5448
		return $this->connection_manager->public_xmlrpc_methods( $methods );
5449
	}
5450
5451
	/**
5452
	 * Handles a getOptions XMLRPC method call.
5453
	 *
5454
	 * @deprecated since 7.7.0
5455
	 * @see Automattic\Jetpack\Connection\Manager::jetpack_getOptions()
5456
	 *
5457
	 * @param array $args method call arguments.
5458
	 * @return array an amended XMLRPC server options array.
5459
	 */
5460
	public function jetpack_getOptions( $args ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
5461
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::jetpack_getOptions' );
5462
		return $this->connection_manager->jetpack_getOptions( $args );
0 ignored issues
show
The method jetpack_getOptions() does not exist on Automattic\Jetpack\Connection\Manager. Did you maybe mean jetpack_get_options()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
5463
	}
5464
5465
	/**
5466
	 * Adds Jetpack-specific options to the output of the XMLRPC options method.
5467
	 *
5468
	 * @deprecated since 7.7.0
5469
	 * @see Automattic\Jetpack\Connection\Manager::xmlrpc_options()
5470
	 *
5471
	 * @param array $options Standard Core options.
5472
	 * @return array Amended options.
5473
	 */
5474
	public function xmlrpc_options( $options ) {
5475
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::xmlrpc_options' );
5476
		return $this->connection_manager->xmlrpc_options( $options );
5477
	}
5478
5479
	/**
5480
	 * Cleans nonces that were saved when calling ::add_nonce.
5481
	 *
5482
	 * @deprecated since 7.7.0
5483
	 * @see Automattic\Jetpack\Connection\Manager::clean_nonces()
5484
	 *
5485
	 * @param bool $all whether to clean even non-expired nonces.
5486
	 */
5487
	public static function clean_nonces( $all = false ) {
5488
		_deprecated_function( __METHOD__, 'jetpack-7.7', 'Automattic\\Jetpack\\Connection\\Manager::clean_nonces' );
5489
		return self::connection()->clean_nonces( $all );
5490
	}
5491
5492
	/**
5493
	 * State is passed via cookies from one request to the next, but never to subsequent requests.
5494
	 * SET: state( $key, $value );
5495
	 * GET: $value = state( $key );
5496
	 *
5497
	 * @param string $key
5498
	 * @param string $value
5499
	 * @param bool   $restate private
5500
	 */
5501
	public static function state( $key = null, $value = null, $restate = false ) {
5502
		static $state = array();
5503
		static $path, $domain;
5504
		if ( ! isset( $path ) ) {
5505
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
5506
			$admin_url = self::admin_url();
5507
			$bits      = wp_parse_url( $admin_url );
5508
5509
			if ( is_array( $bits ) ) {
5510
				$path   = ( isset( $bits['path'] ) ) ? dirname( $bits['path'] ) : null;
5511
				$domain = ( isset( $bits['host'] ) ) ? $bits['host'] : null;
5512
			} else {
5513
				$path = $domain = null;
5514
			}
5515
		}
5516
5517
		// Extract state from cookies and delete cookies
5518
		if ( isset( $_COOKIE['jetpackState'] ) && is_array( $_COOKIE['jetpackState'] ) ) {
5519
			$yum = $_COOKIE['jetpackState'];
5520
			unset( $_COOKIE['jetpackState'] );
5521
			foreach ( $yum as $k => $v ) {
5522
				if ( strlen( $v ) ) {
5523
					$state[ $k ] = $v;
5524
				}
5525
				setcookie( "jetpackState[$k]", false, 0, $path, $domain );
5526
			}
5527
		}
5528
5529
		if ( $restate ) {
5530
			foreach ( $state as $k => $v ) {
5531
				setcookie( "jetpackState[$k]", $v, 0, $path, $domain );
5532
			}
5533
			return;
5534
		}
5535
5536
		// Get a state variable
5537
		if ( isset( $key ) && ! isset( $value ) ) {
5538
			if ( array_key_exists( $key, $state ) ) {
5539
				return $state[ $key ];
5540
			}
5541
			return null;
5542
		}
5543
5544
		// Set a state variable
5545
		if ( isset( $key ) && isset( $value ) ) {
5546
			if ( is_array( $value ) && isset( $value[0] ) ) {
5547
				$value = $value[0];
5548
			}
5549
			$state[ $key ] = $value;
5550
			setcookie( "jetpackState[$key]", $value, 0, $path, $domain );
5551
		}
5552
	}
5553
5554
	public static function restate() {
5555
		self::state( null, null, true );
5556
	}
5557
5558
	public static function check_privacy( $file ) {
5559
		static $is_site_publicly_accessible = null;
5560
5561
		if ( is_null( $is_site_publicly_accessible ) ) {
5562
			$is_site_publicly_accessible = false;
5563
5564
			$rpc = new Jetpack_IXR_Client();
5565
5566
			$success = $rpc->query( 'jetpack.isSitePubliclyAccessible', home_url() );
5567
			if ( $success ) {
5568
				$response = $rpc->getResponse();
5569
				if ( $response ) {
5570
					$is_site_publicly_accessible = true;
5571
				}
5572
			}
5573
5574
			Jetpack_Options::update_option( 'public', (int) $is_site_publicly_accessible );
5575
		}
5576
5577
		if ( $is_site_publicly_accessible ) {
5578
			return;
5579
		}
5580
5581
		$module_slug = self::get_module_slug( $file );
5582
5583
		$privacy_checks = self::state( 'privacy_checks' );
5584
		if ( ! $privacy_checks ) {
5585
			$privacy_checks = $module_slug;
5586
		} else {
5587
			$privacy_checks .= ",$module_slug";
5588
		}
5589
5590
		self::state( 'privacy_checks', $privacy_checks );
5591
	}
5592
5593
	/**
5594
	 * Helper method for multicall XMLRPC.
5595
	 */
5596
	public static function xmlrpc_async_call() {
5597
		global $blog_id;
5598
		static $clients = array();
5599
5600
		$client_blog_id = is_multisite() ? $blog_id : 0;
5601
5602
		if ( ! isset( $clients[ $client_blog_id ] ) ) {
5603
			$clients[ $client_blog_id ] = new Jetpack_IXR_ClientMulticall( array( 'user_id' => JETPACK_MASTER_USER ) );
5604
			if ( function_exists( 'ignore_user_abort' ) ) {
5605
				ignore_user_abort( true );
5606
			}
5607
			add_action( 'shutdown', array( 'Jetpack', 'xmlrpc_async_call' ) );
5608
		}
5609
5610
		$args = func_get_args();
5611
5612
		if ( ! empty( $args[0] ) ) {
5613
			call_user_func_array( array( $clients[ $client_blog_id ], 'addCall' ), $args );
5614
		} elseif ( is_multisite() ) {
5615
			foreach ( $clients as $client_blog_id => $client ) {
5616
				if ( ! $client_blog_id || empty( $client->calls ) ) {
5617
					continue;
5618
				}
5619
5620
				$switch_success = switch_to_blog( $client_blog_id, true );
5621
				if ( ! $switch_success ) {
5622
					continue;
5623
				}
5624
5625
				flush();
5626
				$client->query();
5627
5628
				restore_current_blog();
5629
			}
5630
		} else {
5631
			if ( isset( $clients[0] ) && ! empty( $clients[0]->calls ) ) {
5632
				flush();
5633
				$clients[0]->query();
5634
			}
5635
		}
5636
	}
5637
5638
	public static function staticize_subdomain( $url ) {
5639
5640
		// Extract hostname from URL
5641
		$host = parse_url( $url, PHP_URL_HOST );
5642
5643
		// Explode hostname on '.'
5644
		$exploded_host = explode( '.', $host );
5645
5646
		// Retrieve the name and TLD
5647
		if ( count( $exploded_host ) > 1 ) {
5648
			$name = $exploded_host[ count( $exploded_host ) - 2 ];
5649
			$tld  = $exploded_host[ count( $exploded_host ) - 1 ];
5650
			// Rebuild domain excluding subdomains
5651
			$domain = $name . '.' . $tld;
5652
		} else {
5653
			$domain = $host;
5654
		}
5655
		// Array of Automattic domains
5656
		$domain_whitelist = array( 'wordpress.com', 'wp.com' );
5657
5658
		// Return $url if not an Automattic domain
5659
		if ( ! in_array( $domain, $domain_whitelist ) ) {
5660
			return $url;
5661
		}
5662
5663
		if ( is_ssl() ) {
5664
			return preg_replace( '|https?://[^/]++/|', 'https://s-ssl.wordpress.com/', $url );
5665
		}
5666
5667
		srand( crc32( basename( $url ) ) );
5668
		$static_counter = rand( 0, 2 );
5669
		srand(); // this resets everything that relies on this, like array_rand() and shuffle()
5670
5671
		return preg_replace( '|://[^/]+?/|', "://s$static_counter.wp.com/", $url );
5672
	}
5673
5674
	/* JSON API Authorization */
5675
5676
	/**
5677
	 * Handles the login action for Authorizing the JSON API
5678
	 */
5679
	function login_form_json_api_authorization() {
5680
		$this->verify_json_api_authorization_request();
5681
5682
		add_action( 'wp_login', array( &$this, 'store_json_api_authorization_token' ), 10, 2 );
5683
5684
		add_action( 'login_message', array( &$this, 'login_message_json_api_authorization' ) );
5685
		add_action( 'login_form', array( &$this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
5686
		add_filter( 'site_url', array( &$this, 'post_login_form_to_signed_url' ), 10, 3 );
5687
	}
5688
5689
	// Make sure the login form is POSTed to the signed URL so we can reverify the request
5690
	function post_login_form_to_signed_url( $url, $path, $scheme ) {
5691
		if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) {
5692
			return $url;
5693
		}
5694
5695
		$parsed_url = parse_url( $url );
5696
		$url        = strtok( $url, '?' );
5697
		$url        = "$url?{$_SERVER['QUERY_STRING']}";
5698
		if ( ! empty( $parsed_url['query'] ) ) {
5699
			$url .= "&{$parsed_url['query']}";
5700
		}
5701
5702
		return $url;
5703
	}
5704
5705
	// Make sure the POSTed request is handled by the same action
5706
	function preserve_action_in_login_form_for_json_api_authorization() {
5707
		echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n";
5708
		echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) . "' />\n";
5709
	}
5710
5711
	// If someone logs in to approve API access, store the Access Code in usermeta
5712
	function store_json_api_authorization_token( $user_login, $user ) {
5713
		add_filter( 'login_redirect', array( &$this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
5714
		add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_public_api_domain' ) );
5715
		$token = wp_generate_password( 32, false );
5716
		update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token );
5717
	}
5718
5719
	// Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access
5720
	function allow_wpcom_public_api_domain( $domains ) {
5721
		$domains[] = 'public-api.wordpress.com';
5722
		return $domains;
5723
	}
5724
5725
	static function is_redirect_encoded( $redirect_url ) {
5726
		return preg_match( '/https?%3A%2F%2F/i', $redirect_url ) > 0;
5727
	}
5728
5729
	// Add all wordpress.com environments to the safe redirect whitelist
5730
	function allow_wpcom_environments( $domains ) {
5731
		$domains[] = 'wordpress.com';
5732
		$domains[] = 'wpcalypso.wordpress.com';
5733
		$domains[] = 'horizon.wordpress.com';
5734
		$domains[] = 'calypso.localhost';
5735
		return $domains;
5736
	}
5737
5738
	// Add the Access Code details to the public-api.wordpress.com redirect
5739
	function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
5740
		return add_query_arg(
5741
			urlencode_deep(
5742
				array(
5743
					'jetpack-code'    => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], true ),
5744
					'jetpack-user-id' => (int) $user->ID,
5745
					'jetpack-state'   => $this->json_api_authorization_request['state'],
5746
				)
5747
			),
5748
			$redirect_to
5749
		);
5750
	}
5751
5752
5753
	/**
5754
	 * Verifies the request by checking the signature
5755
	 *
5756
	 * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow
5757
	 * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO.
5758
	 *
5759
	 * @param null|array $environment
5760
	 */
5761
	function verify_json_api_authorization_request( $environment = null ) {
5762
		$environment = is_null( $environment )
5763
			? $_REQUEST
5764
			: $environment;
5765
5766
		list( $envToken, $envVersion, $envUserId ) = explode( ':', $environment['token'] );
5767
		$token                                     = Jetpack_Data::get_access_token( $envUserId, $envToken );
5768
		if ( ! $token || empty( $token->secret ) ) {
5769
			wp_die( __( 'You must connect your Jetpack plugin to WordPress.com to use this feature.', 'jetpack' ) );
5770
		}
5771
5772
		$die_error = __( 'Someone may be trying to trick you into giving them access to your site.  Or it could be you just encountered a bug :).  Either way, please close this window.', 'jetpack' );
5773
5774
		// Host has encoded the request URL, probably as a result of a bad http => https redirect
5775
		if ( self::is_redirect_encoded( $_GET['redirect_to'] ) ) {
5776
			/**
5777
			 * Jetpack authorisation request Error.
5778
			 *
5779
			 * @since 7.5.0
5780
			 */
5781
			do_action( 'jetpack_verify_api_authorization_request_error_double_encode' );
5782
			$die_error = sprintf(
5783
				/* translators: %s is a URL */
5784
				__( 'Your site is incorrectly double-encoding redirects from http to https. This is preventing Jetpack from authenticating your connection. Please visit our <a href="%s">support page</a> for details about how to resolve this.', 'jetpack' ),
5785
				'https://jetpack.com/support/double-encoding/'
5786
			);
5787
		}
5788
5789
		$jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
5790
5791
		if ( isset( $environment['jetpack_json_api_original_query'] ) ) {
5792
			$signature = $jetpack_signature->sign_request(
5793
				$environment['token'],
5794
				$environment['timestamp'],
5795
				$environment['nonce'],
5796
				'',
5797
				'GET',
5798
				$environment['jetpack_json_api_original_query'],
5799
				null,
5800
				true
5801
			);
5802
		} else {
5803
			$signature = $jetpack_signature->sign_current_request(
5804
				array(
5805
					'body'   => null,
5806
					'method' => 'GET',
5807
				)
5808
			);
5809
		}
5810
5811
		if ( ! $signature ) {
5812
			wp_die( $die_error );
5813
		} elseif ( is_wp_error( $signature ) ) {
5814
			wp_die( $die_error );
5815
		} elseif ( ! hash_equals( $signature, $environment['signature'] ) ) {
5816
			if ( is_ssl() ) {
5817
				// If we signed an HTTP request on the Jetpack Servers, but got redirected to HTTPS by the local blog, check the HTTP signature as well
5818
				$signature = $jetpack_signature->sign_current_request(
5819
					array(
5820
						'scheme' => 'http',
5821
						'body'   => null,
5822
						'method' => 'GET',
5823
					)
5824
				);
5825
				if ( ! $signature || is_wp_error( $signature ) || ! hash_equals( $signature, $environment['signature'] ) ) {
5826
					wp_die( $die_error );
5827
				}
5828
			} else {
5829
				wp_die( $die_error );
5830
			}
5831
		}
5832
5833
		$timestamp = (int) $environment['timestamp'];
5834
		$nonce     = stripslashes( (string) $environment['nonce'] );
5835
5836
		if ( ! $this->connection_manager->add_nonce( $timestamp, $nonce ) ) {
5837
			// De-nonce the nonce, at least for 5 minutes.
5838
			// We have to reuse this nonce at least once (used the first time when the initial request is made, used a second time when the login form is POSTed)
5839
			$old_nonce_time = get_option( "jetpack_nonce_{$timestamp}_{$nonce}" );
5840
			if ( $old_nonce_time < time() - 300 ) {
5841
				wp_die( __( 'The authorization process expired.  Please go back and try again.', 'jetpack' ) );
5842
			}
5843
		}
5844
5845
		$data         = json_decode( base64_decode( stripslashes( $environment['data'] ) ) );
5846
		$data_filters = array(
5847
			'state'        => 'opaque',
5848
			'client_id'    => 'int',
5849
			'client_title' => 'string',
5850
			'client_image' => 'url',
5851
		);
5852
5853
		foreach ( $data_filters as $key => $sanitation ) {
5854
			if ( ! isset( $data->$key ) ) {
5855
				wp_die( $die_error );
5856
			}
5857
5858
			switch ( $sanitation ) {
5859
				case 'int':
5860
					$this->json_api_authorization_request[ $key ] = (int) $data->$key;
5861
					break;
5862
				case 'opaque':
5863
					$this->json_api_authorization_request[ $key ] = (string) $data->$key;
5864
					break;
5865
				case 'string':
5866
					$this->json_api_authorization_request[ $key ] = wp_kses( (string) $data->$key, array() );
5867
					break;
5868
				case 'url':
5869
					$this->json_api_authorization_request[ $key ] = esc_url_raw( (string) $data->$key );
5870
					break;
5871
			}
5872
		}
5873
5874
		if ( empty( $this->json_api_authorization_request['client_id'] ) ) {
5875
			wp_die( $die_error );
5876
		}
5877
	}
5878
5879
	function login_message_json_api_authorization( $message ) {
5880
		return '<p class="message">' . sprintf(
5881
			esc_html__( '%s wants to access your site&#8217;s data.  Log in to authorize that access.', 'jetpack' ),
5882
			'<strong>' . esc_html( $this->json_api_authorization_request['client_title'] ) . '</strong>'
5883
		) . '<img src="' . esc_url( $this->json_api_authorization_request['client_image'] ) . '" /></p>';
5884
	}
5885
5886
	/**
5887
	 * Get $content_width, but with a <s>twist</s> filter.
5888
	 */
5889
	public static function get_content_width() {
5890
		$content_width = ( isset( $GLOBALS['content_width'] ) && is_numeric( $GLOBALS['content_width'] ) )
5891
			? $GLOBALS['content_width']
5892
			: false;
5893
		/**
5894
		 * Filter the Content Width value.
5895
		 *
5896
		 * @since 2.2.3
5897
		 *
5898
		 * @param string $content_width Content Width value.
5899
		 */
5900
		return apply_filters( 'jetpack_content_width', $content_width );
5901
	}
5902
5903
	/**
5904
	 * Pings the WordPress.com Mirror Site for the specified options.
5905
	 *
5906
	 * @param string|array $option_names The option names to request from the WordPress.com Mirror Site
5907
	 *
5908
	 * @return array An associative array of the option values as stored in the WordPress.com Mirror Site
5909
	 */
5910
	public function get_cloud_site_options( $option_names ) {
5911
		$option_names = array_filter( (array) $option_names, 'is_string' );
5912
5913
		$xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER ) );
5914
		$xml->query( 'jetpack.fetchSiteOptions', $option_names );
5915
		if ( $xml->isError() ) {
5916
			return array(
5917
				'error_code' => $xml->getErrorCode(),
5918
				'error_msg'  => $xml->getErrorMessage(),
5919
			);
5920
		}
5921
		$cloud_site_options = $xml->getResponse();
5922
5923
		return $cloud_site_options;
5924
	}
5925
5926
	/**
5927
	 * Checks if the site is currently in an identity crisis.
5928
	 *
5929
	 * @return array|bool Array of options that are in a crisis, or false if everything is OK.
5930
	 */
5931
	public static function check_identity_crisis() {
5932
		if ( ! self::is_active() || self::is_development_mode() || ! self::validate_sync_error_idc_option() ) {
5933
			return false;
5934
		}
5935
5936
		return Jetpack_Options::get_option( 'sync_error_idc' );
5937
	}
5938
5939
	/**
5940
	 * Checks whether the home and siteurl specifically are whitelisted
5941
	 * Written so that we don't have re-check $key and $value params every time
5942
	 * we want to check if this site is whitelisted, for example in footer.php
5943
	 *
5944
	 * @since  3.8.0
5945
	 * @return bool True = already whitelisted False = not whitelisted
5946
	 */
5947
	public static function is_staging_site() {
5948
		$is_staging = false;
5949
5950
		$known_staging = array(
5951
			'urls'      => array(
5952
				'#\.staging\.wpengine\.com$#i', // WP Engine
5953
				'#\.staging\.kinsta\.com$#i',   // Kinsta.com
5954
				'#\.stage\.site$#i',            // DreamPress
5955
			),
5956
			'constants' => array(
5957
				'IS_WPE_SNAPSHOT',      // WP Engine
5958
				'KINSTA_DEV_ENV',       // Kinsta.com
5959
				'WPSTAGECOACH_STAGING', // WP Stagecoach
5960
				'JETPACK_STAGING_MODE', // Generic
5961
			),
5962
		);
5963
		/**
5964
		 * Filters the flags of known staging sites.
5965
		 *
5966
		 * @since 3.9.0
5967
		 *
5968
		 * @param array $known_staging {
5969
		 *     An array of arrays that each are used to check if the current site is staging.
5970
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
5971
		 *     @type array $constants PHP constants of known staging/developement environments.
5972
		 *  }
5973
		 */
5974
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
5975
5976
		if ( isset( $known_staging['urls'] ) ) {
5977
			foreach ( $known_staging['urls'] as $url ) {
5978
				if ( preg_match( $url, site_url() ) ) {
5979
					$is_staging = true;
5980
					break;
5981
				}
5982
			}
5983
		}
5984
5985
		if ( isset( $known_staging['constants'] ) ) {
5986
			foreach ( $known_staging['constants'] as $constant ) {
5987
				if ( defined( $constant ) && constant( $constant ) ) {
5988
					$is_staging = true;
5989
				}
5990
			}
5991
		}
5992
5993
		// Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
5994
		if ( ! $is_staging && self::validate_sync_error_idc_option() ) {
5995
			$is_staging = true;
5996
		}
5997
5998
		/**
5999
		 * Filters is_staging_site check.
6000
		 *
6001
		 * @since 3.9.0
6002
		 *
6003
		 * @param bool $is_staging If the current site is a staging site.
6004
		 */
6005
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
6006
	}
6007
6008
	/**
6009
	 * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup.
6010
	 *
6011
	 * @since 4.4.0
6012
	 * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC
6013
	 *
6014
	 * @return bool
6015
	 */
6016
	public static function validate_sync_error_idc_option() {
6017
		$is_valid = false;
6018
6019
		$idc_allowed = get_transient( 'jetpack_idc_allowed' );
6020
		if ( false === $idc_allowed ) {
6021
			$response = wp_remote_get( 'https://jetpack.com/is-idc-allowed/' );
6022
			if ( 200 === (int) wp_remote_retrieve_response_code( $response ) ) {
6023
				$json               = json_decode( wp_remote_retrieve_body( $response ) );
6024
				$idc_allowed        = isset( $json, $json->result ) && $json->result ? '1' : '0';
6025
				$transient_duration = HOUR_IN_SECONDS;
6026
			} else {
6027
				// If the request failed for some reason, then assume IDC is allowed and set shorter transient.
6028
				$idc_allowed        = '1';
6029
				$transient_duration = 5 * MINUTE_IN_SECONDS;
6030
			}
6031
6032
			set_transient( 'jetpack_idc_allowed', $idc_allowed, $transient_duration );
6033
		}
6034
6035
		// Is the site opted in and does the stored sync_error_idc option match what we now generate?
6036
		$sync_error = Jetpack_Options::get_option( 'sync_error_idc' );
6037
		if ( $idc_allowed && $sync_error && self::sync_idc_optin() ) {
6038
			$local_options = self::get_sync_error_idc_option();
6039
			if ( $sync_error['home'] === $local_options['home'] && $sync_error['siteurl'] === $local_options['siteurl'] ) {
6040
				$is_valid = true;
6041
			}
6042
		}
6043
6044
		/**
6045
		 * Filters whether the sync_error_idc option is valid.
6046
		 *
6047
		 * @since 4.4.0
6048
		 *
6049
		 * @param bool $is_valid If the sync_error_idc is valid or not.
6050
		 */
6051
		$is_valid = (bool) apply_filters( 'jetpack_sync_error_idc_validation', $is_valid );
6052
6053
		if ( ! $idc_allowed || ( ! $is_valid && $sync_error ) ) {
6054
			// Since the option exists, and did not validate, delete it
6055
			Jetpack_Options::delete_option( 'sync_error_idc' );
6056
		}
6057
6058
		return $is_valid;
6059
	}
6060
6061
	/**
6062
	 * Normalizes a url by doing three things:
6063
	 *  - Strips protocol
6064
	 *  - Strips www
6065
	 *  - Adds a trailing slash
6066
	 *
6067
	 * @since 4.4.0
6068
	 * @param string $url
6069
	 * @return WP_Error|string
6070
	 */
6071
	public static function normalize_url_protocol_agnostic( $url ) {
6072
		$parsed_url = wp_parse_url( trailingslashit( esc_url_raw( $url ) ) );
6073
		if ( ! $parsed_url || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
6074
			return new WP_Error( 'cannot_parse_url', sprintf( esc_html__( 'Cannot parse URL %s', 'jetpack' ), $url ) );
6075
		}
6076
6077
		// Strip www and protocols
6078
		$url = preg_replace( '/^www\./i', '', $parsed_url['host'] . $parsed_url['path'] );
6079
		return $url;
6080
	}
6081
6082
	/**
6083
	 * Gets the value that is to be saved in the jetpack_sync_error_idc option.
6084
	 *
6085
	 * @since 4.4.0
6086
	 * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB
6087
	 *
6088
	 * @param array $response
6089
	 * @return array Array of the local urls, wpcom urls, and error code
6090
	 */
6091
	public static function get_sync_error_idc_option( $response = array() ) {
6092
		// Since the local options will hit the database directly, store the values
6093
		// in a transient to allow for autoloading and caching on subsequent views.
6094
		$local_options = get_transient( 'jetpack_idc_local' );
6095
		if ( false === $local_options ) {
6096
			$local_options = array(
6097
				'home'    => Functions::home_url(),
6098
				'siteurl' => Functions::site_url(),
6099
			);
6100
			set_transient( 'jetpack_idc_local', $local_options, MINUTE_IN_SECONDS );
6101
		}
6102
6103
		$options = array_merge( $local_options, $response );
6104
6105
		$returned_values = array();
6106
		foreach ( $options as $key => $option ) {
6107
			if ( 'error_code' === $key ) {
6108
				$returned_values[ $key ] = $option;
6109
				continue;
6110
			}
6111
6112
			if ( is_wp_error( $normalized_url = self::normalize_url_protocol_agnostic( $option ) ) ) {
6113
				continue;
6114
			}
6115
6116
			$returned_values[ $key ] = $normalized_url;
6117
		}
6118
6119
		set_transient( 'jetpack_idc_option', $returned_values, MINUTE_IN_SECONDS );
6120
6121
		return $returned_values;
6122
	}
6123
6124
	/**
6125
	 * Returns the value of the jetpack_sync_idc_optin filter, or constant.
6126
	 * If set to true, the site will be put into staging mode.
6127
	 *
6128
	 * @since 4.3.2
6129
	 * @return bool
6130
	 */
6131
	public static function sync_idc_optin() {
6132
		if ( Constants::is_defined( 'JETPACK_SYNC_IDC_OPTIN' ) ) {
6133
			$default = Constants::get_constant( 'JETPACK_SYNC_IDC_OPTIN' );
6134
		} else {
6135
			$default = ! Constants::is_defined( 'SUNRISE' ) && ! is_multisite();
6136
		}
6137
6138
		/**
6139
		 * Allows sites to optin to IDC mitigation which blocks the site from syncing to WordPress.com when the home
6140
		 * URL or site URL do not match what WordPress.com expects. The default value is either false, or the value of
6141
		 * JETPACK_SYNC_IDC_OPTIN constant if set.
6142
		 *
6143
		 * @since 4.3.2
6144
		 *
6145
		 * @param bool $default Whether the site is opted in to IDC mitigation.
6146
		 */
6147
		return (bool) apply_filters( 'jetpack_sync_idc_optin', $default );
6148
	}
6149
6150
	/**
6151
	 * Maybe Use a .min.css stylesheet, maybe not.
6152
	 *
6153
	 * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args.
6154
	 */
6155
	public static function maybe_min_asset( $url, $path, $plugin ) {
6156
		// Short out on things trying to find actual paths.
6157
		if ( ! $path || empty( $plugin ) ) {
6158
			return $url;
6159
		}
6160
6161
		$path = ltrim( $path, '/' );
6162
6163
		// Strip out the abspath.
6164
		$base = dirname( plugin_basename( $plugin ) );
6165
6166
		// Short out on non-Jetpack assets.
6167
		if ( 'jetpack/' !== substr( $base, 0, 8 ) ) {
6168
			return $url;
6169
		}
6170
6171
		// File name parsing.
6172
		$file              = "{$base}/{$path}";
6173
		$full_path         = JETPACK__PLUGIN_DIR . substr( $file, 8 );
6174
		$file_name         = substr( $full_path, strrpos( $full_path, '/' ) + 1 );
6175
		$file_name_parts_r = array_reverse( explode( '.', $file_name ) );
6176
		$extension         = array_shift( $file_name_parts_r );
6177
6178
		if ( in_array( strtolower( $extension ), array( 'css', 'js' ) ) ) {
6179
			// Already pointing at the minified version.
6180
			if ( 'min' === $file_name_parts_r[0] ) {
6181
				return $url;
6182
			}
6183
6184
			$min_full_path = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $full_path );
6185
			if ( file_exists( $min_full_path ) ) {
6186
				$url = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $url );
6187
				// If it's a CSS file, stash it so we can set the .min suffix for rtl-ing.
6188
				if ( 'css' === $extension ) {
6189
					$key                      = str_replace( JETPACK__PLUGIN_DIR, 'jetpack/', $min_full_path );
6190
					self::$min_assets[ $key ] = $path;
6191
				}
6192
			}
6193
		}
6194
6195
		return $url;
6196
	}
6197
6198
	/**
6199
	 * If the asset is minified, let's flag .min as the suffix.
6200
	 *
6201
	 * Attached to `style_loader_src` filter.
6202
	 *
6203
	 * @param string $tag The tag that would link to the external asset.
6204
	 * @param string $handle The registered handle of the script in question.
6205
	 * @param string $href The url of the asset in question.
6206
	 */
6207
	public static function set_suffix_on_min( $src, $handle ) {
6208
		if ( false === strpos( $src, '.min.css' ) ) {
6209
			return $src;
6210
		}
6211
6212
		if ( ! empty( self::$min_assets ) ) {
6213
			foreach ( self::$min_assets as $file => $path ) {
6214
				if ( false !== strpos( $src, $file ) ) {
6215
					wp_style_add_data( $handle, 'suffix', '.min' );
6216
					return $src;
6217
				}
6218
			}
6219
		}
6220
6221
		return $src;
6222
	}
6223
6224
	/**
6225
	 * Maybe inlines a stylesheet.
6226
	 *
6227
	 * If you'd like to inline a stylesheet instead of printing a link to it,
6228
	 * wp_style_add_data( 'handle', 'jetpack-inline', true );
6229
	 *
6230
	 * Attached to `style_loader_tag` filter.
6231
	 *
6232
	 * @param string $tag The tag that would link to the external asset.
6233
	 * @param string $handle The registered handle of the script in question.
6234
	 *
6235
	 * @return string
6236
	 */
6237
	public static function maybe_inline_style( $tag, $handle ) {
6238
		global $wp_styles;
6239
		$item = $wp_styles->registered[ $handle ];
6240
6241
		if ( ! isset( $item->extra['jetpack-inline'] ) || ! $item->extra['jetpack-inline'] ) {
6242
			return $tag;
6243
		}
6244
6245
		if ( preg_match( '# href=\'([^\']+)\' #i', $tag, $matches ) ) {
6246
			$href = $matches[1];
6247
			// Strip off query string
6248
			if ( $pos = strpos( $href, '?' ) ) {
6249
				$href = substr( $href, 0, $pos );
6250
			}
6251
			// Strip off fragment
6252
			if ( $pos = strpos( $href, '#' ) ) {
6253
				$href = substr( $href, 0, $pos );
6254
			}
6255
		} else {
6256
			return $tag;
6257
		}
6258
6259
		$plugins_dir = plugin_dir_url( JETPACK__PLUGIN_FILE );
6260
		if ( $plugins_dir !== substr( $href, 0, strlen( $plugins_dir ) ) ) {
6261
			return $tag;
6262
		}
6263
6264
		// If this stylesheet has a RTL version, and the RTL version replaces normal...
6265
		if ( isset( $item->extra['rtl'] ) && 'replace' === $item->extra['rtl'] && is_rtl() ) {
6266
			// And this isn't the pass that actually deals with the RTL version...
6267
			if ( false === strpos( $tag, " id='$handle-rtl-css' " ) ) {
6268
				// Short out, as the RTL version will deal with it in a moment.
6269
				return $tag;
6270
			}
6271
		}
6272
6273
		$file = JETPACK__PLUGIN_DIR . substr( $href, strlen( $plugins_dir ) );
6274
		$css  = self::absolutize_css_urls( file_get_contents( $file ), $href );
6275
		if ( $css ) {
6276
			$tag = "<!-- Inline {$item->handle} -->\r\n";
6277
			if ( empty( $item->extra['after'] ) ) {
6278
				wp_add_inline_style( $handle, $css );
6279
			} else {
6280
				array_unshift( $item->extra['after'], $css );
6281
				wp_style_add_data( $handle, 'after', $item->extra['after'] );
6282
			}
6283
		}
6284
6285
		return $tag;
6286
	}
6287
6288
	/**
6289
	 * Loads a view file from the views
6290
	 *
6291
	 * Data passed in with the $data parameter will be available in the
6292
	 * template file as $data['value']
6293
	 *
6294
	 * @param string $template - Template file to load
6295
	 * @param array  $data - Any data to pass along to the template
6296
	 * @return boolean - If template file was found
6297
	 **/
6298
	public function load_view( $template, $data = array() ) {
6299
		$views_dir = JETPACK__PLUGIN_DIR . 'views/';
6300
6301
		if ( file_exists( $views_dir . $template ) ) {
6302
			require_once $views_dir . $template;
6303
			return true;
6304
		}
6305
6306
		error_log( "Jetpack: Unable to find view file $views_dir$template" );
6307
		return false;
6308
	}
6309
6310
	/**
6311
	 * Throws warnings for deprecated hooks to be removed from Jetpack
6312
	 */
6313
	public function deprecated_hooks() {
6314
		global $wp_filter;
6315
6316
		/*
6317
		 * Format:
6318
		 * deprecated_filter_name => replacement_name
6319
		 *
6320
		 * If there is no replacement, use null for replacement_name
6321
		 */
6322
		$deprecated_list = array(
6323
			'jetpack_bail_on_shortcode'                    => 'jetpack_shortcodes_to_include',
6324
			'wpl_sharing_2014_1'                           => null,
6325
			'jetpack-tools-to-include'                     => 'jetpack_tools_to_include',
6326
			'jetpack_identity_crisis_options_to_check'     => null,
6327
			'update_option_jetpack_single_user_site'       => null,
6328
			'audio_player_default_colors'                  => null,
6329
			'add_option_jetpack_featured_images_enabled'   => null,
6330
			'add_option_jetpack_update_details'            => null,
6331
			'add_option_jetpack_updates'                   => null,
6332
			'add_option_jetpack_network_name'              => null,
6333
			'add_option_jetpack_network_allow_new_registrations' => null,
6334
			'add_option_jetpack_network_add_new_users'     => null,
6335
			'add_option_jetpack_network_site_upload_space' => null,
6336
			'add_option_jetpack_network_upload_file_types' => null,
6337
			'add_option_jetpack_network_enable_administration_menus' => null,
6338
			'add_option_jetpack_is_multi_site'             => null,
6339
			'add_option_jetpack_is_main_network'           => null,
6340
			'add_option_jetpack_main_network_site'         => null,
6341
			'jetpack_sync_all_registered_options'          => null,
6342
			'jetpack_has_identity_crisis'                  => 'jetpack_sync_error_idc_validation',
6343
			'jetpack_is_post_mailable'                     => null,
6344
			'jetpack_seo_site_host'                        => null,
6345
			'jetpack_installed_plugin'                     => 'jetpack_plugin_installed',
6346
			'jetpack_holiday_snow_option_name'             => null,
6347
			'jetpack_holiday_chance_of_snow'               => null,
6348
			'jetpack_holiday_snow_js_url'                  => null,
6349
			'jetpack_is_holiday_snow_season'               => null,
6350
			'jetpack_holiday_snow_option_updated'          => null,
6351
			'jetpack_holiday_snowing'                      => null,
6352
			'jetpack_sso_auth_cookie_expirtation'          => 'jetpack_sso_auth_cookie_expiration',
6353
			'jetpack_cache_plans'                          => null,
6354
			'jetpack_updated_theme'                        => 'jetpack_updated_themes',
6355
			'jetpack_lazy_images_skip_image_with_atttributes' => 'jetpack_lazy_images_skip_image_with_attributes',
6356
			'jetpack_enable_site_verification'             => null,
6357
			'can_display_jetpack_manage_notice'            => null,
6358
			// Removed in Jetpack 7.3.0
6359
			'atd_load_scripts'                             => null,
6360
			'atd_http_post_timeout'                        => null,
6361
			'atd_http_post_error'                          => null,
6362
			'atd_service_domain'                           => null,
6363
			'jetpack_widget_authors_exclude'               => 'jetpack_widget_authors_params',
6364
		);
6365
6366
		// This is a silly loop depth. Better way?
6367
		foreach ( $deprecated_list as $hook => $hook_alt ) {
6368
			if ( has_action( $hook ) ) {
6369
				foreach ( $wp_filter[ $hook ] as $func => $values ) {
6370
					foreach ( $values as $hooked ) {
6371
						if ( is_callable( $hooked['function'] ) ) {
6372
							$function_name = 'an anonymous function';
6373
						} else {
6374
							$function_name = $hooked['function'];
6375
						}
6376
						_deprecated_function( $hook . ' used for ' . $function_name, null, $hook_alt );
6377
					}
6378
				}
6379
			}
6380
		}
6381
	}
6382
6383
	/**
6384
	 * Converts any url in a stylesheet, to the correct absolute url.
6385
	 *
6386
	 * Considerations:
6387
	 *  - Normal, relative URLs     `feh.png`
6388
	 *  - Data URLs                 `data:image/gif;base64,eh129ehiuehjdhsa==`
6389
	 *  - Schema-agnostic URLs      `//domain.com/feh.png`
6390
	 *  - Absolute URLs             `http://domain.com/feh.png`
6391
	 *  - Domain root relative URLs `/feh.png`
6392
	 *
6393
	 * @param $css string: The raw CSS -- should be read in directly from the file.
6394
	 * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from.
6395
	 *
6396
	 * @return mixed|string
6397
	 */
6398
	public static function absolutize_css_urls( $css, $css_file_url ) {
6399
		$pattern = '#url\((?P<path>[^)]*)\)#i';
6400
		$css_dir = dirname( $css_file_url );
6401
		$p       = parse_url( $css_dir );
6402
		$domain  = sprintf(
6403
			'%1$s//%2$s%3$s%4$s',
6404
			isset( $p['scheme'] ) ? "{$p['scheme']}:" : '',
6405
			isset( $p['user'], $p['pass'] ) ? "{$p['user']}:{$p['pass']}@" : '',
6406
			$p['host'],
6407
			isset( $p['port'] ) ? ":{$p['port']}" : ''
6408
		);
6409
6410
		if ( preg_match_all( $pattern, $css, $matches, PREG_SET_ORDER ) ) {
6411
			$find = $replace = array();
6412
			foreach ( $matches as $match ) {
6413
				$url = trim( $match['path'], "'\" \t" );
6414
6415
				// If this is a data url, we don't want to mess with it.
6416
				if ( 'data:' === substr( $url, 0, 5 ) ) {
6417
					continue;
6418
				}
6419
6420
				// If this is an absolute or protocol-agnostic url,
6421
				// we don't want to mess with it.
6422
				if ( preg_match( '#^(https?:)?//#i', $url ) ) {
6423
					continue;
6424
				}
6425
6426
				switch ( substr( $url, 0, 1 ) ) {
6427
					case '/':
6428
						$absolute = $domain . $url;
6429
						break;
6430
					default:
6431
						$absolute = $css_dir . '/' . $url;
6432
				}
6433
6434
				$find[]    = $match[0];
6435
				$replace[] = sprintf( 'url("%s")', $absolute );
6436
			}
6437
			$css = str_replace( $find, $replace, $css );
6438
		}
6439
6440
		return $css;
6441
	}
6442
6443
	/**
6444
	 * This methods removes all of the registered css files on the front end
6445
	 * from Jetpack in favor of using a single file. In effect "imploding"
6446
	 * all the files into one file.
6447
	 *
6448
	 * Pros:
6449
	 * - Uses only ONE css asset connection instead of 15
6450
	 * - Saves a minimum of 56k
6451
	 * - Reduces server load
6452
	 * - Reduces time to first painted byte
6453
	 *
6454
	 * Cons:
6455
	 * - Loads css for ALL modules. However all selectors are prefixed so it
6456
	 *      should not cause any issues with themes.
6457
	 * - Plugins/themes dequeuing styles no longer do anything. See
6458
	 *      jetpack_implode_frontend_css filter for a workaround
6459
	 *
6460
	 * For some situations developers may wish to disable css imploding and
6461
	 * instead operate in legacy mode where each file loads seperately and
6462
	 * can be edited individually or dequeued. This can be accomplished with
6463
	 * the following line:
6464
	 *
6465
	 * add_filter( 'jetpack_implode_frontend_css', '__return_false' );
6466
	 *
6467
	 * @since 3.2
6468
	 **/
6469
	public function implode_frontend_css( $travis_test = false ) {
6470
		$do_implode = true;
6471
		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
6472
			$do_implode = false;
6473
		}
6474
6475
		// Do not implode CSS when the page loads via the AMP plugin.
6476
		if ( Jetpack_AMP_Support::is_amp_request() ) {
6477
			$do_implode = false;
6478
		}
6479
6480
		/**
6481
		 * Allow CSS to be concatenated into a single jetpack.css file.
6482
		 *
6483
		 * @since 3.2.0
6484
		 *
6485
		 * @param bool $do_implode Should CSS be concatenated? Default to true.
6486
		 */
6487
		$do_implode = apply_filters( 'jetpack_implode_frontend_css', $do_implode );
6488
6489
		// Do not use the imploded file when default behavior was altered through the filter
6490
		if ( ! $do_implode ) {
6491
			return;
6492
		}
6493
6494
		// We do not want to use the imploded file in dev mode, or if not connected
6495
		if ( self::is_development_mode() || ! self::is_active() ) {
6496
			if ( ! $travis_test ) {
6497
				return;
6498
			}
6499
		}
6500
6501
		// Do not use the imploded file if sharing css was dequeued via the sharing settings screen
6502
		if ( get_option( 'sharedaddy_disable_resources' ) ) {
6503
			return;
6504
		}
6505
6506
		/*
6507
		 * Now we assume Jetpack is connected and able to serve the single
6508
		 * file.
6509
		 *
6510
		 * In the future there will be a check here to serve the file locally
6511
		 * or potentially from the Jetpack CDN
6512
		 *
6513
		 * For now:
6514
		 * - Enqueue a single imploded css file
6515
		 * - Zero out the style_loader_tag for the bundled ones
6516
		 * - Be happy, drink scotch
6517
		 */
6518
6519
		add_filter( 'style_loader_tag', array( $this, 'concat_remove_style_loader_tag' ), 10, 2 );
6520
6521
		$version = self::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . 'css/jetpack.css' ) : JETPACK__VERSION;
6522
6523
		wp_enqueue_style( 'jetpack_css', plugins_url( 'css/jetpack.css', __FILE__ ), array(), $version );
6524
		wp_style_add_data( 'jetpack_css', 'rtl', 'replace' );
6525
	}
6526
6527
	function concat_remove_style_loader_tag( $tag, $handle ) {
6528
		if ( in_array( $handle, $this->concatenated_style_handles ) ) {
6529
			$tag = '';
6530
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
6531
				$tag = '<!-- `' . esc_html( $handle ) . "` is included in the concatenated jetpack.css -->\r\n";
6532
			}
6533
		}
6534
6535
		return $tag;
6536
	}
6537
6538
	/**
6539
	 * Add an async attribute to scripts that can be loaded asynchronously.
6540
	 * https://www.w3schools.com/tags/att_script_async.asp
6541
	 *
6542
	 * @since 7.7.0
6543
	 *
6544
	 * @param string $tag    The <script> tag for the enqueued script.
6545
	 * @param string $handle The script's registered handle.
6546
	 * @param string $src    The script's source URL.
6547
	 */
6548
	public function script_add_async( $tag, $handle, $src ) {
6549
		if ( in_array( $handle, $this->async_script_handles, true ) ) {
6550
			return preg_replace( '/^<script /i', '<script async ', $tag );
6551
		}
6552
6553
		return $tag;
6554
	}
6555
6556
	/*
6557
	 * Check the heartbeat data
6558
	 *
6559
	 * Organizes the heartbeat data by severity.  For example, if the site
6560
	 * is in an ID crisis, it will be in the $filtered_data['bad'] array.
6561
	 *
6562
	 * Data will be added to "caution" array, if it either:
6563
	 *  - Out of date Jetpack version
6564
	 *  - Out of date WP version
6565
	 *  - Out of date PHP version
6566
	 *
6567
	 * $return array $filtered_data
6568
	 */
6569
	public static function jetpack_check_heartbeat_data() {
6570
		$raw_data = Jetpack_Heartbeat::generate_stats_array();
6571
6572
		$good    = array();
6573
		$caution = array();
6574
		$bad     = array();
6575
6576
		foreach ( $raw_data as $stat => $value ) {
6577
6578
			// Check jetpack version
6579
			if ( 'version' == $stat ) {
6580
				if ( version_compare( $value, JETPACK__VERSION, '<' ) ) {
6581
					$caution[ $stat ] = $value . ' - min supported is ' . JETPACK__VERSION;
6582
					continue;
6583
				}
6584
			}
6585
6586
			// Check WP version
6587
			if ( 'wp-version' == $stat ) {
6588
				if ( version_compare( $value, JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
6589
					$caution[ $stat ] = $value . ' - min supported is ' . JETPACK__MINIMUM_WP_VERSION;
6590
					continue;
6591
				}
6592
			}
6593
6594
			// Check PHP version
6595
			if ( 'php-version' == $stat ) {
6596
				if ( version_compare( PHP_VERSION, JETPACK__MINIMUM_PHP_VERSION, '<' ) ) {
6597
					$caution[ $stat ] = $value . " - min supported is " . JETPACK__MINIMUM_PHP_VERSION;
6598
					continue;
6599
				}
6600
			}
6601
6602
			// Check ID crisis
6603
			if ( 'identitycrisis' == $stat ) {
6604
				if ( 'yes' == $value ) {
6605
					$bad[ $stat ] = $value;
6606
					continue;
6607
				}
6608
			}
6609
6610
			// The rest are good :)
6611
			$good[ $stat ] = $value;
6612
		}
6613
6614
		$filtered_data = array(
6615
			'good'    => $good,
6616
			'caution' => $caution,
6617
			'bad'     => $bad,
6618
		);
6619
6620
		return $filtered_data;
6621
	}
6622
6623
6624
	/*
6625
	 * This method is used to organize all options that can be reset
6626
	 * without disconnecting Jetpack.
6627
	 *
6628
	 * It is used in class.jetpack-cli.php to reset options
6629
	 *
6630
	 * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat.
6631
	 *
6632
	 * @return array of options to delete.
6633
	 */
6634
	public static function get_jetpack_options_for_reset() {
6635
		return Jetpack_Options::get_options_for_reset();
6636
	}
6637
6638
	/*
6639
	 * Strip http:// or https:// from a url, replaces forward slash with ::,
6640
	 * so we can bring them directly to their site in calypso.
6641
	 *
6642
	 * @param string | url
6643
	 * @return string | url without the guff
6644
	 */
6645
	public static function build_raw_urls( $url ) {
6646
		$strip_http = '/.*?:\/\//i';
6647
		$url        = preg_replace( $strip_http, '', $url );
6648
		$url        = str_replace( '/', '::', $url );
6649
		return $url;
6650
	}
6651
6652
	/**
6653
	 * Stores and prints out domains to prefetch for page speed optimization.
6654
	 *
6655
	 * @param mixed $new_urls
6656
	 */
6657
	public static function dns_prefetch( $new_urls = null ) {
6658
		static $prefetch_urls = array();
6659
		if ( empty( $new_urls ) && ! empty( $prefetch_urls ) ) {
6660
			echo "\r\n";
6661
			foreach ( $prefetch_urls as $this_prefetch_url ) {
6662
				printf( "<link rel='dns-prefetch' href='%s'/>\r\n", esc_attr( $this_prefetch_url ) );
6663
			}
6664
		} elseif ( ! empty( $new_urls ) ) {
6665
			if ( ! has_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) ) ) {
6666
				add_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) );
6667
			}
6668
			foreach ( (array) $new_urls as $this_new_url ) {
6669
				$prefetch_urls[] = strtolower( untrailingslashit( preg_replace( '#^https?://#i', '//', $this_new_url ) ) );
6670
			}
6671
			$prefetch_urls = array_unique( $prefetch_urls );
6672
		}
6673
	}
6674
6675
	public function wp_dashboard_setup() {
6676
		if ( self::is_active() ) {
6677
			add_action( 'jetpack_dashboard_widget', array( __CLASS__, 'dashboard_widget_footer' ), 999 );
6678
		}
6679
6680
		if ( has_action( 'jetpack_dashboard_widget' ) ) {
6681
			$jetpack_logo = new Jetpack_Logo();
6682
			$widget_title = sprintf(
6683
				wp_kses(
6684
					/* translators: Placeholder is a Jetpack logo. */
6685
					__( 'Stats <span>by %s</span>', 'jetpack' ),
6686
					array( 'span' => array() )
6687
				),
6688
				$jetpack_logo->get_jp_emblem( true )
6689
			);
6690
6691
			wp_add_dashboard_widget(
6692
				'jetpack_summary_widget',
6693
				$widget_title,
6694
				array( __CLASS__, 'dashboard_widget' )
6695
			);
6696
			wp_enqueue_style( 'jetpack-dashboard-widget', plugins_url( 'css/dashboard-widget.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
6697
			wp_style_add_data( 'jetpack-dashboard-widget', 'rtl', 'replace' );
6698
6699
			// If we're inactive and not in development mode, sort our box to the top.
6700
			if ( ! self::is_active() && ! self::is_development_mode() ) {
6701
				global $wp_meta_boxes;
6702
6703
				$dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
6704
				$ours      = array( 'jetpack_summary_widget' => $dashboard['jetpack_summary_widget'] );
6705
6706
				$wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard );
6707
			}
6708
		}
6709
	}
6710
6711
	/**
6712
	 * @param mixed $result Value for the user's option
6713
	 * @return mixed
6714
	 */
6715
	function get_user_option_meta_box_order_dashboard( $sorted ) {
6716
		if ( ! is_array( $sorted ) ) {
6717
			return $sorted;
6718
		}
6719
6720
		foreach ( $sorted as $box_context => $ids ) {
6721
			if ( false === strpos( $ids, 'dashboard_stats' ) ) {
6722
				// If the old id isn't anywhere in the ids, don't bother exploding and fail out.
6723
				continue;
6724
			}
6725
6726
			$ids_array = explode( ',', $ids );
6727
			$key       = array_search( 'dashboard_stats', $ids_array );
6728
6729
			if ( false !== $key ) {
6730
				// If we've found that exact value in the option (and not `google_dashboard_stats` for example)
6731
				$ids_array[ $key ]      = 'jetpack_summary_widget';
6732
				$sorted[ $box_context ] = implode( ',', $ids_array );
6733
				// We've found it, stop searching, and just return.
6734
				break;
6735
			}
6736
		}
6737
6738
		return $sorted;
6739
	}
6740
6741
	public static function dashboard_widget() {
6742
		/**
6743
		 * Fires when the dashboard is loaded.
6744
		 *
6745
		 * @since 3.4.0
6746
		 */
6747
		do_action( 'jetpack_dashboard_widget' );
6748
	}
6749
6750
	public static function dashboard_widget_footer() {
6751
		?>
6752
		<footer>
6753
6754
		<div class="protect">
6755
			<?php if ( self::is_module_active( 'protect' ) ) : ?>
6756
				<h3><?php echo number_format_i18n( get_site_option( 'jetpack_protect_blocked_attempts', 0 ) ); ?></h3>
6757
				<p><?php echo esc_html_x( 'Blocked malicious login attempts', '{#} Blocked malicious login attempts -- number is on a prior line, text is a caption.', 'jetpack' ); ?></p>
6758
			<?php elseif ( current_user_can( 'jetpack_activate_modules' ) && ! self::is_development_mode() ) : ?>
6759
				<a href="
6760
				<?php
6761
				echo esc_url(
6762
					wp_nonce_url(
6763
						self::admin_url(
6764
							array(
6765
								'action' => 'activate',
6766
								'module' => 'protect',
6767
							)
6768
						),
6769
						'jetpack_activate-protect'
6770
					)
6771
				);
6772
				?>
6773
							" class="button button-jetpack" title="<?php esc_attr_e( 'Protect helps to keep you secure from brute-force login attacks.', 'jetpack' ); ?>">
6774
					<?php esc_html_e( 'Activate Protect', 'jetpack' ); ?>
6775
				</a>
6776
			<?php else : ?>
6777
				<?php esc_html_e( 'Protect is inactive.', 'jetpack' ); ?>
6778
			<?php endif; ?>
6779
		</div>
6780
6781
		<div class="akismet">
6782
			<?php if ( is_plugin_active( 'akismet/akismet.php' ) ) : ?>
6783
				<h3><?php echo number_format_i18n( get_option( 'akismet_spam_count', 0 ) ); ?></h3>
6784
				<p><?php echo esc_html_x( 'Spam comments blocked by Akismet.', '{#} Spam comments blocked by Akismet -- number is on a prior line, text is a caption.', 'jetpack' ); ?></p>
6785
			<?php elseif ( current_user_can( 'activate_plugins' ) && ! is_wp_error( validate_plugin( 'akismet/akismet.php' ) ) ) : ?>
6786
				<a href="
6787
				<?php
6788
				echo esc_url(
6789
					wp_nonce_url(
6790
						add_query_arg(
6791
							array(
6792
								'action' => 'activate',
6793
								'plugin' => 'akismet/akismet.php',
6794
							),
6795
							admin_url( 'plugins.php' )
6796
						),
6797
						'activate-plugin_akismet/akismet.php'
6798
					)
6799
				);
6800
				?>
6801
							" class="button button-jetpack">
6802
					<?php esc_html_e( 'Activate Akismet', 'jetpack' ); ?>
6803
				</a>
6804
			<?php else : ?>
6805
				<p><a href="<?php echo esc_url( 'https://akismet.com/?utm_source=jetpack&utm_medium=link&utm_campaign=Jetpack%20Dashboard%20Widget%20Footer%20Link' ); ?>"><?php esc_html_e( 'Akismet can help to keep your blog safe from spam!', 'jetpack' ); ?></a></p>
6806
			<?php endif; ?>
6807
		</div>
6808
6809
		</footer>
6810
		<?php
6811
	}
6812
6813
	/*
6814
	 * Adds a "blank" column in the user admin table to display indication of user connection.
6815
	 */
6816
	function jetpack_icon_user_connected( $columns ) {
6817
		$columns['user_jetpack'] = '';
6818
		return $columns;
6819
	}
6820
6821
	/*
6822
	 * Show Jetpack icon if the user is linked.
6823
	 */
6824
	function jetpack_show_user_connected_icon( $val, $col, $user_id ) {
6825
		if ( 'user_jetpack' == $col && self::is_user_connected( $user_id ) ) {
6826
			$jetpack_logo = new Jetpack_Logo();
6827
			$emblem_html  = sprintf(
6828
				'<a title="%1$s" class="jp-emblem-user-admin">%2$s</a>',
6829
				esc_attr__( 'This user is linked and ready to fly with Jetpack.', 'jetpack' ),
6830
				$jetpack_logo->get_jp_emblem()
6831
			);
6832
			return $emblem_html;
6833
		}
6834
6835
		return $val;
6836
	}
6837
6838
	/*
6839
	 * Style the Jetpack user column
6840
	 */
6841
	function jetpack_user_col_style() {
6842
		global $current_screen;
6843
		if ( ! empty( $current_screen->base ) && 'users' == $current_screen->base ) {
6844
			?>
6845
			<style>
6846
				.fixed .column-user_jetpack {
6847
					width: 21px;
6848
				}
6849
				.jp-emblem-user-admin svg {
6850
					width: 20px;
6851
					height: 20px;
6852
				}
6853
				.jp-emblem-user-admin path {
6854
					fill: #00BE28;
6855
				}
6856
			</style>
6857
			<?php
6858
		}
6859
	}
6860
6861
	/**
6862
	 * Checks if Akismet is active and working.
6863
	 *
6864
	 * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key
6865
	 * that implied usage of methods present since more recent version.
6866
	 * See https://github.com/Automattic/jetpack/pull/9585
6867
	 *
6868
	 * @since  5.1.0
6869
	 *
6870
	 * @return bool True = Akismet available. False = Aksimet not available.
6871
	 */
6872
	public static function is_akismet_active() {
6873
		static $status = null;
6874
6875
		if ( ! is_null( $status ) ) {
6876
			return $status;
6877
		}
6878
6879
		// Check if a modern version of Akismet is active.
6880
		if ( ! method_exists( 'Akismet', 'http_post' ) ) {
6881
			$status = false;
6882
			return $status;
6883
		}
6884
6885
		// Make sure there is a key known to Akismet at all before verifying key.
6886
		$akismet_key = Akismet::get_api_key();
6887
		if ( ! $akismet_key ) {
6888
			$status = false;
6889
			return $status;
6890
		}
6891
6892
		// Possible values: valid, invalid, failure via Akismet. false if no status is cached.
6893
		$akismet_key_state = get_transient( 'jetpack_akismet_key_is_valid' );
6894
6895
		// 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.
6896
		$recheck = ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) && 'valid' !== $akismet_key_state;
6897
		// We cache the result of the Akismet key verification for ten minutes.
6898
		if ( ! $akismet_key_state || $recheck ) {
6899
			$akismet_key_state = Akismet::verify_key( $akismet_key );
6900
			set_transient( 'jetpack_akismet_key_is_valid', $akismet_key_state, 10 * MINUTE_IN_SECONDS );
6901
		}
6902
6903
		$status = 'valid' === $akismet_key_state;
6904
6905
		return $status;
6906
	}
6907
6908
	/**
6909
	 * @deprecated
6910
	 *
6911
	 * @see Automattic\Jetpack\Sync\Modules\Users::is_function_in_backtrace
6912
	 */
6913
	public static function is_function_in_backtrace() {
6914
		_deprecated_function( __METHOD__, 'jetpack-7.6.0' );
6915
	}
6916
6917
	/**
6918
	 * Given a minified path, and a non-minified path, will return
6919
	 * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy.
6920
	 *
6921
	 * Both `$min_base` and `$non_min_base` are expected to be relative to the
6922
	 * root Jetpack directory.
6923
	 *
6924
	 * @since 5.6.0
6925
	 *
6926
	 * @param string $min_path
6927
	 * @param string $non_min_path
6928
	 * @return string The URL to the file
6929
	 */
6930
	public static function get_file_url_for_environment( $min_path, $non_min_path ) {
6931
		return Assets::get_file_url_for_environment( $min_path, $non_min_path );
6932
	}
6933
6934
	/**
6935
	 * Checks for whether Jetpack Backup & Scan is enabled.
6936
	 * Will return true if the state of Backup & Scan is anything except "unavailable".
6937
	 *
6938
	 * @return bool|int|mixed
6939
	 */
6940
	public static function is_rewind_enabled() {
6941
		if ( ! self::is_active() ) {
6942
			return false;
6943
		}
6944
6945
		$rewind_enabled = get_transient( 'jetpack_rewind_enabled' );
6946
		if ( false === $rewind_enabled ) {
6947
			jetpack_require_lib( 'class.core-rest-api-endpoints' );
6948
			$rewind_data    = (array) Jetpack_Core_Json_Api_Endpoints::rewind_data();
6949
			$rewind_enabled = ( ! is_wp_error( $rewind_data )
6950
				&& ! empty( $rewind_data['state'] )
6951
				&& 'active' === $rewind_data['state'] )
6952
				? 1
6953
				: 0;
6954
6955
			set_transient( 'jetpack_rewind_enabled', $rewind_enabled, 10 * MINUTE_IN_SECONDS );
6956
		}
6957
		return $rewind_enabled;
6958
	}
6959
6960
	/**
6961
	 * Return Calypso environment value; used for developing Jetpack and pairing
6962
	 * it with different Calypso enrionments, such as localhost.
6963
	 *
6964
	 * @since 7.4.0
6965
	 *
6966
	 * @return string Calypso environment
6967
	 */
6968
	public static function get_calypso_env() {
6969
		if ( isset( $_GET['calypso_env'] ) ) {
6970
			return sanitize_key( $_GET['calypso_env'] );
6971
		}
6972
6973
		if ( getenv( 'CALYPSO_ENV' ) ) {
6974
			return sanitize_key( getenv( 'CALYPSO_ENV' ) );
6975
		}
6976
6977
		if ( defined( 'CALYPSO_ENV' ) && CALYPSO_ENV ) {
6978
			return sanitize_key( CALYPSO_ENV );
6979
		}
6980
6981
		return '';
6982
	}
6983
6984
	/**
6985
	 * Checks whether or not TOS has been agreed upon.
6986
	 * Will return true if a user has clicked to register, or is already connected.
6987
	 */
6988
	public static function jetpack_tos_agreed() {
6989
		return Jetpack_Options::get_option( 'tos_agreed' ) || self::is_active();
6990
	}
6991
6992
	/**
6993
	 * Handles activating default modules as well general cleanup for the new connection.
6994
	 *
6995
	 * @param boolean $activate_sso                 Whether to activate the SSO module when activating default modules.
6996
	 * @param boolean $redirect_on_activation_error Whether to redirect on activation error.
6997
	 * @param boolean $send_state_messages          Whether to send state messages.
6998
	 * @return void
6999
	 */
7000
	public static function handle_post_authorization_actions(
7001
		$activate_sso = false,
7002
		$redirect_on_activation_error = false,
7003
		$send_state_messages = true
7004
	) {
7005
		$other_modules = $activate_sso
7006
			? array( 'sso' )
7007
			: array();
7008
7009
		if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) {
7010
			self::delete_active_modules();
7011
7012
			self::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), $redirect_on_activation_error, $send_state_messages );
7013
		} else {
7014
			self::activate_default_modules( false, false, $other_modules, $redirect_on_activation_error, $send_state_messages );
7015
		}
7016
7017
		// Since this is a fresh connection, be sure to clear out IDC options
7018
		Jetpack_IDC::clear_all_idc_options();
7019
		Jetpack_Options::delete_raw_option( 'jetpack_last_connect_url_check' );
7020
7021
		// Start nonce cleaner
7022
		wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
7023
		wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
7024
7025
		if ( $send_state_messages ) {
7026
			self::state( 'message', 'authorized' );
7027
		}
7028
	}
7029
7030
	/**
7031
	 * Returns a boolean for whether backups UI should be displayed or not.
7032
	 *
7033
	 * @return bool Should backups UI be displayed?
7034
	 */
7035
	public static function show_backups_ui() {
7036
		/**
7037
		 * Whether UI for backups should be displayed.
7038
		 *
7039
		 * @since 6.5.0
7040
		 *
7041
		 * @param bool $show_backups Should UI for backups be displayed? True by default.
7042
		 */
7043
		return self::is_plugin_active( 'vaultpress/vaultpress.php' ) || apply_filters( 'jetpack_show_backups', true );
7044
	}
7045
7046
	/*
7047
	 * Deprecated manage functions
7048
	 */
7049
	function prepare_manage_jetpack_notice() {
7050
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7051
	}
7052
	function manage_activate_screen() {
7053
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7054
	}
7055
	function admin_jetpack_manage_notice() {
7056
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7057
	}
7058
	function opt_out_jetpack_manage_url() {
7059
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7060
	}
7061
	function opt_in_jetpack_manage_url() {
7062
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7063
	}
7064
	function opt_in_jetpack_manage_notice() {
7065
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7066
	}
7067
	function can_display_jetpack_manage_notice() {
7068
		_deprecated_function( __METHOD__, 'jetpack-7.3' );
7069
	}
7070
7071
	/**
7072
	 * Clean leftoveruser meta.
7073
	 *
7074
	 * Delete Jetpack-related user meta when it is no longer needed.
7075
	 *
7076
	 * @since 7.3.0
7077
	 *
7078
	 * @param int $user_id User ID being updated.
7079
	 */
7080
	public static function user_meta_cleanup( $user_id ) {
7081
		$meta_keys = array(
7082
			// AtD removed from Jetpack 7.3
7083
			'AtD_options',
7084
			'AtD_check_when',
7085
			'AtD_guess_lang',
7086
			'AtD_ignored_phrases',
7087
		);
7088
7089
		foreach ( $meta_keys as $meta_key ) {
7090
			if ( get_user_meta( $user_id, $meta_key ) ) {
7091
				delete_user_meta( $user_id, $meta_key );
7092
			}
7093
		}
7094
	}
7095
7096
	function is_active_and_not_development_mode( $maybe ) {
7097
		if ( ! self::is_active() || self::is_development_mode() ) {
7098
			return false;
7099
		}
7100
		return true;
7101
	}
7102
}
7103