Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-admin/includes
File: class-wp-community-events.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Administration: Community Events class.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Administration
[5] Fix | Delete
* @since 4.8.0
[6] Fix | Delete
*/
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Class WP_Community_Events.
[10] Fix | Delete
*
[11] Fix | Delete
* A client for api.wordpress.org/events.
[12] Fix | Delete
*
[13] Fix | Delete
* @since 4.8.0
[14] Fix | Delete
*/
[15] Fix | Delete
#[AllowDynamicProperties]
[16] Fix | Delete
class WP_Community_Events {
[17] Fix | Delete
/**
[18] Fix | Delete
* ID for a WordPress user account.
[19] Fix | Delete
*
[20] Fix | Delete
* @since 4.8.0
[21] Fix | Delete
*
[22] Fix | Delete
* @var int
[23] Fix | Delete
*/
[24] Fix | Delete
protected $user_id = 0;
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Stores location data for the user.
[28] Fix | Delete
*
[29] Fix | Delete
* @since 4.8.0
[30] Fix | Delete
*
[31] Fix | Delete
* @var false|array
[32] Fix | Delete
*/
[33] Fix | Delete
protected $user_location = false;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Constructor for WP_Community_Events.
[37] Fix | Delete
*
[38] Fix | Delete
* @since 4.8.0
[39] Fix | Delete
*
[40] Fix | Delete
* @param int $user_id WP user ID.
[41] Fix | Delete
* @param false|array $user_location {
[42] Fix | Delete
* Stored location data for the user. false to pass no location.
[43] Fix | Delete
*
[44] Fix | Delete
* @type string $description The name of the location
[45] Fix | Delete
* @type string $latitude The latitude in decimal degrees notation, without the degree
[46] Fix | Delete
* symbol. e.g.: 47.615200.
[47] Fix | Delete
* @type string $longitude The longitude in decimal degrees notation, without the degree
[48] Fix | Delete
* symbol. e.g.: -122.341100.
[49] Fix | Delete
* @type string $country The ISO 3166-1 alpha-2 country code. e.g.: BR
[50] Fix | Delete
* }
[51] Fix | Delete
*/
[52] Fix | Delete
public function __construct( $user_id, $user_location = false ) {
[53] Fix | Delete
$this->user_id = absint( $user_id );
[54] Fix | Delete
$this->user_location = $user_location;
[55] Fix | Delete
}
[56] Fix | Delete
[57] Fix | Delete
/**
[58] Fix | Delete
* Gets data about events near a particular location.
[59] Fix | Delete
*
[60] Fix | Delete
* Cached events will be immediately returned if the `user_location` property
[61] Fix | Delete
* is set for the current user, and cached events exist for that location.
[62] Fix | Delete
*
[63] Fix | Delete
* Otherwise, this method sends a request to the w.org Events API with location
[64] Fix | Delete
* data. The API will send back a recognized location based on the data, along
[65] Fix | Delete
* with nearby events.
[66] Fix | Delete
*
[67] Fix | Delete
* The browser's request for events is proxied with this method, rather
[68] Fix | Delete
* than having the browser make the request directly to api.wordpress.org,
[69] Fix | Delete
* because it allows results to be cached server-side and shared with other
[70] Fix | Delete
* users and sites in the network. This makes the process more efficient,
[71] Fix | Delete
* since increasing the number of visits that get cached data means users
[72] Fix | Delete
* don't have to wait as often; if the user's browser made the request
[73] Fix | Delete
* directly, it would also need to make a second request to WP in order to
[74] Fix | Delete
* pass the data for caching. Having WP make the request also introduces
[75] Fix | Delete
* the opportunity to anonymize the IP before sending it to w.org, which
[76] Fix | Delete
* mitigates possible privacy concerns.
[77] Fix | Delete
*
[78] Fix | Delete
* @since 4.8.0
[79] Fix | Delete
* @since 5.5.2 Response no longer contains formatted date field. They're added
[80] Fix | Delete
* in `wp.communityEvents.populateDynamicEventFields()` now.
[81] Fix | Delete
*
[82] Fix | Delete
* @param string $location_search Optional. City name to help determine the location.
[83] Fix | Delete
* e.g., "Seattle". Default empty string.
[84] Fix | Delete
* @param string $timezone Optional. Timezone to help determine the location.
[85] Fix | Delete
* Default empty string.
[86] Fix | Delete
* @return array|WP_Error A WP_Error on failure; an array with location and events on
[87] Fix | Delete
* success.
[88] Fix | Delete
*/
[89] Fix | Delete
public function get_events( $location_search = '', $timezone = '' ) {
[90] Fix | Delete
$cached_events = $this->get_cached_events();
[91] Fix | Delete
[92] Fix | Delete
if ( ! $location_search && $cached_events ) {
[93] Fix | Delete
return $cached_events;
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
// Include an unmodified $wp_version.
[97] Fix | Delete
require ABSPATH . WPINC . '/version.php';
[98] Fix | Delete
[99] Fix | Delete
$api_url = 'http://api.wordpress.org/events/1.0/';
[100] Fix | Delete
$request_args = $this->get_request_args( $location_search, $timezone );
[101] Fix | Delete
$request_args['user-agent'] = 'WordPress/' . $wp_version . '; ' . home_url( '/' );
[102] Fix | Delete
[103] Fix | Delete
if ( wp_http_supports( array( 'ssl' ) ) ) {
[104] Fix | Delete
$api_url = set_url_scheme( $api_url, 'https' );
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$response = wp_remote_get( $api_url, $request_args );
[108] Fix | Delete
$response_code = wp_remote_retrieve_response_code( $response );
[109] Fix | Delete
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
[110] Fix | Delete
$response_error = null;
[111] Fix | Delete
[112] Fix | Delete
if ( is_wp_error( $response ) ) {
[113] Fix | Delete
$response_error = $response;
[114] Fix | Delete
} elseif ( 200 !== $response_code ) {
[115] Fix | Delete
$response_error = new WP_Error(
[116] Fix | Delete
'api-error',
[117] Fix | Delete
/* translators: %d: Numeric HTTP status code, e.g. 400, 403, 500, 504, etc. */
[118] Fix | Delete
sprintf( __( 'Invalid API response code (%d).' ), $response_code )
[119] Fix | Delete
);
[120] Fix | Delete
} elseif ( ! isset( $response_body['location'], $response_body['events'] ) ) {
[121] Fix | Delete
$response_error = new WP_Error(
[122] Fix | Delete
'api-invalid-response',
[123] Fix | Delete
isset( $response_body['error'] ) ? $response_body['error'] : __( 'Unknown API error.' )
[124] Fix | Delete
);
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
if ( is_wp_error( $response_error ) ) {
[128] Fix | Delete
return $response_error;
[129] Fix | Delete
} else {
[130] Fix | Delete
$expiration = false;
[131] Fix | Delete
[132] Fix | Delete
if ( isset( $response_body['ttl'] ) ) {
[133] Fix | Delete
$expiration = $response_body['ttl'];
[134] Fix | Delete
unset( $response_body['ttl'] );
[135] Fix | Delete
}
[136] Fix | Delete
[137] Fix | Delete
/*
[138] Fix | Delete
* The IP in the response is usually the same as the one that was sent
[139] Fix | Delete
* in the request, but in some cases it is different. In those cases,
[140] Fix | Delete
* it's important to reset it back to the IP from the request.
[141] Fix | Delete
*
[142] Fix | Delete
* For example, if the IP sent in the request is private (e.g., 192.168.1.100),
[143] Fix | Delete
* then the API will ignore that and use the corresponding public IP instead,
[144] Fix | Delete
* and the public IP will get returned. If the public IP were saved, though,
[145] Fix | Delete
* then get_cached_events() would always return `false`, because the transient
[146] Fix | Delete
* would be generated based on the public IP when saving the cache, but generated
[147] Fix | Delete
* based on the private IP when retrieving the cache.
[148] Fix | Delete
*/
[149] Fix | Delete
if ( ! empty( $response_body['location']['ip'] ) ) {
[150] Fix | Delete
$response_body['location']['ip'] = $request_args['body']['ip'];
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
/*
[154] Fix | Delete
* The API doesn't return a description for latitude/longitude requests,
[155] Fix | Delete
* but the description is already saved in the user location, so that
[156] Fix | Delete
* one can be used instead.
[157] Fix | Delete
*/
[158] Fix | Delete
if ( $this->coordinates_match( $request_args['body'], $response_body['location'] ) && empty( $response_body['location']['description'] ) ) {
[159] Fix | Delete
$response_body['location']['description'] = $this->user_location['description'];
[160] Fix | Delete
}
[161] Fix | Delete
[162] Fix | Delete
/*
[163] Fix | Delete
* Store the raw response, because events will expire before the cache does.
[164] Fix | Delete
* The response will need to be processed every page load.
[165] Fix | Delete
*/
[166] Fix | Delete
$this->cache_events( $response_body, $expiration );
[167] Fix | Delete
[168] Fix | Delete
$response_body['events'] = $this->trim_events( $response_body['events'] );
[169] Fix | Delete
[170] Fix | Delete
return $response_body;
[171] Fix | Delete
}
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
/**
[175] Fix | Delete
* Builds an array of args to use in an HTTP request to the w.org Events API.
[176] Fix | Delete
*
[177] Fix | Delete
* @since 4.8.0
[178] Fix | Delete
*
[179] Fix | Delete
* @param string $search Optional. City search string. Default empty string.
[180] Fix | Delete
* @param string $timezone Optional. Timezone string. Default empty string.
[181] Fix | Delete
* @return array The request args.
[182] Fix | Delete
*/
[183] Fix | Delete
protected function get_request_args( $search = '', $timezone = '' ) {
[184] Fix | Delete
$args = array(
[185] Fix | Delete
'number' => 5, // Get more than three in case some get trimmed out.
[186] Fix | Delete
'ip' => self::get_unsafe_client_ip(),
[187] Fix | Delete
);
[188] Fix | Delete
[189] Fix | Delete
/*
[190] Fix | Delete
* Include the minimal set of necessary arguments, in order to increase the
[191] Fix | Delete
* chances of a cache-hit on the API side.
[192] Fix | Delete
*/
[193] Fix | Delete
if ( empty( $search ) && isset( $this->user_location['latitude'], $this->user_location['longitude'] ) ) {
[194] Fix | Delete
$args['latitude'] = $this->user_location['latitude'];
[195] Fix | Delete
$args['longitude'] = $this->user_location['longitude'];
[196] Fix | Delete
} else {
[197] Fix | Delete
$args['locale'] = get_user_locale( $this->user_id );
[198] Fix | Delete
[199] Fix | Delete
if ( $timezone ) {
[200] Fix | Delete
$args['timezone'] = $timezone;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
if ( $search ) {
[204] Fix | Delete
$args['location'] = $search;
[205] Fix | Delete
}
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
// Wrap the args in an array compatible with the second parameter of `wp_remote_get()`.
[209] Fix | Delete
return array(
[210] Fix | Delete
'body' => $args,
[211] Fix | Delete
);
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
/**
[215] Fix | Delete
* Determines the user's actual IP address and attempts to partially
[216] Fix | Delete
* anonymize an IP address by converting it to a network ID.
[217] Fix | Delete
*
[218] Fix | Delete
* Geolocating the network ID usually returns a similar location as the
[219] Fix | Delete
* actual IP, but provides some privacy for the user.
[220] Fix | Delete
*
[221] Fix | Delete
* $_SERVER['REMOTE_ADDR'] cannot be used in all cases, such as when the user
[222] Fix | Delete
* is making their request through a proxy, or when the web server is behind
[223] Fix | Delete
* a proxy. In those cases, $_SERVER['REMOTE_ADDR'] is set to the proxy address rather
[224] Fix | Delete
* than the user's actual address.
[225] Fix | Delete
*
[226] Fix | Delete
* Modified from https://stackoverflow.com/a/2031935/450127, MIT license.
[227] Fix | Delete
* Modified from https://github.com/geertw/php-ip-anonymizer, MIT license.
[228] Fix | Delete
*
[229] Fix | Delete
* SECURITY WARNING: This function is _NOT_ intended to be used in
[230] Fix | Delete
* circumstances where the authenticity of the IP address matters. This does
[231] Fix | Delete
* _NOT_ guarantee that the returned address is valid or accurate, and it can
[232] Fix | Delete
* be easily spoofed.
[233] Fix | Delete
*
[234] Fix | Delete
* @since 4.8.0
[235] Fix | Delete
*
[236] Fix | Delete
* @return string|false The anonymized address on success; the given address
[237] Fix | Delete
* or false on failure.
[238] Fix | Delete
*/
[239] Fix | Delete
public static function get_unsafe_client_ip() {
[240] Fix | Delete
$client_ip = false;
[241] Fix | Delete
[242] Fix | Delete
// In order of preference, with the best ones for this purpose first.
[243] Fix | Delete
$address_headers = array(
[244] Fix | Delete
'HTTP_CLIENT_IP',
[245] Fix | Delete
'HTTP_X_FORWARDED_FOR',
[246] Fix | Delete
'HTTP_X_FORWARDED',
[247] Fix | Delete
'HTTP_X_CLUSTER_CLIENT_IP',
[248] Fix | Delete
'HTTP_FORWARDED_FOR',
[249] Fix | Delete
'HTTP_FORWARDED',
[250] Fix | Delete
'REMOTE_ADDR',
[251] Fix | Delete
);
[252] Fix | Delete
[253] Fix | Delete
foreach ( $address_headers as $header ) {
[254] Fix | Delete
if ( array_key_exists( $header, $_SERVER ) ) {
[255] Fix | Delete
/*
[256] Fix | Delete
* HTTP_X_FORWARDED_FOR can contain a chain of comma-separated
[257] Fix | Delete
* addresses. The first one is the original client. It can't be
[258] Fix | Delete
* trusted for authenticity, but we don't need to for this purpose.
[259] Fix | Delete
*/
[260] Fix | Delete
$address_chain = explode( ',', $_SERVER[ $header ] );
[261] Fix | Delete
$client_ip = trim( $address_chain[0] );
[262] Fix | Delete
[263] Fix | Delete
break;
[264] Fix | Delete
}
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
if ( ! $client_ip ) {
[268] Fix | Delete
return false;
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
$anon_ip = wp_privacy_anonymize_ip( $client_ip, true );
[272] Fix | Delete
[273] Fix | Delete
if ( '0.0.0.0' === $anon_ip || '::' === $anon_ip ) {
[274] Fix | Delete
return false;
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
return $anon_ip;
[278] Fix | Delete
}
[279] Fix | Delete
[280] Fix | Delete
/**
[281] Fix | Delete
* Test if two pairs of latitude/longitude coordinates match each other.
[282] Fix | Delete
*
[283] Fix | Delete
* @since 4.8.0
[284] Fix | Delete
*
[285] Fix | Delete
* @param array $a The first pair, with indexes 'latitude' and 'longitude'.
[286] Fix | Delete
* @param array $b The second pair, with indexes 'latitude' and 'longitude'.
[287] Fix | Delete
* @return bool True if they match, false if they don't.
[288] Fix | Delete
*/
[289] Fix | Delete
protected function coordinates_match( $a, $b ) {
[290] Fix | Delete
if ( ! isset( $a['latitude'], $a['longitude'], $b['latitude'], $b['longitude'] ) ) {
[291] Fix | Delete
return false;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
return $a['latitude'] === $b['latitude'] && $a['longitude'] === $b['longitude'];
[295] Fix | Delete
}
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Generates a transient key based on user location.
[299] Fix | Delete
*
[300] Fix | Delete
* This could be reduced to a one-liner in the calling functions, but it's
[301] Fix | Delete
* intentionally a separate function because it's called from multiple
[302] Fix | Delete
* functions, and having it abstracted keeps the logic consistent and DRY,
[303] Fix | Delete
* which is less prone to errors.
[304] Fix | Delete
*
[305] Fix | Delete
* @since 4.8.0
[306] Fix | Delete
*
[307] Fix | Delete
* @param array $location Should contain 'latitude' and 'longitude' indexes.
[308] Fix | Delete
* @return string|false Transient key on success, false on failure.
[309] Fix | Delete
*/
[310] Fix | Delete
protected function get_events_transient_key( $location ) {
[311] Fix | Delete
$key = false;
[312] Fix | Delete
[313] Fix | Delete
if ( isset( $location['ip'] ) ) {
[314] Fix | Delete
$key = 'community-events-' . md5( $location['ip'] );
[315] Fix | Delete
} elseif ( isset( $location['latitude'], $location['longitude'] ) ) {
[316] Fix | Delete
$key = 'community-events-' . md5( $location['latitude'] . $location['longitude'] );
[317] Fix | Delete
}
[318] Fix | Delete
[319] Fix | Delete
return $key;
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
/**
[323] Fix | Delete
* Caches an array of events data from the Events API.
[324] Fix | Delete
*
[325] Fix | Delete
* @since 4.8.0
[326] Fix | Delete
*
[327] Fix | Delete
* @param array $events Response body from the API request.
[328] Fix | Delete
* @param int|false $expiration Optional. Amount of time to cache the events. Defaults to false.
[329] Fix | Delete
* @return bool true if events were cached; false if not.
[330] Fix | Delete
*/
[331] Fix | Delete
protected function cache_events( $events, $expiration = false ) {
[332] Fix | Delete
$set = false;
[333] Fix | Delete
$transient_key = $this->get_events_transient_key( $events['location'] );
[334] Fix | Delete
$cache_expiration = $expiration ? absint( $expiration ) : HOUR_IN_SECONDS * 12;
[335] Fix | Delete
[336] Fix | Delete
if ( $transient_key ) {
[337] Fix | Delete
$set = set_site_transient( $transient_key, $events, $cache_expiration );
[338] Fix | Delete
}
[339] Fix | Delete
[340] Fix | Delete
return $set;
[341] Fix | Delete
}
[342] Fix | Delete
[343] Fix | Delete
/**
[344] Fix | Delete
* Gets cached events.
[345] Fix | Delete
*
[346] Fix | Delete
* @since 4.8.0
[347] Fix | Delete
* @since 5.5.2 Response no longer contains formatted date field. They're added
[348] Fix | Delete
* in `wp.communityEvents.populateDynamicEventFields()` now.
[349] Fix | Delete
*
[350] Fix | Delete
* @return array|false An array containing `location` and `events` items
[351] Fix | Delete
* on success, false on failure.
[352] Fix | Delete
*/
[353] Fix | Delete
public function get_cached_events() {
[354] Fix | Delete
$transient_key = $this->get_events_transient_key( $this->user_location );
[355] Fix | Delete
if ( ! $transient_key ) {
[356] Fix | Delete
return false;
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
$cached_response = get_site_transient( $transient_key );
[360] Fix | Delete
if ( isset( $cached_response['events'] ) ) {
[361] Fix | Delete
$cached_response['events'] = $this->trim_events( $cached_response['events'] );
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
return $cached_response;
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
/**
[368] Fix | Delete
* Adds formatted date and time items for each event in an API response.
[369] Fix | Delete
*
[370] Fix | Delete
* This has to be called after the data is pulled from the cache, because
[371] Fix | Delete
* the cached events are shared by all users. If it was called before storing
[372] Fix | Delete
* the cache, then all users would see the events in the localized data/time
[373] Fix | Delete
* of the user who triggered the cache refresh, rather than their own.
[374] Fix | Delete
*
[375] Fix | Delete
* @since 4.8.0
[376] Fix | Delete
* @deprecated 5.6.0 No longer used in core.
[377] Fix | Delete
*
[378] Fix | Delete
* @param array $response_body The response which contains the events.
[379] Fix | Delete
* @return array The response with dates and times formatted.
[380] Fix | Delete
*/
[381] Fix | Delete
protected function format_event_data_time( $response_body ) {
[382] Fix | Delete
_deprecated_function(
[383] Fix | Delete
__METHOD__,
[384] Fix | Delete
'5.5.2',
[385] Fix | Delete
'This is no longer used by core, and only kept for backward compatibility.'
[386] Fix | Delete
);
[387] Fix | Delete
[388] Fix | Delete
if ( isset( $response_body['events'] ) ) {
[389] Fix | Delete
foreach ( $response_body['events'] as $key => $event ) {
[390] Fix | Delete
$timestamp = strtotime( $event['date'] );
[391] Fix | Delete
[392] Fix | Delete
/*
[393] Fix | Delete
* The `date_format` option is not used because it's important
[394] Fix | Delete
* in this context to keep the day of the week in the formatted date,
[395] Fix | Delete
* so that users can tell at a glance if the event is on a day they
[396] Fix | Delete
* are available, without having to open the link.
[397] Fix | Delete
*/
[398] Fix | Delete
/* translators: Date format for upcoming events on the dashboard. Include the day of the week. See https://www.php.net/manual/datetime.format.php */
[399] Fix | Delete
$formatted_date = date_i18n( __( 'l, M j, Y' ), $timestamp );
[400] Fix | Delete
$formatted_time = date_i18n( get_option( 'time_format' ), $timestamp );
[401] Fix | Delete
[402] Fix | Delete
if ( isset( $event['end_date'] ) ) {
[403] Fix | Delete
$end_timestamp = strtotime( $event['end_date'] );
[404] Fix | Delete
$formatted_end_date = date_i18n( __( 'l, M j, Y' ), $end_timestamp );
[405] Fix | Delete
[406] Fix | Delete
if ( 'meetup' !== $event['type'] && $formatted_end_date !== $formatted_date ) {
[407] Fix | Delete
/* translators: Upcoming events month format. See https://www.php.net/manual/datetime.format.php */
[408] Fix | Delete
$start_month = date_i18n( _x( 'F', 'upcoming events month format' ), $timestamp );
[409] Fix | Delete
$end_month = date_i18n( _x( 'F', 'upcoming events month format' ), $end_timestamp );
[410] Fix | Delete
[411] Fix | Delete
if ( $start_month === $end_month ) {
[412] Fix | Delete
$formatted_date = sprintf(
[413] Fix | Delete
/* translators: Date string for upcoming events. 1: Month, 2: Starting day, 3: Ending day, 4: Year. */
[414] Fix | Delete
__( '%1$s %2$d–%3$d, %4$d' ),
[415] Fix | Delete
$start_month,
[416] Fix | Delete
/* translators: Upcoming events day format. See https://www.php.net/manual/datetime.format.php */
[417] Fix | Delete
date_i18n( _x( 'j', 'upcoming events day format' ), $timestamp ),
[418] Fix | Delete
date_i18n( _x( 'j', 'upcoming events day format' ), $end_timestamp ),
[419] Fix | Delete
/* translators: Upcoming events year format. See https://www.php.net/manual/datetime.format.php */
[420] Fix | Delete
date_i18n( _x( 'Y', 'upcoming events year format' ), $timestamp )
[421] Fix | Delete
);
[422] Fix | Delete
} else {
[423] Fix | Delete
$formatted_date = sprintf(
[424] Fix | Delete
/* translators: Date string for upcoming events. 1: Starting month, 2: Starting day, 3: Ending month, 4: Ending day, 5: Year. */
[425] Fix | Delete
__( '%1$s %2$d – %3$s %4$d, %5$d' ),
[426] Fix | Delete
$start_month,
[427] Fix | Delete
date_i18n( _x( 'j', 'upcoming events day format' ), $timestamp ),
[428] Fix | Delete
$end_month,
[429] Fix | Delete
date_i18n( _x( 'j', 'upcoming events day format' ), $end_timestamp ),
[430] Fix | Delete
date_i18n( _x( 'Y', 'upcoming events year format' ), $timestamp )
[431] Fix | Delete
);
[432] Fix | Delete
}
[433] Fix | Delete
[434] Fix | Delete
$formatted_date = wp_maybe_decline_date( $formatted_date, 'F j, Y' );
[435] Fix | Delete
}
[436] Fix | Delete
}
[437] Fix | Delete
[438] Fix | Delete
$response_body['events'][ $key ]['formatted_date'] = $formatted_date;
[439] Fix | Delete
$response_body['events'][ $key ]['formatted_time'] = $formatted_time;
[440] Fix | Delete
}
[441] Fix | Delete
}
[442] Fix | Delete
[443] Fix | Delete
return $response_body;
[444] Fix | Delete
}
[445] Fix | Delete
[446] Fix | Delete
/**
[447] Fix | Delete
* Prepares the event list for presentation.
[448] Fix | Delete
*
[449] Fix | Delete
* Discards expired events, and makes WordCamps "sticky." Attendees need more
[450] Fix | Delete
* advanced notice about WordCamps than they do for meetups, so camps should
[451] Fix | Delete
* appear in the list sooner. If a WordCamp is coming up, the API will "stick"
[452] Fix | Delete
* it in the response, even if it wouldn't otherwise appear. When that happens,
[453] Fix | Delete
* the event will be at the end of the list, and will need to be moved into a
[454] Fix | Delete
* higher position, so that it doesn't get trimmed off.
[455] Fix | Delete
*
[456] Fix | Delete
* @since 4.8.0
[457] Fix | Delete
* @since 4.9.7 Stick a WordCamp to the final list.
[458] Fix | Delete
* @since 5.5.2 Accepts and returns only the events, rather than an entire HTTP response.
[459] Fix | Delete
* @since 6.0.0 Decode HTML entities from the event title.
[460] Fix | Delete
*
[461] Fix | Delete
* @param array $events The events that will be prepared.
[462] Fix | Delete
* @return array The response body with events trimmed.
[463] Fix | Delete
*/
[464] Fix | Delete
protected function trim_events( array $events ) {
[465] Fix | Delete
$future_events = array();
[466] Fix | Delete
[467] Fix | Delete
foreach ( $events as $event ) {
[468] Fix | Delete
/*
[469] Fix | Delete
* The API's `date` and `end_date` fields are in the _event's_ local timezone, but UTC is needed so
[470] Fix | Delete
* it can be converted to the _user's_ local time.
[471] Fix | Delete
*/
[472] Fix | Delete
$end_time = (int) $event['end_unix_timestamp'];
[473] Fix | Delete
[474] Fix | Delete
if ( time() < $end_time ) {
[475] Fix | Delete
// Decode HTML entities from the event title.
[476] Fix | Delete
$event['title'] = html_entity_decode( $event['title'], ENT_QUOTES, 'UTF-8' );
[477] Fix | Delete
[478] Fix | Delete
array_push( $future_events, $event );
[479] Fix | Delete
}
[480] Fix | Delete
}
[481] Fix | Delete
[482] Fix | Delete
$future_wordcamps = array_filter(
[483] Fix | Delete
$future_events,
[484] Fix | Delete
static function ( $wordcamp ) {
[485] Fix | Delete
return 'wordcamp' === $wordcamp['type'];
[486] Fix | Delete
}
[487] Fix | Delete
);
[488] Fix | Delete
[489] Fix | Delete
$future_wordcamps = array_values( $future_wordcamps ); // Remove gaps in indices.
[490] Fix | Delete
$trimmed_events = array_slice( $future_events, 0, 3 );
[491] Fix | Delete
$trimmed_event_types = wp_list_pluck( $trimmed_events, 'type' );
[492] Fix | Delete
[493] Fix | Delete
// Make sure the soonest upcoming WordCamp is pinned in the list.
[494] Fix | Delete
if ( $future_wordcamps && ! in_array( 'wordcamp', $trimmed_event_types, true ) ) {
[495] Fix | Delete
array_pop( $trimmed_events );
[496] Fix | Delete
array_push( $trimmed_events, $future_wordcamps[0] );
[497] Fix | Delete
}
[498] Fix | Delete
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function