logger = $logger; $this->renderer = $renderer; } protected $did_special_injection = []; public function init() { add_filter( 'the_content', [ $this, 'inject_related_content' ], 25 ); } protected function is_enabled_for_post( $post_id ) { if ( is_admin() ) { return false; } if ( ! is_singular() ) { return false; } $post = get_post( $post_id ); if ( ! $post ) { return false; } if ( 'publish' !== $post->post_status ) { return false; } $enabled_types = AIT_AIRCE_Settings::get_enabled_post_types(); if ( empty( $enabled_types ) || ! in_array( $post->post_type, $enabled_types, true ) ) { return false; } $disabled = get_post_meta( $post_id, '_ait_airce_disable', true ); if ( $disabled ) { return false; } return true; } protected function get_selected_location( $post_id ) { $location = strval( AIT_AIRCE_Settings::get( 'related_location' ) ); return (string) apply_filters( 'ait_airce/selected_location', $location, $post_id ); } protected function get_related_html( $post_id, $location = '' ) { if ( '' === $location ) { $location = $this->get_selected_location( $post_id ); } return $this->renderer->render( $post_id, [ 'context_key' => 'default', 'location' => $location, ] ); } protected function mark_special_injection_done( $post_id ) { $this->did_special_injection[ (int) $post_id ] = true; } protected function has_special_injection_run( $post_id ) { return ! empty( $this->did_special_injection[ (int) $post_id ] ); } public function render_product_fallback() { if ( ! function_exists( 'is_product' ) || ! is_product() ) { return; } $post_id = (int) get_the_ID(); if ( ! $post_id || ! $this->is_enabled_for_post( $post_id ) || $this->has_special_injection_run( $post_id ) ) { return; } $html = $this->get_related_html( $post_id, $this->get_selected_location( $post_id ) ); if ( '' === $html ) { return; } $this->mark_special_injection_done( $post_id ); echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public function render_events_fallback() { if ( ! is_singular( 'tribe_events' ) ) { return; } $post_id = (int) get_the_ID(); if ( ! $post_id || ! $this->is_enabled_for_post( $post_id ) || $this->has_special_injection_run( $post_id ) ) { return; } $html = $this->get_related_html( $post_id, $this->get_selected_location( $post_id ) ); if ( '' === $html ) { return; } $this->mark_special_injection_done( $post_id ); echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public function inject_related_content( $content ) { global $post; if ( ! $post ) { return $content; } $post_id = intval( $post->ID ); if ( ! $this->is_enabled_for_post( $post_id ) ) { return $content; } // WooCommerce and Tribe Calendar are handled separately if ( get_post_type( $post ) == 'product' ) { return $content; } if ( get_post_type( $post ) == 'tribe_events' ) { return $content; } // Avoid double-inserting if shortcode/block used. if ( has_shortcode( (string) $content, 'ait_airce_related' ) ) { return $content; } if ( strpos( (string) $content, 'wp:ait-airce/related-content' ) !== false ) { return $content; } if ( $this->has_special_injection_run( $post_id ) ) { return $content; } $location = $this->get_selected_location( $post_id ); $related_html = $this->get_related_html( $post_id, $location ); if ( empty( $related_html ) ) { return $content; } switch ( $location ) { case 'after_title': // Best-effort: themes output titles outside the_content, so we prepend to content. return $related_html . $content; case 'after_featured_image': // Best-effort: featured image is typically outside the_content. return $related_html . $content; case 'after_paragraph_2': return $this->insert_after_paragraph( $content, $related_html, 2 ); case 'after_content': default: return $content . $related_html; } } protected function insert_after_paragraph( $content, $insert_html, $paragraph_number = 2 ) { $paragraph_number = max( 1, intval( $paragraph_number ) ); // Split by closing p tags. $parts = explode( '

', (string) $content ); if ( count( $parts ) <= 1 ) { // If no paragraphs, append. return $content . $insert_html; } $out = ''; $p_count = 0; foreach ( $parts as $i => $part ) { if ( '' === trim( $part ) ) { continue; } $out .= $part . '

'; $p_count++; if ( $p_count === $paragraph_number ) { $out .= $insert_html; } } // If there weren't enough paragraphs, append at end. if ( $p_count < $paragraph_number ) { $out .= $insert_html; } return $out; } }