Ticket #6119: 6119.03.patch
File 6119.03.patch, 5.8 KB (added by , 10 years ago) |
---|
-
src/bp-blogs/bp-blogs-activity.php
diff --git src/bp-blogs/bp-blogs-activity.php src/bp-blogs/bp-blogs-activity.php index c52287c..030507f 100644
function bp_blogs_format_activity_action_new_blog_post( $action, $activity ) { 117 117 bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name ); 118 118 } 119 119 120 if ( empty( $activity->post_url ) ) { 121 $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) ); 122 } else { 120 /** 121 * When the post is published we are faking an activity object 122 * to which we add 2 properties : 123 * - the post url 124 * - the post title 125 * This is done to build the 'post link' part of the activity 126 * action string. 127 * NB: in this case the activity has not yet been created. 128 */ 129 if ( isset( $activity->post_url ) ) { 123 130 $post_url = $activity->post_url; 124 }125 131 126 if ( empty( $activity->post_title ) ) { 127 $post_title = bp_activity_get_meta( $activity->id, 'post_title' ); 132 /** 133 * The post_url property is not set, we need to build the url 134 * thanks to the post id which is also saved as the secondary 135 * item id property of the activity object. 136 */ 128 137 } else { 138 $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) ); 139 } 140 141 // Should be the case when the post has just been published 142 if ( isset( $activity->post_title ) ) { 129 143 $post_title = $activity->post_title; 144 145 // If activity already exists try to get the post title from activity meta 146 } else if ( ! empty( $activity->id ) ) { 147 $post_title = bp_activity_get_meta( $activity->id, 'post_title' ); 130 148 } 131 149 132 // Should only be empty at the time of post creation 150 /** 151 * In case the post was published without a title 152 * or the activity meta was not found 153 */ 133 154 if ( empty( $post_title ) ) { 155 // Defaults to no title 156 $post_title = esc_html__( '(no title)', 'buddypress' ); 157 134 158 switch_to_blog( $activity->item_id ); 135 159 136 160 $post = get_post( $activity->secondary_item_id ); 137 161 if ( is_a( $post, 'WP_Post' ) ) { 138 $post_title = $post->post_title; 139 bp_activity_update_meta( $activity->id, 'post_title', $post_title ); 162 // Does the post have a title ? 163 if ( ! empty( $post->post_title ) ) { 164 $post_title = $post->post_title; 165 } 166 167 // Make sure the activity exists before saving the post title in activity meta 168 if ( ! empty( $activity->id ) ) { 169 bp_activity_update_meta( $activity->id, 'post_title', $post_title ); 170 } 140 171 } 141 172 142 173 restore_current_blog(); 143 174 } 144 175 176 // Build the 'post link' part of the activity action string 145 177 $post_link = '<a href="' . $post_url . '">' . $post_title . '</a>'; 146 178 147 179 $user_link = bp_core_get_userlink( $activity->user_id ); 148 180 181 // Build the complete activity action string 149 182 if ( is_multisite() ) { 150 183 $action = sprintf( __( '%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress' ), $user_link, $post_link, '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>' ); 151 184 } else { -
tests/phpunit/testcases/blogs/activity.php
diff --git tests/phpunit/testcases/blogs/activity.php tests/phpunit/testcases/blogs/activity.php index e67adab..6eed606 100644
class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 283 283 284 284 $this->assertEquals( $this->comment_post_id, $p ); 285 285 } 286 287 /** 288 * @group activity_action 289 * @group bp_blogs_format_activity_action_new_blog_post 290 */ 291 public function test_bp_blogs_format_activity_action_new_blog_post_no_title() { 292 if ( is_multisite() ) { 293 return; 294 } 295 296 $u = $this->factory->user->create(); 297 $p = wp_insert_post( array( 298 'post_author' => $u, 299 'post_title' => '', // no title: the object of the test 300 'post_status' => 'publish', 301 'post_content' => 'foo bar', 302 ) ); 303 304 $user_link = bp_core_get_userlink( $u ); 305 $blog_url = get_home_url(); 306 $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) ); 307 $post_link = '<a href="' . $post_url . '">(no title)</a>'; 308 309 // Set actions 310 bp_activity_get_actions(); 311 312 $a_obj = bp_activity_get( array( 313 'item_id' => 1, 314 'secondary_item_id' => $p, 315 ) ); 316 317 $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link ); 318 319 $this->assertSame( $expected, $a_obj['activities'][0]->action ); 320 } 321 322 /** 323 * @group activity_action 324 * @group bp_blogs_format_activity_action_new_blog_post 325 */ 326 public function test_bp_blogs_format_activity_action_new_blog_post_updated_without_title() { 327 if ( is_multisite() ) { 328 return; 329 } 330 331 $u = $this->factory->user->create(); 332 $p = wp_insert_post( array( 333 'post_author' => $u, 334 'post_title' => 'foo', 335 'post_status' => 'publish', 336 'post_content' => 'foo bar', 337 ) ); 338 339 $user_link = bp_core_get_userlink( $u ); 340 $blog_url = get_home_url(); 341 $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) ); 342 $post_title = get_the_title( $p ); 343 $post_link = '<a href="' . $post_url . '">' . $post_title . '</a>'; 344 345 // Set actions 346 bp_activity_get_actions(); 347 348 $a_obj = bp_activity_get( array( 349 'item_id' => 1, 350 'secondary_item_id' => $p, 351 ) ); 352 353 $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link ); 354 355 $this->assertSame( $expected, $a_obj['activities'][0]->action ); 356 357 // Update the post by removing its title 358 wp_update_post( array( 359 'ID' => $p, 360 'post_title' => '', 361 ) ); 362 363 // we now expect the (no title) post link 364 $post_link = '<a href="' . $post_url . '">(no title)</a>'; 365 $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link ); 366 367 $a_obj = bp_activity_get( array( 368 'item_id' => 1, 369 'secondary_item_id' => $p, 370 ) ); 371 372 $this->assertSame( $expected, $a_obj['activities'][0]->action ); 373 } 374 286 375 /** 287 376 * Dopey passthrough method so we can check that the correct values 288 377 * are being passed to the filter