WordPress.com edit post redirects

Redirects the new post page to the classic post page

  1. // ==UserScript==
  2. // @name WordPress.com edit post redirects
  3. // @namespace tpenguinltg
  4. // @description Redirects the new post page to the classic post page
  5. // @include https://wordpress.com/post*
  6. // @include https://wordpress.com/page*
  7. // @include https://wordpress.com/block-editor*
  8. // @include https://*.wordpress.com/wp-admin/post.php*
  9. // @include https://*.wordpress.com/wp-admin/post-new.php*
  10. // @version 1.6.0
  11. // @homepageURL https://greasyfork.org/en/scripts/8581-wordpress-com-edit-post-redirects
  12. // @homepageURL https://github.com/tpenguinltg/wpcom-edit-post-redirect.user.js
  13. // @grant none
  14. // @license MPLv2.0; http://mozilla.org/MPL/2.0/
  15. // @copyright 2015, tPenguinLTG (http://tpenguinltg.wordpress.com/)
  16. // @run-at document-start
  17. // ==/UserScript==
  18.  
  19. // if already at the classic editor, don't do anything
  20. if (/classic-editor/.test(window.location.search)) return;
  21.  
  22. // if already at old dashboard URL, redirect to classic editor
  23. if (/wp-admin/.test(window.location.pathname)) {
  24. window.location.replace(window.location.href + (window.location.search ? "&" : "?") + "classic-editor");
  25. return;
  26. }
  27.  
  28.  
  29. // gather information from URL
  30. var parsedUrl = window.location.pathname.match(/(post|page)(?:\/([^\/]+)(?:\/(\d+|new)?)?)?/);
  31. var postType = parsedUrl[1];
  32. var blogid = parsedUrl[2];
  33. var postid = parsedUrl[3];
  34.  
  35. // initiate redirect
  36. redirectToClassic();
  37.  
  38.  
  39. /**
  40. * Initiates the redirect.
  41. */
  42. function redirectToClassic() {
  43. // if no blog specified
  44. if (!blogid) {
  45. guessClassicLink();
  46. } else {
  47. // Redirect to post URL based on API results
  48. // API docs: https://developer.wordpress.com/docs/api/
  49. fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/" + blogid, apiRedirect, guessClassicLink);
  50. }
  51. }
  52.  
  53. /**
  54. * Handles the API request via AJAX.
  55. * @param path the URL to request. The response should be a JSON object.
  56. * @param callback the function to call on success.
  57. * This function should take in a single JSON object.
  58. * @param fallback the function to call on failure
  59. */
  60. // Based on function by dystroy. From http://stackoverflow.com/a/14388512
  61. function fetchJSONFile(path, callback, fallback) {
  62. var httpRequest = new XMLHttpRequest();
  63. httpRequest.onreadystatechange = function() {
  64. if (httpRequest.readyState === 4) {
  65. if (httpRequest.status === 200) {
  66. var data = JSON.parse(httpRequest.responseText);
  67. if (callback) callback(data);
  68. } else {
  69. if (fallback) fallback();
  70. }
  71. }
  72. };
  73. httpRequest.open('GET', path);
  74. httpRequest.send();
  75. }
  76.  
  77. /**
  78. * Builds the classic editor link using the specified base.
  79. * The base should not have a trailing slash.
  80. *
  81. * @return the URL of the classic editor for the current post ID
  82. */
  83. function buildClassicLink(base) {
  84. var posturl = base;
  85. if (postid == "new" || postid == null) { // new post
  86. posturl += "/wp-admin/post-new.php?post_type=" + postType + "&classic-editor"
  87. } else { // existing post
  88. posturl += "/wp-admin/post.php?post=" + postid + "&action=edit&classic-editor";
  89. }
  90.  
  91. return posturl;
  92. }
  93.  
  94. /**
  95. * Guesses the link to the classic editor based on the blog ID, which
  96. * should be the domain if it is not numeric.
  97. */
  98. function guessClassicLink() {
  99. // if the blog ID is not numeric, then it is a domain
  100. if (blogid && isNaN(blogid)) {
  101. // use the blogid to build URL
  102. // and redirect
  103. window.location.replace(buildClassicLink("https://" + blogid));
  104. } else {
  105. scrapeSiteLink();
  106. }
  107. }
  108.  
  109. /**
  110. * Scrapes the page for the site link
  111. */
  112. function scrapeSiteLink() {
  113. window.onload = function() {
  114. // FIXME this will not work for Jetpack-enabled sites that have
  115. // different WordPress and site roots
  116. // e.g. site is accessed at http://example.com/,
  117. // but admin at http://example.com/wordpress/wp-admin/
  118. blogurl = document.querySelector(".site__content[href]").href;
  119.  
  120. // strip trailing slash
  121. if (blogurl.charAt(blogurl.length-1) === '/') {
  122. blogurl = blogurl.substr(0, blogurl.length - 1);
  123. }
  124.  
  125. // in case nothing is returned, don't do anything
  126. if (blogurl) {
  127. window.location.replace(buildClassicLink(blogurl));
  128. }
  129. };
  130. }
  131.  
  132. /**
  133. * Sets up a redirect using the given parsed API data.
  134. * @param data the parsed API results as a JSON object
  135. */
  136. function apiRedirect(data) {
  137. if (data.error) { // private blog
  138. guessClassicLink();
  139. } else {
  140. window.location.replace(buildClassicLink(data.URL));
  141. }
  142. }