WP_Comments_List_Table::handle_row_actions()protectedWP 4.3.0

Generates and displays row actions links.

Метод класса: WP_Comments_List_Table{}

Хуки из метода

Возвращает

Строку. Row actions output for comments. An empty string if the current column is not the primary column, or if the current user cannot edit the comment.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->handle_row_actions( $item, $column_name, $primary );
$item(WP_Comment) (обязательный)
The comment object.
$column_name(строка) (обязательный)
Current column name.
$primary(строка) (обязательный)
Primary column name.

Заметки

  • Global. Строка. $comment_status Status for the current listed comments.

Список изменений

С версии 4.3.0 Введена.
С версии 5.9.0 Renamed $comment to $item to match parent class for PHP 8 named parameter support.

Код WP_Comments_List_Table::handle_row_actions() WP 6.7.1

protected function handle_row_actions( $item, $column_name, $primary ) {
	global $comment_status;

	if ( $primary !== $column_name ) {
		return '';
	}

	if ( ! $this->user_can ) {
		return '';
	}

	// Restores the more descriptive, specific name for use within this method.
	$comment = $item;

	$the_comment_status = wp_get_comment_status( $comment );

	$output = '';

	$approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( 'approve-comment_' . $comment->comment_ID ) );
	$del_nonce     = esc_html( '_wpnonce=' . wp_create_nonce( 'delete-comment_' . $comment->comment_ID ) );

	$action_string = 'comment.php?action=%s&c=' . $comment->comment_ID . '&%s';

	$approve_url   = sprintf( $action_string, 'approvecomment', $approve_nonce );
	$unapprove_url = sprintf( $action_string, 'unapprovecomment', $approve_nonce );
	$spam_url      = sprintf( $action_string, 'spamcomment', $del_nonce );
	$unspam_url    = sprintf( $action_string, 'unspamcomment', $del_nonce );
	$trash_url     = sprintf( $action_string, 'trashcomment', $del_nonce );
	$untrash_url   = sprintf( $action_string, 'untrashcomment', $del_nonce );
	$delete_url    = sprintf( $action_string, 'deletecomment', $del_nonce );

	// Preorder it: Approve | Reply | Quick Edit | Edit | Spam | Trash.
	$actions = array(
		'approve'   => '',
		'unapprove' => '',
		'reply'     => '',
		'quickedit' => '',
		'edit'      => '',
		'spam'      => '',
		'unspam'    => '',
		'trash'     => '',
		'untrash'   => '',
		'delete'    => '',
	);

	// Not looking at all comments.
	if ( $comment_status && 'all' !== $comment_status ) {
		if ( 'approved' === $the_comment_status ) {
			$actions['unapprove'] = sprintf(
				'<a href="%s" data-wp-lists="%s" class="vim-u vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
				esc_url( $unapprove_url ),
				"delete:the-comment-list:comment-{$comment->comment_ID}:e7e7d3:action=dim-comment&amp;new=unapproved",
				esc_attr__( 'Unapprove this comment' ),
				__( 'Unapprove' )
			);
		} elseif ( 'unapproved' === $the_comment_status ) {
			$actions['approve'] = sprintf(
				'<a href="%s" data-wp-lists="%s" class="vim-a vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
				esc_url( $approve_url ),
				"delete:the-comment-list:comment-{$comment->comment_ID}:e7e7d3:action=dim-comment&amp;new=approved",
				esc_attr__( 'Approve this comment' ),
				__( 'Approve' )
			);
		}
	} else {
		$actions['approve'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="vim-a aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $approve_url ),
			"dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved",
			esc_attr__( 'Approve this comment' ),
			__( 'Approve' )
		);

		$actions['unapprove'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="vim-u aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $unapprove_url ),
			"dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved",
			esc_attr__( 'Unapprove this comment' ),
			__( 'Unapprove' )
		);
	}

	if ( 'spam' !== $the_comment_status ) {
		$actions['spam'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="vim-s vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $spam_url ),
			"delete:the-comment-list:comment-{$comment->comment_ID}::spam=1",
			esc_attr__( 'Mark this comment as spam' ),
			/* translators: "Mark as spam" link. */
			_x( 'Spam', 'verb' )
		);
	} elseif ( 'spam' === $the_comment_status ) {
		$actions['unspam'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $unspam_url ),
			"delete:the-comment-list:comment-{$comment->comment_ID}:66cc66:unspam=1",
			esc_attr__( 'Restore this comment from the spam' ),
			_x( 'Not Spam', 'comment' )
		);
	}

	if ( 'trash' === $the_comment_status ) {
		$actions['untrash'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $untrash_url ),
			"delete:the-comment-list:comment-{$comment->comment_ID}:66cc66:untrash=1",
			esc_attr__( 'Restore this comment from the Trash' ),
			__( 'Restore' )
		);
	}

	if ( 'spam' === $the_comment_status || 'trash' === $the_comment_status || ! EMPTY_TRASH_DAYS ) {
		$actions['delete'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $delete_url ),
			"delete:the-comment-list:comment-{$comment->comment_ID}::delete=1",
			esc_attr__( 'Delete this comment permanently' ),
			__( 'Delete Permanently' )
		);
	} else {
		$actions['trash'] = sprintf(
			'<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
			esc_url( $trash_url ),
			"delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
			esc_attr__( 'Move this comment to the Trash' ),
			_x( 'Trash', 'verb' )
		);
	}

	if ( 'spam' !== $the_comment_status && 'trash' !== $the_comment_status ) {
		$actions['edit'] = sprintf(
			'<a href="%s" aria-label="%s">%s</a>',
			"comment.php?action=editcomment&amp;c={$comment->comment_ID}",
			esc_attr__( 'Edit this comment' ),
			__( 'Edit' )
		);

		$format = '<button type="button" data-comment-id="%d" data-post-id="%d" data-action="%s" class="%s button-link" aria-expanded="false" aria-label="%s">%s</button>';

		$actions['quickedit'] = sprintf(
			$format,
			$comment->comment_ID,
			$comment->comment_post_ID,
			'edit',
			'vim-q comment-inline',
			esc_attr__( 'Quick edit this comment inline' ),
			__( 'Quick&nbsp;Edit' )
		);

		$actions['reply'] = sprintf(
			$format,
			$comment->comment_ID,
			$comment->comment_post_ID,
			'replyto',
			'vim-r comment-inline',
			esc_attr__( 'Reply to this comment' ),
			__( 'Reply' )
		);
	}

	/**
	 * Filters the action links displayed for each comment in the Comments list table.
	 *
	 * @since 2.6.0
	 *
	 * @param string[]   $actions An array of comment actions. Default actions include:
	 *                            'Approve', 'Unapprove', 'Edit', 'Reply', 'Spam',
	 *                            'Delete', and 'Trash'.
	 * @param WP_Comment $comment The comment object.
	 */
	$actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );

	$always_visible = false;

	$mode = get_user_setting( 'posts_list_mode', 'list' );

	if ( 'excerpt' === $mode ) {
		$always_visible = true;
	}

	$output .= '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';

	$i = 0;

	foreach ( $actions as $action => $link ) {
		++$i;

		if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i )
			|| 1 === $i
		) {
			$separator = '';
		} else {
			$separator = ' | ';
		}

		// Reply and quickedit need a hide-if-no-js span when not added with Ajax.
		if ( ( 'reply' === $action || 'quickedit' === $action ) && ! wp_doing_ajax() ) {
			$action .= ' hide-if-no-js';
		} elseif ( ( 'untrash' === $action && 'trash' === $the_comment_status )
			|| ( 'unspam' === $action && 'spam' === $the_comment_status )
		) {
			if ( '1' === get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) ) {
				$action .= ' approve';
			} else {
				$action .= ' unapprove';
			}
		}

		$output .= "<span class='$action'>{$separator}{$link}</span>";
	}

	$output .= '</div>';

	$output .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' .
		/* translators: Hidden accessibility text. */
		__( 'Show more details' ) .
	'</span></button>';

	return $output;
}