Skip to content

Commit

Permalink
PLANET-7597 Fix Block/Pattern Usage Report links (#2391)
Browse files Browse the repository at this point in the history
There were several issues with them, the post title links were not correct and also the post id is sometimes removed which caused problems.
  • Loading branch information
mleray authored Oct 2, 2024
1 parent 1326dec commit f3b4c76
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/BlockReportSearch/Block/BlockUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,10 @@ public function column_post_title($item): string

$title_tpl = '%2$s';
$link_tpl = '<a href="%s" title="%s">%s</a>';
$page_uri = get_page_uri($item['post_id']);
// The post id may be empty if we removed it when creating the rows (see single_row function).
$post_id = empty($item['post_id']) ? $this->latest_row : $item['post_id'];
// If the post is still a draft we don't want to show a link in the title column.
$page_uri = $item['post_status'] === 'draft' ? '' : get_permalink($post_id);

return sprintf(
empty($page_uri) ? $title_tpl : $link_tpl,
Expand Down Expand Up @@ -580,7 +583,7 @@ public function single_row($item): void
protected function handle_row_actions($item, $column_name, $primary)
{
return $this->row_actions(
( new RowActions() )->get_post_actions($item, $column_name, $primary)
( new RowActions() )->get_post_actions($item, $column_name, $primary, $this->latest_row)
);
}
// phpcs:enable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint
Expand Down
22 changes: 20 additions & 2 deletions src/BlockReportSearch/Pattern/PatternUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected function display_tablenav($which): void
protected function handle_row_actions($item, $column_name, $primary)
{
return $this->row_actions(
( new RowActions() )->get_post_actions($item, $column_name, $primary)
( new RowActions() )->get_post_actions($item, $column_name, $primary, $this->latest_row)
);
}
// phpcs:enable SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint, SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
Expand Down Expand Up @@ -328,7 +328,10 @@ public function column_post_title($item): string

$title_tpl = '%2$s';
$link_tpl = '<a href="%s" title="%s">%s</a>';
$page_uri = get_page_uri($item['post_id']);
// The post id may be empty if we removed it when creating the rows (see single_row function).
$post_id = empty($item['post_id']) ? $this->latest_row : $item['post_id'];
// If the post is still a draft we don't want to show a link in the title column.
$page_uri = $item['post_status'] === 'draft' ? '' : get_permalink($post_id);

return sprintf(
empty($page_uri) ? $title_tpl : $link_tpl,
Expand All @@ -338,6 +341,21 @@ public function column_post_title($item): string
);
}

/**
* Post ID display.
*
* @param array $item Item.
* @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
public function column_post_id($item): string
{
return sprintf(
'<a href="%s">%s</a>',
get_edit_post_link($item['post_id']),
$item['post_id']
);
}

/**
* Full row display, edited for grouping functionality.
*
Expand Down
21 changes: 13 additions & 8 deletions src/BlockReportSearch/RowActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,30 @@ class RowActions
/**
* Add action links to a row
*
* @param array $item Item.
* @param string $column_name Current column name.
* @param string $primary Primary column name.
* @param array $item Item.
* @param string $column_name Current column name.
* @param string $primary Primary column name.
* @param string $potential_post_id Post id if the rows are grouped by id.
*
* phpcs:disable WordPress.WP.I18n.TextDomainMismatch
*/
public function get_post_actions(array $item, string $column_name, string $primary): array
{
public function get_post_actions(
array $item,
string $column_name,
string $primary,
string $potential_post_id
): array {
if ($column_name !== $primary) {
return [];
}

$id = (int) $item['post_id'];
$id = empty($item['post_id']) ? (int) $potential_post_id : (int) $item['post_id'];
$title = $item['post_title'];
$actions = [];

$actions['edit'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
get_edit_post_link($item['post_id']),
get_edit_post_link($id),
/* translators: %s: Post title. */
esc_attr(sprintf(__('Edit &#8220;%s&#8221;', 'default'), $title)),
__('Edit', 'default')
Expand All @@ -50,7 +55,7 @@ public function get_post_actions(array $item, string $column_name, string $prima
} elseif ('trash' !== $item['post_status']) {
$actions['view'] = sprintf(
'<a href="%s" rel="bookmark" aria-label="%s">%s</a>',
get_permalink($item['post_id']),
get_permalink($id),
/* translators: %s: Post title. */
esc_attr(sprintf(__('View &#8220;%s&#8221;', 'default'), $title)),
__('View', 'default')
Expand Down

0 comments on commit f3b4c76

Please sign in to comment.