Automattic\WooCommerce\Admin

PluginsHelper::get_error_reason │ public static │ WC 11.2.0

Build a merchant-readable reason from the first WP_Error among the given candidates.

WordPress's upgrader reports the real failure (unmet PHP/WP version, filesystem, download) as a WP_Error whose message is a short sentence and whose data is often the detail sentence. Non-error candidates (false, null, true) are skipped.

The data is only appended when it reads as a detail: codes listed in OPAQUE_ERROR_DATA_CODES carry raw plugin output instead, and the result is capped at MAX_ERROR_REASON_LENGTH because callers persist and transmit it.

Method of the class: PluginsHelper{}

No Hooks.

Returns

string. The reason, HTML-stripped, trimmed and length-capped, or an empty string when none is available.

Usage

$result = PluginsHelper::get_error_reason( ...$candidates ): string;
...$candidates(mixed) (required)
Values that may be WP_Error instances, in order of preference.

Changelog

Since 11.2.0 Introduced.

PluginsHelper::get_error_reason() code WC 11.2.1

public static function get_error_reason( ...$candidates ): string {
	foreach ( $candidates as $candidate ) {
		if ( ! is_wp_error( $candidate ) ) {
			continue;
		}

		// Both come from WP_Error::add(), which stores whatever a filter callback passed in.
		$message = $candidate->get_error_message();
		$data    = $candidate->get_error_data();

		$parts = is_string( $message ) ? array( $message ) : array();
		if ( is_string( $data ) && ! in_array( $candidate->get_error_code(), self::OPAQUE_ERROR_DATA_CODES, true ) ) {
			$parts[] = $data;
		}

		return self::normalize_reason( implode( ' ', array_map( 'trim', $parts ) ) );
	}

	return '';
}