WC_Admin_Webhooks::save()privateWC 1.0

Save method.

Method of the class: WC_Admin_Webhooks{}

Hooks from the method

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->save();

WC_Admin_Webhooks::save() code WC 8.7.0

private function save() {
	check_admin_referer( 'woocommerce-settings' );

	if ( ! current_user_can( 'manage_woocommerce' ) ) {
		wp_die( esc_html__( 'You do not have permission to update Webhooks', 'woocommerce' ) );
	}

	$errors = array();
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$webhook_id = isset( $_POST['webhook_id'] ) ? absint( $_POST['webhook_id'] ) : 0;
	$webhook    = new WC_Webhook( $webhook_id );

	// Name.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( ! empty( $_POST['webhook_name'] ) ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$name = sanitize_text_field( wp_unslash( $_POST['webhook_name'] ) );
	} else {
		$name = sprintf(
			/* translators: %s: date */
			__( 'Webhook created on %s', 'woocommerce' ),
			// @codingStandardsIgnoreStart
			(new DateTime('now'))->format( _x( 'M d, Y @ h:i A', 'Webhook created on date parsed by DateTime::format', 'woocommerce' ) )
			// @codingStandardsIgnoreEnd
		);
	}

	$webhook->set_name( $name );

	if ( ! $webhook->get_user_id() ) {
		$webhook->set_user_id( get_current_user_id() );
	}

	// Status.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$webhook->set_status( ! empty( $_POST['webhook_status'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_status'] ) ) : 'disabled' );

	// Delivery URL.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? esc_url_raw( wp_unslash( $_POST['webhook_delivery_url'] ) ) : '';

	if ( wc_is_valid_url( $delivery_url ) ) {
		$webhook->set_delivery_url( $delivery_url );
	}

	// Secret.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$secret = ! empty( $_POST['webhook_secret'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_secret'] ) ) : wp_generate_password( 50, true, true );
	$webhook->set_secret( $secret );

	// Topic.
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( ! empty( $_POST['webhook_topic'] ) ) {
		$resource = '';
		$event    = '';

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		switch ( $_POST['webhook_topic'] ) {
			case 'action':
				$resource = 'action';
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended
				$event = ! empty( $_POST['webhook_action_event'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_action_event'] ) ) : '';
				break;

			default:
				// phpcs:ignore WordPress.Security.NonceVerification.Recommended
				list( $resource, $event ) = explode( '.', sanitize_text_field( wp_unslash( $_POST['webhook_topic'] ) ) );
				break;
		}

		$topic = $resource . '.' . $event;

		if ( wc_is_webhook_valid_topic( $topic ) ) {
			$webhook->set_topic( $topic );
		} else {
			$errors[] = __( 'Webhook topic unknown. Please select a valid topic.', 'woocommerce' );
		}
	}

	// API version.
	$rest_api_versions = wc_get_webhook_rest_api_versions();
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$webhook->set_api_version( ! empty( $_POST['webhook_api_version'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_api_version'] ) ) : end( $rest_api_versions ) );

	$webhook->save();

	// Run actions.
	do_action( 'woocommerce_webhook_options_save', $webhook->get_id() );
	if ( $errors ) {
		// Redirect to webhook edit page to avoid settings save actions.
		wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() . '&error=' . rawurlencode( implode( '|', $errors ) ) ) );
		exit();
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	} elseif ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $webhook->get_pending_delivery() ) {
		// Ping the webhook at the first time that is activated.
		$result = $webhook->deliver_ping();

		if ( is_wp_error( $result ) ) {
			// Redirect to webhook edit page to avoid settings save actions.
			wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() . '&error=' . rawurlencode( $result->get_error_message() ) ) );
			exit();
		}
	}

	// Redirect to webhook edit page to avoid settings save actions.
	wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=advanced&section=webhooks&edit-webhook=' . $webhook->get_id() . '&updated=1' ) );
	exit();
}