Index: wordpress-popular-posts.php =================================================================== --- wordpress-popular-posts.php (revision 724288) +++ wordpress-popular-posts.php (working copy) @@ -827,27 +827,23 @@ array_merge_recursive_distinct($this->defaults, $instance); - /*echo "
"; print_r($instance); echo "
";*/ - + function query_posts($instance) { + /** @var wpdb $wpdb */ global $wpdb; $table = $wpdb->prefix . "popularpostsdata"; $fields = ""; $from = ""; - $where = ""; $post_types = ""; $pids = ""; $cats = ""; $authors = ""; - $content = ""; // post filters // * post types - based on code seen at https://github.com/williamsba/WordPress-Popular-Posts-with-Custom-Post-Type-Support @@ -859,22 +855,22 @@ if ($len > 1) { // we are getting posts from more that one ctp foreach ( $types as $post_type ) { - $sql_post_types .= "'" .$post_type. "'"; + $sql_post_types .= "'" .$post_type. "'"; if ($i != $len - 1) $sql_post_types .= ","; $i++; - } + } $post_types = " p.post_type IN({$sql_post_types}) "; - } else if ($len == 1) { // post from one ctp only + } else if ($len == 1) { // post from one ctp only $post_types = " p.post_type = '".$instance['post_type']."' "; - + // if we're getting just pages, why join the categories table? if ( strtolower($instance['post_type']) == 'page' ) $join_cats = false; - + } // * posts exclusion @@ -895,7 +891,7 @@ $in = array(); $out = array(); $not_in = ""; - + usort($cat_ids, array(&$this, 'sorter')); for ($i=0; $i < count($cat_ids); $i++) { @@ -910,40 +906,40 @@ if ($in_cats != "" && $out_cats == "") { // get posts from from given cats only $cats = " AND p.ID IN ( SELECT object_id - FROM $wpdb->term_relationships AS r - JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id - JOIN $wpdb->terms AS t ON t.term_id = x.term_id - WHERE x.taxonomy = 'category' AND t.term_id IN($in_cats) + FROM {$wpdb->term_relationships} AS r + JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id + JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id + WHERE x.taxonomy = 'category' AND t.term_id IN({$in_cats}) ) "; } else if ($in_cats == "" && $out_cats != "") { // exclude posts from given cats only $cats = " AND p.ID NOT IN ( SELECT object_id - FROM $wpdb->term_relationships AS r - JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id - JOIN $wpdb->terms AS t ON t.term_id = x.term_id - WHERE x.taxonomy = 'category' AND t.term_id IN($out_cats) + FROM {$wpdb->term_relationships} AS r + JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id + JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id + WHERE x.taxonomy = 'category' AND t.term_id IN({$out_cats}) ) "; } else { // mixed, and possibly a heavy load on the DB $cats = " AND p.ID IN ( SELECT object_id - FROM $wpdb->term_relationships AS r - JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id - JOIN $wpdb->terms AS t ON t.term_id = x.term_id - WHERE x.taxonomy = 'category' AND t.term_id IN($in_cats) + FROM {$wpdb->term_relationships} AS r + JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id + JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id + WHERE x.taxonomy = 'category' AND t.term_id IN({$in_cats}) )) OR (p.post_type = 'post' AND p.ID NOT IN ( SELECT object_id - FROM $wpdb->term_relationships AS r - JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id - JOIN $wpdb->terms AS t ON t.term_id = x.term_id - WHERE x.taxonomy = 'category' AND t.term_id IN($out_cats) + FROM {$wpdb->term_relationships} AS r + JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id + JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id + WHERE x.taxonomy = 'category' AND t.term_id IN({$out_cats}) ) "; } } // * authors if ( !empty($instance['author']) ) { - $ath = explode(",", $instance['author']); - $len = count($ath); + $ath = explode(",", $instance['author']); + $len = count($ath); if ($len > 1) { // we are getting posts from more that one author $authors = " AND p.post_author IN(".$instance['author'].") "; @@ -1062,8 +1058,28 @@ //echo $query; $mostpopular = $wpdb->get_results($query); - /*echo "
"; print_r($mostpopular); echo "
";*/ + + return $mostpopular; + } + + /** + * Builds popular posts list + * Since 1.4.0 + */ + function get_popular_posts($instance, $return = false) { + // update instance's settings + $instance = $this->array_merge_recursive_distinct($this->defaults, $instance); + $content = ""; + + $mostpopular = $this->query_posts($instance); + /*echo "
"; print_r($mostpopular); echo "
";*/ + // If any theme or plugin want to apply custom markup and/or very special + // formatting let them creating a filter to build custom output. + if ($content = apply_filters('wpp_get_popular_posts_custom_display', $content, $mostpopular, $instance)) { + return $content; + } + if ( !is_array($mostpopular) || empty($mostpopular) ) { // no posts to show $content .= "

".__('Sorry. No data so far.', 'wordpress-popular-posts')."

"."\n"; } else { // list posts