Advertisement
Guest User

Untitled

a guest
Jul 6th, 2012
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.64 KB | None | 0 0
  1. <?php
  2.  
  3. $GLOBALS['relatedPostsViaTaxonomies_display'] = new relatedPostsViaTaxonomies_display();
  4.  
  5.  
  6. class relatedPostsViaTaxonomies_display
  7. {
  8.  
  9.     function core() {
  10.  
  11.         global $wpdb;
  12.        
  13.         $option_data = get_option("related_posts_via_taxonomies");
  14.         $title = stripslashes( "$option_data[related_posts_title]" );
  15.         $wrap_before = stripslashes( "$option_data[before_related_posts_title_and_related_posts]" );
  16.         $wrap_after = "$option_data[after_related_posts_title_and_related_posts]";
  17.         $before_title = stripslashes( "$option_data[before_related_posts_title]" );
  18.         $after_title = "$option_data[after_related_posts_title]";
  19.         $before_entries = stripslashes( "$option_data[before_related_posts]" );
  20.         $after_entries = "$option_data[after_related_posts]";
  21.         $before_entry = stripslashes( "$option_data[before_each_related_post]" );
  22.         $after_entry = "$option_data[after_each_related_post]";
  23.         $number_of_posts_to_show = "$option_data[maximum_number_of_related_posts]";
  24.         $order_by = "$option_data[order_by]";
  25.         $exclude_posts = "$option_data[exclude_posts]";
  26.         if ( $exclude_posts ) {
  27.             $exclude_posts .= ',';
  28.         }
  29.         $exclude_posts .= get_the_ID();
  30.         if ( !$option_data['exclude_categories'] ){ $option_data['exclude_categories'] = array(); }
  31.         $exclude_categories_str = implode( ",", $option_data['exclude_categories'] );
  32.         if ( !$option_data['exclude_tags'] ){ $option_data['exclude_tags'] = array(); }
  33.         $exclude_tags_str = implode( ",", $option_data['exclude_tags'] );
  34.         if ( !$option_data['post_types'] ){ $option_data['post_types'] = array(); }
  35.         $post_types_str = "'";
  36.         $post_types_str .= implode( "','", $option_data['post_types'] );
  37.         $post_types_str .= "'";
  38.         if ( !$number_of_posts_to_show ) { $number_of_posts_to_show = 10;}
  39.  
  40.         if ( $title ){
  41.             $title = "{$before_title}{$title}{$after_title}";
  42.         }
  43.  
  44.         $args = "SELECT term_taxonomy_id FROM {$wpdb->term_relationships} WHERE object_id = " . get_the_ID();
  45.  
  46.         if ( $exclude_categories_str ) {
  47.             $args .= " AND term_taxonomy_id NOT IN ( {$exclude_categories_str} )";
  48.         }
  49.  
  50.         if ( $exclude_tags_str ) {
  51.             $args .= " AND term_taxonomy_id NOT IN ( {$exclude_tags_str} )";
  52.         }
  53.  
  54.         $exclude_term_taxonomy_ids = $wpdb->get_col( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'link_category'" );
  55.         if ( $exclude_term_taxonomy_ids ) {
  56.             $exclude_term_taxonomy_ids_str = implode( ",", $exclude_term_taxonomy_ids );
  57.             $args .= " AND term_taxonomy_id NOT IN ( {$exclude_term_taxonomy_ids_str} )";
  58.         }
  59.  
  60.         $custom_taxonomies = get_taxonomies( array( '_builtin' => false ), 'names' );
  61.         if ( $custom_taxonomies ) {
  62.             foreach ( $custom_taxonomies as $custom_taxonomy ) {
  63.                 $option_data_key = "exclude_{$custom_taxonomy}";
  64.                 if ( $option_data[$option_data_key] ) {
  65.                     $exclude_custom_taxonomies_str[$option_data_key] = implode( ",", $option_data[$option_data_key] );
  66.                     $args .= " AND term_taxonomy_id NOT IN ( {$exclude_custom_taxonomies_str[$option_data_key]} )";
  67.                 }
  68.             }
  69.         }
  70.  
  71.         $term_taxonomy_ids = $wpdb->get_col( "$args" );
  72.         if ( !$term_taxonomy_ids ) { return; }
  73.         $term_taxonomy_ids_str = implode( ",", $term_taxonomy_ids );
  74.        
  75.         $object_ids = array();
  76.         $object_ids = $wpdb->get_col( "SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( {$term_taxonomy_ids_str} ) AND object_id NOT IN ( {$exclude_posts} )" );
  77.         if ( !$object_ids ) { return; }
  78.        
  79.         $object_ids = array_count_values( $object_ids );
  80.        
  81.         arsort( $object_ids );
  82.        
  83.  
  84.         $entries ='';
  85.         if ( $order_by == "related_scores_high__speedy" ) {
  86.             $count = 1;
  87.             foreach ( $object_ids as $object_id => $relevancy_score ) {
  88.                 $related_post = $wpdb->get_row( "SELECT ID FROM {$wpdb->posts} WHERE ID = {$object_id} AND post_type IN ( {$post_types_str} ) AND post_status = 'publish'" );
  89.                 if ( $related_post ) {
  90.                     $entries .= "{$before_entry}";
  91.                     $entries .= '<a href="' . get_permalink( $related_post->ID ) . '" title="' . get_the_title( $related_post->ID ) . '">' . get_the_title( $related_post->ID ) . '</a>';
  92.                     $entries .= " ({$relevancy_score})";
  93.                     $entries .= "{$after_entry}";
  94.                     if ( $count++ >= $number_of_posts_to_show ) {
  95.                         break;
  96.                     }
  97.                 }
  98.             }
  99.         } else {
  100.             $relevancy_scores = array();
  101.             $post_ids = array();
  102.             $post_date = array();
  103.             $post_modified = array();
  104.             foreach ( $object_ids as $object_id => $relevancy_score ) {
  105.                 $related_post = $wpdb->get_row( "SELECT ID, post_date, post_modified FROM {$wpdb->posts} WHERE ID = {$object_id} AND post_type IN ( {$post_types_str} ) AND post_status = 'publish'" );
  106.                 if ( $related_post ) {
  107.                     array_push( $relevancy_scores, $relevancy_score );
  108.                     array_push( $post_ids, $related_post->ID );
  109.                     array_push( $post_date, $related_post->post_date );
  110.                     array_push( $post_modified, $related_post->post_modified );
  111.                 }
  112.             }
  113.             if ( $post_ids ) { 
  114.                 if ( $order_by == "related_scores_high__date_published_old" ){
  115.                     array_multisort( $relevancy_scores, SORT_DESC, $post_date, SORT_ASC, $post_ids, SORT_ASC, $post_modified, SORT_ASC );
  116.                 } elseif ( $order_by == "related_scores_low__date_published_new" ) {
  117.                     array_multisort( $relevancy_scores, SORT_ASC, $post_date, SORT_DESC, $post_ids, SORT_DESC, $post_modified, SORT_DESC );
  118.                 } elseif ( $order_by == "related_scores_low__date_published_old" ) {
  119.                     array_multisort( $relevancy_scores, SORT_ASC, $post_date, SORT_ASC, $post_ids, SORT_ASC, $post_modified, SORT_ASC );
  120.                 } elseif ( $order_by == "related_scores_high__date_modified_new" ) {
  121.                     array_multisort( $relevancy_scores, SORT_DESC, $post_modified, SORT_DESC, $post_date, SORT_DESC, $post_ids, SORT_DESC );
  122.                 } elseif ( $order_by == "related_scores_high__date_modified_old" ) {
  123.                     array_multisort( $relevancy_scores, SORT_DESC, $post_modified, SORT_ASC, $post_date, SORT_ASC, $post_ids, SORT_ASC );
  124.                 } elseif ( $order_by == "related_scores_low__date_modified_new" ) {
  125.                     array_multisort( $relevancy_scores, SORT_ASC, $post_modified, SORT_DESC, $post_date, SORT_DESC, $post_ids, SORT_DESC );
  126.                 } elseif ( $order_by == "related_scores_low__date_modified_old" ) {
  127.                     array_multisort( $relevancy_scores, SORT_ASC, $post_modified, SORT_ASC, $post_date, SORT_ASC, $post_ids, SORT_ASC );
  128.                 } elseif ( $order_by == "date_published_new" ) {
  129.                     array_multisort( $post_date, SORT_DESC, $post_ids, SORT_DESC, $post_modified, SORT_DESC, $relevancy_scores, SORT_DESC );
  130.                 } elseif ( $order_by == "date_published_old" ) {
  131.                     array_multisort( $post_date, SORT_ASC, $post_ids, SORT_ASC, $post_modified, SORT_ASC, $relevancy_scores, SORT_DESC );
  132.                 } elseif ( $order_by == "date_modified_new" ) {
  133.                     array_multisort( $post_modified, SORT_DESC, $post_date, SORT_DESC, $post_ids, SORT_DESC, $relevancy_scores, SORT_DESC );
  134.                 } elseif ( $order_by == "date_modified_old" ) {
  135.                     array_multisort( $post_modified, SORT_ASC, $post_date, SORT_ASC, $post_ids, SORT_ASC, $relevancy_scores, SORT_DESC );
  136.                 } else {
  137.                     array_multisort( $relevancy_scores, SORT_DESC, $post_date, SORT_DESC, $post_ids, SORT_DESC, $post_modified, SORT_DESC );
  138.                 }
  139.                 $count = 1;
  140.                 foreach ( $post_ids as $key => $post_id ) {
  141.                     if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
  142.                         $the_thumbnail = get_the_post_thumbnail($post_id , 'medium'); }
  143.                     $entries .= "{$before_entry}";
  144.                     $entries .=  $the_thumbnail . '<a href="' . get_permalink( $post_id ) . '" title="' . get_the_title( $post_id ) . '">' . get_the_title( $post_id ) . '</a>';
  145.                     if ( is_user_logged_in() ) { $entries .= " ($relevancy_scores[$key])"; }
  146.                     $entries .= "{$after_entry}";
  147.                     if ( $count++ >= $number_of_posts_to_show ) {
  148.                         break;
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.         if ( !$entries ) { return; }
  154.         $output = $wrap_before.$title.$before_entries.$entries.$after_entries;
  155.         if ( $option_data['promotion_link'] ) {
  156.             $output .= '<p style="font-size: ' . $option_data['promotion_link_fontsize'] . 'px; text-align: ' . $option_data['promotion_link_textalign'] . ';">';
  157.             if ( $option_data['promotion_link_language'] == 'japanese' ) {
  158.                 $output .= '<a href="http://alphasis.info/kaihatu/wordpress-plugins/related-posts-via-taxonomies/" title="To the official site of Related Posts via Taxonomies." target="_blank">' . $option_data['promotion_link_text'] . '</a>';
  159.             } else {
  160.                 $output .= '<a href="http://alphasis.info/developments/wordpress-plugins/related-posts-via-taxonomies/" title="To the official site of Related Posts via Taxonomies." target="_blank">' . $option_data['promotion_link_text'] . '</a>';
  161.             }
  162.             $output .= '</p>';
  163.         }
  164.         $output .= $wrap_after;
  165.         return $output;
  166.  
  167.     } // function core()
  168.  
  169. } // class relatedPostsViaTaxonomies_display
  170.  
  171.  
  172.  
  173.  
  174.  
  175. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement