Skip to content

Commit

Permalink
Merge pull request #86 from voceconnect/i78_attachment_queries
Browse files Browse the repository at this point in the history
adding handling for attachments and the inherit post_status
  • Loading branch information
Michael Pretty committed Mar 5, 2014
2 parents 630a519 + a7ae034 commit be8bc1b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
21 changes: 11 additions & 10 deletions api/v1/controllers/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'] ) ) {
Expand Down
21 changes: 17 additions & 4 deletions api/v1/models/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand All @@ -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
Expand All @@ -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;
}

}
Empty file modified bin/install-wp-tests.sh
100644 → 100755
Empty file.
44 changes: 33 additions & 11 deletions tests/v1/controllers/test-Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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();

Expand All @@ -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(
Expand All @@ -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',
Expand All @@ -135,15 +159,15 @@ public function testGetPostsStatus() {
) );

$data = json_decode( $body );

$this->assertEquals( '200', $status );
$this->assertInternalType( 'object', $data );
$this->assertObjectHasAttribute( 'posts', $data );
$this->assertInternalType( 'array', $data->posts );
$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',
Expand All @@ -152,16 +176,14 @@ 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 );
$this->assertObjectHasAttribute( 'posts', $data );
$this->assertInternalType( 'array', $data->posts );
$this->assertCount( 1, $data->posts );
$this->assertObjectNotHasAttribute( 'found', $data );


}

public function testGetPostsPerPage() {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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' );
Expand All @@ -411,7 +433,7 @@ public function testGetPostsCustomTaxonomy() {
) );

$data = json_decode( $body );

$this->assertEquals( '200', $status );
$this->assertInternalType( 'object', $data );
$this->assertObjectHasAttribute( 'posts', $data );
Expand Down

0 comments on commit be8bc1b

Please sign in to comment.