• 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

97.83
/src/Type/ObjectType/MediaDetails.php
1
<?php
2

3
namespace WPGraphQL\Type\ObjectType;
4

5
class MediaDetails {
6

7
        /**
8
         * Register the MediaDetails type to the Schema
9
         *
10
         * @return void
11
         */
12
        public static function register_type() {
593✔
13
                register_graphql_object_type(
593✔
14
                        'MediaDetails',
593✔
15
                        [
593✔
16
                                'description' => static function () {
593✔
17
                                        return __( 'File details for a Media Item', 'wp-graphql' );
15✔
18
                                },
593✔
19
                                'fields'      => static function () {
593✔
20
                                        return [
129✔
21
                                                'width'    => [
129✔
22
                                                        'type'        => 'Int',
129✔
23
                                                        'description' => static function () {
129✔
24
                                                                return __( 'The width of the mediaItem', 'wp-graphql' );
15✔
25
                                                        },
129✔
26
                                                ],
129✔
27
                                                'height'   => [
129✔
28
                                                        'type'        => 'Int',
129✔
29
                                                        'description' => static function () {
129✔
30
                                                                return __( 'The height of the mediaItem', 'wp-graphql' );
15✔
31
                                                        },
129✔
32
                                                ],
129✔
33
                                                'file'     => [
129✔
34
                                                        'type'        => 'String',
129✔
35
                                                        'description' => static function () {
129✔
36
                                                                return __( 'The filename of the mediaItem', 'wp-graphql' );
15✔
37
                                                        },
129✔
38
                                                        'resolve'     => static function ( $media_details ) {
129✔
39
                                                                return ! empty( $media_details['file'] ) ? basename( $media_details['file'] ) : null;
9✔
40
                                                        },
129✔
41
                                                ],
129✔
42
                                                'filePath' => [
129✔
43
                                                        'type'        => 'String',
129✔
44
                                                        'description' => static function () {
129✔
45
                                                                return __( 'The path to the mediaItem relative to the uploads directory', 'wp-graphql' );
15✔
46
                                                        },
129✔
47
                                                        'resolve'     => static function ( $media_details ) {
129✔
48
                                                                // Get the upload directory info
49
                                                                $upload_dir           = wp_upload_dir();
1✔
50
                                                                $relative_upload_path = wp_make_link_relative( $upload_dir['baseurl'] );
1✔
51

52
                                                                if ( ! empty( $media_details['file'] ) ) {
1✔
53
                                                                        return path_join( $relative_upload_path, $media_details['file'] );
1✔
54
                                                                }
55

56
                                                                return null;
×
57
                                                        },
129✔
58
                                                ],
129✔
59
                                                'sizes'    => [
129✔
60
                                                        'type'        => [
129✔
61
                                                                'list_of' => 'MediaSize',
129✔
62
                                                        ],
129✔
63
                                                        'args'        => [
129✔
64
                                                                'exclude' => [ // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
129✔
65
                                                                        'type'        => [ 'list_of' => 'MediaItemSizeEnum' ],
129✔
66
                                                                        'description' => static function () {
129✔
67
                                                                                return __( 'The sizes to exclude. Will take precedence over `include`.', 'wp-graphql' );
15✔
68
                                                                        },
129✔
69
                                                                ],
129✔
70
                                                                'include' => [
129✔
71
                                                                        'type'        => [ 'list_of' => 'MediaItemSizeEnum' ],
129✔
72
                                                                        'description' => static function () {
129✔
73
                                                                                return __( 'The sizes to include. Can be overridden by `exclude`.', 'wp-graphql' );
15✔
74
                                                                        },
129✔
75
                                                                ],
129✔
76
                                                        ],
129✔
77
                                                        'description' => static function () {
129✔
78
                                                                return __( 'The available sizes of the mediaItem', 'wp-graphql' );
15✔
79
                                                        },
129✔
80
                                                        'resolve'     => static function ( $media_details, array $args ) {
129✔
81
                                                                // Bail early.
82
                                                                if ( empty( $media_details['sizes'] ) ) {
8✔
83
                                                                        return null;
×
84
                                                                }
85

86
                                                                // If the include arg is set, only include the sizes specified.
87
                                                                if ( ! empty( $args['include'] ) ) {
8✔
88
                                                                        $media_details['sizes'] = array_intersect_key( $media_details['sizes'], array_flip( $args['include'] ) );
1✔
89
                                                                }
90

91
                                                                // If the exclude arg is set, exclude the sizes specified.
92
                                                                if ( ! empty( $args['exclude'] ) ) {
8✔
93
                                                                        $media_details['sizes'] = array_diff_key( $media_details['sizes'], array_flip( $args['exclude'] ) );
1✔
94
                                                                }
95

96
                                                                $sizes = [];
8✔
97

98
                                                                foreach ( $media_details['sizes'] as $size_name => $size ) {
8✔
99
                                                                        $size['ID']   = $media_details['ID'];
8✔
100
                                                                        $size['name'] = $size_name;
8✔
101
                                                                        $sizes[]      = $size;
8✔
102
                                                                }
103

104
                                                                return ! empty( $sizes ) ? $sizes : null;
8✔
105
                                                        },
129✔
106
                                                ],
129✔
107
                                                'meta'     => [
129✔
108
                                                        'type'        => 'MediaItemMeta',
129✔
109
                                                        'description' => static function () {
129✔
110
                                                                return __( 'Meta information associated with the mediaItem', 'wp-graphql' );
15✔
111
                                                        },
129✔
112
                                                        'resolve'     => static function ( $media_details ) {
129✔
113
                                                                return ! empty( $media_details['image_meta'] ) ? $media_details['image_meta'] : null;
5✔
114
                                                        },
129✔
115
                                                ],
129✔
116
                                        ];
129✔
117
                                },
593✔
118
                        ]
593✔
119
                );
593✔
120
        }
121
}
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