Skip to:
Content

BuddyPress.org

Changeset 8189


Ignore:
Timestamp:
03/28/2014 10:33:16 PM (10 years ago)
Author:
r-a-y
Message:

Blogs: Mirror a blog's various comment options to BP's blogmeta.

This commit mirrors the following blog options to BP's blogmeta:

  • 'close_comments_for_old_posts'
  • 'close_comment_days_old'
  • 'thread_comments_depth'

These options will be used later in the new bp_blogs_comments_open()
function to determine whether a blog post's activity item should be
closed from commenting. This is designed to prevent multiple calls
to switch_to_blog().

See #5130

Location:
trunk/bp-blogs
Files:
2 edited

Legend:

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

    r8126 r8189  
    346346    ) );
    347347}
     348
     349/**
     350 * Check if a blog post's activity item should be closed from commenting.
     351 *
     352 * This mirrors the {@link comments_open()} and {@link _close_comments_for_old_post()}
     353 * functions, but for use with the BuddyPress activity stream to be as
     354 * lightweight as possible.
     355 *
     356 * By lightweight, we actually mirror a few of the blog's commenting settings
     357 * to blogmeta and checks the values in blogmeta instead.  This is to prevent
     358 * multiple {@link switch_to_blog()} calls in the activity stream.
     359 *
     360 * @since BuddyPress (2.0.0)
     361 *
     362 * @param object $activity The BP_Activity_Activity object
     363 * @return bool
     364 */
     365function bp_blogs_comments_open( $activity ) {
     366    $open = true;
     367
     368    $blog_id = $activity->item_id;
     369
     370    // see if we've mirrored the close comments option before
     371    $days_old = bp_blogs_get_blogmeta( $blog_id, 'close_comments_days_old' );
     372
     373    // we've never cached these items before, so do it now
     374    if ( '' === $days_old ) {
     375        switch_to_blog( $blog_id );
     376
     377        // use comments_open()
     378        remove_filter( 'comments_open', 'bp_comments_open', 10, 2 );
     379        $open = comments_open( $activity->secondary_item_id );
     380        add_filter( 'comments_open', 'bp_comments_open', 10, 2 );
     381
     382        // might as well mirror values to blogmeta since we're here!
     383        $thread_depth = get_option( 'thread_comments' );
     384        if ( ! empty( $thread_depth ) ) {
     385            $thread_depth = get_option( 'thread_comments_depth' );
     386        } else {
     387            // perhaps filter this?
     388            $thread_depth = 1;
     389        }
     390
     391        bp_blogs_update_blogmeta( $blog_id, 'close_comments_for_old_posts', get_option( 'close_comments_for_old_posts' ) );
     392        bp_blogs_update_blogmeta( $blog_id, 'close_comments_days_old',      get_option( 'close_comments_days_old' ) );
     393        bp_blogs_update_blogmeta( $blog_id, 'thread_comments_depth',        $thread_depth );
     394
     395        restore_current_blog();
     396
     397    // check blogmeta and manually check activity item
     398    // basically a copy of _close_comments_for_old_post()
     399    } else {
     400
     401        // comments are closed
     402        if ( 'closed' == bp_activity_get_meta( $activity->id, 'post_comment_status' ) ) {
     403            return false;
     404        }
     405
     406        if ( ! bp_blogs_get_blogmeta( $blog_id, 'close_comments_for_old_posts' ) ) {
     407            return $open;
     408        }
     409
     410        $days_old = (int) $days_old;
     411        if ( ! $days_old ) {
     412            return $open;
     413        }
     414
     415        /* commenting out for now - needs some more thought...
     416           should we add the post type to activity meta?
     417
     418        $post = get_post($post_id);
     419
     420        // This filter is documented in wp-includes/comment.php
     421        $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
     422        if ( ! in_array( $post->post_type, $post_types ) )
     423            return $open;
     424        */
     425
     426        if ( time() - strtotime( $activity->date_recorded ) > ( $days_old * DAY_IN_SECONDS ) ) {
     427            return false;
     428        }
     429
     430        return $open;
     431    }
     432
     433    return $open;
     434}
  • trunk/bp-blogs/bp-blogs-functions.php

    r8135 r8189  
    183183        return false;
    184184
    185     $url         = get_home_url( $blog_id );
    186     $name        = get_blog_option( $blog_id, 'blogname' );
    187     $description = get_blog_option( $blog_id, 'blogdescription' );
    188 
    189     if ( empty( $name ) )
    190         return false;
     185    $name = get_blog_option( $blog_id, 'blogname' );
     186
     187    if ( empty( $name ) ) {
     188        return false;
     189    }
     190
     191    $url             = get_home_url( $blog_id );
     192    $description     = get_blog_option( $blog_id, 'blogdescription' );
     193    $close_old_posts = get_blog_option( $blog_id, 'close_comments_for_old_posts' );
     194    $close_days_old  = get_blog_option( $blog_id, 'close_comments_days_old' );
     195
     196    $thread_depth = get_blog_option( $blog_id, 'thread_comments' );
     197    if ( ! empty( $thread_depth ) ) {
     198        $thread_depth = get_blog_option( $blog_id, 'thread_comments_depth' );
     199    } else {
     200        // perhaps filter this?
     201        $thread_depth = 1;
     202    }
    191203
    192204    $recorded_blog          = new BP_Blogs_Blog;
     
    200212    bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'description', $description );
    201213    bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', bp_core_current_time() );
     214    bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_for_old_posts', $close_old_posts );
     215    bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_days_old', $close_days_old );
     216    bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'thread_comments_depth', $thread_depth );
    202217
    203218    $is_private = !empty( $_POST['blog_public'] ) && (int) $_POST['blog_public'] ? false : true;
     
    251266}
    252267add_action( 'update_option_blogdescription', 'bp_blogs_update_option_blogdescription', 10, 2 );
     268
     269/**
     270 * Update "Close comments for old posts" option in BuddyPress blogmeta table.
     271 *
     272 * @since BuddyPress (2.0.0)
     273 *
     274 * @global object $wpdb DB Layer.
     275 *
     276 * @param string $oldvalue Value before save. Passed by do_action() but
     277 *        unused here.
     278 * @param string $newvalue Value to change meta to.
     279 */
     280function bp_blogs_update_option_close_comments_for_old_posts( $oldvalue, $newvalue ) {
     281    global $wpdb;
     282
     283    bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_for_old_posts', $newvalue );
     284}
     285add_action( 'update_option_close_comments_for_old_posts', 'bp_blogs_update_option_close_comments_for_old_posts', 10, 2 );
     286
     287/**
     288 * Update "Close comments after days old" option in BuddyPress blogmeta table.
     289 *
     290 * @since BuddyPress (2.0.0)
     291 *
     292 * @global object $wpdb DB Layer.
     293 *
     294 * @param string $oldvalue Value before save. Passed by do_action() but
     295 *        unused here.
     296 * @param string $newvalue Value to change meta to.
     297 */
     298function bp_blogs_update_option_close_close_comments_days_old( $oldvalue, $newvalue ) {
     299    global $wpdb;
     300
     301    bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_days_old', $newvalue );
     302}
     303add_action( 'update_option_close_comments_days_old', 'bp_blogs_update_option_close_comments_days_old', 10, 2 );
     304
     305/**
     306 * When toggling threaded comments, update thread depth in blogmeta table.
     307 *
     308 * @since BuddyPress (2.0.0)
     309 *
     310 * @global object $wpdb DB Layer.
     311 *
     312 * @param string $oldvalue Value before save. Passed by do_action() but
     313 *        unused here.
     314 * @param string $newvalue Value to change meta to.
     315 */
     316function bp_blogs_update_option_thread_comments( $oldvalue, $newvalue ) {
     317    global $wpdb;
     318
     319    if ( empty( $newvalue ) ) {
     320        $thread_depth = 1;
     321    } else {
     322        $thread_depth = get_option( 'thread_comments_depth' );
     323    }
     324
     325    bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $thread_depth );
     326}
     327add_action( 'update_option_thread_comments', 'bp_blogs_update_option_thread_comments', 10, 2 );
     328
     329/**
     330 * When updating comment depth, update thread depth in blogmeta table.
     331 *
     332 * @since BuddyPress (2.0.0)
     333 *
     334 * @global object $wpdb DB Layer.
     335 *
     336 * @param string $oldvalue Value before save. Passed by do_action() but
     337 *        unused here.
     338 * @param string $newvalue Value to change meta to.
     339 */
     340function bp_blogs_update_option_thread_comments_depth( $oldvalue, $newvalue ) {
     341    global $wpdb;
     342
     343    $comments_enabled = get_option( 'thread_comments' );
     344
     345    if (  $comments_enabled ) {
     346        bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $newvalue );
     347    }
     348}
     349add_action( 'update_option_thread_comments_depth', 'bp_blogs_update_option_thread_comments_depth', 10, 2 );
    253350
    254351/**
Note: See TracChangeset for help on using the changeset viewer.