| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPGraphQL\Mutation; |
| 4 |
|
| 5 |
use GraphQL\Error\UserError; |
| 6 |
use GraphQL\Type\Definition\ResolveInfo; |
| 7 |
use WPGraphQL\AppContext; |
| 8 |
use WPGraphQL\Data\MediaItemMutation; |
| 9 |
use WPGraphQL\Utils\Utils; |
| 10 |
|
| 11 |
class MediaItemUpdate { |
| 12 |
/** |
| 13 |
* Registers the MediaItemUpdate mutation. |
| 14 |
* |
| 15 |
* @return void |
| 16 |
* @throws \Exception |
| 17 |
*/ |
| 18 |
public static function register_mutation() { |
| 19 |
register_graphql_mutation( |
| 20 |
'updateMediaItem', |
| 21 |
[ |
| 22 |
'inputFields' => self::get_input_fields(), |
| 23 |
'outputFields' => self::get_output_fields(), |
| 24 |
'mutateAndGetPayload' => self::mutate_and_get_payload(), |
| 25 |
] |
| 26 |
); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Defines the mutation input field configuration. |
| 31 |
* |
| 32 |
* @return array<string,array<string,mixed>> |
| 33 |
*/ |
| 34 |
public static function get_input_fields() { |
| 35 |
/** @var \WP_Post_Type $post_type_object */ |
| 36 |
$post_type_object = get_post_type_object( 'attachment' ); |
| 37 |
return array_merge( |
| 38 |
MediaItemCreate::get_input_fields(), |
| 39 |
[ |
| 40 |
'id' => [ |
| 41 |
'type' => [ |
| 42 |
'non_null' => 'ID', |
| 43 |
], |
| 44 |
'description' => static function () use ( $post_type_object ) { |
| 45 |
// translators: the placeholder is the name of the type of post object being updated |
| 46 |
return sprintf( __( 'The ID of the %1$s object', 'wp-graphql' ), $post_type_object->graphql_single_name ); |
| 47 |
}, |
| 48 |
], |
| 49 |
] |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Defines the mutation output field configuration. |
| 55 |
* |
| 56 |
* @return array<string,array<string,mixed>> |
| 57 |
*/ |
| 58 |
public static function get_output_fields() { |
| 59 |
return MediaItemCreate::get_output_fields(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Defines the mutation data modification closure. |
| 64 |
* |
| 65 |
* @return callable(array<string,mixed>$input,\WPGraphQL\AppContext $context,\GraphQL\Type\Definition\ResolveInfo $info):array<string,mixed> |
| 66 |
*/ |
| 67 |
public static function mutate_and_get_payload() { |
| 68 |
return static function ( $input, AppContext $context, ResolveInfo $info ) { |
| 69 |
$post_type_object = get_post_type_object( 'attachment' ); |
| 70 |
|
| 71 |
if ( empty( $post_type_object ) ) { |
| 72 |
return []; |
| 73 |
} |
| 74 |
|
| 75 |
// Get the database ID for the comment. |
| 76 |
$media_item_id = Utils::get_database_id_from_id( $input['id'] ); |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the mediaItem object before deleting it |
| 80 |
*/ |
| 81 |
$existing_media_item = ! empty( $media_item_id ) ? get_post( $media_item_id ) : null; |
| 82 |
|
| 83 |
$mutation_name = 'updateMediaItem'; |
| 84 |
|
| 85 |
/** |
| 86 |
* If there's no existing mediaItem, throw an exception |
| 87 |
*/ |
| 88 |
if ( null === $existing_media_item ) { |
| 89 |
throw new UserError( esc_html__( 'No mediaItem with that ID could be found to update', 'wp-graphql' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Stop now if the post isn't a mediaItem |
| 94 |
*/ |
| 95 |
if ( $post_type_object->name !== $existing_media_item->post_type ) { |
| 96 |
// translators: The placeholder is the ID of the mediaItem being edited |
| 97 |
throw new UserError( esc_html( sprintf( __( 'The id %1$d is not of the type mediaItem', 'wp-graphql' ), $input['id'] ) ) ); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Stop now if a user isn't allowed to edit mediaItems |
| 102 |
*/ |
| 103 |
if ( ! isset( $post_type_object->cap->edit_posts ) || ! current_user_can( $post_type_object->cap->edit_posts ) ) { |
| 104 |
throw new UserError( esc_html__( 'Sorry, you are not allowed to update mediaItems', 'wp-graphql' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
$author_id = absint( $existing_media_item->post_author ); |
| 108 |
|
| 109 |
/** |
| 110 |
* If the mutation is setting the author to be someone other than the user making the request |
| 111 |
* make sure they have permission to edit others posts |
| 112 |
*/ |
| 113 |
if ( ! empty( $input['authorId'] ) ) { |
| 114 |
// Ensure authorId is a valid databaseId. |
| 115 |
$input['authorId'] = Utils::get_database_id_from_id( $input['authorId'] ); |
| 116 |
// Use the new author for checks. |
| 117 |
$author_id = $input['authorId']; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Check to see if the existing_media_item author matches the current user, |
| 122 |
* if not they need to be able to edit others posts to proceed |
| 123 |
*/ |
| 124 |
if ( get_current_user_id() !== $author_id && ( ! isset( $post_type_object->cap->edit_others_posts ) || ! current_user_can( $post_type_object->cap->edit_others_posts ) ) ) { |
| 125 |
throw new UserError( esc_html__( 'Sorry, you are not allowed to update mediaItems as this user.', 'wp-graphql' ) ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Insert the post object and get the ID |
| 130 |
*/ |
| 131 |
$post_args = MediaItemMutation::prepare_media_item( $input, $post_type_object, $mutation_name, false ); |
| 132 |
$post_args['ID'] = $media_item_id; |
| 133 |
|
| 134 |
$clean_args = wp_slash( (array) $post_args ); |
| 135 |
|
| 136 |
if ( ! is_array( $clean_args ) || empty( $clean_args ) ) { |
| 137 |
throw new UserError( esc_html__( 'The media item failed to update', 'wp-graphql' ) ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Insert the post and retrieve the ID |
| 142 |
* |
| 143 |
* This will not fail as long as we have an ID in $post_args |
| 144 |
* Thanks to the validation above we will always have the ID |
| 145 |
*/ |
| 146 |
$post_id = wp_update_post( $clean_args, true ); |
| 147 |
|
| 148 |
if ( is_wp_error( $post_id ) ) { |
| 149 |
$error_message = $post_id->get_error_message(); |
| 150 |
if ( ! empty( $error_message ) ) { |
| 151 |
throw new UserError( esc_html( $error_message ) ); |
| 152 |
} |
| 153 |
|
| 154 |
throw new UserError( esc_html__( 'The media item failed to update but no error was provided', 'wp-graphql' ) ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* This updates additional data not part of the posts table (postmeta, terms, other relations, etc) |
| 159 |
* |
| 160 |
* The input for the postObjectMutation will be passed, along with the $new_post_id for the |
| 161 |
* postObject that was updated so that relations can be set, meta can be updated, etc. |
| 162 |
*/ |
| 163 |
MediaItemMutation::update_additional_media_item_data( $post_id, $input, $post_type_object, $mutation_name, $context, $info ); |
| 164 |
|
| 165 |
/** |
| 166 |
* Return the payload |
| 167 |
*/ |
| 168 |
return [ |
| 169 |
'postObjectId' => $post_id, |
| 170 |
]; |
| 171 |
}; |
| 172 |
} |
| 173 |
} |
| 174 |
|