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