diff --git a/api/v1/controllers/Posts.php b/api/v1/controllers/Posts.php index 2cd0ee1..42425f7 100644 --- a/api/v1/controllers/Posts.php +++ b/api/v1/controllers/Posts.php @@ -24,16 +24,11 @@ public static function find( $app ) { $app->lastModified( strtotime( $lastModified . ' GMT' ) ); } - if( empty( $args['post_status'] ) ) { - //if no post status is set, the user does not have privelages to view any that were given in the request - $posts = array(); - } else { - $model = self::model(); + $model = self::model(); - $posts = $model->find( $args, $found ); - - array_walk( $posts, array( __CLASS__, 'format' ), 'read' ); - } + $posts = $model->find( $args, $found ); + + array_walk( $posts, array( __CLASS__, 'format' ), 'read' ); return empty( $args['no_found_rows'] ) ? compact( 'posts', 'found' ) : compact( 'posts' ); } @@ -164,6 +159,10 @@ protected static function convert_request( $request_args ) { $request_args['post_status'] = 'publish'; } else { $request_args['post_status'] = array_filter( $request_args['post_status'], function( $status ) use ( $request_args ) { + if($status =='inherit') { + return true; + } + $status_obj = get_post_status_object( $status ); if ( !$status_obj ) { return false; @@ -205,7 +204,9 @@ protected static function convert_request( $request_args ) { return true; }); - + if(empty($request_args['post_status'])) { + unset($request_args['post_status']); + } } if ( isset( $request_args['author'] ) ) { diff --git a/api/v1/models/Posts.php b/api/v1/models/Posts.php index 4b49f2d..0fdb610 100644 --- a/api/v1/models/Posts.php +++ b/api/v1/models/Posts.php @@ -8,14 +8,26 @@ public function find( $args = array( ), &$found = null ) { //add filter for before/after handling, hopefully more complex date querying //will exist by wp3.7 if ( isset( $args['before'] ) || isset( $args['after'] ) ) { - add_filter( 'posts_where', array( __CLASS__, '_filter_posts_where_handleDateRange' ), 10, 2 ); + add_filter( 'posts_where', array( $this, '_filter_posts_where_handleDateRange' ), 10, 2 ); + } + + if( isset( $args['post_type'] ) && in_array('attachment', (array) $args['post_type'])) { + if(empty($args['post_status'])) { + $args['post_status'] = array('inherit'); + } else { + $args['post_status'] = array_merge((array) $args['post_status'], array('inherit')); + } + } + + if( empty( $args['post_status'] ) ) { + //a post_status is required + return array(); } if(isset($args['per_page'])) { $args['posts_per_page'] = $args['per_page']; unset($args['per_page']); } - $wp_posts = new \WP_Query( $args ); if ( $wp_posts->have_posts() ) { @@ -30,7 +42,7 @@ public function findById($id) { return get_post($id); } - public static function _filter_posts_where_handleDateRange( $where, $wp_query ) { + public function _filter_posts_where_handleDateRange( $where, $wp_query ) { if ( ($before = $wp_query->get( 'before' ) ) && $beforets = strtotime( $before ) ) { if ( preg_match( '$:[0-9]{2}\s[+-][0-9]{2}$', $before ) || strpos( $before, 'GMT' ) !== false ) { //adjust to site time if a timezone was set in the timestamp @@ -47,7 +59,8 @@ public static function _filter_posts_where_handleDateRange( $where, $wp_query ) $where .= sprintf( " AND post_date > '%s'", gmdate( 'Y-m-d H:i:s', $afterts ) ); } + remove_filter('posts_search', array($this, __METHOD__)); return $where; } - + } \ No newline at end of file diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh old mode 100644 new mode 100755 diff --git a/tests/v1/controllers/test-Posts.php b/tests/v1/controllers/test-Posts.php index 978f198..0b58a7a 100644 --- a/tests/v1/controllers/test-Posts.php +++ b/tests/v1/controllers/test-Posts.php @@ -82,6 +82,7 @@ protected function _make_attachment( $upload, $parent_post_id = -1 ) { } public function testGetPosts() { + $this->_insert_post(); list($status, $headers, $body) = $this->_getResponse( array( 'REQUEST_METHOD' => 'GET', 'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/posts', @@ -94,9 +95,32 @@ public function testGetPosts() { $this->assertInternalType( 'object', $data ); $this->assertObjectHasAttribute( 'posts', $data ); $this->assertInternalType( 'array', $data->posts ); + $this->assertNotEmpty($data->posts); $this->assertObjectNotHasAttribute( 'found', $data ); } + public function testGetAttachments() { + wp_set_current_user(1); + $upload = $this->_upload_file( dirname( dirname( __DIR__ ) ) . '/data/250x250.png' ); + $attachment_id = $this->_make_attachment( $upload ); + + list($status, $headers, $body) = $this->_getResponse( array( + 'REQUEST_METHOD' => 'GET', + 'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/posts', + 'QUERY_STRING' => 'post_type=attachment', + ) ); + + $data = json_decode( $body ); + + $this->assertEquals( '200', $status ); + $this->assertInternalType( 'object', $data ); + $this->assertObjectHasAttribute( 'posts', $data ); + $this->assertInternalType( 'array', $data->posts ); + $this->assertNotEmpty($data->posts); + $this->assertObjectNotHasAttribute( 'found', $data ); + wp_set_current_user(0); + } + public function testLastGetPostsModified() { $this->_insert_post(); @@ -107,7 +131,7 @@ public function testLastGetPostsModified() { ) ); $this->assertEquals( '200', $status ); - $this->assertNotEmpty( $headers['last-modified']); + $this->assertNotEmpty( $headers['last-modified'] ); $last_modified = $headers['last-modified']; list($status, $headers, $body) = $this->_getResponse( array( @@ -124,9 +148,9 @@ public function testLastGetPostsModified() { public function testGetPostsStatus() { $post_id = $this->_insert_post( array( 'post_status' => 'future', - 'post_date' => date('Y-m-d H:i:s', time() + 604800 ), + 'post_date' => date( 'Y-m-d H:i:s', time() + 604800 ), 'post_date_gmt' => '', - )); + ) ); list($status, $headers, $body) = $this->_getResponse( array( 'REQUEST_METHOD' => 'GET', @@ -135,7 +159,7 @@ public function testGetPostsStatus() { ) ); $data = json_decode( $body ); - + $this->assertEquals( '200', $status ); $this->assertInternalType( 'object', $data ); $this->assertObjectHasAttribute( 'posts', $data ); @@ -143,7 +167,7 @@ public function testGetPostsStatus() { $this->assertCount( 0, $data->posts ); $this->assertObjectNotHasAttribute( 'found', $data ); - wp_set_current_user(1); + wp_set_current_user( 1 ); list($status, $headers, $body) = $this->_getResponse( array( 'REQUEST_METHOD' => 'GET', @@ -152,7 +176,7 @@ public function testGetPostsStatus() { ) ); $data = json_decode( $body ); - wp_set_current_user(0); //log back out for other tests. + wp_set_current_user( 0 ); //log back out for other tests. $this->assertEquals( '200', $status ); $this->assertInternalType( 'object', $data ); @@ -160,8 +184,6 @@ public function testGetPostsStatus() { $this->assertInternalType( 'array', $data->posts ); $this->assertCount( 1, $data->posts ); $this->assertObjectNotHasAttribute( 'found', $data ); - - } public function testGetPostsPerPage() { @@ -329,7 +351,7 @@ public function testGetPostLastModified() { ) ); $this->assertEquals( '200', $status ); - $this->assertNotEmpty( $headers['last-modified']); + $this->assertNotEmpty( $headers['last-modified'] ); $last_modified = $headers['last-modified']; list($status, $headers, $body) = $this->_getResponse( array( @@ -393,7 +415,7 @@ public function testGetPostsCustomTaxonomy() { register_taxonomy( 'test_tax', 'post', array( 'public' => true ) ); $post_id = $this->_insert_post(); - if($term_obj = get_term_by('name', 'Test Term', 'test_tax')){ + if ( $term_obj = get_term_by( 'name', 'Test Term', 'test_tax' ) ) { $term_id = $term_obj->term_id; } else { $term_data = wp_insert_term( 'Test Term', 'test_tax' ); @@ -411,7 +433,7 @@ public function testGetPostsCustomTaxonomy() { ) ); $data = json_decode( $body ); - + $this->assertEquals( '200', $status ); $this->assertInternalType( 'object', $data ); $this->assertObjectHasAttribute( 'posts', $data );