2017-08-03 21:24:53 +00:00
< ? php
2017-11-13 16:24:33 +00:00
/**
* Download report .
*
* @ author WooThemes
* @ category Admin
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Admin\Reports
2017-11-13 16:24:33 +00:00
* @ version 3.3 . 0
*/
2017-08-03 21:24:53 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2017-11-13 16:24:33 +00:00
exit ;
2017-08-03 21:24:53 +00:00
}
if ( ! class_exists ( 'WP_List_Table' ) ) {
2018-03-05 18:59:17 +00:00
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ;
2017-08-03 21:24:53 +00:00
}
/**
* WC_Report_Downloads .
*/
class WC_Report_Downloads extends WP_List_Table {
/**
* Max items .
*
* @ var int
*/
protected $max_items ;
/**
* Constructor .
*/
public function __construct () {
2018-03-05 18:59:17 +00:00
parent :: __construct (
array (
'singular' => 'download' ,
'plural' => 'downloads' ,
'ajax' => false ,
)
);
2017-08-03 21:24:53 +00:00
}
/**
* Don ' t need this .
*
2017-11-13 16:24:33 +00:00
* @ param string $position Top or bottom .
2017-08-03 21:24:53 +00:00
*/
public function display_tablenav ( $position ) {
if ( 'top' !== $position ) {
parent :: display_tablenav ( $position );
}
}
/**
* Output the report .
*/
public function output_report () {
$this -> prepare_items ();
2017-08-23 03:10:14 +00:00
// Subtitle for permission if set.
2017-11-13 16:24:33 +00:00
if ( ! empty ( $_GET [ 'permission_id' ] ) ) { // WPCS: input var ok.
2017-11-13 15:58:22 +00:00
$permission_id = absint ( $_GET [ 'permission_id' ] ); // WPCS: input var ok.
2017-08-23 03:10:14 +00:00
// Load the permission, order, etc. so we can render more information.
2018-03-05 18:59:17 +00:00
$permission = null ;
$product = null ;
2017-11-13 19:03:08 +00:00
2017-08-03 21:24:53 +00:00
try {
2018-03-05 18:59:17 +00:00
$permission = new WC_Customer_Download ( $permission_id );
$product = wc_get_product ( $permission -> product_id );
2017-08-03 21:24:53 +00:00
} catch ( Exception $e ) {
2017-11-13 16:24:33 +00:00
wp_die ( sprintf ( esc_html__ ( 'Permission #%d not found.' , 'woocommerce' ), esc_html ( $permission_id ) ) );
2017-08-03 21:24:53 +00:00
}
2017-11-13 19:03:08 +00:00
}
2017-08-03 21:24:53 +00:00
2017-11-13 19:03:08 +00:00
echo '<h1>' . esc_html__ ( 'Customer downloads' , 'woocommerce' );
$filters = $this -> get_filter_vars ();
$filter_list = array ();
$filter_names = array (
'product_id' => __ ( 'Product' , 'woocommerce' ),
'download_id' => __ ( 'File ID' , 'woocommerce' ),
'permission_id' => __ ( 'Permission ID' , 'woocommerce' ),
'order_id' => __ ( 'Order' , 'woocommerce' ),
'user_id' => __ ( 'User' , 'woocommerce' ),
'user_ip_address' => __ ( 'IP address' , 'woocommerce' ),
);
2017-11-13 15:36:22 +00:00
2017-11-13 19:03:08 +00:00
foreach ( $filters as $key => $value ) {
if ( is_null ( $value ) ) {
continue ;
}
switch ( $key ) {
2018-03-05 18:59:17 +00:00
case 'order_id' :
2017-11-13 19:03:08 +00:00
$order = wc_get_order ( $value );
2018-03-05 18:59:17 +00:00
if ( $order ) {
2017-11-13 19:03:08 +00:00
$display_value = _x ( '#' , 'hash before order number' , 'woocommerce' ) . $order -> get_order_number ();
} else {
break 2 ;
}
break ;
2018-03-05 18:59:17 +00:00
case 'product_id' :
2017-11-13 19:03:08 +00:00
$product = wc_get_product ( $value );
2018-03-05 18:59:17 +00:00
if ( $product ) {
2017-11-13 19:03:08 +00:00
$display_value = $product -> get_formatted_name ();
} else {
break 2 ;
}
break ;
2018-03-05 18:59:17 +00:00
default :
2017-11-13 19:03:08 +00:00
$display_value = $value ;
break ;
2017-08-03 21:24:53 +00:00
}
2017-11-13 19:03:08 +00:00
$filter_list [] = $filter_names [ $key ] . ' ' . $display_value . ' <a href="' . esc_url ( remove_query_arg ( $key ) ) . '" class="woocommerce-reports-remove-filter">×</a>' ;
2017-08-03 21:24:53 +00:00
}
2017-11-13 19:03:08 +00:00
echo '</h1>' ;
2017-12-14 06:13:53 +00:00
echo '<div id="active-filters" class="woocommerce-reports-wide"><h2>' ;
2017-12-15 10:26:22 +00:00
echo esc_html__ ( 'Active filters' , 'woocommerce' ) . ': ' ;
2017-12-14 06:13:53 +00:00
echo $filter_list ? wp_kses_post ( implode ( ', ' , $filter_list ) ) : '' ;
echo '</h2></div>' ;
2017-08-03 21:24:53 +00:00
echo '<div id="poststuff" class="woocommerce-reports-wide">' ;
$this -> display ();
echo '</div>' ;
}
/**
* Get column value .
*
2017-11-13 16:24:33 +00:00
* @ param mixed $item Item being displayed .
* @ param string $column_name Column name .
2017-08-03 21:24:53 +00:00
*/
public function column_default ( $item , $column_name ) {
$permission = null ;
2017-11-13 19:03:08 +00:00
$product = null ;
2017-08-03 21:24:53 +00:00
try {
2018-03-05 18:59:17 +00:00
$permission = new WC_Customer_Download ( $item -> permission_id );
$product = wc_get_product ( $permission -> product_id );
2017-08-03 21:24:53 +00:00
} catch ( Exception $e ) {
2017-08-23 03:10:14 +00:00
// Ok to continue rendering other information even if permission and/or product is not found.
2017-11-13 16:24:33 +00:00
return ;
2017-08-03 21:24:53 +00:00
}
switch ( $column_name ) {
2018-03-05 18:59:17 +00:00
case 'timestamp' :
2017-08-03 21:24:53 +00:00
echo esc_html ( $item -> timestamp );
break ;
2018-03-05 18:59:17 +00:00
case 'product' :
2017-11-13 15:58:22 +00:00
if ( ! empty ( $product ) ) {
2017-11-13 19:03:08 +00:00
edit_post_link ( esc_html ( $product -> get_formatted_name () ), '' , '' , $product -> get_id (), 'view-link' );
echo '<div class="row-actions">' ;
echo '<a href="' . esc_url ( add_query_arg ( 'product_id' , $product -> get_id () ) ) . '">' . esc_html__ ( 'Filter by product' , 'woocommerce' ) . '</a>' ;
echo '</div>' ;
}
break ;
2018-03-05 18:59:17 +00:00
case 'file' :
2017-11-13 19:03:08 +00:00
if ( ! empty ( $permission ) && ! empty ( $product ) ) {
// File information.
$file = $product -> get_file ( $permission -> get_download_id () );
2019-07-10 09:22:59 +00:00
if ( false === $file ) {
echo esc_html__ ( 'File does not exist' , 'woocommerce' );
2019-07-10 07:28:07 +00:00
} else {
echo esc_html ( $file -> get_name () . ' - ' . basename ( $file -> get_file () ) );
2017-11-13 19:03:08 +00:00
2019-07-10 07:28:07 +00:00
echo '<div class="row-actions">' ;
echo '<a href="' . esc_url ( add_query_arg ( 'download_id' , $permission -> get_download_id () ) ) . '">' . esc_html__ ( 'Filter by file' , 'woocommerce' ) . '</a>' ;
echo '</div>' ;
}
2017-08-03 21:24:53 +00:00
}
break ;
2018-03-05 18:59:17 +00:00
case 'order' :
2017-11-13 19:03:08 +00:00
if ( ! empty ( $permission ) && ( $order = wc_get_order ( $permission -> order_id ) ) ) {
edit_post_link ( esc_html ( _x ( '#' , 'hash before order number' , 'woocommerce' ) . $order -> get_order_number () ), '' , '' , $permission -> order_id , 'view-link' );
echo '<div class="row-actions">' ;
echo '<a href="' . esc_url ( add_query_arg ( 'order_id' , $order -> get_id () ) ) . '">' . esc_html__ ( 'Filter by order' , 'woocommerce' ) . '</a>' ;
echo '</div>' ;
2017-08-03 21:24:53 +00:00
}
break ;
2018-03-05 18:59:17 +00:00
case 'user' :
2017-08-03 21:24:53 +00:00
if ( $item -> user_id > 0 ) {
$user = get_user_by ( 'id' , $item -> user_id );
if ( ! empty ( $user ) ) {
2017-11-13 19:03:08 +00:00
echo '<a href="' . esc_url ( get_edit_user_link ( $item -> user_id ) ) . '">' . esc_html ( $user -> display_name ) . '</a>' ;
echo '<div class="row-actions">' ;
echo '<a href="' . esc_url ( add_query_arg ( 'user_id' , $item -> user_id ) ) . '">' . esc_html__ ( 'Filter by user' , 'woocommerce' ) . '</a>' ;
echo '</div>' ;
2017-08-03 21:24:53 +00:00
}
2017-11-13 15:54:01 +00:00
} else {
esc_html_e ( 'Guest' , 'woocommerce' );
2017-08-03 21:24:53 +00:00
}
break ;
2018-03-05 18:59:17 +00:00
case 'user_ip_address' :
2017-08-03 21:24:53 +00:00
echo esc_html ( $item -> user_ip_address );
2017-11-13 19:03:08 +00:00
echo '<div class="row-actions">' ;
echo '<a href="' . esc_url ( add_query_arg ( 'user_ip_address' , $item -> user_ip_address ) ) . '">' . esc_html__ ( 'Filter by IP address' , 'woocommerce' ) . '</a>' ;
echo '</div>' ;
2017-08-03 21:24:53 +00:00
break ;
}
}
/**
* Get columns .
*
* @ return array
*/
public function get_columns () {
$columns = array (
2017-11-13 19:03:08 +00:00
'timestamp' => __ ( 'Timestamp' , 'woocommerce' ),
'product' => __ ( 'Product' , 'woocommerce' ),
'file' => __ ( 'File' , 'woocommerce' ),
'order' => __ ( 'Order' , 'woocommerce' ),
'user' => __ ( 'User' , 'woocommerce' ),
'user_ip_address' => __ ( 'IP address' , 'woocommerce' ),
2017-08-03 21:24:53 +00:00
);
return $columns ;
}
/**
* Prepare download list items .
*/
public function prepare_items () {
$this -> _column_headers = array ( $this -> get_columns (), array (), $this -> get_sortable_columns () );
$current_page = absint ( $this -> get_pagenum () );
2017-08-23 03:10:14 +00:00
// Allow filtering per_page value, but ensure it's at least 1.
2018-03-05 18:59:17 +00:00
$per_page = max ( 1 , apply_filters ( 'woocommerce_admin_downloads_report_downloads_per_page' , 20 ) );
2017-08-03 21:24:53 +00:00
$this -> get_items ( $current_page , $per_page );
/**
* Pagination .
*/
2018-03-05 18:59:17 +00:00
$this -> set_pagination_args (
array (
'total_items' => $this -> max_items ,
'per_page' => $per_page ,
'total_pages' => ceil ( $this -> max_items / $per_page ),
)
);
2017-08-03 21:24:53 +00:00
}
/**
* No items found text .
*/
public function no_items () {
2017-11-13 19:03:08 +00:00
esc_html_e ( 'No customer downloads found.' , 'woocommerce' );
}
/**
* Get filters from querystring .
*
* @ return object
*/
protected function get_filter_vars () {
2018-03-05 18:59:17 +00:00
$product_id = ! empty ( $_GET [ 'product_id' ] ) ? absint ( wp_unslash ( $_GET [ 'product_id' ] ) ) : null ; // WPCS: input var ok.
$download_id = ! empty ( $_GET [ 'download_id' ] ) ? wc_clean ( wp_unslash ( $_GET [ 'download_id' ] ) ) : null ; // WPCS: input var ok.
$permission_id = ! empty ( $_GET [ 'permission_id' ] ) ? absint ( wp_unslash ( $_GET [ 'permission_id' ] ) ) : null ; // WPCS: input var ok.
$order_id = ! empty ( $_GET [ 'order_id' ] ) ? absint ( wp_unslash ( $_GET [ 'order_id' ] ) ) : null ; // WPCS: input var ok.
$user_id = ! empty ( $_GET [ 'user_id' ] ) ? absint ( wp_unslash ( $_GET [ 'user_id' ] ) ) : null ; // WPCS: input var ok.
$user_ip_address = ! empty ( $_GET [ 'user_ip_address' ] ) ? wc_clean ( wp_unslash ( $_GET [ 'user_ip_address' ] ) ) : null ; // WPCS: input var ok.
2017-11-13 19:03:08 +00:00
return ( object ) array (
'product_id' => $product_id ,
'download_id' => $download_id ,
'permission_id' => $permission_id ,
'order_id' => $order_id ,
'user_id' => $user_id ,
'user_ip_address' => $user_ip_address ,
);
2017-08-03 21:24:53 +00:00
}
/**
* Get downloads matching criteria .
*
2017-11-13 16:24:33 +00:00
* @ param int $current_page Current viewed page .
* @ param int $per_page How many results to show per page .
2017-08-03 21:24:53 +00:00
*/
public function get_items ( $current_page , $per_page ) {
global $wpdb ;
$this -> max_items = 0 ;
$this -> items = array ();
2017-11-13 19:03:08 +00:00
$filters = $this -> get_filter_vars ();
2017-08-03 21:24:53 +00:00
2017-08-23 03:10:14 +00:00
// Get downloads from database.
2017-11-13 19:03:08 +00:00
$table = $wpdb -> prefix . WC_Customer_Download_Log_Data_Store :: get_table_name ();
$query_from = " FROM { $table } as downloads " ;
2017-08-03 21:24:53 +00:00
2017-11-13 19:03:08 +00:00
if ( ! is_null ( $filters -> product_id ) || ! is_null ( $filters -> download_id ) || ! is_null ( $filters -> order_id ) ) {
$query_from .= " LEFT JOIN { $wpdb -> prefix } woocommerce_downloadable_product_permissions as permissions on downloads.permission_id = permissions.permission_id " ;
}
2017-08-03 21:24:53 +00:00
2017-11-13 19:03:08 +00:00
$query_from .= ' WHERE 1=1 ' ;
2017-08-03 21:24:53 +00:00
2017-11-13 19:03:08 +00:00
if ( ! is_null ( $filters -> product_id ) ) {
$query_from .= $wpdb -> prepare ( ' AND product_id = %d ' , $filters -> product_id );
}
if ( ! is_null ( $filters -> download_id ) ) {
$query_from .= $wpdb -> prepare ( ' AND download_id = %s ' , $filters -> download_id );
}
if ( ! is_null ( $filters -> order_id ) ) {
$query_from .= $wpdb -> prepare ( ' AND order_id = %d ' , $filters -> order_id );
}
if ( ! is_null ( $filters -> permission_id ) ) {
$query_from .= $wpdb -> prepare ( ' AND downloads.permission_id = %d ' , $filters -> permission_id );
}
if ( ! is_null ( $filters -> user_id ) ) {
$query_from .= $wpdb -> prepare ( ' AND downloads.user_id = %d ' , $filters -> user_id );
}
2017-08-03 21:24:53 +00:00
2017-11-13 19:03:08 +00:00
if ( ! is_null ( $filters -> user_ip_address ) ) {
$query_from .= $wpdb -> prepare ( ' AND user_ip_address = %s ' , $filters -> user_ip_address );
2017-08-03 21:24:53 +00:00
}
2017-11-13 16:24:33 +00:00
$query_from = apply_filters ( 'woocommerce_report_downloads_query_from' , $query_from );
$query_order = $wpdb -> prepare ( 'ORDER BY timestamp DESC LIMIT %d, %d;' , ( $current_page - 1 ) * $per_page , $per_page );
2017-08-03 21:24:53 +00:00
2017-11-13 16:24:33 +00:00
$this -> items = $wpdb -> get_results ( " SELECT * { $query_from } { $query_order } " ); // WPCS: cache ok, db call ok, unprepared SQL ok.
$this -> max_items = $wpdb -> get_var ( " SELECT COUNT( DISTINCT download_log_id ) { $query_from } ; " ); // WPCS: cache ok, db call ok, unprepared SQL ok.
2017-08-03 21:24:53 +00:00
}
}