/wp-content/plugins/woocommerce/includes/data-stores/class-wc-customer-download-log-data-store.php

https://gitlab.com/campus-academy/krowkaramel · PHP · 239 lines · 143 code · 37 blank · 59 comment · 12 complexity · f6e13ea8cd41aef9b3d40fe59dbd4e91 MD5 · raw file

  1. <?php
  2. /**
  3. * Class WC_Customer_Download_Log_Data_Store file.
  4. *
  5. * @version 3.3.0
  6. * @package WooCommerce\Classes
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * WC_Customer_Download_Log_Data_Store class.
  11. */
  12. class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Data_Store_Interface {
  13. // Table name for download logs.
  14. const WC_DOWNLOAD_LOG_TABLE = 'wc_download_log';
  15. /**
  16. * Get the table name for download logs.
  17. *
  18. * @return string
  19. */
  20. public static function get_table_name() {
  21. return self::WC_DOWNLOAD_LOG_TABLE;
  22. }
  23. /**
  24. * Create download log entry.
  25. *
  26. * @param WC_Customer_Download_Log $download_log Customer download log object.
  27. */
  28. public function create( WC_Customer_Download_Log &$download_log ) {
  29. global $wpdb;
  30. // Always set a timestamp.
  31. if ( is_null( $download_log->get_timestamp( 'edit' ) ) ) {
  32. $download_log->set_timestamp( time() );
  33. }
  34. $data = array(
  35. 'timestamp' => date( 'Y-m-d H:i:s', $download_log->get_timestamp( 'edit' )->getTimestamp() ),
  36. 'permission_id' => $download_log->get_permission_id( 'edit' ),
  37. 'user_id' => $download_log->get_user_id( 'edit' ),
  38. 'user_ip_address' => $download_log->get_user_ip_address( 'edit' ),
  39. );
  40. $format = array(
  41. '%s',
  42. '%s',
  43. '%s',
  44. '%s',
  45. );
  46. $result = $wpdb->insert(
  47. $wpdb->prefix . self::get_table_name(),
  48. apply_filters( 'woocommerce_downloadable_product_download_log_insert_data', $data ),
  49. apply_filters( 'woocommerce_downloadable_product_download_log_insert_format', $format, $data )
  50. );
  51. do_action( 'woocommerce_downloadable_product_download_log_insert', $data );
  52. if ( $result ) {
  53. $download_log->set_id( $wpdb->insert_id );
  54. $download_log->apply_changes();
  55. } else {
  56. wp_die( esc_html__( 'Unable to insert download log entry in database.', 'woocommerce' ) );
  57. }
  58. }
  59. /**
  60. * Method to read a download log from the database.
  61. *
  62. * @param WC_Customer_Download_Log $download_log Download log object.
  63. * @throws Exception Exception when read is not possible.
  64. */
  65. public function read( &$download_log ) {
  66. global $wpdb;
  67. $download_log->set_defaults();
  68. // Ensure we have an id to pull from the DB.
  69. if ( ! $download_log->get_id() ) {
  70. throw new Exception( __( 'Invalid download log: no ID.', 'woocommerce' ) );
  71. }
  72. $table = $wpdb->prefix . self::get_table_name();
  73. // Query the DB for the download log.
  74. $raw_download_log = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE download_log_id = %d", $download_log->get_id() ) ); // WPCS: unprepared SQL ok.
  75. if ( ! $raw_download_log ) {
  76. throw new Exception( __( 'Invalid download log: not found.', 'woocommerce' ) );
  77. }
  78. $download_log->set_props(
  79. array(
  80. 'timestamp' => strtotime( $raw_download_log->timestamp ),
  81. 'permission_id' => $raw_download_log->permission_id,
  82. 'user_id' => $raw_download_log->user_id,
  83. 'user_ip_address' => $raw_download_log->user_ip_address,
  84. )
  85. );
  86. $download_log->set_object_read( true );
  87. }
  88. /**
  89. * Method to update a download log in the database.
  90. *
  91. * @param WC_Customer_Download_Log $download_log Download log object.
  92. */
  93. public function update( &$download_log ) {
  94. global $wpdb;
  95. $data = array(
  96. 'timestamp' => date( 'Y-m-d H:i:s', $download_log->get_timestamp( 'edit' )->getTimestamp() ),
  97. 'permission_id' => $download_log->get_permission_id( 'edit' ),
  98. 'user_id' => $download_log->get_user_id( 'edit' ),
  99. 'user_ip_address' => $download_log->get_user_ip_address( 'edit' ),
  100. );
  101. $format = array(
  102. '%s',
  103. '%s',
  104. '%s',
  105. '%s',
  106. );
  107. $wpdb->update(
  108. $wpdb->prefix . self::get_table_name(),
  109. $data,
  110. array(
  111. 'download_log_id' => $download_log->get_id(),
  112. ),
  113. $format
  114. );
  115. $download_log->apply_changes();
  116. }
  117. /**
  118. * Get a download log object.
  119. *
  120. * @param array $data From the DB.
  121. * @return WC_Customer_Download_Log
  122. */
  123. private function get_download_log( $data ) {
  124. return new WC_Customer_Download_Log( $data );
  125. }
  126. /**
  127. * Get array of download log ids by specified args.
  128. *
  129. * @param array $args Arguments to define download logs to retrieve.
  130. * @return array
  131. */
  132. public function get_download_logs( $args = array() ) {
  133. global $wpdb;
  134. $args = wp_parse_args(
  135. $args,
  136. array(
  137. 'permission_id' => '',
  138. 'user_id' => '',
  139. 'user_ip_address' => '',
  140. 'orderby' => 'download_log_id',
  141. 'order' => 'ASC',
  142. 'limit' => -1,
  143. 'page' => 1,
  144. 'return' => 'objects',
  145. )
  146. );
  147. $query = array();
  148. $table = $wpdb->prefix . self::get_table_name();
  149. $query[] = "SELECT * FROM {$table} WHERE 1=1";
  150. if ( $args['permission_id'] ) {
  151. $query[] = $wpdb->prepare( 'AND permission_id = %d', $args['permission_id'] );
  152. }
  153. if ( $args['user_id'] ) {
  154. $query[] = $wpdb->prepare( 'AND user_id = %d', $args['user_id'] );
  155. }
  156. if ( $args['user_ip_address'] ) {
  157. $query[] = $wpdb->prepare( 'AND user_ip_address = %s', $args['user_ip_address'] );
  158. }
  159. $allowed_orders = array( 'download_log_id', 'timestamp', 'permission_id', 'user_id' );
  160. $orderby = in_array( $args['orderby'], $allowed_orders, true ) ? $args['orderby'] : 'download_log_id';
  161. $order = 'DESC' === strtoupper( $args['order'] ) ? 'DESC' : 'ASC';
  162. $orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" );
  163. $query[] = "ORDER BY {$orderby_sql}";
  164. if ( 0 < $args['limit'] ) {
  165. $query[] = $wpdb->prepare( 'LIMIT %d, %d', absint( $args['limit'] ) * absint( $args['page'] - 1 ), absint( $args['limit'] ) );
  166. }
  167. $raw_download_logs = $wpdb->get_results( implode( ' ', $query ) ); // WPCS: unprepared SQL ok.
  168. switch ( $args['return'] ) {
  169. case 'ids':
  170. return wp_list_pluck( $raw_download_logs, 'download_log_id' );
  171. default:
  172. return array_map( array( $this, 'get_download_log' ), $raw_download_logs );
  173. }
  174. }
  175. /**
  176. * Get download logs for a given download permission.
  177. *
  178. * @param int $permission_id Permission to get logs for.
  179. * @return array
  180. */
  181. public function get_download_logs_for_permission( $permission_id ) {
  182. // If no permission_id is passed, return an empty array.
  183. if ( empty( $permission_id ) ) {
  184. return array();
  185. }
  186. return $this->get_download_logs(
  187. array(
  188. 'permission_id' => $permission_id,
  189. )
  190. );
  191. }
  192. /**
  193. * Method to delete download logs for a given permission ID.
  194. *
  195. * @since 3.4.0
  196. * @param int $id download_id of the downloads that will be deleted.
  197. */
  198. public function delete_by_permission_id( $id ) {
  199. global $wpdb;
  200. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $id ) );
  201. }
  202. }