From ad306b2982c66004e75d3971c6594343cb872953 Mon Sep 17 00:00:00 2001 From: Kevin Fodness <2650828+kevinfodness@users.noreply.github.com> Date: Wed, 29 May 2024 05:39:47 -0400 Subject: [PATCH] Add subcomponent selector to Customize JSON screen --- admin/class-admin-apple-json.php | 62 +++++++++++++++---- admin/partials/page-json.php | 19 ++++++ assets/js/json.js | 4 ++ .../apple-exporter/components/class-aside.php | 7 +++ .../components/class-component.php | 18 ++++++ 5 files changed, 98 insertions(+), 12 deletions(-) diff --git a/admin/class-admin-apple-json.php b/admin/class-admin-apple-json.php index 4183058e5..25736a79f 100644 --- a/admin/class-admin-apple-json.php +++ b/admin/class-admin-apple-json.php @@ -5,6 +5,8 @@ * @package Apple_News */ +use Apple_Exporter\Components\Component; + /** * This class is in charge of handling the management of custom JSON. */ @@ -35,6 +37,14 @@ class Admin_Apple_JSON extends Apple_News { */ private $selected_component = ''; + /** + * Holds the selected subcomponent from the request. + * + * @since 2.5.0 + * @var string + */ + private $selected_subcomponent = ''; + /** * Holds the selected theme from the request. * @@ -100,6 +110,14 @@ public function action_router() { $this->selected_component = ''; } + // Store the selected subcomponent value for use later. + $this->selected_subcomponent = isset( $_POST['apple_news_subcomponent'] ) + ? sanitize_text_field( wp_unslash( $_POST['apple_news_subcomponent'] ) ) + : ''; + if ( ! array_key_exists( $this->selected_subcomponent, $this->list_components() ) ) { + $this->selected_subcomponent = ''; + } + // Store the selected theme for use later. if ( ! empty( $_POST['apple_news_theme'] ) ) { $this->selected_theme = sanitize_text_field( wp_unslash( $_POST['apple_news_theme'] ) ); @@ -185,10 +203,14 @@ public function page_json_render() { ? $this->get_selected_component() : ''; - // If we have a class, get its specs. - $specs = ( ! empty( $selected_component ) ) - ? $this->get_specs( $selected_component ) - : []; + // Handle subcomponents. + $component_can_be_parent = $this->get_component_class( $selected_component )?->can_be_parent(); + $selected_subcomponent = ( ! empty( $selected_component ) ) + ? $this->get_selected_subcomponent() + : ''; + + // If we have a component or subcomponent, get its specs. + $specs = $this->get_specs( $selected_subcomponent ?: $selected_component ); /* phpcs:enable */ @@ -365,21 +387,28 @@ private function save_json() { } } + /** + * Given a component slug, returns the associated component class. + * + * @param string $component The component to get the class for. + * + * @return ?Component + */ + private function get_component_class( $component ) { + $classname = $this->namespace . $component; + return class_exists( $classname ) ? new $classname() : null; + } + /** * Loads the JSON specs that can be customized for the component * * @param string $component The component to get specs for. + * * @return array - * @access private */ private function get_specs( $component ) { - if ( empty( $component ) ) { - return []; - } - - $classname = $this->namespace . $component; - $component_class = new $classname(); - return $component_class->get_specs(); + $component_class = $this->get_component_class( $component ); + return $component_class ? $component_class->get_specs() : []; } /** @@ -414,6 +443,15 @@ public function get_selected_component() { return $this->selected_component; } + /** + * Gets the currently selected subcomponent. + * + * @return string + */ + public function get_selected_subcomponent() { + return $this->selected_subcomponent; + } + /** * Checks for a valid selected theme. * diff --git a/admin/partials/page-json.php b/admin/partials/page-json.php index 1dba63649..9604007ca 100644 --- a/admin/partials/page-json.php +++ b/admin/partials/page-json.php @@ -5,8 +5,10 @@ * phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable * * @global array $all_themes + * @global bool $component_can_be_parent * @global array $components * @global string $selected_component + * @global string $selected_subcomponent * @global string $selected_theme * @global array $specs * @global string $theme_admin_url @@ -102,6 +104,23 @@ + +
+ +
+ + can_be_anchor_target && empty( $this->uid ); } + /** + * Returns whether this component can be a parent (have subcomponents). + * + * @since 2.5.0 + * @return boolean + */ + public function can_be_parent() { + return $this->can_be_parent; + } + /** * Get the current UID. *