introduce query filter for REST API

Enabled WP REST API to query for ticker slug, GMT timestamp and limit
parameter as used by AJAX calls. This is done in preparation for
migration to WP REST instead of WP AJAX.
This commit is contained in:
Stefan Kalscheuer 2020-11-10 10:05:51 +01:00
parent af609d8928
commit ca0311622e
6 changed files with 4323 additions and 26 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "stklcode/stklcode-liveticker", "name": "stklcode/stklcode-liveticker",
"version": "1.1.1", "version": "1.2.0-alpha",
"description": "A simple Liveticker for Wordpress.", "description": "A simple Liveticker for Wordpress.",
"keywords": [ "keywords": [
"wordpress", "wordpress",

73
includes/class-api.php Normal file
View File

@ -0,0 +1,73 @@
<?php
/**
* Liveticker: Plugin API class.
*
* This file contains the plugin's REST API extensions.
*
* @package SCLiveticker
*/
namespace SCLiveticker;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Liveticker.
*
* @since 1.2
*/
class Api {
/**
* Filter tick queries by ticker slug and date.
*
* @param array $args Query vars.
* @param \WP_REST_Request $request The REST request.
*
* @return array Filtered query values.
*/
public static function tick_query_filter( $args, $request ) {
// Extract arguments.
$ticker_slug = $request->get_param( 'ticker' );
$limit = intval( $request->get_param( 'limit' ) );
$last_poll = intval( $request->get_param( 'last' ) );
if ( ! empty( $ticker_slug ) ) {
$args['tax_query'][] = array(
'taxonomy' => 'scliveticker_ticker',
'field' => 'slug',
'terms' => $ticker_slug,
);
}
if ( ! empty( $limit ) ) {
$args['posts_per_page'] = $limit;
}
if ( $last_poll > 0 ) {
$last_poll = explode(
',',
gmdate(
'Y,m,d,H,i,s',
$last_poll
)
);
$args['date_query'] = array(
'column' => 'post_date_gmt',
'after' => array(
'year' => intval( $last_poll[0] ),
'month' => intval( $last_poll[1] ),
'day' => intval( $last_poll[2] ),
'hour' => intval( $last_poll[3] ),
'minute' => intval( $last_poll[4] ),
'second' => intval( $last_poll[5] ),
),
);
}
return $args;
}
}

View File

@ -26,7 +26,7 @@ class SCLiveticker {
* *
* @var string OPTIONS * @var string OPTIONS
*/ */
const VERSION = '1.1.1'; const VERSION = '1.2.0';
/** /**
* Options tag. * Options tag.
@ -71,7 +71,10 @@ class SCLiveticker {
// Load plugin options. // Load plugin options.
self::update_options(); self::update_options();
// Skip on AJAX if not enabled disabled. // Add filter for REST API queries.
add_filter( 'rest_scliveticker_tick_query', array( 'SCLiveticker\\Api', 'tick_query_filter' ), 10, 2 );
// Skip on AJAX if not enabled.
if ( ( ! isset( self::$options['enable_ajax'] ) || 1 !== self::$options['enable_ajax'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { if ( ( ! isset( self::$options['enable_ajax'] ) || 1 !== self::$options['enable_ajax'] ) && ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return; return;
} }
@ -310,7 +313,7 @@ class SCLiveticker {
$is_widget = true; $is_widget = true;
$slug = sanitize_text_field( $update_req['w'] ); $slug = sanitize_text_field( $update_req['w'] );
} else { } else {
// Should never occur, but for completenes' sake... // Should never occur, but for completeness' sake...
break; break;
} }

4258
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "stklcode-liveticker", "name": "stklcode-liveticker",
"version": "1.1.1", "version": "1.2.0-alpha",
"description": "A simple Liveticker for Wordpress.", "description": "A simple Liveticker for Wordpress.",
"author": "Stefan Kalscheuer", "author": "Stefan Kalscheuer",
"license": "GPL-2.0+", "license": "GPL-2.0+",

View File

@ -9,7 +9,7 @@
* @wordpress-plugin * @wordpress-plugin
* Plugin Name: Liveticker (by stklcode) * Plugin Name: Liveticker (by stklcode)
* Description: A simple Liveticker for WordPress. * Description: A simple Liveticker for WordPress.
* Version: 1.1.1 * Version: 1.2.0-alpha
* Author: Stefan Kalscheuer * Author: Stefan Kalscheuer
* Author URI: https://www.stklcode.de * Author URI: https://www.stklcode.de
* Text Domain: stklcode-liveticker * Text Domain: stklcode-liveticker
@ -72,6 +72,7 @@ function scliveticker_autoload( $class ) {
$plugin_classes = array( $plugin_classes = array(
'SCLiveticker\\SCLiveticker', 'SCLiveticker\\SCLiveticker',
'SCLiveticker\\Admin', 'SCLiveticker\\Admin',
'SCLiveticker\\Api',
'SCLiveticker\\System', 'SCLiveticker\\System',
'SCLiveticker\\Widget', 'SCLiveticker\\Widget',
); );