PluginProbe
Statify Widget / 1.2.1
Statify Widget v1.2.1
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.1, at Statify_Posts.class.php

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