/** * Generate the current group type message. * * @since 2.7.0 * * @return string */ function bp_get_current_group_directory_type_message() { $type_object = bp_groups_get_group_type_object(bp_get_current_group_directory_type()); $message = sprintf(__('Viewing groups of the type: %s', 'buddypress'), '<strong>' . $type_object->labels['singular_name'] . '</strong>'); /** * Filters the current group type message. * * @since 2.7.0 * * @param string $message Message to filter. */ return apply_filters('bp_get_current_group_type_message', $message); }
/** * Process input from the Group Type bulk change select. * * @since 2.7.0 * * @param string $doaction Current $_GET action being performed in admin screen. */ function bp_groups_admin_process_group_type_bulk_changes($doaction) { // Bail if no groups are specified or if this isn't a relevant action. if (empty($_REQUEST['gid']) || empty($_REQUEST['bp_change_type']) && empty($_REQUEST['bp_change_type2']) || empty($_REQUEST['bp_change_group_type'])) { return; } // Bail if nonce check fails. check_admin_referer('bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce'); if (!bp_current_user_can('bp_moderate')) { return; } $new_type = ''; if (!empty($_REQUEST['bp_change_type2'])) { $new_type = sanitize_text_field($_REQUEST['bp_change_type2']); } elseif (!empty($_REQUEST['bp_change_type'])) { $new_type = sanitize_text_field($_REQUEST['bp_change_type']); } // Check that the selected type actually exists. if ('remove_group_type' !== $new_type && null === bp_groups_get_group_type_object($new_type)) { $error = true; } else { // Run through group ids. $error = false; foreach ((array) $_REQUEST['gid'] as $group_id) { $group_id = (int) $group_id; // Get the old group type to check against. $group_type = bp_groups_get_group_type($group_id); if ('remove_group_type' === $new_type) { // Remove the current group type, if there's one to remove. if ($group_type) { $removed = bp_groups_remove_group_type($group_id, $group_type); if (false === $removed || is_wp_error($removed)) { $error = true; } } } else { // Set the new group type. if ($new_type !== $group_type) { $set = bp_groups_set_group_type($group_id, $new_type); if (false === $set || is_wp_error($set)) { $error = true; } } } } } // If there were any errors, show the error message. if ($error) { $redirect = add_query_arg(array('updated' => 'group-type-change-error'), wp_get_referer()); } else { $redirect = add_query_arg(array('updated' => 'group-type-change-success'), wp_get_referer()); } wp_redirect($redirect); exit; }
/** * Set up items for display in the list table. * * Handles filtering of data, sorting, pagination, and any other data * manipulation required prior to rendering. * * @since 1.7.0 */ public function prepare_items() { global $groups_template; $screen = get_current_screen(); // Option defaults. $include_id = false; $search_terms = false; // Set current page. $page = $this->get_pagenum(); // Set per page from the screen options. $per_page = $this->get_items_per_page(str_replace('-', '_', "{$screen->id}_per_page")); // Sort order. $order = 'DESC'; if (!empty($_REQUEST['order'])) { $order = 'desc' == strtolower($_REQUEST['order']) ? 'DESC' : 'ASC'; } // Order by - default to newest. $orderby = 'last_activity'; if (!empty($_REQUEST['orderby'])) { switch ($_REQUEST['orderby']) { case 'name': $orderby = 'name'; break; case 'id': $orderby = 'date_created'; break; case 'members': $orderby = 'total_member_count'; break; case 'last_active': $orderby = 'last_activity'; break; } } // Are we doing a search? if (!empty($_REQUEST['s'])) { $search_terms = $_REQUEST['s']; } // Check if user has clicked on a specific group (if so, fetch only that group). if (!empty($_REQUEST['gid'])) { $include_id = (int) $_REQUEST['gid']; } // Set the current view. if (isset($_GET['group_status']) && in_array($_GET['group_status'], array('public', 'private', 'hidden'))) { $this->view = $_GET['group_status']; } // We'll use the ids of group status types for the 'include' param. $this->group_type_ids = BP_Groups_Group::get_group_type_ids(); // Pass a dummy array if there are no groups of this type. $include = false; if ('all' != $this->view && isset($this->group_type_ids[$this->view])) { $include = !empty($this->group_type_ids[$this->view]) ? $this->group_type_ids[$this->view] : array(0); } // Get group type counts for display in the filter tabs. $this->group_counts = array(); foreach ($this->group_type_ids as $group_type => $group_ids) { $this->group_counts[$group_type] = count($group_ids); } // Group types $group_type = false; if (isset($_GET['bp-group-type']) && null !== bp_groups_get_group_type_object($_GET['bp-group-type'])) { $group_type = $_GET['bp-group-type']; } // If we're viewing a specific group, flatten all activities into a single array. if ($include_id) { $groups = array((array) groups_get_group($include_id)); } else { $groups_args = array('include' => $include, 'per_page' => $per_page, 'page' => $page, 'orderby' => $orderby, 'order' => $order); if ($group_type) { $groups_args['group_type'] = $group_type; } $groups = array(); if (bp_has_groups($groups_args)) { while (bp_groups()) { bp_the_group(); $groups[] = (array) $groups_template->group; } } } // Set raw data to display. $this->items = $groups; // Store information needed for handling table pagination. $this->set_pagination_args(array('per_page' => $per_page, 'total_items' => $groups_template->total_group_count, 'total_pages' => ceil($groups_template->total_group_count / $per_page))); }
/** * Check whether the given group has a certain group type. * * @since 2.6.0 * * @param int $group_id ID of the group. * @param string $group_type Group type. * @return bool Whether the group has the give group type. */ function bp_groups_has_group_type($group_id, $group_type) { if (empty($group_type) || !bp_groups_get_group_type_object($group_type)) { return false; } // Get all group's group types. $types = bp_groups_get_group_type($group_id, false); if (!is_array($types)) { return false; } return in_array($group_type, $types); }
public function test_groups_get_type_object_should_return_null_for_non_existing_group_type() { $this->assertSame(null, bp_groups_get_group_type_object('foo')); }
/** * Get SQL clause for group type(s). * * @since 2.6.0 * * @param string|array $group_types Group type(s). * @param string $operator 'IN' or 'NOT IN'. * @return string $clause SQL clause. */ protected static function get_sql_clause_for_group_types($group_types, $operator) { global $wpdb; // Sanitize operator. if ('NOT IN' !== $operator) { $operator = 'IN'; } // Parse and sanitize types. if (!is_array($group_types)) { $group_types = preg_split('/[,\\s+]/', $group_types); } $types = array(); foreach ($group_types as $gt) { if (bp_groups_get_group_type_object($gt)) { $types[] = $gt; } } $tax_query = new WP_Tax_Query(array(array('taxonomy' => 'bp_group_type', 'field' => 'name', 'operator' => $operator, 'terms' => $types))); $site_id = bp_get_taxonomy_term_site_id('bp_group_type'); $switched = false; if ($site_id !== get_current_blog_id()) { switch_to_blog($site_id); $switched = true; } $sql_clauses = $tax_query->get_sql('g', 'id'); $clause = ''; // The no_results clauses are the same between IN and NOT IN. if (false !== strpos($sql_clauses['where'], '0 = 1')) { $clause = self::strip_leading_and($sql_clauses['where']); // The tax_query clause generated for NOT IN can be used almost as-is. } elseif ('NOT IN' === $operator) { $clause = self::strip_leading_and($sql_clauses['where']); // IN clauses must be converted to a subquery. } elseif (preg_match('/' . $wpdb->term_relationships . '\\.term_taxonomy_id IN \\([0-9, ]+\\)/', $sql_clauses['where'], $matches)) { $clause = " g.id IN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE {$matches[0]} )"; } if ($switched) { restore_current_blog(); } return $clause; }