add formatted date to REST response and use RFC3339 for the query

This commit is contained in:
Stefan Kalscheuer 2020-11-10 15:27:31 +01:00
parent ca0311622e
commit 547fc521d0
2 changed files with 28 additions and 3 deletions

View File

@ -20,6 +20,28 @@ if ( ! defined( 'ABSPATH' ) ) {
* @since 1.2
*/
class Api {
/**
* Initialize custom fields for REST API responses.
*
* @return void
*/
public static function init() {
// Add rendered modification date to WP_Post object.
register_rest_field(
'scliveticker_tick',
'modified_rendered',
array(
'get_callback' => function ( $post ) {
return get_the_modified_date( 'd.m.Y H:i', $post );
},
'schema' => array(
'description' => __( 'Rendered modification date and time.', 'stklcode-liveticker' ),
'type' => 'string',
),
)
);
}
/**
* Filter tick queries by ticker slug and date.
*
@ -32,7 +54,7 @@ class Api {
// Extract arguments.
$ticker_slug = $request->get_param( 'ticker' );
$limit = intval( $request->get_param( 'limit' ) );
$last_poll = intval( $request->get_param( 'last' ) );
$last_poll = $request->get_param( 'last' );
if ( ! empty( $ticker_slug ) ) {
$args['tax_query'][] = array(
@ -42,8 +64,10 @@ class Api {
);
}
if ( ! empty( $limit ) ) {
if ( ! empty( $limit ) && $limit > 0 ) {
$args['posts_per_page'] = $limit;
} else {
$args['paged'] = false;
}
if ( $last_poll > 0 ) {
@ -51,7 +75,7 @@ class Api {
',',
gmdate(
'Y,m,d,H,i,s',
$last_poll
rest_parse_date( $last_poll )
)
);

View File

@ -72,6 +72,7 @@ class SCLiveticker {
self::update_options();
// Add filter for REST API queries.
add_filter( 'rest_api_init', array( 'SCLiveticker\\Api', 'init' ) );
add_filter( 'rest_scliveticker_tick_query', array( 'SCLiveticker\\Api', 'tick_query_filter' ), 10, 2 );
// Skip on AJAX if not enabled.