Overview
  • Namespace
  • Class

Namespaces

  • WPGraphQL
    • Data
    • Type
      • Avatar
      • Comment
        • Connection
      • CommentAuthor
      • EditLock
      • Enum
      • MediaItem
        • Mutation
      • Plugin
        • Connection
      • PostObject
        • Connection
        • Mutation
      • PostType
      • Setting
      • Taxonomy
      • TermObject
        • Connection
        • Mutation
      • Theme
        • Connection
      • Union
      • User
        • Connection
        • Mutation
    • Utils

Classes

  • WPGraphQL\AppContext
  • WPGraphQL\Data\Config
  • WPGraphQL\Data\ConnectionResolver
  • WPGraphQL\Data\DataSource
  • WPGraphQL\Data\Loader
  • WPGraphQL\Router
  • WPGraphQL\Type\Avatar\AvatarType
  • WPGraphQL\Type\Comment\CommentQuery
  • WPGraphQL\Type\Comment\CommentType
  • WPGraphQL\Type\Comment\Connection\CommentConnectionArgs
  • WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition
  • WPGraphQL\Type\Comment\Connection\CommentConnectionResolver
  • WPGraphQL\Type\CommentAuthor\CommentAuthorQuery
  • WPGraphQL\Type\CommentAuthor\CommentAuthorType
  • WPGraphQL\Type\EditLock\EditLockType
  • WPGraphQL\Type\Enum\MediaItemStatusEnumType
  • WPGraphQL\Type\Enum\MimeTypeEnumType
  • WPGraphQL\Type\Enum\PostObjectFieldFormatEnumType
  • WPGraphQL\Type\Enum\PostStatusEnumType
  • WPGraphQL\Type\Enum\PostTypeEnumType
  • WPGraphQL\Type\Enum\RelationEnumType
  • WPGraphQL\Type\Enum\TaxonomyEnumType
  • WPGraphQL\Type\MediaItem\MediaItemType
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemCreate
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemDelete
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemMutation
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemUpdate
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionDefinition
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionResolver
  • WPGraphQL\Type\Plugin\PluginQuery
  • WPGraphQL\Type\Plugin\PluginType
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgs
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgsDateQuery
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionDefinition
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionResolver
  • WPGraphQL\Type\PostObject\Mutation\PostObjectCreate
  • WPGraphQL\Type\PostObject\Mutation\PostObjectDelete
  • WPGraphQL\Type\PostObject\Mutation\PostObjectMutation
  • WPGraphQL\Type\PostObject\Mutation\PostObjectUpdate
  • WPGraphQL\Type\PostObject\Mutation\TermObjectDelete
  • WPGraphQL\Type\PostObject\PostObjectQuery
  • WPGraphQL\Type\PostObject\PostObjectType
  • WPGraphQL\Type\PostType\PostTypeType
  • WPGraphQL\Type\RootMutationType
  • WPGraphQL\Type\RootQueryType
  • WPGraphQL\Type\Setting\SettingQuery
  • WPGraphQL\Type\Setting\SettingType
  • WPGraphQL\Type\Taxonomy\TaxonomyType
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionArgs
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionDefinition
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionResolver
  • WPGraphQL\Type\TermObject\Mutation\TermObjectCreate
  • WPGraphQL\Type\TermObject\Mutation\TermObjectMutation
  • WPGraphQL\Type\TermObject\Mutation\TermObjectUpdate
  • WPGraphQL\Type\TermObject\TermObjectQuery
  • WPGraphQL\Type\TermObject\TermObjectType
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionDefinition
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionResolver
  • WPGraphQL\Type\Theme\ThemeType
  • WPGraphQL\Type\Union\CommentAuthorUnionType
  • WPGraphQL\Type\Union\PostObjectUnionType
  • WPGraphQL\Type\Union\TermObjectUnionType
  • WPGraphQL\Type\User\Connection\UserConnectionArgs
  • WPGraphQL\Type\User\Connection\UserConnectionDefinition
  • WPGraphQL\Type\User\Connection\UserConnectionResolver
  • WPGraphQL\Type\User\Mutation\UserCreate
  • WPGraphQL\Type\User\Mutation\UserDelete
  • WPGraphQL\Type\User\Mutation\UserMutation
  • WPGraphQL\Type\User\Mutation\UserUpdate
  • WPGraphQL\Type\User\UserQuery
  • WPGraphQL\Type\User\UserType
  • WPGraphQL\Type\WPEnumType
  • WPGraphQL\Type\WPInputObjectType
  • WPGraphQL\Type\WPObjectType
  • WPGraphQL\Types
  • WPGraphQL\Utils\InstrumentSchema
  • WPGraphQL\WPSchema

Interfaces

  • WPGraphQL\Data\ConnectionResolverInterface
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 
<?php

namespace WPGraphQL\Type\MediaItem\Mutation;

use GraphQL\Error\UserError;
use GraphQLRelay\Relay;
use WPGraphQL\Types;

/**
 * Class MediaItemCreate
 *
 * @package WPGraphQL\Type\MediaItem\Mutation
 */
class MediaItemCreate {

    /**
     * Holds the mutation field definition
     *
     * @var array mutation
     */
    private static $mutation = [];

    /**
     * Defines the create mutation for MediaItems
     *
     * @var \WP_Post_Type $post_type_object
     *
     * @return array|mixed
     */
    public static function mutate( \WP_Post_Type $post_type_object ) {

        /**
         * Set the name of the mutation being performed
         */
        $mutation_name = 'CreateMediaItem';

        self::$mutation['mediaItem'] = Relay::mutationWithClientMutationId( [
            'name' => esc_html( $mutation_name ),
            'description' => __( 'Create mediaItems', 'wp-graphql' ),
            'inputFields' => self::input_fields( $post_type_object ),
            'outputFields' => [
                'mediaItem' => [
                    'type' => Types::post_object( $post_type_object->name ),
                    'resolve' => function( $payload ) {
                        return get_post( $payload['id'] );
                    },
                ],
            ],
            'mutateAndGetPayload' => function( $input ) use ( $post_type_object, $mutation_name ) {

                /**
                 * Stop now if a user isn't allowed to upload a mediaItem
                 */
                if ( ! current_user_can( 'upload_files' ) ) {
                    throw new UserError( __( 'Sorry, you are not allowed to upload mediaItems', 'wp-graphql' ) );
                }

                /**
                 * Set the file name, whether it's a local file or from a URL.
                 * Then set the url for the uploaded file
                 */
                $file_name = basename( $input['filePath'] );
                $uploaded_file_url = $input['filePath'];

                /**
                 * Require the file.php file from wp-admin. This file includes the
                 * download_url and wp_handle_sideload methods
                 */
                require_once( ABSPATH . 'wp-admin/includes/file.php' );

                /**
                 * If the mediaItem file is from a local server, use wp_upload_bits before saving it to the uploads folder
                 */
                if ( 'file' === parse_url( $input['filePath'], PHP_URL_SCHEME ) ) {
                    $uploaded_file = wp_upload_bits( $file_name, null, file_get_contents( $input['filePath'] ) );
                    $uploaded_file_url = ( empty ( $uploaded_file['error'] ) ? $uploaded_file['url'] : null );
                }

                /**
                 * URL data for the mediaItem, timeout value is the default, see:
                 * https://developer.wordpress.org/reference/functions/download_url/
                 */
                $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 UserError( __( 'Sorry, the URL for this file is invalid, it must be a valid URL', 'wp-graphql' ) );
                }

                /**
                 * Build the file data for side loading
                 */
                $file_data = [
                    'name'     => $file_name,
                    'type'     => ! empty ( $input['fileType'] ) ? $input['fileType'] : wp_check_filetype( $temp_file ),
                    'tmp_name' => $temp_file,
                    'error'    => 0,
                    'size'     => filesize( $temp_file ),
                ];

                /**
                 * Tells WordPress to not look for the POST form fields that would normally be present as
                 * we downloaded the file from a remote server, so there will be no form fields
                 * The default is true
                 */
                $overrides = [
                    'test_form' => false,
                ];

                /**
                 * Insert the mediaItem and retrieve it's data
                 */
                $file = wp_handle_sideload( $file_data, $overrides );

                /**
                 * Handle the error from wp_handle_sideload if it occurs
                 */
                if ( ! empty( $file['error'] ) ) {
                    throw new UserError( __( 'Sorry, the URL for this file is invalid, it must be a path to the mediaItem file', 'wp-graphql' ) );
                }

                /**
                 * Insert the mediaItem object and get the ID
                 */
                $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
                 */
                $parent = get_post( $attachment_parent_id );

                if ( null !== 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 ) ) {
                        throw new UserError( __( '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
                 */
                if ( ! empty( $input['authorId'] ) && get_current_user_id() !== $input['authorId'] && ! current_user_can( $post_type_object->cap->edit_others_posts ) ) {
                    throw new UserError( __( 'Sorry, you are not allowed to create mediaItems as this user', 'wp-graphql' ) );
                }

                /**
                 * Insert the mediaItem
                 *
                 * Required Argument defaults are set in the main MediaItemMutation.php if they aren't set
                 * by the user during input, they are:
                 * post_title (pulled from file if not entered)
                 * post_content (empty string if not entered)
                 * post_status (inherit if not entered)
                 * post_mime_type (pulled from the file if not entered in the mutation)
                 */
                $attachment_id = wp_insert_attachment( $media_item_args, $file['file'], $attachment_parent_id );

                /**
                 * Check if the wp_generate_attachment_metadata method exists and include it if not
                 */
                require_once( ABSPATH . 'wp-admin/includes/image.php' );

                /**
                 * Generate and update the mediaItem's metadata.
                 * If we make it this far the file and attachment
                 * have been validated and we will not receive any errors
                 */
                $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file['file'] );
                $attachment_data_update = wp_update_attachment_metadata( $attachment_id, $attachment_data );

                /**
                 * Update alt text postmeta for mediaItem
                 */
                MediaItemMutation::update_additional_media_item_data( $attachment_id, $input, $post_type_object, $mutation_name );

                return [
                    'id' => $attachment_id,
                ];

            },

        ] );

        return ! empty( self::$mutation['mediaItem'] ) ? self::$mutation['mediaItem'] : null;
    }

    /**
     * Add the filePath as a nonNull field for create mutations as its required
     * to create a media item
     *
     * @param \WP_Post_Type $post_type_object
     *
     * @return array
     */
    private static function input_fields( $post_type_object ) {

        /**
         * Creating mutations requires a filePath to be passed
         */
        return array_merge(
            [
                'filePath'      => [
                    'type'        => Types::non_null( Types::string() ),
                    'description' => __( 'The URL or file path to the mediaItem', 'wp-graphql' ),
                ],
            ],
            MediaItemMutation::input_fields( $post_type_object )
        );

    }
}
API documentation generated by ApiGen