函数文档

_list_meta_row()

💡 云策文档标注

概述

_list_meta_row() 函数用于在自定义字段元框中输出单行公共元数据。它处理元数据的序列化检查、转义和安全验证,并生成包含键、值、删除和更新按钮的 HTML 行。

关键要点

  • 函数输出单行公共元数据,适用于自定义字段元框的显示。
  • 参数包括 $entry(元数据数组,键为 'meta_key' 和 'meta_value')和 $count(行数引用)。
  • 返回值为字符串,表示单行 HTML 内容。
  • 函数会检查元键是否受保护(使用 is_protected_meta()),若受保护则返回空字符串。
  • 处理序列化数据:如果是序列化字符串,则反序列化显示;如果是序列化数组/对象,则不显示并减少计数。
  • 对元键和元值进行转义(esc_attr() 和 esc_textarea()),确保安全性。
  • 生成删除和更新按钮(使用 get_submit_button()),并包含 nonce 字段(wp_nonce_field())用于 AJAX 操作。
  • 相关函数包括 get_submit_button()、esc_textarea()、wp_nonce_field() 等,用于辅助功能实现。

代码示例

function _list_meta_row( $entry, &$count ) {
    static $update_nonce = '';

    if ( is_protected_meta( $entry['meta_key'], 'post' ) ) {
        return '';
    }

    if ( ! $update_nonce ) {
        $update_nonce = wp_create_nonce( 'add-meta' );
    }

    $r = '';
    ++$count;

    if ( is_serialized( $entry['meta_value'] ) ) {
        if ( is_serialized_string( $entry['meta_value'] ) ) {
            // This is a serialized string, so we should display it.
            $entry['meta_value'] = maybe_unserialize( $entry['meta_value'] );
        } else {
            // This is a serialized array/object so we should NOT display it.
            --$count;
            return '';
        }
    }

    $entry['meta_key']   = esc_attr( $entry['meta_key'] );
    $entry['meta_value'] = esc_textarea( $entry['meta_value'] ); // Using a .
    $entry['meta_id']    = (int) $entry['meta_id'];

    $delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );

    $r .= "nt";
    $r .= "ntt" .
        /* translators: Hidden accessibility text. */
        __( 'Key' ) .
    "";

    $r .= "ntt";
    $r .= get_submit_button( __( 'Delete' ), 'deletemeta small', "deletemeta[{$entry['meta_id']}]", false, array( 'data-wp-lists' => "delete:the-list:meta-{$entry['meta_id']}::_ajax_nonce=$delete_nonce" ) );
    $r .= "ntt";
    $r .= get_submit_button( __( 'Update' ), 'updatemeta small', "meta-{$entry['meta_id']}-submit", false, array( 'data-wp-lists' => "add:the-list:meta-{$entry['meta_id']}::_ajax_nonce-add-meta=$update_nonce" ) );
    $r .= '';
    $r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
    $r .= '';

    $r .= "ntt" .
        /* translators: Hidden accessibility text. */
        __( 'Value' ) .
    "{$entry['meta_value']}nt";
    return $r;
}

📄 原文内容

Outputs a single row of public meta data in the Custom Fields meta box.

Parameters

$entryarrayrequired
An array of meta data keyed on 'meta_key' and 'meta_value'.
$countintrequired
Reference to the row number.

Return

string A single row of public meta data.

Source

function _list_meta_row( $entry, &$count ) {
	static $update_nonce = '';

	if ( is_protected_meta( $entry['meta_key'], 'post' ) ) {
		return '';
	}

	if ( ! $update_nonce ) {
		$update_nonce = wp_create_nonce( 'add-meta' );
	}

	$r = '';
	++$count;

	if ( is_serialized( $entry['meta_value'] ) ) {
		if ( is_serialized_string( $entry['meta_value'] ) ) {
			// This is a serialized string, so we should display it.
			$entry['meta_value'] = maybe_unserialize( $entry['meta_value'] );
		} else {
			// This is a serialized array/object so we should NOT display it.
			--$count;
			return '';
		}
	}

	$entry['meta_key']   = esc_attr( $entry['meta_key'] );
	$entry['meta_value'] = esc_textarea( $entry['meta_value'] ); // Using a <textarea />.
	$entry['meta_id']    = (int) $entry['meta_id'];

	$delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );

	$r .= "nt<tr id='meta-{$entry['meta_id']}'>";
	$r .= "ntt<td class='left'><label class='screen-reader-text' for='meta-{$entry['meta_id']}-key'>" .
		/* translators: Hidden accessibility text. */
		__( 'Key' ) .
	"</label><input name='meta[{$entry['meta_id']}][key]' id='meta-{$entry['meta_id']}-key' type='text' size='20' value='{$entry['meta_key']}' />";

	$r .= "ntt<div class='submit'>";
	$r .= get_submit_button( __( 'Delete' ), 'deletemeta small', "deletemeta[{$entry['meta_id']}]", false, array( 'data-wp-lists' => "delete:the-list:meta-{$entry['meta_id']}::_ajax_nonce=$delete_nonce" ) );
	$r .= "ntt";
	$r .= get_submit_button( __( 'Update' ), 'updatemeta small', "meta-{$entry['meta_id']}-submit", false, array( 'data-wp-lists' => "add:the-list:meta-{$entry['meta_id']}::_ajax_nonce-add-meta=$update_nonce" ) );
	$r .= '</div>';
	$r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
	$r .= '</td>';

	$r .= "ntt<td><label class='screen-reader-text' for='meta-{$entry['meta_id']}-value'>" .
		/* translators: Hidden accessibility text. */
		__( 'Value' ) .
	"</label><textarea name='meta[{$entry['meta_id']}][value]' id='meta-{$entry['meta_id']}-value' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>nt</tr>";
	return $r;
}

Changelog

Version Description
2.5.0 Introduced.