@@ -13,6 +13,17 @@ class WPCOM_Liveblog_Entry {
1313 */
1414 const replaces_meta_key = 'liveblog_replaces ' ;
1515
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 ' ;
21+
22+ /**
23+ * @var string Whether or not an entry should show an author
24+ */
25+ const hide_authors_key = 'liveblog_hide_authors ' ;
26+
1627 private $ comment ;
1728 private $ type = 'new ' ;
1829 private static $ allowed_tags_for_entry ;
@@ -97,19 +108,20 @@ public function get_timestamp() {
97108
98109 public function for_json () {
99110 $ entry_id = $ this ->replaces ? $ this ->replaces : $ this ->get_id ();
100- $ avatar_size = apply_filters ( 'liveblog_entry_avatar_size ' , self ::default_avatar_size );
101111 $ css_classes = implode ( ' ' , get_comment_class ( '' , $ entry_id , $ this ->comment ->comment_post_ID ) );
112+ $ share_link = get_permalink ( $ this ->get_post_id () ) . '# ' . $ entry_id ;
113+
102114 $ entry = array (
103115 'id ' => $ entry_id ,
104- 'type ' => $ this ->get_type (),
105- 'html ' => $ this ->render (),
116+ 'type ' => $ this ->get_type (),
117+ 'html ' => $ this ->render (),
106118 'render ' => self ::render_content ( $ this ->get_content (), $ this ->comment ),
107119 'content ' => apply_filters ( 'liveblog_before_edit_entry ' , $ this ->get_content () ),
108120 'css_classes ' => $ css_classes ,
109121 'timestamp ' => $ this ->get_timestamp (),
110- 'avatar_img ' => get_avatar ( $ this ->comment ->comment_author_email , $ avatar_size ),
111- 'author_link ' => get_comment_author_link ( $ entry_id ),
122+ 'authors ' => self ::get_authors ( $ entry_id ),
112123 'entry_time ' => get_comment_date ( 'U ' , $ entry_id ),
124+ 'share_link ' => $ share_link ,
113125 );
114126 $ entry = apply_filters ( 'liveblog_entry_for_json ' , $ entry , $ this );
115127 return (object ) $ entry ;
@@ -122,18 +134,18 @@ public function get_fields_for_render() {
122134 $ comment_text = get_comment_text ( $ entry_id );
123135 $ css_classes = implode ( ' ' , get_comment_class ( '' , $ entry_id , $ post_id ) );
124136 $ entry = array (
125- 'entry_id ' => $ entry_id ,
126- 'post_id ' => $ post_id ,
127- 'css_classes ' => $ css_classes ,
128- 'content ' => self ::render_content ( $ comment_text , $ this ->comment ),
129- 'original_content ' => apply_filters ( 'liveblog_before_edit_entry ' , $ comment_text ),
130- 'avatar_size ' => $ avatar_size ,
131- 'avatar_img ' => get_avatar ( $ this ->comment ->comment_author_email , $ avatar_size ),
132- 'author_link ' => get_comment_author_link ( $ entry_id ),
133- 'entry_date ' => get_comment_date ( get_option ( 'date_format ' ), $ entry_id ),
134- 'entry_time ' => get_comment_date ( get_option ( 'time_format ' ), $ entry_id ),
135- 'timestamp ' => $ this ->get_timestamp (),
136- 'is_liveblog_editable ' => WPCOM_Liveblog::is_liveblog_editable (),
137+ 'entry_id ' => $ entry_id ,
138+ 'post_id ' => $ post_id ,
139+ 'css_classes ' => $ css_classes ,
140+ 'content ' => self ::render_content ( $ comment_text , $ this ->comment ),
141+ 'original_content ' => apply_filters ( 'liveblog_before_edit_entry ' , $ comment_text ),
142+ 'avatar_size ' => $ avatar_size ,
143+ 'avatar_img ' => WPCOM_Liveblog:: get_avatar ( $ this ->comment ->comment_author_email , $ avatar_size ),
144+ 'author_link ' => get_comment_author_link ( $ entry_id ),
145+ 'entry_date ' => get_comment_date ( get_option ('date_format ' ), $ entry_id ),
146+ 'entry_time ' => get_comment_date ( get_option ('time_format ' ), $ entry_id ),
147+ 'timestamp ' => $ this ->get_timestamp (),
148+ 'is_liveblog_editable ' => WPCOM_Liveblog::is_liveblog_editable (),
137149 'allowed_tags_for_entry ' => self ::$ allowed_tags_for_entry ,
138150 );
139151
@@ -339,6 +351,126 @@ public static function handle_restricted_shortcodes( $args ) {
339351 // Return the Original entry arguments with any modifications.
340352 return $ args ;
341353 }
354+
355+ /**
356+ * Return the user using author_id, if user not found then set as current
357+ * user as a fallback, we store a meta to show that authors are hidden as
358+ * a comment must have an author.
359+ *
360+ * If a entry_id is supplied we should update it as its the
361+ * original entry which is used for displaying author information.
362+ *
363+ *
364+ * @param array $args The new Live blog Entry.
365+ * @param int $entry_id If set we should update the original entry
366+ * @return mixed
367+ */
368+ private static function handle_author_select ( $ args , $ entry_id ) {
369+ if ( isset ( $ args ['author_id ' ] ) && $ args ['author_id ' ] ) {
370+ $ user_object = self ::get_userdata_with_filter ( $ args ['author_id ' ] );
371+ if ( $ user_object ) {
372+ $ args ['user ' ] = $ user_object ;
373+
374+ wp_update_comment ( array (
375+ 'comment_ID ' => $ entry_id ,
376+ 'user_id ' => $ args ['user ' ]->ID ,
377+ 'comment_author ' => $ args ['user ' ]->display_name ,
378+ 'comment_author_email ' => $ args ['user ' ]->user_email ,
379+ 'comment_author_url ' => $ args ['user ' ]->user_url ,
380+ ) );
381+
382+ update_comment_meta ( $ entry_id , self ::hide_authors_key, false );
383+ }
384+ } else {
385+ update_comment_meta ( $ entry_id , self ::hide_authors_key, true );
386+ }
387+
388+ if ( isset ( $ args ['contributor_ids ' ] ) ) {
389+ self ::add_contributors ( $ entry_id , $ args ['contributor_ids ' ] );
390+ }
391+
392+ return $ args ['user ' ];
393+ }
394+
395+ /**
396+ * Store the contributors as comment meta.
397+ *
398+ * @param int $comment_id The comment id for the meta we should update.
399+ * @param array $contributors Array of ids to store as meta.
400+ */
401+ private static function add_contributors ( $ comment_id , $ contributors ) {
402+ if ( ! $ contributors ) {
403+ delete_comment_meta ( $ comment_id , self ::contributors_meta_key );
404+ }
405+
406+ if ( is_array ( $ contributors ) ) {
407+ if ( metadata_exists ( 'comment ' , $ comment_id , self ::contributors_meta_key ) ) {
408+ update_comment_meta ( $ comment_id , self ::contributors_meta_key, $ contributors );
409+ return ;
410+ }
411+
412+ add_comment_meta ( $ comment_id , self ::contributors_meta_key, $ contributors , true );
413+ }
414+ }
415+
416+ /**
417+ * Returns a list of contributor user objects.
418+ *
419+ * @param int $comment_id The comment id to retrive the metadata.
420+ */
421+ private static function get_contributors_for_json ( $ comment_id ) {
422+ $ contributors = get_comment_meta ( $ comment_id , self ::contributors_meta_key, true );
423+
424+ if ( ! $ contributors ) {
425+ return array ();
426+ }
427+
428+ return array_map ( function ( $ contributor ) {
429+ $ user_object = self ::get_userdata_with_filter ( $ contributor );
430+ return self ::get_user_data_for_json ( $ user_object );
431+ }, $ contributors );
432+ }
433+
434+ public static function get_userdata_with_filter ( $ author_id ) {
435+ return apply_filters ( 'liveblog_userdata ' , get_userdata ( $ author_id ), $ author_id );
436+ }
437+
438+ /**
439+ * Returns a formatted array of user data.
440+ *
441+ * @param object $user The user object
442+ */
443+ private static function get_user_data_for_json ( $ user ) {
444+ if ( is_wp_error ( $ user ) ) {
445+ return array ();
446+ }
447+
448+ $ avatar_size = apply_filters ( 'liveblog_entry_avatar_size ' , self ::default_avatar_size );
449+ return array (
450+ 'id ' => $ user ->ID ,
451+ 'key ' => strtolower ($ user ->user_nicename ),
452+ 'name ' => $ user ->display_name ,
453+ 'avatar ' => WPCOM_Liveblog::get_avatar ( $ user ->ID , $ avatar_size ),
454+ );
455+ }
456+
457+ /**
458+ * Return an array of authors, based on the original comment author and its contributors.
459+ *
460+ * @param number $comment_id The id of the comment.
461+ */
462+ public static function get_authors ( $ comment_id ) {
463+ $ hide_authors = get_comment_meta ( $ comment_id , self ::hide_authors_key, true );
464+
465+ if ( $ hide_authors ) {
466+ return array ();
467+ }
468+
469+ $ author = [ self ::get_user_data_for_json ( self ::user_object_from_comment_id ( $ comment_id ) ) ];
470+ $ contributors = self ::get_contributors_for_json ( $ comment_id );
471+
472+ return array_merge ( $ author , $ contributors );
473+ }
342474}
343475
344476WPCOM_Liveblog_Entry::generate_allowed_tags_for_entry ();
0 commit comments