Required only if you aren\'t using IAM roles
'; } /** * Render the Secret Key input for this plugin * * @since 1.0.0 */ public function amazon_polly_secret_key_cb() { $secret_key = get_option( 'amazon_polly_secret_key' ); echo ' '; echo 'Required only if you aren\'t using IAM roles
'; } /** * Render the Automated Breath input. * * @since 1.0.7 */ public function amazon_polly_auto_breaths_cb() { if ( $this->amazon_polly_is_ok() ) { echo 'amazon_polly_checked_validator( 'amazon_polly_auto_breaths' ) . '> '; echo 'If enabled, Amazon Polly automatically creates breathing noises at appropriate intervals
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the enable Source language label input. * * @since 2.0.0 */ public function amazon_polly_trans_langs_src_label_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_translation_enabled() ) { $value = $this->replace_if_empty( get_option( 'amazon_polly_trans_langs_src_label' ), 'English' ); echo ' '; } else { echo 'Amazon Translate needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the enable Translation input. * * @since 2.0.0 */ public function amazon_polly_trans_langs_label_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_translation_enabled() ) { $trans_label = $this->replace_if_empty( get_option( 'amazon_polly_trans_langs_label' ), 'Listen in other languages: ' ); echo ' '; } else { echo 'Amazon Translate needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the input which will be responsible for providing information if * transcript of the content in other languages should be visible. * * @since 2.0.0 */ public function amazon_polly_transcript_enabled_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_translation_enabled() ) { echo 'amazon_polly_checked_validator( 'amazon_polly_transcript_enabled' ) . ' > '; } else { echo 'Amazon Translate needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Utility function which checks if checkbox for option input should be checked. * * @param string $option Name of the option which should be checked. * @since 2.0.0 */ private function amazon_polly_checked_validator( $option ) { $option_value = get_option( $option, 'on' ); if ( empty( $option_value ) ) { return ''; } else { return ' checked '; } } /** * Render the enable Translation input. * * @since 2.0.0 */ public function amazon_polly_trans_enabled_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_s3_enabled() ) { $start_value = $this->amazon_polly_checked_validator( 'amazon_polly_trans_enabled' ); $translate_accessible = $this->is_translate_accessible(); $supported_regions = array( 'us-east-1', 'us-east-2', 'us-west-2', 'eu-west-1' ); $selected_region = get_option( 'amazon_polly_region', '' ); if ( in_array( $selected_region, $supported_regions ) ) { echo 'amazon_polly_checked_validator( 'amazon_polly_trans_enabled' ) . '> '; if ( 'checked' == trim($start_value)) { if ( !$translate_accessible ) { echo 'Amazon Translate not accessible. You need to update your IAM policy.
'; } } } else { echo 'You need to use one of following regions: N.Virginia, Ohio, Oregon, Ireland
'; update_option( 'amazon_polly_trans_enabled', '' ); } } else { echo 'Amazon S3 Storage needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Podcast enabled input. * * @since 1.0.7 */ public function amazon_polly_podcast_enabled_cb() { if ( $this->amazon_polly_is_ok() ) { echo 'amazon_polly_checked_validator( 'amazon_polly_podcast_enabled' ) . '> '; echo 'If enabled, Amazon Pollycast will be generated
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Add post title to audio input. * * @since 1.0.7 */ public function amazon_polly_add_post_title_cb() { if ( $this->amazon_polly_is_ok() ) { echo 'amazon_polly_checked_validator( 'amazon_polly_add_post_title' ) . '> '; echo 'If enabled, each audio file will start from post title.
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Add post excerpt to audio input. * * @since 2.0.0 */ public function amazon_polly_add_post_excerpt_cb() { if ( $this->amazon_polly_is_ok() ) { echo 'amazon_polly_checked_validator( 'amazon_polly_add_post_excerpt' ) . '> '; echo 'If enabled, each audio file will have post excerpt at the beginning.
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Default Configuration input. * * @since 1.0.0 */ public function amazon_polly_defconf_cb() { if ( $this->amazon_polly_is_ok() ) { $selected_defconf = get_option( 'amazon_polly_defconf' ); $defconf_values = [ 'Amazon Polly enabled', 'Amazon Polly disabled' ]; echo ''; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Position input. * * @since 1.0.0 */ public function amazon_polly_position_cb() { if ( $this->amazon_polly_is_ok() ) { $selected_position = get_option( 'amazon_polly_position' ); $positions_values = array( 'Before post', 'After post', 'Do not show' ); echo ''; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Player Label input. * * @since 1.0.12 */ public function amazon_polly_lexicons_cb() { if ( $this->validate_credentials() ) { $lexicons = $this->get_lexicons(); echo ' '; echo 'Specify lexicons names (seperated by space), which you have uploaded to your AWS account
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Player Label input. * * @since 1.0.3 */ public function amazon_polly_player_label_cb() { if ( $this->amazon_polly_is_ok() ) { $player_label = get_option( 'amazon_polly_player_label' ); echo ' '; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Post Type input box. * * @since 1.0.7 */ public function amazon_polly_posttypes_cb() { if ( $this->amazon_polly_is_ok() ) { $posttypes = $this->amazon_polly_get_posttypes(); echo ' '; echo 'Post types in your WordPress environment
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the text for the Amazon Polly settings section * * @since 1.0.7 */ public function amazon_polly_pollysettings_cb() { } /** * Render the text for Player settings section * * @since 1.0.7 */ public function amazon_polly_playersettings_cb() { } /** * Render the text for the storage section * * @since 1.0.0 */ public function amazon_polly_storage_cb() { } /** * Render the text for the translation section * * @since 2.0.0 */ public function amazon_polly_trans_cb() { } /** * Render the text for the additional section * * @since 1.0.0 */ public function amazon_polly_additional_cb() { } /** * Render the 'store in S3' input. * * @since 1.0.0 */ public function amazon_polly_s3_cb() { if ( $this->amazon_polly_is_ok() ) { $selected_s3 = get_option( 'amazon_polly_s3' ); $s3_bucket_name = $this->get_bucket_name(); $message = ''; if ( empty( $s3_bucket_name ) ) { $checkbox_disabled = ''; } else { $checkbox_disabled = ''; } if ( empty( $selected_s3 ) ) { $checked = ' '; $bucket_name_visibility = 'display:none'; } else { $checked = ' checked '; $bucket_name_visibility = ' '; } echo '' . esc_attr( $message ) . '
'; $is_s3_enabled = $this->amazon_polly_is_s3_enabled(); if ( $is_s3_enabled ) { echo ''; } echo 'Audio files are saved on and streamed from Amazon S3. Learn more https://aws.amazon.com/s3
'; } else { echo 'Please verify your AWS Credentials are accurate
'; }//end if } /** * Render the 'use CloudFront' input. * * @since 1.0.0 */ public function amazon_polly_cloudfront_cb() { if ( $this->amazon_polly_is_ok() ) { $is_s3_enabled = $this->amazon_polly_is_s3_enabled(); if ( $is_s3_enabled ) { $cloudfront_domain_name = get_option( 'amazon_polly_cloudfront' ); $s3_enabled = get_option( 'amazon_polly_s3' ); if ( empty( $s3_enabled ) ) { $disabled = ' disabled="disabled" '; } else { $disabled = ' '; } echo ' '; echo 'If you have set up CloudFront distribution for your S3 bucket, the name of the domain. For additional information and pricing, see: https://aws.amazon.com/cloudfront
'; } else { echo 'Amazon S3 Storage needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; }//end if } /** * Render the enable SSML input. * * @since 1.0.7 */ public function amazon_polly_ssml_cb() { if ( $this->amazon_polly_is_ok() ) { $is_s3_enabled = $this->amazon_polly_is_s3_enabled(); if ( $is_s3_enabled ) { $is_ssml_enabled = $this->amazon_polly_is_ssml_enabled(); if ( $is_ssml_enabled ) { $checked = ' checked '; } else { $checked = ' '; } echo ' '; } else { echo 'Amazon S3 Storage needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the autoplay input. * * @since 1.0.0 */ public function amazon_polly_autoplay_cb() { if ( $this->amazon_polly_is_ok() ) { $selected_autoplay = get_option( 'amazon_polly_autoplay' ); if ( empty( $selected_autoplay ) ) { $checked = ' '; } else { $checked = ' checked '; } echo ' '; echo 'Automatically play audio content when page loads
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the autoplay input. * * @since 1.0.5 */ public function amazon_polly_speed_cb() { if ( $this->amazon_polly_is_ok() ) { $speed = $this->amazon_polly_get_speed(); echo ''; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Return post type value. * * @since 1.0.7 */ public function amazon_polly_get_posttypes() { $posttypes = get_option( 'amazon_polly_posttypes', 'post' ); return $posttypes; } /** * Return post type value array. * * @since 1.0.7 */ public function amazon_polly_get_posttypes_array() { $posttypes_array = get_option( 'amazon_polly_posttypes', 'post' ); $posttypes_array = explode( ' ', $posttypes_array ); $posttypes_array = apply_filters( 'amazon_polly_post_types', $posttypes_array ); return $posttypes_array; } /** * Check if AWS credentials are correct * * @since 1.0.7 */ private function amazon_polly_is_ok() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { return true; } else { update_option( 'amazon_polly_trans_validated', '' ); return false; } } /** * Check if Translation is enabled. * * @since 2.0.0 */ public function amazon_polly_is_translation_enabled() { $translation_enabled = get_option( 'amazon_polly_trans_enabled', '' ); if ( empty( $translation_enabled ) ) { $result = false; } else { $result = true; } $is_s3_enabled = $this->amazon_polly_is_s3_enabled(); if ( $is_s3_enabled ) { return $result; } return false; } /** * Check if SSML is enabled. * * @since 1.0.7 */ public function amazon_polly_is_ssml_enabled() { $ssml_enabled = get_option( 'amazon_polly_ssml', 'on' ); if ( empty( $ssml_enabled ) ) { $result = false; } else { $result = true; } $is_s3_enabled = $this->amazon_polly_is_s3_enabled(); if ( $is_s3_enabled ) { return $result; } return false; } /** * Return speed for audio files. * * @since 1.0.5 */ public function amazon_polly_get_speed() { $speed = get_option( 'amazon_polly_speed' ); if ( empty( $speed ) ) { $speed = '100'; } if ( intval( $speed ) < 20 ) { $speed = '20'; } if ( intval( $speed ) > 200 ) { $speed = '200'; } update_option( 'amazon_polly_speed', $speed ); return $speed; } /** * Checks if pollycast is enabled. * * @since 1.0.7 */ public function amazon_polly_is_podcast_enabled() { $value = get_option( 'amazon_polly_podcast_enabled', 'on' ); if ( empty( $value ) ) { $result = false; } else { $result = true; } return $result; } /** * Checks if auto breaths are enabled. * * @since 1.0.7 */ public function amazon_polly_is_auto_breaths_enabled() { $value = get_option( 'amazon_polly_auto_breaths', 'on' ); if ( empty( $value ) ) { $result = false; } else { $result = true; } return $result; } /** * Checks if post title should be added. * * @since 1.0.7 */ public function amazon_polly_is_title_adder_enabled() { $value = get_option( 'amazon_polly_add_post_title', 'on' ); if ( empty( $value ) ) { $result = false; } else { $result = true; } return $result; } /** * Checks if post title should be added. * * @since 1.0.7 */ public function amazon_polly_is_excerpt_adder_enabled() { $value = get_option( 'amazon_polly_add_post_excerpt', 'on' ); if ( empty( $value ) ) { $result = false; } else { $result = true; } return $result; } /** * Render the region input. * * @since 1.0.3 */ public function amazon_polly_region_cb() { if ( $this->amazon_polly_is_ok() ) { $selected_region = get_option( 'amazon_polly_region' ); $regions = array( 'us-east-1' => 'US East (N. Virginia)', 'us-east-2' => 'US East (Ohio)', 'us-west-1' => 'US West (N. California)', 'us-west-2' => 'US West (Oregon)', 'eu-west-1' => 'EU (Ireland)', 'eu-west-2' => 'EU (London)', 'eu-west-3' => 'EU (Paris)', 'eu-central-1' => 'EU (Frankfurt)', 'ca-central-1' => 'Canada (Central)', 'sa-east-1' => 'South America (Sao Paulo)', 'ap-southeast-1' => 'Asia Pacific (Singapore)', 'ap-northeast-1' => 'Asia Pacific (Tokyo)', 'ap-southeast-2' => 'Asia Pacific (Sidney)', 'ap-northeast-2' => 'Asia Pacific (Seul)', 'ap-south-1' => 'Asia Pacific (Mumbai)', ); echo ''; } else { echo 'Please verify your AWS Credentials are accurate
'; }//end if } /** * Render the translation target languages input. * * @since 2.0.0 */ public function amazon_polly_trans_langs_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_translation_enabled() ) { $src_lang = $this->get_src_lang(); $voice_list = $this->client->describeVoices(); usort( $voice_list['Voices'], 'sort_voices' ); echo 'Amazon Translate needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; }//end if } /** * Render the translation target languages input. * * @param string $language_label Label which should be used for this language. * @param string $language_name Name (in english) of this language. * @param string $lanuage Language code. * @param string $voice_list List of available voices. * @param string $src_lang Source Language (code). * @since 2.0.0 */ private function show_translate_option( $src_lang, $voice_list, $lanuage, $language_name, $language_label ) { if ( empty( $src_lang ) ) { $src_lang = 'en'; } if ( $src_lang == $lanuage ) { return; } $lan_option = 'amazon_polly_trans_langs_' . $lanuage; $lan_voice_option = 'amazon_polly_trans_langs_' . $lanuage . '_voice'; $lan_label_option = 'amazon_polly_trans_langs_' . $lanuage . '_label'; $disabled = ''; if ( ( $src_lang == $lanuage ) or ( 'en' == $lanuage ) ) { $disabled = 'disabled'; } echo 'Amazon Translate needs to be enabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Sample Rate input for this plugin * * @since 1.0.0 */ public function amazon_polly_sample_rate_cb() { if ( $this->amazon_polly_is_ok() ) { $sample_rate = get_option( 'amazon_polly_sample_rate' ); $sample_array = array( '22050', '16000', '8000' ); echo ''; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Validate if AWS credentials are proper. * * @since 1.0.0 */ private function amazon_polly_validate_credentials() { try { $voice_list = $this->client->describeVoices(); update_option( 'amazon_polly_valid_keys', '1' ); } catch ( Exception $e ) { update_option( 'amazon_polly_valid_keys', '0' ); } } /** * Validate if AWS credentials are proper. * * @since 1.0.0 */ private function validate_credentials() { try { $voice_list = $this->client->describeVoices(); return true; } catch ( Exception $e ) { return false; } } /** * Render the Polly Voice input for this plugin * * @since 1.0.0 */ public function amazon_polly_voice_id_cb() { /** * Compare two voices for ordering purpose. * * @param string $voice1 First voice. * @param string $voice2 Second voice. * @since 1.0.0 */ function sort_voices( $voice1, $voice2 ) { return strcmp( $voice1['LanguageName'], $voice2['LanguageName'] ); } if ( $this->amazon_polly_is_ok() ) { $voice_id = get_option( 'amazon_polly_voice_id' ); $voice_list = $this->client->describeVoices(); echo ''; } else { $voice_id = get_option( 'amazon_polly_voice_id' ); echo ''; echo 'Please verify your AWS Credentials are accurate
'; }//end if } /** * Render the Update All input for this plugin * * @since 1.0.0 */ public function amazon_polly_update_all_cb() { if ( $this->amazon_polly_is_ok() ) { $message = $this->get_price_message_for_update_all(); echo ''; echo ''; echo ''; echo '
'; echo '' . esc_html( $message ) . '
'; echo ''; echo 'Please verify your AWS Credentials are accurate
'; } } /** * Podcast section description. * * @since 1.0.0 */ public function amazon_pollycast_cb() { if ( $this->amazon_polly_is_podcast_enabled() ) { echo 'Amazon Pollycast available at: ' . esc_html( get_feed_link( 'amazon-pollycast' ) ) . '
'; echo 'Submit your Amazon Pollycast to iTunes iConnect: ' . esc_html( 'https://podcastsconnect.apple.com/' ) . '
'; } } /** * Render the Update All input for this plugin * * @since 1.0.0 */ public function amazon_polly_podcast_email_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_podcast_enabled() ) { $selected_email = get_option( 'amazon_polly_podcast_email' ); echo ''; } else { echo 'Amazon Pollycast is disabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Update All input for this plugin * * @since 1.0.0 */ public function amazon_polly_podcast_category_cb() { $default_category = 'News & Politics'; $select_categories = array( 'Arts', 'Business', 'Comedy', 'Education', 'Games & Hobbies', 'Government & Organizations', 'Health', 'Kids', 'Music', 'News & Politics', 'Religion', 'Science & Medicine', 'Society & Culture', 'Sports & Recreation', 'Technology', 'TV & Film', ); if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_podcast_enabled() ) { $selected_category = get_option( 'amazon_polly_podcast_category' ); if ( ! $selected_category ) { $selected_category = 'News & Politics'; } echo ''; } else { echo 'Amazon Pollycast is disabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; }//end if } /** * Render the Update All input for this plugin * * @since 1.0.0 */ public function amazon_polly_podcast_explicit_cb() { $select_explicits = array( 'yes', 'no', ); if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_podcast_enabled() ) { $selected_explicit = get_option( 'amazon_polly_podcast_explicit' ); echo ''; } else { echo 'Amazon Pollycast is disabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the Update All input for this plugin * * @since 1.0.0 */ public function amazon_polly_podcast_icon_cb() { if ( $this->amazon_polly_is_ok() ) { if ( $this->amazon_polly_is_podcast_enabled() ) { $query['autofocus[section]'] = 'amazonpolly'; $section_link = add_query_arg( $query, admin_url( 'customize.php' ) ); echo 'Upload a podcast icon using the Customizer.
'; } else { echo 'Amazon Pollycast is disabled
'; } } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Checks how many posts should be converted. * * @since 1.0.0 */ public function get_num_posts_needing_transcription() { $post_types_supported = $this->amazon_polly_get_posttypes_array(); $amazon_polly_voice_id = get_option( 'amazon_polly_voice_id' ); $amazon_polly_sample_rate = get_option( 'amazon_polly_sample_rate' ); $amazon_polly_audio_location = ( 'on' === get_option( 'amazon_polly_s3' ) ) ? 's3' : 'local'; $args = array( 'posts_per_page' => '-1', 'post_type' => $post_types_supported, 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'amazon_polly_audio_link_location', 'compare' => 'EXISTS', ), array( 'key' => 'amazon_polly_voice_id', 'value' => $amazon_polly_voice_id, 'compare' => '=', ), array( 'key' => 'amazon_polly_sample_rate', 'value' => $amazon_polly_sample_rate, 'compare' => '=', ), array( 'key' => 'amazon_polly_audio_location', 'value' => $amazon_polly_audio_location, 'compare' => '=', ), ), ); $query = new WP_Query( $args ); return count( $query->posts ); } /** * Method returns lexicons specified in plugin configuration. * * @since 1.0.12 */ private function get_lexicons() { $lexicons = get_option( 'amazon_polly_lexicons', '' ); $lexicons = trim( $lexicons ); return $lexicons; } /** * Calculate the total price of converting all posts into audio. * * @since 1.0.0 */ private function get_price_message_for_update_all() { $post_types_supported = $this->amazon_polly_get_posttypes_array(); $number_of_characters = 0; $posts_per_page = apply_filters( 'amazon_polly_posts_per_page', 5 ); $count_posts = wp_count_posts()->publish; $max_count_posts = 100; // Retrieving the number of characters in all posts. $paged = 0; $post_count = 0; do { $paged++; $wp_query = new WP_Query( array( 'posts_per_page' => $posts_per_page, 'post_type' => $post_types_supported, 'fields' => 'ids', 'paged' => $paged, ) ); $number_of_posts = $wp_query->max_num_pages; while ( $wp_query->have_posts() ) { $post_count++; $wp_query->the_post(); $post_id = get_the_ID(); $clean_text = $this->clean_text( $post_id ); $post_sentences = $this->break_text( $clean_text ); if ( ! empty( $post_sentences ) ) { foreach ( $post_sentences as $sentence ) { $sentence = str_replace( '**AMAZONPOLLY*SSML*BREAK*time***1s***SSML**', '', $sentence ); $sentence = str_replace( '**AMAZONPOLLY*SSML*BREAK*time***500ms***SSML**', '', $sentence ); $number_of_characters += strlen( $sentence ); } } } // If we reached the number of posts which we wanted to read, we stop // reading next posts. if ( $post_count >= $max_count_posts ) { break; } } while ( $paged < $number_of_posts ); // Price for converting single character according to Amazon Polly pricing. $amazon_polly_price = 0.000004; // Estimating average number of characters per post. if ( 0 !== $post_count ) { $post_chars_count_avg = $number_of_characters / $post_count; } else { $post_chars_count_avg = 0; } // Estimating the total price of convertion of all posts. $total_price = $amazon_polly_price * $count_posts * $post_chars_count_avg; $message = 'You are about to convert ' . number_format( $count_posts, 0, '.', ',' ) . ' pieces of text-based content, which totals approximately ' . number_format( $number_of_characters, 0, '.', ',' ) . ' characters. Based on the Amazon Polly pricing ($4 dollars per 1 million characters) it will cost you about $' . $total_price . ' to convert all of your content into to speech-based audio. Some or all of your costs might be covered by the Free Tier (conversion of 5 million characters per month for free, for the first 12 months, starting from the first request for speech). Learn more https://aws.amazon.com/polly/'; return $message; } public function amazon_polly_ajax_translate() { check_ajax_referer( 'pollyajaxnonce', 'nonce' ); $post_id = $_POST['post_id']; $phase = $_POST['phase']; $langs = $_POST['langs']; $step = ''; $percentage = 0; $message = ''; $all_langs = []; $index = 0; $src_lang = get_post_meta( $post_id, 'amazon_polly_transcript_source_lan', true ); if ( empty( $src_lang ) ) { $message = 'Amazon Translate functionality needs to be enabled before publishing the post'; $step = 'done'; $percentage = 100; } else { foreach ( $this->translate_langs as $supported_lan ) { if ( $this->check_if_translate( $supported_lan ) and ( $supported_lan != $src_lang ) ) { $all_langs[ $index ] = $supported_lan; $index++; } } if ( 'start' == $phase ) { $langs = $all_langs; } else { if ( ( $key = array_search( 'en', $langs ) ) !== false ) { $lan = 'en'; unset( $langs[ $key ] ); } else { $lan = array_shift( $langs ); } if ( 'en' == $lan ) { $clean_text = $this->clean_text( $post_id ); } else { $clean_text = get_post_meta( $post_id, 'amazon_polly_transcript_en', true ); $src_lang = 'en'; } $wp_filesystem = $this->prepare_wp_filesystem(); $this->do_translation( $src_lang, $clean_text, $wp_filesystem, $post_id, '', '', $lan ); $percentage = 100 - ( count( $langs ) / $index ) * 100; }//end if if ( empty( $langs ) ) { $step = 'done'; $message = 'Translation completed!'; } }//end if $next_langs = $langs; if ( ( 'en' != $src_lang ) && ( $key = array_search( 'en', $next_langs ) ) !== false ) { $next_lang = 'en'; } else { $next_lang = array_shift( $next_langs ); } if ( ! empty( $next_lang ) ) { $message = 'Translating from ' . $this->amazon_polly_map( $src_lang ) . ' to ' . $this->amazon_polly_map( $next_lang ); } else { $message = 'Translation completed!'; } if ( empty( $src_lang ) ) { $message = 'Amazon Translate functionality needs to be enabled before publishing the post'; } echo wp_json_encode( array( 'step' => $step, 'langs' => $langs, 'percentage' => $percentage, 'message' => $message, ) ); wp_die(); } private function amazon_polly_map( $code ) { switch ( $code ) { case 'en': return 'English'; case 'fr': return 'French'; case 'de': return 'German'; case 'pt': return 'Portuguese'; case 'es': return 'Spanish'; } return 'N/A'; } /** * Batch process the post transcriptions. * * @since 1.0.0 */ public function amazon_polly_ajax_transcribe() { check_ajax_referer( 'pollyajaxnonce', 'nonce' ); $batch_size = 1; $post_types_supported = $this->amazon_polly_get_posttypes_array(); $amazon_polly_voice_id = get_option( 'amazon_polly_voice_id' ); $amazon_polly_sample_rate = get_option( 'amazon_polly_sample_rate' ); $amazon_polly_audio_location = ( 'on' === get_option( 'amazon_polly_s3' ) ) ? 's3' : 'local'; // We are using a hash of these values to improve the speed of queries. $amazon_polly_settings_hash = md5( $amazon_polly_voice_id . $amazon_polly_sample_rate . $amazon_polly_audio_location ); $args = array( 'posts_per_page' => $batch_size, 'post_type' => $post_types_supported, 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'amazon_polly_audio_link_location', 'compare' => 'NOT EXISTS', ), array( 'key' => 'amazon_polly_voice_id', 'value' => $amazon_polly_voice_id, 'compare' => '!=', ), array( 'key' => 'amazon_polly_sample_rate', 'value' => $amazon_polly_sample_rate, 'compare' => '!=', ), array( 'key' => 'amazon_polly_audio_location', 'value' => $amazon_polly_audio_location, 'compare' => '!=', ), ), ); $query = new WP_Query( $args ); $post_ids = wp_list_pluck( $query->posts, 'ID' ); if ( is_array( $post_ids ) && ! empty( $post_ids ) ) { foreach ( $post_ids as $post_id ) { $clean_text = $this->clean_text( $post_id ); $sentences = $this->break_text( $clean_text ); $wp_filesystem = $this->prepare_wp_filesystem(); $this->convert_to_audio( $post_id, $amazon_polly_sample_rate, $amazon_polly_voice_id, $sentences, $wp_filesystem, '' ); } } else { $step = 'done'; } $percentage = $this->get_percentage_complete(); echo wp_json_encode( array( 'step' => $step, 'percentage' => $percentage, ) ); wp_die(); } /** * Method validates if plugin has got access to Amazon Translate service by performing simple * translation of single word. * * @since 2.0.0 */ private function is_translate_accessible() { $accessible = false; $option_value = get_option( 'amazon_polly_trans_validated' ); if ( empty($option_value) ) { try { // Perform simple translation of single word. $translated_text_part = $this->translate_client->translateText( array( 'SourceLanguageCode' => 'en', 'TargetLanguageCode' => 'es', 'Text' => 'cloud', ) ); update_option( 'amazon_polly_trans_validated', 'ok' ); $accessible = true; } catch ( Exception $e ) { update_option( 'amazon_polly_trans_validated', '' ); $accessible = false; } } else { $accessible = true; } if ( !$accessible ) { update_option( 'amazon_polly_trans_enabled', '' ); update_option( 'amazon_polly_trans_validated', '' ); } return $accessible; } /** * Calculate the percentage complete. * * @since 1.0.0 */ public function get_percentage_complete() { $total_posts = 0; $post_types_supported = $this->amazon_polly_get_posttypes_array(); $posts_needing_translation = $this->get_num_posts_needing_transcription(); foreach ( $post_types_supported as $post_type ) { $post_type_count = wp_count_posts( $post_type )->publish; $total_posts += $post_type_count; } if ( 0 >= $total_posts || 0 >= $posts_needing_translation ) { $percentage = 100; } else { $percentage = round( $posts_needing_translation / $total_posts * 100, 2 ); } return $percentage; } /** * Adds a Settings action link to the Plugins page. * * @since 1.0.0 * @param string $links A list of plugin action links. */ public function plugin_add_settings_link( $links ) { $settings_link = '' . __( 'Settings' ) . ''; array_unshift( $links, $settings_link ); return $links; } /** * Get S3 bucket name. The method uses filter 'amazon_polly_s3_bucket_name, * which allows to use customer S3 bucket name instead of default one. * * @since 1.0.6 */ private function get_bucket_name() { $s3_bucket_name = get_option( 'amazon_polly_s3_bucket' ); $s3_bucket_name = apply_filters( 'amazon_polly_s3_bucket_name', $s3_bucket_name ); return $s3_bucket_name; } /** * Configure supported HTML tags. * * @since 1.0.7 * @param string $tags supported tags. */ public function amazon_polly_allowed_tags_kses( $tags ) { $tags['ssml'] = true; $tags['speak'] = true; $tags['break'] = array( 'time' => true, ); return $tags; } /** * Configure supported HTML tags. * * @since 1.0.7 * @param string $tags supported tags. */ public function amazon_polly_allowed_tags_tinymce( $tags ) { $ssml_tags = array( 'ssml', 'speak', 'break[time|whatever]', 'emphasis[level]', 'lang', 'mark', 'paragraph', 'phoneme', 'prosody', 's', 'say-as', 'sub', 'w', 'amazon:breath', 'amazon:auto-breaths', 'amazon:effect[name]', 'amazon:effect[phonation]', 'amazon:effect[vocal-tract-length]', 'amazon:effect[name]', ); $tags['extended_valid_elements'] = implode( ',', $ssml_tags ); return $tags; } /** * Returns source language. * * @since 2.0.0 */ private function get_src_lang() { $value = get_option( 'amazon_polly_trans_src_lang', 'en' ); if ( empty($value) ) { $value = 'en'; } return $value; } /** * Checks if auto breaths are enabled. * * @since 1.0.7 */ public function amazon_polly_is_s3_enabled() { $value = get_option( 'amazon_polly_s3', 'on' ); if ( empty( $value ) ) { $result = false; } else { $result = true; } return $result; } /** * Encode SSML tags. * * @since 1.0.7 * @param string $text text which should be encoded. */ private function amazon_polly_encode_ssml_tags( $text ) { $text = preg_replace( '/