Skip to content

Commit

Permalink
Separate out ScrubOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
NickGreen committed Apr 8, 2024
1 parent b4dccc3 commit 41d25c5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 74 deletions.
2 changes: 1 addition & 1 deletion includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace SafetyNet\Admin;

use function SafetyNet\DeactivatePlugins\scrub_options;
use function SafetyNet\ScrubOptions\scrub_options;
use function SafetyNet\DeactivatePlugins\deactivate_plugins;
use function SafetyNet\Delete\delete_users_and_orders;
use function SafetyNet\Utilities\is_production;
Expand Down
74 changes: 1 addition & 73 deletions includes/deactivate-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use function SafetyNet\Utilities\get_denylist_array;

add_action( 'safety_net_deactivate_plugins', __NAMESPACE__ . '\deactivate_plugins' );
add_action( 'safety_net_scrub_options', __NAMESPACE__ . '\scrub_options' );

/*
* Deactivate plugins from a denylist
Expand Down Expand Up @@ -66,75 +65,4 @@ function deactivate_plugins() {
}

update_option( 'safety_net_plugins_deactivated', true );
}

/*
* Clear options such as API keys so that plugins won't talk to 3rd parties
*/
function scrub_options() {

update_option( 'admin_email', '[email protected]' );

$options_to_clear = get_denylist_array( 'options' );
$options_to_clear = apply_filters( 'safety_net_options_to_clear', $options_to_clear );

foreach ( $options_to_clear as $option ) {
$option_value = get_option( $option );
if ( $option_value ) {

update_option( $option . '_backup', $option_value );

if ( 'woocommerce_ppcp-gateway_settings' === $option || 'woocommerce-ppcp-settings' === $option || 'woocommerce_stripe_settings' === $option ) {
// we need to more selectively wipe parts of these options, because the respective plugins will fatal if the entire options are blank
$keys_to_scrub = array( 'enabled', 'client_secret_production', 'client_id_production', 'client_secret', 'client_id', 'merchant_id', 'merchant_email', 'merchant_id_production', 'merchant_email_production', 'publishable_key', 'secret_key', 'webhook_secret' );
$option_array = $option_value;
foreach ( $keys_to_scrub as $key ) {
if ( array_key_exists( $key, $option_array ) ) {
$option_array[ $key ] = '';
}
}
update_option( $option, $option_array );
} elseif ( 'jetpack_active_modules' === $option ) {
// Clear some Jetpack options to disable specific modules.
$modules_to_disable = array( 'enhanced-distribution', 'publicize', 'subscriptions' );
$modules_array = array_filter(
$option_value,
function( $v ) use ( $modules_to_disable ) {
return ! in_array( $v, $modules_to_disable, true );
},
);

update_option( $option, $modules_array );
} elseif ( 'wprus' === $option ) {
// Clear some WP Remote Users Sync options to disable only keys needed for remote connections and keep the remaining settings intact.
$keys_to_scrub = array(
'encryption' => array(
'aes_key',
'hmac_key',
),
);
$option_array = $option_value;
foreach ( $keys_to_scrub as $index => $keys ) {
if ( array_key_exists( $index, $option_array ) ) {
foreach ( $keys as $key ) {
if ( array_key_exists( $key, $option_array[ $index ] ) ) {
$option_array[ $index ][ $key ] = '';
}
}
}
}
update_option( $option, $option_array );
} else {
// Some plugins don't like it when options are deleted, so we will save their value as either an empty string or array, depending on which it already is.
if ( is_array( get_option( $option ) ) ) {
$empty_array = array();
update_option( $option, $empty_array );
} else {
update_option( $option, '' );
}
}
}
}

update_option( 'safety_net_options_scrubbed', true );
}
}
78 changes: 78 additions & 0 deletions includes/scrub-options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace SafetyNet\ScrubOptions;

use function SafetyNet\Utilities\get_denylist_array;

add_action( 'safety_net_scrub_options', __NAMESPACE__ . '\scrub_options' );

/*
* Clear options such as API keys so that plugins won't talk to 3rd parties
*/
function scrub_options() {

update_option( 'admin_email', '[email protected]' );

$options_to_clear = get_denylist_array( 'options' );
$options_to_clear = apply_filters( 'safety_net_options_to_clear', $options_to_clear );

foreach ( $options_to_clear as $option ) {
$option_value = get_option( $option );
if ( $option_value ) {

update_option( $option . '_backup', $option_value );

if ( 'woocommerce_ppcp-gateway_settings' === $option || 'woocommerce-ppcp-settings' === $option || 'woocommerce_stripe_settings' === $option ) {
// we need to more selectively wipe parts of these options, because the respective plugins will fatal if the entire options are blank
$keys_to_scrub = array( 'enabled', 'client_secret_production', 'client_id_production', 'client_secret', 'client_id', 'merchant_id', 'merchant_email', 'merchant_id_production', 'merchant_email_production', 'publishable_key', 'secret_key', 'webhook_secret' );
$option_array = $option_value;
foreach ( $keys_to_scrub as $key ) {
if ( array_key_exists( $key, $option_array ) ) {
$option_array[ $key ] = '';
}
}
update_option( $option, $option_array );
} elseif ( 'jetpack_active_modules' === $option ) {
// Clear some Jetpack options to disable specific modules.
$modules_to_disable = array( 'enhanced-distribution', 'publicize', 'subscriptions' );
$modules_array = array_filter(
$option_value,
function( $v ) use ( $modules_to_disable ) {
return ! in_array( $v, $modules_to_disable, true );
},
);

update_option( $option, $modules_array );
} elseif ( 'wprus' === $option ) {
// Clear some WP Remote Users Sync options to disable only keys needed for remote connections and keep the remaining settings intact.
$keys_to_scrub = array(
'encryption' => array(
'aes_key',
'hmac_key',
),
);
$option_array = $option_value;
foreach ( $keys_to_scrub as $index => $keys ) {
if ( array_key_exists( $index, $option_array ) ) {
foreach ( $keys as $key ) {
if ( array_key_exists( $key, $option_array[ $index ] ) ) {
$option_array[ $index ][ $key ] = '';
}
}
}
}
update_option( $option, $option_array );
} else {
// Some plugins don't like it when options are deleted, so we will save their value as either an empty string or array, depending on which it already is.
if ( is_array( get_option( $option ) ) ) {
$empty_array = array();
update_option( $option, $empty_array );
} else {
update_option( $option, '' );
}
}
}
}

update_option( 'safety_net_options_scrubbed', true );
}
1 change: 1 addition & 0 deletions safety-net.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
require_once __DIR__ . '/includes/delete.php';
require_once __DIR__ . '/includes/utilities.php';
require_once __DIR__ . '/includes/deactivate-plugins.php';
require_once __DIR__ . '/includes/scrub-options.php';

if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once __DIR__ . '/includes/classes/cli/class-safetynet-cli.php';
Expand Down

0 comments on commit 41d25c5

Please sign in to comment.