updated plugin Jetpack Protect version 5.0.0

This commit is contained in:
2026-06-03 21:29:05 +00:00
committed by Gitium
parent 0490bb3940
commit af21e84842
399 changed files with 17749 additions and 5470 deletions

View File

@ -0,0 +1,32 @@
/**
* Marks the wp-admin Jetpack sidebar "Upgrade Jetpack" item for Tracks.
*/
document.addEventListener( 'DOMContentLoaded', () => {
const config = window.jetpackAdminUiUpgradeMenu;
if ( typeof config === 'undefined' ) {
return;
}
const className = config.menuItemClass;
if ( ! className ) {
return;
}
// Class comes from PHP (UPGRADE_MENU_SLUG); safe for querySelector.
const item = document.querySelector( `li.${ className } a` );
if ( ! item ) {
return;
}
// Initialize Tracks
if ( 'undefined' !== typeof window?.analytics && config.tracksUserData ) {
window.analytics.initialize( config.tracksUserData?.userid, config.tracksUserData?.username );
}
item.addEventListener( 'click', function () {
window.analytics?.tracks?.recordEvent(
'jetpack_sidebar_free_upgrade_click',
config.tracksEventData
);
} );
} );

View File

@ -7,13 +7,33 @@
namespace Automattic\Jetpack\Admin_UI;
use Automattic\Jetpack\Tracking;
use Jetpack_Options;
use Jetpack_Tracks_Client;
/**
* This class offers a wrapper to add_submenu_page and makes sure stand-alone plugin's menu items are always added under the Jetpack top level menu.
* If the Jetpack top level was not previously registered by other plugin, it will be registered here.
*/
class Admin_Menu {
const PACKAGE_VERSION = '0.5.7';
const PACKAGE_VERSION = '0.8.0';
/**
* Slug used for the upgrade menu item and redirect URL.
*
* Keep the slug in sync with `$upgrade-menu-slug` at admin-ui-upgrade-menu.scss
*
* @var string
*/
const UPGRADE_MENU_SLUG = 'jetpack-wpadmin-sidebar-free-plan-upsell-menu-item';
/**
* Fallback upgrade URL when the Redirect class is unavailable.
*
* @var string
*/
const UPGRADE_MENU_FALLBACK_URL = 'https://jetpack.com/upgrade/';
/**
* Whether this class has been initialized
@ -29,6 +49,13 @@ class Admin_Menu {
*/
private static $menu_items = array();
/**
* Optional connection manager dependency.
*
* @var object|null
*/
private static $connection_manager = null;
/**
* Initialize the class and set up the main hook
*
@ -40,6 +67,7 @@ class Admin_Menu {
self::handle_akismet_menu();
add_action( 'admin_menu', array( __CLASS__, 'admin_menu_hook_callback' ), 1000 ); // Jetpack uses 998.
add_action( 'network_admin_menu', array( __CLASS__, 'admin_menu_hook_callback' ), 1000 ); // Jetpack uses 998.
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'add_upgrade_menu_item_styles' ) );
}
}
@ -140,6 +168,8 @@ class Admin_Menu {
if ( ! $can_see_toplevel_menu ) {
remove_menu_page( 'jetpack' );
}
self::maybe_add_upgrade_menu_item();
}
/**
@ -149,15 +179,15 @@ class Admin_Menu {
* aggreagate all menu items registered by stand-alone plugins and make sure they all go under the same
* Jetpack top level menu. It will also handle the top level menu registration in case the Jetpack plugin is not present.
*
* @param string $page_title The text to be displayed in the title tags of the page when the menu
* is selected.
* @param string $menu_title The text to be used for the menu.
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu
* and only include lowercase alphanumeric, dashes, and underscores characters
* to be compatible with sanitize_key().
* @param callable $function The function to be called to output the content for this page.
* @param int $position The position in the menu order this item should appear. Leave empty typically.
* @param string $page_title The text to be displayed in the title tags of the page when the menu
* is selected.
* @param string $menu_title The text to be used for the menu.
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by. Should be unique for this menu
* and only include lowercase alphanumeric, dashes, and underscores characters
* to be compatible with sanitize_key().
* @param callable|null $function The function to be called to output the content for this page.
* @param int $position The position in the menu order this item should appear. Leave empty typically.
*
* @return string The resulting page's hook_suffix
*/
@ -225,4 +255,219 @@ class Admin_Menu {
$url = $fallback ? $fallback : admin_url();
return $url;
}
/**
* Checks whether the current site should show the upgrade menu item.
*
* The upgrade menu is only shown to administrators on free-plan sites
* that are not hosted on WordPress.com.
*
* @return bool True if the upgrade menu should be shown.
*/
private static function should_show_upgrade_menu() {
// Only show to administrators.
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
// Don't show upsells on WordPress.com platform.
if ( class_exists( '\Automattic\Jetpack\Status\Host' ) ) {
$host = new \Automattic\Jetpack\Status\Host();
if ( $host->is_wpcom_platform() ) {
return false;
}
}
// Don't show upsells in offline/development mode.
if ( class_exists( '\Automattic\Jetpack\Status' ) ) {
$status = new \Automattic\Jetpack\Status();
if ( $status->is_offline_mode() ) {
return false;
}
}
// Only show after the site and current user are connected.
if ( ! self::is_site_and_user_connected() ) {
return false;
}
// Only show to free-plan sites.
return self::is_free_plan();
}
/**
* Checks whether the site and current user are connected to WordPress.com.
*
* @return bool True if site and current user are connected.
*/
private static function is_site_and_user_connected() {
$connection_manager = self::$connection_manager;
if ( ! $connection_manager && class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
$connection_manager = new \Automattic\Jetpack\Connection\Manager();
self::$connection_manager = $connection_manager;
}
if (
$connection_manager
&& is_callable( array( $connection_manager, 'is_connected' ) )
&& is_callable( array( $connection_manager, 'is_user_connected' ) )
) {
return (bool) $connection_manager->is_connected()
&& (bool) $connection_manager->is_user_connected( get_current_user_id() );
}
return false;
}
/**
* Sets the connection manager dependency; used by tests.
*
* @param object|null $connection_manager Connection manager object.
* @return void
*/
public static function set_connection_manager( $connection_manager ) {
self::$connection_manager = $connection_manager;
}
/**
* Checks whether the current site is on a free Jetpack plan with no active paid license.
*
* @return bool True if the site has no paid plan.
*/
private static function is_free_plan() {
// Check the active plan - use the is_free field or product_slug.
$plan = get_option( 'jetpack_active_plan', array() );
// Back-compat: older plan payloads use class to indicate paid plans.
if ( isset( $plan['class'] ) && 'free' !== $plan['class'] ) {
return false;
}
// If the plan explicitly says it's not free, trust that.
if ( isset( $plan['is_free'] ) && false === $plan['is_free'] ) {
return false;
}
// Check if the product slug indicates a paid plan.
if ( isset( $plan['product_slug'] ) && 'jetpack_free' !== $plan['product_slug'] ) {
return false;
}
// Also check for site products (licenses can add products without changing plan).
$products = get_option( 'jetpack_site_products', array() );
if ( ! empty( $products ) && is_array( $products ) ) {
return false;
}
return true;
}
/**
* Conditionally adds an "Upgrade Jetpack" submenu item for free-plan sites.
*
* Only shown to users with manage_options capability on self-hosted sites without a paid Jetpack plan or license.
*
* @return void
*/
private static function maybe_add_upgrade_menu_item() {
if ( ! self::should_show_upgrade_menu() ) {
return;
}
$upgrade_url = class_exists( '\Automattic\Jetpack\Redirect' )
? \Automattic\Jetpack\Redirect::get_url( self::UPGRADE_MENU_SLUG )
: self::UPGRADE_MENU_FALLBACK_URL;
$menu_title = esc_html__( 'Upgrade Jetpack', 'jetpack-admin-ui' );
add_submenu_page(
'jetpack',
$menu_title,
$menu_title,
'manage_options',
esc_url( $upgrade_url ),
null, // @phan-suppress-current-line PhanTypeMismatchArgumentProbablyReal -- Core should ideally document null for no-callback arg. https://core.trac.wordpress.org/ticket/52539.
999
);
// Add a CSS class to the <li> element so styles can target it precisely.
global $submenu;
if ( ! empty( $submenu['jetpack'] ) ) {
foreach ( $submenu['jetpack'] as $index => $item ) {
if ( isset( $item[2] ) && false !== strpos( $item[2], self::UPGRADE_MENU_SLUG ) ) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$submenu['jetpack'][ $index ][4] = ( ! empty( $item[4] ) ? $item[4] . ' ' : '' ) . self::UPGRADE_MENU_SLUG;
break;
}
}
}
}
/**
* Enqueues admin styles for the "Upgrade Jetpack" menu item.
*
* The sidebar menu is visible on every admin page, so styles load globally.
* Only enqueues for free-plan sites on self-hosted installs.
*
* @return void
*/
public static function add_upgrade_menu_item_styles() {
if ( ! self::should_show_upgrade_menu() ) {
return;
}
$asset_file = dirname( __DIR__ ) . '/build/admin-ui-upgrade-menu.asset.php';
$asset = file_exists( $asset_file ) ? require $asset_file : array();
wp_enqueue_style(
'jetpack-admin-ui-upgrade-menu',
plugins_url( '../build/admin-ui-upgrade-menu.css', __FILE__ ),
$asset['dependencies'] ?? array(),
$asset['version'] ?? self::PACKAGE_VERSION
);
self::enqueue_upgrade_menu_tracks_script( $asset );
}
/**
* Enqueues Tracks for the upgrade submenu item.
*
* @param array $asset Parsed contents of admin-ui-upgrade-menu.asset.php.
* @return void
*/
private static function enqueue_upgrade_menu_tracks_script( $asset ) {
if ( ! class_exists( '\Automattic\Jetpack\Tracking' ) ) {
return;
}
Tracking::register_tracks_functions_scripts( true );
wp_enqueue_script(
'jetpack-admin-ui-upgrade-menu-tracking',
plugins_url( '../build/admin-ui-upgrade-menu-tracking.js', __FILE__ ),
$asset['dependencies'] ?? array(),
$asset['version'] ?? self::PACKAGE_VERSION,
true
);
$current_screen = get_current_screen();
$is_admin = current_user_can( 'jetpack_disconnect' );
$site_id = class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null;
$tracks_user_data = class_exists( 'Jetpack_Tracks_Client' ) ? Jetpack_Tracks_Client::get_connected_user_tracks_identity() : null;
wp_localize_script(
'jetpack-admin-ui-upgrade-menu-tracking',
'jetpackAdminUiUpgradeMenu',
array(
'menuItemClass' => self::UPGRADE_MENU_SLUG,
'tracksUserData' => $tracks_user_data,
'tracksEventData' => array(
'is_admin' => $is_admin,
'current_screen' => $current_screen ? $current_screen->id : false,
'blog_id' => $site_id,
),
)
);
}
}