Skip to content

Instantly share code, notes, and snippets.

@avillegasn
Last active February 18, 2016 20:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save avillegasn/62cb8f43fdec373545f6 to your computer and use it in GitHub Desktop.
Save avillegasn/62cb8f43fdec373545f6 to your computer and use it in GitHub Desktop.
Código para extender la WP REST API
<?php // esta linea no la copies en tu functions.php
//***********************************
// Añadir atributo number_of_comments
//***********************************
// incluye el atributo en la respuesta JSON
function add_comment_count_to_posts( $_post ) {
$_post['number_of_comments'] = get_number_of_comments( $_post['ID'] );
return $_post;
}
// calcula el número de comentarios de una entrada
function get_number_of_comments( $id ) {
$comments_count = wp_count_comments( $id );
return absint( $comments_count->total_comments );
}
// hace el hook con la ruta /posts
add_filter( 'json_prepare_post', 'add_comment_count_to_posts' );
<?php // esta linea no la copies en tu functions.php
//***********************************
// Añadir atributo number_of_comments
//***********************************
function add_comment_count_to_posts() {
// definimos el schema del atributo nuevo
$schema = array(
'type' => 'integer',
'description' => 'number of comments',
'context' => array( 'view' ),
);
// registramos el nueo atributo
register_api_field( 'post', 'number_of_comments', array(
'schema' => $schema,
'get_callback' => 'get_number_of_comments', // indicamos qué función ha de llamar para poner valor al atributo
) );
}
// calcula el número de comentarios de una entrada
function get_number_of_comments( $post, $request ) {
$comments_count = wp_count_comments( $post->ID );
return absint( $comments_count->total_comments );
}
// hace el hook al iniciar la API REST
add_action( 'rest_api_init', 'add_comment_count_to_posts' );
<?php // esta linea no la copies en tu functions.php
// Vamos a crear la ruta /stats con info numérica adicional de tu WordPress
// Para usarla, visita la URL tuweb.com/wp-json/stats
// hacemos hook en el filtro json_endpoints
add_filter( 'json_endpoints', 'register_route_stats' );
// cogemos el array de rutas y creamos la nueva ruta
function register_route_stats( $routes ) {
$routes['/stats'] = array(
array( 'get_stats', WP_JSON_Server::READABLE ), // esto indica que la llamada es GET
);
return $routes;
}
// Aquí devolvemos el array con las estadísticas de /stats
function get_stats() {
$json_url = get_json_url() . '/stats/';
$json_stats = array();
$count_posts = absint( wp_count_posts()->publish );
$count_pages = absint( wp_count_posts('page')->publish );
$count_comments = wp_count_comments()->total_comments;
$count_users = count_users()['total_users'];
$count_tags = absint( wp_count_terms( 'post_tag' ) );
$count_categories = absint( wp_count_terms( 'category' ) );
$json_stats['posts'] = $count_posts;
$json_stats['pages'] = $count_pages;
$json_stats['comments'] = $count_comments;
$json_stats['users'] = $count_users;
$json_stats['tags'] = $count_tags;
$json_stats['categories'] = $count_categories;
$json_stats['meta']['links']['self'] = $json_url;
return $json_stats;
}
<?php // esta linea no la copies en tu functions.php
// Vamos a crear la ruta /stats con info numérica adicional de tu WordPress
// Para usarla, visita la URL tuweb.com/wp-json/wp/v2/stats
// hacemos el hook al iniciar la API REST
add_filter( 'rest_api_init', 'register_route_stats' );
// usamos register_rest_route para registrar la nueva ruta /stats
function register_route_stats() {
$api_namespace = 'wp/v2';
$stats_route = '/stats';
register_rest_route( $api_namespace, $stats_route, array(
array(
'methods' => WP_REST_Server::READABLE, // esto indica que la llamada es GET
'callback' => 'get_stats',
)
) );
}
// Aquí devolvemos el array con las estadísticas de /stats
function get_stats() {
$rest_url = get_rest_url() . $api_namespace . $stats_route;
$rest_stats = array();
$count_posts = absint( wp_count_posts()->publish );
$count_pages = absint( wp_count_posts('page')->publish );
$count_comments = wp_count_comments()->total_comments;
$count_users = count_users()['total_users'];
$count_tags = absint( wp_count_terms( 'post_tag' ) );
$count_categories = absint( wp_count_terms( 'category' ) );
$rest_stats['posts'] = $count_posts;
$rest_stats['pages'] = $count_pages;
$rest_stats['comments'] = $count_comments;
$rest_stats['users'] = $count_users;
$rest_stats['tags'] = $count_tags;
$rest_stats['categories'] = $count_categories;
$rest_stats['meta']['links']['self'] = $rest_url;
return $rest_stats;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment