<!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][12317] trunk: XProfile: Add caching to group and field queries.</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 { white-space: pre-line; 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/12317">12317</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/12317","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2018-12-07 18:41:16 +0000 (Fri, 07 Dec 2018)</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'>XProfile: Add caching to group and field queries.
See <a href="http://buddypress.trac.wordpress.org/ticket/7435">#7435</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpxprofilebpxprofilecachephp">trunk/src/bp-xprofile/bp-xprofile-cache.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilefieldphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php</a></li>
<li><a href="#trunksrcbpxprofileclassesclassbpxprofilegroupphp">trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php</a></li>
<li><a href="#trunktestsphpunittestcasesxprofileclassbpxprofilegroupphp">trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpxprofilebpxprofilecachephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/bp-xprofile-cache.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/bp-xprofile-cache.php 2018-12-07 18:16:57 UTC (rev 12316)
+++ trunk/src/bp-xprofile/bp-xprofile-cache.php 2018-12-07 18:41:16 UTC (rev 12317)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -287,5 +287,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'xprofile_field_before_save', 'bp_xprofile_reset_fields_by_name_cache_incrementor' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * Resets all incremented bp_xprofile_groups caches.
+ *
+ * @since 5.0.0
+ */
+function bp_xprofile_reset_groups_cache_incrementor() {
+ bp_core_reset_incrementor( 'bp_xprofile_groups' );
+}
+add_action( 'xprofile_group_after_delete', 'bp_xprofile_reset_groups_cache_incrementor' );
+add_action( 'xprofile_group_after_save', 'bp_xprofile_reset_groups_cache_incrementor' );
+add_action( 'xprofile_field_after_delete', 'bp_xprofile_reset_groups_cache_incrementor' );
+add_action( 'xprofile_field_after_save', 'bp_xprofile_reset_groups_cache_incrementor' );
+
</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( 'xprofile_updated_profile', 'bp_core_clear_cache' );
</span></span></pre></div>
<a id="trunksrcbpxprofileclassesclassbpxprofilefieldphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php 2018-12-07 18:16:57 UTC (rev 12316)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-field.php 2018-12-07 18:41:16 UTC (rev 12317)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -977,6 +977,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $sql = $wpdb->prepare( "UPDATE {$table_name} SET field_order = %d, group_id = %d WHERE id = %d", $position, $field_group_id, $field_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $parent = $wpdb->query( $sql );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval = false;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Update $field_id with new $position and $field_group_id.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $parent ) && ! is_wp_error( $parent ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -984,13 +986,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $sql = $wpdb->prepare( "UPDATE {$table_name} SET group_id = %d WHERE parent_id = %d", $field_group_id, $field_id );
</span><span class="cx" style="display: block; padding: 0 10px"> $wpdb->query( $sql );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Invalidate profile field cache.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Invalidate profile field and group query cache.
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( $field_id, 'bp_xprofile_fields' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $parent;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval = $parent;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ bp_core_reset_incrementor( 'bp_xprofile_groups' );
+
+ return $retval;
</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>
<a id="trunksrcbpxprofileclassesclassbpxprofilegroupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php 2018-12-07 18:16:57 UTC (rev 12316)
+++ trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php 2018-12-07 18:41:16 UTC (rev 12317)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -288,24 +288,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'data' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // WHERE.
- if ( ! empty( $r['profile_group_id'] ) ) {
- $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
- } elseif ( $r['exclude_groups'] ) {
- $exclude = join( ',', wp_parse_id_list( $r['exclude_groups'] ) );
- $where_sql = "WHERE g.id NOT IN ({$exclude})";
- } else {
- $where_sql = '';
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> $bp = buddypress();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Include or exclude empty groups.
- if ( ! empty( $r['hide_empty_groups'] ) ) {
- $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC" );
- } else {
- $group_ids = $wpdb->get_col( "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC" );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $group_ids = self::get_group_ids( $r );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Get all group data.
</span><span class="cx" style="display: block; padding: 0 10px"> $groups = self::get_group_data( $group_ids );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -326,53 +311,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $groups;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Setup IN query from group IDs.
- $group_ids_in = implode( ',', (array) $group_ids );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $field_ids = self::get_group_field_ids( $group_ids, $r );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Support arrays and comma-separated strings.
- $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] );
-
- // Visibility - Handled here so as not to be overridden by sloppy use of the
- // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user().
- $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] );
- $exclude_fields_cs = array_merge( $exclude_fields_cs, $hidden_user_fields );
- $exclude_fields_cs = implode( ',', $exclude_fields_cs );
-
- // Set up NOT IN query for excluded field IDs.
- if ( ! empty( $exclude_fields_cs ) ) {
- $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})";
- } else {
- $exclude_fields_sql = '';
- }
-
- // Set up IN query for included field IDs.
- $include_field_ids = array();
-
- // Member-type restrictions.
- if ( bp_get_member_types() ) {
- if ( $r['user_id'] || false !== $r['member_type'] ) {
- $member_types = $r['member_type'];
- if ( $r['user_id'] ) {
- $member_types = bp_get_member_type( $r['user_id'], false );
- if ( empty( $member_types ) ) {
- $member_types = array( 'null' );
- }
- }
-
- $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types );
- $include_field_ids += array_keys( $member_types_fields );
- }
- }
-
- $in_sql = '';
- if ( ! empty( $include_field_ids ) ) {
- $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) );
- $in_sql = " AND id IN ({$include_field_ids_cs}) ";
- }
-
- // Fetch the fields.
- $field_ids = $wpdb->get_col( "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order" );
-
</del><span class="cx" style="display: block; padding: 0 10px"> foreach( $groups as $group ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $group->fields = array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -499,6 +439,152 @@
</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">+ * Gets group IDs, based on passed parameters.
+ *
+ * @since 5.0.0
+ *
+ * @param array $args {
+ * Array of optional arguments:
+ * @type int $profile_group_id Limit results to a single profile group. Default false.
+ * @type array $exclude_groups Comma-separated list or array of group IDs to exclude. Default false.
+ * @type bool $hide_empty_groups True to hide groups that don't have any fields. Default: false.
+ * }
+ * @return array
+ */
+ public static function get_group_ids( $args = array() ) {
+ global $wpdb;
+
+ $r = array_merge(
+ array(
+ 'profile_group_id' => false,
+ 'exclude_groups' => false,
+ 'hide_empty_groups' => false,
+ ),
+ $args
+ );
+
+ $bp = buddypress();
+
+ if ( ! empty( $r['profile_group_id'] ) ) {
+ $where_sql = $wpdb->prepare( 'WHERE g.id = %d', $r['profile_group_id'] );
+ } elseif ( $r['exclude_groups'] ) {
+ $exclude = join( ',', wp_parse_id_list( $r['exclude_groups'] ) );
+ $where_sql = "WHERE g.id NOT IN ({$exclude})";
+ } else {
+ $where_sql = '';
+ }
+
+ // Include or exclude empty groups.
+ if ( ! empty( $r['hide_empty_groups'] ) ) {
+ $group_ids_sql = "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g INNER JOIN {$bp->profile->table_name_fields} f ON g.id = f.group_id {$where_sql} ORDER BY g.group_order ASC";
+ } else {
+ $group_ids_sql = "SELECT DISTINCT g.id FROM {$bp->profile->table_name_groups} g {$where_sql} ORDER BY g.group_order ASC";
+ }
+
+ $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' );
+ if ( false === $cached ) {
+ $group_ids = $wpdb->get_col( $group_ids_sql );
+ bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids );
+ } else {
+ $group_ids = $cached;
+ }
+
+ return array_map( 'intval', $group_ids );
+ }
+
+ /**
+ * Gets group field IDs, based on passed parameters.
+ *
+ * @since 5.0.0
+ *
+ * @param array $group_ids Array of group IDs.
+ * @param array $args {
+ * Array of optional arguments:
+ * @type array $exclude_fields Comma-separated list or array of field IDs to exclude.
+ * Default empty.
+ * @type int $user_id Limit results to fields associated with a given user's
+ * member type. Default empty.
+ * @type array|string $member_type Limit fields by those restricted to a given member type, or array of
+ * member types. If `$user_id` is provided, the value of `$member_type`
+ * is honored.
+ * }
+ * @return array
+ */
+ public static function get_group_field_ids( $group_ids, $args = array() ) {
+ global $wpdb;
+
+ $r = array_merge(
+ array(
+ 'exclude_fields' => false,
+ 'user_id' => false,
+ 'member_type' => false,
+ ),
+ $args
+ );
+
+ $bp = buddypress();
+
+ // Setup IN query from group IDs.
+ if ( empty( $group_ids ) ) {
+ $group_ids = array( 0 );
+ }
+ $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) );
+
+ // Support arrays and comma-separated strings.
+ $exclude_fields_cs = wp_parse_id_list( $r['exclude_fields'] );
+
+ // Visibility - Handled here so as not to be overridden by sloppy use of the
+ // exclude_fields parameter. See bp_xprofile_get_hidden_fields_for_user().
+ $hidden_user_fields = bp_xprofile_get_hidden_fields_for_user( $r['user_id'] );
+ $exclude_fields_cs = array_merge( $exclude_fields_cs, $hidden_user_fields );
+ $exclude_fields_cs = implode( ',', $exclude_fields_cs );
+
+ // Set up NOT IN query for excluded field IDs.
+ if ( ! empty( $exclude_fields_cs ) ) {
+ $exclude_fields_sql = "AND id NOT IN ({$exclude_fields_cs})";
+ } else {
+ $exclude_fields_sql = '';
+ }
+
+ // Set up IN query for included field IDs.
+ $include_field_ids = array();
+
+ // Member-type restrictions.
+ if ( bp_get_member_types() ) {
+ if ( $r['user_id'] || false !== $r['member_type'] ) {
+ $member_types = $r['member_type'];
+ if ( $r['user_id'] ) {
+ $member_types = bp_get_member_type( $r['user_id'], false );
+ if ( empty( $member_types ) ) {
+ $member_types = array( 'null' );
+ }
+ }
+
+ $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types );
+ $include_field_ids += array_keys( $member_types_fields );
+ }
+ }
+
+ $in_sql = '';
+ if ( ! empty( $include_field_ids ) ) {
+ $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) );
+ $in_sql = " AND id IN ({$include_field_ids_cs}) ";
+ }
+
+ $field_ids_sql = "SELECT id FROM {$bp->profile->table_name_fields} WHERE group_id IN ( {$group_ids_in} ) AND parent_id = 0 {$exclude_fields_sql} {$in_sql} ORDER BY field_order";
+
+ $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' );
+ if ( false === $cached ) {
+ $field_ids = $wpdb->get_col( $field_ids_sql );
+ bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids );
+ } else {
+ $field_ids = $cached;
+ }
+
+ return array_map( 'intval', $field_ids );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Get data about a set of groups, based on IDs.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.0.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -613,8 +699,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Purge profile field group cache.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Purge profile field group and group query caches.
</ins><span class="cx" style="display: block; padding: 0 10px"> wp_cache_delete( 'all', 'bp_xprofile_groups' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ bp_core_reset_incrementor( 'bp_xprofile_groups' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $bp = buddypress();
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestcasesxprofileclassbpxprofilegroupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php 2018-12-07 18:16:57 UTC (rev 12316)
+++ trunk/tests/phpunit/testcases/xprofile/class-bp-xprofile-group.php 2018-12-07 18:41:16 UTC (rev 12317)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -441,4 +441,217 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( "Test ' Name", $e1->name );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_ids_query_should_be_cached() {
+ global $wpdb;
+
+ $group_ids = array( 1 ); // Default group.
+ $group_ids[] = self::factory()->xprofile_group->create();
+ $group_ids[] = self::factory()->xprofile_group->create();
+ $group_ids[] = self::factory()->xprofile_group->create();
+
+ $params_1 = array(
+ 'exclude_groups' => false,
+ );
+
+ $params_2 = array(
+ 'exclude_groups' => array( 0 ),
+ );
+
+ // Prime cache.
+ $found_1 = BP_XProfile_Group::get_group_ids( $params_1 );
+ $this->assertEqualSets( $group_ids, $found_1 );
+
+ $num_queries = $wpdb->num_queries;
+
+ $found_1 = BP_XProfile_Group::get_group_ids( $params_1 );
+ $this->assertEqualSets( $group_ids, $found_1 );
+ $this->assertSame( $num_queries, $wpdb->num_queries );
+
+ // Different parameters should trigger a cache miss.
+ $found_2 = BP_XProfile_Group::get_group_ids( $params_2 );
+ $this->assertEqualSets( $group_ids, $found_2 );
+ $this->assertNotSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_ids_query_cache_should_be_busted_on_group_delete() {
+ $group_ids = array( 1 ); // Default group.
+ $group_ids[1] = self::factory()->xprofile_group->create();
+ $group_ids[2] = self::factory()->xprofile_group->create();
+ $group_ids[3] = self::factory()->xprofile_group->create();
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_ids();
+ $this->assertContains( $group_ids[1], $found );
+
+ $g1 = new BP_XProfile_Group( $group_ids[1] );
+ $g1->delete();
+
+ $found = BP_XProfile_Group::get_group_ids();
+ $this->assertNotContains( $group_ids[1], $found );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_ids_query_cache_should_be_busted_on_group_save() {
+ global $wpdb;
+
+ $group_ids = array( 1 ); // Default group.
+ $group_ids[1] = self::factory()->xprofile_group->create();
+ $group_ids[2] = self::factory()->xprofile_group->create();
+ $group_ids[3] = self::factory()->xprofile_group->create();
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_ids();
+ $this->assertContains( $group_ids[1], $found );
+
+ $g1 = new BP_XProfile_Group( $group_ids[1] );
+ $g1->save();
+
+ $num_queries = $wpdb->num_queries;
+
+ $found = BP_XProfile_Group::get_group_ids();
+ $this->assertNotSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_ids_query_cache_should_be_busted_on_field_save() {
+ global $wpdb;
+
+ $group_ids = array( 1 ); // Default group.
+ $group_ids[1] = self::factory()->xprofile_group->create();
+ $group_ids[2] = self::factory()->xprofile_group->create();
+ $group_ids[3] = self::factory()->xprofile_group->create();
+
+ $f = self::factory()->xprofile_field->create( array(
+ 'field_group_id' => $group_ids[1],
+ ) );
+ $field = new BP_XProfile_Field( $f );
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_ids();
+ $this->assertContains( $group_ids[1], $found );
+
+ $field->save();
+
+ $num_queries = $wpdb->num_queries;
+
+ $found = BP_XProfile_Group::get_group_ids();
+ $this->assertNotSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_ids_query_cache_should_be_busted_on_field_delete() {
+ $group_ids = array( 1 ); // Default group.
+ $group_ids[1] = self::factory()->xprofile_group->create();
+ $group_ids[2] = self::factory()->xprofile_group->create();
+ $group_ids[3] = self::factory()->xprofile_group->create();
+
+ $f = self::factory()->xprofile_field->create( array(
+ 'field_group_id' => $group_ids[1],
+ ) );
+ $field = new BP_XProfile_Field( $f );
+
+ $args = array(
+ 'hide_empty_groups' => true,
+ );
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_ids( $args );
+ $this->assertContains( $group_ids[1], $found );
+
+ $field->delete();
+
+ $found = BP_XProfile_Group::get_group_ids( $args );
+ $this->assertNotContains( $group_ids[1], $found );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_field_ids_query_cache() {
+ global $wpdb;
+
+ $group_id = self::factory()->xprofile_group->create();
+
+ $f = self::factory()->xprofile_field->create( array(
+ 'field_group_id' => $group_id,
+ ) );
+ $field = new BP_XProfile_Field( $f );
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
+ $this->assertContains( $f, $found );
+
+ $num_queries = $wpdb->num_queries;
+
+ $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
+ $this->assertContains( $f, $found );
+ $this->assertSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_field_ids_query_cache_should_be_busted_on_field_save() {
+ global $wpdb;
+
+ $group_id = self::factory()->xprofile_group->create();
+
+ $f = self::factory()->xprofile_field->create( array(
+ 'field_group_id' => $group_id,
+ ) );
+ $field = new BP_XProfile_Field( $f );
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
+ $this->assertContains( $f, $found );
+
+ $field->save();
+ $num_queries = $wpdb->num_queries;
+
+ $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
+ $this->assertContains( $f, $found );
+ $this->assertNotSame( $num_queries, $wpdb->num_queries );
+ }
+
+ /**
+ * @group BP7435
+ * @group cache
+ */
+ public function test_group_field_ids_query_cache_should_be_busted_on_field_delete() {
+ $group_id = self::factory()->xprofile_group->create();
+
+ $f = self::factory()->xprofile_field->create( array(
+ 'field_group_id' => $group_id,
+ ) );
+ $field = new BP_XProfile_Field( $f );
+
+ // Prime cache.
+ $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
+ $this->assertContains( $f, $found );
+
+ $field->delete();
+
+ $found = BP_XProfile_Group::get_group_field_ids( array( $group_id ) );
+ $this->assertNotContains( $f, $found );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>