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

wp-graphql / wp-graphql-woocommerce / 9122748207

17 May 2024 04:03AM UTC coverage: 85.048% (+0.4%) from 84.647%
9122748207

push

github

web-flow
feat: Queries and mutations for shipping zones, tax classes, and tax rates. (#856)

* fix: General bugfixes and improvements

* devops: New mutations and types tested and compliance with Linter and PHPStan

* chore: hooks added to the mutation resolvers

* feat: permission checks added

* chore: Linter and compliance met

* chore: Linter and compliance met

1252 of 1399 new or added lines in 39 files covered. (89.49%)

9 existing lines in 2 files now uncovered.

12423 of 14607 relevant lines covered (85.05%)

70.56 hits per line

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

91.89
/includes/data/mutation/class-shipping-mutation.php
1
<?php
2
/**
3
 * Defines helper functions for executing mutations related to shipping.
4
 *
5
 * @package WPGraphQL\WooCommerce\Data\Mutation
6
 * @since TBD
7
 */
8

9
namespace WPGraphQL\WooCommerce\Data\Mutation;
10

11
/**
12
 * Class - Shipping_Mutation
13
 */
14
class Shipping_Mutation {
15
        /**
16
         * Maps the settings input for insertion.
17
         *
18
         * @param array<array<string, string>> $settings_input  Settings input.
19
         *
20
         * @return array<string, string>
21
         */
22
        private static function flatten_settings_input( $settings_input ) {
23
                $settings = [];
2✔
24
                foreach ( $settings_input as $setting ) {
2✔
25
                        $settings[ $setting['id'] ] = $setting['value'];
2✔
26
                }
27
                return $settings;
2✔
28
        }
29

30
        /**
31
         * Updates settings on a shipping zone method.
32
         *
33
         * @param int                 $instance_id     Instance ID.
34
         * @param \WC_Shipping_Method $method          Shipping method data.
35
         * @param array               $settings_input  Settings input.
36
         *
37
         * @return \WC_Shipping_Method
38
         */
39
        public static function set_shipping_zone_method_settings( $instance_id, $method, $settings_input ) {
40
                $settings = self::flatten_settings_input( $settings_input );
2✔
41
                $method->init_instance_settings();
2✔
42
                $instance_settings = $method->instance_settings;
2✔
43
                $errors_found      = false;
2✔
44
                foreach ( $method->get_instance_form_fields() as $key => $field ) {
2✔
45
                        if ( isset( $settings[ $key ] ) ) {
2✔
46
                                if ( is_callable( [ Settings_Mutation::class, 'validate_setting_' . $field['type'] . '_field' ] ) ) {
2✔
47
                                        $value = Settings_Mutation::{'validate_setting_' . $field['type'] . '_field'}( $settings[ $key ], $field );
2✔
48
                                } else {
NEW
49
                                        $value = Settings_Mutation::validate_setting_text_field( $settings[ $key ], $field );
×
50
                                }
51
                                $instance_settings[ $key ] = $value;
2✔
52
                        }
53
                }
54

55
                update_option(
2✔
56
                        $method->get_instance_option_key(),
2✔
57
                        apply_filters(
2✔
58
                                'woocommerce_shipping_' . $method->id . '_instance_settings_values', // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
2✔
59
                                $instance_settings,
2✔
60
                                $method
2✔
61
                        )
2✔
62
                );
2✔
63

64
                $method->instance_settings = $instance_settings;
2✔
65
                return $method;
2✔
66
        }
67

68
        /**
69
         * Updates the order of a shipping zone method.
70
         *
71
         * @param int                 $instance_id  Instance ID.
72
         * @param \WC_Shipping_Method $method       Shipping method data.
73
         * @param int                 $order        Order.
74
         *
75
         * @return \WC_Shipping_Method
76
         */
77
        public static function set_shipping_zone_method_order( $instance_id, $method, $order ) {
78
                global $wpdb;
1✔
79

80
                $wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
1✔
81
                        "{$wpdb->prefix}woocommerce_shipping_zone_methods",
1✔
82
                        [ 'method_order' => $order ],
1✔
83
                        [ 'instance_id' => $instance_id ]
1✔
84
                );
1✔
85
                $method->method_order = $order;
1✔
86

87
                return $method;
1✔
88
        }
89

90
        /**
91
         * Updates the enabled status of a shipping zone method.
92
         *
93
         * @param int                 $zone_id      Zone ID.
94
         * @param int                 $instance_id  Instance ID.
95
         * @param \WC_Shipping_Method $method       Shipping method data.
96
         * @param bool                $enabled      Enabled status.
97
         *
98
         * @return \WC_Shipping_Method
99
         */
100
        public static function set_shipping_zone_method_enabled( $zone_id, $instance_id, $method, $enabled ) {
101
                global $wpdb;
1✔
102

103
                if ( $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", [ 'is_enabled' => $enabled ], [ 'instance_id' => $instance_id ] ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery
1✔
NEW
104
                        do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method->id, $zone_id, $enabled ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
×
NEW
105
                        $method->enabled = ( true === $enabled ? 'yes' : 'no' );
×
106
                }
107

108
                return $method;
1✔
109
        }
110
}
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