Automattic\WooCommerce\Blocks\Domain

Bootstrap::deprecated_dependency()protectedWC 1.0

Throws a deprecation notice for a dependency without breaking requests.

Method of the class: Bootstrap{}

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->deprecated_dependency( $function, $version, $replacement, $trigger_error_version );
$function(string) (required)
Class or function being deprecated.
$version(string) (required)
Version in which it was deprecated.
$replacement(string)
Replacement class or function, if applicable.
Default: ''
$trigger_error_version(string)
Optional version to start surfacing this as a PHP error rather than a log.
Default: $version

Bootstrap::deprecated_dependency() code WC 9.0.1

protected function deprecated_dependency( $function, $version, $replacement = '', $trigger_error_version = '' ) {
	if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) {
		return;
	}

	$trigger_error_version = $trigger_error_version ? $trigger_error_version : $version;
	$error_message         = $replacement ? sprintf(
		'%1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.',
		$function,
		$version,
		$replacement
	) : sprintf(
		'%1$s is <strong>deprecated</strong> since version %2$s with no alternative available.',
		$function,
		$version
	);
	/**
	 * Fires when a deprecated function is called.
	 *
	 * @since 7.3.0
	 */
	do_action( 'deprecated_function_run', $function, $replacement, $version );

	$log_error = false;

	// If headers have not been sent yet, log to avoid breaking the request.
	if ( ! headers_sent() ) {
		$log_error = true;
	}

	// If the $trigger_error_version was not yet reached, only log the error.
	if ( version_compare( Constants::get_constant( 'WC_VERSION' ), $trigger_error_version, '<' ) ) {
		$log_error = true;
	}

	/**
	 * Filters whether to trigger an error for deprecated functions. (Same as WP core)
	 *
	 * @since 7.3.0
	 *
	 * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
	 */
	if ( ! apply_filters( 'deprecated_function_trigger_error', true ) ) {
		$log_error = true;
	}

	if ( $log_error ) {
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
		error_log( $error_message );
	} else {
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
		trigger_error( $error_message, E_USER_DEPRECATED );
	}
}