Skip to content

Commit

Permalink
JavaScript API and API routes
Browse files Browse the repository at this point in the history
  • Loading branch information
wpalani committed Jan 4, 2024
1 parent b9c8a88 commit 1716129
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
73 changes: 73 additions & 0 deletions includes/API/ComingSoon.php
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;
}
}
24 changes: 24 additions & 0 deletions includes/ComingSoon.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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( 'admin_bar_menu', array( $this, 'add_tool_bar_item' ), 100 );
\add_action( 'template_redirect', array( $this, 'maybe_load_template' ) );
Expand All @@ -57,6 +59,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
88 changes: 88 additions & 0 deletions static/js/coming-soon.js
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();
} );
}

0 comments on commit 1716129

Please sign in to comment.