WC_REST_Webhooks_V1_Controller::prepare_item_for_database()protectedWC 1.0

Prepare a single webhook for create or update.

Method of the class: WC_REST_Webhooks_V1_Controller{}

Return

WP_Error|stdClass. $data Post object.

Usage

// protected - for code of main (parent) or child class
$result = $this->prepare_item_for_database( $request );
$request(WP_REST_Request) (required)
Request object.

WC_REST_Webhooks_V1_Controller::prepare_item_for_database() code WC 9.4.2

protected function prepare_item_for_database( $request ) {
	$data = new stdClass();

	// Post ID.
	if ( isset( $request['id'] ) ) {
		$data->ID = absint( $request['id'] );
	}

	// Validate required POST fields.
	if ( 'POST' === $request->get_method() && empty( $data->ID ) ) {
		$data->post_title = ! empty( $request['name'] ) ? $request['name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), (new DateTime('now'))->format( _x( 'M d, Y @ h:i A', 'Webhook created on date parsed by DateTime::format', 'woocommerce' ) ) ); // @codingStandardsIgnoreLine

		// Post author.
		$data->post_author = get_current_user_id();

		// Post password.
		$data->post_password = 'webhook_' . wp_generate_password();

		// Post status.
		$data->post_status = 'publish';
	} else {

		// Allow edit post title.
		if ( ! empty( $request['name'] ) ) {
			$data->post_title = $request['name'];
		}
	}

	// Comment status.
	$data->comment_status = 'closed';

	// Ping status.
	$data->ping_status = 'closed';

	/**
	 * Filter the query_vars used in `get_items` for the constructed query.
	 *
	 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
	 * prepared for insertion.
	 *
	 * @param stdClass        $data An object representing a single item prepared
	 *                                       for inserting or updating the database.
	 * @param WP_REST_Request $request       Request object.
	 */
	return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $data, $request );
}