Changeset 5146
- Timestamp:
- 03/10/2017 08:00:51 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php
r5145 r5146 11 11 const EVENTS_TABLE = 'wporg_events'; 12 12 const WORDCAMP_API_BASE_URL = 'https://central.wordcamp.org/wp-json/'; 13 const WORDCAMP_API_VERSION = 2;14 13 const MEETUP_API_BASE_URL = 'https://api.meetup.com/'; 15 14 const MEETUP_MEMBER_ID = 72560962; … … 208 207 209 208 /** 210 * Generate the WordCamps endpoint URL for a particular version of the REST API.211 *212 * @param int $api_version213 *214 * @return string215 */216 protected function get_wordcamp_events_endpoint( $api_version = 1 ) {217 switch ( $api_version ) {218 case 1 :219 default :220 $request_params = array(221 'type' => 'wordcamp',222 );223 $endpoint = add_query_arg( $request_params, self::WORDCAMP_API_BASE_URL . 'posts' );224 break;225 226 case 2 :227 $request_params = array(228 'status' => 'wcpt-scheduled',229 'per_page' => 100,230 // todo 100 is the built-in limit for per_page. As the number of WordCamps per year grows, we may need to increase this. See https://github.com/WP-API/WP-API/issues/2914#issuecomment-266222585231 );232 $endpoint = add_query_arg( $request_params, self::WORDCAMP_API_BASE_URL . 'wp/v2/wordcamps' );233 break;234 }235 236 return $endpoint;237 }238 239 /**240 209 * Retrieve events fromm the WordCamp.org API 241 210 * … … 243 212 */ 244 213 protected function get_wordcamp_events() { 245 $endpoint = $this->get_wordcamp_events_endpoint( self::WORDCAMP_API_VERSION ); 214 $request_params = array( 215 'status' => 'wcpt-scheduled', 216 'per_page' => 100, 217 // Note: With the number of WordCamps per year growing fast, we may need to batch requests in the near future, like we do for meetups 218 ); 219 220 $endpoint = add_query_arg( $request_params, self::WORDCAMP_API_BASE_URL . 'wp/v2/wordcamps' ); 246 221 $response = $this->remote_get( esc_url_raw( $endpoint ) ); 247 248 switch ( self::WORDCAMP_API_VERSION ) { 249 case 1 : 250 default : 251 $events = $this->parse_wordcamp_events_api_v1( $response ); 252 break; 253 254 case 2 : 255 $events = $this->parse_wordcamp_events_api_v2( $response ); 256 break; 257 } 222 $events = $this->parse_wordcamp_events( $response ); 258 223 259 224 $this->log( sprintf( 'returning %d events', count( $events ) ) ); … … 263 228 264 229 /** 265 * Parse a response from the v1 API. 266 * 267 * @param $response 268 * 269 * @return array 270 */ 271 protected function parse_wordcamp_events_api_v1( $response ) { 272 $events = array(); 273 $wordcamps = json_decode( wp_remote_retrieve_body( $response ) ); 274 275 if ( $wordcamps ) { 276 foreach ( $wordcamps as $wordcamp ) { 277 if ( empty( $wordcamp->post_meta ) ) { 278 continue; 279 } 280 281 $event = array( 282 'type' => 'wordcamp', 283 'title' => $wordcamp->title, 284 ); 285 286 foreach ( $wordcamp->post_meta as $meta_item ) { 287 switch ( $meta_item->key ) { 288 case 'Start Date (YYYY-mm-dd)': 289 if ( empty( $meta_item->value ) || $meta_item->value < time() ) { 290 // todo this can be removed when we're able to filter the request by post meta (see above) 291 292 continue 3; 293 } else { 294 $event['start_timestamp'] = $meta_item->value; 295 } 296 break; 297 298 case 'End Date (YYYY-mm-dd)': 299 $event['end_timestamp'] = $meta_item->value; 300 break; 301 302 case 'URL': 303 case 'Location': 304 $event[ strtolower( $meta_item->key ) ] = $meta_item->value; 305 break; 306 } 307 } 308 309 if ( ! empty( $event['url'] ) ) { 310 $events[] = new Official_WordPress_Event( $event ); 311 } 312 } 313 } 314 315 return $events; 316 } 317 318 /** 319 * Parse a response from the v2 API. 320 * 321 * This does additional sorting of the returned events that the v1 parser doesn't do. 322 * 323 * @param $response 324 * 325 * @return array 326 */ 327 protected function parse_wordcamp_events_api_v2( $response ) { 230 * Parse an event response from the WordCamp.org API. 231 * 232 * @param array $response 233 * 234 * @return array 235 */ 236 protected function parse_wordcamp_events( $response ) { 328 237 $events = array(); 329 238 $wordcamps = json_decode( wp_remote_retrieve_body( $response ) );
Note: See TracChangeset
for help on using the changeset viewer.