Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Revert "Add product query support for Product Summary block" #7818

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export const blockAttributes: BlockAttributes = {
type: 'number',
default: 0,
},
isDescendentOfQueryLoop: {
type: 'boolean',
default: false,
},
};

export default blockAttributes;
27 changes: 6 additions & 21 deletions assets/js/atomic/blocks/product-elements/summary/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import type { BlockEditProps } from '@wordpress/blocks';
import { ProductQueryContext as Context } from '@woocommerce/blocks/product-query/types';
import { useEffect } from 'react';

/**
* Internal dependencies
Expand All @@ -19,27 +16,15 @@ import {
import './editor.scss';
import type { BlockAttributes } from './types';

const Edit = ( {
attributes,
setAttributes,
context,
}: BlockEditProps< BlockAttributes > & { context: Context } ): JSX.Element => {
const blockProps = useBlockProps();

const blockAttrs = {
...attributes,
...context,
};
const isDescendentOfQueryLoop = Number.isFinite( context.queryId );

useEffect(
() => setAttributes( { isDescendentOfQueryLoop } ),
[ setAttributes, isDescendentOfQueryLoop ]
);
interface Props {
attributes: BlockAttributes;
}

const Edit = ( { attributes }: Props ): JSX.Element => {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Block { ...blockAttrs } />
<Block { ...attributes } />
</div>
);
};
Expand Down
8 changes: 2 additions & 6 deletions assets/js/atomic/blocks/product-elements/summary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
BLOCK_ICON as icon,
BLOCK_DESCRIPTION as description,
} from './constants';
import { Save } from './save';

const blockConfig: BlockConfiguration = {
...sharedConfig,
Expand All @@ -26,12 +27,7 @@ const blockConfig: BlockConfiguration = {
attributes,
supports,
edit,
usesContext: [ 'query', 'queryId', 'postId' ],
ancestor: [
'@woocommerce/all-products',
'@woocommerce/single-product',
'core/post-template',
],
save: Save,
};

registerBlockType( 'woocommerce/product-summary', blockConfig );
21 changes: 21 additions & 0 deletions assets/js/atomic/blocks/product-elements/summary/save.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';
import classnames from 'classnames';

type Props = {
attributes: Record< string, unknown > & {
className?: string;
};
};

export const Save = ( { attributes }: Props ): JSX.Element => {
return (
<div
{ ...useBlockProps.save( {
className: classnames( 'is-loading', attributes.className ),
} ) }
/>
);
};
1 change: 0 additions & 1 deletion assets/js/atomic/blocks/product-elements/summary/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export interface BlockAttributes {
productId: number;
isDescendentOfQueryLoop: boolean;
}
68 changes: 0 additions & 68 deletions src/BlockTypes/ProductSummary.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Automattic\WooCommerce\Blocks\BlockTypes;

use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils;

/**
* ProductSummary class.
*/
Expand Down Expand Up @@ -53,71 +51,5 @@ protected function get_block_type_supports() {
protected function register_block_type_assets() {
parent::register_block_type_assets();
$this->register_chunk_translations( [ $this->block_name ] );
return null;
}

/**
* Register the context.
*/
protected function get_block_type_uses_context() {
return [ 'query', 'queryId', 'postId' ];
}

/**
* Include and render the block.
*
* @param array $attributes Block attributes. Default empty array.
* @param string $content Block content. Default empty string.
* @param WP_Block $block Block instance.
* @return string Rendered block type output.
*/
protected function render( $attributes, $content, $block ) {
if ( ! empty( $content ) ) {
parent::register_block_type_assets();
$this->register_chunk_translations( [ $this->block_name ] );
return $content;
}

$post_id = $block->context['postId'];
$product = wc_get_product( $post_id );
$summary_content = self::get_summary_content( $product );

if ( ! $summary_content ) {
return '';
}

$classes_and_styles = StyleAttributesUtils::get_classes_and_styles_by_attributes( $attributes );
$classname = isset( $attributes['className'] ) ? $attributes['className'] : '';

$output = '<div class="wc-block-components-product-summary '
. esc_attr( $classes_and_styles['classes'] ) . ' ' . esc_attr( $classname ) . '"';

if ( isset( $classes_and_styles['styles'] ) ) {
$output .= ' style="' . esc_attr( $classes_and_styles['styles'] ) . '"';
}

$output .= '>';
$output .= '<p>' . wp_kses_post( $summary_content ) . '</p>';
$output .= '</div>';

return $output;
}

/**
* Returns summary text for a product.
*
* @param [WC_Product] $product Product to get summary text for.
* @return (string|null) Summary text for the product.
*/
protected function get_summary_content( $product ) {
$short_description = $product->get_short_description();
$summary_content = $short_description ? $short_description : $product->get_description();

if ( ! $summary_content ) {
return null;
}

$summary = wc_trim_string( $summary_content, 150 );
return $summary;
}
}