Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 127 additions & 4 deletions classes/class-wpcom-liveblog-entry-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private function get( $args = array() ) {
$args = wp_parse_args( $args, $defaults );
$comments = get_comments( $args );

return self::entries_from_comments( $comments );
return $this->entries_from_comments( $comments );
}

/**
Expand All @@ -55,7 +55,7 @@ public function get_by_id( $id ) {
if ( $comment->comment_post_ID != $this->post_id || $comment->comment_type != $this->key || $comment->comment_approved != $this->key) {
return null;
}
$entries = self::entries_from_comments( array( $comment ) );
$entries = $this->entries_from_comments( array( $comment ) );
return $entries[0];
}

Expand Down Expand Up @@ -116,6 +116,7 @@ public function has_any() {
}

private function get_all_entries_asc() {
// @todo is the caching here necessary? WP_Comment_Query::query() does caching already
$cached_entries_asc_key = $this->key . '_entries_asc_' . $this->post_id;
$cached_entries_asc = wp_cache_get( $cached_entries_asc_key, 'liveblog' );
if ( false !== $cached_entries_asc ) {
Expand All @@ -132,15 +133,33 @@ private function get_all_entries_asc() {
* @param array $comments
* @return array
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason entries_from_comments and get_reply_comments are no longer static methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjangda yes, for entries_from_comments it's so that we can access $this->post_id below. However, you're right that get_reply_comments should not be unstatic as it is still only invoked statically, and makes no reference to $this. Addressed in 5d303c8.

public static function entries_from_comments( $comments = array() ) {
public function entries_from_comments( $comments = array() ) {

// Bail if no comments
if ( empty( $comments ) )
return null;

$reply_comments = self::get_reply_comments( array( 'post_id' => $this->post_id ) );
$reply_comments_by_parent = self::group_reply_comments_by_parent( $reply_comments );

// Map each comment to a new Liveblog Entry class, so that they inherit
// some neat helper methods.
return array_map( array( 'WPCOM_Liveblog_Entry', 'from_comment' ), $comments );
$entries = array();
foreach ( $comments as $comment ) {
$reply_comments = array();

// @todo Logic here would be better encapsulated inside WPCOM_Liveblog_Entry
$underlying_entry_id = get_comment_meta( $comment->comment_ID, WPCOM_Liveblog_Entry::replaces_meta_key, true );
if ( empty( $underlying_entry_id ) ) {
$underlying_entry_id = $comment->comment_ID;
}
if ( ! empty( $reply_comments_by_parent[$underlying_entry_id] ) ) {
$reply_comments = $reply_comments_by_parent[$underlying_entry_id];
}
array_push( $entries, new WPCOM_Liveblog_Entry( $comment, $reply_comments ) );
}

return $entries;
}

/**
Expand Down Expand Up @@ -174,4 +193,108 @@ public static function assoc_array_by_id( $entries ) {

return $result;
}

/**
* Get the liveblog reply comments for this post
* @param array|string $args
*/
public static function get_reply_comments( $args = array() ) {
$defaults = array(
'orderby' => 'comment_date_gmt',
'order' => 'DESC',
'type' => WPCOM_Liveblog::reply_comment_type,
);
$args = wp_parse_args( $args, $defaults );
$comments = get_comments( $args );
$comments = self::filter_out_undisplayable_comments( $comments );
return $comments;
}

/**
* Remove comments that are not approved or ones which are unapproved and
* yet not authored by the current commenter
* @see comments_template()
*/
private static function filter_out_undisplayable_comments( array $comments ) {
global $user_ID;

/**
* Comment author information fetched from the comment cookies.
*
* @uses wp_get_current_commenter()
*/
$commenter = wp_get_current_commenter();

/**
* The name of the current comment author escaped for use in attributes.
*/
$comment_author = $commenter['comment_author']; // Escaped by sanitize_comment_cookies()

/**
* The email address of the current comment author escaped for use in attributes.
*/
$comment_author_email = $commenter['comment_author_email']; // Escaped by sanitize_comment_cookies()

$displayable_comments = array();
foreach ( $comments as $comment ) {
$is_displayable = (
$comment->comment_approved == '1'
||
(
$user_ID
&&
$comment->comment_approved == '0'
&&
$comment->user_id == $user_ID
)
||
(
! empty( $comment_author )
&&
$comment->comment_approved == '0'
&&
$comment->comment_author == wp_specialchars_decode( $comment_author,ENT_QUOTES )
&&
$comment->comment_author_email == $comment_author_email
)
);
if ( $is_displayable ) {
array_push( $displayable_comments, $comment );
}
}
return $displayable_comments;
}

/**
* Organize comments into an associative array keyed by the comment_parent
* @param array $comments
* @return array
*/
public static function group_reply_comments_by_parent( array $comments ) {
$grouped_comments = array();
foreach ( $comments as $comment ) {
if ( ! isset( $grouped_comments[$comment->comment_parent] ) ) {
$grouped_comments[$comment->comment_parent] = array();
}
array_push( $grouped_comments[$comment->comment_parent], $comment );
}
return $grouped_comments;
}

/**
* Remove all comments that do not have the comment_parent of $entry_id
* @param int $entry_id
* @param array $comments
* return array
*/
public static function filter_reply_comments_by_parent( $entry_id, array $comments ) {
$filtered_comments = array();
foreach ( $comments as $comment ) {
if ( $comment->comment_parent == $entry_id ) {
array_push( $filtered_comments, $comment );
}
}
return $filtered_comments;
}

}
32 changes: 24 additions & 8 deletions classes/class-wpcom-liveblog-entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ class WPCOM_Liveblog_Entry {
const replaces_meta_key = 'liveblog_replaces';

private $comment;
private $reply_comments;
private $type = 'new';

public function __construct( $comment ) {
public function __construct( $comment, $reply_comments = array() ) {
$this->comment = $comment;
$this->reply_comments = $reply_comments;
$this->replaces = get_comment_meta( $comment->comment_ID, self::replaces_meta_key, true );
if ( $this->replaces && $this->get_content() ) {
$this->type = 'update';
Expand All @@ -27,8 +29,8 @@ public function __construct( $comment ) {
}
}

public static function from_comment( $comment ) {
$entry = new WPCOM_Liveblog_Entry( $comment );
public static function from_comment( $comment, $reply_comments = array() ) {
$entry = new WPCOM_Liveblog_Entry( $comment, $reply_comments );
return $entry;
}

Expand Down Expand Up @@ -70,10 +72,15 @@ public function get_fields_for_render() {
$post_id = $this->comment->comment_post_ID;
$avatar_size = apply_filters( 'liveblog_entry_avatar_size', self::default_avatar_size );

$underlying_entry_id = $entry_id;
if ( $this->replaces ) {
$underlying_entry_id = $this->replaces;
}

$entry = array(
'entry_id' => $entry_id,
'post_id' => $entry_id,
'css_classes' => comment_class( '', $entry_id, $post_id, false ),
'entry_id' => $underlying_entry_id,
'post_id' => $post_id,
'css_classes' => comment_class( '', $underlying_entry_id, $post_id, false ),
'content' => self::render_content( get_comment_text( $entry_id ), $this->comment ),
'original_content' => get_comment_text( $entry_id ),
'avatar_size' => $avatar_size,
Expand All @@ -83,6 +90,8 @@ public function get_fields_for_render() {
'entry_time' => get_comment_date( get_option('time_format'), $entry_id ),
'timestamp' => $this->get_timestamp(),
'is_liveblog_editable' => WPCOM_Liveblog::is_liveblog_editable(),
'is_liveblog_commenting_open' => ( 'open' === WPCOM_Liveblog::get_liveblog_comment_status( $post_id) ),
'reply_comments' => $this->reply_comments,
);

return $entry;
Expand All @@ -98,7 +107,6 @@ public function render() {
return $output;

$entry = $this->get_fields_for_render();

$entry = apply_filters( 'liveblog_entry_template_variables', $entry );

return WPCOM_Liveblog::get_template_part( 'liveblog-single-entry.php', $entry );
Expand Down Expand Up @@ -164,7 +172,15 @@ public static function update( $args ) {
'comment_ID' => $args['entry_id'],
'comment_content' => wp_filter_post_kses( $args['content'] ),
) );
$entry = self::from_comment( $comment );

// Include reply comments in response
// @todo Better to encapsulate inside of WPCOM_Liveblog_Entry
$reply_comments = WPCOM_Liveblog_Entry_Query::get_reply_comments( array(
'parent' => $args['entry_id'],
'post_id' => $args['post_id'],
) );

$entry = self::from_comment( $comment, $reply_comments );
return $entry;
}

Expand Down
118 changes: 118 additions & 0 deletions css/liveblog.css
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ a.liveblog-form-entry::-webkit-input-placeholder {
.liveblog-actions .liveblog-submit-wrapper .liveblog-entry-delete {
float: right;
}
.liveblog-actions .liveblog-submit-wrapper .liveblog-entry-reply {
display: none;
}
/* Remove the dotted border on :focus and the extra padding in Firefox */
#liveblog-container .button-secondary::-moz-focus-inner {
border-width: 1px 0;
Expand Down Expand Up @@ -364,3 +367,118 @@ a.liveblog-form-entry::-webkit-input-placeholder {
display: inline-block;
vertical-align: middle;
}

/* =Reply Comments
-------------------------------------------------------------- */

#liveblog-entries #respond,
.liveblog-reply-comments {
margin-left: 40px;
margin-bottom: 1em;
}
.liveblog-reply-comments[hidden] {
display: none;
}

.liveblog-reply-comments ol {
display: none;
}
.liveblog-reply-comments[open] ol {
border-left: solid 1px #ccc;
display: block;
}
.liveblog-reply-comments summary {
font-weight: bold;
font-size: larger;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: pointer;
}
.liveblog-reply-comments summary:hover {
text-decoration: underline;
}

.liveblog-reply-comments .comments-title {
margin-bottom: 48px;
font-size: 16px;
line-height: 1.5;
font-weight: normal;
}
.liveblog-reply-comments .comment {
margin: 24px 0;
}
.liveblog-reply-comments article header {
margin: 0 0 10px;
overflow: hidden;
position: relative;
}
.liveblog-reply-comments article header img {
float: left;
padding: 0;
line-height: 0;
}
.liveblog-reply-comments article header cite,
.liveblog-reply-comments article header time {
display: block;
margin-left: 60px;
}
.liveblog-reply-comments article header cite {
font-style: normal;
font-size: 15px;
line-height: 1.42857143;
}
.liveblog-reply-comments article header time {
line-height: 1.714285714;
text-decoration: none;
font-size: 12px;
color: #5e5e5e;
}
.liveblog-reply-comments article header a {
text-decoration: none;
color: #5e5e5e;
}
.liveblog-reply-comments article header a:hover {
color: #21759b;
}
.liveblog-reply-comments article header cite a {
color: #444;
}
.liveblog-reply-comments article header cite a:hover {
text-decoration: underline;
}
.liveblog-reply-comments article header h4 {
position: absolute;
top: 0;
right: 0;
padding: 6px 12px;
font-size: 12px;
font-weight: normal;
color: #fff;
background-color: #0088d0;
background-repeat: repeat-x;
background-image: -moz-linear-gradient(top, #009cee, #0088d0);
background-image: -ms-linear-gradient(top, #009cee, #0088d0);
background-image: -webkit-linear-gradient(top, #009cee, #0088d0);
background-image: -o-linear-gradient(top, #009cee, #0088d0);
background-image: linear-gradient(top, #009cee, #0088d0);
border-radius: 3px;
border: 1px solid #007cbd;
}
.liveblog-reply-comments li.bypostauthor cite span {
position: absolute;
margin-left: 5px;
padding: 2px 5px;
font-size: 10px;
}
.liveblog-reply-comments a.comment-reply-link,
.liveblog-reply-comments a.comment-edit-link {
color: #686868;
font-size: 13px;
line-height: 1.846153846;
}
.liveblog-reply-comments a.comment-reply-link:hover,
.liveblog-reply-comments a.comment-edit-link:hover {
color: #21759b;
}
11 changes: 9 additions & 2 deletions js/liveblog-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ jQuery( function( $ ) {
};
$meta_box.on( 'click', 'button', function( e ) {
e.preventDefault();
var url = ajaxurl + '?action=set_liveblog_state_for_post&post_id=' + encodeURIComponent( post_id ) + '&state=' + encodeURIComponent( $( this ).val() ) + '&' + liveblog_admin_settings.nonce_key + '=' + liveblog_admin_settings.nonce;
$( '.inside', $meta_box ).load( url, function( response, status, xhr ) {

var data = {
action: 'set_liveblog_state_for_post',
post_id: post_id
};
data[liveblog_admin_settings.nonce_key] = liveblog_admin_settings.nonce;
data[ $(this).attr('name') ] = $(this).val();

$( '.inside', $meta_box ).load( ajaxurl, data, function( response, status, xhr ) {
if ( status === 'success') {
return;
}
Expand Down
Loading