PluginProbe
Statify Widget / 1.2.9
Statify Widget v1.2.9
trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 All 39 releases
statify-widget / Statify_Posts.class.php

Statify_Posts.class.php in Statify Widget 1.2.9, at Statify_Posts.class.php

210 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Statify_Posts {
4 /**
5 * Return the content
6 *
7 * @since 1.0
8 * @change 1.1
9 */
10
11 public static function get_posts($post_type, $post_category, $amount, $interval) {
12 $posts = array();
13 $counter = 0;
14 $wpurl = parse_url(get_bloginfo('url'));
15 $targets = self::get_all_targets(intVal($interval));
16
17 foreach ($targets as $entry) {
18 $clear_url = str_replace($wpurl['host'],"",$entry['url']);
19 $id = url_to_postid(home_url( $clear_url ));
20
21 if ( $id == 0 ) {
22 $page = get_page_by_path( $clear_url );
23 if (empty($page) && get_option('show_on_front') == 'page') $page = get_page(get_option('page_on_front'));
24 } else $page = get_page($id);
25 if ( (isset($page->post_type) && ($page->post_type == $post_type || $post_type == 'postpage')) && (isset($page->post_status) && $page->post_status == 'publish') ) {
26
27 if (!empty($page->ID)) {
28 // When category is select, then ignore other posts!
29 if ($post_type == 'post' && $post_category > 0) {
30 $categories = get_the_category($page->ID);
31
32 foreach($categories as $category) {
33 $termIDArray[] = $category->term_id;
34 }
35 if (!in_array($post_category,$termIDArray)) {
36 continue;
37 }
38 }
39
40 $posts[$page->ID]['title'] = strip_tags($page->post_title);
41 $posts[$page->ID]['url'] = get_permalink($page->ID);
42 if (!array_key_exists('visits', $posts[$page->ID])) $posts[$page->ID]['visits'] = 0;
43 $posts[$page->ID]['visits'] += $entry['count'];
44 }
45
46
47 }
48 if (sizeof($posts) >= $amount) break;
49 }
50
51
52 usort($posts, array("Statify_Posts", "visitSort"));
53 return $posts;
54 }
55
56 /**
57 * Sorted by views
58 *
59 * @since 1.0
60 */
61
62 private static function visitSort($a, $b) {
63 if ($a==$b) return 0;
64 return ($a['visits']>$b['visits'])?-1:1;
65 }
66
67 /**
68 * Returns amount of view per id
69 *
70 * @since 1.1.4
71 */
72
73 public static function get_statify_count($post_id) {
74 if (empty($post_id)) {
75 global $post;
76 $post_id = $post->ID;
77 }
78
79 #$wpurl = parse_url(get_bloginfo('wpurl'));
80 $targets = self::get_all_targets(0);
81 $count = -1;
82
83 foreach ($targets as $entry) {
84 $clear_url = str_replace(get_bloginfo('wpurl'),"",$entry['url']);
85 $id = url_to_postid(home_url( $clear_url ));
86
87 if ($id == $post_id) {
88 $count = $entry['count'];
89 break;
90 }
91 }
92
93 return $count;
94 }
95
96 /**
97 * Returns all views
98 *
99 * @since 1.2
100 */
101
102 public static function statify_count_sum() {
103 if (empty($post_id)) {
104 global $post;
105 $post_id = $post->ID;
106 }
107
108 $wpurl = parse_url(get_bloginfo('wpurl'));
109 $targets = self::get_all_targets(0);
110 $count = -1;
111
112 foreach ($targets as $entry) {
113 $count += $entry['count'];
114 }
115
116 return $count;
117 }
118
119 /**
120 * Return amount of views per id
121 *
122 * @since 1.1.4
123 * @change 1.1.6
124 */
125
126 public static function statify_count($post_id) {
127 $count = self::get_statify_count($post_id);
128 if ($count >= 0) {
129 return $count;
130 } else {
131 return 0;
132 }
133 }
134
135 /**
136 * The shorctode for call amount of views per id.
137 *
138 * @since 1.1.4
139 */
140
141 public static function statify_count_shortcode( $atts ) {
142 global $post;
143
144 // Attr
145 $a = shortcode_atts( array(
146 'prefix' => '',
147 'suffix' => __('views','statify-widget')
148 ), $atts );
149
150 return $a['prefix'] . " " . self::statify_count($post->ID) . " " . $a['suffix'];
151 }
152
153 /**
154 * The shortcode for call all amount of views.
155 *
156 * @since 1.2
157 */
158
159 public static function statify_count_sum_shortcode( $atts ) {
160 global $post;
161
162 // Attr
163 $a = shortcode_atts( array(
164 'prefix' => '',
165 'suffix' => __('views','statify-widget')
166 ), $atts );
167
168 return $a['prefix'] . " " . self::statify_count_sum() . " " . $a['suffix'];
169 }
170
171 /**
172 * Return all targets from statify and saved the values for 4 minutes.
173 *
174 * @since 1.1
175 */
176
177 public static function get_all_targets($interval)
178 {
179 /* Look for cached values */
180 if ($data = get_transient('statify_targets_'.$interval)) {
181 return $data;
182 }
183
184 if ($interval > 0) {
185 $date = date("Y-m-d", strtotime('-' . $interval . ' days'));
186 }
187
188 global $wpdb;
189
190 if ($interval > 0) {
191 $data = $wpdb->get_results(
192 "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE `created` >= '$date' GROUP BY `target` ORDER BY `count` DESC",
193 ARRAY_A
194 );
195 } else {
196 $data = $wpdb->get_results(
197 "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` GROUP BY `target` ORDER BY `count` DESC",
198 ARRAY_A
199 );
200 }
201
202 /* Save values for 4 minutes */
203 set_transient(
204 'statify_targets_'.$interval, $data, 60 * 4 // = 4 minutes.
205 );
206
207 return $data;
208 }
209
210 }