add_filter( 'the_content', $this, 'inject_marker', 999 ); } /** * Fetches $post_id's real permalink and detects its content boundary, * cached — this is a real HTTP round-trip, not something to repeat on * every content-analysis call (compute() in ABDS_SEO_Content_Analyzer * runs on every save_post via ABDS_SEO_Content_Score_Cache, and * potentially from a live editor request too). * * @param int $post_id Post to fetch. * @return array{method:string, html:?string, error:string} `error` is * non-empty * only when the * loopback * request * itself failed * (network/ * timeout), * distinct from * a clean * NOT_DETERMINED * (request * succeeded, * neither * detection * method * matched). */ public function fetch( int $post_id ): array { $post = get_post( $post_id ); $cache_key = $this->cache_key( $post_id, $post ); $cached = get_transient( $cache_key ); if ( is_array( $cached ) ) { return $cached; } $result = $this->fetch_uncached( $post_id ); $ttl = ( '' !== $result['error'] && 'no_permalink' !== $result['error'] ) ? self::CHECK_FAILED_TRANSIENT_TTL : self::TRANSIENT_TTL; set_transient( $cache_key, $result, $ttl ); return $result; } /** * Cache key for one post's fetch() result — includes post_modified_gmt * so a post edit invalidates the cache implicitly (a new key, not a * write to the old one) rather than needing a separate save_post * invalidation hook to stay in sync. A post that no longer exists (e.g. * a stale scheduled job for a since-deleted post) still gets a stable, * per-post key via the literal 'deleted' in place of a modified date. * * @param int $post_id Post ID. * @param WP_Post|null $post Post object, or null if it no longer exists. * @return string */ protected function cache_key( int $post_id, ?WP_Post $post ): string { $modified = $post ? $post->post_modified_gmt : 'deleted'; return 'abds_seo_cbf_' . $post_id . '_' . md5( $modified ); } /** * The actual, uncached loopback fetch + detection — see fetch() for the * caching wrapper around this. * * @param int $post_id Post to fetch. * @return array{method:string, html:?string, error:string} */ protected function fetch_uncached( int $post_id ): array { $permalink = get_permalink( $post_id ); if ( false === $permalink ) { return array( 'method' => ABDS_SEO_Content_Boundary_Detector::METHOD_NOT_DETERMINED, 'html' => null, 'error' => 'no_permalink', ); } $url = add_query_arg( array( self::PROBE_QUERY_ARG => '1', 'abds_seo_probe_post' => $post_id, ), $permalink ); $response = wp_remote_get( $url, array( 'timeout' => self::TIMEOUT_SECONDS ) ); if ( is_wp_error( $response ) ) { return array( 'method' => ABDS_SEO_Content_Boundary_Detector::METHOD_NOT_DETERMINED, 'html' => null, 'error' => $this->classify_error( $response ), ); } if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { return array( 'method' => ABDS_SEO_Content_Boundary_Detector::METHOD_NOT_DETERMINED, 'html' => null, 'error' => 'http_' . wp_remote_retrieve_response_code( $response ), ); } $body = (string) wp_remote_retrieve_body( $response ); $result = ( new ABDS_SEO_Content_Boundary_Detector() )->detect( $body, $post_id ); return array( 'method' => $result['method'], 'html' => $result['html'], 'error' => '', ); } /** * Wraps the_content's output in the detector's marker comments — but * ONLY on this plugin's own probe request, and ONLY for the exact post * that request targets. Both guards matter independently: without the * probe-request guard, every real visitor's page would carry an * invisible-but-present HTML comment pair; without the post-id guard, a * theme that queries related/recent posts and calls the_content() on * them elsewhere on the same template would wrap the WRONG post's * content (the same class of collision the detector's own docblock * documents for id="post-{ID}" — a page routinely contains more than * one the_content-rendered post). * * @param string $content Content this filter chain has produced so far. * @return string */ public function inject_marker( $content ) { if ( ! $this->is_probe_request() ) { return $content; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only recursion/target guard on the plugin's own outbound loopback request, not user input driving any state change. $probed_post_id = isset( $_GET['abds_seo_probe_post'] ) ? absint( wp_unslash( $_GET['abds_seo_probe_post'] ) ) : 0; if ( $probed_post_id <= 0 || get_the_ID() !== $probed_post_id ) { return $content; } return ABDS_SEO_Content_Boundary_Detector::MARKER_START . $content . ABDS_SEO_Content_Boundary_Detector::MARKER_END; } /** * Whether the current request is this plugin's own content-boundary * probe, not a real visitor or crawler — mirrors * ABDS_SEO_Sitemap_Controller::is_probe_request() exactly, under a * distinct query arg (see PROBE_QUERY_ARG's own docblock). * * @return bool */ protected function is_probe_request(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only recursion guard on the plugin's own outbound loopback request, not user input driving any state change. return isset( $_GET[ self::PROBE_QUERY_ARG ] ); } /** * Classifies a failed loopback's WP_Error, reusing the same "time" * substring heuristic ABDS_SEO_Sitemap_Controller::classify_probe_error() * documents in full — see that method for why this is the reliable * cross-transport signal rather than a structured error code. * * @param WP_Error $error Failed request's error. * @return string 'timeout' or 'connection'. */ protected function classify_error( WP_Error $error ): string { $message = strtolower( $error->get_error_message() ); return ( false !== strpos( $message, 'time' ) ) ? 'timeout' : 'connection'; } }