Skip to content

Commit e578f17

Browse files
committed
Begin adding subcomponent structure
1 parent 20e0d23 commit e578f17

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

includes/apple-exporter/class-component-factory.php

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Apple_Exporter;
1010

11+
use Apple_Exporter\Components\Component;
12+
use DOMElement;
13+
1114
/**
1215
* This class in in charge of creating components. Manual component
1316
* instantiation should be avoided, use this instead.
@@ -163,39 +166,44 @@ private static function register_component( $shortname, $classname ) {
163166
/**
164167
* Get a component.
165168
*
166-
* @param string $shortname The short name for the component type to use.
167-
* @param string $html The HTML to be parsed by the component.
168-
* @access public
169-
* @return \Apple_Exporter\Components\Component A component class matching the shortname.
169+
* @param string $shortname The short name for the component type to use.
170+
* @param string $html The HTML to be parsed by the component.
171+
* @param ?Component $parent If provided, treats this component as a subcomponent of this parent.
172+
*
173+
* @return Component A component class matching the shortname.
170174
*/
171-
public static function get_component( $shortname, $html ) {
175+
public static function get_component( $shortname, $html, $parent = null ) {
172176

173177
$class = self::$components[ $shortname ];
174178

175179
if ( is_null( $class ) || ! class_exists( $class ) ) {
176180
return null;
177181
}
178182

179-
return new $class(
183+
$component = new $class(
180184
$html,
181185
self::$workspace,
182186
self::$settings,
183187
self::$styles,
184188
self::$layouts,
185189
null,
186-
self::$component_styles
190+
self::$component_styles,
191+
$parent
187192
);
193+
194+
return $component;
188195
}
189196

190197
/**
191198
* Given a node, returns an array of all the components inside that node. If
192199
* the node is a component itself, returns an array of only one element.
193200
*
194-
* @param \DOMElement $node The node to be examined.
195-
* @access public
201+
* @param DOMElement $node The node to be examined.
202+
* @param ?Component $parent If provided, treats all components as subcomponents of this parent.
203+
*
196204
* @return array An array of components contained in the node.
197205
*/
198-
public static function get_components_from_node( $node ) {
206+
public static function get_components_from_node( $node, $parent = null ) {
199207
/* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
200208
$result = [];
201209

@@ -210,25 +218,25 @@ public static function get_components_from_node( $node ) {
210218

211219
/**
212220
* Did we match several components? If so, a hash is returned. Both the
213-
* body and heading components can returns this, in the case they find
221+
* body and heading components can return this, in the case they find
214222
* non-markdown-able elements inside.
215223
*/
216224
if ( is_array( $matched_node ) ) {
217225
foreach ( $matched_node as $base_component ) {
218-
$result[] = self::get_component( $base_component['name'], $base_component['value'] );
226+
$result[] = self::get_component( $base_component['name'], $base_component['value'], $parent );
219227
}
220228
return $result;
221229
}
222230

223231
// We matched a single node.
224232
$html = $node->ownerDocument->saveXML( $matched_node );
225-
$result[] = self::get_component( $shortname, $html );
233+
$result[] = self::get_component( $shortname, $html, $parent );
226234
return $result;
227235
}
228236
// Nothing found. Maybe it's a container element?
229237
if ( $node->hasChildNodes() ) {
230238
foreach ( $node->childNodes as $child ) {
231-
$result = array_merge( $result, self::get_components_from_node( $child, $node ) );
239+
$result = array_merge( $result, self::get_components_from_node( $child, $parent ) );
232240
}
233241
// Remove all nulls from the array.
234242
$result = array_filter( $result );

includes/apple-exporter/components/class-aside.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ protected function build( $html ) {
166166
[
167167
'#components#' => array_map(
168168
fn ( Component $component ) => $component->to_array(),
169-
Component_Factory::get_components_from_node( $element ),
169+
Component_Factory::get_components_from_node( $element, $this ),
170170
),
171171
],
172172
);

includes/apple-exporter/components/class-component.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ abstract class Component {
144144
*/
145145
protected $layouts;
146146

147+
/**
148+
* The parent component.
149+
*
150+
* @since 2.5.0
151+
*
152+
* @var ?Component
153+
*/
154+
protected $parent;
155+
147156
/**
148157
* The parser to use for this component.
149158
*
@@ -224,6 +233,7 @@ abstract class Component {
224233
* @param \Apple_Exporter\Builders\Component_Layouts $layouts Registered layouts.
225234
* @param \Apple_Exporter\Parser $parser The parser in use during this run.
226235
* @param \Apple_Exporter\Builders\Component_Styles $component_styles Registered text styles.
236+
* @param ?Component $parent The parent component, if this is a subcomponent.
227237
* @access public
228238
*/
229239
public function __construct(
@@ -233,7 +243,8 @@ public function __construct(
233243
$styles = null,
234244
$layouts = null,
235245
$parser = null,
236-
$component_styles = null
246+
$component_styles = null,
247+
$parent = null
237248
) {
238249
// Register specs for this component.
239250
$this->register_specs();
@@ -250,6 +261,7 @@ public function __construct(
250261
$this->component_styles = $component_styles;
251262
$this->layouts = $layouts;
252263
$this->text = $text;
264+
$this->parent = $parent;
253265
$this->json = null;
254266

255267
// Negotiate parser.
@@ -535,6 +547,20 @@ protected function register_spec( $name, $label, $spec ) {
535547
$this->specs[ $name ] = new Component_Spec( $this->get_component_name(), $name, $label, $spec );
536548
}
537549

550+
/**
551+
* Given a base name corresponding to a componentLayout, componentTextStyle, or componentStyle, returns a subcomponent
552+
* key if this component is a subcomponent, or the base name if it is not.
553+
*
554+
* @param string $name The base name of the componentLayout, componentTextStyle, or componentStyle.
555+
*
556+
* @return string The key for the componentLayout, componentTextStyle, or componentStyle with subcomponent namespacing added if necessary.
557+
*/
558+
protected function get_component_object_key( $name ) {
559+
return $this->parent instanceof Component
560+
? sprintf( '%s-subcomponent-%s', $this->parent->get_component_name(), $name )
561+
: $name;
562+
}
563+
538564
/**
539565
* Get a spec to use for creating component JSON.
540566
*
@@ -545,6 +571,7 @@ protected function register_spec( $name, $label, $spec ) {
545571
* @since 1.2.4
546572
*/
547573
protected function get_spec( $spec_name ) {
574+
// TODO: Handle parent specs.
548575
if ( ! isset( $this->specs[ $spec_name ] ) ) {
549576
return null;
550577
}
@@ -587,8 +614,8 @@ protected function register_style( $name, $spec_name, $values = [], $property =
587614
? $this->workspace->content_id
588615
: 0;
589616
$json = $component_spec->substitute_values( $values, $post_id );
590-
$this->styles->register_style( $name, $json );
591-
$this->set_json( $property, $name );
617+
$this->styles->register_style( $this->get_component_object_key( $name ), $json );
618+
$this->set_json( $property, $this->get_component_object_key( $name ) );
592619
}
593620
}
594621

@@ -610,8 +637,8 @@ protected function register_component_style( $name, $spec_name, $values = [], $p
610637
? $this->workspace->content_id
611638
: 0;
612639
$json = $component_spec->substitute_values( $values, $post_id );
613-
$this->component_styles->register_style( $name, $json );
614-
$this->set_json( $property, $name );
640+
$this->component_styles->register_style( $this->get_component_object_key( $name ), $json );
641+
$this->set_json( $property, $this->get_component_object_key( $name ) );
615642
}
616643
}
617644

@@ -632,8 +659,8 @@ protected function register_layout( $name, $spec_name, $values = [], $property =
632659
? $this->workspace->content_id
633660
: 0;
634661
$json = $component_spec->substitute_values( $values, $post_id );
635-
$this->layouts->register_layout( $name, $json );
636-
$this->set_json( $property, $name );
662+
$this->layouts->register_layout( $this->get_component_object_key( $name ), $json );
663+
$this->set_json( $property, $this->get_component_object_key( $name ) );
637664
}
638665
}
639666

0 commit comments

Comments
 (0)