\WP_REST_Server::READABLE, 'callback' => array( $this, 'get_categories' ), 'permission_callback' => array( AcrossAI_Abilities_Rest_Controller::instance(), 'check_permission' ), ), ) ); } /** * Handle GET /abilities/categories. * * @since 0.1.0 * @param \WP_REST_Request $request REST request. * @return \WP_REST_Response|\WP_Error */ public function get_categories( \WP_REST_Request $request ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found if ( ! function_exists( 'wp_get_ability_categories' ) ) { return new \WP_Error( 'rest_not_supported', __( 'Abilities API categories not available.', 'acrossai-abilities-manager' ), array( 'status' => 501 ) ); } $raw = wp_get_ability_categories(); $categories = array(); if ( is_array( $raw ) ) { foreach ( $raw as $slug => $data ) { $label = ''; if ( is_array( $data ) && isset( $data['label'] ) ) { $label = (string) $data['label']; } elseif ( is_object( $data ) && isset( $data->label ) ) { $label = (string) $data->label; } elseif ( is_string( $data ) ) { $label = $data; } $categories[] = array( 'slug' => (string) $slug, 'label' => $label, ); } } return rest_ensure_response( $categories ); } }