` with no post-ID-bearing id attribute * anywhere in the single-post template at all. `entry-content` itself IS * present in both (the FSE theme's block deliberately carries it * alongside its own `wp-block-post-content` class), making it the more * reliable of the two signals across the classic/block-theme split, * not merely an alternative. Only trusted when EXACTLY ONE element on * the page carries it — the same discipline id="post-{ID}" already * needs (a "you might also like" section built from a Query Loop block * can render more than one `.entry-content`-classed element on one * page); on any other count this method defers to POST_ID_WRAPPER * rather than guessing which match is the real one. * 3. POST_ID_WRAPPER — an element carrying `id="post-{$post_id}"` (the * classic-theme `post_class()` convention, confirmed present and * precise — not just "any
" — on a real site: oladance.kyiv.ua's * homepage has SIX elements matching `article[id^="post-"]`, five of them * unrelated "recent posts" teaser cards; only the exact post ID, * resolved via DOMXPath rather than string search, tells them apart). * Kept as a second fallback (not the only id-based method removed) for a * theme that carries an id but not an `entry-content` class. * 4. Neither found → NOT_DETERMINED. The analysis this feeds must refuse to * score rather than fall back to scanning the whole page (header, nav, * footer included) — the same "third state, not a silent zero or a false * claim" principle as 1.4's sitemap probe. * * @package ABDS_SEO */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Parses a fetched HTML page down to its content region. */ class ABDS_SEO_Content_Boundary_Detector { const METHOD_MARKER = 'marker'; const METHOD_CONTENT_CLASS = 'content_class'; const METHOD_POST_ID_WRAPPER = 'post_id_wrapper'; const METHOD_NOT_DETERMINED = 'not_determined'; /** * HTML comment pair ABDS_SEO_Content_Boundary_Fetcher's the_content * filter wraps around its output, requested only on the plugin's own * probe request (see that class) — never present in a real visitor's * page, so no risk of an unrelated site coincidentally containing them. */ const MARKER_START = ''; const MARKER_END = ''; /** * Finds the content region in a fetched page's raw HTML. * * @param string $html Full page HTML, as fetched. * @param int $post_id The post this page is expected to be showing. * @return array{method:string, html:?string} `html` is the content * region's own inner markup, * or null when METHOD_NOT_DETERMINED. */ public function detect( string $html, int $post_id ): array { $via_marker = $this->find_via_marker( $html ); if ( null !== $via_marker ) { return array( 'method' => self::METHOD_MARKER, 'html' => $via_marker, ); } $via_class = $this->find_via_content_class( $html ); if ( null !== $via_class ) { return array( 'method' => self::METHOD_CONTENT_CLASS, 'html' => $via_class, ); } $via_id = $this->find_via_post_id_wrapper( $html, $post_id ); if ( null !== $via_id ) { return array( 'method' => self::METHOD_POST_ID_WRAPPER, 'html' => $via_id, ); } return array( 'method' => self::METHOD_NOT_DETERMINED, 'html' => null, ); } /** * Method 2: the single element carrying an exact `entry-content` class * token — see this class's own docblock for why this is tried before * the id-based method. "Exact token" (padded-space contains(), not a * bare substring contains()) so an unrelated class like * `wp-block-post-content-is-layout-constrained` can't accidentally * satisfy the match. Returns null (defer to the next method) on zero OR * more than one match — a page with a second `.entry-content`-classed * element (a related-posts block, say) is exactly the ambiguous case * this method must not guess through. * * @param string $html Full page HTML. * @return string|null */ protected function find_via_content_class( string $html ): ?string { if ( '' === trim( $html ) ) { return null; } $previous_state = libxml_use_internal_errors( true ); $dom = new DOMDocument(); $loaded = $dom->loadHTML( '' . $html, LIBXML_NOERROR | LIBXML_NOWARNING ); libxml_clear_errors(); libxml_use_internal_errors( $previous_state ); if ( ! $loaded ) { return null; } $xpath = new DOMXPath( $dom ); $nodes = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' entry-content ')]" ); if ( false === $nodes || 1 !== $nodes->length ) { return null; } $node_html = $dom->saveHTML( $nodes->item( 0 ) ); return false !== $node_html ? $node_html : null; } /** * Method 1: the exact substring between the marker comments, if both * are present and correctly ordered. Deliberately a plain substring * search, not DOMDocument — the markers are inserted as literal, * unambiguous text by our own filter, so a full HTML parse buys nothing * here and risks a parser "helpfully" relocating an HTML comment that * happens to sit at an awkward point in a malformed tree. * * @param string $html Full page HTML. * @return string|null */ protected function find_via_marker( string $html ): ?string { $start = strpos( $html, self::MARKER_START ); if ( false === $start ) { return null; } $content_start = $start + strlen( self::MARKER_START ); $end = strpos( $html, self::MARKER_END, $content_start ); if ( false === $end || $end < $content_start ) { return null; } return substr( $html, $content_start, $end - $content_start ); } /** * Method 2: the outer HTML of the element whose `id` attribute is * exactly `post-{$post_id}` — via DOMXPath, not a regex/string search, * per the standing P3 requirement (this is exactly the kind of * attribute match a regex gets subtly wrong on real-world malformed * HTML). `libxml_use_internal_errors` is used deliberately: real pages * routinely have HTML that isn't strictly valid (unclosed tags from a * theme, third-party embeds), and DOMDocument's own warnings for that * are not actionable here — only whether the target element was found. * * @param string $html Full page HTML. * @param int $post_id Target post ID. * @return string|null */ protected function find_via_post_id_wrapper( string $html, int $post_id ): ?string { if ( '' === trim( $html ) ) { return null; } $previous_state = libxml_use_internal_errors( true ); $dom = new DOMDocument(); // UTF-8 declaration prefix: DOMDocument::loadHTML() otherwise // mis-detects encoding from the byte stream alone and can mangle // non-ASCII text (Cyrillic headings/paragraphs, routine on the real // sites this feature exists for). $loaded = $dom->loadHTML( '' . $html, LIBXML_NOERROR | LIBXML_NOWARNING ); libxml_clear_errors(); libxml_use_internal_errors( $previous_state ); if ( ! $loaded ) { return null; } $xpath = new DOMXPath( $dom ); $nodes = $xpath->query( '//*[@id="post-' . (int) $post_id . '"]' ); if ( false === $nodes || 0 === $nodes->length ) { return null; } // First match: the id is expected to be unique per valid HTML (and // per WordPress's own post_class()/id convention) — a second match // would itself be a markup defect on the site, not something this // detector should try to disambiguate further. $node = $nodes->item( 0 ); $html = $dom->saveHTML( $node ); return false !== $html ? $html : null; } }