General: Explicitly assigns all JS globals to the window.

Many variables in the JavaScript were defined in the global scope without being explicitly assigned to the window. When built with Webpack, the code gets encapsulated in anonymous functions and those implicit globals get assigned to the wrong scope. This patch prevents that from happening.

Fixes #44371. See #43731.


git-svn-id: https://develop.svn.wordpress.org/trunk@43577 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Omar Reiss 2018-08-19 13:32:27 +00:00
parent 7d54198c7c
commit 8347d7e22f
22 changed files with 111 additions and 128 deletions

View File

@ -3,7 +3,7 @@
*/
/* global setUserSetting, ajaxurl, commonL10n, alert, confirm, pagenow */
var showNotice, adminMenu, columns, validateForm, screenMeta;
/* global columns, screenMeta */
/**
* Adds common WordPress functionality to the window.
@ -23,7 +23,7 @@ var showNotice, adminMenu, columns, validateForm, screenMeta;
* @since 2.7.0
* @deprecated 3.3.0
*/
adminMenu = {
window.adminMenu = {
init : function() {},
fold : function() {},
restoreMenuState : function() {},
@ -32,7 +32,7 @@ adminMenu = {
};
// Show/hide/save table columns.
columns = {
window.columns = {
/**
* Initializes the column toggles in the screen options.
@ -158,7 +158,7 @@ $document.ready(function(){columns.init();});
*
* @returns {boolean} Returns true if all required fields are not an empty string.
*/
validateForm = function( form ) {
window.validateForm = function( form ) {
return !$( form )
.find( '.form-required' )
.filter( function() { return $( ':input:visible', this ).val() === ''; } )
@ -178,7 +178,7 @@ validateForm = function( form ) {
*
* @returns {void}
*/
showNotice = {
window.showNotice = {
/**
* Shows a delete confirmation pop-up message.
@ -219,7 +219,7 @@ showNotice = {
*
* @returns {void}
*/
screenMeta = {
window.screenMeta = {
element: null, // #screen-meta
toggles: null, // .screen-meta-toggle
page: null, // #wpcontent

View File

@ -3,7 +3,7 @@
*/
/* global adminCommentsL10n, thousandsSeparator, list_args, QTags, ajaxurl, wpAjax */
var setCommentsList, theList, theExtraList, commentReply;
/* global commentReply, theExtraList, theList, setCommentsList */
(function($) {
var getCount, updateCount, updateCountText, updatePending, updateApproved,
@ -188,7 +188,7 @@ var getCount, updateCount, updateCountText, updatePending, updateApproved,
});
};
setCommentsList = function() {
window.setCommentsList = function() {
var totalInput, perPageInput, pageInput, dimAfter, delBefore, updateTotalCount, delAfter, refillTheExtraList, diff,
lastConfidentTime = 0;
@ -564,8 +564,8 @@ setCommentsList = function() {
});
};
theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
window.theExtraList = $('#the-extra-comment-list').wpList( { alt: '', delColor: 'none', addColor: 'none' } );
window.theList = $('#the-comment-list').wpList( { alt: '', delBefore: delBefore, dimAfter: dimAfter, delAfter: delAfter, addColor: 'none' } )
.bind('wpListDelEnd', function(e, s){
var wpListsData = $(s.target).attr('data-wp-lists'), id = s.element.replace(/[^0-9]+/g, '');
@ -574,7 +574,7 @@ setCommentsList = function() {
});
};
commentReply = {
window.commentReply = {
cid : '',
act : '',
originalContent : '',

View File

@ -5,7 +5,7 @@
* @output wp-admin/js/inline-edit-post.js
*/
/* global inlineEditL10n, ajaxurl, typenow */
/* global inlineEditL10n, ajaxurl, typenow, inlineEditPost */
window.wp = window.wp || {};
@ -22,10 +22,9 @@ window.wp = window.wp || {};
* @property {string} what The prefix before the post id.
*
*/
var inlineEditPost;
( function( $, wp ) {
inlineEditPost = {
window.inlineEditPost = {
/**
* Initializes the inline and bulk post editor.

View File

@ -4,7 +4,7 @@
* @output wp-admin/js/inline-edit-tax.js
*/
/* global inlineEditL10n, ajaxurl */
/* global inlineEditL10n, ajaxurl, inlineEditTax */
window.wp = window.wp || {};
@ -17,11 +17,9 @@ window.wp = window.wp || {};
* @property {string} what The type property with a hash prefixed and a dash
* suffixed.
*/
var inlineEditTax;
( function( $, wp ) {
inlineEditTax = {
window.inlineEditTax = {
/**
* Initializes the inline taxonomy editor by adding event handlers to be able to

View File

@ -12,9 +12,7 @@
* @requires jQuery
*/
/* global tinymce, QTags */
var wpActiveEditor, send_to_editor;
/* global tinymce, QTags, wpActiveEditor, tb_position */
/**
* Sends the HTML passed in the parameters to TinyMCE.
@ -28,7 +26,7 @@ var wpActiveEditor, send_to_editor;
* are unavailable. This means that the HTML was not
* sent to the editor.
*/
send_to_editor = function( html ) {
window.send_to_editor = function( html ) {
var editor,
hasTinymce = typeof tinymce !== 'undefined',
hasQuicktags = typeof QTags !== 'undefined';
@ -37,7 +35,7 @@ send_to_editor = function( html ) {
if ( ! wpActiveEditor ) {
if ( hasTinymce && tinymce.activeEditor ) {
editor = tinymce.activeEditor;
wpActiveEditor = editor.id;
window.wpActiveEditor = editor.id;
} else if ( ! hasQuicktags ) {
return false;
}
@ -64,7 +62,6 @@ send_to_editor = function( html ) {
}
};
var tb_position;
(function($) {
/**
* Recalculates and applies the new ThickBox position based on the current
@ -77,7 +74,7 @@ var tb_position;
* @returns {Object[]} Array containing jQuery objects for all the found
* ThickBox anchors.
*/
tb_position = function() {
window.tb_position = function() {
var tbWindow = $('#TB_window'),
width = $(window).width(),
H = $(window).height(),

View File

@ -10,12 +10,10 @@
* @requires jQuery
*/
/* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings, showNotice */
var findPosts;
/* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings, showNotice, findPosts */
( function( $ ){
findPosts = {
window.findPosts = {
/**
* Opens a dialog to attach media to a post.
*

View File

@ -4,8 +4,8 @@
* @output wp-admin/js/plugin-install.js
*/
/* global plugininstallL10n, tb_click, tb_remove */
var tb_position;
/* global plugininstallL10n, tb_click, tb_remove, tb_position */
jQuery( document ).ready( function( $ ) {
var tbWindow,
@ -18,7 +18,7 @@ jQuery( document ).ready( function( $ ) {
$wrap = $ ( '.wrap' ),
$body = $( document.body );
tb_position = function() {
window.tb_position = function() {
var width = $( window ).width(),
H = $( window ).height() - ( ( 792 < width ) ? 60 : 20 ),
W = ( 792 < width ) ? 772 : width - 20;

View File

@ -5,11 +5,11 @@
*/
/* global postL10n, ajaxurl, wpAjax, setPostThumbnailL10n, postboxes, pagenow, tinymce, alert, deleteUserSetting */
/* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply */
/* global theList:true, theExtraList:true, getUserSetting, setUserSetting, commentReply, commentsBox */
/* global WPSetThumbnailHTML, wptitlehint */
var commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint, makeSlugeditClickable, editPermalink;
// Backwards compatibility: prevent fatal errors.
makeSlugeditClickable = editPermalink = function(){};
window.makeSlugeditClickable = window.editPermalink = function(){};
// Make sure the wp object exists.
window.wp = window.wp || {};
@ -24,7 +24,7 @@ window.wp = window.wp || {};
*
* @namespace commentsBox
*/
commentsBox = {
window.commentsBox = {
// Comment offset to use when fetching new comments.
st : 0,
@ -108,7 +108,7 @@ window.wp = window.wp || {};
*
* @global
*/
WPSetThumbnailHTML = function(html){
window.WPSetThumbnailHTML = function(html){
$('.inside', '#postimagediv').html(html);
};
@ -119,7 +119,7 @@ window.wp = window.wp || {};
*
* @global
*/
WPSetThumbnailID = function(id){
window.WPSetThumbnailID = function(id){
var field = $('input[value="_thumbnail_id"]', '#list-table');
if ( field.length > 0 ) {
$('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(id);
@ -133,7 +133,7 @@ window.wp = window.wp || {};
*
* @global
*/
WPRemoveThumbnail = function(nonce){
window.WPRemoveThumbnail = function(nonce){
$.post(ajaxurl, {
action: 'set-post-thumbnail', post_id: $( '#post_ID' ).val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent( document.cookie )
},
@ -1048,7 +1048,7 @@ jQuery(document).ready( function($) {
*
* @returns void
*/
wptitlehint = function(id) {
window.wptitlehint = function(id) {
id = id || 'title';
var title = $('#' + id), titleprompt = $('#' + id + '-prompt-text');

View File

@ -7,24 +7,22 @@
* @output wp-admin/js/postbox.js
*/
/* global ajaxurl, postBoxL10n */
/**
* This object contains all function to handle the behaviour of the post boxes. The post boxes are the boxes you see
* around the content on the edit page.
*
* @since 2.7.0
*
* @namespace postboxes
*
* @type {Object}
*/
var postboxes;
/* global ajaxurl, postBoxL10n, postboxes */
(function($) {
var $document = $( document );
postboxes = {
/**
* This object contains all function to handle the behaviour of the post boxes. The post boxes are the boxes you see
* around the content on the edit page.
*
* @since 2.7.0
*
* @namespace postboxes
*
* @type {Object}
*/
window.postboxes = {
/**
* Handles a click on either the postbox heading or the postbox open/close icon.

View File

@ -5,7 +5,7 @@
/* global setPostThumbnailL10n, ajaxurl, post_id, alert */
/* exported WPSetAsThumbnail */
function WPSetAsThumbnail( id, nonce ) {
window.WPSetAsThumbnail = function( id, nonce ) {
var $link = jQuery('a#wp-post-thumbnail-' + id);
$link.text( setPostThumbnailL10n.saving );
@ -25,4 +25,4 @@ function WPSetAsThumbnail( id, nonce ) {
}
}
);
}
};

View File

@ -3,9 +3,7 @@
*/
/* jshint curly: false, eqeqeq: false */
/* global ajaxurl */
var tagBox, array_unique_noempty;
/* global ajaxurl, tagBox, array_unique_noempty */
( function( $ ) {
var tagDelimiter = ( window.tagsSuggestL10n && window.tagsSuggestL10n.tagDelimiter ) || ',';
@ -24,7 +22,7 @@ var tagBox, array_unique_noempty;
*
* @return {Array} A new array containing only the unique items.
*/
array_unique_noempty = function( array ) {
window.array_unique_noempty = function( array ) {
var out = [];
// Trim the values and ensure they are unique.
@ -49,7 +47,7 @@ var tagBox, array_unique_noempty;
*
* @global
*/
tagBox = {
window.tagBox = {
/**
* Cleans up tags by removing redundant characters.
*

View File

@ -2,12 +2,12 @@
* @output wp-admin/js/widgets.js
*/
/* global ajaxurl, isRtl */
var wpWidgets;
/* global ajaxurl, isRtl, wpWidgets */
(function($) {
var $document = $( document );
wpWidgets = {
window.wpWidgets = {
/**
* A closed Sidebar that gets a Widget dragged over it.
*

View File

@ -2,7 +2,9 @@
* @output wp-includes/js/wp-ajax-response.js
*/
var wpAjax = jQuery.extend( {
/* global wpAjax */
window.wpAjax = jQuery.extend( {
unserialize: function( s ) {
var r = {}, q, pp, i, p;
if ( !s ) { return r; }

View File

@ -8,8 +8,7 @@
*
* @type {Object}
*/
var addComment;
addComment = ( function( window ) {
window.addComment = ( function( window ) {
// Avoid scope lookups on commonly used variables.
var document = window.document;

View File

@ -4,10 +4,10 @@
* @output wp-includes/js/utils.js
*/
/* global userSettings */
/* global userSettings, getAllUserSettings, wpCookies, setUserSetting */
/* exported getUserSetting, setUserSetting, deleteUserSetting */
var wpCookies = {
window.wpCookies = {
// The following functions are from Cookie.js class in TinyMCE 3, Moxiecode, used under LGPL.
each: function( obj, cb, scope ) {
@ -139,7 +139,7 @@ var wpCookies = {
};
// Returns the value as string. Second arg or empty string is returned when value is not set.
function getUserSetting( name, def ) {
window.getUserSetting = function( name, def ) {
var settings = getAllUserSettings();
if ( settings.hasOwnProperty( name ) ) {
@ -151,12 +151,12 @@ function getUserSetting( name, def ) {
}
return '';
}
};
// Both name and value must be only ASCII letters, numbers or underscore
// and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
// The value is converted and stored as string.
function setUserSetting( name, value, _del ) {
window.setUserSetting = function( name, value, _del ) {
if ( 'object' !== typeof userSettings ) {
return false;
}
@ -186,17 +186,17 @@ function setUserSetting( name, value, _del ) {
wpCookies.set( 'wp-settings-time-' + uid, userSettings.time, 31536000, path, '', secure );
return name;
}
};
function deleteUserSetting( name ) {
window.deleteUserSetting = function( name ) {
return setUserSetting( name, '', 1 );
}
};
// Returns all settings as js object.
function getAllUserSettings() {
window.getAllUserSettings = function() {
if ( 'object' !== typeof userSettings ) {
return {};
}
return wpCookies.getHash( 'wp-settings-' + userSettings.uid ) || {};
}
};

View File

@ -2,7 +2,7 @@
* @output wp-admin/js/gallery.js
*/
/* global unescape, getUserSetting, setUserSetting */
/* global unescape, getUserSetting, setUserSetting, wpgallery, tinymce */
jQuery(document).ready(function($) {
var gallerySortable, gallerySortableInit, sortIt, clearAll, w, desc = false;
@ -88,12 +88,12 @@ jQuery(document).ready(function($) {
}
});
jQuery(window).unload( function () { tinymce = tinyMCE = wpgallery = null; } ); // Cleanup
jQuery(window).unload( function () { window.tinymce = window.tinyMCE = window.wpgallery = null; } ); // Cleanup
/* gallery settings */
var tinymce = null, tinyMCE, wpgallery;
window.tinymce = null;
wpgallery = {
window.wpgallery = {
mcemode : false,
editor : {},
dom : {},
@ -123,8 +123,8 @@ wpgallery = {
}
// Find window & API
tinymce = w.tinymce;
tinyMCE = w.tinyMCE;
window.tinymce = w.tinymce;
window.tinyMCE = w.tinyMCE;
t.editor = tinymce.EditorManager.activeEditor;
t.setup();

View File

@ -2,7 +2,7 @@
* @output wp-includes/js/wplink.js
*/
var wpLink;
/* global wpLink */
( function( $, wpLinkL10n, wp ) {
var editor, searchTimer, River, Query, correctedURL, linkNode,
@ -16,7 +16,7 @@ var wpLink;
return linkNode || editor.dom.getParent( editor.selection.getNode(), 'a[href]' );
}
wpLink = {
window.wpLink = {
timeToTriggerRiver: 150,
minRiverAJAXDuration: 200,
riverBottomThreshold: 5,

View File

@ -9,20 +9,18 @@
* @output wp-admin/js/nav-menu.js
*/
/* global menus, postboxes, columns, isRtl, navMenuL10n, ajaxurl */
/**
* Contains all the functions to handle WordPress navigation menus administration.
*
* @namespace
*/
var wpNavMenu;
/* global menus, postboxes, columns, isRtl, navMenuL10n, ajaxurl, wpNavMenu */
(function($) {
var api;
api = wpNavMenu = {
/**
* Contains all the functions to handle WordPress navigation menus administration.
*
* @namespace wpNavMenu
*/
api = window.wpNavMenu = {
options : {
menuItemDepthPerLevel : 30, // Do not use directly. Use depthToPx and pxToDepth instead.

View File

@ -24,10 +24,9 @@
// by Alex King
// http://www.alexking.org/
/* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, prompt */
/* global adminpage, wpActiveEditor, quicktagsL10n, wpLink, prompt, edButtons */
var QTags, edCanvas,
edButtons = [];
window.edButtons = [];
/* jshint ignore:start */
@ -36,24 +35,24 @@ var QTags, edCanvas,
*
* Define all former global functions so plugins that hack quicktags.js directly don't cause fatal errors.
*/
var edAddTag = function(){},
edCheckOpenTags = function(){},
edCloseAllTags = function(){},
edInsertImage = function(){},
edInsertLink = function(){},
edInsertTag = function(){},
edLink = function(){},
edQuickLink = function(){},
edRemoveTag = function(){},
edShowButton = function(){},
edShowLinks = function(){},
edSpell = function(){},
edToolbar = function(){};
window.edAddTag = function(){},
window.edCheckOpenTags = function(){},
window.edCloseAllTags = function(){},
window.edInsertImage = function(){},
window.edInsertLink = function(){},
window.edInsertTag = function(){},
window.edLink = function(){},
window.edQuickLink = function(){},
window.edRemoveTag = function(){},
window.edShowButton = function(){},
window.edShowLinks = function(){},
window.edSpell = function(){},
window.edToolbar = function(){};
/**
* Initialize new instance of the Quicktags editor
*/
function quicktags(settings) {
window.quicktags = function(settings) {
return new QTags(settings);
}
@ -63,7 +62,7 @@ function quicktags(settings) {
* Added for back compatibility
* @see QTags.insertContent()
*/
function edInsertContent(bah, txt) {
window.edInsertContent = function(bah, txt) {
return QTags.insertContent(txt);
}
@ -73,7 +72,7 @@ function edInsertContent(bah, txt) {
* Added for back compatibility, use QTags.addButton() as it gives more flexibility like type of button, button placement, etc.
* @see QTags.addButton()
*/
function edButton(id, display, tagStart, tagEnd, access) {
window.edButton = function(id, display, tagStart, tagEnd, access) {
return QTags.addButton( id, display, tagStart, tagEnd, access, '', -1 );
}
@ -153,10 +152,9 @@ function edButton(id, display, tagStart, tagEnd, access) {
zeroise( now.getUTCMinutes() ) + ':' +
zeroise( now.getUTCSeconds() ) +
'+00:00';
})(),
qt;
})();
qt = QTags = function(settings) {
var qt = window.QTags = function(settings) {
if ( typeof(settings) === 'string' ) {
settings = {id: settings};
} else if ( typeof(settings) !== 'object' ) {
@ -180,7 +178,7 @@ function edButton(id, display, tagStart, tagEnd, access) {
if ( id === 'content' && typeof(adminpage) === 'string' && ( adminpage === 'post-new-php' || adminpage === 'post-php' ) ) {
// back compat hack :-(
edCanvas = canvas;
window.edCanvas = canvas;
toolbar_id = 'ed_toolbar';
} else {
toolbar_id = name + '_toolbar';

View File

@ -2,8 +2,8 @@
* @output wp-admin/js/dashboard.js
*/
/* global pagenow, ajaxurl, postboxes, wpActiveEditor:true */
var ajaxWidgets, ajaxPopulateWidgets, quickPressLoad;
/* global pagenow, ajaxurl, postboxes, wpActiveEditor:true, ajaxWidgets */
/* global ajaxPopulateWidgets, quickPressLoad, */
window.wp = window.wp || {};
/**
@ -61,7 +61,7 @@ jQuery(document).ready( function($) {
*
* @global
*/
ajaxWidgets = ['dashboard_primary'];
window.ajaxWidgets = ['dashboard_primary'];
/**
* Triggers widget updates via AJAX.
@ -74,7 +74,7 @@ jQuery(document).ready( function($) {
*
* @returns {void}
*/
ajaxPopulateWidgets = function(el) {
window.ajaxPopulateWidgets = function(el) {
/**
* Fetch the latest representation of the widget via Ajax and show it.
*
@ -129,7 +129,7 @@ jQuery(document).ready( function($) {
*
* @returns {void}
*/
quickPressLoad = function() {
window.quickPressLoad = function() {
var act = $('#quickpost-action'), t;
// Enable the submit buttons.
@ -210,7 +210,7 @@ jQuery(document).ready( function($) {
autoResizeTextarea();
};
quickPressLoad();
window.quickPressLoad();
// Enable the dragging functionality of the widgets.
$( '.meta-box-sortables' ).sortable( 'option', 'containment', '#wpwrap' );

View File

@ -5,7 +5,6 @@
/* global zxcvbn */
window.wp = window.wp || {};
var passwordStrength;
(function($){
/**
@ -118,5 +117,5 @@ var passwordStrength;
*
* @type {wp.passwordStrength.meter}
*/
passwordStrength = wp.passwordStrength.meter;
window.passwordStrength = wp.passwordStrength.meter;
})(jQuery);

View File

@ -2,7 +2,7 @@
* @output wp-admin/js/theme.js
*/
/* global _wpThemeSettings, confirm */
/* global _wpThemeSettings, confirm, tb_position */
window.wp = window.wp || {};
( function($) {
@ -2041,9 +2041,8 @@ $( document ).ready(function() {
})( jQuery );
// Align theme browser thickbox
var tb_position;
jQuery(document).ready( function($) {
tb_position = function() {
window.tb_position = function() {
var tbWindow = $('#TB_window'),
width = $(window).width(),
H = $(window).height(),