Skip to:
Content

BuddyPress.org

Changeset 12898


Ignore:
Timestamp:
04/20/2021 04:02:43 PM (3 months ago)
Author:
dcavins
Message:

Introduce BP_Optouts.

Add capability to store opt-out requests from
nonmembers who have been contacted by
communication from a BuddyPress site. These
new objects are represented by the class BP_Optout
and are stored in a new database table
wp_bp_optouts. This commit adds the following capabilities:

  • Add new class BP_Optout.
  • Add new table wp_bp_optouts.
  • Create new table on installation or upgrade.
  • Add convenience functions for adding, fetching or

deleting opt-outs.

  • Add tests for basic opt-out management.

See #8448.

Location:
trunk
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/admin/bp-core-admin-schema.php

    r12886 r12898  
    4040    // Install the invitations table.
    4141    bp_core_install_invitations();
     42
     43    // Install the nonmember opt-outs table.
     44    bp_core_install_nonmember_opt_outs();
    4245
    4346    // Notifications.
     
    591594    do_action( 'bp_core_install_invitations' );
    592595}
     596
     597/**
     598 * Install database tables to store opt-out requests from nonmembers.
     599 *
     600 * @since 8.0.0
     601 *
     602 * @uses bp_core_set_charset()
     603 * @uses bp_core_get_table_prefix()
     604 * @uses dbDelta()
     605 */
     606function bp_core_install_nonmember_opt_outs() {
     607    $sql             = array();
     608    $charset_collate = $GLOBALS['wpdb']->get_charset_collate();
     609    $bp_prefix       = bp_core_get_table_prefix();
     610    $optouts_class   = new BP_Optout();
     611    $table_name      = $optouts_class->get_table_name();
     612    $sql = "CREATE TABLE {$table_name} (
     613        id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     614        email_address_hash varchar(255) NOT NULL,
     615        user_id bigint(20) NOT NULL,
     616        email_type varchar(255) NOT NULL,
     617        date_modified datetime NOT NULL,
     618        KEY user_id (user_id),
     619        KEY email_type (email_type),
     620        KEY date_modified (date_modified)
     621        ) {$charset_collate};";
     622    dbDelta( $sql );
     623
     624    /**
     625     * Fires after BuddyPress adds the nonmember opt-outs table.
     626     *
     627     * @since 8.0.0
     628     */
     629    do_action( 'bp_core_install_nonmember_opt_outs' );
     630}
  • trunk/src/bp-core/bp-core-cache.php

    r12729 r12898  
    416416add_action( 'bp_type_updated', 'bp_clear_object_type_terms_cache' );
    417417add_action( 'bp_type_deleted', 'bp_clear_object_type_terms_cache' );
     418
     419/**
     420 * Resets all incremented bp_optout caches.
     421 *
     422 * @since 8.0.0
     423 */
     424function bp_optouts_reset_cache_incrementor() {
     425    bp_core_reset_incrementor( 'bp_optouts' );
     426}
     427add_action( 'bp_optout_after_save', 'bp_optouts_reset_cache_incrementor' );
     428add_action( 'bp_optout_after_delete', 'bp_optouts_reset_cache_incrementor' );
  • trunk/src/bp-core/bp-core-functions.php

    r12893 r12898  
    42774277    return apply_filters( 'bp_get_widget_max_count_limit', 50, $widget_class );
    42784278}
     4279
     4280/**
     4281 * Add a new BP_Optout.
     4282 *
     4283 * @since 8.0.0
     4284 *
     4285 * @param array $args {
     4286 *     An array of arguments describing the new opt-out.
     4287 *     @type string $email_address Email address of user who has opted out.
     4288 *     @type int    $user_id       Optional. ID of user whose communication
     4289 *                                 prompted the user to opt-out.
     4290 *     @type string $email_type    Optional. Name of the email type that
     4291 *                                 prompted the user to opt-out.
     4292 *     @type string $date_modified Optional. Specify a time, else now will be used.
     4293 * }
     4294 * @return false|int False on failure, ID of new (or existing) opt-out if successful.
     4295 */
     4296function bp_add_optout( $args = array() ) {
     4297    $optout = new BP_Optout();
     4298    $r      = bp_parse_args(
     4299        $args, array(
     4300            'email_address' => '',
     4301            'user_id'       => 0,
     4302            'email_type'    => '',
     4303            'date_modified' => bp_core_current_time(),
     4304        ),
     4305        'add_optout'
     4306    );
     4307
     4308    // Opt-outs must have an email address.
     4309    if ( empty( $r['email_address'] ) ) {
     4310        return false;
     4311    }
     4312
     4313    // Avoid creating duplicate opt-outs.
     4314    $optout_id = $optout->optout_exists(
     4315        array(
     4316            'email_address' => $r['email_address'],
     4317            'user_id'       => $r['user_id'],
     4318            'email_type'    => $r['email_type'],
     4319        )
     4320    );
     4321
     4322    if ( ! $optout_id ) {
     4323        // Set up the new opt-out.
     4324        $optout->email_address = $r['email_address'];
     4325        $optout->user_id       = $r['user_id'];
     4326        $optout->email_type    = $r['email_type'];
     4327        $optout->date_modified = $r['date_modified'];
     4328
     4329        $optout_id = $optout->save();
     4330    }
     4331
     4332    return $optout_id;
     4333}
     4334
     4335/**
     4336 * Find matching BP_Optouts.
     4337 *
     4338 * @since 8.0.0
     4339 *
     4340 * @see BP_Optout::get() for a description of parameters and return values.
     4341 *
     4342 * @param array $args See {@link BP_Optout::get()}.
     4343 * @return array See {@link BP_Optout::get()}.
     4344 */
     4345function bp_get_optouts( $args = array() ) {
     4346    $optout_class = new BP_Optout();
     4347    return $optout_class::get( $args );
     4348}
     4349
     4350/**
     4351 * Delete a BP_Optout by ID.
     4352 *
     4353 * @since 8.0.0
     4354 *
     4355 * @param int $id ID of the optout to delete.
     4356 * @return bool True on success, false on failure.
     4357 */
     4358function bp_delete_optout_by_id( $id = 0 ) {
     4359    $optout_class = new BP_Optout();
     4360    return $optout_class::delete_by_id( $id );
     4361}
  • trunk/src/bp-core/bp-core-update.php

    r12886 r12898  
    652652        }
    653653    }
     654
     655    bp_core_install_nonmember_opt_outs();
    654656}
    655657
  • trunk/src/bp-members/classes/class-bp-members-component.php

    r12892 r12898  
    179179                'table_name_invitations'   => bp_core_get_table_prefix() . 'bp_invitations',
    180180                'table_name_last_activity' => bp_core_get_table_prefix() . 'bp_activity',
     181                'table_name_optouts'       => bp_core_get_table_prefix() . 'bp_optouts',
    181182                'table_name_signups'       => $wpdb->base_prefix . 'signups', // Signups is a global WordPress table.
    182183            )
  • trunk/src/class-buddypress.php

    r12851 r12898  
    598598            'BP_REST_Attachments'          => 'core',
    599599            'BP_Admin_Types'               => 'core',
     600            'BP_Optout'                    => 'core',
    600601
    601602            'BP_Core_Friends_Widget'   => 'friends',
Note: See TracChangeset for help on using the changeset viewer.