Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/06/2014 09:02:22 PM (12 years ago)
Author:
boonebgorges
Message:

Improved management of new_blog_post activity items at transition_post_status

Introduces bp_blogs_catch_transition_post_status(). This function, which
replaces bp_blogs_catch_published_post(), performs a similar task - determining
when a new blog post has been published, and recording the post in the activity
stream - but adds a corrolary for unpublishing that deletes the activity item.

Unit tests and deprecated function are included.

See #5333

Props henry.wright for an initial patch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/testcases/blogs/functions.php

    r7887 r8073  
    266266                $this->assertNotEmpty( bp_blogs_add_blogmeta( 1, 'foo', 'baz' ) );
    267267        }
     268
     269        /**
     270         * @group bp_blogs_catch_transition_post_status
     271         */
     272        public function test_transition_post_status_publish_to_publish() {
     273                $post_id = $this->factory->post->create( array(
     274                        'post_status' => 'publish',
     275                        'post_type' => 'post',
     276                ) );
     277                $post = get_post( $post_id );
     278
     279                // 'publish' => 'publish'
     280                $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     281
     282                $post->post_status = 'publish';
     283                $post->post_content .= ' foo';
     284
     285                wp_update_post( $post );
     286
     287                $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity (no change)' );
     288        }
     289
     290        /**
     291         * @group bp_blogs_catch_transition_post_status
     292         */
     293        public function test_transition_post_status_draft_to_draft() {
     294                $post_id = $this->factory->post->create( array(
     295                        'post_status' => 'draft',
     296                        'post_type' => 'post',
     297                ) );
     298                $post = get_post( $post_id );
     299
     300                $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
     301
     302                $post->post_status = 'draft';
     303                $post->post_content .= ' foo';
     304
     305                wp_update_post( $post );
     306
     307                $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity (no change)' );
     308        }
     309
     310        /**
     311         * @group bp_blogs_catch_transition_post_status
     312         */
     313        public function test_transition_post_status_draft_to_publish() {
     314                $post_id = $this->factory->post->create( array(
     315                        'post_status' => 'draft',
     316                        'post_type' => 'post',
     317                ) );
     318                $post = get_post( $post_id );
     319
     320                $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
     321
     322                $post->post_status = 'publish';
     323                $post->post_content .= ' foo';
     324
     325                wp_update_post( $post );
     326
     327                $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     328        }
     329
     330        /**
     331         * @group bp_blogs_catch_transition_post_status
     332         */
     333        public function test_transition_post_status_publish_to_draft() {
     334                $post_id = $this->factory->post->create( array(
     335                        'post_status' => 'publish',
     336                        'post_type' => 'post',
     337                ) );
     338                $post = get_post( $post_id );
     339
     340                $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     341
     342                $post->post_status = 'draft';
     343                $post->post_content .= ' foo';
     344
     345                wp_update_post( $post );
     346
     347                $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
     348        }
     349
     350        /**
     351         * @group bp_blogs_catch_transition_post_status
     352         */
     353        public function test_transition_post_status_wp_delete_post() {
     354                $post_id = $this->factory->post->create( array(
     355                        'post_status' => 'publish',
     356                        'post_type' => 'post',
     357                ) );
     358                $post = get_post( $post_id );
     359
     360                $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     361
     362                wp_delete_post( $post->ID );
     363
     364                $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
     365        }
     366
     367        /**
     368         * @group bp_blogs_catch_transition_post_status
     369         */
     370        public function test_transition_post_status_wp_trash_post() {
     371                $post_id = $this->factory->post->create( array(
     372                        'post_status' => 'publish',
     373                        'post_type' => 'post',
     374                ) );
     375                $post = get_post( $post_id );
     376
     377                $this->assertTrue( $this->activity_exists_for_post( $post_id ), 'Published post should have activity' );
     378
     379                wp_trash_post( $post->ID );
     380
     381                $this->assertFalse( $this->activity_exists_for_post( $post_id ), 'Unpublished post should not have activity' );
     382        }
     383
     384        protected function activity_exists_for_post( $post_id ) {
     385                $a = bp_activity_get( array(
     386                        'component' => buddypress()->blogs->id,
     387                        'action' => 'new_blog_post',
     388                        'item_id' => get_current_blog_id(),
     389                        'secondary_item_id' => $post_id,
     390                ) );
     391
     392                return ! empty( $a['total'] );
     393        }
    268394}
Note: See TracChangeset for help on using the changeset viewer.