Skip to content

Commit

Permalink
Update logic so the actions always fire under the correct conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
wpscholar committed Jan 25, 2024
1 parent 6f63e54 commit ac2a859
Showing 1 changed file with 78 additions and 11 deletions.
89 changes: 78 additions & 11 deletions includes/ComingSoon.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,21 @@ public function __construct( Container $container ) {
new PrePublishModal();
}

/**
* When the coming soon state is updated, make sure we trigger actions and update the legacy option value.
*
* @param mixed $old_value
* @param mixed $value
*
* @return mixed
*/
public function on_update_nfd_coming_soon( $old_value, $value ) {

// When the database value changes, make sure we fire the appropriate action for enabling/disabling.
add_action(
'shutdown',
function () use ( $value ) {
if ( wp_validate_boolean( $value ) ) {
do_action( 'newfold/coming-soon/enabled' );
} else {
do_action( 'newfold/coming-soon/disabled' );
}
}
);
// Ensure the value is a boolean.
$value = wp_validate_boolean( $value );

// Trigger any actions associated with the coming soon state.
$this->conditionally_trigger_coming_soon_action_hooks( $value );

// When the database value changes for the new value, make sure we update the legacy value.
remove_filter( 'update_option_mm_coming_soon', array( $this, 'on_update_mm_coming_soon' ) );
Expand All @@ -92,8 +94,22 @@ function () use ( $value ) {
return $value;
}

/**
* When the coming soon state is updated, make sure we trigger actions and update the new option value.
*
* @param mixed $old_value
* @param mixed $value
*
* @return mixed
*/
public function on_update_mm_coming_soon( $old_value, $value ) {

// Ensure the value is a boolean.
$value = wp_validate_boolean( $value );

// Trigger any actions associated with the coming soon state.
$this->conditionally_trigger_coming_soon_action_hooks( $value );

// When the database value changes for the legacy value, make sure we update the new value.
remove_filter( 'update_option_nfd_coming_soon', array( $this, 'on_update_nfd_coming_soon' ) );
update_option( 'nfd_coming_soon', $value );
Expand All @@ -102,6 +118,57 @@ public function on_update_mm_coming_soon( $old_value, $value ) {
return $value;
}

/**
* Conditionally trigger coming soon actions.
*
* The data module only starts listening for events after the init hook.
* - If the init hook has run, we trigger the action immediately.
* - If the init hook has not run, we add a callback to the init hook to trigger the action.
*
* @param bool $isEnabled
*
* @return void
*/
public function conditionally_trigger_coming_soon_action_hooks( bool $isEnabled ) {
if ( $isEnabled ) {
if ( did_action( 'init' ) ) {
$this->trigger_enabled_action_hook();
} else {
add_action( 'init', array( $this, 'trigger_enabled_action_hook' ), 99 );
}
} else {
if ( did_action( 'init' ) ) {
$this->trigger_disabled_action_hook();
} else {
add_action( 'init', array( $this, 'trigger_disabled_action_hook' ), 99 );
}
}
}

/**
* Trigger the enabled action hook.
*
* @return void
*/
public function trigger_enabled_action_hook() {
if ( ! did_action( 'newfold/coming-soon/enabled' ) ) {
ray( 'enabled' );
do_action( 'newfold/coming-soon/enabled' );
}
}

/**
* Trigger the disabled action hook.
*
* @return void
*/
public function trigger_disabled_action_hook() {
if ( ! did_action( 'newfold/coming-soon/disabled' ) ) {
ray( 'disabled' );
do_action( 'newfold/coming-soon/disabled' );
}
}

/**
* If nfd_coming_soon is not defined, set it to the value of mm_coming_soon.
*
Expand Down

0 comments on commit ac2a859

Please sign in to comment.