|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + * @package MaterialDesign |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * Material design customizer global styles. |
| 22 | + * |
| 23 | + * @package MaterialDesign |
| 24 | + */ |
| 25 | + |
| 26 | +namespace MaterialDesign\Theme\Customizer\Global_Style; |
| 27 | + |
| 28 | +use MaterialDesign\Theme\Customizer; |
| 29 | + |
| 30 | +/** |
| 31 | + * Attach hooks. |
| 32 | + * |
| 33 | + * @return void |
| 34 | + */ |
| 35 | +function setup() { |
| 36 | + if ( material_is_plugin_active() ) { |
| 37 | + add_action( 'customize_register', __NAMESPACE__ . '\register' ); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Add and initialize colors section |
| 43 | + * |
| 44 | + * @param WP_Customize_Manager $wp_customize Theme Customizer object. |
| 45 | + */ |
| 46 | +function register( $wp_customize ) { |
| 47 | + // Add color palettes section. |
| 48 | + $args = [ |
| 49 | + 'priority' => 300, |
| 50 | + 'title' => esc_html__( 'Global styles', 'material-design-google' ), |
| 51 | + 'description' => esc_html__( 'Global style change will affect all the blocks used in your pages. This may override styles applied to blocks locally.', 'material-design-google' ), |
| 52 | + ]; |
| 53 | + |
| 54 | + Customizer\add_section( $wp_customize, 'global-setting', $args ); |
| 55 | + add_settings( $wp_customize ); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Create settings based on controls |
| 60 | + * |
| 61 | + * @param WP_Customize_Manager $wp_customize Theme Customizer object. |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | +function add_settings( $wp_customize ) { |
| 66 | + $settings = []; |
| 67 | + $controls = []; |
| 68 | + |
| 69 | + foreach ( get_controls() as $control ) { |
| 70 | + $settings[ $control['id'] ] = [ |
| 71 | + 'transport' => 'refresh', |
| 72 | + 'sanitize_callback' => Customizer\get_sanitize_callback( $control['type'] ), |
| 73 | + ]; |
| 74 | + |
| 75 | + $controls[ $control['id'] ] = array_merge( |
| 76 | + [ |
| 77 | + 'section' => Customizer\prepend_slug( 'global-setting' ), |
| 78 | + ], |
| 79 | + $control |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + Customizer\add_settings( $wp_customize, $settings ); |
| 84 | + Customizer\add_controls( $wp_customize, $controls ); |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Define color controls to use |
| 89 | + * |
| 90 | + * @return array |
| 91 | + */ |
| 92 | +function get_controls() { |
| 93 | + return [ |
| 94 | + [ |
| 95 | + 'id' => 'card_style', |
| 96 | + 'label' => esc_html__( 'Cards', 'material-design-google' ), |
| 97 | + 'type' => 'radio', |
| 98 | + 'default' => 'elevated', |
| 99 | + 'choices' => [ |
| 100 | + 'elevated' => esc_html__( 'Elevated', 'material-design-google' ), |
| 101 | + 'outlined' => esc_html__( 'Outlined', 'material-design-google' ), |
| 102 | + ], |
| 103 | + ], |
| 104 | + [ |
| 105 | + 'id' => 'text_style', |
| 106 | + 'label' => esc_html__( 'Text field', 'material-design-google' ), |
| 107 | + 'type' => 'radio', |
| 108 | + 'default' => 'elevated', |
| 109 | + 'choices' => [ |
| 110 | + 'elevated' => esc_html__( 'Elevated', 'material-design-google' ), |
| 111 | + 'outlined' => esc_html__( 'Outlined', 'material-design-google' ), |
| 112 | + ], |
| 113 | + ], |
| 114 | + ]; |
| 115 | +} |
0 commit comments