-
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.
Merge branch 'main' into add/hooks-from-ecom-module
* main: remove unnecessary postcss config file Add missing package-lock file and postcss config Update API docs Code comments Update `comingSoon.isEnabled` to return boolean value or null on error JavaScript API and API routes Add service provider class to the container Update README.md Add new API docs # Conflicts: # package-lock.json
- Loading branch information
Showing
6 changed files
with
309 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
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,104 @@ | ||
<?php | ||
/** | ||
* Coming Soon Module API | ||
* | ||
* @package NewfoldLabs\WP\Module\ComingSoon\API | ||
*/ | ||
|
||
namespace NewfoldLabs\WP\Module\ComingSoon\API; | ||
|
||
use function NewfoldLabs\WP\ModuleLoader\container; | ||
|
||
/** | ||
* Class ComingSoon | ||
* | ||
* @package NewfoldLabs\WP\Module\ComingSoon\API | ||
*/ | ||
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'), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Check if the coming soon page is enabled. | ||
* | ||
* @return array | ||
*/ | ||
public function check_status() { | ||
$coming_soon_service = container()->get( 'comingSoon' ); | ||
return array( 'comingSoon' => $coming_soon_service->is_enabled() ); | ||
} | ||
|
||
/** | ||
* Enable the coming soon page. | ||
* | ||
* @return array | ||
*/ | ||
public function enable() { | ||
$coming_soon_service = container()->get( 'comingSoon' ); | ||
$coming_soon_service->enable(); | ||
return array( 'comingSoon' => true ); | ||
} | ||
|
||
/** | ||
* Disable the coming soon page. | ||
* | ||
* @return array | ||
*/ | ||
public function disable() { | ||
$coming_soon_service = container()->get( 'comingSoon' ); | ||
$coming_soon_service->disable(); | ||
return array( 'comingSoon' => false ); | ||
} | ||
|
||
/** | ||
* Check if the current user has permissions to access the API. | ||
* Or if the service provider is not available, return an error. | ||
* | ||
* @return bool|\WP_Error | ||
*/ | ||
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,41 @@ | ||
<?php | ||
/** | ||
* Coming soon service provider. | ||
* | ||
* @package NewfoldLabs\WP\Module\ComingSoon | ||
*/ | ||
|
||
namespace NewfoldLabs\WP\Module\ComingSoon; | ||
|
||
/** | ||
* A service provider class to interact with the coming soon module from the container. | ||
**/ | ||
class Service | ||
{ | ||
/** | ||
* Enable the coming soon page. | ||
* | ||
* @return void | ||
*/ | ||
public function enable() { | ||
update_option( 'nfd_coming_soon', 'true' ); | ||
} | ||
|
||
/** | ||
* Disable the coming soon page. | ||
* | ||
* @return void | ||
*/ | ||
public function disable() { | ||
update_option( 'nfd_coming_soon', 'false' ); | ||
} | ||
|
||
/** | ||
* Check if the coming soon page is enabled. | ||
* | ||
* @return bool | ||
*/ | ||
public function is_enabled() { | ||
return 'true' === get_option( 'nfd_coming_soon', 'false' ); | ||
} | ||
} |
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,87 @@ | ||
{ | ||
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 () => { | ||
let status; | ||
|
||
await window.wp | ||
.apiFetch( { | ||
url: `${ API_ENDPOINT }/status`, | ||
method: 'GET', | ||
} ) | ||
.then( ( response ) => { | ||
if ( response.hasOwnProperty( 'comingSoon' ) ) { | ||
status = response.comingSoon; | ||
} else { | ||
status = null; | ||
} | ||
} ) | ||
.catch( () => { | ||
status = null; | ||
} ); | ||
|
||
return status; | ||
}; | ||
|
||
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(); | ||
} ); | ||
} |