Ejemplo n.º 1
0
 /**
  * Create report from REST request and get response.
  *
  * @access public
  *
  * @param WP_REST_Request $request Current request.
  * @return WP_REST_Response $response Response for /reports endpoint.
  */
 public function report(\WP_REST_Request $request)
 {
     Reports::create($request->get_body_params());
     return self::reports($request);
 }
 /**
  * Retrieve a list of posts matching a search query or a single post by
  * ID
  *
  * @since 0.1
  */
 public function api_get_posts(WP_REST_Request $request)
 {
     $params = stripslashes_deep($request->get_body_params());
     if (!isset($params['s']) && !isset($params['ID'])) {
         return array();
     }
     if (!isset($params['return'])) {
         $params['return'] = array('ID' => 'ID', 'title' => 'title', 'description' => 'post_type_label');
     }
     $args = array('posts_per_page' => 50);
     if (isset($params['s'])) {
         $args['s'] = sanitize_text_field($params['s']);
     }
     if (isset($params['ID'])) {
         $args['post__in'] = is_array($params['ID']) ? array_map('absint', $params['ID']) : array(absint($params['ID']));
     }
     if (isset($params['post_type'])) {
         $args['post_type'] = is_array($params['post_type']) ? array_map('sanitize_text_field', $params['post_type']) : sanitize_text_field($params['post_type']);
     }
     if (isset($params['posts_per_page'])) {
         $args['posts_per_page'] = absint($params['posts_per_page']);
     }
     $query = new WP_Query($args);
     $posts = array();
     while ($query->have_posts()) {
         $query->the_post();
         $match = array();
         foreach ($params['return'] as $key => $type) {
             $match[$key] = $this->api_get_posts_post_data($type);
         }
         $posts[] = $match;
     }
     if (isset($params['s'])) {
         return array('s' => $params['s'], 'posts' => $posts);
     } elseif (isset($params['ID'])) {
         return array('ID' => $params['ID'], 'posts' => empty($posts) ? array() : $posts[0]);
     }
     return array();
 }