Inventory_Presser_Taxonomy_Overlapper::maintain_taxonomy_terms_during_meta_updates( int $meta_id, int $object_id, string $meta_key, string|object $meta_value )

maintain_taxonomy_terms_during_meta_updates


Description Description

When post meta values are updated on vehicle posts, check to see if the same value is also stored in one of our taxonomies to make filtering easy. If so, mirror the changes by creating a new term relationship.


Top ↑

Parameters Parameters

$meta_id

(int) (Required) ID of updated metadata entry.

$object_id

(int) (Required) Post ID.

$meta_key

(string) (Required) Metadata key.

$meta_value

(string|object) (Required) Metadata value. This will be a PHP-serialized string representation of the value if the value is an array, an object, or itself a PHP-serialized string.


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-taxonomy-overlapper.php

	function maintain_taxonomy_terms_during_meta_updates( $meta_id, $object_id, $meta_key, $meta_value )
	{
		//These are the unprefixed meta keys that have overlapping taxonomies
		$overlapping_keys = $this->overlapping_meta_keys();
		//unprefix the meta key
		$unprefixed = apply_filters( 'invp_unprefix_meta_key', $meta_key );

		//does $meta_key have a corresponding taxonomy?
		if( ! in_array( $unprefixed, array_keys( $overlapping_keys ) ) )
		{
			//No
			return;
		}

		$taxonomy = $overlapping_keys[$unprefixed];

		/**
		 * If we are in the Availability taxonomy, the end of this method
		 * appends terms instead of replacing. That means if the $meta_value is
		 * For Sale or Sold, we need to remove the opposite term.
		 */
		if( 'availability' == $unprefixed && ! empty( $meta_value ) )
		{
			$for_sale_and_sold_term_ids = get_terms( array(
				'taxonomy' => $taxonomy,
				'fields'   => 'ids',
				'slug'     => array( 'for-sale', 'sold' ),
			) );
			$this->hooks_remove();
			wp_remove_object_terms( $object_id, $for_sale_and_sold_term_ids, $taxonomy );
			$this->hooks_add();
		}

		//if $meta_value is empty, then remove a term & exit
		//will this only happen for wholesale?
		if( empty( $meta_value ) )
		{
			//remove a term actually
			$terms = array();
			if( 'availability' == $taxonomy )
			{
				$terms = wp_get_object_terms( $object_id, $taxonomy );
				for( $t=0; $t<sizeof($terms); $t++ )
				{
					if( $unprefixed == $terms[$t]->slug )
					{
						/**
						 * Both $unprefixed and $terms[$t]->slug are 'wholesale'
						 * or $unprefixed and $taxonomy are both 'availability'
						 */
						//trash this one
						unset( $terms[$t] );
						break;
					}
				}
			}

			$this->hooks_remove();
			wp_set_object_terms( $object_id, array_column( $terms, 'term_id' ), $taxonomy );
			$this->hooks_add();
			return;
		}

		/**
		 * Wholesale is a term in the Availability taxonomy rather than a real
		 * boolean as the meta field suggests & is registered.
		 */
		if( 'wholesale' == $unprefixed )
		{
			$meta_value = 'Wholesale';
		}

		//is there already a term for this $meta_value in the taxonomy?
		$term = get_term_by( 'slug', INVP::sluggify( $meta_value ), $taxonomy );
		if( ! $term )
		{
			//it's not a slug, what about a name?
			$term = get_term_by( 'name', $meta_value, $taxonomy );
			if( ! $term )
			{
				//No, create a term
				$term_id_array = wp_insert_term( $meta_value, $taxonomy, array(
					'description' => $meta_value,
					'slug'        => INVP::sluggify( $meta_value ),
				) );
				if( ! empty( $term_id_array['term_id'] ) )
				{
					$term = get_term( $term_id_array['term_id'], $taxonomy );
				}
			}
		}

		/** 
		 * Assign the new term for this $object_id. The Availability taxonomy
		 * holds For Sale/Sold and Wholesale, so append in that taxonomy.
		 */
		$this->hooks_remove();
		wp_set_object_terms( $object_id, $term->term_id, $taxonomy, ( 'availability' == $taxonomy ) );
		$this->hooks_add();
	}