web/wp-content/plugins/related-posts-by-category/related_posts.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
     1 <?php
     1 <?php
     2 /*
     2 /*
     3 Plugin Name: Related Posts by Category
     3 Plugin Name: Related Posts by Category
       
     4 Text Domain: related_posts
       
     5 Domain Path: /lang
       
     6 Description: WordPress plugin for related posts ordered by current category. It's small. It's fast. Really!
       
     7 Author: Sergej M&uuml;ller
       
     8 Author URI: http://www.wpSEO.org
     4 Plugin URI: http://playground.ebiene.de/400/related-posts-by-category-the-wordpress-plugin-for-similar-posts/
     9 Plugin URI: http://playground.ebiene.de/400/related-posts-by-category-the-wordpress-plugin-for-similar-posts/
     5 Description: The WordPress Plugin for similar posts grouped by category. It's small. It's fast. Really.
    10 Version: 0.9.1
     6 Author: Sergej M&uuml;ller
       
     7 Version: 0.4
       
     8 Author URI: http://www.wpseo.org
       
     9 */
    11 */
    10 
    12 
    11 
    13 
    12 function related_posts_by_category($params, $post_id = 0) {
    14 if (!function_exists('is_admin')) {
       
    15 header('Status: 403 Forbidden');
       
    16 header('HTTP/1.1 403 Forbidden');
       
    17 exit();
       
    18 }
       
    19 class RPBC {
       
    20 function RPBC($params, $id = 0) {
       
    21 if (!is_array($params) || empty($params)) {
       
    22 return;
       
    23 }
    13 $entries = array();
    24 $entries = array();
    14 $output = '';
    25 $output = '';
    15 $limit = 0;
    26 $limit = 0;
    16 $type = '';
    27 $type = '';
    17 $order = '';
    28 $order = '';
    18 $orderby = '';
    29 $orderby = '';
    19 $post_id = intval($post_id);
    30 $exclude = '';
    20 if (!$post_id) {
    31 if (!intval($id)) {
    21 $post_id = $GLOBALS['post']->ID;
    32 $id = $GLOBALS['post']->ID;
    22 }
    33 }
    23 if (isset($params['limit'])) {
    34 if (isset($params['limit'])) {
    24 $limit = intval($params['limit']);
    35 $limit = intval($params['limit']);
    25 }
    36 }
    26 if (isset($params['type']) && !empty($params['type'])) {
    37 if (!empty($params['type'])) {
    27 $type = ($params['type'] == 'page' ? 'page' : 'post');
    38 $type = ($params['type'] == 'page' ? 'page' : 'post');
    28 }
    39 }
    29 if (isset($params['order']) && !empty($params['order'])) {
    40 if (!empty($params['order'])) {
    30 $order = (strtoupper($params['order']) == 'DESC' ? 'DESC' : 'ASC');
    41 $order = (strtoupper($params['order']) == 'DESC' ? 'DESC' : 'ASC');
    31 }
    42 }
    32 if (isset($params['orderby']) && !empty($params['orderby']) && preg_match('/^[a-zA-Z_]+$/', $params['orderby'])) {
    43 if (!empty($params['orderby']) && preg_match('/^[a-zA-Z_]+$/', $params['orderby'])) {
    33 $orderby = $params['orderby'];
    44 $orderby = $params['orderby'];
    34 }
    45 }
    35 $entries = $GLOBALS['wpdb']->get_results(
    46 if (!empty($params['exclude'])) {
       
    47 $exclude = preg_split('/, /', (string)$params['exclude'], -1, PREG_SPLIT_NO_EMPTY);
       
    48 }
       
    49 $posts = $GLOBALS['wpdb']->get_results(
    36 sprintf(
    50 sprintf(
    37 "SELECT DISTINCT object_id, post_title FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t, {$GLOBALS['wpdb']->posts} p WHERE t.term_id IN (SELECT t.term_id FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t WHERE r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'category' AND r.object_id = $post_id) AND r.term_taxonomy_id = t.term_taxonomy_id AND p.post_status = 'publish' AND p.ID = r.object_id AND object_id <> $post_id %s %s %s",
    51 "SELECT DISTINCT object_id as ID, post_title FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t, {$GLOBALS['wpdb']->posts} p WHERE t.term_id IN (SELECT t.term_id FROM {$GLOBALS['wpdb']->term_relationships} r, {$GLOBALS['wpdb']->term_taxonomy} t WHERE r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'category' AND r.object_id = $id %s) AND r.term_taxonomy_id = t.term_taxonomy_id AND p.post_status = 'publish' AND p.ID = r.object_id AND object_id <> $id %s %s %s",
       
    52 ($exclude ? ('AND t.term_taxonomy_id NOT IN(' .implode(',', (array)$exclude). ')') : ''),
    38 ($type ? ("AND p.post_type = '" .$type. "'") : ''),
    53 ($type ? ("AND p.post_type = '" .$type. "'") : ''),
    39 ($orderby ? ('ORDER BY ' .(strtoupper($params['orderby']) == 'RAND' ? 'RAND()' : $orderby. ' ' .$order)) : ''),
    54 ($orderby ? ('ORDER BY ' .(strtoupper($params['orderby']) == 'RAND' ? 'RAND()' : $orderby. ' ' .$order)) : ''),
    40 ($limit ? ('LIMIT ' .$limit) : '')
    55 ($limit ? ('LIMIT ' .$limit) : '')
    41 ),
    56 ),
    42 OBJECT
    57 OBJECT
    43 );
    58 );
    44 if ($entries) {
    59 if ($posts) {
    45 foreach ($entries as $entry) {
    60 foreach ($posts as $post) {
       
    61 $title = esc_attr($post->post_title);
       
    62 $rel = (!empty($params['rel']) ? (' rel="' .esc_attr($params['rel']). '"') : '');
       
    63 $hidden = (isset($params['hidden']) && $params['hidden'] == 'title' ? '' : $title);
       
    64 $inside = (isset($params['inside']) ? $params['inside'] : '');
       
    65 $outside = (isset($params['outside']) ? $params['outside'] : '');
       
    66 $before = (isset($params['before']) ? $params['before'] : '');
       
    67 $after = (isset($params['after']) ? $params['after'] : '');
       
    68 $image = '';
       
    69 if (!empty($params['image']) && !(isset($params['hidden']) && $params['hidden'] == 'image')) {
       
    70 $thumb = $this->get_image($post->ID, $params['image']);
       
    71 if (empty($thumb)) {
       
    72 if (!empty($params['default'])) {
       
    73 $image = sprintf(
       
    74 '<img src="%s" class="rpbc_default" alt="%2$s" title="%2$s" />',
       
    75 esc_url($params['default']),
       
    76 $title
       
    77 );
       
    78 }
       
    79 } else {
       
    80 $image = preg_replace(
       
    81 '#alt=([\'"]).*?([\'"]) title=([\'"]).*?([\'"])#si',
       
    82 'alt=${1}' .$title. '${2} title=${3}' .$title. '${4}',
       
    83 $thumb
       
    84 );
       
    85 }
       
    86 }
    46 $output .= sprintf(
    87 $output .= sprintf(
    47 '%s<a href="%s" %s title="%s">%s%s%s</a>%s',
    88 '%s<a href="%s" title="%s"%s>%s%s%s%s</a>%s',
    48 isset($params['before']) ? $params['before'] : '',
    89 $before,
    49 get_permalink($entry->object_id),
    90 esc_url(get_permalink($post->ID)),
    50 (isset($params['rel']) ? ('rel="' .$params['rel']. '"') : ''),
    91 $title,
    51 str_replace('"', '&quot;', $entry->post_title),
    92 $rel,
    52 isset($params['inside']) ? $params['inside'] : '',
    93 $image,
    53 $entry->post_title,
    94 $inside,
    54 isset($params['outside']) ? $params['outside'] : '',
    95 $hidden,
    55 isset($params['after']) ? $params['after'] : ''
    96 $outside,
       
    97 $after
    56 );
    98 );
    57 }
    99 }
    58 } else {
   100 } else {
    59 $output = $params['message'];
   101 $output = $params['message'];
    60 }
   102 }
    61 if (isset($params['echo']) === true && $params['echo']) {
   103 if (!empty($params['echo'])) {
    62 echo $output;
   104 echo $output;
    63 } else {
   105 } else {
    64 return $output;
   106 $this->output = $output;
    65 }
   107 }
    66 }
   108 }
    67 ?>
   109 function get_image($id, $type = 'thumbnail') {
       
   110 if (!intval($id)) {
       
   111 return;
       
   112 }
       
   113 if (function_exists('get_the_post_thumbnail') && $image = get_the_post_thumbnail($id, $type)) {
       
   114 return $image;
       
   115 }
       
   116 if (function_exists('wp_get_attachment_image')) {
       
   117 $children = get_children(
       
   118 array(
       
   119 'post_parent'=> $id,
       
   120 'post_type'=> 'attachment',
       
   121 'numberposts'=> 1,
       
   122 'post_status'=> 'inherit',
       
   123 'post_mime_type' => 'image',
       
   124 'order'=> 'ASC',
       
   125 'orderby'=> 'menu_order ASC'
       
   126 )
       
   127 );
       
   128 if (!empty($children)) {
       
   129 foreach ($children as $key => $value) {
       
   130 return wp_get_attachment_image($key, $type);
       
   131 }
       
   132 }
       
   133 }
       
   134 }
       
   135 }
       
   136 if (!function_exists('related_posts_by_category')) {
       
   137 function related_posts_by_category($params, $id = 0) {
       
   138 $GLOBALS['RPBC'] = new RPBC($params, $id);
       
   139 }
       
   140 }
       
   141 if (function_exists('current_theme_supports') && !current_theme_supports('post-thumbnails')) {
       
   142 add_theme_support('post-thumbnails');
       
   143 }
       
   144 add_action(
       
   145 'related_posts_by_category',
       
   146 'related_posts_by_category',
       
   147 10,
       
   148 2
       
   149 );