Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/09/2024 01:13:58 PM (2 years ago)
Author:
espellcaste
Message:

XProfile: Add cache_results flag to the BP_XProfile_Group::get getter.

When performing a request with cache_results, it stops the XProfile group information retrieved from being added to the cache.

Props imath.

See #8552
Closes https://github.com/buddypress/buddypress/pull/357

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-xprofile/classes/class-bp-xprofile-group.php

    r13890 r14000  
    9191
    9292                // Get this group.
    93                 $group = self::get( array(
    94                         'profile_group_id' => $id,
    95                 ) );
     93                $group = self::get(
     94                        array(
     95                                'profile_group_id' => $id,
     96                        )
     97                );
    9698
    9799                // Bail if group not found.
     
    124126
    125127                // Filter the field group attributes.
    126                 $this->name        = apply_filters( 'xprofile_group_name_before_save',        $this->name,        $this->id );
     128                $this->name        = apply_filters( 'xprofile_group_name_before_save', $this->name, $this->id );
    127129                $this->description = apply_filters( 'xprofile_group_description_before_save', $this->description, $this->id );
    128130
     
    240242         * @since 8.0.0  Introduced `$hide_field_types` & `$signup_fields_only` arguments.
    241243         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
     244         * @since 15.0.0 Introduced the `cache_results` parameter.
    242245         *
    243246         * @global wpdb $wpdb WordPress database object.
     
    246249         *      Array of optional arguments.
    247250         *
    248          *      @type int|int[]|bool $profile_group_id   Limit results to a single profile group or a comma-separated list or array of
    249          *                                               profile group ids. Default: false.
    250          *      @type int            $user_id            Required if you want to load a specific user's data.
    251          *                                               Default: displayed user's ID.
    252          *      @type array|string   $member_type        Limit fields by those restricted to a given member type, or array of
    253          *                                               member types. If `$user_id` is provided, the value of `$member_type`
    254          *                                               will be overridden by the member types of the provided user. The
    255          *                                               special value of 'any' will return only those fields that are
    256          *                                               unrestricted by member type - i.e., those applicable to any type.
    257          *      @type bool           $hide_empty_groups  True to hide groups that don't have any fields. Default: false.
    258          *      @type bool           $hide_empty_fields  True to hide fields where the user has not provided data.
    259          *                                              Default: false.
    260          *      @type bool           $fetch_fields       Whether to fetch each group's fields. Default: false.
    261          *      @type bool           $fetch_field_data   Whether to fetch data for each field. Requires a $user_id.
    262          *                                               Default: false.
    263          *      @type int[]|bool     $exclude_groups     Comma-separated list or array of group IDs to exclude.
    264          *      @type int[]|bool     $exclude_fields     Comma-separated list or array of field IDs to exclude.
    265          *      @type string[]       $hide_field_types   List of field types to hide form loop. Default: empty array.
    266          *      @type bool           $signup_fields_only Whether to only return signup fields. Default: false.
    267          *      @type bool           $update_meta_cache  Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
    268          *                                               and data. Default: true.
     251         *      @type int|int[]|bool $profile_group_id        Limit results to a single profile group or a comma-separated list or array of
     252         *                                                    profile group ids. Default: false.
     253         *      @type int            $user_id                 Required if you want to load a specific user's data.
     254         *                                                    Default: displayed user's ID.
     255         *      @type array|string   $member_type             Limit fields by those restricted to a given member type, or array of
     256         *                                                    member types. If `$user_id` is provided, the value of `$member_type`
     257         *                                                    will be overridden by the member types of the provided user. The
     258         *                                                    special value of 'any' will return only those fields that are
     259         *                                                    unrestricted by member type - i.e., those applicable to any type.
     260         *      @type bool           $hide_empty_groups       True to hide groups that don't have any fields. Default: false.
     261         *      @type bool           $hide_empty_fields       True to hide fields where the user has not provided data. Default: false.
     262         *      @type bool           $fetch_visibility_level  Whether to fetch the visibility level for each field. Default: false.
     263         *      @type bool           $fetch_fields            Whether to fetch each group's fields. Default: false.
     264         *      @type bool           $fetch_field_data        Whether to fetch data for each field. Requires a $user_id. Default: false.
     265         *      @type int[]|bool     $exclude_groups          Comma-separated list or array of group IDs to exclude.
     266         *      @type int[]|bool     $exclude_fields          Comma-separated list or array of field IDs to exclude.
     267         *      @type string[]       $hide_field_types        List of field types to hide form loop. Default: empty array.
     268         *      @type bool           $signup_fields_only      Whether to only return signup fields. Default: false.
     269         *      @type bool           $cache_results           Whether to cache the XProfile group information. Default: true.
     270         *      @type bool           $update_meta_cache       Whether to pre-fetch xprofilemeta for all retrieved groups, fields,
     271         *                                                    and data. Default: true.
    269272         * }
    270273         * @return array
     
    286289                                'fetch_visibility_level' => false,
    287290                                'exclude_groups'         => false,
     291                                'cache_results'          => true,
    288292                                'exclude_fields'         => false,
    289293                                'hide_field_types'       => array(),
     
    305309
    306310                // Get all group data.
    307                 $groups = self::get_group_data( $group_ids );
     311                $groups = self::get_group_data( $group_ids, $r );
    308312
    309313                // Bail if not also getting fields.
     
    337341
    338342                // Prime the field cache.
    339                 $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
    340                 if ( ! empty( $uncached_field_ids ) ) {
    341                         $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
    342                         $uncached_fields = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
    343                         foreach ( $uncached_fields as $uncached_field ) {
    344                                 $fid = intval( $uncached_field->id );
    345                                 wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
     343                if ( $r['cache_results'] ) {
     344                        $uncached_field_ids = bp_get_non_cached_ids( $field_ids, 'bp_xprofile_fields' );
     345                        if ( ! empty( $uncached_field_ids ) ) {
     346                                $_uncached_field_ids = implode( ',', array_map( 'intval', $uncached_field_ids ) );
     347                                $uncached_fields     = $wpdb->get_results( "SELECT * FROM {$bp->profile->table_name_fields} WHERE id IN ({$_uncached_field_ids})" );
     348                                foreach ( $uncached_fields as $uncached_field ) {
     349                                        $fid = intval( $uncached_field->id );
     350                                        wp_cache_set( $fid, $uncached_field, 'bp_xprofile_fields' );
     351                                }
    346352                        }
    347353                }
     
    386392                                        $maybe_value = maybe_unserialize( $data->value );
    387393
     394                                        $key = array_search( $data->field_id, $field_ids, true );
     395
    388396                                        // Valid field values of 0 or '0' get caught by empty(), so we have an extra check for these. See #BP5731.
    389                                         if ( ( ! empty( $maybe_value ) || '0' == $maybe_value ) && false !== $key = array_search( $data->field_id, $field_ids ) ) {
     397                                        if ( ( ( ! empty( $maybe_value ) || '0' === $maybe_value ) ) && false !== $key ) {
    390398
    391399                                                // Fields that have data get removed from the list.
     
    396404                                // The remaining members of $field_ids are empty. Remove them.
    397405                                foreach ( $fields as $field_key => $field ) {
    398                                         if ( in_array( $field->id, $field_ids ) ) {
     406                                        if ( in_array( $field->id, $field_ids, true ) ) {
    399407                                                unset( $fields[ $field_key ] );
    400408                                        }
     
    415423
    416424                                                // Assign correct data value to the field.
    417                                                 if ( $field->id == $data->field_id ) {
    418                                                         $fields[ $field_key ]->data        = new stdClass;
     425                                                if ( $field->id === $data->field_id ) {
     426                                                        $fields[ $field_key ]->data        = new stdClass();
    419427                                                        $fields[ $field_key ]->data->value = $data->value;
    420428                                                        $fields[ $field_key ]->data->id    = $data->id;
     
    442450                        // Indexes may have been shifted after previous deletions, so we get a
    443451                        // fresh one each time through the loop.
    444                         $index = array_search( $group, $groups );
     452                        $index = array_search( $group, $groups, true );
    445453
    446454                        foreach ( (array) $fields as $field ) {
     
    468476         * @since 5.0.0
    469477         * @since 11.0.0 `$profile_group_id` accepts an array of profile group ids.
     478         * @since 15.0.0 Introduced the `cache_results` parameter.
    470479         *
    471480         * @global wpdb $wpdb WordPress database object.
     
    475484         *
    476485         *    @type int|int[]|bool $profile_group_id  Limit results to a single profile group or a comma-separated list or array of
    477          *                                       profile group ids. Default: false.
     486         *                                            profile group ids. Default: false.
    478487         *    @type int[]          $exclude_groups    Comma-separated list or array of group IDs to exclude. Default: false.
    479488         *    @type bool           $hide_empty_groups True to hide groups that don't have any fields. Default: false.
     489         *    @type bool           $cache_results     Whether to cache the XProfile group information. Default: true.
    480490         * }
    481491         * @return array
     
    484494                global $wpdb;
    485495
    486                 $r = array_merge(
     496                $r = bp_parse_args(
     497                        $args,
    487498                        array(
    488499                                'profile_group_id'  => false,
    489500                                'exclude_groups'    => false,
    490501                                'hide_empty_groups' => false,
    491                         ),
    492                         $args
     502                                'cache_results'     => true,
     503                        )
    493504                );
    494505
     
    512523                }
    513524
    514                 $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' );
    515                 if ( false === $cached ) {
     525                if ( $r['cache_results'] ) {
     526                        $cached = bp_core_get_incremented_cache( $group_ids_sql, 'bp_xprofile_groups' );
     527                        if ( false === $cached ) {
     528                                $group_ids = $wpdb->get_col( $group_ids_sql );
     529                                bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids );
     530                        } else {
     531                                $group_ids = $cached;
     532                        }
     533                } else {
    516534                        $group_ids = $wpdb->get_col( $group_ids_sql );
    517                         bp_core_set_incremented_cache( $group_ids_sql, 'bp_xprofile_groups', $group_ids );
    518                 } else {
    519                         $group_ids = $cached;
    520535                }
    521536
     
    527542         *
    528543         * @since 5.0.0
     544         * @since 15.0.0 Added support for the `cache_results` parameter.
    529545         *
    530546         * @global wpdb $wpdb WordPress database object.
    531547         *
    532          * @param array $group_ids Array of group IDs.
    533          * @param array $args {
    534          *    Array of optional arguments:
    535          *      @type array        $exclude_fields    Comma-separated list or array of field IDs to exclude.
    536          *                                            Default empty.
    537          *      @type int          $user_id           Limit results to fields associated with a given user's
    538          *                                            member type. Default empty.
    539          *      @type array|string $member_type       Limit fields by those restricted to a given member type, or array of
    540          *                                            member types. If `$user_id` is provided, the value of `$member_type`
    541          *                                            is honored.
    542          * }
    543          * @return array
     548         * @param int[] $group_ids Array of group IDs.
     549         * @param array $args      Optional. See {@link BP_XProfile_Group::get()}.
     550         * @return int[]
    544551         */
    545552        public static function get_group_field_ids( $group_ids, $args = array() ) {
    546553                global $wpdb;
    547554
    548                 $r = array_merge(
     555                $r = bp_parse_args(
     556                        $args,
    549557                        array(
    550558                                'exclude_fields' => false,
    551                                 'user_id' => false,
    552                                 'member_type' => false,
    553                         ),
    554                         $args
     559                                'user_id'        => false,
     560                                'member_type'    => false,
     561                                'cache_results'  => true,
     562                        )
    555563                );
    556564
     
    561569                        $group_ids = array( 0 );
    562570                }
     571
    563572                $group_ids_in = implode( ',', array_map( 'intval', $group_ids ) );
    564573
     
    594603
    595604                                $member_types_fields = BP_XProfile_Field::get_fields_for_member_type( $member_types );
    596                                 $include_field_ids += array_keys( $member_types_fields );
     605                                $include_field_ids  += array_keys( $member_types_fields );
    597606                        }
    598607                }
     
    601610                if ( ! empty( $include_field_ids ) ) {
    602611                        $include_field_ids_cs = implode( ',', array_map( 'intval', $include_field_ids ) );
    603                         $in_sql = " AND id IN ({$include_field_ids_cs}) ";
     612                        $in_sql               = " AND id IN ({$include_field_ids_cs}) ";
    604613                }
    605614
    606615                $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";
    607616
    608                 $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' );
    609                 if ( false === $cached ) {
     617                if ( $r['cache_results'] ) {
     618                        $cached = bp_core_get_incremented_cache( $field_ids_sql, 'bp_xprofile_groups' );
     619                        if ( false === $cached ) {
     620                                $field_ids = $wpdb->get_col( $field_ids_sql );
     621                                bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids );
     622                        } else {
     623                                $field_ids = $cached;
     624                        }
     625                } else {
    610626                        $field_ids = $wpdb->get_col( $field_ids_sql );
    611                         bp_core_set_incremented_cache( $field_ids_sql, 'bp_xprofile_groups', $field_ids );
    612                 } else {
    613                         $field_ids = $cached;
    614627                }
    615628
     
    621634         *
    622635         * @since 2.0.0
     636         * @since 15.0.0 Added additional `$r` parameter.
    623637         *
    624638         * @global wpdb $wpdb WordPress database object.
    625639         *
    626640         * @param array $group_ids Array of IDs.
     641         * @param array $r         Optional. See {@link BP_XProfile_Group::get()}.
    627642         * @return array
    628643         */
    629         protected static function get_group_data( $group_ids ) {
     644        protected static function get_group_data( $group_ids, $r = array() ) {
    630645                global $wpdb;
    631646
     
    634649                        return array();
    635650                }
     651
     652                $r = bp_parse_args(
     653                        $r,
     654                        array( 'cache_results' => true )
     655                );
    636656
    637657                // Setup empty arrays.
     
    647667                                $groups[ $group_id ] = $group_data;
    648668
    649                         // Otherwise leave a placeholder so we don't lose the order.
     669                                // Otherwise leave a placeholder so we don't lose the order.
    650670                        } else {
    651671                                $groups[ $group_id ] = '';
     
    666686
    667687                        // Fetch data, preserving order.
    668                         $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )");
     688                        $queried_gdata = $wpdb->get_results( "SELECT * FROM {$table_name} WHERE id IN ({$uncached_gids_sql}) ORDER BY FIELD( id, {$uncached_gids_sql} )" );
    669689
    670690                        // Make sure query returned valid data.
     
    679699
    680700                                        // Cache previously uncached group data.
    681                                         wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' );
     701                                        if ( $r['cache_results'] ) {
     702                                                wp_cache_set( $gdata->id, $gdata, 'bp_xprofile_groups' );
     703                                        }
    682704                                }
    683705                        }
     
    767789                                $field_visibility = $visibility_levels[ $field->id ];
    768790
    769                         // If no admin-set default is saved, fall back on a global default.
     791                                // If no admin-set default is saved, fall back on a global default.
    770792                        } else {
    771793                                $fallback_visibility = bp_xprofile_get_meta( $field->id, 'field', 'default_visibility' );
     
    815837                        foreach ( $levels as $level ) {
    816838                                switch ( $level->meta_key ) {
    817                                         case 'default_visibility' :
    818                                                 $default_visibility_levels[ $level->object_id ]['default']      = $level->meta_value;
     839                                        case 'default_visibility':
     840                                                $default_visibility_levels[ $level->object_id ]['default'] = $level->meta_value;
    819841                                                break;
    820                                         case 'allow_custom_visibility' :
     842                                        case 'allow_custom_visibility':
    821843                                                $default_visibility_levels[ $level->object_id ]['allow_custom'] = $level->meta_value;
    822844                                                break;
     
    846868
    847869                // URL to cancel to.
    848                 $cancel_url = add_query_arg( array(
    849                         'page' => 'bp-profile-setup',
    850                 ), $users_url );
     870                $cancel_url = add_query_arg(
     871                        array(
     872                                'page' => 'bp-profile-setup',
     873                        ),
     874                        $users_url
     875                );
    851876
    852877                // New field group.
    853878                if ( empty( $this->id ) ) {
    854                         $title       = __( 'Add New Field Group', 'buddypress' );
    855                         $button      = __( 'Save',                'buddypress' );
    856                         $action      = add_query_arg(
     879                        $title       = __( 'Add New Field Group', 'buddypress' );
     880                        $button      = __( 'Save', 'buddypress' );
     881                        $action      = add_query_arg(
    857882                                array(
    858883                                        'page' => 'bp-profile-setup',
     
    863888                        $description = '';
    864889
    865                 // Existing field group.
     890                        // Existing field group.
    866891                } else {
    867892                        $title       = __( 'Edit Field Group', 'buddypress' );
    868                         $button      = __( 'Update',          'buddypress' );
    869                         $action      = add_query_arg(
     893                        $button      = __( 'Update', 'buddypress' );
     894                        $action      = add_query_arg(
    870895                                array(
    871896                                        'page'     => 'bp-profile-setup',
     
    879904                        if ( $this->can_delete ) {
    880905                                // Delete Group URL.
    881                                 $delete_url = wp_nonce_url( add_query_arg( array(
    882                                         'page'     => 'bp-profile-setup',
    883                                         'mode'     => 'delete_group',
    884                                         'group_id' => (int) $this->id,
    885                                 ), $users_url ), 'bp_xprofile_delete_group' );
     906                                $delete_url = wp_nonce_url(
     907                                        add_query_arg(
     908                                                array(
     909                                                        'page'     => 'bp-profile-setup',
     910                                                        'mode'     => 'delete_group',
     911                                                        'group_id' => (int) $this->id,
     912                                                ),
     913                                                $users_url
     914                                        ),
     915                                        'bp_xprofile_delete_group'
     916                                );
    886917                        }
    887918                } ?>
     
    906937                                                        <div id="titlediv">
    907938                                                                <div class="titlewrap">
    908                                                                         <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress') ?></label>
     939                                                                        <label id="title-prompt-text" for="title"><?php esc_html_e( 'Field Group Name (required)', 'buddypress' ); ?></label>
    909940                                                                        <input type="text" name="group_name" id="title" value="<?php echo esc_attr( $this->name ); ?>" autocomplete="off" />
    910941                                                                </div>
     
    932963                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    933964                                                         */
    934                                                         do_action( 'xprofile_group_admin_after_description', $this ); ?>
     965                                                        do_action( 'xprofile_group_admin_after_description', $this );
     966                                                        ?>
    935967
    936968                                                </div><!-- #post-body-content -->
     
    947979                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    948980                                                         */
    949                                                         do_action( 'xprofile_group_before_submitbox', $this ); ?>
     981                                                        do_action( 'xprofile_group_before_submitbox', $this );
     982                                                        ?>
    950983
    951984                                                        <div id="submitdiv" class="postbox">
     
    9671000                                                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    9681001                                                                                         */
    969                                                                                         do_action( 'xprofile_group_submitbox_start', $this ); ?>
     1002                                                                                        do_action( 'xprofile_group_submitbox_start', $this );
     1003                                                                                        ?>
    9701004
    9711005                                                                                        <input type="hidden" name="group_order" id="group_order" value="<?php echo esc_attr( $this->group_order ); ?>" />
     
    9951029                                                         * @param BP_XProfile_Group $field_group Current instance of the field group.
    9961030                                                         */
    997                                                         do_action( 'xprofile_group_after_submitbox', $this ); ?>
     1031                                                        do_action( 'xprofile_group_after_submitbox', $this );
     1032                                                        ?>
    9981033
    9991034                                                </div>
     
    10031038                </div>
    10041039
    1005         <?php
     1040                <?php
    10061041        }
    10071042}
Note: See TracChangeset for help on using the changeset viewer.