Automattic\WooCommerce\Internal\Admin\Orders\MetaBoxes

CustomMetaBox::add_meta_ajax()publicWC 1.0

Reimplementation of WP core's wp_ajax_add_meta method to support order custom meta updates with custom tables.

Method of the class: CustomMetaBox{}

No Hooks.

Return

null. Nothing (null).

Usage

$CustomMetaBox = new CustomMetaBox();
$CustomMetaBox->add_meta_ajax();

CustomMetaBox::add_meta_ajax() code WC 8.7.0

public function add_meta_ajax() {
	if ( ! check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' ) ) {
		wp_send_json_error( 'invalid_nonce' );
		wp_die();
	}

	$order_id = (int) $_POST['order_id'] ?? 0;
	$order    = $this->verify_order_edit_permission_for_ajax( $order_id );

	if ( isset( $_POST['metakeyselect'] ) && '#NONE#' === $_POST['metakeyselect'] && empty( $_POST['metakeyinput'] ) ) {
		wp_die( 1 );
	}

	if ( isset( $_POST['metakeyinput'] ) ) { // add meta.
		$meta_key   = sanitize_text_field( wp_unslash( $_POST['metakeyinput'] ) );
		$meta_value = sanitize_text_field( wp_unslash( $_POST['metavalue'] ?? '' ) );
		$this->handle_add_meta( $order, $meta_key, $meta_value );
	} else { // update.
		$meta = wp_unslash( $_POST['meta'] ?? array() ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- sanitization done below in array_walk.
		$this->handle_update_meta( $order, $meta );
	}
}