Keine Beschreibung

class-wp-xmlrpc-server.php 208KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124
  1. <?php
  2. /**
  3. * XML-RPC protocol support for WordPress
  4. *
  5. * @package WordPress
  6. * @subpackage Publishing
  7. */
  8. /**
  9. * WordPress XMLRPC server implementation.
  10. *
  11. * Implements compatibility for Blogger API, MetaWeblog API, MovableType, and
  12. * pingback. Additional WordPress API for managing comments, pages, posts,
  13. * options, etc.
  14. *
  15. * As of WordPress 3.5.0, XML-RPC is enabled by default. It can be disabled
  16. * via the {@see 'xmlrpc_enabled'} filter found in wp_xmlrpc_server::set_is_enabled().
  17. *
  18. * @since 1.5.0
  19. *
  20. * @see IXR_Server
  21. */
  22. class wp_xmlrpc_server extends IXR_Server {
  23. /**
  24. * Methods.
  25. *
  26. * @var array
  27. */
  28. public $methods;
  29. /**
  30. * Blog options.
  31. *
  32. * @var array
  33. */
  34. public $blog_options;
  35. /**
  36. * IXR_Error instance.
  37. *
  38. * @var IXR_Error
  39. */
  40. public $error;
  41. /**
  42. * Flags that the user authentication has failed in this instance of wp_xmlrpc_server.
  43. *
  44. * @var bool
  45. */
  46. protected $auth_failed = false;
  47. /**
  48. * Flags that XML-RPC is enabled
  49. *
  50. * @var bool
  51. */
  52. private $is_enabled;
  53. /**
  54. * Registers all of the XMLRPC methods that XMLRPC server understands.
  55. *
  56. * Sets up server and method property. Passes XMLRPC
  57. * methods through the {@see 'xmlrpc_methods'} filter to allow plugins to extend
  58. * or replace XML-RPC methods.
  59. *
  60. * @since 1.5.0
  61. */
  62. public function __construct() {
  63. $this->methods = array(
  64. // WordPress API.
  65. 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
  66. 'wp.newPost' => 'this:wp_newPost',
  67. 'wp.editPost' => 'this:wp_editPost',
  68. 'wp.deletePost' => 'this:wp_deletePost',
  69. 'wp.getPost' => 'this:wp_getPost',
  70. 'wp.getPosts' => 'this:wp_getPosts',
  71. 'wp.newTerm' => 'this:wp_newTerm',
  72. 'wp.editTerm' => 'this:wp_editTerm',
  73. 'wp.deleteTerm' => 'this:wp_deleteTerm',
  74. 'wp.getTerm' => 'this:wp_getTerm',
  75. 'wp.getTerms' => 'this:wp_getTerms',
  76. 'wp.getTaxonomy' => 'this:wp_getTaxonomy',
  77. 'wp.getTaxonomies' => 'this:wp_getTaxonomies',
  78. 'wp.getUser' => 'this:wp_getUser',
  79. 'wp.getUsers' => 'this:wp_getUsers',
  80. 'wp.getProfile' => 'this:wp_getProfile',
  81. 'wp.editProfile' => 'this:wp_editProfile',
  82. 'wp.getPage' => 'this:wp_getPage',
  83. 'wp.getPages' => 'this:wp_getPages',
  84. 'wp.newPage' => 'this:wp_newPage',
  85. 'wp.deletePage' => 'this:wp_deletePage',
  86. 'wp.editPage' => 'this:wp_editPage',
  87. 'wp.getPageList' => 'this:wp_getPageList',
  88. 'wp.getAuthors' => 'this:wp_getAuthors',
  89. 'wp.getCategories' => 'this:mw_getCategories', // Alias.
  90. 'wp.getTags' => 'this:wp_getTags',
  91. 'wp.newCategory' => 'this:wp_newCategory',
  92. 'wp.deleteCategory' => 'this:wp_deleteCategory',
  93. 'wp.suggestCategories' => 'this:wp_suggestCategories',
  94. 'wp.uploadFile' => 'this:mw_newMediaObject', // Alias.
  95. 'wp.deleteFile' => 'this:wp_deletePost', // Alias.
  96. 'wp.getCommentCount' => 'this:wp_getCommentCount',
  97. 'wp.getPostStatusList' => 'this:wp_getPostStatusList',
  98. 'wp.getPageStatusList' => 'this:wp_getPageStatusList',
  99. 'wp.getPageTemplates' => 'this:wp_getPageTemplates',
  100. 'wp.getOptions' => 'this:wp_getOptions',
  101. 'wp.setOptions' => 'this:wp_setOptions',
  102. 'wp.getComment' => 'this:wp_getComment',
  103. 'wp.getComments' => 'this:wp_getComments',
  104. 'wp.deleteComment' => 'this:wp_deleteComment',
  105. 'wp.editComment' => 'this:wp_editComment',
  106. 'wp.newComment' => 'this:wp_newComment',
  107. 'wp.getCommentStatusList' => 'this:wp_getCommentStatusList',
  108. 'wp.getMediaItem' => 'this:wp_getMediaItem',
  109. 'wp.getMediaLibrary' => 'this:wp_getMediaLibrary',
  110. 'wp.getPostFormats' => 'this:wp_getPostFormats',
  111. 'wp.getPostType' => 'this:wp_getPostType',
  112. 'wp.getPostTypes' => 'this:wp_getPostTypes',
  113. 'wp.getRevisions' => 'this:wp_getRevisions',
  114. 'wp.restoreRevision' => 'this:wp_restoreRevision',
  115. // Blogger API.
  116. 'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  117. 'blogger.getUserInfo' => 'this:blogger_getUserInfo',
  118. 'blogger.getPost' => 'this:blogger_getPost',
  119. 'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
  120. 'blogger.newPost' => 'this:blogger_newPost',
  121. 'blogger.editPost' => 'this:blogger_editPost',
  122. 'blogger.deletePost' => 'this:blogger_deletePost',
  123. // MetaWeblog API (with MT extensions to structs).
  124. 'metaWeblog.newPost' => 'this:mw_newPost',
  125. 'metaWeblog.editPost' => 'this:mw_editPost',
  126. 'metaWeblog.getPost' => 'this:mw_getPost',
  127. 'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
  128. 'metaWeblog.getCategories' => 'this:mw_getCategories',
  129. 'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
  130. // MetaWeblog API aliases for Blogger API.
  131. // See http://www.xmlrpc.com/stories/storyReader$2460
  132. 'metaWeblog.deletePost' => 'this:blogger_deletePost',
  133. 'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
  134. // MovableType API.
  135. 'mt.getCategoryList' => 'this:mt_getCategoryList',
  136. 'mt.getRecentPostTitles' => 'this:mt_getRecentPostTitles',
  137. 'mt.getPostCategories' => 'this:mt_getPostCategories',
  138. 'mt.setPostCategories' => 'this:mt_setPostCategories',
  139. 'mt.supportedMethods' => 'this:mt_supportedMethods',
  140. 'mt.supportedTextFilters' => 'this:mt_supportedTextFilters',
  141. 'mt.getTrackbackPings' => 'this:mt_getTrackbackPings',
  142. 'mt.publishPost' => 'this:mt_publishPost',
  143. // Pingback.
  144. 'pingback.ping' => 'this:pingback_ping',
  145. 'pingback.extensions.getPingbacks' => 'this:pingback_extensions_getPingbacks',
  146. 'demo.sayHello' => 'this:sayHello',
  147. 'demo.addTwoNumbers' => 'this:addTwoNumbers',
  148. );
  149. $this->initialise_blog_option_info();
  150. /**
  151. * Filters the methods exposed by the XML-RPC server.
  152. *
  153. * This filter can be used to add new methods, and remove built-in methods.
  154. *
  155. * @since 1.5.0
  156. *
  157. * @param string[] $methods An array of XML-RPC methods, keyed by their methodName.
  158. */
  159. $this->methods = apply_filters( 'xmlrpc_methods', $this->methods );
  160. $this->set_is_enabled();
  161. }
  162. /**
  163. * Set wp_xmlrpc_server::$is_enabled property.
  164. *
  165. * Determine whether the xmlrpc server is enabled on this WordPress install
  166. * and set the is_enabled property accordingly.
  167. *
  168. * @since 5.7.3
  169. */
  170. private function set_is_enabled() {
  171. /*
  172. * Respect old get_option() filters left for back-compat when the 'enable_xmlrpc'
  173. * option was deprecated in 3.5.0. Use the 'xmlrpc_enabled' hook instead.
  174. */
  175. $is_enabled = apply_filters( 'pre_option_enable_xmlrpc', false );
  176. if ( false === $is_enabled ) {
  177. $is_enabled = apply_filters( 'option_enable_xmlrpc', true );
  178. }
  179. /**
  180. * Filters whether XML-RPC methods requiring authentication are enabled.
  181. *
  182. * Contrary to the way it's named, this filter does not control whether XML-RPC is *fully*
  183. * enabled, rather, it only controls whether XML-RPC methods requiring authentication - such
  184. * as for publishing purposes - are enabled.
  185. *
  186. * Further, the filter does not control whether pingbacks or other custom endpoints that don't
  187. * require authentication are enabled. This behavior is expected, and due to how parity was matched
  188. * with the `enable_xmlrpc` UI option the filter replaced when it was introduced in 3.5.
  189. *
  190. * To disable XML-RPC methods that require authentication, use:
  191. *
  192. * add_filter( 'xmlrpc_enabled', '__return_false' );
  193. *
  194. * For more granular control over all XML-RPC methods and requests, see the {@see 'xmlrpc_methods'}
  195. * and {@see 'xmlrpc_element_limit'} hooks.
  196. *
  197. * @since 3.5.0
  198. *
  199. * @param bool $is_enabled Whether XML-RPC is enabled. Default true.
  200. */
  201. $this->is_enabled = apply_filters( 'xmlrpc_enabled', $is_enabled );
  202. }
  203. /**
  204. * Make private/protected methods readable for backward compatibility.
  205. *
  206. * @since 4.0.0
  207. *
  208. * @param string $name Method to call.
  209. * @param array $arguments Arguments to pass when calling.
  210. * @return array|IXR_Error|false Return value of the callback, false otherwise.
  211. */
  212. public function __call( $name, $arguments ) {
  213. if ( '_multisite_getUsersBlogs' === $name ) {
  214. return $this->_multisite_getUsersBlogs( ...$arguments );
  215. }
  216. return false;
  217. }
  218. /**
  219. * Serves the XML-RPC request.
  220. *
  221. * @since 2.9.0
  222. */
  223. public function serve_request() {
  224. $this->IXR_Server( $this->methods );
  225. }
  226. /**
  227. * Test XMLRPC API by saying, "Hello!" to client.
  228. *
  229. * @since 1.5.0
  230. *
  231. * @return string Hello string response.
  232. */
  233. public function sayHello() {
  234. return 'Hello!';
  235. }
  236. /**
  237. * Test XMLRPC API by adding two numbers for client.
  238. *
  239. * @since 1.5.0
  240. *
  241. * @param array $args {
  242. * Method arguments. Note: arguments must be ordered as documented.
  243. *
  244. * @type int $number1 A number to add.
  245. * @type int $number2 A second number to add.
  246. * }
  247. * @return int Sum of the two given numbers.
  248. */
  249. public function addTwoNumbers( $args ) {
  250. $number1 = $args[0];
  251. $number2 = $args[1];
  252. return $number1 + $number2;
  253. }
  254. /**
  255. * Log user in.
  256. *
  257. * @since 2.8.0
  258. *
  259. * @param string $username User's username.
  260. * @param string $password User's password.
  261. * @return WP_User|false WP_User object if authentication passed, false otherwise
  262. */
  263. public function login( $username, $password ) {
  264. if ( ! $this->is_enabled ) {
  265. $this->error = new IXR_Error( 405, sprintf( __( 'XML-RPC services are disabled on this site.' ) ) );
  266. return false;
  267. }
  268. if ( $this->auth_failed ) {
  269. $user = new WP_Error( 'login_prevented' );
  270. } else {
  271. $user = wp_authenticate( $username, $password );
  272. }
  273. if ( is_wp_error( $user ) ) {
  274. $this->error = new IXR_Error( 403, __( 'Incorrect username or password.' ) );
  275. // Flag that authentication has failed once on this wp_xmlrpc_server instance.
  276. $this->auth_failed = true;
  277. /**
  278. * Filters the XML-RPC user login error message.
  279. *
  280. * @since 3.5.0
  281. *
  282. * @param IXR_Error $error The XML-RPC error message.
  283. * @param WP_Error $user WP_Error object.
  284. */
  285. $this->error = apply_filters( 'xmlrpc_login_error', $this->error, $user );
  286. return false;
  287. }
  288. wp_set_current_user( $user->ID );
  289. return $user;
  290. }
  291. /**
  292. * Check user's credentials. Deprecated.
  293. *
  294. * @since 1.5.0
  295. * @deprecated 2.8.0 Use wp_xmlrpc_server::login()
  296. * @see wp_xmlrpc_server::login()
  297. *
  298. * @param string $username User's username.
  299. * @param string $password User's password.
  300. * @return bool Whether authentication passed.
  301. */
  302. public function login_pass_ok( $username, $password ) {
  303. return (bool) $this->login( $username, $password );
  304. }
  305. /**
  306. * Escape string or array of strings for database.
  307. *
  308. * @since 1.5.2
  309. *
  310. * @param string|array $data Escape single string or array of strings.
  311. * @return string|void Returns with string is passed, alters by-reference
  312. * when array is passed.
  313. */
  314. public function escape( &$data ) {
  315. if ( ! is_array( $data ) ) {
  316. return wp_slash( $data );
  317. }
  318. foreach ( $data as &$v ) {
  319. if ( is_array( $v ) ) {
  320. $this->escape( $v );
  321. } elseif ( ! is_object( $v ) ) {
  322. $v = wp_slash( $v );
  323. }
  324. }
  325. }
  326. /**
  327. * Send error response to client.
  328. *
  329. * Send an XML error response to the client. If the endpoint is enabled
  330. * an HTTP 200 response is always sent per the XML-RPC specification.
  331. *
  332. * @since 5.7.3
  333. *
  334. * @param IXR_Error|string $error Error code or an error object.
  335. * @param false $message Error message. Optional.
  336. */
  337. public function error( $error, $message = false ) {
  338. // Accepts either an error object or an error code and message
  339. if ( $message && ! is_object( $error ) ) {
  340. $error = new IXR_Error( $error, $message );
  341. }
  342. if ( ! $this->is_enabled ) {
  343. status_header( $error->code );
  344. }
  345. $this->output( $error->getXml() );
  346. }
  347. /**
  348. * Retrieve custom fields for post.
  349. *
  350. * @since 2.5.0
  351. *
  352. * @param int $post_id Post ID.
  353. * @return array Custom fields, if exist.
  354. */
  355. public function get_custom_fields( $post_id ) {
  356. $post_id = (int) $post_id;
  357. $custom_fields = array();
  358. foreach ( (array) has_meta( $post_id ) as $meta ) {
  359. // Don't expose protected fields.
  360. if ( ! current_user_can( 'edit_post_meta', $post_id, $meta['meta_key'] ) ) {
  361. continue;
  362. }
  363. $custom_fields[] = array(
  364. 'id' => $meta['meta_id'],
  365. 'key' => $meta['meta_key'],
  366. 'value' => $meta['meta_value'],
  367. );
  368. }
  369. return $custom_fields;
  370. }
  371. /**
  372. * Set custom fields for post.
  373. *
  374. * @since 2.5.0
  375. *
  376. * @param int $post_id Post ID.
  377. * @param array $fields Custom fields.
  378. */
  379. public function set_custom_fields( $post_id, $fields ) {
  380. $post_id = (int) $post_id;
  381. foreach ( (array) $fields as $meta ) {
  382. if ( isset( $meta['id'] ) ) {
  383. $meta['id'] = (int) $meta['id'];
  384. $pmeta = get_metadata_by_mid( 'post', $meta['id'] );
  385. if ( ! $pmeta || $pmeta->post_id != $post_id ) {
  386. continue;
  387. }
  388. if ( isset( $meta['key'] ) ) {
  389. $meta['key'] = wp_unslash( $meta['key'] );
  390. if ( $meta['key'] !== $pmeta->meta_key ) {
  391. continue;
  392. }
  393. $meta['value'] = wp_unslash( $meta['value'] );
  394. if ( current_user_can( 'edit_post_meta', $post_id, $meta['key'] ) ) {
  395. update_metadata_by_mid( 'post', $meta['id'], $meta['value'] );
  396. }
  397. } elseif ( current_user_can( 'delete_post_meta', $post_id, $pmeta->meta_key ) ) {
  398. delete_metadata_by_mid( 'post', $meta['id'] );
  399. }
  400. } elseif ( current_user_can( 'add_post_meta', $post_id, wp_unslash( $meta['key'] ) ) ) {
  401. add_post_meta( $post_id, $meta['key'], $meta['value'] );
  402. }
  403. }
  404. }
  405. /**
  406. * Retrieve custom fields for a term.
  407. *
  408. * @since 4.9.0
  409. *
  410. * @param int $term_id Term ID.
  411. * @return array Array of custom fields, if they exist.
  412. */
  413. public function get_term_custom_fields( $term_id ) {
  414. $term_id = (int) $term_id;
  415. $custom_fields = array();
  416. foreach ( (array) has_term_meta( $term_id ) as $meta ) {
  417. if ( ! current_user_can( 'edit_term_meta', $term_id ) ) {
  418. continue;
  419. }
  420. $custom_fields[] = array(
  421. 'id' => $meta['meta_id'],
  422. 'key' => $meta['meta_key'],
  423. 'value' => $meta['meta_value'],
  424. );
  425. }
  426. return $custom_fields;
  427. }
  428. /**
  429. * Set custom fields for a term.
  430. *
  431. * @since 4.9.0
  432. *
  433. * @param int $term_id Term ID.
  434. * @param array $fields Custom fields.
  435. */
  436. public function set_term_custom_fields( $term_id, $fields ) {
  437. $term_id = (int) $term_id;
  438. foreach ( (array) $fields as $meta ) {
  439. if ( isset( $meta['id'] ) ) {
  440. $meta['id'] = (int) $meta['id'];
  441. $pmeta = get_metadata_by_mid( 'term', $meta['id'] );
  442. if ( isset( $meta['key'] ) ) {
  443. $meta['key'] = wp_unslash( $meta['key'] );
  444. if ( $meta['key'] !== $pmeta->meta_key ) {
  445. continue;
  446. }
  447. $meta['value'] = wp_unslash( $meta['value'] );
  448. if ( current_user_can( 'edit_term_meta', $term_id ) ) {
  449. update_metadata_by_mid( 'term', $meta['id'], $meta['value'] );
  450. }
  451. } elseif ( current_user_can( 'delete_term_meta', $term_id ) ) {
  452. delete_metadata_by_mid( 'term', $meta['id'] );
  453. }
  454. } elseif ( current_user_can( 'add_term_meta', $term_id ) ) {
  455. add_term_meta( $term_id, $meta['key'], $meta['value'] );
  456. }
  457. }
  458. }
  459. /**
  460. * Set up blog options property.
  461. *
  462. * Passes property through {@see 'xmlrpc_blog_options'} filter.
  463. *
  464. * @since 2.6.0
  465. */
  466. public function initialise_blog_option_info() {
  467. $this->blog_options = array(
  468. // Read-only options.
  469. 'software_name' => array(
  470. 'desc' => __( 'Software Name' ),
  471. 'readonly' => true,
  472. 'value' => 'WordPress',
  473. ),
  474. 'software_version' => array(
  475. 'desc' => __( 'Software Version' ),
  476. 'readonly' => true,
  477. 'value' => get_bloginfo( 'version' ),
  478. ),
  479. 'blog_url' => array(
  480. 'desc' => __( 'WordPress Address (URL)' ),
  481. 'readonly' => true,
  482. 'option' => 'siteurl',
  483. ),
  484. 'home_url' => array(
  485. 'desc' => __( 'Site Address (URL)' ),
  486. 'readonly' => true,
  487. 'option' => 'home',
  488. ),
  489. 'login_url' => array(
  490. 'desc' => __( 'Login Address (URL)' ),
  491. 'readonly' => true,
  492. 'value' => wp_login_url(),
  493. ),
  494. 'admin_url' => array(
  495. 'desc' => __( 'The URL to the admin area' ),
  496. 'readonly' => true,
  497. 'value' => get_admin_url(),
  498. ),
  499. 'image_default_link_type' => array(
  500. 'desc' => __( 'Image default link type' ),
  501. 'readonly' => true,
  502. 'option' => 'image_default_link_type',
  503. ),
  504. 'image_default_size' => array(
  505. 'desc' => __( 'Image default size' ),
  506. 'readonly' => true,
  507. 'option' => 'image_default_size',
  508. ),
  509. 'image_default_align' => array(
  510. 'desc' => __( 'Image default align' ),
  511. 'readonly' => true,
  512. 'option' => 'image_default_align',
  513. ),
  514. 'template' => array(
  515. 'desc' => __( 'Template' ),
  516. 'readonly' => true,
  517. 'option' => 'template',
  518. ),
  519. 'stylesheet' => array(
  520. 'desc' => __( 'Stylesheet' ),
  521. 'readonly' => true,
  522. 'option' => 'stylesheet',
  523. ),
  524. 'post_thumbnail' => array(
  525. 'desc' => __( 'Post Thumbnail' ),
  526. 'readonly' => true,
  527. 'value' => current_theme_supports( 'post-thumbnails' ),
  528. ),
  529. // Updatable options.
  530. 'time_zone' => array(
  531. 'desc' => __( 'Time Zone' ),
  532. 'readonly' => false,
  533. 'option' => 'gmt_offset',
  534. ),
  535. 'blog_title' => array(
  536. 'desc' => __( 'Site Title' ),
  537. 'readonly' => false,
  538. 'option' => 'blogname',
  539. ),
  540. 'blog_tagline' => array(
  541. 'desc' => __( 'Site Tagline' ),
  542. 'readonly' => false,
  543. 'option' => 'blogdescription',
  544. ),
  545. 'date_format' => array(
  546. 'desc' => __( 'Date Format' ),
  547. 'readonly' => false,
  548. 'option' => 'date_format',
  549. ),
  550. 'time_format' => array(
  551. 'desc' => __( 'Time Format' ),
  552. 'readonly' => false,
  553. 'option' => 'time_format',
  554. ),
  555. 'users_can_register' => array(
  556. 'desc' => __( 'Allow new users to sign up' ),
  557. 'readonly' => false,
  558. 'option' => 'users_can_register',
  559. ),
  560. 'thumbnail_size_w' => array(
  561. 'desc' => __( 'Thumbnail Width' ),
  562. 'readonly' => false,
  563. 'option' => 'thumbnail_size_w',
  564. ),
  565. 'thumbnail_size_h' => array(
  566. 'desc' => __( 'Thumbnail Height' ),
  567. 'readonly' => false,
  568. 'option' => 'thumbnail_size_h',
  569. ),
  570. 'thumbnail_crop' => array(
  571. 'desc' => __( 'Crop thumbnail to exact dimensions' ),
  572. 'readonly' => false,
  573. 'option' => 'thumbnail_crop',
  574. ),
  575. 'medium_size_w' => array(
  576. 'desc' => __( 'Medium size image width' ),
  577. 'readonly' => false,
  578. 'option' => 'medium_size_w',
  579. ),
  580. 'medium_size_h' => array(
  581. 'desc' => __( 'Medium size image height' ),
  582. 'readonly' => false,
  583. 'option' => 'medium_size_h',
  584. ),
  585. 'medium_large_size_w' => array(
  586. 'desc' => __( 'Medium-Large size image width' ),
  587. 'readonly' => false,
  588. 'option' => 'medium_large_size_w',
  589. ),
  590. 'medium_large_size_h' => array(
  591. 'desc' => __( 'Medium-Large size image height' ),
  592. 'readonly' => false,
  593. 'option' => 'medium_large_size_h',
  594. ),
  595. 'large_size_w' => array(
  596. 'desc' => __( 'Large size image width' ),
  597. 'readonly' => false,
  598. 'option' => 'large_size_w',
  599. ),
  600. 'large_size_h' => array(
  601. 'desc' => __( 'Large size image height' ),
  602. 'readonly' => false,
  603. 'option' => 'large_size_h',
  604. ),
  605. 'default_comment_status' => array(
  606. 'desc' => __( 'Allow people to submit comments on new posts.' ),
  607. 'readonly' => false,
  608. 'option' => 'default_comment_status',
  609. ),
  610. 'default_ping_status' => array(
  611. 'desc' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new posts.' ),
  612. 'readonly' => false,
  613. 'option' => 'default_ping_status',
  614. ),
  615. );
  616. /**
  617. * Filters the XML-RPC blog options property.
  618. *
  619. * @since 2.6.0
  620. *
  621. * @param array $blog_options An array of XML-RPC blog options.
  622. */
  623. $this->blog_options = apply_filters( 'xmlrpc_blog_options', $this->blog_options );
  624. }
  625. /**
  626. * Retrieve the blogs of the user.
  627. *
  628. * @since 2.6.0
  629. *
  630. * @param array $args {
  631. * Method arguments. Note: arguments must be ordered as documented.
  632. *
  633. * @type string $username Username.
  634. * @type string $password Password.
  635. * }
  636. * @return array|IXR_Error Array contains:
  637. * - 'isAdmin'
  638. * - 'isPrimary' - whether the blog is the user's primary blog
  639. * - 'url'
  640. * - 'blogid'
  641. * - 'blogName'
  642. * - 'xmlrpc' - url of xmlrpc endpoint
  643. */
  644. public function wp_getUsersBlogs( $args ) {
  645. if ( ! $this->minimum_args( $args, 2 ) ) {
  646. return $this->error;
  647. }
  648. // If this isn't on WPMU then just use blogger_getUsersBlogs().
  649. if ( ! is_multisite() ) {
  650. array_unshift( $args, 1 );
  651. return $this->blogger_getUsersBlogs( $args );
  652. }
  653. $this->escape( $args );
  654. $username = $args[0];
  655. $password = $args[1];
  656. $user = $this->login( $username, $password );
  657. if ( ! $user ) {
  658. return $this->error;
  659. }
  660. /**
  661. * Fires after the XML-RPC user has been authenticated but before the rest of
  662. * the method logic begins.
  663. *
  664. * All built-in XML-RPC methods use the action xmlrpc_call, with a parameter
  665. * equal to the method's name, e.g., wp.getUsersBlogs, wp.newPost, etc.
  666. *
  667. * @since 2.5.0
  668. * @since 5.7.0 Added the `$args` and `$server` parameters.
  669. *
  670. * @param string $name The method name.
  671. * @param array|string $args The escaped arguments passed to the method.
  672. * @param wp_xmlrpc_server $server The XML-RPC server instance.
  673. */
  674. do_action( 'xmlrpc_call', 'wp.getUsersBlogs', $args, $this );
  675. $blogs = (array) get_blogs_of_user( $user->ID );
  676. $struct = array();
  677. $primary_blog_id = 0;
  678. $active_blog = get_active_blog_for_user( $user->ID );
  679. if ( $active_blog ) {
  680. $primary_blog_id = (int) $active_blog->blog_id;
  681. }
  682. foreach ( $blogs as $blog ) {
  683. // Don't include blogs that aren't hosted at this site.
  684. if ( get_current_network_id() != $blog->site_id ) {
  685. continue;
  686. }
  687. $blog_id = $blog->userblog_id;
  688. switch_to_blog( $blog_id );
  689. $is_admin = current_user_can( 'manage_options' );
  690. $is_primary = ( (int) $blog_id === $primary_blog_id );
  691. $struct[] = array(
  692. 'isAdmin' => $is_admin,
  693. 'isPrimary' => $is_primary,
  694. 'url' => home_url( '/' ),
  695. 'blogid' => (string) $blog_id,
  696. 'blogName' => get_option( 'blogname' ),
  697. 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
  698. );
  699. restore_current_blog();
  700. }
  701. return $struct;
  702. }
  703. /**
  704. * Checks if the method received at least the minimum number of arguments.
  705. *
  706. * @since 3.4.0
  707. *
  708. * @param array $args An array of arguments to check.
  709. * @param int $count Minimum number of arguments.
  710. * @return bool True if `$args` contains at least `$count` arguments, false otherwise.
  711. */
  712. protected function minimum_args( $args, $count ) {
  713. if ( ! is_array( $args ) || count( $args ) < $count ) {
  714. $this->error = new IXR_Error( 400, __( 'Insufficient arguments passed to this XML-RPC method.' ) );
  715. return false;
  716. }
  717. return true;
  718. }
  719. /**
  720. * Prepares taxonomy data for return in an XML-RPC object.
  721. *
  722. * @param WP_Taxonomy $taxonomy The unprepared taxonomy data.
  723. * @param array $fields The subset of taxonomy fields to return.
  724. * @return array The prepared taxonomy data.
  725. */
  726. protected function _prepare_taxonomy( $taxonomy, $fields ) {
  727. $_taxonomy = array(
  728. 'name' => $taxonomy->name,
  729. 'label' => $taxonomy->label,
  730. 'hierarchical' => (bool) $taxonomy->hierarchical,
  731. 'public' => (bool) $taxonomy->public,
  732. 'show_ui' => (bool) $taxonomy->show_ui,
  733. '_builtin' => (bool) $taxonomy->_builtin,
  734. );
  735. if ( in_array( 'labels', $fields, true ) ) {
  736. $_taxonomy['labels'] = (array) $taxonomy->labels;
  737. }
  738. if ( in_array( 'cap', $fields, true ) ) {
  739. $_taxonomy['cap'] = (array) $taxonomy->cap;
  740. }
  741. if ( in_array( 'menu', $fields, true ) ) {
  742. $_taxonomy['show_in_menu'] = (bool) $taxonomy->show_in_menu;
  743. }
  744. if ( in_array( 'object_type', $fields, true ) ) {
  745. $_taxonomy['object_type'] = array_unique( (array) $taxonomy->object_type );
  746. }
  747. /**
  748. * Filters XML-RPC-prepared data for the given taxonomy.
  749. *
  750. * @since 3.4.0
  751. *
  752. * @param array $_taxonomy An array of taxonomy data.
  753. * @param WP_Taxonomy $taxonomy Taxonomy object.
  754. * @param array $fields The subset of taxonomy fields to return.
  755. */
  756. return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
  757. }
  758. /**
  759. * Prepares term data for return in an XML-RPC object.
  760. *
  761. * @param array|object $term The unprepared term data.
  762. * @return array The prepared term data.
  763. */
  764. protected function _prepare_term( $term ) {
  765. $_term = $term;
  766. if ( ! is_array( $_term ) ) {
  767. $_term = get_object_vars( $_term );
  768. }
  769. // For integers which may be larger than XML-RPC supports ensure we return strings.
  770. $_term['term_id'] = (string) $_term['term_id'];
  771. $_term['term_group'] = (string) $_term['term_group'];
  772. $_term['term_taxonomy_id'] = (string) $_term['term_taxonomy_id'];
  773. $_term['parent'] = (string) $_term['parent'];
  774. // Count we are happy to return as an integer because people really shouldn't use terms that much.
  775. $_term['count'] = (int) $_term['count'];
  776. // Get term meta.
  777. $_term['custom_fields'] = $this->get_term_custom_fields( $_term['term_id'] );
  778. /**
  779. * Filters XML-RPC-prepared data for the given term.
  780. *
  781. * @since 3.4.0
  782. *
  783. * @param array $_term An array of term data.
  784. * @param array|object $term Term object or array.
  785. */
  786. return apply_filters( 'xmlrpc_prepare_term', $_term, $term );
  787. }
  788. /**
  789. * Convert a WordPress date string to an IXR_Date object.
  790. *
  791. * @param string $date Date string to convert.
  792. * @return IXR_Date IXR_Date object.
  793. */
  794. protected function _convert_date( $date ) {
  795. if ( '0000-00-00 00:00:00' === $date ) {
  796. return new IXR_Date( '00000000T00:00:00Z' );
  797. }
  798. return new IXR_Date( mysql2date( 'Ymd\TH:i:s', $date, false ) );
  799. }
  800. /**
  801. * Convert a WordPress GMT date string to an IXR_Date object.
  802. *
  803. * @param string $date_gmt WordPress GMT date string.
  804. * @param string $date Date string.
  805. * @return IXR_Date IXR_Date object.
  806. */
  807. protected function _convert_date_gmt( $date_gmt, $date ) {
  808. if ( '0000-00-00 00:00:00' !== $date && '0000-00-00 00:00:00' === $date_gmt ) {
  809. return new IXR_Date( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $date, false ), 'Ymd\TH:i:s' ) );
  810. }
  811. return $this->_convert_date( $date_gmt );
  812. }
  813. /**
  814. * Prepares post data for return in an XML-RPC object.
  815. *
  816. * @param array $post The unprepared post data.
  817. * @param array $fields The subset of post type fields to return.
  818. * @return array The prepared post data.
  819. */
  820. protected function _prepare_post( $post, $fields ) {
  821. // Holds the data for this post. built up based on $fields.
  822. $_post = array( 'post_id' => (string) $post['ID'] );
  823. // Prepare common post fields.
  824. $post_fields = array(
  825. 'post_title' => $post['post_title'],
  826. 'post_date' => $this->_convert_date( $post['post_date'] ),
  827. 'post_date_gmt' => $this->_convert_date_gmt( $post['post_date_gmt'], $post['post_date'] ),
  828. 'post_modified' => $this->_convert_date( $post['post_modified'] ),
  829. 'post_modified_gmt' => $this->_convert_date_gmt( $post['post_modified_gmt'], $post['post_modified'] ),
  830. 'post_status' => $post['post_status'],
  831. 'post_type' => $post['post_type'],
  832. 'post_name' => $post['post_name'],
  833. 'post_author' => $post['post_author'],
  834. 'post_password' => $post['post_password'],
  835. 'post_excerpt' => $post['post_excerpt'],
  836. 'post_content' => $post['post_content'],
  837. 'post_parent' => (string) $post['post_parent'],
  838. 'post_mime_type' => $post['post_mime_type'],
  839. 'link' => get_permalink( $post['ID'] ),
  840. 'guid' => $post['guid'],
  841. 'menu_order' => (int) $post['menu_order'],
  842. 'comment_status' => $post['comment_status'],
  843. 'ping_status' => $post['ping_status'],
  844. 'sticky' => ( 'post' === $post['post_type'] && is_sticky( $post['ID'] ) ),
  845. );
  846. // Thumbnail.
  847. $post_fields['post_thumbnail'] = array();
  848. $thumbnail_id = get_post_thumbnail_id( $post['ID'] );
  849. if ( $thumbnail_id ) {
  850. $thumbnail_size = current_theme_supports( 'post-thumbnail' ) ? 'post-thumbnail' : 'thumbnail';
  851. $post_fields['post_thumbnail'] = $this->_prepare_media_item( get_post( $thumbnail_id ), $thumbnail_size );
  852. }
  853. // Consider future posts as published.
  854. if ( 'future' === $post_fields['post_status'] ) {
  855. $post_fields['post_status'] = 'publish';
  856. }
  857. // Fill in blank post format.
  858. $post_fields['post_format'] = get_post_format( $post['ID'] );
  859. if ( empty( $post_fields['post_format'] ) ) {
  860. $post_fields['post_format'] = 'standard';
  861. }
  862. // Merge requested $post_fields fields into $_post.
  863. if ( in_array( 'post', $fields, true ) ) {
  864. $_post = array_merge( $_post, $post_fields );
  865. } else {
  866. $requested_fields = array_intersect_key( $post_fields, array_flip( $fields ) );
  867. $_post = array_merge( $_post, $requested_fields );
  868. }
  869. $all_taxonomy_fields = in_array( 'taxonomies', $fields, true );
  870. if ( $all_taxonomy_fields || in_array( 'terms', $fields, true ) ) {
  871. $post_type_taxonomies = get_object_taxonomies( $post['post_type'], 'names' );
  872. $terms = wp_get_object_terms( $post['ID'], $post_type_taxonomies );
  873. $_post['terms'] = array();
  874. foreach ( $terms as $term ) {
  875. $_post['terms'][] = $this->_prepare_term( $term );
  876. }
  877. }
  878. if ( in_array( 'custom_fields', $fields, true ) ) {
  879. $_post['custom_fields'] = $this->get_custom_fields( $post['ID'] );
  880. }
  881. if ( in_array( 'enclosure', $fields, true ) ) {
  882. $_post['enclosure'] = array();
  883. $enclosures = (array) get_post_meta( $post['ID'], 'enclosure' );
  884. if ( ! empty( $enclosures ) ) {
  885. $encdata = explode( "\n", $enclosures[0] );
  886. $_post['enclosure']['url'] = trim( htmlspecialchars( $encdata[0] ) );
  887. $_post['enclosure']['length'] = (int) trim( $encdata[1] );
  888. $_post['enclosure']['type'] = trim( $encdata[2] );
  889. }
  890. }
  891. /**
  892. * Filters XML-RPC-prepared date for the given post.
  893. *
  894. * @since 3.4.0
  895. *
  896. * @param array $_post An array of modified post data.
  897. * @param array $post An array of post data.
  898. * @param array $fields An array of post fields.
  899. */
  900. return apply_filters( 'xmlrpc_prepare_post', $_post, $post, $fields );
  901. }
  902. /**
  903. * Prepares post data for return in an XML-RPC object.
  904. *
  905. * @since 3.4.0
  906. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
  907. *
  908. * @param WP_Post_Type $post_type Post type object.
  909. * @param array $fields The subset of post fields to return.
  910. * @return array The prepared post type data.
  911. */
  912. protected function _prepare_post_type( $post_type, $fields ) {
  913. $_post_type = array(
  914. 'name' => $post_type->name,
  915. 'label' => $post_type->label,
  916. 'hierarchical' => (bool) $post_type->hierarchical,
  917. 'public' => (bool) $post_type->public,
  918. 'show_ui' => (bool) $post_type->show_ui,
  919. '_builtin' => (bool) $post_type->_builtin,
  920. 'has_archive' => (bool) $post_type->has_archive,
  921. 'supports' => get_all_post_type_supports( $post_type->name ),
  922. );
  923. if ( in_array( 'labels', $fields, true ) ) {
  924. $_post_type['labels'] = (array) $post_type->labels;
  925. }
  926. if ( in_array( 'cap', $fields, true ) ) {
  927. $_post_type['cap'] = (array) $post_type->cap;
  928. $_post_type['map_meta_cap'] = (bool) $post_type->map_meta_cap;
  929. }
  930. if ( in_array( 'menu', $fields, true ) ) {
  931. $_post_type['menu_position'] = (int) $post_type->menu_position;
  932. $_post_type['menu_icon'] = $post_type->menu_icon;
  933. $_post_type['show_in_menu'] = (bool) $post_type->show_in_menu;
  934. }
  935. if ( in_array( 'taxonomies', $fields, true ) ) {
  936. $_post_type['taxonomies'] = get_object_taxonomies( $post_type->name, 'names' );
  937. }
  938. /**
  939. * Filters XML-RPC-prepared date for the given post type.
  940. *
  941. * @since 3.4.0
  942. * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
  943. *
  944. * @param array $_post_type An array of post type data.
  945. * @param WP_Post_Type $post_type Post type object.
  946. */
  947. return apply_filters( 'xmlrpc_prepare_post_type', $_post_type, $post_type );
  948. }
  949. /**
  950. * Prepares media item data for return in an XML-RPC object.
  951. *
  952. * @param WP_Post $media_item The unprepared media item data.
  953. * @param string $thumbnail_size The image size to use for the thumbnail URL.
  954. * @return array The prepared media item data.
  955. */
  956. protected function _prepare_media_item( $media_item, $thumbnail_size = 'thumbnail' ) {
  957. $_media_item = array(
  958. 'attachment_id' => (string) $media_item->ID,
  959. 'date_created_gmt' => $this->_convert_date_gmt( $media_item->post_date_gmt, $media_item->post_date ),
  960. 'parent' => $media_item->post_parent,
  961. 'link' => wp_get_attachment_url( $media_item->ID ),
  962. 'title' => $media_item->post_title,
  963. 'caption' => $media_item->post_excerpt,
  964. 'description' => $media_item->post_content,
  965. 'metadata' => wp_get_attachment_metadata( $media_item->ID ),
  966. 'type' => $media_item->post_mime_type,
  967. );
  968. $thumbnail_src = image_downsize( $media_item->ID, $thumbnail_size );
  969. if ( $thumbnail_src ) {
  970. $_media_item['thumbnail'] = $thumbnail_src[0];
  971. } else {
  972. $_media_item['thumbnail'] = $_media_item['link'];
  973. }
  974. /**
  975. * Filters XML-RPC-prepared data for the given media item.
  976. *
  977. * @since 3.4.0
  978. *
  979. * @param array $_media_item An array of media item data.
  980. * @param WP_Post $media_item Media item object.
  981. * @param string $thumbnail_size Image size.
  982. */
  983. return apply_filters( 'xmlrpc_prepare_media_item', $_media_item, $media_item, $thumbnail_size );
  984. }
  985. /**
  986. * Prepares page data for return in an XML-RPC object.
  987. *
  988. * @param WP_Post $page The unprepared page data.
  989. * @return array The prepared page data.
  990. */
  991. protected function _prepare_page( $page ) {
  992. // Get all of the page content and link.
  993. $full_page = get_extended( $page->post_content );
  994. $link = get_permalink( $page->ID );
  995. // Get info the page parent if there is one.
  996. $parent_title = '';
  997. if ( ! empty( $page->post_parent ) ) {
  998. $parent = get_post( $page->post_parent );
  999. $parent_title = $parent->post_title;
  1000. }
  1001. // Determine comment and ping settings.
  1002. $allow_comments = comments_open( $page->ID ) ? 1 : 0;
  1003. $allow_pings = pings_open( $page->ID ) ? 1 : 0;
  1004. // Format page date.
  1005. $page_date = $this->_convert_date( $page->post_date );
  1006. $page_date_gmt = $this->_convert_date_gmt( $page->post_date_gmt, $page->post_date );
  1007. // Pull the categories info together.
  1008. $categories = array();
  1009. if ( is_object_in_taxonomy( 'page', 'category' ) ) {
  1010. foreach ( wp_get_post_categories( $page->ID ) as $cat_id ) {
  1011. $categories[] = get_cat_name( $cat_id );
  1012. }
  1013. }
  1014. // Get the author info.
  1015. $author = get_userdata( $page->post_author );
  1016. $page_template = get_page_template_slug( $page->ID );
  1017. if ( empty( $page_template ) ) {
  1018. $page_template = 'default';
  1019. }
  1020. $_page = array(
  1021. 'dateCreated' => $page_date,
  1022. 'userid' => $page->post_author,
  1023. 'page_id' => $page->ID,
  1024. 'page_status' => $page->post_status,
  1025. 'description' => $full_page['main'],
  1026. 'title' => $page->post_title,
  1027. 'link' => $link,
  1028. 'permaLink' => $link,
  1029. 'categories' => $categories,
  1030. 'excerpt' => $page->post_excerpt,
  1031. 'text_more' => $full_page['extended'],
  1032. 'mt_allow_comments' => $allow_comments,
  1033. 'mt_allow_pings' => $allow_pings,
  1034. 'wp_slug' => $page->post_name,
  1035. 'wp_password' => $page->post_password,
  1036. 'wp_author' => $author->display_name,
  1037. 'wp_page_parent_id' => $page->post_parent,
  1038. 'wp_page_parent_title' => $parent_title,
  1039. 'wp_page_order' => $page->menu_order,
  1040. 'wp_author_id' => (string) $author->ID,
  1041. 'wp_author_display_name' => $author->display_name,
  1042. 'date_created_gmt' => $page_date_gmt,
  1043. 'custom_fields' => $this->get_custom_fields( $page->ID ),
  1044. 'wp_page_template' => $page_template,
  1045. );
  1046. /**
  1047. * Filters XML-RPC-prepared data for the given page.
  1048. *
  1049. * @since 3.4.0
  1050. *
  1051. * @param array $_page An array of page data.
  1052. * @param WP_Post $page Page object.
  1053. */
  1054. return apply_filters( 'xmlrpc_prepare_page', $_page, $page );
  1055. }
  1056. /**
  1057. * Prepares comment data for return in an XML-RPC object.
  1058. *
  1059. * @param WP_Comment $comment The unprepared comment data.
  1060. * @return array The prepared comment data.
  1061. */
  1062. protected function _prepare_comment( $comment ) {
  1063. // Format page date.
  1064. $comment_date_gmt = $this->_convert_date_gmt( $comment->comment_date_gmt, $comment->comment_date );
  1065. if ( '0' == $comment->comment_approved ) {
  1066. $comment_status = 'hold';
  1067. } elseif ( 'spam' === $comment->comment_approved ) {
  1068. $comment_status = 'spam';
  1069. } elseif ( '1' == $comment->comment_approved ) {
  1070. $comment_status = 'approve';
  1071. } else {
  1072. $comment_status = $comment->comment_approved;
  1073. }
  1074. $_comment = array(
  1075. 'date_created_gmt' => $comment_date_gmt,
  1076. 'user_id' => $comment->user_id,
  1077. 'comment_id' => $comment->comment_ID,
  1078. 'parent' => $comment->comment_parent,
  1079. 'status' => $comment_status,
  1080. 'content' => $comment->comment_content,
  1081. 'link' => get_comment_link( $comment ),
  1082. 'post_id' => $comment->comment_post_ID,
  1083. 'post_title' => get_the_title( $comment->comment_post_ID ),
  1084. 'author' => $comment->comment_author,
  1085. 'author_url' => $comment->comment_author_url,
  1086. 'author_email' => $comment->comment_author_email,
  1087. 'author_ip' => $comment->comment_author_IP,
  1088. 'type' => $comment->comment_type,
  1089. );
  1090. /**
  1091. * Filters XML-RPC-prepared data for the given comment.
  1092. *
  1093. * @since 3.4.0
  1094. *
  1095. * @param array $_comment An array of prepared comment data.
  1096. * @param WP_Comment $comment Comment object.
  1097. */
  1098. return apply_filters( 'xmlrpc_prepare_comment', $_comment, $comment );
  1099. }
  1100. /**
  1101. * Prepares user data for return in an XML-RPC object.
  1102. *
  1103. * @param WP_User $user The unprepared user object.
  1104. * @param array $fields The subset of user fields to return.
  1105. * @return array The prepared user data.
  1106. */
  1107. protected function _prepare_user( $user, $fields ) {
  1108. $_user = array( 'user_id' => (string) $user->ID );
  1109. $user_fields = array(
  1110. 'username' => $user->user_login,
  1111. 'first_name' => $user->user_firstname,
  1112. 'last_name' => $user->user_lastname,
  1113. 'registered' => $this->_convert_date( $user->user_registered ),
  1114. 'bio' => $user->user_description,
  1115. 'email' => $user->user_email,
  1116. 'nickname' => $user->nickname,
  1117. 'nicename' => $user->user_nicename,
  1118. 'url' => $user->user_url,
  1119. 'display_name' => $user->display_name,
  1120. 'roles' => $user->roles,
  1121. );
  1122. if ( in_array( 'all', $fields, true ) ) {
  1123. $_user = array_merge( $_user, $user_fields );
  1124. } else {
  1125. if ( in_array( 'basic', $fields, true ) ) {
  1126. $basic_fields = array( 'username', 'email', 'registered', 'display_name', 'nicename' );
  1127. $fields = array_merge( $fields, $basic_fields );
  1128. }
  1129. $requested_fields = array_intersect_key( $user_fields, array_flip( $fields ) );
  1130. $_user = array_merge( $_user, $requested_fields );
  1131. }
  1132. /**
  1133. * Filters XML-RPC-prepared data for the given user.
  1134. *
  1135. * @since 3.5.0
  1136. *
  1137. * @param array $_user An array of user data.
  1138. * @param WP_User $user User object.
  1139. * @param array $fields An array of user fields.
  1140. */
  1141. return apply_filters( 'xmlrpc_prepare_user', $_user, $user, $fields );
  1142. }
  1143. /**
  1144. * Create a new post for any registered post type.
  1145. *
  1146. * @since 3.4.0
  1147. *
  1148. * @link https://en.wikipedia.org/wiki/RSS_enclosure for information on RSS enclosures.
  1149. *
  1150. * @param array $args {
  1151. * Method arguments. Note: top-level arguments must be ordered as documented.
  1152. *
  1153. * @type int $blog_id Blog ID (unused).
  1154. * @type string $username Username.
  1155. * @type string $password Password.
  1156. * @type array $content_struct {
  1157. * Content struct for adding a new post. See wp_insert_post() for information on
  1158. * additional post fields
  1159. *
  1160. * @type string $post_type Post type. Default 'post'.
  1161. * @type string $post_status Post status. Default 'draft'
  1162. * @type string $post_title Post title.
  1163. * @type int $post_author Post author ID.
  1164. * @type string $post_excerpt Post excerpt.
  1165. * @type string $post_content Post content.
  1166. * @type string $post_date_gmt Post date in GMT.
  1167. * @type string $post_date Post date.
  1168. * @type string $post_password Post password (20-character limit).
  1169. * @type string $comment_status Post comment enabled status. Accepts 'open' or 'closed'.
  1170. * @type string $ping_status Post ping status. Accepts 'open' or 'closed'.
  1171. * @type bool $sticky Whether the post should be sticky. Automatically false if
  1172. * `$post_status` is 'private'.
  1173. * @type int $post_thumbnail ID of an image to use as the post thumbnail/featured image.
  1174. * @type array $custom_fields Array of meta key/value pairs to add to the post.
  1175. * @type array $terms Associative array with taxonomy names as keys and arrays
  1176. * of term IDs as values.
  1177. * @type array $terms_names Associative array with taxonomy names as keys and arrays
  1178. * of term names as values.
  1179. * @type array $enclosure {
  1180. * Array of feed enclosure data to add to post meta.
  1181. *
  1182. * @type string $url URL for the feed enclosure.
  1183. * @type int $length Size in bytes of the enclosure.
  1184. * @type string $type Mime-type for the enclosure.
  1185. * }
  1186. * }
  1187. * }
  1188. * @return int|IXR_Error Post ID on success, IXR_Error instance otherwise.
  1189. */
  1190. public function wp_newPost( $args ) {
  1191. if ( ! $this->minimum_args( $args, 4 ) ) {
  1192. return $this->error;
  1193. }
  1194. $this->escape( $args );
  1195. $username = $args[1];
  1196. $password = $args[2];
  1197. $content_struct = $args[3];
  1198. $user = $this->login( $username, $password );
  1199. if ( ! $user ) {
  1200. return $this->error;
  1201. }
  1202. // Convert the date field back to IXR form.
  1203. if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) {
  1204. $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] );
  1205. }
  1206. /*
  1207. * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
  1208. * since _insert_post() will ignore the non-GMT date if the GMT date is set.
  1209. */
  1210. if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) {
  1211. if ( '0000-00-00 00:00:00' === $content_struct['post_date_gmt'] || isset( $content_struct['post_date'] ) ) {
  1212. unset( $content_struct['post_date_gmt'] );
  1213. } else {
  1214. $content_struct['post_date_gmt'] = $this->_convert_date( $content_struct['post_date_gmt'] );
  1215. }
  1216. }
  1217. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1218. do_action( 'xmlrpc_call', 'wp.newPost', $args, $this );
  1219. unset( $content_struct['ID'] );
  1220. return $this->_insert_post( $user, $content_struct );
  1221. }
  1222. /**
  1223. * Helper method for filtering out elements from an array.
  1224. *
  1225. * @since 3.4.0
  1226. *
  1227. * @param int $count Number to compare to one.
  1228. * @return bool True if the number is greater than one, false otherwise.
  1229. */
  1230. private function _is_greater_than_one( $count ) {
  1231. return $count > 1;
  1232. }
  1233. /**
  1234. * Encapsulate the logic for sticking a post
  1235. * and determining if the user has permission to do so
  1236. *
  1237. * @since 4.3.0
  1238. *
  1239. * @param array $post_data
  1240. * @param bool $update
  1241. * @return void|IXR_Error
  1242. */
  1243. private function _toggle_sticky( $post_data, $update = false ) {
  1244. $post_type = get_post_type_object( $post_data['post_type'] );
  1245. // Private and password-protected posts cannot be stickied.
  1246. if ( 'private' === $post_data['post_status'] || ! empty( $post_data['post_password'] ) ) {
  1247. // Error if the client tried to stick the post, otherwise, silently unstick.
  1248. if ( ! empty( $post_data['sticky'] ) ) {
  1249. return new IXR_Error( 401, __( 'Sorry, you cannot stick a private post.' ) );
  1250. }
  1251. if ( $update ) {
  1252. unstick_post( $post_data['ID'] );
  1253. }
  1254. } elseif ( isset( $post_data['sticky'] ) ) {
  1255. if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
  1256. return new IXR_Error( 401, __( 'Sorry, you are not allowed to make posts sticky.' ) );
  1257. }
  1258. $sticky = wp_validate_boolean( $post_data['sticky'] );
  1259. if ( $sticky ) {
  1260. stick_post( $post_data['ID'] );
  1261. } else {
  1262. unstick_post( $post_data['ID'] );
  1263. }
  1264. }
  1265. }
  1266. /**
  1267. * Helper method for wp_newPost() and wp_editPost(), containing shared logic.
  1268. *
  1269. * @since 3.4.0
  1270. *
  1271. * @see wp_insert_post()
  1272. *
  1273. * @param WP_User $user The post author if post_author isn't set in $content_struct.
  1274. * @param array|IXR_Error $content_struct Post data to insert.
  1275. * @return IXR_Error|string
  1276. */
  1277. protected function _insert_post( $user, $content_struct ) {
  1278. $defaults = array(
  1279. 'post_status' => 'draft',
  1280. 'post_type' => 'post',
  1281. 'post_author' => null,
  1282. 'post_password' => null,
  1283. 'post_excerpt' => null,
  1284. 'post_content' => null,
  1285. 'post_title' => null,
  1286. 'post_date' => null,
  1287. 'post_date_gmt' => null,
  1288. 'post_format' => null,
  1289. 'post_name' => null,
  1290. 'post_thumbnail' => null,
  1291. 'post_parent' => null,
  1292. 'ping_status' => null,
  1293. 'comment_status' => null,
  1294. 'custom_fields' => null,
  1295. 'terms_names' => null,
  1296. 'terms' => null,
  1297. 'sticky' => null,
  1298. 'enclosure' => null,
  1299. 'ID' => null,
  1300. );
  1301. $post_data = wp_parse_args( array_intersect_key( $content_struct, $defaults ), $defaults );
  1302. $post_type = get_post_type_object( $post_data['post_type'] );
  1303. if ( ! $post_type ) {
  1304. return new IXR_Error( 403, __( 'Invalid post type.' ) );
  1305. }
  1306. $update = ! empty( $post_data['ID'] );
  1307. if ( $update ) {
  1308. if ( ! get_post( $post_data['ID'] ) ) {
  1309. return new IXR_Error( 401, __( 'Invalid post ID.' ) );
  1310. }
  1311. if ( ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
  1312. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  1313. }
  1314. if ( get_post_type( $post_data['ID'] ) !== $post_data['post_type'] ) {
  1315. return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
  1316. }
  1317. } else {
  1318. if ( ! current_user_can( $post_type->cap->create_posts ) || ! current_user_can( $post_type->cap->edit_posts ) ) {
  1319. return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
  1320. }
  1321. }
  1322. switch ( $post_data['post_status'] ) {
  1323. case 'draft':
  1324. case 'pending':
  1325. break;
  1326. case 'private':
  1327. if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
  1328. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type.' ) );
  1329. }
  1330. break;
  1331. case 'publish':
  1332. case 'future':
  1333. if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
  1334. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type.' ) );
  1335. }
  1336. break;
  1337. default:
  1338. if ( ! get_post_status_object( $post_data['post_status'] ) ) {
  1339. $post_data['post_status'] = 'draft';
  1340. }
  1341. break;
  1342. }
  1343. if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) ) {
  1344. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type.' ) );
  1345. }
  1346. $post_data['post_author'] = absint( $post_data['post_author'] );
  1347. if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
  1348. if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) {
  1349. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts as this user.' ) );
  1350. }
  1351. $author = get_userdata( $post_data['post_author'] );
  1352. if ( ! $author ) {
  1353. return new IXR_Error( 404, __( 'Invalid author ID.' ) );
  1354. }
  1355. } else {
  1356. $post_data['post_author'] = $user->ID;
  1357. }
  1358. if ( isset( $post_data['comment_status'] ) && 'open' !== $post_data['comment_status'] && 'closed' !== $post_data['comment_status'] ) {
  1359. unset( $post_data['comment_status'] );
  1360. }
  1361. if ( isset( $post_data['ping_status'] ) && 'open' !== $post_data['ping_status'] && 'closed' !== $post_data['ping_status'] ) {
  1362. unset( $post_data['ping_status'] );
  1363. }
  1364. // Do some timestamp voodoo.
  1365. if ( ! empty( $post_data['post_date_gmt'] ) ) {
  1366. // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
  1367. $dateCreated = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z';
  1368. } elseif ( ! empty( $post_data['post_date'] ) ) {
  1369. $dateCreated = $post_data['post_date']->getIso();
  1370. }
  1371. // Default to not flagging the post date to be edited unless it's intentional.
  1372. $post_data['edit_date'] = false;
  1373. if ( ! empty( $dateCreated ) ) {
  1374. $post_data['post_date'] = iso8601_to_datetime( $dateCreated );
  1375. $post_data['post_date_gmt'] = iso8601_to_datetime( $dateCreated, 'gmt' );
  1376. // Flag the post date to be edited.
  1377. $post_data['edit_date'] = true;
  1378. }
  1379. if ( ! isset( $post_data['ID'] ) ) {
  1380. $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
  1381. }
  1382. $post_ID = $post_data['ID'];
  1383. if ( 'post' === $post_data['post_type'] ) {
  1384. $error = $this->_toggle_sticky( $post_data, $update );
  1385. if ( $error ) {
  1386. return $error;
  1387. }
  1388. }
  1389. if ( isset( $post_data['post_thumbnail'] ) ) {
  1390. // Empty value deletes, non-empty value adds/updates.
  1391. if ( ! $post_data['post_thumbnail'] ) {
  1392. delete_post_thumbnail( $post_ID );
  1393. } elseif ( ! get_post( absint( $post_data['post_thumbnail'] ) ) ) {
  1394. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  1395. }
  1396. set_post_thumbnail( $post_ID, $post_data['post_thumbnail'] );
  1397. unset( $content_struct['post_thumbnail'] );
  1398. }
  1399. if ( isset( $post_data['custom_fields'] ) ) {
  1400. $this->set_custom_fields( $post_ID, $post_data['custom_fields'] );
  1401. }
  1402. if ( isset( $post_data['terms'] ) || isset( $post_data['terms_names'] ) ) {
  1403. $post_type_taxonomies = get_object_taxonomies( $post_data['post_type'], 'objects' );
  1404. // Accumulate term IDs from terms and terms_names.
  1405. $terms = array();
  1406. // First validate the terms specified by ID.
  1407. if ( isset( $post_data['terms'] ) && is_array( $post_data['terms'] ) ) {
  1408. $taxonomies = array_keys( $post_data['terms'] );
  1409. // Validating term IDs.
  1410. foreach ( $taxonomies as $taxonomy ) {
  1411. if ( ! array_key_exists( $taxonomy, $post_type_taxonomies ) ) {
  1412. return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
  1413. }
  1414. if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->assign_terms ) ) {
  1415. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
  1416. }
  1417. $term_ids = $post_data['terms'][ $taxonomy ];
  1418. $terms[ $taxonomy ] = array();
  1419. foreach ( $term_ids as $term_id ) {
  1420. $term = get_term_by( 'id', $term_id, $taxonomy );
  1421. if ( ! $term ) {
  1422. return new IXR_Error( 403, __( 'Invalid term ID.' ) );
  1423. }
  1424. $terms[ $taxonomy ][] = (int) $term_id;
  1425. }
  1426. }
  1427. }
  1428. // Now validate terms specified by name.
  1429. if ( isset( $post_data['terms_names'] ) && is_array( $post_data['terms_names'] ) ) {
  1430. $taxonomies = array_keys( $post_data['terms_names'] );
  1431. foreach ( $taxonomies as $taxonomy ) {
  1432. if ( ! array_key_exists( $taxonomy, $post_type_taxonomies ) ) {
  1433. return new IXR_Error( 401, __( 'Sorry, one of the given taxonomies is not supported by the post type.' ) );
  1434. }
  1435. if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->assign_terms ) ) {
  1436. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign a term to one of the given taxonomies.' ) );
  1437. }
  1438. /*
  1439. * For hierarchical taxonomies, we can't assign a term when multiple terms
  1440. * in the hierarchy share the same name.
  1441. */
  1442. $ambiguous_terms = array();
  1443. if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  1444. $tax_term_names = get_terms(
  1445. array(
  1446. 'taxonomy' => $taxonomy,
  1447. 'fields' => 'names',
  1448. 'hide_empty' => false,
  1449. )
  1450. );
  1451. // Count the number of terms with the same name.
  1452. $tax_term_names_count = array_count_values( $tax_term_names );
  1453. // Filter out non-ambiguous term names.
  1454. $ambiguous_tax_term_counts = array_filter( $tax_term_names_count, array( $this, '_is_greater_than_one' ) );
  1455. $ambiguous_terms = array_keys( $ambiguous_tax_term_counts );
  1456. }
  1457. $term_names = $post_data['terms_names'][ $taxonomy ];
  1458. foreach ( $term_names as $term_name ) {
  1459. if ( in_array( $term_name, $ambiguous_terms, true ) ) {
  1460. return new IXR_Error( 401, __( 'Ambiguous term name used in a hierarchical taxonomy. Please use term ID instead.' ) );
  1461. }
  1462. $term = get_term_by( 'name', $term_name, $taxonomy );
  1463. if ( ! $term ) {
  1464. // Term doesn't exist, so check that the user is allowed to create new terms.
  1465. if ( ! current_user_can( $post_type_taxonomies[ $taxonomy ]->cap->edit_terms ) ) {
  1466. return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a term to one of the given taxonomies.' ) );
  1467. }
  1468. // Create the new term.
  1469. $term_info = wp_insert_term( $term_name, $taxonomy );
  1470. if ( is_wp_error( $term_info ) ) {
  1471. return new IXR_Error( 500, $term_info->get_error_message() );
  1472. }
  1473. $terms[ $taxonomy ][] = (int) $term_info['term_id'];
  1474. } else {
  1475. $terms[ $taxonomy ][] = (int) $term->term_id;
  1476. }
  1477. }
  1478. }
  1479. }
  1480. $post_data['tax_input'] = $terms;
  1481. unset( $post_data['terms'], $post_data['terms_names'] );
  1482. }
  1483. if ( isset( $post_data['post_format'] ) ) {
  1484. $format = set_post_format( $post_ID, $post_data['post_format'] );
  1485. if ( is_wp_error( $format ) ) {
  1486. return new IXR_Error( 500, $format->get_error_message() );
  1487. }
  1488. unset( $post_data['post_format'] );
  1489. }
  1490. // Handle enclosures.
  1491. $enclosure = isset( $post_data['enclosure'] ) ? $post_data['enclosure'] : null;
  1492. $this->add_enclosure_if_new( $post_ID, $enclosure );
  1493. $this->attach_uploads( $post_ID, $post_data['post_content'] );
  1494. /**
  1495. * Filters post data array to be inserted via XML-RPC.
  1496. *
  1497. * @since 3.4.0
  1498. *
  1499. * @param array $post_data Parsed array of post data.
  1500. * @param array $content_struct Post data array.
  1501. */
  1502. $post_data = apply_filters( 'xmlrpc_wp_insert_post_data', $post_data, $content_struct );
  1503. $post_ID = $update ? wp_update_post( $post_data, true ) : wp_insert_post( $post_data, true );
  1504. if ( is_wp_error( $post_ID ) ) {
  1505. return new IXR_Error( 500, $post_ID->get_error_message() );
  1506. }
  1507. if ( ! $post_ID ) {
  1508. if ( $update ) {
  1509. return new IXR_Error( 401, __( 'Sorry, the post could not be updated.' ) );
  1510. } else {
  1511. return new IXR_Error( 401, __( 'Sorry, the post could not be created.' ) );
  1512. }
  1513. }
  1514. return (string) $post_ID;
  1515. }
  1516. /**
  1517. * Edit a post for any registered post type.
  1518. *
  1519. * The $content_struct parameter only needs to contain fields that
  1520. * should be changed. All other fields will retain their existing values.
  1521. *
  1522. * @since 3.4.0
  1523. *
  1524. * @param array $args {
  1525. * Method arguments. Note: arguments must be ordered as documented.
  1526. *
  1527. * @type int $blog_id Blog ID (unused).
  1528. * @type string $username Username.
  1529. * @type string $password Password.
  1530. * @type int $post_id Post ID.
  1531. * @type array $content_struct Extra content arguments.
  1532. * }
  1533. * @return true|IXR_Error True on success, IXR_Error on failure.
  1534. */
  1535. public function wp_editPost( $args ) {
  1536. if ( ! $this->minimum_args( $args, 5 ) ) {
  1537. return $this->error;
  1538. }
  1539. $this->escape( $args );
  1540. $username = $args[1];
  1541. $password = $args[2];
  1542. $post_id = (int) $args[3];
  1543. $content_struct = $args[4];
  1544. $user = $this->login( $username, $password );
  1545. if ( ! $user ) {
  1546. return $this->error;
  1547. }
  1548. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1549. do_action( 'xmlrpc_call', 'wp.editPost', $args, $this );
  1550. $post = get_post( $post_id, ARRAY_A );
  1551. if ( empty( $post['ID'] ) ) {
  1552. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1553. }
  1554. if ( isset( $content_struct['if_not_modified_since'] ) ) {
  1555. // If the post has been modified since the date provided, return an error.
  1556. if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) {
  1557. return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) );
  1558. }
  1559. }
  1560. // Convert the date field back to IXR form.
  1561. $post['post_date'] = $this->_convert_date( $post['post_date'] );
  1562. /*
  1563. * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct,
  1564. * since _insert_post() will ignore the non-GMT date if the GMT date is set.
  1565. */
  1566. if ( '0000-00-00 00:00:00' === $post['post_date_gmt'] || isset( $content_struct['post_date'] ) ) {
  1567. unset( $post['post_date_gmt'] );
  1568. } else {
  1569. $post['post_date_gmt'] = $this->_convert_date( $post['post_date_gmt'] );
  1570. }
  1571. /*
  1572. * If the API client did not provide 'post_date', then we must not perpetuate the value that
  1573. * was stored in the database, or it will appear to be an intentional edit. Conveying it here
  1574. * as if it was coming from the API client will cause an otherwise zeroed out 'post_date_gmt'
  1575. * to get set with the value that was originally stored in the database when the draft was created.
  1576. */
  1577. if ( ! isset( $content_struct['post_date'] ) ) {
  1578. unset( $post['post_date'] );
  1579. }
  1580. $this->escape( $post );
  1581. $merged_content_struct = array_merge( $post, $content_struct );
  1582. $retval = $this->_insert_post( $user, $merged_content_struct );
  1583. if ( $retval instanceof IXR_Error ) {
  1584. return $retval;
  1585. }
  1586. return true;
  1587. }
  1588. /**
  1589. * Delete a post for any registered post type.
  1590. *
  1591. * @since 3.4.0
  1592. *
  1593. * @see wp_delete_post()
  1594. *
  1595. * @param array $args {
  1596. * Method arguments. Note: arguments must be ordered as documented.
  1597. *
  1598. * @type int $blog_id Blog ID (unused).
  1599. * @type string $username Username.
  1600. * @type string $password Password.
  1601. * @type int $post_id Post ID.
  1602. * }
  1603. * @return true|IXR_Error True on success, IXR_Error instance on failure.
  1604. */
  1605. public function wp_deletePost( $args ) {
  1606. if ( ! $this->minimum_args( $args, 4 ) ) {
  1607. return $this->error;
  1608. }
  1609. $this->escape( $args );
  1610. $username = $args[1];
  1611. $password = $args[2];
  1612. $post_id = (int) $args[3];
  1613. $user = $this->login( $username, $password );
  1614. if ( ! $user ) {
  1615. return $this->error;
  1616. }
  1617. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1618. do_action( 'xmlrpc_call', 'wp.deletePost', $args, $this );
  1619. $post = get_post( $post_id, ARRAY_A );
  1620. if ( empty( $post['ID'] ) ) {
  1621. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1622. }
  1623. if ( ! current_user_can( 'delete_post', $post_id ) ) {
  1624. return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
  1625. }
  1626. $result = wp_delete_post( $post_id );
  1627. if ( ! $result ) {
  1628. return new IXR_Error( 500, __( 'Sorry, the post could not be deleted.' ) );
  1629. }
  1630. return true;
  1631. }
  1632. /**
  1633. * Retrieve a post.
  1634. *
  1635. * @since 3.4.0
  1636. *
  1637. * The optional $fields parameter specifies what fields will be included
  1638. * in the response array. This should be a list of field names. 'post_id' will
  1639. * always be included in the response regardless of the value of $fields.
  1640. *
  1641. * Instead of, or in addition to, individual field names, conceptual group
  1642. * names can be used to specify multiple fields. The available conceptual
  1643. * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields',
  1644. * and 'enclosure'.
  1645. *
  1646. * @see get_post()
  1647. *
  1648. * @param array $args {
  1649. * Method arguments. Note: arguments must be ordered as documented.
  1650. *
  1651. * @type int $blog_id Blog ID (unused).
  1652. * @type string $username Username.
  1653. * @type string $password Password.
  1654. * @type int $post_id Post ID.
  1655. * @type array $fields The subset of post type fields to return.
  1656. * }
  1657. * @return array|IXR_Error Array contains (based on $fields parameter):
  1658. * - 'post_id'
  1659. * - 'post_title'
  1660. * - 'post_date'
  1661. * - 'post_date_gmt'
  1662. * - 'post_modified'
  1663. * - 'post_modified_gmt'
  1664. * - 'post_status'
  1665. * - 'post_type'
  1666. * - 'post_name'
  1667. * - 'post_author'
  1668. * - 'post_password'
  1669. * - 'post_excerpt'
  1670. * - 'post_content'
  1671. * - 'link'
  1672. * - 'comment_status'
  1673. * - 'ping_status'
  1674. * - 'sticky'
  1675. * - 'custom_fields'
  1676. * - 'terms'
  1677. * - 'categories'
  1678. * - 'tags'
  1679. * - 'enclosure'
  1680. */
  1681. public function wp_getPost( $args ) {
  1682. if ( ! $this->minimum_args( $args, 4 ) ) {
  1683. return $this->error;
  1684. }
  1685. $this->escape( $args );
  1686. $username = $args[1];
  1687. $password = $args[2];
  1688. $post_id = (int) $args[3];
  1689. if ( isset( $args[4] ) ) {
  1690. $fields = $args[4];
  1691. } else {
  1692. /**
  1693. * Filters the list of post query fields used by the given XML-RPC method.
  1694. *
  1695. * @since 3.4.0
  1696. *
  1697. * @param array $fields Array of post fields. Default array contains 'post', 'terms', and 'custom_fields'.
  1698. * @param string $method Method name.
  1699. */
  1700. $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPost' );
  1701. }
  1702. $user = $this->login( $username, $password );
  1703. if ( ! $user ) {
  1704. return $this->error;
  1705. }
  1706. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1707. do_action( 'xmlrpc_call', 'wp.getPost', $args, $this );
  1708. $post = get_post( $post_id, ARRAY_A );
  1709. if ( empty( $post['ID'] ) ) {
  1710. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  1711. }
  1712. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  1713. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  1714. }
  1715. return $this->_prepare_post( $post, $fields );
  1716. }
  1717. /**
  1718. * Retrieve posts.
  1719. *
  1720. * @since 3.4.0
  1721. *
  1722. * @see wp_get_recent_posts()
  1723. * @see wp_getPost() for more on `$fields`
  1724. * @see get_posts() for more on `$filter` values
  1725. *
  1726. * @param array $args {
  1727. * Method arguments. Note: arguments must be ordered as documented.
  1728. *
  1729. * @type int $blog_id Blog ID (unused).
  1730. * @type string $username Username.
  1731. * @type string $password Password.
  1732. * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'post_type',
  1733. * 'post_status', 'number', 'offset', 'orderby', 's', and 'order'.
  1734. * Default empty array.
  1735. * @type array $fields Optional. The subset of post type fields to return in the response array.
  1736. * }
  1737. * @return array|IXR_Error Array contains a collection of posts.
  1738. */
  1739. public function wp_getPosts( $args ) {
  1740. if ( ! $this->minimum_args( $args, 3 ) ) {
  1741. return $this->error;
  1742. }
  1743. $this->escape( $args );
  1744. $username = $args[1];
  1745. $password = $args[2];
  1746. $filter = isset( $args[3] ) ? $args[3] : array();
  1747. if ( isset( $args[4] ) ) {
  1748. $fields = $args[4];
  1749. } else {
  1750. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1751. $fields = apply_filters( 'xmlrpc_default_post_fields', array( 'post', 'terms', 'custom_fields' ), 'wp.getPosts' );
  1752. }
  1753. $user = $this->login( $username, $password );
  1754. if ( ! $user ) {
  1755. return $this->error;
  1756. }
  1757. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1758. do_action( 'xmlrpc_call', 'wp.getPosts', $args, $this );
  1759. $query = array();
  1760. if ( isset( $filter['post_type'] ) ) {
  1761. $post_type = get_post_type_object( $filter['post_type'] );
  1762. if ( ! ( (bool) $post_type ) ) {
  1763. return new IXR_Error( 403, __( 'Invalid post type.' ) );
  1764. }
  1765. } else {
  1766. $post_type = get_post_type_object( 'post' );
  1767. }
  1768. if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
  1769. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
  1770. }
  1771. $query['post_type'] = $post_type->name;
  1772. if ( isset( $filter['post_status'] ) ) {
  1773. $query['post_status'] = $filter['post_status'];
  1774. }
  1775. if ( isset( $filter['number'] ) ) {
  1776. $query['numberposts'] = absint( $filter['number'] );
  1777. }
  1778. if ( isset( $filter['offset'] ) ) {
  1779. $query['offset'] = absint( $filter['offset'] );
  1780. }
  1781. if ( isset( $filter['orderby'] ) ) {
  1782. $query['orderby'] = $filter['orderby'];
  1783. if ( isset( $filter['order'] ) ) {
  1784. $query['order'] = $filter['order'];
  1785. }
  1786. }
  1787. if ( isset( $filter['s'] ) ) {
  1788. $query['s'] = $filter['s'];
  1789. }
  1790. $posts_list = wp_get_recent_posts( $query );
  1791. if ( ! $posts_list ) {
  1792. return array();
  1793. }
  1794. // Holds all the posts data.
  1795. $struct = array();
  1796. foreach ( $posts_list as $post ) {
  1797. if ( ! current_user_can( 'edit_post', $post['ID'] ) ) {
  1798. continue;
  1799. }
  1800. $struct[] = $this->_prepare_post( $post, $fields );
  1801. }
  1802. return $struct;
  1803. }
  1804. /**
  1805. * Create a new term.
  1806. *
  1807. * @since 3.4.0
  1808. *
  1809. * @see wp_insert_term()
  1810. *
  1811. * @param array $args {
  1812. * Method arguments. Note: arguments must be ordered as documented.
  1813. *
  1814. * @type int $blog_id Blog ID (unused).
  1815. * @type string $username Username.
  1816. * @type string $password Password.
  1817. * @type array $content_struct Content struct for adding a new term. The struct must contain
  1818. * the term 'name' and 'taxonomy'. Optional accepted values include
  1819. * 'parent', 'description', and 'slug'.
  1820. * }
  1821. * @return int|IXR_Error The term ID on success, or an IXR_Error object on failure.
  1822. */
  1823. public function wp_newTerm( $args ) {
  1824. if ( ! $this->minimum_args( $args, 4 ) ) {
  1825. return $this->error;
  1826. }
  1827. $this->escape( $args );
  1828. $username = $args[1];
  1829. $password = $args[2];
  1830. $content_struct = $args[3];
  1831. $user = $this->login( $username, $password );
  1832. if ( ! $user ) {
  1833. return $this->error;
  1834. }
  1835. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1836. do_action( 'xmlrpc_call', 'wp.newTerm', $args, $this );
  1837. if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) {
  1838. return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  1839. }
  1840. $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
  1841. if ( ! current_user_can( $taxonomy->cap->edit_terms ) ) {
  1842. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create terms in this taxonomy.' ) );
  1843. }
  1844. $taxonomy = (array) $taxonomy;
  1845. // Hold the data of the term.
  1846. $term_data = array();
  1847. $term_data['name'] = trim( $content_struct['name'] );
  1848. if ( empty( $term_data['name'] ) ) {
  1849. return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
  1850. }
  1851. if ( isset( $content_struct['parent'] ) ) {
  1852. if ( ! $taxonomy['hierarchical'] ) {
  1853. return new IXR_Error( 403, __( 'This taxonomy is not hierarchical.' ) );
  1854. }
  1855. $parent_term_id = (int) $content_struct['parent'];
  1856. $parent_term = get_term( $parent_term_id, $taxonomy['name'] );
  1857. if ( is_wp_error( $parent_term ) ) {
  1858. return new IXR_Error( 500, $parent_term->get_error_message() );
  1859. }
  1860. if ( ! $parent_term ) {
  1861. return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
  1862. }
  1863. $term_data['parent'] = $content_struct['parent'];
  1864. }
  1865. if ( isset( $content_struct['description'] ) ) {
  1866. $term_data['description'] = $content_struct['description'];
  1867. }
  1868. if ( isset( $content_struct['slug'] ) ) {
  1869. $term_data['slug'] = $content_struct['slug'];
  1870. }
  1871. $term = wp_insert_term( $term_data['name'], $taxonomy['name'], $term_data );
  1872. if ( is_wp_error( $term ) ) {
  1873. return new IXR_Error( 500, $term->get_error_message() );
  1874. }
  1875. if ( ! $term ) {
  1876. return new IXR_Error( 500, __( 'Sorry, the term could not be created.' ) );
  1877. }
  1878. // Add term meta.
  1879. if ( isset( $content_struct['custom_fields'] ) ) {
  1880. $this->set_term_custom_fields( $term['term_id'], $content_struct['custom_fields'] );
  1881. }
  1882. return (string) $term['term_id'];
  1883. }
  1884. /**
  1885. * Edit a term.
  1886. *
  1887. * @since 3.4.0
  1888. *
  1889. * @see wp_update_term()
  1890. *
  1891. * @param array $args {
  1892. * Method arguments. Note: arguments must be ordered as documented.
  1893. *
  1894. * @type int $blog_id Blog ID (unused).
  1895. * @type string $username Username.
  1896. * @type string $password Password.
  1897. * @type int $term_id Term ID.
  1898. * @type array $content_struct Content struct for editing a term. The struct must contain the
  1899. * term ''taxonomy'. Optional accepted values include 'name', 'parent',
  1900. * 'description', and 'slug'.
  1901. * }
  1902. * @return true|IXR_Error True on success, IXR_Error instance on failure.
  1903. */
  1904. public function wp_editTerm( $args ) {
  1905. if ( ! $this->minimum_args( $args, 5 ) ) {
  1906. return $this->error;
  1907. }
  1908. $this->escape( $args );
  1909. $username = $args[1];
  1910. $password = $args[2];
  1911. $term_id = (int) $args[3];
  1912. $content_struct = $args[4];
  1913. $user = $this->login( $username, $password );
  1914. if ( ! $user ) {
  1915. return $this->error;
  1916. }
  1917. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  1918. do_action( 'xmlrpc_call', 'wp.editTerm', $args, $this );
  1919. if ( ! taxonomy_exists( $content_struct['taxonomy'] ) ) {
  1920. return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  1921. }
  1922. $taxonomy = get_taxonomy( $content_struct['taxonomy'] );
  1923. $taxonomy = (array) $taxonomy;
  1924. // Hold the data of the term.
  1925. $term_data = array();
  1926. $term = get_term( $term_id, $content_struct['taxonomy'] );
  1927. if ( is_wp_error( $term ) ) {
  1928. return new IXR_Error( 500, $term->get_error_message() );
  1929. }
  1930. if ( ! $term ) {
  1931. return new IXR_Error( 404, __( 'Invalid term ID.' ) );
  1932. }
  1933. if ( ! current_user_can( 'edit_term', $term_id ) ) {
  1934. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this term.' ) );
  1935. }
  1936. if ( isset( $content_struct['name'] ) ) {
  1937. $term_data['name'] = trim( $content_struct['name'] );
  1938. if ( empty( $term_data['name'] ) ) {
  1939. return new IXR_Error( 403, __( 'The term name cannot be empty.' ) );
  1940. }
  1941. }
  1942. if ( ! empty( $content_struct['parent'] ) ) {
  1943. if ( ! $taxonomy['hierarchical'] ) {
  1944. return new IXR_Error( 403, __( 'Cannot set parent term, taxonomy is not hierarchical.' ) );
  1945. }
  1946. $parent_term_id = (int) $content_struct['parent'];
  1947. $parent_term = get_term( $parent_term_id, $taxonomy['name'] );
  1948. if ( is_wp_error( $parent_term ) ) {
  1949. return new IXR_Error( 500, $parent_term->get_error_message() );
  1950. }
  1951. if ( ! $parent_term ) {
  1952. return new IXR_Error( 403, __( 'Parent term does not exist.' ) );
  1953. }
  1954. $term_data['parent'] = $content_struct['parent'];
  1955. }
  1956. if ( isset( $content_struct['description'] ) ) {
  1957. $term_data['description'] = $content_struct['description'];
  1958. }
  1959. if ( isset( $content_struct['slug'] ) ) {
  1960. $term_data['slug'] = $content_struct['slug'];
  1961. }
  1962. $term = wp_update_term( $term_id, $taxonomy['name'], $term_data );
  1963. if ( is_wp_error( $term ) ) {
  1964. return new IXR_Error( 500, $term->get_error_message() );
  1965. }
  1966. if ( ! $term ) {
  1967. return new IXR_Error( 500, __( 'Sorry, editing the term failed.' ) );
  1968. }
  1969. // Update term meta.
  1970. if ( isset( $content_struct['custom_fields'] ) ) {
  1971. $this->set_term_custom_fields( $term_id, $content_struct['custom_fields'] );
  1972. }
  1973. return true;
  1974. }
  1975. /**
  1976. * Delete a term.
  1977. *
  1978. * @since 3.4.0
  1979. *
  1980. * @see wp_delete_term()
  1981. *
  1982. * @param array $args {
  1983. * Method arguments. Note: arguments must be ordered as documented.
  1984. *
  1985. * @type int $blog_id Blog ID (unused).
  1986. * @type string $username Username.
  1987. * @type string $password Password.
  1988. * @type string $taxnomy_name Taxonomy name.
  1989. * @type int $term_id Term ID.
  1990. * }
  1991. * @return true|IXR_Error True on success, IXR_Error instance on failure.
  1992. */
  1993. public function wp_deleteTerm( $args ) {
  1994. if ( ! $this->minimum_args( $args, 5 ) ) {
  1995. return $this->error;
  1996. }
  1997. $this->escape( $args );
  1998. $username = $args[1];
  1999. $password = $args[2];
  2000. $taxonomy = $args[3];
  2001. $term_id = (int) $args[4];
  2002. $user = $this->login( $username, $password );
  2003. if ( ! $user ) {
  2004. return $this->error;
  2005. }
  2006. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2007. do_action( 'xmlrpc_call', 'wp.deleteTerm', $args, $this );
  2008. if ( ! taxonomy_exists( $taxonomy ) ) {
  2009. return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2010. }
  2011. $taxonomy = get_taxonomy( $taxonomy );
  2012. $term = get_term( $term_id, $taxonomy->name );
  2013. if ( is_wp_error( $term ) ) {
  2014. return new IXR_Error( 500, $term->get_error_message() );
  2015. }
  2016. if ( ! $term ) {
  2017. return new IXR_Error( 404, __( 'Invalid term ID.' ) );
  2018. }
  2019. if ( ! current_user_can( 'delete_term', $term_id ) ) {
  2020. return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this term.' ) );
  2021. }
  2022. $result = wp_delete_term( $term_id, $taxonomy->name );
  2023. if ( is_wp_error( $result ) ) {
  2024. return new IXR_Error( 500, $term->get_error_message() );
  2025. }
  2026. if ( ! $result ) {
  2027. return new IXR_Error( 500, __( 'Sorry, deleting the term failed.' ) );
  2028. }
  2029. return $result;
  2030. }
  2031. /**
  2032. * Retrieve a term.
  2033. *
  2034. * @since 3.4.0
  2035. *
  2036. * @see get_term()
  2037. *
  2038. * @param array $args {
  2039. * Method arguments. Note: arguments must be ordered as documented.
  2040. *
  2041. * @type int $blog_id Blog ID (unused).
  2042. * @type string $username Username.
  2043. * @type string $password Password.
  2044. * @type string $taxnomy Taxonomy name.
  2045. * @type string $term_id Term ID.
  2046. * }
  2047. * @return array|IXR_Error IXR_Error on failure, array on success, containing:
  2048. * - 'term_id'
  2049. * - 'name'
  2050. * - 'slug'
  2051. * - 'term_group'
  2052. * - 'term_taxonomy_id'
  2053. * - 'taxonomy'
  2054. * - 'description'
  2055. * - 'parent'
  2056. * - 'count'
  2057. */
  2058. public function wp_getTerm( $args ) {
  2059. if ( ! $this->minimum_args( $args, 5 ) ) {
  2060. return $this->error;
  2061. }
  2062. $this->escape( $args );
  2063. $username = $args[1];
  2064. $password = $args[2];
  2065. $taxonomy = $args[3];
  2066. $term_id = (int) $args[4];
  2067. $user = $this->login( $username, $password );
  2068. if ( ! $user ) {
  2069. return $this->error;
  2070. }
  2071. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2072. do_action( 'xmlrpc_call', 'wp.getTerm', $args, $this );
  2073. if ( ! taxonomy_exists( $taxonomy ) ) {
  2074. return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2075. }
  2076. $taxonomy = get_taxonomy( $taxonomy );
  2077. $term = get_term( $term_id, $taxonomy->name, ARRAY_A );
  2078. if ( is_wp_error( $term ) ) {
  2079. return new IXR_Error( 500, $term->get_error_message() );
  2080. }
  2081. if ( ! $term ) {
  2082. return new IXR_Error( 404, __( 'Invalid term ID.' ) );
  2083. }
  2084. if ( ! current_user_can( 'assign_term', $term_id ) ) {
  2085. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign this term.' ) );
  2086. }
  2087. return $this->_prepare_term( $term );
  2088. }
  2089. /**
  2090. * Retrieve all terms for a taxonomy.
  2091. *
  2092. * @since 3.4.0
  2093. *
  2094. * The optional $filter parameter modifies the query used to retrieve terms.
  2095. * Accepted keys are 'number', 'offset', 'orderby', 'order', 'hide_empty', and 'search'.
  2096. *
  2097. * @see get_terms()
  2098. *
  2099. * @param array $args {
  2100. * Method arguments. Note: arguments must be ordered as documented.
  2101. *
  2102. * @type int $blog_id Blog ID (unused).
  2103. * @type string $username Username.
  2104. * @type string $password Password.
  2105. * @type string $taxnomy Taxonomy name.
  2106. * @type array $filter Optional. Modifies the query used to retrieve posts. Accepts 'number',
  2107. * 'offset', 'orderby', 'order', 'hide_empty', and 'search'. Default empty array.
  2108. * }
  2109. * @return array|IXR_Error An associative array of terms data on success, IXR_Error instance otherwise.
  2110. */
  2111. public function wp_getTerms( $args ) {
  2112. if ( ! $this->minimum_args( $args, 4 ) ) {
  2113. return $this->error;
  2114. }
  2115. $this->escape( $args );
  2116. $username = $args[1];
  2117. $password = $args[2];
  2118. $taxonomy = $args[3];
  2119. $filter = isset( $args[4] ) ? $args[4] : array();
  2120. $user = $this->login( $username, $password );
  2121. if ( ! $user ) {
  2122. return $this->error;
  2123. }
  2124. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2125. do_action( 'xmlrpc_call', 'wp.getTerms', $args, $this );
  2126. if ( ! taxonomy_exists( $taxonomy ) ) {
  2127. return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2128. }
  2129. $taxonomy = get_taxonomy( $taxonomy );
  2130. if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) {
  2131. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign terms in this taxonomy.' ) );
  2132. }
  2133. $query = array( 'taxonomy' => $taxonomy->name );
  2134. if ( isset( $filter['number'] ) ) {
  2135. $query['number'] = absint( $filter['number'] );
  2136. }
  2137. if ( isset( $filter['offset'] ) ) {
  2138. $query['offset'] = absint( $filter['offset'] );
  2139. }
  2140. if ( isset( $filter['orderby'] ) ) {
  2141. $query['orderby'] = $filter['orderby'];
  2142. if ( isset( $filter['order'] ) ) {
  2143. $query['order'] = $filter['order'];
  2144. }
  2145. }
  2146. if ( isset( $filter['hide_empty'] ) ) {
  2147. $query['hide_empty'] = $filter['hide_empty'];
  2148. } else {
  2149. $query['get'] = 'all';
  2150. }
  2151. if ( isset( $filter['search'] ) ) {
  2152. $query['search'] = $filter['search'];
  2153. }
  2154. $terms = get_terms( $query );
  2155. if ( is_wp_error( $terms ) ) {
  2156. return new IXR_Error( 500, $terms->get_error_message() );
  2157. }
  2158. $struct = array();
  2159. foreach ( $terms as $term ) {
  2160. $struct[] = $this->_prepare_term( $term );
  2161. }
  2162. return $struct;
  2163. }
  2164. /**
  2165. * Retrieve a taxonomy.
  2166. *
  2167. * @since 3.4.0
  2168. *
  2169. * @see get_taxonomy()
  2170. *
  2171. * @param array $args {
  2172. * Method arguments. Note: arguments must be ordered as documented.
  2173. *
  2174. * @type int $blog_id Blog ID (unused).
  2175. * @type string $username Username.
  2176. * @type string $password Password.
  2177. * @type string $taxnomy Taxonomy name.
  2178. * @type array $fields Optional. Array of taxonomy fields to limit to in the return.
  2179. * Accepts 'labels', 'cap', 'menu', and 'object_type'.
  2180. * Default empty array.
  2181. * }
  2182. * @return array|IXR_Error An array of taxonomy data on success, IXR_Error instance otherwise.
  2183. */
  2184. public function wp_getTaxonomy( $args ) {
  2185. if ( ! $this->minimum_args( $args, 4 ) ) {
  2186. return $this->error;
  2187. }
  2188. $this->escape( $args );
  2189. $username = $args[1];
  2190. $password = $args[2];
  2191. $taxonomy = $args[3];
  2192. if ( isset( $args[4] ) ) {
  2193. $fields = $args[4];
  2194. } else {
  2195. /**
  2196. * Filters the taxonomy query fields used by the given XML-RPC method.
  2197. *
  2198. * @since 3.4.0
  2199. *
  2200. * @param array $fields An array of taxonomy fields to retrieve.
  2201. * @param string $method The method name.
  2202. */
  2203. $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomy' );
  2204. }
  2205. $user = $this->login( $username, $password );
  2206. if ( ! $user ) {
  2207. return $this->error;
  2208. }
  2209. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2210. do_action( 'xmlrpc_call', 'wp.getTaxonomy', $args, $this );
  2211. if ( ! taxonomy_exists( $taxonomy ) ) {
  2212. return new IXR_Error( 403, __( 'Invalid taxonomy.' ) );
  2213. }
  2214. $taxonomy = get_taxonomy( $taxonomy );
  2215. if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) {
  2216. return new IXR_Error( 401, __( 'Sorry, you are not allowed to assign terms in this taxonomy.' ) );
  2217. }
  2218. return $this->_prepare_taxonomy( $taxonomy, $fields );
  2219. }
  2220. /**
  2221. * Retrieve all taxonomies.
  2222. *
  2223. * @since 3.4.0
  2224. *
  2225. * @see get_taxonomies()
  2226. *
  2227. * @param array $args {
  2228. * Method arguments. Note: arguments must be ordered as documented.
  2229. *
  2230. * @type int $blog_id Blog ID (unused).
  2231. * @type string $username Username.
  2232. * @type string $password Password.
  2233. * @type array $filter Optional. An array of arguments for retrieving taxonomies.
  2234. * @type array $fields Optional. The subset of taxonomy fields to return.
  2235. * }
  2236. * @return array|IXR_Error An associative array of taxonomy data with returned fields determined
  2237. * by `$fields`, or an IXR_Error instance on failure.
  2238. */
  2239. public function wp_getTaxonomies( $args ) {
  2240. if ( ! $this->minimum_args( $args, 3 ) ) {
  2241. return $this->error;
  2242. }
  2243. $this->escape( $args );
  2244. $username = $args[1];
  2245. $password = $args[2];
  2246. $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
  2247. if ( isset( $args[4] ) ) {
  2248. $fields = $args[4];
  2249. } else {
  2250. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2251. $fields = apply_filters( 'xmlrpc_default_taxonomy_fields', array( 'labels', 'cap', 'object_type' ), 'wp.getTaxonomies' );
  2252. }
  2253. $user = $this->login( $username, $password );
  2254. if ( ! $user ) {
  2255. return $this->error;
  2256. }
  2257. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2258. do_action( 'xmlrpc_call', 'wp.getTaxonomies', $args, $this );
  2259. $taxonomies = get_taxonomies( $filter, 'objects' );
  2260. // Holds all the taxonomy data.
  2261. $struct = array();
  2262. foreach ( $taxonomies as $taxonomy ) {
  2263. // Capability check for post types.
  2264. if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) {
  2265. continue;
  2266. }
  2267. $struct[] = $this->_prepare_taxonomy( $taxonomy, $fields );
  2268. }
  2269. return $struct;
  2270. }
  2271. /**
  2272. * Retrieve a user.
  2273. *
  2274. * The optional $fields parameter specifies what fields will be included
  2275. * in the response array. This should be a list of field names. 'user_id' will
  2276. * always be included in the response regardless of the value of $fields.
  2277. *
  2278. * Instead of, or in addition to, individual field names, conceptual group
  2279. * names can be used to specify multiple fields. The available conceptual
  2280. * groups are 'basic' and 'all'.
  2281. *
  2282. * @uses get_userdata()
  2283. *
  2284. * @param array $args {
  2285. * Method arguments. Note: arguments must be ordered as documented.
  2286. *
  2287. * @type int $blog_id (unused)
  2288. * @type string $username
  2289. * @type string $password
  2290. * @type int $user_id
  2291. * @type array $fields (optional)
  2292. * }
  2293. * @return array|IXR_Error Array contains (based on $fields parameter):
  2294. * - 'user_id'
  2295. * - 'username'
  2296. * - 'first_name'
  2297. * - 'last_name'
  2298. * - 'registered'
  2299. * - 'bio'
  2300. * - 'email'
  2301. * - 'nickname'
  2302. * - 'nicename'
  2303. * - 'url'
  2304. * - 'display_name'
  2305. * - 'roles'
  2306. */
  2307. public function wp_getUser( $args ) {
  2308. if ( ! $this->minimum_args( $args, 4 ) ) {
  2309. return $this->error;
  2310. }
  2311. $this->escape( $args );
  2312. $username = $args[1];
  2313. $password = $args[2];
  2314. $user_id = (int) $args[3];
  2315. if ( isset( $args[4] ) ) {
  2316. $fields = $args[4];
  2317. } else {
  2318. /**
  2319. * Filters the default user query fields used by the given XML-RPC method.
  2320. *
  2321. * @since 3.5.0
  2322. *
  2323. * @param array $fields User query fields for given method. Default 'all'.
  2324. * @param string $method The method name.
  2325. */
  2326. $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUser' );
  2327. }
  2328. $user = $this->login( $username, $password );
  2329. if ( ! $user ) {
  2330. return $this->error;
  2331. }
  2332. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2333. do_action( 'xmlrpc_call', 'wp.getUser', $args, $this );
  2334. if ( ! current_user_can( 'edit_user', $user_id ) ) {
  2335. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this user.' ) );
  2336. }
  2337. $user_data = get_userdata( $user_id );
  2338. if ( ! $user_data ) {
  2339. return new IXR_Error( 404, __( 'Invalid user ID.' ) );
  2340. }
  2341. return $this->_prepare_user( $user_data, $fields );
  2342. }
  2343. /**
  2344. * Retrieve users.
  2345. *
  2346. * The optional $filter parameter modifies the query used to retrieve users.
  2347. * Accepted keys are 'number' (default: 50), 'offset' (default: 0), 'role',
  2348. * 'who', 'orderby', and 'order'.
  2349. *
  2350. * The optional $fields parameter specifies what fields will be included
  2351. * in the response array.
  2352. *
  2353. * @uses get_users()
  2354. * @see wp_getUser() for more on $fields and return values
  2355. *
  2356. * @param array $args {
  2357. * Method arguments. Note: arguments must be ordered as documented.
  2358. *
  2359. * @type int $blog_id (unused)
  2360. * @type string $username
  2361. * @type string $password
  2362. * @type array $filter (optional)
  2363. * @type array $fields (optional)
  2364. * }
  2365. * @return array|IXR_Error users data
  2366. */
  2367. public function wp_getUsers( $args ) {
  2368. if ( ! $this->minimum_args( $args, 3 ) ) {
  2369. return $this->error;
  2370. }
  2371. $this->escape( $args );
  2372. $username = $args[1];
  2373. $password = $args[2];
  2374. $filter = isset( $args[3] ) ? $args[3] : array();
  2375. if ( isset( $args[4] ) ) {
  2376. $fields = $args[4];
  2377. } else {
  2378. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2379. $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getUsers' );
  2380. }
  2381. $user = $this->login( $username, $password );
  2382. if ( ! $user ) {
  2383. return $this->error;
  2384. }
  2385. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2386. do_action( 'xmlrpc_call', 'wp.getUsers', $args, $this );
  2387. if ( ! current_user_can( 'list_users' ) ) {
  2388. return new IXR_Error( 401, __( 'Sorry, you are not allowed to list users.' ) );
  2389. }
  2390. $query = array( 'fields' => 'all_with_meta' );
  2391. $query['number'] = ( isset( $filter['number'] ) ) ? absint( $filter['number'] ) : 50;
  2392. $query['offset'] = ( isset( $filter['offset'] ) ) ? absint( $filter['offset'] ) : 0;
  2393. if ( isset( $filter['orderby'] ) ) {
  2394. $query['orderby'] = $filter['orderby'];
  2395. if ( isset( $filter['order'] ) ) {
  2396. $query['order'] = $filter['order'];
  2397. }
  2398. }
  2399. if ( isset( $filter['role'] ) ) {
  2400. if ( get_role( $filter['role'] ) === null ) {
  2401. return new IXR_Error( 403, __( 'Invalid role.' ) );
  2402. }
  2403. $query['role'] = $filter['role'];
  2404. }
  2405. if ( isset( $filter['who'] ) ) {
  2406. $query['who'] = $filter['who'];
  2407. }
  2408. $users = get_users( $query );
  2409. $_users = array();
  2410. foreach ( $users as $user_data ) {
  2411. if ( current_user_can( 'edit_user', $user_data->ID ) ) {
  2412. $_users[] = $this->_prepare_user( $user_data, $fields );
  2413. }
  2414. }
  2415. return $_users;
  2416. }
  2417. /**
  2418. * Retrieve information about the requesting user.
  2419. *
  2420. * @uses get_userdata()
  2421. *
  2422. * @param array $args {
  2423. * Method arguments. Note: arguments must be ordered as documented.
  2424. *
  2425. * @type int $blog_id (unused)
  2426. * @type string $username
  2427. * @type string $password
  2428. * @type array $fields (optional)
  2429. * }
  2430. * @return array|IXR_Error (@see wp_getUser)
  2431. */
  2432. public function wp_getProfile( $args ) {
  2433. if ( ! $this->minimum_args( $args, 3 ) ) {
  2434. return $this->error;
  2435. }
  2436. $this->escape( $args );
  2437. $username = $args[1];
  2438. $password = $args[2];
  2439. if ( isset( $args[3] ) ) {
  2440. $fields = $args[3];
  2441. } else {
  2442. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2443. $fields = apply_filters( 'xmlrpc_default_user_fields', array( 'all' ), 'wp.getProfile' );
  2444. }
  2445. $user = $this->login( $username, $password );
  2446. if ( ! $user ) {
  2447. return $this->error;
  2448. }
  2449. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2450. do_action( 'xmlrpc_call', 'wp.getProfile', $args, $this );
  2451. if ( ! current_user_can( 'edit_user', $user->ID ) ) {
  2452. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit your profile.' ) );
  2453. }
  2454. $user_data = get_userdata( $user->ID );
  2455. return $this->_prepare_user( $user_data, $fields );
  2456. }
  2457. /**
  2458. * Edit user's profile.
  2459. *
  2460. * @uses wp_update_user()
  2461. *
  2462. * @param array $args {
  2463. * Method arguments. Note: arguments must be ordered as documented.
  2464. *
  2465. * @type int $blog_id (unused)
  2466. * @type string $username
  2467. * @type string $password
  2468. * @type array $content_struct It can optionally contain:
  2469. * - 'first_name'
  2470. * - 'last_name'
  2471. * - 'website'
  2472. * - 'display_name'
  2473. * - 'nickname'
  2474. * - 'nicename'
  2475. * - 'bio'
  2476. * }
  2477. * @return true|IXR_Error True, on success.
  2478. */
  2479. public function wp_editProfile( $args ) {
  2480. if ( ! $this->minimum_args( $args, 4 ) ) {
  2481. return $this->error;
  2482. }
  2483. $this->escape( $args );
  2484. $username = $args[1];
  2485. $password = $args[2];
  2486. $content_struct = $args[3];
  2487. $user = $this->login( $username, $password );
  2488. if ( ! $user ) {
  2489. return $this->error;
  2490. }
  2491. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2492. do_action( 'xmlrpc_call', 'wp.editProfile', $args, $this );
  2493. if ( ! current_user_can( 'edit_user', $user->ID ) ) {
  2494. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit your profile.' ) );
  2495. }
  2496. // Holds data of the user.
  2497. $user_data = array();
  2498. $user_data['ID'] = $user->ID;
  2499. // Only set the user details if they were given.
  2500. if ( isset( $content_struct['first_name'] ) ) {
  2501. $user_data['first_name'] = $content_struct['first_name'];
  2502. }
  2503. if ( isset( $content_struct['last_name'] ) ) {
  2504. $user_data['last_name'] = $content_struct['last_name'];
  2505. }
  2506. if ( isset( $content_struct['url'] ) ) {
  2507. $user_data['user_url'] = $content_struct['url'];
  2508. }
  2509. if ( isset( $content_struct['display_name'] ) ) {
  2510. $user_data['display_name'] = $content_struct['display_name'];
  2511. }
  2512. if ( isset( $content_struct['nickname'] ) ) {
  2513. $user_data['nickname'] = $content_struct['nickname'];
  2514. }
  2515. if ( isset( $content_struct['nicename'] ) ) {
  2516. $user_data['user_nicename'] = $content_struct['nicename'];
  2517. }
  2518. if ( isset( $content_struct['bio'] ) ) {
  2519. $user_data['description'] = $content_struct['bio'];
  2520. }
  2521. $result = wp_update_user( $user_data );
  2522. if ( is_wp_error( $result ) ) {
  2523. return new IXR_Error( 500, $result->get_error_message() );
  2524. }
  2525. if ( ! $result ) {
  2526. return new IXR_Error( 500, __( 'Sorry, the user could not be updated.' ) );
  2527. }
  2528. return true;
  2529. }
  2530. /**
  2531. * Retrieve page.
  2532. *
  2533. * @since 2.2.0
  2534. *
  2535. * @param array $args {
  2536. * Method arguments. Note: arguments must be ordered as documented.
  2537. *
  2538. * @type int $blog_id (unused)
  2539. * @type int $page_id
  2540. * @type string $username
  2541. * @type string $password
  2542. * }
  2543. * @return array|IXR_Error
  2544. */
  2545. public function wp_getPage( $args ) {
  2546. $this->escape( $args );
  2547. $page_id = (int) $args[1];
  2548. $username = $args[2];
  2549. $password = $args[3];
  2550. $user = $this->login( $username, $password );
  2551. if ( ! $user ) {
  2552. return $this->error;
  2553. }
  2554. $page = get_post( $page_id );
  2555. if ( ! $page ) {
  2556. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  2557. }
  2558. if ( ! current_user_can( 'edit_page', $page_id ) ) {
  2559. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this page.' ) );
  2560. }
  2561. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2562. do_action( 'xmlrpc_call', 'wp.getPage', $args, $this );
  2563. // If we found the page then format the data.
  2564. if ( $page->ID && ( 'page' === $page->post_type ) ) {
  2565. return $this->_prepare_page( $page );
  2566. } else {
  2567. // If the page doesn't exist, indicate that.
  2568. return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
  2569. }
  2570. }
  2571. /**
  2572. * Retrieve Pages.
  2573. *
  2574. * @since 2.2.0
  2575. *
  2576. * @param array $args {
  2577. * Method arguments. Note: arguments must be ordered as documented.
  2578. *
  2579. * @type int $blog_id (unused)
  2580. * @type string $username
  2581. * @type string $password
  2582. * @type int $num_pages
  2583. * }
  2584. * @return array|IXR_Error
  2585. */
  2586. public function wp_getPages( $args ) {
  2587. $this->escape( $args );
  2588. $username = $args[1];
  2589. $password = $args[2];
  2590. $num_pages = isset( $args[3] ) ? (int) $args[3] : 10;
  2591. $user = $this->login( $username, $password );
  2592. if ( ! $user ) {
  2593. return $this->error;
  2594. }
  2595. if ( ! current_user_can( 'edit_pages' ) ) {
  2596. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit pages.' ) );
  2597. }
  2598. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2599. do_action( 'xmlrpc_call', 'wp.getPages', $args, $this );
  2600. $pages = get_posts(
  2601. array(
  2602. 'post_type' => 'page',
  2603. 'post_status' => 'any',
  2604. 'numberposts' => $num_pages,
  2605. )
  2606. );
  2607. $num_pages = count( $pages );
  2608. // If we have pages, put together their info.
  2609. if ( $num_pages >= 1 ) {
  2610. $pages_struct = array();
  2611. foreach ( $pages as $page ) {
  2612. if ( current_user_can( 'edit_page', $page->ID ) ) {
  2613. $pages_struct[] = $this->_prepare_page( $page );
  2614. }
  2615. }
  2616. return $pages_struct;
  2617. }
  2618. return array();
  2619. }
  2620. /**
  2621. * Create new page.
  2622. *
  2623. * @since 2.2.0
  2624. *
  2625. * @see wp_xmlrpc_server::mw_newPost()
  2626. *
  2627. * @param array $args {
  2628. * Method arguments. Note: arguments must be ordered as documented.
  2629. *
  2630. * @type int $blog_id (unused)
  2631. * @type string $username
  2632. * @type string $password
  2633. * @type array $content_struct
  2634. * }
  2635. * @return int|IXR_Error
  2636. */
  2637. public function wp_newPage( $args ) {
  2638. // Items not escaped here will be escaped in wp_newPost().
  2639. $username = $this->escape( $args[1] );
  2640. $password = $this->escape( $args[2] );
  2641. $user = $this->login( $username, $password );
  2642. if ( ! $user ) {
  2643. return $this->error;
  2644. }
  2645. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2646. do_action( 'xmlrpc_call', 'wp.newPage', $args, $this );
  2647. // Mark this as content for a page.
  2648. $args[3]['post_type'] = 'page';
  2649. // Let mw_newPost() do all of the heavy lifting.
  2650. return $this->mw_newPost( $args );
  2651. }
  2652. /**
  2653. * Delete page.
  2654. *
  2655. * @since 2.2.0
  2656. *
  2657. * @param array $args {
  2658. * Method arguments. Note: arguments must be ordered as documented.
  2659. *
  2660. * @type int $blog_id (unused)
  2661. * @type string $username
  2662. * @type string $password
  2663. * @type int $page_id
  2664. * }
  2665. * @return true|IXR_Error True, if success.
  2666. */
  2667. public function wp_deletePage( $args ) {
  2668. $this->escape( $args );
  2669. $username = $args[1];
  2670. $password = $args[2];
  2671. $page_id = (int) $args[3];
  2672. $user = $this->login( $username, $password );
  2673. if ( ! $user ) {
  2674. return $this->error;
  2675. }
  2676. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2677. do_action( 'xmlrpc_call', 'wp.deletePage', $args, $this );
  2678. // Get the current page based on the 'page_id' and
  2679. // make sure it is a page and not a post.
  2680. $actual_page = get_post( $page_id, ARRAY_A );
  2681. if ( ! $actual_page || ( 'page' !== $actual_page['post_type'] ) ) {
  2682. return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
  2683. }
  2684. // Make sure the user can delete pages.
  2685. if ( ! current_user_can( 'delete_page', $page_id ) ) {
  2686. return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this page.' ) );
  2687. }
  2688. // Attempt to delete the page.
  2689. $result = wp_delete_post( $page_id );
  2690. if ( ! $result ) {
  2691. return new IXR_Error( 500, __( 'Failed to delete the page.' ) );
  2692. }
  2693. /**
  2694. * Fires after a page has been successfully deleted via XML-RPC.
  2695. *
  2696. * @since 3.4.0
  2697. *
  2698. * @param int $page_id ID of the deleted page.
  2699. * @param array $args An array of arguments to delete the page.
  2700. */
  2701. do_action( 'xmlrpc_call_success_wp_deletePage', $page_id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  2702. return true;
  2703. }
  2704. /**
  2705. * Edit page.
  2706. *
  2707. * @since 2.2.0
  2708. *
  2709. * @param array $args {
  2710. * Method arguments. Note: arguments must be ordered as documented.
  2711. *
  2712. * @type int $blog_id (unused)
  2713. * @type int $page_id
  2714. * @type string $username
  2715. * @type string $password
  2716. * @type string $content
  2717. * @type string $publish
  2718. * }
  2719. * @return array|IXR_Error
  2720. */
  2721. public function wp_editPage( $args ) {
  2722. // Items will be escaped in mw_editPost().
  2723. $page_id = (int) $args[1];
  2724. $username = $args[2];
  2725. $password = $args[3];
  2726. $content = $args[4];
  2727. $publish = $args[5];
  2728. $escaped_username = $this->escape( $username );
  2729. $escaped_password = $this->escape( $password );
  2730. $user = $this->login( $escaped_username, $escaped_password );
  2731. if ( ! $user ) {
  2732. return $this->error;
  2733. }
  2734. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2735. do_action( 'xmlrpc_call', 'wp.editPage', $args, $this );
  2736. // Get the page data and make sure it is a page.
  2737. $actual_page = get_post( $page_id, ARRAY_A );
  2738. if ( ! $actual_page || ( 'page' !== $actual_page['post_type'] ) ) {
  2739. return new IXR_Error( 404, __( 'Sorry, no such page.' ) );
  2740. }
  2741. // Make sure the user is allowed to edit pages.
  2742. if ( ! current_user_can( 'edit_page', $page_id ) ) {
  2743. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this page.' ) );
  2744. }
  2745. // Mark this as content for a page.
  2746. $content['post_type'] = 'page';
  2747. // Arrange args in the way mw_editPost() understands.
  2748. $args = array(
  2749. $page_id,
  2750. $username,
  2751. $password,
  2752. $content,
  2753. $publish,
  2754. );
  2755. // Let mw_editPost() do all of the heavy lifting.
  2756. return $this->mw_editPost( $args );
  2757. }
  2758. /**
  2759. * Retrieve page list.
  2760. *
  2761. * @since 2.2.0
  2762. *
  2763. * @global wpdb $wpdb WordPress database abstraction object.
  2764. *
  2765. * @param array $args {
  2766. * Method arguments. Note: arguments must be ordered as documented.
  2767. *
  2768. * @type int $blog_id (unused)
  2769. * @type string $username
  2770. * @type string $password
  2771. * }
  2772. * @return array|IXR_Error
  2773. */
  2774. public function wp_getPageList( $args ) {
  2775. global $wpdb;
  2776. $this->escape( $args );
  2777. $username = $args[1];
  2778. $password = $args[2];
  2779. $user = $this->login( $username, $password );
  2780. if ( ! $user ) {
  2781. return $this->error;
  2782. }
  2783. if ( ! current_user_can( 'edit_pages' ) ) {
  2784. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit pages.' ) );
  2785. }
  2786. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2787. do_action( 'xmlrpc_call', 'wp.getPageList', $args, $this );
  2788. // Get list of page IDs and titles.
  2789. $page_list = $wpdb->get_results(
  2790. "
  2791. SELECT ID page_id,
  2792. post_title page_title,
  2793. post_parent page_parent_id,
  2794. post_date_gmt,
  2795. post_date,
  2796. post_status
  2797. FROM {$wpdb->posts}
  2798. WHERE post_type = 'page'
  2799. ORDER BY ID
  2800. "
  2801. );
  2802. // The date needs to be formatted properly.
  2803. $num_pages = count( $page_list );
  2804. for ( $i = 0; $i < $num_pages; $i++ ) {
  2805. $page_list[ $i ]->dateCreated = $this->_convert_date( $page_list[ $i ]->post_date );
  2806. $page_list[ $i ]->date_created_gmt = $this->_convert_date_gmt( $page_list[ $i ]->post_date_gmt, $page_list[ $i ]->post_date );
  2807. unset( $page_list[ $i ]->post_date_gmt );
  2808. unset( $page_list[ $i ]->post_date );
  2809. unset( $page_list[ $i ]->post_status );
  2810. }
  2811. return $page_list;
  2812. }
  2813. /**
  2814. * Retrieve authors list.
  2815. *
  2816. * @since 2.2.0
  2817. *
  2818. * @param array $args {
  2819. * Method arguments. Note: arguments must be ordered as documented.
  2820. *
  2821. * @type int $blog_id (unused)
  2822. * @type string $username
  2823. * @type string $password
  2824. * }
  2825. * @return array|IXR_Error
  2826. */
  2827. public function wp_getAuthors( $args ) {
  2828. $this->escape( $args );
  2829. $username = $args[1];
  2830. $password = $args[2];
  2831. $user = $this->login( $username, $password );
  2832. if ( ! $user ) {
  2833. return $this->error;
  2834. }
  2835. if ( ! current_user_can( 'edit_posts' ) ) {
  2836. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  2837. }
  2838. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2839. do_action( 'xmlrpc_call', 'wp.getAuthors', $args, $this );
  2840. $authors = array();
  2841. foreach ( get_users( array( 'fields' => array( 'ID', 'user_login', 'display_name' ) ) ) as $user ) {
  2842. $authors[] = array(
  2843. 'user_id' => $user->ID,
  2844. 'user_login' => $user->user_login,
  2845. 'display_name' => $user->display_name,
  2846. );
  2847. }
  2848. return $authors;
  2849. }
  2850. /**
  2851. * Get list of all tags
  2852. *
  2853. * @since 2.7.0
  2854. *
  2855. * @param array $args {
  2856. * Method arguments. Note: arguments must be ordered as documented.
  2857. *
  2858. * @type int $blog_id (unused)
  2859. * @type string $username
  2860. * @type string $password
  2861. * }
  2862. * @return array|IXR_Error
  2863. */
  2864. public function wp_getTags( $args ) {
  2865. $this->escape( $args );
  2866. $username = $args[1];
  2867. $password = $args[2];
  2868. $user = $this->login( $username, $password );
  2869. if ( ! $user ) {
  2870. return $this->error;
  2871. }
  2872. if ( ! current_user_can( 'edit_posts' ) ) {
  2873. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view tags.' ) );
  2874. }
  2875. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2876. do_action( 'xmlrpc_call', 'wp.getKeywords', $args, $this );
  2877. $tags = array();
  2878. $all_tags = get_tags();
  2879. if ( $all_tags ) {
  2880. foreach ( (array) $all_tags as $tag ) {
  2881. $struct = array();
  2882. $struct['tag_id'] = $tag->term_id;
  2883. $struct['name'] = $tag->name;
  2884. $struct['count'] = $tag->count;
  2885. $struct['slug'] = $tag->slug;
  2886. $struct['html_url'] = esc_html( get_tag_link( $tag->term_id ) );
  2887. $struct['rss_url'] = esc_html( get_tag_feed_link( $tag->term_id ) );
  2888. $tags[] = $struct;
  2889. }
  2890. }
  2891. return $tags;
  2892. }
  2893. /**
  2894. * Create new category.
  2895. *
  2896. * @since 2.2.0
  2897. *
  2898. * @param array $args {
  2899. * Method arguments. Note: arguments must be ordered as documented.
  2900. *
  2901. * @type int $blog_id (unused)
  2902. * @type string $username
  2903. * @type string $password
  2904. * @type array $category
  2905. * }
  2906. * @return int|IXR_Error Category ID.
  2907. */
  2908. public function wp_newCategory( $args ) {
  2909. $this->escape( $args );
  2910. $username = $args[1];
  2911. $password = $args[2];
  2912. $category = $args[3];
  2913. $user = $this->login( $username, $password );
  2914. if ( ! $user ) {
  2915. return $this->error;
  2916. }
  2917. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2918. do_action( 'xmlrpc_call', 'wp.newCategory', $args, $this );
  2919. // Make sure the user is allowed to add a category.
  2920. if ( ! current_user_can( 'manage_categories' ) ) {
  2921. return new IXR_Error( 401, __( 'Sorry, you are not allowed to add a category.' ) );
  2922. }
  2923. // If no slug was provided, make it empty
  2924. // so that WordPress will generate one.
  2925. if ( empty( $category['slug'] ) ) {
  2926. $category['slug'] = '';
  2927. }
  2928. // If no parent_id was provided, make it empty
  2929. // so that it will be a top-level page (no parent).
  2930. if ( ! isset( $category['parent_id'] ) ) {
  2931. $category['parent_id'] = '';
  2932. }
  2933. // If no description was provided, make it empty.
  2934. if ( empty( $category['description'] ) ) {
  2935. $category['description'] = '';
  2936. }
  2937. $new_category = array(
  2938. 'cat_name' => $category['name'],
  2939. 'category_nicename' => $category['slug'],
  2940. 'category_parent' => $category['parent_id'],
  2941. 'category_description' => $category['description'],
  2942. );
  2943. $cat_id = wp_insert_category( $new_category, true );
  2944. if ( is_wp_error( $cat_id ) ) {
  2945. if ( 'term_exists' === $cat_id->get_error_code() ) {
  2946. return (int) $cat_id->get_error_data();
  2947. } else {
  2948. return new IXR_Error( 500, __( 'Sorry, the category could not be created.' ) );
  2949. }
  2950. } elseif ( ! $cat_id ) {
  2951. return new IXR_Error( 500, __( 'Sorry, the category could not be created.' ) );
  2952. }
  2953. /**
  2954. * Fires after a new category has been successfully created via XML-RPC.
  2955. *
  2956. * @since 3.4.0
  2957. *
  2958. * @param int $cat_id ID of the new category.
  2959. * @param array $args An array of new category arguments.
  2960. */
  2961. do_action( 'xmlrpc_call_success_wp_newCategory', $cat_id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  2962. return $cat_id;
  2963. }
  2964. /**
  2965. * Remove category.
  2966. *
  2967. * @since 2.5.0
  2968. *
  2969. * @param array $args {
  2970. * Method arguments. Note: arguments must be ordered as documented.
  2971. *
  2972. * @type int $blog_id (unused)
  2973. * @type string $username
  2974. * @type string $password
  2975. * @type int $category_id
  2976. * }
  2977. * @return bool|IXR_Error See wp_delete_term() for return info.
  2978. */
  2979. public function wp_deleteCategory( $args ) {
  2980. $this->escape( $args );
  2981. $username = $args[1];
  2982. $password = $args[2];
  2983. $category_id = (int) $args[3];
  2984. $user = $this->login( $username, $password );
  2985. if ( ! $user ) {
  2986. return $this->error;
  2987. }
  2988. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  2989. do_action( 'xmlrpc_call', 'wp.deleteCategory', $args, $this );
  2990. if ( ! current_user_can( 'delete_term', $category_id ) ) {
  2991. return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this category.' ) );
  2992. }
  2993. $status = wp_delete_term( $category_id, 'category' );
  2994. if ( true == $status ) {
  2995. /**
  2996. * Fires after a category has been successfully deleted via XML-RPC.
  2997. *
  2998. * @since 3.4.0
  2999. *
  3000. * @param int $category_id ID of the deleted category.
  3001. * @param array $args An array of arguments to delete the category.
  3002. */
  3003. do_action( 'xmlrpc_call_success_wp_deleteCategory', $category_id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  3004. }
  3005. return $status;
  3006. }
  3007. /**
  3008. * Retrieve category list.
  3009. *
  3010. * @since 2.2.0
  3011. *
  3012. * @param array $args {
  3013. * Method arguments. Note: arguments must be ordered as documented.
  3014. *
  3015. * @type int $blog_id (unused)
  3016. * @type string $username
  3017. * @type string $password
  3018. * @type array $category
  3019. * @type int $max_results
  3020. * }
  3021. * @return array|IXR_Error
  3022. */
  3023. public function wp_suggestCategories( $args ) {
  3024. $this->escape( $args );
  3025. $username = $args[1];
  3026. $password = $args[2];
  3027. $category = $args[3];
  3028. $max_results = (int) $args[4];
  3029. $user = $this->login( $username, $password );
  3030. if ( ! $user ) {
  3031. return $this->error;
  3032. }
  3033. if ( ! current_user_can( 'edit_posts' ) ) {
  3034. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  3035. }
  3036. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3037. do_action( 'xmlrpc_call', 'wp.suggestCategories', $args, $this );
  3038. $category_suggestions = array();
  3039. $args = array(
  3040. 'get' => 'all',
  3041. 'number' => $max_results,
  3042. 'name__like' => $category,
  3043. );
  3044. foreach ( (array) get_categories( $args ) as $cat ) {
  3045. $category_suggestions[] = array(
  3046. 'category_id' => $cat->term_id,
  3047. 'category_name' => $cat->name,
  3048. );
  3049. }
  3050. return $category_suggestions;
  3051. }
  3052. /**
  3053. * Retrieve comment.
  3054. *
  3055. * @since 2.7.0
  3056. *
  3057. * @param array $args {
  3058. * Method arguments. Note: arguments must be ordered as documented.
  3059. *
  3060. * @type int $blog_id (unused)
  3061. * @type string $username
  3062. * @type string $password
  3063. * @type int $comment_id
  3064. * }
  3065. * @return array|IXR_Error
  3066. */
  3067. public function wp_getComment( $args ) {
  3068. $this->escape( $args );
  3069. $username = $args[1];
  3070. $password = $args[2];
  3071. $comment_id = (int) $args[3];
  3072. $user = $this->login( $username, $password );
  3073. if ( ! $user ) {
  3074. return $this->error;
  3075. }
  3076. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3077. do_action( 'xmlrpc_call', 'wp.getComment', $args, $this );
  3078. $comment = get_comment( $comment_id );
  3079. if ( ! $comment ) {
  3080. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  3081. }
  3082. if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
  3083. return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
  3084. }
  3085. return $this->_prepare_comment( $comment );
  3086. }
  3087. /**
  3088. * Retrieve comments.
  3089. *
  3090. * Besides the common blog_id (unused), username, and password arguments, it takes a filter
  3091. * array as last argument.
  3092. *
  3093. * Accepted 'filter' keys are 'status', 'post_id', 'offset', and 'number'.
  3094. *
  3095. * The defaults are as follows:
  3096. * - 'status' - Default is ''. Filter by status (e.g., 'approve', 'hold')
  3097. * - 'post_id' - Default is ''. The post where the comment is posted. Empty string shows all comments.
  3098. * - 'number' - Default is 10. Total number of media items to retrieve.
  3099. * - 'offset' - Default is 0. See WP_Query::query() for more.
  3100. *
  3101. * @since 2.7.0
  3102. *
  3103. * @param array $args {
  3104. * Method arguments. Note: arguments must be ordered as documented.
  3105. *
  3106. * @type int $blog_id (unused)
  3107. * @type string $username
  3108. * @type string $password
  3109. * @type array $struct
  3110. * }
  3111. * @return array|IXR_Error Contains a collection of comments. See wp_xmlrpc_server::wp_getComment() for a description of each item contents
  3112. */
  3113. public function wp_getComments( $args ) {
  3114. $this->escape( $args );
  3115. $username = $args[1];
  3116. $password = $args[2];
  3117. $struct = isset( $args[3] ) ? $args[3] : array();
  3118. $user = $this->login( $username, $password );
  3119. if ( ! $user ) {
  3120. return $this->error;
  3121. }
  3122. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3123. do_action( 'xmlrpc_call', 'wp.getComments', $args, $this );
  3124. if ( isset( $struct['status'] ) ) {
  3125. $status = $struct['status'];
  3126. } else {
  3127. $status = '';
  3128. }
  3129. if ( ! current_user_can( 'moderate_comments' ) && 'approve' !== $status ) {
  3130. return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  3131. }
  3132. $post_id = '';
  3133. if ( isset( $struct['post_id'] ) ) {
  3134. $post_id = absint( $struct['post_id'] );
  3135. }
  3136. $post_type = '';
  3137. if ( isset( $struct['post_type'] ) ) {
  3138. $post_type_object = get_post_type_object( $struct['post_type'] );
  3139. if ( ! $post_type_object || ! post_type_supports( $post_type_object->name, 'comments' ) ) {
  3140. return new IXR_Error( 404, __( 'Invalid post type.' ) );
  3141. }
  3142. $post_type = $struct['post_type'];
  3143. }
  3144. $offset = 0;
  3145. if ( isset( $struct['offset'] ) ) {
  3146. $offset = absint( $struct['offset'] );
  3147. }
  3148. $number = 10;
  3149. if ( isset( $struct['number'] ) ) {
  3150. $number = absint( $struct['number'] );
  3151. }
  3152. $comments = get_comments(
  3153. array(
  3154. 'status' => $status,
  3155. 'post_id' => $post_id,
  3156. 'offset' => $offset,
  3157. 'number' => $number,
  3158. 'post_type' => $post_type,
  3159. )
  3160. );
  3161. $comments_struct = array();
  3162. if ( is_array( $comments ) ) {
  3163. foreach ( $comments as $comment ) {
  3164. $comments_struct[] = $this->_prepare_comment( $comment );
  3165. }
  3166. }
  3167. return $comments_struct;
  3168. }
  3169. /**
  3170. * Delete a comment.
  3171. *
  3172. * By default, the comment will be moved to the Trash instead of deleted.
  3173. * See wp_delete_comment() for more information on this behavior.
  3174. *
  3175. * @since 2.7.0
  3176. *
  3177. * @param array $args {
  3178. * Method arguments. Note: arguments must be ordered as documented.
  3179. *
  3180. * @type int $blog_id (unused)
  3181. * @type string $username
  3182. * @type string $password
  3183. * @type int $comment_ID
  3184. * }
  3185. * @return bool|IXR_Error See wp_delete_comment().
  3186. */
  3187. public function wp_deleteComment( $args ) {
  3188. $this->escape( $args );
  3189. $username = $args[1];
  3190. $password = $args[2];
  3191. $comment_ID = (int) $args[3];
  3192. $user = $this->login( $username, $password );
  3193. if ( ! $user ) {
  3194. return $this->error;
  3195. }
  3196. if ( ! get_comment( $comment_ID ) ) {
  3197. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  3198. }
  3199. if ( ! current_user_can( 'edit_comment', $comment_ID ) ) {
  3200. return new IXR_Error( 403, __( 'Sorry, you are not allowed to delete this comment.' ) );
  3201. }
  3202. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3203. do_action( 'xmlrpc_call', 'wp.deleteComment', $args, $this );
  3204. $status = wp_delete_comment( $comment_ID );
  3205. if ( $status ) {
  3206. /**
  3207. * Fires after a comment has been successfully deleted via XML-RPC.
  3208. *
  3209. * @since 3.4.0
  3210. *
  3211. * @param int $comment_ID ID of the deleted comment.
  3212. * @param array $args An array of arguments to delete the comment.
  3213. */
  3214. do_action( 'xmlrpc_call_success_wp_deleteComment', $comment_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  3215. }
  3216. return $status;
  3217. }
  3218. /**
  3219. * Edit comment.
  3220. *
  3221. * Besides the common blog_id (unused), username, and password arguments, it takes a
  3222. * comment_id integer and a content_struct array as last argument.
  3223. *
  3224. * The allowed keys in the content_struct array are:
  3225. * - 'author'
  3226. * - 'author_url'
  3227. * - 'author_email'
  3228. * - 'content'
  3229. * - 'date_created_gmt'
  3230. * - 'status'. Common statuses are 'approve', 'hold', 'spam'. See get_comment_statuses() for more details
  3231. *
  3232. * @since 2.7.0
  3233. *
  3234. * @param array $args {
  3235. * Method arguments. Note: arguments must be ordered as documented.
  3236. *
  3237. * @type int $blog_id (unused)
  3238. * @type string $username
  3239. * @type string $password
  3240. * @type int $comment_ID
  3241. * @type array $content_struct
  3242. * }
  3243. * @return true|IXR_Error True, on success.
  3244. */
  3245. public function wp_editComment( $args ) {
  3246. $this->escape( $args );
  3247. $username = $args[1];
  3248. $password = $args[2];
  3249. $comment_ID = (int) $args[3];
  3250. $content_struct = $args[4];
  3251. $user = $this->login( $username, $password );
  3252. if ( ! $user ) {
  3253. return $this->error;
  3254. }
  3255. if ( ! get_comment( $comment_ID ) ) {
  3256. return new IXR_Error( 404, __( 'Invalid comment ID.' ) );
  3257. }
  3258. if ( ! current_user_can( 'edit_comment', $comment_ID ) ) {
  3259. return new IXR_Error( 403, __( 'Sorry, you are not allowed to moderate or edit this comment.' ) );
  3260. }
  3261. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3262. do_action( 'xmlrpc_call', 'wp.editComment', $args, $this );
  3263. $comment = array(
  3264. 'comment_ID' => $comment_ID,
  3265. );
  3266. if ( isset( $content_struct['status'] ) ) {
  3267. $statuses = get_comment_statuses();
  3268. $statuses = array_keys( $statuses );
  3269. if ( ! in_array( $content_struct['status'], $statuses, true ) ) {
  3270. return new IXR_Error( 401, __( 'Invalid comment status.' ) );
  3271. }
  3272. $comment['comment_approved'] = $content_struct['status'];
  3273. }
  3274. // Do some timestamp voodoo.
  3275. if ( ! empty( $content_struct['date_created_gmt'] ) ) {
  3276. // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
  3277. $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  3278. $comment['comment_date'] = get_date_from_gmt( $dateCreated );
  3279. $comment['comment_date_gmt'] = iso8601_to_datetime( $dateCreated, 'gmt' );
  3280. }
  3281. if ( isset( $content_struct['content'] ) ) {
  3282. $comment['comment_content'] = $content_struct['content'];
  3283. }
  3284. if ( isset( $content_struct['author'] ) ) {
  3285. $comment['comment_author'] = $content_struct['author'];
  3286. }
  3287. if ( isset( $content_struct['author_url'] ) ) {
  3288. $comment['comment_author_url'] = $content_struct['author_url'];
  3289. }
  3290. if ( isset( $content_struct['author_email'] ) ) {
  3291. $comment['comment_author_email'] = $content_struct['author_email'];
  3292. }
  3293. $result = wp_update_comment( $comment, true );
  3294. if ( is_wp_error( $result ) ) {
  3295. return new IXR_Error( 500, $result->get_error_message() );
  3296. }
  3297. if ( ! $result ) {
  3298. return new IXR_Error( 500, __( 'Sorry, the comment could not be updated.' ) );
  3299. }
  3300. /**
  3301. * Fires after a comment has been successfully updated via XML-RPC.
  3302. *
  3303. * @since 3.4.0
  3304. *
  3305. * @param int $comment_ID ID of the updated comment.
  3306. * @param array $args An array of arguments to update the comment.
  3307. */
  3308. do_action( 'xmlrpc_call_success_wp_editComment', $comment_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  3309. return true;
  3310. }
  3311. /**
  3312. * Create new comment.
  3313. *
  3314. * @since 2.7.0
  3315. *
  3316. * @param array $args {
  3317. * Method arguments. Note: arguments must be ordered as documented.
  3318. *
  3319. * @type int $blog_id (unused)
  3320. * @type string $username
  3321. * @type string $password
  3322. * @type string|int $post
  3323. * @type array $content_struct
  3324. * }
  3325. * @return int|IXR_Error See wp_new_comment().
  3326. */
  3327. public function wp_newComment( $args ) {
  3328. $this->escape( $args );
  3329. $username = $args[1];
  3330. $password = $args[2];
  3331. $post = $args[3];
  3332. $content_struct = $args[4];
  3333. /**
  3334. * Filters whether to allow anonymous comments over XML-RPC.
  3335. *
  3336. * @since 2.7.0
  3337. *
  3338. * @param bool $allow Whether to allow anonymous commenting via XML-RPC.
  3339. * Default false.
  3340. */
  3341. $allow_anon = apply_filters( 'xmlrpc_allow_anonymous_comments', false );
  3342. $user = $this->login( $username, $password );
  3343. if ( ! $user ) {
  3344. $logged_in = false;
  3345. if ( $allow_anon && get_option( 'comment_registration' ) ) {
  3346. return new IXR_Error( 403, __( 'Sorry, you must be logged in to comment.' ) );
  3347. } elseif ( ! $allow_anon ) {
  3348. return $this->error;
  3349. }
  3350. } else {
  3351. $logged_in = true;
  3352. }
  3353. if ( is_numeric( $post ) ) {
  3354. $post_id = absint( $post );
  3355. } else {
  3356. $post_id = url_to_postid( $post );
  3357. }
  3358. if ( ! $post_id ) {
  3359. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3360. }
  3361. if ( ! get_post( $post_id ) ) {
  3362. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3363. }
  3364. if ( ! comments_open( $post_id ) ) {
  3365. return new IXR_Error( 403, __( 'Sorry, comments are closed for this item.' ) );
  3366. }
  3367. if (
  3368. 'publish' === get_post_status( $post_id ) &&
  3369. ! current_user_can( 'edit_post', $post_id ) &&
  3370. post_password_required( $post_id )
  3371. ) {
  3372. return new IXR_Error( 403, __( 'Sorry, you are not allowed to comment on this post.' ) );
  3373. }
  3374. if (
  3375. 'private' === get_post_status( $post_id ) &&
  3376. ! current_user_can( 'read_post', $post_id )
  3377. ) {
  3378. return new IXR_Error( 403, __( 'Sorry, you are not allowed to comment on this post.' ) );
  3379. }
  3380. $comment = array(
  3381. 'comment_post_ID' => $post_id,
  3382. 'comment_content' => trim( $content_struct['content'] ),
  3383. );
  3384. if ( $logged_in ) {
  3385. $display_name = $user->display_name;
  3386. $user_email = $user->user_email;
  3387. $user_url = $user->user_url;
  3388. $comment['comment_author'] = $this->escape( $display_name );
  3389. $comment['comment_author_email'] = $this->escape( $user_email );
  3390. $comment['comment_author_url'] = $this->escape( $user_url );
  3391. $comment['user_ID'] = $user->ID;
  3392. } else {
  3393. $comment['comment_author'] = '';
  3394. if ( isset( $content_struct['author'] ) ) {
  3395. $comment['comment_author'] = $content_struct['author'];
  3396. }
  3397. $comment['comment_author_email'] = '';
  3398. if ( isset( $content_struct['author_email'] ) ) {
  3399. $comment['comment_author_email'] = $content_struct['author_email'];
  3400. }
  3401. $comment['comment_author_url'] = '';
  3402. if ( isset( $content_struct['author_url'] ) ) {
  3403. $comment['comment_author_url'] = $content_struct['author_url'];
  3404. }
  3405. $comment['user_ID'] = 0;
  3406. if ( get_option( 'require_name_email' ) ) {
  3407. if ( strlen( $comment['comment_author_email'] ) < 6 || '' === $comment['comment_author'] ) {
  3408. return new IXR_Error( 403, __( 'Comment author name and email are required.' ) );
  3409. } elseif ( ! is_email( $comment['comment_author_email'] ) ) {
  3410. return new IXR_Error( 403, __( 'A valid email address is required.' ) );
  3411. }
  3412. }
  3413. }
  3414. $comment['comment_parent'] = isset( $content_struct['comment_parent'] ) ? absint( $content_struct['comment_parent'] ) : 0;
  3415. /** This filter is documented in wp-includes/comment.php */
  3416. $allow_empty = apply_filters( 'allow_empty_comment', false, $comment );
  3417. if ( ! $allow_empty && '' === $comment['comment_content'] ) {
  3418. return new IXR_Error( 403, __( 'Comment is required.' ) );
  3419. }
  3420. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3421. do_action( 'xmlrpc_call', 'wp.newComment', $args, $this );
  3422. $comment_ID = wp_new_comment( $comment, true );
  3423. if ( is_wp_error( $comment_ID ) ) {
  3424. return new IXR_Error( 403, $comment_ID->get_error_message() );
  3425. }
  3426. if ( ! $comment_ID ) {
  3427. return new IXR_Error( 403, __( 'Something went wrong.' ) );
  3428. }
  3429. /**
  3430. * Fires after a new comment has been successfully created via XML-RPC.
  3431. *
  3432. * @since 3.4.0
  3433. *
  3434. * @param int $comment_ID ID of the new comment.
  3435. * @param array $args An array of new comment arguments.
  3436. */
  3437. do_action( 'xmlrpc_call_success_wp_newComment', $comment_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  3438. return $comment_ID;
  3439. }
  3440. /**
  3441. * Retrieve all of the comment status.
  3442. *
  3443. * @since 2.7.0
  3444. *
  3445. * @param array $args {
  3446. * Method arguments. Note: arguments must be ordered as documented.
  3447. *
  3448. * @type int $blog_id (unused)
  3449. * @type string $username
  3450. * @type string $password
  3451. * }
  3452. * @return array|IXR_Error
  3453. */
  3454. public function wp_getCommentStatusList( $args ) {
  3455. $this->escape( $args );
  3456. $username = $args[1];
  3457. $password = $args[2];
  3458. $user = $this->login( $username, $password );
  3459. if ( ! $user ) {
  3460. return $this->error;
  3461. }
  3462. if ( ! current_user_can( 'publish_posts' ) ) {
  3463. return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details about this site.' ) );
  3464. }
  3465. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3466. do_action( 'xmlrpc_call', 'wp.getCommentStatusList', $args, $this );
  3467. return get_comment_statuses();
  3468. }
  3469. /**
  3470. * Retrieve comment count.
  3471. *
  3472. * @since 2.5.0
  3473. *
  3474. * @param array $args {
  3475. * Method arguments. Note: arguments must be ordered as documented.
  3476. *
  3477. * @type int $blog_id (unused)
  3478. * @type string $username
  3479. * @type string $password
  3480. * @type int $post_id
  3481. * }
  3482. * @return array|IXR_Error
  3483. */
  3484. public function wp_getCommentCount( $args ) {
  3485. $this->escape( $args );
  3486. $username = $args[1];
  3487. $password = $args[2];
  3488. $post_id = (int) $args[3];
  3489. $user = $this->login( $username, $password );
  3490. if ( ! $user ) {
  3491. return $this->error;
  3492. }
  3493. $post = get_post( $post_id, ARRAY_A );
  3494. if ( empty( $post['ID'] ) ) {
  3495. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  3496. }
  3497. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  3498. return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details of this post.' ) );
  3499. }
  3500. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3501. do_action( 'xmlrpc_call', 'wp.getCommentCount', $args, $this );
  3502. $count = wp_count_comments( $post_id );
  3503. return array(
  3504. 'approved' => $count->approved,
  3505. 'awaiting_moderation' => $count->moderated,
  3506. 'spam' => $count->spam,
  3507. 'total_comments' => $count->total_comments,
  3508. );
  3509. }
  3510. /**
  3511. * Retrieve post statuses.
  3512. *
  3513. * @since 2.5.0
  3514. *
  3515. * @param array $args {
  3516. * Method arguments. Note: arguments must be ordered as documented.
  3517. *
  3518. * @type int $blog_id (unused)
  3519. * @type string $username
  3520. * @type string $password
  3521. * }
  3522. * @return array|IXR_Error
  3523. */
  3524. public function wp_getPostStatusList( $args ) {
  3525. $this->escape( $args );
  3526. $username = $args[1];
  3527. $password = $args[2];
  3528. $user = $this->login( $username, $password );
  3529. if ( ! $user ) {
  3530. return $this->error;
  3531. }
  3532. if ( ! current_user_can( 'edit_posts' ) ) {
  3533. return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details about this site.' ) );
  3534. }
  3535. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3536. do_action( 'xmlrpc_call', 'wp.getPostStatusList', $args, $this );
  3537. return get_post_statuses();
  3538. }
  3539. /**
  3540. * Retrieve page statuses.
  3541. *
  3542. * @since 2.5.0
  3543. *
  3544. * @param array $args {
  3545. * Method arguments. Note: arguments must be ordered as documented.
  3546. *
  3547. * @type int $blog_id (unused)
  3548. * @type string $username
  3549. * @type string $password
  3550. * }
  3551. * @return array|IXR_Error
  3552. */
  3553. public function wp_getPageStatusList( $args ) {
  3554. $this->escape( $args );
  3555. $username = $args[1];
  3556. $password = $args[2];
  3557. $user = $this->login( $username, $password );
  3558. if ( ! $user ) {
  3559. return $this->error;
  3560. }
  3561. if ( ! current_user_can( 'edit_pages' ) ) {
  3562. return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details about this site.' ) );
  3563. }
  3564. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3565. do_action( 'xmlrpc_call', 'wp.getPageStatusList', $args, $this );
  3566. return get_page_statuses();
  3567. }
  3568. /**
  3569. * Retrieve page templates.
  3570. *
  3571. * @since 2.6.0
  3572. *
  3573. * @param array $args {
  3574. * Method arguments. Note: arguments must be ordered as documented.
  3575. *
  3576. * @type int $blog_id (unused)
  3577. * @type string $username
  3578. * @type string $password
  3579. * }
  3580. * @return array|IXR_Error
  3581. */
  3582. public function wp_getPageTemplates( $args ) {
  3583. $this->escape( $args );
  3584. $username = $args[1];
  3585. $password = $args[2];
  3586. $user = $this->login( $username, $password );
  3587. if ( ! $user ) {
  3588. return $this->error;
  3589. }
  3590. if ( ! current_user_can( 'edit_pages' ) ) {
  3591. return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details about this site.' ) );
  3592. }
  3593. $templates = get_page_templates();
  3594. $templates['Default'] = 'default';
  3595. return $templates;
  3596. }
  3597. /**
  3598. * Retrieve blog options.
  3599. *
  3600. * @since 2.6.0
  3601. *
  3602. * @param array $args {
  3603. * Method arguments. Note: arguments must be ordered as documented.
  3604. *
  3605. * @type int $blog_id (unused)
  3606. * @type string $username
  3607. * @type string $password
  3608. * @type array $options
  3609. * }
  3610. * @return array|IXR_Error
  3611. */
  3612. public function wp_getOptions( $args ) {
  3613. $this->escape( $args );
  3614. $username = $args[1];
  3615. $password = $args[2];
  3616. $options = isset( $args[3] ) ? (array) $args[3] : array();
  3617. $user = $this->login( $username, $password );
  3618. if ( ! $user ) {
  3619. return $this->error;
  3620. }
  3621. // If no specific options where asked for, return all of them.
  3622. if ( count( $options ) == 0 ) {
  3623. $options = array_keys( $this->blog_options );
  3624. }
  3625. return $this->_getOptions( $options );
  3626. }
  3627. /**
  3628. * Retrieve blog options value from list.
  3629. *
  3630. * @since 2.6.0
  3631. *
  3632. * @param array $options Options to retrieve.
  3633. * @return array
  3634. */
  3635. public function _getOptions( $options ) {
  3636. $data = array();
  3637. $can_manage = current_user_can( 'manage_options' );
  3638. foreach ( $options as $option ) {
  3639. if ( array_key_exists( $option, $this->blog_options ) ) {
  3640. $data[ $option ] = $this->blog_options[ $option ];
  3641. // Is the value static or dynamic?
  3642. if ( isset( $data[ $option ]['option'] ) ) {
  3643. $data[ $option ]['value'] = get_option( $data[ $option ]['option'] );
  3644. unset( $data[ $option ]['option'] );
  3645. }
  3646. if ( ! $can_manage ) {
  3647. $data[ $option ]['readonly'] = true;
  3648. }
  3649. }
  3650. }
  3651. return $data;
  3652. }
  3653. /**
  3654. * Update blog options.
  3655. *
  3656. * @since 2.6.0
  3657. *
  3658. * @param array $args {
  3659. * Method arguments. Note: arguments must be ordered as documented.
  3660. *
  3661. * @type int $blog_id (unused)
  3662. * @type string $username
  3663. * @type string $password
  3664. * @type array $options
  3665. * }
  3666. * @return array|IXR_Error
  3667. */
  3668. public function wp_setOptions( $args ) {
  3669. $this->escape( $args );
  3670. $username = $args[1];
  3671. $password = $args[2];
  3672. $options = (array) $args[3];
  3673. $user = $this->login( $username, $password );
  3674. if ( ! $user ) {
  3675. return $this->error;
  3676. }
  3677. if ( ! current_user_can( 'manage_options' ) ) {
  3678. return new IXR_Error( 403, __( 'Sorry, you are not allowed to update options.' ) );
  3679. }
  3680. $option_names = array();
  3681. foreach ( $options as $o_name => $o_value ) {
  3682. $option_names[] = $o_name;
  3683. if ( ! array_key_exists( $o_name, $this->blog_options ) ) {
  3684. continue;
  3685. }
  3686. if ( true == $this->blog_options[ $o_name ]['readonly'] ) {
  3687. continue;
  3688. }
  3689. update_option( $this->blog_options[ $o_name ]['option'], wp_unslash( $o_value ) );
  3690. }
  3691. // Now return the updated values.
  3692. return $this->_getOptions( $option_names );
  3693. }
  3694. /**
  3695. * Retrieve a media item by ID
  3696. *
  3697. * @since 3.1.0
  3698. *
  3699. * @param array $args {
  3700. * Method arguments. Note: arguments must be ordered as documented.
  3701. *
  3702. * @type int $blog_id (unused)
  3703. * @type string $username
  3704. * @type string $password
  3705. * @type int $attachment_id
  3706. * }
  3707. * @return array|IXR_Error Associative array contains:
  3708. * - 'date_created_gmt'
  3709. * - 'parent'
  3710. * - 'link'
  3711. * - 'thumbnail'
  3712. * - 'title'
  3713. * - 'caption'
  3714. * - 'description'
  3715. * - 'metadata'
  3716. */
  3717. public function wp_getMediaItem( $args ) {
  3718. $this->escape( $args );
  3719. $username = $args[1];
  3720. $password = $args[2];
  3721. $attachment_id = (int) $args[3];
  3722. $user = $this->login( $username, $password );
  3723. if ( ! $user ) {
  3724. return $this->error;
  3725. }
  3726. if ( ! current_user_can( 'upload_files' ) ) {
  3727. return new IXR_Error( 403, __( 'Sorry, you are not allowed to upload files.' ) );
  3728. }
  3729. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3730. do_action( 'xmlrpc_call', 'wp.getMediaItem', $args, $this );
  3731. $attachment = get_post( $attachment_id );
  3732. if ( ! $attachment || 'attachment' !== $attachment->post_type ) {
  3733. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  3734. }
  3735. return $this->_prepare_media_item( $attachment );
  3736. }
  3737. /**
  3738. * Retrieves a collection of media library items (or attachments)
  3739. *
  3740. * Besides the common blog_id (unused), username, and password arguments, it takes a filter
  3741. * array as last argument.
  3742. *
  3743. * Accepted 'filter' keys are 'parent_id', 'mime_type', 'offset', and 'number'.
  3744. *
  3745. * The defaults are as follows:
  3746. * - 'number' - Default is 5. Total number of media items to retrieve.
  3747. * - 'offset' - Default is 0. See WP_Query::query() for more.
  3748. * - 'parent_id' - Default is ''. The post where the media item is attached. Empty string shows all media items. 0 shows unattached media items.
  3749. * - 'mime_type' - Default is ''. Filter by mime type (e.g., 'image/jpeg', 'application/pdf')
  3750. *
  3751. * @since 3.1.0
  3752. *
  3753. * @param array $args {
  3754. * Method arguments. Note: arguments must be ordered as documented.
  3755. *
  3756. * @type int $blog_id (unused)
  3757. * @type string $username
  3758. * @type string $password
  3759. * @type array $struct
  3760. * }
  3761. * @return array|IXR_Error Contains a collection of media items. See wp_xmlrpc_server::wp_getMediaItem() for a description of each item contents
  3762. */
  3763. public function wp_getMediaLibrary( $args ) {
  3764. $this->escape( $args );
  3765. $username = $args[1];
  3766. $password = $args[2];
  3767. $struct = isset( $args[3] ) ? $args[3] : array();
  3768. $user = $this->login( $username, $password );
  3769. if ( ! $user ) {
  3770. return $this->error;
  3771. }
  3772. if ( ! current_user_can( 'upload_files' ) ) {
  3773. return new IXR_Error( 401, __( 'Sorry, you are not allowed to upload files.' ) );
  3774. }
  3775. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3776. do_action( 'xmlrpc_call', 'wp.getMediaLibrary', $args, $this );
  3777. $parent_id = ( isset( $struct['parent_id'] ) ) ? absint( $struct['parent_id'] ) : '';
  3778. $mime_type = ( isset( $struct['mime_type'] ) ) ? $struct['mime_type'] : '';
  3779. $offset = ( isset( $struct['offset'] ) ) ? absint( $struct['offset'] ) : 0;
  3780. $number = ( isset( $struct['number'] ) ) ? absint( $struct['number'] ) : -1;
  3781. $attachments = get_posts(
  3782. array(
  3783. 'post_type' => 'attachment',
  3784. 'post_parent' => $parent_id,
  3785. 'offset' => $offset,
  3786. 'numberposts' => $number,
  3787. 'post_mime_type' => $mime_type,
  3788. )
  3789. );
  3790. $attachments_struct = array();
  3791. foreach ( $attachments as $attachment ) {
  3792. $attachments_struct[] = $this->_prepare_media_item( $attachment );
  3793. }
  3794. return $attachments_struct;
  3795. }
  3796. /**
  3797. * Retrieves a list of post formats used by the site.
  3798. *
  3799. * @since 3.1.0
  3800. *
  3801. * @param array $args {
  3802. * Method arguments. Note: arguments must be ordered as documented.
  3803. *
  3804. * @type int $blog_id (unused)
  3805. * @type string $username
  3806. * @type string $password
  3807. * }
  3808. * @return array|IXR_Error List of post formats, otherwise IXR_Error object.
  3809. */
  3810. public function wp_getPostFormats( $args ) {
  3811. $this->escape( $args );
  3812. $username = $args[1];
  3813. $password = $args[2];
  3814. $user = $this->login( $username, $password );
  3815. if ( ! $user ) {
  3816. return $this->error;
  3817. }
  3818. if ( ! current_user_can( 'edit_posts' ) ) {
  3819. return new IXR_Error( 403, __( 'Sorry, you are not allowed to access details about this site.' ) );
  3820. }
  3821. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3822. do_action( 'xmlrpc_call', 'wp.getPostFormats', $args, $this );
  3823. $formats = get_post_format_strings();
  3824. // Find out if they want a list of currently supports formats.
  3825. if ( isset( $args[3] ) && is_array( $args[3] ) ) {
  3826. if ( $args[3]['show-supported'] ) {
  3827. if ( current_theme_supports( 'post-formats' ) ) {
  3828. $supported = get_theme_support( 'post-formats' );
  3829. $data = array();
  3830. $data['all'] = $formats;
  3831. $data['supported'] = $supported[0];
  3832. $formats = $data;
  3833. }
  3834. }
  3835. }
  3836. return $formats;
  3837. }
  3838. /**
  3839. * Retrieves a post type
  3840. *
  3841. * @since 3.4.0
  3842. *
  3843. * @see get_post_type_object()
  3844. *
  3845. * @param array $args {
  3846. * Method arguments. Note: arguments must be ordered as documented.
  3847. *
  3848. * @type int $blog_id (unused)
  3849. * @type string $username
  3850. * @type string $password
  3851. * @type string $post_type_name
  3852. * @type array $fields (optional)
  3853. * }
  3854. * @return array|IXR_Error Array contains:
  3855. * - 'labels'
  3856. * - 'description'
  3857. * - 'capability_type'
  3858. * - 'cap'
  3859. * - 'map_meta_cap'
  3860. * - 'hierarchical'
  3861. * - 'menu_position'
  3862. * - 'taxonomies'
  3863. * - 'supports'
  3864. */
  3865. public function wp_getPostType( $args ) {
  3866. if ( ! $this->minimum_args( $args, 4 ) ) {
  3867. return $this->error;
  3868. }
  3869. $this->escape( $args );
  3870. $username = $args[1];
  3871. $password = $args[2];
  3872. $post_type_name = $args[3];
  3873. if ( isset( $args[4] ) ) {
  3874. $fields = $args[4];
  3875. } else {
  3876. /**
  3877. * Filters the default query fields used by the given XML-RPC method.
  3878. *
  3879. * @since 3.4.0
  3880. *
  3881. * @param array $fields An array of post type query fields for the given method.
  3882. * @param string $method The method name.
  3883. */
  3884. $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostType' );
  3885. }
  3886. $user = $this->login( $username, $password );
  3887. if ( ! $user ) {
  3888. return $this->error;
  3889. }
  3890. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3891. do_action( 'xmlrpc_call', 'wp.getPostType', $args, $this );
  3892. if ( ! post_type_exists( $post_type_name ) ) {
  3893. return new IXR_Error( 403, __( 'Invalid post type.' ) );
  3894. }
  3895. $post_type = get_post_type_object( $post_type_name );
  3896. if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
  3897. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts in this post type.' ) );
  3898. }
  3899. return $this->_prepare_post_type( $post_type, $fields );
  3900. }
  3901. /**
  3902. * Retrieves a post types
  3903. *
  3904. * @since 3.4.0
  3905. *
  3906. * @see get_post_types()
  3907. *
  3908. * @param array $args {
  3909. * Method arguments. Note: arguments must be ordered as documented.
  3910. *
  3911. * @type int $blog_id (unused)
  3912. * @type string $username
  3913. * @type string $password
  3914. * @type array $filter (optional)
  3915. * @type array $fields (optional)
  3916. * }
  3917. * @return array|IXR_Error
  3918. */
  3919. public function wp_getPostTypes( $args ) {
  3920. if ( ! $this->minimum_args( $args, 3 ) ) {
  3921. return $this->error;
  3922. }
  3923. $this->escape( $args );
  3924. $username = $args[1];
  3925. $password = $args[2];
  3926. $filter = isset( $args[3] ) ? $args[3] : array( 'public' => true );
  3927. if ( isset( $args[4] ) ) {
  3928. $fields = $args[4];
  3929. } else {
  3930. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3931. $fields = apply_filters( 'xmlrpc_default_posttype_fields', array( 'labels', 'cap', 'taxonomies' ), 'wp.getPostTypes' );
  3932. }
  3933. $user = $this->login( $username, $password );
  3934. if ( ! $user ) {
  3935. return $this->error;
  3936. }
  3937. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3938. do_action( 'xmlrpc_call', 'wp.getPostTypes', $args, $this );
  3939. $post_types = get_post_types( $filter, 'objects' );
  3940. $struct = array();
  3941. foreach ( $post_types as $post_type ) {
  3942. if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
  3943. continue;
  3944. }
  3945. $struct[ $post_type->name ] = $this->_prepare_post_type( $post_type, $fields );
  3946. }
  3947. return $struct;
  3948. }
  3949. /**
  3950. * Retrieve revisions for a specific post.
  3951. *
  3952. * @since 3.5.0
  3953. *
  3954. * The optional $fields parameter specifies what fields will be included
  3955. * in the response array.
  3956. *
  3957. * @uses wp_get_post_revisions()
  3958. * @see wp_getPost() for more on $fields
  3959. *
  3960. * @param array $args {
  3961. * Method arguments. Note: arguments must be ordered as documented.
  3962. *
  3963. * @type int $blog_id (unused)
  3964. * @type string $username
  3965. * @type string $password
  3966. * @type int $post_id
  3967. * @type array $fields (optional)
  3968. * }
  3969. * @return array|IXR_Error contains a collection of posts.
  3970. */
  3971. public function wp_getRevisions( $args ) {
  3972. if ( ! $this->minimum_args( $args, 4 ) ) {
  3973. return $this->error;
  3974. }
  3975. $this->escape( $args );
  3976. $username = $args[1];
  3977. $password = $args[2];
  3978. $post_id = (int) $args[3];
  3979. if ( isset( $args[4] ) ) {
  3980. $fields = $args[4];
  3981. } else {
  3982. /**
  3983. * Filters the default revision query fields used by the given XML-RPC method.
  3984. *
  3985. * @since 3.5.0
  3986. *
  3987. * @param array $field An array of revision query fields.
  3988. * @param string $method The method name.
  3989. */
  3990. $fields = apply_filters( 'xmlrpc_default_revision_fields', array( 'post_date', 'post_date_gmt' ), 'wp.getRevisions' );
  3991. }
  3992. $user = $this->login( $username, $password );
  3993. if ( ! $user ) {
  3994. return $this->error;
  3995. }
  3996. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  3997. do_action( 'xmlrpc_call', 'wp.getRevisions', $args, $this );
  3998. $post = get_post( $post_id );
  3999. if ( ! $post ) {
  4000. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4001. }
  4002. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  4003. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  4004. }
  4005. // Check if revisions are enabled.
  4006. if ( ! wp_revisions_enabled( $post ) ) {
  4007. return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
  4008. }
  4009. $revisions = wp_get_post_revisions( $post_id );
  4010. if ( ! $revisions ) {
  4011. return array();
  4012. }
  4013. $struct = array();
  4014. foreach ( $revisions as $revision ) {
  4015. if ( ! current_user_can( 'read_post', $revision->ID ) ) {
  4016. continue;
  4017. }
  4018. // Skip autosaves.
  4019. if ( wp_is_post_autosave( $revision ) ) {
  4020. continue;
  4021. }
  4022. $struct[] = $this->_prepare_post( get_object_vars( $revision ), $fields );
  4023. }
  4024. return $struct;
  4025. }
  4026. /**
  4027. * Restore a post revision
  4028. *
  4029. * @since 3.5.0
  4030. *
  4031. * @uses wp_restore_post_revision()
  4032. *
  4033. * @param array $args {
  4034. * Method arguments. Note: arguments must be ordered as documented.
  4035. *
  4036. * @type int $blog_id (unused)
  4037. * @type string $username
  4038. * @type string $password
  4039. * @type int $revision_id
  4040. * }
  4041. * @return bool|IXR_Error false if there was an error restoring, true if success.
  4042. */
  4043. public function wp_restoreRevision( $args ) {
  4044. if ( ! $this->minimum_args( $args, 3 ) ) {
  4045. return $this->error;
  4046. }
  4047. $this->escape( $args );
  4048. $username = $args[1];
  4049. $password = $args[2];
  4050. $revision_id = (int) $args[3];
  4051. $user = $this->login( $username, $password );
  4052. if ( ! $user ) {
  4053. return $this->error;
  4054. }
  4055. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4056. do_action( 'xmlrpc_call', 'wp.restoreRevision', $args, $this );
  4057. $revision = wp_get_post_revision( $revision_id );
  4058. if ( ! $revision ) {
  4059. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4060. }
  4061. if ( wp_is_post_autosave( $revision ) ) {
  4062. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4063. }
  4064. $post = get_post( $revision->post_parent );
  4065. if ( ! $post ) {
  4066. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4067. }
  4068. if ( ! current_user_can( 'edit_post', $revision->post_parent ) ) {
  4069. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  4070. }
  4071. // Check if revisions are disabled.
  4072. if ( ! wp_revisions_enabled( $post ) ) {
  4073. return new IXR_Error( 401, __( 'Sorry, revisions are disabled.' ) );
  4074. }
  4075. $post = wp_restore_post_revision( $revision_id );
  4076. return (bool) $post;
  4077. }
  4078. /*
  4079. * Blogger API functions.
  4080. * Specs on http://plant.blogger.com/api and https://groups.yahoo.com/group/bloggerDev/
  4081. */
  4082. /**
  4083. * Retrieve blogs that user owns.
  4084. *
  4085. * Will make more sense once we support multiple blogs.
  4086. *
  4087. * @since 1.5.0
  4088. *
  4089. * @param array $args {
  4090. * Method arguments. Note: arguments must be ordered as documented.
  4091. *
  4092. * @type int $blog_id (unused)
  4093. * @type string $username
  4094. * @type string $password
  4095. * }
  4096. * @return array|IXR_Error
  4097. */
  4098. public function blogger_getUsersBlogs( $args ) {
  4099. if ( ! $this->minimum_args( $args, 3 ) ) {
  4100. return $this->error;
  4101. }
  4102. if ( is_multisite() ) {
  4103. return $this->_multisite_getUsersBlogs( $args );
  4104. }
  4105. $this->escape( $args );
  4106. $username = $args[1];
  4107. $password = $args[2];
  4108. $user = $this->login( $username, $password );
  4109. if ( ! $user ) {
  4110. return $this->error;
  4111. }
  4112. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4113. do_action( 'xmlrpc_call', 'blogger.getUsersBlogs', $args, $this );
  4114. $is_admin = current_user_can( 'manage_options' );
  4115. $struct = array(
  4116. 'isAdmin' => $is_admin,
  4117. 'url' => get_option( 'home' ) . '/',
  4118. 'blogid' => '1',
  4119. 'blogName' => get_option( 'blogname' ),
  4120. 'xmlrpc' => site_url( 'xmlrpc.php', 'rpc' ),
  4121. );
  4122. return array( $struct );
  4123. }
  4124. /**
  4125. * Private function for retrieving a users blogs for multisite setups
  4126. *
  4127. * @since 3.0.0
  4128. *
  4129. * @param array $args {
  4130. * Method arguments. Note: arguments must be ordered as documented.
  4131. *
  4132. * @type string $username Username.
  4133. * @type string $password Password.
  4134. * }
  4135. * @return array|IXR_Error
  4136. */
  4137. protected function _multisite_getUsersBlogs( $args ) {
  4138. $current_blog = get_site();
  4139. $domain = $current_blog->domain;
  4140. $path = $current_blog->path . 'xmlrpc.php';
  4141. $rpc = new IXR_Client( set_url_scheme( "http://{$domain}{$path}" ) );
  4142. $rpc->query( 'wp.getUsersBlogs', $args[1], $args[2] );
  4143. $blogs = $rpc->getResponse();
  4144. if ( isset( $blogs['faultCode'] ) ) {
  4145. return new IXR_Error( $blogs['faultCode'], $blogs['faultString'] );
  4146. }
  4147. if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
  4148. return $blogs;
  4149. } else {
  4150. foreach ( (array) $blogs as $blog ) {
  4151. if ( strpos( $blog['url'], $_SERVER['HTTP_HOST'] ) ) {
  4152. return array( $blog );
  4153. }
  4154. }
  4155. return array();
  4156. }
  4157. }
  4158. /**
  4159. * Retrieve user's data.
  4160. *
  4161. * Gives your client some info about you, so you don't have to.
  4162. *
  4163. * @since 1.5.0
  4164. *
  4165. * @param array $args {
  4166. * Method arguments. Note: arguments must be ordered as documented.
  4167. *
  4168. * @type int $blog_id (unused)
  4169. * @type string $username
  4170. * @type string $password
  4171. * }
  4172. * @return array|IXR_Error
  4173. */
  4174. public function blogger_getUserInfo( $args ) {
  4175. $this->escape( $args );
  4176. $username = $args[1];
  4177. $password = $args[2];
  4178. $user = $this->login( $username, $password );
  4179. if ( ! $user ) {
  4180. return $this->error;
  4181. }
  4182. if ( ! current_user_can( 'edit_posts' ) ) {
  4183. return new IXR_Error( 401, __( 'Sorry, you are not allowed to access user data on this site.' ) );
  4184. }
  4185. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4186. do_action( 'xmlrpc_call', 'blogger.getUserInfo', $args, $this );
  4187. $struct = array(
  4188. 'nickname' => $user->nickname,
  4189. 'userid' => $user->ID,
  4190. 'url' => $user->user_url,
  4191. 'lastname' => $user->last_name,
  4192. 'firstname' => $user->first_name,
  4193. );
  4194. return $struct;
  4195. }
  4196. /**
  4197. * Retrieve post.
  4198. *
  4199. * @since 1.5.0
  4200. *
  4201. * @param array $args {
  4202. * Method arguments. Note: arguments must be ordered as documented.
  4203. *
  4204. * @type int $blog_id (unused)
  4205. * @type int $post_ID
  4206. * @type string $username
  4207. * @type string $password
  4208. * }
  4209. * @return array|IXR_Error
  4210. */
  4211. public function blogger_getPost( $args ) {
  4212. $this->escape( $args );
  4213. $post_ID = (int) $args[1];
  4214. $username = $args[2];
  4215. $password = $args[3];
  4216. $user = $this->login( $username, $password );
  4217. if ( ! $user ) {
  4218. return $this->error;
  4219. }
  4220. $post_data = get_post( $post_ID, ARRAY_A );
  4221. if ( ! $post_data ) {
  4222. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4223. }
  4224. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  4225. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  4226. }
  4227. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4228. do_action( 'xmlrpc_call', 'blogger.getPost', $args, $this );
  4229. $categories = implode( ',', wp_get_post_categories( $post_ID ) );
  4230. $content = '<title>' . wp_unslash( $post_data['post_title'] ) . '</title>';
  4231. $content .= '<category>' . $categories . '</category>';
  4232. $content .= wp_unslash( $post_data['post_content'] );
  4233. $struct = array(
  4234. 'userid' => $post_data['post_author'],
  4235. 'dateCreated' => $this->_convert_date( $post_data['post_date'] ),
  4236. 'content' => $content,
  4237. 'postid' => (string) $post_data['ID'],
  4238. );
  4239. return $struct;
  4240. }
  4241. /**
  4242. * Retrieve list of recent posts.
  4243. *
  4244. * @since 1.5.0
  4245. *
  4246. * @param array $args {
  4247. * Method arguments. Note: arguments must be ordered as documented.
  4248. *
  4249. * @type string $appkey (unused)
  4250. * @type int $blog_id (unused)
  4251. * @type string $username
  4252. * @type string $password
  4253. * @type int $numberposts (optional)
  4254. * }
  4255. * @return array|IXR_Error
  4256. */
  4257. public function blogger_getRecentPosts( $args ) {
  4258. $this->escape( $args );
  4259. // $args[0] = appkey - ignored.
  4260. $username = $args[2];
  4261. $password = $args[3];
  4262. if ( isset( $args[4] ) ) {
  4263. $query = array( 'numberposts' => absint( $args[4] ) );
  4264. } else {
  4265. $query = array();
  4266. }
  4267. $user = $this->login( $username, $password );
  4268. if ( ! $user ) {
  4269. return $this->error;
  4270. }
  4271. if ( ! current_user_can( 'edit_posts' ) ) {
  4272. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  4273. }
  4274. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4275. do_action( 'xmlrpc_call', 'blogger.getRecentPosts', $args, $this );
  4276. $posts_list = wp_get_recent_posts( $query );
  4277. if ( ! $posts_list ) {
  4278. $this->error = new IXR_Error( 500, __( 'Either there are no posts, or something went wrong.' ) );
  4279. return $this->error;
  4280. }
  4281. $recent_posts = array();
  4282. foreach ( $posts_list as $entry ) {
  4283. if ( ! current_user_can( 'edit_post', $entry['ID'] ) ) {
  4284. continue;
  4285. }
  4286. $post_date = $this->_convert_date( $entry['post_date'] );
  4287. $categories = implode( ',', wp_get_post_categories( $entry['ID'] ) );
  4288. $content = '<title>' . wp_unslash( $entry['post_title'] ) . '</title>';
  4289. $content .= '<category>' . $categories . '</category>';
  4290. $content .= wp_unslash( $entry['post_content'] );
  4291. $recent_posts[] = array(
  4292. 'userid' => $entry['post_author'],
  4293. 'dateCreated' => $post_date,
  4294. 'content' => $content,
  4295. 'postid' => (string) $entry['ID'],
  4296. );
  4297. }
  4298. return $recent_posts;
  4299. }
  4300. /**
  4301. * Deprecated.
  4302. *
  4303. * @since 1.5.0
  4304. * @deprecated 3.5.0
  4305. *
  4306. * @param array $args Unused.
  4307. * @return IXR_Error Error object.
  4308. */
  4309. public function blogger_getTemplate( $args ) {
  4310. return new IXR_Error( 403, __( 'Sorry, this method is not supported.' ) );
  4311. }
  4312. /**
  4313. * Deprecated.
  4314. *
  4315. * @since 1.5.0
  4316. * @deprecated 3.5.0
  4317. *
  4318. * @param array $args Unused.
  4319. * @return IXR_Error Error object.
  4320. */
  4321. public function blogger_setTemplate( $args ) {
  4322. return new IXR_Error( 403, __( 'Sorry, this method is not supported.' ) );
  4323. }
  4324. /**
  4325. * Creates new post.
  4326. *
  4327. * @since 1.5.0
  4328. *
  4329. * @param array $args {
  4330. * Method arguments. Note: arguments must be ordered as documented.
  4331. *
  4332. * @type string $appkey (unused)
  4333. * @type int $blog_id (unused)
  4334. * @type string $username
  4335. * @type string $password
  4336. * @type string $content
  4337. * @type string $publish
  4338. * }
  4339. * @return int|IXR_Error
  4340. */
  4341. public function blogger_newPost( $args ) {
  4342. $this->escape( $args );
  4343. $username = $args[2];
  4344. $password = $args[3];
  4345. $content = $args[4];
  4346. $publish = $args[5];
  4347. $user = $this->login( $username, $password );
  4348. if ( ! $user ) {
  4349. return $this->error;
  4350. }
  4351. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4352. do_action( 'xmlrpc_call', 'blogger.newPost', $args, $this );
  4353. $cap = ( $publish ) ? 'publish_posts' : 'edit_posts';
  4354. if ( ! current_user_can( get_post_type_object( 'post' )->cap->create_posts ) || ! current_user_can( $cap ) ) {
  4355. return new IXR_Error( 401, __( 'Sorry, you are not allowed to post on this site.' ) );
  4356. }
  4357. $post_status = ( $publish ) ? 'publish' : 'draft';
  4358. $post_author = $user->ID;
  4359. $post_title = xmlrpc_getposttitle( $content );
  4360. $post_category = xmlrpc_getpostcategory( $content );
  4361. $post_content = xmlrpc_removepostdata( $content );
  4362. $post_date = current_time( 'mysql' );
  4363. $post_date_gmt = current_time( 'mysql', 1 );
  4364. $post_data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status' );
  4365. $post_ID = wp_insert_post( $post_data );
  4366. if ( is_wp_error( $post_ID ) ) {
  4367. return new IXR_Error( 500, $post_ID->get_error_message() );
  4368. }
  4369. if ( ! $post_ID ) {
  4370. return new IXR_Error( 500, __( 'Sorry, the post could not be created.' ) );
  4371. }
  4372. $this->attach_uploads( $post_ID, $post_content );
  4373. /**
  4374. * Fires after a new post has been successfully created via the XML-RPC Blogger API.
  4375. *
  4376. * @since 3.4.0
  4377. *
  4378. * @param int $post_ID ID of the new post.
  4379. * @param array $args An array of new post arguments.
  4380. */
  4381. do_action( 'xmlrpc_call_success_blogger_newPost', $post_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  4382. return $post_ID;
  4383. }
  4384. /**
  4385. * Edit a post.
  4386. *
  4387. * @since 1.5.0
  4388. *
  4389. * @param array $args {
  4390. * Method arguments. Note: arguments must be ordered as documented.
  4391. *
  4392. * @type int $blog_id (unused)
  4393. * @type int $post_ID
  4394. * @type string $username
  4395. * @type string $password
  4396. * @type string $content
  4397. * @type bool $publish
  4398. * }
  4399. * @return true|IXR_Error true when done.
  4400. */
  4401. public function blogger_editPost( $args ) {
  4402. $this->escape( $args );
  4403. $post_ID = (int) $args[1];
  4404. $username = $args[2];
  4405. $password = $args[3];
  4406. $content = $args[4];
  4407. $publish = $args[5];
  4408. $user = $this->login( $username, $password );
  4409. if ( ! $user ) {
  4410. return $this->error;
  4411. }
  4412. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4413. do_action( 'xmlrpc_call', 'blogger.editPost', $args, $this );
  4414. $actual_post = get_post( $post_ID, ARRAY_A );
  4415. if ( ! $actual_post || 'post' !== $actual_post['post_type'] ) {
  4416. return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
  4417. }
  4418. $this->escape( $actual_post );
  4419. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  4420. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  4421. }
  4422. if ( 'publish' === $actual_post['post_status'] && ! current_user_can( 'publish_posts' ) ) {
  4423. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this post.' ) );
  4424. }
  4425. $postdata = array();
  4426. $postdata['ID'] = $actual_post['ID'];
  4427. $postdata['post_content'] = xmlrpc_removepostdata( $content );
  4428. $postdata['post_title'] = xmlrpc_getposttitle( $content );
  4429. $postdata['post_category'] = xmlrpc_getpostcategory( $content );
  4430. $postdata['post_status'] = $actual_post['post_status'];
  4431. $postdata['post_excerpt'] = $actual_post['post_excerpt'];
  4432. $postdata['post_status'] = $publish ? 'publish' : 'draft';
  4433. $result = wp_update_post( $postdata );
  4434. if ( ! $result ) {
  4435. return new IXR_Error( 500, __( 'Sorry, the post could not be updated.' ) );
  4436. }
  4437. $this->attach_uploads( $actual_post['ID'], $postdata['post_content'] );
  4438. /**
  4439. * Fires after a post has been successfully updated via the XML-RPC Blogger API.
  4440. *
  4441. * @since 3.4.0
  4442. *
  4443. * @param int $post_ID ID of the updated post.
  4444. * @param array $args An array of arguments for the post to edit.
  4445. */
  4446. do_action( 'xmlrpc_call_success_blogger_editPost', $post_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  4447. return true;
  4448. }
  4449. /**
  4450. * Remove a post.
  4451. *
  4452. * @since 1.5.0
  4453. *
  4454. * @param array $args {
  4455. * Method arguments. Note: arguments must be ordered as documented.
  4456. *
  4457. * @type int $blog_id (unused)
  4458. * @type int $post_ID
  4459. * @type string $username
  4460. * @type string $password
  4461. * }
  4462. * @return true|IXR_Error True when post is deleted.
  4463. */
  4464. public function blogger_deletePost( $args ) {
  4465. $this->escape( $args );
  4466. $post_ID = (int) $args[1];
  4467. $username = $args[2];
  4468. $password = $args[3];
  4469. $user = $this->login( $username, $password );
  4470. if ( ! $user ) {
  4471. return $this->error;
  4472. }
  4473. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4474. do_action( 'xmlrpc_call', 'blogger.deletePost', $args, $this );
  4475. $actual_post = get_post( $post_ID, ARRAY_A );
  4476. if ( ! $actual_post || 'post' !== $actual_post['post_type'] ) {
  4477. return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
  4478. }
  4479. if ( ! current_user_can( 'delete_post', $post_ID ) ) {
  4480. return new IXR_Error( 401, __( 'Sorry, you are not allowed to delete this post.' ) );
  4481. }
  4482. $result = wp_delete_post( $post_ID );
  4483. if ( ! $result ) {
  4484. return new IXR_Error( 500, __( 'Sorry, the post could not be deleted.' ) );
  4485. }
  4486. /**
  4487. * Fires after a post has been successfully deleted via the XML-RPC Blogger API.
  4488. *
  4489. * @since 3.4.0
  4490. *
  4491. * @param int $post_ID ID of the deleted post.
  4492. * @param array $args An array of arguments to delete the post.
  4493. */
  4494. do_action( 'xmlrpc_call_success_blogger_deletePost', $post_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  4495. return true;
  4496. }
  4497. /*
  4498. * MetaWeblog API functions.
  4499. * Specs on wherever Dave Winer wants them to be.
  4500. */
  4501. /**
  4502. * Create a new post.
  4503. *
  4504. * The 'content_struct' argument must contain:
  4505. * - title
  4506. * - description
  4507. * - mt_excerpt
  4508. * - mt_text_more
  4509. * - mt_keywords
  4510. * - mt_tb_ping_urls
  4511. * - categories
  4512. *
  4513. * Also, it can optionally contain:
  4514. * - wp_slug
  4515. * - wp_password
  4516. * - wp_page_parent_id
  4517. * - wp_page_order
  4518. * - wp_author_id
  4519. * - post_status | page_status - can be 'draft', 'private', 'publish', or 'pending'
  4520. * - mt_allow_comments - can be 'open' or 'closed'
  4521. * - mt_allow_pings - can be 'open' or 'closed'
  4522. * - date_created_gmt
  4523. * - dateCreated
  4524. * - wp_post_thumbnail
  4525. *
  4526. * @since 1.5.0
  4527. *
  4528. * @param array $args {
  4529. * Method arguments. Note: arguments must be ordered as documented.
  4530. *
  4531. * @type int $blog_id (unused)
  4532. * @type string $username
  4533. * @type string $password
  4534. * @type array $content_struct
  4535. * @type int $publish
  4536. * }
  4537. * @return int|IXR_Error
  4538. */
  4539. public function mw_newPost( $args ) {
  4540. $this->escape( $args );
  4541. $username = $args[1];
  4542. $password = $args[2];
  4543. $content_struct = $args[3];
  4544. $publish = isset( $args[4] ) ? $args[4] : 0;
  4545. $user = $this->login( $username, $password );
  4546. if ( ! $user ) {
  4547. return $this->error;
  4548. }
  4549. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4550. do_action( 'xmlrpc_call', 'metaWeblog.newPost', $args, $this );
  4551. $page_template = '';
  4552. if ( ! empty( $content_struct['post_type'] ) ) {
  4553. if ( 'page' === $content_struct['post_type'] ) {
  4554. if ( $publish ) {
  4555. $cap = 'publish_pages';
  4556. } elseif ( isset( $content_struct['page_status'] ) && 'publish' === $content_struct['page_status'] ) {
  4557. $cap = 'publish_pages';
  4558. } else {
  4559. $cap = 'edit_pages';
  4560. }
  4561. $error_message = __( 'Sorry, you are not allowed to publish pages on this site.' );
  4562. $post_type = 'page';
  4563. if ( ! empty( $content_struct['wp_page_template'] ) ) {
  4564. $page_template = $content_struct['wp_page_template'];
  4565. }
  4566. } elseif ( 'post' === $content_struct['post_type'] ) {
  4567. if ( $publish ) {
  4568. $cap = 'publish_posts';
  4569. } elseif ( isset( $content_struct['post_status'] ) && 'publish' === $content_struct['post_status'] ) {
  4570. $cap = 'publish_posts';
  4571. } else {
  4572. $cap = 'edit_posts';
  4573. }
  4574. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  4575. $post_type = 'post';
  4576. } else {
  4577. // No other 'post_type' values are allowed here.
  4578. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  4579. }
  4580. } else {
  4581. if ( $publish ) {
  4582. $cap = 'publish_posts';
  4583. } elseif ( isset( $content_struct['post_status'] ) && 'publish' === $content_struct['post_status'] ) {
  4584. $cap = 'publish_posts';
  4585. } else {
  4586. $cap = 'edit_posts';
  4587. }
  4588. $error_message = __( 'Sorry, you are not allowed to publish posts on this site.' );
  4589. $post_type = 'post';
  4590. }
  4591. if ( ! current_user_can( get_post_type_object( $post_type )->cap->create_posts ) ) {
  4592. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts on this site.' ) );
  4593. }
  4594. if ( ! current_user_can( $cap ) ) {
  4595. return new IXR_Error( 401, $error_message );
  4596. }
  4597. // Check for a valid post format if one was given.
  4598. if ( isset( $content_struct['wp_post_format'] ) ) {
  4599. $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  4600. if ( ! array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  4601. return new IXR_Error( 404, __( 'Invalid post format.' ) );
  4602. }
  4603. }
  4604. // Let WordPress generate the 'post_name' (slug) unless
  4605. // one has been provided.
  4606. $post_name = '';
  4607. if ( isset( $content_struct['wp_slug'] ) ) {
  4608. $post_name = $content_struct['wp_slug'];
  4609. }
  4610. // Only use a password if one was given.
  4611. if ( isset( $content_struct['wp_password'] ) ) {
  4612. $post_password = $content_struct['wp_password'];
  4613. } else {
  4614. $post_password = '';
  4615. }
  4616. // Only set a post parent if one was given.
  4617. if ( isset( $content_struct['wp_page_parent_id'] ) ) {
  4618. $post_parent = $content_struct['wp_page_parent_id'];
  4619. } else {
  4620. $post_parent = 0;
  4621. }
  4622. // Only set the 'menu_order' if it was given.
  4623. if ( isset( $content_struct['wp_page_order'] ) ) {
  4624. $menu_order = $content_struct['wp_page_order'];
  4625. } else {
  4626. $menu_order = 0;
  4627. }
  4628. $post_author = $user->ID;
  4629. // If an author id was provided then use it instead.
  4630. if ( isset( $content_struct['wp_author_id'] ) && ( $user->ID != $content_struct['wp_author_id'] ) ) {
  4631. switch ( $post_type ) {
  4632. case 'post':
  4633. if ( ! current_user_can( 'edit_others_posts' ) ) {
  4634. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create posts as this user.' ) );
  4635. }
  4636. break;
  4637. case 'page':
  4638. if ( ! current_user_can( 'edit_others_pages' ) ) {
  4639. return new IXR_Error( 401, __( 'Sorry, you are not allowed to create pages as this user.' ) );
  4640. }
  4641. break;
  4642. default:
  4643. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  4644. }
  4645. $author = get_userdata( $content_struct['wp_author_id'] );
  4646. if ( ! $author ) {
  4647. return new IXR_Error( 404, __( 'Invalid author ID.' ) );
  4648. }
  4649. $post_author = $content_struct['wp_author_id'];
  4650. }
  4651. $post_title = isset( $content_struct['title'] ) ? $content_struct['title'] : null;
  4652. $post_content = isset( $content_struct['description'] ) ? $content_struct['description'] : null;
  4653. $post_status = $publish ? 'publish' : 'draft';
  4654. if ( isset( $content_struct[ "{$post_type}_status" ] ) ) {
  4655. switch ( $content_struct[ "{$post_type}_status" ] ) {
  4656. case 'draft':
  4657. case 'pending':
  4658. case 'private':
  4659. case 'publish':
  4660. $post_status = $content_struct[ "{$post_type}_status" ];
  4661. break;
  4662. default:
  4663. $post_status = $publish ? 'publish' : 'draft';
  4664. break;
  4665. }
  4666. }
  4667. $post_excerpt = isset( $content_struct['mt_excerpt'] ) ? $content_struct['mt_excerpt'] : null;
  4668. $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
  4669. $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
  4670. if ( isset( $content_struct['mt_allow_comments'] ) ) {
  4671. if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
  4672. switch ( $content_struct['mt_allow_comments'] ) {
  4673. case 'closed':
  4674. $comment_status = 'closed';
  4675. break;
  4676. case 'open':
  4677. $comment_status = 'open';
  4678. break;
  4679. default:
  4680. $comment_status = get_default_comment_status( $post_type );
  4681. break;
  4682. }
  4683. } else {
  4684. switch ( (int) $content_struct['mt_allow_comments'] ) {
  4685. case 0:
  4686. case 2:
  4687. $comment_status = 'closed';
  4688. break;
  4689. case 1:
  4690. $comment_status = 'open';
  4691. break;
  4692. default:
  4693. $comment_status = get_default_comment_status( $post_type );
  4694. break;
  4695. }
  4696. }
  4697. } else {
  4698. $comment_status = get_default_comment_status( $post_type );
  4699. }
  4700. if ( isset( $content_struct['mt_allow_pings'] ) ) {
  4701. if ( ! is_numeric( $content_struct['mt_allow_pings'] ) ) {
  4702. switch ( $content_struct['mt_allow_pings'] ) {
  4703. case 'closed':
  4704. $ping_status = 'closed';
  4705. break;
  4706. case 'open':
  4707. $ping_status = 'open';
  4708. break;
  4709. default:
  4710. $ping_status = get_default_comment_status( $post_type, 'pingback' );
  4711. break;
  4712. }
  4713. } else {
  4714. switch ( (int) $content_struct['mt_allow_pings'] ) {
  4715. case 0:
  4716. $ping_status = 'closed';
  4717. break;
  4718. case 1:
  4719. $ping_status = 'open';
  4720. break;
  4721. default:
  4722. $ping_status = get_default_comment_status( $post_type, 'pingback' );
  4723. break;
  4724. }
  4725. }
  4726. } else {
  4727. $ping_status = get_default_comment_status( $post_type, 'pingback' );
  4728. }
  4729. if ( $post_more ) {
  4730. $post_content = $post_content . '<!--more-->' . $post_more;
  4731. }
  4732. $to_ping = null;
  4733. if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  4734. $to_ping = $content_struct['mt_tb_ping_urls'];
  4735. if ( is_array( $to_ping ) ) {
  4736. $to_ping = implode( ' ', $to_ping );
  4737. }
  4738. }
  4739. // Do some timestamp voodoo.
  4740. if ( ! empty( $content_struct['date_created_gmt'] ) ) {
  4741. // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
  4742. $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  4743. } elseif ( ! empty( $content_struct['dateCreated'] ) ) {
  4744. $dateCreated = $content_struct['dateCreated']->getIso();
  4745. }
  4746. if ( ! empty( $dateCreated ) ) {
  4747. $post_date = iso8601_to_datetime( $dateCreated );
  4748. $post_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' );
  4749. } else {
  4750. $post_date = '';
  4751. $post_date_gmt = '';
  4752. }
  4753. $post_category = array();
  4754. if ( isset( $content_struct['categories'] ) ) {
  4755. $catnames = $content_struct['categories'];
  4756. if ( is_array( $catnames ) ) {
  4757. foreach ( $catnames as $cat ) {
  4758. $post_category[] = get_cat_ID( $cat );
  4759. }
  4760. }
  4761. }
  4762. $postdata = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'to_ping', 'post_type', 'post_name', 'post_password', 'post_parent', 'menu_order', 'tags_input', 'page_template' );
  4763. $post_ID = get_default_post_to_edit( $post_type, true )->ID;
  4764. $postdata['ID'] = $post_ID;
  4765. // Only posts can be sticky.
  4766. if ( 'post' === $post_type && isset( $content_struct['sticky'] ) ) {
  4767. $data = $postdata;
  4768. $data['sticky'] = $content_struct['sticky'];
  4769. $error = $this->_toggle_sticky( $data );
  4770. if ( $error ) {
  4771. return $error;
  4772. }
  4773. }
  4774. if ( isset( $content_struct['custom_fields'] ) ) {
  4775. $this->set_custom_fields( $post_ID, $content_struct['custom_fields'] );
  4776. }
  4777. if ( isset( $content_struct['wp_post_thumbnail'] ) ) {
  4778. if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false ) {
  4779. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  4780. }
  4781. unset( $content_struct['wp_post_thumbnail'] );
  4782. }
  4783. // Handle enclosures.
  4784. $thisEnclosure = isset( $content_struct['enclosure'] ) ? $content_struct['enclosure'] : null;
  4785. $this->add_enclosure_if_new( $post_ID, $thisEnclosure );
  4786. $this->attach_uploads( $post_ID, $post_content );
  4787. // Handle post formats if assigned, value is validated earlier
  4788. // in this function.
  4789. if ( isset( $content_struct['wp_post_format'] ) ) {
  4790. set_post_format( $post_ID, $content_struct['wp_post_format'] );
  4791. }
  4792. $post_ID = wp_insert_post( $postdata, true );
  4793. if ( is_wp_error( $post_ID ) ) {
  4794. return new IXR_Error( 500, $post_ID->get_error_message() );
  4795. }
  4796. if ( ! $post_ID ) {
  4797. return new IXR_Error( 500, __( 'Sorry, the post could not be created.' ) );
  4798. }
  4799. /**
  4800. * Fires after a new post has been successfully created via the XML-RPC MovableType API.
  4801. *
  4802. * @since 3.4.0
  4803. *
  4804. * @param int $post_ID ID of the new post.
  4805. * @param array $args An array of arguments to create the new post.
  4806. */
  4807. do_action( 'xmlrpc_call_success_mw_newPost', $post_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  4808. return (string) $post_ID;
  4809. }
  4810. /**
  4811. * Adds an enclosure to a post if it's new.
  4812. *
  4813. * @since 2.8.0
  4814. *
  4815. * @param int $post_ID Post ID.
  4816. * @param array $enclosure Enclosure data.
  4817. */
  4818. public function add_enclosure_if_new( $post_ID, $enclosure ) {
  4819. if ( is_array( $enclosure ) && isset( $enclosure['url'] ) && isset( $enclosure['length'] ) && isset( $enclosure['type'] ) ) {
  4820. $encstring = $enclosure['url'] . "\n" . $enclosure['length'] . "\n" . $enclosure['type'] . "\n";
  4821. $found = false;
  4822. $enclosures = get_post_meta( $post_ID, 'enclosure' );
  4823. if ( $enclosures ) {
  4824. foreach ( $enclosures as $enc ) {
  4825. // This method used to omit the trailing new line. #23219
  4826. if ( rtrim( $enc, "\n" ) == rtrim( $encstring, "\n" ) ) {
  4827. $found = true;
  4828. break;
  4829. }
  4830. }
  4831. }
  4832. if ( ! $found ) {
  4833. add_post_meta( $post_ID, 'enclosure', $encstring );
  4834. }
  4835. }
  4836. }
  4837. /**
  4838. * Attach upload to a post.
  4839. *
  4840. * @since 2.1.0
  4841. *
  4842. * @global wpdb $wpdb WordPress database abstraction object.
  4843. *
  4844. * @param int $post_ID Post ID.
  4845. * @param string $post_content Post Content for attachment.
  4846. */
  4847. public function attach_uploads( $post_ID, $post_content ) {
  4848. global $wpdb;
  4849. // Find any unattached files.
  4850. $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
  4851. if ( is_array( $attachments ) ) {
  4852. foreach ( $attachments as $file ) {
  4853. if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false ) {
  4854. $wpdb->update( $wpdb->posts, array( 'post_parent' => $post_ID ), array( 'ID' => $file->ID ) );
  4855. }
  4856. }
  4857. }
  4858. }
  4859. /**
  4860. * Edit a post.
  4861. *
  4862. * @since 1.5.0
  4863. *
  4864. * @param array $args {
  4865. * Method arguments. Note: arguments must be ordered as documented.
  4866. *
  4867. * @type int $blog_id (unused)
  4868. * @type string $username
  4869. * @type string $password
  4870. * @type array $content_struct
  4871. * @type int $publish
  4872. * }
  4873. * @return true|IXR_Error True on success.
  4874. */
  4875. public function mw_editPost( $args ) {
  4876. $this->escape( $args );
  4877. $post_ID = (int) $args[0];
  4878. $username = $args[1];
  4879. $password = $args[2];
  4880. $content_struct = $args[3];
  4881. $publish = isset( $args[4] ) ? $args[4] : 0;
  4882. $user = $this->login( $username, $password );
  4883. if ( ! $user ) {
  4884. return $this->error;
  4885. }
  4886. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  4887. do_action( 'xmlrpc_call', 'metaWeblog.editPost', $args, $this );
  4888. $postdata = get_post( $post_ID, ARRAY_A );
  4889. /*
  4890. * If there is no post data for the give post ID, stop now and return an error.
  4891. * Otherwise a new post will be created (which was the old behavior).
  4892. */
  4893. if ( ! $postdata || empty( $postdata['ID'] ) ) {
  4894. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  4895. }
  4896. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  4897. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  4898. }
  4899. // Use wp.editPost to edit post types other than post and page.
  4900. if ( ! in_array( $postdata['post_type'], array( 'post', 'page' ), true ) ) {
  4901. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  4902. }
  4903. // Thwart attempt to change the post type.
  4904. if ( ! empty( $content_struct['post_type'] ) && ( $content_struct['post_type'] != $postdata['post_type'] ) ) {
  4905. return new IXR_Error( 401, __( 'The post type may not be changed.' ) );
  4906. }
  4907. // Check for a valid post format if one was given.
  4908. if ( isset( $content_struct['wp_post_format'] ) ) {
  4909. $content_struct['wp_post_format'] = sanitize_key( $content_struct['wp_post_format'] );
  4910. if ( ! array_key_exists( $content_struct['wp_post_format'], get_post_format_strings() ) ) {
  4911. return new IXR_Error( 404, __( 'Invalid post format.' ) );
  4912. }
  4913. }
  4914. $this->escape( $postdata );
  4915. $ID = $postdata['ID'];
  4916. $post_content = $postdata['post_content'];
  4917. $post_title = $postdata['post_title'];
  4918. $post_excerpt = $postdata['post_excerpt'];
  4919. $post_password = $postdata['post_password'];
  4920. $post_parent = $postdata['post_parent'];
  4921. $post_type = $postdata['post_type'];
  4922. $menu_order = $postdata['menu_order'];
  4923. $ping_status = $postdata['ping_status'];
  4924. $comment_status = $postdata['comment_status'];
  4925. // Let WordPress manage slug if none was provided.
  4926. $post_name = $postdata['post_name'];
  4927. if ( isset( $content_struct['wp_slug'] ) ) {
  4928. $post_name = $content_struct['wp_slug'];
  4929. }
  4930. // Only use a password if one was given.
  4931. if ( isset( $content_struct['wp_password'] ) ) {
  4932. $post_password = $content_struct['wp_password'];
  4933. }
  4934. // Only set a post parent if one was given.
  4935. if ( isset( $content_struct['wp_page_parent_id'] ) ) {
  4936. $post_parent = $content_struct['wp_page_parent_id'];
  4937. }
  4938. // Only set the 'menu_order' if it was given.
  4939. if ( isset( $content_struct['wp_page_order'] ) ) {
  4940. $menu_order = $content_struct['wp_page_order'];
  4941. }
  4942. $page_template = null;
  4943. if ( ! empty( $content_struct['wp_page_template'] ) && 'page' === $post_type ) {
  4944. $page_template = $content_struct['wp_page_template'];
  4945. }
  4946. $post_author = $postdata['post_author'];
  4947. // If an author id was provided then use it instead.
  4948. if ( isset( $content_struct['wp_author_id'] ) ) {
  4949. // Check permissions if attempting to switch author to or from another user.
  4950. if ( $user->ID != $content_struct['wp_author_id'] || $user->ID != $post_author ) {
  4951. switch ( $post_type ) {
  4952. case 'post':
  4953. if ( ! current_user_can( 'edit_others_posts' ) ) {
  4954. return new IXR_Error( 401, __( 'Sorry, you are not allowed to change the post author as this user.' ) );
  4955. }
  4956. break;
  4957. case 'page':
  4958. if ( ! current_user_can( 'edit_others_pages' ) ) {
  4959. return new IXR_Error( 401, __( 'Sorry, you are not allowed to change the page author as this user.' ) );
  4960. }
  4961. break;
  4962. default:
  4963. return new IXR_Error( 401, __( 'Invalid post type.' ) );
  4964. }
  4965. $post_author = $content_struct['wp_author_id'];
  4966. }
  4967. }
  4968. if ( isset( $content_struct['mt_allow_comments'] ) ) {
  4969. if ( ! is_numeric( $content_struct['mt_allow_comments'] ) ) {
  4970. switch ( $content_struct['mt_allow_comments'] ) {
  4971. case 'closed':
  4972. $comment_status = 'closed';
  4973. break;
  4974. case 'open':
  4975. $comment_status = 'open';
  4976. break;
  4977. default:
  4978. $comment_status = get_default_comment_status( $post_type );
  4979. break;
  4980. }
  4981. } else {
  4982. switch ( (int) $content_struct['mt_allow_comments'] ) {
  4983. case 0:
  4984. case 2:
  4985. $comment_status = 'closed';
  4986. break;
  4987. case 1:
  4988. $comment_status = 'open';
  4989. break;
  4990. default:
  4991. $comment_status = get_default_comment_status( $post_type );
  4992. break;
  4993. }
  4994. }
  4995. }
  4996. if ( isset( $content_struct['mt_allow_pings'] ) ) {
  4997. if ( ! is_numeric( $content_struct['mt_allow_pings'] ) ) {
  4998. switch ( $content_struct['mt_allow_pings'] ) {
  4999. case 'closed':
  5000. $ping_status = 'closed';
  5001. break;
  5002. case 'open':
  5003. $ping_status = 'open';
  5004. break;
  5005. default:
  5006. $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5007. break;
  5008. }
  5009. } else {
  5010. switch ( (int) $content_struct['mt_allow_pings'] ) {
  5011. case 0:
  5012. $ping_status = 'closed';
  5013. break;
  5014. case 1:
  5015. $ping_status = 'open';
  5016. break;
  5017. default:
  5018. $ping_status = get_default_comment_status( $post_type, 'pingback' );
  5019. break;
  5020. }
  5021. }
  5022. }
  5023. if ( isset( $content_struct['title'] ) ) {
  5024. $post_title = $content_struct['title'];
  5025. }
  5026. if ( isset( $content_struct['description'] ) ) {
  5027. $post_content = $content_struct['description'];
  5028. }
  5029. $post_category = array();
  5030. if ( isset( $content_struct['categories'] ) ) {
  5031. $catnames = $content_struct['categories'];
  5032. if ( is_array( $catnames ) ) {
  5033. foreach ( $catnames as $cat ) {
  5034. $post_category[] = get_cat_ID( $cat );
  5035. }
  5036. }
  5037. }
  5038. if ( isset( $content_struct['mt_excerpt'] ) ) {
  5039. $post_excerpt = $content_struct['mt_excerpt'];
  5040. }
  5041. $post_more = isset( $content_struct['mt_text_more'] ) ? $content_struct['mt_text_more'] : null;
  5042. $post_status = $publish ? 'publish' : 'draft';
  5043. if ( isset( $content_struct[ "{$post_type}_status" ] ) ) {
  5044. switch ( $content_struct[ "{$post_type}_status" ] ) {
  5045. case 'draft':
  5046. case 'pending':
  5047. case 'private':
  5048. case 'publish':
  5049. $post_status = $content_struct[ "{$post_type}_status" ];
  5050. break;
  5051. default:
  5052. $post_status = $publish ? 'publish' : 'draft';
  5053. break;
  5054. }
  5055. }
  5056. $tags_input = isset( $content_struct['mt_keywords'] ) ? $content_struct['mt_keywords'] : null;
  5057. if ( 'publish' === $post_status || 'private' === $post_status ) {
  5058. if ( 'page' === $post_type && ! current_user_can( 'publish_pages' ) ) {
  5059. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this page.' ) );
  5060. } elseif ( ! current_user_can( 'publish_posts' ) ) {
  5061. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this post.' ) );
  5062. }
  5063. }
  5064. if ( $post_more ) {
  5065. $post_content = $post_content . '<!--more-->' . $post_more;
  5066. }
  5067. $to_ping = null;
  5068. if ( isset( $content_struct['mt_tb_ping_urls'] ) ) {
  5069. $to_ping = $content_struct['mt_tb_ping_urls'];
  5070. if ( is_array( $to_ping ) ) {
  5071. $to_ping = implode( ' ', $to_ping );
  5072. }
  5073. }
  5074. // Do some timestamp voodoo.
  5075. if ( ! empty( $content_struct['date_created_gmt'] ) ) {
  5076. // We know this is supposed to be GMT, so we're going to slap that Z on there by force.
  5077. $dateCreated = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z';
  5078. } elseif ( ! empty( $content_struct['dateCreated'] ) ) {
  5079. $dateCreated = $content_struct['dateCreated']->getIso();
  5080. }
  5081. // Default to not flagging the post date to be edited unless it's intentional.
  5082. $edit_date = false;
  5083. if ( ! empty( $dateCreated ) ) {
  5084. $post_date = iso8601_to_datetime( $dateCreated );
  5085. $post_date_gmt = iso8601_to_datetime( $dateCreated, 'gmt' );
  5086. // Flag the post date to be edited.
  5087. $edit_date = true;
  5088. } else {
  5089. $post_date = $postdata['post_date'];
  5090. $post_date_gmt = $postdata['post_date_gmt'];
  5091. }
  5092. // We've got all the data -- post it.
  5093. $newpost = compact( 'ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'edit_date', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template' );
  5094. $result = wp_update_post( $newpost, true );
  5095. if ( is_wp_error( $result ) ) {
  5096. return new IXR_Error( 500, $result->get_error_message() );
  5097. }
  5098. if ( ! $result ) {
  5099. return new IXR_Error( 500, __( 'Sorry, the post could not be updated.' ) );
  5100. }
  5101. // Only posts can be sticky.
  5102. if ( 'post' === $post_type && isset( $content_struct['sticky'] ) ) {
  5103. $data = $newpost;
  5104. $data['sticky'] = $content_struct['sticky'];
  5105. $data['post_type'] = 'post';
  5106. $error = $this->_toggle_sticky( $data, true );
  5107. if ( $error ) {
  5108. return $error;
  5109. }
  5110. }
  5111. if ( isset( $content_struct['custom_fields'] ) ) {
  5112. $this->set_custom_fields( $post_ID, $content_struct['custom_fields'] );
  5113. }
  5114. if ( isset( $content_struct['wp_post_thumbnail'] ) ) {
  5115. // Empty value deletes, non-empty value adds/updates.
  5116. if ( empty( $content_struct['wp_post_thumbnail'] ) ) {
  5117. delete_post_thumbnail( $post_ID );
  5118. } else {
  5119. if ( set_post_thumbnail( $post_ID, $content_struct['wp_post_thumbnail'] ) === false ) {
  5120. return new IXR_Error( 404, __( 'Invalid attachment ID.' ) );
  5121. }
  5122. }
  5123. unset( $content_struct['wp_post_thumbnail'] );
  5124. }
  5125. // Handle enclosures.
  5126. $thisEnclosure = isset( $content_struct['enclosure'] ) ? $content_struct['enclosure'] : null;
  5127. $this->add_enclosure_if_new( $post_ID, $thisEnclosure );
  5128. $this->attach_uploads( $ID, $post_content );
  5129. // Handle post formats if assigned, validation is handled earlier in this function.
  5130. if ( isset( $content_struct['wp_post_format'] ) ) {
  5131. set_post_format( $post_ID, $content_struct['wp_post_format'] );
  5132. }
  5133. /**
  5134. * Fires after a post has been successfully updated via the XML-RPC MovableType API.
  5135. *
  5136. * @since 3.4.0
  5137. *
  5138. * @param int $post_ID ID of the updated post.
  5139. * @param array $args An array of arguments to update the post.
  5140. */
  5141. do_action( 'xmlrpc_call_success_mw_editPost', $post_ID, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  5142. return true;
  5143. }
  5144. /**
  5145. * Retrieve post.
  5146. *
  5147. * @since 1.5.0
  5148. *
  5149. * @param array $args {
  5150. * Method arguments. Note: arguments must be ordered as documented.
  5151. *
  5152. * @type int $blog_id (unused)
  5153. * @type int $post_ID
  5154. * @type string $username
  5155. * @type string $password
  5156. * }
  5157. * @return array|IXR_Error
  5158. */
  5159. public function mw_getPost( $args ) {
  5160. $this->escape( $args );
  5161. $post_ID = (int) $args[0];
  5162. $username = $args[1];
  5163. $password = $args[2];
  5164. $user = $this->login( $username, $password );
  5165. if ( ! $user ) {
  5166. return $this->error;
  5167. }
  5168. $postdata = get_post( $post_ID, ARRAY_A );
  5169. if ( ! $postdata ) {
  5170. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  5171. }
  5172. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  5173. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5174. }
  5175. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5176. do_action( 'xmlrpc_call', 'metaWeblog.getPost', $args, $this );
  5177. if ( '' !== $postdata['post_date'] ) {
  5178. $post_date = $this->_convert_date( $postdata['post_date'] );
  5179. $post_date_gmt = $this->_convert_date_gmt( $postdata['post_date_gmt'], $postdata['post_date'] );
  5180. $post_modified = $this->_convert_date( $postdata['post_modified'] );
  5181. $post_modified_gmt = $this->_convert_date_gmt( $postdata['post_modified_gmt'], $postdata['post_modified'] );
  5182. $categories = array();
  5183. $catids = wp_get_post_categories( $post_ID );
  5184. foreach ( $catids as $catid ) {
  5185. $categories[] = get_cat_name( $catid );
  5186. }
  5187. $tagnames = array();
  5188. $tags = wp_get_post_tags( $post_ID );
  5189. if ( ! empty( $tags ) ) {
  5190. foreach ( $tags as $tag ) {
  5191. $tagnames[] = $tag->name;
  5192. }
  5193. $tagnames = implode( ', ', $tagnames );
  5194. } else {
  5195. $tagnames = '';
  5196. }
  5197. $post = get_extended( $postdata['post_content'] );
  5198. $link = get_permalink( $postdata['ID'] );
  5199. // Get the author info.
  5200. $author = get_userdata( $postdata['post_author'] );
  5201. $allow_comments = ( 'open' === $postdata['comment_status'] ) ? 1 : 0;
  5202. $allow_pings = ( 'open' === $postdata['ping_status'] ) ? 1 : 0;
  5203. // Consider future posts as published.
  5204. if ( 'future' === $postdata['post_status'] ) {
  5205. $postdata['post_status'] = 'publish';
  5206. }
  5207. // Get post format.
  5208. $post_format = get_post_format( $post_ID );
  5209. if ( empty( $post_format ) ) {
  5210. $post_format = 'standard';
  5211. }
  5212. $sticky = false;
  5213. if ( is_sticky( $post_ID ) ) {
  5214. $sticky = true;
  5215. }
  5216. $enclosure = array();
  5217. foreach ( (array) get_post_custom( $post_ID ) as $key => $val ) {
  5218. if ( 'enclosure' === $key ) {
  5219. foreach ( (array) $val as $enc ) {
  5220. $encdata = explode( "\n", $enc );
  5221. $enclosure['url'] = trim( htmlspecialchars( $encdata[0] ) );
  5222. $enclosure['length'] = (int) trim( $encdata[1] );
  5223. $enclosure['type'] = trim( $encdata[2] );
  5224. break 2;
  5225. }
  5226. }
  5227. }
  5228. $resp = array(
  5229. 'dateCreated' => $post_date,
  5230. 'userid' => $postdata['post_author'],
  5231. 'postid' => $postdata['ID'],
  5232. 'description' => $post['main'],
  5233. 'title' => $postdata['post_title'],
  5234. 'link' => $link,
  5235. 'permaLink' => $link,
  5236. // Commented out because no other tool seems to use this.
  5237. // 'content' => $entry['post_content'],
  5238. 'categories' => $categories,
  5239. 'mt_excerpt' => $postdata['post_excerpt'],
  5240. 'mt_text_more' => $post['extended'],
  5241. 'wp_more_text' => $post['more_text'],
  5242. 'mt_allow_comments' => $allow_comments,
  5243. 'mt_allow_pings' => $allow_pings,
  5244. 'mt_keywords' => $tagnames,
  5245. 'wp_slug' => $postdata['post_name'],
  5246. 'wp_password' => $postdata['post_password'],
  5247. 'wp_author_id' => (string) $author->ID,
  5248. 'wp_author_display_name' => $author->display_name,
  5249. 'date_created_gmt' => $post_date_gmt,
  5250. 'post_status' => $postdata['post_status'],
  5251. 'custom_fields' => $this->get_custom_fields( $post_ID ),
  5252. 'wp_post_format' => $post_format,
  5253. 'sticky' => $sticky,
  5254. 'date_modified' => $post_modified,
  5255. 'date_modified_gmt' => $post_modified_gmt,
  5256. );
  5257. if ( ! empty( $enclosure ) ) {
  5258. $resp['enclosure'] = $enclosure;
  5259. }
  5260. $resp['wp_post_thumbnail'] = get_post_thumbnail_id( $postdata['ID'] );
  5261. return $resp;
  5262. } else {
  5263. return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
  5264. }
  5265. }
  5266. /**
  5267. * Retrieve list of recent posts.
  5268. *
  5269. * @since 1.5.0
  5270. *
  5271. * @param array $args {
  5272. * Method arguments. Note: arguments must be ordered as documented.
  5273. *
  5274. * @type int $blog_id (unused)
  5275. * @type string $username
  5276. * @type string $password
  5277. * @type int $numberposts
  5278. * }
  5279. * @return array|IXR_Error
  5280. */
  5281. public function mw_getRecentPosts( $args ) {
  5282. $this->escape( $args );
  5283. $username = $args[1];
  5284. $password = $args[2];
  5285. if ( isset( $args[3] ) ) {
  5286. $query = array( 'numberposts' => absint( $args[3] ) );
  5287. } else {
  5288. $query = array();
  5289. }
  5290. $user = $this->login( $username, $password );
  5291. if ( ! $user ) {
  5292. return $this->error;
  5293. }
  5294. if ( ! current_user_can( 'edit_posts' ) ) {
  5295. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit posts.' ) );
  5296. }
  5297. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5298. do_action( 'xmlrpc_call', 'metaWeblog.getRecentPosts', $args, $this );
  5299. $posts_list = wp_get_recent_posts( $query );
  5300. if ( ! $posts_list ) {
  5301. return array();
  5302. }
  5303. $recent_posts = array();
  5304. foreach ( $posts_list as $entry ) {
  5305. if ( ! current_user_can( 'edit_post', $entry['ID'] ) ) {
  5306. continue;
  5307. }
  5308. $post_date = $this->_convert_date( $entry['post_date'] );
  5309. $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
  5310. $post_modified = $this->_convert_date( $entry['post_modified'] );
  5311. $post_modified_gmt = $this->_convert_date_gmt( $entry['post_modified_gmt'], $entry['post_modified'] );
  5312. $categories = array();
  5313. $catids = wp_get_post_categories( $entry['ID'] );
  5314. foreach ( $catids as $catid ) {
  5315. $categories[] = get_cat_name( $catid );
  5316. }
  5317. $tagnames = array();
  5318. $tags = wp_get_post_tags( $entry['ID'] );
  5319. if ( ! empty( $tags ) ) {
  5320. foreach ( $tags as $tag ) {
  5321. $tagnames[] = $tag->name;
  5322. }
  5323. $tagnames = implode( ', ', $tagnames );
  5324. } else {
  5325. $tagnames = '';
  5326. }
  5327. $post = get_extended( $entry['post_content'] );
  5328. $link = get_permalink( $entry['ID'] );
  5329. // Get the post author info.
  5330. $author = get_userdata( $entry['post_author'] );
  5331. $allow_comments = ( 'open' === $entry['comment_status'] ) ? 1 : 0;
  5332. $allow_pings = ( 'open' === $entry['ping_status'] ) ? 1 : 0;
  5333. // Consider future posts as published.
  5334. if ( 'future' === $entry['post_status'] ) {
  5335. $entry['post_status'] = 'publish';
  5336. }
  5337. // Get post format.
  5338. $post_format = get_post_format( $entry['ID'] );
  5339. if ( empty( $post_format ) ) {
  5340. $post_format = 'standard';
  5341. }
  5342. $recent_posts[] = array(
  5343. 'dateCreated' => $post_date,
  5344. 'userid' => $entry['post_author'],
  5345. 'postid' => (string) $entry['ID'],
  5346. 'description' => $post['main'],
  5347. 'title' => $entry['post_title'],
  5348. 'link' => $link,
  5349. 'permaLink' => $link,
  5350. // Commented out because no other tool seems to use this.
  5351. // 'content' => $entry['post_content'],
  5352. 'categories' => $categories,
  5353. 'mt_excerpt' => $entry['post_excerpt'],
  5354. 'mt_text_more' => $post['extended'],
  5355. 'wp_more_text' => $post['more_text'],
  5356. 'mt_allow_comments' => $allow_comments,
  5357. 'mt_allow_pings' => $allow_pings,
  5358. 'mt_keywords' => $tagnames,
  5359. 'wp_slug' => $entry['post_name'],
  5360. 'wp_password' => $entry['post_password'],
  5361. 'wp_author_id' => (string) $author->ID,
  5362. 'wp_author_display_name' => $author->display_name,
  5363. 'date_created_gmt' => $post_date_gmt,
  5364. 'post_status' => $entry['post_status'],
  5365. 'custom_fields' => $this->get_custom_fields( $entry['ID'] ),
  5366. 'wp_post_format' => $post_format,
  5367. 'date_modified' => $post_modified,
  5368. 'date_modified_gmt' => $post_modified_gmt,
  5369. 'sticky' => ( 'post' === $entry['post_type'] && is_sticky( $entry['ID'] ) ),
  5370. 'wp_post_thumbnail' => get_post_thumbnail_id( $entry['ID'] ),
  5371. );
  5372. }
  5373. return $recent_posts;
  5374. }
  5375. /**
  5376. * Retrieve the list of categories on a given blog.
  5377. *
  5378. * @since 1.5.0
  5379. *
  5380. * @param array $args {
  5381. * Method arguments. Note: arguments must be ordered as documented.
  5382. *
  5383. * @type int $blog_id (unused)
  5384. * @type string $username
  5385. * @type string $password
  5386. * }
  5387. * @return array|IXR_Error
  5388. */
  5389. public function mw_getCategories( $args ) {
  5390. $this->escape( $args );
  5391. $username = $args[1];
  5392. $password = $args[2];
  5393. $user = $this->login( $username, $password );
  5394. if ( ! $user ) {
  5395. return $this->error;
  5396. }
  5397. if ( ! current_user_can( 'edit_posts' ) ) {
  5398. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  5399. }
  5400. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5401. do_action( 'xmlrpc_call', 'metaWeblog.getCategories', $args, $this );
  5402. $categories_struct = array();
  5403. $cats = get_categories( array( 'get' => 'all' ) );
  5404. if ( $cats ) {
  5405. foreach ( $cats as $cat ) {
  5406. $struct = array();
  5407. $struct['categoryId'] = $cat->term_id;
  5408. $struct['parentId'] = $cat->parent;
  5409. $struct['description'] = $cat->name;
  5410. $struct['categoryDescription'] = $cat->description;
  5411. $struct['categoryName'] = $cat->name;
  5412. $struct['htmlUrl'] = esc_html( get_category_link( $cat->term_id ) );
  5413. $struct['rssUrl'] = esc_html( get_category_feed_link( $cat->term_id, 'rss2' ) );
  5414. $categories_struct[] = $struct;
  5415. }
  5416. }
  5417. return $categories_struct;
  5418. }
  5419. /**
  5420. * Uploads a file, following your settings.
  5421. *
  5422. * Adapted from a patch by Johann Richard.
  5423. *
  5424. * @link http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
  5425. *
  5426. * @since 1.5.0
  5427. *
  5428. * @global wpdb $wpdb WordPress database abstraction object.
  5429. *
  5430. * @param array $args {
  5431. * Method arguments. Note: arguments must be ordered as documented.
  5432. *
  5433. * @type int $blog_id (unused)
  5434. * @type string $username
  5435. * @type string $password
  5436. * @type array $data
  5437. * }
  5438. * @return array|IXR_Error
  5439. */
  5440. public function mw_newMediaObject( $args ) {
  5441. global $wpdb;
  5442. $username = $this->escape( $args[1] );
  5443. $password = $this->escape( $args[2] );
  5444. $data = $args[3];
  5445. $name = sanitize_file_name( $data['name'] );
  5446. $type = $data['type'];
  5447. $bits = $data['bits'];
  5448. $user = $this->login( $username, $password );
  5449. if ( ! $user ) {
  5450. return $this->error;
  5451. }
  5452. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5453. do_action( 'xmlrpc_call', 'metaWeblog.newMediaObject', $args, $this );
  5454. if ( ! current_user_can( 'upload_files' ) ) {
  5455. $this->error = new IXR_Error( 401, __( 'Sorry, you are not allowed to upload files.' ) );
  5456. return $this->error;
  5457. }
  5458. if ( is_multisite() && upload_is_user_over_quota( false ) ) {
  5459. $this->error = new IXR_Error(
  5460. 401,
  5461. sprintf(
  5462. /* translators: %s: Allowed space allocation. */
  5463. __( 'Sorry, you have used your space allocation of %s. Please delete some files to upload more files.' ),
  5464. size_format( get_space_allowed() * MB_IN_BYTES )
  5465. )
  5466. );
  5467. return $this->error;
  5468. }
  5469. /**
  5470. * Filters whether to preempt the XML-RPC media upload.
  5471. *
  5472. * Passing a truthy value will effectively short-circuit the media upload,
  5473. * returning that value as a 500 error instead.
  5474. *
  5475. * @since 2.1.0
  5476. *
  5477. * @param bool $error Whether to pre-empt the media upload. Default false.
  5478. */
  5479. $upload_err = apply_filters( 'pre_upload_error', false );
  5480. if ( $upload_err ) {
  5481. return new IXR_Error( 500, $upload_err );
  5482. }
  5483. $upload = wp_upload_bits( $name, null, $bits );
  5484. if ( ! empty( $upload['error'] ) ) {
  5485. /* translators: 1: File name, 2: Error message. */
  5486. $errorString = sprintf( __( 'Could not write file %1$s (%2$s).' ), $name, $upload['error'] );
  5487. return new IXR_Error( 500, $errorString );
  5488. }
  5489. // Construct the attachment array.
  5490. $post_id = 0;
  5491. if ( ! empty( $data['post_id'] ) ) {
  5492. $post_id = (int) $data['post_id'];
  5493. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  5494. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5495. }
  5496. }
  5497. $attachment = array(
  5498. 'post_title' => $name,
  5499. 'post_content' => '',
  5500. 'post_type' => 'attachment',
  5501. 'post_parent' => $post_id,
  5502. 'post_mime_type' => $type,
  5503. 'guid' => $upload['url'],
  5504. );
  5505. // Save the data.
  5506. $id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
  5507. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
  5508. /**
  5509. * Fires after a new attachment has been added via the XML-RPC MovableType API.
  5510. *
  5511. * @since 3.4.0
  5512. *
  5513. * @param int $id ID of the new attachment.
  5514. * @param array $args An array of arguments to add the attachment.
  5515. */
  5516. do_action( 'xmlrpc_call_success_mw_newMediaObject', $id, $args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.NotLowercase
  5517. $struct = $this->_prepare_media_item( get_post( $id ) );
  5518. // Deprecated values.
  5519. $struct['id'] = $struct['attachment_id'];
  5520. $struct['file'] = $struct['title'];
  5521. $struct['url'] = $struct['link'];
  5522. return $struct;
  5523. }
  5524. /*
  5525. * MovableType API functions.
  5526. * Specs on http://www.movabletype.org/docs/mtmanual_programmatic.html
  5527. */
  5528. /**
  5529. * Retrieve the post titles of recent posts.
  5530. *
  5531. * @since 1.5.0
  5532. *
  5533. * @param array $args {
  5534. * Method arguments. Note: arguments must be ordered as documented.
  5535. *
  5536. * @type int $blog_id (unused)
  5537. * @type string $username
  5538. * @type string $password
  5539. * @type int $numberposts
  5540. * }
  5541. * @return array|IXR_Error
  5542. */
  5543. public function mt_getRecentPostTitles( $args ) {
  5544. $this->escape( $args );
  5545. $username = $args[1];
  5546. $password = $args[2];
  5547. if ( isset( $args[3] ) ) {
  5548. $query = array( 'numberposts' => absint( $args[3] ) );
  5549. } else {
  5550. $query = array();
  5551. }
  5552. $user = $this->login( $username, $password );
  5553. if ( ! $user ) {
  5554. return $this->error;
  5555. }
  5556. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5557. do_action( 'xmlrpc_call', 'mt.getRecentPostTitles', $args, $this );
  5558. $posts_list = wp_get_recent_posts( $query );
  5559. if ( ! $posts_list ) {
  5560. $this->error = new IXR_Error( 500, __( 'Either there are no posts, or something went wrong.' ) );
  5561. return $this->error;
  5562. }
  5563. $recent_posts = array();
  5564. foreach ( $posts_list as $entry ) {
  5565. if ( ! current_user_can( 'edit_post', $entry['ID'] ) ) {
  5566. continue;
  5567. }
  5568. $post_date = $this->_convert_date( $entry['post_date'] );
  5569. $post_date_gmt = $this->_convert_date_gmt( $entry['post_date_gmt'], $entry['post_date'] );
  5570. $recent_posts[] = array(
  5571. 'dateCreated' => $post_date,
  5572. 'userid' => $entry['post_author'],
  5573. 'postid' => (string) $entry['ID'],
  5574. 'title' => $entry['post_title'],
  5575. 'post_status' => $entry['post_status'],
  5576. 'date_created_gmt' => $post_date_gmt,
  5577. );
  5578. }
  5579. return $recent_posts;
  5580. }
  5581. /**
  5582. * Retrieve list of all categories on blog.
  5583. *
  5584. * @since 1.5.0
  5585. *
  5586. * @param array $args {
  5587. * Method arguments. Note: arguments must be ordered as documented.
  5588. *
  5589. * @type int $blog_id (unused)
  5590. * @type string $username
  5591. * @type string $password
  5592. * }
  5593. * @return array|IXR_Error
  5594. */
  5595. public function mt_getCategoryList( $args ) {
  5596. $this->escape( $args );
  5597. $username = $args[1];
  5598. $password = $args[2];
  5599. $user = $this->login( $username, $password );
  5600. if ( ! $user ) {
  5601. return $this->error;
  5602. }
  5603. if ( ! current_user_can( 'edit_posts' ) ) {
  5604. return new IXR_Error( 401, __( 'Sorry, you must be able to edit posts on this site in order to view categories.' ) );
  5605. }
  5606. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5607. do_action( 'xmlrpc_call', 'mt.getCategoryList', $args, $this );
  5608. $categories_struct = array();
  5609. $cats = get_categories(
  5610. array(
  5611. 'hide_empty' => 0,
  5612. 'hierarchical' => 0,
  5613. )
  5614. );
  5615. if ( $cats ) {
  5616. foreach ( $cats as $cat ) {
  5617. $struct = array();
  5618. $struct['categoryId'] = $cat->term_id;
  5619. $struct['categoryName'] = $cat->name;
  5620. $categories_struct[] = $struct;
  5621. }
  5622. }
  5623. return $categories_struct;
  5624. }
  5625. /**
  5626. * Retrieve post categories.
  5627. *
  5628. * @since 1.5.0
  5629. *
  5630. * @param array $args {
  5631. * Method arguments. Note: arguments must be ordered as documented.
  5632. *
  5633. * @type int $post_ID
  5634. * @type string $username
  5635. * @type string $password
  5636. * }
  5637. * @return array|IXR_Error
  5638. */
  5639. public function mt_getPostCategories( $args ) {
  5640. $this->escape( $args );
  5641. $post_ID = (int) $args[0];
  5642. $username = $args[1];
  5643. $password = $args[2];
  5644. $user = $this->login( $username, $password );
  5645. if ( ! $user ) {
  5646. return $this->error;
  5647. }
  5648. if ( ! get_post( $post_ID ) ) {
  5649. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  5650. }
  5651. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  5652. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5653. }
  5654. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5655. do_action( 'xmlrpc_call', 'mt.getPostCategories', $args, $this );
  5656. $categories = array();
  5657. $catids = wp_get_post_categories( (int) $post_ID );
  5658. // First listed category will be the primary category.
  5659. $isPrimary = true;
  5660. foreach ( $catids as $catid ) {
  5661. $categories[] = array(
  5662. 'categoryName' => get_cat_name( $catid ),
  5663. 'categoryId' => (string) $catid,
  5664. 'isPrimary' => $isPrimary,
  5665. );
  5666. $isPrimary = false;
  5667. }
  5668. return $categories;
  5669. }
  5670. /**
  5671. * Sets categories for a post.
  5672. *
  5673. * @since 1.5.0
  5674. *
  5675. * @param array $args {
  5676. * Method arguments. Note: arguments must be ordered as documented.
  5677. *
  5678. * @type int $post_ID
  5679. * @type string $username
  5680. * @type string $password
  5681. * @type array $categories
  5682. * }
  5683. * @return true|IXR_Error True on success.
  5684. */
  5685. public function mt_setPostCategories( $args ) {
  5686. $this->escape( $args );
  5687. $post_ID = (int) $args[0];
  5688. $username = $args[1];
  5689. $password = $args[2];
  5690. $categories = $args[3];
  5691. $user = $this->login( $username, $password );
  5692. if ( ! $user ) {
  5693. return $this->error;
  5694. }
  5695. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5696. do_action( 'xmlrpc_call', 'mt.setPostCategories', $args, $this );
  5697. if ( ! get_post( $post_ID ) ) {
  5698. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  5699. }
  5700. if ( ! current_user_can( 'edit_post', $post_ID ) ) {
  5701. return new IXR_Error( 401, __( 'Sorry, you are not allowed to edit this post.' ) );
  5702. }
  5703. $catids = array();
  5704. foreach ( $categories as $cat ) {
  5705. $catids[] = $cat['categoryId'];
  5706. }
  5707. wp_set_post_categories( $post_ID, $catids );
  5708. return true;
  5709. }
  5710. /**
  5711. * Retrieve an array of methods supported by this server.
  5712. *
  5713. * @since 1.5.0
  5714. *
  5715. * @return array
  5716. */
  5717. public function mt_supportedMethods() {
  5718. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5719. do_action( 'xmlrpc_call', 'mt.supportedMethods', array(), $this );
  5720. return array_keys( $this->methods );
  5721. }
  5722. /**
  5723. * Retrieve an empty array because we don't support per-post text filters.
  5724. *
  5725. * @since 1.5.0
  5726. */
  5727. public function mt_supportedTextFilters() {
  5728. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5729. do_action( 'xmlrpc_call', 'mt.supportedTextFilters', array(), $this );
  5730. /**
  5731. * Filters the MoveableType text filters list for XML-RPC.
  5732. *
  5733. * @since 2.2.0
  5734. *
  5735. * @param array $filters An array of text filters.
  5736. */
  5737. return apply_filters( 'xmlrpc_text_filters', array() );
  5738. }
  5739. /**
  5740. * Retrieve trackbacks sent to a given post.
  5741. *
  5742. * @since 1.5.0
  5743. *
  5744. * @global wpdb $wpdb WordPress database abstraction object.
  5745. *
  5746. * @param int $post_ID
  5747. * @return array|IXR_Error
  5748. */
  5749. public function mt_getTrackbackPings( $post_ID ) {
  5750. global $wpdb;
  5751. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5752. do_action( 'xmlrpc_call', 'mt.getTrackbackPings', $post_ID, $this );
  5753. $actual_post = get_post( $post_ID, ARRAY_A );
  5754. if ( ! $actual_post ) {
  5755. return new IXR_Error( 404, __( 'Sorry, no such post.' ) );
  5756. }
  5757. $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID ) );
  5758. if ( ! $comments ) {
  5759. return array();
  5760. }
  5761. $trackback_pings = array();
  5762. foreach ( $comments as $comment ) {
  5763. if ( 'trackback' === $comment->comment_type ) {
  5764. $content = $comment->comment_content;
  5765. $title = substr( $content, 8, ( strpos( $content, '</strong>' ) - 8 ) );
  5766. $trackback_pings[] = array(
  5767. 'pingTitle' => $title,
  5768. 'pingURL' => $comment->comment_author_url,
  5769. 'pingIP' => $comment->comment_author_IP,
  5770. );
  5771. }
  5772. }
  5773. return $trackback_pings;
  5774. }
  5775. /**
  5776. * Sets a post's publish status to 'publish'.
  5777. *
  5778. * @since 1.5.0
  5779. *
  5780. * @param array $args {
  5781. * Method arguments. Note: arguments must be ordered as documented.
  5782. *
  5783. * @type int $post_ID
  5784. * @type string $username
  5785. * @type string $password
  5786. * }
  5787. * @return int|IXR_Error
  5788. */
  5789. public function mt_publishPost( $args ) {
  5790. $this->escape( $args );
  5791. $post_ID = (int) $args[0];
  5792. $username = $args[1];
  5793. $password = $args[2];
  5794. $user = $this->login( $username, $password );
  5795. if ( ! $user ) {
  5796. return $this->error;
  5797. }
  5798. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5799. do_action( 'xmlrpc_call', 'mt.publishPost', $args, $this );
  5800. $postdata = get_post( $post_ID, ARRAY_A );
  5801. if ( ! $postdata ) {
  5802. return new IXR_Error( 404, __( 'Invalid post ID.' ) );
  5803. }
  5804. if ( ! current_user_can( 'publish_posts' ) || ! current_user_can( 'edit_post', $post_ID ) ) {
  5805. return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish this post.' ) );
  5806. }
  5807. $postdata['post_status'] = 'publish';
  5808. // Retain old categories.
  5809. $postdata['post_category'] = wp_get_post_categories( $post_ID );
  5810. $this->escape( $postdata );
  5811. return wp_update_post( $postdata );
  5812. }
  5813. /*
  5814. * Pingback functions.
  5815. * Specs on www.hixie.ch/specs/pingback/pingback
  5816. */
  5817. /**
  5818. * Retrieves a pingback and registers it.
  5819. *
  5820. * @since 1.5.0
  5821. *
  5822. * @param array $args {
  5823. * Method arguments. Note: arguments must be ordered as documented.
  5824. *
  5825. * @type string $pagelinkedfrom
  5826. * @type string $pagelinkedto
  5827. * }
  5828. * @return string|IXR_Error
  5829. */
  5830. public function pingback_ping( $args ) {
  5831. global $wpdb;
  5832. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  5833. do_action( 'xmlrpc_call', 'pingback.ping', $args, $this );
  5834. $this->escape( $args );
  5835. $pagelinkedfrom = str_replace( '&amp;', '&', $args[0] );
  5836. $pagelinkedto = str_replace( '&amp;', '&', $args[1] );
  5837. $pagelinkedto = str_replace( '&', '&amp;', $pagelinkedto );
  5838. /**
  5839. * Filters the pingback source URI.
  5840. *
  5841. * @since 3.6.0
  5842. *
  5843. * @param string $pagelinkedfrom URI of the page linked from.
  5844. * @param string $pagelinkedto URI of the page linked to.
  5845. */
  5846. $pagelinkedfrom = apply_filters( 'pingback_ping_source_uri', $pagelinkedfrom, $pagelinkedto );
  5847. if ( ! $pagelinkedfrom ) {
  5848. return $this->pingback_error( 0, __( 'A valid URL was not provided.' ) );
  5849. }
  5850. // Check if the page linked to is on our site.
  5851. $pos1 = strpos( $pagelinkedto, str_replace( array( 'http://www.', 'http://', 'https://www.', 'https://' ), '', get_option( 'home' ) ) );
  5852. if ( ! $pos1 ) {
  5853. return $this->pingback_error( 0, __( 'Is there no link to us?' ) );
  5854. }
  5855. /*
  5856. * Let's find which post is linked to.
  5857. * FIXME: Does url_to_postid() cover all these cases already?
  5858. * If so, then let's use it and drop the old code.
  5859. */
  5860. $urltest = parse_url( $pagelinkedto );
  5861. $post_ID = url_to_postid( $pagelinkedto );
  5862. if ( $post_ID ) {
  5863. // $way
  5864. } elseif ( isset( $urltest['path'] ) && preg_match( '#p/[0-9]{1,}#', $urltest['path'], $match ) ) {
  5865. // The path defines the post_ID (archives/p/XXXX).
  5866. $blah = explode( '/', $match[0] );
  5867. $post_ID = (int) $blah[1];
  5868. } elseif ( isset( $urltest['query'] ) && preg_match( '#p=[0-9]{1,}#', $urltest['query'], $match ) ) {
  5869. // The query string defines the post_ID (?p=XXXX).
  5870. $blah = explode( '=', $match[0] );
  5871. $post_ID = (int) $blah[1];
  5872. } elseif ( isset( $urltest['fragment'] ) ) {
  5873. // An #anchor is there, it's either...
  5874. if ( (int) $urltest['fragment'] ) {
  5875. // ...an integer #XXXX (simplest case),
  5876. $post_ID = (int) $urltest['fragment'];
  5877. } elseif ( preg_match( '/post-[0-9]+/', $urltest['fragment'] ) ) {
  5878. // ...a post ID in the form 'post-###',
  5879. $post_ID = preg_replace( '/[^0-9]+/', '', $urltest['fragment'] );
  5880. } elseif ( is_string( $urltest['fragment'] ) ) {
  5881. // ...or a string #title, a little more complicated.
  5882. $title = preg_replace( '/[^a-z0-9]/i', '.', $urltest['fragment'] );
  5883. $sql = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
  5884. $post_ID = $wpdb->get_var( $sql );
  5885. if ( ! $post_ID ) {
  5886. // Returning unknown error '0' is better than die()'ing.
  5887. return $this->pingback_error( 0, '' );
  5888. }
  5889. }
  5890. } else {
  5891. // TODO: Attempt to extract a post ID from the given URL.
  5892. return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
  5893. }
  5894. $post_ID = (int) $post_ID;
  5895. $post = get_post( $post_ID );
  5896. if ( ! $post ) { // Post not found.
  5897. return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
  5898. }
  5899. if ( url_to_postid( $pagelinkedfrom ) == $post_ID ) {
  5900. return $this->pingback_error( 0, __( 'The source URL and the target URL cannot both point to the same resource.' ) );
  5901. }
  5902. // Check if pings are on.
  5903. if ( ! pings_open( $post ) ) {
  5904. return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
  5905. }
  5906. // Let's check that the remote site didn't already pingback this entry.
  5907. if ( $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom ) ) ) {
  5908. return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
  5909. }
  5910. // Very stupid, but gives time to the 'from' server to publish!
  5911. sleep( 1 );
  5912. $remote_ip = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] );
  5913. /** This filter is documented in wp-includes/class-http.php */
  5914. $user_agent = apply_filters( 'http_headers_useragent', 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ), $pagelinkedfrom );
  5915. // Let's check the remote site.
  5916. $http_api_args = array(
  5917. 'timeout' => 10,
  5918. 'redirection' => 0,
  5919. 'limit_response_size' => 153600, // 150 KB
  5920. 'user-agent' => "$user_agent; verifying pingback from $remote_ip",
  5921. 'headers' => array(
  5922. 'X-Pingback-Forwarded-For' => $remote_ip,
  5923. ),
  5924. );
  5925. $request = wp_safe_remote_get( $pagelinkedfrom, $http_api_args );
  5926. $remote_source = wp_remote_retrieve_body( $request );
  5927. $remote_source_original = $remote_source;
  5928. if ( ! $remote_source ) {
  5929. return $this->pingback_error( 16, __( 'The source URL does not exist.' ) );
  5930. }
  5931. /**
  5932. * Filters the pingback remote source.
  5933. *
  5934. * @since 2.5.0
  5935. *
  5936. * @param string $remote_source Response source for the page linked from.
  5937. * @param string $pagelinkedto URL of the page linked to.
  5938. */
  5939. $remote_source = apply_filters( 'pre_remote_source', $remote_source, $pagelinkedto );
  5940. // Work around bug in strip_tags():
  5941. $remote_source = str_replace( '<!DOC', '<DOC', $remote_source );
  5942. $remote_source = preg_replace( '/[\r\n\t ]+/', ' ', $remote_source ); // normalize spaces
  5943. $remote_source = preg_replace( '/<\/*(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/', "\n\n", $remote_source );
  5944. preg_match( '|<title>([^<]*?)</title>|is', $remote_source, $matchtitle );
  5945. $title = isset( $matchtitle[1] ) ? $matchtitle[1] : '';
  5946. if ( empty( $title ) ) {
  5947. return $this->pingback_error( 32, __( 'We cannot find a title on that page.' ) );
  5948. }
  5949. // Remove all script and style tags including their content.
  5950. $remote_source = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $remote_source );
  5951. // Just keep the tag we need.
  5952. $remote_source = strip_tags( $remote_source, '<a>' );
  5953. $p = explode( "\n\n", $remote_source );
  5954. $preg_target = preg_quote( $pagelinkedto, '|' );
  5955. foreach ( $p as $para ) {
  5956. if ( strpos( $para, $pagelinkedto ) !== false ) { // It exists, but is it a link?
  5957. preg_match( '|<a[^>]+?' . $preg_target . '[^>]*>([^>]+?)</a>|', $para, $context );
  5958. // If the URL isn't in a link context, keep looking.
  5959. if ( empty( $context ) ) {
  5960. continue;
  5961. }
  5962. // We're going to use this fake tag to mark the context in a bit.
  5963. // The marker is needed in case the link text appears more than once in the paragraph.
  5964. $excerpt = preg_replace( '|\</?wpcontext\>|', '', $para );
  5965. // prevent really long link text
  5966. if ( strlen( $context[1] ) > 100 ) {
  5967. $context[1] = substr( $context[1], 0, 100 ) . '&#8230;';
  5968. }
  5969. $marker = '<wpcontext>' . $context[1] . '</wpcontext>'; // Set up our marker.
  5970. $excerpt = str_replace( $context[0], $marker, $excerpt ); // Swap out the link for our marker.
  5971. $excerpt = strip_tags( $excerpt, '<wpcontext>' ); // Strip all tags but our context marker.
  5972. $excerpt = trim( $excerpt );
  5973. $preg_marker = preg_quote( $marker, '|' );
  5974. $excerpt = preg_replace( "|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt );
  5975. $excerpt = strip_tags( $excerpt ); // YES, again, to remove the marker wrapper.
  5976. break;
  5977. }
  5978. }
  5979. if ( empty( $context ) ) { // Link to target not found.
  5980. return $this->pingback_error( 17, __( 'The source URL does not contain a link to the target URL, and so cannot be used as a source.' ) );
  5981. }
  5982. $pagelinkedfrom = str_replace( '&', '&amp;', $pagelinkedfrom );
  5983. $context = '[&#8230;] ' . esc_html( $excerpt ) . ' [&#8230;]';
  5984. $pagelinkedfrom = $this->escape( $pagelinkedfrom );
  5985. $comment_post_ID = (int) $post_ID;
  5986. $comment_author = $title;
  5987. $comment_author_email = '';
  5988. $this->escape( $comment_author );
  5989. $comment_author_url = $pagelinkedfrom;
  5990. $comment_content = $context;
  5991. $this->escape( $comment_content );
  5992. $comment_type = 'pingback';
  5993. $commentdata = compact(
  5994. 'comment_post_ID',
  5995. 'comment_author',
  5996. 'comment_author_url',
  5997. 'comment_author_email',
  5998. 'comment_content',
  5999. 'comment_type',
  6000. 'remote_source',
  6001. 'remote_source_original'
  6002. );
  6003. $comment_ID = wp_new_comment( $commentdata );
  6004. if ( is_wp_error( $comment_ID ) ) {
  6005. return $this->pingback_error( 0, $comment_ID->get_error_message() );
  6006. }
  6007. /**
  6008. * Fires after a post pingback has been sent.
  6009. *
  6010. * @since 0.71
  6011. *
  6012. * @param int $comment_ID Comment ID.
  6013. */
  6014. do_action( 'pingback_post', $comment_ID );
  6015. /* translators: 1: URL of the page linked from, 2: URL of the page linked to. */
  6016. return sprintf( __( 'Pingback from %1$s to %2$s registered. Keep the web talking! :-)' ), $pagelinkedfrom, $pagelinkedto );
  6017. }
  6018. /**
  6019. * Retrieve array of URLs that pingbacked the given URL.
  6020. *
  6021. * Specs on http://www.aquarionics.com/misc/archives/blogite/0198.html
  6022. *
  6023. * @since 1.5.0
  6024. *
  6025. * @global wpdb $wpdb WordPress database abstraction object.
  6026. *
  6027. * @param string $url
  6028. * @return array|IXR_Error
  6029. */
  6030. public function pingback_extensions_getPingbacks( $url ) {
  6031. global $wpdb;
  6032. /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
  6033. do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks', $url, $this );
  6034. $url = $this->escape( $url );
  6035. $post_ID = url_to_postid( $url );
  6036. if ( ! $post_ID ) {
  6037. // We aren't sure that the resource is available and/or pingback enabled.
  6038. return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
  6039. }
  6040. $actual_post = get_post( $post_ID, ARRAY_A );
  6041. if ( ! $actual_post ) {
  6042. // No such post = resource not found.
  6043. return $this->pingback_error( 32, __( 'The specified target URL does not exist.' ) );
  6044. }
  6045. $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID ) );
  6046. if ( ! $comments ) {
  6047. return array();
  6048. }
  6049. $pingbacks = array();
  6050. foreach ( $comments as $comment ) {
  6051. if ( 'pingback' === $comment->comment_type ) {
  6052. $pingbacks[] = $comment->comment_author_url;
  6053. }
  6054. }
  6055. return $pingbacks;
  6056. }
  6057. /**
  6058. * Sends a pingback error based on the given error code and message.
  6059. *
  6060. * @since 3.6.0
  6061. *
  6062. * @param int $code Error code.
  6063. * @param string $message Error message.
  6064. * @return IXR_Error Error object.
  6065. */
  6066. protected function pingback_error( $code, $message ) {
  6067. /**
  6068. * Filters the XML-RPC pingback error return.
  6069. *
  6070. * @since 3.5.1
  6071. *
  6072. * @param IXR_Error $error An IXR_Error object containing the error code and message.
  6073. */
  6074. return apply_filters( 'xmlrpc_pingback_error', new IXR_Error( $code, $message ) );
  6075. }
  6076. }