post.php 269 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154
  1. <?php
  2. /**
  3. * Core Post API
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. */
  8. //
  9. // Post Type registration.
  10. //
  11. /**
  12. * Creates the initial post types when 'init' action is fired.
  13. *
  14. * See {@see 'init'}.
  15. *
  16. * @since 2.9.0
  17. */
  18. function create_initial_post_types() {
  19. WP_Post_Type::reset_default_labels();
  20. register_post_type(
  21. 'post',
  22. array(
  23. 'labels' => array(
  24. 'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
  25. ),
  26. 'public' => true,
  27. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  28. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  29. 'capability_type' => 'post',
  30. 'map_meta_cap' => true,
  31. 'menu_position' => 5,
  32. 'menu_icon' => 'dashicons-admin-post',
  33. 'hierarchical' => false,
  34. 'rewrite' => false,
  35. 'query_var' => false,
  36. 'delete_with_user' => true,
  37. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
  38. 'show_in_rest' => true,
  39. 'rest_base' => 'posts',
  40. 'rest_controller_class' => 'WP_REST_Posts_Controller',
  41. )
  42. );
  43. register_post_type(
  44. 'page',
  45. array(
  46. 'labels' => array(
  47. 'name_admin_bar' => _x( 'Page', 'add new from admin bar' ),
  48. ),
  49. 'public' => true,
  50. 'publicly_queryable' => false,
  51. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  52. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  53. 'capability_type' => 'page',
  54. 'map_meta_cap' => true,
  55. 'menu_position' => 20,
  56. 'menu_icon' => 'dashicons-admin-page',
  57. 'hierarchical' => true,
  58. 'rewrite' => false,
  59. 'query_var' => false,
  60. 'delete_with_user' => true,
  61. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  62. 'show_in_rest' => true,
  63. 'rest_base' => 'pages',
  64. 'rest_controller_class' => 'WP_REST_Posts_Controller',
  65. )
  66. );
  67. register_post_type(
  68. 'attachment',
  69. array(
  70. 'labels' => array(
  71. 'name' => _x( 'Media', 'post type general name' ),
  72. 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
  73. 'add_new' => _x( 'Add New', 'file' ),
  74. 'edit_item' => __( 'Edit Media' ),
  75. 'view_item' => __( 'View Attachment Page' ),
  76. 'attributes' => __( 'Attachment Attributes' ),
  77. ),
  78. 'public' => true,
  79. 'show_ui' => true,
  80. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  81. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  82. 'capability_type' => 'post',
  83. 'capabilities' => array(
  84. 'create_posts' => 'upload_files',
  85. ),
  86. 'map_meta_cap' => true,
  87. 'menu_icon' => 'dashicons-admin-media',
  88. 'hierarchical' => false,
  89. 'rewrite' => false,
  90. 'query_var' => false,
  91. 'show_in_nav_menus' => false,
  92. 'delete_with_user' => true,
  93. 'supports' => array( 'title', 'author', 'comments' ),
  94. 'show_in_rest' => true,
  95. 'rest_base' => 'media',
  96. 'rest_controller_class' => 'WP_REST_Attachments_Controller',
  97. )
  98. );
  99. add_post_type_support( 'attachment:audio', 'thumbnail' );
  100. add_post_type_support( 'attachment:video', 'thumbnail' );
  101. register_post_type(
  102. 'revision',
  103. array(
  104. 'labels' => array(
  105. 'name' => __( 'Revisions' ),
  106. 'singular_name' => __( 'Revision' ),
  107. ),
  108. 'public' => false,
  109. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  110. '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
  111. 'capability_type' => 'post',
  112. 'map_meta_cap' => true,
  113. 'hierarchical' => false,
  114. 'rewrite' => false,
  115. 'query_var' => false,
  116. 'can_export' => false,
  117. 'delete_with_user' => true,
  118. 'supports' => array( 'author' ),
  119. )
  120. );
  121. register_post_type(
  122. 'nav_menu_item',
  123. array(
  124. 'labels' => array(
  125. 'name' => __( 'Navigation Menu Items' ),
  126. 'singular_name' => __( 'Navigation Menu Item' ),
  127. ),
  128. 'public' => false,
  129. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  130. 'hierarchical' => false,
  131. 'rewrite' => false,
  132. 'delete_with_user' => false,
  133. 'query_var' => false,
  134. 'map_meta_cap' => true,
  135. 'capability_type' => array( 'edit_theme_options', 'edit_theme_options' ),
  136. 'capabilities' => array(
  137. // Meta Capabilities.
  138. 'edit_post' => 'edit_post',
  139. 'read_post' => 'read_post',
  140. 'delete_post' => 'delete_post',
  141. // Primitive Capabilities.
  142. 'edit_posts' => 'edit_theme_options',
  143. 'edit_others_posts' => 'edit_theme_options',
  144. 'delete_posts' => 'edit_theme_options',
  145. 'publish_posts' => 'edit_theme_options',
  146. 'read_private_posts' => 'edit_theme_options',
  147. 'read' => 'read',
  148. 'delete_private_posts' => 'edit_theme_options',
  149. 'delete_published_posts' => 'edit_theme_options',
  150. 'delete_others_posts' => 'edit_theme_options',
  151. 'edit_private_posts' => 'edit_theme_options',
  152. 'edit_published_posts' => 'edit_theme_options',
  153. ),
  154. 'show_in_rest' => true,
  155. 'rest_base' => 'menu-items',
  156. 'rest_controller_class' => 'WP_REST_Menu_Items_Controller',
  157. )
  158. );
  159. register_post_type(
  160. 'custom_css',
  161. array(
  162. 'labels' => array(
  163. 'name' => __( 'Custom CSS' ),
  164. 'singular_name' => __( 'Custom CSS' ),
  165. ),
  166. 'public' => false,
  167. 'hierarchical' => false,
  168. 'rewrite' => false,
  169. 'query_var' => false,
  170. 'delete_with_user' => false,
  171. 'can_export' => true,
  172. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  173. 'supports' => array( 'title', 'revisions' ),
  174. 'capabilities' => array(
  175. 'delete_posts' => 'edit_theme_options',
  176. 'delete_post' => 'edit_theme_options',
  177. 'delete_published_posts' => 'edit_theme_options',
  178. 'delete_private_posts' => 'edit_theme_options',
  179. 'delete_others_posts' => 'edit_theme_options',
  180. 'edit_post' => 'edit_css',
  181. 'edit_posts' => 'edit_css',
  182. 'edit_others_posts' => 'edit_css',
  183. 'edit_published_posts' => 'edit_css',
  184. 'read_post' => 'read',
  185. 'read_private_posts' => 'read',
  186. 'publish_posts' => 'edit_theme_options',
  187. ),
  188. )
  189. );
  190. register_post_type(
  191. 'customize_changeset',
  192. array(
  193. 'labels' => array(
  194. 'name' => _x( 'Changesets', 'post type general name' ),
  195. 'singular_name' => _x( 'Changeset', 'post type singular name' ),
  196. 'add_new' => _x( 'Add New', 'Customize Changeset' ),
  197. 'add_new_item' => __( 'Add New Changeset' ),
  198. 'new_item' => __( 'New Changeset' ),
  199. 'edit_item' => __( 'Edit Changeset' ),
  200. 'view_item' => __( 'View Changeset' ),
  201. 'all_items' => __( 'All Changesets' ),
  202. 'search_items' => __( 'Search Changesets' ),
  203. 'not_found' => __( 'No changesets found.' ),
  204. 'not_found_in_trash' => __( 'No changesets found in Trash.' ),
  205. ),
  206. 'public' => false,
  207. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  208. 'map_meta_cap' => true,
  209. 'hierarchical' => false,
  210. 'rewrite' => false,
  211. 'query_var' => false,
  212. 'can_export' => false,
  213. 'delete_with_user' => false,
  214. 'supports' => array( 'title', 'author' ),
  215. 'capability_type' => 'customize_changeset',
  216. 'capabilities' => array(
  217. 'create_posts' => 'customize',
  218. 'delete_others_posts' => 'customize',
  219. 'delete_post' => 'customize',
  220. 'delete_posts' => 'customize',
  221. 'delete_private_posts' => 'customize',
  222. 'delete_published_posts' => 'customize',
  223. 'edit_others_posts' => 'customize',
  224. 'edit_post' => 'customize',
  225. 'edit_posts' => 'customize',
  226. 'edit_private_posts' => 'customize',
  227. 'edit_published_posts' => 'do_not_allow',
  228. 'publish_posts' => 'customize',
  229. 'read' => 'read',
  230. 'read_post' => 'customize',
  231. 'read_private_posts' => 'customize',
  232. ),
  233. )
  234. );
  235. register_post_type(
  236. 'oembed_cache',
  237. array(
  238. 'labels' => array(
  239. 'name' => __( 'oEmbed Responses' ),
  240. 'singular_name' => __( 'oEmbed Response' ),
  241. ),
  242. 'public' => false,
  243. 'hierarchical' => false,
  244. 'rewrite' => false,
  245. 'query_var' => false,
  246. 'delete_with_user' => false,
  247. 'can_export' => false,
  248. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  249. 'supports' => array(),
  250. )
  251. );
  252. register_post_type(
  253. 'user_request',
  254. array(
  255. 'labels' => array(
  256. 'name' => __( 'User Requests' ),
  257. 'singular_name' => __( 'User Request' ),
  258. ),
  259. 'public' => false,
  260. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  261. 'hierarchical' => false,
  262. 'rewrite' => false,
  263. 'query_var' => false,
  264. 'can_export' => false,
  265. 'delete_with_user' => false,
  266. 'supports' => array(),
  267. )
  268. );
  269. register_post_type(
  270. 'wp_block',
  271. array(
  272. 'labels' => array(
  273. 'name' => _x( 'Reusable blocks', 'post type general name' ),
  274. 'singular_name' => _x( 'Reusable block', 'post type singular name' ),
  275. 'add_new' => _x( 'Add New', 'Reusable block' ),
  276. 'add_new_item' => __( 'Add new Reusable block' ),
  277. 'new_item' => __( 'New Reusable block' ),
  278. 'edit_item' => __( 'Edit Reusable block' ),
  279. 'view_item' => __( 'View Reusable block' ),
  280. 'all_items' => __( 'All Reusable blocks' ),
  281. 'search_items' => __( 'Search Reusable blocks' ),
  282. 'not_found' => __( 'No reusable blocks found.' ),
  283. 'not_found_in_trash' => __( 'No reusable blocks found in Trash.' ),
  284. 'filter_items_list' => __( 'Filter reusable blocks list' ),
  285. 'items_list_navigation' => __( 'Reusable blocks list navigation' ),
  286. 'items_list' => __( 'Reusable blocks list' ),
  287. 'item_published' => __( 'Reusable block published.' ),
  288. 'item_published_privately' => __( 'Reusable block published privately.' ),
  289. 'item_reverted_to_draft' => __( 'Reusable block reverted to draft.' ),
  290. 'item_scheduled' => __( 'Reusable block scheduled.' ),
  291. 'item_updated' => __( 'Reusable block updated.' ),
  292. ),
  293. 'public' => false,
  294. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  295. 'show_ui' => true,
  296. 'show_in_menu' => false,
  297. 'rewrite' => false,
  298. 'show_in_rest' => true,
  299. 'rest_base' => 'blocks',
  300. 'rest_controller_class' => 'WP_REST_Blocks_Controller',
  301. 'capability_type' => 'block',
  302. 'capabilities' => array(
  303. // You need to be able to edit posts, in order to read blocks in their raw form.
  304. 'read' => 'edit_posts',
  305. // You need to be able to publish posts, in order to create blocks.
  306. 'create_posts' => 'publish_posts',
  307. 'edit_posts' => 'edit_posts',
  308. 'edit_published_posts' => 'edit_published_posts',
  309. 'delete_published_posts' => 'delete_published_posts',
  310. 'edit_others_posts' => 'edit_others_posts',
  311. 'delete_others_posts' => 'delete_others_posts',
  312. ),
  313. 'map_meta_cap' => true,
  314. 'supports' => array(
  315. 'title',
  316. 'editor',
  317. 'revisions',
  318. ),
  319. )
  320. );
  321. register_post_type(
  322. 'wp_template',
  323. array(
  324. 'labels' => array(
  325. 'name' => _x( 'Templates', 'post type general name' ),
  326. 'singular_name' => _x( 'Template', 'post type singular name' ),
  327. 'add_new' => _x( 'Add New', 'Template' ),
  328. 'add_new_item' => __( 'Add New Template' ),
  329. 'new_item' => __( 'New Template' ),
  330. 'edit_item' => __( 'Edit Template' ),
  331. 'view_item' => __( 'View Template' ),
  332. 'all_items' => __( 'Templates' ),
  333. 'search_items' => __( 'Search Templates' ),
  334. 'parent_item_colon' => __( 'Parent Template:' ),
  335. 'not_found' => __( 'No templates found.' ),
  336. 'not_found_in_trash' => __( 'No templates found in Trash.' ),
  337. 'archives' => __( 'Template archives' ),
  338. 'insert_into_item' => __( 'Insert into template' ),
  339. 'uploaded_to_this_item' => __( 'Uploaded to this template' ),
  340. 'filter_items_list' => __( 'Filter templates list' ),
  341. 'items_list_navigation' => __( 'Templates list navigation' ),
  342. 'items_list' => __( 'Templates list' ),
  343. ),
  344. 'description' => __( 'Templates to include in your theme.' ),
  345. 'public' => false,
  346. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  347. 'has_archive' => false,
  348. 'show_ui' => false,
  349. 'show_in_menu' => false,
  350. 'show_in_rest' => true,
  351. 'rewrite' => false,
  352. 'rest_base' => 'templates',
  353. 'rest_controller_class' => 'WP_REST_Templates_Controller',
  354. 'capability_type' => array( 'template', 'templates' ),
  355. 'capabilities' => array(
  356. 'create_posts' => 'edit_theme_options',
  357. 'delete_posts' => 'edit_theme_options',
  358. 'delete_others_posts' => 'edit_theme_options',
  359. 'delete_private_posts' => 'edit_theme_options',
  360. 'delete_published_posts' => 'edit_theme_options',
  361. 'edit_posts' => 'edit_theme_options',
  362. 'edit_others_posts' => 'edit_theme_options',
  363. 'edit_private_posts' => 'edit_theme_options',
  364. 'edit_published_posts' => 'edit_theme_options',
  365. 'publish_posts' => 'edit_theme_options',
  366. 'read' => 'edit_theme_options',
  367. 'read_private_posts' => 'edit_theme_options',
  368. ),
  369. 'map_meta_cap' => true,
  370. 'supports' => array(
  371. 'title',
  372. 'slug',
  373. 'excerpt',
  374. 'editor',
  375. 'revisions',
  376. 'author',
  377. ),
  378. )
  379. );
  380. register_post_type(
  381. 'wp_template_part',
  382. array(
  383. 'labels' => array(
  384. 'name' => _x( 'Template Parts', 'post type general name' ),
  385. 'singular_name' => _x( 'Template Part', 'post type singular name' ),
  386. 'add_new' => _x( 'Add New', 'Template Part' ),
  387. 'add_new_item' => __( 'Add New Template Part' ),
  388. 'new_item' => __( 'New Template Part' ),
  389. 'edit_item' => __( 'Edit Template Part' ),
  390. 'view_item' => __( 'View Template Part' ),
  391. 'all_items' => __( 'Template Parts' ),
  392. 'search_items' => __( 'Search Template Parts' ),
  393. 'parent_item_colon' => __( 'Parent Template Part:' ),
  394. 'not_found' => __( 'No template parts found.' ),
  395. 'not_found_in_trash' => __( 'No template parts found in Trash.' ),
  396. 'archives' => __( 'Template part archives' ),
  397. 'insert_into_item' => __( 'Insert into template part' ),
  398. 'uploaded_to_this_item' => __( 'Uploaded to this template part' ),
  399. 'filter_items_list' => __( 'Filter template parts list' ),
  400. 'items_list_navigation' => __( 'Template parts list navigation' ),
  401. 'items_list' => __( 'Template parts list' ),
  402. ),
  403. 'description' => __( 'Template parts to include in your templates.' ),
  404. 'public' => false,
  405. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  406. 'has_archive' => false,
  407. 'show_ui' => false,
  408. 'show_in_menu' => false,
  409. 'show_in_rest' => true,
  410. 'rewrite' => false,
  411. 'rest_base' => 'template-parts',
  412. 'rest_controller_class' => 'WP_REST_Templates_Controller',
  413. 'map_meta_cap' => true,
  414. 'capabilities' => array(
  415. 'create_posts' => 'edit_theme_options',
  416. 'delete_posts' => 'edit_theme_options',
  417. 'delete_others_posts' => 'edit_theme_options',
  418. 'delete_private_posts' => 'edit_theme_options',
  419. 'delete_published_posts' => 'edit_theme_options',
  420. 'edit_posts' => 'edit_theme_options',
  421. 'edit_others_posts' => 'edit_theme_options',
  422. 'edit_private_posts' => 'edit_theme_options',
  423. 'edit_published_posts' => 'edit_theme_options',
  424. 'publish_posts' => 'edit_theme_options',
  425. 'read' => 'edit_theme_options',
  426. 'read_private_posts' => 'edit_theme_options',
  427. ),
  428. 'supports' => array(
  429. 'title',
  430. 'slug',
  431. 'excerpt',
  432. 'editor',
  433. 'revisions',
  434. 'author',
  435. ),
  436. )
  437. );
  438. register_post_type(
  439. 'wp_global_styles',
  440. array(
  441. 'label' => _x( 'Global Styles', 'post type general name' ),
  442. 'description' => __( 'Global styles to include in themes.' ),
  443. 'public' => false,
  444. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  445. 'show_ui' => false,
  446. 'show_in_rest' => false,
  447. 'rewrite' => false,
  448. 'capabilities' => array(
  449. 'read' => 'edit_theme_options',
  450. 'create_posts' => 'edit_theme_options',
  451. 'edit_posts' => 'edit_theme_options',
  452. 'edit_published_posts' => 'edit_theme_options',
  453. 'delete_published_posts' => 'edit_theme_options',
  454. 'edit_others_posts' => 'edit_theme_options',
  455. 'delete_others_posts' => 'edit_theme_options',
  456. ),
  457. 'map_meta_cap' => true,
  458. 'supports' => array(
  459. 'title',
  460. 'editor',
  461. 'revisions',
  462. ),
  463. )
  464. );
  465. register_post_type(
  466. 'wp_navigation',
  467. array(
  468. 'labels' => array(
  469. 'name' => _x( 'Navigation Menus', 'post type general name' ),
  470. 'singular_name' => _x( 'Navigation Menu', 'post type singular name' ),
  471. 'add_new' => _x( 'Add New', 'Navigation Menu' ),
  472. 'add_new_item' => __( 'Add New Navigation Menu' ),
  473. 'new_item' => __( 'New Navigation Menu' ),
  474. 'edit_item' => __( 'Edit Navigation Menu' ),
  475. 'view_item' => __( 'View Navigation Menu' ),
  476. 'all_items' => __( 'Navigation Menus' ),
  477. 'search_items' => __( 'Search Navigation Menus' ),
  478. 'parent_item_colon' => __( 'Parent Navigation Menu:' ),
  479. 'not_found' => __( 'No Navigation Menu found.' ),
  480. 'not_found_in_trash' => __( 'No Navigation Menu found in Trash.' ),
  481. 'archives' => __( 'Navigation Menu archives' ),
  482. 'insert_into_item' => __( 'Insert into Navigation Menu' ),
  483. 'uploaded_to_this_item' => __( 'Uploaded to this Navigation Menu' ),
  484. 'filter_items_list' => __( 'Filter Navigation Menu list' ),
  485. 'items_list_navigation' => __( 'Navigation Menus list navigation' ),
  486. 'items_list' => __( 'Navigation Menus list' ),
  487. ),
  488. 'description' => __( 'Navigation menus that can be inserted into your site.' ),
  489. 'public' => false,
  490. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  491. 'has_archive' => false,
  492. 'show_ui' => true,
  493. 'show_in_menu' => false,
  494. 'show_in_admin_bar' => false,
  495. 'show_in_rest' => true,
  496. 'rewrite' => false,
  497. 'map_meta_cap' => true,
  498. 'capabilities' => array(
  499. 'edit_others_posts' => 'edit_theme_options',
  500. 'delete_posts' => 'edit_theme_options',
  501. 'publish_posts' => 'edit_theme_options',
  502. 'create_posts' => 'edit_theme_options',
  503. 'read_private_posts' => 'edit_theme_options',
  504. 'delete_private_posts' => 'edit_theme_options',
  505. 'delete_published_posts' => 'edit_theme_options',
  506. 'delete_others_posts' => 'edit_theme_options',
  507. 'edit_private_posts' => 'edit_theme_options',
  508. 'edit_published_posts' => 'edit_theme_options',
  509. 'edit_posts' => 'edit_theme_options',
  510. ),
  511. 'rest_base' => 'navigation',
  512. 'rest_controller_class' => 'WP_REST_Posts_Controller',
  513. 'supports' => array(
  514. 'title',
  515. 'editor',
  516. 'revisions',
  517. ),
  518. )
  519. );
  520. register_post_status(
  521. 'publish',
  522. array(
  523. 'label' => _x( 'Published', 'post status' ),
  524. 'public' => true,
  525. '_builtin' => true, /* internal use only. */
  526. /* translators: %s: Number of published posts. */
  527. 'label_count' => _n_noop(
  528. 'Published <span class="count">(%s)</span>',
  529. 'Published <span class="count">(%s)</span>'
  530. ),
  531. )
  532. );
  533. register_post_status(
  534. 'future',
  535. array(
  536. 'label' => _x( 'Scheduled', 'post status' ),
  537. 'protected' => true,
  538. '_builtin' => true, /* internal use only. */
  539. /* translators: %s: Number of scheduled posts. */
  540. 'label_count' => _n_noop(
  541. 'Scheduled <span class="count">(%s)</span>',
  542. 'Scheduled <span class="count">(%s)</span>'
  543. ),
  544. )
  545. );
  546. register_post_status(
  547. 'draft',
  548. array(
  549. 'label' => _x( 'Draft', 'post status' ),
  550. 'protected' => true,
  551. '_builtin' => true, /* internal use only. */
  552. /* translators: %s: Number of draft posts. */
  553. 'label_count' => _n_noop(
  554. 'Draft <span class="count">(%s)</span>',
  555. 'Drafts <span class="count">(%s)</span>'
  556. ),
  557. 'date_floating' => true,
  558. )
  559. );
  560. register_post_status(
  561. 'pending',
  562. array(
  563. 'label' => _x( 'Pending', 'post status' ),
  564. 'protected' => true,
  565. '_builtin' => true, /* internal use only. */
  566. /* translators: %s: Number of pending posts. */
  567. 'label_count' => _n_noop(
  568. 'Pending <span class="count">(%s)</span>',
  569. 'Pending <span class="count">(%s)</span>'
  570. ),
  571. 'date_floating' => true,
  572. )
  573. );
  574. register_post_status(
  575. 'private',
  576. array(
  577. 'label' => _x( 'Private', 'post status' ),
  578. 'private' => true,
  579. '_builtin' => true, /* internal use only. */
  580. /* translators: %s: Number of private posts. */
  581. 'label_count' => _n_noop(
  582. 'Private <span class="count">(%s)</span>',
  583. 'Private <span class="count">(%s)</span>'
  584. ),
  585. )
  586. );
  587. register_post_status(
  588. 'trash',
  589. array(
  590. 'label' => _x( 'Trash', 'post status' ),
  591. 'internal' => true,
  592. '_builtin' => true, /* internal use only. */
  593. /* translators: %s: Number of trashed posts. */
  594. 'label_count' => _n_noop(
  595. 'Trash <span class="count">(%s)</span>',
  596. 'Trash <span class="count">(%s)</span>'
  597. ),
  598. 'show_in_admin_status_list' => true,
  599. )
  600. );
  601. register_post_status(
  602. 'auto-draft',
  603. array(
  604. 'label' => 'auto-draft',
  605. 'internal' => true,
  606. '_builtin' => true, /* internal use only. */
  607. 'date_floating' => true,
  608. )
  609. );
  610. register_post_status(
  611. 'inherit',
  612. array(
  613. 'label' => 'inherit',
  614. 'internal' => true,
  615. '_builtin' => true, /* internal use only. */
  616. 'exclude_from_search' => false,
  617. )
  618. );
  619. register_post_status(
  620. 'request-pending',
  621. array(
  622. 'label' => _x( 'Pending', 'request status' ),
  623. 'internal' => true,
  624. '_builtin' => true, /* internal use only. */
  625. /* translators: %s: Number of pending requests. */
  626. 'label_count' => _n_noop(
  627. 'Pending <span class="count">(%s)</span>',
  628. 'Pending <span class="count">(%s)</span>'
  629. ),
  630. 'exclude_from_search' => false,
  631. )
  632. );
  633. register_post_status(
  634. 'request-confirmed',
  635. array(
  636. 'label' => _x( 'Confirmed', 'request status' ),
  637. 'internal' => true,
  638. '_builtin' => true, /* internal use only. */
  639. /* translators: %s: Number of confirmed requests. */
  640. 'label_count' => _n_noop(
  641. 'Confirmed <span class="count">(%s)</span>',
  642. 'Confirmed <span class="count">(%s)</span>'
  643. ),
  644. 'exclude_from_search' => false,
  645. )
  646. );
  647. register_post_status(
  648. 'request-failed',
  649. array(
  650. 'label' => _x( 'Failed', 'request status' ),
  651. 'internal' => true,
  652. '_builtin' => true, /* internal use only. */
  653. /* translators: %s: Number of failed requests. */
  654. 'label_count' => _n_noop(
  655. 'Failed <span class="count">(%s)</span>',
  656. 'Failed <span class="count">(%s)</span>'
  657. ),
  658. 'exclude_from_search' => false,
  659. )
  660. );
  661. register_post_status(
  662. 'request-completed',
  663. array(
  664. 'label' => _x( 'Completed', 'request status' ),
  665. 'internal' => true,
  666. '_builtin' => true, /* internal use only. */
  667. /* translators: %s: Number of completed requests. */
  668. 'label_count' => _n_noop(
  669. 'Completed <span class="count">(%s)</span>',
  670. 'Completed <span class="count">(%s)</span>'
  671. ),
  672. 'exclude_from_search' => false,
  673. )
  674. );
  675. }
  676. /**
  677. * Retrieves attached file path based on attachment ID.
  678. *
  679. * By default the path will go through the 'get_attached_file' filter, but
  680. * passing a true to the $unfiltered argument of get_attached_file() will
  681. * return the file path unfiltered.
  682. *
  683. * The function works by getting the single post meta name, named
  684. * '_wp_attached_file' and returning it. This is a convenience function to
  685. * prevent looking up the meta name and provide a mechanism for sending the
  686. * attached filename through a filter.
  687. *
  688. * @since 2.0.0
  689. *
  690. * @param int $attachment_id Attachment ID.
  691. * @param bool $unfiltered Optional. Whether to apply filters. Default false.
  692. * @return string|false The file path to where the attached file should be, false otherwise.
  693. */
  694. function get_attached_file( $attachment_id, $unfiltered = false ) {
  695. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  696. // If the file is relative, prepend upload dir.
  697. if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {
  698. $uploads = wp_get_upload_dir();
  699. if ( false === $uploads['error'] ) {
  700. $file = $uploads['basedir'] . "/$file";
  701. }
  702. }
  703. if ( $unfiltered ) {
  704. return $file;
  705. }
  706. /**
  707. * Filters the attached file based on the given ID.
  708. *
  709. * @since 2.1.0
  710. *
  711. * @param string|false $file The file path to where the attached file should be, false otherwise.
  712. * @param int $attachment_id Attachment ID.
  713. */
  714. return apply_filters( 'get_attached_file', $file, $attachment_id );
  715. }
  716. /**
  717. * Updates attachment file path based on attachment ID.
  718. *
  719. * Used to update the file path of the attachment, which uses post meta name
  720. * '_wp_attached_file' to store the path of the attachment.
  721. *
  722. * @since 2.1.0
  723. *
  724. * @param int $attachment_id Attachment ID.
  725. * @param string $file File path for the attachment.
  726. * @return bool True on success, false on failure.
  727. */
  728. function update_attached_file( $attachment_id, $file ) {
  729. if ( ! get_post( $attachment_id ) ) {
  730. return false;
  731. }
  732. /**
  733. * Filters the path to the attached file to update.
  734. *
  735. * @since 2.1.0
  736. *
  737. * @param string $file Path to the attached file to update.
  738. * @param int $attachment_id Attachment ID.
  739. */
  740. $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  741. $file = _wp_relative_upload_path( $file );
  742. if ( $file ) {
  743. return update_post_meta( $attachment_id, '_wp_attached_file', $file );
  744. } else {
  745. return delete_post_meta( $attachment_id, '_wp_attached_file' );
  746. }
  747. }
  748. /**
  749. * Returns relative path to an uploaded file.
  750. *
  751. * The path is relative to the current upload dir.
  752. *
  753. * @since 2.9.0
  754. * @access private
  755. *
  756. * @param string $path Full path to the file.
  757. * @return string Relative path on success, unchanged path on failure.
  758. */
  759. function _wp_relative_upload_path( $path ) {
  760. $new_path = $path;
  761. $uploads = wp_get_upload_dir();
  762. if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
  763. $new_path = str_replace( $uploads['basedir'], '', $new_path );
  764. $new_path = ltrim( $new_path, '/' );
  765. }
  766. /**
  767. * Filters the relative path to an uploaded file.
  768. *
  769. * @since 2.9.0
  770. *
  771. * @param string $new_path Relative path to the file.
  772. * @param string $path Full path to the file.
  773. */
  774. return apply_filters( '_wp_relative_upload_path', $new_path, $path );
  775. }
  776. /**
  777. * Retrieves all children of the post parent ID.
  778. *
  779. * Normally, without any enhancements, the children would apply to pages. In the
  780. * context of the inner workings of WordPress, pages, posts, and attachments
  781. * share the same table, so therefore the functionality could apply to any one
  782. * of them. It is then noted that while this function does not work on posts, it
  783. * does not mean that it won't work on posts. It is recommended that you know
  784. * what context you wish to retrieve the children of.
  785. *
  786. * Attachments may also be made the child of a post, so if that is an accurate
  787. * statement (which needs to be verified), it would then be possible to get
  788. * all of the attachments for a post. Attachments have since changed since
  789. * version 2.5, so this is most likely inaccurate, but serves generally as an
  790. * example of what is possible.
  791. *
  792. * The arguments listed as defaults are for this function and also of the
  793. * get_posts() function. The arguments are combined with the get_children defaults
  794. * and are then passed to the get_posts() function, which accepts additional arguments.
  795. * You can replace the defaults in this function, listed below and the additional
  796. * arguments listed in the get_posts() function.
  797. *
  798. * The 'post_parent' is the most important argument and important attention
  799. * needs to be paid to the $args parameter. If you pass either an object or an
  800. * integer (number), then just the 'post_parent' is grabbed and everything else
  801. * is lost. If you don't specify any arguments, then it is assumed that you are
  802. * in The Loop and the post parent will be grabbed for from the current post.
  803. *
  804. * The 'post_parent' argument is the ID to get the children. The 'numberposts'
  805. * is the amount of posts to retrieve that has a default of '-1', which is
  806. * used to get all of the posts. Giving a number higher than 0 will only
  807. * retrieve that amount of posts.
  808. *
  809. * The 'post_type' and 'post_status' arguments can be used to choose what
  810. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
  811. * post types are 'post', 'pages', and 'attachments'. The 'post_status'
  812. * argument will accept any post status within the write administration panels.
  813. *
  814. * @since 2.0.0
  815. *
  816. * @see get_posts()
  817. * @todo Check validity of description.
  818. *
  819. * @global WP_Post $post Global post object.
  820. *
  821. * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty.
  822. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  823. * correspond to a WP_Post object, an associative array, or a numeric array,
  824. * respectively. Default OBJECT.
  825. * @return WP_Post[]|array[]|int[] Array of post objects, arrays, or IDs, depending on `$output`.
  826. */
  827. function get_children( $args = '', $output = OBJECT ) {
  828. $kids = array();
  829. if ( empty( $args ) ) {
  830. if ( isset( $GLOBALS['post'] ) ) {
  831. $args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent );
  832. } else {
  833. return $kids;
  834. }
  835. } elseif ( is_object( $args ) ) {
  836. $args = array( 'post_parent' => (int) $args->post_parent );
  837. } elseif ( is_numeric( $args ) ) {
  838. $args = array( 'post_parent' => (int) $args );
  839. }
  840. $defaults = array(
  841. 'numberposts' => -1,
  842. 'post_type' => 'any',
  843. 'post_status' => 'any',
  844. 'post_parent' => 0,
  845. );
  846. $parsed_args = wp_parse_args( $args, $defaults );
  847. $children = get_posts( $parsed_args );
  848. if ( ! $children ) {
  849. return $kids;
  850. }
  851. if ( ! empty( $parsed_args['fields'] ) ) {
  852. return $children;
  853. }
  854. update_post_cache( $children );
  855. foreach ( $children as $key => $child ) {
  856. $kids[ $child->ID ] = $children[ $key ];
  857. }
  858. if ( OBJECT === $output ) {
  859. return $kids;
  860. } elseif ( ARRAY_A === $output ) {
  861. $weeuns = array();
  862. foreach ( (array) $kids as $kid ) {
  863. $weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] );
  864. }
  865. return $weeuns;
  866. } elseif ( ARRAY_N === $output ) {
  867. $babes = array();
  868. foreach ( (array) $kids as $kid ) {
  869. $babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) );
  870. }
  871. return $babes;
  872. } else {
  873. return $kids;
  874. }
  875. }
  876. /**
  877. * Gets extended entry info (<!--more-->).
  878. *
  879. * There should not be any space after the second dash and before the word
  880. * 'more'. There can be text or space(s) after the word 'more', but won't be
  881. * referenced.
  882. *
  883. * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
  884. * the `<!--more-->`. The 'extended' key has the content after the
  885. * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text.
  886. *
  887. * @since 1.0.0
  888. *
  889. * @param string $post Post content.
  890. * @return string[] {
  891. * Extended entry info.
  892. *
  893. * @type string $main Content before the more tag.
  894. * @type string $extended Content after the more tag.
  895. * @type string $more_text Custom read more text, or empty string.
  896. * }
  897. */
  898. function get_extended( $post ) {
  899. // Match the new style more links.
  900. if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) {
  901. list($main, $extended) = explode( $matches[0], $post, 2 );
  902. $more_text = $matches[1];
  903. } else {
  904. $main = $post;
  905. $extended = '';
  906. $more_text = '';
  907. }
  908. // Leading and trailing whitespace.
  909. $main = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main );
  910. $extended = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended );
  911. $more_text = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $more_text );
  912. return array(
  913. 'main' => $main,
  914. 'extended' => $extended,
  915. 'more_text' => $more_text,
  916. );
  917. }
  918. /**
  919. * Retrieves post data given a post ID or post object.
  920. *
  921. * See sanitize_post() for optional $filter values. Also, the parameter
  922. * `$post`, must be given as a variable, since it is passed by reference.
  923. *
  924. * @since 1.5.1
  925. *
  926. * @global WP_Post $post Global post object.
  927. *
  928. * @param int|WP_Post|null $post Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values
  929. * return the current global post inside the loop. A numerically valid post ID that
  930. * points to a non-existent post returns `null`. Defaults to global $post.
  931. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  932. * correspond to a WP_Post object, an associative array, or a numeric array,
  933. * respectively. Default OBJECT.
  934. * @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db',
  935. * or 'display'. Default 'raw'.
  936. * @return WP_Post|array|null Type corresponding to $output on success or null on failure.
  937. * When $output is OBJECT, a `WP_Post` instance is returned.
  938. */
  939. function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
  940. if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
  941. $post = $GLOBALS['post'];
  942. }
  943. if ( $post instanceof WP_Post ) {
  944. $_post = $post;
  945. } elseif ( is_object( $post ) ) {
  946. if ( empty( $post->filter ) ) {
  947. $_post = sanitize_post( $post, 'raw' );
  948. $_post = new WP_Post( $_post );
  949. } elseif ( 'raw' === $post->filter ) {
  950. $_post = new WP_Post( $post );
  951. } else {
  952. $_post = WP_Post::get_instance( $post->ID );
  953. }
  954. } else {
  955. $_post = WP_Post::get_instance( $post );
  956. }
  957. if ( ! $_post ) {
  958. return null;
  959. }
  960. $_post = $_post->filter( $filter );
  961. if ( ARRAY_A === $output ) {
  962. return $_post->to_array();
  963. } elseif ( ARRAY_N === $output ) {
  964. return array_values( $_post->to_array() );
  965. }
  966. return $_post;
  967. }
  968. /**
  969. * Retrieves the IDs of the ancestors of a post.
  970. *
  971. * @since 2.5.0
  972. *
  973. * @param int|WP_Post $post Post ID or post object.
  974. * @return int[] Array of ancestor IDs or empty array if there are none.
  975. */
  976. function get_post_ancestors( $post ) {
  977. $post = get_post( $post );
  978. if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) {
  979. return array();
  980. }
  981. $ancestors = array();
  982. $id = $post->post_parent;
  983. $ancestors[] = $id;
  984. while ( $ancestor = get_post( $id ) ) {
  985. // Loop detection: If the ancestor has been seen before, break.
  986. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) {
  987. break;
  988. }
  989. $id = $ancestor->post_parent;
  990. $ancestors[] = $id;
  991. }
  992. return $ancestors;
  993. }
  994. /**
  995. * Retrieves data from a post field based on Post ID.
  996. *
  997. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
  998. * etc and based off of the post object property or key names.
  999. *
  1000. * The context values are based off of the taxonomy filter functions and
  1001. * supported values are found within those functions.
  1002. *
  1003. * @since 2.3.0
  1004. * @since 4.5.0 The `$post` parameter was made optional.
  1005. *
  1006. * @see sanitize_post_field()
  1007. *
  1008. * @param string $field Post field name.
  1009. * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
  1010. * @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
  1011. * or 'display'. Default 'display'.
  1012. * @return string The value of the post field on success, empty string on failure.
  1013. */
  1014. function get_post_field( $field, $post = null, $context = 'display' ) {
  1015. $post = get_post( $post );
  1016. if ( ! $post ) {
  1017. return '';
  1018. }
  1019. if ( ! isset( $post->$field ) ) {
  1020. return '';
  1021. }
  1022. return sanitize_post_field( $field, $post->$field, $post->ID, $context );
  1023. }
  1024. /**
  1025. * Retrieves the mime type of an attachment based on the ID.
  1026. *
  1027. * This function can be used with any post type, but it makes more sense with
  1028. * attachments.
  1029. *
  1030. * @since 2.0.0
  1031. *
  1032. * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
  1033. * @return string|false The mime type on success, false on failure.
  1034. */
  1035. function get_post_mime_type( $post = null ) {
  1036. $post = get_post( $post );
  1037. if ( is_object( $post ) ) {
  1038. return $post->post_mime_type;
  1039. }
  1040. return false;
  1041. }
  1042. /**
  1043. * Retrieves the post status based on the post ID.
  1044. *
  1045. * If the post ID is of an attachment, then the parent post status will be given
  1046. * instead.
  1047. *
  1048. * @since 2.0.0
  1049. *
  1050. * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
  1051. * @return string|false Post status on success, false on failure.
  1052. */
  1053. function get_post_status( $post = null ) {
  1054. $post = get_post( $post );
  1055. if ( ! is_object( $post ) ) {
  1056. return false;
  1057. }
  1058. $post_status = $post->post_status;
  1059. if (
  1060. 'attachment' === $post->post_type &&
  1061. 'inherit' === $post_status
  1062. ) {
  1063. if (
  1064. 0 === $post->post_parent ||
  1065. ! get_post( $post->post_parent ) ||
  1066. $post->ID === $post->post_parent
  1067. ) {
  1068. // Unattached attachments with inherit status are assumed to be published.
  1069. $post_status = 'publish';
  1070. } elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
  1071. // Get parent status prior to trashing.
  1072. $post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
  1073. if ( ! $post_status ) {
  1074. // Assume publish as above.
  1075. $post_status = 'publish';
  1076. }
  1077. } else {
  1078. $post_status = get_post_status( $post->post_parent );
  1079. }
  1080. } elseif (
  1081. 'attachment' === $post->post_type &&
  1082. ! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
  1083. ) {
  1084. /*
  1085. * Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
  1086. * This is to match the logic in wp_insert_post().
  1087. *
  1088. * Note: 'inherit' is excluded from this check as it is resolved to the parent post's
  1089. * status in the logic block above.
  1090. */
  1091. $post_status = 'publish';
  1092. }
  1093. /**
  1094. * Filters the post status.
  1095. *
  1096. * @since 4.4.0
  1097. * @since 5.7.0 The attachment post type is now passed through this filter.
  1098. *
  1099. * @param string $post_status The post status.
  1100. * @param WP_Post $post The post object.
  1101. */
  1102. return apply_filters( 'get_post_status', $post_status, $post );
  1103. }
  1104. /**
  1105. * Retrieves all of the WordPress supported post statuses.
  1106. *
  1107. * Posts have a limited set of valid status values, this provides the
  1108. * post_status values and descriptions.
  1109. *
  1110. * @since 2.5.0
  1111. *
  1112. * @return string[] Array of post status labels keyed by their status.
  1113. */
  1114. function get_post_statuses() {
  1115. $status = array(
  1116. 'draft' => __( 'Draft' ),
  1117. 'pending' => __( 'Pending Review' ),
  1118. 'private' => __( 'Private' ),
  1119. 'publish' => __( 'Published' ),
  1120. );
  1121. return $status;
  1122. }
  1123. /**
  1124. * Retrieves all of the WordPress support page statuses.
  1125. *
  1126. * Pages have a limited set of valid status values, this provides the
  1127. * post_status values and descriptions.
  1128. *
  1129. * @since 2.5.0
  1130. *
  1131. * @return string[] Array of page status labels keyed by their status.
  1132. */
  1133. function get_page_statuses() {
  1134. $status = array(
  1135. 'draft' => __( 'Draft' ),
  1136. 'private' => __( 'Private' ),
  1137. 'publish' => __( 'Published' ),
  1138. );
  1139. return $status;
  1140. }
  1141. /**
  1142. * Returns statuses for privacy requests.
  1143. *
  1144. * @since 4.9.6
  1145. * @access private
  1146. *
  1147. * @return array
  1148. */
  1149. function _wp_privacy_statuses() {
  1150. return array(
  1151. 'request-pending' => _x( 'Pending', 'request status' ), // Pending confirmation from user.
  1152. 'request-confirmed' => _x( 'Confirmed', 'request status' ), // User has confirmed the action.
  1153. 'request-failed' => _x( 'Failed', 'request status' ), // User failed to confirm the action.
  1154. 'request-completed' => _x( 'Completed', 'request status' ), // Admin has handled the request.
  1155. );
  1156. }
  1157. /**
  1158. * Registers a post status. Do not use before init.
  1159. *
  1160. * A simple function for creating or modifying a post status based on the
  1161. * parameters given. The function will accept an array (second optional
  1162. * parameter), along with a string for the post status name.
  1163. *
  1164. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  1165. *
  1166. * @since 3.0.0
  1167. *
  1168. * @global stdClass[] $wp_post_statuses Inserts new post status object into the list
  1169. *
  1170. * @param string $post_status Name of the post status.
  1171. * @param array|string $args {
  1172. * Optional. Array or string of post status arguments.
  1173. *
  1174. * @type bool|string $label A descriptive name for the post status marked
  1175. * for translation. Defaults to value of $post_status.
  1176. * @type array|false $label_count Nooped plural text from _n_noop() to provide the singular
  1177. * and plural forms of the label for counts. Default false
  1178. * which means the `$label` argument will be used for both
  1179. * the singular and plural forms of this label.
  1180. * @type bool $exclude_from_search Whether to exclude posts with this post status
  1181. * from search results. Default is value of $internal.
  1182. * @type bool $_builtin Whether the status is built-in. Core-use only.
  1183. * Default false.
  1184. * @type bool $public Whether posts of this status should be shown
  1185. * in the front end of the site. Default false.
  1186. * @type bool $internal Whether the status is for internal use only.
  1187. * Default false.
  1188. * @type bool $protected Whether posts with this status should be protected.
  1189. * Default false.
  1190. * @type bool $private Whether posts with this status should be private.
  1191. * Default false.
  1192. * @type bool $publicly_queryable Whether posts with this status should be publicly-
  1193. * queryable. Default is value of $public.
  1194. * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for
  1195. * their post type. Default is the opposite value
  1196. * of $internal.
  1197. * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at
  1198. * the top of the edit listings,
  1199. * e.g. All (12) | Published (9) | My Custom Status (2)
  1200. * Default is the opposite value of $internal.
  1201. * @type bool $date_floating Whether the post has a floating creation date.
  1202. * Default to false.
  1203. * }
  1204. * @return object
  1205. */
  1206. function register_post_status( $post_status, $args = array() ) {
  1207. global $wp_post_statuses;
  1208. if ( ! is_array( $wp_post_statuses ) ) {
  1209. $wp_post_statuses = array();
  1210. }
  1211. // Args prefixed with an underscore are reserved for internal use.
  1212. $defaults = array(
  1213. 'label' => false,
  1214. 'label_count' => false,
  1215. 'exclude_from_search' => null,
  1216. '_builtin' => false,
  1217. 'public' => null,
  1218. 'internal' => null,
  1219. 'protected' => null,
  1220. 'private' => null,
  1221. 'publicly_queryable' => null,
  1222. 'show_in_admin_status_list' => null,
  1223. 'show_in_admin_all_list' => null,
  1224. 'date_floating' => null,
  1225. );
  1226. $args = wp_parse_args( $args, $defaults );
  1227. $args = (object) $args;
  1228. $post_status = sanitize_key( $post_status );
  1229. $args->name = $post_status;
  1230. // Set various defaults.
  1231. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) {
  1232. $args->internal = true;
  1233. }
  1234. if ( null === $args->public ) {
  1235. $args->public = false;
  1236. }
  1237. if ( null === $args->private ) {
  1238. $args->private = false;
  1239. }
  1240. if ( null === $args->protected ) {
  1241. $args->protected = false;
  1242. }
  1243. if ( null === $args->internal ) {
  1244. $args->internal = false;
  1245. }
  1246. if ( null === $args->publicly_queryable ) {
  1247. $args->publicly_queryable = $args->public;
  1248. }
  1249. if ( null === $args->exclude_from_search ) {
  1250. $args->exclude_from_search = $args->internal;
  1251. }
  1252. if ( null === $args->show_in_admin_all_list ) {
  1253. $args->show_in_admin_all_list = ! $args->internal;
  1254. }
  1255. if ( null === $args->show_in_admin_status_list ) {
  1256. $args->show_in_admin_status_list = ! $args->internal;
  1257. }
  1258. if ( null === $args->date_floating ) {
  1259. $args->date_floating = false;
  1260. }
  1261. if ( false === $args->label ) {
  1262. $args->label = $post_status;
  1263. }
  1264. if ( false === $args->label_count ) {
  1265. // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural
  1266. $args->label_count = _n_noop( $args->label, $args->label );
  1267. }
  1268. $wp_post_statuses[ $post_status ] = $args;
  1269. return $args;
  1270. }
  1271. /**
  1272. * Retrieves a post status object by name.
  1273. *
  1274. * @since 3.0.0
  1275. *
  1276. * @global stdClass[] $wp_post_statuses List of post statuses.
  1277. *
  1278. * @see register_post_status()
  1279. *
  1280. * @param string $post_status The name of a registered post status.
  1281. * @return stdClass|null A post status object.
  1282. */
  1283. function get_post_status_object( $post_status ) {
  1284. global $wp_post_statuses;
  1285. if ( empty( $wp_post_statuses[ $post_status ] ) ) {
  1286. return null;
  1287. }
  1288. return $wp_post_statuses[ $post_status ];
  1289. }
  1290. /**
  1291. * Gets a list of post statuses.
  1292. *
  1293. * @since 3.0.0
  1294. *
  1295. * @global stdClass[] $wp_post_statuses List of post statuses.
  1296. *
  1297. * @see register_post_status()
  1298. *
  1299. * @param array|string $args Optional. Array or string of post status arguments to compare against
  1300. * properties of the global `$wp_post_statuses objects`. Default empty array.
  1301. * @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'.
  1302. * @param string $operator Optional. The logical operation to perform. 'or' means only one element
  1303. * from the array needs to match; 'and' means all elements must match.
  1304. * Default 'and'.
  1305. * @return string[]|stdClass[] A list of post status names or objects.
  1306. */
  1307. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  1308. global $wp_post_statuses;
  1309. $field = ( 'names' === $output ) ? 'name' : false;
  1310. return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field );
  1311. }
  1312. /**
  1313. * Determines whether the post type is hierarchical.
  1314. *
  1315. * A false return value might also mean that the post type does not exist.
  1316. *
  1317. * @since 3.0.0
  1318. *
  1319. * @see get_post_type_object()
  1320. *
  1321. * @param string $post_type Post type name
  1322. * @return bool Whether post type is hierarchical.
  1323. */
  1324. function is_post_type_hierarchical( $post_type ) {
  1325. if ( ! post_type_exists( $post_type ) ) {
  1326. return false;
  1327. }
  1328. $post_type = get_post_type_object( $post_type );
  1329. return $post_type->hierarchical;
  1330. }
  1331. /**
  1332. * Determines whether a post type is registered.
  1333. *
  1334. * For more information on this and similar theme functions, check out
  1335. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  1336. * Conditional Tags} article in the Theme Developer Handbook.
  1337. *
  1338. * @since 3.0.0
  1339. *
  1340. * @see get_post_type_object()
  1341. *
  1342. * @param string $post_type Post type name.
  1343. * @return bool Whether post type is registered.
  1344. */
  1345. function post_type_exists( $post_type ) {
  1346. return (bool) get_post_type_object( $post_type );
  1347. }
  1348. /**
  1349. * Retrieves the post type of the current post or of a given post.
  1350. *
  1351. * @since 2.1.0
  1352. *
  1353. * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post.
  1354. * @return string|false Post type on success, false on failure.
  1355. */
  1356. function get_post_type( $post = null ) {
  1357. $post = get_post( $post );
  1358. if ( $post ) {
  1359. return $post->post_type;
  1360. }
  1361. return false;
  1362. }
  1363. /**
  1364. * Retrieves a post type object by name.
  1365. *
  1366. * @since 3.0.0
  1367. * @since 4.6.0 Object returned is now an instance of `WP_Post_Type`.
  1368. *
  1369. * @global array $wp_post_types List of post types.
  1370. *
  1371. * @see register_post_type()
  1372. *
  1373. * @param string $post_type The name of a registered post type.
  1374. * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise.
  1375. */
  1376. function get_post_type_object( $post_type ) {
  1377. global $wp_post_types;
  1378. if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) {
  1379. return null;
  1380. }
  1381. return $wp_post_types[ $post_type ];
  1382. }
  1383. /**
  1384. * Gets a list of all registered post type objects.
  1385. *
  1386. * @since 2.9.0
  1387. *
  1388. * @global array $wp_post_types List of post types.
  1389. *
  1390. * @see register_post_type() for accepted arguments.
  1391. *
  1392. * @param array|string $args Optional. An array of key => value arguments to match against
  1393. * the post type objects. Default empty array.
  1394. * @param string $output Optional. The type of output to return. Accepts post type 'names'
  1395. * or 'objects'. Default 'names'.
  1396. * @param string $operator Optional. The logical operation to perform. 'or' means only one
  1397. * element from the array needs to match; 'and' means all elements
  1398. * must match; 'not' means no elements may match. Default 'and'.
  1399. * @return string[]|WP_Post_Type[] An array of post type names or objects.
  1400. */
  1401. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  1402. global $wp_post_types;
  1403. $field = ( 'names' === $output ) ? 'name' : false;
  1404. return wp_filter_object_list( $wp_post_types, $args, $operator, $field );
  1405. }
  1406. /**
  1407. * Registers a post type.
  1408. *
  1409. * Note: Post type registrations should not be hooked before the
  1410. * {@see 'init'} action. Also, any taxonomy connections should be
  1411. * registered via the `$taxonomies` argument to ensure consistency
  1412. * when hooks such as {@see 'parse_query'} or {@see 'pre_get_posts'}
  1413. * are used.
  1414. *
  1415. * Post types can support any number of built-in core features such
  1416. * as meta boxes, custom fields, post thumbnails, post statuses,
  1417. * comments, and more. See the `$supports` argument for a complete
  1418. * list of supported features.
  1419. *
  1420. * @since 2.9.0
  1421. * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen.
  1422. * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing
  1423. * screen and post editing screen.
  1424. * @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`.
  1425. * @since 4.7.0 Introduced `show_in_rest`, `rest_base` and `rest_controller_class`
  1426. * arguments to register the post type in REST API.
  1427. * @since 5.0.0 The `template` and `template_lock` arguments were added.
  1428. * @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature.
  1429. * @since 5.9.0 The `rest_namespace` argument was added.
  1430. *
  1431. * @global array $wp_post_types List of post types.
  1432. *
  1433. * @param string $post_type Post type key. Must not exceed 20 characters and may
  1434. * only contain lowercase alphanumeric characters, dashes,
  1435. * and underscores. See sanitize_key().
  1436. * @param array|string $args {
  1437. * Array or string of arguments for registering a post type.
  1438. *
  1439. * @type string $label Name of the post type shown in the menu. Usually plural.
  1440. * Default is value of $labels['name'].
  1441. * @type string[] $labels An array of labels for this post type. If not set, post
  1442. * labels are inherited for non-hierarchical types and page
  1443. * labels for hierarchical ones. See get_post_type_labels() for a full
  1444. * list of supported labels.
  1445. * @type string $description A short descriptive summary of what the post type is.
  1446. * Default empty.
  1447. * @type bool $public Whether a post type is intended for use publicly either via
  1448. * the admin interface or by front-end users. While the default
  1449. * settings of $exclude_from_search, $publicly_queryable, $show_ui,
  1450. * and $show_in_nav_menus are inherited from $public, each does not
  1451. * rely on this relationship and controls a very specific intention.
  1452. * Default false.
  1453. * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false.
  1454. * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search
  1455. * results. Default is the opposite value of $public.
  1456. * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type
  1457. * as part of parse_request(). Endpoints would include:
  1458. * * ?post_type={post_type_key}
  1459. * * ?{post_type_key}={single_post_slug}
  1460. * * ?{post_type_query_var}={single_post_slug}
  1461. * If not set, the default is inherited from $public.
  1462. * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the
  1463. * admin. Default is value of $public.
  1464. * @type bool|string $show_in_menu Where to show the post type in the admin menu. To work, $show_ui
  1465. * must be true. If true, the post type is shown in its own top level
  1466. * menu. If false, no menu is shown. If a string of an existing top
  1467. * level menu ('tools.php' or 'edit.php?post_type=page', for example), the
  1468. * post type will be placed as a sub-menu of that.
  1469. * Default is value of $show_ui.
  1470. * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus.
  1471. * Default is value of $public.
  1472. * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value
  1473. * of $show_in_menu.
  1474. * @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true
  1475. * for the post type to be available in the block editor.
  1476. * @type string $rest_base To change the base URL of REST API route. Default is $post_type.
  1477. * @type string $rest_namespace To change the namespace URL of REST API route. Default is wp/v2.
  1478. * @type string $rest_controller_class REST API controller class name. Default is 'WP_REST_Posts_Controller'.
  1479. * @type int $menu_position The position in the menu order the post type should appear. To work,
  1480. * $show_in_menu must be true. Default null (at the bottom).
  1481. * @type string $menu_icon The URL to the icon to be used for this menu. Pass a base64-encoded
  1482. * SVG using a data URI, which will be colored to match the color scheme
  1483. * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name
  1484. * of a Dashicons helper class to use a font icon, e.g.
  1485. * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty
  1486. * so an icon can be added via CSS. Defaults to use the posts icon.
  1487. * @type string|array $capability_type The string to use to build the read, edit, and delete capabilities.
  1488. * May be passed as an array to allow for alternative plurals when using
  1489. * this argument as a base to construct the capabilities, e.g.
  1490. * array('story', 'stories'). Default 'post'.
  1491. * @type string[] $capabilities Array of capabilities for this post type. $capability_type is used
  1492. * as a base to construct capabilities by default.
  1493. * See get_post_type_capabilities().
  1494. * @type bool $map_meta_cap Whether to use the internal default meta capability handling.
  1495. * Default false.
  1496. * @type array $supports Core feature(s) the post type supports. Serves as an alias for calling
  1497. * add_post_type_support() directly. Core features include 'title',
  1498. * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt',
  1499. * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'.
  1500. * Additionally, the 'revisions' feature dictates whether the post type
  1501. * will store revisions, and the 'comments' feature dictates whether the
  1502. * comments count will show on the edit screen. A feature can also be
  1503. * specified as an array of arguments to provide additional information
  1504. * about supporting that feature.
  1505. * Example: `array( 'my_feature', array( 'field' => 'value' ) )`.
  1506. * Default is an array containing 'title' and 'editor'.
  1507. * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the
  1508. * edit form. Do remove_meta_box() and add_meta_box() calls in the
  1509. * callback. Default null.
  1510. * @type string[] $taxonomies An array of taxonomy identifiers that will be registered for the
  1511. * post type. Taxonomies can be registered later with register_taxonomy()
  1512. * or register_taxonomy_for_object_type().
  1513. * Default empty array.
  1514. * @type bool|string $has_archive Whether there should be post type archives, or if a string, the
  1515. * archive slug to use. Will generate the proper rewrite rules if
  1516. * $rewrite is enabled. Default false.
  1517. * @type bool|array $rewrite {
  1518. * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false.
  1519. * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be
  1520. * passed with any of these keys:
  1521. *
  1522. * @type string $slug Customize the permastruct slug. Defaults to $post_type key.
  1523. * @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front.
  1524. * Default true.
  1525. * @type bool $feeds Whether the feed permastruct should be built for this post type.
  1526. * Default is value of $has_archive.
  1527. * @type bool $pages Whether the permastruct should provide for pagination. Default true.
  1528. * @type int $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set,
  1529. * inherits from $permalink_epmask. If not specified and permalink_epmask
  1530. * is not set, defaults to EP_PERMALINK.
  1531. * }
  1532. * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type
  1533. * key. If false, a post type cannot be loaded at
  1534. * ?{query_var}={post_slug}. If specified as a string, the query
  1535. * ?{query_var_string}={post_slug} will be valid.
  1536. * @type bool $can_export Whether to allow this post type to be exported. Default true.
  1537. * @type bool $delete_with_user Whether to delete posts of this type when deleting a user.
  1538. * * If true, posts of this type belonging to the user will be moved
  1539. * to Trash when the user is deleted.
  1540. * * If false, posts of this type belonging to the user will *not*
  1541. * be trashed or deleted.
  1542. * * If not set (the default), posts are trashed if post type supports
  1543. * the 'author' feature. Otherwise posts are not trashed or deleted.
  1544. * Default null.
  1545. * @type array $template Array of blocks to use as the default initial state for an editor
  1546. * session. Each item should be an array containing block name and
  1547. * optional attributes. Default empty array.
  1548. * @type string|false $template_lock Whether the block template should be locked if $template is set.
  1549. * * If set to 'all', the user is unable to insert new blocks,
  1550. * move existing blocks and delete blocks.
  1551. * * If set to 'insert', the user is able to move existing blocks
  1552. * but is unable to insert new blocks and delete blocks.
  1553. * Default false.
  1554. * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or
  1555. * "built-in" post_type. Default false.
  1556. * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of
  1557. * this post type. Default 'post.php?post=%d'.
  1558. * }
  1559. * @return WP_Post_Type|WP_Error The registered post type object on success,
  1560. * WP_Error object on failure.
  1561. */
  1562. function register_post_type( $post_type, $args = array() ) {
  1563. global $wp_post_types;
  1564. if ( ! is_array( $wp_post_types ) ) {
  1565. $wp_post_types = array();
  1566. }
  1567. // Sanitize post type name.
  1568. $post_type = sanitize_key( $post_type );
  1569. if ( empty( $post_type ) || strlen( $post_type ) > 20 ) {
  1570. _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' );
  1571. return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) );
  1572. }
  1573. $post_type_object = new WP_Post_Type( $post_type, $args );
  1574. $post_type_object->add_supports();
  1575. $post_type_object->add_rewrite_rules();
  1576. $post_type_object->register_meta_boxes();
  1577. $wp_post_types[ $post_type ] = $post_type_object;
  1578. $post_type_object->add_hooks();
  1579. $post_type_object->register_taxonomies();
  1580. /**
  1581. * Fires after a post type is registered.
  1582. *
  1583. * @since 3.3.0
  1584. * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
  1585. *
  1586. * @param string $post_type Post type.
  1587. * @param WP_Post_Type $post_type_object Arguments used to register the post type.
  1588. */
  1589. do_action( 'registered_post_type', $post_type, $post_type_object );
  1590. /**
  1591. * Fires after a specific post type is registered.
  1592. *
  1593. * The dynamic portion of the filter name, `$post_type`, refers to the post type key.
  1594. *
  1595. * Possible hook names include:
  1596. *
  1597. * - `registered_post_type_post`
  1598. * - `registered_post_type_page`
  1599. *
  1600. * @since 6.0.0
  1601. *
  1602. * @param string $post_type Post type.
  1603. * @param WP_Post_Type $post_type_object Arguments used to register the post type.
  1604. */
  1605. do_action( "registered_post_type_{$post_type}", $post_type, $post_type_object );
  1606. return $post_type_object;
  1607. }
  1608. /**
  1609. * Unregisters a post type.
  1610. *
  1611. * Cannot be used to unregister built-in post types.
  1612. *
  1613. * @since 4.5.0
  1614. *
  1615. * @global array $wp_post_types List of post types.
  1616. *
  1617. * @param string $post_type Post type to unregister.
  1618. * @return true|WP_Error True on success, WP_Error on failure or if the post type doesn't exist.
  1619. */
  1620. function unregister_post_type( $post_type ) {
  1621. global $wp_post_types;
  1622. if ( ! post_type_exists( $post_type ) ) {
  1623. return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) );
  1624. }
  1625. $post_type_object = get_post_type_object( $post_type );
  1626. // Do not allow unregistering internal post types.
  1627. if ( $post_type_object->_builtin ) {
  1628. return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) );
  1629. }
  1630. $post_type_object->remove_supports();
  1631. $post_type_object->remove_rewrite_rules();
  1632. $post_type_object->unregister_meta_boxes();
  1633. $post_type_object->remove_hooks();
  1634. $post_type_object->unregister_taxonomies();
  1635. unset( $wp_post_types[ $post_type ] );
  1636. /**
  1637. * Fires after a post type was unregistered.
  1638. *
  1639. * @since 4.5.0
  1640. *
  1641. * @param string $post_type Post type key.
  1642. */
  1643. do_action( 'unregistered_post_type', $post_type );
  1644. return true;
  1645. }
  1646. /**
  1647. * Builds an object with all post type capabilities out of a post type object
  1648. *
  1649. * Post type capabilities use the 'capability_type' argument as a base, if the
  1650. * capability is not set in the 'capabilities' argument array or if the
  1651. * 'capabilities' argument is not supplied.
  1652. *
  1653. * The capability_type argument can optionally be registered as an array, with
  1654. * the first value being singular and the second plural, e.g. array('story, 'stories')
  1655. * Otherwise, an 's' will be added to the value for the plural form. After
  1656. * registration, capability_type will always be a string of the singular value.
  1657. *
  1658. * By default, eight keys are accepted as part of the capabilities array:
  1659. *
  1660. * - edit_post, read_post, and delete_post are meta capabilities, which are then
  1661. * generally mapped to corresponding primitive capabilities depending on the
  1662. * context, which would be the post being edited/read/deleted and the user or
  1663. * role being checked. Thus these capabilities would generally not be granted
  1664. * directly to users or roles.
  1665. *
  1666. * - edit_posts - Controls whether objects of this post type can be edited.
  1667. * - edit_others_posts - Controls whether objects of this type owned by other users
  1668. * can be edited. If the post type does not support an author, then this will
  1669. * behave like edit_posts.
  1670. * - delete_posts - Controls whether objects of this post type can be deleted.
  1671. * - publish_posts - Controls publishing objects of this post type.
  1672. * - read_private_posts - Controls whether private objects can be read.
  1673. *
  1674. * These five primitive capabilities are checked in core in various locations.
  1675. * There are also six other primitive capabilities which are not referenced
  1676. * directly in core, except in map_meta_cap(), which takes the three aforementioned
  1677. * meta capabilities and translates them into one or more primitive capabilities
  1678. * that must then be checked against the user or role, depending on the context.
  1679. *
  1680. * - read - Controls whether objects of this post type can be read.
  1681. * - delete_private_posts - Controls whether private objects can be deleted.
  1682. * - delete_published_posts - Controls whether published objects can be deleted.
  1683. * - delete_others_posts - Controls whether objects owned by other users can be
  1684. * can be deleted. If the post type does not support an author, then this will
  1685. * behave like delete_posts.
  1686. * - edit_private_posts - Controls whether private objects can be edited.
  1687. * - edit_published_posts - Controls whether published objects can be edited.
  1688. *
  1689. * These additional capabilities are only used in map_meta_cap(). Thus, they are
  1690. * only assigned by default if the post type is registered with the 'map_meta_cap'
  1691. * argument set to true (default is false).
  1692. *
  1693. * @since 3.0.0
  1694. * @since 5.4.0 'delete_posts' is included in default capabilities.
  1695. *
  1696. * @see register_post_type()
  1697. * @see map_meta_cap()
  1698. *
  1699. * @param object $args Post type registration arguments.
  1700. * @return object Object with all the capabilities as member variables.
  1701. */
  1702. function get_post_type_capabilities( $args ) {
  1703. if ( ! is_array( $args->capability_type ) ) {
  1704. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
  1705. }
  1706. // Singular base for meta capabilities, plural base for primitive capabilities.
  1707. list( $singular_base, $plural_base ) = $args->capability_type;
  1708. $default_capabilities = array(
  1709. // Meta capabilities.
  1710. 'edit_post' => 'edit_' . $singular_base,
  1711. 'read_post' => 'read_' . $singular_base,
  1712. 'delete_post' => 'delete_' . $singular_base,
  1713. // Primitive capabilities used outside of map_meta_cap():
  1714. 'edit_posts' => 'edit_' . $plural_base,
  1715. 'edit_others_posts' => 'edit_others_' . $plural_base,
  1716. 'delete_posts' => 'delete_' . $plural_base,
  1717. 'publish_posts' => 'publish_' . $plural_base,
  1718. 'read_private_posts' => 'read_private_' . $plural_base,
  1719. );
  1720. // Primitive capabilities used within map_meta_cap():
  1721. if ( $args->map_meta_cap ) {
  1722. $default_capabilities_for_mapping = array(
  1723. 'read' => 'read',
  1724. 'delete_private_posts' => 'delete_private_' . $plural_base,
  1725. 'delete_published_posts' => 'delete_published_' . $plural_base,
  1726. 'delete_others_posts' => 'delete_others_' . $plural_base,
  1727. 'edit_private_posts' => 'edit_private_' . $plural_base,
  1728. 'edit_published_posts' => 'edit_published_' . $plural_base,
  1729. );
  1730. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1731. }
  1732. $capabilities = array_merge( $default_capabilities, $args->capabilities );
  1733. // Post creation capability simply maps to edit_posts by default:
  1734. if ( ! isset( $capabilities['create_posts'] ) ) {
  1735. $capabilities['create_posts'] = $capabilities['edit_posts'];
  1736. }
  1737. // Remember meta capabilities for future reference.
  1738. if ( $args->map_meta_cap ) {
  1739. _post_type_meta_capabilities( $capabilities );
  1740. }
  1741. return (object) $capabilities;
  1742. }
  1743. /**
  1744. * Stores or returns a list of post type meta caps for map_meta_cap().
  1745. *
  1746. * @since 3.1.0
  1747. * @access private
  1748. *
  1749. * @global array $post_type_meta_caps Used to store meta capabilities.
  1750. *
  1751. * @param string[] $capabilities Post type meta capabilities.
  1752. */
  1753. function _post_type_meta_capabilities( $capabilities = null ) {
  1754. global $post_type_meta_caps;
  1755. foreach ( $capabilities as $core => $custom ) {
  1756. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) {
  1757. $post_type_meta_caps[ $custom ] = $core;
  1758. }
  1759. }
  1760. }
  1761. /**
  1762. * Builds an object with all post type labels out of a post type object.
  1763. *
  1764. * Accepted keys of the label array in the post type object:
  1765. *
  1766. * - `name` - General name for the post type, usually plural. The same and overridden
  1767. * by `$post_type_object->label`. Default is 'Posts' / 'Pages'.
  1768. * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'.
  1769. * - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types.
  1770. * When internationalizing this string, please use a {@link https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#disambiguation-by-context gettext context}
  1771. * matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`.
  1772. * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'.
  1773. * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'.
  1774. * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'.
  1775. * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'.
  1776. * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'.
  1777. * - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'.
  1778. * - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'.
  1779. * - `not_found_in_trash` - Label used when no items are in the Trash. Default is 'No posts found in Trash' /
  1780. * 'No pages found in Trash'.
  1781. * - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical
  1782. * post types. Default is 'Parent Page:'.
  1783. * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'.
  1784. * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'.
  1785. * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'.
  1786. * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'.
  1787. * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' /
  1788. * 'Uploaded to this page'.
  1789. * - `featured_image` - Label for the featured image meta box title. Default is 'Featured image'.
  1790. * - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'.
  1791. * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'.
  1792. * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'.
  1793. * - `menu_name` - Label for the menu name. Default is the same as `name`.
  1794. * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' /
  1795. * 'Filter pages list'.
  1796. * - `filter_by_date` - Label for the date filter in list tables. Default is 'Filter by date'.
  1797. * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' /
  1798. * 'Pages list navigation'.
  1799. * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'.
  1800. * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.'
  1801. * - `item_published_privately` - Label used when an item is published with private visibility.
  1802. * Default is 'Post published privately.' / 'Page published privately.'
  1803. * - `item_reverted_to_draft` - Label used when an item is switched to a draft.
  1804. * Default is 'Post reverted to draft.' / 'Page reverted to draft.'
  1805. * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' /
  1806. * 'Page scheduled.'
  1807. * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.'
  1808. * - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'.
  1809. * - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' /
  1810. * 'A link to a page.'
  1811. *
  1812. * Above, the first default value is for non-hierarchical post types (like posts)
  1813. * and the second one is for hierarchical post types (like pages).
  1814. *
  1815. * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter.
  1816. *
  1817. * @since 3.0.0
  1818. * @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`,
  1819. * and `use_featured_image` labels.
  1820. * @since 4.4.0 Added the `archives`, `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`,
  1821. * `items_list_navigation`, and `items_list` labels.
  1822. * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
  1823. * @since 4.7.0 Added the `view_items` and `attributes` labels.
  1824. * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`,
  1825. * `item_scheduled`, and `item_updated` labels.
  1826. * @since 5.7.0 Added the `filter_by_date` label.
  1827. * @since 5.8.0 Added the `item_link` and `item_link_description` labels.
  1828. *
  1829. * @access private
  1830. *
  1831. * @param object|WP_Post_Type $post_type_object Post type object.
  1832. * @return object Object with all the labels as member variables.
  1833. */
  1834. function get_post_type_labels( $post_type_object ) {
  1835. $nohier_vs_hier_defaults = WP_Post_Type::get_default_labels();
  1836. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1837. $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1838. $post_type = $post_type_object->name;
  1839. $default_labels = clone $labels;
  1840. /**
  1841. * Filters the labels of a specific post type.
  1842. *
  1843. * The dynamic portion of the hook name, `$post_type`, refers to
  1844. * the post type slug.
  1845. *
  1846. * Possible hook names include:
  1847. *
  1848. * - `post_type_labels_post`
  1849. * - `post_type_labels_page`
  1850. * - `post_type_labels_attachment`
  1851. *
  1852. * @since 3.5.0
  1853. *
  1854. * @see get_post_type_labels() for the full list of labels.
  1855. *
  1856. * @param object $labels Object with labels for the post type as member variables.
  1857. */
  1858. $labels = apply_filters( "post_type_labels_{$post_type}", $labels );
  1859. // Ensure that the filtered labels contain all required default values.
  1860. $labels = (object) array_merge( (array) $default_labels, (array) $labels );
  1861. return $labels;
  1862. }
  1863. /**
  1864. * Builds an object with custom-something object (post type, taxonomy) labels
  1865. * out of a custom-something object
  1866. *
  1867. * @since 3.0.0
  1868. * @access private
  1869. *
  1870. * @param object $object A custom-something object.
  1871. * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels.
  1872. * @return object Object containing labels for the given custom-something object.
  1873. */
  1874. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
  1875. $object->labels = (array) $object->labels;
  1876. if ( isset( $object->label ) && empty( $object->labels['name'] ) ) {
  1877. $object->labels['name'] = $object->label;
  1878. }
  1879. if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) {
  1880. $object->labels['singular_name'] = $object->labels['name'];
  1881. }
  1882. if ( ! isset( $object->labels['name_admin_bar'] ) ) {
  1883. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
  1884. }
  1885. if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) {
  1886. $object->labels['menu_name'] = $object->labels['name'];
  1887. }
  1888. if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) {
  1889. $object->labels['all_items'] = $object->labels['menu_name'];
  1890. }
  1891. if ( ! isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) {
  1892. $object->labels['archives'] = $object->labels['all_items'];
  1893. }
  1894. $defaults = array();
  1895. foreach ( $nohier_vs_hier_defaults as $key => $value ) {
  1896. $defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0];
  1897. }
  1898. $labels = array_merge( $defaults, $object->labels );
  1899. $object->labels = (object) $object->labels;
  1900. return (object) $labels;
  1901. }
  1902. /**
  1903. * Adds submenus for post types.
  1904. *
  1905. * @access private
  1906. * @since 3.1.0
  1907. */
  1908. function _add_post_type_submenus() {
  1909. foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
  1910. $ptype_obj = get_post_type_object( $ptype );
  1911. // Sub-menus only.
  1912. if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) {
  1913. continue;
  1914. }
  1915. add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
  1916. }
  1917. }
  1918. /**
  1919. * Registers support of certain features for a post type.
  1920. *
  1921. * All core features are directly associated with a functional area of the edit
  1922. * screen, such as the editor or a meta box. Features include: 'title', 'editor',
  1923. * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes',
  1924. * 'thumbnail', 'custom-fields', and 'post-formats'.
  1925. *
  1926. * Additionally, the 'revisions' feature dictates whether the post type will
  1927. * store revisions, and the 'comments' feature dictates whether the comments
  1928. * count will show on the edit screen.
  1929. *
  1930. * A third, optional parameter can also be passed along with a feature to provide
  1931. * additional information about supporting that feature.
  1932. *
  1933. * Example usage:
  1934. *
  1935. * add_post_type_support( 'my_post_type', 'comments' );
  1936. * add_post_type_support( 'my_post_type', array(
  1937. * 'author', 'excerpt',
  1938. * ) );
  1939. * add_post_type_support( 'my_post_type', 'my_feature', array(
  1940. * 'field' => 'value',
  1941. * ) );
  1942. *
  1943. * @since 3.0.0
  1944. * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
  1945. * by adding it to the function signature.
  1946. *
  1947. * @global array $_wp_post_type_features
  1948. *
  1949. * @param string $post_type The post type for which to add the feature.
  1950. * @param string|array $feature The feature being added, accepts an array of
  1951. * feature strings or a single string.
  1952. * @param mixed ...$args Optional extra arguments to pass along with certain features.
  1953. */
  1954. function add_post_type_support( $post_type, $feature, ...$args ) {
  1955. global $_wp_post_type_features;
  1956. $features = (array) $feature;
  1957. foreach ( $features as $feature ) {
  1958. if ( $args ) {
  1959. $_wp_post_type_features[ $post_type ][ $feature ] = $args;
  1960. } else {
  1961. $_wp_post_type_features[ $post_type ][ $feature ] = true;
  1962. }
  1963. }
  1964. }
  1965. /**
  1966. * Removes support for a feature from a post type.
  1967. *
  1968. * @since 3.0.0
  1969. *
  1970. * @global array $_wp_post_type_features
  1971. *
  1972. * @param string $post_type The post type for which to remove the feature.
  1973. * @param string $feature The feature being removed.
  1974. */
  1975. function remove_post_type_support( $post_type, $feature ) {
  1976. global $_wp_post_type_features;
  1977. unset( $_wp_post_type_features[ $post_type ][ $feature ] );
  1978. }
  1979. /**
  1980. * Gets all the post type features
  1981. *
  1982. * @since 3.4.0
  1983. *
  1984. * @global array $_wp_post_type_features
  1985. *
  1986. * @param string $post_type The post type.
  1987. * @return array Post type supports list.
  1988. */
  1989. function get_all_post_type_supports( $post_type ) {
  1990. global $_wp_post_type_features;
  1991. if ( isset( $_wp_post_type_features[ $post_type ] ) ) {
  1992. return $_wp_post_type_features[ $post_type ];
  1993. }
  1994. return array();
  1995. }
  1996. /**
  1997. * Checks a post type's support for a given feature.
  1998. *
  1999. * @since 3.0.0
  2000. *
  2001. * @global array $_wp_post_type_features
  2002. *
  2003. * @param string $post_type The post type being checked.
  2004. * @param string $feature The feature being checked.
  2005. * @return bool Whether the post type supports the given feature.
  2006. */
  2007. function post_type_supports( $post_type, $feature ) {
  2008. global $_wp_post_type_features;
  2009. return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) );
  2010. }
  2011. /**
  2012. * Retrieves a list of post type names that support a specific feature.
  2013. *
  2014. * @since 4.5.0
  2015. *
  2016. * @global array $_wp_post_type_features Post type features
  2017. *
  2018. * @param array|string $feature Single feature or an array of features the post types should support.
  2019. * @param string $operator Optional. The logical operation to perform. 'or' means
  2020. * only one element from the array needs to match; 'and'
  2021. * means all elements must match; 'not' means no elements may
  2022. * match. Default 'and'.
  2023. * @return string[] A list of post type names.
  2024. */
  2025. function get_post_types_by_support( $feature, $operator = 'and' ) {
  2026. global $_wp_post_type_features;
  2027. $features = array_fill_keys( (array) $feature, true );
  2028. return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) );
  2029. }
  2030. /**
  2031. * Updates the post type for the post ID.
  2032. *
  2033. * The page or post cache will be cleaned for the post ID.
  2034. *
  2035. * @since 2.5.0
  2036. *
  2037. * @global wpdb $wpdb WordPress database abstraction object.
  2038. *
  2039. * @param int $post_id Optional. Post ID to change post type. Default 0.
  2040. * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to
  2041. * name a few. Default 'post'.
  2042. * @return int|false Amount of rows changed. Should be 1 for success and 0 for failure.
  2043. */
  2044. function set_post_type( $post_id = 0, $post_type = 'post' ) {
  2045. global $wpdb;
  2046. $post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' );
  2047. $return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) );
  2048. clean_post_cache( $post_id );
  2049. return $return;
  2050. }
  2051. /**
  2052. * Determines whether a post type is considered "viewable".
  2053. *
  2054. * For built-in post types such as posts and pages, the 'public' value will be evaluated.
  2055. * For all others, the 'publicly_queryable' value will be used.
  2056. *
  2057. * @since 4.4.0
  2058. * @since 4.5.0 Added the ability to pass a post type name in addition to object.
  2059. * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
  2060. * @since 5.9.0 Added `is_post_type_viewable` hook to filter the result.
  2061. *
  2062. * @param string|WP_Post_Type $post_type Post type name or object.
  2063. * @return bool Whether the post type should be considered viewable.
  2064. */
  2065. function is_post_type_viewable( $post_type ) {
  2066. if ( is_scalar( $post_type ) ) {
  2067. $post_type = get_post_type_object( $post_type );
  2068. if ( ! $post_type ) {
  2069. return false;
  2070. }
  2071. }
  2072. if ( ! is_object( $post_type ) ) {
  2073. return false;
  2074. }
  2075. $is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
  2076. /**
  2077. * Filters whether a post type is considered "viewable".
  2078. *
  2079. * The returned filtered value must be a boolean type to ensure
  2080. * `is_post_type_viewable()` only returns a boolean. This strictness
  2081. * is by design to maintain backwards-compatibility and guard against
  2082. * potential type errors in PHP 8.1+. Non-boolean values (even falsey
  2083. * and truthy values) will result in the function returning false.
  2084. *
  2085. * @since 5.9.0
  2086. *
  2087. * @param bool $is_viewable Whether the post type is "viewable" (strict type).
  2088. * @param WP_Post_Type $post_type Post type object.
  2089. */
  2090. return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type );
  2091. }
  2092. /**
  2093. * Determines whether a post status is considered "viewable".
  2094. *
  2095. * For built-in post statuses such as publish and private, the 'public' value will be evaluated.
  2096. * For all others, the 'publicly_queryable' value will be used.
  2097. *
  2098. * @since 5.7.0
  2099. * @since 5.9.0 Added `is_post_status_viewable` hook to filter the result.
  2100. *
  2101. * @param string|stdClass $post_status Post status name or object.
  2102. * @return bool Whether the post status should be considered viewable.
  2103. */
  2104. function is_post_status_viewable( $post_status ) {
  2105. if ( is_scalar( $post_status ) ) {
  2106. $post_status = get_post_status_object( $post_status );
  2107. if ( ! $post_status ) {
  2108. return false;
  2109. }
  2110. }
  2111. if (
  2112. ! is_object( $post_status ) ||
  2113. $post_status->internal ||
  2114. $post_status->protected
  2115. ) {
  2116. return false;
  2117. }
  2118. $is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public );
  2119. /**
  2120. * Filters whether a post status is considered "viewable".
  2121. *
  2122. * The returned filtered value must be a boolean type to ensure
  2123. * `is_post_status_viewable()` only returns a boolean. This strictness
  2124. * is by design to maintain backwards-compatibility and guard against
  2125. * potential type errors in PHP 8.1+. Non-boolean values (even falsey
  2126. * and truthy values) will result in the function returning false.
  2127. *
  2128. * @since 5.9.0
  2129. *
  2130. * @param bool $is_viewable Whether the post status is "viewable" (strict type).
  2131. * @param stdClass $post_status Post status object.
  2132. */
  2133. return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status );
  2134. }
  2135. /**
  2136. * Determines whether a post is publicly viewable.
  2137. *
  2138. * Posts are considered publicly viewable if both the post status and post type
  2139. * are viewable.
  2140. *
  2141. * @since 5.7.0
  2142. *
  2143. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
  2144. * @return bool Whether the post is publicly viewable.
  2145. */
  2146. function is_post_publicly_viewable( $post = null ) {
  2147. $post = get_post( $post );
  2148. if ( ! $post ) {
  2149. return false;
  2150. }
  2151. $post_type = get_post_type( $post );
  2152. $post_status = get_post_status( $post );
  2153. return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
  2154. }
  2155. /**
  2156. * Retrieves an array of the latest posts, or posts matching the given criteria.
  2157. *
  2158. * For more information on the accepted arguments, see the
  2159. * {@link https://developer.wordpress.org/reference/classes/wp_query/
  2160. * WP_Query} documentation in the Developer Handbook.
  2161. *
  2162. * The `$ignore_sticky_posts` and `$no_found_rows` arguments are ignored by
  2163. * this function and both are set to `true`.
  2164. *
  2165. * The defaults are as follows:
  2166. *
  2167. * @since 1.2.0
  2168. *
  2169. * @see WP_Query
  2170. * @see WP_Query::parse_query()
  2171. *
  2172. * @param array $args {
  2173. * Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments.
  2174. *
  2175. * @type int $numberposts Total number of posts to retrieve. Is an alias of `$posts_per_page`
  2176. * in WP_Query. Accepts -1 for all. Default 5.
  2177. * @type int|string $category Category ID or comma-separated list of IDs (this or any children).
  2178. * Is an alias of `$cat` in WP_Query. Default 0.
  2179. * @type int[] $include An array of post IDs to retrieve, sticky posts will be included.
  2180. * Is an alias of `$post__in` in WP_Query. Default empty array.
  2181. * @type int[] $exclude An array of post IDs not to retrieve. Default empty array.
  2182. * @type bool $suppress_filters Whether to suppress filters. Default true.
  2183. * }
  2184. * @return WP_Post[]|int[] Array of post objects or post IDs.
  2185. */
  2186. function get_posts( $args = null ) {
  2187. $defaults = array(
  2188. 'numberposts' => 5,
  2189. 'category' => 0,
  2190. 'orderby' => 'date',
  2191. 'order' => 'DESC',
  2192. 'include' => array(),
  2193. 'exclude' => array(),
  2194. 'meta_key' => '',
  2195. 'meta_value' => '',
  2196. 'post_type' => 'post',
  2197. 'suppress_filters' => true,
  2198. );
  2199. $parsed_args = wp_parse_args( $args, $defaults );
  2200. if ( empty( $parsed_args['post_status'] ) ) {
  2201. $parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish';
  2202. }
  2203. if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) {
  2204. $parsed_args['posts_per_page'] = $parsed_args['numberposts'];
  2205. }
  2206. if ( ! empty( $parsed_args['category'] ) ) {
  2207. $parsed_args['cat'] = $parsed_args['category'];
  2208. }
  2209. if ( ! empty( $parsed_args['include'] ) ) {
  2210. $incposts = wp_parse_id_list( $parsed_args['include'] );
  2211. $parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included.
  2212. $parsed_args['post__in'] = $incposts;
  2213. } elseif ( ! empty( $parsed_args['exclude'] ) ) {
  2214. $parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] );
  2215. }
  2216. $parsed_args['ignore_sticky_posts'] = true;
  2217. $parsed_args['no_found_rows'] = true;
  2218. $get_posts = new WP_Query;
  2219. return $get_posts->query( $parsed_args );
  2220. }
  2221. //
  2222. // Post meta functions.
  2223. //
  2224. /**
  2225. * Adds a meta field to the given post.
  2226. *
  2227. * Post meta data is called "Custom Fields" on the Administration Screen.
  2228. *
  2229. * @since 1.5.0
  2230. *
  2231. * @param int $post_id Post ID.
  2232. * @param string $meta_key Metadata name.
  2233. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
  2234. * @param bool $unique Optional. Whether the same key should not be added.
  2235. * Default false.
  2236. * @return int|false Meta ID on success, false on failure.
  2237. */
  2238. function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) {
  2239. // Make sure meta is added to the post, not a revision.
  2240. $the_post = wp_is_post_revision( $post_id );
  2241. if ( $the_post ) {
  2242. $post_id = $the_post;
  2243. }
  2244. return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
  2245. }
  2246. /**
  2247. * Deletes a post meta field for the given post ID.
  2248. *
  2249. * You can match based on the key, or key and value. Removing based on key and
  2250. * value, will keep from removing duplicate metadata with the same key. It also
  2251. * allows removing all metadata matching the key, if needed.
  2252. *
  2253. * @since 1.5.0
  2254. *
  2255. * @param int $post_id Post ID.
  2256. * @param string $meta_key Metadata name.
  2257. * @param mixed $meta_value Optional. Metadata value. If provided,
  2258. * rows will only be removed that match the value.
  2259. * Must be serializable if non-scalar. Default empty.
  2260. * @return bool True on success, false on failure.
  2261. */
  2262. function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) {
  2263. // Make sure meta is deleted from the post, not from a revision.
  2264. $the_post = wp_is_post_revision( $post_id );
  2265. if ( $the_post ) {
  2266. $post_id = $the_post;
  2267. }
  2268. return delete_metadata( 'post', $post_id, $meta_key, $meta_value );
  2269. }
  2270. /**
  2271. * Retrieves a post meta field for the given post ID.
  2272. *
  2273. * @since 1.5.0
  2274. *
  2275. * @param int $post_id Post ID.
  2276. * @param string $key Optional. The meta key to retrieve. By default,
  2277. * returns data for all keys. Default empty.
  2278. * @param bool $single Optional. Whether to return a single value.
  2279. * This parameter has no effect if `$key` is not specified.
  2280. * Default false.
  2281. * @return mixed An array of values if `$single` is false.
  2282. * The value of the meta field if `$single` is true.
  2283. * False for an invalid `$post_id` (non-numeric, zero, or negative value).
  2284. * An empty string if a valid but non-existing post ID is passed.
  2285. */
  2286. function get_post_meta( $post_id, $key = '', $single = false ) {
  2287. return get_metadata( 'post', $post_id, $key, $single );
  2288. }
  2289. /**
  2290. * Updates a post meta field based on the given post ID.
  2291. *
  2292. * Use the `$prev_value` parameter to differentiate between meta fields with the
  2293. * same key and post ID.
  2294. *
  2295. * If the meta field for the post does not exist, it will be added and its ID returned.
  2296. *
  2297. * Can be used in place of add_post_meta().
  2298. *
  2299. * @since 1.5.0
  2300. *
  2301. * @param int $post_id Post ID.
  2302. * @param string $meta_key Metadata key.
  2303. * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
  2304. * @param mixed $prev_value Optional. Previous value to check before updating.
  2305. * If specified, only update existing metadata entries with
  2306. * this value. Otherwise, update all entries. Default empty.
  2307. * @return int|bool Meta ID if the key didn't exist, true on successful update,
  2308. * false on failure or if the value passed to the function
  2309. * is the same as the one that is already in the database.
  2310. */
  2311. function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
  2312. // Make sure meta is updated for the post, not for a revision.
  2313. $the_post = wp_is_post_revision( $post_id );
  2314. if ( $the_post ) {
  2315. $post_id = $the_post;
  2316. }
  2317. return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
  2318. }
  2319. /**
  2320. * Deletes everything from post meta matching the given meta key.
  2321. *
  2322. * @since 2.3.0
  2323. *
  2324. * @param string $post_meta_key Key to search for when deleting.
  2325. * @return bool Whether the post meta key was deleted from the database.
  2326. */
  2327. function delete_post_meta_by_key( $post_meta_key ) {
  2328. return delete_metadata( 'post', null, $post_meta_key, '', true );
  2329. }
  2330. /**
  2331. * Registers a meta key for posts.
  2332. *
  2333. * @since 4.9.8
  2334. *
  2335. * @param string $post_type Post type to register a meta key for. Pass an empty string
  2336. * to register the meta key across all existing post types.
  2337. * @param string $meta_key The meta key to register.
  2338. * @param array $args Data used to describe the meta key when registered. See
  2339. * {@see register_meta()} for a list of supported arguments.
  2340. * @return bool True if the meta key was successfully registered, false if not.
  2341. */
  2342. function register_post_meta( $post_type, $meta_key, array $args ) {
  2343. $args['object_subtype'] = $post_type;
  2344. return register_meta( 'post', $meta_key, $args );
  2345. }
  2346. /**
  2347. * Unregisters a meta key for posts.
  2348. *
  2349. * @since 4.9.8
  2350. *
  2351. * @param string $post_type Post type the meta key is currently registered for. Pass
  2352. * an empty string if the meta key is registered across all
  2353. * existing post types.
  2354. * @param string $meta_key The meta key to unregister.
  2355. * @return bool True on success, false if the meta key was not previously registered.
  2356. */
  2357. function unregister_post_meta( $post_type, $meta_key ) {
  2358. return unregister_meta_key( 'post', $meta_key, $post_type );
  2359. }
  2360. /**
  2361. * Retrieves post meta fields, based on post ID.
  2362. *
  2363. * The post meta fields are retrieved from the cache where possible,
  2364. * so the function is optimized to be called more than once.
  2365. *
  2366. * @since 1.2.0
  2367. *
  2368. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  2369. * @return mixed An array of values.
  2370. * False for an invalid `$post_id` (non-numeric, zero, or negative value).
  2371. * An empty string if a valid but non-existing post ID is passed.
  2372. */
  2373. function get_post_custom( $post_id = 0 ) {
  2374. $post_id = absint( $post_id );
  2375. if ( ! $post_id ) {
  2376. $post_id = get_the_ID();
  2377. }
  2378. return get_post_meta( $post_id );
  2379. }
  2380. /**
  2381. * Retrieves meta field names for a post.
  2382. *
  2383. * If there are no meta fields, then nothing (null) will be returned.
  2384. *
  2385. * @since 1.2.0
  2386. *
  2387. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  2388. * @return array|void Array of the keys, if retrieved.
  2389. */
  2390. function get_post_custom_keys( $post_id = 0 ) {
  2391. $custom = get_post_custom( $post_id );
  2392. if ( ! is_array( $custom ) ) {
  2393. return;
  2394. }
  2395. $keys = array_keys( $custom );
  2396. if ( $keys ) {
  2397. return $keys;
  2398. }
  2399. }
  2400. /**
  2401. * Retrieves values for a custom post field.
  2402. *
  2403. * The parameters must not be considered optional. All of the post meta fields
  2404. * will be retrieved and only the meta field key values returned.
  2405. *
  2406. * @since 1.2.0
  2407. *
  2408. * @param string $key Optional. Meta field key. Default empty.
  2409. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  2410. * @return array|null Meta field values.
  2411. */
  2412. function get_post_custom_values( $key = '', $post_id = 0 ) {
  2413. if ( ! $key ) {
  2414. return null;
  2415. }
  2416. $custom = get_post_custom( $post_id );
  2417. return isset( $custom[ $key ] ) ? $custom[ $key ] : null;
  2418. }
  2419. /**
  2420. * Determines whether a post is sticky.
  2421. *
  2422. * Sticky posts should remain at the top of The Loop. If the post ID is not
  2423. * given, then The Loop ID for the current post will be used.
  2424. *
  2425. * For more information on this and similar theme functions, check out
  2426. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  2427. * Conditional Tags} article in the Theme Developer Handbook.
  2428. *
  2429. * @since 2.7.0
  2430. *
  2431. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  2432. * @return bool Whether post is sticky.
  2433. */
  2434. function is_sticky( $post_id = 0 ) {
  2435. $post_id = absint( $post_id );
  2436. if ( ! $post_id ) {
  2437. $post_id = get_the_ID();
  2438. }
  2439. $stickies = get_option( 'sticky_posts' );
  2440. if ( is_array( $stickies ) ) {
  2441. $stickies = array_map( 'intval', $stickies );
  2442. $is_sticky = in_array( $post_id, $stickies, true );
  2443. } else {
  2444. $is_sticky = false;
  2445. }
  2446. /**
  2447. * Filters whether a post is sticky.
  2448. *
  2449. * @since 5.3.0
  2450. *
  2451. * @param bool $is_sticky Whether a post is sticky.
  2452. * @param int $post_id Post ID.
  2453. */
  2454. return apply_filters( 'is_sticky', $is_sticky, $post_id );
  2455. }
  2456. /**
  2457. * Sanitizes every post field.
  2458. *
  2459. * If the context is 'raw', then the post object or array will get minimal
  2460. * sanitization of the integer fields.
  2461. *
  2462. * @since 2.3.0
  2463. *
  2464. * @see sanitize_post_field()
  2465. *
  2466. * @param object|WP_Post|array $post The post object or array
  2467. * @param string $context Optional. How to sanitize post fields.
  2468. * Accepts 'raw', 'edit', 'db', 'display',
  2469. * 'attribute', or 'js'. Default 'display'.
  2470. * @return object|WP_Post|array The now sanitized post object or array (will be the
  2471. * same type as `$post`).
  2472. */
  2473. function sanitize_post( $post, $context = 'display' ) {
  2474. if ( is_object( $post ) ) {
  2475. // Check if post already filtered for this context.
  2476. if ( isset( $post->filter ) && $context == $post->filter ) {
  2477. return $post;
  2478. }
  2479. if ( ! isset( $post->ID ) ) {
  2480. $post->ID = 0;
  2481. }
  2482. foreach ( array_keys( get_object_vars( $post ) ) as $field ) {
  2483. $post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context );
  2484. }
  2485. $post->filter = $context;
  2486. } elseif ( is_array( $post ) ) {
  2487. // Check if post already filtered for this context.
  2488. if ( isset( $post['filter'] ) && $context == $post['filter'] ) {
  2489. return $post;
  2490. }
  2491. if ( ! isset( $post['ID'] ) ) {
  2492. $post['ID'] = 0;
  2493. }
  2494. foreach ( array_keys( $post ) as $field ) {
  2495. $post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context );
  2496. }
  2497. $post['filter'] = $context;
  2498. }
  2499. return $post;
  2500. }
  2501. /**
  2502. * Sanitizes a post field based on context.
  2503. *
  2504. * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and
  2505. * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts
  2506. * are treated like 'display' when calling filters.
  2507. *
  2508. * @since 2.3.0
  2509. * @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'.
  2510. *
  2511. * @param string $field The Post Object field name.
  2512. * @param mixed $value The Post Object value.
  2513. * @param int $post_id Post ID.
  2514. * @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit',
  2515. * 'db', 'display', 'attribute' and 'js'. Default 'display'.
  2516. * @return mixed Sanitized value.
  2517. */
  2518. function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) {
  2519. $int_fields = array( 'ID', 'post_parent', 'menu_order' );
  2520. if ( in_array( $field, $int_fields, true ) ) {
  2521. $value = (int) $value;
  2522. }
  2523. // Fields which contain arrays of integers.
  2524. $array_int_fields = array( 'ancestors' );
  2525. if ( in_array( $field, $array_int_fields, true ) ) {
  2526. $value = array_map( 'absint', $value );
  2527. return $value;
  2528. }
  2529. if ( 'raw' === $context ) {
  2530. return $value;
  2531. }
  2532. $prefixed = false;
  2533. if ( false !== strpos( $field, 'post_' ) ) {
  2534. $prefixed = true;
  2535. $field_no_prefix = str_replace( 'post_', '', $field );
  2536. }
  2537. if ( 'edit' === $context ) {
  2538. $format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' );
  2539. if ( $prefixed ) {
  2540. /**
  2541. * Filters the value of a specific post field to edit.
  2542. *
  2543. * The dynamic portion of the hook name, `$field`, refers to the post
  2544. * field name.
  2545. *
  2546. * @since 2.3.0
  2547. *
  2548. * @param mixed $value Value of the post field.
  2549. * @param int $post_id Post ID.
  2550. */
  2551. $value = apply_filters( "edit_{$field}", $value, $post_id );
  2552. /**
  2553. * Filters the value of a specific post field to edit.
  2554. *
  2555. * The dynamic portion of the hook name, `$field_no_prefix`, refers to
  2556. * the post field name.
  2557. *
  2558. * @since 2.3.0
  2559. *
  2560. * @param mixed $value Value of the post field.
  2561. * @param int $post_id Post ID.
  2562. */
  2563. $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id );
  2564. } else {
  2565. $value = apply_filters( "edit_post_{$field}", $value, $post_id );
  2566. }
  2567. if ( in_array( $field, $format_to_edit, true ) ) {
  2568. if ( 'post_content' === $field ) {
  2569. $value = format_to_edit( $value, user_can_richedit() );
  2570. } else {
  2571. $value = format_to_edit( $value );
  2572. }
  2573. } else {
  2574. $value = esc_attr( $value );
  2575. }
  2576. } elseif ( 'db' === $context ) {
  2577. if ( $prefixed ) {
  2578. /**
  2579. * Filters the value of a specific post field before saving.
  2580. *
  2581. * The dynamic portion of the hook name, `$field`, refers to the post
  2582. * field name.
  2583. *
  2584. * @since 2.3.0
  2585. *
  2586. * @param mixed $value Value of the post field.
  2587. */
  2588. $value = apply_filters( "pre_{$field}", $value );
  2589. /**
  2590. * Filters the value of a specific field before saving.
  2591. *
  2592. * The dynamic portion of the hook name, `$field_no_prefix`, refers
  2593. * to the post field name.
  2594. *
  2595. * @since 2.3.0
  2596. *
  2597. * @param mixed $value Value of the post field.
  2598. */
  2599. $value = apply_filters( "{$field_no_prefix}_save_pre", $value );
  2600. } else {
  2601. $value = apply_filters( "pre_post_{$field}", $value );
  2602. /**
  2603. * Filters the value of a specific post field before saving.
  2604. *
  2605. * The dynamic portion of the hook name, `$field`, refers to the post
  2606. * field name.
  2607. *
  2608. * @since 2.3.0
  2609. *
  2610. * @param mixed $value Value of the post field.
  2611. */
  2612. $value = apply_filters( "{$field}_pre", $value );
  2613. }
  2614. } else {
  2615. // Use display filters by default.
  2616. if ( $prefixed ) {
  2617. /**
  2618. * Filters the value of a specific post field for display.
  2619. *
  2620. * The dynamic portion of the hook name, `$field`, refers to the post
  2621. * field name.
  2622. *
  2623. * @since 2.3.0
  2624. *
  2625. * @param mixed $value Value of the prefixed post field.
  2626. * @param int $post_id Post ID.
  2627. * @param string $context Context for how to sanitize the field.
  2628. * Accepts 'raw', 'edit', 'db', 'display',
  2629. * 'attribute', or 'js'. Default 'display'.
  2630. */
  2631. $value = apply_filters( "{$field}", $value, $post_id, $context );
  2632. } else {
  2633. $value = apply_filters( "post_{$field}", $value, $post_id, $context );
  2634. }
  2635. if ( 'attribute' === $context ) {
  2636. $value = esc_attr( $value );
  2637. } elseif ( 'js' === $context ) {
  2638. $value = esc_js( $value );
  2639. }
  2640. }
  2641. // Restore the type for integer fields after esc_attr().
  2642. if ( in_array( $field, $int_fields, true ) ) {
  2643. $value = (int) $value;
  2644. }
  2645. return $value;
  2646. }
  2647. /**
  2648. * Makes a post sticky.
  2649. *
  2650. * Sticky posts should be displayed at the top of the front page.
  2651. *
  2652. * @since 2.7.0
  2653. *
  2654. * @param int $post_id Post ID.
  2655. */
  2656. function stick_post( $post_id ) {
  2657. $post_id = (int) $post_id;
  2658. $stickies = get_option( 'sticky_posts' );
  2659. $updated = false;
  2660. if ( ! is_array( $stickies ) ) {
  2661. $stickies = array();
  2662. } else {
  2663. $stickies = array_unique( array_map( 'intval', $stickies ) );
  2664. }
  2665. if ( ! in_array( $post_id, $stickies, true ) ) {
  2666. $stickies[] = $post_id;
  2667. $updated = update_option( 'sticky_posts', array_values( $stickies ) );
  2668. }
  2669. if ( $updated ) {
  2670. /**
  2671. * Fires once a post has been added to the sticky list.
  2672. *
  2673. * @since 4.6.0
  2674. *
  2675. * @param int $post_id ID of the post that was stuck.
  2676. */
  2677. do_action( 'post_stuck', $post_id );
  2678. }
  2679. }
  2680. /**
  2681. * Un-sticks a post.
  2682. *
  2683. * Sticky posts should be displayed at the top of the front page.
  2684. *
  2685. * @since 2.7.0
  2686. *
  2687. * @param int $post_id Post ID.
  2688. */
  2689. function unstick_post( $post_id ) {
  2690. $post_id = (int) $post_id;
  2691. $stickies = get_option( 'sticky_posts' );
  2692. if ( ! is_array( $stickies ) ) {
  2693. return;
  2694. }
  2695. $stickies = array_values( array_unique( array_map( 'intval', $stickies ) ) );
  2696. if ( ! in_array( $post_id, $stickies, true ) ) {
  2697. return;
  2698. }
  2699. $offset = array_search( $post_id, $stickies, true );
  2700. if ( false === $offset ) {
  2701. return;
  2702. }
  2703. array_splice( $stickies, $offset, 1 );
  2704. $updated = update_option( 'sticky_posts', $stickies );
  2705. if ( $updated ) {
  2706. /**
  2707. * Fires once a post has been removed from the sticky list.
  2708. *
  2709. * @since 4.6.0
  2710. *
  2711. * @param int $post_id ID of the post that was unstuck.
  2712. */
  2713. do_action( 'post_unstuck', $post_id );
  2714. }
  2715. }
  2716. /**
  2717. * Returns the cache key for wp_count_posts() based on the passed arguments.
  2718. *
  2719. * @since 3.9.0
  2720. * @access private
  2721. *
  2722. * @param string $type Optional. Post type to retrieve count Default 'post'.
  2723. * @param string $perm Optional. 'readable' or empty. Default empty.
  2724. * @return string The cache key.
  2725. */
  2726. function _count_posts_cache_key( $type = 'post', $perm = '' ) {
  2727. $cache_key = 'posts-' . $type;
  2728. if ( 'readable' === $perm && is_user_logged_in() ) {
  2729. $post_type_object = get_post_type_object( $type );
  2730. if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
  2731. $cache_key .= '_' . $perm . '_' . get_current_user_id();
  2732. }
  2733. }
  2734. return $cache_key;
  2735. }
  2736. /**
  2737. * Counts number of posts of a post type and if user has permissions to view.
  2738. *
  2739. * This function provides an efficient method of finding the amount of post's
  2740. * type a blog has. Another method is to count the amount of items in
  2741. * get_posts(), but that method has a lot of overhead with doing so. Therefore,
  2742. * when developing for 2.5+, use this function instead.
  2743. *
  2744. * The $perm parameter checks for 'readable' value and if the user can read
  2745. * private posts, it will display that for the user that is signed in.
  2746. *
  2747. * @since 2.5.0
  2748. *
  2749. * @global wpdb $wpdb WordPress database abstraction object.
  2750. *
  2751. * @param string $type Optional. Post type to retrieve count. Default 'post'.
  2752. * @param string $perm Optional. 'readable' or empty. Default empty.
  2753. * @return stdClass Number of posts for each status.
  2754. */
  2755. function wp_count_posts( $type = 'post', $perm = '' ) {
  2756. global $wpdb;
  2757. if ( ! post_type_exists( $type ) ) {
  2758. return new stdClass;
  2759. }
  2760. $cache_key = _count_posts_cache_key( $type, $perm );
  2761. $counts = wp_cache_get( $cache_key, 'counts' );
  2762. if ( false !== $counts ) {
  2763. // We may have cached this before every status was registered.
  2764. foreach ( get_post_stati() as $status ) {
  2765. if ( ! isset( $counts->{$status} ) ) {
  2766. $counts->{$status} = 0;
  2767. }
  2768. }
  2769. /** This filter is documented in wp-includes/post.php */
  2770. return apply_filters( 'wp_count_posts', $counts, $type, $perm );
  2771. }
  2772. $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
  2773. if ( 'readable' === $perm && is_user_logged_in() ) {
  2774. $post_type_object = get_post_type_object( $type );
  2775. if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
  2776. $query .= $wpdb->prepare(
  2777. " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
  2778. get_current_user_id()
  2779. );
  2780. }
  2781. }
  2782. $query .= ' GROUP BY post_status';
  2783. $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
  2784. $counts = array_fill_keys( get_post_stati(), 0 );
  2785. foreach ( $results as $row ) {
  2786. $counts[ $row['post_status'] ] = $row['num_posts'];
  2787. }
  2788. $counts = (object) $counts;
  2789. wp_cache_set( $cache_key, $counts, 'counts' );
  2790. /**
  2791. * Modifies returned post counts by status for the current post type.
  2792. *
  2793. * @since 3.7.0
  2794. *
  2795. * @param stdClass $counts An object containing the current post_type's post
  2796. * counts by status.
  2797. * @param string $type Post type.
  2798. * @param string $perm The permission to determine if the posts are 'readable'
  2799. * by the current user.
  2800. */
  2801. return apply_filters( 'wp_count_posts', $counts, $type, $perm );
  2802. }
  2803. /**
  2804. * Counts number of attachments for the mime type(s).
  2805. *
  2806. * If you set the optional mime_type parameter, then an array will still be
  2807. * returned, but will only have the item you are looking for. It does not give
  2808. * you the number of attachments that are children of a post. You can get that
  2809. * by counting the number of children that post has.
  2810. *
  2811. * @since 2.5.0
  2812. *
  2813. * @global wpdb $wpdb WordPress database abstraction object.
  2814. *
  2815. * @param string|string[] $mime_type Optional. Array or comma-separated list of
  2816. * MIME patterns. Default empty.
  2817. * @return stdClass An object containing the attachment counts by mime type.
  2818. */
  2819. function wp_count_attachments( $mime_type = '' ) {
  2820. global $wpdb;
  2821. $cache_key = sprintf(
  2822. 'attachments%s',
  2823. ! empty( $mime_type ) ? ':' . str_replace( '/', '_', implode( '-', (array) $mime_type ) ) : ''
  2824. );
  2825. $counts = wp_cache_get( $cache_key, 'counts' );
  2826. if ( false == $counts ) {
  2827. $and = wp_post_mime_type_where( $mime_type );
  2828. $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A );
  2829. $counts = array();
  2830. foreach ( (array) $count as $row ) {
  2831. $counts[ $row['post_mime_type'] ] = $row['num_posts'];
  2832. }
  2833. $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" );
  2834. wp_cache_set( $cache_key, (object) $counts, 'counts' );
  2835. }
  2836. /**
  2837. * Modifies returned attachment counts by mime type.
  2838. *
  2839. * @since 3.7.0
  2840. *
  2841. * @param stdClass $counts An object containing the attachment counts by
  2842. * mime type.
  2843. * @param string|string[] $mime_type Array or comma-separated list of MIME patterns.
  2844. */
  2845. return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type );
  2846. }
  2847. /**
  2848. * Gets default post mime types.
  2849. *
  2850. * @since 2.9.0
  2851. * @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups.
  2852. *
  2853. * @return array List of post mime types.
  2854. */
  2855. function get_post_mime_types() {
  2856. $post_mime_types = array( // array( adj, noun )
  2857. 'image' => array(
  2858. __( 'Images' ),
  2859. __( 'Manage Images' ),
  2860. /* translators: %s: Number of images. */
  2861. _n_noop(
  2862. 'Image <span class="count">(%s)</span>',
  2863. 'Images <span class="count">(%s)</span>'
  2864. ),
  2865. ),
  2866. 'audio' => array(
  2867. _x( 'Audio', 'file type group' ),
  2868. __( 'Manage Audio' ),
  2869. /* translators: %s: Number of audio files. */
  2870. _n_noop(
  2871. 'Audio <span class="count">(%s)</span>',
  2872. 'Audio <span class="count">(%s)</span>'
  2873. ),
  2874. ),
  2875. 'video' => array(
  2876. _x( 'Video', 'file type group' ),
  2877. __( 'Manage Video' ),
  2878. /* translators: %s: Number of video files. */
  2879. _n_noop(
  2880. 'Video <span class="count">(%s)</span>',
  2881. 'Video <span class="count">(%s)</span>'
  2882. ),
  2883. ),
  2884. 'document' => array(
  2885. __( 'Documents' ),
  2886. __( 'Manage Documents' ),
  2887. /* translators: %s: Number of documents. */
  2888. _n_noop(
  2889. 'Document <span class="count">(%s)</span>',
  2890. 'Documents <span class="count">(%s)</span>'
  2891. ),
  2892. ),
  2893. 'spreadsheet' => array(
  2894. __( 'Spreadsheets' ),
  2895. __( 'Manage Spreadsheets' ),
  2896. /* translators: %s: Number of spreadsheets. */
  2897. _n_noop(
  2898. 'Spreadsheet <span class="count">(%s)</span>',
  2899. 'Spreadsheets <span class="count">(%s)</span>'
  2900. ),
  2901. ),
  2902. 'archive' => array(
  2903. _x( 'Archives', 'file type group' ),
  2904. __( 'Manage Archives' ),
  2905. /* translators: %s: Number of archives. */
  2906. _n_noop(
  2907. 'Archive <span class="count">(%s)</span>',
  2908. 'Archives <span class="count">(%s)</span>'
  2909. ),
  2910. ),
  2911. );
  2912. $ext_types = wp_get_ext_types();
  2913. $mime_types = wp_get_mime_types();
  2914. foreach ( $post_mime_types as $group => $labels ) {
  2915. if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) {
  2916. continue;
  2917. }
  2918. if ( ! isset( $ext_types[ $group ] ) ) {
  2919. unset( $post_mime_types[ $group ] );
  2920. continue;
  2921. }
  2922. $group_mime_types = array();
  2923. foreach ( $ext_types[ $group ] as $extension ) {
  2924. foreach ( $mime_types as $exts => $mime ) {
  2925. if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
  2926. $group_mime_types[] = $mime;
  2927. break;
  2928. }
  2929. }
  2930. }
  2931. $group_mime_types = implode( ',', array_unique( $group_mime_types ) );
  2932. $post_mime_types[ $group_mime_types ] = $labels;
  2933. unset( $post_mime_types[ $group ] );
  2934. }
  2935. /**
  2936. * Filters the default list of post mime types.
  2937. *
  2938. * @since 2.5.0
  2939. *
  2940. * @param array $post_mime_types Default list of post mime types.
  2941. */
  2942. return apply_filters( 'post_mime_types', $post_mime_types );
  2943. }
  2944. /**
  2945. * Checks a MIME-Type against a list.
  2946. *
  2947. * If the `$wildcard_mime_types` parameter is a string, it must be comma separated
  2948. * list. If the `$real_mime_types` is a string, it is also comma separated to
  2949. * create the list.
  2950. *
  2951. * @since 2.5.0
  2952. *
  2953. * @param string|string[] $wildcard_mime_types Mime types, e.g. `audio/mpeg`, `image` (same as `image/*`),
  2954. * or `flash` (same as `*flash*`).
  2955. * @param string|string[] $real_mime_types Real post mime type values.
  2956. * @return array array(wildcard=>array(real types)).
  2957. */
  2958. function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) {
  2959. $matches = array();
  2960. if ( is_string( $wildcard_mime_types ) ) {
  2961. $wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) );
  2962. }
  2963. if ( is_string( $real_mime_types ) ) {
  2964. $real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) );
  2965. }
  2966. $patternses = array();
  2967. $wild = '[-._a-z0-9]*';
  2968. foreach ( (array) $wildcard_mime_types as $type ) {
  2969. $mimes = array_map( 'trim', explode( ',', $type ) );
  2970. foreach ( $mimes as $mime ) {
  2971. $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) );
  2972. $patternses[][ $type ] = "^$regex$";
  2973. if ( false === strpos( $mime, '/' ) ) {
  2974. $patternses[][ $type ] = "^$regex/";
  2975. $patternses[][ $type ] = $regex;
  2976. }
  2977. }
  2978. }
  2979. asort( $patternses );
  2980. foreach ( $patternses as $patterns ) {
  2981. foreach ( $patterns as $type => $pattern ) {
  2982. foreach ( (array) $real_mime_types as $real ) {
  2983. if ( preg_match( "#$pattern#", $real )
  2984. && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) )
  2985. ) {
  2986. $matches[ $type ][] = $real;
  2987. }
  2988. }
  2989. }
  2990. }
  2991. return $matches;
  2992. }
  2993. /**
  2994. * Converts MIME types into SQL.
  2995. *
  2996. * @since 2.5.0
  2997. *
  2998. * @param string|string[] $post_mime_types List of mime types or comma separated string
  2999. * of mime types.
  3000. * @param string $table_alias Optional. Specify a table alias, if needed.
  3001. * Default empty.
  3002. * @return string The SQL AND clause for mime searching.
  3003. */
  3004. function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) {
  3005. $where = '';
  3006. $wildcards = array( '', '%', '%/%' );
  3007. if ( is_string( $post_mime_types ) ) {
  3008. $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) );
  3009. }
  3010. $wheres = array();
  3011. foreach ( (array) $post_mime_types as $mime_type ) {
  3012. $mime_type = preg_replace( '/\s/', '', $mime_type );
  3013. $slashpos = strpos( $mime_type, '/' );
  3014. if ( false !== $slashpos ) {
  3015. $mime_group = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) );
  3016. $mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) );
  3017. if ( empty( $mime_subgroup ) ) {
  3018. $mime_subgroup = '*';
  3019. } else {
  3020. $mime_subgroup = str_replace( '/', '', $mime_subgroup );
  3021. }
  3022. $mime_pattern = "$mime_group/$mime_subgroup";
  3023. } else {
  3024. $mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type );
  3025. if ( false === strpos( $mime_pattern, '*' ) ) {
  3026. $mime_pattern .= '/*';
  3027. }
  3028. }
  3029. $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern );
  3030. if ( in_array( $mime_type, $wildcards, true ) ) {
  3031. return '';
  3032. }
  3033. if ( false !== strpos( $mime_pattern, '%' ) ) {
  3034. $wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'";
  3035. } else {
  3036. $wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'";
  3037. }
  3038. }
  3039. if ( ! empty( $wheres ) ) {
  3040. $where = ' AND (' . implode( ' OR ', $wheres ) . ') ';
  3041. }
  3042. return $where;
  3043. }
  3044. /**
  3045. * Trashes or deletes a post or page.
  3046. *
  3047. * When the post and page is permanently deleted, everything that is tied to
  3048. * it is deleted also. This includes comments, post meta fields, and terms
  3049. * associated with the post.
  3050. *
  3051. * The post or page is moved to Trash instead of permanently deleted unless
  3052. * Trash is disabled, item is already in the Trash, or $force_delete is true.
  3053. *
  3054. * @since 1.0.0
  3055. *
  3056. * @global wpdb $wpdb WordPress database abstraction object.
  3057. * @see wp_delete_attachment()
  3058. * @see wp_trash_post()
  3059. *
  3060. * @param int $postid Optional. Post ID. Default 0.
  3061. * @param bool $force_delete Optional. Whether to bypass Trash and force deletion.
  3062. * Default false.
  3063. * @return WP_Post|false|null Post data on success, false or null on failure.
  3064. */
  3065. function wp_delete_post( $postid = 0, $force_delete = false ) {
  3066. global $wpdb;
  3067. $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) );
  3068. if ( ! $post ) {
  3069. return $post;
  3070. }
  3071. $post = get_post( $post );
  3072. if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) {
  3073. return wp_trash_post( $postid );
  3074. }
  3075. if ( 'attachment' === $post->post_type ) {
  3076. return wp_delete_attachment( $postid, $force_delete );
  3077. }
  3078. /**
  3079. * Filters whether a post deletion should take place.
  3080. *
  3081. * @since 4.4.0
  3082. *
  3083. * @param WP_Post|false|null $delete Whether to go forward with deletion.
  3084. * @param WP_Post $post Post object.
  3085. * @param bool $force_delete Whether to bypass the Trash.
  3086. */
  3087. $check = apply_filters( 'pre_delete_post', null, $post, $force_delete );
  3088. if ( null !== $check ) {
  3089. return $check;
  3090. }
  3091. /**
  3092. * Fires before a post is deleted, at the start of wp_delete_post().
  3093. *
  3094. * @since 3.2.0
  3095. * @since 5.5.0 Added the `$post` parameter.
  3096. *
  3097. * @see wp_delete_post()
  3098. *
  3099. * @param int $postid Post ID.
  3100. * @param WP_Post $post Post object.
  3101. */
  3102. do_action( 'before_delete_post', $postid, $post );
  3103. delete_post_meta( $postid, '_wp_trash_meta_status' );
  3104. delete_post_meta( $postid, '_wp_trash_meta_time' );
  3105. wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) );
  3106. $parent_data = array( 'post_parent' => $post->post_parent );
  3107. $parent_where = array( 'post_parent' => $postid );
  3108. if ( is_post_type_hierarchical( $post->post_type ) ) {
  3109. // Point children of this page to its parent, also clean the cache of affected children.
  3110. $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type );
  3111. $children = $wpdb->get_results( $children_query );
  3112. if ( $children ) {
  3113. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) );
  3114. }
  3115. }
  3116. // Do raw query. wp_get_post_revisions() is filtered.
  3117. $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) );
  3118. // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
  3119. foreach ( $revision_ids as $revision_id ) {
  3120. wp_delete_post_revision( $revision_id );
  3121. }
  3122. // Point all attachments to this post up one level.
  3123. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) );
  3124. wp_defer_comment_counting( true );
  3125. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $postid ) );
  3126. foreach ( $comment_ids as $comment_id ) {
  3127. wp_delete_comment( $comment_id, true );
  3128. }
  3129. wp_defer_comment_counting( false );
  3130. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) );
  3131. foreach ( $post_meta_ids as $mid ) {
  3132. delete_metadata_by_mid( 'post', $mid );
  3133. }
  3134. /**
  3135. * Fires immediately before a post is deleted from the database.
  3136. *
  3137. * @since 1.2.0
  3138. * @since 5.5.0 Added the `$post` parameter.
  3139. *
  3140. * @param int $postid Post ID.
  3141. * @param WP_Post $post Post object.
  3142. */
  3143. do_action( 'delete_post', $postid, $post );
  3144. $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
  3145. if ( ! $result ) {
  3146. return false;
  3147. }
  3148. /**
  3149. * Fires immediately after a post is deleted from the database.
  3150. *
  3151. * @since 2.2.0
  3152. * @since 5.5.0 Added the `$post` parameter.
  3153. *
  3154. * @param int $postid Post ID.
  3155. * @param WP_Post $post Post object.
  3156. */
  3157. do_action( 'deleted_post', $postid, $post );
  3158. clean_post_cache( $post );
  3159. if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
  3160. foreach ( $children as $child ) {
  3161. clean_post_cache( $child );
  3162. }
  3163. }
  3164. wp_clear_scheduled_hook( 'publish_future_post', array( $postid ) );
  3165. /**
  3166. * Fires after a post is deleted, at the conclusion of wp_delete_post().
  3167. *
  3168. * @since 3.2.0
  3169. * @since 5.5.0 Added the `$post` parameter.
  3170. *
  3171. * @see wp_delete_post()
  3172. *
  3173. * @param int $postid Post ID.
  3174. * @param WP_Post $post Post object.
  3175. */
  3176. do_action( 'after_delete_post', $postid, $post );
  3177. return $post;
  3178. }
  3179. /**
  3180. * Resets the page_on_front, show_on_front, and page_for_post settings when
  3181. * a linked page is deleted or trashed.
  3182. *
  3183. * Also ensures the post is no longer sticky.
  3184. *
  3185. * @since 3.7.0
  3186. * @access private
  3187. *
  3188. * @param int $post_id Post ID.
  3189. */
  3190. function _reset_front_page_settings_for_post( $post_id ) {
  3191. $post = get_post( $post_id );
  3192. if ( 'page' === $post->post_type ) {
  3193. /*
  3194. * If the page is defined in option page_on_front or post_for_posts,
  3195. * adjust the corresponding options.
  3196. */
  3197. if ( get_option( 'page_on_front' ) == $post->ID ) {
  3198. update_option( 'show_on_front', 'posts' );
  3199. update_option( 'page_on_front', 0 );
  3200. }
  3201. if ( get_option( 'page_for_posts' ) == $post->ID ) {
  3202. update_option( 'page_for_posts', 0 );
  3203. }
  3204. }
  3205. unstick_post( $post->ID );
  3206. }
  3207. /**
  3208. * Moves a post or page to the Trash
  3209. *
  3210. * If Trash is disabled, the post or page is permanently deleted.
  3211. *
  3212. * @since 2.9.0
  3213. *
  3214. * @see wp_delete_post()
  3215. *
  3216. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`
  3217. * if `EMPTY_TRASH_DAYS` equals true.
  3218. * @return WP_Post|false|null Post data on success, false or null on failure.
  3219. */
  3220. function wp_trash_post( $post_id = 0 ) {
  3221. if ( ! EMPTY_TRASH_DAYS ) {
  3222. return wp_delete_post( $post_id, true );
  3223. }
  3224. $post = get_post( $post_id );
  3225. if ( ! $post ) {
  3226. return $post;
  3227. }
  3228. if ( 'trash' === $post->post_status ) {
  3229. return false;
  3230. }
  3231. /**
  3232. * Filters whether a post trashing should take place.
  3233. *
  3234. * @since 4.9.0
  3235. *
  3236. * @param bool|null $trash Whether to go forward with trashing.
  3237. * @param WP_Post $post Post object.
  3238. */
  3239. $check = apply_filters( 'pre_trash_post', null, $post );
  3240. if ( null !== $check ) {
  3241. return $check;
  3242. }
  3243. /**
  3244. * Fires before a post is sent to the Trash.
  3245. *
  3246. * @since 3.3.0
  3247. *
  3248. * @param int $post_id Post ID.
  3249. */
  3250. do_action( 'wp_trash_post', $post_id );
  3251. add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status );
  3252. add_post_meta( $post_id, '_wp_trash_meta_time', time() );
  3253. $post_updated = wp_update_post(
  3254. array(
  3255. 'ID' => $post_id,
  3256. 'post_status' => 'trash',
  3257. )
  3258. );
  3259. if ( ! $post_updated ) {
  3260. return false;
  3261. }
  3262. wp_trash_post_comments( $post_id );
  3263. /**
  3264. * Fires after a post is sent to the Trash.
  3265. *
  3266. * @since 2.9.0
  3267. *
  3268. * @param int $post_id Post ID.
  3269. */
  3270. do_action( 'trashed_post', $post_id );
  3271. return $post;
  3272. }
  3273. /**
  3274. * Restores a post from the Trash.
  3275. *
  3276. * @since 2.9.0
  3277. * @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for
  3278. * attachments which are returned to their original 'inherit' status.
  3279. *
  3280. * @param int $post_id Optional. Post ID. Default is the ID of the global `$post`.
  3281. * @return WP_Post|false|null Post data on success, false or null on failure.
  3282. */
  3283. function wp_untrash_post( $post_id = 0 ) {
  3284. $post = get_post( $post_id );
  3285. if ( ! $post ) {
  3286. return $post;
  3287. }
  3288. $post_id = $post->ID;
  3289. if ( 'trash' !== $post->post_status ) {
  3290. return false;
  3291. }
  3292. $previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true );
  3293. /**
  3294. * Filters whether a post untrashing should take place.
  3295. *
  3296. * @since 4.9.0
  3297. * @since 5.6.0 The `$previous_status` parameter was added.
  3298. *
  3299. * @param bool|null $untrash Whether to go forward with untrashing.
  3300. * @param WP_Post $post Post object.
  3301. * @param string $previous_status The status of the post at the point where it was trashed.
  3302. */
  3303. $check = apply_filters( 'pre_untrash_post', null, $post, $previous_status );
  3304. if ( null !== $check ) {
  3305. return $check;
  3306. }
  3307. /**
  3308. * Fires before a post is restored from the Trash.
  3309. *
  3310. * @since 2.9.0
  3311. * @since 5.6.0 The `$previous_status` parameter was added.
  3312. *
  3313. * @param int $post_id Post ID.
  3314. * @param string $previous_status The status of the post at the point where it was trashed.
  3315. */
  3316. do_action( 'untrash_post', $post_id, $previous_status );
  3317. $new_status = ( 'attachment' === $post->post_type ) ? 'inherit' : 'draft';
  3318. /**
  3319. * Filters the status that a post gets assigned when it is restored from the trash (untrashed).
  3320. *
  3321. * By default posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status`
  3322. * in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()`
  3323. * function is available for this.
  3324. *
  3325. * Prior to WordPress 5.6.0, restored posts were always assigned their original status.
  3326. *
  3327. * @since 5.6.0
  3328. *
  3329. * @param string $new_status The new status of the post being restored.
  3330. * @param int $post_id The ID of the post being restored.
  3331. * @param string $previous_status The status of the post at the point where it was trashed.
  3332. */
  3333. $post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status );
  3334. delete_post_meta( $post_id, '_wp_trash_meta_status' );
  3335. delete_post_meta( $post_id, '_wp_trash_meta_time' );
  3336. $post_updated = wp_update_post(
  3337. array(
  3338. 'ID' => $post_id,
  3339. 'post_status' => $post_status,
  3340. )
  3341. );
  3342. if ( ! $post_updated ) {
  3343. return false;
  3344. }
  3345. wp_untrash_post_comments( $post_id );
  3346. /**
  3347. * Fires after a post is restored from the Trash.
  3348. *
  3349. * @since 2.9.0
  3350. * @since 5.6.0 The `$previous_status` parameter was added.
  3351. *
  3352. * @param int $post_id Post ID.
  3353. * @param string $previous_status The status of the post at the point where it was trashed.
  3354. */
  3355. do_action( 'untrashed_post', $post_id, $previous_status );
  3356. return $post;
  3357. }
  3358. /**
  3359. * Moves comments for a post to the Trash.
  3360. *
  3361. * @since 2.9.0
  3362. *
  3363. * @global wpdb $wpdb WordPress database abstraction object.
  3364. *
  3365. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
  3366. * @return mixed|void False on failure.
  3367. */
  3368. function wp_trash_post_comments( $post = null ) {
  3369. global $wpdb;
  3370. $post = get_post( $post );
  3371. if ( ! $post ) {
  3372. return;
  3373. }
  3374. $post_id = $post->ID;
  3375. /**
  3376. * Fires before comments are sent to the Trash.
  3377. *
  3378. * @since 2.9.0
  3379. *
  3380. * @param int $post_id Post ID.
  3381. */
  3382. do_action( 'trash_post_comments', $post_id );
  3383. $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) );
  3384. if ( ! $comments ) {
  3385. return;
  3386. }
  3387. // Cache current status for each comment.
  3388. $statuses = array();
  3389. foreach ( $comments as $comment ) {
  3390. $statuses[ $comment->comment_ID ] = $comment->comment_approved;
  3391. }
  3392. add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses );
  3393. // Set status for all comments to post-trashed.
  3394. $result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) );
  3395. clean_comment_cache( array_keys( $statuses ) );
  3396. /**
  3397. * Fires after comments are sent to the Trash.
  3398. *
  3399. * @since 2.9.0
  3400. *
  3401. * @param int $post_id Post ID.
  3402. * @param array $statuses Array of comment statuses.
  3403. */
  3404. do_action( 'trashed_post_comments', $post_id, $statuses );
  3405. return $result;
  3406. }
  3407. /**
  3408. * Restores comments for a post from the Trash.
  3409. *
  3410. * @since 2.9.0
  3411. *
  3412. * @global wpdb $wpdb WordPress database abstraction object.
  3413. *
  3414. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
  3415. * @return true|void
  3416. */
  3417. function wp_untrash_post_comments( $post = null ) {
  3418. global $wpdb;
  3419. $post = get_post( $post );
  3420. if ( ! $post ) {
  3421. return;
  3422. }
  3423. $post_id = $post->ID;
  3424. $statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true );
  3425. if ( ! $statuses ) {
  3426. return true;
  3427. }
  3428. /**
  3429. * Fires before comments are restored for a post from the Trash.
  3430. *
  3431. * @since 2.9.0
  3432. *
  3433. * @param int $post_id Post ID.
  3434. */
  3435. do_action( 'untrash_post_comments', $post_id );
  3436. // Restore each comment to its original status.
  3437. $group_by_status = array();
  3438. foreach ( $statuses as $comment_id => $comment_status ) {
  3439. $group_by_status[ $comment_status ][] = $comment_id;
  3440. }
  3441. foreach ( $group_by_status as $status => $comments ) {
  3442. // Sanity check. This shouldn't happen.
  3443. if ( 'post-trashed' === $status ) {
  3444. $status = '0';
  3445. }
  3446. $comments_in = implode( ', ', array_map( 'intval', $comments ) );
  3447. $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) );
  3448. }
  3449. clean_comment_cache( array_keys( $statuses ) );
  3450. delete_post_meta( $post_id, '_wp_trash_meta_comments_status' );
  3451. /**
  3452. * Fires after comments are restored for a post from the Trash.
  3453. *
  3454. * @since 2.9.0
  3455. *
  3456. * @param int $post_id Post ID.
  3457. */
  3458. do_action( 'untrashed_post_comments', $post_id );
  3459. }
  3460. /**
  3461. * Retrieves the list of categories for a post.
  3462. *
  3463. * Compatibility layer for themes and plugins. Also an easy layer of abstraction
  3464. * away from the complexity of the taxonomy layer.
  3465. *
  3466. * @since 2.1.0
  3467. *
  3468. * @see wp_get_object_terms()
  3469. *
  3470. * @param int $post_id Optional. The Post ID. Does not default to the ID of the
  3471. * global $post. Default 0.
  3472. * @param array $args Optional. Category query parameters. Default empty array.
  3473. * See WP_Term_Query::__construct() for supported arguments.
  3474. * @return array|WP_Error List of categories. If the `$fields` argument passed via `$args` is 'all' or
  3475. * 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields`
  3476. * is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names.
  3477. * WP_Error object if 'category' taxonomy doesn't exist.
  3478. */
  3479. function wp_get_post_categories( $post_id = 0, $args = array() ) {
  3480. $post_id = (int) $post_id;
  3481. $defaults = array( 'fields' => 'ids' );
  3482. $args = wp_parse_args( $args, $defaults );
  3483. $cats = wp_get_object_terms( $post_id, 'category', $args );
  3484. return $cats;
  3485. }
  3486. /**
  3487. * Retrieves the tags for a post.
  3488. *
  3489. * There is only one default for this function, called 'fields' and by default
  3490. * is set to 'all'. There are other defaults that can be overridden in
  3491. * wp_get_object_terms().
  3492. *
  3493. * @since 2.3.0
  3494. *
  3495. * @param int $post_id Optional. The Post ID. Does not default to the ID of the
  3496. * global $post. Default 0.
  3497. * @param array $args Optional. Tag query parameters. Default empty array.
  3498. * See WP_Term_Query::__construct() for supported arguments.
  3499. * @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found.
  3500. * WP_Error object if 'post_tag' taxonomy doesn't exist.
  3501. */
  3502. function wp_get_post_tags( $post_id = 0, $args = array() ) {
  3503. return wp_get_post_terms( $post_id, 'post_tag', $args );
  3504. }
  3505. /**
  3506. * Retrieves the terms for a post.
  3507. *
  3508. * @since 2.8.0
  3509. *
  3510. * @param int $post_id Optional. The Post ID. Does not default to the ID of the
  3511. * global $post. Default 0.
  3512. * @param string|string[] $taxonomy Optional. The taxonomy slug or array of slugs for which
  3513. * to retrieve terms. Default 'post_tag'.
  3514. * @param array $args {
  3515. * Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments.
  3516. *
  3517. * @type string $fields Term fields to retrieve. Default 'all'.
  3518. * }
  3519. * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found.
  3520. * WP_Error object if `$taxonomy` doesn't exist.
  3521. */
  3522. function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
  3523. $post_id = (int) $post_id;
  3524. $defaults = array( 'fields' => 'all' );
  3525. $args = wp_parse_args( $args, $defaults );
  3526. $tags = wp_get_object_terms( $post_id, $taxonomy, $args );
  3527. return $tags;
  3528. }
  3529. /**
  3530. * Retrieves a number of recent posts.
  3531. *
  3532. * @since 1.0.0
  3533. *
  3534. * @see get_posts()
  3535. *
  3536. * @param array $args Optional. Arguments to retrieve posts. Default empty array.
  3537. * @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which
  3538. * correspond to a WP_Post object or an associative array, respectively.
  3539. * Default ARRAY_A.
  3540. * @return array|false Array of recent posts, where the type of each element is determined
  3541. * by the `$output` parameter. Empty array on failure.
  3542. */
  3543. function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) {
  3544. if ( is_numeric( $args ) ) {
  3545. _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) );
  3546. $args = array( 'numberposts' => absint( $args ) );
  3547. }
  3548. // Set default arguments.
  3549. $defaults = array(
  3550. 'numberposts' => 10,
  3551. 'offset' => 0,
  3552. 'category' => 0,
  3553. 'orderby' => 'post_date',
  3554. 'order' => 'DESC',
  3555. 'include' => '',
  3556. 'exclude' => '',
  3557. 'meta_key' => '',
  3558. 'meta_value' => '',
  3559. 'post_type' => 'post',
  3560. 'post_status' => 'draft, publish, future, pending, private',
  3561. 'suppress_filters' => true,
  3562. );
  3563. $parsed_args = wp_parse_args( $args, $defaults );
  3564. $results = get_posts( $parsed_args );
  3565. // Backward compatibility. Prior to 3.1 expected posts to be returned in array.
  3566. if ( ARRAY_A === $output ) {
  3567. foreach ( $results as $key => $result ) {
  3568. $results[ $key ] = get_object_vars( $result );
  3569. }
  3570. return $results ? $results : array();
  3571. }
  3572. return $results ? $results : false;
  3573. }
  3574. /**
  3575. * Inserts or update a post.
  3576. *
  3577. * If the $postarr parameter has 'ID' set to a value, then post will be updated.
  3578. *
  3579. * You can set the post date manually, by setting the values for 'post_date'
  3580. * and 'post_date_gmt' keys. You can close the comments or open the comments by
  3581. * setting the value for 'comment_status' key.
  3582. *
  3583. * @since 1.0.0
  3584. * @since 2.6.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
  3585. * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
  3586. * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.
  3587. * @since 5.6.0 Added the `$fire_after_hooks` parameter.
  3588. *
  3589. * @see sanitize_post()
  3590. * @global wpdb $wpdb WordPress database abstraction object.
  3591. *
  3592. * @param array $postarr {
  3593. * An array of elements that make up a post to update or insert.
  3594. *
  3595. * @type int $ID The post ID. If equal to something other than 0,
  3596. * the post with that ID will be updated. Default 0.
  3597. * @type int $post_author The ID of the user who added the post. Default is
  3598. * the current user ID.
  3599. * @type string $post_date The date of the post. Default is the current time.
  3600. * @type string $post_date_gmt The date of the post in the GMT timezone. Default is
  3601. * the value of `$post_date`.
  3602. * @type string $post_content The post content. Default empty.
  3603. * @type string $post_content_filtered The filtered post content. Default empty.
  3604. * @type string $post_title The post title. Default empty.
  3605. * @type string $post_excerpt The post excerpt. Default empty.
  3606. * @type string $post_status The post status. Default 'draft'.
  3607. * @type string $post_type The post type. Default 'post'.
  3608. * @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'.
  3609. * Default is the value of 'default_comment_status' option.
  3610. * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'.
  3611. * Default is the value of 'default_ping_status' option.
  3612. * @type string $post_password The password to access the post. Default empty.
  3613. * @type string $post_name The post name. Default is the sanitized post title
  3614. * when creating a new post.
  3615. * @type string $to_ping Space or carriage return-separated list of URLs to ping.
  3616. * Default empty.
  3617. * @type string $pinged Space or carriage return-separated list of URLs that have
  3618. * been pinged. Default empty.
  3619. * @type string $post_modified The date when the post was last modified. Default is
  3620. * the current time.
  3621. * @type string $post_modified_gmt The date when the post was last modified in the GMT
  3622. * timezone. Default is the current time.
  3623. * @type int $post_parent Set this for the post it belongs to, if any. Default 0.
  3624. * @type int $menu_order The order the post should be displayed in. Default 0.
  3625. * @type string $post_mime_type The mime type of the post. Default empty.
  3626. * @type string $guid Global Unique ID for referencing the post. Default empty.
  3627. * @type int $import_id The post ID to be used when inserting a new post.
  3628. * If specified, must not match any existing post ID. Default 0.
  3629. * @type int[] $post_category Array of category IDs.
  3630. * Defaults to value of the 'default_category' option.
  3631. * @type array $tags_input Array of tag names, slugs, or IDs. Default empty.
  3632. * @type array $tax_input An array of taxonomy terms keyed by their taxonomy name.
  3633. * If the taxonomy is hierarchical, the term list needs to be
  3634. * either an array of term IDs or a comma-separated string of IDs.
  3635. * If the taxonomy is non-hierarchical, the term list can be an array
  3636. * that contains term names or slugs, or a comma-separated string
  3637. * of names or slugs. This is because, in hierarchical taxonomy,
  3638. * child terms can have the same names with different parent terms,
  3639. * so the only way to connect them is using ID. Default empty.
  3640. * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty.
  3641. * @type string $page_template Page template to use.
  3642. * }
  3643. * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  3644. * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
  3645. * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
  3646. */
  3647. function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) {
  3648. global $wpdb;
  3649. // Capture original pre-sanitized array for passing into filters.
  3650. $unsanitized_postarr = $postarr;
  3651. $user_id = get_current_user_id();
  3652. $defaults = array(
  3653. 'post_author' => $user_id,
  3654. 'post_content' => '',
  3655. 'post_content_filtered' => '',
  3656. 'post_title' => '',
  3657. 'post_excerpt' => '',
  3658. 'post_status' => 'draft',
  3659. 'post_type' => 'post',
  3660. 'comment_status' => '',
  3661. 'ping_status' => '',
  3662. 'post_password' => '',
  3663. 'to_ping' => '',
  3664. 'pinged' => '',
  3665. 'post_parent' => 0,
  3666. 'menu_order' => 0,
  3667. 'guid' => '',
  3668. 'import_id' => 0,
  3669. 'context' => '',
  3670. 'post_date' => '',
  3671. 'post_date_gmt' => '',
  3672. );
  3673. $postarr = wp_parse_args( $postarr, $defaults );
  3674. unset( $postarr['filter'] );
  3675. $postarr = sanitize_post( $postarr, 'db' );
  3676. // Are we updating or creating?
  3677. $post_ID = 0;
  3678. $update = false;
  3679. $guid = $postarr['guid'];
  3680. if ( ! empty( $postarr['ID'] ) ) {
  3681. $update = true;
  3682. // Get the post ID and GUID.
  3683. $post_ID = $postarr['ID'];
  3684. $post_before = get_post( $post_ID );
  3685. if ( is_null( $post_before ) ) {
  3686. if ( $wp_error ) {
  3687. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
  3688. }
  3689. return 0;
  3690. }
  3691. $guid = get_post_field( 'guid', $post_ID );
  3692. $previous_status = get_post_field( 'post_status', $post_ID );
  3693. } else {
  3694. $previous_status = 'new';
  3695. $post_before = null;
  3696. }
  3697. $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
  3698. $post_title = $postarr['post_title'];
  3699. $post_content = $postarr['post_content'];
  3700. $post_excerpt = $postarr['post_excerpt'];
  3701. if ( isset( $postarr['post_name'] ) ) {
  3702. $post_name = $postarr['post_name'];
  3703. } elseif ( $update ) {
  3704. // For an update, don't modify the post_name if it wasn't supplied as an argument.
  3705. $post_name = $post_before->post_name;
  3706. }
  3707. $maybe_empty = 'attachment' !== $post_type
  3708. && ! $post_content && ! $post_title && ! $post_excerpt
  3709. && post_type_supports( $post_type, 'editor' )
  3710. && post_type_supports( $post_type, 'title' )
  3711. && post_type_supports( $post_type, 'excerpt' );
  3712. /**
  3713. * Filters whether the post should be considered "empty".
  3714. *
  3715. * The post is considered "empty" if both:
  3716. * 1. The post type supports the title, editor, and excerpt fields
  3717. * 2. The title, editor, and excerpt fields are all empty
  3718. *
  3719. * Returning a truthy value from the filter will effectively short-circuit
  3720. * the new post being inserted and return 0. If $wp_error is true, a WP_Error
  3721. * will be returned instead.
  3722. *
  3723. * @since 3.3.0
  3724. *
  3725. * @param bool $maybe_empty Whether the post should be considered "empty".
  3726. * @param array $postarr Array of post data.
  3727. */
  3728. if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) {
  3729. if ( $wp_error ) {
  3730. return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) );
  3731. } else {
  3732. return 0;
  3733. }
  3734. }
  3735. $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
  3736. if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
  3737. $post_status = 'inherit';
  3738. }
  3739. if ( ! empty( $postarr['post_category'] ) ) {
  3740. // Filter out empty terms.
  3741. $post_category = array_filter( $postarr['post_category'] );
  3742. } elseif ( $update && ! isset( $postarr['post_category'] ) ) {
  3743. $post_category = $post_before->post_category;
  3744. }
  3745. // Make sure we set a valid category.
  3746. if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) {
  3747. // 'post' requires at least one category.
  3748. if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
  3749. $post_category = array( get_option( 'default_category' ) );
  3750. } else {
  3751. $post_category = array();
  3752. }
  3753. }
  3754. /*
  3755. * Don't allow contributors to set the post slug for pending review posts.
  3756. *
  3757. * For new posts check the primitive capability, for updates check the meta capability.
  3758. */
  3759. if ( 'pending' === $post_status ) {
  3760. $post_type_object = get_post_type_object( $post_type );
  3761. if ( ! $update && $post_type_object && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
  3762. $post_name = '';
  3763. } elseif ( $update && ! current_user_can( 'publish_post', $post_ID ) ) {
  3764. $post_name = '';
  3765. }
  3766. }
  3767. /*
  3768. * Create a valid post name. Drafts and pending posts are allowed to have
  3769. * an empty post name.
  3770. */
  3771. if ( empty( $post_name ) ) {
  3772. if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) {
  3773. $post_name = sanitize_title( $post_title );
  3774. } else {
  3775. $post_name = '';
  3776. }
  3777. } else {
  3778. // On updates, we need to check to see if it's using the old, fixed sanitization context.
  3779. $check_name = sanitize_title( $post_name, '', 'old-save' );
  3780. if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
  3781. $post_name = $check_name;
  3782. } else { // new post, or slug has changed.
  3783. $post_name = sanitize_title( $post_name );
  3784. }
  3785. }
  3786. /*
  3787. * Resolve the post date from any provided post date or post date GMT strings;
  3788. * if none are provided, the date will be set to now.
  3789. */
  3790. $post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] );
  3791. if ( ! $post_date ) {
  3792. if ( $wp_error ) {
  3793. return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
  3794. } else {
  3795. return 0;
  3796. }
  3797. }
  3798. if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) {
  3799. if ( ! in_array( $post_status, get_post_stati( array( 'date_floating' => true ) ), true ) ) {
  3800. $post_date_gmt = get_gmt_from_date( $post_date );
  3801. } else {
  3802. $post_date_gmt = '0000-00-00 00:00:00';
  3803. }
  3804. } else {
  3805. $post_date_gmt = $postarr['post_date_gmt'];
  3806. }
  3807. if ( $update || '0000-00-00 00:00:00' === $post_date ) {
  3808. $post_modified = current_time( 'mysql' );
  3809. $post_modified_gmt = current_time( 'mysql', 1 );
  3810. } else {
  3811. $post_modified = $post_date;
  3812. $post_modified_gmt = $post_date_gmt;
  3813. }
  3814. if ( 'attachment' !== $post_type ) {
  3815. $now = gmdate( 'Y-m-d H:i:s' );
  3816. if ( 'publish' === $post_status ) {
  3817. if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) {
  3818. $post_status = 'future';
  3819. }
  3820. } elseif ( 'future' === $post_status ) {
  3821. if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) {
  3822. $post_status = 'publish';
  3823. }
  3824. }
  3825. }
  3826. // Comment status.
  3827. if ( empty( $postarr['comment_status'] ) ) {
  3828. if ( $update ) {
  3829. $comment_status = 'closed';
  3830. } else {
  3831. $comment_status = get_default_comment_status( $post_type );
  3832. }
  3833. } else {
  3834. $comment_status = $postarr['comment_status'];
  3835. }
  3836. // These variables are needed by compact() later.
  3837. $post_content_filtered = $postarr['post_content_filtered'];
  3838. $post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
  3839. $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
  3840. $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
  3841. $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
  3842. $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
  3843. /*
  3844. * The 'wp_insert_post_parent' filter expects all variables to be present.
  3845. * Previously, these variables would have already been extracted
  3846. */
  3847. if ( isset( $postarr['menu_order'] ) ) {
  3848. $menu_order = (int) $postarr['menu_order'];
  3849. } else {
  3850. $menu_order = 0;
  3851. }
  3852. $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
  3853. if ( 'private' === $post_status ) {
  3854. $post_password = '';
  3855. }
  3856. if ( isset( $postarr['post_parent'] ) ) {
  3857. $post_parent = (int) $postarr['post_parent'];
  3858. } else {
  3859. $post_parent = 0;
  3860. }
  3861. $new_postarr = array_merge(
  3862. array(
  3863. 'ID' => $post_ID,
  3864. ),
  3865. compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) )
  3866. );
  3867. /**
  3868. * Filters the post parent -- used to check for and prevent hierarchy loops.
  3869. *
  3870. * @since 3.1.0
  3871. *
  3872. * @param int $post_parent Post parent ID.
  3873. * @param int $post_ID Post ID.
  3874. * @param array $new_postarr Array of parsed post data.
  3875. * @param array $postarr Array of sanitized, but otherwise unmodified post data.
  3876. */
  3877. $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr );
  3878. /*
  3879. * If the post is being untrashed and it has a desired slug stored in post meta,
  3880. * reassign it.
  3881. */
  3882. if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
  3883. $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
  3884. if ( $desired_post_slug ) {
  3885. delete_post_meta( $post_ID, '_wp_desired_post_slug' );
  3886. $post_name = $desired_post_slug;
  3887. }
  3888. }
  3889. // If a trashed post has the desired slug, change it and let this post have it.
  3890. if ( 'trash' !== $post_status && $post_name ) {
  3891. /**
  3892. * Filters whether or not to add a `__trashed` suffix to trashed posts that match the name of the updated post.
  3893. *
  3894. * @since 5.4.0
  3895. *
  3896. * @param bool $add_trashed_suffix Whether to attempt to add the suffix.
  3897. * @param string $post_name The name of the post being updated.
  3898. * @param int $post_ID Post ID.
  3899. */
  3900. $add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_ID );
  3901. if ( $add_trashed_suffix ) {
  3902. wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
  3903. }
  3904. }
  3905. // When trashing an existing post, change its slug to allow non-trashed posts to use it.
  3906. if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {
  3907. $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
  3908. }
  3909. $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
  3910. // Don't unslash.
  3911. $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';
  3912. // Expected_slashed (everything!).
  3913. $data = compact(
  3914. 'post_author',
  3915. 'post_date',
  3916. 'post_date_gmt',
  3917. 'post_content',
  3918. 'post_content_filtered',
  3919. 'post_title',
  3920. 'post_excerpt',
  3921. 'post_status',
  3922. 'post_type',
  3923. 'comment_status',
  3924. 'ping_status',
  3925. 'post_password',
  3926. 'post_name',
  3927. 'to_ping',
  3928. 'pinged',
  3929. 'post_modified',
  3930. 'post_modified_gmt',
  3931. 'post_parent',
  3932. 'menu_order',
  3933. 'post_mime_type',
  3934. 'guid'
  3935. );
  3936. $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' );
  3937. foreach ( $emoji_fields as $emoji_field ) {
  3938. if ( isset( $data[ $emoji_field ] ) ) {
  3939. $charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field );
  3940. if ( 'utf8' === $charset ) {
  3941. $data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] );
  3942. }
  3943. }
  3944. }
  3945. if ( 'attachment' === $post_type ) {
  3946. /**
  3947. * Filters attachment post data before it is updated in or added to the database.
  3948. *
  3949. * @since 3.9.0
  3950. * @since 5.4.1 The `$unsanitized_postarr` parameter was added.
  3951. * @since 6.0.0 The `$update` parameter was added.
  3952. *
  3953. * @param array $data An array of slashed, sanitized, and processed attachment post data.
  3954. * @param array $postarr An array of slashed and sanitized attachment post data, but not processed.
  3955. * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data
  3956. * as originally passed to wp_insert_post().
  3957. * @param bool $update Whether this is an existing attachment post being updated.
  3958. */
  3959. $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update );
  3960. } else {
  3961. /**
  3962. * Filters slashed post data just before it is inserted into the database.
  3963. *
  3964. * @since 2.7.0
  3965. * @since 5.4.1 The `$unsanitized_postarr` parameter was added.
  3966. * @since 6.0.0 The `$update` parameter was added.
  3967. *
  3968. * @param array $data An array of slashed, sanitized, and processed post data.
  3969. * @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
  3970. * @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as
  3971. * originally passed to wp_insert_post().
  3972. * @param bool $update Whether this is an existing post being updated.
  3973. */
  3974. $data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update );
  3975. }
  3976. $data = wp_unslash( $data );
  3977. $where = array( 'ID' => $post_ID );
  3978. if ( $update ) {
  3979. /**
  3980. * Fires immediately before an existing post is updated in the database.
  3981. *
  3982. * @since 2.5.0
  3983. *
  3984. * @param int $post_ID Post ID.
  3985. * @param array $data Array of unslashed post data.
  3986. */
  3987. do_action( 'pre_post_update', $post_ID, $data );
  3988. if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) {
  3989. if ( $wp_error ) {
  3990. if ( 'attachment' === $post_type ) {
  3991. $message = __( 'Could not update attachment in the database.' );
  3992. } else {
  3993. $message = __( 'Could not update post in the database.' );
  3994. }
  3995. return new WP_Error( 'db_update_error', $message, $wpdb->last_error );
  3996. } else {
  3997. return 0;
  3998. }
  3999. }
  4000. } else {
  4001. // If there is a suggested ID, use it if not already present.
  4002. if ( ! empty( $import_id ) ) {
  4003. $import_id = (int) $import_id;
  4004. if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) {
  4005. $data['ID'] = $import_id;
  4006. }
  4007. }
  4008. if ( false === $wpdb->insert( $wpdb->posts, $data ) ) {
  4009. if ( $wp_error ) {
  4010. if ( 'attachment' === $post_type ) {
  4011. $message = __( 'Could not insert attachment into the database.' );
  4012. } else {
  4013. $message = __( 'Could not insert post into the database.' );
  4014. }
  4015. return new WP_Error( 'db_insert_error', $message, $wpdb->last_error );
  4016. } else {
  4017. return 0;
  4018. }
  4019. }
  4020. $post_ID = (int) $wpdb->insert_id;
  4021. // Use the newly generated $post_ID.
  4022. $where = array( 'ID' => $post_ID );
  4023. }
  4024. if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) {
  4025. $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent );
  4026. $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
  4027. clean_post_cache( $post_ID );
  4028. }
  4029. if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
  4030. wp_set_post_categories( $post_ID, $post_category );
  4031. }
  4032. if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
  4033. wp_set_post_tags( $post_ID, $postarr['tags_input'] );
  4034. }
  4035. // Add default term for all associated custom taxonomies.
  4036. if ( 'auto-draft' !== $post_status ) {
  4037. foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
  4038. if ( ! empty( $tax_object->default_term ) ) {
  4039. // Filter out empty terms.
  4040. if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
  4041. $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
  4042. }
  4043. // Passed custom taxonomy list overwrites the existing list if not empty.
  4044. $terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) );
  4045. if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) {
  4046. $postarr['tax_input'][ $taxonomy ] = $terms;
  4047. }
  4048. if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) {
  4049. $default_term_id = get_option( 'default_term_' . $taxonomy );
  4050. if ( ! empty( $default_term_id ) ) {
  4051. $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id );
  4052. }
  4053. }
  4054. }
  4055. }
  4056. }
  4057. // New-style support for all custom taxonomies.
  4058. if ( ! empty( $postarr['tax_input'] ) ) {
  4059. foreach ( $postarr['tax_input'] as $taxonomy => $tags ) {
  4060. $taxonomy_obj = get_taxonomy( $taxonomy );
  4061. if ( ! $taxonomy_obj ) {
  4062. /* translators: %s: Taxonomy name. */
  4063. _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' );
  4064. continue;
  4065. }
  4066. // array = hierarchical, string = non-hierarchical.
  4067. if ( is_array( $tags ) ) {
  4068. $tags = array_filter( $tags );
  4069. }
  4070. if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
  4071. wp_set_post_terms( $post_ID, $tags, $taxonomy );
  4072. }
  4073. }
  4074. }
  4075. if ( ! empty( $postarr['meta_input'] ) ) {
  4076. foreach ( $postarr['meta_input'] as $field => $value ) {
  4077. update_post_meta( $post_ID, $field, $value );
  4078. }
  4079. }
  4080. $current_guid = get_post_field( 'guid', $post_ID );
  4081. // Set GUID.
  4082. if ( ! $update && '' === $current_guid ) {
  4083. $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
  4084. }
  4085. if ( 'attachment' === $postarr['post_type'] ) {
  4086. if ( ! empty( $postarr['file'] ) ) {
  4087. update_attached_file( $post_ID, $postarr['file'] );
  4088. }
  4089. if ( ! empty( $postarr['context'] ) ) {
  4090. add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true );
  4091. }
  4092. }
  4093. // Set or remove featured image.
  4094. if ( isset( $postarr['_thumbnail_id'] ) ) {
  4095. $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type;
  4096. if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
  4097. if ( wp_attachment_is( 'audio', $post_ID ) ) {
  4098. $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
  4099. } elseif ( wp_attachment_is( 'video', $post_ID ) ) {
  4100. $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' );
  4101. }
  4102. }
  4103. if ( $thumbnail_support ) {
  4104. $thumbnail_id = (int) $postarr['_thumbnail_id'];
  4105. if ( -1 === $thumbnail_id ) {
  4106. delete_post_thumbnail( $post_ID );
  4107. } else {
  4108. set_post_thumbnail( $post_ID, $thumbnail_id );
  4109. }
  4110. }
  4111. }
  4112. clean_post_cache( $post_ID );
  4113. $post = get_post( $post_ID );
  4114. if ( ! empty( $postarr['page_template'] ) ) {
  4115. $post->page_template = $postarr['page_template'];
  4116. $page_templates = wp_get_theme()->get_page_templates( $post );
  4117. if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
  4118. if ( $wp_error ) {
  4119. return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) );
  4120. }
  4121. update_post_meta( $post_ID, '_wp_page_template', 'default' );
  4122. } else {
  4123. update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
  4124. }
  4125. }
  4126. if ( 'attachment' !== $postarr['post_type'] ) {
  4127. wp_transition_post_status( $data['post_status'], $previous_status, $post );
  4128. } else {
  4129. if ( $update ) {
  4130. /**
  4131. * Fires once an existing attachment has been updated.
  4132. *
  4133. * @since 2.0.0
  4134. *
  4135. * @param int $post_ID Attachment ID.
  4136. */
  4137. do_action( 'edit_attachment', $post_ID );
  4138. $post_after = get_post( $post_ID );
  4139. /**
  4140. * Fires once an existing attachment has been updated.
  4141. *
  4142. * @since 4.4.0
  4143. *
  4144. * @param int $post_ID Post ID.
  4145. * @param WP_Post $post_after Post object following the update.
  4146. * @param WP_Post $post_before Post object before the update.
  4147. */
  4148. do_action( 'attachment_updated', $post_ID, $post_after, $post_before );
  4149. } else {
  4150. /**
  4151. * Fires once an attachment has been added.
  4152. *
  4153. * @since 2.0.0
  4154. *
  4155. * @param int $post_ID Attachment ID.
  4156. */
  4157. do_action( 'add_attachment', $post_ID );
  4158. }
  4159. return $post_ID;
  4160. }
  4161. if ( $update ) {
  4162. /**
  4163. * Fires once an existing post has been updated.
  4164. *
  4165. * The dynamic portion of the hook name, `$post->post_type`, refers to
  4166. * the post type slug.
  4167. *
  4168. * Possible hook names include:
  4169. *
  4170. * - `edit_post_post`
  4171. * - `edit_post_page`
  4172. *
  4173. * @since 5.1.0
  4174. *
  4175. * @param int $post_ID Post ID.
  4176. * @param WP_Post $post Post object.
  4177. */
  4178. do_action( "edit_post_{$post->post_type}", $post_ID, $post );
  4179. /**
  4180. * Fires once an existing post has been updated.
  4181. *
  4182. * @since 1.2.0
  4183. *
  4184. * @param int $post_ID Post ID.
  4185. * @param WP_Post $post Post object.
  4186. */
  4187. do_action( 'edit_post', $post_ID, $post );
  4188. $post_after = get_post( $post_ID );
  4189. /**
  4190. * Fires once an existing post has been updated.
  4191. *
  4192. * @since 3.0.0
  4193. *
  4194. * @param int $post_ID Post ID.
  4195. * @param WP_Post $post_after Post object following the update.
  4196. * @param WP_Post $post_before Post object before the update.
  4197. */
  4198. do_action( 'post_updated', $post_ID, $post_after, $post_before );
  4199. }
  4200. /**
  4201. * Fires once a post has been saved.
  4202. *
  4203. * The dynamic portion of the hook name, `$post->post_type`, refers to
  4204. * the post type slug.
  4205. *
  4206. * Possible hook names include:
  4207. *
  4208. * - `save_post_post`
  4209. * - `save_post_page`
  4210. *
  4211. * @since 3.7.0
  4212. *
  4213. * @param int $post_ID Post ID.
  4214. * @param WP_Post $post Post object.
  4215. * @param bool $update Whether this is an existing post being updated.
  4216. */
  4217. do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );
  4218. /**
  4219. * Fires once a post has been saved.
  4220. *
  4221. * @since 1.5.0
  4222. *
  4223. * @param int $post_ID Post ID.
  4224. * @param WP_Post $post Post object.
  4225. * @param bool $update Whether this is an existing post being updated.
  4226. */
  4227. do_action( 'save_post', $post_ID, $post, $update );
  4228. /**
  4229. * Fires once a post has been saved.
  4230. *
  4231. * @since 2.0.0
  4232. *
  4233. * @param int $post_ID Post ID.
  4234. * @param WP_Post $post Post object.
  4235. * @param bool $update Whether this is an existing post being updated.
  4236. */
  4237. do_action( 'wp_insert_post', $post_ID, $post, $update );
  4238. if ( $fire_after_hooks ) {
  4239. wp_after_insert_post( $post, $update, $post_before );
  4240. }
  4241. return $post_ID;
  4242. }
  4243. /**
  4244. * Updates a post with new post data.
  4245. *
  4246. * The date does not have to be set for drafts. You can set the date and it will
  4247. * not be overridden.
  4248. *
  4249. * @since 1.0.0
  4250. * @since 3.5.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
  4251. * @since 5.6.0 Added the `$fire_after_hooks` parameter.
  4252. *
  4253. * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped,
  4254. * objects are not. See wp_insert_post() for accepted arguments.
  4255. * Default array.
  4256. * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  4257. * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
  4258. * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
  4259. */
  4260. function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) {
  4261. if ( is_object( $postarr ) ) {
  4262. // Non-escaped post was passed.
  4263. $postarr = get_object_vars( $postarr );
  4264. $postarr = wp_slash( $postarr );
  4265. }
  4266. // First, get all of the original fields.
  4267. $post = get_post( $postarr['ID'], ARRAY_A );
  4268. if ( is_null( $post ) ) {
  4269. if ( $wp_error ) {
  4270. return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
  4271. }
  4272. return 0;
  4273. }
  4274. // Escape data pulled from DB.
  4275. $post = wp_slash( $post );
  4276. // Passed post category list overwrites existing category list if not empty.
  4277. if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] )
  4278. && count( $postarr['post_category'] ) > 0
  4279. ) {
  4280. $post_cats = $postarr['post_category'];
  4281. } else {
  4282. $post_cats = $post['post_category'];
  4283. }
  4284. // Drafts shouldn't be assigned a date unless explicitly done so by the user.
  4285. if ( isset( $post['post_status'] )
  4286. && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true )
  4287. && empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] )
  4288. ) {
  4289. $clear_date = true;
  4290. } else {
  4291. $clear_date = false;
  4292. }
  4293. // Merge old and new fields with new fields overwriting old ones.
  4294. $postarr = array_merge( $post, $postarr );
  4295. $postarr['post_category'] = $post_cats;
  4296. if ( $clear_date ) {
  4297. $postarr['post_date'] = current_time( 'mysql' );
  4298. $postarr['post_date_gmt'] = '';
  4299. }
  4300. if ( 'attachment' === $postarr['post_type'] ) {
  4301. return wp_insert_attachment( $postarr, false, 0, $wp_error );
  4302. }
  4303. // Discard 'tags_input' parameter if it's the same as existing post tags.
  4304. if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
  4305. $tags = get_the_terms( $postarr['ID'], 'post_tag' );
  4306. $tag_names = array();
  4307. if ( $tags && ! is_wp_error( $tags ) ) {
  4308. $tag_names = wp_list_pluck( $tags, 'name' );
  4309. }
  4310. if ( $postarr['tags_input'] === $tag_names ) {
  4311. unset( $postarr['tags_input'] );
  4312. }
  4313. }
  4314. return wp_insert_post( $postarr, $wp_error, $fire_after_hooks );
  4315. }
  4316. /**
  4317. * Publishes a post by transitioning the post status.
  4318. *
  4319. * @since 2.1.0
  4320. *
  4321. * @global wpdb $wpdb WordPress database abstraction object.
  4322. *
  4323. * @param int|WP_Post $post Post ID or post object.
  4324. */
  4325. function wp_publish_post( $post ) {
  4326. global $wpdb;
  4327. $post = get_post( $post );
  4328. if ( ! $post ) {
  4329. return;
  4330. }
  4331. if ( 'publish' === $post->post_status ) {
  4332. return;
  4333. }
  4334. $post_before = get_post( $post->ID );
  4335. // Ensure at least one term is applied for taxonomies with a default term.
  4336. foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) {
  4337. // Skip taxonomy if no default term is set.
  4338. if (
  4339. 'category' !== $taxonomy &&
  4340. empty( $tax_object->default_term )
  4341. ) {
  4342. continue;
  4343. }
  4344. // Do not modify previously set terms.
  4345. if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) {
  4346. continue;
  4347. }
  4348. if ( 'category' === $taxonomy ) {
  4349. $default_term_id = (int) get_option( 'default_category', 0 );
  4350. } else {
  4351. $default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 );
  4352. }
  4353. if ( ! $default_term_id ) {
  4354. continue;
  4355. }
  4356. wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy );
  4357. }
  4358. $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
  4359. clean_post_cache( $post->ID );
  4360. $old_status = $post->post_status;
  4361. $post->post_status = 'publish';
  4362. wp_transition_post_status( 'publish', $old_status, $post );
  4363. /** This action is documented in wp-includes/post.php */
  4364. do_action( "edit_post_{$post->post_type}", $post->ID, $post );
  4365. /** This action is documented in wp-includes/post.php */
  4366. do_action( 'edit_post', $post->ID, $post );
  4367. /** This action is documented in wp-includes/post.php */
  4368. do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
  4369. /** This action is documented in wp-includes/post.php */
  4370. do_action( 'save_post', $post->ID, $post, true );
  4371. /** This action is documented in wp-includes/post.php */
  4372. do_action( 'wp_insert_post', $post->ID, $post, true );
  4373. wp_after_insert_post( $post, true, $post_before );
  4374. }
  4375. /**
  4376. * Publishes future post and make sure post ID has future post status.
  4377. *
  4378. * Invoked by cron 'publish_future_post' event. This safeguard prevents cron
  4379. * from publishing drafts, etc.
  4380. *
  4381. * @since 2.5.0
  4382. *
  4383. * @param int|WP_Post $post Post ID or post object.
  4384. */
  4385. function check_and_publish_future_post( $post ) {
  4386. $post = get_post( $post );
  4387. if ( ! $post ) {
  4388. return;
  4389. }
  4390. if ( 'future' !== $post->post_status ) {
  4391. return;
  4392. }
  4393. $time = strtotime( $post->post_date_gmt . ' GMT' );
  4394. // Uh oh, someone jumped the gun!
  4395. if ( $time > time() ) {
  4396. wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); // Clear anything else in the system.
  4397. wp_schedule_single_event( $time, 'publish_future_post', array( $post->ID ) );
  4398. return;
  4399. }
  4400. // wp_publish_post() returns no meaningful value.
  4401. wp_publish_post( $post->ID );
  4402. }
  4403. /**
  4404. * Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
  4405. * If post_date is not provided, this first checks post_date_gmt if provided,
  4406. * then falls back to use the current time.
  4407. *
  4408. * For back-compat purposes in wp_insert_post, an empty post_date and an invalid
  4409. * post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false.
  4410. *
  4411. * @since 5.7.0
  4412. *
  4413. * @param string $post_date The date in mysql format.
  4414. * @param string $post_date_gmt The GMT date in mysql format.
  4415. * @return string|false A valid Gregorian-calendar date string, or false on failure.
  4416. */
  4417. function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) {
  4418. // If the date is empty, set the date to now.
  4419. if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) {
  4420. if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) {
  4421. $post_date = current_time( 'mysql' );
  4422. } else {
  4423. $post_date = get_date_from_gmt( $post_date_gmt );
  4424. }
  4425. }
  4426. // Validate the date.
  4427. $month = (int) substr( $post_date, 5, 2 );
  4428. $day = (int) substr( $post_date, 8, 2 );
  4429. $year = (int) substr( $post_date, 0, 4 );
  4430. $valid_date = wp_checkdate( $month, $day, $year, $post_date );
  4431. if ( ! $valid_date ) {
  4432. return false;
  4433. }
  4434. return $post_date;
  4435. }
  4436. /**
  4437. * Computes a unique slug for the post, when given the desired slug and some post details.
  4438. *
  4439. * @since 2.8.0
  4440. *
  4441. * @global wpdb $wpdb WordPress database abstraction object.
  4442. * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
  4443. *
  4444. * @param string $slug The desired slug (post_name).
  4445. * @param int $post_ID Post ID.
  4446. * @param string $post_status No uniqueness checks are made if the post is still draft or pending.
  4447. * @param string $post_type Post type.
  4448. * @param int $post_parent Post parent ID.
  4449. * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix)
  4450. */
  4451. function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
  4452. if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true )
  4453. || ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type
  4454. ) {
  4455. return $slug;
  4456. }
  4457. /**
  4458. * Filters the post slug before it is generated to be unique.
  4459. *
  4460. * Returning a non-null value will short-circuit the
  4461. * unique slug generation, returning the passed value instead.
  4462. *
  4463. * @since 5.1.0
  4464. *
  4465. * @param string|null $override_slug Short-circuit return value.
  4466. * @param string $slug The desired slug (post_name).
  4467. * @param int $post_ID Post ID.
  4468. * @param string $post_status The post status.
  4469. * @param string $post_type Post type.
  4470. * @param int $post_parent Post parent ID.
  4471. */
  4472. $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent );
  4473. if ( null !== $override_slug ) {
  4474. return $override_slug;
  4475. }
  4476. global $wpdb, $wp_rewrite;
  4477. $original_slug = $slug;
  4478. $feeds = $wp_rewrite->feeds;
  4479. if ( ! is_array( $feeds ) ) {
  4480. $feeds = array();
  4481. }
  4482. if ( 'attachment' === $post_type ) {
  4483. // Attachment slugs must be unique across all types.
  4484. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
  4485. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
  4486. /**
  4487. * Filters whether the post slug would make a bad attachment slug.
  4488. *
  4489. * @since 3.1.0
  4490. *
  4491. * @param bool $bad_slug Whether the slug would be bad as an attachment slug.
  4492. * @param string $slug The post slug.
  4493. */
  4494. $is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug );
  4495. if ( $post_name_check
  4496. || in_array( $slug, $feeds, true ) || 'embed' === $slug
  4497. || $is_bad_attachment_slug
  4498. ) {
  4499. $suffix = 2;
  4500. do {
  4501. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  4502. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) );
  4503. $suffix++;
  4504. } while ( $post_name_check );
  4505. $slug = $alt_post_name;
  4506. }
  4507. } elseif ( is_post_type_hierarchical( $post_type ) ) {
  4508. if ( 'nav_menu_item' === $post_type ) {
  4509. return $slug;
  4510. }
  4511. /*
  4512. * Page slugs must be unique within their own trees. Pages are in a separate
  4513. * namespace than posts so page slugs are allowed to overlap post slugs.
  4514. */
  4515. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
  4516. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
  4517. /**
  4518. * Filters whether the post slug would make a bad hierarchical post slug.
  4519. *
  4520. * @since 3.1.0
  4521. *
  4522. * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context.
  4523. * @param string $slug The post slug.
  4524. * @param string $post_type Post type.
  4525. * @param int $post_parent Post parent ID.
  4526. */
  4527. $is_bad_hierarchical_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent );
  4528. if ( $post_name_check
  4529. || in_array( $slug, $feeds, true ) || 'embed' === $slug
  4530. || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug )
  4531. || $is_bad_hierarchical_slug
  4532. ) {
  4533. $suffix = 2;
  4534. do {
  4535. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  4536. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) );
  4537. $suffix++;
  4538. } while ( $post_name_check );
  4539. $slug = $alt_post_name;
  4540. }
  4541. } else {
  4542. // Post slugs must be unique across all posts.
  4543. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
  4544. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
  4545. $post = get_post( $post_ID );
  4546. // Prevent new post slugs that could result in URLs that conflict with date archives.
  4547. $conflicts_with_date_archive = false;
  4548. if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) {
  4549. $slug_num = (int) $slug;
  4550. if ( $slug_num ) {
  4551. $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) );
  4552. $postname_index = array_search( '%postname%', $permastructs, true );
  4553. /*
  4554. * Potential date clashes are as follows:
  4555. *
  4556. * - Any integer in the first permastruct position could be a year.
  4557. * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'.
  4558. * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'.
  4559. */
  4560. if ( 0 === $postname_index ||
  4561. ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) ||
  4562. ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num )
  4563. ) {
  4564. $conflicts_with_date_archive = true;
  4565. }
  4566. }
  4567. }
  4568. /**
  4569. * Filters whether the post slug would be bad as a flat slug.
  4570. *
  4571. * @since 3.1.0
  4572. *
  4573. * @param bool $bad_slug Whether the post slug would be bad as a flat slug.
  4574. * @param string $slug The post slug.
  4575. * @param string $post_type Post type.
  4576. */
  4577. $is_bad_flat_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type );
  4578. if ( $post_name_check
  4579. || in_array( $slug, $feeds, true ) || 'embed' === $slug
  4580. || $conflicts_with_date_archive
  4581. || $is_bad_flat_slug
  4582. ) {
  4583. $suffix = 2;
  4584. do {
  4585. $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  4586. $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) );
  4587. $suffix++;
  4588. } while ( $post_name_check );
  4589. $slug = $alt_post_name;
  4590. }
  4591. }
  4592. /**
  4593. * Filters the unique post slug.
  4594. *
  4595. * @since 3.3.0
  4596. *
  4597. * @param string $slug The post slug.
  4598. * @param int $post_ID Post ID.
  4599. * @param string $post_status The post status.
  4600. * @param string $post_type Post type.
  4601. * @param int $post_parent Post parent ID
  4602. * @param string $original_slug The original post slug.
  4603. */
  4604. return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
  4605. }
  4606. /**
  4607. * Truncates a post slug.
  4608. *
  4609. * @since 3.6.0
  4610. * @access private
  4611. *
  4612. * @see utf8_uri_encode()
  4613. *
  4614. * @param string $slug The slug to truncate.
  4615. * @param int $length Optional. Max length of the slug. Default 200 (characters).
  4616. * @return string The truncated slug.
  4617. */
  4618. function _truncate_post_slug( $slug, $length = 200 ) {
  4619. if ( strlen( $slug ) > $length ) {
  4620. $decoded_slug = urldecode( $slug );
  4621. if ( $decoded_slug === $slug ) {
  4622. $slug = substr( $slug, 0, $length );
  4623. } else {
  4624. $slug = utf8_uri_encode( $decoded_slug, $length, true );
  4625. }
  4626. }
  4627. return rtrim( $slug, '-' );
  4628. }
  4629. /**
  4630. * Adds tags to a post.
  4631. *
  4632. * @see wp_set_post_tags()
  4633. *
  4634. * @since 2.3.0
  4635. *
  4636. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post.
  4637. * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags
  4638. * separated by commas. Default empty.
  4639. * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure.
  4640. */
  4641. function wp_add_post_tags( $post_id = 0, $tags = '' ) {
  4642. return wp_set_post_tags( $post_id, $tags, true );
  4643. }
  4644. /**
  4645. * Sets the tags for a post.
  4646. *
  4647. * @since 2.3.0
  4648. *
  4649. * @see wp_set_object_terms()
  4650. *
  4651. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post.
  4652. * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags
  4653. * separated by commas. Default empty.
  4654. * @param bool $append Optional. If true, don't delete existing tags, just add on. If false,
  4655. * replace the tags with the new tags. Default false.
  4656. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
  4657. */
  4658. function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) {
  4659. return wp_set_post_terms( $post_id, $tags, 'post_tag', $append );
  4660. }
  4661. /**
  4662. * Sets the terms for a post.
  4663. *
  4664. * @since 2.8.0
  4665. *
  4666. * @see wp_set_object_terms()
  4667. *
  4668. * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post.
  4669. * @param string|array $terms Optional. An array of terms to set for the post, or a string of terms
  4670. * separated by commas. Hierarchical taxonomies must always pass IDs rather
  4671. * than names so that children with the same names but different parents
  4672. * aren't confused. Default empty.
  4673. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
  4674. * @param bool $append Optional. If true, don't delete existing terms, just add on. If false,
  4675. * replace the terms with the new terms. Default false.
  4676. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
  4677. */
  4678. function wp_set_post_terms( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false ) {
  4679. $post_id = (int) $post_id;
  4680. if ( ! $post_id ) {
  4681. return false;
  4682. }
  4683. if ( empty( $terms ) ) {
  4684. $terms = array();
  4685. }
  4686. if ( ! is_array( $terms ) ) {
  4687. $comma = _x( ',', 'tag delimiter' );
  4688. if ( ',' !== $comma ) {
  4689. $terms = str_replace( $comma, ',', $terms );
  4690. }
  4691. $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
  4692. }
  4693. /*
  4694. * Hierarchical taxonomies must always pass IDs rather than names so that
  4695. * children with the same names but different parents aren't confused.
  4696. */
  4697. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  4698. $terms = array_unique( array_map( 'intval', $terms ) );
  4699. }
  4700. return wp_set_object_terms( $post_id, $terms, $taxonomy, $append );
  4701. }
  4702. /**
  4703. * Sets categories for a post.
  4704. *
  4705. * If no categories are provided, the default category is used.
  4706. *
  4707. * @since 2.1.0
  4708. *
  4709. * @param int $post_ID Optional. The Post ID. Does not default to the ID
  4710. * of the global $post. Default 0.
  4711. * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category.
  4712. * Default empty array.
  4713. * @param bool $append If true, don't delete existing categories, just add on.
  4714. * If false, replace the categories with the new categories.
  4715. * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
  4716. */
  4717. function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
  4718. $post_ID = (int) $post_ID;
  4719. $post_type = get_post_type( $post_ID );
  4720. $post_status = get_post_status( $post_ID );
  4721. // If $post_categories isn't already an array, make it one.
  4722. $post_categories = (array) $post_categories;
  4723. if ( empty( $post_categories ) ) {
  4724. /**
  4725. * Filters post types (in addition to 'post') that require a default category.
  4726. *
  4727. * @since 5.5.0
  4728. *
  4729. * @param string[] $post_types An array of post type names. Default empty array.
  4730. */
  4731. $default_category_post_types = apply_filters( 'default_category_post_types', array() );
  4732. // Regular posts always require a default category.
  4733. $default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) );
  4734. if ( in_array( $post_type, $default_category_post_types, true )
  4735. && is_object_in_taxonomy( $post_type, 'category' )
  4736. && 'auto-draft' !== $post_status
  4737. ) {
  4738. $post_categories = array( get_option( 'default_category' ) );
  4739. $append = false;
  4740. } else {
  4741. $post_categories = array();
  4742. }
  4743. } elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) {
  4744. return true;
  4745. }
  4746. return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
  4747. }
  4748. /**
  4749. * Fires actions related to the transitioning of a post's status.
  4750. *
  4751. * When a post is saved, the post status is "transitioned" from one status to another,
  4752. * though this does not always mean the status has actually changed before and after
  4753. * the save. This function fires a number of action hooks related to that transition:
  4754. * the generic {@see 'transition_post_status'} action, as well as the dynamic hooks
  4755. * {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note
  4756. * that the function does not transition the post object in the database.
  4757. *
  4758. * For instance: When publishing a post for the first time, the post status may transition
  4759. * from 'draft' – or some other status – to 'publish'. However, if a post is already
  4760. * published and is simply being updated, the "old" and "new" statuses may both be 'publish'
  4761. * before and after the transition.
  4762. *
  4763. * @since 2.3.0
  4764. *
  4765. * @param string $new_status Transition to this post status.
  4766. * @param string $old_status Previous post status.
  4767. * @param WP_Post $post Post data.
  4768. */
  4769. function wp_transition_post_status( $new_status, $old_status, $post ) {
  4770. /**
  4771. * Fires when a post is transitioned from one status to another.
  4772. *
  4773. * @since 2.3.0
  4774. *
  4775. * @param string $new_status New post status.
  4776. * @param string $old_status Old post status.
  4777. * @param WP_Post $post Post object.
  4778. */
  4779. do_action( 'transition_post_status', $new_status, $old_status, $post );
  4780. /**
  4781. * Fires when a post is transitioned from one status to another.
  4782. *
  4783. * The dynamic portions of the hook name, `$new_status` and `$old_status`,
  4784. * refer to the old and new post statuses, respectively.
  4785. *
  4786. * Possible hook names include:
  4787. *
  4788. * - `draft_to_publish`
  4789. * - `publish_to_trash`
  4790. * - `pending_to_draft`
  4791. *
  4792. * @since 2.3.0
  4793. *
  4794. * @param WP_Post $post Post object.
  4795. */
  4796. do_action( "{$old_status}_to_{$new_status}", $post );
  4797. /**
  4798. * Fires when a post is transitioned from one status to another.
  4799. *
  4800. * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
  4801. * refer to the new post status and post type, respectively.
  4802. *
  4803. * Possible hook names include:
  4804. *
  4805. * - `draft_post`
  4806. * - `future_post`
  4807. * - `pending_post`
  4808. * - `private_post`
  4809. * - `publish_post`
  4810. * - `trash_post`
  4811. * - `draft_page`
  4812. * - `future_page`
  4813. * - `pending_page`
  4814. * - `private_page`
  4815. * - `publish_page`
  4816. * - `trash_page`
  4817. * - `publish_attachment`
  4818. * - `trash_attachment`
  4819. *
  4820. * Please note: When this action is hooked using a particular post status (like
  4821. * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
  4822. * first transitioned to that status from something else, as well as upon
  4823. * subsequent post updates (old and new status are both the same).
  4824. *
  4825. * Therefore, if you are looking to only fire a callback when a post is first
  4826. * transitioned to a status, use the {@see 'transition_post_status'} hook instead.
  4827. *
  4828. * @since 2.3.0
  4829. * @since 5.9.0 Added `$old_status` parameter.
  4830. *
  4831. * @param int $post_id Post ID.
  4832. * @param WP_Post $post Post object.
  4833. * @param string $old_status Old post status.
  4834. */
  4835. do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status );
  4836. }
  4837. /**
  4838. * Fires actions after a post, its terms and meta data has been saved.
  4839. *
  4840. * @since 5.6.0
  4841. *
  4842. * @param int|WP_Post $post The post ID or object that has been saved.
  4843. * @param bool $update Whether this is an existing post being updated.
  4844. * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
  4845. * to the update for updated posts.
  4846. */
  4847. function wp_after_insert_post( $post, $update, $post_before ) {
  4848. $post = get_post( $post );
  4849. if ( ! $post ) {
  4850. return;
  4851. }
  4852. $post_id = $post->ID;
  4853. /**
  4854. * Fires once a post, its terms and meta data has been saved.
  4855. *
  4856. * @since 5.6.0
  4857. *
  4858. * @param int $post_id Post ID.
  4859. * @param WP_Post $post Post object.
  4860. * @param bool $update Whether this is an existing post being updated.
  4861. * @param null|WP_Post $post_before Null for new posts, the WP_Post object prior
  4862. * to the update for updated posts.
  4863. */
  4864. do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before );
  4865. }
  4866. //
  4867. // Comment, trackback, and pingback functions.
  4868. //
  4869. /**
  4870. * Adds a URL to those already pinged.
  4871. *
  4872. * @since 1.5.0
  4873. * @since 4.7.0 `$post` can be a WP_Post object.
  4874. * @since 4.7.0 `$uri` can be an array of URIs.
  4875. *
  4876. * @global wpdb $wpdb WordPress database abstraction object.
  4877. *
  4878. * @param int|WP_Post $post Post ID or post object.
  4879. * @param string|array $uri Ping URI or array of URIs.
  4880. * @return int|false How many rows were updated.
  4881. */
  4882. function add_ping( $post, $uri ) {
  4883. global $wpdb;
  4884. $post = get_post( $post );
  4885. if ( ! $post ) {
  4886. return false;
  4887. }
  4888. $pung = trim( $post->pinged );
  4889. $pung = preg_split( '/\s/', $pung );
  4890. if ( is_array( $uri ) ) {
  4891. $pung = array_merge( $pung, $uri );
  4892. } else {
  4893. $pung[] = $uri;
  4894. }
  4895. $new = implode( "\n", $pung );
  4896. /**
  4897. * Filters the new ping URL to add for the given post.
  4898. *
  4899. * @since 2.0.0
  4900. *
  4901. * @param string $new New ping URL to add.
  4902. */
  4903. $new = apply_filters( 'add_ping', $new );
  4904. $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) );
  4905. clean_post_cache( $post->ID );
  4906. return $return;
  4907. }
  4908. /**
  4909. * Retrieves enclosures already enclosed for a post.
  4910. *
  4911. * @since 1.5.0
  4912. *
  4913. * @param int $post_id Post ID.
  4914. * @return string[] Array of enclosures for the given post.
  4915. */
  4916. function get_enclosed( $post_id ) {
  4917. $custom_fields = get_post_custom( $post_id );
  4918. $pung = array();
  4919. if ( ! is_array( $custom_fields ) ) {
  4920. return $pung;
  4921. }
  4922. foreach ( $custom_fields as $key => $val ) {
  4923. if ( 'enclosure' !== $key || ! is_array( $val ) ) {
  4924. continue;
  4925. }
  4926. foreach ( $val as $enc ) {
  4927. $enclosure = explode( "\n", $enc );
  4928. $pung[] = trim( $enclosure[0] );
  4929. }
  4930. }
  4931. /**
  4932. * Filters the list of enclosures already enclosed for the given post.
  4933. *
  4934. * @since 2.0.0
  4935. *
  4936. * @param string[] $pung Array of enclosures for the given post.
  4937. * @param int $post_id Post ID.
  4938. */
  4939. return apply_filters( 'get_enclosed', $pung, $post_id );
  4940. }
  4941. /**
  4942. * Retrieves URLs already pinged for a post.
  4943. *
  4944. * @since 1.5.0
  4945. *
  4946. * @since 4.7.0 `$post` can be a WP_Post object.
  4947. *
  4948. * @param int|WP_Post $post Post ID or object.
  4949. * @return string[]|false Array of URLs already pinged for the given post, false if the post is not found.
  4950. */
  4951. function get_pung( $post ) {
  4952. $post = get_post( $post );
  4953. if ( ! $post ) {
  4954. return false;
  4955. }
  4956. $pung = trim( $post->pinged );
  4957. $pung = preg_split( '/\s/', $pung );
  4958. /**
  4959. * Filters the list of already-pinged URLs for the given post.
  4960. *
  4961. * @since 2.0.0
  4962. *
  4963. * @param string[] $pung Array of URLs already pinged for the given post.
  4964. */
  4965. return apply_filters( 'get_pung', $pung );
  4966. }
  4967. /**
  4968. * Retrieves URLs that need to be pinged.
  4969. *
  4970. * @since 1.5.0
  4971. * @since 4.7.0 `$post` can be a WP_Post object.
  4972. *
  4973. * @param int|WP_Post $post Post ID or post object.
  4974. * @return string[]|false List of URLs yet to ping.
  4975. */
  4976. function get_to_ping( $post ) {
  4977. $post = get_post( $post );
  4978. if ( ! $post ) {
  4979. return false;
  4980. }
  4981. $to_ping = sanitize_trackback_urls( $post->to_ping );
  4982. $to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY );
  4983. /**
  4984. * Filters the list of URLs yet to ping for the given post.
  4985. *
  4986. * @since 2.0.0
  4987. *
  4988. * @param string[] $to_ping List of URLs yet to ping.
  4989. */
  4990. return apply_filters( 'get_to_ping', $to_ping );
  4991. }
  4992. /**
  4993. * Does trackbacks for a list of URLs.
  4994. *
  4995. * @since 1.0.0
  4996. *
  4997. * @param string $tb_list Comma separated list of URLs.
  4998. * @param int $post_id Post ID.
  4999. */
  5000. function trackback_url_list( $tb_list, $post_id ) {
  5001. if ( ! empty( $tb_list ) ) {
  5002. // Get post data.
  5003. $postdata = get_post( $post_id, ARRAY_A );
  5004. // Form an excerpt.
  5005. $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] );
  5006. if ( strlen( $excerpt ) > 255 ) {
  5007. $excerpt = substr( $excerpt, 0, 252 ) . '&hellip;';
  5008. }
  5009. $trackback_urls = explode( ',', $tb_list );
  5010. foreach ( (array) $trackback_urls as $tb_url ) {
  5011. $tb_url = trim( $tb_url );
  5012. trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id );
  5013. }
  5014. }
  5015. }
  5016. //
  5017. // Page functions.
  5018. //
  5019. /**
  5020. * Gets a list of page IDs.
  5021. *
  5022. * @since 2.0.0
  5023. *
  5024. * @global wpdb $wpdb WordPress database abstraction object.
  5025. *
  5026. * @return string[] List of page IDs as strings.
  5027. */
  5028. function get_all_page_ids() {
  5029. global $wpdb;
  5030. $page_ids = wp_cache_get( 'all_page_ids', 'posts' );
  5031. if ( ! is_array( $page_ids ) ) {
  5032. $page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" );
  5033. wp_cache_add( 'all_page_ids', $page_ids, 'posts' );
  5034. }
  5035. return $page_ids;
  5036. }
  5037. /**
  5038. * Retrieves page data given a page ID or page object.
  5039. *
  5040. * Use get_post() instead of get_page().
  5041. *
  5042. * @since 1.5.1
  5043. * @deprecated 3.5.0 Use get_post()
  5044. *
  5045. * @param int|WP_Post $page Page object or page ID. Passed by reference.
  5046. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  5047. * correspond to a WP_Post object, an associative array, or a numeric array,
  5048. * respectively. Default OBJECT.
  5049. * @param string $filter Optional. How the return value should be filtered. Accepts 'raw',
  5050. * 'edit', 'db', 'display'. Default 'raw'.
  5051. * @return WP_Post|array|null WP_Post or array on success, null on failure.
  5052. */
  5053. function get_page( $page, $output = OBJECT, $filter = 'raw' ) {
  5054. return get_post( $page, $output, $filter );
  5055. }
  5056. /**
  5057. * Retrieves a page given its path.
  5058. *
  5059. * @since 2.1.0
  5060. *
  5061. * @global wpdb $wpdb WordPress database abstraction object.
  5062. *
  5063. * @param string $page_path Page path.
  5064. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  5065. * correspond to a WP_Post object, an associative array, or a numeric array,
  5066. * respectively. Default OBJECT.
  5067. * @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
  5068. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
  5069. */
  5070. function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
  5071. global $wpdb;
  5072. $last_changed = wp_cache_get_last_changed( 'posts' );
  5073. $hash = md5( $page_path . serialize( $post_type ) );
  5074. $cache_key = "get_page_by_path:$hash:$last_changed";
  5075. $cached = wp_cache_get( $cache_key, 'posts' );
  5076. if ( false !== $cached ) {
  5077. // Special case: '0' is a bad `$page_path`.
  5078. if ( '0' === $cached || 0 === $cached ) {
  5079. return;
  5080. } else {
  5081. return get_post( $cached, $output );
  5082. }
  5083. }
  5084. $page_path = rawurlencode( urldecode( $page_path ) );
  5085. $page_path = str_replace( '%2F', '/', $page_path );
  5086. $page_path = str_replace( '%20', ' ', $page_path );
  5087. $parts = explode( '/', trim( $page_path, '/' ) );
  5088. $parts = array_map( 'sanitize_title_for_query', $parts );
  5089. $escaped_parts = esc_sql( $parts );
  5090. $in_string = "'" . implode( "','", $escaped_parts ) . "'";
  5091. if ( is_array( $post_type ) ) {
  5092. $post_types = $post_type;
  5093. } else {
  5094. $post_types = array( $post_type, 'attachment' );
  5095. }
  5096. $post_types = esc_sql( $post_types );
  5097. $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
  5098. $sql = "
  5099. SELECT ID, post_name, post_parent, post_type
  5100. FROM $wpdb->posts
  5101. WHERE post_name IN ($in_string)
  5102. AND post_type IN ($post_type_in_string)
  5103. ";
  5104. $pages = $wpdb->get_results( $sql, OBJECT_K );
  5105. $revparts = array_reverse( $parts );
  5106. $foundid = 0;
  5107. foreach ( (array) $pages as $page ) {
  5108. if ( $page->post_name == $revparts[0] ) {
  5109. $count = 0;
  5110. $p = $page;
  5111. /*
  5112. * Loop through the given path parts from right to left,
  5113. * ensuring each matches the post ancestry.
  5114. */
  5115. while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) {
  5116. $count++;
  5117. $parent = $pages[ $p->post_parent ];
  5118. if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) {
  5119. break;
  5120. }
  5121. $p = $parent;
  5122. }
  5123. if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) {
  5124. $foundid = $page->ID;
  5125. if ( $page->post_type == $post_type ) {
  5126. break;
  5127. }
  5128. }
  5129. }
  5130. }
  5131. // We cache misses as well as hits.
  5132. wp_cache_set( $cache_key, $foundid, 'posts' );
  5133. if ( $foundid ) {
  5134. return get_post( $foundid, $output );
  5135. }
  5136. return null;
  5137. }
  5138. /**
  5139. * Retrieves a page given its title.
  5140. *
  5141. * If more than one post uses the same title, the post with the smallest ID will be returned.
  5142. * Be careful: in case of more than one post having the same title, it will check the oldest
  5143. * publication date, not the smallest ID.
  5144. *
  5145. * Because this function uses the MySQL '=' comparison, $page_title will usually be matched
  5146. * as case-insensitive with default collation.
  5147. *
  5148. * @since 2.1.0
  5149. * @since 3.0.0 The `$post_type` parameter was added.
  5150. *
  5151. * @global wpdb $wpdb WordPress database abstraction object.
  5152. *
  5153. * @param string $page_title Page title.
  5154. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
  5155. * correspond to a WP_Post object, an associative array, or a numeric array,
  5156. * respectively. Default OBJECT.
  5157. * @param string|array $post_type Optional. Post type or array of post types. Default 'page'.
  5158. * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
  5159. */
  5160. function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
  5161. global $wpdb;
  5162. if ( is_array( $post_type ) ) {
  5163. $post_type = esc_sql( $post_type );
  5164. $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
  5165. $sql = $wpdb->prepare(
  5166. "
  5167. SELECT ID
  5168. FROM $wpdb->posts
  5169. WHERE post_title = %s
  5170. AND post_type IN ($post_type_in_string)
  5171. ",
  5172. $page_title
  5173. );
  5174. } else {
  5175. $sql = $wpdb->prepare(
  5176. "
  5177. SELECT ID
  5178. FROM $wpdb->posts
  5179. WHERE post_title = %s
  5180. AND post_type = %s
  5181. ",
  5182. $page_title,
  5183. $post_type
  5184. );
  5185. }
  5186. $page = $wpdb->get_var( $sql );
  5187. if ( $page ) {
  5188. return get_post( $page, $output );
  5189. }
  5190. return null;
  5191. }
  5192. /**
  5193. * Identifies descendants of a given page ID in a list of page objects.
  5194. *
  5195. * Descendants are identified from the `$pages` array passed to the function. No database queries are performed.
  5196. *
  5197. * @since 1.5.1
  5198. *
  5199. * @param int $page_id Page ID.
  5200. * @param WP_Post[] $pages List of page objects from which descendants should be identified.
  5201. * @return WP_Post[] List of page children.
  5202. */
  5203. function get_page_children( $page_id, $pages ) {
  5204. // Build a hash of ID -> children.
  5205. $children = array();
  5206. foreach ( (array) $pages as $page ) {
  5207. $children[ (int) $page->post_parent ][] = $page;
  5208. }
  5209. $page_list = array();
  5210. // Start the search by looking at immediate children.
  5211. if ( isset( $children[ $page_id ] ) ) {
  5212. // Always start at the end of the stack in order to preserve original `$pages` order.
  5213. $to_look = array_reverse( $children[ $page_id ] );
  5214. while ( $to_look ) {
  5215. $p = array_pop( $to_look );
  5216. $page_list[] = $p;
  5217. if ( isset( $children[ $p->ID ] ) ) {
  5218. foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
  5219. // Append to the `$to_look` stack to descend the tree.
  5220. $to_look[] = $child;
  5221. }
  5222. }
  5223. }
  5224. }
  5225. return $page_list;
  5226. }
  5227. /**
  5228. * Orders the pages with children under parents in a flat list.
  5229. *
  5230. * It uses auxiliary structure to hold parent-children relationships and
  5231. * runs in O(N) complexity
  5232. *
  5233. * @since 2.0.0
  5234. *
  5235. * @param WP_Post[] $pages Posts array (passed by reference).
  5236. * @param int $page_id Optional. Parent page ID. Default 0.
  5237. * @return string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents.
  5238. */
  5239. function get_page_hierarchy( &$pages, $page_id = 0 ) {
  5240. if ( empty( $pages ) ) {
  5241. return array();
  5242. }
  5243. $children = array();
  5244. foreach ( (array) $pages as $p ) {
  5245. $parent_id = (int) $p->post_parent;
  5246. $children[ $parent_id ][] = $p;
  5247. }
  5248. $result = array();
  5249. _page_traverse_name( $page_id, $children, $result );
  5250. return $result;
  5251. }
  5252. /**
  5253. * Traverses and return all the nested children post names of a root page.
  5254. *
  5255. * $children contains parent-children relations
  5256. *
  5257. * @since 2.9.0
  5258. * @access private
  5259. *
  5260. * @see _page_traverse_name()
  5261. *
  5262. * @param int $page_id Page ID.
  5263. * @param array $children Parent-children relations (passed by reference).
  5264. * @param string[] $result Array of page names keyed by ID (passed by reference).
  5265. */
  5266. function _page_traverse_name( $page_id, &$children, &$result ) {
  5267. if ( isset( $children[ $page_id ] ) ) {
  5268. foreach ( (array) $children[ $page_id ] as $child ) {
  5269. $result[ $child->ID ] = $child->post_name;
  5270. _page_traverse_name( $child->ID, $children, $result );
  5271. }
  5272. }
  5273. }
  5274. /**
  5275. * Builds the URI path for a page.
  5276. *
  5277. * Sub pages will be in the "directory" under the parent page post name.
  5278. *
  5279. * @since 1.5.0
  5280. * @since 4.6.0 The `$page` parameter was made optional.
  5281. *
  5282. * @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post.
  5283. * @return string|false Page URI, false on error.
  5284. */
  5285. function get_page_uri( $page = 0 ) {
  5286. if ( ! $page instanceof WP_Post ) {
  5287. $page = get_post( $page );
  5288. }
  5289. if ( ! $page ) {
  5290. return false;
  5291. }
  5292. $uri = $page->post_name;
  5293. foreach ( $page->ancestors as $parent ) {
  5294. $parent = get_post( $parent );
  5295. if ( $parent && $parent->post_name ) {
  5296. $uri = $parent->post_name . '/' . $uri;
  5297. }
  5298. }
  5299. /**
  5300. * Filters the URI for a page.
  5301. *
  5302. * @since 4.4.0
  5303. *
  5304. * @param string $uri Page URI.
  5305. * @param WP_Post $page Page object.
  5306. */
  5307. return apply_filters( 'get_page_uri', $uri, $page );
  5308. }
  5309. /**
  5310. * Retrieves an array of pages (or hierarchical post type items).
  5311. *
  5312. * @global wpdb $wpdb WordPress database abstraction object.
  5313. *
  5314. * @since 1.5.0
  5315. *
  5316. * @param array|string $args {
  5317. * Optional. Array or string of arguments to retrieve pages.
  5318. *
  5319. * @type int $child_of Page ID to return child and grandchild pages of. Note: The value
  5320. * of `$hierarchical` has no bearing on whether `$child_of` returns
  5321. * hierarchical results. Default 0, or no restriction.
  5322. * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'.
  5323. * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author',
  5324. * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order',
  5325. * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'.
  5326. * 'post_' can be omitted for any values that start with it.
  5327. * Default 'post_title'.
  5328. * @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with
  5329. * `$child_of` also being false, both arguments will be disregarded.
  5330. * Default true.
  5331. * @type int[] $exclude Array of page IDs to exclude. Default empty array.
  5332. * @type int[] $include Array of page IDs to include. Cannot be used with `$child_of`,
  5333. * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`.
  5334. * Default empty array.
  5335. * @type string $meta_key Only include pages with this meta key. Default empty.
  5336. * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`.
  5337. * Default empty.
  5338. * @type string $authors A comma-separated list of author IDs. Default empty.
  5339. * @type int $parent Page ID to return direct children of. Default -1, or no restriction.
  5340. * @type string|int[] $exclude_tree Comma-separated string or array of page IDs to exclude.
  5341. * Default empty array.
  5342. * @type int $number The number of pages to return. Default 0, or all pages.
  5343. * @type int $offset The number of pages to skip before returning. Requires `$number`.
  5344. * Default 0.
  5345. * @type string $post_type The post type to query. Default 'page'.
  5346. * @type string|array $post_status A comma-separated list or array of post statuses to include.
  5347. * Default 'publish'.
  5348. * }
  5349. * @return WP_Post[]|false Array of pages (or hierarchical post type items). Boolean false if the
  5350. * specified post type is not hierarchical or the specified status is not
  5351. * supported by the post type.
  5352. */
  5353. function get_pages( $args = array() ) {
  5354. global $wpdb;
  5355. $defaults = array(
  5356. 'child_of' => 0,
  5357. 'sort_order' => 'ASC',
  5358. 'sort_column' => 'post_title',
  5359. 'hierarchical' => 1,
  5360. 'exclude' => array(),
  5361. 'include' => array(),
  5362. 'meta_key' => '',
  5363. 'meta_value' => '',
  5364. 'authors' => '',
  5365. 'parent' => -1,
  5366. 'exclude_tree' => array(),
  5367. 'number' => '',
  5368. 'offset' => 0,
  5369. 'post_type' => 'page',
  5370. 'post_status' => 'publish',
  5371. );
  5372. $parsed_args = wp_parse_args( $args, $defaults );
  5373. $number = (int) $parsed_args['number'];
  5374. $offset = (int) $parsed_args['offset'];
  5375. $child_of = (int) $parsed_args['child_of'];
  5376. $hierarchical = $parsed_args['hierarchical'];
  5377. $exclude = $parsed_args['exclude'];
  5378. $meta_key = $parsed_args['meta_key'];
  5379. $meta_value = $parsed_args['meta_value'];
  5380. $parent = $parsed_args['parent'];
  5381. $post_status = $parsed_args['post_status'];
  5382. // Make sure the post type is hierarchical.
  5383. $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
  5384. if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) {
  5385. return false;
  5386. }
  5387. if ( $parent > 0 && ! $child_of ) {
  5388. $hierarchical = false;
  5389. }
  5390. // Make sure we have a valid post status.
  5391. if ( ! is_array( $post_status ) ) {
  5392. $post_status = explode( ',', $post_status );
  5393. }
  5394. if ( array_diff( $post_status, get_post_stati() ) ) {
  5395. return false;
  5396. }
  5397. // $args can be whatever, only use the args defined in defaults to compute the key.
  5398. $key = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) );
  5399. $last_changed = wp_cache_get_last_changed( 'posts' );
  5400. $cache_key = "get_pages:$key:$last_changed";
  5401. $cache = wp_cache_get( $cache_key, 'posts' );
  5402. if ( false !== $cache ) {
  5403. _prime_post_caches( $cache, false, false );
  5404. // Convert to WP_Post instances.
  5405. $pages = array_map( 'get_post', $cache );
  5406. /** This filter is documented in wp-includes/post.php */
  5407. $pages = apply_filters( 'get_pages', $pages, $parsed_args );
  5408. return $pages;
  5409. }
  5410. $inclusions = '';
  5411. if ( ! empty( $parsed_args['include'] ) ) {
  5412. $child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
  5413. $parent = -1;
  5414. $exclude = '';
  5415. $meta_key = '';
  5416. $meta_value = '';
  5417. $hierarchical = false;
  5418. $incpages = wp_parse_id_list( $parsed_args['include'] );
  5419. if ( ! empty( $incpages ) ) {
  5420. $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')';
  5421. }
  5422. }
  5423. $exclusions = '';
  5424. if ( ! empty( $exclude ) ) {
  5425. $expages = wp_parse_id_list( $exclude );
  5426. if ( ! empty( $expages ) ) {
  5427. $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')';
  5428. }
  5429. }
  5430. $author_query = '';
  5431. if ( ! empty( $parsed_args['authors'] ) ) {
  5432. $post_authors = wp_parse_list( $parsed_args['authors'] );
  5433. if ( ! empty( $post_authors ) ) {
  5434. foreach ( $post_authors as $post_author ) {
  5435. // Do we have an author id or an author login?
  5436. if ( 0 == (int) $post_author ) {
  5437. $post_author = get_user_by( 'login', $post_author );
  5438. if ( empty( $post_author ) ) {
  5439. continue;
  5440. }
  5441. if ( empty( $post_author->ID ) ) {
  5442. continue;
  5443. }
  5444. $post_author = $post_author->ID;
  5445. }
  5446. if ( '' === $author_query ) {
  5447. $author_query = $wpdb->prepare( ' post_author = %d ', $post_author );
  5448. } else {
  5449. $author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author );
  5450. }
  5451. }
  5452. if ( '' !== $author_query ) {
  5453. $author_query = " AND ($author_query)";
  5454. }
  5455. }
  5456. }
  5457. $join = '';
  5458. $where = "$exclusions $inclusions ";
  5459. if ( '' !== $meta_key || '' !== $meta_value ) {
  5460. $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";
  5461. // meta_key and meta_value might be slashed.
  5462. $meta_key = wp_unslash( $meta_key );
  5463. $meta_value = wp_unslash( $meta_value );
  5464. if ( '' !== $meta_key ) {
  5465. $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key );
  5466. }
  5467. if ( '' !== $meta_value ) {
  5468. $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value );
  5469. }
  5470. }
  5471. if ( is_array( $parent ) ) {
  5472. $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
  5473. if ( ! empty( $post_parent__in ) ) {
  5474. $where .= " AND post_parent IN ($post_parent__in)";
  5475. }
  5476. } elseif ( $parent >= 0 ) {
  5477. $where .= $wpdb->prepare( ' AND post_parent = %d ', $parent );
  5478. }
  5479. if ( 1 === count( $post_status ) ) {
  5480. $where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) );
  5481. } else {
  5482. $post_status = implode( "', '", $post_status );
  5483. $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] );
  5484. }
  5485. $orderby_array = array();
  5486. $allowed_keys = array(
  5487. 'author',
  5488. 'post_author',
  5489. 'date',
  5490. 'post_date',
  5491. 'title',
  5492. 'post_title',
  5493. 'name',
  5494. 'post_name',
  5495. 'modified',
  5496. 'post_modified',
  5497. 'modified_gmt',
  5498. 'post_modified_gmt',
  5499. 'menu_order',
  5500. 'parent',
  5501. 'post_parent',
  5502. 'ID',
  5503. 'rand',
  5504. 'comment_count',
  5505. );
  5506. foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) {
  5507. $orderby = trim( $orderby );
  5508. if ( ! in_array( $orderby, $allowed_keys, true ) ) {
  5509. continue;
  5510. }
  5511. switch ( $orderby ) {
  5512. case 'menu_order':
  5513. break;
  5514. case 'ID':
  5515. $orderby = "$wpdb->posts.ID";
  5516. break;
  5517. case 'rand':
  5518. $orderby = 'RAND()';
  5519. break;
  5520. case 'comment_count':
  5521. $orderby = "$wpdb->posts.comment_count";
  5522. break;
  5523. default:
  5524. if ( 0 === strpos( $orderby, 'post_' ) ) {
  5525. $orderby = "$wpdb->posts." . $orderby;
  5526. } else {
  5527. $orderby = "$wpdb->posts.post_" . $orderby;
  5528. }
  5529. }
  5530. $orderby_array[] = $orderby;
  5531. }
  5532. $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";
  5533. $sort_order = strtoupper( $parsed_args['sort_order'] );
  5534. if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) {
  5535. $sort_order = 'ASC';
  5536. }
  5537. $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
  5538. $query .= $author_query;
  5539. $query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order;
  5540. if ( ! empty( $number ) ) {
  5541. $query .= ' LIMIT ' . $offset . ',' . $number;
  5542. }
  5543. $pages = $wpdb->get_results( $query );
  5544. if ( empty( $pages ) ) {
  5545. wp_cache_set( $cache_key, array(), 'posts' );
  5546. /** This filter is documented in wp-includes/post.php */
  5547. $pages = apply_filters( 'get_pages', array(), $parsed_args );
  5548. return $pages;
  5549. }
  5550. // Sanitize before caching so it'll only get done once.
  5551. $num_pages = count( $pages );
  5552. for ( $i = 0; $i < $num_pages; $i++ ) {
  5553. $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' );
  5554. }
  5555. // Update cache.
  5556. update_post_cache( $pages );
  5557. if ( $child_of || $hierarchical ) {
  5558. $pages = get_page_children( $child_of, $pages );
  5559. }
  5560. if ( ! empty( $parsed_args['exclude_tree'] ) ) {
  5561. $exclude = wp_parse_id_list( $parsed_args['exclude_tree'] );
  5562. foreach ( $exclude as $id ) {
  5563. $children = get_page_children( $id, $pages );
  5564. foreach ( $children as $child ) {
  5565. $exclude[] = $child->ID;
  5566. }
  5567. }
  5568. $num_pages = count( $pages );
  5569. for ( $i = 0; $i < $num_pages; $i++ ) {
  5570. if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) {
  5571. unset( $pages[ $i ] );
  5572. }
  5573. }
  5574. }
  5575. $page_structure = array();
  5576. foreach ( $pages as $page ) {
  5577. $page_structure[] = $page->ID;
  5578. }
  5579. wp_cache_set( $cache_key, $page_structure, 'posts' );
  5580. // Convert to WP_Post instances.
  5581. $pages = array_map( 'get_post', $pages );
  5582. /**
  5583. * Filters the retrieved list of pages.
  5584. *
  5585. * @since 2.1.0
  5586. *
  5587. * @param WP_Post[] $pages Array of page objects.
  5588. * @param array $parsed_args Array of get_pages() arguments.
  5589. */
  5590. return apply_filters( 'get_pages', $pages, $parsed_args );
  5591. }
  5592. //
  5593. // Attachment functions.
  5594. //
  5595. /**
  5596. * Determines whether an attachment URI is local and really an attachment.
  5597. *
  5598. * For more information on this and similar theme functions, check out
  5599. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  5600. * Conditional Tags} article in the Theme Developer Handbook.
  5601. *
  5602. * @since 2.0.0
  5603. *
  5604. * @param string $url URL to check
  5605. * @return bool True on success, false on failure.
  5606. */
  5607. function is_local_attachment( $url ) {
  5608. if ( strpos( $url, home_url() ) === false ) {
  5609. return false;
  5610. }
  5611. if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) {
  5612. return true;
  5613. }
  5614. $id = url_to_postid( $url );
  5615. if ( $id ) {
  5616. $post = get_post( $id );
  5617. if ( 'attachment' === $post->post_type ) {
  5618. return true;
  5619. }
  5620. }
  5621. return false;
  5622. }
  5623. /**
  5624. * Inserts an attachment.
  5625. *
  5626. * If you set the 'ID' in the $args parameter, it will mean that you are
  5627. * updating and attempt to update the attachment. You can also set the
  5628. * attachment name or title by setting the key 'post_name' or 'post_title'.
  5629. *
  5630. * You can set the dates for the attachment manually by setting the 'post_date'
  5631. * and 'post_date_gmt' keys' values.
  5632. *
  5633. * By default, the comments will use the default settings for whether the
  5634. * comments are allowed. You can close them manually or keep them open by
  5635. * setting the value for the 'comment_status' key.
  5636. *
  5637. * @since 2.0.0
  5638. * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure.
  5639. * @since 5.6.0 Added the `$fire_after_hooks` parameter.
  5640. *
  5641. * @see wp_insert_post()
  5642. *
  5643. * @param string|array $args Arguments for inserting an attachment.
  5644. * @param string|false $file Optional. Filename.
  5645. * @param int $parent Optional. Parent post ID.
  5646. * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
  5647. * @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true.
  5648. * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
  5649. */
  5650. function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) {
  5651. $defaults = array(
  5652. 'file' => $file,
  5653. 'post_parent' => 0,
  5654. );
  5655. $data = wp_parse_args( $args, $defaults );
  5656. if ( ! empty( $parent ) ) {
  5657. $data['post_parent'] = $parent;
  5658. }
  5659. $data['post_type'] = 'attachment';
  5660. return wp_insert_post( $data, $wp_error, $fire_after_hooks );
  5661. }
  5662. /**
  5663. * Trashes or deletes an attachment.
  5664. *
  5665. * When an attachment is permanently deleted, the file will also be removed.
  5666. * Deletion removes all post meta fields, taxonomy, comments, etc. associated
  5667. * with the attachment (except the main post).
  5668. *
  5669. * The attachment is moved to the Trash instead of permanently deleted unless Trash
  5670. * for media is disabled, item is already in the Trash, or $force_delete is true.
  5671. *
  5672. * @since 2.0.0
  5673. *
  5674. * @global wpdb $wpdb WordPress database abstraction object.
  5675. *
  5676. * @param int $post_id Attachment ID.
  5677. * @param bool $force_delete Optional. Whether to bypass Trash and force deletion.
  5678. * Default false.
  5679. * @return WP_Post|false|null Post data on success, false or null on failure.
  5680. */
  5681. function wp_delete_attachment( $post_id, $force_delete = false ) {
  5682. global $wpdb;
  5683. $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) );
  5684. if ( ! $post ) {
  5685. return $post;
  5686. }
  5687. $post = get_post( $post );
  5688. if ( 'attachment' !== $post->post_type ) {
  5689. return false;
  5690. }
  5691. if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) {
  5692. return wp_trash_post( $post_id );
  5693. }
  5694. /**
  5695. * Filters whether an attachment deletion should take place.
  5696. *
  5697. * @since 5.5.0
  5698. *
  5699. * @param WP_Post|false|null $delete Whether to go forward with deletion.
  5700. * @param WP_Post $post Post object.
  5701. * @param bool $force_delete Whether to bypass the Trash.
  5702. */
  5703. $check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete );
  5704. if ( null !== $check ) {
  5705. return $check;
  5706. }
  5707. delete_post_meta( $post_id, '_wp_trash_meta_status' );
  5708. delete_post_meta( $post_id, '_wp_trash_meta_time' );
  5709. $meta = wp_get_attachment_metadata( $post_id );
  5710. $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true );
  5711. $file = get_attached_file( $post_id );
  5712. if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) {
  5713. clean_dirsize_cache( $file );
  5714. }
  5715. /**
  5716. * Fires before an attachment is deleted, at the start of wp_delete_attachment().
  5717. *
  5718. * @since 2.0.0
  5719. * @since 5.5.0 Added the `$post` parameter.
  5720. *
  5721. * @param int $post_id Attachment ID.
  5722. * @param WP_Post $post Post object.
  5723. */
  5724. do_action( 'delete_attachment', $post_id, $post );
  5725. wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) );
  5726. wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) );
  5727. // Delete all for any posts.
  5728. delete_metadata( 'post', null, '_thumbnail_id', $post_id, true );
  5729. wp_defer_comment_counting( true );
  5730. $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) );
  5731. foreach ( $comment_ids as $comment_id ) {
  5732. wp_delete_comment( $comment_id, true );
  5733. }
  5734. wp_defer_comment_counting( false );
  5735. $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) );
  5736. foreach ( $post_meta_ids as $mid ) {
  5737. delete_metadata_by_mid( 'post', $mid );
  5738. }
  5739. /** This action is documented in wp-includes/post.php */
  5740. do_action( 'delete_post', $post_id, $post );
  5741. $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) );
  5742. if ( ! $result ) {
  5743. return false;
  5744. }
  5745. /** This action is documented in wp-includes/post.php */
  5746. do_action( 'deleted_post', $post_id, $post );
  5747. wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file );
  5748. clean_post_cache( $post );
  5749. return $post;
  5750. }
  5751. /**
  5752. * Deletes all files that belong to the given attachment.
  5753. *
  5754. * @since 4.9.7
  5755. *
  5756. * @global wpdb $wpdb WordPress database abstraction object.
  5757. *
  5758. * @param int $post_id Attachment ID.
  5759. * @param array $meta The attachment's meta data.
  5760. * @param array $backup_sizes The meta data for the attachment's backup images.
  5761. * @param string $file Absolute path to the attachment's file.
  5762. * @return bool True on success, false on failure.
  5763. */
  5764. function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) {
  5765. global $wpdb;
  5766. $uploadpath = wp_get_upload_dir();
  5767. $deleted = true;
  5768. if ( ! empty( $meta['thumb'] ) ) {
  5769. // Don't delete the thumb if another attachment uses it.
  5770. if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) {
  5771. $thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file );
  5772. if ( ! empty( $thumbfile ) ) {
  5773. $thumbfile = path_join( $uploadpath['basedir'], $thumbfile );
  5774. $thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) );
  5775. if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) {
  5776. $deleted = false;
  5777. }
  5778. }
  5779. }
  5780. }
  5781. // Remove intermediate and backup images if there are any.
  5782. if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
  5783. $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
  5784. foreach ( $meta['sizes'] as $size => $sizeinfo ) {
  5785. $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
  5786. if ( ! empty( $intermediate_file ) ) {
  5787. $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
  5788. if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
  5789. $deleted = false;
  5790. }
  5791. }
  5792. }
  5793. }
  5794. if ( ! empty( $meta['original_image'] ) ) {
  5795. if ( empty( $intermediate_dir ) ) {
  5796. $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) );
  5797. }
  5798. $original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file );
  5799. if ( ! empty( $original_image ) ) {
  5800. $original_image = path_join( $uploadpath['basedir'], $original_image );
  5801. if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) {
  5802. $deleted = false;
  5803. }
  5804. }
  5805. }
  5806. if ( is_array( $backup_sizes ) ) {
  5807. $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
  5808. foreach ( $backup_sizes as $size ) {
  5809. $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
  5810. if ( ! empty( $del_file ) ) {
  5811. $del_file = path_join( $uploadpath['basedir'], $del_file );
  5812. if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
  5813. $deleted = false;
  5814. }
  5815. }
  5816. }
  5817. }
  5818. if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
  5819. $deleted = false;
  5820. }
  5821. return $deleted;
  5822. }
  5823. /**
  5824. * Retrieves attachment metadata for attachment ID.
  5825. *
  5826. * @since 2.1.0
  5827. * @since 6.0.0 The `$filesize` value was added to the returned array.
  5828. *
  5829. * @param int $attachment_id Attachment post ID. Defaults to global $post.
  5830. * @param bool $unfiltered Optional. If true, filters are not run. Default false.
  5831. * @return array|false {
  5832. * Attachment metadata. False on failure.
  5833. *
  5834. * @type int $width The width of the attachment.
  5835. * @type int $height The height of the attachment.
  5836. * @type string $file The file path relative to `wp-content/uploads`.
  5837. * @type array $sizes Keys are size slugs, each value is an array containing
  5838. * 'file', 'width', 'height', and 'mime-type'.
  5839. * @type array $image_meta Image metadata.
  5840. * @type int $filesize File size of the attachment.
  5841. * }
  5842. */
  5843. function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) {
  5844. $attachment_id = (int) $attachment_id;
  5845. if ( ! $attachment_id ) {
  5846. $post = get_post();
  5847. if ( ! $post ) {
  5848. return false;
  5849. }
  5850. $attachment_id = $post->ID;
  5851. }
  5852. $data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
  5853. if ( ! $data ) {
  5854. return false;
  5855. }
  5856. if ( $unfiltered ) {
  5857. return $data;
  5858. }
  5859. /**
  5860. * Filters the attachment meta data.
  5861. *
  5862. * @since 2.1.0
  5863. *
  5864. * @param array $data Array of meta data for the given attachment.
  5865. * @param int $attachment_id Attachment post ID.
  5866. */
  5867. return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id );
  5868. }
  5869. /**
  5870. * Updates metadata for an attachment.
  5871. *
  5872. * @since 2.1.0
  5873. *
  5874. * @param int $attachment_id Attachment post ID.
  5875. * @param array $data Attachment meta data.
  5876. * @return int|false False if $post is invalid.
  5877. */
  5878. function wp_update_attachment_metadata( $attachment_id, $data ) {
  5879. $attachment_id = (int) $attachment_id;
  5880. $post = get_post( $attachment_id );
  5881. if ( ! $post ) {
  5882. return false;
  5883. }
  5884. /**
  5885. * Filters the updated attachment meta data.
  5886. *
  5887. * @since 2.1.0
  5888. *
  5889. * @param array $data Array of updated attachment meta data.
  5890. * @param int $attachment_id Attachment post ID.
  5891. */
  5892. $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
  5893. if ( $data ) {
  5894. return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
  5895. } else {
  5896. return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
  5897. }
  5898. }
  5899. /**
  5900. * Retrieves the URL for an attachment.
  5901. *
  5902. * @since 2.1.0
  5903. *
  5904. * @global string $pagenow The filename of the current screen.
  5905. *
  5906. * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post.
  5907. * @return string|false Attachment URL, otherwise false.
  5908. */
  5909. function wp_get_attachment_url( $attachment_id = 0 ) {
  5910. global $pagenow;
  5911. $attachment_id = (int) $attachment_id;
  5912. $post = get_post( $attachment_id );
  5913. if ( ! $post ) {
  5914. return false;
  5915. }
  5916. if ( 'attachment' !== $post->post_type ) {
  5917. return false;
  5918. }
  5919. $url = '';
  5920. // Get attached file.
  5921. $file = get_post_meta( $post->ID, '_wp_attached_file', true );
  5922. if ( $file ) {
  5923. // Get upload directory.
  5924. $uploads = wp_get_upload_dir();
  5925. if ( $uploads && false === $uploads['error'] ) {
  5926. // Check that the upload base exists in the file location.
  5927. if ( 0 === strpos( $file, $uploads['basedir'] ) ) {
  5928. // Replace file location with url location.
  5929. $url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file );
  5930. } elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) {
  5931. // Get the directory name relative to the basedir (back compat for pre-2.7 uploads).
  5932. $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file );
  5933. } else {
  5934. // It's a newly-uploaded file, therefore $file is relative to the basedir.
  5935. $url = $uploads['baseurl'] . "/$file";
  5936. }
  5937. }
  5938. }
  5939. /*
  5940. * If any of the above options failed, Fallback on the GUID as used pre-2.7,
  5941. * not recommended to rely upon this.
  5942. */
  5943. if ( ! $url ) {
  5944. $url = get_the_guid( $post->ID );
  5945. }
  5946. // On SSL front end, URLs should be HTTPS.
  5947. if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) {
  5948. $url = set_url_scheme( $url );
  5949. }
  5950. /**
  5951. * Filters the attachment URL.
  5952. *
  5953. * @since 2.1.0
  5954. *
  5955. * @param string $url URL for the given attachment.
  5956. * @param int $attachment_id Attachment post ID.
  5957. */
  5958. $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
  5959. if ( ! $url ) {
  5960. return false;
  5961. }
  5962. return $url;
  5963. }
  5964. /**
  5965. * Retrieves the caption for an attachment.
  5966. *
  5967. * @since 4.6.0
  5968. *
  5969. * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
  5970. * @return string|false Attachment caption on success, false on failure.
  5971. */
  5972. function wp_get_attachment_caption( $post_id = 0 ) {
  5973. $post_id = (int) $post_id;
  5974. $post = get_post( $post_id );
  5975. if ( ! $post ) {
  5976. return false;
  5977. }
  5978. if ( 'attachment' !== $post->post_type ) {
  5979. return false;
  5980. }
  5981. $caption = $post->post_excerpt;
  5982. /**
  5983. * Filters the attachment caption.
  5984. *
  5985. * @since 4.6.0
  5986. *
  5987. * @param string $caption Caption for the given attachment.
  5988. * @param int $post_id Attachment ID.
  5989. */
  5990. return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID );
  5991. }
  5992. /**
  5993. * Retrieves URL for an attachment thumbnail.
  5994. *
  5995. * @since 2.1.0
  5996. * @since 6.1.0 Changed to use wp_get_attachment_image_url().
  5997. *
  5998. * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`.
  5999. * @return string|false Thumbnail URL on success, false on failure.
  6000. */
  6001. function wp_get_attachment_thumb_url( $post_id = 0 ) {
  6002. $post_id = (int) $post_id;
  6003. // This uses image_downsize() which also looks for the (very) old format $image_meta['thumb']
  6004. // when the newer format $image_meta['sizes']['thumbnail'] doesn't exist.
  6005. $thumbnail_url = wp_get_attachment_image_url( $post_id, 'thumbnail' );
  6006. if ( empty( $thumbnail_url ) ) {
  6007. return false;
  6008. }
  6009. /**
  6010. * Filters the attachment thumbnail URL.
  6011. *
  6012. * @since 2.1.0
  6013. *
  6014. * @param string $thumbnail_url URL for the attachment thumbnail.
  6015. * @param int $post_id Attachment ID.
  6016. */
  6017. return apply_filters( 'wp_get_attachment_thumb_url', $thumbnail_url, $post_id );
  6018. }
  6019. /**
  6020. * Verifies an attachment is of a given type.
  6021. *
  6022. * @since 4.2.0
  6023. *
  6024. * @param string $type Attachment type. Accepts 'image', 'audio', or 'video'.
  6025. * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
  6026. * @return bool True if one of the accepted types, false otherwise.
  6027. */
  6028. function wp_attachment_is( $type, $post = null ) {
  6029. $post = get_post( $post );
  6030. if ( ! $post ) {
  6031. return false;
  6032. }
  6033. $file = get_attached_file( $post->ID );
  6034. if ( ! $file ) {
  6035. return false;
  6036. }
  6037. if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) {
  6038. return true;
  6039. }
  6040. $check = wp_check_filetype( $file );
  6041. if ( empty( $check['ext'] ) ) {
  6042. return false;
  6043. }
  6044. $ext = $check['ext'];
  6045. if ( 'import' !== $post->post_mime_type ) {
  6046. return $type === $ext;
  6047. }
  6048. switch ( $type ) {
  6049. case 'image':
  6050. $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' );
  6051. return in_array( $ext, $image_exts, true );
  6052. case 'audio':
  6053. return in_array( $ext, wp_get_audio_extensions(), true );
  6054. case 'video':
  6055. return in_array( $ext, wp_get_video_extensions(), true );
  6056. default:
  6057. return $type === $ext;
  6058. }
  6059. }
  6060. /**
  6061. * Determines whether an attachment is an image.
  6062. *
  6063. * For more information on this and similar theme functions, check out
  6064. * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  6065. * Conditional Tags} article in the Theme Developer Handbook.
  6066. *
  6067. * @since 2.1.0
  6068. * @since 4.2.0 Modified into wrapper for wp_attachment_is() and
  6069. * allowed WP_Post object to be passed.
  6070. *
  6071. * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post.
  6072. * @return bool Whether the attachment is an image.
  6073. */
  6074. function wp_attachment_is_image( $post = null ) {
  6075. return wp_attachment_is( 'image', $post );
  6076. }
  6077. /**
  6078. * Retrieves the icon for a MIME type or attachment.
  6079. *
  6080. * @since 2.1.0
  6081. *
  6082. * @param string|int $mime MIME type or attachment ID.
  6083. * @return string|false Icon, false otherwise.
  6084. */
  6085. function wp_mime_type_icon( $mime = 0 ) {
  6086. if ( ! is_numeric( $mime ) ) {
  6087. $icon = wp_cache_get( "mime_type_icon_$mime" );
  6088. }
  6089. $post_id = 0;
  6090. if ( empty( $icon ) ) {
  6091. $post_mimes = array();
  6092. if ( is_numeric( $mime ) ) {
  6093. $mime = (int) $mime;
  6094. $post = get_post( $mime );
  6095. if ( $post ) {
  6096. $post_id = (int) $post->ID;
  6097. $file = get_attached_file( $post_id );
  6098. $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file );
  6099. if ( ! empty( $ext ) ) {
  6100. $post_mimes[] = $ext;
  6101. $ext_type = wp_ext2type( $ext );
  6102. if ( $ext_type ) {
  6103. $post_mimes[] = $ext_type;
  6104. }
  6105. }
  6106. $mime = $post->post_mime_type;
  6107. } else {
  6108. $mime = 0;
  6109. }
  6110. } else {
  6111. $post_mimes[] = $mime;
  6112. }
  6113. $icon_files = wp_cache_get( 'icon_files' );
  6114. if ( ! is_array( $icon_files ) ) {
  6115. /**
  6116. * Filters the icon directory path.
  6117. *
  6118. * @since 2.0.0
  6119. *
  6120. * @param string $path Icon directory absolute path.
  6121. */
  6122. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
  6123. /**
  6124. * Filters the icon directory URI.
  6125. *
  6126. * @since 2.0.0
  6127. *
  6128. * @param string $uri Icon directory URI.
  6129. */
  6130. $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) );
  6131. /**
  6132. * Filters the array of icon directory URIs.
  6133. *
  6134. * @since 2.5.0
  6135. *
  6136. * @param string[] $uris Array of icon directory URIs keyed by directory absolute path.
  6137. */
  6138. $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) );
  6139. $icon_files = array();
  6140. while ( $dirs ) {
  6141. $keys = array_keys( $dirs );
  6142. $dir = array_shift( $keys );
  6143. $uri = array_shift( $dirs );
  6144. $dh = opendir( $dir );
  6145. if ( $dh ) {
  6146. while ( false !== $file = readdir( $dh ) ) {
  6147. $file = wp_basename( $file );
  6148. if ( '.' === substr( $file, 0, 1 ) ) {
  6149. continue;
  6150. }
  6151. $ext = strtolower( substr( $file, -4 ) );
  6152. if ( ! in_array( $ext, array( '.png', '.gif', '.jpg' ), true ) ) {
  6153. if ( is_dir( "$dir/$file" ) ) {
  6154. $dirs[ "$dir/$file" ] = "$uri/$file";
  6155. }
  6156. continue;
  6157. }
  6158. $icon_files[ "$dir/$file" ] = "$uri/$file";
  6159. }
  6160. closedir( $dh );
  6161. }
  6162. }
  6163. wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
  6164. }
  6165. $types = array();
  6166. // Icon wp_basename - extension = MIME wildcard.
  6167. foreach ( $icon_files as $file => $uri ) {
  6168. $types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ];
  6169. }
  6170. if ( ! empty( $mime ) ) {
  6171. $post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) );
  6172. $post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 );
  6173. $post_mimes[] = str_replace( '/', '_', $mime );
  6174. }
  6175. $matches = wp_match_mime_types( array_keys( $types ), $post_mimes );
  6176. $matches['default'] = array( 'default' );
  6177. foreach ( $matches as $match => $wilds ) {
  6178. foreach ( $wilds as $wild ) {
  6179. if ( ! isset( $types[ $wild ] ) ) {
  6180. continue;
  6181. }
  6182. $icon = $types[ $wild ];
  6183. if ( ! is_numeric( $mime ) ) {
  6184. wp_cache_add( "mime_type_icon_$mime", $icon );
  6185. }
  6186. break 2;
  6187. }
  6188. }
  6189. }
  6190. /**
  6191. * Filters the mime type icon.
  6192. *
  6193. * @since 2.1.0
  6194. *
  6195. * @param string $icon Path to the mime type icon.
  6196. * @param string $mime Mime type.
  6197. * @param int $post_id Attachment ID. Will equal 0 if the function passed
  6198. * the mime type.
  6199. */
  6200. return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id );
  6201. }
  6202. /**
  6203. * Checks for changed slugs for published post objects and save the old slug.
  6204. *
  6205. * The function is used when a post object of any type is updated,
  6206. * by comparing the current and previous post objects.
  6207. *
  6208. * If the slug was changed and not already part of the old slugs then it will be
  6209. * added to the post meta field ('_wp_old_slug') for storing old slugs for that
  6210. * post.
  6211. *
  6212. * The most logically usage of this function is redirecting changed post objects, so
  6213. * that those that linked to an changed post will be redirected to the new post.
  6214. *
  6215. * @since 2.1.0
  6216. *
  6217. * @param int $post_id Post ID.
  6218. * @param WP_Post $post The post object.
  6219. * @param WP_Post $post_before The previous post object.
  6220. */
  6221. function wp_check_for_changed_slugs( $post_id, $post, $post_before ) {
  6222. // Don't bother if it hasn't changed.
  6223. if ( $post->post_name == $post_before->post_name ) {
  6224. return;
  6225. }
  6226. // We're only concerned with published, non-hierarchical objects.
  6227. if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
  6228. return;
  6229. }
  6230. $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' );
  6231. // If we haven't added this old slug before, add it now.
  6232. if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) {
  6233. add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name );
  6234. }
  6235. // If the new slug was used previously, delete it from the list.
  6236. if ( in_array( $post->post_name, $old_slugs, true ) ) {
  6237. delete_post_meta( $post_id, '_wp_old_slug', $post->post_name );
  6238. }
  6239. }
  6240. /**
  6241. * Checks for changed dates for published post objects and save the old date.
  6242. *
  6243. * The function is used when a post object of any type is updated,
  6244. * by comparing the current and previous post objects.
  6245. *
  6246. * If the date was changed and not already part of the old dates then it will be
  6247. * added to the post meta field ('_wp_old_date') for storing old dates for that
  6248. * post.
  6249. *
  6250. * The most logically usage of this function is redirecting changed post objects, so
  6251. * that those that linked to an changed post will be redirected to the new post.
  6252. *
  6253. * @since 4.9.3
  6254. *
  6255. * @param int $post_id Post ID.
  6256. * @param WP_Post $post The post object.
  6257. * @param WP_Post $post_before The previous post object.
  6258. */
  6259. function wp_check_for_changed_dates( $post_id, $post, $post_before ) {
  6260. $previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) );
  6261. $new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) );
  6262. // Don't bother if it hasn't changed.
  6263. if ( $new_date == $previous_date ) {
  6264. return;
  6265. }
  6266. // We're only concerned with published, non-hierarchical objects.
  6267. if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) {
  6268. return;
  6269. }
  6270. $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' );
  6271. // If we haven't added this old date before, add it now.
  6272. if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) {
  6273. add_post_meta( $post_id, '_wp_old_date', $previous_date );
  6274. }
  6275. // If the new slug was used previously, delete it from the list.
  6276. if ( in_array( $new_date, $old_dates, true ) ) {
  6277. delete_post_meta( $post_id, '_wp_old_date', $new_date );
  6278. }
  6279. }
  6280. /**
  6281. * Retrieves the private post SQL based on capability.
  6282. *
  6283. * This function provides a standardized way to appropriately select on the
  6284. * post_status of a post type. The function will return a piece of SQL code
  6285. * that can be added to a WHERE clause; this SQL is constructed to allow all
  6286. * published posts, and all private posts to which the user has access.
  6287. *
  6288. * @since 2.2.0
  6289. * @since 4.3.0 Added the ability to pass an array to `$post_type`.
  6290. *
  6291. * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'.
  6292. * @return string SQL code that can be added to a where clause.
  6293. */
  6294. function get_private_posts_cap_sql( $post_type ) {
  6295. return get_posts_by_author_sql( $post_type, false );
  6296. }
  6297. /**
  6298. * Retrieves the post SQL based on capability, author, and type.
  6299. *
  6300. * @since 3.0.0
  6301. * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`.
  6302. *
  6303. * @see get_private_posts_cap_sql()
  6304. * @global wpdb $wpdb WordPress database abstraction object.
  6305. *
  6306. * @param string|string[] $post_type Single post type or an array of post types.
  6307. * @param bool $full Optional. Returns a full WHERE statement instead of just
  6308. * an 'andalso' term. Default true.
  6309. * @param int $post_author Optional. Query posts having a single author ID. Default null.
  6310. * @param bool $public_only Optional. Only return public posts. Skips cap checks for
  6311. * $current_user. Default false.
  6312. * @return string SQL WHERE code that can be added to a query.
  6313. */
  6314. function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
  6315. global $wpdb;
  6316. if ( is_array( $post_type ) ) {
  6317. $post_types = $post_type;
  6318. } else {
  6319. $post_types = array( $post_type );
  6320. }
  6321. $post_type_clauses = array();
  6322. foreach ( $post_types as $post_type ) {
  6323. $post_type_obj = get_post_type_object( $post_type );
  6324. if ( ! $post_type_obj ) {
  6325. continue;
  6326. }
  6327. /**
  6328. * Filters the capability to read private posts for a custom post type
  6329. * when generating SQL for getting posts by author.
  6330. *
  6331. * @since 2.2.0
  6332. * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless".
  6333. *
  6334. * @param string $cap Capability.
  6335. */
  6336. $cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' );
  6337. if ( ! $cap ) {
  6338. $cap = current_user_can( $post_type_obj->cap->read_private_posts );
  6339. }
  6340. // Only need to check the cap if $public_only is false.
  6341. $post_status_sql = "post_status = 'publish'";
  6342. if ( false === $public_only ) {
  6343. if ( $cap ) {
  6344. // Does the user have the capability to view private posts? Guess so.
  6345. $post_status_sql .= " OR post_status = 'private'";
  6346. } elseif ( is_user_logged_in() ) {
  6347. // Users can view their own private posts.
  6348. $id = get_current_user_id();
  6349. if ( null === $post_author || ! $full ) {
  6350. $post_status_sql .= " OR post_status = 'private' AND post_author = $id";
  6351. } elseif ( $id == (int) $post_author ) {
  6352. $post_status_sql .= " OR post_status = 'private'";
  6353. } // Else none.
  6354. } // Else none.
  6355. }
  6356. $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )";
  6357. }
  6358. if ( empty( $post_type_clauses ) ) {
  6359. return $full ? 'WHERE 1 = 0' : '1 = 0';
  6360. }
  6361. $sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )';
  6362. if ( null !== $post_author ) {
  6363. $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author );
  6364. }
  6365. if ( $full ) {
  6366. $sql = 'WHERE ' . $sql;
  6367. }
  6368. return $sql;
  6369. }
  6370. /**
  6371. * Retrieves the most recent time that a post on the site was published.
  6372. *
  6373. * The server timezone is the default and is the difference between GMT and
  6374. * server time. The 'blog' value is the date when the last post was posted.
  6375. * The 'gmt' is when the last post was posted in GMT formatted date.
  6376. *
  6377. * @since 0.71
  6378. * @since 4.4.0 The `$post_type` argument was added.
  6379. *
  6380. * @param string $timezone Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'.
  6381. * 'server' uses the server's internal timezone.
  6382. * 'blog' uses the `post_date` field, which proxies to the timezone set for the site.
  6383. * 'gmt' uses the `post_date_gmt` field.
  6384. * Default 'server'.
  6385. * @param string $post_type Optional. The post type to check. Default 'any'.
  6386. * @return string The date of the last post, or false on failure.
  6387. */
  6388. function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) {
  6389. $lastpostdate = _get_last_post_time( $timezone, 'date', $post_type );
  6390. /**
  6391. * Filters the most recent time that a post on the site was published.
  6392. *
  6393. * @since 2.3.0
  6394. * @since 5.5.0 Added the `$post_type` parameter.
  6395. *
  6396. * @param string|false $lastpostdate The most recent time that a post was published,
  6397. * in 'Y-m-d H:i:s' format. False on failure.
  6398. * @param string $timezone Location to use for getting the post published date.
  6399. * See get_lastpostdate() for accepted `$timezone` values.
  6400. * @param string $post_type The post type to check.
  6401. */
  6402. return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type );
  6403. }
  6404. /**
  6405. * Gets the most recent time that a post on the site was modified.
  6406. *
  6407. * The server timezone is the default and is the difference between GMT and
  6408. * server time. The 'blog' value is just when the last post was modified.
  6409. * The 'gmt' is when the last post was modified in GMT time.
  6410. *
  6411. * @since 1.2.0
  6412. * @since 4.4.0 The `$post_type` argument was added.
  6413. *
  6414. * @param string $timezone Optional. The timezone for the timestamp. See get_lastpostdate()
  6415. * for information on accepted values.
  6416. * Default 'server'.
  6417. * @param string $post_type Optional. The post type to check. Default 'any'.
  6418. * @return string The timestamp in 'Y-m-d H:i:s' format, or false on failure.
  6419. */
  6420. function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) {
  6421. /**
  6422. * Pre-filter the return value of get_lastpostmodified() before the query is run.
  6423. *
  6424. * @since 4.4.0
  6425. *
  6426. * @param string|false $lastpostmodified The most recent time that a post was modified,
  6427. * in 'Y-m-d H:i:s' format, or false. Returning anything
  6428. * other than false will short-circuit the function.
  6429. * @param string $timezone Location to use for getting the post modified date.
  6430. * See get_lastpostdate() for accepted `$timezone` values.
  6431. * @param string $post_type The post type to check.
  6432. */
  6433. $lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type );
  6434. if ( false !== $lastpostmodified ) {
  6435. return $lastpostmodified;
  6436. }
  6437. $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type );
  6438. $lastpostdate = get_lastpostdate( $timezone, $post_type );
  6439. if ( $lastpostdate > $lastpostmodified ) {
  6440. $lastpostmodified = $lastpostdate;
  6441. }
  6442. /**
  6443. * Filters the most recent time that a post on the site was modified.
  6444. *
  6445. * @since 2.3.0
  6446. * @since 5.5.0 Added the `$post_type` parameter.
  6447. *
  6448. * @param string|false $lastpostmodified The most recent time that a post was modified,
  6449. * in 'Y-m-d H:i:s' format. False on failure.
  6450. * @param string $timezone Location to use for getting the post modified date.
  6451. * See get_lastpostdate() for accepted `$timezone` values.
  6452. * @param string $post_type The post type to check.
  6453. */
  6454. return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone, $post_type );
  6455. }
  6456. /**
  6457. * Gets the timestamp of the last time any post was modified or published.
  6458. *
  6459. * @since 3.1.0
  6460. * @since 4.4.0 The `$post_type` argument was added.
  6461. * @access private
  6462. *
  6463. * @global wpdb $wpdb WordPress database abstraction object.
  6464. *
  6465. * @param string $timezone The timezone for the timestamp. See get_lastpostdate().
  6466. * for information on accepted values.
  6467. * @param string $field Post field to check. Accepts 'date' or 'modified'.
  6468. * @param string $post_type Optional. The post type to check. Default 'any'.
  6469. * @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure.
  6470. */
  6471. function _get_last_post_time( $timezone, $field, $post_type = 'any' ) {
  6472. global $wpdb;
  6473. if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) {
  6474. return false;
  6475. }
  6476. $timezone = strtolower( $timezone );
  6477. $key = "lastpost{$field}:$timezone";
  6478. if ( 'any' !== $post_type ) {
  6479. $key .= ':' . sanitize_key( $post_type );
  6480. }
  6481. $date = wp_cache_get( $key, 'timeinfo' );
  6482. if ( false !== $date ) {
  6483. return $date;
  6484. }
  6485. if ( 'any' === $post_type ) {
  6486. $post_types = get_post_types( array( 'public' => true ) );
  6487. array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) );
  6488. $post_types = "'" . implode( "', '", $post_types ) . "'";
  6489. } else {
  6490. $post_types = "'" . sanitize_key( $post_type ) . "'";
  6491. }
  6492. switch ( $timezone ) {
  6493. case 'gmt':
  6494. $date = $wpdb->get_var( "SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" );
  6495. break;
  6496. case 'blog':
  6497. $date = $wpdb->get_var( "SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" );
  6498. break;
  6499. case 'server':
  6500. $add_seconds_server = gmdate( 'Z' );
  6501. $date = $wpdb->get_var( "SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" );
  6502. break;
  6503. }
  6504. if ( $date ) {
  6505. wp_cache_set( $key, $date, 'timeinfo' );
  6506. return $date;
  6507. }
  6508. return false;
  6509. }
  6510. /**
  6511. * Updates posts in cache.
  6512. *
  6513. * @since 1.5.1
  6514. *
  6515. * @param WP_Post[] $posts Array of post objects (passed by reference).
  6516. */
  6517. function update_post_cache( &$posts ) {
  6518. if ( ! $posts ) {
  6519. return;
  6520. }
  6521. $data = array();
  6522. foreach ( $posts as $post ) {
  6523. if ( empty( $post->filter ) || 'raw' !== $post->filter ) {
  6524. $post = sanitize_post( $post, 'raw' );
  6525. }
  6526. $data[ $post->ID ] = $post;
  6527. }
  6528. wp_cache_add_multiple( $data, 'posts' );
  6529. }
  6530. /**
  6531. * Will clean the post in the cache.
  6532. *
  6533. * Cleaning means delete from the cache of the post. Will call to clean the term
  6534. * object cache associated with the post ID.
  6535. *
  6536. * This function not run if $_wp_suspend_cache_invalidation is not empty. See
  6537. * wp_suspend_cache_invalidation().
  6538. *
  6539. * @since 2.0.0
  6540. *
  6541. * @global bool $_wp_suspend_cache_invalidation
  6542. *
  6543. * @param int|WP_Post $post Post ID or post object to remove from the cache.
  6544. */
  6545. function clean_post_cache( $post ) {
  6546. global $_wp_suspend_cache_invalidation;
  6547. if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  6548. return;
  6549. }
  6550. $post = get_post( $post );
  6551. if ( ! $post ) {
  6552. return;
  6553. }
  6554. wp_cache_delete( $post->ID, 'posts' );
  6555. wp_cache_delete( $post->ID, 'post_meta' );
  6556. clean_object_term_cache( $post->ID, $post->post_type );
  6557. wp_cache_delete( 'wp_get_archives', 'general' );
  6558. /**
  6559. * Fires immediately after the given post's cache is cleaned.
  6560. *
  6561. * @since 2.5.0
  6562. *
  6563. * @param int $post_id Post ID.
  6564. * @param WP_Post $post Post object.
  6565. */
  6566. do_action( 'clean_post_cache', $post->ID, $post );
  6567. if ( 'page' === $post->post_type ) {
  6568. wp_cache_delete( 'all_page_ids', 'posts' );
  6569. /**
  6570. * Fires immediately after the given page's cache is cleaned.
  6571. *
  6572. * @since 2.5.0
  6573. *
  6574. * @param int $post_id Post ID.
  6575. */
  6576. do_action( 'clean_page_cache', $post->ID );
  6577. }
  6578. wp_cache_set( 'last_changed', microtime(), 'posts' );
  6579. }
  6580. /**
  6581. * Updates post, term, and metadata caches for a list of post objects.
  6582. *
  6583. * @since 1.5.0
  6584. *
  6585. * @param WP_Post[] $posts Array of post objects (passed by reference).
  6586. * @param string $post_type Optional. Post type. Default 'post'.
  6587. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true.
  6588. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true.
  6589. */
  6590. function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) {
  6591. // No point in doing all this work if we didn't match any posts.
  6592. if ( ! $posts ) {
  6593. return;
  6594. }
  6595. update_post_cache( $posts );
  6596. $post_ids = array();
  6597. foreach ( $posts as $post ) {
  6598. $post_ids[] = $post->ID;
  6599. }
  6600. if ( ! $post_type ) {
  6601. $post_type = 'any';
  6602. }
  6603. if ( $update_term_cache ) {
  6604. if ( is_array( $post_type ) ) {
  6605. $ptypes = $post_type;
  6606. } elseif ( 'any' === $post_type ) {
  6607. $ptypes = array();
  6608. // Just use the post_types in the supplied posts.
  6609. foreach ( $posts as $post ) {
  6610. $ptypes[] = $post->post_type;
  6611. }
  6612. $ptypes = array_unique( $ptypes );
  6613. } else {
  6614. $ptypes = array( $post_type );
  6615. }
  6616. if ( ! empty( $ptypes ) ) {
  6617. update_object_term_cache( $post_ids, $ptypes );
  6618. }
  6619. }
  6620. if ( $update_meta_cache ) {
  6621. update_postmeta_cache( $post_ids );
  6622. }
  6623. }
  6624. /**
  6625. * Updates post author user caches for a list of post objects.
  6626. *
  6627. * @since 6.1.0
  6628. *
  6629. * @param WP_Post[] $posts Array of post objects.
  6630. */
  6631. function update_post_author_caches( $posts ) {
  6632. /*
  6633. * cache_users() is a pluggable function so is not available prior
  6634. * to the `plugins_loaded` hook firing. This is to ensure against
  6635. * fatal errors when the function is not available.
  6636. */
  6637. if ( ! function_exists( 'cache_users' ) ) {
  6638. return;
  6639. }
  6640. $author_ids = wp_list_pluck( $posts, 'post_author' );
  6641. $author_ids = array_map( 'absint', $author_ids );
  6642. $author_ids = array_unique( array_filter( $author_ids ) );
  6643. cache_users( $author_ids );
  6644. }
  6645. /**
  6646. * Updates parent post caches for a list of post objects.
  6647. *
  6648. * @since 6.1.0
  6649. *
  6650. * @param WP_Post[] $posts Array of post objects.
  6651. */
  6652. function update_post_parent_caches( $posts ) {
  6653. $parent_ids = wp_list_pluck( $posts, 'post_parent' );
  6654. $parent_ids = array_map( 'absint', $parent_ids );
  6655. $parent_ids = array_unique( array_filter( $parent_ids ) );
  6656. if ( ! empty( $parent_ids ) ) {
  6657. _prime_post_caches( $parent_ids, false );
  6658. }
  6659. }
  6660. /**
  6661. * Updates metadata cache for a list of post IDs.
  6662. *
  6663. * Performs SQL query to retrieve the metadata for the post IDs and updates the
  6664. * metadata cache for the posts. Therefore, the functions, which call this
  6665. * function, do not need to perform SQL queries on their own.
  6666. *
  6667. * @since 2.1.0
  6668. *
  6669. * @param int[] $post_ids Array of post IDs.
  6670. * @return array|false An array of metadata on success, false if there is nothing to update.
  6671. */
  6672. function update_postmeta_cache( $post_ids ) {
  6673. return update_meta_cache( 'post', $post_ids );
  6674. }
  6675. /**
  6676. * Will clean the attachment in the cache.
  6677. *
  6678. * Cleaning means delete from the cache. Optionally will clean the term
  6679. * object cache associated with the attachment ID.
  6680. *
  6681. * This function will not run if $_wp_suspend_cache_invalidation is not empty.
  6682. *
  6683. * @since 3.0.0
  6684. *
  6685. * @global bool $_wp_suspend_cache_invalidation
  6686. *
  6687. * @param int $id The attachment ID in the cache to clean.
  6688. * @param bool $clean_terms Optional. Whether to clean terms cache. Default false.
  6689. */
  6690. function clean_attachment_cache( $id, $clean_terms = false ) {
  6691. global $_wp_suspend_cache_invalidation;
  6692. if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
  6693. return;
  6694. }
  6695. $id = (int) $id;
  6696. wp_cache_delete( $id, 'posts' );
  6697. wp_cache_delete( $id, 'post_meta' );
  6698. if ( $clean_terms ) {
  6699. clean_object_term_cache( $id, 'attachment' );
  6700. }
  6701. /**
  6702. * Fires after the given attachment's cache is cleaned.
  6703. *
  6704. * @since 3.0.0
  6705. *
  6706. * @param int $id Attachment ID.
  6707. */
  6708. do_action( 'clean_attachment_cache', $id );
  6709. }
  6710. //
  6711. // Hooks.
  6712. //
  6713. /**
  6714. * Hook for managing future post transitions to published.
  6715. *
  6716. * @since 2.3.0
  6717. * @access private
  6718. *
  6719. * @see wp_clear_scheduled_hook()
  6720. * @global wpdb $wpdb WordPress database abstraction object.
  6721. *
  6722. * @param string $new_status New post status.
  6723. * @param string $old_status Previous post status.
  6724. * @param WP_Post $post Post object.
  6725. */
  6726. function _transition_post_status( $new_status, $old_status, $post ) {
  6727. global $wpdb;
  6728. if ( 'publish' !== $old_status && 'publish' === $new_status ) {
  6729. // Reset GUID if transitioning to publish and it is empty.
  6730. if ( '' === get_the_guid( $post->ID ) ) {
  6731. $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
  6732. }
  6733. /**
  6734. * Fires when a post's status is transitioned from private to published.
  6735. *
  6736. * @since 1.5.0
  6737. * @deprecated 2.3.0 Use {@see 'private_to_publish'} instead.
  6738. *
  6739. * @param int $post_id Post ID.
  6740. */
  6741. do_action_deprecated( 'private_to_published', array( $post->ID ), '2.3.0', 'private_to_publish' );
  6742. }
  6743. // If published posts changed clear the lastpostmodified cache.
  6744. if ( 'publish' === $new_status || 'publish' === $old_status ) {
  6745. foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
  6746. wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
  6747. wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
  6748. wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' );
  6749. }
  6750. }
  6751. if ( $new_status !== $old_status ) {
  6752. wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
  6753. wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
  6754. }
  6755. // Always clears the hook in case the post status bounced from future to draft.
  6756. wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
  6757. }
  6758. /**
  6759. * Hook used to schedule publication for a post marked for the future.
  6760. *
  6761. * The $post properties used and must exist are 'ID' and 'post_date_gmt'.
  6762. *
  6763. * @since 2.3.0
  6764. * @access private
  6765. *
  6766. * @param int $deprecated Not used. Can be set to null. Never implemented. Not marked
  6767. * as deprecated with _deprecated_argument() as it conflicts with
  6768. * wp_transition_post_status() and the default filter for _future_post_hook().
  6769. * @param WP_Post $post Post object.
  6770. */
  6771. function _future_post_hook( $deprecated, $post ) {
  6772. wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
  6773. wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) );
  6774. }
  6775. /**
  6776. * Hook to schedule pings and enclosures when a post is published.
  6777. *
  6778. * Uses XMLRPC_REQUEST and WP_IMPORTING constants.
  6779. *
  6780. * @since 2.3.0
  6781. * @access private
  6782. *
  6783. * @param int $post_id The ID of the post being published.
  6784. */
  6785. function _publish_post_hook( $post_id ) {
  6786. if ( defined( 'XMLRPC_REQUEST' ) ) {
  6787. /**
  6788. * Fires when _publish_post_hook() is called during an XML-RPC request.
  6789. *
  6790. * @since 2.1.0
  6791. *
  6792. * @param int $post_id Post ID.
  6793. */
  6794. do_action( 'xmlrpc_publish_post', $post_id );
  6795. }
  6796. if ( defined( 'WP_IMPORTING' ) ) {
  6797. return;
  6798. }
  6799. if ( get_option( 'default_pingback_flag' ) ) {
  6800. add_post_meta( $post_id, '_pingme', '1', true );
  6801. }
  6802. add_post_meta( $post_id, '_encloseme', '1', true );
  6803. $to_ping = get_to_ping( $post_id );
  6804. if ( ! empty( $to_ping ) ) {
  6805. add_post_meta( $post_id, '_trackbackme', '1' );
  6806. }
  6807. if ( ! wp_next_scheduled( 'do_pings' ) ) {
  6808. wp_schedule_single_event( time(), 'do_pings' );
  6809. }
  6810. }
  6811. /**
  6812. * Returns the ID of the post's parent.
  6813. *
  6814. * @since 3.1.0
  6815. * @since 5.9.0 The `$post` parameter was made optional.
  6816. *
  6817. * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post.
  6818. * @return int|false Post parent ID (which can be 0 if there is no parent),
  6819. * or false if the post does not exist.
  6820. */
  6821. function wp_get_post_parent_id( $post = null ) {
  6822. $post = get_post( $post );
  6823. if ( ! $post || is_wp_error( $post ) ) {
  6824. return false;
  6825. }
  6826. return (int) $post->post_parent;
  6827. }
  6828. /**
  6829. * Checks the given subset of the post hierarchy for hierarchy loops.
  6830. *
  6831. * Prevents loops from forming and breaks those that it finds. Attached
  6832. * to the {@see 'wp_insert_post_parent'} filter.
  6833. *
  6834. * @since 3.1.0
  6835. *
  6836. * @see wp_find_hierarchy_loop()
  6837. *
  6838. * @param int $post_parent ID of the parent for the post we're checking.
  6839. * @param int $post_ID ID of the post we're checking.
  6840. * @return int The new post_parent for the post, 0 otherwise.
  6841. */
  6842. function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) {
  6843. // Nothing fancy here - bail.
  6844. if ( ! $post_parent ) {
  6845. return 0;
  6846. }
  6847. // New post can't cause a loop.
  6848. if ( ! $post_ID ) {
  6849. return $post_parent;
  6850. }
  6851. // Can't be its own parent.
  6852. if ( $post_parent == $post_ID ) {
  6853. return 0;
  6854. }
  6855. // Now look for larger loops.
  6856. $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent );
  6857. if ( ! $loop ) {
  6858. return $post_parent; // No loop.
  6859. }
  6860. // Setting $post_parent to the given value causes a loop.
  6861. if ( isset( $loop[ $post_ID ] ) ) {
  6862. return 0;
  6863. }
  6864. // There's a loop, but it doesn't contain $post_ID. Break the loop.
  6865. foreach ( array_keys( $loop ) as $loop_member ) {
  6866. wp_update_post(
  6867. array(
  6868. 'ID' => $loop_member,
  6869. 'post_parent' => 0,
  6870. )
  6871. );
  6872. }
  6873. return $post_parent;
  6874. }
  6875. /**
  6876. * Sets the post thumbnail (featured image) for the given post.
  6877. *
  6878. * @since 3.1.0
  6879. *
  6880. * @param int|WP_Post $post Post ID or post object where thumbnail should be attached.
  6881. * @param int $thumbnail_id Thumbnail to attach.
  6882. * @return int|bool True on success, false on failure.
  6883. */
  6884. function set_post_thumbnail( $post, $thumbnail_id ) {
  6885. $post = get_post( $post );
  6886. $thumbnail_id = absint( $thumbnail_id );
  6887. if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
  6888. if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) {
  6889. return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
  6890. } else {
  6891. return delete_post_meta( $post->ID, '_thumbnail_id' );
  6892. }
  6893. }
  6894. return false;
  6895. }
  6896. /**
  6897. * Removes the thumbnail (featured image) from the given post.
  6898. *
  6899. * @since 3.3.0
  6900. *
  6901. * @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed.
  6902. * @return bool True on success, false on failure.
  6903. */
  6904. function delete_post_thumbnail( $post ) {
  6905. $post = get_post( $post );
  6906. if ( $post ) {
  6907. return delete_post_meta( $post->ID, '_thumbnail_id' );
  6908. }
  6909. return false;
  6910. }
  6911. /**
  6912. * Deletes auto-drafts for new posts that are > 7 days old.
  6913. *
  6914. * @since 3.4.0
  6915. *
  6916. * @global wpdb $wpdb WordPress database abstraction object.
  6917. */
  6918. function wp_delete_auto_drafts() {
  6919. global $wpdb;
  6920. // Cleanup old auto-drafts more than 7 days old.
  6921. $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" );
  6922. foreach ( (array) $old_posts as $delete ) {
  6923. // Force delete.
  6924. wp_delete_post( $delete, true );
  6925. }
  6926. }
  6927. /**
  6928. * Queues posts for lazy-loading of term meta.
  6929. *
  6930. * @since 4.5.0
  6931. *
  6932. * @param WP_Post[] $posts Array of WP_Post objects.
  6933. */
  6934. function wp_queue_posts_for_term_meta_lazyload( $posts ) {
  6935. $post_type_taxonomies = array();
  6936. $term_ids = array();
  6937. foreach ( $posts as $post ) {
  6938. if ( ! ( $post instanceof WP_Post ) ) {
  6939. continue;
  6940. }
  6941. if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) {
  6942. $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type );
  6943. }
  6944. foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) {
  6945. // Term cache should already be primed by `update_post_term_cache()`.
  6946. $terms = get_object_term_cache( $post->ID, $taxonomy );
  6947. if ( false !== $terms ) {
  6948. foreach ( $terms as $term ) {
  6949. if ( ! in_array( $term->term_id, $term_ids, true ) ) {
  6950. $term_ids[] = $term->term_id;
  6951. }
  6952. }
  6953. }
  6954. }
  6955. }
  6956. if ( $term_ids ) {
  6957. $lazyloader = wp_metadata_lazyloader();
  6958. $lazyloader->queue_objects( 'term', $term_ids );
  6959. }
  6960. }
  6961. /**
  6962. * Updates the custom taxonomies' term counts when a post's status is changed.
  6963. *
  6964. * For example, default posts term counts (for custom taxonomies) don't include
  6965. * private / draft posts.
  6966. *
  6967. * @since 3.3.0
  6968. * @access private
  6969. *
  6970. * @param string $new_status New post status.
  6971. * @param string $old_status Old post status.
  6972. * @param WP_Post $post Post object.
  6973. */
  6974. function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
  6975. // Update counts for the post's terms.
  6976. foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
  6977. $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
  6978. wp_update_term_count( $tt_ids, $taxonomy );
  6979. }
  6980. }
  6981. /**
  6982. * Adds any posts from the given IDs to the cache that do not already exist in cache.
  6983. *
  6984. * @since 3.4.0
  6985. * @since 6.1.0 This function is no longer marked as "private".
  6986. *
  6987. * @see update_post_caches()
  6988. *
  6989. * @global wpdb $wpdb WordPress database abstraction object.
  6990. *
  6991. * @param int[] $ids ID list.
  6992. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true.
  6993. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true.
  6994. */
  6995. function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) {
  6996. global $wpdb;
  6997. $non_cached_ids = _get_non_cached_ids( $ids, 'posts' );
  6998. if ( ! empty( $non_cached_ids ) ) {
  6999. $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );
  7000. update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache );
  7001. }
  7002. }
  7003. /**
  7004. * Adds a suffix if any trashed posts have a given slug.
  7005. *
  7006. * Store its desired (i.e. current) slug so it can try to reclaim it
  7007. * if the post is untrashed.
  7008. *
  7009. * For internal use.
  7010. *
  7011. * @since 4.5.0
  7012. * @access private
  7013. *
  7014. * @param string $post_name Post slug.
  7015. * @param int $post_ID Optional. Post ID that should be ignored. Default 0.
  7016. */
  7017. function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) {
  7018. $trashed_posts_with_desired_slug = get_posts(
  7019. array(
  7020. 'name' => $post_name,
  7021. 'post_status' => 'trash',
  7022. 'post_type' => 'any',
  7023. 'nopaging' => true,
  7024. 'post__not_in' => array( $post_ID ),
  7025. )
  7026. );
  7027. if ( ! empty( $trashed_posts_with_desired_slug ) ) {
  7028. foreach ( $trashed_posts_with_desired_slug as $_post ) {
  7029. wp_add_trashed_suffix_to_post_name_for_post( $_post );
  7030. }
  7031. }
  7032. }
  7033. /**
  7034. * Adds a trashed suffix for a given post.
  7035. *
  7036. * Store its desired (i.e. current) slug so it can try to reclaim it
  7037. * if the post is untrashed.
  7038. *
  7039. * For internal use.
  7040. *
  7041. * @since 4.5.0
  7042. * @access private
  7043. *
  7044. * @param WP_Post $post The post.
  7045. * @return string New slug for the post.
  7046. */
  7047. function wp_add_trashed_suffix_to_post_name_for_post( $post ) {
  7048. global $wpdb;
  7049. $post = get_post( $post );
  7050. if ( '__trashed' === substr( $post->post_name, -9 ) ) {
  7051. return $post->post_name;
  7052. }
  7053. add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name );
  7054. $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed';
  7055. $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) );
  7056. clean_post_cache( $post->ID );
  7057. return $post_name;
  7058. }
  7059. /**
  7060. * Sets the last changed time for the 'posts' cache group.
  7061. *
  7062. * @since 5.0.0
  7063. */
  7064. function wp_cache_set_posts_last_changed() {
  7065. wp_cache_set( 'last_changed', microtime(), 'posts' );
  7066. }
  7067. /**
  7068. * Gets all available post MIME types for a given post type.
  7069. *
  7070. * @since 2.5.0
  7071. *
  7072. * @global wpdb $wpdb WordPress database abstraction object.
  7073. *
  7074. * @param string $type
  7075. * @return string[] An array of MIME types.
  7076. */
  7077. function get_available_post_mime_types( $type = 'attachment' ) {
  7078. global $wpdb;
  7079. $types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) );
  7080. return $types;
  7081. }
  7082. /**
  7083. * Retrieves the path to an uploaded image file.
  7084. *
  7085. * Similar to `get_attached_file()` however some images may have been processed after uploading
  7086. * to make them suitable for web use. In this case the attached "full" size file is usually replaced
  7087. * with a scaled down version of the original image. This function always returns the path
  7088. * to the originally uploaded image file.
  7089. *
  7090. * @since 5.3.0
  7091. * @since 5.4.0 Added the `$unfiltered` parameter.
  7092. *
  7093. * @param int $attachment_id Attachment ID.
  7094. * @param bool $unfiltered Optional. Passed through to `get_attached_file()`. Default false.
  7095. * @return string|false Path to the original image file or false if the attachment is not an image.
  7096. */
  7097. function wp_get_original_image_path( $attachment_id, $unfiltered = false ) {
  7098. if ( ! wp_attachment_is_image( $attachment_id ) ) {
  7099. return false;
  7100. }
  7101. $image_meta = wp_get_attachment_metadata( $attachment_id );
  7102. $image_file = get_attached_file( $attachment_id, $unfiltered );
  7103. if ( empty( $image_meta['original_image'] ) ) {
  7104. $original_image = $image_file;
  7105. } else {
  7106. $original_image = path_join( dirname( $image_file ), $image_meta['original_image'] );
  7107. }
  7108. /**
  7109. * Filters the path to the original image.
  7110. *
  7111. * @since 5.3.0
  7112. *
  7113. * @param string $original_image Path to original image file.
  7114. * @param int $attachment_id Attachment ID.
  7115. */
  7116. return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id );
  7117. }
  7118. /**
  7119. * Retrieves the URL to an original attachment image.
  7120. *
  7121. * Similar to `wp_get_attachment_url()` however some images may have been
  7122. * processed after uploading. In this case this function returns the URL
  7123. * to the originally uploaded image file.
  7124. *
  7125. * @since 5.3.0
  7126. *
  7127. * @param int $attachment_id Attachment post ID.
  7128. * @return string|false Attachment image URL, false on error or if the attachment is not an image.
  7129. */
  7130. function wp_get_original_image_url( $attachment_id ) {
  7131. if ( ! wp_attachment_is_image( $attachment_id ) ) {
  7132. return false;
  7133. }
  7134. $image_url = wp_get_attachment_url( $attachment_id );
  7135. if ( ! $image_url ) {
  7136. return false;
  7137. }
  7138. $image_meta = wp_get_attachment_metadata( $attachment_id );
  7139. if ( empty( $image_meta['original_image'] ) ) {
  7140. $original_image_url = $image_url;
  7141. } else {
  7142. $original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] );
  7143. }
  7144. /**
  7145. * Filters the URL to the original attachment image.
  7146. *
  7147. * @since 5.3.0
  7148. *
  7149. * @param string $original_image_url URL to original image.
  7150. * @param int $attachment_id Attachment ID.
  7151. */
  7152. return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id );
  7153. }
  7154. /**
  7155. * Filters callback which sets the status of an untrashed post to its previous status.
  7156. *
  7157. * This can be used as a callback on the `wp_untrash_post_status` filter.
  7158. *
  7159. * @since 5.6.0
  7160. *
  7161. * @param string $new_status The new status of the post being restored.
  7162. * @param int $post_id The ID of the post being restored.
  7163. * @param string $previous_status The status of the post at the point where it was trashed.
  7164. * @return string The new status of the post.
  7165. */
  7166. function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) {
  7167. return $previous_status;
  7168. }
  7169. /**
  7170. * Returns whether the post can be edited in the block editor.
  7171. *
  7172. * @since 5.0.0
  7173. * @since 6.1.0 Moved to wp-includes from wp-admin.
  7174. *
  7175. * @param int|WP_Post $post Post ID or WP_Post object.
  7176. * @return bool Whether the post can be edited in the block editor.
  7177. */
  7178. function use_block_editor_for_post( $post ) {
  7179. $post = get_post( $post );
  7180. if ( ! $post ) {
  7181. return false;
  7182. }
  7183. // We're in the meta box loader, so don't use the block editor.
  7184. if ( is_admin() && isset( $_GET['meta-box-loader'] ) ) {
  7185. check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
  7186. return false;
  7187. }
  7188. $use_block_editor = use_block_editor_for_post_type( $post->post_type );
  7189. /**
  7190. * Filters whether a post is able to be edited in the block editor.
  7191. *
  7192. * @since 5.0.0
  7193. *
  7194. * @param bool $use_block_editor Whether the post can be edited or not.
  7195. * @param WP_Post $post The post being checked.
  7196. */
  7197. return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
  7198. }
  7199. /**
  7200. * Returns whether a post type is compatible with the block editor.
  7201. *
  7202. * The block editor depends on the REST API, and if the post type is not shown in the
  7203. * REST API, then it won't work with the block editor.
  7204. *
  7205. * @since 5.0.0
  7206. * @since 6.1.0 Moved to wp-includes from wp-admin.
  7207. *
  7208. * @param string $post_type The post type.
  7209. * @return bool Whether the post type can be edited with the block editor.
  7210. */
  7211. function use_block_editor_for_post_type( $post_type ) {
  7212. if ( ! post_type_exists( $post_type ) ) {
  7213. return false;
  7214. }
  7215. if ( ! post_type_supports( $post_type, 'editor' ) ) {
  7216. return false;
  7217. }
  7218. $post_type_object = get_post_type_object( $post_type );
  7219. if ( $post_type_object && ! $post_type_object->show_in_rest ) {
  7220. return false;
  7221. }
  7222. /**
  7223. * Filters whether a post is able to be edited in the block editor.
  7224. *
  7225. * @since 5.0.0
  7226. *
  7227. * @param bool $use_block_editor Whether the post type can be edited or not. Default true.
  7228. * @param string $post_type The post type being checked.
  7229. */
  7230. return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
  7231. }