placement = new AI_Narration_Placement(); $this->content = new AI_Narration_Content( $this->placement ); $this->background = new AI_Narration_Background(); $this->cache = new AI_Narration_Cache( $this->content ); $this->admin = new AI_Narration_Admin( $this->content, $this->cache ); $this->audio = new AI_Narration_Audio( $this->content, $this->cache ); add_action( 'init', array( $this->placement, 'register_shortcodes' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'the_content', array( $this, 'filter_content' ), 99 ); } public function enqueue_assets(): void { if ( is_admin() || ! is_singular() ) { return; } $post_id = get_queried_object_id(); $has_article_player = $post_id && $this->content->is_eligible( $post_id ) && $this->content->has_narration_marker( $post_id ) && null !== $this->cache->get_available_manifest( $post_id ); if ( ! $has_article_player ) { return; } wp_enqueue_style( 'ai-narration-player', AI_NARRATION_URL . 'assets/player-v4.0.0.css', array(), AI_NARRATION_VERSION ); wp_enqueue_script( 'ai-narration-player', AI_NARRATION_URL . 'assets/player-v4.0.0.js', array(), AI_NARRATION_VERSION, true ); wp_script_add_data( 'ai-narration-player', 'strategy', 'defer' ); wp_localize_script( 'ai-narration-player', 'AINarrationConfig', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'strings' => array( 'play' => __( 'Play', 'ai-narration' ), 'pause' => __( 'Pause', 'ai-narration' ), 'loading' => __( 'Loading…', 'ai-narration' ), 'buffering' => __( 'Buffering…', 'ai-narration' ), 'ready' => __( 'Narration ready.', 'ai-narration' ), 'ended' => __( 'Narration finished.', 'ai-narration' ), 'error' => __( 'Playback failed', 'ai-narration' ), 'rateLimited' => __( 'Try again later', 'ai-narration' ), 'retry' => __( 'Retry', 'ai-narration' ), 'unsupported' => __( 'Audio unavailable', 'ai-narration' ), ), ) ); } public function filter_content( string $content ): string { if ( is_admin() || is_feed() || AI_Narration_Content::is_extracting() || ! is_singular() ) { return $content; } $post_id = get_queried_object_id(); if ( ! $post_id || ( get_the_ID() && get_the_ID() !== $post_id ) ) { return $content; } $enabled = $this->content->is_eligible( $post_id ) && null !== $this->cache->get_available_manifest( $post_id ); $player = $enabled ? $this->player_markup( $post_id ) : ''; return $this->placement->render_article( $content, $player, $enabled ); } private function player_markup( int $post_id ): string { $bootstrap = $this->audio->create_bootstrap_token( $post_id ); $estimated_minutes = $this->content->estimate_listening_minutes( $post_id ); $duration_label = null === $estimated_minutes ? '—:——' /* translators: %d: estimated listening duration in whole minutes. */ : sprintf( __( '~%d:00', 'ai-narration' ), $estimated_minutes ); $show_idle_duration = AI_Narration_Admin::show_idle_duration() && null !== $estimated_minutes; $estimated_time_accessible = null === $estimated_minutes ? '' /* translators: %d: estimated listening duration in whole minutes. */ : sprintf( _n( 'Estimated listening time: approximately %d minute.', 'Estimated listening time: approximately %d minutes.', $estimated_minutes, 'ai-narration' ), $estimated_minutes ); ob_start(); ?>