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

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