-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace NewfoldLabs\WP\Module\ComingSoon\API; | ||
|
||
use function NewfoldLabs\WP\ModuleLoader\container; | ||
|
||
class ComingSoon | ||
{ | ||
private $namespace = 'newfold-coming-soon/v1'; | ||
|
||
public function __construct() { | ||
$this->register_routes(); | ||
} | ||
|
||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
'/status', | ||
array( | ||
'methods' => \WP_REST_Server::READABLE, | ||
'permission_callback' => array( $this, 'check_permissions' ), | ||
'callback' => array( $this, 'check_status' ), | ||
) | ||
); | ||
|
||
register_rest_route( | ||
$this->namespace, | ||
'/enable', | ||
array( | ||
'methods' => \WP_REST_Server::EDITABLE, | ||
'permission_callback' => array( $this, 'check_permissions' ), | ||
'callback' => array( $this, 'enable'), | ||
) | ||
); | ||
|
||
register_rest_route( | ||
$this->namespace, | ||
'/disable', | ||
array( | ||
'methods' => \WP_REST_Server::EDITABLE, | ||
'permission_callback' => array( $this, 'check_permissions' ), | ||
'callback' => array( $this, 'disable'), | ||
) | ||
); | ||
} | ||
|
||
public function check_status() { | ||
$coming_soon_service = container()->get( 'comingSoon' ); | ||
return array( 'comingSoon' => $coming_soon_service->is_enabled() ); | ||
} | ||
|
||
public function enable() { | ||
$coming_soon_service = container()->get( 'comingSoon' ); | ||
$coming_soon_service->enable(); | ||
return array( 'comingSoon' => true ); | ||
} | ||
|
||
public function disable() { | ||
$coming_soon_service = container()->get( 'comingSoon' ); | ||
$coming_soon_service->disable(); | ||
return array( 'comingSoon' => false ); | ||
} | ||
|
||
public function check_permissions() { | ||
if ( ! current_user_can( 'manage_options' ) ) { | ||
return new \WP_Error( 'rest_forbidden', esc_html__( 'You cannot access the resource.' ), array( 'status' => 401 ) ); | ||
} | ||
if ( ! container()->has( 'comingSoon' ) ) { | ||
return new \WP_Error( 'rest_forbidden', esc_html__( 'Coming Soon module service provider error.' ), array( 'status' => 401 ) ); | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
{ | ||
const API_ENDPOINT = window.NewfoldRuntime.restUrl + 'newfold-coming-soon/v1'; | ||
|
||
const attachToRuntime = () => { | ||
window.NewfoldRuntime.comingSoon = buildObject(); | ||
}; | ||
|
||
const buildObject = () => { | ||
return { | ||
isEnabled: checkComingSoonStatus, | ||
enable: enableComingSoon, | ||
disable: disableComingSoon, | ||
}; | ||
}; | ||
|
||
const checkComingSoonStatus = async () => { | ||
const result = {}; | ||
|
||
await window.wp | ||
.apiFetch( { | ||
url: `${ API_ENDPOINT }/status`, | ||
method: 'GET', | ||
} ) | ||
.then( ( response ) => { | ||
if ( response.hasOwnProperty( 'comingSoon' ) ) { | ||
result.success = true; | ||
result.comingSoon = response.comingSoon; | ||
} else { | ||
result.success = false; | ||
} | ||
} ) | ||
.catch( () => { | ||
result.success = false; | ||
} ); | ||
|
||
return result; | ||
}; | ||
|
||
const enableComingSoon = async () => { | ||
const result = {}; | ||
|
||
await window.wp | ||
.apiFetch( { | ||
url: `${ API_ENDPOINT }/enable`, | ||
method: 'POST', | ||
} ) | ||
.then( ( response ) => { | ||
if ( response.hasOwnProperty( 'comingSoon' ) ) { | ||
result.success = true; | ||
result.comingSoon = response.comingSoon; | ||
} else { | ||
result.success = false; | ||
} | ||
} ) | ||
.catch( () => { | ||
result.success = false; | ||
} ); | ||
|
||
return result; | ||
}; | ||
|
||
const disableComingSoon = async () => { | ||
const result = {}; | ||
|
||
await window.wp | ||
.apiFetch( { | ||
url: `${ API_ENDPOINT }/disable`, | ||
method: 'POST', | ||
} ) | ||
.then( ( response ) => { | ||
if ( response.hasOwnProperty( 'comingSoon' ) ) { | ||
result.success = true; | ||
result.comingSoon = response.comingSoon; | ||
} else { | ||
result.success = false; | ||
} | ||
} ) | ||
.catch( () => { | ||
result.success = false; | ||
} ); | ||
|
||
return result; | ||
}; | ||
|
||
window.addEventListener( 'DOMContentLoaded', () => { | ||
attachToRuntime(); | ||
} ); | ||
} |