Brak opisu

post.php 267KB

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