Skip to content

Commit

Permalink
Add WordAds WoA post transfer action (#38915)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
cphilleo authored Jan 7, 2025
1 parent cd2e057 commit 575e4d7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

WOA: add WordAds post transfer action
39 changes: 39 additions & 0 deletions projects/plugins/wpcomsh/woa.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

0 comments on commit 575e4d7

Please sign in to comment.