Skip to:
Content

BuddyPress.org

Ticket #5130: 5130.01.patch

File 5130.01.patch, 12.1 KB (added by r-a-y, 13 years ago)
  • bp-activity/bp-activity-functions.php

    function bp_activity_new_comment( $args = '' ) {  
    12011201        // Clear the comment cache for this activity
    12021202        wp_cache_delete( 'bp_activity_comments_' . $parent_id );
    12031203
    1204         do_action( 'bp_activity_comment_posted', $comment_id, $params );
     1204        do_action( 'bp_activity_comment_posted', $comment_id, $params, $activity );
    12051205
    12061206        return $comment_id;
    12071207}
    function bp_activity_mark_as_ham( &$activity, $source = 'by_a_person' ) {  
    16581658        do_action( 'bp_activity_mark_as_ham', $activity, $source );
    16591659}
    16601660
    1661 
    16621661/** Embeds *******************************************************************/
    16631662
    16641663/**
  • bp-blogs/bp-blogs-activity.php

    function bp_blogs_delete_activity( $args = true ) {  
    128128                'secondary_item_id' => $secondary_item_id
    129129        ) );
    130130}
     131
     132/**
     133 * Syncs activity comments and posts them back as blog comments.
     134 *
     135 * Note: This is only a one-way sync - activity comments -> blog comment.
     136 *
     137 * For example, if a blog comment is made from a blog post page, that comment
     138 * will *not* be added into the nested blog post's activity item.  It will
     139 * create a new activity entry.  This needs additional R&D.
     140 *
     141 * @since BuddyPress (1.9)
     142 *
     143 * @param int $comment_id The activity ID for the posted activity comment.
     144 * @param array $params Parameters for the activity comment.
     145 * @param obj Parameters of the parent activity item (in this case, the blog post).
     146 */
     147function bp_blogs_sync_add_from_activity_comment( $comment_id, $params, $parent_activity ) {
     148        // if parent activity isn't a blog post, stop now!
     149        if ( $parent_activity->type != 'new_blog_post' ) {
     150                return;
     151        }
     152
     153        // if activity comments are disabled for blog posts, stop now!
     154        //
     155        // not using bp_disable_blogforum_comments() as that uses bp_get_option(),
     156        // which would suck on multisite installs.
     157        $cannot_comment = isset( buddypress()->site_options['bp-disable-blogforum-comments'] ) ? buddypress()->site_options['bp-disable-blogforum-comments'] : false;
     158
     159        // if cannot comment, stop now!
     160        if ( $cannot_comment ) {
     161                return;
     162        }
     163
     164        // get userdata
     165        if ( $params['user_id'] == bp_loggedin_user_id() ) {
     166                $user = buddypress()->loggedin_user->userdata;
     167        } else {
     168                $user = bp_core_get_core_userdata( $params['user_id'] );
     169        }
     170
     171        // see if a parent WP comment ID exists
     172        if ( ! empty( $params['parent_id'] ) ) {
     173                $comment_parent = bp_activity_get_meta( $params['parent_id'], 'bp_blogs_post_comment_id' );
     174        } else {
     175                $comment_parent = 0;
     176        }
     177
     178        // comment args
     179        $args = array(
     180                'comment_post_ID'      => $parent_activity->secondary_item_id,
     181
     182                // should this use display name?
     183                // using username for now
     184                'comment_author'       => bp_core_get_username( $params['user_id'], $user->user_nicename, $user->user_login ),
     185
     186                'comment_author_email' => $user->user_email,
     187                'comment_author_url'   => bp_core_get_user_domain( $params['user_id'], $user->user_nicename, $user->user_login ),
     188                'comment_content'      => $params['content'],
     189                'comment_type'         => '', // could be interesting to add 'buddypress' here...
     190                'comment_parent'       => (int) $comment_parent,
     191                'user_id'              => $params['user_id'],
     192
     193                // commenting these out for now
     194                //'comment_author_IP'    => '127.0.0.1',
     195                //'comment_agent'        => '',
     196
     197                'comment_date'         => bp_core_current_time(),
     198                'comment_approved'     => 1             
     199        );
     200
     201        // prevent separate activity entry being made
     202        remove_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     203
     204        // handle multisite
     205        switch_to_blog( $parent_activity->item_id );
     206
     207        // post the comment
     208        $post_comment_id = wp_insert_comment( $args );
     209       
     210        // add meta to comment
     211        add_comment_meta( $post_comment_id, 'bp_activity_comment_id', $comment_id );
     212
     213        // add meta to activity comment
     214        bp_activity_update_meta( $comment_id, 'bp_blogs_post_comment_id', $post_comment_id );
     215
     216        // multisite again!
     217        restore_current_blog();
     218
     219        do_action( 'bp_blogs_sync_add_from_activity_comment', $comment_id, $args, $parent_activity, $user );
     220}
     221
     222/**
     223 * Deletes the blog comment when the associated activity comment is deleted.
     224 *
     225 * @since BuddyPress (1.9)
     226 *
     227 * @param int $activity_id The activity ID for the posted activity comment.
     228 * @param array $params Parameters for the activity comment.
     229 * @param obj Parameters of the parent activity item (in this case, the blog post).
     230 */
     231function bp_blogs_sync_delete_from_activity_comment( $activity_id, $comment_id ) {
     232        // not using bp_disable_blogforum_comments() as that uses bp_get_option(),
     233        // which would suck on multisite installs.
     234        $cannot_comment = isset( buddypress()->site_options['bp-disable-blogforum-comments'] ) ? buddypress()->site_options['bp-disable-blogforum-comments'] : false;
     235
     236        // if cannot comment, stop now!
     237        if ( $cannot_comment ) {
     238                return;
     239        }
     240
     241        // query args
     242        $query_args = array(
     243                'meta_query' => array(
     244                        array(
     245                                'key'   => 'bp_activity_comment_id',
     246                                'value' => $comment_id
     247                        )
     248                )
     249        );
     250       
     251        // get comment
     252        $comment_query = new WP_Comment_Query;
     253        $comment = $comment_query->query( $query_args );
     254
     255        // no comment? stop now!
     256        if ( empty( $comment[0] ) ) {
     257                return;
     258        } else {
     259                $comment = $comment[0];
     260        }
     261
     262        // delete the comment
     263        // we force delete the comment, since activity items are deleted immediately
     264        wp_delete_comment( $comment->comment_ID, true );
     265}
     266
  • bp-blogs/bp-blogs-functions.php

    function bp_blogs_remove_post( $post_id, $blog_id = 0, $user_id = 0 ) {  
    504504add_action( 'delete_post', 'bp_blogs_remove_post' );
    505505
    506506function bp_blogs_remove_comment( $comment_id ) {
    507         global $wpdb;
     507        // get BP's activity comment status for blog / forum posts
     508        $cannot_activity_comment = isset( buddypress()->site_options['bp-disable-blogforum-comments'] ) ? buddypress()->site_options['bp-disable-blogforum-comments'] : false;
    508509
    509         // Delete activity stream item
    510         bp_blogs_delete_activity( array( 'item_id' => $wpdb->blogid, 'secondary_item_id' => $comment_id, 'type' => 'new_blog_comment' ) );
     510        // activity comments are disabled for blog posts
     511        // which means that individual activity items exist for blog comments
     512        if ( $cannot_activity_comment ) {
     513                global $wpdb;
     514
     515                // Delete the individual activity stream item
     516                bp_blogs_delete_activity( array( 'item_id' => $wpdb->blogid, 'secondary_item_id' => $comment_id, 'type' => 'new_blog_comment' ) );
     517
     518        // activity comments are enabled for blog posts
     519        // remove the associated activity item
     520        } else {
     521                // get associated activity ID from comment meta
     522                $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
     523
     524                // delete the associated activity comment
     525                //
     526                // @todo WP moves nested comments up a level when a comment is deleted,
     527                //       while BP deletes all nested activity comments...
     528                //
     529                //       right now, we're simply deleting the singular activity comment
     530                //       with bp_activity_delete() and not using bp_activity_delete_comment()
     531                //       b/c that deletes activity comment children
     532                //
     533                //       this needs further research:
     534                //       - how hard would it be to emulate WP's model?
     535                //       - what would happen if BP_Activity_Activity::rebuild_activity_comment_tree()
     536                //         is used when the main activity comment is not deleted?
     537                bp_activity_delete( array(
     538                        'id' => $activity_id
     539                ) );
     540        }
    511541
    512542        do_action( 'bp_blogs_remove_comment', $wpdb->blogid, $comment_id, bp_loggedin_user_id() );
    513543}
  • bp-blogs/bp-blogs-loader.php

    class BP_Blogs_Component extends BP_Component {  
    6262
    6363                // Setup the globals
    6464                parent::setup_globals( $globals );
     65
     66                // Activity comment post callback
     67                $this->activity_comment_post_callback = apply_filters( 'bp_blogs_activity_comment_post_callback', array(
     68                        'add'    => 'bp_blogs_sync_add_from_activity_comment',
     69                        'delete' => 'bp_blogs_sync_delete_from_activity_comment'
     70                ) );
    6571        }
    6672
    6773        /**
  • bp-core/bp-core-component.php

    class BP_Component {  
    247247        public function setup_actions() {
    248248
    249249                // Setup globals
    250                 add_action( 'bp_setup_globals',          array( $this, 'setup_globals'          ), 10 );
     250                add_action( 'bp_setup_globals',          array( $this, 'setup_globals'                  ), 10 );
    251251
    252252                // Include required files. Called early to ensure that BP core
    253253                // components are loaded before plugins that hook their loader functions
    254254                // to bp_include with the default priority of 10. This is for backwards
    255255                // compatibility; henceforth, plugins should register themselves by
    256256                // extending this base class.
    257                 add_action( 'bp_include',                array( $this, 'includes'               ), 8 );
     257                add_action( 'bp_include',                array( $this, 'includes'                       ), 10 );
    258258
    259259                // Setup navigation
    260                 add_action( 'bp_setup_nav',              array( $this, 'setup_nav'              ), 10 );
     260                add_action( 'bp_setup_nav',              array( $this, 'setup_nav'                      ), 10 );
    261261
    262262                // Setup WP Toolbar menus
    263                 add_action( 'bp_setup_admin_bar',        array( $this, 'setup_admin_bar'        ), 10 );
     263                add_action( 'bp_setup_admin_bar',        array( $this, 'setup_admin_bar'                ), 10 );
    264264
    265265                // Setup component title
    266                 add_action( 'bp_setup_title',            array( $this, 'setup_title'            ), 10 );
     266                add_action( 'bp_setup_title',            array( $this, 'setup_title'                    ), 10 );
    267267
    268268                // Register post types
    269                 add_action( 'bp_register_post_types',    array( $this, 'register_post_types'    ), 10 );
     269                add_action( 'bp_register_post_types',    array( $this, 'register_post_types'            ), 10 );
    270270
    271271                // Register taxonomies
    272                 add_action( 'bp_register_taxonomies',    array( $this, 'register_taxonomies'    ), 10 );
     272                add_action( 'bp_register_taxonomies',    array( $this, 'register_taxonomies'            ), 10 );
    273273
    274274                // Add the rewrite tags
    275                 add_action( 'bp_add_rewrite_tags',       array( $this, 'add_rewrite_tags'       ), 10 );
     275                add_action( 'bp_add_rewrite_tags',       array( $this, 'add_rewrite_tags'               ), 10 );
    276276
    277277                // Generate rewrite rules
    278                 add_action( 'bp_generate_rewrite_rules', array( $this, 'generate_rewrite_rules' ), 10 );
     278                add_action( 'bp_generate_rewrite_rules', array( $this, 'generate_rewrite_rules'         ), 10 );
     279
     280                // Activity comment callback setup
     281                add_action( 'bp_setup_globals',          array( $this, 'activity_comment_post_callback' ), 10 );
    279282
    280283                // Additional actions can be attached here
    281284                do_action( 'bp_' . $this->id . '_setup_actions' );
    class BP_Component {  
    394397        public function generate_rewrite_rules() {
    395398                do_action( 'bp_' . $this->id . '_generate_rewrite_rules' );
    396399        }
     400
     401        /**
     402         * Setup activity comment post callbacka.
     403         *
     404         * If a component has the 'activity_comment_post_callback' property set,
     405         * automatically setup the hooks to be used.
     406         *
     407         * @since BuddyPress (1.9)
     408         */
     409        public function activity_comment_post_callback() {
     410                // if activity component is not active or callback is not an array, stop now!
     411                if ( ! bp_is_active( 'activity' ) || ! is_array( $this->activity_comment_post_callback ) ) {
     412                        return;
     413                }
     414
     415                $callback = $this->activity_comment_post_callback;
     416
     417                // Activity comment addition callback
     418                if ( ! empty( $callback['add'] ) && is_callable( $callback['add'] ) ) {
     419                        add_action( 'bp_activity_comment_posted', $callback['add'],    10, 3 );
     420                }
     421
     422                // Activity comment deletion callback
     423                if ( ! empty( $callback['delete'] ) && is_callable( $callback['delete'] ) ) {
     424                        add_action( 'bp_activity_delete_comment', $callback['delete'], 10, 2 );
     425                }
     426
     427        }
    397428}
    398429endif; // BP_Component