If the module is active and not already in the array, add it if ( in_array( $status, Products::$active_module_statuses, true ) && ! in_array( $product_slug, $historically_active_modules, true ) ) { $historically_active_modules[] = $product_slug; } // If the module has been disabled due to a manual user action, // or because of a missing plan error, remove it from the array if ( in_array( $status, Products::$disabled_module_statuses, true ) ) { $historically_active_modules = array_values( array_diff( $historically_active_modules, array( $product_slug ) ) ); } } \Jetpack_Options::update_option( 'historically_active_modules', array_unique( $historically_active_modules ) ); } /** * Site full-data endpoint. * * @return object Site data. */ public static function get_site() { $site_id = \Jetpack_Options::get_option( 'id' ); $wpcom_endpoint = sprintf( '/sites/%d?force=wpcom', $site_id ); $wpcom_api_version = '1.1'; $response = Client::wpcom_json_api_request_as_blog( $wpcom_endpoint, $wpcom_api_version ); $response_code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ) ); if ( is_wp_error( $response ) || empty( $response['body'] ) ) { return new WP_Error( 'site_data_fetch_failed', 'Site data fetch failed', array( 'status' => $response_code ) ); } return rest_ensure_response( $body ); } /** * Populates the self::$site_info var with site data from the /sites/%d endpoint * * @return object|WP_Error */ public static function get_site_info() { static $site_info = null; if ( $site_info !== null ) { return $site_info; } // Check for a cached value before doing lookup $stored_site_info = get_transient( self::MY_JETPACK_SITE_INFO_TRANSIENT_KEY ); if ( $stored_site_info !== false ) { return $stored_site_info; } $response = self::get_site(); if ( is_wp_error( $response ) ) { return $response; } $site_info = $response->data; set_transient( self::MY_JETPACK_SITE_INFO_TRANSIENT_KEY, $site_info, DAY_IN_SECONDS ); return $site_info; } /** * Returns whether a site has been determined "commercial" or not. * * @return bool|null */ public static function is_commercial_site() { if ( is_wp_error( self::$site_info ) ) { return null; } return empty( self::$site_info->options->is_commercial ) ? false : self::$site_info->options->is_commercial; } /** * Check if site is registered (has been connected before). * * @return bool */ public static function is_registered() { return (bool) \Jetpack_Options::get_option( 'id' ); } /** * Dismiss the welcome banner. * * @return \WP_REST_Response */ public static function dismiss_welcome_banner() { \Jetpack_Options::update_option( 'dismissed_welcome_banner', true ); return rest_ensure_response( array( 'success' => true ) ); } /** * Returns true if the site has file write access to the plugins folder, false otherwise. * * @return string **/ public static function has_file_system_write_access() { $cache = get_transient( 'my_jetpack_write_access' ); if ( false !== $cache ) { return $cache; } if ( ! function_exists( 'get_filesystem_method' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } require_once ABSPATH . 'wp-admin/includes/template.php'; $write_access = 'no'; $filesystem_method = get_filesystem_method( array(), WP_PLUGIN_DIR ); if ( 'direct' === $filesystem_method ) { $write_access = 'yes'; } if ( ! $write_access ) { ob_start(); $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); ob_end_clean(); if ( $filesystem_credentials_are_stored ) { $write_access = 'yes'; } } set_transient( 'my_jetpack_write_access', $write_access, 30 * MINUTE_IN_SECONDS ); return $write_access; } /** * Get container IDC for the IDC screen. * * @return string */ public static function get_idc_container_id() { return static::IDC_CONTAINER_ID; } /** * Conditionally append the red bubble notification to the "Jetpack" menu item if there are alerts to show * * @return void */ public static function maybe_show_red_bubble() { global $menu; // filters for the items in this file add_filter( 'my_jetpack_red_bubble_notification_slugs', array( __CLASS__, 'add_red_bubble_alerts' ) ); $red_bubble_alerts = array_filter( self::get_red_bubble_alerts(), function ( $alert ) { // We don't want to show silent alerts return empty( $alert['is_silent'] ); } ); // The Jetpack menu item should be on index 3 if ( ! empty( $red_bubble_alerts ) && is_countable( $red_bubble_alerts ) && isset( $menu[3] ) && $menu[3][0] === 'Jetpack' ) { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $menu[3][0] .= sprintf( ' %d', count( $red_bubble_alerts ) ); } } /** * Collect all possible alerts that we might use a red bubble notification for * * @return array */ public static function get_red_bubble_alerts() { static $red_bubble_alerts = array(); // using a static cache since we call this function more than once in the class if ( ! empty( $red_bubble_alerts ) ) { return $red_bubble_alerts; } // go find the alerts $red_bubble_alerts = apply_filters( 'my_jetpack_red_bubble_notification_slugs', $red_bubble_alerts ); return $red_bubble_alerts; } /** * Get list of module names sorted by their recommendation score * * @return array|null */ public static function get_recommended_modules() { $recommendations_evaluation = \Jetpack_Options::get_option( 'recommendations_evaluation', null ); if ( ! $recommendations_evaluation ) { return null; } arsort( $recommendations_evaluation ); // Sort by scores in descending order return array_keys( $recommendations_evaluation ); // Get only module names } /** * Check for features broken by a disconnected user or site * * @return array */ public static function check_for_broken_modules() { $connection = new Connection_Manager(); $is_user_connected = $connection->is_user_connected() || $connection->has_connected_owner(); $is_site_connected = $connection->is_connected(); $broken_modules = array( 'needs_site_connection' => array(), 'needs_user_connection' => array(), ); if ( $is_user_connected && $is_site_connected ) { return $broken_modules; } $products = Products::get_products_classes(); $historically_active_modules = \Jetpack_Options::get_option( 'historically_active_modules', array() ); foreach ( $products as $product ) { if ( ! in_array( $product::$slug, $historically_active_modules, true ) ) { continue; } if ( $product::$requires_user_connection && ! $is_user_connected ) { if ( ! in_array( $product::$slug, $broken_modules['needs_user_connection'], true ) ) { $broken_modules['needs_user_connection'][] = $product::$slug; } } elseif ( ! $is_site_connected ) { if ( ! in_array( $product::$slug, $broken_modules['needs_site_connection'], true ) ) { $broken_modules['needs_site_connection'][] = $product::$slug; } } } return $broken_modules; } /** * Add relevant red bubble notifications * * @param array $red_bubble_slugs - slugs that describe the reasons the red bubble is showing. * @return array */ public static function add_red_bubble_alerts( array $red_bubble_slugs ) { if ( wp_doing_ajax() ) { return array(); } $connection = new Connection_Manager(); $welcome_banner_dismissed = \Jetpack_Options::get_option( 'dismissed_welcome_banner', false ); if ( self::is_jetpack_user_new() && ! $welcome_banner_dismissed ) { $red_bubble_slugs['welcome-banner-active'] = array( 'is_silent' => $connection->is_connected(), // we don't display the red bubble if the user is connected ); return $red_bubble_slugs; } else { return self::alert_if_missing_connection( $red_bubble_slugs ); } } /** * Add an alert slug if the site is missing a site connection * * @param array $red_bubble_slugs - slugs that describe the reasons the red bubble is showing. * @return array */ public static function alert_if_missing_connection( array $red_bubble_slugs ) { $broken_modules = self::check_for_broken_modules(); $connection = new Connection_Manager(); if ( ! empty( $broken_modules['needs_user_connection'] ) ) { $red_bubble_slugs[ self::MISSING_CONNECTION_NOTIFICATION_KEY ] = array( 'type' => 'user', 'is_error' => true, ); return $red_bubble_slugs; } if ( ! empty( $broken_modules['needs_site_connection'] ) ) { $red_bubble_slugs[ self::MISSING_CONNECTION_NOTIFICATION_KEY ] = array( 'type' => 'site', 'is_error' => true, ); return $red_bubble_slugs; } if ( ! $connection->is_connected() ) { $red_bubble_slugs[ self::MISSING_CONNECTION_NOTIFICATION_KEY ] = array( 'type' => 'site', 'is_error' => false, ); return $red_bubble_slugs; } return $red_bubble_slugs; } }