public function amazon_polly_ajax_transcribe()

in admin/class-amazonpolly-admin.php [2269:2330]


	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();
	}