Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #331 from alleyinteractive/feat/deprecate-theme-op…
Browse files Browse the repository at this point in the history
…tions

Feat/deprecate theme options
  • Loading branch information
jameswburke authored May 11, 2021
2 parents 28058fb + 996f998 commit 9e80f5e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 287 deletions.
138 changes: 0 additions & 138 deletions inc/components/class-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,6 @@ class Component implements JsonSerializable {
*/
protected $theme = '';

/**
* Theme options.
*
* @var array
*/
protected $theme_options = [];

/**
* Map of provided context.
*
Expand Down Expand Up @@ -144,7 +137,6 @@ private function setup_schema(): self {
$schema_defaults = [
'_alias' => '',
'config' => [],
'theme_options' => [ 'default' ],
'provides_context' => [],
'use_context' => [],
'config_callback' => null,
Expand All @@ -161,7 +153,6 @@ private function setup_schema(): self {

$this->set_alias( $schema['_alias'] );
$this->set_schema( $schema['config'] );
$this->set_theme_options( $schema['theme_options'] );
$this->set_provides_context( $schema['provides_context'] );
$this->set_use_context( $schema['use_context'] );
$this->set_callback( 'config', $schema['config_callback'] );
Expand Down Expand Up @@ -638,129 +629,6 @@ private function set_theme( string $theme ): self {
return $this;
}

/**
* Get all theme options.
*
* @return array
*/
public function get_theme_options(): array {
return $this->theme_options;
}

/**
* Test if a string is in the theme options.
*
* @param string $theme Theme name.
* @return bool
*/
public function is_available_theme( string $theme ): bool {
return in_array( $theme, $this->get_theme_options(), true );
}

/**
* Set theme options.
*
* @param array $theme_options New theme options. Ensures uniques.
* @return self
*/
private function set_theme_options( array $theme_options ): self {
$this->theme_options = array_unique( $theme_options );
return $this;
}

/**
* Add theme options.
*
* @param array $theme_options One or more theme names to add.
* @return self
*/
private function add_theme_options( array $theme_options ): self {

array_map(
function( $theme_option ) {

// Ignore non-strings for now.
if ( ! is_string( $theme_option ) ) {
return;
}

$this->add_theme_option( $theme_option );
},
$theme_options
);

return $this;
}

/**
* Add a theme option.
*
* @param string $theme_option Theme name.
* @return self
*/
private function add_theme_option( string $theme_option ): self {

// Ensure it's not already an option.
if ( ! $this->is_available_theme( $theme_option ) ) {
$this->theme_options[] = $theme_option;
}

return $this;
}

/**
* Remove theme options.
*
* @param array $theme_options One or more themes names to remove.
* @return self
*/
private function remove_theme_options( array $theme_options ): self {

array_map(
function( $theme_option ) {

// Ignore non-strings for now.
if ( ! is_string( $theme_option ) ) {
return;
}

$this->remove_theme_option( $theme_option );
},
$theme_options
);

return $this;
}

/**
* Remove theme option.
*
* @param string $theme_option Theme name.
* @return [type] [description]
*/
private function remove_theme_option( string $theme_option ): self {

// Ensure it's not already an option.
if ( $this->is_available_theme( $theme_option ) ) {

// Loop through options, removing the correct key.
$theme_options = $this->get_theme_options();
foreach ( $theme_options as $index => $key ) {
if ( $key === $theme_option ) {
unset( $theme_options[ $index ] );
break;
}
}

// Reset the array every time.
$this->set_theme_options(
$this->reset_array( $theme_options )
);
}

return $this;
}

/**
* Get the context provider.
*
Expand Down Expand Up @@ -1052,11 +920,6 @@ public function to_array(): array {
$do_camel_case ? $this->camel_case( $this->get_theme() ) : $this->get_theme()
);

$this->set_config_value(
'theme_options',
$do_camel_case ? array_keys( $this->camel_case_keys( array_flip( $this->get_theme_options() ) ) ) : $this->get_theme_options()
);

// Remove the `theme` key to avoid confusion with `theme_name`.
if ( 'irving/site-theme' !== $this->get_name() ) {
$this->unset_config_value( 'theme' );
Expand All @@ -1083,7 +946,6 @@ public function to_array(): array {
$force_camel_keys = [
'class_name',
'theme_name',
'theme_options',
];

foreach ( $config as $key => $value ) {
Expand Down
92 changes: 31 additions & 61 deletions tests/components/test-class-component.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public function test_component_defaults() {
$this->assertSame( [ 'theme' => 'default' ], $component->get_config(), 'Default config not empty.' );
$this->assertSame( [], $component->get_children(), 'Default children not empty.' );
$this->assertSame( 'default', $component->get_theme(), 'Default theme not set.' );
$this->assertSame( [ 'default' ], $component->get_theme_options(), 'Default theme options not set.' );
$this->assertSame( [], $component->get_context(), 'Default context values not set.' );
$this->assertNull( $component->get_callback( 'config' ), 'Default config callback not null.' );
$this->assertNull( $component->get_callback( 'children' ), 'Default children callback not null.' );
Expand Down Expand Up @@ -261,20 +260,6 @@ public function test_get_config_by_key() {
);
}

/**
* Test get_theme_options().
*/
public function test_get_theme_options() {
// 'test/theme-options' is a registered component type.
$component = new Component( 'test/theme-options' );

$this->assertSame(
[ 'primary', 'secondary' ],
$component->get_theme_options(),
'Did not get expected theme options.'
);
}

/**
* Test get_theme().
*
Expand Down Expand Up @@ -608,10 +593,9 @@ public function get_components_to_serialize() {
'name' => 'test/basic',
'_alias' => '',
'config' => (object) [
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [ 'default' ],
'className' => '',
'style' => [],
'themeName' => 'default',
],
'children' => [],
],
Expand All @@ -631,11 +615,10 @@ public function get_components_to_serialize() {
'name' => 'test/basic',
'_alias' => '',
'config' => (object) [
'foo' => 'bar',
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [ 'default' ],
'foo' => 'bar',
'className' => '',
'style' => [],
'themeName' => 'default',
],
'children' => [],
],
Expand All @@ -648,10 +631,9 @@ public function get_components_to_serialize() {
'name' => 'test/alias',
'_alias' => 'test/component',
'config' => (object) [
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [ 'default' ],
'className' => '',
'style' => [],
'themeName' => 'default',
],
'children' => [],
],
Expand All @@ -664,11 +646,10 @@ public function get_components_to_serialize() {
'name' => 'test/schema',
'_alias' => '',
'config' => (object) [
'testDefault' => 'default',
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [ 'default' ],
'testDefault' => 'default',
'className' => '',
'style' => [],
'themeName' => 'default',
],
'children' => [],
],
Expand All @@ -681,10 +662,9 @@ public function get_components_to_serialize() {
'name' => 'test/theme-options',
'_alias' => '',
'config' => (object) [
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [ 'primary', 'secondary' ],
'className' => '',
'style' => [],
'themeName' => 'default',
],
'children' => [],
],
Expand All @@ -710,7 +690,6 @@ public function get_components_to_serialize() {
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [ 'default' ],
],
'children' => [],
],
Expand All @@ -732,13 +711,10 @@ public function test_json_serialize_component_array_filter() {
'name' => 'test/basic',
'_alias' => '',
'config' => (object) [
'className' => '',
'style' => [],
'test' => true,
'themeName' => 'default',
'themeOptions' => [
'default',
],
'className' => '',
'style' => [],
'test' => true,
'themeName' => 'default',
],
'children' => [],
],
Expand All @@ -756,12 +732,9 @@ public function test_json_serialize_component_array_filter() {
'name' => 'test/basic',
'_alias' => '',
'config' => (object) [
'className' => '',
'style' => [],
'themeName' => 'default',
'themeOptions' => [
'default',
],
'className' => '',
'style' => [],
'themeName' => 'default',
],
'children' => [],
],
Expand Down Expand Up @@ -798,16 +771,13 @@ public function test_disabling_camel_casing() {
'name' => 'example/no-camels',
'_alias' => '',
'config' => (object) [
'className' => '',
'style' => [],
'themeName' => 'no_camels',
'themeOptions' => [
'default',
],
'under_score' => 'under_score',
'camelCase' => 'camelCase',
'cabob-case' => 'cabob-case',
'nested_case' => [
'className' => '',
'style' => [],
'themeName' => 'no_camels',
'under_score' => 'under_score',
'camelCase' => 'camelCase',
'cabob-case' => 'cabob-case',
'nested_case' => [
'under_score' => 'under_score',
'camelCase' => 'camelCase',
'cabob-case' => 'cabob-case',
Expand Down
Loading

0 comments on commit 9e80f5e

Please sign in to comment.