tobozo / YOURLS-GeoShortURL

🔗 YOURLS plugin to add per-country support to existing short URLs
MIT License
7 stars 2 forks source link

Country primary - continet secondary #2

Open dragosion opened 5 years ago

dragosion commented 5 years ago

Thinking like that it might be even more useful.

Have the country or when there is no country code alternate link it could look for a continent code as a next step.

Thinking of adding this in a separate fork, didn't do this before here so hope I'm doing it right. :)

For example: link for Romania redirects to: link-ro if it exists and stops here. if link-ro does not exist try to find link-eu and redirect to that.

if nothing is found, go to link.

This should be possible hopefully, one extra step.

tobozo commented 5 years ago

hey, thanks for your feedback :+1:

this sounds good as a general rule, do you want to submit a PR for that ?

dragosion commented 5 years ago

I'll try to add it in my fork and will send a PR. Hope to have something within 1-2 weeks.

fezzzza commented 1 year ago

I came here to suggest the same thing, but i've already implemented it. I think the following code works. It resolves the country first, and then the continent if no country match is found. Country codes are in lower case, continent codes are in upper case, because there are some official continent codes that clash with country codes. Replace the final "if/else" block with this (sorry I don't know how or have the time to do a PR):

` $countries = array('ao','bf','bi','bj','bw','cd','cf','cg','ci','cm','cv','dj','dz','eg','eh','er','et','ga','gh','gm','gn','gq','gw','ke','km','lr','ls','ly','ma','mg','ml','mr','mu','mw','mz','na','ne','ng','re','rw','sc','sd','sh','sl','sn','so','ss','st','sz','td','tg','tn','tz','ug','yt','za','zm','zw','aq','bv','gs','hm','tf','ae','af','am','az','bd','bh','bn','bt','cc','cn','ge','hk','id','il','in','io','iq','ir','jo','jp','kg','kh','kp','kr','kw','kz','la','lb','lk','mm','mn','mo','mv','my','np','om','ph','pk','ps','qa','sa','sg','sy','th','tj','tm','tr','tw','uz','vn','ye','ad','al','at','ax','ba','be','bg','by','ch','cy','cz','de','dk','ee','es','fi','fo','fr','gb','gg','gi','gr','hr','hu','ie','im','is','it','je','li','lt','lu','lv','mc','md','me','mk','mt','nl','no','pl','pt','ro','rs','ru','se','si','sj','sk','sm','ua','va','xk','ag','ai','aw','bb','bl','bm','bq','bs','bz','ca','cr','cu','cw','dm','do','gd','gl','gp','gt','hn','ht','jm','kn','ky','lc','mf','mq','ms','mx','ni','pa','pm','pr','sv','sx','tc','tt','us','vc','vg','vi','as','au','ck','cx','fj','fm','gu','ki','mh','mp','nc','nf','nr','nu','nz','pf','pg','pn','pw','sb','tk','tl','to','tv','um','vu','wf','ws','ar','bo','br','cl','co','ec','fk','gf','gy','pe','py','sr','uy','ve'); $continents = array('AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AF','AN','AN','AN','AN','AN','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','AS','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','EU','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','NA','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','OC','SA','SA','SA','SA','SA','SA','SA','SA','SA','SA','SA','SA','SA','SA'); //continents are AF=Africa, AN=Antarctica, AS=Asia, EU=Europe, NA=North America, OC=Oceania, SA=South America

$continentCode=str_replace($countries,$continents,$countryCode);

if(!empty($geoLongURL)) { return $geoRequest; } else { $geoRequest = $request .'-'. $continentCode; $geoLongURL = yourls_get_keyword_longurl( $geoRequest ); if(!empty($geoLongURL)) { return $geoRequest; } else { return $request; } } `