diff --git a/includes/ComingSoon.php b/includes/ComingSoon.php index ec9e746..d06f58e 100644 --- a/includes/ComingSoon.php +++ b/includes/ComingSoon.php @@ -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' ) ); @@ -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 ); @@ -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. *