public function tax_query($data) { $allowed = array('post_type', 'tax_query'); foreach ($data as $key => $value) { if (!in_array($key, $allowed)) { unset($data[$key]); } } if (!is_array($data) || empty($data) || !isset($data['tax_query'])) { return new WP_Error('jp_api_tax_query', __('Invalid tax query.'), array('status' => 500)); } $post_query = new WP_Query(); $posts_list = $post_query->query($data); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (json_check_post_permission($post, 'read')) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, 'view'); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
/** * Retrieve posts. * * @since 3.4.0 * * The optional $filter parameter modifies the query used to retrieve posts. * Accepted keys are 'post_type', 'post_status', 'number', 'offset', * 'orderby', and 'order'. * * The optional $fields parameter specifies what fields will be included * in the response array. * * @uses wp_get_recent_posts() * @see WP_JSON_Posts::get_post() for more on $fields * @see get_posts() for more on $filter values * * @param array $filter Parameters to pass through to `WP_Query` * @param string $context * @param string|array $type Post type slug, or array of slugs * @param int $page Page number (1-indexed) * @return stdClass[] Collection of Post entities */ public function list_posts($filter = array(), $context = 'view', $type = 'posts', $page = 1) { $query = array(); //Block screen ids $internal_menu_screens = array("100", "1100", "1200", "1300", "1400", "1500", "2100", "5200"); //swap type to screen id and screen_id to type if ($type != 'posts') { $screens = $this->get_screens(); $screen_id = $type; $type = $screens[$type]; } else { $this->set_status(404); return array('message' => __('Invalid Type Param.')); } //Redirect to other function and return weather api service if ($type == 'weather-api') { $screen_attrs = $this->get_screen_attrs(); $struct = $this->get_weather($type); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'result' => $struct); return $data; } //Redirect to other function and return dashboard and screen service if (in_array($screen_id, $internal_menu_screens)) { /* if ($parent == '9301') { return $this->get_screens_data(); } */ $screen_attrs = $this->get_screen_attrs(); $screen_image = ""; //$page_id = $this->get_ID_by_page_slug('dashboard'); $page_id = $this->get_post_id_by_screen_id($screen_id); $screen_image_data = get_field('screen_image', $page_id); $screen_image = empty($screen_image_data['url']) || $screen_image_data['url'] == 'null' || $screen_image_data['url'] == null ? "" : $screen_image_data['url']; // if($screen_id=='2100' || $screen_id=='1500') { // return array($page_id); // } $struct = $this->get_screen_content($page_id); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => $screen_image, 'result' => $struct); return $data; } //Redirect to other function and return in room ordering service if ($type == 'in-room-ordering') { $screen_attrs = $this->get_screen_attrs(); //$struct = $this->get_taxonomy_terms('food_category'); $struct = $this->get_menu_category('food_category'); $total_menu_count = !empty($struct['total_menu_count']) ? $struct['total_menu_count'] : 0; unset($struct['total_menu_count']); $data = array('count' => count($struct), 'total_menu_count' => $total_menu_count, 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'currency' => "\$", 'result' => $struct); return $data; } //Redirect to other function and return todays events service if ($type == 'todays-events') { $screen_attrs = $this->get_screen_attrs(); $struct = $this->get_list_events($type); $banner_data = $this->get_banner_image_by_slug('events'); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'banner_image' => $banner_data['image'], 'description' => $banner_data['description'], 'result' => $struct); return $data; } //Redirect to other function and return activity guide service if ($type == 'activity-guide') { $screen_attrs = $this->get_screen_attrs(); $struct = $this->get_list_activity_guide($type); $banner_data = $this->get_banner_image_by_slug('activity-guide'); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'banner_image' => $banner_data['image'], 'description' => $banner_data['description'], 'title' => 'Activities and Events', 'weekday_title' => 'Week day Activities', 'weekend_title' => 'Weekend Activities', 'result' => $struct); return $data; } // Validate post types and permissions $query['post_type'] = array(); foreach ((array) $type as $type_name) { $post_type = get_post_type_object($type_name); if (!(bool) $post_type || !$post_type->show_in_json) { return new WP_Error('json_invalid_post_type', sprintf(__('The post type "%s" is not valid'), $type_name), array('status' => 403)); } $query['post_type'][] = $post_type->name; } global $wp; // Allow the same as normal WP $valid_vars = apply_filters('query_vars', $wp->public_query_vars); // If the user has the correct permissions, also allow use of internal // query parameters, which are only undesirable on the frontend // // To disable anyway, use `add_filter('json_private_query_vars', '__return_empty_array');` if (current_user_can($post_type->cap->edit_posts)) { $private = apply_filters('json_private_query_vars', $wp->private_query_vars); $valid_vars = array_merge($valid_vars, $private); } // Define our own in addition to WP's normal vars $json_valid = array('posts_per_page'); $valid_vars = array_merge($valid_vars, $json_valid); // Filter and flip for querying $valid_vars = apply_filters('json_query_vars', $valid_vars); $valid_vars = array_flip($valid_vars); // Exclude the post_type query var to avoid dodging the permission // check above unset($valid_vars['post_type']); foreach ($valid_vars as $var => $index) { if (isset($filter[$var])) { $query[$var] = apply_filters('json_query_var-' . $var, $filter[$var]); } } // Special parameter handling $query['paged'] = absint($page); $query['posts_per_page'] = -1; $post_query = new WP_Query(); $posts_list = $post_query->query($query); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); /* if (!$posts_list) { $response->set_data(array()); return $response; } */ // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!$this->check_read_permission($post)) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $post_data['tag_name'] = $post['post_name']; $post_data = $this->format_get_data($post_data, $post['ID']); $struct[] = $post_data; } $screen_attrs = $this->get_screen_attrs(); $data = array('count' => count($struct), 'screen_id' => $screen_id, 'screen_title' => $screen_attrs[$screen_id], 'screen_image' => "", 'result' => $struct); $response->set_data($data); return $response; }
/** * Retrieve posts. * * @since 3.4.0 * * The optional $filter parameter modifies the query used to retrieve posts. * Accepted keys are 'post_type', 'post_status', 'number', 'offset', * 'orderby', and 'order'. * * @uses wp_get_recent_posts() * @see get_posts() for more on $filter values * * @param array $filter Parameters to pass through to `WP_Query` * @param string $context The context; 'view' (default) or 'edit'. * @param string|array $type Post type slug, or array of slugs * @param int $page Page number (1-indexed) * @return stdClass[] Collection of Post entities */ public function get_posts($filter = array(), $context = 'view', $type = 'post', $page = 1) { $query = array(); // Validate post types and permissions $query['post_type'] = array(); foreach ((array) $type as $type_name) { $post_type = get_post_type_object($type_name); if (!(bool) $post_type || !$post_type->show_in_json) { return new WP_Error('json_invalid_post_type', sprintf(__('The post type "%s" is not valid'), $type_name), array('status' => 403)); } $query['post_type'][] = $post_type->name; } global $wp; // Allow the same as normal WP $valid_vars = apply_filters('query_vars', $wp->public_query_vars); // If the user has the correct permissions, also allow use of internal // query parameters, which are only undesirable on the frontend // // To disable anyway, use `add_filter('json_private_query_vars', '__return_empty_array');` if (current_user_can($post_type->cap->edit_posts)) { $private = apply_filters('json_private_query_vars', $wp->private_query_vars); $valid_vars = array_merge($valid_vars, $private); } // Define our own in addition to WP's normal vars $json_valid = array('posts_per_page'); $valid_vars = array_merge($valid_vars, $json_valid); // Filter and flip for querying $valid_vars = apply_filters('json_query_vars', $valid_vars); $valid_vars = array_flip($valid_vars); // Exclude the post_type query var to avoid dodging the permission // check above unset($valid_vars['post_type']); foreach ($valid_vars as $var => $index) { if (isset($filter[$var])) { $query[$var] = apply_filters('json_query_var-' . $var, $filter[$var]); } } // Special parameter handling $query['paged'] = absint($page); $post_query = new WP_Query(); $posts_list = $post_query->query($query); $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!json_check_post_permission($post, 'read')) { continue; } // $response->link_header( 'item', json_url( '/posts/' . $post['ID'] ), array( 'title' => $post['post_title'] ) ); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
/** * Retrieve posts. * * @since 3.4.0 * * The optional $filter parameter modifies the query used to retrieve posts. * Accepted keys are 'post_type', 'post_status', 'number', 'offset', * 'orderby', 'order','s','cat','post_per_page'. * * @uses wp_get_recent_posts() * @see get_posts() for more on $filter values * * @param array $filter Parameters to pass through to `WP_Query` * @param string $context The context; 'view' (default) or 'edit'. * @param string|array $type Post type slug, or array of slugs * @param int $page Page number (1-indexed) * @return stdClass[] Collection of Post entities */ public function get_posts($filter = array(), $context = 'view', $type = 'post', $page = 1) { $query = array(); // Validate post types and permissions $query['post_type'] = array(); foreach ((array) $type as $type_name) { $post_type = get_post_type_object($type_name); //如果不允许show_in_json,那么则报错,说明不允许json展示,直接拒绝,否则将这个信息加入 if (!(bool) $post_type || !$post_type->show_in_json) { json_error(BigAppErr::$post['code'], "Invalid post type"); } //只有这几个值:post/page/attachment/revision/nav_menu_item $query['post_type'][] = $post_type->name; } global $wp; // Allow the same as normal WP $valid_vars = apply_filters('query_vars', $wp->public_query_vars); // If the user has the correct permissions, also allow use of internal // query parameters, which are only undesirable on the frontend // // To disable anyway, use `add_filter('json_private_query_vars', '__return_empty_array');` //private_query_vars: ["offset","posts_per_page","posts_per_archive_page","showposts","nopaging","post_type","post_status","category__in","category__not_in","category__and","tag__in","tag__not_in","tag__and","tag_slug__in","tag_slug__and","tag_id","post_mime_type","perm","comments_per_page","post__in","post__not_in","post_parent","post_parent__in","post_parent__not_in"] if (current_user_can($post_type->cap->edit_posts)) { $private = apply_filters('json_private_query_vars', $wp->private_query_vars); $valid_vars = array_merge($valid_vars, $private); } // Define our own in addition to WP's normal vars $json_valid = array('posts_per_page'); $valid_vars = array_merge($valid_vars, $json_valid); // Filter and flip for querying $valid_vars = apply_filters('json_query_vars', $valid_vars); $valid_vars = array_flip($valid_vars); // Exclude the post_type query var to avoid dodging the permission // check above unset($valid_vars['post_type']); //["m","p","posts","w","cat","withcomments","withoutcomments","s","search","exact","sentence","calendar","page","paged","more","tb","pb","author","order","orderby","year","monthnum","day","hour","minute","second","name","category_name","tag","feed","author_name","static","pagename","page_id","error","comments_popup","attachment","attachment_id","subpost","subpost_id","preview","robots","taxonomy","term","cpage","post_type","post_format","json_route","posts_per_page"] //对所有的query_var做过滤 foreach ($valid_vars as $var => $index) { if (isset($filter[$var])) { $query[$var] = apply_filters('json_query_var-' . $var, $filter[$var]); } } //后门为收藏列表留的 if (isset($filter['_bigapp_post_ids'])) { if (!is_array($filter['_bigapp_post_ids'])) { $filter['_bigapp_post_ids'] = explode($filter['_bigapp_post_ids'], ','); } $query['post__in'] = $filter['_bigapp_post_ids']; } // Special parameter handling $query['paged'] = absint($page); show_debug($query, __FILE__, __LINE__); //重新建立主循环,从DB中找出指定文章 $post_query = new WP_Query(); $posts_list = $post_query->query($query); show_debug($posts_list, __FILE__, __LINE__); $response = new WP_JSON_Response(); //输出页码等信息到头部 //但期望是能输出到json中 $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $only_one = 0; $show_type = 'list'; if (isset($filter['only_one']) && $filter['only_one'] == 1) { //这地方是为了banner链接是文章的带有搜索条件的情况,设置的标示 $only_one = 1; $show_type = 'row'; } foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!json_check_post_permission($post, 'read')) { continue; } $post_data = $this->prepare_post($post, $context, $show_type); if ($post_data == false) { continue; } $struct[] = $post_data; } if ($only_one == 1) { $response->set_data(count($struct) ? $struct[0] : $struct); } else { $response->set_data($struct); } return $response; }
/** * Retrieve posts. * * @since 3.4.0 * * The optional $filter parameter modifies the query used to retrieve posts. * Accepted keys are 'post_type', 'post_status', 'number', 'offset', * 'orderby', and 'order'. * * @uses wp_get_recent_posts() * @see get_posts() for more on $filter values * * @param array $filter Parameters to pass through to `WP_Query` * @param string $context The context; 'view' (default) or 'edit'. * @param string|array $type Post type slug, or array of slugs * @param int $page Page number (1-indexed) * @return stdClass[] Collection of Post entities */ public function get_posts($filter = array(), $context = 'view', $type = 'post', $page = 1) { $query = array(); // Validate post types and permissions $query['post_type'] = array(); foreach ((array) $type as $type_name) { $post_type = get_post_type_object($type_name); if (!(bool) $post_type || !$post_type->show_in_json) { return new WP_Error('json_invalid_post_type', sprintf(__('The post type "%s" is not valid'), $type_name), array('status' => 403)); } $query['post_type'][] = $post_type->name; } global $wp; // Allow the same as normal WP $valid_vars = apply_filters('query_vars', $wp->public_query_vars); // If the user has the correct permissions, also allow use of internal // query parameters, which are only undesirable on the frontend // // To disable anyway, use `add_filter('json_private_query_vars', '__return_empty_array');` if (current_user_can($post_type->cap->edit_posts)) { $private = apply_filters('json_private_query_vars', $wp->private_query_vars); $valid_vars = array_merge($valid_vars, $private); } // Define our own in addition to WP's normal vars $json_valid = array('posts_per_page', 'ignore_sticky_posts', 'post_parent', 'meta_key'); $valid_vars = array_merge($valid_vars, $json_valid); // Filter and flip for querying $valid_vars = apply_filters('json_query_vars', $valid_vars); $valid_vars = array_flip($valid_vars); // Exclude the post_type query var to avoid dodging the permission // check above unset($valid_vars['post_type']); foreach ($valid_vars as $var => $index) { if (isset($filter[$var])) { $query[$var] = apply_filters('json_query_var-' . $var, $filter[$var]); } } //<!--peter modify, for bbpress sticky post if ($query['post_type'][0] == 'topic' && count($query['post_type']) == 1) { //get super sticky $super_sticky = get_option('_bbp_super_sticky_topics'); //forum id = post_parent $forum_sticky = get_post_meta($query['post_parent'], '_bbp_sticky_topics', true); $posts_sticky = null; if ($super_sticky == false) { //no super sticky if ($forum_sticky == false) { //no forum sticky $sticky = array(0); $posts_sticky = null; } else { $query2 = array('post_type' => 'topic', 'post__in' => $forum_sticky); $post_query2 = new WP_Query(); $posts_list2 = $post_query2->query($query2); $sticky = $forum_sticky; $posts_sticky = $posts_list2; } } else { $query1 = array('post_type' => 'topic', 'post__in' => $super_sticky); $post_query1 = new WP_Query(); $posts_list1 = $post_query1->query($query1); if ($forum_sticky == false) { //no forum sticky $sticky = $super_sticky; $posts_sticky = $posts_list1; } else { $query2 = array('post_type' => 'topic', 'post__in' => $forum_sticky); $post_query2 = new WP_Query(); $posts_list2 = $post_query2->query($query2); $sticky = array_merge($super_sticky, $forum_sticky); $posts_sticky = array_merge($posts_list1, $posts_list2); } } //exclude sticky post $query['post__not_in'] = $sticky; } //--> // Special parameter handling $query['paged'] = absint($page); //***validate cookie ,peter modify //modified@20150918,發現server.php那隻的登入不會繼承至此,故再登入一次 $user_id = wp_validate_auth_cookie($cookie, 'logged_in'); if ($user_id) { //echo 'user_id' . $user_id . '<br>'; $user = get_userdata($user_id); wp_set_current_user($user->ID, $user->user_login); } else { //echo 'validate failed<br>'; //do nothing } //***validate cookie ,peter modify $post_query = new WP_Query(); $posts_list = $post_query->query($query); //<!--peter modify if process bbpress sticky, merge result if ($query['post_type'][0] == 'topic' && count($query['post_type']) == 1 && $page == 1) { if (!empty($posts_sticky)) { //merge $posts_list = array_merge($posts_sticky, $posts_list); } } //--> $response = new WP_JSON_Response(); $response->query_navigation_headers($post_query); if (!$posts_list) { $response->set_data(array()); return $response; } // holds all the posts data $struct = array(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', get_lastpostmodified('GMT'), 0) . ' GMT'); foreach ($posts_list as $post) { $post = get_object_vars($post); // Do we have permission to read this post? if (!json_check_post_permission($post, 'read')) { continue; } $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $post_data = $this->prepare_post($post, $context); if (is_wp_error($post_data)) { continue; } $struct[] = $post_data; } $response->set_data($struct); return $response; }