supported_post_types = get_option( 'accessibility_audio_tts_post_types', array( 'post' ) ); } else { $this->supported_post_types = $supported_post_types; } if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { accessibility_audio_tts_debug_log( 'AAT: Creating Player_Renderer...' ); } $this->renderer = new Player_Renderer(); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { accessibility_audio_tts_debug_log( 'AAT: Player_Renderer created successfully' ); } add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'the_content', array( $this, 'add_player_to_content' ), 20 ); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { accessibility_audio_tts_debug_log( 'AAT: Player constructor completed, content filter registered' ); } } /** * Add player to post content if audio exists * * @param string $content Post content. * @return string Modified content. */ public function add_player_to_content( $content ) { global $post; // Only add player on single post/page views. if ( ! is_singular() || ! $post || ! in_array( $post->post_type, $this->supported_post_types, true ) ) { return $content; } if ( ! \Accessibility_Audio_TTS\Core\Audio_Access::can_read( $post ) ) { return $content; } // Check if post has generated audio. try { $file = \Accessibility_Audio_TTS\Core\Audio_Access::resolve_audio_file( $post->ID ); } catch ( \Throwable $error ) { return $content; } $audio_url = $file['url'] ?? ''; if ( $audio_url ) { // Add player before content. $player_html = $this->renderer->render_player( $post->ID, $audio_url ); $content = $player_html . $content; } return $content; } /** * Enqueue assets for frontend player * Note: Asset loading is now handled by Frontend_Asset_Loader via Asset_Manager * This method only handles preconnect optimization */ public function enqueue_assets() { // Only enqueue preconnect if we're on a singular post/page with audio. if ( ! is_singular() ) { return; } global $post; if ( ! $post || ! in_array( $post->post_type, $this->supported_post_types, true ) ) { return; } // Check if post has generated audio. $audio_url = get_post_meta( $post->ID, 'accessibility_audio_tts_url', true ); if ( ! $audio_url ) { return; } // Load jQuery (required by player scripts loaded by Frontend_Asset_Loader). wp_enqueue_script( 'jquery' ); } }