public function fetch_items() { $url = $this->get_listing_url(); $dom = rpi_fetch_html( $url ); if ( is_wp_error( $dom ) || ! $dom instanceof DOMDocument ) { rpi_log( 'error', 'LNER: Failed to fetch listing.', array( 'url' => $url, 'error' => is_wp_error( $dom ) ? $dom->get_error_message() : 'Unknown', ) ); return array(); } $xpath = new DOMXPath( $dom ); // LNER articles are wrapped in
blocks $article_nodes = @$xpath->query( '//article' ); rpi_log( 'info', 'LNER: Article nodes found.', array( 'count' => $article_nodes ? $article_nodes->length : 0, ) ); $items = array(); $seen = array(); if ( $article_nodes ) { foreach ( $article_nodes as $article ) { // Headline link is usually inside h2 > a $link = @$xpath->query( './/h2//a', $article )->item( 0 ); if ( ! $link ) { continue; } $href = trim( $link->getAttribute( 'href' ) ); $title = trim( preg_replace( '/\s+/', ' ', $link->textContent ) ); if ( ! $href || ! $title ) { continue; } if ( strpos( $href, 'http' ) !== 0 ) { $href = 'https://news.lner.co.uk' . $href; } if ( isset( $seen[ $href ] ) ) { continue; } $seen[ $href ] = true; $items[] = array( 'title' => $title, 'url' => $href, ); } } rpi_log( 'info', 'LNER: Listing items extracted.', array( 'count' => count( $items ), ) ); return $items; } jaksackjsa-acjqwjnknasc – LW Rail Train Magazine