functions.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 1.0.0
  10. */
  11. /**
  12. * Table of Contents:
  13. * Theme Support
  14. * Required Files
  15. * Register Styles
  16. * Register Scripts
  17. * Register Menus
  18. * Custom Logo
  19. * WP Body Open
  20. * Register Sidebars
  21. * Enqueue Block Editor Assets
  22. * Enqueue Classic Editor Styles
  23. * Block Editor Settings
  24. */
  25. /**
  26. * Sets up theme defaults and registers support for various WordPress features.
  27. *
  28. * Note that this function is hooked into the after_setup_theme hook, which
  29. * runs before the init hook. The init hook is too late for some features, such
  30. * as indicating support for post thumbnails.
  31. */
  32. function twentytwenty_theme_support() {
  33. // Add default posts and comments RSS feed links to head.
  34. add_theme_support( 'automatic-feed-links' );
  35. // Custom background color.
  36. add_theme_support(
  37. 'custom-background',
  38. array(
  39. 'default-color' => 'f5efe0',
  40. )
  41. );
  42. // Set content-width.
  43. global $content_width;
  44. if ( ! isset( $content_width ) ) {
  45. $content_width = 580;
  46. }
  47. /*
  48. * Enable support for Post Thumbnails on posts and pages.
  49. *
  50. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  51. */
  52. add_theme_support( 'post-thumbnails' );
  53. // Set post thumbnail size.
  54. set_post_thumbnail_size( 1200, 9999 );
  55. // Add custom image size used in Cover Template.
  56. add_image_size( 'twentytwenty-fullscreen', 1980, 9999 );
  57. // Custom logo.
  58. $logo_width = 120;
  59. $logo_height = 90;
  60. // If the retina setting is active, double the recommended width and height.
  61. if ( get_theme_mod( 'retina_logo', false ) ) {
  62. $logo_width = floor( $logo_width * 2 );
  63. $logo_height = floor( $logo_height * 2 );
  64. }
  65. add_theme_support(
  66. 'custom-logo',
  67. array(
  68. 'height' => $logo_height,
  69. 'width' => $logo_width,
  70. 'flex-height' => true,
  71. 'flex-width' => true,
  72. )
  73. );
  74. /*
  75. * Let WordPress manage the document title.
  76. * By adding theme support, we declare that this theme does not use a
  77. * hard-coded <title> tag in the document head, and expect WordPress to
  78. * provide it for us.
  79. */
  80. add_theme_support( 'title-tag' );
  81. /*
  82. * Switch default core markup for search form, comment form, and comments
  83. * to output valid HTML5.
  84. */
  85. add_theme_support(
  86. 'html5',
  87. array(
  88. 'search-form',
  89. 'comment-form',
  90. 'comment-list',
  91. 'gallery',
  92. 'caption',
  93. 'script',
  94. 'style',
  95. )
  96. );
  97. /*
  98. * Make theme available for translation.
  99. * Translations can be filed in the /languages/ directory.
  100. * If you're building a theme based on Twenty Twenty, use a find and replace
  101. * to change 'twentytwenty' to the name of your theme in all the template files.
  102. */
  103. load_theme_textdomain( 'twentytwenty' );
  104. // Add support for full and wide align images.
  105. add_theme_support( 'align-wide' );
  106. /*
  107. * Adds starter content to highlight the theme on fresh sites.
  108. * This is done conditionally to avoid loading the starter content on every
  109. * page load, as it is a one-off operation only needed once in the customizer.
  110. */
  111. if ( is_customize_preview() ) {
  112. require get_template_directory() . '/inc/starter-content.php';
  113. add_theme_support( 'starter-content', twentytwenty_get_starter_content() );
  114. }
  115. // Add theme support for selective refresh for widgets.
  116. add_theme_support( 'customize-selective-refresh-widgets' );
  117. /*
  118. * Adds `async` and `defer` support for scripts registered or enqueued
  119. * by the theme.
  120. */
  121. $loader = new TwentyTwenty_Script_Loader();
  122. add_filter( 'script_loader_tag', array( $loader, 'filter_script_loader_tag' ), 10, 2 );
  123. }
  124. add_action( 'after_setup_theme', 'twentytwenty_theme_support' );
  125. /**
  126. * REQUIRED FILES
  127. * Include required files.
  128. */
  129. require get_template_directory() . '/inc/template-tags.php';
  130. // Handle SVG icons.
  131. require get_template_directory() . '/classes/class-twentytwenty-svg-icons.php';
  132. require get_template_directory() . '/inc/svg-icons.php';
  133. // Handle Customizer settings.
  134. require get_template_directory() . '/classes/class-twentytwenty-customize.php';
  135. // Require Separator Control class.
  136. require get_template_directory() . '/classes/class-twentytwenty-separator-control.php';
  137. // Custom comment walker.
  138. require get_template_directory() . '/classes/class-twentytwenty-walker-comment.php';
  139. // Custom page walker.
  140. require get_template_directory() . '/classes/class-twentytwenty-walker-page.php';
  141. // Custom script loader class.
  142. require get_template_directory() . '/classes/class-twentytwenty-script-loader.php';
  143. // Non-latin language handling.
  144. require get_template_directory() . '/classes/class-twentytwenty-non-latin-languages.php';
  145. // Custom CSS.
  146. require get_template_directory() . '/inc/custom-css.php';
  147. /**
  148. * Register and Enqueue Styles.
  149. */
  150. function twentytwenty_register_styles() {
  151. $theme_version = wp_get_theme()->get( 'Version' );
  152. wp_enqueue_style( 'twentytwenty-style', get_stylesheet_uri(), array(), $theme_version );
  153. wp_style_add_data( 'twentytwenty-style', 'rtl', 'replace' );
  154. // Add output of Customizer settings as inline style.
  155. wp_add_inline_style( 'twentytwenty-style', twentytwenty_get_customizer_css( 'front-end' ) );
  156. // Add print CSS.
  157. wp_enqueue_style( 'twentytwenty-print-style', get_template_directory_uri() . '/print.css', null, $theme_version, 'print' );
  158. }
  159. add_action( 'wp_enqueue_scripts', 'twentytwenty_register_styles' );
  160. /**
  161. * Register and Enqueue Scripts.
  162. */
  163. function twentytwenty_register_scripts() {
  164. $theme_version = wp_get_theme()->get( 'Version' );
  165. if ( ( ! is_admin() ) && is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  166. wp_enqueue_script( 'comment-reply' );
  167. }
  168. wp_enqueue_script( 'twentytwenty-js', get_template_directory_uri() . '/assets/js/index.js', array(), $theme_version, false );
  169. wp_script_add_data( 'twentytwenty-js', 'async', true );
  170. }
  171. add_action( 'wp_enqueue_scripts', 'twentytwenty_register_scripts' );
  172. /**
  173. * Fix skip link focus in IE11.
  174. *
  175. * This does not enqueue the script because it is tiny and because it is only for IE11,
  176. * thus it does not warrant having an entire dedicated blocking script being loaded.
  177. *
  178. * @link https://git.io/vWdr2
  179. */
  180. function twentytwenty_skip_link_focus_fix() {
  181. // The following is minified via `terser --compress --mangle -- assets/js/skip-link-focus-fix.js`.
  182. ?>
  183. <script>
  184. /(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);
  185. </script>
  186. <?php
  187. }
  188. add_action( 'wp_print_footer_scripts', 'twentytwenty_skip_link_focus_fix' );
  189. /** Enqueue non-latin language styles
  190. *
  191. * @since 1.0.0
  192. *
  193. * @return void
  194. */
  195. function twentytwenty_non_latin_languages() {
  196. $custom_css = TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'front-end' );
  197. if ( $custom_css ) {
  198. wp_add_inline_style( 'twentytwenty-style', $custom_css );
  199. }
  200. }
  201. add_action( 'wp_enqueue_scripts', 'twentytwenty_non_latin_languages' );
  202. /**
  203. * Register navigation menus uses wp_nav_menu in five places.
  204. */
  205. function twentytwenty_menus() {
  206. $locations = array(
  207. 'primary' => __( 'Desktop Horizontal Menu', 'twentytwenty' ),
  208. 'expanded' => __( 'Desktop Expanded Menu', 'twentytwenty' ),
  209. 'mobile' => __( 'Mobile Menu', 'twentytwenty' ),
  210. 'footer' => __( 'Footer Menu', 'twentytwenty' ),
  211. 'social' => __( 'Social Menu', 'twentytwenty' ),
  212. );
  213. register_nav_menus( $locations );
  214. }
  215. add_action( 'init', 'twentytwenty_menus' );
  216. /**
  217. * Get the information about the logo.
  218. *
  219. * @param string $html The HTML output from get_custom_logo (core function).
  220. *
  221. * @return string $html
  222. */
  223. function twentytwenty_get_custom_logo( $html ) {
  224. $logo_id = get_theme_mod( 'custom_logo' );
  225. if ( ! $logo_id ) {
  226. return $html;
  227. }
  228. $logo = wp_get_attachment_image_src( $logo_id, 'full' );
  229. if ( $logo ) {
  230. // For clarity.
  231. $logo_width = esc_attr( $logo[1] );
  232. $logo_height = esc_attr( $logo[2] );
  233. // If the retina logo setting is active, reduce the width/height by half.
  234. if ( get_theme_mod( 'retina_logo', false ) ) {
  235. $logo_width = floor( $logo_width / 2 );
  236. $logo_height = floor( $logo_height / 2 );
  237. $search = array(
  238. '/width=\"\d+\"/iU',
  239. '/height=\"\d+\"/iU',
  240. );
  241. $replace = array(
  242. "width=\"{$logo_width}\"",
  243. "height=\"{$logo_height}\"",
  244. );
  245. // Add a style attribute with the height, or append the height to the style attribute if the style attribute already exists.
  246. if ( strpos( $html, ' style=' ) === false ) {
  247. $search[] = '/(src=)/';
  248. $replace[] = "style=\"height: {$logo_height}px;\" src=";
  249. } else {
  250. $search[] = '/(style="[^"]*)/';
  251. $replace[] = "$1 height: {$logo_height}px;";
  252. }
  253. $html = preg_replace( $search, $replace, $html );
  254. }
  255. }
  256. return $html;
  257. }
  258. add_filter( 'get_custom_logo', 'twentytwenty_get_custom_logo' );
  259. if ( ! function_exists( 'wp_body_open' ) ) {
  260. /**
  261. * Shim for wp_body_open, ensuring backwards compatibility with versions of WordPress older than 5.2.
  262. */
  263. function wp_body_open() {
  264. do_action( 'wp_body_open' );
  265. }
  266. }
  267. /**
  268. * Include a skip to content link at the top of the page so that users can bypass the menu.
  269. */
  270. function twentytwenty_skip_link() {
  271. echo '<a class="skip-link screen-reader-text" href="#site-content">' . __( 'Skip to the content', 'twentytwenty' ) . '</a>';
  272. }
  273. add_action( 'wp_body_open', 'twentytwenty_skip_link', 5 );
  274. /**
  275. * Register widget areas.
  276. *
  277. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  278. */
  279. function twentytwenty_sidebar_registration() {
  280. // Arguments used in all register_sidebar() calls.
  281. $shared_args = array(
  282. 'before_title' => '<h2 class="widget-title subheading heading-size-3">',
  283. 'after_title' => '</h2>',
  284. 'before_widget' => '<div class="widget %2$s"><div class="widget-content">',
  285. 'after_widget' => '</div></div>',
  286. );
  287. // Footer #1.
  288. register_sidebar(
  289. array_merge(
  290. $shared_args,
  291. array(
  292. 'name' => __( 'Footer #1', 'twentytwenty' ),
  293. 'id' => 'sidebar-1',
  294. 'description' => __( 'Widgets in this area will be displayed in the first column in the footer.', 'twentytwenty' ),
  295. )
  296. )
  297. );
  298. // Footer #2.
  299. register_sidebar(
  300. array_merge(
  301. $shared_args,
  302. array(
  303. 'name' => __( 'Footer #2', 'twentytwenty' ),
  304. 'id' => 'sidebar-2',
  305. 'description' => __( 'Widgets in this area will be displayed in the second column in the footer.', 'twentytwenty' ),
  306. )
  307. )
  308. );
  309. }
  310. add_action( 'widgets_init', 'twentytwenty_sidebar_registration' );
  311. /**
  312. * Enqueue supplemental block editor styles.
  313. */
  314. function twentytwenty_block_editor_styles() {
  315. $css_dependencies = array();
  316. // Enqueue the editor styles.
  317. wp_enqueue_style( 'twentytwenty-block-editor-styles', get_theme_file_uri( '/assets/css/editor-style-block.css' ), $css_dependencies, wp_get_theme()->get( 'Version' ), 'all' );
  318. wp_style_add_data( 'twentytwenty-block-editor-styles', 'rtl', 'replace' );
  319. // Add inline style from the Customizer.
  320. wp_add_inline_style( 'twentytwenty-block-editor-styles', twentytwenty_get_customizer_css( 'block-editor' ) );
  321. // Add inline style for non-latin fonts.
  322. wp_add_inline_style( 'twentytwenty-block-editor-styles', TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'block-editor' ) );
  323. // Enqueue the editor script.
  324. wp_enqueue_script( 'twentytwenty-block-editor-script', get_theme_file_uri( '/assets/js/editor-script-block.js' ), array( 'wp-blocks', 'wp-dom' ), wp_get_theme()->get( 'Version' ), true );
  325. }
  326. add_action( 'enqueue_block_editor_assets', 'twentytwenty_block_editor_styles', 1, 1 );
  327. /**
  328. * Enqueue classic editor styles.
  329. */
  330. function twentytwenty_classic_editor_styles() {
  331. $classic_editor_styles = array(
  332. '/assets/css/editor-style-classic.css',
  333. );
  334. add_editor_style( $classic_editor_styles );
  335. }
  336. add_action( 'init', 'twentytwenty_classic_editor_styles' );
  337. /**
  338. * Output Customizer settings in the classic editor.
  339. * Adds styles to the head of the TinyMCE iframe. Kudos to @Otto42 for the original solution.
  340. *
  341. * @param array $mce_init TinyMCE styles.
  342. *
  343. * @return array $mce_init TinyMCE styles.
  344. */
  345. function twentytwenty_add_classic_editor_customizer_styles( $mce_init ) {
  346. $styles = twentytwenty_get_customizer_css( 'classic-editor' );
  347. if ( ! isset( $mce_init['content_style'] ) ) {
  348. $mce_init['content_style'] = $styles . ' ';
  349. } else {
  350. $mce_init['content_style'] .= ' ' . $styles . ' ';
  351. }
  352. return $mce_init;
  353. }
  354. add_filter( 'tiny_mce_before_init', 'twentytwenty_add_classic_editor_customizer_styles' );
  355. /**
  356. * Output non-latin font styles in the classic editor.
  357. * Adds styles to the head of the TinyMCE iframe. Kudos to @Otto42 for the original solution.
  358. *
  359. * @param array $mce_init TinyMCE styles.
  360. *
  361. * @return array $mce_init TinyMCE styles.
  362. */
  363. function twentytwenty_add_classic_editor_non_latin_styles( $mce_init ) {
  364. $styles = TwentyTwenty_Non_Latin_Languages::get_non_latin_css( 'classic-editor' );
  365. // Return if there are no styles to add.
  366. if ( ! $styles ) {
  367. return $mce_init;
  368. }
  369. if ( ! isset( $mce_init['content_style'] ) ) {
  370. $mce_init['content_style'] = $styles . ' ';
  371. } else {
  372. $mce_init['content_style'] .= ' ' . $styles . ' ';
  373. }
  374. return $mce_init;
  375. }
  376. add_filter( 'tiny_mce_before_init', 'twentytwenty_add_classic_editor_non_latin_styles' );
  377. /**
  378. * Block Editor Settings.
  379. * Add custom colors and font sizes to the block editor.
  380. */
  381. function twentytwenty_block_editor_settings() {
  382. // Block Editor Palette.
  383. $editor_color_palette = array(
  384. array(
  385. 'name' => __( 'Accent Color', 'twentytwenty' ),
  386. 'slug' => 'accent',
  387. 'color' => twentytwenty_get_color_for_area( 'content', 'accent' ),
  388. ),
  389. array(
  390. 'name' => __( 'Primary', 'twentytwenty' ),
  391. 'slug' => 'primary',
  392. 'color' => twentytwenty_get_color_for_area( 'content', 'text' ),
  393. ),
  394. array(
  395. 'name' => __( 'Secondary', 'twentytwenty' ),
  396. 'slug' => 'secondary',
  397. 'color' => twentytwenty_get_color_for_area( 'content', 'secondary' ),
  398. ),
  399. array(
  400. 'name' => __( 'Subtle Background', 'twentytwenty' ),
  401. 'slug' => 'subtle-background',
  402. 'color' => twentytwenty_get_color_for_area( 'content', 'borders' ),
  403. ),
  404. );
  405. // Add the background option.
  406. $background_color = get_theme_mod( 'background_color' );
  407. if ( ! $background_color ) {
  408. $background_color_arr = get_theme_support( 'custom-background' );
  409. $background_color = $background_color_arr[0]['default-color'];
  410. }
  411. $editor_color_palette[] = array(
  412. 'name' => __( 'Background Color', 'twentytwenty' ),
  413. 'slug' => 'background',
  414. 'color' => '#' . $background_color,
  415. );
  416. // If we have accent colors, add them to the block editor palette.
  417. if ( $editor_color_palette ) {
  418. add_theme_support( 'editor-color-palette', $editor_color_palette );
  419. }
  420. // Block Editor Font Sizes.
  421. add_theme_support(
  422. 'editor-font-sizes',
  423. array(
  424. array(
  425. 'name' => _x( 'Small', 'Name of the small font size in the block editor', 'twentytwenty' ),
  426. 'shortName' => _x( 'S', 'Short name of the small font size in the block editor.', 'twentytwenty' ),
  427. 'size' => 18,
  428. 'slug' => 'small',
  429. ),
  430. array(
  431. 'name' => _x( 'Regular', 'Name of the regular font size in the block editor', 'twentytwenty' ),
  432. 'shortName' => _x( 'M', 'Short name of the regular font size in the block editor.', 'twentytwenty' ),
  433. 'size' => 21,
  434. 'slug' => 'normal',
  435. ),
  436. array(
  437. 'name' => _x( 'Large', 'Name of the large font size in the block editor', 'twentytwenty' ),
  438. 'shortName' => _x( 'L', 'Short name of the large font size in the block editor.', 'twentytwenty' ),
  439. 'size' => 26.25,
  440. 'slug' => 'large',
  441. ),
  442. array(
  443. 'name' => _x( 'Larger', 'Name of the larger font size in the block editor', 'twentytwenty' ),
  444. 'shortName' => _x( 'XL', 'Short name of the larger font size in the block editor.', 'twentytwenty' ),
  445. 'size' => 32,
  446. 'slug' => 'larger',
  447. ),
  448. )
  449. );
  450. // If we have a dark background color then add support for dark editor style.
  451. // We can determine if the background color is dark by checking if the text-color is white.
  452. if ( '#ffffff' === strtolower( twentytwenty_get_color_for_area( 'content', 'text' ) ) ) {
  453. add_theme_support( 'dark-editor-style' );
  454. }
  455. }
  456. add_action( 'after_setup_theme', 'twentytwenty_block_editor_settings' );
  457. /**
  458. * Overwrite default more tag with styling and screen reader markup.
  459. *
  460. * @param string $html The default output HTML for the more tag.
  461. *
  462. * @return string $html
  463. */
  464. function twentytwenty_read_more_tag( $html ) {
  465. 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 );
  466. }
  467. add_filter( 'the_content_more_link', 'twentytwenty_read_more_tag' );
  468. /**
  469. * Enqueues scripts for customizer controls & settings.
  470. *
  471. * @since 1.0.0
  472. *
  473. * @return void
  474. */
  475. function twentytwenty_customize_controls_enqueue_scripts() {
  476. $theme_version = wp_get_theme()->get( 'Version' );
  477. // Add main customizer js file.
  478. wp_enqueue_script( 'twentytwenty-customize', get_template_directory_uri() . '/assets/js/customize.js', array( 'jquery' ), $theme_version, false );
  479. // Add script for color calculations.
  480. wp_enqueue_script( 'twentytwenty-color-calculations', get_template_directory_uri() . '/assets/js/color-calculations.js', array( 'wp-color-picker' ), $theme_version, false );
  481. // Add script for controls.
  482. 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, false );
  483. wp_localize_script( 'twentytwenty-customize-controls', 'twentyTwentyBgColors', twentytwenty_get_customizer_color_vars() );
  484. }
  485. add_action( 'customize_controls_enqueue_scripts', 'twentytwenty_customize_controls_enqueue_scripts' );
  486. /**
  487. * Enqueue scripts for the customizer preview.
  488. *
  489. * @since 1.0.0
  490. *
  491. * @return void
  492. */
  493. function twentytwenty_customize_preview_init() {
  494. $theme_version = wp_get_theme()->get( 'Version' );
  495. wp_enqueue_script( 'twentytwenty-customize-preview', get_theme_file_uri( '/assets/js/customize-preview.js' ), array( 'customize-preview', 'customize-selective-refresh', 'jquery' ), $theme_version, true );
  496. wp_localize_script( 'twentytwenty-customize-preview', 'twentyTwentyBgColors', twentytwenty_get_customizer_color_vars() );
  497. wp_localize_script( 'twentytwenty-customize-preview', 'twentyTwentyPreviewEls', twentytwenty_get_elements_array() );
  498. wp_add_inline_script(
  499. 'twentytwenty-customize-preview',
  500. sprintf(
  501. 'wp.customize.selectiveRefresh.partialConstructor[ %1$s ].prototype.attrs = %2$s;',
  502. wp_json_encode( 'cover_opacity' ),
  503. wp_json_encode( twentytwenty_customize_opacity_range() )
  504. )
  505. );
  506. }
  507. add_action( 'customize_preview_init', 'twentytwenty_customize_preview_init' );
  508. /**
  509. * Get accessible color for an area.
  510. *
  511. * @since 1.0.0
  512. *
  513. * @param string $area The area we want to get the colors for.
  514. * @param string $context Can be 'text' or 'accent'.
  515. * @return string Returns a HEX color.
  516. */
  517. function twentytwenty_get_color_for_area( $area = 'content', $context = 'text' ) {
  518. // Get the value from the theme-mod.
  519. $settings = get_theme_mod(
  520. 'accent_accessible_colors',
  521. array(
  522. 'content' => array(
  523. 'text' => '#000000',
  524. 'accent' => '#cd2653',
  525. 'secondary' => '#6d6d6d',
  526. 'borders' => '#dcd7ca',
  527. ),
  528. 'header-footer' => array(
  529. 'text' => '#000000',
  530. 'accent' => '#cd2653',
  531. 'secondary' => '#6d6d6d',
  532. 'borders' => '#dcd7ca',
  533. ),
  534. )
  535. );
  536. // If we have a value return it.
  537. if ( isset( $settings[ $area ] ) && isset( $settings[ $area ][ $context ] ) ) {
  538. return $settings[ $area ][ $context ];
  539. }
  540. // Return false if the option doesn't exist.
  541. return false;
  542. }
  543. /**
  544. * Returns an array of variables for the customizer preview.
  545. *
  546. * @since 1.0.0
  547. *
  548. * @return array
  549. */
  550. function twentytwenty_get_customizer_color_vars() {
  551. $colors = array(
  552. 'content' => array(
  553. 'setting' => 'background_color',
  554. ),
  555. 'header-footer' => array(
  556. 'setting' => 'header_footer_background_color',
  557. ),
  558. );
  559. return $colors;
  560. }
  561. /**
  562. * Get an array of elements.
  563. *
  564. * @since 1.0
  565. *
  566. * @return array
  567. */
  568. function twentytwenty_get_elements_array() {
  569. // The array is formatted like this:
  570. // [key-in-saved-setting][sub-key-in-setting][css-property] = [elements].
  571. $elements = array(
  572. 'content' => array(
  573. 'accent' => array(
  574. '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' ),
  575. 'border-color' => array( 'blockquote', '.border-color-accent', '.border-color-accent-hover:hover', '.border-color-accent-hover:focus' ),
  576. 'background-color' => array( 'button:not(.toggle)', '.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' ),
  577. 'fill' => array( '.fill-children-accent', '.fill-children-accent *' ),
  578. ),
  579. 'background' => array(
  580. '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)' ),
  581. 'background-color' => array( ':root .has-background-background-color' ),
  582. ),
  583. 'text' => array(
  584. 'color' => array( 'body', '.entry-title a', ':root .has-primary-color' ),
  585. 'background-color' => array( ':root .has-primary-background-color' ),
  586. ),
  587. 'secondary' => array(
  588. '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' ),
  589. 'background-color' => array( ':root .has-secondary-background-color' ),
  590. ),
  591. 'borders' => array(
  592. 'border-color' => array( 'pre', 'fieldset', 'input', 'textarea', 'table', 'table *', 'hr' ),
  593. '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' ),
  594. 'border-bottom-color' => array( '.wp-block-table.is-style-stripes' ),
  595. 'border-top-color' => array( '.wp-block-latest-posts.is-grid li' ),
  596. 'color' => array( ':root .has-subtle-background-color' ),
  597. ),
  598. ),
  599. 'header-footer' => array(
  600. 'accent' => array(
  601. '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', '#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' ),
  602. '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"]' ),
  603. ),
  604. 'background' => array(
  605. '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"]' ),
  606. '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' ),
  607. ),
  608. 'text' => array(
  609. 'color' => array( '.header-footer-group', 'body:not(.overlay-header) #site-header .toggle', '.menu-modal .toggle' ),
  610. 'background-color' => array( 'body:not(.overlay-header) .primary-menu ul' ),
  611. 'border-bottom-color' => array( 'body:not(.overlay-header) .primary-menu > li > ul:after' ),
  612. 'border-left-color' => array( 'body:not(.overlay-header) .primary-menu ul ul:after' ),
  613. ),
  614. 'secondary' => array(
  615. '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', '.to-the-top', '.singular .entry-header .post-meta', '.singular:not(.overlay-header) .entry-header .post-meta a' ),
  616. ),
  617. 'borders' => array(
  618. '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' ),
  619. 'background-color' => array( '.header-footer-group table caption', 'body:not(.overlay-header) .header-inner .toggle-wrapper::before' ),
  620. ),
  621. ),
  622. );
  623. /**
  624. * Filters Twenty Twenty theme elements
  625. *
  626. * @since 1.0.0
  627. *
  628. * @param array Array of elements
  629. */
  630. return apply_filters( 'twentytwenty_get_elements_array', $elements );
  631. }