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