From 29f1f4dc568ad1f878159f7f2b67d9956f953ac2 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 20 Dec 2009 01:35:35 -0500 Subject: [PATCH 1/2] forgot to add new related posts code --- classes/ComicPressRelatedPosts.inc | 126 ++++++++++++++++++++++++++++ test/ComicPressRelatedPostsTest.php | 40 +++++++++ 2 files changed, 166 insertions(+) create mode 100644 classes/ComicPressRelatedPosts.inc create mode 100644 test/ComicPressRelatedPostsTest.php diff --git a/classes/ComicPressRelatedPosts.inc b/classes/ComicPressRelatedPosts.inc new file mode 100644 index 0000000..248d8b3 --- /dev/null +++ b/classes/ComicPressRelatedPosts.inc @@ -0,0 +1,126 @@ +_handle_shortcode( + extract(ComicPressRelatedPosts::_handle_shortcode_attrs($attrs)), + __('Related Comics', 'comicpress'), true + ); + } + + function shortcode_related_posts($attrs = '') { + $rp = new ComicPressRelatedPosts(); + return $rp->_handle_shortcode( + extract(ComicPressRelatedPosts::_handle_shortcode_attrs($attrs)), + __('Related Posts', 'comicpress'), false + ); + } + + function _handle_shortcode($attrs, $title, $is_in_storyline) { + global $post; + + if (is_object($post)) { + if (isset($post->ID)) { + $this->_setup_categories($is_in_storyline); + $tags = $this->_extract_tag_ids($post->ID); + + if (!empty($tags)) { + $posts = $this->_do_tags_query($post->ID, $tags, $attrs['limit']); + if (!empty($posts)) { + return $this->build_post_table($title, $posts); + } + } + } + } + + return ''; + } + + function _handle_shortcode_attrs($attrs) { + return shortcode_atts(array( + 'limit' => '5', + ), $atts); + } + + function _new_comicpressstoryline() { return new ComicPressStoryline(); } + + function _setup_categories($is_in_storyline = true) { + $storyline = new ComicPressStoryline(); + $storyline->read_from_options(); + $storyline_categories = $storyline->build_from_restrictions(); + + if ($is_in_storyline) { + $this->related_categories = $storyline_categories; + } else { + $this->related_categories = array_diff(get_all_category_ids(), $storyline_categories); + } + } + + function build_post_table($title, $posts) { + $output = array(); + if (!empty($posts)) { + $output[] = ''; + } + return implode('', $output); + } + + function _extract_tag_ids($post_id) { + $output = array(); + foreach (wp_get_post_tags($post_id) as $tag) { + $output[] = $tag->term_id; + } + return $output; + } + + // @codeCoverageIgnoreStart + function _do_tags_query($post_id, $tags = array(), $limit = 5) { + global $wpdb; + + if (!empty($tags)) { + if (empty($limit)) { + $limit = 5; + } + + // Do the query + $q = "SELECT p.*, count(tr.object_id) as count + FROM + $wpdb->term_taxonomy AS tt, + $wpdb->term_relationships AS tr, + $wpdb->posts AS p + WHERE + tt.taxonomy ='post_tag' AND + tt.term_taxonomy_id = tr.term_taxonomy_id AND + tr.object_id = p.ID AND + tt.term_id IN (%s) AND + p.ID != %d AND + p.post_status = 'publish' AND + p.post_date < NOW() + GROUP BY tr.object_id + ORDER BY count DESC, p.post_date DESC + LIMIT %d"; + + return $wpdb->get_results($wpdb->prepare($q, implode(',', $tags), $post_id, $limit)); + } + return false; + } + // @codeCoverageIgnoreEnd +} + +add_shortcode('related_posts', array('ComicPressRelatedPosts', 'shortcode_related_posts')); +add_shortcode('related_comics', array('ComicPressRelatedPosts', 'shortcode_related_comics')); + diff --git a/test/ComicPressRelatedPostsTest.php b/test/ComicPressRelatedPostsTest.php new file mode 100644 index 0000000..dab7f96 --- /dev/null +++ b/test/ComicPressRelatedPostsTest.php @@ -0,0 +1,40 @@ +rp = new ComicPressRelatedPosts(); + } + + function testBuildPostTable() { + $posts = array( + (object)array('ID' => 1, 'post_date' => '2009-01-01', 'post_title' => 'Post 1', 'guid' => 'post-1'), + (object)array('ID' => 2, 'post_date' => '2009-01-02', 'post_title' => 'Post 2', 'guid' => 'post-2'), + (object)array('ID' => 3, 'post_date' => '2009-01-03', 'post_title' => 'Post 3', 'guid' => 'post-3'), + ); + + foreach ($posts as $post) { + wp_insert_post($post); + } + + $categories = array( + 1 => array(1), + 2 => array(1), + 3 => array(2), + ); + + foreach ($categories as $id => $cats) { + wp_set_post_categories($id, $cats); + } + + $output = ''; + + $this->rp->related_categories = array(2); + + $this->assertEquals($output, $this->rp->build_post_table('Title', $posts)); + } +} From d5b1fd6b52eac0e85627b2c0852472641bde50fe Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 20 Dec 2009 01:38:38 -0500 Subject: [PATCH 2/2] remove old related posts code --- functions/relatedcomics.php | 3 ++- functions/relatedposts.php | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/functions/relatedcomics.php b/functions/relatedcomics.php index 3e6cb8c..09b7e13 100644 --- a/functions/relatedcomics.php +++ b/functions/relatedcomics.php @@ -6,7 +6,7 @@ * Usage: [related_comics] * */ - +/* function related_comics_shortcode( $atts = '' ) { extract(shortcode_atts(array( 'limit' => '5', @@ -69,3 +69,4 @@ function related_comics_shortcode( $atts = '' ) { add_shortcode('related_comics', 'related_comics_shortcode'); ?> +*/ diff --git a/functions/relatedposts.php b/functions/relatedposts.php index f37b629..89c136a 100644 --- a/functions/relatedposts.php +++ b/functions/relatedposts.php @@ -6,7 +6,7 @@ * Usage: [related_posts] * */ - +/* function related_posts_shortcode( $atts = '' ) { extract(shortcode_atts(array( @@ -72,4 +72,5 @@ function related_posts_shortcode( $atts = '' ) { add_shortcode('related_posts', 'related_posts_shortcode'); -?> \ No newline at end of file +?> +*/