public static function activate_module( $module, $exit = true, $redirect = true ) {
return ( new Modules() )->activate( $module, $exit, $redirect );
* @param string $module Module slug.
public static function deactivate_module( $module ) {
return ( new Modules() )->deactivate( $module );
* Enable a configuable module.
* @param string $module Module slug.
public static function enable_module_configurable( $module ) {
$module = self::get_module_slug( $module );
add_filter( 'jetpack_module_configurable_' . $module, '__return_true' );
* Composes a module configure URL. It uses Jetpack settings search as default value
* It is possible to redefine resulting URL by using "jetpack_module_configuration_url_$module" filter
* @param string $module Module slug.
* @return string $url module configuration URL.
public static function module_configuration_url( $module ) {
$module = self::get_module_slug( $module );
$default_url = self::admin_url() . "#/settings?term=$module";
* Allows to modify configure_url of specific module to be able to redirect to some custom location.
* @param string $default_url Default url, which redirects to jetpack settings page.
$url = apply_filters( 'jetpack_module_configuration_url_' . $module, $default_url );
* Bail on activation if there is an issue.
* @param string $message Error message.
* @param bool $deactivate Deactivate Jetpack or not.
public static function bail_on_activation( $message, $deactivate = true ) {
<meta charset="<?php bloginfo( 'charset' ); ?>">
font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
<p><?php echo esc_html( $message ); ?></p>
$plugins = get_option( 'active_plugins' );
$jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' );
foreach ( $plugins as $i => $plugin ) {
if ( $plugin === $jetpack ) {
update_option( 'active_plugins', array_filter( $plugins ) );
* Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
* @param bool $network_wide Network-wide activation.
public static function plugin_activation( $network_wide ) {
Jetpack_Options::update_option( 'activated', 1 );
if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
/* translators: Jetpack version number. */
self::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) );
self::state( 'network_nag', true );
// For firing one-off events (notices) immediately after activation.
set_transient( 'activated_jetpack', true, 0.1 * MINUTE_IN_SECONDS );
update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) );
Health::on_jetpack_activated();
if ( self::is_connection_ready() && method_exists( 'Automattic\Jetpack\Sync\Actions', 'do_only_first_initial_sync' ) ) {
Sync_Actions::do_only_first_initial_sync();
if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
Woocommerce_Analytics::maybe_add_proxy_speed_module();
self::plugin_initialize();
* Returns the activation source.
* @param string $referer_url URL.
* @return array source_type, source_query.
public static function get_activation_source( $referer_url ) {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return array( 'wp-cli', null );
$referer = wp_parse_url( $referer_url );
$source_type = 'unknown';
if ( ! is_array( $referer ) || ! isset( $referer['path'] ) ) {
return array( $source_type, $source_query );
$plugins_path = wp_parse_url( admin_url( 'plugins.php' ), PHP_URL_PATH );
$plugins_install_path = wp_parse_url( admin_url( 'plugin-install.php' ), PHP_URL_PATH );// /wp-admin/plugin-install.php
if ( isset( $referer['query'] ) ) {
parse_str( $referer['query'], $query_parts );
if ( $plugins_path === $referer['path'] ) {
} elseif ( $plugins_install_path === $referer['path'] ) {
$tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured';
$source_type = 'popular';
$source_type = 'recommended';
$source_type = 'favorites';
$source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' );
$source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null;
$source_type = 'featured';
return array( $source_type, $source_query );
* Runs before bumping version numbers up to a new version
* @param string $version Version:timestamp.
* @param string $old_version Old Version:timestamp or false if not set yet.
public static function do_version_bump( $version, $old_version ) {
if ( $old_version ) { // For existing Jetpack installations.
add_action( 'admin_enqueue_scripts', __CLASS__ . '::enqueue_block_style' );
// If a front end page is visited after the update, the 'wp' action will fire.
add_action( 'wp', 'Jetpack::set_update_modal_display' );
// If an admin page is visited after the update, the 'current_screen' action will fire.
add_action( 'current_screen', 'Jetpack::set_update_modal_display' );
* Sets the display_update_modal state.
public static function set_update_modal_display() {
self::state( 'display_update_modal', true );
* Enqueues the block library styles.
* @param string $hook The current admin page.
public static function enqueue_block_style( $hook ) {
if ( 'toplevel_page_jetpack' === $hook ) {
wp_enqueue_style( 'wp-block-library' );
* Sets the internal version number and activation state.
public static function plugin_initialize() {
if ( ! Jetpack_Options::get_option( 'activated' ) ) {
Jetpack_Options::update_option( 'activated', 2 );
if ( ! Jetpack_Options::get_option( 'version' ) ) {
$old_version = JETPACK__VERSION . ':' . time();
/** This action is documented in class.jetpack.php */
do_action( 'updating_jetpack_version', $version, false );
Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
if ( self::is_connection_ready() ) {
self::handle_default_module_activation( true );
Jetpack_Options::delete_option( 'do_activate' );
* Handles the activation of the default modules depending on the current state of the site:
* - If the site already has the jetpack_active_modules option, activate those.
* - If the site has a site-only connection, only activate the default modules that require only a site connection.
* - If the site has a user connection, activate the default modules that require a user connection.
* @param bool $should_activate_user_modules Whether the status of the user connection should be checked and the default modules that
* require a user connection activated.
private static function handle_default_module_activation( $should_activate_user_modules ) {
$active_modules = Jetpack_Options::get_option( 'active_modules' );
self::delete_active_modules();
* Previously active modules could mean two things. First, it could mean
* that Jetpack was previously active on the site. In this case we would like
* to only activate the modules that were set to active.
* Another case could be that the module option was set by a standalone
* plugin. In that case the `active_modules_initalized` option will not
* be set, so we need to enable default Jetpack modules as well.
if ( ! Jetpack_Options::get_option( 'active_modules_initialized' ) ) {
$default_modules = self::get_default_modules();
$active_modules = array_merge( $active_modules, $default_modules );
Jetpack_Options::update_option( 'active_modules_initialized', true );
self::activate_default_modules(
999, // This version trick basically excludes every default module.
} elseif ( $should_activate_user_modules && ( new Connection_Manager() )->get_connection_owner_id() ) { // Check for a user connection.
self::activate_default_modules( false, false, array(), false, null, null, null );
Jetpack_Options::update_option( 'active_modules_initialized', true );
self::activate_default_modules( false, false, array(), false, null, null, false );
* Removes all connection options
public static function plugin_deactivation() {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
$tracking = new Tracking();
$tracking->record_user_event( 'deactivate_plugin', array() );
if ( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
Jetpack_Network::init()->deactivate();
add_filter( 'jetpack_update_activated_state_on_disconnect', '__return_false' );
Jetpack_Options::delete_option( 'version' );
if ( ! defined( 'WC_ANALYTICS' ) && class_exists( 'Automattic\Woocommerce_Analytics' ) ) {
Woocommerce_Analytics::maybe_remove_proxy_speed_module();
* Set activated option to 4 on jetpack_idc_disconnect action.
public static function on_idc_disconnect() {
\Jetpack_Options::update_option( 'activated', 4 );
* Disconnects from the Jetpack servers.
* Forgets all connection details and tells the Jetpack servers to do the same.
* Will not disconnect if there are other plugins using the connection.
* @since 11.0 Do not disconnect if other plugins are using the connection.
public static function disconnect() {
$connection = self::connection();
// If the site is in an IDC because sync is not allowed,
// let's make sure to not disconnect the production site.
$connection->remove_connection( ! Identity_Crisis::validate_sync_error_idc_option() );
* Happens after a successful disconnection.
public static function jetpack_site_disconnected() {
Identity_Crisis::clear_all_idc_options();
// Delete all the sync related data. Since it could be taking up space.
Sender::get_instance()->uninstall();
* Filters whether the Jetpack activated state should be updated after disconnecting.
* @param bool $update_activated_state Whether activated state should be updated after disconnecting, defaults to true.
$update_activated_state = apply_filters( 'jetpack_update_activated_state_on_disconnect', true );
if ( $update_activated_state ) {
Jetpack_Options::update_option( 'activated', 4 );
* @see \Automattic\Jetpack\Connection\Manager::disconnect_user()
* @param int $user_id The user ID to disconnect.
public function disconnect_user( $user_id ) {
$this->connection_manager->disconnect_user( $user_id );
* Checking the domain names in beta versions.
* If this is a development version, before attempting to connect, let's make sure that the domains are viable.
* @param null|\WP_Error $error The domain validation error, or `null` if everything's fine.
* @return null|\WP_Error The domain validation error, or `null` if everything's fine.
public static function registration_check_domains( $error ) {
if ( static::is_development_version() ) {
$domains_to_check = array_unique(
'siteurl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
'homeurl' => wp_parse_url( get_home_url(), PHP_URL_HOST ),
foreach ( $domains_to_check as $domain ) {
$result = static::connection()->is_usable_domain( $domain );
if ( is_wp_error( $result ) ) {
* Tracking an internal event log. Try not to put too much chaff in here.
* [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA)
* @param mixed $code Error code to log.
* @param mixed $data Data to log.
public static function log( $code, $data = null ) {
$raw_log = Jetpack_Options::get_option( 'log', array() );
// This can be modified by the `jetpack_options` filter, so abort if we don't have an array.
if ( ! is_array( $raw_log ) ) {
// only grab the latest 200 entries.
$log = array_slice( $raw_log, -199, 199 );
// Append our event to the log.
'user_id' => get_current_user_id(),
'blog_id' => Jetpack_Options::get_option( 'id' ),
// Don't bother storing it unless we've got some.
$log_entry['data'] = $data;
// Try add_option first, to make sure it's not autoloaded.
// @todo: Add an add_option method to Jetpack_Options.
if ( ! add_option( 'jetpack_log', $log, '', 'no' ) ) {
Jetpack_Options::update_option( 'log', $log );
* Fires when Jetpack logs an internal event.
* @param array $log_entry {
* Array of details about the log entry.
* @param string time Time of the event.
* @param int user_id ID of the user who trigerred the event.
* @param int blog_id Jetpack Blog ID.
* @param string code Unique name for the event.
* @param string data Data about the event.
do_action( 'jetpack_log_entry', $log_entry );
* Get the internal event log.
* @param string $event only return the specific log events.
* @param int $num - get specific number of latest results, limited to 200.
* @return array of log events || WP_Error for invalid params
public static function get_log( $event = false, $num = false ) {
if ( $event && ! is_string( $event ) ) {
return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) );
if ( $num && ! is_numeric( $num ) ) {
return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) );
$entire_log = Jetpack_Options::get_option( 'log', array() );
// If nothing set - act as it did before, otherwise let's start customizing the output.
if ( ! $num && ! $event ) {
$entire_log = array_reverse( $entire_log );
$custom_log_output = array();
foreach ( $entire_log as $log_event ) {
if ( $event === $log_event['code'] ) {
$custom_log_output[] = $log_event;
$custom_log_output = $entire_log;