tag, schema): answer once. // Never memoized where posts change during the request (admin, cron, REST, CLI). $memo = ! is_admin() && ! wp_doing_cron() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && ! ( defined( 'WP_CLI' ) && WP_CLI ); $key = $post->ID . '|' . $surface . '|' . get_current_user_id(); if ( $memo && isset( self::$public[ $key ] ) ) { return self::$public[ $key ]; } $ok = true; if ( 'publish' !== $post->post_status || '' !== $post->post_password ) { $ok = false; } $type = get_post_type_object( $post->post_type ); if ( $ok && ( ! $type || ! $type->public || 'attachment' === $post->post_type ) ) { $ok = false; } if ( $ok && in_array( $post->post_type, self::gated_post_types(), true ) ) { $ok = false; } if ( $ok && get_post_meta( $post->ID, '_aeoora_exclude', true ) ) { $ok = false; } if ( $ok && 'audit' !== $surface && Aeoora_Seo::is_noindex( $post->ID ) ) { $ok = false; } if ( $ok && ! self::passes_membership_checks( $post ) ) { $ok = false; } $ok = (bool) apply_filters( 'aeoora_content_is_public', $ok, $post, $surface ); if ( $memo ) { self::$public[ $key ] = $ok; } return $ok; } /** * Ask the membership plugins that expose an access API. * * @param WP_Post $post Post. * @return bool */ private static function passes_membership_checks( WP_Post $post ) { try { if ( function_exists( 'pmpro_has_membership_access' ) ) { $access = pmpro_has_membership_access( $post->ID, 0, false ); if ( ! $access ) { return false; } } if ( function_exists( 'wc_memberships_is_post_content_restricted' ) && wc_memberships_is_post_content_restricted( $post->ID ) ) { return false; } if ( function_exists( 'rcp_is_restricted_content' ) && rcp_is_restricted_content( $post->ID ) ) { return false; } if ( class_exists( 'MeprRule' ) && method_exists( 'MeprRule', 'is_locked' ) && MeprRule::is_locked( $post ) ) { return false; } if ( function_exists( 'members_can_user_view_post' ) && ! members_can_user_view_post( 0, $post->ID ) ) { return false; } if ( function_exists( 'sensei_can_user_view_lesson' ) && 'lesson' === $post->post_type && ! sensei_can_user_view_lesson( $post->ID, 0 ) ) { return false; } } catch ( Throwable $e ) { return false; } return true; } /** * Does the post use a page builder whose output lives outside post_content? * * @param int $post_id Post ID. * @return string Builder slug or ''. */ public static function builder( $post_id ) { if ( 'builder' === get_post_meta( $post_id, '_elementor_edit_mode', true ) && did_action( 'elementor/loaded' ) ) { return 'elementor'; } if ( get_post_meta( $post_id, '_fl_builder_enabled', true ) && class_exists( 'FLBuilder' ) ) { return 'beaver'; } if ( get_post_meta( $post_id, '_bricks_page_content_2', true ) && defined( 'BRICKS_VERSION' ) ) { return 'bricks'; } if ( 'on' === get_post_meta( $post_id, '_et_pb_use_builder', true ) ) { return 'divi'; } if ( get_post_meta( $post_id, 'ct_builder_shortcodes', true ) || get_post_meta( $post_id, '_ct_builder_json', true ) ) { return 'oxygen'; } if ( get_post_meta( $post_id, '_breakdance_data', true ) ) { return 'breakdance'; } if ( get_post_meta( $post_id, 'brizy', true ) ) { return 'brizy'; } if ( get_post_meta( $post_id, 'tve_updated_post', true ) ) { return 'thrive'; } return ''; } /** * Rendered HTML of a post's main content. * * @param WP_Post|int $post Post. * @param string|null $mode auto|content|rendered (null = setting). * @return string */ public static function render_html( $post, $mode = null ) { $post = get_post( $post ); if ( ! $post ) { return ''; } $mode = null === $mode ? Aeoora_Settings::get( 'markdown', 'render', 'auto' ) : $mode; $builder = self::builder( $post->ID ); $html = ''; if ( 'rendered' !== $mode ) { $html = self::render_with_filters( $post, $builder ); } $needs_fallback = 'rendered' === $mode || ( 'auto' === $mode && in_array( $builder, array( 'oxygen', 'breakdance', 'brizy', 'thrive', 'divi' ), true ) ) || ( 'auto' === $mode && Aeoora_Util::word_count( wp_strip_all_tags( $html ) ) < 40 && strlen( $post->post_content ) > 400 ); if ( $needs_fallback ) { // Never block a visitor-/bot-triggered request on a loopback: use the // cached render if there is one and warm it in the background otherwise. // Batch jobs never wait on loopbacks either (one per page could take minutes). $sync = ! self::$batch && ( wp_doing_cron() || is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ); $rendered = $sync ? self::fetch_rendered( get_permalink( $post ) ) : self::cached_render( $post ); if ( Aeoora_Util::word_count( wp_strip_all_tags( $rendered ) ) > Aeoora_Util::word_count( wp_strip_all_tags( $html ) ) ) { $html = $rendered; } } return (string) apply_filters( 'aeoora_rendered_content', $html, $post ); } /** * Render through the_content with full post context, as a logged-out visitor. * * @param WP_Post $post Post. * @param string $builder Builder slug. * @return string */ private static function render_with_filters( WP_Post $post, $builder ) { global $wp_query; $prev_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null; $prev_user = get_current_user_id(); $prev_query = $wp_query; $switch = $prev_user && ! self::is_main_query_for( $post ); self::$rendering = true; if ( $switch ) { wp_set_current_user( 0 ); } $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited setup_postdata( $post ); $html = ''; try { switch ( $builder ) { case 'elementor': if ( class_exists( '\Elementor\Plugin' ) && isset( \Elementor\Plugin::$instance->frontend ) ) { $html = \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $post->ID, false ); } break; case 'beaver': if ( class_exists( 'FLBuilder' ) && method_exists( 'FLBuilder', 'render_content_by_id' ) ) { ob_start(); FLBuilder::render_content_by_id( $post->ID ); $html = ob_get_clean(); } break; case 'bricks': if ( class_exists( '\Bricks\Frontend' ) && class_exists( '\Bricks\Database' ) ) { $data = get_post_meta( $post->ID, '_bricks_page_content_2', true ); if ( is_array( $data ) && method_exists( '\Bricks\Frontend', 'render_data' ) ) { $html = \Bricks\Frontend::render_data( $data ); } } break; } if ( '' === trim( wp_strip_all_tags( (string) $html ) ) ) { $content = $post->post_content; $html = apply_filters( 'the_content', $content ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals } } catch ( Throwable $e ) { $html = wpautop( $post->post_content ); } // Leftover unprocessed shortcodes (builder not loaded in this context). $html = preg_replace( '~\[/?[a-zA-Z][\w-]*(?:\s[^\]]*)?\]~', ' ', (string) $html ); self::$rendering = false; if ( $switch ) { wp_set_current_user( $prev_user ); } $GLOBALS['post'] = $prev_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited if ( $prev_post instanceof WP_Post ) { setup_postdata( $prev_post ); } $wp_query = $prev_query; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited return (string) $html; } /** * Fetch the real page as an anonymous visitor and extract its main content. * * @param string $url URL. * @return string HTML of the main content area. */ public static function fetch_rendered( $url ) { if ( ! $url ) { return ''; } $cached = Aeoora_Cache::get( 'render', $url ); if ( null !== $cached ) { return (string) $cached; } $res = wp_remote_get( $url, Aeoora_Util::loopback_args( array( 'timeout' => 8 ) ) ); if ( is_wp_error( $res ) || 200 !== (int) wp_remote_retrieve_response_code( $res ) ) { // Remember failures too, so hosts without loopbacks are not hammered. Aeoora_Cache::set( 'render', $url, '', HOUR_IN_SECONDS ); update_option( 'aeoora_loopback_ok', 0, false ); return ''; } update_option( 'aeoora_loopback_ok', 1, false ); $html = self::extract_main( (string) wp_remote_retrieve_body( $res ) ); Aeoora_Cache::set( 'render', $url, $html, 6 * HOUR_IN_SECONDS ); return $html; } /** * Cached rendered HTML, or '' after queueing a background warm-up. * * @param WP_Post $post Post. * @return string */ public static function cached_render( WP_Post $post ) { $url = get_permalink( $post ); $cached = $url ? Aeoora_Cache::get( 'render', $url ) : null; if ( null !== $cached ) { return (string) $cached; } Aeoora_Queue::push( 'render', (int) $post->ID ); return ''; } /** * Background: render a page through a loopback and refresh its Markdown * cache (and its AEO score, now that the full content is known). * * @param int $post_id Post ID. */ public static function warm_render( $post_id ) { $url = get_permalink( (int) $post_id ); if ( $url ) { self::fetch_rendered( $url ); Aeoora_Markdown::flush_post( (int) $post_id, false ); if ( '' !== (string) get_post_meta( (int) $post_id, '_aeoora_score', true ) ) { Aeoora_Queue::push( 'score', (int) $post_id ); } } } /** * Run a batch job (llms-full.txt, bulk scans) without synchronous * loopbacks: pages that need one use the cached render and are queued. * * @param callable $callback Callback. * @return mixed Callback result. */ public static function in_batch( $callback ) { $prev = self::$batch; self::$batch = true; try { return call_user_func( $callback ); } finally { self::$batch = $prev; } } /** * Extract the main content of a full HTML document. * * @param string $html Full document. * @return string */ public static function extract_main( $html ) { if ( '' === $html || ! class_exists( 'DOMDocument' ) ) { return ''; } $doc = new DOMDocument(); $prev = libxml_use_internal_errors( true ); $doc->loadHTML( '' . $html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_PARSEHUGE ); libxml_clear_errors(); libxml_use_internal_errors( $prev ); $xpath = new DOMXPath( $doc ); // Remove chrome that is never content. foreach ( array( '//script', '//style', '//noscript', '//header', '//footer', '//nav', '//aside', '//form', '//*[@role="navigation"]', '//*[@role="banner"]', '//*[@role="contentinfo"]', '//*[contains(concat(" ",normalize-space(@class)," ")," comments-area ")]', '//*[@id="comments"]' ) as $q ) { $nodes = $xpath->query( $q ); if ( $nodes ) { foreach ( iterator_to_array( $nodes ) as $n ) { if ( $n->parentNode ) { $n->parentNode->removeChild( $n ); } } } } $candidates = array( '//*[contains(concat(" ",normalize-space(@class)," ")," entry-content ")]', '//*[contains(concat(" ",normalize-space(@class)," ")," post-content ")]', '//*[contains(concat(" ",normalize-space(@class)," ")," elementor-widget-theme-post-content ")]', '//*[@data-elementor-type="wp-post" or @data-elementor-type="wp-page" or @data-elementor-type="single-post" or @data-elementor-type="single-page"]', '//article', '//main', '//*[@role="main"]', '//*[@id="content"]', '//body', ); foreach ( $candidates as $q ) { $nodes = $xpath->query( $q ); if ( $nodes && $nodes->length ) { // Pick the candidate with most text. $best = null; $max = 0; foreach ( $nodes as $n ) { $len = strlen( trim( $n->textContent ) ); if ( $len > $max ) { $max = $len; $best = $n; } } if ( $best && $max > 200 ) { $out = ''; foreach ( $best->childNodes as $c ) { $out .= $doc->saveHTML( $c ); } return $out; } } } return ''; } /** * Is the main query currently showing this very post (so the visitor's own * access already applies and we must not switch users)? * * @param WP_Post $post Post. * @return bool */ private static function is_main_query_for( WP_Post $post ) { return function_exists( 'is_singular' ) && did_action( 'wp' ) && is_singular() && (int) get_queried_object_id() === (int) $post->ID; } }