extras.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Custom functions that act independently of the theme templates
  4. *
  5. * Eventually, some of the functionality here could be replaced by core features
  6. *
  7. * @package Illustratr
  8. */
  9. /**
  10. * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
  11. *
  12. * @param array $args Configuration arguments.
  13. * @return array
  14. */
  15. function illustratr_page_menu_args( $args ) {
  16. $args['show_home'] = true;
  17. return $args;
  18. }
  19. add_filter( 'wp_page_menu_args', 'illustratr_page_menu_args' );
  20. /**
  21. * Adds custom classes to the array of body classes.
  22. *
  23. * @param array $classes Classes for the body element.
  24. * @return array
  25. */
  26. function illustratr_body_classes( $classes ) {
  27. // Adds a class of body-borders to blogs with default background settings.
  28. if ( ! get_background_image() ) {
  29. $classes[] = 'body-borders';
  30. }
  31. // Adds a class of group-blog to blogs with more than 1 published author.
  32. if ( is_multi_author() ) {
  33. $classes[] = 'group-blog';
  34. }
  35. // Adds a class of has-header-image to blogs with a header image.
  36. if ( get_header_image() ) {
  37. $classes[] = 'has-header-image';
  38. }
  39. // Adds a class of hide-portfolio-page-content to blogs if Theme Option hide portfolio page content is ticked and page is using the Portfolio Template
  40. if ( get_theme_mod( 'illustratr_hide_portfolio_page_content' ) && is_page_template( 'page-templates/portfolio-page.php' ) ) {
  41. $classes[] = 'hide-portfolio-page-content';
  42. }
  43. return $classes;
  44. }
  45. add_filter( 'body_class', 'illustratr_body_classes' );
  46. /**
  47. * Adds custom classes to the array of post classes.
  48. *
  49. * @param array $classes Classes for the post element.
  50. * @return array
  51. */
  52. function illustratr_post_classes( $classes ) {
  53. // Adds a class of empty-entry-meta to pages/projects without any entry meta.
  54. $comments_status = false;
  55. $tags_list = false;
  56. if ( ! post_password_required() && ( comments_open() || '0' != get_comments_number() ) ) {
  57. $comments_status = true;
  58. }
  59. if ( 'jetpack-portfolio' == get_post_type() ) {
  60. $tags_list = get_the_term_list( get_the_ID(), 'jetpack-portfolio-tag' );
  61. }
  62. if ( ! current_user_can( 'edit_posts' ) && 'post' != get_post_type() && ! $comments_status && ! $tags_list ) {
  63. $classes[] = 'empty-entry-meta';
  64. }
  65. // Adds a class of portfolio-entry to portfolio projects.
  66. if ( 'jetpack-portfolio' == get_post_type() ) {
  67. $classes[] = 'portfolio-entry';
  68. }
  69. return $classes;
  70. }
  71. add_filter( 'post_class', 'illustratr_post_classes' );
  72. /**
  73. * Filters wp_title to print a neat <title> tag based on what is being viewed.
  74. *
  75. * @param string $title Default title text for current view.
  76. * @param string $sep Optional separator.
  77. * @return string The filtered title.
  78. */
  79. function illustratr_wp_title( $title, $sep ) {
  80. if ( is_feed() ) {
  81. return $title;
  82. }
  83. global $page, $paged;
  84. // Add the blog name
  85. $title .= get_bloginfo( 'name', 'display' );
  86. // Add the blog description for the home/front page.
  87. $site_description = get_bloginfo( 'description', 'display' );
  88. if ( $site_description && ( is_home() || is_front_page() ) ) {
  89. $title .= " $sep $site_description";
  90. }
  91. // Add a page number if necessary:
  92. if ( $paged >= 2 || $page >= 2 ) {
  93. $title .= " $sep " . sprintf( __( 'Page %s', 'illustratr' ), max( $paged, $page ) );
  94. }
  95. return $title;
  96. }
  97. add_filter( 'wp_title', 'illustratr_wp_title', 10, 2 );
  98. /**
  99. * Sets the authordata global when viewing an author archive.
  100. *
  101. * This provides backwards compatibility with
  102. * http://core.trac.wordpress.org/changeset/25574
  103. *
  104. * It removes the need to call the_post() and rewind_posts() in an author
  105. * template to print information about the author.
  106. *
  107. * @global WP_Query $wp_query WordPress Query object.
  108. * @return void
  109. */
  110. function illustratr_setup_author() {
  111. global $wp_query;
  112. if ( $wp_query->is_author() && isset( $wp_query->post ) ) {
  113. $GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author );
  114. }
  115. }
  116. add_action( 'wp', 'illustratr_setup_author' );
  117. /**
  118. * Returns the URL from the post.
  119. *
  120. * @uses get_the_link() to get the URL in the post meta (if it exists) or
  121. * the first link found in the post content.
  122. *
  123. * Falls back to the post permalink if no URL is found in the post.
  124. *
  125. * @return string URL
  126. */
  127. function illustratr_get_link_url() {
  128. $content = get_the_content();
  129. $has_url = get_url_in_content( $content );
  130. return ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
  131. }
  132. /**
  133. * Use &hellip; instead of [...] for excerpts.
  134. */
  135. function illustratr_excerpt_more( $more ) {
  136. return '&hellip;';
  137. }
  138. add_filter( 'excerpt_more', 'illustratr_excerpt_more' );
  139. /**
  140. * Wrap more link
  141. */
  142. function illustratr_more_link( $link ) {
  143. return '<div class="more-link-wrapper">' . $link . '</div>';
  144. }
  145. add_filter( 'the_content_more_link', 'illustratr_more_link' );
  146. /**
  147. * Remove WordPress's default padding on images with captions
  148. *
  149. * @param int $width Default WP .wp-caption width (image width + 10px)
  150. * @return int Updated width to remove 10px padding
  151. */
  152. function illustratr_remove_caption_padding( $width ) {
  153. return $width - 10;
  154. }
  155. add_filter( 'img_caption_shortcode_width', 'illustratr_remove_caption_padding' );
  156. /**
  157. * Remove the 1st gallery shortcode from gallery post format content.
  158. */
  159. function illustratr_strip_first_gallery( $content ) {
  160. if ( 'gallery' === get_post_format() && 'post' === get_post_type() && get_post_gallery() ) {
  161. $regex = get_shortcode_regex( array( 'gallery' ) );
  162. $content = preg_replace( '/'. $regex .'/s', '', $content, 1 );
  163. $content = wp_kses_post( $content );
  164. }
  165. return $content;
  166. }
  167. add_filter( 'the_content', 'illustratr_strip_first_gallery' );
  168. /**
  169. * Checking for the existence of a Gravatar
  170. */
  171. function illustratr_validate_gravatar( $email ) {
  172. $hash = md5( strtolower( trim( $email ) ) );
  173. $uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
  174. $headers = @get_headers( $uri );
  175. if ( ! preg_match( "|200|", $headers[0] ) ) {
  176. $has_valid_avatar = false;
  177. } else {
  178. $has_valid_avatar = true;
  179. }
  180. return $has_valid_avatar;
  181. }
  182. /**
  183. * Adds a wrapper to videos and enqueue script
  184. *
  185. * @return string
  186. */
  187. function illustratr_videos_embed_html( $html ) {
  188. if ( empty( $html ) || ! is_string( $html ) ) {
  189. return $html;
  190. }
  191. wp_enqueue_script( 'illustratr-responsive-videos', get_template_directory_uri() . '/js/responsive-videos.js', array( 'jquery', 'underscore' ), '20140320', true );
  192. return '<div class="video-wrapper">' . $html . '</div>';
  193. }
  194. add_filter( 'wp_video_shortcode', 'illustratr_videos_embed_html' );
  195. add_filter( 'video_embed_html', 'illustratr_videos_embed_html' );
  196. add_filter( 'embed_oembed_html', 'illustratr_videos_maybe_wrap_oembed', 10, 2 );
  197. add_filter( 'embed_handler_html', 'illustratr_videos_maybe_wrap_oembed', 10, 2 );
  198. /**
  199. * Check if oEmbed is YouTube or Vimeo before wrapping.
  200. *
  201. * @return string
  202. */
  203. function illustratr_videos_maybe_wrap_oembed( $html, $url ) {
  204. if ( empty( $html ) || ! is_string( $html ) || ! $url ) {
  205. return $html;
  206. }
  207. $video_wrapper = '<div class="video-wrapper">';
  208. $already_wrapped = strpos( $html, $video_wrapper );
  209. // If the oEmbed has already been wrapped, return the html.
  210. if ( false !== $already_wrapped ) {
  211. return $html;
  212. }
  213. /**
  214. * oEmbed Video Providers.
  215. *
  216. * A whitelist of oEmbed video provider Regex patterns to check against before wrapping the output.
  217. *
  218. * @param array $video_patterns oEmbed video provider Regex patterns.
  219. */
  220. $video_patterns = apply_filters( 'illustratr_videos_oembed_videos', array(
  221. 'https?://((m|www)\.)?youtube\.com/watch',
  222. 'https?://((m|www)\.)?youtube\.com/playlist',
  223. 'https?://youtu\.be/',
  224. 'https?://(.+\.)?vimeo\.com/',
  225. 'https?://(www\.)?dailymotion\.com/',
  226. 'https?://dai.ly/',
  227. 'https?://(www\.)?hulu\.com/watch/',
  228. 'https?://wordpress.tv/',
  229. 'https?://(www\.)?funnyordie\.com/videos/',
  230. 'https?://vine.co/v/',
  231. 'https?://(www\.)?collegehumor\.com/video/',
  232. 'https?://(www\.|embed\.)?ted\.com/talks/'
  233. ) );
  234. // Merge patterns to run in a single preg_match call.
  235. $video_patterns = '(' . implode( '|', $video_patterns ) . ')';
  236. $is_video = preg_match( $video_patterns, $url );
  237. // If the oEmbed is a video, wrap it in the responsive wrapper.
  238. if ( false === $already_wrapped && 1 === $is_video ) {
  239. return illustratr_videos_embed_html( $html );
  240. }
  241. return $html;
  242. }