Example #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;
 }
Example #2
0
/**
 * Featured Product
 *
 * Refactoring Featured Product Plugin to utilize Sticky Post Status, available since WP 2.7
 * also utilizes Featured Image functionality, available as post_thumbnail since 2.9, Featured Image since 3.0
 * Main differences - Removed 3.8 conditions, removed meta box from admin, changed meta_values
 * Removes shortcode, as it automatically ties in to top_of_page hook if sticky AND featured product exists.
 *
 * @package wp-e-commerce
 * @since 3.8
 */
function wpsc_the_sticky_image($product_id)
{
    $attached_images = (array) get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $product_id, 'orderby' => 'menu_order', 'order' => 'ASC'));
    if (has_post_thumbnail($product_id)) {
        add_image_size('featured-product-thumbnails', 540, 260, TRUE);
        $image = get_the_post_thumbnail($product_id, 'featured-product-thumbnails');
        return $image;
    } elseif (!empty($attached_images)) {
        $attached_image = $attached_images[0];
        $image_link = wpsc_product_image($attached_image->ID, 540, 260);
        return '<img src="' . $image_link . '" alt="" />';
    } else {
        return false;
    }
}
/**
 * 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);
}
/**
 * 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);
    // Get ID of parent product if one exists
    if (!empty($product->post_parent)) {
        $product_id = $product->post_parent;
    }
    // Load image proportions if none were passed
    if ($width < 10 || $height < 10) {
        $width = get_option('product_image_width');
        $height = get_option('product_image_height');
    }
    // Use product thumbnail
    if (has_post_thumbnail($product_id)) {
        $thumbnail_id = get_post_thumbnail_id($product_id);
        // Use first product image
    } else {
        // Get all attached images to this product
        $attached_images = (array) get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $product_id, 'orderby' => 'menu_order', 'order' => 'ASC'));
        if (!empty($attached_images)) {
            $thumbnail_id = $attached_images[0]->ID;
        }
    }
    //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');
            // regenerate size metadata in case it's missing
            if (!$current_size || $current_size['width'] != $settings_width || $current_size['height'] != $settings_height) {
                require_once ABSPATH . 'wp-admin/includes/image.php';
                if (!($metadata = wp_get_attachment_metadata($thumbnail_id))) {
                    $metadata = array();
                }
                if (empty($metadata['sizes'])) {
                    $metadata['sizes'] = array();
                }
                $file = get_attached_file($thumbnail_id);
                $generated = wp_generate_attachment_metadata($thumbnail_id, $file);
                $metadata['sizes'] = array_merge($metadata['sizes'], $generated['sizes']);
                wp_update_attachment_metadata($thumbnail_id, $metadata);
            }
        }
        $src = wp_get_attachment_image_src($thumbnail_id, $custom_thumbnail);
        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;
}
Example #5
0
function wpsc_donations($args = null)
{
    global $wpdb;
    // Args not used yet but this is ready for when it is
    $args = wp_parse_args((array) $args, array());
    $products = $wpdb->get_results("SELECT DISTINCT `p` . * , `m`.`meta_value` AS `special_price`\n\t\tFROM `" . $wpdb->postmeta . "` AS `m`\n\t\tJOIN `" . $wpdb->posts . "` AS `p` ON `m`.`post_id` = `p`.`ID`\n\t\tWHERE `p`.`post_parent` IN ('0')\n\t\t\tAND `m`.`meta_key` IN ('_wpsc_is_donation')\n\t\t\tAND `m`.`meta_value` IN( '1' )\n\t\tORDER BY RAND( )\n\t\tLIMIT 1", ARRAY_A);
    $output = '';
    if ($products != null) {
        foreach ($products as $product) {
            $attached_images = (array) get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $product['ID'], 'orderby' => 'menu_order', 'order' => 'ASC'));
            $attached_image = $attached_images[0];
            $output .= "<div class='wpsc_product_donation'>";
            if ($attached_image->ID > 0) {
                $output .= "<img src='" . wpsc_product_image($attached_image->ID, get_option('product_image_width'), get_option('product_image_height')) . "' title='" . $product['post_title'] . "' alt='" . esc_attr($product['post_title']) . "' /><br />";
            }
            // Get currency options
            $currency_sign_location = get_option('currency_sign_location');
            $currency_type = get_option('currency_type');
            WPSC_Countries::get_currency_symbol($currency_type);
            $price = get_post_meta($product['ID'], '_wpsc_price', true);
            // Output
            $output .= "<strong>" . $product['post_title'] . "</strong><br />";
            $output .= $product['post_content'] . "<br />";
            $output .= "<form class='product_form'  name='donation_widget_" . $product['ID'] . "' method='post' action='' id='donation_widget_" . $product['ID'] . "'>";
            $output .= "<input type='hidden' name='product_id' value='" . $product['ID'] . "'/>";
            $output .= "<input type='hidden' name='item' value='" . $product['ID'] . "' />";
            $output .= "<input type='hidden' name='wpsc_ajax_action' value='add_to_cart' />";
            $output .= "<label for='donation_widget_price_" . $product['ID'] . "'>" . __('Donation', 'wp-e-commerce') . ":</label> {$currency_symbol}<input type='text' id='donation_widget_price_" . $product['ID'] . "' name='donation_price' value='" . esc_attr(number_format($price, 2)) . "' size='6' /><br />";
            $output .= "<input type='submit' id='donation_widget_" . $product['ID'] . "_submit_button' name='Buy' class='wpsc_buy_button' value='" . __('Add To Cart', 'wp-e-commerce') . "' />";
            $output .= "</form>";
            $output .= "</div>";
        }
    } else {
        $output = '';
    }
    echo $output;
}
Example #6
0
/**
 * 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')
{
    $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);
    // Get ID of parent product if one exists
    if (!empty($product->post_parent)) {
        $product_id = $product->post_parent;
    }
    // Load image proportions if none were passed
    if ($width < 10 || $height < 10) {
        $width = get_option('product_image_width');
        $height = get_option('product_image_height');
    }
    // Use product thumbnail
    if (has_post_thumbnail($product_id) && 'single' != $page) {
        $thumbnail_id = get_post_thumbnail_id($product_id);
        // Use first product image
    } else {
        // Get all attached images to this product
        $attached_images = (array) get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $product_id, 'orderby' => 'menu_order', 'order' => 'ASC'));
        if (!empty($attached_images)) {
            $thumbnail_id = $attached_images[0]->ID;
        }
    }
    //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 = array($width, $height);
        }
        $src = wp_get_attachment_image_src($thumbnail_id, $custom_thumbnail);
        if (!empty($src) && is_string($src[0])) {
            return $src[0];
        }
    }
    // Return image link...
    if (isset($thumbnail_id) && ($image_link = wpsc_product_image($thumbnail_id, $width, $height))) {
        return $image_link;
    } else {
        return false;
    }
}
/**
 * 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;
}
Example #8
0
function wpsc_latest_product($args = null, $instance)
{
    global $wpdb;
    $args = wp_parse_args((array) $args, array('number' => 5));
    $siteurl = get_option('siteurl');
    $options = get_option('wpsc-widget_latest_products');
    $number = isset($instance['number']) ? (int) $instance['number'] : 5;
    $image = isset($instance['image']) ? (bool) $instance['image'] : FALSE;
    if (isset($instance['width'])) {
        $width = $instance['width'];
    }
    if (isset($instance['height'])) {
        $height = $instance['height'];
    }
    $latest_products = get_posts(array('post_type' => 'wpsc-product', 'numberposts' => $number, 'orderby' => 'post_date', 'post_parent' => 0, 'post_status' => 'publish', 'order' => 'DESC'));
    $output = '';
    if (count($latest_products) > 0) {
        foreach ($latest_products as $latest_product) {
            // Link
            $output .= '<a href="' . wpsc_product_url($latest_product->ID, null) . '" class="wpsc-product-title">' . stripslashes($latest_product->post_title) . '</a>';
            // Thumbnails, if required
            if ($image) {
                $output .= '<div class="item_image">';
                $output .= '<a href="' . wpsc_product_url($latest_product->ID, null) . '">';
                $attached_images = (array) get_posts(array('post_type' => 'attachment', 'numberposts' => 1, 'post_status' => null, 'post_parent' => $latest_product->ID, 'orderby' => 'menu_order', 'order' => 'ASC'));
                $attached_image = $attached_images[0];
                if ($attached_image->ID > 0) {
                    $output .= '<img src="' . wpsc_product_image($attached_image->ID, $width, $height) . '" title="' . $latest_product->post_title . '" alt="' . $latest_product->post_title . '" />';
                } else {
                    $output .= '<img class="no-image" id="product_image_' . wpsc_the_product_id() . '" alt="No Image" title="' . wpsc_the_product_title() . '" src="' . WPSC_URL . '/wpsc-theme/images/noimage.png" width="' . $width . '" height="' . $height . '" />';
                }
                $output .= '<span></span>';
                $output .= '</a>';
                $output .= '</div>';
            }
        }
    }
    echo $output;
}