woocommerce/includes/admin/reports/class-wc-report-downloads.php

333 lines
10 KiB
PHP
Raw Normal View History

<?php
2017-11-13 16:24:33 +00:00
/**
* Download report.
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Admin/Reports
* @version 3.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
2017-11-13 16:24:33 +00:00
exit;
}
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* WC_Report_Downloads.
*/
class WC_Report_Downloads extends WP_List_Table {
/**
* Max items.
*
* @var int
*/
protected $max_items;
/**
* Constructor.
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'download',
'plural' => 'downloads',
'ajax' => false,
)
);
}
/**
* Don't need this.
*
2017-11-13 16:24:33 +00:00
* @param string $position Top or bottom.
*/
public function display_tablenav( $position ) {
if ( 'top' !== $position ) {
parent::display_tablenav( $position );
}
}
/**
* Output the report.
*/
public function output_report() {
$this->prepare_items();
// 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.
// Load the permission, order, etc. so we can render more information.
$permission = null;
$product = null;
2017-11-13 19:03:08 +00:00
try {
$permission = new WC_Customer_Download( $permission_id );
$product = wc_get_product( $permission->product_id );
} 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-11-13 19:03:08 +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 ) {
case 'order_id':
2017-11-13 19:03:08 +00:00
$order = wc_get_order( $value );
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;
case 'product_id':
2017-11-13 19:03:08 +00:00
$product = wc_get_product( $value );
if ( $product ) {
2017-11-13 19:03:08 +00:00
$display_value = $product->get_formatted_name();
} else {
break 2;
}
break;
default:
2017-11-13 19:03:08 +00:00
$display_value = $value;
break;
}
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">&times;</a>';
}
2017-11-13 19:03:08 +00:00
echo '</h1>';
echo '<div id="active-filters" class="woocommerce-reports-wide"><h2>';
2017-12-15 10:26:22 +00:00
echo esc_html__( 'Active filters', 'woocommerce' ) . ': ';
echo $filter_list ? wp_kses_post( implode( ', ', $filter_list ) ) : '';
echo '</h2></div>';
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.
*/
public function column_default( $item, $column_name ) {
$permission = null;
2017-11-13 19:03:08 +00:00
$product = null;
try {
$permission = new WC_Customer_Download( $item->permission_id );
$product = wc_get_product( $permission->product_id );
} catch ( Exception $e ) {
// Ok to continue rendering other information even if permission and/or product is not found.
2017-11-13 16:24:33 +00:00
return;
}
switch ( $column_name ) {
case 'timestamp':
echo esc_html( $item->timestamp );
break;
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;
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() );
echo esc_html( $file->get_name() . ' - ' . basename( $file->get_file() ) );
2017-11-13 19:03:08 +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>';
}
break;
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>';
}
break;
case 'user':
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-11-13 15:54:01 +00:00
} else {
esc_html_e( 'Guest', 'woocommerce' );
}
break;
case 'user_ip_address':
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>';
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' ),
);
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() );
// Allow filtering per_page value, but ensure it's at least 1.
$per_page = max( 1, apply_filters( 'woocommerce_admin_downloads_report_downloads_per_page', 20 ) );
$this->get_items( $current_page, $per_page );
/**
* Pagination.
*/
$this->set_pagination_args(
array(
'total_items' => $this->max_items,
'per_page' => $per_page,
'total_pages' => ceil( $this->max_items / $per_page ),
)
);
}
/**
* 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() {
$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,
);
}
/**
* 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.
*/
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();
// 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-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-11-13 19:03:08 +00:00
$query_from .= ' WHERE 1=1 ';
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-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-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-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.
}
}