|
4 | 4 |
|
5 | 5 | use WP_CLI; |
6 | 6 | use WP_CLI_Command; |
| 7 | +use WP_Community_Events; |
| 8 | +use WP_Error; |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * |
@@ -122,6 +124,96 @@ private function register_tools($server, $client) { |
122 | 124 | }, |
123 | 125 | ] |
124 | 126 | ); |
| 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 | + |
125 | 217 | } |
126 | 218 |
|
127 | 219 | // Register resources for AI access |
|
0 commit comments