返回介绍

wp_ajax_get_community_events()

发布于 2017-09-11 11:29:04 字数 3210 浏览 1228 评论 0 收藏 0

wp_ajax_get_community_events()

Handles AJAX requests for community events


description


源代码

File: wp-admin/includes/ajax-actions.php

function wp_ajax_get_community_events() {
	require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );

	check_ajax_referer( 'community_events' );

	$search         = isset( $_POST['location'] ) ? wp_unslash( $_POST['location'] ) : '';
	$timezone       = isset( $_POST['timezone'] ) ? wp_unslash( $_POST['timezone'] ) : '';
	$user_id        = get_current_user_id();
	$saved_location = get_user_option( 'community-events-location', $user_id );
	$events_client  = new WP_Community_Events( $user_id, $saved_location );
	$events         = $events_client->get_events( $search, $timezone );
	$ip_changed     = false;

	if ( is_wp_error( $events ) ) {
		wp_send_json_error( array(
			'error' => $events->get_error_message(),
		) );
	} else {
		if ( empty( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) ) {
			$ip_changed = true;
		} elseif ( isset( $saved_location['ip'] ) && ! empty( $events['location']['ip'] ) && $saved_location['ip'] !== $events['location']['ip'] ) {
			$ip_changed = true;
		}

		/*
		 * The location should only be updated when it changes. The API doesn't always return
		 * a full location; sometimes it's missing the description or country. The location
		 * that was saved during the initial request is known to be good and complete, though.
		 * It should be left in tact until the user explicitly changes it (either by manually
		 * searching for a new location, or by changing their IP address).
		 *
		 * If the location were updated with an incomplete response from the API, then it could
		 * break assumptions that the UI makes (e.g., that there will always be a description
		 * that corresponds to a latitude/longitude location).
		 *
		 * The location is stored network-wide, so that the user doesn't have to set it on each site.
		 */
		if ( $ip_changed || $search ) {
			update_user_option( $user_id, 'community-events-location', $events['location'], true );
		}

		wp_send_json_success( $events );
	}
}

更新日志

Versiondescription
4.8.0Introduced.

相关函数

Uses

  • wp-admin/includes/class-wp-community-events.php: WP_Community_Events::__construct()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/pluggable.php: check_ajax_referer()
  • wp-includes/functions.php: wp_send_json_error()
  • wp-includes/functions.php: wp_send_json_success()
  • wp-includes/user.php: update_user_option()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/user.php: get_user_option()
  • wp-includes/load.php: is_wp_error()
  • Show 4 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文