<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][8189] trunk/bp-blogs: Blogs: Mirror a blog's various comment options to BP's blogmeta.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://buddypress.trac.wordpress.org/changeset/8189">8189</a></dd>
<dt>Author</dt> <dd>r-a-y</dd>
<dt>Date</dt> <dd>2014-03-28 22:33:16 +0000 (Fri, 28 Mar 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>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 <a href="http://buddypress.trac.wordpress.org/ticket/5130">#5130</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpblogsbpblogsactivityphp">trunk/bp-blogs/bp-blogs-activity.php</a></li>
<li><a href="#trunkbpblogsbpblogsfunctionsphp">trunk/bp-blogs/bp-blogs-functions.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpblogsbpblogsactivityphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-activity.php (8188 => 8189)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-activity.php 2014-03-28 22:16:42 UTC (rev 8188)
+++ trunk/bp-blogs/bp-blogs-activity.php 2014-03-28 22:33:16 UTC (rev 8189)
</span><span class="lines">@@ -345,3 +345,90 @@
</span><span class="cx"> 'secondary_item_id' => $secondary_item_id
</span><span class="cx"> ) );
</span><span class="cx"> }
</span><ins>+
+/**
+ * Check if a blog post's activity item should be closed from commenting.
+ *
+ * This mirrors the {@link comments_open()} and {@link _close_comments_for_old_post()}
+ * functions, but for use with the BuddyPress activity stream to be as
+ * lightweight as possible.
+ *
+ * By lightweight, we actually mirror a few of the blog's commenting settings
+ * to blogmeta and checks the values in blogmeta instead. This is to prevent
+ * multiple {@link switch_to_blog()} calls in the activity stream.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @param object $activity The BP_Activity_Activity object
+ * @return bool
+ */
+function bp_blogs_comments_open( $activity ) {
+ $open = true;
+
+ $blog_id = $activity->item_id;
+
+ // see if we've mirrored the close comments option before
+ $days_old = bp_blogs_get_blogmeta( $blog_id, 'close_comments_days_old' );
+
+ // we've never cached these items before, so do it now
+ if ( '' === $days_old ) {
+ switch_to_blog( $blog_id );
+
+ // use comments_open()
+ remove_filter( 'comments_open', 'bp_comments_open', 10, 2 );
+ $open = comments_open( $activity->secondary_item_id );
+ add_filter( 'comments_open', 'bp_comments_open', 10, 2 );
+
+ // might as well mirror values to blogmeta since we're here!
+ $thread_depth = get_option( 'thread_comments' );
+ if ( ! empty( $thread_depth ) ) {
+ $thread_depth = get_option( 'thread_comments_depth' );
+ } else {
+ // perhaps filter this?
+ $thread_depth = 1;
+ }
+
+ bp_blogs_update_blogmeta( $blog_id, 'close_comments_for_old_posts', get_option( 'close_comments_for_old_posts' ) );
+ bp_blogs_update_blogmeta( $blog_id, 'close_comments_days_old', get_option( 'close_comments_days_old' ) );
+ bp_blogs_update_blogmeta( $blog_id, 'thread_comments_depth', $thread_depth );
+
+ restore_current_blog();
+
+ // check blogmeta and manually check activity item
+ // basically a copy of _close_comments_for_old_post()
+ } else {
+
+ // comments are closed
+ if ( 'closed' == bp_activity_get_meta( $activity->id, 'post_comment_status' ) ) {
+ return false;
+ }
+
+ if ( ! bp_blogs_get_blogmeta( $blog_id, 'close_comments_for_old_posts' ) ) {
+ return $open;
+ }
+
+ $days_old = (int) $days_old;
+ if ( ! $days_old ) {
+ return $open;
+ }
+
+ /* commenting out for now - needs some more thought...
+ should we add the post type to activity meta?
+
+ $post = get_post($post_id);
+
+ // This filter is documented in wp-includes/comment.php
+ $post_types = apply_filters( 'close_comments_for_post_types', array( 'post' ) );
+ if ( ! in_array( $post->post_type, $post_types ) )
+ return $open;
+ */
+
+ if ( time() - strtotime( $activity->date_recorded ) > ( $days_old * DAY_IN_SECONDS ) ) {
+ return false;
+ }
+
+ return $open;
+ }
+
+ return $open;
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunkbpblogsbpblogsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-blogs/bp-blogs-functions.php (8188 => 8189)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-blogs/bp-blogs-functions.php 2014-03-28 22:16:42 UTC (rev 8188)
+++ trunk/bp-blogs/bp-blogs-functions.php 2014-03-28 22:33:16 UTC (rev 8189)
</span><span class="lines">@@ -182,13 +182,25 @@
</span><span class="cx"> if ( !bp_blogs_is_blog_recordable( $blog_id, $user_id ) )
</span><span class="cx"> return false;
</span><span class="cx">
</span><del>- $url = get_home_url( $blog_id );
- $name = get_blog_option( $blog_id, 'blogname' );
- $description = get_blog_option( $blog_id, 'blogdescription' );
</del><ins>+ $name = get_blog_option( $blog_id, 'blogname' );
</ins><span class="cx">
</span><del>- if ( empty( $name ) )
</del><ins>+ if ( empty( $name ) ) {
</ins><span class="cx"> return false;
</span><ins>+ }
</ins><span class="cx">
</span><ins>+ $url = get_home_url( $blog_id );
+ $description = get_blog_option( $blog_id, 'blogdescription' );
+ $close_old_posts = get_blog_option( $blog_id, 'close_comments_for_old_posts' );
+ $close_days_old = get_blog_option( $blog_id, 'close_comments_days_old' );
+
+ $thread_depth = get_blog_option( $blog_id, 'thread_comments' );
+ if ( ! empty( $thread_depth ) ) {
+ $thread_depth = get_blog_option( $blog_id, 'thread_comments_depth' );
+ } else {
+ // perhaps filter this?
+ $thread_depth = 1;
+ }
+
</ins><span class="cx"> $recorded_blog = new BP_Blogs_Blog;
</span><span class="cx"> $recorded_blog->user_id = $user_id;
</span><span class="cx"> $recorded_blog->blog_id = $blog_id;
</span><span class="lines">@@ -199,6 +211,9 @@
</span><span class="cx"> bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'name', $name );
</span><span class="cx"> bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'description', $description );
</span><span class="cx"> bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', bp_core_current_time() );
</span><ins>+ bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_for_old_posts', $close_old_posts );
+ bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_days_old', $close_days_old );
+ bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'thread_comments_depth', $thread_depth );
</ins><span class="cx">
</span><span class="cx"> $is_private = !empty( $_POST['blog_public'] ) && (int) $_POST['blog_public'] ? false : true;
</span><span class="cx"> $is_private = !apply_filters( 'bp_is_new_blog_public', !$is_private );
</span><span class="lines">@@ -252,6 +267,88 @@
</span><span class="cx"> add_action( 'update_option_blogdescription', 'bp_blogs_update_option_blogdescription', 10, 2 );
</span><span class="cx">
</span><span class="cx"> /**
</span><ins>+ * Update "Close comments for old posts" option in BuddyPress blogmeta table.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @global object $wpdb DB Layer.
+ *
+ * @param string $oldvalue Value before save. Passed by do_action() but
+ * unused here.
+ * @param string $newvalue Value to change meta to.
+ */
+function bp_blogs_update_option_close_comments_for_old_posts( $oldvalue, $newvalue ) {
+ global $wpdb;
+
+ bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_for_old_posts', $newvalue );
+}
+add_action( 'update_option_close_comments_for_old_posts', 'bp_blogs_update_option_close_comments_for_old_posts', 10, 2 );
+
+/**
+ * Update "Close comments after days old" option in BuddyPress blogmeta table.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @global object $wpdb DB Layer.
+ *
+ * @param string $oldvalue Value before save. Passed by do_action() but
+ * unused here.
+ * @param string $newvalue Value to change meta to.
+ */
+function bp_blogs_update_option_close_close_comments_days_old( $oldvalue, $newvalue ) {
+ global $wpdb;
+
+ bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_days_old', $newvalue );
+}
+add_action( 'update_option_close_comments_days_old', 'bp_blogs_update_option_close_comments_days_old', 10, 2 );
+
+/**
+ * When toggling threaded comments, update thread depth in blogmeta table.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @global object $wpdb DB Layer.
+ *
+ * @param string $oldvalue Value before save. Passed by do_action() but
+ * unused here.
+ * @param string $newvalue Value to change meta to.
+ */
+function bp_blogs_update_option_thread_comments( $oldvalue, $newvalue ) {
+ global $wpdb;
+
+ if ( empty( $newvalue ) ) {
+ $thread_depth = 1;
+ } else {
+ $thread_depth = get_option( 'thread_comments_depth' );
+ }
+
+ bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $thread_depth );
+}
+add_action( 'update_option_thread_comments', 'bp_blogs_update_option_thread_comments', 10, 2 );
+
+/**
+ * When updating comment depth, update thread depth in blogmeta table.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @global object $wpdb DB Layer.
+ *
+ * @param string $oldvalue Value before save. Passed by do_action() but
+ * unused here.
+ * @param string $newvalue Value to change meta to.
+ */
+function bp_blogs_update_option_thread_comments_depth( $oldvalue, $newvalue ) {
+ global $wpdb;
+
+ $comments_enabled = get_option( 'thread_comments' );
+
+ if ( $comments_enabled ) {
+ bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $newvalue );
+ }
+}
+add_action( 'update_option_thread_comments_depth', 'bp_blogs_update_option_thread_comments_depth', 10, 2 );
+
+/**
</ins><span class="cx"> * Detect a change in post status, and initiate an activity update if necessary.
</span><span class="cx"> *
</span><span class="cx"> * Posts get new activity updates when (a) they are being published, and (b)
</span></span></pre>
</div>
</div>
</body>
</html>