]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-walker-comment.php
WordPress 4.4
[autoinstalls/wordpress.git] / wp-includes / class-walker-comment.php
1 <?php
2 /**
3  * Comment API: Walker_Comment class
4  *
5  * @package WordPress
6  * @subpackage Comments
7  * @since 4.4.0
8  */
9
10 /**
11  * HTML comment list class.
12  *
13  * @uses Walker
14  * @since 2.7.0
15  */
16 class Walker_Comment extends Walker {
17         /**
18          * What the class handles.
19          *
20          * @see Walker::$tree_type
21          *
22          * @since 2.7.0
23          * @var string
24          */
25         public $tree_type = 'comment';
26
27         /**
28          * DB fields to use.
29          *
30          * @see Walker::$db_fields
31          *
32          * @since 2.7.0
33          * @var array
34          */
35         public $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID');
36
37         /**
38          * Start the list before the elements are added.
39          *
40          * @see Walker::start_lvl()
41          *
42          * @since 2.7.0
43          *
44          * @global int $comment_depth
45          *
46          * @param string $output Passed by reference. Used to append additional content.
47          * @param int $depth Depth of comment.
48          * @param array $args Uses 'style' argument for type of HTML list.
49          */
50         public function start_lvl( &$output, $depth = 0, $args = array() ) {
51                 $GLOBALS['comment_depth'] = $depth + 1;
52
53                 switch ( $args['style'] ) {
54                         case 'div':
55                                 break;
56                         case 'ol':
57                                 $output .= '<ol class="children">' . "\n";
58                                 break;
59                         case 'ul':
60                         default:
61                                 $output .= '<ul class="children">' . "\n";
62                                 break;
63                 }
64         }
65
66         /**
67          * End the list of items after the elements are added.
68          *
69          * @see Walker::end_lvl()
70          *
71          * @since 2.7.0
72          *
73          * @global int $comment_depth
74          *
75          * @param string $output Passed by reference. Used to append additional content.
76          * @param int    $depth  Depth of comment.
77          * @param array  $args   Will only append content if style argument value is 'ol' or 'ul'.
78          */
79         public function end_lvl( &$output, $depth = 0, $args = array() ) {
80                 $GLOBALS['comment_depth'] = $depth + 1;
81
82                 switch ( $args['style'] ) {
83                         case 'div':
84                                 break;
85                         case 'ol':
86                                 $output .= "</ol><!-- .children -->\n";
87                                 break;
88                         case 'ul':
89                         default:
90                                 $output .= "</ul><!-- .children -->\n";
91                                 break;
92                 }
93         }
94
95         /**
96          * Traverse elements to create list from elements.
97          *
98          * This function is designed to enhance Walker::display_element() to
99          * display children of higher nesting levels than selected inline on
100          * the highest depth level displayed. This prevents them being orphaned
101          * at the end of the comment list.
102          *
103          * Example: max_depth = 2, with 5 levels of nested content.
104          *     1
105          *      1.1
106          *        1.1.1
107          *        1.1.1.1
108          *        1.1.1.1.1
109          *        1.1.2
110          *        1.1.2.1
111          *     2
112          *      2.2
113          *
114          * @see Walker::display_element()
115          * @see wp_list_comments()
116          *
117          * @since 2.7.0
118          *
119          * @param object $element           Data object.
120          * @param array  $children_elements List of elements to continue traversing.
121          * @param int    $max_depth         Max depth to traverse.
122          * @param int    $depth             Depth of current element.
123          * @param array  $args              An array of arguments.
124          * @param string $output            Passed by reference. Used to append additional content.
125          */
126         public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
127                 if ( !$element )
128                         return;
129
130                 $id_field = $this->db_fields['id'];
131                 $id = $element->$id_field;
132
133                 parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
134
135                 /*
136                  * If we're at the max depth, and the current element still has children,
137                  * loop over those and display them at this level. This is to prevent them
138                  * being orphaned to the end of the list.
139                  */
140                 if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) {
141                         foreach ( $children_elements[ $id ] as $child )
142                                 $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output );
143
144                         unset( $children_elements[ $id ] );
145                 }
146
147         }
148
149         /**
150          * Start the element output.
151          *
152          * @since 2.7.0
153          *
154          * @see Walker::start_el()
155          * @see wp_list_comments()
156          *
157          * @global int        $comment_depth
158          * @global WP_Comment $comment
159          *
160          * @param string $output  Passed by reference. Used to append additional content.
161          * @param object $comment Comment data object.
162          * @param int    $depth   Depth of comment in reference to parents.
163          * @param array  $args    An array of arguments.
164          */
165         public function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
166                 $depth++;
167                 $GLOBALS['comment_depth'] = $depth;
168                 $GLOBALS['comment'] = $comment;
169
170                 if ( !empty( $args['callback'] ) ) {
171                         ob_start();
172                         call_user_func( $args['callback'], $comment, $args, $depth );
173                         $output .= ob_get_clean();
174                         return;
175                 }
176
177                 if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) {
178                         ob_start();
179                         $this->ping( $comment, $depth, $args );
180                         $output .= ob_get_clean();
181                 } elseif ( 'html5' === $args['format'] ) {
182                         ob_start();
183                         $this->html5_comment( $comment, $depth, $args );
184                         $output .= ob_get_clean();
185                 } else {
186                         ob_start();
187                         $this->comment( $comment, $depth, $args );
188                         $output .= ob_get_clean();
189                 }
190         }
191
192         /**
193          * Ends the element output, if needed.
194          *
195          * @since 2.7.0
196          *
197          * @see Walker::end_el()
198          * @see wp_list_comments()
199          *
200          * @param string     $output  Passed by reference. Used to append additional content.
201          * @param WP_Comment $comment The comment object. Default current comment.
202          * @param int        $depth   Depth of comment.
203          * @param array      $args    An array of arguments.
204          */
205         public function end_el( &$output, $comment, $depth = 0, $args = array() ) {
206                 if ( !empty( $args['end-callback'] ) ) {
207                         ob_start();
208                         call_user_func( $args['end-callback'], $comment, $args, $depth );
209                         $output .= ob_get_clean();
210                         return;
211                 }
212                 if ( 'div' == $args['style'] )
213                         $output .= "</div><!-- #comment-## -->\n";
214                 else
215                         $output .= "</li><!-- #comment-## -->\n";
216         }
217
218         /**
219          * Output a pingback comment.
220          *
221          * @access protected
222          * @since 3.6.0
223          *
224          * @see wp_list_comments()
225          *
226          * @param WP_Comment $comment The comment object.
227          * @param int        $depth   Depth of comment.
228          * @param array      $args    An array of arguments.
229          */
230         protected function ping( $comment, $depth, $args ) {
231                 $tag = ( 'div' == $args['style'] ) ? 'div' : 'li';
232 ?>
233                 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( '', $comment ); ?>>
234                         <div class="comment-body">
235                                 <?php _e( 'Pingback:' ); ?> <?php comment_author_link( $comment ); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
236                         </div>
237 <?php
238         }
239
240         /**
241          * Output a single comment.
242          *
243          * @access protected
244          * @since 3.6.0
245          *
246          * @see wp_list_comments()
247          *
248          * @param object $comment Comment to display.
249          * @param int    $depth   Depth of comment.
250          * @param array  $args    An array of arguments.
251          */
252         protected function comment( $comment, $depth, $args ) {
253                 if ( 'div' == $args['style'] ) {
254                         $tag = 'div';
255                         $add_below = 'comment';
256                 } else {
257                         $tag = 'li';
258                         $add_below = 'div-comment';
259                 }
260 ?>
261                 <<?php echo $tag; ?> <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?> id="comment-<?php comment_ID(); ?>">
262                 <?php if ( 'div' != $args['style'] ) : ?>
263                 <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
264                 <?php endif; ?>
265                 <div class="comment-author vcard">
266                         <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
267                         <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link( $comment ) ); ?>
268                 </div>
269                 <?php if ( '0' == $comment->comment_approved ) : ?>
270                 <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em>
271                 <br />
272                 <?php endif; ?>
273
274                 <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
275                         <?php
276                                 /* translators: 1: comment date, 2: comment time */
277                                 printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), '&nbsp;&nbsp;', '' );
278                         ?>
279                 </div>
280
281                 <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
282
283                 <?php
284                 comment_reply_link( array_merge( $args, array(
285                         'add_below' => $add_below,
286                         'depth'     => $depth,
287                         'max_depth' => $args['max_depth'],
288                         'before'    => '<div class="reply">',
289                         'after'     => '</div>'
290                 ) ) );
291                 ?>
292
293                 <?php if ( 'div' != $args['style'] ) : ?>
294                 </div>
295                 <?php endif; ?>
296 <?php
297         }
298
299         /**
300          * Output a comment in the HTML5 format.
301          *
302          * @access protected
303          * @since 3.6.0
304          *
305          * @see wp_list_comments()
306          *
307          * @param object $comment Comment to display.
308          * @param int    $depth   Depth of comment.
309          * @param array  $args    An array of arguments.
310          */
311         protected function html5_comment( $comment, $depth, $args ) {
312                 $tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
313 ?>
314                 <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
315                         <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
316                                 <footer class="comment-meta">
317                                         <div class="comment-author vcard">
318                                                 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
319                                                 <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link( $comment ) ) ); ?>
320                                         </div><!-- .comment-author -->
321
322                                         <div class="comment-metadata">
323                                                 <a href="<?php echo esc_url( get_comment_link( $comment, $args ) ); ?>">
324                                                         <time datetime="<?php comment_time( 'c' ); ?>">
325                                                                 <?php
326                                                                         /* translators: 1: comment date, 2: comment time */
327                                                                         printf( __( '%1$s at %2$s' ), get_comment_date( '', $comment ), get_comment_time() );
328                                                                 ?>
329                                                         </time>
330                                                 </a>
331                                                 <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?>
332                                         </div><!-- .comment-metadata -->
333
334                                         <?php if ( '0' == $comment->comment_approved ) : ?>
335                                         <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p>
336                                         <?php endif; ?>
337                                 </footer><!-- .comment-meta -->
338
339                                 <div class="comment-content">
340                                         <?php comment_text(); ?>
341                                 </div><!-- .comment-content -->
342
343                                 <?php
344                                 comment_reply_link( array_merge( $args, array(
345                                         'add_below' => 'div-comment',
346                                         'depth'     => $depth,
347                                         'max_depth' => $args['max_depth'],
348                                         'before'    => '<div class="reply">',
349                                         'after'     => '</div>'
350                                 ) ) );
351                                 ?>
352                         </article><!-- .comment-body -->
353 <?php
354         }
355 }