Skip to content

Commit

Permalink
Make linter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Nov 10, 2020
1 parent 9dae680 commit b43d928
Showing 1 changed file with 61 additions and 49 deletions.
110 changes: 61 additions & 49 deletions lib/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class WP_Theme_JSON {

/**
* Container of data in theme.json format.
*/
*
* @var array
*/
private $contexts = null;

/**
Expand Down Expand Up @@ -52,11 +54,11 @@ class WP_Theme_JSON {
* }
* }
*/
private const SCHEMA = array(
const SCHEMA = array(
'selector' => null,
'supports' => null,
'styles' => array(
'color' => array(
'styles' => array(
'color' => array(
'background' => null,
'gradient' => null,
'link' => null,
Expand All @@ -73,14 +75,14 @@ class WP_Theme_JSON {
),
),
'settings' => array(
'color' => array(
'color' => array(
'custom' => null,
'customGradient' => null,
'gradients' => null,
'link' => null,
'palette' => null,
),
'spacing' => array(
'spacing' => array(
'customPadding' => null,
'units' => null,
),
Expand All @@ -95,7 +97,7 @@ class WP_Theme_JSON {
'textDecorations' => null,
'textTransforms' => null,
),
'custom' => null,
'custom' => null,
),
);

Expand All @@ -105,15 +107,15 @@ class WP_Theme_JSON {
*
* This contains the necessary metadata to process them.
*/
private const PRESETS_METADATA = array(
const PRESETS_METADATA = array(
array(
'path' => array( 'settings', 'color', 'palette' ),
'value_key' => 'color',
'preset_infix' => 'color',
'class' => array(
'class' => array(
array(
'suffix' => 'color',
'property' => 'color'
'suffix' => 'color',
'property' => 'color',
),
array(
'suffix' => 'background-color',
Expand All @@ -125,18 +127,18 @@ class WP_Theme_JSON {
'path' => array( 'settings', 'color', 'gradients' ),
'value_key' => 'gradient',
'preset_infix' => 'gradient',
'class' => array(
'class' => array(
array(
'suffix' => 'gradient-background',
'property' => 'background'
'property' => 'background',
),
),
),
array(
'path' => array( 'settings', 'typography', 'fontSizes' ),
'value_key' => 'size',
'preset_infix' => 'font-size',
'class' => array(
'class' => array(
array(
'suffix' => 'font-size',
'property' => 'font-size',
Expand All @@ -147,7 +149,7 @@ class WP_Theme_JSON {
'path' => array( 'settings', 'typography', 'fontFamilies' ),
'value_key' => 'fontFamily',
'preset_infix' => 'font-family',
'class' => array(
'class' => array(
array(
'suffix' => 'font-family',
'property' => 'font-family',
Expand All @@ -158,7 +160,7 @@ class WP_Theme_JSON {
'path' => array( 'settings', 'typography', 'fontStyles' ),
'value_key' => 'slug',
'preset_infix' => 'font-style',
'class' => array(
'class' => array(
array(
'suffix' => 'font-style',
'property' => 'font-style',
Expand All @@ -169,7 +171,7 @@ class WP_Theme_JSON {
'path' => array( 'settings', 'typography', 'fontWeights' ),
'value_key' => 'slug',
'preset_infix' => 'font-weight',
'class' => array(
'class' => array(
array(
'suffix' => 'font-weight',
'property' => 'font-weight',
Expand All @@ -180,7 +182,7 @@ class WP_Theme_JSON {
'path' => array( 'settings', 'typography', 'textDecorations' ),
'value_key' => 'value',
'preset_infix' => 'text-decoration',
'class' => array(
'class' => array(
array(
'suffix' => 'text-decoration',
'property' => 'text-decoration',
Expand All @@ -191,7 +193,7 @@ class WP_Theme_JSON {
'path' => array( 'settings', 'typography', 'textTransforms' ),
'value_key' => 'slug',
'preset_infix' => 'text-transform',
'class' => array(
'class' => array(
array(
'suffix' => 'text-transform',
'property' => 'text-transform',
Expand All @@ -204,7 +206,7 @@ class WP_Theme_JSON {
* Metadata to know which style properties are supported
* by theme.json and block.json an how to process them.
*/
private const PROPERTIES_METADATA = array(
const PROPERTIES_METADATA = array(
'--wp--style--color--link' => array(
'theme_json' => array( 'color', 'link' ),
'block_json' => array( 'color', 'link' ),
Expand Down Expand Up @@ -233,7 +235,7 @@ class WP_Theme_JSON {
'fontSize' => array(
'theme_json' => array( 'typography', 'fontSize' ),
'block_json' => array( 'fontSize' ),
'property' => 'font-size'
'property' => 'font-size',
),
'fontStyle' => array(
'theme_json' => array( 'typography', 'fontStyle' ),
Expand All @@ -243,7 +245,7 @@ class WP_Theme_JSON {
'fontWeight' => array(
'theme_json' => array( 'typography', 'fontWeight' ),
'block_json' => array( '__experimentalFontAppearance' ),
'property' => 'font-weight',
'property' => 'font-weight',
),
'lineHeight' => array(
'theme_json' => array( 'typography', 'lineHeight' ),
Expand All @@ -267,7 +269,7 @@ class WP_Theme_JSON {
*
* @param array $contexts A structure that follows the theme.json schema.
*/
public function __construct( $contexts = array() ){
public function __construct( $contexts = array() ) {
$this->contexts = array();

if ( ! is_array( $contexts ) ) {
Expand Down Expand Up @@ -365,9 +367,9 @@ public function get_blocks_metadata() {
'fontSize' => true,
'lineHeight' => true,
),
),
)
),
),
)
);
foreach ( $blocks as $block_name => $block_type ) {
if (
Expand Down Expand Up @@ -446,8 +448,8 @@ public function get_blocks_metadata() {
* the nodes that aren't valid per the schema.
*
* @param string $key Key of the subtree to normalize.
* @param array $input Whole tree to normalize.
* @param array $schema Schema to use for normalization.
* @param array $input Whole tree to normalize.
* @param array $schema Schema to use for normalization.
*/
private function process_key( $key, &$input, $schema ) {
if ( ! is_array( $input ) || ! array_key_exists( $key, $input ) ) {
Expand All @@ -466,7 +468,7 @@ private function process_key( $key, &$input, $schema ) {

$input[ $key ] = array_intersect_key(
$input[ $key ],
$schema[ $key ],
$schema[ $key ]
);

if ( 0 === count( $input[ $key ] ) ) {
Expand Down Expand Up @@ -603,7 +605,7 @@ private function get_property_value( $styles, $path ) {
$token_out,
substr( $value, $prefix_len )
);
$value = "var(--wp--$unwrapped_name)";
$value = "var(--wp--$unwrapped_name)";
}

return $value;
Expand All @@ -613,10 +615,12 @@ private function get_property_value( $styles, $path ) {
* Given a context, it extracts the style properties
* and adds them to the $declarations array following the format:
*
* ```php
* array(
* 'name' => 'property_name',
* 'value' => 'property_value,
* )
* ```
*
* Note that this modifies the $declarations in place.
*
Expand All @@ -634,7 +638,7 @@ private function compute_style_properties( &$declarations, $context ) {
}

foreach ( self::PROPERTIES_METADATA as $name => $metadata ) {
if ( ! in_array( $name, $context['supports'] ) ) {
if ( ! in_array( $name, $context['supports'], true ) ) {
continue;
}

Expand All @@ -655,20 +659,20 @@ private function compute_style_properties( &$declarations, $context ) {
* Note this function modifies $stylesheet in place.
*
* @param string $stylesheet Input stylesheet to add the presets to.
* @param array $context Context to process.
* @param array $context Context to process.
*/
private function compute_preset_classes( &$stylesheet, $context ) {
foreach ( self::PRESETS_METADATA as $preset ) {
$values = $this->get_from_path( $context, $preset['path'], array() );
foreach ( $values as $value ) {
foreach ( $preset['class'] as $class ) {
$stylesheet .= $this->to_ruleset(
$context['selector'] . ".has-" . $value['slug'] . "-" . $class['suffix'],
$context['selector'] . '.has-' . $value['slug'] . '-' . $class['suffix'],
array(
array(
'name' => $class['property'],
'value' => $value[ $preset['value_key'] ]
)
'value' => $value[ $preset['value_key'] ],
),
)
);
}
Expand All @@ -681,10 +685,12 @@ private function compute_preset_classes( &$stylesheet, $context ) {
* for the presets and adds them to the $declarations array
* following the format:
*
* ```php
* array(
* 'name' => 'property_name',
* 'value' => 'property_value,
* )
* ```
*
* Note that this modifies the $declarations in place.
*
Expand All @@ -708,18 +714,20 @@ private function compute_preset_vars( &$declarations, $context ) {
* for the custom values and adds them to the $declarations
* array following the format:
*
* ```php
* array(
* 'name' => 'property_name',
* 'value' => 'property_value,
* )
* ```
*
* Note that this modifies the $declarations in place.
*
* @param array $declarations Holds the existing declarations.
* @param array $context Input context to process.
*/
private function compute_theme_vars( &$declarations, $context ) {
$custom_values = $this->get_from_path( $context, [ 'settings', 'custom' ] );
$custom_values = $this->get_from_path( $context, array( 'settings', 'custom' ) );
$css_vars = $this->flatten_tree( $custom_values );
foreach ( $css_vars as $key => $value ) {
$declarations[] = array(
Expand All @@ -737,7 +745,7 @@ private function compute_theme_vars( &$declarations, $context ) {
* if SCRIPT_DEBUG is defined and true.
*
* @param string $selector CSS selector.
* @param array $declarations List of declarations.
* @param array $declarations List of declarations.
*
* @return string CSS ruleset.
*/
Expand All @@ -747,17 +755,19 @@ private function to_ruleset( $selector, $declarations ) {
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
$declaration_block = array_reduce(
$declarations,
function ( $carry, $element ) { return $carry .= "\t" . $element['name'] . ': ' . $element['value'] . ";\n"; },
'',
function ( $carry, $element ) {
return $carry .= "\t" . $element['name'] . ': ' . $element['value'] . ";\n"; },
''
);
$ruleset .= $selector . " {\n" . $declaration_block . "}\n";
$ruleset .= $selector . " {\n" . $declaration_block . "}\n";
} else {
$declaration_block = array_reduce(
$declarations,
function ( $carry, $element ) { return $carry .= $element['name'] . ': ' . $element['value'] . ';'; },
'',
function ( $carry, $element ) {
return $carry .= $element['name'] . ': ' . $element['value'] . ';'; },
''
);
$ruleset .= $selector . '{' . $declaration_block . '}';
$ruleset .= $selector . '{' . $declaration_block . '}';
}

return $ruleset;
Expand Down Expand Up @@ -796,8 +806,8 @@ function ( $carry, $element ) { return $carry .= $element['name'] . ': ' . $elem
* background: value;
* }
*
* @param string $stylesheet
* @param array $context Context to be processed.
* @param string $stylesheet Stylesheet to append new rules to.
* @param array $context Context to be processed.
*
* @return string The new stylesheet.
*/
Expand Down Expand Up @@ -853,7 +863,9 @@ private function to_stylesheet( $stylesheet, $context ) {
public function get_settings() {
return array_filter(
array_map( array( $this, 'extract_settings' ), $this->contexts ),
function ( $element ) { return $element !== null; }
function ( $element ) {
return null !== $element;
}
);
}

Expand All @@ -870,7 +882,7 @@ public function get_stylesheet() {
/**
* Merge new incoming data.
*
* @param WP_Theme_JSON $incoming Data to merge.
* @param WP_Theme_JSON $theme_json Data to merge.
*/
public function merge( $theme_json ) {
$incoming_data = $theme_json->get_raw_data();
Expand All @@ -881,7 +893,7 @@ public function merge( $theme_json ) {
$this->contexts[ $context ]['selector'] = $metadata[ $context ]['selector'];
$this->contexts[ $context ]['supports'] = $metadata[ $context ]['supports'];

foreach ( [ 'settings', 'styles' ] as $subtree ) {
foreach ( array( 'settings', 'styles' ) as $subtree ) {
if ( ! array_key_exists( $subtree, $incoming_data[ $context ] ) ) {
continue;
}
Expand All @@ -903,7 +915,7 @@ public function merge( $theme_json ) {

$this->contexts[ $context ][ $subtree ][ $leaf ] = array_merge(
$this->contexts[ $context ][ $subtree ][ $leaf ],
$incoming_data[ $context ][ $subtree ][ $leaf ],
$incoming_data[ $context ][ $subtree ][ $leaf ]
);
}
}
Expand All @@ -919,4 +931,4 @@ public function get_raw_data() {
return $this->contexts;
}

}
}

0 comments on commit b43d928

Please sign in to comment.