From f3b4c7680bb56b291e35872697413e11519d7b6f Mon Sep 17 00:00:00 2001 From: Maud Leray Date: Wed, 2 Oct 2024 14:07:25 +0200 Subject: [PATCH] PLANET-7597 Fix Block/Pattern Usage Report links (#2391) There were several issues with them, the post title links were not correct and also the post id is sometimes removed which caused problems. --- .../Block/BlockUsageTable.php | 7 ++++-- .../Pattern/PatternUsageTable.php | 22 +++++++++++++++++-- src/BlockReportSearch/RowActions.php | 21 +++++++++++------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/BlockReportSearch/Block/BlockUsageTable.php b/src/BlockReportSearch/Block/BlockUsageTable.php index 9f0457ffec..4230f80e9c 100644 --- a/src/BlockReportSearch/Block/BlockUsageTable.php +++ b/src/BlockReportSearch/Block/BlockUsageTable.php @@ -516,7 +516,10 @@ public function column_post_title($item): string $title_tpl = '%2$s'; $link_tpl = '%s'; - $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, @@ -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 diff --git a/src/BlockReportSearch/Pattern/PatternUsageTable.php b/src/BlockReportSearch/Pattern/PatternUsageTable.php index d04835a2a1..3cf82978ef 100644 --- a/src/BlockReportSearch/Pattern/PatternUsageTable.php +++ b/src/BlockReportSearch/Pattern/PatternUsageTable.php @@ -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 @@ -328,7 +328,10 @@ public function column_post_title($item): string $title_tpl = '%2$s'; $link_tpl = '%s'; - $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, @@ -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( + '%s', + get_edit_post_link($item['post_id']), + $item['post_id'] + ); + } + /** * Full row display, edited for grouping functionality. * diff --git a/src/BlockReportSearch/RowActions.php b/src/BlockReportSearch/RowActions.php index e3dd17584a..8f4ca58fa0 100644 --- a/src/BlockReportSearch/RowActions.php +++ b/src/BlockReportSearch/RowActions.php @@ -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( '%s', - get_edit_post_link($item['post_id']), + get_edit_post_link($id), /* translators: %s: Post title. */ esc_attr(sprintf(__('Edit “%s”', 'default'), $title)), __('Edit', 'default') @@ -50,7 +55,7 @@ public function get_post_actions(array $item, string $column_name, string $prima } elseif ('trash' !== $item['post_status']) { $actions['view'] = sprintf( '%s', - get_permalink($item['post_id']), + get_permalink($id), /* translators: %s: Post title. */ esc_attr(sprintf(__('View “%s”', 'default'), $title)), __('View', 'default')