| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Main class return the posts |
| 5 |
* |
| 6 |
* @since 1.0 |
| 7 |
*/ |
| 8 |
class Statify_Posts { |
| 9 |
|
| 10 |
/** |
| 11 |
* Return the content |
| 12 |
* |
| 13 |
* @since 1.0 |
| 14 |
* @change 1.4.6 |
| 15 |
*/ |
| 16 |
public static function get_post_ids($post_type, $post_category, $amount, $days) { |
| 17 |
$posts = array(); |
| 18 |
$counter = 0; |
| 19 |
$wpurl = parse_url(get_bloginfo('wpurl')); |
| 20 |
$targets = self::get_all_targets(intVal($days)); |
| 21 |
$show_on_front = get_option('show_on_front'); |
| 22 |
$page_for_posts = get_option( 'page_for_posts' ); |
| 23 |
|
| 24 |
foreach ($targets as $entry) { |
| 25 |
$clear_url = str_replace($wpurl['host'],"",$entry['url']); |
| 26 |
$id = $entry['id']; |
| 27 |
|
| 28 |
// Add "frontpage" view counter if blog view is frontpage |
| 29 |
if ($clear_url == '/' && 'page' != $show_on_front) { |
| 30 |
if ($post_type != 'page' && $post_type != 'postpage') continue; |
| 31 |
if (!isset($posts[0])) $posts[0] = 0; |
| 32 |
$posts[0] += $entry['count']; |
| 33 |
continue; |
| 34 |
} |
| 35 |
|
| 36 |
// Overwrite id with selected frontpage, if option is activated |
| 37 |
if ($id == 0 && 'page' == $show_on_front) { |
| 38 |
$id = $page_for_posts; |
| 39 |
} |
| 40 |
|
| 41 |
// Get page by ID |
| 42 |
$page = get_post($id); |
| 43 |
|
| 44 |
// Find statistics for published "post&page" entries |
| 45 |
if ( (isset($page->post_type) && ($page->post_type == $post_type || $post_type == 'postpage')) && (isset($page->post_status) && $page->post_status == 'publish') ) { |
| 46 |
|
| 47 |
if (!empty($page->ID)) { |
| 48 |
// When category is select, then ignore other posts! |
| 49 |
if ($post_type == 'post' && $post_category > 0) { |
| 50 |
$categories = get_the_category($page->ID); |
| 51 |
if ( ! empty( $categories ) && is_array( $categories ) ) { |
| 52 |
foreach($categories as $category) $termIDArray[] = $category->term_id; |
| 53 |
} |
| 54 |
if (!in_array($post_category,$termIDArray)) continue; |
| 55 |
} |
| 56 |
|
| 57 |
if (!isset($posts[$page->ID])) $posts[$page->ID] = 0; |
| 58 |
$posts[$page->ID] += $entry['count']; |
| 59 |
} |
| 60 |
} |
| 61 |
if (sizeof($posts) >= $amount) break; |
| 62 |
} |
| 63 |
|
| 64 |
return $posts; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Return array of post objects |
| 69 |
* |
| 70 |
* @since 1.3 |
| 71 |
*/ |
| 72 |
public static function get_posts($post_type, $post_category, $amount, $days) { |
| 73 |
$posts = self::get_post_ids($post_type, $post_category, $amount, $days); |
| 74 |
$wp_posts = array(); |
| 75 |
|
| 76 |
foreach ($posts as $post_id=>$views) { |
| 77 |
$wp_posts[$post_id] = new Statify_Post($post_id, $views); |
| 78 |
} |
| 79 |
|
| 80 |
return $wp_posts; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Return array of post objects |
| 85 |
* |
| 86 |
* @since 1.3 |
| 87 |
*/ |
| 88 |
public static function get_post_list($post_type, $post_category, $amount, $days) { |
| 89 |
return new WP_Query(array('post__in' =>array_keys(self::get_post_ids($post_type, $post_category, $amount, $days)))); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Sorted by views |
| 94 |
* |
| 95 |
* @since 1.0 |
| 96 |
*/ |
| 97 |
|
| 98 |
private static function visitSort($a, $b) { |
| 99 |
if ($a==$b) return 0; |
| 100 |
return ($a['visits']>$b['visits'])?-1:1; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Returns amount of view per id |
| 105 |
* |
| 106 |
* @since 1.1.4 |
| 107 |
* @change 1.4.6 |
| 108 |
*/ |
| 109 |
|
| 110 |
public static function get_statify_count($post_id, $days) { |
| 111 |
if (empty($post_id)) { |
| 112 |
global $post; |
| 113 |
$post_id = $post->ID; |
| 114 |
} |
| 115 |
|
| 116 |
$targets = self::get_all_targets(intval($days)); |
| 117 |
$count = 0; |
| 118 |
|
| 119 |
foreach ($targets as $entry) { |
| 120 |
if ($entry['id'] == $post_id) { |
| 121 |
$count = $entry['count']; |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $count; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Returns all views |
| 131 |
* |
| 132 |
* @since 1.2 |
| 133 |
* @change 1.4.6 |
| 134 |
*/ |
| 135 |
|
| 136 |
public static function statify_count_sum($days) { |
| 137 |
$wpurl = parse_url(get_bloginfo('wpurl')); |
| 138 |
$targets = self::get_all_targets(intval($days)); |
| 139 |
return array_sum(array_column($targets, 'count')); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Return amount of views per id |
| 144 |
* |
| 145 |
* @since 1.1.4 |
| 146 |
* @change 1.1.6 |
| 147 |
*/ |
| 148 |
public static function statify_count($post_id, $days) { |
| 149 |
$count = self::get_statify_count($post_id, $days); |
| 150 |
return $count >= 0 ? $count : 0; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* The shorctode for call amount of views per id. |
| 155 |
* |
| 156 |
* @since 1.1.4 |
| 157 |
* @change 1.3.1 |
| 158 |
*/ |
| 159 |
|
| 160 |
public static function statify_count_shortcode( $atts ) { |
| 161 |
global $post; |
| 162 |
|
| 163 |
// Attr |
| 164 |
$a = shortcode_atts( array( |
| 165 |
'prefix' => '', |
| 166 |
'suffix' => __('views','statify-widget'), |
| 167 |
'days' => 0 |
| 168 |
), $atts ); |
| 169 |
|
| 170 |
$prefix = trim( $a['prefix'] ); |
| 171 |
$suffix = trim( $a['suffix'] ); |
| 172 |
$days = intval( $a['days'] ); |
| 173 |
|
| 174 |
$count = intval( self::statify_count( $post->ID, $days ) ); |
| 175 |
|
| 176 |
return esc_html( $prefix ) . ' ' . $count . ' ' . esc_html( $suffix ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* The shortcode for call all amount of views. |
| 181 |
* |
| 182 |
* @since 1.2 |
| 183 |
* @change 1.3.1 |
| 184 |
*/ |
| 185 |
|
| 186 |
public static function statify_count_sum_shortcode( $atts ) { |
| 187 |
global $post; |
| 188 |
|
| 189 |
// Attr |
| 190 |
$a = shortcode_atts( array( |
| 191 |
'prefix' => '', |
| 192 |
'suffix' => __('views','statify-widget'), |
| 193 |
'days' => 0 |
| 194 |
), $atts ); |
| 195 |
|
| 196 |
return $a['prefix'] . " " . self::statify_count_sum($a['days']) . " " . $a['suffix']; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Return all targets from statify and saved the values for 4 minutes. |
| 201 |
* |
| 202 |
* @since 1.1 |
| 203 |
* @change 1.4.6 |
| 204 |
*/ |
| 205 |
|
| 206 |
public static function get_all_targets($interval = 0) |
| 207 |
{ |
| 208 |
$expiry_seconds = apply_filters( 'statify_targets_cache_expiry', STATIFY_WIDGET_DEFAULT_EXPIRATION ); |
| 209 |
if (!is_numeric($expiry_seconds) || $expiry_seconds <= 0) { |
| 210 |
$expiry_seconds = STATIFY_WIDGET_DEFAULT_EXPIRATION; |
| 211 |
} |
| 212 |
|
| 213 |
/* Look for cached values */ |
| 214 |
if ($data = get_transient(STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX.$interval)) { |
| 215 |
return $data; |
| 216 |
} |
| 217 |
|
| 218 |
global $wpdb; |
| 219 |
|
| 220 |
$query = " |
| 221 |
SELECT COUNT(`target`) AS `count`, `target` AS `url` |
| 222 |
FROM `$wpdb->statify` |
| 223 |
"; |
| 224 |
|
| 225 |
if ($interval > 0) { |
| 226 |
$timezone = new DateTimeZone(Statify_Posts::wp_timezone()); |
| 227 |
$datetime = new DateTime('now', $timezone); |
| 228 |
$date = $datetime->modify("-{$interval} days")->format('Y-m-d'); |
| 229 |
$query .= "WHERE `created` > '$date'\n"; |
| 230 |
} |
| 231 |
|
| 232 |
$query .= " |
| 233 |
GROUP BY `target` |
| 234 |
ORDER BY `count` DESC |
| 235 |
"; |
| 236 |
|
| 237 |
$data = $wpdb->get_results($query, ARRAY_A); |
| 238 |
|
| 239 |
if ($all_transient = get_transient(STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX."0")) { |
| 240 |
$map = array_column($all_transient, 'id', 'url'); |
| 241 |
foreach ($data as &$item) { |
| 242 |
$item['id'] = $map[$item['url']] ?? 0; |
| 243 |
} |
| 244 |
unset($item); |
| 245 |
} else { |
| 246 |
foreach ($data as &$item) { |
| 247 |
$item['id'] = ($item['url'] == '/') ? 0 : url_to_postid($item['url']); |
| 248 |
} |
| 249 |
unset($item); |
| 250 |
} |
| 251 |
|
| 252 |
set_transient( |
| 253 |
STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX.$interval, $data, $expiry_seconds |
| 254 |
); |
| 255 |
|
| 256 |
return $data; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Copy of wp_timezone_string() for fallback < WP v5.3 |
| 261 |
* @since 1.4.6 |
| 262 |
*/ |
| 263 |
private static function wp_timezone() { |
| 264 |
if (function_exists('wp_timezone_string')) { |
| 265 |
return wp_timezone_string(); |
| 266 |
} |
| 267 |
|
| 268 |
$timezone_string = get_option( 'timezone_string' ); |
| 269 |
|
| 270 |
if ( $timezone_string ) { |
| 271 |
return $timezone_string; |
| 272 |
} |
| 273 |
|
| 274 |
$offset = (float) get_option( 'gmt_offset' ); |
| 275 |
$hours = (int) $offset; |
| 276 |
$minutes = ( $offset - $hours ); |
| 277 |
|
| 278 |
$sign = ( $offset < 0 ) ? '-' : '+'; |
| 279 |
$abs_hour = abs( $hours ); |
| 280 |
$abs_mins = abs( $minutes * 60 ); |
| 281 |
$tz_offset = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins ); |
| 282 |
|
| 283 |
return $tz_offset; |
| 284 |
} |
| 285 |
} |