/** * VIP Legacy Related Posts (get post_id, title, url) * * Don't use for new projects, just use WPCOM_RelatedPosts directly, since it has hooks * like jetpack_relatedposts_filter_args, jetpack_relatedposts_filter_filters * * For backwards compatability, this function finds related posts on the current blog * using Elasticsearch, then converts the results to match the original sphere results format. * * @param int $max_num Optional. Maximum number of results you want (default: 5). * @param array $additional_stopwords No longer used. * @param bool $exclude_own_titles No longer used. * @return array of related posts. */ function wpcom_vip_get_flaptor_related_posts($max_num = 5, $additional_stopwords = array(), $exclude_own_titles = true) { if (method_exists('Jetpack_RelatedPosts', 'init_raw')) { $post_id = get_the_ID(); $related = Jetpack_RelatedPosts::init_raw()->set_query_name('MLT-VIP-Flaptor')->get_for_post_id($post_id, array('size' => $max_num)); $host = parse_url(home_url(), PHP_URL_HOST); $source_info = array('sourcename' => get_bloginfo('name'), 'sourceurl' => home_url(), 'sourcetype' => 'same_domain'); if ($related) { //rebuilding the array to match sphere related posts (and flaptor related posts) $results = array(); foreach ($related as $result) { $results[] = array('post_id' => $result['id'], 'url' => get_permalink($result['id']), 'title' => get_the_title($result['id']), 'timestamp' => get_the_time('Y-m-d', $result['id']), 'host' => $host, 'source' => $source_info); } return $results; } return false; } else { // Fallback for local environments where flaptor isn't available $related_posts = array(); $host = parse_url(home_url(), PHP_URL_HOST); $source_info = array('sourcename' => get_bloginfo('name'), 'sourceurl' => home_url(), 'sourcetype' => 'same_domain'); $post_id = get_the_ID(); $related_query_args = array('posts_per_page' => $max_num); $categories = get_the_category($post_id); if (!empty($categories)) { $related_query_args['cat'] = $categories[0]->term_id; } $related_query = new WP_Query($related_query_args); foreach ($related_query->get_posts() as $related_post) { $related_post_id = $related_post->ID; $related_posts[] = array('url' => get_permalink($related_post_id), 'title' => get_the_title($related_post_id), 'timestamp' => get_the_time('Y-m-d', $related_post_id), 'host' => $host, 'source' => $source_info, 'post_id' => $related_post_id); } return $related_posts; } }
function wptouch_remove_jetpack_related() { if (class_exists('Jetpack_RelatedPosts')) { $jprp = Jetpack_RelatedPosts::init(); remove_filter('the_content', array($jprp, 'filter_add_target_to_dom'), 40); } }
/** * This action triggers if the module is in an active state, load related posts and options. * * @uses Jetpack_RelatedPosts::init, is_admin, Jetpack::enable_module_configurable, Jetpack::module_configuration_load, Jetpack_Sync::sync_posts * @return null */ public function action_on_load() { require_once 'related-posts/jetpack-related-posts.php'; Jetpack_RelatedPosts::init(); if (is_admin()) { // Enable "Configure" button on module card Jetpack::enable_module_configurable(__FILE__); Jetpack::module_configuration_load(__FILE__, array($this, 'module_configuration_load')); } }
function jetpackme_remove_rp() { $jprp = Jetpack_RelatedPosts::init(); $callback = array($jprp, 'filter_add_target_to_dom'); remove_filter('the_content', $callback, 40); }
function test_remove_related_posts_shortcode_from_filtered_content() { require_once JETPACK__PLUGIN_DIR . 'modules/related-posts.php'; require_once JETPACK__PLUGIN_DIR . 'modules/related-posts/jetpack-related-posts.php'; Jetpack_RelatedPosts::init()->action_frontend_init(); $this->post->post_content = '[jetpack-related-posts]'; wp_update_post($this->post); $this->assertContains('<!-- Jetpack Related Posts is not supported in this context. -->', apply_filters('the_content', $this->post->post_content)); $this->sender->do_sync(); $synced_post = $this->server_replica_storage->get_post($this->post->ID); $this->assertEquals("\n", $synced_post->post_content_filtered); }
?> </div> </div> <div class="sidebar3 xs-all s-all m-all l-all xl-all "> <div class="narrow"> <?php /*get_sidebar('horizontal'); */ ?> <h4>Related Posts</h4> <?php if (class_exists('Jetpack_RelatedPosts') && method_exists('Jetpack_RelatedPosts', 'init_raw')) { $orig_rel_posts = Jetpack_RelatedPosts::init_raw()->get_for_post_id(get_the_ID(), array('size' => 5)); //We need five related posts. /* This returns an array that looks like this: array( array ('id' => 12345), array ('id' => 54321), ) ....We need just the id's in the array. */ foreach ($orig_rel_posts as $key => $value) { $rel_posts[$key] = $value['id']; } ?> <div> <ul> <?php foreach ($rel_posts as $id => $rel_post_id) { ?>
function remove_related_posts_from_content() { if ($this->jetpackInstalled()) { $jprp = Jetpack_RelatedPosts::init(); remove_action('wp', array($jprp, 'action_frontend_init'), 10); remove_filter('the_content', array($jprp, 'filter_add_target_to_dom'), 40); } }
/** * @return array $related_posts An array of related WP_Post objects */ public function get_related_posts($post_id = null, $args = array()) { if (is_null($post_id)) { $post_id = get_the_ID(); } $defaults = array('posts_per_page' => $this->options['post-count'], 'post_type' => get_post_type($post_id), 'has_terms' => array(), 'date_range' => array('from' => strtotime('-1 year'), 'to' => time())); $args = wp_parse_args($args, $defaults); $args['date_range'] = apply_filters('wrp_date_range', $args['date_range'], $post_id); if (is_array($args['date_range'])) { $args['date_range'] = array_map('intval', $args['date_range']); } // To have a reasonable cache hitrate, we must ignore the date range, as it's comprised (by default) of // time() calls $cache_key_args = $args; unset($cache_key_args['date_range']); $cache_key = 'related_posts_' . $post_id . '_' . md5(serialize($cache_key_args)); $related_posts = wp_cache_get($cache_key, self::CACHE_GROUP); if (false !== $related_posts) { return $related_posts; } // Store the args so any 'posts_where' filters can access them $this->args = $args; $related_posts = array(); $this->_generation_method = null; if ($this->_use_related_api()) { // Use related posts API $response = Jetpack_RelatedPosts::init_raw()->get_for_post_id($post_id, array('size' => (int) $args['posts_per_page'], 'post_type' => $args['post_type'], 'has_terms' => $args['has_terms'], 'date_range' => $args['date_range'])); $related_posts = array(); foreach ($response as $hit) { $related_posts[] = get_post($hit['id']); } $this->_generation_method = 'MLT-VIP-Raw'; } else { $related_query_args = array('posts_per_page' => (int) $args['posts_per_page'], 'post__not_in' => array($post_id), 'post_type' => $args['post_type']); $categories = get_the_category($post_id); if (!empty($categories)) { $related_query_args['cat'] = $categories[0]->term_id; } if (!empty($args['has_terms'])) { $tax_query = array(); foreach ((array) $args['has_terms'] as $term) { $tax_query[] = array('taxonomy' => $term->taxonomy, 'field' => 'slug', 'terms' => $term->slug); } $related_query_args['tax_query'] = $tax_query; } add_filter('posts_where', array($this, 'filter_related_posts_where')); $related_query = new WP_Query($related_query_args); remove_filter('posts_where', array($this, 'filter_related_posts_where')); $related_posts = $related_query->get_posts(); $this->_generation_method = 'WP-Query'; } if ($this->_can_bump_stats()) { a8c_bump_stat('ES-Related-Post-Gen', $this->_generation_method); } // Clear out the $args, as they are only meaningful inside get_related_posts() $this->args = array(); wp_cache_set($cache_key, $related_posts, self::CACHE_GROUP, self::CACHE_LIFETIME); return $related_posts; }
/** * Remove the related posts from below the content area. * * @package lsx * @subpackage jetpack * @category portfolio */ function lsx_remove_single_portfolio_related_posts() { if (is_single() && 'jetpack-portfolio' == get_post_type() && class_exists('Jetpack_RelatedPosts')) { $jprp = Jetpack_RelatedPosts::init(); $callback = array($jprp, 'filter_add_target_to_dom'); remove_filter('the_content', $callback, 40); } }