This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
/
wppa-statistics.php
221 lines (176 loc) · 6.31 KB
/
wppa-statistics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/* wppa-statistics.php
* Package: wp-photo-album-plus
*
* Functions for counts etc
* Common use front and admin
* Version 6.3.0
*
*/
if ( ! defined( 'ABSPATH' ) ) die( "Can't load this file directly" );
// show system statistics
function wppa_statistics() {
global $wppa;
$wppa['out'] .= wppa_get_statistics();
}
function wppa_get_statistics() {
$count = wppa_get_total_album_count();
$y_id = wppa_get_youngest_album_id();
$y_name = __(wppa_get_album_name($y_id), 'wp-photo-album-plus');
$p_id = wppa_get_parentalbumid($y_id);
$p_name = __(wppa_get_album_name($p_id), 'wp-photo-album-plus');
$result = '<div class="wppa-box wppa-nav" style="text-align: center; '.__wcs('wppa-box').__wcs('wppa-nav').'">';
$result .= __('There are', 'wp-photo-album-plus').' '.$count.' '.__('photo albums. The last album added is', 'wp-photo-album-plus').' ';
$result .= '<a href="'.wppa_get_permalink().'wppa-album='.$y_id.'&wppa-cover=0&wppa-occur=1">'.$y_name.'</a>';
if ($p_id > '0') {
$result .= __(', a subalbum of', 'wp-photo-album-plus').' ';
$result .= '<a href="'.wppa_get_permalink().'wppa-album='.$p_id.'&wppa-cover=0&wppa-occur=1">'.$p_name.'</a>';
}
$result .= '.</div>';
return $result;
}
// get number of photos in album
function wppa_get_photo_count( $id = '0', $use_treecounts = false ) {
global $wpdb;
if ( $use_treecounts ) {
$treecounts = wppa_treecount_a( $id );
if ( current_user_can('wppa_moderate') ) {
$count = $treecounts['selfphotos'] + $treecounts['pendphotos'] + $treecounts['scheduledphotos'];
}
else {
$count = $treecounts['selfphotos'];
}
}
elseif ( ! $id ) {
if ( current_user_can('wppa_moderate') ) {
$count = $wpdb->get_var( "SELECT COUNT(*) FROM `".WPPA_PHOTOS."` " );
}
else {
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM `".WPPA_PHOTOS."` WHERE ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s )", wppa_get_user() ) );
}
}
else {
if ( current_user_can('wppa_moderate') ) {
$count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM " . WPPA_PHOTOS . " WHERE album = %s", $id ) );
}
else {
$count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM " . WPPA_PHOTOS .
" WHERE `album` = %s AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR owner = %s )",
$id, wppa_get_user() ) );
}
wppa_dbg_q('Q-gpc');
}
// Substract private photos if not logged in and album given
if ( $id && ! is_user_logged_in() ) {
$count -= $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM `".WPPA_PHOTOS."` WHERE `album` = %s AND `status` = 'private' ", $id ) );
}
return $count;
}
// get number of albums in album
function wppa_get_album_count( $id, $use_treecounts = false ) {
global $wpdb;
if ( $use_treecounts ) {
$treecounts = wppa_treecount_a( $id );
$count = $treecounts['selfalbums'];
}
else {
$count = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*) FROM " . WPPA_ALBUMS . " WHERE a_parent=%s", $id ) );
wppa_dbg_q('Q-gac');
}
return $count;
}
// get number of albums in system
function wppa_get_total_album_count() {
global $wpdb;
$count = $wpdb->get_var("SELECT COUNT(*) FROM `".WPPA_ALBUMS."`");
wppa_dbg_q('Q-gtac');
return $count;
}
// get youngest photo id
function wppa_get_youngest_photo_id() {
global $wpdb;
$result = $wpdb->get_var(
"SELECT `id` FROM `" . WPPA_PHOTOS .
"` WHERE `status` <> 'pending' AND `status` <> 'scheduled' ORDER BY `id` DESC LIMIT 1" );
wppa_dbg_q('Q-gypi');
return $result;
}
// get n youngest photo ids
function wppa_get_youngest_photo_ids( $n = '3' ) {
global $wpdb;
if ( ! wppa_is_int( $n ) ) $n = '3';
$result = $wpdb->get_col(
"SELECT `id` FROM `" . WPPA_PHOTOS .
"` WHERE `status` <> 'pending' AND `status` <> 'scheduled' ORDER BY `timestamp` DESC LIMIT ".$n );
wppa_dbg_q('Q-gypin');
return $result;
}
// get youngest album id
function wppa_get_youngest_album_id() {
global $wpdb;
$result = $wpdb->get_var( "SELECT `id` FROM `" . WPPA_ALBUMS . "` ORDER BY `timestamp` DESC LIMIT 1" );
wppa_dbg_q('Q16');
return $result;
}
// get youngest album name
function wppa_get_youngest_album_name() {
global $wpdb;
$result = $wpdb->get_var( "SELECT `name` FROM `" . WPPA_ALBUMS . "` ORDER BY `id` DESC LIMIT 1" );
wppa_dbg_q('Q17');
return stripslashes($result);
}
// Bump Viewcount
function wppa_bump_viewcount($type, $id) {
global $wpdb;
global $wppa_session;
if ( ! wppa_switch('wppa_track_viewcounts') ) return;
if ( $type != 'album' && $type != 'photo' ) die ( 'Illegal $type in wppa_bump_viewcount: '.$type);
if ( ! is_numeric($id) ) return; //die ( 'Illegal $id in wppa_bump_viewcount: '.$id);
if ( ! $id ) return; // Not a wppa image
if ( ! isset($wppa_session[$type]) ) $wppa_session[$type] = array();
if ( ! isset($wppa_session[$type][$id] ) ) { // This one not done yest
$wppa_session[$type][$id] = true; // Mark as viewed
if ( $type == 'album' ) $table = WPPA_ALBUMS; else $table = WPPA_PHOTOS;
$count = $wpdb->get_var("SELECT `views` FROM `".$table."` WHERE `id` = ".$id);
$count++;
$wpdb->query("UPDATE `".$table."` SET `views` = ".$count." WHERE `id` = ".$id);
wppa_dbg_msg('Bumped viewcount for '.$type.' '.$id.' to '.$count, 'red');
}
}
function wppa_get_upldr_cache() {
$result = get_option( 'wppa_upldr_cache', array() );
return $result;
}
function wppa_flush_upldr_cache( $key = '', $id = '' ) {
$upldrcache = wppa_get_upldr_cache();
foreach ( array_keys( $upldrcache ) as $widget_id ) {
switch ( $key ) {
case 'widgetid':
if ( $id == $widget_id ) {
unset ( $upldrcache[$widget_id] );
}
case 'photoid':
$usr = wppa_get_photo_item( $id, 'owner');
if ( isset ( $upldrcache[$widget_id][$usr] ) ) {
unset ( $upldrcache[$widget_id][$usr] );
}
break;
case 'username':
$usr = $id;
if ( isset ( $upldrcache[$widget_id][$usr] ) ) {
unset ( $upldrcache[$widget_id][$usr] );
}
break;
case 'all':
$upldrcache = array();
break;
default:
wppa_dbg_msg('Missing key in wppa_flush_upldr_cache()', 'red');
break;
}
}
update_option('wppa_upldr_cache', $upldrcache);
}