Change downloads system to store order ID, and check status for access. Closes #204.
This commit is contained in:
parent
97afdc2aeb
commit
5ec62c6852
|
@ -213,6 +213,8 @@ function woocommerce_create_pages() {
|
|||
function woocommerce_tables_install() {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->hide_errors();
|
||||
|
||||
$collate = '';
|
||||
if($wpdb->supports_collation()) {
|
||||
if(!empty($wpdb->charset)) $collate = "DEFAULT CHARACTER SET $wpdb->charset";
|
||||
|
@ -221,31 +223,55 @@ function woocommerce_tables_install() {
|
|||
|
||||
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
|
||||
|
||||
$sql = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix . "woocommerce_attribute_taxonomies" ." (
|
||||
`attribute_id` mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
`attribute_name` varchar(200) NOT NULL,
|
||||
`attribute_label` longtext NULL,
|
||||
`attribute_type` varchar(200) NOT NULL,
|
||||
PRIMARY KEY id (`attribute_id`)) $collate;";
|
||||
$sql = "CREATE TABLE ". $wpdb->prefix . "woocommerce_attribute_taxonomies" ." (
|
||||
attribute_id mediumint(9) NOT NULL AUTO_INCREMENT,
|
||||
attribute_name varchar(200) NOT NULL,
|
||||
attribute_label longtext NULL,
|
||||
attribute_type varchar(200) NOT NULL,
|
||||
PRIMARY KEY id (attribute_id)) $collate;";
|
||||
dbDelta($sql);
|
||||
|
||||
$sql = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix . "woocommerce_downloadable_product_permissions" ." (
|
||||
`product_id` mediumint(9) NOT NULL,
|
||||
`user_email` varchar(200) NOT NULL,
|
||||
`user_id` mediumint(9) NULL,
|
||||
`order_key` varchar(200) NOT NULL,
|
||||
`downloads_remaining` varchar(9) NULL,
|
||||
PRIMARY KEY id (`product_id`, `order_key`)) $collate;";
|
||||
$sql = "CREATE TABLE ". $wpdb->prefix . "woocommerce_downloadable_product_permissions" ." (
|
||||
product_id mediumint(9) NOT NULL,
|
||||
user_email varchar(200) NOT NULL,
|
||||
user_id mediumint(9) NULL,
|
||||
order_id mediumint(9) NULL,
|
||||
order_key varchar(200) NOT NULL,
|
||||
downloads_remaining varchar(9) NULL,
|
||||
PRIMARY KEY id (product_id,order_key)) $collate;";
|
||||
dbDelta($sql);
|
||||
|
||||
$sql = "CREATE TABLE IF NOT EXISTS ". $wpdb->prefix . "woocommerce_termmeta" ." (
|
||||
`meta_id` bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
`woocommerce_term_id` bigint(20) NOT NULL,
|
||||
`meta_key` varchar(255) NULL,
|
||||
`meta_value` longtext NULL,
|
||||
PRIMARY KEY id (`meta_id`)) $collate;";
|
||||
$sql = "CREATE TABLE ". $wpdb->prefix . "woocommerce_termmeta" ." (
|
||||
meta_id bigint(20) NOT NULL AUTO_INCREMENT,
|
||||
woocommerce_term_id bigint(20) NOT NULL,
|
||||
meta_key varchar(255) NULL,
|
||||
meta_value longtext NULL,
|
||||
PRIMARY KEY id (meta_id)) $collate;";
|
||||
dbDelta($sql);
|
||||
|
||||
|
||||
// Update woocommerce_downloadable_product_permissions table to include order ID's as well as keys
|
||||
$results = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."woocommerce_downloadable_product_permissions WHERE order_id IS NULL;" );
|
||||
|
||||
if ($results) foreach ($results as $result) :
|
||||
|
||||
if (!$result->order_key) continue;
|
||||
|
||||
$order_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM ".$wpdb->postmeta." WHERE meta_key = '_order_key' AND meta_value = '%s' LIMIT 1;", $result->order_key) );
|
||||
|
||||
if ($order_id) :
|
||||
|
||||
$wpdb->update( $wpdb->prefix . "woocommerce_downloadable_product_permissions", array(
|
||||
'order_id' => $order_id,
|
||||
), array(
|
||||
'product_id' => $result->product_id,
|
||||
'order_key' => $result->order_key
|
||||
), array( '%s' ), array( '%s', '%s' ) );
|
||||
|
||||
endif;
|
||||
|
||||
endforeach;
|
||||
|
||||
$wpdb->show_errors();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -164,38 +164,44 @@ class woocommerce_customer {
|
|||
$downloads = array();
|
||||
|
||||
if (is_user_logged_in()) :
|
||||
|
||||
$woocommerce_orders = &new woocommerce_orders();
|
||||
$woocommerce_orders->get_customer_orders( get_current_user_id() );
|
||||
if ($woocommerce_orders->orders) foreach ($woocommerce_orders->orders as $order) :
|
||||
if ( $order->status == 'completed' ) {
|
||||
$results = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."woocommerce_downloadable_product_permissions WHERE order_key = \"".$order->order_key."\" AND user_id = ".get_current_user_id().";" );
|
||||
$user_info = get_userdata(get_current_user_id());
|
||||
if ($results) foreach ($results as $result) :
|
||||
|
||||
$product_post = get_post( $result->product_id );
|
||||
|
||||
$user_info = get_userdata(get_current_user_id());
|
||||
|
||||
if ($product_post->post_type=='product_variation') :
|
||||
$_product = &new woocommerce_product_variation( $result->product_id );
|
||||
else :
|
||||
$_product = &new woocommerce_product( $result->product_id );
|
||||
endif;
|
||||
|
||||
if ($_product->exists) :
|
||||
$download_name = $_product->get_title();
|
||||
else :
|
||||
$download_name = '#' . $result->product_id;
|
||||
endif;
|
||||
$downloads[] = array(
|
||||
'download_url' => add_query_arg('download_file', $result->product_id, add_query_arg('order', $result->order_key, add_query_arg('email', $user_info->user_email, home_url()))),
|
||||
'product_id' => $result->product_id,
|
||||
'download_name' => $download_name,
|
||||
'order_id' => $order->id,
|
||||
'order_key' => $order->order_key,
|
||||
'downloads_remaining' => $result->downloads_remaining
|
||||
);
|
||||
endforeach;
|
||||
}
|
||||
$results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM ".$wpdb->prefix."woocommerce_downloadable_product_permissions WHERE user_id = '%s';", get_current_user_id()) );
|
||||
|
||||
if ($results) foreach ($results as $result) :
|
||||
|
||||
if (isset($result->order_id) && $result->order_id>0) :
|
||||
|
||||
$order = &new woocommerce_order( $result->order_id );
|
||||
|
||||
if ( $order->status != 'completed' ) continue;
|
||||
|
||||
$product_post = get_post( $result->product_id );
|
||||
|
||||
if ($product_post->post_type=='product_variation') :
|
||||
$_product = &new woocommerce_product_variation( $result->product_id );
|
||||
else :
|
||||
$_product = &new woocommerce_product( $result->product_id );
|
||||
endif;
|
||||
|
||||
if ($_product->exists) :
|
||||
$download_name = $_product->get_title();
|
||||
else :
|
||||
$download_name = '#' . $result->product_id;
|
||||
endif;
|
||||
|
||||
$downloads[] = array(
|
||||
'download_url' => add_query_arg('download_file', $result->product_id, add_query_arg('order', $result->order_key, add_query_arg('email', $user_info->user_email, home_url()))),
|
||||
'product_id' => $result->product_id,
|
||||
'download_name' => $download_name,
|
||||
'order_id' => $order->id,
|
||||
'order_key' => $order->order_key,
|
||||
'downloads_remaining' => $result->downloads_remaining
|
||||
);
|
||||
|
||||
endif;
|
||||
|
||||
endforeach;
|
||||
|
||||
endif;
|
||||
|
|
|
@ -103,6 +103,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
|
|||
* Made order notes optional
|
||||
* PayPal standard stores payer email address
|
||||
* Added handling for paypal reversal and refunded statuses
|
||||
* Downloads check order status is completed before allowing access - to do this we've added a new column to the permissions table (order id). Existing rows will be updated upon activation.
|
||||
|
||||
= 1.2.1 - 10/11/2011 =
|
||||
* Reworked downloadable and virtual products - now variations can be downloadable/virtual too making it more flexible
|
||||
|
@ -279,7 +280,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
|
|||
== Upgrade Notice ==
|
||||
|
||||
= 1.2.2 =
|
||||
Bug fixes. Due to some changes in the plugin, if you are using any of our extensions please ensure you check the changelogs and download any updates from your account - especially if using 2CO, iDeal or authorize.net.
|
||||
Due to some changes in the plugin, if you are using any of our extensions please ensure you check the changelogs and download any updates from your account - especially if using 2CO, iDeal or authorize.net. This version also updates the download permissions table so please ensure you backup your database before upgrading.
|
||||
|
||||
= 1.2.1 =
|
||||
This version has improved product types - ensure you de/re-activate the theme to ensure existing products get converted correctly.
|
||||
|
|
|
@ -799,18 +799,40 @@ function woocommerce_download_product() {
|
|||
global $wpdb;
|
||||
|
||||
$download_file = (int) urldecode($_GET['download_file']);
|
||||
$order = urldecode( $_GET['order'] );
|
||||
$order_key = urldecode( $_GET['order'] );
|
||||
$email = urldecode( $_GET['email'] );
|
||||
|
||||
if (!is_email($email)) wp_safe_redirect( home_url() );
|
||||
if (!is_email($email)) :
|
||||
wp_die( sprintf(__('Invalid email address. <a href="%s">Go to homepage →</a>', 'woothemes'), home_url()) );
|
||||
endif;
|
||||
|
||||
$downloads_remaining = $wpdb->get_var( $wpdb->prepare("
|
||||
SELECT downloads_remaining
|
||||
if (!is_email($email)) :
|
||||
|
||||
endif;
|
||||
|
||||
$download_result = $wpdb->get_row( $wpdb->prepare("
|
||||
SELECT order_id, downloads_remaining
|
||||
FROM ".$wpdb->prefix."woocommerce_downloadable_product_permissions
|
||||
WHERE user_email = %s
|
||||
AND order_key = %s
|
||||
AND product_id = %s
|
||||
;", $email, $order, $download_file ) );
|
||||
;", $email, $order_key, $download_file ) );
|
||||
|
||||
if (!$download_result) :
|
||||
wp_die( sprintf(__('Invalid download. <a href="%s">Go to homepage →</a>', 'woothemes'), home_url()) );
|
||||
exit;
|
||||
endif;
|
||||
|
||||
$order_id = $download_result->order_id;
|
||||
$downloads_remaining = $download_result->downloads_remaining;
|
||||
|
||||
if ($order_id) :
|
||||
$order = &new woocommerce_order( $order_id );
|
||||
if ($order->status!=='completed') :
|
||||
wp_die( sprintf(__('Invalid order. <a href="%s">Go to homepage →</a>', 'woothemes'), home_url()) );
|
||||
exit;
|
||||
endif;
|
||||
endif;
|
||||
|
||||
if ($downloads_remaining=='0') :
|
||||
wp_die( sprintf(__('Sorry, you have reached your download limit for this file. <a href="%s">Go to homepage →</a>', 'woothemes'), home_url()) );
|
||||
|
@ -821,7 +843,7 @@ function woocommerce_download_product() {
|
|||
'downloads_remaining' => $downloads_remaining - 1,
|
||||
), array(
|
||||
'user_email' => $email,
|
||||
'order_key' => $order,
|
||||
'order_key' => $order_key,
|
||||
'product_id' => $download_file
|
||||
), array( '%d' ), array( '%s', '%s', '%d' ) );
|
||||
endif;
|
||||
|
@ -949,6 +971,7 @@ function woocommerce_downloadable_product_permissions( $order_id ) {
|
|||
'product_id' => $download_id,
|
||||
'user_id' => $order->user_id,
|
||||
'user_email' => $user_email,
|
||||
'order_id' => $order->id,
|
||||
'order_key' => $order->order_key,
|
||||
'downloads_remaining' => $limit
|
||||
), array(
|
||||
|
|
Loading…
Reference in New Issue