media-taxonomies.php 11.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
<?php
/*
Plugin Name: Media Taxonomies
Plugin URI: http://horttcore.de
Description: Taxononmies for media files
Version: 1.3.0
Author: Ralf Hortt
Author URI: http://horttcore.de
License: GPL2
*/


/**
 * Media_Taxonomies
 */
class Media_Taxonomies
{



	/** Refers to a single instance of this class. */
	private static $instance = null;



	/**
	 * Constructor
	 *
	 * @access public
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	public function __construct()
	{

		add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_assets' ) );
		add_action( 'admin_head', array( $this, 'admin_head' ) );
		add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
		add_action( 'init', array( $this, 'register_taxonomy' ) );
		add_filter( 'manage_edit-attachment_sortable_columns', array( $this, 'manage_edit_attachment_sortable_columns' ) );
		add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 0, 1 );
		add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
		add_action( 'wp_ajax_save-media-terms', array( $this, 'save_media_terms' ), 0, 1 );
		add_action( 'wp_ajax_add-media-term', array( $this, 'add_media_term' ), 0, 1 );
		load_plugin_textdomain( 'media-taxonomies', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

	} // end __construct



	/**
	 * Add media term
	 * ajax callback
	 *
	 * @since v.1.3
	 * @author Ralf Hortt
	 **/
	public function add_media_term()
	{
		$response = array();
		$attachment_id = intval( $_REQUEST['attachment_id'] );
		$taxonomy = get_taxonomy( sanitize_text_field( $_REQUEST['taxonomy'] ) );
		$parent = ( intval( $_REQUEST['parent'] ) > 0 ) ? intval( $_REQUEST['parent'] ) : 0;

		// Check if term already exists
		$term = get_term_by( 'name', sanitize_text_field( $_REQUEST['term'] ), $taxonomy->name );

		// No, so lets add it
		if ( !$term ) :
			$term = wp_insert_term( sanitize_text_field( $_REQUEST['term'] ), $taxonomy->name, array( 'parent' => $parent ) );
			$term = get_term_by( 'id', $term['term_id'], $taxonomy->name );
		endif;

		// Connect attachment with term
		wp_set_object_terms( $attachment_id, $term->term_id, $taxonomy->name, TRUE );

		$attachment_terms = wp_get_object_terms( $attachment_id, $taxonomy->name, array(
			'fields' => 'ids'
		));

		ob_start();
		wp_terms_checklist( 0, array(
			'selected_cats'         => $attachment_terms,
			'taxonomy'              => $taxonomy->name,
			'checked_ontop'         => FALSE
		));
		$checklist = ob_get_contents();
		ob_end_clean();

		$response['checkboxes'] = $checklist;
		$response['selectbox'] = wp_dropdown_categories( array(
			'taxonomy' => $taxonomy->name,
			'class' => 'parent-' . $taxonomy->name,
			'id' => 'parent-' . $taxonomy->name,
			'name' => 'parent-' . $taxonomy->name,
			'show_option_none' => '- ' . $taxonomy->labels->parent_item . ' -',
			'hide_empty' => FALSE,
			'echo' => FALSE,
		) );

		die( json_encode( $response ) );
	}



	/**
	 * Enqueue admin scripts and styles
	 *
	 * @access public
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	public function admin_enqueue_assets()
	{

		wp_enqueue_script( 'media-taxonomies', plugins_url( 'javascript/media-taxonomies.js', __FILE__ ), array( 'jquery' ) );
		wp_enqueue_style( 'media-taxonomies', plugins_url( 'css/media-taxonomies.css', __FILE__ ) );

	} // end admin_enqueue_assets



	/**
	 * Add taxonomy information
	 *
	 * @access public
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	public function admin_head()
	{

		$taxonomies = apply_filters( 'media-taxonomies', get_object_taxonomies( 'attachment', 'objects' ) );

		if ( !$taxonomies )
			return;

		$attachment_taxonomies = $attachment_terms = array();

		foreach ( $taxonomies as $taxonomyname => $taxonomy ) :

			$terms = get_terms( $taxonomy->name, array(
				'orderby'       => 'name',
				'order'         => 'ASC',
				'hide_empty'    => true,
			) );

			if ( !$terms )
				break;

			$attachment_taxonomies[$taxonomy->name] = $taxonomy->labels->name;
			$attachment_terms[ $taxonomy->name ][] = array( 'id' => 0, 'label' => __('All') . ' ' . $taxonomy->labels->name, 'slug' => '' );

			foreach ( $terms as $term )
				$attachment_terms[ $taxonomy->name ][] = array( 'id' => $term->term_id, 'label' => $term->name, 'slug' => $term->slug );

		endforeach;


		?>
		<script type="text/javascript">
			var mediaTaxonomies = <?php echo json_encode( $attachment_taxonomies ) ?>,
				mediaTerms = <?php echo json_encode( $attachment_terms ) ?>;
		</script>
		<?php

	} // end admin_head



	/**
	 * Add taxonomy checkboxes
	 *
	 * @access public
	 * @param array $fields Fields
	 * @param obj $post Post obj
	 * @return array Fields
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	public function attachment_fields_to_edit( $fields, $post )
	{

		$screen = get_current_screen();

		if ( isset( $screen->id ) && 'attachment' == $screen->id )
			return $fields;

		$taxonomies = apply_filters( 'media-taxonomies', get_object_taxonomies( 'attachment', 'objects' ) );

		if ( !$taxonomies )
			return $fields;

		foreach ( $taxonomies as $taxonomyname => $taxonomy ) :

			$fields[$taxonomyname] = array(
				'label' => $taxonomy->labels->singular_name,
				'input' => 'html',
				'html' => $this->terms_checkboxes( $taxonomy, $post->ID ),
				// 'value' => '',
				// 'helps' => '',
				'show_in_edit' => true,
			);

		endforeach;

		return $fields;

	} // end attachment_fields_to_edit



	/**
	* Creates or returns an instance of this class.
	*
	* @access public
	* @return A single instance of this class.
	* @since v0.9
	*/
	public static function get_instance() {

		if ( null == self::$instance )
			self::$instance = new self;

		return self::$instance;

	} // end get_instance;



	/**
	 *
	 * Filter attachments in modal box
	 *
	 * @access public
	 * @since v0.9.1
	 * @author Ralf Hortt
	 */
	public function pre_get_posts( $query )
	{
		if ( !isset( $query->query_vars['post_type'] ) || 'attachment' != $query->query_vars['post_type'] )
			return;

		$taxonomies = apply_filters( 'media-taxonomies', get_object_taxonomies( 'attachment', 'objects' ) );

		if ( !$taxonomies )
			return;

		foreach ( $taxonomies as $taxonomyname => $taxonomy ) :

			if ( isset( $_REQUEST['query'][$taxonomyname] ) && $_REQUEST['query'][$taxonomyname]['term_slug'] )	 :
				$query->set( $taxonomyname, $_REQUEST['query'][$taxonomyname]['term_slug'] );
			elseif ( isset( $_REQUEST[$taxonomyname] ) && is_numeric( $_REQUEST[$taxonomyname] ) && 0 != intval( $_REQUEST[$taxonomyname] ) ) :
				$term = get_term_by( 'id', $_REQUEST[$taxonomyname], $taxonomyname );
				if ( is_object( $term ) )
					set_query_var( $taxonomyname, $term->slug );
			endif;

		endforeach;

	} // end pre_get_posts



	/**
	 *
	 * Register taxonomy
	 *
	 * @access public
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	public function register_taxonomy()
	{

		register_taxonomy( 'media-category', array( 'attachment' ), array(
			'hierarchical' => TRUE,
			'labels' => array(
				'name' => _x( 'Categories', 'taxonomy general name' ),
				'singular_name' => _x( 'Category', 'taxonomy singular name' ),
				'search_items' =>  __( 'Search Categories' ),
				'all_items' => __( 'All Categories' ),
				'parent_item' => __( 'Parent Category' ),
				'parent_item_colon' => __( 'Parent Category:' ),
				'edit_item' => __( 'Edit Category' ),
				'update_item' => __( 'Update Category' ),
				'add_new_item' => __( 'Add New Category' ),
				'new_item_name' => __( 'New Category Name' ),
				'menu_name' => __( 'Categories' ),
			),
			'show_ui' => TRUE,
			'query_var' => TRUE,
			'rewrite' => array( 'slug' => _x( 'media-category', 'Category Slug', 'media-taxonomies' ) ),
			'show_admin_column' => TRUE,
			'update_count_callback' => '_update_generic_term_count',
		));

		register_taxonomy( 'media-tag', array( 'attachment' ), array(
			'hierarchical' => FALSE,
			'labels' => array(
				'name' => _x( 'Tags', 'taxonomy general name' ),
				'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
				'search_items' =>  __( 'Search Tags' ),
				'all_items' => __( 'All Tags' ),
				'parent_item' => __( 'Parent Tag' ),
				'parent_item_colon' => __( 'Parent Tag:' ),
				'edit_item' => __( 'Edit Tag' ),
				'update_item' => __( 'Update Tag' ),
				'add_new_item' => __( 'Add New Tag' ),
				'new_item_name' => __( 'New Tag Name' ),
				'menu_name' => __( 'Tags' ),
			),
			'show_ui' => TRUE,
			'query_var' => TRUE,
			'rewrite' => array( 'slug' => _x( 'media-tag', 'Tag Slug', 'media-taxonomies' ) ),
			'show_admin_column' => TRUE,
			'update_count_callback' => '_update_generic_term_count',
		));

	} // end register_taxonomy;



	/**
	 * Add custom filters in attachment listing
	 *
	 * @access public
	 * @since v0.9
	 * @author Ralf Hortt
	 **/
	public function restrict_manage_posts()
	{

		global $wp_query;

		$taxonomies = apply_filters( 'media-taxonomies', get_object_taxonomies( 'attachment', 'objects' ) );

		if ( !$taxonomies )
			return;

		foreach ( $taxonomies as $taxonomyname => $taxonomy ) :

			wp_dropdown_categories( array(
				'show_option_all' => sprintf( _x( 'View all %s', '%1$s = plural, %2$s = singular', 'media-taxonomies' ), $taxonomy->labels->name, $taxonomy->labels->singular_name ),
				'taxonomy' => $taxonomyname,
				'name' => $taxonomyname,
				'orderby' => 'name',
				'selected' => ( isset( $wp_query->query[$taxonomyname] ) ? $wp_query->query[$taxonomyname] : '' ),
				'hierarchical' => TRUE,
				'hide_empty' => TRUE,
				'hide_if_empty' => TRUE,
			) );

		endforeach;

	} // end restrict_manage_posts



	/**
	 * Save media terms
	 *
	 * @todo security nonce
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	public function save_media_terms()
	{

		$post_id = intval( $_REQUEST['attachment_id'] );

		if ( !current_user_can( 'edit_post', $post_id ) )
			die();

		$term_ids = array_map( 'intval', $_REQUEST['term_ids'] );

		$response = wp_set_post_terms( $post_id, $term_ids, sanitize_text_field( $_REQUEST['taxonomy'] ) );
		wp_update_term_count_now( $term_ids, sanitize_text_field( $_REQUEST['taxonomy'] ) );

	} // end save_media_terms



	/**
	 * Create a terms box
	 *
	 * @access protected
	 * @param obj $taxonomy Taxonomy
	 * @return str HTML output
	 * @since v0.9
	 * @author Ralf Hortt
	 */
	protected function terms_checkboxes( $taxonomy, $post_id )
	{

		if ( !is_object( $taxonomy ) ) :

			$taxonomy = get_taxonomy( $taxonomy );

		endif;

		$terms = get_terms( $taxonomy->name, array(
			'hide_empty' => FALSE,
		));

		$attachment_terms = wp_get_object_terms( $post_id, $taxonomy->name, array(
			'fields' => 'ids'
		));

		ob_start();

		?>
		<div class="media-term-section">

			<div class="media-terms" data-id="<?php echo $post_id ?>" data-taxonomy="<?php echo $taxonomy->name ?>">

				<ul>
					<?php
					wp_terms_checklist( 0, array(
						'selected_cats'         => $attachment_terms,
						'taxonomy'              => $taxonomy->name,
						'checked_ontop'         => FALSE
					));
					?>
				</ul>

			</div><!-- .media-terms -->

			<a href="#" class="toggle-add-media-term"><?php echo $taxonomy->labels->add_new_item ?></a>

			<div class="add-new-term">

				<input type="text" value="">

				<?php
				if ( 1 == $taxonomy->hierarchical ) :
					wp_dropdown_categories( array(
						'taxonomy' => $taxonomy->name,
						'class' => 'parent-' . $taxonomy->name,
						'id' => 'parent-' . $taxonomy->name,
						'name' => 'parent-' . $taxonomy->name,
						'show_option_none' => '- ' . $taxonomy->labels->parent_item . ' -',
						'hide_empty' => FALSE,
					) );
				endif;
				?>

				<button class="button save-media-term" data-taxonomy="<?php echo $taxonomy->name ?>" data-id="<?php echo $post_id ?>"><?php echo $taxonomy->labels->add_new_item ?></button>

			</div><!-- .add-new-term -->

		</div><!-- .media-term-section -->

		<?php

		$output = ob_get_contents();
		ob_end_clean();

		return apply_filters( 'media-checkboxes', $output, $taxonomy, $terms );

	} // end terms_checkboxes



}


Media_Taxonomies::get_instance();