woocommerce/plugins/woocommerce-blocks/assets/php/class-wgpb-block-grid-base.php

391 lines
10 KiB
PHP

<?php
/**
* Set up some shared functionality for grid blocks.
* NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-gutenberg-products-block.
*
* @package WooCommerce\Blocks
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Abstract class for product grid functionality
*/
abstract class WGPB_Block_Grid_Base {
/**
* Block name.
*
* @var string
*/
protected $block_name = '';
/**
* Attributes.
*
* @var array
*/
protected $attributes = array();
/**
* InnerBlocks content.
*
* @var string
*/
protected $content = '';
/**
* Initialize block.
*
* @param array $attributes Block attributes. Default empty array.
* @param string $content Block content. Default empty string.
*/
public function __construct( $attributes = array(), $content = '' ) {
$this->attributes = $this->parse_attributes( $attributes );
$this->content = $content;
}
/**
* Get the block's attributes.
*
* @param array $attributes Block attributes. Default empty array.
* @return array Block attributes merged with defaults.
*/
protected function parse_attributes( $attributes ) {
// These should match what's set in JS `registerBlockType`.
$defaults = array(
'columns' => wc_get_theme_support( 'product_blocks::default_columns', 3 ),
'rows' => wc_get_theme_support( 'product_blocks::default_rows', 1 ),
'categories' => array(),
'catOperator' => 'any',
'contentVisibility' => array(
'title' => true,
'price' => true,
'rating' => true,
'button' => true,
),
);
$attributes = wp_parse_args( $attributes, $defaults );
if ( ! empty( $attributes['rows'] ) && ! empty( $attributes['columns'] ) ) {
$attributes['limit'] = intval( $attributes['columns'] ) * intval( $attributes['rows'] );
} else {
$attributes['limit'] = -1;
}
return $attributes;
}
/**
* Set args specific to this block
*
* @param array $query_args Query args.
*/
abstract protected function set_block_query_args( &$query_args );
/**
* Set orderby/order query args.
*
* @param array $query_args Query args.
*/
protected function set_ordering_query_args( &$query_args ) {
$orderby = '';
$order = '';
if ( isset( $this->attributes['orderby'] ) ) {
if ( 'price_desc' === $this->attributes['orderby'] ) {
$orderby = 'price';
$order = 'DESC';
} elseif ( 'price_asc' === $this->attributes['orderby'] ) {
$orderby = 'price';
$order = 'ASC';
} else {
$orderby = $this->attributes['orderby'];
}
}
// This handles orderby queries and hooks in custom orderby functions.
$query_args = array_merge( $query_args, WC()->query->get_catalog_ordering_args( $orderby, $order ) );
}
/**
* Set categories query args.
*
* @param array $query_args Query args.
*/
protected function set_categories_query_args( &$query_args ) {
if ( ! empty( $this->attributes['categories'] ) ) {
$categories = array_map( 'absint', $this->attributes['categories'] );
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'terms' => $categories,
'field' => 'term_id',
'operator' => 'all' === $this->attributes['catOperator'] ? 'AND' : 'IN',
/*
* When cat_operator is AND, the children categories should be excluded,
* as only products belonging to all the children categories would be selected.
*/
'include_children' => 'all' === $this->attributes['catOperator'] ? false : true,
);
}
}
/**
* Get all product query args for WP_Query.
*
* @return array
*/
protected function get_products_query_args() {
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'ignore_sticky_posts' => true,
'no_found_rows' => false,
'posts_per_page' => intval( $this->attributes['limit'] ),
'tax_query' => array(), // phpcs:ignore WordPress.DB.SlowDBQuery
'meta_query' => array(), // phpcs:ignore WordPress.DB.SlowDBQuery
);
$this->set_ordering_query_args( $query_args );
$this->set_categories_query_args( $query_args );
$this->set_block_query_args( $query_args );
return $query_args;
}
/**
* Run the query and return an array of product IDs
*
* @return array List of product IDs
*/
protected function get_products() {
$query_hash = md5( wp_json_encode( $this->attributes ) . __CLASS__ );
$transient_name = 'wc_block_' . $query_hash;
$transient_value = get_transient( $transient_name );
$transient_version = WC_Cache_Helper::get_transient_version( 'product_query' );
if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) {
$results = $transient_value['value'];
} else {
$query = new WP_Query( $this->get_products_query_args() );
$results = wp_parse_id_list( $query->posts );
$transient_value = array(
'version' => $transient_version,
'value' => $results,
);
set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
// Remove ordering query arguments which may have been added by get_catalog_ordering_args.
WC()->query->remove_ordering_args();
}
// Prime caches to reduce future queries.
if ( is_callable( '_prime_post_caches' ) ) {
_prime_post_caches( $results );
}
return $results;
}
/**
* Render the Products block.
*
* @return string Rendered block type output.
*/
public function render() {
$products = $this->get_products();
$classes = $this->get_container_classes();
$output = implode( '', array_map( array( $this, 'render_product' ), $products ) );
return sprintf( '<div class="%s"><ul class="wc-block-grid__products">%s</ul></div>', esc_attr( $classes ), $output );
}
/**
* Get the list of classes to apply to this block.
*
* @return string space-separated list of classes.
*/
protected function get_container_classes() {
$classes = array(
'wc-block-grid',
"wp-block-{$this->block_name}",
"wc-block-{$this->block_name}",
"has-{$this->attributes['columns']}-columns",
);
if ( $this->attributes['rows'] > 1 ) {
$classes[] = 'has-multiple-rows';
}
if ( isset( $this->attributes['align'] ) ) {
$classes[] = "align{$this->attributes['align']}";
}
return implode( ' ', $classes );
}
/**
* Render a single products.
*
* @param int $id Product ID.
* @return string Rendered product output.
*/
public function render_product( $id ) {
$product = wc_get_product( $id );
if ( ! $product ) {
return '';
}
$data = (object) array(
'permalink' => esc_url( $product->get_permalink() ),
'image' => $this->get_image_html( $product ),
'title' => $this->get_title_html( $product ),
'rating' => $this->get_rating_html( $product ),
'price' => $this->get_price_html( $product ),
'badge' => $this->get_sale_badge_html( $product ),
'button' => $this->get_button_html( $product ),
);
return apply_filters(
'woocommerce_blocks_product_grid_item_html',
"<li class=\"wc-block-grid__product\">
<a href=\"{$data->permalink}\" class=\"wc-block-grid__product-link\">
{$data->image}
{$data->title}
</a>
{$data->price}
{$data->badge}
{$data->rating}
{$data->button}
</li>",
$data,
$product
);
}
/**
* Get the product image.
*
* @param WC_Product $product Product.
* @return string
*/
protected function get_image_html( $product ) {
return '<div class="wc-block-grid__product-image">' . $product->get_image( 'woocommerce_thumbnail' ) . '</div>';
}
/**
* Get the product title.
*
* @param WC_Product $product Product.
* @return string
*/
protected function get_title_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['title'] ) ) {
return '';
}
return '<div class="wc-block-grid__product-title">' . $product->get_title() . '</div>';
}
/**
* Render the rating icons.
*
* @param WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_rating_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['rating'] ) ) {
return '';
}
$rating_count = $product->get_rating_count();
$review_count = $product->get_review_count();
$average = $product->get_average_rating();
if ( $rating_count > 0 ) {
return sprintf(
'<div class="wc-block-grid__product-rating">%s</div>',
wc_get_rating_html( $average, $rating_count )
);
}
return '';
}
/**
* Get the price.
*
* @param WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_price_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['price'] ) ) {
return '';
}
return sprintf(
'<div class="wc-block-grid__product-price price">%s</div>',
$product->get_price_html()
);
}
/**
* Get the sale badge.
*
* @param WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_sale_badge_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['price'] ) ) {
return '';
}
if ( ! $product->is_on_sale() ) {
return;
}
return '<span class="wc-block-grid__product-onsale">' . esc_html__( 'Sale!', 'woo-gutenberg-products-block' ) . '</span>';
}
/**
* Get the button.
*
* @param WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_button_html( $product ) {
if ( empty( $this->attributes['contentVisibility']['button'] ) ) {
return '';
}
return '<div class="wp-block-button wc-block-grid__product-add-to-cart">' . $this->get_add_to_cart( $product ) . '</div>';
}
/**
* Get the "add to cart" button.
*
* @param WC_Product $product Product.
* @return string Rendered product output.
*/
protected function get_add_to_cart( $product ) {
$attributes = array(
'aria-label' => $product->add_to_cart_description(),
'data-quantity' => '1',
'data-product_id' => $product->get_id(),
'data-product_sku' => $product->get_sku(),
'rel' => 'nofollow',
'class' => 'wp-block-button__link add_to_cart_button',
);
if ( $product->supports( 'ajax_add_to_cart' ) ) {
$attributes['class'] .= ' ajax_add_to_cart';
}
return sprintf(
'<a href="%s" %s>%s</a>',
esc_url( $product->add_to_cart_url() ),
wc_implode_html_attributes( $attributes ),
esc_html( $product->add_to_cart_text() )
);
}
}