函数文档

wp_privacy_generate_personal_data_export_group_html()

💡 云策文档标注

概述

wp_privacy_generate_personal_data_export_group_html() 函数用于生成个人数据导出报告中单个组的 HTML 输出。它处理组数据,包括标签、描述和项目,并应用适当的转义和过滤以确保安全性。

关键要点

  • 函数接受 $group_data、$group_id 和 $groups_count 参数,其中 $group_data 必须包含 group_label 和 items 数组。
  • 输出 HTML 包括组标题、项目列表(每个项目为名称-值对),并自动将类似链接的值转换为超链接。
  • 使用 esc_html()、wp_kses() 等函数进行安全转义,防止 XSS 攻击。
  • 当 $groups_count 大于 1 时,会添加“返回顶部”链接以增强导航性。
  • 函数从 WordPress 5.4.0 开始支持 $group_id 和 $groups_count 参数,初始版本为 4.9.6。

代码示例

function wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id = '', $groups_count = 1 ) {
    $group_id_attr = sanitize_title_with_dashes( $group_data['group_label'] . '-' . $group_id );
    $group_html  = '';
    $group_html .= esc_html( $group_data['group_label'] );
    $items_count = count( (array) $group_data['items'] );
    if ( $items_count > 1 ) {
        $group_html .= sprintf( ' (%d)', $items_count );
    }
    $group_html .= '';
    if ( ! empty( $group_data['group_description'] ) ) {
        $group_html .= '' . esc_html( $group_data['group_description'] ) . '';
    }
    $group_html .= '';
    foreach ( (array) $group_data['items'] as $group_item_id => $group_item_data ) {
        $group_html .= '';
        $group_html .= '';
        foreach ( (array) $group_item_data as $group_item_datum ) {
            $value = $group_item_datum['value'];
            if ( ! str_contains( $value, ' ' ) && ( str_starts_with( $value, 'http://' ) || str_starts_with( $value, 'https://' ) ) ) {
                $value = '' . esc_html( $value ) . '';
            }
            $group_html .= '';
            $group_html .= '' . esc_html( $group_item_datum['name'] ) . '';
            $group_html .= '' . wp_kses( $value, 'personal_data_export' ) . '';
            $group_html .= '';
        }
        $group_html .= '';
        $group_html .= '';
    }
    if ( $groups_count > 1 ) {
        $group_html .= '';
        $group_html .= '↑  ' . esc_html__( 'Go to top' ) . '';
        $group_html .= '';
    }
    $group_html .= '';
    return $group_html;
}

注意事项

  • 确保 $group_data 数组结构正确,包含 group_label 和 items 键,否则可能导致错误输出。
  • 函数内部使用 wp_kses() 和 'personal_data_export' 上下文过滤值,需确保该上下文已定义或允许必要的 HTML 标签。
  • 在 WordPress 5.4.0 之前,函数仅接受 $group_data 参数,升级时需注意参数兼容性。

📄 原文内容

Generate a single group for the personal data export report.

Parameters

$group_dataarrayrequired
The group data to render.
  • group_label string
    The user-facing heading for the group, e.g. 'Comments'.
  • items array
    An array of group items.
    • group_item_data array
      An array of name-value pairs for the item.
      • name string
        The user-facing name of an item name-value pair, e.g. ‘IP Address’.
      • value string
        The user-facing value of an item data pair, e.g. '50.60.70.0'.
        }
$group_idstringrequired
The group identifier.
$groups_countintoptional
The number of all groups

Default:1

Return

string The HTML for this group and its items.

Source

function wp_privacy_generate_personal_data_export_group_html( $group_data, $group_id = '', $groups_count = 1 ) {
	$group_id_attr = sanitize_title_with_dashes( $group_data['group_label'] . '-' . $group_id );

	$group_html  = '<h2 id="' . esc_attr( $group_id_attr ) . '">';
	$group_html .= esc_html( $group_data['group_label'] );

	$items_count = count( (array) $group_data['items'] );
	if ( $items_count > 1 ) {
		$group_html .= sprintf( ' <span class="count">(%d)</span>', $items_count );
	}

	$group_html .= '</h2>';

	if ( ! empty( $group_data['group_description'] ) ) {
		$group_html .= '<p>' . esc_html( $group_data['group_description'] ) . '</p>';
	}

	$group_html .= '<div>';

	foreach ( (array) $group_data['items'] as $group_item_id => $group_item_data ) {
		$group_html .= '<table>';
		$group_html .= '<tbody>';

		foreach ( (array) $group_item_data as $group_item_datum ) {
			$value = $group_item_datum['value'];
			// If it looks like a link, make it a link.
			if ( ! str_contains( $value, ' ' ) && ( str_starts_with( $value, 'http://' ) || str_starts_with( $value, 'https://' ) ) ) {
				$value = '<a href="' . esc_url( $value ) . '">' . esc_html( $value ) . '</a>';
			}

			$group_html .= '<tr>';
			$group_html .= '<th>' . esc_html( $group_item_datum['name'] ) . '</th>';
			$group_html .= '<td>' . wp_kses( $value, 'personal_data_export' ) . '</td>';
			$group_html .= '</tr>';
		}

		$group_html .= '</tbody>';
		$group_html .= '</table>';
	}

	if ( $groups_count > 1 ) {
		$group_html .= '<div class="return-to-top">';
		$group_html .= '<a href="#top"><span aria-hidden="true">↑ </span> ' . esc_html__( 'Go to top' ) . '</a>';
		$group_html .= '</div>';
	}

	$group_html .= '</div>';

	return $group_html;
}

Changelog

VersionDescription
5.4.0Added the $group_id and $groups_count parameters.
4.9.6Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.