WordPress.com classic stats

Redirects the new stats page to the classic stats page

Stan na 23-06-2017. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name        WordPress.com classic stats
// @namespace   tpenguinltg
// @description Redirects the new stats page to the classic stats page
// @include     https://wordpress.com/stats*
// @version     2.2.1
// @homepageURL https://greasyfork.org/en/scripts/8621-wordpress-com-classic-stats
// @homepageURL https://github.com/tpenguinltg/wpcom-stats-redirect.user.js
// @grant       none
// @license     MPLv2.0; http://mozilla.org/MPL/2.0/
// @copyright   2015-2016, tPenguinLTG (http://tpenguinltg.wordpress.com/)
// @run-at      document-start
// ==/UserScript==

// Function by dystroy. From http://stackoverflow.com/a/14388512
function fetchJSONFile(path, callback, fallback) {
  var httpRequest = new XMLHttpRequest();
  httpRequest.onreadystatechange = function() {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        if (callback) callback(JSON.parse(httpRequest.responseText));
      } else if (fallback) {
        fallback();
      }
    }
  };
  httpRequest.open('GET', path);
  httpRequest.send();
}

function redirectToClassicStats(baseUrl, statsType) {
  var query = ["page=stats"];

  switch (statsType) {
    case "day":
      query.push("unit=1");
      break;
    case "week":
      query.push("unit=7");
      break;
    case "month":
      query.push("unit=31");
      break;
  }

  window.location.replace(baseUrl + '/wp-admin/index.php?' + query.join("&"));
}

function doRedirect(uri) {
  var parsedUri = uri.match(/stats(?:\/(insights|day|week|month|year))?(?:\/(countryviews|posts))?(?:\/([^\/]*))?/);
  var statsType = parsedUri[1];
  var viewType = parsedUri[2];
  var blogDomain = parsedUri[3];

  if (blogDomain) {
    // Redirect to post URL based on API results
    // API docs: https://developer.wordpress.com/docs/api/
    fetchJSONFile("https://public-api.wordpress.com/rest/v1.1/sites/" + blogDomain,
      // attempt to redirect using API
      function(data) {
        redirectToClassicStats(data.URL, statsType);
      },

      // fallback: attempt to use the blog domain
      function() {
        // use http instead of https in case the server doesn't support https
        // (e.g. for Jetpack sites)
        redirectToClassicStats('http://' + blogDomain, statsType);
      }
    );
  } else if (statsType != "insights") {
    window.onload = function() {
      // construct a stats URI from the user's default blog
      var defaultBlogStatsUri = "/stats";
      if (statsType) defaultBlogStatsUri += "/" + statsType;
      defaultBlogStatsUri += "/" + currentUser.primarySiteSlug;
      doRedirect(defaultBlogStatsUri);
    };
  } else {
    window.onload = function() {
      // construct an insights URI from the user's default blog
      doRedirect("/stats/insights/" + currentUser.primarySiteSlug);
    };
  }
}

// redirect unless new stats is explicitly requested
if (window.location.search.search(/from=wp-admin/) === -1) {
  doRedirect(window.location.pathname);
}