Skip to content

Commit

Permalink
1.0.1 Add ability to switch comma-separated list of term IDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsternberg committed Jul 22, 2014
1 parent 952a16a commit da1e3b7
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 85 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ Switch the taxonomy for terms by a specific parent of another taxonomy.
## Instructions

1. **Backup!**
2. Activate the plugin and browse to yourdomain.com/wp-admin/options-general.php?page=taxonomy-switcher
2. Activate the plugin and browse to yourdomain.com/wp-admin/tools.php?page=taxonomy-switcher
3. Select your "From", and "To" Taxonomies.
4. Optionally select a parent term to limit terms to switch. Typing term names will do a live search of terms with the name you are typing AND possess child terms.

**Optional**

1. Select a parent term to limit terms to switch. Typing term names will do a live search of terms with the name you are typing AND possess child terms.
2. OR add a comma-separated list of term ids to switch.

## Notes

If parent isn't set, it will default to zero, which will migrate *all* terms for that taxonomy to the new taxonomy.
If parent isn't set, or you don't specify a comma-separated list of term ids to migrate, it will migrate *all* terms for that taxonomy to the new taxonomy.

Compatible with [wp-cli](http://wp-cli.org/). `wp taxonomy-switcher` for instructions.

## Changelog

#### 1.0.1
* Add ability to switch comma-separated list of term IDs.

Compatible with [wp-cli](http://wp-cli.org/). `wp taxonomy-switcher` for instructions.
#### 1.0.0
* Release
87 changes: 69 additions & 18 deletions Taxonomy_Switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Taxonomy_Switcher {
*/
public $parent = 0;

/**
* Array of term IDs to convert
*
* @var array
*/
public $terms = array();

/**
* Array of Term IDs to convert
*
Expand All @@ -39,28 +46,47 @@ class Taxonomy_Switcher {
*/
public $notices = array();

/**
* Array of Error/Success messages
*
* @var array
*/
public $messages = array();

/**
* Setup the object
*
* @param null|int $from Taxonomy to switch from
* @param null|int $to Taxonomy to switch to
* @param int $parent Parent term_id to limit by
* @param array $args Arguments containing from taxonomy, to taxonomy,
* and additional optional params
*
* @since 1.0.0
*/
public function __construct( $from = null, $to = null, $parent = 0 ) {
public function __construct( $args = array() ) {

$this->is_ui = ( isset( $_GET['page'] ) && 'taxonomy-switcher' == $_GET['page'] );
$args = wp_parse_args( $args, array(
'from_tax' => '',
'to_tax' => '',
'parent' => '',
'terms' => '',
) );

if ( null !== $from && null !== $to ) {
$this->from = sanitize_text_field( $from );
$this->to = sanitize_text_field( $to );
if ( ! $args[ 'from_tax' ] || ! $args[ 'to_tax' ] ) {
return;
}

if ( !empty( $parent ) ) {
$this->parent = absint( $parent );
}
if ( !empty( $args[ 'parent' ] ) ) {
$this->parent = absint( $args[ 'parent' ] );
}

if ( !empty( $args[ 'terms' ] ) ) {
$this->terms = wp_parse_id_list( $args[ 'terms' ] );
}

$this->is_ui = ( isset( $_GET['page'] ) && 'taxonomy-switcher' == $_GET['page'] );

$this->from = sanitize_text_field( $args[ 'from_tax' ] );
$this->to = sanitize_text_field( $args[ 'to_tax' ] );

}

/**
Expand All @@ -72,17 +98,23 @@ public function admin_convert() {

$count = $this->count();

$this->notice( sprintf( __( 'Switching %d terms with the taxonomy \'%s\' to the taxonomy \'%s\'', 'wds' ), $count, $this->from, $this->to ) );
if ( ! $count && $this->is_ui ) {
return $this->notice( $this->notices( 'no_terms' ) );
}

$this->notice( $this->notices( 'switching' ) );

if ( 0 < $this->parent ) {
$this->notice( sprintf( __( 'Limiting the switch by the parent term_id of %d', 'wds' ), $this->parent ) );
$this->notice( $this->notices( 'limit_by_parent' ) );
} elseif ( ! empty( $this->terms ) ) {
$this->notice( $this->notices( 'limit_by_terms' ) );
}

set_time_limit( 0 );

$this->convert();

$this->notice( __( 'Taxonomies switched!', 'wds' ) );
$this->notice( $this->notices( 'switched' ) );

if ( $this->is_ui ) {
return $this->notices;
Expand All @@ -103,6 +135,24 @@ public function notice( $notice ) {
if ( ! $this->is_ui ) {
echo $notice;
}
return $this->notices;
}

public function notices( $key ) {
if ( ! empty( $this->messages ) ) {
return $this->messages[ $key ];
}

$count = $this->count();
$count_name = sprintf( _n( '1 term', '%d terms', $count, 'wds' ), $count );
$this->messages = array(
'no_terms' => __( 'No terms to be switched. Check if the term exists in your "from" taxonomy.', 'wds' ) ,
'switching' => sprintf( __( 'Switching %s with the taxonomy \'%s\' to the taxonomy \'%s\'', 'wds' ), $count_name, $this->from, $this->to ) ,
'limit_by_parent' => sprintf( __( 'Limiting the switch by the parent term_id of %d', 'wds' ), $this->parent ) ,
'limit_by_terms' => sprintf( __( 'Limiting the switch to these terms: %s', 'wds' ), implode(', ', $this->terms ) ) ,
'switched' => sprintf( __( 'Taxonomies switched for %s!', 'wds' ), $count_name ) ,
);
return $this->messages[ $key ];
}

/**
Expand All @@ -115,11 +165,12 @@ public function get_term_ids() {

$args = array(
'hide_empty' => false,
'fields' => 'ids',
'child_of' => $this->parent
'fields' => 'ids',
'child_of' => $this->parent,
'include' => $this->terms,
);

$args = apply_filters( 'taxonomy_switcher_get_terms_args', $args, $this->from, $this->to, $this->parent );
$args = apply_filters( 'taxonomy_switcher_get_terms_args', $args, $this->from, $this->to, array( 'parent' => $this->parent, 'terms' => $this->terms ) );

$terms = get_terms( $this->from, $args );

Expand Down Expand Up @@ -188,4 +239,4 @@ public function convert() {

}

}
}
8 changes: 8 additions & 0 deletions Taxonomy_Switcher_UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ public function do_page() {
</select>
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="taxonomy-switcher-terms"><?php _e( 'Comma separated list of term ids to switch', 'wds' ); ?></label>
</th>
<td>
<input placeholder="1,2,13" class="regular-text" type="text" id="taxonomy-switcher-terms" name="terms" value="<?php echo isset( $_GET[ 'terms' ] ) ? esc_attr( $_GET[ 'terms' ] ) : ''; ?>">
</td>
</tr>
<tr valign="top">
<th scope="row">
<label for="taxonomy-switcher-parent"><?php _e( 'Limit taxomoy switch for child terms of a specific parent', 'wds' ); ?></label>
Expand Down
13 changes: 9 additions & 4 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
Tags: taxonomy, taxonomies, term, terms, category, categories, convert, converter, tag, tags, custom taxonomy, custom taxonomies, switch taxonomies
Requires at least: 3.5
Tested up to: 3.8.1
Stable tag: 1.0.0
Stable tag: 1.0.1
License: GNU AGPLv3
License URI: http://www.gnu.org/licenses/agpl-3.0.html

Expand All @@ -20,8 +20,7 @@ Plugin also has built-in support for [WP-CLI](http://wp-cli.org/). In the comman

### Notes

Please keep in mind, if parent term isn't set, it will default to zero, which will migrate *all* terms for that taxonomy to the new taxonomy.

Please keep in mind, if parent isn't set, or you don't specify a comma-separated list of term ids to migrate, it will migrate *all* terms for that taxonomy to the new taxonomy.

== Installation ==

Expand All @@ -30,7 +29,7 @@ Please keep in mind, if parent term isn't set, it will default to zero, which wi
3. Activate the plugin through the 'Plugins' menu in WordPress
4. Navigate to the 'Taxonomy Switcher' admin page. You'll find the menu item under the 'Tools' menu item on the left.
5. Select your "From", and "To" Taxonomies.
6. Optionally select a parent term to limit terms to switch. Typing term names will do a live search of terms with the name you are typing AND possess child terms.
6. **a)** Optionally select a parent term to limit terms to switch. Typing term names will do a live search of terms with the name you are typing AND possess child terms. **OR** **b)** Add a comma-separated list of term ids to switch.
7. Switch them!

== Frequently Asked Questions ==
Expand All @@ -43,10 +42,16 @@ Please keep in mind, if parent term isn't set, it will default to zero, which wi

== Changelog ==

= 1.0.1 =
* Add ability to switch comma-separated list of term IDs.

= 1.0.0 =
* Release

== Upgrade Notice ==

= 1.0.1 =
* Add ability to switch comma-separated list of term IDs.

= 1.0.0 =
* Release
42 changes: 22 additions & 20 deletions taxonomy-switcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Taxonomy Switcher
Plugin URI: https://github.com/WebDevStudios/taxonomy-switcher
Description: Switches the Taxonomy of terms to a different Taxonomy
Version: 1.0.0
Version: 1.0.1
Author: WebDevStudios
Author URI: http://webdevstudios.com
*/
Expand Down Expand Up @@ -45,32 +45,34 @@ public function __construct() {
* @since 1.0.0
*/
public function taxonomy_switcher_init() {
if ( isset( $_GET[ 'taxonomy_switcher' ] ) && 1 == $_GET[ 'taxonomy_switcher' ] && current_user_can( 'install_plugins' ) ) {
if ( isset( $_GET[ 'from_tax' ] ) && !empty( $_GET[ 'from_tax' ] ) && isset( $_GET[ 'to_tax' ] ) && !empty( $_GET[ 'to_tax' ] ) ) {

require_once( dirname( __FILE__ ) . '/Taxonomy_Switcher.php' );

$from = sanitize_text_field( $_GET[ 'from_tax' ] );
$to = sanitize_text_field( $_GET[ 'to_tax' ] );
$parent = 0;
if (
! isset( $_GET[ 'taxonomy_switcher' ] )
|| 1 != $_GET[ 'taxonomy_switcher' ]
|| ! current_user_can( 'install_plugins' )
|| ! isset( $_GET[ 'from_tax' ] )
|| empty( $_GET[ 'from_tax' ] )
|| ! isset( $_GET[ 'to_tax' ] )
|| empty( $_GET[ 'to_tax' ] )
) {
return;
}

if ( isset( $_GET[ 'parent' ] ) && !empty( $_GET[ 'parent' ] ) ) {
$parent = absint( $_GET[ 'parent' ] );
}
require_once( dirname( __FILE__ ) . '/Taxonomy_Switcher.php' );

$taxonomy_switcher = new Taxonomy_Switcher( $from, $to, $parent );
$taxonomy_switcher = new Taxonomy_Switcher( $_GET );

$success_notices = $taxonomy_switcher->admin_convert();
$success_notices = $taxonomy_switcher->admin_convert();

if ( ! empty( $success_notices ) ) {
// Save notices
update_option( 'taxonomy-switcher-notices', $success_notices );
// Redirect and strip query string
wp_redirect( add_query_arg( 'page', $this->ui->admin_slug, admin_url( '/options-general.php' ) ) );
}
}
if ( empty( $success_notices ) ) {
return;
}

// Save notices
add_option( 'taxonomy-switcher-notices', $success_notices, null, 'no' );
// Redirect and strip query string
wp_redirect( add_query_arg( 'page', $this->ui->admin_slug, admin_url( '/tools.php' ) ) );

}

/**
Expand Down
Loading

0 comments on commit da1e3b7

Please sign in to comment.