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