| 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.3.1 |
| 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('url')); |
| 20 |
$targets = self::get_all_targets(intVal($days)); |
| 21 |
|
| 22 |
foreach ($targets as $entry) { |
| 23 |
$clear_url = str_replace($wpurl['host'],"",$entry['url']); |
| 24 |
|
| 25 |
// Add "frontpage" view counter, if post_type page/postpage and blog view is frontpage |
| 26 |
if ($clear_url == '/' && 'page' != get_option('show_on_front')) { |
| 27 |
if (!isset($posts[0])) $posts[0] = 0; |
| 28 |
$posts[0] += $entry['count']; |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
// Try to get ID from Statify Count URL |
| 33 |
$id = url_to_postid(home_url( $clear_url )); |
| 34 |
if ($id == 0 && 'page' == get_option('show_on_front')) { |
| 35 |
$id = get_option( 'page_for_posts' ); |
| 36 |
} |
| 37 |
|
| 38 |
// Get page by ID |
| 39 |
$page = get_page($id); |
| 40 |
|
| 41 |
// Find statistics for published "post&page" entries |
| 42 |
if ( (isset($page->post_type) && ($page->post_type == $post_type || $post_type == 'postpage')) && (isset($page->post_status) && $page->post_status == 'publish') ) { |
| 43 |
|
| 44 |
if (!empty($page->ID)) { |
| 45 |
// When category is select, then ignore other posts! |
| 46 |
if ($post_type == 'post' && $post_category > 0) { |
| 47 |
$categories = get_the_category($page->ID); |
| 48 |
foreach($categories as $category) $termIDArray[] = $category->term_id; |
| 49 |
if (!in_array($post_category,$termIDArray)) continue; |
| 50 |
} |
| 51 |
|
| 52 |
if (!isset($posts[$page->ID])) $posts[$page->ID] = 0; |
| 53 |
$posts[$page->ID] += $entry['count']; |
| 54 |
} |
| 55 |
} |
| 56 |
if (sizeof($posts) >= $amount) break; |
| 57 |
} |
| 58 |
|
| 59 |
return $posts; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Return array of post objects |
| 64 |
* |
| 65 |
* @since 1.3 |
| 66 |
*/ |
| 67 |
public static function get_posts($post_type, $post_category, $amount, $days) { |
| 68 |
$posts = self::get_post_ids($post_type, $post_category, $amount, $days); |
| 69 |
$wp_posts = array(); |
| 70 |
|
| 71 |
foreach ($posts as $post_id=>$views) { |
| 72 |
$wp_posts[$post_id] = new Statify_Post($post_id, $views); |
| 73 |
} |
| 74 |
|
| 75 |
return $wp_posts; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Return array of post objects |
| 80 |
* |
| 81 |
* @since 1.3 |
| 82 |
*/ |
| 83 |
public static function get_post_list($post_type, $post_category, $amount, $days) { |
| 84 |
return new WP_Query(array('post__in' =>array_keys(self::get_post_ids($post_type, $post_category, $amount, $days)))); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Sorted by views |
| 89 |
* |
| 90 |
* @since 1.0 |
| 91 |
*/ |
| 92 |
|
| 93 |
private static function visitSort($a, $b) { |
| 94 |
if ($a==$b) return 0; |
| 95 |
return ($a['visits']>$b['visits'])?-1:1; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns amount of view per id |
| 100 |
* |
| 101 |
* @since 1.1.4 |
| 102 |
*/ |
| 103 |
|
| 104 |
public static function get_statify_count($post_id, $days) { |
| 105 |
if (empty($post_id)) { |
| 106 |
global $post; |
| 107 |
$post_id = $post->ID; |
| 108 |
} |
| 109 |
|
| 110 |
$targets = self::get_all_targets(intval($days)); |
| 111 |
$count = 0; |
| 112 |
|
| 113 |
foreach ($targets as $entry) { |
| 114 |
$clear_url = str_replace(get_bloginfo('wpurl'),"",$entry['url']); |
| 115 |
$id = url_to_postid(home_url( $clear_url )); |
| 116 |
|
| 117 |
if ($id == $post_id) { |
| 118 |
$count = $entry['count']; |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
return $count; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns all views |
| 128 |
* |
| 129 |
* @since 1.2 |
| 130 |
*/ |
| 131 |
|
| 132 |
public static function statify_count_sum($days) { |
| 133 |
$wpurl = parse_url(get_bloginfo('wpurl')); |
| 134 |
$targets = self::get_all_targets(intval($days)); |
| 135 |
$count = 0; |
| 136 |
|
| 137 |
foreach ($targets as $entry) { |
| 138 |
$count += $entry['count']; |
| 139 |
} |
| 140 |
|
| 141 |
return $count; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Return amount of views per id |
| 146 |
* |
| 147 |
* @since 1.1.4 |
| 148 |
* @change 1.1.6 |
| 149 |
*/ |
| 150 |
|
| 151 |
public static function statify_count($post_id, $days) { |
| 152 |
$count = self::get_statify_count($post_id, $days); |
| 153 |
return $count >= 0 ? $count : 0; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* The shorctode for call amount of views per id. |
| 158 |
* |
| 159 |
* @since 1.1.4 |
| 160 |
* @change 1.3.1 |
| 161 |
*/ |
| 162 |
|
| 163 |
public static function statify_count_shortcode( $atts ) { |
| 164 |
global $post; |
| 165 |
|
| 166 |
// Attr |
| 167 |
$a = shortcode_atts( array( |
| 168 |
'prefix' => '', |
| 169 |
'suffix' => __('views','statify-widget'), |
| 170 |
'days' => 0 |
| 171 |
), $atts ); |
| 172 |
|
| 173 |
return $a['prefix'] . " " . self::statify_count($post->ID, $a['days']) . " " . $a['suffix']; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* The shortcode for call all amount of views. |
| 178 |
* |
| 179 |
* @since 1.2 |
| 180 |
* @change 1.3.1 |
| 181 |
*/ |
| 182 |
|
| 183 |
public static function statify_count_sum_shortcode( $atts ) { |
| 184 |
global $post; |
| 185 |
|
| 186 |
// Attr |
| 187 |
$a = shortcode_atts( array( |
| 188 |
'prefix' => '', |
| 189 |
'suffix' => __('views','statify-widget'), |
| 190 |
'days' => 0 |
| 191 |
), $atts ); |
| 192 |
|
| 193 |
return $a['prefix'] . " " . self::statify_count_sum($a['days']) . " " . $a['suffix']; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Return all targets from statify and saved the values for 4 minutes. |
| 198 |
* |
| 199 |
* @since 1.1 |
| 200 |
*/ |
| 201 |
|
| 202 |
public static function get_all_targets($interval) |
| 203 |
{ |
| 204 |
/* Look for cached values */ |
| 205 |
if ($data = get_transient('statify_targets_'.$interval)) { |
| 206 |
return $data; |
| 207 |
} |
| 208 |
|
| 209 |
if ($interval > 0) { |
| 210 |
$current_time = current_time('timestamp'); |
| 211 |
$interval_time = $current_time - ($interval * DAY_IN_SECONDS); |
| 212 |
$date = date("Y-m-d", $interval_time); |
| 213 |
} |
| 214 |
|
| 215 |
global $wpdb; |
| 216 |
|
| 217 |
if ($interval > 0) { |
| 218 |
$data = $wpdb->get_results( |
| 219 |
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE `created` > '$date' GROUP BY `target` ORDER BY `count` DESC", |
| 220 |
ARRAY_A |
| 221 |
); |
| 222 |
} else { |
| 223 |
$data = $wpdb->get_results( |
| 224 |
"SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` GROUP BY `target` ORDER BY `count` DESC", |
| 225 |
ARRAY_A |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
/* Save values for 4 minutes */ |
| 230 |
set_transient( |
| 231 |
'statify_targets_'.$interval, $data, 60 * 4 // = 4 minutes. |
| 232 |
); |
| 233 |
|
| 234 |
return $data; |
| 235 |
} |
| 236 |
|
| 237 |
} |