<?php
/*
Plugin Name: WP Ajax Response Debug
Plugin URI: http://trac.wordpress.org/ticket/6305
Author: Michael D Adams
Version: 2.5-7434
*/

function wp_ajax_debug_js() {

?>
<script type="text/javascript">
/* <![CDATA[ */
if ( 'undefined' != typeof jQuery ) {
jQuery( function($) {
	if ( 'undefined' == typeof wpAjax ) { return; }
	var parseAjaxResponse = wpAjax.parseAjaxResponse; // store the old function
	wpAjax.parseAjaxResponse = function( x, r, e ) {
		parsed = parseAjaxResponse( x, r, e ); // call the old function
		if ( 'object' != typeof parsed ) { return parsed; } // if (bool) bail out


		var div = $('<div></div>'); // helper to escape html

		var re = jQuery('#' + r); // the ajax-response element;

		var html = re.html();

		html += parsed.errors ? "<br /><div class='error'><h4>Ajax Responses (has errors)</h4>" : "<div class='updated'><h4>Ajax Responses (no errors)</h4>";
		html += "<ol>";

		var first = true;
		$.each( parsed.responses, function() { // foreach response
			if ( first ) {
				html += "<li>";
				first = false;
			} else {
				html += "<li style='border-top: 1px solid #999'>";
			}

			// "action" name and data sent back
			html += "<p>DATA (" + this.action + "): <pre>" + div.text( this.data ).html() + "</pre></p>";

			// what kind of data, what numeric id, what was it's old id (if any), where should it go in the list
			html += "<p>WHAT: " + this.what + " | ID: " + this.id.toString() + " | OLDID: " + parseInt(this.oldId).toString() + " | POS: " + this.position.toString() + "</p>";

			html += "<p>SUPPLEMENTAL:";
			var supp = '';
			$.each( this.supplemental, function(i,j) { // foreach suppplemental datum
				supp += "<li>" + i + ": " + div.text( j ).html() + "</li>";
			} );
			html += ( supp ? ( "<ul>" + supp + "</ul>" ) : ' (none)' ) + "</p>";

			if ( parsed.errors ) {
				html += "<p><strong>ERRORS</strong>:<ol>";
				$.each( this.errors, function() { // foreach error
					html += "<li>";
					html += "<p>CODE: " + this.code + "</p>"; // error code
					html += "<p>MESSAGE: " + div.text( this.message ).html() + "</p>"; // error message
					html += "<p>DATA:";
					// This is kind of ghetto.  Maybe wpAjax.parseAjaxResponse should do this work for us as it does supplemental?
					if ( this.data[0].firstChild == this.data[0].lastChild && this.data[0].firstChild.nodeType != 1 ) { // scalar error data
						html += " " + $(this.data).text();
					} else { // array/object error data
						var dataClass = this.data[0].getAttribute( 'class' ); // 2 for some IE thing
						if ( dataClass ) { html += " (object: " + dataClass + ")"; }
						html += "<ul>"
						$.each( this.data[0].childNodes, function() { // error datum
							html += "<li>" + this.tagName + ": " + $(this).text() + "</li>";
						} );
						html += "</ul>"
					}

					html += "</p></li>";
				} );
				html += "</ol></p>";
			}

			html += "</li>";
		} );

		html += "</ol></div>";

		re.html( html );

		return parsed;
	}
} );
}
/* ]]> */
</script>
<?php

}

add_action( 'admin_head', 'wp_ajax_debug_js' );
