functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * Pique functions and definitions
  4. *
  5. * @package Pique
  6. */
  7. if ( ! function_exists( 'pique_setup' ) ) :
  8. /**
  9. * Sets up theme defaults and registers support for various WordPress features.
  10. *
  11. * Note that this function is hooked into the after_setup_theme hook, which
  12. * runs before the init hook. The init hook is too late for some features, such
  13. * as indicating support for post thumbnails.
  14. */
  15. function pique_setup() {
  16. /*
  17. * Make theme available for translation.
  18. * Translations can be filed in the /languages/ directory.
  19. * If you're building a theme based on Pique, use a find and replace
  20. * to change 'pique' to the name of your theme in all the template files
  21. */
  22. load_theme_textdomain( 'pique', get_template_directory() . '/languages' );
  23. // Add default posts and comments RSS feed links to head.
  24. add_theme_support( 'automatic-feed-links' );
  25. /*
  26. * Let WordPress manage the document title.
  27. * By adding theme support, we declare that this theme does not use a
  28. * hard-coded <title> tag in the document head, and expect WordPress to
  29. * provide it for us.
  30. */
  31. add_theme_support( 'title-tag' );
  32. /*
  33. * Enable support for Post Thumbnails on posts and pages.
  34. *
  35. * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
  36. */
  37. add_theme_support( 'post-thumbnails' );
  38. add_image_size( 'pique-hero', 1400, 1000, true );
  39. add_image_size( 'pique-square', 280, 280, true );
  40. add_image_size( 'pique-header', 1400, 400, true );
  41. add_image_size( 'pique-thumbnail-avatar', 100, 100, true );
  42. // This theme uses wp_nav_menu() in three locations.
  43. register_nav_menus( array(
  44. 'primary' => esc_html__( 'Primary Menu', 'pique' ),
  45. 'secondary' => esc_html__( 'Secondary Menu', 'pique' ),
  46. ) );
  47. /*
  48. * Switch default core markup for search form, comment form, and comments
  49. * to output valid HTML5.
  50. */
  51. add_theme_support( 'html5', array(
  52. 'search-form',
  53. 'comment-form',
  54. 'comment-list',
  55. 'gallery',
  56. 'caption',
  57. ) );
  58. /*
  59. * Enable support for Post Formats.
  60. * See http://codex.wordpress.org/Post_Formats
  61. */
  62. add_theme_support( 'post-formats', array(
  63. 'aside',
  64. 'chat',
  65. 'gallery',
  66. 'image',
  67. 'video',
  68. 'quote',
  69. 'link',
  70. 'status',
  71. 'audio',
  72. ) );
  73. // Set up the WordPress core custom background feature.
  74. add_theme_support( 'custom-background', apply_filters( 'pique_custom_background_args', array(
  75. 'default-color' => 'ffffff',
  76. 'default-image' => '',
  77. ) ) );
  78. // Add support for responsive embeds.
  79. add_theme_support( 'responsive-embeds' );
  80. // Add support for full and wide align images.
  81. add_theme_support( 'align-wide' );
  82. // Add support for custom color scheme.
  83. add_theme_support( 'editor-color-palette', array(
  84. array(
  85. 'name' => esc_html__( 'Dark Blue', 'pique' ),
  86. 'slug' => 'dark-blue',
  87. 'color' => '#293940',
  88. ),
  89. array(
  90. 'name' => esc_html__( 'Medium Blue', 'pique' ),
  91. 'slug' => 'medium-blue',
  92. 'color' => '#3c7993',
  93. ),
  94. array(
  95. 'name' => esc_html__( 'Light Blue', 'pique' ),
  96. 'slug' => 'light-blue',
  97. 'color' => '#83b6cc',
  98. ),
  99. array(
  100. 'name' => esc_html__( 'Dark Brown', 'pique' ),
  101. 'slug' => 'dark-brown',
  102. 'color' => '#2d2a26',
  103. ),
  104. array(
  105. 'name' => esc_html__( 'Dark Gray', 'pique' ),
  106. 'slug' => 'dark-gray',
  107. 'color' => '#5d5d5d',
  108. ),
  109. array(
  110. 'name' => esc_html__( 'Medium Gray', 'pique' ),
  111. 'slug' => 'medium-gray',
  112. 'color' => '#a9a9a9',
  113. ),
  114. array(
  115. 'name' => esc_html__( 'White', 'pique' ),
  116. 'slug' => 'white',
  117. 'color' => '#fff',
  118. ),
  119. ) );
  120. }
  121. endif; // pique_setup
  122. add_action( 'after_setup_theme', 'pique_setup' );
  123. /**
  124. * Set the content width in pixels, based on the theme's design and stylesheet.
  125. *
  126. * Priority 0 to make it available to lower priority callbacks.
  127. *
  128. * @global int $content_width
  129. */
  130. function pique_content_width() {
  131. $GLOBALS['content_width'] = apply_filters( 'pique_content_width', 775 );
  132. }
  133. add_action( 'after_setup_theme', 'pique_content_width', 0 );
  134. /**
  135. * Use a larger content width for full-width pages.
  136. */
  137. if ( ! function_exists( 'pique_content_width_tweak' ) ) :
  138. function pique_content_width_tweak() {
  139. if ( is_page_template( 'page-templates/template-full-width.php' ) ) :
  140. global $content_width;
  141. $content_width = 1400; /* pixels */
  142. endif;
  143. }
  144. endif;
  145. add_action( 'template_redirect', 'pique_content_width_tweak' );
  146. /**
  147. * Register widget area.
  148. *
  149. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  150. */
  151. function pique_widgets_init() {
  152. register_sidebar( array(
  153. 'name' => esc_html__( 'Sidebar', 'pique' ),
  154. 'id' => 'sidebar-1',
  155. 'description' => esc_html__( 'Add widgets here to appear in your sidebar', 'pique' ),
  156. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  157. 'after_widget' => '</aside>',
  158. 'before_title' => '<h2 class="widget-title">',
  159. 'after_title' => '</h2>',
  160. ) );
  161. register_sidebar( array(
  162. 'name' => esc_html__( 'First Footer Widget Area', 'pique' ),
  163. 'id' => 'sidebar-2',
  164. 'description' => esc_html__( 'Add widgets here to appear in your footer', 'pique' ),
  165. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  166. 'after_widget' => '</aside>',
  167. 'before_title' => '<h2 class="widget-title">',
  168. 'after_title' => '</h2>',
  169. ) );
  170. register_sidebar( array(
  171. 'name' => esc_html__( 'Second Footer Widget Area', 'pique' ),
  172. 'id' => 'sidebar-3',
  173. 'description' => esc_html__( 'Add widgets here to appear in your footer', 'pique' ),
  174. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  175. 'after_widget' => '</aside>',
  176. 'before_title' => '<h2 class="widget-title">',
  177. 'after_title' => '</h2>',
  178. ) );
  179. register_sidebar( array(
  180. 'name' => esc_html__( 'Third Footer Widget Area', 'pique' ),
  181. 'id' => 'sidebar-4',
  182. 'description' => esc_html__( 'Add widgets here to appear in your footer', 'pique' ),
  183. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  184. 'after_widget' => '</aside>',
  185. 'before_title' => '<h2 class="widget-title">',
  186. 'after_title' => '</h2>',
  187. ) );
  188. }
  189. add_action( 'widgets_init', 'pique_widgets_init' );
  190. /**
  191. * Register Google Fonts
  192. */
  193. function pique_fonts_url() {
  194. $fonts_url = '';
  195. /* Translators: If there are characters in your language that are not
  196. * supported by Lora, translate this to 'off'. Do not translate
  197. * into your own language.
  198. */
  199. $lora = esc_html_x( 'on', 'Lora font: on or off', 'pique' );
  200. /* Translators: If there are characters in your language that are not
  201. * supported by Karla, translate this to 'off'. Do not translate
  202. * into your own language.
  203. */
  204. $karla = esc_html_x( 'on', 'Karla font: on or off', 'pique' );
  205. if ( 'off' !== $lora || 'off' !== $karla ) :
  206. $font_families = array();
  207. if ( 'off' !== $lora ) {
  208. $font_families[] = 'Lora:400,700,400italic,700italic';
  209. }
  210. if ( 'off' !== $karla ) {
  211. $font_families[] = 'Karla:400,700,400italic,700italic';
  212. }
  213. $query_args = array(
  214. 'family' => urlencode( implode( '|', $font_families ) ),
  215. 'subset' => urlencode( 'latin,latin-ext' ),
  216. );
  217. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  218. endif;
  219. return $fonts_url;
  220. }
  221. /**
  222. * Enqueue Google Fonts for custom headers
  223. */
  224. function pique_admin_scripts() {
  225. wp_enqueue_style( 'pique-fonts', pique_fonts_url(), array(), null );
  226. }
  227. add_action( 'admin_print_styles-appearance_page_custom-header', 'pique_admin_scripts' );
  228. /**
  229. * Enqueue scripts and styles.
  230. */
  231. function pique_scripts() {
  232. wp_enqueue_style( 'pique-style', get_stylesheet_uri(), array(), null, 'screen' );
  233. wp_enqueue_style( 'pique-fonts', pique_fonts_url(), array(), null );
  234. // Block stylesheets
  235. wp_enqueue_style( 'pique-block-style', get_template_directory_uri() . '/assets/css/blocks.css', array( 'pique-style' ), '20181018' );
  236. // Background fix for iOS
  237. if ( is_front_page() || is_home() ) :
  238. wp_enqueue_script( 'pique-background-fix', get_template_directory_uri() . '/assets/js/background-fix.js', array( 'jquery' ), '20170302', true );
  239. endif;
  240. // Header and navigation
  241. wp_enqueue_script( 'waypoints', get_template_directory_uri() . '/assets/js/jquery.waypoints.min.js', array( 'jquery' ), '20150813', true );
  242. wp_enqueue_script( 'pique-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array( 'jquery' ), '20120206', true );
  243. wp_enqueue_script( 'pique-skip-link-focus-fix', get_template_directory_uri() . '/assets/js/skip-link-focus-fix.js', array(), '20130115', true );
  244. wp_enqueue_script( 'pique-header', get_template_directory_uri() . '/assets/js/header.js', array( 'jquery', 'waypoints' ), '20151030', true );
  245. // Scroll effects (only loaded on front page)
  246. if ( pique_is_frontpage() ) :
  247. wp_enqueue_script( 'scrollTo', get_template_directory_uri() . '/assets/js/jquery.scrollTo.min.js', array( 'jquery' ), '20151030', true );
  248. wp_enqueue_script( 'pique-front-page', get_template_directory_uri() . '/assets/js/front-page.js', array( 'scrollTo', 'waypoints' ), '20151030', true );
  249. endif;
  250. // Font icons, because we're retro like that
  251. wp_enqueue_style( 'fontawesome', get_template_directory_uri() . '/fonts/font-awesome.min.css', array(), null );
  252. if ( wp_style_is( 'genericons', 'registered' ) ) {
  253. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), null );
  254. } else {
  255. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/fonts/genericons.css', array(), null );
  256. }
  257. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  258. wp_enqueue_script( 'comment-reply' );
  259. }
  260. }
  261. add_action( 'wp_enqueue_scripts', 'pique_scripts' );
  262. /**
  263. * Enqueue editor styles for Gutenberg
  264. */
  265. function pique_block_editor_styles() {
  266. // Block styles.
  267. wp_enqueue_style( 'pique-block-editor-style', get_template_directory_uri() . '/assets/css/editor-blocks.css' );
  268. // Font styles.
  269. wp_enqueue_style( 'pique-fonts', pique_fonts_url(), array(), null );
  270. }
  271. add_action( 'enqueue_block_editor_assets', 'pique_block_editor_styles' );
  272. /**
  273. * Filter the front page template so it's bypassed entirely if the user selects
  274. * to display blog posts on their homepage instead of a static page.
  275. */
  276. function pique_filter_front_page_template( $template ) {
  277. return is_home() ? '' : $template;
  278. }
  279. add_filter( 'frontpage_template', 'pique_filter_front_page_template' );
  280. function pique_query_vars( $qvars ) {
  281. $qvars[] = 'pique_panel';
  282. return $qvars;
  283. }
  284. add_filter( 'query_vars', 'pique_query_vars' , 10, 1 );
  285. /**
  286. * Get random posts; a simple, more efficient approach.
  287. * MySQL queries that use ORDER BY RAND() can be pretty challenging and slow on large datasets.
  288. * Also it works better with heavy caching.
  289. */
  290. function pique_get_random_posts( $number = 1, $post_type = 'post' ) {
  291. $query = new WP_Query( array(
  292. 'posts_per_page' => 100,
  293. 'fields' => 'ids',
  294. 'post_type' => $post_type
  295. ) );
  296. $post_ids = $query->posts;
  297. shuffle( $post_ids );
  298. $post_ids = array_splice( $post_ids, 0, $number );
  299. $random_posts = get_posts( array(
  300. 'post__in' => $post_ids,
  301. 'numberposts' => count( $post_ids ),
  302. 'post_type' => $post_type
  303. ) );
  304. return $random_posts;
  305. }
  306. /**
  307. * Implement the Custom Header feature.
  308. */
  309. require get_template_directory() . '/inc/custom-header.php';
  310. /**
  311. * Custom template tags for this theme.
  312. */
  313. require get_template_directory() . '/inc/template-tags.php';
  314. /**
  315. * Custom functions that act independently of the theme templates.
  316. */
  317. require get_template_directory() . '/inc/extras.php';
  318. /**
  319. * Customizer additions.
  320. */
  321. require get_template_directory() . '/inc/customizer.php';
  322. /**
  323. * Load Jetpack compatibility file.
  324. */
  325. require get_template_directory() . '/inc/jetpack.php';
  326. /**
  327. * Load WooCommerce compatibility file.
  328. */
  329. if ( class_exists( 'WooCommerce' ) ) {
  330. require get_template_directory() . '/inc/woocommerce.php';
  331. }