Пример #1
0
 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;
 }
 /**
  * Add meta to a post.
  *
  * Ensures that the correct location header is sent with the response.
  *
  * @param int $id Post ID
  * @param array $data {
  *     @type string|null $key Meta key
  *     @type string|null $key Meta value
  * }
  * @return bool|WP_Error
  */
 public function add_meta($id, $data)
 {
     $response = parent::add_meta($id, $data);
     if (is_wp_error($response)) {
         return $response;
     }
     $data = (object) $response->get_data();
     $response = new WP_JSON_Response();
     $response->header('Location', json_url('/posts/' . $id . '/meta/' . $data->ID));
     $response->set_data($data);
     $response = json_ensure_response($response);
     return $response;
 }
Пример #4
0
 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;
 }
Пример #5
0
/**
 * Handle OPTIONS requests for the server
 *
 * This is handled outside of the server code, as it doesn't obey normal route
 * mapping.
 *
 * @param mixed $response Current response, either response or `null` to indicate pass-through
 * @param WP_JSON_Server $handler ResponseHandler instance (usually WP_JSON_Server)
 * @return WP_JSON_ResponseHandler Modified response, either response or `null` to indicate pass-through
 */
function json_handle_options_request($response, $handler)
{
    if (!empty($response) || $handler->method !== 'OPTIONS') {
        return $response;
    }
    $response = new WP_JSON_Response();
    $accept = array();
    $handler_class = get_class($handler);
    $class_vars = get_class_vars($handler_class);
    $map = $class_vars['method_map'];
    foreach ($handler->get_routes() as $route => $endpoints) {
        $match = preg_match('@^' . $route . '$@i', $handler->path, $args);
        if (!$match) {
            continue;
        }
        foreach ($endpoints as $endpoint) {
            foreach ($map as $type => $bitmask) {
                if ($endpoint[1] & $bitmask) {
                    $accept[] = $type;
                }
            }
        }
        break;
    }
    $accept = array_unique($accept);
    $response->header('Accept', implode(', ', $accept));
    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;
 }
Пример #7
0
 /**
  * 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);
     return $response;
 }
 /**
  * Create a new user.
  *
  * @param $data
  * @return mixed
  */
 public function create_user($data)
 {
     if (!current_user_can('create_users')) {
         return new WP_Error('json_cannot_create', __('Sorry, you are not allowed to create users.'), array('status' => 403));
     }
     if (!empty($data['ID'])) {
         return new WP_Error('json_user_exists', __('Cannot create existing user.'), array('status' => 400));
     }
     $user_id = $this->insert_user($data);
     if (is_wp_error($user_id)) {
         return $user_id;
     }
     $response = $this->get_user($user_id);
     if (!$response instanceof WP_JSON_ResponseInterface) {
         $response = new WP_JSON_Response($response);
     }
     $response->set_status(201);
     $response->header('Location', json_url('/users/' . $user_id));
     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;
 }
Пример #10
0
 public function new_user()
 {
     $data = null;
     if (isset($_POST['data'])) {
         $data = json_decode(stripslashes($_POST['data']), ARRAY_N);
     }
     if (empty($data)) {
         return new WP_Error('json_money_invalid_data', __('Invalid data parameters.'), array('status' => 404));
     }
     if (!DLN_Helper_Decrypt::get_decrypt()) {
         return new WP_Error('json_user_invalid_code', __('Invalid data verify code.'), array('status' => 404));
     }
     //if ( ! current_user_can( 'create_users' ) ) {
     //	return new WP_Error( 'json_cannot_create', __( 'Sorry, you are not allowed to create users.' ), array( 'status' => 403 ) );
     //}
     if (!empty($data['ID'])) {
         return new WP_Error('json_user_exists', __('Cannot create existing user.'), array('status' => 400));
     }
     $user_id = $this->insert_user($data);
     if (is_wp_error($user_id)) {
         return $user_id;
     }
     $response = $this->get_user($user_id);
     if (!$response instanceof WP_JSON_ResponseInterface) {
         $response = new WP_JSON_Response($response);
     }
     $response->set_status(201);
     $response->header('Location', json_url('/users/' . $user_id));
     return $response;
 }
Пример #11
0
 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;
 }