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

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

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