<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][12731] trunk: BP Types: add functions to generate the Group types WP Admin screens</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/12731">12731</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/12731","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>imath</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2020-09-21 01:36:43 +0000 (Mon, 21 Sep 2020)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>BP Types: add functions to generate the Group types WP Admin screens
- Adds labels and arguments to the Group Type taxonomy.
- Adds the Group type's Administration screens.
- Introduces the `bp_groups_register_group_type_taxonomy()` function to register the Group type's taxonomy and stop doing it from the BP_Groups_Component::register_taxonomies() method.
- Introduces the `bp_get_group_type_metadata_schema()` to add custom metadata for the Group type's taxonomy.
- Introduces the `bp_get_group_types_registered_by_code()` to only get Group types registered by code.
- Stops using the hardcoded `bp_group_type` identifier in favor of the filterable `bp_get_group_type_tax_name()` function.
- Updates the `test_groups_get_type_should_not_return_unregistered_types()` unit test to `test_groups_registered_by_code_group_type_should_not_return_unregistered_types()` to check the previous point new function.
Props mercime, DJPaul, dcavins, boonebgorges
Fixes <a href="http://buddypress.trac.wordpress.org/ticket/7179">#7179</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpgroupsbpgroupsadminphp">trunk/src/bp-groups/bp-groups-admin.php</a></li>
<li><a href="#trunksrcbpgroupsbpgroupsfunctionsphp">trunk/src/bp-groups/bp-groups-functions.php</a></li>
<li><a href="#trunksrcbpgroupsclassesclassbpgroupscomponentphp">trunk/src/bp-groups/classes/class-bp-groups-component.php</a></li>
<li><a href="#trunksrcbpgroupsclassesclassbpgroupsgroupphp">trunk/src/bp-groups/classes/class-bp-groups-group.php</a></li>
<li><a href="#trunktestsphpunittestcasesgroupstypesphp">trunk/tests/phpunit/testcases/groups/types.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpgroupsbpgroupsadminphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/bp-groups-admin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/bp-groups-admin.php 2020-09-21 01:29:06 UTC (rev 12730)
+++ trunk/src/bp-groups/bp-groups-admin.php 2020-09-21 01:36:43 UTC (rev 12731)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -14,11 +14,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> defined( 'ABSPATH' ) || exit;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Include WP's list table class.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( !class_exists( 'WP_List_Table' ) ) require( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! class_exists( 'WP_List_Table' ) ) {
+ require ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // The per_page screen option. Has to be hooked in extremely early.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups' == $_REQUEST['page'] )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( is_admin() && ! empty( $_REQUEST['page'] ) && 'bp-groups' == $_REQUEST['page'] ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'set-screen-option', 'bp_groups_admin_screen_options', 10, 3 );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+}
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Register the Groups component admin screen.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -43,6 +46,63 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( bp_core_admin_hook(), 'bp_groups_add_admin_menu' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Redirects the user on the Goups network admin screen when BuddyPress is network activated.
+ *
+ * @since 7.0.0
+ */
+function bp_group_site_admin_network_admin_redirect() {
+ wp_safe_redirect( add_query_arg( 'page', 'bp-groups', network_admin_url( 'admin.php' ) ) );
+ exit();
+}
+
+/**
+ * Create Groups submenu to manage BuddyPress types.
+ *
+ * @since 7.0.0
+ */
+function bp_groups_admin_types_menu() {
+ if ( ! bp_is_root_blog() ) {
+ return;
+ }
+
+ if ( bp_is_network_activated() && is_network_admin() ) {
+ // Adds a 'bp-groups' submenu to go to the root blog Group types screen.
+ $group_type_admin_url = add_query_arg( 'taxonomy', 'bp_group_type', get_admin_url( bp_get_root_blog_id(), 'edit-tags.php' ) );
+ add_submenu_page(
+ 'bp-groups',
+ __( 'Group types', 'buddypress' ),
+ __( 'Group types', 'buddypress' ),
+ 'bp_moderate',
+ esc_url( $group_type_admin_url )
+ );
+ } elseif ( ! is_network_admin() ) {
+ if ( is_multisite() ) {
+ // Adds a 'bp-groups' menu to the root blog menu.
+ $redirect_hook = add_menu_page(
+ _x( 'Groups', 'Admin Groups page title', 'buddypress' ),
+ _x( 'Groups', 'Admin Groups menu', 'buddypress' ),
+ 'bp_moderate',
+ 'bp-groups',
+ '__return_empty_string',
+ 'div'
+ );
+
+ add_action( "load-{$redirect_hook}", 'bp_group_site_admin_network_admin_redirect' );
+ }
+
+ // Add the submenu to manage Group Types.
+ add_submenu_page(
+ 'bp-groups',
+ __( 'Group types', 'buddypress' ),
+ __( 'Group types', 'buddypress' ),
+ 'bp_moderate',
+ basename( add_query_arg( 'taxonomy', 'bp_group_type', bp_get_admin_url( 'edit-tags.php' ) ) )
+ );
+ }
+}
+add_action( 'bp_admin_menu', 'bp_groups_admin_types_menu' );
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Add groups component to custom menus array.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * This ensures that the Groups menu item appears in the proper order on the
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1379,3 +1439,51 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Checks whether a group type already exists.
+ *
+ * @since 7.0.0
+ *
+ * @param boolean $exists True if the group type already exists. False otherwise.
+ * @param string $type_id The group type identifier.
+ * @return boolean True if the group type already exists. False otherwise.
+ */
+function bp_groups_type_admin_type_exists( $exists = false, $type_id = '' ) {
+ if ( ! $type_id ) {
+ return $exists;
+ }
+
+ return ! is_null( bp_groups_get_group_type_object( $type_id ) );
+}
+add_filter( bp_get_group_type_tax_name() . '_check_existing_type', 'bp_groups_type_admin_type_exists', 1, 2 );
+
+/**
+ * Set the feedback messages for the Group Types Admin actions.
+ *
+ * @since 7.0.0
+ *
+ * @param array $messages The feedback messages.
+ * @return array The feedback messages including the ones for the Group Types Admin actions.
+ */
+function bp_groups_type_admin_updated_messages( $messages = array() ) {
+ $type_taxonomy = bp_get_group_type_tax_name();
+
+ $messages[ $type_taxonomy ] = array(
+ 0 => '',
+ 1 => __( 'Please define the Group Type ID field.', 'buddypress' ),
+ 2 => __( 'Group type successfully added.', 'buddypress' ),
+ 3 => __( 'Sorry, there was an error and the Group type wasn’t added.', 'buddypress' ),
+ // The following one needs to be != 5.
+ 4 => __( 'Group type successfully updated.', 'buddypress' ),
+ 5 => __( 'Sorry, this Group type already exists.', 'buddypress' ),
+ 6 => __( 'Sorry, the Group type was not deleted: it does not exist.', 'buddypress' ),
+ 7 => __( 'Sorry, This Group type is registered using code, deactivate the plugin or remove the custom code before trying to delete it again.', 'buddypress' ),
+ 8 => __( 'Sorry, there was an error while trying to delete this Group type.', 'buddypress' ),
+ 9 => __( 'Group type successfully deleted.', 'buddypress' ),
+ 10 => __( 'Group type could not be updated due to missing required information.', 'buddypress' ),
+ );
+
+ return $messages;
+}
+add_filter( 'term_updated_messages', 'bp_groups_type_admin_updated_messages' );
</ins></span></pre></div>
<a id="trunksrcbpgroupsbpgroupsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/bp-groups-functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/bp-groups-functions.php 2020-09-21 01:29:06 UTC (rev 12730)
+++ trunk/src/bp-groups/bp-groups-functions.php 2020-09-21 01:36:43 UTC (rev 12731)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2464,6 +2464,130 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /** Group Types ***************************************************************/
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Output the slug of the Group type taxonomy.
+ *
+ * @since 7.0.0
+ */
+function bp_group_type_tax_name() {
+ echo bp_get_group_type_tax_name();
+}
+
+ /**
+ * Return the slug of the Group type taxonomy.
+ *
+ * @since 7.0.0
+ *
+ * @return string The unique Group taxonomy slug.
+ */
+ function bp_get_group_type_tax_name() {
+ /**
+ * Filters the slug of the Group type taxonomy.
+ *
+ * @since 7.0.0
+ *
+ * @param string $value Group type taxonomy slug.
+ */
+ return apply_filters( 'bp_get_group_type_tax_name', 'bp_group_type' );
+ }
+
+/**
+ * Returns labels used by the Group type taxonomy.
+ *
+ * @since 7.0.0
+ *
+ * @return array
+ */
+function bp_get_group_type_tax_labels() {
+
+ /**
+ * Filters Group type taxonomy labels.
+ *
+ * @since 7.0.0
+ *
+ * @param array $value Associative array (name => label).
+ */
+ return apply_filters(
+ 'bp_get_group_type_tax_labels',
+ array(
+ 'name' => _x( 'Group types', 'Group type taxonomy name', 'buddypress' ),
+ 'singular_name' => _x( 'Group type', 'Group type taxonomy singular name', 'buddypress' ),
+ 'search_items' => _x( 'Search Group types', 'Group type taxonomy search items label', 'buddypress' ),
+ 'popular_items' => _x( 'Most used Group types', 'Group type taxonomy popular items label', 'buddypress' ),
+ 'all_items' => _x( 'All Group types', 'Group type taxonomy all items label', 'buddypress' ),
+ 'edit_item' => _x( 'Edit Group type', 'Group type taxonomy edit item label', 'buddypress' ),
+ 'view_item' => _x( 'View Group type', 'Group type taxonomy view item label', 'buddypress' ),
+ 'update_item' => _x( 'Update Group type', 'Group type taxonomy update item label', 'buddypress' ),
+ 'add_new_item' => _x( 'Add new Group type', 'Group type taxonomy add new item label', 'buddypress' ),
+ 'new_item_name' => _x( 'New Group type name', 'Group type taxonomy new item name label', 'buddypress' ),
+ 'separate_items_with_commas' => _x( 'Separate Group types with commas', 'Group type taxonomy separate items with commas label', 'buddypress' ),
+ 'add_or_remove_items' => _x( 'Add or remove Group types', 'Group type taxonomy add or remove items label', 'buddypress' ),
+ 'choose_from_most_used' => _x( 'Choose from the most used Group types', 'Group type taxonomy choose from most used label', 'buddypress' ),
+ 'not_found' => _x( 'No Group types found', 'Group type taxonomy not found label', 'buddypress' ),
+ 'no_terms' => _x( 'No Group types', 'Group type taxonomy no terms label', 'buddypress' ),
+ 'items_list_navigation' => _x( 'Group types list navigation', 'Group type taxonomy items list navigation label', 'buddypress' ),
+ 'items_list' => _x( 'Group types list', 'Group type taxonomy items list label', 'buddypress' ),
+ 'back_to_items' => _x( 'Back to all Group types', 'Group type taxonomy back to items label', 'buddypress' ),
+ // Specific to BuddyPress.
+ 'bp_type_id_label' => _x( 'Group Type ID', 'BP Member type ID label', 'buddypress' ),
+ 'bp_type_id_description' => _x( 'Enter a lower-case string without spaces or special characters (used internally to identify the group type).', 'BP Group type ID description', 'buddypress' ),
+ 'bp_type_show_in_create_screen' => _x( 'Add to Available Types on Create Screen', 'BP Group type show in create screen', 'buddypress' ),
+ 'bp_type_show_in_list' => _x( 'Include when Group Types are Listed for a Group', 'BP Group type show in list', 'buddypress' ),
+ )
+ );
+}
+
+/**
+ * Returns arguments used by the Group type taxonomy.
+ *
+ * @since 7.0.0
+ *
+ * @return array
+ */
+function bp_get_group_type_tax_args() {
+
+ /**
+ * Filters Group type taxonomy args.
+ *
+ * @since 7.0.0
+ *
+ * @param array $value Associative array (key => arg).
+ */
+ return apply_filters(
+ 'bp_get_group_type_tax_args',
+ array_merge(
+ array(
+ 'description' => _x( 'BuddyPress Group types', 'Group type taxonomy description', 'buddypress' ),
+ 'labels' => array_merge( bp_get_group_type_tax_labels(), bp_get_taxonomy_common_labels() ),
+ ),
+ bp_get_taxonomy_common_args()
+ )
+ );
+}
+
+/**
+ * Register the Group Types taxonomy.
+ *
+ * @since 7.0.0
+ *
+ * @param array $taxonomies BuddyPress default taxonomies.
+ * @return array BuddyPress default taxonomies.
+ */
+function bp_groups_register_group_type_taxonomy( $taxonomies = array() ) {
+ return array_merge(
+ $taxonomies,
+ array(
+ // Group Type.
+ bp_get_group_type_tax_name() => array(
+ 'object' => 'bp_group',
+ 'component' => 'groups',
+ 'args' => bp_get_group_type_tax_args(),
+ ),
+ )
+ );
+}
+add_filter( 'bp_get_default_taxonomies', 'bp_groups_register_group_type_taxonomy', 1 );
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Fire the 'bp_groups_register_group_types' action.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2479,6 +2603,62 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'bp_register_taxonomies', 'bp_groups_register_group_types' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Extend generic Type metadata schema to match Group Type needs.
+ *
+ * @since 7.0.0
+ *
+ * @param array $schema The generic Type metadata schema.
+ * @param string $taxonomy The taxonomy name the schema applies to.
+ * @return array The Group Type metadata schema.
+ */
+function bp_get_group_type_metadata_schema( $schema = array(), $taxonomy = '' ) {
+ if ( bp_get_group_type_tax_name() === $taxonomy ) {
+ if ( isset( $schema['bp_type_has_directory']['description'] ) ) {
+ $schema['bp_type_has_directory']['description'] = __( 'Add a list of groups matching the member type available on the Groups Directory page (e.g. site.url/groups/type/ninja/).', 'buddypress' );
+ }
+
+ if ( isset( $schema['bp_type_directory_slug']['description'] ) ) {
+ $schema['bp_type_directory_slug']['description'] = __( 'If you want to use a slug that is different from the Group Type ID above, enter it here.', 'buddypress' );
+ }
+
+ $schema = array_merge(
+ $schema,
+ array(
+ 'bp_type_show_in_create_screen' => array(
+ 'description' => __( 'Include this group type during group creation and when a group administrator is on the group’s “Manage > Settings” page.', 'buddypress' ),
+ 'type' => 'boolean',
+ 'single' => true,
+ 'sanitize_callback' => 'absint',
+ ),
+ 'bp_type_show_in_list' => array(
+ 'description' => __( 'Include this group type when group types are listed, like in the group header.', 'buddypress' ),
+ 'type' => 'boolean',
+ 'single' => true,
+ 'sanitize_callback' => 'absint',
+ ),
+ )
+ );
+ }
+
+ return $schema;
+}
+add_filter( 'bp_get_type_metadata_schema', 'bp_get_group_type_metadata_schema', 1, 2 );
+
+/**
+ * Registers the Group type metadata.
+ *
+ * @since 7.0.0
+ */
+function bp_register_group_type_metadata() {
+ $type_taxonomy = bp_get_group_type_tax_name();
+
+ foreach ( bp_get_type_metadata_schema( false, $type_taxonomy ) as $meta_key => $meta_args ) {
+ bp_register_type_meta( $type_taxonomy, $meta_key, $meta_args );
+ }
+}
+add_action( 'bp_register_type_metadata', 'bp_register_group_type_metadata', 11 );
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Register a group type.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2525,6 +2705,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'description' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'create_screen_checked' => false,
</span><span class="cx" style="display: block; padding: 0 10px"> 'labels' => array(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'code' => true,
+ 'db_id' => 0,
</ins><span class="cx" style="display: block; padding: 0 10px"> ), 'register_group_type' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $group_type = sanitize_key( $group_type );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2617,6 +2799,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $types = wp_filter_object_list( $types, $args, $operator );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Merge with types available into the database.
+ if ( ! isset( $args['code'] ) || true !== $args['code'] ) {
+ $types = bp_get_taxonomy_types( bp_get_group_type_tax_name(), $types );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the array of group type objects.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2657,6 +2844,78 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Only gets the group types registered by code.
+ *
+ * @since 7.0.0
+ *
+ * @return array The group types registered by code.
+ */
+function bp_get_group_types_registered_by_code() {
+ return bp_groups_get_group_types(
+ array(
+ 'code' => true,
+ ),
+ 'objects'
+ );
+}
+add_filter( bp_get_group_type_tax_name() . '_registered_by_code', 'bp_get_group_types_registered_by_code' );
+
+/**
+ * Generates missing metadata for a type registered by code.
+ *
+ * @since 7.0.0
+ *
+ * @return array The group type metadata.
+ */
+function bp_set_registered_by_code_group_type_metadata( $metadata = array(), $type = '' ) {
+ $group_type = bp_groups_get_group_type_object( $type );
+
+ foreach ( get_object_vars( $group_type ) as $object_key => $object_value ) {
+ if ( 'labels' === $object_key ) {
+ foreach ( $object_value as $label_key => $label_value ) {
+ $metadata[ 'bp_type_' . $label_key ] = $label_value;
+ }
+ } elseif ( ! in_array( $object_key, array( 'name', 'code', 'db_id' ), true ) ) {
+ $metadata[ 'bp_type_' . $object_key ] = $object_value;
+ }
+ }
+
+ /**
+ * Save metadata into database to avoid generating metadata
+ * each time a type is listed into the Types Admin screen.
+ */
+ if ( isset( $group_type->db_id ) && $group_type->db_id ) {
+ bp_update_type_metadata( $group_type->db_id, bp_get_group_type_tax_name(), $metadata );
+ }
+
+ return $metadata;
+}
+add_filter( bp_get_group_type_tax_name() . '_set_registered_by_code_metada', 'bp_set_registered_by_code_group_type_metadata', 10, 2 );
+
+/**
+ * Insert group types registered by code not yet saved into the database as WP Terms.
+ *
+ * @since 7.0.0
+ */
+function bp_insert_group_types_registered_by_code() {
+ $all_types = bp_groups_get_group_types( array(), 'objects' );
+ $unsaved_types = wp_filter_object_list( $all_types, array( 'db_id' => 0 ), 'and', 'name' );
+
+ if ( $unsaved_types ) {
+ foreach ( $unsaved_types as $type_name ) {
+ bp_insert_term(
+ $type_name,
+ bp_get_group_type_tax_name(),
+ array(
+ 'slug' => $type_name,
+ )
+ );
+ }
+ }
+}
+add_action( bp_get_group_type_tax_name() . '_add_form', 'bp_insert_group_types_registered_by_code', 1 );
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Set type for a group.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2685,7 +2944,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $retval = bp_set_object_terms( $group_id, $group_type, 'bp_group_type', $append );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $retval = bp_set_object_terms( $group_id, $group_type, bp_get_group_type_tax_name(), $append );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Bust the cache if the type has been updated.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $retval ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2710,18 +2969,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Get type for a group.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 7.0.0 Adds the `$use_db` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $group_id ID of the group.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param bool $single Optional. Whether to return a single type string. If multiple types are found
</span><span class="cx" style="display: block; padding: 0 10px"> * for the group, the oldest one will be returned. Default: true.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param bool $use_db Optional. Whether to request all group types or only the ones registered by code.
+ * Default: true.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string|array|bool On success, returns a single group type (if `$single` is true) or an array of group
</span><span class="cx" style="display: block; padding: 0 10px"> * types (if `$single` is false). Returns false on failure.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function bp_groups_get_group_type( $group_id, $single = true ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function bp_groups_get_group_type( $group_id, $single = true, $use_db = true ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $types = wp_cache_get( $group_id, 'bp_groups_group_type' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( false === $types ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $raw_types = bp_get_object_terms( $group_id, 'bp_group_type' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $raw_types = bp_get_object_terms( $group_id, bp_get_group_type_tax_name() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $raw_types ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $types = array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2737,6 +2999,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( false === $use_db && $types ) {
+ $registred_by_code = bp_get_group_types_registered_by_code();
+ $ctype_names = wp_list_pluck( $registred_by_code, 'name' );
+ $types = array_intersect( $types, $ctype_names );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $type = false;
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $types ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $single ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2778,7 +3046,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $deleted = bp_remove_object_terms( $group_id, $group_type, 'bp_group_type' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $deleted = bp_remove_object_terms( $group_id, $group_type, bp_get_group_type_tax_name() );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Bust the case, if the type has been removed.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_wp_error( $deleted ) ) {
</span></span></pre></div>
<a id="trunksrcbpgroupsclassesclassbpgroupscomponentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/classes/class-bp-groups-component.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/classes/class-bp-groups-component.php 2020-09-21 01:29:06 UTC (rev 12730)
+++ trunk/src/bp-groups/classes/class-bp-groups-component.php 2020-09-21 01:36:43 UTC (rev 12731)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -924,12 +924,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Set up taxonomies.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.6.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 7.0.0 The Group Type taxonomy is registered using the `bp_groups_register_group_type_taxonomy()` function.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function register_taxonomies() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Group Type.
- register_taxonomy( 'bp_group_type', 'bp_group', array(
- 'public' => false,
- ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Just let BP Component fire 'bp_groups_register_taxonomies'.
+ return parent::register_taxonomies();
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcbpgroupsclassesclassbpgroupsgroupphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/classes/class-bp-groups-group.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/classes/class-bp-groups-group.php 2020-09-21 01:29:06 UTC (rev 12730)
+++ trunk/src/bp-groups/classes/class-bp-groups-group.php 2020-09-21 01:36:43 UTC (rev 12731)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1746,7 +1746,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $tax_query = new WP_Tax_Query( array(
</span><span class="cx" style="display: block; padding: 0 10px"> array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'taxonomy' => 'bp_group_type',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'taxonomy' => bp_get_group_type_tax_name(),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'field' => 'name',
</span><span class="cx" style="display: block; padding: 0 10px"> 'operator' => $operator,
</span><span class="cx" style="display: block; padding: 0 10px"> 'terms' => $types,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1753,7 +1753,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><span class="cx" style="display: block; padding: 0 10px"> ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $site_id = bp_get_taxonomy_term_site_id( 'bp_group_type' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $site_id = bp_get_taxonomy_term_site_id( bp_get_group_type_tax_name() );
</ins><span class="cx" style="display: block; padding: 0 10px"> $switched = false;
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $site_id !== get_current_blog_id() ) {
</span><span class="cx" style="display: block; padding: 0 10px"> switch_to_blog( $site_id );
</span></span></pre></div>
<a id="trunktestsphpunittestcasesgroupstypesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/testcases/groups/types.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/testcases/groups/types.php 2020-09-21 01:29:06 UTC (rev 12730)
+++ trunk/tests/phpunit/testcases/groups/types.php 2020-09-21 01:36:43 UTC (rev 12731)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -246,7 +246,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( array( 'bar' ), $types );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_groups_get_type_should_not_return_unregistered_types() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_groups_registered_by_code_group_type_should_not_return_unregistered_types() {
</ins><span class="cx" style="display: block; padding: 0 10px"> $g = self::factory()->group->create( array( 'creator_id' => self::$u1 ) );
</span><span class="cx" style="display: block; padding: 0 10px"> bp_groups_register_group_type( 'foo' );
</span><span class="cx" style="display: block; padding: 0 10px"> bp_groups_set_group_type( $g, 'foo' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -254,7 +254,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Directly set a type that hasn't been registered.
</span><span class="cx" style="display: block; padding: 0 10px"> bp_set_object_terms( $g, 'ugh', 'bp_group_type', true );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $type = bp_groups_get_group_type( $g, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $type = bp_groups_get_group_type( $g, false, false );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( array( 'foo' ), $type );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>