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