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

172 lines 4.1 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 /**
6 * Gibt die Inhalte zurück
7 *
8 * @since 1.0
9 * @change 1.1
10 */
11
12 public static function get_posts($post_type, $amount, $interval) {
13 $posts = array();
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 // Falls noch leer und Artikel als Startseite
28 if (empty($page) && ($post_type == 'page' || $post_type == 'postpage')) {
29 $posts[0]['title'] = "Startseite";
30 $posts[0]['url'] = home_url("/");
31 if ($posts[0]['visits'] == NULL) $posts[0]['visits'] = 0;
32 $posts[0]['visits'] += $entry['count'];
33 $counter++;
34 }
35
36 if ($post_type == "postpage") {
37 if ($page->post_type == 'post' || $page->post_type == 'page') {
38 $posts[$page->ID]['title'] = $page->post_title;
39 $posts[$page->ID]['url'] = home_url($clear_url);
40 if ($posts[$page->ID]['visits'] == NULL) $posts[$page->ID]['visits'] = 0;
41 $posts[$page->ID]['visits'] += $entry['count'];
42 $counter++;
43 }
44 } else if($page->post_type == $post_type) {
45 $posts[$page->ID]['title'] = $page->post_title;
46 $posts[$page->ID]['url'] = home_url($clear_url);
47 if ($posts[$page->ID]['visits'] == NULL) $posts[$page->ID]['visits'] = 0;
48 $posts[$page->ID]['visits'] += $entry['count'];
49 $counter++;
50 }
51
52 if ($counter >= $amount) break;
53
54 }
55 usort($posts, array("Statify_Posts", "visitSort"));
56 return $posts;
57 }
58
59 /**
60 * Sortiert nach Aufurfen
61 *
62 * @since 1.0
63 */
64
65 private static function visitSort($a, $b) {
66 if ($a==$b) return 0;
67 return ($a['visits']>$b['visits'])?-1:1;
68 }
69
70 /**
71 * Gibt Anzahl Aufrufe eines Beitrags wieder
72 *
73 * @since 1.1.4
74 */
75
76 public static function get_statify_count($post_id) {
77 if (empty($post_id)) {
78 global $post;
79 $post_id = $post->ID;
80 }
81
82 $wpurl = parse_url(get_bloginfo('wpurl'));
83 $targets = self::get_all_targets(0);
84 $count = -1;
85
86 foreach ($targets as $entry) {
87 $clear_url = str_replace($wpurl['path'],"",$entry['url']);
88 $id = url_to_postid(home_url( $clear_url ));
89
90 if ($id == $post_id) {
91 $count = $entry['count'];
92 break;
93 }
94 }
95
96 return $count;
97 }
98
99 /**
100 * Gibt Anzahl Aufrufe eines Beitrags wieder
101 *
102 * @since 1.1.4
103 */
104
105 public static function statify_count($post_id) {
106 $count = self::get_statify_count($post_id);
107 if ($count >= 0) {
108 echo self::get_statify_count($post_id) . " Aufrufe.";
109 } else {
110 echo "Noch keine Aufrufe.";
111 }
112 }
113
114 /**
115 * Gibt Anzahl Aufrufe eines Beitrags mit Hilfe eines Shortcodes wieder
116 *
117 * @since 1.1.4
118 */
119
120 public static function statify_count_shortcode( $atts ) {
121 global $post;
122
123 // Attr
124 $a = shortcode_atts( array(
125 'prefix' => '',
126 'suffix' => 'Aufurfe'
127 ), $atts );
128
129 return $a['prefix'] . " " . self::statify_count($post->ID) . " " . $a['suffix'];
130 }
131
132 /**
133 * Gibt alle Ziele zurück und cachet diese.
134 *
135 * @since 1.1
136 */
137
138 public static function get_all_targets($interval)
139 {
140 /* Auf Cache zugreifen */
141 if ($data = get_transient('statify_targets_'.$interval)) {
142 return $data;
143 }
144
145 if ($interval > 0) {
146 $date = date("Y-m-d", strtotime('-' . $interval . ' days'));
147 }
148
149 global $wpdb;
150
151 if ($interval > 0) {
152 $data = $wpdb->get_results(
153 "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` WHERE `created` >= '$date' GROUP BY `target` ORDER BY `count` DESC",
154 ARRAY_A
155 );
156 } else {
157 $data = $wpdb->get_results(
158 "SELECT COUNT(`target`) as `count`, `target` as `url` FROM `$wpdb->statify` GROUP BY `target` ORDER BY `count` DESC",
159 ARRAY_A
160 );
161 }
162
163 /* Merken */
164 set_transient(
165 'statify_targets_'.$interval, $data, 60 * 4 // = 4 Minuten
166 );
167
168 return $data;
169 }
170
171 }
172