Merge pull request #17635 from woocommerce/feature/17551

Unsupported theme single product enhancements
This commit is contained in:
Claudiu Lodromanean 2017-11-13 12:24:46 -08:00 committed by GitHub
commit c1d0e9d79c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 161 additions and 82 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -682,7 +682,6 @@ p.demo_store,
color: $secondarytext;
background-color: $secondary;
border: 0;
white-space: nowrap;
display: inline-block;
background-image: none;
box-shadow: none;
@ -762,7 +761,6 @@ p.demo_store,
a.added_to_cart {
padding-top: 0.5em;
white-space: nowrap;
display: inline-block;
}

View File

@ -498,6 +498,11 @@ class WC_Shortcodes {
$args['p'] = absint( $atts['id'] );
}
// Don't render titles if desired.
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
$single_product = new WP_Query( $args );
$preselected_id = '0';
@ -565,6 +570,11 @@ class WC_Shortcodes {
// @codingStandardsIgnoreEnd
wp_reset_postdata();
// Re-enable titles if they were removed.
if ( isset( $atts['show_title'] ) && ! $atts['show_title'] ) {
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
}
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}

View File

@ -24,19 +24,49 @@ class WC_Template_Loader {
*/
private static $shop_page_id = 0;
/**
* Store whether we're processing a product inside the_content filter.
*
* @var boolean
*/
private static $in_content_filter = false;
/**
* Hook in methods.
*/
public static function init() {
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
self::$shop_page_id = wc_get_page_id( 'shop' );
if ( ! current_theme_supports( 'woocommerce' ) && self::$shop_page_id ) {
add_filter( 'the_content', array( __CLASS__, 'the_content_filter' ), 10 );
add_filter( 'the_title', array( __CLASS__, 'the_title_filter' ), 10, 2 );
// Supported themes.
if ( current_theme_supports( 'woocommerce' ) ) {
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
} else {
// Unsupported themes.
add_action( 'template_redirect', array( __CLASS__, 'unsupported_theme_init' ) );
}
}
/**
* Hook in methods to enhance the unsupported theme experience on Shop and Product pages.
*
* @since 3.3.0
*/
public static function unsupported_theme_init() {
if ( self::$shop_page_id || is_product() ) {
add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_content_filter' ), 10 );
add_filter( 'the_title', array( __CLASS__, 'unsupported_theme_title_filter' ), 10, 2 );
add_filter( 'post_thumbnail_html', array( __CLASS__, 'unsupported_theme_single_featured_image_filter' ) );
add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'unsupported_theme_remove_review_tab' ) );
add_filter( 'comments_number', '__return_empty_string' );
if ( is_product() ) {
remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
}
}
@ -54,73 +84,6 @@ class WC_Template_Loader {
);
}
/**
* Filter the title and insert WooCommerce content on the shop page.
*
* For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
*
* @since 3.3.0
* @param string $title Existing title.
* @param int $id ID of the post being filtered.
* @return string
*/
public static function the_title_filter( $title, $id ) {
if ( ! current_theme_supports( 'woocommerce' ) && is_page( self::$shop_page_id ) && $id === self::$shop_page_id ) {
$args = self::get_current_shop_view_args();
$title_suffix = array();
if ( $args->page > 1 ) {
$title_suffix[] = sprintf( esc_html__( 'Page %d', 'woocommerce' ), $args->page );
}
if ( $title_suffix ) {
$title = $title . ' &ndash; ' . implode( ', ', $title_suffix );
}
}
return $title;
}
/**
* Filter the content and insert WooCommerce content on the shop page.
*
* For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
*
* @since 3.3.0
* @param string $content Existing post content.
* @return string
*/
public static function the_content_filter( $content ) {
global $wp_query;
if ( ! current_theme_supports( 'woocommerce' ) && is_page( self::$shop_page_id ) && is_main_query() ) {
$args = self::get_current_shop_view_args();
$shortcode = new WC_Shortcode_Products(
array_merge(
wc()->query->get_catalog_ordering_args(),
array(
'page' => $args->page,
'columns' => $args->columns,
'rows' => $args->rows,
'orderby' => '',
'order' => '',
'paginate' => true,
'cache' => false,
)
),
'products' );
// Allow queries to run e.g. layered nav.
add_action( 'pre_get_posts', array( wc()->query, 'product_query' ) );
$content = $content . $shortcode->get_content();
// Remove actions and self to avoid nested calls.
remove_action( 'pre_get_posts', array( wc()->query, 'product_query' ) );
remove_filter( 'the_content', array( __CLASS__, 'the_content_filter' ) );
}
return $content;
}
/**
* Load a template.
*
@ -242,6 +205,116 @@ class WC_Template_Loader {
}
}
}
/**
* Filter the title and insert WooCommerce content on the shop page.
*
* For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
*
* @since 3.3.0
* @param string $title Existing title.
* @param int $id ID of the post being filtered.
* @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 ) {
$args = self::get_current_shop_view_args();
$title_suffix = array();
if ( $args->page > 1 ) {
$title_suffix[] = sprintf( esc_html__( 'Page %d', 'woocommerce' ), $args->page );
}
if ( $title_suffix ) {
$title = $title . ' &ndash; ' . implode( ', ', $title_suffix );
}
}
return $title;
}
/**
* Filter the content and insert WooCommerce content on the shop page.
*
* For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
*
* @since 3.3.0
* @param string $content Existing post content.
* @return string
*/
public static function unsupported_theme_content_filter( $content ) {
global $wp_query;
if ( current_theme_supports( 'woocommerce' ) || ! is_main_query() ) {
return $content;
}
self::$in_content_filter = true;
// Remove the filter we're in to avoid nested calls.
remove_filter( 'the_content', array( __CLASS__, 'the_content_filter' ) );
// Unsupported theme shop page.
if ( is_page( self::$shop_page_id ) ) {
$args = self::get_current_shop_view_args();
$shortcode = new WC_Shortcode_Products(
array_merge(
wc()->query->get_catalog_ordering_args(),
array(
'page' => $args->page,
'columns' => $args->columns,
'rows' => $args->rows,
'orderby' => '',
'order' => '',
'paginate' => true,
'cache' => false,
)
),
'products' );
// Allow queries to run e.g. layered nav.
add_action( 'pre_get_posts', array( wc()->query, 'product_query' ) );
$content = $content . $shortcode->get_content();
// Remove actions and self to avoid nested calls.
remove_action( 'pre_get_posts', array( wc()->query, 'product_query' ) );
// Unsupported theme product page.
} elseif ( is_product() ) {
$content = do_shortcode( '[product_page id="' . get_the_ID() . '" show_title=0]' );
}
self::$in_content_filter = false;
return $content;
}
/**
* Prevent the main featured image on product pages because there will be another featured image
* in the gallery.
*
* @since 3.3.0
* @param string $html Img element HTML.
* @return string
*/
public static function unsupported_theme_single_featured_image_filter( $html ) {
if ( self::$in_content_filter || ! is_product() || ! is_main_query() ) {
return $html;
}
return '';
}
/**
* Remove the Review tab and just use the regular comment form.
*
* @param array $tabs Tab info.
* @return array
*/
public static function unsupported_theme_remove_review_tab( $tabs ) {
unset( $tabs['reviews'] );
return $tabs;
}
}
add_action( 'after_setup_theme', array( 'WC_Template_Loader', 'init' ) );
add_action( 'init', array( 'WC_Template_Loader', 'init' ) );

View File

@ -403,8 +403,6 @@ final class WooCommerce {
* @since 3.3.0
*/
private function theme_support_includes() {
$theme_support = array( 'twentyseventeen', 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' );
if ( $this->is_active_theme( array( 'twentyseventeen', 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' ) ) ) {
switch ( get_template() ) {
case 'twentyten' :