@@ -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