Wp_Helpers::select_post_type_taxonomies()

Return all the specific taxonomies of selected post type in ajax call


Source Source

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

	public function select_post_type_taxonomies() {


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

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

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

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

			$post_type = get_post_type_object( $post_type );

			if ( null !== $post_type ) {
				$output     = 'object';
				$taxonomies = get_object_taxonomies( $post_type->name, $output );

				if ( ! empty( $taxonomies ) ) {
					foreach ( $taxonomies as $taxonomy ) {
						$fields[] = array(
							'value' => $taxonomy->name,
							'text'  => esc_html( $taxonomy->labels->singular_name ),
						);
					}
				}
			}
		}

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