get_abilities() as $name => $definition ) { $results[ $name ] = $registry->register( $name, $definition ); } return $results; } /** Match the existing accessible-form query's statuses. */ private function statuses(): array { return [ 'publish', 'draft', 'pending', 'private', 'future', 'trash' ]; } /** Native discovery schemas and shared AI Access metadata. */ protected function get_abilities(): array { $fields = [ 'id' => [ 'type' => 'integer', 'minimum' => 1 ] ]; foreach ( [ 'title', 'status', 'created_at', 'modified_at', 'edit_url' ] as $key ) { $fields[ $key ] = [ 'type' => 'string' ]; } $pagination = []; foreach ( [ 'page', 'per_page', 'total', 'total_pages' ] as $key ) { $pagination[ $key ] = [ 'type' => 'integer', 'minimum' => in_array( $key, [ 'page', 'per_page' ], true ) ? 1 : 0 ]; } $definitions = [ 'admin-optimizer/list-forms' => [ 'label' => __( 'List Forms', 'admin-optimizer' ), 'description' => __( 'List forms created with Admin Optimizer that the current user is permitted to access.', 'admin-optimizer' ), 'category' => 'admin-optimizer', 'input_schema' => [ 'type' => 'object', 'default' => new \stdClass(), 'additionalProperties' => false, 'properties' => [ 'page' => [ 'type' => 'integer', 'minimum' => 1, 'default' => 1 ], 'per_page' => [ 'type' => 'integer', 'minimum' => 1, 'maximum' => 100, 'default' => 20 ], 'status' => [ 'type' => 'string', 'enum' => array_merge( [ '' ], $this->statuses() ), 'default' => '' ], 'search' => [ 'type' => 'string', 'default' => '' ], ], ], 'output_schema' => [ 'type' => 'object', 'required' => [ 'forms', 'pagination' ], 'additionalProperties' => false, 'properties' => [ 'forms' => [ 'type' => 'array', 'items' => [ 'type' => 'object', 'properties' => $fields, 'required' => [ 'id', 'title', 'status', 'created_at', 'modified_at' ], 'additionalProperties' => false ] ], 'pagination' => [ 'type' => 'object', 'properties' => $pagination, 'required' => array_keys( $pagination ), 'additionalProperties' => false ], ], ], 'execute_callback' => [ $this, 'execute_list_forms' ], 'permission_callback' => [ $this, 'can_list_forms' ], 'meta' => [ 'show_in_rest' => true, 'mcp' => [ 'public' => true ], 'annotations' => [ 'readonly' => true, 'destructive' => false, 'idempotent' => true ], 'adminoptimizer' => [ 'module' => 'form_builder', 'summary' => __( 'Lists accessible forms without submissions or settings.', 'admin-optimizer' ) ], ], ] ]; return $definitions; } /** @param mixed $input Native callback input. */ public function can_list_forms( $input = null ): bool { return Form_Builder::can_manage_forms(); } /** * @param mixed $input Native object parameters or direct PHP input. * @return array|\WP_Error Accessible metadata or a WordPress error. */ public function execute_list_forms( $input = null ) { if ( ! $this->can_list_forms( $input ) ) { return new \WP_Error( 'ao_list_forms_forbidden', __( 'You are not allowed to list forms.', 'admin-optimizer' ) ); } $input = $input instanceof \stdClass ? get_object_vars( $input ) : ( $input ?? [] ); if ( ! is_array( $input ) || array_diff( array_keys( $input ), [ 'page', 'per_page', 'status', 'search' ] ) ) { return $this->invalid_input(); } $input = array_merge( [ 'page' => 1, 'per_page' => 20, 'status' => '', 'search' => '' ], $input ); if ( ! is_int( $input['page'] ) || $input['page'] < 1 || ! is_int( $input['per_page'] ) || $input['per_page'] < 1 || $input['per_page'] > 100 || ! is_string( $input['status'] ) || ! in_array( $input['status'], array_merge( [ '' ], $this->statuses() ), true ) || ! is_string( $input['search'] ) ) { return $this->invalid_input(); } $page = absint( $input['page'] ); $per_page = absint( $input['per_page'] ); $start = ( $page - 1 ) * $per_page; $total = 0; $forms = []; $batch = 1; do { // Scan IDs in bounded batches: counts must exclude inaccessible objects. $ids = get_posts( [ 'post_type' => Form_Builder::ADMIN_POST_TYPE, 'post_status' => '' === $input['status'] ? $this->statuses() : sanitize_text_field( $input['status'] ), 'posts_per_page' => 100, 'paged' => $batch++, 'fields' => 'ids', 'orderby' => 'ID', 'order' => 'ASC', 's' => sanitize_text_field( $input['search'] ), 'search_columns' => [ 'post_title' ], 'update_post_meta_cache' => false, 'update_post_term_cache' => false, ] ); if ( ! is_array( $ids ) ) { return new \WP_Error( 'ao_list_forms_query_failed', __( 'Unable to list forms.', 'admin-optimizer' ) ); } foreach ( $ids as $id ) { $id = absint( $id ); if ( ! Form_Builder::can_edit_form( $id ) ) { continue; } if ( $total++ < $start || count( $forms ) >= $per_page ) { continue; } $post = get_post( $id ); if ( ! $post instanceof \WP_Post ) { continue; } $form = [ 'id' => $id, 'title' => sanitize_text_field( $post->post_title ), 'status' => $post->post_status, 'created_at' => str_replace( ' ', 'T', $post->post_date ), 'modified_at' => str_replace( ' ', 'T', $post->post_modified ), ]; $url = get_edit_post_link( $id, 'raw' ); if ( is_string( $url ) && '' !== $url ) { $form['edit_url'] = esc_url_raw( $url ); } $forms[] = $form; } } while ( count( $ids ) === 100 ); return [ 'forms' => $forms, 'pagination' => [ 'page' => $page, 'per_page' => $per_page, 'total' => $total, 'total_pages' => (int) ceil( $total / $per_page ) ] ]; } /** Invalid direct-call input uses the native WordPress error shape. */ private function invalid_input(): \WP_Error { return new \WP_Error( 'ao_list_forms_invalid_input', __( 'Invalid form listing parameters.', 'admin-optimizer' ) ); } }