-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
81 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters