Inventory_Presser_Taxonomy_Overlapper::maintain_taxonomy_terms_during_meta_updates( int $meta_id, int $object_id, string $meta_key, string|object $meta_value )
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.
On This Page
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.
Return Return
(void)
Source Source
File: includes/class-taxonomy-overlapper.php
public function maintain_taxonomy_terms_during_meta_updates( $meta_id, $object_id, $meta_key, $meta_value ) { if ( '_edit_lock' === strtolower( $meta_key ) ) { return; } // These are the unprefixed meta keys that have overlapping taxonomies. $overlapping_keys = self::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 ), true ) ) { // No. return; } $taxonomy = $overlapping_keys[ $unprefixed ]; /** * If we are in the Availability taxonomy, the end of this method * appends terms instead of replacing so that a vehicle can be both * For Sale and Wholesale. If the $meta_value is For Sale, Sold or Sale * Pending, we need to remove the other of these terms. */ if ( 'availability' === strtolower( $unprefixed ) && ! empty( $meta_value ) && in_array( INVP::sluggify( $meta_value ), array( 'for-sale', 'sold', 'sale-pending' ), true ) ) { $remove_term_ids = get_terms( array( 'taxonomy' => $taxonomy, 'fields' => 'ids', 'slug' => array_diff( array( 'for-sale', 'sold', 'sale-pending' ), array( INVP::sluggify( $meta_value ) ) ), ) ); $this->hooks_remove(); wp_remove_object_terms( $object_id, $remove_term_ids, $taxonomy ); $this->hooks_add(); } // if $meta_value is empty or we have deleted a meta value, remove a term. global $action; if ( empty( $meta_value ) || 'delete-meta' === $action ) { // remove a term actually. $terms = array(); if ( 'availability' === strtolower( $taxonomy ) ) { $terms = wp_get_object_terms( $object_id, $taxonomy ); $term_count = count( $terms ); for ( $t = 0; $t < $term_count; $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' === strtolower( $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 ( ! is_wp_error( $term_id_array ) && ! 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/Sale Pending and Wholesale, so append in that * taxonomy. */ $this->hooks_remove(); wp_set_object_terms( $object_id, $term->term_id, $taxonomy, ( 'availability' === strtolower( $taxonomy ) ) ); $this->hooks_add(); }
Expand full source codeCollapse full source codeView on Github