Skip to content

Commit

Permalink
Merge branch 'main' into add/hooks-from-ecom-module
Browse files Browse the repository at this point in the history
* 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
circlecube committed Jan 8, 2024
2 parents 69f317f + 9719bae commit ec61466
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 0 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,53 @@ Coming Soon functionality for WordPress.
setContainer( $nfd_module_container );
```

### 4. API
The module offers the ability to check the status of the coming soon, enable the coming soon or disable the coming soon.

For PHP applications, you can use its container service provider to interact with the API.

#### Container Service Provider Usage
```php
use NewfoldLabs\WP\ModuleLoader\Container;

// Check if coming soon is enabled
public function check_coming_soon() {
if ( $this->container->has( 'comingSoon' ) ) {
$comingSoonService = $this->container->get( 'comingSoon' );
return $comingSoonService->is_enabled();
}
}

// Enable coming soon
public function enable_coming_soon() {
if ( $this->container->has( 'comingSoon' ) ) {
$comingSoonService = $this->container->get( 'comingSoon' );
$comingSoonService->enable();
}
}

// Disable coming soon
public function disable_coming_soon() {
if ( $this->container->has( 'comingSoon' ) ) {
$comingSoonService = $this->container->get( 'comingSoon' );
$comingSoonService->disable();
}
}
```

For JavaScript applications, you can use `NewfoldRuntime.comingSoon` window object to interact with the API.

The object exposes three asynchronous methods:
1. `isEnable`: Checks the current state of the coming soon and returns a boolean value or null on error.
2. `enable`: Enables the coming soon. It will return an object containing two properties, `success` and `comingSoon.` 'success' is a boolean that indicates whether the operation succeeded or not. 'comingSoon' is also a boolean with the new value. If the operation does not succeed, the object will not contain a 'comingSoon' property.
3. `disable`: Disables the coming soon. It will return an object containing two properties, `success` and `comingSoon.` 'success' is a boolean that indicates whether the operation succeeded or not. 'comingSoon' is also a boolean with the new value. If the operation does not succeed, the object will not contain a 'comingSoon' property.

All three methods are asynchronous, so keep that in mind when calling them.

#### Window API (JavaScript) Usage
```JavaScript
await NewfoldRuntime.comingSoon.isEnabled();
await NewfoldRuntime.comingSoon.enable();
await NewfoldRuntime.comingSoon.disable();

[More on Newfold WordPress Modules](https://github.com/newfold-labs/wp-module-loader)
4 changes: 4 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use NewfoldLabs\WP\ModuleLoader\Container;
use NewfoldLabs\WP\Module\ComingSoon\ComingSoon;
use NewfoldLabs\WP\Module\ComingSoon\Service;
use function NewfoldLabs\WP\ModuleLoader\register;

if ( function_exists( 'add_action' ) ) {
Expand All @@ -24,6 +25,9 @@ function () {
if ( ! defined( 'NFD_COMING_SOON_BUILD_URL' ) && defined( 'NFD_COMING_SOON_VERSION' ) ) {
define( 'NFD_COMING_SOON_BUILD_URL', $container->plugin()->url . 'vendor/newfold-labs/wp-module-coming-soon/build/' . NFD_COMING_SOON_VERSION );
}
$container->set( 'comingSoon', $container->service( function () {
return new Service();
}) );
return new ComingSoon( $container );
},
'isActive' => true,
Expand Down
104 changes: 104 additions & 0 deletions includes/API/ComingSoon.php
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;
}
}
24 changes: 24 additions & 0 deletions includes/ComingSoon.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public function __construct( Container $container ) {
$this->args['template_styles'] = $this->args['template_styles'] . '?v=' . container()->plugin()->version;
}
// 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( 'admin_notices', array( $this, 'notice_display' ) );
\add_action( 'template_redirect', array( $this, 'maybe_load_template' ) );
\add_action( 'wp_ajax_newfold_coming_soon_subscribe', array( $this, 'coming_soon_subscribe' ) );
Expand All @@ -62,6 +64,28 @@ public function __construct( Container $container ) {
new PrePublishModal();
}

/**
* Enqueue admin scripts.
*/
public function enqueue_admin_scripts() {
$assetsDir = container()->plugin()->url . 'vendor/newfold-labs/wp-module-coming-soon/static/js/';

wp_enqueue_script(
'newfold-coming-soon-api',
$assetsDir . 'coming-soon.js',
array( 'wp-api-fetch', 'nfd-runtime' ),
container()->plugin()->version,
true
);
}

/**
* Register the coming soon route.
*/
public function rest_api_init() {
new API\ComingSoon();
}

/**
* Display coming soon notice.
*/
Expand Down
41 changes: 41 additions & 0 deletions includes/Service.php
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' );
}
}
87 changes: 87 additions & 0 deletions static/js/coming-soon.js
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();
} );
}

0 comments on commit ec61466

Please sign in to comment.