Created
April 11, 2016 11:47
-
-
Save websupporter/a33e2455ff53ff0ffd81d7d48d583f35 to your computer and use it in GitHub Desktop.
An Ajax Request via WP Rest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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