2017-07-30 22:38:17 +00:00
< ? php
/**
2018-03-08 19:28:05 +00:00
* Class WC_Customer_Download_Log_Data_Store file .
2017-07-30 22:38:17 +00:00
*
* @ version 3.3 . 0
2018-04-13 17:03:02 +00:00
* @ package WooCommerce\Classes
*/
defined ( 'ABSPATH' ) || exit ;
/**
* WC_Customer_Download_Log_Data_Store class .
2017-07-30 22:38:17 +00:00
*/
class WC_Customer_Download_Log_Data_Store implements WC_Customer_Download_Log_Data_Store_Interface {
2017-08-23 03:10:14 +00:00
// Table name for download logs.
const WC_DOWNLOAD_LOG_TABLE = 'wc_download_log' ;
/**
* Get the table name for download logs .
*
* @ return string
*/
public static function get_table_name () {
return self :: WC_DOWNLOAD_LOG_TABLE ;
}
2017-07-30 22:38:17 +00:00
/**
* Create download log entry .
*
2018-03-08 19:28:05 +00:00
* @ param WC_Customer_Download_Log $download_log Customer download log object .
2017-07-30 22:38:17 +00:00
*/
2017-08-23 03:10:14 +00:00
public function create ( WC_Customer_Download_Log & $download_log ) {
2017-07-30 22:38:17 +00:00
global $wpdb ;
2017-08-23 03:10:14 +00:00
// Always set a timestamp.
2017-07-30 22:38:17 +00:00
if ( is_null ( $download_log -> get_timestamp ( 'edit' ) ) ) {
$download_log -> set_timestamp ( current_time ( 'timestamp' , true ) );
}
$data = array (
2018-04-13 17:03:02 +00:00
'timestamp' => date ( 'Y-m-d H:i:s' , $download_log -> get_timestamp ( 'edit' ) -> getTimestamp () ),
'permission_id' => $download_log -> get_permission_id ( 'edit' ),
'user_id' => $download_log -> get_user_id ( 'edit' ),
'user_ip_address' => $download_log -> get_user_ip_address ( 'edit' ),
2017-07-30 22:38:17 +00:00
);
$format = array (
'%s' ,
'%s' ,
'%s' ,
'%s' ,
);
$result = $wpdb -> insert (
2017-08-23 03:10:14 +00:00
$wpdb -> prefix . self :: get_table_name (),
apply_filters ( 'woocommerce_downloadable_product_download_log_insert_data' , $data ),
apply_filters ( 'woocommerce_downloadable_product_download_log_insert_format' , $format , $data )
2017-07-30 22:38:17 +00:00
);
2017-08-23 03:10:14 +00:00
do_action ( 'woocommerce_downloadable_product_download_log_insert' , $data );
2017-07-30 22:38:17 +00:00
if ( $result ) {
$download_log -> set_id ( $wpdb -> insert_id );
$download_log -> apply_changes ();
2018-04-13 17:03:02 +00:00
} else {
wp_die ( esc_html__ ( 'Unable to insert download log entry in database.' , 'woocommerce' ) );
2017-08-23 03:10:14 +00:00
}
2017-07-30 22:38:17 +00:00
}
/**
* Method to read a download log from the database .
*
2018-04-13 17:03:02 +00:00
* @ param WC_Customer_Download_Log $download_log Download log object .
* @ throws Exception Exception when read is not possible .
2017-07-30 22:38:17 +00:00
*/
public function read ( & $download_log ) {
global $wpdb ;
$download_log -> set_defaults ();
2017-08-23 03:10:14 +00:00
// Ensure we have an id to pull from the DB.
if ( ! $download_log -> get_id () ) {
throw new Exception ( __ ( 'Invalid download log: no ID.' , 'woocommerce' ) );
}
2018-04-13 17:03:02 +00:00
$table = $wpdb -> prefix . self :: get_table_name ();
2017-08-23 03:10:14 +00:00
// Query the DB for the download log.
2018-04-13 17:03:02 +00:00
$raw_download_log = $wpdb -> get_row ( $wpdb -> prepare ( " SELECT * FROM { $table } WHERE download_log_id = %d " , $download_log -> get_id () ) ); // WPCS: unprepared SQL ok.
2017-08-23 03:10:14 +00:00
if ( ! $raw_download_log ) {
throw new Exception ( __ ( 'Invalid download log: not found.' , 'woocommerce' ) );
2017-07-30 22:38:17 +00:00
}
2018-03-07 19:16:01 +00:00
$download_log -> set_props (
array (
'timestamp' => strtotime ( $raw_download_log -> timestamp ),
'permission_id' => $raw_download_log -> permission_id ,
'user_id' => $raw_download_log -> user_id ,
'user_ip_address' => $raw_download_log -> user_ip_address ,
)
);
2017-07-30 22:38:17 +00:00
$download_log -> set_object_read ( true );
}
/**
* Method to update a download log in the database .
*
2018-04-13 17:03:02 +00:00
* @ param WC_Customer_Download_Log $download_log Download log object .
2017-07-30 22:38:17 +00:00
*/
public function update ( & $download_log ) {
global $wpdb ;
$data = array (
2018-04-13 17:03:02 +00:00
'timestamp' => date ( 'Y-m-d H:i:s' , $download_log -> get_timestamp ( 'edit' ) -> getTimestamp () ),
'permission_id' => $download_log -> get_permission_id ( 'edit' ),
'user_id' => $download_log -> get_user_id ( 'edit' ),
'user_ip_address' => $download_log -> get_user_ip_address ( 'edit' ),
2017-07-30 22:38:17 +00:00
);
$format = array (
'%s' ,
'%s' ,
'%s' ,
'%s' ,
);
$wpdb -> update (
2017-08-23 03:10:14 +00:00
$wpdb -> prefix . self :: get_table_name (),
2017-07-30 22:38:17 +00:00
$data ,
array (
'download_log_id' => $download_log -> get_id (),
),
$format
);
$download_log -> apply_changes ();
}
/**
* Get a download log object .
*
* @ param array $data From the DB .
* @ return WC_Customer_Download_Log
*/
private function get_download_log ( $data ) {
return new WC_Customer_Download_Log ( $data );
}
/**
* Get array of download log ids by specified args .
*
2018-03-08 19:28:05 +00:00
* @ param array $args Arguments to define download logs to retrieve .
2017-07-30 22:38:17 +00:00
* @ return array
*/
public function get_download_logs ( $args = array () ) {
global $wpdb ;
$args = wp_parse_args ( $args , array (
'permission_id' => '' ,
'user_id' => '' ,
'user_ip_address' => '' ,
2018-04-13 16:00:26 +00:00
'orderby' => 'download_log_id' ,
'order' => 'DESC' ,
'limit' => - 1 ,
'page' => 1 ,
'return' => 'objects' ,
2017-07-30 22:38:17 +00:00
) );
$query = array ();
2018-04-13 17:03:02 +00:00
$table = $wpdb -> prefix . self :: get_table_name ();
$query [] = " SELECT * FROM { $table } WHERE 1=1 " ;
2017-07-30 22:38:17 +00:00
if ( $args [ 'permission_id' ] ) {
2018-04-13 17:03:02 +00:00
$query [] = $wpdb -> prepare ( 'AND permission_id = %d' , $args [ 'permission_id' ] );
2017-07-30 22:38:17 +00:00
}
if ( $args [ 'user_id' ] ) {
2018-04-13 17:03:02 +00:00
$query [] = $wpdb -> prepare ( 'AND user_id = %d' , $args [ 'user_id' ] );
2017-07-30 22:38:17 +00:00
}
if ( $args [ 'user_ip_address' ] ) {
2018-04-13 17:03:02 +00:00
$query [] = $wpdb -> prepare ( 'AND user_ip_address = %s' , $args [ 'user_ip_address' ] );
2017-07-30 22:38:17 +00:00
}
$allowed_orders = array ( 'download_log_id' , 'timestamp' , 'permission_id' , 'user_id' );
2018-04-13 17:03:02 +00:00
$order = in_array ( $args [ 'order' ], $allowed_orders , true ) ? $args [ 'order' ] : 'download_log_id' ;
2017-07-30 22:38:17 +00:00
$orderby = 'DESC' === strtoupper ( $args [ 'orderby' ] ) ? 'DESC' : 'ASC' ;
$orderby_sql = sanitize_sql_orderby ( " { $order } { $orderby } " );
$query [] = " ORDER BY { $orderby_sql } " ;
if ( 0 < $args [ 'limit' ] ) {
2018-04-13 16:00:26 +00:00
$query [] = $wpdb -> prepare ( 'LIMIT %d, %d' , absint ( $args [ 'limit' ] ) * absint ( $args [ 'page' ] - 1 ), absint ( $args [ 'limit' ] ) );
2017-07-30 22:38:17 +00:00
}
2018-04-13 17:03:02 +00:00
$raw_download_logs = $wpdb -> get_results ( implode ( ' ' , $query ) ); // WPCS: unprepared SQL ok.
2017-07-30 22:38:17 +00:00
switch ( $args [ 'return' ] ) {
2018-04-13 17:03:02 +00:00
case 'ids' :
2017-07-30 22:38:17 +00:00
return wp_list_pluck ( $raw_download_logs , 'download_log_id' );
2018-04-13 17:03:02 +00:00
default :
2017-07-30 22:38:17 +00:00
return array_map ( array ( $this , 'get_download_log' ), $raw_download_logs );
}
}
/**
2017-07-31 03:44:11 +00:00
* Get download logs for a given download permission .
2017-07-30 22:38:17 +00:00
*
2018-04-13 17:03:02 +00:00
* @ param int $permission_id Permission to get logs for .
2017-07-30 22:38:17 +00:00
* @ return array
*/
public function get_download_logs_for_permission ( $permission_id ) {
2017-08-23 03:10:14 +00:00
// If no permission_id is passed, return an empty array.
2017-08-03 21:24:53 +00:00
if ( empty ( $permission_id ) ) {
return array ();
}
2018-03-07 19:16:01 +00:00
return $this -> get_download_logs (
array (
'permission_id' => $permission_id ,
)
);
2017-07-30 22:38:17 +00:00
}
2018-04-13 17:03:02 +00:00
/**
* Method to delete download logs for a given permission ID .
*
* @ since 3.4 . 0
* @ param int $id download_id of the downloads that will be deleted .
*/
public function delete_by_permission_id ( $id ) {
global $wpdb ;
$wpdb -> query ( $wpdb -> prepare ( " DELETE FROM { $wpdb -> prefix } woocommerce_downloadable_product_permissions WHERE permission_id = %d " , $id ) );
}
2017-07-30 22:38:17 +00:00
}