예제 #1
0
/**
 * Returns the formatted list item with link and and thumbnail for 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 tptn_list_link($args, $result)
{
    $output = '';
    $title = tptn_post_title($args, $result);
    $link_attributes = tptn_link_attributes($args);
    if ('after' == $args['post_thumb_op']) {
        $output .= '<a href="' . get_permalink($result->ID) . '" ' . $link_attributes . ' class="tptn_link">';
        // Add beginning of link
        $output .= '<span class="tptn_title">' . $title . '</span>';
        // Add title if post thumbnail is to be displayed after
        $output .= '</a>';
        // Close the link
    }
    if ('inline' == $args['post_thumb_op'] || 'after' == $args['post_thumb_op'] || 'thumbs_only' == $args['post_thumb_op']) {
        $output .= '<a href="' . get_permalink($result->ID) . '" ' . $link_attributes . ' class="tptn_link">';
        // Add beginning of link
        $output .= tptn_get_the_post_thumbnail(array('postid' => $result, 'thumb_height' => $args['thumb_height'], 'thumb_width' => $args['thumb_width'], 'thumb_meta' => $args['thumb_meta'], 'thumb_html' => $args['thumb_html'], 'thumb_default' => $args['thumb_default'], 'thumb_default_show' => $args['thumb_default_show'], 'scan_images' => $args['scan_images'], 'class' => "tptn_thumb"));
        $output .= '</a>';
        // Close the link
    }
    if ('inline' == $args['post_thumb_op'] || 'text_only' == $args['post_thumb_op']) {
        $output .= '<span class="tptn_after_thumb">';
        $output .= '<a href="' . get_permalink($result->ID) . '" ' . $link_attributes . ' class="tptn_link">';
        // Add beginning of link
        $output .= '<span class="tptn_title">' . $title . '</span>';
        // Add title when required by settings
        $output .= '</a>';
        // Close the link
    }
    /**
     * Filter Formatted list item with link and and thumbnail.
     *
     * @since	2.2.0
     *
     * @param	string	$output	Formatted list item with link and and thumbnail
     * @param	object	$result	Object of the current post result
     * @param	array	$args	Array of arguments
     */
    return apply_filters('tptn_list_link', $output, $result, $args);
}
예제 #2
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 $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, 'instance_id' => 1, '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' . $args['instance_id'] : '';
        $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.', 'top-10'));
        return $results;
    }
    $counter = 0;
    $daily_class = $args['daily'] ? 'tptn_posts_daily ' : 'tptn_posts ';
    $widget_class = $args['is_widget'] ? ' tptn_posts_widget tptn_posts_widget' . $args['instance_id'] : '';
    $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 this is NULL or already processed ID or matches current post then skip processing this loop.
            if (!$resultid || in_array($resultid, $processed_results)) {
                continue;
            }
            // Push the current ID into the array to ensure we're not repeating it
            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
             */
            $resultid = apply_filters('tptn_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 .= 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 .= '<p class="tptn_excerpt"> ' . tptn_excerpt($result->ID, $args['excerpt_length']) . '</p>';
            }
            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>', 'top-10'), esc_url('https://webberzone.com/plugins/top-10/'), tptn_link_attributes($args));
            $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);
}