[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-core/classes/ -> class-bp-optout.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Nonmember Opt-out Class
   4   *
   5   * @package BuddyPress
   6   * @subpackage Nonmember Opt-outs
   7   *
   8   * @since 8.0.0
   9   */
  10  
  11  // Exit if accessed directly.
  12  defined( 'ABSPATH' ) || exit;
  13  
  14  /**
  15   * BuddyPress opt-outs.
  16   *
  17   * Use this class to create, access, edit, or delete BuddyPress Nonmember Opt-outs.
  18   *
  19   * @since 8.0.0
  20   */
  21  class BP_Optout {
  22  
  23      /**
  24       * The opt-out ID.
  25       *
  26       * @since 8.0.0
  27       * @access public
  28       * @var int
  29       */
  30      public $id;
  31  
  32      /**
  33       * The hashed email address of the user that wishes to opt out of
  34       * communications from this site.
  35       *
  36       * @since 8.0.0
  37       * @access public
  38       * @var string
  39       */
  40      public $email_address;
  41  
  42      /**
  43       * The ID of the user that generated the contact that resulted in the opt-out.
  44       *
  45       * @since 8.0.0
  46       * @access public
  47       * @var int
  48       */
  49      public $user_id;
  50  
  51      /**
  52       * The type of email contact that resulted in the opt-out.
  53       * This should be one of the known BP_Email types.
  54       *
  55       * @since 8.0.0
  56       * @access public
  57       * @var string
  58       */
  59      public $email_type;
  60  
  61      /**
  62       * The date the opt-out was last modified.
  63       *
  64       * @since 8.0.0
  65       * @access public
  66       * @var string
  67       */
  68      public $date_modified;
  69  
  70      /** Public Methods ****************************************************/
  71  
  72      /**
  73       * Constructor method.
  74       *
  75       * @since 8.0.0
  76       *
  77       * @param int $id Optional. Provide an ID to access an existing
  78       *        optout item.
  79       */
  80  	public function __construct( $id = 0 ) {
  81          if ( ! empty( $id ) ) {
  82              $this->id = (int) $id;
  83              $this->populate();
  84          }
  85      }
  86  
  87      /**
  88       * Get the opt-outs table name.
  89       *
  90       * @since 8.0.0
  91       * @access public
  92       * @return string
  93       */
  94  	public static function get_table_name() {
  95          return buddypress()->members->table_name_optouts;
  96      }
  97  
  98      /**
  99       * Update or insert opt-out details into the database.
 100       *
 101       * @since 8.0.0
 102       *
 103       * @global wpdb $wpdb WordPress database object.
 104       *
 105       * @return bool True on success, false on failure.
 106       */
 107  	public function save() {
 108  
 109          // Return value
 110          $retval = false;
 111  
 112          // Default data and format
 113          $data = array(
 114              'email_address_hash' => $this->email_address,
 115              'user_id'            => $this->user_id,
 116              'email_type'         => sanitize_key( $this->email_type ),
 117              'date_modified'      => $this->date_modified,
 118          );
 119          $data_format = array( '%s', '%d', '%s', '%s' );
 120  
 121          /**
 122           * Fires before an opt-out is saved.
 123           *
 124           * @since 8.0.0
 125           *
 126           * @param BP_Optout object $this Characteristics of the opt-out to be saved.
 127           */
 128          do_action_ref_array( 'bp_optout_before_save', array( &$this ) );
 129  
 130          // Update.
 131          if ( ! empty( $this->id ) ) {
 132              $result = self::_update( $data, array( 'ID' => $this->id ), $data_format, array( '%d' ) );
 133          // Insert.
 134          } else {
 135              $result = self::_insert( $data, $data_format );
 136          }
 137  
 138          // Set the opt-out ID if successful.
 139          if ( ! empty( $result ) && ! is_wp_error( $result ) ) {
 140              global $wpdb;
 141  
 142              $this->id = $wpdb->insert_id;
 143              $retval   = $wpdb->insert_id;
 144          }
 145  
 146          wp_cache_delete( $this->id, 'bp_optouts' );
 147  
 148          /**
 149           * Fires after an optout is saved.
 150           *
 151           * @since 8.0.0
 152           *
 153           * @param BP_optout object $this Characteristics of the opt-out just saved.
 154           */
 155          do_action_ref_array( 'bp_optout_after_save', array( &$this ) );
 156  
 157          // Return the result.
 158          return $retval;
 159      }
 160  
 161      /**
 162       * Fetch data for an existing opt-out from the database.
 163       *
 164       * @since 8.0.0
 165       *
 166       * @global BuddyPress $bp The one true BuddyPress instance.
 167       * @global wpdb $wpdb WordPress database object.
 168       */
 169  	public function populate() {
 170          global $wpdb;
 171          $optouts_table_name = $this->get_table_name();
 172  
 173          // Check cache for optout data.
 174          $optout = wp_cache_get( $this->id, 'bp_optouts' );
 175  
 176          // Cache missed, so query the DB.
 177          if ( false === $optout ) {
 178              $optout = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$optouts_table_name} WHERE id = %d", $this->id ) );
 179              wp_cache_set( $this->id, $optout, 'bp_optouts' );
 180          }
 181  
 182          // No optout found so set the ID and bail.
 183          if ( empty( $optout ) || is_wp_error( $optout ) ) {
 184              $this->id = 0;
 185              return;
 186          }
 187  
 188          $this->email_address = $optout->email_address_hash;
 189          $this->user_id       = (int) $optout->user_id;
 190          $this->email_type    = sanitize_key( $optout->email_type );
 191          $this->date_modified = $optout->date_modified;
 192  
 193      }
 194  
 195      /** Protected Static Methods ******************************************/
 196  
 197      /**
 198       * Create an opt-out entry.
 199       *
 200       * @since 8.0.0
 201       *
 202       * @param array $data {
 203       *     Array of optout data, passed to {@link wpdb::insert()}.
 204       *       @type string $email_address     The hashed email address of the user that wishes to opt out of
 205       *                                     communications from this site.
 206       *       @type int    $user_id           The ID of the user that generated the contact that resulted in the opt-out.
 207       *        @type string $email_type        The type of email contact that resulted in the opt-out.
 208       *        @type string $date_modified     Date the opt-out was last modified.
 209       * }
 210       * @param array $data_format See {@link wpdb::insert()}.
 211       * @return int|false The number of rows inserted, or false on error.
 212       */
 213  	protected static function _insert( $data = array(), $data_format = array() ) {
 214          global $wpdb;
 215          // We must lowercase and hash the email address at insert.
 216          $email                      = strtolower( $data['email_address_hash'] );
 217          $data['email_address_hash'] = wp_hash( $email );
 218          return $wpdb->insert( BP_Optout::get_table_name(), $data, $data_format );
 219      }
 220  
 221      /**
 222       * Update opt-outs.
 223       *
 224       * @since 8.0.0
 225       *
 226       * @see wpdb::update() for further description of paramater formats.
 227       *
 228       * @param array $data         Array of opt-out data to update, passed to
 229       *                            {@link wpdb::update()}. Accepts any property of a
 230       *                            BP_optout object.
 231       * @param array $where        The WHERE params as passed to wpdb::update().
 232       *                            Typically consists of array( 'ID' => $id ) to specify the ID
 233       *                            of the item being updated. See {@link wpdb::update()}.
 234       * @param array $data_format  See {@link wpdb::insert()}.
 235       * @param array $where_format See {@link wpdb::insert()}.
 236       * @return int|false The number of rows updated, or false on error.
 237       */
 238  	protected static function _update( $data = array(), $where = array(), $data_format = array(), $where_format = array() ) {
 239          global $wpdb;
 240  
 241          // Ensure that a passed email address is lowercased and hashed.
 242          if ( ! empty( $data['email_address_hash'] ) && is_email( $data['email_address_hash'] ) ) {
 243              $email                      = strtolower( $data['email_address_hash'] );
 244              $data['email_address_hash'] = wp_hash( $email );
 245          }
 246  
 247          return $wpdb->update( BP_Optout::get_table_name(), $data, $where, $data_format, $where_format );
 248      }
 249  
 250      /**
 251       * Delete opt-outs.
 252       *
 253       * @since 8.0.0
 254       *
 255       * @see wpdb::update() for further description of paramater formats.
 256       *
 257       * @param array $where        Array of WHERE clauses to filter by, passed to
 258       *                            {@link wpdb::delete()}. Accepts any property of a
 259       *                            BP_optout object.
 260       * @param array $where_format See {@link wpdb::insert()}.
 261       * @return int|false The number of rows updated, or false on error.
 262       */
 263  	protected static function _delete( $where = array(), $where_format = array() ) {
 264          global $wpdb;
 265          return $wpdb->delete( BP_Optout::get_table_name(), $where, $where_format );
 266      }
 267  
 268      /**
 269       * Assemble the WHERE clause of a get() SQL statement.
 270       *
 271       * Used by BP_optout::get() to create its WHERE
 272       * clause.
 273       *
 274       * @since 8.0.0
 275       *
 276       * @param array $args See {@link BP_optout::get()} for more details.
 277       * @return string WHERE clause.
 278       */
 279  	protected static function get_where_sql( $args = array() ) {
 280          global $wpdb;
 281  
 282          $where_conditions = array();
 283          $where            = '';
 284  
 285          // id.
 286          if ( false !== $args['id'] ) {
 287              $id_in                  = implode( ',', wp_parse_id_list( $args['id'] ) );
 288              $where_conditions['id'] = "id IN ({$id_in})";
 289          }
 290  
 291          // email_address.
 292          if ( ! empty( $args['email_address'] ) ) {
 293              if ( ! is_array( $args['email_address'] ) ) {
 294                  $emails = explode( ',', $args['email_address'] );
 295              } else {
 296                  $emails = $args['email_address'];
 297              }
 298  
 299              $email_clean = array();
 300              foreach ( $emails as $email ) {
 301                  $email         = strtolower( $email );
 302                  $email_hash    = wp_hash( $email );
 303                  $email_clean[] = $wpdb->prepare( '%s', $email_hash );
 304              }
 305  
 306              $email_in                          = implode( ',', $email_clean );
 307              $where_conditions['email_address'] = "email_address_hash IN ({$email_in})";
 308          }
 309  
 310          // user_id.
 311          if ( ! empty( $args['user_id'] ) ) {
 312              $user_id_in                  = implode( ',', wp_parse_id_list( $args['user_id'] ) );
 313              $where_conditions['user_id'] = "user_id IN ({$user_id_in})";
 314          }
 315  
 316          // email_type.
 317          if ( ! empty( $args['email_type'] ) ) {
 318              if ( ! is_array( $args['email_type'] ) ) {
 319                  $email_types = explode( ',', $args['email_type'] );
 320              } else {
 321                  $email_types = $args['email_type'];
 322              }
 323  
 324              $et_clean = array();
 325              foreach ( $email_types as $et ) {
 326                  $et_clean[] = $wpdb->prepare( '%s', sanitize_key( $et ) );
 327              }
 328  
 329              $et_in                          = implode( ',', $et_clean );
 330              $where_conditions['email_type'] = "email_type IN ({$et_in})";
 331          }
 332  
 333          // search_terms.
 334          if ( ! empty( $args['search_terms'] ) ) {
 335              // Matching email_address is an exact match because of the hashing.
 336              $args['search_terms']             = strtolower( $args['search_terms'] );
 337              $search_terms_like                = wp_hash( $args['search_terms'] );
 338              $where_conditions['search_terms'] = $wpdb->prepare( '( email_address_hash LIKE %s )', $search_terms_like );
 339          }
 340  
 341          // Custom WHERE.
 342          if ( ! empty( $where_conditions ) ) {
 343              $where = 'WHERE ' . implode( ' AND ', $where_conditions );
 344          }
 345  
 346          return $where;
 347      }
 348  
 349      /**
 350       * Assemble the ORDER BY clause of a get() SQL statement.
 351       *
 352       * Used by BP_Optout::get() to create its ORDER BY
 353       * clause.
 354       *
 355       * @since 8.0.0
 356       *
 357       * @param array $args See {@link BP_optout::get()} for more details.
 358       * @return string ORDER BY clause.
 359       */
 360  	protected static function get_order_by_sql( $args = array() ) {
 361  
 362          $conditions = array();
 363          $retval     = '';
 364  
 365          // Order by.
 366          if ( ! empty( $args['order_by'] ) ) {
 367              $order_by_clean = array();
 368              $columns        = array( 'id', 'email_address_hash', 'user_id', 'email_type', 'date_modified' );
 369              foreach ( (array) $args['order_by'] as $key => $value ) {
 370                  if ( in_array( $value, $columns, true ) ) {
 371                      $order_by_clean[] = $value;
 372                  }
 373              }
 374              if ( ! empty( $order_by_clean ) ) {
 375                  $order_by               = implode( ', ', $order_by_clean );
 376                  $conditions['order_by'] = "{$order_by}";
 377              }
 378          }
 379  
 380          // Sort order direction.
 381          if ( ! empty( $args['sort_order'] ) ) {
 382              $sort_order               = bp_esc_sql_order( $args['sort_order'] );
 383              $conditions['sort_order'] = "{$sort_order}";
 384          }
 385  
 386          // Custom ORDER BY.
 387          if ( ! empty( $conditions['order_by'] ) ) {
 388              $retval = 'ORDER BY ' . implode( ' ', $conditions );
 389          }
 390  
 391          return $retval;
 392      }
 393  
 394      /**
 395       * Assemble the LIMIT clause of a get() SQL statement.
 396       *
 397       * Used by BP_Optout::get() to create its LIMIT clause.
 398       *
 399       * @since 8.0.0
 400       *
 401       * @param array $args See {@link BP_optout::get()} for more details.
 402       * @return string LIMIT clause.
 403       */
 404  	protected static function get_paged_sql( $args = array() ) {
 405          global $wpdb;
 406  
 407          // Setup local variable.
 408          $retval = '';
 409  
 410          // Custom LIMIT.
 411          if ( ! empty( $args['page'] ) && ! empty( $args['per_page'] ) ) {
 412              $page     = absint( $args['page']     );
 413              $per_page = absint( $args['per_page'] );
 414              $offset   = $per_page * ( $page - 1 );
 415              $retval   = $wpdb->prepare( "LIMIT %d, %d", $offset, $per_page );
 416          }
 417  
 418          return $retval;
 419      }
 420  
 421      /**
 422       * Assemble query clauses, based on arguments, to pass to $wpdb methods.
 423       *
 424       * The insert(), update(), and delete() methods of {@link wpdb} expect
 425       * arguments of the following forms:
 426       *
 427       * - associative arrays whose key/value pairs are column => value, to
 428       *   be used in WHERE, SET, or VALUES clauses
 429       * - arrays of "formats", which tell $wpdb->prepare() which type of
 430       *   value to expect when sanitizing (eg, array( '%s', '%d' ))
 431       *
 432       * This utility method can be used to assemble both kinds of params,
 433       * out of a single set of associative array arguments, such as:
 434       *
 435       *     $args = array(
 436       *         'user_id'    => 4,
 437       *         'email_type' => 'type_string',
 438       *     );
 439       *
 440       * This will be converted to:
 441       *
 442       *     array(
 443       *         'data' => array(
 444       *             'user_id' => 4,
 445       *             'email_type'   => 'type_string',
 446       *         ),
 447       *         'format' => array(
 448       *             '%d',
 449       *             '%s',
 450       *         ),
 451       *     )
 452       *
 453       * which can easily be passed as arguments to the $wpdb methods.
 454       *
 455       * @since 8.0.0
 456       *
 457       * @param array $args Associative array of filter arguments.
 458       *                    See {@BP_optout::get()} for a breakdown.
 459       * @return array Associative array of 'data' and 'format' args.
 460       */
 461  	protected static function get_query_clauses( $args = array() ) {
 462          $where_clauses = array(
 463              'data'   => array(),
 464              'format' => array(),
 465          );
 466  
 467          // id.
 468          if ( ! empty( $args['id'] ) ) {
 469              $where_clauses['data']['id'] = absint( $args['id'] );
 470              $where_clauses['format'][]   = '%d';
 471          }
 472  
 473          // email_address.
 474          if ( ! empty( $args['email_address'] ) ) {
 475              $where_clauses['data']['email_address_hash'] = $args['email_address'];
 476              $where_clauses['format'][]                   = '%s';
 477          }
 478  
 479          // user_id.
 480          if ( ! empty( $args['user_id'] ) ) {
 481              $where_clauses['data']['user_id'] = absint( $args['user_id'] );
 482              $where_clauses['format'][]        = '%d';
 483          }
 484  
 485          // email_type.
 486          if ( ! empty( $args['email_type'] ) ) {
 487              $where_clauses['data']['email_type'] = $args['email_type'];
 488              $where_clauses['format'][]           = '%s';
 489          }
 490  
 491          return $where_clauses;
 492      }
 493  
 494      /** Public Static Methods *********************************************/
 495  
 496      /**
 497       * Get opt-outs, based on provided filter parameters.
 498       *
 499       * @since 8.0.0
 500       *
 501       * @param array $args {
 502       *     Associative array of arguments. All arguments but $page and
 503       *     $per_page can be treated as filter values for get_where_sql()
 504       *     and get_query_clauses(). All items are optional.
 505       *     @type int|array    $id                ID of opt-out.
 506       *                                           Can be an array of IDs.
 507       *     @type string|array $email_address     Email address of users who have opted out
 508       *                                             being queried. Can be an array of addresses.
 509       *     @type int|array    $user_id           ID of user whose communication prompted the
 510       *                                           opt-out. Can be an array of IDs.
 511       *     @type string|array $email_type        Name of the emil type to filter by.
 512       *                                           Can be an array of email types.
 513       *     @type string       $search_terms      Term to match against email_address field.
 514       *     @type string       $order_by          Database column to order by.
 515       *     @type string       $sort_order        Either 'ASC' or 'DESC'.
 516       *     @type int          $page              Number of the current page of results.
 517       *                                           Default: false (no pagination,
 518       *                                           all items).
 519       *     @type int          $per_page          Number of items to show per page.
 520       *                                           Default: false (no pagination,
 521       *                                           all items).
 522         *     @type string       $fields            Which fields to return. Specify 'email_addresses' to
 523         *                                           fetch a list of opt-out email_addresses.
 524         *                                           Specify 'user_ids' to
 525         *                                           fetch a list of opt-out user_ids.
 526         *                                           Specify 'ids' to fetch a list of opt-out IDs.
 527        *                                           Default: 'all' (return BP_Optout objects).
 528       * }
 529       *
 530       * @return array BP_Optout objects | IDs of found opt-outs | Email addresses of matches.
 531       */
 532  	public static function get( $args = array() ) {
 533          global $wpdb;
 534          $optouts_table_name = BP_Optout::get_table_name();
 535  
 536          // Parse the arguments.
 537          $r = bp_parse_args(
 538              $args,
 539              array(
 540                  'id'            => false,
 541                  'email_address' => false,
 542                  'user_id'       => false,
 543                  'email_type'    => false,
 544                  'search_terms'  => '',
 545                  'order_by'      => false,
 546                  'sort_order'    => false,
 547                  'page'          => false,
 548                  'per_page'      => false,
 549                  'fields'        => 'all',
 550              ),
 551              'bp_optout_get'
 552          );
 553  
 554          $sql = array(
 555              'select'     => "SELECT",
 556              'fields'     => '',
 557              'from'       => "FROM {$optouts_table_name} o",
 558              'where'      => '',
 559              'orderby'    => '',
 560              'pagination' => '',
 561          );
 562  
 563          if ( 'user_ids' === $r['fields'] ) {
 564              $sql['fields'] = "DISTINCT o.user_id";
 565          } else if ( 'email_addresses' === $r['fields'] ) {
 566              $sql['fields'] = "DISTINCT o.email_address_hash";
 567          } else {
 568              $sql['fields'] = 'DISTINCT o.id';
 569          }
 570  
 571          // WHERE.
 572          $sql['where'] = self::get_where_sql(
 573              array(
 574                  'id'            => $r['id'],
 575                  'email_address' => $r['email_address'],
 576                  'user_id'       => $r['user_id'],
 577                  'email_type'    => $r['email_type'],
 578                  'search_terms'  => $r['search_terms'],
 579              )
 580          );
 581  
 582          // ORDER BY.
 583          $sql['orderby'] = self::get_order_by_sql(
 584              array(
 585                  'order_by'   => $r['order_by'],
 586                  'sort_order' => $r['sort_order']
 587              )
 588          );
 589  
 590          // LIMIT %d, %d.
 591          $sql['pagination'] = self::get_paged_sql(
 592              array(
 593                  'page'     => $r['page'],
 594                  'per_page' => $r['per_page'],
 595              )
 596          );
 597  
 598          $paged_optouts_sql = "{$sql['select']} {$sql['fields']} {$sql['from']} {$sql['where']} {$sql['orderby']} {$sql['pagination']}";
 599  
 600          /**
 601           * Filters the pagination SQL statement.
 602           *
 603           * @since 8.0.0
 604           *
 605           * @param string $value Concatenated SQL statement.
 606           * @param array  $sql   Array of SQL parts before concatenation.
 607           * @param array  $r     Array of parsed arguments for the get method.
 608           */
 609          $paged_optouts_sql = apply_filters( 'bp_optouts_get_paged_optouts_sql', $paged_optouts_sql, $sql, $r );
 610  
 611          $cached = bp_core_get_incremented_cache( $paged_optouts_sql, 'bp_optouts' );
 612          if ( false === $cached ) {
 613              $paged_optout_ids = $wpdb->get_col( $paged_optouts_sql );
 614              bp_core_set_incremented_cache( $paged_optouts_sql, 'bp_optouts', $paged_optout_ids );
 615          } else {
 616              $paged_optout_ids = $cached;
 617          }
 618  
 619          // Special return format cases.
 620          if ( in_array( $r['fields'], array( 'ids', 'user_ids' ), true ) ) {
 621              // We only want the field that was found.
 622              return array_map( 'intval', $paged_optout_ids );
 623          } else if ( 'email_addresses' === $r['fields'] ) {
 624              return $paged_optout_ids;
 625          }
 626  
 627          $uncached_ids = bp_get_non_cached_ids( $paged_optout_ids, 'bp_optouts' );
 628          if ( $uncached_ids ) {
 629              $ids_sql = implode( ',', array_map( 'intval', $uncached_ids ) );
 630              $data_objects = $wpdb->get_results( "SELECT o.* FROM {$optouts_table_name} o WHERE o.id IN ({$ids_sql})" );
 631              foreach ( $data_objects as $data_object ) {
 632                  wp_cache_set( $data_object->id, $data_object, 'bp_optouts' );
 633              }
 634          }
 635  
 636          $paged_optouts = array();
 637          foreach ( $paged_optout_ids as $paged_optout_id ) {
 638              $paged_optouts[] = new BP_optout( $paged_optout_id );
 639          }
 640  
 641          return $paged_optouts;
 642      }
 643  
 644      /**
 645       * Get a count of total optouts matching a set of arguments.
 646       *
 647       * @since 8.0.0
 648       *
 649       * @see BP_optout::get() for a description of
 650       *      arguments.
 651       *
 652       * @param array $args See {@link BP_optout::get()}.
 653       * @return int Count of located items.
 654       */
 655  	public static function get_total_count( $args ) {
 656          global $wpdb;
 657          $optouts_table_name = BP_Optout::get_table_name();
 658  
 659          // Parse the arguments.
 660          $r  = bp_parse_args(
 661              $args,
 662              array(
 663                  'id'            => false,
 664                  'email_address' => false,
 665                  'user_id'       => false,
 666                  'email_type'    => false,
 667                  'search_terms'  => '',
 668                  'order_by'      => false,
 669                  'sort_order'    => false,
 670                  'page'          => false,
 671                  'per_page'      => false,
 672                  'fields'        => 'all',
 673              ),
 674              'bp_optout_get_total_count'
 675          );
 676  
 677          // Build the query
 678          $select_sql = "SELECT COUNT(*)";
 679          $from_sql   = "FROM {$optouts_table_name}";
 680          $where_sql  = self::get_where_sql( $r );
 681          $sql        = "{$select_sql} {$from_sql} {$where_sql}";
 682  
 683          // Return the queried results
 684          return $wpdb->get_var( $sql );
 685      }
 686  
 687      /**
 688       * Update optouts.
 689       *
 690       * @since 8.0.0
 691       *
 692       * @see BP_optout::get() for a description of
 693       *      accepted update/where arguments.
 694       *
 695       * @param array $update_args Associative array of fields to update,
 696       *                           and the values to update them to. Of the format
 697       *                           array( 'user_id' => 4, 'email_address' => 'bar@foo.com', ).
 698       * @param array $where_args  Associative array of columns/values, to
 699       *                           determine which rows should be updated. Of the format
 700       *                           array( 'user_id' => 7, 'email_address' => 'bar@foo.com', ).
 701       * @return int|bool Number of rows updated on success, false on failure.
 702       */
 703  	public static function update( $update_args = array(), $where_args = array() ) {
 704          $update = self::get_query_clauses( $update_args );
 705          $where  = self::get_query_clauses( $where_args  );
 706  
 707          /**
 708           * Fires before an opt-out is updated.
 709           *
 710           * @since 8.0.0
 711           *
 712           * @param array $where_args  Associative array of columns/values describing
 713           *                           opt-outs about to be deleted.
 714           * @param array $update_args Array of new values.
 715           */
 716          do_action( 'bp_optout_before_update', $where_args, $update_args );
 717  
 718          $retval = self::_update( $update['data'], $where['data'], $update['format'], $where['format'] );
 719  
 720          // Clear matching items from the cache.
 721          $cache_args           = $where_args;
 722          $cache_args['fields'] = 'ids';
 723          $maybe_cached_ids     = self::get( $cache_args );
 724          foreach ( $maybe_cached_ids as $invite_id ) {
 725              wp_cache_delete( $invite_id, 'bp_optouts' );
 726          }
 727  
 728          /**
 729           * Fires after an opt-out is updated.
 730           *
 731           * @since 8.0.0
 732           *
 733           * @param array $where_args  Associative array of columns/values describing
 734           *                           opt-outs about to be deleted.
 735           * @param array $update_args Array of new values.
 736           */
 737          do_action( 'bp_optout_after_update', $where_args, $update_args );
 738  
 739            return $retval;
 740      }
 741  
 742      /**
 743       * Delete opt-outs.
 744       *
 745       * @since 8.0.0
 746       *
 747       * @see BP_optout::get() for a description of
 748       *      accepted where arguments.
 749       *
 750       * @param array $args Associative array of columns/values, to determine
 751       *                    which rows should be deleted.  Of the format
 752       *                    array( 'user_id' => 7, 'email_address' => 'bar@foo.com', ).
 753       * @return int|bool Number of rows deleted on success, false on failure.
 754       */
 755  	public static function delete( $args = array() ) {
 756          $where = self::get_query_clauses( $args );
 757  
 758          /**
 759           * Fires before an opt-out is deleted.
 760           *
 761           * @since 8.0.0
 762           *
 763           * @param array $args Characteristics of the opt-outs to be deleted.
 764           */
 765          do_action( 'bp_optout_before_delete', $args );
 766  
 767          // Clear matching items from the cache.
 768          $cache_args           = $args;
 769          $cache_args['fields'] = 'ids';
 770          $maybe_cached_ids     = self::get( $cache_args );
 771          foreach ( $maybe_cached_ids as $invite_id ) {
 772              wp_cache_delete( $invite_id, 'bp_optouts' );
 773          }
 774  
 775          $retval = self::_delete( $where['data'], $where['format'] );
 776  
 777          /**
 778           * Fires after an opt-out is deleted.
 779           *
 780           * @since 8.0.0
 781           *
 782           * @param array $args Characteristics of the opt-outs just deleted.
 783           */
 784          do_action( 'bp_optout_after_delete', $args );
 785  
 786          return $retval;
 787      }
 788  
 789      /** Convenience methods ***********************************************/
 790  
 791      /**
 792       * Check whether an invitation exists matching the passed arguments.
 793       *
 794       * @since 5.0.0
 795       *
 796       * @see BP_Optout::get() for a description of accepted parameters.
 797       *
 798       * @return int|bool ID of first found invitation or false if none found.
 799       */
 800  	public function optout_exists( $args = array() ) {
 801          $exists = false;
 802  
 803          $args['fields'] = 'ids';
 804          $optouts        = BP_Optout::get( $args );
 805          if ( $optouts ) {
 806              $exists = current( $optouts );
 807          }
 808  
 809          return $exists;
 810      }
 811  
 812      /**
 813       * Delete a single opt-out by ID.
 814       *
 815       * @since 8.0.0
 816       *
 817       * @see BP_optout::delete() for explanation of
 818       *      return value.
 819       *
 820       * @param int $id ID of the opt-out item to be deleted.
 821       * @return bool True on success, false on failure.
 822       */
 823  	public static function delete_by_id( $id ) {
 824          return self::delete( array(
 825              'id' => $id,
 826          ) );
 827      }
 828  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1