/** * 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; // Initialise some variables $fields = ''; $where = ''; $join = ''; $groupby = ''; $orderby = ''; $limits = ''; $match_fields = ''; $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']; } if ($daily) { $table_name = $wpdb->base_prefix . "top_ten_daily"; } else { $table_name = $wpdb->base_prefix . "top_ten"; } $limit = $strict_limit ? $limit : $limit * 5; $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 parse_str($post_types, $post_types); // Save post types in $post_types variable if (empty($post_types)) { $post_types = get_post_types(array('public' => true)); } $blog_id = get_current_blog_id(); if ($daily_midnight) { $current_time = current_time('timestamp', 0); $from_date = $current_time - max(0, $daily_range - 1) * DAY_IN_SECONDS; $from_date = gmdate('Y-m-d 0', $from_date); } else { $current_time = current_time('timestamp', 0); $from_date = $current_time - ($daily_range * DAY_IN_SECONDS + $hour_range * HOUR_IN_SECONDS); $from_date = gmdate('Y-m-d H', $from_date); } /** * * We're going to create a mySQL query that is fully extendable which would look something like this: * "SELECT $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits" * */ // Fields to return $fields = " postnumber, "; $fields .= $daily ? "SUM(cntaccess) as sumCount, dp_date, " : "cntaccess as sumCount, "; $fields .= "ID "; // Create the JOIN clause $join = " INNER JOIN {$wpdb->posts} ON postnumber=ID "; // Create the base WHERE clause $where .= $wpdb->prepare(" AND blog_id = %d ", $blog_id); // Posts need to be from the current blog only $where .= " AND {$wpdb->posts}.post_status = 'publish' "; // Only show published posts //DMG: Mostrar el top de los posts creados hace 14 días $postDate = new DateTime(); $postDate->sub(new DateInterval('P15D')); $mysql_date = date('Y-m-d H:i:s', $postDate->getTimestamp()); $where .= $wpdb->prepare(" AND post_date > '%s' ", $mysql_date); if ($daily) { $where .= $wpdb->prepare(" AND dp_date >= '%s' ", $from_date); // Only fetch posts that are tracked after this date } if ('' != $exclude_post_ids) { $where .= " AND {$wpdb->posts}.ID NOT IN ({$exclude_post_ids}) "; } $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $post_types) . "') "; // Array of post types // Create the base GROUP BY clause if ($daily) { $groupby = " postnumber "; } // Create the base ORDER BY clause $orderby = " sumCount DESC "; // Create the base LIMITS clause $limits .= $wpdb->prepare(" LIMIT %d ", $limit); /** * Filter the SELECT clause of the query. * * @param string $fields The SELECT clause of the query. */ $fields = apply_filters('tptn_posts_fields', $fields); /** * Filter the JOIN clause of the query. * * @param string $join The JOIN clause of the query. */ $join = apply_filters('tptn_posts_join', $join); /** * Filter the WHERE clause of the query. * * @param string $where The WHERE clause of the query. */ $where = apply_filters('tptn_posts_where', $where); /** * Filter the GROUP BY clause of the query. * * @param string $groupby The GROUP BY clause of the query. */ $groupby = apply_filters('tptn_posts_groupby', $groupby); /** * Filter the ORDER BY clause of the query. * * @param string $orderby The ORDER BY clause of the query. */ $orderby = apply_filters('tptn_posts_orderby', $orderby); /** * Filter the LIMIT clause of the query. * * @param string $limits The LIMIT clause of the query. */ $limits = apply_filters('tptn_posts_limits', $limits); if (!empty($groupby)) { $groupby = " GROUP BY {$groupby} "; } if (!empty($orderby)) { $orderby = " ORDER BY {$orderby} "; } $sql = "SELECT {$fields} FROM {$table_name} {$join} WHERE 1=1 {$where} {$groupby} {$orderby} {$limits}"; if ($posts_only) { // Return the array of posts only if the variable is set $tptn_pop_posts_array = $wpdb->get_results($sql, ARRAY_A); /** * Filter the array of top post IDs. * * @since 1.9.8.5 * * @param array $tptn_pop_posts_array Posts array. */ return apply_filters('tptn_pop_posts_array', $tptn_pop_posts_array); } $results = $wpdb->get_results($sql); $counter = 0; $output = ''; $shortcode_class = $is_shortcode ? ' tptn_posts_shortcode' : ''; $widget_class = $is_widget ? ' tptn_posts_widget' : ''; if ($heading) { if (!$daily) { $output .= '<div id="tptn_related" class="tptn_posts ' . $widget_class . $shortcode_class . '">'; /** * 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 . '">'; /** * 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_daily); } } else { if (!$daily) { $output .= '<div class="tptn_posts' . $widget_class . $shortcode_class . '">'; } else { $output .= '<div class="tptn_posts_daily' . $widget_class . $shortcode_class . '">'; } } 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); foreach ($results as $result) { $sumcount = $result->sumCount; $result = get_post($result->ID); // 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); /**** DMG *****/ $output .= '<article class="cp-wrap cp-small clearfix"> '; if ('inline' == $post_thumb_op || 'after' == $post_thumb_op || 'thumbs_only' == $post_thumb_op) { $post_img = get_the_post_thumbnail($postid, 'cp-thumb-small'); //PREVENT WHEN DOES NOT HAVE A THUMB IMG if ($post_img !== '') { $output .= '<div class="cp-thumb-small"> <a href="' . get_permalink($postid) . '" title=" ' . $post_title . '">' . $post_img . '</a> </div>'; } } //else is 'text_only' == $post_thumb_op $post_details = ''; 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); $post_details .= $tptn_author; } if ($show_date) { $post_details .= '<span class="tptn_date"> ' . mysql2date(get_option('date_format', 'd/m/y'), $result->post_date) . '</span> '; } if ($disp_list_count) { $post_details .= ' <span class="tptn_list_count">(' . number_format_i18n($sumcount) . ')</span>'; } $post_summary = ''; if ($show_excerpt) { $post_summary = '<div class="post-summary"> <a href="' . get_permalink($postid) . '" title="' . $post_title . '" rel="bookmark"> <p>' . tptn_excerpt($postid, $excerpt_length) . '</p> </a> </div>'; } /***** DMG *****/ $output .= ' <p class="entry-meta"> <span class="updated">' . $post_details . '</span> </p> <h3 class="cp-title-small"> <a href="' . get_permalink($postid) . '" title="' . $post_title . '" rel="bookmark"> ' . $post_title . ' </a> </h3>' . $post_summary . '</article>'; /** * 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 / 5) { 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); }
/** * 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); }
/** * 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); }
/** * Function to return popular posts. * * @access public * @param mixed $args * @return void */ function tptn_pop_posts($args) { global $wpdb, $siteurl, $tableposts, $id; global $tptn_settings; $defaults = array('is_widget' => FALSE, 'daily' => FALSE, 'echo' => FALSE, 'strict_limit' => FALSE, 'posts_only' => FALSE); $defaults = array_merge($defaults, tptn_read_options()); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); // OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before. extract($args, EXTR_SKIP); if ($daily) { $table_name = $wpdb->prefix . "top_ten_daily"; } else { $table_name = $wpdb->prefix . "top_ten"; } $limit = $strict_limit ? $limit : $limit * 5; $exclude_categories = explode(',', $exclude_categories); $target_attribute = $link_new_window ? ' target="_blank" ' : ' '; // Set Target attribute $rel_attribute = $link_nofollow ? ' nofollow' : ''; // Set nofollow attribute parse_str($post_types, $post_types); // Save post types in $post_types variable if (!$daily) { $sql = "SELECT postnumber, cntaccess as sumCount, ID, post_type, post_status "; $sql .= "FROM {$table_name} INNER JOIN " . $wpdb->posts . " ON postnumber=ID "; $sql .= "AND post_status = 'publish' "; if ($exclude_post_ids != '') { $sql .= "AND ID NOT IN (" . $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 .= "ORDER BY sumCount DESC LIMIT {$limit}"; } else { $current_time = current_time('timestamp', 0); $current_time = $current_time - ($daily_range - 1) * 3600 * 24; $current_date = date('Y-m-j', $current_time); $sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status "; $sql .= "FROM {$table_name} INNER JOIN " . $wpdb->posts . " ON postnumber=ID "; $sql .= "AND post_status = 'publish' AND dp_date >= '{$current_date}' "; if ($exclude_post_ids != '') { $sql .= "AND ID NOT IN (" . $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 .= "GROUP BY postnumber "; $sql .= "ORDER BY sumCount DESC LIMIT {$limit}"; } $results = $wpdb->get_results($sql); if ($posts_only) { return apply_filters('tptn_pop_posts_array', $results); } // Return the array of posts only if the variable is set $counter = 0; $output = ''; if (!$is_widget) { if (!$daily) { $output .= '<div id="tptn_related" class="tptn_posts">' . apply_filters('tptn_heading_title', $title); } else { $output .= '<div id="tptn_related_daily" class="tptn_posts_daily">' . apply_filters('tptn_heading_title', $title_daily); } } else { if (!$daily) { $output .= '<div class="tptn_posts">'; } else { $output .= '<div class="tptn_posts_daily">'; } } if ($results) { $output .= $before_list; foreach ($results as $result) { $sumcount = $result->sumCount; $result = get_post(apply_filters('tptn_post_id', $result->ID)); // Let's get the Post using the ID $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 } $title = tptn_max_formatted_content(get_the_title($result->ID), $title_length); if (!$p_in_c) { $output .= $before_list_item; if ($post_thumb_op == 'after') { $output .= '<a href="' . get_permalink($result->ID) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . '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 ($post_thumb_op == 'inline' || $post_thumb_op == 'after' || $post_thumb_op == 'thumbs_only') { $output .= '<a href="' . get_permalink($result->ID) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . 'class="tptn_link">'; // Add beginning of link $output .= tptn_get_the_post_thumbnail('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 . '&scan_images=' . $scan_images . '&class=tptn_thumb&filter=tptn_postimage'); $output .= '</a>'; // Close the link } if ($post_thumb_op == 'inline' || $post_thumb_op == 'text_only') { $output .= '<span class="tptn_after_thumb">'; $output .= '<a href="' . get_permalink($result->ID) . '" rel="bookmark' . $rel_attribute . '" ' . $target_attribute . '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 } 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); $output .= '<span class="tptn_author"> ' . __(' by ', TPTN_LOCAL_NAME) . '<a href="' . $author_link . '">' . $author_name . '</a></span> '; } 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($result->ID, $excerpt_length) . '</span>'; } if ($disp_list_count) { $output .= ' <span class="tptn_list_count">(' . number_format_i18n($sumcount) . ')</span>'; } if ($post_thumb_op == 'inline' || $post_thumb_op == 'text_only') { $output .= '</span>'; } $output .= $after_list_item; $counter++; } if ($counter == $limit / 5) { break; } // End loop when related posts limit is reached } if ($show_credit) { $output .= $before_list_item . 'Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/" rel="nofollow">Top 10 plugin</a>' . $after_list_item; } $output .= $after_list; } else { $output .= $blank_output ? '' : $blank_output_text; } $output .= '</div>'; return apply_filters('tptn_pop_posts', $output); }