Merge branch 'master' of github.com:johnbintz/comicpress-2.8
This commit is contained in:
commit
12ffca740a
|
@ -0,0 +1,126 @@
|
|||
<?php
|
||||
|
||||
require_once('ComicPressStoryline.inc');
|
||||
|
||||
class ComicPressRelatedPosts {
|
||||
var $related_categories;
|
||||
|
||||
function shortcode_related_comics($attrs = '') {
|
||||
$rp = new ComicPressRelatedPosts();
|
||||
return $rp->_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[] = '<div class="related_posts">';
|
||||
$output[] = '<span>' . $title . '</span>';
|
||||
$output[] = '<table class="month-table">';
|
||||
foreach ($posts as $post) {
|
||||
if (array_intersect($this->related_categories, wp_get_post_categories($post->ID))) {
|
||||
$output[] = '<tr><td class="archive-date" align="right">' . date('M j, Y', strtotime($post->post_date)) . '</td>';
|
||||
$output[] = '<td class="archive-title"><a title="' . esc_attr(get_the_title($post)) . '" href="' . esc_attr(get_permalink($post)) . '">' . esc_html(get_the_title($post)) . '</a></td></tr>';
|
||||
}
|
||||
}
|
||||
$output[] = '</table>';
|
||||
$output[] = '</div>';
|
||||
}
|
||||
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'));
|
||||
|
|
@ -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');
|
||||
|
||||
?>
|
||||
*/
|
||||
|
|
|
@ -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');
|
||||
|
||||
?>
|
||||
?>
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
require_once('PHPUnit/Framework.php');
|
||||
require_once('MockPress/mockpress.php');
|
||||
require_once(dirname(__FILE__) . '/../classes/ComicPressRelatedPosts.inc');
|
||||
|
||||
class ComicPressRelatedPostsTest extends PHPUnit_Framework_TestCase {
|
||||
function setUp() {
|
||||
_reset_wp();
|
||||
$this->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 = '<div class="related_posts"><span>Title</span><table class="month-table"><tr><td class="archive-date" align="right">Jan 3, 2009</td><td class="archive-title"><a title="Post 3" href="post-3">Post 3</a></td></tr></table></div>';
|
||||
|
||||
$this->rp->related_categories = array(2);
|
||||
|
||||
$this->assertEquals($output, $this->rp->build_post_table('Title', $posts));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue