wpdb::process_fields()protectedWP 4.2.0

Processes arrays of field/value pairs and field formats.

This is a helper method for wpdb's CRUD methods, which take field/value pairs for inserts, updates, and where clauses. This method first pairs each value with a format. Then it determines the charset of that field, using that to determine if any invalid text would be stripped. If text is stripped, then field processing is rejected and the query fails.

Method of the class: wpdb{}

No Hooks.

Return

Array|false. An array of fields that contain paired value and formats. False for invalid values.

Usage

// protected - for code of main (parent) or child class
$result = $this->process_fields( $table, $data, $format );
$table(string) (required)
Table name.
$data(array) (required)
Array of values keyed by their field names.
$format(string[]|string) (required)
Formats or format to be mapped to the values in the data.

Changelog

Since 4.2.0 Introduced.

wpdb::process_fields() code WP 6.5.2

protected function process_fields( $table, $data, $format ) {
	$data = $this->process_field_formats( $data, $format );
	if ( false === $data ) {
		return false;
	}

	$data = $this->process_field_charsets( $data, $table );
	if ( false === $data ) {
		return false;
	}

	$data = $this->process_field_lengths( $data, $table );
	if ( false === $data ) {
		return false;
	}

	$converted_data = $this->strip_invalid_text( $data );

	if ( $data !== $converted_data ) {

		$problem_fields = array();
		foreach ( $data as $field => $value ) {
			if ( $value !== $converted_data[ $field ] ) {
				$problem_fields[] = $field;
			}
		}

		wp_load_translations_early();

		if ( 1 === count( $problem_fields ) ) {
			$this->last_error = sprintf(
				/* translators: %s: Database field where the error occurred. */
				__( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' ),
				reset( $problem_fields )
			);
		} else {
			$this->last_error = sprintf(
				/* translators: %s: Database fields where the error occurred. */
				__( 'WordPress database error: Processing the values for the following fields failed: %s. The supplied values may be too long or contain invalid data.' ),
				implode( ', ', $problem_fields )
			);
		}

		return false;
	}

	return $data;
}