Advertisement
tikaszvince

add_filter_wpp_get_popular_posts_custom_display.patch

Jun 8th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.10 KB | None | 0 0
  1. Index: wordpress-popular-posts.php
  2. ===================================================================
  3. --- wordpress-popular-posts.php (revision 724288)
  4. +++ wordpress-popular-posts.php (working copy)
  5. @@ -827,27 +827,23 @@
  6.              <?php
  7.             }
  8.         }
  9. -      
  10. +
  11.         /**
  12. -        * Builds popular posts list
  13. -        * Since 1.4.0
  14. +        * Build and run SQL query for popular posts
  15. +        *
  16. +        * @param array $instance widget instance
  17. +        * @return Array|NULL
  18.          */
  19. -       function get_popular_posts($instance, $return = false) {
  20. -                      
  21. -           // update instance's settings          
  22. -           $instance = $this->array_merge_recursive_distinct($this->defaults, $instance);         
  23. -           /*echo "<pre>"; print_r($instance); echo "</pre>";*/
  24. -          
  25. +       function query_posts($instance) {
  26. +           /** @var wpdb $wpdb */
  27.             global $wpdb;
  28.             $table = $wpdb->prefix . "popularpostsdata";
  29.             $fields = "";
  30.             $from = "";
  31. -           $where = "";
  32.             $post_types = "";
  33.             $pids = "";
  34.             $cats = "";
  35.             $authors = "";
  36. -           $content = "";
  37.            
  38.             // post filters        
  39.             // * post types - based on code seen at https://github.com/williamsba/WordPress-Popular-Posts-with-Custom-Post-Type-Support
  40. @@ -859,22 +855,22 @@
  41.                    
  42.             if ($len > 1) { // we are getting posts from more that one ctp             
  43.                 foreach ( $types as $post_type ) {
  44. -                   $sql_post_types .= "'" .$post_type. "'";
  45. +                   $sql_post_types .= "'" .$post_type. "'";
  46.                    
  47.                     if ($i != $len - 1) $sql_post_types .= ",";
  48.                    
  49.                     $i++;
  50. -               }
  51. +               }
  52.  
  53.                 $post_types = " p.post_type IN({$sql_post_types}) ";
  54. -           } else if ($len == 1) { // post from one ctp only
  55. +           } else if ($len == 1) { // post from one ctp only
  56.            
  57.                 $post_types = " p.post_type = '".$instance['post_type']."' ";
  58. -              
  59. +              
  60.                 // if we're getting just pages, why join the categories table?
  61.                 if ( strtolower($instance['post_type']) == 'page' )
  62.                     $join_cats = false;
  63. -              
  64. +              
  65.             }
  66.            
  67.             // * posts exclusion
  68. @@ -895,7 +891,7 @@
  69.                 $in = array();
  70.                 $out = array();
  71.                 $not_in = "";
  72. -              
  73. +              
  74.                 usort($cat_ids, array(&$this, 'sorter'));                  
  75.                
  76.                 for ($i=0; $i < count($cat_ids); $i++) {
  77. @@ -910,40 +906,40 @@
  78.                 if ($in_cats != "" && $out_cats == "") { // get posts from from given cats only
  79.                     $cats = " AND p.ID IN (
  80.                         SELECT object_id
  81. -                       FROM $wpdb->term_relationships AS r
  82. -                            JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  83. -                            JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  84. -                       WHERE x.taxonomy = 'category' AND t.term_id IN($in_cats)
  85. +                       FROM {$wpdb->term_relationships} AS r
  86. +                            JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  87. +                            JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id
  88. +                       WHERE x.taxonomy = 'category' AND t.term_id IN({$in_cats})
  89.                         ) ";
  90.                 } else if ($in_cats == "" && $out_cats != "") { // exclude posts from given cats only
  91.                     $cats = " AND p.ID NOT IN (
  92.                         SELECT object_id
  93. -                       FROM $wpdb->term_relationships AS r
  94. -                            JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  95. -                            JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  96. -                       WHERE x.taxonomy = 'category' AND t.term_id IN($out_cats)
  97. +                       FROM {$wpdb->term_relationships} AS r
  98. +                            JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  99. +                            JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id
  100. +                       WHERE x.taxonomy = 'category' AND t.term_id IN({$out_cats})
  101.                         ) ";
  102.                 } else { // mixed, and possibly a heavy load on the DB
  103.                     $cats = " AND p.ID IN (
  104.                         SELECT object_id
  105. -                       FROM $wpdb->term_relationships AS r
  106. -                            JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  107. -                            JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  108. -                       WHERE x.taxonomy = 'category' AND t.term_id IN($in_cats)
  109. +                       FROM {$wpdb->term_relationships} AS r
  110. +                            JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  111. +                            JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id
  112. +                       WHERE x.taxonomy = 'category' AND t.term_id IN({$in_cats})
  113.                         )) OR (p.post_type = 'post' AND p.ID NOT IN (
  114.                         SELECT object_id
  115. -                       FROM $wpdb->term_relationships AS r
  116. -                            JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  117. -                            JOIN $wpdb->terms AS t ON t.term_id = x.term_id
  118. -                       WHERE x.taxonomy = 'category' AND t.term_id IN($out_cats)
  119. +                       FROM {$wpdb->term_relationships} AS r
  120. +                            JOIN {$wpdb->term_taxonomy} AS x ON x.term_taxonomy_id = r.term_taxonomy_id
  121. +                            JOIN {$wpdb->terms} AS t ON t.term_id = x.term_id
  122. +                       WHERE x.taxonomy = 'category' AND t.term_id IN({$out_cats})
  123.                         ) ";
  124.                 }
  125.             }
  126.            
  127.             // * authors
  128.             if ( !empty($instance['author']) ) {           
  129. -               $ath = explode(",", $instance['author']);          
  130. -               $len = count($ath);
  131. +               $ath = explode(",", $instance['author']);          
  132. +               $len = count($ath);
  133.                
  134.                 if ($len > 1) { // we are getting posts from more that one author
  135.                     $authors = " AND p.post_author IN(".$instance['author'].") ";
  136. @@ -1062,8 +1058,28 @@
  137.             //echo $query;
  138.            
  139.             $mostpopular = $wpdb->get_results($query);         
  140. -           /*echo "<pre>"; print_r($mostpopular); echo "</pre>";*/
  141. +
  142. +           return $mostpopular;
  143. +       }
  144. +
  145. +       /**
  146. +        * Builds popular posts list
  147. +        * Since 1.4.0
  148. +        */
  149. +       function get_popular_posts($instance, $return = false) {
  150. +           // update instance's settings
  151. +           $instance = $this->array_merge_recursive_distinct($this->defaults, $instance);         
  152. +           $content = "";
  153. +
  154. +           $mostpopular = $this->query_posts($instance);
  155. +           /*echo "<pre>"; print_r($mostpopular); echo "</pre>";*/
  156.            
  157. +           // If any theme or plugin want to apply custom markup and/or very special
  158. +           // formatting let them creating a filter to build custom output.
  159. +           if ($content = apply_filters('wpp_get_popular_posts_custom_display', $content, $mostpopular, $instance)) {
  160. +               return $content;
  161. +           }
  162. +
  163.             if ( !is_array($mostpopular) || empty($mostpopular) ) { // no posts to show
  164.                 $content .= "<p>".__('Sorry. No data so far.', 'wordpress-popular-posts')."</p>"."\n";
  165.             } else { // list posts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement