From 58e3477a308fcb2e70d5654ab251ffa2f899e5fa Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Wed, 13 Sep 2023 00:37:54 -0300 Subject: [PATCH 1/2] Support for BuddyPress Group Terms --- inc/objects/class-bp-group.php | 85 +++++++++++++++++++++++++++++++--- inc/objects/class-post.php | 8 ++-- 2 files changed, 83 insertions(+), 10 deletions(-) diff --git a/inc/objects/class-bp-group.php b/inc/objects/class-bp-group.php index 44aca4e..22bb97f 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; @@ -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(), + $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 ) { + + // 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; + } + + ( 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 ) ) { From 8223a7bd2a4c488bca2bad44192d2ed48ea833eb Mon Sep 17 00:00:00 2001 From: Renato Alves <19148962+renatonascalves@users.noreply.github.com> Date: Wed, 13 Sep 2023 09:25:15 -0300 Subject: [PATCH 2/2] CR suggestions --- inc/class-wp-object.php | 1 + inc/objects/class-bp-group.php | 24 +++++++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) 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 22bb97f..2505a0b 100644 --- a/inc/objects/class-bp-group.php +++ b/inc/objects/class-bp-group.php @@ -37,11 +37,17 @@ 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; + + // 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'] ?? 0 ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended + $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( @@ -82,6 +88,12 @@ public function render_terms() { // 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 = []; @@ -106,12 +118,6 @@ public function render_terms() { ]; } - $taxonomy_object = get_taxonomy( $taxonomy->name ); - - if ( empty( $taxonomy_object ) ) { - continue; - } - ( new Table( $data, [