PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 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 All 507 releases
← All changes | class.jetpack.php +1053 -1196 12.3.2 → 16.3-beta View file →
@@ -7,33 +7,38 @@
7 7 * @package automattic/jetpack
8 8 */
9 9
10 10 use Automattic\Jetpack\Assets;
11 -use Automattic\Jetpack\Assets\Logo as Jetpack_Logo;
12 11 use Automattic\Jetpack\Boost_Speed_Score\Speed_Score;
13 12 use Automattic\Jetpack\Config;
13 +use Automattic\Jetpack\Connection\Authorize_Json_Api;
14 14 use Automattic\Jetpack\Connection\Client;
15 -use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
16 15 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
17 -use Automattic\Jetpack\Connection\Nonce_Handler;
18 16 use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
19 17 use Automattic\Jetpack\Connection\Secrets;
20 18 use Automattic\Jetpack\Connection\Tokens;
21 -use Automattic\Jetpack\Connection\Utils as Connection_Utils;
19 +use Automattic\Jetpack\Connection\Webhooks\Authorize_Redirect;
22 20 use Automattic\Jetpack\Constants;
23 21 use Automattic\Jetpack\CookieState;
24 22 use Automattic\Jetpack\Current_Plan as Jetpack_Plan;
25 23 use Automattic\Jetpack\Device_Detection\User_Agent_Info;
26 24 use Automattic\Jetpack\Errors;
25 +use Automattic\Jetpack\Feature_Policy;
27 26 use Automattic\Jetpack\Files;
27 +use Automattic\Jetpack\Heartbeat;
28 28 use Automattic\Jetpack\Identity_Crisis;
29 +use Automattic\Jetpack\Import\Main as Import_Main;
29 30 use Automattic\Jetpack\Licensing;
30 31 use Automattic\Jetpack\Modules;
31 32 use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
32 -use Automattic\Jetpack\Partner;
33 +use Automattic\Jetpack\Newsletter\Reader_Link;
33 34 use Automattic\Jetpack\Paths;
35 +use Automattic\Jetpack\Plugin\Deprecate;
34 36 use Automattic\Jetpack\Plugin\Tracking as Plugin_Tracking;
37 +use Automattic\Jetpack\Podcast\Podcast;
35 38 use Automattic\Jetpack\Redirect;
39 +use Automattic\Jetpack\Scan_Page\Jetpack_Scan as Scan_Page_Init;
40 +use Automattic\Jetpack\SEO\Initializer as Jetpack_SEO_Initializer;
36 41 use Automattic\Jetpack\Status;
37 42 use Automattic\Jetpack\Status\Host;
38 43 use Automattic\Jetpack\Status\Visitor;
39 44 use Automattic\Jetpack\Sync\Actions as Sync_Actions;
@@ -40,9 +45,14 @@
40 45 use Automattic\Jetpack\Sync\Health;
41 46 use Automattic\Jetpack\Sync\Sender;
42 47 use Automattic\Jetpack\Terms_Of_Service;
43 48 use Automattic\Jetpack\Tracking;
49 +use Automattic\Woocommerce_Analytics;
44 50
51 +if ( ! defined( 'ABSPATH' ) ) {
52 + exit( 0 );
53 +}
54 +
45 55 /*
46 56 Options:
47 57 jetpack_options (array)
48 58 An array of options.
@@ -77,48 +87,8 @@
77 87 */
78 88 public $xmlrpc_server = null;
79 89
80 90 /**
81 - * The handles of styles that are concatenated into jetpack.css.
82 - *
83 - * When making changes to that list, you must also update concat_list in tools/webpack.config.css.js.
84 - *
85 - * @var array The handles of styles that are concatenated into jetpack.css.
86 - */
87 - public $concatenated_style_handles = array(
88 - 'jetpack-carousel-swiper-css',
89 - 'jetpack-carousel',
90 - 'grunion.css',
91 - 'the-neverending-homepage',
92 - 'jetpack_likes',
93 - 'jetpack_related-posts',
94 - 'sharedaddy',
95 - 'jetpack-slideshow',
96 - 'presentations',
97 - 'quiz',
98 - 'jetpack-subscriptions',
99 - 'jetpack-responsive-videos',
100 - 'jetpack-social-menu',
101 - 'tiled-gallery',
102 - 'jetpack_display_posts_widget',
103 - 'gravatar-profile-widget',
104 - 'goodreads-widget',
105 - 'jetpack_social_media_icons_widget',
106 - 'jetpack-top-posts-widget',
107 - 'jetpack_image_widget',
108 - 'jetpack-my-community-widget',
109 - 'jetpack-authors-widget',
110 - 'wordads',
111 - 'eu-cookie-law-style',
112 - 'flickr-widget-style',
113 - 'jetpack-search-widget',
114 - 'jetpack-simple-payments-widget-style',
115 - 'jetpack-widget-social-icons-styles',
116 - 'wpcom_instagram_widget',
117 - 'milestone-widget',
118 - );
119 -
120 - /**
121 91 * Contains all assets that have had their URL rewritten to minified versions.
122 92 *
123 93 * @var array
124 94 */
@@ -133,11 +103,8 @@
133 103 'contact-form' => array(
134 104 array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
135 105 array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
136 106 ),
137 - 'custom-css' => array(
138 - array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
139 - ),
140 107 'gravatar-hovercards' => array(
141 108 array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
142 109 ),
143 110 'latex' => array(
@@ -298,13 +265,8 @@
298 265 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php',
299 266 'Rank Math' => 'seo-by-rank-math/rank-math.php',
300 267 'Slim SEO' => 'slim-seo/slim-seo.php',
301 268 ),
302 - 'lazy-images' => array(
303 - 'Lazy Load' => 'lazy-load/lazy-load.php',
304 - 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php',
305 - 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php',
306 - ),
307 269 );
308 270
309 271 /**
310 272 * Plugins for which we turn off our Facebook OG Tags implementation.
@@ -313,9 +275,8 @@
313 275 * Graph tags via filter when their Social Meta modules are active:
314 276 *
315 277 * - All in One SEO Pack, All in one SEO Pack Pro
316 278 * - WordPress SEO by Yoast, WordPress SEO Premium by Yoast
317 - * - SEOPress, SEOPress Pro
318 279 *
319 280 * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter:
320 281 * add_filter( 'jetpack_enable_open_graph', '__return_false' );
321 282 *
@@ -358,8 +319,10 @@
358 319 'wp-caregiver/wp-caregiver.php', // WP Caregiver.
359 320 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', // WP Facebook Like Send & Open Graph Meta.
360 321 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol.
361 322 'wp-ogp/wp-ogp.php', // WP-OGP.
323 + 'wp-seopress/seopress.php', // SEOPress.
324 + 'wp-seopress-pro/seopress-pro.php', // SEOPress Pro.
362 325 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin.
363 326 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button.
364 327 'open-graph-metabox/open-graph-metabox.php', // Open Graph Metabox.
365 328 'seo-by-rank-math/rank-math.php', // Rank Math.
@@ -426,8 +389,10 @@
426 389
427 390 /**
428 391 * Verified data for JSON authorization request
429 392 *
393 + * @deprecated 13.4
394 + *
430 395 * @var array
431 396 */
432 397 public $json_api_authorization_request = array();
433 398
@@ -468,8 +433,16 @@
468 433 */
469 434 public static $instance = false;
470 435
471 436 /**
437 + * Resolved answer for `is_premium_analytics_enabled()`, or null before the first call.
438 + *
439 + * @since 16.1
440 + * @var bool|null
441 + */
442 + private static $premium_analytics_enabled = null;
443 +
444 + /**
472 445 * Singleton
473 446 *
474 447 * @static
475 448 */
@@ -489,17 +462,21 @@
489 462 public function plugin_upgrade() {
490 463 if ( self::is_connection_ready() ) {
491 464 list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
492 465 if ( JETPACK__VERSION !== $version ) {
493 - // Prevent multiple upgrades at once - only a single process should trigger
494 - // an upgrade to avoid stampedes.
495 - if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) {
496 - return;
466 + // Prevent multiple upgrades at once - only a single process should trigger an upgrade to avoid stampedes.
467 + if ( wp_using_ext_object_cache() ) {
468 + if ( true !== wp_cache_add( self::$plugin_upgrade_lock_key, 1, 'transient', 10 ) ) {
469 + return;
470 + }
471 + } else {
472 + if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) {
473 + return;
474 + }
475 +
476 + set_transient( self::$plugin_upgrade_lock_key, 1, 10 );
497 477 }
498 478
499 - // Set a short lock to prevent multiple instances of the upgrade.
500 - set_transient( self::$plugin_upgrade_lock_key, 1, 10 );
501 -
502 479 // check which active modules actually exist and remove others from active_modules list.
503 480 $unfiltered_modules = self::get_active_modules();
504 481 $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) );
505 482 if ( array_diff( $unfiltered_modules, $modules ) ) {
@@ -505,9 +482,9 @@
505 482 if ( array_diff( $unfiltered_modules, $modules ) ) {
506 483 self::update_active_modules( $modules );
507 484 }
508 485
509 - add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
486 + self::register_upgrade_init_hooks();
510 487
511 488 // Upgrade to 4.3.0.
512 489 if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) {
513 490 Jetpack_Options::delete_option( 'identity_crisis_whitelist' );
@@ -562,8 +539,16 @@
562 539 Jetpack_Options::delete_option( 'autoupdate_plugins' );
563 540 } // Should we have some type of fallback if something fails here?
564 541 }
565 542
543 + // Set the newsletter send default option for existing sites.
544 + if ( false === get_option( 'wpcom_newsletter_send_default' ) ) {
545 + add_option( 'wpcom_newsletter_send_default', 1 );
546 + }
547 +
548 + // Its handler went with the Recommendations assistant.
549 + wp_clear_scheduled_hook( 'jetpack_recommend_videopress' );
550 +
566 551 if ( did_action( 'wp_loaded' ) ) {
567 552 self::upgrade_on_load();
568 553 } else {
569 554 add_action(
@@ -597,9 +582,8 @@
597 582 }
598 583
599 584 if (
600 585 class_exists( 'Jetpack_Sitemap_Manager' )
601 - && version_compare( JETPACK__VERSION, '5.3', '>=' )
602 586 ) {
603 587 do_action( 'jetpack_sitemaps_purge_data' );
604 588 }
605 589
@@ -614,9 +598,9 @@
614 598 * Also fires Action hooks for each newly activated and deactivated module.
615 599 *
616 600 * @param array $modules Array of active modules to be saved in options.
617 601 *
618 - * @return $success bool true for success, false for failure.
602 + * @return bool $success true for success, false for failure.
619 603 */
620 604 public static function update_active_modules( $modules ) {
621 605 return ( new Modules() )->update_active( $modules );
622 606 }
@@ -646,10 +630,10 @@
646 630 sort( $taken_priorities );
647 631
648 632 $first_priority = array_shift( $taken_priorities );
649 633
650 - if ( defined( 'PHP_INT_MAX' ) && $first_priority <= - PHP_INT_MAX ) {
651 - $new_priority = - PHP_INT_MAX;
634 + if ( $first_priority <= PHP_INT_MIN ) {
635 + $new_priority = PHP_INT_MIN;
652 636 } else {
653 637 $new_priority = $first_priority - 1;
654 638 }
655 639
@@ -670,32 +654,17 @@
670 654 add_action( 'network_plugin_loaded', array( $this, 'add_configure_hook' ), 90 );
671 655 add_action( 'mu_plugin_loaded', array( $this, 'add_configure_hook' ), 90 );
672 656 add_action( 'plugins_loaded', array( $this, 'late_initialization' ), 90 );
673 657
674 - add_action( 'jetpack_verify_signature_error', array( $this, 'track_xmlrpc_error' ) );
675 -
676 - add_filter(
677 - 'jetpack_signature_check_token',
678 - array( __CLASS__, 'verify_onboarding_token' ),
679 - 10,
680 - 3
681 - );
682 -
683 658 /**
684 659 * Prepare Gutenberg Editor functionality
660 + *
661 + * The hooks previously here have been moved to modules/blocks.php but leaving this here pending
662 + * a longer investigation to see if code is expecting the Gutenberg class to always be available.
685 663 */
686 664 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-gutenberg.php';
687 - add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'load_independent_blocks' ) );
688 - add_action( 'plugins_loaded', array( 'Jetpack_Gutenberg', 'load_block_editor_extensions' ), 9 );
689 - add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
690 - add_filter( 'render_block', array( 'Jetpack_Gutenberg', 'display_deprecated_block_message' ), 10, 2 );
691 -
692 665 add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );
693 666
694 - // Unlink user before deleting the user from WP.com.
695 - add_action( 'deleted_user', array( $this, 'disconnect_user' ), 10, 1 );
696 - add_action( 'remove_user_from_blog', array( $this, 'disconnect_user' ), 10, 1 );
697 -
698 667 add_action( 'jetpack_event_log', array( 'Jetpack', 'log' ), 10, 2 );
699 668
700 669 add_filter( 'login_url', array( $this, 'login_url' ), 10, 2 );
701 670 add_action( 'login_init', array( $this, 'login_init' ) );
@@ -702,8 +671,13 @@
702 671
703 672 // Set up the REST authentication hooks.
704 673 Connection_Rest_Authentication::init();
705 674
675 + // Register Jetpack-specific connection tests (sync health, etc.) with the connection
676 + // package's health test suite. This runs on all requests (not just admin), because
677 + // the connection/test REST endpoint can be called outside admin context.
678 + add_action( 'jetpack_connection_tests_loaded', array( $this, 'register_jetpack_connection_tests' ) );
679 +
706 680 add_action( 'admin_init', array( $this, 'admin_init' ) );
707 681 add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
708 682
709 683 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ), 20 );
@@ -717,12 +691,8 @@
717 691
718 692 // Returns HTTPS support status.
719 693 add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) );
720 694
721 - add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) );
722 -
723 - add_action( 'wp_ajax_jetpack_recommendations_banner', array( 'Jetpack_Recommendations_Banner', 'ajax_callback' ) );
724 -
725 695 add_action( 'wp_loaded', array( $this, 'register_assets' ) );
726 696
727 697 /**
728 698 * These actions run checks to load additional files.
@@ -740,29 +710,8 @@
740 710
741 711 add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
742 712 add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
743 713
744 - require_once JETPACK__PLUGIN_DIR . 'class-jetpack-pre-connection-jitms.php';
745 - $jetpack_jitm_messages = ( new Jetpack_Pre_Connection_JITMs() );
746 - add_filter( 'jetpack_pre_connection_jitms', array( $jetpack_jitm_messages, 'add_pre_connection_jitms' ) );
747 -
748 - /*
749 - * If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin.
750 - * We should make sure to only do this for front end links.
751 - */
752 - if ( self::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) {
753 - add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 );
754 - add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 );
755 -
756 - /*
757 - * We'll shortcircuit wp_notify_postauthor and wp_notify_moderator pluggable functions
758 - * so they point moderation links on emails to Calypso.
759 - */
760 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/functions.wp-notify.php';
761 - add_filter( 'comment_notification_recipients', 'jetpack_notify_postauthor', 1, 2 );
762 - add_filter( 'notify_moderator', 'jetpack_notify_moderator', 1, 2 );
763 - }
764 -
765 714 add_action(
766 715 'plugins_loaded',
767 716 function () {
768 717 if ( User_Agent_Info::is_mobile_app() ) {
@@ -773,18 +722,10 @@
773 722
774 723 // Update the site's Jetpack plan and products from API on heartbeats.
775 724 add_action( 'jetpack_heartbeat', array( Jetpack_Plan::class, 'refresh_from_wpcom' ) );
776 725
777 - /**
778 - * This is the hack to concatenate all css files into one.
779 - * For description and reasoning see the implode_frontend_css method.
780 - *
781 - * Super late priority so we catch all the registered styles.
782 - */
783 - if ( ! is_admin() ) {
784 - add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first.
785 - add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles`.
786 - }
726 + // The Connection package fetches the site record for `jetpack/v4/site`; reuse it to refresh the plan.
727 + add_action( 'jetpack_site_data_fetched', array( Jetpack_Plan::class, 'update_from_site_record' ) );
787 728
788 729 // Actually push the stats on shutdown.
789 730 if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
790 731 add_action( 'shutdown', array( $this, 'push_stats' ) );
@@ -792,8 +733,10 @@
792 733
793 734 // After a successful connection.
794 735 add_action( 'jetpack_site_registered', array( $this, 'activate_default_modules_on_site_register' ) );
795 736 add_action( 'jetpack_site_registered', array( $this, 'handle_unique_registrations_stats' ) );
737 + add_action( 'jetpack_site_registered', array( Reader_Link::class, 'activate_on_connection' ), 9 );
738 + add_action( 'jetpack_site_registered', array( \Automattic\Jetpack\Reprint_Export\Reprint_Exporter::class, 'discard_credentials' ) );
796 739
797 740 // Actions for Manager::authorize().
798 741 add_action( 'jetpack_authorize_starting', array( $this, 'authorize_starting' ) );
799 742 add_action( 'jetpack_authorize_ending_linked', array( $this, 'authorize_ending_linked' ) );
@@ -798,8 +741,9 @@
798 741 add_action( 'jetpack_authorize_starting', array( $this, 'authorize_starting' ) );
799 742 add_action( 'jetpack_authorize_ending_linked', array( $this, 'authorize_ending_linked' ) );
800 743 add_action( 'jetpack_authorize_ending_authorized', array( $this, 'authorize_ending_authorized' ) );
801 744
745 + Jetpack_Client_Server::init();
802 746 add_action( 'jetpack_client_authorize_error', array( Jetpack_Client_Server::class, 'client_authorize_error' ) );
803 747 add_filter( 'jetpack_client_authorize_already_authorized_url', array( Jetpack_Client_Server::class, 'client_authorize_already_authorized_url' ) );
804 748 add_action( 'jetpack_client_authorize_processing', array( Jetpack_Client_Server::class, 'client_authorize_processing' ) );
805 749 add_filter( 'jetpack_client_authorize_fallback_url', array( Jetpack_Client_Server::class, 'client_authorize_fallback_url' ) );
@@ -804,9 +748,9 @@
804 748 add_action( 'jetpack_client_authorize_processing', array( Jetpack_Client_Server::class, 'client_authorize_processing' ) );
805 749 add_filter( 'jetpack_client_authorize_fallback_url', array( Jetpack_Client_Server::class, 'client_authorize_fallback_url' ) );
806 750
807 751 // Filters for the Manager::get_token() urls and request body.
808 - add_filter( 'jetpack_token_redirect_url', array( __CLASS__, 'filter_connect_redirect_url' ) );
752 + add_filter( 'jetpack_token_redirect_url', array( Authorize_Redirect::class, 'filter_connect_redirect_url' ) );
809 753 add_filter( 'jetpack_token_request_body', array( __CLASS__, 'filter_token_request_body' ) );
810 754
811 755 // Filter for the `jetpack/v4/connection/data` API response.
812 756 add_filter( 'jetpack_current_user_connection_data', array( __CLASS__, 'filter_jetpack_current_user_connection_data' ) );
@@ -827,18 +771,189 @@
827 771 // Make resources use static domain when possible.
828 772 add_filter( 'jetpack_static_url', array( 'Automattic\\Jetpack\\Assets', 'staticize_subdomain' ) );
829 773
830 774 // Validate the domain names in Jetpack development versions.
831 - add_action( 'jetpack_pre_register', array( get_called_class(), 'registration_check_domains' ) );
775 + add_action( 'jetpack_pre_register', array( static::class, 'registration_check_domains' ) );
832 776
833 777 // Register product descriptions for partner coupon usage.
834 778 add_filter( 'jetpack_partner_coupon_products', array( $this, 'get_partner_coupon_product_descriptions' ) );
835 779
836 - // Actions for conditional recommendations.
837 - add_action( 'plugins_loaded', array( 'Jetpack_Recommendations', 'init_conditional_recommendation_actions' ) );
780 + // Add 5-star
781 + add_filter( 'plugin_row_meta', array( $this, 'add_5_star_review_link' ), 10, 2 );
782 + add_action( 'init', array( Deprecate::class, 'instance' ) );
783 +
784 + // Register Jetpack module management abilities (WordPress Abilities API, WP 6.9+).
785 + \Automattic\Jetpack\Plugin\Abilities\Modules_Abilities::init();
786 +
787 + // Register Connection abilities (WordPress Abilities API, WP 6.9+). Scoped to the
788 + // Jetpack plugin for now: the Connection package no longer auto-wires these, so
789 + // connection-only consumers (Boost, Protect, Search, etc.) do not register them yet.
790 + \Automattic\Jetpack\Connection\Abilities\Connection_Abilities::init();
838 791 }
839 792
840 793 /**
794 + * Whether the current request should eagerly initialize the admin/REST-only
795 + * packages (the Import package and My Jetpack) now, at `plugins_loaded` time.
796 + *
797 + * Returns true for admin, cron, POST, and WP-CLI requests — the contexts,
798 + * knowable this early, where those packages have work to do. Returns false
799 + * for a plain front-end GET *and* for a REST request: the two can't be told
800 + * apart yet (this runs before `rest_api_init`), so callers defer the REST
801 + * case by initializing the package on `rest_api_init` instead, while a plain
802 + * page view never fires that hook and so loads nothing. This keeps
803 + * admin/REST-only PHP out of opcache on the front-end GET hot path.
804 + *
805 + * No in-repo code depends on the deferral. The one externally observable
806 + * change is timing: the packages' documented init hooks
807 + * (`jetpack_import_initialized`, `jetpack_feature_import_enabled`, and
808 + * `my_jetpack_init`) no longer fire on a plain front-end GET — they fire on
809 + * the admin, cron, POST, WP-CLI, and REST requests where the packages load.
810 + *
811 + * @return bool
812 + */
813 + private static function should_eager_load_packages() {
814 + $is_post_request = isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) );
815 + $is_wp_cli = Constants::is_true( 'WP_CLI' );
816 +
817 + return is_admin() || wp_doing_cron() || $is_post_request || $is_wp_cli;
818 + }
819 +
820 + /**
821 + * Configure the Import package from a deferred hook.
822 + *
823 + * The eager path uses Config::ensure( 'import' ), but the deferred REST path
824 + * runs after Config::on_plugins_loaded() has already processed its feature
825 + * flags, so it needs a hookable bootstrap callback. Preserve Config's
826 + * feature-enabled action for hook consumers.
827 + *
828 + * @since 16.0
829 + *
830 + * @return void
831 + */
832 + public static function configure_import_package() {
833 + if ( class_exists( Import_Main::class ) ) {
834 + Import_Main::configure();
835 +
836 + if ( ! did_action( 'jetpack_feature_import_enabled' ) ) {
837 + do_action( 'jetpack_feature_import_enabled' );
838 + }
839 + }
840 + }
841 +
842 + /**
843 + * Enable the bundled Backup dashboard.
844 + *
845 + * The standalone plugin initializes the package first, so with both active this is a no-op.
846 + *
847 + * @return void
848 + */
849 + public static function configure_backup_package() {
850 + // Not offered on multisite, which the Backup package does not support, or without a
851 + // connected owner, since buying and managing backups needs a linked account.
852 + if ( is_multisite() || ! self::is_connection_ready() || ! self::connection()->has_connected_owner() ) {
853 + return;
854 + }
855 +
856 + if ( ! self::is_module_active( 'backup' ) ) {
857 + return;
858 + }
859 +
860 + /**
861 + * Filters whether the Jetpack plugin offers its bundled Backup dashboard.
862 + *
863 + * Resolved at the earliest `plugins_loaded` priority, so hook it from a mu-plugin.
864 + *
865 + * @since 16.3
866 + *
867 + * @param bool $enabled Whether to initialize the bundled Backup dashboard. Default true.
868 + */
869 + if ( ! apply_filters( 'jetpack_backup_dashboard_enabled', true ) ) {
870 + return;
871 + }
872 +
873 + $backup = 'Automattic\\Jetpack\\Backup\\V0005\\Jetpack_Backup';
874 +
875 + // An older package would take over the connection (see Jetpack_Backup::DEFAULT_INIT_OPTIONS);
876 + // only the standalone plugin ships one that old, and it draws its own menu.
877 + if ( ! class_exists( $backup ) || ! defined( $backup . '::DEFAULT_INIT_OPTIONS' ) ) {
878 + return;
879 + }
880 +
881 + $backup::initialize( array( 'manage_connection' => false ) );
882 + }
883 +
884 + /**
885 + * Whether the bundled Stats v2 dashboard is enabled.
886 + *
887 + * Stats v2 (formerly "Premium Analytics") ships with the plugin behind this
888 + * flag while it rolls out (WOOA7S-1595). When enabled it adds its own admin
889 + * menu alongside the existing Stats UI; it never replaces or hides the
890 + * legacy Stats menu, admin-bar entries, post-list column, or WP dashboard
891 + * widget. The Stats module's tracking is unaffected either way, and Stats v2
892 + * reads what that module collects, so while the module is off the plugin
893 + * answers false here before even reading the flag.
894 + *
895 + * The package has to be loadable for this to be true, so a site with the
896 + * flag on but a missing package answers false here and never adds the
897 + * Stats v2 menu (a warning is logged instead).
898 + *
899 + * @since 16.1
900 + *
901 + * @return bool
902 + */
903 + public static function is_premium_analytics_enabled() {
904 + if ( null !== self::$premium_analytics_enabled ) {
905 + return self::$premium_analytics_enabled;
906 + }
907 +
908 + if ( ! self::is_module_active( 'stats' ) ) {
909 + self::$premium_analytics_enabled = false;
910 + return false;
911 + }
912 +
913 + /**
914 + * Filters whether the bundled Premium Analytics dashboard is enabled.
915 + *
916 + * Resolved once, from `Jetpack::configure()` on `plugins_loaded`, and only
917 + * while the Stats module is active. Register this from a mu-plugin or a
918 + * plugin's main file — a callback added on `plugins_loaded` or later runs
919 + * too late to be seen.
920 + *
921 + * @since 16.1
922 + *
923 + * @param bool $enabled Defaults to the `jetpack_premium_analytics_enabled` option (false).
924 + */
925 + $flag = (bool) apply_filters( 'jetpack_premium_analytics_enabled', (bool) get_option( 'jetpack_premium_analytics_enabled' ) );
926 +
927 + self::$premium_analytics_enabled = $flag && class_exists( 'Automattic\Jetpack\PremiumAnalytics\Analytics' );
928 +
929 + if ( $flag && ! self::$premium_analytics_enabled ) {
930 + wp_trigger_error(
931 + __METHOD__,
932 + 'The jetpack_premium_analytics_enabled flag is on but the Premium Analytics package is not loadable; keeping the Stats UI in place.'
933 + );
934 + }
935 +
936 + return self::$premium_analytics_enabled;
937 + }
938 +
939 + /**
940 + * Expose the setting that turns the Premium Analytics dashboard on and off.
941 + *
942 + * Deliberately not behind is_premium_analytics_enabled(): this is the setting that flips that
943 + * check, so it has to answer while the dashboard is still off.
944 + *
945 + * @since 16.2
946 + *
947 + * @return void
948 + */
949 + public static function register_premium_analytics_enablement_setting() {
950 + if ( class_exists( 'Automattic\Jetpack\PremiumAnalytics\Enablement_Setting' ) ) {
951 + \Automattic\Jetpack\PremiumAnalytics\Enablement_Setting::register();
952 + }
953 + }
954 +
955 + /**
841 956 * Before everything else starts getting initalized, we need to initialize Jetpack using the
842 957 * Config object.
843 958 */
844 959 public function configure() {
@@ -847,13 +962,10 @@
847 962 foreach (
848 963 array(
849 964 'jitm',
850 965 'sync',
966 + 'account_protection',
851 967 'waf',
852 - 'videopress',
853 - 'stats',
854 - 'stats_admin',
855 - 'import',
856 968 )
857 969 as $feature
858 970 ) {
859 971 $config->ensure( $feature );
@@ -858,8 +970,110 @@
858 970 ) {
859 971 $config->ensure( $feature );
860 972 }
861 973
974 + // Enable the VideoPress admin UI (the "Jetpack > VideoPress" dashboard) inside the
975 + // Jetpack plugin, mirroring the standalone Jetpack VideoPress plugin. The dashboard
976 + // only renders when the VideoPress module is active (Status::is_active()); when it
977 + // is not, the menu item links to the My Jetpack interstitial to activate it.
978 + $config->ensure( 'videopress', array( 'admin_ui' => true ) );
979 +
980 + // The Backup dashboard is only an admin menu and REST routes, so it is deferred like Import below.
981 + if ( self::should_eager_load_packages() ) {
982 + self::configure_backup_package();
983 + } else {
984 + add_action( 'rest_api_init', array( __CLASS__, 'configure_backup_package' ), 0 );
985 + }
986 +
987 + /*
988 + * The Import package only registers `jetpack/v4/import` REST routes — it
989 + * does nothing when rendering a front-end page — so gate its `ensure()`
990 + * to keep its PHP out of opcache on the front-end GET hot path. It still
991 + * loads on admin, cron, POST, and WP-CLI requests, and on `rest_api_init`
992 + * for REST: a REST request can't be identified yet at `plugins_loaded`
993 + * (this runs before `Config::on_plugins_loaded`, and `rest_api_init`
994 + * fires later), so it is initialized directly when that hook fires, while
995 + * a plain page view never fires it and so loads nothing.
996 + *
997 + * JITM stays eager (above): unlike Import, its `register()` adds a
998 + * `jetpack_sync_before_send_updated_option` filter that records the
999 + * `jetpack_last_plugin_sync` transient, and a Jetpack Sync send can fire
1000 + * on a plain front-end GET — including the dedicated-sync `spawn-sync`
1001 + * GET, which runs on `init` and exits before `rest_api_init`. Deferring
1002 + * JITM would skip that bookkeeping and leave its message cache stale after
1003 + * a plugin change, so it loads on every request as before.
1004 + */
1005 + if ( self::should_eager_load_packages() ) {
1006 + $config->ensure( 'import' );
1007 + } else {
1008 + add_action(
1009 + 'rest_api_init',
1010 + array( __CLASS__, 'configure_import_package' ),
1011 + 0
1012 + );
1013 + }
1014 +
1015 + /*
1016 + * The Stats and Stats Admin packages only do work when the Stats module
1017 + * is active (the front-end tracking pixel) or on wp-admin, REST, cron,
1018 + * POST, and WP-CLI requests: the Stats dashboard page, the stats /
1019 + * stats-app REST endpoints (which the block editor also calls for
1020 + * email-open rates), the transient-cleanup cron, the connection
1021 + * package's package-version tracker (which runs on POSTs and reads the
1022 + * `jetpack_package_versions` filter that Stats registers), and CLI
1023 + * introspection such as the heartbeat inspector. On a plain front-end
1024 + * GET page view with the module off they are inert: the pixel
1025 + * short-circuits on `Stats\Main::should_track()` and every other entry
1026 + * point only hooks rest_api_init, admin, cron, the POST-only tracker, or
1027 + * is reached through WP-CLI.
1028 + * Skip loading them — and eagerly constructing the Stats Admin REST
1029 + * controller — on that hot path to keep their PHP out of opcache, but
1030 + * keep loading them everywhere else exactly as before so the stats REST
1031 + * permission mapping (view_stats, registered by Stats\Main), the
1032 + * editor's stats-app calls, the cleanup cron, and the package-version
1033 + * tracker are all unchanged.
1034 + *
1035 + * REST requests are not yet identifiable here (REST_REQUEST is defined
1036 + * after plugins_loaded), so defer those to rest_api_init. Call the
1037 + * package initializers directly rather than `$config->ensure()`: ensure()
1038 + * only flags a feature for `Config::on_plugins_loaded()` (plugins_loaded
1039 + * priority 2), which has already run by the time rest_api_init fires.
1040 + * Priority 0 runs before each package's own priority-10 route
1041 + * registration, so their routes still register within the same dispatch.
1042 + * A plain page view never fires rest_api_init, so the packages stay
1043 + * unloaded there. See JETPACK-1747.
1044 + */
1045 + $is_post_request = isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'];
1046 + $is_wp_cli = defined( 'WP_CLI' ) && WP_CLI;
1047 +
1048 + if ( self::is_module_active( 'stats' ) || is_admin() || wp_doing_cron() || $is_post_request || $is_wp_cli ) {
1049 + $config->ensure( 'stats' );
1050 + $config->ensure( 'stats_admin' );
1051 + } else {
1052 + add_action(
1053 + 'rest_api_init',
1054 + static function () {
1055 + if ( class_exists( 'Automattic\Jetpack\Stats\Main' ) ) {
1056 + \Automattic\Jetpack\Stats\Main::init();
1057 + }
1058 + if ( class_exists( 'Automattic\Jetpack\Stats_Admin\Main' ) ) {
1059 + \Automattic\Jetpack\Stats_Admin\Main::init();
1060 + }
1061 + },
1062 + 0
1063 + );
1064 + }
1065 +
1066 + // Stats v2 (WOOA7S-1595). Unlike Stats above it cannot be deferred when enabled — see
1067 + // Analytics::init() for why, and for why it takes no menu_title here.
1068 + if ( self::is_premium_analytics_enabled() ) {
1069 + \Automattic\Jetpack\PremiumAnalytics\Analytics::init();
1070 + }
1071 +
1072 + // Outside the check above on purpose — see Enablement_Setting. Deferred like Stats, to keep
1073 + // the autoload off the front-end hot path.
1074 + add_action( 'rest_api_init', array( __CLASS__, 'register_premium_analytics_enablement_setting' ), 0 );
1075 +
862 1076 $config->ensure(
863 1077 'connection',
864 1078 array(
865 1079 'slug' => 'jetpack',
@@ -877,12 +1091,8 @@
877 1091 );
878 1092
879 1093 $config->ensure( 'search' );
880 1094
881 - if ( defined( 'ENABLE_WORDADS_SHARED_UI' ) && ENABLE_WORDADS_SHARED_UI ) {
882 - $config->ensure( 'wordads' );
883 - }
884 -
885 1095 if ( ! $this->connection_manager ) {
886 1096 $this->connection_manager = new Connection_Manager( 'jetpack' );
887 1097 }
888 1098
@@ -890,8 +1100,10 @@
890 1100 if ( $modules->is_active( 'publicize' ) && $this->connection_manager->has_connected_user() ) {
891 1101 $config->ensure( 'publicize' );
892 1102 }
893 1103
1104 + add_action( 'jetpack_initialize_tracking', array( $this, 'initialize_tracking' ) );
1105 +
894 1106 /*
895 1107 * Load things that should only be in Network Admin.
896 1108 *
897 1109 * For now blow away everything else until a more full
@@ -906,8 +1118,9 @@
906 1118 $is_connection_ready = self::is_connection_ready();
907 1119
908 1120 if ( $is_connection_ready ) {
909 1121 add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) );
1122 + $this->run_initialize_tracking_action();
910 1123
911 1124 Jetpack_Heartbeat::init();
912 1125 if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) {
913 1126 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
@@ -912,8 +1125,19 @@
912 1125 if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) {
913 1126 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
914 1127 Jetpack_Search_Performance_Logger::init();
915 1128 }
1129 + } else {
1130 + add_action( 'jetpack_agreed_to_terms_of_service', array( $this, 'run_initialize_tracking_action' ) );
1131 + add_action( 'rest_api_init', array( $this, 'run_initialize_tracking_action' ) );
1132 + add_filter(
1133 + 'xmlrpc_methods',
1134 + function ( $methods ) {
1135 + $this->run_initialize_tracking_action();
1136 + return $methods;
1137 + },
1138 + 1
1139 + );
916 1140 }
917 1141
918 1142 // Initialize remote file upload request handlers.
919 1143 $this->add_remote_request_handlers();
@@ -923,20 +1147,10 @@
923 1147 */
924 1148 if ( $is_connection_ready ) {
925 1149 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
926 1150 add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );
927 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php';
928 - add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 );
1151 + add_action( 'rest_api_init', array( $this, 'maybe_initialize_rest_jsonapi' ) );
929 1152 }
930 -
931 - if ( ( new Tracking( 'jetpack', $this->connection_manager ) )->should_enable_tracking( new Terms_Of_Service(), new Status() ) ) {
932 - add_action( 'init', array( new Plugin_Tracking(), 'init' ) );
933 - } else {
934 - /**
935 - * Initialize tracking right after the user agrees to the terms of service.
936 - */
937 - add_action( 'jetpack_agreed_to_terms_of_service', array( new Plugin_Tracking(), 'init' ) );
938 - }
939 1153 }
940 1154
941 1155 /**
942 1156 * Runs on plugins_loaded. Use this to add code that needs to be executed later than other
@@ -944,17 +1158,58 @@
944 1158 *
945 1159 * @action plugins_loaded
946 1160 */
947 1161 public function late_initialization() {
948 - add_action( 'plugins_loaded', array( 'Jetpack', 'load_modules' ), 100 );
1162 + add_action( 'after_setup_theme', array( 'Jetpack', 'load_modules' ), -2 );
949 1163
950 - Partner::init();
951 - My_Jetpack_Initializer::init();
1164 + /*
1165 + * My Jetpack is a wp-admin dashboard. Its Initializer::init() only wires
1166 + * up admin-menu, admin_init, and rest_api_init surfaces — and eagerly
1167 + * loads every product class (backup, boost, protect, …) just to register
1168 + * admin plugin-action links — so none of it is needed on a plain
1169 + * front-end GET page view. (The pieces that do immediate work, e.g.
1170 + * Connection REST authentication and Licensing, are already initialized
1171 + * unconditionally in Jetpack's constructor, so they are unaffected here.)
1172 + *
1173 + * Gate the call to the request types where My Jetpack actually does work.
1174 + * REST can't be detected yet at plugins_loaded, so initialize on
1175 + * rest_api_init for that branch; a plain page view never fires it, so My
1176 + * Jetpack stays unloaded there.
1177 + */
1178 + if ( self::should_eager_load_packages() ) {
1179 + My_Jetpack_Initializer::init();
1180 + } else {
1181 + add_action( 'rest_api_init', array( My_Jetpack_Initializer::class, 'init' ), 0 );
1182 + }
952 1183
953 - // Initialize Boost Speed Score
954 - $modules = Jetpack_Boost_Modules::init();
955 - new Speed_Score( $modules, 'jetpack-dashboard' );
1184 + Scan_Page_Init::initialize();
1185 + Jetpack_SEO_Initializer::init();
956 1186
1187 + if ( ( new Modules() )->is_active( 'podcast' ) ) {
1188 + Podcast::init();
1189 + }
1190 +
1191 + /*
1192 + * Initialize Boost Speed Score. It only does work on REST requests (the
1193 + * dashboard speed-score endpoints) and on a few Jetpack Boost lifecycle
1194 + * actions, so defer constructing it — and loading the boost-speed-score
1195 + * package classes — until one of those hooks actually fires instead of on
1196 + * every request. Priority 0 ensures the object's own callbacks (added in
1197 + * its constructor at the default priority) still run for the firing hook.
1198 + */
1199 + $initialize_speed_score = static function () {
1200 + static $initialized = false;
1201 + if ( $initialized ) {
1202 + return;
1203 + }
1204 + $initialized = true;
1205 + new Speed_Score( array(), 'jetpack-dashboard' );
1206 + };
1207 + add_action( 'rest_api_init', $initialize_speed_score, 0 );
1208 + add_action( 'jetpack_boost_deactivate', $initialize_speed_score, 0 );
1209 + add_action( 'jetpack_boost_environment_changed', $initialize_speed_score, 0 );
1210 + add_action( 'handle_environment_change', $initialize_speed_score, 0 );
1211 +
957 1212 /**
958 1213 * Fires when Jetpack is fully loaded and ready. This is the point where it's safe
959 1214 * to instantiate classes from packages and namespaces that are managed by the Jetpack Autoloader.
960 1215 *
@@ -992,8 +1247,10 @@
992 1247
993 1248 /**
994 1249 * Redirect edit post links to Calypso.
995 1250 *
1251 + * @deprecated since 13.9
1252 + *
996 1253 * @param string $default_url Post edit URL.
997 1254 * @param int $post_id Post ID.
998 1255 *
999 1256 * @return string
@@ -998,8 +1255,10 @@
998 1255 *
999 1256 * @return string
1000 1257 */
1001 1258 public function point_edit_post_links_to_calypso( $default_url, $post_id ) {
1259 + _deprecated_function( __METHOD__, '13.9' );
1260 +
1002 1261 $post = get_post( $post_id );
1003 1262
1004 1263 if ( empty( $post ) ) {
1005 1264 return $default_url;
@@ -1030,13 +1289,17 @@
1030 1289
1031 1290 /**
1032 1291 * Redirect edit comment links to Calypso.
1033 1292 *
1293 + * @deprecated since 13.9
1294 + *
1034 1295 * @param string $url Comment edit URL.
1035 1296 *
1036 1297 * @return string
1037 1298 */
1038 1299 public function point_edit_comment_links_to_calypso( $url ) {
1300 + _deprecated_function( __METHOD__, '13.9' );
1301 +
1039 1302 // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID.
1040 1303 $query_args = null;
1041 1304 wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args );
1042 1305
@@ -1055,30 +1318,16 @@
1055 1318 *
1056 1319 * @return array list of callables.
1057 1320 */
1058 1321 public function filter_sync_callable_whitelist( $callables ) {
1059 -
1060 1322 // Jetpack Functions.
1061 1323 $jetpack_callables = array(
1062 1324 'single_user_site' => array( 'Jetpack', 'is_single_user_site' ),
1063 1325 'updates' => array( 'Jetpack', 'get_updates' ),
1064 1326 'available_jetpack_blocks' => array( 'Jetpack_Gutenberg', 'get_availability' ), // Includes both Gutenberg blocks *and* plugins.
1327 + 'theme_styles' => array( 'Automattic\\Jetpack\\Plugin\\Theme_Styles_Sync', 'get_theme_styles' ),
1065 1328 );
1066 - $callables = array_merge( $callables, $jetpack_callables );
1067 -
1068 - // Jetpack_SSO_Helpers.
1069 - if ( include_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-helpers.php' ) {
1070 - $sso_helpers = array(
1071 - 'sso_is_two_step_required' => array( 'Jetpack_SSO_Helpers', 'is_two_step_required' ),
1072 - 'sso_should_hide_login_form' => array( 'Jetpack_SSO_Helpers', 'should_hide_login_form' ),
1073 - 'sso_match_by_email' => array( 'Jetpack_SSO_Helpers', 'match_by_email' ),
1074 - 'sso_new_user_override' => array( 'Jetpack_SSO_Helpers', 'new_user_override' ),
1075 - 'sso_bypass_default_login_form' => array( 'Jetpack_SSO_Helpers', 'bypass_login_forward_wpcom' ),
1076 - );
1077 - $callables = array_merge( $callables, $sso_helpers );
1078 - }
1079 -
1080 - return $callables;
1329 + return array_merge( $callables, $jetpack_callables );
1081 1330 }
1082 1331
1083 1332 /**
1084 1333 * Extend Sync multisite callables with Jetpack Plugin functions.
@@ -1103,41 +1352,8 @@
1103 1352 return $callables;
1104 1353 }
1105 1354
1106 1355 /**
1107 - * Deprecated
1108 - * Please use Automattic\Jetpack\JITMS\JITM::jetpack_track_last_sync_callback instead.
1109 - *
1110 - * @param array $params The action parameters.
1111 - *
1112 - * @deprecated since 9.8.
1113 - */
1114 - public function jetpack_track_last_sync_callback( $params ) {
1115 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\Automattic\Jetpack\JITMS\JITM->jetpack_track_last_sync_callback' );
1116 - return Automattic\Jetpack\JITMS\JITM::get_instance()->jetpack_track_last_sync_callback( $params );
1117 - }
1118 -
1119 - /**
1120 - * Jetpack Connection banner callback function.
1121 - *
1122 - * @return void
1123 - */
1124 - public function jetpack_connection_banner_callback() {
1125 - check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' );
1126 -
1127 - // Disable the banner dismiss functionality if the pre-connection prompt helpers filter is set.
1128 - if (
1129 - isset( $_REQUEST['dismissBanner'] ) &&
1130 - ! Jetpack_Connection_Banner::force_display()
1131 - ) {
1132 - Jetpack_Options::update_option( 'dismissed_connection_banner', 1 );
1133 - wp_send_json_success();
1134 - }
1135 -
1136 - wp_die();
1137 - }
1138 -
1139 - /**
1140 1356 * If there are any stats that need to be pushed, but haven't been, push them now.
1141 1357 */
1142 1358 public function push_stats() {
1143 1359 if ( ! empty( $this->stats ) ) {
@@ -1152,16 +1368,8 @@
1152 1368 * @param string $cap Capability name.
1153 1369 */
1154 1370 public function jetpack_custom_caps( $caps, $cap ) {
1155 1371 switch ( $cap ) {
1156 - case 'jetpack_manage_modules':
1157 - case 'jetpack_activate_modules':
1158 - case 'jetpack_deactivate_modules':
1159 - $caps = array( 'manage_options' );
1160 - break;
1161 - case 'jetpack_configure_modules':
1162 - $caps = array( 'manage_options' );
1163 - break;
1164 1372 case 'jetpack_manage_autoupdates':
1165 1373 $caps = array(
1166 1374 'manage_options',
1167 1375 'update_plugins',
@@ -1179,9 +1387,9 @@
1179 1387 if ( $is_offline_mode ) {
1180 1388 $caps = array( 'manage_options' );
1181 1389 break;
1182 1390 } else {
1183 - $caps = array( 'read' );
1391 + $caps = array( 'edit_posts' );
1184 1392 }
1185 1393 break;
1186 1394 }
1187 1395 return $caps;
@@ -1340,9 +1548,9 @@
1340 1548 }
1341 1549 /**
1342 1550 * Does the network allow admins to add new users.
1343 1551 *
1344 - * @return boolian
1552 + * @return bool
1345 1553 */
1346 1554 public static function network_add_new_users() {
1347 1555 return (bool) get_site_option( 'add_new_users' );
1348 1556 }
@@ -1349,9 +1557,9 @@
1349 1557 /**
1350 1558 * File upload psace left per site in MB.
1351 1559 * -1 means NO LIMIT.
1352 1560 *
1353 - * @return number
1561 + * @return int
1354 1562 */
1355 1563 public static function network_site_upload_space() {
1356 1564 // value in MB.
1357 1565 return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() );
@@ -1368,9 +1576,9 @@
1368 1576
1369 1577 /**
1370 1578 * Maximum file upload size set by the network.
1371 1579 *
1372 - * @return number
1580 + * @return int
1373 1581 */
1374 1582 public static function network_max_upload_file_size() {
1375 1583 // value in KB.
1376 1584 return get_site_option( 'fileupload_maxk', 300 );
@@ -1529,9 +1737,9 @@
1529 1737 }
1530 1738 return 0;
1531 1739 }
1532 1740
1533 -// phpcs:disable WordPress.WP.CapitalPDangit.Misspelled
1741 + // phpcs:disable WordPress.WP.CapitalPDangit.MisspelledInComment
1534 1742 /**
1535 1743 * Gets updates and stores in jetpack_updates.
1536 1744 *
1537 1745 * The jetpack_updates option is saved in the following schema:
@@ -1547,8 +1755,9 @@
1547 1755 *
1548 1756 * @return array
1549 1757 */
1550 1758 public static function get_updates() {
1759 + $updates = array();
1551 1760 $update_data = wp_get_update_data();
1552 1761
1553 1762 // Stores the individual update counts as well as the total count.
1554 1763 if ( isset( $update_data['counts'] ) ) {
@@ -1561,11 +1770,11 @@
1561 1770 if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
1562 1771 $updates['wp_update_version'] = $cur->current;
1563 1772 }
1564 1773 }
1565 - return isset( $updates ) ? $updates : array();
1774 + return $updates;
1566 1775 }
1567 - // phpcs:enable
1776 + // phpcs:enable WordPress.WP.CapitalPDangit.MisspelledInComment
1568 1777
1569 1778 /**
1570 1779 * Get update details for core, plugins, and themes.
1571 1780 *
@@ -1623,44 +1832,8 @@
1623 1832 return apply_filters( 'jetpack_is_connection_ready', self::connection()->is_connected(), self::connection() );
1624 1833 }
1625 1834
1626 1835 /**
1627 - * Deprecated: Is Jetpack in development (offline) mode?
1628 - *
1629 - * This static method is being left here intentionally without the use of _deprecated_function(), as other plugins
1630 - * and themes still use it, and we do not want to flood them with notices.
1631 - *
1632 - * Please use Automattic\Jetpack\Status()->is_offline_mode() instead.
1633 - *
1634 - * @deprecated since 8.0.
1635 - */
1636 - public static function is_development_mode() {
1637 - _deprecated_function( __METHOD__, 'jetpack-8.0', '\Automattic\Jetpack\Status->is_offline_mode' );
1638 - return ( new Status() )->is_offline_mode();
1639 - }
1640 -
1641 - /**
1642 - * Whether the site is currently onboarding or not.
1643 - * A site is considered as being onboarded if it currently has an onboarding token.
1644 - *
1645 - * @since 5.8
1646 - * @deprecated Use \Automattic\Jetpack\Status()->is_onboarding()
1647 - *
1648 - * @access public
1649 - * @static
1650 - *
1651 - * @return bool True if the site is currently onboarding, false otherwise
1652 - */
1653 - public static function is_onboarding() {
1654 - _deprecated_function( __METHOD__, 'jetpack-10.9', 'Automattic\\Jetpack\\Status\\is_onboarding' );
1655 -
1656 - if ( ! method_exists( 'Automattic\Jetpack\Status', 'is_onboarding' ) ) {
1657 - return Jetpack_Options::get_option( 'onboarding' ) !== false;
1658 - }
1659 - return ( new Status() )->is_onboarding();
1660 - }
1661 -
1662 - /**
1663 1836 * Determines reason for Jetpack offline mode.
1664 1837 */
1665 1838 public static function development_mode_trigger_text() {
1666 1839 $status = new Status();
@@ -1674,11 +1847,10 @@
1674 1847 } elseif ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
1675 1848 $notice = __( 'The WP_LOCAL_DEV constant is defined in wp-config.php or elsewhere.', 'jetpack' );
1676 1849 } elseif ( $status->is_local_site() ) {
1677 1850 $notice = __( 'The site URL is a known local development environment URL (e.g. http://localhost).', 'jetpack' );
1678 - /** This filter is documented in packages/status/src/class-status.php */
1679 - } elseif ( has_filter( 'jetpack_development_mode' ) && apply_filters( 'jetpack_development_mode', false ) ) { // This is a deprecated filter name.
1680 - $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' );
1851 + } elseif ( get_option( 'jetpack_offline_mode' ) ) {
1852 + $notice = __( 'The jetpack_offline_mode option is set to true.', 'jetpack' );
1681 1853 } else {
1682 1854 $notice = __( 'The jetpack_offline_mode filter is set to true.', 'jetpack' );
1683 1855 }
1684 1856
@@ -1693,9 +1865,9 @@
1693 1865 if ( ( new Status() )->is_offline_mode() ) {
1694 1866 $notice = sprintf(
1695 1867 /* translators: %s is a URL */
1696 1868 __( 'In <a href="%s" target="_blank">Offline Mode</a>:', 'jetpack' ),
1697 - Redirect::get_url( 'jetpack-support-development-mode' )
1869 + esc_url( Redirect::get_url( 'jetpack-support-development-mode' ) )
1698 1870 );
1699 1871
1700 1872 $notice .= ' ' . self::development_mode_trigger_text();
1701 1873
@@ -1704,19 +1876,12 @@
1704 1876
1705 1877 // Throw up a notice if using a development version and as for feedback.
1706 1878 if ( self::is_development_version() ) {
1707 1879 /* translators: %s is a URL */
1708 - $notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), Redirect::get_url( 'jetpack-contact-support-beta-group' ) );
1880 + $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' ) ) );
1709 1881
1710 1882 echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All provided text.
1711 1883 }
1712 - // Throw up a notice if using staging mode.
1713 - if ( ( new Status() )->is_staging_site() ) {
1714 - /* translators: %s is a URL */
1715 - $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), Redirect::get_url( 'jetpack-support-staging-sites' ) );
1716 -
1717 - echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All provided text.
1718 - }
1719 1884 }
1720 1885
1721 1886 /**
1722 1887 * Whether Jetpack's version maps to a public release, or a development version.
@@ -1737,28 +1902,8 @@
1737 1902 );
1738 1903 }
1739 1904
1740 1905 /**
1741 - * Is a given user (or the current user if none is specified) linked to a WordPress.com user?
1742 - *
1743 - * @param int $user_id User ID or will use get_current_user_id if false/not provided.
1744 - */
1745 - public static function is_user_connected( $user_id = false ) {
1746 - _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Manager\\is_user_connected' );
1747 - return self::connection()->is_user_connected( $user_id );
1748 - }
1749 -
1750 - /**
1751 - * Get the wpcom user data of the current|specified connected user.
1752 - *
1753 - * @param null|int $user_id User ID or will use get_current_user_id if null.
1754 - */
1755 - public static function get_connected_user_data( $user_id = null ) {
1756 - _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Manager\\get_connected_user_data' );
1757 - return self::connection()->get_connected_user_data( $user_id );
1758 - }
1759 -
1760 - /**
1761 1906 * Get the wpcom email of the current|specified connected user.
1762 1907 *
1763 1908 * @param null|int $user_id User ID or will use get_current_user_id if null.
1764 1909 */
@@ -1810,18 +1955,11 @@
1810 1955 */
1811 1956 public static function load_modules() {
1812 1957 $status = new Status();
1813 1958
1814 - if ( method_exists( $status, 'is_onboarding' ) ) {
1815 - $is_onboarding = $status->is_onboarding();
1816 - } else {
1817 - $is_onboarding = self::is_onboarding();
1818 - }
1819 -
1820 1959 if (
1821 1960 ! self::is_connection_ready()
1822 1961 && ! $status->is_offline_mode()
1823 - && ! $is_onboarding
1824 1962 && (
1825 1963 ! is_multisite()
1826 1964 || ! get_site_option( 'jetpack_protect_active' )
1827 1965 )
@@ -1942,22 +2080,14 @@
1942 2080 *
1943 2081 * @todo Store the result in core's object cache maybe?
1944 2082 */
1945 2083 public static function get_active_plugins() {
1946 - $active_plugins = (array) get_option( 'active_plugins', array() );
1947 -
1948 - if ( is_multisite() ) {
1949 - // Due to legacy code, active_sitewide_plugins stores them in the keys,
1950 - // whereas active_plugins stores them in the values.
1951 - $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
1952 - if ( $network_plugins ) {
1953 - $active_plugins = array_merge( $active_plugins, $network_plugins );
1954 - }
2084 + // Older Connection copies can load first and lack this method.
2085 + if ( ! method_exists( Heartbeat::class, 'get_active_plugins' ) ) {
2086 + return array();
1955 2087 }
1956 2088
1957 - sort( $active_plugins );
1958 -
1959 - return array_unique( $active_plugins );
2089 + return Heartbeat::get_active_plugins();
1960 2090 }
1961 2091
1962 2092 /**
1963 2093 * Gets and parses additional plugin data to send with the heartbeat data
@@ -2095,8 +2225,10 @@
2095 2225 *
2096 2226 * @param bool true Should Twitter Card Meta tags be disabled. Default to true.
2097 2227 */
2098 2228 if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) {
2229 + // @todo Remove this require once the deprecated Jetpack_Twitter_Cards wrapper has been removed.
2230 + // Twitter Cards functionality now lives in the jetpack-post-media package (Automattic\Jetpack\Post_Media\Twitter_Cards).
2099 2231 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
2100 2232 }
2101 2233 }
2102 2234
@@ -2199,9 +2331,9 @@
2199 2331 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.
2200 2332 $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we're not changing the site.
2201 2333 }
2202 2334 wp_safe_redirect( self::admin_url( 'page=' . rawurlencode( $page ) ) );
2203 - exit;
2335 + exit( 0 );
2204 2336 }
2205 2337 }
2206 2338
2207 2339 /**
@@ -2248,8 +2380,12 @@
2248 2380 default:
2249 2381 break;
2250 2382 }
2251 2383 }
2384 + if ( method_exists( Feature_Policy::class, 'ensure_hooks' ) ) {
2385 + Feature_Policy::ensure_hooks();
2386 + }
2387 +
2252 2388 /**
2253 2389 * Filters the array of default modules.
2254 2390 *
2255 2391 * @since 2.5.0
@@ -2281,12 +2417,14 @@
2281 2417 * @return array
2282 2418 */
2283 2419 public function handle_deprecated_modules( $modules ) {
2284 2420 $deprecated_modules = array(
2285 - 'debug' => null, // Closed out and moved to the debugger library.
2286 - 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2287 - 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support.
2288 - 'minileven' => null, // Closed out in 8.3 -- Responsive themes are common now, and so is AMP.
2421 + 'debug' => null, // Closed out and moved to the debugger library.
2422 + 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2423 + 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support.
2424 + 'minileven' => null, // Closed out in 8.3 -- Responsive themes are common now, and so is AMP.
2425 + 'lazy-images' => null, // Closed out in 12.8 -- WordPress core now has native lazy loading.
2426 + 'enhanced-distribution' => null, // Closed out in 13.3 -- WP.com is winding down the firehose.
2289 2427 );
2290 2428
2291 2429 // Don't activate SSO if they never completed activating WPCC.
2292 2430 if ( self::is_module_active( 'wpcc' ) ) {
@@ -2340,8 +2478,17 @@
2340 2478 }
2341 2479 }
2342 2480 }
2343 2481
2482 + // Special case to convert block setting to a block module.
2483 + $block_key = array_search( 'blocks', $modules, true );
2484 + if ( $block_key !== false ) { // Only care if 'blocks' made it through the previous filters.
2485 + $block_option = get_option( 'jetpack_blocks_disabled', null );
2486 + if ( $block_option ) {
2487 + unset( $modules[ $block_key ] );
2488 + }
2489 + }
2490 +
2344 2491 return $modules;
2345 2492 }
2346 2493
2347 2494 /**
@@ -2398,23 +2545,30 @@
2398 2545
2399 2546 /**
2400 2547 * Return module name translation. Uses matching string created in modules/module-headings.php.
2401 2548 *
2549 + * The module list is globbed from `modules/` at runtime, so a module can be listed with no
2550 + * entry in that generated file. Fall back to the untranslated header rather than overwriting
2551 + * it with the null `jetpack_get_module_i18n()` returns for an unknown slug.
2552 + *
2402 2553 * @since 3.9.2
2403 2554 *
2404 2555 * @param array $modules Array of Jetpack modules.
2405 2556 *
2406 - * @return string|void
2557 + * @return array
2407 2558 */
2408 2559 public static function get_translated_modules( $modules ) {
2409 2560 foreach ( $modules as $index => $module ) {
2410 2561 $i18n_module = jetpack_get_module_i18n( $module['module'] );
2411 - if ( isset( $module['name'] ) ) {
2412 - $modules[ $index ]['name'] = $i18n_module['name'];
2562 + $name = $i18n_module['name'] ?? null;
2563 + $description = $i18n_module['description'] ?? null;
2564 +
2565 + if ( null !== $name && isset( $module['name'] ) ) {
2566 + $modules[ $index ]['name'] = $name;
2413 2567 }
2414 - if ( isset( $module['description'] ) ) {
2415 - $modules[ $index ]['description'] = $i18n_module['description'];
2416 - $modules[ $index ]['short_description'] = $i18n_module['description'];
2568 + if ( null !== $description && isset( $module['description'] ) ) {
2569 + $modules[ $index ]['description'] = $description;
2570 + $modules[ $index ]['short_description'] = $description;
2417 2571 }
2418 2572 if ( isset( $module['module_tags'] ) ) {
2419 2573 $modules[ $index ]['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] );
2420 2574 }
@@ -2454,20 +2608,28 @@
2454 2608
2455 2609 /**
2456 2610 * Catches PHP errors. Must be used in conjunction with output buffering.
2457 2611 *
2612 + * @deprecated since 13.5
2458 2613 * @param bool $catch True to start catching, False to stop.
2459 2614 *
2460 2615 * @static
2616 + * @deprecated 13.5
2617 + * @see \Automattic\Jetpack\Errors
2461 2618 */
2462 2619 public static function catch_errors( $catch ) {
2620 + _deprecated_function( __METHOD__, '13.5' );
2621 + // @phan-suppress-next-line PhanDeprecatedClass
2463 2622 return ( new Errors() )->catch_errors( $catch );
2464 2623 }
2465 2624
2466 2625 /**
2467 2626 * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
2627 + *
2628 + * @deprecated since 13.5
2468 2629 */
2469 2630 public static function catch_errors_on_shutdown() {
2631 + _deprecated_function( __METHOD__, '13.5' );
2470 2632 self::state( 'php_errors', self::alias_directories( ob_get_clean() ) );
2471 2633 }
2472 2634
2473 2635 /**
@@ -2571,9 +2733,9 @@
2571 2733 ),
2572 2734 add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), self::admin_url( 'page=jetpack' ) )
2573 2735 );
2574 2736 wp_safe_redirect( $url );
2575 - exit;
2737 + exit( 0 );
2576 2738 }
2577 2739 }
2578 2740
2579 2741 /**
@@ -2591,9 +2753,8 @@
2591 2753
2592 2754 // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating.
2593 2755 if ( $send_state_messages ) {
2594 2756 self::restate();
2595 - self::catch_errors( true );
2596 2757 }
2597 2758
2598 2759 $active = self::get_active_modules();
2599 2760
@@ -2661,10 +2822,8 @@
2661 2822 if ( $send_state_messages ) {
2662 2823 self::state( 'error', false );
2663 2824 self::state( 'module', false );
2664 2825 }
2665 -
2666 - self::catch_errors( false );
2667 2826 /**
2668 2827 * Fires when default modules are activated.
2669 2828 *
2670 2829 * @since 1.9.0
@@ -2722,9 +2881,9 @@
2722 2881 * @return string $url module configuration URL.
2723 2882 */
2724 2883 public static function module_configuration_url( $module ) {
2725 2884 $module = self::get_module_slug( $module );
2726 - $default_url = self::admin_url() . "#/settings?term=$module";
2885 + $default_url = self::admin_url( array( 'page' => 'jetpack-settings' ) ) . "#/settings?term=$module";
2727 2886 /**
2728 2887 * Allows to modify configure_url of specific module to be able to redirect to some custom location.
2729 2888 *
2730 2889 * @since 6.9.0
@@ -2742,9 +2901,9 @@
2742 2901 *
2743 2902 * @param string $message Error message.
2744 2903 * @param bool $deactivate Deactivate Jetpack or not.
2745 2904 *
2746 - * @return void
2905 + * @return never
2747 2906 */
2748 2907 public static function bail_on_activation( $message, $deactivate = true ) {
2749 2908 ?>
2750 2909 <!doctype html>
@@ -2782,9 +2941,9 @@
2782 2941 if ( $update ) {
2783 2942 update_option( 'active_plugins', array_filter( $plugins ) );
2784 2943 }
2785 2944 }
2786 - exit;
2945 + exit( 0 );
2787 2946 }
2788 2947
2789 2948 /**
2790 2949 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
@@ -2809,12 +2968,18 @@
2809 2968 update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) );
2810 2969
2811 2970 Health::on_jetpack_activated();
2812 2971
2972 + \Automattic\Jetpack\Reprint_Export\Reprint_Exporter::discard_credentials();
2973 +
2813 2974 if ( self::is_connection_ready() && method_exists( 'Automattic\Jetpack\Sync\Actions', 'do_only_first_initial_sync' ) ) {
2814 2975 Sync_Actions::do_only_first_initial_sync();
2815 2976 }
2816 2977
2978 + if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
2979 + Woocommerce_Analytics::maybe_add_proxy_speed_module();
2980 + }
2981 +
2817 2982 self::plugin_initialize();
2818 2983 }
2819 2984
2820 2985 /**
@@ -2849,9 +3014,9 @@
2849 3014
2850 3015 if ( $plugins_path === $referer['path'] ) {
2851 3016 $source_type = 'list';
2852 3017 } elseif ( $plugins_install_path === $referer['path'] ) {
2853 - $tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured';
3018 + $tab = $query_parts['tab'] ?? 'featured';
2854 3019 switch ( $tab ) {
2855 3020 case 'popular':
2856 3021 $source_type = 'popular';
2857 3022 break;
@@ -2861,10 +3026,10 @@
2861 3026 case 'favorites':
2862 3027 $source_type = 'favorites';
2863 3028 break;
2864 3029 case 'search':
2865 - $source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' );
2866 - $source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null;
3030 + $source_type = 'search-' . ( $query_parts['type'] ?? 'term' );
3031 + $source_query = $query_parts['s'] ?? null;
2867 3032 break;
2868 3033 default:
2869 3034 $source_type = 'featured';
2870 3035 }
@@ -2873,29 +3038,171 @@
2873 3038 return array( $source_type, $source_query );
2874 3039 }
2875 3040
2876 3041 /**
2877 - * Runs before bumping version numbers up to a new version
3042 + * Runs before bumping version numbers up to a new version.
2878 3043 *
2879 - * @param string $version Version:timestamp.
3044 + * Only ever registered the hooks for the release post update modal, which has been removed.
3045 + * No longer hooked to `updating_jetpack_version`.
3046 + *
3047 + * @deprecated 16.2
3048 + *
3049 + * @param string $version Version:timestamp.
2880 3050 * @param string $old_version Old Version:timestamp or false if not set yet.
2881 3051 */
2882 - public static function do_version_bump( $version, $old_version ) {
2883 - if ( $old_version ) { // For existing Jetpack installations.
2884 - add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
3052 + public static function do_version_bump( $version, $old_version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Signature preserved for the deprecation shim.
3053 + _deprecated_function( __METHOD__, 'jetpack-16.2' );
3054 + }
2885 3055
2886 - // If a front end page is visited after the update, the 'wp' action will fire.
2887 - add_action( 'wp', 'Jetpack::set_update_modal_display' );
3056 + /**
3057 + * Enables the Newsletter (subscriptions) module for existing sites now that it is a default-on module.
3058 + *
3059 + * Fresh installs receive the module via its "Auto Activate: Yes" header, so this only handles sites
3060 + * upgrading from a version where the module defaulted off. It runs once per site (guarded by the
3061 + * subscriptions_default_on_migrated option). Fresh installs are marked as migrated immediately so the
3062 + * migration never runs for them. After it has run, the user's choice to deactivate the module again
3063 + * (for example from the My Jetpack Products page) is respected and never reverted.
3064 + *
3065 + * The module requires a connection, so on a disconnected site the migration is deferred without setting
3066 + * the guard, allowing a later version bump to retry once the site is connected.
3067 + *
3068 + * @param string $version New Jetpack version:timestamp.
3069 + * @param string|false $old_version Previous Jetpack version:timestamp, or false on a fresh install.
3070 + */
3071 + public static function activate_subscriptions_module_for_existing_sites( $version, $old_version ) {
3072 + if ( get_option( 'jetpack_subscriptions_default_on_migrated' ) ) {
3073 + return;
3074 + }
2888 3075
2889 - // If an admin page is visited after the update, the 'current_screen' action will fire.
2890 - add_action( 'current_screen', 'Jetpack::set_update_modal_display' );
3076 + // Fresh installs get the module via its "Auto Activate: Yes" header. Mark them as migrated so a
3077 + // later opt-out is never reverted by the existing-site path on a subsequent version bump.
3078 + if ( ! $old_version ) {
3079 + update_option( 'jetpack_subscriptions_default_on_migrated', true );
3080 + return;
2891 3081 }
3082 +
3083 + if ( ! self::is_connection_ready() ) {
3084 + return;
3085 + }
3086 +
3087 + // Mark as migrated only once the module is active, so a transient activation failure is retried on
3088 + // a later version bump rather than being silently skipped.
3089 + if ( self::is_module_active( 'subscriptions' ) || self::activate_module( 'subscriptions', false, false ) ) {
3090 + update_option( 'jetpack_subscriptions_default_on_migrated', true );
3091 + }
2892 3092 }
2893 3093
2894 3094 /**
3095 + * Option flag that records the AI master-switch opt-out reconciliation has run,
3096 + * so it never runs twice.
3097 + *
3098 + * @var string
3099 + */
3100 + const AI_MASTER_OPTOUT_MIGRATED_OPTION = 'jetpack_ai_master_optout_migrated';
3101 +
3102 + /**
3103 + * Register the on-upgrade init hooks whose relative ORDER matters, extracted so
3104 + * the ordering can be asserted in tests without invoking plugin_upgrade() (whose
3105 + * guards make it unreliable to trigger under test). activate_new_modules()
3106 + * (init, default priority 10) auto-activates "Auto Activate: Yes" modules
3107 + * including the `ai` master; reconcile_ai_master_optout() must run at a LATER
3108 + * priority to honor an explicit AI opt-out.
3109 + *
3110 + * @return void
3111 + */
3112 + public static function register_upgrade_init_hooks() {
3113 + add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
3114 + add_action( 'init', array( __CLASS__, 'reconcile_ai_master_optout' ), 20 );
3115 + }
3116 +
3117 + /**
3118 + * Preserves an explicit Jetpack AI opt-out when the `ai` module becomes the site-wide
3119 + * master switch off WordPress.com Simple (self-hosted and Atomic).
3120 + *
3121 + * The `ai` module is "Auto Activate: Yes", so on upgrade {@see self::activate_new_modules()}
3122 + * turns it on for connected sites — the desired default-on / auto-enable-on-connection
3123 + * behavior, which this method deliberately leaves alone. The one case it corrects is a site
3124 + * that had explicitly disabled Jetpack AI (the `jetpack_ai_enabled` option present and falsey)
3125 + * before the module shipped: that opt-out must survive the module becoming the master, so the
3126 + * module is deactivated for exactly those sites. An absent or truthy option is left untouched.
3127 + *
3128 + * Ordering is the whole point. `activate_new_modules()` is hooked on `init` at priority 10 and
3129 + * auto-activates the module there; this method is hooked on `init` at priority 20 (see
3130 + * {@see self::plugin_upgrade()}), so it runs AFTER the auto-activation and its deactivation is
3131 + * the final state. A version-guarded block that ran inline during `plugins_loaded` would be
3132 + * undone by the later auto-activation, which is why this is a late-init hook rather than an
3133 + * inline upgrade step.
3134 + *
3135 + * WordPress.com Simple never runs modules — the option stays the master there — so this is a
3136 + * no-op on Simple. The {@see self::AI_MASTER_OPTOUT_MIGRATED_OPTION} flag makes it run exactly
3137 + * once, which matters because off-Simple the option is no longer the master after this runs:
3138 + * a stale falsey option must not keep re-deactivating a module the user later turns back on.
3139 + *
3140 + * @return void
3141 + */
3142 + public static function reconcile_ai_master_optout() {
3143 + if ( get_option( self::AI_MASTER_OPTOUT_MIGRATED_OPTION ) ) {
3144 + return;
3145 + }
3146 +
3147 + // Simple keeps the `jetpack_ai_enabled` option as the master; modules don't run there.
3148 + if ( ( new Host() )->is_wpcom_simple() ) {
3149 + return;
3150 + }
3151 +
3152 + // A sentinel default distinguishes an absent option (leave auto-activation alone) from one
3153 + // explicitly stored falsey (an opt-out to preserve).
3154 + $stored = get_option( 'jetpack_ai_enabled', 'not-set' );
3155 + if ( 'not-set' !== $stored && ! (bool) $stored ) {
3156 + ( new Modules() )->deactivate( 'ai' );
3157 + }
3158 +
3159 + update_option( self::AI_MASTER_OPTOUT_MIGRATED_OPTION, true );
3160 + }
3161 +
3162 + /**
3163 + * Deletes obsolete SEO module-state options without changing module activation.
3164 + *
3165 + * @since 16.3
3166 + */
3167 + public static function cleanup_seo_module_state_options() {
3168 + delete_option( 'jetpack_seo_sitemap_enabled' );
3169 + delete_option( 'jetpack_seo_canonical_urls_enabled' );
3170 + delete_option( 'jetpack_seo_module_state_reconciled' );
3171 + }
3172 +
3173 + /**
3174 + * Seeds the Jetpack SEO discoverability cohort once, so the new SEO surface is
3175 + * auto-discoverable on fresh installs but opt-in on existing ones (JETPACK-1700).
3176 + *
3177 + * Hooked on `updating_jetpack_version`, which fires on every install including the
3178 + * first — with `$old_version === false` on a brand-new site (the same signal
3179 + * {@see self::activate_subscriptions_module_for_existing_sites()} keys off). Fresh
3180 + * installs are seeded visible; existing installs are seeded hidden and opt in later
3181 + * via the legacy Traffic page or My Jetpack. `add_option()` makes this seed-once: it
3182 + * never overrides a value a later opt-in (or opt-out) has set. WordPress.com sites
3183 + * ignore this option entirely (always visible) — see
3184 + * {@see \Automattic\Jetpack\SEO\Initializer::is_seo_surface_visible()}.
3185 + *
3186 + * @param string $version The new Jetpack version (unused).
3187 + * @param string|false $old_version The previous version, or false on a fresh install.
3188 + */
3189 + public static function seed_seo_visibility_cohort( $version, $old_version ) {
3190 + add_option( Jetpack_SEO_Initializer::VISIBILITY_OPTION, ! $old_version );
3191 + }
3192 +
3193 + /**
2895 3194 * Sets the display_update_modal state.
3195 + *
3196 + * The release post update modal that read this state has been removed. The write is kept so the
3197 + * method still behaves as documented for the deprecation window. When this is deleted, also drop
3198 + * the matching `display_update_modal` guard in Automattic\Jetpack\CookieState::should_set_cookie(),
3199 + * which exists only to keep this key out of the cookie on the Jetpack admin screen.
3200 + *
3201 + * @deprecated 16.2
2896 3202 */
2897 3203 public static function set_update_modal_display() {
3204 + _deprecated_function( __METHOD__, 'jetpack-16.2' );
2898 3205 self::state( 'display_update_modal', true );
2899 3206 }
2900 3207
2901 3208 /**
@@ -2900,11 +3207,16 @@
2900 3207
2901 3208 /**
2902 3209 * Enqueues the block library styles.
2903 3210 *
3211 + * Only ever used by the release post update modal, which has been removed.
3212 + *
3213 + * @deprecated 16.2
3214 + *
2904 3215 * @param string $hook The current admin page.
2905 3216 */
2906 3217 public static function enqueue_block_style( $hook ) {
3218 + _deprecated_function( __METHOD__, 'jetpack-16.2' );
2907 3219 if ( 'toplevel_page_jetpack' === $hook ) {
2908 3220 wp_enqueue_style( 'wp-block-library' );
2909 3221 }
2910 3222 }
@@ -2933,9 +3245,8 @@
2933 3245
2934 3246 self::load_modules();
2935 3247
2936 3248 Jetpack_Options::delete_option( 'do_activate' );
2937 - Jetpack_Options::delete_option( 'dismissed_connection_banner' );
2938 3249 }
2939 3250
2940 3251 /**
2941 3252 * Handles the activation of the default modules depending on the current state of the site:
@@ -2994,8 +3305,12 @@
2994 3305 add_filter( 'jetpack_update_activated_state_on_disconnect', '__return_false' );
2995 3306 self::disconnect();
2996 3307 Jetpack_Options::delete_option( 'version' );
2997 3308 }
3309 +
3310 + if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
3311 + Woocommerce_Analytics::maybe_remove_proxy_speed_module();
3312 + }
2998 3313 }
2999 3314
3000 3315 /**
3001 3316 * Set activated option to 4 on jetpack_idc_disconnect action.
@@ -3023,9 +3338,9 @@
3023 3338 $connection->remove_connection( ! Identity_Crisis::validate_sync_error_idc_option() );
3024 3339 }
3025 3340
3026 3341 /**
3027 - * Happens after a successfull disconnection.
3342 + * Happens after a successful disconnection.
3028 3343 *
3029 3344 * @static
3030 3345 */
3031 3346 public static function jetpack_site_disconnected() {
@@ -3030,8 +3345,10 @@
3030 3345 */
3031 3346 public static function jetpack_site_disconnected() {
3032 3347 Identity_Crisis::clear_all_idc_options();
3033 3348
3349 + \Automattic\Jetpack\Reprint_Export\Reprint_Exporter::discard_credentials();
3350 +
3034 3351 // Delete all the sync related data. Since it could be taking up space.
3035 3352 Sender::get_instance()->uninstall();
3036 3353
3037 3354 /**
@@ -3048,10 +3365,13 @@
3048 3365 }
3049 3366 }
3050 3367
3051 3368 /**
3052 - * Disconnects the user
3369 + * Disconnects the user.
3053 3370 *
3371 + * @deprecated 13.4
3372 + * @see \Automattic\Jetpack\Connection\Manager::disconnect_user()
3373 + *
3054 3374 * @param int $user_id The user ID to disconnect.
3055 3375 */
3056 3376 public function disconnect_user( $user_id ) {
3057 3377 $this->connection_manager->disconnect_user( $user_id );
@@ -3057,21 +3377,8 @@
3057 3377 $this->connection_manager->disconnect_user( $user_id );
3058 3378 }
3059 3379
3060 3380 /**
3061 - * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load()
3062 - *
3063 - * @deprecated since Jetpack 9.7.0
3064 - * @see Automattic\Jetpack\Connection\Manager::try_registration()
3065 - *
3066 - * @return bool|WP_Error
3067 - */
3068 - public static function try_registration() {
3069 - _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Manager::try_registration' );
3070 - return static::connection()->try_registration();
3071 - }
3072 -
3073 - /**
3074 3381 * Checking the domain names in beta versions.
3075 3382 * If this is a development version, before attempting to connect, let's make sure that the domains are viable.
3076 3383 *
3077 3384 * @param null|\WP_Error $error The domain validation error, or `null` if everything's fine.
@@ -3078,9 +3385,9 @@
3078 3385 *
3079 3386 * @return null|\WP_Error The domain validation error, or `null` if everything's fine.
3080 3387 */
3081 3388 public static function registration_check_domains( $error ) {
3082 - if ( static::is_development_version() && defined( 'PHP_URL_HOST' ) ) {
3389 + if ( static::is_development_version() ) {
3083 3390 $domains_to_check = array_unique(
3084 3391 array(
3085 3392 'siteurl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
3086 3393 'homeurl' => wp_parse_url( get_home_url(), PHP_URL_HOST ),
@@ -3105,10 +3412,17 @@
3105 3412 * @param mixed $code Error code to log.
3106 3413 * @param mixed $data Data to log.
3107 3414 */
3108 3415 public static function log( $code, $data = null ) {
3416 +
3417 + $raw_log = Jetpack_Options::get_option( 'log', array() );
3418 + // This can be modified by the `jetpack_options` filter, so abort if we don't have an array.
3419 + if ( ! is_array( $raw_log ) ) {
3420 + return;
3421 + }
3422 +
3109 3423 // only grab the latest 200 entries.
3110 - $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 );
3424 + $log = array_slice( $raw_log, -199, 199 );
3111 3425
3112 3426 // Append our event to the log.
3113 3427 $log_entry = array(
3114 3428 'time' => time(),
@@ -3208,8 +3522,15 @@
3208 3522
3209 3523 /**
3210 3524 * Return stat data for WPCOM sync.
3211 3525 *
3526 + * The Sync stats module was this method's last caller and moved to the Connection package's
3527 + * `Heartbeat::generate_stats_array()`, which assembles the heartbeat data through the
3528 + * `jetpack_heartbeat_stats_array` filter. Note the package method does not include the extended
3529 + * data from `get_additional_stat_data()`, so callers relying on `$extended` need to add it themselves.
3530 + *
3531 + * @deprecated 16.2
3532 + *
3212 3533 * @param bool $encode JSON encode the result.
3213 3534 * @param bool $extended Adds additional stats data.
3214 3535 *
3215 3536 * @return array|string Stats data. Array if $encode is false. JSON-encoded string is $encode is true.
@@ -3214,10 +3535,15 @@
3214 3535 *
3215 3536 * @return array|string Stats data. Array if $encode is false. JSON-encoded string is $encode is true.
3216 3537 */
3217 3538 public static function get_stat_data( $encode = true, $extended = true ) {
3218 - $data = Jetpack_Heartbeat::generate_stats_array();
3539 + _deprecated_function( __METHOD__, 'jetpack-16.2', 'Automattic\\Jetpack\\Heartbeat::generate_stats_array' );
3219 3540
3541 + $env_stats = method_exists( Heartbeat::class, 'get_environment_stats' )
3542 + ? Heartbeat::get_environment_stats()
3543 + : array();
3544 + $data = array_merge( Jetpack_Heartbeat::generate_stats_array(), $env_stats );
3545 +
3220 3546 if ( $extended ) {
3221 3547 $additional_data = self::get_additional_stat_data();
3222 3548 $data = array_merge( $data, $additional_data );
3223 3549 }
@@ -3222,9 +3548,9 @@
3222 3548 $data = array_merge( $data, $additional_data );
3223 3549 }
3224 3550
3225 3551 if ( $encode ) {
3226 - return wp_json_encode( $data );
3552 + return wp_json_encode( $data, JSON_UNESCAPED_SLASHES );
3227 3553 }
3228 3554
3229 3555 return $data;
3230 3556 }
@@ -3296,23 +3622,24 @@
3296 3622 $fallback_no_verify_ssl_certs = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' );
3297 3623 /** Already documented in automattic/jetpack-connection::src/class-client.php */
3298 3624 $client_verify_ssl_certs = apply_filters( 'jetpack_client_verify_ssl_certs', false );
3299 3625
3300 - if ( ! $is_offline_mode ) {
3301 - Jetpack_Connection_Banner::init();
3302 - Jetpack_Connection_Widget::init();
3303 - }
3626 + // Run post-activation actions if needed.
3627 + $this->plugin_post_activation();
3304 3628
3305 3629 if ( ( self::is_connection_ready() || $is_offline_mode ) && false === $fallback_no_verify_ssl_certs && ! $client_verify_ssl_certs ) {
3306 3630 // Upgrade: 1.1 -> 1.1.1
3307 3631 // Check and see if host can verify the Jetpack servers' SSL certificate.
3308 3632 $args = array();
3633 + // @phan-suppress-next-line PhanAccessMethodInternal -- Phan is correct, but the usage is intentional.
3309 3634 Client::_wp_remote_request( self::connection()->api_url( 'test' ), $args, true );
3310 3635 }
3311 3636
3312 - Jetpack_Recommendations_Banner::init();
3313 -
3314 - if ( current_user_can( 'manage_options' ) && ! self::permit_ssl() ) {
3637 + if (
3638 + current_user_can( 'manage_options' )
3639 + && ! self::permit_ssl()
3640 + && ! $is_offline_mode
3641 + ) {
3315 3642 add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
3316 3643 }
3317 3644
3318 3645 add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
@@ -3321,12 +3648,8 @@
3321 3648 if ( ! ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) && ! is_network_admin() ) ) {
3322 3649 add_action( 'admin_enqueue_scripts', array( $this, 'deactivate_dialog' ) );
3323 3650 }
3324 3651
3325 - if ( isset( $_COOKIE['jetpackState']['display_update_modal'] ) ) {
3326 - add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
3327 - }
3328 -
3329 3652 add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) );
3330 3653
3331 3654 if ( self::is_connection_ready() || $is_offline_mode ) {
3332 3655 // Artificially throw errors in certain specific cases during plugin activation.
@@ -3331,13 +3654,8 @@
3331 3654 if ( self::is_connection_ready() || $is_offline_mode ) {
3332 3655 // Artificially throw errors in certain specific cases during plugin activation.
3333 3656 add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
3334 3657 }
3335 -
3336 - // Add custom column in wp-admin/users.php to show whether user is linked.
3337 - add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) );
3338 - add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 );
3339 - add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) );
3340 3658 }
3341 3659
3342 3660 /**
3343 3661 * Adds body classes.
@@ -3370,8 +3688,9 @@
3370 3688 * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
3371 3689 * This function artificially throws errors for such cases (per a specific list).
3372 3690 *
3373 3691 * @param string $plugin The activated plugin.
3692 + * @throws RuntimeException If a conflicting plugin is detected.
3374 3693 */
3375 3694 public function throw_error_on_activate_plugin( $plugin ) {
3376 3695 $active_modules = self::get_active_modules();
3377 3696
@@ -3384,8 +3703,9 @@
3384 3703 if ( 'stats.php' === basename( $plugin ) ) {
3385 3704 $throw = true;
3386 3705 }
3387 3706 } else {
3707 + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- Checked above. See also https://github.com/phan/phan/issues/1204.
3388 3708 $reflection = new ReflectionFunction( 'stats_get_api_key' );
3389 3709 if ( basename( $plugin ) === basename( $reflection->getFileName() ) ) {
3390 3710 $throw = true;
3391 3711 }
@@ -3392,9 +3712,9 @@
3392 3712 }
3393 3713
3394 3714 if ( $throw ) {
3395 3715 /* translators: Plugin name to deactivate. */
3396 - trigger_error( sprintf( esc_html__( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
3716 + throw new RuntimeException( sprintf( __( 'Jetpack contains the most recent version of the old "%1$s" plugin.', 'jetpack' ), 'WordPress.com Stats' ) );
3397 3717 }
3398 3718 }
3399 3719 }
3400 3720
@@ -3476,8 +3796,9 @@
3476 3796 /**
3477 3797 * Handler for Jetpack remote file uploads.
3478 3798 *
3479 3799 * @access public
3800 + * @return never
3480 3801 */
3481 3802 public function remote_request_handlers() {
3482 3803 switch ( current_filter() ) {
3483 3804 case 'wp_ajax_nopriv_jetpack_upload_file':
@@ -3504,18 +3825,17 @@
3504 3825 if ( ! is_int( $status_code ) ) {
3505 3826 $status_code = 400;
3506 3827 }
3507 3828
3508 - status_header( $status_code );
3509 - die( wp_json_encode( (object) compact( 'error', 'error_description' ) ) );
3829 + wp_send_json( (object) compact( 'error', 'error_description' ), $status_code, JSON_UNESCAPED_SLASHES );
3510 3830 }
3511 3831
3512 - status_header( 200 );
3513 3832 if ( true === $response ) {
3514 - exit;
3833 + status_header( 200 );
3834 + exit( 0 );
3515 3835 }
3516 3836
3517 - die( wp_json_encode( (object) $response ) );
3837 + wp_send_json( (object) $response, 200, JSON_UNESCAPED_SLASHES );
3518 3838 }
3519 3839
3520 3840 /**
3521 3841 * Uploads a file gotten from the global $_FILES.
@@ -3523,9 +3843,9 @@
3523 3843 * the attachment file of the media item (gotten through of the post_id)
3524 3844 * will be updated instead of add a new one.
3525 3845 *
3526 3846 * @param boolean $update_media_item - update media attachment.
3527 - * @return array - An array describing the uploadind files process.
3847 + * @return array|WP_Error - An array describing the uploading files process.
3528 3848 */
3529 3849 public function upload_handler( $update_media_item = false ) {
3530 3850 if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' !== strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) ) {
3531 3851 return new WP_Error( 405, get_status_header_desc( 405 ), 405 );
@@ -3576,9 +3896,9 @@
3576 3896 return new WP_Error( 'handler_cannot_upload', __( 'The upload handler cannot upload files', 'jetpack' ), 400 );
3577 3897 }
3578 3898
3579 3899 $uploaded_files = array();
3580 - $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
3900 + $global_post = $GLOBALS['post'] ?? null;
3581 3901 unset( $GLOBALS['post'] );
3582 3902 if ( empty( $_FILES['media']['name'] ) ) {
3583 3903 // Nothing to process, just return.
3584 3904 return $uploaded_files;
@@ -3585,9 +3905,9 @@
3585 3905 }
3586 3906 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.
3587 3907 $file = array();
3588 3908 foreach ( $media_keys as $media_key ) {
3589 - $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.
3909 + $file[ $media_key ] = $_FILES['media'][ $media_key ][ $index ] ?? null; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash,,WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- As above, the unslash sniff is wrong.
3590 3910 }
3591 3911
3592 3912 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.
3593 3913
@@ -3633,9 +3953,9 @@
3633 3953 'type' => (string) $edited_media_item->post_mime_type,
3634 3954 'meta' => (array) wp_get_attachment_metadata( $post_id ),
3635 3955 );
3636 3956
3637 - return (array) array( $response );
3957 + return array( $response );
3638 3958 }
3639 3959
3640 3960 $attachment_id = media_handle_upload(
3641 3961 '.jetpack.upload.',
@@ -3674,67 +3994,8 @@
3674 3994 return $uploaded_files;
3675 3995 }
3676 3996
3677 3997 /**
3678 - * Add help to the Jetpack page
3679 - *
3680 - * @since Jetpack (1.2.3)
3681 - * @return void
3682 - */
3683 - public function admin_help() {
3684 - $current_screen = get_current_screen();
3685 -
3686 - // Overview.
3687 - $current_screen->add_help_tab(
3688 - array(
3689 - 'id' => 'home',
3690 - 'title' => __( 'Home', 'jetpack' ),
3691 - 'content' =>
3692 - '<p><strong>' . __( 'Jetpack', 'jetpack' ) . '</strong></p>' .
3693 - '<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' .
3694 - '<p>' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '</p>',
3695 - )
3696 - );
3697 -
3698 - // Screen Content.
3699 - if ( current_user_can( 'manage_options' ) ) {
3700 - $current_screen->add_help_tab(
3701 - array(
3702 - 'id' => 'settings',
3703 - 'title' => __( 'Settings', 'jetpack' ),
3704 - 'content' =>
3705 - '<p><strong>' . __( 'Jetpack', 'jetpack' ) . '</strong></p>' .
3706 - '<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' .
3707 - '<ol>' .
3708 - '<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' .
3709 - '<li>' . __( 'Using the checkboxes next to each module, you can select multiple modules to toggle via the Bulk Actions menu at the top of the list.', 'jetpack' ) . '</li>' .
3710 - '</ol>' .
3711 - '<p>' . __( 'Using the tools on the right, you can search for specific modules, filter by module categories or which are active, or change the sorting order.', 'jetpack' ) . '</p>',
3712 - )
3713 - );
3714 - }
3715 -
3716 - // Help Sidebar.
3717 - $support_url = Redirect::get_url( 'jetpack-support' );
3718 - $faq_url = Redirect::get_url( 'jetpack-faq' );
3719 - $current_screen->set_help_sidebar(
3720 - '<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' .
3721 - '<p><a href="' . $faq_url . '" rel="noopener noreferrer" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' .
3722 - '<p><a href="' . $support_url . '" rel="noopener noreferrer" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' .
3723 - '<p><a href="' . self::admin_url( array( 'page' => 'jetpack-debugger' ) ) . '">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>'
3724 - );
3725 - }
3726 -
3727 - /**
3728 - * Enqueues the jetpack-icons style.
3729 - *
3730 - * @return void
3731 - */
3732 - public function admin_menu_css() {
3733 - wp_enqueue_style( 'jetpack-icons' );
3734 - }
3735 -
3736 - /**
3737 3998 * Add action links for the Jetpack plugin.
3738 3999 *
3739 4000 * @param array $actions Plugin actions.
3740 4001 *
@@ -3740,14 +4001,11 @@
3740 4001 *
3741 4002 * @return array
3742 4003 */
3743 4004 public function plugin_action_links( $actions ) {
3744 - $support_link = ( new Host() )->is_woa_site() ? 'https://wordpress.com/help/contact/' : self::admin_url( 'page=jetpack-debugger' );
3745 -
3746 4005 if ( current_user_can( 'jetpack_manage_modules' ) && ( self::is_connection_ready() || ( new Status() )->is_offline_mode() ) ) {
3747 4006 return array_merge(
3748 - array( 'settings' => sprintf( '<a href="%s">%s</a>', self::admin_url( 'page=jetpack#/settings' ), __( 'Settings', 'jetpack' ) ) ),
3749 - array( 'support' => sprintf( '<a href="%s">%s</a>', $support_link, __( 'Support', 'jetpack' ) ) ),
4007 + array( 'settings' => sprintf( '<a href="%s">%s</a>', esc_url( self::admin_url( 'page=jetpack-settings#/settings' ) ), __( 'Settings', 'jetpack' ) ) ),
3750 4008 $actions
3751 4009 );
3752 4010 }
3753 4011
@@ -3754,53 +4012,8 @@
3754 4012 return $actions;
3755 4013 }
3756 4014
3757 4015 /**
3758 - * Add an activation modal to the plugins page and the main dashboard.
3759 - *
3760 - * @param string $hook The current admin page.
3761 - *
3762 - * @return void
3763 - */
3764 - public function activate_dialog( $hook ) {
3765 - /*
3766 - * We do not need it on other plugin pages,
3767 - * or when Jetpack is already connected.
3768 - */
3769 - if (
3770 - ! in_array( $hook, array( 'plugins.php', 'index.php' ), true )
3771 - || self::is_connection_ready()
3772 - ) {
3773 - return;
3774 - }
3775 -
3776 - // add an activation script that will pick up deactivation actions for the Jetpack plugin.
3777 - Assets::register_script(
3778 - 'jetpack-full-activation-modal-js',
3779 - '_inc/build/activation-modal.js',
3780 - JETPACK__PLUGIN_FILE,
3781 - array(
3782 - 'enqueue' => true,
3783 - 'in_footer' => true,
3784 - 'textdomain' => 'jetpack',
3785 - 'dependencies' => array(
3786 - 'wp-polyfill',
3787 - 'wp-components',
3788 - ),
3789 - )
3790 - );
3791 -
3792 - // Add objects to be passed to the initial state of the app.
3793 - // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
3794 - wp_add_inline_script( 'jetpack-full-activation-modal-js', 'var Initial_State=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( Jetpack_Redux_State_Helper::get_minimal_state() ) ) . '"));', 'before' );
3795 -
3796 - // Adds Connection package initial state.
3797 - wp_add_inline_script( 'jetpack-full-activation-modal-js', Connection_Initial_State::render(), 'before' );
3798 -
3799 - add_action( 'admin_notices', array( $this, 'jetpack_plugin_portal_containers' ) );
3800 - }
3801 -
3802 - /**
3803 4016 * Adds the deactivation warning modal for Jetpack.
3804 4017 *
3805 4018 * @param string $hook The current admin page.
3806 4019 *
@@ -3820,14 +4033,10 @@
3820 4033 'jetpack-plugins-page-js',
3821 4034 '_inc/build/plugins-page.js',
3822 4035 JETPACK__PLUGIN_FILE,
3823 4036 array(
3824 - 'in_footer' => true,
3825 - 'textdomain' => 'jetpack',
3826 - 'dependencies' => array(
3827 - 'wp-polyfill',
3828 - 'wp-components',
3829 - ),
4037 + 'in_footer' => true,
4038 + 'textdomain' => 'jetpack',
3830 4039 )
3831 4040 );
3832 4041 Assets::enqueue_script( 'jetpack-plugins-page-js' );
3833 4042
@@ -3832,9 +4041,9 @@
3832 4041 Assets::enqueue_script( 'jetpack-plugins-page-js' );
3833 4042
3834 4043 // Add objects to be passed to the initial state of the app.
3835 4044 // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
3836 - wp_add_inline_script( 'jetpack-plugins-page-js', 'var Initial_State=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( Jetpack_Redux_State_Helper::get_minimal_state() ) ) . '"));', 'before' );
4045 + wp_add_inline_script( 'jetpack-plugins-page-js', 'var Initial_State=' . wp_json_encode( Jetpack_Redux_State_Helper::get_plugins_page_state(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';', 'before' );
3837 4046
3838 4047 add_action( 'admin_footer', array( $this, 'jetpack_plugin_portal_containers' ) );
3839 4048 }
3840 4049 }
@@ -3883,9 +4092,9 @@
3883 4092 // @todo provide way to go to specific calypso env.
3884 4093 self::get_calypso_host() . 'jetpack/connect'
3885 4094 )
3886 4095 );
3887 - exit;
4096 + exit( 0 );
3888 4097 }
3889 4098 }
3890 4099
3891 4100 /*
@@ -3920,8 +4129,28 @@
3920 4129 * Done!
3921 4130 */
3922 4131
3923 4132 /**
4133 + * Build the user-facing description stored alongside a registration error code.
4134 + *
4135 + * @since 16.2
4136 + *
4137 + * @param string $error_code The WP_Error code.
4138 + * @param string $message The WP_Error message.
4139 + * @return string The description, empty when the message is not user-facing copy.
4140 + */
4141 + public static function get_registration_error_description( $error_code, $message ) {
4142 + // Manager::validate_remote_register_response() does not always put user-facing copy in the
4143 + // message slot: wpcom_5??, wpcom_408 and wpcom_bad_response store the HTTP status there,
4144 + // and jetpack_id stores the raw response body, which can also overflow the state cookie.
4145 + if ( 'jetpack_id' === $error_code || is_numeric( $message ) ) {
4146 + return '';
4147 + }
4148 +
4149 + return mb_substr( (string) $message, 0, 250 );
4150 + }
4151 +
4152 + /**
3924 4153 * Handles the page load events for the Jetpack admin page
3925 4154 */
3926 4155 public function admin_page_load() {
3927 4156 $error = false;
@@ -3957,10 +4186,11 @@
3957 4186 $registered = static::connection()->try_registration();
3958 4187 if ( is_wp_error( $registered ) ) {
3959 4188 $error = $registered->get_error_code();
3960 4189 self::state( 'error', $error );
3961 - self::state( 'error', $registered->get_error_message() );
3962 4190
4191 + self::state( 'error_description', self::get_registration_error_description( $error, $registered->get_error_message() ) );
4192 +
3963 4193 /**
3964 4194 * Jetpack registration Error.
3965 4195 *
3966 4196 * @since 7.5.0
@@ -3984,12 +4214,8 @@
3984 4214 do_action( 'jetpack_connection_register_success', $from );
3985 4215
3986 4216 $url = $this->build_connect_url( true, $redirect, $from );
3987 4217
3988 - if ( ! empty( $_GET['onboarding'] ) ) {
3989 - $url = add_query_arg( 'onboarding', rawurlencode_deep( wp_unslash( $_GET['onboarding'] ) ), $url ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
3990 - }
3991 -
3992 4218 if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) {
3993 4219 $url = add_query_arg( 'auth_approved', 'true', $url );
3994 4220 }
3995 4221
@@ -3994,9 +4220,9 @@
3994 4220 }
3995 4221
3996 4222 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
3997 4223 wp_safe_redirect( $url );
3998 - exit;
4224 + exit( 0 );
3999 4225 case 'activate':
4000 4226 if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
4001 4227 $error = 'cheatin';
4002 4228 break;
@@ -4010,9 +4236,9 @@
4010 4236 self::state( 'error', sprintf( __( 'Could not activate %s', 'jetpack' ), $module ) );
4011 4237 }
4012 4238 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
4013 4239 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4014 - exit;
4240 + exit( 0 );
4015 4241 case 'activate_default_modules':
4016 4242 check_admin_referer( 'activate_default_modules' );
4017 4243 self::log( 'activate_default_modules' );
4018 4244 self::restate();
@@ -4020,9 +4246,9 @@
4020 4246 $max_version = isset( $_GET['max_version'] ) ? sanitize_text_field( wp_unslash( $_GET['max_version'] ) ) : false;
4021 4247 $other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_GET['other_modules'] ) ) : array();
4022 4248 self::activate_default_modules( $min_version, $max_version, $other_modules );
4023 4249 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4024 - exit;
4250 + exit( 0 );
4025 4251 case 'disconnect':
4026 4252 if ( ! current_user_can( 'jetpack_disconnect' ) ) {
4027 4253 $error = 'cheatin';
4028 4254 break;
@@ -4031,9 +4257,9 @@
4031 4257 check_admin_referer( 'jetpack-disconnect' );
4032 4258 self::log( 'disconnect' );
4033 4259 self::disconnect();
4034 4260 wp_safe_redirect( self::admin_url( 'disconnected=true' ) );
4035 - exit;
4261 + exit( 0 );
4036 4262 case 'reconnect':
4037 4263 if ( ! current_user_can( 'jetpack_reconnect' ) ) {
4038 4264 $error = 'cheatin';
4039 4265 break;
@@ -4044,9 +4270,9 @@
4044 4270 self::disconnect();
4045 4271
4046 4272 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
4047 4273 wp_safe_redirect( $this->build_connect_url( true, false, 'reconnect' ) );
4048 - exit;
4274 + exit( 0 );
4049 4275 case 'deactivate':
4050 4276 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
4051 4277 $error = 'cheatin';
4052 4278 break;
@@ -4060,9 +4286,9 @@
4060 4286 self::state( 'message', 'module_deactivated' );
4061 4287 }
4062 4288 self::state( 'module', $modules );
4063 4289 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4064 - exit;
4290 + exit( 0 );
4065 4291 case 'unlink':
4066 4292 $redirect = isset( $_GET['redirect'] ) ? sanitize_text_field( wp_unslash( $_GET['redirect'] ) ) : '';
4067 4293 check_admin_referer( 'jetpack-unlink' );
4068 4294 self::log( 'unlink' );
@@ -4072,32 +4298,9 @@
4072 4298 wp_safe_redirect( admin_url() );
4073 4299 } else {
4074 4300 wp_safe_redirect( self::admin_url( array( 'page' => rawurlencode( $redirect ) ) ) );
4075 4301 }
4076 - exit;
4077 - case 'onboard':
4078 - if ( ! current_user_can( 'manage_options' ) ) {
4079 - wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4080 - } else {
4081 - self::create_onboarding_token();
4082 - $url = $this->build_connect_url( true );
4083 -
4084 - $token = Jetpack_Options::get_option( 'onboarding' );
4085 -
4086 - if ( false !== ( $token ) ) {
4087 - $url = add_query_arg( 'onboarding', $token, $url );
4088 - }
4089 -
4090 - $calypso_env = $this->get_calypso_env();
4091 - if ( ! empty( $calypso_env ) ) {
4092 - $url = add_query_arg( 'calypso_env', $calypso_env, $url );
4093 - }
4094 -
4095 - add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
4096 - wp_safe_redirect( $url );
4097 - exit;
4098 - }
4099 - exit;
4302 + exit( 0 );
4100 4303 default:
4101 4304 /**
4102 4305 * Fires when a Jetpack admin page is loaded with an unrecognized parameter.
4103 4306 *
@@ -4113,9 +4316,10 @@
4113 4316 if ( ! $error ) {
4114 4317 self::activate_new_modules( true );
4115 4318 }
4116 4319
4117 - $message_code = self::state( 'message' );
4320 + $activated_manage = false;
4321 + $message_code = self::state( 'message' );
4118 4322 if ( self::state( 'optin-manage' ) ) {
4119 4323 $activated_manage = $message_code;
4120 4324 $message_code = 'jetpack-manage';
4121 4325 }
@@ -4289,17 +4493,19 @@
4289 4493 count( $privacy_checks ),
4290 4494 '%1$s = deactivation URL, %2$s = "Deactivate {list of Jetpack module/feature names}',
4291 4495 'jetpack'
4292 4496 ),
4293 - wp_nonce_url(
4294 - self::admin_url(
4295 - array(
4296 - 'page' => 'jetpack',
4297 - 'action' => 'deactivate',
4298 - 'module' => rawurlencode( $module_slugs ),
4299 - )
4300 - ),
4301 - "jetpack_deactivate-$module_slugs"
4497 + esc_url(
4498 + wp_nonce_url(
4499 + self::admin_url(
4500 + array(
4501 + 'page' => 'jetpack',
4502 + 'action' => 'deactivate',
4503 + 'module' => rawurlencode( $module_slugs ),
4504 + )
4505 + ),
4506 + "jetpack_deactivate-$module_slugs"
4507 + )
4302 4508 ),
4303 4509 esc_attr( wp_kses( wp_sprintf( _x( 'Deactivate %l', '%l = list of Jetpack module/feature names', 'jetpack' ), $module_names ), array() ) )
4304 4510 ),
4305 4511 array(
@@ -4317,37 +4523,8 @@
4317 4523 endif;
4318 4524 }
4319 4525
4320 4526 /**
4321 - * We can't always respond to a signed XML-RPC request with a
4322 - * helpful error message. In some circumstances, doing so could
4323 - * leak information.
4324 - *
4325 - * Instead, track that the error occurred via a Jetpack_Option,
4326 - * and send that data back in the heartbeat.
4327 - * All this does is increment a number, but it's enough to find
4328 - * trends.
4329 - *
4330 - * @param WP_Error $xmlrpc_error The error produced during
4331 - * signature validation.
4332 - */
4333 - public function track_xmlrpc_error( $xmlrpc_error ) {
4334 - $code = is_wp_error( $xmlrpc_error )
4335 - ? $xmlrpc_error->get_error_code()
4336 - : 'should-not-happen';
4337 -
4338 - $xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
4339 - if ( isset( $xmlrpc_errors[ $code ] ) && $xmlrpc_errors[ $code ] ) {
4340 - // No need to update the option if we already have
4341 - // this code stored.
4342 - return;
4343 - }
4344 - $xmlrpc_errors[ $code ] = true;
4345 -
4346 - Jetpack_Options::update_option( 'xmlrpc_errors', $xmlrpc_errors, false );
4347 - }
4348 -
4349 - /**
4350 4527 * Initialize the jetpack stats instance only when needed
4351 4528 *
4352 4529 * @return void
4353 4530 */
@@ -4445,9 +4622,9 @@
4445 4622 if ( is_network_admin() ) {
4446 4623 $url = add_query_arg( 'is_multisite', network_admin_url( 'admin.php?page=jetpack-settings' ), $url );
4447 4624 }
4448 4625
4449 - $calypso_env = self::get_calypso_env();
4626 + $calypso_env = ( new Host() )->get_calypso_env();
4450 4627
4451 4628 if ( ! empty( $calypso_env ) ) {
4452 4629 $url = add_query_arg( 'calypso_env', $calypso_env, $url );
4453 4630 }
@@ -4470,9 +4647,9 @@
4470 4647 return $this->build_connect_url( $raw, $redirect, $from, true );
4471 4648 }
4472 4649 }
4473 4650
4474 - $url = $this->build_authorize_url( $redirect );
4651 + $url = ( new Authorize_Redirect( static::connection() ) )->build_authorize_url( $redirect );
4475 4652 }
4476 4653
4477 4654 if ( $from ) {
4478 4655 $url = add_query_arg( 'from', $from, $url );
@@ -4497,66 +4674,30 @@
4497 4674 * @param null $deprecated Deprecated since Jetpack 10.9.
4498 4675 *
4499 4676 * @todo Update default value for redirect since the called function expects a string.
4500 4677 *
4678 + * @deprecated 13.4
4679 + *
4501 4680 * @return mixed|void
4502 4681 */
4503 4682 public static function build_authorize_url( $redirect = false, $deprecated = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
4683 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::build_authorize_url' );
4504 4684
4505 - add_filter( 'jetpack_connect_request_body', array( __CLASS__, 'filter_connect_request_body' ) );
4506 - add_filter( 'jetpack_connect_redirect_url', array( __CLASS__, 'filter_connect_redirect_url' ) );
4507 -
4508 - $c8n = self::connection();
4509 - $url = $c8n->get_authorization_url( wp_get_current_user(), $redirect );
4510 -
4511 - remove_filter( 'jetpack_connect_request_body', array( __CLASS__, 'filter_connect_request_body' ) );
4512 - remove_filter( 'jetpack_connect_redirect_url', array( __CLASS__, 'filter_connect_redirect_url' ) );
4513 -
4514 - /**
4515 - * Filter the URL used when authorizing a user to a WordPress.com account.
4516 - *
4517 - * @since 8.9.0
4518 - *
4519 - * @param string $url Connection URL.
4520 - */
4521 - return apply_filters( 'jetpack_build_authorize_url', $url );
4685 + return ( new Authorize_Redirect( static::connection() ) )->build_authorize_url( $redirect );
4522 4686 }
4523 4687
4524 4688 /**
4525 4689 * Filters the connection URL parameter array.
4526 4690 *
4691 + * @deprecated 13.4
4692 + *
4527 4693 * @param array $args default URL parameters used by the package.
4528 4694 * @return array the modified URL arguments array.
4529 4695 */
4530 4696 public static function filter_connect_request_body( $args ) {
4531 - if (
4532 - Constants::is_defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' )
4533 - && include_once Constants::get_constant( 'JETPACK__GLOTPRESS_LOCALES_PATH' )
4534 - ) {
4535 - $gp_locale = GP_Locales::by_field( 'wp_locale', get_locale() );
4536 - $args['locale'] = isset( $gp_locale ) && isset( $gp_locale->slug )
4537 - ? $gp_locale->slug
4538 - : '';
4539 - }
4697 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::filter_connect_request_body' );
4540 4698
4541 - $tracking = new Tracking();
4542 - $tracks_identity = $tracking->tracks_get_identity( $args['state'] );
4543 -
4544 - $args = array_merge(
4545 - $args,
4546 - array(
4547 - '_ui' => $tracks_identity['_ui'],
4548 - '_ut' => $tracks_identity['_ut'],
4549 - )
4550 - );
4551 -
4552 - $calypso_env = self::get_calypso_env();
4553 -
4554 - if ( ! empty( $calypso_env ) ) {
4555 - $args['calypso_env'] = $calypso_env;
4556 - }
4557 -
4558 - return $args;
4699 + return Authorize_Redirect::filter_connect_request_body( $args );
4559 4700 }
4560 4701
4561 4702 /**
4562 4703 * Filters the `jetpack/v4/connection/data` API response of the Connection package in order to
@@ -4593,41 +4734,19 @@
4593 4734 return $current_user_connection_data;
4594 4735 }
4595 4736
4596 4737 /**
4597 - * Filters the URL that will process the connection data. It can be different from the URL
4598 - * that we send the user to after everything is done.
4599 - *
4600 - * @param String $processing_url the default redirect URL used by the package.
4601 - * @return String the modified URL.
4602 - *
4603 - * @deprecated since Jetpack 9.5.0
4604 - */
4605 - public static function filter_connect_processing_url( $processing_url ) {
4606 - _deprecated_function( __METHOD__, 'jetpack-9.5' );
4607 -
4608 - $processing_url = admin_url( 'admin.php?page=jetpack' ); // Making PHPCS happy.
4609 - return $processing_url;
4610 - }
4611 -
4612 - /**
4613 4738 * Filters the redirection URL that is used for connect requests. The redirect
4614 4739 * URL should return the user back to the Jetpack console.
4615 4740 *
4741 + * @deprecated 13.4
4742 + *
4616 4743 * @param String $redirect the default redirect URL used by the package.
4617 4744 * @return String the modified URL.
4618 4745 */
4619 4746 public static function filter_connect_redirect_url( $redirect ) {
4620 - $jetpack_admin_page = esc_url_raw( admin_url( 'admin.php?page=jetpack' ) );
4621 - $redirect = $redirect
4622 - ? wp_validate_redirect( esc_url_raw( $redirect ), $jetpack_admin_page )
4623 - : $jetpack_admin_page;
4624 -
4625 - if ( isset( $_REQUEST['is_multisite'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not making a site change here.
4626 - $redirect = Jetpack_Network::init()->get_url( 'network_admin_page' );
4627 - }
4628 -
4629 - return $redirect;
4747 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::filter_connect_redirect_url' );
4748 + return Authorize_Redirect::filter_connect_redirect_url( $redirect );
4630 4749 }
4631 4750
4632 4751 /**
4633 4752 * This action fires at the beginning of the Manager::authorize method.
@@ -4691,11 +4810,8 @@
4691 4810 *
4692 4811 * @param array $data The request data.
4693 4812 */
4694 4813 public static function authorize_ending_authorized( $data ) {
4695 - // If this site has been through the Jetpack Onboarding flow, delete the onboarding token.
4696 - self::invalidate_onboarding_token();
4697 -
4698 4814 // If redirect_uri is SSO, ensure SSO module is enabled.
4699 4815 parse_str( wp_parse_url( $data['redirect_uri'], PHP_URL_QUERY ), $redirect_options );
4700 4816
4701 4817 /** This filter is documented in class.jetpack-cli.php */
@@ -4819,13 +4935,9 @@
4819 4935 *
4820 4936 * @return int 0 if the same sort or (+/-) to indicate which is greater.
4821 4937 */
4822 4938 public static function sort_modules( $a, $b ) {
4823 - if ( $a['sort'] === $b['sort'] ) {
4824 - return 0;
4825 - }
4826 -
4827 - return ( $a['sort'] < $b['sort'] ) ? -1 : 1;
4939 + return $a['sort'] <=> $b['sort'];
4828 4940 }
4829 4941
4830 4942 /**
4831 4943 * Recheck SSL status for use via an AJAX call.
@@ -4839,10 +4951,12 @@
4839 4951 $result = self::permit_ssl( true );
4840 4952 wp_send_json(
4841 4953 array(
4842 4954 'enabled' => $result,
4843 - 'message' => get_transient( 'jetpack_https_test_message' ),
4844 - )
4955 + 'message' => self::get_ssl_test_message(),
4956 + ),
4957 + null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
4958 + JSON_UNESCAPED_SLASHES
4845 4959 );
4846 4960 }
4847 4961
4848 4962 /* Client API */
@@ -4849,8 +4963,10 @@
4849 4963
4850 4964 /**
4851 4965 * Verify the onboarding token.
4852 4966 *
4967 + * @deprecated since 13.9
4968 + *
4853 4969 * @param array $token_data Token data.
4854 4970 * @param string $token Token value.
4855 4971 * @param string $request_data JSON-encoded request data.
4856 4972 *
@@ -4856,8 +4972,9 @@
4856 4972 *
4857 4973 * @return mixed
4858 4974 */
4859 4975 public static function verify_onboarding_token( $token_data, $token, $request_data ) {
4976 + _deprecated_function( __METHOD__, '13.9' );
4860 4977 // Default to a blog token.
4861 4978 $token_type = 'blog';
4862 4979
4863 4980 // Let's see if this is onboarding. In such case, use user token type and the provided user id.
@@ -4885,9 +5002,9 @@
4885 5002 $jp_user = get_user_by( 'email', $jpo_user );
4886 5003 if ( is_a( $jp_user, 'WP_User' ) ) {
4887 5004 wp_set_current_user( $jp_user->ID );
4888 5005 $user_can = is_multisite()
4889 - ? current_user_can_for_blog( get_current_blog_id(), 'manage_options' )
5006 + ? current_user_can_for_site( get_current_blog_id(), 'manage_options' )
4890 5007 : current_user_can( 'manage_options' );
4891 5008 if ( $user_can ) {
4892 5009 $token_type = 'user';
4893 5010 $token->external_user_id = $jp_user->ID;
@@ -4904,11 +5021,13 @@
4904 5021
4905 5022 /**
4906 5023 * Create a random secret for validating onboarding payload
4907 5024 *
5025 + * @deprecated since 13.9
4908 5026 * @return string Secret token
4909 5027 */
4910 5028 public static function create_onboarding_token() {
5029 + _deprecated_function( __METHOD__, '13.9' );
4911 5030 $token = Jetpack_Options::get_option( 'onboarding' );
4912 5031 if ( false === ( $token ) ) {
4913 5032 $token = wp_generate_password( 32, false );
4914 5033 Jetpack_Options::update_option( 'onboarding', $token );
@@ -4919,11 +5038,13 @@
4919 5038
4920 5039 /**
4921 5040 * Remove the onboarding token
4922 5041 *
5042 + * @deprecated since 13.9
4923 5043 * @return bool True on success, false on failure
4924 5044 */
4925 5045 public static function invalidate_onboarding_token() {
5046 + _deprecated_function( __METHOD__, '13.9' );
4926 5047 return Jetpack_Options::delete_option( 'onboarding' );
4927 5048 }
4928 5049
4929 5050 /**
@@ -4928,8 +5049,10 @@
4928 5049
4929 5050 /**
4930 5051 * Validate an onboarding token for a specific action
4931 5052 *
5053 + * @deprecated since 13.9
5054 + *
4932 5055 * @param string $token Onboarding token.
4933 5056 * @param string $action Action name.
4934 5057 *
4935 5058 * @return boolean True if token/action pair is accepted, false if not
@@ -4934,8 +5057,9 @@
4934 5057 *
4935 5058 * @return boolean True if token/action pair is accepted, false if not
4936 5059 */
4937 5060 public static function validate_onboarding_token_action( $token, $action ) {
5061 + _deprecated_function( __METHOD__, '13.9' );
4938 5062 // Compare tokens, bail if tokens do not match.
4939 5063 if ( ! hash_equals( $token, Jetpack_Options::get_option( 'onboarding' ) ) ) {
4940 5064 return false;
4941 5065 }
@@ -4961,39 +5085,41 @@
4961 5085 * @return boolean
4962 5086 * @since 2.3.3
4963 5087 */
4964 5088 public static function permit_ssl( $force_recheck = false ) {
4965 - // Do some fancy tests to see if ssl is being supported.
4966 - if ( ! $force_recheck ) {
4967 - $ssl = get_transient( 'jetpack_https_test' );
5089 + if ( ! method_exists( Heartbeat::class, 'permit_ssl' ) ) {
5090 + // Skip the SSL-fail notice when the check cannot run.
5091 + return true;
4968 5092 }
4969 5093
4970 - if ( $force_recheck || false === $ssl ) {
4971 - $message = '';
4972 - if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) {
4973 - $ssl = 0;
4974 - } else {
4975 - $ssl = 1;
5094 + return Heartbeat::permit_ssl( $force_recheck );
5095 + }
4976 5096
4977 - if ( ! wp_http_supports( array( 'ssl' => true ) ) ) {
4978 - $ssl = 0;
4979 - $message = __( 'WordPress reports no SSL support', 'jetpack' );
4980 - } else {
4981 - $response = wp_remote_get( JETPACK__API_BASE . 'test/1/' );
4982 - if ( is_wp_error( $response ) ) {
4983 - $ssl = 0;
4984 - $message = __( 'WordPress reports no SSL support', 'jetpack' );
4985 - } elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) {
4986 - $ssl = 0;
4987 - $message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response );
4988 - }
4989 - }
4990 - }
4991 - set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS );
4992 - set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS );
5097 + /**
5098 + * Returns a localized message describing the last SSL connectivity failure, if any.
5099 + *
5100 + * The Connection package's canonical SSL check stores a neutral reason code; this maps it to
5101 + * a translated, `jetpack`-domain message for display in the admin notice and AJAX recheck.
5102 + *
5103 + * @since 16.1
5104 + *
5105 + * @return string The localized message, or an empty string when there is no failure.
5106 + */
5107 + public static function get_ssl_test_message() {
5108 + if ( ! method_exists( Heartbeat::class, 'get_ssl_test_error' ) ) {
5109 + return '';
4993 5110 }
4994 5111
4995 - return (bool) $ssl;
5112 + $error = Heartbeat::get_ssl_test_error();
5113 +
5114 + switch ( $error['code'] ) {
5115 + case 'no_ssl_support':
5116 + return __( 'WordPress reports no SSL support', 'jetpack' );
5117 + case 'bad_response':
5118 + return __( 'Response was not OK: ', 'jetpack' ) . $error['detail'];
5119 + default:
5120 + return '';
5121 + }
4996 5122 }
4997 5123
4998 5124 /**
4999 5125 * Displays an admin_notice, alerting the user that outbound SSL isn't working.
@@ -5012,9 +5138,9 @@
5012 5138 <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>
5013 5139 <p>
5014 5140 <?php esc_html_e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?>
5015 5141 <a href="#" id="jetpack-recheck-ssl-button"><?php esc_html_e( 'Try again', 'jetpack' ); ?></a>
5016 - <span id="jetpack-recheck-ssl-output"><?php echo esc_html( get_transient( 'jetpack_https_test_message' ) ); ?></span>
5142 + <span id="jetpack-recheck-ssl-output"><?php echo esc_html( self::get_ssl_test_message() ); ?></span>
5017 5143 </p>
5018 5144 <p>
5019 5145 <?php
5020 5146 printf(
@@ -5033,18 +5159,18 @@
5033 5159 <script type="text/javascript">
5034 5160 jQuery( document ).ready( function( $ ) {
5035 5161 $( '#jetpack-recheck-ssl-button' ).click( function( e ) {
5036 5162 var $this = $( this );
5037 - $this.html( <?php echo wp_json_encode( __( 'Checking', 'jetpack' ) ); ?> );
5163 + $this.html( <?php echo wp_json_encode( esc_html__( 'Checking', 'jetpack' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> );
5038 5164 $( '#jetpack-recheck-ssl-output' ).html( '' );
5039 5165 e.preventDefault();
5040 - var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': <?php echo wp_json_encode( $ajax_nonce ); ?> };
5166 + var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': <?php echo wp_json_encode( $ajax_nonce, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> };
5041 5167 $.post( ajaxurl, data )
5042 5168 .done( function( response ) {
5043 5169 if ( response.enabled ) {
5044 5170 $( '#jetpack-ssl-warning' ).hide();
5045 5171 } else {
5046 - this.html( <?php echo wp_json_encode( __( 'Try again', 'jetpack' ) ); ?> );
5172 + this.html( <?php echo wp_json_encode( esc_html__( 'Try again', 'jetpack' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> );
5047 5173 $( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message );
5048 5174 }
5049 5175 }.bind( $this ) );
5050 5176 } );
@@ -5070,26 +5196,8 @@
5070 5196 return $jetpack->connection_manager;
5071 5197 }
5072 5198
5073 5199 /**
5074 - * Creates two secret tokens and the end of life timestamp for them.
5075 - *
5076 - * Note these tokens are unique per call, NOT static per site for connecting.
5077 - *
5078 - * @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->generate() instead.
5079 - *
5080 - * @since 2.6
5081 - * @param String $action The action name.
5082 - * @param Integer $user_id The user identifier.
5083 - * @param Integer $exp Expiration time in seconds.
5084 - * @return array
5085 - */
5086 - public static function generate_secrets( $action, $user_id = false, $exp = 600 ) {
5087 - _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Secrets->generate' );
5088 - return self::connection()->generate_secrets( $action, $user_id, $exp );
5089 - }
5090 -
5091 - /**
5092 5200 * Get verification secrets.
5093 5201 *
5094 5202 * @param string $action Action name.
5095 5203 * @param int $user_id User ID.
@@ -5110,35 +5218,8 @@
5110 5218 return $secrets;
5111 5219 }
5112 5220
5113 5221 /**
5114 - * Register a connection.
5115 - *
5116 - * @deprecated Jetpack 9.7.0
5117 - * @see Automattic\Jetpack\Connection\Manager::try_registration()
5118 - *
5119 - * @return bool|WP_Error
5120 - */
5121 - public static function register() {
5122 - _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Manager::try_registration' );
5123 - return static::connection()->try_registration( false );
5124 - }
5125 -
5126 - /**
5127 - * Filters the registration request body to include tracking properties.
5128 - *
5129 - * @deprecated Jetpack 9.7.0
5130 - * @see Automattic\Jetpack\Connection\Utils::filter_register_request_body()
5131 - *
5132 - * @param array $properties Token request properties.
5133 - * @return array amended properties.
5134 - */
5135 - public static function filter_register_request_body( $properties ) {
5136 - _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Utils::filter_register_request_body' );
5137 - return Connection_Utils::filter_register_request_body( $properties );
5138 - }
5139 -
5140 - /**
5141 5222 * Filters the token request body to include tracking properties.
5142 5223 *
5143 5224 * @param array $properties Token request properties.
5144 5225 *
@@ -5159,9 +5240,9 @@
5159 5240
5160 5241 /**
5161 5242 * If the db version is showing something other that what we've got now, bump it to current.
5162 5243 *
5163 - * @return bool: True if the option was incorrect and updated, false if nothing happened.
5244 + * @return bool True if the option was incorrect and updated, false if nothing happened.
5164 5245 */
5165 5246 public static function maybe_set_version_option() {
5166 5247 list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
5167 5248 if ( JETPACK__VERSION !== $version ) {
@@ -5179,21 +5260,8 @@
5179 5260
5180 5261 /* Client Server API */
5181 5262
5182 5263 /**
5183 - * Loads the Jetpack XML-RPC client.
5184 - * No longer necessary, as the XML-RPC client will be automagically loaded.
5185 - *
5186 - * Note: we cannot remove this function yet as it is used in this plugin:
5187 - * https://wordpress.org/plugins/jetpack-subscription-form/
5188 - *
5189 - * @deprecated since 7.7.0
5190 - */
5191 - public static function load_xml_rpc_client() {
5192 - _deprecated_function( __METHOD__, 'jetpack-7.7' );
5193 - }
5194 -
5195 - /**
5196 5264 * State is passed via cookies from one request to the next, but never to subsequent requests.
5197 5265 * SET: state( $key, $value );
5198 5266 * GET: $value = state( $key );
5199 5267 *
@@ -5267,20 +5335,8 @@
5267 5335
5268 5336 self::state( 'privacy_checks', $privacy_checks );
5269 5337 }
5270 5338
5271 - /**
5272 - * Serve a WordPress.com static resource via a randomized wp.com subdomain.
5273 - *
5274 - * @deprecated 9.3.0 Use Assets::staticize_subdomain.
5275 - *
5276 - * @param string $url WordPress.com static resource URL.
5277 - */
5278 - public static function staticize_subdomain( $url ) {
5279 - _deprecated_function( __METHOD__, 'jetpack-9.3.0', 'Automattic\Jetpack\Assets::staticize_subdomain' );
5280 - return Assets::staticize_subdomain( $url );
5281 - }
5282 -
5283 5339 /* JSON API Authorization */
5284 5340
5285 5341 /**
5286 5342 * Handles the login action for Authorizing the JSON API
@@ -5285,13 +5341,14 @@
5285 5341 /**
5286 5342 * Handles the login action for Authorizing the JSON API
5287 5343 */
5288 5344 public function login_form_json_api_authorization() {
5289 - $this->verify_json_api_authorization_request();
5345 + $authorize_json_api = new Authorize_Json_Api();
5346 + $authorize_json_api->verify_json_api_authorization_request();
5290 5347
5291 - add_action( 'wp_login', array( $this, 'store_json_api_authorization_token' ), 10, 2 );
5348 + add_action( 'wp_login', array( $authorize_json_api, 'store_json_api_authorization_token' ), 10, 2 );
5292 5349
5293 - add_action( 'login_message', array( $this, 'login_message_json_api_authorization' ) );
5350 + add_action( 'login_message', array( $authorize_json_api, 'login_message_json_api_authorization' ) );
5294 5351 add_action( 'login_form', array( $this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
5295 5352 add_filter( 'site_url', array( $this, 'post_login_form_to_signed_url' ), 10, 3 );
5296 5353 }
5297 5354
@@ -5329,16 +5386,17 @@
5329 5386
5330 5387 /**
5331 5388 * If someone logs in to approve API access, store the Access Code in usermeta.
5332 5389 *
5390 + * @deprecated 13.4
5391 + *
5333 5392 * @param string $user_login Unused.
5334 5393 * @param WP_User $user User logged in.
5335 5394 */
5336 5395 public function store_json_api_authorization_token( $user_login, $user ) {
5337 - add_filter( 'login_redirect', array( $this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
5338 - add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_public_api_domain' ) );
5339 - $token = wp_generate_password( 32, false );
5340 - update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token );
5396 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::store_json_api_authorization_token' );
5397 +
5398 + return ( new Authorize_Json_Api() )->store_json_api_authorization_token( $user_login, $user );
5341 5399 }
5342 5400
5343 5401 /**
5344 5402 * Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
@@ -5344,23 +5402,29 @@
5344 5402 * Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
5345 5403 *
5346 5404 * To be used with a filter of allowed domains for a redirect.
5347 5405 *
5406 + * @deprecated 13.4
5407 + *
5348 5408 * @param array $domains Allowed WP.com Environments.
5349 5409 */
5350 5410 public function allow_wpcom_public_api_domain( $domains ) {
5351 - $domains[] = 'public-api.wordpress.com';
5352 - return $domains;
5411 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Status\\Host::allow_wpcom_public_api_domain' );
5412 +
5413 + return Host::allow_wpcom_public_api_domain( $domains );
5353 5414 }
5354 5415
5355 5416 /**
5356 5417 * Check if the redirect is encoded.
5357 5418 *
5419 + * @deprecated 13.4
5420 + *
5358 5421 * @param string $redirect_url Redirect URL.
5359 5422 *
5360 5423 * @return bool If redirect has been encoded.
5361 5424 */
5362 5425 public static function is_redirect_encoded( $redirect_url ) {
5426 + _deprecated_function( __METHOD__, 'jetpack-13.4' );
5363 5427 return preg_match( '/https?%3A%2F%2F/i', $redirect_url ) > 0;
5364 5428 }
5365 5429
5366 5430 /**
@@ -5378,8 +5442,10 @@
5378 5442
5379 5443 /**
5380 5444 * Add the Access Code details to the public-api.wordpress.com redirect.
5381 5445 *
5446 + * @deprecated 13.4
5447 + *
5382 5448 * @param string $redirect_to URL.
5383 5449 * @param string $original_redirect_to URL.
5384 5450 * @param WP_User $user WP_User for the redirect.
5385 5451 *
@@ -5385,23 +5451,18 @@
5385 5451 *
5386 5452 * @return string
5387 5453 */
5388 5454 public function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
5389 - return add_query_arg(
5390 - urlencode_deep(
5391 - array(
5392 - 'jetpack-code' => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], true ),
5393 - 'jetpack-user-id' => (int) $user->ID,
5394 - 'jetpack-state' => $this->json_api_authorization_request['state'],
5395 - )
5396 - ),
5397 - $redirect_to
5398 - );
5455 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::add_token_to_login_redirect_json_api_authorization' );
5456 +
5457 + return ( new Authorize_Json_Api() )->add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user );
5399 5458 }
5400 5459
5401 5460 /**
5402 5461 * Verifies the request by checking the signature
5403 5462 *
5463 + * @deprecated 13.4
5464 + *
5404 5465 * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow
5405 5466 * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO.
5406 5467 *
5407 5468 * @param null|array $environment Value to override $_REQUEST.
@@ -5406,194 +5467,24 @@
5406 5467 *
5407 5468 * @param null|array $environment Value to override $_REQUEST.
5408 5469 */
5409 5470 public function verify_json_api_authorization_request( $environment = null ) {
5410 - $environment = $environment === null
5411 - ? $_REQUEST // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce verification handled later in function.
5412 - : $environment;
5471 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::verify_json_api_authorization_request' );
5413 5472
5414 - list( $env_token,, $env_user_id ) = explode( ':', $environment['token'] );
5415 - $token = ( new Tokens() )->get_access_token( $env_user_id, $env_token );
5416 - if ( ! $token || empty( $token->secret ) ) {
5417 - wp_die( esc_html__( 'You must connect your Jetpack plugin to WordPress.com to use this feature.', 'jetpack' ) );
5418 - }
5419 -
5420 - $die_error = __( 'Someone may be trying to trick you into giving them access to your site. Or it could be you just encountered a bug :). Either way, please close this window.', 'jetpack' );
5421 -
5422 - // Host has encoded the request URL, probably as a result of a bad http => https redirect.
5423 - if ( self::is_redirect_encoded( esc_url_raw( wp_unslash( $_GET['redirect_to'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- no site changes, we're erroring out.
5424 - /**
5425 - * Jetpack authorisation request Error.
5426 - *
5427 - * @since 7.5.0
5428 - */
5429 - do_action( 'jetpack_verify_api_authorization_request_error_double_encode' );
5430 - $die_error = sprintf(
5431 - /* translators: %s is a URL */
5432 - __( 'Your site is incorrectly double-encoding redirects from http to https. This is preventing Jetpack from authenticating your connection. Please visit our <a href="%s">support page</a> for details about how to resolve this.', 'jetpack' ),
5433 - Redirect::get_url( 'jetpack-support-double-encoding' )
5434 - );
5435 - }
5436 -
5437 - $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
5438 -
5439 - if ( isset( $environment['jetpack_json_api_original_query'] ) ) {
5440 - $signature = $jetpack_signature->sign_request(
5441 - $environment['token'],
5442 - $environment['timestamp'],
5443 - $environment['nonce'],
5444 - '',
5445 - 'GET',
5446 - $environment['jetpack_json_api_original_query'],
5447 - null,
5448 - true
5449 - );
5450 - } else {
5451 - $signature = $jetpack_signature->sign_current_request(
5452 - array(
5453 - 'body' => null,
5454 - 'method' => 'GET',
5455 - )
5456 - );
5457 - }
5458 -
5459 - if ( ! $signature ) {
5460 - wp_die(
5461 - wp_kses(
5462 - $die_error,
5463 - array(
5464 - 'a' => array(
5465 - 'href' => array(),
5466 - ),
5467 - )
5468 - )
5469 - );
5470 - } elseif ( is_wp_error( $signature ) ) {
5471 - wp_die(
5472 - wp_kses(
5473 - $die_error,
5474 - array(
5475 - 'a' => array(
5476 - 'href' => array(),
5477 - ),
5478 - )
5479 - )
5480 - );
5481 - } elseif ( ! hash_equals( $signature, $environment['signature'] ) ) {
5482 - if ( is_ssl() ) {
5483 - // If we signed an HTTP request on the Jetpack Servers, but got redirected to HTTPS by the local blog, check the HTTP signature as well.
5484 - $signature = $jetpack_signature->sign_current_request(
5485 - array(
5486 - 'scheme' => 'http',
5487 - 'body' => null,
5488 - 'method' => 'GET',
5489 - )
5490 - );
5491 - if ( ! $signature || is_wp_error( $signature ) || ! hash_equals( $signature, $environment['signature'] ) ) {
5492 - wp_die(
5493 - wp_kses(
5494 - $die_error,
5495 - array(
5496 - 'a' => array(
5497 - 'href' => array(),
5498 - ),
5499 - )
5500 - )
5501 - );
5502 - }
5503 - } else {
5504 - wp_die(
5505 - wp_kses(
5506 - $die_error,
5507 - array(
5508 - 'a' => array(
5509 - 'href' => array(),
5510 - ),
5511 - )
5512 - )
5513 - );
5514 - }
5515 - }
5516 -
5517 - $timestamp = (int) $environment['timestamp'];
5518 - $nonce = stripslashes( (string) $environment['nonce'] );
5519 -
5520 - if ( ! $this->connection_manager ) {
5521 - $this->connection_manager = new Connection_Manager();
5522 - }
5523 -
5524 - if ( ! ( new Nonce_Handler() )->add( $timestamp, $nonce ) ) {
5525 - // De-nonce the nonce, at least for 5 minutes.
5526 - // We have to reuse this nonce at least once (used the first time when the initial request is made, used a second time when the login form is POSTed).
5527 - $old_nonce_time = get_option( "jetpack_nonce_{$timestamp}_{$nonce}" );
5528 - if ( $old_nonce_time < time() - 300 ) {
5529 - wp_die( esc_html__( 'The authorization process expired. Please go back and try again.', 'jetpack' ) );
5530 - }
5531 - }
5532 -
5533 - $data = json_decode( base64_decode( stripslashes( $environment['data'] ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
5534 - $data_filters = array(
5535 - 'state' => 'opaque',
5536 - 'client_id' => 'int',
5537 - 'client_title' => 'string',
5538 - 'client_image' => 'url',
5539 - );
5540 -
5541 - foreach ( $data_filters as $key => $sanitation ) {
5542 - if ( ! isset( $data->$key ) ) {
5543 - wp_die(
5544 - wp_kses(
5545 - $die_error,
5546 - array(
5547 - 'a' => array(
5548 - 'href' => array(),
5549 - ),
5550 - )
5551 - )
5552 - );
5553 - }
5554 -
5555 - switch ( $sanitation ) {
5556 - case 'int':
5557 - $this->json_api_authorization_request[ $key ] = (int) $data->$key;
5558 - break;
5559 - case 'opaque':
5560 - $this->json_api_authorization_request[ $key ] = (string) $data->$key;
5561 - break;
5562 - case 'string':
5563 - $this->json_api_authorization_request[ $key ] = wp_kses( (string) $data->$key, array() );
5564 - break;
5565 - case 'url':
5566 - $this->json_api_authorization_request[ $key ] = esc_url_raw( (string) $data->$key );
5567 - break;
5568 - }
5569 - }
5570 -
5571 - if ( empty( $this->json_api_authorization_request['client_id'] ) ) {
5572 - wp_die(
5573 - wp_kses(
5574 - $die_error,
5575 - array(
5576 - 'a' => array(
5577 - 'href' => array(),
5578 - ),
5579 - )
5580 - )
5581 - );
5582 - }
5473 + return ( new Authorize_Json_Api() )->verify_json_api_authorization_request( $environment );
5583 5474 }
5584 5475
5585 5476 /**
5586 5477 * HTML for the JSON API authorization notice.
5587 5478 *
5479 + * @deprecated 13.4
5480 + *
5588 5481 * @return string
5589 5482 */
5590 5483 public function login_message_json_api_authorization() {
5591 - return '<p class="message">' . sprintf(
5592 - /* translators: Name/image of the client requesting authorization */
5593 - esc_html__( '%s wants to access your site’s data. Log in to authorize that access.', 'jetpack' ),
5594 - '<strong>' . esc_html( $this->json_api_authorization_request['client_title'] ) . '</strong>'
5595 - ) . '<img src="' . esc_url( $this->json_api_authorization_request['client_image'] ) . '" /></p>';
5484 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::login_message_json_api_authorization' );
5485 +
5486 + return ( new Authorize_Json_Api() )->login_message_json_api_authorization();
5596 5487 }
5597 5488
5598 5489 /**
5599 5490 * Get $content_width, but with a <s>twist</s> filter.
@@ -5637,32 +5528,23 @@
5637 5528
5638 5529 /**
5639 5530 * Checks if the site is currently in an identity crisis.
5640 5531 *
5532 + * Now delegates to the Connection package so this matches what the heartbeat itself reports.
5533 + * Note the package guards on `Connection\Manager::is_connected()` where this used to guard on
5534 + * `Jetpack::is_connection_ready()`, so the `jetpack_is_connection_ready` filter no longer applies.
5535 + *
5536 + * @deprecated 16.2
5537 + *
5641 5538 * @return array|bool Array of options that are in a crisis, or false if everything is OK.
5642 5539 */
5643 5540 public static function check_identity_crisis() {
5644 - if ( ! self::is_connection_ready() || ( new Status() )->is_offline_mode() || ! Identity_Crisis::validate_sync_error_idc_option() ) {
5645 - return false;
5646 - }
5541 + _deprecated_function( __METHOD__, 'jetpack-16.2', 'Automattic\\Jetpack\\Identity_Crisis::check_identity_crisis' );
5647 5542
5648 - return Jetpack_Options::get_option( 'sync_error_idc' );
5543 + return Identity_Crisis::check_identity_crisis();
5649 5544 }
5650 5545
5651 5546 /**
5652 - * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup.
5653 - *
5654 - * @since 4.4.0
5655 - * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC
5656 - *
5657 - * @return bool
5658 - */
5659 - public static function validate_sync_error_idc_option() {
5660 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\\Automattic\\Jetpack\\Identity_Crisis::validate_sync_error_idc_option' );
5661 - return Identity_Crisis::validate_sync_error_idc_option();
5662 - }
5663 -
5664 - /**
5665 5547 * Normalizes a url by doing three things:
5666 5548 * - Strips protocol
5667 5549 * - Strips www
5668 5550 * - Adds a trailing slash
@@ -5683,35 +5565,8 @@
5683 5565 return $url;
5684 5566 }
5685 5567
5686 5568 /**
5687 - * Gets the value that is to be saved in the jetpack_sync_error_idc option.
5688 - *
5689 - * @since 4.4.0
5690 - * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB
5691 - * @deprecated 9.8.0 Use \\Automattic\\Jetpack\\Identity_Crisis::get_sync_error_idc_option
5692 - *
5693 - * @param array $response HTTP response.
5694 - * @return array Array of the local urls, wpcom urls, and error code
5695 - */
5696 - public static function get_sync_error_idc_option( $response = array() ) {
5697 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\\Automattic\\Jetpack\\Identity_Crisis::get_sync_error_idc_option' );
5698 - return Identity_Crisis::get_sync_error_idc_option( $response );
5699 - }
5700 -
5701 - /**
5702 - * Returns the value of the jetpack_sync_idc_optin filter, or constant.
5703 - * If set to true, the site will be put into staging mode.
5704 - *
5705 - * @since 4.3.2
5706 - * @return bool
5707 - */
5708 - public static function sync_idc_optin() {
5709 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\\Automattic\\Jetpack\\Identity_Crisis::sync_idc_optin' );
5710 - return Identity_Crisis::sync_idc_optin();
5711 - }
5712 -
5713 - /**
5714 5569 * Maybe Use a .min.css stylesheet, maybe not.
5715 5570 *
5716 5571 * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args.
5717 5572 *
@@ -5732,9 +5587,9 @@
5732 5587 // Strip out the abspath.
5733 5588 $base = dirname( plugin_basename( $plugin ) );
5734 5589
5735 5590 // Short out on non-Jetpack assets.
5736 - if ( 'jetpack/' !== substr( $base, 0, 8 ) ) {
5591 + if ( ! str_starts_with( $base, 'jetpack/' ) ) {
5737 5592 return $url;
5738 5593 }
5739 5594
5740 5595 // File name parsing.
@@ -5774,15 +5629,15 @@
5774 5629 *
5775 5630 * @return mixed
5776 5631 */
5777 5632 public static function set_suffix_on_min( $src, $handle ) {
5778 - if ( false === strpos( $src, '.min.css' ) ) {
5633 + if ( ! is_string( $src ) || ! str_contains( $src, '.min.css' ) ) {
5779 5634 return $src;
5780 5635 }
5781 5636
5782 5637 if ( ! empty( self::$min_assets ) ) {
5783 5638 foreach ( self::$min_assets as $file => $path ) {
5784 - if ( false !== strpos( $src, $file ) ) {
5639 + if ( str_contains( $src, $file ) ) {
5785 5640 wp_style_add_data( $handle, 'suffix', '.min' );
5786 5641 return $src;
5787 5642 }
5788 5643 }
@@ -5812,8 +5667,10 @@
5812 5667 *
5813 5668 * Data passed in with the $data parameter will be available in the
5814 5669 * template file as $data['value']
5815 5670 *
5671 + * @html-template-var array $data
5672 + *
5816 5673 * @param string $template - Template file to load.
5817 5674 * @param array $data - Any data to pass along to the template.
5818 5675 * @return boolean - If template file was found.
5819 5676 **/
@@ -5831,8 +5688,19 @@
5831 5688 return false;
5832 5689 }
5833 5690
5834 5691 /**
5692 + * Register Jetpack-specific tests on the connection package's health test suite.
5693 + *
5694 + * @param \Automattic\Jetpack\Connection\Connection_Health_Tests $connection_tests The test suite instance.
5695 + */
5696 + public function register_jetpack_connection_tests( $connection_tests ) {
5697 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger/class-jetpack-cxn-tests.php';
5698 + $jetpack_tests = new Jetpack_Cxn_Tests();
5699 + $jetpack_tests->register_tests_on( $connection_tests );
5700 + }
5701 +
5702 + /**
5835 5703 * Throws warnings for deprecated hooks to be removed from Jetpack that cannot remain in the original place in the code.
5836 5704 */
5837 5705 public function deprecated_hooks() {
5838 5706 $filter_deprecated_list = array(
@@ -5959,13 +5827,8 @@
5959 5827 'jetpack_cache_plans' => array(
5960 5828 'replacement' => null,
5961 5829 'version' => 'jetpack-6.1.0',
5962 5830 ),
5963 -
5964 - 'jetpack_lazy_images_skip_image_with_atttributes' => array(
5965 - 'replacement' => 'jetpack_lazy_images_skip_image_with_attributes',
5966 - 'version' => 'jetpack-6.5.0',
5967 - ),
5968 5831 'jetpack_enable_site_verification' => array(
5969 5832 'replacement' => null,
5970 5833 'version' => 'jetpack-6.5.0',
5971 5834 ),
@@ -6049,8 +5912,22 @@
6049 5912 'jetpack_are_blogging_prompts_enabled' => array(
6050 5913 'replacement' => null,
6051 5914 'version' => 'jetpack-11.8.0',
6052 5915 ),
5916 + 'jetpack_subscriptions_modal_enabled' => array(
5917 + 'replacement' => null,
5918 + 'version' => 'jetpack-12.7.0',
5919 + ),
5920 + 'jetpack_pre_connection_prompt_helpers' => array(
5921 + 'replacement' => null,
5922 + 'version' => 'jetpack-13.2.0',
5923 + ),
5924 + 'jetpack_contact_form_use_package' => array(
5925 + 'replacement' => null,
5926 + 'version' => 'jetpack-13.4.0',
5927 + ),
5928 + // jetpack_implode_frontend_css has been removed, but is not listed here. The updated behavior is exactly the only use of the filter.
5929 + // We can reassess formally deprecating it here later; for now, it would be noise with no functional difference.
6053 5930 );
6054 5931
6055 5932 foreach ( $filter_deprecated_list as $tag => $args ) {
6056 5933 if ( has_filter( $tag ) ) {
@@ -6153,9 +6030,9 @@
6153 6030 foreach ( $matches as $match ) {
6154 6031 $url = trim( $match['path'], "'\" \t" );
6155 6032
6156 6033 // If this is a data url, we don't want to mess with it.
6157 - if ( 'data:' === substr( $url, 0, 5 ) ) {
6034 + if ( str_starts_with( $url, 'data:' ) ) {
6158 6035 continue;
6159 6036 }
6160 6037
6161 6038 // If this is an absolute or protocol-agnostic url,
@@ -6181,113 +6058,8 @@
6181 6058 return $css;
6182 6059 }
6183 6060
6184 6061 /**
6185 - * This methods removes all of the registered css files on the front end
6186 - * from Jetpack in favor of using a single file. In effect "imploding"
6187 - * all the files into one file.
6188 - *
6189 - * Pros:
6190 - * - Uses only ONE css asset connection instead of 15
6191 - * - Saves a minimum of 56k
6192 - * - Reduces server load
6193 - * - Reduces time to first painted byte
6194 - *
6195 - * Cons:
6196 - * - Loads css for ALL modules. However all selectors are prefixed so it
6197 - * should not cause any issues with themes.
6198 - * - Plugins/themes dequeuing styles no longer do anything. See
6199 - * jetpack_implode_frontend_css filter for a workaround
6200 - *
6201 - * For some situations developers may wish to disable css imploding and
6202 - * instead operate in legacy mode where each file loads seperately and
6203 - * can be edited individually or dequeued. This can be accomplished with
6204 - * the following line:
6205 - *
6206 - * add_filter( 'jetpack_implode_frontend_css', '__return_false' );
6207 - *
6208 - * @param bool $travis_test Is this a test run.
6209 - *
6210 - * @since 3.2
6211 - */
6212 - public function implode_frontend_css( $travis_test = false ) {
6213 - $do_implode = true;
6214 - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
6215 - $do_implode = false;
6216 - }
6217 -
6218 - // Do not implode CSS when the page loads via the AMP plugin.
6219 - if ( class_exists( Jetpack_AMP_Support::class ) && Jetpack_AMP_Support::is_amp_request() ) {
6220 - $do_implode = false;
6221 - }
6222 -
6223 - /**
6224 - * Allow CSS to be concatenated into a single jetpack.css file.
6225 - *
6226 - * @since 3.2.0
6227 - *
6228 - * @param bool $do_implode Should CSS be concatenated? Default to true.
6229 - */
6230 - $do_implode = apply_filters( 'jetpack_implode_frontend_css', $do_implode );
6231 -
6232 - // Do not use the imploded file when default behavior was altered through the filter.
6233 - if ( ! $do_implode ) {
6234 - return;
6235 - }
6236 -
6237 - // We do not want to use the imploded file in dev mode, or if not connected.
6238 - if ( ( new Status() )->is_offline_mode() || ! self::is_connection_ready() ) {
6239 - if ( ! $travis_test ) {
6240 - return;
6241 - }
6242 - }
6243 -
6244 - // Do not use the imploded file if sharing css was dequeued via the sharing settings screen.
6245 - if ( get_option( 'sharedaddy_disable_resources' ) ) {
6246 - return;
6247 - }
6248 -
6249 - /*
6250 - * Now we assume Jetpack is connected and able to serve the single
6251 - * file.
6252 - *
6253 - * In the future there will be a check here to serve the file locally
6254 - * or potentially from the Jetpack CDN
6255 - *
6256 - * For now:
6257 - * - Enqueue a single imploded css file
6258 - * - Zero out the style_loader_tag for the bundled ones
6259 - * - Be happy, drink scotch
6260 - */
6261 -
6262 - add_filter( 'style_loader_tag', array( $this, 'concat_remove_style_loader_tag' ), 10, 2 );
6263 -
6264 - $version = self::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . 'css/jetpack.css' ) : JETPACK__VERSION;
6265 -
6266 - wp_enqueue_style( 'jetpack_css', plugins_url( 'css/jetpack.css', __FILE__ ), array(), $version );
6267 - wp_style_add_data( 'jetpack_css', 'rtl', 'replace' );
6268 - }
6269 -
6270 - /**
6271 - * Removes styles that are part of concatenated group.
6272 - *
6273 - * @param string $tag Style tag.
6274 - * @param string $handle Style handle.
6275 - *
6276 - * @return string
6277 - */
6278 - public function concat_remove_style_loader_tag( $tag, $handle ) {
6279 - if ( in_array( $handle, $this->concatenated_style_handles, true ) ) {
6280 - $tag = '';
6281 - if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
6282 - $tag = '<!-- `' . esc_html( $handle ) . "` is included in the concatenated jetpack.css -->\r\n";
6283 - }
6284 - }
6285 -
6286 - return $tag;
6287 - }
6288 -
6289 - /**
6290 6062 * Check the heartbeat data
6291 6063 *
6292 6064 * Organizes the heartbeat data by severity. For example, if the site
6293 6065 * is in an ID crisis, it will be in the $filtered_data['bad'] array.
@@ -6299,9 +6071,23 @@
6299 6071 *
6300 6072 * $return array $filtered_data
6301 6073 */
6302 6074 public static function jetpack_check_heartbeat_data() {
6303 - $raw_data = Jetpack_Heartbeat::generate_stats_array();
6075 + /*
6076 + * Site environment stats (incl. wp-version/php-version checked below) now live in the Connection package,
6077 + * and the IDC stat is contributed by the Connection package's `jetpack_heartbeat_stats_array` filter
6078 + * callback. We rebuild the stat here rather than running that filter: the filter's callbacks have side
6079 + * effects (Connection\Manager::add_stats_to_heartbeat() consumes and deletes the `xmlrpc_errors` option)
6080 + * and return non-scalar values, neither of which is appropriate for this read-only diagnostic.
6081 + */
6082 + $env_stats = method_exists( Heartbeat::class, 'get_environment_stats' )
6083 + ? Heartbeat::get_environment_stats()
6084 + : array();
6085 + $raw_data = array_merge(
6086 + Jetpack_Heartbeat::generate_stats_array(),
6087 + $env_stats,
6088 + array( 'identitycrisis' => Identity_Crisis::check_identity_crisis() ? 'yes' : 'no' )
6089 + );
6304 6090
6305 6091 $good = array();
6306 6092 $caution = array();
6307 6093 $bad = array();
@@ -6367,23 +6153,8 @@
6367 6153 return Jetpack_Options::get_options_for_reset();
6368 6154 }
6369 6155
6370 6156 /**
6371 - * Strip http:// or https:// from a url, replaces forward slash with ::,
6372 - * so we can bring them directly to their site in calypso.
6373 - *
6374 - * @deprecated 9.2.0 Use Automattic\Jetpack\Status::get_site_suffix
6375 - *
6376 - * @param string $url URL.
6377 - * @return string url without the guff.
6378 - */
6379 - public static function build_raw_urls( $url ) {
6380 - _deprecated_function( __METHOD__, 'jetpack-9.2.0', 'Automattic\Jetpack\Status::get_site_suffix' );
6381 -
6382 - return ( new Status() )->get_site_suffix( $url );
6383 - }
6384 -
6385 - /**
6386 6157 * Get the user's meta box order.
6387 6158 *
6388 6159 * @param array $sorted Value for the user's option.
6389 6160 * @return mixed
@@ -6393,9 +6164,9 @@
6393 6164 return $sorted;
6394 6165 }
6395 6166
6396 6167 foreach ( $sorted as $box_context => $ids ) {
6397 - if ( false === strpos( $ids, 'dashboard_stats' ) ) {
6168 + if ( ! str_contains( $ids, 'dashboard_stats' ) ) {
6398 6169 // If the old id isn't anywhere in the ids, don't bother exploding and fail out.
6399 6170 continue;
6400 6171 }
6401 6172
@@ -6412,68 +6183,9 @@
6412 6183 }
6413 6184
6414 6185 return $sorted;
6415 6186 }
6416 -
6417 6187 /**
6418 - * Adds a "blank" column in the user admin table to display indication of user connection.
6419 - *
6420 - * @param array $columns User list table columns.
6421 - *
6422 - * @return array
6423 - */
6424 - public function jetpack_icon_user_connected( $columns ) {
6425 - $columns['user_jetpack'] = '';
6426 - return $columns;
6427 - }
6428 -
6429 - /**
6430 - * Show Jetpack icon if the user is linked.
6431 - *
6432 - * @param string $val HTML for the icon.
6433 - * @param string $col User list table column.
6434 - * @param int $user_id User ID.
6435 - *
6436 - * @return string
6437 - */
6438 - public function jetpack_show_user_connected_icon( $val, $col, $user_id ) {
6439 - if ( 'user_jetpack' === $col && self::connection()->is_user_connected( $user_id ) ) {
6440 - $jetpack_logo = new Jetpack_Logo();
6441 - $emblem_html = sprintf(
6442 - '<a title="%1$s" class="jp-emblem-user-admin">%2$s</a>',
6443 - esc_attr__( 'This user is linked and ready to fly with Jetpack.', 'jetpack' ),
6444 - $jetpack_logo->get_jp_emblem()
6445 - );
6446 - return $emblem_html;
6447 - }
6448 -
6449 - return $val;
6450 - }
6451 -
6452 - /**
6453 - * Style the Jetpack user column
6454 - */
6455 - public function jetpack_user_col_style() {
6456 - global $current_screen;
6457 - if ( ! empty( $current_screen->base ) && 'users' === $current_screen->base ) {
6458 - ?>
6459 - <style>
6460 - .fixed .column-user_jetpack {
6461 - width: 21px;
6462 - }
6463 - .jp-emblem-user-admin svg {
6464 - width: 20px;
6465 - height: 20px;
6466 - }
6467 - .jp-emblem-user-admin path {
6468 - fill: #069e08;
6469 - }
6470 - </style>
6471 - <?php
6472 - }
6473 - }
6474 -
6475 - /**
6476 6188 * Checks if Akismet is active and working.
6477 6189 *
6478 6190 * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key
6479 6191 * that implied usage of methods present since more recent version.
@@ -6565,26 +6277,15 @@
6565 6277 /**
6566 6278 * Return Calypso environment value; used for developing Jetpack and pairing
6567 6279 * it with different Calypso enrionments, such as localhost.
6568 6280 *
6569 - * @since 7.4.0
6281 + * @deprecated 12.4 Moved to the Status package.
6570 6282 *
6571 6283 * @return string Calypso environment
6572 6284 */
6573 6285 public static function get_calypso_env() {
6574 - if ( isset( $_GET['calypso_env'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required; only used for changing environments.
6575 - return sanitize_key( $_GET['calypso_env'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is not required; only used for changing environments.
6576 - }
6577 -
6578 - if ( getenv( 'CALYPSO_ENV' ) ) {
6579 - return sanitize_key( getenv( 'CALYPSO_ENV' ) );
6580 - }
6581 -
6582 - if ( defined( 'CALYPSO_ENV' ) && CALYPSO_ENV ) {
6583 - return sanitize_key( CALYPSO_ENV );
6584 - }
6585 -
6586 - return '';
6286 + _deprecated_function( __METHOD__, 'jetpack-12.4', '\Automattic\Jetpack\Status\Host->get_calypso_env' );
6287 + return ( new Host() )->get_calypso_env();
6587 6288 }
6588 6289
6589 6290 /**
6590 6291 * Returns the hostname with protocol for Calypso.
@@ -6594,9 +6295,9 @@
6594 6295 *
6595 6296 * @return string Calypso host.
6596 6297 */
6597 6298 public static function get_calypso_host() {
6598 - $calypso_env = self::get_calypso_env();
6299 + $calypso_env = ( new Host() )->get_calypso_env();
6599 6300 switch ( $calypso_env ) {
6600 6301 case 'development':
6601 6302 return 'http://calypso.localhost:3000/';
6602 6303 case 'wpcalypso':
@@ -6645,13 +6346,20 @@
6645 6346 }
6646 6347 }
6647 6348
6648 6349 /**
6649 - * Returns a boolean for whether backups UI should be displayed or not.
6350 + * Whether UI for backups should be displayed.
6650 6351 *
6352 + * On WPCom platforms this is gated on the backups-self-serve site feature.
6353 + * On self-hosted Jetpack sites it falls back to the jetpack_show_backups filter.
6354 + *
6651 6355 * @return bool Should backups UI be displayed?
6652 6356 */
6653 6357 public static function show_backups_ui() {
6358 + if ( ( new \Automattic\Jetpack\Status\Host() )->is_wpcom_platform() ) {
6359 + return function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'backups-self-serve' );
6360 + }
6361 +
6654 6362 /**
6655 6363 * Whether UI for backups should be displayed.
6656 6364 *
6657 6365 * @since 6.5.0
@@ -6661,8 +6369,24 @@
6661 6369 return self::is_plugin_active( 'vaultpress/vaultpress.php' ) || apply_filters( 'jetpack_show_backups', true );
6662 6370 }
6663 6371
6664 6372 /**
6373 + * Whether UI for security scanning should be displayed.
6374 + *
6375 + * On WPCom platforms this is gated on the scan-self-serve site feature.
6376 + * On self-hosted Jetpack sites it always returns true.
6377 + *
6378 + * @return bool Should scan UI be displayed?
6379 + */
6380 + public static function show_scan_ui() {
6381 + if ( ( new \Automattic\Jetpack\Status\Host() )->is_wpcom_platform() ) {
6382 + return function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'scan-self-serve' );
6383 + }
6384 +
6385 + return true;
6386 + }
6387 +
6388 + /**
6665 6389 * Clean leftoveruser meta.
6666 6390 *
6667 6391 * Delete Jetpack-related user meta when it is no longer needed.
6668 6392 *
@@ -6734,8 +6458,40 @@
6734 6458 'url' => Redirect::get_url( 'jetpack-faq-backup-disclaimer' ),
6735 6459 ),
6736 6460 );
6737 6461
6462 + $products['creator'] = array(
6463 + 'title' => __( 'Jetpack Creator', 'jetpack' ),
6464 + 'slug' => 'jetpack_creator_yearly',
6465 + 'description' => __( 'Craft stunning content, boost your subscriber base, and monetize your online presence.', 'jetpack' ),
6466 + 'show_promotion' => true,
6467 + 'discount_percent' => 50,
6468 + 'included_in_plans' => array( 'complete' ),
6469 + 'features' => array(
6470 + _x( 'Unlimited subscriber imports', 'Creator Product Feature', 'jetpack' ),
6471 + _x( 'Earn more from your content', 'Creator Product Feature', 'jetpack' ),
6472 + _x( 'Accept payments with PayPal', 'Creator Product Feature', 'jetpack' ),
6473 + _x( 'Increase earnings with WordAds', 'Creator Product Feature', 'jetpack' ),
6474 + ),
6475 + );
6476 +
6477 + $products['growth'] = array(
6478 + 'title' => __( 'Jetpack Growth', 'jetpack' ),
6479 + 'slug' => 'jetpack_growth_yearly',
6480 + 'description' => __( 'Essential tools to help you grow your audience, track visitor engagement, and turn leads into loyal customers and advocates.', 'jetpack' ),
6481 + 'show_promotion' => true,
6482 + 'discount_percent' => 50,
6483 + 'included_in_plans' => array( 'complete' ),
6484 + 'features' => array(
6485 + _x( 'Jetpack Social', 'Growth Product Feature', 'jetpack' ),
6486 + _x( 'Jetpack Stats (10K site views, upgradeable)', 'Growth Product Feature', 'jetpack' ),
6487 + _x( 'Unlimited subscriber imports', 'Growth Product Feature', 'jetpack' ),
6488 + _x( 'Earn more from your content', 'Growth Product Feature', 'jetpack' ),
6489 + _x( 'Accept payments with PayPal', 'Growth Product Feature', 'jetpack' ),
6490 + _x( 'Increase earnings with WordAds', 'Growth Product Feature', 'jetpack' ),
6491 + ),
6492 + );
6493 +
6738 6494 $products['scan'] = array(
6739 6495 'title' => __( 'Jetpack Scan', 'jetpack' ),
6740 6496 'slug' => 'jetpack_scan',
6741 6497 'description' => __( 'Automatic scanning and one-click fixes keep your site one step ahead of security threats and malware.', 'jetpack' ),
@@ -6765,9 +6521,9 @@
6765 6521 ),
6766 6522 );
6767 6523
6768 6524 $products['akismet'] = array(
6769 - 'title' => __( 'Akismet Anti-Spam', 'jetpack' ),
6525 + 'title' => __( 'Akismet Anti-spam', 'jetpack' ),
6770 6526 'slug' => 'jetpack_anti_spam',
6771 6527 'description' => __( 'Save time and get better responses by automatically blocking spam from your comments and forms.', 'jetpack' ),
6772 6528 'show_promotion' => true,
6773 6529 'discount_percent' => 50,
@@ -6861,5 +6617,106 @@
6861 6617
6862 6618 return true;
6863 6619 }
6864 6620
6621 + /**
6622 + * Add 5-star review link on the Jetpack Plugin meta, in the plugins list table (on the plugins page).
6623 + *
6624 + * @param array $plugin_meta An array of the plugin's metadata.
6625 + * @param string $plugin_file Path to the plugin file.
6626 + *
6627 + * @return array $plugin_meta An array of the plugin's metadata.
6628 + */
6629 + public function add_5_star_review_link( $plugin_meta, $plugin_file ) {
6630 + if ( $plugin_file !== 'jetpack/jetpack.php' ) {
6631 + return $plugin_meta;
6632 + }
6633 +
6634 + $u = get_current_user_id();
6635 + $site = get_site_url();
6636 +
6637 + $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">'
6638 + . str_repeat( '<span class="dashicons dashicons-star-filled" style="font-size: 16px; width:16px; height: 16px"></span>', 5 )
6639 + . '</a>';
6640 +
6641 + return $plugin_meta;
6642 + }
6643 +
6644 + /**
6645 + * Lazy instantiation of the Plugin_Tracking object.
6646 + *
6647 + * @since 13.9
6648 + *
6649 + * @return void
6650 + */
6651 + public function initialize_tracking() {
6652 + if ( did_action( 'jetpack_initialize_tracking' ) > 1 ) {
6653 + // Only need to run once.
6654 + return;
6655 + }
6656 +
6657 + if ( ( new Tracking( 'jetpack', $this->connection_manager ) )->should_enable_tracking( new Terms_Of_Service(), new Status() ) || static::is_connection_ready() ) {
6658 + ( new Plugin_Tracking() )->init();
6659 + }
6660 + }
6661 +
6662 + /**
6663 + * Run the "initialize tracking" hook.
6664 + *
6665 + * @since 13.9
6666 + */
6667 + public function run_initialize_tracking_action() {
6668 + /**
6669 + * Fires when the tracking needs to be initialized.
6670 + * Doesn't necessarily mean that will actually happen, depends if the 'jetpack_tos_agreed' option is set.
6671 + *
6672 + * @since 13.9
6673 + */
6674 + do_action( 'jetpack_initialize_tracking' );
6675 + }
6676 +
6677 + /**
6678 + * Initialize REST jsonAPI if needed.
6679 + *
6680 + * @return void
6681 + */
6682 + public function maybe_initialize_rest_jsonapi() {
6683 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
6684 + if ( ! empty( $_GET['jsonapi'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) ) {
6685 + require_once ABSPATH . 'wp-admin/includes/admin.php'; // JSON API relies on WP functionality not autoloaded in REST.
6686 +
6687 + define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
6688 + require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
6689 + }
6690 + }
6691 +
6692 + /**
6693 + * Run plugin post-activation actions if we need to.
6694 + *
6695 + * @return void
6696 + */
6697 + private function plugin_post_activation() {
6698 + if ( ( new Status() )->is_offline_mode() ) {
6699 + return;
6700 + }
6701 +
6702 + if ( get_transient( 'activated_jetpack' ) ) {
6703 + delete_transient( 'activated_jetpack' );
6704 +
6705 + if ( ( new Host() )->is_woa_site() ) {
6706 + $redirect_url = static::admin_url( 'page=jetpack' );
6707 + } elseif ( is_network_admin() ) {
6708 + $redirect_url = admin_url( 'network/admin.php?page=jetpack' );
6709 + } elseif ( get_transient( 'my_jetpack_product_activated' ) ) {
6710 + // don't redirect if this is an activation that just came from My Jetpack
6711 + // My Jetpack has its own set of post-activation redirects
6712 + return;
6713 + } elseif ( My_Jetpack_Initializer::should_initialize() ) {
6714 + $redirect_url = static::admin_url( 'page=my-jetpack' );
6715 + } else {
6716 + $redirect_url = static::admin_url( 'page=jetpack' );
6717 + }
6718 +
6719 + wp_safe_redirect( $redirect_url );
6720 + }
6721 + }
6865 6722 }