class-wp-xmlrpc-server.php 195 KB

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