Archived
Adds an example JSON API Events Controller
This commit is contained in:
1 parent
5960814a06
commit
b2bc6b5f5f
2 files changed
+60
No files matched your search
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Custom events controller providing handy methods.
|
||||
*/
|
||||
class JSON_API_Events_Controller {
|
||||
|
||||
/* /?json=events.get_upcoming */
|
||||
public function get_upcoming() {
|
||||
global $json_api;
|
||||
$meta_query = array(
|
||||
'key' => '_neuf_events_starttime',
|
||||
'value' => date( 'U' , strtotime( '-8 hours' ) ),
|
||||
'compare' => '>',
|
||||
'type' => 'numeric'
|
||||
);
|
||||
|
||||
$query = array(
|
||||
'post_type' => 'event',
|
||||
'meta_query' => array( $meta_query ),
|
||||
'posts_per_page' => 50,
|
||||
'orderby' => 'meta_value_num',
|
||||
'meta_key' => '_neuf_events_starttime',
|
||||
'order' => 'ASC'
|
||||
);
|
||||
$custom_fields = array(
|
||||
'_neuf_events_price',
|
||||
'_neuf_events_starttime',
|
||||
'_neuf_events_endtime',
|
||||
'_neuf_events_venue',
|
||||
'_neuf_events_bs_url',
|
||||
'_neuf_events_fb_url',
|
||||
);
|
||||
$json_api->query->custom_fields = implode(',', $custom_fields);
|
||||
$events = $json_api->introspector->get_posts($query);
|
||||
return $this->events_result($events);
|
||||
}
|
||||
protected function events_result($events) {
|
||||
global $wp_query;
|
||||
return array(
|
||||
'count' => count($events),
|
||||
'count_total' => (int) $wp_query->found_posts,
|
||||
'pages' => $wp_query->max_num_pages,
|
||||
'events' => $events
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -32,5 +32,17 @@ add_shortcode( 'neuf-events-program' , 'neuf_events_program' );
|
||||
function neuf_events_i18n() {
|
||||
load_plugin_textdomain( 'neuf_event', false, dirname( plugin_basename( __FILE__ ) ) .'/languages' );
|
||||
}
|
||||
/* JSON API controller */
|
||||
function add_events_controller($controllers) {
|
||||
$controllers[] = 'events';
|
||||
return $controllers;
|
||||
}
|
||||
add_filter('json_api_controllers', 'add_events_controller');
|
||||
|
||||
function set_events_controller_path() {
|
||||
return WP_PLUGIN_DIR."/".dirname( plugin_basename( __FILE__ ))."/neuf-events-json-controller.php";
|
||||
}
|
||||
add_filter('json_api_events_controller_path', 'set_events_controller_path');
|
||||
|
||||
|
||||
?>
|
||||
Reference in new issue
Block a user