wpcom.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Seedlet
  9. */
  10. /**
  11. * Adds support for wp.com-specific theme functions.
  12. *
  13. * @global array $themecolors
  14. */
  15. function seedlet_wpcom_setup() {
  16. global $themecolors;
  17. // Set theme colors for third party services.
  18. if ( ! isset( $themecolors ) ) {
  19. $themecolors = array(
  20. 'bg' => 'FFFFFF',
  21. 'txt' => '333333',
  22. 'link' => '000000',
  23. 'fg1' => '3C8067',
  24. 'fg2' => 'FAFBF6',
  25. );
  26. }
  27. // Disable automatically generated color palettes.
  28. add_theme_support( 'wpcom-colors', array(
  29. 'only-featured-palettes' => true,
  30. ) );
  31. $wpcom_colors_array = get_theme_mod( 'colors_manager' );
  32. if ( ! empty( $wpcom_colors_array ) ) {
  33. $primary = $wpcom_colors_array['colors']['link'] ?? '';
  34. $secondary = $wpcom_colors_array['colors']['fg1'] ?? '';
  35. $foreground = $wpcom_colors_array['colors']['txt'] ?? '';
  36. $tertiary = $wpcom_colors_array['colors']['fg2'] ?? '';
  37. $background = $wpcom_colors_array['colors']['bg'] ?? '';
  38. /**
  39. * De-register original editor color palette in favor of the wpcom implementation
  40. */
  41. remove_theme_support( 'editor-color-palette' );
  42. remove_theme_support( 'editor-gradient-presets' );
  43. add_theme_support(
  44. 'editor-color-palette',
  45. array(
  46. array(
  47. 'name' => __( 'Primary', 'seedlet' ),
  48. 'slug' => 'primary',
  49. 'color' => $primary
  50. ),
  51. array(
  52. 'name' => __( 'Secondary', 'seedlet' ),
  53. 'slug' => 'secondary',
  54. 'color' => $secondary
  55. ),
  56. array(
  57. 'name' => __( 'Foreground', 'seedlet' ),
  58. 'slug' => 'foreground',
  59. 'color' => $foreground
  60. ),
  61. array(
  62. 'name' => __( 'Tertiary', 'seedlet' ),
  63. 'slug' => 'tertiary',
  64. 'color' => $tertiary
  65. ),
  66. array(
  67. 'name' => __( 'Background', 'seedlet' ),
  68. 'slug' => 'background',
  69. 'color' => $background
  70. ),
  71. )
  72. );
  73. $gradient_color_a = $secondary;
  74. $gradient_color_b = $tertiary;
  75. add_theme_support(
  76. 'editor-gradient-presets',
  77. array(
  78. array(
  79. 'name' => __( 'Diagonal', 'seedlet' ),
  80. 'gradient' => 'linear-gradient(to bottom right, ' . $gradient_color_a . ' 49.9%, ' . $gradient_color_b . ' 50%)',
  81. 'slug' => 'hard-diagonal',
  82. ),
  83. array(
  84. 'name' => __( 'Diagonal inverted', 'seedlet' ),
  85. 'gradient' => 'linear-gradient(to top left, ' . $gradient_color_a . ' 49.9%, ' . $gradient_color_b . ' 50%)',
  86. 'slug' => 'hard-diagonal-inverted',
  87. ),
  88. array(
  89. 'name' => __( 'Horizontal', 'seedlet' ),
  90. 'gradient' => 'linear-gradient(to bottom, ' . $gradient_color_a . ' 50%, ' . $gradient_color_b . ' 50%)',
  91. 'slug' => 'hard-horizontal',
  92. ),
  93. array(
  94. 'name' => __( 'Horizontal inverted', 'seedlet' ),
  95. 'gradient' => 'linear-gradient(to top, ' . $gradient_color_a . ' 50%, ' . $gradient_color_b . ' 50%)',
  96. 'slug' => 'hard-horizontal-inverted',
  97. ),
  98. array(
  99. 'name' => __( 'Diagonal gradient', 'seedlet' ),
  100. 'gradient' => 'linear-gradient(to bottom right, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  101. 'slug' => 'diagonal',
  102. ),
  103. array(
  104. 'name' => __( 'Diagonal inverted gradient', 'seedlet' ),
  105. 'gradient' => 'linear-gradient(to top left, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  106. 'slug' => 'diagonal-inverted',
  107. ),
  108. array(
  109. 'name' => __( 'Horizontal gradient', 'seedlet' ),
  110. 'gradient' => 'linear-gradient(to bottom, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  111. 'slug' => 'horizontal',
  112. ),
  113. array(
  114. 'name' => __( 'Horizontal inverted gradient', 'seedlet' ),
  115. 'gradient' => 'linear-gradient(to top, ' . $gradient_color_a . ', ' . $gradient_color_b . ')',
  116. 'slug' => 'horizontal-inverted',
  117. ),
  118. array(
  119. 'name' => __( 'Stripe', 'seedlet' ),
  120. 'gradient' => 'linear-gradient(to bottom, transparent 20%, ' . $gradient_color_a . ' 20%, ' . $gradient_color_a . ' 80%, transparent 80%)',
  121. 'slug' => 'stripe',
  122. ),
  123. )
  124. );
  125. }
  126. }
  127. add_action( 'after_setup_theme', 'seedlet_wpcom_setup' );
  128. /**
  129. * Add settings for hiding page title on the homepage
  130. * and a customizer message about contrast.
  131. */
  132. function seedlet_wpcom_customize_update( $wp_customize ) {
  133. $wp_customize->add_setting( 'hide_front_page_title', array(
  134. 'default' => false,
  135. 'type' => 'theme_mod',
  136. 'transport' => 'postMessage',
  137. 'sanitize_callback' => 'seedlet_sanitize_checkbox',
  138. ) );
  139. $wp_customize->add_control( 'hide_front_page_title', array(
  140. 'label' => esc_html__( 'Hide Homepage Title', 'seedlet' ),
  141. 'description' => esc_html__( 'Check to hide the page title, if your homepage is set to display a static page.', 'seedlet' ),
  142. 'section' => 'static_front_page',
  143. 'priority' => 10,
  144. 'type' => 'checkbox',
  145. 'settings' => 'hide_front_page_title',
  146. ) );
  147. $wp_customize->add_setting( 'color_a11y_warning' );
  148. $wp_customize->add_control( 'color_a11y_warning', array(
  149. 'id' => 'id',
  150. 'label' => esc_html__( 'Color Accessibility Warning', 'seedlet' ),
  151. 'description' => sprintf(
  152. __( '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>.', 'seedlet' ),
  153. esc_url( 'https://a11yproject.com/posts/what-is-color-contrast/' )
  154. ),
  155. 'section' => 'colors_manager_tool',
  156. 'priority' => 10,
  157. 'type' => 'hidden',
  158. ) );
  159. }
  160. add_action( 'customize_register', 'seedlet_wpcom_customize_update' );
  161. /**
  162. * Sanitize the checkbox.
  163. *
  164. * @param boolean $input.
  165. *
  166. * @return boolean true if is 1 or '1', false if anything else
  167. */
  168. function seedlet_sanitize_checkbox( $input ) {
  169. if ( 1 == $input ) {
  170. return true;
  171. } else {
  172. return false;
  173. }
  174. }
  175. /**
  176. * Bind JS handlers to instantly live-preview changes.
  177. */
  178. function seedlet_wpcom_customize_preview_js() {
  179. wp_enqueue_script( 'seedlet_wpcom_customize_preview', get_theme_file_uri( '/inc/wpcom-customize-preview.js' ), array( 'customize-preview' ), '1.1', true );
  180. }
  181. add_action( 'customize_preview_init', 'seedlet_wpcom_customize_preview_js' );
  182. /**
  183. * Enqueue our WP.com styles for front-end.
  184. * Loads after style.css so we can add overrides.
  185. */
  186. function seedlet_wpcom_scripts() {
  187. wp_enqueue_style( 'seedlet-wpcom-style', get_template_directory_uri() . '/inc/wpcom-style.css', array( 'seedlet-style' ), '20200629' );
  188. }
  189. add_action( 'wp_enqueue_scripts', 'seedlet_wpcom_scripts' );
  190. /**
  191. * Adds custom classes to the array of body classes.
  192. *
  193. * @param array $classes Classes for the body element.
  194. * @return array
  195. */
  196. function seedlet_wpcom_body_classes( $classes ) {
  197. $hide = get_theme_mod( 'hide_front_page_title', false );
  198. if ( true === $hide ) {
  199. $classes[] = 'hide-homepage-title';
  200. }
  201. $credit_option = get_option( 'footercredit' );
  202. if ( 'hidden' == $credit_option ) {
  203. $classes[] = 'hide-footer-credit';
  204. }
  205. return $classes;
  206. }
  207. add_filter( 'body_class', 'seedlet_wpcom_body_classes' );
  208. /**
  209. * Adds custom classes to the admin body classes.
  210. *
  211. * @param string $classes Classes for the body element.
  212. * @return string
  213. */
  214. function seedlet_wpcom_admin_body_classes( $classes ) {
  215. global $post;
  216. $is_block_editor_screen = ( function_exists( 'get_current_screen' ) && get_current_screen() && get_current_screen()->is_block_editor() );
  217. $hide = get_theme_mod( 'hide_front_page_title', false );
  218. $front_page = (int) get_option( 'page_on_front' );
  219. if ( $is_block_editor_screen && $front_page === $post->ID && true === $hide ) {
  220. $classes .= ' hide-homepage-title';
  221. }
  222. return $classes;
  223. }
  224. add_filter( 'admin_body_class', 'seedlet_wpcom_admin_body_classes' );
  225. /**
  226. * Enqueue our WP.com styles for the block editor.
  227. */
  228. function seedlet_wpcom_editor_scripts() {
  229. wp_enqueue_style( 'seedlet-wpcom-editor-style', get_template_directory_uri() . '/inc/wpcom-style-editor.css', array(), '20200629' );
  230. }
  231. add_action( 'enqueue_block_editor_assets', 'seedlet_wpcom_editor_scripts' );
  232. /**
  233. * Enqueue CSS for customizer pane.
  234. */
  235. function seedlet_enqueue_message_scripts() {
  236. wp_enqueue_style( 'seedlet-customize-message-wpcom-style', get_template_directory_uri() . '/inc/wpcom-customize-message.css', array(), wp_get_theme()->get( 'Version' ) );
  237. }
  238. add_action( 'customize_controls_enqueue_scripts', 'seedlet_enqueue_message_scripts' );