Skip to content

Commit 4609ab3

Browse files
Add tool to find wordpress events near a location (#5)
* Add tool to find wordpress events near a location * Fixed event list to work in other countries
1 parent f434586 commit 4609ab3

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/AiCommand.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use WP_CLI;
66
use WP_CLI_Command;
7+
use WP_Community_Events;
8+
use WP_Error;
79

810
/**
911
*
@@ -122,6 +124,96 @@ private function register_tools($server, $client) {
122124
},
123125
]
124126
);
127+
128+
$server->register_tool(
129+
[
130+
'name' => 'fetch_wp_community_events',
131+
'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.',
132+
'inputSchema' => [
133+
'type' => 'object',
134+
'properties' => [
135+
'location' => [
136+
'type' => 'string',
137+
'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).',
138+
],
139+
],
140+
'required' => [ 'location' ], // We only require the location
141+
],
142+
'callable' => function ( $params ) {
143+
// Default user ID is 0
144+
$user_id = 0;
145+
146+
// Get the location from the parameters (already supplied in the prompt)
147+
$location_input = strtolower( trim( $params['location'] ) );
148+
149+
// Manually include the WP_Community_Events class if it's not loaded
150+
if ( ! class_exists( 'WP_Community_Events' ) ) {
151+
require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
152+
}
153+
154+
// Determine location for the WP_Community_Events class
155+
$location = null;
156+
if ( $location_input !== 'near me' ) {
157+
// Provide city name (WP will resolve coordinates)
158+
$location = [
159+
'description' => $location_input,
160+
];
161+
}
162+
163+
// Instantiate WP_Community_Events with user ID (0) and optional location
164+
$events_instance = new WP_Community_Events( $user_id, $location );
165+
166+
// Get events from WP_Community_Events
167+
$events = $events_instance->get_events($location_input);
168+
169+
// Check for WP_Error
170+
if ( is_wp_error( $events ) ) {
171+
return [ 'error' => $events->get_error_message() ];
172+
}
173+
174+
// If no events found
175+
if ( empty( $events['events'] ) ) {
176+
return [ 'message' => 'No events found near ' . ( $location_input === 'near me' ? 'your location' : $location_input ) ];
177+
}
178+
179+
// Format and return the events correctly
180+
$formatted_events = array_map( function ( $event ) {
181+
// Log event details to ensure properties are accessible
182+
error_log( 'Event details: ' . print_r( $event, true ) );
183+
184+
// Initialize a formatted event string
185+
$formatted_event = '';
186+
187+
// Format event title
188+
if ( isset( $event['title'] ) ) {
189+
$formatted_event .= $event['title'] . "\n";
190+
}
191+
192+
// Format the date nicely
193+
$formatted_event .= ' - Date: ' . ( isset( $event['date'] ) ? date( 'F j, Y g:i A', strtotime( $event['date'] ) ) : 'No date available' ) . "\n";
194+
195+
// Format the location
196+
if ( isset( $event['location']['location'] ) ) {
197+
$formatted_event .= ' - Location: ' . $event['location']['location'] . "\n";
198+
}
199+
200+
// Format the event URL
201+
$formatted_event .= isset( $event['url'] ) ? ' - URL: ' . $event['url'] . "\n" : '';
202+
203+
return $formatted_event;
204+
}, $events['events'] );
205+
206+
// Combine the formatted events into a single string
207+
$formatted_events_output = implode("\n", $formatted_events);
208+
209+
// Return the formatted events string
210+
return [
211+
'message' => "OK. I found " . count($formatted_events) . " WordPress events near " . ( $location_input === 'near me' ? 'your location' : $location_input ) . ":\n\n" . $formatted_events_output
212+
];
213+
},
214+
]
215+
);
216+
125217
}
126218

127219
// Register resources for AI access

0 commit comments

Comments
 (0)