[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-content/themes/twentytwenty/classes/ -> class-twentytwenty-customize.php (source)

   1  <?php
   2  /**
   3   * Customizer settings for this theme.
   4   *
   5   * @package WordPress
   6   * @subpackage Twenty_Twenty
   7   * @since 1.0.0
   8   */
   9  
  10  if ( ! class_exists( 'TwentyTwenty_Customize' ) ) {
  11      /**
  12       * CUSTOMIZER SETTINGS
  13       */
  14      class TwentyTwenty_Customize {
  15  
  16          /**
  17           * Register customizer options.
  18           *
  19           * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  20           */
  21  		public static function register( $wp_customize ) {
  22  
  23              /**
  24               * Site Title & Description.
  25               * */
  26              $wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
  27              $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  28  
  29              $wp_customize->selective_refresh->add_partial(
  30                  'blogname',
  31                  array(
  32                      'selector'        => '.site-title a',
  33                      'render_callback' => 'twentytwenty_customize_partial_blogname',
  34                  )
  35              );
  36  
  37              $wp_customize->selective_refresh->add_partial(
  38                  'blogdescription',
  39                  array(
  40                      'selector'        => '.site-description',
  41                      'render_callback' => 'twentytwenty_customize_partial_blogdescription',
  42                  )
  43              );
  44  
  45              $wp_customize->selective_refresh->add_partial(
  46                  'custom_logo',
  47                  array(
  48                      'selector'        => '.header-titles [class*=site-]:not(.site-description)',
  49                      'render_callback' => 'twentytwenty_customize_partial_site_logo',
  50                  )
  51              );
  52  
  53              $wp_customize->selective_refresh->add_partial(
  54                  'retina_logo',
  55                  array(
  56                      'selector'        => '.header-titles [class*=site-]:not(.site-description)',
  57                      'render_callback' => 'twentytwenty_customize_partial_site_logo',
  58                  )
  59              );
  60  
  61              /**
  62               * Site Identity
  63               */
  64  
  65              /* 2X Header Logo ---------------- */
  66              $wp_customize->add_setting(
  67                  'retina_logo',
  68                  array(
  69                      'capability'        => 'edit_theme_options',
  70                      'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
  71                      'transport'         => 'postMessage',
  72                  )
  73              );
  74  
  75              $wp_customize->add_control(
  76                  'retina_logo',
  77                  array(
  78                      'type'        => 'checkbox',
  79                      'section'     => 'title_tagline',
  80                      'priority'    => 10,
  81                      'label'       => __( 'Retina logo', 'twentytwenty' ),
  82                      'description' => __( 'Scales the logo to half its uploaded size, making it sharp on high-res screens.', 'twentytwenty' ),
  83                  )
  84              );
  85  
  86              // Header & Footer Background Color.
  87              $wp_customize->add_setting(
  88                  'header_footer_background_color',
  89                  array(
  90                      'default'           => '#ffffff',
  91                      'sanitize_callback' => 'sanitize_hex_color',
  92                      'transport'         => 'postMessage',
  93                  )
  94              );
  95  
  96              $wp_customize->add_control(
  97                  new WP_Customize_Color_Control(
  98                      $wp_customize,
  99                      'header_footer_background_color',
 100                      array(
 101                          'label'   => __( 'Header &amp; Footer Background Color', 'twentytwenty' ),
 102                          'section' => 'colors',
 103                      )
 104                  )
 105              );
 106  
 107              // Enable picking an accent color.
 108              $wp_customize->add_setting(
 109                  'accent_hue_active',
 110                  array(
 111                      'capability'        => 'edit_theme_options',
 112                      'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
 113                      'transport'         => 'postMessage',
 114                      'default'           => 'default',
 115                  )
 116              );
 117  
 118              $wp_customize->add_control(
 119                  'accent_hue_active',
 120                  array(
 121                      'type'    => 'radio',
 122                      'section' => 'colors',
 123                      'label'   => __( 'Primary Color', 'twentytwenty' ),
 124                      'choices' => array(
 125                          'default' => __( 'Default', 'twentytwenty' ),
 126                          'custom'  => __( 'Custom', 'twentytwenty' ),
 127                      ),
 128                  )
 129              );
 130  
 131              /**
 132               * Implementation for the accent color.
 133               * This is different to all other color options because of the accessibility enhancements.
 134               * The control is a hue-only colorpicker, and there is a separate setting that holds values
 135               * for other colors calculated based on the selected hue and various background-colors on the page.
 136               *
 137               * @since 1.0.0
 138               */
 139  
 140              // Add the setting for the hue colorpicker.
 141              $wp_customize->add_setting(
 142                  'accent_hue',
 143                  array(
 144                      'default'           => 344,
 145                      'type'              => 'theme_mod',
 146                      'sanitize_callback' => 'absint',
 147                      'transport'         => 'postMessage',
 148                  )
 149              );
 150  
 151              // Add setting to hold colors derived from the accent hue.
 152              $wp_customize->add_setting(
 153                  'accent_accessible_colors',
 154                  array(
 155                      'default'           => array(
 156                          'content'       => array(
 157                              'text'      => '#000000',
 158                              'accent'    => '#cd2653',
 159                              'secondary' => '#6d6d6d',
 160                              'borders'   => '#dcd7ca',
 161                          ),
 162                          'header-footer' => array(
 163                              'text'      => '#000000',
 164                              'accent'    => '#cd2653',
 165                              'secondary' => '#6d6d6d',
 166                              'borders'   => '#dcd7ca',
 167                          ),
 168                      ),
 169                      'type'              => 'theme_mod',
 170                      'transport'         => 'postMessage',
 171                      'sanitize_callback' => array( __CLASS__, 'sanitize_accent_accessible_colors' ),
 172                  )
 173              );
 174  
 175              // Add the hue-only colorpicker for the accent color.
 176              $wp_customize->add_control(
 177                  new WP_Customize_Color_Control(
 178                      $wp_customize,
 179                      'accent_hue',
 180                      array(
 181                          'section'         => 'colors',
 182                          'settings'        => 'accent_hue',
 183                          'description'     => __( 'Apply a custom color for links, buttons, featured images.', 'twentytwenty' ),
 184                          'mode'            => 'hue',
 185                          'active_callback' => function() use ( $wp_customize ) {
 186                              return ( 'custom' === $wp_customize->get_setting( 'accent_hue_active' )->value() );
 187                          },
 188                      )
 189                  )
 190              );
 191  
 192              // Update background color with postMessage, so inline CSS output is updated as well.
 193              $wp_customize->get_setting( 'background_color' )->transport = 'postMessage';
 194  
 195              /**
 196               * Theme Options
 197               */
 198  
 199              $wp_customize->add_section(
 200                  'options',
 201                  array(
 202                      'title'      => __( 'Theme Options', 'twentytwenty' ),
 203                      'priority'   => 40,
 204                      'capability' => 'edit_theme_options',
 205                  )
 206              );
 207  
 208              /* Enable Header Search --------- */
 209  
 210              $wp_customize->add_setting(
 211                  'enable_header_search',
 212                  array(
 213                      'capability'        => 'edit_theme_options',
 214                      'default'           => true,
 215                      'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
 216                  )
 217              );
 218  
 219              $wp_customize->add_control(
 220                  'enable_header_search',
 221                  array(
 222                      'type'     => 'checkbox',
 223                      'section'  => 'options',
 224                      'priority' => 10,
 225                      'label'    => __( 'Show search in header', 'twentytwenty' ),
 226                  )
 227              );
 228  
 229              /* Display full content or excerpts on the blog and archives --------- */
 230  
 231              $wp_customize->add_setting(
 232                  'blog_content',
 233                  array(
 234                      'capability'        => 'edit_theme_options',
 235                      'default'           => 'full',
 236                      'sanitize_callback' => array( __CLASS__, 'sanitize_select' ),
 237                  )
 238              );
 239  
 240              $wp_customize->add_control(
 241                  'blog_content',
 242                  array(
 243                      'type'     => 'radio',
 244                      'section'  => 'options',
 245                      'priority' => 10,
 246                      'label'    => __( 'On archive pages, posts show:', 'twentytwenty' ),
 247                      'choices'  => array(
 248                          'full'    => __( 'Full text', 'twentytwenty' ),
 249                          'summary' => __( 'Summary', 'twentytwenty' ),
 250                      ),
 251                  )
 252              );
 253  
 254              /**
 255               * Template: Cover Template.
 256               */
 257              $wp_customize->add_section(
 258                  'cover_template_options',
 259                  array(
 260                      'title'       => __( 'Cover Template', 'twentytwenty' ),
 261                      'capability'  => 'edit_theme_options',
 262                      'description' => __( 'Settings for the "Cover Template" page template. Add a featured image to use as background.', 'twentytwenty' ),
 263                      'priority'    => 42,
 264                  )
 265              );
 266  
 267              /* Overlay Fixed Background ------ */
 268  
 269              $wp_customize->add_setting(
 270                  'cover_template_fixed_background',
 271                  array(
 272                      'capability'        => 'edit_theme_options',
 273                      'default'           => true,
 274                      'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
 275                      'transport'         => 'postMessage',
 276                  )
 277              );
 278  
 279              $wp_customize->add_control(
 280                  'cover_template_fixed_background',
 281                  array(
 282                      'type'        => 'checkbox',
 283                      'section'     => 'cover_template_options',
 284                      'label'       => __( 'Fixed Background Image', 'twentytwenty' ),
 285                      'description' => __( 'Creates a parallax effect when the visitor scrolls.', 'twentytwenty' ),
 286                  )
 287              );
 288  
 289              $wp_customize->selective_refresh->add_partial(
 290                  'cover_template_fixed_background',
 291                  array(
 292                      'selector' => '.cover-header',
 293                      'type'     => 'cover_fixed',
 294                  )
 295              );
 296  
 297              /* Separator --------------------- */
 298  
 299              $wp_customize->add_setting(
 300                  'cover_template_separator_1',
 301                  array(
 302                      'sanitize_callback' => 'wp_filter_nohtml_kses',
 303                  )
 304              );
 305  
 306              $wp_customize->add_control(
 307                  new TwentyTwenty_Separator_Control(
 308                      $wp_customize,
 309                      'cover_template_separator_1',
 310                      array(
 311                          'section' => 'cover_template_options',
 312                      )
 313                  )
 314              );
 315  
 316              /* Overlay Background Color ------ */
 317  
 318              $wp_customize->add_setting(
 319                  'cover_template_overlay_background_color',
 320                  array(
 321                      'default'           => twentytwenty_get_color_for_area( 'content', 'accent' ),
 322                      'sanitize_callback' => 'sanitize_hex_color',
 323                  )
 324              );
 325  
 326              $wp_customize->add_control(
 327                  new WP_Customize_Color_Control(
 328                      $wp_customize,
 329                      'cover_template_overlay_background_color',
 330                      array(
 331                          'label'       => __( 'Overlay Background Color', 'twentytwenty' ),
 332                          'description' => __( 'The color used for the overlay. Defaults to the accent color.', 'twentytwenty' ),
 333                          'section'     => 'cover_template_options',
 334                      )
 335                  )
 336              );
 337  
 338              /* Overlay Text Color ------------ */
 339  
 340              $wp_customize->add_setting(
 341                  'cover_template_overlay_text_color',
 342                  array(
 343                      'default'           => '#ffffff',
 344                      'sanitize_callback' => 'sanitize_hex_color',
 345                  )
 346              );
 347  
 348              $wp_customize->add_control(
 349                  new WP_Customize_Color_Control(
 350                      $wp_customize,
 351                      'cover_template_overlay_text_color',
 352                      array(
 353                          'label'       => __( 'Overlay Text Color', 'twentytwenty' ),
 354                          'description' => __( 'The color used for the text in the overlay.', 'twentytwenty' ),
 355                          'section'     => 'cover_template_options',
 356                      )
 357                  )
 358              );
 359  
 360              /* Overlay Color Opacity --------- */
 361  
 362              $wp_customize->add_setting(
 363                  'cover_template_overlay_opacity',
 364                  array(
 365                      'default'           => 80,
 366                      'sanitize_callback' => 'absint',
 367                      'transport'         => 'postMessage',
 368                  )
 369              );
 370  
 371              $wp_customize->add_control(
 372                  'cover_template_overlay_opacity',
 373                  array(
 374                      'label'       => __( 'Overlay Opacity', 'twentytwenty' ),
 375                      'description' => __( 'Make sure that the contrast is high enough so that the text is readable.', 'twentytwenty' ),
 376                      'section'     => 'cover_template_options',
 377                      'type'        => 'range',
 378                      'input_attrs' => twentytwenty_customize_opacity_range(),
 379                  )
 380              );
 381  
 382              $wp_customize->selective_refresh->add_partial(
 383                  'cover_template_overlay_opacity',
 384                  array(
 385                      'selector' => '.cover-color-overlay',
 386                      'type'     => 'cover_opacity',
 387                  )
 388              );
 389          }
 390  
 391          /**
 392           * Sanitization callback for the "accent_accessible_colors" setting.
 393           *
 394           * @static
 395           * @access public
 396           * @since 1.0.0
 397           * @param array $value The value we want to sanitize.
 398           * @return array       Returns sanitized value. Each item in the array gets sanitized separately.
 399           */
 400  		public static function sanitize_accent_accessible_colors( $value ) {
 401  
 402              // Make sure the value is an array. Do not typecast, use empty array as fallback.
 403              $value = is_array( $value ) ? $value : array();
 404  
 405              // Loop values.
 406              foreach ( $value as $area => $values ) {
 407                  foreach ( $values as $context => $color_val ) {
 408                      $value[ $area ][ $context ] = sanitize_hex_color( $color_val );
 409                  }
 410              }
 411  
 412              return $value;
 413          }
 414  
 415          /**
 416           * Sanitize select.
 417           *
 418           * @param string $input The input from the setting.
 419           * @param object $setting The selected setting.
 420           *
 421           * @return string $input|$setting->default The input from the setting or the default setting.
 422           */
 423  		public static function sanitize_select( $input, $setting ) {
 424              $input   = sanitize_key( $input );
 425              $choices = $setting->manager->get_control( $setting->id )->choices;
 426              return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
 427          }
 428  
 429          /**
 430           * Sanitize boolean for checkbox.
 431           *
 432           * @param bool $checked Whether or not a box is checked.
 433           *
 434           * @return bool
 435           */
 436  		public static function sanitize_checkbox( $checked ) {
 437              return ( ( isset( $checked ) && true === $checked ) ? true : false );
 438          }
 439  
 440      }
 441  
 442      // Setup the Theme Customizer settings and controls.
 443      add_action( 'customize_register', array( 'TwentyTwenty_Customize', 'register' ) );
 444  
 445  }
 446  
 447  /**
 448   * PARTIAL REFRESH FUNCTIONS
 449   * */
 450  if ( ! function_exists( 'twentytwenty_customize_partial_blogname' ) ) {
 451      /**
 452       * Render the site title for the selective refresh partial.
 453       */
 454  	function twentytwenty_customize_partial_blogname() {
 455          bloginfo( 'name' );
 456      }
 457  }
 458  
 459  if ( ! function_exists( 'twentytwenty_customize_partial_blogdescription' ) ) {
 460      /**
 461       * Render the site description for the selective refresh partial.
 462       */
 463      function twentytwenty_customize_partial_blogdescription() {
 464          bloginfo( 'description' );
 465      }
 466  }
 467  
 468  if ( ! function_exists( 'twentytwenty_customize_partial_site_logo' ) ) {
 469      /**
 470       * Render the site logo for the selective refresh partial.
 471       *
 472       * Doing it this way so we don't have issues with `render_callback`'s arguments.
 473       */
 474  	function twentytwenty_customize_partial_site_logo() {
 475          twentytwenty_site_logo();
 476      }
 477  }
 478  
 479  
 480  /**
 481   * Input attributes for cover overlay opacity option.
 482   *
 483   * @return array Array containing attribute names and their values.
 484   */
 485  function twentytwenty_customize_opacity_range() {
 486      /**
 487       * Filter the input attributes for opacity
 488       *
 489       * @param array $attrs {
 490       *     The attributes
 491       *
 492       *     @type int $min Minimum value
 493       *     @type int $max Maximum value
 494       *     @type int $step Interval between numbers
 495       * }
 496       */
 497      return apply_filters(
 498          'twentytwenty_customize_opacity_range',
 499          array(
 500              'min'  => 0,
 501              'max'  => 90,
 502              'step' => 5,
 503          )
 504      );
 505  }


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7