woocommerce/includes/data-stores/class-wc-customer-download-...

447 lines
12 KiB
PHP
Raw Normal View History

2016-11-18 14:07:21 +00:00
<?php
/**
* WC_Customer_Download_Data_Store class file.
*
* @package WooCommerce\Classes
*/
2016-11-18 14:07:21 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC Customer Download Data Store.
*
2017-03-15 16:36:53 +00:00
* @version 3.0.0
2016-11-18 14:07:21 +00:00
*/
class WC_Customer_Download_Data_Store implements WC_Customer_Download_Data_Store_Interface {
/**
* Create download permission for a user.
2016-11-18 14:07:21 +00:00
*
* @param WC_Customer_Download $download WC_Customer_Download object.
2016-11-18 14:07:21 +00:00
*/
2016-11-18 17:13:02 +00:00
public function create( &$download ) {
2016-11-18 14:07:21 +00:00
global $wpdb;
2017-03-13 23:54:33 +00:00
// Always set a access granted date.
if ( is_null( $download->get_access_granted( 'edit' ) ) ) {
$download->set_access_granted( current_time( 'timestamp', true ) );
}
2016-11-18 17:13:02 +00:00
$data = array(
2017-02-20 13:36:15 +00:00
'download_id' => $download->get_download_id( 'edit' ),
'product_id' => $download->get_product_id( 'edit' ),
'user_id' => $download->get_user_id( 'edit' ),
'user_email' => $download->get_user_email( 'edit' ),
'order_id' => $download->get_order_id( 'edit' ),
'order_key' => $download->get_order_key( 'edit' ),
'downloads_remaining' => $download->get_downloads_remaining( 'edit' ),
2017-03-13 23:54:33 +00:00
'access_granted' => date( 'Y-m-d', $download->get_access_granted( 'edit' )->getTimestamp() ),
2017-02-20 13:36:15 +00:00
'download_count' => $download->get_download_count( 'edit' ),
2017-03-13 23:54:33 +00:00
'access_expires' => ! is_null( $download->get_access_expires( 'edit' ) ) ? date( 'Y-m-d', $download->get_access_expires( 'edit' )->getTimestamp() ) : null,
2016-11-18 17:13:02 +00:00
);
2016-11-18 14:07:21 +00:00
2016-11-18 17:13:02 +00:00
$format = array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%s',
);
$result = $wpdb->insert(
$wpdb->prefix . 'woocommerce_downloadable_product_permissions',
apply_filters( 'woocommerce_downloadable_file_permission_data', $data ),
apply_filters( 'woocommerce_downloadable_file_permission_format', $format, $data )
);
do_action( 'woocommerce_grant_product_download_access', $data );
if ( $result ) {
$download->set_id( $wpdb->insert_id );
$download->apply_changes();
}
}
/**
* Method to read a download permission from the database.
*
* @param WC_Customer_Download $download WC_Customer_Download object.
2017-05-12 08:52:26 +00:00
*
* @throws Exception Throw exception if invalid download is passed.
2016-11-18 17:13:02 +00:00
*/
public function read( &$download ) {
global $wpdb;
2016-11-18 14:07:21 +00:00
if ( ! $download->get_id() ) {
throw new Exception( __( 'Invalid download.', 'woocommerce' ) );
}
2016-11-18 17:13:02 +00:00
$download->set_defaults();
$raw_download = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d",
$download->get_id()
)
);
2016-11-18 17:13:02 +00:00
if ( ! $raw_download ) {
2016-11-18 17:13:02 +00:00
throw new Exception( __( 'Invalid download.', 'woocommerce' ) );
2016-11-18 14:07:21 +00:00
}
$download->set_props(
array(
'download_id' => $raw_download->download_id,
'product_id' => $raw_download->product_id,
'user_id' => $raw_download->user_id,
'user_email' => $raw_download->user_email,
'order_id' => $raw_download->order_id,
'order_key' => $raw_download->order_key,
'downloads_remaining' => $raw_download->downloads_remaining,
'access_granted' => strtotime( $raw_download->access_granted ),
'download_count' => $raw_download->download_count,
'access_expires' => is_null( $raw_download->access_expires ) ? null : strtotime( $raw_download->access_expires ),
)
);
2016-11-18 17:13:02 +00:00
$download->set_object_read( true );
}
/**
* Method to update a download in the database.
*
* @param WC_Customer_Download $download WC_Customer_Download object.
2016-11-18 17:13:02 +00:00
*/
public function update( &$download ) {
global $wpdb;
2016-11-18 14:07:21 +00:00
$data = array(
2017-02-20 13:36:15 +00:00
'download_id' => $download->get_download_id( 'edit' ),
'product_id' => $download->get_product_id( 'edit' ),
'user_id' => $download->get_user_id( 'edit' ),
'user_email' => $download->get_user_email( 'edit' ),
'order_id' => $download->get_order_id( 'edit' ),
'order_key' => $download->get_order_key( 'edit' ),
'downloads_remaining' => $download->get_downloads_remaining( 'edit' ),
2017-03-13 23:54:33 +00:00
'access_granted' => date( 'Y-m-d', $download->get_access_granted( 'edit' )->getTimestamp() ),
2017-02-20 13:36:15 +00:00
'download_count' => $download->get_download_count( 'edit' ),
2017-03-13 23:54:33 +00:00
'access_expires' => ! is_null( $download->get_access_expires( 'edit' ) ) ? date( 'Y-m-d', $download->get_access_expires( 'edit' )->getTimestamp() ) : null,
2016-11-18 14:07:21 +00:00
);
$format = array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
2016-11-18 17:13:02 +00:00
'%s',
2016-11-18 14:07:21 +00:00
);
2016-11-18 19:56:17 +00:00
$wpdb->update(
$wpdb->prefix . 'woocommerce_downloadable_product_permissions',
$data,
array(
'permission_id' => $download->get_id(),
),
$format
);
2016-11-18 17:13:02 +00:00
$download->apply_changes();
}
2016-11-18 14:07:21 +00:00
2016-11-18 17:13:02 +00:00
/**
* Method to delete a download permission from the database.
*
* @param WC_Customer_Download $download WC_Customer_Download object.
* @param array $args Array of args to pass to the delete method.
2016-11-18 17:13:02 +00:00
*/
public function delete( &$download, $args = array() ) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE permission_id = %d",
$download->get_id()
)
);
2016-11-18 17:13:02 +00:00
$download->set_id( 0 );
}
/**
* Method to delete a download permission from the database by ID.
*
* @param int $id permission_id of the download to be deleted.
2016-11-18 17:13:02 +00:00
*/
public function delete_by_id( $id ) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE permission_id = %d",
$id
)
);
2016-11-18 17:13:02 +00:00
}
/**
* Method to delete a download permission from the database by order ID.
*
* @param int $id Order ID of the downloads that will be deleted.
2016-11-18 17:13:02 +00:00
*/
public function delete_by_order_id( $id ) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE order_id = %d",
$id
)
);
2016-11-18 17:13:02 +00:00
}
/**
* Method to delete a download permission from the database by download ID.
*
* @param int $id download_id of the downloads that will be deleted.
2016-11-18 17:13:02 +00:00
*/
public function delete_by_download_id( $id ) {
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE download_id = %s",
$id
)
);
2016-11-18 17:13:02 +00:00
}
/**
* Method to delete a download permission from the database by user ID.
*
* @since 3.4.0
* @param int $id user ID of the downloads that will be deleted.
2018-05-02 10:45:02 +00:00
* @return bool True if deleted rows.
*/
public function delete_by_user_id( $id ) {
global $wpdb;
2018-05-02 10:45:02 +00:00
return (bool) $wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE user_id = %d",
$id
)
);
}
2018-04-13 14:35:03 +00:00
/**
* Method to delete a download permission from the database by user email.
*
* @since 3.4.0
* @param string $email email of the downloads that will be deleted.
2018-05-02 10:45:02 +00:00
* @return bool True if deleted rows.
2018-04-13 14:35:03 +00:00
*/
public function delete_by_user_email( $email ) {
global $wpdb;
2018-05-02 10:45:02 +00:00
return (bool) $wpdb->query(
2018-04-13 14:35:03 +00:00
$wpdb->prepare(
"DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
WHERE user_email = %s",
$email
)
);
}
2016-11-18 17:13:02 +00:00
/**
* Get a download object.
*
* @param array $data From the DB.
* @return WC_Customer_Download
*/
private function get_download( $data ) {
return new WC_Customer_Download( $data );
}
/**
* Get array of download ids by specified args.
*
* @param array $args Arguments to filter downloads. $args['return'] accepts the following values: 'objects' (default), 'ids' or a comma separeted list of fields (for example: 'order_id,user_id,user_email').
* @return array Can be an array of permission_ids, an array of WC_Customer_Download objects or an array of arrays containing specified fields depending on the value of $args['return'].
2016-11-18 17:13:02 +00:00
*/
public function get_downloads( $args = array() ) {
global $wpdb;
$args = wp_parse_args(
$args, array(
'user_email' => '',
2018-04-13 16:00:26 +00:00
'user_id' => '',
'order_id' => '',
'order_key' => '',
'product_id' => '',
'download_id' => '',
'orderby' => 'permission_id',
'order' => 'ASC',
'limit' => -1,
2018-04-13 16:00:26 +00:00
'page' => 1,
'return' => 'objects',
)
);
2016-11-18 17:13:02 +00:00
$valid_fields = array( 'permission_id', 'download_id', 'product_id', 'order_id', 'order_key', 'user_email', 'user_id', 'downloads_remaining', 'access_granted', 'access_expires', 'download_count' );
$get_results_output = ARRAY_A;
if ( 'ids' === $args['return'] ) {
$fields = 'permission_id';
} elseif ( 'objects' === $args['return'] ) {
$fields = '*';
$get_results_output = OBJECT;
} else {
$fields = explode( ',', (string) $args['return'] );
$fields = implode( ', ', array_intersect( $fields, $valid_fields ) );
}
2016-11-18 17:13:02 +00:00
$query = array();
$query[] = "SELECT {$fields} FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE 1=1";
2016-11-18 17:13:02 +00:00
if ( $args['user_email'] ) {
$query[] = $wpdb->prepare( 'AND user_email = %s', sanitize_email( $args['user_email'] ) );
2016-11-18 14:07:21 +00:00
}
2018-04-13 16:00:26 +00:00
if ( $args['user_id'] ) {
$query[] = $wpdb->prepare( 'AND user_id = %d', absint( $args['user_id'] ) );
}
if ( $args['order_id'] ) {
$query[] = $wpdb->prepare( 'AND order_id = %d', $args['order_id'] );
2016-11-18 19:29:37 +00:00
}
if ( $args['order_key'] ) {
$query[] = $wpdb->prepare( 'AND order_key = %s', $args['order_key'] );
2016-11-18 17:13:02 +00:00
}
2016-11-18 14:07:21 +00:00
if ( $args['product_id'] ) {
$query[] = $wpdb->prepare( 'AND product_id = %d', $args['product_id'] );
2016-11-18 17:13:02 +00:00
}
2017-04-05 20:37:42 +00:00
if ( $args['download_id'] ) {
$query[] = $wpdb->prepare( 'AND download_id = %s', $args['download_id'] );
2017-04-05 20:37:42 +00:00
}
$orderby = in_array( $args['orderby'], $valid_fields, true ) ? $args['orderby'] : 'permission_id';
$order = 'DESC' === strtoupper( $args['order'] ) ? 'DESC' : 'ASC';
$orderby_sql = sanitize_sql_orderby( "{$orderby} {$order}" );
$query[] = "ORDER BY {$orderby_sql}";
2016-11-18 17:13:02 +00:00
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'] ) );
2016-11-18 17:13:02 +00:00
}
// phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
$results = $wpdb->get_results( implode( ' ', $query ), $get_results_output );
2016-11-18 17:13:02 +00:00
switch ( $args['return'] ) {
case 'ids':
return wp_list_pluck( $results, 'permission_id' );
case 'objects':
return array_map( array( $this, 'get_download' ), $results );
default:
return $results;
2016-11-18 19:29:37 +00:00
}
2016-11-18 17:13:02 +00:00
}
2016-11-18 19:29:37 +00:00
/**
* Update download ids if the hash changes.
*
* @deprecated 3.3.0 Download id is now a static UUID and should not be changed based on file hash.
*
* @param int $product_id Product ID.
* @param string $old_id Old download_id.
* @param string $new_id New download_id.
2016-11-18 19:29:37 +00:00
*/
public function update_download_id( $product_id, $old_id, $new_id ) {
global $wpdb;
2016-11-18 17:13:02 +00:00
wc_deprecated_function( __METHOD__, '3.3' );
2016-11-18 19:29:37 +00:00
$wpdb->update(
$wpdb->prefix . 'woocommerce_downloadable_product_permissions',
array(
'download_id' => $new_id,
),
array(
'download_id' => $old_id,
'product_id' => $product_id,
)
);
}
2016-11-18 17:13:02 +00:00
/**
* Get a customers downloads.
2016-11-18 19:29:37 +00:00
*
* @param int $customer_id Customer ID.
2016-11-18 17:13:02 +00:00
* @return array
*/
public function get_downloads_for_customer( $customer_id ) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions
2016-11-18 17:13:02 +00:00
WHERE user_id = %d
AND permissions.order_id > 0
AND
(
permissions.downloads_remaining > 0
OR permissions.downloads_remaining = ''
)
AND
(
permissions.access_expires IS NULL
OR permissions.access_expires >= %s
OR permissions.access_expires = '0000-00-00 00:00:00'
)
ORDER BY permissions.order_id, permissions.product_id, permissions.permission_id;",
2016-11-18 17:13:02 +00:00
$customer_id,
date( 'Y-m-d', current_time( 'timestamp' ) )
)
2016-11-18 19:29:37 +00:00
);
2016-11-18 14:07:21 +00:00
}
2016-11-18 17:13:02 +00:00
2016-11-18 19:29:37 +00:00
/**
* Update user prop for downloads based on order id.
*
* @param int $order_id Order ID.
* @param int $customer_id Customer ID.
* @param string $email Customer email address.
2016-11-18 19:29:37 +00:00
*/
2016-11-18 17:13:02 +00:00
public function update_user_by_order_id( $order_id, $customer_id, $email ) {
global $wpdb;
$wpdb->update(
$wpdb->prefix . 'woocommerce_downloadable_product_permissions',
2016-11-18 17:13:02 +00:00
array(
'user_id' => $customer_id,
'user_email' => $email,
),
array(
'order_id' => $order_id,
2016-11-18 17:13:02 +00:00
),
array(
'%d',
'%s',
),
array(
'%d',
)
);
}
2016-11-18 14:07:21 +00:00
}