Changeset 13888 for trunk/src/bp-core/classes/class-bp-invitation.php
- Timestamp:
- 06/01/2024 10:25:18 PM (11 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-core/classes/class-bp-invitation.php
r13395 r13888 25 25 * 26 26 * @since 5.0.0 27 * @access public27 * 28 28 * @var int 29 29 */ … … 34 34 * 35 35 * @since 5.0.0 36 * @access public36 * 37 37 * @var int 38 38 */ … … 43 43 * 44 44 * @since 5.0.0 45 * @access public45 * 46 46 * @var int 47 47 */ … … 53 53 * 54 54 * @since 5.0.0 55 * @access public55 * 56 56 * @var string 57 57 */ … … 62 62 * 63 63 * @since 5.0.0 64 * @access public64 * 65 65 * @var string 66 66 */ … … 72 72 * 73 73 * @since 5.0.0 74 * @access public74 * 75 75 * @var int 76 76 */ … … 82 82 * 83 83 * @since 5.0.0 84 * @access public85 * @var int 84 * 85 * @var int|null 86 86 */ 87 87 public $secondary_item_id = null; … … 91 91 * 92 92 * @since 5.0.0 93 * @access public93 * 94 94 * @var string 95 95 */ … … 100 100 * 101 101 * @since 5.0.0 102 * @access public102 * 103 103 * @var string 104 104 */ … … 109 109 * 110 110 * @since 5.0.0 111 * @access public111 * 112 112 * @var string 113 113 */ … … 118 118 * 119 119 * @since 5.0.0 120 * @access public120 * 121 121 * @var bool 122 122 */ … … 127 127 * 128 128 * @since 5.0.0 129 * @access public129 * 130 130 * @var bool 131 131 */ … … 136 136 * 137 137 * @since 9.0.0 138 * @access public138 * 139 139 * @var array 140 140 */ … … 151 151 'date_modified', 152 152 'invite_sent', 153 'accepted' 153 'accepted', 154 154 ); 155 155 … … 161 161 * @since 5.0.0 162 162 * 163 * @param int $id Optional. Provide an ID to access an existing 164 * invitation item. 163 * @param int $id Optional. Provide an ID to access an existing invitation item. 165 164 */ 166 165 public function __construct( $id = 0 ) { … … 182 181 public function save() { 183 182 184 // Return value 183 // Return value. 185 184 $retval = false; 186 185 187 // Default data and format 188 $data = array(186 // Default data and format. 187 $data = array( 189 188 'user_id' => $this->user_id, 190 189 'inviter_id' => $this->inviter_id, … … 213 212 if ( ! empty( $this->id ) ) { 214 213 $result = self::_update( $data, array( 'ID' => $this->id ), $data_format, array( '%d' ) ); 215 // Insert.214 // Insert. 216 215 } else { 217 216 $result = self::_insert( $data, $data_format ); … … 258 257 if ( false === $invitation ) { 259 258 $invitation = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$invites_table_name} WHERE id = %d", $this->id ) ); 260 wp_cache_set( $this->id, $invitation, 'bp_invitations' );259 wp_cache_set( $this->id, $invitation, 'bp_invitations' ); 261 260 } 262 261 … … 278 277 $this->invite_sent = (int) $invitation->invite_sent; 279 278 $this->accepted = (int) $invitation->accepted; 280 281 279 } 282 280 … … 287 285 * 288 286 * @since 5.0.0 287 * 288 * @global wpdb $wpdb WordPress database object. 289 289 * 290 290 * @param array $data { 291 291 * Array of invitation data, passed to {@link wpdb::insert()}. 292 * 293 * 294 * 295 * 296 * 297 * 292 * @type int $user_id ID of the invited user. 293 * @type int $inviter_id ID of the user who created the invitation. 294 * @type string $invitee_email Email address of the invited user. 295 * @type string $class Name of the related class. 296 * @type int $item_id ID associated with the invitation and component. 297 * @type int $secondary_item_id Secondary ID associated with the invitation and 298 298 * component. 299 * 300 * 301 * 302 * 299 * @type string $content Extra information provided by the requester 300 * or inviter. 301 * @type string $date_modified Date the invitation was last modified. 302 * @type int $invite_sent Has the invitation been sent, or is it a draft 303 303 * invite? 304 304 * } … … 315 315 * 316 316 * @since 5.0.0 317 * 318 * @global wpdb $wpdb WordPress database object. 317 319 * 318 320 * @see wpdb::update() for further description of paramater formats. … … 338 340 * @since 5.0.0 339 341 * 342 * @global wpdb $wpdb WordPress database object. 343 * 340 344 * @see wpdb::update() for further description of paramater formats. 341 345 * … … 359 363 * @since 5.0.0 360 364 * 365 * @global wpdb $wpdb WordPress database object. 366 * 361 367 * @param array $args See {@link BP_Invitation::get()} for more details. 362 368 * @return string WHERE clause. … … 370 376 // id. 371 377 if ( false !== $args['id'] ) { 372 $id_in = implode( ',', wp_parse_id_list( $args['id'] ) );378 $id_in = implode( ',', wp_parse_id_list( $args['id'] ) ); 373 379 $where_conditions['id'] = "id IN ({$id_in})"; 374 380 } … … 376 382 // user_id. 377 383 if ( ! empty( $args['user_id'] ) ) { 378 $user_id_in = implode( ',', wp_parse_id_list( $args['user_id'] ) );384 $user_id_in = implode( ',', wp_parse_id_list( $args['user_id'] ) ); 379 385 $where_conditions['user_id'] = "user_id IN ({$user_id_in})"; 380 386 } … … 382 388 // inviter_id. 0 can be meaningful, in the case of requests. 383 389 if ( ! empty( $args['inviter_id'] ) || 0 === $args['inviter_id'] ) { 384 $inviter_id_in = implode( ',', wp_parse_id_list( $args['inviter_id'] ) );390 $inviter_id_in = implode( ',', wp_parse_id_list( $args['inviter_id'] ) ); 385 391 $where_conditions['inviter_id'] = "inviter_id IN ({$inviter_id_in})"; 386 392 } … … 399 405 } 400 406 401 $invitee_email_in = implode( ',', $email_clean );407 $invitee_email_in = implode( ',', $email_clean ); 402 408 $where_conditions['invitee_email'] = "invitee_email IN ({$invitee_email_in})"; 403 409 } … … 416 422 } 417 423 418 $cn_in = implode( ',', $cn_clean );424 $cn_in = implode( ',', $cn_clean ); 419 425 $where_conditions['class'] = "class IN ({$cn_in})"; 420 426 } … … 422 428 // item_id. 423 429 if ( ! empty( $args['item_id'] ) ) { 424 $item_id_in = implode( ',', wp_parse_id_list( $args['item_id'] ) );430 $item_id_in = implode( ',', wp_parse_id_list( $args['item_id'] ) ); 425 431 $where_conditions['item_id'] = "item_id IN ({$item_id_in})"; 426 432 } … … 428 434 // secondary_item_id. 429 435 if ( ! empty( $args['secondary_item_id'] ) ) { 430 $secondary_item_id_in = implode( ',', wp_parse_id_list( $args['secondary_item_id'] ) );436 $secondary_item_id_in = implode( ',', wp_parse_id_list( $args['secondary_item_id'] ) ); 431 437 $where_conditions['secondary_item_id'] = "secondary_item_id IN ({$secondary_item_id_in})"; 432 438 } … … 434 440 // Type. 435 441 if ( ! empty( $args['type'] ) && 'all' !== $args['type'] ) { 436 if ( 'invite' == $args['type'] || 'request'== $args['type'] ) {437 $type_clean = $wpdb->prepare( '%s', $args['type'] );442 if ( 'invite' === $args['type'] || 'request' === $args['type'] ) { 443 $type_clean = $wpdb->prepare( '%s', $args['type'] ); 438 444 $where_conditions['type'] = "type = {$type_clean}"; 439 445 } … … 441 447 442 448 /** 443 * invite_sent444 449 * Only create a where statement if something less than "all" has been 445 450 * specifically requested. 446 451 */ 447 452 if ( ! empty( $args['invite_sent'] ) && 'all' !== $args['invite_sent'] ) { 448 if ( $args['invite_sent'] == 'draft' ) {449 $where_conditions['invite_sent'] = "invite_sent = 0";450 } else if ( $args['invite_sent']== 'sent' ) {451 $where_conditions['invite_sent'] = "invite_sent = 1";453 if ( $args['invite_sent'] === 'draft' ) { 454 $where_conditions['invite_sent'] = 'invite_sent = 0'; 455 } elseif ( $args['invite_sent'] === 'sent' ) { 456 $where_conditions['invite_sent'] = 'invite_sent = 1'; 452 457 } 453 458 } … … 455 460 // Accepted. 456 461 if ( ! empty( $args['accepted'] ) && 'all' !== $args['accepted'] ) { 457 if ( $args['accepted'] == 'pending' ) {458 $where_conditions['accepted'] = "accepted = 0";459 } else if ( $args['accepted']== 'accepted' ) {460 $where_conditions['accepted'] = "accepted = 1";462 if ( $args['accepted'] === 'pending' ) { 463 $where_conditions['accepted'] = 'accepted = 0'; 464 } elseif ( $args['accepted'] === 'accepted' ) { 465 $where_conditions['accepted'] = 'accepted = 1'; 461 466 } 462 467 } … … 464 469 // search_terms. 465 470 if ( ! empty( $args['search_terms'] ) ) { 466 $search_terms_like = '%' . bp_esc_like( $args['search_terms'] ) . '%';471 $search_terms_like = '%' . bp_esc_like( $args['search_terms'] ) . '%'; 467 472 $where_conditions['search_terms'] = $wpdb->prepare( '( invitee_email LIKE %s OR content LIKE %s )', $search_terms_like, $search_terms_like ); 468 473 } … … 539 544 // Custom LIMIT. 540 545 if ( ! empty( $args['page'] ) && ! empty( $args['per_page'] ) ) { 541 $page = absint( $args['page'] 546 $page = absint( $args['page'] ); 542 547 $per_page = absint( $args['per_page'] ); 543 548 $offset = $per_page * ( $page - 1 ); 544 $retval = $wpdb->prepare( "LIMIT %d, %d", $offset, $per_page );549 $retval = $wpdb->prepare( 'LIMIT %d, %d', $offset, $per_page ); 545 550 } 546 551 … … 597 602 if ( ! empty( $args['id'] ) ) { 598 603 $where_clauses['data']['id'] = absint( $args['id'] ); 599 $where_clauses['format'][] = '%d';604 $where_clauses['format'][] = '%d'; 600 605 } 601 606 … … 603 608 if ( ! empty( $args['user_id'] ) ) { 604 609 $where_clauses['data']['user_id'] = absint( $args['user_id'] ); 605 $where_clauses['format'][] = '%d';610 $where_clauses['format'][] = '%d'; 606 611 } 607 612 … … 609 614 if ( ! empty( $args['inviter_id'] ) ) { 610 615 $where_clauses['data']['inviter_id'] = absint( $args['inviter_id'] ); 611 $where_clauses['format'][] = '%d';616 $where_clauses['format'][] = '%d'; 612 617 } 613 618 … … 615 620 if ( ! empty( $args['invitee_email'] ) ) { 616 621 $where_clauses['data']['invitee_email'] = $args['invitee_email']; 617 $where_clauses['format'][] = '%s';622 $where_clauses['format'][] = '%s'; 618 623 } 619 624 … … 621 626 if ( ! empty( $args['class'] ) ) { 622 627 $where_clauses['data']['class'] = $args['class']; 623 $where_clauses['format'][] = '%s';628 $where_clauses['format'][] = '%s'; 624 629 } 625 630 … … 627 632 if ( ! empty( $args['item_id'] ) ) { 628 633 $where_clauses['data']['item_id'] = absint( $args['item_id'] ); 629 $where_clauses['format'][] = '%d';634 $where_clauses['format'][] = '%d'; 630 635 } 631 636 … … 633 638 if ( ! empty( $args['secondary_item_id'] ) ) { 634 639 $where_clauses['data']['secondary_item_id'] = absint( $args['secondary_item_id'] ); 635 $where_clauses['format'][] = '%d';640 $where_clauses['format'][] = '%d'; 636 641 } 637 642 638 643 // type. 639 644 if ( ! empty( $args['type'] ) && 'all' !== $args['type'] ) { 640 if ( 'invite' == $args['type'] || 'request'== $args['type'] ) {645 if ( 'invite' === $args['type'] || 'request' === $args['type'] ) { 641 646 $where_clauses['data']['type'] = $args['type']; 642 $where_clauses['format'][] = '%s';647 $where_clauses['format'][] = '%s'; 643 648 } 644 649 } 645 650 646 651 /** 647 * invite_sent648 652 * Only create a where statement if something less than "all" has been 649 653 * specifically requested. 650 654 */ 651 655 if ( isset( $args['invite_sent'] ) && 'all' !== $args['invite_sent'] ) { 652 if ( $args['invite_sent'] == 'draft' ) {656 if ( $args['invite_sent'] === 'draft' ) { 653 657 $where_clauses['data']['invite_sent'] = 0; 654 $where_clauses['format'][] = '%d';655 } else if ( $args['invite_sent']== 'sent' ) {658 $where_clauses['format'][] = '%d'; 659 } elseif ( $args['invite_sent'] === 'sent' ) { 656 660 $where_clauses['data']['invite_sent'] = 1; 657 $where_clauses['format'][] = '%d';661 $where_clauses['format'][] = '%d'; 658 662 } 659 663 } … … 661 665 // accepted. 662 666 if ( ! empty( $args['accepted'] ) && 'all' !== $args['accepted'] ) { 663 if ( $args['accepted'] == 'pending' ) {667 if ( $args['accepted'] === 'pending' ) { 664 668 $where_clauses['data']['accepted'] = 0; 665 $where_clauses['format'][] = '%d';666 } else if ( $args['accepted']== 'accepted' ) {669 $where_clauses['format'][] = '%d'; 670 } elseif ( $args['accepted'] === 'accepted' ) { 667 671 $where_clauses['data']['accepted'] = 1; 668 $where_clauses['format'][] = '%d';669 } 670 } 671 672 // date_modified 672 $where_clauses['format'][] = '%d'; 673 } 674 } 675 676 // date_modified. 673 677 if ( ! empty( $args['date_modified'] ) ) { 674 678 $where_clauses['data']['date_modified'] = $args['date_modified']; 675 $where_clauses['format'][] = '%s';679 $where_clauses['format'][] = '%s'; 676 680 } 677 681 … … 685 689 * 686 690 * @since 5.0.0 691 * 692 * @global wpdb $wpdb WordPress database object. 687 693 * 688 694 * @param array $args { … … 697 703 * invitation. Can be an array of IDs. 698 704 * @type string|array $invitee_email Email address of invited users 699 * 705 * being queried. Can be an array of 700 706 * addresses. 701 707 * @type string|array $class Name of the class to filter by. … … 735 741 * } 736 742 * 737 * @return arrayBP_Invitation objects | IDs of found invite.743 * @return int[]|BP_Invitation[] BP_Invitation objects | IDs of found invite. 738 744 */ 739 745 public static function get( $args = array() ) { … … 766 772 767 773 $sql = array( 768 'select' => "SELECT",774 'select' => 'SELECT', 769 775 'fields' => '', 770 776 'from' => "FROM {$invites_table_name} i", … … 775 781 776 782 if ( 'item_ids' === $r['fields'] ) { 777 $sql['fields'] = "DISTINCT i.item_id";778 } else 779 $sql['fields'] = "DISTINCT i.user_id";780 } else 781 $sql['fields'] = "DISTINCT i.inviter_id";783 $sql['fields'] = 'DISTINCT i.item_id'; 784 } elseif ( 'user_ids' === $r['fields'] ) { 785 $sql['fields'] = 'DISTINCT i.user_id'; 786 } elseif ( 'inviter_ids' === $r['fields'] ) { 787 $sql['fields'] = 'DISTINCT i.inviter_id'; 782 788 } else { 783 789 $sql['fields'] = 'DISTINCT i.id'; … … 785 791 786 792 // WHERE. 787 $sql['where'] = self::get_where_sql( array( 788 'id' => $r['id'], 789 'user_id' => $r['user_id'], 790 'inviter_id' => $r['inviter_id'], 791 'invitee_email' => $r['invitee_email'], 792 'class' => $r['class'], 793 'item_id' => $r['item_id'], 794 'secondary_item_id' => $r['secondary_item_id'], 795 'type' => $r['type'], 796 'invite_sent' => $r['invite_sent'], 797 'accepted' => $r['accepted'], 798 'search_terms' => $r['search_terms'], 799 ) ); 793 $sql['where'] = self::get_where_sql( 794 array( 795 'id' => $r['id'], 796 'user_id' => $r['user_id'], 797 'inviter_id' => $r['inviter_id'], 798 'invitee_email' => $r['invitee_email'], 799 'class' => $r['class'], 800 'item_id' => $r['item_id'], 801 'secondary_item_id' => $r['secondary_item_id'], 802 'type' => $r['type'], 803 'invite_sent' => $r['invite_sent'], 804 'accepted' => $r['accepted'], 805 'search_terms' => $r['search_terms'], 806 ) 807 ); 800 808 801 809 // ORDER BY. 802 $sql['orderby'] = self::get_order_by_sql( array( 803 'order_by' => $r['order_by'], 804 'sort_order' => $r['sort_order'] 805 ) ); 810 $sql['orderby'] = self::get_order_by_sql( 811 array( 812 'order_by' => $r['order_by'], 813 'sort_order' => $r['sort_order'], 814 ) 815 ); 806 816 807 817 // LIMIT %d, %d. 808 $sql['pagination'] = self::get_paged_sql( array( 809 'page' => $r['page'], 810 'per_page' => $r['per_page'], 811 ) ); 818 $sql['pagination'] = self::get_paged_sql( 819 array( 820 'page' => $r['page'], 821 'per_page' => $r['per_page'], 822 ) 823 ); 812 824 813 825 $paged_invites_sql = "{$sql['select']} {$sql['fields']} {$sql['from']} {$sql['where']} {$sql['orderby']} {$sql['pagination']}"; … … 840 852 $uncached_ids = bp_get_non_cached_ids( $paged_invite_ids, 'bp_invitations' ); 841 853 if ( $uncached_ids ) { 842 $ids_sql = implode( ',', array_map( 'intval', $uncached_ids ) );854 $ids_sql = implode( ',', array_map( 'intval', $uncached_ids ) ); 843 855 $data_objects = $wpdb->get_results( "SELECT i.* FROM {$invites_table_name} i WHERE i.id IN ({$ids_sql})" ); 844 856 foreach ( $data_objects as $data_object ) { … … 859 871 * 860 872 * @since 5.0.0 873 * 874 * @global wpdb $wpdb WordPress database object. 861 875 * 862 876 * @see BP_Invitation::get() for a description of … … 893 907 ); 894 908 895 // Build the query 896 $select_sql = "SELECT COUNT(*)";909 // Build the query. 910 $select_sql = 'SELECT COUNT(*)'; 897 911 $from_sql = "FROM {$invites_table_name}"; 898 912 $where_sql = self::get_where_sql( $r ); 899 913 $sql = "{$select_sql} {$from_sql} {$where_sql}"; 900 914 901 // Return the queried results 915 // Return the queried results. 902 916 return $wpdb->get_var( $sql ); 903 917 } … … 921 935 public static function update( $update_args = array(), $where_args = array() ) { 922 936 $update = self::get_query_clauses( $update_args ); 923 $where = self::get_query_clauses( $where_args 937 $where = self::get_query_clauses( $where_args ); 924 938 925 939 /** … … 937 951 938 952 // Clear matching items from the cache. 939 $cache_args = $where_args;953 $cache_args = $where_args; 940 954 $cache_args['fields'] = 'ids'; 941 $maybe_cached_ids = self::get( $cache_args );955 $maybe_cached_ids = self::get( $cache_args ); 942 956 foreach ( $maybe_cached_ids as $invite_id ) { 943 957 wp_cache_delete( $invite_id, 'bp_invitations' ); … … 984 998 985 999 // Clear matching items from the cache. 986 $cache_args = $args;1000 $cache_args = $args; 987 1001 $cache_args['fields'] = 'ids'; 988 $maybe_cached_ids = self::get( $cache_args );1002 $maybe_cached_ids = self::get( $cache_args ); 989 1003 foreach ( $maybe_cached_ids as $invite_id ) { 990 1004 wp_cache_delete( $invite_id, 'bp_invitations' ); … … 1016 1030 * 1017 1031 * @param int $id ID of the invitation item to be deleted. 1018 * @return bool Trueon success, false on failure.1032 * @return bool|int Number of rows deleted on success, false on failure. 1019 1033 */ 1020 1034 public static function delete_by_id( $id ) { 1021 return self::delete( array( 1022 'id' => $id, 1023 ) ); 1035 return self::delete( array( 'id' => $id ) ); 1024 1036 } 1025 1037 … … 1153 1165 return self::update( $update_args, $args ); 1154 1166 } 1155 1156 1167 }
Note: See TracChangeset
for help on using the changeset viewer.