File "class-wp-community-events.php"
Full path: /home/kosmetik/public_html/wp-includes/wp-admin/includes/class-wp-community-events.php
File
size: 8.5 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
class WP_Community_Events
{
protected $user_id = 0;
protected $user_location = false;
public function __construct($user_id, $user_location = false)
{
$this->user_id = absint($user_id);
$this->user_location = $user_location;
}
public function get_events($location_search = '', $timezone = '')
{
$cached_events = $this->get_cached_events();
if (!$location_search && $cached_events) {
return $cached_events;
}
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', 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']);
}
if (!empty($response_body['location']['ip'])) {
$response_body['location']['ip'] = $request_args['body']['ip'];
}
if ($this->coordinates_match($request_args['body'], $response_body['location']) && empty($response_body['location']['description'])) {
$response_body['location']['description'] = $this->user_location['description'];
}
$this->cache_events($response_body, $expiration);
$response_body['events'] = $this->trim_events($response_body['events']);
return $response_body;
}
}
protected function get_request_args($search = '', $timezone = '')
{
$args = array('number' => 5, 'ip' => self::get_unsafe_client_ip());
if (empty($search) && isset($this->user_location['latitude'], $this->user_location['longitude'])) {
$args['latitude'] = $this->user_location['latitude'];
$args['longitude'] = $this->user_location['longitude'];
} else {
$args['locale'] = get_user_locale($this->user_id);
if ($timezone) {
$args['timezone'] = $timezone;
}
if ($search) {
$args['location'] = $search;
}
}
return array('body' => $args);
}
public static function get_unsafe_client_ip()
{
$client_ip = false;
$address_headers = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR');
foreach ($address_headers as $header) {
if (array_key_exists($header, $_SERVER)) {
$address_chain = explode(',', $_SERVER[$header]);
$client_ip = trim($address_chain[0]);
break;
}
}
if (!$client_ip) {
return false;
}
$anon_ip = wp_privacy_anonymize_ip($client_ip, true);
if ('0.0.0.0' === $anon_ip || '::' === $anon_ip) {
return false;
}
return $anon_ip;
}
protected function coordinates_match($a, $b)
{
if (!isset($a['latitude'], $a['longitude'], $b['latitude'], $b['longitude'])) {
return false;
}
return $a['latitude'] === $b['latitude'] && $a['longitude'] === $b['longitude'];
}
protected function get_events_transient_key($location)
{
$key = false;
if (isset($location['ip'])) {
$key = 'community-events-' . md5($location['ip']);
} elseif (isset($location['latitude'], $location['longitude'])) {
$key = 'community-events-' . md5($location['latitude'] . $location['longitude']);
}
return $key;
}
protected function cache_events($events, $expiration = false)
{
$set = false;
$transient_key = $this->get_events_transient_key($events['location']);
$cache_expiration = $expiration ? absint($expiration) : HOUR_IN_SECONDS * 12;
if ($transient_key) {
$set = set_site_transient($transient_key, $events, $cache_expiration);
}
return $set;
}
public function get_cached_events()
{
$cached_response = get_site_transient($this->get_events_transient_key($this->user_location));
if (isset($cached_response['events'])) {
$cached_response['events'] = $this->trim_events($cached_response['events']);
}
return $cached_response;
}
protected function format_event_data_time($response_body)
{
_deprecated_function(__METHOD__, '5.5.2', 'This is no longer used by core, and only kept for backward compatibility.');
if (isset($response_body['events'])) {
foreach ($response_body['events'] as $key => $event) {
$timestamp = strtotime($event['date']);
$formatted_date = date_i18n(__('l, M j, Y'), $timestamp);
$formatted_time = date_i18n(get_option('time_format'), $timestamp);
if (isset($event['end_date'])) {
$end_timestamp = strtotime($event['end_date']);
$formatted_end_date = date_i18n(__('l, M j, Y'), $end_timestamp);
if ('meetup' !== $event['type'] && $formatted_end_date !== $formatted_date) {
$start_month = date_i18n(_x('F', 'upcoming events month format'), $timestamp);
$end_month = date_i18n(_x('F', 'upcoming events month format'), $end_timestamp);
if ($start_month === $end_month) {
$formatted_date = sprintf(__('%1$s %2$dā%3$d, %4$d'), $start_month, date_i18n(_x('j', 'upcoming events day format'), $timestamp), date_i18n(_x('j', 'upcoming events day format'), $end_timestamp), date_i18n(_x('Y', 'upcoming events year format'), $timestamp));
} else {
$formatted_date = sprintf(__('%1$s %2$d ā %3$s %4$d, %5$d'), $start_month, date_i18n(_x('j', 'upcoming events day format'), $timestamp), $end_month, date_i18n(_x('j', 'upcoming events day format'), $end_timestamp), date_i18n(_x('Y', 'upcoming events year format'), $timestamp));
}
$formatted_date = wp_maybe_decline_date($formatted_date, 'F j, Y');
}
}
$response_body['events'][$key]['formatted_date'] = $formatted_date;
$response_body['events'][$key]['formatted_time'] = $formatted_time;
}
}
return $response_body;
}
protected function trim_events(array $events)
{
$future_events = array();
foreach ($events as $event) {
$end_time = (int) $event['end_unix_timestamp'];
if (time() < $end_time) {
array_push($future_events, $event);
}
}
$future_wordcamps = array_filter($future_events, function ($wordcamp) {
return 'wordcamp' === $wordcamp['type'];
});
$future_wordcamps = array_values($future_wordcamps);
$trimmed_events = array_slice($future_events, 0, 3);
$trimmed_event_types = wp_list_pluck($trimmed_events, 'type');
if ($future_wordcamps && !in_array('wordcamp', $trimmed_event_types, true)) {
array_pop($trimmed_events);
array_push($trimmed_events, $future_wordcamps[0]);
}
return $trimmed_events;
}
protected function maybe_log_events_response($message, $details)
{
_deprecated_function(__METHOD__, '4.9.0');
if (!WP_DEBUG_LOG) {
return;
}
error_log(sprintf('%s: %s. Details: %s', __METHOD__, trim($message, '.'), wp_json_encode($details)));
}
}