wpcom.php 8.3 KB

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