<?php

/*
Plugin Name: Ordered Post Tags
Plugin URI: https://gist.github.com/3217544
Description: Proof of concept plugin, not for production use. Converts post tags to an ordered taxonomy.
Version: 0.1
Author: Simon Wheatley @ Code for the People Ltd
Author URI: http://codeforthepeople.com
*/

/**
 * Copyright 2012 Code for the People Ltd. All rights reserved
 * Released under the GPL license
 * http://www.opensource.org/licenses/gpl-license.php
 *
 * This is an add-on for WordPress
 * http://wordpress.org/
 *
 * **********************************************************************
 * 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.
 * **********************************************************************
 */

/**
 * Hooks the WordPress init action to interfere with the
  * post_tag taxonomy.
 *
 * @return void
 **/
function opt_init() {
	// Get the current args for the post_tag taxonomy
	$post_tag_tax = get_taxonomy( 'post_tag', ARRAY_A );
	// Convert to an array
	$post_tag_args = opt_convert_to_array( $post_tag_tax );
	// Now make it sortable
	$post_tag_args[ 'sort' ] = true;
	// Exploit a feature whereby you can overwrite a taxonomy :)
	register_taxonomy( 'post_tag', 'post', $post_tag_args );
}
add_action( 'init', 'opt_init' );

/**
 * A utility function that recursively traverses an object
 * and turns it into an associative array.
 *
 * @param mixed $thing Might be an object, might not be
 * @return array A (possibly multidimensional) array if an object (or array) was passed in
 **/
function opt_convert_to_array( & $thing ) {
	$thing = get_object_vars( $thing );
	if ( is_array( $thing ) )
		foreach ( $thing as $key => & $_thing )
			if ( is_object( $_thing ) )
				$thing[ $key ] = opt_convert_to_array( $_thing );
	return $thing;
}

?>