[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentytwenty/ -> functions.php (source)

   1  <?php
   2  /**
   3   * Twenty Twenty functions and definitions
   4   *
   5   * @link https://developer.wordpress.org/themes/basics/theme-functions/
   6   *
   7   * @package WordPress
   8   * @subpackage Twenty_Twenty
   9   * @since Twenty Twenty 1.0
  10   */
  11  
  12  /**
  13   * Table of Contents:
  14   * Theme Support
  15   * Required Files
  16   * Register Styles
  17   * Register Scripts
  18   * Register Menus
  19   * Custom Logo
  20   * WP Body Open
  21   * Register Sidebars
  22   * Enqueue Block Editor Assets
  23   * Enqueue Classic Editor Styles
  24   * Block Editor Settings
  25   */
  26  
  27  /**
  28   * Sets up theme defaults and registers support for various WordPress features.
  29   *
  30   * Note that this function is hooked into the after_setup_theme hook, which
  31   * runs before the init hook. The init hook is too late for some features, such
  32   * as indicating support for post thumbnails.
  33   *
  34   * @since Twenty Twenty 1.0
  35   */
  36  function twentytwenty_theme_support() {
  37  
  38      // Add default posts and comments RSS feed links to head.
  39      add_theme_support( 'automatic-feed-links' );
  40  
  41      // Custom background color.
  42      add_theme_support(
  43          'custom-background',
  44          array(
  45              'default-color' => 'f5efe0',
  46          )
  47      );
  48  
  49      // Set content-width.
  50      global $content_width;
  51      if ( ! isset( $content_width ) ) {
  52          $content_width = 580;
  53      }
  54  
  55      /*
  56       * Enable support for Post Thumbnails on posts and pages.
  57       *
  58       * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  59       */
  60      add_theme_support( 'post-thumbnails' );
  61  
  62      // Set post thumbnail size.
  63      set_post_thumbnail_size( 1200, 9999 );
  64  
  65      // Add custom image size used in Cover Template.
  66      add_image_size( 'twentytwenty-fullscreen', 1980, 9999 );
  67  
  68      // Custom logo.
  69      $logo_width  = 120;
  70      $logo_height = 90;
  71  
  72      // If the retina setting is active, double the recommended width and height.
  73      if ( get_theme_mod( 'retina_logo', false ) ) {
  74          $logo_width  = floor( $logo_width * 2 );
  75          $logo_height = floor( $logo_height * 2 );
  76      }
  77  
  78      add_theme_support(
  79          'custom-logo',
  80          array(
  81              'height'      => $logo_height,
  82              'width'       => $logo_width,
  83              'flex-height' => true,
  84              'flex-width'  => true,
  85          )
  86      );
  87  
  88      /*
  89       * Let WordPress manage the document title.
  90       * By adding theme support, we declare that this theme does not use a
  91       * hard-coded <title> tag in the document head, and expect WordPress to
  92       * provide it for us.
  93       */
  94      add_theme_support( 'title-tag' );
  95  
  96      /*
  97       * Switch default core markup for search form, comment form, and comments
  98       * to output valid HTML5.
  99       */
 100      add_theme_support(
 101          'html5',
 102          array(
 103              'search-form',
 104              'comment-form',
 105              'comment-list',
 106              'gallery',
 107              'caption',
 108              'script',
 109              'style',
 110              'navigation-widgets',
 111          )
 112      );
 113  
 114      // Add support for full and wide align images.
 115      add_theme_support( 'align-wide' );
 116  
 117      // Add support for responsive embeds.
 118      add_theme_support( 'responsive-embeds' );
 119  
 120      /*
 121       * Adds starter content to highlight the theme on fresh sites.
 122       * This is done conditionally to avoid loading the starter content on every
 123       * page load, as it is a one-off operation only needed once in the customizer.
 124       */
 125      if ( is_customize_preview() ) {
 126          require get_template_directory() . '/inc/starter-content.php';
 127          add_theme_support( 'starter-content', twentytwenty_get_starter_content() );
 128      }
 129  
 130      // Add theme support for selective refresh for widgets.
 131      add_theme_support( 'customize-selective-refresh-widgets' );
 132  
 133      /*
 134       * Adds `async` and `defer` support for scripts registered or enqueued
 135       * by the theme.
 136       */
 137      $loader = new TwentyTwenty_Script_Loader();
 138      if ( version_compare( $GLOBALS['wp_version'], '6.3', '<' ) ) {
 139          add_filter( 'script_loader_tag', array( $loader, 'filter_script_loader_tag' ), 10, 2 );
 140      } else {
 141          add_filter( 'print_scripts_array', array( $loader, 'migrate_legacy_strategy_script_data' ), 100 );
 142      }
 143  }
 144  
 145  add_action( 'after_setup_theme', 'twentytwenty_theme_support' );
 146  
 147  /**
 148   * REQUIRED FILES
 149   * Include required files.
 150   */
 151  require get_template_directory() . '/inc/template-tags.php';
 152  
 153  // Handle SVG icons.
 154  require get_template_directory() . '/classes/class-twentytwenty-svg-icons.php';
 155  require get_template_directory() . '/inc/svg-icons.php';
 156  
 157  // Handle Customizer settings.
 158  require get_template_directory() . '/classes/class-twentytwenty-customize.php';
 159  
 160  // Require Separator Control class.
 161  require get_template_directory() . '/classes/class-twentytwenty-separator-control.php';
 162  
 163  // Custom comment walker.
 164  require get_template_directory() . '/classes/class-twentytwenty-walker-comment.php';
 165  
 166  // Custom page walker.
 167  require get_template_directory() . '/classes/class-twentytwenty-walker-page.php';
 168  
 169  // Custom script loader class.
 170  require get_template_directory() . '/classes/class-twentytwenty-script-loader.php';
 171  
 172  // Non-latin language handling.
 173  require get_template_directory() . '/classes/class-twentytwenty-non-latin-languages.php';
 174  
 175  // Custom CSS.
 176  require get_template_directory() . '/inc/custom-css.php';
 177  
 178  // Block Patterns.
 179  require get_template_directory() . '/inc/block-patterns.php';
 180  
 181  /**
 182   * Register and Enqueue Styles.
 183   *
 184   * @since Twenty Twenty 1.0
 185   * @since Twenty Twenty 2.6 Enqueue the CSS file for the variable font.
 186   */
 187  function twentytwenty_register_styles() {
 188  
 189      $theme_version = wp_get_theme()->get( 'Version' );
 190  
 191      wp_enqueue_style( 'twentytwenty-style', get_stylesheet_uri(), array(), $theme_version );
 192      wp_style_add_data( 'twentytwenty-style', 'rtl', 'replace' );
 193  
 194      // Enqueue the CSS file for the variable font, Inter.
 195      wp_enqueue_style( 'twentytwenty-fonts', get_theme_file_uri( '/assets/css/font-inter.css' ), array(), $theme_version, 'all' );
 196  
 197      // Add output of Customizer settings as inline style.
 198      $customizer_css = twentytwenty_get_customizer_css( 'front-end' );
 199      if ( $customizer_css ) {
 200          wp_add_inline_style( 'twentytwenty-style', $customizer_css );
 201      }
 202  
 203      // Add print CSS.
 204      wp_enqueue_style( 'twentytwenty-print-style', get_template_directory_uri() . '/print.css', null, $theme_version, 'print' );
 205  }
 206  
 207  add_action( 'wp_enqueue_scripts', 'twentytwenty_register_styles' );
 208  
 209  /**
 210   * Register and Enqueue Scripts.
 211   *
 212   * @since Twenty Twenty 1.0
 213   */
 214  function twentytwenty_register_scripts() {
 215  
 216      $theme_version = wp_get_theme()->get( 'Version' );
 217  
 218      if ( ( ! is_admin() ) && is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
 219          wp_enqueue_script( 'comment-reply' );
 220      }
 221  
 222      /*
 223       * This script is intentionally printed in the head because it involves the page header. The `defer` script loading
 224       * strategy ensures that it does not block rendering; being in the head it will start loading earlier so that it
 225       * will execute sooner once the DOM has loaded. The $args array is not used here to avoid unintentional footer
 226       * placement in WP<6.3; the wp_script_add_data() call is used instead.
 227       */
 228      wp_enqueue_script( 'twentytwenty-js', get_template_directory_uri() . '/assets/js/index.js', array(), $theme_version );
 229      wp_script_add_data( 'twentytwenty-js', 'strategy', 'defer' );
 230  }
 231  
 232  add_action( 'wp_enqueue_scripts', 'twentytwenty_register_scripts' );
 233  
 234  /**
 235   * Fix skip link focus in IE11.
 236   *
 237   * This does not enqueue the script because it is tiny and because it is only for IE11,
 238   * thus it does not warrant having an entire dedicated blocking script being loaded.
 239   *
 240   * @since Twenty Twenty 1.0
 241   * @deprecated Twenty Twenty 2.3 Removed from wp_print_footer_scripts action.
 242   *
 243   * @link https://git.io/vWdr2
 244   */
 245  function twentytwenty_skip_link_focus_fix() {
 246      // The following is minified via `terser --compress --mangle -- assets/js/skip-link-focus-fix.js`.
 247      ?>
 248      <script>
 249      /(trident|msie)/i.test(navigator.userAgent)&&document.getElementById&&window.addEventListener&&window.addEventListener("hashchange",function(){var t,e=location.hash.substring(1);/^[A-z0-9_-]+$/.test(e)&&(t=document.getElementById(e))&&(/^(?:a|select|input|button|textarea)$/i.test(t.tagName)||(t.tabIndex=-1),t.focus())},!1);
 250      </script>
 251      <?php
 252  }
 253  
 254  /**
 255   * Enqueue non-latin language styles.
 256   *
 257   * @since Twenty Twenty 1.0
 258   *
 259   * @return void
 260   */
 261  function twentytwenty_non_latin_languages() {
 262      $custom_css = TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'front-end' );
 263  
 264      if ( $custom_css ) {
 265          wp_add_inline_style( 'twentytwenty-style', $custom_css );
 266      }
 267  }
 268  
 269  add_action( 'wp_enqueue_scripts', 'twentytwenty_non_latin_languages' );
 270  
 271  /**
 272   * Register navigation menus uses wp_nav_menu in five places.
 273   *
 274   * @since Twenty Twenty 1.0
 275   */
 276  function twentytwenty_menus() {
 277  
 278      $locations = array(
 279          'primary'  => __( 'Desktop Horizontal Menu', 'twentytwenty' ),
 280          'expanded' => __( 'Desktop Expanded Menu', 'twentytwenty' ),
 281          'mobile'   => __( 'Mobile Menu', 'twentytwenty' ),
 282          'footer'   => __( 'Footer Menu', 'twentytwenty' ),
 283          'social'   => __( 'Social Menu', 'twentytwenty' ),
 284      );
 285  
 286      register_nav_menus( $locations );
 287  }
 288  
 289  add_action( 'init', 'twentytwenty_menus' );
 290  
 291  /**
 292   * Get the information about the logo.
 293   *
 294   * @since Twenty Twenty 1.0
 295   *
 296   * @param string $html The HTML output from get_custom_logo (core function).
 297   * @return string
 298   */
 299  function twentytwenty_get_custom_logo( $html ) {
 300  
 301      $logo_id = get_theme_mod( 'custom_logo' );
 302  
 303      if ( ! $logo_id ) {
 304          return $html;
 305      }
 306  
 307      $logo = wp_get_attachment_image_src( $logo_id, 'full' );
 308  
 309      if ( $logo ) {
 310          // For clarity.
 311          $logo_width  = esc_attr( $logo[1] );
 312          $logo_height = esc_attr( $logo[2] );
 313  
 314          // If the retina logo setting is active, reduce the width/height by half.
 315          if ( get_theme_mod( 'retina_logo', false ) ) {
 316              $logo_width  = floor( $logo_width / 2 );
 317              $logo_height = floor( $logo_height / 2 );
 318  
 319              $search = array(
 320                  '/width=\"\d+\"/iU',
 321                  '/height=\"\d+\"/iU',
 322              );
 323  
 324              $replace = array(
 325                  "width=\"{$logo_width}\"",
 326                  "height=\"{$logo_height}\"",
 327              );
 328  
 329              // Add a style attribute with the height, or append the height to the style attribute if the style attribute already exists.
 330              if ( false === strpos( $html, ' style=' ) ) {
 331                  $search[]  = '/(src=)/';
 332                  $replace[] = "style=\"height: {$logo_height}px;\" src=";
 333              } else {
 334                  $search[]  = '/(style="[^"]*)/';
 335                  $replace[] = "$1 height: {$logo_height}px;";
 336              }
 337  
 338              $html = preg_replace( $search, $replace, $html );
 339  
 340          }
 341      }
 342  
 343      return $html;
 344  }
 345  
 346  add_filter( 'get_custom_logo', 'twentytwenty_get_custom_logo' );
 347  
 348  if ( ! function_exists( 'wp_body_open' ) ) {
 349  
 350      /**
 351       * Shim for wp_body_open, ensuring backward compatibility with versions of WordPress older than 5.2.
 352       *
 353       * @since Twenty Twenty 1.0
 354       */
 355  	function wp_body_open() {
 356          /** This action is documented in wp-includes/general-template.php */
 357          do_action( 'wp_body_open' );
 358      }
 359  }
 360  
 361  /**
 362   * Include a skip to content link at the top of the page so that users can bypass the menu.
 363   *
 364   * @since Twenty Twenty 1.0
 365   */
 366  function twentytwenty_skip_link() {
 367      echo '<a class="skip-link screen-reader-text" href="#site-content">' .
 368          /* translators: Hidden accessibility text. */
 369          __( 'Skip to the content', 'twentytwenty' ) .
 370      '</a>';
 371  }
 372  
 373  add_action( 'wp_body_open', 'twentytwenty_skip_link', 5 );
 374  
 375  /**
 376   * Register widget areas.
 377   *
 378   * @since Twenty Twenty 1.0
 379   *
 380   * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 381   */
 382  function twentytwenty_sidebar_registration() {
 383  
 384      // Arguments used in all register_sidebar() calls.
 385      $shared_args = array(
 386          'before_title'  => '<h2 class="widget-title subheading heading-size-3">',
 387          'after_title'   => '</h2>',
 388          'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
 389          'after_widget'  => '</div></div>',
 390      );
 391  
 392      // Footer #1.
 393      register_sidebar(
 394          array_merge(
 395              $shared_args,
 396              array(
 397                  'name'        => __( 'Footer #1', 'twentytwenty' ),
 398                  'id'          => 'sidebar-1',
 399                  'description' => __( 'Widgets in this area will be displayed in the first column in the footer.', 'twentytwenty' ),
 400              )
 401          )
 402      );
 403  
 404      // Footer #2.
 405      register_sidebar(
 406          array_merge(
 407              $shared_args,
 408              array(
 409                  'name'        => __( 'Footer #2', 'twentytwenty' ),
 410                  'id'          => 'sidebar-2',
 411                  'description' => __( 'Widgets in this area will be displayed in the second column in the footer.', 'twentytwenty' ),
 412              )
 413          )
 414      );
 415  }
 416  
 417  add_action( 'widgets_init', 'twentytwenty_sidebar_registration' );
 418  
 419  /**
 420   * Enqueue supplemental block editor styles.
 421   *
 422   * @since Twenty Twenty 1.0
 423   * @since Twenty Twenty 2.4 Removed a script related to the obsolete Squared style of Button blocks.
 424   * @since Twenty Twenty 2.6 Enqueue the CSS file for the variable font.
 425   */
 426  function twentytwenty_block_editor_styles() {
 427  
 428      $theme_version = wp_get_theme()->get( 'Version' );
 429  
 430      // Enqueue the editor styles.
 431      wp_enqueue_style( 'twentytwenty-block-editor-styles', get_theme_file_uri( '/assets/css/editor-style-block.css' ), array(), $theme_version, 'all' );
 432      wp_style_add_data( 'twentytwenty-block-editor-styles', 'rtl', 'replace' );
 433  
 434      // Add inline style from the Customizer.
 435      $customizer_css = twentytwenty_get_customizer_css( 'block-editor' );
 436      if ( $customizer_css ) {
 437          wp_add_inline_style( 'twentytwenty-block-editor-styles', $customizer_css );
 438      }
 439  
 440      // Enqueue the CSS file for the variable font, Inter.
 441      wp_enqueue_style( 'twentytwenty-fonts', get_theme_file_uri( '/assets/css/font-inter.css' ), array(), $theme_version, 'all' );
 442  
 443      // Add inline style for non-latin fonts.
 444      $custom_css = TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'block-editor' );
 445      if ( $custom_css ) {
 446          wp_add_inline_style( 'twentytwenty-block-editor-styles', $custom_css );
 447      }
 448  }
 449  
 450  if ( is_admin() && version_compare( $GLOBALS['wp_version'], '6.3', '>=' ) ) {
 451      add_action( 'enqueue_block_assets', 'twentytwenty_block_editor_styles', 1, 1 );
 452  } else {
 453      add_action( 'enqueue_block_editor_assets', 'twentytwenty_block_editor_styles', 1, 1 );
 454  }
 455  
 456  /**
 457   * Enqueue classic editor styles.
 458   *
 459   * @since Twenty Twenty 1.0
 460   * @since Twenty Twenty 2.6 Enqueue the CSS file for the variable font.
 461   */
 462  function twentytwenty_classic_editor_styles() {
 463  
 464      $classic_editor_styles = array(
 465          '/assets/css/editor-style-classic.css',
 466          '/assets/css/font-inter.css',
 467      );
 468  
 469      add_editor_style( $classic_editor_styles );
 470  }
 471  
 472  add_action( 'init', 'twentytwenty_classic_editor_styles' );
 473  
 474  /**
 475   * Output Customizer settings in the classic editor.
 476   * Adds styles to the head of the TinyMCE iframe. Kudos to @Otto42 for the original solution.
 477   *
 478   * @since Twenty Twenty 1.0
 479   *
 480   * @param array $mce_init TinyMCE styles.
 481   * @return array TinyMCE styles.
 482   */
 483  function twentytwenty_add_classic_editor_customizer_styles( $mce_init ) {
 484  
 485      $styles = twentytwenty_get_customizer_css( 'classic-editor' );
 486  
 487      if ( ! $styles ) {
 488          return $mce_init;
 489      }
 490  
 491      if ( ! isset( $mce_init['content_style'] ) ) {
 492          $mce_init['content_style'] = $styles . ' ';
 493      } else {
 494          $mce_init['content_style'] .= ' ' . $styles . ' ';
 495      }
 496  
 497      return $mce_init;
 498  }
 499  
 500  add_filter( 'tiny_mce_before_init', 'twentytwenty_add_classic_editor_customizer_styles' );
 501  
 502  /**
 503   * Output non-latin font styles in the classic editor.
 504   * Adds styles to the head of the TinyMCE iframe. Kudos to @Otto42 for the original solution.
 505   *
 506   * @param array $mce_init TinyMCE styles.
 507   * @return array TinyMCE styles.
 508   */
 509  function twentytwenty_add_classic_editor_non_latin_styles( $mce_init ) {
 510  
 511      $styles = TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'classic-editor' );
 512  
 513      // Return if there are no styles to add.
 514      if ( ! $styles ) {
 515          return $mce_init;
 516      }
 517  
 518      if ( ! isset( $mce_init['content_style'] ) ) {
 519          $mce_init['content_style'] = $styles . ' ';
 520      } else {
 521          $mce_init['content_style'] .= ' ' . $styles . ' ';
 522      }
 523  
 524      return $mce_init;
 525  }
 526  
 527  add_filter( 'tiny_mce_before_init', 'twentytwenty_add_classic_editor_non_latin_styles' );
 528  
 529  /**
 530   * Block Editor Settings.
 531   * Add custom colors and font sizes to the block editor.
 532   *
 533   * @since Twenty Twenty 1.0
 534   */
 535  function twentytwenty_block_editor_settings() {
 536  
 537      // Block Editor Palette.
 538      $editor_color_palette = array(
 539          array(
 540              'name'  => __( 'Accent Color', 'twentytwenty' ),
 541              'slug'  => 'accent',
 542              'color' => twentytwenty_get_color_for_area( 'content', 'accent' ),
 543          ),
 544          array(
 545              'name'  => _x( 'Primary', 'color', 'twentytwenty' ),
 546              'slug'  => 'primary',
 547              'color' => twentytwenty_get_color_for_area( 'content', 'text' ),
 548          ),
 549          array(
 550              'name'  => _x( 'Secondary', 'color', 'twentytwenty' ),
 551              'slug'  => 'secondary',
 552              'color' => twentytwenty_get_color_for_area( 'content', 'secondary' ),
 553          ),
 554          array(
 555              'name'  => __( 'Subtle Background', 'twentytwenty' ),
 556              'slug'  => 'subtle-background',
 557              'color' => twentytwenty_get_color_for_area( 'content', 'borders' ),
 558          ),
 559      );
 560  
 561      // Add the background option.
 562      $background_color = get_theme_mod( 'background_color' );
 563      if ( ! $background_color ) {
 564          $background_color_arr = get_theme_support( 'custom-background' );
 565          $background_color     = $background_color_arr[0]['default-color'];
 566      }
 567      $editor_color_palette[] = array(
 568          'name'  => __( 'Background Color', 'twentytwenty' ),
 569          'slug'  => 'background',
 570          'color' => '#' . $background_color,
 571      );
 572  
 573      // If we have accent colors, add them to the block editor palette.
 574      if ( $editor_color_palette ) {
 575          add_theme_support( 'editor-color-palette', $editor_color_palette );
 576      }
 577  
 578      // Block Editor Font Sizes.
 579      add_theme_support(
 580          'editor-font-sizes',
 581          array(
 582              array(
 583                  'name'      => _x( 'Small', 'Name of the small font size in the block editor', 'twentytwenty' ),
 584                  'shortName' => _x( 'S', 'Short name of the small font size in the block editor.', 'twentytwenty' ),
 585                  'size'      => 18,
 586                  'slug'      => 'small',
 587              ),
 588              array(
 589                  'name'      => _x( 'Regular', 'Name of the regular font size in the block editor', 'twentytwenty' ),
 590                  'shortName' => _x( 'M', 'Short name of the regular font size in the block editor.', 'twentytwenty' ),
 591                  'size'      => 21,
 592                  'slug'      => 'normal',
 593              ),
 594              array(
 595                  'name'      => _x( 'Large', 'Name of the large font size in the block editor', 'twentytwenty' ),
 596                  'shortName' => _x( 'L', 'Short name of the large font size in the block editor.', 'twentytwenty' ),
 597                  'size'      => 26.25,
 598                  'slug'      => 'large',
 599              ),
 600              array(
 601                  'name'      => _x( 'Larger', 'Name of the larger font size in the block editor', 'twentytwenty' ),
 602                  'shortName' => _x( 'XL', 'Short name of the larger font size in the block editor.', 'twentytwenty' ),
 603                  'size'      => 32,
 604                  'slug'      => 'larger',
 605              ),
 606          )
 607      );
 608  
 609      add_theme_support( 'editor-styles' );
 610  
 611      // If we have a dark background color then add support for dark editor style.
 612      // We can determine if the background color is dark by checking if the text-color is white.
 613      if ( '#ffffff' === strtolower( twentytwenty_get_color_for_area( 'content', 'text' ) ) ) {
 614          add_theme_support( 'dark-editor-style' );
 615      }
 616  }
 617  
 618  add_action( 'after_setup_theme', 'twentytwenty_block_editor_settings' );
 619  
 620  /**
 621   * Overwrite default more tag with styling and screen reader markup.
 622   *
 623   * @param string $html The default output HTML for the more tag.
 624   * @return string
 625   */
 626  function twentytwenty_read_more_tag( $html ) {
 627      return preg_replace( '/<a(.*)>(.*)<\/a>/iU', sprintf( '<div class="read-more-button-wrap"><a$1><span class="faux-button">$2</span> <span class="screen-reader-text">"%1$s"</span></a></div>', get_the_title( get_the_ID() ) ), $html );
 628  }
 629  
 630  add_filter( 'the_content_more_link', 'twentytwenty_read_more_tag' );
 631  
 632  /**
 633   * Enqueues scripts for customizer controls & settings.
 634   *
 635   * @since Twenty Twenty 1.0
 636   *
 637   * @return void
 638   */
 639  function twentytwenty_customize_controls_enqueue_scripts() {
 640      $theme_version = wp_get_theme()->get( 'Version' );
 641  
 642      // Add main customizer js file.
 643      wp_enqueue_script( 'twentytwenty-customize', get_template_directory_uri() . '/assets/js/customize.js', array( 'jquery' ), $theme_version );
 644  
 645      // Add script for color calculations.
 646      wp_enqueue_script( 'twentytwenty-color-calculations', get_template_directory_uri() . '/assets/js/color-calculations.js', array( 'wp-color-picker' ), $theme_version );
 647  
 648      // Add script for controls.
 649      wp_enqueue_script( 'twentytwenty-customize-controls', get_template_directory_uri() . '/assets/js/customize-controls.js', array( 'twentytwenty-color-calculations', 'customize-controls', 'underscore', 'jquery' ), $theme_version );
 650      wp_localize_script( 'twentytwenty-customize-controls', 'twentyTwentyBgColors', twentytwenty_get_customizer_color_vars() );
 651  }
 652  
 653  add_action( 'customize_controls_enqueue_scripts', 'twentytwenty_customize_controls_enqueue_scripts' );
 654  
 655  /**
 656   * Enqueue scripts for the customizer preview.
 657   *
 658   * @since Twenty Twenty 1.0
 659   *
 660   * @return void
 661   */
 662  function twentytwenty_customize_preview_init() {
 663      $theme_version = wp_get_theme()->get( 'Version' );
 664  
 665      wp_enqueue_script( 'twentytwenty-customize-preview', get_theme_file_uri( '/assets/js/customize-preview.js' ), array( 'customize-preview', 'customize-selective-refresh', 'jquery' ), $theme_version, array( 'in_footer' => true ) );
 666      wp_localize_script( 'twentytwenty-customize-preview', 'twentyTwentyBgColors', twentytwenty_get_customizer_color_vars() );
 667      wp_localize_script( 'twentytwenty-customize-preview', 'twentyTwentyPreviewEls', twentytwenty_get_elements_array() );
 668  
 669      wp_add_inline_script(
 670          'twentytwenty-customize-preview',
 671          sprintf(
 672              'wp.customize.selectiveRefresh.partialConstructor[ %1$s ].prototype.attrs = %2$s;',
 673              wp_json_encode( 'cover_opacity' ),
 674              wp_json_encode( twentytwenty_customize_opacity_range() )
 675          )
 676      );
 677  }
 678  
 679  add_action( 'customize_preview_init', 'twentytwenty_customize_preview_init' );
 680  
 681  /**
 682   * Get accessible color for an area.
 683   *
 684   * @since Twenty Twenty 1.0
 685   *
 686   * @param string $area    The area we want to get the colors for.
 687   * @param string $context Can be 'text' or 'accent'.
 688   * @return string Returns a HEX color.
 689   */
 690  function twentytwenty_get_color_for_area( $area = 'content', $context = 'text' ) {
 691  
 692      // Get the value from the theme-mod.
 693      $settings = get_theme_mod(
 694          'accent_accessible_colors',
 695          array(
 696              'content'       => array(
 697                  'text'      => '#000000',
 698                  'accent'    => '#cd2653',
 699                  'secondary' => '#6d6d6d',
 700                  'borders'   => '#dcd7ca',
 701              ),
 702              'header-footer' => array(
 703                  'text'      => '#000000',
 704                  'accent'    => '#cd2653',
 705                  'secondary' => '#6d6d6d',
 706                  'borders'   => '#dcd7ca',
 707              ),
 708          )
 709      );
 710  
 711      // If we have a value return it.
 712      if ( isset( $settings[ $area ] ) && isset( $settings[ $area ][ $context ] ) ) {
 713          return $settings[ $area ][ $context ];
 714      }
 715  
 716      // Return false if the option doesn't exist.
 717      return false;
 718  }
 719  
 720  /**
 721   * Returns an array of variables for the customizer preview.
 722   *
 723   * @since Twenty Twenty 1.0
 724   *
 725   * @return array
 726   */
 727  function twentytwenty_get_customizer_color_vars() {
 728      $colors = array(
 729          'content'       => array(
 730              'setting' => 'background_color',
 731          ),
 732          'header-footer' => array(
 733              'setting' => 'header_footer_background_color',
 734          ),
 735      );
 736      return $colors;
 737  }
 738  
 739  /**
 740   * Get an array of elements.
 741   *
 742   * @since Twenty Twenty 1.0
 743   *
 744   * @return array
 745   */
 746  function twentytwenty_get_elements_array() {
 747  
 748      // The array is formatted like this:
 749      // [key-in-saved-setting][sub-key-in-setting][css-property] = [elements].
 750      $elements = array(
 751          'content'       => array(
 752              'accent'     => array(
 753                  'color'            => array( '.color-accent', '.color-accent-hover:hover', '.color-accent-hover:focus', ':root .has-accent-color', '.has-drop-cap:not(:focus):first-letter', '.wp-block-button.is-style-outline', 'a' ),
 754                  'border-color'     => array( 'blockquote', '.border-color-accent', '.border-color-accent-hover:hover', '.border-color-accent-hover:focus' ),
 755                  'background-color' => array( 'button', '.button', '.faux-button', '.wp-block-button__link', '.wp-block-file .wp-block-file__button', 'input[type="button"]', 'input[type="reset"]', 'input[type="submit"]', '.bg-accent', '.bg-accent-hover:hover', '.bg-accent-hover:focus', ':root .has-accent-background-color', '.comment-reply-link' ),
 756                  'fill'             => array( '.fill-children-accent', '.fill-children-accent *' ),
 757              ),
 758              'background' => array(
 759                  'color'            => array( ':root .has-background-color', 'button', '.button', '.faux-button', '.wp-block-button__link', '.wp-block-file__button', 'input[type="button"]', 'input[type="reset"]', 'input[type="submit"]', '.wp-block-button', '.comment-reply-link', '.has-background.has-primary-background-color:not(.has-text-color)', '.has-background.has-primary-background-color *:not(.has-text-color)', '.has-background.has-accent-background-color:not(.has-text-color)', '.has-background.has-accent-background-color *:not(.has-text-color)' ),
 760                  'background-color' => array( ':root .has-background-background-color' ),
 761              ),
 762              'text'       => array(
 763                  'color'            => array( 'body', '.entry-title a', ':root .has-primary-color' ),
 764                  'background-color' => array( ':root .has-primary-background-color' ),
 765              ),
 766              'secondary'  => array(
 767                  'color'            => array( 'cite', 'figcaption', '.wp-caption-text', '.post-meta', '.entry-content .wp-block-archives li', '.entry-content .wp-block-categories li', '.entry-content .wp-block-latest-posts li', '.wp-block-latest-comments__comment-date', '.wp-block-latest-posts__post-date', '.wp-block-embed figcaption', '.wp-block-image figcaption', '.wp-block-pullquote cite', '.comment-metadata', '.comment-respond .comment-notes', '.comment-respond .logged-in-as', '.pagination .dots', '.entry-content hr:not(.has-background)', 'hr.styled-separator', ':root .has-secondary-color' ),
 768                  'background-color' => array( ':root .has-secondary-background-color' ),
 769              ),
 770              'borders'    => array(
 771                  'border-color'        => array( 'pre', 'fieldset', 'input', 'textarea', 'table', 'table *', 'hr' ),
 772                  'background-color'    => array( 'caption', 'code', 'code', 'kbd', 'samp', '.wp-block-table.is-style-stripes tbody tr:nth-child(odd)', ':root .has-subtle-background-background-color' ),
 773                  'border-bottom-color' => array( '.wp-block-table.is-style-stripes' ),
 774                  'border-top-color'    => array( '.wp-block-latest-posts.is-grid li' ),
 775                  'color'               => array( ':root .has-subtle-background-color' ),
 776              ),
 777          ),
 778          'header-footer' => array(
 779              'accent'     => array(
 780                  'color'            => array( 'body:not(.overlay-header) .primary-menu > li > a', 'body:not(.overlay-header) .primary-menu > li > .icon', '.modal-menu a', '.footer-menu a, .footer-widgets a:where(:not(.wp-block-button__link))', '#site-footer .wp-block-button.is-style-outline', '.wp-block-pullquote:before', '.singular:not(.overlay-header) .entry-header a', '.archive-header a', '.header-footer-group .color-accent', '.header-footer-group .color-accent-hover:hover' ),
 781                  'background-color' => array( '.social-icons a', '#site-footer button:not(.toggle)', '#site-footer .button', '#site-footer .faux-button', '#site-footer .wp-block-button__link', '#site-footer .wp-block-file__button', '#site-footer input[type="button"]', '#site-footer input[type="reset"]', '#site-footer input[type="submit"]' ),
 782              ),
 783              'background' => array(
 784                  'color'            => array( '.social-icons a', 'body:not(.overlay-header) .primary-menu ul', '.header-footer-group button', '.header-footer-group .button', '.header-footer-group .faux-button', '.header-footer-group .wp-block-button:not(.is-style-outline) .wp-block-button__link', '.header-footer-group .wp-block-file__button', '.header-footer-group input[type="button"]', '.header-footer-group input[type="reset"]', '.header-footer-group input[type="submit"]' ),
 785                  'background-color' => array( '#site-header', '.footer-nav-widgets-wrapper', '#site-footer', '.menu-modal', '.menu-modal-inner', '.search-modal-inner', '.archive-header', '.singular .entry-header', '.singular .featured-media:before', '.wp-block-pullquote:before' ),
 786              ),
 787              'text'       => array(
 788                  'color'               => array( '.header-footer-group', 'body:not(.overlay-header) #site-header .toggle', '.menu-modal .toggle' ),
 789                  'background-color'    => array( 'body:not(.overlay-header) .primary-menu ul' ),
 790                  'border-bottom-color' => array( 'body:not(.overlay-header) .primary-menu > li > ul:after' ),
 791                  'border-left-color'   => array( 'body:not(.overlay-header) .primary-menu ul ul:after' ),
 792              ),
 793              'secondary'  => array(
 794                  'color' => array( '.site-description', 'body:not(.overlay-header) .toggle-inner .toggle-text', '.widget .post-date', '.widget .rss-date', '.widget_archive li', '.widget_categories li', '.widget cite', '.widget_pages li', '.widget_meta li', '.widget_nav_menu li', '.powered-by-wordpress', '.footer-credits .privacy-policy', '.to-the-top', '.singular .entry-header .post-meta', '.singular:not(.overlay-header) .entry-header .post-meta a' ),
 795              ),
 796              'borders'    => array(
 797                  'border-color'     => array( '.header-footer-group pre', '.header-footer-group fieldset', '.header-footer-group input', '.header-footer-group textarea', '.header-footer-group table', '.header-footer-group table *', '.footer-nav-widgets-wrapper', '#site-footer', '.menu-modal nav *', '.footer-widgets-outer-wrapper', '.footer-top' ),
 798                  'background-color' => array( '.header-footer-group table caption', 'body:not(.overlay-header) .header-inner .toggle-wrapper::before' ),
 799              ),
 800          ),
 801      );
 802  
 803      /**
 804       * Filters Twenty Twenty theme elements.
 805       *
 806       * @since Twenty Twenty 1.0
 807       *
 808       * @param array Array of elements.
 809       */
 810      return apply_filters( 'twentytwenty_get_elements_array', $elements );
 811  }


Generated : Thu May 9 08:20:02 2024 Cross-referenced by PHPXref