Skip to:
Content

BuddyPress.org

Changeset 9369


Ignore:
Timestamp:
01/17/2015 04:46:05 AM (10 years ago)
Author:
imath
Message:

Fix notice errors when an activity is generated for a post having no title.

Post types activities are using dynamic action strings when the activity is displayed in the stream and when the activity is saved into the database. In this particular case, once the post is published, we are "faking" an activity object to which we add two extra properties : the post_title and the post_url. To avoid switching blogs too many times, we are saving the post title into an activity meta. To know if we must build the action string for insert or display, we are checking the post_title property of the activity object. When a post is published without a title, the empty() check was not right. Changing for an isset() check prevents the notice errors.

props boonebgorges

Fixes #6119

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-blogs/bp-blogs-activity.php

    r9351 r9369  
    118118    }
    119119
    120     if ( empty( $activity->post_url ) ) {
     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 ) ) {
     130        $post_url = $activity->post_url;
     131
     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     */
     137    } else {
    121138        $post_url = add_query_arg( 'p', $activity->secondary_item_id, trailingslashit( $blog_url ) );
    122     } else {
    123         $post_url = $activity->post_url;
    124     }
    125 
    126     if ( empty( $activity->post_title ) ) {
     139    }
     140
     141    // Should be the case when the post has just been published
     142    if ( isset( $activity->post_title ) ) {
     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 ) ) {
    127147        $post_title = bp_activity_get_meta( $activity->id, 'post_title' );
    128     } else {
    129         $post_title = $activity->post_title;
    130     }
    131 
    132     // Should only be empty at the time of post creation
     148    }
     149
     150    /**
     151     * In case the post was published without a title
     152     * or the activity meta was not found
     153     */
    133154    if ( empty( $post_title ) ) {
     155        // Defaults to no title
     156        $post_title = esc_html__( '(no title)', 'buddypress' );
     157
    134158        switch_to_blog( $activity->item_id );
    135159
    136160        $post = get_post( $activity->secondary_item_id );
    137161        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            }
    140171        }
    141172
     
    143174    }
    144175
     176    // Build the 'post link' part of the activity action string
    145177    $post_link  = '<a href="' . $post_url . '">' . $post_title . '</a>';
    146178
    147179    $user_link = bp_core_get_userlink( $activity->user_id );
    148180
     181    // Build the complete activity action string
    149182    if ( is_multisite() ) {
    150183        $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>' );
  • trunk/tests/phpunit/testcases/blogs/activity.php

    r9366 r9369  
    311311
    312312    /**
     313     * @group activity_action
     314     * @group bp_blogs_format_activity_action_new_blog_post
     315     */
     316    public function test_bp_blogs_format_activity_action_new_blog_post_no_title() {
     317        if ( is_multisite() ) {
     318            return;
     319        }
     320
     321        $bp = buddypress();
     322        $activity_actions = $bp->activity->actions;
     323        $bp->activity->actions = new stdClass();
     324
     325        $u = $this->factory->user->create();
     326        $p = wp_insert_post( array(
     327            'post_author' => $u,
     328            'post_title'  => '', // no title: the object of the test
     329            'post_status' => 'publish',
     330            'post_content' => 'foo bar',
     331        ) );
     332
     333        $user_link = bp_core_get_userlink( $u );
     334        $blog_url = get_home_url();
     335        $post_url = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
     336        $post_link = '<a href="' . $post_url . '">(no title)</a>';
     337
     338        // Set activity actions
     339        bp_activity_get_actions();
     340
     341        $a_obj = bp_activity_get( array(
     342            'item_id'           => 1,
     343            'secondary_item_id' => $p,
     344        ) );
     345
     346        $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
     347
     348        $this->assertSame( $expected, $a_obj['activities'][0]->action );
     349
     350        // Reset activity actions
     351        $bp->activity->actions = $activity_actions;
     352        $bp->activity->track = array();
     353    }
     354
     355    /**
     356     * @group activity_action
     357     * @group bp_blogs_format_activity_action_new_blog_post
     358     */
     359    public function test_bp_blogs_format_activity_action_new_blog_post_updated_without_title() {
     360        if ( is_multisite() ) {
     361            return;
     362        }
     363
     364        $bp = buddypress();
     365        $activity_actions = $bp->activity->actions;
     366        $bp->activity->actions = new stdClass();
     367
     368        $u = $this->factory->user->create();
     369        $p = wp_insert_post( array(
     370            'post_author' => $u,
     371            'post_title'  => 'foo',
     372            'post_status' => 'publish',
     373            'post_content' => 'foo bar',
     374        ) );
     375
     376        $user_link  = bp_core_get_userlink( $u );
     377        $blog_url   = get_home_url();
     378        $post_url   = add_query_arg( 'p', $p, trailingslashit( $blog_url ) );
     379        $post_title = get_the_title( $p );
     380        $post_link  = '<a href="' . $post_url . '">' . $post_title . '</a>';
     381
     382        // Set actions
     383        bp_activity_get_actions();
     384
     385        $a_obj = bp_activity_get( array(
     386            'item_id'           => 1,
     387            'secondary_item_id' => $p,
     388        ) );
     389
     390        $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
     391
     392        $this->assertSame( $expected, $a_obj['activities'][0]->action );
     393
     394        // Update the post by removing its title
     395        wp_update_post( array(
     396            'ID'         => $p,
     397            'post_title' => '',
     398        ) );
     399
     400        // we now expect the (no title) post link
     401        $post_link = '<a href="' . $post_url . '">(no title)</a>';
     402        $expected = sprintf( '%s wrote a new post, %s', $user_link, $post_link );
     403
     404        $a_obj = bp_activity_get( array(
     405            'item_id'           => 1,
     406            'secondary_item_id' => $p,
     407        ) );
     408
     409        $this->assertSame( $expected, $a_obj['activities'][0]->action );
     410
     411        // Reset activity actions
     412        $bp->activity->actions = $activity_actions;
     413        $bp->activity->track = array();
     414    }
     415
     416    /**
    313417     * Dopey passthrough method so we can check that the correct values
    314418     * are being passed to the filter
Note: See TracChangeset for help on using the changeset viewer.