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