GC_Debug_Data::format()

最后更新于:2021-11-27 13:56:26

GC_Debug_Data::format( array$info_array, string$type)

Format the information gathered for debugging, in a manner suitable for copying to a forum or support ticket.

参数

$info_array

(array) (Required) Information gathered from the GC_Debug_Data::debug_data function.

$type

(string) (Required) The data type to return, either ‘info’ or ‘debug’.

响应

(string) The formatted data.

源文件

文件: gc-admin/includes/class-gc-debug-data.php

	public static function format( $info_array, $type ) {
		$return = "`n";

		foreach ( $info_array as $section => $details ) {
			// Skip this section if there are no fields, or the section has been declared as private.
			if ( empty( $details['fields'] ) || ( isset( $details['private'] ) && $details['private'] ) ) {
				continue;
			}

			$section_label = 'debug' === $type ? $section : $details['label'];

			$return .= sprintf(
				"### %s%s ###nn",
				$section_label,
				( isset( $details['show_count'] ) && $details['show_count'] ? sprintf( ' (%d)', count( $details['fields'] ) ) : '' )
			);

			foreach ( $details['fields'] as $field_name => $field ) {
				if ( isset( $field['private'] ) && true === $field['private'] ) {
					continue;
				}

				if ( 'debug' === $type && isset( $field['debug'] ) ) {
					$debug_data = $field['debug'];
				} else {
					$debug_data = $field['value'];
				}

				// Can be array, one level deep only.
				if ( is_array( $debug_data ) ) {
					$value = '';

					foreach ( $debug_data as $sub_field_name => $sub_field_value ) {
						$value .= sprintf( "nt%s: %s", $sub_field_name, $sub_field_value );
					}
				} elseif ( is_bool( $debug_data ) ) {
					$value = $debug_data ? 'true' : 'false';
				} elseif ( empty( $debug_data ) && '0' !== $debug_data ) {
					$value = 'undefined';
				} else {
					$value = $debug_data;
				}

				if ( 'debug' === $type ) {
					$label = $field_name;
				} else {
					$label = $field['label'];
				}

				$return .= sprintf( "%s: %sn", $label, $value );
			}

			$return .= "n";
		}

		$return .= '`';

		return $return;
	}