extras.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Independent_Publisher_2
  8. */
  9. /**
  10. * Adds custom classes to the array of body classes.
  11. *
  12. * @param array $classes Classes for the body element.
  13. * @return array
  14. */
  15. function independent_publisher_2_body_classes( $classes ) {
  16. // Adds a class of group-blog to blogs with more than 1 published author.
  17. if ( is_multi_author() ) {
  18. $classes[] = 'group-blog';
  19. }
  20. // Adds a class if are no widgets to render in main sidebar.
  21. if ( is_active_sidebar( 'sidebar-1' ) && ! is_404() ) {
  22. $classes[] = 'has-sidebar';
  23. }
  24. $is_wc_product_page = false;
  25. if ( class_exists( 'WooCommerce' ) && is_product() ) {
  26. $is_wc_product_page = true;
  27. }
  28. if ( independent_publisher_2_has_header_image() ||
  29. ( independent_publisher_2_has_cover_image() && 1 == independent_publisher_2_jetpack_featured_image_display() && ! $is_wc_product_page ) ) {
  30. $classes[] = 'has-header-image';
  31. }
  32. // Adds a gravatar-logo-disabled when gravatar display is disabled.
  33. if ( false === get_theme_mod( 'independent_publisher_2_display_gravatar', true ) ) {
  34. $classes[] = 'gravatar-logo-disabled';
  35. }
  36. return $classes;
  37. }
  38. add_filter( 'body_class', 'independent_publisher_2_body_classes' );
  39. /**
  40. * Adds custom classes to the array of post classes.
  41. *
  42. * @param array $classes Classes for the post
  43. * @return array
  44. */
  45. function independent_publisher_2_post_class( $classes ) {
  46. $content = get_post_field( 'post_content', get_the_ID() );
  47. if ( empty( $content ) ) {
  48. $classes[] = 'empty-content';
  49. }
  50. return $classes;
  51. }
  52. add_filter( 'post_class', 'independent_publisher_2_post_class' );
  53. /**
  54. * Add a pingback url auto-discovery header for singularly identifiable articles.
  55. */
  56. function independent_publisher_2_pingback_header() {
  57. if ( is_singular() && pings_open() ) {
  58. echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  59. }
  60. }
  61. add_action( 'wp_head', 'independent_publisher_2_pingback_header' );
  62. /**
  63. * Utility function to check if a gravatar exists for a given email or id
  64. * @param int|string|object $id_or_email A user ID, email address, or comment object
  65. * @return bool if the gravatar exists or not
  66. * @link https://gist.github.com/justinph/5197810
  67. */
  68. function validate_gravatar( $id_or_email ) {
  69. // The id or email code is borrowed from wp-includes/pluggable.php.
  70. $email = '';
  71. if ( is_numeric( $id_or_email ) ) {
  72. $id = (int) $id_or_email;
  73. $user = get_userdata( $id );
  74. if ( $user ) {
  75. $email = $user->user_email;
  76. }
  77. } elseif ( is_object( $id_or_email ) ) {
  78. // No avatar for pingbacks or trackbacks.
  79. $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  80. if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types, true ) ) {
  81. return false;
  82. }
  83. if ( ! empty( $id_or_email->user_id ) ) {
  84. $id = (int) $id_or_email->user_id;
  85. $user = get_userdata( $id );
  86. if ( $user ) {
  87. $email = $user->user_email;
  88. }
  89. } elseif ( ! empty( $id_or_email->comment_author_email ) ) {
  90. $email = $id_or_email->comment_author_email;
  91. }
  92. } else {
  93. $email = $id_or_email;
  94. }
  95. $hashkey = md5( strtolower( trim( $email ) ) );
  96. $uri = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404';
  97. $data = wp_cache_get( $hashkey );
  98. $expire = 60 * 5;
  99. $group = '';
  100. if ( false === $data ) {
  101. $response = wp_remote_head( $uri );
  102. if ( is_wp_error( $response ) ) {
  103. $data = 'not200';
  104. } else {
  105. $data = $response['response']['code'];
  106. }
  107. wp_cache_set( $hashkey, $data, $group, $expire );
  108. }
  109. if ( 200 === $data ) {
  110. return true;
  111. } else {
  112. return false;
  113. }
  114. }