plugin_name = $plugin_name; $this->version = $version; $this->common = $common; $this->object_cache = $object_cache; } /** * WordPress filter, responsible for adding audio player to post content. * * @since 0.1 */ public function content_filter( $content ) { if ( ! isset( $GLOBALS ) || ! array_key_exists( 'post', $GLOBALS )) { return $content; } $post_id = isset( $GLOBALS['post'] ) ? $GLOBALS['post']->ID : null; if ( ! $post_id ) { return $content; } $common = $this->common; if ( ! $common->is_polly_enabled() || post_password_required( $post_id ) ) { return $content; } if ( 'publish' !== get_post_status( $post_id ) && ! current_user_can( 'read_post', $post_id ) ) { return $content; } if ( get_post_meta( $post_id, 'itron_polly_tts_enable', true ) !== '1' ) { return $content; } // Text markers apply even when no player is displayed or audio is still pending. $content = preg_replace( '/-AMAZONPOLLY-ONLYAUDIO-START-[\S\s]*?-AMAZONPOLLY-ONLYAUDIO-END-/', '', $content ); $content = str_replace( array( '-AMAZONPOLLY-ONLYWORDS-START-', '-AMAZONPOLLY-ONLYWORDS-END-' ), '', $content ); if ( ! is_singular() || 'Do not show' === get_option( 'itron_polly_tts_position' ) || ! ( new AudioConsent( $common ) )->is_allowed( (int) $post_id ) ) { return $content; } $audio_location = (string) get_post_meta( $post_id, 'itron_polly_tts_audio_link_location', true ); if ( ! $this->has_available_audio( (int) $post_id, $audio_location ) ) { $lock = new WP_Lock( PollyService::LOCK_PREFIX . $post_id ); $background_task = new BackgroundTask(); if ( $lock->lock_exists() || $background_task->has_queued_audio( $post_id ) ) { return $this->include_coming_soon() . $content; } return $content; } $selected_autoplay = get_option( 'itron_polly_tts_autoplay' ); $player_label = get_option( 'itron_polly_tts_player_label' ); if ( is_singular() && ! empty( $selected_autoplay ) ) { $autoplay = 'autoplay'; } else { $autoplay = ''; } // Prepare "Powered By" label. $voice_by_part = ''; if ( $common->is_poweredby_enabled() ) { if ( is_singular() ) { $credit = sprintf( '%s', esc_html__( 'Text-to-speech via AWS Polly', 'ai-text-to-speech-using-aws-polly' ) ); $voice_by_part = wp_kses_post( apply_filters( 'itron_polly_tts_voiced_by_html', $credit, get_locale() ) ); } } // Create player area. $polly_content = ''; if ( is_singular() ) { $this->enqueue_styles(); $audio_part = $this->include_audio_player( $audio_location, $autoplay ); $polly_content = '
|
' . esc_html( (string) $player_label ) . '
' . $audio_part . '
' . $voice_by_part . '
|