__( 'AdminEase', 'adminease' ), 'description' => __( 'Read site information such as terms and user roles.', 'adminease' ), ] ); } /** * Registers the abilities. * * @return void */ public function register_abilities(): void { if ( ! function_exists( 'wp_register_ability' ) ) { return; } $meta = [ 'annotations' => [ 'readonly' => true, 'destructive' => false, 'idempotent' => true, ], 'show_in_rest' => true, 'public' => true, 'mcp' => [ 'public' => true ], ]; $text = [ 'type' => 'string' ]; $int = [ 'type' => 'integer' ]; wp_register_ability( 'adminease/list-terms', [ 'label' => __( 'List terms', 'adminease' ), 'description' => __( 'Lists the terms of one taxonomy (categories by default, or tags, product categories and any other registered taxonomy) with their id, name, slug, parent and post count, oldest first. Empty terms are included.', 'adminease' ), 'category' => self::CATEGORY, 'input_schema' => [ 'type' => 'object', 'default' => [], 'properties' => [ 'taxonomy' => [ 'type' => 'string', 'default' => 'category', 'description' => __( 'The taxonomy name, such as category, post_tag or product_cat.', 'adminease' ), ], 'search' => [ 'type' => 'string', 'description' => __( 'Only terms whose name or slug contains this text.', 'adminease' ), ], 'per_page' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => self::MAX_PER_PAGE, 'default' => self::MAX_PER_PAGE, ], 'page' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => self::MAX_PAGE, 'default' => 1, ], ], ], 'output_schema' => [ 'type' => 'object', 'properties' => [ 'terms' => [ 'type' => 'array', 'items' => [ 'type' => 'object', 'properties' => [ 'id' => $int, 'name' => $text, 'slug' => $text, 'parent' => $int, 'count' => $int, ], ], ], 'total' => $int, ], ], 'execute_callback' => [ $this, 'list_terms' ], 'permission_callback' => [ $this, 'can_manage' ], 'meta' => $meta, ] ); wp_register_ability( 'adminease/list-roles', [ 'label' => __( 'List user roles', 'adminease' ), 'description' => __( 'Lists the user roles of the site with their slug and display name.', 'adminease' ), 'category' => self::CATEGORY, 'input_schema' => [ 'type' => 'object', 'default' => [], ], 'output_schema' => [ 'type' => 'object', 'properties' => [ 'roles' => [ 'type' => 'array', 'items' => [ 'type' => 'object', 'properties' => [ 'slug' => $text, 'name' => $text, ], ], ], ], ], 'execute_callback' => [ $this, 'list_roles' ], 'permission_callback' => [ $this, 'can_manage' ], 'meta' => $meta, ] ); } /** * Both abilities need the capability the AdminEase screens need. * * @return bool */ public function can_manage(): bool { return current_user_can( 'manage_options' ); } /** * Lists the terms of one taxonomy, one page at a time. * * @param mixed $input The ability input. * * @return array|WP_Error */ public function list_terms( $input = null ) { $input = is_array( $input ) ? $input : []; $taxonomy = sanitize_key( (string) ( $input['taxonomy'] ?? 'category' ) ); $search = sanitize_text_field( (string) ( $input['search'] ?? '' ) ); $per_page = min( self::MAX_PER_PAGE, max( 1, (int) ( $input['per_page'] ?? self::MAX_PER_PAGE ) ) ); $page = min( self::MAX_PAGE, max( 1, (int) ( $input['page'] ?? 1 ) ) ); if ( ! taxonomy_exists( $taxonomy ) ) { /* translators: %s: the taxonomy name that was sent. */ return new WP_Error( 'adminease_invalid_taxonomy', sprintf( __( '%s is not a registered taxonomy.', 'adminease' ), $taxonomy ), [ 'status' => 400 ] ); } $args = [ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'search' => $search, ]; $total = wp_count_terms( $args ); $terms = get_terms( $args + [ 'orderby' => 'term_id', 'number' => $per_page, 'offset' => ( $page - 1 ) * $per_page, ] ); if ( is_wp_error( $terms ) ) { return $terms; } $items = []; foreach ( $terms as $term ) { $items[] = [ 'id' => (int) $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => (int) $term->parent, 'count' => (int) $term->count, ]; } return [ 'terms' => $items, 'total' => is_wp_error( $total ) ? count( $items ) : (int) $total, ]; } /** * Lists the site's user roles. * * @param mixed $input The ability input. Not used. * * @return array */ public function list_roles( $input = null ): array { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Core passes the input once a schema is set. $items = []; foreach ( wp_roles()->get_names() as $slug => $name ) { $items[] = [ 'slug' => (string) $slug, 'name' => translate_user_role( $name ), ]; } return [ 'roles' => $items ]; } }