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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 
<?php

namespace WPGraphQL\Type\MediaItem;

use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\Type\WPObjectType;
use WPGraphQL\Types;

/**
 * Class MediaItemType
 *
 * This class isn't a full definition of a new Type, instead it's used to customize
 * the shape of the mediaItemType (via filter), which is instantiated as a PostObjectType.
 *
 * @see     : wp-graphql.php - add_filter( 'graphql_mediaItem_fields', [ '\WPGraphQL\Type\MediaItem\MediaItemType',
 *          'fields' ], 10, 1 );
 *
 * @package WPGraphQL\Type\MediaItem
 */
class MediaItemType {

    /**
     * Holds the object definition for media details
     *
     * @var object $media_details
     */
    private static $media_details;

    /**
     * Holds the object definition for media item meta
     *
     * @var object $media_item_meta
     */
    private static $media_item_meta;

    /**
     * Holds the object definition for media sizes
     *
     * @var object $media_sizes
     */
    private static $media_sizes;

    /**
     * This customizes the fields for the mediaItem type ( attachment post_type) as the shape of the mediaItem Schema
     * is different than a standard post
     *
     * @see: wp-graphql.php - add_filter( 'graphql_mediaItem_fields' );add_filter( 'graphql_mediaItem_fields', [
     *       '\WPGraphQL\Type\MediaItem\MediaItemType', 'fields' ], 10, 1 );
     *
     * @param array $fields
     *
     * @return array
     */
    public static function fields( $fields ) {

        /**
         * Deprecate fields for the mediaItem type.
         * These fields can still be queried, but are just not preferred for the mediaItem type
         *
         * @since 0.0.6
         */
        $fields['excerpt']['isDeprecated']      = true;
        $fields['excerpt']['deprecationReason'] = __( 'Use the caption field instead of excerpt', 'wp-graphql' );
        $fields['content']['isDeprecated']      = true;
        $fields['content']['deprecationReason'] = __( 'Use the description field instead of content', 'wp-graphql' );

        /**
         * Add new fields to the mediaItem type
         *
         * @since 0.0.6
         */
        $new_fields = [
            'caption'      => [
                'type'        => Types::string(),
                'description' => __( 'The caption for the resource', 'wp-graphql' ),
                'resolve'     => function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    $caption = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );

                    return ! empty( $caption ) ? $caption : null;
                },
            ],
            'altText'      => [
                'type'        => Types::string(),
                'description' => __( 'Alternative text to display when resource is not displayed', 'wp-graphql' ),
                'resolve'     => function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    return get_post_meta( $post->ID, '_wp_attachment_image_alt', true );
                },
            ],
            'description'  => [
                'type'        => Types::string(),
                'description' => __( 'Description of the image (stored as post_content)', 'wp-graphql' ),
                'resolve'     => function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    return apply_filters( 'the_content', $post->post_content );
                },
            ],
            'mediaType'    => [
                'type'        => Types::string(),
                'description' => __( 'Type of resource', 'wp-graphql' ),
                'resolve'     => function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    return wp_attachment_is_image( $post->ID ) ? 'image' : 'file';
                },
            ],
            'sourceUrl'    => [
                'type'        => Types::string(),
                'description' => __( 'Url of the mediaItem', 'wp-graphql' ),
                'resolve'     => function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    return wp_get_attachment_url( $post->ID );
                },
            ],
            'mimeType' => [
                'type'        => Types::string(),
                'description' => __( 'The mime type of the mediaItem', 'wp-graphql' ),
                'resolve'     =>function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    return ! empty( $post->post_mime_type ) ? $post->post_mime_type : null;
                },
            ],
            'mediaDetails' => [
                'type'        => self::media_details(),
                'description' => __( 'Details about the mediaItem', 'wp-graphql' ),
                'resolve'     => function( \WP_Post $post, $args, $context, ResolveInfo $info ) {
                    return wp_get_attachment_metadata( $post->ID );
                },
            ],

        ];

        return array_merge( $fields, $new_fields );

    }

    /**
     * This defines the media details object type that can be queried on mediaItems
     *
     * @return null|WPObjectType
     * @since 0.0.6
     */
    private static function media_details() {

        if ( null === self::$media_details ) {
            self::$media_details = new WPObjectType( [
                'name'   => 'MediaDetails',
                'fields' => function() {
                    $fields = [
                        'width'  => [
                            'type'        => Types::int(),
                            'description' => __( 'The width of the mediaItem', 'wp-graphql' ),
                        ],
                        'height' => [
                            'type'        => Types::int(),
                            'description' => __( 'The height of the mediaItem', 'wp-graphql' ),
                        ],
                        'file'   => [
                            'type'        => Types::string(),
                            'description' => __( 'The height of the mediaItem', 'wp-graphql' ),
                        ],
                        'sizes'  => [
                            'type'        => Types::list_of( self::media_sizes() ),
                            'description' => __( 'The available sizes of the mediaItem', 'wp-graphql' ),
                            'resolve'     => function( $media_details, $args, $context, ResolveInfo $info ) {
                                if ( ! empty( $media_details['sizes'] ) ) {
                                    foreach ( $media_details['sizes'] as $size_name => $size ) {
                                        $size['name']   = $size_name;
                                        $sizes[]        = $size;
                                    }
                                }
                                return ! empty( $sizes ) ? $sizes : null;
                            },
                        ],
                        'meta'   => [
                            'type'    => self::media_item_meta(),
                            'resolve' => function( $media_details, $args, $context, ResolveInfo $info ) {
                                return ! empty( $media_details['image_meta'] ) ? $media_details['image_meta'] : null;
                            },
                        ],
                    ];

                    return WPObjectType::prepare_fields( $fields, 'MediaDetails' );
                },
            ] );
        } // End if().

        return ! empty( self::$media_details ) ? self::$media_details : null;

    }

    /**
     * This defines the media item meta object type that can be queried on mediaItems
     *
     * @return null|WPObjectType
     * @since 0.0.6
     */
    private static function media_item_meta() {
        if ( null === self::$media_item_meta ) {
            self::$media_item_meta = new WPObjectType( [
                'name'   => 'MediaItemMeta',
                'fields' => [
                    'aperture'         => [
                        'type' => Types::float(),
                    ],
                    'credit'           => [
                        'type' => Types::string(),
                    ],
                    'camera'           => [
                        'type' => Types::string(),
                    ],
                    'caption'          => [
                        'type' => Types::string(),
                    ],
                    'createdTimestamp' => [
                        'type'    => Types::int(),
                        'resolve' => function( $meta, $args, $context, ResolveInfo $info ) {
                            return ! empty( $meta['created_timestamp'] ) ? $meta['created_timestamp'] : null;
                        },
                    ],
                    'copyright'        => [
                        'type' => Types::string(),
                    ],
                    'focalLength'      => [
                        'type'    => Types::int(),
                        'resolve' => function( $meta, $args, $context, ResolveInfo $info ) {
                            return ! empty( $meta['focal_length'] ) ? $meta['focal_length'] : null;
                        },
                    ],
                    'iso'              => [
                        'type' => Types::int(),
                    ],
                    'shutterSpeed'     => [
                        'type'    => Types::float(),
                        'resolve' => function( $meta, $args, $context, ResolveInfo $info ) {
                            return ! empty( $meta['shutter_speed'] ) ? $meta['shutter_speed'] : null;
                        },
                    ],
                    'title'            => [
                        'type' => Types::string(),
                    ],
                    'orientation'      => [
                        'type' => Types::string(),
                    ],
                    'keywords'         => [
                        'type' => Types::list_of( Types::string() ),
                    ],
                ],
            ] );
        } // End if().

        return ! empty( self::$media_item_meta ) ? self::$media_item_meta : null;
    }

    /**
     * This defines the sizes object type that can be queried on mediaItems within the mediaDetails
     *
     * @return null|WPObjectType
     * @since 0.0.6
     */
    private static function media_sizes() {

        if ( null === self::$media_sizes ) {
            self::$media_sizes = new WPObjectType( [
                'name'   => 'MediaSizes',
                'fields' => [
                    'name'      => [
                        'type'        => Types::string(),
                        'description' => __( 'The referenced size name', 'wp-graphql' ),
                    ],
                    'file'      => [
                        'type'        => Types::string(),
                        'description' => __( 'The file of the for the referenced size', 'wp-graphql' ),
                    ],
                    'width'     => [
                        'type'        => Types::string(),
                        'description' => __( 'The width of the for the referenced size', 'wp-graphql' ),
                    ],
                    'height'    => [
                        'type'        => Types::string(),
                        'description' => __( 'The height of the for the referenced size', 'wp-graphql' ),
                    ],
                    'mimeType'  => [
                        'type'        => Types::string(),
                        'description' => __( 'The mime type of the resource', 'wp-graphql' ),
                        'resolve'     => function( $image, $args, $context, ResolveInfo $info ) {
                            return ! empty( $image['mime-type'] ) ? $image['mime-type'] : null;
                        },
                    ],
                    'sourceUrl' => [
                        'type'        => Types::string(),
                        'description' => __( 'The url of the for the referenced size', 'wp-graphql' ),
                        'resolve'     => function( $image, $args, $context, ResolveInfo $info ) {
                            return ! empty( $image['file'] ) ? $image['file'] : null;
                        },
                    ],
                ],
            ] );
        } // End if().

        return ! empty( self::$media_sizes ) ? self::$media_sizes : null;

    }

}
API documentation generated by ApiGen