12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562 |
- <?php
- /*
- * acf_is_array
- *
- * This function will return true for a non empty array
- *
- * @type function
- * @date 6/07/2016
- * @since 5.4.0
- *
- * @param $array (array)
- * @return (boolean)
- */
- function acf_is_array( $array ) {
- return ( is_array( $array ) && ! empty( $array ) );
- }
- /**
- * acf_has_setting
- *
- * alias of acf()->has_setting()
- *
- * @date 2/2/18
- * @since 5.6.5
- *
- * @param n/a
- * @return n/a
- */
- function acf_has_setting( $name = '' ) {
- return acf()->has_setting( $name );
- }
- /**
- * acf_raw_setting
- *
- * alias of acf()->get_setting()
- *
- * @date 2/2/18
- * @since 5.6.5
- *
- * @param n/a
- * @return n/a
- */
- function acf_raw_setting( $name = '' ) {
- return acf()->get_setting( $name );
- }
- /*
- * acf_update_setting
- *
- * alias of acf()->update_setting()
- *
- * @type function
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param $name (string)
- * @param $value (mixed)
- * @return n/a
- */
- function acf_update_setting( $name, $value ) {
- // validate name
- $name = acf_validate_setting( $name );
- // update
- return acf()->update_setting( $name, $value );
- }
- /**
- * acf_validate_setting
- *
- * Returns the changed setting name if available.
- *
- * @date 2/2/18
- * @since 5.6.5
- *
- * @param n/a
- * @return n/a
- */
- function acf_validate_setting( $name = '' ) {
- return apply_filters( 'acf/validate_setting', $name );
- }
- /*
- * acf_get_setting
- *
- * alias of acf()->get_setting()
- *
- * @type function
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- function acf_get_setting( $name, $value = null ) {
- // validate name
- $name = acf_validate_setting( $name );
- // check settings
- if ( acf_has_setting( $name ) ) {
- $value = acf_raw_setting( $name );
- }
- // filter
- $value = apply_filters( "acf/settings/{$name}", $value );
- // return
- return $value;
- }
- /*
- * acf_append_setting
- *
- * This function will add a value into the settings array found in the acf object
- *
- * @type function
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param $name (string)
- * @param $value (mixed)
- * @return n/a
- */
- function acf_append_setting( $name, $value ) {
- // vars
- $setting = acf_raw_setting( $name );
- // bail early if not array
- if ( ! is_array( $setting ) ) {
- $setting = array();
- }
- // append
- $setting[] = $value;
- // update
- return acf_update_setting( $name, $setting );
- }
- /**
- * acf_get_data
- *
- * Returns data.
- *
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param string $name
- * @return mixed
- */
- function acf_get_data( $name ) {
- return acf()->get_data( $name );
- }
- /**
- * acf_set_data
- *
- * Sets data.
- *
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param string $name
- * @param mixed $value
- * @return n/a
- */
- function acf_set_data( $name, $value ) {
- return acf()->set_data( $name, $value );
- }
- /**
- * Appends data to an existing key.
- *
- * @date 11/06/2020
- * @since 5.9.0
- *
- * @param string $name The data name.
- * @return array $data The data array.
- */
- function acf_append_data( $name, $data ) {
- $prev_data = acf()->get_data( $name );
- if ( is_array( $prev_data ) ) {
- $data = array_merge( $prev_data, $data );
- }
- acf()->set_data( $name, $data );
- }
- /*
- * acf_init
- *
- * alias of acf()->init()
- *
- * @type function
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- function acf_init() {
- acf()->init();
- }
- /*
- * acf_has_done
- *
- * This function will return true if this action has already been done
- *
- * @type function
- * @date 16/12/2015
- * @since 5.3.2
- *
- * @param $name (string)
- * @return (boolean)
- */
- function acf_has_done( $name ) {
- // return true if already done
- if ( acf_raw_setting( "has_done_{$name}" ) ) {
- return true;
- }
- // update setting and return
- acf_update_setting( "has_done_{$name}", true );
- return false;
- }
- /*
- * acf_get_external_path
- *
- * This function will return the path to a file within an external folder
- *
- * @type function
- * @date 22/2/17
- * @since 5.5.8
- *
- * @param $file (string)
- * @param $path (string)
- * @return (string)
- */
- function acf_get_external_path( $file, $path = '' ) {
- return plugin_dir_path( $file ) . $path;
- }
- /*
- * acf_get_external_dir
- *
- * This function will return the url to a file within an external folder
- *
- * @type function
- * @date 22/2/17
- * @since 5.5.8
- *
- * @param $file (string)
- * @param $path (string)
- * @return (string)
- */
- function acf_get_external_dir( $file, $path = '' ) {
- return acf_plugin_dir_url( $file ) . $path;
- }
- /**
- * acf_plugin_dir_url
- *
- * This function will calculate the url to a plugin folder.
- * Different to the WP plugin_dir_url(), this function can calculate for urls outside of the plugins folder (theme include).
- *
- * @date 13/12/17
- * @since 5.6.8
- *
- * @param type $var Description. Default.
- * @return type Description.
- */
- function acf_plugin_dir_url( $file ) {
- // vars
- $path = plugin_dir_path( $file );
- $path = wp_normalize_path( $path );
- // check plugins
- $check_path = wp_normalize_path( realpath( WP_PLUGIN_DIR ) );
- if ( strpos( $path, $check_path ) === 0 ) {
- return str_replace( $check_path, plugins_url(), $path );
- }
- // check wp-content
- $check_path = wp_normalize_path( realpath( WP_CONTENT_DIR ) );
- if ( strpos( $path, $check_path ) === 0 ) {
- return str_replace( $check_path, content_url(), $path );
- }
- // check root
- $check_path = wp_normalize_path( realpath( ABSPATH ) );
- if ( strpos( $path, $check_path ) === 0 ) {
- return str_replace( $check_path, site_url( '/' ), $path );
- }
- // return
- return plugin_dir_url( $file );
- }
- /*
- * acf_parse_args
- *
- * This function will merge together 2 arrays and also convert any numeric values to ints
- *
- * @type function
- * @date 18/10/13
- * @since 5.0.0
- *
- * @param $args (array)
- * @param $defaults (array)
- * @return $args (array)
- */
- function acf_parse_args( $args, $defaults = array() ) {
- // parse args
- $args = wp_parse_args( $args, $defaults );
- // parse types
- $args = acf_parse_types( $args );
- // return
- return $args;
- }
- /*
- * acf_parse_types
- *
- * This function will convert any numeric values to int and trim strings
- *
- * @type function
- * @date 18/10/13
- * @since 5.0.0
- *
- * @param $var (mixed)
- * @return $var (mixed)
- */
- function acf_parse_types( $array ) {
- return array_map( 'acf_parse_type', $array );
- }
- /*
- * acf_parse_type
- *
- * description
- *
- * @type function
- * @date 11/11/2014
- * @since 5.0.9
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_parse_type( $v ) {
- // Check if is string.
- if ( is_string( $v ) ) {
- // Trim ("Word " = "Word").
- $v = trim( $v );
- // Convert int strings to int ("123" = 123).
- if ( is_numeric( $v ) && strval( intval( $v ) ) === $v ) {
- $v = intval( $v );
- }
- }
- // return.
- return $v;
- }
- /**
- * This function will load in a file from the 'admin/views' folder and allow variables to be passed through
- *
- * @date 28/09/13
- * @since 5.0.0
- *
- * @param string $view_path
- * @param array $view_args
- *
- * @return void
- */
- function acf_get_view( $view_path = '', $view_args = array() ) {
- // allow view file name shortcut
- if ( substr( $view_path, -4 ) !== '.php' ) {
- $view_path = acf_get_path( "includes/admin/views/{$view_path}.php" );
- }
- // include
- if ( file_exists( $view_path ) ) {
- // Use `EXTR_SKIP` here to prevent `$view_path` from being accidentally/maliciously overridden.
- extract( $view_args, EXTR_SKIP );
- include $view_path;
- }
- }
- /*
- * acf_merge_atts
- *
- * description
- *
- * @type function
- * @date 2/11/2014
- * @since 5.0.9
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_merge_atts( $atts, $extra = array() ) {
- // bail early if no $extra
- if ( empty( $extra ) ) {
- return $atts;
- }
- // trim
- $extra = array_map( 'trim', $extra );
- $extra = array_filter( $extra );
- // merge in new atts
- foreach ( $extra as $k => $v ) {
- // append
- if ( $k == 'class' || $k == 'style' ) {
- $atts[ $k ] .= ' ' . $v;
- // merge
- } else {
- $atts[ $k ] = $v;
- }
- }
- // return
- return $atts;
- }
- /*
- * acf_nonce_input
- *
- * This function will create a basic nonce input
- *
- * @type function
- * @date 24/5/17
- * @since 5.6.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_nonce_input( $nonce = '' ) {
- echo '<input type="hidden" name="_acf_nonce" value="' . wp_create_nonce( $nonce ) . '" />';
- }
- /*
- * acf_extract_var
- *
- * This function will remove the var from the array, and return the var
- *
- * @type function
- * @date 2/10/13
- * @since 5.0.0
- *
- * @param $array (array)
- * @param $key (string)
- * @return (mixed)
- */
- function acf_extract_var( &$array, $key, $default = null ) {
- // check if exists
- // - uses array_key_exists to extract NULL values (isset will fail)
- if ( is_array( $array ) && array_key_exists( $key, $array ) ) {
- // store value
- $v = $array[ $key ];
- // unset
- unset( $array[ $key ] );
- // return
- return $v;
- }
- // return
- return $default;
- }
- /*
- * acf_extract_vars
- *
- * This function will remove the vars from the array, and return the vars
- *
- * @type function
- * @date 8/10/13
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_extract_vars( &$array, $keys ) {
- $r = array();
- foreach ( $keys as $key ) {
- $r[ $key ] = acf_extract_var( $array, $key );
- }
- return $r;
- }
- /*
- * acf_get_sub_array
- *
- * This function will return a sub array of data
- *
- * @type function
- * @date 15/03/2016
- * @since 5.3.2
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_get_sub_array( $array, $keys ) {
- $r = array();
- foreach ( $keys as $key ) {
- $r[ $key ] = $array[ $key ];
- }
- return $r;
- }
- /**
- * acf_get_post_types
- *
- * Returns an array of post type names.
- *
- * @date 7/10/13
- * @since 5.0.0
- *
- * @param array $args Optional. An array of key => value arguments to match against the post type objects. Default empty array.
- * @return array A list of post type names.
- */
- function acf_get_post_types( $args = array() ) {
- // vars
- $post_types = array();
- // extract special arg
- $exclude = acf_extract_var( $args, 'exclude', array() );
- $exclude[] = 'acf-field';
- $exclude[] = 'acf-field-group';
- // get post type objects
- $objects = get_post_types( $args, 'objects' );
- // loop
- foreach ( $objects as $i => $object ) {
- // bail early if is exclude
- if ( in_array( $i, $exclude ) ) {
- continue;
- }
- // bail early if is builtin (WP) private post type
- // - nav_menu_item, revision, customize_changeset, etc
- if ( $object->_builtin && ! $object->public ) {
- continue;
- }
- // append
- $post_types[] = $i;
- }
- // filter
- $post_types = apply_filters( 'acf/get_post_types', $post_types, $args );
- // return
- return $post_types;
- }
- function acf_get_pretty_post_types( $post_types = array() ) {
- // get post types
- if ( empty( $post_types ) ) {
- // get all custom post types
- $post_types = acf_get_post_types();
- }
- // get labels
- $ref = array();
- $r = array();
- foreach ( $post_types as $post_type ) {
- // vars
- $label = acf_get_post_type_label( $post_type );
- // append to r
- $r[ $post_type ] = $label;
- // increase counter
- if ( ! isset( $ref[ $label ] ) ) {
- $ref[ $label ] = 0;
- }
- $ref[ $label ]++;
- }
- // get slugs
- foreach ( array_keys( $r ) as $i ) {
- // vars
- $post_type = $r[ $i ];
- if ( $ref[ $post_type ] > 1 ) {
- $r[ $i ] .= ' (' . $i . ')';
- }
- }
- // return
- return $r;
- }
- /*
- * acf_get_post_type_label
- *
- * This function will return a pretty label for a specific post_type
- *
- * @type function
- * @date 5/07/2016
- * @since 5.4.0
- *
- * @param $post_type (string)
- * @return (string)
- */
- function acf_get_post_type_label( $post_type ) {
- // vars
- $label = $post_type;
- // check that object exists
- // - case exists when importing field group from another install and post type does not exist
- if ( post_type_exists( $post_type ) ) {
- $obj = get_post_type_object( $post_type );
- $label = $obj->labels->singular_name;
- }
- // return
- return $label;
- }
- /*
- * acf_verify_nonce
- *
- * This function will look at the $_POST['_acf_nonce'] value and return true or false
- *
- * @type function
- * @date 15/10/13
- * @since 5.0.0
- *
- * @param $nonce (string)
- * @return (boolean)
- */
- function acf_verify_nonce( $value ) {
- // vars
- $nonce = acf_maybe_get_POST( '_acf_nonce' );
- // bail early nonce does not match (post|user|comment|term)
- if ( ! $nonce || ! wp_verify_nonce( $nonce, $value ) ) {
- return false;
- }
- // reset nonce (only allow 1 save)
- $_POST['_acf_nonce'] = false;
- // return
- return true;
- }
- /*
- * acf_verify_ajax
- *
- * This function will return true if the current AJAX request is valid
- * It's action will also allow WPML to set the lang and avoid AJAX get_posts issues
- *
- * @type function
- * @date 7/08/2015
- * @since 5.2.3
- *
- * @param n/a
- * @return (boolean)
- */
- function acf_verify_ajax() {
- // bail early if not acf nonce
- if ( empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['nonce'] ), 'acf_nonce' ) ) {
- return false;
- }
- // action for 3rd party customization
- do_action( 'acf/verify_ajax' );
- // return
- return true;
- }
- /*
- * acf_get_image_sizes
- *
- * This function will return an array of available image sizes
- *
- * @type function
- * @date 23/10/13
- * @since 5.0.0
- *
- * @param n/a
- * @return (array)
- */
- function acf_get_image_sizes() {
- // vars
- $sizes = array(
- 'thumbnail' => __( 'Thumbnail', 'acf' ),
- 'medium' => __( 'Medium', 'acf' ),
- 'large' => __( 'Large', 'acf' ),
- );
- // find all sizes
- $all_sizes = get_intermediate_image_sizes();
- // add extra registered sizes
- if ( ! empty( $all_sizes ) ) {
- foreach ( $all_sizes as $size ) {
- // bail early if already in array
- if ( isset( $sizes[ $size ] ) ) {
- continue;
- }
- // append to array
- $label = str_replace( '-', ' ', $size );
- $label = ucwords( $label );
- $sizes[ $size ] = $label;
- }
- }
- // add sizes
- foreach ( array_keys( $sizes ) as $s ) {
- // vars
- $data = acf_get_image_size( $s );
- // append
- if ( $data['width'] && $data['height'] ) {
- $sizes[ $s ] .= ' (' . $data['width'] . ' x ' . $data['height'] . ')';
- }
- }
- // add full end
- $sizes['full'] = __( 'Full Size', 'acf' );
- // filter for 3rd party customization
- $sizes = apply_filters( 'acf/get_image_sizes', $sizes );
- // return
- return $sizes;
- }
- function acf_get_image_size( $s = '' ) {
- // global
- global $_wp_additional_image_sizes;
- // rename for nicer code
- $_sizes = $_wp_additional_image_sizes;
- // vars
- $data = array(
- 'width' => isset( $_sizes[ $s ]['width'] ) ? $_sizes[ $s ]['width'] : get_option( "{$s}_size_w" ),
- 'height' => isset( $_sizes[ $s ]['height'] ) ? $_sizes[ $s ]['height'] : get_option( "{$s}_size_h" ),
- );
- // return
- return $data;
- }
- /**
- * acf_version_compare
- *
- * Similar to the version_compare() function but with extra functionality.
- *
- * @date 21/11/16
- * @since 5.5.0
- *
- * @param string $left The left version number.
- * @param string $compare The compare operator.
- * @param string $right The right version number.
- * @return bool
- */
- function acf_version_compare( $left = '', $compare = '>', $right = '' ) {
- // Detect 'wp' placeholder.
- if ( $left === 'wp' ) {
- global $wp_version;
- $left = $wp_version;
- }
- // Return result.
- return version_compare( $left, $right, $compare );
- }
- /*
- * acf_get_full_version
- *
- * This function will remove any '-beta1' or '-RC1' strings from a version
- *
- * @type function
- * @date 24/11/16
- * @since 5.5.0
- *
- * @param $version (string)
- * @return (string)
- */
- function acf_get_full_version( $version = '1' ) {
- // remove '-beta1' or '-RC1'
- if ( $pos = strpos( $version, '-' ) ) {
- $version = substr( $version, 0, $pos );
- }
- // return
- return $version;
- }
- /*
- * acf_get_terms
- *
- * This function is a wrapper for the get_terms() function
- *
- * @type function
- * @date 28/09/2016
- * @since 5.4.0
- *
- * @param $args (array)
- * @return (array)
- */
- function acf_get_terms( $args ) {
- // defaults
- $args = wp_parse_args(
- $args,
- array(
- 'taxonomy' => null,
- 'hide_empty' => false,
- 'update_term_meta_cache' => false,
- )
- );
- // parameters changed in version 4.5
- if ( acf_version_compare( 'wp', '<', '4.5' ) ) {
- return get_terms( $args['taxonomy'], $args );
- }
- // return
- return get_terms( $args );
- }
- /*
- * acf_get_taxonomy_terms
- *
- * This function will return an array of available taxonomy terms
- *
- * @type function
- * @date 7/10/13
- * @since 5.0.0
- *
- * @param $taxonomies (array)
- * @return (array)
- */
- function acf_get_taxonomy_terms( $taxonomies = array() ) {
- // force array
- $taxonomies = acf_get_array( $taxonomies );
- // get pretty taxonomy names
- $taxonomies = acf_get_pretty_taxonomies( $taxonomies );
- // vars
- $r = array();
- // populate $r
- foreach ( array_keys( $taxonomies ) as $taxonomy ) {
- // vars
- $label = $taxonomies[ $taxonomy ];
- $is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
- $terms = acf_get_terms(
- array(
- 'taxonomy' => $taxonomy,
- 'hide_empty' => false,
- )
- );
- // bail early i no terms
- if ( empty( $terms ) ) {
- continue;
- }
- // sort into hierachial order!
- if ( $is_hierarchical ) {
- $terms = _get_term_children( 0, $terms, $taxonomy );
- }
- // add placeholder
- $r[ $label ] = array();
- // add choices
- foreach ( $terms as $term ) {
- $k = "{$taxonomy}:{$term->slug}";
- $r[ $label ][ $k ] = acf_get_term_title( $term );
- }
- }
- // return
- return $r;
- }
- /*
- * acf_decode_taxonomy_terms
- *
- * This function decodes the $taxonomy:$term strings into a nested array
- *
- * @type function
- * @date 27/02/2014
- * @since 5.0.0
- *
- * @param $terms (array)
- * @return (array)
- */
- function acf_decode_taxonomy_terms( $strings = false ) {
- // bail early if no terms
- if ( empty( $strings ) ) {
- return false;
- }
- // vars
- $terms = array();
- // loop
- foreach ( $strings as $string ) {
- // vars
- $data = acf_decode_taxonomy_term( $string );
- $taxonomy = $data['taxonomy'];
- $term = $data['term'];
- // create empty array
- if ( ! isset( $terms[ $taxonomy ] ) ) {
- $terms[ $taxonomy ] = array();
- }
- // append
- $terms[ $taxonomy ][] = $term;
- }
- // return
- return $terms;
- }
- /*
- * acf_decode_taxonomy_term
- *
- * This function will return the taxonomy and term slug for a given value
- *
- * @type function
- * @date 31/03/2014
- * @since 5.0.0
- *
- * @param $string (string)
- * @return (array)
- */
- function acf_decode_taxonomy_term( $value ) {
- // vars
- $data = array(
- 'taxonomy' => '',
- 'term' => '',
- );
- // int
- if ( is_numeric( $value ) ) {
- $data['term'] = $value;
- // string
- } elseif ( is_string( $value ) ) {
- $value = explode( ':', $value );
- $data['taxonomy'] = isset( $value[0] ) ? $value[0] : '';
- $data['term'] = isset( $value[1] ) ? $value[1] : '';
- // error
- } else {
- return false;
- }
- // allow for term_id (Used by ACF v4)
- if ( is_numeric( $data['term'] ) ) {
- // global
- global $wpdb;
- // find taxonomy
- if ( ! $data['taxonomy'] ) {
- $data['taxonomy'] = $wpdb->get_var( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d LIMIT 1", $data['term'] ) );
- }
- // find term (may have numeric slug '123')
- $term = get_term_by( 'slug', $data['term'], $data['taxonomy'] );
- // attempt get term via ID (ACF4 uses ID)
- if ( ! $term ) {
- $term = get_term( $data['term'], $data['taxonomy'] );
- }
- // bail early if no term
- if ( ! $term ) {
- return false;
- }
- // update
- $data['taxonomy'] = $term->taxonomy;
- $data['term'] = $term->slug;
- }
- // return
- return $data;
- }
- /**
- * acf_array
- *
- * Casts the value into an array.
- *
- * @date 9/1/19
- * @since 5.7.10
- *
- * @param mixed $val The value to cast.
- * @return array
- */
- function acf_array( $val = array() ) {
- return (array) $val;
- }
- /**
- * Returns a non-array value.
- *
- * @date 11/05/2020
- * @since 5.8.10
- *
- * @param mixed $val The value to review.
- * @return mixed
- */
- function acf_unarray( $val ) {
- if ( is_array( $val ) ) {
- return reset( $val );
- }
- return $val;
- }
- /*
- * acf_get_array
- *
- * This function will force a variable to become an array
- *
- * @type function
- * @date 4/02/2014
- * @since 5.0.0
- *
- * @param $var (mixed)
- * @return (array)
- */
- function acf_get_array( $var = false, $delimiter = '' ) {
- // array
- if ( is_array( $var ) ) {
- return $var;
- }
- // bail early if empty
- if ( acf_is_empty( $var ) ) {
- return array();
- }
- // string
- if ( is_string( $var ) && $delimiter ) {
- return explode( $delimiter, $var );
- }
- // place in array
- return (array) $var;
- }
- /*
- * acf_get_numeric
- *
- * This function will return numeric values
- *
- * @type function
- * @date 15/07/2016
- * @since 5.4.0
- *
- * @param $value (mixed)
- * @return (mixed)
- */
- function acf_get_numeric( $value = '' ) {
- // vars
- $numbers = array();
- $is_array = is_array( $value );
- // loop
- foreach ( (array) $value as $v ) {
- if ( is_numeric( $v ) ) {
- $numbers[] = (int) $v;
- }
- }
- // bail early if is empty
- if ( empty( $numbers ) ) {
- return false;
- }
- // convert array
- if ( ! $is_array ) {
- $numbers = $numbers[0];
- }
- // return
- return $numbers;
- }
- /**
- * acf_get_posts
- *
- * Similar to the get_posts() function but with extra functionality.
- *
- * @date 3/03/15
- * @since 5.1.5
- *
- * @param array $args The query args.
- * @return array
- */
- function acf_get_posts( $args = array() ) {
- // Vars.
- $posts = array();
- // Apply default args.
- $args = wp_parse_args(
- $args,
- array(
- 'posts_per_page' => -1,
- 'post_type' => '',
- 'post_status' => 'any',
- 'update_post_meta_cache' => false,
- 'update_post_term_cache' => false,
- )
- );
- // Avoid default 'post' post_type by providing all public types.
- if ( ! $args['post_type'] ) {
- $args['post_type'] = acf_get_post_types();
- }
- // Check if specifc post ID's have been provided.
- if ( $args['post__in'] ) {
- // Clean value into an array of IDs.
- $args['post__in'] = array_map( 'intval', acf_array( $args['post__in'] ) );
- }
- // Query posts.
- $posts = get_posts( $args );
- // Remove any potential empty results.
- $posts = array_filter( $posts );
- // Manually order results.
- if ( $posts && $args['post__in'] ) {
- $order = array();
- foreach ( $posts as $i => $post ) {
- $order[ $i ] = array_search( $post->ID, $args['post__in'] );
- }
- array_multisort( $order, $posts );
- }
- // Return posts.
- return $posts;
- }
- /*
- * _acf_query_remove_post_type
- *
- * This function will remove the 'wp_posts.post_type' WHERE clause completely
- * When using 'post__in', this clause is unneccessary and slow.
- *
- * @type function
- * @date 4/03/2015
- * @since 5.1.5
- *
- * @param $sql (string)
- * @return $sql
- */
- function _acf_query_remove_post_type( $sql ) {
- // global
- global $wpdb;
- // bail early if no 'wp_posts.ID IN'
- if ( strpos( $sql, "$wpdb->posts.ID IN" ) === false ) {
- return $sql;
- }
- // get bits
- $glue = 'AND';
- $bits = explode( $glue, $sql );
- // loop through $where and remove any post_type queries
- foreach ( $bits as $i => $bit ) {
- if ( strpos( $bit, "$wpdb->posts.post_type" ) !== false ) {
- unset( $bits[ $i ] );
- }
- }
- // join $where back together
- $sql = implode( $glue, $bits );
- // return
- return $sql;
- }
- /*
- * acf_get_grouped_posts
- *
- * This function will return all posts grouped by post_type
- * This is handy for select settings
- *
- * @type function
- * @date 27/02/2014
- * @since 5.0.0
- *
- * @param $args (array)
- * @return (array)
- */
- function acf_get_grouped_posts( $args ) {
- // vars
- $data = array();
- // defaults
- $args = wp_parse_args(
- $args,
- array(
- 'posts_per_page' => -1,
- 'paged' => 0,
- 'post_type' => 'post',
- 'orderby' => 'menu_order title',
- 'order' => 'ASC',
- 'post_status' => 'any',
- 'suppress_filters' => false,
- 'update_post_meta_cache' => false,
- )
- );
- // find array of post_type
- $post_types = acf_get_array( $args['post_type'] );
- $post_types_labels = acf_get_pretty_post_types( $post_types );
- $is_single_post_type = ( count( $post_types ) == 1 );
- // attachment doesn't work if it is the only item in an array
- if ( $is_single_post_type ) {
- $args['post_type'] = reset( $post_types );
- }
- // add filter to orderby post type
- if ( ! $is_single_post_type ) {
- add_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
- }
- // get posts
- $posts = get_posts( $args );
- // remove this filter (only once)
- if ( ! $is_single_post_type ) {
- remove_filter( 'posts_orderby', '_acf_orderby_post_type', 10, 2 );
- }
- // loop
- foreach ( $post_types as $post_type ) {
- // vars
- $this_posts = array();
- $this_group = array();
- // populate $this_posts
- foreach ( $posts as $post ) {
- if ( $post->post_type == $post_type ) {
- $this_posts[] = $post;
- }
- }
- // bail early if no posts for this post type
- if ( empty( $this_posts ) ) {
- continue;
- }
- // sort into hierachial order!
- // this will fail if a search has taken place because parents wont exist
- if ( is_post_type_hierarchical( $post_type ) && empty( $args['s'] ) ) {
- // vars
- $post_id = $this_posts[0]->ID;
- $parent_id = acf_maybe_get( $args, 'post_parent', 0 );
- $offset = 0;
- $length = count( $this_posts );
- // get all posts from this post type
- $all_posts = get_posts(
- array_merge(
- $args,
- array(
- 'posts_per_page' => -1,
- 'paged' => 0,
- 'post_type' => $post_type,
- )
- )
- );
- // find starting point (offset)
- foreach ( $all_posts as $i => $post ) {
- if ( $post->ID == $post_id ) {
- $offset = $i;
- break;
- }
- }
- // order posts
- $ordered_posts = get_page_children( $parent_id, $all_posts );
- // compare aray lengths
- // if $ordered_posts is smaller than $all_posts, WP has lost posts during the get_page_children() function
- // this is possible when get_post( $args ) filter out parents (via taxonomy, meta and other search parameters)
- if ( count( $ordered_posts ) == count( $all_posts ) ) {
- $this_posts = array_slice( $ordered_posts, $offset, $length );
- }
- }
- // populate $this_posts
- foreach ( $this_posts as $post ) {
- $this_group[ $post->ID ] = $post;
- }
- // group by post type
- $label = $post_types_labels[ $post_type ];
- $data[ $label ] = $this_group;
- }
- // return
- return $data;
- }
- function _acf_orderby_post_type( $ordeby, $wp_query ) {
- // global
- global $wpdb;
- // get post types
- $post_types = $wp_query->get( 'post_type' );
- // prepend SQL
- if ( is_array( $post_types ) ) {
- $post_types = implode( "','", $post_types );
- $ordeby = "FIELD({$wpdb->posts}.post_type,'$post_types')," . $ordeby;
- }
- // return
- return $ordeby;
- }
- function acf_get_post_title( $post = 0, $is_search = false ) {
- // vars
- $post = get_post( $post );
- $title = '';
- $prepend = '';
- $append = '';
- // bail early if no post
- if ( ! $post ) {
- return '';
- }
- // title
- $title = get_the_title( $post->ID );
- // empty
- if ( $title === '' ) {
- $title = __( '(no title)', 'acf' );
- }
- // status
- if ( get_post_status( $post->ID ) != 'publish' ) {
- $append .= ' (' . get_post_status( $post->ID ) . ')';
- }
- // ancestors
- if ( $post->post_type !== 'attachment' ) {
- // get ancestors
- $ancestors = get_ancestors( $post->ID, $post->post_type );
- $prepend .= str_repeat( '- ', count( $ancestors ) );
- // add parent
- /*
- removed in 5.6.5 as not used by the UI
- if( $is_search && !empty($ancestors) ) {
- // reverse
- $ancestors = array_reverse($ancestors);
- // convert id's into titles
- foreach( $ancestors as $i => $id ) {
- $ancestors[ $i ] = get_the_title( $id );
- }
- // append
- $append .= ' | ' . __('Parent', 'acf') . ': ' . implode(' / ', $ancestors);
- }
- */
- }
- // merge
- $title = $prepend . $title . $append;
- // return
- return $title;
- }
- function acf_order_by_search( $array, $search ) {
- // vars
- $weights = array();
- $needle = strtolower( $search );
- // add key prefix
- foreach ( array_keys( $array ) as $k ) {
- $array[ '_' . $k ] = acf_extract_var( $array, $k );
- }
- // add search weight
- foreach ( $array as $k => $v ) {
- // vars
- $weight = 0;
- $haystack = strtolower( $v );
- $strpos = strpos( $haystack, $needle );
- // detect search match
- if ( $strpos !== false ) {
- // set eright to length of match
- $weight = strlen( $search );
- // increase weight if match starts at begining of string
- if ( $strpos == 0 ) {
- $weight++;
- }
- }
- // append to wights
- $weights[ $k ] = $weight;
- }
- // sort the array with menu_order ascending
- array_multisort( $weights, SORT_DESC, $array );
- // remove key prefix
- foreach ( array_keys( $array ) as $k ) {
- $array[ substr( $k, 1 ) ] = acf_extract_var( $array, $k );
- }
- // return
- return $array;
- }
- /*
- * acf_get_pretty_user_roles
- *
- * description
- *
- * @type function
- * @date 23/02/2016
- * @since 5.3.2
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_get_pretty_user_roles( $allowed = false ) {
- // vars
- $editable_roles = get_editable_roles();
- $allowed = acf_get_array( $allowed );
- $roles = array();
- // loop
- foreach ( $editable_roles as $role_name => $role_details ) {
- // bail early if not allowed
- if ( ! empty( $allowed ) && ! in_array( $role_name, $allowed ) ) {
- continue;
- }
- // append
- $roles[ $role_name ] = translate_user_role( $role_details['name'] );
- }
- // return
- return $roles;
- }
- /*
- * acf_get_grouped_users
- *
- * This function will return all users grouped by role
- * This is handy for select settings
- *
- * @type function
- * @date 27/02/2014
- * @since 5.0.0
- *
- * @param $args (array)
- * @return (array)
- */
- function acf_get_grouped_users( $args = array() ) {
- // vars
- $r = array();
- // defaults
- $args = wp_parse_args(
- $args,
- array(
- 'users_per_page' => -1,
- 'paged' => 0,
- 'role' => '',
- 'orderby' => 'login',
- 'order' => 'ASC',
- )
- );
- // offset
- $i = 0;
- $min = 0;
- $max = 0;
- $users_per_page = acf_extract_var( $args, 'users_per_page' );
- $paged = acf_extract_var( $args, 'paged' );
- if ( $users_per_page > 0 ) {
- // prevent paged from being -1
- $paged = max( 0, $paged );
- // set min / max
- $min = ( ( $paged - 1 ) * $users_per_page ) + 1; // 1, 11
- $max = ( $paged * $users_per_page ); // 10, 20
- }
- // find array of post_type
- $user_roles = acf_get_pretty_user_roles( $args['role'] );
- // fix role
- if ( is_array( $args['role'] ) ) {
- // global
- global $wp_version, $wpdb;
- // vars
- $roles = acf_extract_var( $args, 'role' );
- // new WP has role__in
- if ( version_compare( $wp_version, '4.4', '>=' ) ) {
- $args['role__in'] = $roles;
- // old WP doesn't have role__in
- } else {
- // vars
- $blog_id = get_current_blog_id();
- $meta_query = array( 'relation' => 'OR' );
- // loop
- foreach ( $roles as $role ) {
- $meta_query[] = array(
- 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
- 'value' => '"' . $role . '"',
- 'compare' => 'LIKE',
- );
- }
- // append
- $args['meta_query'] = $meta_query;
- }
- }
- // get posts
- $users = get_users( $args );
- // loop
- foreach ( $user_roles as $user_role_name => $user_role_label ) {
- // vars
- $this_users = array();
- $this_group = array();
- // populate $this_posts
- foreach ( array_keys( $users ) as $key ) {
- // bail early if not correct role
- if ( ! in_array( $user_role_name, $users[ $key ]->roles ) ) {
- continue;
- }
- // extract user
- $user = acf_extract_var( $users, $key );
- // increase
- $i++;
- // bail early if too low
- if ( $min && $i < $min ) {
- continue;
- }
- // bail early if too high (don't bother looking at any more users)
- if ( $max && $i > $max ) {
- break;
- }
- // group by post type
- $this_users[ $user->ID ] = $user;
- }
- // bail early if no posts for this post type
- if ( empty( $this_users ) ) {
- continue;
- }
- // append
- $r[ $user_role_label ] = $this_users;
- }
- // return
- return $r;
- }
- /**
- * acf_json_encode
- *
- * Returns json_encode() ready for file / database use.
- *
- * @date 29/4/19
- * @since 5.0.0
- *
- * @param array $json The array of data to encode.
- * @return string
- */
- function acf_json_encode( $json ) {
- return json_encode( $json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
- }
- /*
- * acf_str_exists
- *
- * This function will return true if a sub string is found
- *
- * @type function
- * @date 1/05/2014
- * @since 5.0.0
- *
- * @param $needle (string)
- * @param $haystack (string)
- * @return (boolean)
- */
- function acf_str_exists( $needle, $haystack ) {
- // return true if $haystack contains the $needle
- if ( is_string( $haystack ) && strpos( $haystack, $needle ) !== false ) {
- return true;
- }
- // return
- return false;
- }
- /*
- * acf_debug
- *
- * description
- *
- * @type function
- * @date 2/05/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_debug() {
- // vars
- $args = func_get_args();
- $s = array_shift( $args );
- $o = '';
- $nl = "\r\n";
- // start script
- $o .= '<script type="text/javascript">' . $nl;
- $o .= 'console.log("' . $s . '"';
- if ( ! empty( $args ) ) {
- foreach ( $args as $arg ) {
- if ( is_object( $arg ) || is_array( $arg ) ) {
- $arg = json_encode( $arg );
- } elseif ( is_bool( $arg ) ) {
- $arg = $arg ? 'true' : 'false';
- } elseif ( is_string( $arg ) ) {
- $arg = '"' . $arg . '"';
- }
- $o .= ', ' . $arg;
- }
- }
- $o .= ');' . $nl;
- // end script
- $o .= '</script>' . $nl;
- // echo
- echo $o;
- }
- function acf_debug_start() {
- acf_update_setting( 'debug_start', memory_get_usage() );
- }
- function acf_debug_end() {
- $start = acf_get_setting( 'debug_start' );
- $end = memory_get_usage();
- return $end - $start;
- }
- /*
- * acf_encode_choices
- *
- * description
- *
- * @type function
- * @date 4/06/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_encode_choices( $array = array(), $show_keys = true ) {
- // bail early if not array (maybe a single string)
- if ( ! is_array( $array ) ) {
- return $array;
- }
- // bail early if empty array
- if ( empty( $array ) ) {
- return '';
- }
- // vars
- $string = '';
- // if allowed to show keys (good for choices, not for default values)
- if ( $show_keys ) {
- // loop
- foreach ( $array as $k => $v ) {
- // ignore if key and value are the same
- if ( strval( $k ) == strval( $v ) ) {
- continue;
- }
- // show key in the value
- $array[ $k ] = $k . ' : ' . $v;
- }
- }
- // implode
- $string = implode( "\n", $array );
- // return
- return $string;
- }
- function acf_decode_choices( $string = '', $array_keys = false ) {
- // bail early if already array
- if ( is_array( $string ) ) {
- return $string;
- // allow numeric values (same as string)
- } elseif ( is_numeric( $string ) ) {
- // do nothing
- // bail early if not a string
- } elseif ( ! is_string( $string ) ) {
- return array();
- // bail early if is empty string
- } elseif ( $string === '' ) {
- return array();
- }
- // vars
- $array = array();
- // explode
- $lines = explode( "\n", $string );
- // key => value
- foreach ( $lines as $line ) {
- // vars
- $k = trim( $line );
- $v = trim( $line );
- // look for ' : '
- if ( acf_str_exists( ' : ', $line ) ) {
- $line = explode( ' : ', $line );
- $k = trim( $line[0] );
- $v = trim( $line[1] );
- }
- // append
- $array[ $k ] = $v;
- }
- // return only array keys? (good for checkbox default_value)
- if ( $array_keys ) {
- return array_keys( $array );
- }
- // return
- return $array;
- }
- /*
- * acf_str_replace
- *
- * This function will replace an array of strings much like str_replace
- * The difference is the extra logic to avoid replacing a string that has alread been replaced
- * This is very useful for replacing date characters as they overlap with eachother
- *
- * @type function
- * @date 21/06/2016
- * @since 5.3.8
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_str_replace( $string = '', $search_replace = array() ) {
- // vars
- $ignore = array();
- // remove potential empty search to avoid PHP error
- unset( $search_replace[''] );
- // loop over conversions
- foreach ( $search_replace as $search => $replace ) {
- // ignore this search, it was a previous replace
- if ( in_array( $search, $ignore ) ) {
- continue;
- }
- // bail early if subsctring not found
- if ( strpos( $string, $search ) === false ) {
- continue;
- }
- // replace
- $string = str_replace( $search, $replace, $string );
- // append to ignore
- $ignore[] = $replace;
- }
- // return
- return $string;
- }
- /*
- * date & time formats
- *
- * These settings contain an association of format strings from PHP => JS
- *
- * @type function
- * @date 21/06/2016
- * @since 5.3.8
- *
- * @param n/a
- * @return n/a
- */
- acf_update_setting(
- 'php_to_js_date_formats',
- array(
- // Year
- 'Y' => 'yy', // Numeric, 4 digits 1999, 2003
- 'y' => 'y', // Numeric, 2 digits 99, 03
- // Month
- 'm' => 'mm', // Numeric, with leading zeros 01–12
- 'n' => 'm', // Numeric, without leading zeros 1–12
- 'F' => 'MM', // Textual full January – December
- 'M' => 'M', // Textual three letters Jan - Dec
- // Weekday
- 'l' => 'DD', // Full name (lowercase 'L') Sunday – Saturday
- 'D' => 'D', // Three letter name Mon – Sun
- // Day of Month
- 'd' => 'dd', // Numeric, with leading zeros 01–31
- 'j' => 'd', // Numeric, without leading zeros 1–31
- 'S' => '', // The English suffix for the day of the month st, nd or th in the 1st, 2nd or 15th.
- )
- );
- acf_update_setting(
- 'php_to_js_time_formats',
- array(
- 'a' => 'tt', // Lowercase Ante meridiem and Post meridiem am or pm
- 'A' => 'TT', // Uppercase Ante meridiem and Post meridiem AM or PM
- 'h' => 'hh', // 12-hour format of an hour with leading zeros 01 through 12
- 'g' => 'h', // 12-hour format of an hour without leading zeros 1 through 12
- 'H' => 'HH', // 24-hour format of an hour with leading zeros 00 through 23
- 'G' => 'H', // 24-hour format of an hour without leading zeros 0 through 23
- 'i' => 'mm', // Minutes with leading zeros 00 to 59
- 's' => 'ss', // Seconds, with leading zeros 00 through 59
- )
- );
- /*
- * acf_split_date_time
- *
- * This function will split a format string into seperate date and time
- *
- * @type function
- * @date 26/05/2016
- * @since 5.3.8
- *
- * @param $date_time (string)
- * @return $formats (array)
- */
- function acf_split_date_time( $date_time = '' ) {
- // vars
- $php_date = acf_get_setting( 'php_to_js_date_formats' );
- $php_time = acf_get_setting( 'php_to_js_time_formats' );
- $chars = str_split( $date_time );
- $type = 'date';
- // default
- $data = array(
- 'date' => '',
- 'time' => '',
- );
- // loop
- foreach ( $chars as $i => $c ) {
- // find type
- // - allow misc characters to append to previous type
- if ( isset( $php_date[ $c ] ) ) {
- $type = 'date';
- } elseif ( isset( $php_time[ $c ] ) ) {
- $type = 'time';
- }
- // append char
- $data[ $type ] .= $c;
- }
- // trim
- $data['date'] = trim( $data['date'] );
- $data['time'] = trim( $data['time'] );
- // return
- return $data;
- }
- /*
- * acf_convert_date_to_php
- *
- * This fucntion converts a date format string from JS to PHP
- *
- * @type function
- * @date 20/06/2014
- * @since 5.0.0
- *
- * @param $date (string)
- * @return (string)
- */
- function acf_convert_date_to_php( $date = '' ) {
- // vars
- $php_to_js = acf_get_setting( 'php_to_js_date_formats' );
- $js_to_php = array_flip( $php_to_js );
- // return
- return acf_str_replace( $date, $js_to_php );
- }
- /*
- * acf_convert_date_to_js
- *
- * This fucntion converts a date format string from PHP to JS
- *
- * @type function
- * @date 20/06/2014
- * @since 5.0.0
- *
- * @param $date (string)
- * @return (string)
- */
- function acf_convert_date_to_js( $date = '' ) {
- // vars
- $php_to_js = acf_get_setting( 'php_to_js_date_formats' );
- // return
- return acf_str_replace( $date, $php_to_js );
- }
- /*
- * acf_convert_time_to_php
- *
- * This fucntion converts a time format string from JS to PHP
- *
- * @type function
- * @date 20/06/2014
- * @since 5.0.0
- *
- * @param $time (string)
- * @return (string)
- */
- function acf_convert_time_to_php( $time = '' ) {
- // vars
- $php_to_js = acf_get_setting( 'php_to_js_time_formats' );
- $js_to_php = array_flip( $php_to_js );
- // return
- return acf_str_replace( $time, $js_to_php );
- }
- /*
- * acf_convert_time_to_js
- *
- * This fucntion converts a date format string from PHP to JS
- *
- * @type function
- * @date 20/06/2014
- * @since 5.0.0
- *
- * @param $time (string)
- * @return (string)
- */
- function acf_convert_time_to_js( $time = '' ) {
- // vars
- $php_to_js = acf_get_setting( 'php_to_js_time_formats' );
- // return
- return acf_str_replace( $time, $php_to_js );
- }
- /*
- * acf_update_user_setting
- *
- * description
- *
- * @type function
- * @date 15/07/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_update_user_setting( $name, $value ) {
- // get current user id
- $user_id = get_current_user_id();
- // get user settings
- $settings = get_user_meta( $user_id, 'acf_user_settings', true );
- // ensure array
- $settings = acf_get_array( $settings );
- // delete setting (allow 0 to save)
- if ( acf_is_empty( $value ) ) {
- unset( $settings[ $name ] );
- // append setting
- } else {
- $settings[ $name ] = $value;
- }
- // update user data
- return update_metadata( 'user', $user_id, 'acf_user_settings', $settings );
- }
- /*
- * acf_get_user_setting
- *
- * description
- *
- * @type function
- * @date 15/07/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_get_user_setting( $name = '', $default = false ) {
- // get current user id
- $user_id = get_current_user_id();
- // get user settings
- $settings = get_user_meta( $user_id, 'acf_user_settings', true );
- // ensure array
- $settings = acf_get_array( $settings );
- // bail arly if no settings
- if ( ! isset( $settings[ $name ] ) ) {
- return $default;
- }
- // return
- return $settings[ $name ];
- }
- /*
- * acf_in_array
- *
- * description
- *
- * @type function
- * @date 22/07/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_in_array( $value = '', $array = false ) {
- // bail early if not array
- if ( ! is_array( $array ) ) {
- return false;
- }
- // find value in array
- return in_array( $value, $array );
- }
- /*
- * acf_get_valid_post_id
- *
- * This function will return a valid post_id based on the current screen / parameter
- *
- * @type function
- * @date 8/12/2013
- * @since 5.0.0
- *
- * @param $post_id (mixed)
- * @return $post_id (mixed)
- */
- function acf_get_valid_post_id( $post_id = 0 ) {
- // allow filter to short-circuit load_value logic
- $preload = apply_filters( 'acf/pre_load_post_id', null, $post_id );
- if ( $preload !== null ) {
- return $preload;
- }
- // vars
- $_post_id = $post_id;
- // if not $post_id, load queried object
- if ( ! $post_id ) {
- // try for global post (needed for setup_postdata)
- $post_id = (int) get_the_ID();
- // try for current screen
- if ( ! $post_id ) {
- $post_id = get_queried_object();
- }
- }
- // $post_id may be an object.
- // todo: Compare class types instead.
- if ( is_object( $post_id ) ) {
- // post
- if ( isset( $post_id->post_type, $post_id->ID ) ) {
- $post_id = $post_id->ID;
- // user
- } elseif ( isset( $post_id->roles, $post_id->ID ) ) {
- $post_id = 'user_' . $post_id->ID;
- // term
- } elseif ( isset( $post_id->taxonomy, $post_id->term_id ) ) {
- $post_id = 'term_' . $post_id->term_id;
- // comment
- } elseif ( isset( $post_id->comment_ID ) ) {
- $post_id = 'comment_' . $post_id->comment_ID;
- // default
- } else {
- $post_id = 0;
- }
- }
- // allow for option == options
- if ( $post_id === 'option' ) {
- $post_id = 'options';
- }
- // append language code
- if ( $post_id == 'options' ) {
- $dl = acf_get_setting( 'default_language' );
- $cl = acf_get_setting( 'current_language' );
- if ( $cl && $cl !== $dl ) {
- $post_id .= '_' . $cl;
- }
- }
- // filter for 3rd party
- $post_id = apply_filters( 'acf/validate_post_id', $post_id, $_post_id );
- // return
- return $post_id;
- }
- /*
- * acf_get_post_id_info
- *
- * This function will return the type and id for a given $post_id string
- *
- * @type function
- * @date 2/07/2016
- * @since 5.4.0
- *
- * @param $post_id (mixed)
- * @return $info (array)
- */
- function acf_get_post_id_info( $post_id = 0 ) {
- // vars
- $info = array(
- 'type' => 'post',
- 'id' => 0,
- );
- // bail early if no $post_id
- if ( ! $post_id ) {
- return $info;
- }
- // check cache
- // - this function will most likely be called multiple times (saving loading fields from post)
- // $cache_key = "get_post_id_info/post_id={$post_id}";
- // if( acf_isset_cache($cache_key) ) return acf_get_cache($cache_key);
- // numeric
- if ( is_numeric( $post_id ) ) {
- $info['id'] = (int) $post_id;
- // string
- } elseif ( is_string( $post_id ) ) {
- // vars
- $glue = '_';
- $type = explode( $glue, $post_id );
- $id = array_pop( $type );
- $type = implode( $glue, $type );
- $meta = array( 'post', 'user', 'comment', 'term' );
- // check if is taxonomy (ACF < 5.5)
- // - avoid scenario where taxonomy exists with name of meta type
- if ( ! in_array( $type, $meta ) && acf_isset_termmeta( $type ) ) {
- $type = 'term';
- }
- // meta
- if ( is_numeric( $id ) && in_array( $type, $meta ) ) {
- $info['type'] = $type;
- $info['id'] = (int) $id;
- // option
- } else {
- $info['type'] = 'option';
- $info['id'] = $post_id;
- }
- }
- // update cache
- // acf_set_cache($cache_key, $info);
- // filter
- $info = apply_filters( 'acf/get_post_id_info', $info, $post_id );
- // return
- return $info;
- }
- /*
- acf_log( acf_get_post_id_info(4) );
- acf_log( acf_get_post_id_info('post_4') );
- acf_log( acf_get_post_id_info('user_123') );
- acf_log( acf_get_post_id_info('term_567') );
- acf_log( acf_get_post_id_info('category_204') );
- acf_log( acf_get_post_id_info('comment_6') );
- acf_log( acf_get_post_id_info('options_lol!') );
- acf_log( acf_get_post_id_info('option') );
- acf_log( acf_get_post_id_info('options') );
- */
- /*
- * acf_isset_termmeta
- *
- * This function will return true if the termmeta table exists
- * https://developer.wordpress.org/reference/functions/get_term_meta/
- *
- * @type function
- * @date 3/09/2016
- * @since 5.4.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_isset_termmeta( $taxonomy = '' ) {
- // bail early if no table
- if ( get_option( 'db_version' ) < 34370 ) {
- return false;
- }
- // check taxonomy
- if ( $taxonomy && ! taxonomy_exists( $taxonomy ) ) {
- return false;
- }
- // return
- return true;
- }
- /**
- * This function will walk through the $_FILES data and upload each found.
- *
- * @date 25/10/2014
- * @since 5.0.9
- *
- * @param array $ancestors An internal parameter, not required.
- * @return void
- */
- function acf_upload_files( $ancestors = array() ) {
- $file = acf_sanitize_files_array( $_FILES['acf'] );
- // walk through ancestors
- if ( ! empty( $ancestors ) ) {
- foreach ( $ancestors as $a ) {
- foreach ( array_keys( $file ) as $k ) {
- $file[ $k ] = $file[ $k ][ $a ];
- }
- }
- }
- // is array?
- if ( is_array( $file['name'] ) ) {
- foreach ( array_keys( $file['name'] ) as $k ) {
- $_ancestors = array_merge( $ancestors, array( $k ) );
- acf_upload_files( $_ancestors );
- }
- return;
- }
- // Bail early if file has error (no file uploaded).
- if ( $file['error'] ) {
- return;
- }
- $field_key = end( $ancestors );
- $nonce_name = $field_key . '_file_nonce';
- if ( empty( $_REQUEST['acf'][ $nonce_name ] ) || ! wp_verify_nonce( sanitize_text_field( $_REQUEST['acf'][ $nonce_name ] ), 'acf/file_uploader_nonce/' . $field_key ) ) {
- return;
- }
- // Assign global _acfuploader for media validation.
- $_POST['_acfuploader'] = $field_key;
- // file found!
- $attachment_id = acf_upload_file( $file );
- // update $_POST
- array_unshift( $ancestors, 'acf' );
- acf_update_nested_array( $_POST, $ancestors, $attachment_id );
- }
- /*
- * acf_upload_file
- *
- * This function will uploade a $_FILE
- *
- * @type function
- * @date 27/10/2014
- * @since 5.0.9
- *
- * @param $uploaded_file (array) array found from $_FILE data
- * @return $id (int) new attachment ID
- */
- function acf_upload_file( $uploaded_file ) {
- // required
- // require_once( ABSPATH . "/wp-load.php" ); // WP should already be loaded
- require_once ABSPATH . '/wp-admin/includes/media.php'; // video functions
- require_once ABSPATH . '/wp-admin/includes/file.php';
- require_once ABSPATH . '/wp-admin/includes/image.php';
- // required for wp_handle_upload() to upload the file
- $upload_overrides = array( 'test_form' => false );
- // upload
- $file = wp_handle_upload( $uploaded_file, $upload_overrides );
- // bail early if upload failed
- if ( isset( $file['error'] ) ) {
- return $file['error'];
- }
- // vars
- $url = $file['url'];
- $type = $file['type'];
- $file = $file['file'];
- $filename = basename( $file );
- // Construct the object array
- $object = array(
- 'post_title' => $filename,
- 'post_mime_type' => $type,
- 'guid' => $url,
- );
- // Save the data
- $id = wp_insert_attachment( $object, $file );
- // Add the meta-data
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
- /** This action is documented in wp-admin/custom-header.php */
- do_action( 'wp_create_file_in_uploads', $file, $id ); // For replication
- // return new ID
- return $id;
- }
- /*
- * acf_update_nested_array
- *
- * This function will update a nested array value. Useful for modifying the $_POST array
- *
- * @type function
- * @date 27/10/2014
- * @since 5.0.9
- *
- * @param $array (array) target array to be updated
- * @param $ancestors (array) array of keys to navigate through to find the child
- * @param $value (mixed) The new value
- * @return (boolean)
- */
- function acf_update_nested_array( &$array, $ancestors, $value ) {
- // if no more ancestors, update the current var
- if ( empty( $ancestors ) ) {
- $array = $value;
- // return
- return true;
- }
- // shift the next ancestor from the array
- $k = array_shift( $ancestors );
- // if exists
- if ( isset( $array[ $k ] ) ) {
- return acf_update_nested_array( $array[ $k ], $ancestors, $value );
- }
- // return
- return false;
- }
- /*
- * acf_is_screen
- *
- * This function will return true if all args are matched for the current screen
- *
- * @type function
- * @date 9/12/2014
- * @since 5.1.5
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_is_screen( $id = '' ) {
- // bail early if not defined
- if ( ! function_exists( 'get_current_screen' ) ) {
- return false;
- }
- // vars
- $current_screen = get_current_screen();
- // no screen
- if ( ! $current_screen ) {
- return false;
- // array
- } elseif ( is_array( $id ) ) {
- return in_array( $current_screen->id, $id );
- // string
- } else {
- return ( $id === $current_screen->id );
- }
- }
- /*
- * acf_maybe_get
- *
- * This function will return a var if it exists in an array
- *
- * @type function
- * @date 9/12/2014
- * @since 5.1.5
- *
- * @param $array (array) the array to look within
- * @param $key (key) the array key to look for. Nested values may be found using '/'
- * @param $default (mixed) the value returned if not found
- * @return $post_id (int)
- */
- function acf_maybe_get( $array = array(), $key = 0, $default = null ) {
- return isset( $array[ $key ] ) ? $array[ $key ] : $default;
- }
- function acf_maybe_get_POST( $key = '', $default = null ) {
- return isset( $_POST[ $key ] ) ? acf_sanitize_request_args( $_POST[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- Checked elsewhere.
- }
- function acf_maybe_get_GET( $key = '', $default = null ) {
- return isset( $_GET[ $key ] ) ? acf_sanitize_request_args( $_GET[ $key ] ) : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Checked elsewhere.
- }
- /**
- * Returns an array of attachment data.
- *
- * @date 05/01/2015
- * @since 5.1.5
- *
- * @param int|WP_Post The attachment ID or object.
- * @return array|false
- */
- function acf_get_attachment( $attachment ) {
- // Allow filter to short-circuit load attachment logic.
- // Alternatively, this filter may be used to switch blogs for multisite media functionality.
- $response = apply_filters( 'acf/pre_load_attachment', null, $attachment );
- if ( $response !== null ) {
- return $response;
- }
- // Get the attachment post object.
- $attachment = get_post( $attachment );
- if ( ! $attachment ) {
- return false;
- }
- if ( $attachment->post_type !== 'attachment' ) {
- return false;
- }
- // Load various attachment details.
- $meta = wp_get_attachment_metadata( $attachment->ID );
- $attached_file = get_attached_file( $attachment->ID );
- if ( strpos( $attachment->post_mime_type, '/' ) !== false ) {
- list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
- } else {
- list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
- }
- // Generate response.
- $response = array(
- 'ID' => $attachment->ID,
- 'id' => $attachment->ID,
- 'title' => $attachment->post_title,
- 'filename' => wp_basename( $attached_file ),
- 'filesize' => 0,
- 'url' => wp_get_attachment_url( $attachment->ID ),
- 'link' => get_attachment_link( $attachment->ID ),
- 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
- 'author' => $attachment->post_author,
- 'description' => $attachment->post_content,
- 'caption' => $attachment->post_excerpt,
- 'name' => $attachment->post_name,
- 'status' => $attachment->post_status,
- 'uploaded_to' => $attachment->post_parent,
- 'date' => $attachment->post_date_gmt,
- 'modified' => $attachment->post_modified_gmt,
- 'menu_order' => $attachment->menu_order,
- 'mime_type' => $attachment->post_mime_type,
- 'type' => $type,
- 'subtype' => $subtype,
- 'icon' => wp_mime_type_icon( $attachment->ID ),
- );
- // Append filesize data.
- if ( isset( $meta['filesize'] ) ) {
- $response['filesize'] = $meta['filesize'];
- } elseif ( file_exists( $attached_file ) ) {
- $response['filesize'] = filesize( $attached_file );
- }
- // Restrict the loading of image "sizes".
- $sizes_id = 0;
- // Type specific logic.
- switch ( $type ) {
- case 'image':
- $sizes_id = $attachment->ID;
- $src = wp_get_attachment_image_src( $attachment->ID, 'full' );
- if ( $src ) {
- $response['url'] = $src[0];
- $response['width'] = $src[1];
- $response['height'] = $src[2];
- }
- break;
- case 'video':
- $response['width'] = acf_maybe_get( $meta, 'width', 0 );
- $response['height'] = acf_maybe_get( $meta, 'height', 0 );
- if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
- $sizes_id = $featured_id;
- }
- break;
- case 'audio':
- if ( $featured_id = get_post_thumbnail_id( $attachment->ID ) ) {
- $sizes_id = $featured_id;
- }
- break;
- }
- // Load array of image sizes.
- if ( $sizes_id ) {
- $sizes = get_intermediate_image_sizes();
- $sizes_data = array();
- foreach ( $sizes as $size ) {
- $src = wp_get_attachment_image_src( $sizes_id, $size );
- if ( $src ) {
- $sizes_data[ $size ] = $src[0];
- $sizes_data[ $size . '-width' ] = $src[1];
- $sizes_data[ $size . '-height' ] = $src[2];
- }
- }
- $response['sizes'] = $sizes_data;
- }
- /**
- * Filters the attachment $response after it has been loaded.
- *
- * @date 16/06/2020
- * @since 5.9.0
- *
- * @param array $response Array of loaded attachment data.
- * @param WP_Post $attachment Attachment object.
- * @param array|false $meta Array of attachment meta data, or false if there is none.
- */
- return apply_filters( 'acf/load_attachment', $response, $attachment, $meta );
- }
- /**
- * This function will truncate and return a string
- *
- * @date 8/08/2014
- * @since 5.0.0
- *
- * @param string $text The text to truncate.
- * @param int $length The number of characters to allow in the string.
- *
- * @return string
- */
- function acf_get_truncated( $text, $length = 64 ) {
- $text = trim( $text );
- $the_length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );
- $cut_length = $length - 3;
- $return = function_exists( 'mb_substr' ) ? mb_substr( $text, 0, $cut_length ) : substr( $text, 0, $cut_length );
- if ( $the_length > $cut_length ) {
- $return .= '...';
- }
- return $return;
- }
- /*
- * acf_current_user_can_admin
- *
- * This function will return true if the current user can administrate the ACF field groups
- *
- * @type function
- * @date 9/02/2015
- * @since 5.1.5
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_current_user_can_admin() {
- if ( acf_get_setting( 'show_admin' ) && current_user_can( acf_get_setting( 'capability' ) ) ) {
- return true;
- }
- // return
- return false;
- }
- /*
- * acf_get_filesize
- *
- * This function will return a numeric value of bytes for a given filesize string
- *
- * @type function
- * @date 18/02/2015
- * @since 5.1.5
- *
- * @param $size (mixed)
- * @return (int)
- */
- function acf_get_filesize( $size = 1 ) {
- // vars
- $unit = 'MB';
- $units = array(
- 'TB' => 4,
- 'GB' => 3,
- 'MB' => 2,
- 'KB' => 1,
- );
- // look for $unit within the $size parameter (123 KB)
- if ( is_string( $size ) ) {
- // vars
- $custom = strtoupper( substr( $size, -2 ) );
- foreach ( $units as $k => $v ) {
- if ( $custom === $k ) {
- $unit = $k;
- $size = substr( $size, 0, -2 );
- }
- }
- }
- // calc bytes
- $bytes = floatval( $size ) * pow( 1024, $units[ $unit ] );
- // return
- return $bytes;
- }
- /*
- * acf_format_filesize
- *
- * This function will return a formatted string containing the filesize and unit
- *
- * @type function
- * @date 18/02/2015
- * @since 5.1.5
- *
- * @param $size (mixed)
- * @return (int)
- */
- function acf_format_filesize( $size = 1 ) {
- // convert
- $bytes = acf_get_filesize( $size );
- // vars
- $units = array(
- 'TB' => 4,
- 'GB' => 3,
- 'MB' => 2,
- 'KB' => 1,
- );
- // loop through units
- foreach ( $units as $k => $v ) {
- $result = $bytes / pow( 1024, $v );
- if ( $result >= 1 ) {
- return $result . ' ' . $k;
- }
- }
- // return
- return $bytes . ' B';
- }
- /*
- * acf_get_valid_terms
- *
- * This function will replace old terms with new split term ids
- *
- * @type function
- * @date 27/02/2015
- * @since 5.1.5
- *
- * @param $terms (int|array)
- * @param $taxonomy (string)
- * @return $terms
- */
- function acf_get_valid_terms( $terms = false, $taxonomy = 'category' ) {
- // force into array
- $terms = acf_get_array( $terms );
- // force ints
- $terms = array_map( 'intval', $terms );
- // bail early if function does not yet exist or
- if ( ! function_exists( 'wp_get_split_term' ) || empty( $terms ) ) {
- return $terms;
- }
- // attempt to find new terms
- foreach ( $terms as $i => $term_id ) {
- $new_term_id = wp_get_split_term( $term_id, $taxonomy );
- if ( $new_term_id ) {
- $terms[ $i ] = $new_term_id;
- }
- }
- // return
- return $terms;
- }
- /*
- * acf_validate_attachment
- *
- * This function will validate an attachment based on a field's restrictions and return an array of errors
- *
- * @type function
- * @date 3/07/2015
- * @since 5.2.3
- *
- * @param $attachment (array) attachment data. Changes based on context
- * @param $field (array) field settings containing restrictions
- * @param $context (string) $file is different when uploading / preparing
- * @return $errors (array)
- */
- function acf_validate_attachment( $attachment, $field, $context = 'prepare' ) {
- // vars
- $errors = array();
- $file = array(
- 'type' => '',
- 'width' => 0,
- 'height' => 0,
- 'size' => 0,
- );
- // upload
- if ( $context == 'upload' ) {
- // vars
- $file['type'] = pathinfo( $attachment['name'], PATHINFO_EXTENSION );
- $file['size'] = filesize( $attachment['tmp_name'] );
- if ( strpos( $attachment['type'], 'image' ) !== false ) {
- $size = getimagesize( $attachment['tmp_name'] );
- $file['width'] = acf_maybe_get( $size, 0 );
- $file['height'] = acf_maybe_get( $size, 1 );
- }
- // prepare
- } elseif ( $context == 'prepare' ) {
- $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
- $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
- $file['size'] = acf_maybe_get( $attachment, 'filesizeInBytes', 0 );
- $file['width'] = acf_maybe_get( $attachment, 'width', 0 );
- $file['height'] = acf_maybe_get( $attachment, 'height', 0 );
- // custom
- } else {
- $file = array_merge( $file, $attachment );
- $use_path = isset( $attachment['filename'] ) ? $attachment['filename'] : $attachment['url'];
- $file['type'] = pathinfo( $use_path, PATHINFO_EXTENSION );
- }
- // image
- if ( $file['width'] || $file['height'] ) {
- // width
- $min_width = (int) acf_maybe_get( $field, 'min_width', 0 );
- $max_width = (int) acf_maybe_get( $field, 'max_width', 0 );
- if ( $file['width'] ) {
- if ( $min_width && $file['width'] < $min_width ) {
- // min width
- $errors['min_width'] = sprintf( __( 'Image width must be at least %dpx.', 'acf' ), $min_width );
- } elseif ( $max_width && $file['width'] > $max_width ) {
- // min width
- $errors['max_width'] = sprintf( __( 'Image width must not exceed %dpx.', 'acf' ), $max_width );
- }
- }
- // height
- $min_height = (int) acf_maybe_get( $field, 'min_height', 0 );
- $max_height = (int) acf_maybe_get( $field, 'max_height', 0 );
- if ( $file['height'] ) {
- if ( $min_height && $file['height'] < $min_height ) {
- // min height
- $errors['min_height'] = sprintf( __( 'Image height must be at least %dpx.', 'acf' ), $min_height );
- } elseif ( $max_height && $file['height'] > $max_height ) {
- // min height
- $errors['max_height'] = sprintf( __( 'Image height must not exceed %dpx.', 'acf' ), $max_height );
- }
- }
- }
- // file size
- if ( $file['size'] ) {
- $min_size = acf_maybe_get( $field, 'min_size', 0 );
- $max_size = acf_maybe_get( $field, 'max_size', 0 );
- if ( $min_size && $file['size'] < acf_get_filesize( $min_size ) ) {
- // min width
- $errors['min_size'] = sprintf( __( 'File size must be at least %s.', 'acf' ), acf_format_filesize( $min_size ) );
- } elseif ( $max_size && $file['size'] > acf_get_filesize( $max_size ) ) {
- // min width
- $errors['max_size'] = sprintf( __( 'File size must not exceed %s.', 'acf' ), acf_format_filesize( $max_size ) );
- }
- }
- // file type
- if ( $file['type'] ) {
- $mime_types = acf_maybe_get( $field, 'mime_types', '' );
- // lower case
- $file['type'] = strtolower( $file['type'] );
- $mime_types = strtolower( $mime_types );
- // explode
- $mime_types = str_replace( array( ' ', '.' ), '', $mime_types );
- $mime_types = explode( ',', $mime_types ); // split pieces
- $mime_types = array_filter( $mime_types ); // remove empty pieces
- if ( ! empty( $mime_types ) && ! in_array( $file['type'], $mime_types ) ) {
- // glue together last 2 types
- if ( count( $mime_types ) > 1 ) {
- $last1 = array_pop( $mime_types );
- $last2 = array_pop( $mime_types );
- $mime_types[] = $last2 . ' ' . __( 'or', 'acf' ) . ' ' . $last1;
- }
- $errors['mime_types'] = sprintf( __( 'File type must be %s.', 'acf' ), implode( ', ', $mime_types ) );
- }
- }
- /**
- * Filters the errors for a file before it is uploaded or displayed in the media modal.
- *
- * @date 3/07/2015
- * @since 5.2.3
- *
- * @param array $errors An array of errors.
- * @param array $file An array of data for a single file.
- * @param array $attachment An array of attachment data which differs based on the context.
- * @param array $field The field array.
- * @param string $context The curent context (uploading, preparing)
- */
- $errors = apply_filters( "acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field, $context );
- $errors = apply_filters( "acf/validate_attachment/name={$field['_name']}", $errors, $file, $attachment, $field, $context );
- $errors = apply_filters( "acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field, $context );
- $errors = apply_filters( 'acf/validate_attachment', $errors, $file, $attachment, $field, $context );
- // return
- return $errors;
- }
- /*
- * _acf_settings_uploader
- *
- * Dynamic logic for uploader setting
- *
- * @type function
- * @date 7/05/2015
- * @since 5.2.3
- *
- * @param $uploader (string)
- * @return $uploader
- */
- add_filter( 'acf/settings/uploader', '_acf_settings_uploader' );
- function _acf_settings_uploader( $uploader ) {
- // if can't upload files
- if ( ! current_user_can( 'upload_files' ) ) {
- $uploader = 'basic';
- }
- // return
- return $uploader;
- }
- /*
- * acf_translate_keys
- *
- * description
- *
- * @type function
- * @date 7/12/2015
- * @since 5.3.2
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- /*
- function acf_translate_keys( $array, $keys ) {
- // bail early if no keys
- if( empty($keys) ) return $array;
- // translate
- foreach( $keys as $k ) {
- // bail early if not exists
- if( !isset($array[ $k ]) ) continue;
- // translate
- $array[ $k ] = acf_translate( $array[ $k ] );
- }
- // return
- return $array;
- }
- */
- /*
- * acf_translate
- *
- * This function will translate a string using the new 'l10n_textdomain' setting
- * Also works for arrays which is great for fields - select -> choices
- *
- * @type function
- * @date 4/12/2015
- * @since 5.3.2
- *
- * @param $string (mixed) string or array containins strings to be translated
- * @return $string
- */
- function acf_translate( $string ) {
- // vars
- $l10n = acf_get_setting( 'l10n' );
- $textdomain = acf_get_setting( 'l10n_textdomain' );
- // bail early if not enabled
- if ( ! $l10n ) {
- return $string;
- }
- // bail early if no textdomain
- if ( ! $textdomain ) {
- return $string;
- }
- // is array
- if ( is_array( $string ) ) {
- return array_map( 'acf_translate', $string );
- }
- // bail early if not string
- if ( ! is_string( $string ) ) {
- return $string;
- }
- // bail early if empty
- if ( $string === '' ) {
- return $string;
- }
- // allow for var_export export
- if ( acf_get_setting( 'l10n_var_export' ) ) {
- // bail early if already translated
- if ( substr( $string, 0, 7 ) === '!!__(!!' ) {
- return $string;
- }
- // return
- return "!!__(!!'" . $string . "!!', !!'" . $textdomain . "!!')!!";
- }
- // vars
- return __( $string, $textdomain );
- }
- /*
- * acf_maybe_add_action
- *
- * This function will determine if the action has already run before adding / calling the function
- *
- * @type function
- * @date 13/01/2016
- * @since 5.3.2
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_maybe_add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
- // if action has already run, execute it
- // - if currently doing action, allow $tag to be added as per usual to allow $priority ordering needed for 3rd party asset compatibility
- if ( did_action( $tag ) && ! doing_action( $tag ) ) {
- call_user_func( $function_to_add );
- // if action has not yet run, add it
- } else {
- add_action( $tag, $function_to_add, $priority, $accepted_args );
- }
- }
- /*
- * acf_is_row_collapsed
- *
- * This function will return true if the field's row is collapsed
- *
- * @type function
- * @date 2/03/2016
- * @since 5.3.2
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_is_row_collapsed( $field_key = '', $row_index = 0 ) {
- // collapsed
- $collapsed = acf_get_user_setting( 'collapsed_' . $field_key, '' );
- // cookie fallback ( version < 5.3.2 )
- if ( $collapsed === '' ) {
- $collapsed = acf_extract_var( $_COOKIE, "acf_collapsed_{$field_key}", '' );
- $collapsed = str_replace( '|', ',', $collapsed );
- // update
- acf_update_user_setting( 'collapsed_' . $field_key, $collapsed );
- }
- // explode
- $collapsed = explode( ',', $collapsed );
- $collapsed = array_filter( $collapsed, 'is_numeric' );
- // collapsed class
- return in_array( $row_index, $collapsed );
- }
- /*
- * acf_get_attachment_image
- *
- * description
- *
- * @type function
- * @date 24/10/16
- * @since 5.5.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_get_attachment_image( $attachment_id = 0, $size = 'thumbnail' ) {
- // vars
- $url = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
- $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
- // bail early if no url
- if ( ! $url ) {
- return '';
- }
- // return
- $value = '<img src="' . $url . '" alt="' . $alt . '" />';
- }
- /*
- * acf_get_post_thumbnail
- *
- * This function will return a thumbail image url for a given post
- *
- * @type function
- * @date 3/05/2016
- * @since 5.3.8
- *
- * @param $post (obj)
- * @param $size (mixed)
- * @return (string)
- */
- function acf_get_post_thumbnail( $post = null, $size = 'thumbnail' ) {
- // vars
- $data = array(
- 'url' => '',
- 'type' => '',
- 'html' => '',
- );
- // post
- $post = get_post( $post );
- // bail early if no post
- if ( ! $post ) {
- return $data;
- }
- // vars
- $thumb_id = $post->ID;
- $mime_type = acf_maybe_get( explode( '/', $post->post_mime_type ), 0 );
- // attachment
- if ( $post->post_type === 'attachment' ) {
- // change $thumb_id
- if ( $mime_type === 'audio' || $mime_type === 'video' ) {
- $thumb_id = get_post_thumbnail_id( $post->ID );
- }
- // post
- } else {
- $thumb_id = get_post_thumbnail_id( $post->ID );
- }
- // try url
- $data['url'] = wp_get_attachment_image_src( $thumb_id, $size );
- $data['url'] = acf_maybe_get( $data['url'], 0 );
- // default icon
- if ( ! $data['url'] && $post->post_type === 'attachment' ) {
- $data['url'] = wp_mime_type_icon( $post->ID );
- $data['type'] = 'icon';
- }
- // html
- $data['html'] = '<img src="' . $data['url'] . '" alt="" />';
- // return
- return $data;
- }
- /**
- * acf_get_browser
- *
- * Returns the name of the current browser.
- *
- * @date 17/01/2014
- * @since 5.0.0
- *
- * @param void
- * @return string
- */
- function acf_get_browser() {
- // Check server var.
- if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
- $agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] );
- // Loop over search terms.
- $browsers = array(
- 'Firefox' => 'firefox',
- 'Trident' => 'msie',
- 'MSIE' => 'msie',
- 'Edge' => 'edge',
- 'Chrome' => 'chrome',
- 'Safari' => 'safari',
- );
- foreach ( $browsers as $k => $v ) {
- if ( strpos( $agent, $k ) !== false ) {
- return $v;
- }
- }
- }
- // Return default.
- return '';
- }
- /*
- * acf_is_ajax
- *
- * This function will reutrn true if performing a wp ajax call
- *
- * @type function
- * @date 7/06/2016
- * @since 5.3.8
- *
- * @param n/a
- * @return (boolean)
- */
- function acf_is_ajax( $action = '' ) {
- // vars
- $is_ajax = false;
- // check if is doing ajax
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
- $is_ajax = true;
- }
- // phpcs:disable WordPress.Security.NonceVerification.Missing
- // check $action
- if ( $action && acf_maybe_get( $_POST, 'action' ) !== $action ) {
- // phpcs:enable WordPress.Security.NonceVerification.Missing
- $is_ajax = false;
- }
- // return
- return $is_ajax;
- }
- /*
- * acf_format_date
- *
- * This function will accept a date value and return it in a formatted string
- *
- * @type function
- * @date 16/06/2016
- * @since 5.3.8
- *
- * @param $value (string)
- * @return $format (string)
- */
- function acf_format_date( $value, $format ) {
- // bail early if no value
- if ( ! $value ) {
- return $value;
- }
- // vars
- $unixtimestamp = 0;
- // numeric (either unix or YYYYMMDD)
- if ( is_numeric( $value ) && strlen( $value ) !== 8 ) {
- $unixtimestamp = $value;
- } else {
- $unixtimestamp = strtotime( $value );
- }
- // return
- return date_i18n( $format, $unixtimestamp );
- }
- /**
- * acf_clear_log
- *
- * Deletes the debug.log file.
- *
- * @date 21/1/19
- * @since 5.7.10
- *
- * @param type $var Description. Default.
- * @return type Description.
- */
- function acf_clear_log() {
- unlink( WP_CONTENT_DIR . '/debug.log' );
- }
- /*
- * acf_log
- *
- * description
- *
- * @type function
- * @date 24/06/2016
- * @since 5.3.8
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function acf_log() {
- // vars
- $args = func_get_args();
- // loop
- foreach ( $args as $i => $arg ) {
- // array | object
- if ( is_array( $arg ) || is_object( $arg ) ) {
- $arg = print_r( $arg, true );
- // bool
- } elseif ( is_bool( $arg ) ) {
- $arg = 'bool(' . ( $arg ? 'true' : 'false' ) . ')';
- }
- // update
- $args[ $i ] = $arg;
- }
- // log
- error_log( implode( ' ', $args ) );
- }
- /**
- * acf_dev_log
- *
- * Used to log variables only if ACF_DEV is defined
- *
- * @date 25/8/18
- * @since 5.7.4
- *
- * @param mixed
- * @return void
- */
- function acf_dev_log() {
- if ( defined( 'ACF_DEV' ) && ACF_DEV ) {
- call_user_func_array( 'acf_log', func_get_args() );
- }
- }
- /*
- * acf_doing
- *
- * This function will tell ACF what task it is doing
- *
- * @type function
- * @date 28/06/2016
- * @since 5.3.8
- *
- * @param $event (string)
- * @param context (string)
- * @return n/a
- */
- function acf_doing( $event = '', $context = '' ) {
- acf_update_setting( 'doing', $event );
- acf_update_setting( 'doing_context', $context );
- }
- /*
- * acf_is_doing
- *
- * This function can be used to state what ACF is doing, or to check
- *
- * @type function
- * @date 28/06/2016
- * @since 5.3.8
- *
- * @param $event (string)
- * @param context (string)
- * @return (boolean)
- */
- function acf_is_doing( $event = '', $context = '' ) {
- // vars
- $doing = false;
- // task
- if ( acf_get_setting( 'doing' ) === $event ) {
- $doing = true;
- }
- // context
- if ( $context && acf_get_setting( 'doing_context' ) !== $context ) {
- $doing = false;
- }
- // return
- return $doing;
- }
- /*
- * acf_is_plugin_active
- *
- * This function will return true if the ACF plugin is active
- * - May be included within a theme or other plugin
- *
- * @type function
- * @date 13/07/2016
- * @since 5.4.0
- *
- * @param $basename (int)
- * @return $post_id (int)
- */
- function acf_is_plugin_active() {
- // vars
- $basename = acf_get_setting( 'basename' );
- // ensure is_plugin_active() exists (not on frontend)
- if ( ! function_exists( 'is_plugin_active' ) ) {
- include_once ABSPATH . 'wp-admin/includes/plugin.php';
- }
- // return
- return is_plugin_active( $basename );
- }
- /*
- * acf_send_ajax_results
- *
- * This function will print JSON data for a Select2 AJAX query
- *
- * @type function
- * @date 19/07/2016
- * @since 5.4.0
- *
- * @param $response (array)
- * @return n/a
- */
- function acf_send_ajax_results( $response ) {
- // validate
- $response = wp_parse_args(
- $response,
- array(
- 'results' => array(),
- 'more' => false,
- 'limit' => 0,
- )
- );
- // limit
- if ( $response['limit'] && $response['results'] ) {
- // vars
- $total = 0;
- foreach ( $response['results'] as $result ) {
- // parent
- $total++;
- // children
- if ( ! empty( $result['children'] ) ) {
- $total += count( $result['children'] );
- }
- }
- // calc
- if ( $total >= $response['limit'] ) {
- $response['more'] = true;
- }
- }
- // return
- wp_send_json( $response );
- }
- /*
- * acf_is_sequential_array
- *
- * This function will return true if the array contains only numeric keys
- *
- * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
- * @type function
- * @date 9/09/2016
- * @since 5.4.0
- *
- * @param $array (array)
- * @return (boolean)
- */
- function acf_is_sequential_array( $array ) {
- // bail early if not array
- if ( ! is_array( $array ) ) {
- return false;
- }
- // loop
- foreach ( $array as $key => $value ) {
- // bail early if is string
- if ( is_string( $key ) ) {
- return false;
- }
- }
- // return
- return true;
- }
- /*
- * acf_is_associative_array
- *
- * This function will return true if the array contains one or more string keys
- *
- * @source http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
- * @type function
- * @date 9/09/2016
- * @since 5.4.0
- *
- * @param $array (array)
- * @return (boolean)
- */
- function acf_is_associative_array( $array ) {
- // bail early if not array
- if ( ! is_array( $array ) ) {
- return false;
- }
- // loop
- foreach ( $array as $key => $value ) {
- // bail early if is string
- if ( is_string( $key ) ) {
- return true;
- }
- }
- // return
- return false;
- }
- /*
- * acf_add_array_key_prefix
- *
- * This function will add a prefix to all array keys
- * Useful to preserve numeric keys when performing array_multisort
- *
- * @type function
- * @date 15/09/2016
- * @since 5.4.0
- *
- * @param $array (array)
- * @param $prefix (string)
- * @return (array)
- */
- function acf_add_array_key_prefix( $array, $prefix ) {
- // vars
- $array2 = array();
- // loop
- foreach ( $array as $k => $v ) {
- $k2 = $prefix . $k;
- $array2[ $k2 ] = $v;
- }
- // return
- return $array2;
- }
- /*
- * acf_remove_array_key_prefix
- *
- * This function will remove a prefix to all array keys
- * Useful to preserve numeric keys when performing array_multisort
- *
- * @type function
- * @date 15/09/2016
- * @since 5.4.0
- *
- * @param $array (array)
- * @param $prefix (string)
- * @return (array)
- */
- function acf_remove_array_key_prefix( $array, $prefix ) {
- // vars
- $array2 = array();
- $l = strlen( $prefix );
- // loop
- foreach ( $array as $k => $v ) {
- $k2 = ( substr( $k, 0, $l ) === $prefix ) ? substr( $k, $l ) : $k;
- $array2[ $k2 ] = $v;
- }
- // return
- return $array2;
- }
- /*
- * acf_strip_protocol
- *
- * This function will remove the proticol from a url
- * Used to allow licenses to remain active if a site is switched to https
- *
- * @type function
- * @date 10/01/2017
- * @since 5.5.4
- * @author Aaron
- *
- * @param $url (string)
- * @return (string)
- */
- function acf_strip_protocol( $url ) {
- // strip the protical
- return str_replace( array( 'http://', 'https://' ), '', $url );
- }
- /*
- * acf_connect_attachment_to_post
- *
- * This function will connect an attacment (image etc) to the post
- * Used to connect attachements uploaded directly to media that have not been attaced to a post
- *
- * @type function
- * @date 11/01/2017
- * @since 5.8.0 Added filter to prevent connection.
- * @since 5.5.4
- *
- * @param int $attachment_id The attachment ID.
- * @param int $post_id The post ID.
- * @return bool True if attachment was connected.
- */
- function acf_connect_attachment_to_post( $attachment_id = 0, $post_id = 0 ) {
- // bail early if $attachment_id is not valid.
- if ( ! $attachment_id || ! is_numeric( $attachment_id ) ) {
- return false;
- }
- // bail early if $post_id is not valid.
- if ( ! $post_id || ! is_numeric( $post_id ) ) {
- return false;
- }
- /**
- * Filters whether or not to connect the attachment.
- *
- * @date 8/11/18
- * @since 5.8.0
- *
- * @param bool $bool Returning false will prevent the connection. Default true.
- * @param int $attachment_id The attachment ID.
- * @param int $post_id The post ID.
- */
- if ( ! apply_filters( 'acf/connect_attachment_to_post', true, $attachment_id, $post_id ) ) {
- return false;
- }
- // vars
- $post = get_post( $attachment_id );
- // Check if is valid post.
- if ( $post && $post->post_type == 'attachment' && $post->post_parent == 0 ) {
- // update
- wp_update_post(
- array(
- 'ID' => $post->ID,
- 'post_parent' => $post_id,
- )
- );
- // return
- return true;
- }
- // return
- return true;
- }
- /*
- * acf_encrypt
- *
- * This function will encrypt a string using PHP
- * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
- *
- * @type function
- * @date 27/2/17
- * @since 5.5.8
- *
- * @param $data (string)
- * @return (string)
- */
- function acf_encrypt( $data = '' ) {
- // bail early if no encrypt function
- if ( ! function_exists( 'openssl_encrypt' ) ) {
- return base64_encode( $data );
- }
- // generate a key
- $key = wp_hash( 'acf_encrypt' );
- // Generate an initialization vector
- $iv = openssl_random_pseudo_bytes( openssl_cipher_iv_length( 'aes-256-cbc' ) );
- // Encrypt the data using AES 256 encryption in CBC mode using our encryption key and initialization vector.
- $encrypted_data = openssl_encrypt( $data, 'aes-256-cbc', $key, 0, $iv );
- // The $iv is just as important as the key for decrypting, so save it with our encrypted data using a unique separator (::)
- return base64_encode( $encrypted_data . '::' . $iv );
- }
- /*
- * acf_decrypt
- *
- * This function will decrypt an encrypted string using PHP
- * https://bhoover.com/using-php-openssl_encrypt-openssl_decrypt-encrypt-decrypt-data/
- *
- * @type function
- * @date 27/2/17
- * @since 5.5.8
- *
- * @param $data (string)
- * @return (string)
- */
- function acf_decrypt( $data = '' ) {
- // bail early if no decrypt function
- if ( ! function_exists( 'openssl_decrypt' ) ) {
- return base64_decode( $data );
- }
- // generate a key
- $key = wp_hash( 'acf_encrypt' );
- // To decrypt, split the encrypted data from our IV - our unique separator used was "::"
- list($encrypted_data, $iv) = explode( '::', base64_decode( $data ), 2 );
- // decrypt
- return openssl_decrypt( $encrypted_data, 'aes-256-cbc', $key, 0, $iv );
- }
- /**
- * acf_parse_markdown
- *
- * A very basic regex-based Markdown parser function based off [slimdown](https://gist.github.com/jbroadway/2836900).
- *
- * @date 6/8/18
- * @since 5.7.2
- *
- * @param string $text The string to parse.
- * @return string
- */
- function acf_parse_markdown( $text = '' ) {
- // trim
- $text = trim( $text );
- // rules
- $rules = array(
- '/=== (.+?) ===/' => '<h2>$1</h2>', // headings
- '/== (.+?) ==/' => '<h3>$1</h3>', // headings
- '/= (.+?) =/' => '<h4>$1</h4>', // headings
- '/\[([^\[]+)\]\(([^\)]+)\)/' => '<a href="$2">$1</a>', // links
- '/(\*\*)(.*?)\1/' => '<strong>$2</strong>', // bold
- '/(\*)(.*?)\1/' => '<em>$2</em>', // intalic
- '/`(.*?)`/' => '<code>$1</code>', // inline code
- '/\n\*(.*)/' => "\n<ul>\n\t<li>$1</li>\n</ul>", // ul lists
- '/\n[0-9]+\.(.*)/' => "\n<ol>\n\t<li>$1</li>\n</ol>", // ol lists
- '/<\/ul>\s?<ul>/' => '', // fix extra ul
- '/<\/ol>\s?<ol>/' => '', // fix extra ol
- );
- foreach ( $rules as $k => $v ) {
- $text = preg_replace( $k, $v, $text );
- }
- // autop
- $text = wpautop( $text );
- // return
- return $text;
- }
- /**
- * acf_get_sites
- *
- * Returns an array of sites for a network.
- *
- * @date 29/08/2016
- * @since 5.4.0
- *
- * @param void
- * @return array
- */
- function acf_get_sites() {
- $results = array();
- $sites = get_sites( array( 'number' => 0 ) );
- if ( $sites ) {
- foreach ( $sites as $site ) {
- $results[] = get_site( $site )->to_array();
- }
- }
- return $results;
- }
- /**
- * acf_convert_rules_to_groups
- *
- * Converts an array of rules from ACF4 to an array of groups for ACF5
- *
- * @date 25/8/18
- * @since 5.7.4
- *
- * @param array $rules An array of rules.
- * @param string $anyorall The anyorall setting used in ACF4. Defaults to 'any'.
- * @return array
- */
- function acf_convert_rules_to_groups( $rules, $anyorall = 'any' ) {
- // vars
- $groups = array();
- $index = 0;
- // loop
- foreach ( $rules as $rule ) {
- // extract vars
- $group = acf_extract_var( $rule, 'group_no' );
- $order = acf_extract_var( $rule, 'order_no' );
- // calculate group if not defined
- if ( $group === null ) {
- $group = $index;
- // use $anyorall to determine if a new group is needed
- if ( $anyorall == 'any' ) {
- $index++;
- }
- }
- // calculate order if not defined
- if ( $order === null ) {
- $order = isset( $groups[ $group ] ) ? count( $groups[ $group ] ) : 0;
- }
- // append to group
- $groups[ $group ][ $order ] = $rule;
- // sort groups
- ksort( $groups[ $group ] );
- }
- // sort groups
- ksort( $groups );
- // return
- return $groups;
- }
- /**
- * acf_register_ajax
- *
- * Regsiters an ajax callback.
- *
- * @date 5/10/18
- * @since 5.7.7
- *
- * @param string $name The ajax action name.
- * @param array $callback The callback function or array.
- * @param bool $public Whether to allow access to non logged in users.
- * @return void
- */
- function acf_register_ajax( $name = '', $callback = false, $public = false ) {
- // vars
- $action = "acf/ajax/$name";
- // add action for logged-in users
- add_action( "wp_ajax_$action", $callback );
- // add action for non logged-in users
- if ( $public ) {
- add_action( "wp_ajax_nopriv_$action", $callback );
- }
- }
- /**
- * acf_str_camel_case
- *
- * Converts a string into camelCase.
- * Thanks to https://stackoverflow.com/questions/31274782/convert-array-keys-from-underscore-case-to-camelcase-recursively
- *
- * @date 24/10/18
- * @since 5.8.0
- *
- * @param string $string The string ot convert.
- * @return string
- */
- function acf_str_camel_case( $string = '' ) {
- return lcfirst( str_replace( ' ', '', ucwords( str_replace( '_', ' ', $string ) ) ) );
- }
- /**
- * acf_array_camel_case
- *
- * Converts all aray keys to camelCase.
- *
- * @date 24/10/18
- * @since 5.8.0
- *
- * @param array $array The array to convert.
- * @return array
- */
- function acf_array_camel_case( $array = array() ) {
- $array2 = array();
- foreach ( $array as $k => $v ) {
- $array2[ acf_str_camel_case( $k ) ] = $v;
- }
- return $array2;
- }
- /**
- * Returns true if the current screen is using the block editor.
- *
- * @date 13/12/18
- * @since 5.8.0
- *
- * @return bool
- */
- function acf_is_block_editor() {
- if ( function_exists( 'get_current_screen' ) ) {
- $screen = get_current_screen();
- if ( $screen && method_exists( $screen, 'is_block_editor' ) ) {
- return $screen->is_block_editor();
- }
- }
- return false;
- }
|