PluginProbe
Statify Widget / 1.2.7
Statify Widget v1.2.7
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.7, at Statify_Posts.class.php

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