function wc1c_wp_unique_term_slug($slug, $term, $original_slug) { if (mb_strlen($slug) <= 200) { return $slug; } do { $slug = urldecode($slug); $slug = mb_substr($slug, 0, mb_strlen($slug) - 1); $slug = urlencode($slug); $slug = wp_unique_term_slug($slug, $term); } while (mb_strlen($slug) > 200); return $slug; }
public function test_nonunique_slug_in_same_hierarchical_taxonomy_at_different_level_of_hierarchy_should_be_suffixed_with_number() { $parent = $this->factory->term->create(array('taxonomy' => 'wptests_tax2', 'slug' => 'parent-term')); $term1 = $this->factory->term->create(array('taxonomy' => 'wptests_tax2', 'name' => 'bar', 'slug' => 'bar', 'parent' => $parent)); $term2 = $this->factory->term->create(array('taxonomy' => 'wptests_tax2', 'name' => 'foo', 'slug' => 'foo')); $term2_object = get_term($term2, 'wptests_tax2'); $actual = wp_unique_term_slug('bar', $term2_object); $this->assertEquals('bar-2', $actual); }
function wpmf_import_categories() { $option_import_taxo = get_option('_wpmf_import_notice_flag'); if (isset($option_import_taxo) && $option_import_taxo == 'yes') { die; } if ($_POST['doit'] === 'true') { $terms = get_terms('category', array('orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, 'child_of' => 0)); $termsRel = array('0' => 0); foreach ($terms as $term) { $inserted = wp_insert_term($term->name, 'wpmf-category', array('slug' => wp_unique_term_slug($term->slug, $term))); if (is_wp_error($inserted)) { wp_send_json($inserted->get_error_message()); } $termsRel[$term->term_id] = $inserted['term_id']; } foreach ($terms as $term) { wp_update_term($termsRel[$term->term_id], 'wpmf-category', array('parent' => $termsRel[$term->parent])); } //update attachments $attachments = get_posts(array('posts_per_page' => -1, 'post_type' => 'attachment')); foreach ($attachments as $attachment) { $terms = wp_get_post_terms($attachment->ID, 'category'); $termsArray = array(); foreach ($terms as $term) { $termsArray[] = $termsRel[$term->term_id]; } if ($termsArray != null) { wp_set_post_terms($attachment->ID, $termsArray, 'wpmf-category'); } } } if ($_POST['doit'] === 'true') { update_option('_wpmf_import_notice_flag', 'yes'); } else { update_option('_wpmf_import_notice_flag', 'no'); } die; }
/** * wp_update_term() - Update term based on arguments provided * * The $args will indiscriminately override all values with the same field name. Care * must be taken to not override important information need to update or update will * fail (or perhaps create a new term, neither would be acceptable). * * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not defined * in $args already. * * 'alias_of' will create a term group, if it doesn't already exist, and update it for * the $term. * * If the 'slug' argument in $args is missing, then the 'name' in $args will be used. * It should also be noted that if you set 'slug' and it isn't unique then a WP_Error * will be passed back. If you don't pass any slug, then a unique one will be created * for you. * * For what can be overrode in $args, check the term scheme can contain and stay away * from the term keys. * * @package WordPress * @subpackage Taxonomy * @since 2.3 * * @uses $wpdb * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice. * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term id and * taxonomy id. * * @param int $term The ID of the term * @param string $taxonomy The context in which to relate the term to the object. * @param array|string $args Overwrite term field values * @return array|WP_Error Returns Term ID and Taxonomy Term ID */ function wp_update_term( $term, $taxonomy, $args = array() ) { global $wpdb; if ( ! is_taxonomy($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); $term_id = (int) $term; // First, get all of the original args $term = get_term ($term_id, $taxonomy, ARRAY_A); // Escape data pulled from DB. $term = add_magic_quotes($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); extract($args, EXTR_SKIP); // expected_slashed ($name) $name = stripslashes($name); $description = stripslashes($description); $empty_slug = false; if ( empty($slug) ) { $empty_slug = true; $slug = sanitize_title($name); } if ( $alias_of ) { $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) ); if ( $alias->term_group ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); } } // Check for duplicate slug $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); if ( $id && ($id != $term_id) ) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ( $empty_slug || ( $parent != $term->parent) ) $slug = wp_unique_term_slug($slug, (object) $args); else return new WP_Error('duplicate_term_slug', sprintf(__('The slug "%s" is already in use by another term'), $slug)); } $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); if ( empty($slug) ) { $slug = sanitize_title($name, $term_id); $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); } $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); do_action("edit_term", $term_id, $tt_id); do_action("edit_$taxonomy", $term_id, $tt_id); $term_id = apply_filters('term_id_filter', $term_id, $tt_id); clean_term_cache($term_id, $taxonomy); do_action("edited_term", $term_id, $tt_id); do_action("edited_$taxonomy", $term_id, $tt_id); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); }
function wpsc_convert_variation_sets() { global $wpdb, $user_ID; $variation_sets = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_PRODUCT_VARIATIONS . "`"); $wpsc_update = WPSC_Update::get_instance(); foreach ((array) $variation_sets as $variation_set) { $wpsc_update->check_timeout(); $variation_set_id = wpsc_get_meta($variation_set->id, 'variation_set_id', 'wpsc_variation_set'); if (!is_numeric($variation_set_id) || $variation_set_id < 1) { $slug = sanitize_title($variation_set->name); $dummy_term = (object) array('taxonomy' => 'wpsc-variation', 'parent' => 0); $slug = wp_unique_term_slug($slug, $dummy_term); $new_variation_set = wp_insert_term($variation_set->name, 'wpsc-variation', array('parent' => 0, 'slug' => $slug)); if (!is_wp_error($new_variation_set)) { $variation_set_id = $new_variation_set['term_id']; } } if (!empty($variation_set_id) && is_numeric($variation_set_id)) { wpsc_update_meta($variation_set->id, 'variation_set_id', $variation_set_id, 'wpsc_variation_set'); $variations = $wpdb->get_results("SELECT * FROM `" . WPSC_TABLE_VARIATION_VALUES . "` WHERE `variation_id` IN ({$variation_set->id})"); foreach ((array) $variations as $variation) { $variation_id = wpsc_get_meta($variation->id, 'variation_id', 'wpsc_variation'); if (!is_numeric($variation_id) || $variation_id < 1) { $new_variation = wp_insert_term($variation->name, 'wpsc-variation', array('parent' => $variation_set_id)); if (!is_wp_error($new_variation)) { $variation_id = $new_variation['term_id']; } } if (is_numeric($variation_id)) { wpsc_update_meta($variation->id, 'variation_id', $variation_id, 'wpsc_variation'); } } } } }
function test_wp_unique_term_slug() { // set up test data $a = wp_insert_term( 'parent', $this->taxonomy ); $this->assertInternalType( 'array', $a ); $b = wp_insert_term( 'child', $this->taxonomy, array( 'parent' => $a['term_id'] ) ); $this->assertInternalType( 'array', $b ); $c = wp_insert_term( 'neighbor', $this->taxonomy ); $this->assertInternalType( 'array', $c ); $d = wp_insert_term( 'pet', $this->taxonomy, array( 'parent' => $c['term_id'] ) ); $this->assertInternalType( 'array', $c ); $a_term = get_term( $a['term_id'], $this->taxonomy ); $b_term = get_term( $b['term_id'], $this->taxonomy ); $c_term = get_term( $c['term_id'], $this->taxonomy ); $d_term = get_term( $d['term_id'], $this->taxonomy ); // a unique slug gets unchanged $this->assertEquals( 'unique-term', wp_unique_term_slug( 'unique-term', $c_term ) ); // a non-hierarchicial dupe gets suffixed with "-#" $this->assertEquals( 'parent-2', wp_unique_term_slug( 'parent', $c_term ) ); // a hierarchical dupe initially gets suffixed with its parent term $this->assertEquals( 'child-neighbor', wp_unique_term_slug( 'child', $d_term ) ); // a hierarchical dupe whose parent already contains the {term}-{parent term} // term gets suffixed with parent term name and then '-#' $e = wp_insert_term( 'child-neighbor', $this->taxonomy, array( 'parent' => $c['term_id'] ) ); $this->assertEquals( 'child-neighbor-2', wp_unique_term_slug( 'child', $d_term ) ); $f = wp_insert_term( 'foo', $this->taxonomy ); $this->assertInternalType( 'array', $f ); $f_term = get_term( $f['term_id'], $this->taxonomy ); $this->assertEquals( 'foo', $f_term->slug ); $this->assertEquals( 'foo', wp_unique_term_slug( 'foo', $f_term ) ); $g = wp_insert_term( 'foo', $this->taxonomy ); $this->assertInstanceOf( 'WP_Error', $g ); // clean up foreach ( array( $a, $b, $c, $d, $e, $f ) as $t ) $this->assertTrue( wp_delete_term( $t['term_id'], $this->taxonomy ) ); }
/** * Update term based on arguments provided. * * The $args will indiscriminately override all values with the same field name. * Care must be taken to not override important information need to update or * update will fail (or perhaps create a new term, neither would be acceptable). * * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not * defined in $args already. * * 'alias_of' will create a term group, if it doesn't already exist, and update * it for the $term. * * If the 'slug' argument in $args is missing, then the 'name' in $args will be * used. It should also be noted that if you set 'slug' and it isn't unique then * a WP_Error will be passed back. If you don't pass any slug, then a unique one * will be created for you. * * For what can be overrode in $args, check the term scheme can contain and stay * away from the term keys. * * @package WordPress * @subpackage Taxonomy * @since 2.3.0 * * @uses $wpdb * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice. * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term * id and taxonomy id. * * @param int $term_id The ID of the term * @param string $taxonomy The context in which to relate the term to the object. * @param array|string $args Overwrite term field values * @return array|WP_Error Returns Term ID and Taxonomy Term ID */ function wp_update_term($term_id, $taxonomy, $args = array()) { global $wpdb; if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } $term_id = (int) $term_id; // First, get all of the original args $term = get_term($term_id, $taxonomy, ARRAY_A); if (is_wp_error($term)) { return $term; } // Escape data pulled from DB. $term = wp_slash($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array('alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); extract($args, EXTR_SKIP); // expected_slashed ($name) $name = wp_unslash($name); $description = wp_unslash($description); if ('' == trim($name)) { return new WP_Error('empty_term_name', __('A name is required for this term')); } $empty_slug = false; if (empty($slug)) { $empty_slug = true; $slug = sanitize_title($name); } if ($alias_of) { $alias = $wpdb->get_row($wpdb->prepare("SELECT term_id, term_group FROM {$wpdb->terms} WHERE slug = %s", $alias_of)); if ($alias->term_group) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM {$wpdb->terms}") + 1; do_action('edit_terms', $alias->term_id); $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id)); do_action('edited_terms', $alias->term_id); } } // Check $parent to see if it will cause a hierarchy loop $parent = apply_filters('wp_update_term_parent', $parent, $term_id, $taxonomy, compact(array_keys($args)), $args); // Check for duplicate slug $id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} WHERE slug = %s", $slug)); if ($id && $id != $term_id) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ($empty_slug || $parent != $term['parent']) { $slug = wp_unique_term_slug($slug, (object) $args); } else { return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); } } do_action('edit_terms', $term_id); $wpdb->update($wpdb->terms, compact('name', 'slug', 'term_group'), compact('term_id')); if (empty($slug)) { $slug = sanitize_title($name, $term_id); $wpdb->update($wpdb->terms, compact('slug'), compact('term_id')); } do_action('edited_terms', $term_id); $tt_id = $wpdb->get_var($wpdb->prepare("SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} AS tt INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id)); do_action('edit_term_taxonomy', $tt_id, $taxonomy); $wpdb->update($wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent'), array('term_taxonomy_id' => $tt_id)); do_action('edited_term_taxonomy', $tt_id, $taxonomy); do_action("edit_term", $term_id, $tt_id, $taxonomy); do_action("edit_{$taxonomy}", $term_id, $tt_id); $term_id = apply_filters('term_id_filter', $term_id, $tt_id); clean_term_cache($term_id, $taxonomy); do_action("edited_term", $term_id, $tt_id, $taxonomy); do_action("edited_{$taxonomy}", $term_id, $tt_id); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); }
/** * Handles Product/Category slug editor * * @author Jonathan Davis * @since 1.1 * * @return void **/ public function edit_slug() { check_admin_referer('wp_ajax_shopp_edit_slug'); $defaults = array('slug' => false, 'type' => false, 'id' => false, 'title' => false); $p = array_merge($defaults, $_POST); extract($p); if (false === $slug || false === $id) { die('-1'); } switch ($type) { case 'category': $Category = new ProductCategory($_POST['id']); if ($slug == $Category->slug) { die('-1'); } $term = get_term($Category->id, $Category->taxonomy); $Category->slug = wp_unique_term_slug(sanitize_title_with_dashes($slug), $term); $Category->save(); echo apply_filters('editable_slug', $Category->slug); break; case 'product': $Product = new ShoppProduct($_POST['id']); if ($slug == $Product->slug) { die('-1'); } // Same as before? Nothing to do so bail // Regardless of the true post status, we'll pass 'publish' here to ensure we get a unique slug back $Product->slug = wp_unique_post_slug(sanitize_title_with_dashes($slug), $Product->id, 'publish', ShoppProduct::posttype(), 0); $Product->save(); echo apply_filters('editable_slug', $Product->slug); break; } exit; }
function sslp_staff_member_options_page() { // Get existing options $default_slug = get_option('_staff_listing_default_slug'); $default_name_singular = get_option('_staff_listing_default_name_singular'); $default_name_plural = get_option('_staff_listing_default_name_plural'); // Check Nonce and then update options if (!empty($_POST) && check_admin_referer('staff-member-options', 'staff-list-options')) { update_option('_staff_listing_custom_slug', wp_unique_term_slug($_POST["staff-listing-slug"], 'staff-member')); update_option('_staff_listing_custom_name_singular', $_POST["staff-listing-name-singular"]); update_option('_staff_listing_custom_name_plural', $_POST["staff-listing-name-plural"]); $custom_slug = stripslashes_deep(get_option('_staff_listing_custom_slug')); $custom_name_singular = stripslashes_deep(get_option('_staff_listing_custom_name_singular')); $custom_name_plural = stripslashes_deep(get_option('_staff_listing_custom_name_plural')); } else { $custom_slug = stripslashes_deep(get_option('_staff_listing_custom_slug')); $custom_name_singular = stripslashes_deep(get_option('_staff_listing_custom_name_singular')); $custom_name_plural = stripslashes_deep(get_option('_staff_listing_custom_name_plural')); } $output .= '<div class="wrap sslp-options">'; $output .= '<div id="icon-edit" class="icon32 icon32-posts-staff-member"><br></div><h2>' . __('Simple Staff List', 'simple-staff-list') . '</h2>'; $output .= '<h2>Options</h2>'; $output .= '<div>'; $output .= '<form method="post" action="">'; $output .= '<fieldset id="staff-listing-field-slug" class="sslp-fieldset">'; $output .= '<legend class="sslp-field-label">' . __('Staff Members URL Slug', 'simple-staff-list') . '</legend>'; $output .= '<input type="text" name="staff-listing-slug" value="' . $custom_slug . '"></fieldset>'; $output .= '<p>' . __('The slug used for building the staff members URL. The current URL is: ', 'simple-staff-list'); $output .= site_url($custom_slug) . '/'; $output .= '</p>'; $output .= '<fieldset id="staff-listing-field-name-plural" class="sslp-fieldset">'; $output .= '<legend class="sslp-field-label">' . __('Staff Member title', 'simple-staff-list') . '</legend>'; $output .= '<input type="text" name="staff-listing-name-plural" value="' . $custom_name_plural . '"></fieldset>'; $output .= '<p>' . __('The title that displays on the Staff Member archive page. Default is "Staff Members"', 'simple-staff-list') . '</p>'; $output .= '<fieldset id="staff-listing-field-name-singular" class="sslp-fieldset">'; $output .= '<legend class="sslp-field-label">' . __('Staff Member singular title', 'simple-staff-list') . '</legend>'; $output .= '<input type="text" name="staff-listing-name-singular" value="' . $custom_name_singular . '"></fieldset>'; $output .= '<p>' . __('The Staff Member taxonomy singular name. No need to change this unless you need to use the singular_name field in your theme. Default is "Staff Member"', 'simple-staff-list') . '</p>'; $output .= '<p><input type="submit" value="' . __('Save ALL Changes', 'simple-staff-list') . '" class="button button-primary button-large"></p><br /><br />'; $output .= wp_nonce_field('staff-member-options', 'staff-list-options'); $output .= '</form>'; $output .= '</div>'; echo $output; }
/** * Update term based on arguments provided. * * The $args will indiscriminately override all values with the same field name. * Care must be taken to not override important information need to update or * update will fail (or perhaps create a new term, neither would be acceptable). * * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not * defined in $args already. * * 'alias_of' will create a term group, if it doesn't already exist, and update * it for the $term. * * If the 'slug' argument in $args is missing, then the 'name' in $args will be * used. It should also be noted that if you set 'slug' and it isn't unique then * a WP_Error will be passed back. If you don't pass any slug, then a unique one * will be created for you. * * For what can be overrode in $args, check the term scheme can contain and stay * away from the term keys. * * @package WordPress * @subpackage Taxonomy * @since 2.3.0 * * @uses $wpdb * @uses do_action() Will call both 'edit_term' and 'edit_$taxonomy' twice. * @uses apply_filters() Will call the 'term_id_filter' filter and pass the term * id and taxonomy id. * * @param int $term_id The ID of the term * @param string $taxonomy The context in which to relate the term to the object. * @param array|string $args Overwrite term field values * @return array|WP_Error Returns Term ID and Taxonomy Term ID */ function wp_update_term($term_id, $taxonomy, $args = array(), $engelTuru) { global $wpdb; if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } $term_id = (int) $term_id; // First, get all of the original args $term = get_term($term_id, $taxonomy, ARRAY_A); if (is_wp_error($term)) { return $term; } // Escape data pulled from DB. $term = add_magic_quotes($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array('alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); extract($args, EXTR_SKIP); // expected_slashed ($name) $name = stripslashes($name); $description = stripslashes($description); if ('' == trim($name)) { return new WP_Error('empty_term_name', __('A name is required for this term')); } $empty_slug = false; if (empty($slug)) { $empty_slug = true; $slug = sanitize_title($name); } if ($alias_of) { $alias = $wpdb->get_row($wpdb->prepare("SELECT term_id, term_group FROM {$wpdb->terms} WHERE slug = %s", $alias_of)); if ($alias->term_group) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM {$wpdb->terms}") + 1; do_action('edit_terms', $alias->term_id); $wpdb->update($wpdb->terms, compact('term_group'), array('term_id' => $alias->term_id)); do_action('edited_terms', $alias->term_id); } } // Check $parent to see if it will cause a hierarchy loop $parent = apply_filters('wp_update_term_parent', $parent, $term_id, $taxonomy, compact(array_keys($args)), $args); // Check for duplicate slug $id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM {$wpdb->terms} WHERE slug = %s", $slug)); if ($id && $id != $term_id) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ($empty_slug || $parent != $term['parent']) { $slug = wp_unique_term_slug($slug, (object) $args); } else { return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); } } do_action('edit_terms', $term_id); $wpdb->update($wpdb->terms, compact('name', 'slug', 'term_group'), compact('term_id')); if (empty($slug)) { $slug = sanitize_title($name, $term_id); $wpdb->update($wpdb->terms, compact('slug'), compact('term_id')); } do_action('edited_terms', $term_id); $tt_id = $wpdb->get_var($wpdb->prepare("SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} AS tt INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id)); do_action('edit_term_taxonomy', $tt_id, $taxonomy); $wpdb->update($wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent'), array('term_taxonomy_id' => $tt_id)); do_action('edited_term_taxonomy', $tt_id, $taxonomy); do_action("edit_term", $term_id, $tt_id, $taxonomy); do_action("edit_{$taxonomy}", $term_id, $tt_id); $term_id = apply_filters('term_id_filter', $term_id, $tt_id); clean_term_cache($term_id, $taxonomy); do_action("edited_term", $term_id, $tt_id, $taxonomy); do_action("edited_{$taxonomy}", $term_id, $tt_id); // EKLEME KATEGORI DISABILITY GUNCELLEME //require_once('./dbconnect.php'); $con = mysql_connect("localhost", "root", ""); mysql_select_db("erisimdb", $con); $deleteSql = "DELETE FROM er_disability_av WHERE post_id = " . $term_id; mysql_query($deleteSql); for ($i = 0; $i < count($engelTuru); $i++) { $sql = "INSERT INTO er_disability_av (post_id,disability_id) VALUES(" . $term_id . "," . $engelTuru[$i] . ")"; mysql_query($sql); } mysql_close($con); // return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); }
/** * Create new the post term. * * @since 1.4.6 * @package GeoDirectory * * @param string $taxonomy Post taxonomy. * @param array $term_data { * Attributes of term data. * * @type string $name Term name. * @type string $slug Term slug. * @type string $description Term description. * @type string $top_description Term top description. * @type string $image Default Term image. * @type string $icon Default Term icon. * @type string $taxonomy Term taxonomy. * @type int $parent Term parent ID. * * } * @return int|bool Term id when success, false when fail. */ function geodir_imex_insert_term($taxonomy, $term_data) { if (empty($taxonomy) || empty($term_data)) { return false; } $term = isset($term_data['name']) && !empty($term_data['name']) ? $term_data['name'] : ''; $args = array(); $args['description'] = isset($term_data['description']) ? $term_data['description'] : ''; $args['slug'] = isset($term_data['slug']) ? $term_data['slug'] : ''; $args['parent'] = isset($term_data['parent']) ? (int) $term_data['parent'] : ''; if (!empty($args['slug']) && term_exists($args['slug'], $taxonomy) || empty($args['slug'])) { $term_args = array_merge($term_data, $args); $defaults = array('alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $term_args = wp_parse_args($term_args, $defaults); $term_args = sanitize_term($term_args, $taxonomy, 'db'); $args['slug'] = wp_unique_term_slug($args['slug'], (object) $term_args); } if (!empty($term)) { $result = wp_insert_term($term, $taxonomy, $args); if (!is_wp_error($result)) { return $term_id = isset($result['term_id']) ? $result['term_id'] : 0; } } return false; }
function wp_update_term($term, $taxonomy, $args = array()) { global $wpdb; if (!is_taxonomy($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } $term_id = (int) $term; // First, get all of the original args $term = get_term($term_id, $taxonomy, ARRAY_A); // Escape data pulled from DB. $term = add_magic_quotes($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array('alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); extract($args, EXTR_SKIP); $empty_slug = false; if (empty($slug)) { $empty_slug = true; $slug = sanitize_title($name); } if ($alias_of) { $alias = $wpdb->fetch_row("SELECT term_id, term_group FROM {$wpdb->terms} WHERE slug = '{$alias_of}'"); if ($alias->term_group) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM {$wpdb->terms} GROUP BY term_group") + 1; $wpdb->query("UPDATE {$wpdb->terms} SET term_group = {$term_group} WHERE term_id = {$alias->term_id}"); } } // Check for duplicate slug $id = $wpdb->get_var("SELECT term_id FROM {$wpdb->terms} WHERE slug = '{$slug}'"); if ($id && $id != $term_id) { // If an empty slug was passed, reset the slug to something unique. // Otherwise, bail. if ($empty_slug) { $slug = wp_unique_term_slug($slug, (object) $args); } else { return new WP_Error('duplicate_term_slug', sprintf(__('The slug "%s" is already in use by another term'), $slug)); } } $wpdb->query("UPDATE {$wpdb->terms} SET name = '{$name}', slug = '{$slug}', term_group = '{$term_group}' WHERE term_id = '{$term_id}'"); if (empty($slug)) { $slug = sanitize_title($name, $term_id); $wpdb->query("UPDATE {$wpdb->terms} SET slug = '{$slug}' WHERE term_id = '{$term_id}'"); } $tt_id = $wpdb->get_var("SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} AS tt INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = '{$taxonomy}' AND t.term_id = {$term_id}"); $wpdb->query("UPDATE {$wpdb->term_taxonomy} SET term_id = '{$term_id}', taxonomy = '{$taxonomy}', description = '{$description}', parent = '{$parent}' WHERE term_taxonomy_id = '{$tt_id}'"); do_action("edit_term", $term_id, $tt_id); do_action("edit_{$taxonomy}", $term_id, $tt_id); $term_id = apply_filters('term_id_filter', $term_id, $tt_id); clean_term_cache($term_id, $taxonomy); do_action("edited_term", $term_id, $tt_id); do_action("edited_{$taxonomy}", $term_id, $tt_id); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); }
function sync_term() { try { // return false if request method is empty if (empty($_REQUEST['method'])) { throw new Exception(__('There is an error occurred', ET_DOMAIN), 400); } $method = empty($_REQUEST['method']) ? '' : $_REQUEST['method']; $data = $_REQUEST['content']; switch ($method) { case 'delete': $term = $_REQUEST['content']['id']; $default = isset($_REQUEST['content']['default']) ? $_REQUEST['content']['default'] : false; $taxonomy = $_REQUEST['content']['tax']; // check if category has children or not $children = get_terms($taxonomy, array('parent' => $term, 'hide_empty' => false)); if (!empty($children)) { throw new Exception(__('You cannot delete a parent category. You need to delete its sub-categories first.', ET_DOMAIN)); } // delete $result = $this->tax->delete($term, $taxonomy, $default); if (is_wp_error($result)) { throw new Exception($result->get_error_message()); } else { if ($result) { $resp = array('success' => true); } else { throw new Exception(__("Can't delete category", ET_DOMAIN)); } } break; case 'create': $term = $_POST['content']['name']; $args = array('color' => 0, 'icon' => 'fa-map-marker'); if (empty($term)) { throw new Exception(__('Category name is required', ET_DOMAIN)); } if (isset($_REQUEST['content']['color'])) { $args['color'] = $_REQUEST['content']['color']; } if (isset($_REQUEST['content']['icon'])) { $args['icon'] = $_REQUEST['content']['icon']; } if (isset($_REQUEST['content']['parent'])) { $args['parent'] = $_REQUEST['content']['parent']; } if (isset($_REQUEST['content']['tax'])) { $args['taxonomy'] = $_REQUEST['content']['tax']; } $result = $this->tax->create($term, $args); if (is_wp_error($result)) { throw new Exception($result->get_error_message()); } else { $data = get_term($result['term_id'], $args['taxonomy']); $data->color = $this->tax->get_term_color($result['term_id'], $args['taxonomy']); $data->icon = $this->tax->get_term_icon($result['term_id'], $args['taxonomy']); $resp = array('success' => true, 'data' => array('term' => $data)); } break; case 'update': $term = $_REQUEST['content']['id']; $args = array(); if (empty($term)) { throw new Exception(__("Cannot find category", ET_DOMAIN)); } if (isset($_REQUEST['content']['name'])) { $args['name'] = $_REQUEST['content']['name']; } if (isset($_REQUEST['content']['color'])) { $args['color'] = $_REQUEST['content']['color']; } if (isset($_REQUEST['content']['icon'])) { $args['icon'] = $_REQUEST['content']['icon']; } if (isset($_REQUEST['content']['parent'])) { $args['parent'] = $_REQUEST['content']['parent']; } if (isset($_REQUEST['content']['tax'])) { $args['taxonomy'] = $_REQUEST['content']['tax']; } $term_data = get_term($term, $this->tax->taxonomy); $slug = sanitize_title($_REQUEST['content']['name']); $slug = wp_unique_term_slug($slug, (object) $term_data); if ($slug) { $args['slug'] = $slug; } $result = $this->tax->update($term, $args); if (is_wp_error($result)) { throw new Exception($result->get_error_message()); } else { $data = get_term($result['term_id'], $args['taxonomy']); $data->color = $this->tax->get_term_color($result['term_id'], $args['taxonomy']); $data->icon = $this->tax->get_term_icon($result['term_id'], $args['taxonomy']); $resp = array('success' => true, 'data' => array('term' => $data)); } break; case 'sort': $taxonomy = $_POST['content']['tax']; wp_parse_str($_POST['content']['order'], $order); $order = $order['tax']; $handle = new AE_Category(array('taxonomy' => $taxonomy)); $handle->sort($order, array('taxonomy' => $taxonomy)); $resp = array('success' => true, 'data' => array('order' => $order)); break; case 'changeColor': $resp = $this->tax->change_color($data); break; case 'changeIcon': $resp = $this->tax->change_icon($data); break; default: throw new Exception(__('There is an error occurred', ET_DOMAIN), 400); break; } // refresh sorted job categories } catch (Exception $e) { $resp = build_error_ajax_response(array(), $e->getMessage()); } wp_send_json($resp); }
private function string2slug($string) { $slug = strtolower($string); $slug = sanitize_title($slug); $slug = @wp_unique_term_slug($slug); return $slug; }
function ipin_edit() { $nonce = $_POST['nonce']; if (!wp_verify_nonce($nonce, 'ajax-nonce')) { die; } do_action('ipin_before_edit_pin', $_POST); global $user_ID; $postinfo = get_post(intval($_POST['postdata_pid']), ARRAY_A); $user_id = $postinfo['post_author']; if (!(current_user_can('administrator') || current_user_can('editor') || $user_id == $user_ID)) { die; } //Get board info $board_add_new = sanitize_text_field($_POST['postdata_board_add_new']); $board_add_new_category = $_POST['postdata_board_add_new_category']; $board_parent_id = get_user_meta($user_id, '_Board Parent ID', true); if ($board_add_new !== '') { $board_children = get_term_children($board_parent_id, 'board'); $found = '0'; foreach ($board_children as $board_child) { $board_child_term = get_term_by('id', $board_child, 'board'); if (stripslashes(htmlspecialchars($board_add_new, ENT_NOQUOTES, 'UTF-8')) == $board_child_term->name) { $found = '1'; $found_board_id = $board_child_term->term_id; break; } } if ($found == '0') { $slug = wp_unique_term_slug($board_add_new . '__ipinboard', 'board'); //append __ipinboard to solve slug conflict with category and 0 in title if ($board_add_new_category == '-1') { $board_add_new_category = '1'; } $new_board_id = wp_insert_term($board_add_new, 'board', array('description' => $board_add_new_category, 'parent' => $board_parent_id, 'slug' => $slug)); $postdata_board = $new_board_id['term_id']; } else { $postdata_board = $found_board_id; } } else { $postdata_board = $_POST['postdata_board']; } //category ID is stored in the board description field $category_id = get_term_by('id', $postdata_board, 'board'); $post_id = intval($_POST['postdata_pid']); $edit_post = array(); $edit_post['ID'] = $post_id; $edit_post['post_category'] = array($category_id->description); $edit_post['post_name'] = ''; $allowed_html = array('a' => array('href' => true), 'em' => array(), 'blockquote' => array(), 'p' => array(), 'li' => array(), 'ol' => array(), 'strong' => array(), 'ul' => array()); if (of_get_option('htmltags') != 'enable') { unset($allowed_html); $allowed_html = array(); } if (of_get_option('form_title_desc') != 'separate') { $edit_post['post_title'] = balanceTags(wp_kses($_POST['postdata_title'], $allowed_html), true); } else { $edit_post['post_title'] = sanitize_text_field($_POST['postdata_title']); } $edit_post['post_content'] = balanceTags(wp_kses($_POST['postdata_content'], $allowed_html), true); remove_action('save_post', 'ipin_save_post', 50, 2); wp_update_post($edit_post); wp_set_post_terms($post_id, array($postdata_board), 'board'); //update postmeta for new post if ($_POST['postdata_source'] != '') { update_post_meta($post_id, '_Photo Source', esc_url($_POST['postdata_source'])); update_post_meta($post_id, '_Photo Source Domain', parse_url(esc_url($_POST['postdata_source']), PHP_URL_HOST)); } else { delete_post_meta($post_id, '_Photo Source'); delete_post_meta($post_id, '_Photo Source Domain'); } //add tags wp_set_post_tags($post_id, sanitize_text_field($_POST['postdata_tags'])); //add price if ($_POST['postdata_price']) { if (strpos($_POST['postdata_price'], '.') !== false) { $_POST['postdata_price'] = number_format($_POST['postdata_price'], 2); } update_post_meta($post_id, '_Price', sanitize_text_field($_POST['postdata_price'])); } else { if (get_post_meta($post_id, '_Price', true) !== '') { delete_post_meta($post_id, '_Price'); } } //add new board to followers who fully follow user if ($new_board_id && !is_wp_error($new_board_id)) { $usermeta_followers_id_allboards = get_user_meta($user_id, '_Followers User ID All Boards'); $followers_id_allboards = $usermeta_followers_id_allboards[0]; if (!empty($followers_id_allboards)) { foreach ($followers_id_allboards as $followers_id_allboard) { $usermeta_following_board_id = get_user_meta($followers_id_allboard, '_Following Board ID'); $following_board_id = $usermeta_following_board_id[0]; array_unshift($following_board_id, $new_board_id['term_id']); update_user_meta($followers_id_allboard, '_Following Board ID', $following_board_id); } } } do_action('ipin_after_edit_pin', $post_id); echo get_permalink($post_id); exit; }
/** * Update term based on arguments provided. * * The $args will indiscriminately override all values with the same field name. * Care must be taken to not override important information need to update or * update will fail (or perhaps create a new term, neither would be acceptable). * * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not * defined in $args already. * * 'alias_of' will create a term group, if it doesn't already exist, and update * it for the $term. * * If the 'slug' argument in $args is missing, then the 'name' in $args will be * used. It should also be noted that if you set 'slug' and it isn't unique then * a WP_Error will be passed back. If you don't pass any slug, then a unique one * will be created for you. * * For what can be overrode in $args, check the term scheme can contain and stay * away from the term keys. * * @since 2.3.0 * * @uses $wpdb * * @param int $term_id The ID of the term * @param string $taxonomy The context in which to relate the term to the object. * @param array|string $args Overwrite term field values * @return array|WP_Error Returns Term ID and Taxonomy Term ID */ function wp_update_term( $term_id, $taxonomy, $args = array() ) { global $wpdb; if ( ! taxonomy_exists($taxonomy) ) return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); $term_id = (int) $term_id; // First, get all of the original args $term = get_term ($term_id, $taxonomy, ARRAY_A); if ( is_wp_error( $term ) ) return $term; // Escape data pulled from DB. $term = wp_slash($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); extract($args, EXTR_SKIP); // expected_slashed ($name) $name = wp_unslash($name); $description = wp_unslash($description); if ( '' == trim($name) ) return new WP_Error('empty_term_name', __('A name is required for this term')); $empty_slug = false; if ( empty($slug) ) { $empty_slug = true; $slug = sanitize_title($name); } if ( $alias_of ) { $alias = $wpdb->get_row( $wpdb->prepare( "SELECT term_id, term_group FROM $wpdb->terms WHERE slug = %s", $alias_of) ); if ( $alias->term_group ) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } else { // The alias isn't in a group, so let's create a new one and firstly add the alias term to it. $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1; /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_terms', $alias->term_id, $taxonomy ); $wpdb->update( $wpdb->terms, compact('term_group'), array( 'term_id' => $alias->term_id ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_terms', $alias->term_id, $taxonomy ); } } /** * Filter the term parent. * * Hook to this filter to see if it will cause a hierarchy loop. * * @since 3.1.0 * * @param int $parent ID of the parent term. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @param array $args Compacted array of update arguments for the given term. * @param array $args An array of update arguments for the given term. */ $parent = apply_filters( 'wp_update_term_parent', $parent, $term_id, $taxonomy, compact( array_keys( $args ) ), $args ); // Check for duplicate slug $id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->terms WHERE slug = %s", $slug ) ); if ( $id && ($id != $term_id) ) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ( $empty_slug || ( $parent != $term['parent']) ) $slug = wp_unique_term_slug($slug, (object) $args); else return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); } /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_terms', $term_id, $taxonomy ); $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) ); if ( empty($slug) ) { $slug = sanitize_title($name, $term_id); $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) ); } /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_terms', $term_id, $taxonomy ); $tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) ); /** * Fires immediate before a term-taxonomy relationship is updated. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( 'edit_term_taxonomy', $tt_id, $taxonomy ); $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) ); /** * Fires immediately after a term-taxonomy relationship is updated. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( 'edited_term_taxonomy', $tt_id, $taxonomy ); // Clean the relationship caches for all object types using this term $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) ); $tax_object = get_taxonomy( $taxonomy ); foreach ( $tax_object->object_type as $object_type ) { clean_object_term_cache( $objects, $object_type ); } /** * Fires after a term has been updated, but before the term cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( "edit_term", $term_id, $tt_id, $taxonomy ); /** * Fires after a term in a specific taxonomy has been updated, but before the term * cache has been cleaned. * * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action( "edit_$taxonomy", $term_id, $tt_id ); /** This filter is documented in wp-includes/taxonomy.php */ $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id ); clean_term_cache($term_id, $taxonomy); /** * Fires after a term has been updated, and the term cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action( "edited_term", $term_id, $tt_id, $taxonomy ); /** * Fires after a term for a specific taxonomy has been updated, and the term * cache has been cleaned. * * The dynamic portion of the hook name, $taxonomy, refers to the taxonomy slug. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action( "edited_$taxonomy", $term_id, $tt_id ); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); }
/** * Update term based on arguments provided. * * The $args will indiscriminately override all values with the same field name. * Care must be taken to not override important information need to update or * update will fail (or perhaps create a new term, neither would be acceptable). * * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not * defined in $args already. * * 'alias_of' will create a term group, if it doesn't already exist, and update * it for the $term. * * If the 'slug' argument in $args is missing, then the 'name' in $args will be * used. It should also be noted that if you set 'slug' and it isn't unique then * a WP_Error will be passed back. If you don't pass any slug, then a unique one * will be created for you. * * For what can be overrode in `$args`, check the term scheme can contain and stay * away from the term keys. * * @since 2.3.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $term_id The ID of the term * @param string $taxonomy The context in which to relate the term to the object. * @param array|string $args Optional. Array of get_terms() arguments. Default empty array. * @return array|WP_Error Returns Term ID and Taxonomy Term ID */ function wp_update_term($term_id, $taxonomy, $args = array()) { global $wpdb; if (!taxonomy_exists($taxonomy)) { return new WP_Error('invalid_taxonomy', __('Invalid taxonomy')); } $term_id = (int) $term_id; // First, get all of the original args $term = get_term($term_id, $taxonomy); if (is_wp_error($term)) { return $term; } if (!$term) { return new WP_Error('invalid_term', __('Empty Term')); } $term = (array) $term->data; // Escape data pulled from DB. $term = wp_slash($term); // Merge old and new args with new args overwriting old ones. $args = array_merge($term, $args); $defaults = array('alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => ''); $args = wp_parse_args($args, $defaults); $args = sanitize_term($args, $taxonomy, 'db'); $parsed_args = $args; // expected_slashed ($name) $name = wp_unslash($args['name']); $description = wp_unslash($args['description']); $parsed_args['name'] = $name; $parsed_args['description'] = $description; if ('' == trim($name)) { return new WP_Error('empty_term_name', __('A name is required for this term')); } if ($parsed_args['parent'] > 0 && !term_exists((int) $parsed_args['parent'])) { return new WP_Error('missing_parent', __('Parent term does not exist.')); } $empty_slug = false; if (empty($args['slug'])) { $empty_slug = true; $slug = sanitize_title($name); } else { $slug = $args['slug']; } $parsed_args['slug'] = $slug; $term_group = isset($parsed_args['term_group']) ? $parsed_args['term_group'] : 0; if ($args['alias_of']) { $alias = get_term_by('slug', $args['alias_of'], $taxonomy); if (!empty($alias->term_group)) { // The alias we want is already in a group, so let's use that one. $term_group = $alias->term_group; } elseif (!empty($alias->term_id)) { /* * The alias is not in a group, so we create a new one * and add the alias to it. */ $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM {$wpdb->terms}") + 1; wp_update_term($alias->term_id, $taxonomy, array('term_group' => $term_group)); } $parsed_args['term_group'] = $term_group; } /** * Filter the term parent. * * Hook to this filter to see if it will cause a hierarchy loop. * * @since 3.1.0 * * @param int $parent ID of the parent term. * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. * @param array $parsed_args An array of potentially altered update arguments for the given term. * @param array $args An array of update arguments for the given term. */ $parent = apply_filters('wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args); // Check for duplicate slug $duplicate = get_term_by('slug', $slug, $taxonomy); if ($duplicate && $duplicate->term_id != $term_id) { // If an empty slug was passed or the parent changed, reset the slug to something unique. // Otherwise, bail. if ($empty_slug || $parent != $term['parent']) { $slug = wp_unique_term_slug($slug, (object) $args); } else { return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug)); } } $tt_id = (int) $wpdb->get_var($wpdb->prepare("SELECT tt.term_taxonomy_id FROM {$wpdb->term_taxonomy} AS tt INNER JOIN {$wpdb->terms} AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id)); // Check whether this is a shared term that needs splitting. $_term_id = _split_shared_term($term_id, $tt_id); if (!is_wp_error($_term_id)) { $term_id = $_term_id; } /** * Fires immediately before the given terms are edited. * * @since 2.9.0 * * @param int $term_id Term ID. * @param string $taxonomy Taxonomy slug. */ do_action('edit_terms', $term_id, $taxonomy); $wpdb->update($wpdb->terms, compact('name', 'slug', 'term_group'), compact('term_id')); if (empty($slug)) { $slug = sanitize_title($name, $term_id); $wpdb->update($wpdb->terms, compact('slug'), compact('term_id')); } /** * Fires immediately after the given terms are edited. * * @since 2.9.0 * * @param int $term_id Term ID * @param string $taxonomy Taxonomy slug. */ do_action('edited_terms', $term_id, $taxonomy); /** * Fires immediate before a term-taxonomy relationship is updated. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action('edit_term_taxonomy', $tt_id, $taxonomy); $wpdb->update($wpdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent'), array('term_taxonomy_id' => $tt_id)); /** * Fires immediately after a term-taxonomy relationship is updated. * * @since 2.9.0 * * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action('edited_term_taxonomy', $tt_id, $taxonomy); // Clean the relationship caches for all object types using this term. $objects = $wpdb->get_col($wpdb->prepare("SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id = %d", $tt_id)); $tax_object = get_taxonomy($taxonomy); foreach ($tax_object->object_type as $object_type) { clean_object_term_cache($objects, $object_type); } /** * Fires after a term has been updated, but before the term cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action("edit_term", $term_id, $tt_id, $taxonomy); /** * Fires after a term in a specific taxonomy has been updated, but before the term * cache has been cleaned. * * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action("edit_{$taxonomy}", $term_id, $tt_id); /** This filter is documented in wp-includes/taxonomy-functions.php */ $term_id = apply_filters('term_id_filter', $term_id, $tt_id); clean_term_cache($term_id, $taxonomy); /** * Fires after a term has been updated, and the term cache has been cleaned. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ do_action("edited_term", $term_id, $tt_id, $taxonomy); /** * Fires after a term for a specific taxonomy has been updated, and the term * cache has been cleaned. * * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug. * * @since 2.3.0 * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID. */ do_action("edited_{$taxonomy}", $term_id, $tt_id); return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); }
/** * Function which creates a new stack from a stack template array * Parameter: Stack Template Array * Parameter: Stack Name * Returns New Stack's ID */ function mp_stacks_create_stack_from_template($mp_stack_template_array, $new_stack_name) { //Make new stack $new_stack_array = wp_insert_term($new_stack_name, 'mp_stacks', array('description' => isset($mp_stack_template_array['stack_description']) ? $mp_stack_template_array['stack_description'] : NULL, 'slug' => wp_unique_term_slug(sanitize_title($new_stack_name), (object) array('parent' => 0, 'taxonomy' => 'mp_stacks')))); //The new stack's id $new_stack_id = $new_stack_array['term_id']; //Get the wp upload dir $wp_upload_dir = wp_upload_dir(); //Loop through each brick in the original stack if (!empty($mp_stack_template_array['stack_bricks']) && is_array($mp_stack_template_array['stack_bricks'])) { foreach ($mp_stack_template_array['stack_bricks'] as $original_brick) { //New Brick Setup $new_brick = array('post_title' => $original_brick['brick_title'], 'post_status' => 'publish', 'post_type' => 'mp_brick'); //Create a new brick $new_brick_id = wp_insert_post($new_brick, true); //Apply the new Stack ID (Taxonomy Term) to this Brick (Post) wp_set_object_terms($new_brick_id, $new_stack_id, 'mp_stacks'); //Loop through the meta in the original brick foreach ($original_brick as $brick_meta_id => $brick_meta_value) { //Don't save the brick title as a meta field if ($brick_meta_id != 'brick_title') { //If this meta field is the stack order one if ($brick_meta_id == 'mp_stack_order') { //Set the Stack Order update_post_meta($new_brick_id, 'mp_stack_order_' . $new_stack_id, $brick_meta_value); } else { //If this is a repeater if (isset($brick_meta_value['value']) && is_array($brick_meta_value['value'])) { //Reset our checked meta variable $brick_meta_checked_value = array(); $repeat_counter = 0; //Loop through each repeat in this repeater foreach ($brick_meta_value['value'] as $repeat) { //Loop through each field in this repeat foreach ($repeat as $field_id => $field_value_array) { //If this should be an imported attachment if (isset($field_value_array['attachment']) && $field_value_array['attachment']) { $brick_meta_checked_value[$repeat_counter][$field_id] = mp_stack_check_value_for_attachment($field_value_array['value']); } else { $brick_meta_checked_value[$repeat_counter][$field_id] = $field_value_array['value']; } } $repeat_counter = $repeat_counter + 1; } } else { //If this should be an imported attachment if (isset($brick_meta_value['attachment']) && $brick_meta_value['attachment']) { $brick_meta_checked_value = mp_stack_check_value_for_attachment($brick_meta_value['value']); } else { $brick_meta_checked_value = isset($brick_meta_value['value']) ? $brick_meta_value['value'] : NULL; } } //End of: If $brick_meta_value['value'] is not a repeater //Save the metadata to the new brick update_post_meta($new_brick_id, $brick_meta_id, $brick_meta_checked_value); } } } } } return $new_stack_id; }