functions.php 16 KB

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