wpcom.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * WordPress.com-specific functions and definitions.
  5. *
  6. * This file is centrally included from `wp-content/mu-plugins/wpcom-theme-compat.php`.
  7. *
  8. * @package Varia
  9. */
  10. /**
  11. * Adds support for wp.com-specific theme functions.
  12. *
  13. * @global array $themecolors
  14. */
  15. function varia_wpcom_setup() {
  16. global $themecolors;
  17. // Disable automatically generated color palettes.
  18. add_theme_support(
  19. 'wpcom-colors',
  20. array(
  21. 'only-featured-palettes' => true,
  22. )
  23. );
  24. // Set theme colors for third party services.
  25. if ( ! isset( $themecolors ) ) {
  26. $themecolors = array(
  27. 'bg' => 'ffffff',
  28. 'border' => '767676',
  29. 'text' => '111111',
  30. 'link' => '0073aa',
  31. 'url' => '0073aa',
  32. );
  33. }
  34. }
  35. add_action( 'after_setup_theme', 'varia_wpcom_setup' );
  36. /**
  37. * Add setting for hiding page title on the homepage.
  38. */
  39. function varia_wpcom_customize_update( $wp_customize ) {
  40. $wp_customize->add_setting(
  41. 'hide_front_page_title',
  42. array(
  43. 'default' => false,
  44. 'type' => 'theme_mod',
  45. 'transport' => 'postMessage',
  46. 'sanitize_callback' => 'varia_sanitize_checkbox',
  47. )
  48. );
  49. $wp_customize->add_control(
  50. 'hide_front_page_title',
  51. array(
  52. 'label' => esc_html__( 'Hide Homepage Title', 'varia' ),
  53. 'description' => esc_html__( 'Check to hide the page title, if your homepage is set to display a static page.', 'varia' ),
  54. 'section' => 'static_front_page',
  55. 'priority' => 10,
  56. 'type' => 'checkbox',
  57. 'settings' => 'hide_front_page_title',
  58. )
  59. );
  60. $wp_customize->add_setting( 'color_a11y_warning' );
  61. $wp_customize->add_control(
  62. 'color_a11y_warning',
  63. array(
  64. 'id' => 'id',
  65. 'label' => esc_html__( 'Color Accessibility Warning', 'varia' ),
  66. 'description' => sprintf(
  67. __( 'In order to ensure people can read your site, try to maintain a strong contrast ratio between the colors you choose here. <a href="%s" target="_blank">Learn more about color contrast</a>.', 'varia' ),
  68. esc_url( 'https://a11yproject.com/posts/what-is-color-contrast/' )
  69. ),
  70. 'section' => 'colors_manager_tool',
  71. 'priority' => 10,
  72. 'type' => 'hidden',
  73. )
  74. );
  75. }
  76. add_action( 'customize_register', 'varia_wpcom_customize_update' );
  77. /**
  78. * Bind JS handlers to instantly live-preview changes.
  79. */
  80. function varia_wpcom_customize_preview_js() {
  81. wp_enqueue_script( 'varia_wpcom_customize_preview', get_theme_file_uri( '/inc/customize-preview-wpcom.js' ), array( 'customize-preview' ), '1.0', true );
  82. }
  83. add_action( 'customize_preview_init', 'varia_wpcom_customize_preview_js' );
  84. /**
  85. * Enqueue our WP.com styles for front-end.
  86. * Loads after style.css so we can add overrides.
  87. */
  88. function varia_wpcom_scripts() {
  89. wp_enqueue_style( 'varia-wpcom-style', get_template_directory_uri() . '/inc/style-wpcom.css', array( 'varia-style' ), '20181218' );
  90. }
  91. add_action( 'wp_enqueue_scripts', 'varia_wpcom_scripts' );
  92. /**
  93. * Adds custom classes to the array of body classes.
  94. *
  95. * @param array $classes Classes for the body element.
  96. * @return array
  97. */
  98. function varia_wpcom_body_classes( $classes ) {
  99. $hide = get_theme_mod( 'hide_front_page_title', false );
  100. if ( true === $hide ) {
  101. $classes[] = 'hide-homepage-title';
  102. }
  103. $hide_site_header = get_theme_mod( 'hide_site_header', false );
  104. if ( true === $hide_site_header ) {
  105. $classes[] = 'hide-homepage-header';
  106. }
  107. $hide_site_footer = get_theme_mod( 'hide_site_footer', false );
  108. if ( true === $hide_site_footer ) {
  109. $classes[] = 'hide-homepage-footer';
  110. }
  111. $credit_option = get_option( 'footercredit' );
  112. if ( 'hidden' == $credit_option ) {
  113. $classes[] = 'hide-footer-credit';
  114. }
  115. return $classes;
  116. }
  117. add_filter( 'body_class', 'varia_wpcom_body_classes' );
  118. /**
  119. * Adds custom classes to the admin body classes.
  120. *
  121. * @param string $classes Classes for the body element.
  122. * @return string
  123. */
  124. function varia_wpcom_admin_body_classes( $classes ) {
  125. global $post;
  126. $is_block_editor_screen = ( function_exists( 'get_current_screen' ) && get_current_screen() && get_current_screen()->is_block_editor() );
  127. $hide = get_theme_mod( 'hide_front_page_title', false );
  128. $front_page = (int) get_option( 'page_on_front' );
  129. if ( $is_block_editor_screen && $front_page === $post->ID && true === $hide ) {
  130. $classes .= ' hide-homepage-title';
  131. }
  132. return $classes;
  133. }
  134. add_filter( 'admin_body_class', 'varia_wpcom_admin_body_classes' );
  135. /**
  136. * Enqueue our WP.com styles for the block editor.
  137. */
  138. function varia_wpcom_editor_scripts() {
  139. wp_enqueue_style( 'varia-wpcom-editor-style', get_template_directory_uri() . '/inc/style-editor-wpcom.css', array(), '20190906' );
  140. }
  141. add_action( 'enqueue_block_editor_assets', 'varia_wpcom_editor_scripts' );
  142. /**
  143. * Enqueue CSS for customizer a11y warning.
  144. */
  145. function varia_wpcom_enqueue_message_scripts() {
  146. wp_enqueue_style( 'varia-customize-message-wpcom-style', get_template_directory_uri() . '/inc/customize-message-wpcom.css', array(), wp_get_theme()->get( 'Version' ) );
  147. }
  148. add_action( 'customize_controls_enqueue_scripts', 'varia_wpcom_enqueue_message_scripts' );