Skip to content

Commit

Permalink
Merge pull request #23 from alleyinteractive/feature/bp-group-terms
Browse files Browse the repository at this point in the history
Support for BuddyPress Group Terms
  • Loading branch information
renatonascalves authored Sep 13, 2023
2 parents 3c3c06a + 8223a7b commit a18cd7a
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
1 change: 1 addition & 0 deletions inc/class-wp-object.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class WP_Object {
* @param bool $hide_empty Optional flag to hide the meta box if there is no meta.
*/
public function render_meta_table( string $title = '', bool $hide_empty = false ) {

// Store meta.
$meta = [];

Expand Down
93 changes: 85 additions & 8 deletions inc/objects/class-bp-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Meta_Inspector;

/**
* Inspect meta for a BuddyPress group.
* Inspect meta and terms for BuddyPress groups.
*/
class BP_Group extends WP_Object {
use Singleton;
Expand Down Expand Up @@ -38,23 +38,100 @@ protected function __construct() {
*/
public function add_meta_boxes() {

// Store group ID.
$this->object_id = (int) sanitize_text_field( wp_unslash( $_GET['gid'] ?? 0 ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
// Ensure the group id is set.
if ( ! isset( $_GET['gid'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return;
}

// Store group id.
$this->object_id = (int) sanitize_text_field( wp_unslash( $_GET['gid'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

// Get screen id.
$screen_id = get_current_screen()->id;

// Group meta.
add_meta_box(
'meta-inspector-bp-group-meta',
__( 'Meta', 'meta-inspector' ),
[ $this, 'render_meta' ],
get_current_screen()->id,
fn () => $this->render_meta_table(),
$screen_id,
'normal'
);

// Group terms.
add_meta_box(
'meta-inspector-bp-group-terms',
__( 'Terms', 'meta-inspector' ),
[ $this, 'render_terms' ],
$screen_id,
'normal'
);
}

/**
* Render a table of group meta.
* Render a table of group terms.
*/
public function render_meta() {
$this->render_meta_table();
public function render_terms() {

// Get group taxonomies.
$taxonomies = get_object_taxonomies( 'bp_group', 'objects' );

if ( empty( $taxonomies ) ) {
printf(
'<p>%s</p>',
esc_html__( 'No taxonomies registered for this group.', 'meta-inspector' )
);

return;
}

// Loop through taxonomies and terms and build data array.
foreach ( $taxonomies as $taxonomy ) {

$taxonomy_object = get_taxonomy( $taxonomy->name );

if ( empty( $taxonomy_object ) ) {
continue;
}

// Reset data for this taxonomy.
$data = [];

// Get all terms.
$terms = bp_get_object_terms(
$this->object_id,
$taxonomy->name,
[ 'hide_empty' => false ]
);

// Build data array [ id, name, slug, taxonomy ].
foreach ( $terms as $term ) {

// Get singular name if available.
$term_name = (string) get_term_meta( $term->term_id, 'bp_type_singular_name', true ) ?: $term->name;

$data[] = [
$term->term_id,
$term_name,
$term->slug,
$term->taxonomy,
];
}

( new Table(
$data,
[
__( 'ID', 'meta-inspector' ),
__( 'Name', 'meta-inspector' ),
__( 'Slug', 'meta-inspector' ),
__( 'Taxonomy', 'meta-inspector' ),
],
sprintf(
/* translators: %s: taxonomy name */
__( 'Taxonomy: %s', 'meta-inspector' ),
$taxonomy_object->label ?? ucfirst( $taxonomy ),
),
) )->render();
}
}
}
8 changes: 5 additions & 3 deletions inc/objects/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ protected function __construct() {
* Add meta boxes to the post edit screen.
*/
public function add_meta_boxes() {
// Get screen id.
$screen = get_post_type();

// Store post id.
$this->object_id = get_the_ID();
Expand All @@ -40,15 +42,15 @@ public function add_meta_boxes() {
'meta-inspector-post-meta',
__( 'Meta', 'meta-inspector' ),
fn () => $this->render_meta_table(),
get_post_type()
$screen
);

// Post terms.
add_meta_box(
'meta-inspector-post-terms',
__( 'Terms', 'meta-inspector' ),
[ $this, 'render_terms' ],
get_post_type()
$screen
);
}

Expand All @@ -57,7 +59,7 @@ public function add_meta_boxes() {
*/
public function render_terms() {

// Get taxonomies for this post.
// Get post taxonomies.
$taxonomies = get_post_taxonomies( $this->object_id );

if ( empty( $taxonomies ) ) {
Expand Down

0 comments on commit a18cd7a

Please sign in to comment.