/** * Setup hook to prepare returned form. Setup field/choice attributes with callbacks * * @param WP_JSON_ResponseHandler $server * @since 6.0 */ public function __construct($server) { parent::__construct($server); add_filter('json_prepare_post', array($this, 'filter_prepare_post'), 10, 3); $this->field_attribute_keys = apply_filters('ccf_field_attributes', array('type' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'slug' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'placeholder' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'className' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'label' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'), 'description' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'), 'value' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'), 'required' => array('sanitize' => array($this, 'boolval'), 'escape' => array($this, 'boolval')), 'showDate' => array('sanitize' => array($this, 'boolval'), 'escape' => array($this, 'boolval')), 'addressType' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'siteKey' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'secretKey' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'phoneFormat' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'emailConfirmation' => array('sanitize' => array($this, 'boolval'), 'escape' => array($this, 'boolval')), 'showTime' => array('sanitize' => array($this, 'boolval'), 'escape' => array($this, 'boolval')), 'heading' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'), 'subheading' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'), 'html' => array('sanitize' => 'wp_kses_post', 'escape' => 'wp_kses_post'), 'maxFileSize' => array('sanitize' => 'intval', 'escape' => 'intval'), 'fileExtensions' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'))); $this->choice_attribute_keys = apply_filters('ccf_choice_attributes', array('label' => array('sanitize' => 'sanitize_text_field', 'escape' => 'esc_html'), 'value' => array('sanitize' => 'esc_attr', 'escape' => 'esc_attr'), 'selected' => array('sanitize' => array($this, 'boolval'), 'escape' => array($this, 'boolval')))); }
/** * Gets groups of posts * * @global WP $wp Current WordPress environment instance. * @param array $type Array of post type slugs * @return stdClass[] Collection of Post entities */ public function get_post_groups($type = array('post')) { global $wp; $groups = array(); foreach ($this->server->params['GET'] as $var => $value) { $varParts = explode(':', $var); if (count($varParts) > 1 && $varParts[1] == 'filter') { $groups[$varParts[0]] = $value; } } $wp_posts = new WP_JSON_Posts($this->server); $posts = array(); foreach ($groups as $name => $filters) { $posts[$name] = $wp_posts->get_posts($filters, 'view', $type); } return $posts; }
/** * Prepare post data * * @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') { $_post = parent::prepare_post($post, $context); if (is_wp_error($_post)) { return $_post; } // Override entity meta keys with the correct links $_post['meta'] = array('links' => array('self' => json_url($this->base . '/' . $post['ID']), 'author' => json_url('/users/' . $post['post_author']), 'collection' => json_url($this->base), 'replies' => json_url($this->base . '/' . $post['ID'] . '/comments'), 'version-history' => json_url($this->base . '/' . $post['ID'] . '/revisions'))); if (!empty($post['post_parent'])) { $_post['meta']['links']['up'] = json_url($this->base . '/' . $post['ID']); } return apply_filters("json_prepare_{$this->type}", $_post, $post, $context); }
/** * Delete a attachment * * @see WP_JSON_Posts::delete_post() */ public function delete_post($id, $force = false) { $id = (int) $id; if (empty($id)) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } $post = get_post($id, ARRAY_A); if ($post['post_type'] !== 'attachment') { return new WP_Error('json_post_invalid_type', __('Invalid post type'), array('status' => 400)); } return parent::delete_post($id, $force); }
/** * 获取收藏列表 * 参数:page 控制分页的 * posts_per_page */ public function list_favorites($filter = array()) { if (!is_user_logged_in() && get_option(BigAppConf::$option_favorite_switch, 0) == 0) { //默认,只要登录,就可以添加 json_error(BigAppErr::$login['code'], BigAppErr::$login['msg'], __lan("need login")); } $favorite_post_ids = $this->_get_users_favorites(); if ($favorite_post_ids) { $post = new WP_JSON_Posts($this->server); $filter['_bigapp_post_ids'] = $favorite_post_ids; $response = $post->get_posts($filter); $lists = $response->get_data(); foreach ($lists as $list) { // 过滤置顶文章 if (in_array($list['ID'], $favorite_post_ids)) { $struct[] = $list; } $response->set_data($struct); } return $response; } }
/** * Delete a attachment * * @see WP_JSON_Posts::delete_post() */ public function delete_post($id, $force = false) { $id = (int) $id; if (empty($id)) { json_error(BigAppErr::$post['code'], BigAppErr::$post['msg'], ""); } $post = get_post($id, ARRAY_A); if ($post['post_type'] !== 'attachment') { json_error(BigAppErr::$post['code'], BigAppErr::$post['msg'], ""); } return parent::delete_post($id, $force); }
/** * 获取某个人已经发表过的评论.包括评论内容,评论时间,评论文章id/title/url * 默认的是每页10个. * @param filter= array(pre_page,page) * return */ public function my_comments($filter = array(), $page = 10) { $struct = array(); $number = 10; //每页的评论数目 if (isset($filter['pre_page'])) { $number = intval($filter['pre_page']); } $offset = 0; //偏移量 if (isset($filter['page']) && $filter['page'] > 0) { $offset = (intval($filter['page']) - 1) * $number; } else { $offset = $page > 0 ? ($page - 1) * $number : 0; } $user_id = get_current_user_id(); show_debug("my_comments:user_id" . $user_id, __FILE__, __LINE__); if ($user_id == 0) { //not login return empty } else { $arg['orderby'] = 'comment_date'; $arg['order'] = 'desc'; $arg['number'] = $number; $arg['offset'] = $offset; $arg['user_id'] = $user_id; $comments = get_comments($arg); $post_ids = array(); $post_infos = array(); foreach ($comments as $comment) { $post_ids[] = intval($comment->comment_post_ID); } show_debug($post_ids, __FILE__, __LINE__); if ($post_ids) { $post_model = new WP_JSON_Posts($this->server); $filter['_bigapp_post_ids'] = $post_ids; $response = $post_model->get_posts($filter); $lists = $response->get_data(); foreach ($lists as $list) { $post_info[$list['ID']] = $list; } } foreach ($comments as $comment) { $comment = $this->prepare_comment($comment, array('comment', 'meta'), 'collection'); if (isset($post_info[$comment['post']])) { $comment['post_info']['title'] = $post_info[$comment['post']]['title']; $comment['post_info']['link'] = $post_info[$comment['post']]['link']; } else { continue; } $struct[] = $comment; } } return $struct; }