Skip to content

Commit aa39048

Browse files
authored
Merge pull request #358 from bigbitecreative/1.8-mergeable
1.8 mergeable
2 parents dbcf6f7 + 8e1d1e1 commit aa39048

File tree

10 files changed

+1871
-751
lines changed

10 files changed

+1871
-751
lines changed

assets/app.css

Lines changed: 512 additions & 1 deletion
Large diffs are not rendered by default.

assets/app.js

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/app.js.map

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

classes/class-wpcom-liveblog-entry.php

Lines changed: 117 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ class WPCOM_Liveblog_Entry {
1111
* @var string In case the current entry is an edit (replaces) of
1212
* another entry, we store the other entry's ID in this meta key.
1313
*/
14-
const replaces_meta_key = 'liveblog_replaces';
14+
const replaces_meta_key = 'liveblog_replaces';
15+
16+
/**
17+
* @var string If author editing is enabled, we stored contributors
18+
* in this meta key.
19+
*/
20+
const contributors_meta_key = 'liveblog_contributors';
1521

1622
private $comment;
1723
private $type = 'new';
@@ -95,9 +101,9 @@ public function get_timestamp() {
95101

96102
public function for_json() {
97103
$entry_id = $this->replaces ? $this->replaces : $this->get_id();
98-
$avatar_size = apply_filters( 'liveblog_entry_avatar_size', self::default_avatar_size );
99-
$css_classes = implode( ' ', get_comment_class( '', $entry_id, $this->comment->comment_post_ID ) );
104+
$css_classes = implode( ' ', get_comment_class( '', $entry_id, $this->comment->comment_post_ID ) );
100105
$share_link = get_permalink( $this->get_post_id() ) . '#' . $entry_id;
106+
101107
$entry = array(
102108
'id' => $entry_id,
103109
'type' => $this->get_type(),
@@ -106,11 +112,12 @@ public function for_json() {
106112
'content' => apply_filters( 'liveblog_before_edit_entry', $this->get_content() ),
107113
'css_classes' => $css_classes,
108114
'timestamp' => $this->get_timestamp(),
109-
'avatar_img' => get_avatar( $this->comment->comment_author_email, $avatar_size ),
110-
'author_link' => get_comment_author_link( $entry_id ),
115+
'author' => self::get_user_data_for_json( self::user_object_from_comment_id( $entry_id ) ),
116+
'contributors' => self::get_contributors_for_json( $entry_id ),
111117
'entry_time' => get_comment_date( 'U', $entry_id ),
112-
'share_link' => $share_link,
118+
'share_link' => $share_link,
113119
);
120+
114121
$entry = apply_filters( 'liveblog_entry_for_json', $entry, $this );
115122
return (object) $entry;
116123
}
@@ -177,11 +184,20 @@ public static function render_content( $content, $comment = false ) {
177184
* @return WPCOM_Liveblog_Entry|WP_Error The newly inserted entry
178185
*/
179186
public static function insert( $args ) {
180-
$args = apply_filters( 'liveblog_before_insert_entry', $args );
187+
$args = apply_filters( 'liveblog_before_insert_entry', $args );
188+
189+
$args['user'] = self::handle_author_select( $args, false );
190+
181191
$comment = self::insert_comment( $args );
182192
if ( is_wp_error( $comment ) ) {
183193
return $comment;
184194
}
195+
196+
if ( isset( $args['contributor_ids'] ) ) {
197+
self::add_contributors( $comment->comment_ID, $args['contributor_ids'] );
198+
}
199+
200+
185201
do_action( 'liveblog_insert_entry', $comment->comment_ID, $args['post_id'] );
186202
$entry = self::from_comment( $comment );
187203
return $entry;
@@ -207,6 +223,12 @@ public static function update( $args ) {
207223
return $args['user'];
208224
}
209225

226+
$args['user'] = self::handle_author_select( $args, $args['entry_id'] );
227+
228+
if ( isset( $args['contributor_ids'] ) ) {
229+
self::add_contributors( $args['entry_id'], $args['contributor_ids'] );
230+
}
231+
210232
$args = apply_filters( 'liveblog_before_update_entry', $args );
211233
$comment = self::insert_comment( $args );
212234
if ( is_wp_error( $comment ) ) {
@@ -262,6 +284,7 @@ private static function insert_comment( $args ) {
262284
if ( is_wp_error( $valid_args ) ) {
263285
return $valid_args;
264286
}
287+
265288
$new_comment_id = wp_insert_comment( array(
266289
'comment_post_ID' => $args['post_id'],
267290
'comment_content' => wp_filter_post_kses( $args['content'] ),
@@ -323,7 +346,7 @@ public static function handle_restricted_shortcodes( $args ) {
323346
foreach ( self::$restricted_shortcodes as $key => $value ) {
324347

325348
// Regex Pattern will match all shortcode formats.
326-
$pattern = get_shortcode_regex();
349+
$pattern = get_shortcode_regex( array( $key ) );
327350

328351
// if there's a match we replace it with the configured replacement.
329352
$args['content'] = preg_replace( '/' . $pattern . '/s', $value, $args['content'] );
@@ -333,6 +356,92 @@ public static function handle_restricted_shortcodes( $args ) {
333356
// Return the Original entry arguments with any modifications.
334357
return $args;
335358
}
359+
360+
/**
361+
* If author select is enabled return the user using author_id,
362+
* if user not found then set as current user as a fallback.
363+
*
364+
* If a entry_id is supplied we should update it as its the
365+
* original entry which is used for displaying author information.
366+
*
367+
* @param array $args The new Live blog Entry.
368+
* @param int $entry_id If set we should update the original entry
369+
* @return mixed
370+
*/
371+
private static function handle_author_select( $args, $entry_id ) {
372+
if ( isset( $args['author_id'] ) && $args['author_id'] ) {
373+
$user_object = get_userdata( $args['author_id'] );
374+
if ( $user_object ) {
375+
$args['user'] = $user_object;
376+
377+
if ( $entry_id ) {
378+
wp_update_comment( array(
379+
'comment_ID' => $entry_id,
380+
'user_id' => $args['user']->ID,
381+
'comment_author' => $args['user']->display_name,
382+
'comment_author_email' => $args['user']->user_email,
383+
'comment_author_url' => $args['user']->user_url,
384+
) );
385+
}
386+
}
387+
}
388+
389+
return $args['user'];
390+
}
391+
392+
/**
393+
* If author select is enabled then we store the contributors
394+
* as comment meta.
395+
*
396+
* @param int $comment_id The comment id for the meta we should update.
397+
* @param array $contributors Array of ids to store as meta.
398+
*/
399+
private static function add_contributors( $comment_id, $contributors ) {
400+
if ( is_array( $contributors ) ) {
401+
if ( metadata_exists( 'comment', $comment_id, self::contributors_meta_key ) ) {
402+
update_comment_meta( $comment_id, self::contributors_meta_key, $contributors );
403+
return;
404+
}
405+
406+
add_comment_meta( $comment_id, self::contributors_meta_key, $contributors, true );
407+
}
408+
}
409+
410+
/**
411+
* Returns a list of contributor user objects.
412+
*
413+
* @param int $comment_id The comment id to retrive the metadata.
414+
*/
415+
private static function get_contributors_for_json( $comment_id ) {
416+
$contributors = get_comment_meta( $comment_id, self::contributors_meta_key, true );
417+
418+
if ( ! $contributors ) {
419+
return array();
420+
}
421+
422+
return array_map(function( $contributor ) {
423+
return self::get_user_data_for_json( get_userdata( $contributor ) );
424+
}, $contributors );
425+
}
426+
427+
/**
428+
* Returns a formatted array of user data.
429+
*
430+
* @param object $user The user object
431+
*/
432+
private static function get_user_data_for_json( $user ) {
433+
if ( is_wp_error( $user ) ) {
434+
return array();
435+
}
436+
437+
$avatar_size = apply_filters( 'liveblog_entry_avatar_size', self::default_avatar_size );
438+
return array(
439+
'id' => $user->ID,
440+
'key' => strtolower($user->user_nicename),
441+
'name' => $user->display_name,
442+
'avatar' => get_avatar( $user->ID, $avatar_size ),
443+
);
444+
}
336445
}
337446

338447
WPCOM_Liveblog_Entry::generate_allowed_tags_for_entry();

src/react/components/EntryMeta.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import { timeAgo, formattedTime } from '../utils/utils';
44

5-
const EntryMeta = ({ entry }) => (
5+
const EntryMeta = ({ entry, config }) => (
66
<header className="liveblog-meta">
7-
<div className="liveblog-meta-time">
8-
<span>{timeAgo(entry.entry_time)}</span>
9-
<span>{formattedTime(entry.entry_time)}</span>
10-
</div>
7+
<a className="liveblog-meta-time" href={entry.share_link} target="_blank">
8+
<span>{timeAgo(entry.entry_time, config.utc_offset, config.date_format)}</span>
9+
<span>{formattedTime(entry.entry_time, config.utc_offset, config.date_format)}</span>
10+
</a>
1111
<div className="liveblog-meta-author">
1212
{ entry.author.avatar &&
1313
<div
14-
className="liveblog-meta-authour-avatar"
14+
className="liveblog-meta-author-avatar"
1515
dangerouslySetInnerHTML={{ __html: entry.author.avatar }} />
1616
}
1717
<span className="liveblog-meta-author-name"
@@ -25,7 +25,7 @@ const EntryMeta = ({ entry }) => (
2525
<div className="liveblog-meta-author" key={contributor.id}>
2626
{ contributor.avatar &&
2727
<div
28-
className="liveblog-meta-authour-avatar"
28+
className="liveblog-meta-author-avatar"
2929
dangerouslySetInnerHTML={{ __html: contributor.avatar }} />
3030
}
3131
<span className="liveblog-meta-author-name"
@@ -41,6 +41,7 @@ const EntryMeta = ({ entry }) => (
4141
EntryMeta.propTypes = {
4242
entry: PropTypes.object,
4343
authorEditEnabled: PropTypes.bool,
44+
config: PropTypes.object,
4445
};
4546

4647
export default EntryMeta;

0 commit comments

Comments
 (0)