From 57f12ef7530fab384493729a2f8cfa1a63e3e405 Mon Sep 17 00:00:00 2001 From: Moritz Bappert Date: Sat, 15 Mar 2025 18:51:32 +0100 Subject: [PATCH 1/2] Add tool to find wordpress events near a location --- src/AiCommand.php | 95 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/AiCommand.php b/src/AiCommand.php index 6687fcc..a6b6677 100644 --- a/src/AiCommand.php +++ b/src/AiCommand.php @@ -4,6 +4,8 @@ use WP_CLI; use WP_CLI_Command; +use WP_Community_Events; +use WP_Error; /** * @@ -122,6 +124,99 @@ private function register_tools($server, $client) { }, ] ); + + $server->register_tool( + [ + 'name' => 'fetch_wp_community_events', + 'description' => 'Fetches upcoming WordPress community events near a specified city or the user\'s current location. If no events are found in the exact location, nearby events within a specific radius will be considered.', + 'inputSchema' => [ + 'type' => 'object', + 'properties' => [ + 'location' => [ + 'type' => 'string', + 'description' => 'City name or "near me" for auto-detected location. If no events are found in the exact location, the tool will also consider nearby events within a specified radius (default: 100 km).', + ], + ], + 'required' => [ 'location' ], // We only require the location + ], + 'callable' => function ( $params ) { + // Default user ID is 0 + $user_id = 0; + + // Get the location from the parameters (already supplied in the prompt) + $location_input = strtolower( trim( $params['location'] ) ); + + // Manually include the WP_Community_Events class if it's not loaded + if ( ! class_exists( 'WP_Community_Events' ) ) { + require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php'; + } + + // Determine location for the WP_Community_Events class + $location = null; + if ( $location_input !== 'near me' ) { + // Provide city name (WP will resolve coordinates) + $location = [ + 'latitude' => null, // WP will attempt geolocation + 'longitude' => null, + 'city' => $location_input, + 'country' => '', // Optional, WP may infer + ]; + } + + // Instantiate WP_Community_Events with user ID (0) and optional location + $events_instance = new WP_Community_Events( $user_id, $location ); + + // Get events from WP_Community_Events + $events = $events_instance->get_events(); + + // Check for WP_Error + if ( is_wp_error( $events ) ) { + return [ 'error' => $events->get_error_message() ]; + } + + // If no events found + if ( empty( $events['events'] ) ) { + return [ 'message' => 'No events found near ' . ( $location_input === 'near me' ? 'your location' : $location_input ) ]; + } + + // Format and return the events correctly + $formatted_events = array_map( function ( $event ) { + // Log event details to ensure properties are accessible + error_log( 'Event details: ' . print_r( $event, true ) ); + + // Initialize a formatted event string + $formatted_event = ''; + + // Format event title + if ( isset( $event['title'] ) ) { + $formatted_event .= $event['title'] . "\n"; + } + + // Format the date nicely + $formatted_event .= ' - Date: ' . ( isset( $event['date'] ) ? date( 'F j, Y g:i A', strtotime( $event['date'] ) ) : 'No date available' ) . "\n"; + + // Format the location + if ( isset( $event['location']['location'] ) ) { + $formatted_event .= ' - Location: ' . $event['location']['location'] . "\n"; + } + + // Format the event URL + $formatted_event .= isset( $event['url'] ) ? ' - URL: ' . $event['url'] . "\n" : ''; + + return $formatted_event; + }, $events['events'] ); + + // Combine the formatted events into a single string + $formatted_events_output = implode("\n", $formatted_events); + + // Return the formatted events string + return [ + 'message' => "OK. I found " . count($formatted_events) . " WordPress events near " . ( $location_input === 'near me' ? 'your location' : $location_input ) . ":\n\n" . $formatted_events_output + ]; + }, + ] + ); + } // Register resources for AI access From 14aae17431b6eead004d412b30f608978d19fd19 Mon Sep 17 00:00:00 2001 From: Moritz Bappert Date: Sat, 15 Mar 2025 19:12:52 +0100 Subject: [PATCH 2/2] Fixed event list to work in other countries --- src/AiCommand.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/AiCommand.php b/src/AiCommand.php index a6b6677..91e4f7c 100644 --- a/src/AiCommand.php +++ b/src/AiCommand.php @@ -156,10 +156,7 @@ private function register_tools($server, $client) { if ( $location_input !== 'near me' ) { // Provide city name (WP will resolve coordinates) $location = [ - 'latitude' => null, // WP will attempt geolocation - 'longitude' => null, - 'city' => $location_input, - 'country' => '', // Optional, WP may infer + 'description' => $location_input, ]; } @@ -167,7 +164,7 @@ private function register_tools($server, $client) { $events_instance = new WP_Community_Events( $user_id, $location ); // Get events from WP_Community_Events - $events = $events_instance->get_events(); + $events = $events_instance->get_events($location_input); // Check for WP_Error if ( is_wp_error( $events ) ) {