Skip to content

Commit

Permalink
#17 Adding code review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hughdevore committed Aug 9, 2017
1 parent c717e11 commit 4a4f57f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
56 changes: 46 additions & 10 deletions src/Type/MediaItem/MediaItemCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,27 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
}

/**
* Stop now if a user isn't allowed to create a mediaItem
* Stop now if a user isn't allowed to upload a mediaItem
*/
if ( ! current_user_can( $post_type_object->cap->create_posts ) ) {
throw new \Exception( __( 'Sorry, you are not allowed to create mediaItems', 'wp-graphql' ) );
if ( ! current_user_can( 'upload_files' ) ) {
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' ) );
}
}

/**
Expand All @@ -85,7 +102,7 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
/**
* If the mediaItem file is from a local server, use wp_upload_bits before saving it to the uploads folder
*/
if ( 'false' === filter_var( $input['filePath'], FILTER_VALIDATE_URL ) ) {
if ( false === filter_var( $input['filePath'], FILTER_VALIDATE_URL ) ) {
$uploaded_file = wp_upload_bits( $file_name, null, file_get_contents( $input['filePath'] ) );
$uploaded_file_url = $uploaded_file['url'];
} else {
Expand All @@ -98,6 +115,12 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
*/
$timeout_seconds = 300;
$temp_file = download_url( $uploaded_file_url, $timeout_seconds );
/**
* Handle the error from download_url if it occurs
*/
if ( is_wp_error( $temp_file ) ) {
throw new \Exception( __( 'Sorry, the URL for this file is invalid, it must be a path to the mediaItem file', 'wp-graphql' ) );
}

/**
* Build the file data for side loading
Expand All @@ -123,21 +146,28 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
* Insert the mediaItem and retrieve it's data
*/
$file = wp_handle_sideload( $file_data, $overrides );

/**
* Insert the mediIitem object and get the ID
* Handle the error from wp_handle_sideload if it occurs
*/
$media_item_args = MediaItemMutation::prepare_media_item( $input, $post_type_object, $mutation_name, $file );
if ( ! empty( $file['error'] ) ) {
throw new \Exception( __( 'Sorry, there was an error uploading the mediaItem', 'wp-graphql' ) );
}

/**
* Get the post parent and if it's not set, set it to false
* Insert the mediaItem object and get the ID
*/
$attachment_parent_id = ( ! empty( $media_item_args['post_parent'] ) ? $media_item_args['post_parent'] : false );
$media_item_args = MediaItemMutation::prepare_media_item( $input, $post_type_object, $mutation_name, $file );

/**
* Insert the mediaItem
*/
$attachment_id = wp_insert_attachment( $media_item_args, $file['file'], $attachment_parent_id );
/**
* Handle the error from wp_insert_attachment if it occurs
*/
if ( 0 === $attachment_id ) {
throw new \Exception( __( 'Sorry, the mediaItem failed to create', 'wp-graphql' ) );
}

/**
* Check if the wp_generate_attachment_metadata method exists and include it if not
Expand All @@ -150,7 +180,13 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
* Generate and update the mediaItem's metadata
*/
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
$attachment_data_update = wp_update_attachment_metadata( $attachment_id, $attachment_data );
/**
* Handle the error from wp_update_attachment_metadata if it occurs
*/
if ( false === $attachment_data_update ) {
throw new \Exception( __( 'Sorry, the mediaItem metadata failed to update', 'wp-graphql' ) );
}

/**
* Update alt text postmeta for mediaItem
Expand Down
9 changes: 7 additions & 2 deletions src/Type/MediaItem/MediaItemDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ public static function mutate( \WP_Post_Type $post_type_object ) {

self::$mutation['mediaItem'] = Relay::mutationWithClientMutationId( [
'name' => esc_html( $mutation_name ),
// translators: The placeholder is the name of the object type
'description' => __( 'Delete mediaItem objects. By default mediaItem objects will be moved to the trash unless the forceDelete is used', 'wp-graphql' ),
'inputFields' => [
'id' => [
'type' => Types::non_null( Types::id() ),
// translators: The placeholder is the name of the post's post_type being deleted
'description' => __( 'The ID of the mediaItem to delete', 'wp-graphql' ),
],
'forceDelete' => [
Expand Down Expand Up @@ -113,6 +111,13 @@ public static function mutate( \WP_Post_Type $post_type_object ) {
* Delete the mediaItem
*/
$deleted = wp_delete_attachment( $id_parts['id'], $force_delete );
/**
* Handle the error from wp_delete_attachment if it occurs
*/
if ( false === $deleted ) {
throw new \Exception( __( 'Sorry, the mediaItem failed to delete', 'wp-graphql' ) );
}


/**
* If the post was moved to the trash, spoof the object's status before returning it
Expand Down
8 changes: 4 additions & 4 deletions src/Type/MediaItem/MediaItemMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_
* NOTE: These are organized in the same order as: http://v2.wp-api.org/reference/media/#schema-meta
*/
if ( ! empty( $input['date'] ) && false !== strtotime( $input['date'] ) ) {
$insert_post_args['post_date'] = date("Y-m-d H:i:s", strtotime( $input['date'] ) );
$insert_post_args['post_date'] = date( 'Y-m-d H:i:s', strtotime( $input['date'] ) );
}

if ( ! empty( $input['dateGmt'] ) && false !== strtotime( $input['dateGmt'] ) ) {
$insert_post_args['post_date_gmt'] = date("Y-m-d H:i:s", strtotime( $input['dateGmt'] ) );
$insert_post_args['post_date_gmt'] = date( 'Y-m-d H:i:s', strtotime( $input['dateGmt'] ) );
}

if ( ! empty( $input['slug'] ) ) {
Expand All @@ -141,7 +141,7 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_

if ( ! empty( $input['title'] ) ) {
$insert_post_args['post_title'] = $input['title'];
} else if ( ! empty( $file['file'] ) ) {
} elseif ( ! empty( $file['file'] ) ) {
$insert_post_args['post_title'] = basename( $file['file'] );
}

Expand All @@ -168,7 +168,7 @@ public static function prepare_media_item( $input, $post_type_object, $mutation_

if ( ! empty( $file['type'] ) ) {
$insert_post_args['post_mime_type'] = $file['type'];
} else if ( ! empty( $input['fileType'] ) ) {
} elseif ( ! empty( $input['fileType'] ) ) {
$insert_post_args['post_mime_type'] = $input['fileType'];
}

Expand Down

0 comments on commit 4a4f57f

Please sign in to comment.