/** * Retrieve pages * * Overrides the $type to set to 'attachment', then passes through to the post * endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_posts($filter = array(), $context = 'view', $type = 'attachment', $page = 1) { if ($type !== 'attachment') { return new WP_Error('json_post_invalid_type', __('Invalid post type'), array('status' => 400)); } if (empty($filter['post_status'])) { $filter['post_status'] = array('publish', 'inherit'); // Always allow status queries for attachments add_filter('query_vars', array($this, 'allow_status_query')); } $posts = parent::get_posts($filter, $context, 'attachment', $page); return $posts; }
/** * Retrieve pages * * Overrides the $type to set to 'attachment', then passes through to the post * endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_posts($filter = array(), $context = 'view', $type = 'attachment', $page = 1) { if ($type !== 'attachment') { json_error(BigAppErr::$post['code'], BigAppErr::$post['msg'], ""); } if (empty($filter['post_status'])) { $filter['post_status'] = array('publish', 'inherit'); // Always allow status queries for attachments add_filter('query_vars', array($this, 'allow_status_query')); } $posts = parent::get_posts($filter, $context, 'attachment', $page); return $posts; }
/** * 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; }
/** * Retrieve posts * * Overrides the $type to set to $this->type, then passes through to the * post endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_posts($filter = array(), $context = 'view', $type = null, $page = 1) { if (!empty($type) && $type !== $this->type) { json_error(BigAppErr::$post['code'], BigAppErr::$post['msg'], ""); } return parent::get_posts($filter, $context, $this->type, $page); }
/** * Retrieve posts * * Overrides the $type to set to $this->type, then passes through to the * post endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_posts($filter = array(), $context = 'view', $type = null, $page = 1) { if (!empty($type) && $type !== $this->type) { return new WP_Error('json_post_invalid_type', __('Invalid post type'), array('status' => 400)); } return parent::get_posts($filter, $context, $this->type, $page); }
/** * 获取收藏列表 * 参数: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; } }
/** * 获取某个人已经发表过的评论.包括评论内容,评论时间,评论文章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; }