Skip to content

Instantly share code, notes, and snippets.

View milindmore22's full-sized avatar
🏠
Working from home

Milind More milindmore22

🏠
Working from home
View GitHub Profile
@milindmore22
milindmore22 / cached_count_user_posts.php
Last active January 27, 2018 11:54
cached_count_user_posts
<?php
/**
* Cached version of count_user_posts, which is uncached but doesn't always need to hit the db
*
* Count_user_posts is generally fast, but it can be easy to end up with many redundant queries.
* if it's called several times per request. This allows bypassing the db queries in favor of
* the cache
*
* @param int $user_id user id to get count.
@milindmore22
milindmore22 / cached_get_item_permissions_check.php
Last active January 27, 2018 11:42
cached_get_item_permissions_check
<?php
function cached_get_item_permissions_check( $request ) {
$error = new WP_Error( 'rest_user_invalid_id', __( 'Invalid user ID.' ), array( 'status' => 404 ) );
if ( (int) $request['id'] <= 0 ) {
return $error;
}
@milindmore22
milindmore22 / default-registered-route.php
Last active January 27, 2018 12:34
WP default registered user end point
<?php
// https://core.trac.wordpress.org/browser/tags/4.9/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php#L64
register_rest_route(
$this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),