Files
SIGE-WEB-snapshot/wp-content/plugins/modern-events-calendar-lite/app/features/restful.php

137 lines
4.1 KiB
PHP

<?php
/** no direct access **/
defined('MECEXEC') or die();
/**
* Webnus MEC RESTful class.
* @author Webnus <info@webnus.net>
*/
class MEC_feature_restful extends MEC_base
{
/**
* @var MEC_factory
*/
public $factory;
/**
* @var MEC_restful
*/
public $restful;
private $settings;
/**
* Constructor method
* @author Webnus <info@webnus.net>
*/
public function __construct()
{
// Import MEC Factory
$this->factory = $this->getFactory();
// Import MEC RESTful
$this->restful = $this->getRestful();
// MEC Settings
$this->settings = $this->getMain()->get_settings();
}
/**
* Initialize
* @author Webnus <info@webnus.net>
*/
public function init()
{
// Disabled
if(!isset($this->settings['restful_api_status']) || !$this->settings['restful_api_status']) return;
$this->factory->action('rest_api_init', [$this, 'register']);
}
public function register()
{
// Get Events
register_rest_route($this->restful->get_namespace(), 'events', [
'methods' => 'GET',
'callback' => [$this, 'events'],
'permission_callback' => [$this->restful, 'guest'],
]);
}
public function events(WP_REST_Request $request)
{
$limit = $request->get_param('limit');
if(!$limit) $limit = 12;
if(!is_numeric($limit))
{
return $this->restful->response([
'data' => new WP_Error(400, esc_html__('Limit parameter must be numeric!', 'modern-events-calendar-lite')),
'status' => 400,
]);
}
$order = $request->get_param('order');
if(!$order) $order = 'ASC';
if(!in_array($order, ['ASC', 'DESC']))
{
return $this->restful->response([
'data' => new WP_Error(400, esc_html__('Order parameter is invalid!', 'modern-events-calendar-lite')),
'status' => 400,
]);
}
$start_date_type = $request->get_param('start_date_type');
if(!$start_date_type) $start_date_type = 'today';
$start_date = $request->get_param('start_date');
if($start_date_type === 'date' && !$start_date)
{
return $this->restful->response([
'data' => new WP_Error(400, esc_html__('When the start_date_type parameter is set to date, then start_date parameter is required.', 'modern-events-calendar-lite')),
'status' => 400,
]);
}
$end_date_type = $request->get_param('end_date_type');
if(!$end_date_type) $end_date_type = 'date';
$end_date = $request->get_param('end_date');
$show_only_past_events = (int) $request->get_param('show_only_past_events');
$include_past_events = (int) $request->get_param('include_past_events');
$show_only_ongoing_events = (int) $request->get_param('show_only_ongoing_events');
$include_ongoing_events = (int) $request->get_param('include_ongoing_events');
$args = [
'sk-options' => [
'list' => [
'limit' => $limit,
'order_method' => $order,
'start_date_type' => $start_date_type,
'start_date' => $start_date,
'end_date_type' => $end_date_type,
'maximum_date_range' => $end_date
]
],
'show_only_past_events' => $show_only_past_events,
'show_past_events' => $include_past_events,
'show_only_ongoing_events' => $show_only_ongoing_events,
'show_ongoing_events' => $include_ongoing_events,
];
$EO = new MEC_skin_list(); // Events Object
$EO->initialize($args);
$EO->search();
// Todo: Search & Data Render
return $this->restful->response([
'data' => []
]);
}
}