/** * 定制化url * 获取分类/tag下面的所有的文章列表 */ public function get_posts_list_url_by_taxonomy($term) { if ($term->taxonomy == 'category') { $param['filter[cat]'] = $term->term_id; } elseif ($term->taxonomy == 'post_tag') { $param['filter[tag]'] = $term->name; } $url = get_json_url_posts_list(0, $param); return apply_filters('json_posts_url_by_taxonomy', $url); }
/** * 链接转换 * src:外链,直接pass * 内链,通过url路由规则,检测出具体参数 * type:1,外链 * 2,文章链接 * 3,菜单链接 * return :json api link */ function convert_link2json($src, $type = 0) { $dest = $src; switch ($type) { case 1: return $dest; break; case 2: case 3: global $wp_rewrite; $rewrite = $wp_rewrite->wp_rewrite_rules(); if ($rewrite != false) { //自定义链接 $home_path = trim(parse_url(home_url(), PHP_URL_PATH), '/'); $req_uri = trim(parse_url($src, PHP_URL_PATH), '/'); $req_uri = preg_replace("|^{$home_path}|i", '', $req_uri); $req_uri = trim($req_uri, '/'); $matches = null; foreach ((array) $rewrite as $match => $query) { if (preg_match("#^{$match}#", $req_uri, $matches) || preg_match("#^{$match}#", urldecode($req_uri), $matches)) { break; } } if ($matches) { $query = preg_replace("!^.+\\?!", '', $query); $query = addslashes(WP_MatchesMapRegex::apply($query, $matches)); parse_str($query, $param); $filter = array(); foreach ($param as $key => $value) { $filter["filter[{$key}]"] = $value; } $filter["filter[only_one]"] = 1; $dest = get_json_url_posts_list(0, $filter); } } //默认链接 $match = "p=([0-9]{1,})"; if (preg_match("#{$match}#", $src, $matches)) { $dest = get_json_url_posts_list($matches[1]); } break; default: } return $dest; }
/** * 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); }
/** * 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('get_comment_text', $comment->comment_content, $comment); // 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'; } // Parent if (('single' === $context || 'single-parent' === $context) && (int) $comment->comment_parent) { $parent_fields = array('meta'); if ($context === 'single') { $parent_fields[] = 'comment'; } $parent = get_comment($comment->comment_parent); show_debug($parent, __FILE__, __LINE__); $fields['parent'] = $this->prepare_comment($parent, $parent_fields, 'single-parent'); } else { $fields['parent'] = (int) $comment->comment_parent; } // Author if ((int) $comment->user_id !== 0) { $fields['author'] = (int) $comment->user_id; } else { if (empty($comment->comment_author)) { $a_name = __('Anonymous'); } else { $a_name = $comment->comment_author; } $fields['author'] = array('ID' => 0, 'name' => $a_name, 'URL' => $comment->comment_author_url, 'avatar' => json_get_avatar_url($comment->comment_author_email)); } // Date $timezone = json_get_timezone(); $comment_date = WP_JSON_DateTime::createFromFormat('Y-m-d H:i:s', $comment->comment_date, $timezone); $fields['date'] = $comment->comment_date; $fields['date_tz'] = $comment_date->format('e'); $fields['date_gmt'] = json_mysql_to_rfc3339($comment->comment_date_gmt); // Meta $meta = array('links' => array('up' => get_json_url_posts_list((int) $comment->comment_post_ID))); if (0 !== (int) $comment->comment_parent) { $meta['links']['in-reply-to'] = get_json_url_comments_get_comments((int) $comment->comment_post_ID, (int) $comment->comment_parent); } if ('single' !== $context) { $meta['links']['self'] = get_json_url_comments_get_comments((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); }