Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/paid-memberships-pro/adminpages/reports/logins.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/*
3
+
PMPro Report
4
+
Title: Logins
5
+
Slug: login
6
+
7
+
For each report, write three functions:
8
+
* pmpro_report_{slug}_register() to register the widget (slug and title).
9
+
* pmpro_report_{slug}_widget() to show up on the report homepage.
10
+
* pmpro_report_{slug}_page() to show up when users click on the report page widget.
11
+
*/
12
+
function pmpro_report_login_register( $pmpro_reports ) {
13
+
$pmpro_reports['login'] = __( 'Visits, Views, and Logins', 'paid-memberships-pro' );
14
+
15
+
return $pmpro_reports;
16
+
}
17
+
18
+
add_filter( 'pmpro_registered_reports', 'pmpro_report_login_register' );
19
+
20
+
function pmpro_report_login_widget() {
21
+
global $wpdb, $pmpro_reports;
22
+
$now = current_time('timestamp');
23
+
24
+
$visits = pmpro_reports_get_all_values('visits');
25
+
$views = pmpro_reports_get_all_values('views');
26
+
$logins = pmpro_reports_get_all_values('logins');
27
+
?>
28
+
<span id="pmpro_report_login" class="pmpro_report-holder">
29
+
<table class="wp-list-table widefat fixed striped">
30
+
<thead>
31
+
<tr>
32
+
<th scope="col"> </th>
33
+
<th scope="col"><?php esc_html_e('Visits','paid-memberships-pro'); ?></th>
34
+
<th scope="col"><?php esc_html_e('Views','paid-memberships-pro'); ?></th>
35
+
<th scope="col"><?php esc_html_e('Logins','paid-memberships-pro'); ?></th>
36
+
</tr>
37
+
</thead>
38
+
<tbody>
39
+
<tr>
40
+
<th scope="row"><?php esc_html_e('Today','paid-memberships-pro'); ?></th>
41
+
<td><?php echo esc_html( number_format_i18n($visits['today']) ); ?></td>
42
+
<td><?php echo esc_html( number_format_i18n($views['today']) ); ?></td>
43
+
<td><?php echo esc_html( number_format_i18n($logins['today'] ));?></td>
44
+
</tr>
45
+
<tr>
46
+
<th scope="row"><?php esc_html_e('This Week','paid-memberships-pro'); ?></th>
47
+
<td><?php echo esc_html( number_format_i18n($visits['week']) ); ?></td>
48
+
<td><?php echo esc_html( number_format_i18n($views['week']) ); ?></td>
49
+
<td><?php echo esc_html( number_format_i18n($logins['week']) ); ?></td>
50
+
</tr>
51
+
<tr>
52
+
<th scope="row"><?php esc_html_e('This Month','paid-memberships-pro'); ?></th>
53
+
<td><?php echo esc_html( number_format_i18n($visits['month']) ); ?></td>
54
+
<td><?php echo esc_html( number_format_i18n($views['month']) ); ?></td>
55
+
<td><?php echo esc_html( number_format_i18n($logins['month']) ); ?></td>
56
+
</tr>
57
+
<tr>
58
+
<th scope="row"><?php esc_html_e('Year to Date','paid-memberships-pro'); ?></th>
59
+
<td><?php echo esc_html( number_format_i18n($visits['ytd']) ); ?></td>
60
+
<td><?php echo esc_html( number_format_i18n($views['ytd']) ); ?></td>
61
+
<td><?php echo esc_html( number_format_i18n($logins['ytd']) );?></td>
62
+
</tr>
63
+
<tr>
64
+
<th scope="row"><?php esc_html_e('All Time','paid-memberships-pro'); ?></th>
65
+
<td><?php echo esc_html( number_format_i18n($visits['alltime']) ); ?></td>
66
+
<td><?php echo esc_html( number_format_i18n($views['alltime']) );?></td>
67
+
<td><?php echo esc_html( number_format_i18n($logins['alltime']) ); ?></td>
68
+
</tr>
69
+
</tbody>
70
+
</table>
71
+
<?php if ( function_exists( 'pmpro_report_login_page' ) ) { ?>
72
+
<p class="pmpro_report-button">
73
+
<a class="button button-primary" href="<?php echo esc_url( admin_url( 'admin.php?page=pmpro-reports&report=login' ) ); ?>" aria-label="<?php echo esc_attr( sprintf( __( 'View the full %s report', 'paid-memberships-pro' ), $pmpro_reports['login'] ) ); ?>"><?php esc_html_e('Details', 'paid-memberships-pro' );?></a>
74
+
</p>
75
+
<?php } ?>
76
+
</span>
77
+
<?php
78
+
}
79
+
80
+
function pmpro_report_login_page()
81
+
{
82
+
global $wpdb;
83
+
84
+
//vars
85
+
if(!empty($_REQUEST['s']))
86
+
$s = sanitize_text_field( $_REQUEST['s'] );
87
+
else
88
+
$s = "";
89
+
90
+
if(!empty($_REQUEST['l'])) {
91
+
if($_REQUEST['l'] == 'all')
92
+
$l = 'all';
93
+
else
94
+
$l = intval($_REQUEST['l']);
95
+
} else {
96
+
$l = "";
97
+
}
98
+
99
+
// Build CSV export link.
100
+
$csv_export_link = admin_url( 'admin-ajax.php' ) . '?action=login_report_csv';
101
+
if ( ! empty( $s ) ) {
102
+
$csv_export_link = add_query_arg( 's', $s, $csv_export_link );
103
+
}
104
+
if ( ! empty( $l ) ) {
105
+
$csv_export_link = add_query_arg( 'l', $l, $csv_export_link );
106
+
}
107
+
?>
108
+
<form id="visits-views-logins-form" method="get" action="">
109
+
<h1 class="wp-heading-inline">
110
+
<?php esc_html_e('Visits, Views, and Logins', 'paid-memberships-pro');?>
111
+
</h1>
112
+
<?php if ( current_user_can( 'pmpro_loginscsv' ) ) { ?>
113
+
<a target="_blank" href="<?php echo esc_url( $csv_export_link ); ?>" class="page-title-action pmpro-has-icon pmpro-has-icon-download"><?php esc_html_e( 'Export to CSV', 'paid-memberships-pro' ); ?></a>
114
+
<?php } ?>
115
+
<p class="search-box">
116
+
<label class="screen-reader-text" for="post-search-input"><?php echo esc_html_x( 'Search', 'Search form label', 'paid-memberships-pro')?> <?php if(empty($l)) esc_html_e( 'Users', 'paid-memberships-pro' ); else esc_html_e( 'Members', 'paid-memberships-pro' );?>:</label>
117
+
<input type="hidden" name="page" value="pmpro-reports" />
118
+
<input type="hidden" name="report" value="login" />
119
+
<input id="post-search-input" type="text" value="<?php echo esc_attr($s)?>" name="s"/>
120
+
<input class="button" type="submit" value="<?php esc_attr_e( 'Search', 'paid-memberships-pro' ) ?>"/>
121
+
</p>
122
+
<?php
123
+
//some vars for the search
124
+
if(isset($_REQUEST['pn']))
125
+
$pn = intval($_REQUEST['pn']);
126
+
else
127
+
$pn = 1;
128
+
129
+
if(isset($_REQUEST['limit']))
130
+
$limit = intval($_REQUEST['limit']);
131
+
else
132
+
$limit = 15;
133
+
134
+
$end = $pn * $limit;
135
+
$start = $end - $limit;
136
+
137
+
if($s)
138
+
{
139
+
$sqlQuery = "SELECT SQL_CALC_FOUND_ROWS u.ID, u.user_login, u.user_email, UNIX_TIMESTAMP(CONVERT_TZ(u.user_registered, '+00:00', @@global.time_zone)) as joindate, mu.membership_id, mu.initial_payment, mu.billing_amount, mu.cycle_period, mu.cycle_number, mu.billing_limit, mu.trial_amount, mu.trial_limit, UNIX_TIMESTAMP(CONVERT_TZ(mu.startdate, '+00:00', @@global.time_zone)) as startdate, UNIX_TIMESTAMP(CONVERT_TZ(mu.enddate, '+00:00', @@global.time_zone)) as enddate, m.name as membership FROM $wpdb->users u LEFT JOIN $wpdb->usermeta um ON u.ID = um.user_id LEFT JOIN $wpdb->pmpro_memberships_users mu ON u.ID = mu.user_id AND mu.status = 'active' LEFT JOIN $wpdb->pmpro_membership_levels m ON mu.membership_id = m.id WHERE (u.user_login LIKE '%" . esc_sql($s) . "%' OR u.user_email LIKE '%" . esc_sql($s) . "%' OR um.meta_value LIKE '%" . esc_sql($s) . "%') ";
140
+
141
+
if($l == "all")
142
+
$sqlQuery .= " AND mu.status = 'active' AND mu.membership_id > 0 ";
143
+
elseif($l)
144
+
$sqlQuery .= " AND mu.membership_id = '" . esc_sql($l) . "' ";
145
+
146
+
$sqlQuery .= "GROUP BY u.ID ORDER BY user_registered DESC LIMIT " . (int) $start . "," . (int) $limit;
147
+
}
148
+
else
149
+
{
150
+
$sqlQuery = "SELECT SQL_CALC_FOUND_ROWS u.ID, u.user_login, u.user_email, UNIX_TIMESTAMP(CONVERT_TZ(u.user_registered, '+00:00', @@global.time_zone)) as joindate, mu.membership_id, mu.initial_payment, mu.billing_amount, mu.cycle_period, mu.cycle_number, mu.billing_limit, mu.trial_amount, mu.trial_limit, UNIX_TIMESTAMP(CONVERT_TZ(mu.startdate, '+00:00', @@global.time_zone)) as startdate, UNIX_TIMESTAMP(CONVERT_TZ(mu.enddate, '+00:00', @@global.time_zone)) as enddate, m.name as membership FROM $wpdb->users u LEFT JOIN $wpdb->pmpro_memberships_users mu ON u.ID = mu.user_id AND mu.status = 'active' LEFT JOIN $wpdb->pmpro_membership_levels m ON mu.membership_id = m.id";
151
+
$sqlQuery .= " WHERE 1=1 ";
152
+
153
+
if($l == "all")
154
+
$sqlQuery .= " AND mu.membership_id > 0 AND mu.status = 'active' ";
155
+
elseif($l)
156
+
$sqlQuery .= " AND mu.membership_id = '" . esc_sql($l) . "' ";
157
+
$sqlQuery .= "GROUP BY u.ID ORDER BY user_registered DESC LIMIT " . (int) $start . "," . (int) $limit;
158
+
}
159
+
160
+
$sqlQuery = apply_filters("pmpro_members_list_sql", $sqlQuery);
161
+
162
+
$theusers = $wpdb->get_results($sqlQuery);
163
+
$totalrows = $wpdb->get_var("SELECT FOUND_ROWS() as found_rows");
164
+
?>
165
+
<p>
166
+
<?php esc_html_e( 'This report offers a detailed view of data points by user and member. For various reasons, the numbers below will not perfectly match up to other tracking you might be doing (such as the data provided by an analytics plugin).', 'paid-memberships-pro' ); ?>
167
+
<a href="https://www.paidmembershipspro.com/documentation/admin/reports/?utm_source=plugin&utm_medium=pmpro-reports&utm_campaign=documentation&utm_content=visits-views-logins' ); ?>" target="_blank" rel="nofollow noopener"><?php esc_html_e( 'Click here to learn how these numbers are calculated.', 'paid-memberships-pro' ); ?></a>
168
+
</p>
169
+
<div class="pmpro_report-filters">
170
+
<h3><?php esc_html_e( 'Customize Report', 'paid-memberships-pro'); ?></h3>
171
+
<div class="tablenav top">
172
+
<label for="l" class="pmpro_report-filter-text"><?php echo esc_html_x( 'Show', 'Dropdown label, e.g. Show All Users', 'paid-memberships-pro' ); ?></label>
173
+
<select id="l" name="l" onchange="jQuery('#visits-views-logins-form').trigger('submit');" aria-label="<?php esc_attr_e( 'Select a membership level to customize this report', 'paid-memberships-pro' ); ?>">
174
+
<option value="" <?php if(!$l) { ?>selected="selected"<?php } ?>><?php esc_html_e('All Users', 'paid-memberships-pro')?></option>
175
+
<option value="all" <?php if($l == "all") { ?>selected="selected"<?php } ?>><?php esc_html_e('All Levels', 'paid-memberships-pro')?></option>
176
+
<?php
177
+
$levels = $wpdb->get_results("SELECT id, name FROM $wpdb->pmpro_membership_levels ORDER BY name");
178
+
$levels = pmpro_sort_levels_by_order( $levels );
179
+
foreach($levels as $level)
180
+
{
181
+
?>
182
+
<option value="<?php echo esc_attr( $level->id ) ?>" <?php if($l == $level->id) { ?>selected="selected"<?php } ?>><?php echo esc_html( $level->name ); ?></option>
183
+
184
+
<?php
185
+
}
186
+
?>
187
+
</select>
188
+
</div> <!-- end tablenav -->
189
+
</div> <!-- end pmpro_report-filters -->
190
+
<?php if ( $theusers ) { ?>
191
+
<div class="tablenav top">
192
+
<div class="tablenav-pages one-page">
193
+
<span class="displaying-num"><?php echo esc_html( strval($totalrows) ); ?> <?php if(empty($l)) echo "users"; else echo "members";?> found.</span>
194
+
</div>
195
+
<br class="clear" />
196
+
</div> <!-- end tablenav -->
197
+
<?php } ?>
198
+
<table id="pmpro_report_login_data" class="widefat striped">
199
+
<thead>
200
+
<tr>
201
+
<th colspan="4"></th>
202
+
<th colspan="5"><?php esc_html_e( 'Visits', 'paid-memberships-pro' ); ?></th>
203
+
<th colspan="4"><?php esc_html_e( 'Views', 'paid-memberships-pro' ); ?></th>
204
+
<th colspan="5"><?php esc_html_e( 'Logins', 'paid-memberships-pro' ); ?></th>
205
+
</tr>
206
+
<tr class="thead-sub">
207
+
<th><?php esc_html_e( 'User', 'paid-memberships-pro' ); ?></th>
208
+
<th><?php esc_html_e( 'Level', 'paid-memberships-pro' ); ?></th>
209
+
<th><?php esc_html_e( 'Joined', 'paid-memberships-pro' ); ?></th>
210
+
<th><?php esc_html_e( 'Expires', 'paid-memberships-pro' ); ?></th>
211
+
<th><?php esc_html_e( 'Last', 'paid-memberships-pro' ); ?></th>
212
+
<th><?php esc_html_e( 'This Week', 'paid-memberships-pro' ); ?></th>
213
+
<th><?php esc_html_e( 'This Month', 'paid-memberships-pro' ); ?></th>
214
+
<th><?php esc_html_e( 'This Year', 'paid-memberships-pro' ); ?></th>
215
+
<th><?php esc_html_e( 'All Time', 'paid-memberships-pro' ); ?></th>
216
+
<th><?php esc_html_e( 'This Week', 'paid-memberships-pro' ); ?></th>
217
+
<th><?php esc_html_e( 'This Month', 'paid-memberships-pro' ); ?></th>
218
+
<th><?php esc_html_e( 'This Year', 'paid-memberships-pro' ); ?></th>
219
+
<th><?php esc_html_e( 'All Time', 'paid-memberships-pro' ); ?></th>
220
+
<th><?php esc_html_e( 'Last', 'paid-memberships-pro' ); ?></th>
221
+
<th><?php esc_html_e( 'Week', 'paid-memberships-pro' ); ?></th>
222
+
<th><?php esc_html_e( 'This Month', 'paid-memberships-pro' ); ?></th>
223
+
<th><?php esc_html_e( 'This Year', 'paid-memberships-pro' ); ?></th>
224
+
<th><?php esc_html_e( 'All Time', 'paid-memberships-pro' ); ?></th>
225
+
</tr>
226
+
</thead>
227
+
<tbody id="users">
228
+
<?php
229
+
foreach($theusers as $auser)
230
+
{
231
+
//get meta
232
+
$theuser = get_userdata($auser->ID);
233
+
$visits = pmpro_reports_get_values_for_user("visits", $auser->ID);
234
+
$views = pmpro_reports_get_values_for_user("views", $auser->ID);
235
+
$logins = pmpro_reports_get_values_for_user("logins", $auser->ID);
236
+
?>
237
+
<tr>
238
+
<td class="user column-username has-row-actions column-primary">
239
+
<?php echo get_avatar($theuser->ID, 32)?>
240
+
<strong>
241
+
<?php
242
+
$userlink_escaped = '<a href="' . esc_url( add_query_arg( array( 'page' => 'pmpro-member', 'user_id' => (int)$theuser->ID ), admin_url( 'admin.php' ) ) ) . '">' . esc_html( $theuser->display_name ) . '</a>';
243
+
$userlink_escaped = apply_filters("pmpro_members_list_user_link", $userlink_escaped, $theuser);
244
+
echo $userlink_escaped; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
245
+
?>
246
+
</strong>
247
+
<div class="row-actions">
248
+
<?php
249
+
printf(
250
+
// translators: %s is the User ID.
251
+
esc_html__( 'ID: %s', 'paid-memberships-pro' ),
252
+
esc_attr( $theuser->ID )
253
+
);
254
+
?>
255
+
</div>
256
+
</td>
257
+
<td><?php echo esc_html( $auser->membership );?></td>
258
+
<td><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( get_date_from_gmt( $theuser->user_registered ), current_time( 'timestamp' ) ) ) ); ?></td>
259
+
<td>
260
+
<?php
261
+
if($auser->enddate)
262
+
echo esc_html( date_i18n(get_option('date_format'), $auser->enddate) );
263
+
else
264
+
esc_html_e( 'Never', 'paid-memberships-pro' );
265
+
?>
266
+
</td>
267
+
<td><?php if(!empty($visits['last'])) echo esc_html( $visits['last'] );?></td>
268
+
<td><?php if(!empty($visits['week']) && pmpro_isDateThisWeek($visits['last'])) echo esc_html( $visits['week'] );?></td>
269
+
<td><?php if(!empty($visits['month']) && pmpro_isDateThisMonth($visits['last'])) echo esc_html( $visits['month'] );?></td>
270
+
<td><?php if(!empty($visits['ytd']) && pmpro_isDateThisYear($visits['last'])) echo esc_html( $visits['ytd'] );?></td>
271
+
<td><?php if(!empty($visits['alltime'])) echo esc_html( $visits['alltime'] );?></td>
272
+
<td><?php if(!empty($views['week']) && pmpro_isDateThisWeek($views['last'])) echo esc_html( $views['week'] );?></td>
273
+
<td><?php if(!empty($views['month']) && pmpro_isDateThisMonth($views['last'])) echo esc_html( $views['month'] );?></td>
274
+
<td><?php if(!empty($views['ytd']) && pmpro_isDateThisYear($views['last'])) echo esc_html( $views['ytd'] );?></td>
275
+
<td><?php if(!empty($views['alltime'])) echo esc_html( $views['alltime'] );?></td>
276
+
<td><?php if(!empty($logins['last'])) echo esc_html( $logins['last'] );?></td>
277
+
<td><?php if(!empty($logins['week']) && pmpro_isDateThisWeek($logins['last'])) echo esc_html( $logins['week'] );?></td>
278
+
<td><?php if(!empty($logins['month']) && pmpro_isDateThisMonth($logins['last'])) echo esc_html( $logins['month'] );?></td>
279
+
<td><?php if(!empty($logins['ytd']) && pmpro_isDateThisYear($logins['last'])) echo esc_html( $logins['ytd'] );?></td>
280
+
<td><?php if(!empty($logins['alltime'])) echo esc_html( $logins['alltime'] );?></td>
281
+
</tr>
282
+
<?php
283
+
}
284
+
285
+
if(!$theusers)
286
+
{
287
+
?>
288
+
<tr>
289
+
<td colspan="18"><p><?php esc_html_e( 'No members found.', 'paid-memberships-pro' ); ?> <?php if($l) { ?><a href="?page=pmpro-memberslist&s=<?php echo esc_attr($s)?>"><?php esc_html_e( 'Search all levels', 'paid-memberships-pro' ); ?></a>.<?php } ?></p></td>
290
+
</tr>
291
+
<?php
292
+
}
293
+
?>
294
+
</tbody>
295
+
</table>
296
+
</form>
297
+
<div class="tablenav bottom">
298
+
<div class="tablenav-pages">
299
+
<?php
300
+
echo wp_kses_post( pmpro_getPaginationString($pn, $totalrows, $limit, 1, admin_url( "admin.php?page=pmpro-reports&report=login&s=" . urlencode($s)), "&l=$l&limit=$limit&pn=", __( 'Logins Report Pagination', 'paid-memberships-pro' ) ) );
301
+
?>
302
+
</div>
303
+
</div>
304
+
<?php
305
+
}
306
+
307
+
/*
308
+
Other code required for your reports. This file is loaded every time WP loads with PMPro enabled.
309
+
*/
310
+
//get values for a user
311
+
function pmpro_reports_get_values_for_user($type, $user_id = NULL) {
312
+
// Default to current user.
313
+
if ( empty( $user_id )) {
314
+
global $current_user;
315
+
$user_id = $current_user->ID;
316
+
}
317
+
318
+
// Need a type and user.
319
+
if ( empty( $type ) || empty( $user_id ) ) {
320
+
return false;
321
+
}
322
+
323
+
// Get values from user meta.
324
+
$values = get_user_meta($user_id, "pmpro_" . $type, true);
325
+
326
+
// Set up empty $values array if it is not set or empty.
327
+
if ( ! is_array( $values ) || empty( $values ) ) {
328
+
$values = array(
329
+
"last" => "N/A",
330
+
"thisdate" => NULL,
331
+
"week" => 0,
332
+
"thisweek" => NULL,
333
+
"month" => 0,
334
+
"thismonth" => NULL,
335
+
"ytd" => 0,
336
+
"thisyear" => NULL,
337
+
"alltime" => 0
338
+
);
339
+
} else {
340
+
// Check if we should reset any of the values.
341
+
$now = current_time('timestamp');
342
+
$thisdate = date("Y-d-m", $now);
343
+
$thisweek = date("W", $now);
344
+
$thismonth = date("n", $now);
345
+
$thisyear = date("Y", $now);
346
+
347
+
if ( ! isset( $values['thisdate'] ) || $thisdate != $values['thisdate'] ) {
348
+
$values['today'] = 0;
349
+
$values['thisdate'] = $thisdate;
350
+
$update = true;
351
+
}
352
+
353
+
if ( ! isset($values['thisweek']) || $thisweek != $values['thisweek'] ) {
354
+
$values['week'] = 0;
355
+
$values['thisweek'] = $thisweek;
356
+
$update = true;
357
+
}
358
+
359
+
if ( ! isset($values['thismonth']) || $thismonth != $values['thismonth'] ) {
360
+
$values['month'] = 0;
361
+
$values['thismonth'] = $thismonth;
362
+
$update = true;
363
+
}
364
+
365
+
if ( ! isset($values['thisyear']) || $thisyear != $values['thisyear'] ) {
366
+
$values['ytd'] = 0;
367
+
$values['thisyear'] = $thisyear;
368
+
$update = true;
369
+
}
370
+
}
371
+
372
+
// Update user meta if changes were made.
373
+
if ( ! empty( $update ) ) {
374
+
update_user_meta( $user_id, 'pmpro_' . $type, $values );
375
+
}
376
+
377
+
return $values;
378
+
}
379
+
380
+
//get all values
381
+
function pmpro_reports_get_all_values($type) {
382
+
//need a type and user
383
+
if(empty($type))
384
+
return false;
385
+
386
+
$allvalues = get_option("pmpro_" . $type);
387
+
if(empty($allvalues))
388
+
$allvalues = array("today"=>0, "thisdate"=>NULL, "week"=>0, "thisweek"=>NULL, "month"=>0, "thismonth"=> NULL, "ytd"=>0, "thisyear"=>NULL, "alltime"=>0);
389
+
else
390
+
{
391
+
//check if we should reset any of the values
392
+
$now = current_time('timestamp');
393
+
$thisdate = date("Y-d-m", $now);
394
+
$thisweek = date("W", $now);
395
+
$thismonth = date("n", $now);
396
+
$thisyear = date("Y", $now);
397
+
398
+
if(!isset($allvalues['thisdate']) || $thisdate != $allvalues['thisdate'])
399
+
{
400
+
$allvalues['today'] = 0;
401
+
$allvalues['thisdate'] = $thisdate;
402
+
$update = true;
403
+
}
404
+
405
+
if(!isset($allvalues['thisweek']) || $thisweek != $allvalues['thisweek'])
406
+
{
407
+
$allvalues['week'] = 0;
408
+
$allvalues['thisweek'] = $thisweek;
409
+
$update = true;
410
+
}
411
+
412
+
if(!isset($allvalues['thismonth']) || $thismonth != $allvalues['thismonth'])
413
+
{
414
+
$allvalues['month'] = 0;
415
+
$allvalues['thismonth'] = $thismonth;
416
+
$update = true;
417
+
}
418
+
419
+
if(!isset($allvalues['thisyear']) || $thisyear != $allvalues['thisyear'])
420
+
{
421
+
$allvalues['ytd'] = 0;
422
+
$allvalues['thisyear'] = $thisyear;
423
+
$update = true;
424
+
}
425
+
426
+
if(!empty($update))
427
+
update_option('pmpro_' . $type, $allvalues);
428
+
}
429
+
430
+
return $allvalues;
431
+
}
432
+
433
+
//track visits, views, and logins and save to user meta
434
+
function pmpro_report_track_values($type, $user_id = NULL) {
435
+
//don't track admin
436
+
if(is_admin())
437
+
return false;
438
+
439
+
//default to current user
440
+
if(empty($user_id))
441
+
{
442
+
global $current_user;
443
+
$user_id = $current_user->ID;
444
+
}
445
+
446
+
//need a type
447
+
if(empty($type))
448
+
return false;
449
+
450
+
//check for cookie for visits
451
+
if( $type === 'visits' && !empty( $_COOKIE['pmpro_visit'] ) ) {
452
+
return false;
453
+
}
454
+
455
+
//set cookie for visits
456
+
if( $type === 'visits' && empty( $_COOKIE['pmpro_visit'] ) ) {
457
+
// The secure parameter is set to is_ssl(), true if HTTPS.
458
+
setcookie( 'pmpro_visit', '1', 0, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
459
+
}
460
+
461
+
//some vars for below
462
+
$now = current_time('timestamp');
463
+
$thisdate = date("Y-d-m", $now);
464
+
$thisweek = date("W", $now);
465
+
$thismonth = date("n", $now);
466
+
$thisyear = date("Y", $now);
467
+
468
+
//track user stats if we have one
469
+
if(!empty($user_id))
470
+
{
471
+
//get values
472
+
$values = pmpro_reports_get_values_for_user($type, $user_id);
473
+
474
+
if($values !== false)
475
+
{
476
+
//track for user
477
+
$values['last'] = date(get_option("date_format"), $now);
478
+
$values['alltime'] = $values['alltime'] + 1;
479
+
480
+
if($thisweek == $values['thisweek'])
481
+
$values['week'] = $values['week'] + 1;
482
+
else
483
+
{
484
+
$values['week'] = 1;
485
+
$values['thisweek'] = $thisweek;
486
+
}
487
+
488
+
if($thismonth == $values['thismonth'])
489
+
$values['month'] = $values['month'] + 1;
490
+
else
491
+
{
492
+
$values['month'] = 1;
493
+
$values['thismonth'] = $thismonth;
494
+
}
495
+
496
+
if($thisyear == $values['thisyear'])
497
+
$values['ytd'] = $values['ytd'] + 1;
498
+
else
499
+
{
500
+
$values['ytd'] = 1;
501
+
$values['thisyear'] = $thisyear;
502
+
}
503
+
504
+
//update user data
505
+
update_user_meta($user_id, "pmpro_" . $type, $values);
506
+
}
507
+
}
508
+
509
+
//track cumulative stats
510
+
$allvalues = pmpro_reports_get_all_values($type);
511
+
512
+
$allvalues['alltime'] = $allvalues['alltime'] + 1;
513
+
514
+
if($thisdate == $allvalues['thisdate'])
515
+
$allvalues['today'] = $allvalues['today'] + 1;
516
+
else
517
+
{
518
+
$allvalues['today'] = 1;
519
+
$allvalues['thisdate'] = $thisdate;
520
+
}
521
+
522
+
if($thisweek == $allvalues['thisweek'])
523
+
$allvalues['week'] = $allvalues['week'] + 1;
524
+
else
525
+
{
526
+
$allvalues['week'] = 1;
527
+
$allvalues['thisweek'] = $thisweek;
528
+
}
529
+
530
+
if($thismonth == $allvalues['thismonth'])
531
+
$allvalues['month'] = $allvalues['month'] + 1;
532
+
else
533
+
{
534
+
$allvalues['month'] = 1;
535
+
$allvalues['thismonth'] = $thismonth;
536
+
}
537
+
538
+
if($thisyear == $allvalues['thisyear'])
539
+
$allvalues['ytd'] = $allvalues['ytd'] + 1;
540
+
else
541
+
{
542
+
$allvalues['ytd'] = 1;
543
+
$allvalues['thisyear'] = $thisyear;
544
+
}
545
+
546
+
update_option('pmpro_' . $type, $allvalues);
547
+
}
548
+
549
+
//track visits
550
+
function pmpro_report_login_wp_visits() {
551
+
pmpro_report_track_values("visits");
552
+
}
553
+
add_action("wp", "pmpro_report_login_wp_visits");
554
+
555
+
//we want to clear the pmpro_visit cookie on login/logout
556
+
function pmpro_report_login_clear_visit_cookie() {
557
+
if(isset($_COOKIE['pmpro_visit']))
558
+
unset($_COOKIE['pmpro_visit']);
559
+
}
560
+
add_action("wp_login", "pmpro_report_login_clear_visit_cookie");
561
+
add_action("wp_logout", "pmpro_report_login_clear_visit_cookie");
562
+
563
+
//track views
564
+
function pmpro_report_login_wp_views() {
565
+
pmpro_report_track_values("views");
566
+
}
567
+
add_action("wp_head", "pmpro_report_login_wp_views");
568
+
569
+
//track logins
570
+
function pmpro_report_login_wp_login($user_login, $user) {
571
+
pmpro_report_track_values("logins", $user->ID);
572
+
}
573
+
add_action("wp_login", "pmpro_report_login_wp_login", 10 ,2);