PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/toolbox/functions.php

https://bitbucket.org/lgorence/quickpress
PHP | 310 lines | 160 code | 39 blank | 111 comment | 25 complexity | 86fc5588b6519f3d3909f1715949713b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Toolbox functions and definitions
  4. *
  5. * Sets up the theme and provides some helper functions. Some helper functions
  6. * are used in the theme as custom template tags. Others are attached to action and
  7. * filter hooks in WordPress to change core functionality.
  8. *
  9. * When using a child theme (see http://codex.wordpress.org/Theme_Development and
  10. * http://codex.wordpress.org/Child_Themes), you can override certain functions
  11. * (those wrapped in a function_exists() call) by defining them first in your child theme's
  12. * functions.php file. The child theme's functions.php file is included before the parent
  13. * theme's file, so the child theme functions would be used.
  14. *
  15. * Functions that are not pluggable (not wrapped in function_exists()) are instead attached
  16. * to a filter or action hook. The hook can be removed by using remove_action() or
  17. * remove_filter() and you can attach your own function to the hook.
  18. *
  19. * For more information on hooks, actions, and filters, see http://codex.wordpress.org/Plugin_API.
  20. *
  21. * @package Toolbox
  22. * @since Toolbox 0.1
  23. */
  24. /**
  25. * Set the content width based on the theme's design and stylesheet.
  26. */
  27. if ( ! isset( $content_width ) )
  28. $content_width = 640; /* pixels */
  29. if ( ! function_exists( 'toolbox_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 runs
  34. * before the init hook. The init hook is too late for some features, such as indicating
  35. * support post thumbnails.
  36. *
  37. * To override toolbox_setup() in a child theme, add your own toolbox_setup to your child theme's
  38. * functions.php file.
  39. */
  40. function toolbox_setup() {
  41. /**
  42. * Make theme available for translation
  43. * Translations can be filed in the /languages/ directory
  44. * If you're building a theme based on toolbox, use a find and replace
  45. * to change 'toolbox' to the name of your theme in all the template files
  46. */
  47. load_theme_textdomain( 'toolbox', get_template_directory() . '/languages' );
  48. $locale = get_locale();
  49. $locale_file = get_template_directory() . "/languages/$locale.php";
  50. if ( is_readable( $locale_file ) )
  51. require_once( $locale_file );
  52. /**
  53. * Add default posts and comments RSS feed links to head
  54. */
  55. add_theme_support( 'automatic-feed-links' );
  56. /**
  57. * This theme uses wp_nav_menu() in one location.
  58. */
  59. register_nav_menus( array(
  60. 'primary' => __( 'Primary Menu', 'toolbox' ),
  61. ) );
  62. /**
  63. * Add support for the Aside and Gallery Post Formats
  64. */
  65. add_theme_support( 'post-formats', array( 'aside', 'image', 'gallery' ) );
  66. }
  67. endif; // toolbox_setup
  68. /**
  69. * Tell WordPress to run toolbox_setup() when the 'after_setup_theme' hook is run.
  70. */
  71. add_action( 'after_setup_theme', 'toolbox_setup' );
  72. /**
  73. * Set a default theme color array for WP.com.
  74. */
  75. $themecolors = array(
  76. 'bg' => 'ffffff',
  77. 'border' => 'eeeeee',
  78. 'text' => '444444',
  79. );
  80. /**
  81. * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
  82. */
  83. function toolbox_page_menu_args( $args ) {
  84. $args['show_home'] = true;
  85. return $args;
  86. }
  87. add_filter( 'wp_page_menu_args', 'toolbox_page_menu_args' );
  88. /**
  89. * Register widgetized area and update sidebar with default widgets
  90. */
  91. function toolbox_widgets_init() {
  92. register_sidebar( array(
  93. 'name' => __( 'Sidebar 1', 'toolbox' ),
  94. 'id' => 'sidebar-1',
  95. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  96. 'after_widget' => "</aside>",
  97. 'before_title' => '<h1 class="widget-title">',
  98. 'after_title' => '</h1>',
  99. ) );
  100. register_sidebar( array(
  101. 'name' => __( 'Sidebar 2', 'toolbox' ),
  102. 'id' => 'sidebar-2',
  103. 'description' => __( 'An optional second sidebar area', 'toolbox' ),
  104. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  105. 'after_widget' => "</aside>",
  106. 'before_title' => '<h1 class="widget-title">',
  107. 'after_title' => '</h1>',
  108. ) );
  109. }
  110. add_action( 'init', 'toolbox_widgets_init' );
  111. if ( ! function_exists( 'toolbox_content_nav' ) ):
  112. /**
  113. * Display navigation to next/previous pages when applicable
  114. *
  115. * @since Toolbox 1.2
  116. */
  117. function toolbox_content_nav( $nav_id ) {
  118. global $wp_query;
  119. ?>
  120. <nav id="<?php echo $nav_id; ?>">
  121. <h1 class="assistive-text section-heading"><?php _e( 'Post navigation', 'toolbox' ); ?></h1>
  122. <?php if ( is_single() ) : // navigation links for single posts ?>
  123. <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'toolbox' ) . '</span> %title' ); ?>
  124. <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'toolbox' ) . '</span>' ); ?>
  125. <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
  126. <?php if ( get_next_posts_link() ) : ?>
  127. <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'toolbox' ) ); ?></div>
  128. <?php endif; ?>
  129. <?php if ( get_previous_posts_link() ) : ?>
  130. <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'toolbox' ) ); ?></div>
  131. <?php endif; ?>
  132. <?php endif; ?>
  133. </nav><!-- #<?php echo $nav_id; ?> -->
  134. <?php
  135. }
  136. endif; // toolbox_content_nav
  137. if ( ! function_exists( 'toolbox_comment' ) ) :
  138. /**
  139. * Template for comments and pingbacks.
  140. *
  141. * To override this walker in a child theme without modifying the comments template
  142. * simply create your own toolbox_comment(), and that function will be used instead.
  143. *
  144. * Used as a callback by wp_list_comments() for displaying the comments.
  145. *
  146. * @since Toolbox 0.4
  147. */
  148. function toolbox_comment( $comment, $args, $depth ) {
  149. $GLOBALS['comment'] = $comment;
  150. switch ( $comment->comment_type ) :
  151. case 'pingback' :
  152. case 'trackback' :
  153. ?>
  154. <li class="post pingback">
  155. <p><?php _e( 'Pingback:', 'toolbox' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'toolbox' ), ' ' ); ?></p>
  156. <?php
  157. break;
  158. default :
  159. ?>
  160. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  161. <article id="comment-<?php comment_ID(); ?>" class="comment">
  162. <footer>
  163. <div class="comment-author vcard">
  164. <?php echo get_avatar( $comment, 40 ); ?>
  165. <?php printf( __( '%s <span class="says">says:</span>', 'toolbox' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
  166. </div><!-- .comment-author .vcard -->
  167. <?php if ( $comment->comment_approved == '0' ) : ?>
  168. <em><?php _e( 'Your comment is awaiting moderation.', 'toolbox' ); ?></em>
  169. <br />
  170. <?php endif; ?>
  171. <div class="comment-meta commentmetadata">
  172. <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><time pubdate datetime="<?php comment_time( 'c' ); ?>">
  173. <?php
  174. /* translators: 1: date, 2: time */
  175. printf( __( '%1$s at %2$s', 'toolbox' ), get_comment_date(), get_comment_time() ); ?>
  176. </time></a>
  177. <?php edit_comment_link( __( '(Edit)', 'toolbox' ), ' ' );
  178. ?>
  179. </div><!-- .comment-meta .commentmetadata -->
  180. </footer>
  181. <div class="comment-content"><?php comment_text(); ?></div>
  182. <div class="reply">
  183. <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  184. </div><!-- .reply -->
  185. </article><!-- #comment-## -->
  186. <?php
  187. break;
  188. endswitch;
  189. }
  190. endif; // ends check for toolbox_comment()
  191. if ( ! function_exists( 'toolbox_posted_on' ) ) :
  192. /**
  193. * Prints HTML with meta information for the current post-date/time and author.
  194. * Create your own toolbox_posted_on to override in a child theme
  195. *
  196. * @since Toolbox 1.2
  197. */
  198. function toolbox_posted_on() {
  199. printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="byline"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'toolbox' ),
  200. esc_url( get_permalink() ),
  201. esc_attr( get_the_time() ),
  202. esc_attr( get_the_date( 'c' ) ),
  203. esc_html( get_the_date() ),
  204. esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
  205. esc_attr( sprintf( __( 'View all posts by %s', 'toolbox' ), get_the_author() ) ),
  206. esc_html( get_the_author() )
  207. );
  208. }
  209. endif;
  210. /**
  211. * Adds custom classes to the array of body classes.
  212. *
  213. * @since Toolbox 1.2
  214. */
  215. function toolbox_body_classes( $classes ) {
  216. // Adds a class of single-author to blogs with only 1 published author
  217. if ( ! is_multi_author() ) {
  218. $classes[] = 'single-author';
  219. }
  220. return $classes;
  221. }
  222. add_filter( 'body_class', 'toolbox_body_classes' );
  223. /**
  224. * Returns true if a blog has more than 1 category
  225. *
  226. * @since Toolbox 1.2
  227. */
  228. function toolbox_categorized_blog() {
  229. if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
  230. // Create an array of all the categories that are attached to posts
  231. $all_the_cool_cats = get_categories( array(
  232. 'hide_empty' => 1,
  233. ) );
  234. // Count the number of categories that are attached to the posts
  235. $all_the_cool_cats = count( $all_the_cool_cats );
  236. set_transient( 'all_the_cool_cats', $all_the_cool_cats );
  237. }
  238. if ( '1' != $all_the_cool_cats ) {
  239. // This blog has more than 1 category so toolbox_categorized_blog should return true
  240. return true;
  241. } else {
  242. // This blog has only 1 category so toolbox_categorized_blog should return false
  243. return false;
  244. }
  245. }
  246. /**
  247. * Flush out the transients used in toolbox_categorized_blog
  248. *
  249. * @since Toolbox 1.2
  250. */
  251. function toolbox_category_transient_flusher() {
  252. // Like, beat it. Dig?
  253. delete_transient( 'all_the_cool_cats' );
  254. }
  255. add_action( 'edit_category', 'toolbox_category_transient_flusher' );
  256. add_action( 'save_post', 'toolbox_category_transient_flusher' );
  257. /**
  258. * Filter in a link to a content ID attribute for the next/previous image links on image attachment pages
  259. */
  260. function toolbox_enhanced_image_navigation( $url ) {
  261. global $post, $wp_rewrite;
  262. $id = (int) $post->ID;
  263. $object = get_post( $id );
  264. if ( wp_attachment_is_image( $post->ID ) && ( $wp_rewrite->using_permalinks() && ( $object->post_parent > 0 ) && ( $object->post_parent != $id ) ) )
  265. $url = $url . '#main';
  266. return $url;
  267. }
  268. add_filter( 'attachment_link', 'toolbox_enhanced_image_navigation' );
  269. /**
  270. * This theme was built with PHP, Semantic HTML, CSS, love, and a Toolbox.
  271. */