Split download permission query from order retrieval to reduce overhead.

Closes #11274 @claudiosmweb
This commit is contained in:
Mike Jolley 2016-06-29 12:06:03 +01:00
parent 01a2acabdc
commit 9a77620cc4
2 changed files with 38 additions and 29 deletions

View File

@ -86,13 +86,11 @@ function wc_get_account_menu_items() {
'customer-logout' => __( 'Logout', 'woocommerce' ), 'customer-logout' => __( 'Logout', 'woocommerce' ),
); );
// Remove empty items. // Remove downloads if no downloadable products exist.
if ( ! empty( WC()->customer ) ) { $downloads = wc_get_customer_download_permissions( get_current_user_id() );
$downloads = WC()->customer->get_downloadable_products();
if ( ! sizeof( $downloads ) ) { if ( ! sizeof( $downloads ) ) {
unset( $items['downloads'] ); unset( $items['downloads'] );
}
} }
// Check if payment gateways support add new payment methods. // Check if payment gateways support add new payment methods.

View File

@ -351,6 +351,39 @@ function wc_modify_map_meta_cap( $caps, $cap, $user_id, $args ) {
} }
add_filter( 'map_meta_cap', 'wc_modify_map_meta_cap', 10, 4 ); add_filter( 'map_meta_cap', 'wc_modify_map_meta_cap', 10, 4 );
/**
* Get customer download permissions from the database.
*
* @param int $customer_id Customer/User ID
* @return array
*/
function wc_get_customer_download_permissions( $customer_id ) {
global $wpdb;
return apply_filters( 'woocommerce_permission_list', $wpdb->get_results(
$wpdb->prepare( "
SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions
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;
",
$customer_id,
date( 'Y-m-d', current_time( 'timestamp' ) )
)
), $customer_id );
}
/** /**
* Get customer available downloads. * Get customer available downloads.
* *
@ -358,35 +391,13 @@ add_filter( 'map_meta_cap', 'wc_modify_map_meta_cap', 10, 4 );
* @return array * @return array
*/ */
function wc_get_customer_available_downloads( $customer_id ) { function wc_get_customer_available_downloads( $customer_id ) {
global $wpdb;
$downloads = array(); $downloads = array();
$_product = null; $_product = null;
$order = null; $order = null;
$file_number = 0; $file_number = 0;
// Get results from valid orders only // Get results from valid orders only
$results = apply_filters( 'woocommerce_permission_list', $wpdb->get_results( $wpdb->prepare( " $results = wc_get_customer_download_permissions( $customer_id );
SELECT permissions.*
FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions as permissions
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;
", $customer_id, date( 'Y-m-d', current_time( 'timestamp' ) ) ) ), $customer_id );
if ( $results ) { if ( $results ) {
foreach ( $results as $result ) { foreach ( $results as $result ) {