Example #1
0
/**
 * Function to return formatted list of popular posts.
 *
 * @since	1.5
 *
 * @param	mixed	$args	Arguments array
 * @return	array|string	Array of posts if posts_only = 0 or a formatted string if posts_only = 1
 */
function tptn_pop_posts($args)
{
    global $wpdb, $id, $tptn_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('daily' => FALSE, 'is_widget' => FALSE, 'is_shortcode' => FALSE, 'is_manual' => FALSE, 'echo' => FALSE, 'strict_limit' => FALSE, 'posts_only' => FALSE, 'heading' => 1);
    // Merge the $defaults array with the $tptn_settings array
    $defaults = array_merge($defaults, $tptn_settings);
    // Parse incomming $args into an array and merge it with $defaults
    $args = wp_parse_args($args, $defaults);
    $output = '';
    /**
     * Fires before the output processing begins.
     *
     * @since	2.2.0
     *
     * @param	string	$output	Formatted list of top posts
     * @param	array	$args	Array of arguments
     */
    do_action('pre_tptn_pop_posts', $output, $args);
    // Check if the cache is enabled and if the output exists. If so, return the output
    if ($args['cache'] && !$args['posts_only']) {
        $cache_name = 'tptn';
        $cache_name .= $args['daily'] ? '_daily' : '_total';
        $cache_name .= $args['is_widget'] ? '_widget' : '';
        $cache_name .= $args['is_shortcode'] ? '_shortcode' : '';
        $cache_name .= $args['is_manual'] ? '_manual' : '';
        $output = get_transient($cache_name);
        if (false !== $output) {
            /**
             * Filter the output
             *
             * @since	1.9.8.5
             *
             * @param	string	$output	Formatted list of top posts
             * @param	array	$args	Array of arguments
             */
            return apply_filters('tptn_pop_posts', $output, $args);
        }
    }
    // Get thumbnail size
    list($args['thumb_width'], $args['thumb_height']) = tptn_get_thumb_size($args);
    // Retrieve the popular posts
    $results = get_tptn_pop_posts($args);
    if ($args['posts_only']) {
        // Return the array of posts only if the variable is set
        _deprecated_argument(__FUNCTION__, '2.2.0', __('posts_only argument has been deprecated. Use get_tptn_pop_posts() to get the posts only.', 'tptn'));
        return $results;
    }
    $counter = 0;
    $daily_class = $args['daily'] ? 'tptn_posts_daily ' : 'tptn_posts ';
    $widget_class = $args['is_widget'] ? ' tptn_posts_widget' : '';
    $shortcode_class = $args['is_shortcode'] ? ' tptn_posts_shortcode' : '';
    $post_classes = $daily_class . $widget_class . $shortcode_class;
    /**
     * Filter the classes added to the div wrapper of the Top 10.
     *
     * @since	2.1.0
     *
     * @param	string   $post_classes	Post classes string.
     */
    $post_classes = apply_filters('tptn_post_class', $post_classes);
    $output .= '<div class="' . $post_classes . '">';
    if ($results) {
        $output .= tptn_heading_title($args);
        $output .= tptn_before_list($args);
        // We need this for WPML support
        $processed_results = array();
        foreach ($results as $result) {
            /* Support WPML */
            $resultid = tptn_object_id_cur_lang($result->ID);
            if (in_array($resultid, $processed_results)) {
                continue;
            }
            array_push($processed_results, $resultid);
            $sumcount = $result->sumCount;
            // Store the count. We'll need this later
            /**
             * Filter the post ID for each result. Allows a custom function to hook in and change the ID if needed.
             *
             * @since	1.9.8.5
             *
             * @param	int	$resultid	ID of the post
             */
            $postid = apply_filters('tptn_post_id', $resultid);
            $result = get_post($postid);
            // 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 .= tptn_before_list_item($args, $result);
            $output .= tptn_list_link($args, $result);
            if ($args['show_author']) {
                $output .= tptn_author($args, $result);
            }
            if ($args['show_date']) {
                $output .= '<span class="tptn_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> ';
            }
            if ($args['show_excerpt']) {
                $output .= '<span class="tptn_excerpt"> ' . tptn_excerpt($result->ID, $args['excerpt_length']) . '</span>';
            }
            if ($args['disp_list_count']) {
                $tptn_list_count = '(' . number_format_i18n($sumcount) . ')';
                /**
                 * Filter the formatted list count text.
                 *
                 * @since	2.1.0
                 *
                 * @param	string	$tptn_list_count	Formatted list count
                 * @param	int		$sumcount			Post count
                 * @param	object	$result				Post object
                 */
                $tptn_list_count = apply_filters('tptn_list_count', $tptn_list_count, $sumcount, $result);
                $output .= ' <span class="tptn_list_count">' . $tptn_list_count . '</span>';
            }
            $tptn_list = '';
            /**
             * Filter Formatted list item with link and and thumbnail.
             *
             * @since	2.2.0
             *
             * @param	string	$tptn_list
             * @param	object	$result	Object of the current post result
             * @param	array	$args	Array of arguments
             */
            $output .= apply_filters('tptn_list', $tptn_list, $result, $args);
            // Opening span created in tptn_list_link()
            if ('inline' == $args['post_thumb_op'] || 'text_only' == $args['post_thumb_op']) {
                $output .= '</span>';
            }
            $output .= tptn_after_list_item($args, $result);
            $counter++;
            if ($counter == $args['limit']) {
                break;
                // End loop when related posts limit is reached
            }
        }
        if ($args['show_credit']) {
            $output .= tptn_before_list_item($args, $result);
            $output .= sprintf(__('Popular posts by <a href="%s" rel="nofollow" %s>Top 10 plugin</a>', 'tptn'), esc_url('https://webberzone.com/plugins/top-10/'), $target_attribute);
            $output .= tptn_after_list_item($args, $result);
        }
        $output .= tptn_after_list($args);
        $clearfix = '<div class="tptn_clear"></div>';
        /**
         * Filter the clearfix div tag. This is included after the closing tag to clear any miscellaneous floating elements;
         *
         * @since	2.2.0
         *
         * @param	string	$clearfix	Contains: <div style="clear:both"></div>
         */
        $output .= apply_filters('tptn_clearfix', $clearfix);
    } else {
        $output .= $args['blank_output'] ? '' : $args['blank_output_text'];
    }
    $output .= '</div>';
    // Check if the cache is enabled and if the output exists. If so, return the output
    if ($args['cache']) {
        /**
         * Filter the cache time which allows a function to override this
         *
         * @since	2.2.0
         *
         * @param	int		$args['cache_time']	Cache time in seconds
         * @param	array	$args				Array of all the arguments
         */
        $cache_time = apply_filters('tptn_cache_time', $args['cache_time'], $args);
        $output .= "<br /><!-- Cached output. Cached time is {$cache_time} seconds -->";
        set_transient($cache_name, $output, $cache_time);
    }
    /**
     * Filter already documented in top-10.php
     */
    return apply_filters('tptn_pop_posts', $output, $args);
}
Example #2
0
/**
 * Function to return popular posts.
 *
 * @since	1.5
 *
 * @param	mixed	$args	Arguments array
 * @return	array|string	Array of posts if posts_only = 0 or a formatted string if posts_only = 1
 */
function tptn_pop_posts($args)
{
    global $wpdb, $id, $tptn_settings;
    $defaults = array('is_widget' => FALSE, 'daily' => FALSE, 'echo' => FALSE, 'strict_limit' => FALSE, 'posts_only' => FALSE, 'is_shortcode' => FALSE, 'heading' => 1);
    // Merge the $defaults array with the $tptn_settings array
    $defaults = array_merge($defaults, $tptn_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);
    $tptn_thumb_size = tptn_get_all_image_sizes($thumb_size);
    if (isset($tptn_thumb_size['width'])) {
        $thumb_width = $tptn_thumb_size['width'];
        $thumb_height = $tptn_thumb_size['height'];
    }
    if (empty($thumb_width)) {
        $thumb_width = $tptn_settings['thumb_width'];
    }
    if (empty($thumb_height)) {
        $thumb_height = $tptn_settings['thumb_height'];
    }
    $exclude_categories = explode(',', $exclude_categories);
    $target_attribute = $link_new_window ? ' target="_blank" ' : ' ';
    // Set Target attribute
    $rel_attribute = $link_nofollow ? 'bookmark nofollow' : 'bookmark';
    // Set nofollow attribute
    // Retrieve the popular posts
    $results = get_tptn_pop_posts($args);
    if ($posts_only) {
        // Return the array of posts only if the variable is set
        return $results;
    }
    $counter = 0;
    $output = '';
    $shortcode_class = $is_shortcode ? ' tptn_posts_shortcode' : '';
    $widget_class = $is_widget ? ' tptn_posts_widget' : '';
    $post_classes = $widget_class . $shortcode_class;
    /**
     * Filter the classes added to the div wrapper of the Top 10.
     *
     * @since	2.1.0
     *
     * @param	string   $post_classes	Post classes string.
     */
    $post_classes = apply_filters('tptn_post_class', $post_classes);
    if ($heading) {
        if (!$daily) {
            $output .= '<div id="tptn_related" class="tptn_posts ' . $post_classes . '">';
            /**
             * Filter the title of the Top posts.
             *
             * @since	1.9.5
             *
             * @param	string   $title	Title of the popular posts.
             */
            $output .= apply_filters('tptn_heading_title', $title);
        } else {
            $output .= '<div id="tptn_related_daily" class="tptn_posts_daily' . $shortcode_class . '">';
            /**
             * Already defined in top-10.php
             */
            $output .= apply_filters('tptn_heading_title', $title_daily);
        }
    } else {
        if (!$daily) {
            $output .= '<div class="tptn_posts' . $post_classes . '">';
        } else {
            $output .= '<div class="tptn_posts_daily' . $post_classes . '">';
        }
    }
    if ($results) {
        /**
         * Filter the opening tag of the popular posts list
         *
         * @since	1.9.10.1
         *
         * @param	string	$before_list	Opening tag set in the Settings Page
         */
        $output .= apply_filters('tptn_before_list', $before_list);
        $processed_results = array();
        foreach ($results as $result) {
            /* Support WPML */
            $resultid = tptn_object_id_cur_lang($result->ID);
            if (in_array($resultid, $processed_results)) {
                continue;
            }
            array_push($processed_results, $resultid);
            $sumcount = $result->sumCount;
            // Store the count. We'll need this later
            $result = get_post($resultid);
            // Let's get the Post using the ID
            /**
             * Filter the post ID for each result. Allows a custom function to hook in and change the ID if needed.
             *
             * @since	1.9.8.5
             *
             * @param	int	$result->ID	ID of the post
             */
            $postid = apply_filters('tptn_post_id', $result->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.8.5
             *
             * @param	int	$result->ID	ID of the post
             */
            $categorys = get_the_category(apply_filters('tptn_post_cat_id', $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;
                }
                // End loop if post found in category
            }
            $post_title = tptn_max_formatted_content(get_the_title($postid), $title_length);
            /**
             * Filter the post title of each list item.
             *
             * @since	2.0.0
             *
             * @param	string	$post_title	Post title in the list.
             * @param	object	$result	Object of the current post result
             */
            $post_title = apply_filters('tptn_post_title', $post_title, $result);
            if (!$p_in_c) {
                /**
                 * Filter the opening tag of each list item.
                 *
                 * @since	1.9.10.1
                 *
                 * @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('tptn_before_list_item', $before_list_item, $result);
                /**
                 * Filter the `rel` attribute each list item.
                 *
                 * @since	1.9.10.1
                 *
                 * @param	string	$rel_attribute	rel attribute
                 * @param	object	$result			Object of the current post result
                 */
                $rel_attribute = apply_filters('tptn_rel_attribute', $rel_attribute, $result);
                /**
                 * Filter the target attribute each list item.
                 *
                 * @since	1.9.10.1
                 *
                 * @param	string	$target_attribute	target attribute
                 * @param	object	$result				Object of the current post result
                 */
                $target_attribute = apply_filters('tptn_rel_attribute', $target_attribute, $result);
                if ('after' == $post_thumb_op) {
                    $output .= '<a href="' . get_permalink($postid) . '" rel="' . $rel_attribute . '" ' . $target_attribute . ' class="tptn_link">';
                    // Add beginning of link
                    $output .= '<span class="tptn_title">' . $post_title . '</span>';
                    // Add title if post thumbnail is to be displayed after
                    $output .= '</a>';
                    // Close the link
                }
                if ('inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op) {
                    $output .= '<a href="' . get_permalink($postid) . '" rel="' . $rel_attribute . '" ' . $target_attribute . ' class="tptn_link">';
                    // Add beginning of link
                    $output .= tptn_get_the_post_thumbnail(array('postid' => $postid, '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, 'scan_images' => $scan_images, 'class' => "tptn_thumb"));
                    $output .= '</a>';
                    // Close the link
                }
                if ('inline' == $post_thumb_op || 'text_only' == $post_thumb_op) {
                    $output .= '<span class="tptn_after_thumb">';
                    $output .= '<a href="' . get_permalink($postid) . '" rel="' . $rel_attribute . '" ' . $target_attribute . ' class="tptn_link">';
                    // Add beginning of link
                    $output .= '<span class="tptn_title">' . $post_title . '</span>';
                    // Add title when required by settings
                    $output .= '</a>';
                    // Close the link
                }
                if ($show_author) {
                    $author_info = get_userdata($result->post_author);
                    $author_name = ucwords(trim(stripslashes($author_info->display_name)));
                    $author_link = get_author_posts_url($author_info->ID);
                    /**
                     * 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('tptn_author_name', $author_name, $author_info);
                    $tptn_author = '<span class="tptn_author"> ' . __(' by ', TPTN_LOCAL_NAME) . '<a href="' . $author_link . '">' . $author_name . '</a></span> ';
                    /**
                     * Filter the text with the author details.
                     *
                     * @since	2.0.0
                     *
                     * @param	string	$tptn_author	Formatted string with author details and link
                     * @param	object	$author_info	WP_User object of the post author
                     */
                    $tptn_author = apply_filters('tptn_author', $tptn_author, $author_info);
                    $output .= $tptn_author;
                }
                if ($show_date) {
                    $output .= '<span class="tptn_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> ';
                }
                if ($show_excerpt) {
                    $output .= '<span class="tptn_excerpt"> ' . tptn_excerpt($postid, $excerpt_length) . '</span>';
                }
                if ($disp_list_count) {
                    $tptn_list_count = '(' . number_format_i18n($sumcount) . ')';
                    /**
                     * Filter the formatted list count text.
                     *
                     * @since	2.1.0
                     *
                     * @param	string	$tptn_list_count	Formatted list count
                     * @param	int		$sumcount			Post count
                     * @param	object	$result				Post object
                     */
                    $tptn_list_count = apply_filters('tptn_list_count', $tptn_list_count, $sumcount, $result);
                    $output .= ' <span class="tptn_list_count">' . $tptn_list_count . '</span>';
                }
                if ('inline' == $post_thumb_op || 'text_only' == $post_thumb_op) {
                    $output .= '</span>';
                }
                /**
                 * Filter the closing tag of each list item.
                 *
                 * @since	1.9.10.1
                 *
                 * @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('tptn_after_list_item', $after_list_item, $result);
                $counter++;
            }
            if ($counter == $limit) {
                break;
                // End loop when related posts limit is reached
            }
        }
        if ($show_credit) {
            /** This filter is documented in contextual-related-posts.php */
            $output .= apply_filters('tptn_before_list_item', $before_list_item, $result);
            $output .= sprintf(__('Popular posts by <a href="%s" rel="nofollow" %s>Top 10 plugin</a>', TPTN_LOCAL_NAME), esc_url('http://ajaydsouza.com/wordpress/plugins/top-10/'), $target_attribute);
            /** This filter is documented in contextual-related-posts.php */
            $output .= apply_filters('tptn_after_list_item', $after_list_item, $result);
        }
        /**
         * Filter the closing tag of the related posts list
         *
         * @since	1.9.10.1
         *
         * @param	string	$after_list	Closing tag set in the Settings Page
         */
        $output .= apply_filters('tptn_after_list', $after_list);
    } else {
        $output .= $blank_output ? '' : $blank_output_text;
    }
    $output .= '</div>';
    /**
     * Filter the output
     *
     * @since	1.9.8.5
     *
     * @param	string	$output	Formatted list of top posts
     */
    return apply_filters('tptn_pop_posts', $output, $args);
}