Ultimate Tag Cloud Widget - Version 2.0-beta2

Version Description

= 2.0 =

  • New plugin architecture and a big rewrite of the plugin foundation. Watch out for breaking changes, please see http://exz.nu/utcwbreaking for more information.
Download this release

Release Info

Developer exz
Plugin Icon wp plugin Ultimate Tag Cloud Widget
Version 2.0-beta2
Comparing to
See all releases

Code changes from version 2.0-beta1 to 2.0-beta2

Files changed (3) hide show
  1. js/utcw.min.js +1 -1
  2. utcw-data.php +44 -22
  3. utcw-widget.php +9 -0
js/utcw.min.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! Ultimate Tag Cloud Widget - v2.0 alpha - 2012-10-23
2
  * https://0x539.se/wordpress/ultimate-tag-cloud-widget/
3
  * Copyright (c) 2012 Rickard Andersson; Licensed GPLv2 */
4
  var Query=function(e){"use strict";var t=function(e){var t=[],n,r,i,s;if(typeof e=="undefined"||e===null||e==="")return t;e.indexOf("?")===0&&(e=e.substring(1)),r=e.toString().split(/[&;]/);for(n=0;n<r.length;n++)i=r[n],s=i.split("="),t.push([s[0],s[1]]);return t},n=t(e),r=function(){var e="",t,r;for(t=0;t<n.length;t++)r=n[t],e.length>0&&(e+="&"),e+=r.join("=");return e.length>0?"?"+e:e},i=function(e){return e=decodeURIComponent(e),e=e.replace("+"," "),e},s=function(e){var t,r;for(r=0;r<n.length;r++){t=n[r];if(i(e)===i(t[0]))return t[1]}},o=function(e){var t=[],r,s;for(r=0;r<n.length;r++)s=n[r],i(e)===i(s[0])&&t.push(s[1]);return t},u=function(e,t){var r=[],s,o,u,a;for(s=0;s<n.length;s++)o=n[s],u=i(o[0])===i(e),a=i(o[1])===i(t),(arguments.length===1&&!u||arguments.length===2&&!u&&!a)&&r.push(o);return n=r,this},a=function(e,t,r){return arguments.length===3&&r!==-1?(r=Math.min(r,n.length),n.splice(r,0,[e,t])):arguments.length>0&&n.push([e,t]),this},f=function(e,t,r){var s=-1,o,f;if(arguments.length===3){for(o=0;o<n.length;o++){f=n[o];if(i(f[0])===i(e)&&decodeURIComponent(f[1])===i(r)){s=o;break}}u(e,r).addParam(e,t,s)}else{for(o=0;o<n.length;o++){f=n[o];if(i(f[0])===i(e)){s=o;break}}u(e),a(e,t,s)}return this};return{getParamValue:s,getParamValues:o,deleteParam:u,addParam:a,replaceParam:f,toString:r}},Uri=function(e){"use strict";var t=!1,n=function(e){var n={strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},r=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],i={name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},s=n[t?"strict":"loose"].exec(e),o={},u=14;while(u--)o[r[u]]=s[u]||"";return o[i.name]={},o[r[12]].replace(i.parser,function(e,t,n){t&&(o[i.name][t]=n)}),o},r=n(e||""),i=new Query(r.query),s=function(e){return typeof e!="undefined"&&(r.protocol=e),r.protocol},o=null,u=function(e){return typeof e!="undefined"&&(o=e),o===null?r.source.indexOf("//")!==-1:o},a=function(e){return typeof e!="undefined"&&(r.userInfo=e),r.userInfo},f=function(e){return typeof e!="undefined"&&(r.host=e),r.host},l=function(e){return typeof e!="undefined"&&(r.port=e),r.port},c=function(e){return typeof e!="undefined"&&(r.path=e),r.path},h=function(e){return typeof e!="undefined"&&(i=new Query(e)),i},p=function(e){return typeof e!="undefined"&&(r.anchor=e),r.anchor},d=function(e){return s(e),this},v=function(e){return u(e),this},m=function(e){return a(e),this},g=function(e){return f(e),this},y=function(e){return l(e),this},b=function(e){return c(e),this},w=function(e){return h(e),this},E=function(e){return p(e),this},S=function(e){return h().getParamValue(e)},x=function(e){return h().getParamValues(e)},T=function(e,t){return arguments.length===2?h().deleteParam(e,t):h().deleteParam(e),this},N=function(e,t,n){return arguments.length===3?h().addParam(e,t,n):h().addParam(e,t),this},C=function(e,t,n){return arguments.length===3?h().replaceParam(e,t,n):h().replaceParam(e,t),this},k=function(){var e="",t=function(e){return e!==null&&e!==""};return t(s())?(e+=s(),s().indexOf(":")!==s().length-1&&(e+=":"),e+="//"):u()&&t(f())&&(e+="//"),t(a())&&t(f())&&(e+=a(),a().indexOf("@")!==a().length-1&&(e+="@")),t(f())&&(e+=f(),t(l())&&(e+=":"+l())),t(c())?e+=c():t(f())&&(t(h().toString())||t(p()))&&(e+="/"),t(h().toString())&&(h().toString().indexOf("?")!==0&&(e+="?"),e+=h().toString()),t(p())&&(p().indexOf("#")!==0&&(e+="#"),e+=p()),e},L=function(){return new Uri(k())};return{protocol:s,hasAuthorityPrefix:u,userInfo:a,host:f,port:l,path:c,query:h,anchor:p,setProtocol:d,setHasAuthorityPrefix:v,setUserInfo:m,setHost:g,setPort:y,setPath:b,setQuery:w,setAnchor:E,getQueryParamValue:S,getQueryParamValues:x,deleteQueryParam:T,addQueryParam:N,replaceQueryParam:C,toString:k,clone:L}},jsUri=Uri;(function(e){e.fn.wTooltip=function(t,n){function m(n){n&&(t.degrade?e(h).html(t.content.replace(/<\/?[^>]+>/gi,"")):e(h).html(t.content))}function g(n){function i(e){r&&!t.content&&(r="")}function u(){!l&&t.auto&&(clearInterval(o),t.fadeOut?e(h).fadeOut(t.fadeOut,function(){i(n)}):(i(n),h.style.display="none")),typeof t.callAfter=="function"&&t.callAfter(h,n,t),d&&(t=e.listen(t))}t.timeout>0?s=setTimeout(function(){u()},t.timeout):u()}t=e.extend({content:null,ajax:null,follow:!0,auto:!0,fadeIn:0,fadeOut:0,appendTip:document.body,degrade:!1,offsetY:10,offsetX:1,style:{},className:null,id:null,callBefore:function(e,t,n){},callAfter:function(e,t,n){},clickAction:function(t,n){e(t).hide()},delay:0,timeout:0},t||{}),!t.style&&typeof t.style!="object"?(t.style={},t.style.zIndex="1000"):t.style=e.extend({border:"1px solid gray",background:"#edeef0",color:"#000",padding:"10px",zIndex:"1000",textAlign:"left"},t.style||{}),typeof n=="function"&&(t.callAfter=n||t.callAfter),t.style.display="none",t.style.position="absolute";var r,i,s,o,u={},f=!0,l=!1,c=!1,h=document.createElement("div"),p=typeof document.body.style.maxWidth=="undefined"?!0:!1,d=typeof e.talk=="function"&&typeof e.listen=="function"?!0:!1;t.id&&(h.id=t.id),t.className&&(h.className=t.className),t.degrade=t.degrade&&p?!0:!1;for(var v in t.style)h.style[v]=t.style[v];return t.ajax&&e.get(t.ajax,function(e){e&&(t.content=e),m(t.content)}),e(h).hover(function(){l=!0},function(){l=!1,g(u)}),d&&(t.key=h,t.plugin="wTooltip",t.channel="wayfarer",e.talk(t)),m(t.content&&!t.ajax),e(h).appendTo(t.appendTip),this.each(function(){e(this).hover(function(){function o(){typeof t.callBefore=="function"&&t.callBefore(h,n,t),d&&(t=e.listen(t));var i;t.content?t.degrade||(i="block"):r&&!t.degrade?(e(h).html(unescape(r)),i="block",r=""):i="none",t.auto&&(i=="block"&&t.fadeIn?e(h).fadeIn(t.fadeIn):h.style.display=i)}var n=this;clearTimeout(s),(this.title||this.titleMemKeep)&&!t.degrade&&!t.content&&(r=this.title||this.titleMemKeep,this.title&&(this.titleMemKeep=this.title,this.title="")),t.content&&t.degrade&&(this.title=h.innerHTML),t.delay>0?i=setTimeout(function(){o()},t.delay):o()},function(){clearTimeout(i);var n=this;f=!0,!t.follow||c||t.offsetX<0&&0-t.offsetX<e(h).outerWidth()&&t.offsetY>0&&0-t.offsetY<e(h).outerHeight()?setTimeout(function(){o=setInterval(function(){g(n)},1)},1):g(this)}),e(this).mousemove(function(n){u=this;if(t.follow||f){var r=e(window).scrollTop(),i=e(window).scrollLeft(),s=n.clientY+r+t.offsetY,o=n.clientX+i+t.offsetX,l=e(t.appendTip).outerHeight(),p=e(t.appendTip).innerHeight(),d=e(window).width()+i-e(h).outerWidth(),v=e(window).height()+r-e(h).outerHeight();s=l>p?s-(l-p):s,c=s>v||o>d?!0:!1,o-i<=0&&t.offsetX<0?o=i:o>d&&(o=d),s-r<=0&&t.offsetY<0?s=r:s>v&&(s=v),h.style.top=s+"px",h.style.left=o+"px",f=!1}}),typeof t.clickAction=="function"&&e(this).click(function(){t.clickAction(h,this)})})}})(jQuery),function(e){"use strict";var t={border:"solid 1px #6295fb",background:"#fff",color:"#000",padding:"5px",zIndex:1e3},n={activeTab:{},init:function(){e("input[id$=-color_none], input[id$=-color_random], input[id$=-color_set], input[id$=-color_span]").live("click",this.colorClickHandler),e(".utcw-tab-button").live("click",this.tabClickHandler),e("select[id$=-taxonomy]").live("change",this.taxonomyChangeHandler),e(document).ready(this.initTooltip),e(document).ajaxSuccess(this.ajaxSuccessHandler)},initTooltip:function(){e(".utcw-help").wTooltip({style:t,className:"utcw-tooltip"})},tabClickHandler:function(){var t=e(this);return t.data("id")==="utcw-__i__"?!1:(t.parent().find(".utcw-tab-button").removeClass("utcw-active"),t.addClass("utcw-active"),t.parent().find("fieldset.utcw").addClass("hidden"),e("#"+t.data("tab")).removeClass("hidden"),n.activeTab[t.data("id")]=t.data("tab"),!1)},colorClickHandler:function(){var t=e("div[id$='set_chooser']"),n=e("div[id$='span_chooser']"),r=e(this).val();t.addClass("utcw-hidden"),n.addClass("utcw-hidden"),r==="set"?t.removeClass("utcw-hidden"):r==="span"&&n.removeClass("utcw-hidden")},taxonomyChangeHandler:function(){var t=e(this),n=t.find(":selected").val(),r=t.parents(".widget-content");r.find("div[id$=-terms]").addClass("hidden"),r.find("#"+n+"-terms").removeClass("hidden")},ajaxSuccessHandler:function(e,t,r){n.setCurrentTab.apply(n,[r.data]),n.initTooltip.apply(n)},setCurrentTab:function(t){var n=new Uri,r;n.setQuery(decodeURI(t)),n.getQueryParamValue("action")==="save-widget"&&n.getQueryParamValue("id_base")==="utcw"&&(r=n.getQueryParamValue("widget-id"),this.activeTab[r]&&e("button[data-tab='"+this.activeTab[r]+"']").trigger("click"))}};n.init()}(jQuery);
1
+ /*! Ultimate Tag Cloud Widget - v2.0 beta 2 - 2012-11-11
2
  * https://0x539.se/wordpress/ultimate-tag-cloud-widget/
3
  * Copyright (c) 2012 Rickard Andersson; Licensed GPLv2 */
4
  var Query=function(e){"use strict";var t=function(e){var t=[],n,r,i,s;if(typeof e=="undefined"||e===null||e==="")return t;e.indexOf("?")===0&&(e=e.substring(1)),r=e.toString().split(/[&;]/);for(n=0;n<r.length;n++)i=r[n],s=i.split("="),t.push([s[0],s[1]]);return t},n=t(e),r=function(){var e="",t,r;for(t=0;t<n.length;t++)r=n[t],e.length>0&&(e+="&"),e+=r.join("=");return e.length>0?"?"+e:e},i=function(e){return e=decodeURIComponent(e),e=e.replace("+"," "),e},s=function(e){var t,r;for(r=0;r<n.length;r++){t=n[r];if(i(e)===i(t[0]))return t[1]}},o=function(e){var t=[],r,s;for(r=0;r<n.length;r++)s=n[r],i(e)===i(s[0])&&t.push(s[1]);return t},u=function(e,t){var r=[],s,o,u,a;for(s=0;s<n.length;s++)o=n[s],u=i(o[0])===i(e),a=i(o[1])===i(t),(arguments.length===1&&!u||arguments.length===2&&!u&&!a)&&r.push(o);return n=r,this},a=function(e,t,r){return arguments.length===3&&r!==-1?(r=Math.min(r,n.length),n.splice(r,0,[e,t])):arguments.length>0&&n.push([e,t]),this},f=function(e,t,r){var s=-1,o,f;if(arguments.length===3){for(o=0;o<n.length;o++){f=n[o];if(i(f[0])===i(e)&&decodeURIComponent(f[1])===i(r)){s=o;break}}u(e,r).addParam(e,t,s)}else{for(o=0;o<n.length;o++){f=n[o];if(i(f[0])===i(e)){s=o;break}}u(e),a(e,t,s)}return this};return{getParamValue:s,getParamValues:o,deleteParam:u,addParam:a,replaceParam:f,toString:r}},Uri=function(e){"use strict";var t=!1,n=function(e){var n={strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/},r=["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],i={name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},s=n[t?"strict":"loose"].exec(e),o={},u=14;while(u--)o[r[u]]=s[u]||"";return o[i.name]={},o[r[12]].replace(i.parser,function(e,t,n){t&&(o[i.name][t]=n)}),o},r=n(e||""),i=new Query(r.query),s=function(e){return typeof e!="undefined"&&(r.protocol=e),r.protocol},o=null,u=function(e){return typeof e!="undefined"&&(o=e),o===null?r.source.indexOf("//")!==-1:o},a=function(e){return typeof e!="undefined"&&(r.userInfo=e),r.userInfo},f=function(e){return typeof e!="undefined"&&(r.host=e),r.host},l=function(e){return typeof e!="undefined"&&(r.port=e),r.port},c=function(e){return typeof e!="undefined"&&(r.path=e),r.path},h=function(e){return typeof e!="undefined"&&(i=new Query(e)),i},p=function(e){return typeof e!="undefined"&&(r.anchor=e),r.anchor},d=function(e){return s(e),this},v=function(e){return u(e),this},m=function(e){return a(e),this},g=function(e){return f(e),this},y=function(e){return l(e),this},b=function(e){return c(e),this},w=function(e){return h(e),this},E=function(e){return p(e),this},S=function(e){return h().getParamValue(e)},x=function(e){return h().getParamValues(e)},T=function(e,t){return arguments.length===2?h().deleteParam(e,t):h().deleteParam(e),this},N=function(e,t,n){return arguments.length===3?h().addParam(e,t,n):h().addParam(e,t),this},C=function(e,t,n){return arguments.length===3?h().replaceParam(e,t,n):h().replaceParam(e,t),this},k=function(){var e="",t=function(e){return e!==null&&e!==""};return t(s())?(e+=s(),s().indexOf(":")!==s().length-1&&(e+=":"),e+="//"):u()&&t(f())&&(e+="//"),t(a())&&t(f())&&(e+=a(),a().indexOf("@")!==a().length-1&&(e+="@")),t(f())&&(e+=f(),t(l())&&(e+=":"+l())),t(c())?e+=c():t(f())&&(t(h().toString())||t(p()))&&(e+="/"),t(h().toString())&&(h().toString().indexOf("?")!==0&&(e+="?"),e+=h().toString()),t(p())&&(p().indexOf("#")!==0&&(e+="#"),e+=p()),e},L=function(){return new Uri(k())};return{protocol:s,hasAuthorityPrefix:u,userInfo:a,host:f,port:l,path:c,query:h,anchor:p,setProtocol:d,setHasAuthorityPrefix:v,setUserInfo:m,setHost:g,setPort:y,setPath:b,setQuery:w,setAnchor:E,getQueryParamValue:S,getQueryParamValues:x,deleteQueryParam:T,addQueryParam:N,replaceQueryParam:C,toString:k,clone:L}},jsUri=Uri;(function(e){e.fn.wTooltip=function(t,n){function m(n){n&&(t.degrade?e(h).html(t.content.replace(/<\/?[^>]+>/gi,"")):e(h).html(t.content))}function g(n){function i(e){r&&!t.content&&(r="")}function u(){!l&&t.auto&&(clearInterval(o),t.fadeOut?e(h).fadeOut(t.fadeOut,function(){i(n)}):(i(n),h.style.display="none")),typeof t.callAfter=="function"&&t.callAfter(h,n,t),d&&(t=e.listen(t))}t.timeout>0?s=setTimeout(function(){u()},t.timeout):u()}t=e.extend({content:null,ajax:null,follow:!0,auto:!0,fadeIn:0,fadeOut:0,appendTip:document.body,degrade:!1,offsetY:10,offsetX:1,style:{},className:null,id:null,callBefore:function(e,t,n){},callAfter:function(e,t,n){},clickAction:function(t,n){e(t).hide()},delay:0,timeout:0},t||{}),!t.style&&typeof t.style!="object"?(t.style={},t.style.zIndex="1000"):t.style=e.extend({border:"1px solid gray",background:"#edeef0",color:"#000",padding:"10px",zIndex:"1000",textAlign:"left"},t.style||{}),typeof n=="function"&&(t.callAfter=n||t.callAfter),t.style.display="none",t.style.position="absolute";var r,i,s,o,u={},f=!0,l=!1,c=!1,h=document.createElement("div"),p=typeof document.body.style.maxWidth=="undefined"?!0:!1,d=typeof e.talk=="function"&&typeof e.listen=="function"?!0:!1;t.id&&(h.id=t.id),t.className&&(h.className=t.className),t.degrade=t.degrade&&p?!0:!1;for(var v in t.style)h.style[v]=t.style[v];return t.ajax&&e.get(t.ajax,function(e){e&&(t.content=e),m(t.content)}),e(h).hover(function(){l=!0},function(){l=!1,g(u)}),d&&(t.key=h,t.plugin="wTooltip",t.channel="wayfarer",e.talk(t)),m(t.content&&!t.ajax),e(h).appendTo(t.appendTip),this.each(function(){e(this).hover(function(){function o(){typeof t.callBefore=="function"&&t.callBefore(h,n,t),d&&(t=e.listen(t));var i;t.content?t.degrade||(i="block"):r&&!t.degrade?(e(h).html(unescape(r)),i="block",r=""):i="none",t.auto&&(i=="block"&&t.fadeIn?e(h).fadeIn(t.fadeIn):h.style.display=i)}var n=this;clearTimeout(s),(this.title||this.titleMemKeep)&&!t.degrade&&!t.content&&(r=this.title||this.titleMemKeep,this.title&&(this.titleMemKeep=this.title,this.title="")),t.content&&t.degrade&&(this.title=h.innerHTML),t.delay>0?i=setTimeout(function(){o()},t.delay):o()},function(){clearTimeout(i);var n=this;f=!0,!t.follow||c||t.offsetX<0&&0-t.offsetX<e(h).outerWidth()&&t.offsetY>0&&0-t.offsetY<e(h).outerHeight()?setTimeout(function(){o=setInterval(function(){g(n)},1)},1):g(this)}),e(this).mousemove(function(n){u=this;if(t.follow||f){var r=e(window).scrollTop(),i=e(window).scrollLeft(),s=n.clientY+r+t.offsetY,o=n.clientX+i+t.offsetX,l=e(t.appendTip).outerHeight(),p=e(t.appendTip).innerHeight(),d=e(window).width()+i-e(h).outerWidth(),v=e(window).height()+r-e(h).outerHeight();s=l>p?s-(l-p):s,c=s>v||o>d?!0:!1,o-i<=0&&t.offsetX<0?o=i:o>d&&(o=d),s-r<=0&&t.offsetY<0?s=r:s>v&&(s=v),h.style.top=s+"px",h.style.left=o+"px",f=!1}}),typeof t.clickAction=="function"&&e(this).click(function(){t.clickAction(h,this)})})}})(jQuery),function(e){"use strict";var t={border:"solid 1px #6295fb",background:"#fff",color:"#000",padding:"5px",zIndex:1e3},n={activeTab:{},init:function(){e("input[id$=-color_none], input[id$=-color_random], input[id$=-color_set], input[id$=-color_span]").live("click",this.colorClickHandler),e(".utcw-tab-button").live("click",this.tabClickHandler),e("select[id$=-taxonomy]").live("change",this.taxonomyChangeHandler),e(document).ready(this.initTooltip),e(document).ajaxSuccess(this.ajaxSuccessHandler)},initTooltip:function(){e(".utcw-help").wTooltip({style:t,className:"utcw-tooltip"})},tabClickHandler:function(){var t=e(this);return t.data("id")==="utcw-__i__"?!1:(t.parent().find(".utcw-tab-button").removeClass("utcw-active"),t.addClass("utcw-active"),t.parent().find("fieldset.utcw").addClass("hidden"),e("#"+t.data("tab")).removeClass("hidden"),n.activeTab[t.data("id")]=t.data("tab"),!1)},colorClickHandler:function(){var t=e("div[id$='set_chooser']"),n=e("div[id$='span_chooser']"),r=e(this).val();t.addClass("utcw-hidden"),n.addClass("utcw-hidden"),r==="set"?t.removeClass("utcw-hidden"):r==="span"&&n.removeClass("utcw-hidden")},taxonomyChangeHandler:function(){var t=e(this),n=t.find(":selected").val(),r=t.parents(".widget-content");r.find("div[id$=-terms]").addClass("hidden"),r.find("#"+n+"-terms").removeClass("hidden")},ajaxSuccessHandler:function(e,t,r){n.setCurrentTab.apply(n,[r.data]),n.initTooltip.apply(n)},setCurrentTab:function(t){var n=new Uri,r;n.setQuery(decodeURI(t)),n.getQueryParamValue("action")==="save-widget"&&n.getQueryParamValue("id_base")==="utcw"&&(r=n.getQueryParamValue("widget-id"),this.activeTab[r]&&e("button[data-tab='"+this.activeTab[r]+"']").trigger("click"))}};n.init()}(jQuery);
utcw-data.php CHANGED
@@ -138,35 +138,57 @@ class UTCW_Data {
138
  $parameters[ ] = $this->config->minimum;
139
  }
140
 
141
- // Try to sort the result using SQL if possible
142
- $order = $this->config->reverse ? 'DESC' : 'ASC';
143
- $binary = $this->config->case_sensitive ? 'BINARY ' : '';
144
 
145
- switch ( $this->config->order ) {
146
- case 'random':
147
- $query[ ] = 'ORDER BY RAND() ' . $order;
148
- break;
149
 
150
- case 'name':
151
- $query[ ] = 'ORDER BY ' . $binary . 'name ' . $order;
152
- break;
153
 
154
- case 'slug':
155
- $query[ ] = 'ORDER BY ' . $binary . 'slug ' . $order;
156
- break;
 
157
 
158
- case 'id':
159
- $query[ ] = 'ORDER BY term_id ' . $order;
160
- break;
161
 
162
- case 'count':
163
- $query[ ] = 'ORDER BY count ' . $order;
164
- break;
165
  }
166
 
167
- // Add limit constraint
168
- $query[ ] = 'LIMIT %d';
169
- $parameters[ ] = $this->config->max;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  // Build query
172
  $query = join( "\n", $query );
138
  $parameters[ ] = $this->config->minimum;
139
  }
140
 
141
+ // Always sort the result by count DESC to always work with the top result
142
+ $query[ ] = 'ORDER BY count DESC';
 
143
 
144
+ // Add limit constraint
145
+ $query[ ] = 'LIMIT %d';
146
+ $parameters[ ] = $this->config->max;
 
147
 
148
+ // If the result should be ordered in another way, try to create a sub-query to sort the result
149
+ // directly in the database query
150
+ $subquery_required = true;
151
 
152
+ // No subquery is needed if the order should be by count desc (it's already sorted that way)
153
+ if ( $this->config->reverse && $this->config->order == 'count' ) {
154
+ $subquery_required = false;
155
+ }
156
 
 
 
 
157
 
158
+ // No subquery is needed if the order should be by color since the sorting is done in PHP afterwards
159
+ if ( $this->config->order == 'color' ) {
160
+ $subquery_required = false;
161
  }
162
 
163
+ if ( $subquery_required ) {
164
+ array_unshift( $query, 'SELECT * FROM (' );
165
+ $query[ ] = ') AS subQuery';
166
+
167
+ $order = $this->config->reverse ? 'DESC' : 'ASC';
168
+ $binary = $this->config->case_sensitive ? 'BINARY ' : '';
169
+
170
+ switch ( $this->config->order ) {
171
+ case 'random':
172
+ $query[ ] = 'ORDER BY RAND() ' . $order;
173
+ break;
174
+
175
+ case 'name':
176
+ $query[ ] = 'ORDER BY ' . $binary . 'name ' . $order;
177
+ break;
178
+
179
+ case 'slug':
180
+ $query[ ] = 'ORDER BY ' . $binary . 'slug ' . $order;
181
+ break;
182
+
183
+ case 'id':
184
+ $query[ ] = 'ORDER BY term_id ' . $order;
185
+ break;
186
+
187
+ case 'count':
188
+ $query[ ] = 'ORDER BY count ' . $order;
189
+ break;
190
+ }
191
+ }
192
 
193
  // Build query
194
  $query = join( "\n", $query );
utcw-widget.php CHANGED
@@ -62,6 +62,15 @@ class UTCW extends WP_Widget {
62
  }
63
  }
64
 
 
 
 
 
 
 
 
 
 
65
  $config = new UTCW_Config( $new_instance, $this->plugin );
66
 
67
  if ( isset( $new_instance[ 'save_config' ] ) && isset( $new_instance[ 'save_config_name' ] ) && $new_instance[ 'save_config_name' ] ) {
62
  }
63
  }
64
 
65
+ // Checkbox inputs which are unchecked, will not be set in $new_instance. Set them manually to false
66
+ $checkbox_settings = array( 'show_title_text', 'show_title', 'debug', 'reverse', 'case_sensitive' );
67
+
68
+ foreach ( $checkbox_settings as $checkbox_setting ) {
69
+ if ( ! isset( $new_instance[ $checkbox_setting ] ) ) {
70
+ $new_instance[ $checkbox_setting ] = false;
71
+ }
72
+ }
73
+
74
  $config = new UTCW_Config( $new_instance, $this->plugin );
75
 
76
  if ( isset( $new_instance[ 'save_config' ] ) && isset( $new_instance[ 'save_config_name' ] ) && $new_instance[ 'save_config_name' ] ) {