WordPressを読む 46-4 /blog/wp-includes/comment-template.php 4
2015/01/14
目次
- 1 /blog/wp-includes/comment-template.php 4
- 2 関数 comment_id_fields()
- 3 関数 comment_form_title()
- 4 Walker_Comment::クラス変数
- 5 Walker_Comment::start_lvl()
- 6 Walker_Comment::end_lvl()
- 7 Walker_Comment::display_element()
- 8 Walker_Comment::start_el()
- 9 Walker_Comment::end_el()
- 10 Walker_Comment::ping()
- 11 Walker_Comment::comment()
- 12 Walker_Comment::html5_comment()
- 13 関数 wp_list_comments()
/blog/wp-includes/comment-template.php 4
関数 comment_id_fields()
1 2 3 4 5 6 7 8 9 10 | /** * Output hidden input HTML for replying to comments. * * @since 2.7.0 * * @param int $id Optional. Post ID. Default current post ID. */ function comment_id_fields( $id = 0 ) { echo get_comment_id_fields( $id ); } |
関数 comment_form_title()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Display text based on comment reply status. * * Only affects users with Javascript disabled. * * @since 2.7.0 * * @param string $noreplytext Optional. Text to display when not replying to a comment. * Default false. * @param string $replytext Optional. Text to display when replying to a comment. * Default false. Accepts "%s" for the author of the comment * being replied to. * @param string $linktoparent Optional. Boolean to control making the author's name a link * to their comment. Default true. */ function comment_form_title( $noreplytext = false, $replytext = false, $linktoparent = true ) { global $comment; if ( false === $noreplytext ) $noreplytext = __( 'Leave a Reply' ); if ( false === $replytext ) $replytext = __( 'Leave a Reply to %s' ); $replytoid = isset($_GET['replytocom']) ? (int) $_GET['replytocom'] : 0; if ( 0 == $replytoid ) echo $noreplytext; else { $comment = get_comment($replytoid); $author = ( $linktoparent ) ? '<a href="#comment-' . get_comment_ID() . '">' . get_comment_author() . '</a>' : get_comment_author(); printf( $replytext, $author ); } } |
Walker_Comment::クラス変数
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * HTML comment list class. * * @uses Walker * @since 2.7.0 */ class Walker_Comment extends Walker { /** * What the class handles. * * @see Walker::$tree_type * * @since 2.7.0 * @var string */ var $tree_type = 'comment'; /** * DB fields to use. * * @see Walker::$db_fields * * @since 2.7.0 * @var array */ var $db_fields = array ('parent' => 'comment_parent', 'id' => 'comment_ID'); |
Walker_Comment::start_lvl()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * Start the list before the elements are added. * * @see Walker::start_lvl() * * @since 2.7.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of comment. * @param array $args Uses 'style' argument for type of HTML list. */ function start_lvl( &$output, $depth = 0, $args = array() ) { $GLOBALS['comment_depth'] = $depth + 1; switch ( $args['style'] ) { case 'div': break; case 'ol': $output .= '<ol class="children">' . "\n"; break; default: case 'ul': $output .= '<ul class="children">' . "\n"; break; } } |
Walker_Comment::end_lvl()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * End the list of items after the elements are added. * * @see Walker::end_lvl() * * @since 2.7.0 * * @param string $output Passed by reference. Used to append additional content. * @param int $depth Depth of comment. * @param array $args Will only append content if style argument value is 'ol' or 'ul'. */ function end_lvl( &$output, $depth = 0, $args = array() ) { $GLOBALS['comment_depth'] = $depth + 1; switch ( $args['style'] ) { case 'div': break; case 'ol': $output .= "</ol><!-- .children -->\n"; break; default: case 'ul': $output .= "</ul><!-- .children -->\n"; break; } } |
Walker_Comment::display_element()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** * Traverse elements to create list from elements. * * This function is designed to enhance Walker::display_element() to * display children of higher nesting levels than selected inline on * the highest depth level displayed. This prevents them being orphaned * at the end of the comment list. * * Example: max_depth = 2, with 5 levels of nested content. * 1 * 1.1 * 1.1.1 * 1.1.1.1 * 1.1.1.1.1 * 1.1.2 * 1.1.2.1 * 2 * 2.2 * * @see Walker::display_element() * @see wp_list_comments() * * @since 2.7.0 * * @param object $element Data object. * @param array $children_elements List of elements to continue traversing. * @param int $max_depth Max depth to traverse. * @param int $depth Depth of current element. * @param array $args An array of arguments. * @param string $output Passed by reference. Used to append additional content. * @return null Null on failure with no changes to parameters. */ function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { if ( !$element ) return; $id_field = $this->db_fields['id']; $id = $element->$id_field; parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); // If we're at the max depth, and the current element still has children, loop over those and display them at this level // This is to prevent them being orphaned to the end of the list. if ( $max_depth <= $depth + 1 && isset( $children_elements[$id]) ) { foreach ( $children_elements[ $id ] as $child ) $this->display_element( $child, $children_elements, $max_depth, $depth, $args, $output ); unset( $children_elements[ $id ] ); } } |
Walker_Comment::start_el()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * Start the element output. * * @since 2.7.0 * * @see Walker::start_el() * @see wp_list_comments() * * @param string $output Passed by reference. Used to append additional content. * @param object $comment Comment data object. * @param int $depth Depth of comment in reference to parents. * @param array $args An array of arguments. */ function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) { $depth++; $GLOBALS['comment_depth'] = $depth; $GLOBALS['comment'] = $comment; if ( !empty( $args['callback'] ) ) { ob_start(); call_user_func( $args['callback'], $comment, $args, $depth ); $output .= ob_get_clean(); return; } if ( ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) && $args['short_ping'] ) { ob_start(); $this->ping( $comment, $depth, $args ); $output .= ob_get_clean(); } elseif ( 'html5' === $args['format'] ) { ob_start(); $this->html5_comment( $comment, $depth, $args ); $output .= ob_get_clean(); } else { ob_start(); $this->comment( $comment, $depth, $args ); $output .= ob_get_clean(); } } |
Walker_Comment::end_el()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /** * Ends the element output, if needed. * * @since 2.7.0 * * @see Walker::end_el() * @see wp_list_comments() * * @param string $output Passed by reference. Used to append additional content. * @param object $comment The comment object. Default current comment. * @param int $depth Depth of comment. * @param array $args An array of arguments. */ function end_el( &$output, $comment, $depth = 0, $args = array() ) { if ( !empty( $args['end-callback'] ) ) { ob_start(); call_user_func( $args['end-callback'], $comment, $args, $depth ); $output .= ob_get_clean(); return; } if ( 'div' == $args['style'] ) $output .= "</div><!-- #comment-## -->\n"; else $output .= "</li><!-- #comment-## -->\n"; } |
Walker_Comment::ping()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Output a pingback comment. * * @access protected * @since 3.6.0 * * @see wp_list_comments() * * @param object $comment The comment object. * @param int $depth Depth of comment. * @param array $args An array of arguments. */ protected function ping( $comment, $depth, $args ) { $tag = ( 'div' == $args['style'] ) ? 'div' : 'li'; ?> <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>> <div class="comment-body"> <?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?> </div> <?php } |
Walker_Comment::comment()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /** * Output a single comment. * * @access protected * @since 3.6.0 * * @see wp_list_comments() * * @param object $comment Comment to display. * @param int $depth Depth of comment. * @param array $args An array of arguments. */ protected function comment( $comment, $depth, $args ) { if ( 'div' == $args['style'] ) { $tag = 'div'; $add_below = 'comment'; } else { $tag = 'li'; $add_below = 'div-comment'; } ?> <<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID(); ?>"> <?php if ( 'div' != $args['style'] ) : ?> <div id="div-comment-<?php comment_ID(); ?>" class="comment-body"> <?php endif; ?> <div class="comment-author vcard"> <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?> <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?> </div> <?php if ( '0' == $comment->comment_approved ) : ?> <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ) ?></em> <br /> <?php endif; ?> <div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>"> <?php /* translators: 1: date, 2: time */ printf( __( '%1$s at %2$s' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)' ), ' ', '' ); ?> </div> <?php comment_text( get_comment_id(), array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> <div class="reply"> <?php comment_reply_link( array_merge( $args, array( 'add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> </div> <?php if ( 'div' != $args['style'] ) : ?> </div> <?php endif; ?> <?php } |
Walker_Comment::html5_comment()
継承元::Walker
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * Output a comment in the HTML5 format. * * @access protected * @since 3.6.0 * * @see wp_list_comments() * * @param object $comment Comment to display. * @param int $depth Depth of comment. * @param array $args An array of arguments. */ protected function html5_comment( $comment, $depth, $args ) { $tag = ( 'div' === $args['style'] ) ? 'div' : 'li'; ?> <<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>> <article id="div-comment-<?php comment_ID(); ?>" class="comment-body"> <footer class="comment-meta"> <div class="comment-author vcard"> <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?> <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link() ) ); ?> </div><!-- .comment-author --> <div class="comment-metadata"> <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>"> <time datetime="<?php comment_time( 'c' ); ?>"> <?php printf( _x( '%1$s at %2$s', '1: date, 2: time' ), get_comment_date(), get_comment_time() ); ?> </time> </a> <?php edit_comment_link( __( 'Edit' ), '<span class="edit-link">', '</span>' ); ?> </div><!-- .comment-metadata --> <?php if ( '0' == $comment->comment_approved ) : ?> <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.' ); ?></p> <?php endif; ?> </footer><!-- .comment-meta --> <div class="comment-content"> <?php comment_text(); ?> </div><!-- .comment-content --> <div class="reply"> <?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> </div><!-- .reply --> </article><!-- .comment-body --> <?php } } |
Walker_Comment::クラス定義終了
継承元::Walker
関数 wp_list_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | /** * List comments. * * Used in the comments.php template to list comments for a particular post. * * @since 2.7.0 * * @see WP_Query->comments * * @param string|array $args { * Optional. Formatting options. * * @type string $walker The Walker class used to list comments. Default null. * @type int $max_depth The maximum comments depth. Default empty. * @type string $style The style of list ordering. Default 'ul'. Accepts 'ul', 'ol'. * @type string $callback Callback function to use. Default null. * @type string $end-callback Callback function to use at the end. Default null. * @type string $type Type of comments to list. * Default 'all'. Accepts 'all', 'comment', 'pingback', 'trackback', 'pings'. * @type int $page Page ID to list comments for. Default empty. * @type int $per_page Number of comments to list per page. Default empty. * @type int $avatar_size Height and width dimensions of the avatar size. Default 32. * @type string $reverse_top_level Ordering of the listed comments. Default null. Accepts 'desc', 'asc'. * @type bool $reverse_children Whether to reverse child comments in the list. Default null. * @type string $format How to format the comments list. * Default 'html5' if the theme supports it. Accepts 'html5', 'xhtml'. * @type bool $short_ping Whether to output short pings. Default false. * @type bool $echo Whether to echo the output or return it. Default true. * } * @param array $comments Optional. Array of comment objects. */ function wp_list_comments( $args = array(), $comments = null ) { global $wp_query, $comment_alt, $comment_depth, $comment_thread_alt, $overridden_cpage, $in_comment_loop; $in_comment_loop = true; $comment_alt = $comment_thread_alt = 0; $comment_depth = 1; $defaults = array( 'walker' => null, 'max_depth' => '', 'style' => 'ul', 'callback' => null, 'end-callback' => null, 'type' => 'all', 'page' => '', 'per_page' => '', 'avatar_size' => 32, 'reverse_top_level' => null, 'reverse_children' => '', 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml', 'short_ping' => false, 'echo' => true, ); $r = wp_parse_args( $args, $defaults ); // Figure out what comments we'll be looping through ($_comments) if ( null !== $comments ) { $comments = (array) $comments; if ( empty($comments) ) return; if ( 'all' != $r['type'] ) { $comments_by_type = separate_comments($comments); if ( empty($comments_by_type[$r['type']]) ) return; $_comments = $comments_by_type[$r['type']]; } else { $_comments = $comments; } } else { if ( empty($wp_query->comments) ) return; if ( 'all' != $r['type'] ) { if ( empty($wp_query->comments_by_type) ) $wp_query->comments_by_type = separate_comments($wp_query->comments); if ( empty($wp_query->comments_by_type[$r['type']]) ) return; $_comments = $wp_query->comments_by_type[$r['type']]; } else { $_comments = $wp_query->comments; } } if ( '' === $r['per_page'] && get_option('page_comments') ) $r['per_page'] = get_query_var('comments_per_page'); if ( empty($r['per_page']) ) { $r['per_page'] = 0; $r['page'] = 0; } if ( '' === $r['max_depth'] ) { if ( get_option('thread_comments') ) $r['max_depth'] = get_option('thread_comments_depth'); else $r['max_depth'] = -1; } if ( '' === $r['page'] ) { if ( empty($overridden_cpage) ) { $r['page'] = get_query_var('cpage'); } else { $threaded = ( -1 != $r['max_depth'] ); $r['page'] = ( 'newest' == get_option('default_comments_page') ) ? get_comment_pages_count($_comments, $r['per_page'], $threaded) : 1; set_query_var( 'cpage', $r['page'] ); } } // Validation check $r['page'] = intval($r['page']); if ( 0 == $r['page'] && 0 != $r['per_page'] ) $r['page'] = 1; if ( null === $r['reverse_top_level'] ) $r['reverse_top_level'] = ( 'desc' == get_option('comment_order') ); extract( $r, EXTR_SKIP ); if ( empty($walker) ) $walker = new Walker_Comment; $output = $walker->paged_walk($_comments, $max_depth, $page, $per_page, $r); $wp_query->max_num_comment_pages = $walker->max_pages; $in_comment_loop = false; if ( $r['echo'] ) echo $output; else return $output; } |