diff --git a/includes/class-api.php b/includes/class-api.php index 97bc8fe..2134954 100644 --- a/includes/class-api.php +++ b/includes/class-api.php @@ -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 ) ) ); diff --git a/includes/class-scliveticker.php b/includes/class-scliveticker.php index 580121f..109e4e7 100644 --- a/includes/class-scliveticker.php +++ b/includes/class-scliveticker.php @@ -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.