diff --git a/includes/class-wc-regenerate-images.php b/includes/class-wc-regenerate-images.php index cb78ddc1b3f..66d7d20c59b 100644 --- a/includes/class-wc-regenerate-images.php +++ b/includes/class-wc-regenerate-images.php @@ -169,16 +169,10 @@ class WC_Regenerate_Images { } /** - * Check if we should generate images when new themes declares custom sizes - * - * @return void + * Check if we should generate images when new themes declares custom sizes. */ public static function maybe_regenerate_image_theme_switch() { - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; - - // Only queue image generation if the theme declares custom sizes via theme_support. - if ( is_array( $theme_support ) && ( isset( $theme_support['single_image_width'] ) || isset( $theme_support['thumbnail_image_width'] ) ) ) { + if ( wc_get_theme_support( 'single_image_width' ) || wc_get_theme_support( 'thumbnail_image_width' ) ) { self::queue_image_regeneration(); } } diff --git a/includes/customizer/class-wc-shop-customizer.php b/includes/customizer/class-wc-shop-customizer.php index e7cd6d13b5c..a00dd214015 100644 --- a/includes/customizer/class-wc-shop-customizer.php +++ b/includes/customizer/class-wc-shop-customizer.php @@ -77,6 +77,10 @@ class WC_Shop_Customizer { * Scripts to improve our form. */ public function add_scripts() { + $min_rows = wc_get_theme_support( 'product_grid::min_rows', 1 ); + $max_rows = wc_get_theme_support( 'product_grid::max_rows', '' ); + $min_columns = wc_get_theme_support( 'product_grid::min_columns', 1 ); + $max_columns = wc_get_theme_support( 'product_grid::max_columns', '' ); ?> add_section( 'woocommerce_product_catalog', array( @@ -333,8 +396,8 @@ class WC_Shop_Customizer { 'settings' => 'woocommerce_catalog_columns', 'type' => 'number', 'input_attrs' => array( - 'min' => isset( $theme_support['product_grid']['min_columns'] ) ? absint( $theme_support['product_grid']['min_columns'] ) : 1, - 'max' => isset( $theme_support['product_grid']['max_columns'] ) ? absint( $theme_support['product_grid']['max_columns'] ) : '', + 'min' => wc_get_theme_support( 'product_grid::min_columns', 1 ), + 'max' => wc_get_theme_support( 'product_grid::max_columns', '' ), 'step' => 1, ), ) @@ -360,8 +423,8 @@ class WC_Shop_Customizer { 'settings' => 'woocommerce_catalog_rows', 'type' => 'number', 'input_attrs' => array( - 'min' => isset( $theme_support['product_grid']['min_rows'] ) ? absint( $theme_support['product_grid']['min_rows'] ) : 1, - 'max' => isset( $theme_support['product_grid']['max_rows'] ) ? absint( $theme_support['product_grid']['max_rows'] ) : '', + 'min' => wc_get_theme_support( 'product_grid::min_rows', 1 ), + 'max' => wc_get_theme_support( 'product_grid::max_rows', '' ), 'step' => 1, ), ) @@ -374,9 +437,6 @@ class WC_Shop_Customizer { * @param WP_Customize_Manager $wp_customize Theme Customizer object. */ private function add_product_images_section( $wp_customize ) { - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; - $wp_customize->add_section( 'woocommerce_product_images', array( @@ -387,7 +447,7 @@ class WC_Shop_Customizer { ) ); - if ( ! isset( $theme_support['single_image_width'] ) ) { + if ( ! wc_get_theme_support( 'single_image_width' ) ) { $wp_customize->add_setting( 'woocommerce_single_image_width', array( @@ -415,7 +475,7 @@ class WC_Shop_Customizer { ); } - if ( ! isset( $theme_support['thumbnail_image_width'] ) ) { + if ( ! wc_get_theme_support( 'thumbnail_image_width' ) ) { $wp_customize->add_setting( 'woocommerce_thumbnail_image_width', array( diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index da5b7e042c8..4c820344c79 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -660,6 +660,49 @@ function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r $mailer->send( $to, $subject, $message, $headers, $attachments ); } +/** + * Return "theme support" values from the current theme, if set. + * + * @since 3.3.0 + * @param string $prop Name of prop (or key::subkey for arrays of props) if you want a specific value. Leave blank to get all props as an array. + * @param mixed $default Optional value to return if the theme does not declare support for a prop. + * @return mixed Value of prop(s). + */ +function wc_get_theme_support( $prop = '', $default = null ) { + $theme_support = get_theme_support( 'woocommerce' ); + $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; + + if ( ! $theme_support ) { + return $default; + } + + if ( $prop ) { + $prop_stack = explode( '::', $prop ); + $prop_key = array_shift( $prop_stack ); + + if ( isset( $theme_support[ $prop_key ] ) ) { + $value = $theme_support[ $prop_key ]; + + if ( count( $prop_stack ) ) { + foreach ( $prop_stack as $prop_key ) { + if ( is_array( $value ) && isset( $value[ $prop_key ] ) ) { + $value = $value[ $prop_key ]; + } else { + $value = $default; + break; + } + } + } + } else { + $value = $default; + } + + return $value; + } + + return $theme_support; +} + /** * Get an image size by name or defined dimensions. * @@ -673,9 +716,7 @@ function wc_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r * @return array Array of dimensions including width, height, and cropping mode. Cropping mode is 0 for no crop, and 1 for hard crop. */ function wc_get_image_size( $image_size ) { - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; - $size = array( + $size = array( 'width' => 600, 'height' => 600, 'crop' => 1, @@ -689,24 +730,13 @@ function wc_get_image_size( $image_size ) { ); $image_size = $size['width'] . '_' . $size['height']; } elseif ( in_array( $image_size, array( 'single', 'shop_single', 'woocommerce_single' ), true ) ) { - // If the theme supports woocommerce, take image sizes from that definition. - if ( isset( $theme_support['single_image_width'] ) ) { - $size['width'] = $theme_support['single_image_width']; - } else { - $size['width'] = get_option( 'woocommerce_single_image_width', 600 ); - } + $size['width'] = wc_get_theme_support( 'single_image_width', get_option( 'woocommerce_single_image_width', 600 ) ); $size['height'] = 9999999999; $size['crop'] = 0; $image_size = 'single'; } elseif ( in_array( $image_size, array( 'thumbnail', 'shop_thumbnail', 'shop_catalog', 'woocommerce_thumbnail' ), true ) ) { - // If the theme supports woocommerce, take image sizes from that definition. - if ( isset( $theme_support['thumbnail_image_width'] ) ) { - $size['width'] = $theme_support['thumbnail_image_width']; - } else { - $size['width'] = get_option( 'woocommerce_thumbnail_image_width', 300 ); - } - - $cropping = get_option( 'woocommerce_thumbnail_cropping', '1:1' ); + $size['width'] = wc_get_theme_support( 'thumbnail_image_width', get_option( 'woocommerce_thumbnail_image_width', 300 ) ); + $cropping = get_option( 'woocommerce_thumbnail_cropping', '1: 1' ); if ( 'uncropped' === $cropping ) { $size['height'] = 9999999999; diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index 652be50a71f..a4615cc9bb1 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -310,22 +310,20 @@ function wc_product_cat_class( $class = '', $category = null ) { * @return int */ function wc_get_default_products_per_row() { - $columns = get_option( 'woocommerce_catalog_columns', 3 ); + $columns = get_option( 'woocommerce_catalog_columns', 3 ); + $product_grid = wc_get_theme_support( 'product_grid' ); + $min_columns = isset( $product_grid['min_columns'] ) ? absint( $product_grid['min_columns'] ) : 0; + $max_columns = isset( $product_grid['max_columns'] ) ? absint( $product_grid['max_columns'] ) : 0; - // Theme support. - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; - - if ( isset( $theme_support['product_grid']['min_columns'] ) && $columns < $theme_support['product_grid']['min_columns'] ) { - $columns = $theme_support['product_grid']['min_columns']; + if ( $min_columns && $columns < $min_columns ) { + $columns = $min_columns; update_option( 'woocommerce_catalog_columns', $columns ); - } elseif ( ! empty( $theme_support['product_grid']['max_columns'] ) && $columns > $theme_support['product_grid']['max_columns'] ) { - $columns = $theme_support['product_grid']['max_columns']; + } elseif ( $max_columns && $columns > $max_columns ) { + $columns = $max_columns; update_option( 'woocommerce_catalog_columns', $columns ); } - // Legacy filter. - if ( has_filter( 'loop_shop_columns' ) ) { + if ( has_filter( 'loop_shop_columns' ) ) { // Legacy filter handling. $columns = apply_filters( 'loop_shop_columns', $columns ); } @@ -339,17 +337,16 @@ function wc_get_default_products_per_row() { * @return int */ function wc_get_default_product_rows_per_page() { - $rows = absint( get_option( 'woocommerce_catalog_rows', 4 ) ); + $rows = absint( get_option( 'woocommerce_catalog_rows', 4 ) ); + $product_grid = wc_get_theme_support( 'product_grid' ); + $min_rows = isset( $product_grid['min_rows'] ) ? absint( $product_grid['min_rows'] ): 0; + $max_rows = isset( $product_grid['max_rows'] ) ? absint( $product_grid['max_rows'] ): 0; - // Theme support. - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; - - if ( isset( $theme_support['product_grid']['min_rows'] ) && $rows < $theme_support['product_grid']['min_rows'] ) { - $rows = $theme_support['product_grid']['min_rows']; + if ( $min_rows && $rows < $min_rows ) { + $rows = $min_rows; update_option( 'woocommerce_catalog_rows', $rows ); - } elseif ( ! empty( $theme_support['product_grid']['max_rows'] ) && $rows > $theme_support['product_grid']['max_rows'] ) { - $rows = $theme_support['product_grid']['max_rows']; + } elseif ( $max_rows && $rows > $max_rows ) { + $rows = $max_rows; update_option( 'woocommerce_catalog_rows', $rows ); } @@ -362,19 +359,14 @@ function wc_get_default_product_rows_per_page() { * @since 3.3.0 */ function wc_reset_product_grid_settings() { - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0] : false; + $product_grid = wc_get_theme_support( 'product_grid' ); - if ( isset( $theme_support['product_grid']['default_rows'] ) ) { - update_option( 'woocommerce_catalog_rows', absint( $theme_support['product_grid']['default_rows'] ) ); - } else { - delete_option( 'woocommerce_catalog_rows' ); + if ( ! empty( $product_grid['default_rows'] ) ) { + update_option( 'woocommerce_catalog_rows', absint( $product_grid['default_rows'] ) ); } - if ( isset( $theme_support['product_grid']['default_columns'] ) ) { - update_option( 'woocommerce_catalog_columns', absint( $theme_support['product_grid']['default_columns'] ) ); - } else { - delete_option( 'woocommerce_catalog_columns' ); + if ( ! empty( $product_grid['default_columns'] ) ) { + update_option( 'woocommerce_catalog_columns', absint( $product_grid['default_columns'] ) ); } } add_action( 'after_switch_theme', 'wc_reset_product_grid_settings' );