Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/28/2014 10:33:16 PM (12 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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.