Make WordPress Core

Ticket #6305: wp-ajax-response-debug.php

File wp-ajax-response-debug.php, 3.0 KB (added by mdawaffe, 18 years ago)
Line 
1<?php
2/*
3Plugin Name: WP Ajax Response Debug
4Plugin URI: http://trac.wordpress.org/ticket/6305
5Author: Michael D Adams
6Version: 2.5-7434
7*/
8
9function wp_ajax_debug_js() {
10
11?>
12<script type="text/javascript">
13/* <![CDATA[ */
14if ( 'undefined' != typeof jQuery ) {
15jQuery( function($) {
16        if ( 'undefined' == typeof wpAjax ) { return; }
17        var parseAjaxResponse = wpAjax.parseAjaxResponse; // store the old function
18        wpAjax.parseAjaxResponse = function( x, r, e ) {
19                parsed = parseAjaxResponse( x, r, e ); // call the old function
20                if ( 'object' != typeof parsed ) { return parsed; } // if (bool) bail out
21
22
23                var div = $('<div></div>'); // helper to escape html
24
25                var re = jQuery('#' + r); // the ajax-response element;
26
27                var html = re.html();
28
29                html += parsed.errors ? "<br /><div class='error'><h4>Ajax Responses (has errors)</h4>" : "<div class='updated'><h4>Ajax Responses (no errors)</h4>";
30                html += "<ol>";
31
32                var first = true;
33                $.each( parsed.responses, function() { // foreach response
34                        if ( first ) {
35                                html += "<li>";
36                                first = false;
37                        } else {
38                                html += "<li style='border-top: 1px solid #999'>";
39                        }
40
41                        // "action" name and data sent back
42                        html += "<p>DATA (" + this.action + "): <pre>" + div.text( this.data ).html() + "</pre></p>";
43
44                        // what kind of data, what numeric id, what was it's old id (if any), where should it go in the list
45                        html += "<p>WHAT: " + this.what + " | ID: " + this.id.toString() + " | OLDID: " + parseInt(this.oldId).toString() + " | POS: " + this.position.toString() + "</p>";
46
47                        html += "<p>SUPPLEMENTAL:";
48                        var supp = '';
49                        $.each( this.supplemental, function(i,j) { // foreach suppplemental datum
50                                supp += "<li>" + i + ": " + div.text( j ).html() + "</li>";
51                        } );
52                        html += ( supp ? ( "<ul>" + supp + "</ul>" ) : ' (none)' ) + "</p>";
53
54                        if ( parsed.errors ) {
55                                html += "<p><strong>ERRORS</strong>:<ol>";
56                                $.each( this.errors, function() { // foreach error
57                                        html += "<li>";
58                                        html += "<p>CODE: " + this.code + "</p>"; // error code
59                                        html += "<p>MESSAGE: " + div.text( this.message ).html() + "</p>"; // error message
60                                        html += "<p>DATA:";
61                                        // This is kind of ghetto.  Maybe wpAjax.parseAjaxResponse should do this work for us as it does supplemental?
62                                        if ( this.data[0].firstChild == this.data[0].lastChild && this.data[0].firstChild.nodeType != 1 ) { // scalar error data
63                                                html += " " + $(this.data).text();
64                                        } else { // array/object error data
65                                                var dataClass = this.data[0].getAttribute( 'class' ); // 2 for some IE thing
66                                                if ( dataClass ) { html += " (object: " + dataClass + ")"; }
67                                                html += "<ul>"
68                                                $.each( this.data[0].childNodes, function() { // error datum
69                                                        html += "<li>" + this.tagName + ": " + $(this).text() + "</li>";
70                                                } );
71                                                html += "</ul>"
72                                        }
73
74                                        html += "</p></li>";
75                                } );
76                                html += "</ol></p>";
77                        }
78
79                        html += "</li>";
80                } );
81
82                html += "</ol></div>";
83
84                re.html( html );
85
86                return parsed;
87        }
88} );
89}
90/* ]]> */
91</script>
92<?php
93
94}
95
96add_action( 'admin_head', 'wp_ajax_debug_js' );