PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/news/library/functions/comments.php

https://bitbucket.org/lgorence/quickpress
PHP | 233 lines | 87 code | 38 blank | 108 comment | 15 complexity | c568e0221c77f2184257515bf6033648 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * Functions for handling how comments are displayed and used on the site. This allows more precise
  4. * control over their display and makes more filter and action hooks available to developers to use in their
  5. * customizations.
  6. *
  7. * @package HybridCore
  8. * @subpackage Functions
  9. * @author Justin Tadlock <justin@justintadlock.com>
  10. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  11. * @link http://themehybrid.com/hybrid-core
  12. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. */
  14. /* Filter the comment form defaults. */
  15. add_filter( 'comment_form_defaults', 'hybrid_comment_form_args' );
  16. /* Add a few comment types to the allowed avatar comment types list. */
  17. add_filter( 'get_avatar_comment_types', 'hybrid_avatar_comment_types' );
  18. /**
  19. * Arguments for the wp_list_comments_function() used in comments.php. Users can set up a
  20. * custom comments callback function by changing $callback to the custom function. Note that
  21. * $style should remain 'ol' since this is hardcoded into the theme and is the semantically correct
  22. * element to use for listing comments.
  23. *
  24. * @since 0.7.0
  25. * @access public
  26. * @return array $args Arguments for listing comments.
  27. */
  28. function hybrid_list_comments_args() {
  29. /* Set the default arguments for listing comments. */
  30. $args = array(
  31. 'style' => 'ol',
  32. 'type' => 'all',
  33. 'avatar_size' => 80,
  34. 'callback' => 'hybrid_comments_callback',
  35. 'end-callback' => 'hybrid_comments_end_callback'
  36. );
  37. /* Return the arguments and allow devs to overwrite them. */
  38. return apply_atomic( 'list_comments_args', $args );
  39. }
  40. /**
  41. * Uses the $comment_type to determine which comment template should be used. Once the
  42. * template is located, it is loaded for use. Child themes can create custom templates based off
  43. * the $comment_type. The comment template hierarchy is comment-$comment_type.php,
  44. * comment.php.
  45. *
  46. * The templates are saved in $hybrid->comment_template[$comment_type], so each comment template
  47. * is only located once if it is needed. Following comments will use the saved template.
  48. *
  49. * @since 0.2.3
  50. * @access public
  51. * @param $comment The comment object.
  52. * @param $args Array of arguments passed from wp_list_comments().
  53. * @param $depth What level the particular comment is.
  54. * @return void
  55. */
  56. function hybrid_comments_callback( $comment, $args, $depth ) {
  57. global $hybrid;
  58. $GLOBALS['comment'] = $comment;
  59. $GLOBALS['comment_depth'] = $depth;
  60. /* Get the comment type of the current comment. */
  61. $comment_type = get_comment_type( $comment->comment_ID );
  62. /* Create an empty array if the comment template array is not set. */
  63. if ( !isset( $hybrid->comment_template) || !is_array( $hybrid->comment_template ) )
  64. $hybrid->comment_template = array();
  65. /* Check if a template has been provided for the specific comment type. If not, get the template. */
  66. if ( !isset( $hybrid->comment_template[$comment_type] ) ) {
  67. /* Create an array of template files to look for. */
  68. $templates = array( "comment-{$comment_type}.php" );
  69. /* If the comment type is a 'pingback' or 'trackback', allow the use of 'comment-ping.php'. */
  70. if ( 'pingback' == $comment_type || 'trackback' == $comment_type )
  71. $templates[] = 'comment-ping.php';
  72. /* Add the fallback 'comment.php' template. */
  73. $templates[] = 'comment.php';
  74. /* Locate the comment template. */
  75. $template = locate_template( $templates );
  76. /* Set the template in the comment template array. */
  77. $hybrid->comment_template[$comment_type] = $template;
  78. }
  79. /* If a template was found, load the template. */
  80. if ( !empty( $hybrid->comment_template[$comment_type] ) )
  81. require( $hybrid->comment_template[$comment_type] );
  82. }
  83. /**
  84. * Ends the display of individual comments. Uses the callback parameter for wp_list_comments().
  85. * Needs to be used in conjunction with hybrid_comments_callback(). Not needed but used just in
  86. * case something is changed.
  87. *
  88. * @since 0.2.3
  89. * @access public
  90. * @return void
  91. */
  92. function hybrid_comments_end_callback() {
  93. echo '</li><!-- .comment -->';
  94. }
  95. /**
  96. * Displays the avatar for the comment author and wraps it in the comment author's URL if it is
  97. * available. Adds a call to HYBRID_IMAGES . "/{$comment_type}.png" for the default avatars for
  98. * trackbacks and pingbacks.
  99. *
  100. * @since 0.2.0
  101. * @access public
  102. * @global $comment The current comment's DB object.
  103. * @global $hybrid The global Hybrid object.
  104. * @return void
  105. */
  106. function hybrid_avatar() {
  107. global $comment, $hybrid;
  108. /* Make sure avatars are allowed before proceeding. */
  109. if ( !get_option( 'show_avatars' ) )
  110. return false;
  111. /* Get/set some comment variables. */
  112. $comment_type = get_comment_type( $comment->comment_ID );
  113. $author = get_comment_author( $comment->comment_ID );
  114. $url = get_comment_author_url( $comment->comment_ID );
  115. $avatar = '';
  116. $default_avatar = '';
  117. /* Get comment types that are allowed to have an avatar. */
  118. $avatar_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
  119. /* If comment type is in the allowed list, check if it's a pingback or trackback. */
  120. if ( in_array( $comment_type, $avatar_comment_types ) ) {
  121. /* Set a default avatar for pingbacks and trackbacks. */
  122. $default_avatar = ( ( 'pingback' == $comment_type || 'trackback' == $comment_type ) ? trailingslashit( HYBRID_IMAGES ) . "{$comment_type}.png" : '' );
  123. /* Allow the default avatar to be filtered by comment type. */
  124. $default_avatar = apply_filters( "{$hybrid->prefix}_{$comment_type}_avatar", $default_avatar );
  125. }
  126. /* Set up the avatar size. */
  127. $comment_list_args = hybrid_list_comments_args();
  128. $size = ( ( $comment_list_args['avatar_size'] ) ? $comment_list_args['avatar_size'] : 80 );
  129. /* Get the avatar provided by the get_avatar() function. */
  130. $avatar = get_avatar( $comment, absint( $size ), $default_avatar, $author );
  131. /* If URL input, wrap avatar in hyperlink. */
  132. if ( !empty( $url ) && !empty( $avatar ) )
  133. $avatar = '<a href="' . esc_url( $url ) . '" rel="external nofollow" title="' . esc_attr( $author ) . '">' . $avatar . '</a>';
  134. /* Display the avatar and allow it to be filtered. Note: Use the get_avatar filter hook where possible. */
  135. echo apply_filters( "{$hybrid->prefix}_avatar", $avatar );
  136. }
  137. /**
  138. * Filters the WordPress comment_form() function that was added in WordPress 3.0. This allows
  139. * the theme to preserve some backwards compatibility with its old comment form. It also allows
  140. * users to build custom comment forms by filtering 'comment_form_defaults' in their child theme.
  141. *
  142. * @since 0.8.0
  143. * @access public
  144. * @param array $args The default comment form arguments.
  145. * @return array $args The filtered comment form arguments.
  146. */
  147. function hybrid_comment_form_args( $args ) {
  148. global $user_identity;
  149. /* Get the current commenter. */
  150. $commenter = wp_get_current_commenter();
  151. /* Create the required <span> and <input> element class. */
  152. $req = ( ( get_option( 'require_name_email' ) ) ? ' <span class="required">' . __( '*', 'hybrid-core' ) . '</span> ' : '' );
  153. $input_class = ( ( get_option( 'require_name_email' ) ) ? ' req' : '' );
  154. /* Sets up the default comment form fields. */
  155. $fields = array(
  156. 'author' => '<p class="form-author' . esc_attr( $input_class ) . '"><label for="author">' . __( 'Name', 'hybrid-core' ) . $req . '</label> <input type="text" class="text-input" name="author" id="author" value="' . esc_attr( $commenter['comment_author'] ) . '" size="40" /></p>',
  157. 'email' => '<p class="form-email' . esc_attr( $input_class ) . '"><label for="email">' . __( 'Email', 'hybrid-core' ) . $req . '</label> <input type="text" class="text-input" name="email" id="email" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="40" /></p>',
  158. 'url' => '<p class="form-url"><label for="url">' . __( 'Website', 'hybrid-core' ) . '</label><input type="text" class="text-input" name="url" id="url" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="40" /></p>'
  159. );
  160. /* Sets the default arguments for displaying the comment form. */
  161. $args = array(
  162. 'fields' => apply_filters( 'comment_form_default_fields', $fields ),
  163. 'comment_field' => '<p class="form-textarea req"><label for="comment">' . __( 'Comment', 'hybrid-core' ) . '</label><textarea name="comment" id="comment" cols="60" rows="10"></textarea></p>',
  164. 'must_log_in' => '<p class="alert">' . sprintf( __( 'You must be <a href="%1$s" title="Log in">logged in</a> to post a comment.', 'hybrid-core' ), wp_login_url( get_permalink() ) ) . '</p><!-- .alert -->',
  165. 'logged_in_as' => '<p class="log-in-out">' . sprintf( __( 'Logged in as <a href="%1$s" title="%2$s">%2$s</a>.', 'hybrid-core' ), admin_url( 'profile.php' ), esc_attr( $user_identity ) ) . ' <a href="' . wp_logout_url( get_permalink() ) . '" title="' . esc_attr__( 'Log out of this account', 'hybrid-core' ) . '">' . __( 'Log out &raquo;', 'hybrid-core' ) . '</a></p><!-- .log-in-out -->',
  166. 'comment_notes_before' => '',
  167. 'comment_notes_after' => '',
  168. 'id_form' => 'commentform',
  169. 'id_submit' => 'submit',
  170. 'title_reply' => __( 'Leave a Reply', 'hybrid-core' ),
  171. 'title_reply_to' => __( 'Leave a Reply to %s', 'hybrid-core' ),
  172. 'cancel_reply_link' => __( 'Click here to cancel reply.', 'hybrid-core' ),
  173. 'label_submit' => __( 'Post Comment', 'hybrid-core' ),
  174. );
  175. /* Return the arguments for displaying the comment form. */
  176. return $args;
  177. }
  178. /**
  179. * Adds the 'pingback' and 'trackback' comment types to the allowed list of avatar comment types. By
  180. * default, WordPress only allows the 'comment' comment type to have an avatar.
  181. *
  182. * @since 1.2.0
  183. * @access private
  184. * @param array $types List of all comment types allowed to have avatars.
  185. * @return array $types
  186. */
  187. function hybrid_avatar_comment_types( $types ) {
  188. /* Add the 'pingback' comment type. */
  189. $types[] = 'pingback';
  190. /* Add the 'trackback' comment type. */
  191. $types[] = 'trackback';
  192. /* Return the array of comment types. */
  193. return $types;
  194. }
  195. ?>