Merge branch 'master' into update/uncategorized-improvements

This commit is contained in:
Mike Jolley 2018-01-31 20:28:37 +00:00
commit ca4f0cf0c8
2 changed files with 107 additions and 45 deletions

View File

@ -31,14 +31,22 @@ class WC_Template_Loader {
*/
private static $in_content_filter = false;
/**
* Is WooCommerce support defined?
*
* @var boolean
*/
private static $theme_support = false;
/**
* Hook in methods.
*/
public static function init() {
self::$shop_page_id = wc_get_page_id( 'shop' );
self::$theme_support = current_theme_supports( 'woocommerce' );
self::$shop_page_id = wc_get_page_id( 'shop' );
// Supported themes.
if ( current_theme_supports( 'woocommerce' ) ) {
if ( self::$theme_support ) {
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
} else {
@ -103,7 +111,7 @@ class WC_Template_Loader {
$default_file = 'archive-product.php';
}
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
$default_file = current_theme_supports( 'woocommerce' ) ? 'archive-product.php' : '';
$default_file = self::$theme_support ? 'archive-product.php' : '';
} else {
$default_file = '';
}
@ -362,7 +370,7 @@ class WC_Template_Loader {
* @return string
*/
public static function unsupported_theme_title_filter( $title, $id ) {
if ( ! current_theme_supports( 'woocommerce' ) && is_page( self::$shop_page_id ) && $id === self::$shop_page_id ) {
if ( ! self::$theme_support && is_page( self::$shop_page_id ) && $id === self::$shop_page_id ) {
$args = self::get_current_shop_view_args();
$title_suffix = array();
@ -389,7 +397,7 @@ class WC_Template_Loader {
public static function unsupported_theme_shop_content_filter( $content ) {
global $wp_query;
if ( current_theme_supports( 'woocommerce' ) || ! is_main_query() ) {
if ( self::$theme_support || ! is_main_query() ) {
return $content;
}
@ -442,7 +450,7 @@ class WC_Template_Loader {
public static function unsupported_theme_product_content_filter( $content ) {
global $wp_query;
if ( current_theme_supports( 'woocommerce' ) || ! is_main_query() ) {
if ( self::$theme_support || ! is_main_query() ) {
return $content;
}

View File

@ -151,10 +151,6 @@ add_action( 'the_post', 'wc_setup_product_data' );
* @param array $args Args to pass into the global.
*/
function wc_setup_loop( $args = array() ) {
if ( isset( $GLOBALS['woocommerce_loop'] ) ) {
return; // If the loop has already been setup, bail.
}
$default_args = array(
'loop' => 0,
'columns' => wc_get_default_products_per_row(),
@ -169,6 +165,11 @@ function wc_setup_loop( $args = array() ) {
'current_page' => 1,
);
// Merge any existing values.
if ( isset( $GLOBALS['woocommerce_loop'] ) ) {
$default_args = array_merge( $default_args, $GLOBALS['woocommerce_loop'] );
}
// If this is a main WC query, use global args as defaults.
if ( $GLOBALS['wp_query']->get( 'wc_query' ) ) {
$default_args = array_merge( $default_args, array(
@ -1820,7 +1821,7 @@ if ( ! function_exists( 'woocommerce_maybe_show_product_subcategories' ) ) {
// If displaying categories, append to the loop.
if ( 'subcategories' === $display_type || 'both' === $display_type ) {
ob_start();
woocommerce_product_subcategories( array(
woocommerce_output_product_categories( array(
'parent_id' => is_product_category() ? get_queried_object_id() : 0,
) );
$loop_html .= ob_get_clean();
@ -1834,6 +1835,93 @@ if ( ! function_exists( 'woocommerce_maybe_show_product_subcategories' ) ) {
}
}
if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
/**
* This is a legacy function which used to check if we needed to display subcats and then output them. It was called by templates.
*
* From 3.3 onwards this is all handled via hooks and the woocommerce_maybe_show_product_subcategories function.
*
* Since some templates have not updated compatibility, to avoid showing incorrect categories this function has been deprecated and will
* return nothing. Replace usage with woocommerce_output_product_categories to render the category list manually.
*
* This is a legacy function which also checks if things should display.
* Themes no longer need to call these functions. It's all done via hooks.
*
* @deprecated 3.3.1 @todo Add a notice in a future version.
* @param array $args Arguments.
* @return null|boolean
*/
function woocommerce_product_subcategories( $args = array() ) {
$defaults = array(
'before' => '',
'after' => '',
'force_display' => false,
);
$args = wp_parse_args( $args, $defaults );
if ( $args['force_display'] ) {
// We can still render if display is forced.
woocommerce_output_product_categories( array(
'before' => $args['before'],
'after' => $args['after'],
'parent_id' => is_product_category() ? get_queried_object_id(): 0,
) );
return true;
} else {
// Output nothing. woocommerce_maybe_show_product_subcategories will handle the output of cats.
$display_type = woocommerce_get_loop_display_mode();
if ( 'subcategories' === $display_type ) {
// Legacy - if the template is using woocommerce_product_subcategories, this keeps the rest of the loop working.
global $wp_query;
$wp_query->post_count = 0;
$wp_query->max_num_pages = 0;
}
return 'subcategories' === $display_type || 'both' === $display_type;
}
}
}
if ( ! function_exists( 'woocommerce_output_product_categories' ) ) {
/**
* Display product sub categories as thumbnails.
*
* This is a replacement for woocommerce_product_subcategories which also does some logic
* based on the loop. This function however just outputs when called.
*
* @since 3.3.1
* @param array $args Arguments.
* @return boolean
*/
function woocommerce_output_product_categories( $args = array() ) {
$args = wp_parse_args( $args, array(
'before' => '',
'after' => '',
'parent_id' => 0,
) );
$product_categories = woocommerce_get_product_subcategories( $args['parent_id'] );
if ( ! $product_categories ) {
return false;
}
echo $args['before']; // WPCS: XSS ok.
foreach ( $product_categories as $category ) {
wc_get_template( 'content-product_cat.php', array(
'category' => $category,
) );
}
echo $args['after']; // WPCS: XSS ok.
return true;
}
}
if ( ! function_exists( 'woocommerce_get_product_subcategories' ) ) {
/**
* Get (and cache) product subcategories.
@ -1866,40 +1954,6 @@ if ( ! function_exists( 'woocommerce_get_product_subcategories' ) ) {
}
}
if ( ! function_exists( 'woocommerce_product_subcategories' ) ) {
/**
* Display product sub categories as thumbnails.
*
* @param array $args Arguments.
* @return boolean
*/
function woocommerce_product_subcategories( $args = array() ) {
$args = wp_parse_args( $args, array(
'before' => '',
'after' => '',
'parent_id' => 0,
) );
$product_categories = woocommerce_get_product_subcategories( $args['parent_id'] );
if ( ! $product_categories ) {
return false;
}
echo $args['before']; // WPCS: XSS ok.
foreach ( $product_categories as $category ) {
wc_get_template( 'content-product_cat.php', array(
'category' => $category,
) );
}
echo $args['after']; // WPCS: XSS ok.
return true;
}
}
if ( ! function_exists( 'woocommerce_subcategory_thumbnail' ) ) {
/**