0, 'image_size' => 'medium', 'template' => 'default', 'show_button' => 'true', 'button_text' => __('Check Product*', 'affililabs'), 'button_color' => '#0073aa', 'show_rating' => 'true', 'show_features' => 'true', 'show_pros_cons' => 'true', 'show_description' => 'true', 'show_category' => 'true', 'show_marketplace' => 'true', 'show_badge' => 'true', 'badge_text' => __('Top Pick', 'affililabs'), 'badge_background_color' => '#ffc107', 'badge_text_color' => '#000000', 'border_radius' => '8', 'enable_shadow' => 'true', ); if (isset($atts['template']) && $atts['template'] === 'compact') { $default_atts['show_features'] = isset($atts['show_features']) ? $atts['show_features'] : 'false'; $default_atts['show_pros_cons'] = isset($atts['show_pros_cons']) ? $atts['show_pros_cons'] : 'false'; $default_atts['show_description'] = isset($atts['show_description']) ? $atts['show_description'] : 'false'; $default_atts['show_category'] = isset($atts['show_category']) ? $atts['show_category'] : 'false'; $default_atts['show_marketplace'] = isset($atts['show_marketplace']) ? $atts['show_marketplace'] : 'false'; $default_atts['show_badge'] = isset($atts['show_badge']) ? $atts['show_badge'] : 'false'; $default_atts['enable_shadow'] = isset($atts['enable_shadow']) ? $atts['enable_shadow'] : 'false'; } $atts = shortcode_atts($default_atts, $atts); // Handle random product selection $product_data = null; $product_id = 0; if (isset($atts['id']) && (strtolower($atts['id']) === 'random' || $atts['id'] === '0')) { // Get a random product $product_data = AFFLProductService::get_random_product('publish'); if ($product_data) { $product_id = $product_data['id']; } } elseif (isset($atts['id']) && strtolower($atts['id']) === 'category') { // Get a random product from the same category as the current post $current_post_id = get_the_ID(); $current_post = get_post($current_post_id); if ($current_post && $current_post->post_type === AFFILILABS_CPT_PRODUCT) { $categories = wp_get_post_terms($current_post_id, AFFILILABS_TAXONOMY_PRODUCT_CATEGORY); if (!is_wp_error($categories) && !empty($categories)) { $category_id = $categories[0]->term_id; $product_data = AFFLProductService::get_random_product_by_category($category_id, 'publish'); if ($product_data) { $product_id = $product_data['id']; } } } // Fall back to random if (!$product_data) { $product_data = AFFLProductService::get_random_product('publish'); if ($product_data) { $product_id = $product_data['id']; } } } else { // Standard behavior: use the provided ID or try to get from current post $product_id = intval($atts['id']); // If no ID provided and current post is not a product, get a random product if (!$product_id) { $current_post_id = get_the_ID(); $current_post = get_post($current_post_id); if (!$current_post || $current_post->post_type !== AFFILILABS_CPT_PRODUCT) { $product_data = AFFLProductService::get_random_product('publish'); if ($product_data) { $product_id = $product_data['id']; } } else { $product_id = $current_post_id; } } } // If we still don't have a product ID, return error if (!$product_id) { return __('Error: No product available', 'affililabs'); } // The assets are enqueued only when the shortcode is used affililabs_product_box_shortcode_assets(); // Get product data if we haven't already if (!$product_data) { $product_data = AFFLProductService::get_product_by_id($product_id); } if (!$product_data || get_post_status($product_id) !== 'publish') { return __('Error: Product not found or not published', 'affililabs'); } $image_size = sanitize_text_field($atts['image_size']); $show_button = filter_var($atts['show_button'], FILTER_VALIDATE_BOOLEAN); $button_text = sanitize_text_field($atts['button_text']); $button_color = sanitize_hex_color($atts['button_color']) ?: '#0073aa'; $show_rating = filter_var($atts['show_rating'], FILTER_VALIDATE_BOOLEAN); $show_features = filter_var($atts['show_features'], FILTER_VALIDATE_BOOLEAN); $show_pros_cons = filter_var($atts['show_pros_cons'], FILTER_VALIDATE_BOOLEAN); $show_description = filter_var($atts['show_description'], FILTER_VALIDATE_BOOLEAN); $show_category = filter_var($atts['show_category'], FILTER_VALIDATE_BOOLEAN); $show_marketplace = filter_var($atts['show_marketplace'], FILTER_VALIDATE_BOOLEAN); $show_badge = filter_var($atts['show_badge'], FILTER_VALIDATE_BOOLEAN); $badge_text = sanitize_text_field($atts['badge_text']); $badge_background_color = sanitize_hex_color($atts['badge_background_color']) ?: '#ffc107'; $badge_text_color = sanitize_hex_color($atts['badge_text_color']) ?: '#000000'; $border_radius = intval($atts['border_radius']); $enable_shadow = filter_var($atts['enable_shadow'], FILTER_VALIDATE_BOOLEAN); $image_url = get_the_post_thumbnail_url($product_id, $image_size) ?: AFFILILABS_DEFAULT_PRODUCT_IMAGE; $box_classes = 'affililabs-product-box'; if ($enable_shadow) { $box_classes .= ' affililabs-product-box--shadow'; } $box_style = 'border-radius: ' . $border_radius . 'px;'; $badge_style = 'background-color:' . $badge_background_color . '; color:' . $badge_text_color . ';'; ob_start(); ?>