Skip to content

Commit

Permalink
Merge pull request #148 from material-components/revert-127-customize…
Browse files Browse the repository at this point in the history
…r/global-settings

Revert "Add global settings for card and text field"
  • Loading branch information
emeaguiar authored Jun 3, 2021
2 parents 8cc470d + 655a26a commit 1b599de
Show file tree
Hide file tree
Showing 20 changed files with 38 additions and 291 deletions.
1 change: 0 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ module.exports = function( grunt ) {
'!theme/assets/css/*.map',
'!theme/assets/js/.gitignore',
'!theme/assets/js/*.css',
'!theme/assets/js/*.php',
'!theme/assets/js/*.map',
'!theme/assets/js/editor*',
'!theme/assets/src/**',
Expand Down
2 changes: 1 addition & 1 deletion plugin/assets/css/src/customize-controls.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
content: "\f460" !important;
}

& .customize-section-description-container .customize-section-title {
& .customize-section-description-container {
display: none;
}
}
Expand Down
3 changes: 1 addition & 2 deletions plugin/assets/src/block-editor/blocks/card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import edit from './edit';
import save from './save';
import metadata from './block.json';
import { example } from './example';
import { isDefaultCardStyleOutlined } from '../../utils';
metadata.attributes.outlined.default = isDefaultCardStyleOutlined();

const { name } = metadata;

export { metadata, name };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import edit from './edit';
import save from './save';
import { example } from './example';
import metadata from './block.json';
import { isDefaultCardStyleOutlined } from '../../utils';

const { name } = metadata;

metadata.attributes.outlined.default = isDefaultCardStyleOutlined();
export { metadata, name };

/**
Expand Down
3 changes: 0 additions & 3 deletions plugin/assets/src/block-editor/blocks/contact-form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import { __ } from '@wordpress/i18n';
import edit from './edit';
import save from './save';
import metadata from './block.json';
import { isDefaultTextFieldStyleOutlined } from '../../utils';

metadata.attributes.outlined.default = isDefaultTextFieldStyleOutlined();

const { name } = metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import metadata from './block.json';

const { name } = metadata;

import { isDefaultCardStyleOutlined } from '../../utils';
metadata.attributes.outlined.default = isDefaultCardStyleOutlined();
export { metadata, name };

/**
Expand Down
3 changes: 1 addition & 2 deletions plugin/assets/src/block-editor/blocks/recent-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ import { __ } from '@wordpress/i18n';
import edit from './edit';
import metadata from './block.json';
import { example } from './example';
import { isDefaultCardStyleOutlined } from '../../utils';

const { name } = metadata;
metadata.attributes.outlined.default = isDefaultCardStyleOutlined();

export { metadata, name };

/**
Expand Down
2 changes: 0 additions & 2 deletions plugin/assets/src/block-editor/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@
export { default as findIcon } from './find-icon';
export { default as getConfig } from './get-config';
export { default as getIconName } from './get-icon-name';
export { default as isDefaultCardStyleOutlined } from './is-default-card-style-outlined';
export { default as isDefaultTextFieldStyleOutlined } from './is-default-text-field-outlined';

This file was deleted.

This file was deleted.

84 changes: 13 additions & 71 deletions plugin/php/class-block-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,19 @@ public function register_blocks() {

$blocks_dir = $this->plugin->dir_path . '/assets/js/blocks/';
$block_folders = [
'button' => [],
'card' => [
'outlined' => [ $this, 'is_default_card_style_outlined' ],
],
'cards-collection' => [
'outlined' => [ $this, 'is_default_card_style_outlined' ],
],
'contact-form' => [
'outlined' => [ $this, 'is_default_text_field_style_outlined' ],
],
'data-table' => [],
'hand-picked-posts' => [],
'image-list' => [],
'list' => [],
'recent-posts' => [
'outlined' => [ $this, 'is_default_card_style_outlined' ],
],
'tab-bar' => [],
'button',
'card',
'cards-collection',
'contact-form',
'data-table',
'hand-picked-posts',
'image-list',
'list',
'recent-posts',
'tab-bar',
];

foreach ( $block_folders as $block_name => $override_attributes ) {
foreach ( $block_folders as $block_name ) {
$block_json_file = $blocks_dir . $block_name . '/block.json';
if ( ! file_exists( $block_json_file ) ) {
continue;
Expand All @@ -117,10 +109,6 @@ public function register_blocks() {
continue;
}

foreach ( $override_attributes as $attr_key => $attr ) {
$metadata['attributes'][ $attr_key ]['default'] = call_user_func( $attr );
}

$metadata['editor_script'] = 'material-block-editor-js';
$metadata['editor_style'] = 'material-block-editor-css';

Expand Down Expand Up @@ -225,9 +213,8 @@ public function enqueue_block_editor_assets() {
'tab_bar_preview' => $this->plugin->asset_url( 'assets/images/preview/tab-bar.jpg' ),
'contact_form_preview' => $this->plugin->asset_url( 'assets/images/preview/contact-form.jpg' ),
'defaults' => [
'blocks' => $this->get_block_defaults(),
'colors' => $this->get_color_defaults(),
'globalStyle' => $this->get_global_styles(),
'blocks' => $this->get_block_defaults(),
'colors' => $this->get_color_defaults(),
],
'customizerUrls' => [
'colors' => add_query_arg( 'autofocus[section]', $slug . '_colors', $customizer_url ),
Expand Down Expand Up @@ -321,51 +308,6 @@ public function get_color_defaults() {
return $defaults;
}

/**
* Get global style configs.
*
* @param string $key get single value.
*
* @return array|string
*/
public function get_global_styles( $key = null ) {
$defaults = [];
$controls = $this->plugin->customizer_controls;

foreach ( $controls->get_global_style_controls() as $control ) {
$value = $controls->get_option( $control['id'] );
if ( ! empty( $value ) ) {
$defaults[ $control['id'] ] = $value;
}
}

return ( $key ? ( isset( $defaults[ $key ] ) ? $defaults[ $key ] : '' ) : $defaults );
}

/**
* Whether default card style is outlined.
*
* @return bool
*/
public function is_default_card_style_outlined() {
$style = $this->get_global_styles();
$style = isset( $style['card_style'] ) ? $style['card_style'] : '';

return 'outlined' === $style;
}

/**
* Whether default text field style is outlined.
*
* @return bool
*/
public function is_default_text_field_style_outlined() {
$style = $this->get_global_styles();
$style = isset( $style['text_field_style'] ) ? $style['text_field_style'] : '';

return 'outlined' === $style;
}

/**
* Gets the REST API controller for a post type.
*
Expand Down
111 changes: 9 additions & 102 deletions plugin/php/customizer/class-controls.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,6 @@ public function register( $wp_customize ) {

// Add all controls in the "Icon Styles" section.
$this->add_icon_collection_controls();

// Add global style control.
$this->add_global_style_control();
}

/**
Expand Down Expand Up @@ -131,38 +128,31 @@ public function add_panel() {
*/
public function add_sections() {
$sections = [
'style' => __( 'Starter Styles', 'material-design' ),
'colors' => __( 'Color Palette ', 'material-design' ),
'typography' => __( 'Typography (Font Styles)', 'material-design' ),
'corner_styles' => __( 'Shape Size', 'material-design' ),
'icons' => __( 'Icon Styles', 'material-design' ),
'global-setting' => [
'label' => __( 'Global Styles', 'material-design' ),
'priority' => 300,
'description' => esc_html__( 'Global styles will be applied to any new and existing static block using the default style. This will not affect any local style overrides for supported blocks.', 'material-design' ),
],
'style' => __( 'Starter Styles', 'material-design' ),
'colors' => __( 'Color Palette ', 'material-design' ),
'typography' => __( 'Typography (Font Styles)', 'material-design' ),
'corner_styles' => __( 'Shape Size', 'material-design' ),
'icons' => __( 'Icon Styles', 'material-design' ),
];

foreach ( $sections as $id => $maybe_label ) {
foreach ( $sections as $id => $label ) {
$id = $this->prepend_slug( $id );

$label = is_array( $maybe_label ) ? $maybe_label['label'] : $maybe_label;
$args = [
$args = [
'priority' => 10,
'capability' => 'edit_theme_options',
'title' => esc_html( $label ),
'panel' => $this->slug,
'type' => 'collapse',
];
$args = is_array( $maybe_label ) ? array_merge( $args, $maybe_label ) : $args;

/**
* Filters the customizer section args.
*
* This allows other plugins/themes to change the customizer section args.
*
* @param array $args Array of section args.
* @param string $id ID of the section.
* @param array $args Array of section args.
* @param string $id ID of the section.
*/
$section = apply_filters( $this->slug . '_customizer_section_args', $args, $id );

Expand Down Expand Up @@ -514,89 +504,6 @@ function ( $child ) {
}
}

/**
* Select sanitization callback.
*
* - Sanitization: select
* - Control: select, radio
*
* Sanitization callback for 'select' and 'radio' type controls. This callback sanitizes `$input`
* as a slug, and then validates `$input` against the choices defined for the control.
*
* @see sanitize_key() https://developer.wordpress.org/reference/functions/sanitize_key/
* @see $wp_customize->get_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/get_control/
*
* @param string $input Slug to sanitize.
* @param WP_Customize_Setting $setting Setting instance.
* @return string Sanitized slug if it is a valid choice; otherwise, the setting default.
*/
public function sanitize_select( $input, $setting ) {
// Ensure input is a slug.
$input = sanitize_key( $input );

// Get list of choices from the control associated with the setting.
$choices = $setting->manager->get_control( $setting->id )->choices;

// If the input is a valid key, return it; otherwise, return the default.
return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}

/**
* Get global style controls.
*
* @return array[]
*/
public function get_global_style_controls() {
return [
[
'id' => 'card_style',
'label' => esc_html__( 'Cards', 'material-design' ),
'type' => 'radio',
'default' => 'elevated',
'choices' => [
'elevated' => esc_html__( 'Elevated', 'material-design' ),
'outlined' => esc_html__( 'Outlined', 'material-design' ),
],
],
[
'id' => 'text_field_style',
'label' => esc_html__( 'Text field', 'material-design' ),
'type' => 'radio',
'default' => 'elevated',
'choices' => [
'elevated' => esc_html__( 'Elevated', 'material-design' ),
'outlined' => esc_html__( 'Outlined', 'material-design' ),
],
],
];
}

/**
* Add global style control.
*/
public function add_global_style_control() {
$settings = [];
$controls = [];

foreach ( $this->get_global_style_controls() as $control ) {
$settings[ $control['id'] ] = [
'transport' => 'refresh',
'sanitize_callback' => [ $this, 'sanitize_select' ],
'default' => $control['default'],
];

$controls[ $control['id'] ] = array_merge(
[
'section' => 'global-setting',
],
$control
);
}

$this->add_settings( $settings );
$this->add_controls( $controls );
}

/**
* Enqueue Customizer scripts.
*/
Expand Down
Loading

0 comments on commit 1b599de

Please sign in to comment.