functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. <?php
  2. /**
  3. * toujours functions and definitions.
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Toujours
  8. */
  9. if ( ! function_exists( 'toujours_setup' ) ) :
  10. /**
  11. * Sets up theme defaults and registers support for various WordPress features.
  12. *
  13. * Note that this function is hooked into the after_setup_theme hook, which
  14. * runs before the init hook. The init hook is too late for some features, such
  15. * as indicating support for post thumbnails.
  16. */
  17. function toujours_setup() {
  18. /*
  19. * Make theme available for translation.
  20. * Translations can be filed in the /languages/ directory.
  21. * If you're building a theme based on toujours, use a find and replace
  22. * to change 'toujours' to the name of your theme in all the template files.
  23. */
  24. load_theme_textdomain( 'toujours', get_template_directory() . '/languages' );
  25. // Add default posts and comments RSS feed links to head.
  26. add_theme_support( 'automatic-feed-links' );
  27. /*
  28. * Let WordPress manage the document title.
  29. * By adding theme support, we declare that this theme does not use a
  30. * hard-coded <title> tag in the document head, and expect WordPress to
  31. * provide it for us.
  32. */
  33. add_theme_support( 'title-tag' );
  34. // Add support for responsive embeds.
  35. add_theme_support( 'responsive-embeds' );
  36. // Add custom colors to Gutenberg
  37. add_theme_support(
  38. 'editor-color-palette',
  39. array(
  40. array(
  41. 'name' => esc_html__( 'Blue', 'toujours' ),
  42. 'slug' => 'blue',
  43. 'color' => '#2590ec',
  44. ),
  45. array(
  46. 'name' => esc_html__( 'Dark Gray', 'toujours' ),
  47. 'slug' => 'dark-gray',
  48. 'color' => '#404040',
  49. ),
  50. array(
  51. 'name' => esc_html__( 'Medium Gray', 'toujours' ),
  52. 'slug' => 'medium-gray',
  53. 'color' => '#666',
  54. ),
  55. array(
  56. 'name' => esc_html__( 'Light Gray', 'toujours' ),
  57. 'slug' => 'light-gray',
  58. 'color' => '#eee',
  59. ),
  60. array(
  61. 'name' => esc_html__( 'White', 'toujours' ),
  62. 'slug' => 'white',
  63. 'color' => '#fff',
  64. ),
  65. )
  66. );
  67. /*
  68. * Editor styles
  69. */
  70. add_editor_style( array( get_template_directory_uri() . '/editor-style.css', toujours_fonts_url() ) );
  71. /*
  72. * Enable support for Post Thumbnails on posts and pages.
  73. *
  74. * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
  75. */
  76. add_theme_support( 'post-thumbnails' );
  77. /*
  78. * Custom image sizes
  79. */
  80. add_image_size( 'toujours-banner', 1160, 600, true );
  81. add_image_size( 'toujours-featured', 940, 9999 );
  82. add_image_size( 'toujours-trio', 680, 383, true ); /* image size larger for mobile */
  83. /*
  84. * Register menu locations
  85. *
  86. * Include check to see if jetpack_social_menu supported
  87. */
  88. if ( ! function_exists( 'jetpack_social_menu' ) ) {
  89. register_nav_menus(
  90. array(
  91. 'primary' => esc_html__( 'Primary Menu', 'toujours' ),
  92. 'social' => esc_html__( 'Social Menu', 'toujours' ),
  93. )
  94. );
  95. } else {
  96. register_nav_menus(
  97. array(
  98. 'primary' => esc_html__( 'Primary Menu', 'toujours' ),
  99. )
  100. );
  101. }
  102. /*
  103. * Switch default core markup for search form, comment form, and comments
  104. * to output valid HTML5.
  105. */
  106. add_theme_support(
  107. 'html5',
  108. array(
  109. 'search-form',
  110. 'comment-form',
  111. 'comment-list',
  112. 'gallery',
  113. 'caption',
  114. )
  115. );
  116. /*
  117. * Enable support for Post Formats.
  118. * See https://developer.wordpress.org/themes/functionality/post-formats/
  119. */
  120. add_theme_support(
  121. 'post-formats',
  122. array(
  123. 'aside',
  124. 'audio',
  125. 'gallery',
  126. 'image',
  127. 'video',
  128. 'quote',
  129. 'link',
  130. 'status',
  131. )
  132. );
  133. // Set up the WordPress core custom background feature.
  134. add_theme_support(
  135. 'custom-background',
  136. apply_filters(
  137. 'toujours_custom_background_args',
  138. array(
  139. 'default-color' => 'eeeeee',
  140. /*
  141. * We're using a unique filename for the background image
  142. * to avoid users uploading a file with the same filename
  143. * and avoiding conflict with the `user-background` body_class
  144. * in inc/extras.php and assets/js/customizer.js
  145. */
  146. 'default-image' => esc_url( get_template_directory_uri() ) . '/images/toujoursbackground20160105.png',
  147. )
  148. )
  149. );
  150. }
  151. endif; // toujours_setup
  152. add_action( 'after_setup_theme', 'toujours_setup' );
  153. /**
  154. * Set the content width in pixels, based on the theme's design and stylesheet.
  155. *
  156. * Priority 0 to make it available to lower priority callbacks.
  157. *
  158. * @global int $content_width
  159. */
  160. function toujours_content_width() {
  161. $GLOBALS['content_width'] = apply_filters( 'toujours_content_width', 760 );
  162. }
  163. add_action( 'after_setup_theme', 'toujours_content_width', 0 );
  164. function toujours_excerpt_length( $length ) {
  165. return 35;
  166. }
  167. add_filter( 'excerpt_length', 'toujours_excerpt_length', 999 );
  168. /**
  169. * Replaces the excerpt "more" text by a link
  170. */
  171. if ( ! function_exists( 'toujours_excerpt_continue_reading' ) ) {
  172. function toujours_excerpt_continue_reading() {
  173. return '&hellip; <a href="' . esc_url( get_permalink() ) . '">' . sprintf( esc_html__( 'More', 'toujours' ), '<span class="screen-reader-text"> "' . get_the_title() . '"</span>' ) . '</a>';
  174. }
  175. }
  176. add_filter( 'excerpt_more', 'toujours_excerpt_continue_reading' );
  177. /**
  178. *
  179. */
  180. function toujours_recent_three_posts() {
  181. // Return early if this feature has been disabled.
  182. if ( ! get_theme_mod( 'toujours_recent_posts' ) ) {
  183. return false;
  184. }
  185. if ( false === ( $latest_post_ids = get_transient( 'latest_post_ids' ) ) ) {
  186. /*
  187. Check if Featured Content option 'Display tag content in all listings' is checked
  188. If not, exclude those posts from the three recent posts as well
  189. */
  190. $exclude_tag = '';
  191. if ( toujours_has_banner_posts( 1 ) ) {
  192. $featured_options = get_option( 'featured-content' );
  193. $featured_tag_id = $featured_options['tag-id'];
  194. $featured_show = $featured_options['show-all'];
  195. if ( 0 == $featured_show ) {
  196. $exclude_tag = $featured_tag_id;
  197. }
  198. }
  199. $args = array(
  200. 'ignore_sticky_posts' => 1,
  201. 'post__not_in' => get_option( 'sticky_posts' ),
  202. 'posts_per_page' => '3',
  203. 'tag__not_in' => array( $exclude_tag ),
  204. );
  205. $latest = new WP_Query();
  206. $latest->query( $args );
  207. while ( $latest->have_posts() ) {
  208. $latest->the_post();
  209. $latest_post_ids[] = $latest->post->ID;
  210. }
  211. wp_reset_postdata();
  212. }
  213. return $latest_post_ids;
  214. }
  215. add_filter( 'toujours-recent-posts', 'toujours_recent_three_posts' );
  216. /**
  217. * Filter the home page posts, and remove any featured post IDs from it. Hooked
  218. * onto the 'pre_get_posts' action, this changes the parameters of the query
  219. * before it gets any posts.
  220. *
  221. * @global array $featured_post_id
  222. * @param WP_Query $query
  223. * @return WP_Query Possibly modified WP_query
  224. */
  225. function toujours_home_posts( $query = false ) {
  226. // Bail if not home, not a query, not main query.
  227. if ( ! is_home() || ! is_a( $query, 'WP_Query' ) || ! $query->is_main_query() ) {
  228. return $query;
  229. }
  230. $recent = apply_filters( 'toujours-recent-posts', false );
  231. // Bail if no featured posts.
  232. if ( ! $recent ) {
  233. return $query;
  234. }
  235. // Exclude featured posts from the main query.
  236. $query->query_vars['post__not_in'] = (array) $recent;
  237. return $query;
  238. }
  239. add_action( 'pre_get_posts', 'toujours_home_posts' );
  240. /**
  241. * Wrap avatars in HTML for easier styling
  242. */
  243. function toujours_get_avatar( $avatar ) {
  244. if ( ! is_admin() ) {
  245. $avatar = '<span class="avatar-container"><span class="avatar-crop">' . $avatar . '</span></span>';
  246. }
  247. return $avatar;
  248. }
  249. add_filter( 'get_avatar', 'toujours_get_avatar', 10, 5 );
  250. /**
  251. * Enqueuing Google Fonts
  252. */
  253. function toujours_fonts_url() {
  254. $fonts_url = '';
  255. /* Translators: If there are characters in your language that are not
  256. * supported by Alegreya Sans, translate this to 'off'. Do not translate
  257. * into your own language.
  258. */
  259. $alegreya_sans = esc_html_x( 'on', 'Alegreya Sans font: on or off', 'toujours' );
  260. /* Translators: If there are characters in your language that are not
  261. * supported by Open Sans, translate this to 'off'. Do not translate
  262. * into your own language.
  263. */
  264. $merriweather = esc_html_x( 'on', 'Merriweather font: on or off', 'toujours' );
  265. if ( 'off' !== $alegreya_sans || 'off' !== $merriweather ) {
  266. $font_families = array();
  267. if ( 'off' !== $alegreya_sans ) {
  268. $font_families[] = 'Alegreya Sans:300,500,300italic,500italic';
  269. }
  270. if ( 'off' !== $merriweather ) {
  271. $font_families[] = 'Merriweather:700italic,400,400italic,700,600';
  272. }
  273. /**
  274. * A filter to enable child themes to add/change/omit font families.
  275. *
  276. * @param array $font_families An array of font families to be imploded for the Google Font API
  277. */
  278. $font_families = apply_filters( 'included_google_font_families', $font_families );
  279. $query_args = array(
  280. 'family' => urlencode( implode( '|', $font_families ) ),
  281. 'subset' => urlencode( 'latin,latin-ext' ),
  282. );
  283. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  284. }
  285. return esc_url_raw( $fonts_url );
  286. }
  287. /**
  288. * Register widget area.
  289. *
  290. * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
  291. */
  292. function toujours_widgets_init() {
  293. register_sidebar(
  294. array(
  295. 'name' => esc_html__( 'Sidebar', 'toujours' ),
  296. 'id' => 'sidebar-1',
  297. 'description' => '',
  298. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  299. 'after_widget' => '</aside>',
  300. 'before_title' => '<h3 class="widget-title">',
  301. 'after_title' => '</h3>',
  302. )
  303. );
  304. register_sidebar(
  305. array(
  306. 'name' => esc_html__( 'Footer', 'toujours' ),
  307. 'id' => 'footer',
  308. 'description' => '',
  309. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  310. 'after_widget' => '</aside>',
  311. 'before_title' => '<h3 class="widget-title">',
  312. 'after_title' => '</h3>',
  313. )
  314. );
  315. }
  316. add_action( 'widgets_init', 'toujours_widgets_init' );
  317. /**
  318. * Counts the number of widgets in a specific sidebar
  319. *
  320. * @param string $id
  321. * @return integer number of widgets in the sidebar
  322. */
  323. function toujours_count_widgets( $id ) {
  324. $count = 0;
  325. $sidebars_widgets = wp_get_sidebars_widgets();
  326. if ( array_key_exists( $id, $sidebars_widgets ) ) {
  327. $count = (int) count( (array) $sidebars_widgets[ $id ] );
  328. }
  329. return $count;
  330. }
  331. /**
  332. * Widget column class helper
  333. *
  334. * @param string $id
  335. * @return string grid class
  336. */
  337. function toujours_widget_column_class( $widget_id ) {
  338. $count = toujours_count_widgets( $widget_id );
  339. $class = '';
  340. if ( $count >= 4 ) {
  341. $class .= 'widgets-four';
  342. } elseif ( 3 === $count ) {
  343. $class .= 'widgets-three';
  344. } elseif ( 2 === $count ) {
  345. $class .= 'widgets-two';
  346. } else {
  347. $class .= 'widget-one';
  348. }
  349. return $class;
  350. }
  351. /**
  352. * Return the post URL.
  353. *
  354. * @uses get_url_in_content() to get the URL in the post meta (if it exists) or
  355. * the first link found in the post content.
  356. *
  357. * Falls back to the post permalink if no URL is found in the post.
  358. *
  359. * Borrowed from Twenty Thirteen.
  360. */
  361. function toujours_get_link_url() {
  362. $content = get_the_content();
  363. $has_url = get_url_in_content( $content );
  364. return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
  365. }
  366. /**
  367. * Enqueue scripts and styles.
  368. */
  369. function toujours_scripts() {
  370. wp_enqueue_style( 'toujours-style', get_stylesheet_uri() );
  371. wp_enqueue_style( 'toujours-fonts', toujours_fonts_url(), array(), null );
  372. // Gutenberg styles
  373. wp_enqueue_style( 'toujours-blocks', get_template_directory_uri() . '/blocks.css' );
  374. wp_enqueue_script( 'flexslider', get_template_directory_uri() . '/js/jquery.flexslider.js', array( 'jquery' ), '20160104', true );
  375. wp_enqueue_script( 'imagesloaded', get_template_directory_uri() . '/js/imagesloaded.pk.js', array( 'jquery' ), '20151221', true );
  376. wp_enqueue_script( 'toujours-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
  377. wp_enqueue_script( 'toujours-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  378. wp_enqueue_script( 'toujours-global', get_template_directory_uri() . '/js/global.js', array( 'jquery', 'flexslider', 'masonry' ), '20151215', true );
  379. // Localize 'Previous' and 'Next' links in slideshow
  380. $translation_array = array(
  381. 'next_link' => esc_html__( 'Next Slide', 'toujours' ),
  382. 'previous_link' => esc_html__( 'Previous Slide', 'toujours' ),
  383. );
  384. wp_localize_script( 'toujours-global', 'toujours_script_strings', $translation_array );
  385. if ( wp_style_is( 'genericons', 'registered' ) ) {
  386. wp_enqueue_style( 'genericons' );
  387. } else {
  388. wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), null );
  389. }
  390. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  391. wp_enqueue_script( 'comment-reply' );
  392. }
  393. }
  394. add_action( 'wp_enqueue_scripts', 'toujours_scripts' );
  395. /**
  396. * Gutenberg Editor Styles
  397. */
  398. function toujours_editor_styles() {
  399. wp_enqueue_style( 'toujours-editor-block-style', get_template_directory_uri() . '/editor-blocks.css' );
  400. wp_enqueue_style( 'toujours-fonts', toujours_fonts_url(), array(), null );
  401. }
  402. add_action( 'enqueue_block_editor_assets', 'toujours_editor_styles' );
  403. /**
  404. * Implement the Custom Header feature.
  405. */
  406. require get_template_directory() . '/inc/custom-header.php';
  407. /**
  408. * Custom template tags for this theme.
  409. */
  410. require get_template_directory() . '/inc/template-tags.php';
  411. /**
  412. * Custom functions that act independently of the theme templates.
  413. */
  414. require get_template_directory() . '/inc/extras.php';
  415. /**
  416. * Customizer additions.
  417. */
  418. require get_template_directory() . '/inc/customizer.php';
  419. /**
  420. * Load Jetpack compatibility file.
  421. */
  422. require get_template_directory() . '/inc/jetpack.php';