|  | 
 | <?php
  | 
 | function ck_edd_user_download_button( $purchase_form, $args ) {
  | 
 | 	global $edd_options;
  | 
 | 
  | 
 | 	if ( !is_user_logged_in() ) {
  | 
 | 		return $purchase_form;
  | 
 | 	}
  | 
 | 
  | 
 | 	$download_id = (string)$args['download_id'];
  | 
 | 	$current_user_id = get_current_user_id();
  | 
 | 	
  | 
 | 	
  | 
 | 	
  | 
 | 	if ( edd_has_user_purchased( $current_user_id, $download_id, $variable_price_id = null ) ) {
  | 
 | 		$user_purchases = edd_get_users_purchases( $current_user_id, -1, false, 'complete' );
  | 
 | 		foreach ( $user_purchases as $purchase ) {
  | 
 | 			$cart_items    = edd_get_payment_meta_cart_details( $purchase->ID );
  | 
 | 			$item_ids = wp_list_pluck( $cart_items, 'id' );
  | 
 | 			if ( in_array( $download_id, $item_ids ) ) {
  | 
 | 				$email       = edd_get_payment_user_email( $purchase->ID );
  | 
 | 				$payment_key = edd_get_payment_key( $purchase->ID );
  | 
 | 			}
  | 
 | 		}
  | 
 | 
  | 
 | 		$download_ids = array();
  | 
 | 		if ( edd_is_bundled_product( $download_id ) ) {
  | 
 | 			$download_ids = edd_get_bundled_products( $download_id );
  | 
 | 		} else {
  | 
 | 			$download_ids[] = $download_id;
  | 
 | 		}
  | 
 | 
  | 
 | 		
  | 
 | 		$style             = isset( $edd_options['button_style'] )   ? $edd_options['button_style'] : 'button';
  | 
 | 		$color             = isset( $edd_options['checkout_color'] ) ? $edd_options['checkout_color'] : 'blue';
  | 
 | 		$new_purchase_form = '';
  | 
 | 
  | 
 | 		foreach ( $download_ids as $item ) {
  | 
 | 	 		
  | 
 | 			$download_data = edd_get_download_files( $item, null );
  | 
 | 
  | 
 | 			if ( $download_data ) {
  | 
 | 				foreach ( $download_data as $filekey => $file ) {
  | 
 | 	 				
  | 
 | 					$file_url = edd_get_download_file_url( $payment_key, $email, $filekey, $item, null );
  | 
 | 					$new_purchase_form .= '<a href="' . $file_url . '" class="' . $style . ' ' . $color . ' edd-submit"><span class="edd-add-to-cart-label">Download ' . $file['name'] . '</span></a> ';
  | 
 | 				}
  | 
 | 			}
  | 
 | 
  | 
 | 			
  | 
 | 			if ( !empty( $new_purchase_form ) ) {
  | 
 | 				$purchase_form = '<h4>' . __( 'You already own this product. Download it now:', 'edd' ) . '</h4>' . $new_purchase_form;
  | 
 | 			}
  | 
 | 		}
  | 
 | 	}
  | 
 | 
  | 
 | 	return $purchase_form;
  | 
 | 
  | 
 | }
  | 
 | add_filter( 'edd_purchase_download_form', 'ck_edd_user_download_button', 10, 2 );
  | 
 |  | 
 |  | 
Comments