Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product Block Editor: Add the fundamental support and map Text and Integer attribute inputs to currently available generic blocks #2151

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
63df208
Extract the `get_applicable_product_types` method from `AttributesTab…
eason9487 Nov 10, 2023
e74060a
Extract the processing of getting the visible and hidden product type…
eason9487 Nov 10, 2023
518c145
Convert the `init_input` method in `AttributesForm` to a static method.
eason9487 Nov 10, 2023
f3dc932
Add methods to `Admin\Input\Input` for getting block config.
eason9487 Nov 10, 2023
6f12692
Add a new class `AttributesBlock` to register blocks to Product Block…
eason9487 Nov 10, 2023
a89b40d
Map `Text` and `Integer` inputs to currently available generic blocks.
eason9487 Nov 10, 2023
0b71d99
Add the block attribute 'min' to the multipack input.
eason9487 Nov 10, 2023
2b33f82
Use another way to add the visible attributes for the variation produ…
eason9487 Nov 13, 2023
02b978d
Remove an unused method from `SelectWithTextInput`.
eason9487 Nov 22, 2023
97d2af3
Remove unused PHP classes `Admin\Input\Checkbox` and `Admin\Input\Dec…
eason9487 Nov 22, 2023
2f96cfd
Add basic PHP unit tests for the `Admin\Input\Input` class and its in…
eason9487 Nov 17, 2023
1b959cb
Add PHP unit tests for the attribute input classes under `Admin\Produ…
eason9487 Nov 17, 2023
838f469
Add PHP unit tests for the `AttributesForm` class.
eason9487 Nov 20, 2023
0408038
Add PHP unit tests for the `AttributesBlock` class.
eason9487 Nov 21, 2023
46830dc
Avoid calling the ProductTemplates API `add_hide_condition` with an e…
eason9487 Dec 15, 2023
466d83e
Should hide a block field when its all applicable product types are n…
eason9487 Dec 18, 2023
9ff6a97
Update the PHP docs for the `$attribute_type` and `$input_type` param…
eason9487 Dec 25, 2023
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
20 changes: 0 additions & 20 deletions src/Admin/Input/Checkbox.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Admin/Input/Decimal.php

This file was deleted.

77 changes: 75 additions & 2 deletions src/Admin/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;

use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;

defined( 'ABSPATH' ) || exit;

/**
Expand All @@ -12,6 +14,8 @@
*/
class Input extends Form implements InputInterface {

use PluginHelper;

/**
* @var string
*/
Expand All @@ -22,6 +26,16 @@ class Input extends Form implements InputInterface {
*/
protected $type;

/**
* @var string
*/
protected $block_name;

/**
* @var array
*/
protected $block_attributes = [];

/**
* @var string
*/
Expand All @@ -41,9 +55,12 @@ class Input extends Form implements InputInterface {
* Input constructor.
*
* @param string $type
* @param string $block_name The name of a generic product block in WooCommerce core or a custom block in this extension.
*/
public function __construct( string $type ) {
$this->type = $type;
public function __construct( string $type, string $block_name = '' ) {
// TODO: Remove the default value of $block_name after all attribute inputs have specified a block name
$this->type = $type;
$this->block_name = $block_name;
parent::__construct();
}

Expand Down Expand Up @@ -159,4 +176,60 @@ public function get_view_id(): string {

return sprintf( 'gla_%s', $this->get_name() );
}

/**
* Return the name of a generic product block in WooCommerce core or a custom block in this extension.
*
* @return string
*/
public function get_block_name(): string {
return $this->block_name;
}

/**
* Add or update a block attribute used for block config.
*
* @param string $key The attribute key defined in the corresponding block.json
* @param mixed $value The attribute value defined in the corresponding block.json
*
* @return InputInterface
*/
public function set_block_attribute( string $key, $value ): InputInterface {
$this->block_attributes[ $key ] = $value;

return $this;
}

/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array {
$meta_key = $this->prefix_meta_key( $this->get_id() );

return array_merge(
[
'property' => "meta_data.{$meta_key}",
'label' => $this->get_label(),
'tooltip' => $this->get_description(),
],
$this->block_attributes
);
}

/**
* Return the config used for the input's block within the Product Block Editor.
*
* @return array
*/
public function get_block_config(): array {
$id = $this->get_id();

return [
'id' => "google-listings-and-ads-product-attributes-{$id}",
'blockName' => $this->get_block_name(),
'attributes' => $this->get_block_attributes(),
];
}
}
31 changes: 31 additions & 0 deletions src/Admin/Input/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,35 @@ public function set_value( $value ): InputInterface;
* @return string
*/
public function get_view_id(): string;

/**
* Return the name of a generic product block in WooCommerce core or a custom block in this extension.
*
* @return string
*/
public function get_block_name(): string;

/**
* Add or update a block attribute used for block config.
*
* @param string $key The attribute key defined in the corresponding block.json
* @param mixed $value The attribute value defined in the corresponding block.json
*
* @return InputInterface
*/
public function set_block_attribute( string $key, $value ): InputInterface;

/**
* Return the attributes of block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_attributes(): array;

/**
* Return the block config used for the input's view within the Product Block Editor.
*
* @return array
*/
public function get_block_config(): array;
}
2 changes: 1 addition & 1 deletion src/Admin/Input/Integer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class Integer extends Input {
* Integer constructor.
*/
public function __construct() {
parent::__construct( 'integer' );
parent::__construct( 'integer', 'woocommerce/product-number-field' );
}
}
9 changes: 0 additions & 9 deletions src/Admin/Input/SelectWithTextInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,6 @@ protected function get_custom_input(): Text {
return $this->children[ self::CUSTOM_INPUT_KEY ];
}

/**
* Whether the set value is a custom value (i.e. not from the list of specified options).
*
* @return bool
*/
protected function is_custom_value(): bool {
return ! empty( $this->get_value() ) && ! is_array( $this->get_value() ) && ! isset( $this->get_options()[ $this->get_value() ] );
}

/**
* Return the data used for the input's view.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/Input/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class Text extends Input {
* Text constructor.
*/
public function __construct() {
parent::__construct( 'text' );
parent::__construct( 'text', 'woocommerce/product-text-field' );
}
}
Loading