PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / stats-export.php

stats-export.php in FV Player 8 trunk, at models/stats-export.php

81 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 if( !class_exists('FV_Player_Stats_Export') ) :
8
9 class FV_Player_Stats_Export {
10
11 public function __construct() {
12 if( isset( $_GET['fv-stats-export-user']) ) {
13 add_action('admin_init', array( $this, 'export_user_data' ) );
14 }
15 }
16
17 public function export_user_data() {
18 if( isset($_GET['nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['nonce'] ) ), 'fv-stats-export-user-' . intval($_GET['fv-stats-export-user']) ) ) {
19
20 if( !current_user_can('manage_options') ) return;
21
22 global $wpdb;
23
24 $user_id = intval($_GET['fv-stats-export-user']);
25 $date_range = sanitize_text_field($_GET['stats_range']);
26
27 $interval = FV_Player_Stats::get_interval_from_range($date_range);
28
29 $results = $wpdb->get_results(
30 $wpdb->prepare( "SELECT user_email, date, pl.id AS player_id, src, post_title, play, seconds, ROUND(duration)
31 FROM `{$wpdb->prefix}fv_player_stats` AS s
32 JOIN `{$wpdb->users}` AS u ON s.user_id = u.ID
33 JOIN `{$wpdb->posts}` AS p ON s.id_post = p.ID
34 JOIN `{$wpdb->prefix}fv_player_videos` AS v ON s.id_video = v.id
35 JOIN `{$wpdb->prefix}fv_player_players` AS pl ON FIND_IN_SET( v.id, pl.videos )
36 WHERE u.ID = %d AND date BETWEEN %s AND %s
37 ORDER BY s.id DESC",
38 $user_id,
39 $interval[0],
40 $interval[1]
41 ),
42 ARRAY_A
43 );
44
45 $this->serve_csv($results, $user_id);
46 }
47
48 }
49
50 private function serve_csv( $data, $user_id ) {
51 $user = get_user_by('id', $user_id);
52
53 $user_email = $user->user_email;
54
55 $header = array('User Email', 'Date', 'Player ID', 'Video URL', 'Video Title', 'Plays', 'Seconds Watched', 'Video Duration');
56 $filename = 'fv-player-stats-export-' . $user_email . '-' . gmdate('Y-m-d') . '.csv';
57
58 header("Content-type: text/csv");
59 header("Content-Disposition: attachment; filename=$filename");
60 header("Pragma: no-cache");
61 header("Expires: 0");
62
63 $fp = fopen('php://output', 'wb');
64 fputcsv($fp, $header);
65
66 foreach( $data as $row ) {
67 fputcsv($fp, $row);
68 }
69
70 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose
71 fclose($fp);
72 die();
73 }
74
75 }
76
77 global $FV_Player_Stats_Export;
78 $FV_Player_Stats_Export = new FV_Player_Stats_Export();
79
80 endif;
81