tag-dropdown-widget.php 25.6 KB
Newer Older
1 2 3
<?php
/*
Plugin Name: Taxonomy Dropdown Widget
4
Plugin URI: https://ethitter.com/plugins/taxonomy-dropdown-widget/
5 6
Description: Creates a dropdown list of non-hierarchical taxonomies as an alternative to the term (tag) cloud. Widget provides numerous options to tailor the output to fit your site. Dropdown function can also be called directly for use outside of the widget. Formerly known as <strong><em>Tag Dropdown Widget</em></strong>.
Author: Erick Hitter
Erick Hitter's avatar
Erick Hitter committed
7
Version: 2.3.1
8
Author URI: https://ethitter.com/
Erick Hitter's avatar
Erick Hitter committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 24 25 26 27 28
*/

/**
 ** TAXONOMY DROPDOWN WIDGET PLUGIN
 **/
class taxonomy_dropdown_widget_plugin {
Erick Hitter's avatar
Erick Hitter committed
29 30 31 32 33
	/**
	 * Singleton!
	 */
	private static $__instance = null;

Erick Hitter's avatar
Erick Hitter committed
34
	/**
35 36
	 * Class variables
	 */
Erick Hitter's avatar
Erick Hitter committed
37
	protected $option_defaults = array(
Erick Hitter's avatar
Erick Hitter committed
38 39
		'taxonomy'        => 'post_tag',
		'select_name'     => 'Select Tag',
40
		'max_name_length' => 0,
Erick Hitter's avatar
Erick Hitter committed
41 42 43 44 45 46 47 48 49
		'cutoff'          => '&hellip;',
		'limit'           => 0,
		'order'           => 'ASC',
		'orderby'         => 'name',
		'threshold'       => 0,
		'incexc'          => 'exclude',
		'incexc_ids'      => array(),
		'hide_empty'      => true,
		'post_counts'     => false,
50 51
	);

Erick Hitter's avatar
Erick Hitter committed
52 53 54 55 56 57 58 59
	/**
	 * Implement the singleton
	 *
	 * @return object
	 */
	public static function get_instance() {
		if ( ! is_a( self::$__instance, __CLASS__ ) ) {
			self::$__instance = new self;
60 61

			self::$__instance->setup();
Erick Hitter's avatar
Erick Hitter committed
62 63 64 65 66
		}

		return self::$__instance;
	}

67 68 69 70 71
	/**
	 * Silence is golden!
	 */
	private function __construct() {}

Erick Hitter's avatar
Erick Hitter committed
72
	/**
73
	 * Register actions and activation/deactivation hooks
Erick Hitter's avatar
Erick Hitter committed
74 75 76
	 * @uses add_action
	 * @uses register_activation_hook
	 * @uses register_deactivation_hook
77 78
	 * @return null
	 */
79
	protected function setup() {
80 81 82 83
		add_action( 'widgets_init', array( $this, 'action_widgets_init' ) );

		register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
		register_deactivation_hook( __FILE__, array( $this, 'deactivation_hook' ) );
Erick Hitter's avatar
v2.2  
Erick Hitter committed
84 85

		add_action( 'split_shared_term', array( $this, 'action_split_shared_term' ), 10, 4 );
86 87
	}

88 89 90 91 92 93
	/**
	 * Allow access to certain variables that were previously public
	 *
	 * @return mixed
	 */
	public function __get( $var ) {
94
		if ( 'option_defaults' == $var ) {
95
			return $this->option_defaults;
96
		}
97

98
		return null;
99 100
	}

Erick Hitter's avatar
Erick Hitter committed
101
	/**
102
	 * Run plugin cleanup on activation
103
	 * @uses this::cleanup
104 105 106
	 * @hook activation
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
107
	public function activation_hook() {
108 109 110
		$this->cleanup();
	}

Erick Hitter's avatar
Erick Hitter committed
111
	/**
112
	 * Unregister widget when plugin is deactivated and run cleanup
113
	 * @uses this::cleanup
114 115 116
	 * @hook deactivation
	 * @return null
	 */
117
	public function deactivation_hook() {
118 119 120
		$this->cleanup();
	}

Erick Hitter's avatar
Erick Hitter committed
121
	/**
122
	 * Remove options related to plugin versions older than 2.0.
123
	 * @uses apply_filters
124 125 126
	 * @uses delete_option
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
127
	private function cleanup() {
128
		if ( ! apply_filters( 'taxonomy_dropdown_widget_run_cleanup', true ) ) {
129
			return;
130
		}
131

132
		// Remove unused options
133 134 135 136
		$legacy_options = array(
			'widget_TagDropdown',
			'widget_TagDropdown_exclude',
			'function_TagDropdown',
Erick Hitter's avatar
Erick Hitter committed
137
			'TDW_direct',
138 139
		);

140
		foreach ( $legacy_options as $legacy_option ) {
141
			delete_option( $legacy_option );
142
		}
143 144 145

		// Let others play too
		do_action( 'taxonomy_dropdown_widget_cleanup' );
146 147
	}

Erick Hitter's avatar
Erick Hitter committed
148
	/**
149 150 151 152 153
	 * Register widget
	 * @uses register_widget
	 * @action widgets_init
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
154
	public function action_widgets_init() {
155
		register_widget( 'taxonomy_dropdown_widget' );
156 157
	}

Erick Hitter's avatar
v2.2  
Erick Hitter committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
	/**
	 * Update widget options when terms are split
	 *
	 * Starting in WP 4.2, terms that were previously shared will now be split into their own terms when the terms are updated.
	 * To ensure the widget continues to include/exclude the updated terms, we search widget options on terms split and update stored IDs.
	 *
	 * @param int    $old_id   ID of shared term before the split
	 * @param int    $new_id   ID of new term created after the split
	 * @param int    $tt_id    Term taxonomy ID of split term
	 * @param string $taxonomy Taxonomy of the term being split from its shared entry
	 * @action split_shared_term
	 * @return null
	 */
	public function action_split_shared_term( $old_id, $new_id, $tt_id, $taxonomy ) {
		// WP provides no utility function for getting widget options, so we go straight to the source
		$all_widget_options = $_all_widget_options = get_option( 'widget_taxonomy_dropdown_widget', false );

		// Loop through each widget's options and update stored term IDs if they're being split here
		if ( is_array( $all_widget_options ) && ! empty( $all_widget_options ) ) {
			foreach ( $all_widget_options as $key => $options ) {
				// Check if widget needs updating
				if ( ! is_array( $options ) ) {
					continue;
				}

				if ( $options['taxonomy'] !== $taxonomy ) {
					continue;
				}

				if ( empty( $options['incexc_ids'] ) ) {
					continue;
				}

				// Account for legacy data storage option
				if ( is_string( $options['incexc_ids'] ) ) {
					$options['incexc_ids'] = explode( ',', $options['incexc_ids'] );
					$options['incexc_ids'] = array_map( 'absint', $options['incexc_ids'] );
					$options['incexc_ids'] = array_filter( $options['incexc_ids'] );
				}

				// Find stored term to update and do so
				$key_to_update = array_search( $old_id, $options['incexc_ids'] );

				if ( false === $key_to_update ) {
					continue;
				} else {
					$all_widget_options[ $key ]['incexc_ids'][ $key_to_update ] = $new_id;
				}
			}
		}

		// If the term split was one in a widget option, update the options
		// Reduces `update_option()` calls if nothing's changed
		if ( $all_widget_options !== $_all_widget_options ) {
			update_option( 'widget_taxonomy_dropdown_widget', $all_widget_options );
		}
	}

Erick Hitter's avatar
Erick Hitter committed
216
	/**
217
	 * Render widget
218
	 *
219 220
	 * @param array $options
	 * @param string|int $id
Erick Hitter's avatar
Erick Hitter committed
221
	 * @uses wp_parse_args
222
	 * @uses this::sanitize_options
Erick Hitter's avatar
Erick Hitter committed
223 224 225
	 * @uses sanitize_title
	 * @uses apply_filters
	 * @uses get_terms
226 227
	 * @uses esc_attr
	 * @uses esc_html
Erick Hitter's avatar
Erick Hitter committed
228 229 230 231 232
	 * @uses is_tag
	 * @uses is_tax
	 * @uses esc_url
	 * @uses get_term_link
	 * @uses selected
233 234
	 * @return string or false
	 */
Erick Hitter's avatar
Erick Hitter committed
235
	public function render_dropdown( $options, $id = false ) {
236
		$options = wp_parse_args( $options, $this->option_defaults );
237 238 239 240 241 242 243 244 245
		$options = $this->sanitize_options( $options );

		$id = is_numeric( $id ) ? intval( $id ) : sanitize_title( $id );

		// Set up options array for get_terms
		$terms_options = array(
			'order'        => $options['order'],
			'orderby'      => $options['orderby'],
			'hide_empty'   => $options['hide_empty'],
Erick Hitter's avatar
Erick Hitter committed
246
			'hierarchical' => false,
247 248
		);

249
		if ( $options['limit'] ) {
250
			$terms_options[ 'number' ] = $options['limit'];
251
		}
252

253
		if ( ! empty( $options['incexc_ids'] ) ) {
254
			$terms_options[ $options['incexc'] ] = $options['incexc_ids'];
255
		}
256

257 258
		$terms_options = apply_filters( 'taxonomy_dropdown_widget_options', $terms_options, $id );
		$terms_options = apply_filters( 'TagDropdown_get_tags', $terms_options );
259

260 261
		// Get terms
		$terms = get_terms( $options['taxonomy'], $terms_options );
262

263 264
		if ( is_array( $terms ) && ! empty( $terms ) ) {
			// Determine CSS ID
265
			if ( is_int( $id ) ) {
266
				$css_id = ' id="taxonomy_dropdown_widget_dropdown_' . $id . '"';
267
			} elseif ( ! empty( $id ) ) {
268
				$css_id = ' id="' . esc_attr( $id ) . '"';
269
			} else {
270
				$css_id = '';
271
			}
272

273 274
			// Start dropdown
			$output = '<select name="taxonomy_dropdown_widget_dropdown_' . esc_attr( $id ) . '" class="taxonomy_dropdown_widget_dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;"' . $css_id . '>' . "\r\n";
275

276 277 278 279
			$default = apply_filters( 'taxonomy_dropdown_widget_dropdown_default_item', '<option value="">' . esc_html( $options['select_name'] ) . '</option>', $id, $options, $terms_options, $terms );
			if ( ! empty( $default ) ) {
				$output .= "\t" . $default . "\r\n";
			}
280

281
			// Populate dropdown
282
			$i = 1;
283
			foreach ( $terms as $term ) {
284
				if ( $options['threshold'] > 0 && $term->count < $options['threshold'] ) {
285
					continue;
286
				}
287

288 289
				// Set selected attribute if on an archive page for the current term
				$current = is_tag() ? is_tag( $term->slug ) : is_tax( $term->taxonomy, $term->slug );
290

291
				// Open option tag
292
				$item = '<option value="' . esc_url( get_term_link( (int) $term->term_id, $term->taxonomy ) ) . '"' . ( selected( $current, true, false ) ) . '>';
293

294
				// Tag name
295
				$name = esc_attr( $term->name );
296
				if ( $options['max_name_length'] > 0 && strlen( $name ) > $options['max_name_length'] ) {
297
					$name = substr( $name, 0, $options['max_name_length'] ) . $options['cutoff'];
298
				}
299
				$item .= $name;
300

301
				// Count
302
				if ( $options['post_counts'] ) {
303
					$item .= ' (' . intval( $term->count ) . ')';
304
				}
305

306
				// Close option tag
307 308 309 310 311 312
				$item .= '</option>';

				$item = apply_filters( 'taxonomy_dropdown_widget_dropdown_single_item', $item, $term, $id, $options, $terms_options, $terms );
				if ( ! empty( $item ) ) {
					$output .= "\t" . $item . "\r\n";
				}
313 314 315 316

				$i++;
			}

317
			// End dropdown
318
			$output .= '</select>' . "\r\n";
319 320 321 322
			$output = apply_filters( 'taxonomy_dropdown_widget_dropdown', $output, $id, $options, $terms_options, $terms );

			// Depending on size of list, `$terms` could be quite large.
			unset( $terms );
323 324

			return $output;
325
		} else {
326
			return false;
327
		}
328 329
	}

Erick Hitter's avatar
Erick Hitter committed
330
	/**
331
	 * Sanitize plugin options
332
	 * @param array|string $options
Erick Hitter's avatar
Erick Hitter committed
333 334 335 336
	 * @uses taxonomy_exists
	 * @uses sanitize_text_field
	 * @uses absint
	 * @uses wp_parse_args
337 338
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
339
	public function sanitize_options( $options ) {
340 341 342 343 344 345
		// WP supports strings for arguments in place of arrays.
		// Match the expectation, though arrays are preferred.
		if ( is_string( $options ) ) {
			wp_parse_str( $options, $options );
		}

346
		$options_sanitized = array(
Erick Hitter's avatar
Erick Hitter committed
347 348
			'hide_empty'  => true,
			'post_counts' => false,
349 350 351 352
		);

		$keys = array_merge( array_keys( $this->option_defaults ), array( 'title' ) );

353 354
		if ( is_array( $options ) ) {
			foreach ( $keys as $key ) {
355
				if ( ! array_key_exists( $key, $options ) ) {
356
					continue;
357
				}
358 359 360 361

				$value = $options[ $key ];

				switch( $key ) {
Erick Hitter's avatar
Erick Hitter committed
362
					case 'taxonomy' :
363
						if ( taxonomy_exists( $value ) ) {
364
							$options_sanitized[ $key ] = $value;
365
						}
366 367
					break;

Erick Hitter's avatar
Erick Hitter committed
368 369 370
					case 'title' :
					case 'select_name' :
					case 'cutoff' :
371 372
						$value = sanitize_text_field( $value );

373
						if ( ! empty( $value ) || $key == 'title' ) {
374
							$options_sanitized[ $key ] = $value;
375
						}
376 377
					break;

Erick Hitter's avatar
Erick Hitter committed
378 379 380
					case 'max_name_length' :
					case 'limit' :
					case 'threshold' :
381 382 383
						$options_sanitized[ $key ] = absint( $value );
					break;

Erick Hitter's avatar
Erick Hitter committed
384
					case 'order' :
385
						if ( $value == 'ASC' || $value == 'DESC' ) {
386
							$options_sanitized[ $key ] = $value;
387
						}
388 389
					break;

Erick Hitter's avatar
Erick Hitter committed
390
					case 'orderby' :
391
						if ( $value == 'name' || $value == 'count' ) {
392
							$options_sanitized[ $key ] = $value;
393
						}
394 395
					break;

Erick Hitter's avatar
Erick Hitter committed
396
					case 'incexc' :
397
						if ( $value == 'include' || $value == 'exclude' ) {
398
							$options_sanitized[ $key ] = $value;
399
						}
400 401
					break;

Erick Hitter's avatar
Erick Hitter committed
402
					case 'incexc_ids' :
403 404
						$options_sanitized[ $key ] = array();

405
						if ( is_string( $value ) ) {
406
							$value = explode( ',', $value );
407
						}
408

409 410
						if ( is_array( $value ) ) {
							foreach ( $value as $term_id ) {
411 412
								$term_id = intval( $term_id );

413
								if ( $term_id > 0 ) {
414
									$options_sanitized[ $key ][] = $term_id;
415
								}
416 417 418 419 420 421 422 423

								unset( $term_id );
							}

							sort( $options_sanitized[ $key ], SORT_NUMERIC );
						}
					break;

Erick Hitter's avatar
Erick Hitter committed
424 425
					case 'hide_empty' :
					case 'post_counts' :
426 427 428
						$options_sanitized[ $key ] = (bool)$value;
					break;

Erick Hitter's avatar
Erick Hitter committed
429
					default :
430 431 432 433 434
					break;
				}
			}
		}

Erick Hitter's avatar
Erick Hitter committed
435
		// Ensure array contains all keys by parsing against defaults after options are sanitized
436 437 438 439 440
		$options_sanitized = wp_parse_args( $options_sanitized, $this->option_defaults );

		return $options_sanitized;
	}
}
441 442

// Prior to introduction of singleton, plugin was instantiated in a global. Continuing to do so improves backwards compatibility.
Erick Hitter's avatar
Erick Hitter committed
443
$GLOBALS['taxonomy_dropdown_widget_plugin'] = taxonomy_dropdown_widget_plugin::get_instance();
444 445 446 447 448

/**
 ** TAXONOMY DROPDOWN WIDGET
 **/
class taxonomy_dropdown_widget extends WP_Widget {
Erick Hitter's avatar
Erick Hitter committed
449
	/**
450 451
	 * Class variables
	 */
Erick Hitter's avatar
Erick Hitter committed
452
	private $defaults = array(
Erick Hitter's avatar
Erick Hitter committed
453
		'title' => 'Tags',
454 455
	);

456 457
	private $plugin = null;

Erick Hitter's avatar
Erick Hitter committed
458
	/**
459
	 * Register widget and populate class variables
Erick Hitter's avatar
Erick Hitter committed
460
	 * @uses parent::__construct()
461
	 * @uses taxonomy_dropdown_widget_plugin::get_instance
462 463
	 * @return null
	 */
Erick Hitter's avatar
Erick Hitter committed
464
	public function __construct() {
Erick Hitter's avatar
Erick Hitter committed
465
		parent::__construct( false, 'Taxonomy Dropdown Widget', array( 'description' => 'Displays selected non-hierarchical taxonomy terms in a dropdown list.' ) );
466

467 468
		// Shortcut to the main plugin instance from within the widget class
		$this->plugin = taxonomy_dropdown_widget_plugin::get_instance();
469

Erick Hitter's avatar
Erick Hitter committed
470
		// Load plugin class and populate defaults
471
		if ( is_object( $this->plugin ) && is_array( $this->plugin->option_defaults ) ) {
472
			$this->defaults = array_merge( $this->plugin->option_defaults, $this->defaults );
473
		}
474 475
	}

Erick Hitter's avatar
Erick Hitter committed
476
	/**
477 478 479
	 * Render widget
	 * @param array $args
	 * @param array $instance
Erick Hitter's avatar
Erick Hitter committed
480 481
	 * @uses wp_parse_args
	 * @uses apply_filters
482 483
	 * @return string or null
	 */
Erick Hitter's avatar
Erick Hitter committed
484
	public function widget( $args, $instance ) {
485
		// Options
486 487
		$instance = wp_parse_args( $instance, $this->defaults );

488 489 490
		// Widget
		if ( $widget = $this->plugin->render_dropdown( $instance, $this->number ) ) {
			// Wrapper and title
491
			$output = $args['before_widget'] . "\r\n";
492

493 494
			if ( ! empty( $instance['title'] ) ) {
				$output .= $args['before_title'] . apply_filters( 'taxonomy_dropdown_widget_title', '<label for="taxonomy_dropdown_widget_dropdown_' . $this->number . '">' . $instance['title'] . '</label>', $this->number ) . $args['after_title'] . "\r\n";
495
			}
496

497
			// Widget
498
			$output .= $widget . "\r\n";
499

500
			// Wrapper
501
			$output .= $args['after_widget'] . "\r\n";
502 503 504 505 506

			echo $output;
		}
	}

Erick Hitter's avatar
Erick Hitter committed
507
	/**
508 509 510 511 512
	 * Options sanitization
	 * @param array $new_instance
	 * @param array $old_instance
	 * @return array
	 */
Erick Hitter's avatar
Erick Hitter committed
513
	public function update( $new_instance, $old_instance ) {
514
		return $this->plugin->sanitize_options( $new_instance );
515 516
	}

Erick Hitter's avatar
Erick Hitter committed
517
	/**
518 519
	 * Widget options
	 * @param array $instance
Erick Hitter's avatar
Erick Hitter committed
520 521 522 523 524 525 526 527
	 * @uses wp_parse_args
	 * @uses get_taxonomies
	 * @uses _e
	 * @uses this::get_field_id
	 * @uses this::get_field_name
	 * @uses esc_attr
	 * @uses selected
	 * @uses checked
528 529
	 * @return string
	 */
Erick Hitter's avatar
Erick Hitter committed
530
	public function form( $instance ) {
Erick Hitter's avatar
Erick Hitter committed
531
		// Get options
532 533
		$options = wp_parse_args( $instance, $this->defaults );

Erick Hitter's avatar
Erick Hitter committed
534
		// Get taxonomies and remove certain Core taxonomies that shouldn't be accessed directly.
535
		$taxonomies = get_taxonomies( array(
Erick Hitter's avatar
Erick Hitter committed
536 537
			'public'       => true,
			'hierarchical' => false,
538 539
		), 'objects' );

540
		if ( array_key_exists( 'nav_menu', $taxonomies ) ) {
541
			unset( $taxonomies[ 'nav_menu' ] );
542
		}
543

544
		if ( array_key_exists( 'post_format', $taxonomies ) ) {
545
			unset( $taxonomies[ 'post_format' ] );
546
		}
547 548

	?>
Erick Hitter's avatar
Erick Hitter committed
549
		<h3><?php _e( 'Basic Settings', 'taxonomy_dropdown_widget' ); ?></h3>
550 551

		<p>
Erick Hitter's avatar
Erick Hitter committed
552
			<label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e( 'Taxonomy', 'taxonomy_dropdown_widget' ); ?>:</label><br />
553
			<select name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>">
554
				<?php foreach ( $taxonomies as $tax ) : ?>
555
					<option value="<?php echo esc_attr( $tax->name ); ?>"<?php selected( $tax->name, $options['taxonomy'], true ); ?>><?php echo $tax->labels->name; ?></option>
556 557 558 559 560
				<?php endforeach; ?>
			</select>
		</p>

		<p>
Erick Hitter's avatar
Erick Hitter committed
561
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'taxonomy_dropdown_widget' ); ?></label><br />
562
			<input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" class="widefat code" id="<?php echo $this->get_field_id( 'title' ); ?>" value="<?php echo esc_attr( $options['title'] ); ?>" />
563 564 565
		</p>

		<p>
Erick Hitter's avatar
Erick Hitter committed
566
			<label for="<?php echo $this->get_field_id( 'select_name' ); ?>"><?php _e( 'Default dropdown item:', 'taxonomy_dropdown_widget' ); ?></label><br />
567
			<input type="text" name="<?php echo $this->get_field_name( 'select_name' ); ?>" class="widefat code" id="<?php echo $this->get_field_id( 'select_name' ); ?>" value="<?php echo esc_attr( $options['select_name'] ); ?>" />
568 569
		</p>

Erick Hitter's avatar
Erick Hitter committed
570
		<h3><?php _e( 'Order', 'taxonomy_dropdown_widget' ); ?></h3>
571 572

		<p>
Erick Hitter's avatar
Erick Hitter committed
573
			<label><?php _e( 'Order terms by:', 'taxonomy_dropdown_widget' ); ?></label><br />
574

575
			<input type="radio" name="<?php echo $this->get_field_name( 'orderby' ); ?>" value="name" id="<?php echo $this->get_field_name( 'order_name' ); ?>"<?php checked( $options['orderby'], 'name', true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
576
			<label for="<?php echo $this->get_field_name( 'order_name' ); ?>"><?php _e( 'Name', 'taxonomy_dropdown_widget' ); ?></label><br />
577

578
			<input type="radio" name="<?php echo $this->get_field_name( 'orderby' ); ?>" value="count" id="<?php echo $this->get_field_name( 'order_count' ); ?>"<?php checked( $options['orderby'], 'count', true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
579
			<label for="<?php echo $this->get_field_name( 'order_count' ); ?>"><?php _e( 'Post count', 'taxonomy_dropdown_widget' ); ?></label>
580 581 582
		</p>

		<p>
Erick Hitter's avatar
Erick Hitter committed
583
			<label><?php _e( 'Order terms:', 'taxonomy_dropdown_widget' ); ?></label><br />
584

585
			<input type="radio" name="<?php echo $this->get_field_name( 'order' ); ?>" value="ASC" id="<?php echo $this->get_field_name( 'order_asc' ); ?>"<?php checked( $options['order'], 'ASC', true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
586
			<label for="<?php echo $this->get_field_name( 'order_asc' ); ?>"><?php _e( 'Ascending', 'taxonomy_dropdown_widget' ); ?></label><br />
587

588
			<input type="radio" name="<?php echo $this->get_field_name( 'order' ); ?>" value="DESC" id="<?php echo $this->get_field_name( 'order_desc' ); ?>"<?php checked( $options['order'], 'DESC', true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
589
			<label for="<?php echo $this->get_field_name( 'order_desc' ); ?>"><?php _e( 'Descending', 'taxonomy_dropdown_widget' ); ?></label>
590 591
		</p>

Erick Hitter's avatar
Erick Hitter committed
592
		<h3><?php _e( 'Term Display', 'taxonomy_dropdown_widget' ); ?></h3>
593 594

		<p>
Erick Hitter's avatar
Erick Hitter committed
595
			<label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Limit number of terms shown to:', 'taxonomy_dropdown_widget' ); ?></label><br />
596
			<input type="text" name="<?php echo $this->get_field_name( 'limit' ); ?>" id="<?php echo $this->get_field_id( 'limit' ); ?>" value="<?php echo intval( $options['limit'] ); ?>" size="3" /><br />
Erick Hitter's avatar
Erick Hitter committed
597
			<span class="description"><small><?php _e( 'Enter <strong>0</strong> for no limit.', 'taxonomy_dropdown_widget' ); ?></small></span>
598 599 600
		</p>

		<p>
Erick Hitter's avatar
Erick Hitter committed
601
			<label for="<?php echo $this->get_field_id( 'max_name_length' ); ?>"><?php _e( 'Trim long term names to <em>x</em> characters:</label>', 'taxonomy_dropdown_widget' ); ?><br />
602
			<input type="text" name="<?php echo $this->get_field_name( 'max_name_length' ); ?>" id="<?php echo $this->get_field_id( 'max_name_length' ); ?>" value="<?php echo intval( $options['max_name_length'] ); ?>" size="3" /><br />
Erick Hitter's avatar
Erick Hitter committed
603
			<span class="description"><small><?php _e( 'Enter <strong>0</strong> to show full tag names.', 'taxonomy_dropdown_widget' ); ?></small></span>
604 605 606
		</p>

		<p>
Erick Hitter's avatar
Erick Hitter committed
607
			<label for="<?php echo $this->get_field_id( 'cutoff' ); ?>"><?php _e( 'Indicator that term names are trimmed:', 'taxonomy_dropdown_widget' ); ?></label><br />
608
			<input type="text" name="<?php echo $this->get_field_name( 'cutoff' ); ?>" id="<?php echo $this->get_field_id( 'cutoff' ); ?>" value="<?php echo esc_attr( $options['cutoff'] ); ?>" size="3" /><br />
Erick Hitter's avatar
Erick Hitter committed
609
			<span class="description"><small><?php _e( 'Leave blank to use an elipsis (&hellip;).', 'taxonomy_dropdown_widget' ); ?></small></span>
610 611 612
		</p>

		<p>
613
			<input type="checkbox" name="<?php echo $this->get_field_name( 'hide_empty' ); ?>" id="<?php echo $this->get_field_id( 'hide_empty' ); ?>"  value="0"<?php checked( false, $options['hide_empty'], true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
614
			<label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>"><?php _e( 'Include terms that aren\'t assigned to any objects (empty terms).', 'taxonomy_dropdown_widget' ); ?></label>
615 616 617
		</p>

		<p>
618
			<input type="checkbox" name="<?php echo $this->get_field_name( 'post_counts' ); ?>" id="<?php echo $this->get_field_id( 'post_counts' ); ?>"  value="1"<?php checked( true, $options['post_counts'], true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
619
			<label for="<?php echo $this->get_field_id( 'post_counts' ); ?>"><?php _e( 'Display object (post) counts after term names.', 'taxonomy_dropdown_widget' ); ?></label>
620 621
		</p>

Erick Hitter's avatar
Erick Hitter committed
622
		<h3><?php _e( 'Include/Exclude Terms', 'taxonomy_dropdown_widget' ); ?></h3>
623 624

		<p>
Erick Hitter's avatar
Erick Hitter committed
625
			<label><?php _e( 'Include/exclude terms:', 'taxonomy_dropdown_widget' ); ?></label><br />
626

627
			<input type="radio" name="<?php echo $this->get_field_name( 'incexc' ); ?>" value="include" id="<?php echo $this->get_field_id( 'include' ); ?>"<?php checked( $options['incexc'], 'include', true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
628
			<label for="<?php echo $this->get_field_id( 'include' ); ?>"><?php _e( 'Include only the term IDs listed below', 'taxonomy_dropdown_widget' ); ?></label><br />
629

630
			<input type="radio" name="<?php echo $this->get_field_name( 'incexc' ); ?>" value="exclude" id="<?php echo $this->get_field_id( 'exclude' ); ?>"<?php checked( $options['incexc'], 'exclude', true ); ?> />
Erick Hitter's avatar
Erick Hitter committed
631
			<label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e( 'Exclude the term IDs listed below', 'taxonomy_dropdown_widget' ); ?></label>
632 633 634
		</p>

		<p>
Erick Hitter's avatar
Erick Hitter committed
635
			<label for="<?php echo $this->get_field_id( 'incexc_ids' ); ?>"><?php _e( 'Term IDs to include/exclude based on above setting:', 'taxonomy_dropdown_widget' ); ?></label><br />
636
			<input type="text" name="<?php echo $this->get_field_name( 'incexc_ids' ); ?>" class="widefat code" id="<?php echo $this->get_field_id( 'incexc_ids' ); ?>" value="<?php echo esc_attr( implode( ', ', $options['incexc_ids'] ) ); ?>" /><br />
Erick Hitter's avatar
Erick Hitter committed
637
			<span class="description"><small><?php _e( 'Enter comma-separated list of term IDs.', 'taxonomy_dropdown_widget' ); ?></small></span>
638 639
		</p>

Erick Hitter's avatar
Erick Hitter committed
640
		<h3><?php _e( 'Advanced', 'taxonomy_dropdown_widget' ); ?></h3>
641 642

		<p>
Erick Hitter's avatar
Erick Hitter committed
643
			<label for="<?php echo $this->get_field_id( 'threshold' ); ?>"><?php _e( 'Show terms assigned to at least this many posts:', 'taxonomy_dropdown_widget' ); ?></label><br />
644
			<input type="text" name="<?php echo $this->get_field_name( 'threshold' ); ?>" id="<?php echo $this->get_field_id( 'threshold' ); ?>" value="<?php echo intval( $options['threshold'] ); ?>" size="3" /><br />
Erick Hitter's avatar
Erick Hitter committed
645
			<span class="description"><small><?php _e( 'Set to <strong>0</strong> to display all terms matching the above criteria.', 'taxonomy_dropdown_widget' ); ?></small></span>
646 647 648 649 650 651 652 653 654 655
		</p>

	<?php
	}
}

/**
 ** HELPER FUNCTIONS
 **/

Erick Hitter's avatar
Erick Hitter committed
656
/**
657 658 659
 * Render taxonomy dropdown
 * @param array $options
 * @param string|int $id
Erick Hitter's avatar
Erick Hitter committed
660
 * @uses taxonomy_dropdown_widget_plugin::get_instance
661 662 663
 * @return string or false
 */
function taxonomy_dropdown_widget( $options = array(), $id = '' ) {
Erick Hitter's avatar
Erick Hitter committed
664 665
	// Sanitize options
	$options = taxonomy_dropdown_widget_plugin::get_instance()->sanitize_options( $options );
666

Erick Hitter's avatar
Erick Hitter committed
667
	return taxonomy_dropdown_widget_plugin::get_instance()->render_dropdown( $options, $id );
668 669 670 671 672 673
}

/**
 ** LEGACY FUNCTIONS FOR BACKWARDS COMPATIBILITY
 **/

674
if ( ! function_exists( 'generateTagDropdown' ) ) :
Erick Hitter's avatar
Erick Hitter committed
675
	/**
676 677
	 * Build tag dropdown based on provided arguments
	 * @since 1.7
678
	 * @uses _deprecated_function
Erick Hitter's avatar
Erick Hitter committed
679
	 * @uses taxonomy_dropdown_widget_plugin::get_instance
680 681 682
	 * @return string or false
	 */
	function generateTagDropdown( $args ) {
683 684
		_deprecated_function( 'generateTagDropdown', '2.0 of Taxonomy (Tag) Dropdown Widget', 'taxonomy_dropdown_widget' );

Erick Hitter's avatar
Erick Hitter committed
685 686
		// Sanitize options
		$options = taxonomy_dropdown_widget_plugin::get_instance()->sanitize_options( $args );
687

Erick Hitter's avatar
Erick Hitter committed
688
		return '<!-- NOTICE: The function used to generate this dropdown list is deprecated as of version 2.0 of Taxonomy Dropdown Widget. You should update your template to use `taxonomy_dropdown_widget()` instead. -->' . "\r\n" . taxonomy_dropdown_widget_plugin::get_instance()->render_dropdown( $options, 'legacy_gtd' );
689 690 691
	}
endif;

692
if ( ! function_exists( 'TDW_direct' ) ) :
Erick Hitter's avatar
Erick Hitter committed
693
	/**
694 695
	 * Build tag dropdown based on provided arguments
	 * @since 1.6
696
	 * @uses _deprecated_function
Erick Hitter's avatar
Erick Hitter committed
697
	 * @uses taxonomy_dropdown_widget_plugin::get_instance
698 699 700
	 * @return string or false
	 */
	function TDW_direct( $limit = false, $count = false, $exclude = false ) {
701 702
		_deprecated_function( 'TDW_direct', '1.7 of Taxonomy (Tag) Dropdown Widget', 'taxonomy_dropdown_widget' );

Erick Hitter's avatar
Erick Hitter committed
703
		// Build options array from function parameters
704 705
		$options = array(
			'max_name_length' => $limit,
Erick Hitter's avatar
Erick Hitter committed
706
			'post_count'      => $count,
707 708
		);

709
		if ( $exclude ) {
Erick Hitter's avatar
Erick Hitter committed
710
			$options[ 'incexc' ]     = 'exclude';
711 712 713
			$options[ 'incexc_ids' ] = $exclude;
		}

Erick Hitter's avatar
Erick Hitter committed
714
		// Sanitize options
Erick Hitter's avatar
Erick Hitter committed
715
		$options = taxonomy_dropdown_widget_plugin::get_instance()->sanitize_options( $options );
716

Erick Hitter's avatar
Erick Hitter committed
717
		echo '<!-- NOTICE: The function used to generate this dropdown list is deprecated as of version 1.7 of Taxonomy Dropdown Widget. You should update your template to use `taxonomy_dropdown_widget()` instead. -->' . "\r\n" . taxonomy_dropdown_widget_plugin::get_instance()->render_dropdown( $options, 'legacy_tdw' );
718 719 720
	}
endif;

721
if ( ! function_exists( 'makeTagDropdown' ) ) :
Erick Hitter's avatar
Erick Hitter committed
722
	/**
723 724
	 * Build tag dropdown based on provided arguments
	 * @since 1.3
725
	 * @uses _deprecated_function
Erick Hitter's avatar
Erick Hitter committed
726
	 * @uses taxonomy_dropdown_widget_plugin::get_instance
727 728 729
	 * @return string or false
	 */
	function makeTagDropdown( $limit = false ) {
730 731 732
		_deprecated_function( 'makeTagDropdown', '1.6 of Taxonomy (Tag) Dropdown Widget', 'taxonomy_dropdown_widget' );

		// Sanitize options
733
		$options = array(
Erick Hitter's avatar
Erick Hitter committed
734
			'max_name_length' => intval( $limit ),
735 736
		);

Erick Hitter's avatar
Erick Hitter committed
737
		echo '<!-- NOTICE: The function used to generate this dropdown list is deprecated as of version 1.6 of Taxonomy Dropdown Widget. You should update your template to use `taxonomy_dropdown_widget()` instead. -->' . "\r\n" . taxonomy_dropdown_widget_plugin::get_instance()->render_dropdown( $options, 'legacy_mtd' );
Erick Hitter's avatar
Erick Hitter committed
738
	}
739
endif;