$tag_list) { // Trim data $tag_list = trim(stripslashes($tag_list)); // String to array $tags = explode(',', $tag_list); // Remove empty and trim tag $tags = array_filter($tags, '_delete_empty_element'); $show_slug = SimpleTags_Plugin::get_option_value('enable_mass-edit_terms_slug'); $term_ids = array(); foreach ($tags as $tag) { $tag = trim($tag); if (empty($tag)) { continue; } if ($show_slug && preg_match('/^(.+?)\s*\(([^)]+)\)$/', $tag, $matches)) { $term_name = trim($matches[1]); $term_slug = trim($matches[2]); $term = get_term_by('slug', $term_slug, SimpleTags_Admin::$taxonomy); if ($term) { if ($term->name === $term_name) { $term_ids[] = $term->term_id; } else { $unique_slug = $term_slug; $counter = 1; while (get_term_by('slug', $unique_slug, SimpleTags_Admin::$taxonomy)) { $unique_slug = $term_slug . '-' . $counter; $counter++; } $new_term = wp_insert_term($term_name, SimpleTags_Admin::$taxonomy, array('slug' => $unique_slug)); if (!is_wp_error($new_term)) { $term_ids[] = $new_term['term_id']; } } } else { $new_term = wp_insert_term($term_name, SimpleTags_Admin::$taxonomy, array('slug' => $term_slug)); if (!is_wp_error($new_term)) { $term_ids[] = $new_term['term_id']; } } } else { if ($show_slug) { $tag = trim(preg_replace('/\s*\([^)]+\)$/', '', $tag)); } $term = get_term_by('name', $tag, SimpleTags_Admin::$taxonomy); if ($term) { $term_ids[] = $term->term_id; } else { $new_term = wp_insert_term($tag, SimpleTags_Admin::$taxonomy); if (!is_wp_error($new_term)) { $term_ids[] = $new_term['term_id']; } } } } wp_set_object_terms($object_id, $term_ids, SimpleTags_Admin::$taxonomy); $counter++; // Clean cache clean_post_cache($object_id); } add_settings_error(__CLASS__, __CLASS__, sprintf(esc_html__('%1$s %2$s(s) terms updated with success !', 'simple-tags'), (int) $counter, strtolower(SimpleTags_Admin::$post_type_name)), 'updated taxopress-notice'); return true; } } return false; } /** * Helper function to extract term name (ignoring slug in brackets) */ private static function extractTermName($term) { return trim(preg_replace('/\s*\(.*?\)$/', '', $term)); } /** * WP Page - Mass edit tags * */ public static function pageMassEditTags() { global $wpdb, $wp_locale, $wp_query; list($post_stati, $avail_post_stati) = self::edit_data_query(); if (! isset($_GET['paged'])) { $_GET['paged'] = 1; } // Display message settings_errors(__CLASS__); ?>