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 Default Configuration input. * * @since 1.0.0 */ public function amazon_polly_defconf_cb() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $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() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $selected_position = get_option( 'amazon_polly_position' ); $positions_values = [ 'Before post', 'After post', 'Do not show' ]; echo ''; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Render the text for the storage section * * @since 1.0.0 */ public function amazon_polly_storage_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() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $selected_s3 = get_option( 'amazon_polly_s3' ); $s3_bucket_name = get_option( $this->s3_bucket_metakey ); if ( empty( $s3_bucket_name ) ) { $checkbox_disabled = 'disabled'; } else { $checkbox_disabled = ''; } if ( empty( $selected_s3 ) ) { $checked = ' '; $bucket_name_visibility = 'display:none'; } else { $checked = ' checked '; $bucket_name_visibility = ' '; } echo ' '; 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() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $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 'Please verify your AWS Credentials are accurate
'; } } /** * Render the autoplay input. * * @since 1.0.0 */ public function amazon_polly_autoplay_cb() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $selected_autoplay = get_option( 'amazon_polly_autoplay' ); if ( empty( $selected_autoplay ) ) { $checked = ' '; } else { $checked = ' checked '; } echo ' '; echo ''; } 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() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $sample_rate = get_option( 'amazon_polly_sample_rate' ); $sample_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' ); } } /** * 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'] ); } $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $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() { $this->amazon_polly_validate_credentials(); $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $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() { 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() { $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $selected_email = get_option( 'amazon_polly_podcast_email' ); echo ''; } 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() { $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); $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 ( $is_key_valid ) { $selected_category = get_option( 'amazon_polly_podcast_category' ); if ( ! $selected_category ) { $selected_category = 'News & Politics'; } echo ''; } 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_explicit_cb() { $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); $select_explicits = array( 'yes', 'no', ); if ( $is_key_valid ) { $selected_explicit = get_option( 'amazon_polly_podcast_explicit' ); echo ''; } 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() { $is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' ); if ( $is_key_valid ) { $query['autofocus[section]'] = 'amazonpolly'; $section_link = add_query_arg( $query, admin_url( 'customize.php' ) ); echo 'Upload a podcast icon using the Customizer.
'; } else { echo 'Please verify your AWS Credentials are accurate
'; } } /** * Determine how many posts should be transcribed. */ public function get_num_posts_needing_transcription() { $post_types_supported = apply_filters( 'amazon_polly_post_types', array( 'post' ) ); $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' => $this->audio_location_link_metakey, '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 ); } /** * 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 = apply_filters( 'amazon_polly_post_types', array( 'post' ) ); $number_of_characters = 0; // Retrieving the number of characters in all posts. $paged = 0; do { $paged++; $wp_query = new WP_Query( array( 'posts_per_page' => 1, 'post_type' => $post_types_supported, 'fields' => 'ids', 'paged' => $paged, ) ); $number_of_posts = $wp_query->max_num_pages; while ( $wp_query->have_posts() ) { $wp_query->the_post(); $post_id = get_the_ID(); $post_sentences = $this->prepare_post_text( $post_id ); if ( ! empty( $post_sentences ) ) { foreach ( $post_sentences as $sentence ) { $number_of_characters += strlen( $sentence ); } } } } while ( $paged < $number_of_posts ); $amazon_polly_price = 0.000004; $total_price = $amazon_polly_price * $number_of_characters; $message = 'You are about to convert ' . number_format( $number_of_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 $' . money_format( '%i', $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; } /** * 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 = apply_filters( 'amazon_polly_post_types', array( 'post' ) ); $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' => $this->audio_location_link_metakey, '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 ) { $sentences = $this->prepare_post_text( $post_id ); $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(); } /** * Calculate the percentage complete. * * @since 1.0.0 */ public function get_percentage_complete() { $counter = 0; $post_types_supported = apply_filters( 'amazon_polly_post_types', array( 'post' ) ); foreach ( $post_types_supported as $post_type ) { $post_type_count = wp_count_posts( $post_type )->publish; $counter += $post_type_count; } if ( $counter > 0 ) { return round( $this->get_num_posts_needing_transcription() / ( $counter / 100 ), 2 ); } else { return 0; } } /** * 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; } }