function wpsc_the_product_thumbnail($width = null, $height = null, $product_id = 0, $page = false) { $thumbnail = ''; // Get the product ID if none was passed if (empty($product_id)) { $product_id = get_the_ID(); } // Load the product $product = get_post($product_id); $thumbnail_id = wpsc_the_product_thumbnail_id($product_id); // If no thumbnail found for item, get it's parent image (props. TJM) if (!$thumbnail_id && $product->post_parent) { $thumbnail_id = wpsc_the_product_thumbnail_id($product->post_parent); } if ($page == 'manage-products' && isset($thumbnail_id)) { $current_size = image_get_intermediate_size($thumbnail_id, 'wpsc_product_admin_thumbnail'); if (!$current_size) { _wpsc_regenerate_thumbnail_size($thumbnail_id, 'admin-product-thumbnails'); } $src = wp_get_attachment_image_src($thumbnail_id, 'wpsc_product_admin_thumbnail'); if (!empty($src) && is_string($src[0])) { $thumbnail = $src[0]; } } if (!$thumbnail && isset($thumbnail_id)) { $thumbnail = wpsc_product_image($thumbnail_id, $width, $height); } if (!empty($thumbnail) && is_ssl()) { $thumbnail = str_replace('http://', 'https://', $thumbnail); } return $thumbnail; }
/** * wpsc product thumbnail function * * Show the thumbnail image for the product * * @return string - the URL to the thumbnail image */ function wpsc_the_product_thumbnail($width = null, $height = null, $product_id = 0, $page = false) { $thumbnail = false; $display = wpsc_check_display_type(); // Get the product ID if none was passed if (empty($product_id)) { $product_id = get_the_ID(); } // Load the product $product = get_post($product_id); $thumbnail_id = wpsc_the_product_thumbnail_id($product_id); // If no thumbnail found for item, get its parent image if (!$thumbnail_id && (is_a($product, 'WP_Post') && $product->post_parent)) { $thumbnail_id = wpsc_the_product_thumbnail_id($product->post_parent); } // if still no thumbnail ID is found, return our fallback function if (!$thumbnail_id) { return wpsc_product_image(); } if (!$page) { $page = is_single() ? 'single' : 'products-page'; } if (!$width && !$height) { $width = get_option('product_image_width'); $height = get_option('product_image_height'); // Overwrite height & width if custom dimensions exist for thumbnail_id if ('grid' != $display && 'products-page' == $page && isset($thumbnail_id)) { $custom_width = get_post_meta($thumbnail_id, '_wpsc_custom_thumb_w', true); $custom_height = get_post_meta($thumbnail_id, '_wpsc_custom_thumb_h', true); if (!empty($custom_width) && !empty($custom_height)) { $width = $custom_width; $height = $custom_height; } } elseif ($page == 'single' && isset($thumbnail_id)) { $custom_thumbnail = get_post_meta($thumbnail_id, '_wpsc_selected_image_size', true); if (!$custom_thumbnail) { $custom_thumbnail = 'medium-single-product'; } $src = wp_get_attachment_image_src($thumbnail_id, $custom_thumbnail); if (!$src) { $custom_thumbnail = 'medium-single-product'; $current_size = image_get_intermediate_size($thumbnail_id, $custom_thumbnail); $settings_width = get_option('single_view_image_width'); $settings_height = get_option('single_view_image_height'); if (!$current_size || $current_size['width'] != $settings_width && $current_size['height'] != $settings_height) { _wpsc_regenerate_thumbnail_size($thumbnail_id, $custom_thumbnail); } $src = wp_get_attachment_image_src($thumbnail_id, $custom_thumbnail); } if (!empty($src) && is_string($src[0])) { $thumbnail = $src[0]; } } elseif ($page == 'manage-products' && isset($thumbnail_id)) { $current_size = image_get_intermediate_size($thumbnail_id, 'admin-product-thumbnails'); if (!$current_size) { _wpsc_regenerate_thumbnail_size($thumbnail_id, 'admin-product-thumbnails'); } $src = wp_get_attachment_image_src($thumbnail_id, 'admin-product-thumbnails'); if (!empty($src) && is_string($src[0])) { $thumbnail = $src[0]; } } } // Calculate the height based on the ratio of the original dimensions. if ($height == 0 || $width == 0) { $attachment_meta = get_post_meta($thumbnail_id, '_wp_attachment_metadata', true); $original_width = isset($attachment_meta['width']) ? absint($attachment_meta['width']) : 0; $original_height = isset($attachment_meta['height']) ? absint($attachment_meta['height']) : 0; // bail if either original value is zero. can't divide by zero. if ($original_width == 0 || $original_height == 0) { return; } if ($width != 0) { $height = $original_height / $original_width * $width; $height = round($height, 0); } elseif ($height != 0) { $width = $original_width / $original_height * $height; $width = round($width, 0); } } if (!$thumbnail && isset($thumbnail_id)) { $thumbnail = wpsc_product_image($thumbnail_id, $width, $height); } // WordPress's esc_url() function strips out spaces, so encode them here to ensure they don't get stripped out // Ref: http://core.trac.wordpress.org/ticket/23605 $thumbnail = str_replace(' ', '%20', $thumbnail); return apply_filters('wpsc_the_product_thumbnail', set_url_scheme($thumbnail), $product_id, $product); }
function _wpsc_ajax_get_variation_gallery() { $id = absint($_REQUEST['id']); $gallery = _wpsc_get_product_gallery_json($id); return array('models' => $gallery, 'featuredId' => wpsc_the_product_thumbnail_id($id)); }
/** * Maybe Get The Parent Product Thumbnail ID * * If no thumbnail is found and the product is a variation it will * then try getting the parent product's image instead. * * @param int $thumbnail_id Thumbnail ID * @param int $product_id Product ID * @return int Product thumbnail ID * * @uses wpsc_the_product_thumbnail_id() Get the product thumbnail ID */ function wpsc_maybe_get_the_parent_product_thumbnail_id($thumbnail_id, $product_id) { if (!$thumbnail_id) { $product = get_post($product_id); if (is_a($product, 'WP_Post') && $product->post_parent > 0) { $thumbnail_id = wpsc_the_product_thumbnail_id($product->post_parent); } } return $thumbnail_id; }
/** * wpsc product thumbnail function * * Show the thumbnail image for the product * * @return string - the URL to the thumbnail image */ function wpsc_the_product_thumbnail($width = null, $height = null, $product_id = 0, $page = 'products-page') { $thumbnail = false; $display = wpsc_check_display_type(); // Get the product ID if none was passed if (empty($product_id)) { $product_id = get_the_ID(); } // Load the product $product = get_post($product_id); // Load image proportions if none were passed if ($width < 10 || $height < 10) { $width = get_option('product_image_width'); $height = get_option('product_image_height'); } $thumbnail_id = wpsc_the_product_thumbnail_id($product_id); // If no thumbnail found for item, get it's parent image (props. TJM) if (!$thumbnail_id && $product->post_parent) { $thumbnail_id = wpsc_the_product_thumbnail_id($product->post_parent); } if (!$width && !$height) { //Overwrite height & width if custom dimensions exist for thumbnail_id if ('grid' != $display && 'products-page' == $page && isset($thumbnail_id)) { $custom_width = get_post_meta($thumbnail_id, '_wpsc_custom_thumb_w', true); $custom_height = get_post_meta($thumbnail_id, '_wpsc_custom_thumb_h', true); if (!empty($custom_width) && !empty($custom_height)) { $width = $custom_width; $height = $custom_height; } } elseif ($page == 'single' && isset($thumbnail_id)) { $custom_thumbnail = get_post_meta($thumbnail_id, '_wpsc_selected_image_size', true); if (!$custom_thumbnail) { $custom_thumbnail = 'medium-single-product'; $current_size = image_get_intermediate_size($thumbnail_id, $custom_thumbnail); $settings_width = get_option('single_view_image_width'); $settings_height = get_option('single_view_image_height'); if (!$current_size || $current_size['width'] != $settings_width && $current_size['height'] != $settings_height) { _wpsc_regenerate_thumbnail_size($thumbnail_id, $custom_thumbnail); } } $src = wp_get_attachment_image_src($thumbnail_id, $custom_thumbnail); if (!empty($src) && is_string($src[0])) { $thumbnail = $src[0]; } } elseif ($page == 'manage-products' && isset($thumbnail_id)) { $current_size = image_get_intermediate_size($thumbnail_id, 'admin-product-thumbnails'); if (!$current_size) { _wpsc_regenerate_thumbnail_size($thumbnail_id, 'admin-product-thumbnails'); } $src = wp_get_attachment_image_src($thumbnail_id, 'admin-product-thumbnails'); if (!empty($src) && is_string($src[0])) { $thumbnail = $src[0]; } } } // calculate the height based on the ratio of the original demensions // blame Cameron if this is buggy :P if ($height == 0 || $width == 0) { $attachment_meta = get_post_meta($thumbnail_id, '_wp_attachment_metadata', false); $original_width = $attachment_meta[0]['width']; $original_height = $attachment_meta[0]['height']; if ($width != 0) { $height = $original_height / $original_width * $width; $height = round($height, 0); } elseif ($height != 0) { $width = $original_width / $original_height * $height; $width = round($width, 0); } } if (!$thumbnail && isset($thumbnail_id)) { $thumbnail = wpsc_product_image($thumbnail_id, $width, $height); } if (!empty($thumbnail) && is_ssl()) { $thumbnail = str_replace('http://', 'https://', $thumbnail); } return $thumbnail; }