class-wc-report-downloads.php 10 KB

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