Skip to content

Commit

Permalink
Merge pull request #358 from bigbitecreative/1.8-mergeable
Browse files Browse the repository at this point in the history
1.8 mergeable
  • Loading branch information
philipjohn authored Feb 15, 2018
2 parents dbcf6f7 + 8e1d1e1 commit aa39048
Show file tree
Hide file tree
Showing 10 changed files with 1,871 additions and 751 deletions.
513 changes: 512 additions & 1 deletion assets/app.css

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions assets/app.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions assets/app.js.map

Large diffs are not rendered by default.

125 changes: 117 additions & 8 deletions classes/class-wpcom-liveblog-entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ class WPCOM_Liveblog_Entry {
* @var string In case the current entry is an edit (replaces) of
* another entry, we store the other entry's ID in this meta key.
*/
const replaces_meta_key = 'liveblog_replaces';
const replaces_meta_key = 'liveblog_replaces';

/**
* @var string If author editing is enabled, we stored contributors
* in this meta key.
*/
const contributors_meta_key = 'liveblog_contributors';

private $comment;
private $type = 'new';
Expand Down Expand Up @@ -95,9 +101,9 @@ public function get_timestamp() {

public function for_json() {
$entry_id = $this->replaces ? $this->replaces : $this->get_id();
$avatar_size = apply_filters( 'liveblog_entry_avatar_size', self::default_avatar_size );
$css_classes = implode( ' ', get_comment_class( '', $entry_id, $this->comment->comment_post_ID ) );
$css_classes = implode( ' ', get_comment_class( '', $entry_id, $this->comment->comment_post_ID ) );
$share_link = get_permalink( $this->get_post_id() ) . '#' . $entry_id;

$entry = array(
'id' => $entry_id,
'type' => $this->get_type(),
Expand All @@ -106,11 +112,12 @@ public function for_json() {
'content' => apply_filters( 'liveblog_before_edit_entry', $this->get_content() ),
'css_classes' => $css_classes,
'timestamp' => $this->get_timestamp(),
'avatar_img' => get_avatar( $this->comment->comment_author_email, $avatar_size ),
'author_link' => get_comment_author_link( $entry_id ),
'author' => self::get_user_data_for_json( self::user_object_from_comment_id( $entry_id ) ),
'contributors' => self::get_contributors_for_json( $entry_id ),
'entry_time' => get_comment_date( 'U', $entry_id ),
'share_link' => $share_link,
'share_link' => $share_link,
);

$entry = apply_filters( 'liveblog_entry_for_json', $entry, $this );
return (object) $entry;
}
Expand Down Expand Up @@ -177,11 +184,20 @@ public static function render_content( $content, $comment = false ) {
* @return WPCOM_Liveblog_Entry|WP_Error The newly inserted entry
*/
public static function insert( $args ) {
$args = apply_filters( 'liveblog_before_insert_entry', $args );
$args = apply_filters( 'liveblog_before_insert_entry', $args );

$args['user'] = self::handle_author_select( $args, false );

$comment = self::insert_comment( $args );
if ( is_wp_error( $comment ) ) {
return $comment;
}

if ( isset( $args['contributor_ids'] ) ) {
self::add_contributors( $comment->comment_ID, $args['contributor_ids'] );
}


do_action( 'liveblog_insert_entry', $comment->comment_ID, $args['post_id'] );
$entry = self::from_comment( $comment );
return $entry;
Expand All @@ -207,6 +223,12 @@ public static function update( $args ) {
return $args['user'];
}

$args['user'] = self::handle_author_select( $args, $args['entry_id'] );

if ( isset( $args['contributor_ids'] ) ) {
self::add_contributors( $args['entry_id'], $args['contributor_ids'] );
}

$args = apply_filters( 'liveblog_before_update_entry', $args );
$comment = self::insert_comment( $args );
if ( is_wp_error( $comment ) ) {
Expand Down Expand Up @@ -262,6 +284,7 @@ private static function insert_comment( $args ) {
if ( is_wp_error( $valid_args ) ) {
return $valid_args;
}

$new_comment_id = wp_insert_comment( array(
'comment_post_ID' => $args['post_id'],
'comment_content' => wp_filter_post_kses( $args['content'] ),
Expand Down Expand Up @@ -323,7 +346,7 @@ public static function handle_restricted_shortcodes( $args ) {
foreach ( self::$restricted_shortcodes as $key => $value ) {

// Regex Pattern will match all shortcode formats.
$pattern = get_shortcode_regex();
$pattern = get_shortcode_regex( array( $key ) );

// if there's a match we replace it with the configured replacement.
$args['content'] = preg_replace( '/' . $pattern . '/s', $value, $args['content'] );
Expand All @@ -333,6 +356,92 @@ public static function handle_restricted_shortcodes( $args ) {
// Return the Original entry arguments with any modifications.
return $args;
}

/**
* If author select is enabled return the user using author_id,
* if user not found then set as current user as a fallback.
*
* If a entry_id is supplied we should update it as its the
* original entry which is used for displaying author information.
*
* @param array $args The new Live blog Entry.
* @param int $entry_id If set we should update the original entry
* @return mixed
*/
private static function handle_author_select( $args, $entry_id ) {
if ( isset( $args['author_id'] ) && $args['author_id'] ) {
$user_object = get_userdata( $args['author_id'] );
if ( $user_object ) {
$args['user'] = $user_object;

if ( $entry_id ) {
wp_update_comment( array(
'comment_ID' => $entry_id,
'user_id' => $args['user']->ID,
'comment_author' => $args['user']->display_name,
'comment_author_email' => $args['user']->user_email,
'comment_author_url' => $args['user']->user_url,
) );
}
}
}

return $args['user'];
}

/**
* If author select is enabled then we store the contributors
* as comment meta.
*
* @param int $comment_id The comment id for the meta we should update.
* @param array $contributors Array of ids to store as meta.
*/
private static function add_contributors( $comment_id, $contributors ) {
if ( is_array( $contributors ) ) {
if ( metadata_exists( 'comment', $comment_id, self::contributors_meta_key ) ) {
update_comment_meta( $comment_id, self::contributors_meta_key, $contributors );
return;
}

add_comment_meta( $comment_id, self::contributors_meta_key, $contributors, true );
}
}

/**
* Returns a list of contributor user objects.
*
* @param int $comment_id The comment id to retrive the metadata.
*/
private static function get_contributors_for_json( $comment_id ) {
$contributors = get_comment_meta( $comment_id, self::contributors_meta_key, true );

if ( ! $contributors ) {
return array();
}

return array_map(function( $contributor ) {
return self::get_user_data_for_json( get_userdata( $contributor ) );
}, $contributors );
}

/**
* Returns a formatted array of user data.
*
* @param object $user The user object
*/
private static function get_user_data_for_json( $user ) {
if ( is_wp_error( $user ) ) {
return array();
}

$avatar_size = apply_filters( 'liveblog_entry_avatar_size', self::default_avatar_size );
return array(
'id' => $user->ID,
'key' => strtolower($user->user_nicename),
'name' => $user->display_name,
'avatar' => get_avatar( $user->ID, $avatar_size ),
);
}
}

WPCOM_Liveblog_Entry::generate_allowed_tags_for_entry();
15 changes: 8 additions & 7 deletions src/react/components/EntryMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import React from 'react';
import PropTypes from 'prop-types';
import { timeAgo, formattedTime } from '../utils/utils';

const EntryMeta = ({ entry }) => (
const EntryMeta = ({ entry, config }) => (
<header className="liveblog-meta">
<div className="liveblog-meta-time">
<span>{timeAgo(entry.entry_time)}</span>
<span>{formattedTime(entry.entry_time)}</span>
</div>
<a className="liveblog-meta-time" href={entry.share_link} target="_blank">
<span>{timeAgo(entry.entry_time, config.utc_offset, config.date_format)}</span>
<span>{formattedTime(entry.entry_time, config.utc_offset, config.date_format)}</span>
</a>
<div className="liveblog-meta-author">
{ entry.author.avatar &&
<div
className="liveblog-meta-authour-avatar"
className="liveblog-meta-author-avatar"
dangerouslySetInnerHTML={{ __html: entry.author.avatar }} />
}
<span className="liveblog-meta-author-name"
Expand All @@ -25,7 +25,7 @@ const EntryMeta = ({ entry }) => (
<div className="liveblog-meta-author" key={contributor.id}>
{ contributor.avatar &&
<div
className="liveblog-meta-authour-avatar"
className="liveblog-meta-author-avatar"
dangerouslySetInnerHTML={{ __html: contributor.avatar }} />
}
<span className="liveblog-meta-author-name"
Expand All @@ -41,6 +41,7 @@ const EntryMeta = ({ entry }) => (
EntryMeta.propTypes = {
entry: PropTypes.object,
authorEditEnabled: PropTypes.bool,
config: PropTypes.object,
};

export default EntryMeta;
Loading

0 comments on commit aa39048

Please sign in to comment.