namespace WPForms\Integrations\Abilities;
use WPForms\Integrations\IntegrationInterface;
* WordPress Abilities API Integration for WPForms.
* Provides a standardized interface for AI assistants and automation tools
* to discover and interact with WPForms functionality.
abstract class Abilities implements IntegrationInterface {
* Ability namespace for WPForms abilities.
protected const ABILITY_NAMESPACE = 'wpforms';
* Category slug for WPForms abilities.
protected const CATEGORY_SLUG = 'wpforms-forms';
* Indicate if the current integration is allowed to load.
public function allow_load(): bool {
// Only load if the Abilities API is available (WordPress 6.9+).
return function_exists( 'wp_register_ability' );
public function load(): void {
protected function hooks(): void {
add_action( 'wp_abilities_api_categories_init', [ $this, 'register_category' ] );
add_action( 'wp_abilities_api_init', [ $this, 'register_abilities' ] );
* Register the WPForms ability category.
public function register_category(): void {
wp_register_ability_category(
'label' => __( 'WPForms', 'wpforms-lite' ),
'description' => __( 'Abilities for interacting with WPForms forms and entries.', 'wpforms-lite' ),
* Register WPForms abilities.
abstract public function register_abilities();
* Register common abilities shared between Lite and Pro.
protected function register_common_abilities(): void {
$this->register_list_forms_ability();
$this->register_get_form_ability();
* Register the list_forms ability.
protected function register_list_forms_ability(): void {
self::ABILITY_NAMESPACE . '/list-forms',
'label' => __( 'List Forms', 'wpforms-lite' ),
'description' => __( 'List all available WPForms forms with their metadata.', 'wpforms-lite' ),
'category' => self::CATEGORY_SLUG,
'execute_callback' => [ $this, 'ability_list_forms' ],
'permission_callback' => [ $this, 'check_view_forms_permission' ],
'description' => __( 'Filter forms by status.', 'wpforms-lite' ),
'enum' => [ 'publish', 'draft', 'trash' ],
'description' => __( 'Maximum number of forms to return.', 'wpforms-lite' ),
'description' => __( 'Number of forms to skip.', 'wpforms-lite' ),
'description' => __( 'Array of form objects.', 'wpforms-lite' ),
'id' => [ 'type' => 'integer' ],
'title' => [ 'type' => 'string' ],
'status' => [ 'type' => 'string' ],
'created' => [ 'type' => 'string' ],
'modified' => [ 'type' => 'string' ],
'description' => __( 'Total number of forms returned.', 'wpforms-lite' ),
* Register the get_form ability.
protected function register_get_form_ability(): void {
self::ABILITY_NAMESPACE . '/get-form',
'label' => __( 'Get Form', 'wpforms-lite' ),
'description' => __( 'Get detailed information about a specific WPForms form including its fields.', 'wpforms-lite' ),
'category' => self::CATEGORY_SLUG,
'execute_callback' => [ $this, 'ability_get_form' ],
'permission_callback' => [ $this, 'check_view_single_form_permission' ],
'description' => __( 'The ID of the form to retrieve.', 'wpforms-lite' ),
'description' => __( 'Whether to include field configuration.', 'wpforms-lite' ),
'required' => [ 'form_id' ],
'id' => [ 'type' => 'integer' ],
'title' => [ 'type' => 'string' ],
'status' => [ 'type' => 'string' ],
'settings' => [ 'type' => 'object' ],
'fields' => [ 'type' => 'array' ],
* Permission callback: Check if the user can view forms.
public function check_view_forms_permission() {
if ( ! wpforms_current_user_can( 'view_forms' ) ) {
__( 'You do not have permission to view forms.', 'wpforms-lite' ),
* Permission callback: Check if the user can view a specific form.
* @param mixed $input Input data containing form_id.
public function check_view_single_form_permission( $input = null ) {
$input = $this->normalize_input( $input );
$form_id = absint( $input['form_id'] ?? 0 );
if ( ! $form_id || ! wpforms_current_user_can( 'view_form_single', $form_id ) ) {
__( 'You do not have permission to view this form.', 'wpforms-lite' ),
* Ability callback: List forms.
* @param mixed $input Input data.
public function ability_list_forms( $input = null ): array {
$args = $this->normalize_input( $input );
$form_handler = $this->get_form_handler();
if ( is_wp_error( $form_handler ) ) {
$limit = absint( $args['limit'] ?? 20 );
$offset = absint( $args['offset'] ?? 0 );
$status = sanitize_text_field( $args['status'] ?? 'publish' );
// Get total count efficiently using the cached WordPress function.
$counts = wp_count_posts( 'wpforms' );
$total = $counts->{$status} ?? 0;
// Get paginated forms with proper WordPress pagination.
'post_status' => $status,
'posts_per_page' => $limit,
'nopaging' => false, // Override default to enable pagination.
$forms = $form_handler->get( '', $query_args );
foreach ( $forms as $form ) {
$result[] = $this->format_form_summary( $form );
* Ability callback: Get single form.
* @param mixed $input Input data.
public function ability_get_form( $input = null ) {
$args = $this->normalize_input( $input );
$form_id = absint( $args['form_id'] ?? 0 );
if ( empty( $form_id ) ) {
'wpforms_invalid_form_id',
__( 'Invalid form ID.', 'wpforms-lite' ),
$form_handler = $this->get_form_handler();
if ( is_wp_error( $form_handler ) ) {
$form = $form_handler->get( $form_id );
'wpforms_form_not_found',
__( 'Form not found.', 'wpforms-lite' ),
$include_fields = wp_validate_boolean( $args['include_fields'] ?? true );
return $this->format_form_detail( $form, $include_fields );
* Normalize input data to array format.
* @param mixed $input Input data (can be the array, object, or null).
protected function normalize_input( $input ): array {
if ( is_array( $input ) ) {
if ( is_object( $input ) ) {
* Get the form handler and validate it.
* @return object|WP_Error Form handler object or WP_Error on failure.
protected function get_form_handler() {
$form_handler = wpforms()->obj( 'form' );
'wpforms_form_handler_error',
__( 'Form handler not available.', 'wpforms-lite' ),
* Format form data for summary listing.
* @param WP_Post $form Form the `post` object.
protected function format_form_summary( WP_Post $form ): array {
'title' => $form->post_title,
'status' => $form->post_status,
'created' => $form->post_date,
'modified' => $form->post_modified,
'author' => absint( $form->post_author ),
* Format form data for the detailed view.
* @param WP_Post $form Form `post` object.
* @param bool $include_fields Whether to include fields.
protected function format_form_detail( WP_Post $form, bool $include_fields = true ): array {
$form_handler = $this->get_form_handler();
$form_data = ! is_wp_error( $form_handler ) ? $form_handler->get( $form->ID, [ 'content_only' => true ] ) : [];
// Ensure form_data is an array.
if ( ! is_array( $form_data ) ) {
'title' => $form->post_title,
'status' => $form->post_status,
'created' => $form->post_date,
'modified' => $form->post_modified,
'author' => absint( $form->post_author ),
'settings' => $this->get_safe_settings( $form_data ),
if ( $include_fields && ! empty( $form_data['fields'] ) ) {
$result['fields'] = $this->format_fields( $form_data['fields'] );
* Get safe settings (without sensitive data).
* @param array $form_data Form data.
protected function get_safe_settings( array $form_data ): array {
$settings = $form_data['settings'] ?? [];