WC_AJAX::shipping_zone_methods_save_changes()public staticWC 1.0

Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.

Метод класса: WC_AJAX{}

Возвращает

null. Ничего (null).

Использование

$result = WC_AJAX::shipping_zone_methods_save_changes();

Код WC_AJAX::shipping_zone_methods_save_changes() WC 8.7.0

public static function shipping_zone_methods_save_changes() {
	if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['changes'] ) ) {
		wp_send_json_error( 'missing_fields' );
		wp_die();
	}

	if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_zones_nonce'] ), 'wc_shipping_zones_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
		wp_send_json_error( 'bad_nonce' );
		wp_die();
	}

	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		wp_send_json_error( 'missing_capabilities' );
		wp_die();
	}

	global $wpdb;

	$zone_id = wc_clean( wp_unslash( $_POST['zone_id'] ) );
	$zone    = new WC_Shipping_Zone( $zone_id );
	// A shipping zone can be created here if the user is adding a method without first saving the shipping zone.
	if ( '' === $zone_id ) {
		/**
		 * Notifies that a non-option setting has been added.
		 *
		 * @since 7.8.0
		 */
		do_action(
			'woocommerce_update_non_option_setting',
			array(
				'id'     => 'shipping_zone',
				'action' => 'add',
			)
		);
	}
	$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	if ( isset( $changes['zone_name'] ) ) {
		/**
		 * Notifies that a non-option setting has been updated.
		 *
		 * @since 7.8.0
		 */
		do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_name' ) );
		$zone->set_zone_name( wc_clean( $changes['zone_name'] ) );
	}

	if ( isset( $changes['zone_locations'] ) ) {
		/**
		 * Notifies that a non-option setting has been updated.
		 *
		 * @since 7.8.0
		 */
		do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_locations' ) );
		$zone->clear_locations( array( 'state', 'country', 'continent' ) );
		$locations = array_filter( array_map( 'wc_clean', (array) $changes['zone_locations'] ) );
		foreach ( $locations as $location ) {
			// Each posted location will be in the format type:code.
			$location_parts = explode( ':', $location );
			switch ( $location_parts[0] ) {
				case 'state':
					$zone->add_location( $location_parts[1] . ':' . $location_parts[2], 'state' );
					break;
				case 'country':
					$zone->add_location( $location_parts[1], 'country' );
					break;
				case 'continent':
					$zone->add_location( $location_parts[1], 'continent' );
					break;
			}
		}
	}

	if ( isset( $changes['zone_postcodes'] ) ) {
		/**
		 * Notifies that a non-option setting has been updated.
		 *
		 * @since 7.8.0
		 */
		do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_postcodes' ) );
		$zone->clear_locations( 'postcode' );
		$postcodes = array_filter( array_map( 'strtoupper', array_map( 'wc_clean', explode( "\n", $changes['zone_postcodes'] ) ) ) );
		foreach ( $postcodes as $postcode ) {
			$zone->add_location( $postcode, 'postcode' );
		}
	}

	if ( isset( $changes['methods'] ) ) {
		foreach ( $changes['methods'] as $instance_id => $data ) {
			$method_id = $wpdb->get_var( $wpdb->prepare( "SELECT method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d", $instance_id ) );

			if ( isset( $data['deleted'] ) ) {
				$shipping_method = WC_Shipping_Zones::get_shipping_method( $instance_id );
				$option_key      = $shipping_method->get_instance_option_key();
				if ( $wpdb->delete( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'instance_id' => $instance_id ) ) ) {
					delete_option( $option_key );
					/**
					 * Notifies that a non-option setting has been deleted.
					 *
					 * @since 7.8.0
					 */
					do_action(
						'woocommerce_update_non_option_setting',
						array(
							'id'     => 'zone_method',
							'action' => 'delete',
						)
					);
					do_action( 'woocommerce_shipping_zone_method_deleted', $instance_id, $method_id, $zone_id );
				}
				continue;
			}

			$method_data = array_intersect_key(
				$data,
				array(
					'method_order' => 1,
					'enabled'      => 1,
				)
			);

			if ( isset( $method_data['method_order'] ) ) {
				/**
				 * Notifies that a non-option setting has been updated.
				 *
				 * @since 7.8.0
				 */
				do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_methods_order' ) );
				$wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'method_order' => absint( $method_data['method_order'] ) ), array( 'instance_id' => absint( $instance_id ) ) );
			}

			if ( isset( $method_data['enabled'] ) ) {
				/**
				 * Notifies that a non-option setting has been updated.
				 *
				 * @since 7.8.0
				 */
				do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'zone_methods_enabled' ) );
				$is_enabled = absint( 'yes' === $method_data['enabled'] );
				if ( $wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'is_enabled' => $is_enabled ), array( 'instance_id' => absint( $instance_id ) ) ) ) {
					do_action( 'woocommerce_shipping_zone_method_status_toggled', $instance_id, $method_id, $zone_id, $is_enabled );
				}
			}
		}
	}

	$zone->save();

	global $current_tab;
	$current_tab = 'shipping';
	/**
	 * Completes the saving process for options.
	 *
	 * @since 7.8.0
	 */
	do_action( 'woocommerce_update_options' );

	wp_send_json_success(
		array(
			'zone_id'   => $zone->get_id(),
			'zone_name' => $zone->get_zone_name(),
			'methods'   => $zone->get_shipping_methods( false, 'json' ),
		)
	);
}