public function amazon_polly_save_post()

in admin/class-amazonpolly-admin.php [173:229]


	public function amazon_polly_save_post( $post_id, $post, $updated ) {

		// Check if this isn't an auto save.
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		// Check to make sure this is not a new post creation.
		if ( ! $updated ) {
			return;
		}

		$is_quick_edit = isset($_POST['_inline_edit']) && wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce');
		$is_polly_nonce_ok = isset( $_POST['amazon-polly-post-nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['amazon-polly-post-nonce'] ), 'amazon-polly' );

		// Check if this post is saved in 'regular' way.
		if (  $is_polly_nonce_ok || $is_quick_edit ) {

			$is_post_available = isset( $_POST['content'] );
			// Input var okay.
			$is_polly_enabled = isset( $_POST['amazon_polly_enable'] );
			// Input var okay.
			$is_post_id_available = isset( $post_id );
			// Input var okay.
			$is_key_valid = ( get_option( 'amazon_polly_valid_keys' ) === '1' );

			// If this is quick edit, we need to check other place to see if polly was already enabled for this post.
			if ( $is_post_id_available && $is_quick_edit ) {
				if ( 1 == get_post_meta( $post_id, 'amazon_polly_enable', true)) {
					$is_polly_enabled = true;
				}
			}

			if ( $is_post_id_available && $is_post_available && $is_polly_enabled && $is_key_valid ) {

				wp_nonce_field( 'amazon_polly', 'amazon_polly_sample_rate' );

				$voice_id      = get_option( 'amazon_polly_voice_id' );
				$sample_rate   = get_option( 'amazon_polly_sample_rate' );
				$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, $sample_rate, $voice_id, $sentences, $wp_filesystem, '' );

				$src_lang = $this->get_src_lang();
				update_post_meta( $post_id, 'amazon_polly_transcript_' . $src_lang, $clean_text );
				update_post_meta( $post_id, 'amazon_polly_transcript_source_lan', $src_lang );

			}

			if ( $is_post_id_available && ! $is_polly_enabled ) {
				$this->delete_post_audio( $post_id );
				update_post_meta( $post_id, 'amazon_polly_enable', 0 );
				update_post_meta( $post_id, 'amazon_polly_audio_location', '' );
			}
		}
	}