WP_Community_Events
- Since
- 4.8.0
- Source
wp-admin/includes/class-wp-community-events.php:17
Class WP_Community_Events.
Description
A client for api.wordpress.org/events.
Compatibility
- WordPress
- since 4.8.0
- 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).
Properties · 2
$user_idintprotected- ID for a WordPress user account.
$user_locationfalse|arrayprotected- Stores location data for the user.
Methods · 11
- __construct()Constructor for WP_Community_Events.
- get_events()Gets data about events near a particular location.
- get_request_args()Builds an array of args to use in an HTTP request to the w.org Events API.
- get_unsafe_client_ip()Determines the user's actual IP address and attempts to partially anonymize an IP address by converting it to a network ID.
- coordinates_match()Test if two pairs of latitude/longitude coordinates match each other.
- get_events_transient_key()Generates a transient key based on user location.
- cache_events()Caches an array of events data from the Events API.
- get_cached_events()Gets cached events.
- format_event_data_time()Adds formatted date and time items for each event in an API response.
- trim_events()Prepares the event list for presentation.
- maybe_log_events_response()Logs responses to Events API requests.
Source code
#[AllowDynamicProperties]class WP_Community_Events { /** * ID for a WordPress user account. * * @since 4.8.0 * * @var int */ protected $user_id = 0; /** * Stores location data for the user. * * @since 4.8.0 * * @var false|array */ protected $user_location = false; /** * Constructor for WP_Community_Events. * * @since 4.8.0 * * @param int $user_id WP user ID. * @param false|array $user_location { * Stored location data for the user. false to pass no location. * * @type string $description The name of the location * @type string $latitude The latitude in decimal degrees notation, without the degree * symbol. e.g.: 47.615200. * @type string $longitude The longitude in decimal degrees notation, without the degree * symbol. e.g.: -122.341100. * @type string $country The ISO 3166-1 alpha-2 country code. e.g.: BR * } */ public function __construct( $user_id, $user_location = false ) { $this->user_id = absint( $user_id ); $this->user_location = $user_location; } /** * Gets data about events near a particular location. * * 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. * * @since 4.8.0 * @since 5.5.2 Response no longer contains formatted date field. They're added * in `wp.communityEvents.populateDynamicEventFields()` now. * * @param string $location_search Optional. City name to help determine the location. * e.g., "Seattle". Default empty string. * @param string $timezone Optional. Timezone to help determine the location. * Default empty string. * @return array|WP_Error A WP_Error on failure; an array with location and events on * success. */ public function get_events( $location_search = '', $timezone = '' ) { $cached_events = $this->get_cached_events(); if ( ! $location_search && $cached_events ) { return $cached_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.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.