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

wp-graphql / wp-graphql-woocommerce / 13569798601

27 Feb 2025 03:21PM UTC coverage: 83.658% (-0.006%) from 83.664%
13569798601

push

github

web-flow
fix: "key" argument for MetaData fields fixed (#921)

18 of 19 new or added lines in 1 file covered. (94.74%)

1 existing line in 1 file now uncovered.

12429 of 14857 relevant lines covered (83.66%)

71.79 hits per line

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

97.8
/includes/type/object/class-meta-data-type.php
1
<?php
2
/**
3
 * WPObject Type - Order_Item_Type
4
 *
5
 * Registers MetaData type and queries
6
 *
7
 * @package WPGraphQL\WooCommerce\Type\WPObject
8
 * @since   0.0.2
9
 */
10

11
namespace WPGraphQL\WooCommerce\Type\WPObject;
12

13
/**
14
 * Class Meta_Data_Type
15
 */
16
class Meta_Data_Type {
17
        /**
18
         * Register Order type and queries to the WPGraphQL schema
19
         *
20
         * @return void
21
         */
22
        public static function register() {
23
                register_graphql_object_type(
141✔
24
                        'MetaData',
141✔
25
                        [
141✔
26
                                'description' => __( 'Extra data defined on the WC object', 'wp-graphql-woocommerce' ),
141✔
27
                                'fields'      => [
141✔
28
                                        'id'    => [
141✔
29
                                                'type'        => 'ID',
141✔
30
                                                'description' => __( 'Meta ID.', 'wp-graphql-woocommerce' ),
141✔
31
                                                'resolve'     => static function ( $source ) {
141✔
32
                                                        return ! empty( $source->id ) ? $source->id : null;
1✔
33
                                                },
141✔
34
                                        ],
141✔
35
                                        'key'   => [
141✔
36
                                                'type'        => [ 'non_null' => 'String' ],
141✔
37
                                                'description' => __( 'Meta key.', 'wp-graphql-woocommerce' ),
141✔
38
                                                'resolve'     => static function ( $source ) {
141✔
39
                                                        return ! empty( $source->key ) ? (string) $source->key : null;
13✔
40
                                                },
141✔
41
                                        ],
141✔
42
                                        'value' => [
141✔
43
                                                'type'        => 'String',
141✔
44
                                                'description' => __( 'Meta value.', 'wp-graphql-woocommerce' ),
141✔
45
                                                'resolve'     => static function ( $source ) {
141✔
46
                                                        if ( empty( $source->value ) ) {
13✔
47
                                                                return null;
1✔
48
                                                        }
49

50
                                                        if ( is_array( $source->value ) || is_object( $source->value ) ) {
13✔
51
                                                                return wp_json_encode( $source->value );
1✔
52
                                                        }
53

54
                                                        return (string) $source->value;
13✔
55
                                                },
141✔
56
                                        ],
141✔
57
                                ],
141✔
58
                        ]
141✔
59
                );
141✔
60
        }
61

62
        /**
63
         * Definition/Resolution of the metaData field used across the schema.
64
         *
65
         * @return array
66
         */
67
        public static function get_metadata_field_definition() {
68
                return [
141✔
69
                        'type'        => [ 'list_of' => 'MetaData' ],
141✔
70
                        'description' => __( 'Object meta data', 'wp-graphql-woocommerce' ),
141✔
71
                        'args'        => [
141✔
72
                                'key'      => [
141✔
73
                                        'type'        => 'String',
141✔
74
                                        'description' => __( 'Retrieve meta by key', 'wp-graphql-woocommerce' ),
141✔
75
                                ],
141✔
76
                                'keysIn'   => [
141✔
77
                                        'type'        => [ 'list_of' => 'String' ],
141✔
78
                                        'description' => __( 'Retrieve multiple metas by key', 'wp-graphql-woocommerce' ),
141✔
79
                                ],
141✔
80
                                'multiple' => [
141✔
81
                                        'type'        => 'Boolean',
141✔
82
                                        'description' => __( 'Retrieve meta with matching keys', 'wp-graphql-woocommerce' ),
141✔
83
                                ],
141✔
84
                        ],
141✔
85
                        'resolve'     => static function ( $source, array $args ) {
141✔
86
                                // Set unique flag.
87
                                $single = empty( $args['multiple'] );
11✔
88

89
                                // Check "key" argument and format meta_data objects.
90
                                if ( ! empty( $args['key'] ) && $source->meta_exists( $args['key'] ) ) {
11✔
91
                                        $key  = $args['key'];
1✔
92
                                        $data = $source->get_meta( $key, false );
1✔
93
                                        if ( empty( $data ) ) {
1✔
NEW
94
                                                $data = [];
×
95
                                        } elseif ( $single ) {
1✔
96
                                                $data = array_slice( $data, 0, 1 );
1✔
97
                                        }
98

99
                                        $data = array_map(
1✔
100
                                                static function ( $value ) {
1✔
101
                                                        return (object) $value;
1✔
102
                                                },
1✔
103
                                                $data
1✔
104
                                        );
1✔
105
                                } elseif ( ! empty( $args['keysIn'] ) ) {
11✔
106
                                        // Check "keysIn" argument and format meta_data objects.
107
                                        $keys_in = $args['keysIn'];
1✔
108

109
                                        $found = [];
1✔
110
                                        $data  = array_filter(
1✔
111
                                                $source->get_meta_data(),
1✔
112
                                                static function ( $meta ) use ( $keys_in, $single, &$found ) {
1✔
113
                                                        if ( in_array( $meta->key, $keys_in, true ) ) {
1✔
114
                                                                if ( $single && in_array( $meta->key, $found, true ) ) {
1✔
UNCOV
115
                                                                        return false;
×
116
                                                                }
117
                                                                $found[] = $meta->key;
1✔
118
                                                                return true;
1✔
119
                                                        }
120
                                                }
1✔
121
                                        );
1✔
122
                                } else {
123
                                        // If no arguments set return all meta (in accordance with unique flag).
124
                                        $found = [];
11✔
125
                                        $data  = array_filter(
11✔
126
                                                $source->get_meta_data(),
11✔
127
                                                static function ( $meta ) use ( $single, &$found ) {
11✔
128
                                                        if ( $single && in_array( $meta->key, $found, true ) ) {
11✔
129
                                                                return false;
1✔
130
                                                        }
131

132
                                                        $found[] = $meta->key;
11✔
133
                                                        return true;
11✔
134
                                                }
11✔
135
                                        );
11✔
136
                                }//end if
137

138
                                return ! empty( $data ) ? $data : null;
11✔
139
                        },
141✔
140
                ];
141✔
141
        }
142
}
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

© 2026 Coveralls, Inc