set_args()->create()/save()), using the * repository's OWN field aliases - confirmed directly from * Tribe__Events__Repositories__Venue's constructor (`venue` => post_title, `address` => * _VenueAddress, `city` => _VenueCity, `state_province` => _VenueStateProvince, `zip` => * _VenueZip, `country` => _VenueCountry, `phone` => _VenuePhone, `website` => _VenueURL) - NOT * the title-case shape the legacy tribe_create_venue()/tribe_update_venue() wrappers translate * internally, which the ORM's own alias table does not recognize. * * @package AgentAbilitiesForMCP */ declare( strict_types=1 ); defined( 'ABSPATH' ) || exit; add_filter( 'aafm_abilities_registry', 'aafm_register_tec_venues_definitions' ); add_filter( 'aafm_abilities_registry_integrations', 'aafm_register_tec_venues_full_definitions' ); /** * Contribute TEC venue ability definitions, only while TEC is active. * * @param array> $registry Registry. * @return array> */ function aafm_register_tec_venues_definitions( array $registry ): array { if ( ! aafm_integration_active( 'tec' ) ) { return $registry; } return array_merge( $registry, aafm_tec_venues_registry_definitions() ); } /** * Contribute TEC venue ability definitions to the host-independent full registry. * * @param array> $registry Registry. * @return array> */ function aafm_register_tec_venues_full_definitions( array $registry ): array { return array_merge( $registry, aafm_tec_venues_registry_definitions() ); } /** * The four TEC venue ability rows. * * @return array> */ function aafm_tec_venues_registry_definitions(): array { return array( 'aafm/tec-get-venues' => array( 'label' => __( 'Get venues', 'agent-abilities-for-mcp' ), 'description' => __( 'List venues via the Events Calendar ORM (tribe_venues()), optionally filtered by status (by default what you can already see; draft, pending and future need edit access to venues).', 'agent-abilities-for-mcp' ), 'group' => 'reads', 'risk' => 'read', 'subject' => 'tec', 'args_builder' => 'aafm_args_tec_get_venues', ), 'aafm/tec-get-venue' => array( 'label' => __( 'Get venue', 'agent-abilities-for-mcp' ), 'description' => __( 'Read a single venue by ID.', 'agent-abilities-for-mcp' ), 'group' => 'reads', 'risk' => 'read', 'subject' => 'tec', 'args_builder' => 'aafm_args_tec_get_venue', ), 'aafm/tec-create-venue' => array( 'label' => __( 'Create venue', 'agent-abilities-for-mcp' ), 'description' => __( 'Create a venue via the Events Calendar ORM. Requires the edit_tribe_venues capability. Defaults to draft; publish requires the publish_tribe_venues capability.', 'agent-abilities-for-mcp' ), 'group' => 'writes', 'risk' => 'write', 'subject' => 'tec', 'args_builder' => 'aafm_args_tec_create_venue', ), 'aafm/tec-update-venue' => array( 'label' => __( 'Update venue', 'agent-abilities-for-mcp' ), 'description' => __( 'Update a venue by ID via the Events Calendar ORM. Requires edit access to that venue. Setting status to publish requires the publish_tribe_venues capability.', 'agent-abilities-for-mcp' ), 'group' => 'writes', 'risk' => 'write', 'subject' => 'tec', 'args_builder' => 'aafm_args_tec_update_venue', ), ); } /** * The wire shape for one venue. * * @param int $id Venue (post) id. * @return array */ function aafm_tec_venue_shape( int $id ): array { $post = aafm_exact_object( 'post', $id ); return array( 'id' => $id, 'title' => $post instanceof WP_Post ? get_the_title( $post ) : '', 'status' => $post instanceof WP_Post ? (string) $post->post_status : '', 'address' => (string) aafm_meta_get( 'post', $id, '_VenueAddress', true ), 'city' => (string) aafm_meta_get( 'post', $id, '_VenueCity', true ), 'state' => (string) aafm_meta_get( 'post', $id, '_VenueStateProvince', true ), 'zip' => (string) aafm_meta_get( 'post', $id, '_VenueZip', true ), 'country' => (string) aafm_meta_get( 'post', $id, '_VenueCountry', true ), 'phone' => (string) aafm_meta_get( 'post', $id, '_VenuePhone', true ), 'website' => (string) aafm_meta_get( 'post', $id, '_VenueURL', true ), ); } /** * Shared venue input properties (create + update). * * @return array> */ function aafm_tec_venue_input_properties(): array { return array( 'title' => array( 'type' => 'string', 'minLength' => 1, 'description' => __( 'Venue name.', 'agent-abilities-for-mcp' ), ), 'address' => array( 'type' => 'string', 'description' => __( 'Street address.', 'agent-abilities-for-mcp' ), ), 'city' => array( 'type' => 'string', 'description' => __( 'City.', 'agent-abilities-for-mcp' ), ), 'state' => array( 'type' => 'string', 'description' => __( 'State or province.', 'agent-abilities-for-mcp' ), ), 'zip' => array( 'type' => 'string', 'description' => __( 'Postal/zip code.', 'agent-abilities-for-mcp' ), ), 'country' => array( 'type' => 'string', 'description' => __( 'Country.', 'agent-abilities-for-mcp' ), ), 'phone' => array( 'type' => 'string', 'description' => __( 'Phone number.', 'agent-abilities-for-mcp' ), ), 'website' => array( 'type' => 'string', 'format' => 'uri', 'description' => __( 'Website URL.', 'agent-abilities-for-mcp' ), ), 'status' => array( 'type' => 'string', 'enum' => array( 'draft', 'publish' ), 'default' => 'draft', 'description' => __( 'Publication status. Defaults to draft. Setting publish requires the publish_tribe_venues capability.', 'agent-abilities-for-mcp' ), ), ); } /** * Translate this ability's lowercase input into the venue repository's own field aliases. * * @param array $input Validated input. * @return array */ function aafm_tec_venue_orm_args( array $input ): array { $args = array(); if ( isset( $input['title'] ) ) { $args['venue'] = aafm_sanitize_plain_text( (string) $input['title'] ); } foreach ( array( 'address', 'city', 'zip', 'country', 'phone' ) as $field ) { if ( isset( $input[ $field ] ) ) { $args[ $field ] = aafm_sanitize_plain_text( (string) $input[ $field ] ); } } if ( isset( $input['state'] ) ) { $args['state_province'] = aafm_sanitize_plain_text( (string) $input['state'] ); } if ( isset( $input['website'] ) ) { $args['website'] = esc_url_raw( (string) $input['website'] ); } return $args; } /** * Args for aafm/tec-get-venues. * * @return array */ function aafm_args_tec_get_venues(): array { return array( 'label' => aafm_ability_label( 'aafm/tec-get-venues' ), 'description' => aafm_ability_description( 'aafm/tec-get-venues' ), 'category' => 'aafm-reads', 'input_schema' => array( 'type' => 'object', 'properties' => array_merge( array( 'status' => array( 'type' => 'string', 'description' => __( 'Post status to filter by. Defaults to whatever the caller can already see (publish, plus private with read-private access); draft/pending/future require edit access to venues (scoped to your own unless you can edit others\' venues).', 'agent-abilities-for-mcp' ), ), ), aafm_pagination_schema_props( AAFM_LIST_PER_PAGE_MAX, __( 'Number of venues per page, clamped to the 1-50 range. Defaults to 10 when omitted.', 'agent-abilities-for-mcp' ), __( '1-based page number for pagination. Defaults to 1.', 'agent-abilities-for-mcp' ) ) ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'venues' => array( 'type' => 'array', 'items' => array( 'type' => 'object' ), ), 'total' => array( 'type' => 'integer' ), ), ), 'execute_callback' => 'aafm_exec_tec_get_venues', 'permission_callback' => 'aafm_perm_read', 'meta' => array( 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ), ), ); } /** * Execute aafm/tec-get-venues. * * @param array $input Validated input. * @return array|WP_Error */ function aafm_exec_tec_get_venues( array $input ) { $resolved = aafm_tec_resolve_list_status( isset( $input['status'] ) ? sanitize_key( (string) $input['status'] ) : '', Tribe__Events__Venue::POSTTYPE ); if ( is_wp_error( $resolved ) ) { return $resolved; } $paging = aafm_paginate_args( $input, AAFM_LIST_PER_PAGE_MAX ); $repo = tribe_venues() ->where( 'post_status', $resolved['status'] ) ->page( $paging['page'] ) ->per_page( $paging['per_page'] ); if ( $resolved['own_only'] ) { $repo = $repo->where( 'author', get_current_user_id() ); } $ids = $repo->get_ids(); return array( 'venues' => array_map( 'aafm_tec_venue_shape', array_map( 'intval', $ids ) ), 'total' => (int) $repo->found(), ); } /** * Args for aafm/tec-get-venue. * * @return array */ function aafm_args_tec_get_venue(): array { return array( 'label' => aafm_ability_label( 'aafm/tec-get-venue' ), 'description' => aafm_ability_description( 'aafm/tec-get-venue' ), 'category' => 'aafm-reads', 'input_schema' => array( 'type' => 'object', 'properties' => array( 'venue_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => __( 'ID of the venue to read.', 'agent-abilities-for-mcp' ), ), ), 'required' => array( 'venue_id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'venue' => array( 'type' => 'object' ) ), ), 'execute_callback' => 'aafm_exec_tec_get_venue', 'permission_callback' => 'aafm_tec_perm_read_venue', 'meta' => array( 'annotations' => array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ), ), ); } /** * Execute aafm/tec-get-venue. * * @param array $input Validated input. * @return array|WP_Error */ function aafm_exec_tec_get_venue( array $input ) { $id = absint( $input['venue_id'] ?? 0 ); $post = $id ? aafm_exact_object( 'post', $id ) : null; if ( ! $post instanceof WP_Post || Tribe__Events__Venue::POSTTYPE !== $post->post_type ) { return aafm_generic_error(); } return array( 'venue' => aafm_tec_venue_shape( $id ) ); } /** * Args for aafm/tec-create-venue. * * @return array */ function aafm_args_tec_create_venue(): array { return array( 'label' => aafm_ability_label( 'aafm/tec-create-venue' ), 'description' => aafm_ability_description( 'aafm/tec-create-venue' ), 'category' => 'aafm-writes', 'input_schema' => array( 'type' => 'object', 'properties' => aafm_tec_venue_input_properties(), 'required' => array( 'title' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'venue' => array( 'type' => 'object' ) ), ), 'execute_callback' => 'aafm_exec_tec_create_venue', 'permission_callback' => 'aafm_tec_perm_create_venue', 'meta' => array( 'annotations' => array( 'readonly' => false, 'destructive' => false, ), ), ); } /** * Execute aafm/tec-create-venue. * * @param array $input Validated input. * @return array|WP_Error */ function aafm_exec_tec_create_venue( array $input ) { $status = aafm_resolve_create_status( $input, 'draft', aafm_tec_venue_publish_cap() ); if ( is_wp_error( $status ) ) { return $status; } $checked = aafm_tec_checked_website( $input ); if ( is_wp_error( $checked ) ) { return $checked; } $args = aafm_tec_venue_orm_args( $checked ); $args['post_status'] = $status; $safety = aafm_tec_enforce_content_safety( $args, 'venue' ); if ( is_wp_error( $safety ) ) { return $safety; } $created = aafm_tec_write( 'venues', $args )['returned']; if ( ! $created instanceof WP_Post ) { return aafm_generic_error(); } $created_id = (int) $created->ID; if ( ! aafm_exact_object( 'post', $created_id ) instanceof WP_Post ) { return aafm_generic_error(); } return aafm_with_checked_reads( static fn(): array => array( 'venue' => aafm_tec_venue_shape( $created_id ) ), aafm_generic_error() ); } /** * Args for aafm/tec-update-venue. * * @return array */ function aafm_args_tec_update_venue(): array { $properties = aafm_tec_venue_input_properties(); $properties['title'] = array( 'type' => 'string', 'description' => __( 'Venue name. Optional on update, required only on create.', 'agent-abilities-for-mcp' ), ); unset( $properties['status']['default'] ); $properties['status']['description'] = __( 'Publication status. Leave it out to keep the venue\'s current status. Setting publish requires the publish_tribe_venues capability.', 'agent-abilities-for-mcp' ); $properties['venue_id'] = array( 'type' => 'integer', 'minimum' => 1, 'description' => __( 'ID of the venue to update.', 'agent-abilities-for-mcp' ), ); return array( 'label' => aafm_ability_label( 'aafm/tec-update-venue' ), 'description' => aafm_ability_description( 'aafm/tec-update-venue' ), 'category' => 'aafm-writes', 'input_schema' => array( 'type' => 'object', 'properties' => $properties, 'required' => array( 'venue_id' ), 'additionalProperties' => false, ), 'output_schema' => array( 'type' => 'object', 'properties' => array( 'venue' => array( 'type' => 'object' ) ), ), 'execute_callback' => 'aafm_exec_tec_update_venue', 'permission_callback' => 'aafm_tec_perm_edit_venue', 'meta' => array( 'annotations' => array( 'readonly' => false, 'destructive' => false, ), ), ); } /** * Execute aafm/tec-update-venue. * * @param array $input Validated input. * @return array|WP_Error */ function aafm_exec_tec_update_venue( array $input ) { $id = absint( $input['venue_id'] ?? 0 ); if ( $id < 1 ) { return aafm_generic_error(); } $checked = aafm_tec_checked_website( $input ); if ( is_wp_error( $checked ) ) { return $checked; } $args = aafm_tec_venue_orm_args( $checked ); if ( isset( $input['status'] ) ) { $status = aafm_authorize_post_status( (string) $input['status'], aafm_tec_venue_publish_cap(), null, aafm_current_post_status( $id ) ); if ( is_wp_error( $status ) ) { return $status; } $args['post_status'] = $status; } if ( array() !== $args ) { $safety = aafm_tec_enforce_content_safety( $args, 'venue' ); if ( is_wp_error( $safety ) ) { return $safety; } $result = aafm_tec_write( 'venues', $args, $id )['returned']; if ( empty( $result[ $id ] ) || is_wp_error( $result[ $id ] ) ) { return aafm_generic_error(); } } if ( ! aafm_exact_object( 'post', $id ) instanceof WP_Post ) { return aafm_generic_error(); } return aafm_with_checked_reads( static fn(): array => array( 'venue' => aafm_tec_venue_shape( $id ) ), aafm_generic_error() ); }