Jetpack_RelatedPosts::_get_related_post_ids PHP Метод

_get_related_post_ids() защищенный Метод

Get array of related posts matched by ElasticSearch.
protected _get_related_post_ids ( integer $post_id, integer $size, array $filters ) : array
$post_id integer
$size integer
$filters array
Результат array
    protected function _get_related_post_ids($post_id, $size, array $filters)
    {
        $now_ts = time();
        $cache_meta_key = '_jetpack_related_posts_cache';
        $body = array('size' => (int) $size);
        if (!empty($filters)) {
            $body['filter'] = array('and' => $filters);
        }
        // Build cache key
        $cache_key = md5(serialize($body));
        // Load all cached values
        if (wp_using_ext_object_cache()) {
            $transient_name = "{$cache_meta_key}_{$cache_key}_{$post_id}";
            $cache = get_transient($transient_name);
            if (false !== $cache) {
                return $cache;
            }
        } else {
            $cache = get_post_meta($post_id, $cache_meta_key, true);
            if (empty($cache)) {
                $cache = array();
            }
            // Cache is valid! Return cached value.
            if (isset($cache[$cache_key]) && is_array($cache[$cache_key]) && $cache[$cache_key]['expires'] > $now_ts) {
                return $cache[$cache_key]['payload'];
            }
        }
        $response = wp_remote_post("https://public-api.wordpress.com/rest/v1/sites/{$this->_blog_id_wpcom}/posts/{$post_id}/related/", array('timeout' => 10, 'user-agent' => 'jetpack_related_posts', 'sslverify' => true, 'body' => $body));
        // Oh no... return nothing don't cache errors.
        if (is_wp_error($response)) {
            if (isset($cache[$cache_key]) && is_array($cache[$cache_key])) {
                return $cache[$cache_key]['payload'];
            } else {
                return array();
            }
        }
        $results = json_decode(wp_remote_retrieve_body($response), true);
        $related_posts = array();
        if (is_array($results) && !empty($results['hits'])) {
            foreach ($results['hits'] as $hit) {
                $related_posts[] = array('id' => $hit['fields']['post_id']);
            }
        }
        // An empty array might indicate no related posts or that posts
        // are not yet synced to WordPress.com, so we cache for only 1
        // minute in this case
        if (empty($related_posts)) {
            $cache_ttl = 60;
        } else {
            $cache_ttl = 12 * HOUR_IN_SECONDS;
        }
        // Update cache
        if (wp_using_ext_object_cache()) {
            set_transient($transient_name, $related_posts, $cache_ttl);
        } else {
            // Copy all valid cache values
            $new_cache = array();
            foreach ($cache as $k => $v) {
                if (is_array($v) && $v['expires'] > $now_ts) {
                    $new_cache[$k] = $v;
                }
            }
            // Set new cache value
            $cache_expires = $cache_ttl + $now_ts;
            $new_cache[$cache_key] = array('expires' => $cache_expires, 'payload' => $related_posts);
            update_post_meta($post_id, $cache_meta_key, $new_cache);
        }
        return $related_posts;
    }