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:
parent
af609d8928
commit
ca0311622e
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stklcode/stklcode-liveticker",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.0-alpha",
|
||||
"description": "A simple Liveticker for Wordpress.",
|
||||
"keywords": [
|
||||
"wordpress",
|
||||
|
73
includes/class-api.php
Normal file
73
includes/class-api.php
Normal 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;
|
||||
}
|
||||
}
|
@ -26,7 +26,7 @@ class SCLiveticker {
|
||||
*
|
||||
* @var string OPTIONS
|
||||
*/
|
||||
const VERSION = '1.1.1';
|
||||
const VERSION = '1.2.0';
|
||||
|
||||
/**
|
||||
* Options tag.
|
||||
@ -71,7 +71,10 @@ class SCLiveticker {
|
||||
// Load plugin 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 ) ) {
|
||||
return;
|
||||
}
|
||||
@ -310,7 +313,7 @@ class SCLiveticker {
|
||||
$is_widget = true;
|
||||
$slug = sanitize_text_field( $update_req['w'] );
|
||||
} else {
|
||||
// Should never occur, but for completenes' sake...
|
||||
// Should never occur, but for completeness' sake...
|
||||
break;
|
||||
}
|
||||
|
||||
|
4258
package-lock.json
generated
4258
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stklcode-liveticker",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.0-alpha",
|
||||
"description": "A simple Liveticker for Wordpress.",
|
||||
"author": "Stefan Kalscheuer",
|
||||
"license": "GPL-2.0+",
|
||||
|
@ -9,7 +9,7 @@
|
||||
* @wordpress-plugin
|
||||
* Plugin Name: Liveticker (by stklcode)
|
||||
* Description: A simple Liveticker for WordPress.
|
||||
* Version: 1.1.1
|
||||
* Version: 1.2.0-alpha
|
||||
* Author: Stefan Kalscheuer
|
||||
* Author URI: https://www.stklcode.de
|
||||
* Text Domain: stklcode-liveticker
|
||||
@ -72,6 +72,7 @@ function scliveticker_autoload( $class ) {
|
||||
$plugin_classes = array(
|
||||
'SCLiveticker\\SCLiveticker',
|
||||
'SCLiveticker\\Admin',
|
||||
'SCLiveticker\\Api',
|
||||
'SCLiveticker\\System',
|
||||
'SCLiveticker\\Widget',
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user