updated plugin Jetpack Protect version 2.1.0

This commit is contained in:
2024-04-19 10:49:36 +00:00
committed by Gitium
parent 620280b550
commit 7841fd5dc6
179 changed files with 6360 additions and 1476 deletions

View File

@ -19,6 +19,7 @@ use Automattic\Jetpack\Modules;
use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
use Automattic\Jetpack\My_Jetpack\Products as My_Jetpack_Products;
use Automattic\Jetpack\Plugins_Installer;
use Automattic\Jetpack\Protect\Onboarding;
use Automattic\Jetpack\Protect\Plan;
use Automattic\Jetpack\Protect\REST_Controller;
use Automattic\Jetpack\Protect\Site_Health;
@ -206,18 +207,20 @@ class Jetpack_Protect {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
$refresh_status_from_wpcom = isset( $_GET['checkPlan'] );
$initial_state = array(
'apiRoot' => esc_url_raw( rest_url() ),
'apiNonce' => wp_create_nonce( 'wp_rest' ),
'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
'status' => Status::get_status( $refresh_status_from_wpcom ),
'installedPlugins' => Plugins_Installer::get_plugins(),
'installedThemes' => Sync_Functions::get_themes(),
'wpVersion' => $wp_version,
'adminUrl' => 'admin.php?page=jetpack-protect',
'siteSuffix' => ( new Jetpack_Status() )->get_site_suffix(),
'jetpackScan' => My_Jetpack_Products::get_product( 'scan' ),
'hasRequiredPlan' => Plan::has_required_plan(),
'waf' => array(
'apiRoot' => esc_url_raw( rest_url() ),
'apiNonce' => wp_create_nonce( 'wp_rest' ),
'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
'status' => Status::get_status( $refresh_status_from_wpcom ),
'installedPlugins' => Plugins_Installer::get_plugins(),
'installedThemes' => Sync_Functions::get_themes(),
'wpVersion' => $wp_version,
'adminUrl' => 'admin.php?page=jetpack-protect',
'siteSuffix' => ( new Jetpack_Status() )->get_site_suffix(),
'blogID' => Connection_Manager::get_site_id( true ),
'jetpackScan' => My_Jetpack_Products::get_product( 'scan' ),
'hasRequiredPlan' => Plan::has_required_plan(),
'onboardingProgress' => Onboarding::get_current_user_progress(),
'waf' => array(
'wafSupported' => Waf_Runner::is_supported_environment(),
'currentIp' => IP_Utils::get_ip(),
'isSeen' => self::get_waf_seen_status(),

View File

@ -0,0 +1,80 @@
<?php
/**
* Class file for managing the user onboarding experience.
*
* @package automattic/jetpack-protect-plugin
*/
namespace Automattic\Jetpack\Protect;
/**
* Onboarding
*/
class Onboarding {
const OPTION_NAME = 'protect_onboarding_progress';
/**
* The current user's ID
*
* @var int
*/
private static $user_id;
/**
* Current User Progress
*
* @var array<string>
*/
private static $current_user_progress;
/**
* Onboarding Init
*
* @return void
*/
private static function init() {
self::$user_id = get_current_user_id();
$current_user_progress = get_user_meta( self::$user_id, self::OPTION_NAME, true );
self::$current_user_progress = $current_user_progress ? $current_user_progress : array();
}
/**
* Set Onboarding Items As Completed
*
* @param array $step_ids The IDs of the steps to complete.
* @return bool True if the update was successful, false otherwise.
*/
public static function complete_steps( $step_ids ) {
self::init();
if ( empty( self::$current_user_progress ) ) {
self::$current_user_progress = $step_ids;
} else {
// Find step IDs that are not already in the current user progress
$new_steps = array_diff( $step_ids, self::$current_user_progress );
// Merge new steps with current progress
self::$current_user_progress = array_merge( self::$current_user_progress, $new_steps );
}
// Update the user meta only once
return (bool) update_user_meta(
self::$user_id,
self::OPTION_NAME,
self::$current_user_progress
);
}
/**
* Get Current User's Onboarding Progress
*
* @return array<string>
*/
public static function get_current_user_progress() {
self::init();
return self::$current_user_progress;
}
}

View File

@ -206,6 +206,30 @@ class REST_Controller {
},
)
);
register_rest_route(
'jetpack-protect/v1',
'onboarding-progress',
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => __CLASS__ . '::api_get_onboarding_progress',
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
)
);
register_rest_route(
'jetpack-protect/v1',
'onboarding-progress',
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => __CLASS__ . '::api_complete_onboarding_steps',
'permission_callback' => function () {
return current_user_can( 'manage_options' );
},
)
);
}
/**
@ -424,4 +448,35 @@ class REST_Controller {
public static function api_set_waf_upgrade_seen_status() {
return Jetpack_Protect::set_waf_upgrade_seen_status();
}
/**
* Gets the current user's onboarding progress for the API endpoint
*
* @return WP_REST_Response
*/
public static function api_get_onboarding_progress() {
$progress = Onboarding::get_current_user_progress();
return rest_ensure_response( $progress, 200 );
}
/**
* Set an onboarding step as completed for the API endpoint
*
* @param WP_REST_Request $request The request object.
*
* @return WP_REST_Response
*/
public static function api_complete_onboarding_steps( $request ) {
if ( empty( $request['step_ids'] ) || ! is_array( $request['step_ids'] ) ) {
return new WP_REST_Response( 'Missing or invalid onboarding step IDs.', 400 );
}
$completed = Onboarding::complete_steps( $request['step_ids'] );
if ( ! $completed ) {
return new WP_REST_Response( 'An error occured completing the onboarding step(s).', 500 );
}
return new WP_REST_Response( 'Onboarding step(s) completed.' );
}
}