WP_Community_Events::get_events( string $location_search = '', string $timezone = '' ): array|WP_Error
- Since
- 4.8.0, 5.5.2
- Source
wp-admin/includes/class-wp-community-events.php:91
Description
Cached events will be immediately returned if the user_location property is set for the current user, and cached events exist for that location.
Otherwise, this method sends a request to the w.org Events API with location data. The API will send back a recognized location based on the data, along with nearby events.
The browser's request for events is proxied with this method, rather than having the browser make the request directly to api.wordpress.org, because it allows results to be cached server-side and shared with other users and sites in the network. This makes the process more efficient, since increasing the number of visits that get cached data means users don't have to wait as often; if the user's browser made the request directly, it would also need to make a second request to WP in order to pass the data for caching. Having WP make the request also introduces the opportunity to anonymize the IP before sending it to w.org, which mitigates possible privacy concerns.
Compatibility
- WordPress
- since 5.5.2
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$location_searchstringoptional- City name to help determine the location.
e.g., "Seattle". Default empty string.Default:'' $timezonestringoptional- Timezone to help determine the location.
Default empty string.Default:''
Return value
array|WP_Error- A WP_Error on failure; an array with location and events on success.
Performance profile
How much work a call to WP_Community_Events::get_events() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Expensive
- Scaling
- Constant
- Instructions
- 8–121
- Plugin surface
- None
- Called by
- 0
Reaches an outbound HTTP request via wp_remote_get().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 140.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- httpoutbound HTTP request
wp_remote_get()called directly - serializeserialisation
json_decode()called directly - hookthird-party callbacks
apply_filters()one call below WP_Community_Events::get_events()
Further down the call graph this can also reach option, cache, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 13 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Community_Events::get_events() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | ->get_cached_events() |
!wp_http_supports() && is_wp_error() | 58–72 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), is_wp_error() |
wp_http_supports() && is_wp_error() | 63–77 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), is_wp_error() |
!wp_http_supports() && $response_code === 200 && !isset($response_body) | 71–74 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), is_wp_error() |
!wp_http_supports() && $response_code !== 200 | 71–72 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), sprintf(), is_wp_error() |
wp_http_supports() && $response_code === 200 && !isset($response_body) | 76–79 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), is_wp_error() |
wp_http_supports() && $response_code !== 200 | 76–77 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), sprintf(), is_wp_error() |
!wp_http_supports() && !is_wp_error() | 84–114 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), is_wp_error(), ->coordinates_match(), ->cache_events(), ->trim_events() |
wp_http_supports() && !is_wp_error() | 89–119 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), is_wp_error(), ->coordinates_match(), ->cache_events(), ->trim_events() |
!wp_http_supports() && !is_wp_error() && $response_code === 200 && !isset($response_body) | 97–116 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), is_wp_error(), ->coordinates_match(), ->cache_events(), ->trim_events() |
!wp_http_supports() && !is_wp_error() && $response_code !== 200 | 97–114 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), sprintf(), is_wp_error(), ->coordinates_match(), ->cache_events(), ->trim_events() |
wp_http_supports() && !is_wp_error() && $response_code === 200 && !isset($response_body) | 102–121 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), is_wp_error(), ->coordinates_match(), ->cache_events(), ->trim_events() |
1 further outcome, up to 119 instructions
wp_http_supports() && !is_wp_error() && $response_code !== 200 | 102–119 | ->get_cached_events(), ->get_request_args(), home_url(), wp_http_supports(), set_url_scheme(), wp_remote_get(), wp_remote_retrieve_response_code(), wp_remote_retrieve_body(), json_decode(), is_wp_error(), __(), sprintf(), is_wp_error(), ->coordinates_match(), ->cache_events(), ->trim_events() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 139 | 8–120 | 13 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 140 | 8–121 | 13 | |
| 8.4 | 140 | 8–121 | 13 | |
| 8.3 | 140 | 8–121 | 13 | |
| 8.2 | 140 | 8–121 | 13 | |
| 8.1 | 140 | 8–121 | 13 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 141 | 8–121 | 13 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 14
- home_url()Retrieves the URL for the current site where the front end is accessible.
- wp_http_supports()Determines if there is an HTTP Transport that can process this request.
- set_url_scheme()Sets the scheme for a URL.
- wp_remote_get()Performs an HTTP request using the GET method and returns its response.
- wp_remote_retrieve_response_code()Retrieves only the response code from the raw response.
- wp_remote_retrieve_body()Retrieves only the body from the raw response.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- WP_Community_Events::get_cached_events()Gets cached events.
- WP_Community_Events::get_request_args()Builds an array of args to use in an HTTP request to the w.org Events API.
- WP_Error::__construct()Initializes the error.
- WP_Community_Events::coordinates_match()Test if two pairs of latitude/longitude coordinates match each other.
Show all 14
- WP_Community_Events::cache_events()Caches an array of events data from the Events API.
- WP_Community_Events::trim_events()Prepares the event list for presentation.
Source code
public function get_events( $location_search = '', $timezone = '' ) { $cached_events = $this->get_cached_events(); if ( ! $location_search && $cached_events ) { return $cached_events; } // Include an unmodified $wp_version. require ABSPATH . WPINC . '/version.php'; $api_url = 'http://api.wordpress.org/events/1.0/'; $request_args = $this->get_request_args( $location_search, $timezone ); $request_args['user-agent'] = 'WordPress/' . $wp_version . '; ' . home_url( '/' ); if ( wp_http_supports( array( 'ssl' ) ) ) { $api_url = set_url_scheme( $api_url, 'https' ); } $response = wp_remote_get( $api_url, $request_args ); $response_code = wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); $response_error = null; if ( is_wp_error( $response ) ) { $response_error = $response; } elseif ( 200 !== $response_code ) { $response_error = new WP_Error( 'api-error', /* translators: %d: Numeric HTTP status code, e.g. 400, 403, 500, 504, etc. */ sprintf( __( 'Invalid API response code (%d).' ), $response_code ) ); } elseif ( ! isset( $response_body['location'], $response_body['events'] ) ) { $response_error = new WP_Error( 'api-invalid-response', isset( $response_body['error'] ) ? $response_body['error'] : __( 'Unknown API error.' ) ); } if ( is_wp_error( $response_error ) ) { return $response_error; } else { $expiration = false; if ( isset( $response_body['ttl'] ) ) { $expiration = $response_body['ttl']; unset( $response_body['ttl'] ); } /* * The IP in the response is usually the same as the one that was sent * in the request, but in some cases it is different. In those cases, * it's important to reset it back to the IP from the request. * * For example, if the IP sent in the request is private (e.g., 192.168.1.100), * then the API will ignore that and use the corresponding public IP instead, * and the public IP will get returned. If the public IP were saved, though, * then get_cached_events() would always return `false`, because the transient * would be generated based on the public IP when saving the cache, but generated * based on the private IP when retrieving the cache. */ if ( ! empty( $response_body['location']['ip'] ) ) { $response_body['location']['ip'] = $request_args['body']['ip']; } /* * The API doesn't return a description for latitude/longitude requests, * but the description is already saved in the user location, so that * one can be used instead. */ if ( $this->coordinates_match( $request_args['body'], $response_body['location'] ) && empty( $response_body['location']['description'] ) ) { $response_body['location']['description'] = $this->user_location['description']; } /* * Store the raw response, because events will expire before the cache does. * The response will need to be processed every page load. */ $this->cache_events( $response_body, $expiration ); $response_body['events'] = $this->trim_events( $response_body['events'] );Changelog
Introduced in 4.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
wp.communityEvents.populateDynamicEventFields() now.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-admin/includes/class-wp-community-events.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.