3, 'per_page' => 12, 'category' => '', 'orderby' => 'date', 'order' => 'DESC', 'show_filters' => 'yes' ), $atts); ob_start(); $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'post_type' => 'wpdd_product', 'posts_per_page' => $atts['per_page'], 'paged' => $paged, 'orderby' => $atts['orderby'], 'order' => $atts['order'], 'post_status' => 'publish' ); if (!empty($atts['category'])) { $args['tax_query'] = array( array( 'taxonomy' => 'wpdd_product_category', 'field' => 'slug', 'terms' => explode(',', $atts['category']) ) ); } // Handle search $search_performed = false; if (isset($_GET['wpdd_search']) && !empty($_GET['wpdd_search'])) { $search_term = sanitize_text_field($_GET['wpdd_search']); $search_performed = true; // Bypass WordPress search and do direct query global $wpdb; $search_like = '%' . $wpdb->esc_like($search_term) . '%'; $matching_ids = $wpdb->get_col($wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpdd_product' AND post_status = 'publish' AND (post_title LIKE %s OR post_content LIKE %s)", $search_like, $search_like )); // Add debug output if (current_user_can('manage_options')) { echo ""; } if (!empty($matching_ids)) { // Found matching products $args['post__in'] = $matching_ids; // Only set orderby to post__in if no sort parameter is provided if (!isset($_GET['wpdd_sort']) || empty($_GET['wpdd_sort'])) { $args['orderby'] = 'post__in'; } } else { // No matches - force empty result $args['post__in'] = array(-1); // Use -1 instead of 0 } } if (isset($_GET['wpdd_category']) && !empty($_GET['wpdd_category'])) { $args['tax_query'] = array( array( 'taxonomy' => 'wpdd_product_category', 'field' => 'slug', 'terms' => sanitize_text_field($_GET['wpdd_category']) ) ); } // Handle creator filter if (isset($_GET['wpdd_creator']) && !empty($_GET['wpdd_creator'])) { $args['author'] = intval($_GET['wpdd_creator']); } if (isset($_GET['wpdd_sort']) && !empty($_GET['wpdd_sort'])) { switch ($_GET['wpdd_sort']) { case 'price_low': $args['meta_key'] = '_wpdd_price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'ASC'; break; case 'price_high': $args['meta_key'] = '_wpdd_price'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; break; case 'newest': $args['orderby'] = 'date'; $args['order'] = 'DESC'; break; case 'popular': $args['meta_key'] = '_wpdd_sales_count'; $args['orderby'] = 'meta_value_num'; $args['order'] = 'DESC'; break; } } $query = new WP_Query($args); // Debug output for admins if (current_user_can('manage_options') && isset($_GET['debug']) && $_GET['debug'] == '1') { echo '
'; echo '

Debug Info (Admin Only)

'; echo 'GET Parameters:
' . print_r($_GET, true) . '
'; echo 'Query Args:
' . print_r($args, true) . '
'; echo 'Found Posts: ' . $query->found_posts . '
'; echo '
'; } ?>
$value) { if (!in_array($key, array('wpdd_search', 'wpdd_category', 'wpdd_creator', 'wpdd_sort', 'paged'))) { if (is_array($value)) { foreach ($value as $sub_value) { echo ''; } } else { echo ''; } } } ?>
have_posts()) : ?>
have_posts()) : $query->the_post(); ?>
$base_url, 'total' => $query->max_num_pages, 'current' => $paged, 'format' => '', 'prev_text' => __('« Previous', 'wp-digital-download'), 'next_text' => __('Next »', 'wp-digital-download'), )); ?>

post_author); ?>
<?php echo esc_attr($product->post_title); ?>

post_title); ?>

ID, $shop_url); ?> display_name); ?>
post_excerpt ?: $product->post_content, 20); ?>
$ $ $
' . __('Invalid access key.', 'wp-digital-download') . '

'; } $orders = $wpdb->get_results($wpdb->prepare( "SELECT o.*, p.post_title as product_name, l.license_key, (SELECT COUNT(*) FROM {$wpdb->prefix}wpdd_downloads d WHERE d.order_id = o.id) as download_count FROM {$wpdb->prefix}wpdd_orders o LEFT JOIN {$wpdb->posts} p ON o.product_id = p.ID LEFT JOIN {$wpdb->prefix}wpdd_licenses l ON o.id = l.order_id WHERE o.customer_email = %s AND o.status = 'completed' ORDER BY o.purchase_date DESC", $email )); } elseif (is_user_logged_in()) { $current_user = wp_get_current_user(); // Get orders by user ID or email (to include guest purchases before account creation) $orders = $wpdb->get_results($wpdb->prepare( "SELECT o.*, p.post_title as product_name, l.license_key, (SELECT COUNT(*) FROM {$wpdb->prefix}wpdd_downloads d WHERE d.order_id = o.id) as download_count FROM {$wpdb->prefix}wpdd_orders o LEFT JOIN {$wpdb->posts} p ON o.product_id = p.ID LEFT JOIN {$wpdb->prefix}wpdd_licenses l ON o.id = l.order_id WHERE (o.customer_id = %d OR o.customer_email = %s) AND o.status = 'completed' ORDER BY o.purchase_date DESC", $current_user->ID, $current_user->user_email )); } else { return '

' . sprintf( __('Please login to view your purchases.', 'wp-digital-download'), wp_login_url(get_permalink()) ) . '

'; } ob_start(); ?>

product_id, '_wpdd_download_limit', true); $download_limit = $download_limit ?: 0; // Convert empty string to 0 (unlimited) $download_expiry = get_post_meta($order->product_id, '_wpdd_download_expiry', true); $is_expired = false; if ($download_expiry > 0) { $expiry_date = date('Y-m-d H:i:s', strtotime($order->purchase_date . ' + ' . $download_expiry . ' days')); $is_expired = current_time('mysql') > $expiry_date; } // Ensure download_count is a number $current_downloads = (int) $order->download_count; $can_download = !$is_expired && ($download_limit == 0 || $current_downloads < $download_limit); ?> license_key)) : ?>
order_number); ?> product_name); ?> purchase_date)); ?> $amount, 2); ?> 0) { echo sprintf('%d / %d', $current_downloads, $download_limit); } else { echo sprintf('%d / %s', $current_downloads, __('unlimited', 'wp-digital-download')); } ?> $order->id); // For guest access, include email and key if (isset($_GET['customer_email']) && isset($_GET['key'])) { $download_args['customer_email'] = $_GET['customer_email']; $download_args['key'] = $_GET['key']; } $download_url = wp_nonce_url( add_query_arg($download_args), 'wpdd_download_' . $order->id ); ?>
license_key); ?>

' . __('No product selected for checkout.', 'wp-digital-download') . '

'; } $product_id = intval($_GET['product_id']); $product = get_post($product_id); if (!$product || $product->post_type !== 'wpdd_product') { return '

' . __('Invalid product.', 'wp-digital-download') . '

'; } $price = get_post_meta($product_id, '_wpdd_price', true); $sale_price = get_post_meta($product_id, '_wpdd_sale_price', true); $is_free = get_post_meta($product_id, '_wpdd_is_free', true); $final_price = ($sale_price && $sale_price < $price) ? $sale_price : $price; ob_start(); ?>

post_title); ?>

$

' . __('Invalid order.', 'wp-digital-download') . '

'; } global $wpdb; $order_id = sanitize_text_field($_GET['order_id']); // Get order details along with download token $order = $wpdb->get_row($wpdb->prepare( "SELECT o.*, p.post_title as product_name, dl.token as download_token FROM {$wpdb->prefix}wpdd_orders o LEFT JOIN {$wpdb->posts} p ON o.product_id = p.ID LEFT JOIN {$wpdb->prefix}wpdd_download_links dl ON o.id = dl.order_id WHERE o.order_number = %s", $order_id )); if (!$order) { return '

' . __('Order not found.', 'wp-digital-download') . '

'; } ob_start(); ?>

order_number); ?>

product_name); ?>

$amount, 2); ?>

download_token) { $download_url = add_query_arg(array( 'wpdd_download_token' => $order->download_token ), home_url()); } else { // For legacy orders without tokens, create one now if (class_exists('WPDD_Download_Handler')) { $token = WPDD_Download_Handler::ensure_download_token($order->id); if ($token) { $download_url = add_query_arg(array( 'wpdd_download_token' => $token ), home_url()); } else { // Still fallback to old method if token creation fails $download_args = array('wpdd_download' => $order->id); // For guest users, include email and authentication key if (!is_user_logged_in()) { $download_args['customer_email'] = $order->customer_email; $download_args['key'] = substr(md5($order->customer_email . AUTH_KEY), 0, 10); } $download_url = wp_nonce_url( add_query_arg($download_args), 'wpdd_download_' . $order->id ); } } else { // Fallback if WPDD_Download_Handler class not found $download_args = array('wpdd_download' => $order->id); // For guest users, include email and authentication key if (!is_user_logged_in()) { $download_args['customer_email'] = $order->customer_email; $download_args['key'] = substr(md5($order->customer_email . AUTH_KEY), 0, 10); } $download_url = wp_nonce_url( add_query_arg($download_args), 'wpdd_download_' . $order->id ); } } ?>

purchase history.', 'wp-digital-download'), get_permalink(get_option('wpdd_purchases_page_id')) ); ?>

0 ), $atts); if (!$atts['id']) { return ''; } ob_start(); self::render_product_card($atts['id']); return ob_get_clean(); } public static function buy_button_shortcode($atts) { $atts = shortcode_atts(array( 'id' => get_the_ID(), 'text' => __('Buy Now', 'wp-digital-download'), 'class' => 'wpdd-buy-button' ), $atts); if (!$atts['id']) { return ''; } return self::render_buy_button($atts['id'], $atts['text'], $atts['class']); } public static function render_buy_button($product_id, $text = '', $class = '') { global $wpdb; // Check if user already owns this product $user_owns_product = false; $existing_order = null; if (is_user_logged_in()) { $current_user = wp_get_current_user(); $existing_order = $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpdd_orders WHERE product_id = %d AND (customer_id = %d OR customer_email = %s) AND status = 'completed' ORDER BY purchase_date DESC LIMIT 1", $product_id, $current_user->ID, $current_user->user_email )); if ($existing_order) { $user_owns_product = true; } } // If user already owns the product, show download button if ($user_owns_product) { $download_url = wp_nonce_url( add_query_arg(array('wpdd_download' => $existing_order->id)), 'wpdd_download_' . $existing_order->id ); return sprintf( '%s', esc_url($download_url), esc_attr($class ?: 'wpdd-download-button'), esc_html(__('Download (Already Purchased)', 'wp-digital-download')) ); } // Regular buy button for products not owned $price = get_post_meta($product_id, '_wpdd_price', true); $sale_price = get_post_meta($product_id, '_wpdd_sale_price', true); $is_free = get_post_meta($product_id, '_wpdd_is_free', true); $actual_price = ($sale_price && $sale_price < $price) ? $sale_price : $price; if (!$text) { if ($is_free) { $text = __('Download Free', 'wp-digital-download'); } else { $text = sprintf(__('Buy Now - $%s', 'wp-digital-download'), number_format($actual_price, 2)); } } $checkout_url = add_query_arg(array( 'wpdd_action' => 'add_to_cart', 'product_id' => $product_id, 'nonce' => wp_create_nonce('wpdd_add_to_cart') ), get_permalink(get_option('wpdd_checkout_page_id'))); $button_html = sprintf( '%s', esc_url($checkout_url), esc_attr($class ?: 'wpdd-buy-button'), esc_attr($product_id), esc_attr($actual_price), esc_html($text) ); return $button_html; } public static function add_buy_button_to_product($content) { if (!is_singular('wpdd_product')) { return $content; } global $post; // Get product details $price = get_post_meta($post->ID, '_wpdd_price', true); $sale_price = get_post_meta($post->ID, '_wpdd_sale_price', true); $is_free = get_post_meta($post->ID, '_wpdd_is_free', true); $files = get_post_meta($post->ID, '_wpdd_files', true); // Build product info box $product_info = '
'; // Left section with meta $product_info .= '
'; // File count if (is_array($files)) { $file_count = count($files); $product_info .= '

' . __('Files:', 'wp-digital-download') . ' ' . sprintf(_n('%d file', '%d files', $file_count, 'wp-digital-download'), $file_count) . '

'; } // Creator info with shop filter link $creator = get_userdata($post->post_author); $shop_page_id = get_option('wpdd_shop_page_id'); $shop_url = $shop_page_id ? get_permalink($shop_page_id) : home_url('/shop/'); $creator_filter_url = add_query_arg('wpdd_creator', $creator->ID, $shop_url); $product_info .= '

' . __('Created by', 'wp-digital-download') . ' ' . '' . esc_html($creator->display_name) . '

'; // Categories $categories = wp_get_post_terms($post->ID, 'wpdd_product_category', array('fields' => 'names')); if (!empty($categories)) { $product_info .= '

' . __('Categories:', 'wp-digital-download') . ' ' . implode(', ', $categories) . '

'; } $product_info .= '
'; // close meta // Right section with buy button $product_info .= '
'; // Buy button (price is included in button text) $product_info .= '
'; $product_info .= self::render_buy_button($post->ID); $product_info .= '
'; $product_info .= '
'; // close purchase section $product_info .= '
'; // close product info box // Add CSS for the product info box $product_info .= ''; // Add the product info box before the content return $product_info . $content; } public static function creator_dashboard_shortcode($atts) { if (!is_user_logged_in()) { return '

' . __('Please log in to view your creator dashboard.', 'wp-digital-download') . '

'; } $user_id = get_current_user_id(); // Check if user is a creator if (!WPDD_Creator::is_creator($user_id)) { return '

' . __('This dashboard is only available for creators.', 'wp-digital-download') . '

'; } global $wpdb; // Get creator stats $total_earnings = WPDD_Creator::get_creator_total_earnings($user_id); $net_earnings = WPDD_Creator::get_creator_net_earnings($user_id); $current_balance = WPDD_Creator::get_creator_balance($user_id); $commission_rate = floatval(get_option('wpdd_commission_rate', 0)); $currency = get_option('wpdd_currency', 'USD'); $paypal_email = get_user_meta($user_id, 'wpdd_paypal_email', true); // Get recent sales $recent_sales = $wpdb->get_results($wpdb->prepare( "SELECT o.*, p.post_title as product_name, (o.total * %f / 100) as platform_fee, (o.total * (100 - %f) / 100) as creator_earning FROM {$wpdb->prefix}wpdd_orders o INNER JOIN {$wpdb->posts} p ON o.product_id = p.ID WHERE p.post_author = %d AND o.status = 'completed' ORDER BY o.purchase_date DESC LIMIT 20", $commission_rate, $commission_rate, $user_id )); // Get payout history $payouts = $wpdb->get_results($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wpdd_payouts WHERE creator_id = %d ORDER BY created_at DESC LIMIT 10", $user_id )); ob_start(); ?>

0) { echo '

' . sprintf(__('Automatic payouts are triggered when balance reaches %s', 'wp-digital-download'), wpdd_format_price($threshold, $currency)) . '

'; } ?>

purchase_date))); ?> product_name); ?> customer_name); ?> total, $currency); ?> platform_fee, $currency); ?> creator_earning, $currency); ?>

created_at))); ?> amount, $payout->currency); ?> status)); ?> transaction_id ?: '-'); ?> payout_method)); ?>