/** * Returns the title of each list item. * * @since 2.2.0 * * @param array $args Array of arguments * @param object $result Object of the current post result * @return string Space separated list of link attributes */ function crp_title($args, $result) { $title = crp_max_formatted_content(get_the_title($result->ID), $args['title_length']); // Get the post title and crop it if needed /** * Filter the title of each list item. * * @since 1.9 * * @param string $title Title of the post. * @param object $result Object of the current post result * @param array $args Array of arguments */ return apply_filters('crp_title', $title, $result, $args); }
/** * Main function to generate the related posts output * * @since 1.0.1 * * @param array $args Parameters in a query string format * @return string HTML formatted list of related posts */ function ald_crp($args = array()) { global $wpdb, $post, $single, $crp_settings; $defaults = array('is_widget' => FALSE, 'echo' => TRUE); $defaults = array_merge($defaults, $crp_settings); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); // Declare each item in $args as its own variable i.e. $type, $before. extract($args, EXTR_SKIP); //Support caching to speed up retrieval if (!empty($cache)) { $output = $is_widget ? get_post_meta($post->ID, 'crp_related_posts_widget', true) : get_post_meta($post->ID, 'crp_related_posts', true); if ($output) { return $output; } } $exclude_categories = explode(',', $exclude_categories); $rel_attribute = $link_nofollow ? ' rel="nofollow" ' : ' '; $target_attribute = $link_new_window ? ' target="_blank" ' : ' '; // Retrieve the list of posts $results = get_crp_posts_id(array_merge($args, array('postid' => $post->ID, 'strict_limit' => TRUE))); $output = is_singular() ? '<div id="crp_related" class="crp_related' . ($is_widget ? '_widget' : '') . '">' : '<div class="crp_related' . ($is_widget ? '_widget' : '') . '">'; if ($results) { $loop_counter = 0; if (!$is_widget) { $title = str_replace("%postname%", $post->post_title, $title); // Replace %postname% with the title of the current post /** * Filter the title of the Related Posts list * * @since 1.9 * * @param string $title Title/heading of the Related Posts list */ $output .= apply_filters('crp_heading_title', $title); } /** * Filter the opening tag of the related posts list * * @since 1.9 * * @param string $before_list Opening tag set in the Settings Page */ $output .= apply_filters('crp_before_list', $before_list); foreach ($results as $result) { /** * Filter the post ID for each result. Allows a custom function to hook in and change the ID if needed. * * @since 1.9 * * @param int $result->ID ID of the post */ $resultid = apply_filters('crp_post_id', $result->ID); $result = get_post($resultid); // Let's get the Post using the ID /** * Filter the post ID for each result. This filtered ID is passed as a parameter to fetch categories. * * This is useful since you might want to fetch a different set of categories for a linked post ID, * typically in the case of plugins that let you set mutiple languages * * @since 1.9 * * @param int $result->ID ID of the post */ $resultid = apply_filters('crp_post_cat_id', $result->ID); $categorys = get_the_category($resultid); //Fetch categories of the plugin $p_in_c = false; // Variable to check if post exists in a particular category foreach ($categorys as $cat) { // Loop to check if post exists in excluded category $p_in_c = in_array($cat->cat_ID, $exclude_categories) ? true : false; if ($p_in_c) { break; } // End loop if post found in category } if (!$p_in_c) { /** * Filter the opening tag of each list item. * * @since 1.9 * * @param string $before_list_item Tag before each list item. Can be defined in the Settings page. * @param object $result Object of the current post result */ $output .= apply_filters('crp_before_list_item', $before_list_item, $result); // Pass the post object to the filter $title = crp_max_formatted_content(get_the_title($result->ID), $title_length); // Get the post title and crop it if needed /** * Filter the title of each list item. * * @since 1.9 * * @param string $title Title of the post. * @param object $result Object of the current post result */ $title = apply_filters('crp_title', $title, $result); if ('after' == $post_thumb_op) { $output .= '<a href="' . get_permalink($result->ID) . '" ' . $rel_attribute . ' ' . $target_attribute . 'class="crp_title">' . $title . '</a>'; // Add title if post thumbnail is to be displayed after } if ('inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op) { $output .= '<a href="' . get_permalink($result->ID) . '" ' . $rel_attribute . ' ' . $target_attribute . '>'; $output .= crp_get_the_post_thumbnail(array('postid' => $result->ID, 'thumb_height' => $thumb_height, 'thumb_width' => $thumb_width, 'thumb_meta' => $thumb_meta, 'thumb_html' => $thumb_html, 'thumb_default' => $thumb_default, 'thumb_default_show' => $thumb_default_show, 'thumb_timthumb' => $thumb_timthumb, 'thumb_timthumb_q' => $thumb_timthumb_q, 'scan_images' => $scan_images, 'class' => 'crp_thumb', 'filter' => 'crp_postimage')); $output .= '</a>'; } if ('inline' == $post_thumb_op || 'text_only' == $post_thumb_op) { $output .= '<a href="' . get_permalink($result->ID) . '" ' . $rel_attribute . ' ' . $target_attribute . ' class="crp_title">' . $title . '</a>'; // Add title when required by settings } if ($show_author) { $author_info = get_userdata($result->post_author); $author_link = get_author_posts_url($author_info->ID); $author_name = ucwords(trim(stripslashes($author_info->display_name))); /** * Filter the author name. * * @since 1.9.1 * * @param string $author_name Proper name of the post author. * @param object $author_info WP_User object of the post author */ $author_name = apply_filters('crp_author_name', $author_name, $author_info); $crp_author .= '<span class="crp_author"> ' . __(' by ', CRP_LOCAL_NAME) . '<a href="' . $author_link . '">' . $author_name . '</a></span> '; /** * Filter the text with the author details. * * @since 2.0.0 * * @param string $crp_author Formatted string with author details and link * @param object $author_info WP_User object of the post author */ $crp_author = apply_filters('crp_author', $crp_author, $author_info); $output .= $crp_author; } if ($show_date) { $output .= '<span class="crp_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> '; } if ($show_excerpt) { $output .= '<span class="crp_excerpt"> ' . crp_excerpt($result->ID, $excerpt_length) . '</span>'; } $loop_counter++; /** * Filter the closing tag of each list item. * * @since 1.9 * * @param string $after_list_item Tag after each list item. Can be defined in the Settings page. * @param object $result Object of the current post result */ $output .= apply_filters('crp_after_list_item', $after_list_item, $result); } if ($loop_counter == $limit) { break; } // End loop when related posts limit is reached } //end of foreach loop if ($show_credit) { /** This filter is documented in contextual-related-posts.php */ $output .= apply_filters('crp_before_list_item', $before_list_item, $result); // Pass the post object to the filter $output .= sprintf(__('Powered by <a href="%s" rel="nofollow">Contextual Related Posts</a>', CRP_LOCAL_NAME), esc_url('http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/')); /** This filter is documented in contextual-related-posts.php */ $output .= apply_filters('crp_after_list_item', $after_list_item, $result); } /** * Filter the closing tag of the related posts list * * @since 1.9 * * @param string $after_list Closing tag set in the Settings Page */ $output .= apply_filters('crp_after_list', $after_list); $clearfix = '<div style="clear:both"></div>'; /** * Filter the clearfix div tag. This is included after the closing tag to clear any miscellaneous floating elements; * * @since 2.0.0 * * @param string $clearfix Contains: <div style="clear:both"></div> */ $output .= apply_filters('crp_clearfix', $clearfix); } else { $output .= $blank_output ? ' ' : '<p>' . $blank_output_text . '</p>'; } if (false === strpos($output, $before_list_item)) { $output = '<div id="crp_related">'; $output .= $blank_output ? ' ' : '<p>' . $blank_output_text . '</p>'; } $output .= '</div>'; // closing div of 'crp_related' //Support caching to speed up retrieval if (!empty($cache)) { if ($is_widget) { update_post_meta($post->ID, 'crp_related_posts_widget', $output, ''); } else { update_post_meta($post->ID, 'crp_related_posts', $output, ''); } } /** * Filter the output * * @since 1.9.1 * * @param string $output Formatted list of related posts * @param array $args Complete set of arguments */ return apply_filters('ald_crp', $output, $args); }
function get_crp($is_widget, $limit, $show_excerpt, $post_thumb_op, $thumb_height, $thumb_width) { global $wpdb, $post, $single; $crp_settings = crp_read_options(); if (empty($limit)) $limit = stripslashes($crp_settings['limit']); if (empty($post_thumb_op)) $post_thumb_op = stripslashes($crp_settings['post_thumb_op']); if (!isset($show_excerpt)) $show_excerpt = $crp_settings['show_excerpt']; if (empty($thumb_height)) $thumb_height = stripslashes($crp_settings['thumb_height']); if (empty($thumb_width)) $thumb_width = stripslashes($crp_settings['thumb_width']); parse_str($crp_settings['post_types'],$post_types); $exclude_categories = explode(',',$crp_settings['exclude_categories']); $rel_attribute = (($crp_settings['link_nofollow']) ? ' rel="nofollow" ' : ' ' ); $target_attribute = (($crp_settings['link_nofollow']) ? ' target="_blank" ' : ' ' ); // Make sure the post is not from the future $time_difference = get_option('gmt_offset'); $now = gmdate("Y-m-d H:i:s",(time()+($time_difference*3600))); // Are we matching only the title or the post content as well? if($crp_settings['match_content']) { $stuff = addslashes($post->post_title. ' ' . $post->post_content); } else { $stuff = addslashes($post->post_title); } // Limit the related posts by time $daily_range = $crp_settings['daily_range'] - 1; $current_date = strtotime ( '-'.$daily_range. ' DAY' , strtotime ( $now ) ); $current_date = date ( 'Y-m-d H:i:s' , $current_date ); // Create the SQL query to fetch the related posts from the database if ((is_int($post->ID))&&($stuff != '')) { $sql = "SELECT DISTINCT ID,post_title,post_date " . " FROM ".$wpdb->posts." WHERE " . "MATCH (post_title,post_content) AGAINST ('".$stuff."') " . "AND post_date <= '".$now."' " . "AND post_date >= '".$current_date."' " . "AND post_status = 'publish' " . "AND ID != ".$post->ID." "; if ($crp_settings['exclude_post_ids']!='') $sql .= "AND ID NOT IN (".$crp_settings['exclude_post_ids'].") "; $sql .= "AND ( "; $multiple = false; foreach ($post_types as $post_type) { if ( $multiple ) $sql .= ' OR '; $sql .= " post_type = '".$post_type."' "; $multiple = true; } $sql .=" ) "; $sql .= "LIMIT ".$limit*3; $search_counter = 0; $searches = $wpdb->get_results($sql); } else { $searches = false; } $output = (is_singular()) ? '<div id="crp_related" class="crp_related">' : '<div class="crp_related">'; if($searches){ if(!$is_widget) $output .= (stripslashes($crp_settings['title'])); $output .= $crp_settings['before_list']; foreach($searches as $search) { $categorys = get_the_category($search->ID); //Fetch categories of the plugin $p_in_c = false; // Variable to check if post exists in a particular category $title = crp_max_formatted_content(get_the_title($search->ID),$crp_settings['title_length']); foreach ($categorys as $cat) { // Loop to check if post exists in excluded category $p_in_c = (in_array($cat->cat_ID, $exclude_categories)) ? true : false; if ($p_in_c) break; // End loop if post found in category } if (!$p_in_c) { $output .= $crp_settings['before_list_item']; //$output .= '<a href="'.get_permalink($search->ID).'" class="crp_link">'; // Add beginning of link if ($post_thumb_op=='after') { $output .= '<a href="'.get_permalink($search->ID).'" '.$rel_attribute.' '.$target_attribute.'class="crp_title">'.$title.'</a>'; // Add title if post thumbnail is to be displayed after } if ($post_thumb_op=='inline' || $post_thumb_op=='after' || $post_thumb_op=='thumbs_only') { $output .= '<a href="'.get_permalink($search->ID).'" '.$rel_attribute.' '.$target_attribute.'>'; $output .= crp_get_the_post_thumbnail('postid='.$search->ID.'&thumb_height='.$thumb_height.'&thumb_width='.$thumb_width.'&thumb_meta='.$crp_settings['thumb_meta'].'&thumb_default='.$crp_settings['thumb_default'].'&thumb_default_show='.$crp_settings['thumb_default_show'].'&thumb_timthumb='.$crp_settings['thumb_timthumb'].'&thumb_timthumb_q='.$crp_settings['thumb_timthumb_q'].'&scan_images='.$crp_settings['scan_images'].'&class=crp_thumb&filter=crp_postimage'); //$output .= crp_get_the_post_thumbnail($search->ID, $crp_settings); $output .= '</a>'; } if ($post_thumb_op=='inline' || $post_thumb_op=='text_only') { $output .= '<a href="'.get_permalink($search->ID).'" '.$rel_attribute.' '.$target_attribute.' class="crp_title">'.$title.'</a>'; // Add title when required by settings } //$output .= '</a>'; // Close the link if ($show_excerpt) { $output .= '<span class="crp_excerpt"> '.crp_excerpt($search->ID,$crp_settings['excerpt_length']).'</span>'; } $output .= $crp_settings['after_list_item']; $search_counter++; } if ($search_counter == $limit) break; // End loop when related posts limit is reached } //end of foreach loop if ($crp_settings['show_credit']) { $output .= $crp_settings['before_list_item']; $output .= __('Powered by',CRP_LOCAL_NAME); $output .= ' <a href="http://ajaydsouza.com/wordpress/plugins/contextual-related-posts/" rel="nofollow">Contextual Related Posts</a>'.$crp_settings['after_list_item']; } $output .= $crp_settings['after_list']; }else{ $output .= ($crp_settings['blank_output']) ? ' ' : '<p>'.$crp_settings['blank_output_text'].'</p>'; //$output .= '<p>'.strip_tags($sql).'</p>'; } if ((strpos($output, $crp_settings['before_list_item'])) === false) { $output = '<div id="crp_related">'; $output .= ($crp_settings['blank_output']) ? ' ' : '<p>'.$crp_settings['blank_output_text'].'</p>'; } $output .= '</div>'; return $output; }