/**
 * Inserts the terms into a given post, based on their saved meta/
 *
 * This function replaces the old taxonomy code in the converter class, utilizing the new meta data
 * format used for the taxonomies. The terms for each taxonomy, along with any auto created feed terms
 * are inserted into the post.
 *
 * @since 3.1
 * @param int $post_id The ID of the post.
 * @param int $feed_id The ID of the feed source that imported the post.
 * @param object $sp_item The SimplePie_Item from which the post was converted.
 * @return bool TRUE if the terms where inserted successfully, FALSE if the post was not found.
 */
function wprss_ftp_add_taxonomies_to_post( $post_id, $feed_id, $sp_item ) {
	// If the post does not exist, stop immediately
	if ( get_post( $post_id ) === null ) return NULL;

	// Get the taxonomies meta
	$meta = WPRSS_FTP_Meta::get_instance()->get( $feed_id, 'taxonomies' );
	// If the source has the old meta saved, convert it into the new meta
	if ( $meta === '' ) {
		$meta = WPRSS_FTP_Meta::convert_post_taxonomy_meta( $feed_id );
	}

	// Get the settings instance
	$settings = WPRSS_FTP_Settings::get_instance();
	// Get the post type saved in the settings
	$settings_post_type = $settings->get( 'post_type' );
	// If it matches, add the settings taxonomies to the meta taxonomies
	if ( $settings_post_type === get_post_type( $post_id ) ) {
		// Get the taxonomies settings, and convert from old format if needed
		$settings_taxonomies = $settings->get( 'taxonomies' );
		if ( $settings_taxonomies === '' ) {
			$settings_taxonomies = WPRSS_FTP_Settings::convert_post_taxonomy_settings();
		}
		// Add to meta taxonomies
		$meta = array_merge( (array) $settings_taxonomies, (array) $meta );
	}

	$tax_iterated = array();

	// For each entry
	foreach( $meta as $entry ) {
		// Check if this taxonomy term should be applied.
		if ( wprss_ftp_should_apply_taxonomy( $entry, $sp_item ) === FALSE ) {
			continue;
		}

		// Get the data
		$taxonomy = $entry['taxonomy'];
		$terms = $entry['terms'];
		$terms = is_array( $terms ) ? $terms : array();
		$auto = WPRSS_FTP_Utils::multiboolean( $entry['auto'] );
		$feed_terms = array();

		// Repeat the taxonomy slug in an array with the same length as the terms array
		$taxonomies_array = count( $terms ) > 0 ? array_fill( 0, count($terms), $taxonomy ) : array();
		// Run the term slugs through the 'wprss_ftp_create_or_get_term'
		$terms_to_set = array_map( 'wprss_ftp_create_or_get_term', $terms, $taxonomies_array);
		// Filter the terms to remove NULL entries (NULL can be returned by 'wprss_ftp_create_or_get_term')
		$terms_to_set = array_filter( $terms_to_set );

		// If auto creation is enabled
		if ( $auto === TRUE ) {
			// If auto create is enabled, get the terms from the feed
			$feed_terms = $sp_item->get_categories();
			$feed_terms = is_array( $feed_terms )? $feed_terms : array();
			// Map the terms through our preparation function, and filter them for custom user manipulation
			$feed_terms = array_map( 'wprss_ftp_prepare_auto_created_term', $feed_terms );
			$feed_terms = apply_filters( 'wprss_auto_create_terms', $feed_terms, $taxonomy, $feed_id );
			// Repeat the taxonomy slug in an array with the same length as the terms array
			$num_terms = count( $feed_terms );
			$taxonomies_array = $num_terms > 0? array_fill( 0, $num_terms, $taxonomy ) : array();
			// Run them through the `wprss_ftp_process_auto_created_terms` function for processing
			$feed_terms = array_map( 'wprss_ftp_process_auto_created_terms', $feed_terms, $taxonomies_array );
			// Filter the terms to remove NULL entries (NULL can be returned by 'wprss_ftp_create_or_get_term')
			$feed_terms = array_filter( $feed_terms );
			// Add them to the terms to set
			$terms_to_set = array_merge( (array) $terms_to_set, (array) $feed_terms );
		}

		// Add the taxonomy to the tax_iterated array if it's not already in the array
		if ( ! in_array( $taxonomy, $tax_iterated ) ) {
			$tax_iterated[] = $taxonomy;
			// Set no terms, and override existing - to remove any default terms, like 'Uncategorized'
			wp_set_object_terms( $post_id, array(), $taxonomy, FALSE );
		}
		// Insert the terms
		wp_set_object_terms( $post_id, $terms_to_set, $taxonomy, TRUE );
		// clear the cache
		delete_option($taxonomy."_children");
	}

	return TRUE;
}