Completed
Push — add/sync-action ( 45ef1e...ba7a9e )
by
unknown
65:55 queued 56:50
created

class.jetpack.php (10 issues)

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
/*
4
Options:
5
jetpack_options (array)
6
	An array of options.
7
	@see Jetpack_Options::get_option_names()
8
9
jetpack_register (string)
10
	Temporary verification secrets.
11
12
jetpack_activated (int)
13
	1: the plugin was activated normally
14
	2: the plugin was activated on this site because of a network-wide activation
15
	3: the plugin was auto-installed
16
	4: the plugin was manually disconnected (but is still installed)
17
18
jetpack_active_modules (array)
19
	Array of active module slugs.
20
21
jetpack_do_activate (bool)
22
	Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
23
*/
24
25
class Jetpack {
26
	public $xmlrpc_server = null;
27
28
	private $xmlrpc_verification = null;
29
30
	public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA']
31
32
	/**
33
	 * @var array The handles of styles that are concatenated into jetpack.css
34
	 */
35
	public $concatenated_style_handles = array(
36
		'jetpack-carousel',
37
		'grunion.css',
38
		'the-neverending-homepage',
39
		'jetpack_likes',
40
		'jetpack_related-posts',
41
		'sharedaddy',
42
		'jetpack-slideshow',
43
		'presentations',
44
		'jetpack-subscriptions',
45
		'tiled-gallery',
46
		'widget-conditions',
47
		'jetpack_display_posts_widget',
48
		'gravatar-profile-widget',
49
		'widget-grid-and-list',
50
		'jetpack-widgets',
51
		'goodreads-widget',
52
		'jetpack_social_media_icons_widget',
53
	);
54
55
	public $plugins_to_deactivate = array(
56
		'stats'               => array( 'stats/stats.php', 'WordPress.com Stats' ),
57
		'shortlinks'          => array( 'stats/stats.php', 'WordPress.com Stats' ),
58
		'sharedaddy'          => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
59
		'twitter-widget'      => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
60
		'after-the-deadline'  => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ),
61
		'contact-form'        => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
62
		'contact-form'        => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
63
		'custom-css'          => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
64
		'random-redirect'     => array( 'random-redirect/random-redirect.php', 'Random Redirect' ),
65
		'videopress'          => array( 'video/video.php', 'VideoPress' ),
66
		'widget-visibility'   => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),
67
		'widget-visibility'   => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),
68
		'sharedaddy'          => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),
69
		'omnisearch'          => array( 'jetpack-omnisearch/omnisearch.php', 'Jetpack Omnisearch' ),
70
		'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
71
		'latex'               => array( 'wp-latex/wp-latex.php', 'WP LaTeX' )
72
	);
73
74
	public $capability_translations = array(
75
		'administrator' => 'manage_options',
76
		'editor'        => 'edit_others_posts',
77
		'author'        => 'publish_posts',
78
		'contributor'   => 'edit_posts',
79
		'subscriber'    => 'read',
80
	);
81
82
	/**
83
	 * Map of modules that have conflicts with plugins and should not be auto-activated
84
	 * if the plugins are active.  Used by filter_default_modules
85
	 *
86
	 * Plugin Authors: If you'd like to prevent a single module from auto-activating,
87
	 * change `module-slug` and add this to your plugin:
88
	 *
89
	 * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' );
90
	 * function my_jetpack_get_default_modules( $modules ) {
91
	 *     return array_diff( $modules, array( 'module-slug' ) );
92
	 * }
93
	 *
94
	 * @var array
95
	 */
96
	private $conflicting_plugins = array(
97
		'comments'          => array(
98
			'Intense Debate'                       => 'intensedebate/intensedebate.php',
99
			'Disqus'                               => 'disqus-comment-system/disqus.php',
100
			'Livefyre'                             => 'livefyre-comments/livefyre.php',
101
			'Comments Evolved for WordPress'       => 'gplus-comments/comments-evolved.php',
102
			'Google+ Comments'                     => 'google-plus-comments/google-plus-comments.php',
103
			'WP-SpamShield Anti-Spam'              => 'wp-spamshield/wp-spamshield.php',
104
		),
105
		'contact-form'      => array(
106
			'Contact Form 7'                       => 'contact-form-7/wp-contact-form-7.php',
107
			'Gravity Forms'                        => 'gravityforms/gravityforms.php',
108
			'Contact Form Plugin'                  => 'contact-form-plugin/contact_form.php',
109
			'Easy Contact Forms'                   => 'easy-contact-forms/easy-contact-forms.php',
110
			'Fast Secure Contact Form'             => 'si-contact-form/si-contact-form.php',
111
		),
112
		'minileven'         => array(
113
			'WPtouch'                              => 'wptouch/wptouch.php',
114
		),
115
		'latex'             => array(
116
			'LaTeX for WordPress'                  => 'latex/latex.php',
117
			'Youngwhans Simple Latex'              => 'youngwhans-simple-latex/yw-latex.php',
118
			'Easy WP LaTeX'                        => 'easy-wp-latex-lite/easy-wp-latex-lite.php',
119
			'MathJax-LaTeX'                        => 'mathjax-latex/mathjax-latex.php',
120
			'Enable Latex'                         => 'enable-latex/enable-latex.php',
121
			'WP QuickLaTeX'                        => 'wp-quicklatex/wp-quicklatex.php',
122
		),
123
		'protect'           => array(
124
			'Limit Login Attempts'                 => 'limit-login-attempts/limit-login-attempts.php',
125
			'Captcha'                              => 'captcha/captcha.php',
126
			'Brute Force Login Protection'         => 'brute-force-login-protection/brute-force-login-protection.php',
127
			'Login Security Solution'              => 'login-security-solution/login-security-solution.php',
128
			'WPSecureOps Brute Force Protect'      => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php',
129
			'BulletProof Security'                 => 'bulletproof-security/bulletproof-security.php',
130
			'SiteGuard WP Plugin'                  => 'siteguard/siteguard.php',
131
			'Security-protection'                  => 'security-protection/security-protection.php',
132
			'Login Security'                       => 'login-security/login-security.php',
133
			'Botnet Attack Blocker'                => 'botnet-attack-blocker/botnet-attack-blocker.php',
134
			'Wordfence Security'                   => 'wordfence/wordfence.php',
135
			'All In One WP Security & Firewall'    => 'all-in-one-wp-security-and-firewall/wp-security.php',
136
			'iThemes Security'                     => 'better-wp-security/better-wp-security.php',
137
		),
138
		'random-redirect'   => array(
139
			'Random Redirect 2'                    => 'random-redirect-2/random-redirect.php',
140
		),
141
		'related-posts'     => array(
142
			'YARPP'                                => 'yet-another-related-posts-plugin/yarpp.php',
143
			'WordPress Related Posts'              => 'wordpress-23-related-posts-plugin/wp_related_posts.php',
144
			'nrelate Related Content'              => 'nrelate-related-content/nrelate-related.php',
145
			'Contextual Related Posts'             => 'contextual-related-posts/contextual-related-posts.php',
146
			'Related Posts for WordPress'          => 'microkids-related-posts/microkids-related-posts.php',
147
			'outbrain'                             => 'outbrain/outbrain.php',
148
			'Shareaholic'                          => 'shareaholic/shareaholic.php',
149
			'Sexybookmarks'                        => 'sexybookmarks/shareaholic.php',
150
		),
151
		'sharedaddy'        => array(
152
			'AddThis'                              => 'addthis/addthis_social_widget.php',
153
			'Add To Any'                           => 'add-to-any/add-to-any.php',
154
			'ShareThis'                            => 'share-this/sharethis.php',
155
			'Shareaholic'                          => 'shareaholic/shareaholic.php',
156
		),
157
		'verification-tools' => array(
158
			'WordPress SEO by Yoast'               => 'wordpress-seo/wp-seo.php',
159
			'WordPress SEO Premium by Yoast'       => 'wordpress-seo-premium/wp-seo-premium.php',
160
			'All in One SEO Pack'                  => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
161
		),
162
		'widget-visibility' => array(
163
			'Widget Logic'                         => 'widget-logic/widget_logic.php',
164
			'Dynamic Widgets'                      => 'dynamic-widgets/dynamic-widgets.php',
165
		),
166
		'sitemaps' => array(
167
			'Google XML Sitemaps'                  => 'google-sitemap-generator/sitemap.php',
168
			'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php',
169
			'Google XML Sitemaps for qTranslate'   => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php',
170
			'XML Sitemap & Google News feeds'      => 'xml-sitemap-feed/xml-sitemap.php',
171
			'Google Sitemap by BestWebSoft'        => 'google-sitemap-plugin/google-sitemap-plugin.php',
172
			'WordPress SEO by Yoast'               => 'wordpress-seo/wp-seo.php',
173
			'WordPress SEO Premium by Yoast'       => 'wordpress-seo-premium/wp-seo-premium.php',
174
			'All in One SEO Pack'                  => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
175
			'Sitemap'                              => 'sitemap/sitemap.php',
176
			'Simple Wp Sitemap'                    => 'simple-wp-sitemap/simple-wp-sitemap.php',
177
			'Simple Sitemap'                       => 'simple-sitemap/simple-sitemap.php',
178
			'XML Sitemaps'                         => 'xml-sitemaps/xml-sitemaps.php',
179
			'MSM Sitemaps'                         => 'msm-sitemap/msm-sitemap.php',
180
		),
181
	);
182
183
	/**
184
	 * Plugins for which we turn off our Facebook OG Tags implementation.
185
	 *
186
	 * Note: WordPress SEO by Yoast and WordPress SEO Premium by Yoast automatically deactivate
187
	 * Jetpack's Open Graph tags via filter when their Social Meta modules are active.
188
	 *
189
	 * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter:
190
	 * add_filter( 'jetpack_enable_open_graph', '__return_false' );
191
	 */
192
	private $open_graph_conflicting_plugins = array(
193
		'2-click-socialmedia-buttons/2-click-socialmedia-buttons.php',
194
		                                                         // 2 Click Social Media Buttons
195
		'add-link-to-facebook/add-link-to-facebook.php',         // Add Link to Facebook
196
		'add-meta-tags/add-meta-tags.php',                       // Add Meta Tags
197
		'easy-facebook-share-thumbnails/esft.php',               // Easy Facebook Share Thumbnail
198
		'facebook/facebook.php',                                 // Facebook (official plugin)
199
		'facebook-awd/AWD_facebook.php',                         // Facebook AWD All in one
200
		'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php',
201
		                                                         // Facebook Featured Image & OG Meta Tags
202
		'facebook-meta-tags/facebook-metatags.php',              // Facebook Meta Tags
203
		'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php',
204
		                                                         // Facebook Open Graph Meta Tags for WordPress
205
		'facebook-revised-open-graph-meta-tag/index.php',        // Facebook Revised Open Graph Meta Tag
206
		'facebook-thumb-fixer/_facebook-thumb-fixer.php',        // Facebook Thumb Fixer
207
		'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php',
208
		                                                         // Fedmich's Facebook Open Graph Meta
209
		'header-footer/plugin.php',                              // Header and Footer
210
		'network-publisher/networkpub.php',                      // Network Publisher
211
		'nextgen-facebook/nextgen-facebook.php',                 // NextGEN Facebook OG
212
		'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php',
213
		                                                         // NextScripts SNAP
214
		'opengraph/opengraph.php',                               // Open Graph
215
		'open-graph-protocol-framework/open-graph-protocol-framework.php',
216
		                                                         // Open Graph Protocol Framework
217
		'seo-facebook-comments/seofacebook.php',                 // SEO Facebook Comments
218
		'seo-ultimate/seo-ultimate.php',                         // SEO Ultimate
219
		'sexybookmarks/sexy-bookmarks.php',                      // Shareaholic
220
		'shareaholic/sexy-bookmarks.php',                        // Shareaholic
221
		'sharepress/sharepress.php',                             // SharePress
222
		'simple-facebook-connect/sfc.php',                       // Simple Facebook Connect
223
		'social-discussions/social-discussions.php',             // Social Discussions
224
		'social-sharing-toolkit/social_sharing_toolkit.php',     // Social Sharing Toolkit
225
		'socialize/socialize.php',                               // Socialize
226
		'only-tweet-like-share-and-google-1/tweet-like-plusone.php',
227
		                                                         // Tweet, Like, Google +1 and Share
228
		'wordbooker/wordbooker.php',                             // Wordbooker
229
		'wpsso/wpsso.php',                                       // WordPress Social Sharing Optimization
230
		'wp-caregiver/wp-caregiver.php',                         // WP Caregiver
231
		'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php',
232
		                                                         // WP Facebook Like Send & Open Graph Meta
233
		'wp-facebook-open-graph-protocol/wp-facebook-ogp.php',   // WP Facebook Open Graph protocol
234
		'wp-ogp/wp-ogp.php',                                     // WP-OGP
235
		'zoltonorg-social-plugin/zosp.php',                      // Zolton.org Social Plugin
236
		'wp-fb-share-like-button/wp_fb_share-like_widget.php'    // WP Facebook Like Button
237
	);
238
239
	/**
240
	 * Plugins for which we turn off our Twitter Cards Tags implementation.
241
	 */
242
	private $twitter_cards_conflicting_plugins = array(
243
	//	'twitter/twitter.php',                       // The official one handles this on its own.
244
	//	                                             // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php
245
		'eewee-twitter-card/index.php',              // Eewee Twitter Card
246
		'ig-twitter-cards/ig-twitter-cards.php',     // IG:Twitter Cards
247
		'jm-twitter-cards/jm-twitter-cards.php',     // JM Twitter Cards
248
		'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php',
249
		                                             // Pure Web Brilliant's Social Graph Twitter Cards Extension
250
		'twitter-cards/twitter-cards.php',           // Twitter Cards
251
		'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta
252
		'wp-twitter-cards/twitter_cards.php',        // WP Twitter Cards
253
	);
254
255
	/**
256
	 * Message to display in admin_notice
257
	 * @var string
258
	 */
259
	public $message = '';
260
261
	/**
262
	 * Error to display in admin_notice
263
	 * @var string
264
	 */
265
	public $error = '';
266
267
	/**
268
	 * Modules that need more privacy description.
269
	 * @var string
270
	 */
271
	public $privacy_checks = '';
272
273
	/**
274
	 * Stats to record once the page loads
275
	 *
276
	 * @var array
277
	 */
278
	public $stats = array();
279
280
	/**
281
	 * Allows us to build a temporary security report
282
	 *
283
	 * @var array
284
	 */
285
	static $security_report = array();
286
287
	/**
288
	 * Jetpack_Sync object
289
	 */
290
	public $sync;
291
292
	/**
293
	 * Verified data for JSON authorization request
294
	 */
295
	public $json_api_authorization_request = array();
296
297
	/**
298
	 * Holds the singleton instance of this class
299
	 * @since 2.3.3
300
	 * @var Jetpack
301
	 */
302
	static $instance = false;
303
304
	/**
305
	 * Singleton
306
	 * @static
307
	 */
308
	public static function init() {
309
		if ( ! self::$instance ) {
310
			if ( did_action( 'plugins_loaded' ) )
311
				self::plugin_textdomain();
312
			else
313
				add_action( 'plugins_loaded', array( __CLASS__, 'plugin_textdomain' ), 99 );
314
315
			self::$instance = new Jetpack;
316
317
			self::$instance->plugin_upgrade();
318
319
			add_action( 'init', array( __CLASS__, 'perform_security_reporting' ) );
320
321
		}
322
323
		return self::$instance;
324
	}
325
326
	/**
327
	 * Must never be called statically
328
	 */
329
	function plugin_upgrade() {
330
		if ( Jetpack::is_active() ) {
331
			list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
332
			if ( JETPACK__VERSION != $version ) {
333
334
				// Check which active modules actually exist and remove others from active_modules list
335
				$unfiltered_modules = Jetpack::get_active_modules();
336
				$modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) );
337
				if ( array_diff( $unfiltered_modules, $modules ) ) {
338
					Jetpack_Options::update_option( 'active_modules', $modules );
339
				}
340
341
				add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
342
				/**
343
				 * Fires when synchronizing all registered options and constants.
344
				 *
345
				 * @since 3.3.0
346
				 */
347
				do_action( 'jetpack_sync_all_registered_options' );
348
			}
349
		}
350
	}
351
352
	static function activate_manage( ) {
353
354
		if ( did_action( 'init' ) || current_filter() == 'init' ) {
355
			self::activate_module( 'manage', false, false );
356
		} else if ( !  has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) {
357
			add_action( 'init', array( __CLASS__, 'activate_manage' ) );
358
		}
359
360
	}
361
362
	/**
363
	 * Constructor.  Initializes WordPress hooks
364
	 */
365
	private function __construct() {
366
		/*
367
		 * Check for and alert any deprecated hooks
368
		 */
369
		add_action( 'init', array( $this, 'deprecated_hooks' ) );
370
371
		/**
372
		 * Trigger a wp_version sync when updating WP versions
373
		 **/
374
		add_action( 'upgrader_process_complete', array( 'Jetpack', 'update_get_wp_version' ), 10, 2 );
375
376
		/*
377
		 * Load things that should only be in Network Admin.
378
		 *
379
		 * For now blow away everything else until a more full
380
		 * understanding of what is needed at the network level is
381
		 * available
382
		 */
383
		if( is_multisite() ) {
384
			Jetpack_Network::init();
385
		}
386
387
		// Unlink user before deleting the user from .com
388
		add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 );
389
		add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 );
390
391
		if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
392
			@ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed.
393
394
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
395
			$this->xmlrpc_server = new Jetpack_XMLRPC_Server();
396
397
			$this->require_jetpack_authentication();
398
399
			if ( Jetpack::is_active() ) {
400
				// Hack to preserve $HTTP_RAW_POST_DATA
401
				add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
402
403
				$signed = $this->verify_xml_rpc_signature();
404
				if ( $signed && ! is_wp_error( $signed ) ) {
405
					// The actual API methods.
406
					add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) );
407
				} else {
408
					// The jetpack.authorize method should be available for unauthenticated users on a site with an
409
					// active Jetpack connection, so that additional users can link their account.
410
					add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) );
411
				}
412
			} else {
413
				// The bootstrap API methods.
414
				add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) );
415
			}
416
417
			// Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on.
418
			add_filter( 'pre_option_enable_xmlrpc', '__return_true' );
419
		} elseif ( is_admin() && isset( $_POST['action'] ) && 'jetpack_upload_file' == $_POST['action'] ) {
420
			$this->require_jetpack_authentication();
421
			$this->add_remote_request_handlers();
422
		} else {
423
			if ( Jetpack::is_active() ) {
424
				add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) );
425
				add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) );
426
			}
427
		}
428
429
		if ( Jetpack::is_active() ) {
430
			Jetpack_Heartbeat::init();
431
		}
432
433
		add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) );
434
		if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) {
435
			wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
436
		}
437
438
		add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) );
439
440
		add_action( 'admin_init', array( $this, 'admin_init' ) );
441
		add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
442
443
		add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
444
445
		add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
446
		// Filter the dashboard meta box order to swap the new one in in place of the old one.
447
		add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) );
448
449
		add_action( 'wp_ajax_jetpack-sync-reindex-trigger', array( $this, 'sync_reindex_trigger' ) );
450
		add_action( 'wp_ajax_jetpack-sync-reindex-status', array( $this, 'sync_reindex_status' ) );
451
452
		// If any module option is updated before Jump Start is dismissed, hide Jump Start.
453
		add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) );
454
455
		// Identity Crisis AJAX callback function
456
		add_action( 'wp_ajax_jetpack_resolve_identity_crisis', array( $this, 'resolve_identity_crisis_ajax_callback' ) );
457
458
		// JITM AJAX callback function
459
		add_action( 'wp_ajax_jitm_ajax',  array( $this, 'jetpack_jitm_ajax_callback' ) );
460
461
		add_action( 'wp_ajax_jetpack_admin_ajax',          array( $this, 'jetpack_admin_ajax_callback' ) );
462
		add_action( 'wp_ajax_jetpack_admin_ajax_refresh',  array( $this, 'jetpack_admin_ajax_refresh_data' ) );
463
464
		// Universal ajax callback for all tracking events triggered via js
465
		add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) );
466
467
		add_action( 'wp_loaded', array( $this, 'register_assets' ) );
468
		add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
469
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
470
		add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
471
472
		add_action( 'jetpack_activate_module', array( $this, 'activate_module_actions' ) );
473
474
		add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 );
475
476
		add_action( 'jetpack_notices', array( $this, 'show_development_mode_notice' ) );
477
478
		/**
479
		 * These actions run checks to load additional files.
480
		 * They check for external files or plugins, so they need to run as late as possible.
481
		 */
482
		add_action( 'wp_head', array( $this, 'check_open_graph' ),       1 );
483
		add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ),     999 );
484
		add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
485
486
		add_filter( 'plugins_url',      array( 'Jetpack', 'maybe_min_asset' ),     1, 3 );
487
		add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 );
488
489
		add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 );
490
491
		add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
492
		add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
493
494
		// A filter to control all just in time messages
495
		add_filter( 'jetpack_just_in_time_msgs', '__return_false' );
496
497
		/**
498
		 * This is the hack to concatinate all css files into one.
499
		 * For description and reasoning see the implode_frontend_css method
500
		 *
501
		 * Super late priority so we catch all the registered styles
502
		 */
503
		if( !is_admin() ) {
504
			add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first
505
			add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles`
506
		}
507
508
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
509
		add_action( 'add_option_site_icon',    array( $this, 'jetpack_sync_core_icon' ) );
510
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
511
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
512
		add_action( 'jetpack_heartbeat',       array( $this, 'jetpack_sync_core_icon' ) );
513
514
	}
515
516
	/*
517
	 * Make sure any site icon added to core can get
518
	 * synced back to dotcom, so we can display it there.
519
	 */
520 View Code Duplication
	function jetpack_sync_core_icon() {
521
		if ( function_exists( 'get_site_icon_url' ) ) {
522
			$url = get_site_icon_url();
523
		} else {
524
			return;
525
		}
526
527
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
528
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
529
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
530
			// This is the option that is synced with dotcom
531
			Jetpack_Options::update_option( 'site_icon_url', $url );
532
		} else if ( empty( $url ) && did_action( 'delete_option_site_icon' ) ) {
533
			Jetpack_Options::delete_option( 'site_icon_url' );
534
		}
535
	}
536
537
	function jetpack_admin_ajax_tracks_callback() {
538
		// Check for nonce
539
		if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) {
540
			wp_die( 'Permissions check failed.' );
541
		}
542
543
		if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] )  ) {
544
			wp_die( 'No valid event name or type.' );
545
		}
546
547
		$tracks_data = array();
548
		if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) {
549
			$tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] );
550
		}
551
552
		JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data );
553
		wp_send_json_success();
554
		wp_die();
555
	}
556
557
	function jetpack_admin_ajax_callback() {
558
		// Check for nonce
559 View Code Duplication
		if ( ! isset( $_REQUEST['adminNonce'] ) || ! wp_verify_nonce( $_REQUEST['adminNonce'], 'jetpack-admin-nonce' ) || ! current_user_can( 'jetpack_manage_modules' ) ) {
560
			wp_die( 'permissions check failed' );
561
		}
562
563
		if ( isset( $_REQUEST['toggleModule'] ) && 'nux-toggle-module' == $_REQUEST['toggleModule'] ) {
564
			$slug = $_REQUEST['thisModuleSlug'];
565
566
			if ( ! in_array( $slug, Jetpack::get_available_modules() ) ) {
567
				wp_die( 'That is not a Jetpack module slug' );
568
			}
569
570
			if ( Jetpack::is_module_active( $slug ) ) {
571
				Jetpack::deactivate_module( $slug );
572
			} else {
573
				Jetpack::activate_module( $slug, false, false );
574
			}
575
576
			$modules = Jetpack_Admin::init()->get_modules();
577
			echo json_encode( $modules[ $slug ] );
578
579
			exit;
580
		}
581
582
		wp_die();
583
	}
584
585
	/*
586
	 * Sometimes we need to refresh the data,
587
	 * especially if the page is visited via a 'history'
588
	 * event like back/forward
589
	 */
590
	function jetpack_admin_ajax_refresh_data() {
591
		// Check for nonce
592 View Code Duplication
		if ( ! isset( $_REQUEST['adminNonce'] ) || ! wp_verify_nonce( $_REQUEST['adminNonce'], 'jetpack-admin-nonce' ) ) {
593
			wp_die( 'permissions check failed' );
594
		}
595
596
		if ( isset( $_REQUEST['refreshData'] ) && 'refresh' == $_REQUEST['refreshData'] ) {
597
			$modules = Jetpack_Admin::init()->get_modules();
598
			echo json_encode( $modules );
599
			exit;
600
		}
601
602
		wp_die();
603
	}
604
605
	/**
606
	 * The callback for the JITM ajax requests.
607
	 */
608
	function jetpack_jitm_ajax_callback() {
609
		// Check for nonce
610
		if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) {
611
			wp_die( 'Module activation failed due to lack of appropriate permissions' );
612
		}
613
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) {
614
			$module_slug = $_REQUEST['jitmModule'];
615
			Jetpack::log( 'activate', $module_slug );
616
			Jetpack::activate_module( $module_slug, false, false );
617
			Jetpack::state( 'message', 'no_message' );
618
619
			//A Jetpack module is being activated through a JITM, track it
620
			$this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION );
621
			$this->do_stats( 'server_side' );
622
623
			wp_send_json_success();
624
		}
625
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) {
626
			// get the hide_jitm options array
627
			$jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
628
			$module_slug = $_REQUEST['jitmModule'];
629
630
			if( ! $jetpack_hide_jitm ) {
631
				$jetpack_hide_jitm = array(
632
					$module_slug => 'hide'
633
				);
634
			} else {
635
				$jetpack_hide_jitm[$module_slug] = 'hide';
636
			}
637
638
			Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm );
639
640
			//jitm is being dismissed forever, track it
641
			$this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION );
642
			$this->do_stats( 'server_side' );
643
644
			wp_send_json_success();
645
		}
646 View Code Duplication
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) {
647
			$module_slug = $_REQUEST['jitmModule'];
648
649
			// User went to WordPress.com, track this
650
			$this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION );
651
			$this->do_stats( 'server_side' );
652
653
			wp_send_json_success();
654
		}
655 View Code Duplication
		if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) {
656
			$track = $_REQUEST['jitmModule'];
657
658
			// User is viewing JITM, track it.
659
			$this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION );
660
			$this->do_stats( 'server_side' );
661
662
			wp_send_json_success();
663
		}
664
	}
665
666
	/**
667
	 * If there are any stats that need to be pushed, but haven't been, push them now.
668
	 */
669
	function __destruct() {
670
		if ( ! empty( $this->stats ) ) {
671
			$this->do_stats( 'server_side' );
672
		}
673
	}
674
675
	function jetpack_custom_caps( $caps, $cap, $user_id, $args ) {
676
		switch( $cap ) {
677
			case 'jetpack_connect' :
678
			case 'jetpack_reconnect' :
679
				if ( Jetpack::is_development_mode() ) {
680
					$caps = array( 'do_not_allow' );
681
					break;
682
				}
683
				/**
684
				 * Pass through. If it's not development mode, these should match disconnect.
685
				 * Let users disconnect if it's development mode, just in case things glitch.
686
				 */
687
			case 'jetpack_disconnect' :
688
				/**
689
				 * In multisite, can individual site admins manage their own connection?
690
				 *
691
				 * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class.
692
				 */
693
				if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
694
					if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) {
695
						/**
696
						 * We need to update the option name -- it's terribly unclear which
697
						 * direction the override goes.
698
						 *
699
						 * @todo: Update the option name to `sub-sites-can-manage-own-connections`
700
						 */
701
						$caps = array( 'do_not_allow' );
702
						break;
703
					}
704
				}
705
706
				$caps = array( 'manage_options' );
707
				break;
708
			case 'jetpack_manage_modules' :
709
			case 'jetpack_activate_modules' :
710
			case 'jetpack_deactivate_modules' :
711
				$caps = array( 'manage_options' );
712
				break;
713
			case 'jetpack_configure_modules' :
714
				$caps = array( 'manage_options' );
715
				break;
716
			case 'jetpack_network_admin_page':
717
			case 'jetpack_network_settings_page':
718
				$caps = array( 'manage_network_plugins' );
719
				break;
720
			case 'jetpack_network_sites_page':
721
				$caps = array( 'manage_sites' );
722
				break;
723
			case 'jetpack_admin_page' :
724
				if ( Jetpack::is_development_mode() ) {
725
					$caps = array( 'manage_options' );
726
					break;
727
				}
728
729
				// Don't ever show to subscribers, but allow access to the page if they're trying to unlink.
730
				if ( ! current_user_can( 'edit_posts' ) ) {
731
					if ( isset( $_GET['redirect'] ) && 'sub-unlink' == $_GET['redirect'] ) {
732
						// We need this in order to unlink the user.
733
						$this->admin_page_load();
734
					}
735
					if ( ! wp_verify_nonce( 'jetpack-unlink' ) ) {
736
						$caps = array( 'do_not_allow' );
737
						break;
738
					}
739
				}
740
741
				if ( ! self::is_active() && ! current_user_can( 'jetpack_connect' ) ) {
742
					$caps = array( 'do_not_allow' );
743
					break;
744
				}
745
				/**
746
				 * Pass through. If it's not development mode, these should match the admin page.
747
				 * Let users disconnect if it's development mode, just in case things glitch.
748
				 */
749
			case 'jetpack_connect_user' :
750
				if ( Jetpack::is_development_mode() ) {
751
					$caps = array( 'do_not_allow' );
752
					break;
753
				}
754
				$caps = array( 'read' );
755
				break;
756
		}
757
		return $caps;
758
	}
759
760
	function require_jetpack_authentication() {
761
		// Don't let anyone authenticate
762
		$_COOKIE = array();
763
		remove_all_filters( 'authenticate' );
764
765
		/**
766
		 * For the moment, remove Limit Login Attempts if its xmlrpc for Jetpack.
767
		 * If Limit Login Attempts is installed as a mu-plugin, it can occasionally
768
		 * generate false-positives.
769
		 */
770
		remove_filter( 'wp_login_failed', 'limit_login_failed' );
771
772
		if ( Jetpack::is_active() ) {
773
			// Allow Jetpack authentication
774
			add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 );
775
		}
776
	}
777
778
	/**
779
	 * Load language files
780
	 */
781
	public static function plugin_textdomain() {
782
		// Note to self, the third argument must not be hardcoded, to account for relocated folders.
783
		load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' );
784
	}
785
786
	/**
787
	 * Register assets for use in various modules and the Jetpack admin page.
788
	 *
789
	 * @uses wp_script_is, wp_register_script, plugins_url
790
	 * @action wp_loaded
791
	 * @return null
792
	 */
793
	public function register_assets() {
794
		if ( ! wp_script_is( 'spin', 'registered' ) ) {
795
			wp_register_script( 'spin', plugins_url( '_inc/spin.js', JETPACK__PLUGIN_FILE ), false, '1.3' );
796
		}
797
798 View Code Duplication
		if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) {
799
			wp_register_script( 'jquery.spin', plugins_url( '_inc/jquery.spin.js', JETPACK__PLUGIN_FILE ) , array( 'jquery', 'spin' ), '1.3' );
800
		}
801
802 View Code Duplication
		if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
803
			wp_register_script( 'jetpack-gallery-settings', plugins_url( '_inc/gallery-settings.js', JETPACK__PLUGIN_FILE ), array( 'media-views' ), '20121225' );
804
		}
805
806 View Code Duplication
		if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) {
807
			wp_register_script( 'jetpack-twitter-timeline', plugins_url( '_inc/twitter-timeline.js', JETPACK__PLUGIN_FILE ) , array( 'jquery' ), '4.0.0', true );
808
		}
809
810
		if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) {
811
			wp_register_script( 'jetpack-facebook-embed', plugins_url( '_inc/facebook-embed.js', __FILE__ ), array( 'jquery' ), null, true );
812
813
			/** This filter is documented in modules/sharedaddy/sharing-sources.php */
814
			$fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
815
			if ( ! is_numeric( $fb_app_id ) ) {
816
				$fb_app_id = '';
817
			}
818
			wp_localize_script(
819
				'jetpack-facebook-embed',
820
				'jpfbembed',
821
				array(
822
					'appid' => $fb_app_id,
823
					'locale' => $this->get_locale(),
824
				)
825
			);
826
		}
827
828
		/**
829
		 * As jetpack_register_genericons is by default fired off a hook,
830
		 * the hook may have already fired by this point.
831
		 * So, let's just trigger it manually.
832
		 */
833
		require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' );
834
		jetpack_register_genericons();
835
836 View Code Duplication
		if ( ! wp_style_is( 'jetpack-icons', 'registered' ) )
837
			wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION );
838
	}
839
840
	/**
841
	 * Guess locale from language code.
842
	 *
843
	 * @param string $lang Language code.
844
	 * @return string|bool
845
	 */
846 View Code Duplication
	function guess_locale_from_lang( $lang ) {
847
		if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) {
848
			return 'en_US';
849
		}
850
851
		if ( ! class_exists( 'GP_Locales' ) ) {
852
			if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
853
				return false;
854
			}
855
856
			require JETPACK__GLOTPRESS_LOCALES_PATH;
857
		}
858
859
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
860
			// WP.com: get_locale() returns 'it'
861
			$locale = GP_Locales::by_slug( $lang );
862
		} else {
863
			// Jetpack: get_locale() returns 'it_IT';
864
			$locale = GP_Locales::by_field( 'facebook_locale', $lang );
865
		}
866
867
		if ( ! $locale ) {
868
			return false;
869
		}
870
871
		if ( empty( $locale->facebook_locale ) ) {
872
			if ( empty( $locale->wp_locale ) ) {
873
				return false;
874
			} else {
875
				// Facebook SDK is smart enough to fall back to en_US if a
876
				// locale isn't supported. Since supported Facebook locales
877
				// can fall out of sync, we'll attempt to use the known
878
				// wp_locale value and rely on said fallback.
879
				return $locale->wp_locale;
880
			}
881
		}
882
883
		return $locale->facebook_locale;
884
	}
885
886
	/**
887
	 * Get the locale.
888
	 *
889
	 * @return string|bool
890
	 */
891
	function get_locale() {
892
		$locale = $this->guess_locale_from_lang( get_locale() );
893
894
		if ( ! $locale ) {
895
			$locale = 'en_US';
896
		}
897
898
		return $locale;
899
	}
900
901
	/**
902
	 * Device Pixels support
903
	 * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers.
904
	 */
905
	function devicepx() {
906
		if ( Jetpack::is_active() ) {
907
			wp_enqueue_script( 'devicepx', set_url_scheme( 'http://s0.wp.com/wp-content/js/devicepx-jetpack.js' ), array(), gmdate( 'oW' ), true );
908
		}
909
	}
910
911
	/**
912
	 * Return the network_site_url so that .com knows what network this site is a part of.
913
	 * @param  bool $option
914
	 * @return string
915
	 */
916
	public function jetpack_main_network_site_option( $option ) {
917
		return network_site_url();
918
	}
919
	/**
920
	 * Network Name.
921
	 */
922
	static function network_name( $option = null ) {
923
		global $current_site;
924
		return $current_site->site_name;
925
	}
926
	/**
927
	 * Does the network allow new user and site registrations.
928
	 * @return string
929
	 */
930
	static function network_allow_new_registrations( $option = null ) {
931
		return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' );
932
	}
933
	/**
934
	 * Does the network allow admins to add new users.
935
	 * @return boolian
936
	 */
937
	static function network_add_new_users( $option = null ) {
938
		return (bool) get_site_option( 'add_new_users' );
939
	}
940
	/**
941
	 * File upload psace left per site in MB.
942
	 *  -1 means NO LIMIT.
943
	 * @return number
944
	 */
945
	static function network_site_upload_space( $option = null ) {
946
		// value in MB
947
		return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() );
948
	}
949
950
	/**
951
	 * Network allowed file types.
952
	 * @return string
953
	 */
954
	static function network_upload_file_types( $option = null ) {
955
		return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' );
956
	}
957
958
	/**
959
	 * Maximum file upload size set by the network.
960
	 * @return number
961
	 */
962
	static function network_max_upload_file_size( $option = null ) {
963
		// value in KB
964
		return get_site_option( 'fileupload_maxk', 300 );
965
	}
966
967
	/**
968
	 * Lets us know if a site allows admins to manage the network.
969
	 * @return array
970
	 */
971
	static function network_enable_administration_menus( $option = null ) {
972
		return get_site_option( 'menu_items' );
973
	}
974
975
	/**
976
	 * Return whether we are dealing with a multi network setup or not.
977
	 * The reason we are type casting this is because we want to avoid the situation where
978
	 * the result is false since when is_main_network_option return false it cases
979
	 * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the
980
	 * database which could be set to anything as opposed to what this function returns.
981
	 * @param  bool  $option
982
	 *
983
	 * @return boolean
984
	 */
985
	public function is_main_network_option( $option ) {
986
		// return '1' or ''
987
		return (string) (bool) Jetpack::is_multi_network();
988
	}
989
990
	/**
991
	 * Return true if we are with multi-site or multi-network false if we are dealing with single site.
992
	 *
993
	 * @param  string  $option
994
	 * @return boolean
995
	 */
996
	public function is_multisite( $option ) {
997
		return (string) (bool) is_multisite();
998
	}
999
1000
	/**
1001
	 * Implemented since there is no core is multi network function
1002
	 * Right now there is no way to tell if we which network is the dominant network on the system
1003
	 *
1004
	 * @since  3.3
1005
	 * @return boolean
1006
	 */
1007
	public static function is_multi_network() {
1008
		global  $wpdb;
1009
1010
		// if we don't have a multi site setup no need to do any more
1011
		if ( ! is_multisite() ) {
1012
			return false;
1013
		}
1014
1015
		$num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
1016
		if ( $num_sites > 1 ) {
1017
			return true;
1018
		} else {
1019
			return false;
1020
		}
1021
	}
1022
1023
	/**
1024
	 * Trigger an update to the main_network_site when we update the siteurl of a site.
1025
	 * @return null
1026
	 */
1027
	function update_jetpack_main_network_site_option() {
1028
		// do_action( 'add_option_$option', '$option', '$value-of-the-option' );
1029
		/**
1030
		 * Fires when the site URL is updated.
1031
		 * Determines if the site is the main site of a Mulitiste network.
1032
		 *
1033
		 * @since 3.3.0
1034
		 *
1035
		 * @param string jetpack_main_network_site.
1036
		 * @param string network_site_url() Site URL for the "main" site of the current Multisite network.
1037
		 */
1038
		do_action( 'add_option_jetpack_main_network_site', 'jetpack_main_network_site', network_site_url() );
1039
		/**
1040
		 * Fires when the site URL is updated.
1041
		 * Determines if the is part of a multi network.
1042
		 *
1043
		 * @since 3.3.0
1044
		 *
1045
		 * @param string jetpack_is_main_network.
1046
		 * @param bool Jetpack::is_multi_network() Is the site part of a multi network.
1047
		 */
1048
		do_action( 'add_option_jetpack_is_main_network', 'jetpack_is_main_network', (string) (bool) Jetpack::is_multi_network() );
1049
		/**
1050
		 * Fires when the site URL is updated.
1051
		 * Determines if the site is part of a multisite network.
1052
		 *
1053
		 * @since 3.4.0
1054
		 *
1055
		 * @param string jetpack_is_multi_site.
1056
		 * @param bool is_multisite() Is the site part of a mutlisite network.
1057
		 */
1058
		do_action( 'add_option_jetpack_is_multi_site', 'jetpack_is_multi_site', (string) (bool) is_multisite() );
1059
	}
1060
	/**
1061
	 * Triggered after a user updates the network settings via Network Settings Admin Page
1062
	 *
1063
	 */
1064
	function update_jetpack_network_settings() {
1065
		// Only sync this info for the main network site.
1066
		do_action( 'add_option_jetpack_network_name', 'jetpack_network_name', Jetpack::network_name() );
1067
		do_action( 'add_option_jetpack_network_allow_new_registrations', 'jetpack_network_allow_new_registrations', Jetpack::network_allow_new_registrations() );
1068
		do_action( 'add_option_jetpack_network_add_new_users', 'jetpack_network_add_new_users', Jetpack::network_add_new_users() );
1069
		do_action( 'add_option_jetpack_network_site_upload_space', 'jetpack_network_site_upload_space', Jetpack::network_site_upload_space() );
1070
		do_action( 'add_option_jetpack_network_upload_file_types', 'jetpack_network_upload_file_types', Jetpack::network_upload_file_types() );
1071
		do_action( 'add_option_jetpack_network_enable_administration_menus', 'jetpack_network_enable_administration_menus', Jetpack::network_enable_administration_menus() );
1072
1073
	}
1074
1075
	/**
1076
	 * Get back if the current site is single user site.
1077
	 *
1078
	 * @return bool
1079
	 */
1080
	public static function is_single_user_site() {
1081
1082
		$user_query = new WP_User_Query( array(
1083
			'blog_id' => get_current_blog_id(),
1084
			'fields'  => 'ID',
1085
			'number' => 2
1086
		) );
1087
		return 1 === (int) $user_query->get_total();
1088
	}
1089
1090
	/**
1091
	 * Returns true if the site has file write access false otherwise.
1092
	 * @return string ( '1' | '0' )
1093
	 **/
1094 View Code Duplication
	public static function file_system_write_access() {
1095
		if ( ! function_exists( 'get_filesystem_method' ) ) {
1096
			require_once( ABSPATH . 'wp-admin/includes/file.php' );
1097
		}
1098
1099
		require_once( ABSPATH . 'wp-admin/includes/template.php' );
1100
1101
		$filesystem_method = get_filesystem_method();
1102
		if ( $filesystem_method === 'direct' ) {
1103
			return 1;
1104
		}
1105
1106
		ob_start();
1107
		$filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
1108
		ob_end_clean();
1109
		if ( $filesystem_credentials_are_stored ) {
1110
			return 1;
1111
		}
1112
		return 0;
1113
	}
1114
1115
	/**
1116
	 * Finds out if a site is using a version control system.
1117
	 * @return string ( '1' | '0' )
1118
	 **/
1119 View Code Duplication
	public static function is_version_controlled() {
1120
1121
		if ( !class_exists( 'WP_Automatic_Updater' ) ) {
1122
			require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
1123
		}
1124
		$updater = new WP_Automatic_Updater();
1125
		$is_version_controlled = strval( $updater->is_vcs_checkout( $context = ABSPATH ) );
1126
		// transients should not be empty
1127
		if ( empty( $is_version_controlled ) ) {
1128
			$is_version_controlled = '0';
1129
		}
1130
		return $is_version_controlled;
1131
	}
1132
1133
	/**
1134
	 * Determines whether the current theme supports featured images or not.
1135
	 * @return string ( '1' | '0' )
1136
	 */
1137
	public static function featured_images_enabled() {
1138
		return current_theme_supports( 'post-thumbnails' ) ? '1' : '0';
1139
	}
1140
1141
	/*
1142
	 * Sync back wp_version
1143
	 */
1144
	public static function get_wp_version() {
1145
		global $wp_version;
1146
		return $wp_version;
1147
	}
1148
1149
	/**
1150
	 * Keeps wp_version in sync with .com when WordPress core updates
1151
	 **/
1152
	public static function update_get_wp_version( $update, $meta_data ) {
1153
		if ( 'update' === $meta_data['action'] && 'core' === $meta_data['type'] ) {
1154
			/** This action is documented in wp-includes/option.php */
1155
			/**
1156
			 * This triggers the sync for the jetpack version
1157
			 * See Jetpack_Sync options method for more info.
1158
			 */
1159
			do_action( 'add_option_jetpack_wp_version', 'jetpack_wp_version', (string) Jetpack::get_wp_version() );
1160
		}
1161
	}
1162
1163
	/**
1164
	 * jetpack_updates is saved in the following schema:
1165
	 *
1166
	 * array (
1167
	 *      'plugins'                       => (int) Number of plugin updates available.
1168
	 *      'themes'                        => (int) Number of theme updates available.
1169
	 *      'wordpress'                     => (int) Number of WordPress core updates available.
1170
	 *      'translations'                  => (int) Number of translation updates available.
1171
	 *      'total'                         => (int) Total of all available updates.
1172
	 *      'wp_update_version'             => (string) The latest available version of WordPress, only present if a WordPress update is needed.
1173
	 * )
1174
	 * @return array
1175
	 */
1176
	public static function get_updates() {
1177
		$update_data = wp_get_update_data();
1178
1179
		// Stores the individual update counts as well as the total count.
1180
		if ( isset( $update_data['counts'] ) ) {
1181
			$updates = $update_data['counts'];
1182
		}
1183
1184
		// If we need to update WordPress core, let's find the latest version number.
1185 View Code Duplication
		if ( ! empty( $updates['wordpress'] ) ) {
1186
			$cur = get_preferred_from_update_core();
1187
			if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
1188
				$updates['wp_update_version'] = $cur->current;
1189
			}
1190
		}
1191
		return isset( $updates ) ? $updates : array();
1192
	}
1193
1194
	public static function get_update_details() {
1195
		$update_details = array(
1196
			'update_core' => get_site_transient( 'update_core' ),
1197
			'update_plugins' => get_site_transient( 'update_plugins' ),
1198
			'update_themes' => get_site_transient( 'update_themes' ),
1199
		);
1200
		return $update_details;
1201
	}
1202
1203
	public static function refresh_update_data() {
1204
		if ( current_user_can( 'update_core' ) && current_user_can( 'update_plugins' ) && current_user_can( 'update_themes' ) ) {
1205
			/**
1206
			 * Fires whenever the amount of updates needed for a site changes.
1207
			 * Syncs an array that includes the number of theme, plugin, and core updates available, as well as the latest core version available.
1208
			 *
1209
			 * @since 3.7.0
1210
			 *
1211
			 * @param string jetpack_updates
1212
			 * @param array Update counts calculated by Jetpack::get_updates
1213
			 */
1214
			do_action( 'add_option_jetpack_updates', 'jetpack_updates', Jetpack::get_updates() );
1215
		}
1216
		/**
1217
		 * Fires whenever the amount of updates needed for a site changes.
1218
		 * Syncs an array of core, theme, and plugin data, and which of each is out of date
1219
		 *
1220
		 * @since 3.7.0
1221
		 *
1222
		 * @param string jetpack_update_details
1223
		 * @param array Update details calculated by Jetpack::get_update_details
1224
		 */
1225
		do_action( 'add_option_jetpack_update_details', 'jetpack_update_details', Jetpack::get_update_details() );
1226
	}
1227
1228
	public static function refresh_theme_data() {
1229
		/**
1230
		 * Fires whenever a theme change is made.
1231
		 *
1232
		 * @since 3.8.1
1233
		 *
1234
		 * @param string featured_images_enabled
1235
		 * @param boolean Whether featured images are enabled or not
1236
		 */
1237
		do_action( 'add_option_jetpack_featured_images_enabled', 'jetpack_featured_images_enabled', Jetpack::featured_images_enabled() );
1238
	}
1239
1240
	/**
1241
	 * Is Jetpack active?
1242
	 */
1243
	public static function is_active() {
1244
		return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
1245
	}
1246
1247
	/**
1248
	 * Is Jetpack in development (offline) mode?
1249
	 */
1250
	public static function is_development_mode() {
1251
		$development_mode = false;
1252
1253
		if ( defined( 'JETPACK_DEV_DEBUG' ) ) {
1254
			$development_mode = JETPACK_DEV_DEBUG;
1255
		}
1256
1257
		elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
1258
			$development_mode = true;
1259
		}
1260
		/**
1261
		 * Filters Jetpack's development mode.
1262
		 *
1263
		 * @see http://jetpack.com/support/development-mode/
1264
		 *
1265
		 * @since 2.2.1
1266
		 *
1267
		 * @param bool $development_mode Is Jetpack's development mode active.
1268
		 */
1269
		return apply_filters( 'jetpack_development_mode', $development_mode );
1270
	}
1271
1272
	/**
1273
	* Get Jetpack development mode notice text and notice class.
1274
	*
1275
	* Mirrors the checks made in Jetpack::is_development_mode
1276
	*
1277
	*/
1278
	public static function show_development_mode_notice() {
1279
		if ( Jetpack::is_development_mode() ) {
1280
			if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) {
1281
				$notice = sprintf(
1282
					/* translators: %s is a URL */
1283
					__( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ),
1284
					'http://jetpack.com/support/development-mode/'
1285
				);
1286
			} elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
1287
				$notice = sprintf(
1288
					/* translators: %s is a URL */
1289
					__( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ),
1290
					'http://jetpack.com/support/development-mode/'
1291
				);
1292
			} else {
1293
				$notice = sprintf(
1294
					/* translators: %s is a URL */
1295
					__( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ),
1296
					'http://jetpack.com/support/development-mode/'
1297
				);
1298
			}
1299
1300
			echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
1301
		}
1302
1303
		// Throw up a notice if using a development version and as for feedback.
1304
		if ( Jetpack::is_development_version() ) {
1305
			/* translators: %s is a URL */
1306
			$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/' );
1307
1308
			echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
1309
		}
1310
		// Throw up a notice if using staging mode
1311
		if ( Jetpack::is_staging_site() ) {
1312
			/* translators: %s is a URL */
1313
			$notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' );
1314
1315
			echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
1316
		}
1317
	}
1318
1319
	/**
1320
	 * Whether Jetpack's version maps to a public release, or a development version.
1321
	 */
1322
	public static function is_development_version() {
1323
		return ! preg_match( '/^\d+(\.\d+)+$/', JETPACK__VERSION );
1324
	}
1325
1326
	/**
1327
	 * Is a given user (or the current user if none is specified) linked to a WordPress.com user?
1328
	 */
1329
	public static function is_user_connected( $user_id = false ) {
1330
		$user_id = false === $user_id ? get_current_user_id() : absint( $user_id );
1331
		if ( ! $user_id ) {
1332
			return false;
1333
		}
1334
		return (bool) Jetpack_Data::get_access_token( $user_id );
1335
	}
1336
1337
	/**
1338
	 * Get the wpcom user data of the current|specified connected user.
1339
	 */
1340 View Code Duplication
	public static function get_connected_user_data( $user_id = null ) {
1341
		if ( ! $user_id ) {
1342
			$user_id = get_current_user_id();
1343
		}
1344
		Jetpack::load_xml_rpc_client();
1345
		$xml = new Jetpack_IXR_Client( array(
1346
			'user_id' => $user_id,
1347
		) );
1348
		$xml->query( 'wpcom.getUser' );
1349
		if ( ! $xml->isError() ) {
1350
			return $xml->getResponse();
1351
		}
1352
		return false;
1353
	}
1354
1355
	/**
1356
	 * Get the wpcom email of the current|specified connected user.
1357
	 */
1358 View Code Duplication
	public static function get_connected_user_email( $user_id = null ) {
1359
		if ( ! $user_id ) {
1360
			$user_id = get_current_user_id();
1361
		}
1362
		Jetpack::load_xml_rpc_client();
1363
		$xml = new Jetpack_IXR_Client( array(
1364
			'user_id' => $user_id,
1365
		) );
1366
		$xml->query( 'wpcom.getUserEmail' );
1367
		if ( ! $xml->isError() ) {
1368
			return $xml->getResponse();
1369
		}
1370
		return false;
1371
	}
1372
1373
	/**
1374
	 * Get the wpcom email of the master user.
1375
	 */
1376
	public static function get_master_user_email() {
1377
		$master_user_id = Jetpack_Options::get_option( 'master_user' );
1378
		if ( $master_user_id ) {
1379
			return self::get_connected_user_email( $master_user_id );
1380
		}
1381
		return '';
1382
	}
1383
1384
	function current_user_is_connection_owner() {
1385
		$user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
1386
		return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id;
1387
	}
1388
1389
	/**
1390
	 * Add any extra oEmbed providers that we know about and use on wpcom for feature parity.
1391
	 */
1392
	function extra_oembed_providers() {
1393
		// Cloudup: https://dev.cloudup.com/#oembed
1394
		wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' );
1395
		wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' );
1396
		wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true );
1397
		wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true );
1398
		wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true );
1399
	}
1400
1401
	/**
1402
	 * Synchronize connected user role changes
1403
	 */
1404
	function user_role_change( $user_id ) {
1405
		if ( Jetpack::is_active() && Jetpack::is_user_connected( $user_id ) ) {
1406
			$current_user_id = get_current_user_id();
1407
			wp_set_current_user( $user_id );
1408
			$role = $this->translate_current_user_to_role();
1409
			$signed_role = $this->sign_role( $role );
1410
			wp_set_current_user( $current_user_id );
1411
1412
			$master_token   = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
1413
			$master_user_id = absint( $master_token->external_user_id );
1414
1415
			if ( ! $master_user_id )
1416
				return; // this shouldn't happen
1417
1418
			Jetpack::xmlrpc_async_call( 'jetpack.updateRole', $user_id, $signed_role );
1419
			//@todo retry on failure
1420
1421
			//try to choose a new master if we're demoting the current one
1422 View Code Duplication
			if ( $user_id == $master_user_id && 'administrator' != $role ) {
1423
				$query = new WP_User_Query(
1424
					array(
1425
						'fields'  => array( 'id' ),
1426
						'role'    => 'administrator',
1427
						'orderby' => 'id',
1428
						'exclude' => array( $master_user_id ),
1429
					)
1430
				);
1431
				$new_master = false;
1432
				foreach ( $query->results as $result ) {
1433
					$uid = absint( $result->id );
1434
					if ( $uid && Jetpack::is_user_connected( $uid ) ) {
1435
						$new_master = $uid;
1436
						break;
1437
					}
1438
				}
1439
1440
				if ( $new_master ) {
1441
					Jetpack_Options::update_option( 'master_user', $new_master );
1442
				}
1443
				// else disconnect..?
1444
			}
1445
		}
1446
	}
1447
1448
	/**
1449
	 * Loads the currently active modules.
1450
	 */
1451
	public static function load_modules() {
1452
		if ( ! self::is_active() && !self::is_development_mode() ) {
1453
			if ( ! is_multisite() || ! get_site_option( 'jetpack_protect_active' ) ) {
1454
				return;
1455
			}
1456
		}
1457
1458
		$version = Jetpack_Options::get_option( 'version' );
1459 View Code Duplication
		if ( ! $version ) {
1460
			$version = $old_version = JETPACK__VERSION . ':' . time();
1461
			/** This action is documented in class.jetpack.php */
1462
			do_action( 'updating_jetpack_version', $version, false );
1463
			Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
1464
		}
1465
		list( $version ) = explode( ':', $version );
1466
1467
		$modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) );
1468
1469
		$modules_data = array();
1470
1471
		// Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check.
1472
		if ( version_compare( $version, JETPACK__VERSION, '<' ) ) {
1473
			$updated_modules = array();
1474
			foreach ( $modules as $module ) {
1475
				$modules_data[ $module ] = Jetpack::get_module( $module );
1476
				if ( ! isset( $modules_data[ $module ]['changed'] ) ) {
1477
					continue;
1478
				}
1479
1480
				if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) {
1481
					continue;
1482
				}
1483
1484
				$updated_modules[] = $module;
1485
			}
1486
1487
			$modules = array_diff( $modules, $updated_modules );
1488
		}
1489
1490
		$is_development_mode = Jetpack::is_development_mode();
1491
1492
		foreach ( $modules as $index => $module ) {
1493
			// If we're in dev mode, disable modules requiring a connection
1494
			if ( $is_development_mode ) {
1495
				// Prime the pump if we need to
1496
				if ( empty( $modules_data[ $module ] ) ) {
1497
					$modules_data[ $module ] = Jetpack::get_module( $module );
1498
				}
1499
				// If the module requires a connection, but we're in local mode, don't include it.
1500
				if ( $modules_data[ $module ]['requires_connection'] ) {
1501
					continue;
1502
				}
1503
			}
1504
1505
			if ( did_action( 'jetpack_module_loaded_' . $module ) ) {
1506
				continue;
1507
			}
1508
1509
			if ( ! @include( Jetpack::get_module_path( $module ) ) ) {
1510
				unset( $modules[ $index ] );
1511
				Jetpack_Options::update_option( 'active_modules', array_values( $modules ) );
1512
				continue;
1513
			}
1514
1515
			/**
1516
			 * Fires when a specific module is loaded.
1517
			 * The dynamic part of the hook, $module, is the module slug.
1518
			 *
1519
			 * @since 1.1.0
1520
			 */
1521
			do_action( 'jetpack_module_loaded_' . $module );
1522
		}
1523
1524
		/**
1525
		 * Fires when all the modules are loaded.
1526
		 *
1527
		 * @since 1.1.0
1528
		 */
1529
		do_action( 'jetpack_modules_loaded' );
1530
1531
		// 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.
1532
		if ( Jetpack::is_active() || Jetpack::is_development_mode() )
1533
			require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' );
1534
	}
1535
1536
	/**
1537
	 * Check if Jetpack's REST API compat file should be included
1538
	 * @action plugins_loaded
1539
	 * @return null
1540
	 */
1541
	public function check_rest_api_compat() {
1542
		/**
1543
		 * Filters the list of REST API compat files to be included.
1544
		 *
1545
		 * @since 2.2.5
1546
		 *
1547
		 * @param array $args Array of REST API compat files to include.
1548
		 */
1549
		$_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() );
1550
1551
		if ( function_exists( 'bbpress' ) )
1552
			$_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php';
1553
1554
		foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include )
1555
			require_once $_jetpack_rest_api_compat_include;
1556
	}
1557
1558
	/**
1559
	 * Gets all plugins currently active in values, regardless of whether they're
1560
	 * traditionally activated or network activated.
1561
	 *
1562
	 * @todo Store the result in core's object cache maybe?
1563
	 */
1564
	public static function get_active_plugins() {
1565
		$active_plugins = (array) get_option( 'active_plugins', array() );
1566
1567
		if ( is_multisite() ) {
1568
			// Due to legacy code, active_sitewide_plugins stores them in the keys,
1569
			// whereas active_plugins stores them in the values.
1570
			$network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
1571
			if ( $network_plugins ) {
1572
				$active_plugins = array_merge( $active_plugins, $network_plugins );
1573
			}
1574
		}
1575
1576
		sort( $active_plugins );
1577
1578
		return array_unique( $active_plugins );
1579
	}
1580
1581
	/**
1582
	 * Gets and parses additional plugin data to send with the heartbeat data
1583
	 *
1584
	 * @since 3.8.1
1585
	 *
1586
	 * @return array Array of plugin data
1587
	 */
1588
	public static function get_parsed_plugin_data() {
1589
		if ( ! function_exists( 'get_plugins' ) ) {
1590
			require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
1591
		}
1592
		$all_plugins    = get_plugins();
1593
		$active_plugins = Jetpack::get_active_plugins();
1594
1595
		$plugins = array();
1596
		foreach ( $all_plugins as $path => $plugin_data ) {
1597
			$plugins[ $path ] = array(
1598
					'is_active' => in_array( $path, $active_plugins ),
1599
					'file'      => $path,
1600
					'name'      => $plugin_data['Name'],
1601
					'version'   => $plugin_data['Version'],
1602
					'author'    => $plugin_data['Author'],
1603
			);
1604
		}
1605
1606
		return $plugins;
1607
	}
1608
1609
	/**
1610
	 * Gets and parses theme data to send with the heartbeat data
1611
	 *
1612
	 * @since 3.8.1
1613
	 *
1614
	 * @return array Array of theme data
1615
	 */
1616
	public static function get_parsed_theme_data() {
1617
		$all_themes = wp_get_themes( array( 'allowed' => true ) );
1618
		$header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' );
1619
1620
		$themes = array();
1621
		foreach ( $all_themes as $slug => $theme_data ) {
1622
			$theme_headers = array();
1623
			foreach ( $header_keys as $header_key ) {
1624
				$theme_headers[ $header_key ] = $theme_data->get( $header_key );
1625
			}
1626
1627
			$themes[ $slug ] = array(
1628
					'is_active_theme' => $slug == wp_get_theme()->get_template(),
1629
					'slug' => $slug,
1630
					'theme_root' => $theme_data->get_theme_root_uri(),
1631
					'parent' => $theme_data->parent(),
1632
					'headers' => $theme_headers
1633
			);
1634
		}
1635
1636
		return $themes;
1637
	}
1638
1639
	/**
1640
	 * Checks whether a specific plugin is active.
1641
	 *
1642
	 * We don't want to store these in a static variable, in case
1643
	 * there are switch_to_blog() calls involved.
1644
	 */
1645
	public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) {
1646
		return in_array( $plugin, self::get_active_plugins() );
1647
	}
1648
1649
	/**
1650
	 * Check if Jetpack's Open Graph tags should be used.
1651
	 * If certain plugins are active, Jetpack's og tags are suppressed.
1652
	 *
1653
	 * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
1654
	 * @action plugins_loaded
1655
	 * @return null
1656
	 */
1657
	public function check_open_graph() {
1658
		if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) {
1659
			add_filter( 'jetpack_enable_open_graph', '__return_true', 0 );
1660
		}
1661
1662
		$active_plugins = self::get_active_plugins();
1663
1664
		if ( ! empty( $active_plugins ) ) {
1665
			foreach ( $this->open_graph_conflicting_plugins as $plugin ) {
1666
				if ( in_array( $plugin, $active_plugins ) ) {
1667
					add_filter( 'jetpack_enable_open_graph', '__return_false', 99 );
1668
					break;
1669
				}
1670
			}
1671
		}
1672
1673
		/**
1674
		 * Allow the addition of Open Graph Meta Tags to all pages.
1675
		 *
1676
		 * @since 2.0.3
1677
		 *
1678
		 * @param bool false Should Open Graph Meta tags be added. Default to false.
1679
		 */
1680
		if ( apply_filters( 'jetpack_enable_open_graph', false ) ) {
1681
			require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php';
1682
		}
1683
	}
1684
1685
	/**
1686
	 * Check if Jetpack's Twitter tags should be used.
1687
	 * If certain plugins are active, Jetpack's twitter tags are suppressed.
1688
	 *
1689
	 * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
1690
	 * @action plugins_loaded
1691
	 * @return null
1692
	 */
1693
	public function check_twitter_tags() {
1694
1695
		$active_plugins = self::get_active_plugins();
1696
1697
		if ( ! empty( $active_plugins ) ) {
1698
			foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) {
1699
				if ( in_array( $plugin, $active_plugins ) ) {
1700
					add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 );
1701
					break;
1702
				}
1703
			}
1704
		}
1705
1706
		/**
1707
		 * Allow Twitter Card Meta tags to be disabled.
1708
		 *
1709
		 * @since 2.6.0
1710
		 *
1711
		 * @param bool true Should Twitter Card Meta tags be disabled. Default to true.
1712
		 */
1713
		if ( apply_filters( 'jetpack_disable_twitter_cards', true ) ) {
1714
			require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
1715
		}
1716
	}
1717
1718
1719
1720
1721
	/*
1722
	 *
1723
	 * Jetpack Security Reports
1724
	 *
1725
	 * Allowed types: login_form, backup, file_scanning, spam
1726
	 *
1727
	 * Args for login_form and spam: 'blocked'=>(int)(optional), 'status'=>(string)(ok, warning, error), 'message'=>(optional, disregarded if status is ok, allowed tags: a, em, strong)
1728
	 *
1729
	 * Args for backup and file_scanning: 'last'=>(timestamp)(optional), 'next'=>(timestamp)(optional), 'status'=>(string)(ok, warning, error), 'message'=>(optional, disregarded if status is ok, allowed tags: a, em, strong)
1730
	 *
1731
	 *
1732
	 * Example code to submit a security report:
1733
	 *
1734
	 *  function akismet_submit_jetpack_security_report() {
1735
	 *  	Jetpack::submit_security_report( 'spam', __FILE__, $args = array( 'blocked' => 138284, status => 'ok' ) );
1736
	 *  }
1737
	 *  add_action( 'jetpack_security_report', 'akismet_submit_jetpack_security_report' );
1738
	 *
1739
	 */
1740
1741
1742
	/**
1743
	 * Calls for security report submissions.
1744
	 *
1745
	 * @return null
1746
	 */
1747
	public static function perform_security_reporting() {
1748
		$no_check_needed = get_site_transient( 'security_report_performed_recently' );
1749
1750
		if ( $no_check_needed ) {
1751
			return;
1752
		}
1753
1754
		/**
1755
		 * Fires before a security report is created.
1756
		 *
1757
		 * @since 3.4.0
1758
		 */
1759
		do_action( 'jetpack_security_report' );
1760
1761
		Jetpack_Options::update_option( 'security_report', self::$security_report );
1762
		set_site_transient( 'security_report_performed_recently', 1, 15 * MINUTE_IN_SECONDS );
1763
	}
1764
1765
	/**
1766
	 * Allows plugins to submit security reports.
1767
 	 *
1768
	 * @param string  $type         Report type (login_form, backup, file_scanning, spam)
1769
	 * @param string  $plugin_file  Plugin __FILE__, so that we can pull plugin data
1770
	 * @param array   $args         See definitions above
1771
	 */
1772
	public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) {
1773
1774
		if( !doing_action( 'jetpack_security_report' ) ) {
1775
			return new WP_Error( 'not_collecting_report', 'Not currently collecting security reports.  Please use the jetpack_security_report hook.' );
1776
		}
1777
1778
		if( !is_string( $type ) || !is_string( $plugin_file ) ) {
1779
			return new WP_Error( 'invalid_security_report', 'Invalid Security Report' );
1780
		}
1781
1782
		if( !function_exists( 'get_plugin_data' ) ) {
1783
			include( ABSPATH . 'wp-admin/includes/plugin.php' );
1784
		}
1785
1786
		//Get rid of any non-allowed args
1787
		$args = array_intersect_key( $args, array_flip( array( 'blocked', 'last', 'next', 'status', 'message' ) ) );
1788
1789
		$plugin = get_plugin_data( $plugin_file );
1790
1791
		if ( !$plugin['Name'] ) {
1792
			return new WP_Error( 'security_report_missing_plugin_name', 'Invalid Plugin File Provided' );
1793
		}
1794
1795
		// Sanitize everything to make sure we're not syncing something wonky
1796
		$type = sanitize_key( $type );
1797
1798
		$args['plugin'] = $plugin;
1799
1800
		// Cast blocked, last and next as integers.
1801
		// Last and next should be in unix timestamp format
1802
		if ( isset( $args['blocked'] ) ) {
1803
			$args['blocked'] = (int) $args['blocked'];
1804
		}
1805
		if ( isset( $args['last'] ) ) {
1806
			$args['last'] = (int) $args['last'];
1807
		}
1808
		if ( isset( $args['next'] ) ) {
1809
			$args['next'] = (int) $args['next'];
1810
		}
1811
		if ( !in_array( $args['status'], array( 'ok', 'warning', 'error' ) ) ) {
1812
			$args['status'] = 'ok';
1813
		}
1814
		if ( isset( $args['message'] ) ) {
1815
1816
			if( $args['status'] == 'ok' ) {
1817
				unset( $args['message'] );
1818
			}
1819
1820
			$allowed_html = array(
1821
			    'a' => array(
1822
			        'href' => array(),
1823
			        'title' => array()
1824
			    ),
1825
			    'em' => array(),
1826
			    'strong' => array(),
1827
			);
1828
1829
			$args['message'] = wp_kses( $args['message'], $allowed_html );
1830
		}
1831
1832
		$plugin_name = $plugin[ 'Name' ];
1833
1834
		self::$security_report[ $type ][ $plugin_name ] = $args;
1835
	}
1836
1837
	/**
1838
	 * Collects a new report if needed, then returns it.
1839
	 */
1840
	public function get_security_report() {
1841
		self::perform_security_reporting();
1842
		return Jetpack_Options::get_option( 'security_report' );
1843
	}
1844
1845
1846
/* Jetpack Options API */
1847
1848
	public static function get_option_names( $type = 'compact' ) {
1849
		return Jetpack_Options::get_option_names( $type );
1850
	}
1851
1852
	/**
1853
	 * Returns the requested option.  Looks in jetpack_options or jetpack_$name as appropriate.
1854
 	 *
1855
	 * @param string $name    Option name
1856
	 * @param mixed  $default (optional)
1857
	 */
1858
	public static function get_option( $name, $default = false ) {
1859
		return Jetpack_Options::get_option( $name, $default );
1860
	}
1861
1862
	/**
1863
	* Stores two secrets and a timestamp so WordPress.com can make a request back and verify an action
1864
	* Does some extra verification so urls (such as those to public-api, register, etc) can't just be crafted
1865
	* $name must be a registered option name.
1866
	*/
1867
	public static function create_nonce( $name ) {
1868
		$secret = wp_generate_password( 32, false ) . ':' . wp_generate_password( 32, false ) . ':' . ( time() + 600 );
1869
1870
		Jetpack_Options::update_option( $name, $secret );
1871
		@list( $secret_1, $secret_2, $eol ) = explode( ':', Jetpack_Options::get_option( $name ) );
1872
		if ( empty( $secret_1 ) || empty( $secret_2 ) || $eol < time() )
1873
			return new Jetpack_Error( 'missing_secrets' );
1874
1875
		return array(
1876
			'secret_1' => $secret_1,
1877
			'secret_2' => $secret_2,
1878
			'eol'      => $eol,
1879
		);
1880
	}
1881
1882
	/**
1883
	 * Updates the single given option.  Updates jetpack_options or jetpack_$name as appropriate.
1884
 	 *
1885
	 * @deprecated 3.4 use Jetpack_Options::update_option() instead.
1886
	 * @param string $name  Option name
1887
	 * @param mixed  $value Option value
1888
	 */
1889
	public static function update_option( $name, $value ) {
1890
		_deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' );
1891
		return Jetpack_Options::update_option( $name, $value );
1892
	}
1893
1894
	/**
1895
	 * Updates the multiple given options.  Updates jetpack_options and/or jetpack_$name as appropriate.
1896
 	 *
1897
	 * @deprecated 3.4 use Jetpack_Options::update_options() instead.
1898
	 * @param array $array array( option name => option value, ... )
1899
	 */
1900
	public static function update_options( $array ) {
1901
		_deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' );
1902
		return Jetpack_Options::update_options( $array );
1903
	}
1904
1905
	/**
1906
	 * Deletes the given option.  May be passed multiple option names as an array.
1907
	 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
1908
	 *
1909
	 * @deprecated 3.4 use Jetpack_Options::delete_option() instead.
1910
	 * @param string|array $names
1911
	 */
1912
	public static function delete_option( $names ) {
1913
		_deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' );
1914
		return Jetpack_Options::delete_option( $names );
1915
	}
1916
1917
	/**
1918
	 * Enters a user token into the user_tokens option
1919
	 *
1920
	 * @param int $user_id
1921
	 * @param string $token
1922
	 * return bool
1923
	 */
1924
	public static function update_user_token( $user_id, $token, $is_master_user ) {
1925
		// not designed for concurrent updates
1926
		$user_tokens = Jetpack_Options::get_option( 'user_tokens' );
1927
		if ( ! is_array( $user_tokens ) )
1928
			$user_tokens = array();
1929
		$user_tokens[$user_id] = $token;
1930
		if ( $is_master_user ) {
1931
			$master_user = $user_id;
1932
			$options     = compact( 'user_tokens', 'master_user' );
1933
		} else {
1934
			$options = compact( 'user_tokens' );
1935
		}
1936
		return Jetpack_Options::update_options( $options );
1937
	}
1938
1939
	/**
1940
	 * Returns an array of all PHP files in the specified absolute path.
1941
	 * Equivalent to glob( "$absolute_path/*.php" ).
1942
	 *
1943
	 * @param string $absolute_path The absolute path of the directory to search.
1944
	 * @return array Array of absolute paths to the PHP files.
1945
	 */
1946
	public static function glob_php( $absolute_path ) {
1947
		if ( function_exists( 'glob' ) ) {
1948
			return glob( "$absolute_path/*.php" );
1949
		}
1950
1951
		$absolute_path = untrailingslashit( $absolute_path );
1952
		$files = array();
1953
		if ( ! $dir = @opendir( $absolute_path ) ) {
1954
			return $files;
1955
		}
1956
1957
		while ( false !== $file = readdir( $dir ) ) {
1958
			if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) {
1959
				continue;
1960
			}
1961
1962
			$file = "$absolute_path/$file";
1963
1964
			if ( ! is_file( $file ) ) {
1965
				continue;
1966
			}
1967
1968
			$files[] = $file;
1969
		}
1970
1971
		closedir( $dir );
1972
1973
		return $files;
1974
	}
1975
1976
	public static function activate_new_modules( $redirect = false ) {
1977
		if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
1978
			return;
1979
		}
1980
1981
		$jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic]
1982 View Code Duplication
		if ( ! $jetpack_old_version ) {
1983
			$jetpack_old_version = $version = $old_version = '1.1:' . time();
1984
			/** This action is documented in class.jetpack.php */
1985
			do_action( 'updating_jetpack_version', $version, false );
1986
			Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
1987
		}
1988
1989
		list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic]
1990
1991
		if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) {
1992
			return;
1993
		}
1994
1995
		$active_modules     = Jetpack::get_active_modules();
1996
		$reactivate_modules = array();
1997
		foreach ( $active_modules as $active_module ) {
1998
			$module = Jetpack::get_module( $active_module );
1999
			if ( ! isset( $module['changed'] ) ) {
2000
				continue;
2001
			}
2002
2003
			if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) {
2004
				continue;
2005
			}
2006
2007
			$reactivate_modules[] = $active_module;
2008
			Jetpack::deactivate_module( $active_module );
2009
		}
2010
2011
		$new_version = JETPACK__VERSION . ':' . time();
2012
		/** This action is documented in class.jetpack.php */
2013
		do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version );
2014
		Jetpack_Options::update_options(
2015
			array(
2016
				'version'     => $new_version,
2017
				'old_version' => $jetpack_old_version,
2018
			)
2019
		);
2020
2021
		Jetpack::state( 'message', 'modules_activated' );
2022
		Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules );
2023
2024
		if ( $redirect ) {
2025
			$page = 'jetpack'; // make sure we redirect to either settings or the jetpack page
2026
			if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) {
2027
				$page = $_GET['page'];
2028
			}
2029
2030
			wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) );
2031
			exit;
2032
		}
2033
	}
2034
2035
	/**
2036
	 * List available Jetpack modules. Simply lists .php files in /modules/.
2037
	 * Make sure to tuck away module "library" files in a sub-directory.
2038
	 */
2039
	public static function get_available_modules( $min_version = false, $max_version = false ) {
2040
		static $modules = null;
2041
2042
		if ( ! isset( $modules ) ) {
2043
			$available_modules_option = Jetpack_Options::get_option( 'available_modules', array() );
2044
			// Use the cache if we're on the front-end and it's available...
2045
			if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) {
2046
				$modules = $available_modules_option[ JETPACK__VERSION ];
2047
			} else {
2048
				$files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' );
2049
2050
				$modules = array();
2051
2052
				foreach ( $files as $file ) {
2053
					if ( ! $headers = Jetpack::get_module( $file ) ) {
2054
						continue;
2055
					}
2056
2057
					$modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced'];
2058
				}
2059
2060
				Jetpack_Options::update_option( 'available_modules', array(
2061
					JETPACK__VERSION => $modules,
2062
				) );
2063
			}
2064
		}
2065
2066
		/**
2067
		 * Filters the array of modules available to be activated.
2068
		 *
2069
		 * @since 2.4.0
2070
		 *
2071
		 * @param array $modules Array of available modules.
2072
		 * @param string $min_version Minimum version number required to use modules.
2073
		 * @param string $max_version Maximum version number required to use modules.
2074
		 */
2075
		$mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version );
2076
2077
		if ( ! $min_version && ! $max_version ) {
2078
			return array_keys( $mods );
2079
		}
2080
2081
		$r = array();
2082
		foreach ( $mods as $slug => $introduced ) {
2083
			if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) {
2084
				continue;
2085
			}
2086
2087
			if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) {
2088
				continue;
2089
			}
2090
2091
			$r[] = $slug;
2092
		}
2093
2094
		return $r;
2095
	}
2096
2097
	/**
2098
	 * Default modules loaded on activation.
2099
	 */
2100
	public static function get_default_modules( $min_version = false, $max_version = false ) {
2101
		$return = array();
2102
2103
		foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) {
2104
			$module_data = Jetpack::get_module( $module );
2105
2106
			switch ( strtolower( $module_data['auto_activate'] ) ) {
2107
				case 'yes' :
2108
					$return[] = $module;
2109
					break;
2110
				case 'public' :
2111
					if ( Jetpack_Options::get_option( 'public' ) ) {
2112
						$return[] = $module;
2113
					}
2114
					break;
2115
				case 'no' :
2116
				default :
2117
					break;
2118
			}
2119
		}
2120
		/**
2121
		 * Filters the array of default modules.
2122
		 *
2123
		 * @since 2.5.0
2124
		 *
2125
		 * @param array $return Array of default modules.
2126
		 * @param string $min_version Minimum version number required to use modules.
2127
		 * @param string $max_version Maximum version number required to use modules.
2128
		 */
2129
		return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version );
2130
	}
2131
2132
	/**
2133
	 * Checks activated modules during auto-activation to determine
2134
	 * if any of those modules are being deprecated.  If so, close
2135
	 * them out, and add any replacement modules.
2136
	 *
2137
	 * Runs at priority 99 by default.
2138
	 *
2139
	 * This is run late, so that it can still activate a module if
2140
	 * the new module is a replacement for another that the user
2141
	 * currently has active, even if something at the normal priority
2142
	 * would kibosh everything.
2143
	 *
2144
	 * @since 2.6
2145
	 * @uses jetpack_get_default_modules filter
2146
	 * @param array $modules
2147
	 * @return array
2148
	 */
2149
	function handle_deprecated_modules( $modules ) {
2150
		$deprecated_modules = array(
2151
			'debug'            => null,  // Closed out and moved to ./class.jetpack-debugger.php
2152
			'wpcc'             => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2153
			'gplus-authorship' => null,  // Closed out in 3.2 -- Google dropped support.
2154
		);
2155
2156
		// Don't activate SSO if they never completed activating WPCC.
2157
		if ( Jetpack::is_module_active( 'wpcc' ) ) {
2158
			$wpcc_options = Jetpack_Options::get_option( 'wpcc_options' );
2159
			if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) {
2160
				$deprecated_modules['wpcc'] = null;
2161
			}
2162
		}
2163
2164
		foreach ( $deprecated_modules as $module => $replacement ) {
2165
			if ( Jetpack::is_module_active( $module ) ) {
2166
				self::deactivate_module( $module );
2167
				if ( $replacement ) {
2168
					$modules[] = $replacement;
2169
				}
2170
			}
2171
		}
2172
2173
		return array_unique( $modules );
2174
	}
2175
2176
	/**
2177
	 * Checks activated plugins during auto-activation to determine
2178
	 * if any of those plugins are in the list with a corresponding module
2179
	 * that is not compatible with the plugin. The module will not be allowed
2180
	 * to auto-activate.
2181
	 *
2182
	 * @since 2.6
2183
	 * @uses jetpack_get_default_modules filter
2184
	 * @param array $modules
2185
	 * @return array
2186
	 */
2187
	function filter_default_modules( $modules ) {
2188
2189
		$active_plugins = self::get_active_plugins();
2190
2191
		if ( ! empty( $active_plugins ) ) {
2192
2193
			// For each module we'd like to auto-activate...
2194
			foreach ( $modules as $key => $module ) {
2195
				// If there are potential conflicts for it...
2196
				if ( ! empty( $this->conflicting_plugins[ $module ] ) ) {
2197
					// For each potential conflict...
2198
					foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) {
2199
						// If that conflicting plugin is active...
2200
						if ( in_array( $plugin, $active_plugins ) ) {
2201
							// Remove that item from being auto-activated.
2202
							unset( $modules[ $key ] );
2203
						}
2204
					}
2205
				}
2206
			}
2207
		}
2208
2209
		return $modules;
2210
	}
2211
2212
	/**
2213
	 * Extract a module's slug from its full path.
2214
	 */
2215
	public static function get_module_slug( $file ) {
2216
		return str_replace( '.php', '', basename( $file ) );
2217
	}
2218
2219
	/**
2220
	 * Generate a module's path from its slug.
2221
	 */
2222
	public static function get_module_path( $slug ) {
2223
		return JETPACK__PLUGIN_DIR . "modules/$slug.php";
2224
	}
2225
2226
	/**
2227
	 * Load module data from module file. Headers differ from WordPress
2228
	 * plugin headers to avoid them being identified as standalone
2229
	 * plugins on the WordPress plugins page.
2230
	 */
2231
	public static function get_module( $module ) {
2232
		$headers = array(
2233
			'name'                      => 'Module Name',
2234
			'description'               => 'Module Description',
2235
			'jumpstart_desc'            => 'Jumpstart Description',
2236
			'sort'                      => 'Sort Order',
2237
			'recommendation_order'      => 'Recommendation Order',
2238
			'introduced'                => 'First Introduced',
2239
			'changed'                   => 'Major Changes In',
2240
			'deactivate'                => 'Deactivate',
2241
			'free'                      => 'Free',
2242
			'requires_connection'       => 'Requires Connection',
2243
			'auto_activate'             => 'Auto Activate',
2244
			'module_tags'               => 'Module Tags',
2245
			'feature'                   => 'Feature',
2246
			'additional_search_queries' => 'Additional Search Queries',
2247
		);
2248
2249
		$file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) );
2250
2251
		$mod = Jetpack::get_file_data( $file, $headers );
2252
		if ( empty( $mod['name'] ) ) {
2253
			return false;
2254
		}
2255
2256
		$mod['sort']                    = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
2257
		$mod['recommendation_order']    = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order'];
2258
		$mod['deactivate']              = empty( $mod['deactivate'] );
2259
		$mod['free']                    = empty( $mod['free'] );
2260
		$mod['requires_connection']     = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true;
2261
2262
		if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) {
2263
			$mod['auto_activate'] = 'No';
2264
		} else {
2265
			$mod['auto_activate'] = (string) $mod['auto_activate'];
2266
		}
2267
2268
		if ( $mod['module_tags'] ) {
2269
			$mod['module_tags'] = explode( ',', $mod['module_tags'] );
2270
			$mod['module_tags'] = array_map( 'trim', $mod['module_tags'] );
2271
			$mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] );
2272
		} else {
2273
			$mod['module_tags'] = array( self::translate_module_tag( 'Other' ) );
2274
		}
2275
2276
		if ( $mod['feature'] ) {
2277
			$mod['feature'] = explode( ',', $mod['feature'] );
2278
			$mod['feature'] = array_map( 'trim', $mod['feature'] );
2279
		} else {
2280
			$mod['feature'] = array( self::translate_module_tag( 'Other' ) );
2281
		}
2282
2283
		/**
2284
		 * Filters the feature array on a module.
2285
		 *
2286
		 * This filter allows you to control where each module is filtered: Recommended,
2287
		 * Jumpstart, and the default "Other" listing.
2288
		 *
2289
		 * @since 3.5.0
2290
		 *
2291
		 * @param array   $mod['feature'] The areas to feature this module:
2292
		 *     'Jumpstart' adds to the "Jumpstart" option to activate many modules at once.
2293
		 *     'Recommended' shows on the main Jetpack admin screen.
2294
		 *     'Other' should be the default if no other value is in the array.
2295
		 * @param string  $module The slug of the module, e.g. sharedaddy.
2296
		 * @param array   $mod All the currently assembled module data.
2297
		 */
2298
		$mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod );
2299
2300
		/**
2301
		 * Filter the returned data about a module.
2302
		 *
2303
		 * This filter allows overriding any info about Jetpack modules. It is dangerous,
2304
		 * so please be careful.
2305
		 *
2306
		 * @since 3.6.0
2307
		 *
2308
		 * @param array   $mod    The details of the requested module.
2309
		 * @param string  $module The slug of the module, e.g. sharedaddy
2310
		 * @param string  $file   The path to the module source file.
2311
		 */
2312
		return apply_filters( 'jetpack_get_module', $mod, $module, $file );
2313
	}
2314
2315
	/**
2316
	 * Like core's get_file_data implementation, but caches the result.
2317
	 */
2318
	public static function get_file_data( $file, $headers ) {
2319
		//Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated
2320
		$file_name = basename( $file );
2321
		$file_data_option = Jetpack_Options::get_option( 'file_data', array() );
2322
		$key              = md5( $file_name . serialize( $headers ) );
2323
		$refresh_cache    = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 );
2324
2325
		// If we don't need to refresh the cache, and already have the value, short-circuit!
2326
		if ( ! $refresh_cache && isset( $file_data_option[ JETPACK__VERSION ][ $key ] ) ) {
2327
			return $file_data_option[ JETPACK__VERSION ][ $key ];
2328
		}
2329
2330
		$data = get_file_data( $file, $headers );
2331
2332
		// Strip out any old Jetpack versions that are cluttering the option.
2333
		$file_data_option = array_intersect_key( (array) $file_data_option, array( JETPACK__VERSION => null ) );
2334
		$file_data_option[ JETPACK__VERSION ][ $key ] = $data;
2335
		Jetpack_Options::update_option( 'file_data', $file_data_option );
2336
2337
		return $data;
2338
	}
2339
2340
	/**
2341
	 * Return translated module tag.
2342
	 *
2343
	 * @param string $tag Tag as it appears in each module heading.
2344
	 *
2345
	 * @return mixed
2346
	 */
2347
	public static function translate_module_tag( $tag ) {
2348
		return jetpack_get_module_i18n_tag( $tag );
2349
	}
2350
2351
	/**
2352
	 * Return module name translation. Uses matching string created in modules/module-headings.php.
2353
	 *
2354
	 * @since 3.9.2
2355
	 *
2356
	 * @param array $modules
2357
	 *
2358
	 * @return string|void
2359
	 */
2360
	public static function get_translated_modules( $modules ) {
2361
		foreach ( $modules as $index => $module ) {
2362
			$i18n_module = jetpack_get_module_i18n( $module['module'] );
2363
			if ( isset( $module['name'] ) ) {
2364
				$modules[ $index ]['name'] = $i18n_module['name'];
2365
			}
2366
			if ( isset( $module['description'] ) ) {
2367
				$modules[ $index ]['description'] = $i18n_module['description'];
2368
				$modules[ $index ]['short_description'] = $i18n_module['description'];
2369
			}
2370
		}
2371
		return $modules;
2372
	}
2373
2374
	/**
2375
	 * Get a list of activated modules as an array of module slugs.
2376
	 */
2377
	public static function get_active_modules() {
2378
		$active = Jetpack_Options::get_option( 'active_modules' );
2379
		if ( ! is_array( $active ) )
2380
			$active = array();
2381
		if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) {
2382
			$active[] = 'vaultpress';
2383
		} else {
2384
			$active = array_diff( $active, array( 'vaultpress' ) );
2385
		}
2386
2387
		//If protect is active on the main site of a multisite, it should be active on all sites.
2388
		if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) {
2389
			$active[] = 'protect';
2390
		}
2391
2392
		return array_unique( $active );
2393
	}
2394
2395
	/**
2396
	 * Check whether or not a Jetpack module is active.
2397
	 *
2398
	 * @param string $module The slug of a Jetpack module.
2399
	 * @return bool
2400
	 *
2401
	 * @static
2402
	 */
2403
	public static function is_module_active( $module ) {
2404
		return in_array( $module, self::get_active_modules() );
2405
	}
2406
2407
	public static function is_module( $module ) {
2408
		return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() );
2409
	}
2410
2411
	/**
2412
	 * Catches PHP errors.  Must be used in conjunction with output buffering.
2413
	 *
2414
	 * @param bool $catch True to start catching, False to stop.
2415
	 *
2416
	 * @static
2417
	 */
2418
	public static function catch_errors( $catch ) {
2419
		static $display_errors, $error_reporting;
2420
2421
		if ( $catch ) {
2422
			$display_errors  = @ini_set( 'display_errors', 1 );
2423
			$error_reporting = @error_reporting( E_ALL );
2424
			add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
2425
		} else {
2426
			@ini_set( 'display_errors', $display_errors );
2427
			@error_reporting( $error_reporting );
2428
			remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
2429
		}
2430
	}
2431
2432
	/**
2433
	 * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
2434
	 */
2435
	public static function catch_errors_on_shutdown() {
2436
		Jetpack::state( 'php_errors', ob_get_clean() );
2437
	}
2438
2439
	public static function activate_default_modules( $min_version = false, $max_version = false, $other_modules = array(), $redirect = true ) {
2440
		$jetpack = Jetpack::init();
2441
2442
		$modules = Jetpack::get_default_modules( $min_version, $max_version );
2443
		$modules = array_merge( $other_modules, $modules );
2444
2445
		// Look for standalone plugins and disable if active.
2446
2447
		$to_deactivate = array();
2448
		foreach ( $modules as $module ) {
2449
			if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) {
2450
				$to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module];
2451
			}
2452
		}
2453
2454
		$deactivated = array();
2455
		foreach ( $to_deactivate as $module => $deactivate_me ) {
2456
			list( $probable_file, $probable_title ) = $deactivate_me;
2457
			if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) {
2458
				$deactivated[] = $module;
2459
			}
2460
		}
2461
2462
		if ( $deactivated && $redirect ) {
2463
			Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) );
2464
2465
			$url = add_query_arg(
2466
				array(
2467
					'action'   => 'activate_default_modules',
2468
					'_wpnonce' => wp_create_nonce( 'activate_default_modules' ),
2469
				),
2470
				add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) )
2471
			);
2472
			wp_safe_redirect( $url );
2473
			exit;
2474
		}
2475
2476
		/**
2477
		 * Fires before default modules are activated.
2478
		 *
2479
		 * @since 1.9.0
2480
		 *
2481
		 * @param string $min_version Minimum version number required to use modules.
2482
		 * @param string $max_version Maximum version number required to use modules.
2483
		 * @param array $other_modules Array of other modules to activate alongside the default modules.
2484
		 */
2485
		do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules );
2486
2487
		// Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating
2488
		Jetpack::restate();
2489
		Jetpack::catch_errors( true );
2490
2491
		$active = Jetpack::get_active_modules();
2492
2493
		foreach ( $modules as $module ) {
2494
			if ( did_action( "jetpack_module_loaded_$module" ) ) {
2495
				$active[] = $module;
2496
				Jetpack_Options::update_option( 'active_modules', array_unique( $active ) );
2497
				continue;
2498
			}
2499
2500
			if ( in_array( $module, $active ) ) {
2501
				$module_info = Jetpack::get_module( $module );
2502
				if ( ! $module_info['deactivate'] ) {
2503
					$state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
2504 View Code Duplication
					if ( $active_state = Jetpack::state( $state ) ) {
2505
						$active_state = explode( ',', $active_state );
2506
					} else {
2507
						$active_state = array();
2508
					}
2509
					$active_state[] = $module;
2510
					Jetpack::state( $state, implode( ',', $active_state ) );
2511
				}
2512
				continue;
2513
			}
2514
2515
			$file = Jetpack::get_module_path( $module );
2516
			if ( ! file_exists( $file ) ) {
2517
				continue;
2518
			}
2519
2520
			// we'll override this later if the plugin can be included without fatal error
2521
			if ( $redirect ) {
2522
				wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
2523
			}
2524
			Jetpack::state( 'error', 'module_activation_failed' );
2525
			Jetpack::state( 'module', $module );
2526
			ob_start();
2527
			require $file;
2528
			/**
2529
			 * Fires when a specific module is activated.
2530
			 *
2531
			 * @since 1.9.0
2532
			 *
2533
			 * @param string $module Module slug.
2534
			 */
2535
			do_action( 'jetpack_activate_module', $module );
2536
			$active[] = $module;
2537
			$state    = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
2538 View Code Duplication
			if ( $active_state = Jetpack::state( $state ) ) {
2539
				$active_state = explode( ',', $active_state );
2540
			} else {
2541
				$active_state = array();
2542
			}
2543
			$active_state[] = $module;
2544
			Jetpack::state( $state, implode( ',', $active_state ) );
2545
			Jetpack_Options::update_option( 'active_modules', array_unique( $active ) );
2546
			ob_end_clean();
2547
		}
2548
		Jetpack::state( 'error', false );
2549
		Jetpack::state( 'module', false );
2550
		Jetpack::catch_errors( false );
2551
		/**
2552
		 * Fires when default modules are activated.
2553
		 *
2554
		 * @since 1.9.0
2555
		 *
2556
		 * @param string $min_version Minimum version number required to use modules.
2557
		 * @param string $max_version Maximum version number required to use modules.
2558
		 * @param array $other_modules Array of other modules to activate alongside the default modules.
2559
		 */
2560
		do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules );
2561
	}
2562
2563
	public static function activate_module( $module, $exit = true, $redirect = true ) {
2564
		/**
2565
		 * Fires before a module is activated.
2566
		 *
2567
		 * @since 2.6.0
2568
		 *
2569
		 * @param string $module Module slug.
2570
		 * @param bool $exit Should we exit after the module has been activated. Default to true.
2571
		 * @param bool $redirect Should the user be redirected after module activation? Default to true.
2572
		 */
2573
		do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect );
2574
2575
		$jetpack = Jetpack::init();
2576
2577
		if ( ! strlen( $module ) )
2578
			return false;
2579
2580
		if ( ! Jetpack::is_module( $module ) )
2581
			return false;
2582
2583
		// If it's already active, then don't do it again
2584
		$active = Jetpack::get_active_modules();
2585
		foreach ( $active as $act ) {
2586
			if ( $act == $module )
2587
				return true;
2588
		}
2589
2590
		$module_data = Jetpack::get_module( $module );
2591
2592
		if ( ! Jetpack::is_active() ) {
2593
			if ( !Jetpack::is_development_mode() )
2594
				return false;
2595
2596
			// If we're not connected but in development mode, make sure the module doesn't require a connection
2597
			if ( Jetpack::is_development_mode() && $module_data['requires_connection'] )
2598
				return false;
2599
		}
2600
2601
		// Check and see if the old plugin is active
2602
		if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) {
2603
			// Deactivate the old plugin
2604
			if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) {
2605
				// If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module
2606
				// We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load.
2607
				Jetpack::state( 'deactivated_plugins', $module );
2608
				wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
2609
				exit;
2610
			}
2611
		}
2612
2613
		// Check the file for fatal errors, a la wp-admin/plugins.php::activate
2614
		Jetpack::state( 'module', $module );
2615
		Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error
2616
2617
		Jetpack::catch_errors( true );
2618
		ob_start();
2619
		require Jetpack::get_module_path( $module );
2620
		/** This action is documented in class.jetpack.php */
2621
		do_action( 'jetpack_activate_module', $module );
2622
		$active[] = $module;
2623
		Jetpack_Options::update_option( 'active_modules', array_unique( $active ) );
2624
		Jetpack::state( 'error', false ); // the override
2625
		Jetpack::state( 'message', 'module_activated' );
2626
		Jetpack::state( 'module', $module );
2627
		ob_end_clean();
2628
		Jetpack::catch_errors( false );
2629
2630
		// A flag for Jump Start so it's not shown again. Only set if it hasn't been yet.
2631 View Code Duplication
		if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
2632
			Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' );
2633
2634
			//Jump start is being dismissed send data to MC Stats
2635
			$jetpack->stat( 'jumpstart', 'manual,'.$module );
2636
2637
			$jetpack->do_stats( 'server_side' );
2638
		}
2639
2640
		if ( $redirect ) {
2641
			wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
2642
		}
2643
		if ( $exit ) {
2644
			exit;
2645
		}
2646
		return true;
2647
	}
2648
2649
	function activate_module_actions( $module ) {
2650
		/**
2651
		 * Fires when a module is activated.
2652
		 * The dynamic part of the filter, $module, is the module slug.
2653
		 *
2654
		 * @since 1.9.0
2655
		 *
2656
		 * @param string $module Module slug.
2657
		 */
2658
		do_action( "jetpack_activate_module_$module", $module );
2659
	}
2660
2661
	public static function deactivate_module( $module ) {
2662
		/**
2663
		 * Fires when a module is deactivated.
2664
		 *
2665
		 * @since 1.9.0
2666
		 *
2667
		 * @param string $module Module slug.
2668
		 */
2669
		do_action( 'jetpack_pre_deactivate_module', $module );
2670
2671
		$jetpack = Jetpack::init();
2672
2673
		$active = Jetpack::get_active_modules();
2674
		$new    = array_filter( array_diff( $active, (array) $module ) );
2675
2676
		/**
2677
		 * Fires when a module is deactivated.
2678
		 * The dynamic part of the filter, $module, is the module slug.
2679
		 *
2680
		 * @since 1.9.0
2681
		 *
2682
		 * @param string $module Module slug.
2683
		 */
2684
		do_action( "jetpack_deactivate_module_$module", $module );
2685
2686
		// A flag for Jump Start so it's not shown again.
2687 View Code Duplication
		if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
2688
			Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' );
2689
2690
			//Jump start is being dismissed send data to MC Stats
2691
			$jetpack->stat( 'jumpstart', 'manual,deactivated-'.$module );
2692
2693
			$jetpack->do_stats( 'server_side' );
2694
		}
2695
2696
		return Jetpack_Options::update_option( 'active_modules', array_unique( $new ) );
2697
	}
2698
2699
	public static function enable_module_configurable( $module ) {
2700
		$module = Jetpack::get_module_slug( $module );
2701
		add_filter( 'jetpack_module_configurable_' . $module, '__return_true' );
2702
	}
2703
2704
	public static function module_configuration_url( $module ) {
2705
		$module = Jetpack::get_module_slug( $module );
2706
		return Jetpack::admin_url( array( 'page' => 'jetpack', 'configure' => $module ) );
2707
	}
2708
2709
	public static function module_configuration_load( $module, $method ) {
2710
		$module = Jetpack::get_module_slug( $module );
2711
		add_action( 'jetpack_module_configuration_load_' . $module, $method );
2712
	}
2713
2714
	public static function module_configuration_head( $module, $method ) {
2715
		$module = Jetpack::get_module_slug( $module );
2716
		add_action( 'jetpack_module_configuration_head_' . $module, $method );
2717
	}
2718
2719
	public static function module_configuration_screen( $module, $method ) {
2720
		$module = Jetpack::get_module_slug( $module );
2721
		add_action( 'jetpack_module_configuration_screen_' . $module, $method );
2722
	}
2723
2724
	public static function module_configuration_activation_screen( $module, $method ) {
2725
		$module = Jetpack::get_module_slug( $module );
2726
		add_action( 'display_activate_module_setting_' . $module, $method );
2727
	}
2728
2729
/* Installation */
2730
2731
	public static function bail_on_activation( $message, $deactivate = true ) {
2732
?>
2733
<!doctype html>
2734
<html>
2735
<head>
2736
<meta charset="<?php bloginfo( 'charset' ); ?>">
2737
<style>
2738
* {
2739
	text-align: center;
2740
	margin: 0;
2741
	padding: 0;
2742
	font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
2743
}
2744
p {
2745
	margin-top: 1em;
2746
	font-size: 18px;
2747
}
2748
</style>
2749
<body>
2750
<p><?php echo esc_html( $message ); ?></p>
2751
</body>
2752
</html>
2753
<?php
2754
		if ( $deactivate ) {
2755
			$plugins = get_option( 'active_plugins' );
2756
			$jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' );
2757
			$update  = false;
2758
			foreach ( $plugins as $i => $plugin ) {
2759
				if ( $plugin === $jetpack ) {
2760
					$plugins[$i] = false;
2761
					$update = true;
2762
				}
2763
			}
2764
2765
			if ( $update ) {
2766
				update_option( 'active_plugins', array_filter( $plugins ) );
2767
			}
2768
		}
2769
		exit;
2770
	}
2771
2772
	/**
2773
	 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
2774
	 * @static
2775
	 */
2776
	public static function plugin_activation( $network_wide ) {
2777
		Jetpack_Options::update_option( 'activated', 1 );
2778
2779
		if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
2780
			Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) );
2781
		}
2782
2783
		if ( $network_wide )
2784
			Jetpack::state( 'network_nag', true );
2785
2786
		Jetpack::plugin_initialize();
2787
	}
2788
	/**
2789
	 * Runs before bumping version numbers up to a new version
2790
	 * @param  (string) $version    Version:timestamp
2791
	 * @param  (string) $old_version Old Version:timestamp or false if not set yet.
2792
	 * @return null              [description]
2793
	 */
2794
	public static function do_version_bump( $version, $old_version ) {
2795
2796
		if ( ! $old_version ) { // For new sites
2797
			// Setting up jetpack manage
2798
			Jetpack::activate_manage();
2799
		}
2800
	}
2801
2802
	/**
2803
	 * Sets the internal version number and activation state.
2804
	 * @static
2805
	 */
2806
	public static function plugin_initialize() {
2807
		if ( ! Jetpack_Options::get_option( 'activated' ) ) {
2808
			Jetpack_Options::update_option( 'activated', 2 );
2809
		}
2810
2811 View Code Duplication
		if ( ! Jetpack_Options::get_option( 'version' ) ) {
2812
			$version = $old_version = JETPACK__VERSION . ':' . time();
2813
			/** This action is documented in class.jetpack.php */
2814
			do_action( 'updating_jetpack_version', $version, false );
2815
			Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
2816
		}
2817
2818
		Jetpack::load_modules();
2819
2820
		Jetpack_Options::delete_option( 'do_activate' );
2821
	}
2822
2823
	/**
2824
	 * Removes all connection options
2825
	 * @static
2826
	 */
2827
	public static function plugin_deactivation( ) {
2828
		require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
2829
		if( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
2830
			Jetpack_Network::init()->deactivate();
2831
		} else {
2832
			Jetpack::disconnect( false );
2833
			//Jetpack_Heartbeat::init()->deactivate();
2834
		}
2835
	}
2836
2837
	/**
2838
	 * Disconnects from the Jetpack servers.
2839
	 * Forgets all connection details and tells the Jetpack servers to do the same.
2840
	 * @static
2841
	 */
2842
	public static function disconnect( $update_activated_state = true ) {
2843
		wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
2844
		Jetpack::clean_nonces( true );
2845
2846
		Jetpack::load_xml_rpc_client();
2847
		$xml = new Jetpack_IXR_Client();
2848
		$xml->query( 'jetpack.deregister' );
2849
2850
		Jetpack_Options::delete_option(
2851
			array(
2852
				'register',
2853
				'blog_token',
2854
				'user_token',
2855
				'user_tokens',
2856
				'master_user',
2857
				'time_diff',
2858
				'fallback_no_verify_ssl_certs',
2859
			)
2860
		);
2861
2862
		if ( $update_activated_state ) {
2863
			Jetpack_Options::update_option( 'activated', 4 );
2864
		}
2865
2866
		$jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' );
2867
		// Check then record unique disconnection if site has never been disconnected previously
2868
		if ( -1 == $jetpack_unique_connection['disconnected'] ) {
2869
			$jetpack_unique_connection['disconnected'] = 1;
2870
		}
2871
		else {
2872
			if ( 0 == $jetpack_unique_connection['disconnected'] ) {
2873
				//track unique disconnect
2874
				$jetpack = Jetpack::init();
2875
2876
				$jetpack->stat( 'connections', 'unique-disconnect' );
2877
				$jetpack->do_stats( 'server_side' );
2878
			}
2879
			// increment number of times disconnected
2880
			$jetpack_unique_connection['disconnected'] += 1;
2881
		}
2882
2883
		Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection );
2884
2885
		// Disable the Heartbeat cron
2886
		Jetpack_Heartbeat::init()->deactivate();
2887
	}
2888
2889
	/**
2890
	 * Unlinks the current user from the linked WordPress.com user
2891
	 */
2892
	public static function unlink_user( $user_id = null ) {
2893
		if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) )
2894
			return false;
2895
2896
		$user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id );
2897
2898
		if ( Jetpack_Options::get_option( 'master_user' ) == $user_id )
2899
			return false;
2900
2901
		if ( ! isset( $tokens[ $user_id ] ) )
2902
			return false;
2903
2904
		Jetpack::load_xml_rpc_client();
2905
		$xml = new Jetpack_IXR_Client( compact( 'user_id' ) );
2906
		$xml->query( 'jetpack.unlink_user', $user_id );
2907
2908
		unset( $tokens[ $user_id ] );
2909
2910
		Jetpack_Options::update_option( 'user_tokens', $tokens );
2911
2912
		return true;
2913
	}
2914
2915
	/**
2916
	 * Attempts Jetpack registration.  If it fail, a state flag is set: @see ::admin_page_load()
2917
	 */
2918
	public static function try_registration() {
2919
		// Let's get some testing in beta versions and such.
2920
		if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) {
2921
			// Before attempting to connect, let's make sure that the domains are viable.
2922
			$domains_to_check = array_unique( array(
2923
				'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ),
2924
				'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ),
2925
			) );
2926
			foreach ( $domains_to_check as $domain ) {
2927
				$result = Jetpack_Data::is_usable_domain( $domain );
2928
				if ( is_wp_error( $result ) ) {
2929
					return $result;
2930
				}
2931
			}
2932
		}
2933
2934
		$result = Jetpack::register();
2935
2936
		// If there was an error with registration and the site was not registered, record this so we can show a message.
2937
		if ( ! $result || is_wp_error( $result ) ) {
2938
			return $result;
2939
		} else {
2940
			return true;
2941
		}
2942
	}
2943
2944
	/**
2945
	 * Tracking an internal event log. Try not to put too much chaff in here.
2946
	 *
2947
	 * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA)
2948
	 */
2949
	public static function log( $code, $data = null ) {
2950
		// only grab the latest 200 entries
2951
		$log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 );
2952
2953
		// Append our event to the log
2954
		$log_entry = array(
2955
			'time'    => time(),
2956
			'user_id' => get_current_user_id(),
2957
			'blog_id' => Jetpack_Options::get_option( 'id' ),
2958
			'code'    => $code,
2959
		);
2960
		// Don't bother storing it unless we've got some.
2961
		if ( ! is_null( $data ) ) {
2962
			$log_entry['data'] = $data;
2963
		}
2964
		$log[] = $log_entry;
2965
2966
		// Try add_option first, to make sure it's not autoloaded.
2967
		// @todo: Add an add_option method to Jetpack_Options
2968
		if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) {
2969
			Jetpack_Options::update_option( 'log', $log );
2970
		}
2971
2972
		/**
2973
		 * Fires when Jetpack logs an internal event.
2974
		 *
2975
		 * @since 3.0.0
2976
		 *
2977
		 * @param array $log_entry {
2978
		 *	Array of details about the log entry.
2979
		 *
2980
		 *	@param string time Time of the event.
2981
		 *	@param int user_id ID of the user who trigerred the event.
2982
		 *	@param int blog_id Jetpack Blog ID.
2983
		 *	@param string code Unique name for the event.
2984
		 *	@param string data Data about the event.
2985
		 * }
2986
		 */
2987
		do_action( 'jetpack_log_entry', $log_entry );
2988
	}
2989
2990
	/**
2991
	 * Get the internal event log.
2992
	 *
2993
	 * @param $event (string) - only return the specific log events
2994
	 * @param $num   (int)    - get specific number of latest results, limited to 200
2995
	 *
2996
	 * @return array of log events || WP_Error for invalid params
2997
	 */
2998
	public static function get_log( $event = false, $num = false ) {
2999
		if ( $event && ! is_string( $event ) ) {
3000
			return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) );
3001
		}
3002
3003
		if ( $num && ! is_numeric( $num ) ) {
3004
			return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) );
3005
		}
3006
3007
		$entire_log = Jetpack_Options::get_option( 'log', array() );
3008
3009
		// If nothing set - act as it did before, otherwise let's start customizing the output
3010
		if ( ! $num && ! $event ) {
3011
			return $entire_log;
3012
		} else {
3013
			$entire_log = array_reverse( $entire_log );
3014
		}
3015
3016
		$custom_log_output = array();
3017
3018
		if ( $event ) {
3019
			foreach ( $entire_log as $log_event ) {
3020
				if ( $event == $log_event[ 'code' ] ) {
3021
					$custom_log_output[] = $log_event;
3022
				}
3023
			}
3024
		} else {
3025
			$custom_log_output = $entire_log;
3026
		}
3027
3028
		if ( $num ) {
3029
			$custom_log_output = array_slice( $custom_log_output, 0, $num );
3030
		}
3031
3032
		return $custom_log_output;
3033
	}
3034
3035
	/**
3036
	 * Log modification of important settings.
3037
	 */
3038
	public static function log_settings_change( $option, $old_value, $value ) {
3039
		switch( $option ) {
3040
			case 'jetpack_sync_non_public_post_stati':
3041
				self::log( $option, $value );
3042
				break;
3043
		}
3044
	}
3045
3046
	/**
3047
	 * Return stat data for WPCOM sync
3048
	 */
3049
	function get_stat_data() {
3050
		$heartbeat_data = Jetpack_Heartbeat::generate_stats_array();
3051
		$additional_data = $this->get_additional_stat_data();
3052
3053
		return json_encode( array_merge( $heartbeat_data, $additional_data ) );
3054
	}
3055
3056
	/**
3057
	 * Get additional stat data to sync to WPCOM
3058
	 */
3059
	function get_additional_stat_data( $prefix = '' ) {
3060
		$return["{$prefix}themes"]         = Jetpack::get_parsed_theme_data();
3061
		$return["{$prefix}plugins-extra"]  = Jetpack::get_parsed_plugin_data();
3062
		$return["{$prefix}users"]          = count_users();
3063
		$return["{$prefix}site-count"]     = 0;
3064
		if ( function_exists( 'get_blog_count' ) ) {
3065
			$return["{$prefix}site-count"] = get_blog_count();
3066
		}
3067
		return $return;
3068
	}
3069
3070
	/* Admin Pages */
3071
3072
	function admin_init() {
3073
		// If the plugin is not connected, display a connect message.
3074
		if (
3075
			// the plugin was auto-activated and needs its candy
3076
			Jetpack_Options::get_option( 'do_activate' )
3077
		||
3078
			// the plugin is active, but was never activated.  Probably came from a site-wide network activation
3079
			! Jetpack_Options::get_option( 'activated' )
3080
		) {
3081
			Jetpack::plugin_initialize();
3082
		}
3083
3084
		if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
3085
			if ( 4 != Jetpack_Options::get_option( 'activated' ) ) {
3086
				// Show connect notice on dashboard and plugins pages
3087
				add_action( 'load-index.php', array( $this, 'prepare_connect_notice' ) );
3088
				add_action( 'load-plugins.php', array( $this, 'prepare_connect_notice' ) );
3089
			}
3090
		} elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) {
3091
			// Upgrade: 1.1 -> 1.1.1
3092
			// Check and see if host can verify the Jetpack servers' SSL certificate
3093
			$args = array();
3094
			Jetpack_Client::_wp_remote_request(
3095
				Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ) ),
3096
				$args,
3097
				true
3098
			);
3099
		} else {
3100
			// Show the notice on the Dashboard only for now
3101
3102
			add_action( 'load-index.php', array( $this, 'prepare_manage_jetpack_notice' ) );
3103
3104
			// Identity crisis notices
3105
			add_action( 'jetpack_notices', array( $this, 'alert_identity_crisis' ) );
3106
		}
3107
3108
		// If the plugin has just been disconnected from WP.com, show the survey notice
3109
		if ( isset( $_GET['disconnected'] ) && 'true' === $_GET['disconnected'] ) {
3110
			add_action( 'jetpack_notices', array( $this, 'disconnect_survey_notice' ) );
3111
		}
3112
3113
		if ( current_user_can( 'manage_options' ) && 'AUTO' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) {
3114
			add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
3115
		}
3116
3117
		add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
3118
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
3119
		add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) );
3120
3121
		if ( Jetpack::is_active() || Jetpack::is_development_mode() ) {
3122
			// Artificially throw errors in certain whitelisted cases during plugin activation
3123
			add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
3124
3125
			// Kick off synchronization of user role when it changes
3126
			add_action( 'set_user_role', array( $this, 'user_role_change' ) );
3127
		}
3128
3129
		// Jetpack Manage Activation Screen from .com
3130
		Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) );
3131
	}
3132
3133
	function admin_body_class( $admin_body_class = '' ) {
3134
		$classes = explode( ' ', trim( $admin_body_class ) );
3135
3136
		$classes[] = self::is_active() ? 'jetpack-connected' : 'jetpack-disconnected';
3137
3138
		$admin_body_class = implode( ' ', array_unique( $classes ) );
3139
		return " $admin_body_class ";
3140
	}
3141
3142
	static function add_jetpack_pagestyles( $admin_body_class = '' ) {
3143
		return $admin_body_class . ' jetpack-pagestyles ';
3144
	}
3145
3146
	function prepare_connect_notice() {
3147
		add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
3148
3149
		add_action( 'admin_notices', array( $this, 'admin_connect_notice' ) );
3150
3151
		if ( Jetpack::state( 'network_nag' ) )
3152
			add_action( 'network_admin_notices', array( $this, 'network_connect_notice' ) );
3153
	}
3154
	/**
3155
	 * Call this function if you want the Big Jetpack Manage Notice to show up.
3156
	 *
3157
	 * @return null
3158
	 */
3159
	function prepare_manage_jetpack_notice() {
3160
3161
		add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
3162
		add_action( 'admin_notices', array( $this, 'admin_jetpack_manage_notice' ) );
3163
	}
3164
3165
	function manage_activate_screen() {
3166
		include ( JETPACK__PLUGIN_DIR . 'modules/manage/activate-admin.php' );
3167
	}
3168
	/**
3169
	 * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
3170
	 * This function artificially throws errors for such cases (whitelisted).
3171
	 *
3172
	 * @param string $plugin The activated plugin.
3173
	 */
3174
	function throw_error_on_activate_plugin( $plugin ) {
3175
		$active_modules = Jetpack::get_active_modules();
3176
3177
		// The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks.
3178
		if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) {
3179
			$throw = false;
3180
3181
			// Try and make sure it really was the stats plugin
3182
			if ( ! class_exists( 'ReflectionFunction' ) ) {
3183
				if ( 'stats.php' == basename( $plugin ) ) {
3184
					$throw = true;
3185
				}
3186
			} else {
3187
				$reflection = new ReflectionFunction( 'stats_get_api_key' );
3188
				if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) {
3189
					$throw = true;
3190
				}
3191
			}
3192
3193
			if ( $throw ) {
3194
				trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR );
3195
			}
3196
		}
3197
	}
3198
3199
	function intercept_plugin_error_scrape_init() {
3200
		add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 );
3201
	}
3202
3203
	function intercept_plugin_error_scrape( $action, $result ) {
3204
		if ( ! $result ) {
3205
			return;
3206
		}
3207
3208
		foreach ( $this->plugins_to_deactivate as $deactivate_me ) {
3209
			if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) {
3210
				Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), $deactivate_me[1] ), false );
3211
			}
3212
		}
3213
	}
3214
3215
	function add_remote_request_handlers() {
3216
		add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) );
3217
	}
3218
3219
	function remote_request_handlers() {
3220
		switch ( current_filter() ) {
3221
		case 'wp_ajax_nopriv_jetpack_upload_file' :
3222
			$response = $this->upload_handler();
3223
			break;
3224
		default :
3225
			$response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 );
3226
			break;
3227
		}
3228
3229
		if ( ! $response ) {
3230
			$response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 );
3231
		}
3232
3233
		if ( is_wp_error( $response ) ) {
3234
			$status_code       = $response->get_error_data();
3235
			$error             = $response->get_error_code();
3236
			$error_description = $response->get_error_message();
3237
3238
			if ( ! is_int( $status_code ) ) {
3239
				$status_code = 400;
3240
			}
3241
3242
			status_header( $status_code );
3243
			die( json_encode( (object) compact( 'error', 'error_description' ) ) );
3244
		}
3245
3246
		status_header( 200 );
3247
		if ( true === $response ) {
3248
			exit;
3249
		}
3250
3251
		die( json_encode( (object) $response ) );
3252
	}
3253
3254
	function upload_handler() {
3255
		if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
3256
			return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 );
3257
		}
3258
3259
		$user = wp_authenticate( '', '' );
3260
		if ( ! $user || is_wp_error( $user ) ) {
3261
			return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 );
3262
		}
3263
3264
		wp_set_current_user( $user->ID );
3265
3266
		if ( ! current_user_can( 'upload_files' ) ) {
3267
			return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 );
3268
		}
3269
3270
		if ( empty( $_FILES ) ) {
3271
			return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 );
3272
		}
3273
3274
		foreach ( array_keys( $_FILES ) as $files_key ) {
3275
			if ( ! isset( $_POST["_jetpack_file_hmac_{$files_key}"] ) ) {
3276
				return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 );
3277
			}
3278
		}
3279
3280
		$media_keys = array_keys( $_FILES['media'] );
3281
3282
		$token = Jetpack_Data::get_access_token( get_current_user_id() );
3283
		if ( ! $token || is_wp_error( $token ) ) {
3284
			return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 );
3285
		}
3286
3287
		$uploaded_files = array();
3288
		$global_post    = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
3289
		unset( $GLOBALS['post'] );
3290
		foreach ( $_FILES['media']['name'] as $index => $name ) {
3291
			$file = array();
3292
			foreach ( $media_keys as $media_key ) {
3293
				$file[$media_key] = $_FILES['media'][$media_key][$index];
3294
			}
3295
3296
			list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][$index] );
3297
3298
			$hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret );
3299
			if ( $hmac_provided !== $hmac_file ) {
3300
				$uploaded_files[$index] = (object) array( 'error' => 'invalid_hmac', 'error_description' => 'The corresponding HMAC for this file does not match' );
3301
				continue;
3302
			}
3303
3304
			$_FILES['.jetpack.upload.'] = $file;
3305
			$post_id = isset( $_POST['post_id'][$index] ) ? absint( $_POST['post_id'][$index] ) : 0;
3306
			if ( ! current_user_can( 'edit_post', $post_id ) ) {
3307
				$post_id = 0;
3308
			}
3309
			$attachment_id = media_handle_upload(
3310
				'.jetpack.upload.',
3311
				$post_id,
3312
				array(),
3313
				array(
3314
					'action' => 'jetpack_upload_file',
3315
				)
3316
			);
3317
3318
			if ( ! $attachment_id ) {
3319
				$uploaded_files[$index] = (object) array( 'error' => 'unknown', 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site' );
3320
			} elseif ( is_wp_error( $attachment_id ) ) {
3321
				$uploaded_files[$index] = (object) array( 'error' => 'attachment_' . $attachment_id->get_error_code(), 'error_description' => $attachment_id->get_error_message() );
3322
			} else {
3323
				$attachment = get_post( $attachment_id );
3324
				$uploaded_files[$index] = (object) array(
3325
					'id'   => (string) $attachment_id,
3326
					'file' => $attachment->post_title,
3327
					'url'  => wp_get_attachment_url( $attachment_id ),
3328
					'type' => $attachment->post_mime_type,
3329
					'meta' => wp_get_attachment_metadata( $attachment_id ),
3330
				);
3331
			}
3332
		}
3333
		if ( ! is_null( $global_post ) ) {
3334
			$GLOBALS['post'] = $global_post;
3335
		}
3336
3337
		return $uploaded_files;
3338
	}
3339
3340
	/**
3341
	 * Add help to the Jetpack page
3342
	 *
3343
	 * @since Jetpack (1.2.3)
3344
	 * @return false if not the Jetpack page
3345
	 */
3346
	function admin_help() {
3347
		$current_screen = get_current_screen();
3348
3349
		// Overview
3350
		$current_screen->add_help_tab(
3351
			array(
3352
				'id'		=> 'home',
3353
				'title'		=> __( 'Home', 'jetpack' ),
3354
				'content'	=>
3355
					'<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' .
3356
					'<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' .
3357
					'<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>',
3358
			)
3359
		);
3360
3361
		// Screen Content
3362
		if ( current_user_can( 'manage_options' ) ) {
3363
			$current_screen->add_help_tab(
3364
				array(
3365
					'id'		=> 'settings',
3366
					'title'		=> __( 'Settings', 'jetpack' ),
3367
					'content'	=>
3368
						'<p><strong>' . __( 'Jetpack by WordPress.com',                                              'jetpack' ) . '</strong></p>' .
3369
						'<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' .
3370
						'<ol>' .
3371
							'<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.',														'jetpack' ) . '</li>' .
3372
							'<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>' .
3373
						'</ol>' .
3374
						'<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>'
3375
				)
3376
			);
3377
		}
3378
3379
		// Help Sidebar
3380
		$current_screen->set_help_sidebar(
3381
			'<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' .
3382
			'<p><a href="http://jetpack.com/faq/" target="_blank">'     . __( 'Jetpack FAQ',     'jetpack' ) . '</a></p>' .
3383
			'<p><a href="http://jetpack.com/support/" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' .
3384
			'<p><a href="' . Jetpack::admin_url( array( 'page' => 'jetpack-debugger' )  ) .'">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>'
3385
		);
3386
	}
3387
3388
	function admin_menu_css() {
3389
		wp_enqueue_style( 'jetpack-icons' );
3390
	}
3391
3392
	function admin_menu_order() {
3393
		return true;
3394
	}
3395
3396 View Code Duplication
	function jetpack_menu_order( $menu_order ) {
3397
		$jp_menu_order = array();
3398
3399
		foreach ( $menu_order as $index => $item ) {
3400
			if ( $item != 'jetpack' ) {
3401
				$jp_menu_order[] = $item;
3402
			}
3403
3404
			if ( $index == 0 ) {
3405
				$jp_menu_order[] = 'jetpack';
3406
			}
3407
		}
3408
3409
		return $jp_menu_order;
3410
	}
3411
3412
	function admin_head() {
3413 View Code Duplication
		if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) )
3414
			/** This action is documented in class.jetpack-admin-page.php */
3415
			do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
3416
	}
3417
3418
	function admin_banner_styles() {
3419
		$min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
3420
3421
		wp_enqueue_style( 'jetpack', plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION . '-20121016' );
3422
		wp_style_add_data( 'jetpack', 'rtl', 'replace' );
3423
		wp_style_add_data( 'jetpack', 'suffix', $min );
3424
	}
3425
3426
	function admin_scripts() {
3427
		wp_enqueue_script( 'jetpack-js', plugins_url( '_inc/jp.js', JETPACK__PLUGIN_FILE ), array( 'jquery', 'wp-util' ), JETPACK__VERSION . '-20121111' );
3428
		wp_localize_script(
3429
			'jetpack-js',
3430
			'jetpackL10n',
3431
			array(
3432
				'ays_disconnect' => "This will deactivate all Jetpack modules.\nAre you sure you want to disconnect?",
3433
				'ays_unlink'     => "This will prevent user-specific modules such as Publicize, Notifications and Post By Email from working.\nAre you sure you want to unlink?",
3434
				'ays_dismiss'    => "This will deactivate Jetpack.\nAre you sure you want to deactivate Jetpack?",
3435
			)
3436
		);
3437
		add_action( 'admin_footer', array( $this, 'do_stats' ) );
3438
	}
3439
3440
	function plugin_action_links( $actions ) {
3441
3442
		$jetpack_home = array( 'jetpack-home' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack' ), __( 'Jetpack', 'jetpack' ) ) );
3443
3444
		if( current_user_can( 'jetpack_manage_modules' ) && ( Jetpack::is_active() || Jetpack::is_development_mode() ) ) {
3445
			return array_merge(
3446
				$jetpack_home,
3447
				array( 'settings' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack_modules' ), __( 'Settings', 'jetpack' ) ) ),
3448
				array( 'support' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack-debugger '), __( 'Support', 'jetpack' ) ) ),
3449
				$actions
3450
				);
3451
			}
3452
3453
		return array_merge( $jetpack_home, $actions );
3454
	}
3455
3456
	function admin_connect_notice() {
3457
		// Don't show the connect notice anywhere but the plugins.php after activating
3458
		$current = get_current_screen();
3459
		if ( 'plugins' !== $current->parent_base )
3460
			return;
3461
3462
		if ( ! current_user_can( 'jetpack_connect' ) )
3463
			return;
3464
3465
		$dismiss_and_deactivate_url = wp_nonce_url( Jetpack::admin_url( '?page=jetpack&jetpack-notice=dismiss' ), 'jetpack-deactivate' );
3466
		?>
3467
		<div id="message" class="updated jetpack-message jp-banner" style="display:block !important;">
3468
			<a class="jp-banner__dismiss" href="<?php echo esc_url( $dismiss_and_deactivate_url ); ?>" title="<?php esc_attr_e( 'Dismiss this notice and deactivate Jetpack.', 'jetpack' ); ?>"></a>
3469
			<?php if ( in_array( Jetpack_Options::get_option( 'activated' ) , array( 1, 2, 3 ) ) ) : ?>
3470
				<div class="jp-banner__content is-connection">
3471
					<h2><?php _e( 'Your Jetpack is almost ready!', 'jetpack' ); ?></h2>
3472
					<p><?php _e( 'Connect now to enable features like Stats, Likes, and Social Sharing.', 'jetpack' ); ?></p>
3473
				</div>
3474
				<div class="jp-banner__action-container is-connection">
3475
						<a href="<?php echo $this->build_connect_url( false, false, 'banner' ) ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Connect to WordPress.com', 'jetpack' ); ?></a>
3476
				</div>
3477 View Code Duplication
			<?php else : ?>
3478
				<div class="jp-banner__content">
3479
					<h2><?php _e( 'Jetpack is installed!', 'jetpack' ) ?></h2>
3480
					<p><?php _e( 'It\'s ready to bring awesome, WordPress.com cloud-powered features to your site.', 'jetpack' ) ?></p>
3481
				</div>
3482
				<div class="jp-banner__action-container">
3483
					<a href="<?php echo Jetpack::admin_url() ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Learn More', 'jetpack' ); ?></a>
3484
				</div>
3485
			<?php endif; ?>
3486
		</div>
3487
3488
		<?php
3489
	}
3490
3491
	/**
3492
	 * This is the first banner
3493
	 * It should be visible only to user that can update the option
3494
	 * Are not connected
3495
	 *
3496
	 * @return null
3497
	 */
3498
	function admin_jetpack_manage_notice() {
3499
		$screen = get_current_screen();
3500
3501
		// Don't show the connect notice on the jetpack settings page.
3502
		if ( ! in_array( $screen->base, array( 'dashboard' ) ) || $screen->is_network || $screen->action )
3503
			return;
3504
3505
		// Only show it if don't have the managment option set.
3506
		// And not dismissed it already.
3507
		if ( ! $this->can_display_jetpack_manage_notice() || Jetpack_Options::get_option( 'dismissed_manage_banner' ) ) {
3508
			return;
3509
		}
3510
3511
		$opt_out_url = $this->opt_out_jetpack_manage_url();
3512
		$opt_in_url  = $this->opt_in_jetpack_manage_url();
3513
		/**
3514
		 * I think it would be great to have different wordsing depending on where you are
3515
		 * for example if we show the notice on dashboard and a different one if we show it on Plugins screen
3516
		 * etc..
3517
		 */
3518
3519
		?>
3520
		<div id="message" class="updated jetpack-message jp-banner is-opt-in" style="display:block !important;">
3521
			<a class="jp-banner__dismiss" href="<?php echo esc_url( $opt_out_url ); ?>" title="<?php esc_attr_e( 'Dismiss this notice for now.', 'jetpack' ); ?>"></a>
3522
			<div class="jp-banner__content">
3523
				<h2><?php esc_html_e( 'New in Jetpack: Centralized Site Management', 'jetpack' ); ?></h2>
3524
				<p><?php printf( __( 'Manage multiple sites from one dashboard at wordpress.com/sites. Enabling allows all existing, connected Administrators to modify your site from WordPress.com. <a href="%s" target="_blank">Learn More</a>.', 'jetpack' ), 'http://jetpack.com/support/site-management' ); ?></p>
3525
			</div>
3526
			<div class="jp-banner__action-container is-opt-in">
3527
				<a href="<?php echo esc_url( $opt_in_url ); ?>" class="jp-banner__button" id="wpcom-connect"><?php _e( 'Activate now', 'jetpack' ); ?></a>
3528
			</div>
3529
		</div>
3530
		<?php
3531
	}
3532
3533
	/**
3534
	 * Returns the url that the user clicks to remove the notice for the big banner
3535
	 * @return (string)
3536
	 */
3537
	function opt_out_jetpack_manage_url() {
3538
		$referer = '&_wp_http_referer=' . add_query_arg( '_wp_http_referer', null );
3539
		return wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-manage-opt-out' . $referer ), 'jetpack_manage_banner_opt_out' );
3540
	}
3541
	/**
3542
	 * Returns the url that the user clicks to opt in to Jetpack Manage
3543
	 * @return (string)
3544
	 */
3545
	function opt_in_jetpack_manage_url() {
3546
		return wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-manage-opt-in' ), 'jetpack_manage_banner_opt_in' );
3547
	}
3548
3549
	function opt_in_jetpack_manage_notice() {
3550
		?>
3551
		<div class="wrap">
3552
			<div id="message" class="jetpack-message is-opt-in">
3553
				<?php echo sprintf( __( '<p><a href="%1$s" title="Opt in to WordPress.com Site Management" >Activate Site Management</a> to manage multiple sites from our centralized dashboard at wordpress.com/sites. <a href="%2$s" target="_blank">Learn more</a>.</p><a href="%1$s" class="jp-button">Activate Now</a>', 'jetpack' ), $this->opt_in_jetpack_manage_url(), 'http://jetpack.com/support/site-management' ); ?>
3554
			</div>
3555
		</div>
3556
		<?php
3557
3558
	}
3559
	/**
3560
	 * Determines whether to show the notice of not true = display notice
3561
	 * @return (bool)
3562
	 */
3563
	function can_display_jetpack_manage_notice() {
3564
		// never display the notice to users that can't do anything about it anyways
3565
		if( ! current_user_can( 'jetpack_manage_modules' ) )
3566
			return false;
3567
3568
		// don't display if we are in development more
3569
		if( Jetpack::is_development_mode() ) {
3570
			return false;
3571
		}
3572
		// don't display if the site is private
3573
		if(  ! Jetpack_Options::get_option( 'public' ) )
3574
			return false;
3575
3576
		/**
3577
		 * Should the Jetpack Remote Site Management notice be displayed.
3578
		 *
3579
		 * @since 3.3.0
3580
		 *
3581
		 * @param bool ! self::is_module_active( 'manage' ) Is the Manage module inactive.
3582
		 */
3583
		return apply_filters( 'can_display_jetpack_manage_notice', ! self::is_module_active( 'manage' ) );
3584
	}
3585
3586
	function network_connect_notice() {
3587
		?>
3588
		<div id="message" class="updated jetpack-message">
3589
			<div class="squeezer">
3590
				<h2><?php _e( '<strong>Jetpack is activated!</strong> Each site on your network must be connected individually by an admin on that site.', 'jetpack' ) ?></h2>
3591
			</div>
3592
		</div>
3593
		<?php
3594
	}
3595
3596
	public static function jetpack_comment_notice() {
3597
		if ( in_array( 'comments', Jetpack::get_active_modules() ) ) {
3598
			return '';
3599
		}
3600
3601
		$jetpack_old_version = explode( ':', Jetpack_Options::get_option( 'old_version' ) );
3602
		$jetpack_new_version = explode( ':', Jetpack_Options::get_option( 'version' ) );
3603
3604
		if ( $jetpack_old_version ) {
3605
			if ( version_compare( $jetpack_old_version[0], '1.4', '>=' ) ) {
3606
				return '';
3607
			}
3608
		}
3609
3610
		if ( $jetpack_new_version ) {
3611
			if ( version_compare( $jetpack_new_version[0], '1.4-something', '<' ) ) {
3612
				return '';
3613
			}
3614
		}
3615
3616
		return '<br /><br />' . sprintf(
3617
			__( 'Jetpack now includes Comments, which enables your visitors to use their WordPress.com, Twitter, or Facebook accounts when commenting on your site. To activate Comments, <a href="%s">%s</a>.', 'jetpack' ),
3618
			wp_nonce_url(
3619
				Jetpack::admin_url(
3620
					array(
3621
						'page'   => 'jetpack',
3622
						'action' => 'activate',
3623
						'module' => 'comments',
3624
					)
3625
				),
3626
				'jetpack_activate-comments'
3627
			),
3628
			__( 'click here', 'jetpack' )
3629
		);
3630
	}
3631
3632
	/**
3633
	 * Show the survey link when the user has just disconnected Jetpack.
3634
	 */
3635
	function disconnect_survey_notice() {
3636
		?>
3637
		<div class="wrap">
3638
			<div id="message" class="jetpack-message stay-visible">
3639
				<div class="squeezer">
3640
					<h2>
3641
						<?php _e( 'You have successfully disconnected Jetpack.', 'jetpack' ); ?>
3642
						<br />
3643
						<?php echo sprintf(
3644
							__( 'Would you tell us why? Just <a href="%1$s" target="%2$s">answering two simple questions</a> would help us improve Jetpack.', 'jetpack' ),
3645
							'https://jetpack.com/survey-disconnected/',
3646
							'_blank'
3647
						); ?>
3648
					</h2>
3649
				</div>
3650
			</div>
3651
		</div>
3652
		<?php
3653
	}
3654
3655
	/*
3656
	 * Registration flow:
3657
	 * 1 - ::admin_page_load() action=register
3658
	 * 2 - ::try_registration()
3659
	 * 3 - ::register()
3660
	 *     - Creates jetpack_register option containing two secrets and a timestamp
3661
	 *     - Calls https://jetpack.wordpress.com/jetpack.register/1/ with
3662
	 *       siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id
3663
	 *     - That request to jetpack.wordpress.com does not immediately respond.  It first makes a request BACK to this site's
3664
	 *       xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1
3665
	 *     - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2
3666
	 *     - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with
3667
	 *       jetpack_id, jetpack_secret, jetpack_public
3668
	 *     - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret
3669
	 * 4 - redirect to https://wordpress.com/start/jetpack-connect
3670
	 * 5 - user logs in with WP.com account
3671
	 * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize
3672
	 *		- Jetpack_Client_Server::authorize()
3673
	 *		- Jetpack_Client_Server::get_token()
3674
	 *		- GET https://jetpack.wordpress.com/jetpack.token/1/ with
3675
	 *        client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login
3676
	 *			- which responds with access_token, token_type, scope
3677
	 *		- Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id
3678
	 *		- Jetpack::activate_default_modules()
3679
	 *     		- Deactivates deprecated plugins
3680
	 *     		- Activates all default modules
3681
	 *		- Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users
3682
	 * 7 - For a new connection, user selects a Jetpack plan on wordpress.com
3683
	 * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized
3684
	 *     Done!
3685
	 */
3686
3687
	/**
3688
	 * Handles the page load events for the Jetpack admin page
3689
	 */
3690
	function admin_page_load() {
3691
		$error = false;
3692
3693
		// Make sure we have the right body class to hook stylings for subpages off of.
3694
		add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ) );
3695
3696
		if ( ! empty( $_GET['jetpack_restate'] ) ) {
3697
			// Should only be used in intermediate redirects to preserve state across redirects
3698
			Jetpack::restate();
3699
		}
3700
3701
		if ( isset( $_GET['connect_url_redirect'] ) ) {
3702
			// User clicked in the iframe to link their accounts
3703
			if ( ! Jetpack::is_user_connected() ) {
3704
				$connect_url = $this->build_connect_url( true, false, 'iframe' );
3705
				if ( isset( $_GET['notes_iframe'] ) )
3706
					$connect_url .= '&notes_iframe';
3707
				wp_redirect( $connect_url );
3708
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3709
			} else {
3710
				Jetpack::state( 'message', 'already_authorized' );
3711
				wp_safe_redirect( Jetpack::admin_url() );
3712
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3713
			}
3714
		}
3715
3716
3717
		if ( isset( $_GET['action'] ) ) {
3718
			switch ( $_GET['action'] ) {
3719
			case 'authorize':
3720
				if ( Jetpack::is_active() && Jetpack::is_user_connected() ) {
3721
					Jetpack::state( 'message', 'already_authorized' );
3722
					wp_safe_redirect( Jetpack::admin_url() );
3723
					exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3724
				}
3725
				Jetpack::log( 'authorize' );
3726
				$client_server = new Jetpack_Client_Server;
3727
				$client_server->client_authorize();
3728
				exit;
3729
			case 'register' :
3730
				if ( ! current_user_can( 'jetpack_connect' ) ) {
3731
					$error = 'cheatin';
3732
					break;
3733
				}
3734
				check_admin_referer( 'jetpack-register' );
3735
				Jetpack::log( 'register' );
3736
				Jetpack::maybe_set_version_option();
3737
				$registered = Jetpack::try_registration();
3738
				if ( is_wp_error( $registered ) ) {
3739
					$error = $registered->get_error_code();
3740
					Jetpack::state( 'error_description', $registered->get_error_message() );
3741
					break;
3742
				}
3743
3744
				$from = isset( $_GET['from'] ) ? $_GET['from'] : false;
3745
3746
				wp_redirect( $this->build_connect_url( true, false, $from ) );
3747
				exit;
3748
			case 'activate' :
3749
				if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
3750
					$error = 'cheatin';
3751
					break;
3752
				}
3753
3754
				$module = stripslashes( $_GET['module'] );
3755
				check_admin_referer( "jetpack_activate-$module" );
3756
				Jetpack::log( 'activate', $module );
3757
				Jetpack::activate_module( $module );
3758
				// The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
3759
				wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
3760
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3761
			case 'activate_default_modules' :
3762
				check_admin_referer( 'activate_default_modules' );
3763
				Jetpack::log( 'activate_default_modules' );
3764
				Jetpack::restate();
3765
				$min_version   = isset( $_GET['min_version'] ) ? $_GET['min_version'] : false;
3766
				$max_version   = isset( $_GET['max_version'] ) ? $_GET['max_version'] : false;
3767
				$other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? $_GET['other_modules'] : array();
3768
				Jetpack::activate_default_modules( $min_version, $max_version, $other_modules );
3769
				wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
3770
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3771
			case 'disconnect' :
3772
				if ( ! current_user_can( 'jetpack_disconnect' ) ) {
3773
					$error = 'cheatin';
3774
					break;
3775
				}
3776
3777
				check_admin_referer( 'jetpack-disconnect' );
3778
				Jetpack::log( 'disconnect' );
3779
				Jetpack::disconnect();
3780
				wp_safe_redirect( Jetpack::admin_url( 'disconnected=true' ) );
3781
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3782
			case 'reconnect' :
3783
				if ( ! current_user_can( 'jetpack_reconnect' ) ) {
3784
					$error = 'cheatin';
3785
					break;
3786
				}
3787
3788
				check_admin_referer( 'jetpack-reconnect' );
3789
				Jetpack::log( 'reconnect' );
3790
				$this->disconnect();
3791
				wp_redirect( $this->build_connect_url( true, false, 'reconnect' ) );
3792
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3793 View Code Duplication
			case 'deactivate' :
3794
				if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
3795
					$error = 'cheatin';
3796
					break;
3797
				}
3798
3799
				$modules = stripslashes( $_GET['module'] );
3800
				check_admin_referer( "jetpack_deactivate-$modules" );
3801
				foreach ( explode( ',', $modules ) as $module ) {
3802
					Jetpack::log( 'deactivate', $module );
3803
					Jetpack::deactivate_module( $module );
3804
					Jetpack::state( 'message', 'module_deactivated' );
3805
				}
3806
				Jetpack::state( 'module', $modules );
3807
				wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
3808
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3809
			case 'unlink' :
3810
				$redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : '';
3811
				check_admin_referer( 'jetpack-unlink' );
3812
				Jetpack::log( 'unlink' );
3813
				$this->unlink_user();
3814
				Jetpack::state( 'message', 'unlinked' );
3815
				if ( 'sub-unlink' == $redirect ) {
3816
					wp_safe_redirect( admin_url() );
3817
				} else {
3818
					wp_safe_redirect( Jetpack::admin_url( array( 'page' => $redirect ) ) );
3819
				}
3820
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_page_load() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
3821
			default:
3822
				/**
3823
				 * Fires when a Jetpack admin page is loaded with an unrecognized parameter.
3824
				 *
3825
				 * @since 2.6.0
3826
				 *
3827
				 * @param string sanitize_key( $_GET['action'] ) Unrecognized URL parameter.
3828
				 */
3829
				do_action( 'jetpack_unrecognized_action', sanitize_key( $_GET['action'] ) );
3830
			}
3831
		}
3832
3833
		if ( ! $error = $error ? $error : Jetpack::state( 'error' ) ) {
3834
			self::activate_new_modules( true );
3835
		}
3836
3837
		switch ( $error ) {
3838
		case 'cheatin' :
3839
			$this->error = __( 'Cheatin&#8217; uh?', 'jetpack' );
3840
			break;
3841
		case 'access_denied' :
3842
			$this->error = sprintf( __( 'Would you mind telling us why you did not complete the Jetpack connection in this <a href="%s">1 question survey</a>?', 'jetpack' ), 'http://jetpack.com/cancelled-connection/' ) . '<br /><small>' . __( 'A Jetpack connection is required for our free security and traffic features to work.', 'jetpack' ) . '</small>';
3843
			break;
3844
		case 'wrong_state' :
3845
			$this->error = __( 'You need to stay logged in to your WordPress blog while you authorize Jetpack.', 'jetpack' );
3846
			break;
3847
		case 'invalid_client' :
3848
			// @todo re-register instead of deactivate/reactivate
3849
			$this->error = __( 'We had an issue connecting Jetpack; deactivate then reactivate the Jetpack plugin, then connect again.', 'jetpack' );
3850
			break;
3851
		case 'invalid_grant' :
3852
			$this->error = __( 'There was an issue connecting your Jetpack. Please click &#8220;Connect to WordPress.com&#8221; again.', 'jetpack' );
3853
			break;
3854
		case 'site_inaccessible' :
3855
		case 'site_requires_authorization' :
3856
			$this->error = sprintf( __( 'Your website needs to be publicly accessible to use Jetpack: %s', 'jetpack' ), "<code>$error</code>" );
3857
			break;
3858
		case 'module_activation_failed' :
3859
			$module = Jetpack::state( 'module' );
3860
			if ( ! empty( $module ) && $mod = Jetpack::get_module( $module ) ) {
3861
				$this->error = sprintf( __( '%s could not be activated because it triggered a <strong>fatal error</strong>. Perhaps there is a conflict with another plugin you have installed?', 'jetpack' ), $mod['name'] );
3862
				if ( isset( $this->plugins_to_deactivate[$module] ) ) {
3863
					$this->error .= ' ' . sprintf( __( 'Do you still have the %s plugin installed?', 'jetpack' ), $this->plugins_to_deactivate[$module][1] );
3864
				}
3865
			} else {
3866
				$this->error = __( 'Module could not be activated because it triggered a <strong>fatal error</strong>. Perhaps there is a conflict with another plugin you have installed?', 'jetpack' );
3867
			}
3868
			if ( $php_errors = Jetpack::state( 'php_errors' ) ) {
3869
				$this->error .= "<br />\n";
3870
				$this->error .= $php_errors;
3871
			}
3872
			break;
3873
		case 'master_user_required' :
3874
			$module = Jetpack::state( 'module' );
3875
			$module_name = '';
3876
			if ( ! empty( $module ) && $mod = Jetpack::get_module( $module ) ) {
3877
				$module_name = $mod['name'];
3878
			}
3879
3880
			$master_user = Jetpack_Options::get_option( 'master_user' );
3881
			$master_userdata = get_userdata( $master_user ) ;
3882
			if ( $master_userdata ) {
3883
				if ( ! in_array( $module, Jetpack::get_active_modules() ) ) {
3884
					$this->error = sprintf( __( '%s was not activated.' , 'jetpack' ), $module_name );
3885
				} else {
3886
					$this->error = sprintf( __( '%s was not deactivated.' , 'jetpack' ), $module_name );
3887
				}
3888
				$this->error .= '  ' . sprintf( __( 'This module can only be altered by %s, the user who initiated the Jetpack connection on this site.' , 'jetpack' ), esc_html( $master_userdata->display_name ) );
3889
3890
			} else {
3891
				$this->error = sprintf( __( 'Only the user who initiated the Jetpack connection on this site can toggle %s, but that user no longer exists. This should not happen.', 'jetpack' ), $module_name );
3892
			}
3893
			break;
3894
		case 'not_public' :
3895
			$this->error = __( '<strong>Your Jetpack has a glitch.</strong> Connecting this site with WordPress.com is not possible. This usually means your site is not publicly accessible (localhost).', 'jetpack' );
3896
			break;
3897
		case 'wpcom_408' :
3898
		case 'wpcom_5??' :
3899
		case 'wpcom_bad_response' :
3900
		case 'wpcom_outage' :
3901
			$this->error = __( 'WordPress.com is currently having problems and is unable to fuel up your Jetpack.  Please try again later.', 'jetpack' );
3902
			break;
3903
		case 'register_http_request_failed' :
3904
		case 'token_http_request_failed' :
3905
			$this->error = sprintf( __( 'Jetpack could not contact WordPress.com: %s.  This usually means something is incorrectly configured on your web host.', 'jetpack' ), "<code>$error</code>" );
3906
			break;
3907
		default :
3908
			if ( empty( $error ) ) {
3909
				break;
3910
			}
3911
			$error = trim( substr( strip_tags( $error ), 0, 20 ) );
3912
			// no break: fall through
3913
		case 'no_role' :
3914
		case 'no_cap' :
3915
		case 'no_code' :
3916
		case 'no_state' :
3917
		case 'invalid_state' :
3918
		case 'invalid_request' :
3919
		case 'invalid_scope' :
3920
		case 'unsupported_response_type' :
3921
		case 'invalid_token' :
3922
		case 'no_token' :
3923
		case 'missing_secrets' :
3924
		case 'home_missing' :
3925
		case 'siteurl_missing' :
3926
		case 'gmt_offset_missing' :
3927
		case 'site_name_missing' :
3928
		case 'secret_1_missing' :
3929
		case 'secret_2_missing' :
3930
		case 'site_lang_missing' :
3931
		case 'home_malformed' :
3932
		case 'siteurl_malformed' :
3933
		case 'gmt_offset_malformed' :
3934
		case 'timezone_string_malformed' :
3935
		case 'site_name_malformed' :
3936
		case 'secret_1_malformed' :
3937
		case 'secret_2_malformed' :
3938
		case 'site_lang_malformed' :
3939
		case 'secrets_mismatch' :
3940
		case 'verify_secret_1_missing' :
3941
		case 'verify_secret_1_malformed' :
3942
		case 'verify_secrets_missing' :
3943
		case 'verify_secrets_mismatch' :
3944
			$error = esc_html( $error );
3945
			$this->error = sprintf( __( '<strong>Your Jetpack has a glitch.</strong>  We&#8217;re sorry for the inconvenience. Please try again later, if the issue continues please contact support with this message: %s', 'jetpack' ), "<code>$error</code>" );
3946
			if ( ! Jetpack::is_active() ) {
3947
				$this->error .= '<br />';
3948
				$this->error .= sprintf( __( 'Try connecting again.', 'jetpack' ) );
3949
			}
3950
			break;
3951
		}
3952
3953
		$message_code = Jetpack::state( 'message' );
3954
3955
		$active_state = Jetpack::state( 'activated_modules' );
3956
		if ( ! empty( $active_state ) ) {
3957
			$available    = Jetpack::get_available_modules();
3958
			$active_state = explode( ',', $active_state );
3959
			$active_state = array_intersect( $active_state, $available );
3960
			if ( count( $active_state ) ) {
3961
				foreach ( $active_state as $mod ) {
3962
					$this->stat( 'module-activated', $mod );
3963
				}
3964
			} else {
3965
				$active_state = false;
3966
			}
3967
		}
3968
		if( Jetpack::state( 'optin-manage' ) ) {
3969
			$activated_manage = $message_code;
3970
			$message_code = 'jetpack-manage';
3971
3972
		}
3973
		switch ( $message_code ) {
3974
		case 'modules_activated' :
3975
			$this->message = sprintf(
3976
				__( 'Welcome to <strong>Jetpack %s</strong>!', 'jetpack' ),
3977
				JETPACK__VERSION
3978
			);
3979
3980
			if ( $active_state ) {
3981
				$titles = array();
3982 View Code Duplication
				foreach ( $active_state as $mod ) {
3983
					if ( $mod_headers = Jetpack::get_module( $mod ) ) {
3984
						$titles[] = '<strong>' . preg_replace( '/\s+(?![^<>]++>)/', '&nbsp;', $mod_headers['name'] ) . '</strong>';
3985
					}
3986
				}
3987
				if ( $titles ) {
3988
					$this->message .= '<br /><br />' . wp_sprintf( __( 'The following new modules have been activated: %l.', 'jetpack' ), $titles );
3989
				}
3990
			}
3991
3992
			if ( $reactive_state = Jetpack::state( 'reactivated_modules' ) ) {
3993
				$titles = array();
3994 View Code Duplication
				foreach ( explode( ',',  $reactive_state ) as $mod ) {
3995
					if ( $mod_headers = Jetpack::get_module( $mod ) ) {
3996
						$titles[] = '<strong>' . preg_replace( '/\s+(?![^<>]++>)/', '&nbsp;', $mod_headers['name'] ) . '</strong>';
3997
					}
3998
				}
3999
				if ( $titles ) {
4000
					$this->message .= '<br /><br />' . wp_sprintf( __( 'The following modules have been updated: %l.', 'jetpack' ), $titles );
4001
				}
4002
			}
4003
4004
			$this->message .= Jetpack::jetpack_comment_notice();
4005
			break;
4006
		case 'jetpack-manage':
4007
			$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>';
4008
			if ( $activated_manage ) {
4009
				$this->message .= '<br /><strong>' . __( 'Manage has been activated for you!', 'jetpack'  ) . '</strong>';
4010
			}
4011
			break;
4012
		case 'module_activated' :
4013
			if ( $module = Jetpack::get_module( Jetpack::state( 'module' ) ) ) {
4014
				$this->message = sprintf( __( '<strong>%s Activated!</strong> You can deactivate at any time by clicking the Deactivate link next to each module.', 'jetpack' ), $module['name'] );
4015
				$this->stat( 'module-activated', Jetpack::state( 'module' ) );
4016
			}
4017
			break;
4018
4019
		case 'module_deactivated' :
4020
			$modules = Jetpack::state( 'module' );
4021
			if ( ! $modules ) {
4022
				break;
4023
			}
4024
4025
			$module_names = array();
4026
			foreach ( explode( ',', $modules ) as $module_slug ) {
4027
				$module = Jetpack::get_module( $module_slug );
4028
				if ( $module ) {
4029
					$module_names[] = $module['name'];
4030
				}
4031
4032
				$this->stat( 'module-deactivated', $module_slug );
4033
			}
4034
4035
			if ( ! $module_names ) {
4036
				break;
4037
			}
4038
4039
			$this->message = wp_sprintf(
4040
				_nx(
4041
					'<strong>%l Deactivated!</strong> You can activate it again at any time using the activate link next to each module.',
4042
					'<strong>%l Deactivated!</strong> You can activate them again at any time using the activate links next to each module.',
4043
					count( $module_names ),
4044
					'%l = list of Jetpack module/feature names',
4045
					'jetpack'
4046
				),
4047
				$module_names
4048
			);
4049
			break;
4050
4051
		case 'module_configured' :
4052
			$this->message = __( '<strong>Module settings were saved.</strong> ', 'jetpack' );
4053
			break;
4054
4055
		case 'already_authorized' :
4056
			$this->message = __( '<strong>Your Jetpack is already connected.</strong> ', 'jetpack' );
4057
			break;
4058
4059
		case 'authorized' :
4060
			$this->message  = __( '<strong>You&#8217;re fueled up and ready to go, Jetpack is now active.</strong> ', 'jetpack' );
4061
			$this->message .= Jetpack::jetpack_comment_notice();
4062
			break;
4063
4064
		case 'linked' :
4065
			$this->message  = __( '<strong>You&#8217;re fueled up and ready to go.</strong> ', 'jetpack' );
4066
			$this->message .= Jetpack::jetpack_comment_notice();
4067
			break;
4068
4069
		case 'unlinked' :
4070
			$user = wp_get_current_user();
4071
			$this->message = sprintf( __( '<strong>You have unlinked your account (%s) from WordPress.com.</strong>', 'jetpack' ), $user->user_login );
4072
			break;
4073
4074
		case 'switch_master' :
4075
			global $current_user;
4076
			$is_master_user = $current_user->ID == Jetpack_Options::get_option( 'master_user' );
4077
			$master_userdata = get_userdata( Jetpack_Options::get_option( 'master_user' ) );
4078
			if ( $is_master_user ) {
4079
				$this->message = __( 'You have successfully set yourself as Jetpack’s primary user.', 'jetpack' );
4080
			} else {
4081
				$this->message = sprintf( _x( 'You have successfully set %s as Jetpack’s primary user.', '%s is a username', 'jetpack' ), $master_userdata->user_login );
4082
			}
4083
			break;
4084
		}
4085
4086
		$deactivated_plugins = Jetpack::state( 'deactivated_plugins' );
4087
4088
		if ( ! empty( $deactivated_plugins ) ) {
4089
			$deactivated_plugins = explode( ',', $deactivated_plugins );
4090
			$deactivated_titles  = array();
4091
			foreach ( $deactivated_plugins as $deactivated_plugin ) {
4092
				if ( ! isset( $this->plugins_to_deactivate[$deactivated_plugin] ) ) {
4093
					continue;
4094
				}
4095
4096
				$deactivated_titles[] = '<strong>' . str_replace( ' ', '&nbsp;', $this->plugins_to_deactivate[$deactivated_plugin][1] ) . '</strong>';
4097
			}
4098
4099
			if ( $deactivated_titles ) {
4100
				if ( $this->message ) {
4101
					$this->message .= "<br /><br />\n";
4102
				}
4103
4104
				$this->message .= wp_sprintf(
4105
					_n(
4106
						'Jetpack contains the most recent version of the old %l plugin.',
4107
						'Jetpack contains the most recent versions of the old %l plugins.',
4108
						count( $deactivated_titles ),
4109
						'jetpack'
4110
					),
4111
					$deactivated_titles
4112
				);
4113
4114
				$this->message .= "<br />\n";
4115
4116
				$this->message .= _n(
4117
					'The old version has been deactivated and can be removed from your site.',
4118
					'The old versions have been deactivated and can be removed from your site.',
4119
					count( $deactivated_titles ),
4120
					'jetpack'
4121
				);
4122
			}
4123
		}
4124
4125
		$this->privacy_checks = Jetpack::state( 'privacy_checks' );
4126
4127
		if ( $this->message || $this->error || $this->privacy_checks || $this->can_display_jetpack_manage_notice() ) {
4128
			add_action( 'jetpack_notices', array( $this, 'admin_notices' ) );
4129
		}
4130
4131 View Code Duplication
		if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
4132
			/**
4133
			 * Fires when a module configuration page is loaded.
4134
			 * The dynamic part of the hook is the configure parameter from the URL.
4135
			 *
4136
			 * @since 1.1.0
4137
			 */
4138
			do_action( 'jetpack_module_configuration_load_' . $_GET['configure'] );
4139
		}
4140
4141
		add_filter( 'jetpack_short_module_description', 'wptexturize' );
4142
	}
4143
4144
	function admin_notices() {
4145
4146
		if ( $this->error ) {
4147
?>
4148
<div id="message" class="jetpack-message jetpack-err">
4149
	<div class="squeezer">
4150
		<h2><?php echo wp_kses( $this->error, array( 'a' => array( 'href' => array() ), 'small' => true, 'code' => true, 'strong' => true, 'br' => true, 'b' => true ) ); ?></h2>
4151
<?php	if ( $desc = Jetpack::state( 'error_description' ) ) : ?>
4152
		<p><?php echo esc_html( stripslashes( $desc ) ); ?></p>
4153
<?php	endif; ?>
4154
	</div>
4155
</div>
4156
<?php
4157
		}
4158
4159
		if ( $this->message ) {
4160
?>
4161
<div id="message" class="jetpack-message">
4162
	<div class="squeezer">
4163
		<h2><?php echo wp_kses( $this->message, array( 'strong' => array(), 'a' => array( 'href' => true ), 'br' => true ) ); ?></h2>
4164
	</div>
4165
</div>
4166
<?php
4167
		}
4168
4169
		if ( $this->privacy_checks ) :
4170
			$module_names = $module_slugs = array();
4171
4172
			$privacy_checks = explode( ',', $this->privacy_checks );
4173
			$privacy_checks = array_filter( $privacy_checks, array( 'Jetpack', 'is_module' ) );
4174
			foreach ( $privacy_checks as $module_slug ) {
4175
				$module = Jetpack::get_module( $module_slug );
4176
				if ( ! $module ) {
4177
					continue;
4178
				}
4179
4180
				$module_slugs[] = $module_slug;
4181
				$module_names[] = "<strong>{$module['name']}</strong>";
4182
			}
4183
4184
			$module_slugs = join( ',', $module_slugs );
4185
?>
4186
<div id="message" class="jetpack-message jetpack-err">
4187
	<div class="squeezer">
4188
		<h2><strong><?php esc_html_e( 'Is this site private?', 'jetpack' ); ?></strong></h2><br />
4189
		<p><?php
4190
			echo wp_kses(
4191
				wptexturize(
4192
					wp_sprintf(
4193
						_nx(
4194
							"Like your site's RSS feeds, %l allows access to your posts and other content to third parties.",
4195
							"Like your site's RSS feeds, %l allow access to your posts and other content to third parties.",
4196
							count( $privacy_checks ),
4197
							'%l = list of Jetpack module/feature names',
4198
							'jetpack'
4199
						),
4200
						$module_names
4201
					)
4202
				),
4203
				array( 'strong' => true )
4204
			);
4205
4206
			echo "\n<br />\n";
4207
4208
			echo wp_kses(
4209
				sprintf(
4210
					_nx(
4211
						'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating this feature</a>.',
4212
						'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating these features</a>.',
4213
						count( $privacy_checks ),
4214
						'%1$s = deactivation URL, %2$s = "Deactivate {list of Jetpack module/feature names}',
4215
						'jetpack'
4216
					),
4217
					wp_nonce_url(
4218
						Jetpack::admin_url(
4219
							array(
4220
								'page'   => 'jetpack',
4221
								'action' => 'deactivate',
4222
								'module' => urlencode( $module_slugs ),
4223
							)
4224
						),
4225
						"jetpack_deactivate-$module_slugs"
4226
					),
4227
					esc_attr( wp_kses( wp_sprintf( _x( 'Deactivate %l', '%l = list of Jetpack module/feature names', 'jetpack' ), $module_names ), array() ) )
4228
				),
4229
				array( 'a' => array( 'href' => true, 'title' => true ) )
4230
			);
4231
		?></p>
4232
	</div>
4233
</div>
4234
<?php endif;
4235
	// only display the notice if the other stuff is not there
4236
	if( $this->can_display_jetpack_manage_notice() && !  $this->error && ! $this->message && ! $this->privacy_checks ) {
4237
		if( isset( $_GET['page'] ) && 'jetpack' != $_GET['page'] )
4238
			$this->opt_in_jetpack_manage_notice();
4239
		}
4240
	}
4241
4242
	/**
4243
	 * Record a stat for later output.  This will only currently output in the admin_footer.
4244
	 */
4245
	function stat( $group, $detail ) {
4246
		if ( ! isset( $this->stats[ $group ] ) )
4247
			$this->stats[ $group ] = array();
4248
		$this->stats[ $group ][] = $detail;
4249
	}
4250
4251
	/**
4252
	 * Load stats pixels. $group is auto-prefixed with "x_jetpack-"
4253
	 */
4254
	function do_stats( $method = '' ) {
4255
		if ( is_array( $this->stats ) && count( $this->stats ) ) {
4256
			foreach ( $this->stats as $group => $stats ) {
4257
				if ( is_array( $stats ) && count( $stats ) ) {
4258
					$args = array( "x_jetpack-{$group}" => implode( ',', $stats ) );
4259
					if ( 'server_side' === $method ) {
4260
						self::do_server_side_stat( $args );
4261
					} else {
4262
						echo '<img src="' . esc_url( self::build_stats_url( $args ) ) . '" width="1" height="1" style="display:none;" />';
4263
					}
4264
				}
4265
				unset( $this->stats[ $group ] );
4266
			}
4267
		}
4268
	}
4269
4270
	/**
4271
	 * Runs stats code for a one-off, server-side.
4272
	 *
4273
	 * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store.
4274
	 *
4275
	 * @return bool If it worked.
4276
	 */
4277
	static function do_server_side_stat( $args ) {
4278
		$response = wp_remote_get( esc_url_raw( self::build_stats_url( $args ) ) );
4279
		if ( is_wp_error( $response ) )
4280
			return false;
4281
4282
		if ( 200 !== wp_remote_retrieve_response_code( $response ) )
4283
			return false;
4284
4285
		return true;
4286
	}
4287
4288
	/**
4289
	 * Builds the stats url.
4290
	 *
4291
	 * @param $args array|string The arguments to append to the URL.
4292
	 *
4293
	 * @return string The URL to be pinged.
4294
	 */
4295
	static function build_stats_url( $args ) {
4296
		$defaults = array(
4297
			'v'    => 'wpcom2',
4298
			'rand' => md5( mt_rand( 0, 999 ) . time() ),
4299
		);
4300
		$args     = wp_parse_args( $args, $defaults );
4301
		/**
4302
		 * Filter the URL used as the Stats tracking pixel.
4303
		 *
4304
		 * @since 2.3.2
4305
		 *
4306
		 * @param string $url Base URL used as the Stats tracking pixel.
4307
		 */
4308
		$base_url = apply_filters(
4309
			'jetpack_stats_base_url',
4310
			set_url_scheme( 'http://pixel.wp.com/g.gif' )
4311
		);
4312
		$url      = add_query_arg( $args, $base_url );
4313
		return $url;
4314
	}
4315
4316
	function translate_current_user_to_role() {
4317
		foreach ( $this->capability_translations as $role => $cap ) {
4318
			if ( current_user_can( $role ) || current_user_can( $cap ) ) {
4319
				return $role;
4320
			}
4321
		}
4322
4323
		return false;
4324
	}
4325
4326
	function translate_role_to_cap( $role ) {
4327
		if ( ! isset( $this->capability_translations[$role] ) ) {
4328
			return false;
4329
		}
4330
4331
		return $this->capability_translations[$role];
4332
	}
4333
4334
	function sign_role( $role ) {
4335
		if ( ! $user_id = (int) get_current_user_id() ) {
4336
			return false;
4337
		}
4338
4339
		$token = Jetpack_Data::get_access_token();
4340
		if ( ! $token || is_wp_error( $token ) ) {
4341
			return false;
4342
		}
4343
4344
		return $role . ':' . hash_hmac( 'md5', "{$role}|{$user_id}", $token->secret );
4345
	}
4346
4347
4348
	/**
4349
	 * Builds a URL to the Jetpack connection auth page
4350
	 *
4351
	 * @since 3.9.5
4352
	 *
4353
	 * @param bool $raw If true, URL will not be escaped.
4354
	 * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection.
4355
	 *                              If string, will be a custom redirect.
4356
	 * @param bool|string $from If not false, adds 'from=$from' param to the connect URL.
4357
	 *
4358
	 * @return string Connect URL
4359
	 */
4360
	function build_connect_url( $raw = false, $redirect = false, $from = false ) {
4361
		if ( ! Jetpack_Options::get_option( 'blog_token' ) || ! Jetpack_Options::get_option( 'id' ) ) {
4362
			$url = Jetpack::nonce_url_no_esc( Jetpack::admin_url( 'action=register' ), 'jetpack-register' );
4363
			if( is_network_admin() ) {
4364
			    $url = add_query_arg( 'is_multisite', network_admin_url(
4365
			    'admin.php?page=jetpack-settings' ), $url );
4366
			}
4367
		} else {
4368
			$role = $this->translate_current_user_to_role();
4369
			$signed_role = $this->sign_role( $role );
4370
4371
			$user = wp_get_current_user();
4372
4373
			$redirect = $redirect ? esc_url_raw( $redirect ) : esc_url_raw( admin_url( 'admin.php?page=jetpack' ) );
4374
4375
			if( isset( $_REQUEST['is_multisite'] ) ) {
4376
				$redirect = Jetpack_Network::init()->get_url( 'network_admin_page' );
4377
			}
4378
4379
			$secrets = Jetpack::init()->generate_secrets();
4380
			Jetpack_Options::update_option( 'authorize', $secrets[0] . ':' . $secrets[1] . ':' . $secrets[2] . ':' . $secrets[3] );
4381
4382
			@list( $secret ) = explode( ':', Jetpack_Options::get_option( 'authorize' ) );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
4383
4384
			$args = urlencode_deep(
4385
				array(
4386
					'response_type' => 'code',
4387
					'client_id'     => Jetpack_Options::get_option( 'id' ),
4388
					'redirect_uri'  => add_query_arg(
4389
						array(
4390
							'action'   => 'authorize',
4391
							'_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
4392
							'redirect' => urlencode( $redirect ),
4393
						),
4394
						esc_url( admin_url( 'admin.php?page=jetpack' ) )
4395
					),
4396
					'state'         => $user->ID,
4397
					'scope'         => $signed_role,
4398
					'user_email'    => $user->user_email,
4399
					'user_login'    => $user->user_login,
4400
					'is_active'     => Jetpack::is_active(),
4401
					'jp_version'    => JETPACK__VERSION,
4402
					'auth_type'     => 'calypso',
4403
					'secret'		=> $secret,
4404
				)
4405
			);
4406
4407
			$url = add_query_arg( $args, Jetpack::api_url( 'authorize' ) );
4408
		}
4409
4410
		if ( $from ) {
4411
			$url = add_query_arg( 'from', $from, $url );
4412
		}
4413
4414
		if ( isset( $_GET['calypso_env'] ) ) {
4415
			$url = add_query_arg( 'calypso_env', sanitize_key( $_GET['calypso_env'] ), $url );
4416
		}
4417
4418
		return $raw ? $url : esc_url( $url );
4419
	}
4420
4421
	function build_reconnect_url( $raw = false ) {
4422
		$url = wp_nonce_url( Jetpack::admin_url( 'action=reconnect' ), 'jetpack-reconnect' );
4423
		return $raw ? $url : esc_url( $url );
4424
	}
4425
4426
	public static function admin_url( $args = null ) {
4427
		$args = wp_parse_args( $args, array( 'page' => 'jetpack' ) );
4428
		$url = add_query_arg( $args, admin_url( 'admin.php' ) );
4429
		return $url;
4430
	}
4431
4432
	public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) {
4433
		$actionurl = str_replace( '&amp;', '&', $actionurl );
4434
		return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
4435
	}
4436
4437
	function dismiss_jetpack_notice() {
4438
4439
		if ( ! isset( $_GET['jetpack-notice'] ) ) {
4440
			return;
4441
		}
4442
4443
		switch( $_GET['jetpack-notice'] ) {
4444
			case 'dismiss':
4445
				if ( check_admin_referer( 'jetpack-deactivate' ) && ! is_plugin_active_for_network( plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ) ) ) {
4446
4447
					require_once ABSPATH . 'wp-admin/includes/plugin.php';
4448
					deactivate_plugins( JETPACK__PLUGIN_DIR . 'jetpack.php', false, false );
4449
					wp_safe_redirect( admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s=' );
4450
				}
4451
				break;
4452 View Code Duplication
			case 'jetpack-manage-opt-out':
4453
4454
				if ( check_admin_referer( 'jetpack_manage_banner_opt_out' ) ) {
4455
					// Don't show the banner again
4456
4457
					Jetpack_Options::update_option( 'dismissed_manage_banner', true );
4458
					// redirect back to the page that had the notice
4459
					if ( wp_get_referer() ) {
4460
						wp_safe_redirect( wp_get_referer() );
4461
					} else {
4462
						// Take me to Jetpack
4463
						wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) );
4464
					}
4465
				}
4466
				break;
4467 View Code Duplication
			case 'jetpack-protect-multisite-opt-out':
4468
4469
				if ( check_admin_referer( 'jetpack_protect_multisite_banner_opt_out' ) ) {
4470
					// Don't show the banner again
4471
4472
					update_site_option( 'jetpack_dismissed_protect_multisite_banner', true );
4473
					// redirect back to the page that had the notice
4474
					if ( wp_get_referer() ) {
4475
						wp_safe_redirect( wp_get_referer() );
4476
					} else {
4477
						// Take me to Jetpack
4478
						wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) );
4479
					}
4480
				}
4481
				break;
4482
			case 'jetpack-manage-opt-in':
4483
				if ( check_admin_referer( 'jetpack_manage_banner_opt_in' ) ) {
4484
					// This makes sure that we are redirect to jetpack home so that we can see the Success Message.
4485
4486
					$redirection_url = Jetpack::admin_url();
4487
					remove_action( 'jetpack_pre_activate_module',   array( Jetpack_Admin::init(), 'fix_redirect' ) );
4488
4489
					// Don't redirect form the Jetpack Setting Page
4490
					$referer_parsed = parse_url ( wp_get_referer() );
4491
					// check that we do have a wp_get_referer and the query paramater is set orderwise go to the Jetpack Home
4492
					if ( isset( $referer_parsed['query'] ) && false !== strpos( $referer_parsed['query'], 'page=jetpack_modules' ) ) {
4493
						// Take the user to Jetpack home except when on the setting page
4494
						$redirection_url = wp_get_referer();
4495
						add_action( 'jetpack_pre_activate_module',   array( Jetpack_Admin::init(), 'fix_redirect' ) );
4496
					}
4497
					// Also update the JSON API FULL MANAGEMENT Option
4498
					Jetpack::activate_module( 'manage', false, false );
4499
4500
					// Special Message when option in.
4501
					Jetpack::state( 'optin-manage', 'true' );
4502
					// Activate the Module if not activated already
4503
4504
					// Redirect properly
4505
					wp_safe_redirect( $redirection_url );
4506
4507
				}
4508
				break;
4509
		}
4510
	}
4511
4512
	function debugger_page() {
4513
		nocache_headers();
4514
		if ( ! current_user_can( 'manage_options' ) ) {
4515
			die( '-1' );
4516
		}
4517
		Jetpack_Debugger::jetpack_debug_display_handler();
4518
		exit;
4519
	}
4520
4521
	public static function admin_screen_configure_module( $module_id ) {
4522
4523
		// User that doesn't have 'jetpack_configure_modules' will never end up here since Jetpack Landing Page woun't let them.
4524
		if ( ! in_array( $module_id, Jetpack::get_active_modules() ) && current_user_can( 'manage_options' ) ) {
4525
			if ( has_action( 'display_activate_module_setting_' . $module_id ) ) {
4526
				/**
4527
				 * Fires to diplay a custom module activation screen.
4528
				 *
4529
				 * To add a module actionation screen use Jetpack::module_configuration_activation_screen method.
4530
				 * Example: Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) );
4531
				 *
4532
				 * @module manage
4533
				 *
4534
				 * @since 3.8.0
4535
				 *
4536
				 * @param int $module_id Module ID.
4537
				 */
4538
				do_action( 'display_activate_module_setting_' . $module_id );
4539
			} else {
4540
				self::display_activate_module_link( $module_id );
4541
			}
4542
4543
			return false;
4544
		} ?>
4545
4546
		<div id="jp-settings-screen" style="position: relative">
4547
			<h3>
4548
			<?php
4549
				$module = Jetpack::get_module( $module_id );
4550
				echo '<a href="' . Jetpack::admin_url( 'page=jetpack_modules' ) . '">' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</a> &rarr; ';
4551
				printf( __( 'Configure %s', 'jetpack' ), $module['name'] );
4552
			?>
4553
			</h3>
4554
			<?php
4555
				/**
4556
				 * Fires within the displayed message when a feature configuation is updated.
4557
				 *
4558
				 * @since 3.4.0
4559
				 *
4560
				 * @param int $module_id Module ID.
4561
				 */
4562
				do_action( 'jetpack_notices_update_settings', $module_id );
4563
				/**
4564
				 * Fires when a feature configuation screen is loaded.
4565
				 * The dynamic part of the hook, $module_id, is the module ID.
4566
				 *
4567
				 * @since 1.1.0
4568
				 */
4569
				do_action( 'jetpack_module_configuration_screen_' . $module_id );
4570
			?>
4571
		</div><?php
4572
	}
4573
4574
	/**
4575
	 * Display link to activate the module to see the settings screen.
4576
	 * @param  string $module_id
4577
	 * @return null
4578
	 */
4579
	public static function display_activate_module_link( $module_id ) {
4580
4581
		$info =  Jetpack::get_module( $module_id );
4582
		$extra = '';
4583
		$activate_url = wp_nonce_url(
4584
				Jetpack::admin_url(
4585
					array(
4586
						'page'   => 'jetpack',
4587
						'action' => 'activate',
4588
						'module' => $module_id,
4589
					)
4590
				),
4591
				"jetpack_activate-$module_id"
4592
			);
4593
4594
		?>
4595
4596
		<div class="wrap configure-module">
4597
			<div id="jp-settings-screen">
4598
				<?php
4599
				if ( $module_id == 'json-api' ) {
4600
4601
					$info['name'] = esc_html__( 'Activate Site Management and JSON API', 'jetpack' );
4602
4603
					$activate_url = Jetpack::init()->opt_in_jetpack_manage_url();
4604
4605
					$info['description'] = sprintf( __( 'Manage your multiple Jetpack sites from our centralized dashboard at wordpress.com/sites. <a href="%s" target="_blank">Learn more</a>.', 'jetpack' ), 'http://jetpack.com/support/site-management' );
4606
4607
					// $extra = __( 'To use Site Management, you need to first activate JSON API to allow remote management of your site. ', 'jetpack' );
4608
				} ?>
4609
4610
				<h3><?php echo esc_html( $info['name'] ); ?></h3>
4611
				<div class="narrow">
4612
					<p><?php echo  $info['description']; ?></p>
4613
					<?php if( $extra ) { ?>
4614
					<p><?php echo esc_html( $extra ); ?></p>
4615
					<?php } ?>
4616
					<p>
4617
						<?php
4618
						if( wp_get_referer() ) {
4619
							printf( __( '<a class="button-primary" href="%s">Activate Now</a> or <a href="%s" >return to previous page</a>.', 'jetpack' ) , $activate_url, wp_get_referer() );
4620
						} else {
4621
							printf( __( '<a class="button-primary" href="%s">Activate Now</a>', 'jetpack' ) , $activate_url  );
4622
						} ?>
4623
					</p>
4624
				</div>
4625
4626
			</div>
4627
		</div>
4628
4629
		<?php
4630
	}
4631
4632
	public static function sort_modules( $a, $b ) {
4633
		if ( $a['sort'] == $b['sort'] )
4634
			return 0;
4635
4636
		return ( $a['sort'] < $b['sort'] ) ? -1 : 1;
4637
	}
4638
4639 View Code Duplication
	function sync_reindex_trigger() {
4640
		if ( $this->current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) {
4641
			echo json_encode( $this->sync->reindex_trigger() );
4642
		} else {
4643
			echo '{"status":"ERROR"}';
4644
		}
4645
		exit;
4646
	}
4647
4648 View Code Duplication
	function sync_reindex_status(){
4649
		if ( $this->current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) {
4650
			echo json_encode( $this->sync->reindex_status() );
4651
		} else {
4652
			echo '{"status":"ERROR"}';
4653
		}
4654
		exit;
4655
	}
4656
4657
/* Client API */
4658
4659
	/**
4660
	 * Returns the requested Jetpack API URL
4661
	 *
4662
	 * @return string
4663
	 */
4664
	public static function api_url( $relative_url ) {
4665
		return trailingslashit( JETPACK__API_BASE . $relative_url  ) . JETPACK__API_VERSION . '/';
4666
	}
4667
4668
	/**
4669
	 * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets
4670
	 */
4671
	public static function fix_url_for_bad_hosts( $url ) {
4672
		if ( 0 !== strpos( $url, 'https://' ) ) {
4673
			return $url;
4674
		}
4675
4676
		switch ( JETPACK_CLIENT__HTTPS ) {
4677
			case 'ALWAYS' :
4678
				return $url;
4679
			case 'NEVER' :
4680
				return set_url_scheme( $url, 'http' );
4681
			// default : case 'AUTO' :
4682
		}
4683
4684
		// we now return the unmodified SSL URL by default, as a security precaution
4685
		return $url;
4686
	}
4687
4688
	/**
4689
	 * Checks to see if the URL is using SSL to connect with Jetpack
4690
	 *
4691
	 * @since 2.3.3
4692
	 * @return boolean
4693
	 */
4694
	public static function permit_ssl( $force_recheck = false ) {
4695
		// Do some fancy tests to see if ssl is being supported
4696
		if ( $force_recheck || false === ( $ssl = get_transient( 'jetpack_https_test' ) ) ) {
4697
			$message = '';
4698
			if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) {
4699
				$ssl = 0;
4700
			} else {
4701
				switch ( JETPACK_CLIENT__HTTPS ) {
4702
					case 'NEVER':
4703
						$ssl = 0;
4704
						$message = __( 'JETPACK_CLIENT__HTTPS is set to NEVER', 'jetpack' );
4705
						break;
4706
					case 'ALWAYS':
4707
					case 'AUTO':
4708
					default:
4709
						$ssl = 1;
4710
						break;
4711
				}
4712
4713
				// If it's not 'NEVER', test to see
4714
				if ( $ssl ) {
4715
					if ( ! wp_http_supports( array( 'ssl' => true ) ) ) {
4716
						$ssl = 0;
4717
						$message = __( 'WordPress reports no SSL support', 'jetpack' );
4718
					} else {
4719
						$response = wp_remote_get( JETPACK__API_BASE . 'test/1/' );
4720
						if ( is_wp_error( $response ) ) {
4721
							$ssl = 0;
4722
							$message = __( 'WordPress reports no SSL support', 'jetpack' );
4723
						} elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) {
4724
							$ssl = 0;
4725
							$message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response );
4726
						}
4727
					}
4728
				}
4729
			}
4730
			set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS );
4731
			set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS );
4732
		}
4733
4734
		return (bool) $ssl;
4735
	}
4736
4737
	/*
4738
	 * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working.
4739
	 */
4740
	public function alert_auto_ssl_fail() {
4741
		if ( ! current_user_can( 'manage_options' ) )
4742
			return;
4743
		?>
4744
4745
		<div id="jetpack-ssl-warning" class="error jp-identity-crisis">
4746
			<div class="jp-banner__content">
4747
				<h2><?php _e( 'Outbound HTTPS not working', 'jetpack' ); ?></h2>
4748
				<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>
4749
				<p>
4750
					<?php _e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?>
4751
					<a href="#" id="jetpack-recheck-ssl-button"><?php _e( 'Try again', 'jetpack' ); ?></a>
4752
					<span id="jetpack-recheck-ssl-output"><?php echo get_transient( 'jetpack_https_test_message' ); ?></span>
4753
				</p>
4754
				<p>
4755
					<?php printf( __( 'For more help, try our <a href="$1%s">connection debugger</a> or <a href="$2%s" target="_blank">troubleshooting tips</a>', 'jetpack' ), 
4756
							esc_url( Jetpack::admin_url( array( 'page' => 'jetpack-debugger' )  ) ),
4757
							esc_url( 'https://jetpack.com/support/getting-started-with-jetpack/troubleshooting-tips/' ) ); ?>
4758
				</p>
4759
			</div>
4760
		</div>
4761
		<style>
4762
			#jetpack-recheck-ssl-output { margin-left: 5px; color: red; }
4763
		</style>
4764
		<script type="text/javascript">
4765
			jQuery( document ).ready( function( $ ) {
4766
				$( '#jetpack-recheck-ssl-button' ).click( function( e ) {
4767
					var $this = $( this );
4768
					$this.html( <?php echo json_encode( __( 'Checking', 'jetpack' ) ); ?> );
4769
					$( '#jetpack-recheck-ssl-output' ).html( '' );
4770
					e.preventDefault();
4771
					$.post( '/wp-json/jetpack/v4/recheck-ssl' )
4772
					  .done( function( response ) {
4773
					  	if ( response.enabled ) {
4774
					  		$( '#jetpack-ssl-warning' ).hide();
4775
					  	} else {
4776
					  		this.html( <?php echo json_encode( __( 'Try again', 'jetpack' ) ); ?> );
4777
					  		$( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message );
4778
					  	}
4779
					  }.bind( $this ) );
4780
				} );
4781
			} );
4782
		</script>
4783
4784
		<?php
4785
	}
4786
4787
	/**
4788
	 * Returns the Jetpack XML-RPC API
4789
	 *
4790
	 * @return string
4791
	 */
4792
	public static function xmlrpc_api_url() {
4793
		$base = preg_replace( '#(https?://[^?/]+)(/?.*)?$#', '\\1', JETPACK__API_BASE );
4794
		return untrailingslashit( $base ) . '/xmlrpc.php';
4795
	}
4796
4797
	/**
4798
	 * Creates two secret tokens and the end of life timestamp for them.
4799
	 *
4800
	 * Note these tokens are unique per call, NOT static per site for connecting.
4801
	 *
4802
	 * @since 2.6
4803
	 * @return array
4804
	 */
4805
	public function generate_secrets() {
4806
	    $secrets = array(
4807
			wp_generate_password( 32, false ), // secret_1
4808
			wp_generate_password( 32, false ), // secret_2
4809
			( time() + 600 ), // eol ( End of Life )
4810
			get_current_user_id(), // ties the secrets to the current user
4811
	    );
4812
4813
	    return $secrets;
4814
	}
4815
4816
	/**
4817
	 * Builds the timeout limit for queries talking with the wpcom servers.
4818
	 *
4819
	 * Based on local php max_execution_time in php.ini
4820
	 *
4821
	 * @since 2.6
4822
	 * @return int
4823
	 **/
4824
	public function get_remote_query_timeout_limit() {
4825
	    $timeout = (int) ini_get( 'max_execution_time' );
4826
	    if ( ! $timeout ) // Ensure exec time set in php.ini
4827
		$timeout = 30;
4828
	    return intval( $timeout / 2 );
4829
	}
4830
4831
4832
	/**
4833
	 * Takes the response from the Jetpack register new site endpoint and
4834
	 * verifies it worked properly.
4835
	 *
4836
	 * @since 2.6
4837
	 * @return true or Jetpack_Error
4838
	 **/
4839
	public function validate_remote_register_response( $response ) {
4840
	    	if ( is_wp_error( $response ) ) {
4841
			return new Jetpack_Error( 'register_http_request_failed', $response->get_error_message() );
4842
		}
4843
4844
		$code   = wp_remote_retrieve_response_code( $response );
4845
		$entity = wp_remote_retrieve_body( $response );
4846
		if ( $entity )
4847
			$json = json_decode( $entity );
4848
		else
4849
			$json = false;
4850
4851
		$code_type = intval( $code / 100 );
4852
		if ( 5 == $code_type ) {
4853
			return new Jetpack_Error( 'wpcom_5??', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code );
4854
		} elseif ( 408 == $code ) {
4855
			return new Jetpack_Error( 'wpcom_408', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code );
4856
		} elseif ( ! empty( $json->error ) ) {
4857
			$error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : '';
4858
			return new Jetpack_Error( (string) $json->error, $error_description, $code );
4859
		} elseif ( 200 != $code ) {
4860
			return new Jetpack_Error( 'wpcom_bad_response', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code );
4861
		}
4862
4863
		// Jetpack ID error block
4864
		if ( empty( $json->jetpack_id ) ) {
4865
			return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is empty. Do not publicly post this error message! %s', 'jetpack' ), $entity ), $entity );
4866
		} elseif ( ! is_scalar( $json->jetpack_id ) ) {
4867
			return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is not a scalar. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity );
4868
		} elseif ( preg_match( '/[^0-9]/', $json->jetpack_id ) ) {
4869
			return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID begins with a numeral. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity );
4870
		}
4871
4872
	    return true;
4873
	}
4874
	/**
4875
	 * @return bool|WP_Error
4876
	 */
4877
	public static function register() {
4878
		add_action( 'pre_update_jetpack_option_register', array( 'Jetpack_Options', 'delete_option' ) );
4879
		$secrets = Jetpack::init()->generate_secrets();
4880
4881
		Jetpack_Options::update_option( 'register', $secrets[0] . ':' . $secrets[1] . ':' . $secrets[2] . ':' . $secrets[3] );
4882
4883
		@list( $secret_1, $secret_2, $secret_eol ) = explode( ':', Jetpack_Options::get_option( 'register' ) );
4884
		if ( empty( $secret_1 ) || empty( $secret_2 ) || empty( $secret_eol ) || $secret_eol < time() ) {
4885
			return new Jetpack_Error( 'missing_secrets' );
4886
		}
4887
4888
		$timeout = Jetpack::init()->get_remote_query_timeout_limit();
4889
4890
		$gmt_offset = get_option( 'gmt_offset' );
4891
		if ( ! $gmt_offset ) {
4892
			$gmt_offset = 0;
4893
		}
4894
4895
		$stats_options = get_option( 'stats_options' );
4896
		$stats_id = isset($stats_options['blog_id']) ? $stats_options['blog_id'] : null;
4897
4898
		$args = array(
4899
			'method'  => 'POST',
4900
			'body'    => array(
4901
				'siteurl'         => site_url(),
4902
				'home'            => home_url(),
4903
				'gmt_offset'      => $gmt_offset,
4904
				'timezone_string' => (string) get_option( 'timezone_string' ),
4905
				'site_name'       => (string) get_option( 'blogname' ),
4906
				'secret_1'        => $secret_1,
4907
				'secret_2'        => $secret_2,
4908
				'site_lang'       => get_locale(),
4909
				'timeout'         => $timeout,
4910
				'stats_id'        => $stats_id,
4911
				'state'           => get_current_user_id(),
4912
			),
4913
			'headers' => array(
4914
				'Accept' => 'application/json',
4915
			),
4916
			'timeout' => $timeout,
4917
		);
4918
		$response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'register' ) ), $args, true );
4919
4920
4921
		// Make sure the response is valid and does not contain any Jetpack errors
4922
		$valid_response = Jetpack::init()->validate_remote_register_response( $response );
4923
		if( is_wp_error( $valid_response ) || !$valid_response ) {
4924
		    return $valid_response;
4925
		}
4926
4927
		// Grab the response values to work with
4928
		$code   = wp_remote_retrieve_response_code( $response );
4929
		$entity = wp_remote_retrieve_body( $response );
4930
4931
		if ( $entity )
4932
			$json = json_decode( $entity );
4933
		else
4934
			$json = false;
4935
4936 View Code Duplication
		if ( empty( $json->jetpack_secret ) || ! is_string( $json->jetpack_secret ) )
4937
			return new Jetpack_Error( 'jetpack_secret', '', $code );
4938
4939
		if ( isset( $json->jetpack_public ) ) {
4940
			$jetpack_public = (int) $json->jetpack_public;
4941
		} else {
4942
			$jetpack_public = false;
4943
		}
4944
4945
		Jetpack_Options::update_options(
4946
			array(
4947
				'id'         => (int)    $json->jetpack_id,
4948
				'blog_token' => (string) $json->jetpack_secret,
4949
				'public'     => $jetpack_public,
4950
			)
4951
		);
4952
4953
		/**
4954
		 * Fires when a site is registered on WordPress.com.
4955
		 *
4956
		 * @since 3.7.0
4957
		 *
4958
		 * @param int $json->jetpack_id Jetpack Blog ID.
4959
		 * @param string $json->jetpack_secret Jetpack Blog Token.
4960
		 * @param int|bool $jetpack_public Is the site public.
4961
		 */
4962
		do_action( 'jetpack_site_registered', $json->jetpack_id, $json->jetpack_secret, $jetpack_public );
4963
4964
		// Initialize Jump Start for the first and only time.
4965
		if ( ! Jetpack_Options::get_option( 'jumpstart' ) ) {
4966
			Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
4967
4968
			$jetpack = Jetpack::init();
4969
4970
			$jetpack->stat( 'jumpstart', 'unique-views' );
4971
			$jetpack->do_stats( 'server_side' );
4972
		};
4973
4974
		return true;
4975
	}
4976
4977
	/**
4978
	 * If the db version is showing something other that what we've got now, bump it to current.
4979
	 *
4980
	 * @return bool: True if the option was incorrect and updated, false if nothing happened.
4981
	 */
4982
	public static function maybe_set_version_option() {
4983
		list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
4984
		if ( JETPACK__VERSION != $version ) {
4985
			Jetpack_Options::update_option( 'version', JETPACK__VERSION . ':' . time() );
4986
			return true;
4987
		}
4988
		return false;
4989
	}
4990
4991
/* Client Server API */
4992
4993
	/**
4994
	 * Loads the Jetpack XML-RPC client
4995
	 */
4996
	public static function load_xml_rpc_client() {
4997
		require_once ABSPATH . WPINC . '/class-IXR.php';
4998
		require_once JETPACK__PLUGIN_DIR . 'class.jetpack-ixr-client.php';
4999
	}
5000
5001
	function verify_xml_rpc_signature() {
5002
		if ( $this->xmlrpc_verification ) {
5003
			return $this->xmlrpc_verification;
5004
		}
5005
5006
		// It's not for us
5007
		if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) {
5008
			return false;
5009
		}
5010
5011
		@list( $token_key, $version, $user_id ) = explode( ':', $_GET['token'] );
5012
		if (
5013
			empty( $token_key )
5014
		||
5015
			empty( $version ) || strval( JETPACK__API_VERSION ) !== $version
5016
		) {
5017
			return false;
5018
		}
5019
5020
		if ( '0' === $user_id ) {
5021
			$token_type = 'blog';
5022
			$user_id = 0;
5023
		} else {
5024
			$token_type = 'user';
5025
			if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) {
5026
				return false;
5027
			}
5028
			$user_id = (int) $user_id;
5029
5030
			$user = new WP_User( $user_id );
5031
			if ( ! $user || ! $user->exists() ) {
5032
				return false;
5033
			}
5034
		}
5035
5036
		$token = Jetpack_Data::get_access_token( $user_id );
5037
		if ( ! $token ) {
5038
			return false;
5039
		}
5040
5041
		$token_check = "$token_key.";
5042
		if ( ! hash_equals( substr( $token->secret, 0, strlen( $token_check ) ), $token_check ) ) {
5043
			return false;
5044
		}
5045
5046
		require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php';
5047
5048
		$jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
5049
		if ( isset( $_POST['_jetpack_is_multipart'] ) ) {
5050
			$post_data   = $_POST;
5051
			$file_hashes = array();
5052
			foreach ( $post_data as $post_data_key => $post_data_value ) {
5053
				if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) {
5054
					continue;
5055
				}
5056
				$post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) );
5057
				$file_hashes[$post_data_key] = $post_data_value;
5058
			}
5059
5060
			foreach ( $file_hashes as $post_data_key => $post_data_value ) {
5061
				unset( $post_data["_jetpack_file_hmac_{$post_data_key}"] );
5062
				$post_data[$post_data_key] = $post_data_value;
5063
			}
5064
5065
			ksort( $post_data );
5066
5067
			$body = http_build_query( stripslashes_deep( $post_data ) );
5068
		} elseif ( is_null( $this->HTTP_RAW_POST_DATA ) ) {
5069
			$body = file_get_contents( 'php://input' );
5070
		} else {
5071
			$body = null;
5072
		}
5073
		$signature = $jetpack_signature->sign_current_request(
5074
			array( 'body' => is_null( $body ) ? $this->HTTP_RAW_POST_DATA : $body, )
5075
		);
5076
5077
		if ( ! $signature ) {
5078
			return false;
5079
		} else if ( is_wp_error( $signature ) ) {
5080
			return $signature;
5081
		} else if ( ! hash_equals( $signature, $_GET['signature'] ) ) {
5082
			return false;
5083
		}
5084
5085
		$timestamp = (int) $_GET['timestamp'];
5086
		$nonce     = stripslashes( (string) $_GET['nonce'] );
5087
5088
		if ( ! $this->add_nonce( $timestamp, $nonce ) ) {
5089
			return false;
5090
		}
5091
5092
		$this->xmlrpc_verification = array(
5093
			'type'    => $token_type,
5094
			'user_id' => $token->external_user_id,
5095
		);
5096
5097
		return $this->xmlrpc_verification;
5098
	}
5099
5100
	/**
5101
	 * Authenticates XML-RPC and other requests from the Jetpack Server
5102
	 */
5103
	function authenticate_jetpack( $user, $username, $password ) {
5104
		if ( is_a( $user, 'WP_User' ) ) {
5105
			return $user;
5106
		}
5107
5108
		$token_details = $this->verify_xml_rpc_signature();
5109
5110
		if ( ! $token_details || is_wp_error( $token_details ) ) {
5111
			return $user;
5112
		}
5113
5114
		if ( 'user' !== $token_details['type'] ) {
5115
			return $user;
5116
		}
5117
5118
		if ( ! $token_details['user_id'] ) {
5119
			return $user;
5120
		}
5121
5122
		nocache_headers();
5123
5124
		return new WP_User( $token_details['user_id'] );
5125
	}
5126
5127
	function add_nonce( $timestamp, $nonce ) {
5128
		global $wpdb;
5129
		static $nonces_used_this_request = array();
5130
5131
		if ( isset( $nonces_used_this_request["$timestamp:$nonce"] ) ) {
5132
			return $nonces_used_this_request["$timestamp:$nonce"];
5133
		}
5134
5135
		// This should always have gone through Jetpack_Signature::sign_request() first to check $timestamp an $nonce
5136
		$timestamp = (int) $timestamp;
5137
		$nonce     = esc_sql( $nonce );
5138
5139
		// Raw query so we can avoid races: add_option will also update
5140
		$show_errors = $wpdb->show_errors( false );
5141
5142
		$old_nonce = $wpdb->get_row(
5143
			$wpdb->prepare( "SELECT * FROM `$wpdb->options` WHERE option_name = %s", "jetpack_nonce_{$timestamp}_{$nonce}" )
5144
		);
5145
5146
		if ( is_null( $old_nonce ) ) {
5147
			$return = $wpdb->query(
5148
				$wpdb->prepare(
5149
					"INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)",
5150
					"jetpack_nonce_{$timestamp}_{$nonce}",
5151
					time(),
5152
					'no'
5153
				)
5154
			);
5155
		} else {
5156
			$return = false;
5157
		}
5158
5159
		$wpdb->show_errors( $show_errors );
5160
5161
		$nonces_used_this_request["$timestamp:$nonce"] = $return;
5162
5163
		return $return;
5164
	}
5165
5166
	/**
5167
	 * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods.
5168
	 * Capture it here so we can verify the signature later.
5169
	 */
5170
	function xmlrpc_methods( $methods ) {
5171
		$this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA'];
5172
		return $methods;
5173
	}
5174
5175
	function public_xmlrpc_methods( $methods ) {
5176
		if ( array_key_exists( 'wp.getOptions', $methods ) ) {
5177
			$methods['wp.getOptions'] = array( $this, 'jetpack_getOptions' );
5178
		}
5179
		return $methods;
5180
	}
5181
5182
	function jetpack_getOptions( $args ) {
5183
		global $wp_xmlrpc_server;
5184
5185
		$wp_xmlrpc_server->escape( $args );
5186
5187
		$username	= $args[1];
5188
		$password	= $args[2];
5189
5190
		if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
5191
			return $wp_xmlrpc_server->error;
5192
		}
5193
5194
		$options = array();
5195
		$user_data = $this->get_connected_user_data();
5196
		if ( is_array( $user_data ) ) {
5197
			$options['jetpack_user_id'] = array(
5198
				'desc'          => __( 'The WP.com user ID of the connected user', 'jetpack' ),
5199
				'readonly'      => true,
5200
				'value'         => $user_data['ID'],
5201
			);
5202
			$options['jetpack_user_login'] = array(
5203
				'desc'          => __( 'The WP.com username of the connected user', 'jetpack' ),
5204
				'readonly'      => true,
5205
				'value'         => $user_data['login'],
5206
			);
5207
			$options['jetpack_user_email'] = array(
5208
				'desc'          => __( 'The WP.com user email of the connected user', 'jetpack' ),
5209
				'readonly'      => true,
5210
				'value'         => $user_data['email'],
5211
			);
5212
			$options['jetpack_user_site_count'] = array(
5213
				'desc'          => __( 'The number of sites of the connected WP.com user', 'jetpack' ),
5214
				'readonly'      => true,
5215
				'value'         => $user_data['site_count'],
5216
			);
5217
		}
5218
		$wp_xmlrpc_server->blog_options = array_merge( $wp_xmlrpc_server->blog_options, $options );
5219
		$args = stripslashes_deep( $args );
5220
		return $wp_xmlrpc_server->wp_getOptions( $args );
5221
	}
5222
5223
	function xmlrpc_options( $options ) {
5224
		$jetpack_client_id = false;
5225
		if ( self::is_active() ) {
5226
			$jetpack_client_id = Jetpack_Options::get_option( 'id' );
5227
		}
5228
		$options['jetpack_version'] = array(
5229
				'desc'          => __( 'Jetpack Plugin Version', 'jetpack' ),
5230
				'readonly'      => true,
5231
				'value'         => JETPACK__VERSION,
5232
		);
5233
5234
		$options['jetpack_client_id'] = array(
5235
				'desc'          => __( 'The Client ID/WP.com Blog ID of this site', 'jetpack' ),
5236
				'readonly'      => true,
5237
				'value'         => $jetpack_client_id,
5238
		);
5239
		return $options;
5240
	}
5241
5242
	public static function clean_nonces( $all = false ) {
5243
		global $wpdb;
5244
5245
		$sql = "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s";
5246
		if ( method_exists ( $wpdb , 'esc_like' ) ) {
5247
			$sql_args = array( $wpdb->esc_like( 'jetpack_nonce_' ) . '%' );
5248
		} else {
5249
			$sql_args = array( like_escape( 'jetpack_nonce_' ) . '%' );
5250
		}
5251
5252
		if ( true !== $all ) {
5253
			$sql .= ' AND CAST( `option_value` AS UNSIGNED ) < %d';
5254
			$sql_args[] = time() - 3600;
5255
		}
5256
5257
		$sql .= ' ORDER BY `option_id` LIMIT 100';
5258
5259
		$sql = $wpdb->prepare( $sql, $sql_args );
5260
5261
		for ( $i = 0; $i < 1000; $i++ ) {
5262
			if ( ! $wpdb->query( $sql ) ) {
5263
				break;
5264
			}
5265
		}
5266
	}
5267
5268
	/**
5269
	 * State is passed via cookies from one request to the next, but never to subsequent requests.
5270
	 * SET: state( $key, $value );
5271
	 * GET: $value = state( $key );
5272
	 *
5273
	 * @param string $key
5274
	 * @param string $value
5275
	 * @param bool $restate private
5276
	 */
5277
	public static function state( $key = null, $value = null, $restate = false ) {
5278
		static $state = array();
5279
		static $path, $domain;
5280
		if ( ! isset( $path ) ) {
5281
			require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
5282
			$admin_url = Jetpack::admin_url();
5283
			$bits      = parse_url( $admin_url );
5284
5285
			if ( is_array( $bits ) ) {
5286
				$path   = ( isset( $bits['path'] ) ) ? dirname( $bits['path'] ) : null;
5287
				$domain = ( isset( $bits['host'] ) ) ? $bits['host'] : null;
5288
			} else {
5289
				$path = $domain = null;
5290
			}
5291
		}
5292
5293
		// Extract state from cookies and delete cookies
5294
		if ( isset( $_COOKIE[ 'jetpackState' ] ) && is_array( $_COOKIE[ 'jetpackState' ] ) ) {
5295
			$yum = $_COOKIE[ 'jetpackState' ];
5296
			unset( $_COOKIE[ 'jetpackState' ] );
5297
			foreach ( $yum as $k => $v ) {
5298
				if ( strlen( $v ) )
5299
					$state[ $k ] = $v;
5300
				setcookie( "jetpackState[$k]", false, 0, $path, $domain );
5301
			}
5302
		}
5303
5304
		if ( $restate ) {
5305
			foreach ( $state as $k => $v ) {
5306
				setcookie( "jetpackState[$k]", $v, 0, $path, $domain );
5307
			}
5308
			return;
5309
		}
5310
5311
		// Get a state variable
5312
		if ( isset( $key ) && ! isset( $value ) ) {
5313
			if ( array_key_exists( $key, $state ) )
5314
				return $state[ $key ];
5315
			return null;
5316
		}
5317
5318
		// Set a state variable
5319
		if ( isset ( $key ) && isset( $value ) ) {
5320
			if( is_array( $value ) && isset( $value[0] ) ) {
5321
				$value = $value[0];
5322
			}
5323
			$state[ $key ] = $value;
5324
			setcookie( "jetpackState[$key]", $value, 0, $path, $domain );
5325
		}
5326
	}
5327
5328
	public static function restate() {
5329
		Jetpack::state( null, null, true );
5330
	}
5331
5332
	public static function check_privacy( $file ) {
5333
		static $is_site_publicly_accessible = null;
5334
5335
		if ( is_null( $is_site_publicly_accessible ) ) {
5336
			$is_site_publicly_accessible = false;
5337
5338
			Jetpack::load_xml_rpc_client();
5339
			$rpc = new Jetpack_IXR_Client();
5340
5341
			$success = $rpc->query( 'jetpack.isSitePubliclyAccessible', home_url() );
5342
			if ( $success ) {
5343
				$response = $rpc->getResponse();
5344
				if ( $response ) {
5345
					$is_site_publicly_accessible = true;
5346
				}
5347
			}
5348
5349
			Jetpack_Options::update_option( 'public', (int) $is_site_publicly_accessible );
5350
		}
5351
5352
		if ( $is_site_publicly_accessible ) {
5353
			return;
5354
		}
5355
5356
		$module_slug = self::get_module_slug( $file );
5357
5358
		$privacy_checks = Jetpack::state( 'privacy_checks' );
5359
		if ( ! $privacy_checks ) {
5360
			$privacy_checks = $module_slug;
5361
		} else {
5362
			$privacy_checks .= ",$module_slug";
5363
		}
5364
5365
		Jetpack::state( 'privacy_checks', $privacy_checks );
5366
	}
5367
5368
	/**
5369
	 * Helper method for multicall XMLRPC.
5370
	 */
5371
	public static function xmlrpc_async_call() {
5372
		global $blog_id;
5373
		static $clients = array();
5374
5375
		$client_blog_id = is_multisite() ? $blog_id : 0;
5376
5377
		if ( ! isset( $clients[$client_blog_id] ) ) {
5378
			Jetpack::load_xml_rpc_client();
5379
			$clients[$client_blog_id] = new Jetpack_IXR_ClientMulticall( array( 'user_id' => JETPACK_MASTER_USER, ) );
5380
			if ( function_exists( 'ignore_user_abort' ) ) {
5381
				ignore_user_abort( true );
5382
			}
5383
			add_action( 'shutdown', array( 'Jetpack', 'xmlrpc_async_call' ) );
5384
		}
5385
5386
		$args = func_get_args();
5387
5388
		if ( ! empty( $args[0] ) ) {
5389
			call_user_func_array( array( $clients[$client_blog_id], 'addCall' ), $args );
5390
		} elseif ( is_multisite() ) {
5391
			foreach ( $clients as $client_blog_id => $client ) {
5392
				if ( ! $client_blog_id || empty( $client->calls ) ) {
5393
					continue;
5394
				}
5395
5396
				$switch_success = switch_to_blog( $client_blog_id, true );
5397
				if ( ! $switch_success ) {
5398
					continue;
5399
				}
5400
5401
				flush();
5402
				$client->query();
5403
5404
				restore_current_blog();
5405
			}
5406
		} else {
5407
			if ( isset( $clients[0] ) && ! empty( $clients[0]->calls ) ) {
5408
				flush();
5409
				$clients[0]->query();
5410
			}
5411
		}
5412
	}
5413
5414
	public static function staticize_subdomain( $url ) {
5415
5416
		// Extract hostname from URL
5417
		$host = parse_url( $url, PHP_URL_HOST );
5418
5419
		// Explode hostname on '.'
5420
		$exploded_host = explode( '.', $host );
5421
5422
		// Retrieve the name and TLD
5423
		if ( count( $exploded_host ) > 1 ) {
5424
			$name = $exploded_host[ count( $exploded_host ) - 2 ];
5425
			$tld = $exploded_host[ count( $exploded_host ) - 1 ];
5426
			// Rebuild domain excluding subdomains
5427
			$domain = $name . '.' . $tld;
5428
		} else {
5429
			$domain = $host;
5430
		}
5431
		// Array of Automattic domains
5432
		$domain_whitelist = array( 'wordpress.com', 'wp.com' );
5433
5434
		// Return $url if not an Automattic domain
5435
		if ( ! in_array( $domain, $domain_whitelist ) ) {
5436
			return $url;
5437
		}
5438
5439
		if ( is_ssl() ) {
5440
			return preg_replace( '|https?://[^/]++/|', 'https://s-ssl.wordpress.com/', $url );
5441
		}
5442
5443
		srand( crc32( basename( $url ) ) );
5444
		$static_counter = rand( 0, 2 );
5445
		srand(); // this resets everything that relies on this, like array_rand() and shuffle()
5446
5447
		return preg_replace( '|://[^/]+?/|', "://s$static_counter.wp.com/", $url );
5448
	}
5449
5450
/* JSON API Authorization */
5451
5452
	/**
5453
	 * Handles the login action for Authorizing the JSON API
5454
	 */
5455
	function login_form_json_api_authorization() {
5456
		$this->verify_json_api_authorization_request();
5457
5458
		add_action( 'wp_login', array( &$this, 'store_json_api_authorization_token' ), 10, 2 );
5459
5460
		add_action( 'login_message', array( &$this, 'login_message_json_api_authorization' ) );
5461
		add_action( 'login_form', array( &$this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
5462
		add_filter( 'site_url', array( &$this, 'post_login_form_to_signed_url' ), 10, 3 );
5463
	}
5464
5465
	// Make sure the login form is POSTed to the signed URL so we can reverify the request
5466
	function post_login_form_to_signed_url( $url, $path, $scheme ) {
5467
		if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) {
5468
			return $url;
5469
		}
5470
5471
		$parsed_url = parse_url( $url );
5472
		$url = strtok( $url, '?' );
5473
		$url = "$url?{$_SERVER['QUERY_STRING']}";
5474
		if ( ! empty( $parsed_url['query'] ) )
5475
			$url .= "&{$parsed_url['query']}";
5476
5477
		return $url;
5478
	}
5479
5480
	// Make sure the POSTed request is handled by the same action
5481
	function preserve_action_in_login_form_for_json_api_authorization() {
5482
		echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n";
5483
		echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) . "' />\n";
5484
	}
5485
5486
	// If someone logs in to approve API access, store the Access Code in usermeta
5487
	function store_json_api_authorization_token( $user_login, $user ) {
5488
		add_filter( 'login_redirect', array( &$this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
5489
		add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_public_api_domain' ) );
5490
		$token = wp_generate_password( 32, false );
5491
		update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token );
5492
	}
5493
5494
	// Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access
5495
	function allow_wpcom_public_api_domain( $domains ) {
5496
		$domains[] = 'public-api.wordpress.com';
5497
		return $domains;
5498
	}
5499
5500
	// Add the Access Code details to the public-api.wordpress.com redirect
5501
	function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
5502
		return add_query_arg(
5503
			urlencode_deep(
5504
				array(
5505
					'jetpack-code'    => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], true ),
5506
					'jetpack-user-id' => (int) $user->ID,
5507
					'jetpack-state'   => $this->json_api_authorization_request['state'],
5508
				)
5509
			),
5510
			$redirect_to
5511
		);
5512
	}
5513
5514
	// Verifies the request by checking the signature
5515
	function verify_json_api_authorization_request() {
5516
		require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php';
5517
5518
		$token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
5519
		if ( ! $token || empty( $token->secret ) ) {
5520
			wp_die( __( 'You must connect your Jetpack plugin to WordPress.com to use this feature.' , 'jetpack' ) );
5521
		}
5522
5523
		$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' );
5524
5525
		$jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
5526
5527
		if ( isset( $_POST['jetpack_json_api_original_query'] ) ) {
5528
			$signature = $jetpack_signature->sign_request( $_GET['token'], $_GET['timestamp'], $_GET['nonce'], '', 'GET', $_POST['jetpack_json_api_original_query'], null, true );
5529
		} else {
5530
			$signature = $jetpack_signature->sign_current_request( array( 'body' => null, 'method' => 'GET' ) );
5531
		}
5532
5533
		if ( ! $signature ) {
5534
			wp_die( $die_error );
5535
		} else if ( is_wp_error( $signature ) ) {
5536
			wp_die( $die_error );
5537
		} else if ( $signature !== $_GET['signature'] ) {
5538
			if ( is_ssl() ) {
5539
				// 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
5540
				$signature = $jetpack_signature->sign_current_request( array( 'scheme' => 'http', 'body' => null, 'method' => 'GET' ) );
5541
				if ( ! $signature || is_wp_error( $signature ) || $signature !== $_GET['signature'] ) {
5542
					wp_die( $die_error );
5543
				}
5544
			} else {
5545
				wp_die( $die_error );
5546
			}
5547
		}
5548
5549
		$timestamp = (int) $_GET['timestamp'];
5550
		$nonce     = stripslashes( (string) $_GET['nonce'] );
5551
5552
		if ( ! $this->add_nonce( $timestamp, $nonce ) ) {
5553
			// De-nonce the nonce, at least for 5 minutes.
5554
			// 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)
5555
			$old_nonce_time = get_option( "jetpack_nonce_{$timestamp}_{$nonce}" );
5556
			if ( $old_nonce_time < time() - 300 ) {
5557
				wp_die( __( 'The authorization process expired.  Please go back and try again.' , 'jetpack' ) );
5558
			}
5559
		}
5560
5561
		$data = json_decode( base64_decode( stripslashes( $_GET['data'] ) ) );
5562
		$data_filters = array(
5563
			'state'        => 'opaque',
5564
			'client_id'    => 'int',
5565
			'client_title' => 'string',
5566
			'client_image' => 'url',
5567
		);
5568
5569
		foreach ( $data_filters as $key => $sanitation ) {
5570
			if ( ! isset( $data->$key ) ) {
5571
				wp_die( $die_error );
5572
			}
5573
5574
			switch ( $sanitation ) {
5575
			case 'int' :
5576
				$this->json_api_authorization_request[$key] = (int) $data->$key;
5577
				break;
5578
			case 'opaque' :
5579
				$this->json_api_authorization_request[$key] = (string) $data->$key;
5580
				break;
5581
			case 'string' :
5582
				$this->json_api_authorization_request[$key] = wp_kses( (string) $data->$key, array() );
5583
				break;
5584
			case 'url' :
5585
				$this->json_api_authorization_request[$key] = esc_url_raw( (string) $data->$key );
5586
				break;
5587
			}
5588
		}
5589
5590
		if ( empty( $this->json_api_authorization_request['client_id'] ) ) {
5591
			wp_die( $die_error );
5592
		}
5593
	}
5594
5595
	function login_message_json_api_authorization( $message ) {
5596
		return '<p class="message">' . sprintf(
5597
			esc_html__( '%s wants to access your site&#8217;s data.  Log in to authorize that access.' , 'jetpack' ),
5598
			'<strong>' . esc_html( $this->json_api_authorization_request['client_title'] ) . '</strong>'
5599
		) . '<img src="' . esc_url( $this->json_api_authorization_request['client_image'] ) . '" /></p>';
5600
	}
5601
5602
	/**
5603
	 * Get $content_width, but with a <s>twist</s> filter.
5604
	 */
5605
	public static function get_content_width() {
5606
		$content_width = isset( $GLOBALS['content_width'] ) ? $GLOBALS['content_width'] : false;
5607
		/**
5608
		 * Filter the Content Width value.
5609
		 *
5610
		 * @since 2.2.3
5611
		 *
5612
		 * @param string $content_width Content Width value.
5613
		 */
5614
		return apply_filters( 'jetpack_content_width', $content_width );
5615
	}
5616
5617
	/**
5618
	 * Centralize the function here until it gets added to core.
5619
	 *
5620
	 * @param int|string|object $id_or_email A user ID,  email address, or comment object
5621
	 * @param int $size Size of the avatar image
5622
	 * @param string $default URL to a default image to use if no avatar is available
5623
	 * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled
5624
	 *
5625
	 * @return array First element is the URL, second is the class.
5626
	 */
5627
	public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) {
5628
		// Don't bother adding the __return_true filter if it's already there.
5629
		$has_filter = has_filter( 'pre_option_show_avatars', '__return_true' );
5630
5631
		if ( $force_display && ! $has_filter )
5632
			add_filter( 'pre_option_show_avatars', '__return_true' );
5633
5634
		$avatar = get_avatar( $id_or_email, $size, $default );
5635
5636
		if ( $force_display && ! $has_filter )
5637
			remove_filter( 'pre_option_show_avatars', '__return_true' );
5638
5639
		// If no data, fail out.
5640
		if ( is_wp_error( $avatar ) || ! $avatar )
5641
			return array( null, null );
5642
5643
		// Pull out the URL.  If it's not there, fail out.
5644
		if ( ! preg_match( '/src=["\']([^"\']+)["\']/', $avatar, $url_matches ) )
5645
			return array( null, null );
5646
		$url = wp_specialchars_decode( $url_matches[1], ENT_QUOTES );
5647
5648
		// Pull out the class, but it's not a big deal if it's missing.
5649
		$class = '';
5650
		if ( preg_match( '/class=["\']([^"\']+)["\']/', $avatar, $class_matches ) )
5651
			$class = wp_specialchars_decode( $class_matches[1], ENT_QUOTES );
5652
5653
		return array( $url, $class );
5654
	}
5655
5656
	/**
5657
	 * Pings the WordPress.com Mirror Site for the specified options.
5658
	 *
5659
	 * @param string|array $option_names The option names to request from the WordPress.com Mirror Site
5660
	 *
5661
	 * @return array An associative array of the option values as stored in the WordPress.com Mirror Site
5662
	 */
5663
	public function get_cloud_site_options( $option_names ) {
5664
		$option_names = array_filter( (array) $option_names, 'is_string' );
5665
5666
		Jetpack::load_xml_rpc_client();
5667
		$xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) );
5668
		$xml->query( 'jetpack.fetchSiteOptions', $option_names );
5669
		if ( $xml->isError() ) {
5670
			return array(
5671
				'error_code' => $xml->getErrorCode(),
5672
				'error_msg'  => $xml->getErrorMessage(),
5673
			);
5674
		}
5675
		$cloud_site_options = $xml->getResponse();
5676
5677
		return $cloud_site_options;
5678
	}
5679
5680
	/**
5681
	 * Fetch the filtered array of options that we should compare to determine an identity crisis.
5682
	 *
5683
	 * @return array An array of options to check.
5684
	 */
5685
	public static function identity_crisis_options_to_check() {
5686
		return array(
5687
			'siteurl',
5688
			'home',
5689
		);
5690
	}
5691
5692
	/**
5693
	 * Checks to make sure that local options have the same values as remote options.  Will cache the results for up to 24 hours.
5694
	 *
5695
	 * @param bool $force_recheck Whether to ignore any cached transient and manually re-check.
5696
	 *
5697
	 * @return array An array of options that do not match.  If everything is good, it will evaluate to false.
5698
	 */
5699
	public static function check_identity_crisis( $force_recheck = false ) {
5700
		if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || Jetpack::is_staging_site() )
5701
			return false;
5702
5703
		if ( $force_recheck || false === ( $errors = get_transient( 'jetpack_has_identity_crisis' ) ) ) {
5704
			$options_to_check = self::identity_crisis_options_to_check();
5705
			$cloud_options = Jetpack::init()->get_cloud_site_options( $options_to_check );
5706
			$errors        = array();
5707
5708
			foreach ( $cloud_options as $cloud_key => $cloud_value ) {
5709
5710
				// If it's not the same as the local value...
5711
				if ( $cloud_value !== get_option( $cloud_key ) ) {
5712
5713
					// Break out if we're getting errors.  We are going to check the error keys later when we alert.
5714
					if ( 'error_code' == $cloud_key ) {
5715
						$errors[ $cloud_key ] = $cloud_value;
5716
						break;
5717
					}
5718
5719
					$parsed_cloud_value = parse_url( $cloud_value );
5720
					// If the current options is an IP address
5721
					if ( filter_var( $parsed_cloud_value['host'], FILTER_VALIDATE_IP ) ) {
5722
						// Give the new value a Jetpack to fly in to the clouds
5723
						Jetpack::resolve_identity_crisis( $cloud_key );
5724
						continue;
5725
					}
5726
5727
					// And it's not been added to the whitelist...
5728
					if ( ! self::is_identity_crisis_value_whitelisted( $cloud_key, $cloud_value ) ) {
5729
						/*
5730
						 * This should be a temporary hack until a cleaner solution is found.
5731
						 *
5732
						 * The siteurl and home can be set to use http in General > Settings
5733
						 * however some constants can be defined that can force https in wp-admin
5734
						 * when this happens wpcom can confuse wporg with a fake identity
5735
						 * crisis with a mismatch of http vs https when it should be allowed.
5736
						 * we need to check that here.
5737
						 *
5738
						 * @see https://github.com/Automattic/jetpack/issues/1006
5739
						 */
5740
						if ( ( 'home' == $cloud_key || 'siteurl' == $cloud_key )
5741
							&& ( substr( $cloud_value, 0, 8 ) == "https://" )
5742
							&& Jetpack::init()->is_ssl_required_to_visit_site() ) {
5743
							// Ok, we found a mismatch of http and https because of wp-config, not an invalid url
5744
							continue;
5745
						}
5746
5747
5748
						// Then kick an error!
5749
						$errors[ $cloud_key ] = $cloud_value;
5750
					}
5751
				}
5752
			}
5753
		}
5754
5755
		/**
5756
		 * Filters the errors returned when checking for an Identity Crisis.
5757
		 *
5758
		 * @since 2.3.2
5759
		 *
5760
		 * @param array $errors Array of Identity Crisis errors.
5761
		 * @param bool $force_recheck Ignore any cached transient and manually re-check. Default to false.
5762
		 */
5763
		return apply_filters( 'jetpack_has_identity_crisis', $errors, $force_recheck );
5764
	}
5765
5766
	/*
5767
	 * Resolve ID crisis
5768
	 *
5769
	 * If the URL has changed, but the rest of the options are the same (i.e. blog/user tokens)
5770
	 * The user has the option to update the shadow site with the new URL before a new
5771
	 * token is created.
5772
	 *
5773
	 * @param $key : Which option to sync.  null defautlts to home and siteurl
5774
	 */
5775
	public static function resolve_identity_crisis( $key = null ) {
5776
		if ( $key ) {
5777
			$identity_options = array( $key );
5778
		} else {
5779
			$identity_options = self::identity_crisis_options_to_check();
5780
		}
5781
5782
		if ( is_array( $identity_options ) ) {
5783
			foreach( $identity_options as $identity_option ) {
5784
				/**
5785
				 * Fires when a shadow site option is updated.
5786
				 * These options are updated via the Identity Crisis UI.
5787
				 * $identity_option is the option that gets updated.
5788
				 *
5789
				 * @since 3.7.0
5790
				 */
5791
				do_action( "update_option_{$identity_option}" );
5792
			}
5793
		}
5794
	}
5795
5796
	/*
5797
	 * Whitelist URL
5798
	 *
5799
	 * Ignore the URL differences between the blog and the shadow site.
5800
	 */
5801
	public static function whitelist_current_url() {
5802
		$options_to_check = Jetpack::identity_crisis_options_to_check();
5803
		$cloud_options = Jetpack::init()->get_cloud_site_options( $options_to_check );
5804
5805
		foreach ( $cloud_options as $cloud_key => $cloud_value ) {
5806
			Jetpack::whitelist_identity_crisis_value( $cloud_key, $cloud_value );
5807
		}
5808
	}
5809
5810
	/*
5811
	 * Ajax callbacks for ID crisis resolutions
5812
	 *
5813
	 * Things that could happen here:
5814
	 *  - site_migrated : Update the URL on the shadow blog to match new domain
5815
	 *  - whitelist     : Ignore the URL difference
5816
	 *  - default       : Error message
5817
	 */
5818
	public static function resolve_identity_crisis_ajax_callback() {
5819
		check_ajax_referer( 'resolve-identity-crisis', 'ajax-nonce' );
5820
5821
		switch ( $_POST[ 'crisis_resolution_action' ] ) {
5822
			case 'site_migrated':
5823
				Jetpack::resolve_identity_crisis();
5824
				echo 'resolved';
5825
				break;
5826
5827
			case 'whitelist':
5828
				Jetpack::whitelist_current_url();
5829
				echo 'whitelisted';
5830
				break;
5831
5832
			case 'reset_connection':
5833
				// Delete the options first so it doesn't get confused which site to disconnect dotcom-side
5834
				Jetpack_Options::delete_option(
5835
					array(
5836
						'register',
5837
						'blog_token',
5838
						'user_token',
5839
						'user_tokens',
5840
						'master_user',
5841
						'time_diff',
5842
						'fallback_no_verify_ssl_certs',
5843
						'id',
5844
					)
5845
				);
5846
				delete_transient( 'jetpack_has_identity_crisis' );
5847
5848
				echo 'reset-connection-success';
5849
				break;
5850
5851
			default:
5852
				echo 'missing action';
5853
				break;
5854
		}
5855
5856
		wp_die();
5857
	}
5858
5859
	/**
5860
	 * Adds a value to the whitelist for the specified key.
5861
	 *
5862
	 * @param string $key The option name that we're whitelisting the value for.
5863
	 * @param string $value The value that we're intending to add to the whitelist.
5864
	 *
5865
	 * @return bool Whether the value was added to the whitelist, or false if it was already there.
5866
	 */
5867
	public static function whitelist_identity_crisis_value( $key, $value ) {
5868
		if ( Jetpack::is_identity_crisis_value_whitelisted( $key, $value ) ) {
5869
			return false;
5870
		}
5871
5872
		$whitelist = Jetpack_Options::get_option( 'identity_crisis_whitelist', array() );
5873
		if ( empty( $whitelist[ $key ] ) || ! is_array( $whitelist[ $key ] ) ) {
5874
			$whitelist[ $key ] = array();
5875
		}
5876
		array_push( $whitelist[ $key ], $value );
5877
5878
		Jetpack_Options::update_option( 'identity_crisis_whitelist', $whitelist );
5879
		return true;
5880
	}
5881
5882
	/**
5883
	 * Checks whether a value is already whitelisted.
5884
	 *
5885
	 * @param string $key The option name that we're checking the value for.
5886
	 * @param string $value The value that we're curious to see if it's on the whitelist.
5887
	 *
5888
	 * @return bool Whether the value is whitelisted.
5889
	 */
5890
	public static function is_identity_crisis_value_whitelisted( $key, $value ) {
5891
		$whitelist = Jetpack_Options::get_option( 'identity_crisis_whitelist', array() );
5892
		if ( ! empty( $whitelist[ $key ] ) && is_array( $whitelist[ $key ] ) && in_array( $value, $whitelist[ $key ] ) ) {
5893
			return true;
5894
		}
5895
		return false;
5896
	}
5897
5898
	/**
5899
	 * Checks whether the home and siteurl specifically are whitelisted
5900
	 * Written so that we don't have re-check $key and $value params every time
5901
	 * we want to check if this site is whitelisted, for example in footer.php
5902
	 *
5903
	 * @return bool True = already whitelsisted False = not whitelisted
5904
	 */
5905
	public static function is_staging_site() {
5906
		$is_staging = false;
5907
5908
		$current_whitelist = Jetpack_Options::get_option( 'identity_crisis_whitelist' );
5909
		if ( $current_whitelist ) {
5910
			$options_to_check  = Jetpack::identity_crisis_options_to_check();
5911
			$cloud_options     = Jetpack::init()->get_cloud_site_options( $options_to_check );
5912
5913
			foreach ( $cloud_options as $cloud_key => $cloud_value ) {
5914
				if ( self::is_identity_crisis_value_whitelisted( $cloud_key, $cloud_value ) ) {
5915
					$is_staging = true;
5916
					break;
5917
				}
5918
			}
5919
		}
5920
		$known_staging = array(
5921
			'urls' => array(
5922
				'#\.staging\.wpengine\.com$#i',
5923
				),
5924
			'constants' => array(
5925
				'IS_WPE_SNAPSHOT',
5926
				'KINSTA_DEV_ENV',
5927
				'JETPACK_STAGING_MODE',
5928
				)
5929
			);
5930
		/**
5931
		 * Filters the flags of known staging sites.
5932
		 *
5933
		 * @since 3.9.0
5934
		 *
5935
		 * @param array $known_staging {
5936
		 *     An array of arrays that each are used to check if the current site is staging.
5937
		 *     @type array $urls      URLs of staging sites in regex to check against site_url.
5938
		 *     @type array $cosntants PHP constants of known staging/developement environments.
5939
		 *  }
5940
		 */
5941
		$known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
5942
5943
		if ( isset( $known_staging['urls'] ) ) {
5944
			foreach ( $known_staging['urls'] as $url ){
5945
				if ( preg_match( $url, site_url() ) ) {
5946
					$is_staging = true;
5947
					break;
5948
				}
5949
			}
5950
		}
5951
5952
		if ( isset( $known_staging['constants'] ) ) {
5953
			foreach ( $known_staging['constants'] as $constant ) {
5954
				if ( defined( $constant ) && constant( $constant ) ) {
5955
					$is_staging = true;
5956
				}
5957
			}
5958
		}
5959
5960
		/**
5961
		 * Filters is_staging_site check.
5962
		 *
5963
		 * @since 3.9.0
5964
		 *
5965
		 * @param bool $is_staging If the current site is a staging site.
5966
		 */
5967
		return apply_filters( 'jetpack_is_staging_site', $is_staging );
5968
	}
5969
5970
	public function identity_crisis_js( $nonce ) {
5971
?>
5972
<script>
5973
(function( $ ) {
5974
	var SECOND_IN_MS = 1000;
5975
5976
	function contactSupport( e ) {
5977
		e.preventDefault();
5978
		$( '.jp-id-crisis-question' ).hide();
5979
		$( '#jp-id-crisis-contact-support' ).show();
5980
	}
5981
5982
	function autodismissSuccessBanner() {
5983
		$( '.jp-identity-crisis' ).fadeOut(600); //.addClass( 'dismiss' );
5984
	}
5985
5986
	var data = { action: 'jetpack_resolve_identity_crisis', 'ajax-nonce': '<?php echo $nonce; ?>' };
5987
5988
	$( document ).ready(function() {
5989
5990
		// Site moved: Update the URL on the shadow blog
5991
		$( '.site-moved' ).click(function( e ) {
5992
			e.preventDefault();
5993
			data.crisis_resolution_action = 'site_migrated';
5994
			$( '#jp-id-crisis-question-1 .spinner' ).show();
5995
			$.post( ajaxurl, data, function() {
5996
				$( '.jp-id-crisis-question' ).hide();
5997
				$( '.banner-title' ).hide();
5998
				$( '#jp-id-crisis-success' ).show();
5999
				setTimeout( autodismissSuccessBanner, 6 * SECOND_IN_MS );
6000
			});
6001
6002
		});
6003
6004
		// URL hasn't changed, next question please.
6005
		$( '.site-not-moved' ).click(function( e ) {
6006
			e.preventDefault();
6007
			$( '.jp-id-crisis-question' ).hide();
6008
			$( '#jp-id-crisis-question-2' ).show();
6009
		});
6010
6011
		// Reset connection: two separate sites.
6012
		$( '.reset-connection' ).click(function( e ) {
6013
			data.crisis_resolution_action = 'reset_connection';
6014
			$.post( ajaxurl, data, function( response ) {
6015
				if ( 'reset-connection-success' === response ) {
6016
					window.location.replace( '<?php echo Jetpack::admin_url(); ?>' );
6017
				}
6018
			});
6019
		});
6020
6021
		// It's a dev environment.  Ignore.
6022
		$( '.is-dev-env' ).click(function( e ) {
6023
			data.crisis_resolution_action = 'whitelist';
6024
			$( '#jp-id-crisis-question-2 .spinner' ).show();
6025
			$.post( ajaxurl, data, function() {
6026
				$( '.jp-id-crisis-question' ).hide();
6027
				$( '.banner-title' ).hide();
6028
				$( '#jp-id-crisis-success' ).show();
6029
				setTimeout( autodismissSuccessBanner, 4 * SECOND_IN_MS );
6030
			});
6031
		});
6032
6033
		$( '.not-reconnecting' ).click(contactSupport);
6034
		$( '.not-staging-or-dev' ).click(contactSupport);
6035
	});
6036
})( jQuery );
6037
</script>
6038
<?php
6039
	}
6040
6041
	/**
6042
	 * Displays an admin_notice, alerting the user to an identity crisis.
6043
	 */
6044
	public function alert_identity_crisis() {
6045
		// @todo temporary killing of feature in 3.8.1 as it revealed a number of scenarios not foreseen.
6046
		if ( ! Jetpack::is_development_version() ) {
6047
			return;
6048
		}
6049
6050
		// @todo temporary copout for dealing with domain mapping
6051
		// @see https://github.com/Automattic/jetpack/issues/2702
6052
		if ( is_multisite() && defined( 'SUNRISE' ) && ! Jetpack::is_development_version() ) {
6053
			return;
6054
		}
6055
6056
		if ( ! current_user_can( 'jetpack_disconnect' ) ) {
6057
			return;
6058
		}
6059
6060
		if ( ! $errors = self::check_identity_crisis() ) {
6061
			return;
6062
		}
6063
6064
		// Only show on dashboard and jetpack pages
6065
		$screen = get_current_screen();
6066
		if ( 'dashboard' !== $screen->base && ! did_action( 'jetpack_notices' ) ) {
6067
			return;
6068
		}
6069
6070
		// Include the js!
6071
		$ajax_nonce = wp_create_nonce( 'resolve-identity-crisis' );
6072
		$this->identity_crisis_js( $ajax_nonce );
6073
6074
		// Include the CSS!
6075
		if ( ! wp_script_is( 'jetpack', 'done' ) ) {
6076
			$this->admin_banner_styles();
6077
		}
6078
6079
		if ( ! array_key_exists( 'error_code', $errors ) ) {
6080
			$key = 'siteurl';
6081
			if ( ! $errors[ $key ] ) {
6082
				$key = 'home';
6083
			}
6084
		} else {
6085
			$key = 'error_code';
6086
			// 401 is the only error we care about.  Any other errors should not trigger the alert.
6087
			if ( 401 !== $errors[ $key ] ) {
6088
				return;
6089
			}
6090
		}
6091
6092
		?>
6093
6094
		<style>
6095
			.jp-identity-crisis .jp-btn-group {
6096
					margin: 15px 0;
6097
				}
6098
			.jp-identity-crisis strong {
6099
					color: #518d2a;
6100
				}
6101
			.jp-identity-crisis.dismiss {
6102
				display: none;
6103
			}
6104
			.jp-identity-crisis .button {
6105
				margin-right: 4px;
6106
			}
6107
		</style>
6108
6109
		<div id="message" class="error jetpack-message jp-identity-crisis stay-visible">
6110
			<div class="service-mark"></div>
6111
			<div class="jp-id-banner__content">
6112
				<!-- <h3 class="banner-title"><?php _e( 'Something\'s not quite right with your Jetpack connection! Let\'s fix that.', 'jetpack' ); ?></h3> -->
6113
6114
				<div class="jp-id-crisis-question" id="jp-id-crisis-question-1">
6115
					<?php
6116
					// 401 means that this site has been disconnected from wpcom, but the remote site still thinks it's connected.
6117
					if ( 'error_code' == $key && '401' == $errors[ $key ] ) : ?>
6118
						<div class="banner-content">
6119
							<p><?php
6120
								/* translators: %s is a URL */
6121
								printf( __( 'Our records show that this site does not have a valid connection to WordPress.com. Please reset your connection to fix this. <a href="%s" target="_blank">What caused this?</a>', 'jetpack' ), 'https://jetpack.com/support/no-valid-wordpress-com-connection/' );
6122
							?></p>
6123
						</div>
6124
						<div class="jp-btn-group">
6125
							<a href="#" class="reset-connection"><?php _e( 'Reset the connection', 'jetpack' ); ?></a>
6126
							<span class="idc-separator">|</span>
6127
							<a href="<?php echo esc_url( wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=dismiss' ), 'jetpack-deactivate' ) ); ?>"><?php _e( 'Deactivate Jetpack', 'jetpack' ); ?></a>
6128
						</div>
6129
					<?php else : ?>
6130
							<div class="banner-content">
6131
							<p><?php printf( __( 'It looks like you may have changed your domain. Is <strong>%1$s</strong> still your site\'s domain, or have you updated it to <strong> %2$s </strong>?', 'jetpack' ), $errors[ $key ], (string) get_option( $key ) ); ?></p>
6132
							</div>
6133
						<div class="jp-btn-group">
6134
							<a href="#" class="regular site-moved"><?php printf( __( '%s is now my domain.', 'jetpack' ), $errors[ $key ] ); ?></a> <span class="idc-separator">|</span> <a href="#" class="site-not-moved" ><?php printf( __( '%s is still my domain.', 'jetpack' ), (string) get_option( $key ) ); ?></a>
6135
							<span class="spinner"></span>
6136
						</div>
6137
					<?php endif ; ?>
6138
				</div>
6139
6140
				<div class="jp-id-crisis-question" id="jp-id-crisis-question-2" style="display: none;">
6141
					<div class="banner-content">
6142
						<p><?php printf(
6143
							/* translators: %1$s, %2$s and %3$s are URLs */
6144
							__(
6145
								'Are <strong> %2$s </strong> and <strong> %1$s </strong> two completely separate websites? If so we should create a new connection, which will reset your followers and linked services. <a href="%3$s"><em>What does this mean?</em></a>',
6146
								'jetpack'
6147
							),
6148
							$errors[ $key ],
6149
							(string) get_option( $key ),
6150
							'https://jetpack.com/support/what-does-resetting-the-connection-mean/'
6151
						); ?></p>
6152
					</div>
6153
					<div class="jp-btn-group">
6154
						<a href="#" class="reset-connection"><?php _e( 'Reset the connection', 'jetpack' ); ?></a> <span class="idc-separator">|</span>
6155
						<a href="#" class="is-dev-env"><?php _e( 'This is a development environment', 'jetpack' ); ?></a> <span class="idc-separator">|</span>
6156
						<a href="https://jetpack.com/contact-support/" class="contact-support"><?php _e( 'Submit a support ticket', 'jetpack' ); ?></a>
6157
						<span class="spinner"></span>
6158
					</div>
6159
				</div>
6160
6161
				<div class="jp-id-crisis-success" id="jp-id-crisis-success" style="display: none;">
6162
					<h3 class="success-notice"><?php printf( __( 'Thanks for taking the time to sort things out. We&#039;ve updated our records accordingly!', 'jetpack' ) ); ?></h3>
6163
				</div>
6164
			</div>
6165
		</div>
6166
6167
		<?php
6168
	}
6169
6170
	/**
6171
	 * Maybe Use a .min.css stylesheet, maybe not.
6172
	 *
6173
	 * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args.
6174
	 */
6175
	public static function maybe_min_asset( $url, $path, $plugin ) {
6176
		// Short out on things trying to find actual paths.
6177
		if ( ! $path || empty( $plugin ) ) {
6178
			return $url;
6179
		}
6180
6181
		// Strip out the abspath.
6182
		$base = dirname( plugin_basename( $plugin ) );
6183
6184
		// Short out on non-Jetpack assets.
6185
		if ( 'jetpack/' !== substr( $base, 0, 8 ) ) {
6186
			return $url;
6187
		}
6188
6189
		// File name parsing.
6190
		$file              = "{$base}/{$path}";
6191
		$full_path         = JETPACK__PLUGIN_DIR . substr( $file, 8 );
6192
		$file_name         = substr( $full_path, strrpos( $full_path, '/' ) + 1 );
6193
		$file_name_parts_r = array_reverse( explode( '.', $file_name ) );
6194
		$extension         = array_shift( $file_name_parts_r );
6195
6196
		if ( in_array( strtolower( $extension ), array( 'css', 'js' ) ) ) {
6197
			// Already pointing at the minified version.
6198
			if ( 'min' === $file_name_parts_r[0] ) {
6199
				return $url;
6200
			}
6201
6202
			$min_full_path = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $full_path );
6203
			if ( file_exists( $min_full_path ) ) {
6204
				$url = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $url );
6205
			}
6206
		}
6207
6208
		return $url;
6209
	}
6210
6211
	/**
6212
	 * Maybe inlines a stylesheet.
6213
	 *
6214
	 * If you'd like to inline a stylesheet instead of printing a link to it,
6215
	 * wp_style_add_data( 'handle', 'jetpack-inline', true );
6216
	 *
6217
	 * Attached to `style_loader_tag` filter.
6218
	 *
6219
	 * @param string $tag The tag that would link to the external asset.
6220
	 * @param string $handle The registered handle of the script in question.
6221
	 *
6222
	 * @return string
6223
	 */
6224
	public static function maybe_inline_style( $tag, $handle ) {
6225
		global $wp_styles;
6226
		$item = $wp_styles->registered[ $handle ];
6227
6228
		if ( ! isset( $item->extra['jetpack-inline'] ) || ! $item->extra['jetpack-inline'] ) {
6229
			return $tag;
6230
		}
6231
6232
		if ( preg_match( '# href=\'([^\']+)\' #i', $tag, $matches ) ) {
6233
			$href = $matches[1];
6234
			// Strip off query string
6235
			if ( $pos = strpos( $href, '?' ) ) {
6236
				$href = substr( $href, 0, $pos );
6237
			}
6238
			// Strip off fragment
6239
			if ( $pos = strpos( $href, '#' ) ) {
6240
				$href = substr( $href, 0, $pos );
6241
			}
6242
		} else {
6243
			return $tag;
6244
		}
6245
6246
		$plugins_dir = plugin_dir_url( JETPACK__PLUGIN_FILE );
6247
		if ( $plugins_dir !== substr( $href, 0, strlen( $plugins_dir ) ) ) {
6248
			return $tag;
6249
		}
6250
6251
		// If this stylesheet has a RTL version, and the RTL version replaces normal...
6252
		if ( isset( $item->extra['rtl'] ) && 'replace' === $item->extra['rtl'] && is_rtl() ) {
6253
			// And this isn't the pass that actually deals with the RTL version...
6254
			if ( false === strpos( $tag, " id='$handle-rtl-css' " ) ) {
6255
				// Short out, as the RTL version will deal with it in a moment.
6256
				return $tag;
6257
			}
6258
		}
6259
6260
		$file = JETPACK__PLUGIN_DIR . substr( $href, strlen( $plugins_dir ) );
6261
		$css  = Jetpack::absolutize_css_urls( file_get_contents( $file ), $href );
6262
		if ( $css ) {
6263
			$tag = "<!-- Inline {$item->handle} -->\r\n";
6264
			if ( empty( $item->extra['after'] ) ) {
6265
				wp_add_inline_style( $handle, $css );
6266
			} else {
6267
				array_unshift( $item->extra['after'], $css );
6268
				wp_style_add_data( $handle, 'after', $item->extra['after'] );
6269
			}
6270
		}
6271
6272
		return $tag;
6273
	}
6274
6275
	/**
6276
	 * Loads a view file from the views
6277
	 *
6278
	 * Data passed in with the $data parameter will be available in the
6279
	 * template file as $data['value']
6280
	 *
6281
	 * @param string $template - Template file to load
6282
	 * @param array $data - Any data to pass along to the template
6283
	 * @return boolean - If template file was found
6284
	 **/
6285
	public function load_view( $template, $data = array() ) {
6286
		$views_dir = JETPACK__PLUGIN_DIR . 'views/';
6287
6288
		if( file_exists( $views_dir . $template ) ) {
6289
			require_once( $views_dir . $template );
6290
			return true;
6291
		}
6292
6293
		error_log( "Jetpack: Unable to find view file $views_dir$template" );
6294
		return false;
6295
	}
6296
6297
	/**
6298
	 * Throws warnings for deprecated hooks to be removed from Jetpack
6299
	 */
6300
	public function deprecated_hooks() {
6301
		global $wp_filter;
6302
6303
		/*
6304
		 * Format:
6305
		 * deprecated_filter_name => replacement_name
6306
		 *
6307
		 * If there is no replacement us null for replacement_name
6308
		 */
6309
		$deprecated_list = array(
6310
			'jetpack_bail_on_shortcode'                => 'jetpack_shortcodes_to_include',
6311
			'wpl_sharing_2014_1'                       => null,
6312
			'jetpack-tools-to-include'                 => 'jetpack_tools_to_include',
6313
			'jetpack_identity_crisis_options_to_check' => null,
6314
		);
6315
6316
		// This is a silly loop depth. Better way?
6317
		foreach( $deprecated_list AS $hook => $hook_alt ) {
6318
			if( isset( $wp_filter[ $hook ] ) && is_array( $wp_filter[ $hook ] ) ) {
6319
				foreach( $wp_filter[$hook] AS $func => $values ) {
6320
					foreach( $values AS $hooked ) {
6321
						_deprecated_function( $hook . ' used for ' . $hooked['function'], null, $hook_alt );
6322
					}
6323
				}
6324
			}
6325
		}
6326
	}
6327
6328
	/**
6329
	 * Converts any url in a stylesheet, to the correct absolute url.
6330
	 *
6331
	 * Considerations:
6332
	 *  - Normal, relative URLs     `feh.png`
6333
	 *  - Data URLs                 `data:image/gif;base64,eh129ehiuehjdhsa==`
6334
	 *  - Schema-agnostic URLs      `//domain.com/feh.png`
6335
	 *  - Absolute URLs             `http://domain.com/feh.png`
6336
	 *  - Domain root relative URLs `/feh.png`
6337
	 *
6338
	 * @param $css string: The raw CSS -- should be read in directly from the file.
6339
	 * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from.
6340
	 *
6341
	 * @return mixed|string
6342
	 */
6343
	public static function absolutize_css_urls( $css, $css_file_url ) {
6344
		$pattern = '#url\((?P<path>[^)]*)\)#i';
6345
		$css_dir = dirname( $css_file_url );
6346
		$p       = parse_url( $css_dir );
6347
		$domain  = sprintf(
6348
					'%1$s//%2$s%3$s%4$s',
6349
					isset( $p['scheme'] )           ? "{$p['scheme']}:" : '',
6350
					isset( $p['user'], $p['pass'] ) ? "{$p['user']}:{$p['pass']}@" : '',
6351
					$p['host'],
6352
					isset( $p['port'] )             ? ":{$p['port']}" : ''
6353
				);
6354
6355
		if ( preg_match_all( $pattern, $css, $matches, PREG_SET_ORDER ) ) {
6356
			$find = $replace = array();
6357
			foreach ( $matches as $match ) {
6358
				$url = trim( $match['path'], "'\" \t" );
6359
6360
				// If this is a data url, we don't want to mess with it.
6361
				if ( 'data:' === substr( $url, 0, 5 ) ) {
6362
					continue;
6363
				}
6364
6365
				// If this is an absolute or protocol-agnostic url,
6366
				// we don't want to mess with it.
6367
				if ( preg_match( '#^(https?:)?//#i', $url ) ) {
6368
					continue;
6369
				}
6370
6371
				switch ( substr( $url, 0, 1 ) ) {
6372
					case '/':
6373
						$absolute = $domain . $url;
6374
						break;
6375
					default:
6376
						$absolute = $css_dir . '/' . $url;
6377
				}
6378
6379
				$find[]    = $match[0];
6380
				$replace[] = sprintf( 'url("%s")', $absolute );
6381
			}
6382
			$css = str_replace( $find, $replace, $css );
6383
		}
6384
6385
		return $css;
6386
	}
6387
6388
	/**
6389
	 * This method checks to see if SSL is required by the site in
6390
	 * order to visit it in some way other than only setting the
6391
	 * https value in the home or siteurl values.
6392
	 *
6393
	 * @since 3.2
6394
	 * @return boolean
6395
	 **/
6396
	private function is_ssl_required_to_visit_site() {
6397
		global $wp_version;
6398
		$ssl = is_ssl();
6399
6400
		if ( force_ssl_admin() ) {
6401
			$ssl = true;
6402
		}
6403
		return $ssl;
6404
	}
6405
6406
	/**
6407
	 * This methods removes all of the registered css files on the frontend
6408
	 * from Jetpack in favor of using a single file. In effect "imploding"
6409
	 * all the files into one file.
6410
	 *
6411
	 * Pros:
6412
	 * - Uses only ONE css asset connection instead of 15
6413
	 * - Saves a minimum of 56k
6414
	 * - Reduces server load
6415
	 * - Reduces time to first painted byte
6416
	 *
6417
	 * Cons:
6418
	 * - Loads css for ALL modules. However all selectors are prefixed so it
6419
	 *		should not cause any issues with themes.
6420
	 * - Plugins/themes dequeuing styles no longer do anything. See
6421
	 *		jetpack_implode_frontend_css filter for a workaround
6422
	 *
6423
	 * For some situations developers may wish to disable css imploding and
6424
	 * instead operate in legacy mode where each file loads seperately and
6425
	 * can be edited individually or dequeued. This can be accomplished with
6426
	 * the following line:
6427
	 *
6428
	 * add_filter( 'jetpack_implode_frontend_css', '__return_false' );
6429
	 *
6430
	 * @since 3.2
6431
	 **/
6432
	public function implode_frontend_css( $travis_test = false ) {
6433
		$do_implode = true;
6434
		if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
6435
			$do_implode = false;
6436
		}
6437
6438
		/**
6439
		 * Allow CSS to be concatenated into a single jetpack.css file.
6440
		 *
6441
		 * @since 3.2.0
6442
		 *
6443
		 * @param bool $do_implode Should CSS be concatenated? Default to true.
6444
		 */
6445
		$do_implode = apply_filters( 'jetpack_implode_frontend_css', $do_implode );
6446
6447
		// Do not use the imploded file when default behaviour was altered through the filter
6448
		if ( ! $do_implode ) {
6449
			return;
6450
		}
6451
6452
		// We do not want to use the imploded file in dev mode, or if not connected
6453
		if ( Jetpack::is_development_mode() || ! self::is_active() ) {
6454
			if ( ! $travis_test ) {
6455
				return;
6456
			}
6457
		}
6458
6459
		// Do not use the imploded file if sharing css was dequeued via the sharing settings screen
6460
		if ( get_option( 'sharedaddy_disable_resources' ) ) {
6461
			return;
6462
		}
6463
6464
		/*
6465
		 * Now we assume Jetpack is connected and able to serve the single
6466
		 * file.
6467
		 *
6468
		 * In the future there will be a check here to serve the file locally
6469
		 * or potentially from the Jetpack CDN
6470
		 *
6471
		 * For now:
6472
		 * - Enqueue a single imploded css file
6473
		 * - Zero out the style_loader_tag for the bundled ones
6474
		 * - Be happy, drink scotch
6475
		 */
6476
6477
		add_filter( 'style_loader_tag', array( $this, 'concat_remove_style_loader_tag' ), 10, 2 );
6478
6479
		$version = Jetpack::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . 'css/jetpack.css' ) : JETPACK__VERSION;
6480
6481
		wp_enqueue_style( 'jetpack_css', plugins_url( 'css/jetpack.css', __FILE__ ), array(), $version );
6482
		wp_style_add_data( 'jetpack_css', 'rtl', 'replace' );
6483
	}
6484
6485
	function concat_remove_style_loader_tag( $tag, $handle ) {
6486
		if ( in_array( $handle, $this->concatenated_style_handles ) ) {
6487
			$tag = '';
6488
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
6489
				$tag = "<!-- `" . esc_html( $handle ) . "` is included in the concatenated jetpack.css -->\r\n";
6490
			}
6491
		}
6492
6493
		return $tag;
6494
	}
6495
6496
	/*
6497
	 * Check the heartbeat data
6498
	 *
6499
	 * Organizes the heartbeat data by severity.  For example, if the site
6500
	 * is in an ID crisis, it will be in the $filtered_data['bad'] array.
6501
	 *
6502
	 * Data will be added to "caution" array, if it either:
6503
	 *  - Out of date Jetpack version
6504
	 *  - Out of date WP version
6505
	 *  - Out of date PHP version
6506
	 *
6507
	 * $return array $filtered_data
6508
	 */
6509
	public static function jetpack_check_heartbeat_data() {
6510
		$raw_data = Jetpack_Heartbeat::generate_stats_array();
6511
6512
		$good    = array();
6513
		$caution = array();
6514
		$bad     = array();
6515
6516
		foreach ( $raw_data as $stat => $value ) {
6517
6518
			// Check jetpack version
6519
			if ( 'version' == $stat ) {
6520
				if ( version_compare( $value, JETPACK__VERSION, '<' ) ) {
6521
					$caution[ $stat ] = $value . " - min supported is " . JETPACK__VERSION;
6522
					continue;
6523
				}
6524
			}
6525
6526
			// Check WP version
6527
			if ( 'wp-version' == $stat ) {
6528
				if ( version_compare( $value, JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
6529
					$caution[ $stat ] = $value . " - min supported is " . JETPACK__MINIMUM_WP_VERSION;
6530
					continue;
6531
				}
6532
			}
6533
6534
			// Check PHP version
6535
			if ( 'php-version' == $stat ) {
6536
				if ( version_compare( PHP_VERSION, '5.2.4', '<' ) ) {
6537
					$caution[ $stat ] = $value . " - min supported is 5.2.4";
6538
					continue;
6539
				}
6540
			}
6541
6542
			// Check ID crisis
6543
			if ( 'identitycrisis' == $stat ) {
6544
				if ( 'yes' == $value ) {
6545
					$bad[ $stat ] = $value;
6546
					continue;
6547
				}
6548
			}
6549
6550
			// The rest are good :)
6551
			$good[ $stat ] = $value;
6552
		}
6553
6554
		$filtered_data = array(
6555
			'good'    => $good,
6556
			'caution' => $caution,
6557
			'bad'     => $bad
6558
		);
6559
6560
		return $filtered_data;
6561
	}
6562
6563
6564
	/*
6565
	 * This method is used to organize all options that can be reset
6566
	 * without disconnecting Jetpack.
6567
	 *
6568
	 * It is used in class.jetpack-cli.php to reset options
6569
	 *
6570
	 * @return array of options to delete.
6571
	 */
6572
	public static function get_jetpack_options_for_reset() {
6573
		$jetpack_options            = Jetpack_Options::get_option_names();
6574
		$jetpack_options_non_compat = Jetpack_Options::get_option_names( 'non_compact' );
6575
		$jetpack_options_private    = Jetpack_Options::get_option_names( 'private' );
6576
6577
		$all_jp_options = array_merge( $jetpack_options, $jetpack_options_non_compat, $jetpack_options_private );
6578
6579
		// A manual build of the wp options
6580
		$wp_options = array(
6581
			'sharing-options',
6582
			'disabled_likes',
6583
			'disabled_reblogs',
6584
			'jetpack_comments_likes_enabled',
6585
			'wp_mobile_excerpt',
6586
			'wp_mobile_featured_images',
6587
			'wp_mobile_app_promos',
6588
			'stats_options',
6589
			'stats_dashboard_widget',
6590
			'safecss_preview_rev',
6591
			'safecss_rev',
6592
			'safecss_revision_migrated',
6593
			'nova_menu_order',
6594
			'jetpack_portfolio',
6595
			'jetpack_portfolio_posts_per_page',
6596
			'jetpack_testimonial',
6597
			'jetpack_testimonial_posts_per_page',
6598
			'wp_mobile_custom_css',
6599
			'sharedaddy_disable_resources',
6600
			'sharing-options',
6601
			'sharing-services',
6602
			'site_icon_temp_data',
6603
			'featured-content',
6604
			'site_logo',
6605
		);
6606
6607
		// Flag some Jetpack options as unsafe
6608
		$unsafe_options = array(
6609
			'id',                           // (int)    The Client ID/WP.com Blog ID of this site.
6610
			'master_user',                  // (int)    The local User ID of the user who connected this site to jetpack.wordpress.com.
6611
			'version',                      // (string) Used during upgrade procedure to auto-activate new modules. version:time
6612
			'jumpstart',                    // (string) A flag for whether or not to show the Jump Start.  Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed.
6613
6614
			// non_compact
6615
			'activated',
6616
6617
			// private
6618
			'register',
6619
			'blog_token',                  // (string) The Client Secret/Blog Token of this site.
6620
			'user_token',                  // (string) The User Token of this site. (deprecated)
6621
			'user_tokens'
6622
		);
6623
6624
		// Remove the unsafe Jetpack options
6625
		foreach ( $unsafe_options as $unsafe_option ) {
6626
			if ( false !== ( $key = array_search( $unsafe_option, $all_jp_options ) ) ) {
6627
				unset( $all_jp_options[ $key ] );
6628
			}
6629
		}
6630
6631
		$options = array(
6632
			'jp_options' => $all_jp_options,
6633
			'wp_options' => $wp_options
6634
		);
6635
6636
		return $options;
6637
	}
6638
6639
	/**
6640
	 * Check if an option of a Jetpack module has been updated.
6641
	 *
6642
	 * If any module option has been updated before Jump Start has been dismissed,
6643
	 * update the 'jumpstart' option so we can hide Jump Start.
6644
	 *
6645
	 * @param string $option_name
6646
	 *
6647
	 * @return bool
6648
	 */
6649
	public static function jumpstart_has_updated_module_option( $option_name = '' ) {
6650
		// Bail if Jump Start has already been dismissed
6651
		if ( 'new_connection' !== Jetpack_Options::get_option( 'jumpstart' ) ) {
6652
			return false;
6653
		}
6654
6655
		$jetpack = Jetpack::init();
6656
6657
		// Manual build of module options
6658
		$option_names = self::get_jetpack_options_for_reset();
6659
6660
		if ( in_array( $option_name, $option_names['wp_options'] ) ) {
6661
			Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' );
6662
6663
			//Jump start is being dismissed send data to MC Stats
6664
			$jetpack->stat( 'jumpstart', 'manual,'.$option_name );
6665
6666
			$jetpack->do_stats( 'server_side' );
6667
		}
6668
6669
	}
6670
6671
	/*
6672
	 * Strip http:// or https:// from a url, replaces forward slash with ::,
6673
	 * so we can bring them directly to their site in calypso.
6674
	 *
6675
	 * @param string | url
6676
	 * @return string | url without the guff
6677
	 */
6678
	public static function build_raw_urls( $url ) {
6679
		$strip_http = '/.*?:\/\//i';
6680
		$url = preg_replace( $strip_http, '', $url  );
6681
		$url = str_replace( '/', '::', $url );
6682
		return $url;
6683
	}
6684
6685
	/**
6686
	 * Stores and prints out domains to prefetch for page speed optimization.
6687
	 *
6688
	 * @param mixed $new_urls
6689
	 */
6690
	public static function dns_prefetch( $new_urls = null ) {
6691
		static $prefetch_urls = array();
6692
		if ( empty( $new_urls ) && ! empty( $prefetch_urls ) ) {
6693
			echo "\r\n";
6694
			foreach ( $prefetch_urls as $this_prefetch_url ) {
6695
				printf( "<link rel='dns-prefetch' href='%s'>\r\n", esc_attr( $this_prefetch_url ) );
6696
			}
6697
		} elseif ( ! empty( $new_urls ) ) {
6698
			if ( ! has_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) ) ) {
6699
				add_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) );
6700
			}
6701
			foreach ( (array) $new_urls as $this_new_url ) {
6702
				$prefetch_urls[] = strtolower( untrailingslashit( preg_replace( '#^https?://#i', '//', $this_new_url ) ) );
6703
			}
6704
			$prefetch_urls = array_unique( $prefetch_urls );
6705
		}
6706
	}
6707
6708
	public function wp_dashboard_setup() {
6709
		if ( self::is_active() ) {
6710
			add_action( 'jetpack_dashboard_widget', array( __CLASS__, 'dashboard_widget_footer' ), 999 );
6711
			$widget_title = __( 'Site Stats', 'jetpack' );
6712
		} elseif ( ! self::is_development_mode() && current_user_can( 'jetpack_connect' ) ) {
6713
			add_action( 'jetpack_dashboard_widget', array( $this, 'dashboard_widget_connect_to_wpcom' ) );
6714
			$widget_title = __( 'Please Connect Jetpack', 'jetpack' );
6715
		}
6716
6717
		if ( has_action( 'jetpack_dashboard_widget' ) ) {
6718
			wp_add_dashboard_widget(
6719
				'jetpack_summary_widget',
6720
				$widget_title,
6721
				array( __CLASS__, 'dashboard_widget' )
6722
			);
6723
			wp_enqueue_style( 'jetpack-dashboard-widget', plugins_url( 'css/dashboard-widget.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
6724
6725
			// If we're inactive and not in development mode, sort our box to the top.
6726
			if ( ! self::is_active() && ! self::is_development_mode() ) {
6727
				global $wp_meta_boxes;
6728
6729
				$dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
6730
				$ours      = array( 'jetpack_summary_widget' => $dashboard['jetpack_summary_widget'] );
6731
6732
				$wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard );
6733
			}
6734
		}
6735
	}
6736
6737
	/**
6738
	 * @param mixed $result Value for the user's option
6739
	 * @return mixed
6740
	 */
6741
	function get_user_option_meta_box_order_dashboard( $sorted ) {
6742
		if ( ! is_array( $sorted ) ) {
6743
			return $sorted;
6744
		}
6745
6746
		foreach ( $sorted as $box_context => $ids ) {
6747
			if ( false === strpos( $ids, 'dashboard_stats' ) ) {
6748
				// If the old id isn't anywhere in the ids, don't bother exploding and fail out.
6749
				continue;
6750
			}
6751
6752
			$ids_array = explode( ',', $ids );
6753
			$key = array_search( 'dashboard_stats', $ids_array );
6754
6755
			if ( false !== $key ) {
6756
				// If we've found that exact value in the option (and not `google_dashboard_stats` for example)
6757
				$ids_array[ $key ] = 'jetpack_summary_widget';
6758
				$sorted[ $box_context ] = implode( ',', $ids_array );
6759
				// We've found it, stop searching, and just return.
6760
				break;
6761
			}
6762
		}
6763
6764
		return $sorted;
6765
	}
6766
6767
	public static function dashboard_widget() {
6768
		/**
6769
		 * Fires when the dashboard is loaded.
6770
		 *
6771
		 * @since 3.4.0
6772
		 */
6773
		do_action( 'jetpack_dashboard_widget' );
6774
	}
6775
6776
	public static function dashboard_widget_footer() {
6777
		?>
6778
		<footer>
6779
6780
		<div class="protect">
6781
			<?php if ( Jetpack::is_module_active( 'protect' ) ) : ?>
6782
				<h3><?php echo number_format_i18n( get_site_option( 'jetpack_protect_blocked_attempts', 0 ) ); ?></h3>
6783
				<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>
6784
			<?php elseif ( current_user_can( 'jetpack_activate_modules' ) && ! self::is_development_mode() ) : ?>
6785
				<a href="<?php echo esc_url( wp_nonce_url( Jetpack::admin_url( array( 'action' => 'activate', 'module' => 'protect' ) ), 'jetpack_activate-protect' ) ); ?>" class="button button-jetpack" title="<?php esc_attr_e( 'Protect helps to keep you secure from brute-force login attacks.', 'jetpack' ); ?>">
6786
					<?php esc_html_e( 'Activate Protect', 'jetpack' ); ?>
6787
				</a>
6788
			<?php else : ?>
6789
				<?php esc_html_e( 'Protect is inactive.', 'jetpack' ); ?>
6790
			<?php endif; ?>
6791
		</div>
6792
6793
		<div class="akismet">
6794
			<?php if ( is_plugin_active( 'akismet/akismet.php' ) ) : ?>
6795
				<h3><?php echo number_format_i18n( get_option( 'akismet_spam_count', 0 ) ); ?></h3>
6796
				<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>
6797 View Code Duplication
			<?php elseif ( current_user_can( 'activate_plugins' ) && ! is_wp_error( validate_plugin( 'akismet/akismet.php' ) ) ) : ?>
6798
				<a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'activate', 'plugin' => 'akismet/akismet.php' ), admin_url( 'plugins.php' ) ), 'activate-plugin_akismet/akismet.php' ) ); ?>" class="button button-jetpack">
6799
					<?php esc_html_e( 'Activate Akismet', 'jetpack' ); ?>
6800
				</a>
6801
			<?php else : ?>
6802
				<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>
6803
			<?php endif; ?>
6804
		</div>
6805
6806
6807 View Code Duplication
		<?php if ( ! current_user_can( 'edit_posts' ) && self::is_user_connected() ) : ?>
6808
			<div style="width: 100%; text-align: center; padding-top: 20px; clear: both;"><a class="button" title="<?php esc_attr_e( 'Unlink your account from WordPress.com', 'jetpack' ); ?>" href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'unlink', 'redirect' => 'sub-unlink' ), admin_url( 'index.php' ) ), 'jetpack-unlink' ) ); ?>"><?php esc_html_e( 'Unlink your account from WordPress.com', 'jetpack' ); ?></a></div>
6809
		<?php endif; ?>
6810
6811
		</footer>
6812
		<?php
6813
	}
6814
6815
	public function dashboard_widget_connect_to_wpcom() {
6816
		if ( Jetpack::is_active() || Jetpack::is_development_mode() || ! current_user_can( 'jetpack_connect' ) ) {
6817
			return;
6818
		}
6819
		?>
6820
		<div class="wpcom-connect">
6821
			<div class="jp-emblem">
6822
			<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0" y="0" viewBox="0 0 172.9 172.9" enable-background="new 0 0 172.9 172.9" xml:space="preserve">
6823
				<path d="M86.4 0C38.7 0 0 38.7 0 86.4c0 47.7 38.7 86.4 86.4 86.4s86.4-38.7 86.4-86.4C172.9 38.7 134.2 0 86.4 0zM83.1 106.6l-27.1-6.9C49 98 45.7 90.1 49.3 84l33.8-58.5V106.6zM124.9 88.9l-33.8 58.5V66.3l27.1 6.9C125.1 74.9 128.4 82.8 124.9 88.9z"/>
6824
			</svg>
6825
			</div>
6826
			<h3><?php esc_html_e( 'Please Connect Jetpack', 'jetpack' ); ?></h3>
6827
			<p><?php echo wp_kses( __( 'Connecting Jetpack will show you <strong>stats</strong> about your traffic, <strong>protect</strong> you from brute force attacks, <strong>speed up</strong> your images and photos, and enable other <strong>traffic and security</strong> features.', 'jetpack' ), 'jetpack' ) ?></p>
6828
6829
			<div class="actions">
6830
				<a href="<?php echo $this->build_connect_url( false, false, 'widget-btn' ); ?>" class="button button-primary">
6831
					<?php esc_html_e( 'Connect Jetpack', 'jetpack' ); ?>
6832
				</a>
6833
			</div>
6834
		</div>
6835
		<?php
6836
	}
6837
6838
	/*
6839
	 * A graceful transition to using Core's site icon.
6840
	 *
6841
	 * All of the hard work has already been done with the image
6842
	 * in all_done_page(). All that needs to be done now is update
6843
	 * the option and display proper messaging.
6844
	 *
6845
	 * @todo remove when WP 4.3 is minimum
6846
	 *
6847
	 * @since 3.6.1
6848
	 *
6849
	 * @return bool false = Core's icon not available || true = Core's icon is available
6850
	 */
6851
	public static function jetpack_site_icon_available_in_core() {
6852
		global $wp_version;
6853
		$core_icon_available = function_exists( 'has_site_icon' ) && version_compare( $wp_version, '4.3-beta' ) >= 0;
6854
6855
		if ( ! $core_icon_available ) {
6856
			return false;
6857
		}
6858
6859
		// No need for Jetpack's site icon anymore if core's is already set
6860
		if ( has_site_icon() ) {
6861
			if ( Jetpack::is_module_active( 'site-icon' ) ) {
6862
				Jetpack::log( 'deactivate', 'site-icon' );
6863
				Jetpack::deactivate_module( 'site-icon' );
6864
			}
6865
			return true;
6866
		}
6867
6868
		// Transfer Jetpack's site icon to use core.
6869
		$site_icon_id = Jetpack::get_option( 'site_icon_id' );
6870
		if ( $site_icon_id ) {
6871
			// Update core's site icon
6872
			update_option( 'site_icon', $site_icon_id );
6873
6874
			// Delete Jetpack's icon option. We still want the blavatar and attached data though.
6875
			delete_option( 'site_icon_id' );
6876
		}
6877
6878
		// No need for Jetpack's site icon anymore
6879
		if ( Jetpack::is_module_active( 'site-icon' ) ) {
6880
			Jetpack::log( 'deactivate', 'site-icon' );
6881
			Jetpack::deactivate_module( 'site-icon' );
6882
		}
6883
6884
		return true;
6885
	}
6886
6887
}
6888