<?php
/**
 * REST API endpoints for AxcelersBlocks plugin.
 *
 * @package AxcelersBlocks
 * @since 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class AxcelersBlocks_Rest_API
 *
 * Manages REST API endpoints for the AxcelersBlocks plugin.
 */
class AxcelersBlocks_Rest_API {

	/**
	 * Initialize hooks.
	 *
	 * @since 1.0.0
	 */
	public function init() {
		add_action( 'rest_api_init', array( $this, 'register_endpoints' ) );
	}

	/**
	 * Register REST API endpoints.
	 *
	 * @since 1.0.0
	 */
	public function register_endpoints() {

		register_rest_route(
			'axcelersblocks/v1',
			'/device-settings',
			array(
				'methods'             => \WP_REST_Server::READABLE, // GET
				'callback'            => array( $this, 'get_device_settings' ),
				'permission_callback' => array( $this, 'can_edit_posts' ),
			)
		);
	}

	/**
	 * Basic permission for editor-level features.
	 *
	 * @return bool
	 */
	public function can_edit_posts() {
		return current_user_can( 'edit_posts' );
	}

	/**
	 * Callback to return device settings via REST API.
	 *
	 * @param \WP_REST_Request $request Request.
	 * @return \WP_REST_Response
	 *
	 * @since 1.0.0
	 */
	public function get_device_settings( \WP_REST_Request $request ) {
		$responsive_settings = get_option(
			'axcelersblocks_responsive_settings',
			array(
				'devices' => array(
					'tablet' => array(
						'name'       => 'Tablet',
						'width'      => '1024px',
						'is_default' => true,
					),
					'mobile' => array(
						'name'       => 'Mobile',
						'width'      => '768px',
						'is_default' => true,
					),
				),
			)
		);

		return new \WP_REST_Response( $responsive_settings, 200 );
	}

}
