functions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. /**
  3. * Varia functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package WordPress
  8. * @subpackage Varia
  9. * @since 1.0.0
  10. */
  11. if ( ! function_exists( 'varia_default_colors' ) ) {
  12. function varia_default_colors() {
  13. return array(
  14. 'background' => '#FFFFFF', //bg
  15. 'foreground' => '#444444', //txt
  16. 'primary' => '#0000ff', //link
  17. 'secondary' => '#ff0000', //fg1
  18. 'tertiary' => null, //fg2
  19. );
  20. }
  21. }
  22. /**
  23. * Varia only works in WordPress 4.7 or later.
  24. */
  25. if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
  26. require get_template_directory() . '/inc/back-compat.php';
  27. return;
  28. }
  29. if ( ! function_exists( 'varia_setup' ) ) :
  30. /**
  31. * Sets up theme defaults and registers support for various WordPress features.
  32. *
  33. * Note that this function is hooked into the after_setup_theme hook, which
  34. * runs before the init hook. The init hook is too late for some features, such
  35. * as indicating support for post thumbnails.
  36. */
  37. function varia_setup() {
  38. /*
  39. * Make theme available for translation.
  40. * Translations can be filed in the /languages/ directory.
  41. * If you're building a theme based on Varia, use a find and replace
  42. * to change 'varia' to the name of your theme in all the template files.
  43. */
  44. load_theme_textdomain( 'varia', get_template_directory() . '/languages' );
  45. // Add default posts and comments RSS feed links to head.
  46. add_theme_support( 'automatic-feed-links' );
  47. // Enable appearance tools for Block Editor.
  48. add_theme_support( 'appearance-tools' );
  49. /*
  50. * Let WordPress manage the document title.
  51. * By adding theme support, we declare that this theme does not use a
  52. * hard-coded <title> tag in the document head, and expect WordPress to
  53. * provide it for us.
  54. */
  55. add_theme_support( 'title-tag' );
  56. /*
  57. * Enable support for Post Thumbnails on posts and pages.
  58. *
  59. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  60. */
  61. add_theme_support( 'post-thumbnails' );
  62. set_post_thumbnail_size( 1568, 9999 );
  63. // This theme uses wp_nav_menu() in two locations.
  64. register_nav_menus(
  65. array(
  66. 'menu-1' => __( 'Primary', 'varia' ),
  67. 'menu-2' => __( 'Footer Menu', 'varia' ),
  68. 'social' => __( 'Social Links Menu', 'varia' ),
  69. )
  70. );
  71. /*
  72. * Switch default core markup for search form, comment form, and comments
  73. * to output valid HTML5.
  74. */
  75. add_theme_support(
  76. 'html5',
  77. array(
  78. 'search-form',
  79. 'comment-form',
  80. 'comment-list',
  81. 'gallery',
  82. 'caption',
  83. )
  84. );
  85. /**
  86. * Add support for core custom logo.
  87. *
  88. * @link https://codex.wordpress.org/Theme_Logo
  89. */
  90. add_theme_support(
  91. 'custom-logo',
  92. array(
  93. 'height' => 96,
  94. 'width' => 100,
  95. 'flex-width' => true,
  96. 'flex-height' => true,
  97. 'header-text' => array( 'site-title', 'site-description' ),
  98. )
  99. );
  100. // Add theme support for selective refresh for widgets.
  101. add_theme_support( 'customize-selective-refresh-widgets' );
  102. // Add support for Block Styles.
  103. add_theme_support( 'wp-block-styles' );
  104. // Add support for full and wide align images.
  105. add_theme_support( 'align-wide' );
  106. // Add support for editor styles.
  107. add_theme_support( 'editor-styles' );
  108. // Enqueue editor styles.
  109. add_editor_style( 'style-editor.css' );
  110. // add_editor_style( 'child-theme-alt-style.css' ); // this will get loaded in a child theme
  111. // Add custom editor font sizes.
  112. add_theme_support(
  113. 'editor-font-sizes',
  114. array(
  115. array(
  116. 'name' => __( 'Small', 'varia' ),
  117. 'shortName' => __( 'S', 'varia' ),
  118. 'size' => 15,
  119. 'slug' => 'small',
  120. ),
  121. array(
  122. 'name' => __( 'Normal', 'varia' ),
  123. 'shortName' => __( 'M', 'varia' ),
  124. 'size' => 18,
  125. 'slug' => 'normal',
  126. ),
  127. array(
  128. 'name' => __( 'Large', 'varia' ),
  129. 'shortName' => __( 'L', 'varia' ),
  130. 'size' => 25.92,
  131. 'slug' => 'large',
  132. ),
  133. array(
  134. 'name' => __( 'Huge', 'varia' ),
  135. 'shortName' => __( 'XL', 'varia' ),
  136. 'size' => 31.105,
  137. 'slug' => 'huge',
  138. ),
  139. )
  140. );
  141. /*
  142. * Get customizer colors and add them to the editor color palettes
  143. *
  144. * - if the customizer color is empty, use the default
  145. */
  146. $colors_array = get_theme_mod( 'colors_manager' ); // color annotations array()
  147. $default_colors = varia_default_colors();
  148. $primary = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['link'] : $default_colors['primary']; // $config-global--color-primary-default;
  149. $secondary = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['fg1'] : $default_colors['secondary']; // $config-global--color-secondary-default;
  150. $tertiary = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['fg2'] : $default_colors['tertiary']; // $config-global--color-tertiary-default;
  151. $foreground = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['txt'] : $default_colors['foreground']; // $config-global--color-foreground-default;
  152. $background = is_array( $colors_array ) && array_key_exists( 'colors', $colors_array ) ? $colors_array['colors']['bg'] : $default_colors['background']; // $config-global--color-background-default;
  153. $editor_colors_array = array(
  154. array(
  155. 'name' => __( 'Primary', 'varia' ),
  156. 'slug' => 'primary',
  157. 'color' => $primary,
  158. ),
  159. array(
  160. 'name' => __( 'Secondary', 'varia' ),
  161. 'slug' => 'secondary',
  162. 'color' => $secondary,
  163. ),
  164. array(
  165. 'name' => __( 'Foreground', 'varia' ),
  166. 'slug' => 'foreground',
  167. 'color' => $foreground,
  168. ),
  169. array(
  170. 'name' => __( 'Background', 'varia' ),
  171. 'slug' => 'background',
  172. 'color' => $background,
  173. ),
  174. );
  175. if ( $tertiary ) {
  176. $editor_colors_array[] = array(
  177. 'name' => __( 'Tertiary', 'varia' ),
  178. 'slug' => 'tertiary',
  179. 'color' => $tertiary,
  180. );
  181. }
  182. $editor_colors_array = apply_filters( 'varia_editor_color_palette', $editor_colors_array );
  183. // Editor color palette.
  184. add_theme_support( 'editor-color-palette', $editor_colors_array );
  185. // Add support for responsive embedded content.
  186. add_theme_support( 'responsive-embeds' );
  187. // Add support for custom line height controls.
  188. add_theme_support( 'custom-line-height' );
  189. // Add support for experimental cover block spacing.
  190. add_theme_support( 'custom-spacing' );
  191. // Add support for custom units.
  192. add_theme_support( 'custom-units' );
  193. // Add support for Global Styles.
  194. add_theme_support(
  195. 'jetpack-global-styles',
  196. array(
  197. 'enable_theme_default' => true,
  198. )
  199. );
  200. // Add support for Content Options.
  201. add_theme_support( 'jetpack-content-options', array(
  202. 'blog-display' => 'content',
  203. 'featured-images' => array(
  204. 'archive' => true,
  205. 'archive-default' => true,
  206. 'post' => true,
  207. 'page' => true,
  208. ),
  209. ) );
  210. }
  211. endif;
  212. add_action( 'after_setup_theme', 'varia_setup' );
  213. /**
  214. * Register widget area.
  215. *
  216. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  217. */
  218. function varia_widgets_init() {
  219. register_sidebar(
  220. array(
  221. 'name' => __( 'Footer', 'varia' ),
  222. 'id' => 'sidebar-1',
  223. 'description' => __( 'Add widgets here to appear in your footer.', 'varia' ),
  224. 'before_widget' => '<section id="%1$s" class="widget %2$s">',
  225. 'after_widget' => '</section>',
  226. 'before_title' => '<h2 class="widget-title">',
  227. 'after_title' => '</h2>',
  228. )
  229. );
  230. }
  231. add_action( 'widgets_init', 'varia_widgets_init' );
  232. /**
  233. * Set the content width in pixels, based on the theme's design and stylesheet.
  234. *
  235. * Priority 0 to make it available to lower priority callbacks.
  236. *
  237. * @global int $content_width Content width.
  238. */
  239. function varia_content_width() {
  240. // This variable is intended to be overruled from themes.
  241. // Open WPCS issue: {@link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/issues/1043}.
  242. // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
  243. $GLOBALS['content_width'] = apply_filters( 'varia_content_width', 750 );
  244. }
  245. add_action( 'after_setup_theme', 'varia_content_width', 0 );
  246. /**
  247. * Enqueue scripts and styles.
  248. */
  249. function varia_scripts() {
  250. // Theme styles
  251. wp_enqueue_style( 'varia-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  252. // RTL styles
  253. wp_style_add_data( 'varia-style', 'rtl', 'replace' );
  254. // Print styles
  255. wp_enqueue_style( 'varia-print-style', get_template_directory_uri() . '/print.css', array(), wp_get_theme()->get( 'Version' ), 'print' );
  256. // Threaded comment reply styles
  257. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  258. wp_enqueue_script( 'comment-reply' );
  259. }
  260. // Note, the is_IE global variable is defined by WordPress and is used
  261. // to detect if the current browser is internet explorer.
  262. global $is_IE;
  263. if ( $is_IE ) {
  264. // If IE 11 or below, use a ponyfill to add CSS Variable support
  265. wp_register_script( 'css-vars-ponyfill', get_template_directory_uri() . '/js/css-vars-ponyfill2.js' );
  266. wp_enqueue_script(
  267. 'ie11-fix',
  268. get_template_directory_uri() . '/js/ie11-fix.js',
  269. array( 'css-vars-ponyfill' ),
  270. wp_get_theme()->get( 'Version' )
  271. );
  272. wp_enqueue_style( 'varia-ie-styles', get_template_directory_uri() . '/ie.css', array(), wp_get_theme()->get( 'Version' ) );
  273. }
  274. }
  275. add_action( 'wp_enqueue_scripts', 'varia_scripts' );
  276. function varia_add_mobile_nav_on_side_scripts() {
  277. if ( get_theme_mod( 'enable_side_menu' ) !== 1 ) {
  278. return;
  279. }
  280. // Main navigation scripts
  281. wp_enqueue_script( 'varia-primary-navigation-script', get_template_directory_uri() . '/js/primary-navigation.js', array(), wp_get_theme()->get( 'Version' ), true );
  282. }
  283. /**
  284. * Fix skip link focus in IE11.
  285. *
  286. * This does not enqueue the script because it is tiny and because it is only for IE11,
  287. * thus it does not warrant having an entire dedicated blocking script being loaded.
  288. *
  289. * @link https://git.io/vWdr2
  290. */
  291. function varia_skip_link_focus_fix() {
  292. // Prevent outputting skip-link-focus-fix in AMP since the AMP framework has it built-in,
  293. // per <https://github.com/ampproject/amphtml/issues/18671>.
  294. if ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) {
  295. return;
  296. }
  297. // The following is minified via `terser --compress --mangle -- js/skip-link-focus-fix.js`.
  298. ?>
  299. <script>
  300. /(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);
  301. </script>
  302. <?php
  303. }
  304. add_action( 'wp_print_footer_scripts', 'varia_skip_link_focus_fix' );
  305. /**
  306. * Enqueue block editor content-width styles.
  307. * - These need to be enqueued separately to include the selectors
  308. * that live outside of `.editor-styles-wrapper`
  309. *
  310. */
  311. function varia_editor_content_width() {
  312. wp_enqueue_style( 'varia-editor-content-width-style', get_theme_file_uri( '/style-editor-content-width.css' ), false, wp_get_theme()->get( 'Version' ), 'all' );
  313. }
  314. add_action( 'enqueue_block_editor_assets', 'varia_editor_content_width' );
  315. // This makes it possible to define the function in earlier to alter in one way or another.
  316. if ( ! function_exists( 'varia_mobile_nav_on_side' ) ) {
  317. function varia_mobile_nav_on_side( $classes ) {
  318. if ( get_theme_mod( 'enable_side_menu' ) === 1 ) {
  319. return array_merge( $classes, array( 'mobile-nav-side' ) );
  320. }
  321. return $classes;
  322. }
  323. }
  324. if ( ! function_exists( 'varia_enable_mobile_nav_on_side' ) ) {
  325. function varia_enable_mobile_nav_on_side() {
  326. set_theme_mod( 'enable_side_menu', 1 );
  327. }
  328. }
  329. if ( ! function_exists( 'varia_register_mobile_nav_on_side_customizer_control' ) ) {
  330. function varia_register_mobile_nav_on_side_customizer_control( $wp_customize ) {
  331. $wp_customize->add_setting(
  332. 'enable_side_menu',
  333. array(
  334. 'default' => 1,
  335. 'sanitize_callback' => 'absint',
  336. )
  337. );
  338. $wp_customize->add_section(
  339. 'nav_menus_mobile',
  340. array(
  341. 'title' => __( 'Mobile Settings', 'varia' ),
  342. 'panel' => 'nav_menus',
  343. )
  344. );
  345. $wp_customize->add_control(
  346. 'enable_side_menu',
  347. array(
  348. 'label' => __( 'Display mobile menu on the side', 'varia' ),
  349. 'section' => 'nav_menus_mobile',
  350. 'settings' => 'enable_side_menu',
  351. 'type' => 'checkbox',
  352. )
  353. );
  354. }
  355. }
  356. function varia_mobile_nav_on_side_setup() {
  357. // Add .mobile-nav-side body class.
  358. add_filter( 'body_class', 'varia_mobile_nav_on_side' );
  359. // Enable the mobile nav on side on theme switch.
  360. add_action( 'after_switch_theme', 'varia_enable_mobile_nav_on_side' );
  361. // Enable the customizer control toggle for the mobile nav on the side.
  362. add_action( 'customize_register', 'varia_register_mobile_nav_on_side_customizer_control' );
  363. // Adds the script that help toggle the mobile nav.
  364. add_action( 'wp_enqueue_scripts', 'varia_add_mobile_nav_on_side_scripts' );
  365. }
  366. /**
  367. * Sanitize the checkbox.
  368. *
  369. * @param boolean $input.
  370. *
  371. * @return boolean true if is 1 or '1', false if anything else
  372. */
  373. function varia_sanitize_checkbox( $input ) {
  374. if ( 1 == $input ) {
  375. return true;
  376. } else {
  377. return false;
  378. }
  379. }
  380. /**
  381. * Add ability to show or hide header and footer elements on the homepage.
  382. */
  383. function varia_customize_header_footer( $wp_customize ) {
  384. // Add setting to hide the site header on the homepage.
  385. $wp_customize->add_setting(
  386. 'hide_site_header',
  387. array(
  388. 'default' => false,
  389. 'type' => 'theme_mod',
  390. 'transport' => 'refresh',
  391. 'sanitize_callback' => 'varia_sanitize_checkbox',
  392. )
  393. );
  394. // Add control to hide the site header on the homepage.
  395. $wp_customize->add_control(
  396. 'hide_site_header',
  397. array(
  398. 'label' => esc_html__( 'Hide the Site Header', 'varia' ),
  399. 'description' => esc_html__( 'Check to hide the site header, if your homepage is set to display a static page.', 'varia' ),
  400. 'section' => 'static_front_page',
  401. 'priority' => 10,
  402. 'type' => 'checkbox',
  403. 'settings' => 'hide_site_header',
  404. )
  405. );
  406. // Add setting to hide footer elements on the homepage.
  407. $wp_customize->add_setting(
  408. 'hide_site_footer',
  409. array(
  410. 'default' => false,
  411. 'type' => 'theme_mod',
  412. 'transport' => 'refresh',
  413. 'sanitize_callback' => 'varia_sanitize_checkbox',
  414. )
  415. );
  416. // Add control to hide footer elements on the homepage.
  417. $wp_customize->add_control(
  418. 'hide_site_footer',
  419. array(
  420. 'label' => esc_html__( 'Hide the Site Footer Menu & Widgets', 'varia' ),
  421. 'description' => esc_html__( 'Check to hide the site menu & widgets in the footer, if your homepage is set to display a static page.', 'varia' ),
  422. 'section' => 'static_front_page',
  423. 'priority' => 10,
  424. 'type' => 'checkbox',
  425. 'settings' => 'hide_site_footer',
  426. )
  427. );
  428. }
  429. add_action( 'customize_register', 'varia_customize_header_footer' );
  430. /**
  431. * SVG Icons class.
  432. */
  433. require get_template_directory() . '/classes/class-varia-svg-icons.php';
  434. /**
  435. * Enhance the theme by hooking into WordPress.
  436. */
  437. require get_template_directory() . '/inc/template-functions.php';
  438. /**
  439. * SVG Icons related functions.
  440. */
  441. require get_template_directory() . '/inc/icon-functions.php';
  442. /**
  443. * Custom template tags for the theme.
  444. */
  445. require get_template_directory() . '/inc/template-tags.php';
  446. /**
  447. * Load WooCommerce compatibility file.
  448. */
  449. if ( class_exists( 'WooCommerce' ) ) {
  450. require get_template_directory() . '/inc/woocommerce.php';
  451. }