s active? $active_modules = self::get_active_modules(); if ( ! empty( $active_modules ) ) { return false; } // check for other Jetpack plugins that are installed on the site (active or not) // If there's more than one Jetpack plugin active, this user is not "new" $plugin_slugs = array_keys( Plugins_Installer::get_plugins() ); $plugin_slugs = array_map( static function ( $slug ) { $parts = explode( '/', $slug ); // Return the last segment of the filepath without the PHP extension return str_replace( '.php', '', $parts[ count( $parts ) - 1 ] ); }, $plugin_slugs ); $installed_jetpack_plugins = array_intersect( self::JETPACK_PLUGIN_SLUGS, $plugin_slugs ); if ( is_countable( $installed_jetpack_plugins ) && count( $installed_jetpack_plugins ) >= 2 ) { return false; } // Does the site have any purchases? $purchases = Wpcom_Products::get_site_current_purchases(); if ( ! empty( $purchases ) && ! is_wp_error( $purchases ) ) { return false; } return true; } /** * Build flags for My Jetpack UI * * @return array */ public static function get_my_jetpack_flags() { $flags = array( 'videoPressStats' => Jetpack_Constants::is_true( 'JETPACK_MY_JETPACK_VIDEOPRESS_STATS_ENABLED' ), 'showFullJetpackStatsCard' => class_exists( 'Jetpack' ), ); return $flags; } /** * Echoes the admin page content. * * @return void */ public static function admin_page() { $step = isset( $_GET['step'] ) ? sanitize_text_field( wp_unslash( $_GET['step'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $is_onboarding = $step === 'onboarding'; // Add data attribute for onboarding, otherwise render normal container echo '
'; } /** * Register the REST API routes. * * @return void */ public static function register_rest_endpoints() { new REST_Products(); new REST_Purchases(); new REST_Zendesk_Chat(); new REST_AI(); new REST_Recommendations_Evaluation(); Products::register_product_endpoints(); Historically_Active_Modules::register_rest_endpoints(); Jetpack_Manage::register_rest_endpoints(); Red_Bubble_Notifications::register_rest_endpoints(); register_rest_route( 'my-jetpack/v1', 'site', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => __CLASS__ . '::get_site', 'permission_callback' => __CLASS__ . '::permissions_callback', ) ); register_rest_route( 'my-jetpack/v1', 'site/dismiss-welcome-banner', array( 'methods' => \WP_REST_Server::EDITABLE, 'callback' => __CLASS__ . '::dismiss_welcome_banner', 'permission_callback' => __CLASS__ . '::permissions_callback', ) ); } /** * Check user capability to access the endpoint. * * @access public * @static * * @return true|WP_Error */ public static function permissions_callback() { return current_user_can( 'manage_options' ); } /** * Return true if we should initialize the My Jetpack admin page. */ public static function should_initialize() { $should = true; // All options presented in My Jetpack require a connection to WordPress.com. if ( ( new Status() )->is_offline_mode() ) { $should = false; } /** * Allows filtering whether My Jetpack should be initialized. * * @since 0.5.0-alpha * * @param bool $shoud_initialize Should we initialize My Jetpack? */ return apply_filters( 'jetpack_my_jetpack_should_initialize', $should ); } /** * Hook into several connection-based actions to update the historically active Jetpack modules * If the transient that indicates the list needs to be synced, update it and delete the transient * * @return void */ public static function setup_historically_active_jetpack_modules_sync() { // yummmm. ham. $ham = new Historically_Active_Modules(); if ( get_transient( $ham::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY ) && ! wp_doing_ajax() ) { $ham::update_historically_active_jetpack_modules(); delete_transient( $ham::UPDATE_HISTORICALLY_ACTIVE_JETPACK_MODULES_KEY ); } $actions = array( 'jetpack_site_registered', 'jetpack_user_authorized', 'activated_plugin', ); foreach ( $actions as $action ) { add_action( $action, array( $ham, 'queue_historically_active_jetpack_modules_update' ), 5 ); } // Modules are often updated async, so we need to update them right away as there will sometimes be no page reload. add_action( 'jetpack_activate_module', array( $ham, 'update_historically_active_jetpack_modules' ), 5 ); } /** * 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 "yes" if the site has file write access to the plugins folder, "no" 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 ( 'no' === $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; // Don't show red bubble alerts for non-admin users // These alerts are generally only actionable for admins if ( ! current_user_can( 'manage_options' ) ) { return; } // Don't show any red bubbles when Jetpack is disconnected // Users can't act on most alerts without a connection $connection = new Connection_Manager(); if ( ! $connection->is_connected() ) { return; } $rbn = new Red_Bubble_Notifications(); // filters for the items in this file add_filter( 'my_jetpack_red_bubble_notification_slugs', array( $rbn, 'add_red_bubble_alerts' ) ); $red_bubble_alerts = array_filter( $rbn::get_red_bubble_alerts(), function ( $alert ) { // We don't want to show the red bubble for silent alerts return empty( $alert['is_silent'] ); } ); // The Jetpack menu item should be on index 3 if ( ! empty( $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 ) ); } } /** * 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 } }