Skip to content

Commit

Permalink
Merge pull request #21 from newfold-labs/add/onboarding-hook
Browse files Browse the repository at this point in the history
Disable coming soon after onboarding if the user didn't opt-in
  • Loading branch information
wpalani authored Jan 11, 2024
2 parents 726f18d + 61b3fdb commit 3758f8e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
49 changes: 43 additions & 6 deletions includes/API/ComingSoon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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'),
)
);
}

/**
Expand All @@ -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() );
}

/**
Expand All @@ -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 );
}

Expand All @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions includes/ComingSoon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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.
*/
Expand Down
41 changes: 39 additions & 2 deletions includes/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,25 @@ class Service
*
* @return void
*/
public function enable() {
public function enable( $timestamp = true ) {
update_option( 'nfd_coming_soon', 'true' );

if ( $timestamp ) {
$this->last_changed_timestamp();
}
}

/**
* Disable the coming soon page.
*
* @return void
*/
public function disable() {
public function disable( $timestamp = true ) {
update_option( 'nfd_coming_soon', 'false' );

if ( $timestamp ) {
$this->last_changed_timestamp();
}
}

/**
Expand All @@ -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;
}
}
23 changes: 23 additions & 0 deletions static/js/coming-soon.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
isEnabled: checkComingSoonStatus,
enable: enableComingSoon,
disable: disableComingSoon,
lastChanged: getLastChanged,
};
};

Expand Down Expand Up @@ -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();
} );
Expand Down
2 changes: 1 addition & 1 deletion upgrades/1.1.14.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
$isFreshInstall = container()->has( 'isFreshInstallation' ) ? container()->get( 'isFreshInstallation' ) : false;
if ( $isFreshInstall ) {
$comingSoonService = new Service();
$comingSoonService->enable();
$comingSoonService->enable( false );
}
} );

0 comments on commit 3758f8e

Please sign in to comment.