PageRenderTime 86ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/blog.old/wp-content/themes/esquire/functions.php

https://github.com/chopsuei3/oscc
PHP | 410 lines | 225 code | 55 blank | 130 comment | 40 complexity | 9fd4236a854754fbe62aba96bf1133fe MD5 | raw file
  1. <?php
  2. /**
  3. * @package Esquire
  4. */
  5. /**
  6. * Load Esquire scripts
  7. */
  8. function esquire_scripts() {
  9. wp_enqueue_script( 'esquire', get_template_directory_uri() .'/js/esquire.js', 'jquery', '2011-07-29' );
  10. if ( ! is_singular() || ( is_singular() && 'audio' == get_post_format() ) )
  11. wp_enqueue_script( 'audio-player', get_template_directory_uri() . '/js/audio-player.js', array( 'jquery'), '20110801' );
  12. }
  13. add_action( 'wp_enqueue_scripts', 'esquire_scripts' );
  14. /**
  15. * Set the content width based on the theme's design and stylesheet.
  16. */
  17. if ( ! isset( $content_width ) )
  18. $content_width = 560;
  19. /**
  20. * Make theme available for translation
  21. * Translations can be filed in the /languages/ directory
  22. */
  23. load_theme_textdomain( 'esquire', get_template_directory_uri() . '/languages' );
  24. $locale = get_locale();
  25. $locale_file = get_template_directory_uri() . "/languages/$locale.php";
  26. if ( is_readable( $locale_file ) )
  27. require_once( $locale_file );
  28. /**
  29. * Add feed links to head
  30. */
  31. add_theme_support( 'automatic-feed-links' );
  32. /**
  33. * This theme uses wp_nav_menu() in one location.
  34. */
  35. register_nav_menus( array(
  36. 'primary' => __( 'Main Menu', 'esquire' ),
  37. ) );
  38. /**
  39. * Enable Post Formats
  40. */
  41. add_theme_support( 'post-formats', array( 'aside', 'gallery', 'image', 'quote', 'link', 'audio', 'video' ) );
  42. /**
  43. * Sniff out the number of categories in use and return the number of categories
  44. */
  45. function esquire_category_counter() {
  46. if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
  47. // Create an array of all the categories that are attached to posts
  48. $all_the_cool_cats = get_categories( array(
  49. 'hide_empty' => 1,
  50. ) );
  51. // Count the number of categories that are attached to the posts
  52. $all_the_cool_cats = count( $all_the_cool_cats );
  53. set_transient( 'all_the_cool_cats', $all_the_cool_cats );
  54. }
  55. return $all_the_cool_cats;
  56. }
  57. /**
  58. * Flush out the transients used in esquire_category_counter
  59. */
  60. function esquire_category_transient_flusher() {
  61. // Like, beat it. Dig?
  62. delete_transient( 'all_the_cool_cats' );
  63. }
  64. add_action( 'edit_category', 'esquire_category_transient_flusher' );
  65. add_action( 'save_post', 'esquire_category_transient_flusher' );
  66. /**
  67. * Add a class to the Older Posts link
  68. */
  69. function esquire_next_posts_link_attributes( $attr ) {
  70. $attr = 'rel="prev"';
  71. return $attr;
  72. }
  73. add_filter( 'next_posts_link_attributes', 'esquire_next_posts_link_attributes' );
  74. /**
  75. * Add a class to the Newer Posts link
  76. */
  77. function esquire_previous_posts_link_attributes( $attr ) {
  78. $attr = 'rel="next"';
  79. return $attr;
  80. }
  81. add_filter( 'previous_posts_link_attributes', 'esquire_previous_posts_link_attributes' );
  82. /**
  83. * Sets the post excerpt length to 40 words.
  84. *
  85. * To override this length in a child theme, remove the filter and add your own
  86. * function tied to the excerpt_length filter hook.
  87. */
  88. function esquire_excerpt_length( $length ) {
  89. return 40;
  90. }
  91. add_filter( 'excerpt_length', 'esquire_excerpt_length' );
  92. /**
  93. * Returns a "Continue Reading" link for excerpts
  94. */
  95. function esquire_continue_reading_link() {
  96. return ' <a href="'. esc_url( get_permalink() ) . '"><em>' .__( 'Continue&nbsp;reading&nbsp;<span class="meta-nav">&rarr;</span>', 'esquire' ) . '</em></a>';
  97. }
  98. /**
  99. * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and esquire_continue_reading_link().
  100. *
  101. * To override this in a child theme, remove the filter and add your own
  102. * function tied to the excerpt_more filter hook.
  103. */
  104. function esquire_auto_excerpt_more( $more ) {
  105. return ' &hellip;' . esquire_continue_reading_link();
  106. }
  107. add_filter( 'excerpt_more', 'esquire_auto_excerpt_more' );
  108. /**
  109. * Adds a pretty "Continue Reading" link to custom post excerpts.
  110. *
  111. * To override this link in a child theme, remove the filter and add your own
  112. * function tied to the get_the_excerpt filter hook.
  113. */
  114. function esquire_custom_excerpt_more( $output ) {
  115. if ( has_excerpt() && ! is_attachment() ) {
  116. $output .= esquire_continue_reading_link();
  117. }
  118. return $output;
  119. }
  120. add_filter( 'get_the_excerpt', 'esquire_custom_excerpt_more' );
  121. /**
  122. * Register our footer widget area
  123. *
  124. * @since Esquire 1.0
  125. */
  126. function esquire_widgets_init() {
  127. register_sidebar( array(
  128. 'name' => __( 'Footer', 'esquire' ),
  129. 'id' => 'sidebar-1',
  130. 'before_widget' => '<aside id="%1$s" class="widget %2$s">',
  131. 'after_widget' => "</aside>",
  132. 'before_title' => '<h3 class="widget-title">',
  133. 'after_title' => '</h3>',
  134. ) );
  135. }
  136. add_action( 'widgets_init', 'esquire_widgets_init' );
  137. /**
  138. * Template for comments and pingbacks.
  139. * Used as a callback by wp_list_comments() for displaying the comments.
  140. *
  141. * @since Esquire 1.0
  142. */
  143. function esquire_comment( $comment, $args, $depth ) {
  144. $GLOBALS['comment'] = $comment;
  145. switch ( $comment->comment_type ) :
  146. case 'pingback' :
  147. case 'trackback' :
  148. ?>
  149. <li class="post pingback">
  150. <p><?php _e( 'Pingback:', 'esquire' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( 'Edit', 'esquire' ), '<span class="edit-link">', '</span>' ); ?></p>
  151. <?php
  152. break;
  153. default :
  154. ?>
  155. <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
  156. <article id="comment-<?php comment_ID(); ?>" class="comment">
  157. <footer class="comment-meta">
  158. <div class="comment-author vcard">
  159. <?php
  160. $avatar_size = 16;
  161. echo get_avatar( $comment, $avatar_size );
  162. /* translators: 1: comment author, 2: date and time */
  163. printf( __( '%1$s on %2$s <span class="says">said:</span>', 'esquire' ),
  164. sprintf( '<span class="fn">%s</span>', get_comment_author_link() ),
  165. sprintf( '<a href="%1$s"><time pubdate datetime="%2$s">%3$s</time></a>',
  166. esc_url( get_comment_link( $comment->comment_ID ) ),
  167. get_comment_time( 'c' ),
  168. /* translators: 1: date, 2: time */
  169. sprintf( __( '%1$s at %2$s', 'esquire' ), get_comment_date(), get_comment_time() )
  170. )
  171. );
  172. ?>
  173. <?php edit_comment_link( __( 'Edit', 'esquire' ), '<span class="edit-link">', '</span>' ); ?>
  174. </div><!-- .comment-author .vcard -->
  175. <?php if ( $comment->comment_approved == '0' ) : ?>
  176. <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'esquire' ); ?></em>
  177. <br />
  178. <?php endif; ?>
  179. </footer>
  180. <div class="comment-content"><?php comment_text(); ?></div>
  181. <div class="reply">
  182. <?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply <span>&darr;</span>', 'esquire' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
  183. </div><!-- .reply -->
  184. </article><!-- #comment-## -->
  185. <?php
  186. break;
  187. endswitch;
  188. }
  189. /**
  190. * Filter the_content for post formats, and add extra presentational markup as needed.
  191. *
  192. * @param string the_content
  193. * @return string Updated content with extra markup.
  194. */
  195. function esquire_the_content( $content ) {
  196. global $post;
  197. $format = get_post_format();
  198. if ( ! $format )
  199. return $content;
  200. switch ( $format ) {
  201. case 'image':
  202. $first_image = wpcom_themes_image_grabber( $post->ID, $content, '<div class="frame"><div class="wrapper">', '</div></div>' );
  203. if ( $first_image )
  204. $content = preg_replace( WPCOM_THEMES_IMAGE_REPLACE_REGEX, $first_image, $content, 1 );
  205. break;
  206. default:
  207. break;
  208. }
  209. return $content;
  210. }
  211. if ( ! is_admin() )
  212. add_filter( 'the_content', 'esquire_the_content', 11 );
  213. /**
  214. * Add extra markup to VideoPress embeds.
  215. *
  216. * @param string html Video content from VideoPress plugin.
  217. * @return string Updated content with extra markup.
  218. */
  219. function esquire_video_embed_html( $html ) {
  220. $html = '<div class="frame"><div class="player">' . $html . '</div></div>';
  221. return $html;
  222. }
  223. /**
  224. * Add extra markup to auto-embedded videos.
  225. *
  226. * @param string html Content from the auto-embed plugin.
  227. * @param string url Link embedded in the post, used to determine if this is a video we want to filter.
  228. * @return string Updated content with extra markup.
  229. */
  230. function esquire_check_video_embeds( $html, $url ) {
  231. if ( false !== ( strstr( $url, 'youtube' ) ) || false !== ( strstr( $url, 'vimeo' ) ) )
  232. $html = esquire_video_embed_html( $html );
  233. return $html;
  234. }
  235. // Add post thumbnail support for audio album art
  236. add_theme_support( 'post-thumbnails' );
  237. add_image_size( 'audio', 207, 207, false );
  238. // Add in-head JS block for audio post format
  239. function esquire_add_audio_support() {
  240. if ( ! is_singular() || ( is_singular() && 'audio' == get_post_format() ) ) {
  241. ?>
  242. <script type="text/javascript">
  243. /* <![CDATA[ */
  244. AudioPlayer.setup( "<?php echo get_template_directory_uri(); ?>/swf/player.swf", {
  245. bg: "222222",
  246. leftbg: "444444",
  247. rightbg: "444444",
  248. track: "222222",
  249. text: "ffffff",
  250. lefticon: "eeeeee",
  251. righticon: "eeeeee",
  252. border: "222222",
  253. tracker: "3d87cb",
  254. loader: "666666"
  255. });
  256. /* ]]> */
  257. </script>
  258. <?php }
  259. }
  260. add_action( 'wp_head', 'esquire_add_audio_support' );
  261. /**
  262. * Return the URL for the first link found in this post.
  263. *
  264. * @param string the_content Post content, falls back to current post content if empty.
  265. * @return string|bool URL or false when no link is present.
  266. */
  267. function wpcom_themes_url_grabber( $the_content = '' ) {
  268. if ( empty( $the_content ) )
  269. $the_content = get_the_content();
  270. if ( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/is', $the_content, $matches ) )
  271. return false;
  272. return esc_url_raw( $matches[1] );
  273. }
  274. // Define common regex lookup patterns
  275. if ( ! defined( 'WPCOM_THEMES_IMAGE_REGEX' ) )
  276. define( 'WPCOM_THEMES_IMAGE_REGEX', '/(<img.+src=[\'"]([^\'"]+)[\'"].*?>)/i' );
  277. if ( ! defined( 'WPCOM_THEMES_IMAGE_REPLACE_REGEX' ) )
  278. define( 'WPCOM_THEMES_IMAGE_REPLACE_REGEX', '/\[caption.*\[\/caption\]|<img[^>]+./' );
  279. if ( ! defined( 'WPCOM_THEMES_VIDEO_REGEX' ) ) {
  280. // iframe: <iframe[^>]*+></iframe>
  281. define( 'WPCOM_THEMES_VIDEO_REGEX', '#(<object[^>]*+>(?>[^<]*+(?><(?!/object>)[^<]*+)*)</object>|<embed[^>]*+>(?:\s*</embed>)?)#i' );
  282. }
  283. /**
  284. * Return the HTML output for first image found for a post.
  285. *
  286. * @param int post_id ID for parent post
  287. * @param string the_content
  288. * @param string before Optional before string
  289. * @param string after Optional after string
  290. * @return boolean|string HTML output or false if no match
  291. */
  292. function wpcom_themes_image_grabber( $post_id, $the_content = '', $before = '', $after = '' ) {
  293. global $wpdb;
  294. $image_src = '';
  295. if ( empty( $the_content ) )
  296. $the_content = get_the_content();
  297. $first_image = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'attachment' AND INSTR(post_mime_type, 'image') ORDER BY menu_order ASC LIMIT 0,1", (int) $post_id ) );
  298. if ( ! empty( $first_image ) ) {
  299. // We have an attachment, so just use its data.
  300. $image_src = wp_get_attachment_image( $first_image, 'image' );
  301. } else {
  302. // Try to get the image for the linked image (not attached)
  303. $output = preg_match( WPCOM_THEMES_IMAGE_REGEX, $the_content, $matches );
  304. if ( isset( $matches[0] ) )
  305. $image_src = $matches[0];
  306. }
  307. if ( ! empty( $image_src ) ) {
  308. // Add wrapper markup, if specified
  309. if ( ! empty( $before ) )
  310. $image_src = $before . $image_src;
  311. if ( ! empty( $after ) )
  312. $image_src = $image_src . $after;
  313. return $image_src;
  314. }
  315. return false;
  316. }
  317. /**
  318. * Return the HTML output for the first video found for a post.
  319. *
  320. * @param string the_content
  321. * @param string before Optional before string
  322. * @param string after Optional after string
  323. * @return boolean|string HTML output or false if no match
  324. */
  325. function wpcom_themes_video_grabber( $the_content = '', $before = '', $after = '' ) {
  326. $first_video = '';
  327. if ( empty( $the_content ) )
  328. $the_content = get_the_content();
  329. // Try to get the markup for the first video in this post
  330. $output = preg_match( WPCOM_THEMES_VIDEO_REGEX, $the_content, $matches );
  331. if ( isset( $matches[0] ) )
  332. $first_video = $matches[0];
  333. if ( ! empty( $first_video ) ) {
  334. // Add wrapper markup, if specified
  335. if ( ! empty( $before ) )
  336. $first_video = $before . $first_video;
  337. if ( ! empty( $after ) )
  338. $first_video = $first_video . $after;
  339. return $first_video;
  340. }
  341. return false;
  342. }
  343. /**
  344. * Return the first audio file found for a post.
  345. *
  346. * @param int post_id ID for parent post
  347. * @return boolean|string Path to audio file
  348. */
  349. function wpcom_themes_audio_grabber( $post_id ) {
  350. global $wpdb;
  351. $first_audio = $wpdb->get_var( $wpdb->prepare( "SELECT guid FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'attachment' AND INSTR(post_mime_type, 'audio') ORDER BY menu_order ASC LIMIT 0,1", (int) $post_id ) );
  352. if ( ! empty( $first_audio ) )
  353. return $first_audio;
  354. return false;
  355. }