diff --git a/includes/API/ComingSoon.php b/includes/API/ComingSoon.php new file mode 100644 index 0000000..c4d2b5b --- /dev/null +++ b/includes/API/ComingSoon.php @@ -0,0 +1,73 @@ +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; + } +} \ No newline at end of file diff --git a/includes/ComingSoon.php b/includes/ComingSoon.php index 76c0ea9..029b198 100644 --- a/includes/ComingSoon.php +++ b/includes/ComingSoon.php @@ -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' ) ); @@ -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. */ diff --git a/static/js/coming-soon.js b/static/js/coming-soon.js new file mode 100644 index 0000000..5e53ab8 --- /dev/null +++ b/static/js/coming-soon.js @@ -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(); + } ); +}