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

wp-graphql / wp-graphql-woocommerce / 5441823169

pending completion
5441823169

push

github

web-flow
feat: Account URL added to Auth URLs. (#755)

* feat: Account URL added to Auth URLs.

* fix: Account URL issues resolved + Variation Attribute connections refactored.

128 of 128 new or added lines in 7 files covered. (100.0%)

10250 of 12439 relevant lines covered (82.4%)

54.01 hits per line

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

96.61
/includes/data/connection/class-variation-attribute-connection-resolver.php
1
<?php
2
/**
3
 * ConnectionResolver - Variation_Attribute_Connection_Resolver
4
 *
5
 * Resolves connections to VariationAttributes
6
 *
7
 * @package WPGraphQL\WooCommerce\Data\Connection
8
 * @since 0.0.4
9
 */
10

11
namespace WPGraphQL\WooCommerce\Data\Connection;
12

13
use GraphQL\Type\Definition\ResolveInfo;
14
use GraphQLRelay\Relay;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\WooCommerce\Model\Product;
17

18
/**
19
 * Class Variation_Attribute_Connection_Resolver
20
 *
21
 * @package WPGraphQL\WooCommerce\Data\Connection
22
 */
23
class Variation_Attribute_Connection_Resolver {
24

25
        /**
26
         * Returns data array from WC_Product_Attribute ArrayAccess object.
27
         *
28
         * @param array $attrs       Source Product's WC_Product_Attribute(s) object.
29
         * @param int   $product_id  Source Product ID.
30
         * @return array
31
         */
32
        public static function product_attributes_to_data_array( $attrs, $product_id ) {
33
                $attributes = [];
1✔
34

35
                foreach ( $attrs as $attribute ) {
1✔
36
                        if ( ! is_a( $attribute, 'WC_Product_Attribute' ) ) {
1✔
37
                                continue;
×
38
                        }
39
                        $name = wc_attribute_label( $attribute->get_name() );
1✔
40

41
                        if ( $attribute->is_taxonomy() ) {
1✔
42
                                $attribute_taxonomy = $attribute->get_taxonomy_object();
1✔
43
                                $attribute_values   = wc_get_product_terms( $product_id, $attribute->get_name(), [ 'fields' => 'all' ] );
1✔
44
                                foreach ( $attribute_values as $attribute_value ) {
1✔
45
                                        $id           = base64_encode( $product_id . '|' . $name . '|' . $attribute_value->name ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
1✔
46
                                        $attributes[] = [
1✔
47
                                                'id'          => $id,
1✔
48
                                                'attributeId' => $attribute_value->term_id,
1✔
49
                                                'name'        => $name,
1✔
50
                                                'value'       => $attribute_value->name,
1✔
51
                                        ];
1✔
52
                                }
53
                        } else {
54
                                $values = $attribute->get_options();
1✔
55
                                foreach ( $values as $attribute_value ) {
1✔
56
                                        $id           = base64_encode( $product_id . '|' . $name . '|' . $attribute_value ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
1✔
57
                                        $attributes[] = [
1✔
58
                                                'id'          => $id,
1✔
59
                                                'attributeId' => 0,
1✔
60
                                                'name'        => $name,
1✔
61
                                                'value'       => $attribute_value,
1✔
62
                                        ];
1✔
63
                                }
64
                        }//end if
65
                }//end foreach
66

67
                return $attributes;
1✔
68
        }
69

70
        /**
71
         * Returns data array from WC_Product_Attribute ArrayAccess object.
72
         *
73
         * @param array      $attrs         WC_Product_Attribute object.
74
         * @param string|int $variation_id  Variable Product or Variation ID.
75
         *
76
         * @return array
77
         */
78
        public static function variation_attributes_to_data_array( $attrs, $variation_id ) {
79
                $attributes = [];
3✔
80

81
                // Bail early if explicitly '0' attributes.
82
                if ( [ '0' ] === $attrs ) {
3✔
83
                        return $attributes;
×
84
                }
85

86
                foreach ( $attrs as $name => $value ) {
3✔
87
                        $term = \get_term_by( 'slug', $value, $name );
3✔
88

89
                        // ID create for caching only, not object retrieval.
90
                        // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
91
                        $id = base64_encode( $variation_id . '||' . $name . '||' . $value );
3✔
92

93
                        if ( ! $term instanceof \WP_Term ) {
3✔
94
                                $attributes[] = [
2✔
95
                                        'id'          => $id,
2✔
96
                                        'attributeId' => 0,
2✔
97
                                        'name'        => $name,
2✔
98
                                        'value'       => $value,
2✔
99
                                ];
2✔
100
                        } else {
101
                                $attributes[] = [
3✔
102
                                        'id'          => $id,
3✔
103
                                        'attributeId' => $term->term_id,
3✔
104
                                        'name'        => $term->taxonomy,
3✔
105
                                        'value'       => $term->name,
3✔
106
                                ];
3✔
107
                        }
108
                }//end foreach
109

110
                return $attributes;
3✔
111
        }
112

113
        /**
114
         * Creates connection
115
         *
116
         * @param mixed       $source     - Connection source Model instance.
117
         * @param array       $args       - Connection arguments.
118
         * @param AppContext  $context    - AppContext object.
119
         * @param ResolveInfo $info       - ResolveInfo object.
120
         *
121
         * @return array|null
122
         */
123
        public function resolve( $source, array $args, AppContext $context, ResolveInfo $info ) {
124
                if ( is_a( $source, Product::class ) && 'simple' === $source->get_type() ) {
3✔
125
                        $attributes = self::product_attributes_to_data_array( $source->attributes, $source->ID );
1✔
126
                } else {
127
                        $attributes = self::variation_attributes_to_data_array(
2✔
128
                                is_a( $source, Product::class ) ? $source->default_attributes : $source->attributes,
2✔
129
                                $source->ID
2✔
130
                        );
2✔
131
                }
132

133
                $connection = Relay::connectionFromArray( $attributes, $args );
3✔
134
                $nodes      = [];
3✔
135
                if ( ! empty( $connection['edges'] ) && is_array( $connection['edges'] ) ) {
3✔
136
                        foreach ( $connection['edges'] as $edge ) {
3✔
137
                                $nodes[] = ! empty( $edge['node'] ) ? $edge['node'] : null;
3✔
138
                        }
139
                }
140
                $connection['nodes'] = ! empty( $nodes ) ? $nodes : null;
3✔
141
                return ! empty( $attributes ) ? $connection : null;
3✔
142
        }
143
}
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