/** * add new comment * @param int $id post id * @param string $comment comment value * @param int $parent_id 父评论的ID */ public function add_comment($id, $comment, $author = '', $email = '', $parent_id = 0, $type = 0) { if (empty($id) || empty($comment)) { json_error(BigAppErr::$comment['code'], "empty id or comment"); } $user_id = get_current_user_id(); $comment_type = bigapp_core::check_comment_status(); if ($comment_type == 2 && $user_id == 0) { if ($author == '' or $email == '') { json_error(BigAppErr::$comment['code'], 'need email or author'); } if (false == check_email($email)) { json_error(BigAppErr::$comment['code'], 'email format is wrong'); } } if ($comment_type == 3) { if ($user_id == 0) { json_error(BigAppErr::$login['code'], 'need login'); } } $commentdata = array("comment_post_ID" => $id, 'comment_content' => $comment, 'comment_approved' => 1, 'comment_author' => $author, 'comment_author_email' => $email, 'comment_parent' => $parent_id, "user_ID" => $user_id); $result = wp_new_comment($commentdata); if (!$result) { json_error(BigAppErr::$comment['code'], "creat new comment failed"); } return array('id' => $result); }
/** * 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); }