Skip to:
Content

BuddyPress.org

Ticket #8540: 8540.2.patch

File 8540.2.patch, 27.6 KB (added by dcavins, 2 years ago)

Improvements on original patch (including imath's suggestions, corrections to tests, and improved documentation in the BP_Signup class)

  • src/bp-members/bp-members-cache.php

    diff --git src/bp-members/bp-members-cache.php src/bp-members/bp-members-cache.php
    index 54185a454..296582726 100644
    function bp_members_reset_activity_cache_incrementor() { 
    7373        return bp_core_reset_incrementor( 'bp_activity_with_last_activity' );
    7474}
    7575add_action( 'bp_core_user_updated_last_activity', 'bp_members_reset_activity_cache_incrementor' );
     76
     77/**
     78 * Bust signup caches when editing or deleting.
     79 *
     80 * @since 10.0.0
     81 *
     82 * @param int $signup_id The ID of the signup affected.
     83 */
     84function bp_members_delete_signup_cache( $signup_id = 0 ) {
     85        wp_cache_delete( $signup_id, 'bp_signups' );
     86}
     87add_action( 'bp_core_signups_after_add',         'bp_members_delete_signup_cache' );
     88add_action( 'bp_core_signups_after_update_meta', 'bp_members_delete_signup_cache' );
     89
     90/**
     91 * Bust signup caches for arrays of signup IDs.
     92 *
     93 * @since 10.0.0
     94 *
     95 * @param array $signup_ids The IDs of the signups affected.
     96 */
     97function bp_members_delete_signup_cache_multiples( $signup_ids = array() ) {
     98        // Ensure that the incoming item is an array.
     99        $signup_ids = wp_parse_id_list( $signup_ids );
     100        foreach ( $signup_ids as $signup_id ) {
     101                bp_members_delete_signup_cache( $signup_id );
     102        }
     103}
     104add_action( 'bp_core_signup_after_resend',   'bp_members_delete_signup_cache_multiples' );
     105add_action( 'bp_core_signup_after_activate', 'bp_members_delete_signup_cache_multiples' );
     106add_action( 'bp_core_signup_after_delete',   'bp_members_delete_signup_cache_multiples' );
     107
     108/**
     109 * Reset cache incrementor for BP_Signups.
     110 *
     111 * This function invalidates all cached results of BP_Signup queries,
     112 * whenever one of the following events takes place:
     113 *   - A record is created or updated.
     114 *   - A record is deleted.
     115 *
     116 * @since 10.0.0
     117 *
     118 * @return bool True on success, false on failure.
     119 */
     120function bp_members_reset_signup_cache_incrementor() {
     121        return bp_core_reset_incrementor( 'bp_signups' );
     122}
     123add_filter( 'bp_core_signups_after_add',         'bp_members_reset_signup_cache_incrementor' );
     124add_action( 'bp_core_activated_user',            'bp_members_reset_signup_cache_incrementor' );
     125add_action( 'bp_core_signup_after_activate',     'bp_members_reset_signup_cache_incrementor' );
     126add_action( 'bp_core_signups_after_update_meta', 'bp_members_reset_signup_cache_incrementor' );
     127add_action( 'bp_core_signup_after_delete',       'bp_members_reset_signup_cache_incrementor' );
     128
  • src/bp-members/classes/class-bp-signup.php

    diff --git src/bp-members/classes/class-bp-signup.php src/bp-members/classes/class-bp-signup.php
    index f1eece597..db1d5a0f1 100644
    class BP_Signup { 
    7878         */
    7979        public $activation_key;
    8080
     81        /**
     82         * The activated date for the user.
     83         *
     84         * @since 10.0.0
     85         * @var string
     86         */
     87        public $activated;
     88
     89        /**
     90         * Whether the user account is activated or not.
     91         *
     92         * @since 10.0.0
     93         * @var bool
     94         */
     95        public $active;
     96
     97        /**
     98         * The date that the last activation email was sent.
     99         *
     100         * @since 10.0.0
     101         * @var string
     102         */
     103        public $date_sent;
     104
     105        /**
     106         * Was the last activation email sent in the last 24 hours?
     107         *
     108         * @since 10.0.0
     109         * @var bool
     110         */
     111        public $recently_sent;
     112
     113        /**
     114         * The number of activation emails sent to this user.
     115         *
     116         * @since 10.0.0
     117         * @var int
     118         */
     119        public $count_sent;
     120
     121        /**
     122         * The domain for the signup.
     123         *
     124         * @since 10.0.0
     125         * @var string
     126         */
     127        public $domain;
     128
     129        /**
     130         * The path for the signup.
     131         *
     132         * @since 10.0.0
     133         * @var string
     134         */
     135        public $path;
     136
     137        /**
     138         * The title for the signup.
     139         *
     140         * @since 10.0.0
     141         * @var string
     142         */
     143        public $title;
     144
    81145
    82146        /** Public Methods *******************************************************/
    83147
    class BP_Signup { 
    89153         * @param integer $signup_id The ID for the signup being queried.
    90154         */
    91155        public function __construct( $signup_id = 0 ) {
    92                 if ( !empty( $signup_id ) ) {
     156                if ( ! empty( $signup_id ) ) {
    93157                        $this->id = $signup_id;
    94158                        $this->populate();
    95159                }
    class BP_Signup { 
    103167        public function populate() {
    104168                global $wpdb;
    105169
    106                 $signups_table = buddypress()->members->table_name_signups;
    107                 $signup        = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$signups_table} WHERE signup_id = %d AND active = 0", $this->id ) );
     170                // Get BuddyPress.
     171                $bp = buddypress();
     172
     173                // Check cache for signup data.
     174                $signup = wp_cache_get( $this->id, 'bp_signups' );
     175
     176                // Cache missed, so query the DB.
     177                if ( false === $signup ) {
     178                        $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id = %d", $this->id ) );
     179
     180                        wp_cache_set( $this->id, $signup, 'bp_signups' );
     181                }
     182
     183                // No signup found so set the ID and bail.
     184                if ( empty( $signup ) || is_wp_error( $signup ) ) {
     185                        $this->id = 0;
     186                        return;
     187                }
    108188
    109                 $this->avatar         = get_avatar( $signup->user_email, 32 );
     189                /*
     190                 * Add every db column to the object.
     191                 */
     192                $this->signup_id      = $this->id;
     193                $this->domain         = $signup->domain;
     194                $this->path           = $signup->path;
     195                $this->title          = $signup->title;
    110196                $this->user_login     = $signup->user_login;
    111197                $this->user_email     = $signup->user_email;
    112                 $this->meta           = maybe_unserialize( $signup->meta );
    113                 $this->user_name      = ! empty( $this->meta['field_1'] ) ? wp_unslash( $this->meta['field_1'] ) : '';
    114198                $this->registered     = $signup->registered;
     199                $this->activated      = $signup->activated;
     200                $this->active         = (bool) $signup->active;
    115201                $this->activation_key = $signup->activation_key;
     202                $this->meta           = maybe_unserialize( $signup->meta );
     203
     204                // Add richness.
     205                $this->avatar    = get_avatar( $signup->user_email, 32 );
     206                $this->user_name = ! empty( $this->meta['field_1'] ) ? wp_unslash( $this->meta['field_1'] ) : '';
     207
     208                // When was the activation email sent?
     209                if ( isset( $this->meta['sent_date'] ) && '0000-00-00 00:00:00' !== $this->meta['sent_date'] ) {
     210                        $this->date_sent = $this->meta['sent_date'];
     211
     212                        // Sent date defaults to date of registration.
     213                } else {
     214                        $this->date_sent = $signup->registered;
     215                }
     216
     217                /**
     218                 * Calculate a diff between now & last time
     219                 * an activation link has been resent.
     220                 */
     221                $sent_at = mysql2date( 'U', $this->date_sent );
     222                $now     = current_time( 'timestamp', true );
     223                $diff    = $now - $sent_at;
     224
     225                /**
     226                 * Set a boolean to track whether an activation link
     227                 * was sent in the last day.
     228                 */
     229                $this->recently_sent = ( $diff < 1 * DAY_IN_SECONDS );
     230
     231                // How many times has the activation email been sent?
     232                if ( isset( $this->meta['count_sent'] ) ) {
     233                        $this->count_sent = absint( $this->meta['count_sent'] );
     234                } else {
     235                        $this->count_sent = 0;
     236                }
    116237        }
    117238
    118239        /** Static Methods *******************************************************/
    class BP_Signup { 
    126247         * @param array $args {
    127248         *     The argument to retrieve desired signups.
    128249         *     @type int         $offset         Offset amount. Default 0.
    129          *     @type int         $number         How many to fetch. Default 1.
     250         *     @type int         $number         How many to fetch. Pass -1 to fetch all. Default 1.
    130251         *     @type bool|string $usersearch     Whether or not to search for a username. Default false.
    131252         *     @type string      $orderby        Order By parameter. Possible values are `signup_id`, `login`, `email`,
    132253         *                                       `registered`, `activated`. Default `signup_id`.
    133254         *     @type string      $order          Order direction. Default 'DESC'.
    134255         *     @type bool        $include        Whether or not to include more specific query params.
    135          *     @type string      $activation_key Activation key to search for.
     256         *     @type string      $activation_key Activation key to search for. If specified, all other
     257         *                                       parameters will be ignored.
     258         *     @type int|bool    $active         Pass 0 for inactive signups, 1 for active signups,
     259         *                                       and `false` to ignore.
    136260         *     @type string      $user_login     Specific user login to return.
    137261         *     @type string      $fields         Which fields to return. Specify 'ids' to fetch a list of signups IDs.
    138262         *                                       Default: 'all' (return BP_Signup objects).
    class BP_Signup { 
    145269        public static function get( $args = array() ) {
    146270                global $wpdb;
    147271
    148                 $r = bp_parse_args( $args,
     272                $bp = buddypress();
     273                $r  = bp_parse_args(
     274                        $args,
    149275                        array(
    150276                                'offset'         => 0,
    151277                                'number'         => 1,
    class BP_Signup { 
    154280                                'order'          => 'DESC',
    155281                                'include'        => false,
    156282                                'activation_key' => '',
     283                                'active'         => 0,
     284                                'user_email'     => '',
    157285                                'user_login'     => '',
    158286                                'fields'         => 'all',
    159287                        ),
    class BP_Signup { 
    171299
    172300                $r['orderby'] = sanitize_title( $r['orderby'] );
    173301
    174                 $sql = array();
    175                 $signups_table  = buddypress()->members->table_name_signups;
    176                 $sql['select']  = "SELECT * FROM {$signups_table}";
    177                 $sql['where']   = array();
    178                 $sql['where'][] = "active = 0";
     302                $sql = array(
     303                        'select'     => "SELECT DISTINCT signup_id",
     304                        'from'       => "{$bp->members->table_name_signups}",
     305                        'where'      => array(),
     306                        'orderby'    => '',
     307                        'limit'      => '',
     308                );
     309
     310                // Activation key trumps other parameters because it should be unique.
     311                if ( ! empty( $r['activation_key'] ) ) {
     312                        $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] );
     313
     314                        // `Include` finds signups by ID.
     315                } else if ( ! empty( $r['include'] ) ) {
    179316
    180                 if ( empty( $r['include'] ) ) {
     317                        $in             = implode( ',', wp_parse_id_list( $r['include'] ) );
     318                        $sql['where'][] = "signup_id IN ({$in})";
     319
     320                        /**
     321                         * Finally, the general case where a variety of parameters
     322                         * can be used in combination to find signups.
     323                         */
     324                } else {
     325                        // Active.
     326                        if ( false !== $r['active'] ) {
     327                                $sql['where'][] = $wpdb->prepare( "active = %d", absint( $r['active'] ) );
     328                        }
    181329
    182330                        // Search terms.
    183331                        if ( ! empty( $r['usersearch'] ) ) {
    class BP_Signup { 
    185333                                $sql['where'][]    = $wpdb->prepare( "( user_login LIKE %s OR user_email LIKE %s OR meta LIKE %s )", $search_terms_like, $search_terms_like, $search_terms_like );
    186334                        }
    187335
    188                         // Activation key.
    189                         if ( ! empty( $r['activation_key'] ) ) {
    190                                 $sql['where'][] = $wpdb->prepare( "activation_key = %s", $r['activation_key'] );
     336                        // User email.
     337                        if ( ! empty( $r['user_email'] ) ) {
     338                                $sql['where'][] = $wpdb->prepare( "user_email = %s", $r['user_email'] );
    191339                        }
    192340
    193341                        // User login.
    class BP_Signup { 
    195343                                $sql['where'][] = $wpdb->prepare( "user_login = %s", $r['user_login'] );
    196344                        }
    197345
    198                         $sql['orderby'] = "ORDER BY {$r['orderby']}";
    199                         $sql['order']   = bp_esc_sql_order( $r['order'] );
    200                         $sql['limit']   = $wpdb->prepare( "LIMIT %d, %d", $r['offset'], $r['number'] );
    201                 } else {
    202                         $in = implode( ',', wp_parse_id_list( $r['include'] ) );
    203                         $sql['in'] = "AND signup_id IN ({$in})";
     346                        $order          = bp_esc_sql_order( $r['order'] );
     347                        $sql['orderby'] = "ORDER BY {$r['orderby']} {$order}";
     348
     349                        $number = intval( $r['number'] );
     350                        if ( -1 !== $number ) {
     351                                $sql['limit'] = $wpdb->prepare( "LIMIT %d, %d", absint( $r['offset'] ), $number );
     352                        }
    204353                }
    205354
    206355                // Implode WHERE clauses.
    207356                $sql['where'] = 'WHERE ' . implode( ' AND ', $sql['where'] );
    208357
     358                $paged_signups_sql = "{$sql['select']} FROM {$sql['from']} {$sql['where']} {$sql['orderby']} {$sql['limit']}";
     359
    209360                /**
    210361                 * Filters the Signups paged query.
    211362                 *
    class BP_Signup { 
    216367                 * @param array  $args  Array of original arguments for get() method.
    217368                 * @param array  $r     Array of parsed arguments for get() method.
    218369                 */
    219                 $paged_signups = $wpdb->get_results( apply_filters( 'bp_members_signups_paged_query', join( ' ', $sql ), $sql, $args, $r ) );
     370                $paged_signups_sql = apply_filters( 'bp_members_signups_paged_query', $paged_signups_sql, $sql, $args, $r );
    220371
    221                 if ( empty( $paged_signups ) ) {
    222                         return array( 'signups' => false, 'total' => false );
     372                $cached = bp_core_get_incremented_cache( $paged_signups_sql, 'bp_signups' );
     373                if ( false === $cached ) {
     374                        $paged_signup_ids = $wpdb->get_col( $paged_signups_sql );
     375                        bp_core_set_incremented_cache( $paged_signups_sql, 'bp_signups', $paged_signup_ids );
     376                } else {
     377                        $paged_signup_ids = $cached;
    223378                }
    224379
    225380                // We only want the IDs.
    226381                if ( 'ids' === $r['fields'] ) {
    227                         $paged_signups = wp_list_pluck( $paged_signups, 'signup_id' );
    228                 } else {
     382                        $paged_signups = array_map( 'intval', $paged_signup_ids );
    229383
    230                         // Used to calculate a diff between now & last
    231                         // time an activation link has been resent.
    232                         $now = current_time( 'timestamp', true );
    233 
    234                         foreach ( (array) $paged_signups as $key => $signup ) {
    235 
    236                                 $signup->id   = intval( $signup->signup_id );
    237 
    238                                 $signup->meta = ! empty( $signup->meta ) ? maybe_unserialize( $signup->meta ) : false;
    239 
    240                                 $signup->user_name = '';
    241                                 if ( ! empty( $signup->meta['field_1'] ) ) {
    242                                         $signup->user_name = wp_unslash( $signup->meta['field_1'] );
    243                                 }
    244 
    245                                 // Sent date defaults to date of registration.
    246                                 if ( ! empty( $signup->meta['sent_date'] ) ) {
    247                                         $signup->date_sent = $signup->meta['sent_date'];
    248                                 } else {
    249                                         $signup->date_sent = $signup->registered;
    250                                 }
    251 
    252                                 $sent_at = mysql2date('U', $signup->date_sent );
    253                                 $diff    = $now - $sent_at;
    254 
    255                                 /**
    256                                  * Add a boolean in case the last time an activation link
    257                                  * has been sent happened less than a day ago.
    258                                  */
    259                                 if ( $diff < 1 * DAY_IN_SECONDS ) {
    260                                         $signup->recently_sent = true;
    261                                 }
    262 
    263                                 if ( ! empty( $signup->meta['count_sent'] ) ) {
    264                                         $signup->count_sent = absint( $signup->meta['count_sent'] );
    265                                 } else {
    266                                         $signup->count_sent = 1;
     384                } else {
     385                        $uncached_signup_ids = bp_get_non_cached_ids( $paged_signup_ids, 'bp_signups' );
     386                        if ( $uncached_signup_ids ) {
     387                                $signup_ids_sql      = implode( ',', array_map( 'intval', $uncached_signup_ids ) );
     388                                $signup_data_objects = $wpdb->get_results( "SELECT * FROM {$bp->members->table_name_signups} WHERE signup_id IN ({$signup_ids_sql})" );
     389                                foreach ( $signup_data_objects as $signup_data_object ) {
     390                                        wp_cache_set( $signup_data_object->signup_id, $signup_data_object, 'bp_signups' );
    267391                                }
     392                        }
    268393
    269                                 $paged_signups[ $key ] = $signup;
     394                        $paged_signups = array();
     395                        foreach ( $paged_signup_ids as $paged_signup_id ) {
     396                                $paged_signups[] = new BP_Signup( $paged_signup_id );
    270397                        }
    271398                }
    272399
    273                 unset( $sql['limit'] );
    274                 $sql['select'] = preg_replace( "/SELECT.*?FROM/", "SELECT COUNT(*) FROM", $sql['select'] );
     400                // Find the total number of signups in the results set.
     401                $total_signups_sql = "SELECT COUNT(DISTINCT signup_id) FROM {$sql['from']} {$sql['where']}";
    275402
    276403                /**
    277404                 * Filters the Signups count query.
    class BP_Signup { 
    283410                 * @param array  $args  Array of original arguments for get() method.
    284411                 * @param array  $r     Array of parsed arguments for get() method.
    285412                 */
    286                 $total_signups = $wpdb->get_var( apply_filters( 'bp_members_signups_count_query', join( ' ', $sql ), $sql, $args, $r ) );
     413                $total_signups_sql = apply_filters( 'bp_members_signups_count_query', $total_signups_sql, $sql, $args, $r );
     414
     415                $cached = bp_core_get_incremented_cache( $total_signups_sql, 'bp_signups' );
     416                if ( false === $cached ) {
     417                        $total_signups = (int) $wpdb->get_var( $total_signups_sql );
     418                        bp_core_set_incremented_cache( $total_signups_sql, 'bp_signups', $total_signups );
     419                } else {
     420                        $total_signups = (int) $cached;
     421                }
    287422
    288423                return array( 'signups' => $paged_signups, 'total' => $total_signups );
    289424        }
    class BP_Signup { 
    309444        public static function add( $args = array() ) {
    310445                global $wpdb;
    311446
    312                 $r = bp_parse_args( $args,
     447                $r = bp_parse_args(
     448                        $args,
    313449                        array(
    314450                                'domain'         => '',
    315451                                'path'           => '',
    class BP_Signup { 
    318454                                'user_email'     => '',
    319455                                'registered'     => current_time( 'mysql', true ),
    320456                                'activation_key' => '',
    321                                 'meta'           => '',
     457                                'meta'           => array(),
    322458                        ),
    323459                        'bp_core_signups_add_args'
    324460                );
    325461
     462                // Ensure that sent_date and count_sent are set in meta.
     463                if ( ! isset( $r['meta']['sent_date'] ) ) {
     464                        $r['meta']['sent_date'] = '0000-00-00 00:00:00';
     465                }
     466                if ( ! isset( $r['meta']['count_sent'] ) ) {
     467                        $r['meta']['count_sent'] = 0;
     468                }
     469
    326470                $r['meta'] = maybe_serialize( $r['meta'] );
    327471
    328472                $inserted = $wpdb->insert(
    class BP_Signup { 
    337481                        $retval = false;
    338482                }
    339483
     484                /**
     485                 * Fires after adding a new BP_Signup.
     486                 *
     487                 * @since 10.0.0
     488                 *
     489                 * @param int|bool $retval ID of the BP_Signup just added.
     490                 * @param array    $r      Array of parsed arguments for add() method.
     491                 * @param array    $args   Array of original arguments for add() method.
     492                 */
     493                do_action( 'bp_core_signups_after_add', $retval, $r, $args );
     494
    340495                /**
    341496                 * Filters the result of a signup addition.
    342497                 *
    343498                 * @since 2.0.0
    344499                 *
    345                  * @param int|bool $retval Newly added user ID on success, false on failure.
     500                 * @param int|bool $retval Newly added signup ID on success, false on failure.
    346501                 */
    347502                return apply_filters( 'bp_core_signups_add', $retval );
    348503        }
    class BP_Signup { 
    368523        public static function add_backcompat( $user_login = '', $user_password = '', $user_email = '', $usermeta = array() ) {
    369524                global $wpdb;
    370525
    371                 $user_id = wp_insert_user( array(
    372                         'user_login'   => $user_login,
    373                         'user_pass'    => $user_password,
    374                         'display_name' => sanitize_title( $user_login ),
    375                         'user_email'   => $user_email
    376                 ) );
     526                $user_id = wp_insert_user(
     527                        array(
     528                                'user_login'   => $user_login,
     529                                'user_pass'    => $user_password,
     530                                'display_name' => sanitize_title( $user_login ),
     531                                'user_email'   => $user_email
     532                        )
     533                );
    377534
    378535                if ( is_wp_error( $user_id ) || empty( $user_id ) ) {
    379536                        return $user_id;
    class BP_Signup { 
    415572                                                $vfield           = xprofile_get_field( $field_id, null, false );
    416573                                                $visibility_level = isset( $vfield->default_visibility ) ? $vfield->default_visibility : 'public';
    417574                                        }
     575
    418576                                        xprofile_set_field_visibility_level( $field_id, $user_id, $visibility_level );
    419577                                }
    420578                        }
    421579                }
    422580
     581                /**
     582                 * Fires after adding a new WP User (backcompat).
     583                 *
     584                 * @since 10.0.0
     585                 *
     586                 * @param int $user_id ID of the WP_User just added.
     587                 */
     588                do_action( 'bp_core_signups_after_add_backcompat', $user_id );
     589
    423590                /**
    424591                 * Filters the user ID for the backcompat functionality.
    425592                 *
    class BP_Signup { 
    445612                        return false;
    446613                }
    447614
    448                 $user_status = $wpdb->get_var( $wpdb->prepare( "SELECT user_status FROM {$wpdb->users} WHERE ID = %d", $user_id ) );
     615                $user        = get_user_by( 'id', $user_id );
     616                $user_status = $user->user_status;
    449617
    450618                /**
    451619                 * Filters the user status of a provided user ID.
    class BP_Signup { 
    511679         * @return int The number of signups.
    512680         */
    513681        public static function count_signups() {
    514                 global $wpdb;
    515 
    516                 $signups_table = buddypress()->members->table_name_signups;
    517                 $count_signups = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) AS total FROM {$signups_table} WHERE active = %d", 0 ) );
     682                $all_signups   = self::get(
     683                        array(
     684                                'fields' => 'ids',
     685                        )
     686                );
     687                $count_signups = $all_signups['total'];
    518688
    519689                /**
    520690                 * Filters the total inactive signups.
    class BP_Signup { 
    544714        public static function update( $args = array() ) {
    545715                global $wpdb;
    546716
    547                 $r = bp_parse_args( $args,
     717                $r = bp_parse_args(
     718                        $args,
    548719                        array(
    549720                                'signup_id'  => 0,
    550721                                'meta'       => array(),
    class BP_Signup { 
    556727                        return false;
    557728                }
    558729
     730                $signup_id = absint( $r['signup_id'] );
     731
     732                // Figure out which meta keys should be updated.
     733                $signup       = new BP_Signup( $signup_id );
     734                $blended_meta = wp_parse_args( $r['meta'], $signup->meta );
     735
    559736                $wpdb->update(
    560737                        // Signups table.
    561738                        buddypress()->members->table_name_signups,
    562739                        // Data to update.
    563740                        array(
    564                                 'meta' => serialize( $r['meta'] ),
     741                                'meta' => serialize( $blended_meta ),
    565742                        ),
    566743                        // WHERE.
    567744                        array(
    568                                 'signup_id' => $r['signup_id'],
     745                                'signup_id' => $signup_id,
    569746                        ),
    570747                        // Data sanitization format.
    571748                        array(
    class BP_Signup { 
    577754                        )
    578755                );
    579756
     757                /**
     758                 * Fires after updating the meta of a new BP_Signup.
     759                 *
     760                 * @since 10.0.0
     761                 *
     762                 * @param int|bool $signup_id    ID of the BP_Signup updated.
     763                 * @param array    $r            Array of parsed arguments to update() method.
     764                 * @param array    $args         Array of original arguments to update() method.
     765                 * @param array    $blended_meta The complete set of meta to save.
     766                 */
     767                do_action( 'bp_core_signups_after_update_meta', $signup_id, $r, $args, $blended_meta );
     768
    580769                /**
    581770                 * Filters the signup ID which received a meta update.
    582771                 *
    class BP_Signup { 
    600789                        return false;
    601790                }
    602791
    603                 $to_resend = self::get( array(
    604                         'include' => $signup_ids,
    605                 ) );
     792                $to_resend = self::get(
     793                        array(
     794                                'include' => $signup_ids,
     795                        )
     796                );
    606797
    607798                if ( ! $signups = $to_resend['signups'] ) {
    608799                        return false;
    class BP_Signup { 
    621812
    622813                foreach ( $signups as $signup ) {
    623814
    624                         $meta               = $signup->meta;
    625                         $meta['sent_date']  = current_time( 'mysql', true );
    626                         $meta['count_sent'] = $signup->count_sent + 1;
     815                        $meta = array(
     816                                'sent_date'  => current_time( 'mysql', true ),
     817                                'count_sent' => $signup->count_sent + 1
     818                        );
    627819
    628820                        // Send activation email.
    629821                        if ( is_multisite() ) {
    class BP_Signup { 
    655847                        }
    656848
    657849                        // Update metas.
    658                         $result['resent'][] = self::update( array(
    659                                 'signup_id' => $signup->signup_id,
    660                                 'meta'      => $meta,
    661                         ) );
     850                        $result['resent'][] = self::update(
     851                                array(
     852                                        'signup_id' => $signup->signup_id,
     853                                        'meta'      => $meta,
     854                                )
     855                        );
    662856                }
    663857
    664858                /**
    class BP_Signup { 
    694888                        return false;
    695889                }
    696890
    697                 $to_activate = self::get( array(
    698                         'include' => $signup_ids,
    699                 ) );
     891                $to_activate = self::get(
     892                        array(
     893                                'include' => $signup_ids,
     894                        )
     895                );
    700896
    701897                if ( ! $signups = $to_activate['signups'] ) {
    702898                        return false;
    class BP_Signup { 
    778974                        return false;
    779975                }
    780976
    781                 $to_delete = self::get( array(
    782                         'include' => $signup_ids,
    783                 ) );
     977                $to_delete = self::get(
     978                        array(
     979                                'include' => $signup_ids,
     980                        )
     981                );
    784982
    785983                if ( ! $signups = $to_delete['signups'] ) {
    786984                        return false;
  • tests/phpunit/testcases/members/class-bp-signup.php

    diff --git tests/phpunit/testcases/members/class-bp-signup.php tests/phpunit/testcases/members/class-bp-signup.php
    index 4a420d414..ad42db952 100644
    class BP_Tests_BP_Signup extends BP_UnitTestCase { 
    356356
    357357                $this->assertEquals( array( $s3, $s2, $s1 ), $ss['signups'] );
    358358        }
     359
     360        /**
     361         * @group cache
     362         */
     363        public function test_get_queries_should_be_cached() {
     364                global $wpdb;
     365
     366                $s = self::factory()->signup->create();
     367
     368                $found1 = BP_Signup::get(
     369                        array(
     370                                'fields' => 'ids',
     371                        )
     372                );
     373
     374                $num_queries = $wpdb->num_queries;
     375
     376                $found2 = BP_Signup::get(
     377                        array(
     378                                'fields' => 'ids',
     379                        )
     380                );
     381
     382                $this->assertEqualSets( $found1, $found2 );
     383                $this->assertSame( $num_queries, $wpdb->num_queries );
     384        }
     385
     386        /**
     387         * @group cache
     388         */
     389        public function test_get_query_caches_should_be_busted_by_add() {
     390                $s1 = self::factory()->signup->create();
     391
     392                $found1 = BP_Signup::get(
     393                        array(
     394                                'fields' => 'ids',
     395                        )
     396                );
     397                $this->assertEqualSets( array( $s1 ), $found1['signups'] );
     398
     399                $s2 = self::factory()->signup->create();
     400                $found2 = BP_Signup::get(
     401                        array(
     402                                'fields' => 'ids',
     403                        )
     404                );
     405                $this->assertEqualSets( array( $s2 ), $found2['signups'] );
     406        }
     407
     408        /**
     409         * @group cache
     410         */
     411        public function test_get_query_caches_should_be_busted_by_meta_update() {
     412                $time = bp_core_current_time();
     413
     414                $args = array(
     415                        'domain' => 'foo',
     416                        'path' => 'bar',
     417                        'title' => 'Foo bar',
     418                        'user_login' => 'user1',
     419                        'user_email' => 'user1@example.com',
     420                        'registered' => $time,
     421                        'activation_key' => '12345',
     422                        'meta' => array(
     423                                'field_1' => 'Fozzie',
     424                                'meta1' => 'meta2',
     425                        ),
     426                );
     427                $s1 = BP_Signup::add( $args );
     428
     429                $args['meta']['field_1'] = 'Fozz';
     430                $s2 = BP_Signup::add( $args );
     431
     432                // Should find both.
     433                $found1 = BP_Signup::get( array(
     434                        'fields' => 'ids',
     435                        'number'  => -1,
     436                        'usersearch' => 'Fozz',
     437                ) );
     438                $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
     439
     440                BP_Signup::update( array(
     441                        'signup_id'  => $s1,
     442                        'meta'       => array(
     443                                'field_1' => 'Fonzie'
     444                        ),
     445                ) );
     446
     447                $found2 = BP_Signup::get( array(
     448                        'fields' => 'ids',
     449                        'number'  => -1,
     450                        'usersearch' => 'Fozz',
     451                ) );
     452
     453                $this->assertEqualSets( array( $s2 ), $found2['signups'] );
     454        }
     455
     456        /**
     457         * @group cache
     458         */
     459        public function test_get_query_caches_should_be_busted_by_delete() {
     460                global $wpdb;
     461                $time = bp_core_current_time();
     462
     463                $args = array(
     464                        'domain' => 'foo',
     465                        'path' => 'bar',
     466                        'title' => 'Foo bar',
     467                        'user_login' => 'user1',
     468                        'user_email' => 'user1@example.com',
     469                        'registered' => $time,
     470                        'activation_key' => '12345',
     471                        'meta' => array(
     472                                'field_1' => 'Fozzie',
     473                                'meta1' => 'meta2',
     474                        ),
     475                );
     476                $s1 = BP_Signup::add( $args );
     477
     478                $args['meta']['field_1'] = 'Fozz';
     479                $s2 = BP_Signup::add( $args );
     480
     481                // Should find both.
     482                $found1 = BP_Signup::get( array(
     483                        'fields' => 'ids',
     484                        'number'  => -1,
     485                        'usersearch' => 'Fozz',
     486                ) );
     487                $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
     488
     489                BP_Signup::delete( array( $s1 ) );
     490
     491                $found2 = BP_Signup::get( array(
     492                        'fields' => 'ids',
     493                        'number'  => -1,
     494                        'usersearch' => 'Fozz',
     495                ) );
     496
     497                $this->assertEqualSets( array( $s2 ), $found2['signups'] );
     498        }
     499
     500        /**
     501         * @group cache
     502         */
     503        public function test_get_query_caches_should_be_busted_by_activation() {
     504                $s1 = self::factory()->signup->create( array(
     505                        'user_login'     => 'accountone',
     506                        'user_email'     => 'accountone@example.com',
     507                        'activation_key' => 'activationkeyone',
     508                ) );
     509
     510                $s2 = self::factory()->signup->create( array(
     511                        'user_login'     => 'accounttwo',
     512                        'user_email'     => 'accounttwo@example.com',
     513                        'activation_key' => 'activationkeytwo',
     514                ) );
     515                $found1 = BP_Signup::get(
     516                        array(
     517                                'number' => -1,
     518                                'fields' => 'ids',
     519                        )
     520                );
     521                $this->assertEqualSets( array( $s1, $s2 ), $found1['signups'] );
     522
     523                $activate = BP_Signup::activate( (array) $s2 );
     524
     525                $found2 = BP_Signup::get(
     526                        array(
     527                                'number' => -1,
     528                                'fields' => 'ids',
     529                        )
     530                );
     531                $this->assertEqualSets( array( $s1 ), $found2['signups'] );
     532        }
     533
     534        /**
     535         * @group cache
     536         */
     537        public function signup_objects_should_be_cached() {
     538                global $wpdb;
     539
     540                $s1 = self::factory()->signup->create( array(
     541                        'user_login'     => 'accountone',
     542                        'user_email'     => 'accountone@example.com',
     543                        'activation_key' => 'activationkeyone',
     544                ) );
     545
     546                $found1 = new BP_Signup( $s1 );
     547
     548                $num_queries = $wpdb->num_queries;
     549
     550                // Object should be rebuilt from cache.
     551                $found2 = new BP_Signup( $s1 );
     552
     553                // @TODO: This fails because "get_avatar()" in populate() results in db queries.
     554                $this->assertEquals( $found1, $found2 );
     555                $this->assertEquals( $num_queries, $wpdb->num_queries );
     556        }
     557
     558        /**
     559         * @group cache
     560         */
     561        public function test_signup_object_caches_should_be_busted_by_activation() {
     562                $s1 = self::factory()->signup->create( array(
     563                        'user_login'     => 'accountone',
     564                        'user_email'     => 'accountone@example.com',
     565                        'activation_key' => 'activationkeyone',
     566                ) );
     567
     568                $found1 = new BP_Signup( $s1 );
     569                $this->assertEquals( $s1, $found1->id );
     570                $this->assertFalse( $found1->active );
     571
     572                $activate = BP_Signup::activate( (array) $s1 );
     573
     574                $found2 = new BP_Signup( $s1 );
     575                $this->assertEquals( $s1, $found2->id );
     576                $this->assertTrue( $found2->active );
     577
     578        }
    359579}