@@ -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
338447WPCOM_Liveblog_Entry::generate_allowed_tags_for_entry ();
0 commit comments