Skip to content

Commit

Permalink
Fix token decoding during wp-env startup. (#220)
Browse files Browse the repository at this point in the history
* Handle `false` return in decrypt_option()

* Return array from decrypt_option(), condtionally show admin error

* Move block registration to `init` action

* Add explicit arity and priority to integration init actions

* Use init action in Salesforce integration

---------

Co-authored-by: Max Schmeling <[email protected]>
  • Loading branch information
alecgeatches and maxschmeling authored Dec 30, 2024
1 parent f6cdf37 commit 8242f14
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions inc/Integrations/Airtable/AirtableIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class AirtableIntegration {
public static function init(): void {
add_action( 'init', [ __CLASS__, 'register_blocks' ], 10, 0 );
}

public static function register_blocks(): void {
$data_source_configs = DataSourceCrud::get_configs_by_service( REMOTE_DATA_BLOCKS_AIRTABLE_SERVICE );

foreach ( $data_source_configs as $config ) {
Expand Down
4 changes: 4 additions & 0 deletions inc/Integrations/SalesforceB2C/SalesforceB2CIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

class SalesforceB2CIntegration {
public static function init(): void {
add_action( 'init', [ __CLASS__, 'register_blocks' ], 10, 0 );
}

public static function register_blocks(): void {
$data_source_configs = DataSourceCrud::get_configs_by_service( REMOTE_DATA_BLOCKS_SALESFORCE_B2C_SERVICE );

foreach ( $data_source_configs as $config ) {
Expand Down
4 changes: 4 additions & 0 deletions inc/Integrations/Shopify/ShopifyIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

class ShopifyIntegration {
public static function init(): void {
add_action( 'init', [ __CLASS__, 'register_blocks' ], 10, 0 );
}

public static function register_blocks(): void {
$data_source_configs = DataSourceCrud::get_configs_by_service( REMOTE_DATA_BLOCKS_SHOPIFY_SERVICE );

foreach ( $data_source_configs as $config ) {
Expand Down
24 changes: 21 additions & 3 deletions inc/PluginSettings/PluginSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RemoteDataBlocks\WpdbStorage\DataSourceCrud;
use function wp_get_environment_type;
use function wp_is_development_mode;
use function add_settings_error;

defined( 'ABSPATH' ) || exit();

Expand Down Expand Up @@ -154,19 +155,36 @@ public static function encrypt_option( array $new_value, string|array|bool $old_
}
}

public static function decrypt_option( string $value ): array|null {
public static function decrypt_option( string $value ): array {
$decryptor = new \RemoteDataBlocks\WpdbStorage\DataEncryption();
$is_error = false;

try {
$decrypted = $decryptor->decrypt( $value );
return json_decode( $decrypted, true );

if ( false === $decrypted ) {
$is_error = true;
}
} catch ( \Exception $e ) {
$is_error = true;
}

if ( $is_error ) {
self::show_decryption_error();
return [];
} else {
return json_decode( $decrypted, true );
}
}

private static function show_decryption_error(): void {
// Check that we have add_settings_error() available. This can be unavailable during wp-env startup.
if ( is_admin() ) {
add_settings_error(
'remote_data_blocks_settings',
'decryption_error',
__( 'Error decrypting remote-data-blocks settings.', 'remote-data-blocks' )
);
return null;
}
}
}

0 comments on commit 8242f14

Please sign in to comment.