api-helpers.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562
  1. <?php
  2. /*
  3. * acf_is_array
  4. *
  5. * This function will return true for a non empty array
  6. *
  7. * @type function
  8. * @date 6/07/2016
  9. * @since 5.4.0
  10. *
  11. * @param $array (array)
  12. * @return (boolean)
  13. */
  14. function acf_is_array( $array ) {
  15. return ( is_array( $array ) && ! empty( $array ) );
  16. }
  17. /**
  18. * acf_has_setting
  19. *
  20. * alias of acf()->has_setting()
  21. *
  22. * @date 2/2/18
  23. * @since 5.6.5
  24. *
  25. * @param n/a
  26. * @return n/a
  27. */
  28. function acf_has_setting( $name = '' ) {
  29. return acf()->has_setting( $name );
  30. }
  31. /**
  32. * acf_raw_setting
  33. *
  34. * alias of acf()->get_setting()
  35. *
  36. * @date 2/2/18
  37. * @since 5.6.5
  38. *
  39. * @param n/a
  40. * @return n/a
  41. */
  42. function acf_raw_setting( $name = '' ) {
  43. return acf()->get_setting( $name );
  44. }
  45. /*
  46. * acf_update_setting
  47. *
  48. * alias of acf()->update_setting()
  49. *
  50. * @type function
  51. * @date 28/09/13
  52. * @since 5.0.0
  53. *
  54. * @param $name (string)
  55. * @param $value (mixed)
  56. * @return n/a
  57. */
  58. function acf_update_setting( $name, $value ) {
  59. // validate name
  60. $name = acf_validate_setting( $name );
  61. // update
  62. return acf()->update_setting( $name, $value );
  63. }
  64. /**
  65. * acf_validate_setting
  66. *
  67. * Returns the changed setting name if available.
  68. *
  69. * @date 2/2/18
  70. * @since 5.6.5
  71. *
  72. * @param n/a
  73. * @return n/a
  74. */
  75. function acf_validate_setting( $name = '' ) {
  76. return apply_filters( 'acf/validate_setting', $name );
  77. }
  78. /*
  79. * acf_get_setting
  80. *
  81. * alias of acf()->get_setting()
  82. *
  83. * @type function
  84. * @date 28/09/13
  85. * @since 5.0.0
  86. *
  87. * @param n/a
  88. * @return n/a
  89. */
  90. function acf_get_setting( $name, $value = null ) {
  91. // validate name
  92. $name = acf_validate_setting( $name );
  93. // check settings
  94. if ( acf_has_setting( $name ) ) {
  95. $value = acf_raw_setting( $name );
  96. }
  97. // filter
  98. $value = apply_filters( "acf/settings/{$name}", $value );
  99. // return
  100. return $value;
  101. }
  102. /*
  103. * acf_append_setting
  104. *
  105. * This function will add a value into the settings array found in the acf object
  106. *
  107. * @type function
  108. * @date 28/09/13
  109. * @since 5.0.0
  110. *
  111. * @param $name (string)
  112. * @param $value (mixed)
  113. * @return n/a
  114. */
  115. function acf_append_setting( $name, $value ) {
  116. // vars
  117. $setting = acf_raw_setting( $name );
  118. // bail early if not array
  119. if ( ! is_array( $setting ) ) {
  120. $setting = array();
  121. }
  122. // append
  123. $setting[] = $value;
  124. // update
  125. return acf_update_setting( $name, $setting );
  126. }
  127. /**
  128. * acf_get_data
  129. *
  130. * Returns data.
  131. *
  132. * @date 28/09/13
  133. * @since 5.0.0
  134. *
  135. * @param string $name
  136. * @return mixed
  137. */
  138. function acf_get_data( $name ) {
  139. return acf()->get_data( $name );
  140. }
  141. /**
  142. * acf_set_data
  143. *
  144. * Sets data.
  145. *
  146. * @date 28/09/13
  147. * @since 5.0.0
  148. *
  149. * @param string $name
  150. * @param mixed $value
  151. * @return n/a
  152. */
  153. function acf_set_data( $name, $value ) {
  154. return acf()->set_data( $name, $value );
  155. }
  156. /**
  157. * Appends data to an existing key.
  158. *
  159. * @date 11/06/2020
  160. * @since 5.9.0
  161. *
  162. * @param string $name The data name.
  163. * @return array $data The data array.
  164. */
  165. function acf_append_data( $name, $data ) {
  166. $prev_data = acf()->get_data( $name );
  167. if ( is_array( $prev_data ) ) {
  168. $data = array_merge( $prev_data, $data );
  169. }
  170. acf()->set_data( $name, $data );
  171. }
  172. /*
  173. * acf_init
  174. *
  175. * alias of acf()->init()
  176. *
  177. * @type function
  178. * @date 28/09/13
  179. * @since 5.0.0
  180. *
  181. * @param n/a
  182. * @return n/a
  183. */
  184. function acf_init() {
  185. acf()->init();
  186. }
  187. /*
  188. * acf_has_done
  189. *
  190. * This function will return true if this action has already been done
  191. *
  192. * @type function
  193. * @date 16/12/2015
  194. * @since 5.3.2
  195. *
  196. * @param $name (string)
  197. * @return (boolean)
  198. */
  199. function acf_has_done( $name ) {
  200. // return true if already done
  201. if ( acf_raw_setting( "has_done_{$name}" ) ) {
  202. return true;
  203. }
  204. // update setting and return
  205. acf_update_setting( "has_done_{$name}", true );
  206. return false;
  207. }
  208. /*
  209. * acf_get_external_path
  210. *
  211. * This function will return the path to a file within an external folder
  212. *
  213. * @type function
  214. * @date 22/2/17
  215. * @since 5.5.8
  216. *
  217. * @param $file (string)
  218. * @param $path (string)
  219. * @return (string)
  220. */
  221. function acf_get_external_path( $file, $path = '' ) {
  222. return plugin_dir_path( $file ) . $path;
  223. }
  224. /*
  225. * acf_get_external_dir
  226. *
  227. * This function will return the url to a file within an external folder
  228. *
  229. * @type function
  230. * @date 22/2/17
  231. * @since 5.5.8
  232. *
  233. * @param $file (string)
  234. * @param $path (string)
  235. * @return (string)
  236. */
  237. function acf_get_external_dir( $file, $path = '' ) {
  238. return acf_plugin_dir_url( $file ) . $path;
  239. }
  240. /**
  241. * acf_plugin_dir_url
  242. *
  243. * This function will calculate the url to a plugin folder.
  244. * Different to the WP plugin_dir_url(), this function can calculate for urls outside of the plugins folder (theme include).
  245. *
  246. * @date 13/12/17
  247. * @since 5.6.8
  248. *
  249. * @param type $var Description. Default.
  250. * @return type Description.
  251. */
  252. function acf_plugin_dir_url( $file ) {
  253. // vars
  254. $path = plugin_dir_path( $file );
  255. $path = wp_normalize_path( $path );
  256. // check plugins
  257. $check_path = wp_normalize_path( realpath( WP_PLUGIN_DIR ) );
  258. if ( strpos( $path, $check_path ) === 0 ) {
  259. return str_replace( $check_path, plugins_url(), $path );
  260. }
  261. // check wp-content
  262. $check_path = wp_normalize_path( realpath( WP_CONTENT_DIR ) );
  263. if ( strpos( $path, $check_path ) === 0 ) {
  264. return str_replace( $check_path, content_url(), $path );
  265. }
  266. // check root
  267. $check_path = wp_normalize_path( realpath( ABSPATH ) );
  268. if ( strpos( $path, $check_path ) === 0 ) {
  269. return str_replace( $check_path, site_url( '/' ), $path );
  270. }
  271. // return
  272. return plugin_dir_url( $file );
  273. }
  274. /*
  275. * acf_parse_args
  276. *
  277. * This function will merge together 2 arrays and also convert any numeric values to ints
  278. *
  279. * @type function
  280. * @date 18/10/13
  281. * @since 5.0.0
  282. *
  283. * @param $args (array)
  284. * @param $defaults (array)
  285. * @return $args (array)
  286. */
  287. function acf_parse_args( $args, $defaults = array() ) {
  288. // parse args
  289. $args = wp_parse_args( $args, $defaults );
  290. // parse types
  291. $args = acf_parse_types( $args );
  292. // return
  293. return $args;
  294. }
  295. /*
  296. * acf_parse_types
  297. *
  298. * This function will convert any numeric values to int and trim strings
  299. *
  300. * @type function
  301. * @date 18/10/13
  302. * @since 5.0.0
  303. *
  304. * @param $var (mixed)
  305. * @return $var (mixed)
  306. */
  307. function acf_parse_types( $array ) {
  308. return array_map( 'acf_parse_type', $array );
  309. }
  310. /*
  311. * acf_parse_type
  312. *
  313. * description
  314. *
  315. * @type function
  316. * @date 11/11/2014
  317. * @since 5.0.9
  318. *
  319. * @param $post_id (int)
  320. * @return $post_id (int)
  321. */
  322. function acf_parse_type( $v ) {
  323. // Check if is string.
  324. if ( is_string( $v ) ) {
  325. // Trim ("Word " = "Word").
  326. $v = trim( $v );
  327. // Convert int strings to int ("123" = 123).
  328. if ( is_numeric( $v ) && strval( intval( $v ) ) === $v ) {
  329. $v = intval( $v );
  330. }
  331. }
  332. // return.
  333. return $v;
  334. }
  335. /**
  336. * This function will load in a file from the 'admin/views' folder and allow variables to be passed through
  337. *
  338. * @date 28/09/13
  339. * @since 5.0.0
  340. *
  341. * @param string $view_path
  342. * @param array $view_args
  343. *
  344. * @return void
  345. */
  346. function acf_get_view( $view_path = '', $view_args = array() ) {
  347. // allow view file name shortcut
  348. if ( substr( $view_path, -4 ) !== '.php' ) {
  349. $view_path = acf_get_path( "includes/admin/views/{$view_path}.php" );
  350. }
  351. // include
  352. if ( file_exists( $view_path ) ) {
  353. // Use `EXTR_SKIP` here to prevent `$view_path` from being accidentally/maliciously overridden.
  354. extract( $view_args, EXTR_SKIP );
  355. include $view_path;
  356. }
  357. }
  358. /*
  359. * acf_merge_atts
  360. *
  361. * description
  362. *
  363. * @type function
  364. * @date 2/11/2014
  365. * @since 5.0.9
  366. *
  367. * @param $post_id (int)
  368. * @return $post_id (int)
  369. */
  370. function acf_merge_atts( $atts, $extra = array() ) {
  371. // bail early if no $extra
  372. if ( empty( $extra ) ) {
  373. return $atts;
  374. }
  375. // trim
  376. $extra = array_map( 'trim', $extra );
  377. $extra = array_filter( $extra );
  378. // merge in new atts
  379. foreach ( $extra as $k => $v ) {
  380. // append
  381. if ( $k == 'class' || $k == 'style' ) {
  382. $atts[ $k ] .= ' ' . $v;
  383. // merge
  384. } else {
  385. $atts[ $k ] = $v;
  386. }
  387. }
  388. // return
  389. return $atts;
  390. }
  391. /*
  392. * acf_nonce_input
  393. *
  394. * This function will create a basic nonce input
  395. *
  396. * @type function
  397. * @date 24/5/17
  398. * @since 5.6.0
  399. *
  400. * @param $post_id (int)
  401. * @return $post_id (int)
  402. */
  403. function acf_nonce_input( $nonce = '' ) {
  404. echo '<input type="hidden" name="_acf_nonce" value="' . wp_create_nonce( $nonce ) . '" />';
  405. }
  406. /*
  407. * acf_extract_var
  408. *
  409. * This function will remove the var from the array, and return the var
  410. *
  411. * @type function
  412. * @date 2/10/13
  413. * @since 5.0.0
  414. *
  415. * @param $array (array)
  416. * @param $key (string)
  417. * @return (mixed)
  418. */
  419. function acf_extract_var( &$array, $key, $default = null ) {
  420. // check if exists
  421. // - uses array_key_exists to extract NULL values (isset will fail)
  422. if ( is_array( $array ) && array_key_exists( $key, $array ) ) {
  423. // store value
  424. $v = $array[ $key ];
  425. // unset
  426. unset( $array[ $key ] );
  427. // return
  428. return $v;
  429. }
  430. // return
  431. return $default;
  432. }
  433. /*
  434. * acf_extract_vars
  435. *
  436. * This function will remove the vars from the array, and return the vars
  437. *
  438. * @type function
  439. * @date 8/10/13
  440. * @since 5.0.0
  441. *
  442. * @param $post_id (int)
  443. * @return $post_id (int)
  444. */
  445. function acf_extract_vars( &$array, $keys ) {
  446. $r = array();
  447. foreach ( $keys as $key ) {
  448. $r[ $key ] = acf_extract_var( $array, $key );
  449. }
  450. return $r;
  451. }
  452. /*
  453. * acf_get_sub_array
  454. *
  455. * This function will return a sub array of data
  456. *
  457. * @type function
  458. * @date 15/03/2016
  459. * @since 5.3.2
  460. *
  461. * @param $post_id (int)
  462. * @return $post_id (int)
  463. */
  464. function acf_get_sub_array( $array, $keys ) {
  465. $r = array();
  466. foreach ( $keys as $key ) {
  467. $r[ $key ] = $array[ $key ];
  468. }
  469. return $r;
  470. }
  471. /**
  472. * acf_get_post_types
  473. *
  474. * Returns an array of post type names.
  475. *
  476. * @date 7/10/13
  477. * @since 5.0.0
  478. *
  479. * @param array $args Optional. An array of key => value arguments to match against the post type objects. Default empty array.
  480. * @return array A list of post type names.
  481. */
  482. function acf_get_post_types( $args = array() ) {
  483. // vars
  484. $post_types = array();
  485. // extract special arg
  486. $exclude = acf_extract_var( $args, 'exclude', array() );
  487. $exclude[] = 'acf-field';
  488. $exclude[] = 'acf-field-group';
  489. // get post type objects
  490. $objects = get_post_types( $args, 'objects' );
  491. // loop
  492. foreach ( $objects as $i => $object ) {
  493. // bail early if is exclude
  494. if ( in_array( $i, $exclude ) ) {
  495. continue;
  496. }
  497. // bail early if is builtin (WP) private post type
  498. // - nav_menu_item, revision, customize_changeset, etc
  499. if ( $object->_builtin && ! $object->public ) {
  500. continue;
  501. }
  502. // append
  503. $post_types[] = $i;
  504. }
  505. // filter
  506. $post_types = apply_filters( 'acf/get_post_types', $post_types, $args );
  507. // return
  508. return $post_types;
  509. }
  510. function acf_get_pretty_post_types( $post_types = array() ) {
  511. // get post types
  512. if ( empty( $post_types ) ) {
  513. // get all custom post types
  514. $post_types = acf_get_post_types();
  515. }
  516. // get labels
  517. $ref = array();
  518. $r = array();
  519. foreach ( $post_types as $post_type ) {
  520. // vars
  521. $label = acf_get_post_type_label( $post_type );
  522. // append to r
  523. $r[ $post_type ] = $label;
  524. // increase counter
  525. if ( ! isset( $ref[ $label ] ) ) {
  526. $ref[ $label ] = 0;
  527. }
  528. $ref[ $label ]++;
  529. }
  530. // get slugs
  531. foreach ( array_keys( $r ) as $i ) {
  532. // vars
  533. $post_type = $r[ $i ];
  534. if ( $ref[ $post_type ] > 1 ) {
  535. $r[ $i ] .= ' (' . $i . ')';
  536. }
  537. }
  538. // return
  539. return $r;
  540. }
  541. /*
  542. * acf_get_post_type_label
  543. *
  544. * This function will return a pretty label for a specific post_type
  545. *
  546. * @type function
  547. * @date 5/07/2016
  548. * @since 5.4.0
  549. *
  550. * @param $post_type (string)
  551. * @return (string)
  552. */
  553. function acf_get_post_type_label( $post_type ) {
  554. // vars
  555. $label = $post_type;
  556. // check that object exists
  557. // - case exists when importing field group from another install and post type does not exist
  558. if ( post_type_exists( $post_type ) ) {
  559. $obj = get_post_type_object( $post_type );
  560. $label = $obj->labels->singular_name;
  561. }
  562. // return
  563. return $label;
  564. }
  565. /*
  566. * acf_verify_nonce
  567. *
  568. * This function will look at the $_POST['_acf_nonce'] value and return true or false
  569. *
  570. * @type function
  571. * @date 15/10/13
  572. * @since 5.0.0
  573. *
  574. * @param $nonce (string)
  575. * @return (boolean)
  576. */
  577. function acf_verify_nonce( $value ) {
  578. // vars
  579. $nonce = acf_maybe_get_POST( '_acf_nonce' );
  580. // bail early nonce does not match (post|user|comment|term)
  581. if ( ! $nonce || ! wp_verify_nonce( $nonce, $value ) ) {
  582. return false;
  583. }
  584. // reset nonce (only allow 1 save)
  585. $_POST['_acf_nonce'] = false;
  586. // return
  587. return true;
  588. }
  589. /*
  590. * acf_verify_ajax
  591. *
  592. * This function will return true if the current AJAX request is valid
  593. * It's action will also allow WPML to set the lang and avoid AJAX get_posts issues
  594. *
  595. * @type function
  596. * @date 7/08/2015
  597. * @since 5.2.3
  598. *
  599. * @param n/a
  600. * @return (boolean)
  601. */
  602. function acf_verify_ajax() {
  603. // bail early if not acf nonce
  604. if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['nonce'] ), 'acf_nonce' ) ) {
  605. return false;
  606. }
  607. // action for 3rd party customization
  608. do_action( 'acf/verify_ajax' );
  609. // return
  610. return true;
  611. }
  612. /*
  613. * acf_get_image_sizes
  614. *
  615. * This function will return an array of available image sizes
  616. *
  617. * @type function
  618. * @date 23/10/13
  619. * @since 5.0.0
  620. *
  621. * @param n/a
  622. * @return (array)
  623. */
  624. function acf_get_image_sizes() {
  625. // vars
  626. $sizes = array(
  627. 'thumbnail' => __( 'Thumbnail', 'acf' ),
  628. 'medium' => __( 'Medium', 'acf' ),
  629. 'large' => __( 'Large', 'acf' ),
  630. );
  631. // find all sizes
  632. $all_sizes = get_intermediate_image_sizes();
  633. // add extra registered sizes
  634. if ( ! empty( $all_sizes ) ) {
  635. foreach ( $all_sizes as $size ) {
  636. // bail early if already in array
  637. if ( isset( $sizes[ $size ] ) ) {
  638. continue;
  639. }
  640. // append to array
  641. $label = str_replace( '-', ' ', $size );
  642. $label = ucwords( $label );
  643. $sizes[ $size ] = $label;
  644. }
  645. }
  646. // add sizes
  647. foreach ( array_keys( $sizes ) as $s ) {
  648. // vars
  649. $data = acf_get_image_size( $s );
  650. // append
  651. if ( $data['width'] && $data['height'] ) {
  652. $sizes[ $s ] .= ' (' . $data['width'] . ' x ' . $data['height'] . ')';
  653. }
  654. }
  655. // add full end
  656. $sizes['full'] = __( 'Full Size', 'acf' );
  657. // filter for 3rd party customization
  658. $sizes = apply_filters( 'acf/get_image_sizes', $sizes );
  659. // return
  660. return $sizes;
  661. }
  662. function acf_get_image_size( $s = '' ) {
  663. // global
  664. global $_wp_additional_image_sizes;
  665. // rename for nicer code
  666. $_sizes = $_wp_additional_image_sizes;
  667. // vars
  668. $data = array(
  669. 'width' => isset( $_sizes[ $s ]['width'] ) ? $_sizes[ $s ]['width'] : get_option( "{$s}_size_w" ),
  670. 'height' => isset( $_sizes[ $s ]['height'] ) ? $_sizes[ $s ]['height'] : get_option( "{$s}_size_h" ),
  671. );
  672. // return
  673. return $data;
  674. }
  675. /**
  676. * acf_version_compare
  677. *
  678. * Similar to the version_compare() function but with extra functionality.
  679. *
  680. * @date 21/11/16
  681. * @since 5.5.0
  682. *
  683. * @param string $left The left version number.
  684. * @param string $compare The compare operator.
  685. * @param string $right The right version number.
  686. * @return bool
  687. */
  688. function acf_version_compare( $left = '', $compare = '>', $right = '' ) {
  689. // Detect 'wp' placeholder.
  690. if ( $left === 'wp' ) {
  691. global $wp_version;
  692. $left = $wp_version;
  693. }
  694. // Return result.
  695. return version_compare( $left, $right, $compare );
  696. }
  697. /*
  698. * acf_get_full_version
  699. *
  700. * This function will remove any '-beta1' or '-RC1' strings from a version
  701. *
  702. * @type function
  703. * @date 24/11/16
  704. * @since 5.5.0
  705. *
  706. * @param $version (string)
  707. * @return (string)
  708. */
  709. function acf_get_full_version( $version = '1' ) {
  710. // remove '-beta1' or '-RC1'
  711. if ( $pos = strpos( $version, '-' ) ) {
  712. $version = substr( $version, 0, $pos );
  713. }
  714. // return
  715. return $version;
  716. }
  717. /*
  718. * acf_get_terms
  719. *
  720. * This function is a wrapper for the get_terms() function
  721. *
  722. * @type function
  723. * @date 28/09/2016
  724. * @since 5.4.0
  725. *
  726. * @param $args (array)
  727. * @return (array)
  728. */
  729. function acf_get_terms( $args ) {
  730. // defaults
  731. $args = wp_parse_args(
  732. $args,
  733. array(
  734. 'taxonomy' => null,
  735. 'hide_empty' => false,
  736. 'update_term_meta_cache' => false,
  737. )
  738. );
  739. // parameters changed in version 4.5
  740. if ( acf_version_compare( 'wp', '<', '4.5' ) ) {
  741. return get_terms( $args['taxonomy'], $args );
  742. }
  743. // return
  744. return get_terms( $args );
  745. }
  746. /*
  747. * acf_get_taxonomy_terms
  748. *
  749. * This function will return an array of available taxonomy terms
  750. *
  751. * @type function
  752. * @date 7/10/13
  753. * @since 5.0.0
  754. *
  755. * @param $taxonomies (array)
  756. * @return (array)
  757. */
  758. function acf_get_taxonomy_terms( $taxonomies = array() ) {
  759. // force array
  760. $taxonomies = acf_get_array( $taxonomies );
  761. // get pretty taxonomy names
  762. $taxonomies = acf_get_pretty_taxonomies( $taxonomies );
  763. // vars
  764. $r = array();
  765. // populate $r
  766. foreach ( array_keys( $taxonomies ) as $taxonomy ) {
  767. // vars
  768. $label = $taxonomies[ $taxonomy ];
  769. $is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
  770. $terms = acf_get_terms(
  771. array(
  772. 'taxonomy' => $taxonomy,
  773. 'hide_empty' => false,
  774. )
  775. );
  776. // bail early i no terms
  777. if ( empty( $terms ) ) {
  778. continue;
  779. }
  780. // sort into hierachial order!
  781. if ( $is_hierarchical ) {
  782. $terms = _get_term_children( 0, $terms, $taxonomy );
  783. }
  784. // add placeholder
  785. $r[ $label ] = array();
  786. // add choices
  787. foreach ( $terms as $term ) {
  788. $k = "{$taxonomy}:{$term->slug}";
  789. $r[ $label ][ $k ] = acf_get_term_title( $term );
  790. }
  791. }
  792. // return
  793. return $r;
  794. }
  795. /*
  796. * acf_decode_taxonomy_terms
  797. *
  798. * This function decodes the $taxonomy:$term strings into a nested array
  799. *
  800. * @type function
  801. * @date 27/02/2014
  802. * @since 5.0.0
  803. *
  804. * @param $terms (array)
  805. * @return (array)
  806. */
  807. function acf_decode_taxonomy_terms( $strings = false ) {
  808. // bail early if no terms
  809. if ( empty( $strings ) ) {
  810. return false;
  811. }
  812. // vars
  813. $terms = array();
  814. // loop
  815. foreach ( $strings as $string ) {
  816. // vars
  817. $data = acf_decode_taxonomy_term( $string );
  818. $taxonomy = $data['taxonomy'];
  819. $term = $data['term'];
  820. // create empty array
  821. if ( ! isset( $terms[ $taxonomy ] ) ) {
  822. $terms[ $taxonomy ] = array();
  823. }
  824. // append
  825. $terms[ $taxonomy ][] = $term;
  826. }
  827. // return
  828. return $terms;
  829. }
  830. /*
  831. * acf_decode_taxonomy_term
  832. *
  833. * This function will return the taxonomy and term slug for a given value
  834. *
  835. * @type function
  836. * @date 31/03/2014
  837. * @since 5.0.0
  838. *
  839. * @param $string (string)
  840. * @return (array)
  841. */
  842. function acf_decode_taxonomy_term( $value ) {
  843. // vars
  844. $data = array(
  845. 'taxonomy' => '',
  846. 'term' => '',
  847. );
  848. // int
  849. if ( is_numeric( $value ) ) {
  850. $data['term'] = $value;
  851. // string
  852. } elseif ( is_string( $value ) ) {
  853. $value = explode( ':', $value );
  854. $data['taxonomy'] = isset( $value[0] ) ? $value[0] : '';
  855. $data['term'] = isset( $value[1] ) ? $value[1] : '';
  856. // error
  857. } else {
  858. return false;
  859. }
  860. // allow for term_id (Used by ACF v4)
  861. if ( is_numeric( $data['term'] ) ) {
  862. // global
  863. global $wpdb;
  864. // find taxonomy
  865. if ( ! $data['taxonomy'] ) {
  866. $data['taxonomy'] = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $data['term'] ) );
  867. }
  868. // find term (may have numeric slug '123')
  869. $term = get_term_by( 'slug', $data['term'], $data['taxonomy'] );
  870. // attempt get term via ID (ACF4 uses ID)
  871. if ( ! $term ) {
  872. $term = get_term( $data['term'], $data['taxonomy'] );
  873. }
  874. // bail early if no term
  875. if ( ! $term ) {
  876. return false;
  877. }
  878. // update
  879. $data['taxonomy'] = $term->taxonomy;
  880. $data['term'] = $term->slug;
  881. }
  882. // return
  883. return $data;
  884. }
  885. /**
  886. * acf_array
  887. *
  888. * Casts the value into an array.
  889. *
  890. * @date 9/1/19
  891. * @since 5.7.10
  892. *
  893. * @param mixed $val The value to cast.
  894. * @return array
  895. */
  896. function acf_array( $val = array() ) {
  897. return (array) $val;
  898. }
  899. /**
  900. * Returns a non-array value.
  901. *
  902. * @date 11/05/2020
  903. * @since 5.8.10
  904. *
  905. * @param mixed $val The value to review.
  906. * @return mixed
  907. */
  908. function acf_unarray( $val ) {
  909. if ( is_array( $val ) ) {
  910. return reset( $val );
  911. }
  912. return $val;
  913. }
  914. /*
  915. * acf_get_array
  916. *
  917. * This function will force a variable to become an array
  918. *
  919. * @type function
  920. * @date 4/02/2014
  921. * @since 5.0.0
  922. *
  923. * @param $var (mixed)
  924. * @return (array)
  925. */
  926. function acf_get_array( $var = false, $delimiter = '' ) {
  927. // array
  928. if ( is_array( $var ) ) {
  929. return $var;
  930. }
  931. // bail early if empty
  932. if ( acf_is_empty( $var ) ) {
  933. return array();
  934. }
  935. // string
  936. if ( is_string( $var ) && $delimiter ) {
  937. return explode( $delimiter, $var );
  938. }
  939. // place in array
  940. return (array) $var;
  941. }
  942. /*
  943. * acf_get_numeric
  944. *
  945. * This function will return numeric values
  946. *
  947. * @type function
  948. * @date 15/07/2016
  949. * @since 5.4.0
  950. *
  951. * @param $value (mixed)
  952. * @return (mixed)
  953. */
  954. function acf_get_numeric( $value = '' ) {
  955. // vars
  956. $numbers = array();
  957. $is_array = is_array( $value );
  958. // loop
  959. foreach ( (array) $value as $v ) {
  960. if ( is_numeric( $v ) ) {
  961. $numbers[] = (int) $v;
  962. }
  963. }
  964. // bail early if is empty
  965. if ( empty( $numbers ) ) {
  966. return false;
  967. }
  968. // convert array
  969. if ( ! $is_array ) {
  970. $numbers = $numbers[0];
  971. }
  972. // return
  973. return $numbers;
  974. }
  975. /**
  976. * acf_get_posts
  977. *
  978. * Similar to the get_posts() function but with extra functionality.
  979. *
  980. * @date 3/03/15
  981. * @since 5.1.5
  982. *
  983. * @param array $args The query args.
  984. * @return array
  985. */
  986. function acf_get_posts( $args = array() ) {
  987. // Vars.
  988. $posts = array();
  989. // Apply default args.
  990. $args = wp_parse_args(
  991. $args,
  992. array(
  993. 'posts_per_page' => -1,
  994. 'post_type' => '',
  995. 'post_status' => 'any',
  996. 'update_post_meta_cache' => false,
  997. 'update_post_term_cache' => false,
  998. )
  999. );
  1000. // Avoid default 'post' post_type by providing all public types.
  1001. if ( ! $args['post_type'] ) {
  1002. $args['post_type'] = acf_get_post_types();
  1003. }
  1004. // Check if specifc post ID's have been provided.
  1005. if ( $args['post__in'] ) {
  1006. // Clean value into an array of IDs.
  1007. $args['post__in'] = array_map( 'intval', acf_array( $args['post__in'] ) );
  1008. }
  1009. // Query posts.
  1010. $posts = get_posts( $args );
  1011. // Remove any potential empty results.
  1012. $posts = array_filter( $posts );
  1013. // Manually order results.
  1014. if ( $posts && $args['post__in'] ) {
  1015. $order = array();
  1016. foreach ( $posts as $i => $post ) {
  1017. $order[ $i ] = array_search( $post->ID, $args['post__in'] );
  1018. }
  1019. array_multisort( $order, $posts );
  1020. }
  1021. // Return posts.
  1022. return $posts;
  1023. }
  1024. /*
  1025. * _acf_query_remove_post_type
  1026. *
  1027. * This function will remove the 'wp_posts.post_type' WHERE clause completely
  1028. * When using 'post__in', this clause is unneccessary and slow.
  1029. *
  1030. * @type function
  1031. * @date 4/03/2015
  1032. * @since 5.1.5
  1033. *
  1034. * @param $sql (string)
  1035. * @return $sql
  1036. */
  1037. function _acf_query_remove_post_type( $sql ) {
  1038. // global
  1039. global $wpdb;
  1040. // bail early if no 'wp_posts.ID IN'
  1041. if ( strpos( $sql, "$wpdb->posts.ID IN" ) === false ) {
  1042. return $sql;
  1043. }
  1044. // get bits
  1045. $glue = 'AND';
  1046. $bits = explode( $glue, $sql );
  1047. // loop through $where and remove any post_type queries
  1048. foreach ( $bits as $i => $bit ) {
  1049. if ( strpos( $bit, "$wpdb->posts.post_type" ) !== false ) {
  1050. unset( $bits[ $i ] );
  1051. }
  1052. }
  1053. // join $where back together
  1054. $sql = implode( $glue, $bits );
  1055. // return
  1056. return $sql;
  1057. }
  1058. /*
  1059. * acf_get_grouped_posts
  1060. *
  1061. * This function will return all posts grouped by post_type
  1062. * This is handy for select settings
  1063. *
  1064. * @type function
  1065. * @date 27/02/2014
  1066. * @since 5.0.0
  1067. *
  1068. * @param $args (array)
  1069. * @return (array)
  1070. */
  1071. function acf_get_grouped_posts( $args ) {
  1072. // vars
  1073. $data = array();
  1074. // defaults
  1075. $args = wp_parse_args(
  1076. $args,
  1077. array(
  1078. 'posts_per_page' => -1,
  1079. 'paged' => 0,
  1080. 'post_type' => 'post',
  1081. 'orderby' => 'menu_order title',
  1082. 'order' => 'ASC',
  1083. 'post_status' => 'any',
  1084. 'suppress_filters' => false,
  1085. 'update_post_meta_cache' => false,
  1086. )
  1087. );
  1088. // find array of post_type
  1089. $post_types = acf_get_array( $args['post_type'] );
  1090. $post_types_labels = acf_get_pretty_post_types( $post_types );
  1091. $is_single_post_type = ( count( $post_types ) == 1 );
  1092. // attachment doesn't work if it is the only item in an array
  1093. if ( $is_single_post_type ) {
  1094. $args['post_type'] = reset( $post_types );
  1095. }
  1096. // add filter to orderby post type
  1097. if ( ! $is_single_post_type ) {
  1098. add_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
  1099. }
  1100. // get posts
  1101. $posts = get_posts( $args );
  1102. // remove this filter (only once)
  1103. if ( ! $is_single_post_type ) {
  1104. remove_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
  1105. }
  1106. // loop
  1107. foreach ( $post_types as $post_type ) {
  1108. // vars
  1109. $this_posts = array();
  1110. $this_group = array();
  1111. // populate $this_posts
  1112. foreach ( $posts as $post ) {
  1113. if ( $post->post_type == $post_type ) {
  1114. $this_posts[] = $post;
  1115. }
  1116. }
  1117. // bail early if no posts for this post type
  1118. if ( empty( $this_posts ) ) {
  1119. continue;
  1120. }
  1121. // sort into hierachial order!
  1122. // this will fail if a search has taken place because parents wont exist
  1123. if ( is_post_type_hierarchical( $post_type ) && empty( $args['s'] ) ) {
  1124. // vars
  1125. $post_id = $this_posts[0]->ID;
  1126. $parent_id = acf_maybe_get( $args, 'post_parent', 0 );
  1127. $offset = 0;
  1128. $length = count( $this_posts );
  1129. // get all posts from this post type
  1130. $all_posts = get_posts(
  1131. array_merge(
  1132. $args,
  1133. array(
  1134. 'posts_per_page' => -1,
  1135. 'paged' => 0,
  1136. 'post_type' => $post_type,
  1137. )
  1138. )
  1139. );
  1140. // find starting point (offset)
  1141. foreach ( $all_posts as $i => $post ) {
  1142. if ( $post->ID == $post_id ) {
  1143. $offset = $i;
  1144. break;
  1145. }
  1146. }
  1147. // order posts
  1148. $ordered_posts = get_page_children( $parent_id, $all_posts );
  1149. // compare aray lengths
  1150. // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
  1151. // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
  1152. if ( count( $ordered_posts ) == count( $all_posts ) ) {
  1153. $this_posts = array_slice( $ordered_posts, $offset, $length );
  1154. }
  1155. }
  1156. // populate $this_posts
  1157. foreach ( $this_posts as $post ) {
  1158. $this_group[ $post->ID ] = $post;
  1159. }
  1160. // group by post type
  1161. $label = $post_types_labels[ $post_type ];
  1162. $data[ $label ] = $this_group;
  1163. }
  1164. // return
  1165. return $data;
  1166. }
  1167. function _acf_orderby_post_type( $ordeby, $wp_query ) {
  1168. // global
  1169. global $wpdb;
  1170. // get post types
  1171. $post_types = $wp_query->get( 'post_type' );
  1172. // prepend SQL
  1173. if ( is_array( $post_types ) ) {
  1174. $post_types = implode( "','", $post_types );
  1175. $ordeby = "FIELD({$wpdb->posts}.post_type,'$post_types')," . $ordeby;
  1176. }
  1177. // return
  1178. return $ordeby;
  1179. }
  1180. function acf_get_post_title( $post = 0, $is_search = false ) {
  1181. // vars
  1182. $post = get_post( $post );
  1183. $title = '';
  1184. $prepend = '';
  1185. $append = '';
  1186. // bail early if no post
  1187. if ( ! $post ) {
  1188. return '';
  1189. }
  1190. // title
  1191. $title = get_the_title( $post->ID );
  1192. // empty
  1193. if ( $title === '' ) {
  1194. $title = __( '(no title)', 'acf' );
  1195. }
  1196. // status
  1197. if ( get_post_status( $post->ID ) != 'publish' ) {
  1198. $append .= ' (' . get_post_status( $post->ID ) . ')';
  1199. }
  1200. // ancestors
  1201. if ( $post->post_type !== 'attachment' ) {
  1202. // get ancestors
  1203. $ancestors = get_ancestors( $post->ID, $post->post_type );
  1204. $prepend .= str_repeat( '- ', count( $ancestors ) );
  1205. // add parent
  1206. /*
  1207. removed in 5.6.5 as not used by the UI
  1208. if( $is_search && !empty($ancestors) ) {
  1209. // reverse
  1210. $ancestors = array_reverse($ancestors);
  1211. // convert id's into titles
  1212. foreach( $ancestors as $i => $id ) {
  1213. $ancestors[ $i ] = get_the_title( $id );
  1214. }
  1215. // append
  1216. $append .= ' | ' . __('Parent', 'acf') . ': ' . implode(' / ', $ancestors);
  1217. }
  1218. */
  1219. }
  1220. // merge
  1221. $title = $prepend . $title . $append;
  1222. // return
  1223. return $title;
  1224. }
  1225. function acf_order_by_search( $array, $search ) {
  1226. // vars
  1227. $weights = array();
  1228. $needle = strtolower( $search );
  1229. // add key prefix
  1230. foreach ( array_keys( $array ) as $k ) {
  1231. $array[ '_' . $k ] = acf_extract_var( $array, $k );
  1232. }
  1233. // add search weight
  1234. foreach ( $array as $k => $v ) {
  1235. // vars
  1236. $weight = 0;
  1237. $haystack = strtolower( $v );
  1238. $strpos = strpos( $haystack, $needle );
  1239. // detect search match
  1240. if ( $strpos !== false ) {
  1241. // set eright to length of match
  1242. $weight = strlen( $search );
  1243. // increase weight if match starts at begining of string
  1244. if ( $strpos == 0 ) {
  1245. $weight++;
  1246. }
  1247. }
  1248. // append to wights
  1249. $weights[ $k ] = $weight;
  1250. }
  1251. // sort the array with menu_order ascending
  1252. array_multisort( $weights, SORT_DESC, $array );
  1253. // remove key prefix
  1254. foreach ( array_keys( $array ) as $k ) {
  1255. $array[ substr( $k, 1 ) ] = acf_extract_var( $array, $k );
  1256. }
  1257. // return
  1258. return $array;
  1259. }
  1260. /*
  1261. * acf_get_pretty_user_roles
  1262. *
  1263. * description
  1264. *
  1265. * @type function
  1266. * @date 23/02/2016
  1267. * @since 5.3.2
  1268. *
  1269. * @param $post_id (int)
  1270. * @return $post_id (int)
  1271. */
  1272. function acf_get_pretty_user_roles( $allowed = false ) {
  1273. // vars
  1274. $editable_roles = get_editable_roles();
  1275. $allowed = acf_get_array( $allowed );
  1276. $roles = array();
  1277. // loop
  1278. foreach ( $editable_roles as $role_name => $role_details ) {
  1279. // bail early if not allowed
  1280. if ( ! empty( $allowed ) && ! in_array( $role_name, $allowed ) ) {
  1281. continue;
  1282. }
  1283. // append
  1284. $roles[ $role_name ] = translate_user_role( $role_details['name'] );
  1285. }
  1286. // return
  1287. return $roles;
  1288. }
  1289. /*
  1290. * acf_get_grouped_users
  1291. *
  1292. * This function will return all users grouped by role
  1293. * This is handy for select settings
  1294. *
  1295. * @type function
  1296. * @date 27/02/2014
  1297. * @since 5.0.0
  1298. *
  1299. * @param $args (array)
  1300. * @return (array)
  1301. */
  1302. function acf_get_grouped_users( $args = array() ) {
  1303. // vars
  1304. $r = array();
  1305. // defaults
  1306. $args = wp_parse_args(
  1307. $args,
  1308. array(
  1309. 'users_per_page' => -1,
  1310. 'paged' => 0,
  1311. 'role' => '',
  1312. 'orderby' => 'login',
  1313. 'order' => 'ASC',
  1314. )
  1315. );
  1316. // offset
  1317. $i = 0;
  1318. $min = 0;
  1319. $max = 0;
  1320. $users_per_page = acf_extract_var( $args, 'users_per_page' );
  1321. $paged = acf_extract_var( $args, 'paged' );
  1322. if ( $users_per_page > 0 ) {
  1323. // prevent paged from being -1
  1324. $paged = max( 0, $paged );
  1325. // set min / max
  1326. $min = ( ( $paged - 1 ) * $users_per_page ) + 1; // 1, 11
  1327. $max = ( $paged * $users_per_page ); // 10, 20
  1328. }
  1329. // find array of post_type
  1330. $user_roles = acf_get_pretty_user_roles( $args['role'] );
  1331. // fix role
  1332. if ( is_array( $args['role'] ) ) {
  1333. // global
  1334. global $wp_version, $wpdb;
  1335. // vars
  1336. $roles = acf_extract_var( $args, 'role' );
  1337. // new WP has role__in
  1338. if ( version_compare( $wp_version, '4.4', '>=' ) ) {
  1339. $args['role__in'] = $roles;
  1340. // old WP doesn't have role__in
  1341. } else {
  1342. // vars
  1343. $blog_id = get_current_blog_id();
  1344. $meta_query = array( 'relation' => 'OR' );
  1345. // loop
  1346. foreach ( $roles as $role ) {
  1347. $meta_query[] = array(
  1348. 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
  1349. 'value' => '"' . $role . '"',
  1350. 'compare' => 'LIKE',
  1351. );
  1352. }
  1353. // append
  1354. $args['meta_query'] = $meta_query;
  1355. }
  1356. }
  1357. // get posts
  1358. $users = get_users( $args );
  1359. // loop
  1360. foreach ( $user_roles as $user_role_name => $user_role_label ) {
  1361. // vars
  1362. $this_users = array();
  1363. $this_group = array();
  1364. // populate $this_posts
  1365. foreach ( array_keys( $users ) as $key ) {
  1366. // bail early if not correct role
  1367. if ( ! in_array( $user_role_name, $users[ $key ]->roles ) ) {
  1368. continue;
  1369. }
  1370. // extract user
  1371. $user = acf_extract_var( $users, $key );
  1372. // increase
  1373. $i++;
  1374. // bail early if too low
  1375. if ( $min && $i < $min ) {
  1376. continue;
  1377. }
  1378. // bail early if too high (don't bother looking at any more users)
  1379. if ( $max && $i > $max ) {
  1380. break;
  1381. }
  1382. // group by post type
  1383. $this_users[ $user->ID ] = $user;
  1384. }
  1385. // bail early if no posts for this post type
  1386. if ( empty( $this_users ) ) {
  1387. continue;
  1388. }
  1389. // append
  1390. $r[ $user_role_label ] = $this_users;
  1391. }
  1392. // return
  1393. return $r;
  1394. }
  1395. /**
  1396. * acf_json_encode
  1397. *
  1398. * Returns json_encode() ready for file / database use.
  1399. *
  1400. * @date 29/4/19
  1401. * @since 5.0.0
  1402. *
  1403. * @param array $json The array of data to encode.
  1404. * @return string
  1405. */
  1406. function acf_json_encode( $json ) {
  1407. return json_encode( $json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
  1408. }
  1409. /*
  1410. * acf_str_exists
  1411. *
  1412. * This function will return true if a sub string is found
  1413. *
  1414. * @type function
  1415. * @date 1/05/2014
  1416. * @since 5.0.0
  1417. *
  1418. * @param $needle (string)
  1419. * @param $haystack (string)
  1420. * @return (boolean)
  1421. */
  1422. function acf_str_exists( $needle, $haystack ) {
  1423. // return true if $haystack contains the $needle
  1424. if ( is_string( $haystack ) && strpos( $haystack, $needle ) !== false ) {
  1425. return true;
  1426. }
  1427. // return
  1428. return false;
  1429. }
  1430. /*
  1431. * acf_debug
  1432. *
  1433. * description
  1434. *
  1435. * @type function
  1436. * @date 2/05/2014
  1437. * @since 5.0.0
  1438. *
  1439. * @param $post_id (int)
  1440. * @return $post_id (int)
  1441. */
  1442. function acf_debug() {
  1443. // vars
  1444. $args = func_get_args();
  1445. $s = array_shift( $args );
  1446. $o = '';
  1447. $nl = "\r\n";
  1448. // start script
  1449. $o .= '<script type="text/javascript">' . $nl;
  1450. $o .= 'console.log("' . $s . '"';
  1451. if ( ! empty( $args ) ) {
  1452. foreach ( $args as $arg ) {
  1453. if ( is_object( $arg ) || is_array( $arg ) ) {
  1454. $arg = json_encode( $arg );
  1455. } elseif ( is_bool( $arg ) ) {
  1456. $arg = $arg ? 'true' : 'false';
  1457. } elseif ( is_string( $arg ) ) {
  1458. $arg = '"' . $arg . '"';
  1459. }
  1460. $o .= ', ' . $arg;
  1461. }
  1462. }
  1463. $o .= ');' . $nl;
  1464. // end script
  1465. $o .= '</script>' . $nl;
  1466. // echo
  1467. echo $o;
  1468. }
  1469. function acf_debug_start() {
  1470. acf_update_setting( 'debug_start', memory_get_usage() );
  1471. }
  1472. function acf_debug_end() {
  1473. $start = acf_get_setting( 'debug_start' );
  1474. $end = memory_get_usage();
  1475. return $end - $start;
  1476. }
  1477. /*
  1478. * acf_encode_choices
  1479. *
  1480. * description
  1481. *
  1482. * @type function
  1483. * @date 4/06/2014
  1484. * @since 5.0.0
  1485. *
  1486. * @param $post_id (int)
  1487. * @return $post_id (int)
  1488. */
  1489. function acf_encode_choices( $array = array(), $show_keys = true ) {
  1490. // bail early if not array (maybe a single string)
  1491. if ( ! is_array( $array ) ) {
  1492. return $array;
  1493. }
  1494. // bail early if empty array
  1495. if ( empty( $array ) ) {
  1496. return '';
  1497. }
  1498. // vars
  1499. $string = '';
  1500. // if allowed to show keys (good for choices, not for default values)
  1501. if ( $show_keys ) {
  1502. // loop
  1503. foreach ( $array as $k => $v ) {
  1504. // ignore if key and value are the same
  1505. if ( strval( $k ) == strval( $v ) ) {
  1506. continue;
  1507. }
  1508. // show key in the value
  1509. $array[ $k ] = $k . ' : ' . $v;
  1510. }
  1511. }
  1512. // implode
  1513. $string = implode( "\n", $array );
  1514. // return
  1515. return $string;
  1516. }
  1517. function acf_decode_choices( $string = '', $array_keys = false ) {
  1518. // bail early if already array
  1519. if ( is_array( $string ) ) {
  1520. return $string;
  1521. // allow numeric values (same as string)
  1522. } elseif ( is_numeric( $string ) ) {
  1523. // do nothing
  1524. // bail early if not a string
  1525. } elseif ( ! is_string( $string ) ) {
  1526. return array();
  1527. // bail early if is empty string
  1528. } elseif ( $string === '' ) {
  1529. return array();
  1530. }
  1531. // vars
  1532. $array = array();
  1533. // explode
  1534. $lines = explode( "\n", $string );
  1535. // key => value
  1536. foreach ( $lines as $line ) {
  1537. // vars
  1538. $k = trim( $line );
  1539. $v = trim( $line );
  1540. // look for ' : '
  1541. if ( acf_str_exists( ' : ', $line ) ) {
  1542. $line = explode( ' : ', $line );
  1543. $k = trim( $line[0] );
  1544. $v = trim( $line[1] );
  1545. }
  1546. // append
  1547. $array[ $k ] = $v;
  1548. }
  1549. // return only array keys? (good for checkbox default_value)
  1550. if ( $array_keys ) {
  1551. return array_keys( $array );
  1552. }
  1553. // return
  1554. return $array;
  1555. }
  1556. /*
  1557. * acf_str_replace
  1558. *
  1559. * This function will replace an array of strings much like str_replace
  1560. * The difference is the extra logic to avoid replacing a string that has alread been replaced
  1561. * This is very useful for replacing date characters as they overlap with eachother
  1562. *
  1563. * @type function
  1564. * @date 21/06/2016
  1565. * @since 5.3.8
  1566. *
  1567. * @param $post_id (int)
  1568. * @return $post_id (int)
  1569. */
  1570. function acf_str_replace( $string = '', $search_replace = array() ) {
  1571. // vars
  1572. $ignore = array();
  1573. // remove potential empty search to avoid PHP error
  1574. unset( $search_replace[''] );
  1575. // loop over conversions
  1576. foreach ( $search_replace as $search => $replace ) {
  1577. // ignore this search, it was a previous replace
  1578. if ( in_array( $search, $ignore ) ) {
  1579. continue;
  1580. }
  1581. // bail early if subsctring not found
  1582. if ( strpos( $string, $search ) === false ) {
  1583. continue;
  1584. }
  1585. // replace
  1586. $string = str_replace( $search, $replace, $string );
  1587. // append to ignore
  1588. $ignore[] = $replace;
  1589. }
  1590. // return
  1591. return $string;
  1592. }
  1593. /*
  1594. * date & time formats
  1595. *
  1596. * These settings contain an association of format strings from PHP => JS
  1597. *
  1598. * @type function
  1599. * @date 21/06/2016
  1600. * @since 5.3.8
  1601. *
  1602. * @param n/a
  1603. * @return n/a
  1604. */
  1605. acf_update_setting(
  1606. 'php_to_js_date_formats',
  1607. array(
  1608. // Year
  1609. 'Y' => 'yy', // Numeric, 4 digits 1999, 2003
  1610. 'y' => 'y', // Numeric, 2 digits 99, 03
  1611. // Month
  1612. 'm' => 'mm', // Numeric, with leading zeros 01–12
  1613. 'n' => 'm', // Numeric, without leading zeros 1–12
  1614. 'F' => 'MM', // Textual full January – December
  1615. 'M' => 'M', // Textual three letters Jan - Dec
  1616. // Weekday
  1617. 'l' => 'DD', // Full name (lowercase 'L') Sunday – Saturday
  1618. 'D' => 'D', // Three letter name Mon – Sun
  1619. // Day of Month
  1620. 'd' => 'dd', // Numeric, with leading zeros 01–31
  1621. 'j' => 'd', // Numeric, without leading zeros 1–31
  1622. 'S' => '', // The English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
  1623. )
  1624. );
  1625. acf_update_setting(
  1626. 'php_to_js_time_formats',
  1627. array(
  1628. 'a' => 'tt', // Lowercase Ante meridiem and Post meridiem am or pm
  1629. 'A' => 'TT', // Uppercase Ante meridiem and Post meridiem AM or PM
  1630. 'h' => 'hh', // 12-hour format of an hour with leading zeros 01 through 12
  1631. 'g' => 'h', // 12-hour format of an hour without leading zeros 1 through 12
  1632. 'H' => 'HH', // 24-hour format of an hour with leading zeros 00 through 23
  1633. 'G' => 'H', // 24-hour format of an hour without leading zeros 0 through 23
  1634. 'i' => 'mm', // Minutes with leading zeros 00 to 59
  1635. 's' => 'ss', // Seconds, with leading zeros 00 through 59
  1636. )
  1637. );
  1638. /*
  1639. * acf_split_date_time
  1640. *
  1641. * This function will split a format string into seperate date and time
  1642. *
  1643. * @type function
  1644. * @date 26/05/2016
  1645. * @since 5.3.8
  1646. *
  1647. * @param $date_time (string)
  1648. * @return $formats (array)
  1649. */
  1650. function acf_split_date_time( $date_time = '' ) {
  1651. // vars
  1652. $php_date = acf_get_setting( 'php_to_js_date_formats' );
  1653. $php_time = acf_get_setting( 'php_to_js_time_formats' );
  1654. $chars = str_split( $date_time );
  1655. $type = 'date';
  1656. // default
  1657. $data = array(
  1658. 'date' => '',
  1659. 'time' => '',
  1660. );
  1661. // loop
  1662. foreach ( $chars as $i => $c ) {
  1663. // find type
  1664. // - allow misc characters to append to previous type
  1665. if ( isset( $php_date[ $c ] ) ) {
  1666. $type = 'date';
  1667. } elseif ( isset( $php_time[ $c ] ) ) {
  1668. $type = 'time';
  1669. }
  1670. // append char
  1671. $data[ $type ] .= $c;
  1672. }
  1673. // trim
  1674. $data['date'] = trim( $data['date'] );
  1675. $data['time'] = trim( $data['time'] );
  1676. // return
  1677. return $data;
  1678. }
  1679. /*
  1680. * acf_convert_date_to_php
  1681. *
  1682. * This fucntion converts a date format string from JS to PHP
  1683. *
  1684. * @type function
  1685. * @date 20/06/2014
  1686. * @since 5.0.0
  1687. *
  1688. * @param $date (string)
  1689. * @return (string)
  1690. */
  1691. function acf_convert_date_to_php( $date = '' ) {
  1692. // vars
  1693. $php_to_js = acf_get_setting( 'php_to_js_date_formats' );
  1694. $js_to_php = array_flip( $php_to_js );
  1695. // return
  1696. return acf_str_replace( $date, $js_to_php );
  1697. }
  1698. /*
  1699. * acf_convert_date_to_js
  1700. *
  1701. * This fucntion converts a date format string from PHP to JS
  1702. *
  1703. * @type function
  1704. * @date 20/06/2014
  1705. * @since 5.0.0
  1706. *
  1707. * @param $date (string)
  1708. * @return (string)
  1709. */
  1710. function acf_convert_date_to_js( $date = '' ) {
  1711. // vars
  1712. $php_to_js = acf_get_setting( 'php_to_js_date_formats' );
  1713. // return
  1714. return acf_str_replace( $date, $php_to_js );
  1715. }
  1716. /*
  1717. * acf_convert_time_to_php
  1718. *
  1719. * This fucntion converts a time format string from JS to PHP
  1720. *
  1721. * @type function
  1722. * @date 20/06/2014
  1723. * @since 5.0.0
  1724. *
  1725. * @param $time (string)
  1726. * @return (string)
  1727. */
  1728. function acf_convert_time_to_php( $time = '' ) {
  1729. // vars
  1730. $php_to_js = acf_get_setting( 'php_to_js_time_formats' );
  1731. $js_to_php = array_flip( $php_to_js );
  1732. // return
  1733. return acf_str_replace( $time, $js_to_php );
  1734. }
  1735. /*
  1736. * acf_convert_time_to_js
  1737. *
  1738. * This fucntion converts a date format string from PHP to JS
  1739. *
  1740. * @type function
  1741. * @date 20/06/2014
  1742. * @since 5.0.0
  1743. *
  1744. * @param $time (string)
  1745. * @return (string)
  1746. */
  1747. function acf_convert_time_to_js( $time = '' ) {
  1748. // vars
  1749. $php_to_js = acf_get_setting( 'php_to_js_time_formats' );
  1750. // return
  1751. return acf_str_replace( $time, $php_to_js );
  1752. }
  1753. /*
  1754. * acf_update_user_setting
  1755. *
  1756. * description
  1757. *
  1758. * @type function
  1759. * @date 15/07/2014
  1760. * @since 5.0.0
  1761. *
  1762. * @param $post_id (int)
  1763. * @return $post_id (int)
  1764. */
  1765. function acf_update_user_setting( $name, $value ) {
  1766. // get current user id
  1767. $user_id = get_current_user_id();
  1768. // get user settings
  1769. $settings = get_user_meta( $user_id, 'acf_user_settings', true );
  1770. // ensure array
  1771. $settings = acf_get_array( $settings );
  1772. // delete setting (allow 0 to save)
  1773. if ( acf_is_empty( $value ) ) {
  1774. unset( $settings[ $name ] );
  1775. // append setting
  1776. } else {
  1777. $settings[ $name ] = $value;
  1778. }
  1779. // update user data
  1780. return update_metadata( 'user', $user_id, 'acf_user_settings', $settings );
  1781. }
  1782. /*
  1783. * acf_get_user_setting
  1784. *
  1785. * description
  1786. *
  1787. * @type function
  1788. * @date 15/07/2014
  1789. * @since 5.0.0
  1790. *
  1791. * @param $post_id (int)
  1792. * @return $post_id (int)
  1793. */
  1794. function acf_get_user_setting( $name = '', $default = false ) {
  1795. // get current user id
  1796. $user_id = get_current_user_id();
  1797. // get user settings
  1798. $settings = get_user_meta( $user_id, 'acf_user_settings', true );
  1799. // ensure array
  1800. $settings = acf_get_array( $settings );
  1801. // bail arly if no settings
  1802. if ( ! isset( $settings[ $name ] ) ) {
  1803. return $default;
  1804. }
  1805. // return
  1806. return $settings[ $name ];
  1807. }
  1808. /*
  1809. * acf_in_array
  1810. *
  1811. * description
  1812. *
  1813. * @type function
  1814. * @date 22/07/2014
  1815. * @since 5.0.0
  1816. *
  1817. * @param $post_id (int)
  1818. * @return $post_id (int)
  1819. */
  1820. function acf_in_array( $value = '', $array = false ) {
  1821. // bail early if not array
  1822. if ( ! is_array( $array ) ) {
  1823. return false;
  1824. }
  1825. // find value in array
  1826. return in_array( $value, $array );
  1827. }
  1828. /*
  1829. * acf_get_valid_post_id
  1830. *
  1831. * This function will return a valid post_id based on the current screen / parameter
  1832. *
  1833. * @type function
  1834. * @date 8/12/2013
  1835. * @since 5.0.0
  1836. *
  1837. * @param $post_id (mixed)
  1838. * @return $post_id (mixed)
  1839. */
  1840. function acf_get_valid_post_id( $post_id = 0 ) {
  1841. // allow filter to short-circuit load_value logic
  1842. $preload = apply_filters( 'acf/pre_load_post_id', null, $post_id );
  1843. if ( $preload !== null ) {
  1844. return $preload;
  1845. }
  1846. // vars
  1847. $_post_id = $post_id;
  1848. // if not $post_id, load queried object
  1849. if ( ! $post_id ) {
  1850. // try for global post (needed for setup_postdata)
  1851. $post_id = (int) get_the_ID();
  1852. // try for current screen
  1853. if ( ! $post_id ) {
  1854. $post_id = get_queried_object();
  1855. }
  1856. }
  1857. // $post_id may be an object.
  1858. // todo: Compare class types instead.
  1859. if ( is_object( $post_id ) ) {
  1860. // post
  1861. if ( isset( $post_id->post_type, $post_id->ID ) ) {
  1862. $post_id = $post_id->ID;
  1863. // user
  1864. } elseif ( isset( $post_id->roles, $post_id->ID ) ) {
  1865. $post_id = 'user_' . $post_id->ID;
  1866. // term
  1867. } elseif ( isset( $post_id->taxonomy, $post_id->term_id ) ) {
  1868. $post_id = 'term_' . $post_id->term_id;
  1869. // comment
  1870. } elseif ( isset( $post_id->comment_ID ) ) {
  1871. $post_id = 'comment_' . $post_id->comment_ID;
  1872. // default
  1873. } else {
  1874. $post_id = 0;
  1875. }
  1876. }
  1877. // allow for option == options
  1878. if ( $post_id === 'option' ) {
  1879. $post_id = 'options';
  1880. }
  1881. // append language code
  1882. if ( $post_id == 'options' ) {
  1883. $dl = acf_get_setting( 'default_language' );
  1884. $cl = acf_get_setting( 'current_language' );
  1885. if ( $cl && $cl !== $dl ) {
  1886. $post_id .= '_' . $cl;
  1887. }
  1888. }
  1889. // filter for 3rd party
  1890. $post_id = apply_filters( 'acf/validate_post_id', $post_id, $_post_id );
  1891. // return
  1892. return $post_id;
  1893. }
  1894. /*
  1895. * acf_get_post_id_info
  1896. *
  1897. * This function will return the type and id for a given $post_id string
  1898. *
  1899. * @type function
  1900. * @date 2/07/2016
  1901. * @since 5.4.0
  1902. *
  1903. * @param $post_id (mixed)
  1904. * @return $info (array)
  1905. */
  1906. function acf_get_post_id_info( $post_id = 0 ) {
  1907. // vars
  1908. $info = array(
  1909. 'type' => 'post',
  1910. 'id' => 0,
  1911. );
  1912. // bail early if no $post_id
  1913. if ( ! $post_id ) {
  1914. return $info;
  1915. }
  1916. // check cache
  1917. // - this function will most likely be called multiple times (saving loading fields from post)
  1918. // $cache_key = "get_post_id_info/post_id={$post_id}";
  1919. // if( acf_isset_cache($cache_key) ) return acf_get_cache($cache_key);
  1920. // numeric
  1921. if ( is_numeric( $post_id ) ) {
  1922. $info['id'] = (int) $post_id;
  1923. // string
  1924. } elseif ( is_string( $post_id ) ) {
  1925. // vars
  1926. $glue = '_';
  1927. $type = explode( $glue, $post_id );
  1928. $id = array_pop( $type );
  1929. $type = implode( $glue, $type );
  1930. $meta = array( 'post', 'user', 'comment', 'term' );
  1931. // check if is taxonomy (ACF < 5.5)
  1932. // - avoid scenario where taxonomy exists with name of meta type
  1933. if ( ! in_array( $type, $meta ) && acf_isset_termmeta( $type ) ) {
  1934. $type = 'term';
  1935. }
  1936. // meta
  1937. if ( is_numeric( $id ) && in_array( $type, $meta ) ) {
  1938. $info['type'] = $type;
  1939. $info['id'] = (int) $id;
  1940. // option
  1941. } else {
  1942. $info['type'] = 'option';
  1943. $info['id'] = $post_id;
  1944. }
  1945. }
  1946. // update cache
  1947. // acf_set_cache($cache_key, $info);
  1948. // filter
  1949. $info = apply_filters( 'acf/get_post_id_info', $info, $post_id );
  1950. // return
  1951. return $info;
  1952. }
  1953. /*
  1954. acf_log( acf_get_post_id_info(4) );
  1955. acf_log( acf_get_post_id_info('post_4') );
  1956. acf_log( acf_get_post_id_info('user_123') );
  1957. acf_log( acf_get_post_id_info('term_567') );
  1958. acf_log( acf_get_post_id_info('category_204') );
  1959. acf_log( acf_get_post_id_info('comment_6') );
  1960. acf_log( acf_get_post_id_info('options_lol!') );
  1961. acf_log( acf_get_post_id_info('option') );
  1962. acf_log( acf_get_post_id_info('options') );
  1963. */
  1964. /*
  1965. * acf_isset_termmeta
  1966. *
  1967. * This function will return true if the termmeta table exists
  1968. * https://developer.wordpress.org/reference/functions/get_term_meta/
  1969. *
  1970. * @type function
  1971. * @date 3/09/2016
  1972. * @since 5.4.0
  1973. *
  1974. * @param $post_id (int)
  1975. * @return $post_id (int)
  1976. */
  1977. function acf_isset_termmeta( $taxonomy = '' ) {
  1978. // bail early if no table
  1979. if ( get_option( 'db_version' ) < 34370 ) {
  1980. return false;
  1981. }
  1982. // check taxonomy
  1983. if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
  1984. return false;
  1985. }
  1986. // return
  1987. return true;
  1988. }
  1989. /**
  1990. * This function will walk through the $_FILES data and upload each found.
  1991. *
  1992. * @date 25/10/2014
  1993. * @since 5.0.9
  1994. *
  1995. * @param array $ancestors An internal parameter, not required.
  1996. * @return void
  1997. */
  1998. function acf_upload_files( $ancestors = array() ) {
  1999. $file = acf_sanitize_files_array( $_FILES['acf'] );
  2000. // walk through ancestors
  2001. if ( ! empty( $ancestors ) ) {
  2002. foreach ( $ancestors as $a ) {
  2003. foreach ( array_keys( $file ) as $k ) {
  2004. $file[ $k ] = $file[ $k ][ $a ];
  2005. }
  2006. }
  2007. }
  2008. // is array?
  2009. if ( is_array( $file['name'] ) ) {
  2010. foreach ( array_keys( $file['name'] ) as $k ) {
  2011. $_ancestors = array_merge( $ancestors, array( $k ) );
  2012. acf_upload_files( $_ancestors );
  2013. }
  2014. return;
  2015. }
  2016. // Bail early if file has error (no file uploaded).
  2017. if ( $file['error'] ) {
  2018. return;
  2019. }
  2020. $field_key = end( $ancestors );
  2021. $nonce_name = $field_key . '_file_nonce';
  2022. if ( empty( $_REQUEST['acf'][ $nonce_name ] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['acf'][ $nonce_name ] ), 'acf/file_uploader_nonce/' . $field_key ) ) {
  2023. return;
  2024. }
  2025. // Assign global _acfuploader for media validation.
  2026. $_POST['_acfuploader'] = $field_key;
  2027. // file found!
  2028. $attachment_id = acf_upload_file( $file );
  2029. // update $_POST
  2030. array_unshift( $ancestors, 'acf' );
  2031. acf_update_nested_array( $_POST, $ancestors, $attachment_id );
  2032. }
  2033. /*
  2034. * acf_upload_file
  2035. *
  2036. * This function will uploade a $_FILE
  2037. *
  2038. * @type function
  2039. * @date 27/10/2014
  2040. * @since 5.0.9
  2041. *
  2042. * @param $uploaded_file (array) array found from $_FILE data
  2043. * @return $id (int) new attachment ID
  2044. */
  2045. function acf_upload_file( $uploaded_file ) {
  2046. // required
  2047. // require_once( ABSPATH . "/wp-load.php" ); // WP should already be loaded
  2048. require_once ABSPATH . '/wp-admin/includes/media.php'; // video functions
  2049. require_once ABSPATH . '/wp-admin/includes/file.php';
  2050. require_once ABSPATH . '/wp-admin/includes/image.php';
  2051. // required for wp_handle_upload() to upload the file
  2052. $upload_overrides = array( 'test_form' => false );
  2053. // upload
  2054. $file = wp_handle_upload( $uploaded_file, $upload_overrides );
  2055. // bail early if upload failed
  2056. if ( isset( $file['error'] ) ) {
  2057. return $file['error'];
  2058. }
  2059. // vars
  2060. $url = $file['url'];
  2061. $type = $file['type'];
  2062. $file = $file['file'];
  2063. $filename = basename( $file );
  2064. // Construct the object array
  2065. $object = array(
  2066. 'post_title' => $filename,
  2067. 'post_mime_type' => $type,
  2068. 'guid' => $url,
  2069. );
  2070. // Save the data
  2071. $id = wp_insert_attachment( $object, $file );
  2072. // Add the meta-data
  2073. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  2074. /** This action is documented in wp-admin/custom-header.php */
  2075. do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
  2076. // return new ID
  2077. return $id;
  2078. }
  2079. /*
  2080. * acf_update_nested_array
  2081. *
  2082. * This function will update a nested array value. Useful for modifying the $_POST array
  2083. *
  2084. * @type function
  2085. * @date 27/10/2014
  2086. * @since 5.0.9
  2087. *
  2088. * @param $array (array) target array to be updated
  2089. * @param $ancestors (array) array of keys to navigate through to find the child
  2090. * @param $value (mixed) The new value
  2091. * @return (boolean)
  2092. */
  2093. function acf_update_nested_array( &$array, $ancestors, $value ) {
  2094. // if no more ancestors, update the current var
  2095. if ( empty( $ancestors ) ) {
  2096. $array = $value;
  2097. // return
  2098. return true;
  2099. }
  2100. // shift the next ancestor from the array
  2101. $k = array_shift( $ancestors );
  2102. // if exists
  2103. if ( isset( $array[ $k ] ) ) {
  2104. return acf_update_nested_array( $array[ $k ], $ancestors, $value );
  2105. }
  2106. // return
  2107. return false;
  2108. }
  2109. /*
  2110. * acf_is_screen
  2111. *
  2112. * This function will return true if all args are matched for the current screen
  2113. *
  2114. * @type function
  2115. * @date 9/12/2014
  2116. * @since 5.1.5
  2117. *
  2118. * @param $post_id (int)
  2119. * @return $post_id (int)
  2120. */
  2121. function acf_is_screen( $id = '' ) {
  2122. // bail early if not defined
  2123. if ( ! function_exists( 'get_current_screen' ) ) {
  2124. return false;
  2125. }
  2126. // vars
  2127. $current_screen = get_current_screen();
  2128. // no screen
  2129. if ( ! $current_screen ) {
  2130. return false;
  2131. // array
  2132. } elseif ( is_array( $id ) ) {
  2133. return in_array( $current_screen->id, $id );
  2134. // string
  2135. } else {
  2136. return ( $id === $current_screen->id );
  2137. }
  2138. }
  2139. /*
  2140. * acf_maybe_get
  2141. *
  2142. * This function will return a var if it exists in an array
  2143. *
  2144. * @type function
  2145. * @date 9/12/2014
  2146. * @since 5.1.5
  2147. *
  2148. * @param $array (array) the array to look within
  2149. * @param $key (key) the array key to look for. Nested values may be found using '/'
  2150. * @param $default (mixed) the value returned if not found
  2151. * @return $post_id (int)
  2152. */
  2153. function acf_maybe_get( $array = array(), $key = 0, $default = null ) {
  2154. return isset( $array[ $key ] ) ? $array[ $key ] : $default;
  2155. }
  2156. function acf_maybe_get_POST( $key = '', $default = null ) {
  2157. return isset( $_POST[ $key ] ) ? acf_sanitize_request_args( $_POST[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- Checked elsewhere.
  2158. }
  2159. function acf_maybe_get_GET( $key = '', $default = null ) {
  2160. return isset( $_GET[ $key ] ) ? acf_sanitize_request_args( $_GET[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked elsewhere.
  2161. }
  2162. /**
  2163. * Returns an array of attachment data.
  2164. *
  2165. * @date 05/01/2015
  2166. * @since 5.1.5
  2167. *
  2168. * @param int|WP_Post The attachment ID or object.
  2169. * @return array|false
  2170. */
  2171. function acf_get_attachment( $attachment ) {
  2172. // Allow filter to short-circuit load attachment logic.
  2173. // Alternatively, this filter may be used to switch blogs for multisite media functionality.
  2174. $response = apply_filters( 'acf/pre_load_attachment', null, $attachment );
  2175. if ( $response !== null ) {
  2176. return $response;
  2177. }
  2178. // Get the attachment post object.
  2179. $attachment = get_post( $attachment );
  2180. if ( ! $attachment ) {
  2181. return false;
  2182. }
  2183. if ( $attachment->post_type !== 'attachment' ) {
  2184. return false;
  2185. }
  2186. // Load various attachment details.
  2187. $meta = wp_get_attachment_metadata( $attachment->ID );
  2188. $attached_file = get_attached_file( $attachment->ID );
  2189. if ( strpos( $attachment->post_mime_type, '/' ) !== false ) {
  2190. list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
  2191. } else {
  2192. list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
  2193. }
  2194. // Generate response.
  2195. $response = array(
  2196. 'ID' => $attachment->ID,
  2197. 'id' => $attachment->ID,
  2198. 'title' => $attachment->post_title,
  2199. 'filename' => wp_basename( $attached_file ),
  2200. 'filesize' => 0,
  2201. 'url' => wp_get_attachment_url( $attachment->ID ),
  2202. 'link' => get_attachment_link( $attachment->ID ),
  2203. 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
  2204. 'author' => $attachment->post_author,
  2205. 'description' => $attachment->post_content,
  2206. 'caption' => $attachment->post_excerpt,
  2207. 'name' => $attachment->post_name,
  2208. 'status' => $attachment->post_status,
  2209. 'uploaded_to' => $attachment->post_parent,
  2210. 'date' => $attachment->post_date_gmt,
  2211. 'modified' => $attachment->post_modified_gmt,
  2212. 'menu_order' => $attachment->menu_order,
  2213. 'mime_type' => $attachment->post_mime_type,
  2214. 'type' => $type,
  2215. 'subtype' => $subtype,
  2216. 'icon' => wp_mime_type_icon( $attachment->ID ),
  2217. );
  2218. // Append filesize data.
  2219. if ( isset( $meta['filesize'] ) ) {
  2220. $response['filesize'] = $meta['filesize'];
  2221. } elseif ( file_exists( $attached_file ) ) {
  2222. $response['filesize'] = filesize( $attached_file );
  2223. }
  2224. // Restrict the loading of image "sizes".
  2225. $sizes_id = 0;
  2226. // Type specific logic.
  2227. switch ( $type ) {
  2228. case 'image':
  2229. $sizes_id = $attachment->ID;
  2230. $src = wp_get_attachment_image_src( $attachment->ID, 'full' );
  2231. if ( $src ) {
  2232. $response['url'] = $src[0];
  2233. $response['width'] = $src[1];
  2234. $response['height'] = $src[2];
  2235. }
  2236. break;
  2237. case 'video':
  2238. $response['width'] = acf_maybe_get( $meta, 'width', 0 );
  2239. $response['height'] = acf_maybe_get( $meta, 'height', 0 );
  2240. if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
  2241. $sizes_id = $featured_id;
  2242. }
  2243. break;
  2244. case 'audio':
  2245. if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
  2246. $sizes_id = $featured_id;
  2247. }
  2248. break;
  2249. }
  2250. // Load array of image sizes.
  2251. if ( $sizes_id ) {
  2252. $sizes = get_intermediate_image_sizes();
  2253. $sizes_data = array();
  2254. foreach ( $sizes as $size ) {
  2255. $src = wp_get_attachment_image_src( $sizes_id, $size );
  2256. if ( $src ) {
  2257. $sizes_data[ $size ] = $src[0];
  2258. $sizes_data[ $size . '-width' ] = $src[1];
  2259. $sizes_data[ $size . '-height' ] = $src[2];
  2260. }
  2261. }
  2262. $response['sizes'] = $sizes_data;
  2263. }
  2264. /**
  2265. * Filters the attachment $response after it has been loaded.
  2266. *
  2267. * @date 16/06/2020
  2268. * @since 5.9.0
  2269. *
  2270. * @param array $response Array of loaded attachment data.
  2271. * @param WP_Post $attachment Attachment object.
  2272. * @param array|false $meta Array of attachment meta data, or false if there is none.
  2273. */
  2274. return apply_filters( 'acf/load_attachment', $response, $attachment, $meta );
  2275. }
  2276. /**
  2277. * This function will truncate and return a string
  2278. *
  2279. * @date 8/08/2014
  2280. * @since 5.0.0
  2281. *
  2282. * @param string $text The text to truncate.
  2283. * @param int $length The number of characters to allow in the string.
  2284. *
  2285. * @return string
  2286. */
  2287. function acf_get_truncated( $text, $length = 64 ) {
  2288. $text = trim( $text );
  2289. $the_length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );
  2290. $cut_length = $length - 3;
  2291. $return = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $cut_length ) : substr( $text, 0, $cut_length );
  2292. if ( $the_length > $cut_length ) {
  2293. $return .= '...';
  2294. }
  2295. return $return;
  2296. }
  2297. /*
  2298. * acf_current_user_can_admin
  2299. *
  2300. * This function will return true if the current user can administrate the ACF field groups
  2301. *
  2302. * @type function
  2303. * @date 9/02/2015
  2304. * @since 5.1.5
  2305. *
  2306. * @param $post_id (int)
  2307. * @return $post_id (int)
  2308. */
  2309. function acf_current_user_can_admin() {
  2310. if ( acf_get_setting( 'show_admin' ) && current_user_can( acf_get_setting( 'capability' ) ) ) {
  2311. return true;
  2312. }
  2313. // return
  2314. return false;
  2315. }
  2316. /*
  2317. * acf_get_filesize
  2318. *
  2319. * This function will return a numeric value of bytes for a given filesize string
  2320. *
  2321. * @type function
  2322. * @date 18/02/2015
  2323. * @since 5.1.5
  2324. *
  2325. * @param $size (mixed)
  2326. * @return (int)
  2327. */
  2328. function acf_get_filesize( $size = 1 ) {
  2329. // vars
  2330. $unit = 'MB';
  2331. $units = array(
  2332. 'TB' => 4,
  2333. 'GB' => 3,
  2334. 'MB' => 2,
  2335. 'KB' => 1,
  2336. );
  2337. // look for $unit within the $size parameter (123 KB)
  2338. if ( is_string( $size ) ) {
  2339. // vars
  2340. $custom = strtoupper( substr( $size, -2 ) );
  2341. foreach ( $units as $k => $v ) {
  2342. if ( $custom === $k ) {
  2343. $unit = $k;
  2344. $size = substr( $size, 0, -2 );
  2345. }
  2346. }
  2347. }
  2348. // calc bytes
  2349. $bytes = floatval( $size ) * pow( 1024, $units[ $unit ] );
  2350. // return
  2351. return $bytes;
  2352. }
  2353. /*
  2354. * acf_format_filesize
  2355. *
  2356. * This function will return a formatted string containing the filesize and unit
  2357. *
  2358. * @type function
  2359. * @date 18/02/2015
  2360. * @since 5.1.5
  2361. *
  2362. * @param $size (mixed)
  2363. * @return (int)
  2364. */
  2365. function acf_format_filesize( $size = 1 ) {
  2366. // convert
  2367. $bytes = acf_get_filesize( $size );
  2368. // vars
  2369. $units = array(
  2370. 'TB' => 4,
  2371. 'GB' => 3,
  2372. 'MB' => 2,
  2373. 'KB' => 1,
  2374. );
  2375. // loop through units
  2376. foreach ( $units as $k => $v ) {
  2377. $result = $bytes / pow( 1024, $v );
  2378. if ( $result >= 1 ) {
  2379. return $result . ' ' . $k;
  2380. }
  2381. }
  2382. // return
  2383. return $bytes . ' B';
  2384. }
  2385. /*
  2386. * acf_get_valid_terms
  2387. *
  2388. * This function will replace old terms with new split term ids
  2389. *
  2390. * @type function
  2391. * @date 27/02/2015
  2392. * @since 5.1.5
  2393. *
  2394. * @param $terms (int|array)
  2395. * @param $taxonomy (string)
  2396. * @return $terms
  2397. */
  2398. function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) {
  2399. // force into array
  2400. $terms = acf_get_array( $terms );
  2401. // force ints
  2402. $terms = array_map( 'intval', $terms );
  2403. // bail early if function does not yet exist or
  2404. if ( ! function_exists( 'wp_get_split_term' ) || empty( $terms ) ) {
  2405. return $terms;
  2406. }
  2407. // attempt to find new terms
  2408. foreach ( $terms as $i => $term_id ) {
  2409. $new_term_id = wp_get_split_term( $term_id, $taxonomy );
  2410. if ( $new_term_id ) {
  2411. $terms[ $i ] = $new_term_id;
  2412. }
  2413. }
  2414. // return
  2415. return $terms;
  2416. }
  2417. /*
  2418. * acf_validate_attachment
  2419. *
  2420. * This function will validate an attachment based on a field's restrictions and return an array of errors
  2421. *
  2422. * @type function
  2423. * @date 3/07/2015
  2424. * @since 5.2.3
  2425. *
  2426. * @param $attachment (array) attachment data. Changes based on context
  2427. * @param $field (array) field settings containing restrictions
  2428. * @param $context (string) $file is different when uploading / preparing
  2429. * @return $errors (array)
  2430. */
  2431. function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) {
  2432. // vars
  2433. $errors = array();
  2434. $file = array(
  2435. 'type' => '',
  2436. 'width' => 0,
  2437. 'height' => 0,
  2438. 'size' => 0,
  2439. );
  2440. // upload
  2441. if ( $context == 'upload' ) {
  2442. // vars
  2443. $file['type'] = pathinfo( $attachment['name'], PATHINFO_EXTENSION );
  2444. $file['size'] = filesize( $attachment['tmp_name'] );
  2445. if ( strpos( $attachment['type'], 'image' ) !== false ) {
  2446. $size = getimagesize( $attachment['tmp_name'] );
  2447. $file['width'] = acf_maybe_get( $size, 0 );
  2448. $file['height'] = acf_maybe_get( $size, 1 );
  2449. }
  2450. // prepare
  2451. } elseif ( $context == 'prepare' ) {
  2452. $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
  2453. $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
  2454. $file['size'] = acf_maybe_get( $attachment, 'filesizeInBytes', 0 );
  2455. $file['width'] = acf_maybe_get( $attachment, 'width', 0 );
  2456. $file['height'] = acf_maybe_get( $attachment, 'height', 0 );
  2457. // custom
  2458. } else {
  2459. $file = array_merge( $file, $attachment );
  2460. $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
  2461. $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
  2462. }
  2463. // image
  2464. if ( $file['width'] || $file['height'] ) {
  2465. // width
  2466. $min_width = (int) acf_maybe_get( $field, 'min_width', 0 );
  2467. $max_width = (int) acf_maybe_get( $field, 'max_width', 0 );
  2468. if ( $file['width'] ) {
  2469. if ( $min_width && $file['width'] < $min_width ) {
  2470. // min width
  2471. $errors['min_width'] = sprintf( __( 'Image width must be at least %dpx.', 'acf' ), $min_width );
  2472. } elseif ( $max_width && $file['width'] > $max_width ) {
  2473. // min width
  2474. $errors['max_width'] = sprintf( __( 'Image width must not exceed %dpx.', 'acf' ), $max_width );
  2475. }
  2476. }
  2477. // height
  2478. $min_height = (int) acf_maybe_get( $field, 'min_height', 0 );
  2479. $max_height = (int) acf_maybe_get( $field, 'max_height', 0 );
  2480. if ( $file['height'] ) {
  2481. if ( $min_height && $file['height'] < $min_height ) {
  2482. // min height
  2483. $errors['min_height'] = sprintf( __( 'Image height must be at least %dpx.', 'acf' ), $min_height );
  2484. } elseif ( $max_height && $file['height'] > $max_height ) {
  2485. // min height
  2486. $errors['max_height'] = sprintf( __( 'Image height must not exceed %dpx.', 'acf' ), $max_height );
  2487. }
  2488. }
  2489. }
  2490. // file size
  2491. if ( $file['size'] ) {
  2492. $min_size = acf_maybe_get( $field, 'min_size', 0 );
  2493. $max_size = acf_maybe_get( $field, 'max_size', 0 );
  2494. if ( $min_size && $file['size'] < acf_get_filesize( $min_size ) ) {
  2495. // min width
  2496. $errors['min_size'] = sprintf( __( 'File size must be at least %s.', 'acf' ), acf_format_filesize( $min_size ) );
  2497. } elseif ( $max_size && $file['size'] > acf_get_filesize( $max_size ) ) {
  2498. // min width
  2499. $errors['max_size'] = sprintf( __( 'File size must not exceed %s.', 'acf' ), acf_format_filesize( $max_size ) );
  2500. }
  2501. }
  2502. // file type
  2503. if ( $file['type'] ) {
  2504. $mime_types = acf_maybe_get( $field, 'mime_types', '' );
  2505. // lower case
  2506. $file['type'] = strtolower( $file['type'] );
  2507. $mime_types = strtolower( $mime_types );
  2508. // explode
  2509. $mime_types = str_replace( array( ' ', '.' ), '', $mime_types );
  2510. $mime_types = explode( ',', $mime_types ); // split pieces
  2511. $mime_types = array_filter( $mime_types ); // remove empty pieces
  2512. if ( ! empty( $mime_types ) && ! in_array( $file['type'], $mime_types ) ) {
  2513. // glue together last 2 types
  2514. if ( count( $mime_types ) > 1 ) {
  2515. $last1 = array_pop( $mime_types );
  2516. $last2 = array_pop( $mime_types );
  2517. $mime_types[] = $last2 . ' ' . __( 'or', 'acf' ) . ' ' . $last1;
  2518. }
  2519. $errors['mime_types'] = sprintf( __( 'File type must be %s.', 'acf' ), implode( ', ', $mime_types ) );
  2520. }
  2521. }
  2522. /**
  2523. * Filters the errors for a file before it is uploaded or displayed in the media modal.
  2524. *
  2525. * @date 3/07/2015
  2526. * @since 5.2.3
  2527. *
  2528. * @param array $errors An array of errors.
  2529. * @param array $file An array of data for a single file.
  2530. * @param array $attachment An array of attachment data which differs based on the context.
  2531. * @param array $field The field array.
  2532. * @param string $context The curent context (uploading, preparing)
  2533. */
  2534. $errors = apply_filters( "acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field, $context );
  2535. $errors = apply_filters( "acf/validate_attachment/name={$field['_name']}", $errors, $file, $attachment, $field, $context );
  2536. $errors = apply_filters( "acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field, $context );
  2537. $errors = apply_filters( 'acf/validate_attachment', $errors, $file, $attachment, $field, $context );
  2538. // return
  2539. return $errors;
  2540. }
  2541. /*
  2542. * _acf_settings_uploader
  2543. *
  2544. * Dynamic logic for uploader setting
  2545. *
  2546. * @type function
  2547. * @date 7/05/2015
  2548. * @since 5.2.3
  2549. *
  2550. * @param $uploader (string)
  2551. * @return $uploader
  2552. */
  2553. add_filter( 'acf/settings/uploader', '_acf_settings_uploader' );
  2554. function _acf_settings_uploader( $uploader ) {
  2555. // if can't upload files
  2556. if ( ! current_user_can( 'upload_files' ) ) {
  2557. $uploader = 'basic';
  2558. }
  2559. // return
  2560. return $uploader;
  2561. }
  2562. /*
  2563. * acf_translate_keys
  2564. *
  2565. * description
  2566. *
  2567. * @type function
  2568. * @date 7/12/2015
  2569. * @since 5.3.2
  2570. *
  2571. * @param $post_id (int)
  2572. * @return $post_id (int)
  2573. */
  2574. /*
  2575. function acf_translate_keys( $array, $keys ) {
  2576. // bail early if no keys
  2577. if( empty($keys) ) return $array;
  2578. // translate
  2579. foreach( $keys as $k ) {
  2580. // bail early if not exists
  2581. if( !isset($array[ $k ]) ) continue;
  2582. // translate
  2583. $array[ $k ] = acf_translate( $array[ $k ] );
  2584. }
  2585. // return
  2586. return $array;
  2587. }
  2588. */
  2589. /*
  2590. * acf_translate
  2591. *
  2592. * This function will translate a string using the new 'l10n_textdomain' setting
  2593. * Also works for arrays which is great for fields - select -> choices
  2594. *
  2595. * @type function
  2596. * @date 4/12/2015
  2597. * @since 5.3.2
  2598. *
  2599. * @param $string (mixed) string or array containins strings to be translated
  2600. * @return $string
  2601. */
  2602. function acf_translate( $string ) {
  2603. // vars
  2604. $l10n = acf_get_setting( 'l10n' );
  2605. $textdomain = acf_get_setting( 'l10n_textdomain' );
  2606. // bail early if not enabled
  2607. if ( ! $l10n ) {
  2608. return $string;
  2609. }
  2610. // bail early if no textdomain
  2611. if ( ! $textdomain ) {
  2612. return $string;
  2613. }
  2614. // is array
  2615. if ( is_array( $string ) ) {
  2616. return array_map( 'acf_translate', $string );
  2617. }
  2618. // bail early if not string
  2619. if ( ! is_string( $string ) ) {
  2620. return $string;
  2621. }
  2622. // bail early if empty
  2623. if ( $string === '' ) {
  2624. return $string;
  2625. }
  2626. // allow for var_export export
  2627. if ( acf_get_setting( 'l10n_var_export' ) ) {
  2628. // bail early if already translated
  2629. if ( substr( $string, 0, 7 ) === '!!__(!!' ) {
  2630. return $string;
  2631. }
  2632. // return
  2633. return "!!__(!!'" . $string . "!!', !!'" . $textdomain . "!!')!!";
  2634. }
  2635. // vars
  2636. return __( $string, $textdomain );
  2637. }
  2638. /*
  2639. * acf_maybe_add_action
  2640. *
  2641. * This function will determine if the action has already run before adding / calling the function
  2642. *
  2643. * @type function
  2644. * @date 13/01/2016
  2645. * @since 5.3.2
  2646. *
  2647. * @param $post_id (int)
  2648. * @return $post_id (int)
  2649. */
  2650. function acf_maybe_add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
  2651. // if action has already run, execute it
  2652. // - if currently doing action, allow $tag to be added as per usual to allow $priority ordering needed for 3rd party asset compatibility
  2653. if ( did_action( $tag ) && ! doing_action( $tag ) ) {
  2654. call_user_func( $function_to_add );
  2655. // if action has not yet run, add it
  2656. } else {
  2657. add_action( $tag, $function_to_add, $priority, $accepted_args );
  2658. }
  2659. }
  2660. /*
  2661. * acf_is_row_collapsed
  2662. *
  2663. * This function will return true if the field's row is collapsed
  2664. *
  2665. * @type function
  2666. * @date 2/03/2016
  2667. * @since 5.3.2
  2668. *
  2669. * @param $post_id (int)
  2670. * @return $post_id (int)
  2671. */
  2672. function acf_is_row_collapsed( $field_key = '', $row_index = 0 ) {
  2673. // collapsed
  2674. $collapsed = acf_get_user_setting( 'collapsed_' . $field_key, '' );
  2675. // cookie fallback ( version < 5.3.2 )
  2676. if ( $collapsed === '' ) {
  2677. $collapsed = acf_extract_var( $_COOKIE, "acf_collapsed_{$field_key}", '' );
  2678. $collapsed = str_replace( '|', ',', $collapsed );
  2679. // update
  2680. acf_update_user_setting( 'collapsed_' . $field_key, $collapsed );
  2681. }
  2682. // explode
  2683. $collapsed = explode( ',', $collapsed );
  2684. $collapsed = array_filter( $collapsed, 'is_numeric' );
  2685. // collapsed class
  2686. return in_array( $row_index, $collapsed );
  2687. }
  2688. /*
  2689. * acf_get_attachment_image
  2690. *
  2691. * description
  2692. *
  2693. * @type function
  2694. * @date 24/10/16
  2695. * @since 5.5.0
  2696. *
  2697. * @param $post_id (int)
  2698. * @return $post_id (int)
  2699. */
  2700. function acf_get_attachment_image( $attachment_id = 0, $size = 'thumbnail' ) {
  2701. // vars
  2702. $url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
  2703. $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
  2704. // bail early if no url
  2705. if ( ! $url ) {
  2706. return '';
  2707. }
  2708. // return
  2709. $value = '<img src="' . $url . '" alt="' . $alt . '" />';
  2710. }
  2711. /*
  2712. * acf_get_post_thumbnail
  2713. *
  2714. * This function will return a thumbail image url for a given post
  2715. *
  2716. * @type function
  2717. * @date 3/05/2016
  2718. * @since 5.3.8
  2719. *
  2720. * @param $post (obj)
  2721. * @param $size (mixed)
  2722. * @return (string)
  2723. */
  2724. function acf_get_post_thumbnail( $post = null, $size = 'thumbnail' ) {
  2725. // vars
  2726. $data = array(
  2727. 'url' => '',
  2728. 'type' => '',
  2729. 'html' => '',
  2730. );
  2731. // post
  2732. $post = get_post( $post );
  2733. // bail early if no post
  2734. if ( ! $post ) {
  2735. return $data;
  2736. }
  2737. // vars
  2738. $thumb_id = $post->ID;
  2739. $mime_type = acf_maybe_get( explode( '/', $post->post_mime_type ), 0 );
  2740. // attachment
  2741. if ( $post->post_type === 'attachment' ) {
  2742. // change $thumb_id
  2743. if ( $mime_type === 'audio' || $mime_type === 'video' ) {
  2744. $thumb_id = get_post_thumbnail_id( $post->ID );
  2745. }
  2746. // post
  2747. } else {
  2748. $thumb_id = get_post_thumbnail_id( $post->ID );
  2749. }
  2750. // try url
  2751. $data['url'] = wp_get_attachment_image_src( $thumb_id, $size );
  2752. $data['url'] = acf_maybe_get( $data['url'], 0 );
  2753. // default icon
  2754. if ( ! $data['url'] && $post->post_type === 'attachment' ) {
  2755. $data['url'] = wp_mime_type_icon( $post->ID );
  2756. $data['type'] = 'icon';
  2757. }
  2758. // html
  2759. $data['html'] = '<img src="' . $data['url'] . '" alt="" />';
  2760. // return
  2761. return $data;
  2762. }
  2763. /**
  2764. * acf_get_browser
  2765. *
  2766. * Returns the name of the current browser.
  2767. *
  2768. * @date 17/01/2014
  2769. * @since 5.0.0
  2770. *
  2771. * @param void
  2772. * @return string
  2773. */
  2774. function acf_get_browser() {
  2775. // Check server var.
  2776. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
  2777. $agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
  2778. // Loop over search terms.
  2779. $browsers = array(
  2780. 'Firefox' => 'firefox',
  2781. 'Trident' => 'msie',
  2782. 'MSIE' => 'msie',
  2783. 'Edge' => 'edge',
  2784. 'Chrome' => 'chrome',
  2785. 'Safari' => 'safari',
  2786. );
  2787. foreach ( $browsers as $k => $v ) {
  2788. if ( strpos( $agent, $k ) !== false ) {
  2789. return $v;
  2790. }
  2791. }
  2792. }
  2793. // Return default.
  2794. return '';
  2795. }
  2796. /*
  2797. * acf_is_ajax
  2798. *
  2799. * This function will reutrn true if performing a wp ajax call
  2800. *
  2801. * @type function
  2802. * @date 7/06/2016
  2803. * @since 5.3.8
  2804. *
  2805. * @param n/a
  2806. * @return (boolean)
  2807. */
  2808. function acf_is_ajax( $action = '' ) {
  2809. // vars
  2810. $is_ajax = false;
  2811. // check if is doing ajax
  2812. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  2813. $is_ajax = true;
  2814. }
  2815. // phpcs:disable WordPress.Security.NonceVerification.Missing
  2816. // check $action
  2817. if ( $action && acf_maybe_get( $_POST, 'action' ) !== $action ) {
  2818. // phpcs:enable WordPress.Security.NonceVerification.Missing
  2819. $is_ajax = false;
  2820. }
  2821. // return
  2822. return $is_ajax;
  2823. }
  2824. /*
  2825. * acf_format_date
  2826. *
  2827. * This function will accept a date value and return it in a formatted string
  2828. *
  2829. * @type function
  2830. * @date 16/06/2016
  2831. * @since 5.3.8
  2832. *
  2833. * @param $value (string)
  2834. * @return $format (string)
  2835. */
  2836. function acf_format_date( $value, $format ) {
  2837. // bail early if no value
  2838. if ( ! $value ) {
  2839. return $value;
  2840. }
  2841. // vars
  2842. $unixtimestamp = 0;
  2843. // numeric (either unix or YYYYMMDD)
  2844. if ( is_numeric( $value ) && strlen( $value ) !== 8 ) {
  2845. $unixtimestamp = $value;
  2846. } else {
  2847. $unixtimestamp = strtotime( $value );
  2848. }
  2849. // return
  2850. return date_i18n( $format, $unixtimestamp );
  2851. }
  2852. /**
  2853. * acf_clear_log
  2854. *
  2855. * Deletes the debug.log file.
  2856. *
  2857. * @date 21/1/19
  2858. * @since 5.7.10
  2859. *
  2860. * @param type $var Description. Default.
  2861. * @return type Description.
  2862. */
  2863. function acf_clear_log() {
  2864. unlink( WP_CONTENT_DIR . '/debug.log' );
  2865. }
  2866. /*
  2867. * acf_log
  2868. *
  2869. * description
  2870. *
  2871. * @type function
  2872. * @date 24/06/2016
  2873. * @since 5.3.8
  2874. *
  2875. * @param $post_id (int)
  2876. * @return $post_id (int)
  2877. */
  2878. function acf_log() {
  2879. // vars
  2880. $args = func_get_args();
  2881. // loop
  2882. foreach ( $args as $i => $arg ) {
  2883. // array | object
  2884. if ( is_array( $arg ) || is_object( $arg ) ) {
  2885. $arg = print_r( $arg, true );
  2886. // bool
  2887. } elseif ( is_bool( $arg ) ) {
  2888. $arg = 'bool(' . ( $arg ? 'true' : 'false' ) . ')';
  2889. }
  2890. // update
  2891. $args[ $i ] = $arg;
  2892. }
  2893. // log
  2894. error_log( implode( ' ', $args ) );
  2895. }
  2896. /**
  2897. * acf_dev_log
  2898. *
  2899. * Used to log variables only if ACF_DEV is defined
  2900. *
  2901. * @date 25/8/18
  2902. * @since 5.7.4
  2903. *
  2904. * @param mixed
  2905. * @return void
  2906. */
  2907. function acf_dev_log() {
  2908. if ( defined( 'ACF_DEV' ) && ACF_DEV ) {
  2909. call_user_func_array( 'acf_log', func_get_args() );
  2910. }
  2911. }
  2912. /*
  2913. * acf_doing
  2914. *
  2915. * This function will tell ACF what task it is doing
  2916. *
  2917. * @type function
  2918. * @date 28/06/2016
  2919. * @since 5.3.8
  2920. *
  2921. * @param $event (string)
  2922. * @param context (string)
  2923. * @return n/a
  2924. */
  2925. function acf_doing( $event = '', $context = '' ) {
  2926. acf_update_setting( 'doing', $event );
  2927. acf_update_setting( 'doing_context', $context );
  2928. }
  2929. /*
  2930. * acf_is_doing
  2931. *
  2932. * This function can be used to state what ACF is doing, or to check
  2933. *
  2934. * @type function
  2935. * @date 28/06/2016
  2936. * @since 5.3.8
  2937. *
  2938. * @param $event (string)
  2939. * @param context (string)
  2940. * @return (boolean)
  2941. */
  2942. function acf_is_doing( $event = '', $context = '' ) {
  2943. // vars
  2944. $doing = false;
  2945. // task
  2946. if ( acf_get_setting( 'doing' ) === $event ) {
  2947. $doing = true;
  2948. }
  2949. // context
  2950. if ( $context && acf_get_setting( 'doing_context' ) !== $context ) {
  2951. $doing = false;
  2952. }
  2953. // return
  2954. return $doing;
  2955. }
  2956. /*
  2957. * acf_is_plugin_active
  2958. *
  2959. * This function will return true if the ACF plugin is active
  2960. * - May be included within a theme or other plugin
  2961. *
  2962. * @type function
  2963. * @date 13/07/2016
  2964. * @since 5.4.0
  2965. *
  2966. * @param $basename (int)
  2967. * @return $post_id (int)
  2968. */
  2969. function acf_is_plugin_active() {
  2970. // vars
  2971. $basename = acf_get_setting( 'basename' );
  2972. // ensure is_plugin_active() exists (not on frontend)
  2973. if ( ! function_exists( 'is_plugin_active' ) ) {
  2974. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  2975. }
  2976. // return
  2977. return is_plugin_active( $basename );
  2978. }
  2979. /*
  2980. * acf_send_ajax_results
  2981. *
  2982. * This function will print JSON data for a Select2 AJAX query
  2983. *
  2984. * @type function
  2985. * @date 19/07/2016
  2986. * @since 5.4.0
  2987. *
  2988. * @param $response (array)
  2989. * @return n/a
  2990. */
  2991. function acf_send_ajax_results( $response ) {
  2992. // validate
  2993. $response = wp_parse_args(
  2994. $response,
  2995. array(
  2996. 'results' => array(),
  2997. 'more' => false,
  2998. 'limit' => 0,
  2999. )
  3000. );
  3001. // limit
  3002. if ( $response['limit'] && $response['results'] ) {
  3003. // vars
  3004. $total = 0;
  3005. foreach ( $response['results'] as $result ) {
  3006. // parent
  3007. $total++;
  3008. // children
  3009. if ( ! empty( $result['children'] ) ) {
  3010. $total += count( $result['children'] );
  3011. }
  3012. }
  3013. // calc
  3014. if ( $total >= $response['limit'] ) {
  3015. $response['more'] = true;
  3016. }
  3017. }
  3018. // return
  3019. wp_send_json( $response );
  3020. }
  3021. /*
  3022. * acf_is_sequential_array
  3023. *
  3024. * This function will return true if the array contains only numeric keys
  3025. *
  3026. * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
  3027. * @type function
  3028. * @date 9/09/2016
  3029. * @since 5.4.0
  3030. *
  3031. * @param $array (array)
  3032. * @return (boolean)
  3033. */
  3034. function acf_is_sequential_array( $array ) {
  3035. // bail early if not array
  3036. if ( ! is_array( $array ) ) {
  3037. return false;
  3038. }
  3039. // loop
  3040. foreach ( $array as $key => $value ) {
  3041. // bail early if is string
  3042. if ( is_string( $key ) ) {
  3043. return false;
  3044. }
  3045. }
  3046. // return
  3047. return true;
  3048. }
  3049. /*
  3050. * acf_is_associative_array
  3051. *
  3052. * This function will return true if the array contains one or more string keys
  3053. *
  3054. * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
  3055. * @type function
  3056. * @date 9/09/2016
  3057. * @since 5.4.0
  3058. *
  3059. * @param $array (array)
  3060. * @return (boolean)
  3061. */
  3062. function acf_is_associative_array( $array ) {
  3063. // bail early if not array
  3064. if ( ! is_array( $array ) ) {
  3065. return false;
  3066. }
  3067. // loop
  3068. foreach ( $array as $key => $value ) {
  3069. // bail early if is string
  3070. if ( is_string( $key ) ) {
  3071. return true;
  3072. }
  3073. }
  3074. // return
  3075. return false;
  3076. }
  3077. /*
  3078. * acf_add_array_key_prefix
  3079. *
  3080. * This function will add a prefix to all array keys
  3081. * Useful to preserve numeric keys when performing array_multisort
  3082. *
  3083. * @type function
  3084. * @date 15/09/2016
  3085. * @since 5.4.0
  3086. *
  3087. * @param $array (array)
  3088. * @param $prefix (string)
  3089. * @return (array)
  3090. */
  3091. function acf_add_array_key_prefix( $array, $prefix ) {
  3092. // vars
  3093. $array2 = array();
  3094. // loop
  3095. foreach ( $array as $k => $v ) {
  3096. $k2 = $prefix . $k;
  3097. $array2[ $k2 ] = $v;
  3098. }
  3099. // return
  3100. return $array2;
  3101. }
  3102. /*
  3103. * acf_remove_array_key_prefix
  3104. *
  3105. * This function will remove a prefix to all array keys
  3106. * Useful to preserve numeric keys when performing array_multisort
  3107. *
  3108. * @type function
  3109. * @date 15/09/2016
  3110. * @since 5.4.0
  3111. *
  3112. * @param $array (array)
  3113. * @param $prefix (string)
  3114. * @return (array)
  3115. */
  3116. function acf_remove_array_key_prefix( $array, $prefix ) {
  3117. // vars
  3118. $array2 = array();
  3119. $l = strlen( $prefix );
  3120. // loop
  3121. foreach ( $array as $k => $v ) {
  3122. $k2 = ( substr( $k, 0, $l ) === $prefix ) ? substr( $k, $l ) : $k;
  3123. $array2[ $k2 ] = $v;
  3124. }
  3125. // return
  3126. return $array2;
  3127. }
  3128. /*
  3129. * acf_strip_protocol
  3130. *
  3131. * This function will remove the proticol from a url
  3132. * Used to allow licenses to remain active if a site is switched to https
  3133. *
  3134. * @type function
  3135. * @date 10/01/2017
  3136. * @since 5.5.4
  3137. * @author Aaron
  3138. *
  3139. * @param $url (string)
  3140. * @return (string)
  3141. */
  3142. function acf_strip_protocol( $url ) {
  3143. // strip the protical
  3144. return str_replace( array( 'http://', 'https://' ), '', $url );
  3145. }
  3146. /*
  3147. * acf_connect_attachment_to_post
  3148. *
  3149. * This function will connect an attacment (image etc) to the post
  3150. * Used to connect attachements uploaded directly to media that have not been attaced to a post
  3151. *
  3152. * @type function
  3153. * @date 11/01/2017
  3154. * @since 5.8.0 Added filter to prevent connection.
  3155. * @since 5.5.4
  3156. *
  3157. * @param int $attachment_id The attachment ID.
  3158. * @param int $post_id The post ID.
  3159. * @return bool True if attachment was connected.
  3160. */
  3161. function acf_connect_attachment_to_post( $attachment_id = 0, $post_id = 0 ) {
  3162. // bail early if $attachment_id is not valid.
  3163. if ( ! $attachment_id || ! is_numeric( $attachment_id ) ) {
  3164. return false;
  3165. }
  3166. // bail early if $post_id is not valid.
  3167. if ( ! $post_id || ! is_numeric( $post_id ) ) {
  3168. return false;
  3169. }
  3170. /**
  3171. * Filters whether or not to connect the attachment.
  3172. *
  3173. * @date 8/11/18
  3174. * @since 5.8.0
  3175. *
  3176. * @param bool $bool Returning false will prevent the connection. Default true.
  3177. * @param int $attachment_id The attachment ID.
  3178. * @param int $post_id The post ID.
  3179. */
  3180. if ( ! apply_filters( 'acf/connect_attachment_to_post', true, $attachment_id, $post_id ) ) {
  3181. return false;
  3182. }
  3183. // vars
  3184. $post = get_post( $attachment_id );
  3185. // Check if is valid post.
  3186. if ( $post && $post->post_type == 'attachment' && $post->post_parent == 0 ) {
  3187. // update
  3188. wp_update_post(
  3189. array(
  3190. 'ID' => $post->ID,
  3191. 'post_parent' => $post_id,
  3192. )
  3193. );
  3194. // return
  3195. return true;
  3196. }
  3197. // return
  3198. return true;
  3199. }
  3200. /*
  3201. * acf_encrypt
  3202. *
  3203. * This function will encrypt a string using PHP
  3204. * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
  3205. *
  3206. * @type function
  3207. * @date 27/2/17
  3208. * @since 5.5.8
  3209. *
  3210. * @param $data (string)
  3211. * @return (string)
  3212. */
  3213. function acf_encrypt( $data = '' ) {
  3214. // bail early if no encrypt function
  3215. if ( ! function_exists( 'openssl_encrypt' ) ) {
  3216. return base64_encode( $data );
  3217. }
  3218. // generate a key
  3219. $key = wp_hash( 'acf_encrypt' );
  3220. // Generate an initialization vector
  3221. $iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );
  3222. // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
  3223. $encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $key, 0, $iv );
  3224. // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
  3225. return base64_encode( $encrypted_data . '::' . $iv );
  3226. }
  3227. /*
  3228. * acf_decrypt
  3229. *
  3230. * This function will decrypt an encrypted string using PHP
  3231. * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
  3232. *
  3233. * @type function
  3234. * @date 27/2/17
  3235. * @since 5.5.8
  3236. *
  3237. * @param $data (string)
  3238. * @return (string)
  3239. */
  3240. function acf_decrypt( $data = '' ) {
  3241. // bail early if no decrypt function
  3242. if ( ! function_exists( 'openssl_decrypt' ) ) {
  3243. return base64_decode( $data );
  3244. }
  3245. // generate a key
  3246. $key = wp_hash( 'acf_encrypt' );
  3247. // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
  3248. list($encrypted_data, $iv) = explode( '::', base64_decode( $data ), 2 );
  3249. // decrypt
  3250. return openssl_decrypt( $encrypted_data, 'aes-256-cbc', $key, 0, $iv );
  3251. }
  3252. /**
  3253. * acf_parse_markdown
  3254. *
  3255. * A very basic regex-based Markdown parser function based off [slimdown](https://gist.github.com/jbroadway/2836900).
  3256. *
  3257. * @date 6/8/18
  3258. * @since 5.7.2
  3259. *
  3260. * @param string $text The string to parse.
  3261. * @return string
  3262. */
  3263. function acf_parse_markdown( $text = '' ) {
  3264. // trim
  3265. $text = trim( $text );
  3266. // rules
  3267. $rules = array(
  3268. '/=== (.+?) ===/' => '<h2>$1</h2>', // headings
  3269. '/== (.+?) ==/' => '<h3>$1</h3>', // headings
  3270. '/= (.+?) =/' => '<h4>$1</h4>', // headings
  3271. '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href="$2">$1</a>', // links
  3272. '/(\*\*)(.*?)\1/' => '<strong>$2</strong>', // bold
  3273. '/(\*)(.*?)\1/' => '<em>$2</em>', // intalic
  3274. '/`(.*?)`/' => '<code>$1</code>', // inline code
  3275. '/\n\*(.*)/' => "\n<ul>\n\t<li>$1</li>\n</ul>", // ul lists
  3276. '/\n[0-9]+\.(.*)/' => "\n<ol>\n\t<li>$1</li>\n</ol>", // ol lists
  3277. '/<\/ul>\s?<ul>/' => '', // fix extra ul
  3278. '/<\/ol>\s?<ol>/' => '', // fix extra ol
  3279. );
  3280. foreach ( $rules as $k => $v ) {
  3281. $text = preg_replace( $k, $v, $text );
  3282. }
  3283. // autop
  3284. $text = wpautop( $text );
  3285. // return
  3286. return $text;
  3287. }
  3288. /**
  3289. * acf_get_sites
  3290. *
  3291. * Returns an array of sites for a network.
  3292. *
  3293. * @date 29/08/2016
  3294. * @since 5.4.0
  3295. *
  3296. * @param void
  3297. * @return array
  3298. */
  3299. function acf_get_sites() {
  3300. $results = array();
  3301. $sites = get_sites( array( 'number' => 0 ) );
  3302. if ( $sites ) {
  3303. foreach ( $sites as $site ) {
  3304. $results[] = get_site( $site )->to_array();
  3305. }
  3306. }
  3307. return $results;
  3308. }
  3309. /**
  3310. * acf_convert_rules_to_groups
  3311. *
  3312. * Converts an array of rules from ACF4 to an array of groups for ACF5
  3313. *
  3314. * @date 25/8/18
  3315. * @since 5.7.4
  3316. *
  3317. * @param array $rules An array of rules.
  3318. * @param string $anyorall The anyorall setting used in ACF4. Defaults to 'any'.
  3319. * @return array
  3320. */
  3321. function acf_convert_rules_to_groups( $rules, $anyorall = 'any' ) {
  3322. // vars
  3323. $groups = array();
  3324. $index = 0;
  3325. // loop
  3326. foreach ( $rules as $rule ) {
  3327. // extract vars
  3328. $group = acf_extract_var( $rule, 'group_no' );
  3329. $order = acf_extract_var( $rule, 'order_no' );
  3330. // calculate group if not defined
  3331. if ( $group === null ) {
  3332. $group = $index;
  3333. // use $anyorall to determine if a new group is needed
  3334. if ( $anyorall == 'any' ) {
  3335. $index++;
  3336. }
  3337. }
  3338. // calculate order if not defined
  3339. if ( $order === null ) {
  3340. $order = isset( $groups[ $group ] ) ? count( $groups[ $group ] ) : 0;
  3341. }
  3342. // append to group
  3343. $groups[ $group ][ $order ] = $rule;
  3344. // sort groups
  3345. ksort( $groups[ $group ] );
  3346. }
  3347. // sort groups
  3348. ksort( $groups );
  3349. // return
  3350. return $groups;
  3351. }
  3352. /**
  3353. * acf_register_ajax
  3354. *
  3355. * Regsiters an ajax callback.
  3356. *
  3357. * @date 5/10/18
  3358. * @since 5.7.7
  3359. *
  3360. * @param string $name The ajax action name.
  3361. * @param array $callback The callback function or array.
  3362. * @param bool $public Whether to allow access to non logged in users.
  3363. * @return void
  3364. */
  3365. function acf_register_ajax( $name = '', $callback = false, $public = false ) {
  3366. // vars
  3367. $action = "acf/ajax/$name";
  3368. // add action for logged-in users
  3369. add_action( "wp_ajax_$action", $callback );
  3370. // add action for non logged-in users
  3371. if ( $public ) {
  3372. add_action( "wp_ajax_nopriv_$action", $callback );
  3373. }
  3374. }
  3375. /**
  3376. * acf_str_camel_case
  3377. *
  3378. * Converts a string into camelCase.
  3379. * Thanks to https://stackoverflow.com/questions/31274782/convert-array-keys-from-underscore-case-to-camelcase-recursively
  3380. *
  3381. * @date 24/10/18
  3382. * @since 5.8.0
  3383. *
  3384. * @param string $string The string ot convert.
  3385. * @return string
  3386. */
  3387. function acf_str_camel_case( $string = '' ) {
  3388. return lcfirst( str_replace( ' ', '', ucwords( str_replace( '_', ' ', $string ) ) ) );
  3389. }
  3390. /**
  3391. * acf_array_camel_case
  3392. *
  3393. * Converts all aray keys to camelCase.
  3394. *
  3395. * @date 24/10/18
  3396. * @since 5.8.0
  3397. *
  3398. * @param array $array The array to convert.
  3399. * @return array
  3400. */
  3401. function acf_array_camel_case( $array = array() ) {
  3402. $array2 = array();
  3403. foreach ( $array as $k => $v ) {
  3404. $array2[ acf_str_camel_case( $k ) ] = $v;
  3405. }
  3406. return $array2;
  3407. }
  3408. /**
  3409. * Returns true if the current screen is using the block editor.
  3410. *
  3411. * @date 13/12/18
  3412. * @since 5.8.0
  3413. *
  3414. * @return bool
  3415. */
  3416. function acf_is_block_editor() {
  3417. if ( function_exists( 'get_current_screen' ) ) {
  3418. $screen = get_current_screen();
  3419. if ( $screen && method_exists( $screen, 'is_block_editor' ) ) {
  3420. return $screen->is_block_editor();
  3421. }
  3422. }
  3423. return false;
  3424. }