Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Ajax Request via WP Rest
<?php
/**
* register the API route
*
* @since 0.9
**/
add_action( 'rest_api_init', 'dn_rest_routes' );
function dn_rest_routes() {
register_rest_route(
'buddypress-desktop-notification/v1', '/notifications/',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'dn_query',
'args' => array(
'since' => array(
'validate_callback' => 'dn_strtotime',
),
),
'permission_callback' => 'is_user_logged_in',
)
);
}
/**
* Validate 'since' parameter
* @since 0.9
*
* @param (string) $data the data string to validate
* @param (object) $rest the rest response object
* @param (string) $key the argument key
*
* @return (boolean) valide or invalid
**/
function dn_strtotime( $data, $rest, $key ) {
if ( 'since' === $key ) {
return strtotime( $data );
}
return false;
}
/**
* dn_query()
* Checks for new messages to send
*
* @since 0.6
* @param (array) $data The REST API data
* @return (array) $entries The REST return
**/
function dn_query( $data ) {
$entries = array();
$entries = dn_messages_query();
if ( count( $entries ) === 0 ) {
$entries = dn_notifications_query();
}
if ( count( $entries ) === 0 ) {
$entries = dn_activities_query( $data );
}
return apply_filters( 'dn_entries', $entries );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment