public function get_related($id = '', $filter = array(), $context = 'view') { $option = get_option('sirp_options'); $num = !empty($filter['num']) ? (int) $filter['num'] : (int) $option['display_num']; $ids = sirp_get_related_posts_id_api($num, $id); $posts_list = array(); foreach ($ids as $id) { $posts_list[] = get_post($id['ID']); } $response = new WP_JSON_Response(); if (!$posts_list) { $response->set_data(array()); return $response; } $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); 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; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
/** * Retrieve ranking * * Overrides the $type to set to 'post', then passes through to the post * endpoints. * * @see WP_JSON_Posts::get_posts() */ public function get_ranking($filter = array(), $context = 'view') { $ids = sga_ranking_get_date($filter); $posts_list = array(); foreach ($ids as $id) { $posts_list[] = get_post($id); } $response = new WP_JSON_Response(); 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; } $struct[] = $post_data; } $response->set_data($struct); return $response; }
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 a post. * * @uses get_post() * @param int $id Post ID * @param array $fields Post fields to return (optional) * @return array Post entity */ public function display_post($id, $context = 'view') { $id = (int) $id; if (empty($id)) { $this->set_status(404); return array('message' => __('Invalid Request ID.')); } $post = get_post($id, ARRAY_A); if (empty($post['ID'])) { $this->set_status(404); return array('message' => __('Invalid Request ID.')); } if (!$this->check_read_permission($post)) { //return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 401)); } // Link headers (see RFC 5988) $response = new WP_JSON_Response(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', $post['post_modified_gmt']) . 'GMT'); $post = $this->prepare_post($post, $context); if (is_wp_error($post)) { return $post; } foreach ($post['meta']['links'] as $rel => $url) { $response->link_header($rel, $url); } $post = $this->format_get_data($post, $id); $response->link_header('alternate', get_permalink($id), array('type' => 'text/html')); $response->set_data($post); return $response; }
/** * Retrieve posts. We need to override last modified date * * @since 6.4.9 * * 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 = 'edit', $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(); // Modified now, no cache $response->header('Cache-Control', 'no-cache, no-store, must-revalidate'); $response->header('Expires', 'Wed, 11 Jan 1984 05:00:00 GMT'); $response->header('Pragma', 'no-cache'); $response->header('Last-Modified', gmdate('D, d M Y H:i:s') . ' 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 a post. * * @uses get_post() * @param int $id Post ID * @param string $context The context; 'view' (default) or 'edit'. * @return array Post entity */ public function get_post($id, $context = 'view') { $id = (int) $id; $post = get_post($id, ARRAY_A); if (empty($id) || empty($post['ID'])) { return new WP_Error('json_post_invalid_id', __('Invalid post ID.'), array('status' => 404)); } $checked_permission = 'read'; if ('inherit' === $post['post_status'] && $post['post_parent'] > 0) { $checked_post = get_post($post['post_parent'], ARRAY_A); if ('revision' === $post['post_type']) { $checked_permission = 'edit'; } } else { $checked_post = $post; } if (!json_check_post_permission($checked_post, $checked_permission)) { return new WP_Error('json_user_cannot_read', __('Sorry, you cannot read this post.'), array('status' => 401)); } // Link headers (see RFC 5988) $response = new WP_JSON_Response(); $response->header('Last-Modified', mysql2date('D, d M Y H:i:s', $post['post_modified_gmt']) . 'GMT'); $post = $this->prepare_post($post, $context); if (is_wp_error($post)) { return $post; } foreach ($post['meta']['links'] as $rel => $url) { $response->link_header($rel, $url); } $response->link_header('alternate', get_permalink($id), array('type' => 'text/html')); $response->set_data($post); //<!--peter modify,support wordpress popular posts plugin if (is_plugin_active('wordpress-popular-posts/wordpress-popular-posts.php')) { //plugin is activated $this->__update_views($post['ID']); } //peter modify,support wordpress popular posts plugin--> return $response; }
public function get_dln_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); // 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 ( ! $this->check_read_permission( $post ) ) // continue; $response->link_header('item', json_url('/posts/' . $post['ID']), array('title' => $post['post_title'])); $struct[] = $this->prepare_post($post, $context); } $response->set_data($struct); return $response; }