Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decrease Largo_Related resource use #676

Merged
merged 9 commits into from Apr 29, 2015
106 changes: 61 additions & 45 deletions inc/related-content.php
Expand Up @@ -399,6 +399,7 @@ class Largo_Related {
var $number;
var $post_id;
var $post_ids = array();
var $post;

/**
* Constructor.
Expand All @@ -421,6 +422,8 @@ function __construct( $number = 1, $post_id = '' ) {
} else {
$this->post_id = get_the_ID();
}

$this->post = get_post($this->post_id);
}

/**
Expand Down Expand Up @@ -467,29 +470,31 @@ protected function get_series_posts() {

//loop thru all the series this post belongs to
foreach ( $series as $term ) {

//start to build our query of posts in this series
// get the posts in this series, ordered by rank or (if missing?) date
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, //should usually be enough
'taxonomy' => 'series',
'term' => $term->slug,
'orderby' => 'date',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'post_type' => 'post',
'posts_per_page' => $this->number,
'taxonomy' => 'series',
'term' => $term->slug,
'orderby' => 'date',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'date_query' => array(
'after' => $this->post->post_date,
),
);

// see if there's a post that has the sort order info for this series
$pq = new WP_Query( array(
$cftl_query = new WP_Query( array(
'post_type' => 'cftl-tax-landing',
'series' => $term->slug,
'posts_per_page' => 1
));

if ( $pq->have_posts() ) {
$pq->next_post();
$has_order = get_post_meta( $pq->post->ID, 'post_order', TRUE );
if ( $cftl_query->have_posts() ) {
$cftl_query->next_post();
$has_order = get_post_meta( $cftl_query->post->ID, 'post_order', TRUE );
if ( !empty($has_order) ) {
switch ( $has_order ) {
case 'ASC':
Expand All @@ -511,6 +516,9 @@ protected function get_series_posts() {

if ( $series_query->have_posts() ) {
$this->add_from_query( $series_query );
if ( $this->have_enough_posts() ) {
break;
}
}
}
}
Expand All @@ -533,20 +541,27 @@ protected function get_term_posts() {

foreach ( $taxonomies as $term ) {
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'orderby' => 'date',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'post_type' => 'post',
'posts_per_page' => $this->number,
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'orderby' => 'date',
'order' => 'ASC',
'ignore_sticky_posts' => 1,
'date_query' => array(
'after' => $this->post->post_date,
),
);
}
// run the query
$term_query = new WP_Query( $args );

if ( $term_query->have_posts() ) {
$this->add_from_query( $term_query );
// run the query
$term_query = new WP_Query( $args );

if ( $term_query->have_posts() ) {
$this->add_from_query( $term_query );
if ( $this->have_enough_posts() ) {
break;
}
}
}
}
}
Expand All @@ -560,17 +575,14 @@ protected function get_recent_posts() {

$args = array(
'post_type' => 'post',
'posts_per_page' => $this->number + 1,
'posts_per_page' => $this->number,
'post__not_in' => array( $this->post_id ),
);

$posts_query = new WP_Query( $args );

if ( $posts_query->have_posts() ) {
while ( $posts_query->have_posts() ) {
$posts_query->the_post();
if ( !in_array($posts_query->post->ID, $this->post_ids) ) $this->post_ids[] = $posts_query->post->ID;
}
$this->add_from_query($posts_query);
}
}

Expand All @@ -586,19 +598,19 @@ public function ids() {
//see if this post has manually set related posts
$post_ids = get_post_meta( $this->post_id, 'largo_custom_related_posts', true );
if ( ! empty( $post_ids ) ) {
$this->post_ids = explode( ",", $post_ids );
if ( count( $this->post_ids ) >= $this->number ) {
$this->post_ids = explode( ",", str_replace(' ', '', $post_ids) );
if ( $this->have_enough_posts() ) {
return $this->cleanup_ids();
}
}

$this->get_series_posts();
//are we done yet?
if ( count($this->post_ids) >= $this->number ) return $this->cleanup_ids();
if ( $this->have_enough_posts() ) return $this->cleanup_ids();

$this->get_term_posts();
//are we done yet?
if ( count($this->post_ids) >= $this->number ) return $this->cleanup_ids();
if ( $this->have_enough_posts() ) return $this->cleanup_ids();

$this->get_recent_posts();
return $this->cleanup_ids();
Expand All @@ -618,26 +630,30 @@ protected function add_from_query( $q, $reversed = FALSE ) {

while ( $q->have_posts() ) {
$q->the_post();
//don't show our post, but record that we've found it
if ( $q->post->ID == $this->post_id ) {
$found_ours = TRUE;
continue;
// don't add any posts until we're adding posts newer than the one being displayed
} else if ( ! $found_ours ) {
continue;
// add this post if it's new
} else if ( ! in_array( $q->post->ID, $this->post_ids ) ) { // only add it if it wasn't already there
$this->post_ids[] = $q->post->ID;
if ( ! in_array( $q->post->ID, $this->post_ids ) ) { // only add it if it wasn't already there
$this->post_ids[] = (int) trim($q->post->ID);
// stop if we have enough
if ( count( $this->post_ids ) >= $this->number ) return;
if ( $this->have_enough_posts() ) return;
}
}

//still here? reverse and try again
// still here? reverse and try again
// NOTE: we have no idea what this is used for (4/29/2015)
if ( ! $reversed ) {
$q->posts = array_reverse($q->posts);
$q->rewind_posts();
$this->add_from_query( $q, TRUE );
}
}
}

/**
* Counts to see if enough posts have been found
*/
protected function have_enough_posts() {
if ( count( $this->post_ids ) >= $this->number )
return true;

return false;
}
}
2 changes: 1 addition & 1 deletion inc/widgets/largo-author-bio.php
Expand Up @@ -23,7 +23,7 @@ function widget( $args, $instance ) {
if( get_post_meta( $post->ID, 'largo_byline_text' ) )
$byline_text = esc_attr( get_post_meta( $post->ID, 'largo_byline_text', true ) );

$is_series_landing = (function_exists('is_series_landing'))? is_series_landing() : false;
$is_series_landing = (function_exists('is_series_landing'))? is_series_landing($post->ID) : false;

if( (is_singular() || is_author() || $is_series_landing) && empty($byline_text) ):

Expand Down
133 changes: 133 additions & 0 deletions tests/inc/test-related-content.php
@@ -0,0 +1,133 @@
<?php

// Test functions in inc/related-content.php

class RelatedContentTestFunctions extends wp_UnitTestCase{
function setUp() {
parent::setUp();
}

function test_largo_get_related_topics_for_category() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test__tags_associated_with_category() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test__subcategories_for_category() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_get_post_related_topics() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_get_recent_posts_for_term() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_has_categories_or_tags() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_categories_and_tags() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_top_term() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_filter_get_post_related_topics() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

function test_largo_filter_get_recent_posts_for_term_query_args() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

}

class LargoRelatedTestFunctions extends WP_UnitTestCase {
function setUp() {
parent::setUp();
}

function test___construct() {
// check that Largo_Related->number equals 1 if number is not set
// check that Largo_Related->post_id is the value of the set post, or the value of the global post
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_popularity_sort() {
$this->markTestIncomplete('This test has not been implemented yet.');

}

/**
* Test the function with a lot of different conditions
*
* - Series without organization
* - Series with CFTL post with organization information
* - ASC
* - series_custom
* - DESC
* - featured, DESC
* - featured, ASC
* - No series, but category
* - No series, but tag
* - Tags and Categories
* - No series or category or tag
*/

function test_unorganized_series() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_series_asc() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_series_series_custom() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_series_desc() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_series_featured_desc() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_series_featured_asc() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_category() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_tags() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_category_and_tag() {
$this->markTestIncomplete('This test has not been implemented yet.');
}

function test_recent_posts() {
$this->markTestIncomplete('This test has not been implemented yet.');
}
}