/**
  *
  * 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' => get_json_url_users_get_user($user->ID), 'archives' => get_json_url_users_get_posts($user->ID)));
     return apply_filters('json_prepare_user', $user_fields, $user, $context);
 }
 /**
  * Prepares post data for return in an XML-RPC object.
  *
  * @access protected
  *
  * @param array $post The unprepared post data
  * @param string $context The context for the prepared post. (view|view-revision|edit|embed|single-parent)
  * @return array The prepared post data
  */
 protected function prepare_post($post, $context = 'view', $show_type = 'row')
 {
     // Holds the data for this post.
     $_post = array('ID' => (int) $post['ID']);
     $post_type = get_post_type_object($post['post_type']);
     if (!json_check_post_permission($post, 'read')) {
         return false;
     }
     $previous_post = null;
     if (!empty($GLOBALS['post'])) {
         $previous_post = $GLOBALS['post'];
     }
     $post_obj = get_post($post['ID']);
     // Don't allow unauthenticated users to read password-protected posts
     if (!empty($post['post_password'])) {
         if (!json_check_post_permission($post, 'edit')) {
             return false;
         }
         // Fake the correct cookie to fool post_password_required().
         // Without this, get_the_content() will give a password form.
         require_once ABSPATH . 'wp-includes/class-phpass.php';
         $hasher = new PasswordHash(8, true);
         $value = $hasher->HashPassword($post['post_password']);
         $_COOKIE['wp-postpass_' . COOKIEHASH] = wp_slash($value);
     }
     $GLOBALS['post'] = $post_obj;
     setup_postdata($post_obj);
     //comment num
     $comment_num = $this->comments->get_comments_num_by_post_id($_post['ID']);
     // prepare common post fields
     $post_content = '';
     if ($show_type == 'row') {
         $post_content = $post['post_content'];
     }
     $post_fields = array('title' => get_the_title($post['ID']), 'status' => $post['post_status'], 'type' => $post['post_type'], 'author' => (int) $post['post_author'], 'content' => apply_filters('the_content', $post_content), 'parent' => (int) $post['post_parent'], 'link' => get_json_url_posts_list($post['ID']));
     $post_fields_extended = array('excerpt' => $this->prepare_excerpt($post['post_excerpt']), 'comment_status' => $post['comment_status'], 'comment_num' => (int) $comment_num);
     $post_fields_raw = array();
     if ($show_type == 'row') {
         $post_fields_raw = array('title_raw' => $post['post_title'], 'content_raw' => $post['post_content'], 'excerpt_raw' => $post['post_excerpt'], 'guid_raw' => $post['guid']);
     }
     // Dates
     $timezone = json_get_timezone();
     if ($post['post_date_gmt'] === '0000-00-00 00:00:00') {
         $post_fields['date'] = null;
         $post_fields_extended['date_tz'] = null;
         $post_fields_extended['date_gmt'] = null;
     } else {
         $post_date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $post['post_date'], $timezone);
         $post_fields['date'] = json_mysql_to_rfc3339($post['post_date']);
         $post_fields_extended['date_tz'] = $post_date->format('e');
         $post_fields_extended['date_gmt'] = json_mysql_to_rfc3339($post['post_date_gmt']);
     }
     if ($post['post_modified_gmt'] === '0000-00-00 00:00:00') {
         $post_fields['modified'] = null;
         $post_fields_extended['modified_tz'] = null;
         $post_fields_extended['modified_gmt'] = null;
     } else {
         $modified_date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $post['post_modified'], $timezone);
         $post_fields['modified'] = json_mysql_to_rfc3339($post['post_modified']);
         $post_fields_extended['modified_tz'] = $modified_date->format('e');
         $post_fields_extended['modified_gmt'] = json_mysql_to_rfc3339($post['post_modified_gmt']);
     }
     // Authorized fields
     // TODO: Send `Vary: Authorization` to clarify that the data can be
     // changed by the user's auth status
     if (json_check_post_permission($post, 'edit')) {
         $post_fields_extended['password'] = $post['post_password'];
     }
     // Consider future posts as published
     if ($post_fields['status'] === 'future') {
         $post_fields['status'] = 'publish';
     }
     // Fill in blank post format
     $post_fields['format'] = get_post_format($post['ID']);
     if (empty($post_fields['format'])) {
         $post_fields['format'] = 'standard';
     }
     if (0 === $post['post_parent']) {
         $post_fields['parent'] = null;
     }
     if (('view' === $context || 'view-revision' == $context) && 0 !== $post['post_parent']) {
         // Avoid nesting too deeply
         // This gives post + post-extended + meta for the main post,
         // post + meta for the parent and just meta for the grandparent
         $parent = get_post($post['post_parent'], ARRAY_A);
         $post_fields['parent'] = $this->prepare_post($parent, 'embed');
     }
     // Merge requested $post_fields fields into $_post
     $_post = array_merge($_post, $post_fields);
     // Include extended fields. We might come back to this.
     $_post = array_merge($_post, $post_fields_extended);
     if ('edit' === $context) {
         if (json_check_post_permission($post, 'edit')) {
             $_post = array_merge($_post, $post_fields_raw);
         } else {
             $GLOBALS['post'] = $previous_post;
             if ($previous_post) {
                 setup_postdata($previous_post);
             }
             json_error(BigAppErr::$post['code'], "post id is not valid", $id);
         }
     } elseif ('view-revision' == $context) {
         if (json_check_post_permission($post, 'edit')) {
             $_post = array_merge($_post, $post_fields_raw);
         } else {
             $GLOBALS['post'] = $previous_post;
             if ($previous_post) {
                 setup_postdata($previous_post);
             }
             return false;
         }
     }
     // Entity meta
     $links = array('self' => get_json_url_posts_list($post['ID']), 'author' => get_json_url_users_get_user($post['post_author']), 'collection' => get_json_url_posts_list());
     if ('view-revision' != $context) {
         $links['replies'] = get_json_url_comments_get_comments($post['ID']);
         $links['version-history'] = get_json_url_post_get_revisions($post['ID']);
     }
     #$_post['meta'] = array( 'links' => $links );
     if (!empty($post['post_parent'])) {
         $_post['meta']['links']['up'] = get_json_url_posts_list((int) $post['post_parent']);
     }
     $GLOBALS['post'] = $previous_post;
     if ($previous_post) {
         setup_postdata($previous_post);
     }
     //控制发表评论状态
     if ($_post['comment_status'] == 'closed') {
         $comment_type = 0;
     } else {
         $comment_type = bigapp_core::check_comment_status();
         if ($comment_type == 0 && $_post['comment_status'] == 'open') {
             $comment_type = 1;
         }
     }
     $_post['comment_type'] = $comment_type;
     //浏览量次数
     $post_views = new WP_JSON_PostViews($this->server);
     $_post['views'] = $post_views->get_views_by_id($post['ID']);
     return apply_filters('json_prepare_post', $_post, $post, $context);
 }
 /**
  * Get attachment-specific data
  *
  * @param array $post
  * @return array
  */
 protected function prepare_post($post, $context = 'single')
 {
     $data = parent::prepare_post($post, $context);
     if (is_wp_error($data) || $post['post_type'] !== 'attachment') {
         return $data;
     }
     // $thumbnail_size = current_theme_supports( 'post-thumbnail' ) ? 'post-thumbnail' : 'thumbnail';
     $data['source'] = wp_get_attachment_url($post['ID']);
     $data['is_image'] = wp_attachment_is_image($post['ID']);
     $data['attachment_meta'] = wp_get_attachment_metadata($post['ID']);
     // Ensure empty meta is an empty object
     if (empty($data['attachment_meta'])) {
         $data['attachment_meta'] = new stdClass();
     } elseif (!empty($data['attachment_meta']['sizes'])) {
         $img_url_basename = wp_basename($data['source']);
         foreach ($data['attachment_meta']['sizes'] as $size => &$size_data) {
             // Use the same method image_downsize() does
             $size_data['url'] = str_replace($img_url_basename, $size_data['file'], $data['source']);
         }
     } else {
         $data['attachment_meta']['sizes'] = new stdClass();
     }
     if (is_array($data['attachment_meta']['sizes']) && isset($data['attachment_meta']['sizes']['post-thumbnail'])) {
         //hardcode
         $data['source'] = $data['attachment_meta']['sizes']['post-thumbnail']['url'];
     }
     // Override entity meta keys with the correct links
     $data['meta'] = array('links' => array('self' => get_json_url_media_get_posts($post['ID']), 'author' => get_json_url_users_get_user($post['post_author']), 'collection' => get_json_url_media_get_posts(), 'replies' => get_json_url_media_get_comments($post['ID']), 'version-history' => get_json_url_media_get_revisions($post['ID'])));
     if (!empty($post['post_parent'])) {
         $data['meta']['links']['up'] = get_json_url_media_get_posts((int) $post['post_parent']);
     }
     return apply_filters('json_prepare_attachment', $data, $post, $context);
 }