From d765e2319cce31482cf443a2819914854408c4ad Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 9 Nov 2017 18:11:35 +0000 Subject: [PATCH] Added theme support vars for grid settings - product_grid - min_rows - max_rows - min_columns - max_columns If settings violate these, they will be updated. If loop_shop_columns is defined, settings are hidden with a notice. --- .../settings/class-wc-settings-products.php | 27 ++++++++++-------- includes/wc-core-functions.php | 8 +++--- includes/wc-template-functions.php | 28 ++++++++++++++++++- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/includes/admin/settings/class-wc-settings-products.php b/includes/admin/settings/class-wc-settings-products.php index eba6a8bb8aa..48a9be5898a 100644 --- a/includes/admin/settings/class-wc-settings-products.php +++ b/includes/admin/settings/class-wc-settings-products.php @@ -76,7 +76,9 @@ class WC_Settings_Products extends WC_Settings_Page { */ public function get_settings( $current_section = '' ) { if ( 'display' === $current_section ) { - $settings = array( + $theme_support = get_theme_support( 'woocommerce' ); + $theme_support = is_array( $theme_support ) ? $theme_support[0]: false; + $settings = array( array( 'title' => __( 'Shop & product pages', 'woocommerce' ), 'type' => 'title', @@ -141,7 +143,7 @@ class WC_Settings_Products extends WC_Settings_Page { ) ), 'desc_tip' => true, ), - array( + 'columns' => array( 'title' => __( 'Products per row', 'woocommerce' ), 'desc' => __( 'How many products should be shown per row?', 'woocommerce' ), 'id' => 'woocommerce_catalog_columns', @@ -149,13 +151,13 @@ class WC_Settings_Products extends WC_Settings_Page { 'type' => 'number', 'css' => 'width: 50px;', 'custom_attributes' => array( - 'min' => 0, - 'step' => 1, + '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'] ) : '', + 'step' => 1, ), 'desc_tip' => true, ), - - array( + 'rows' => array( 'title' => __( 'Rows per page', 'woocommerce' ), 'desc' => __( 'How many rows of products should be shown per page?', 'woocommerce' ), 'id' => 'woocommerce_catalog_rows', @@ -163,12 +165,12 @@ class WC_Settings_Products extends WC_Settings_Page { 'type' => 'number', 'css' => 'width: 50px;', 'custom_attributes' => array( - 'min' => 0, - 'step' => 1, + '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'] ): '', + 'step' => 1, ), 'desc_tip' => true, ), - array( 'title' => __( 'Add to cart behaviour', 'woocommerce' ), 'desc' => __( 'Redirect to the cart page after successful addition', 'woocommerce' ), @@ -190,8 +192,11 @@ class WC_Settings_Products extends WC_Settings_Page { ), ); - $theme_support = get_theme_support( 'woocommerce' ); - $theme_support = is_array( $theme_support ) ? $theme_support[0]: false; + if ( has_filter( 'loop_shop_columns' ) ) { + wc_deprecated_function( 'The loop_shop_columns filter', '3.3', 'the woocomerce add_theme_support declaration, or remove the filter and set in product settings' ); + unset( $settings['columns'], $settings['rows'] ); + } + $image_settings = array( array( 'title' => __( 'Product images', 'woocommerce' ), diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index e796925dd37..09892a13387 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -688,8 +688,8 @@ function wc_get_image_size( $image_size ) { $image_size = $size['width'] . '_' . $size['height']; } elseif ( in_array( $image_size, array( 'single', 'shop_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' ]; + if ( isset( $theme_support['single_image_width'] ) ) { + $size['width'] = $theme_support['single_image_width']; } else { $size['width'] = get_option( 'woocommerce_single_image_width', 600 ); } @@ -698,8 +698,8 @@ function wc_get_image_size( $image_size ) { $image_size = 'single'; } elseif ( in_array( $image_size, array( 'thumbnail', 'shop_thumbnail', 'shop_catalog' ), 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' ]; + if ( isset( $theme_support['thumbnail_image_width'] ) ) { + $size['width'] = $theme_support['thumbnail_image_width']; } else { $size['width'] = get_option( 'woocommerce_thumbnail_image_width', 300 ); } diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index aecd7a2dd69..6a57fc6fd69 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -289,6 +289,18 @@ function wc_product_cat_class( $class = '', $category = null ) { function wc_get_default_products_per_row() { $columns = get_option( 'woocommerce_catalog_columns', 3 ); + // 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']; + 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']; + update_option( 'woocommerce_catalog_columns', $columns ); + } + // Legacy filter. if ( has_filter( 'loop_shop_columns' ) ) { $columns = apply_filters( 'loop_shop_columns', $columns ); @@ -305,7 +317,21 @@ function wc_get_default_products_per_row() { * @return int */ function wc_get_default_product_rows_per_page() { - return absint( get_option( 'woocommerce_catalog_rows', 4 ) ); + $rows = absint( get_option( 'woocommerce_catalog_rows', 4 ) ); + + // 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']; + 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']; + update_option( 'woocommerce_catalog_rows', $rows ); + } + + return $rows; } /**