Changeset 6842 for trunk/src/includes/core/abstraction.php
- Timestamp:
- 07/29/2018 06:05:55 PM (7 years ago)
- File:
-
- 1 edited
-
trunk/src/includes/core/abstraction.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/core/abstraction.php
r6816 r6842 458 458 : $filtered; 459 459 } 460 461 /** Engagements ***************************************************************/462 463 /**464 * Return the strategy used for storing user engagements465 *466 * @since 2.6.0 bbPress (r6722)467 *468 * @param string $rel_key The key used to index this relationship469 * @param string $rel_type The type of meta to look in470 *471 * @return string472 */473 function bbp_user_engagements_interface( $rel_key = '', $rel_type = 'post' ) {474 return apply_filters( 'bbp_user_engagements_interface', bbpress()->engagements, $rel_key, $rel_type );475 }476 477 /**478 * Meta strategy for interfacing with User Engagements479 *480 * @since 2.6.0 bbPress (r6722)481 */482 class BBP_User_Engagements_Base {483 484 /**485 *486 * @since 2.6.0 bbPress (r6737)487 *488 * @var string489 */490 public $type = '';491 492 /**493 * Add a user id to an object494 *495 * @since 2.6.0 bbPress (r6722)496 *497 * @param int $object_id The object id498 * @param int $user_id The user id499 * @param string $meta_key The relationship key500 * @param string $meta_type The relationship type (usually 'post')501 * @param bool $unique Whether meta key should be unique to the object502 *503 * @return bool Returns true on success, false on failure504 */505 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {506 507 }508 509 /**510 * Remove a user id from an object511 *512 * @since 2.6.0 bbPress (r6722)513 *514 * @param int $object_id The object id515 * @param int $user_id The user id516 * @param string $meta_key The relationship key517 * @param string $meta_type The relationship type (usually 'post')518 *519 * @return bool Returns true on success, false on failure520 */521 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {522 523 }524 525 /**526 * Remove a user id from all objects527 *528 * @since 2.6.0 bbPress (r6722)529 *530 * @param int $user_id The user id531 * @param string $meta_key The relationship key532 * @param string $meta_type The relationship type (usually 'post')533 *534 * @return bool Returns true on success, false on failure535 */536 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {537 538 }539 540 /**541 * Remove an object from all users542 *543 * @since 2.6.0 bbPress (r6722)544 *545 * @param int $object_id The object id546 * @param int $user_id The user id547 * @param string $meta_key The relationship key548 * @param string $meta_type The relationship type (usually 'post')549 *550 * @return bool Returns true on success, false on failure551 */552 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {553 554 }555 556 /**557 * Remove all users from all objects558 *559 * @since 2.6.0 bbPress (r6722)560 *561 * @param string $meta_key The relationship key562 * @param string $meta_type The relationship type (usually 'post')563 *564 * @return bool Returns true on success, false on failure565 */566 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {567 568 }569 570 /**571 * Get users of an object572 *573 * @since 2.6.0 bbPress (r6722)574 *575 * @param int $object_id The object id576 * @param string $meta_key The key used to index this relationship577 * @param string $meta_type The type of meta to look in578 *579 * @return array Returns ids of users580 */581 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {582 583 }584 585 /**586 * Get the part of the query responsible for JOINing objects to relationships.587 *588 * @since 2.6.0 bbPress (r6737)589 *590 * @param array $args591 * @param string $meta_key592 * @param string $meta_type593 *594 * @return array595 */596 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {597 598 }599 }600 601 /**602 * Meta strategy for interfacing with User Engagements603 *604 * @since 2.6.0 bbPress (r6722)605 */606 class BBP_User_Engagements_Meta extends BBP_User_Engagements_Base {607 608 /**609 *610 * @since 2.6.0 bbPress (r6737)611 *612 * @var string613 */614 public $type = 'meta';615 616 /**617 * Add a user id to an object618 *619 * @since 2.6.0 bbPress (r6722)620 *621 * @param int $object_id The object id622 * @param int $user_id The user id623 * @param string $meta_key The relationship key624 * @param string $meta_type The relationship type (usually 'post')625 * @param bool $unique Whether meta key should be unique to the object626 *627 * @return bool Returns true on success, false on failure628 */629 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {630 return add_metadata( $meta_type, $object_id, $meta_key, $user_id, $unique );631 }632 633 /**634 * Remove a user id from an object635 *636 * @since 2.6.0 bbPress (r6722)637 *638 * @param int $object_id The object id639 * @param int $user_id The user id640 * @param string $meta_key The relationship key641 * @param string $meta_type The relationship type (usually 'post')642 *643 * @return bool Returns true on success, false on failure644 */645 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {646 return delete_metadata( $meta_type, $object_id, $meta_key, $user_id, false );647 }648 649 /**650 * Remove a user id from all objects651 *652 * @since 2.6.0 bbPress (r6722)653 *654 * @param int $user_id The user id655 * @param string $meta_key The relationship key656 * @param string $meta_type The relationship type (usually 'post')657 *658 * @return bool Returns true on success, false on failure659 */660 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {661 return delete_metadata( $meta_type, null, $meta_key, $user_id, true );662 }663 664 /**665 * Remove an object from all users666 *667 * @since 2.6.0 bbPress (r6722)668 *669 * @param int $object_id The object id670 * @param int $user_id The user id671 * @param string $meta_key The relationship key672 * @param string $meta_type The relationship type (usually 'post')673 *674 * @return bool Returns true on success, false on failure675 */676 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {677 return delete_metadata( $meta_type, $object_id, $meta_key, null, false );678 }679 680 /**681 * Remove all users from all objects682 *683 * @since 2.6.0 bbPress (r6722)684 *685 * @param string $meta_key The relationship key686 * @param string $meta_type The relationship type (usually 'post')687 *688 * @return bool Returns true on success, false on failure689 */690 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {691 return delete_metadata( $meta_type, null, $meta_key, null, true );692 }693 694 /**695 * Get users of an object696 *697 * @since 2.6.0 bbPress (r6722)698 *699 * @param int $object_id The object id700 * @param string $meta_key The key used to index this relationship701 * @param string $meta_type The type of meta to look in702 *703 * @return array Returns ids of users704 */705 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {706 return wp_parse_id_list( get_metadata( $meta_type, $object_id, $meta_key, false ) );707 }708 709 /**710 * Get the part of the query responsible for JOINing objects to relationships.711 *712 * @since 2.6.0 bbPress (r6737)713 *714 * @param array $args715 * @param string $meta_key716 * @param string $meta_type717 *718 * @return array719 */720 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {721 722 // Backwards compat for pre-2.6.0723 if ( is_numeric( $args ) ) {724 $args = array(725 'meta_query' => array( array(726 'key' => $meta_key,727 'value' => bbp_get_user_id( $args, false, false ),728 'compare' => 'NUMERIC'729 ) )730 );731 }732 733 // Default arguments734 $defaults = array(735 'meta_query' => array( array(736 'key' => $meta_key,737 'value' => bbp_get_displayed_user_id(),738 'compare' => 'NUMERIC'739 ) )740 );741 742 // Parse arguments743 return bbp_parse_args( $args, $defaults, $context_key );744 }745 }746 747 /**748 * Term strategy for interfacing with User Engagements749 *750 * @since 2.6.0 bbPress (r6737)751 */752 class BBP_User_Engagements_Term extends BBP_User_Engagements_Base {753 754 /**755 * Term type756 *757 * @since 2.6.0 bbPress (r6737)758 *759 * @var string760 */761 public $type = 'term';762 763 /**764 * Register an engagement taxonomy just-in-time765 *766 * @since 2.6.0 bbPress (r6737)767 *768 * @param string $tax_key769 * @param string $object_type770 */771 private function jit_taxonomy( $tax_key = '', $object_type = 'user' ) {772 773 // Bail if taxonomy already exists774 if ( taxonomy_exists( $tax_key ) ) {775 return;776 }777 778 // Register the taxonomy779 register_taxonomy( $tax_key, 'bbp_' . $object_type, array(780 'labels' => array(),781 'description' => '',782 'public' => false,783 'publicly_queryable' => false,784 'hierarchical' => false,785 'show_ui' => false,786 'show_in_menu' => false,787 'show_in_nav_menus' => false,788 'show_tagcloud' => false,789 'show_in_quick_edit' => false,790 'show_admin_column' => false,791 'meta_box_cb' => false,792 'capabilities' => array(),793 'rewrite' => false,794 'query_var' => '',795 'update_count_callback' => '',796 'show_in_rest' => false,797 'rest_base' => false,798 'rest_controller_class' => false,799 '_builtin' => false800 ) );801 }802 803 /**804 * Add a user id to an object805 *806 * @since 2.6.0 bbPress (r6737)807 *808 * @param int $object_id The object id809 * @param int $user_id The user id810 * @param string $meta_key The relationship key811 * @param string $meta_type The relationship type (usually 'post')812 * @param bool $unique Whether meta key should be unique to the object813 *814 * @return bool Returns true on success, false on failure815 */816 public function add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {817 $user_key = "{$meta_key}_user_id_{$user_id}";818 $tax_key = "{$meta_key}_{$meta_type}";819 $this->jit_taxonomy( $tax_key );820 821 return wp_add_object_terms( $object_id, $user_key, $tax_key );822 }823 824 /**825 * Remove a user id from an object826 *827 * @since 2.6.0 bbPress (r6737)828 *829 * @param int $object_id The object id830 * @param int $user_id The user id831 * @param string $meta_key The relationship key832 * @param string $meta_type The relationship type (usually 'post')833 *834 * @return bool Returns true on success, false on failure835 */836 public function remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) {837 $user_key = "{$meta_key}_user_id_{$user_id}";838 $tax_key = "{$meta_key}_{$meta_type}";839 $this->jit_taxonomy( $tax_key );840 841 return wp_remove_object_terms( $object_id, $user_key, $tax_key );842 }843 844 /**845 * Remove a user id from all objects846 *847 * @since 2.6.0 bbPress (r6737)848 *849 * @param int $user_id The user id850 * @param string $meta_key The relationship key851 * @param string $meta_type The relationship type (usually 'post')852 *853 * @return bool Returns true on success, false on failure854 */855 public function remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {856 $user_key = "{$meta_key}_user_id_{$user_id}";857 $tax_key = "{$meta_key}_{$meta_type}";858 $this->jit_taxonomy( $tax_key );859 $term = get_term_by( 'slug', $user_key, $tax_key );860 861 return wp_delete_term( $term->term_id, $tax_key );862 }863 864 /**865 * Remove an object from all users866 *867 * @since 2.6.0 bbPress (r6737)868 *869 * @param int $object_id The object id870 * @param int $user_id The user id871 * @param string $meta_key The relationship key872 * @param string $meta_type The relationship type (usually 'post')873 *874 * @return bool Returns true on success, false on failure875 */876 public function remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {877 return wp_delete_object_term_relationships( $object_id, get_object_taxonomies( 'bbp_user' ) );878 }879 880 /**881 * Remove all users from all objects882 *883 * @since 2.6.0 bbPress (r6737)884 *885 * @param string $meta_key The relationship key886 * @param string $meta_type The relationship type (usually 'post')887 *888 * @return bool Returns true on success, false on failure889 */890 public function remove_all_users_from_all_objects( $meta_key = '', $meta_type = 'post' ) {891 // TODO892 }893 894 /**895 * Get users of an object896 *897 * @since 2.6.0 bbPress (r6737)898 *899 * @param int $object_id The object id900 * @param string $meta_key The key used to index this relationship901 * @param string $meta_type The type of meta to look in902 *903 * @return array Returns ids of users904 */905 public function get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {906 $user_key = "{$meta_key}_user_id_";907 $tax_key = "{$meta_key}_{$meta_type}";908 $this->jit_taxonomy( $tax_key );909 910 // Get terms911 $terms = get_terms( array(912 'object_ids' => $object_id,913 'taxonomy' => $tax_key914 ) );915 916 // Slug part to replace917 $user_ids = array();918 919 // Loop through terms and get the user ID920 foreach ( $terms as $term ) {921 $user_ids[] = str_replace( $user_key, '', $term->slug );922 }923 924 // Parse & return925 return wp_parse_id_list( $user_ids );926 }927 928 /**929 * Get the part of the query responsible for JOINing objects to relationships.930 *931 * @since 2.6.0 bbPress (r6737)932 *933 * @param array $args934 * @param string $meta_key935 * @param string $meta_type936 *937 * @return array938 */939 public function get_query( $args = array(), $context_key = '', $meta_key = '', $meta_type = 'post' ) {940 $tax_key = "{$meta_key}_{$meta_type}";941 $user_key = "{$meta_key}_user_id_";942 943 // Make sure the taxonomy is registered944 $this->jit_taxonomy( $tax_key );945 946 // Backwards compat for pre-2.6.0947 if ( is_numeric( $args ) ) {948 $args = array(949 'tax_query' => array( array(950 'taxonomy' => $tax_key,951 'terms' => $user_key . bbp_get_user_id( $args, false, false ),952 'field' => 'slug'953 ) )954 );955 }956 957 // Default arguments958 $defaults = array(959 'tax_query' => array( array(960 'taxonomy' => $tax_key,961 'terms' => $user_key . bbp_get_displayed_user_id(),962 'field' => 'slug'963 ) )964 );965 966 // Parse arguments967 return bbp_parse_args( $args, $defaults, $context_key );968 }969 }
Note: See TracChangeset
for help on using the changeset viewer.