( $range ) ) { $args = Helper::merge_array_r( $args, $range ); } else { $range = is_string( $range ) ? trim( $range ) : null; $args['range'] = ! $range ? 'all' : $range; } $args['range'] = strtolower( $args['range'] ); // Get all-time views count if ( 'all' == $args['range'] ) { $query = "SELECT pageviews FROM {$prefix}data WHERE postid = '{$id}'"; } // Get views count within time range else { $start_date = new \DateTime( Helper::now(), new \DateTimeZone( Helper::get_timezone() ) ); // Determine time range switch ( $args['range'] ) { case 'last24hours': case 'daily': $start_date = $start_date->sub( new \DateInterval( 'P1D' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; break; case 'last7days': case 'weekly': $start_date = $start_date->sub( new \DateInterval( 'P6D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; break; case 'last30days': case 'monthly': $start_date = $start_date->sub( new \DateInterval( 'P29D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; break; case 'custom': $time_units = array( 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH' ); // Valid time unit if ( isset( $args['time_unit'] ) && in_array( strtoupper( $args['time_unit'] ), $time_units ) && isset( $args['time_quantity'] ) && filter_var( $args['time_quantity'], FILTER_VALIDATE_INT ) && $args['time_quantity'] > 0 ) { $time_quantity = $args['time_quantity']; $time_unit = strtoupper( $args['time_unit'] ); if ( 'MINUTE' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'PT' . ( 60 * $time_quantity ) . 'S' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; } elseif ( 'HOUR' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'PT' . ( ( 60 * $time_quantity ) - 1 ) . 'M59S' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; } elseif ( 'DAY' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'P' . ( $time_quantity - 1 ) . 'D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; } elseif ( 'WEEK' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'P' . ( ( 7 * $time_quantity ) - 1 ) . 'D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; } else { $start_date = $start_date->sub( new \DateInterval( 'P' . ( ( 30 * $time_quantity ) - 1 ) . 'D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; } } // Invalid time unit, default to last 24 hours else { $start_date = $start_date->sub( new \DateInterval( 'P1D' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; } break; default: $start_date = $start_date->sub( new \DateInterval( 'P1D' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; break; } $query = $wpdb->prepare( "SELECT SUM(pageviews) AS pageviews FROM `{$prefix}summary` WHERE {$views_time_range} AND postid = %d;", $args['_postID'] ); } $results = $wpdb->get_var( $query ); if ( ! $results ) { return 0; } return $number_format ? number_format_i18n( intval( $results ) ) : $results; } public function query( $instance ) { /** * @var wpdb $wpdb */ global $wpdb; $default = array( 'limit' => 10, 'offset' => 0, 'paged' => 1, 'range' => 'all', 'freshness' => false, 'order_by' => 'views', 'post_type' => 'post', 'include_post' => '', 'included_only' => false, 'exclude_post' => '', 'include_category' => '', 'exclude_category' => '', 'include_tag' => '', 'exclude_tag' => '', 'author' => '', 'post_format' => '', ); if ( isset( $wpdb ) ) { $prefix = $wpdb->prefix . 'popularposts'; $joint_query = JNews_View_Counter()->options['general']['optimize_query'] ? 'JOIN' : 'LEFT JOIN'; $instance = Helper::merge_array_r( $default, (array) $instance ); $now = new \DateTime( Helper::now(), new \DateTimeZone( Helper::get_timezone() ) ); $args = array(); $included_args = array(); $fields = 'p.ID AS id, p.post_title AS title, p.post_author AS uid'; $table = ''; $join = ''; $where = 'WHERE 1 = 1'; $groupby = ''; $orderby = ''; $limit = 'LIMIT ' . ( filter_var( $instance['limit'], FILTER_VALIDATE_INT ) && $instance['limit'] > 0 ? $instance['limit'] : 10 ) . ( isset( $instance['offset'] ) && filter_var( $instance['offset'], FILTER_VALIDATE_INT ) !== false && $instance['offset'] >= 0 ? " OFFSET {$instance['offset']}" : '' ); // Get post date $fields .= ', p.post_date AS date'; // we don't need get get post excerpt $instance /* * see SDan7CUJ * Check if add include post in this query , the included post will always displayed at frist placed */ if ( isset( $instance['include_post'] ) && ! empty( $instance['include_post'] ) ) { $included_post_ids = explode( ',', $instance['include_post'] ); $ipid = ''; $where = 'WHERE (p.ID IN('; $oreder_by_included = 'CASE '; foreach ( $included_post_ids as $key => $included_post_id ) { $ipid .= '%d, '; array_push( $args, trim( $included_post_id ) ); array_push( $included_args, trim( $included_post_id ) ); array_push( $included_args, $key ); $oreder_by_included .= 'WHEN ID = %d THEN %d '; } $oreder_by_included .= 'ELSE ' . ( count( $included_post_ids ) + 1 ) . ' END, '; if ( $instance['included_only'] ) { $where_clause = '1 = 0'; /* make other condition false if included only option is enabled */ } else { $where_clause = '1 = 1'; } $where .= rtrim( $ipid, ', ' ) . ') ) OR ' . $where_clause; } // List only published, non password-protected posts $where .= " AND p.post_password = '' AND p.post_status = 'publish'"; // we only support one post_type at once if ( isset( $instance['post_type'] ) && ! empty( $instance['post_type'] ) ) { $post_types = explode( ',', $instance['post_type'] ); $pt = ''; $where .= ' AND p.post_type IN('; foreach ( $post_types as $post_type ) { $pt .= '%s, '; array_push( $args, trim( $post_type ) ); } $where .= rtrim( $pt, ', ' ) . ')'; } else { $where .= " AND p.post_type IN('post')"; } // Get entries from these authors if ( isset( $instance['author'] ) && ! empty( $instance['author'] ) ) { $author_IDs = explode( ',', $instance['author'] ); $uid = ''; $where .= ' AND p.post_author IN('; foreach ( $author_IDs as $author_ID ) { $uid .= '%d, '; array_push( $args, trim( $author_ID ) ); } $where .= rtrim( $uid, ', ' ) . ')'; } // Custom Get / exclude entries from this taxonomies if ( isset( $instance['include_category'] ) && ! empty( $instance['include_category'] ) || isset( $instance['exclude_category'] ) && ! empty( $instance['exclude_category'] ) || isset( $instance['include_tag'] ) && ! empty( $instance['include_tag'] ) || isset( $instance['exclude_tag'] ) && ! empty( $instance['exclude_tag'] ) ) { $taxonomies = array( 'category', 'post_tag', ); $in_term_IDs_for_taxonomies = array(); $out_term_IDs_for_taxonomies = array(); if ( isset( $instance['include_category'] ) && ! empty( $instance['include_category'] ) || isset( $instance['exclude_category'] ) && ! empty( $instance['exclude_category'] ) ) { $in_term_IDs_for_taxonomies[] = isset( $instance['include_category'] ) && ! empty( $instance['include_category'] ) ? explode( ',', $instance['include_category'] ) : array(); $out_term_IDs_for_taxonomies[] = isset( $instance['exclude_category'] ) && ! empty( $instance['exclude_category'] ) ? explode( ',', $instance['exclude_category'] ) : array(); } if ( isset( $instance['include_tag'] ) && ! empty( $instance['include_tag'] ) || isset( $instance['exclude_tag'] ) && ! empty( $instance['exclude_tag'] ) ) { $in_term_IDs_for_taxonomies[] = isset( $instance['include_tag'] ) && ! empty( $instance['include_tag'] ) ? explode( ',', $instance['include_tag'] ) : array(); $out_term_IDs_for_taxonomies[] = isset( $instance['exclude_tag'] ) && ! empty( $instance['exclude_tag'] ) ? explode( ',', $instance['exclude_tag'] ) : array(); } foreach ( $taxonomies as $taxIndex => $taxonomy ) { $in_term_IDs = isset( $in_term_IDs_for_taxonomies[ $taxIndex ] ) ? $in_term_IDs_for_taxonomies[ $taxIndex ] : array(); $out_term_IDs = isset( $out_term_IDs_for_taxonomies[ $taxIndex ] ) ? $out_term_IDs_for_taxonomies[ $taxIndex ] : array(); $is_mixed = ! empty( $in_term_IDs ) && ! empty( $out_term_IDs ); if ( $is_mixed ) { $where .= " AND p.ID IN ( SELECT object_id FROM `{$wpdb->term_relationships}` AS r JOIN `{$wpdb->term_taxonomy}` AS x ON x.term_taxonomy_id = r.term_taxonomy_id WHERE x.taxonomy = %s"; array_push( $args, $taxonomy ); $inTID = ''; foreach ( $in_term_IDs as $in_term_ID ) { $inTID .= '%d, '; array_push( $args, $in_term_ID ); } $outTID = ''; foreach ( $out_term_IDs as $out_term_ID ) { $outTID .= '%d, '; array_push( $args, $out_term_ID ); } $where .= ' AND x.term_id IN(' . rtrim( $inTID, ', ' ) . ') AND x.term_id NOT IN(' . rtrim( $outTID, ', ' ) . ') )'; } else { $pattern = ! empty( $in_term_IDs ) ? 'IN' : 'NOT IN'; $term_IDs = ! empty( $in_term_IDs ) ? $in_term_IDs : $out_term_IDs; if ( ! empty( $term_IDs ) ) { $where .= " AND p.ID {$pattern} ( SELECT object_id FROM `{$wpdb->term_relationships}` AS r JOIN `{$wpdb->term_taxonomy}` AS x ON x.term_taxonomy_id = r.term_taxonomy_id"; // array_push( $args, $taxonomy ); //WMwjGnQX $TID = ''; foreach ( $term_IDs as $term_ID ) { $TID .= '%d, '; array_push( $args, $term_ID ); } $where .= ' AND x.term_id IN(' . rtrim( $TID, ', ' ) . ') )'; } } } } // Custom Exclude these entries from the listing if ( isset( $instance['exclude_post'] ) && ! empty( $instance['exclude_post'] ) ) { $excluded_post_IDs = explode( ',', $instance['exclude_post'] ); $xpid = ''; $where .= ' AND p.ID NOT IN('; foreach ( $excluded_post_IDs as $excluded_post_ID ) { $xpid .= '%d, '; array_push( $args, trim( $excluded_post_ID ) ); } $where .= rtrim( $xpid, ', ' ) . ')'; } $table = "`{$wpdb->posts}` p"; if ( isset( $instance['post_format'] ) && ! empty( $instance['post_format'] ) ) { if ( 'standard' === $instance['post_format'] ) { array_push( $args, 'post_format', 'post-format-gallery', 'post-format-video' ); $where .= " AND p.ID NOT IN ( SELECT r.object_id FROM {$wpdb->term_relationships} r WHERE r.term_taxonomy_id IN ( SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} tt JOIN {$wpdb->terms} t ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.slug IN (%s, %s) ) )"; } else { array_push( $args, $instance['post_format'] ); $where .= " AND p.ID IN ( SELECT object_id FROM {$wpdb->term_relationships} r LEFT JOIN {$wpdb->terms} t ON t.term_id = r.term_taxonomy_id WHERE t.name = %s ) "; } } if ( isset( $instance['content_type'] ) && ! empty( $instance['content_type'] ) ) { if ( 'review' === $instance['content_type'] ) { $where .= " AND p.ID IN ( SELECT m.post_id FROM {$wpdb->postmeta} m WHERE m.meta_key = 'enable_review' AND m.meta_value = 1 ) "; } if ( 'post' === $instance['content_type'] ) { $where .= " AND p.ID NOT IN ( SELECT m.post_id FROM {$wpdb->postmeta} m WHERE m.meta_key = 'enable_review' AND m.meta_value = 1 ) "; } } // All-time range if ( 'all' == $instance['range'] ) { // Order by views count if ( 'comments' != $instance['order_by'] ) { $join = "{$joint_query} `{$prefix}data` v ON p.ID = v.postid"; // Order by views if ( 'views' == $instance['order_by'] ) { $fields .= ', v.pageviews AS total_count'; $orderby = 'total_count DESC'; } // Order by average views else { $fields .= ", ( v.pageviews/(IF ( DATEDIFF('{$now->format('Y-m-d')}', MIN(v.day)) > 0, DATEDIFF('{$now->format('Y-m-d')}', MIN(v.day)), 1) ) ) AS total_count"; $groupby = 'GROUP BY v.postid'; $orderby = 'total_count DESC'; } // Display comments count, too $fields .= ', p.comment_count'; } // Order by comments count else { $where .= ' AND p.comment_count > 0'; $orderby = 'p.comment_count DESC'; // Display comment count $fields .= ', p.comment_count'; // Display views count, too // if ( isset($instance['stats_tag']['views']) && $instance['stats_tag']['views'] ) { // $fields .= ", IFNULL(v.pageviews, 0) AS pageviews"; // $join = "LEFT JOIN `{$prefix}data` v ON p.ID = v.postid"; // } } } // Custom time range else { $start_date = clone $now; // Determine time range switch ( $instance['range'] ) { case 'last24hours': case 'daily': $start_date = $start_date->sub( new \DateInterval( 'P1D' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; break; case 'last7days': case 'weekly': $start_date = $start_date->sub( new \DateInterval( 'P6D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; break; case 'last30days': case 'monthly': $start_date = $start_date->sub( new \DateInterval( 'P29D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; break; case 'custom': $time_units = array( 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH' ); // Valid time unit if ( isset( $instance['time_unit'] ) && in_array( strtoupper( $instance['time_unit'] ), $time_units ) && isset( $instance['time_quantity'] ) && filter_var( $instance['time_quantity'], FILTER_VALIDATE_INT ) && $instance['time_quantity'] > 0 ) { $time_quantity = $instance['time_quantity']; $time_unit = strtoupper( $instance['time_unit'] ); if ( 'MINUTE' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'PT' . ( 60 * $time_quantity ) . 'S' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; } elseif ( 'HOUR' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'PT' . ( ( 60 * $time_quantity ) - 1 ) . 'M59S' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; } elseif ( 'DAY' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'P' . ( $time_quantity - 1 ) . 'D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; } elseif ( 'WEEK' == $time_unit ) { $start_date = $start_date->sub( new \DateInterval( 'P' . ( ( 7 * $time_quantity ) - 1 ) . 'D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; } else { $start_date = $start_date->sub( new \DateInterval( 'P' . ( ( 30 * $time_quantity ) - 1 ) . 'D' ) ); $start_datetime = $start_date->format( 'Y-m-d' ); $views_time_range = "view_date >= '{$start_datetime}'"; } } // Invalid time unit, default to last 24 hours else { $start_date = $start_date->sub( new \DateInterval( 'P1D' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; } break; default: $start_date = $start_date->sub( new \DateInterval( 'P1D' ) ); $start_datetime = $start_date->format( 'Y-m-d H:i:s' ); $views_time_range = "view_datetime >= '{$start_datetime}'"; break; } // Get entries published within the specified time range if ( isset( $instance['freshness'] ) && $instance['freshness'] ) { $where .= " AND p.post_date >= '{$start_datetime}'"; } // Order by views count if ( 'comments' != $instance['order_by'] ) { // Order by views if ( 'views' == $instance['order_by'] ) { $fields .= ', v.pageviews AS total_count'; $join = "{$joint_query} (SELECT SUM(pageviews) AS pageviews, postid FROM `{$prefix}summary` WHERE {$views_time_range} GROUP BY postid) v ON p.ID = v.postid"; $orderby = 'total_count DESC'; } // Order by average views else { $fields .= ', v.avg_views AS total_count'; $join = "LEFT JOIN (SELECT SUM(pageviews)/(IF ( DATEDIFF('{$now->format('Y-m-d H:i:s')}', '{$start_datetime}') > 0, DATEDIFF('{$now->format('Y-m-d H:i:s')}', '{$start_datetime}'), 1) ) AS avg_views, postid FROM `{$prefix}summary` WHERE {$views_time_range} GROUP BY postid) v ON p.ID = v.postid"; $orderby = 'total_count DESC'; } // Display comments count, too // if ( isset( $instance['stats_tag']['comment_count'] ) && $instance['stats_tag']['comment_count'] ) { // $fields .= ', IFNULL(c.comment_count, 0) AS comment_count'; // $join .= " LEFT JOIN (SELECT comment_post_ID, COUNT(comment_post_ID) AS comment_count FROM `{$wpdb->comments}` WHERE comment_date_gmt >= '{$start_datetime}' AND comment_approved = '1' GROUP BY comment_post_ID) c ON p.ID = c.comment_post_ID"; // } } // Order by comments count else { $fields .= ', c.comment_count AS total_count'; $join = "{$joint_query} (SELECT COUNT(comment_post_ID) AS comment_count, comment_post_ID FROM `{$wpdb->comments}` WHERE comment_date_gmt >= '{$start_datetime}' AND comment_approved = '1' GROUP BY comment_post_ID) c ON p.ID = c.comment_post_ID"; $orderby = 'total_count DESC'; // Display views count, too // if ( isset( $instance['stats_tag']['views'] ) && $instance['stats_tag']['views'] ) { // $fields .= ', v.pageviews'; // $join .= " LEFT JOIN (SELECT SUM(pageviews) AS pageviews, postid FROM `{$prefix}summary` WHERE {$views_time_range} GROUP BY postid) v ON p.ID = v.postid"; // } } } if ( ! empty( $args ) ) { $where = $wpdb->prepare( $where, $args ); } if ( ! empty( $included_args ) && ! $instance['included_only'] ) { $orderby = 'ORDER BY ' . $oreder_by_included . $orderby; $orderby = $wpdb->prepare( $orderby, $included_args ); } else { $orderby = 'ORDER BY ' . $orderby; } $orderby .= ', p.ID DESC'; /* see LFo7cVgv */ // Custom Include these entries from the listing $fields = apply_filters( 'jnews_view_counter_query_fields', $fields, $instance ); $table = apply_filters( 'jnews_view_counter_query_table', $table, $instance ); $join = apply_filters( 'jnews_view_counter_query_join', $join, $instance ); $where = apply_filters( 'jnews_view_counter_query_where', $where, $instance ); $groupby = apply_filters( 'jnews_view_counter_query_group_by', $groupby, $instance ); $orderby = apply_filters( 'jnews_view_counter_query_order_by', $orderby, $instance ); $limit = apply_filters( 'jnews_view_counter_query_limit', $limit, $instance ); // Finally, build the query $query = "SELECT {$fields} FROM {$table} {$join} {$where} {$groupby} {$orderby} {$limit};"; $query = $wpdb->get_results( $query ); if ( isset( $instance['no_found_rows'] ) && ! $instance['no_found_rows'] ) { $total_row = $wpdb->get_results( "SELECT COUNT(*) as total FROM {$table} {$join} {$where} {$groupby}" ); $total_row = $groupby ? count( $total_row ) : $total_row[0]->total; } else { $total_row = 0; } $result_ids = array(); $pagesviews = array(); foreach ( $query as $result ) { $result_ids[] = JNews_View_Counter()->options['general']['sync_multilang'] ? Helper::get_translate_id( $result->id ) : $result->id; $pagesviews[ $result->id ] = $result->total_count; } $all_post = get_posts( array( 'post__in' => $result_ids, 'post_type' => ( isset( $instance['post_type'] ) && ! empty( $instance['post_type'] ) ) ? $instance['post_type'] : 'post', // see qMcFaETm 'showposts' => empty( $result_ids ) ? $instance['limit'] : count( $result_ids ), ) ); $results = Helper::arrange_index( $all_post, $result_ids, $pagesviews ); return array( 'result' => $results, 'total' => $total_row, ); } } }