Skip to content

Commit efb3810

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 5872fb1 + 4609ab3 commit efb3810

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
*
@@ -150,6 +152,96 @@ private function register_tools($server, $client) {
150152
},
151153
]
152154
);
155+
156+
$server->register_tool(
157+
[
158+
'name' => 'fetch_wp_community_events',
159+
'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.',
160+
'inputSchema' => [
161+
'type' => 'object',
162+
'properties' => [
163+
'location' => [
164+
'type' => 'string',
165+
'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).',
166+
],
167+
],
168+
'required' => [ 'location' ], // We only require the location
169+
],
170+
'callable' => function ( $params ) {
171+
// Default user ID is 0
172+
$user_id = 0;
173+
174+
// Get the location from the parameters (already supplied in the prompt)
175+
$location_input = strtolower( trim( $params['location'] ) );
176+
177+
// Manually include the WP_Community_Events class if it's not loaded
178+
if ( ! class_exists( 'WP_Community_Events' ) ) {
179+
require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
180+
}
181+
182+
// Determine location for the WP_Community_Events class
183+
$location = null;
184+
if ( $location_input !== 'near me' ) {
185+
// Provide city name (WP will resolve coordinates)
186+
$location = [
187+
'description' => $location_input,
188+
];
189+
}
190+
191+
// Instantiate WP_Community_Events with user ID (0) and optional location
192+
$events_instance = new WP_Community_Events( $user_id, $location );
193+
194+
// Get events from WP_Community_Events
195+
$events = $events_instance->get_events($location_input);
196+
197+
// Check for WP_Error
198+
if ( is_wp_error( $events ) ) {
199+
return [ 'error' => $events->get_error_message() ];
200+
}
201+
202+
// If no events found
203+
if ( empty( $events['events'] ) ) {
204+
return [ 'message' => 'No events found near ' . ( $location_input === 'near me' ? 'your location' : $location_input ) ];
205+
}
206+
207+
// Format and return the events correctly
208+
$formatted_events = array_map( function ( $event ) {
209+
// Log event details to ensure properties are accessible
210+
error_log( 'Event details: ' . print_r( $event, true ) );
211+
212+
// Initialize a formatted event string
213+
$formatted_event = '';
214+
215+
// Format event title
216+
if ( isset( $event['title'] ) ) {
217+
$formatted_event .= $event['title'] . "\n";
218+
}
219+
220+
// Format the date nicely
221+
$formatted_event .= ' - Date: ' . ( isset( $event['date'] ) ? date( 'F j, Y g:i A', strtotime( $event['date'] ) ) : 'No date available' ) . "\n";
222+
223+
// Format the location
224+
if ( isset( $event['location']['location'] ) ) {
225+
$formatted_event .= ' - Location: ' . $event['location']['location'] . "\n";
226+
}
227+
228+
// Format the event URL
229+
$formatted_event .= isset( $event['url'] ) ? ' - URL: ' . $event['url'] . "\n" : '';
230+
231+
return $formatted_event;
232+
}, $events['events'] );
233+
234+
// Combine the formatted events into a single string
235+
$formatted_events_output = implode("\n", $formatted_events);
236+
237+
// Return the formatted events string
238+
return [
239+
'message' => "OK. I found " . count($formatted_events) . " WordPress events near " . ( $location_input === 'near me' ? 'your location' : $location_input ) . ":\n\n" . $formatted_events_output
240+
];
241+
},
242+
]
243+
);
244+
153245
}
154246

155247
// Register resources for AI access

0 commit comments

Comments
 (0)