Skip to:
Content

BuddyPress.org

Ticket #6482: 6482.03b.patch

File 6482.03b.patch, 17.9 KB (added by r-a-y, 8 years ago)
  • src/bp-activity/bp-activity-actions.php

     
    797797        if ( $new_status === $old_status ) {
    798798                // An edit of an existing post should update the existing activity item.
    799799                if ( $new_status == 'publish' ) {
    800                         bp_activity_post_type_update( $post );
     800                        $edit = bp_activity_post_type_update( $post );
     801
     802                        // Post was never recorded into activity stream, so record it now!
     803                        if ( null === $edit ) {
     804                                bp_activity_post_type_publish( $post->ID, $post );
     805                        }
    801806                }
    802807
    803808                return;
  • src/bp-activity/bp-activity-functions.php

     
    606606}
    607607
    608608/**
    609  * Helper function to check if the activity action is about a "parent" post type activity
    610  * supporting comments tracking
     609 * Check if a certain activity type supports a specific feature.
    611610 *
    612611 * @since 2.5.0
    613612 *
    614  * @param  string $action_id the activity action to check
    615  * @return bool   true if it's a parent post type activity supporting comments
    616  *                false otherwise
     613 * @param  string $activity_type The activity type to check.
     614 * @param  string $supports      The feature to check. Currently supports:
     615 *                               'comment-tracking', 'post-comments'. See inline doc for more info.
     616 * @return bool
    617617 */
    618 function bp_activity_action_is_post_tracking_action( $action_id ) {
    619         if ( empty( $action_id ) ) {
    620                 return false;
    621         }
     618function bp_activity_type_supports( $activity_type = '', $supports = '' ) {
     619        $retval = false;
    622620
    623621        $bp = buddypress();
    624622
    625         $retval = bp_activity_action_supports_comments_tracking( $action_id );
    626 
    627         if ( empty( $retval ) ) {
    628                 return $retval;
    629         }
    630 
    631         return ! empty( $bp->activity->track[ $action_id ]->comment_action_id );
    632 }
     623        switch ( $supports ) {
     624                /**
     625                 * Does this activity type support comment tracking?
     626                 *
     627                 * eg. 'new_blog_post' and 'new_blog_comment' will both return true.
     628                 */
     629                case 'comment-tracking' :
     630                        // Set the activity track global if not set yet
     631                        if ( empty( $bp->activity->track ) ) {
     632                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     633                        }
    633634
    634 /**
    635  * Helper function to check if the activity action is supporting comments tracking
    636  *
    637  * @since 2.5.0
    638  *
    639  * @param  string $action_id the activity action to check
    640  * @return bool   true activity action supports comments tracking
    641  *                false otherwise
    642  */
    643 function bp_activity_action_supports_comments_tracking( $action_id ) {
    644         if ( empty( $action_id ) ) {
    645                 return false;
    646         }
     635                        if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) ) {
     636                                $retval = true;
     637                        }
     638                        break;
    647639
    648         $bp = buddypress();
     640                /**
     641                 * Is this a parent activity type that support post comments?
     642                 *
     643                 * eg. 'new_blog_post' will return true; 'new_blog_comment' will return false.
     644                 *
     645                 * Might need to rename 'post-comments' to something else...
     646                 */
     647                case 'post-comments' :
     648                        // Set the activity track global if not set yet
     649                        if ( empty( $bp->activity->track ) ) {
     650                                $bp->activity->track = bp_activity_get_post_types_tracking_args();
     651                        }
    649652
    650         // Set the activity track global if not set yet
    651         if ( empty( $bp->activity->track ) ) {
    652                 $bp->activity->track = bp_activity_get_post_types_tracking_args();
     653                        if ( ! empty( $bp->activity->track[ $activity_type ]->comments_tracking ) && ! empty( $bp->activity->track[ $activity_type ]->comment_action_id ) ) {
     654                                $retval = true;
     655                        }
     656                        break;
    653657        }
    654658
    655         return ! empty( $bp->activity->track[ $action_id ]->comments_tracking );
     659        return $retval;
    656660}
    657661
    658662/**
  • src/bp-blogs/bp-blogs-activity.php

     
    1818 * @return bool|null Returns false if activity component is not active.
    1919 */
    2020function bp_blogs_register_activity_actions() {
    21         $bp = buddypress();
    22 
    23         // Bail if activity is not active.
    24         if ( ! bp_is_active( 'activity' ) ) {
    25                 return false;
    26         }
    27 
    2821        if ( is_multisite() ) {
    2922                bp_activity_set_action(
    30                         $bp->blogs->id,
     23                        buddypress()->blogs->id,
    3124                        'new_blog',
    3225                        __( 'New site created', 'buddypress' ),
    3326                        'bp_blogs_format_activity_action_new_blog',
     
    4740add_action( 'bp_register_activity_actions', 'bp_blogs_register_activity_actions' );
    4841
    4942/**
     43 * Set up the tracking arguments for the 'post' post type.
     44 *
     45 * @since 2.5.0 This was moved out of the BP_Blogs_Component class.
     46 *
     47 * @see bp_activity_get_post_type_tracking_args() for information on parameters.
     48 *
     49 * @param object|null $params    Tracking arguments.
     50 * @param string|int  $post_type Post type to track.
     51 * @return object
     52 */
     53function bp_blogs_register_post_tracking_args( $params = null, $post_type = 0 ) {
     54
     55        /**
     56         * Filters the post types to track for the Blogs component.
     57         *
     58         * @since 1.5.0
     59         * @deprecated 2.3.0
     60         *
     61         * Make sure plugins still using 'bp_blogs_record_post_post_types'
     62         * to track their post types will generate new_blog_post activities
     63         * See https://buddypress.trac.wordpress.org/ticket/6306
     64         *
     65         * @param array $value Array of post types to track.
     66         */
     67        $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );
     68        $post_types_array = array_flip( $post_types );
     69
     70        if ( ! isset( $post_types_array[ $post_type ] ) ) {
     71                return $params;
     72        }
     73
     74        // Set specific params for the 'post' post type.
     75        $params->component_id    = buddypress()->blogs->id;
     76        $params->action_id       = 'new_blog_post';
     77        $params->admin_filter    = __( 'New post published', 'buddypress' );
     78        $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post';
     79        $params->front_filter    = __( 'Posts', 'buddypress' );
     80        $params->contexts        = array( 'activity', 'member' );
     81        $params->position        = 5;
     82
     83        if ( post_type_supports( $post_type, 'comments' ) ) {
     84                $params->comment_action_id = 'new_blog_comment';
     85
     86                /**
     87                 * Filters the post types to track for the Blogs component.
     88                 *
     89                 * @since 1.5.0
     90                 * @deprecated 2.5.0
     91                 *
     92                 * Make sure plugins still using 'bp_blogs_record_comment_post_types'
     93                 * to track comment about their post types will generate new_blog_comment activities
     94                 * See https://buddypress.trac.wordpress.org/ticket/6306
     95                 *
     96                 * @param array $value Array of post types to track.
     97                 */
     98                $comment_post_types = apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) );
     99                $comment_post_types_array = array_flip( $comment_post_types );
     100
     101                if ( isset( $comment_post_types_array[ $post_type ] ) ) {
     102                        $params->comments_tracking = new stdClass();
     103                        $params->comments_tracking->component_id    = buddypress()->blogs->id;
     104                        $params->comments_tracking->action_id       = 'new_blog_comment';
     105                        $params->comments_tracking->admin_filter    = __( 'New post comment posted', 'buddypress' );
     106                        $params->comments_tracking->format_callback = 'bp_blogs_format_activity_action_new_blog_comment';
     107                        $params->comments_tracking->front_filter    = __( 'Comments', 'buddypress' );
     108                        $params->comments_tracking->contexts        = array( 'activity', 'member' );
     109                        $params->comments_tracking->position        = 10;
     110                }
     111        }
     112
     113        return $params;
     114}
     115add_filter( 'bp_activity_get_post_type_tracking_args', 'bp_blogs_register_post_tracking_args', 10, 2 );
     116
     117/**
    50118 * Format 'new_blog' activity actions.
    51119 *
    52120 * @since 2.0.0
     
    370438 * @return int|bool On success, returns the activity ID. False on failure.
    371439 */
    372440function bp_blogs_record_activity( $args = '' ) {
    373 
    374         // Bail if activity is not active.
    375         if ( ! bp_is_active( 'activity' ) ) {
    376                 return false;
    377         }
    378 
    379         $bp = buddypress();
    380 
    381441        $defaults = array(
    382442                'user_id'           => bp_loggedin_user_id(),
    383443                'action'            => '',
    384444                'content'           => '',
    385445                'primary_link'      => '',
    386                 'component'         => $bp->blogs->id,
     446                'component'         => buddypress()->blogs->id,
    387447                'type'              => false,
    388448                'item_id'           => false,
    389449                'secondary_item_id' => false,
     
    447507 * @return bool True on success, false on failure.
    448508 */
    449509function bp_blogs_delete_activity( $args = '' ) {
    450 
    451         // Bail if activity is not active.
    452         if ( ! bp_is_active( 'activity' ) ) {
    453                 return false;
    454         }
    455 
    456510        $r = bp_parse_args( $args, array(
    457511                'item_id'           => false,
    458512                'component'         => buddypress()->blogs->id,
     
    570624 */
    571625function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_activity ) {
    572626        // if parent activity isn't a post type having the buddypress-activity support, stop now!
    573         if ( ! bp_activity_action_supports_comments_tracking( $parent_activity->type ) ) {
     627        if ( ! bp_activity_type_supports( $parent_activity->type, 'comment-tracking' ) ) {
    574628                return;
    575629        }
    576630
     
    688742        $parent_activity = new BP_Activity_Activity( $parent_activity_id );
    689743
    690744        // if parent activity isn't a post type having the buddypress-activity support, stop now!
    691         if ( ! bp_activity_action_supports_comments_tracking( $parent_activity->type ) ) {
     745        if ( ! bp_activity_type_supports( $parent_activity->type, 'comment-tracking' ) ) {
    692746                return $retval;
    693747        }
    694748
     
    744798        $parent_activity = new BP_Activity_Activity( $activity->item_id );
    745799
    746800        // if parent activity isn't a post type having the buddypress-activity support for comments, stop now!
    747         if ( ! bp_activity_action_supports_comments_tracking( $parent_activity->type ) ) {
     801        if ( ! bp_activity_type_supports( $parent_activity->type, 'comment-tracking' ) ) {
    748802                return;
    749803        }
    750804
     
    834888 * powers the 'Comments' filter in the activity directory dropdown) includes
    835889 * both old-style and new-style activity comments.
    836890 *
    837  * This implementation involves filtering the activity queries directly, and
    838  * should be considered a stopgap. The proper solution would involve enabling
    839  * multiple query condition clauses, connected by an OR, in the bp_has_activities()
    840  * API.
    841  *
    842891 * @since 2.1.0
    843892 * @since 2.5.0 Used for any synced Post type comments, in wp-admin or front-end contexts.
    844893 *
     
    849898        global $wpdb;
    850899        $bp = buddypress();
    851900
    852         // if activity comments are disabled for blog posts, stop now!
     901        // If activity comments are disabled for blog posts, stop now!
    853902        if ( bp_disable_blogforum_comments() ) {
    854903                return $args;
    855904        }
     
    934983        }
    935984
    936985        // parent not a post post type action ? stop now!
    937         if ( ! bp_activity_action_is_post_tracking_action( $activity->type ) ) {
     986        if ( ! bp_activity_type_supports( $activity->type, 'post-comments' ) ) {
    938987                return;
    939988        }
    940989
     
    10161065                return $retval;
    10171066        }
    10181067
    1019         $action = bp_get_activity_action_name();
     1068        $type = bp_get_activity_type();
    10201069
    1021         // It's a post type action supporting comment tracking
    1022         if ( bp_activity_action_supports_comments_tracking( $action ) ) {
     1070        // It's a post type supporting comment tracking.
     1071        if ( bp_activity_type_supports( $type, 'comment-tracking' ) ) {
    10231072                // it's a "post" action
    1024                 if ( bp_activity_action_is_post_tracking_action( $action ) ) {
     1073                if ( bp_activity_type_supports( $type, 'post-comments' ) ) {
    10251074                        global $activities_template;
    10261075
    10271076                        // Setup some globals we'll need to reference later.
     
    11871236                return $retval;
    11881237        }
    11891238
    1190         $bp = buddypress();
    11911239        $blog_comment_id = bp_activity_get_meta( $activity->id, 'bp_blogs_post_comment_id' );
    11921240
    11931241        if ( ! empty( $blog_comment_id ) ) {
     1242                $bp = buddypress();
     1243
    11941244                // Fetch the parent blog post activity item.
    11951245                $parent_blog_post_activity = new BP_Activity_Activity( $activity->item_id );
    11961246
  • src/bp-blogs/bp-blogs-loader.php

     
    106106                                add_post_type_support( $post_type, 'buddypress-activity' );
    107107                        }
    108108                }
    109 
    110                 // Filter the generic track parameters for the 'post' post type.
    111                 add_filter( 'bp_activity_get_post_type_tracking_args', array( $this, 'post_tracking_args' ), 10, 2 );
    112109        }
    113110
    114111        /**
     
    128125                        'classes',
    129126                        'template',
    130127                        'filters',
    131                         'activity',
    132128                        'functions',
    133129                );
    134130
     131                if ( bp_is_active( 'activity' ) ) {
     132                        $includes[] = 'activity';
     133                }
     134
    135135                if ( is_multisite() ) {
    136136                        $includes[] = 'widgets';
    137137                }
     
    300300
    301301                parent::setup_cache_groups();
    302302        }
    303 
    304         /**
    305          * Set up the tracking arguments for the 'post' post type.
    306          *
    307          * @since 2.2.0
    308          *
    309          * @see bp_activity_get_post_type_tracking_args() for information on parameters.
    310          *
    311          * @param object|null $params    Tracking arguments.
    312          * @param string|int  $post_type Post type to track.
    313          * @return object
    314          */
    315         public function post_tracking_args( $params = null, $post_type = 0 ) {
    316 
    317                 /**
    318                  * Filters the post types to track for the Blogs component.
    319                  *
    320                  * @since 1.5.0
    321                  * @deprecated 2.3.0
    322                  *
    323                  * Make sure plugins still using 'bp_blogs_record_post_post_types'
    324                  * to track their post types will generate new_blog_post activities
    325                  * See https://buddypress.trac.wordpress.org/ticket/6306
    326                  *
    327                  * @param array $value Array of post types to track.
    328                  */
    329                 $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );
    330                 $post_types_array = array_flip( $post_types );
    331 
    332                 if ( ! isset( $post_types_array[ $post_type ] ) ) {
    333                         return $params;
    334                 }
    335 
    336                 // Set specific params for the 'post' post type.
    337                 $params->component_id    = $this->id;
    338                 $params->action_id       = 'new_blog_post';
    339                 $params->admin_filter    = __( 'New post published', 'buddypress' );
    340                 $params->format_callback = 'bp_blogs_format_activity_action_new_blog_post';
    341                 $params->front_filter    = __( 'Posts', 'buddypress' );
    342                 $params->contexts        = array( 'activity', 'member' );
    343                 $params->position        = 5;
    344 
    345                 if ( post_type_supports( $post_type, 'comments' ) ) {
    346                         $params->comment_action_id = 'new_blog_comment';
    347 
    348                         /**
    349                          * Filters the post types to track for the Blogs component.
    350                          *
    351                          * @since 1.5.0
    352                          * @deprecated 2.5.0
    353                          *
    354                          * Make sure plugins still using 'bp_blogs_record_comment_post_types'
    355                          * to track comment about their post types will generate new_blog_comment activities
    356                          * See https://buddypress.trac.wordpress.org/ticket/6306
    357                          *
    358                          * @param array $value Array of post types to track.
    359                          */
    360                         $comment_post_types = apply_filters( 'bp_blogs_record_comment_post_types', array( 'post' ) );
    361                         $comment_post_types_array = array_flip( $comment_post_types );
    362 
    363                         if ( isset( $comment_post_types_array[ $post_type ] ) ) {
    364                                 $params->comments_tracking = new stdClass();
    365                                 $params->comments_tracking->component_id      = $this->id;
    366                                 $params->comments_tracking->action_id         = 'new_blog_comment';
    367                                 $params->comments_tracking->admin_filter      = __( 'New post comment posted', 'buddypress' );
    368                                 $params->comments_tracking->format_callback   = 'bp_blogs_format_activity_action_new_blog_comment';
    369                                 $params->comments_tracking->front_filter      = __( 'Comments', 'buddypress' );
    370                                 $params->comments_tracking->contexts          = array( 'activity', 'member' );
    371                                 $params->comments_tracking->position          = 10;
    372                         }
    373                 }
    374 
    375                 return $params;
    376         }
    377303}
    378304
    379305/**
  • tests/phpunit/testcases/activity/actions.php

     
    4949                // 'new' => 'publish'
    5050                $this->assertTrue( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Published post type should have activity' );
    5151
    52                 // Delete the activity
    53                 bp_activity_post_type_unpublish( $post_id, $post );
    54 
    55                 $post->post_status = 'publish';
    56                 $post->post_content .= ' foo';
    57 
    58                 wp_update_post( $post );
    59 
    60                 $this->assertFalse( $this->activity_exists_for_post( $post_id, 'new_foo' ), 'Updating a post type should not create a new activity' );
    61 
    6252                _unregister_post_type( 'foo' );
    6353        }
    6454
     
    123113                _unregister_post_type( 'foo' );
    124114        }
    125115
     116        /**
     117         * @group bp_activity_catch_transition_post_type_status
     118         * @group activity_tracking
     119         */
     120        public function test_bp_activity_catch_transition_post_type_status_publish_existing_post() {
     121                $u = $this->factory->user->create();
     122
     123                $labels = array(
     124                        'bp_activity_admin_filter' => 'New Foo',
     125                        'bp_activity_front_filter' => 'Foos',
     126                        'bp_activity_new_post'    => '%1$s posted a new <a href="%2$s">foo</a>',
     127                        'bp_activity_new_post_ms' => '%1$s posted a new <a href="%2$s">foo</a>, on the site %3$s',
     128                );
     129
     130                /**
     131                 * 'public' must be set to true, otherwise bp_activity_get_post_types_tracking_args() fails.
     132                 */
     133                register_post_type( 'foo', array(
     134                        'labels'      => $labels,
     135                        'public'      => true,
     136                        'supports'    => array( 'buddypress-activity' ),
     137                        'bp_activity' => array(
     138                                'action_id'    => 'new_foo',
     139                                'contexts'     => array( 'activity' ),
     140                                'position'     => 40,
     141                        )
     142                ) );
     143
     144                // Temporarily remove post type activity hook so activity item isn't created.
     145                remove_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
     146
     147                // Create the initial post.
     148                $p = $this->factory->post->create( array(
     149                        'post_author' => $u,
     150                        'post_type'   => 'foo',
     151                ) );
     152
     153                $this->assertEmpty( bp_activity_get_activity_id( array( 'type' => 'new_foo' ) ) );
     154
     155                // Add the post type activity hook back.
     156                add_action( 'transition_post_status', 'bp_activity_catch_transition_post_type_status', 10, 3 );
     157
     158                // Emulate updating a post; this should create an activity item.
     159                wp_update_post( array(
     160                        'ID'     => $p,
     161                        'post_title' => 'This is an edit',
     162                ) );
     163
     164                // Assert!
     165                $this->assertNotEmpty( bp_activity_get_activity_id( array( 'type' => 'new_foo' ) ), 'Activity item was not created during an edit of an existing WordPress post.' );
     166
     167                // Clean up.
     168                _unregister_post_type( 'foo' );
     169        }
     170
    126171        protected function activity_exists_for_post( $post_id, $action ) {
    127172                $a = bp_activity_get( array(
    128173                        'action'            => $action,