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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 
<?php
namespace WPGraphQL\Type\MediaItem\Mutation;

use GraphQLRelay\Relay;
use WPGraphQL\Types;

/**
 * Class MediaItemMutation
 *
 * @package WPGraphQL\Type\MediaItem
 */
class MediaItemMutation {

    /**
     * Holds the input fields configuration
     *
     * @var array
     */
    private static $input_fields = [];

    /**
     * @param $post_type_object
     *
     * @return mixed|array|null $input_fields
     */
    public static function input_fields( $post_type_object ) {

        if ( ! empty( $post_type_object->graphql_single_name ) && empty( self::$input_fields[ $post_type_object->graphql_single_name ] ) ) {

            $input_fields = [
                'altText'       => [
                    'type'        => Types::string(),
                    'description' => __( 'Alternative text to display when mediaItem is not displayed', 'wp-graphql' ),
                ],
                'authorId'      => [
                    'type'        => Types::id(),
                    'description' => __( 'The userId to assign as the author of the mediaItem', 'wp-graphql' ),
                ],
                'caption'       => [
                    'type'        => Types::string(),
                    'description' => __( 'The caption for the mediaItem', 'wp-graphql' ),
                ],
                'commentStatus' => [
                    'type'        => Types::string(),
                    'description' => __( 'The comment status for the mediaItem', 'wp-graphql' ),
                ],
                'date'          => [
                    'type'        => Types::string(),
                    'description' => __( 'The date of the mediaItem', 'wp-graphql' ),
                ],
                'dateGmt'       => [
                    'type'        => Types::string(),
                    'description' => __( 'The date (in GMT zone) of the mediaItem', 'wp-graphql' ),
                ],
                'description'   => [
                    'type'        => Types::string(),
                    'description' => __( 'Description of the mediaItem', 'wp-graphql' ),
                ],
                'filePath'      => [
                    'type'        => Types::string(),
                    'description' => __( 'The file name of the mediaItem', 'wp-graphql' ),
                ],
                'fileType'      => [
                    'type'        => Types::mime_type_enum(),
                    'description' => __( 'The file type of the mediaItem', 'wp-graphql' ),
                ],
                'slug'          => [
                    'type'        => Types::string(),
                    'description' => __( 'The slug of the mediaItem', 'wp-graphql' ),
                ],
                'status'        => [
                    'type'        => Types::media_item_status_enum(),
                    'description' => __( 'The status of the mediaItem', 'wp-graphql' ),
                ],
                'title'         => [
                    'type'        => Types::string(),
                    'description' => __( 'The title of the mediaItem', 'wp-graphql' ),
                ],
                'pingStatus'    => [
                    'type'        => Types::string(),
                    'description' => __( 'The ping status for the mediaItem', 'wp-graphql' ),
                ],
                'parentId'      => [
                    'type'        => Types::id(),
                    'description' => __( 'The WordPress post ID or the graphQL postId of the parent object', 'wp-graphql' ),
                ],
            ];

            /**
             * Filters the mutation input fields for the mediaItem
             *
             * @param array         $input_fields     The array of input fields
             * @param \WP_Post_Type $post_type_object The post_type object for the mediaItem
             */
            self::$input_fields[ $post_type_object->graphql_single_name ] = apply_filters( 'graphql_media_item_mutation_input_fields', $input_fields, $post_type_object );

        } // End if().

        return ! empty( self::$input_fields[ $post_type_object->graphql_single_name ] ) ? self::$input_fields[ $post_type_object->graphql_single_name ] : null;

    }

    /**
     * This prepares the media item for insertion
     *
     * @param array         $input            The input for the mutation from the GraphQL request
     * @param \WP_Post_Type $post_type_object The post_type_object for the mediaItem (attachment)
     * @param string        $mutation_name    The name of the mutation being performed (create, update, etc.)
     * @param mixed         $file             The mediaItem (attachment) file
     *
     * @return array $media_item_args
     */
    public static function prepare_media_item( $input, $post_type_object, $mutation_name, $file ) {

        /**
         * Set the post_type (attachment) for the insert
         */
        $insert_post_args['post_type'] = $post_type_object->name;

        /**
         * Prepare the data for inserting the mediaItem
         * 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'] ) );
        }

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

        if ( ! empty( $input['slug'] ) ) {
            $insert_post_args['post_name'] = $input['slug'];
        }

        if ( ! empty( $input['status'] ) ) {
            $insert_post_args['post_status'] = $input['status'];
        } else {
            $insert_post_args['post_status'] = 'inherit';
        }

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

        $author_id_parts = ! empty( $input['authorId'] ) ? Relay::fromGlobalId( $input['authorId'] ) : null;
        if ( is_array( $author_id_parts ) && ! empty( $author_id_parts['id'] ) ) {
            $insert_post_args['post_author'] = absint( $author_id_parts['id'] );
        }

        if ( ! empty( $input['commentStatus'] ) ) {
            $insert_post_args['comment_status'] = $input['commentStatus'];
        }

        if ( ! empty( $input['pingStatus'] ) ) {
            $insert_post_args['ping_status'] = $input['pingStatus'];
        }

        if ( ! empty( $input['caption'] ) ) {
            $insert_post_args['post_excerpt'] = $input['caption'];
        }

        if ( ! empty( $input['description'] ) ) {
            $insert_post_args['post_content'] = $input['description'];
        } else {
            $insert_post_args['post_content'] = '';
        }

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

        if ( ! empty( $input['parentId'] ) ) {

            $parent_id_parts = ( ! empty( $input['parentId'] ) ? Relay::fromGlobalId( $input['parentId'] ) : null );

            if ( is_array( $parent_id_parts ) && ! empty( $parent_id_parts['id'] ) ) {
                $insert_post_args['post_parent'] = absint( $parent_id_parts['id'] );
            } else {
                $insert_post_args['post_parent'] = $input['parentId'];

            }

        }

        /**
         * Filter the $insert_post_args
         *
         * @param array         $insert_post_args The array of $input_post_args that will be passed to wp_insert_attachment
         * @param array         $input            The data that was entered as input for the mutation
         * @param \WP_Post_Type $post_type_object The post_type_object that the mutation is affecting
         * @param string        $mutation_type    The type of mutation being performed (create, update, delete)
         */
        $insert_post_args = apply_filters( 'graphql_media_item_insert_post_args', $insert_post_args, $input, $post_type_object, $mutation_name );

        return $insert_post_args;
    }

    /**
     * This updates additional data related to a mediaItem, such as postmeta.
     *
     * @param int           $media_item_id
     * @param array         $input
     * @param \WP_Post_Type $post_type_object
     * @param string        $mutation_name
     */
    public static function update_additional_media_item_data( $media_item_id, $input, $post_type_object, $mutation_name ) {

        /**
         * Update alt text postmeta for the mediaItem
         */
        if ( ! empty( $input['altText'] ) ) {
            update_post_meta( $media_item_id, '_wp_attachment_image_alt', $input['altText'] );
        }

        /**
         * Run an action after the additional data has been updated. This is a great spot to hook into to
         * update additional data related to mediaItems, such as updating additional postmeta,
         * or sending emails to Jason. . .whatever you need to do with the mediaItem.
         *
         * @param int           $media_item_id    The ID of the mediaItem being mutated
         * @param array         $input            The input for the mutation
         * @param \WP_Post_Type $post_type_object The Post Type Object for the type of post being mutated
         * @param string        $mutation_name    The name of the mutation (ex: create, update, delete)
         */
        do_action( 'graphql_media_item_mutation_update_additional_data', $media_item_id, $input, $post_type_object, $mutation_name );

    }

}
API documentation generated by ApiGen