forked from woocommerce/woocommerce-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract LegacyTemplate block (woocommerce#4991)
* Legacy Template block * Return render_single_product value * Make placeholder text translatable * Don't allow removing the block * Update block title Co-authored-by: Albert Juhé Lluveras <[email protected]>
- Loading branch information
1 parent
bb654f0
commit a4871f6
Showing
5 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { registerExperimentalBlockType } from '@woocommerce/block-settings'; | ||
import { useBlockProps } from '@wordpress/block-editor'; | ||
import { Placeholder } from '@wordpress/components'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
interface Props { | ||
attributes: { | ||
template: string; | ||
}; | ||
} | ||
|
||
const Edit = ( { attributes }: Props ) => { | ||
const blockProps = useBlockProps(); | ||
return ( | ||
<div { ...blockProps }> | ||
<Placeholder | ||
label={ sprintf( | ||
/* translators: %s is the template name */ | ||
__( | ||
'Wireframe template for %s will be rendered here.', | ||
'woo-gutenberg-products-block' | ||
), | ||
attributes.template | ||
) } | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
registerExperimentalBlockType( 'woocommerce/legacy-template', { | ||
title: __( 'WooCommerce Legacy Template', 'woo-gutenberg-products-block' ), | ||
category: 'woocommerce', | ||
apiVersion: 2, | ||
keywords: [ __( 'WooCommerce', 'woo-gutenberg-products-block' ) ], | ||
description: __( | ||
'Renders legacy WooCommerce PHP templates.', | ||
'woo-gutenberg-products-block' | ||
), | ||
supports: { | ||
align: false, | ||
html: false, | ||
multiple: false, | ||
reusable: false, | ||
inserter: false, | ||
}, | ||
example: { | ||
attributes: { | ||
isPreview: true, | ||
}, | ||
}, | ||
attributes: { | ||
/** | ||
* Template attribute is used to determine which core PHP template gets rendered. | ||
*/ | ||
template: { | ||
type: 'string', | ||
default: 'any', | ||
}, | ||
lock: { | ||
type: 'object', | ||
default: { | ||
remove: true, | ||
}, | ||
}, | ||
}, | ||
edit: Edit, | ||
save: () => null, | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
namespace Automattic\WooCommerce\Blocks\BlockTypes; | ||
|
||
/** | ||
* Legacy Single Product class | ||
* | ||
* @internal | ||
*/ | ||
class LegacyTemplate extends AbstractDynamicBlock { | ||
/** | ||
* Block name. | ||
* | ||
* @var string | ||
*/ | ||
protected $block_name = 'legacy-template'; | ||
|
||
/** | ||
* API version. | ||
* | ||
* @var string | ||
*/ | ||
protected $api_version = '2'; | ||
|
||
/** | ||
* Render method for the Legacy Template block. This method will determine which template to render. | ||
* | ||
* @param array $attributes Block attributes. | ||
* @param string $content Block content. | ||
* | ||
* @return string | void Rendered block type output. | ||
*/ | ||
protected function render( $attributes, $content ) { | ||
if ( null === $attributes['template'] ) { | ||
return; | ||
} | ||
|
||
if ( 'single-product' === $attributes['template'] ) { | ||
return $this->render_single_product(); | ||
} else { | ||
ob_start(); | ||
|
||
echo "You're using the LegacyTemplate block"; | ||
|
||
wp_reset_postdata(); | ||
return ob_get_clean(); | ||
} | ||
} | ||
|
||
/** | ||
* Render method for the single product template and parts. | ||
* | ||
* @return string Rendered block type output. | ||
*/ | ||
protected function render_single_product() { | ||
ob_start(); | ||
|
||
echo 'This method will render the single product template'; | ||
|
||
wp_reset_postdata(); | ||
return ob_get_clean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters