diff --git a/includes/API/ComingSoon.php b/includes/API/ComingSoon.php index 44fc15d..a5ff356 100644 --- a/includes/API/ComingSoon.php +++ b/includes/API/ComingSoon.php @@ -16,9 +16,25 @@ */ class ComingSoon { + /** + * The namespace for the API. + * + * @var string + */ private $namespace = 'newfold-coming-soon/v1'; + /** + * The coming soon service provider. + * + * @var \NewfoldLabs\WP\Module\ComingSoon\Service + */ + private $coming_soon_service; + + /** + * ComingSoon constructor. + */ public function __construct() { + $this->coming_soon_service = container()->get( 'comingSoon' ); $this->register_routes(); } @@ -52,6 +68,16 @@ public function register_routes() { 'callback' => array( $this, 'disable'), ) ); + + register_rest_route( + $this->namespace, + '/last-changed', + array( + 'methods' => \WP_REST_Server::READABLE, + 'permission_callback' => array( $this, 'check_permissions' ), + 'callback' => array( $this, 'last_changed_timestamp'), + ) + ); } /** @@ -60,8 +86,7 @@ public function register_routes() { * @return array */ public function check_status() { - $coming_soon_service = container()->get( 'comingSoon' ); - return array( 'comingSoon' => $coming_soon_service->is_enabled() ); + return array( 'comingSoon' => $this->coming_soon_service->is_enabled() ); } /** @@ -70,8 +95,7 @@ public function check_status() { * @return array */ public function enable() { - $coming_soon_service = container()->get( 'comingSoon' ); - $coming_soon_service->enable(); + $this->coming_soon_service->enable(); return array( 'comingSoon' => true ); } @@ -81,11 +105,24 @@ public function enable() { * @return array */ public function disable() { - $coming_soon_service = container()->get( 'comingSoon' ); - $coming_soon_service->disable(); + $this->coming_soon_service->disable(); return array( 'comingSoon' => false ); } + /** + * Get the last changed timestamp value. + * + * @return array + */ + public function last_changed_timestamp() { + $last_changed = $this->coming_soon_service->get_last_changed_timestamp( true ); + if ( ! $last_changed ) { + return array( 'lastChanged' => false ); + } + + return array( 'lastChanged' => $last_changed ); + } + /** * Check if the current user has permissions to access the API. * Or if the service provider is not available, return an error. diff --git a/includes/ComingSoon.php b/includes/ComingSoon.php index 029b198..8317927 100644 --- a/includes/ComingSoon.php +++ b/includes/ComingSoon.php @@ -49,6 +49,7 @@ public function __construct( Container $container ) { // set up all actions \add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); \add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); + \add_action( 'newfold/onboarding/completed', array( $this, 'handle_onboarding_completed' ) ); \add_action( 'admin_notices', array( $this, 'notice_display' ) ); \add_action( 'admin_bar_menu', array( $this, 'add_tool_bar_item' ), 100 ); \add_action( 'template_redirect', array( $this, 'maybe_load_template' ) ); @@ -81,6 +82,21 @@ public function rest_api_init() { new API\ComingSoon(); } + /** + * Handle the onboarding complete action. + * When the onboarding is complete, disable the coming soon page if the user has not opted in. + * + * @return void + */ + public function handle_onboarding_completed() { + $coming_soon_service = container()->get( 'comingSoon' ); + + $coming_soon_last_changed = $coming_soon_service->get_last_changed_timestamp(); + if ( ! $coming_soon_last_changed ) { + $coming_soon_service->disable(); + } + } + /** * Display coming soon notice. */ diff --git a/includes/Service.php b/includes/Service.php index b0a5abf..e52b627 100644 --- a/includes/Service.php +++ b/includes/Service.php @@ -17,8 +17,12 @@ class Service * * @return void */ - public function enable() { + public function enable( $timestamp = true ) { update_option( 'nfd_coming_soon', 'true' ); + + if ( $timestamp ) { + $this->last_changed_timestamp(); + } } /** @@ -26,8 +30,12 @@ public function enable() { * * @return void */ - public function disable() { + public function disable( $timestamp = true ) { update_option( 'nfd_coming_soon', 'false' ); + + if ( $timestamp ) { + $this->last_changed_timestamp(); + } } /** @@ -38,4 +46,33 @@ public function disable() { public function is_enabled() { return 'true' === get_option( 'nfd_coming_soon', 'false' ); } + + /** + * Create/update the last changed timestamp. + * + * @return void + */ + private function last_changed_timestamp() { + update_option( 'nfd_coming_soon_last_changed', time() ); + } + + /** + * Get the last changed timestamp. + * + * @return int + */ + public function get_last_changed_timestamp( $as_date = false ) { + $timestamp = get_option( 'nfd_coming_soon_last_changed' ); + + if ( ! $timestamp ) { + return false; + } + + // If requested as date convert and return. + if ( $as_date ) { + return date( 'Y-m-d H:i:s', $timestamp ); + } + + return $timestamp; + } } \ No newline at end of file diff --git a/static/js/coming-soon.js b/static/js/coming-soon.js index c9e7adc..077cefe 100644 --- a/static/js/coming-soon.js +++ b/static/js/coming-soon.js @@ -10,6 +10,7 @@ isEnabled: checkComingSoonStatus, enable: enableComingSoon, disable: disableComingSoon, + lastChanged: getLastChanged, }; }; @@ -81,6 +82,28 @@ return result; }; + const getLastChanged = async () => { + let value; + + await window.wp + .apiFetch( { + url: `${ API_ENDPOINT }/last-changed`, + method: 'GET', + } ) + .then( ( response ) => { + if ( response.hasOwnProperty( 'lastChanged' ) ) { + value = response.lastChanged; + } else { + value = null; + } + } ) + .catch( () => { + value = null; + } ); + + return value; + }; + window.addEventListener( 'DOMContentLoaded', () => { attachToRuntime(); } ); diff --git a/upgrades/1.1.14.php b/upgrades/1.1.14.php index 85cfeae..94f375b 100644 --- a/upgrades/1.1.14.php +++ b/upgrades/1.1.14.php @@ -14,6 +14,6 @@ $isFreshInstall = container()->has( 'isFreshInstallation' ) ? container()->get( 'isFreshInstallation' ) : false; if ( $isFreshInstall ) { $comingSoonService = new Service(); - $comingSoonService->enable(); + $comingSoonService->enable( false ); } } );