Esempio n. 1
0
/**
 * Cached version of `get_posts`.
 *
 * @see https://developer.wordpress.org/reference/functions/get_posts/
 *
 * Think before using `get_posts` since it can be bad.
 *
 * @see https://10up.github.io/Engineering-Best-Practices/php/#performance
 * @see https://vip.wordpress.com/documentation/caching/uncached-functions/
 *
 * @param  array|null $args
 *
 * @return array
 */
function xu_get_posts(array $args = null)
{
    $args = is_array($args) ? $args : [];
    // Set suppress filters to false always.
    $args['suppress_filters'] = false;
    return xu_cache_get('get_posts', [$args], __FUNCTION__);
}
Esempio n. 2
0
/**
 * Cached version of `url_to_post_id`.
 *
 * @see https://developer.wordpress.org/reference/functions/url_to_post_id/
 *
 * @param  string $url
 *
 * @return int
 */
function xu_url_to_postid($url = '')
{
    // `url_to_postid` don't work before `init` action.
    if (!did_action('init')) {
        return 0;
    }
    // Only valid urls.
    if (parse_url($url, PHP_URL_HOST) !== parse_url(home_url(), PHP_URL_HOST)) {
        return 0;
    }
    return (int) xu_cache_get('url_to_postid', [$url], __FUNCTION__);
}
Esempio n. 3
0
/**
 * Cached version of `wp_nav_menu`.
 *
 * @see https://developer.wordpress.org/reference/functions/wp_nav_menu/
 *
 * @param  array $args
 *
 * @return mixed
 */
function xu_wp_nav_menu(array $args = [])
{
    // Store echo value for later use.
    $echo = isset($args['echo']) ? $args['echo'] : true;
    // Set echo argument to false so `wp_nav_menu` don't echo.
    $args['echo'] = false;
    // Get cached value.
    $value = xu_cache_get('wp_nav_menu', [$args], __FUNCTION__);
    // Don't print anything if false.
    if ($value === false) {
        return $value;
    }
    // Echo value if it should be echo.
    if ($echo) {
        echo $value;
        // WPCS: xss ok
    } else {
        return $value;
    }
}
Esempio n. 4
0
/**
 * Cached version of `wp_get_attachment_url`.
 *
 * @param  int $post_id
 *
 * @return string
 */
function xu_wp_get_attachment_url($post_id = 0)
{
    return xu_cache_get('wp_get_attachment_url', [$post_id], __FUNCTION__);
}