*/ public function present( Link $link ): array { $detail = $this->link_presenter->present( $link ); $detail['uses'] = $this->uses_of( $link->id ); $detail['suggested_url'] = $this->suggested_url_for( $link ); return $detail; } private function suggested_url_for( Link $link ): string|null { if ( Verdict::Moved === $link->verdict ) { return $link->final_url; } if ( $link->internal && Verdict::Broken === $link->verdict ) { return $this->closest_page_finder->closest_to( $link->url ); } return null; } /** @return list> */ private function uses_of( int $link_id ): array { $rows = $this->query_runner->rows( $this->query_runner->prepare( 'SELECT post_id, text, before_text, after_text FROM ' . $this->tables->uses() . ' WHERE link_id = %d ORDER BY id LIMIT %d', $link_id, self::SHOWN_USES ) ); $uses = array(); foreach ( $rows as $row ) { $uses[] = $this->use_from( $row, $link_id ); } return $uses; } /** * @param array $row * @return array */ private function use_from( array $row, int $link_id ): array { $post_id = RowColumns::whole_number( $row, 'post_id' ); $post = get_post( $post_id ); $view_url = $this->place_address->of( $post_id, $link_id ); return array( 'post_id' => $post_id, 'post_title' => $this->post_title( $post ), 'post_type_label' => $this->post_type_label( $post ), 'edit_url' => get_edit_post_link( $post_id, 'raw' ), 'view_url' => $view_url, 'builder' => $this->page_builders->name_of( $post_id ), 'text' => RowColumns::text( $row, 'text' ), 'before' => RowColumns::text( $row, 'before_text' ), 'after' => RowColumns::text( $row, 'after_text' ), ); } private function post_title( WP_Post|null $post ): string { if ( null === $post ) { return ''; } return trim( $this->plain_text->from_html( $post->post_title ) ); } private function post_type_label( WP_Post|null $post ): string { if ( null === $post ) { return ''; } $type = get_post_type_object( $post->post_type ); if ( null === $type ) { return ''; } $label = $type->labels->singular_name ?? null; return is_string( $label ) ? $label : ''; } }