Wp_Helpers::select_terms_for_selected_taxonomy()

Return all the specific terms of the selected taxonomy in ajax call


Source Source

File: src/integrations/wp/helpers/wp-helpers.php

	public function select_terms_for_selected_taxonomy() {


		Automator()->utilities->ajax_auth_check( $_POST );
		$fields = array();

		$fields[] = array(
			'value' => '0',
			'text'  => __( 'Any taxonomy term', 'uncanny-automator' ),
		);

		if ( isset( $_POST ) && key_exists( 'value', $_POST ) && ! empty( $_POST['value'] ) ) {

			$taxonomy = sanitize_text_field( $_POST['value'] );

			if ( '0' !== $taxonomy ) {

				$taxonomy = get_taxonomy( $taxonomy );

				if ( false !== $taxonomy ) {

					$terms = get_terms(
						array(
							'taxonomy'   => $taxonomy->name,
							'hide_empty' => false,
						)
					);

					if ( ! empty( $terms ) ) {
						foreach ( $terms as $term ) {
							/* translators: %1$s The ID of the post. */
							$term_name = ! empty( $term->name ) ? $term->name : sprintf( __( 'ID: %1$s (no title)', 'uncanny-automator' ), $term->term_id );

							$fields[] = array(
								'value' => $term->term_id,
								'text'  => $term_name,
							);
						}
					}
				}
			}
		}

		echo wp_json_encode( $fields );
		die();
	}