Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for BuddyPress Group Terms #23

Merged
merged 2 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 78 additions & 7 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 @@ -37,24 +37,95 @@ protected function __construct() {
* Add meta boxes to the BuddyPress group edit screen.
*/
public function add_meta_boxes() {
// Get screen id.
$screen_id = get_current_screen()->id;

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

// 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(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this needs to be an anonymous function rather than [ $this, 'render_meta_table' ]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$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 ) {

// 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,
];
}

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

if ( empty( $taxonomy_object ) ) {
continue;
}
renatonascalves marked this conversation as resolved.
Show resolved Hide resolved

( 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