<!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][7840] trunk: Migrate groupmeta functions to the WP metadata API</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/7840">7840</a></dd>
<dt>Author</dt> <dd>boonebgorges</dd>
<dt>Date</dt> <dd>2014-02-11 02:58:50 +0000 (Tue, 11 Feb 2014)</dd>
</dl>
<h3>Log Message</h3>
<pre>Migrate groupmeta functions to the WP metadata API
See <a href="http://buddypress.trac.wordpress.org/ticket/4551">#4551</a>
Props boonebgorges, johnjamesjacoby</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpcorebpcorefiltersphp">trunk/bp-core/bp-core-filters.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsfunctionsphp">trunk/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#trunkbpgroupsbpgroupsloaderphp">trunk/bp-groups/bp-groups-loader.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpcorebpcorefiltersphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-core/bp-core-filters.php (7839 => 7840)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-core/bp-core-filters.php 2014-02-11 02:58:47 UTC (rev 7839)
+++ trunk/bp-core/bp-core-filters.php 2014-02-11 02:58:50 UTC (rev 7840)
</span><span class="lines">@@ -529,3 +529,23 @@
</span><span class="cx"> return $menu_item;
</span><span class="cx"> }
</span><span class="cx"> add_filter( 'wp_setup_nav_menu_item', 'bp_setup_nav_menu_item', 10, 1 );
</span><ins>+
+/**
+ * Filter SQL query strings to swap out the 'meta_id' column.
+ *
+ * WordPress uses the meta_id column for commentmeta and postmeta, and so
+ * hardcodes the column name into its *_metadata() functions. BuddyPress, on
+ * the other hand, uses 'id' for the primary column. To make WP's functions
+ * usable for BuddyPress, we use this just-in-time filter on 'query' to swap
+ * 'meta_id' with 'id.
+ *
+ * @since BuddyPress (2.0.0)
+ *
+ * @access private Do not use.
+ *
+ * @param string $q SQL query.
+ * @return string
+ */
+function bp_filter_metaid_column_name( $q ) {
+ return str_replace( 'meta_id', 'id', $q );
+}
</ins></span></pre></div>
<a id="trunkbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-functions.php (7839 => 7840)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-functions.php 2014-02-11 02:58:47 UTC (rev 7839)
+++ trunk/bp-groups/bp-groups-functions.php 2014-02-11 02:58:50 UTC (rev 7840)
</span><span class="lines">@@ -956,94 +956,85 @@
</span><span class="cx">
</span><span class="cx"> /*** Group Meta ****************************************************/
</span><span class="cx">
</span><del>-function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false ) {
- global $wpdb, $bp;
</del><ins>+/**
+ * Delete metadata for a group.
+ *
+ * @param int $group_id ID of the group.
+ * @param string $meta_key The key of the row to delete.
+ * @param string $meta_value Optional. Metadata value. If specified, only delete
+ * metadata entries with this value.
+ * @param bool $delete_all Optional. If true, delete matching metadata entries
+ * for all groups. Default: false.
+ * @return bool True on success, false on failure.
+ */
+function groups_delete_groupmeta( $group_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
</ins><span class="cx">
</span><del>- if ( !is_numeric( $group_id ) )
</del><ins>+ // Legacy - return false if non-int group ID
+ if ( ! is_numeric( $group_id ) ) {
</ins><span class="cx"> return false;
</span><ins>+ }
</ins><span class="cx">
</span><ins>+ // Legacy - Sanitize keys
</ins><span class="cx"> $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
</span><span class="cx">
</span><del>- if ( is_array( $meta_value ) || is_object( $meta_value ) )
- $meta_value = serialize($meta_value);
</del><ins>+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+ $retval = delete_metadata( 'group', $group_id, $meta_key, $meta_value, $delete_all );
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
</ins><span class="cx">
</span><del>- $meta_value = trim( $meta_value );
-
- if ( !$meta_key )
- $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
- else if ( $meta_value )
- $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s AND meta_value = %s", $group_id, $meta_key, $meta_value ) );
- else
- $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
-
- // Delete the cached object
- wp_cache_delete( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
-
- return true;
</del><ins>+ return $retval;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-function groups_get_groupmeta( $group_id, $meta_key = '') {
- global $wpdb, $bp;
</del><ins>+/**
+ * Get a piece of group metadata.
+ *
+ * @param int $group_id ID of the group.
+ * @param string $meta_key Metadata key.
+ * @param bool $single Optional. If true, return only the first value of the
+ * specified meta_key. This parameter has no effect if meta_key is
+ * empty.
+ * @return mixed Metadata value.
+ */
+function groups_get_groupmeta( $group_id, $meta_key = '', $single = true ) {
</ins><span class="cx">
</span><del>- $group_id = (int) $group_id;
</del><ins>+ // Legacy - Sanitize keys
+ $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
</ins><span class="cx">
</span><del>- if ( !$group_id )
- return false;
</del><ins>+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+ $retval = get_metadata( 'group', $group_id, $meta_key, $single );
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
</ins><span class="cx">
</span><del>- if ( !empty($meta_key) ) {
- $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
-
- $metas = wp_cache_get( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, 'bp' );
- if ( false === $metas ) {
- $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
- wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $metas, 'bp' );
</del><ins>+ // Legacy - If fetching all meta for a group, just return values
+ if ( empty( $meta_key ) ) {
+ $values = array();
+ foreach ( (array) $retval as $r ) {
+ $values[] = array_pop( $r );
</ins><span class="cx"> }
</span><del>- } else {
- $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d", $group_id ) );
</del><ins>+ $retval = $values;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( empty( $metas ) ) {
- if ( empty( $meta_key ) )
- return array();
- else
- return '';
- }
-
- $metas = array_map( 'maybe_unserialize', (array) $metas );
-
- if ( 1 == count( $metas ) )
- return $metas[0];
- else
- return $metas;
</del><ins>+ return $retval;
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Update a piece of group metadata.
+ *
+ * @param int $group_id ID of the group.
+ * @param string $meta_key Metadata key.
+ * @param mixed $meta_value Value to store.
+ * @return bool True on success, false on failure.
+ */
</ins><span class="cx"> function groups_update_groupmeta( $group_id, $meta_key, $meta_value ) {
</span><del>- global $wpdb, $bp;
</del><span class="cx">
</span><del>- if ( !is_numeric( $group_id ) )
- return false;
</del><ins>+ add_filter( 'query', 'bp_filter_metaid_column_name' );
+ $retval = update_metadata( 'group', $group_id, $meta_key, $meta_value );
+ remove_filter( 'query', 'bp_filter_metaid_column_name' );
</ins><span class="cx">
</span><del>- $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
-
- if ( is_string( $meta_value ) ) {
- $meta_value = stripslashes( $meta_value );
</del><ins>+ // Legacy - return true if we fall through to add_metadata()
+ if ( is_int( $retval ) ) {
+ $retval = true;
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- $meta_value = maybe_serialize( $meta_value );
-
- $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $bp->groups->table_name_groupmeta . " WHERE group_id = %d AND meta_key = %s", $group_id, $meta_key ) );
-
- if ( !$cur )
- $wpdb->query( $wpdb->prepare( "INSERT INTO " . $bp->groups->table_name_groupmeta . " ( group_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $group_id, $meta_key, $meta_value ) );
- else if ( $cur->meta_value != $meta_value )
- $wpdb->query( $wpdb->prepare( "UPDATE " . $bp->groups->table_name_groupmeta . " SET meta_value = %s WHERE group_id = %d AND meta_key = %s", $meta_value, $group_id, $meta_key ) );
- else
- return false;
-
- // Update the cached object and recache
- wp_cache_set( 'bp_groups_groupmeta_' . $group_id . '_' . $meta_key, $meta_value, 'bp' );
-
- return true;
</del><ins>+ return $retval;
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /*** Group Cleanup Functions ****************************************************/
</span></span></pre></div>
<a id="trunkbpgroupsbpgroupsloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-groups/bp-groups-loader.php (7839 => 7840)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-groups/bp-groups-loader.php 2014-02-11 02:58:47 UTC (rev 7839)
+++ trunk/bp-groups/bp-groups-loader.php 2014-02-11 02:58:50 UTC (rev 7840)
</span><span class="lines">@@ -136,6 +136,11 @@
</span><span class="cx"> 'table_name_groupmeta' => $bp->table_prefix . 'bp_groups_groupmeta'
</span><span class="cx"> );
</span><span class="cx">
</span><ins>+ // Metadata tables for groups component
+ $meta_tables = array(
+ 'group' => $bp->table_prefix . 'bp_groups_groupmeta',
+ );
+
</ins><span class="cx"> // All globals for groups component.
</span><span class="cx"> // Note that global_tables is included in this array.
</span><span class="cx"> $args = array(
</span><span class="lines">@@ -144,7 +149,8 @@
</span><span class="cx"> 'has_directory' => true,
</span><span class="cx"> 'notification_callback' => 'groups_format_notifications',
</span><span class="cx"> 'search_string' => __( 'Search Groups...', 'buddypress' ),
</span><del>- 'global_tables' => $global_tables
</del><ins>+ 'global_tables' => $global_tables,
+ 'meta_tables' => $meta_tables,
</ins><span class="cx"> );
</span><span class="cx">
</span><span class="cx"> parent::setup_globals( $args );
</span></span></pre>
</div>
</div>
</body>
</html>