Skip to:
Content

BuddyPress.org

Ticket #6784: 6784.diff

File 6784.diff, 22.1 KB (added by Mamaduka, 9 years ago)
  • src/bp-core/bp-core-taxonomy.php

    diff --git src/bp-core/bp-core-taxonomy.php src/bp-core/bp-core-taxonomy.php
    index 5fd50cf..dd76658 100644
    function bp_register_default_taxonomies() { 
    2525                'public' => false,
    2626        ) );
    2727
     28        // Group Type.
     29        register_taxonomy( 'bp_group_type', 'bp_group', array(
     30                'public' => false,
     31        ) );
     32
    2833        // Email type.
    2934        register_taxonomy(
    3035                bp_get_email_tax_type(),
  • src/bp-groups/bp-groups-cache.php

    diff --git src/bp-groups/bp-groups-cache.php src/bp-groups/bp-groups-cache.php
    index 3d09916..0f61c05 100644
    function groups_clear_group_administrator_cache_on_member_save( BP_Groups_Member 
    212212}
    213213add_action( 'groups_member_after_save', 'groups_clear_group_administrator_cache_on_member_save' );
    214214
     215/**
     216 * Clear the group type cache for group.
     217 *
     218 * Called when group is deleted.
     219 *
     220 * @since 2.5.0
     221 *
     222 * @param int $group_id The group ID.
     223 */
     224function groups_clear_group_type_cache( $group_id = 0 ) {
     225        wp_cache_delete( $group_id, 'bp_groups_group_type' );
     226}
     227add_action( 'groups_delete_group', 'groups_clear_group_type_cache' );
     228
    215229/* List actions to clear super cached pages on, if super cache is installed */
    216230add_action( 'groups_join_group',                 'bp_core_clear_cache' );
    217231add_action( 'groups_leave_group',                'bp_core_clear_cache' );
  • src/bp-groups/bp-groups-functions.php

    diff --git src/bp-groups/bp-groups-functions.php src/bp-groups/bp-groups-functions.php
    index 2e77c3e..8b49016 100644
    function groups_remove_data_for_user( $user_id ) { 
    18491849add_action( 'wpmu_delete_user',  'groups_remove_data_for_user' );
    18501850add_action( 'delete_user',       'groups_remove_data_for_user' );
    18511851add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' );
     1852
     1853/** Group Types *************************************************************/
     1854
     1855/**
     1856 * Register a group type.
     1857 *
     1858 * @since 2.5.0
     1859 *
     1860 * @param string $group_type Unique string identifier for the group type.
     1861 * @param array  $args {
     1862 *     Array of arguments describing the group type.
     1863 *
     1864 *     @type array       $labels {
     1865 *         Array of labels to use in various parts of the interface.
     1866 *
     1867 *         @type string $name          Default name. Should typically be plural.
     1868 *         @type string $singular_name Singular name.
     1869 *     }
     1870 *     @type bool|string $has_directory Whether the group type should have its own type-specific directory.
     1871 *                                      Pass `true` to use the `$group_type` string as the type's slug.
     1872 *                                      Pass a string to customize the slug. Pass `false` to disable.
     1873 *                                      Default: true.
     1874 * }
     1875 * @return object|WP_Error Group type object on success, WP_Error object on failure.
     1876 */
     1877function bp_groups_register_group_type( $group_type, $args = array() ) {
     1878        $bp = buddypress();
     1879
     1880        if ( isset( $bp->groups->types[ $group_type ] ) ) {
     1881                return new WP_Error( 'bp_group_type_exists', __( 'Group type already exists.', 'buddypress' ), $group_type );
     1882        }
     1883
     1884        $r = bp_parse_args( $args, array(
     1885                'labels'        => array(),
     1886                'has_directory' => true,
     1887        ), 'register_group_type' );
     1888
     1889        $group_type = sanitize_key( $group_type );
     1890
     1891        /**
     1892         * Filters the list of illegal group type names.
     1893         *
     1894         * - 'any' is a special pseudo-type, representing items unassociated with any group type.
     1895         * - 'null' is a special pseudo-type, representing users without any type.
     1896         * - '_none' is used internally to denote an item that should not apply to any group types.
     1897         *
     1898         * @since 2.5.0
     1899         *
     1900         * @param array $illegal_names Array of illegal names.
     1901         */
     1902        $illegal_names = apply_filters( 'bp_group_type_illegal_names', array( 'any', 'null', '_none' ) );
     1903        if ( in_array( $group_type, $illegal_names, true ) ) {
     1904                return new WP_Error( 'bp_group_type_illegal_name', __( 'You may not register a group type with this name.', 'buddypress' ), $group_type );
     1905        }
     1906
     1907        // Store the group type name as data in the object (not just as the array key).
     1908        $r['name'] = $group_type;
     1909
     1910        // Make sure the relevant labels have been filled in.
     1911        $default_name = isset( $r['labels']['name'] ) ? $r['labels']['name'] : ucfirst( $r['name'] );
     1912        $r['labels'] = array_merge( array(
     1913                'name'          => $default_name,
     1914                'singular_name' => $default_name,
     1915        ), $r['labels'] );
     1916
     1917        if ( $r['has_directory'] ) {
     1918                if ( is_string( $r['has_directory'] ) ) {
     1919                        $directory_slug = $r['has_directory'];
     1920                } else {
     1921                        $directory_slug = $group_type;
     1922                }
     1923
     1924                // Sanitize for use in URls.
     1925                $r['directory_slug'] = sanitize_title( $directory_slug );
     1926                $r['has_directory']  = true;
     1927        } else {
     1928                $r['directory_slug'] = '';
     1929                $r['has_directory']  = false;
     1930        }
     1931
     1932        $bp->groups->types[ $group_type ] = $type = (object) $r;
     1933
     1934        /**
     1935         * Fires after a group type is registered.
     1936         *
     1937         * @since 2.5.0
     1938         *
     1939         * @param  string $group_type Group type indentifier.
     1940         * @param  object $$type      Group type object.
     1941         */
     1942        do_action( 'bp_groups_register_group_type', $group_type, $type );
     1943
     1944        return $type;
     1945}
     1946
     1947/**
     1948 * Get a list of all registered group type objects.
     1949 *
     1950 * @since 2.5.0
     1951 *
     1952 * @see bp_groups_register_group_type() for accepted arguments.
     1953 *
     1954 * @param array|string $args     Optional. An array of key => value arguments to match against
     1955 *                               the group type objects. Default empty array.
     1956 * @param string       $output   Optional. The type of output to return. Accepts 'names'
     1957 *                               or 'objects'. Default 'names'.
     1958 * @param string       $operator Optional. The logical operation to perform. 'or' means only one
     1959 *                               element from the array needs to match; 'and' means all elements
     1960 *                               must match. Accepts 'or' or 'and'. Default 'and'.
     1961 * @return array       $types    A list of groups type names or objects.
     1962 */
     1963function bp_groups_get_group_types( $args = array(), $output = 'names', $operator = 'and' ) {
     1964        $types = buddypress()->groups->types;
     1965
     1966        $type = wp_filter_object_list( $types, $args, $operator );
     1967
     1968        /**
     1969         * Filters the array of group type objects.
     1970         *
     1971         * This filter is run before the $output filter has been applied, so that
     1972         * filtering functions have access to the entire group type objects.
     1973         *
     1974         * @since 2.5.0
     1975         *
     1976         * @param array  $types     group type objects, keyed by name.
     1977         * @param array  $args      Array of key=>value arguments for filtering.
     1978         * @param string $operator  'or' to match any of $args, 'and' to require all.
     1979         */
     1980        $types = apply_filters( 'bp_groups_get_group_types', $types, $args, $operator );
     1981
     1982        if ( 'names' === $output ) {
     1983                $types = wp_list_pluck( $types, 'name' );
     1984        }
     1985
     1986        return $types;
     1987}
     1988
     1989/**
     1990 * Retrieve a group type object by name.
     1991 *
     1992 * @since 2.5.0
     1993 *
     1994 * @param string $group_type The name of the group type.
     1995 * @return object A group type object.
     1996 */
     1997function bp_groups_get_group_type_object( $group_type ) {
     1998        $types = bp_groups_get_group_types( array(), 'objects' );
     1999
     2000        if ( empty( $types[ $group_type ] ) ) {
     2001                return null;
     2002        }
     2003
     2004        return $types[ $group_type ];
     2005}
     2006
     2007/**
     2008 * Set type for a group.
     2009 *
     2010 * @since 2.5.0
     2011 *
     2012 * @param int    $group      ID of the group.
     2013 * @param string $group_type Group type.
     2014 * @param bool   $append     Optional. True to append this to existing types for group,
     2015 *                            false to replace. Default: false.
     2016 * @return array $retval See {@see bp_set_object_terms()}.
     2017 */
     2018function bp_groups_set_group_type( $group_id, $group_type, $append = false ) {
     2019        // Pass an empty group type to remove group's type.
     2020        if ( ! empty( $group_type ) && ! bp_groups_get_group_type_object( $group_type ) ) {
     2021                return false;
     2022        }
     2023
     2024        $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append );
     2025
     2026        // Bust the cache if the type has been updated.
     2027        if ( ! is_wp_error( $retval ) ) {
     2028                wp_cache_delete( $group_id, 'bp_groups_group_type' );
     2029
     2030                /**
     2031                 * Fires just after a group type has been changed.
     2032                 *
     2033                 * @since 2.2.0
     2034                 *
     2035                 * @param int    $group_id   ID of the group whose group type has been updated.
     2036                 * @param string $group_type Group type.
     2037                 * @param bool   $append     Whether the type is being appended to existing types.
     2038                 */
     2039                do_action( 'bp_groups_set_group_type', $group_id, $group_type, $append );
     2040        }
     2041
     2042        return $retval;
     2043}
     2044
     2045/**
     2046 * Get type for a group.
     2047 *
     2048 * @since 2.5.0
     2049 *
     2050 * @param int  $group_id ID of the group.
     2051 * @param bool $single   Optional. Whether to return a single type string. If multiple types are found
     2052 *                       for the group, the oldest one will be returned. Default: true.
     2053 * @return string|array|bool On success, returns a single group type (if $single is true) or an array of group
     2054 *                           types (if $single is false). Returns false on failure.
     2055 */
     2056function bp_groups_get_group_type( $group_id, $single = true ) {
     2057        $types = wp_cache_get( $group_id, 'bp_groups_group_type' );
     2058
     2059        if ( false === $types ) {
     2060                $types = bp_get_object_terms( $group_id, 'bp_group_type' );
     2061
     2062                if ( ! is_wp_error( $types ) ) {
     2063                        $types = wp_list_pluck( $types, 'name' );
     2064                        wp_cache_set( $group_id, $types, 'bp_groups_group_type' );
     2065                }
     2066        }
     2067
     2068        $type = false;
     2069        if ( ! empty( $types ) ) {
     2070                if ( $single ) {
     2071                        $type = end( $types );
     2072                } else {
     2073                        $type = $types;
     2074                }
     2075        }
     2076
     2077        /**
     2078         * Filter's a groups's group type(s).
     2079         *
     2080         * @since 2.5.0
     2081         *
     2082         * @param string $type     Group type.
     2083         * @param int    $group_id ID of the group.
     2084         * @param bool   $single   Whether to return a single type srting, or an array.
     2085         */
     2086        return apply_filters( 'bp_groups_get_group_type', $type, $group_id, $single );
     2087}
     2088
     2089/**
     2090 * Remove type for a group.
     2091 *
     2092 * @since 2.5.0
     2093 *
     2094 * @param int            $group      ID of the user.
     2095 * @param string         $group_type Group type.
     2096 * @return bool|WP_Error $deleted    True on success. False or WP_Error on failure.
     2097 */
     2098function bp_groups_remove_group_type( $group_id, $group_type ) {
     2099        if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) {
     2100                return false;
     2101        }
     2102
     2103        $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' );
     2104
     2105        // Bust the case, if the type has been removed.
     2106        if ( ! is_wp_error( $deleted ) ) {
     2107                wp_cache_delete( $group_id, 'bp_groups_group_type' );
     2108
     2109                /**
     2110                 * Fire just after a group's group type has been removed.
     2111                 *
     2112                 * @since 2.5.0
     2113                 *
     2114                 * @param int    $group      ID of the group whose group type has been removed.
     2115                 * @param string $group_type Group type.
     2116                 */
     2117                do_action( 'bp_groups_remove_group_type', $group_id, $group_type );
     2118        }
     2119
     2120        return $deleted;
     2121}
     2122
     2123/**
     2124 * Check whether the given group has a certain group type.
     2125 *
     2126 * @param  int    $group_id   ID of the group.
     2127 * @param  srting $group_type Group type.
     2128 * @return bool   Whether the group has the give group type.
     2129 */
     2130function bp_groups_has_group_type( $group_id, $group_type ) {
     2131        if ( empty( $group_type ) || ! bp_groups_get_group_type_object( $group_type ) ) {
     2132                return false;
     2133        }
     2134
     2135        // Get all group's group types.
     2136        $types = bp_groups_get_group_type( $group_id, false );
     2137
     2138        if ( ! is_array( $types ) ) {
     2139                return false;
     2140        }
     2141
     2142        return in_array( $group_type, $types );
     2143}
     2144
     2145/**
     2146 * Delete a group's type when the group is deleted.
     2147 *
     2148 * @param  int   $group_id ID of the group.
     2149 * @return array $value    See {@see bp_groups_set_group_type()}.
     2150 */
     2151function bp_remove_group_type_on_group_delete( $group_id = 0 ) {
     2152        bp_groups_set_group_type( $group_id, '' );
     2153}
     2154add_action( 'groups_delete_group', 'bp_remove_group_type_on_group_delete' );
     2155 No newline at end of file
  • src/bp-groups/classes/class-bp-groups-group.php

    diff --git src/bp-groups/classes/class-bp-groups-group.php src/bp-groups/classes/class-bp-groups-group.php
    index 224caf8..b6e5fcc 100644
    class BP_Groups_Group { 
    686686         *
    687687         * @param array $args {
    688688         *     Array of parameters. All items are optional.
    689          *     @type string       $type              Optional. Shorthand for certain orderby/
    690          *                                           order combinations. 'newest', 'active', 'popular',
    691          *                                           'alphabetical', 'random'. When present, will override
    692          *                                           orderby and order params. Default: null.
    693          *     @type string       $orderby           Optional. Property to sort by.
    694          *                                           'date_created', 'last_activity', 'total_member_count',
    695          *                                           'name', 'random'. Default: 'date_created'.
    696          *     @type string       $order             Optional. Sort order. 'ASC' or 'DESC'.
    697          *                                           Default: 'DESC'.
    698          *     @type int          $per_page          Optional. Number of items to return per page
    699          *                                           of results. Default: null (no limit).
    700          *     @type int          $page              Optional. Page offset of results to return.
    701          *                                           Default: null (no limit).
    702          *     @type int          $user_id           Optional. If provided, results will be limited to groups
    703          *                                           of which the specified user is a member. Default: null.
    704          *     @type string       $search_terms      Optional. If provided, only groups whose names
    705          *                                           or descriptions match the search terms will be
    706          *                                           returned. Default: false.
    707          *     @type array        $meta_query        Optional. An array of meta_query conditions.
    708          *                                           See {@link WP_Meta_Query::queries} for description.
    709          *     @type array|string $value             Optional. Array or comma-separated list of group IDs.
    710          *                                           Results will be limited to groups within the
    711          *                                           list. Default: false.
    712          *     @type bool         $populate_extras   Whether to fetch additional information
    713          *                                           (such as member count) about groups. Default: true.
    714          *     @type array|string $exclude           Optional. Array or comma-separated list of group IDs.
    715          *                                           Results will exclude the listed groups. Default: false.
    716          *     @type bool         $update_meta_cache Whether to pre-fetch groupmeta for
    717          *                                           the returned groups. Default: true.
    718          *     @type bool         $show_hidden       Whether to include hidden groups in results. Default: false.
     689         *     @type string       $type                Optional. Shorthand for certain orderby/
     690         *                                             order combinations. 'newest', 'active', 'popular',
     691         *                                             'alphabetical', 'random'. When present, will override
     692         *                                             orderby and order params. Default: null.
     693         *     @type string       $orderby             Optional. Property to sort by.
     694         *                                             'date_created', 'last_activity', 'total_member_count',
     695         *                                             'name', 'random'. Default: 'date_created'.
     696         *     @type string       $order               Optional. Sort order. 'ASC' or 'DESC'.
     697         *                                             Default: 'DESC'.
     698         *     @type int          $per_page            Optional. Number of items to return per page
     699         *                                             of results. Default: null (no limit).
     700         *     @type int          $page                Optional. Page offset of results to return.
     701         *                                             Default: null (no limit).
     702         *     @type int          $user_id             Optional. If provided, results will be limited to groups
     703         *                                             of which the specified user is a member. Default: null.
     704         *     @type string       $search_terms        Optional. If provided, only groups whose names
     705         *                                             or descriptions match the search terms will be
     706         *                                             returned. Default: false.
     707         *     @type array|string $group_type          Array or comma-separated list of group types to limit results to.
     708         *     @type array|string $group_type__in      Array or comma-separated list of group types to limit results to.
     709         *     @type array|string $group_type__not_in  Array or comma-separated list of group types,that will be
     710         *                                             excluded from results.
     711         *     @type array        $meta_query          Optional. An array of meta_query conditions.
     712         *                                             See {@link WP_Meta_Query::queries} for description.
     713         *     @type array|string $value               Optional. Array or comma-separated list of group IDs.
     714         *                                             Results will be limited to groups within the
     715         *                                             list. Default: false.
     716         *     @type bool         $populate_extras     Whether to fetch additional information
     717         *                                             (such as member count) about groups. Default: true.
     718         *     @type array|string $exclude             Optional. Array or comma-separated list of group IDs.
     719         *                                             Results will exclude the listed groups. Default: false.
     720         *     @type bool         $update_meta_cache   Whether to pre-fetch groupmeta for
     721         *                                             the returned groups. Default: true.
     722         *     @type bool         $show_hidden         Whether to include hidden groups in results. Default: false.
    719723         * }
    720724         * @return array {
    721725         *     @type array $groups Array of group objects returned by the
    class BP_Groups_Group { 
    748752                }
    749753
    750754                $defaults = array(
    751                         'type'              => null,
    752                         'orderby'           => 'date_created',
    753                         'order'             => 'DESC',
    754                         'per_page'          => null,
    755                         'page'              => null,
    756                         'user_id'           => 0,
    757                         'search_terms'      => false,
    758                         'meta_query'        => false,
    759                         'include'           => false,
    760                         'populate_extras'   => true,
    761                         'update_meta_cache' => true,
    762                         'exclude'           => false,
    763                         'show_hidden'       => false,
     755                        'type'               => null,
     756                        'orderby'            => 'date_created',
     757                        'order'              => 'DESC',
     758                        'per_page'           => null,
     759                        'page'               => null,
     760                        'user_id'            => 0,
     761                        'search_terms'       => false,
     762                        'group_type'         => '',
     763                        'group_type__in'     => '',
     764                        'group_type__not_in' => '',
     765                        'meta_query'         => false,
     766                        'include'            => false,
     767                        'populate_extras'    => true,
     768                        'update_meta_cache'  => true,
     769                        'exclude'            => false,
     770                        'show_hidden'        => false,
    764771                );
    765772
    766773                $r = wp_parse_args( $args, $defaults );
    class BP_Groups_Group { 
    804811                        $sql['meta'] = $meta_query_sql['where'];
    805812                }
    806813
     814                // Only use 'group_type__in', if 'group_type' is not set.
     815                if ( empty( $r['group_type'] ) && ! empty( $r['group_type__in']) ) {
     816                        $r['group_type'] = $r['group_type__in'];
     817                }
     818
     819                // Group types to exclude. This has priority over inclusions.
     820                if ( ! empty( $r['group_type__not_in'] ) ) {
     821                        $group_type_clause = self::get_sql_clause_for_group_types($r['group_type__not_in'], 'NOT IN' );
     822
     823                // Group types to include.
     824                } elseif ( ! empty( $r['group_type'] ) ) {
     825                        $group_type_clause = self::get_sql_clause_for_group_types($r['group_type'], 'IN' );
     826                }
     827
     828                if ( ! empty( $group_type_clause ) ) {
     829                        $sql['group_type'] = $group_type_clause;
     830                }
     831
    807832                if ( ! empty( $r['user_id'] ) ) {
    808833                        $sql['user'] = $wpdb->prepare( " AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $r['user_id'] );
    809834                }
    class BP_Groups_Group { 
    915940                        $total_sql['where'][] = $meta_query_clause;
    916941                }
    917942
     943                // Trim leading 'AND' to match $total_sql query style.
     944                if ( ! empty( $group_type_clause ) ) {
     945                        $total_sql['where'][] = preg_replace( '/^\s*AND\s*/', '', $group_type_clause );
     946                }
     947
    918948                // Already escaped in the paginated results block.
    919949                if ( ! empty( $include ) ) {
    920950                        $total_sql['where'][] = "g.id IN ({$include})";
    class BP_Groups_Group { 
    16241654
    16251655                return $ids;
    16261656        }
     1657
     1658        /**
     1659         * Get SQL clause for group type(s).
     1660         *
     1661         * @since 2.5.0
     1662         *
     1663         * @param  string|array $group_types Group type(s).
     1664         * @param  string       $operator    'IN' or 'NOT IN'.
     1665         * @return string       $clause      SQL clause.
     1666         */
     1667        protected static function get_sql_clause_for_group_types( $group_types, $operator ) {
     1668                global $wpdb;
     1669
     1670                // Sanitize operator.
     1671                if ( 'NOT IN' !== $operator ) {
     1672                        $operator = 'IN';
     1673                }
     1674
     1675                // Parse and sanitize types.
     1676                if ( ! is_array( $group_types ) ) {
     1677                        $group_types = preg_split( '/[,\s+]/', $group_types );
     1678                }
     1679
     1680                $types = array();
     1681                foreach ( $group_types as $gt ) {
     1682                        if ( bp_groups_get_group_type_object( $gt ) ) {
     1683                                $types[] = $gt;
     1684                        }
     1685                }
     1686
     1687                $tax_query = new WP_Tax_Query( array(
     1688                        array(
     1689                                'taxonomy' => 'bp_group_type',
     1690                                'field'    => 'name',
     1691                                'operator' => $operator,
     1692                                'terms'    => $types,
     1693                        ),
     1694                ) );
     1695
     1696                $switched = false;
     1697                if ( ! bp_is_root_blog() ) {
     1698                        switch_to_blog( bp_get_root_blog_id() );
     1699                        $switched = true;
     1700                }
     1701
     1702                $sql_clauses = $tax_query->get_sql( 'g', 'id' );
     1703
     1704                if ( $switched ) {
     1705                        restore_current_blog();
     1706                }
     1707
     1708                $clause = '';
     1709
     1710                // The no_results clauses are the same between IN and NOT IN.
     1711                if ( false !== strpos( $sql_clauses['where'], '0 = 1' ) ) {
     1712                        $clause = $sql_clauses['where'];
     1713
     1714                // The tax_query clause generated for NOT IN can be used almost as-is.
     1715                } elseif ( 'NOT IN' === $operator ) {
     1716                        $clause = $sql_clauses['where'];
     1717
     1718                // IN clauses must be converted to a subquery.
     1719                } elseif ( preg_match( '/' . $wpdb->term_relationships . '\.term_taxonomy_id IN \([0-9, ]+\)/', $sql_clauses['where'], $matches ) ) {
     1720                        $clause = " AND g.id IN ( SELECT object_id FROM $wpdb->term_relationships WHERE {$matches[0]} )";
     1721                }
     1722
     1723                return $clause;
     1724        }
    16271725}