Beispiel #1
0
 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);
}
Beispiel #3
0
/**
 * Return the HTML of a product's featured thumbnail.
 *
 * Note that the $size argument of this function is different from that of get_the_post_thumbnail().
 *
 * This function works similarly to get_the_post_thumbnail(), except the $size argument takes the
 * following shortcut values corresponding to the options in Settings->Store->Presentation:
 *     'single'   - corresponds to "Single Product Image Size" option.
 *     'archive'  - corresponds to "Default Product Thumbnail Size" option.
 *     'taxonomy' - corresponds to "Default Product Group Thumbnail Size" option.
 *     'cart'     - corresponds to the cart product thumbnail size option.
 *
 * @see   wpsc_check_thumbnail_support() Where the thumbnail sizes are registered.
 * @since 4.0
 *
 * @uses  $_wp_additional_image_sizes The array holding registered thumbnail sizes.
 * @uses  get_attached_file()
 * @uses  get_post_meta()
 * @uses  get_the_post_thumbnail()
 * @uses  wp_get_attachment_metadata()
 * @uses  wp_update_attachment_metadata()
 * @uses  wpsc_get_product_thumbnail_id()
 * @uses  wpsc_has_product_thumbnail()
 * @uses  update_post_meta()
 *
 * @param  null|int $id   Optional. The product ID. Defaults to the current product in the loop.
 * @param  string   $size Optional. Size of the product thumbnail. Defaults to 'single'.
 * @param  string   $attr Optional. Query string or array of attributes. Defaults to ''.
 * @return string
 */
function wpsc_get_product_thumbnail($id = null, $size = false, $attr = '')
{
    global $_wp_additional_image_sizes;
    $parent = get_post_field('post_parent', $id);
    if ($parent) {
        return wpsc_get_product_thumbnail($parent, $size, $attr);
    }
    if (!$size || !in_array($size, array('archive', 'taxonomy', 'single', 'cart', 'widget'))) {
        if (is_tax('wpsc_product_category') || is_tax('product_tag')) {
            $size = 'taxonomy';
        } elseif (wpsc_is_cart() || wpsc_is_checkout() || wpsc_is_customer_account()) {
            $size = 'cart';
        } elseif (is_singular('wpsc-product')) {
            $size = 'single';
        } elseif (is_post_type_archive('wpsc-product')) {
            $size = 'archive';
        } else {
            $size = 'full';
        }
    }
    $wpec_sizes = array('cart', 'single', 'archive', 'taxonomy');
    $wp_size = $size;
    if (in_array($size, $wpec_sizes)) {
        $wp_size = 'wpsc_product_' . $size . '_thumbnail';
        if (wpsc_has_product_thumbnail($id)) {
            $thumb_id = wpsc_get_product_thumbnail_id($id);
            // Get the size metadata registered in wpsc_check_thumbnail_support()
            $size_metadata = $_wp_additional_image_sizes[$wp_size];
            // Get the current size metadata that has been generated for this product
            // This metadata is generated in {@link _wpsc_filter_generate_attachment_metadata()}
            $current_size_metadata = get_post_meta($thumb_id, '_wpsc_generated_sizes', true);
            if (empty($current_size_metadata)) {
                $current_size_metadata = array();
            }
            // If this thumbnail for the current size was not generated yet, or generated with different
            // parameters (crop, for example), we need to regenerate the thumbnail
            if (!array_key_exists($size, $current_size_metadata) || $current_size_metadata[$size] != $size_metadata) {
                _wpsc_regenerate_thumbnail_size($thumb_id, $wp_size);
            }
        }
    }
    return get_the_post_thumbnail($id, $wp_size, $attr);
}
function wpsc_attachment_fields($form_fields, $post)
{
    $out = '';
    if (isset($_REQUEST["post_id"])) {
        $parent_post = get_post(absint($_REQUEST["post_id"]));
    } else {
        $parent_post = get_post($post->post_parent);
    }
    // check if post is set before accessing
    if (isset($parent_post) && $parent_post->post_type == "wpsc-product") {
        //Unfortunate hack, as I'm not sure why the From Computer tab doesn't process filters the same way the Gallery does
        ob_start();
        echo '
<script type="text/javascript">

	jQuery(function(){

		jQuery("a.wp-post-thumbnail").each(function(){
			var product_image = jQuery(this).text();
			if (product_image == "' . esc_js(__('Use as featured image', 'wp-e-commerce')) . '") {
				jQuery(this).text("' . esc_js(__('Use as Product Thumbnail', 'wp-e-commerce')) . '");
			}
		});

		var trash = jQuery("#media-upload a.del-link").text();

		if (trash == "' . esc_js(__('Delete', 'wp-e-commerce')) . '") {
			jQuery("#media-upload a.del-link").text("' . esc_js(__('Trash', 'wp-e-commerce')) . '");
		}


		});

</script>';
        $out .= ob_get_clean();
        $size_names = array('small-product-thumbnail' => __('Default Product Thumbnail Size', 'wp-e-commerce'), 'medium-single-product' => __('Single Product Image Size', 'wp-e-commerce'), 'full' => __('Full Size', 'wp-e-commerce'));
        $check = get_post_meta($post->ID, '_wpsc_selected_image_size', true);
        if (!$check) {
            $check = 'medium-single-product';
        }
        $current_size = image_get_intermediate_size($post->ID, $check);
        $settings_width = get_option('single_view_image_width');
        $settings_height = get_option('single_view_image_height');
        // regenerate size metadata in case it's missing
        if (!$check || $current_size['width'] != $settings_width && $current_size['height'] != $settings_height) {
            _wpsc_regenerate_thumbnail_size($post->ID, $check);
        }
        //This loop attaches the custom thumbnail/single image sizes to this page
        foreach ($size_names as $size => $name) {
            $downsize = image_downsize($post->ID, $size);
            // is this size selectable?
            $enabled = $downsize[3] || 'full' == $size;
            $css_id = "image-size-{$size}-{$post->ID}";
            // if this size is the default but that's not available, don't select it
            $html = "<div class='image-size-item'><input type='radio' " . disabled($enabled, false, false) . "name='attachments[{$post->ID}][wpsc_image_size]' id='{$css_id}' value='{$size}' " . checked($size, $check, false) . " />";
            $html .= "<label for='{$css_id}'>{$name}</label>";
            // only show the dimensions if that choice is available
            if ($enabled) {
                $html .= " <label for='{$css_id}' class='help'>" . sprintf(__("(%d&nbsp;&times;&nbsp;%d)", 'wp-e-commerce'), $downsize[1], $downsize[2]) . "</label>";
            }
            $html .= '</div>';
            $out .= $html;
        }
        unset($form_fields['post_excerpt'], $form_fields['image_url'], $form_fields['post_content'], $form_fields['post_title'], $form_fields['url'], $form_fields['align'], $form_fields['image_alt']['helps'], $form_fields["image-size"]);
        $form_fields['image_alt']['helps'] = __('Alt text for the product image, e.g. &#8220;Rockstar T-Shirt&#8221;', 'wp-e-commerce');
        $form_fields["wpsc_image_size"] = array('label' => __('Single Product Page Thumbnail:', 'wp-e-commerce'), 'input' => 'html', 'html' => $out, 'helps' => "<span style='text-align:left; clear:both; display:block; padding-top:3px;'>" . __('This is the Thumbnail size that will be displayed on the Single Product page. You can change the default sizes under your store settings', 'wp-e-commerce') . "</span>");
        //This is for the custom thumbnail size.
        $custom_thumb_size_w = get_post_meta($post->ID, "_wpsc_custom_thumb_w", true);
        $custom_thumb_size_h = get_post_meta($post->ID, "_wpsc_custom_thumb_h", true);
        $custom_thumb_html = "\n\n\t\t\t<input style='width:50px; text-align:center' type='text' name='attachments[{$post->ID}][wpsc_custom_thumb_w]' value='{$custom_thumb_size_w}' /> X <input style='width:50px; text-align:center' type='text' name='attachments[{$post->ID}][wpsc_custom_thumb_h]' value='{$custom_thumb_size_h}' />\n\n\t\t";
        $form_fields["wpsc_custom_thumb"] = array("label" => __('Products Page Thumbnail Size:', 'wp-e-commerce'), "input" => "html", "helps" => "<span style='text-align:left; clear:both; display:block; padding-top:3px;'>" . __('Custom thumbnail size for this image on the main Product Page', 'wp-e-commerce') . "</span>", "html" => $custom_thumb_html);
    }
    return $form_fields;
}
/**
 * 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;
}