• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 14716683875

28 Apr 2025 07:58PM UTC coverage: 84.287% (+1.6%) from 82.648%
14716683875

push

github

actions-user
release: merge develop into master for v2.3.0

15905 of 18870 relevant lines covered (84.29%)

257.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.32
/src/Mutation/MediaItemUpdate.php
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() {
593✔
19
                register_graphql_mutation(
593✔
20
                        'updateMediaItem',
593✔
21
                        [
593✔
22
                                'inputFields'         => self::get_input_fields(),
593✔
23
                                'outputFields'        => self::get_output_fields(),
593✔
24
                                'mutateAndGetPayload' => self::mutate_and_get_payload(),
593✔
25
                        ]
593✔
26
                );
593✔
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() {
593✔
35
                /** @var \WP_Post_Type $post_type_object */
36
                $post_type_object = get_post_type_object( 'attachment' );
593✔
37
                return array_merge(
593✔
38
                        MediaItemCreate::get_input_fields(),
593✔
39
                        [
593✔
40
                                'id' => [
593✔
41
                                        'type'        => [
593✔
42
                                                'non_null' => 'ID',
593✔
43
                                        ],
593✔
44
                                        'description' => static function () use ( $post_type_object ) {
593✔
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 );
15✔
47
                                        },
593✔
48
                                ],
593✔
49
                        ]
593✔
50
                );
593✔
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() {
593✔
59
                return MediaItemCreate::get_output_fields();
593✔
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() {
593✔
68
                return static function ( $input, AppContext $context, ResolveInfo $info ) {
593✔
69
                        $post_type_object = get_post_type_object( 'attachment' );
8✔
70

71
                        if ( empty( $post_type_object ) ) {
8✔
72
                                return [];
×
73
                        }
74

75
                        // Get the database ID for the comment.
76
                        $media_item_id = Utils::get_database_id_from_id( $input['id'] );
8✔
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;
8✔
82

83
                        $mutation_name = 'updateMediaItem';
8✔
84

85
                        /**
86
                         * If there's no existing mediaItem, throw an exception
87
                         */
88
                        if ( null === $existing_media_item ) {
8✔
89
                                throw new UserError( esc_html__( 'No mediaItem with that ID could be found to update', 'wp-graphql' ) );
1✔
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 ) {
7✔
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'] ) ) );
1✔
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 ) ) {
6✔
104
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to update mediaItems', 'wp-graphql' ) );
1✔
105
                        }
106

107
                        $author_id = absint( $existing_media_item->post_author );
5✔
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'] ) ) {
5✔
114
                                // Ensure authorId is a valid databaseId.
115
                                $input['authorId'] = Utils::get_database_id_from_id( $input['authorId'] );
2✔
116
                                // Use the new author for checks.
117
                                $author_id = $input['authorId'];
2✔
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 ) ) ) {
5✔
125
                                throw new UserError( esc_html__( 'Sorry, you are not allowed to update mediaItems as this user.', 'wp-graphql' ) );
2✔
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 );
3✔
132
                        $post_args['ID'] = $media_item_id;
3✔
133

134
                        $clean_args = wp_slash( (array) $post_args );
3✔
135

136
                        if ( ! is_array( $clean_args ) || empty( $clean_args ) ) {
3✔
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 );
3✔
147

148
                        if ( is_wp_error( $post_id ) ) {
3✔
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 );
3✔
164

165
                        /**
166
                         * Return the payload
167
                         */
168
                        return [
3✔
169
                                'postObjectId' => $post_id,
3✔
170
                        ];
3✔
171
                };
593✔
172
        }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc