PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
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 13.8.3 All 506 releases
← All changes | class.jetpack.php +1009 -1155 12.8.3 → 16.3-a.7 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,49 +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 - 'subscribe-modal-css',
119 - );
120 -
121 - /**
122 91 * Contains all assets that have had their URL rewritten to minified versions.
123 92 *
124 93 * @var array
125 94 */
@@ -134,11 +103,8 @@
134 103 'contact-form' => array(
135 104 array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
136 105 array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
137 106 ),
138 - 'custom-css' => array(
139 - array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
140 - ),
141 107 'gravatar-hovercards' => array(
142 108 array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
143 109 ),
144 110 'latex' => array(
@@ -309,9 +275,8 @@
309 275 * Graph tags via filter when their Social Meta modules are active:
310 276 *
311 277 * - All in One SEO Pack, All in one SEO Pack Pro
312 278 * - WordPress SEO by Yoast, WordPress SEO Premium by Yoast
313 - * - SEOPress, SEOPress Pro
314 279 *
315 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:
316 281 * add_filter( 'jetpack_enable_open_graph', '__return_false' );
317 282 *
@@ -354,8 +319,10 @@
354 319 'wp-caregiver/wp-caregiver.php', // WP Caregiver.
355 320 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php', // WP Facebook Like Send & Open Graph Meta.
356 321 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol.
357 322 'wp-ogp/wp-ogp.php', // WP-OGP.
323 + 'wp-seopress/seopress.php', // SEOPress.
324 + 'wp-seopress-pro/seopress-pro.php', // SEOPress Pro.
358 325 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin.
359 326 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button.
360 327 'open-graph-metabox/open-graph-metabox.php', // Open Graph Metabox.
361 328 'seo-by-rank-math/rank-math.php', // Rank Math.
@@ -422,8 +389,10 @@
422 389
423 390 /**
424 391 * Verified data for JSON authorization request
425 392 *
393 + * @deprecated 13.4
394 + *
426 395 * @var array
427 396 */
428 397 public $json_api_authorization_request = array();
429 398
@@ -464,8 +433,16 @@
464 433 */
465 434 public static $instance = false;
466 435
467 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 + /**
468 445 * Singleton
469 446 *
470 447 * @static
471 448 */
@@ -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,41 +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 - /**
690 - * We've switched from enqueue_block_editor_assets to enqueue_block_assets in WP-Admin because the assets with the former are loaded on the main site-editor.php.
691 - *
692 - * With the latter, the assets are now loaded in the SE iframe; the implementation is now faster because Gutenberg doesn't need to inject the assets in the iframe on client-side.
693 - */
694 - if ( is_admin() ) {
695 - add_action( 'enqueue_block_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
696 - } else {
697 - add_action( 'enqueue_block_editor_assets', array( 'Jetpack_Gutenberg', 'enqueue_block_editor_assets' ) );
698 - }
699 - add_filter( 'render_block', array( 'Jetpack_Gutenberg', 'display_deprecated_block_message' ), 10, 2 );
700 -
701 665 add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );
702 666
703 - // Unlink user before deleting the user from WP.com.
704 - add_action( 'deleted_user', array( $this, 'disconnect_user' ), 10, 1 );
705 - add_action( 'remove_user_from_blog', array( $this, 'disconnect_user' ), 10, 1 );
706 -
707 667 add_action( 'jetpack_event_log', array( 'Jetpack', 'log' ), 10, 2 );
708 668
709 669 add_filter( 'login_url', array( $this, 'login_url' ), 10, 2 );
710 670 add_action( 'login_init', array( $this, 'login_init' ) );
@@ -711,8 +671,13 @@
711 671
712 672 // Set up the REST authentication hooks.
713 673 Connection_Rest_Authentication::init();
714 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 +
715 680 add_action( 'admin_init', array( $this, 'admin_init' ) );
716 681 add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
717 682
718 683 add_filter( 'admin_body_class', array( $this, 'admin_body_class' ), 20 );
@@ -726,12 +691,8 @@
726 691
727 692 // Returns HTTPS support status.
728 693 add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) );
729 694
730 - add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) );
731 -
732 - add_action( 'wp_ajax_jetpack_recommendations_banner', array( 'Jetpack_Recommendations_Banner', 'ajax_callback' ) );
733 -
734 695 add_action( 'wp_loaded', array( $this, 'register_assets' ) );
735 696
736 697 /**
737 698 * These actions run checks to load additional files.
@@ -749,29 +710,8 @@
749 710
750 711 add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
751 712 add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
752 713
753 - require_once JETPACK__PLUGIN_DIR . 'class-jetpack-pre-connection-jitms.php';
754 - $jetpack_jitm_messages = ( new Jetpack_Pre_Connection_JITMs() );
755 - add_filter( 'jetpack_pre_connection_jitms', array( $jetpack_jitm_messages, 'add_pre_connection_jitms' ) );
756 -
757 - /*
758 - * If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin.
759 - * We should make sure to only do this for front end links.
760 - */
761 - if ( self::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) {
762 - add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 );
763 - add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 );
764 -
765 - /*
766 - * We'll shortcircuit wp_notify_postauthor and wp_notify_moderator pluggable functions
767 - * so they point moderation links on emails to Calypso.
768 - */
769 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/functions.wp-notify.php';
770 - add_filter( 'comment_notification_recipients', 'jetpack_notify_postauthor', 1, 2 );
771 - add_filter( 'notify_moderator', 'jetpack_notify_moderator', 1, 2 );
772 - }
773 -
774 714 add_action(
775 715 'plugins_loaded',
776 716 function () {
777 717 if ( User_Agent_Info::is_mobile_app() ) {
@@ -782,18 +722,10 @@
782 722
783 723 // Update the site's Jetpack plan and products from API on heartbeats.
784 724 add_action( 'jetpack_heartbeat', array( Jetpack_Plan::class, 'refresh_from_wpcom' ) );
785 725
786 - /**
787 - * This is the hack to concatenate all css files into one.
788 - * For description and reasoning see the implode_frontend_css method.
789 - *
790 - * Super late priority so we catch all the registered styles.
791 - */
792 - if ( ! is_admin() ) {
793 - add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first.
794 - add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles`.
795 - }
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' ) );
796 728
797 729 // Actually push the stats on shutdown.
798 730 if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
799 731 add_action( 'shutdown', array( $this, 'push_stats' ) );
@@ -801,8 +733,10 @@
801 733
802 734 // After a successful connection.
803 735 add_action( 'jetpack_site_registered', array( $this, 'activate_default_modules_on_site_register' ) );
804 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' ) );
805 739
806 740 // Actions for Manager::authorize().
807 741 add_action( 'jetpack_authorize_starting', array( $this, 'authorize_starting' ) );
808 742 add_action( 'jetpack_authorize_ending_linked', array( $this, 'authorize_ending_linked' ) );
@@ -807,8 +741,9 @@
807 741 add_action( 'jetpack_authorize_starting', array( $this, 'authorize_starting' ) );
808 742 add_action( 'jetpack_authorize_ending_linked', array( $this, 'authorize_ending_linked' ) );
809 743 add_action( 'jetpack_authorize_ending_authorized', array( $this, 'authorize_ending_authorized' ) );
810 744
745 + Jetpack_Client_Server::init();
811 746 add_action( 'jetpack_client_authorize_error', array( Jetpack_Client_Server::class, 'client_authorize_error' ) );
812 747 add_filter( 'jetpack_client_authorize_already_authorized_url', array( Jetpack_Client_Server::class, 'client_authorize_already_authorized_url' ) );
813 748 add_action( 'jetpack_client_authorize_processing', array( Jetpack_Client_Server::class, 'client_authorize_processing' ) );
814 749 add_filter( 'jetpack_client_authorize_fallback_url', array( Jetpack_Client_Server::class, 'client_authorize_fallback_url' ) );
@@ -813,9 +748,9 @@
813 748 add_action( 'jetpack_client_authorize_processing', array( Jetpack_Client_Server::class, 'client_authorize_processing' ) );
814 749 add_filter( 'jetpack_client_authorize_fallback_url', array( Jetpack_Client_Server::class, 'client_authorize_fallback_url' ) );
815 750
816 751 // Filters for the Manager::get_token() urls and request body.
817 - 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' ) );
818 753 add_filter( 'jetpack_token_request_body', array( __CLASS__, 'filter_token_request_body' ) );
819 754
820 755 // Filter for the `jetpack/v4/connection/data` API response.
821 756 add_filter( 'jetpack_current_user_connection_data', array( __CLASS__, 'filter_jetpack_current_user_connection_data' ) );
@@ -841,13 +776,180 @@
841 776
842 777 // Register product descriptions for partner coupon usage.
843 778 add_filter( 'jetpack_partner_coupon_products', array( $this, 'get_partner_coupon_product_descriptions' ) );
844 779
845 - // Actions for conditional recommendations.
846 - 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();
847 791 }
848 792
849 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 + /**
857 + * Filters whether the Jetpack plugin offers its bundled Backup dashboard.
858 + *
859 + * Resolved at the earliest `plugins_loaded` priority, so hook it from a mu-plugin.
860 + *
861 + * @since 16.3
862 + *
863 + * @param bool $enabled Whether to initialize the bundled Backup dashboard. Default true.
864 + */
865 + if ( ! apply_filters( 'jetpack_backup_dashboard_enabled', true ) ) {
866 + return;
867 + }
868 +
869 + $backup = 'Automattic\\Jetpack\\Backup\\V0005\\Jetpack_Backup';
870 +
871 + // An older package would take over the connection (see Jetpack_Backup::DEFAULT_INIT_OPTIONS);
872 + // only the standalone plugin ships one that old, and it draws its own menu.
873 + if ( ! class_exists( $backup ) || ! defined( $backup . '::DEFAULT_INIT_OPTIONS' ) ) {
874 + return;
875 + }
876 +
877 + $backup::initialize( array( 'manage_connection' => false ) );
878 + }
879 +
880 + /**
881 + * Whether the bundled Stats v2 dashboard is enabled.
882 + *
883 + * Stats v2 (formerly "Premium Analytics") ships with the plugin behind this
884 + * flag while it rolls out (WOOA7S-1595). When enabled it adds its own admin
885 + * menu alongside the existing Stats UI; it never replaces or hides the
886 + * legacy Stats menu, admin-bar entries, post-list column, or WP dashboard
887 + * widget. The Stats module's tracking is unaffected either way, and Stats v2
888 + * reads what that module collects, so while the module is off the plugin
889 + * answers false here before even reading the flag.
890 + *
891 + * The package has to be loadable for this to be true, so a site with the
892 + * flag on but a missing package answers false here and never adds the
893 + * Stats v2 menu (a warning is logged instead).
894 + *
895 + * @since 16.1
896 + *
897 + * @return bool
898 + */
899 + public static function is_premium_analytics_enabled() {
900 + if ( null !== self::$premium_analytics_enabled ) {
901 + return self::$premium_analytics_enabled;
902 + }
903 +
904 + if ( ! self::is_module_active( 'stats' ) ) {
905 + self::$premium_analytics_enabled = false;
906 + return false;
907 + }
908 +
909 + /**
910 + * Filters whether the bundled Premium Analytics dashboard is enabled.
911 + *
912 + * Resolved once, from `Jetpack::configure()` on `plugins_loaded`, and only
913 + * while the Stats module is active. Register this from a mu-plugin or a
914 + * plugin's main file — a callback added on `plugins_loaded` or later runs
915 + * too late to be seen.
916 + *
917 + * @since 16.1
918 + *
919 + * @param bool $enabled Defaults to the `jetpack_premium_analytics_enabled` option (false).
920 + */
921 + $flag = (bool) apply_filters( 'jetpack_premium_analytics_enabled', (bool) get_option( 'jetpack_premium_analytics_enabled' ) );
922 +
923 + self::$premium_analytics_enabled = $flag && class_exists( 'Automattic\Jetpack\PremiumAnalytics\Analytics' );
924 +
925 + if ( $flag && ! self::$premium_analytics_enabled ) {
926 + wp_trigger_error(
927 + __METHOD__,
928 + 'The jetpack_premium_analytics_enabled flag is on but the Premium Analytics package is not loadable; keeping the Stats UI in place.'
929 + );
930 + }
931 +
932 + return self::$premium_analytics_enabled;
933 + }
934 +
935 + /**
936 + * Expose the setting that turns the Premium Analytics dashboard on and off.
937 + *
938 + * Deliberately not behind is_premium_analytics_enabled(): this is the setting that flips that
939 + * check, so it has to answer while the dashboard is still off.
940 + *
941 + * @since 16.2
942 + *
943 + * @return void
944 + */
945 + public static function register_premium_analytics_enablement_setting() {
946 + if ( class_exists( 'Automattic\Jetpack\PremiumAnalytics\Enablement_Setting' ) ) {
947 + \Automattic\Jetpack\PremiumAnalytics\Enablement_Setting::register();
948 + }
949 + }
950 +
951 + /**
850 952 * Before everything else starts getting initalized, we need to initialize Jetpack using the
851 953 * Config object.
852 954 */
853 955 public function configure() {
@@ -856,13 +958,10 @@
856 958 foreach (
857 959 array(
858 960 'jitm',
859 961 'sync',
962 + 'account_protection',
860 963 'waf',
861 - 'videopress',
862 - 'stats',
863 - 'stats_admin',
864 - 'import',
865 964 )
866 965 as $feature
867 966 ) {
868 967 $config->ensure( $feature );
@@ -867,8 +966,110 @@
867 966 ) {
868 967 $config->ensure( $feature );
869 968 }
870 969
970 + // Enable the VideoPress admin UI (the "Jetpack > VideoPress" dashboard) inside the
971 + // Jetpack plugin, mirroring the standalone Jetpack VideoPress plugin. The dashboard
972 + // only renders when the VideoPress module is active (Status::is_active()); when it
973 + // is not, the menu item links to the My Jetpack interstitial to activate it.
974 + $config->ensure( 'videopress', array( 'admin_ui' => true ) );
975 +
976 + // The Backup dashboard is only an admin menu and REST routes, so it is deferred like Import below.
977 + if ( self::should_eager_load_packages() ) {
978 + self::configure_backup_package();
979 + } else {
980 + add_action( 'rest_api_init', array( __CLASS__, 'configure_backup_package' ), 0 );
981 + }
982 +
983 + /*
984 + * The Import package only registers `jetpack/v4/import` REST routes — it
985 + * does nothing when rendering a front-end page — so gate its `ensure()`
986 + * to keep its PHP out of opcache on the front-end GET hot path. It still
987 + * loads on admin, cron, POST, and WP-CLI requests, and on `rest_api_init`
988 + * for REST: a REST request can't be identified yet at `plugins_loaded`
989 + * (this runs before `Config::on_plugins_loaded`, and `rest_api_init`
990 + * fires later), so it is initialized directly when that hook fires, while
991 + * a plain page view never fires it and so loads nothing.
992 + *
993 + * JITM stays eager (above): unlike Import, its `register()` adds a
994 + * `jetpack_sync_before_send_updated_option` filter that records the
995 + * `jetpack_last_plugin_sync` transient, and a Jetpack Sync send can fire
996 + * on a plain front-end GET — including the dedicated-sync `spawn-sync`
997 + * GET, which runs on `init` and exits before `rest_api_init`. Deferring
998 + * JITM would skip that bookkeeping and leave its message cache stale after
999 + * a plugin change, so it loads on every request as before.
1000 + */
1001 + if ( self::should_eager_load_packages() ) {
1002 + $config->ensure( 'import' );
1003 + } else {
1004 + add_action(
1005 + 'rest_api_init',
1006 + array( __CLASS__, 'configure_import_package' ),
1007 + 0
1008 + );
1009 + }
1010 +
1011 + /*
1012 + * The Stats and Stats Admin packages only do work when the Stats module
1013 + * is active (the front-end tracking pixel) or on wp-admin, REST, cron,
1014 + * POST, and WP-CLI requests: the Stats dashboard page, the stats /
1015 + * stats-app REST endpoints (which the block editor also calls for
1016 + * email-open rates), the transient-cleanup cron, the connection
1017 + * package's package-version tracker (which runs on POSTs and reads the
1018 + * `jetpack_package_versions` filter that Stats registers), and CLI
1019 + * introspection such as the heartbeat inspector. On a plain front-end
1020 + * GET page view with the module off they are inert: the pixel
1021 + * short-circuits on `Stats\Main::should_track()` and every other entry
1022 + * point only hooks rest_api_init, admin, cron, the POST-only tracker, or
1023 + * is reached through WP-CLI.
1024 + * Skip loading them — and eagerly constructing the Stats Admin REST
1025 + * controller — on that hot path to keep their PHP out of opcache, but
1026 + * keep loading them everywhere else exactly as before so the stats REST
1027 + * permission mapping (view_stats, registered by Stats\Main), the
1028 + * editor's stats-app calls, the cleanup cron, and the package-version
1029 + * tracker are all unchanged.
1030 + *
1031 + * REST requests are not yet identifiable here (REST_REQUEST is defined
1032 + * after plugins_loaded), so defer those to rest_api_init. Call the
1033 + * package initializers directly rather than `$config->ensure()`: ensure()
1034 + * only flags a feature for `Config::on_plugins_loaded()` (plugins_loaded
1035 + * priority 2), which has already run by the time rest_api_init fires.
1036 + * Priority 0 runs before each package's own priority-10 route
1037 + * registration, so their routes still register within the same dispatch.
1038 + * A plain page view never fires rest_api_init, so the packages stay
1039 + * unloaded there. See JETPACK-1747.
1040 + */
1041 + $is_post_request = isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'];
1042 + $is_wp_cli = defined( 'WP_CLI' ) && WP_CLI;
1043 +
1044 + if ( self::is_module_active( 'stats' ) || is_admin() || wp_doing_cron() || $is_post_request || $is_wp_cli ) {
1045 + $config->ensure( 'stats' );
1046 + $config->ensure( 'stats_admin' );
1047 + } else {
1048 + add_action(
1049 + 'rest_api_init',
1050 + static function () {
1051 + if ( class_exists( 'Automattic\Jetpack\Stats\Main' ) ) {
1052 + \Automattic\Jetpack\Stats\Main::init();
1053 + }
1054 + if ( class_exists( 'Automattic\Jetpack\Stats_Admin\Main' ) ) {
1055 + \Automattic\Jetpack\Stats_Admin\Main::init();
1056 + }
1057 + },
1058 + 0
1059 + );
1060 + }
1061 +
1062 + // Stats v2 (WOOA7S-1595). Unlike Stats above it cannot be deferred when enabled — see
1063 + // Analytics::init() for why, and for why it takes no menu_title here.
1064 + if ( self::is_premium_analytics_enabled() ) {
1065 + \Automattic\Jetpack\PremiumAnalytics\Analytics::init();
1066 + }
1067 +
1068 + // Outside the check above on purpose — see Enablement_Setting. Deferred like Stats, to keep
1069 + // the autoload off the front-end hot path.
1070 + add_action( 'rest_api_init', array( __CLASS__, 'register_premium_analytics_enablement_setting' ), 0 );
1071 +
871 1072 $config->ensure(
872 1073 'connection',
873 1074 array(
874 1075 'slug' => 'jetpack',
@@ -886,12 +1087,8 @@
886 1087 );
887 1088
888 1089 $config->ensure( 'search' );
889 1090
890 - if ( defined( 'ENABLE_WORDADS_SHARED_UI' ) && ENABLE_WORDADS_SHARED_UI ) {
891 - $config->ensure( 'wordads' );
892 - }
893 -
894 1091 if ( ! $this->connection_manager ) {
895 1092 $this->connection_manager = new Connection_Manager( 'jetpack' );
896 1093 }
897 1094
@@ -899,8 +1096,10 @@
899 1096 if ( $modules->is_active( 'publicize' ) && $this->connection_manager->has_connected_user() ) {
900 1097 $config->ensure( 'publicize' );
901 1098 }
902 1099
1100 + add_action( 'jetpack_initialize_tracking', array( $this, 'initialize_tracking' ) );
1101 +
903 1102 /*
904 1103 * Load things that should only be in Network Admin.
905 1104 *
906 1105 * For now blow away everything else until a more full
@@ -915,8 +1114,9 @@
915 1114 $is_connection_ready = self::is_connection_ready();
916 1115
917 1116 if ( $is_connection_ready ) {
918 1117 add_action( 'login_form_jetpack_json_api_authorization', array( $this, 'login_form_json_api_authorization' ) );
1118 + $this->run_initialize_tracking_action();
919 1119
920 1120 Jetpack_Heartbeat::init();
921 1121 if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) {
922 1122 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
@@ -921,8 +1121,19 @@
921 1121 if ( self::is_module_active( 'stats' ) && self::is_module_active( 'search' ) ) {
922 1122 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
923 1123 Jetpack_Search_Performance_Logger::init();
924 1124 }
1125 + } else {
1126 + add_action( 'jetpack_agreed_to_terms_of_service', array( $this, 'run_initialize_tracking_action' ) );
1127 + add_action( 'rest_api_init', array( $this, 'run_initialize_tracking_action' ) );
1128 + add_filter(
1129 + 'xmlrpc_methods',
1130 + function ( $methods ) {
1131 + $this->run_initialize_tracking_action();
1132 + return $methods;
1133 + },
1134 + 1
1135 + );
925 1136 }
926 1137
927 1138 // Initialize remote file upload request handlers.
928 1139 $this->add_remote_request_handlers();
@@ -932,20 +1143,10 @@
932 1143 */
933 1144 if ( $is_connection_ready ) {
934 1145 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
935 1146 add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );
936 - require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-keyring-service-helper.php';
937 - add_action( 'init', array( 'Jetpack_Keyring_Service_Helper', 'init' ), 9, 0 );
1147 + add_action( 'rest_api_init', array( $this, 'maybe_initialize_rest_jsonapi' ) );
938 1148 }
939 -
940 - if ( ( new Tracking( 'jetpack', $this->connection_manager ) )->should_enable_tracking( new Terms_Of_Service(), new Status() ) ) {
941 - add_action( 'init', array( new Plugin_Tracking(), 'init' ) );
942 - } else {
943 - /**
944 - * Initialize tracking right after the user agrees to the terms of service.
945 - */
946 - add_action( 'jetpack_agreed_to_terms_of_service', array( new Plugin_Tracking(), 'init' ) );
947 - }
948 1149 }
949 1150
950 1151 /**
951 1152 * Runs on plugins_loaded. Use this to add code that needs to be executed later than other
@@ -953,17 +1154,58 @@
953 1154 *
954 1155 * @action plugins_loaded
955 1156 */
956 1157 public function late_initialization() {
957 - add_action( 'plugins_loaded', array( 'Jetpack', 'load_modules' ), 100 );
1158 + add_action( 'after_setup_theme', array( 'Jetpack', 'load_modules' ), -2 );
958 1159
959 - Partner::init();
960 - My_Jetpack_Initializer::init();
1160 + /*
1161 + * My Jetpack is a wp-admin dashboard. Its Initializer::init() only wires
1162 + * up admin-menu, admin_init, and rest_api_init surfaces — and eagerly
1163 + * loads every product class (backup, boost, protect, …) just to register
1164 + * admin plugin-action links — so none of it is needed on a plain
1165 + * front-end GET page view. (The pieces that do immediate work, e.g.
1166 + * Connection REST authentication and Licensing, are already initialized
1167 + * unconditionally in Jetpack's constructor, so they are unaffected here.)
1168 + *
1169 + * Gate the call to the request types where My Jetpack actually does work.
1170 + * REST can't be detected yet at plugins_loaded, so initialize on
1171 + * rest_api_init for that branch; a plain page view never fires it, so My
1172 + * Jetpack stays unloaded there.
1173 + */
1174 + if ( self::should_eager_load_packages() ) {
1175 + My_Jetpack_Initializer::init();
1176 + } else {
1177 + add_action( 'rest_api_init', array( My_Jetpack_Initializer::class, 'init' ), 0 );
1178 + }
961 1179
962 - // Initialize Boost Speed Score
963 - $modules = Jetpack_Boost_Modules::init();
964 - new Speed_Score( $modules, 'jetpack-dashboard' );
1180 + Scan_Page_Init::initialize();
1181 + Jetpack_SEO_Initializer::init();
965 1182
1183 + if ( ( new Modules() )->is_active( 'podcast' ) ) {
1184 + Podcast::init();
1185 + }
1186 +
1187 + /*
1188 + * Initialize Boost Speed Score. It only does work on REST requests (the
1189 + * dashboard speed-score endpoints) and on a few Jetpack Boost lifecycle
1190 + * actions, so defer constructing it — and loading the boost-speed-score
1191 + * package classes — until one of those hooks actually fires instead of on
1192 + * every request. Priority 0 ensures the object's own callbacks (added in
1193 + * its constructor at the default priority) still run for the firing hook.
1194 + */
1195 + $initialize_speed_score = static function () {
1196 + static $initialized = false;
1197 + if ( $initialized ) {
1198 + return;
1199 + }
1200 + $initialized = true;
1201 + new Speed_Score( array(), 'jetpack-dashboard' );
1202 + };
1203 + add_action( 'rest_api_init', $initialize_speed_score, 0 );
1204 + add_action( 'jetpack_boost_deactivate', $initialize_speed_score, 0 );
1205 + add_action( 'jetpack_boost_environment_changed', $initialize_speed_score, 0 );
1206 + add_action( 'handle_environment_change', $initialize_speed_score, 0 );
1207 +
966 1208 /**
967 1209 * Fires when Jetpack is fully loaded and ready. This is the point where it's safe
968 1210 * to instantiate classes from packages and namespaces that are managed by the Jetpack Autoloader.
969 1211 *
@@ -1001,8 +1243,10 @@
1001 1243
1002 1244 /**
1003 1245 * Redirect edit post links to Calypso.
1004 1246 *
1247 + * @deprecated since 13.9
1248 + *
1005 1249 * @param string $default_url Post edit URL.
1006 1250 * @param int $post_id Post ID.
1007 1251 *
1008 1252 * @return string
@@ -1007,8 +1251,10 @@
1007 1251 *
1008 1252 * @return string
1009 1253 */
1010 1254 public function point_edit_post_links_to_calypso( $default_url, $post_id ) {
1255 + _deprecated_function( __METHOD__, '13.9' );
1256 +
1011 1257 $post = get_post( $post_id );
1012 1258
1013 1259 if ( empty( $post ) ) {
1014 1260 return $default_url;
@@ -1039,13 +1285,17 @@
1039 1285
1040 1286 /**
1041 1287 * Redirect edit comment links to Calypso.
1042 1288 *
1289 + * @deprecated since 13.9
1290 + *
1043 1291 * @param string $url Comment edit URL.
1044 1292 *
1045 1293 * @return string
1046 1294 */
1047 1295 public function point_edit_comment_links_to_calypso( $url ) {
1296 + _deprecated_function( __METHOD__, '13.9' );
1297 +
1048 1298 // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID.
1049 1299 $query_args = null;
1050 1300 wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args );
1051 1301
@@ -1064,30 +1314,16 @@
1064 1314 *
1065 1315 * @return array list of callables.
1066 1316 */
1067 1317 public function filter_sync_callable_whitelist( $callables ) {
1068 -
1069 1318 // Jetpack Functions.
1070 1319 $jetpack_callables = array(
1071 1320 'single_user_site' => array( 'Jetpack', 'is_single_user_site' ),
1072 1321 'updates' => array( 'Jetpack', 'get_updates' ),
1073 1322 'available_jetpack_blocks' => array( 'Jetpack_Gutenberg', 'get_availability' ), // Includes both Gutenberg blocks *and* plugins.
1323 + 'theme_styles' => array( 'Automattic\\Jetpack\\Plugin\\Theme_Styles_Sync', 'get_theme_styles' ),
1074 1324 );
1075 - $callables = array_merge( $callables, $jetpack_callables );
1076 -
1077 - // Jetpack_SSO_Helpers.
1078 - if ( include_once JETPACK__PLUGIN_DIR . 'modules/sso/class.jetpack-sso-helpers.php' ) {
1079 - $sso_helpers = array(
1080 - 'sso_is_two_step_required' => array( 'Jetpack_SSO_Helpers', 'is_two_step_required' ),
1081 - 'sso_should_hide_login_form' => array( 'Jetpack_SSO_Helpers', 'should_hide_login_form' ),
1082 - 'sso_match_by_email' => array( 'Jetpack_SSO_Helpers', 'match_by_email' ),
1083 - 'sso_new_user_override' => array( 'Jetpack_SSO_Helpers', 'new_user_override' ),
1084 - 'sso_bypass_default_login_form' => array( 'Jetpack_SSO_Helpers', 'bypass_login_forward_wpcom' ),
1085 - );
1086 - $callables = array_merge( $callables, $sso_helpers );
1087 - }
1088 -
1089 - return $callables;
1325 + return array_merge( $callables, $jetpack_callables );
1090 1326 }
1091 1327
1092 1328 /**
1093 1329 * Extend Sync multisite callables with Jetpack Plugin functions.
@@ -1112,41 +1348,8 @@
1112 1348 return $callables;
1113 1349 }
1114 1350
1115 1351 /**
1116 - * Deprecated
1117 - * Please use Automattic\Jetpack\JITMS\JITM::jetpack_track_last_sync_callback instead.
1118 - *
1119 - * @param array $params The action parameters.
1120 - *
1121 - * @deprecated since 9.8.
1122 - */
1123 - public function jetpack_track_last_sync_callback( $params ) {
1124 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\Automattic\Jetpack\JITMS\JITM->jetpack_track_last_sync_callback' );
1125 - return Automattic\Jetpack\JITMS\JITM::get_instance()->jetpack_track_last_sync_callback( $params );
1126 - }
1127 -
1128 - /**
1129 - * Jetpack Connection banner callback function.
1130 - *
1131 - * @return void
1132 - */
1133 - public function jetpack_connection_banner_callback() {
1134 - check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' );
1135 -
1136 - // Disable the banner dismiss functionality if the pre-connection prompt helpers filter is set.
1137 - if (
1138 - isset( $_REQUEST['dismissBanner'] ) &&
1139 - ! Jetpack_Connection_Banner::force_display()
1140 - ) {
1141 - Jetpack_Options::update_option( 'dismissed_connection_banner', 1 );
1142 - wp_send_json_success();
1143 - }
1144 -
1145 - wp_die();
1146 - }
1147 -
1148 - /**
1149 1352 * If there are any stats that need to be pushed, but haven't been, push them now.
1150 1353 */
1151 1354 public function push_stats() {
1152 1355 if ( ! empty( $this->stats ) ) {
@@ -1161,16 +1364,8 @@
1161 1364 * @param string $cap Capability name.
1162 1365 */
1163 1366 public function jetpack_custom_caps( $caps, $cap ) {
1164 1367 switch ( $cap ) {
1165 - case 'jetpack_manage_modules':
1166 - case 'jetpack_activate_modules':
1167 - case 'jetpack_deactivate_modules':
1168 - $caps = array( 'manage_options' );
1169 - break;
1170 - case 'jetpack_configure_modules':
1171 - $caps = array( 'manage_options' );
1172 - break;
1173 1368 case 'jetpack_manage_autoupdates':
1174 1369 $caps = array(
1175 1370 'manage_options',
1176 1371 'update_plugins',
@@ -1188,9 +1383,9 @@
1188 1383 if ( $is_offline_mode ) {
1189 1384 $caps = array( 'manage_options' );
1190 1385 break;
1191 1386 } else {
1192 - $caps = array( 'read' );
1387 + $caps = array( 'edit_posts' );
1193 1388 }
1194 1389 break;
1195 1390 }
1196 1391 return $caps;
@@ -1349,9 +1544,9 @@
1349 1544 }
1350 1545 /**
1351 1546 * Does the network allow admins to add new users.
1352 1547 *
1353 - * @return boolian
1548 + * @return bool
1354 1549 */
1355 1550 public static function network_add_new_users() {
1356 1551 return (bool) get_site_option( 'add_new_users' );
1357 1552 }
@@ -1358,9 +1553,9 @@
1358 1553 /**
1359 1554 * File upload psace left per site in MB.
1360 1555 * -1 means NO LIMIT.
1361 1556 *
1362 - * @return number
1557 + * @return int
1363 1558 */
1364 1559 public static function network_site_upload_space() {
1365 1560 // value in MB.
1366 1561 return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() );
@@ -1377,9 +1572,9 @@
1377 1572
1378 1573 /**
1379 1574 * Maximum file upload size set by the network.
1380 1575 *
1381 - * @return number
1576 + * @return int
1382 1577 */
1383 1578 public static function network_max_upload_file_size() {
1384 1579 // value in KB.
1385 1580 return get_site_option( 'fileupload_maxk', 300 );
@@ -1633,44 +1828,8 @@
1633 1828 return apply_filters( 'jetpack_is_connection_ready', self::connection()->is_connected(), self::connection() );
1634 1829 }
1635 1830
1636 1831 /**
1637 - * Deprecated: Is Jetpack in development (offline) mode?
1638 - *
1639 - * This static method is being left here intentionally without the use of _deprecated_function(), as other plugins
1640 - * and themes still use it, and we do not want to flood them with notices.
1641 - *
1642 - * Please use Automattic\Jetpack\Status()->is_offline_mode() instead.
1643 - *
1644 - * @deprecated since 8.0.
1645 - */
1646 - public static function is_development_mode() {
1647 - _deprecated_function( __METHOD__, 'jetpack-8.0', '\Automattic\Jetpack\Status->is_offline_mode' );
1648 - return ( new Status() )->is_offline_mode();
1649 - }
1650 -
1651 - /**
1652 - * Whether the site is currently onboarding or not.
1653 - * A site is considered as being onboarded if it currently has an onboarding token.
1654 - *
1655 - * @since 5.8
1656 - * @deprecated Use \Automattic\Jetpack\Status()->is_onboarding()
1657 - *
1658 - * @access public
1659 - * @static
1660 - *
1661 - * @return bool True if the site is currently onboarding, false otherwise
1662 - */
1663 - public static function is_onboarding() {
1664 - _deprecated_function( __METHOD__, 'jetpack-10.9', 'Automattic\\Jetpack\\Status\\is_onboarding' );
1665 -
1666 - if ( ! method_exists( 'Automattic\Jetpack\Status', 'is_onboarding' ) ) {
1667 - return Jetpack_Options::get_option( 'onboarding' ) !== false;
1668 - }
1669 - return ( new Status() )->is_onboarding();
1670 - }
1671 -
1672 - /**
1673 1832 * Determines reason for Jetpack offline mode.
1674 1833 */
1675 1834 public static function development_mode_trigger_text() {
1676 1835 $status = new Status();
@@ -1684,11 +1843,10 @@
1684 1843 } elseif ( defined( 'WP_LOCAL_DEV' ) && WP_LOCAL_DEV ) {
1685 1844 $notice = __( 'The WP_LOCAL_DEV constant is defined in wp-config.php or elsewhere.', 'jetpack' );
1686 1845 } elseif ( $status->is_local_site() ) {
1687 1846 $notice = __( 'The site URL is a known local development environment URL (e.g. http://localhost).', 'jetpack' );
1688 - /** This filter is documented in packages/status/src/class-status.php */
1689 - } elseif ( has_filter( 'jetpack_development_mode' ) && apply_filters( 'jetpack_development_mode', false ) ) { // This is a deprecated filter name.
1690 - $notice = __( 'The jetpack_development_mode filter is set to true.', 'jetpack' );
1847 + } elseif ( get_option( 'jetpack_offline_mode' ) ) {
1848 + $notice = __( 'The jetpack_offline_mode option is set to true.', 'jetpack' );
1691 1849 } else {
1692 1850 $notice = __( 'The jetpack_offline_mode filter is set to true.', 'jetpack' );
1693 1851 }
1694 1852
@@ -1718,15 +1876,8 @@
1718 1876 $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' ) ) );
1719 1877
1720 1878 echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All provided text.
1721 1879 }
1722 - // Throw up a notice if using staging mode.
1723 - if ( ( new Status() )->is_staging_site() ) {
1724 - /* translators: %s is a URL */
1725 - $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), esc_url( Redirect::get_url( 'jetpack-support-staging-sites' ) ) );
1726 -
1727 - echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All provided text.
1728 - }
1729 1880 }
1730 1881
1731 1882 /**
1732 1883 * Whether Jetpack's version maps to a public release, or a development version.
@@ -1747,28 +1898,8 @@
1747 1898 );
1748 1899 }
1749 1900
1750 1901 /**
1751 - * Is a given user (or the current user if none is specified) linked to a WordPress.com user?
1752 - *
1753 - * @param int $user_id User ID or will use get_current_user_id if false/not provided.
1754 - */
1755 - public static function is_user_connected( $user_id = false ) {
1756 - _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Manager\\is_user_connected' );
1757 - return self::connection()->is_user_connected( $user_id );
1758 - }
1759 -
1760 - /**
1761 - * Get the wpcom user data of the current|specified connected user.
1762 - *
1763 - * @param null|int $user_id User ID or will use get_current_user_id if null.
1764 - */
1765 - public static function get_connected_user_data( $user_id = null ) {
1766 - _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Manager\\get_connected_user_data' );
1767 - return self::connection()->get_connected_user_data( $user_id );
1768 - }
1769 -
1770 - /**
1771 1902 * Get the wpcom email of the current|specified connected user.
1772 1903 *
1773 1904 * @param null|int $user_id User ID or will use get_current_user_id if null.
1774 1905 */
@@ -1820,18 +1951,11 @@
1820 1951 */
1821 1952 public static function load_modules() {
1822 1953 $status = new Status();
1823 1954
1824 - if ( method_exists( $status, 'is_onboarding' ) ) {
1825 - $is_onboarding = $status->is_onboarding();
1826 - } else {
1827 - $is_onboarding = self::is_onboarding();
1828 - }
1829 -
1830 1955 if (
1831 1956 ! self::is_connection_ready()
1832 1957 && ! $status->is_offline_mode()
1833 - && ! $is_onboarding
1834 1958 && (
1835 1959 ! is_multisite()
1836 1960 || ! get_site_option( 'jetpack_protect_active' )
1837 1961 )
@@ -1952,22 +2076,14 @@
1952 2076 *
1953 2077 * @todo Store the result in core's object cache maybe?
1954 2078 */
1955 2079 public static function get_active_plugins() {
1956 - $active_plugins = (array) get_option( 'active_plugins', array() );
1957 -
1958 - if ( is_multisite() ) {
1959 - // Due to legacy code, active_sitewide_plugins stores them in the keys,
1960 - // whereas active_plugins stores them in the values.
1961 - $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
1962 - if ( $network_plugins ) {
1963 - $active_plugins = array_merge( $active_plugins, $network_plugins );
1964 - }
2080 + // Older Connection copies can load first and lack this method.
2081 + if ( ! method_exists( Heartbeat::class, 'get_active_plugins' ) ) {
2082 + return array();
1965 2083 }
1966 2084
1967 - sort( $active_plugins );
1968 -
1969 - return array_unique( $active_plugins );
2085 + return Heartbeat::get_active_plugins();
1970 2086 }
1971 2087
1972 2088 /**
1973 2089 * Gets and parses additional plugin data to send with the heartbeat data
@@ -2105,8 +2221,10 @@
2105 2221 *
2106 2222 * @param bool true Should Twitter Card Meta tags be disabled. Default to true.
2107 2223 */
2108 2224 if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) {
2225 + // @todo Remove this require once the deprecated Jetpack_Twitter_Cards wrapper has been removed.
2226 + // Twitter Cards functionality now lives in the jetpack-post-media package (Automattic\Jetpack\Post_Media\Twitter_Cards).
2109 2227 require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
2110 2228 }
2111 2229 }
2112 2230
@@ -2209,9 +2327,9 @@
2209 2327 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.
2210 2328 $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- we're not changing the site.
2211 2329 }
2212 2330 wp_safe_redirect( self::admin_url( 'page=' . rawurlencode( $page ) ) );
2213 - exit;
2331 + exit( 0 );
2214 2332 }
2215 2333 }
2216 2334
2217 2335 /**
@@ -2258,8 +2376,12 @@
2258 2376 default:
2259 2377 break;
2260 2378 }
2261 2379 }
2380 + if ( method_exists( Feature_Policy::class, 'ensure_hooks' ) ) {
2381 + Feature_Policy::ensure_hooks();
2382 + }
2383 +
2262 2384 /**
2263 2385 * Filters the array of default modules.
2264 2386 *
2265 2387 * @since 2.5.0
@@ -2291,12 +2413,14 @@
2291 2413 * @return array
2292 2414 */
2293 2415 public function handle_deprecated_modules( $modules ) {
2294 2416 $deprecated_modules = array(
2295 - 'debug' => null, // Closed out and moved to the debugger library.
2296 - 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2297 - 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support.
2298 - 'minileven' => null, // Closed out in 8.3 -- Responsive themes are common now, and so is AMP.
2417 + 'debug' => null, // Closed out and moved to the debugger library.
2418 + 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
2419 + 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support.
2420 + 'minileven' => null, // Closed out in 8.3 -- Responsive themes are common now, and so is AMP.
2421 + 'lazy-images' => null, // Closed out in 12.8 -- WordPress core now has native lazy loading.
2422 + 'enhanced-distribution' => null, // Closed out in 13.3 -- WP.com is winding down the firehose.
2299 2423 );
2300 2424
2301 2425 // Don't activate SSO if they never completed activating WPCC.
2302 2426 if ( self::is_module_active( 'wpcc' ) ) {
@@ -2350,8 +2474,17 @@
2350 2474 }
2351 2475 }
2352 2476 }
2353 2477
2478 + // Special case to convert block setting to a block module.
2479 + $block_key = array_search( 'blocks', $modules, true );
2480 + if ( $block_key !== false ) { // Only care if 'blocks' made it through the previous filters.
2481 + $block_option = get_option( 'jetpack_blocks_disabled', null );
2482 + if ( $block_option ) {
2483 + unset( $modules[ $block_key ] );
2484 + }
2485 + }
2486 +
2354 2487 return $modules;
2355 2488 }
2356 2489
2357 2490 /**
@@ -2408,23 +2541,30 @@
2408 2541
2409 2542 /**
2410 2543 * Return module name translation. Uses matching string created in modules/module-headings.php.
2411 2544 *
2545 + * The module list is globbed from `modules/` at runtime, so a module can be listed with no
2546 + * entry in that generated file. Fall back to the untranslated header rather than overwriting
2547 + * it with the null `jetpack_get_module_i18n()` returns for an unknown slug.
2548 + *
2412 2549 * @since 3.9.2
2413 2550 *
2414 2551 * @param array $modules Array of Jetpack modules.
2415 2552 *
2416 - * @return string|void
2553 + * @return array
2417 2554 */
2418 2555 public static function get_translated_modules( $modules ) {
2419 2556 foreach ( $modules as $index => $module ) {
2420 2557 $i18n_module = jetpack_get_module_i18n( $module['module'] );
2421 - if ( isset( $module['name'] ) ) {
2422 - $modules[ $index ]['name'] = $i18n_module['name'];
2558 + $name = $i18n_module['name'] ?? null;
2559 + $description = $i18n_module['description'] ?? null;
2560 +
2561 + if ( null !== $name && isset( $module['name'] ) ) {
2562 + $modules[ $index ]['name'] = $name;
2423 2563 }
2424 - if ( isset( $module['description'] ) ) {
2425 - $modules[ $index ]['description'] = $i18n_module['description'];
2426 - $modules[ $index ]['short_description'] = $i18n_module['description'];
2564 + if ( null !== $description && isset( $module['description'] ) ) {
2565 + $modules[ $index ]['description'] = $description;
2566 + $modules[ $index ]['short_description'] = $description;
2427 2567 }
2428 2568 if ( isset( $module['module_tags'] ) ) {
2429 2569 $modules[ $index ]['module_tags'] = array_map( 'jetpack_get_module_i18n_tag', $module['module_tags'] );
2430 2570 }
@@ -2464,20 +2604,28 @@
2464 2604
2465 2605 /**
2466 2606 * Catches PHP errors. Must be used in conjunction with output buffering.
2467 2607 *
2608 + * @deprecated since 13.5
2468 2609 * @param bool $catch True to start catching, False to stop.
2469 2610 *
2470 2611 * @static
2612 + * @deprecated 13.5
2613 + * @see \Automattic\Jetpack\Errors
2471 2614 */
2472 2615 public static function catch_errors( $catch ) {
2616 + _deprecated_function( __METHOD__, '13.5' );
2617 + // @phan-suppress-next-line PhanDeprecatedClass
2473 2618 return ( new Errors() )->catch_errors( $catch );
2474 2619 }
2475 2620
2476 2621 /**
2477 2622 * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
2623 + *
2624 + * @deprecated since 13.5
2478 2625 */
2479 2626 public static function catch_errors_on_shutdown() {
2627 + _deprecated_function( __METHOD__, '13.5' );
2480 2628 self::state( 'php_errors', self::alias_directories( ob_get_clean() ) );
2481 2629 }
2482 2630
2483 2631 /**
@@ -2581,9 +2729,9 @@
2581 2729 ),
2582 2730 add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), self::admin_url( 'page=jetpack' ) )
2583 2731 );
2584 2732 wp_safe_redirect( $url );
2585 - exit;
2733 + exit( 0 );
2586 2734 }
2587 2735 }
2588 2736
2589 2737 /**
@@ -2601,9 +2749,8 @@
2601 2749
2602 2750 // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating.
2603 2751 if ( $send_state_messages ) {
2604 2752 self::restate();
2605 - self::catch_errors( true );
2606 2753 }
2607 2754
2608 2755 $active = self::get_active_modules();
2609 2756
@@ -2671,10 +2818,8 @@
2671 2818 if ( $send_state_messages ) {
2672 2819 self::state( 'error', false );
2673 2820 self::state( 'module', false );
2674 2821 }
2675 -
2676 - self::catch_errors( false );
2677 2822 /**
2678 2823 * Fires when default modules are activated.
2679 2824 *
2680 2825 * @since 1.9.0
@@ -2732,9 +2877,9 @@
2732 2877 * @return string $url module configuration URL.
2733 2878 */
2734 2879 public static function module_configuration_url( $module ) {
2735 2880 $module = self::get_module_slug( $module );
2736 - $default_url = self::admin_url() . "#/settings?term=$module";
2881 + $default_url = self::admin_url( array( 'page' => 'jetpack-settings' ) ) . "#/settings?term=$module";
2737 2882 /**
2738 2883 * Allows to modify configure_url of specific module to be able to redirect to some custom location.
2739 2884 *
2740 2885 * @since 6.9.0
@@ -2752,9 +2897,9 @@
2752 2897 *
2753 2898 * @param string $message Error message.
2754 2899 * @param bool $deactivate Deactivate Jetpack or not.
2755 2900 *
2756 - * @return void
2901 + * @return never
2757 2902 */
2758 2903 public static function bail_on_activation( $message, $deactivate = true ) {
2759 2904 ?>
2760 2905 <!doctype html>
@@ -2792,9 +2937,9 @@
2792 2937 if ( $update ) {
2793 2938 update_option( 'active_plugins', array_filter( $plugins ) );
2794 2939 }
2795 2940 }
2796 - exit;
2941 + exit( 0 );
2797 2942 }
2798 2943
2799 2944 /**
2800 2945 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
@@ -2819,12 +2964,18 @@
2819 2964 update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) );
2820 2965
2821 2966 Health::on_jetpack_activated();
2822 2967
2968 + \Automattic\Jetpack\Reprint_Export\Reprint_Exporter::discard_credentials();
2969 +
2823 2970 if ( self::is_connection_ready() && method_exists( 'Automattic\Jetpack\Sync\Actions', 'do_only_first_initial_sync' ) ) {
2824 2971 Sync_Actions::do_only_first_initial_sync();
2825 2972 }
2826 2973
2974 + if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
2975 + Woocommerce_Analytics::maybe_add_proxy_speed_module();
2976 + }
2977 +
2827 2978 self::plugin_initialize();
2828 2979 }
2829 2980
2830 2981 /**
@@ -2859,9 +3010,9 @@
2859 3010
2860 3011 if ( $plugins_path === $referer['path'] ) {
2861 3012 $source_type = 'list';
2862 3013 } elseif ( $plugins_install_path === $referer['path'] ) {
2863 - $tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured';
3014 + $tab = $query_parts['tab'] ?? 'featured';
2864 3015 switch ( $tab ) {
2865 3016 case 'popular':
2866 3017 $source_type = 'popular';
2867 3018 break;
@@ -2871,10 +3022,10 @@
2871 3022 case 'favorites':
2872 3023 $source_type = 'favorites';
2873 3024 break;
2874 3025 case 'search':
2875 - $source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' );
2876 - $source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null;
3026 + $source_type = 'search-' . ( $query_parts['type'] ?? 'term' );
3027 + $source_query = $query_parts['s'] ?? null;
2877 3028 break;
2878 3029 default:
2879 3030 $source_type = 'featured';
2880 3031 }
@@ -2883,29 +3034,171 @@
2883 3034 return array( $source_type, $source_query );
2884 3035 }
2885 3036
2886 3037 /**
2887 - * Runs before bumping version numbers up to a new version
3038 + * Runs before bumping version numbers up to a new version.
2888 3039 *
2889 - * @param string $version Version:timestamp.
3040 + * Only ever registered the hooks for the release post update modal, which has been removed.
3041 + * No longer hooked to `updating_jetpack_version`.
3042 + *
3043 + * @deprecated 16.2
3044 + *
3045 + * @param string $version Version:timestamp.
2890 3046 * @param string $old_version Old Version:timestamp or false if not set yet.
2891 3047 */
2892 - public static function do_version_bump( $version, $old_version ) {
2893 - if ( $old_version ) { // For existing Jetpack installations.
2894 - add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
3048 + public static function do_version_bump( $version, $old_version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Signature preserved for the deprecation shim.
3049 + _deprecated_function( __METHOD__, 'jetpack-16.2' );
3050 + }
2895 3051
2896 - // If a front end page is visited after the update, the 'wp' action will fire.
2897 - add_action( 'wp', 'Jetpack::set_update_modal_display' );
3052 + /**
3053 + * Enables the Newsletter (subscriptions) module for existing sites now that it is a default-on module.
3054 + *
3055 + * Fresh installs receive the module via its "Auto Activate: Yes" header, so this only handles sites
3056 + * upgrading from a version where the module defaulted off. It runs once per site (guarded by the
3057 + * subscriptions_default_on_migrated option). Fresh installs are marked as migrated immediately so the
3058 + * migration never runs for them. After it has run, the user's choice to deactivate the module again
3059 + * (for example from the My Jetpack Products page) is respected and never reverted.
3060 + *
3061 + * The module requires a connection, so on a disconnected site the migration is deferred without setting
3062 + * the guard, allowing a later version bump to retry once the site is connected.
3063 + *
3064 + * @param string $version New Jetpack version:timestamp.
3065 + * @param string|false $old_version Previous Jetpack version:timestamp, or false on a fresh install.
3066 + */
3067 + public static function activate_subscriptions_module_for_existing_sites( $version, $old_version ) {
3068 + if ( get_option( 'jetpack_subscriptions_default_on_migrated' ) ) {
3069 + return;
3070 + }
2898 3071
2899 - // If an admin page is visited after the update, the 'current_screen' action will fire.
2900 - add_action( 'current_screen', 'Jetpack::set_update_modal_display' );
3072 + // Fresh installs get the module via its "Auto Activate: Yes" header. Mark them as migrated so a
3073 + // later opt-out is never reverted by the existing-site path on a subsequent version bump.
3074 + if ( ! $old_version ) {
3075 + update_option( 'jetpack_subscriptions_default_on_migrated', true );
3076 + return;
2901 3077 }
3078 +
3079 + if ( ! self::is_connection_ready() ) {
3080 + return;
3081 + }
3082 +
3083 + // Mark as migrated only once the module is active, so a transient activation failure is retried on
3084 + // a later version bump rather than being silently skipped.
3085 + if ( self::is_module_active( 'subscriptions' ) || self::activate_module( 'subscriptions', false, false ) ) {
3086 + update_option( 'jetpack_subscriptions_default_on_migrated', true );
3087 + }
2902 3088 }
2903 3089
2904 3090 /**
3091 + * Option flag that records the AI master-switch opt-out reconciliation has run,
3092 + * so it never runs twice.
3093 + *
3094 + * @var string
3095 + */
3096 + const AI_MASTER_OPTOUT_MIGRATED_OPTION = 'jetpack_ai_master_optout_migrated';
3097 +
3098 + /**
3099 + * Register the on-upgrade init hooks whose relative ORDER matters, extracted so
3100 + * the ordering can be asserted in tests without invoking plugin_upgrade() (whose
3101 + * guards make it unreliable to trigger under test). activate_new_modules()
3102 + * (init, default priority 10) auto-activates "Auto Activate: Yes" modules
3103 + * including the `ai` master; reconcile_ai_master_optout() must run at a LATER
3104 + * priority to honor an explicit AI opt-out.
3105 + *
3106 + * @return void
3107 + */
3108 + public static function register_upgrade_init_hooks() {
3109 + add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
3110 + add_action( 'init', array( __CLASS__, 'reconcile_ai_master_optout' ), 20 );
3111 + }
3112 +
3113 + /**
3114 + * Preserves an explicit Jetpack AI opt-out when the `ai` module becomes the site-wide
3115 + * master switch off WordPress.com Simple (self-hosted and Atomic).
3116 + *
3117 + * The `ai` module is "Auto Activate: Yes", so on upgrade {@see self::activate_new_modules()}
3118 + * turns it on for connected sites — the desired default-on / auto-enable-on-connection
3119 + * behavior, which this method deliberately leaves alone. The one case it corrects is a site
3120 + * that had explicitly disabled Jetpack AI (the `jetpack_ai_enabled` option present and falsey)
3121 + * before the module shipped: that opt-out must survive the module becoming the master, so the
3122 + * module is deactivated for exactly those sites. An absent or truthy option is left untouched.
3123 + *
3124 + * Ordering is the whole point. `activate_new_modules()` is hooked on `init` at priority 10 and
3125 + * auto-activates the module there; this method is hooked on `init` at priority 20 (see
3126 + * {@see self::plugin_upgrade()}), so it runs AFTER the auto-activation and its deactivation is
3127 + * the final state. A version-guarded block that ran inline during `plugins_loaded` would be
3128 + * undone by the later auto-activation, which is why this is a late-init hook rather than an
3129 + * inline upgrade step.
3130 + *
3131 + * WordPress.com Simple never runs modules — the option stays the master there — so this is a
3132 + * no-op on Simple. The {@see self::AI_MASTER_OPTOUT_MIGRATED_OPTION} flag makes it run exactly
3133 + * once, which matters because off-Simple the option is no longer the master after this runs:
3134 + * a stale falsey option must not keep re-deactivating a module the user later turns back on.
3135 + *
3136 + * @return void
3137 + */
3138 + public static function reconcile_ai_master_optout() {
3139 + if ( get_option( self::AI_MASTER_OPTOUT_MIGRATED_OPTION ) ) {
3140 + return;
3141 + }
3142 +
3143 + // Simple keeps the `jetpack_ai_enabled` option as the master; modules don't run there.
3144 + if ( ( new Host() )->is_wpcom_simple() ) {
3145 + return;
3146 + }
3147 +
3148 + // A sentinel default distinguishes an absent option (leave auto-activation alone) from one
3149 + // explicitly stored falsey (an opt-out to preserve).
3150 + $stored = get_option( 'jetpack_ai_enabled', 'not-set' );
3151 + if ( 'not-set' !== $stored && ! (bool) $stored ) {
3152 + ( new Modules() )->deactivate( 'ai' );
3153 + }
3154 +
3155 + update_option( self::AI_MASTER_OPTOUT_MIGRATED_OPTION, true );
3156 + }
3157 +
3158 + /**
3159 + * Deletes obsolete SEO module-state options without changing module activation.
3160 + *
3161 + * @since 16.3
3162 + */
3163 + public static function cleanup_seo_module_state_options() {
3164 + delete_option( 'jetpack_seo_sitemap_enabled' );
3165 + delete_option( 'jetpack_seo_canonical_urls_enabled' );
3166 + delete_option( 'jetpack_seo_module_state_reconciled' );
3167 + }
3168 +
3169 + /**
3170 + * Seeds the Jetpack SEO discoverability cohort once, so the new SEO surface is
3171 + * auto-discoverable on fresh installs but opt-in on existing ones (JETPACK-1700).
3172 + *
3173 + * Hooked on `updating_jetpack_version`, which fires on every install including the
3174 + * first — with `$old_version === false` on a brand-new site (the same signal
3175 + * {@see self::activate_subscriptions_module_for_existing_sites()} keys off). Fresh
3176 + * installs are seeded visible; existing installs are seeded hidden and opt in later
3177 + * via the legacy Traffic page or My Jetpack. `add_option()` makes this seed-once: it
3178 + * never overrides a value a later opt-in (or opt-out) has set. WordPress.com sites
3179 + * ignore this option entirely (always visible) — see
3180 + * {@see \Automattic\Jetpack\SEO\Initializer::is_seo_surface_visible()}.
3181 + *
3182 + * @param string $version The new Jetpack version (unused).
3183 + * @param string|false $old_version The previous version, or false on a fresh install.
3184 + */
3185 + public static function seed_seo_visibility_cohort( $version, $old_version ) {
3186 + add_option( Jetpack_SEO_Initializer::VISIBILITY_OPTION, ! $old_version );
3187 + }
3188 +
3189 + /**
2905 3190 * Sets the display_update_modal state.
3191 + *
3192 + * The release post update modal that read this state has been removed. The write is kept so the
3193 + * method still behaves as documented for the deprecation window. When this is deleted, also drop
3194 + * the matching `display_update_modal` guard in Automattic\Jetpack\CookieState::should_set_cookie(),
3195 + * which exists only to keep this key out of the cookie on the Jetpack admin screen.
3196 + *
3197 + * @deprecated 16.2
2906 3198 */
2907 3199 public static function set_update_modal_display() {
3200 + _deprecated_function( __METHOD__, 'jetpack-16.2' );
2908 3201 self::state( 'display_update_modal', true );
2909 3202 }
2910 3203
2911 3204 /**
@@ -2910,11 +3203,16 @@
2910 3203
2911 3204 /**
2912 3205 * Enqueues the block library styles.
2913 3206 *
3207 + * Only ever used by the release post update modal, which has been removed.
3208 + *
3209 + * @deprecated 16.2
3210 + *
2914 3211 * @param string $hook The current admin page.
2915 3212 */
2916 3213 public static function enqueue_block_style( $hook ) {
3214 + _deprecated_function( __METHOD__, 'jetpack-16.2' );
2917 3215 if ( 'toplevel_page_jetpack' === $hook ) {
2918 3216 wp_enqueue_style( 'wp-block-library' );
2919 3217 }
2920 3218 }
@@ -2943,9 +3241,8 @@
2943 3241
2944 3242 self::load_modules();
2945 3243
2946 3244 Jetpack_Options::delete_option( 'do_activate' );
2947 - Jetpack_Options::delete_option( 'dismissed_connection_banner' );
2948 3245 }
2949 3246
2950 3247 /**
2951 3248 * Handles the activation of the default modules depending on the current state of the site:
@@ -3004,8 +3301,12 @@
3004 3301 add_filter( 'jetpack_update_activated_state_on_disconnect', '__return_false' );
3005 3302 self::disconnect();
3006 3303 Jetpack_Options::delete_option( 'version' );
3007 3304 }
3305 +
3306 + if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
3307 + Woocommerce_Analytics::maybe_remove_proxy_speed_module();
3308 + }
3008 3309 }
3009 3310
3010 3311 /**
3011 3312 * Set activated option to 4 on jetpack_idc_disconnect action.
@@ -3033,9 +3334,9 @@
3033 3334 $connection->remove_connection( ! Identity_Crisis::validate_sync_error_idc_option() );
3034 3335 }
3035 3336
3036 3337 /**
3037 - * Happens after a successfull disconnection.
3338 + * Happens after a successful disconnection.
3038 3339 *
3039 3340 * @static
3040 3341 */
3041 3342 public static function jetpack_site_disconnected() {
@@ -3040,8 +3341,10 @@
3040 3341 */
3041 3342 public static function jetpack_site_disconnected() {
3042 3343 Identity_Crisis::clear_all_idc_options();
3043 3344
3345 + \Automattic\Jetpack\Reprint_Export\Reprint_Exporter::discard_credentials();
3346 +
3044 3347 // Delete all the sync related data. Since it could be taking up space.
3045 3348 Sender::get_instance()->uninstall();
3046 3349
3047 3350 /**
@@ -3058,10 +3361,13 @@
3058 3361 }
3059 3362 }
3060 3363
3061 3364 /**
3062 - * Disconnects the user
3365 + * Disconnects the user.
3063 3366 *
3367 + * @deprecated 13.4
3368 + * @see \Automattic\Jetpack\Connection\Manager::disconnect_user()
3369 + *
3064 3370 * @param int $user_id The user ID to disconnect.
3065 3371 */
3066 3372 public function disconnect_user( $user_id ) {
3067 3373 $this->connection_manager->disconnect_user( $user_id );
@@ -3067,21 +3373,8 @@
3067 3373 $this->connection_manager->disconnect_user( $user_id );
3068 3374 }
3069 3375
3070 3376 /**
3071 - * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load()
3072 - *
3073 - * @deprecated since Jetpack 9.7.0
3074 - * @see Automattic\Jetpack\Connection\Manager::try_registration()
3075 - *
3076 - * @return bool|WP_Error
3077 - */
3078 - public static function try_registration() {
3079 - _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Manager::try_registration' );
3080 - return static::connection()->try_registration();
3081 - }
3082 -
3083 - /**
3084 3377 * Checking the domain names in beta versions.
3085 3378 * If this is a development version, before attempting to connect, let's make sure that the domains are viable.
3086 3379 *
3087 3380 * @param null|\WP_Error $error The domain validation error, or `null` if everything's fine.
@@ -3088,9 +3381,9 @@
3088 3381 *
3089 3382 * @return null|\WP_Error The domain validation error, or `null` if everything's fine.
3090 3383 */
3091 3384 public static function registration_check_domains( $error ) {
3092 - if ( static::is_development_version() && defined( 'PHP_URL_HOST' ) ) {
3385 + if ( static::is_development_version() ) {
3093 3386 $domains_to_check = array_unique(
3094 3387 array(
3095 3388 'siteurl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
3096 3389 'homeurl' => wp_parse_url( get_home_url(), PHP_URL_HOST ),
@@ -3115,10 +3408,17 @@
3115 3408 * @param mixed $code Error code to log.
3116 3409 * @param mixed $data Data to log.
3117 3410 */
3118 3411 public static function log( $code, $data = null ) {
3412 +
3413 + $raw_log = Jetpack_Options::get_option( 'log', array() );
3414 + // This can be modified by the `jetpack_options` filter, so abort if we don't have an array.
3415 + if ( ! is_array( $raw_log ) ) {
3416 + return;
3417 + }
3418 +
3119 3419 // only grab the latest 200 entries.
3120 - $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 );
3420 + $log = array_slice( $raw_log, -199, 199 );
3121 3421
3122 3422 // Append our event to the log.
3123 3423 $log_entry = array(
3124 3424 'time' => time(),
@@ -3218,8 +3518,15 @@
3218 3518
3219 3519 /**
3220 3520 * Return stat data for WPCOM sync.
3221 3521 *
3522 + * The Sync stats module was this method's last caller and moved to the Connection package's
3523 + * `Heartbeat::generate_stats_array()`, which assembles the heartbeat data through the
3524 + * `jetpack_heartbeat_stats_array` filter. Note the package method does not include the extended
3525 + * data from `get_additional_stat_data()`, so callers relying on `$extended` need to add it themselves.
3526 + *
3527 + * @deprecated 16.2
3528 + *
3222 3529 * @param bool $encode JSON encode the result.
3223 3530 * @param bool $extended Adds additional stats data.
3224 3531 *
3225 3532 * @return array|string Stats data. Array if $encode is false. JSON-encoded string is $encode is true.
@@ -3224,10 +3531,15 @@
3224 3531 *
3225 3532 * @return array|string Stats data. Array if $encode is false. JSON-encoded string is $encode is true.
3226 3533 */
3227 3534 public static function get_stat_data( $encode = true, $extended = true ) {
3228 - $data = Jetpack_Heartbeat::generate_stats_array();
3535 + _deprecated_function( __METHOD__, 'jetpack-16.2', 'Automattic\\Jetpack\\Heartbeat::generate_stats_array' );
3229 3536
3537 + $env_stats = method_exists( Heartbeat::class, 'get_environment_stats' )
3538 + ? Heartbeat::get_environment_stats()
3539 + : array();
3540 + $data = array_merge( Jetpack_Heartbeat::generate_stats_array(), $env_stats );
3541 +
3230 3542 if ( $extended ) {
3231 3543 $additional_data = self::get_additional_stat_data();
3232 3544 $data = array_merge( $data, $additional_data );
3233 3545 }
@@ -3232,9 +3544,9 @@
3232 3544 $data = array_merge( $data, $additional_data );
3233 3545 }
3234 3546
3235 3547 if ( $encode ) {
3236 - return wp_json_encode( $data );
3548 + return wp_json_encode( $data, JSON_UNESCAPED_SLASHES );
3237 3549 }
3238 3550
3239 3551 return $data;
3240 3552 }
@@ -3306,23 +3618,24 @@
3306 3618 $fallback_no_verify_ssl_certs = Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' );
3307 3619 /** Already documented in automattic/jetpack-connection::src/class-client.php */
3308 3620 $client_verify_ssl_certs = apply_filters( 'jetpack_client_verify_ssl_certs', false );
3309 3621
3310 - if ( ! $is_offline_mode ) {
3311 - Jetpack_Connection_Banner::init();
3312 - Jetpack_Connection_Widget::init();
3313 - }
3622 + // Run post-activation actions if needed.
3623 + $this->plugin_post_activation();
3314 3624
3315 3625 if ( ( self::is_connection_ready() || $is_offline_mode ) && false === $fallback_no_verify_ssl_certs && ! $client_verify_ssl_certs ) {
3316 3626 // Upgrade: 1.1 -> 1.1.1
3317 3627 // Check and see if host can verify the Jetpack servers' SSL certificate.
3318 3628 $args = array();
3629 + // @phan-suppress-next-line PhanAccessMethodInternal -- Phan is correct, but the usage is intentional.
3319 3630 Client::_wp_remote_request( self::connection()->api_url( 'test' ), $args, true );
3320 3631 }
3321 3632
3322 - Jetpack_Recommendations_Banner::init();
3323 -
3324 - if ( current_user_can( 'manage_options' ) && ! self::permit_ssl() ) {
3633 + if (
3634 + current_user_can( 'manage_options' )
3635 + && ! self::permit_ssl()
3636 + && ! $is_offline_mode
3637 + ) {
3325 3638 add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
3326 3639 }
3327 3640
3328 3641 add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
@@ -3331,12 +3644,8 @@
3331 3644 if ( ! ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) && ! is_network_admin() ) ) {
3332 3645 add_action( 'admin_enqueue_scripts', array( $this, 'deactivate_dialog' ) );
3333 3646 }
3334 3647
3335 - if ( isset( $_COOKIE['jetpackState']['display_update_modal'] ) ) {
3336 - add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
3337 - }
3338 -
3339 3648 add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) );
3340 3649
3341 3650 if ( self::is_connection_ready() || $is_offline_mode ) {
3342 3651 // Artificially throw errors in certain specific cases during plugin activation.
@@ -3341,13 +3650,8 @@
3341 3650 if ( self::is_connection_ready() || $is_offline_mode ) {
3342 3651 // Artificially throw errors in certain specific cases during plugin activation.
3343 3652 add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
3344 3653 }
3345 -
3346 - // Add custom column in wp-admin/users.php to show whether user is linked.
3347 - add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) );
3348 - add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 );
3349 - add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) );
3350 3654 }
3351 3655
3352 3656 /**
3353 3657 * Adds body classes.
@@ -3380,8 +3684,9 @@
3380 3684 * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
3381 3685 * This function artificially throws errors for such cases (per a specific list).
3382 3686 *
3383 3687 * @param string $plugin The activated plugin.
3688 + * @throws RuntimeException If a conflicting plugin is detected.
3384 3689 */
3385 3690 public function throw_error_on_activate_plugin( $plugin ) {
3386 3691 $active_modules = self::get_active_modules();
3387 3692
@@ -3394,8 +3699,9 @@
3394 3699 if ( 'stats.php' === basename( $plugin ) ) {
3395 3700 $throw = true;
3396 3701 }
3397 3702 } else {
3703 + // @phan-suppress-next-line PhanUndeclaredFunctionInCallable -- Checked above. See also https://github.com/phan/phan/issues/1204.
3398 3704 $reflection = new ReflectionFunction( 'stats_get_api_key' );
3399 3705 if ( basename( $plugin ) === basename( $reflection->getFileName() ) ) {
3400 3706 $throw = true;
3401 3707 }
@@ -3402,9 +3708,9 @@
3402 3708 }
3403 3709
3404 3710 if ( $throw ) {
3405 3711 /* translators: Plugin name to deactivate. */
3406 - 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
3712 + throw new RuntimeException( sprintf( __( 'Jetpack contains the most recent version of the old "%1$s" plugin.', 'jetpack' ), 'WordPress.com Stats' ) );
3407 3713 }
3408 3714 }
3409 3715 }
3410 3716
@@ -3486,8 +3792,9 @@
3486 3792 /**
3487 3793 * Handler for Jetpack remote file uploads.
3488 3794 *
3489 3795 * @access public
3796 + * @return never
3490 3797 */
3491 3798 public function remote_request_handlers() {
3492 3799 switch ( current_filter() ) {
3493 3800 case 'wp_ajax_nopriv_jetpack_upload_file':
@@ -3514,18 +3821,17 @@
3514 3821 if ( ! is_int( $status_code ) ) {
3515 3822 $status_code = 400;
3516 3823 }
3517 3824
3518 - status_header( $status_code );
3519 - die( wp_json_encode( (object) compact( 'error', 'error_description' ) ) );
3825 + wp_send_json( (object) compact( 'error', 'error_description' ), $status_code, JSON_UNESCAPED_SLASHES );
3520 3826 }
3521 3827
3522 - status_header( 200 );
3523 3828 if ( true === $response ) {
3524 - exit;
3829 + status_header( 200 );
3830 + exit( 0 );
3525 3831 }
3526 3832
3527 - die( wp_json_encode( (object) $response ) );
3833 + wp_send_json( (object) $response, 200, JSON_UNESCAPED_SLASHES );
3528 3834 }
3529 3835
3530 3836 /**
3531 3837 * Uploads a file gotten from the global $_FILES.
@@ -3533,9 +3839,9 @@
3533 3839 * the attachment file of the media item (gotten through of the post_id)
3534 3840 * will be updated instead of add a new one.
3535 3841 *
3536 3842 * @param boolean $update_media_item - update media attachment.
3537 - * @return array - An array describing the uploadind files process.
3843 + * @return array|WP_Error - An array describing the uploading files process.
3538 3844 */
3539 3845 public function upload_handler( $update_media_item = false ) {
3540 3846 if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' !== strtoupper( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) ) ) {
3541 3847 return new WP_Error( 405, get_status_header_desc( 405 ), 405 );
@@ -3586,9 +3892,9 @@
3586 3892 return new WP_Error( 'handler_cannot_upload', __( 'The upload handler cannot upload files', 'jetpack' ), 400 );
3587 3893 }
3588 3894
3589 3895 $uploaded_files = array();
3590 - $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
3896 + $global_post = $GLOBALS['post'] ?? null;
3591 3897 unset( $GLOBALS['post'] );
3592 3898 if ( empty( $_FILES['media']['name'] ) ) {
3593 3899 // Nothing to process, just return.
3594 3900 return $uploaded_files;
@@ -3595,9 +3901,9 @@
3595 3901 }
3596 3902 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.
3597 3903 $file = array();
3598 3904 foreach ( $media_keys as $media_key ) {
3599 - $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.
3905 + $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.
3600 3906 }
3601 3907
3602 3908 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.
3603 3909
@@ -3643,9 +3949,9 @@
3643 3949 'type' => (string) $edited_media_item->post_mime_type,
3644 3950 'meta' => (array) wp_get_attachment_metadata( $post_id ),
3645 3951 );
3646 3952
3647 - return (array) array( $response );
3953 + return array( $response );
3648 3954 }
3649 3955
3650 3956 $attachment_id = media_handle_upload(
3651 3957 '.jetpack.upload.',
@@ -3684,67 +3990,8 @@
3684 3990 return $uploaded_files;
3685 3991 }
3686 3992
3687 3993 /**
3688 - * Add help to the Jetpack page
3689 - *
3690 - * @since Jetpack (1.2.3)
3691 - * @return void
3692 - */
3693 - public function admin_help() {
3694 - $current_screen = get_current_screen();
3695 -
3696 - // Overview.
3697 - $current_screen->add_help_tab(
3698 - array(
3699 - 'id' => 'home',
3700 - 'title' => __( 'Home', 'jetpack' ),
3701 - 'content' =>
3702 - '<p><strong>' . __( 'Jetpack', 'jetpack' ) . '</strong></p>' .
3703 - '<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' .
3704 - '<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>',
3705 - )
3706 - );
3707 -
3708 - // Screen Content.
3709 - if ( current_user_can( 'manage_options' ) ) {
3710 - $current_screen->add_help_tab(
3711 - array(
3712 - 'id' => 'settings',
3713 - 'title' => __( 'Settings', 'jetpack' ),
3714 - 'content' =>
3715 - '<p><strong>' . __( 'Jetpack', 'jetpack' ) . '</strong></p>' .
3716 - '<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' .
3717 - '<ol>' .
3718 - '<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' .
3719 - '<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>' .
3720 - '</ol>' .
3721 - '<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>',
3722 - )
3723 - );
3724 - }
3725 -
3726 - // Help Sidebar.
3727 - $support_url = Redirect::get_url( 'jetpack-support' );
3728 - $faq_url = Redirect::get_url( 'jetpack-faq' );
3729 - $current_screen->set_help_sidebar(
3730 - '<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' .
3731 - '<p><a href="' . esc_url( $faq_url ) . '" rel="noopener noreferrer" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' .
3732 - '<p><a href="' . esc_url( $support_url ) . '" rel="noopener noreferrer" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' .
3733 - '<p><a href="' . esc_url( self::admin_url( array( 'page' => 'jetpack-debugger' ) ) ) . '">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>'
3734 - );
3735 - }
3736 -
3737 - /**
3738 - * Enqueues the jetpack-icons style.
3739 - *
3740 - * @return void
3741 - */
3742 - public function admin_menu_css() {
3743 - wp_enqueue_style( 'jetpack-icons' );
3744 - }
3745 -
3746 - /**
3747 3994 * Add action links for the Jetpack plugin.
3748 3995 *
3749 3996 * @param array $actions Plugin actions.
3750 3997 *
@@ -3750,14 +3997,11 @@
3750 3997 *
3751 3998 * @return array
3752 3999 */
3753 4000 public function plugin_action_links( $actions ) {
3754 - $support_link = ( new Host() )->is_woa_site() ? 'https://wordpress.com/help/contact/' : self::admin_url( 'page=jetpack-debugger' );
3755 -
3756 4001 if ( current_user_can( 'jetpack_manage_modules' ) && ( self::is_connection_ready() || ( new Status() )->is_offline_mode() ) ) {
3757 4002 return array_merge(
3758 - array( 'settings' => sprintf( '<a href="%s">%s</a>', esc_url( self::admin_url( 'page=jetpack#/settings' ) ), __( 'Settings', 'jetpack' ) ) ),
3759 - array( 'support' => sprintf( '<a href="%s">%s</a>', esc_url( $support_link ), __( 'Support', 'jetpack' ) ) ),
4003 + array( 'settings' => sprintf( '<a href="%s">%s</a>', esc_url( self::admin_url( 'page=jetpack-settings#/settings' ) ), __( 'Settings', 'jetpack' ) ) ),
3760 4004 $actions
3761 4005 );
3762 4006 }
3763 4007
@@ -3764,53 +4008,8 @@
3764 4008 return $actions;
3765 4009 }
3766 4010
3767 4011 /**
3768 - * Add an activation modal to the plugins page and the main dashboard.
3769 - *
3770 - * @param string $hook The current admin page.
3771 - *
3772 - * @return void
3773 - */
3774 - public function activate_dialog( $hook ) {
3775 - /*
3776 - * We do not need it on other plugin pages,
3777 - * or when Jetpack is already connected.
3778 - */
3779 - if (
3780 - ! in_array( $hook, array( 'plugins.php', 'index.php' ), true )
3781 - || self::is_connection_ready()
3782 - ) {
3783 - return;
3784 - }
3785 -
3786 - // add an activation script that will pick up deactivation actions for the Jetpack plugin.
3787 - Assets::register_script(
3788 - 'jetpack-full-activation-modal-js',
3789 - '_inc/build/activation-modal.js',
3790 - JETPACK__PLUGIN_FILE,
3791 - array(
3792 - 'enqueue' => true,
3793 - 'in_footer' => true,
3794 - 'textdomain' => 'jetpack',
3795 - 'dependencies' => array(
3796 - 'wp-polyfill',
3797 - 'wp-components',
3798 - ),
3799 - )
3800 - );
3801 -
3802 - // Add objects to be passed to the initial state of the app.
3803 - // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
3804 - 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' );
3805 -
3806 - // Adds Connection package initial state.
3807 - Connection_Initial_State::render_script( 'jetpack-full-activation-modal-js' );
3808 -
3809 - add_action( 'admin_notices', array( $this, 'jetpack_plugin_portal_containers' ) );
3810 - }
3811 -
3812 - /**
3813 4012 * Adds the deactivation warning modal for Jetpack.
3814 4013 *
3815 4014 * @param string $hook The current admin page.
3816 4015 *
@@ -3830,14 +4029,10 @@
3830 4029 'jetpack-plugins-page-js',
3831 4030 '_inc/build/plugins-page.js',
3832 4031 JETPACK__PLUGIN_FILE,
3833 4032 array(
3834 - 'in_footer' => true,
3835 - 'textdomain' => 'jetpack',
3836 - 'dependencies' => array(
3837 - 'wp-polyfill',
3838 - 'wp-components',
3839 - ),
4033 + 'in_footer' => true,
4034 + 'textdomain' => 'jetpack',
3840 4035 )
3841 4036 );
3842 4037 Assets::enqueue_script( 'jetpack-plugins-page-js' );
3843 4038
@@ -3842,9 +4037,9 @@
3842 4037 Assets::enqueue_script( 'jetpack-plugins-page-js' );
3843 4038
3844 4039 // Add objects to be passed to the initial state of the app.
3845 4040 // Use wp_add_inline_script instead of wp_localize_script, see https://core.trac.wordpress.org/ticket/25280.
3846 - 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' );
4041 + 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' );
3847 4042
3848 4043 add_action( 'admin_footer', array( $this, 'jetpack_plugin_portal_containers' ) );
3849 4044 }
3850 4045 }
@@ -3893,9 +4088,9 @@
3893 4088 // @todo provide way to go to specific calypso env.
3894 4089 self::get_calypso_host() . 'jetpack/connect'
3895 4090 )
3896 4091 );
3897 - exit;
4092 + exit( 0 );
3898 4093 }
3899 4094 }
3900 4095
3901 4096 /*
@@ -3930,8 +4125,28 @@
3930 4125 * Done!
3931 4126 */
3932 4127
3933 4128 /**
4129 + * Build the user-facing description stored alongside a registration error code.
4130 + *
4131 + * @since 16.2
4132 + *
4133 + * @param string $error_code The WP_Error code.
4134 + * @param string $message The WP_Error message.
4135 + * @return string The description, empty when the message is not user-facing copy.
4136 + */
4137 + public static function get_registration_error_description( $error_code, $message ) {
4138 + // Manager::validate_remote_register_response() does not always put user-facing copy in the
4139 + // message slot: wpcom_5??, wpcom_408 and wpcom_bad_response store the HTTP status there,
4140 + // and jetpack_id stores the raw response body, which can also overflow the state cookie.
4141 + if ( 'jetpack_id' === $error_code || is_numeric( $message ) ) {
4142 + return '';
4143 + }
4144 +
4145 + return mb_substr( (string) $message, 0, 250 );
4146 + }
4147 +
4148 + /**
3934 4149 * Handles the page load events for the Jetpack admin page
3935 4150 */
3936 4151 public function admin_page_load() {
3937 4152 $error = false;
@@ -3967,10 +4182,11 @@
3967 4182 $registered = static::connection()->try_registration();
3968 4183 if ( is_wp_error( $registered ) ) {
3969 4184 $error = $registered->get_error_code();
3970 4185 self::state( 'error', $error );
3971 - self::state( 'error', $registered->get_error_message() );
3972 4186
4187 + self::state( 'error_description', self::get_registration_error_description( $error, $registered->get_error_message() ) );
4188 +
3973 4189 /**
3974 4190 * Jetpack registration Error.
3975 4191 *
3976 4192 * @since 7.5.0
@@ -3994,12 +4210,8 @@
3994 4210 do_action( 'jetpack_connection_register_success', $from );
3995 4211
3996 4212 $url = $this->build_connect_url( true, $redirect, $from );
3997 4213
3998 - if ( ! empty( $_GET['onboarding'] ) ) {
3999 - $url = add_query_arg( 'onboarding', rawurlencode_deep( wp_unslash( $_GET['onboarding'] ) ), $url ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
4000 - }
4001 -
4002 4214 if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) {
4003 4215 $url = add_query_arg( 'auth_approved', 'true', $url );
4004 4216 }
4005 4217
@@ -4004,9 +4216,9 @@
4004 4216 }
4005 4217
4006 4218 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
4007 4219 wp_safe_redirect( $url );
4008 - exit;
4220 + exit( 0 );
4009 4221 case 'activate':
4010 4222 if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
4011 4223 $error = 'cheatin';
4012 4224 break;
@@ -4020,9 +4232,9 @@
4020 4232 self::state( 'error', sprintf( __( 'Could not activate %s', 'jetpack' ), $module ) );
4021 4233 }
4022 4234 // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
4023 4235 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4024 - exit;
4236 + exit( 0 );
4025 4237 case 'activate_default_modules':
4026 4238 check_admin_referer( 'activate_default_modules' );
4027 4239 self::log( 'activate_default_modules' );
4028 4240 self::restate();
@@ -4030,9 +4242,9 @@
4030 4242 $max_version = isset( $_GET['max_version'] ) ? sanitize_text_field( wp_unslash( $_GET['max_version'] ) ) : false;
4031 4243 $other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_GET['other_modules'] ) ) : array();
4032 4244 self::activate_default_modules( $min_version, $max_version, $other_modules );
4033 4245 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4034 - exit;
4246 + exit( 0 );
4035 4247 case 'disconnect':
4036 4248 if ( ! current_user_can( 'jetpack_disconnect' ) ) {
4037 4249 $error = 'cheatin';
4038 4250 break;
@@ -4041,9 +4253,9 @@
4041 4253 check_admin_referer( 'jetpack-disconnect' );
4042 4254 self::log( 'disconnect' );
4043 4255 self::disconnect();
4044 4256 wp_safe_redirect( self::admin_url( 'disconnected=true' ) );
4045 - exit;
4257 + exit( 0 );
4046 4258 case 'reconnect':
4047 4259 if ( ! current_user_can( 'jetpack_reconnect' ) ) {
4048 4260 $error = 'cheatin';
4049 4261 break;
@@ -4054,9 +4266,9 @@
4054 4266 self::disconnect();
4055 4267
4056 4268 add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
4057 4269 wp_safe_redirect( $this->build_connect_url( true, false, 'reconnect' ) );
4058 - exit;
4270 + exit( 0 );
4059 4271 case 'deactivate':
4060 4272 if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
4061 4273 $error = 'cheatin';
4062 4274 break;
@@ -4070,9 +4282,9 @@
4070 4282 self::state( 'message', 'module_deactivated' );
4071 4283 }
4072 4284 self::state( 'module', $modules );
4073 4285 wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4074 - exit;
4286 + exit( 0 );
4075 4287 case 'unlink':
4076 4288 $redirect = isset( $_GET['redirect'] ) ? sanitize_text_field( wp_unslash( $_GET['redirect'] ) ) : '';
4077 4289 check_admin_referer( 'jetpack-unlink' );
4078 4290 self::log( 'unlink' );
@@ -4082,32 +4294,9 @@
4082 4294 wp_safe_redirect( admin_url() );
4083 4295 } else {
4084 4296 wp_safe_redirect( self::admin_url( array( 'page' => rawurlencode( $redirect ) ) ) );
4085 4297 }
4086 - exit;
4087 - case 'onboard':
4088 - if ( ! current_user_can( 'manage_options' ) ) {
4089 - wp_safe_redirect( self::admin_url( 'page=jetpack' ) );
4090 - } else {
4091 - self::create_onboarding_token();
4092 - $url = $this->build_connect_url( true );
4093 -
4094 - $token = Jetpack_Options::get_option( 'onboarding' );
4095 -
4096 - if ( false !== ( $token ) ) {
4097 - $url = add_query_arg( 'onboarding', $token, $url );
4098 - }
4099 -
4100 - $calypso_env = ( new Host() )->get_calypso_env();
4101 - if ( ! empty( $calypso_env ) ) {
4102 - $url = add_query_arg( 'calypso_env', $calypso_env, $url );
4103 - }
4104 -
4105 - add_filter( 'allowed_redirect_hosts', array( Host::class, 'allow_wpcom_environments' ) );
4106 - wp_safe_redirect( $url );
4107 - exit;
4108 - }
4109 - exit;
4298 + exit( 0 );
4110 4299 default:
4111 4300 /**
4112 4301 * Fires when a Jetpack admin page is loaded with an unrecognized parameter.
4113 4302 *
@@ -4330,37 +4519,8 @@
4330 4519 endif;
4331 4520 }
4332 4521
4333 4522 /**
4334 - * We can't always respond to a signed XML-RPC request with a
4335 - * helpful error message. In some circumstances, doing so could
4336 - * leak information.
4337 - *
4338 - * Instead, track that the error occurred via a Jetpack_Option,
4339 - * and send that data back in the heartbeat.
4340 - * All this does is increment a number, but it's enough to find
4341 - * trends.
4342 - *
4343 - * @param WP_Error $xmlrpc_error The error produced during
4344 - * signature validation.
4345 - */
4346 - public function track_xmlrpc_error( $xmlrpc_error ) {
4347 - $code = is_wp_error( $xmlrpc_error )
4348 - ? $xmlrpc_error->get_error_code()
4349 - : 'should-not-happen';
4350 -
4351 - $xmlrpc_errors = Jetpack_Options::get_option( 'xmlrpc_errors', array() );
4352 - if ( isset( $xmlrpc_errors[ $code ] ) && $xmlrpc_errors[ $code ] ) {
4353 - // No need to update the option if we already have
4354 - // this code stored.
4355 - return;
4356 - }
4357 - $xmlrpc_errors[ $code ] = true;
4358 -
4359 - Jetpack_Options::update_option( 'xmlrpc_errors', $xmlrpc_errors, false );
4360 - }
4361 -
4362 - /**
4363 4523 * Initialize the jetpack stats instance only when needed
4364 4524 *
4365 4525 * @return void
4366 4526 */
@@ -4483,9 +4643,9 @@
4483 4643 return $this->build_connect_url( $raw, $redirect, $from, true );
4484 4644 }
4485 4645 }
4486 4646
4487 - $url = static::build_authorize_url( $redirect );
4647 + $url = ( new Authorize_Redirect( static::connection() ) )->build_authorize_url( $redirect );
4488 4648 }
4489 4649
4490 4650 if ( $from ) {
4491 4651 $url = add_query_arg( 'from', $from, $url );
@@ -4510,66 +4670,30 @@
4510 4670 * @param null $deprecated Deprecated since Jetpack 10.9.
4511 4671 *
4512 4672 * @todo Update default value for redirect since the called function expects a string.
4513 4673 *
4674 + * @deprecated 13.4
4675 + *
4514 4676 * @return mixed|void
4515 4677 */
4516 4678 public static function build_authorize_url( $redirect = false, $deprecated = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
4679 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::build_authorize_url' );
4517 4680
4518 - add_filter( 'jetpack_connect_request_body', array( __CLASS__, 'filter_connect_request_body' ) );
4519 - add_filter( 'jetpack_connect_redirect_url', array( __CLASS__, 'filter_connect_redirect_url' ) );
4520 -
4521 - $c8n = self::connection();
4522 - $url = $c8n->get_authorization_url( wp_get_current_user(), $redirect );
4523 -
4524 - remove_filter( 'jetpack_connect_request_body', array( __CLASS__, 'filter_connect_request_body' ) );
4525 - remove_filter( 'jetpack_connect_redirect_url', array( __CLASS__, 'filter_connect_redirect_url' ) );
4526 -
4527 - /**
4528 - * Filter the URL used when authorizing a user to a WordPress.com account.
4529 - *
4530 - * @since 8.9.0
4531 - *
4532 - * @param string $url Connection URL.
4533 - */
4534 - return apply_filters( 'jetpack_build_authorize_url', $url );
4681 + return ( new Authorize_Redirect( static::connection() ) )->build_authorize_url( $redirect );
4535 4682 }
4536 4683
4537 4684 /**
4538 4685 * Filters the connection URL parameter array.
4539 4686 *
4687 + * @deprecated 13.4
4688 + *
4540 4689 * @param array $args default URL parameters used by the package.
4541 4690 * @return array the modified URL arguments array.
4542 4691 */
4543 4692 public static function filter_connect_request_body( $args ) {
4544 - if (
4545 - Constants::is_defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' )
4546 - && include_once Constants::get_constant( 'JETPACK__GLOTPRESS_LOCALES_PATH' )
4547 - ) {
4548 - $gp_locale = GP_Locales::by_field( 'wp_locale', get_locale() );
4549 - $args['locale'] = isset( $gp_locale ) && isset( $gp_locale->slug )
4550 - ? $gp_locale->slug
4551 - : '';
4552 - }
4693 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::filter_connect_request_body' );
4553 4694
4554 - $tracking = new Tracking();
4555 - $tracks_identity = $tracking->tracks_get_identity( $args['state'] );
4556 -
4557 - $args = array_merge(
4558 - $args,
4559 - array(
4560 - '_ui' => $tracks_identity['_ui'],
4561 - '_ut' => $tracks_identity['_ut'],
4562 - )
4563 - );
4564 -
4565 - $calypso_env = ( new Host() )->get_calypso_env();
4566 -
4567 - if ( ! empty( $calypso_env ) ) {
4568 - $args['calypso_env'] = $calypso_env;
4569 - }
4570 -
4571 - return $args;
4695 + return Authorize_Redirect::filter_connect_request_body( $args );
4572 4696 }
4573 4697
4574 4698 /**
4575 4699 * Filters the `jetpack/v4/connection/data` API response of the Connection package in order to
@@ -4606,41 +4730,19 @@
4606 4730 return $current_user_connection_data;
4607 4731 }
4608 4732
4609 4733 /**
4610 - * Filters the URL that will process the connection data. It can be different from the URL
4611 - * that we send the user to after everything is done.
4612 - *
4613 - * @param String $processing_url the default redirect URL used by the package.
4614 - * @return String the modified URL.
4615 - *
4616 - * @deprecated since Jetpack 9.5.0
4617 - */
4618 - public static function filter_connect_processing_url( $processing_url ) {
4619 - _deprecated_function( __METHOD__, 'jetpack-9.5' );
4620 -
4621 - $processing_url = admin_url( 'admin.php?page=jetpack' ); // Making PHPCS happy.
4622 - return $processing_url;
4623 - }
4624 -
4625 - /**
4626 4734 * Filters the redirection URL that is used for connect requests. The redirect
4627 4735 * URL should return the user back to the Jetpack console.
4628 4736 *
4737 + * @deprecated 13.4
4738 + *
4629 4739 * @param String $redirect the default redirect URL used by the package.
4630 4740 * @return String the modified URL.
4631 4741 */
4632 4742 public static function filter_connect_redirect_url( $redirect ) {
4633 - $jetpack_admin_page = esc_url_raw( admin_url( 'admin.php?page=jetpack' ) );
4634 - $redirect = $redirect
4635 - ? wp_validate_redirect( esc_url_raw( $redirect ), $jetpack_admin_page )
4636 - : $jetpack_admin_page;
4637 -
4638 - if ( isset( $_REQUEST['is_multisite'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- not making a site change here.
4639 - $redirect = Jetpack_Network::init()->get_url( 'network_admin_page' );
4640 - }
4641 -
4642 - return $redirect;
4743 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Authorize_Redirect::filter_connect_redirect_url' );
4744 + return Authorize_Redirect::filter_connect_redirect_url( $redirect );
4643 4745 }
4644 4746
4645 4747 /**
4646 4748 * This action fires at the beginning of the Manager::authorize method.
@@ -4704,11 +4806,8 @@
4704 4806 *
4705 4807 * @param array $data The request data.
4706 4808 */
4707 4809 public static function authorize_ending_authorized( $data ) {
4708 - // If this site has been through the Jetpack Onboarding flow, delete the onboarding token.
4709 - self::invalidate_onboarding_token();
4710 -
4711 4810 // If redirect_uri is SSO, ensure SSO module is enabled.
4712 4811 parse_str( wp_parse_url( $data['redirect_uri'], PHP_URL_QUERY ), $redirect_options );
4713 4812
4714 4813 /** This filter is documented in class.jetpack-cli.php */
@@ -4832,13 +4931,9 @@
4832 4931 *
4833 4932 * @return int 0 if the same sort or (+/-) to indicate which is greater.
4834 4933 */
4835 4934 public static function sort_modules( $a, $b ) {
4836 - if ( $a['sort'] === $b['sort'] ) {
4837 - return 0;
4838 - }
4839 -
4840 - return ( $a['sort'] < $b['sort'] ) ? -1 : 1;
4935 + return $a['sort'] <=> $b['sort'];
4841 4936 }
4842 4937
4843 4938 /**
4844 4939 * Recheck SSL status for use via an AJAX call.
@@ -4852,10 +4947,12 @@
4852 4947 $result = self::permit_ssl( true );
4853 4948 wp_send_json(
4854 4949 array(
4855 4950 'enabled' => $result,
4856 - 'message' => get_transient( 'jetpack_https_test_message' ),
4857 - )
4951 + 'message' => self::get_ssl_test_message(),
4952 + ),
4953 + null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- It takes null, but its phpdoc only says int.
4954 + JSON_UNESCAPED_SLASHES
4858 4955 );
4859 4956 }
4860 4957
4861 4958 /* Client API */
@@ -4862,8 +4959,10 @@
4862 4959
4863 4960 /**
4864 4961 * Verify the onboarding token.
4865 4962 *
4963 + * @deprecated since 13.9
4964 + *
4866 4965 * @param array $token_data Token data.
4867 4966 * @param string $token Token value.
4868 4967 * @param string $request_data JSON-encoded request data.
4869 4968 *
@@ -4869,8 +4968,9 @@
4869 4968 *
4870 4969 * @return mixed
4871 4970 */
4872 4971 public static function verify_onboarding_token( $token_data, $token, $request_data ) {
4972 + _deprecated_function( __METHOD__, '13.9' );
4873 4973 // Default to a blog token.
4874 4974 $token_type = 'blog';
4875 4975
4876 4976 // Let's see if this is onboarding. In such case, use user token type and the provided user id.
@@ -4898,9 +4998,9 @@
4898 4998 $jp_user = get_user_by( 'email', $jpo_user );
4899 4999 if ( is_a( $jp_user, 'WP_User' ) ) {
4900 5000 wp_set_current_user( $jp_user->ID );
4901 5001 $user_can = is_multisite()
4902 - ? current_user_can_for_blog( get_current_blog_id(), 'manage_options' )
5002 + ? current_user_can_for_site( get_current_blog_id(), 'manage_options' )
4903 5003 : current_user_can( 'manage_options' );
4904 5004 if ( $user_can ) {
4905 5005 $token_type = 'user';
4906 5006 $token->external_user_id = $jp_user->ID;
@@ -4917,11 +5017,13 @@
4917 5017
4918 5018 /**
4919 5019 * Create a random secret for validating onboarding payload
4920 5020 *
5021 + * @deprecated since 13.9
4921 5022 * @return string Secret token
4922 5023 */
4923 5024 public static function create_onboarding_token() {
5025 + _deprecated_function( __METHOD__, '13.9' );
4924 5026 $token = Jetpack_Options::get_option( 'onboarding' );
4925 5027 if ( false === ( $token ) ) {
4926 5028 $token = wp_generate_password( 32, false );
4927 5029 Jetpack_Options::update_option( 'onboarding', $token );
@@ -4932,11 +5034,13 @@
4932 5034
4933 5035 /**
4934 5036 * Remove the onboarding token
4935 5037 *
5038 + * @deprecated since 13.9
4936 5039 * @return bool True on success, false on failure
4937 5040 */
4938 5041 public static function invalidate_onboarding_token() {
5042 + _deprecated_function( __METHOD__, '13.9' );
4939 5043 return Jetpack_Options::delete_option( 'onboarding' );
4940 5044 }
4941 5045
4942 5046 /**
@@ -4941,8 +5045,10 @@
4941 5045
4942 5046 /**
4943 5047 * Validate an onboarding token for a specific action
4944 5048 *
5049 + * @deprecated since 13.9
5050 + *
4945 5051 * @param string $token Onboarding token.
4946 5052 * @param string $action Action name.
4947 5053 *
4948 5054 * @return boolean True if token/action pair is accepted, false if not
@@ -4947,8 +5053,9 @@
4947 5053 *
4948 5054 * @return boolean True if token/action pair is accepted, false if not
4949 5055 */
4950 5056 public static function validate_onboarding_token_action( $token, $action ) {
5057 + _deprecated_function( __METHOD__, '13.9' );
4951 5058 // Compare tokens, bail if tokens do not match.
4952 5059 if ( ! hash_equals( $token, Jetpack_Options::get_option( 'onboarding' ) ) ) {
4953 5060 return false;
4954 5061 }
@@ -4974,39 +5081,41 @@
4974 5081 * @return boolean
4975 5082 * @since 2.3.3
4976 5083 */
4977 5084 public static function permit_ssl( $force_recheck = false ) {
4978 - // Do some fancy tests to see if ssl is being supported.
4979 - if ( ! $force_recheck ) {
4980 - $ssl = get_transient( 'jetpack_https_test' );
5085 + if ( ! method_exists( Heartbeat::class, 'permit_ssl' ) ) {
5086 + // Skip the SSL-fail notice when the check cannot run.
5087 + return true;
4981 5088 }
4982 5089
4983 - if ( $force_recheck || false === $ssl ) {
4984 - $message = '';
4985 - if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) {
4986 - $ssl = 0;
4987 - } else {
4988 - $ssl = 1;
5090 + return Heartbeat::permit_ssl( $force_recheck );
5091 + }
4989 5092
4990 - if ( ! wp_http_supports( array( 'ssl' => true ) ) ) {
4991 - $ssl = 0;
4992 - $message = __( 'WordPress reports no SSL support', 'jetpack' );
4993 - } else {
4994 - $response = wp_remote_get( JETPACK__API_BASE . 'test/1/' );
4995 - if ( is_wp_error( $response ) ) {
4996 - $ssl = 0;
4997 - $message = __( 'WordPress reports no SSL support', 'jetpack' );
4998 - } elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) {
4999 - $ssl = 0;
5000 - $message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response );
5001 - }
5002 - }
5003 - }
5004 - set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS );
5005 - set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS );
5093 + /**
5094 + * Returns a localized message describing the last SSL connectivity failure, if any.
5095 + *
5096 + * The Connection package's canonical SSL check stores a neutral reason code; this maps it to
5097 + * a translated, `jetpack`-domain message for display in the admin notice and AJAX recheck.
5098 + *
5099 + * @since 16.1
5100 + *
5101 + * @return string The localized message, or an empty string when there is no failure.
5102 + */
5103 + public static function get_ssl_test_message() {
5104 + if ( ! method_exists( Heartbeat::class, 'get_ssl_test_error' ) ) {
5105 + return '';
5006 5106 }
5007 5107
5008 - return (bool) $ssl;
5108 + $error = Heartbeat::get_ssl_test_error();
5109 +
5110 + switch ( $error['code'] ) {
5111 + case 'no_ssl_support':
5112 + return __( 'WordPress reports no SSL support', 'jetpack' );
5113 + case 'bad_response':
5114 + return __( 'Response was not OK: ', 'jetpack' ) . $error['detail'];
5115 + default:
5116 + return '';
5117 + }
5009 5118 }
5010 5119
5011 5120 /**
5012 5121 * Displays an admin_notice, alerting the user that outbound SSL isn't working.
@@ -5025,9 +5134,9 @@
5025 5134 <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>
5026 5135 <p>
5027 5136 <?php esc_html_e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?>
5028 5137 <a href="#" id="jetpack-recheck-ssl-button"><?php esc_html_e( 'Try again', 'jetpack' ); ?></a>
5029 - <span id="jetpack-recheck-ssl-output"><?php echo esc_html( get_transient( 'jetpack_https_test_message' ) ); ?></span>
5138 + <span id="jetpack-recheck-ssl-output"><?php echo esc_html( self::get_ssl_test_message() ); ?></span>
5030 5139 </p>
5031 5140 <p>
5032 5141 <?php
5033 5142 printf(
@@ -5046,18 +5155,18 @@
5046 5155 <script type="text/javascript">
5047 5156 jQuery( document ).ready( function( $ ) {
5048 5157 $( '#jetpack-recheck-ssl-button' ).click( function( e ) {
5049 5158 var $this = $( this );
5050 - $this.html( <?php echo wp_json_encode( __( 'Checking', 'jetpack' ) ); ?> );
5159 + $this.html( <?php echo wp_json_encode( esc_html__( 'Checking', 'jetpack' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> );
5051 5160 $( '#jetpack-recheck-ssl-output' ).html( '' );
5052 5161 e.preventDefault();
5053 - var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': <?php echo wp_json_encode( $ajax_nonce ); ?> };
5162 + var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': <?php echo wp_json_encode( $ajax_nonce, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> };
5054 5163 $.post( ajaxurl, data )
5055 5164 .done( function( response ) {
5056 5165 if ( response.enabled ) {
5057 5166 $( '#jetpack-ssl-warning' ).hide();
5058 5167 } else {
5059 - this.html( <?php echo wp_json_encode( __( 'Try again', 'jetpack' ) ); ?> );
5168 + this.html( <?php echo wp_json_encode( esc_html__( 'Try again', 'jetpack' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ); ?> );
5060 5169 $( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message );
5061 5170 }
5062 5171 }.bind( $this ) );
5063 5172 } );
@@ -5083,26 +5192,8 @@
5083 5192 return $jetpack->connection_manager;
5084 5193 }
5085 5194
5086 5195 /**
5087 - * Creates two secret tokens and the end of life timestamp for them.
5088 - *
5089 - * Note these tokens are unique per call, NOT static per site for connecting.
5090 - *
5091 - * @deprecated 9.5 Use Automattic\Jetpack\Connection\Secrets->generate() instead.
5092 - *
5093 - * @since 2.6
5094 - * @param String $action The action name.
5095 - * @param Integer $user_id The user identifier.
5096 - * @param Integer $exp Expiration time in seconds.
5097 - * @return array
5098 - */
5099 - public static function generate_secrets( $action, $user_id = false, $exp = 600 ) {
5100 - _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\\Jetpack\\Connection\\Secrets->generate' );
5101 - return self::connection()->generate_secrets( $action, $user_id, $exp );
5102 - }
5103 -
5104 - /**
5105 5196 * Get verification secrets.
5106 5197 *
5107 5198 * @param string $action Action name.
5108 5199 * @param int $user_id User ID.
@@ -5123,35 +5214,8 @@
5123 5214 return $secrets;
5124 5215 }
5125 5216
5126 5217 /**
5127 - * Register a connection.
5128 - *
5129 - * @deprecated Jetpack 9.7.0
5130 - * @see Automattic\Jetpack\Connection\Manager::try_registration()
5131 - *
5132 - * @return bool|WP_Error
5133 - */
5134 - public static function register() {
5135 - _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Manager::try_registration' );
5136 - return static::connection()->try_registration( false );
5137 - }
5138 -
5139 - /**
5140 - * Filters the registration request body to include tracking properties.
5141 - *
5142 - * @deprecated Jetpack 9.7.0
5143 - * @see Automattic\Jetpack\Connection\Utils::filter_register_request_body()
5144 - *
5145 - * @param array $properties Token request properties.
5146 - * @return array amended properties.
5147 - */
5148 - public static function filter_register_request_body( $properties ) {
5149 - _deprecated_function( __METHOD__, 'jetpack-9.7', 'Automattic\\Jetpack\\Connection\\Utils::filter_register_request_body' );
5150 - return Connection_Utils::filter_register_request_body( $properties );
5151 - }
5152 -
5153 - /**
5154 5218 * Filters the token request body to include tracking properties.
5155 5219 *
5156 5220 * @param array $properties Token request properties.
5157 5221 *
@@ -5172,9 +5236,9 @@
5172 5236
5173 5237 /**
5174 5238 * If the db version is showing something other that what we've got now, bump it to current.
5175 5239 *
5176 - * @return bool: True if the option was incorrect and updated, false if nothing happened.
5240 + * @return bool True if the option was incorrect and updated, false if nothing happened.
5177 5241 */
5178 5242 public static function maybe_set_version_option() {
5179 5243 list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
5180 5244 if ( JETPACK__VERSION !== $version ) {
@@ -5192,21 +5256,8 @@
5192 5256
5193 5257 /* Client Server API */
5194 5258
5195 5259 /**
5196 - * Loads the Jetpack XML-RPC client.
5197 - * No longer necessary, as the XML-RPC client will be automagically loaded.
5198 - *
5199 - * Note: we cannot remove this function yet as it is used in this plugin:
5200 - * https://wordpress.org/plugins/jetpack-subscription-form/
5201 - *
5202 - * @deprecated since 7.7.0
5203 - */
5204 - public static function load_xml_rpc_client() {
5205 - _deprecated_function( __METHOD__, 'jetpack-7.7' );
5206 - }
5207 -
5208 - /**
5209 5260 * State is passed via cookies from one request to the next, but never to subsequent requests.
5210 5261 * SET: state( $key, $value );
5211 5262 * GET: $value = state( $key );
5212 5263 *
@@ -5280,20 +5331,8 @@
5280 5331
5281 5332 self::state( 'privacy_checks', $privacy_checks );
5282 5333 }
5283 5334
5284 - /**
5285 - * Serve a WordPress.com static resource via a randomized wp.com subdomain.
5286 - *
5287 - * @deprecated 9.3.0 Use Assets::staticize_subdomain.
5288 - *
5289 - * @param string $url WordPress.com static resource URL.
5290 - */
5291 - public static function staticize_subdomain( $url ) {
5292 - _deprecated_function( __METHOD__, 'jetpack-9.3.0', 'Automattic\Jetpack\Assets::staticize_subdomain' );
5293 - return Assets::staticize_subdomain( $url );
5294 - }
5295 -
5296 5335 /* JSON API Authorization */
5297 5336
5298 5337 /**
5299 5338 * Handles the login action for Authorizing the JSON API
@@ -5298,13 +5337,14 @@
5298 5337 /**
5299 5338 * Handles the login action for Authorizing the JSON API
5300 5339 */
5301 5340 public function login_form_json_api_authorization() {
5302 - $this->verify_json_api_authorization_request();
5341 + $authorize_json_api = new Authorize_Json_Api();
5342 + $authorize_json_api->verify_json_api_authorization_request();
5303 5343
5304 - add_action( 'wp_login', array( $this, 'store_json_api_authorization_token' ), 10, 2 );
5344 + add_action( 'wp_login', array( $authorize_json_api, 'store_json_api_authorization_token' ), 10, 2 );
5305 5345
5306 - add_action( 'login_message', array( $this, 'login_message_json_api_authorization' ) );
5346 + add_action( 'login_message', array( $authorize_json_api, 'login_message_json_api_authorization' ) );
5307 5347 add_action( 'login_form', array( $this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
5308 5348 add_filter( 'site_url', array( $this, 'post_login_form_to_signed_url' ), 10, 3 );
5309 5349 }
5310 5350
@@ -5342,16 +5382,17 @@
5342 5382
5343 5383 /**
5344 5384 * If someone logs in to approve API access, store the Access Code in usermeta.
5345 5385 *
5386 + * @deprecated 13.4
5387 + *
5346 5388 * @param string $user_login Unused.
5347 5389 * @param WP_User $user User logged in.
5348 5390 */
5349 5391 public function store_json_api_authorization_token( $user_login, $user ) {
5350 - add_filter( 'login_redirect', array( $this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
5351 - add_filter( 'allowed_redirect_hosts', array( $this, 'allow_wpcom_public_api_domain' ) );
5352 - $token = wp_generate_password( 32, false );
5353 - update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token );
5392 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::store_json_api_authorization_token' );
5393 +
5394 + return ( new Authorize_Json_Api() )->store_json_api_authorization_token( $user_login, $user );
5354 5395 }
5355 5396
5356 5397 /**
5357 5398 * Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
@@ -5357,23 +5398,29 @@
5357 5398 * Add public-api.wordpress.com to the safe redirect allowed list - only added when someone allows API access.
5358 5399 *
5359 5400 * To be used with a filter of allowed domains for a redirect.
5360 5401 *
5402 + * @deprecated 13.4
5403 + *
5361 5404 * @param array $domains Allowed WP.com Environments.
5362 5405 */
5363 5406 public function allow_wpcom_public_api_domain( $domains ) {
5364 - $domains[] = 'public-api.wordpress.com';
5365 - return $domains;
5407 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Status\\Host::allow_wpcom_public_api_domain' );
5408 +
5409 + return Host::allow_wpcom_public_api_domain( $domains );
5366 5410 }
5367 5411
5368 5412 /**
5369 5413 * Check if the redirect is encoded.
5370 5414 *
5415 + * @deprecated 13.4
5416 + *
5371 5417 * @param string $redirect_url Redirect URL.
5372 5418 *
5373 5419 * @return bool If redirect has been encoded.
5374 5420 */
5375 5421 public static function is_redirect_encoded( $redirect_url ) {
5422 + _deprecated_function( __METHOD__, 'jetpack-13.4' );
5376 5423 return preg_match( '/https?%3A%2F%2F/i', $redirect_url ) > 0;
5377 5424 }
5378 5425
5379 5426 /**
@@ -5391,8 +5438,10 @@
5391 5438
5392 5439 /**
5393 5440 * Add the Access Code details to the public-api.wordpress.com redirect.
5394 5441 *
5442 + * @deprecated 13.4
5443 + *
5395 5444 * @param string $redirect_to URL.
5396 5445 * @param string $original_redirect_to URL.
5397 5446 * @param WP_User $user WP_User for the redirect.
5398 5447 *
@@ -5398,23 +5447,18 @@
5398 5447 *
5399 5448 * @return string
5400 5449 */
5401 5450 public function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
5402 - return add_query_arg(
5403 - urlencode_deep(
5404 - array(
5405 - 'jetpack-code' => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], true ),
5406 - 'jetpack-user-id' => (int) $user->ID,
5407 - 'jetpack-state' => $this->json_api_authorization_request['state'],
5408 - )
5409 - ),
5410 - $redirect_to
5411 - );
5451 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::add_token_to_login_redirect_json_api_authorization' );
5452 +
5453 + return ( new Authorize_Json_Api() )->add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user );
5412 5454 }
5413 5455
5414 5456 /**
5415 5457 * Verifies the request by checking the signature
5416 5458 *
5459 + * @deprecated 13.4
5460 + *
5417 5461 * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow
5418 5462 * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO.
5419 5463 *
5420 5464 * @param null|array $environment Value to override $_REQUEST.
@@ -5419,194 +5463,24 @@
5419 5463 *
5420 5464 * @param null|array $environment Value to override $_REQUEST.
5421 5465 */
5422 5466 public function verify_json_api_authorization_request( $environment = null ) {
5423 - $environment = $environment === null
5424 - ? $_REQUEST // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce verification handled later in function.
5425 - : $environment;
5467 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::verify_json_api_authorization_request' );
5426 5468
5427 - list( $env_token,, $env_user_id ) = explode( ':', $environment['token'] );
5428 - $token = ( new Tokens() )->get_access_token( $env_user_id, $env_token );
5429 - if ( ! $token || empty( $token->secret ) ) {
5430 - wp_die( esc_html__( 'You must connect your Jetpack plugin to WordPress.com to use this feature.', 'jetpack' ) );
5431 - }
5432 -
5433 - $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' );
5434 -
5435 - // Host has encoded the request URL, probably as a result of a bad http => https redirect.
5436 - 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.
5437 - /**
5438 - * Jetpack authorisation request Error.
5439 - *
5440 - * @since 7.5.0
5441 - */
5442 - do_action( 'jetpack_verify_api_authorization_request_error_double_encode' );
5443 - $die_error = sprintf(
5444 - /* translators: %s is a URL */
5445 - __( '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' ),
5446 - esc_url( Redirect::get_url( 'jetpack-support-double-encoding' ) )
5447 - );
5448 - }
5449 -
5450 - $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
5451 -
5452 - if ( isset( $environment['jetpack_json_api_original_query'] ) ) {
5453 - $signature = $jetpack_signature->sign_request(
5454 - $environment['token'],
5455 - $environment['timestamp'],
5456 - $environment['nonce'],
5457 - '',
5458 - 'GET',
5459 - $environment['jetpack_json_api_original_query'],
5460 - null,
5461 - true
5462 - );
5463 - } else {
5464 - $signature = $jetpack_signature->sign_current_request(
5465 - array(
5466 - 'body' => null,
5467 - 'method' => 'GET',
5468 - )
5469 - );
5470 - }
5471 -
5472 - if ( ! $signature ) {
5473 - wp_die(
5474 - wp_kses(
5475 - $die_error,
5476 - array(
5477 - 'a' => array(
5478 - 'href' => array(),
5479 - ),
5480 - )
5481 - )
5482 - );
5483 - } elseif ( is_wp_error( $signature ) ) {
5484 - wp_die(
5485 - wp_kses(
5486 - $die_error,
5487 - array(
5488 - 'a' => array(
5489 - 'href' => array(),
5490 - ),
5491 - )
5492 - )
5493 - );
5494 - } elseif ( ! hash_equals( $signature, $environment['signature'] ) ) {
5495 - if ( is_ssl() ) {
5496 - // 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.
5497 - $signature = $jetpack_signature->sign_current_request(
5498 - array(
5499 - 'scheme' => 'http',
5500 - 'body' => null,
5501 - 'method' => 'GET',
5502 - )
5503 - );
5504 - if ( ! $signature || is_wp_error( $signature ) || ! hash_equals( $signature, $environment['signature'] ) ) {
5505 - wp_die(
5506 - wp_kses(
5507 - $die_error,
5508 - array(
5509 - 'a' => array(
5510 - 'href' => array(),
5511 - ),
5512 - )
5513 - )
5514 - );
5515 - }
5516 - } else {
5517 - wp_die(
5518 - wp_kses(
5519 - $die_error,
5520 - array(
5521 - 'a' => array(
5522 - 'href' => array(),
5523 - ),
5524 - )
5525 - )
5526 - );
5527 - }
5528 - }
5529 -
5530 - $timestamp = (int) $environment['timestamp'];
5531 - $nonce = stripslashes( (string) $environment['nonce'] );
5532 -
5533 - if ( ! $this->connection_manager ) {
5534 - $this->connection_manager = new Connection_Manager();
5535 - }
5536 -
5537 - if ( ! ( new Nonce_Handler() )->add( $timestamp, $nonce ) ) {
5538 - // De-nonce the nonce, at least for 5 minutes.
5539 - // 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).
5540 - $old_nonce_time = get_option( "jetpack_nonce_{$timestamp}_{$nonce}" );
5541 - if ( $old_nonce_time < time() - 300 ) {
5542 - wp_die( esc_html__( 'The authorization process expired. Please go back and try again.', 'jetpack' ) );
5543 - }
5544 - }
5545 -
5546 - $data = json_decode( base64_decode( stripslashes( $environment['data'] ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
5547 - $data_filters = array(
5548 - 'state' => 'opaque',
5549 - 'client_id' => 'int',
5550 - 'client_title' => 'string',
5551 - 'client_image' => 'url',
5552 - );
5553 -
5554 - foreach ( $data_filters as $key => $sanitation ) {
5555 - if ( ! isset( $data->$key ) ) {
5556 - wp_die(
5557 - wp_kses(
5558 - $die_error,
5559 - array(
5560 - 'a' => array(
5561 - 'href' => array(),
5562 - ),
5563 - )
5564 - )
5565 - );
5566 - }
5567 -
5568 - switch ( $sanitation ) {
5569 - case 'int':
5570 - $this->json_api_authorization_request[ $key ] = (int) $data->$key;
5571 - break;
5572 - case 'opaque':
5573 - $this->json_api_authorization_request[ $key ] = (string) $data->$key;
5574 - break;
5575 - case 'string':
5576 - $this->json_api_authorization_request[ $key ] = wp_kses( (string) $data->$key, array() );
5577 - break;
5578 - case 'url':
5579 - $this->json_api_authorization_request[ $key ] = esc_url_raw( (string) $data->$key );
5580 - break;
5581 - }
5582 - }
5583 -
5584 - if ( empty( $this->json_api_authorization_request['client_id'] ) ) {
5585 - wp_die(
5586 - wp_kses(
5587 - $die_error,
5588 - array(
5589 - 'a' => array(
5590 - 'href' => array(),
5591 - ),
5592 - )
5593 - )
5594 - );
5595 - }
5469 + return ( new Authorize_Json_Api() )->verify_json_api_authorization_request( $environment );
5596 5470 }
5597 5471
5598 5472 /**
5599 5473 * HTML for the JSON API authorization notice.
5600 5474 *
5475 + * @deprecated 13.4
5476 + *
5601 5477 * @return string
5602 5478 */
5603 5479 public function login_message_json_api_authorization() {
5604 - return '<p class="message">' . sprintf(
5605 - /* translators: Name/image of the client requesting authorization */
5606 - esc_html__( '%s wants to access your site’s data. Log in to authorize that access.', 'jetpack' ),
5607 - '<strong>' . esc_html( $this->json_api_authorization_request['client_title'] ) . '</strong>'
5608 - ) . '<img src="' . esc_url( $this->json_api_authorization_request['client_image'] ) . '" /></p>';
5480 + _deprecated_function( __METHOD__, 'jetpack-13.4', 'Automattic\\Jetpack\\Connection\\Authorize_Json_Api::login_message_json_api_authorization' );
5481 +
5482 + return ( new Authorize_Json_Api() )->login_message_json_api_authorization();
5609 5483 }
5610 5484
5611 5485 /**
5612 5486 * Get $content_width, but with a <s>twist</s> filter.
@@ -5650,28 +5524,20 @@
5650 5524
5651 5525 /**
5652 5526 * Checks if the site is currently in an identity crisis.
5653 5527 *
5528 + * Now delegates to the Connection package so this matches what the heartbeat itself reports.
5529 + * Note the package guards on `Connection\Manager::is_connected()` where this used to guard on
5530 + * `Jetpack::is_connection_ready()`, so the `jetpack_is_connection_ready` filter no longer applies.
5531 + *
5532 + * @deprecated 16.2
5533 + *
5654 5534 * @return array|bool Array of options that are in a crisis, or false if everything is OK.
5655 5535 */
5656 5536 public static function check_identity_crisis() {
5657 - if ( ! self::is_connection_ready() || ( new Status() )->is_offline_mode() || ! Identity_Crisis::validate_sync_error_idc_option() ) {
5658 - return false;
5659 - }
5660 - return Jetpack_Options::get_option( 'sync_error_idc' );
5661 - }
5537 + _deprecated_function( __METHOD__, 'jetpack-16.2', 'Automattic\\Jetpack\\Identity_Crisis::check_identity_crisis' );
5662 5538
5663 - /**
5664 - * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup.
5665 - *
5666 - * @since 4.4.0
5667 - * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC
5668 - *
5669 - * @return bool
5670 - */
5671 - public static function validate_sync_error_idc_option() {
5672 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\\Automattic\\Jetpack\\Identity_Crisis::validate_sync_error_idc_option' );
5673 - return Identity_Crisis::validate_sync_error_idc_option();
5539 + return Identity_Crisis::check_identity_crisis();
5674 5540 }
5675 5541
5676 5542 /**
5677 5543 * Normalizes a url by doing three things:
@@ -5695,35 +5561,8 @@
5695 5561 return $url;
5696 5562 }
5697 5563
5698 5564 /**
5699 - * Gets the value that is to be saved in the jetpack_sync_error_idc option.
5700 - *
5701 - * @since 4.4.0
5702 - * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB
5703 - * @deprecated 9.8.0 Use \\Automattic\\Jetpack\\Identity_Crisis::get_sync_error_idc_option
5704 - *
5705 - * @param array $response HTTP response.
5706 - * @return array Array of the local urls, wpcom urls, and error code
5707 - */
5708 - public static function get_sync_error_idc_option( $response = array() ) {
5709 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\\Automattic\\Jetpack\\Identity_Crisis::get_sync_error_idc_option' );
5710 - return Identity_Crisis::get_sync_error_idc_option( $response );
5711 - }
5712 -
5713 - /**
5714 - * Returns the value of the jetpack_sync_idc_optin filter, or constant.
5715 - * If set to true, the site will be put into staging mode.
5716 - *
5717 - * @since 4.3.2
5718 - * @return bool
5719 - */
5720 - public static function sync_idc_optin() {
5721 - _deprecated_function( __METHOD__, 'jetpack-9.8', '\\Automattic\\Jetpack\\Identity_Crisis::sync_idc_optin' );
5722 - return Identity_Crisis::sync_idc_optin();
5723 - }
5724 -
5725 - /**
5726 5565 * Maybe Use a .min.css stylesheet, maybe not.
5727 5566 *
5728 5567 * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args.
5729 5568 *
@@ -5744,9 +5583,9 @@
5744 5583 // Strip out the abspath.
5745 5584 $base = dirname( plugin_basename( $plugin ) );
5746 5585
5747 5586 // Short out on non-Jetpack assets.
5748 - if ( 'jetpack/' !== substr( $base, 0, 8 ) ) {
5587 + if ( ! str_starts_with( $base, 'jetpack/' ) ) {
5749 5588 return $url;
5750 5589 }
5751 5590
5752 5591 // File name parsing.
@@ -5786,15 +5625,15 @@
5786 5625 *
5787 5626 * @return mixed
5788 5627 */
5789 5628 public static function set_suffix_on_min( $src, $handle ) {
5790 - if ( false === strpos( $src, '.min.css' ) ) {
5629 + if ( ! is_string( $src ) || ! str_contains( $src, '.min.css' ) ) {
5791 5630 return $src;
5792 5631 }
5793 5632
5794 5633 if ( ! empty( self::$min_assets ) ) {
5795 5634 foreach ( self::$min_assets as $file => $path ) {
5796 - if ( false !== strpos( $src, $file ) ) {
5635 + if ( str_contains( $src, $file ) ) {
5797 5636 wp_style_add_data( $handle, 'suffix', '.min' );
5798 5637 return $src;
5799 5638 }
5800 5639 }
@@ -5824,8 +5663,10 @@
5824 5663 *
5825 5664 * Data passed in with the $data parameter will be available in the
5826 5665 * template file as $data['value']
5827 5666 *
5667 + * @html-template-var array $data
5668 + *
5828 5669 * @param string $template - Template file to load.
5829 5670 * @param array $data - Any data to pass along to the template.
5830 5671 * @return boolean - If template file was found.
5831 5672 **/
@@ -5843,8 +5684,19 @@
5843 5684 return false;
5844 5685 }
5845 5686
5846 5687 /**
5688 + * Register Jetpack-specific tests on the connection package's health test suite.
5689 + *
5690 + * @param \Automattic\Jetpack\Connection\Connection_Health_Tests $connection_tests The test suite instance.
5691 + */
5692 + public function register_jetpack_connection_tests( $connection_tests ) {
5693 + require_once JETPACK__PLUGIN_DIR . '_inc/lib/debugger/class-jetpack-cxn-tests.php';
5694 + $jetpack_tests = new Jetpack_Cxn_Tests();
5695 + $jetpack_tests->register_tests_on( $connection_tests );
5696 + }
5697 +
5698 + /**
5847 5699 * Throws warnings for deprecated hooks to be removed from Jetpack that cannot remain in the original place in the code.
5848 5700 */
5849 5701 public function deprecated_hooks() {
5850 5702 $filter_deprecated_list = array(
@@ -6060,8 +5912,18 @@
6060 5912 'jetpack_subscriptions_modal_enabled' => array(
6061 5913 'replacement' => null,
6062 5914 'version' => 'jetpack-12.7.0',
6063 5915 ),
5916 + 'jetpack_pre_connection_prompt_helpers' => array(
5917 + 'replacement' => null,
5918 + 'version' => 'jetpack-13.2.0',
5919 + ),
5920 + 'jetpack_contact_form_use_package' => array(
5921 + 'replacement' => null,
5922 + 'version' => 'jetpack-13.4.0',
5923 + ),
5924 + // jetpack_implode_frontend_css has been removed, but is not listed here. The updated behavior is exactly the only use of the filter.
5925 + // We can reassess formally deprecating it here later; for now, it would be noise with no functional difference.
6064 5926 );
6065 5927
6066 5928 foreach ( $filter_deprecated_list as $tag => $args ) {
6067 5929 if ( has_filter( $tag ) ) {
@@ -6164,9 +6026,9 @@
6164 6026 foreach ( $matches as $match ) {
6165 6027 $url = trim( $match['path'], "'\" \t" );
6166 6028
6167 6029 // If this is a data url, we don't want to mess with it.
6168 - if ( 'data:' === substr( $url, 0, 5 ) ) {
6030 + if ( str_starts_with( $url, 'data:' ) ) {
6169 6031 continue;
6170 6032 }
6171 6033
6172 6034 // If this is an absolute or protocol-agnostic url,
@@ -6192,113 +6054,8 @@
6192 6054 return $css;
6193 6055 }
6194 6056
6195 6057 /**
6196 - * This methods removes all of the registered css files on the front end
6197 - * from Jetpack in favor of using a single file. In effect "imploding"
6198 - * all the files into one file.
6199 - *
6200 - * Pros:
6201 - * - Uses only ONE css asset connection instead of 15
6202 - * - Saves a minimum of 56k
6203 - * - Reduces server load
6204 - * - Reduces time to first painted byte
6205 - *
6206 - * Cons:
6207 - * - Loads css for ALL modules. However all selectors are prefixed so it
6208 - * should not cause any issues with themes.
6209 - * - Plugins/themes dequeuing styles no longer do anything. See
6210 - * jetpack_implode_frontend_css filter for a workaround
6211 - *
6212 - * For some situations developers may wish to disable css imploding and
6213 - * instead operate in legacy mode where each file loads seperately and
6214 - * can be edited individually or dequeued. This can be accomplished with
6215 - * the following line:
6216 - *
6217 - * add_filter( 'jetpack_implode_frontend_css', '__return_false' );
6218 - *
6219 - * @param bool $travis_test Is this a test run.
6220 - *
6221 - * @since 3.2
6222 - */
6223 - public function implode_frontend_css( $travis_test = false ) {
6224 - $do_implode = true;
6225 - if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
6226 - $do_implode = false;
6227 - }
6228 -
6229 - // Do not implode CSS when the page loads via the AMP plugin.
6230 - if ( class_exists( Jetpack_AMP_Support::class ) && Jetpack_AMP_Support::is_amp_request() ) {
6231 - $do_implode = false;
6232 - }
6233 -
6234 - /**
6235 - * Allow CSS to be concatenated into a single jetpack.css file.
6236 - *
6237 - * @since 3.2.0
6238 - *
6239 - * @param bool $do_implode Should CSS be concatenated? Default to true.
6240 - */
6241 - $do_implode = apply_filters( 'jetpack_implode_frontend_css', $do_implode );
6242 -
6243 - // Do not use the imploded file when default behavior was altered through the filter.
6244 - if ( ! $do_implode ) {
6245 - return;
6246 - }
6247 -
6248 - // We do not want to use the imploded file in dev mode, or if not connected.
6249 - if ( ( new Status() )->is_offline_mode() || ! self::is_connection_ready() ) {
6250 - if ( ! $travis_test ) {
6251 - return;
6252 - }
6253 - }
6254 -
6255 - // Do not use the imploded file if sharing css was dequeued via the sharing settings screen.
6256 - if ( get_option( 'sharedaddy_disable_resources' ) ) {
6257 - return;
6258 - }
6259 -
6260 - /*
6261 - * Now we assume Jetpack is connected and able to serve the single
6262 - * file.
6263 - *
6264 - * In the future there will be a check here to serve the file locally
6265 - * or potentially from the Jetpack CDN
6266 - *
6267 - * For now:
6268 - * - Enqueue a single imploded css file
6269 - * - Zero out the style_loader_tag for the bundled ones
6270 - * - Be happy, drink scotch
6271 - */
6272 -
6273 - add_filter( 'style_loader_tag', array( $this, 'concat_remove_style_loader_tag' ), 10, 2 );
6274 -
6275 - $version = self::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . 'css/jetpack.css' ) : JETPACK__VERSION;
6276 -
6277 - wp_enqueue_style( 'jetpack_css', plugins_url( 'css/jetpack.css', __FILE__ ), array(), $version );
6278 - wp_style_add_data( 'jetpack_css', 'rtl', 'replace' );
6279 - }
6280 -
6281 - /**
6282 - * Removes styles that are part of concatenated group.
6283 - *
6284 - * @param string $tag Style tag.
6285 - * @param string $handle Style handle.
6286 - *
6287 - * @return string
6288 - */
6289 - public function concat_remove_style_loader_tag( $tag, $handle ) {
6290 - if ( in_array( $handle, $this->concatenated_style_handles, true ) ) {
6291 - $tag = '';
6292 - if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
6293 - $tag = '<!-- `' . esc_html( $handle ) . "` is included in the concatenated jetpack.css -->\r\n";
6294 - }
6295 - }
6296 -
6297 - return $tag;
6298 - }
6299 -
6300 - /**
6301 6058 * Check the heartbeat data
6302 6059 *
6303 6060 * Organizes the heartbeat data by severity. For example, if the site
6304 6061 * is in an ID crisis, it will be in the $filtered_data['bad'] array.
@@ -6310,9 +6067,23 @@
6310 6067 *
6311 6068 * $return array $filtered_data
6312 6069 */
6313 6070 public static function jetpack_check_heartbeat_data() {
6314 - $raw_data = Jetpack_Heartbeat::generate_stats_array();
6071 + /*
6072 + * Site environment stats (incl. wp-version/php-version checked below) now live in the Connection package,
6073 + * and the IDC stat is contributed by the Connection package's `jetpack_heartbeat_stats_array` filter
6074 + * callback. We rebuild the stat here rather than running that filter: the filter's callbacks have side
6075 + * effects (Connection\Manager::add_stats_to_heartbeat() consumes and deletes the `xmlrpc_errors` option)
6076 + * and return non-scalar values, neither of which is appropriate for this read-only diagnostic.
6077 + */
6078 + $env_stats = method_exists( Heartbeat::class, 'get_environment_stats' )
6079 + ? Heartbeat::get_environment_stats()
6080 + : array();
6081 + $raw_data = array_merge(
6082 + Jetpack_Heartbeat::generate_stats_array(),
6083 + $env_stats,
6084 + array( 'identitycrisis' => Identity_Crisis::check_identity_crisis() ? 'yes' : 'no' )
6085 + );
6315 6086
6316 6087 $good = array();
6317 6088 $caution = array();
6318 6089 $bad = array();
@@ -6378,23 +6149,8 @@
6378 6149 return Jetpack_Options::get_options_for_reset();
6379 6150 }
6380 6151
6381 6152 /**
6382 - * Strip http:// or https:// from a url, replaces forward slash with ::,
6383 - * so we can bring them directly to their site in calypso.
6384 - *
6385 - * @deprecated 9.2.0 Use Automattic\Jetpack\Status::get_site_suffix
6386 - *
6387 - * @param string $url URL.
6388 - * @return string url without the guff.
6389 - */
6390 - public static function build_raw_urls( $url ) {
6391 - _deprecated_function( __METHOD__, 'jetpack-9.2.0', 'Automattic\Jetpack\Status::get_site_suffix' );
6392 -
6393 - return ( new Status() )->get_site_suffix( $url );
6394 - }
6395 -
6396 - /**
6397 6153 * Get the user's meta box order.
6398 6154 *
6399 6155 * @param array $sorted Value for the user's option.
6400 6156 * @return mixed
@@ -6404,9 +6160,9 @@
6404 6160 return $sorted;
6405 6161 }
6406 6162
6407 6163 foreach ( $sorted as $box_context => $ids ) {
6408 - if ( false === strpos( $ids, 'dashboard_stats' ) ) {
6164 + if ( ! str_contains( $ids, 'dashboard_stats' ) ) {
6409 6165 // If the old id isn't anywhere in the ids, don't bother exploding and fail out.
6410 6166 continue;
6411 6167 }
6412 6168
@@ -6423,68 +6179,9 @@
6423 6179 }
6424 6180
6425 6181 return $sorted;
6426 6182 }
6427 -
6428 6183 /**
6429 - * Adds a "blank" column in the user admin table to display indication of user connection.
6430 - *
6431 - * @param array $columns User list table columns.
6432 - *
6433 - * @return array
6434 - */
6435 - public function jetpack_icon_user_connected( $columns ) {
6436 - $columns['user_jetpack'] = '';
6437 - return $columns;
6438 - }
6439 -
6440 - /**
6441 - * Show Jetpack icon if the user is linked.
6442 - *
6443 - * @param string $val HTML for the icon.
6444 - * @param string $col User list table column.
6445 - * @param int $user_id User ID.
6446 - *
6447 - * @return string
6448 - */
6449 - public function jetpack_show_user_connected_icon( $val, $col, $user_id ) {
6450 - if ( 'user_jetpack' === $col && self::connection()->is_user_connected( $user_id ) ) {
6451 - $jetpack_logo = new Jetpack_Logo();
6452 - $emblem_html = sprintf(
6453 - '<a title="%1$s" class="jp-emblem-user-admin">%2$s</a>',
6454 - esc_attr__( 'This user is linked and ready to fly with Jetpack.', 'jetpack' ),
6455 - $jetpack_logo->get_jp_emblem()
6456 - );
6457 - return $emblem_html;
6458 - }
6459 -
6460 - return $val;
6461 - }
6462 -
6463 - /**
6464 - * Style the Jetpack user column
6465 - */
6466 - public function jetpack_user_col_style() {
6467 - global $current_screen;
6468 - if ( ! empty( $current_screen->base ) && 'users' === $current_screen->base ) {
6469 - ?>
6470 - <style>
6471 - .fixed .column-user_jetpack {
6472 - width: 21px;
6473 - }
6474 - .jp-emblem-user-admin svg {
6475 - width: 20px;
6476 - height: 20px;
6477 - }
6478 - .jp-emblem-user-admin path {
6479 - fill: #069e08;
6480 - }
6481 - </style>
6482 - <?php
6483 - }
6484 - }
6485 -
6486 - /**
6487 6184 * Checks if Akismet is active and working.
6488 6185 *
6489 6186 * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key
6490 6187 * that implied usage of methods present since more recent version.
@@ -6645,13 +6342,20 @@
6645 6342 }
6646 6343 }
6647 6344
6648 6345 /**
6649 - * Returns a boolean for whether backups UI should be displayed or not.
6346 + * Whether UI for backups should be displayed.
6650 6347 *
6348 + * On WPCom platforms this is gated on the backups-self-serve site feature.
6349 + * On self-hosted Jetpack sites it falls back to the jetpack_show_backups filter.
6350 + *
6651 6351 * @return bool Should backups UI be displayed?
6652 6352 */
6653 6353 public static function show_backups_ui() {
6354 + if ( ( new \Automattic\Jetpack\Status\Host() )->is_wpcom_platform() ) {
6355 + return function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'backups-self-serve' );
6356 + }
6357 +
6654 6358 /**
6655 6359 * Whether UI for backups should be displayed.
6656 6360 *
6657 6361 * @since 6.5.0
@@ -6661,8 +6365,24 @@
6661 6365 return self::is_plugin_active( 'vaultpress/vaultpress.php' ) || apply_filters( 'jetpack_show_backups', true );
6662 6366 }
6663 6367
6664 6368 /**
6369 + * Whether UI for security scanning should be displayed.
6370 + *
6371 + * On WPCom platforms this is gated on the scan-self-serve site feature.
6372 + * On self-hosted Jetpack sites it always returns true.
6373 + *
6374 + * @return bool Should scan UI be displayed?
6375 + */
6376 + public static function show_scan_ui() {
6377 + if ( ( new \Automattic\Jetpack\Status\Host() )->is_wpcom_platform() ) {
6378 + return function_exists( 'wpcom_site_has_feature' ) && wpcom_site_has_feature( 'scan-self-serve' );
6379 + }
6380 +
6381 + return true;
6382 + }
6383 +
6384 + /**
6665 6385 * Clean leftoveruser meta.
6666 6386 *
6667 6387 * Delete Jetpack-related user meta when it is no longer needed.
6668 6388 *
@@ -6734,8 +6454,40 @@
6734 6454 'url' => Redirect::get_url( 'jetpack-faq-backup-disclaimer' ),
6735 6455 ),
6736 6456 );
6737 6457
6458 + $products['creator'] = array(
6459 + 'title' => __( 'Jetpack Creator', 'jetpack' ),
6460 + 'slug' => 'jetpack_creator_yearly',
6461 + 'description' => __( 'Craft stunning content, boost your subscriber base, and monetize your online presence.', 'jetpack' ),
6462 + 'show_promotion' => true,
6463 + 'discount_percent' => 50,
6464 + 'included_in_plans' => array( 'complete' ),
6465 + 'features' => array(
6466 + _x( 'Unlimited subscriber imports', 'Creator Product Feature', 'jetpack' ),
6467 + _x( 'Earn more from your content', 'Creator Product Feature', 'jetpack' ),
6468 + _x( 'Accept payments with PayPal', 'Creator Product Feature', 'jetpack' ),
6469 + _x( 'Increase earnings with WordAds', 'Creator Product Feature', 'jetpack' ),
6470 + ),
6471 + );
6472 +
6473 + $products['growth'] = array(
6474 + 'title' => __( 'Jetpack Growth', 'jetpack' ),
6475 + 'slug' => 'jetpack_growth_yearly',
6476 + 'description' => __( 'Essential tools to help you grow your audience, track visitor engagement, and turn leads into loyal customers and advocates.', 'jetpack' ),
6477 + 'show_promotion' => true,
6478 + 'discount_percent' => 50,
6479 + 'included_in_plans' => array( 'complete' ),
6480 + 'features' => array(
6481 + _x( 'Jetpack Social', 'Growth Product Feature', 'jetpack' ),
6482 + _x( 'Jetpack Stats (10K site views, upgradeable)', 'Growth Product Feature', 'jetpack' ),
6483 + _x( 'Unlimited subscriber imports', 'Growth Product Feature', 'jetpack' ),
6484 + _x( 'Earn more from your content', 'Growth Product Feature', 'jetpack' ),
6485 + _x( 'Accept payments with PayPal', 'Growth Product Feature', 'jetpack' ),
6486 + _x( 'Increase earnings with WordAds', 'Growth Product Feature', 'jetpack' ),
6487 + ),
6488 + );
6489 +
6738 6490 $products['scan'] = array(
6739 6491 'title' => __( 'Jetpack Scan', 'jetpack' ),
6740 6492 'slug' => 'jetpack_scan',
6741 6493 'description' => __( 'Automatic scanning and one-click fixes keep your site one step ahead of security threats and malware.', 'jetpack' ),
@@ -6859,6 +6611,108 @@
6859 6611 return false;
6860 6612 }
6861 6613
6862 6614 return true;
6615 + }
6616 +
6617 + /**
6618 + * Add 5-star review link on the Jetpack Plugin meta, in the plugins list table (on the plugins page).
6619 + *
6620 + * @param array $plugin_meta An array of the plugin's metadata.
6621 + * @param string $plugin_file Path to the plugin file.
6622 + *
6623 + * @return array $plugin_meta An array of the plugin's metadata.
6624 + */
6625 + public function add_5_star_review_link( $plugin_meta, $plugin_file ) {
6626 + if ( $plugin_file !== 'jetpack/jetpack.php' ) {
6627 + return $plugin_meta;
6628 + }
6629 +
6630 + $u = get_current_user_id();
6631 + $site = get_site_url();
6632 +
6633 + $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">'
6634 + . str_repeat( '<span class="dashicons dashicons-star-filled" style="font-size: 16px; width:16px; height: 16px"></span>', 5 )
6635 + . '</a>';
6636 +
6637 + return $plugin_meta;
6638 + }
6639 +
6640 + /**
6641 + * Lazy instantiation of the Plugin_Tracking object.
6642 + *
6643 + * @since 13.9
6644 + *
6645 + * @return void
6646 + */
6647 + public function initialize_tracking() {
6648 + if ( did_action( 'jetpack_initialize_tracking' ) > 1 ) {
6649 + // Only need to run once.
6650 + return;
6651 + }
6652 +
6653 + if ( ( new Tracking( 'jetpack', $this->connection_manager ) )->should_enable_tracking( new Terms_Of_Service(), new Status() ) || static::is_connection_ready() ) {
6654 + ( new Plugin_Tracking() )->init();
6655 + }
6656 + }
6657 +
6658 + /**
6659 + * Run the "initialize tracking" hook.
6660 + *
6661 + * @since 13.9
6662 + */
6663 + public function run_initialize_tracking_action() {
6664 + /**
6665 + * Fires when the tracking needs to be initialized.
6666 + * Doesn't necessarily mean that will actually happen, depends if the 'jetpack_tos_agreed' option is set.
6667 + *
6668 + * @since 13.9
6669 + */
6670 + do_action( 'jetpack_initialize_tracking' );
6671 + }
6672 +
6673 + /**
6674 + * Initialize REST jsonAPI if needed.
6675 + *
6676 + * @return void
6677 + */
6678 + public function maybe_initialize_rest_jsonapi() {
6679 + // phpcs:ignore WordPress.Security.NonceVerification.Recommended
6680 + if ( ! empty( $_GET['jsonapi'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) ) {
6681 + require_once ABSPATH . 'wp-admin/includes/admin.php'; // JSON API relies on WP functionality not autoloaded in REST.
6682 +
6683 + define( 'WPCOM_JSON_API__BASE', 'public-api.wordpress.com/rest/v1' );
6684 + require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
6685 + }
6686 + }
6687 +
6688 + /**
6689 + * Run plugin post-activation actions if we need to.
6690 + *
6691 + * @return void
6692 + */
6693 + private function plugin_post_activation() {
6694 + if ( ( new Status() )->is_offline_mode() ) {
6695 + return;
6696 + }
6697 +
6698 + if ( get_transient( 'activated_jetpack' ) ) {
6699 + delete_transient( 'activated_jetpack' );
6700 +
6701 + if ( ( new Host() )->is_woa_site() ) {
6702 + $redirect_url = static::admin_url( 'page=jetpack' );
6703 + } elseif ( is_network_admin() ) {
6704 + $redirect_url = admin_url( 'network/admin.php?page=jetpack' );
6705 + } elseif ( get_transient( 'my_jetpack_product_activated' ) ) {
6706 + // don't redirect if this is an activation that just came from My Jetpack
6707 + // My Jetpack has its own set of post-activation redirects
6708 + return;
6709 + } elseif ( My_Jetpack_Initializer::should_initialize() ) {
6710 + $redirect_url = static::admin_url( 'page=my-jetpack' );
6711 + } else {
6712 + $redirect_url = static::admin_url( 'page=jetpack' );
6713 + }
6714 +
6715 + wp_safe_redirect( $redirect_url );
6716 + }
6863 6717 }
6864 6718 }