Automattic\WooCommerce\Internal\Logging

SafeGlobalFunctionProxy::__callStatic()public staticWC 9.4.0

Proxy for trapping all calls on SafeGlobalFunctionProxy. Use this for calling WP and WC global functions safely. Example usage:

SafeGlobalFunctionProxy::wp_parse_url('https://example.com', PHP_URL_PATH);

Method of the class: SafeGlobalFunctionProxy{}

No Hooks.

Return

Mixed. The result of the function call, or null if an error occurs.

Usage

$result = SafeGlobalFunctionProxy::__callStatic( $name, $arguments );
$name(string) (required)
The name of the function to call.
$arguments(array) (required)
The arguments to pass to the function.

Changelog

Since 9.4.0 Introduced.

SafeGlobalFunctionProxy::__callStatic() code WC 9.6.0

public static function __callStatic( $name, $arguments ) {
	// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_set_error_handler -- Custom error handler is necessary to convert errors to exceptions
	set_error_handler(
		static function ( int $type, string $message, string $file, int $line ) {
			if ( __FILE__ === $file ) {
				// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace -- Used to adjust file and line number for accurate error reporting
				$trace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 3 );
				$file  = $trace[2]['file'] ?? $file;
				$line  = $trace[2]['line'] ?? $line;
			}
			$sanitized_message = filter_var( $message, FILTER_SANITIZE_FULL_SPECIAL_CHARS );
			// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- $message sanitised above. we don't want to rely on esc_html since it's not a PHP built-in
			throw new \ErrorException( $sanitized_message, 0, $type, $file, $line );
		}
	);

	try {
		self::maybe_load_missing_function( $name );
		$results = call_user_func_array( $name, $arguments );
	} catch ( \Throwable $e ) {
		self::log_wrapper_error( $name, $e->getMessage(), $arguments );
		$results = null;
	} finally {
		restore_error_handler();
	}

	return $results;
}