<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][8944] trunk: Add activity meta when recording a blog post or blog comment.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/8944">8944</a></dd>
<dt>Author</dt> <dd>r-a-y</dd>
<dt>Date</dt> <dd>2014-08-18 16:51:11 +0000 (Mon, 18 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Add activity meta when recording a blog post or blog comment.

This commit:
- Adds the `post_title` and `post_url` information as activity meta when recording a blog post or blog comment into the activity table.
- Updates the `post_title` and `post_url` activity meta whenever a blog post is updated.
- Adds a unit test.

This addresses issues with showing the correct blog post information in the
activity loop for blog post and blog comment activity items.

Props r-a-y, DJPaul, boonebgorges.

Fixes <a href="http://buddypress.trac.wordpress.org/ticket/5609">#5609</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpblogsbpblogsfunctionsphp">trunk/src/bp-blogs/bp-blogs-functions.php</a></li>
<li><a href="#trunktestsphpunittestcasesblogsfunctionsphp">trunk/tests/phpunit/testcases/blogs/functions.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpblogsbpblogsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/bp-blogs/bp-blogs-functions.php (8943 => 8944)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/bp-blogs/bp-blogs-functions.php        2014-08-18 13:30:40 UTC (rev 8943)
+++ trunk/src/bp-blogs/bp-blogs-functions.php   2014-08-18 16:51:11 UTC (rev 8944)
</span><span class="lines">@@ -523,7 +523,7 @@
</span><span class="cx"> 
</span><span class="cx">                  $activity_content = $post->post_content;
</span><span class="cx"> 
</span><del>-                       bp_blogs_record_activity( array(
</del><ins>+                        $activity_id = bp_blogs_record_activity( array(
</ins><span class="cx">                           'user_id'           => (int) $post->post_author,
</span><span class="cx">                          'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
</span><span class="cx">                          'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink,   $post_id               ),
</span><span class="lines">@@ -531,7 +531,11 @@
</span><span class="cx">                          'item_id'           => $blog_id,
</span><span class="cx">                          'secondary_item_id' => $post_id,
</span><span class="cx">                          'recorded_time'     => $post->post_date_gmt,
</span><del>-                       ));
</del><ins>+                        ) );
+
+                       // save post title in activity meta
+                       bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
+                       bp_activity_update_meta( $activity_id, 'post_url',   $post_permalink );
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          // Update the blogs last activity
</span><span class="lines">@@ -583,6 +587,62 @@
</span><span class="cx">  // Save the updated activity
</span><span class="cx">  $activity->save();
</span><span class="cx"> 
</span><ins>+       // update post title in activity meta
+       $existing_title = bp_activity_get_meta( $activity_id, 'post_title' );
+       if ( $post->post_title !== $existing_title ) {
+               bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
+
+               // now update activity meta for post comments... sigh
+               $comment_ids = get_comments( array( 'post_id' => $post->ID, 'fields' => 'ids' ) );
+
+               if ( ! empty( $comment_ids ) ) {
+                       $activity_ids = array();
+
+                       // setup activity args
+                       $args = array(
+                               'update_meta_cache' => false,
+                               'show_hidden'       => true,
+                               'per_page'          => 99999,
+                       );
+
+                       // query for old-style "new_blog_comment" activity items
+                       $args['filter'] = array(
+                               'object'     => buddypress()->blogs->id,
+                               'action'     => 'new_blog_comment',
+                               'secondary_id' => implode( ',', $comment_ids ),
+                       );
+
+                       $activities = bp_activity_get( $args );
+                       if ( ! empty( $activities['activities'] ) ) {
+                               $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' );
+                       }
+
+                       // query for activity comments connected to a blog post
+                       unset( $args['filter'] );
+                       $args['meta_query'] = array( array(
+                               'key'     => 'bp_blogs_post_comment_id',
+                               'value'   => $comment_ids,
+                               'compare' => 'IN',
+                       ) );
+                       $args['type'] = 'activity_comment';
+                       $args['display_comments'] = 'stream';
+
+                       $activities = bp_activity_get( $args );
+                       if ( ! empty( $activities['activities'] ) ) {
+                               $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) );
+                       }
+
+                       // update activity meta for all found activity items
+                       if ( ! empty( $activity_ids ) ) {
+                               foreach ( $activity_ids as $aid ) {
+                                       bp_activity_update_meta( $aid, 'post_title', $post->post_title );
+                               }
+                       }
+
+                       unset( $activities, $activity_ids, $comment_ids );
+               }
+       }
+
</ins><span class="cx">   // add post comment status to activity meta if closed
</span><span class="cx">  if( 'closed' == $post->comment_status ) {
</span><span class="cx">          bp_activity_update_meta( $activity_id, 'post_comment_status', $post->comment_status );
</span><span class="lines">@@ -680,8 +740,12 @@
</span><span class="cx">                  $args['secondary_item_id'] = $comment_id;
</span><span class="cx"> 
</span><span class="cx">                  // record the activity entry
</span><del>-                       bp_blogs_record_activity( $args );
</del><ins>+                        $activity_id = bp_blogs_record_activity( $args );
</ins><span class="cx"> 
</span><ins>+                       // add some post info in activity meta
+                       bp_activity_update_meta( $activity_id, 'post_title', $recorded_comment->post->post_title );
+                       bp_activity_update_meta( $activity_id, 'post_url',   add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) );
+
</ins><span class="cx">           // record comment as BP activity comment under the parent 'new_blog_post'
</span><span class="cx">          // activity item
</span><span class="cx">          } else {
</span><span class="lines">@@ -744,6 +808,8 @@
</span><span class="cx">                          if ( empty( $args['id'] ) ) {
</span><span class="cx">                                  // add meta to activity comment
</span><span class="cx">                                  bp_activity_update_meta( $comment_activity_id, 'bp_blogs_post_comment_id', $comment_id );
</span><ins>+                                       bp_activity_update_meta( $comment_activity_id, 'post_title', $recorded_comment->post->post_title );
+                                       bp_activity_update_meta( $comment_activity_id, 'post_url', add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) );
</ins><span class="cx"> 
</span><span class="cx">                                  // add meta to comment
</span><span class="cx">                                  add_comment_meta( $comment_id, 'bp_activity_comment_id', $comment_activity_id );
</span></span></pre></div>
<a id="trunktestsphpunittestcasesblogsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/testcases/blogs/functions.php (8943 => 8944)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/testcases/blogs/functions.php        2014-08-18 13:30:40 UTC (rev 8943)
+++ trunk/tests/phpunit/testcases/blogs/functions.php   2014-08-18 16:51:11 UTC (rev 8944)
</span><span class="lines">@@ -394,6 +394,89 @@
</span><span class="cx">          $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       /**
+        * @group bp_blogs_catch_transition_post_status
+        */
+       public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() {
+               // save the current user and override logged-in user
+               $old_user = get_current_user_id();
+               $u = $this->create_user();
+               $this->set_current_user( $u );
+               $userdata = get_userdata( $u );
+
+               // create the blog post
+               $post_id = $this->factory->post->create( array(
+                       'post_status' => 'publish',
+                       'post_type' => 'post',
+                       'post_title' => 'First title',
+               ) );
+
+               // remove comment flood protection temporarily
+               add_filter( 'comment_flood_filter', '__return_false' );
+
+               // let's use activity comments instead of single "new_blog_comment" activity items
+               add_filter( 'bp_disable_blogforum_comments', '__return_false' );
+               $c1 = wp_new_comment( array(
+                       'comment_post_ID'      => $post_id,
+                       'comment_author'       => $userdata->user_nicename,
+                       'comment_author_url'   => 'http://buddypress.org',
+                       'comment_author_email' => $userdata->user_email,
+                       'comment_content'      => 'this is a blog comment',
+                       'comment_type'         => '',
+                       'comment_parent'       => 0,
+                       'user_id'              => $u,
+               ) );
+               remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
+
+               // let's also add a "new_blog_comment" activity entry
+               $c2 = wp_new_comment( array(
+                       'comment_post_ID'      => $post_id,
+                       'comment_author'       => $userdata->user_nicename,
+                       'comment_author_url'   => 'http://buddypress.org',
+                       'comment_author_email' => $userdata->user_email,
+                       'comment_content'      => 'this is another blog comment',
+                       'comment_type'         => '',
+                       'comment_parent'       => 0,
+                       'user_id'              => $u,
+               ) );
+
+               // bring back flood protection
+               remove_filter( 'comment_flood_filter', '__return_false' );
+
+               // update the initial blog post
+               wp_update_post( array(
+                       'ID'        => $post_id,
+                       'post_title' => 'Second title',
+               ) );
+
+               // grab the activity ID for the activity comment
+               $a1 = bp_activity_get_activity_id( array(
+                       'type'              => 'activity_comment',
+                       'display_comments'  => 'stream',
+                       'meta_query'        => array( array(
+                               'key'     => 'bp_blogs_post_comment_id',
+                               'value'   => $c1,
+                       ) )
+               ) );
+
+               // grab the activity ID for the blog comment
+               $a2 = bp_activity_get_activity_id( array(
+                       'component'         => buddypress()->blogs->id,
+                       'type'              => 'new_blog_comment',
+                       'secondary_item_id' => $c2,
+               ) );
+
+               // see if blog comment activity meta matches the post items
+               $this->assertEquals( 'Second title', bp_activity_get_meta( $a1, 'post_title' ) );
+               $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a1, 'post_url' ) );
+
+               $this->assertEquals( 'Second title', bp_activity_get_meta( $a2, 'post_title' ) );
+               $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a2, 'post_url' ) );
+
+               // reset
+               $this->set_current_user( $old_user );
+       }
+
</ins><span class="cx">   protected function activity_exists_for_post( $post_id ) {
</span><span class="cx">          $a = bp_activity_get( array(
</span><span class="cx">                  'component' => buddypress()->blogs->id,
</span></span></pre>
</div>
</div>

</body>
</html>