WC_AJAX::shipping_classes_save_changes()public staticWC 1.0

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

Method of the class: WC_AJAX{}

Return

null. Nothing (null).

Usage

$result = WC_AJAX::shipping_classes_save_changes();

WC_AJAX::shipping_classes_save_changes() code WC 8.9.0

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

	if ( ! wp_verify_nonce( wp_unslash( $_POST['wc_shipping_classes_nonce'] ), 'wc_shipping_classes_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();
	}

	$changes = wp_unslash( $_POST['changes'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

	foreach ( $changes as $term_id => $data ) {
		$term_id = absint( $term_id );

		if ( isset( $data['deleted'] ) ) {
			if ( isset( $data['newRow'] ) ) {
				// So the user added and deleted a new row.
				// That's fine, it's not in the database anyways. NEXT!
				continue;
			}
			/**
			 * Notifies that a non-option setting has been deleted.
			 *
			 * @since 7.8.0
			 */
			do_action(
				'woocommerce_update_non_option_setting',
				array(
					'id'     => 'shipping_class',
					'action' => 'delete',
				)
			);
			wp_delete_term( $term_id, 'product_shipping_class' );
			continue;
		}

		$update_args = array();

		if ( isset( $data['name'] ) ) {
			/**
			 * Notify that a non-option setting has been updated.
			 *
			 * @since 7.8.0
			 */
			do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class_name' ) );
			$update_args['name'] = wc_clean( $data['name'] );
		}

		if ( isset( $data['slug'] ) ) {
			/**
			 * Notify that a non-option setting has been updated.
			 *
			 * @since 7.8.0
			 */
			do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class_slug' ) );
			$update_args['slug'] = wc_clean( $data['slug'] );
		}

		if ( isset( $data['description'] ) ) {
			/**
			 * Notify that a non-option setting has been updated.
			 *
			 * @since 7.8.0
			 */
			do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class_description' ) );
			$update_args['description'] = wc_clean( $data['description'] );
		}

		if ( isset( $data['newRow'] ) ) {
			$update_args = array_filter( $update_args );
			if ( empty( $update_args['name'] ) ) {
				continue;
			}
			/**
			 * Notifies that a non-option setting has been added.
			 *
			 * @since 7.8.0
			 */
			do_action(
				'woocommerce_update_non_option_setting',
				array(
					'id'     => 'shipping_class',
					'action' => 'add',
				)
			);
			$inserted_term = wp_insert_term( $update_args['name'], 'product_shipping_class', $update_args );
			$term_id       = is_wp_error( $inserted_term ) ? 0 : $inserted_term['term_id'];
		} else {
			/**
			 * Notifies that a non-option setting has been updated.
			 *
			 * @since 7.8.0
			 */
			do_action( 'woocommerce_update_non_option_setting', array( 'id' => 'shipping_class' ) );
			wp_update_term( $term_id, 'product_shipping_class', $update_args );
		}

		do_action( 'woocommerce_shipping_classes_save_class', $term_id, $data );
	}

	global $current_tab, $current_section;
	$current_tab     = 'shipping';
	$current_section = 'classes';
	/**
	 * Completes the saving process for options.
	 *
	 * @since 7.8.0
	 */
	do_action( 'woocommerce_update_options' );
	$wc_shipping = WC_Shipping::instance();

	wp_send_json_success(
		array(
			'shipping_classes' => $wc_shipping->get_shipping_classes(),
		)
	);
}