-
Notifications
You must be signed in to change notification settings - Fork 21
/
Input.php
235 lines (198 loc) · 4.59 KB
/
Input.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input;
use Automattic\WooCommerce\GoogleListingsAndAds\PluginHelper;
defined( 'ABSPATH' ) || exit;
/**
* Class Input
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\Admin\Input
*/
class Input extends Form implements InputInterface {
use PluginHelper;
/**
* @var string
*/
protected $id;
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $block_name;
/**
* @var array
*/
protected $block_attributes = [];
/**
* @var string
*/
protected $label;
/**
* @var string
*/
protected $description;
/**
* @var mixed
*/
protected $value;
/**
* 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, 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();
}
/**
* @return string|null
*/
public function get_id(): ?string {
return $this->id;
}
/**
* @return string
*/
public function get_type(): string {
return $this->type;
}
/**
* @return string|null
*/
public function get_label(): ?string {
return $this->label;
}
/**
* @return string|null
*/
public function get_description(): ?string {
return $this->description;
}
/**
* @return mixed
*/
public function get_value() {
return $this->get_data();
}
/**
* @param string|null $id
*
* @return InputInterface
*/
public function set_id( ?string $id ): InputInterface {
$this->id = $id;
return $this;
}
/**
* @param string|null $label
*
* @return InputInterface
*/
public function set_label( ?string $label ): InputInterface {
$this->label = $label;
return $this;
}
/**
* @param string|null $description
*
* @return InputInterface
*/
public function set_description( ?string $description ): InputInterface {
$this->description = $description;
return $this;
}
/**
* @param mixed $value
*
* @return InputInterface
*/
public function set_value( $value ): InputInterface {
$this->set_data( $value );
return $this;
}
/**
* Return the data used for the input's view.
*
* @return array
*/
public function get_view_data(): array {
$view_data = [
'id' => $this->get_view_id(),
'type' => $this->get_type(),
'label' => $this->get_label(),
'value' => $this->get_value(),
'description' => $this->get_description(),
'desc_tip' => true,
];
return array_merge( parent::get_view_data(), $view_data );
}
/**
* Return the id used for the input's view.
*
* @return string
*/
public function get_view_id(): string {
$parent = $this->get_parent();
if ( $parent instanceof InputInterface ) {
return sprintf( '%s_%s', $parent->get_view_id(), $this->get_id() );
} elseif ( $parent instanceof FormInterface ) {
return sprintf( '%s_%s', $parent->get_view_name(), $this->get_id() );
}
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(),
];
}
}