Changeset 6844
- Timestamp:
- 07/29/2018 07:13:34 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/includes/common/engagements.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/common/engagements.php
r6843 r6844 523 523 } 524 524 } 525 526 /** 527 * Backwards compatibility strategy for interfacing with User Engagements 528 * 529 * @since 2.6.0 bbPress (r6844) 530 */ 531 class BBP_User_Engagements_Back_Compat extends BBP_User_Engagements_Base { 532 533 /** 534 * Type of strategy being used. 535 * 536 * @since 2.6.0 bbPress (r6844) 537 * 538 * @var string 539 */ 540 public $type = 'compat'; 541 542 /** 543 * Private function to map 2.6 meta keys to 2.5 user-option keys. 544 * 545 * @since 2.6.0 bbPress (r6844) 546 * 547 * @param string $meta_key 548 * 549 * @return string 550 */ 551 private function get_user_option_key( $meta_key = '', $object_id = 0 ) { 552 switch ( $meta_key ) { 553 554 // Favorites 555 case '_bbp_favorite' : 556 $key = '_bbp_favorites'; 557 break; 558 559 // Subscriptions 560 case '_bbp_subscription' : 561 562 // Maybe guess at post type 563 $post_type = ! empty( $object_id ) 564 ? get_post_type( $object_id ) 565 : bbp_get_topic_post_type(); 566 567 // Forums & Topics used different keys :/ 568 $key = ( bbp_get_forum_post_type() === $post_type ) 569 ? '_bbp_forum_subscriptions' 570 : '_bbp_subscriptions'; 571 572 break; 573 574 // Unknown, so pluralize 575 default : 576 $key = "{$meta_key}s"; 577 break; 578 } 579 580 // Return the old (pluralized) user option key 581 return $key; 582 } 583 584 /** 585 * Turn a comma-separated string into an array of integers 586 * 587 * @since 2.6.0 bbPress (r6844) 588 * 589 * @param string $results 590 * @return array 591 */ 592 private function format_results( $results = '' ) { 593 return wp_parse_id_list( array_filter( $results ) ); 594 } 595 596 /** 597 * Add a user id to an object 598 * 599 * @since 2.6.0 bbPress (r6844) 600 * 601 * @param int $object_id The object id 602 * @param int $user_id The user id 603 * @param string $meta_key The relationship key 604 * @param string $meta_type The relationship type (usually 'post') 605 * @param bool $unique Whether meta key should be unique to the object 606 * 607 * @return bool Returns true on success, false on failure 608 */ 609 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) { 610 $retval = false; 611 $option_key = $this->get_user_option_key( $meta_key, $object_id ); 612 $object_ids = $this->format_results( get_user_option( $option_key, $user_id ) ); 613 614 // Not already added, so add it 615 if ( ! in_array( $object_id, $object_ids, true ) ) { 616 $object_ids[] = $object_id; 617 $object_ids = implode( ',', $this->format_results( $object_ids ) ); 618 $retval = update_user_option( $user_id, $option_key, $object_ids ); 619 } 620 621 // Return true if added, or false if not 622 return $retval; 623 } 624 625 /** 626 * Remove a user id from an object 627 * 628 * @since 2.6.0 bbPress (r6844) 629 * 630 * @param int $object_id The object id 631 * @param int $user_id The user id 632 * @param string $meta_key The relationship key 633 * @param string $meta_type The relationship type (usually 'post') 634 * 635 * @return bool Returns true on success, false on failure 636 */ 637 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) { 638 $retval = false; 639 $option_key = $this->get_user_option_key( $meta_key, $object_id ); 640 $object_ids = $this->format_results( get_user_option( $option_key, $user_id ) ); 641 642 // Exists, so remove it 643 if ( in_array( $object_id, $object_ids, true ) ) { 644 unset( $object_ids[ $object_id ] ); 645 646 $object_ids = implode( ',', $this->format_results( $object_ids ) ); 647 $retval = update_user_option( $user_id, $option_key, $object_ids ); 648 } 649 650 // Return true if removed, or false if not 651 return $retval; 652 } 653 654 /** 655 * Remove a user id from all objects 656 * 657 * @since 2.6.0 bbPress (r6844) 658 * 659 * @param int $user_id The user id 660 * @param string $meta_key The relationship key 661 * @param string $meta_type The relationship type (usually 'post') 662 * 663 * @return bool Returns true on success, false on failure 664 */ 665 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) { 666 $option_key = $this->get_user_option_key( $meta_key ); 667 return delete_user_option( $user_id, $option_key ); 668 } 669 670 /** 671 * Remove an object from all users 672 * 673 * @since 2.6.0 bbPress (r6844) 674 * 675 * @param int $object_id The object id 676 * @param int $user_id The user id 677 * @param string $meta_key The relationship key 678 * @param string $meta_type The relationship type (usually 'post') 679 * 680 * @return bool Returns true on success, false on failure 681 */ 682 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { 683 684 // Query for users 685 $user_ids = $this->get_users_for_object( $object_id, $meta_key, $meta_type ); 686 $u_count = count( $user_ids ); 687 688 // Count number of removals 689 $removed = array(); 690 $r_count = 0; 691 692 // Users have engaged, so remove them 693 if ( ! empty( $u_count ) ) { 694 695 // Loop through users and remove them from the object 696 foreach ( $user_ids as $user_id ) { 697 $removed[] = $this->remove_user_from_object( $object_id, $user_id, $meta_key, $meta_type ); 698 } 699 700 // Count the removed users 701 $r_count = count( $removed ); 702 } 703 704 // Return true if successfully removed from all users 705 return ( $r_count === $u_count ); 706 } 707 708 /** 709 * Remove all users from all objects 710 * 711 * @since 2.6.0 bbPress (r6844) 712 * 713 * @param string $meta_key The relationship key 714 * @param string $meta_type The relationship type (usually 'post') 715 * 716 * @return bool Returns true on success, false on failure 717 */ 718 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) { 719 720 // Query for users 721 $option_key = $this->get_user_option_key( $meta_key ); 722 $bbp_db = bbp_db(); 723 $user_ids = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}'" ); 724 $u_count = count( $user_ids ); 725 726 // Count number of removals 727 $removed = array(); 728 $r_count = 0; 729 730 // Users have engaged, so remove them 731 if ( ! empty( $u_count ) ) { 732 733 // Loop through users and remove their user options 734 foreach ( $user_ids as $user_id ) { 735 $removed[] = delete_user_option( $user_id, $option_key ); 736 } 737 738 // Count the removed users 739 $r_count = count( $removed ); 740 } 741 742 // Return true if successfully removed from all users 743 return ( $r_count === $u_count ); 744 } 745 746 /** 747 * Get users of an object 748 * 749 * The database queries in this function were cached in bbPress versions 750 * older than 2.6, but no longer are to avoid cache pollution. 751 * 752 * @since 2.6.0 bbPress (r6844) 753 * 754 * @param int $object_id The object id 755 * @param string $meta_key The key used to index this relationship 756 * @param string $meta_type The type of meta to look in 757 * 758 * @return array Returns ids of users 759 */ 760 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { 761 $option_key = $this->get_user_option_key( $meta_key, $object_id ); 762 $bbp_db = bbp_db(); 763 $user_ids = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$option_key}' and FIND_IN_SET('{$object_id}', meta_value) > 0" ); 764 765 return wp_parse_id_list( $user_ids ); 766 } 767 768 /** 769 * Get the part of the query responsible for JOINing objects to relationships. 770 * 771 * @since 2.6.0 bbPress (r6844) 772 * 773 * @param array $args 774 * @param string $meta_key 775 * @param string $meta_type 776 * 777 * @return array 778 */ 779 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) { 780 // TODO 781 return array(); 782 } 783 }
Note: See TracChangeset
for help on using the changeset viewer.