Handle shop content only

This commit is contained in:
Mike Jolley 2017-11-09 10:44:29 +00:00
parent ddb357cc48
commit 95a7857f41
1 changed files with 70 additions and 15 deletions

View File

@ -17,41 +17,96 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class WC_Template_Loader {
/**
* Store the shop page ID.
*
* @var integer
*/
private static $shop_page_id = 0;
/**
* Hook in methods.
*/
public static function init() {
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
add_filter( 'the_content', array( __CLASS__, 'the_content_filter' ) );
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 );
add_filter( 'comments_number', '__return_empty_string' );
}
}
/**
* Filter the content and insert WooCommerce content. @todo
* Get information about the current shop page view.
*
* @since 3.3.0
* @return array
*/
private static function get_current_shop_view_args() {
return (object) array(
'page' => absint( max( 1, absint( get_query_var( 'paged' ) ) ) ),
'columns' => 3,
'rows' => 3,
);
}
/**
* 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 . ' – ' . 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 ) {
if ( ! current_theme_supports( 'woocommerce' ) && is_page( wc_get_page_id( 'shop' ) ) && is_main_query() ) {
$page = max( 1, absint( get_query_var( 'paged' ) ) );
$columns = 3;
$rows = 3;
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' => $page,
'columns' => $columns,
'rows' => $rows,
'page' => $args->page,
'columns' => $args->columns,
'rows' => $args->rows,
'paginate' => true,
)
),
'products' );
$content .= $shortcode->get_content();
$content = $content . $shortcode->get_content();
// Remove self to avoid nested calls.
remove_filter( 'the_content', array( __CLASS__, 'the_content_filter' ) );
@ -71,11 +126,11 @@ class WC_Template_Loader {
* this to the theme (containing a woocommerce() inside) this will be used for all.
* woocommerce templates.
*
* @param mixed $template
* @param string $template Template to load.
* @return string
*/
public static function template_loader( $template ) {
if ( is_embed() || ! current_theme_supports( 'woocommerce' ) ) {
if ( is_embed() ) {
return $template;
}
@ -115,7 +170,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 = 'archive-product.php';
$default_file = current_theme_supports( 'woocommerce' ) ? 'archive-product.php' : '';
} else {
$default_file = '';
}
@ -158,7 +213,7 @@ class WC_Template_Loader {
* @return string
*/
public static function comments_template_loader( $template ) {
if ( get_post_type() !== 'product' || ! current_theme_supports( 'woocommerce' ) ) {
if ( get_post_type() !== 'product' ) {
return $template;
}
@ -182,4 +237,4 @@ class WC_Template_Loader {
}
}
WC_Template_Loader::init();
add_action( 'after_setup_theme', array( 'WC_Template_Loader', 'init' ) );