Skip to content

Commit

Permalink
Protect: React Query
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweller committed Sep 2, 2024
1 parent 9efcf6d commit 160323a
Show file tree
Hide file tree
Showing 89 changed files with 2,038 additions and 2,081 deletions.
26 changes: 23 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion projects/plugins/protect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ Jetpack Protect plugin

## Developing

Use the [Jetpack Debug Helper plugin](https://github.com/Automattic/jetpack/tree/trunk/projects/plugins/debug-helper) to force Protect into different statuses. The plugin will allow you to emulate different responses from the server so you can work on all the different statuses the plugin support.
### Debug helper plugin

Use the [Jetpack Debug Helper plugin](https://github.com/Automattic/jetpack/tree/trunk/projects/plugins/debug-helper) to mock different scan results and application states. The plugin will allow you to emulate different responses from the server so you can work on all the different statuses the plugin support.

### Bypassing the cache

If you want to force Protect to always fetch data from the server you can use the constant below:

`JETPACK_PROTECT_DEV__BYPASS_CACHE` - will ignore the cached results and will always request fresh data from WPCOM servers.

Be aware that a request to the server will be made in all admin pages! Use it only for debugging.

### Component Storybooks

[Storybook](https://storybook.js.org/) is available for developing UI components in isolation.

Run `pnpm run storybook:dev` in `projects/js-packages/storybook` to get started.

### React Query Browser Tools

This project also includes [React Query Devtools](https://tanstack.com/query/latest/docs/framework/react/devtools).

Whenever the application is running in development mode (i.e. via `jetpack watch`), the tools will be available in the plugin via a floating icon in the bottom right corner.

## Contribute

Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/protect/changelog/add-protect-tanstack-query
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Added react query, no user-facing impact.


3 changes: 2 additions & 1 deletion projects/plugins/protect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"@automattic/jetpack-base-styles": "workspace:*",
"@automattic/jetpack-components": "workspace:*",
"@automattic/jetpack-connection": "workspace:*",
"@tanstack/react-query": "5.20.5",
"@tanstack/react-query-devtools": "5.20.5",
"@wordpress/api-fetch": "7.5.0",
"@wordpress/components": "28.5.0",
"@wordpress/data": "10.5.0",
"@wordpress/date": "5.5.0",
"@wordpress/element": "6.5.0",
"@wordpress/i18n": "5.5.0",
Expand Down
11 changes: 3 additions & 8 deletions projects/plugins/protect/src/class-jetpack-protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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\Credentials;
use Automattic\Jetpack\Protect\Onboarding;
use Automattic\Jetpack\Protect\REST_Controller;
use Automattic\Jetpack\Protect\Scan_History;
Expand All @@ -38,11 +39,6 @@
*/
class Jetpack_Protect {

/**
* Licenses product ID.
*
* @var string
*/
const JETPACK_SCAN_PRODUCT_IDS = array(
2010, // JETPACK_SECURITY_DAILY.
2011, // JETPACK_SECURITY_DAILY_MOTNHLY.
Expand Down Expand Up @@ -214,6 +210,7 @@ public function initial_state() {
'apiRoot' => esc_url_raw( rest_url() ),
'apiNonce' => wp_create_nonce( 'wp_rest' ),
'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
'credentials' => Credentials::get_credential_array(),
'status' => Status::get_status( $refresh_status_from_wpcom ),
'scanHistory' => Scan_History::get_scan_history( $refresh_status_from_wpcom ),
'installedPlugins' => Plugins_Installer::get_plugins(),
Expand All @@ -223,7 +220,7 @@ public function initial_state() {
'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(),
'hasPlan' => Plan::has_required_plan(),
'onboardingProgress' => Onboarding::get_current_user_progress(),
'waf' => array(
'wafSupported' => Waf_Runner::is_supported_environment(),
Expand All @@ -232,8 +229,6 @@ public function initial_state() {
'upgradeIsSeen' => self::get_waf_upgrade_seen_status(),
'displayUpgradeBadge' => self::get_waf_upgrade_badge_display_status(),
'isEnabled' => Waf_Runner::is_enabled(),
'isToggling' => false,
'isUpdating' => false,
'config' => Waf_Runner::get_config(),
'stats' => self::get_waf_stats(),
'globalStats' => Waf_Stats::get_global_stats(),
Expand Down
33 changes: 20 additions & 13 deletions projects/plugins/protect/src/class-rest-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
namespace Automattic\Jetpack\Protect;

use Automattic\Jetpack\Connection\Rest_Authentication as Connection_Rest_Authentication;
use Automattic\Jetpack\IP\Utils as IP_Utils;
use Automattic\Jetpack\Protect_Status\REST_Controller as Protect_Status_REST_Controller;
use Automattic\Jetpack\Waf\Waf_Runner;
use Automattic\Jetpack\Waf\Waf_Stats;
use Jetpack_Protect;
use WP_Error;
use WP_REST_Request;
Expand Down Expand Up @@ -239,7 +241,7 @@ public static function api_ignore_threat( $request ) {
$threat_ignored = Threats::ignore_threat( $request['threat_id'] );

if ( ! $threat_ignored ) {
return new WP_REST_Response( 'An error occured while attempting to ignore the threat.', 500 );
return new WP_REST_Response( 'An error occurred while attempting to ignore the threat.', 500 );
}

return new WP_REST_Response( 'Threat ignored.' );
Expand All @@ -260,7 +262,7 @@ public static function api_unignore_threat( $request ) {
$threat_ignored = Threats::unignore_threat( $request['threat_id'] );

if ( ! $threat_ignored ) {
return new WP_REST_Response( 'An error occured while attempting to unignore the threat.', 500 );
return new WP_REST_Response( 'An error occurred while attempting to unignore the threat.', 500 );
}

return new WP_REST_Response( 'Threat unignored.' );
Expand All @@ -281,7 +283,7 @@ public static function api_fix_threats( $request ) {
$threats_fixed = Threats::fix_threats( $request['threat_ids'] );

if ( ! $threats_fixed ) {
return new WP_REST_Response( 'An error occured while attempting to fix the threat.', 500 );
return new WP_REST_Response( 'An error occurred while attempting to fix the threat.', 500 );
}

return new WP_REST_Response( $threats_fixed );
Expand All @@ -302,7 +304,7 @@ public static function api_fix_threats_status( $request ) {
$threats_fixed = Threats::fix_threats_status( $request['threat_ids'] );

if ( ! $threats_fixed ) {
return new WP_REST_Response( 'An error occured while attempting to get the fixer status of the threats.', 500 );
return new WP_REST_Response( 'An error occurred while attempting to get the fixer status of the threats.', 500 );
}

return new WP_REST_Response( $threats_fixed );
Expand All @@ -317,7 +319,7 @@ public static function api_check_credentials() {
$credential_array = Credentials::get_credential_array();

if ( ! isset( $credential_array ) ) {
return new WP_REST_Response( 'An error occured while attempting to fetch the credentials array', 500 );
return new WP_REST_Response( 'An error occurred while attempting to fetch the credentials array', 500 );
}

return new WP_REST_Response( $credential_array );
Expand All @@ -332,7 +334,7 @@ public static function api_scan() {
$scan_enqueued = Threats::scan();

if ( ! $scan_enqueued ) {
return new WP_REST_Response( 'An error occured while attempting to enqueue the scan.', 500 );
return new WP_REST_Response( 'An error occurred while attempting to enqueue the scan.', 500 );
}

return new WP_REST_Response( 'Scan enqueued.' );
Expand All @@ -349,7 +351,7 @@ public static function api_toggle_waf() {
if ( ! $disabled ) {
return new WP_Error(
'waf_disable_failed',
__( 'An error occured disabling the firewall.', 'jetpack-protect' ),
__( 'An error occurred disabling the firewall.', 'jetpack-protect' ),
array( 'status' => 500 )
);
}
Expand All @@ -361,7 +363,7 @@ public static function api_toggle_waf() {
if ( ! $enabled ) {
return new WP_Error(
'waf_enable_failed',
__( 'An error occured enabling the firewall.', 'jetpack-protect' ),
__( 'An error occurred enabling the firewall.', 'jetpack-protect' ),
array( 'status' => 500 )
);
}
Expand All @@ -380,10 +382,15 @@ public static function api_get_waf() {

return new WP_REST_Response(
array(
'is_seen' => Jetpack_Protect::get_waf_seen_status(),
'is_enabled' => Waf_Runner::is_enabled(),
'config' => Waf_Runner::get_config(),
'stats' => Jetpack_Protect::get_waf_stats(),
'wafSupported' => Waf_Runner::is_supported_environment(),
'currentIp' => IP_Utils::get_ip(),
'isSeen' => Jetpack_Protect::get_waf_seen_status(),
'upgradeIsSeen' => Jetpack_Protect::get_waf_upgrade_seen_status(),
'displayUpgradeBadge' => Jetpack_Protect::get_waf_upgrade_badge_display_status(),
'isEnabled' => Waf_Runner::is_enabled(),
'config' => Waf_Runner::get_config(),
'stats' => Jetpack_Protect::get_waf_stats(),
'globalStats' => Waf_Stats::get_global_stats(),
)
);
}
Expand Down Expand Up @@ -449,7 +456,7 @@ public static function api_complete_onboarding_steps( $request ) {
$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( 'An error occurred completing the onboarding step(s).', 500 );
}

return new WP_REST_Response( 'Onboarding step(s) completed.' );
Expand Down
56 changes: 0 additions & 56 deletions projects/plugins/protect/src/js/api.js

This file was deleted.

Loading

0 comments on commit 160323a

Please sign in to comment.