Introduce wp_insert_term_duplicate_term_check filter.

This filter allows plugins to intervene in the duplicate-term check
that takes place at the time of term creation. See [30238], #22023.

Props strategio.
Fixes #43271.

git-svn-id: https://develop.svn.wordpress.org/trunk@43570 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2018-08-15 19:49:50 +00:00
parent cb3d1777b1
commit fed48ba3fe

View File

@ -2312,7 +2312,26 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
* and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks * and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks
* are not fired. * are not fired.
*/ */
$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, tt.term_taxonomy_id FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) ); $duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, t.slug, tt.term_taxonomy_id, tt.taxonomy FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) );
/**
* Filters the duplicate term check that takes place during term creation.
*
* Term parent+taxonomy+slug combinations are meant to be unique, and wp_insert_term()
* performs a last-minute confirmation of this uniqueness before allowing a new term
* to be created. Plugins with different uniqueness requirements may use this filter
* to bypass or modify the duplicate-term check.
*
* @since 5.0.0
*
* @param object $duplicate_term Duplicate term row from terms table, if found.
* @param string $term Term being inserted.
* @param string $taxonomy Taxonomy name.
* @param array $args Term arguments passed to the function.
* @param int $tt_id term_taxonomy_id for the newly created term.
*/
$duplicate_term = apply_filters( 'wp_insert_term_duplicate_term_check', $duplicate_term, $term, $taxonomy, $args, $tt_id );
if ( $duplicate_term ) { if ( $duplicate_term ) {
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) ); $wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) );
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );