plugin author.', 'simpletags' ), 'error' );
} elseif ( ! isset( SimpleTags_Admin::$taxonomy ) || ! taxonomy_exists( SimpleTags_Admin::$taxonomy ) ) { // Valid taxo ?
add_settings_error( __CLASS__, __CLASS__, __( 'Missing valid taxonomy for work... Try again. If this problem persist, contact plugin author.', 'simpletags' ), 'error' );
} elseif ( $_POST['term_action'] == 'renameterm' ) {
$oldtag = ( isset( $_POST['renameterm_old'] ) ) ? $_POST['renameterm_old'] : '';
$newtag = ( isset( $_POST['renameterm_new'] ) ) ? $_POST['renameterm_new'] : '';
self::renameTerms( SimpleTags_Admin::$taxonomy, $oldtag, $newtag );
} elseif ( $_POST['term_action'] == 'deleteterm' ) {
$todelete = ( isset( $_POST['deleteterm_name'] ) ) ? $_POST['deleteterm_name'] : '';
self::deleteTermsByTermList( SimpleTags_Admin::$taxonomy, $todelete );
} elseif ( $_POST['term_action'] == 'addterm' ) {
$matchtag = ( isset( $_POST['addterm_match'] ) ) ? $_POST['addterm_match'] : '';
$newtag = ( isset( $_POST['addterm_new'] ) ) ? $_POST['addterm_new'] : '';
self::addMatchTerms( SimpleTags_Admin::$taxonomy, $matchtag, $newtag );
} elseif ( $_POST['term_action'] == 'remove-rarelyterms' ) {
self::removeRarelyUsed( SimpleTags_Admin::$taxonomy, (int) $_POST['number-rarely'] );
} /* elseif ( $_POST['term_action'] == 'editslug' ) {
$matchtag = (isset($_POST['tagname_match'])) ? $_POST['tagname_match'] : '';
$newslug = (isset($_POST['tagslug_new'])) ? $_POST['tagslug_new'] : '';
self::editTermSlug( SimpleTags_Admin::$taxonomy, $matchtag, $newslug );
}*/
}
// Default order
if ( ! isset( $_GET['order'] ) ) {
$_GET['order'] = 'name-asc';
}
settings_errors( __CLASS__ );
?>
plugin\'s homepage for further details. If you find a bug, or have a fantastic idea for this plugin, ask me !', 'simpletags' ); ?>
exit !
if ( empty( $old_terms ) || empty( $new_terms ) ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No new/old valid term specified!', 'simpletags' ), 'error' );
return false;
}
$counter = 0;
if ( count( $old_terms ) == count( $new_terms ) ) { // Rename only
foreach ( (array) $old_terms as $i => $old_tag ) {
$new_name = $new_terms[ $i ];
// Get term by name
$term = get_term_by( 'name', $old_tag, $taxonomy );
if ( ! $term ) {
continue;
}
// Get objects from term ID
$objects_id = get_objects_in_term( $term->term_id, $taxonomy, array( 'fields' => 'all_with_object_id' ) );
// Create the new term
if ( ! $term_info = term_exists( $new_name, $taxonomy ) ) {
$term_info = wp_insert_term( $new_name, $taxonomy );
}
// If default category, update the ID for new term...
if ( 'category' == $taxonomy && $term->term_id == get_option( 'default_category' ) ) {
update_option( 'default_category', $term_info['term_id'] );
clean_term_cache( $term_info['term_id'], $taxonomy );
}
// Delete old term
wp_delete_term( $term->term_id, $taxonomy );
// Set objects to new term ! (Append no replace)
foreach ( (array) $objects_id as $object_id ) {
wp_set_object_terms( $object_id, $new_name, $taxonomy, true );
}
// Clean cache
clean_object_term_cache( $objects_id, $taxonomy );
clean_term_cache( $term->term_id, $taxonomy );
// Increment
$counter ++;
}
if ( $counter == 0 ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No term renamed.', 'simpletags' ), 'updated' );
} else {
add_settings_error( __CLASS__, __CLASS__, sprintf( __( 'Renamed term(s) «%1$s» to «%2$s»', 'simpletags' ), $old, $new ), 'updated' );
}
} elseif ( count( $new_terms ) == 1 ) { // Merge
// Set new tag
$new_tag = $new_terms[0];
if ( empty( $new_tag ) ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No valid new term.', 'simpletags' ), 'error' );
return false;
}
// Get terms ID from old terms names
$terms_id = array();
foreach ( (array) $old_terms as $old_tag ) {
$term = get_term_by( 'name', addslashes( $old_tag ), $taxonomy );
$terms_id[] = (int) $term->term_id;
}
// Get objects from terms ID
$objects_id = get_objects_in_term( $terms_id, $taxonomy, array( 'fields' => 'all_with_object_id' ) );
// No objects ? exit !
if ( ! $objects_id ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No objects found for specified old terms.', 'simpletags' ), 'error' );
return false;
}
// Delete old terms
foreach ( (array) $terms_id as $term_id ) {
wp_delete_term( $term_id, $taxonomy );
}
// Set objects to new term ! (Append no replace)
foreach ( (array) $objects_id as $object_id ) {
wp_set_object_terms( $object_id, $new_tag, $taxonomy, true );
$counter ++;
}
// Test if term is also a category
/*
if ( is_term($new_tag, 'category') ) {
// Edit the slug to use the new term
self::editTermSlug( $new_tag, sanitize_title($new_tag) );
}
*/
// Clean cache
clean_object_term_cache( $objects_id, $taxonomy );
clean_term_cache( $terms_id, $taxonomy );
if ( $counter == 0 ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No term merged.', 'simpletags' ), 'updated' );
} else {
add_settings_error( __CLASS__, __CLASS__, sprintf( __( 'Merge term(s) «%1$s» to «%2$s». %3$s objects edited.', 'simpletags' ), $old, $new, $counter ), 'updated' );
}
} else { // Error
add_settings_error( __CLASS__, __CLASS__, sprintf( __( 'Error. No enough terms for rename. Too for merge. Choose !', 'simpletags' ), $old ), 'error' );
}
return true;
}
/**
* Method for delete a list of terms
*
* @param string $taxonomy
* @param string $delete
*
* @return boolean
* @author Amaury Balmer
*/
public static function deleteTermsByTermList( $taxonomy = 'post_tag', $delete ) {
if ( trim( str_replace( ',', '', stripslashes( $delete ) ) ) == '' ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No term specified!', 'simpletags' ), 'error' );
return false;
}
// In array + filter
$delete_terms = explode( ',', $delete );
$delete_terms = array_filter( $delete_terms, '_delete_empty_element' );
// Delete tags
$counter = 0;
foreach ( (array) $delete_terms as $term ) {
$term = get_term_by( 'name', $term, $taxonomy );
$term_id = (int) $term->term_id;
if ( $term_id != 0 ) {
wp_delete_term( $term_id, $taxonomy );
clean_term_cache( $term_id, $taxonomy );
$counter ++;
}
}
if ( $counter == 0 ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No term deleted.', 'simpletags' ), 'updated' );
} else {
add_settings_error( __CLASS__, __CLASS__, sprintf( __( '%1s term(s) deleted.', 'simpletags' ), $counter ), 'updated' );
}
return true;
}
/**
* Method for add terms for all or specified posts
*
* @param string $taxonomy
* @param string $match
* @param string $new
*
* @return boolean
* @author Amaury Balmer
*/
public static function addMatchTerms( $taxonomy = 'post_tag', $match = '', $new = '' ) {
if ( trim( str_replace( ',', '', stripslashes( $new ) ) ) == '' ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No new term(s) specified!', 'simpletags' ), 'error' );
return false;
}
$match_terms = explode( ',', $match );
$new_terms = explode( ',', $new );
$match_terms = array_filter( $match_terms, '_delete_empty_element' );
$new_terms = array_filter( $new_terms, '_delete_empty_element' );
$counter = 0;
if ( ! empty( $match_terms ) ) { // Match and add
// Get terms ID from old match names
$terms_id = array();
foreach ( (array) $match_terms as $match_term ) {
$term = get_term_by( 'name', $match_term, $taxonomy );
$terms_id[] = (int) $term->term_id;
}
// Get object ID with terms ID
$objects_id = get_objects_in_term( $terms_id, $taxonomy, array( 'fields' => 'all_with_object_id' ) );
// Add new tags for specified post
foreach ( (array) $objects_id as $object_id ) {
wp_set_object_terms( $object_id, $new_terms, $taxonomy, true ); // Append terms
$counter ++;
}
// Clean cache
clean_object_term_cache( $objects_id, $taxonomy );
clean_term_cache( $terms_id, $taxonomy );
} else { // Add for all posts
// Page or not ?
$post_type_sql = ( is_page_have_tags() ) ? "post_type IN('page', 'post')" : "post_type = 'post'"; // TODO, CPT
// Get all posts ID
global $wpdb;
$objects_id = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE {$post_type_sql}" );
// Add new tags for all posts
foreach ( (array) $objects_id as $object_id ) {
wp_set_object_terms( $object_id, $new_terms, $taxonomy, true ); // Append terms
$counter ++;
}
// Clean cache
clean_object_term_cache( $objects_id, $taxonomy );
}
if ( $counter == 0 ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No term added.', 'simpletags' ), 'updated' );
} else {
add_settings_error( __CLASS__, __CLASS__, sprintf( __( 'Term(s) added to %1s post(s).', 'simpletags' ), $counter ), 'updated' );
}
return true;
}
/**
* Delete terms when counter if inferior to a specific number
*
* @param string $taxonomy
* @param integer $number
*
* @return boolean
* @author Amaury Balmer
*/
public static function removeRarelyUsed( $taxonomy = 'post_tag', $number = 0 ) {
global $wpdb;
if ( (int) $number > 100 ) {
wp_die( 'Tcheater ?' );
}
// Get terms with counter inferior to...
$terms_id = $wpdb->get_col( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count < %d", $taxonomy, $number ) );
// Delete terms
$counter = 0;
foreach ( (array) $terms_id as $term_id ) {
if ( $term_id != 0 ) {
wp_delete_term( $term_id, $taxonomy );
clean_term_cache( $term_id, $taxonomy );
$counter ++;
}
}
if ( $counter == 0 ) {
add_settings_error( __CLASS__, __CLASS__, __( 'No term deleted.', 'simpletags' ), 'updated' );
} else {
add_settings_error( __CLASS__, __CLASS__, sprintf( __( '%1s term(s) deleted.', 'simpletags' ), $counter ), 'updated' );
}
return true;
}
/**
* Method for edit one or more terms slug
*
* @param string $taxonomy
* @param string $names
* @param string $slugs
*
* @return boolean
* @author Amaury Balmer
*/
/*
public static function editTermSlug( $taxonomy = 'post_tag', $names = '', $slugs = '') {
if ( trim( str_replace(',', '', stripslashes($slugs)) ) == '' ) {
add_settings_error( __CLASS__, __CLASS__, __('No new slug(s) specified!', 'simpletags'), 'error' );
return false;
}
$match_names = explode(',', $names);
$new_slugs = explode(',', $slugs);
$match_names = array_filter($match_names, '_delete_empty_element');
$new_slugs = array_filter($new_slugs, '_delete_empty_element');
if ( count($match_names) != count($new_slugs) ) {
add_settings_error( __CLASS__, __CLASS__, __('Terms number and slugs number isn\'t the same!', 'simpletags'), 'error' );
return false;
} else {
$counter = 0;
foreach ( (array) $match_names as $i => $match_name ) {
// Sanitize slug + Escape
$new_slug = sanitize_title($new_slugs[$i]);
// Get term by name
$term = get_term_by('name', $match_name, $taxonomy);
if ( !$term ) {
continue;
}
// Increment
$counter++;
// Update term
wp_update_term($term->term_id, $taxonomy, array('slug' => $new_slug));
// Clean cache
clean_term_cache($term->term_id, $taxonomy);
}
}
if ( $counter == 0 ) {
add_settings_error( __CLASS__, __CLASS__, __('No slug edited.', 'simpletags'), 'updated' );
} else {
add_settings_error( __CLASS__, __CLASS__, sprintf(__('%s slug(s) edited.', 'simpletags'), $counter), 'updated' );
}
return true;
}
*/
}