Exemplo n.º 1
0
/**
 * Fetch related posts.
 *
 * @since 1.8.6
 *
 * @deprecated v2.0.0
 * @see	get_crp_posts_id
 *
 * @param int $postid (default: FALSE) The post ID for which you want the posts for
 * @param int $limit (default: FALSE) Maximum posts to retreive
 * @param boolean $strict_limit (default: TRUE) Setting to true will fetch exactly as per limit above
 * @return object Object with Post IDs
 */
function get_crp_posts($postid = FALSE, $limit = FALSE, $strict_limit = TRUE)
{
    _deprecated_function('get_crp_posts', '2.0.0', 'get_crp_posts_id');
    $results = get_crp_posts_id(array('postid' => $postid, 'limit' => $limit, 'strict_limit' => $strict_limit));
    /**
     * Filter object containing the post IDs.
     *
     * @since	1.9
     *
     * @param	object   $results  Object containing the related post IDs
     */
    return apply_filters('get_crp_posts', $results);
}
/**
 * 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);
}
/**
 * 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 get_crp($args = array())
{
    global $wpdb, $post, $crp_settings;
    // if set, save $exclude_categories
    if (isset($args['exclude_categories']) && '' != $args['exclude_categories']) {
        $exclude_categories = explode(',', $args['exclude_categories']);
        $args['strict_limit'] = false;
    }
    $defaults = array('is_widget' => false, 'is_shortcode' => false, 'is_manual' => false, 'echo' => true, 'heading' => true);
    $defaults = array_merge($defaults, $crp_settings);
    // Parse incomming $args into an array and merge it with $defaults
    $args = wp_parse_args($args, $defaults);
    // WPML support.
    if (function_exists('wpml_object_id_filter') || function_exists('icl_object_id')) {
        $args['strict_limit'] = false;
    }
    // Support caching to speed up retrieval
    if (!empty($args['cache'])) {
        $meta_key = 'crp_related_posts';
        if ($args['is_widget']) {
            $meta_key .= '_widget';
        }
        if ($args['is_manual']) {
            $meta_key .= '_manual';
        }
        if (is_feed()) {
            $meta_key .= '_feed';
        }
        $output = get_post_meta($post->ID, $meta_key, true);
        if ($output) {
            return $output;
        }
    }
    // Retrieve the list of posts
    $results = get_crp_posts_id(array_merge($args, array('postid' => $post->ID, 'strict_limit' => isset($args['strict_limit']) ? $args['strict_limit'] : true)));
    /**
     * Filter to create a custom HTML output
     *
     * @since 2.2.3
     *
     * @param	mixed              Default return value
     * @param	array   $results   Array of IDs of related posts
     * @param	array   $args      Array of settings
     * @return	string             Custom HTML formatted list of related posts
     */
    $custom_template = apply_filters('crp_custom_template', NULL, $results, $args);
    if (!empty($custom_template)) {
        if (!empty($args['cache'])) {
            update_post_meta($post->ID, $meta_key, $custom_template, '');
        }
        return $custom_template;
    }
    $widget_class = $args['is_widget'] ? 'crp_related_widget' : 'crp_related ';
    $shortcode_class = $args['is_shortcode'] ? 'crp_related_shortcode ' : '';
    $post_classes = $widget_class . $shortcode_class;
    /**
     * Filter the classes added to the div wrapper of the Top 10.
     *
     * @since	2.2.3
     *
     * @param	string   $post_classes	Post classes string.
     */
    $post_classes = apply_filters('crp_post_class', $post_classes);
    $output = '<div class="' . $post_classes . '">';
    if ($results) {
        $loop_counter = 0;
        $output .= crp_heading_title($args);
        $output .= crp_before_list($args);
        // We need this for WPML support
        $processed_results = array();
        foreach ($results as $result) {
            /* Support WPML */
            $resultid = crp_object_id_cur_lang($result->ID);
            // If this is NULL or already processed ID or matches current post then skip processing this loop.
            if (!$resultid || in_array($resultid, $processed_results) || intval($resultid) === intval($post->ID)) {
                continue;
            }
            // Push the current ID into the array to ensure we're not repeating it
            array_push($processed_results, $resultid);
            /**
             * 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	$resultid	ID of the post
             */
            $resultid = apply_filters('crp_post_id', $resultid);
            $result = get_post($resultid);
            // Let's get the Post using the ID
            // Process the category exclusion if passed in the shortcode
            if (isset($exclude_categories)) {
                $categorys = get_the_category($result->ID);
                // 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;
                        // Skip loop execution and go to the next step
                    }
                }
                if ($p_in_c) {
                    continue;
                    // Skip loop execution and go to the next step
                }
            }
            $output .= crp_before_list_item($args, $result);
            $output .= crp_list_link($args, $result);
            if ($args['show_author']) {
                $output .= crp_author($args, $result);
            }
            if ($args['show_date']) {
                $output .= '<span class="crp_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> ';
            }
            if ($args['show_excerpt']) {
                $output .= '<span class="crp_excerpt"> ' . crp_excerpt($result->ID, $args['excerpt_length']) . '</span>';
            }
            $loop_counter++;
            $output .= crp_after_list_item($args, $result);
            if ($loop_counter == $args['limit']) {
                break;
                // End loop when related posts limit is reached
            }
        }
        //end of foreach loop
        if ($args['show_credit']) {
            $output .= crp_before_list_item($args, $result);
            $output .= sprintf(__('Powered by <a href="%s" rel="nofollow">Contextual Related Posts</a>', 'contextual-related-posts'), esc_url('https://webberzone.com/plugins/contextual-related-posts/'));
            $output .= crp_after_list_item($args, $result);
        }
        $output .= crp_after_list($args);
        $clearfix = '<div class="crp_clear"></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 .= $args['blank_output'] ? ' ' : '<p>' . $args['blank_output_text'] . '</p>';
    }
    // Check if the opening list tag is missing in the output, it means all of our results were eliminated cause of the category filter
    if (false === strpos($output, $args['before_list_item'])) {
        $output = '<div id="crp_related">';
        $output .= $args['blank_output'] ? ' ' : '<p>' . $args['blank_output_text'] . '</p>';
    }
    $output .= '</div>';
    // closing div of 'crp_related'
    // Support caching to speed up retrieval
    if (!empty($args['cache'])) {
        update_post_meta($post->ID, $meta_key, $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('get_crp', $output, $args);
}