class-wc-admin-report.php
1 year ago
class-wc-report-coupon-usage.php
5 years ago
class-wc-report-customer-list.php
9 months ago
class-wc-report-customers.php
9 months ago
class-wc-report-downloads.php
5 years ago
class-wc-report-low-in-stock.php
5 years ago
class-wc-report-most-stocked.php
5 years ago
class-wc-report-out-of-stock.php
5 years ago
class-wc-report-sales-by-category.php
2 years ago
class-wc-report-sales-by-date.php
1 year ago
class-wc-report-sales-by-product.php
3 months ago
class-wc-report-stock.php
1 year ago
class-wc-report-taxes-by-code.php
1 year ago
class-wc-report-taxes-by-date.php
1 year ago
class-wc-report-downloads.php
337 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Download report. |
| 4 | * |
| 5 | * @author WooThemes |
| 6 | * @category Admin |
| 7 | * @package WooCommerce\Admin\Reports |
| 8 | * @version 3.3.0 |
| 9 | */ |
| 10 | |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 16 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * WC_Report_Downloads. |
| 21 | */ |
| 22 | class WC_Report_Downloads extends WP_List_Table { |
| 23 | |
| 24 | /** |
| 25 | * Max items. |
| 26 | * |
| 27 | * @var int |
| 28 | */ |
| 29 | protected $max_items; |
| 30 | |
| 31 | /** |
| 32 | * Constructor. |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | |
| 36 | parent::__construct( |
| 37 | array( |
| 38 | 'singular' => 'download', |
| 39 | 'plural' => 'downloads', |
| 40 | 'ajax' => false, |
| 41 | ) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Don't need this. |
| 47 | * |
| 48 | * @param string $position Top or bottom. |
| 49 | */ |
| 50 | public function display_tablenav( $position ) { |
| 51 | if ( 'top' !== $position ) { |
| 52 | parent::display_tablenav( $position ); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Output the report. |
| 58 | */ |
| 59 | public function output_report() { |
| 60 | |
| 61 | $this->prepare_items(); |
| 62 | |
| 63 | // Subtitle for permission if set. |
| 64 | if ( ! empty( $_GET['permission_id'] ) ) { // WPCS: input var ok. |
| 65 | $permission_id = absint( $_GET['permission_id'] ); // WPCS: input var ok. |
| 66 | |
| 67 | // Load the permission, order, etc. so we can render more information. |
| 68 | $permission = null; |
| 69 | $product = null; |
| 70 | |
| 71 | try { |
| 72 | $permission = new WC_Customer_Download( $permission_id ); |
| 73 | $product = wc_get_product( $permission->product_id ); |
| 74 | } catch ( Exception $e ) { |
| 75 | wp_die( sprintf( esc_html__( 'Permission #%d not found.', 'woocommerce' ), esc_html( $permission_id ) ) ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | echo '<h1>' . esc_html__( 'Customer downloads', 'woocommerce' ); |
| 80 | |
| 81 | $filters = $this->get_filter_vars(); |
| 82 | $filter_list = array(); |
| 83 | $filter_names = array( |
| 84 | 'product_id' => __( 'Product', 'woocommerce' ), |
| 85 | 'download_id' => __( 'File ID', 'woocommerce' ), |
| 86 | 'permission_id' => __( 'Permission ID', 'woocommerce' ), |
| 87 | 'order_id' => __( 'Order', 'woocommerce' ), |
| 88 | 'user_id' => __( 'User', 'woocommerce' ), |
| 89 | 'user_ip_address' => __( 'IP address', 'woocommerce' ), |
| 90 | ); |
| 91 | |
| 92 | foreach ( $filters as $key => $value ) { |
| 93 | if ( is_null( $value ) ) { |
| 94 | continue; |
| 95 | } |
| 96 | switch ( $key ) { |
| 97 | case 'order_id': |
| 98 | $order = wc_get_order( $value ); |
| 99 | if ( $order ) { |
| 100 | $display_value = _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number(); |
| 101 | } else { |
| 102 | break 2; |
| 103 | } |
| 104 | break; |
| 105 | case 'product_id': |
| 106 | $product = wc_get_product( $value ); |
| 107 | if ( $product ) { |
| 108 | $display_value = $product->get_formatted_name(); |
| 109 | } else { |
| 110 | break 2; |
| 111 | } |
| 112 | break; |
| 113 | default: |
| 114 | $display_value = $value; |
| 115 | break; |
| 116 | } |
| 117 | $filter_list[] = $filter_names[ $key ] . ' ' . $display_value . ' <a href="' . esc_url( remove_query_arg( $key ) ) . '" class="woocommerce-reports-remove-filter">×</a>'; |
| 118 | } |
| 119 | |
| 120 | echo '</h1>'; |
| 121 | |
| 122 | echo '<div id="active-filters" class="woocommerce-reports-wide"><h2>'; |
| 123 | echo esc_html__( 'Active filters', 'woocommerce' ) . ': '; |
| 124 | echo $filter_list ? wp_kses_post( implode( ', ', $filter_list ) ) : ''; |
| 125 | echo '</h2></div>'; |
| 126 | |
| 127 | echo '<div id="poststuff" class="woocommerce-reports-wide">'; |
| 128 | $this->display(); |
| 129 | echo '</div>'; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get column value. |
| 134 | * |
| 135 | * @param mixed $item Item being displayed. |
| 136 | * @param string $column_name Column name. |
| 137 | */ |
| 138 | public function column_default( $item, $column_name ) { |
| 139 | $permission = null; |
| 140 | $product = null; |
| 141 | try { |
| 142 | $permission = new WC_Customer_Download( $item->permission_id ); |
| 143 | $product = wc_get_product( $permission->product_id ); |
| 144 | } catch ( Exception $e ) { |
| 145 | // Ok to continue rendering other information even if permission and/or product is not found. |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | switch ( $column_name ) { |
| 150 | case 'timestamp': |
| 151 | echo esc_html( $item->timestamp ); |
| 152 | break; |
| 153 | case 'product': |
| 154 | if ( ! empty( $product ) ) { |
| 155 | edit_post_link( esc_html( $product->get_formatted_name() ), '', '', $product->get_id(), 'view-link' ); |
| 156 | |
| 157 | echo '<div class="row-actions">'; |
| 158 | echo '<a href="' . esc_url( add_query_arg( 'product_id', $product->get_id() ) ) . '">' . esc_html__( 'Filter by product', 'woocommerce' ) . '</a>'; |
| 159 | echo '</div>'; |
| 160 | } |
| 161 | break; |
| 162 | case 'file': |
| 163 | if ( ! empty( $permission ) && ! empty( $product ) ) { |
| 164 | // File information. |
| 165 | $file = $product->get_file( $permission->get_download_id() ); |
| 166 | |
| 167 | if ( false === $file ) { |
| 168 | echo esc_html__( 'File does not exist', 'woocommerce' ); |
| 169 | } else { |
| 170 | echo esc_html( $file->get_name() . ' - ' . basename( $file->get_file() ) ); |
| 171 | |
| 172 | echo '<div class="row-actions">'; |
| 173 | echo '<a href="' . esc_url( add_query_arg( 'download_id', $permission->get_download_id() ) ) . '">' . esc_html__( 'Filter by file', 'woocommerce' ) . '</a>'; |
| 174 | echo '</div>'; |
| 175 | } |
| 176 | } |
| 177 | break; |
| 178 | case 'order': |
| 179 | if ( ! empty( $permission ) && ( $order = wc_get_order( $permission->order_id ) ) ) { |
| 180 | edit_post_link( esc_html( _x( '#', 'hash before order number', 'woocommerce' ) . $order->get_order_number() ), '', '', $permission->order_id, 'view-link' ); |
| 181 | |
| 182 | echo '<div class="row-actions">'; |
| 183 | echo '<a href="' . esc_url( add_query_arg( 'order_id', $order->get_id() ) ) . '">' . esc_html__( 'Filter by order', 'woocommerce' ) . '</a>'; |
| 184 | echo '</div>'; |
| 185 | } |
| 186 | break; |
| 187 | case 'user': |
| 188 | if ( $item->user_id > 0 ) { |
| 189 | $user = get_user_by( 'id', $item->user_id ); |
| 190 | |
| 191 | if ( ! empty( $user ) ) { |
| 192 | echo '<a href="' . esc_url( get_edit_user_link( $item->user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>'; |
| 193 | echo '<div class="row-actions">'; |
| 194 | echo '<a href="' . esc_url( add_query_arg( 'user_id', $item->user_id ) ) . '">' . esc_html__( 'Filter by user', 'woocommerce' ) . '</a>'; |
| 195 | echo '</div>'; |
| 196 | } |
| 197 | } else { |
| 198 | esc_html_e( 'Guest', 'woocommerce' ); |
| 199 | } |
| 200 | break; |
| 201 | case 'user_ip_address': |
| 202 | echo esc_html( $item->user_ip_address ); |
| 203 | |
| 204 | echo '<div class="row-actions">'; |
| 205 | echo '<a href="' . esc_url( add_query_arg( 'user_ip_address', $item->user_ip_address ) ) . '">' . esc_html__( 'Filter by IP address', 'woocommerce' ) . '</a>'; |
| 206 | echo '</div>'; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get columns. |
| 213 | * |
| 214 | * @return array |
| 215 | */ |
| 216 | public function get_columns() { |
| 217 | $columns = array( |
| 218 | 'timestamp' => __( 'Timestamp', 'woocommerce' ), |
| 219 | 'product' => __( 'Product', 'woocommerce' ), |
| 220 | 'file' => __( 'File', 'woocommerce' ), |
| 221 | 'order' => __( 'Order', 'woocommerce' ), |
| 222 | 'user' => __( 'User', 'woocommerce' ), |
| 223 | 'user_ip_address' => __( 'IP address', 'woocommerce' ), |
| 224 | ); |
| 225 | |
| 226 | return $columns; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Prepare download list items. |
| 231 | */ |
| 232 | public function prepare_items() { |
| 233 | |
| 234 | $this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() ); |
| 235 | $current_page = absint( $this->get_pagenum() ); |
| 236 | // Allow filtering per_page value, but ensure it's at least 1. |
| 237 | $per_page = max( 1, apply_filters( 'woocommerce_admin_downloads_report_downloads_per_page', 20 ) ); |
| 238 | |
| 239 | $this->get_items( $current_page, $per_page ); |
| 240 | |
| 241 | /** |
| 242 | * Pagination. |
| 243 | */ |
| 244 | $this->set_pagination_args( |
| 245 | array( |
| 246 | 'total_items' => $this->max_items, |
| 247 | 'per_page' => $per_page, |
| 248 | 'total_pages' => ceil( $this->max_items / $per_page ), |
| 249 | ) |
| 250 | ); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * No items found text. |
| 255 | */ |
| 256 | public function no_items() { |
| 257 | esc_html_e( 'No customer downloads found.', 'woocommerce' ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get filters from querystring. |
| 262 | * |
| 263 | * @return object |
| 264 | */ |
| 265 | protected function get_filter_vars() { |
| 266 | $product_id = ! empty( $_GET['product_id'] ) ? absint( wp_unslash( $_GET['product_id'] ) ) : null; // WPCS: input var ok. |
| 267 | $download_id = ! empty( $_GET['download_id'] ) ? wc_clean( wp_unslash( $_GET['download_id'] ) ) : null; // WPCS: input var ok. |
| 268 | $permission_id = ! empty( $_GET['permission_id'] ) ? absint( wp_unslash( $_GET['permission_id'] ) ) : null; // WPCS: input var ok. |
| 269 | $order_id = ! empty( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : null; // WPCS: input var ok. |
| 270 | $user_id = ! empty( $_GET['user_id'] ) ? absint( wp_unslash( $_GET['user_id'] ) ) : null; // WPCS: input var ok. |
| 271 | $user_ip_address = ! empty( $_GET['user_ip_address'] ) ? wc_clean( wp_unslash( $_GET['user_ip_address'] ) ) : null; // WPCS: input var ok. |
| 272 | |
| 273 | return (object) array( |
| 274 | 'product_id' => $product_id, |
| 275 | 'download_id' => $download_id, |
| 276 | 'permission_id' => $permission_id, |
| 277 | 'order_id' => $order_id, |
| 278 | 'user_id' => $user_id, |
| 279 | 'user_ip_address' => $user_ip_address, |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Get downloads matching criteria. |
| 285 | * |
| 286 | * @param int $current_page Current viewed page. |
| 287 | * @param int $per_page How many results to show per page. |
| 288 | */ |
| 289 | public function get_items( $current_page, $per_page ) { |
| 290 | global $wpdb; |
| 291 | |
| 292 | $this->max_items = 0; |
| 293 | $this->items = array(); |
| 294 | $filters = $this->get_filter_vars(); |
| 295 | |
| 296 | // Get downloads from database. |
| 297 | $table = $wpdb->prefix . WC_Customer_Download_Log_Data_Store::get_table_name(); |
| 298 | $query_from = " FROM {$table} as downloads "; |
| 299 | |
| 300 | if ( ! is_null( $filters->product_id ) || ! is_null( $filters->download_id ) || ! is_null( $filters->order_id ) ) { |
| 301 | $query_from .= " LEFT JOIN {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions on downloads.permission_id = permissions.permission_id "; |
| 302 | } |
| 303 | |
| 304 | $query_from .= ' WHERE 1=1 '; |
| 305 | |
| 306 | if ( ! is_null( $filters->product_id ) ) { |
| 307 | $query_from .= $wpdb->prepare( ' AND product_id = %d ', $filters->product_id ); |
| 308 | } |
| 309 | |
| 310 | if ( ! is_null( $filters->download_id ) ) { |
| 311 | $query_from .= $wpdb->prepare( ' AND download_id = %s ', $filters->download_id ); |
| 312 | } |
| 313 | |
| 314 | if ( ! is_null( $filters->order_id ) ) { |
| 315 | $query_from .= $wpdb->prepare( ' AND order_id = %d ', $filters->order_id ); |
| 316 | } |
| 317 | |
| 318 | if ( ! is_null( $filters->permission_id ) ) { |
| 319 | $query_from .= $wpdb->prepare( ' AND downloads.permission_id = %d ', $filters->permission_id ); |
| 320 | } |
| 321 | |
| 322 | if ( ! is_null( $filters->user_id ) ) { |
| 323 | $query_from .= $wpdb->prepare( ' AND downloads.user_id = %d ', $filters->user_id ); |
| 324 | } |
| 325 | |
| 326 | if ( ! is_null( $filters->user_ip_address ) ) { |
| 327 | $query_from .= $wpdb->prepare( ' AND user_ip_address = %s ', $filters->user_ip_address ); |
| 328 | } |
| 329 | |
| 330 | $query_from = apply_filters( 'woocommerce_report_downloads_query_from', $query_from ); |
| 331 | $query_order = $wpdb->prepare( 'ORDER BY timestamp DESC LIMIT %d, %d;', ( $current_page - 1 ) * $per_page, $per_page ); |
| 332 | |
| 333 | $this->items = $wpdb->get_results( "SELECT * {$query_from} {$query_order}" ); // WPCS: cache ok, db call ok, unprepared SQL ok. |
| 334 | $this->max_items = $wpdb->get_var( "SELECT COUNT( DISTINCT download_log_id ) {$query_from};" ); // WPCS: cache ok, db call ok, unprepared SQL ok. |
| 335 | } |
| 336 | } |
| 337 |