Skip to content

Instantly share code, notes, and snippets.

  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
WordPress Admin Switcher Bookmark Source
var WordPressAdminSwitcherBookmark = {};
/**
* Switch to/from the WordPress Admin.
*/
WordPressAdminSwitcherBookmark.toggleAdmin = function() {
if ( WordPressAdminSwitcherBookmark.isWordPressAdmin( window.location.pathname ) ) {
WordPressAdminSwitcherBookmark.switchToFrontEnd();
} else {
WordPressAdminSwitcherBookmark.switchToAdmin();
}
};
/**
* Is this the WordPress admin?
*
* @param {string} url The URL to check.
* @return {boolean} Whether currently in the WordPress admin.
*/
WordPressAdminSwitcherBookmark.isWordPressAdmin = function( url ) {
return ( url.indexOf( '/wp-admin/' ) > -1 ) || ( url.indexOf( '/wp-login.php' ) > -1 );
};
/**
* Switch to the front end.
*/
WordPressAdminSwitcherBookmark.switchToFrontEnd = function() {
WordPressAdminSwitcherBookmark.changeWindowLocation( WordPressAdminSwitcherBookmark.getFrontEndUrl() );
};
/**
* Switch to the WP admin.
*/
WordPressAdminSwitcherBookmark.switchToAdmin = function() {
WordPressAdminSwitcherBookmark.changeWindowLocation( WordPressAdminSwitcherBookmark.getAdminUrl() );
};
/**
* Get frontend URL.
*
* @return {string} The frontend URL.
*/
WordPressAdminSwitcherBookmark.getFrontEndUrl = function() {
var url = WordPressAdminSwitcherBookmark.getFrontEndUrlFromAdminBar();
if ( url ) {
return url;
}
url = WordPressAdminSwitcherBookmark.getFrontEndUrlFromWindowLocation();
if ( url ) {
return url;
}
// Return WordPressAdminSwitcherBookmark.as a last resort. Has the potential to
// be incorrect on subdirectory multisite installs.
return window.location.origin;
};
/**
* Get front end URL from admin bar.
*
* Try to get the link to view/preview the post first,
* then fallback to getting the main front end url.
*
* @return {string|boolean} The url or false on failure.
*/
WordPressAdminSwitcherBookmark.getFrontEndUrlFromAdminBar = function() {
var adminBarIds = {
view: '#wp-admin-bar-view',
preview: '#wp-admin-bar-preview',
siteName: '#wp-admin-bar-site-name'
};
return WordPressAdminSwitcherBookmark.getUrlFromAdminBar( adminBarIds );
};
/**
* Get front end URL from window.location.
*
* @return {string|boolean} The url or false on failure.
*/
WordPressAdminSwitcherBookmark.getFrontEndUrlFromWindowLocation = function() {
var adminUrlParts = {
wpAdmin: '/wp-admin/',
wpLogin: '/wp-login.php'
};
for ( var index in adminUrlParts ) {
var adminUrlPartPosition = WordPressAdminSwitcherBookmark.getPositionOfStringInCurrentUrl( adminUrlParts[ index ] );
if ( WordPressAdminSwitcherBookmark.doesUrlContainString( adminUrlPartPosition ) ) {
return WordPressAdminSwitcherBookmark.getPartOfUrlBeforePosition( adminUrlPartPosition );
}
}
return false;
};
/**
* Get the position of a string in the current URL.
*
* @param {string} string The string to find within the URL.
* @return {number} The position of string within the URL.
*/
WordPressAdminSwitcherBookmark.getPositionOfStringInCurrentUrl = function( string ) {
return window.location.href.indexOf( string );
};
/**
* Does the URL contain a string?
*
* @param {number} stringPosition The position of string within the URL.
* @return {boolean} Whether the URL contains the string.
*/
WordPressAdminSwitcherBookmark.doesUrlContainString = function( stringPosition ) {
return WordPressAdminSwitcherBookmark.doesStringContainSubstring( stringPosition );
};
/**
* Does string contain a substring?
*
* @param {number} stringPosition The position of substring within the string.
* @return {boolean} Whether the substring was found within the string.
*/
WordPressAdminSwitcherBookmark.doesStringContainSubstring = function( stringPosition ) {
return stringPosition > -1;
};
/**
* Get part of URL before the provided position.
*
* @param {number} stringPosition Exclude the character at WordPressAdminSwitcherBookmark.position and following.
* @return {string} The part of the URL before stringPosition.
*/
WordPressAdminSwitcherBookmark.getPartOfUrlBeforePosition = function( stringPosition ) {
return window.location.href.substring( 0, stringPosition );
};
/**
* Get the admin URL.
*
* @return {string} The admin URL.
*/
WordPressAdminSwitcherBookmark.getAdminUrl = function() {
var url = WordPressAdminSwitcherBookmark.getAdminUrlFromAdminBar();
if ( url ) {
return url;
}
url = WordPressAdminSwitcherBookmark.getAdminUrlFromPageSource();
if ( url ) {
return url;
}
// Return WordPressAdminSwitcherBookmark.as a last resort. Has the potential to
// be incorrect on subdirectory multisite installs.
return WordPressAdminSwitcherBookmark.trailingSlashIt( window.location.origin ) + 'wp-admin/';
};
/**
* Get admin URL from page source.
*
* @return {string|boolean} The admin URL or false on failure.
*/
WordPressAdminSwitcherBookmark.getAdminUrlFromPageSource = function() {
var url = WordPressAdminSwitcherBookmark.inferUrlFromPageLinks(),
postId = WordPressAdminSwitcherBookmark.inferPostIdFromPageSource();
if ( url ) {
var adminUrl = WordPressAdminSwitcherBookmark.getAdminUrlFromSiteUrl( url );
if ( postId ) {
adminUrl = WordPressAdminSwitcherBookmark.getPostSpecificAdminUrl( adminUrl, postId );
}
return adminUrl;
}
return false;
};
/**
* Turn site URL into admin URL.
*
* @param {string} url The site URL.
* @return {string} The admin URL.
*/
WordPressAdminSwitcherBookmark.getAdminUrlFromSiteUrl = function( url ) {
return WordPressAdminSwitcherBookmark.trailingSlashIt( url ) + 'wp-admin/';
};
/**
* Turn base admin URL into a post-specific admin URL.
*
* @param {string} adminUrl The base admin URL.
* @param {string} postId The post ID.
* @return {string} The post-specific admin URL.
*/
WordPressAdminSwitcherBookmark.getPostSpecificAdminUrl = function( adminUrl, postId ) {
return adminUrl + 'post.php?post=' + postId + '&action=edit';
};
/**
* Get WP admin URL from the admin bar.
*
* @return {string|boolean} The URL or false on failure.
*/
WordPressAdminSwitcherBookmark.getAdminUrlFromAdminBar = function() {
var adminBarIds = {
edit: '#wp-admin-bar-edit',
siteName: '#wp-admin-bar-site-name'
};
return WordPressAdminSwitcherBookmark.getUrlFromAdminBar( adminBarIds );
};
/**
* Get URL from the WP admin bar.
*
* @param {Object} adminBarIds The element IDs to use as selectors.
* @return {string|boolean} The URL or false on failure.
*/
WordPressAdminSwitcherBookmark.getUrlFromAdminBar = function ( adminBarIds ) {
if ( WordPressAdminSwitcherBookmark.isEmptyObject( adminBarIds ) ) {
return false;
}
for ( var key in adminBarIds ) {
var adminBarAnchorHtmlElement = WordPressAdminSwitcherBookmark.getAdminBarAnchorHtmlElement( adminBarIds[ key ] );
if ( WordPressAdminSwitcherBookmark.wereHtmlElementsFound( adminBarAnchorHtmlElement ) ) {
return WordPressAdminSwitcherBookmark.getHrefUrlFromHtmlElement( adminBarAnchorHtmlElement );
}
}
return false;
};
/**
* Is WordPressAdminSwitcherBookmark.an empty object?
*
* @param {Object} object The object to check.
* @return {boolean} Whether the object is empty.
*/
WordPressAdminSwitcherBookmark.isEmptyObject = function( object ) {
return ( Object != object.constructor ) || ( Object.keys( object ).length < 1 );
};
/**
* Get admin bar anchor HTML element.
*
* @param {string} adminBarId The id of the HTML element to get.
* @return {Object} The matching HTML element NodeList.
*/
WordPressAdminSwitcherBookmark.getAdminBarAnchorHtmlElement = function( adminBarId ) {
return document.querySelectorAll( adminBarId + ' > a.ab-item[href]' );
};
/**
* Here any HTML elements found?
*
* @param {Object} nodelist The NodeList of HTML elements found.
* @return {boolean} Whether HTML elements were found.
*/
WordPressAdminSwitcherBookmark.wereHtmlElementsFound = function( nodelist ) {
return nodelist.length > 0;
};
/**
* Get the URL from an HTML element's href property.
*
* @param {Object} element The HTML element NodeList.
* @return {string} The URL.
*/
WordPressAdminSwitcherBookmark.getHrefUrlFromHtmlElement = function( element ) {
return element[0].getAttribute( 'href' );
};
/**
* Infer the site's URL from links in the page source.
*
* We can't simply get the site's URL from window.location
* because subdomain multisite installs need to have the
* /site-name/ part of the pathname preserved.
*
* @return {string|boolean} The URL or false on failure.
*/
WordPressAdminSwitcherBookmark.inferUrlFromPageLinks = function() {
var selectors = {
stylesheets: 'link[rel="stylesheet"][href]',
scripts: 'script[src]',
rss: 'link[type="application/rss+xml"][href]',
xmlrpc: 'link[rel="pingback"][href]'
};
for ( var selectorIndex in selectors ) {
var elements = document.querySelectorAll( selectors[ selectorIndex ] );
if ( WordPressAdminSwitcherBookmark.wereHtmlElementsFound( elements ) ) {
for ( var elementIndex in elements ) {
var attribute = WordPressAdminSwitcherBookmark.getAttributeForSelector( selectorIndex ),
url = WordPressAdminSwitcherBookmark.inferUrlFromPageLink( elements[ elementIndex ], attribute );
if ( url ) {
return url;
}
}
}
}
return false;
};
/**
* Extract the URL from an HTML element.
*
* @param {Object} element The HTML element
* @param {string} attribute The attribute to get the URL from: 'href' or 'src'.
* @return {string|boolean} The URL or false on failure.
*/
WordPressAdminSwitcherBookmark.inferUrlFromPageLink = function( element, attribute ) {
var paths = {
wpContent: '/wp-content/',
wpIncludes: '/wp-includes/'
};
for ( var index in paths ) {
var url = WordPressAdminSwitcherBookmark.getUrlSubstringFromHtmlElement( element, attribute, paths[ index ] );
if ( url ) {
return url;
}
}
return false;
};
/**
* Get the URL-containing attribute for a selector type.
*
* @param {string} selector The selector type.
* @return {string} The attribute for contains the URL.
*/
WordPressAdminSwitcherBookmark.getAttributeForSelector = function( selector ) {
switch ( selector ) {
case 'scripts':
return 'src';
default:
return 'href';
}
};
/**
* Extract part of a URL from an HTML element.
*
* @param {Object} element The HTML element.
* @param {string} attribute The attribute to get URL from ('href' or 'src').
* @param {string} string The text to search for and exclude, along with
* everything after it.
* @return {string|boolean} The part of the URL or false on failure.
*/
WordPressAdminSwitcherBookmark.getUrlSubstringFromHtmlElement = function( element, attribute, string ) {
var stringPosition = element[ attribute ].indexOf( string );
if ( WordPressAdminSwitcherBookmark.doesUrlContainString( stringPosition ) ) {
return element[ attribute ].substring( 0, stringPosition );
}
return false;
};
/**
* Infer post ID from the page source.
*
* Not all WordPress sites will have WordPressAdminSwitcherBookmark.exposed.
*
* @return {string|boolean} The post ID or false on failure.
*/
WordPressAdminSwitcherBookmark.inferPostIdFromPageSource = function() {
var postId = false,
postIdSelectors = {
commentsForm: '#comment_post_ID[value]',
shortlink: 'link[rel="shortlink"][href]',
body: 'body[class]'
};
for ( var index in postIdSelectors ) {
var postIdElement = document.querySelectorAll( postIdSelectors[ index ] );
if ( ! WordPressAdminSwitcherBookmark.wereHtmlElementsFound( postIdElement ) ) {
continue;
}
switch ( index ) {
case 'commentsForm':
postId = WordPressAdminSwitcherBookmark.getPostIdFromCommentsForm( postIdElement );
break;
case 'shortlink':
postId = WordPressAdminSwitcherBookmark.getPostIdFromShortlink( postIdElement );
break;
case 'body':
postId = WordPressAdminSwitcherBookmark.getPostIdFromBodyClass( postIdElement );
}
}
return postId;
};
/**
* Get post ID from comments form.
*
* @param {Object} postIdElement The HTML element NodeList containing the post ID.
* @return {string} The post ID.
*/
WordPressAdminSwitcherBookmark.getPostIdFromCommentsForm = function( postIdElement ) {
return postIdElement[0].value;
};
/**
* Get post ID from shortlink.
*
* @param {Object} postIdElement The HTML element NodeList containing the post ID.
* @return {string|boolean} The post ID or false on failure.
*/
WordPressAdminSwitcherBookmark.getPostIdFromShortlink = function( postIdElement ) {
var shortlinkUrl = WordPressAdminSwitcherBookmark.getHrefUrlFromHtmlElement( postIdElement );
if ( ! WordPressAdminSwitcherBookmark.isShortlinkUsingWpMeService( shortlinkUrl ) ) {
var positionOfUrlTextBeforePostId = WordPressAdminSwitcherBookmark.getPositionOfUrlTextBeforePostId( shortlinkUrl );
if ( WordPressAdminSwitcherBookmark.doesShortlinkUrlContainPostId( positionOfUrlTextBeforePostId ) ) {
return WordPressAdminSwitcherBookmark.getPostIdFromShortlinkUrl( shortlinkUrl, positionOfUrlTextBeforePostId );
}
}
return false;
};
/**
* Is shortlink using wp.me service?
*
* @param {string} shortlinkUrl The shortlink URL.
* @return {boolean} Whether shorlinkUrl is using wp.me.
*/
WordPressAdminSwitcherBookmark.isShortlinkUsingWpMeService = function( shortlinkUrl ) {
return shortlinkUrl.indexOf( '//wp.me/' ) > -1;
};
/**
* Get the position of the URL before the post ID.
*
* @param {string} shortlinkUrl The URL to search.
* @return {number} The position of the post ID.
*/
WordPressAdminSwitcherBookmark.getPositionOfUrlTextBeforePostId = function( shortlinkUrl ) {
return shortlinkUrl.indexOf( '/?p=' );
};
/**
* Does the shortlink URL contain the post ID?
*
* @param {number} positionOfUrlTextBeforePostId The position of the URL text before the post ID.
* @return {boolean} Whether the URL contains the post ID.
*/
WordPressAdminSwitcherBookmark.doesShortlinkUrlContainPostId = function( positionOfUrlTextBeforePostId ) {
return WordPressAdminSwitcherBookmark.doesStringContainSubstring( positionOfUrlTextBeforePostId );
};
/**
* Extract the post ID from the shortlink URL.
*
* @param {string} shortlinkUrl The shortlink URL.
* @param {number} positionOfUrlTextBeforePostId The position of URL text before the post ID.
* @return {string} The post ID.
*/
WordPressAdminSwitcherBookmark.getPostIdFromShortlinkUrl = function( shortlinkUrl, positionOfUrlTextBeforePostId ) {
return shortlinkUrl.substring( positionOfUrlTextBeforePostId + 4, shortlinkUrl.length );
};
/**
* Get post ID from body class.
*
* @param {Object} postIdElement The HTML element NodeList containing the post ID.
* @return {string|boolean} The post ID or false on failure.
*/
WordPressAdminSwitcherBookmark.getPostIdFromBodyClass = function( postIdElement ) {
var bodyClasses = postIdElement[0].getAttribute( 'class' ),
postIdPosition = bodyClasses.indexOf( 'postid-' );
if ( WordPressAdminSwitcherBookmark.doesStringContainSubstring( postIdPosition ) ) {
bodyClasses = bodyClasses.substring( postIdPosition + 7, bodyClasses.length );
var firstSpacePosition = bodyClasses.indexOf( ' ' );
if ( WordPressAdminSwitcherBookmark.doesStringContainSubstring( firstSpacePosition ) ) {
return bodyClasses.substring( 0, firstSpacePosition );
}
return bodyClasses;
}
return false;
};
/**
* Add a trailing slash to a URL if it doesn't already have one.
*
* @param {string} url The URL.
* @return {string} url The URL with a trailing slash.
*/
WordPressAdminSwitcherBookmark.trailingSlashIt = function( url ) {
if ( ! url.endsWith( '/' ) ) {
url += '/';
}
return url;
};
/**
* Change the window location.
*
* @param {string} url The URL to set as the new location.
*/
WordPressAdminSwitcherBookmark.changeWindowLocation = function( url ) {
window.location = url;
};
WordPressAdminSwitcherBookmark.toggleAdmin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment