wppaste
WordPress

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
Gets data about events near a particular location.

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

$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

Reaches an outbound HTTP request via wp_remote_get().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–121

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 138.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • httpoutbound HTTP requestwp_remote_get()called directly
  • serializeserialisationjson_decode()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always8->get_cached_events()
!wp_http_supports() && is_wp_error()58–70->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–75->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) && $response_body71–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 !== 20071–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) && $response_body76–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 !== 20076–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–112->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–117->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) && $response_body97–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 !== 20097–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) && $response_body102–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 !== 200102–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

PHPCompiledExecutedBranchesNotes
8.6-dev1378–120131 fewer instruction than PHP 8.5
8.51388–12113
8.41388–12113
8.31388–12113
8.21388–12113
8.11388–12113
7.41388–12113

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

Show all 14

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',				$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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.5.2
Response no longer contains formatted date field. They're added in wp.communityEvents.populateDynamicEventFields() now.from the docblock
4.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 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.