diff --git a/inc/class-wp-object.php b/inc/class-wp-object.php index 239f734..4840491 100644 --- a/inc/class-wp-object.php +++ b/inc/class-wp-object.php @@ -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 = []; diff --git a/inc/objects/class-bp-group.php b/inc/objects/class-bp-group.php index 44aca4e..2505a0b 100644 --- a/inc/objects/class-bp-group.php +++ b/inc/objects/class-bp-group.php @@ -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; @@ -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( + '

%s

', + 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(); + } } } diff --git a/inc/objects/class-post.php b/inc/objects/class-post.php index fdf8d9b..3868d79 100644 --- a/inc/objects/class-post.php +++ b/inc/objects/class-post.php @@ -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(); @@ -40,7 +42,7 @@ public function add_meta_boxes() { 'meta-inspector-post-meta', __( 'Meta', 'meta-inspector' ), fn () => $this->render_meta_table(), - get_post_type() + $screen ); // Post terms. @@ -48,7 +50,7 @@ public function add_meta_boxes() { 'meta-inspector-post-terms', __( 'Terms', 'meta-inspector' ), [ $this, 'render_terms' ], - get_post_type() + $screen ); } @@ -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 ) ) {