Skip to content

Instantly share code, notes, and snippets.

@psorensen
Created February 13, 2015 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psorensen/ed981693d581accf58d4 to your computer and use it in GitHub Desktop.
Save psorensen/ed981693d581accf58d4 to your computer and use it in GitHub Desktop.
Yoast SEO JS Fix
function yst_clean(a){if(""==a||void 0==a)return"";try{a=jQuery("<div/>").html(a).text(),a=a.replace(/<\/?[^>]+>/gi,""),a=a.replace(/\[(.+?)\](.+?\[\/\\1\])?/g,"")}catch(b){}return a}function ptest(a,b){a=yst_clean(a),a=a.toLowerCase();var c=a.match(b);return null!=c?'<span class="good">Yes ('+c.length+")</span>":'<span class="wrong">No</span>'}function removeLowerCaseDiacritics(a){var b,c=[{base:"a",letters:/[\u0061\u24D0\uFF41\u1E9A\u00E0\u00E1\u00E2\u1EA7\u1EA5\u1EAB\u1EA9\u00E3\u0101\u0103\u1EB1\u1EAF\u1EB5\u1EB3\u0227\u01E1\u00E4\u01DF\u1EA3\u00E5\u01FB\u01CE\u0201\u0203\u1EA1\u1EAD\u1EB7\u1E01\u0105\u2C65\u0250]/g},{base:"aa",letters:/[\uA733]/g},{base:"ae",letters:/[\u00E6\u01FD\u01E3]/g},{base:"ao",letters:/[\uA735]/g},{base:"au",letters:/[\uA737]/g},{base:"av",letters:/[\uA739\uA73B]/g},{base:"ay",letters:/[\uA73D]/g},{base:"b",letters:/[\u0062\u24D1\uFF42\u1E03\u1E05\u1E07\u0180\u0183\u0253]/g},{base:"c",letters:/[\u0063\u24D2\uFF43\u0107\u0109\u010B\u010D\u00E7\u1E09\u0188\u023C\uA73F\u2184]/g},{base:"d",letters:/[\u0064\u24D3\uFF44\u1E0B\u010F\u1E0D\u1E11\u1E13\u1E0F\u0111\u018C\u0256\u0257\uA77A]/g},{base:"dz",letters:/[\u01F3\u01C6]/g},{base:"e",letters:/[\u0065\u24D4\uFF45\u00E8\u00E9\u00EA\u1EC1\u1EBF\u1EC5\u1EC3\u1EBD\u0113\u1E15\u1E17\u0115\u0117\u00EB\u1EBB\u011B\u0205\u0207\u1EB9\u1EC7\u0229\u1E1D\u0119\u1E19\u1E1B\u0247\u025B\u01DD]/g},{base:"f",letters:/[\u0066\u24D5\uFF46\u1E1F\u0192\uA77C]/g},{base:"g",letters:/[\u0067\u24D6\uFF47\u01F5\u011D\u1E21\u011F\u0121\u01E7\u0123\u01E5\u0260\uA7A1\u1D79\uA77F]/g},{base:"h",letters:/[\u0068\u24D7\uFF48\u0125\u1E23\u1E27\u021F\u1E25\u1E29\u1E2B\u1E96\u0127\u2C68\u2C76\u0265]/g},{base:"hv",letters:/[\u0195]/g},{base:"i",letters:/[\u0069\u24D8\uFF49\u00EC\u00ED\u00EE\u0129\u012B\u012D\u00EF\u1E2F\u1EC9\u01D0\u0209\u020B\u1ECB\u012F\u1E2D\u0268\u0131]/g},{base:"j",letters:/[\u006A\u24D9\uFF4A\u0135\u01F0\u0249]/g},{base:"k",letters:/[\u006B\u24DA\uFF4B\u1E31\u01E9\u1E33\u0137\u1E35\u0199\u2C6A\uA741\uA743\uA745\uA7A3]/g},{base:"l",letters:/[\u006C\u24DB\uFF4C\u0140\u013A\u013E\u1E37\u1E39\u013C\u1E3D\u1E3B\u017F\u0142\u019A\u026B\u2C61\uA749\uA781\uA747]/g},{base:"lj",letters:/[\u01C9]/g},{base:"m",letters:/[\u006D\u24DC\uFF4D\u1E3F\u1E41\u1E43\u0271\u026F]/g},{base:"n",letters:/[\u006E\u24DD\uFF4E\u01F9\u0144\u00F1\u1E45\u0148\u1E47\u0146\u1E4B\u1E49\u019E\u0272\u0149\uA791\uA7A5]/g},{base:"nj",letters:/[\u01CC]/g},{base:"o",letters:/[\u006F\u24DE\uFF4F\u00F2\u00F3\u00F4\u1ED3\u1ED1\u1ED7\u1ED5\u00F5\u1E4D\u022D\u1E4F\u014D\u1E51\u1E53\u014F\u022F\u0231\u00F6\u022B\u1ECF\u0151\u01D2\u020D\u020F\u01A1\u1EDD\u1EDB\u1EE1\u1EDF\u1EE3\u1ECD\u1ED9\u01EB\u01ED\u00F8\u01FF\u0254\uA74B\uA74D\u0275]/g},{base:"oi",letters:/[\u01A3]/g},{base:"ou",letters:/[\u0223]/g},{base:"oo",letters:/[\uA74F]/g},{base:"p",letters:/[\u0070\u24DF\uFF50\u1E55\u1E57\u01A5\u1D7D\uA751\uA753\uA755]/g},{base:"q",letters:/[\u0071\u24E0\uFF51\u024B\uA757\uA759]/g},{base:"r",letters:/[\u0072\u24E1\uFF52\u0155\u1E59\u0159\u0211\u0213\u1E5B\u1E5D\u0157\u1E5F\u024D\u027D\uA75B\uA7A7\uA783]/g},{base:"s",letters:/[\u0073\u24E2\uFF53\u00DF\u015B\u1E65\u015D\u1E61\u0161\u1E67\u1E63\u1E69\u0219\u015F\u023F\uA7A9\uA785\u1E9B]/g},{base:"t",letters:/[\u0074\u24E3\uFF54\u1E6B\u1E97\u0165\u1E6D\u021B\u0163\u1E71\u1E6F\u0167\u01AD\u0288\u2C66\uA787]/g},{base:"tz",letters:/[\uA729]/g},{base:"u",letters:/[\u0075\u24E4\uFF55\u00F9\u00FA\u00FB\u0169\u1E79\u016B\u1E7B\u016D\u00FC\u01DC\u01D8\u01D6\u01DA\u1EE7\u016F\u0171\u01D4\u0215\u0217\u01B0\u1EEB\u1EE9\u1EEF\u1EED\u1EF1\u1EE5\u1E73\u0173\u1E77\u1E75\u0289]/g},{base:"v",letters:/[\u0076\u24E5\uFF56\u1E7D\u1E7F\u028B\uA75F\u028C]/g},{base:"vy",letters:/[\uA761]/g},{base:"w",letters:/[\u0077\u24E6\uFF57\u1E81\u1E83\u0175\u1E87\u1E85\u1E98\u1E89\u2C73]/g},{base:"x",letters:/[\u0078\u24E7\uFF58\u1E8B\u1E8D]/g},{base:"y",letters:/[\u0079\u24E8\uFF59\u1EF3\u00FD\u0177\u1EF9\u0233\u1E8F\u00FF\u1EF7\u1E99\u1EF5\u01B4\u024F\u1EFF]/g},{base:"z",letters:/[\u007A\u24E9\uFF5A\u017A\u1E91\u017C\u017E\u1E93\u1E95\u01B6\u0225\u0240\u2C6C\uA763]/g}];b||(b=c);for(var d=0;d<b.length;d++)a=a.replace(b[d].letters,b[d].base);return a}function yst_testFocusKw(){var a=jQuery.trim(jQuery("#"+wpseoMetaboxL10n.field_prefix+"focuskw").val());if(a=yst_escapeFocusKw(a).toLowerCase(),jQuery("#editable-post-name-full").length)var b=jQuery("#editable-post-name-full").text(),c=wpseoMetaboxL10n.wpseo_permalink_template.replace("%postname%",b).replace("http://","");var d=new RegExp("(^|[ s\n\r .,'(\"+;!?:-])"+a+"($|[ s\n\r .,')\"+!?:;-])","gim"),e=removeLowerCaseDiacritics(a),f=new RegExp(e.replace(/\s+/g,"[-_\\//]"),"gim"),g=jQuery("#focuskwresults"),h=jQuery("#wpseosnippet").find(".desc span.content").text();if(""!=a){var i="<p>"+wpseoMetaboxL10n.keyword_header+"</p>";i+="<ul>",jQuery("#title").length&&(i+="<li>"+wpseoMetaboxL10n.article_header_text+ptest(jQuery("#title").val(),d)+"</li>"),i+="<li>"+wpseoMetaboxL10n.page_title_text+ptest(jQuery("#wpseosnippet_title").text(),d)+"</li>",i+="<li>"+wpseoMetaboxL10n.page_url_text+ptest(c,f)+"</li>",jQuery("#content").length&&(i+="<li>"+wpseoMetaboxL10n.content_text+ptest(jQuery("#content").val(),d)+"</li>"),i+="<li>"+wpseoMetaboxL10n.meta_description_text+ptest(h,d)+"</li>",i+="</ul>",g.html(i)}else g.html("")}function yst_replaceVariables(a,b){if("undefined"==typeof a)return"";jQuery("#title").length&&(a=a.replace(/%%title%%/g,jQuery("#title").val())),a=a.replace(/%%sitedesc%%/g,wpseoMetaboxL10n.sitedesc),a=a.replace(/%%sitename%%/g,wpseoMetaboxL10n.sitename),a=a.replace(/%%sep%%/g,wpseoMetaboxL10n.sep),a=a.replace(/%%date%%/g,wpseoMetaboxL10n.date),a=a.replace(/%%id%%/g,wpseoMetaboxL10n.id),a=a.replace(/%%page%%/g,wpseoMetaboxL10n.page),a=a.replace(/%%currenttime%%/g,wpseoMetaboxL10n.currenttime),a=a.replace(/%%currentdate%%/g,wpseoMetaboxL10n.currentdate),a=a.replace(/%%currentday%%/g,wpseoMetaboxL10n.currentday),a=a.replace(/%%currentmonth%%/g,wpseoMetaboxL10n.currentmonth),a=a.replace(/%%currentyear%%/g,wpseoMetaboxL10n.currentyear),a=a.replace(/%%focuskw%%/g,jQuery("#yoast_wpseo_focuskw").val());var c="";jQuery("#excerpt").length&&(c=yst_clean(jQuery("#excerpt").val()),a=a.replace(/%%excerpt_only%%/g,c)),""==c&&jQuery("#content").length&&(c=jQuery("#content").val().replace(/(<([^>]+)>)/gi,"").substring(0,wpseoMetaboxL10n.wpseo_meta_desc_length-1)),a=a.replace(/%%excerpt%%/g,c),jQuery("#parent_id").length&&jQuery("#parent_id option:selected").text()!=wpseoMetaboxL10n.no_parent_text&&(a=a.replace(/%%parent_title%%/g,jQuery("#parent_id option:selected").text()));var d=yst_escapeFocusKw(wpseoMetaboxL10n.sep),e=new RegExp(d+" "+d,"g");if(a=a.replace(e,wpseoMetaboxL10n.sep),-1!=a.indexOf("%%")&&null!=a.match(/%%[a-z0-9_-]+%%/i))for(regex=/%%[a-z0-9_-]+%%/gi,matches=a.match(regex),i=0;i<matches.length;i++)void 0!=replacedVars[matches[i]]?a=a.replace(matches[i],replacedVars[matches[i]]):(replaceableVar=matches[i],replacedVars[replaceableVar]="",jQuery.post(ajaxurl,{action:"wpseo_replace_vars",string:matches[i],post_id:jQuery("#post_ID").val(),_wpnonce:wpseoMetaboxL10n.wpseo_replace_vars_nonce},function(c){c?(replacedVars[replaceableVar]=c,yst_replaceVariables(a,b)):yst_replaceVariables(a,b)}));b(a)}function yst_updateTitle(a){var b="",c=jQuery("#"+wpseoMetaboxL10n.field_prefix+"title"),d=jQuery("#"+wpseoMetaboxL10n.field_prefix+"title-length-warning"),e=jQuery("<div />"),f=jQuery("#wpseosnippet_title");return c.val()?b=c.val():(b=wpseoMetaboxL10n.wpseo_title_template,b=e.html(b).text()),""==b?(f.html(""),void d.hide()):(b=yst_clean(b),b=jQuery.trim(b),b=e.text(b).html(),a&&c.val(b),void(b=yst_replaceVariables(b,function(a){var b=e.html(a).text();c.attr("placeholder",b),a=yst_clean(a),a=yst_boldKeywords(a,!1),jQuery("#wpseosnippet_title").html(a);var f=document.getElementById("wpseosnippet_title");null!=f&&(f.scrollWidth>f.clientWidth?d.show():d.hide()),yst_testFocusKw()})))}function yst_updateDesc(){var a=jQuery.trim(yst_clean(jQuery("#"+wpseoMetaboxL10n.field_prefix+"metadesc").val())),b=jQuery("<div />"),c=jQuery("#wpseosnippet");if(""==a&&""!=wpseoMetaboxL10n.wpseo_metadesc_template&&(a=wpseoMetaboxL10n.wpseo_metadesc_template),""!=a)a=yst_replaceVariables(a,function(a){a=b.text(a).html(),a=yst_clean(a);var d=-1;d=wpseoMetaboxL10n.wpseo_meta_desc_length-a.length,d=0>d?'<span class="wrong">'+d+"</span>":'<span class="good">'+d+"</span>",jQuery("#"+wpseoMetaboxL10n.field_prefix+"metadesc-length").html(d),a=yst_trimDesc(a),a=yst_boldKeywords(a,!1),c.find(".desc span.autogen").html(""),c.find(".desc span.content").html(a),yst_testFocusKw()});else{c.find(".desc span.content").html(""),yst_testFocusKw(),jQuery("#content").length&&(a=jQuery("#content").val(),a=yst_clean(a));var d=yst_escapeFocusKw(jQuery.trim(jQuery("#"+wpseoMetaboxL10n.field_prefix+"focuskw").val()));if(""!=d){var e=new RegExp(d,"gim");a=-1!=a.search(e)&&a.length>wpseoMetaboxL10n.wpseo_meta_desc_length?a.substr(a.search(e),wpseoMetaboxL10n.wpseo_meta_desc_length):a.substr(0,wpseoMetaboxL10n.wpseo_meta_desc_length)}else a=a.substr(0,wpseoMetaboxL10n.wpseo_meta_desc_length);a=yst_boldKeywords(a,!1),a=yst_trimDesc(a),c.find(".desc span.autogen").html(a)}}function yst_trimDesc(a){if(a.length>wpseoMetaboxL10n.wpseo_meta_desc_length){var b;b=a.length>wpseoMetaboxL10n.wpseo_meta_desc_length?a.lastIndexOf(" ",wpseoMetaboxL10n.wpseo_meta_desc_length-3):wpseoMetaboxL10n.wpseo_meta_desc_length,a=a.substring(0,b).concat(" ...")}return a}function yst_updateURL(){if(jQuery("#editable-post-name-full").length)var a=jQuery("#editable-post-name-full").text(),b=wpseoMetaboxL10n.wpseo_permalink_template.replace("%postname%",a).replace("http://","");b=yst_boldKeywords(b,!0),jQuery("#wpseosnippet").find(".url").html(b),yst_testFocusKw()}function yst_boldKeywords(a,b){var c,d=yst_escapeFocusKw(jQuery.trim(jQuery("#"+wpseoMetaboxL10n.field_prefix+"focuskw").val()));if(""==d)return a;c=-1!=d.search(" ")?d.split(" "):new Array(d);for(var e=0;e<c.length;e++){var f=yst_clean(c[e]),g="";b?(f=f.replace(" ","-").toLowerCase(),g=new RegExp("([-/])("+f+")([-/])?")):g=new RegExp("(^|[ s\n\r .,'(\"+;!?:-]+)("+f+")($|[ s\n\r .,')\"+;!?:-]+)","gim"),void 0!=a&&(a=a.replace(g,"$1<strong>$2</strong>$3"))}return a}function yst_updateSnippet(){yst_updateURL(),yst_updateTitle(),yst_updateDesc()}function yst_escapeFocusKw(a){return a.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&")}var delay=function(){var a=0;return function(b,c){clearTimeout(a),a=setTimeout(b,c)}}();jQuery(document).ready(function(){if(replacedVars=new Array,jQuery(".wpseo-metabox-tabs-div").length>0){var a=window.location.hash;a=""==a||-1==a.search("wpseo")?"general":a.replace("#wpseo_",""),jQuery("."+a).addClass("active");var b=jQuery("#"+wpseoMetaboxL10n.field_prefix+"metadesc"),c=jQuery.trim(yst_clean(b.val()));c=jQuery("<div />").html(c).text(),b.val(c),jQuery("a.wpseo_tablink").click(function(){jQuery(".wpseo-metabox-tabs li").removeClass("active"),jQuery(".wpseotab").removeClass("active");var a=jQuery(this).attr("href").replace("#wpseo_","");if(jQuery("."+a).addClass("active"),jQuery(this).parent().addClass("active"),jQuery(this).hasClass("scroll")){var b=jQuery(this).attr("href").replace("wpseo_","");jQuery("html, body").animate({scrollTop:jQuery(b).offset().top},500)}})}jQuery(".wpseo-heading").hide(),jQuery(".wpseo-metabox-tabs").show();var d,e={};
//This line causes Javascript errors throughought the admin
//jQuery("#"+wpseoMetaboxL10n.field_prefix+"focuskw").autocomplete({minLength:3,formatResult:function(a){return jQuery("<div/>").html(a).html()},source:function(a,b){var c=a.term;return c in e?void b(e[c]):(a._ajax_nonce=wpseoMetaboxL10n.wpseo_keyword_suggest_nonce,a.action="wpseo_get_suggest",void(d=jQuery.getJSON(ajaxurl,a,function(a,f,g){e[c]=a,g===d&&b(a)})))}}),
jQuery("#"+wpseoMetaboxL10n.field_prefix+"title").keyup(function(){yst_updateTitle()}),jQuery("#title").keyup(function(){yst_updateTitle(),yst_updateDesc()}),jQuery("#parent_id").change(function(){yst_updateTitle(),yst_updateDesc()}),jQuery("#"+wpseoMetaboxL10n.field_prefix+"metadesc").keyup(function(){yst_updateDesc()}),jQuery("#excerpt").keyup(function(){yst_updateDesc()}),jQuery("#content").focusout(function(){yst_updateDesc()});var f=!1;jQuery(document).on("change","#"+wpseoMetaboxL10n.field_prefix+"focuskw",function(){var a=jQuery("#focuskwhelp");-1!=jQuery("#"+wpseoMetaboxL10n.field_prefix+"focuskw").val().search(",")?(a.click(),f=!0):f&&(a.qtip("hide"),f=!1),yst_updateSnippet()}),jQuery(".yoast_help").qtip({content:{attr:"alt"},position:{my:"bottom left",at:"top center"},style:{tip:{corner:!0},classes:"yoast-qtip qtip-rounded qtip-blue"},show:{when:{event:"mouseover"}},hide:{fixed:!0,when:{event:"mouseout"}}}),yst_updateSnippet()});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment