functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * Libre 2 functions and definitions
  4. *
  5. * @package Libre 2
  6. */
  7. if ( ! function_exists( 'libre_2_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 libre_2_setup() {
  16. /*
  17. * Make theme available for translation.
  18. * Translations can be filed in the /languages/ directory.
  19. */
  20. load_theme_textdomain( 'libre-2', get_template_directory() . '/languages' );
  21. // Add default posts and comments RSS feed links to head.
  22. add_theme_support( 'automatic-feed-links' );
  23. /*
  24. * Let WordPress manage the document title.
  25. * By adding theme support, we declare that this theme does not use a
  26. * hard-coded <title> tag in the document head, and expect WordPress to
  27. * provide it for us.
  28. */
  29. add_theme_support( 'title-tag' );
  30. /* Add support for editor styles */
  31. add_editor_style( array( 'editor-style.css', libre_2_fonts_url() ) );
  32. // This theme uses wp_nav_menu() in one location.
  33. register_nav_menus( array(
  34. 'menu-1' => esc_html__( 'Header', 'libre-2' ),
  35. ) );
  36. /*
  37. * Add support for Featured Images
  38. */
  39. add_theme_support( 'post-thumbnails' );
  40. add_image_size( 'libre-2-post-thumbnail', '1088', '9999' );
  41. /*
  42. * Add support for core Custom Logos
  43. */
  44. add_theme_support( 'custom-logo', array(
  45. 'height' => 300,
  46. 'width' => 300,
  47. 'flex-width' => true,
  48. ) );
  49. // Add theme support for selective refresh for widgets.
  50. add_theme_support( 'customize-selective-refresh-widgets' );
  51. /*
  52. * Switch default core markup for search form, comment form, and comments
  53. * to output valid HTML5.
  54. */
  55. add_theme_support( 'html5', array(
  56. 'search-form',
  57. 'comment-form',
  58. 'gallery',
  59. 'caption',
  60. ) );
  61. // Set up the WordPress core custom background feature.
  62. add_theme_support( 'custom-background', apply_filters( 'libre_2_custom_background_args', array(
  63. 'default-color' => 'ffffff',
  64. ) ) );
  65. }
  66. endif; // libre_2_setup
  67. add_action( 'after_setup_theme', 'libre_2_setup' );
  68. /**
  69. * Set the content width in pixels, based on the theme's design and stylesheet.
  70. *
  71. * Priority 0 to make it available to lower priority callbacks.
  72. *
  73. * @global int $content_width
  74. */
  75. function libre_2_content_width() {
  76. $GLOBALS['content_width'] = apply_filters( 'libre_2_content_width', 739 );
  77. }
  78. add_action( 'after_setup_theme', 'libre_2_content_width', 0 );
  79. /*
  80. * Adjust $content_width for full-width page template
  81. */
  82. if ( ! function_exists( 'libre_2_content_width' ) ) :
  83. function libre_2_content_width() {
  84. global $content_width;
  85. if ( is_page_template( 'templates/full-width-page.php' ) ) {
  86. $content_width = 1088; //pixels
  87. }
  88. }
  89. add_action( 'template_redirect', 'libre_2_content_width' );
  90. endif; // if ! function_exists( 'libre_2_content_width' )
  91. /**
  92. * Register widget area.
  93. *
  94. * @link http://codex.wordpress.org/Function_Reference/register_sidebar
  95. */
  96. function libre_2_widgets_init() {
  97. register_sidebar( array(
  98. 'name' => esc_html__( 'Sidebar', 'libre-2' ),
  99. 'id' => 'sidebar-1',
  100. 'description' => '',
  101. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  102. 'after_widget' => '</aside>',
  103. 'before_title' => '<h2 class="widget-title">',
  104. 'after_title' => '</h2>',
  105. ) );
  106. register_sidebar( array(
  107. 'name' => esc_html__( 'Footer 1', 'libre-2' ),
  108. 'id' => 'sidebar-2',
  109. 'description' => '',
  110. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  111. 'after_widget' => '</aside>',
  112. 'before_title' => '<h2 class="widget-title">',
  113. 'after_title' => '</h2>',
  114. ) );
  115. register_sidebar( array(
  116. 'name' => esc_html__( 'Footer 2', 'libre-2' ),
  117. 'id' => 'sidebar-3',
  118. 'description' => '',
  119. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  120. 'after_widget' => '</aside>',
  121. 'before_title' => '<h2 class="widget-title">',
  122. 'after_title' => '</h2>',
  123. ) );
  124. register_sidebar( array(
  125. 'name' => esc_html__( 'Footer 3', 'libre-2' ),
  126. 'id' => 'sidebar-4',
  127. 'description' => '',
  128. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  129. 'after_widget' => '</aside>',
  130. 'before_title' => '<h2 class="widget-title">',
  131. 'after_title' => '</h2>',
  132. ) );
  133. }
  134. add_action( 'widgets_init', 'libre_2_widgets_init' );
  135. /**
  136. * Register Google Fonts
  137. */
  138. function libre_2_fonts_url() {
  139. $fonts_url = '';
  140. /* Translators: If there are characters in your language that are not
  141. * supported by Libre Baskerville, translate this to 'off'. Do not translate
  142. * into your own language.
  143. */
  144. $libre = esc_html_x( 'on', 'Libre Baskerville font: on or off', 'libre-2' );
  145. if ( 'off' !== $libre ) {
  146. $font_families = array();
  147. $font_families[] = 'Libre Baskerville:400,400italic,700';
  148. $query_args = array(
  149. 'family' => urlencode( implode( '|', $font_families ) ),
  150. 'subset' => urlencode( 'latin,latin-ext' ),
  151. );
  152. $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
  153. }
  154. return esc_url_raw( $fonts_url );
  155. }
  156. /**
  157. * Add preconnect for Google Fonts.
  158. *
  159. * @param array $urls URLs to print for resource hints.
  160. * @param string $relation_type The relation type the URLs are printed.
  161. * @return array $urls URLs to print for resource hints.
  162. function libre_2_resource_hints( $urls, $relation_type ) {
  163. if ( wp_style_is( 'libre-2-fonts', 'queue' ) && 'preconnect' === $relation_type ) {
  164. $urls[] = array(
  165. 'href' => 'https://fonts.gstatic.com',
  166. 'crossorigin',
  167. );
  168. }
  169. return $urls;
  170. }
  171. add_filter( 'wp_resource_hints', 'libre_2_resource_hints', 10, 2 );
  172. */
  173. /**
  174. * Enqueue scripts and styles.
  175. */
  176. function libre_2_scripts() {
  177. wp_enqueue_style( 'libre-2-style', get_stylesheet_uri() );
  178. wp_enqueue_style( 'libre-2-fonts', libre_2_fonts_url(), array(), null );
  179. wp_enqueue_script( 'libre-2-script', get_template_directory_uri() . '/js/libre.js', array( 'jquery' ), '20150623', true );
  180. $adminbar = is_admin_bar_showing();
  181. wp_localize_script( 'libre-2-script', 'libreadminbar', array( $adminbar ) );
  182. wp_enqueue_script( 'libre-2-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );
  183. wp_enqueue_script( 'libre-2-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );
  184. if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
  185. wp_enqueue_script( 'comment-reply' );
  186. }
  187. }
  188. add_action( 'wp_enqueue_scripts', 'libre_2_scripts' );
  189. /*
  190. * Filters the Categories archive widget to add a span around the post count
  191. */
  192. function libre_2_cat_count_span( $links ) {
  193. $links = str_replace( '</a> (', '</a><span class="post-count">(', $links );
  194. $links = str_replace( ')', ')</span>', $links );
  195. return $links;
  196. }
  197. add_filter( 'wp_list_categories', 'libre_2_cat_count_span' );
  198. /*
  199. * Add a span around the post count in the Archives widget
  200. */
  201. function libre_2_archive_count_span( $links ) {
  202. $links = str_replace( '</a>&nbsp;(', '</a><span class="post-count">(', $links );
  203. $links = str_replace( ')', ')</span>', $links );
  204. return $links;
  205. }
  206. add_filter( 'get_archives_link', 'libre_2_archive_count_span' );
  207. if ( ! function_exists( 'libre_2_continue_reading_link' ) ) :
  208. /**
  209. * Returns an ellipsis and "Continue reading" plus off-screen title link for excerpts
  210. */
  211. function libre_2_continue_reading_link() {
  212. return '&hellip; <a class="more-link" href="'. esc_url( get_permalink() ) . '">' . sprintf( wp_kses_post( __( 'Continue reading <span class="screen-reader-text">%1$s</span> <span class="meta-nav" aria-hidden="true">&rarr;</span>', 'libre-2' ) ), esc_attr( strip_tags( get_the_title() ) ) ) . '</a>';
  213. }
  214. endif; // libre_2_continue_reading_link
  215. /**
  216. * Replaces "[...]" (appended to automatically generated excerpts) with libre_2_continue_reading_link().
  217. *
  218. * To override this in a child theme, remove the filter and add your own
  219. * function tied to the excerpt_more filter hook.
  220. */
  221. function libre_2_auto_excerpt_more( $more ) {
  222. return libre_2_continue_reading_link();
  223. }
  224. add_filter( 'excerpt_more', 'libre_2_auto_excerpt_more' );
  225. /**
  226. * Adds a pretty "Continue Reading" link to custom post excerpts.
  227. *
  228. * To override this link in a child theme, remove the filter and add your own
  229. * function tied to the get_the_excerpt filter hook.
  230. */
  231. function libre_2_custom_excerpt_more( $output ) {
  232. if ( has_excerpt() && ! is_attachment() ) {
  233. $output .= libre_2_continue_reading_link();
  234. }
  235. return $output;
  236. }
  237. add_filter( 'get_the_excerpt', 'libre_2_custom_excerpt_more' );
  238. /*
  239. * Custom comments display to move Reply link,
  240. * used in comments.php
  241. */
  242. function libre_2_comments( $comment, $args, $depth ) {
  243. ?>
  244. <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
  245. <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
  246. <footer class="comment-meta">
  247. <div class="comment-metadata">
  248. <span class="comment-author vcard">
  249. <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
  250. <?php printf( '<b class="fn">%s</b>', get_comment_author_link() ); ?>
  251. </span>
  252. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
  253. <time datetime="<?php comment_time( 'c' ); ?>">
  254. <?php printf( '<span class="comment-date">%1$s</span><span class="comment-time screen-reader-text">%2$s</span>', get_comment_date(), get_comment_time() ); ?>
  255. </time>
  256. </a>
  257. <?php
  258. comment_reply_link( array_merge( $args, array(
  259. 'add_below' => 'div-comment',
  260. 'depth' => $depth,
  261. 'max_depth' => $args['max_depth'],
  262. 'before' => '<span class="reply">',
  263. 'after' => '</span>'
  264. ) ) );
  265. ?>
  266. <?php edit_comment_link( esc_html__( 'Edit', 'libre-2' ), '<span class="edit-link">', '</span>' ); ?>
  267. </div><!-- .comment-metadata -->
  268. <?php if ( '0' == $comment->comment_approved ) : ?>
  269. <p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'libre-2' ); ?></p>
  270. <?php endif; ?>
  271. </footer><!-- .comment-meta -->
  272. <div class="comment-content">
  273. <?php comment_text(); ?>
  274. </div><!-- .comment-content -->
  275. </article><!-- .comment-body -->
  276. <?php
  277. }
  278. /**
  279. * Implement the Custom Header feature.
  280. */
  281. require get_template_directory() . '/inc/custom-header.php';
  282. /**
  283. * Custom template tags for this theme.
  284. */
  285. require get_template_directory() . '/inc/template-tags.php';
  286. /**
  287. * Custom functions that act independently of the theme templates.
  288. */
  289. require get_template_directory() . '/inc/extras.php';
  290. /**
  291. * Customizer additions.
  292. */
  293. require get_template_directory() . '/inc/customizer.php';
  294. /**
  295. * Load Jetpack compatibility file.
  296. */
  297. require get_template_directory() . '/inc/jetpack.php';
  298. /**
  299. * Load WooCommerce compatibility file.
  300. */
  301. if ( class_exists( 'WooCommerce' ) ) {
  302. require get_template_directory() . '/inc/woocommerce.php';
  303. }