Skip to:
Content

BuddyPress.org

Changeset 8073


Ignore:
Timestamp:
03/06/2014 09:02:22 PM (10 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

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs/bp-blogs-functions.php

    r8042 r8073  
    260260 * See #4090, #3746, #2546 for background.
    261261 *
    262  * @since BuddyPress (1.9.0)
     262 * @since BuddyPress (2.0.0)
    263263 *
    264264 * @param string $new_status New status for the post.
     
    266266 * @param object $post Post data.
    267267 */
    268 function bp_blogs_catch_published_post( $new_status, $old_status, $post ) {
    269 
    270     // Only record published posts
    271     if ( 'publish' !== $new_status ) {
     268function bp_blogs_catch_transition_post_status( $new_status, $old_status, $post ) {
     269
     270    // Do nothing for edits
     271    if ( $new_status === $old_status ) {
    272272        return;
    273273    }
    274274
    275     // Don't record edits (publish -> publish)
    276     if ( 'publish' === $old_status ) {
    277         return;
    278     }
    279 
    280     return bp_blogs_record_post( $post->ID, $post );
    281 }
    282 add_action( 'transition_post_status', 'bp_blogs_catch_published_post', 10, 3 );
     275    // Publishing a previously unpublished post
     276    if ( 'publish' === $new_status ) {
     277        return bp_blogs_record_post( $post->ID, $post );
     278
     279    // Unpublishing a previously published post
     280    } else if ( 'publish' === $old_status ) {
     281        return bp_blogs_remove_post( $post->ID );
     282    }
     283}
     284add_action( 'transition_post_status', 'bp_blogs_catch_transition_post_status', 10, 3 );
    283285
    284286/**
  • trunk/bp-core/deprecated/2.0.php

    r7854 r8073  
    11<?php
     2/**
     3 * Deprecated Functions
     4 *
     5 * @package BuddyPress
     6 * @subpackage Core
     7 * @deprecated 2.0.0
     8 */
     9
     10// Exit if accessed directly
     11if ( !defined( 'ABSPATH' ) ) exit;
    212
    313/**
     
    717    _deprecated_function( __FUNCTION__, '2.0.0', 'Use WP metadata API instead' );
    818}
     19
     20/**
     21 * @deprecated BuddyPress (2.0.0)
     22 */
     23function bp_blogs_catch_published_post() {
     24    _deprecated_function( __FUNCTION__, '2.0', 'bp_blogs_catch_transition_post_status()' );
     25}
  • 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.