functions.php 13 KB

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