Skip to content

Commit

Permalink
Updating code for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughdevore committed Aug 11, 2017
1 parent c03dbff commit f48b55d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,6 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
return new \Exception( __( 'Sorry, you are not allowed to upload mediaItems', 'wp-graphql' ) );
}

/**
* Get the post parent and if it's not set, set it to false
*/
$attachment_parent_id = ( ! empty( $media_item_args['post_parent'] ) ? $media_item_args['post_parent'] : false );

/**
* Stop now if a user isn't allowed to edit the parent post
*/
// Attaching media to a post requires ability to edit said post.
if ( ! empty( $attachment_parent_id ) ) {
$parent = get_post( $attachment_parent_id );
$post_parent_type = get_post_type_object( $parent->post_type );
if ( ! current_user_can( $post_parent_type->cap->edit_post, $attachment_parent_id ) ) {
return new \Exception( __( 'Sorry, you are not allowed to upload mediaItems to this post', 'wp-graphql' ) );
}
}

/**
* If the mediaItem being created is being assigned to another user that's not the current user, make sure
* the current user has permission to edit others mediaItems
Expand Down Expand Up @@ -158,6 +141,23 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
*/
$media_item_args = MediaItemMutation::prepare_media_item( $input, $post_type_object, $mutation_name, $file );

/**
* Get the post parent and if it's not set, set it to false
*/
$attachment_parent_id = ( ! empty( $media_item_args['post_parent'] ) ? $media_item_args['post_parent'] : false );

/**
* Stop now if a user isn't allowed to edit the parent post
*/
// Attaching media to a post requires ability to edit said post.
if ( ! empty( $attachment_parent_id ) ) {
$parent = get_post( $attachment_parent_id );
$post_parent_type = get_post_type_object( $parent->post_type );
if ( ! current_user_can( $post_parent_type->cap->edit_post, $attachment_parent_id ) ) {
return new \Exception( __( 'Sorry, you are not allowed to upload mediaItems to this post', 'wp-graphql' ) );
}
}

/**
* Insert the mediaItem
*/
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function input_fields( $post_type_object ) {
],
'parentId' => [
'type' => Types::id(),
'description' => __( 'The ID of the parent object', 'wp-graphql' ),
'description' => __( 'The WordPress post ID or the graphQL postId of the parent object', 'wp-graphql' ),
],
];

Expand Down Expand Up @@ -172,6 +172,9 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_
$insert_post_args['post_mime_type'] = $input['fileType'];
}

if ( ! empty( $input['parentId'] ) && null !== get_post( $input['parentId'] ) ) {
$insert_post_args['post_parent'] = $input['parentId'];
}
$parent_id_parts = ( ! empty( $input['parentId'] ) ? Relay::fromGlobalId( $input['parentId'] ) : null );

if ( is_array( $parent_id_parts ) && ! empty( $parent_id_parts['id'] ) ) {
Expand Down
File renamed without changes.

0 comments on commit f48b55d

Please sign in to comment.