/**
  * Retrieve the avatar url for a user who provided a user ID or email address.
  *
  * {@see get_avatar()} doesn't return just the URL, so we have to
  * extract it here.
  *
  * @deprecated
  * @param string $email Email address
  * @return string url for the user's avatar
  */
 public function get_avatar_url($email)
 {
     _deprecated_function(__CLASS__ . '::' . __METHOD__, 'WPAPI-1.1', 'json_get_avatar_url');
     return json_get_avatar_url($email);
 }
 /**
  * Prepares comment data for returning as a JSON response.
  *
  * @param stdClass $comment Comment object
  * @param array $requested_fields Fields to retrieve from the comment
  * @param string $context Where is the comment being loaded?
  * @return array Comment data for JSON serialization
  */
 protected function prepare_comment($comment, $requested_fields = array('comment', 'meta'), $context = 'single')
 {
     $fields = array('ID' => (int) $comment->comment_ID, 'post' => (int) $comment->comment_post_ID);
     $post = (array) get_post($fields['post']);
     // Content
     $fields['content'] = apply_filters('comment_text', $comment->comment_content, $comment);
     // $fields['content_raw'] = $comment->comment_content;
     // Status
     switch ($comment->comment_approved) {
         case 'hold':
         case '0':
             $fields['status'] = 'hold';
             break;
         case 'approve':
         case '1':
             $fields['status'] = 'approved';
             break;
         case 'spam':
         case 'trash':
         default:
             $fields['status'] = $comment->comment_approved;
             break;
     }
     // Type
     $fields['type'] = apply_filters('get_comment_type', $comment->comment_type);
     if (empty($fields['type'])) {
         $fields['type'] = 'comment';
     }
     // Post
     if ('single' === $context) {
         $parent = get_post($post['post_parent'], ARRAY_A);
         $fields['parent'] = $this->prepare_post($parent, 'single-parent');
     }
     // Parent
     if (('single' === $context || 'single-parent' === $context) && (int) $comment->comment_parent) {
         $parent_fields = array('meta');
         if ($context === 'single') {
             $parent_fields[] = 'comment';
         }
         $parent = get_comment($post['post_parent']);
         $fields['parent'] = $this->prepare_comment($parent, $parent_fields, 'single-parent');
     }
     // Parent
     $fields['parent'] = (int) $comment->comment_parent;
     // Author
     if ((int) $comment->user_id !== 0) {
         $fields['author'] = (int) $comment->user_id;
     } else {
         $fields['author'] = array('ID' => 0, 'name' => $comment->comment_author, 'URL' => $comment->comment_author_url, 'avatar' => json_get_avatar_url($comment->comment_author_email));
     }
     // Date
     $timezone = json_get_timezone();
     $date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $comment->comment_date, $timezone);
     $fields['date'] = $date->format('c');
     $fields['date_tz'] = $date->format('e');
     $fields['date_gmt'] = date('c', strtotime($comment->comment_date_gmt));
     // Meta
     $meta = array('links' => array('up' => json_url(sprintf('/posts/%d', (int) $comment->comment_post_ID))));
     if (0 !== (int) $comment->comment_parent) {
         $meta['links']['in-reply-to'] = json_url(sprintf('/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_parent));
     }
     if ('single' !== $context) {
         $meta['links']['self'] = json_url(sprintf('/posts/%d/comments/%d', (int) $comment->comment_post_ID, (int) $comment->comment_ID));
     }
     // Remove unneeded fields
     $data = array();
     if (in_array('comment', $requested_fields)) {
         $data = array_merge($data, $fields);
     }
     if (in_array('meta', $requested_fields)) {
         $data['meta'] = $meta;
     }
     return apply_filters('json_prepare_comment', $data, $comment, $context);
 }
 /**
  *
  * Prepare a User entity from a WP_User instance.
  *
  * @param WP_User $user
  * @param string $context One of 'view', 'edit', 'embed'
  * @return array
  */
 protected function prepare_user($user, $context = 'view')
 {
     $user_fields = array('ID' => $user->ID, 'username' => $user->user_login, 'name' => $user->display_name, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'nickname' => $user->nickname, 'slug' => $user->user_nicename, 'URL' => $user->user_url, 'avatar' => json_get_avatar_url($user->user_email), 'description' => $user->description);
     $user_fields['registered'] = date('c', strtotime($user->user_registered));
     if ($context === 'view' || $context === 'edit') {
         $user_fields['roles'] = $user->roles;
         $user_fields['capabilities'] = $user->allcaps;
         $user_fields['email'] = false;
     }
     if ($context === 'edit') {
         // The user's specific caps should only be needed if you're editing
         // the user, as allcaps should handle most uses
         $user_fields['email'] = $user->user_email;
         $user_fields['extra_capabilities'] = $user->caps;
     }
     $user_fields['meta'] = array('links' => array('self' => json_url('/users/' . $user->ID), 'archives' => json_url('/users/' . $user->ID . '/posts')));
     return apply_filters('json_prepare_user', $user_fields, $user, $context);
 }
示例#4
0
 function prepare_contributor($user)
 {
     $user_fields = array('ID' => $user->ID, 'name' => $user->display_name, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'nickname' => $user->nickname, 'URL' => $user->user_url, 'avatar' => json_get_avatar_url($user->user_email), 'description' => $user->description);
     return apply_filters('bikeit_prepare_contributor', $user_fields, $user);
 }