Make WordPress Core

Ticket #3099: 3099.diff

File 3099.diff, 18.4 KB (added by mdawaffe, 18 years ago)

1

  • wp-includes/js/wp-ajax-js.php

     
     1<?php @require_once('../../wp-config.php'); cache_javascript_headers(); ?>
     2var WPAjax = Class.create();
     3Object.extend(WPAjax.prototype, Ajax.Request.prototype);
     4Object.extend(WPAjax.prototype, {
     5        WPComplete: false, // onComplete function
     6        WPError: false, // onWPError function
     7        initialize: function(url, responseEl) {
     8                var tempObj = this;
     9                this.transport = Ajax.getTransport();
     10                if ( !this.transport )
     11                        return false;
     12                this.setOptions( {
     13                        parameters: 'cookie=' + encodeURIComponent(document.cookie),
     14                        onComplete: function(transport) { // transport = XMLHttpRequest object
     15                                if ( tempObj.parseAjaxResponse() ) {
     16                                        if ( 'function' == typeof tempObj.WPComplete )
     17                                                tempObj.WPComplete(transport);
     18                                } else if ( 'function' == typeof tempObj.WPError ) // if response corresponds to an error (bad data, say, not 404)
     19                                        tempObj.WPError(transport);
     20                        }
     21                });
     22                this.url = url;
     23                this.getResponseElement(responseEl);
     24        },
     25        addArg: function(key, value) {
     26                var a = $H(this.options.parameters.parseQuery());
     27                a[encodeURIComponent(key)] = encodeURIComponent(value);
     28                this.options.parameters = a.map(function(pair) {
     29                        return pair.join('=');
     30                }).join('&');
     31        },
     32        getResponseElement: function(r) {
     33                var p = $(r + '-p');
     34                if ( !p ) {
     35                        new Insertion.Bottom(r, "<span id='" + r + "-p'></span>");
     36                        var p = $(r + '-p');
     37                }
     38                this.myResponseElement = p;
     39        },
     40        parseAjaxResponse: function() { // 1 = good, 0 = strange (bad data?), -1 = you lack permission
     41                if ( this.transport.responseXML && typeof this.transport.responseXML == 'object' ) // To do: allow for XML coded errors
     42                        return true;
     43                var r = this.transport.responseText;
     44                if ( isNaN(r) ) {
     45                        Element.update( this.myResponseElement, '<div class="error"><p>' + r + '</p></div>' );
     46                        return false;
     47                }
     48                var r = parseInt(r,10);
     49                if ( -1 == r ) {
     50                        Element.update( this.myResponseElement, "<div class='error'><p><?php _e("You don't have permission to do that."); ?></p></div>" );
     51                        return false;
     52                } else if ( 0 == r ) {
     53                        Element.update( this.myResponseElement, "<div class='error'><p><?php _e("Something strange happened.  Try refreshing the page."); ?></p></div>" );
     54                        return false;
     55                }
     56                return true;
     57        },
     58        addOnComplete: function(f) {
     59                if ( 'function' == typeof f ) { var of = this.WPComplete; this.WPComplete = function(t) { if ( of ) of(t); f(t); } }
     60        },
     61        addOnWPError: function(f) {
     62                if ( 'function' == typeof f ) { var of = this.WPError; this.WPError = function(t) { if ( of ) of(t); f(t); } }
     63        },
     64        notInitialized: function() {
     65                return this.transport ? false : true;
     66        }
     67});
  • wp-includes/js/list-manipulation-js.php

     
     1<?php
     2@require_once('../../wp-config.php');
     3cache_javascript_headers();
     4$handler =  get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php';
     5?>
     6addLoadEvent(function(){theList=new listMan();});
     7function deleteSomething(what,id,message,obj){if(!obj)obj=theList;if(!message)message="<?php printf(__('Are you sure you want to delete this %s?'),"'+what+'"); ?>";if(confirm(message))return obj.ajaxDelete(what,id);else return false;}
     8function dimSomething(what,id,dimClass,obj){if(!obj)obj=theList;return obj.ajaxDimmer(what,id,dimClass);}
     9
     10var listMan = Class.create();
     11Object.extend(listMan.prototype, {
     12        reg_color: '#FFFFFF',
     13        alt_color: '#F1F1F1',
     14        ajaxRespEl: 'ajax-response',
     15        ajaxHandler: '<?php echo $handler; ?>',
     16        inputData: '',
     17        clearInputs: [],
     18        showLink: true,
     19        topAdder: false,
     20        alt: 'alternate',
     21        addComplete: null,
     22        delComplete: null,
     23        dimComplete: null,
     24        dataStore: null,
     25
     26        initialize: function(theListId) {
     27                this.theList = $(theListId ? theListId : 'the-list');
     28                if ( !this.theList )
     29                        return false;
     30                this.theList.cleanWhitespace();
     31        },
     32
     33        // sends add-what and fields contained in where
     34        // recieves html with top element having an id like what-#
     35        ajaxAdder: function( what, where, update ) { // Do NOT wrap TR in TABLE TBODY
     36                var ajaxAdd = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
     37                if ( ajaxAdd.notInitialized() )
     38                        return true;
     39                ajaxAdd.options.parameters += '&action=' + ( update ? 'update-' : 'add-' ) + what + '&' + this.grabInputs(where) + this.inputData;
     40                var tempObj=this;
     41                ajaxAdd.addOnComplete( function(transport) {
     42                        var newItems = $A(transport.responseXML.getElementsByTagName(what));
     43                        if ( newItems ) {
     44                                newItems.each( function(i) {
     45                                        var id = getNodeValue(i,'id');
     46                                        var exists = $(what+'-'+id);
     47                                        if ( exists )
     48                                                tempObj.replaceListItem( exists, getNodeValue(i,'newitem'), update );
     49                                        else
     50                                                tempObj.addListItem( getNodeValue(i, 'newitem') );
     51                                        if ( tempObj.showLink )
     52                                                tempObj.showLink = id;
     53                                });
     54                        }
     55                        ajaxAdd.myResponseElement.update(tempObj.showLink ? ( "<div id='jumplink' class='updated fade'><p><a href='#" + what + '-' + tempObj.showLink + "'><?php _e('Jump to new item'); ?></a></p></div>" ) : '');
     56                        tempObj.clear();
     57                        if ( tempObj.addComplete && typeof tempObj.addComplete == 'function' )
     58                                tempObj.addComplete( what, where, update );
     59                        tempObj.recolorList();
     60                });
     61                ajaxAdd.request(ajaxAdd.url);
     62                return false;
     63        },
     64
     65        // sends update-what and fields contained in where
     66        // recieves html with top element having an id like what-#
     67        ajaxUpdater: function( what, where ) { return this.ajaxAdder( what, where, true ); },
     68
     69        // sends delete-what and id#
     70        ajaxDelete: function( what, id ) {
     71                var ajaxDel = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
     72                if( ajaxDel.notInitialized() )
     73                        return true;
     74                var tempObj = this;
     75                var action = 'delete-' + what + '&id=' + id;
     76                var idName = what.replace('-as-spam','') + '-' + id;
     77                ajaxDel.addOnComplete( function(transport) {
     78                        ajaxDel.myResponseElement.update('');
     79                        tempObj.destore(action);
     80                        if( tempObj.delComplete && typeof tempObj.delComplete == 'function' )
     81                                tempObj.delComplete( what, id );
     82                });
     83                ajaxDel.addOnWPError( function(transport) { tempObj.restore(action, true); });
     84                ajaxDel.options.parameters += '&action=' + action + this.inputData;
     85                ajaxDel.request(ajaxDel.url);
     86                this.store(action, idName);
     87                tempObj.removeListItem( idName );
     88                return false;
     89        },
     90
     91        // Toggles class nomes
     92        // sends dim-what and id#
     93        ajaxDimmer: function( what, id, dimClass ) {
     94                ajaxDim = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
     95                if ( ajaxDim.notInitialized() )
     96                        return true;
     97                var tempObj = this;
     98                var action = 'dim-' + what + '&id=' + id;
     99                var idName = what + '-' + id;
     100                ajaxDim.addOnComplete( function(transport) {
     101                        ajaxDim.myResponseElement.update('');
     102                        tempObj.destore(action);
     103                        if ( tempObj.dimComplete && typeof tempObj.dimComplete == 'function' )
     104                                tempObj.dimComplete( what, id, dimClass);
     105                });
     106                ajaxDim.addOnWPError( function(transport) { tempObj.restore(action, true); });
     107                ajaxDim.options.parameters += '&action=' + action + this.inputData;
     108                ajaxDim.request(ajaxDim.url);
     109                this.store(action, idName);
     110                this.dimItem( idName, dimClass );
     111                return false;
     112        },
     113
     114        addListItem: function( h ) {
     115                new Insertion[this.topAdder ? 'Top' : 'Bottom'](this.theList,h);
     116                this.theList.cleanWhitespace();
     117                var id = this.topAdder ? this.theList.firstChild.id : this.theList.lastChild.id;
     118                if ( this.alt )
     119                        if ( this.theList.childNodes.length % 2 )
     120                                $(id).addClassName(this.alt);
     121                Fat.fade_element(id);
     122        },
     123
     124        // only hides the element sa it can be put back again if necessary
     125        removeListItem: function( id, noFade ) {
     126                id = $(id);
     127                if ( !noFade ) {
     128                        Fat.fade_element(id.id,null,700,'#FF3333');
     129                        var tempObj = this;
     130                        var func = function() { id.hide(); tempObj.recolorList(); }
     131                        setTimeout(func, 705);
     132                } else {
     133                        id.hide();
     134                        this.recolorList();
     135                }
     136        },
     137
     138        replaceListItem: function( id, h, update ) {
     139                id = $(id);
     140                if ( !update ) {
     141                        id.remove();
     142                        this.addListItem( h );
     143                        return;
     144                }
     145                id.replace(h);
     146                Fat.fade_element(id.id);
     147        },
     148
     149        // toggles class
     150        dimItem: function( id, dimClass, noFade ) {
     151                id = $(id);
     152                if ( id.hasClassName(dimClass) ) {
     153                        if ( !noFade )
     154                                Fat.fade_element(id.id,null,700,null);
     155                        id.removeClassName(dimClass);
     156                } else {
     157                        if ( !noFade )
     158                                Fat.fade_element(id.id,null,700,'#FF3333');
     159                        id.addClassName(dimClass);
     160                }
     161        },
     162
     163        // store an element in case we need it later
     164        store: function(action, id) {
     165                if ( !this.dataStore )
     166                        this.dataStore = $H();
     167                this.dataStore[action] = $(id).cloneNode(true);
     168        },
     169
     170        // delete from store
     171        destore: function(action) { delete(this.dataStore[action]); },
     172
     173        // restore element from store into existing (possibly hidden) element of same id
     174        restore: function(action, error) {
     175                var id = this.dataStore[action].id;
     176                this.theList.replaceChild(this.dataStore[action], $(id));
     177                delete(this.dataStore[action]);
     178                if ( error ) {
     179                        func = function() { $(id).setStyle( { 'background-color': '#FF3333' } ); }
     180                        func(); setTimeout(func, 705); // Hit it twice in case it's still fading.
     181                }
     182        },
     183
     184        // Like Form.serialize, but excludes action and sets up clearInputs
     185        grabInputs: function( where ) {
     186                var elements = Form.getElements($(where));
     187                var queryComponents = new Array();
     188                for (var i = 0; i < elements.length; i++) {
     189                        if ( 'action' == elements[i].name )
     190                                continue;
     191                        if ( 'hidden' != elements[i].type && 'submit' != elements[i].type && 'button' != elements[i].type )
     192                                this.clearInputs.push(elements[i]);
     193                        var queryComponent = Form.Element.serialize(elements[i]);
     194                        if (queryComponent)
     195                                queryComponents.push(queryComponent);
     196                }
     197                return queryComponents.join('&');
     198        },
     199
     200        // form.reset() can only do whole forms.  This can do subsections.
     201        clear: function() {
     202                this.clearInputs.each( function(i) {
     203                        if ( 'textarea' == i.tagName.toLowerCase() )
     204                                i.value = '';
     205                        else
     206                                switch ( i.type.toLowerCase() ) {
     207                                        case 'password': case 'text':
     208                                                i.value = '';
     209                                                break;
     210                                        case 'checkbox': case 'radio':
     211                                                i.checked = false;
     212                                                break;
     213                                        case 'select': case 'select-one':
     214                                                i.selectedIndex = null;
     215                                                break;
     216                                        case 'select-multiple':
     217                                                for (var o = 0; o < i.length; o++) i.options[o].selected = false;
     218                                                break;
     219                                }
     220                });
     221                this.clearInputs = [];
     222        },
     223
     224        recolorList: function() {
     225                if ( !this.alt )
     226                        return;
     227                var alt = this.alt;
     228                var listItems = $A(this.theList.childNodes).findAll( function(i) { return i.visible() } );
     229                listItems.each( function(i,n) {
     230                        if ( n % 2 )
     231                                i.removeClassName(alt);
     232                        else
     233                                i.addClassName(alt);
     234                });
     235        }
     236});
     237
     238//No submit unless code returns true.
     239function killSubmit ( code, e ) {
     240        e = e ? e : window.event;
     241        if ( !e ) return;
     242        var t = e.target ? e.target : e.srcElement;
     243        if ( ( 'text' == t.type && e.keyCode == 13 ) || ( 'submit' == t.type && 'click' == e.type ) ) {
     244                if ( ( 'string' == typeof code && !eval(code) ) || ( 'function' == typeof code && !code() ) ) {
     245                        e.returnValue = false; e.cancelBubble = true; return false;
     246                }
     247        }
     248}
     249//Pretty func adapted from ALA http://www.alistapart.com/articles/gettingstartedwithajax
     250function getNodeValue(tree,el){try { var r = tree.getElementsByTagName(el)[0].firstChild.nodeValue; } catch(err) { var r = null; } return r; }
     251//Generic but lame JS closure
     252function encloseFunc(f){var a=arguments[1];return function(){return f(a);}}
  • wp-includes/script-loader.php

     
    1919                $this->add( 'wp_tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('tiny_mce'), '04162006' );
    2020                $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0');
    2121                $this->add( 'autosave', '/wp-includes/js/autosave.js.php', array('prototype', 'sack'), '4107');
     22                $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), rand());
     23                $this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), rand());
    2224                if ( is_admin() ) {
    2325                        $this->add( 'dbx-admin-key', '/wp-admin/dbx-admin-key-js.php', array('dbx'), '3651' );
    24                         $this->add( 'listman', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '4042' ); // Make changeset # the correct one
     26                        $this->add( 'listman-old', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '4042' ); // Make changeset # the correct one
    2527                        $this->add( 'ajaxcat', '/wp-admin/cat-js.php', array('listman'), '3684' );
    2628                        $this->add( 'admin-categories', '/wp-admin/categories.js', array('listman'), '3684' );
    2729                        $this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman'), '3733' );
  • wp-admin/custom-fields.js

     
    11function customFieldsOnComplete() {
    2         var pidEl = document.getElementById('post_ID');
     2        var pidEl = $('post_ID');
    33        pidEl.name = 'post_ID';
    44        pidEl.value = getNodeValue(theList.ajaxAdd.responseXML, 'postid');
    5         var aEl = document.getElementById('hiddenaction')
     5        var aEl = $('hiddenaction')
    66        if ( aEl.value == 'post' ) aEl.value = 'postajaxpost';
    77}
    88addLoadEvent(customFieldsAddIn);
     
    2121                }
    2222        }
    2323
    24         document.getElementById('metakeyinput').onkeypress = function(e) {return killSubmit('theList.inputData+="&id="+document.getElementById("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
    25         document.getElementById('updatemetasub').onclick = function(e) {return killSubmit('theList.inputData+="&id="+document.getElementById("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
     24        $('metakeyinput').onkeypress = function(e) {return killSubmit('theList.inputData+="&id="+$("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
     25        $('updatemetasub').onclick = function(e) {return killSubmit('theList.inputData+="&id="+$("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
    2626}
  • wp-admin/admin-ajax.php

     
    55
    66define('DOING_AJAX', true);
    77
    8 
    98check_ajax_referer();
    109if ( !is_user_logged_in() )
    1110        die('-1');
     
    1716        $value = wp_specialchars($value, true);
    1817        $key_js = addslashes(wp_specialchars($key, 'double'));
    1918        $key = wp_specialchars($key, true);
    20         $r  = "<meta><id>$mid</id><postid>$pid</postid><newitem><![CDATA[<table><tbody>";
     19        $r  = "<meta><id>$mid</id><postid>$pid</postid><newitem><![CDATA[";
    2120        $r .= "<tr id='meta-$mid'><td valign='top'>";
    2221        $r .= "<input name='meta[$mid][key]' tabindex='6' onkeypress='return killSubmit(\"theList.ajaxUpdater(&#039;meta&#039;,&#039;meta-$mid&#039;);\",event);' type='text' size='20' value='$key' />";
    2322        $r .= "</td><td><textarea name='meta[$mid][value]' tabindex='6' rows='2' cols='30'>$value</textarea></td><td align='center'>";
     
    2524        $r .= "<input name='deletemeta[$mid]' type='submit' onclick=\"return deleteSomething( 'meta', $mid, '";
    2625        $r .= sprintf(__("You are about to delete the &quot;%s&quot; custom field on this post.\\n&quot;OK&quot; to delete, &quot;Cancel&quot; to stop."), $key_js);
    2726        $r .= "' );\" class='deletemeta' tabindex='6' value='Delete' />";
    28         $r .= "</td></tr></tbody></table>]]></newitem></meta>";
     27        $r .= "</td></tr>]]></newitem></meta>";
    2928        return $r;
    3029}
    3130
     
    148147        $cat_full_name = wp_specialchars( $cat_full_name, 1 );
    149148
    150149        $r  = "<?xml version='1.0' standalone='yes'?><ajaxresponse>";
    151         $r .= "<cat><id>$cat->cat_ID</id><name>$cat_full_name</name><newitem><![CDATA[<table><tbody>";
     150        $r .= "<cat><id>$cat->cat_ID</id><name>$cat_full_name</name><newitem><![CDATA[";
    152151        $r .= _cat_row( $cat, $level, $cat_full_name );
    153         $r .= "</tbody></table>]]></newitem></cat></ajaxresponse>";
     152        $r .= "]]></newitem></cat></ajaxresponse>";
    154153        header('Content-type: text/xml');
    155154        die($r);
    156155        break;
     
    210209        } elseif ( !$user_id ) {
    211210                die('0');
    212211        }
    213         $r  = "<?xml version='1.0' standalone='yes'?><ajaxresponse><user><id>$user_id</id><newitem><![CDATA[<table><tbody>";
     212        $r  = "<?xml version='1.0' standalone='yes'?><ajaxresponse><user><id>$user_id</id><newitem><![CDATA[";
    214213        $r .= user_row( $user_id );
    215         $r .= "</tbody></table>]]></newitem></user></ajaxresponse>";
     214        $r .= "]]></newitem></user></ajaxresponse>";
    216215        header('Content-type: text/xml');
    217216        die($r);
    218217        break;
  • wp-admin/cat-js.php

     
    55addLoadEvent(function(){catList=new listMan('categorychecklist');catList.ajaxRespEl='jaxcat';catList.topAdder=1;catList.alt=0;catList.showLink=0;});
    66addLoadEvent(newCatAddIn);
    77function newCatAddIn() {
    8         if ( !document.getElementById('jaxcat') ) return false;
    9         var ajaxcat = document.createElement('span');
    10         ajaxcat.id = 'ajaxcat';
    11 
    12         newcat = document.createElement('input');
    13         newcat.type = 'text';
    14         newcat.name = 'newcat';
    15         newcat.id = 'newcat';
    16         newcat.size = '16';
    17         newcat.setAttribute('autocomplete', 'off');
    18         newcat.onkeypress = function(e) { return killSubmit("catList.ajaxAdder('category','categorydiv');", e); };
    19 
    20         var newcatSub = document.createElement('input');
    21         newcatSub.type = 'button';
    22         newcatSub.name = 'Button';
    23         newcatSub.id = 'catadd';
    24         newcatSub.value = 'Add';
    25         newcatSub.onclick = function() { catList.ajaxAdder('category', 'categorydiv'); };
    26 
    27         ajaxcat.appendChild(newcat);
    28         ajaxcat.appendChild(newcatSub);
    29         document.getElementById('jaxcat').appendChild(ajaxcat);
    30 
    31         howto = document.createElement('span');
    32         howto.innerHTML = "<?php _e('Separate multiple categories with commas.'); ?>";
    33         howto.id = 'howto';
    34         ajaxcat.appendChild(howto);
     8        var jaxcat = $('jaxcat');
     9        if ( !jaxcat )
     10                return false;
     11        jaxcat.update('<span id="ajaxcat"><input type="text" name="newcat" id="newcat" size="16" autocomplete="off"/><input type="button" name="Button" id="catadd" value="Add"/><span id="howto"><?php _e('Separate multiple categories with commas.'); ?></span></span>');
     12        $('newcat').onkeypress = function(e) { return killSubmit("catList.ajaxAdder('category','jaxcat');", e); };
     13        $('catadd').onclick = function() { catList.ajaxAdder('category', 'jaxcat'); };
    3514}