self::get_input_fields(), 'outputFields' => self::get_output_fields(), 'mutateAndGetPayload' => self::mutate_and_get_payload(), ] ); } /** * Defines the mutation input field configuration * * @return array */ public static function get_input_fields() { return [ 'group' => [ 'type' => [ 'non_null' => 'String' ], 'description' => static function () { return __( 'Settings group ID.', 'graphql-for-ecommerce' ); }, ], 'settings' => [ 'type' => [ 'non_null' => [ 'list_of' => 'WCSettingInput' ] ], 'description' => static function () { return __( 'Settings to update.', 'graphql-for-ecommerce' ); }, ], ]; } /** * Defines the mutation output field configuration * * @return array */ public static function get_output_fields() { return [ 'settings' => [ 'type' => [ 'list_of' => 'WCSetting' ], 'description' => static function () { return __( 'The updated settings.', 'graphql-for-ecommerce' ); }, 'resolve' => static function ( $payload ) { return $payload['settings']; }, ], ]; } /** * Defines the mutation data modification closure. * * @return callable */ public static function mutate_and_get_payload() { return static function ( $input ) { if ( ! \wc_rest_check_manager_permissions( 'settings', 'edit' ) ) { throw new UserError( __( 'Sorry, you cannot update settings.', 'graphql-for-ecommerce' ) ); } $group_id = $input['group']; $controller = new \WC_REST_Setting_Options_Controller(); $updated = []; foreach ( $input['settings'] as $setting_input ) { $setting_id = $setting_input['id']; $value = $setting_input['value']; /** @var array|\WP_Error $setting */ $setting = $controller->get_setting( $group_id, $setting_id ); if ( is_wp_error( $setting ) ) { throw new UserError( $setting->get_error_message() ); } $validated = Setting_Update::validate_value( $value, $setting ); Setting_Update::save_setting( $setting, $validated ); /** @var array|\WP_Error $refreshed */ $refreshed = $controller->get_setting( $group_id, $setting_id ); if ( is_wp_error( $refreshed ) ) { throw new UserError( $refreshed->get_error_message() ); } $updated[] = $refreshed; } return [ 'settings' => $updated ]; }; } }