From 575e4d70811c3e5adacb9bbb837df4fe5dee0760 Mon Sep 17 00:00:00 2001 From: Collyn Philleo Date: Tue, 7 Jan 2025 15:24:38 -0800 Subject: [PATCH] Add WordAds WoA post transfer action (#38915) * Add post transfer action to enable the WordAds module if applicable * changelog * Set options passed in the CLI flag * Decode the JSON as an associative array * Adding allowlist of options to set * Remove check for allow listed options --- .../update-wpcomsh-woa-transfer-wordads | 4 ++ projects/plugins/wpcomsh/woa.php | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 projects/plugins/wpcomsh/changelog/update-wpcomsh-woa-transfer-wordads diff --git a/projects/plugins/wpcomsh/changelog/update-wpcomsh-woa-transfer-wordads b/projects/plugins/wpcomsh/changelog/update-wpcomsh-woa-transfer-wordads new file mode 100644 index 0000000000000..0d21b0f097a33 --- /dev/null +++ b/projects/plugins/wpcomsh/changelog/update-wpcomsh-woa-transfer-wordads @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +WOA: add WordAds post transfer action diff --git a/projects/plugins/wpcomsh/woa.php b/projects/plugins/wpcomsh/woa.php index b2d69e07717ce..b75df2dea8597 100644 --- a/projects/plugins/wpcomsh/woa.php +++ b/projects/plugins/wpcomsh/woa.php @@ -226,3 +226,42 @@ function wpcomsh_woa_post_transfer_install_marketplace_software( $args, $assoc_a } } add_action( 'wpcomsh_woa_post_transfer', 'wpcomsh_woa_post_transfer_install_marketplace_software', 10, 2 ); + +/** + * Sets WordAds options and enables the WordAds Jetpack module if required. + * + * @param array $args Arguments. + * @param array $assoc_args Associated arguments. + * + * @return void + */ +function wpcomsh_woa_post_process_maybe_enable_wordads( $args, $assoc_args ) { + + // wordads-options is expected to be a JSON object with option name=>value pairs. + $wordads_options = WP_CLI\Utils\get_flag_value( $assoc_args, 'wordads-options', false ); + + if ( false === $wordads_options ) { + return; + } + + $options_decoded = json_decode( $wordads_options, true ); + + if ( ! is_array( $options_decoded ) ) { + return; + } + + foreach ( $options_decoded as $option => $value ) { + // Convert boolean options to string first to work around update_option not setting the option if the value is false. + // This sets the option to either '1' if true or '' if false. + update_option( $option, is_bool( $value ) ? (string) $value : $value ); + } + + if ( ! defined( 'JETPACK__VERSION' ) || ! class_exists( 'Jetpack' ) ) { + return; + } + + if ( ! Jetpack::is_module_active( 'wordads' ) ) { + Jetpack::activate_module( 'wordads', false, false ); + } +} +add_action( 'wpcomsh_woa_post_transfer', 'wpcomsh_woa_post_process_maybe_enable_wordads', 10, 2 );