<!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][9182] trunk/src: Messages: Introduces meta table and related cache functions.</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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/9182">9182</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/9182","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>r-a-y</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2014-11-26 06:08:03 +0000 (Wed, 26 Nov 2014)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Messages: Introduces meta table and related cache functions.
This commit alters the messages DB schema to add a meta table, which will
allow plugin developers to record information about a message.
The new message meta functions in bp-messages-functions.php mirrors those
in other BP components (activity, groups, etc.).
The DB version is also bumped to 9181.
See <a href="http://buddypress.trac.wordpress.org/ticket/3083">#3083</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpcoreadminbpcoreschemaphp">trunk/src/bp-core/admin/bp-core-schema.php</a></li>
<li><a href="#trunksrcbpcorebpcoreupdatephp">trunk/src/bp-core/bp-core-update.php</a></li>
<li><a href="#trunksrcbploaderphp">trunk/src/bp-loader.php</a></li>
<li><a href="#trunksrcbpmessagesbpmessagescachephp">trunk/src/bp-messages/bp-messages-cache.php</a></li>
<li><a href="#trunksrcbpmessagesbpmessagesfunctionsphp">trunk/src/bp-messages/bp-messages-functions.php</a></li>
<li><a href="#trunksrcbpmessagesbpmessagesloaderphp">trunk/src/bp-messages/bp-messages-loader.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpcoreadminbpcoreschemaphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/admin/bp-core-schema.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/admin/bp-core-schema.php 2014-11-25 22:32:19 UTC (rev 9181)
+++ trunk/src/bp-core/admin/bp-core-schema.php 2014-11-26 06:08:03 UTC (rev 9182)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -304,6 +304,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> KEY is_active (is_active)
</span><span class="cx" style="display: block; padding: 0 10px"> ) {$charset_collate};";
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sql[] = "CREATE TABLE {$bp_prefix}bp_messages_meta (
+ id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
+ message_id bigint(20) NOT NULL,
+ meta_key varchar(255) DEFAULT NULL,
+ meta_value longtext DEFAULT NULL,
+ KEY message_id (message_id),
+ KEY meta_key (meta_key)
+ ) {$charset_collate};";
+
</ins><span class="cx" style="display: block; padding: 0 10px"> dbDelta( $sql );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcbpcorebpcoreupdatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-core/bp-core-update.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-core/bp-core-update.php 2014-11-25 22:32:19 UTC (rev 9181)
+++ trunk/src/bp-core/bp-core-update.php 2014-11-26 06:08:03 UTC (rev 9182)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -241,6 +241,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $raw_db_version < 8311 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> bp_update_to_2_0_1();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // 2.2
+ if ( $raw_db_version < 9181 ) {
+ bp_update_to_2_2();
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /** All done! *************************************************************/
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -390,6 +395,19 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * 2.2.0 update routine.
+ *
+ * - Add messages meta table
+ *
+ * @since BuddyPress (2.2.0)
+ */
+function bp_update_to_2_2() {
+ if ( bp_is_active( 'messages' ) ) {
+ bp_core_install_private_messaging();
+ }
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Redirect user to BP's What's New page on first page load after activation.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since BuddyPress (1.7.0)
</span></span></pre></div>
<a id="trunksrcbploaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-loader.php 2014-11-25 22:32:19 UTC (rev 9181)
+++ trunk/src/bp-loader.php 2014-11-26 06:08:03 UTC (rev 9182)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,7 +301,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /** Versions **********************************************************/
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->version = '2.2-alpha';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->db_version = 9167;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->db_version = 9181;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /** Loading ***********************************************************/
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcbpmessagesbpmessagescachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/bp-messages-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/bp-messages-cache.php 2014-11-25 22:32:19 UTC (rev 9181)
+++ trunk/src/bp-messages/bp-messages-cache.php 2014-11-26 06:08:03 UTC (rev 9182)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +12,29 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Exit if accessed directly
</span><span class="cx" style="display: block; padding: 0 10px"> if ( !defined( 'ABSPATH' ) ) exit;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Slurp up metadata for a set of messages.
+ *
+ * It grabs all message meta associated with all of the messages passed in
+ * $message_ids and adds it to WP cache. This improves efficiency when using
+ * message meta within a loop context.
+ *
+ * @since BuddyPress (2.2.0)
+ *
+ * @param int|str|array $message_ids Accepts a single message_id, or a
+ * comma-separated list or array of message ids.
+ */
+function bp_messages_update_meta_cache( $message_ids = false ) {
+ bp_update_meta_cache( array(
+ 'object_ids' => $message_ids,
+ 'object_type' => buddypress()->messages->id,
+ 'cache_group' => 'message_meta',
+ 'object_column' => 'message_id',
+ 'meta_table' => buddypress()->messages->table_name_meta,
+ 'cache_key_prefix' => 'bp_messages_meta'
+ ) );
+}
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // List actions to clear super cached pages on, if super cache is installed
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'messages_delete_thread', 'bp_core_clear_cache' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'messages_send_notice', 'bp_core_clear_cache' );
</span></span></pre></div>
<a id="trunksrcbpmessagesbpmessagesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/bp-messages-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/bp-messages-functions.php 2014-11-25 22:32:19 UTC (rev 9181)
+++ trunk/src/bp-messages/bp-messages-functions.php 2014-11-26 06:08:03 UTC (rev 9182)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -335,3 +335,88 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function messages_is_valid_thread( $thread_id ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return BP_Messages_Thread::is_valid( $thread_id );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/** Messages Meta *******************************************************/
+
+/**
+ * Delete metadata for a message.
+ *
+ * If $meta_key is false, this will delete all meta for the message ID.
+ *
+ * @since BuddyPress (2.2.0)
+ *
+ * @see delete_metadata() for full documentation excluding $meta_type variable.
+ */
+function bp_messages_delete_meta( $message_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
+ // Legacy - if no meta_key is passed, delete all for the item
+ if ( empty( $meta_key ) ) {
+ global $wpdb;
+
+ $keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->messagemeta} WHERE message_id = %d", $message_id ) );
+
+ // With no meta_key, ignore $delete_all
+ $delete_all = false;
+ } else {
+ $keys = array( $meta_key );
+ }
+
+ // no keys, so stop now!
+ if ( empty( $keys ) ) {
+ return false;
+ }
+
+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+
+ foreach ( $keys as $key ) {
+ $retval = delete_metadata( 'message', $message_id, $key, $meta_value, $delete_all );
+ }
+
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
+
+ return $retval;
+}
+
+/**
+ * Get a piece of message metadata.
+ *
+ * @since BuddyPress (2.2.0)
+ *
+ * @see get_metadata() for full documentation excluding $meta_type variable.
+ */
+function bp_messages_get_meta( $message_id, $meta_key = '', $single = true ) {
+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+ $retval = get_metadata( 'message', $message_id, $meta_key, $single );
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
+
+ return $retval;
+}
+
+/**
+ * Update a piece of message metadata.
+ *
+ * @since BuddyPress (2.2.0)
+ *
+ * @see update_metadata() for full documentation excluding $meta_type variable.
+ */
+function bp_messages_update_meta( $message_id, $meta_key, $meta_value, $prev_value = '' ) {
+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+ $retval = update_metadata( 'message', $message_id, $meta_key, $meta_value, $prev_value );
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
+
+ return $retval;
+}
+
+/**
+ * Add a piece of message metadata.
+ *
+ * @since BuddyPress (2.2.0)
+ *
+ * @see add_metadata() for full documentation excluding $meta_type variable.
+ */
+function bp_message_add_meta( $message_id, $meta_key, $meta_value, $unique = false ) {
+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+ $retval = add_metadata( 'message', $message_id, $meta_key, $meta_value, $unique );
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
+
+ return $retval;
+}
</ins></span></pre></div>
<a id="trunksrcbpmessagesbpmessagesloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-messages/bp-messages-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-messages/bp-messages-loader.php 2014-11-25 22:32:19 UTC (rev 9181)
+++ trunk/src/bp-messages/bp-messages-loader.php 2014-11-26 06:08:03 UTC (rev 9182)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -90,22 +90,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $global_tables = array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'table_name_notices' => $bp->table_prefix . 'bp_messages_notices',
</span><span class="cx" style="display: block; padding: 0 10px"> 'table_name_messages' => $bp->table_prefix . 'bp_messages_messages',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'table_name_recipients' => $bp->table_prefix . 'bp_messages_recipients'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'table_name_recipients' => $bp->table_prefix . 'bp_messages_recipients',
+ 'table_name_meta' => $bp->table_prefix . 'bp_messages_meta',
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Metadata tables for messaging component
+ $meta_tables = array(
+ 'message' => $bp->table_prefix . 'bp_messages_meta',
+ );
+
+ $this->autocomplete_all = defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // All globals for messaging component.
</span><span class="cx" style="display: block; padding: 0 10px"> // Note that global_tables is included in this array.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $globals = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ parent::setup_globals( array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'slug' => BP_MESSAGES_SLUG,
</span><span class="cx" style="display: block; padding: 0 10px"> 'has_directory' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'notification_callback' => 'messages_format_notifications',
</span><span class="cx" style="display: block; padding: 0 10px"> 'search_string' => __( 'Search Messages...', 'buddypress' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'global_tables' => $global_tables
- );
-
- $this->autocomplete_all = defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' );
-
- parent::setup_globals( $globals );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'global_tables' => $global_tables,
+ 'meta_tables' => $meta_tables
+ ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre>
</div>
</div>
</body>
</html>