Settings API tweaks.

Uses same names as the settings API for gateways which Closes #1848.
Allows a single level of array based IDs for options which Closes #1826.
Related to #1826 this is now used for image size settings.
This commit is contained in:
Mike Jolley 2012-11-27 15:37:01 +00:00
parent 46ca015e4f
commit 0361a2f0cb
14 changed files with 407 additions and 295 deletions

File diff suppressed because it is too large Load Diff

View File

@ -23,13 +23,20 @@ function woocommerce_update_options( $options ) {
if ( empty( $_POST ) )
return false;
// Options to update will be stored here
$update_options = array();
// Loop options and get values to save
foreach ( $options as $value ) {
if ( ! isset( $value['id'] ) )
continue;
$type = isset( $value['type'] ) ? sanitize_title( $value['type'] ) : '';
// Get the option name
$option_value = null;
switch ( $type ) {
@ -37,9 +44,9 @@ function woocommerce_update_options( $options ) {
case "checkbox" :
if ( isset( $_POST[$value['id']] ) ) {
update_option( $value['id'], 'yes' );
$option_value = 'yes';
} else {
update_option( $value['id'], 'no' );
$option_value = 'no';
}
break;
@ -47,9 +54,9 @@ function woocommerce_update_options( $options ) {
case "textarea" :
if ( isset( $_POST[$value['id']] ) ) {
update_option( $value['id'], wp_kses_post( $_POST[ $value['id'] ] ) );
$option_value = wp_kses_post( $_POST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
$option_value = '';
}
break;
@ -67,17 +74,17 @@ function woocommerce_update_options( $options ) {
// price separators get a special treatment as they should allow a spaces (don't trim)
if ( isset( $_POST[ $value['id'] ] ) ) {
update_option( $value['id'], esc_attr( $_POST[ $value['id'] ] ) );
$option_value = esc_attr( $_POST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
$option_value = '';
}
} else {
if ( isset( $_POST[$value['id']] ) ) {
update_option( $value['id'], woocommerce_clean( $_POST[ $value['id'] ] ) );
$option_value = woocommerce_clean( $_POST[ $value['id'] ] );
} else {
delete_option( $value['id'] );
$option_value = '';
}
}
@ -137,8 +144,8 @@ function woocommerce_update_options( $options ) {
);
}
}
update_option( 'woocommerce_tax_rates', $tax_rates );
$update_options[ 'woocommerce_tax_rates' ] = $tax_rates;
// Local tax rates saving
$local_tax_rates = array();
@ -196,8 +203,8 @@ function woocommerce_update_options( $options ) {
);
}
}
update_option( 'woocommerce_local_tax_rates', $local_tax_rates );
$update_options[ 'woocommerce_local_tax_rates' ] = $local_tax_rates;
break;
@ -209,26 +216,26 @@ function woocommerce_update_options( $options ) {
else
$selected_countries = array();
update_option( $value['id'], $selected_countries );
$option_value = $selected_countries;
break;
case "image_width" :
if ( isset( $_POST[$value['id'] . '_width'] ) ) {
if ( isset( $_POST[$value['id'] ]['width'] ) ) {
update_option( $value['id'] . '_width', woocommerce_clean( $_POST[ $value['id'] . '_width'] ) );
update_option( $value['id'] . '_height', woocommerce_clean( $_POST[ $value['id'] . '_height'] ) );
$update_options[ $value['id'] ]['width'] = woocommerce_clean( $_POST[$value['id'] ]['width'] );
$update_options[ $value['id'] ]['height'] = woocommerce_clean( $_POST[$value['id'] ]['height'] );
if ( isset( $_POST[ $value['id'] . '_crop'] ) )
update_option( $value['id'] . '_crop', 1 );
if ( isset( $_POST[ $value['id'] ]['crop'] ) )
$update_options[ $value['id'] ]['crop'] = 1;
else
update_option( $value['id'].'_crop', 0 );
$update_options[ $value['id'] ]['crop'] = 0;
} else {
update_option( $value['id'] . '_width', $value['std'] );
update_option( $value['id'] . '_height', $value['std'] );
update_option( $value['id'] . '_crop', 1 );
$update_options[ $value['id'] ]['width'] = $value['default']['width'];
$update_options[ $value['id'] ]['height'] = $value['default']['height'];
$update_options[ $value['id'] ]['crop'] = $value['default']['crop'];
}
break;
@ -242,9 +249,40 @@ function woocommerce_update_options( $options ) {
}
if ( ! is_null( $option_value ) ) {
// Check if option is an array
if ( strstr( $value['id'], '[' ) ) {
parse_str( $value['id'], $option_array );
// Option name is first key
$option_name = current( array_keys( $option_array ) );
// Get old option value
if ( ! isset( $update_options[ $option_name ] ) )
$update_options[ $option_name ] = get_option( $option_name, array() );
if ( ! is_array( $update_options[ $option_name ] ) )
$update_options[ $option_name ] = array();
// Set keys and value
$key = key( $option_array[ $option_name ] );
$update_options[ $option_name ][ $key ] = $option_value;
// Single value
} else {
$update_options[ $value['id'] ] = $option_value;
}
}
// Custom handling
do_action( 'woocommerce_update_option', $value );
}
// Now save the options
foreach( $update_options as $name => $value )
update_option( $name, $value, true );
return true;
}

View File

@ -111,14 +111,8 @@ function woocommerce_default_options() {
foreach ( $woocommerce_settings as $section ) {
foreach ( $section as $value ) {
if ( isset( $value['std'] ) && isset( $value['id'] ) ) {
if ( $value['type'] == 'image_width' ) {
add_option( $value['id'] . '_width', $value['std'] );
add_option( $value['id'] . '_height', $value['std'] );
add_option( $value['id'] . '_crop', 1 );
} else {
add_option( $value['id'], $value['std'] );
}
if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
add_option( $value['id'], $value['default'] );
}
}
}

View File

@ -490,6 +490,45 @@ if ( ! function_exists( 'woocommerce_settings' ) ) {
}
/**
* Get a setting from the settings API.
*
* @access public
* @param mixed $option
* @return void
*/
function woocommerce_settings_get_option( $option_name, $default = '' ) {
// Array value
if ( strstr( $option_name, '[' ) ) {
parse_str( $option_name, $option_array );
// Option name is first key
$option_name = current( array_keys( $option_array ) );
// Get value
$option_values = get_option( $option_name, '' );
$key = key( $option_array[ $option_name ] );
if ( isset( $option_values[ $key ] ) )
$option_value = $option_values[ $key ];
else
$option_value = null;
// Single value
} else {
$option_value = get_option( $option_name, null );
}
if ( is_array( $option_value ) )
$option_value = array_map( 'stripslashes', $option_value );
elseif ( $option_value )
$option_value = stripslashes( $option_value );
return $option_value == null ? $default : $option_value;
}
/**
* Output admin fields.
*
@ -505,10 +544,10 @@ function woocommerce_admin_fields( $options ) {
foreach ( $options as $value ) {
if ( ! isset( $value['type'] ) ) continue;
if ( ! isset( $value['id'] ) ) $value['id'] = '';
if ( ! isset( $value['name'] ) ) $value['name'] = '';
if ( ! isset( $value['title'] ) ) $value['title'] = '';
if ( ! isset( $value['class'] ) ) $value['class'] = '';
if ( ! isset( $value['css'] ) ) $value['css'] = '';
if ( ! isset( $value['std'] ) ) $value['std'] = '';
if ( ! isset( $value['default'] ) ) $value['default'] = '';
if ( ! isset( $value['desc'] ) ) $value['desc'] = '';
if ( ! isset( $value['desc_tip'] ) ) $value['desc_tip'] = false;
@ -550,7 +589,7 @@ function woocommerce_admin_fields( $options ) {
// Section Titles
case 'title':
if ( ! empty( $value['name'] ) ) echo '<h3>' . esc_html( $value['name'] ) . '</h3>';
if ( ! empty( $value['title'] ) ) echo '<h3>' . esc_html( $value['title'] ) . '</h3>';
if ( ! empty( $value['desc'] ) ) echo wpautop( wptexturize( wp_kses_post( $value['desc'] ) ) );
echo '<table class="form-table">'. "\n\n";
if ( ! empty( $value['id'] ) ) do_action( 'woocommerce_settings_' . sanitize_title( $value['id'] ) );
@ -571,7 +610,7 @@ function woocommerce_admin_fields( $options ) {
$type = $value['type'];
$class = '';
$option_value = get_option( $value['id'], null ) !== null ? esc_attr( stripslashes( get_option($value['id'] ) ) ) : esc_attr( $value['std'] );
$option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
if ( $value['type'] == 'color' ) {
$type = 'text';
@ -581,7 +620,7 @@ function woocommerce_admin_fields( $options ) {
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['name'] ); ?></label>
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
@ -590,7 +629,7 @@ function woocommerce_admin_fields( $options ) {
id="<?php echo esc_attr( $value['id'] ); ?>"
type="<?php echo esc_attr( $type ); ?>"
style="<?php echo esc_attr( $value['css'] ); ?>"
value="<?php echo $option_value; ?>"
value="<?php echo esc_attr( $option_value ); ?>"
class="<?php echo esc_attr( $value['class'] ); ?>"
<?php echo implode( ' ', $custom_attributes ); ?>
/> <?php echo $description; ?>
@ -600,9 +639,12 @@ function woocommerce_admin_fields( $options ) {
// Textarea
case 'textarea':
$option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['name'] ); ?></label>
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
@ -614,12 +656,7 @@ function woocommerce_admin_fields( $options ) {
style="<?php echo esc_attr( $value['css'] ); ?>"
class="<?php echo esc_attr( $value['class'] ); ?>"
<?php echo implode( ' ', $custom_attributes ); ?>
><?php
if ( false !== get_option($value['id'] ) )
echo esc_textarea( stripslashes( get_option( $value['id'] ) ) );
else
echo esc_textarea( $value['std'] );
?></textarea>
><?php echo esc_textarea( $option_value ); ?></textarea>
</td>
</tr><?php
break;
@ -628,13 +665,11 @@ function woocommerce_admin_fields( $options ) {
case 'select' :
case 'multiselect' :
$option_value = get_option( $value['id'] );
if ( ! $option_value )
$option_value = $value['std'];
$option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['name'] ); ?></label>
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
@ -668,13 +703,11 @@ function woocommerce_admin_fields( $options ) {
// Radio inputs
case 'radio' :
$option_value = get_option( $value['id'] );
if ( ! $option_value )
$option_value = $value['std'];
$option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['name'] ); ?></label>
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp forminp-<?php echo sanitize_title( $value['type'] ) ?>">
@ -706,6 +739,8 @@ function woocommerce_admin_fields( $options ) {
// Checkbox input
case 'checkbox' :
$option_value = woocommerce_settings_get_option( $value['id'], $value['default'] );
if ( ! isset( $value['hide_if_checked'] ) ) $value['hide_if_checked'] = false;
if ( ! isset( $value['show_if_checked'] ) ) $value['show_if_checked'] = false;
@ -717,7 +752,7 @@ function woocommerce_admin_fields( $options ) {
if ( $value['hide_if_checked'] == 'option' ) echo 'hide_options_if_checked';
if ( $value['show_if_checked'] == 'option' ) echo 'show_options_if_checked';
?>">
<th scope="row" class="titledesc"><?php echo esc_html( $value['name'] ) ?></th>
<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
<td class="forminp forminp-checkbox">
<fieldset>
<?php
@ -732,7 +767,7 @@ function woocommerce_admin_fields( $options ) {
}
?>
<legend class="screen-reader-text"><span><?php echo esc_html( $value['name'] ) ?></span></legend>
<legend class="screen-reader-text"><span><?php echo esc_html( $value['title'] ) ?></span></legend>
<label for="<?php echo $value['id'] ?>">
<input
@ -740,7 +775,7 @@ function woocommerce_admin_fields( $options ) {
id="<?php echo esc_attr( $value['id'] ); ?>"
type="checkbox"
value="1"
<?php checked(get_option($value['id']), 'yes'); ?>
<?php checked( $option_value, 'yes'); ?>
<?php echo implode( ' ', $custom_attributes ); ?>
/> <?php echo wp_kses_post( $value['desc'] ) ?></label> <?php echo $tip; ?><br />
<?php
@ -762,19 +797,19 @@ function woocommerce_admin_fields( $options ) {
// Image width settings
case 'image_width' :
$width = get_option( $value['id'] . '_width', null ) !== null ? esc_attr( stripslashes( get_option( $value['id'] . '_width' ) ) ) : esc_attr( $value['std'] );
$height = get_option( $value['id'] . '_height', null ) !== null ? esc_attr( stripslashes( get_option( $value['id'] . '_height' ) ) ) : esc_attr( $value['std'] );
$crop = get_option( $value['id'] . '_crop', null ) !== null ? checked( get_option( $value['id'] . '_crop'), 1, false ) : checked( 1, 1, false );
$width = woocommerce_settings_get_option( $value['id'] . '[width]', $value['default']['width'] );
$height = woocommerce_settings_get_option( $value['id'] . '[height]', $value['default']['height'] );
$crop = checked( 1, woocommerce_settings_get_option( $value['id'] . '[crop]', $value['default']['crop'] ), false );
?><tr valign="top">
<th scope="row" class="titledesc"><?php echo esc_html( $value['name'] ) ?></th>
<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?></th>
<td class="forminp">
<?php _e( 'Width', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>_width" id="<?php echo esc_attr( $value['id'] ); ?>_width" type="text" size="3" value="<?php echo $width; ?>" />
<?php _e( 'Width', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[width]" id="<?php echo esc_attr( $value['id'] ); ?>-width" type="text" size="3" value="<?php echo $width; ?>" />
<?php _e( 'Height', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>_height" id="<?php echo esc_attr( $value['id'] ); ?>_height" type="text" size="3" value="<?php echo $height; ?>" />
<?php _e( 'Height', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[height]" id="<?php echo esc_attr( $value['id'] ); ?>-height" type="text" size="3" value="<?php echo $height; ?>" />
<label><?php _e( 'Hard Crop', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>_crop" id="<?php echo esc_attr( $value['id'] ); ?>_crop" type="checkbox" <?php echo $crop; ?> /></label>
<label><?php _e( 'Hard Crop', 'woocommerce' ); ?> <input name="<?php echo esc_attr( $value['id'] ); ?>[crop]" id="<?php echo esc_attr( $value['id'] ); ?>-crop" type="checkbox" <?php echo $crop; ?> /></label>
<?php echo $description; ?></td>
</tr><?php
@ -782,22 +817,22 @@ function woocommerce_admin_fields( $options ) {
// Single page selects
case 'single_select_page' :
$args = array( 'name' => $value['id'],
$args = array( 'title' => $value['id'],
'id' => $value['id'],
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
'show_option_none' => ' ',
'class' => $value['class'],
'echo' => false,
'selected' => absint( get_option( $value['id'] ) )
'selected' => absint( woocommerce_settings_get_option( $value['id'] ) )
);
if( isset( $value['args'] ) )
$args = wp_parse_args( $value['args'], $args );
?><tr valign="top" class="single_select_page">
<th scope="row" class="titledesc"><?php echo esc_html( $value['name'] ) ?> <?php echo $tip; ?></th>
<th scope="row" class="titledesc"><?php echo esc_html( $value['title'] ) ?> <?php echo $tip; ?></th>
<td class="forminp">
<?php echo str_replace(' id=', " data-placeholder='" . __( 'Select a page&hellip;', 'woocommerce' ) . "' style='" . $value['css'] . "' class='" . $value['class'] . "' id=", wp_dropdown_pages( $args ) ); ?> <?php echo $description; ?>
</td>
@ -806,8 +841,10 @@ function woocommerce_admin_fields( $options ) {
// Single country selects
case 'single_select_country' :
$country_setting = (string) woocommerce_settings_get_option( $value['id'] );
$countries = $woocommerce->countries->countries;
$country_setting = (string) get_option($value['id']);
if (strstr($country_setting, ':')) :
$country = current(explode(':', $country_setting));
$state = end(explode(':', $country_setting));
@ -817,7 +854,7 @@ function woocommerce_admin_fields( $options ) {
endif;
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['name'] ); ?></label>
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp"><select name="<?php echo esc_attr( $value['id'] ); ?>" style="<?php echo esc_attr( $value['css'] ); ?>" data-placeholder="<?php _e( 'Choose a country&hellip;', 'woocommerce' ); ?>" title="Country" class="chosen_select">
@ -829,12 +866,14 @@ function woocommerce_admin_fields( $options ) {
// Country multiselects
case 'multi_select_countries' :
$selections = (array) woocommerce_settings_get_option( $value['id'] );
$countries = $woocommerce->countries->countries;
asort( $countries );
$selections = (array) get_option( $value['id'] );
?><tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['name'] ); ?></label>
<label for="<?php echo esc_attr( $value['id'] ); ?>"><?php echo esc_html( $value['title'] ); ?></label>
<?php echo $tip; ?>
</th>
<td class="forminp">

View File

@ -1184,7 +1184,7 @@ abstract class WC_Product {
} elseif ( ( $parent_id = wp_get_post_parent_id( $this->id ) ) && has_post_thumbnail( $parent_id ) ) {
$image = get_the_post_thumbnail( $parent_id, $size );
} else {
$image = '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="' . $woocommerce->get_image_size( 'shop_thumbnail_image_width' ) . '" height="' . $woocommerce->get_image_size( 'shop_thumbnail_image_height' ) . '" />';
$image = woocommerce_placeholder_img( $size );
}
return $image;

View File

@ -297,7 +297,7 @@ class WC_Product_Variation extends WC_Product {
} elseif ( $parent_id = wp_get_post_parent_id( $this->id ) && has_post_thumbnail( $parent_id ) ) {
$image = get_the_post_thumbnail( $parent_id, $size );
} else {
$image = '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="' . $woocommerce->get_image_size( 'shop_thumbnail_image_width' ) . '" height="' . $woocommerce->get_image_size( 'shop_thumbnail_image_height' ) . '" />';
$image = woocommerce_placeholder_img( $size );
}
return $image;

View File

@ -108,7 +108,12 @@ class WooCommerce_Widget_Best_Sellers extends WP_Widget {
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php
if ( has_post_thumbnail() )
the_post_thumbnail( 'shop_thumbnail' );
else
echo woocommerce_placeholder_img( 'shop_thumbnail' );
?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>

View File

@ -135,7 +135,12 @@ class WooCommerce_Widget_On_Sale extends WP_Widget {
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php
if ( has_post_thumbnail() )
the_post_thumbnail( 'shop_thumbnail' );
else
echo woocommerce_placeholder_img( 'shop_thumbnail' );
?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>

View File

@ -77,12 +77,12 @@ class WooCommerce_Widget_Random_Products extends WP_Widget {
<?php while ($query->have_posts()) : $query->the_post(); global $product; ?>
<li>
<a href="<?php the_permalink() ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('shop_thumbnail');
} else {
echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size( 'shop_thumbnail_image_width' ).'" height="'.$woocommerce->get_image_size( 'shop_thumbnail_image_height' ).'" />';
} ?>
<?php
if ( has_post_thumbnail() )
the_post_thumbnail( 'shop_thumbnail' );
else
echo woocommerce_placeholder_img( 'shop_thumbnail' );
?>
<?php the_title() ?>
</a>
<?php echo $product->get_price_html() ?>

View File

@ -97,7 +97,12 @@ class WooCommerce_Widget_Recent_Products extends WP_Widget {
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php
if ( has_post_thumbnail() )
the_post_thumbnail( 'shop_thumbnail' );
else
echo woocommerce_placeholder_img( 'shop_thumbnail' );
?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>

View File

@ -92,7 +92,12 @@ class WooCommerce_Widget_Recently_Viewed extends WP_Widget {
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php
if ( has_post_thumbnail() )
the_post_thumbnail( 'shop_thumbnail' );
else
echo woocommerce_placeholder_img( 'shop_thumbnail' );
?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>

View File

@ -137,7 +137,7 @@ function woocommerce_get_weight( $weight, $to_unit ) {
/**
* Get the placeholder for products etc
* Get the placeholder image URL for products etc
*
* @access public
* @return string
@ -149,6 +149,21 @@ function woocommerce_placeholder_img_src() {
}
/**
* Get the placeholder image
*
* @access public
* @return string
*/
function woocommerce_placeholder_img( $size = 'shop_thumbnail' ) {
global $woocommerce;
$dimensions = $woocommerce->get_image_size( $size );
return apply_filters('woocommerce_placeholder_img', '<img src="' . woocommerce_placeholder_img_src() . '" alt="Placeholder" width="' . $dimensions['width'] . '" height="' . $dimensions['height'] . '" />' );
}
/**
* Send HTML emails from WooCommerce
*

View File

@ -394,17 +394,12 @@ if ( ! function_exists( 'woocommerce_get_product_thumbnail' ) ) {
* @return string
*/
function woocommerce_get_product_thumbnail( $size = 'shop_catalog', $placeholder_width = 0, $placeholder_height = 0 ) {
global $post, $woocommerce;
if ( ! $placeholder_width )
$placeholder_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
if ( ! $placeholder_height )
$placeholder_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );
global $post;
if ( has_post_thumbnail() )
return get_the_post_thumbnail( $post->ID, $size );
elseif ( woocommerce_placeholder_img_src() )
return '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="' . $placeholder_width . '" height="' . $placeholder_height . '" />';
return woocommerce_placeholder_img( $size );
}
}
@ -1176,14 +1171,12 @@ if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
* @subpackage Loop
* @return void
*/
function woocommerce_subcategory_thumbnail( $category ) {
function woocommerce_subcategory_thumbnail( $category ) {
global $woocommerce;
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
$image_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
$image_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
$dimensions = $woocommerce->get_image_size( $small_thumbnail_size );
$thumbnail_id = get_woocommerce_term_meta( $category->term_id, 'thumbnail_id', true );
if ( $thumbnail_id ) {
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
@ -1193,7 +1186,7 @@ if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
}
if ( $image )
echo '<img src="' . $image . '" alt="' . $category->name . '" width="' . $image_width . '" height="' . $image_height . '" />';
echo '<img src="' . $image . '" alt="' . $category->name . '" width="' . $dimensions['width'] . '" height="' . $dimensions['height'] . '" />';
}
}

View File

@ -1002,14 +1002,13 @@ class Woocommerce {
* @return void
*/
function init_image_sizes() {
// Image sizes
$shop_thumbnail_crop = get_option('woocommerce_thumbnail_image_crop') == 1 ? true : false;
$shop_catalog_crop = get_option('woocommerce_catalog_image_crop') == 1 ? true : false;
$shop_single_crop = get_option('woocommerce_single_image_crop') == 1 ? true : false;
$shop_thumbnail = $this->get_image_size( 'shop_thumbnail' );
$shop_catalog = $this->get_image_size( 'shop_catalog' );
$shop_single = $this->get_image_size( 'shop_single' );
add_image_size( 'shop_thumbnail', $this->get_image_size('shop_thumbnail_image_width'), $this->get_image_size('shop_thumbnail_image_height'), $shop_thumbnail_crop );
add_image_size( 'shop_catalog', $this->get_image_size('shop_catalog_image_width'), $this->get_image_size('shop_catalog_image_height'), $shop_catalog_crop );
add_image_size( 'shop_single', $this->get_image_size('shop_single_image_width'), $this->get_image_size('shop_single_image_height'), $shop_single_crop );
add_image_size( 'shop_thumbnail', $shop_thumbnail['width'], $shop_thumbnail['height'], $shop_thumbnail['crop'] );
add_image_size( 'shop_catalog', $shop_catalog['width'], $shop_catalog['height'], $shop_catalog['crop'] );
add_image_size( 'shop_single', $shop_single['width'], $shop_single['height'], $shop_single['crop'] );
}
@ -1276,16 +1275,18 @@ class Woocommerce {
* @return string
*/
function get_image_size( $image_size ) {
$return = '';
switch ( $image_size ) {
case "shop_thumbnail_image_width" : $return = get_option('woocommerce_thumbnail_image_width'); break;
case "shop_thumbnail_image_height" : $return = get_option('woocommerce_thumbnail_image_height'); break;
case "shop_catalog_image_width" : $return = get_option('woocommerce_catalog_image_width'); break;
case "shop_catalog_image_height" : $return = get_option('woocommerce_catalog_image_height'); break;
case "shop_single_image_width" : $return = get_option('woocommerce_single_image_width'); break;
case "shop_single_image_height" : $return = get_option('woocommerce_single_image_height'); break;
}
return apply_filters( 'woocommerce_get_image_size_' . $image_size, $return );
// Only return sizes we define in settings
if ( ! in_array( $image_size, array( 'shop_thumbnail', 'shop_catalog', 'shop_single' ) ) )
return apply_filters( 'woocommerce_get_image_size_' . $image_size, '' );
$size = get_option( $image_size . '_image', array() );
$size['width'] = isset( $size['width'] ) ? $size['width'] : '300';
$size['height'] = isset( $size['height'] ) ? $size['height'] : '300';
$size['crop'] = isset( $size['crop'] ) ? $size['crop'] : 1;
return apply_filters( 'woocommerce_get_image_size_' . $image_size, $size );
}
/** Messages ****************************************************************/