Edit File by line
/home/barbar84/www/wp-inclu...
File: taxonomy.php
*
[2500] Fix | Delete
* @since 2.3.0
[2501] Fix | Delete
*
[2502] Fix | Delete
* @param int $term_id Term ID.
[2503] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2504] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2505] Fix | Delete
*/
[2506] Fix | Delete
do_action( 'created_term', $term_id, $tt_id, $taxonomy );
[2507] Fix | Delete
[2508] Fix | Delete
/**
[2509] Fix | Delete
* Fires after a new term in a specific taxonomy is created, and after the term
[2510] Fix | Delete
* cache has been cleaned.
[2511] Fix | Delete
*
[2512] Fix | Delete
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
[2513] Fix | Delete
*
[2514] Fix | Delete
* @since 2.3.0
[2515] Fix | Delete
*
[2516] Fix | Delete
* @param int $term_id Term ID.
[2517] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2518] Fix | Delete
*/
[2519] Fix | Delete
do_action( "created_{$taxonomy}", $term_id, $tt_id );
[2520] Fix | Delete
[2521] Fix | Delete
/**
[2522] Fix | Delete
* Fires after a term has been saved, and the term cache has been cleared.
[2523] Fix | Delete
*
[2524] Fix | Delete
* The {@see 'saved_$taxonomy'} hook is also available for targeting a specific
[2525] Fix | Delete
* taxonomy.
[2526] Fix | Delete
*
[2527] Fix | Delete
* @since 5.5.0
[2528] Fix | Delete
*
[2529] Fix | Delete
* @param int $term_id Term ID.
[2530] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2531] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2532] Fix | Delete
* @param bool $update Whether this is an existing term being updated.
[2533] Fix | Delete
*/
[2534] Fix | Delete
do_action( 'saved_term', $term_id, $tt_id, $taxonomy, false );
[2535] Fix | Delete
[2536] Fix | Delete
/**
[2537] Fix | Delete
* Fires after a term in a specific taxonomy has been saved, and the term
[2538] Fix | Delete
* cache has been cleared.
[2539] Fix | Delete
*
[2540] Fix | Delete
* The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
[2541] Fix | Delete
*
[2542] Fix | Delete
* @since 5.5.0
[2543] Fix | Delete
*
[2544] Fix | Delete
* @param int $term_id Term ID.
[2545] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2546] Fix | Delete
* @param bool $update Whether this is an existing term being updated.
[2547] Fix | Delete
*/
[2548] Fix | Delete
do_action( "saved_{$taxonomy}", $term_id, $tt_id, false );
[2549] Fix | Delete
[2550] Fix | Delete
return array(
[2551] Fix | Delete
'term_id' => $term_id,
[2552] Fix | Delete
'term_taxonomy_id' => $tt_id,
[2553] Fix | Delete
);
[2554] Fix | Delete
}
[2555] Fix | Delete
[2556] Fix | Delete
/**
[2557] Fix | Delete
* Create Term and Taxonomy Relationships.
[2558] Fix | Delete
*
[2559] Fix | Delete
* Relates an object (post, link etc) to a term and taxonomy type. Creates the
[2560] Fix | Delete
* term and taxonomy relationship if it doesn't already exist. Creates a term if
[2561] Fix | Delete
* it doesn't exist (using the slug).
[2562] Fix | Delete
*
[2563] Fix | Delete
* A relationship means that the term is grouped in or belongs to the taxonomy.
[2564] Fix | Delete
* A term has no meaning until it is given context by defining which taxonomy it
[2565] Fix | Delete
* exists under.
[2566] Fix | Delete
*
[2567] Fix | Delete
* @since 2.3.0
[2568] Fix | Delete
*
[2569] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2570] Fix | Delete
*
[2571] Fix | Delete
* @param int $object_id The object to relate to.
[2572] Fix | Delete
* @param string|int|array $terms A single term slug, single term ID, or array of either term slugs or IDs.
[2573] Fix | Delete
* Will replace all existing related terms in this taxonomy. Passing an
[2574] Fix | Delete
* empty value will remove all related terms.
[2575] Fix | Delete
* @param string $taxonomy The context in which to relate the term to the object.
[2576] Fix | Delete
* @param bool $append Optional. If false will delete difference of terms. Default false.
[2577] Fix | Delete
* @return array|WP_Error Term taxonomy IDs of the affected terms or WP_Error on failure.
[2578] Fix | Delete
*/
[2579] Fix | Delete
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
[2580] Fix | Delete
global $wpdb;
[2581] Fix | Delete
[2582] Fix | Delete
$object_id = (int) $object_id;
[2583] Fix | Delete
[2584] Fix | Delete
if ( ! taxonomy_exists( $taxonomy ) ) {
[2585] Fix | Delete
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
[2586] Fix | Delete
}
[2587] Fix | Delete
[2588] Fix | Delete
if ( ! is_array( $terms ) ) {
[2589] Fix | Delete
$terms = array( $terms );
[2590] Fix | Delete
}
[2591] Fix | Delete
[2592] Fix | Delete
if ( ! $append ) {
[2593] Fix | Delete
$old_tt_ids = wp_get_object_terms(
[2594] Fix | Delete
$object_id,
[2595] Fix | Delete
$taxonomy,
[2596] Fix | Delete
array(
[2597] Fix | Delete
'fields' => 'tt_ids',
[2598] Fix | Delete
'orderby' => 'none',
[2599] Fix | Delete
'update_term_meta_cache' => false,
[2600] Fix | Delete
)
[2601] Fix | Delete
);
[2602] Fix | Delete
} else {
[2603] Fix | Delete
$old_tt_ids = array();
[2604] Fix | Delete
}
[2605] Fix | Delete
[2606] Fix | Delete
$tt_ids = array();
[2607] Fix | Delete
$term_ids = array();
[2608] Fix | Delete
$new_tt_ids = array();
[2609] Fix | Delete
[2610] Fix | Delete
foreach ( (array) $terms as $term ) {
[2611] Fix | Delete
if ( '' === trim( $term ) ) {
[2612] Fix | Delete
continue;
[2613] Fix | Delete
}
[2614] Fix | Delete
[2615] Fix | Delete
$term_info = term_exists( $term, $taxonomy );
[2616] Fix | Delete
[2617] Fix | Delete
if ( ! $term_info ) {
[2618] Fix | Delete
// Skip if a non-existent term ID is passed.
[2619] Fix | Delete
if ( is_int( $term ) ) {
[2620] Fix | Delete
continue;
[2621] Fix | Delete
}
[2622] Fix | Delete
[2623] Fix | Delete
$term_info = wp_insert_term( $term, $taxonomy );
[2624] Fix | Delete
}
[2625] Fix | Delete
[2626] Fix | Delete
if ( is_wp_error( $term_info ) ) {
[2627] Fix | Delete
return $term_info;
[2628] Fix | Delete
}
[2629] Fix | Delete
[2630] Fix | Delete
$term_ids[] = $term_info['term_id'];
[2631] Fix | Delete
$tt_id = $term_info['term_taxonomy_id'];
[2632] Fix | Delete
$tt_ids[] = $tt_id;
[2633] Fix | Delete
[2634] Fix | Delete
if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) ) {
[2635] Fix | Delete
continue;
[2636] Fix | Delete
}
[2637] Fix | Delete
[2638] Fix | Delete
/**
[2639] Fix | Delete
* Fires immediately before an object-term relationship is added.
[2640] Fix | Delete
*
[2641] Fix | Delete
* @since 2.9.0
[2642] Fix | Delete
* @since 4.7.0 Added the `$taxonomy` parameter.
[2643] Fix | Delete
*
[2644] Fix | Delete
* @param int $object_id Object ID.
[2645] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2646] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2647] Fix | Delete
*/
[2648] Fix | Delete
do_action( 'add_term_relationship', $object_id, $tt_id, $taxonomy );
[2649] Fix | Delete
[2650] Fix | Delete
$wpdb->insert(
[2651] Fix | Delete
$wpdb->term_relationships,
[2652] Fix | Delete
array(
[2653] Fix | Delete
'object_id' => $object_id,
[2654] Fix | Delete
'term_taxonomy_id' => $tt_id,
[2655] Fix | Delete
)
[2656] Fix | Delete
);
[2657] Fix | Delete
[2658] Fix | Delete
/**
[2659] Fix | Delete
* Fires immediately after an object-term relationship is added.
[2660] Fix | Delete
*
[2661] Fix | Delete
* @since 2.9.0
[2662] Fix | Delete
* @since 4.7.0 Added the `$taxonomy` parameter.
[2663] Fix | Delete
*
[2664] Fix | Delete
* @param int $object_id Object ID.
[2665] Fix | Delete
* @param int $tt_id Term taxonomy ID.
[2666] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2667] Fix | Delete
*/
[2668] Fix | Delete
do_action( 'added_term_relationship', $object_id, $tt_id, $taxonomy );
[2669] Fix | Delete
[2670] Fix | Delete
$new_tt_ids[] = $tt_id;
[2671] Fix | Delete
}
[2672] Fix | Delete
[2673] Fix | Delete
if ( $new_tt_ids ) {
[2674] Fix | Delete
wp_update_term_count( $new_tt_ids, $taxonomy );
[2675] Fix | Delete
}
[2676] Fix | Delete
[2677] Fix | Delete
if ( ! $append ) {
[2678] Fix | Delete
$delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
[2679] Fix | Delete
[2680] Fix | Delete
if ( $delete_tt_ids ) {
[2681] Fix | Delete
$in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
[2682] Fix | Delete
$delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
[2683] Fix | Delete
$delete_term_ids = array_map( 'intval', $delete_term_ids );
[2684] Fix | Delete
[2685] Fix | Delete
$remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
[2686] Fix | Delete
if ( is_wp_error( $remove ) ) {
[2687] Fix | Delete
return $remove;
[2688] Fix | Delete
}
[2689] Fix | Delete
}
[2690] Fix | Delete
}
[2691] Fix | Delete
[2692] Fix | Delete
$t = get_taxonomy( $taxonomy );
[2693] Fix | Delete
[2694] Fix | Delete
if ( ! $append && isset( $t->sort ) && $t->sort ) {
[2695] Fix | Delete
$values = array();
[2696] Fix | Delete
$term_order = 0;
[2697] Fix | Delete
[2698] Fix | Delete
$final_tt_ids = wp_get_object_terms(
[2699] Fix | Delete
$object_id,
[2700] Fix | Delete
$taxonomy,
[2701] Fix | Delete
array(
[2702] Fix | Delete
'fields' => 'tt_ids',
[2703] Fix | Delete
'update_term_meta_cache' => false,
[2704] Fix | Delete
)
[2705] Fix | Delete
);
[2706] Fix | Delete
[2707] Fix | Delete
foreach ( $tt_ids as $tt_id ) {
[2708] Fix | Delete
if ( in_array( (int) $tt_id, $final_tt_ids, true ) ) {
[2709] Fix | Delete
$values[] = $wpdb->prepare( '(%d, %d, %d)', $object_id, $tt_id, ++$term_order );
[2710] Fix | Delete
}
[2711] Fix | Delete
}
[2712] Fix | Delete
[2713] Fix | Delete
if ( $values ) {
[2714] Fix | Delete
if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . implode( ',', $values ) . ' ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)' ) ) {
[2715] Fix | Delete
return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database.' ), $wpdb->last_error );
[2716] Fix | Delete
}
[2717] Fix | Delete
}
[2718] Fix | Delete
}
[2719] Fix | Delete
[2720] Fix | Delete
wp_cache_delete( $object_id, $taxonomy . '_relationships' );
[2721] Fix | Delete
wp_cache_delete( 'last_changed', 'terms' );
[2722] Fix | Delete
[2723] Fix | Delete
/**
[2724] Fix | Delete
* Fires after an object's terms have been set.
[2725] Fix | Delete
*
[2726] Fix | Delete
* @since 2.8.0
[2727] Fix | Delete
*
[2728] Fix | Delete
* @param int $object_id Object ID.
[2729] Fix | Delete
* @param array $terms An array of object terms.
[2730] Fix | Delete
* @param array $tt_ids An array of term taxonomy IDs.
[2731] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2732] Fix | Delete
* @param bool $append Whether to append new terms to the old terms.
[2733] Fix | Delete
* @param array $old_tt_ids Old array of term taxonomy IDs.
[2734] Fix | Delete
*/
[2735] Fix | Delete
do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids );
[2736] Fix | Delete
[2737] Fix | Delete
return $tt_ids;
[2738] Fix | Delete
}
[2739] Fix | Delete
[2740] Fix | Delete
/**
[2741] Fix | Delete
* Add term(s) associated with a given object.
[2742] Fix | Delete
*
[2743] Fix | Delete
* @since 3.6.0
[2744] Fix | Delete
*
[2745] Fix | Delete
* @param int $object_id The ID of the object to which the terms will be added.
[2746] Fix | Delete
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to add.
[2747] Fix | Delete
* @param array|string $taxonomy Taxonomy name.
[2748] Fix | Delete
* @return array|WP_Error Term taxonomy IDs of the affected terms.
[2749] Fix | Delete
*/
[2750] Fix | Delete
function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
[2751] Fix | Delete
return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
[2752] Fix | Delete
}
[2753] Fix | Delete
[2754] Fix | Delete
/**
[2755] Fix | Delete
* Remove term(s) associated with a given object.
[2756] Fix | Delete
*
[2757] Fix | Delete
* @since 3.6.0
[2758] Fix | Delete
*
[2759] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2760] Fix | Delete
*
[2761] Fix | Delete
* @param int $object_id The ID of the object from which the terms will be removed.
[2762] Fix | Delete
* @param string|int|array $terms The slug(s) or ID(s) of the term(s) to remove.
[2763] Fix | Delete
* @param array|string $taxonomy Taxonomy name.
[2764] Fix | Delete
* @return bool|WP_Error True on success, false or WP_Error on failure.
[2765] Fix | Delete
*/
[2766] Fix | Delete
function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
[2767] Fix | Delete
global $wpdb;
[2768] Fix | Delete
[2769] Fix | Delete
$object_id = (int) $object_id;
[2770] Fix | Delete
[2771] Fix | Delete
if ( ! taxonomy_exists( $taxonomy ) ) {
[2772] Fix | Delete
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
[2773] Fix | Delete
}
[2774] Fix | Delete
[2775] Fix | Delete
if ( ! is_array( $terms ) ) {
[2776] Fix | Delete
$terms = array( $terms );
[2777] Fix | Delete
}
[2778] Fix | Delete
[2779] Fix | Delete
$tt_ids = array();
[2780] Fix | Delete
[2781] Fix | Delete
foreach ( (array) $terms as $term ) {
[2782] Fix | Delete
if ( '' === trim( $term ) ) {
[2783] Fix | Delete
continue;
[2784] Fix | Delete
}
[2785] Fix | Delete
[2786] Fix | Delete
$term_info = term_exists( $term, $taxonomy );
[2787] Fix | Delete
if ( ! $term_info ) {
[2788] Fix | Delete
// Skip if a non-existent term ID is passed.
[2789] Fix | Delete
if ( is_int( $term ) ) {
[2790] Fix | Delete
continue;
[2791] Fix | Delete
}
[2792] Fix | Delete
}
[2793] Fix | Delete
[2794] Fix | Delete
if ( is_wp_error( $term_info ) ) {
[2795] Fix | Delete
return $term_info;
[2796] Fix | Delete
}
[2797] Fix | Delete
[2798] Fix | Delete
$tt_ids[] = $term_info['term_taxonomy_id'];
[2799] Fix | Delete
}
[2800] Fix | Delete
[2801] Fix | Delete
if ( $tt_ids ) {
[2802] Fix | Delete
$in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
[2803] Fix | Delete
[2804] Fix | Delete
/**
[2805] Fix | Delete
* Fires immediately before an object-term relationship is deleted.
[2806] Fix | Delete
*
[2807] Fix | Delete
* @since 2.9.0
[2808] Fix | Delete
* @since 4.7.0 Added the `$taxonomy` parameter.
[2809] Fix | Delete
*
[2810] Fix | Delete
* @param int $object_id Object ID.
[2811] Fix | Delete
* @param array $tt_ids An array of term taxonomy IDs.
[2812] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2813] Fix | Delete
*/
[2814] Fix | Delete
do_action( 'delete_term_relationships', $object_id, $tt_ids, $taxonomy );
[2815] Fix | Delete
[2816] Fix | Delete
$deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
[2817] Fix | Delete
[2818] Fix | Delete
wp_cache_delete( $object_id, $taxonomy . '_relationships' );
[2819] Fix | Delete
wp_cache_delete( 'last_changed', 'terms' );
[2820] Fix | Delete
[2821] Fix | Delete
/**
[2822] Fix | Delete
* Fires immediately after an object-term relationship is deleted.
[2823] Fix | Delete
*
[2824] Fix | Delete
* @since 2.9.0
[2825] Fix | Delete
* @since 4.7.0 Added the `$taxonomy` parameter.
[2826] Fix | Delete
*
[2827] Fix | Delete
* @param int $object_id Object ID.
[2828] Fix | Delete
* @param array $tt_ids An array of term taxonomy IDs.
[2829] Fix | Delete
* @param string $taxonomy Taxonomy slug.
[2830] Fix | Delete
*/
[2831] Fix | Delete
do_action( 'deleted_term_relationships', $object_id, $tt_ids, $taxonomy );
[2832] Fix | Delete
[2833] Fix | Delete
wp_update_term_count( $tt_ids, $taxonomy );
[2834] Fix | Delete
[2835] Fix | Delete
return (bool) $deleted;
[2836] Fix | Delete
}
[2837] Fix | Delete
[2838] Fix | Delete
return false;
[2839] Fix | Delete
}
[2840] Fix | Delete
[2841] Fix | Delete
/**
[2842] Fix | Delete
* Will make slug unique, if it isn't already.
[2843] Fix | Delete
*
[2844] Fix | Delete
* The `$slug` has to be unique global to every taxonomy, meaning that one
[2845] Fix | Delete
* taxonomy term can't have a matching slug with another taxonomy term. Each
[2846] Fix | Delete
* slug has to be globally unique for every taxonomy.
[2847] Fix | Delete
*
[2848] Fix | Delete
* The way this works is that if the taxonomy that the term belongs to is
[2849] Fix | Delete
* hierarchical and has a parent, it will append that parent to the $slug.
[2850] Fix | Delete
*
[2851] Fix | Delete
* If that still doesn't return a unique slug, then it tries to append a number
[2852] Fix | Delete
* until it finds a number that is truly unique.
[2853] Fix | Delete
*
[2854] Fix | Delete
* The only purpose for `$term` is for appending a parent, if one exists.
[2855] Fix | Delete
*
[2856] Fix | Delete
* @since 2.3.0
[2857] Fix | Delete
*
[2858] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2859] Fix | Delete
*
[2860] Fix | Delete
* @param string $slug The string that will be tried for a unique slug.
[2861] Fix | Delete
* @param object $term The term object that the `$slug` will belong to.
[2862] Fix | Delete
* @return string Will return a true unique slug.
[2863] Fix | Delete
*/
[2864] Fix | Delete
function wp_unique_term_slug( $slug, $term ) {
[2865] Fix | Delete
global $wpdb;
[2866] Fix | Delete
[2867] Fix | Delete
$needs_suffix = true;
[2868] Fix | Delete
$original_slug = $slug;
[2869] Fix | Delete
[2870] Fix | Delete
// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
[2871] Fix | Delete
if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
[2872] Fix | Delete
$needs_suffix = false;
[2873] Fix | Delete
}
[2874] Fix | Delete
[2875] Fix | Delete
/*
[2876] Fix | Delete
* If the taxonomy supports hierarchy and the term has a parent, make the slug unique
[2877] Fix | Delete
* by incorporating parent slugs.
[2878] Fix | Delete
*/
[2879] Fix | Delete
$parent_suffix = '';
[2880] Fix | Delete
if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
[2881] Fix | Delete
$the_parent = $term->parent;
[2882] Fix | Delete
while ( ! empty( $the_parent ) ) {
[2883] Fix | Delete
$parent_term = get_term( $the_parent, $term->taxonomy );
[2884] Fix | Delete
if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
[2885] Fix | Delete
break;
[2886] Fix | Delete
}
[2887] Fix | Delete
$parent_suffix .= '-' . $parent_term->slug;
[2888] Fix | Delete
if ( ! term_exists( $slug . $parent_suffix ) ) {
[2889] Fix | Delete
break;
[2890] Fix | Delete
}
[2891] Fix | Delete
[2892] Fix | Delete
if ( empty( $parent_term->parent ) ) {
[2893] Fix | Delete
break;
[2894] Fix | Delete
}
[2895] Fix | Delete
$the_parent = $parent_term->parent;
[2896] Fix | Delete
}
[2897] Fix | Delete
}
[2898] Fix | Delete
[2899] Fix | Delete
// If we didn't get a unique slug, try appending a number to make it unique.
[2900] Fix | Delete
[2901] Fix | Delete
/**
[2902] Fix | Delete
* Filters whether the proposed unique term slug is bad.
[2903] Fix | Delete
*
[2904] Fix | Delete
* @since 4.3.0
[2905] Fix | Delete
*
[2906] Fix | Delete
* @param bool $needs_suffix Whether the slug needs to be made unique with a suffix.
[2907] Fix | Delete
* @param string $slug The slug.
[2908] Fix | Delete
* @param object $term Term object.
[2909] Fix | Delete
*/
[2910] Fix | Delete
if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
[2911] Fix | Delete
if ( $parent_suffix ) {
[2912] Fix | Delete
$slug .= $parent_suffix;
[2913] Fix | Delete
}
[2914] Fix | Delete
[2915] Fix | Delete
if ( ! empty( $term->term_id ) ) {
[2916] Fix | Delete
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
[2917] Fix | Delete
} else {
[2918] Fix | Delete
$query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
[2919] Fix | Delete
}
[2920] Fix | Delete
[2921] Fix | Delete
if ( $wpdb->get_var( $query ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
[2922] Fix | Delete
$num = 2;
[2923] Fix | Delete
do {
[2924] Fix | Delete
$alt_slug = $slug . "-$num";
[2925] Fix | Delete
$num++;
[2926] Fix | Delete
$slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
[2927] Fix | Delete
} while ( $slug_check );
[2928] Fix | Delete
$slug = $alt_slug;
[2929] Fix | Delete
}
[2930] Fix | Delete
}
[2931] Fix | Delete
[2932] Fix | Delete
/**
[2933] Fix | Delete
* Filters the unique term slug.
[2934] Fix | Delete
*
[2935] Fix | Delete
* @since 4.3.0
[2936] Fix | Delete
*
[2937] Fix | Delete
* @param string $slug Unique term slug.
[2938] Fix | Delete
* @param object $term Term object.
[2939] Fix | Delete
* @param string $original_slug Slug originally passed to the function for testing.
[2940] Fix | Delete
*/
[2941] Fix | Delete
return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );
[2942] Fix | Delete
}
[2943] Fix | Delete
[2944] Fix | Delete
/**
[2945] Fix | Delete
* Update term based on arguments provided.
[2946] Fix | Delete
*
[2947] Fix | Delete
* The `$args` will indiscriminately override all values with the same field name.
[2948] Fix | Delete
* Care must be taken to not override important information need to update or
[2949] Fix | Delete
* update will fail (or perhaps create a new term, neither would be acceptable).
[2950] Fix | Delete
*
[2951] Fix | Delete
* Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
[2952] Fix | Delete
* defined in `$args` already.
[2953] Fix | Delete
*
[2954] Fix | Delete
* 'alias_of' will create a term group, if it doesn't already exist, and
[2955] Fix | Delete
* update it for the `$term`.
[2956] Fix | Delete
*
[2957] Fix | Delete
* If the 'slug' argument in `$args` is missing, then the 'name' will be used.
[2958] Fix | Delete
* If you set 'slug' and it isn't unique, then a WP_Error is returned.
[2959] Fix | Delete
* If you don't pass any slug, then a unique one will be created.
[2960] Fix | Delete
*
[2961] Fix | Delete
* @since 2.3.0
[2962] Fix | Delete
*
[2963] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[2964] Fix | Delete
*
[2965] Fix | Delete
* @param int $term_id The ID of the term.
[2966] Fix | Delete
* @param string $taxonomy The taxonomy of the term.
[2967] Fix | Delete
* @param array|string $args {
[2968] Fix | Delete
* Optional. Array or string of arguments for updating a term.
[2969] Fix | Delete
*
[2970] Fix | Delete
* @type string $alias_of Slug of the term to make this term an alias of.
[2971] Fix | Delete
* Default empty string. Accepts a term slug.
[2972] Fix | Delete
* @type string $description The term description. Default empty string.
[2973] Fix | Delete
* @type int $parent The id of the parent term. Default 0.
[2974] Fix | Delete
* @type string $slug The term slug to use. Default empty string.
[2975] Fix | Delete
* }
[2976] Fix | Delete
* @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
[2977] Fix | Delete
* WP_Error otherwise.
[2978] Fix | Delete
*/
[2979] Fix | Delete
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
[2980] Fix | Delete
global $wpdb;
[2981] Fix | Delete
[2982] Fix | Delete
if ( ! taxonomy_exists( $taxonomy ) ) {
[2983] Fix | Delete
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
[2984] Fix | Delete
}
[2985] Fix | Delete
[2986] Fix | Delete
$term_id = (int) $term_id;
[2987] Fix | Delete
[2988] Fix | Delete
// First, get all of the original args.
[2989] Fix | Delete
$term = get_term( $term_id, $taxonomy );
[2990] Fix | Delete
[2991] Fix | Delete
if ( is_wp_error( $term ) ) {
[2992] Fix | Delete
return $term;
[2993] Fix | Delete
}
[2994] Fix | Delete
[2995] Fix | Delete
if ( ! $term ) {
[2996] Fix | Delete
return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
[2997] Fix | Delete
}
[2998] Fix | Delete
[2999] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function