class.jetpack.php 241 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393
  1. <?php
  2. /*
  3. Options:
  4. jetpack_options (array)
  5. An array of options.
  6. @see Jetpack_Options::get_option_names()
  7. jetpack_register (string)
  8. Temporary verification secrets.
  9. jetpack_activated (int)
  10. 1: the plugin was activated normally
  11. 2: the plugin was activated on this site because of a network-wide activation
  12. 3: the plugin was auto-installed
  13. 4: the plugin was manually disconnected (but is still installed)
  14. jetpack_active_modules (array)
  15. Array of active module slugs.
  16. jetpack_do_activate (bool)
  17. Flag for "activating" the plugin on sites where the activation hook never fired (auto-installs)
  18. */
  19. require_once( JETPACK__PLUGIN_DIR . '_inc/lib/class.media.php' );
  20. class Jetpack {
  21. public $xmlrpc_server = null;
  22. private $xmlrpc_verification = null;
  23. private $rest_authentication_status = null;
  24. public $HTTP_RAW_POST_DATA = null; // copy of $GLOBALS['HTTP_RAW_POST_DATA']
  25. /**
  26. * @var array The handles of styles that are concatenated into jetpack.css
  27. */
  28. public $concatenated_style_handles = array(
  29. 'jetpack-carousel',
  30. 'grunion.css',
  31. 'the-neverending-homepage',
  32. 'jetpack_likes',
  33. 'jetpack_related-posts',
  34. 'sharedaddy',
  35. 'jetpack-slideshow',
  36. 'presentations',
  37. 'jetpack-subscriptions',
  38. 'jetpack-responsive-videos-style',
  39. 'jetpack-social-menu',
  40. 'tiled-gallery',
  41. 'jetpack_display_posts_widget',
  42. 'gravatar-profile-widget',
  43. 'goodreads-widget',
  44. 'jetpack_social_media_icons_widget',
  45. 'jetpack-top-posts-widget',
  46. 'jetpack_image_widget',
  47. 'jetpack-my-community-widget',
  48. 'wordads',
  49. 'eu-cookie-law-style',
  50. 'flickr-widget-style',
  51. 'jetpack-search-widget',
  52. 'jetpack-simple-payments-widget-style',
  53. );
  54. /**
  55. * Contains all assets that have had their URL rewritten to minified versions.
  56. *
  57. * @var array
  58. */
  59. static $min_assets = array();
  60. public $plugins_to_deactivate = array(
  61. 'stats' => array( 'stats/stats.php', 'WordPress.com Stats' ),
  62. 'shortlinks' => array( 'stats/stats.php', 'WordPress.com Stats' ),
  63. 'sharedaddy' => array( 'sharedaddy/sharedaddy.php', 'Sharedaddy' ),
  64. 'twitter-widget' => array( 'wickett-twitter-widget/wickett-twitter-widget.php', 'Wickett Twitter Widget' ),
  65. 'after-the-deadline' => array( 'after-the-deadline/after-the-deadline.php', 'After The Deadline' ),
  66. 'contact-form' => array( 'grunion-contact-form/grunion-contact-form.php', 'Grunion Contact Form' ),
  67. 'contact-form' => array( 'mullet/mullet-contact-form.php', 'Mullet Contact Form' ),
  68. 'custom-css' => array( 'safecss/safecss.php', 'WordPress.com Custom CSS' ),
  69. 'random-redirect' => array( 'random-redirect/random-redirect.php', 'Random Redirect' ),
  70. 'videopress' => array( 'video/video.php', 'VideoPress' ),
  71. 'widget-visibility' => array( 'jetpack-widget-visibility/widget-visibility.php', 'Jetpack Widget Visibility' ),
  72. 'widget-visibility' => array( 'widget-visibility-without-jetpack/widget-visibility-without-jetpack.php', 'Widget Visibility Without Jetpack' ),
  73. 'sharedaddy' => array( 'jetpack-sharing/sharedaddy.php', 'Jetpack Sharing' ),
  74. 'gravatar-hovercards' => array( 'jetpack-gravatar-hovercards/gravatar-hovercards.php', 'Jetpack Gravatar Hovercards' ),
  75. 'latex' => array( 'wp-latex/wp-latex.php', 'WP LaTeX' )
  76. );
  77. static $capability_translations = array(
  78. 'administrator' => 'manage_options',
  79. 'editor' => 'edit_others_posts',
  80. 'author' => 'publish_posts',
  81. 'contributor' => 'edit_posts',
  82. 'subscriber' => 'read',
  83. );
  84. /**
  85. * Map of modules that have conflicts with plugins and should not be auto-activated
  86. * if the plugins are active. Used by filter_default_modules
  87. *
  88. * Plugin Authors: If you'd like to prevent a single module from auto-activating,
  89. * change `module-slug` and add this to your plugin:
  90. *
  91. * add_filter( 'jetpack_get_default_modules', 'my_jetpack_get_default_modules' );
  92. * function my_jetpack_get_default_modules( $modules ) {
  93. * return array_diff( $modules, array( 'module-slug' ) );
  94. * }
  95. *
  96. * @var array
  97. */
  98. private $conflicting_plugins = array(
  99. 'comments' => array(
  100. 'Intense Debate' => 'intensedebate/intensedebate.php',
  101. 'Disqus' => 'disqus-comment-system/disqus.php',
  102. 'Livefyre' => 'livefyre-comments/livefyre.php',
  103. 'Comments Evolved for WordPress' => 'gplus-comments/comments-evolved.php',
  104. 'Google+ Comments' => 'google-plus-comments/google-plus-comments.php',
  105. 'WP-SpamShield Anti-Spam' => 'wp-spamshield/wp-spamshield.php',
  106. ),
  107. 'comment-likes' => array(
  108. 'Epoch' => 'epoch/plugincore.php',
  109. ),
  110. 'contact-form' => array(
  111. 'Contact Form 7' => 'contact-form-7/wp-contact-form-7.php',
  112. 'Gravity Forms' => 'gravityforms/gravityforms.php',
  113. 'Contact Form Plugin' => 'contact-form-plugin/contact_form.php',
  114. 'Easy Contact Forms' => 'easy-contact-forms/easy-contact-forms.php',
  115. 'Fast Secure Contact Form' => 'si-contact-form/si-contact-form.php',
  116. 'Ninja Forms' => 'ninja-forms/ninja-forms.php',
  117. ),
  118. 'minileven' => array(
  119. 'WPtouch' => 'wptouch/wptouch.php',
  120. ),
  121. 'latex' => array(
  122. 'LaTeX for WordPress' => 'latex/latex.php',
  123. 'Youngwhans Simple Latex' => 'youngwhans-simple-latex/yw-latex.php',
  124. 'Easy WP LaTeX' => 'easy-wp-latex-lite/easy-wp-latex-lite.php',
  125. 'MathJax-LaTeX' => 'mathjax-latex/mathjax-latex.php',
  126. 'Enable Latex' => 'enable-latex/enable-latex.php',
  127. 'WP QuickLaTeX' => 'wp-quicklatex/wp-quicklatex.php',
  128. ),
  129. 'protect' => array(
  130. 'Limit Login Attempts' => 'limit-login-attempts/limit-login-attempts.php',
  131. 'Captcha' => 'captcha/captcha.php',
  132. 'Brute Force Login Protection' => 'brute-force-login-protection/brute-force-login-protection.php',
  133. 'Login Security Solution' => 'login-security-solution/login-security-solution.php',
  134. 'WPSecureOps Brute Force Protect' => 'wpsecureops-bruteforce-protect/wpsecureops-bruteforce-protect.php',
  135. 'BulletProof Security' => 'bulletproof-security/bulletproof-security.php',
  136. 'SiteGuard WP Plugin' => 'siteguard/siteguard.php',
  137. 'Security-protection' => 'security-protection/security-protection.php',
  138. 'Login Security' => 'login-security/login-security.php',
  139. 'Botnet Attack Blocker' => 'botnet-attack-blocker/botnet-attack-blocker.php',
  140. 'Wordfence Security' => 'wordfence/wordfence.php',
  141. 'All In One WP Security & Firewall' => 'all-in-one-wp-security-and-firewall/wp-security.php',
  142. 'iThemes Security' => 'better-wp-security/better-wp-security.php',
  143. ),
  144. 'random-redirect' => array(
  145. 'Random Redirect 2' => 'random-redirect-2/random-redirect.php',
  146. ),
  147. 'related-posts' => array(
  148. 'YARPP' => 'yet-another-related-posts-plugin/yarpp.php',
  149. 'WordPress Related Posts' => 'wordpress-23-related-posts-plugin/wp_related_posts.php',
  150. 'nrelate Related Content' => 'nrelate-related-content/nrelate-related.php',
  151. 'Contextual Related Posts' => 'contextual-related-posts/contextual-related-posts.php',
  152. 'Related Posts for WordPress' => 'microkids-related-posts/microkids-related-posts.php',
  153. 'outbrain' => 'outbrain/outbrain.php',
  154. 'Shareaholic' => 'shareaholic/shareaholic.php',
  155. 'Sexybookmarks' => 'sexybookmarks/shareaholic.php',
  156. ),
  157. 'sharedaddy' => array(
  158. 'AddThis' => 'addthis/addthis_social_widget.php',
  159. 'Add To Any' => 'add-to-any/add-to-any.php',
  160. 'ShareThis' => 'share-this/sharethis.php',
  161. 'Shareaholic' => 'shareaholic/shareaholic.php',
  162. ),
  163. 'seo-tools' => array(
  164. 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
  165. 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
  166. 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
  167. 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
  168. 'The SEO Framework' => 'autodescription/autodescription.php',
  169. ),
  170. 'verification-tools' => array(
  171. 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
  172. 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
  173. 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
  174. 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
  175. 'The SEO Framework' => 'autodescription/autodescription.php',
  176. ),
  177. 'widget-visibility' => array(
  178. 'Widget Logic' => 'widget-logic/widget_logic.php',
  179. 'Dynamic Widgets' => 'dynamic-widgets/dynamic-widgets.php',
  180. ),
  181. 'sitemaps' => array(
  182. 'Google XML Sitemaps' => 'google-sitemap-generator/sitemap.php',
  183. 'Better WordPress Google XML Sitemaps' => 'bwp-google-xml-sitemaps/bwp-simple-gxs.php',
  184. 'Google XML Sitemaps for qTranslate' => 'google-xml-sitemaps-v3-for-qtranslate/sitemap.php',
  185. 'XML Sitemap & Google News feeds' => 'xml-sitemap-feed/xml-sitemap.php',
  186. 'Google Sitemap by BestWebSoft' => 'google-sitemap-plugin/google-sitemap-plugin.php',
  187. 'WordPress SEO by Yoast' => 'wordpress-seo/wp-seo.php',
  188. 'WordPress SEO Premium by Yoast' => 'wordpress-seo-premium/wp-seo-premium.php',
  189. 'All in One SEO Pack' => 'all-in-one-seo-pack/all_in_one_seo_pack.php',
  190. 'All in One SEO Pack Pro' => 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php',
  191. 'The SEO Framework' => 'autodescription/autodescription.php',
  192. 'Sitemap' => 'sitemap/sitemap.php',
  193. 'Simple Wp Sitemap' => 'simple-wp-sitemap/simple-wp-sitemap.php',
  194. 'Simple Sitemap' => 'simple-sitemap/simple-sitemap.php',
  195. 'XML Sitemaps' => 'xml-sitemaps/xml-sitemaps.php',
  196. 'MSM Sitemaps' => 'msm-sitemap/msm-sitemap.php',
  197. ),
  198. 'lazy-images' => array(
  199. 'Lazy Load' => 'lazy-load/lazy-load.php',
  200. 'BJ Lazy Load' => 'bj-lazy-load/bj-lazy-load.php',
  201. 'Lazy Load by WP Rocket' => 'rocket-lazy-load/rocket-lazy-load.php',
  202. ),
  203. );
  204. /**
  205. * Plugins for which we turn off our Facebook OG Tags implementation.
  206. *
  207. * Note: All in One SEO Pack, All in one SEO Pack Pro, WordPress SEO by Yoast, and WordPress SEO Premium by Yoast automatically deactivate
  208. * Jetpack's Open Graph tags via filter when their Social Meta modules are active.
  209. *
  210. * Plugin authors: If you'd like to prevent Jetpack's Open Graph tag generation in your plugin, you can do so via this filter:
  211. * add_filter( 'jetpack_enable_open_graph', '__return_false' );
  212. */
  213. private $open_graph_conflicting_plugins = array(
  214. '2-click-socialmedia-buttons/2-click-socialmedia-buttons.php',
  215. // 2 Click Social Media Buttons
  216. 'add-link-to-facebook/add-link-to-facebook.php', // Add Link to Facebook
  217. 'add-meta-tags/add-meta-tags.php', // Add Meta Tags
  218. 'easy-facebook-share-thumbnails/esft.php', // Easy Facebook Share Thumbnail
  219. 'heateor-open-graph-meta-tags/heateor-open-graph-meta-tags.php',
  220. // Open Graph Meta Tags by Heateor
  221. 'facebook/facebook.php', // Facebook (official plugin)
  222. 'facebook-awd/AWD_facebook.php', // Facebook AWD All in one
  223. 'facebook-featured-image-and-open-graph-meta-tags/fb-featured-image.php',
  224. // Facebook Featured Image & OG Meta Tags
  225. 'facebook-meta-tags/facebook-metatags.php', // Facebook Meta Tags
  226. 'wonderm00ns-simple-facebook-open-graph-tags/wonderm00n-open-graph.php',
  227. // Facebook Open Graph Meta Tags for WordPress
  228. 'facebook-revised-open-graph-meta-tag/index.php', // Facebook Revised Open Graph Meta Tag
  229. 'facebook-thumb-fixer/_facebook-thumb-fixer.php', // Facebook Thumb Fixer
  230. 'facebook-and-digg-thumbnail-generator/facebook-and-digg-thumbnail-generator.php',
  231. // Fedmich's Facebook Open Graph Meta
  232. 'network-publisher/networkpub.php', // Network Publisher
  233. 'nextgen-facebook/nextgen-facebook.php', // NextGEN Facebook OG
  234. 'social-networks-auto-poster-facebook-twitter-g/NextScripts_SNAP.php',
  235. // NextScripts SNAP
  236. 'og-tags/og-tags.php', // OG Tags
  237. 'opengraph/opengraph.php', // Open Graph
  238. 'open-graph-protocol-framework/open-graph-protocol-framework.php',
  239. // Open Graph Protocol Framework
  240. 'seo-facebook-comments/seofacebook.php', // SEO Facebook Comments
  241. 'seo-ultimate/seo-ultimate.php', // SEO Ultimate
  242. 'sexybookmarks/sexy-bookmarks.php', // Shareaholic
  243. 'shareaholic/sexy-bookmarks.php', // Shareaholic
  244. 'sharepress/sharepress.php', // SharePress
  245. 'simple-facebook-connect/sfc.php', // Simple Facebook Connect
  246. 'social-discussions/social-discussions.php', // Social Discussions
  247. 'social-sharing-toolkit/social_sharing_toolkit.php', // Social Sharing Toolkit
  248. 'socialize/socialize.php', // Socialize
  249. 'squirrly-seo/squirrly.php', // SEO by SQUIRRLY™
  250. 'only-tweet-like-share-and-google-1/tweet-like-plusone.php',
  251. // Tweet, Like, Google +1 and Share
  252. 'wordbooker/wordbooker.php', // Wordbooker
  253. 'wpsso/wpsso.php', // WordPress Social Sharing Optimization
  254. 'wp-caregiver/wp-caregiver.php', // WP Caregiver
  255. 'wp-facebook-like-send-open-graph-meta/wp-facebook-like-send-open-graph-meta.php',
  256. // WP Facebook Like Send & Open Graph Meta
  257. 'wp-facebook-open-graph-protocol/wp-facebook-ogp.php', // WP Facebook Open Graph protocol
  258. 'wp-ogp/wp-ogp.php', // WP-OGP
  259. 'zoltonorg-social-plugin/zosp.php', // Zolton.org Social Plugin
  260. 'wp-fb-share-like-button/wp_fb_share-like_widget.php', // WP Facebook Like Button
  261. 'open-graph-metabox/open-graph-metabox.php' // Open Graph Metabox
  262. );
  263. /**
  264. * Plugins for which we turn off our Twitter Cards Tags implementation.
  265. */
  266. private $twitter_cards_conflicting_plugins = array(
  267. // 'twitter/twitter.php', // The official one handles this on its own.
  268. // // https://github.com/twitter/wordpress/blob/master/src/Twitter/WordPress/Cards/Compatibility.php
  269. 'eewee-twitter-card/index.php', // Eewee Twitter Card
  270. 'ig-twitter-cards/ig-twitter-cards.php', // IG:Twitter Cards
  271. 'jm-twitter-cards/jm-twitter-cards.php', // JM Twitter Cards
  272. 'kevinjohn-gallagher-pure-web-brilliants-social-graph-twitter-cards-extention/kevinjohn_gallagher___social_graph_twitter_output.php',
  273. // Pure Web Brilliant's Social Graph Twitter Cards Extension
  274. 'twitter-cards/twitter-cards.php', // Twitter Cards
  275. 'twitter-cards-meta/twitter-cards-meta.php', // Twitter Cards Meta
  276. 'wp-twitter-cards/twitter_cards.php', // WP Twitter Cards
  277. );
  278. /**
  279. * Message to display in admin_notice
  280. * @var string
  281. */
  282. public $message = '';
  283. /**
  284. * Error to display in admin_notice
  285. * @var string
  286. */
  287. public $error = '';
  288. /**
  289. * Modules that need more privacy description.
  290. * @var string
  291. */
  292. public $privacy_checks = '';
  293. /**
  294. * Stats to record once the page loads
  295. *
  296. * @var array
  297. */
  298. public $stats = array();
  299. /**
  300. * Jetpack_Sync object
  301. */
  302. public $sync;
  303. /**
  304. * Verified data for JSON authorization request
  305. */
  306. public $json_api_authorization_request = array();
  307. /**
  308. * @var string Transient key used to prevent multiple simultaneous plugin upgrades
  309. */
  310. public static $plugin_upgrade_lock_key = 'jetpack_upgrade_lock';
  311. /**
  312. * Holds the singleton instance of this class
  313. * @since 2.3.3
  314. * @var Jetpack
  315. */
  316. static $instance = false;
  317. /**
  318. * Singleton
  319. * @static
  320. */
  321. public static function init() {
  322. if ( ! self::$instance ) {
  323. self::$instance = new Jetpack;
  324. self::$instance->plugin_upgrade();
  325. }
  326. return self::$instance;
  327. }
  328. /**
  329. * Must never be called statically
  330. */
  331. function plugin_upgrade() {
  332. if ( Jetpack::is_active() ) {
  333. list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
  334. if ( JETPACK__VERSION != $version ) {
  335. // Prevent multiple upgrades at once - only a single process should trigger
  336. // an upgrade to avoid stampedes
  337. if ( false !== get_transient( self::$plugin_upgrade_lock_key ) ) {
  338. return;
  339. }
  340. // Set a short lock to prevent multiple instances of the upgrade
  341. set_transient( self::$plugin_upgrade_lock_key, 1, 10 );
  342. // check which active modules actually exist and remove others from active_modules list
  343. $unfiltered_modules = Jetpack::get_active_modules();
  344. $modules = array_filter( $unfiltered_modules, array( 'Jetpack', 'is_module' ) );
  345. if ( array_diff( $unfiltered_modules, $modules ) ) {
  346. Jetpack::update_active_modules( $modules );
  347. }
  348. add_action( 'init', array( __CLASS__, 'activate_new_modules' ) );
  349. // Upgrade to 4.3.0
  350. if ( Jetpack_Options::get_option( 'identity_crisis_whitelist' ) ) {
  351. Jetpack_Options::delete_option( 'identity_crisis_whitelist' );
  352. }
  353. // Make sure Markdown for posts gets turned back on
  354. if ( ! get_option( 'wpcom_publish_posts_with_markdown' ) ) {
  355. update_option( 'wpcom_publish_posts_with_markdown', true );
  356. }
  357. if ( did_action( 'wp_loaded' ) ) {
  358. self::upgrade_on_load();
  359. } else {
  360. add_action(
  361. 'wp_loaded',
  362. array( __CLASS__, 'upgrade_on_load' )
  363. );
  364. }
  365. }
  366. }
  367. }
  368. /**
  369. * Runs upgrade routines that need to have modules loaded.
  370. */
  371. static function upgrade_on_load() {
  372. // Not attempting any upgrades if jetpack_modules_loaded did not fire.
  373. // This can happen in case Jetpack has been just upgraded and is
  374. // being initialized late during the page load. In this case we wait
  375. // until the next proper admin page load with Jetpack active.
  376. if ( ! did_action( 'jetpack_modules_loaded' ) ) {
  377. delete_transient( self::$plugin_upgrade_lock_key );
  378. return;
  379. }
  380. Jetpack::maybe_set_version_option();
  381. if ( class_exists( 'Jetpack_Widget_Conditions' ) ) {
  382. Jetpack_Widget_Conditions::migrate_post_type_rules();
  383. }
  384. if (
  385. class_exists( 'Jetpack_Sitemap_Manager' )
  386. && version_compare( JETPACK__VERSION, '5.3', '>=' )
  387. ) {
  388. do_action( 'jetpack_sitemaps_purge_data' );
  389. }
  390. delete_transient( self::$plugin_upgrade_lock_key );
  391. }
  392. static function activate_manage( ) {
  393. if ( did_action( 'init' ) || current_filter() == 'init' ) {
  394. self::activate_module( 'manage', false, false );
  395. } else if ( ! has_action( 'init' , array( __CLASS__, 'activate_manage' ) ) ) {
  396. add_action( 'init', array( __CLASS__, 'activate_manage' ) );
  397. }
  398. }
  399. static function update_active_modules( $modules ) {
  400. $current_modules = Jetpack_Options::get_option( 'active_modules', array() );
  401. $success = Jetpack_Options::update_option( 'active_modules', array_unique( $modules ) );
  402. if ( is_array( $modules ) && is_array( $current_modules ) ) {
  403. $new_active_modules = array_diff( $modules, $current_modules );
  404. foreach( $new_active_modules as $module ) {
  405. /**
  406. * Fires when a specific module is activated.
  407. *
  408. * @since 1.9.0
  409. *
  410. * @param string $module Module slug.
  411. * @param boolean $success whether the module was activated. @since 4.2
  412. */
  413. do_action( 'jetpack_activate_module', $module, $success );
  414. /**
  415. * Fires when a module is activated.
  416. * The dynamic part of the filter, $module, is the module slug.
  417. *
  418. * @since 1.9.0
  419. *
  420. * @param string $module Module slug.
  421. */
  422. do_action( "jetpack_activate_module_$module", $module );
  423. }
  424. $new_deactive_modules = array_diff( $current_modules, $modules );
  425. foreach( $new_deactive_modules as $module ) {
  426. /**
  427. * Fired after a module has been deactivated.
  428. *
  429. * @since 4.2.0
  430. *
  431. * @param string $module Module slug.
  432. * @param boolean $success whether the module was deactivated.
  433. */
  434. do_action( 'jetpack_deactivate_module', $module, $success );
  435. /**
  436. * Fires when a module is deactivated.
  437. * The dynamic part of the filter, $module, is the module slug.
  438. *
  439. * @since 1.9.0
  440. *
  441. * @param string $module Module slug.
  442. */
  443. do_action( "jetpack_deactivate_module_$module", $module );
  444. }
  445. }
  446. return $success;
  447. }
  448. static function delete_active_modules() {
  449. self::update_active_modules( array() );
  450. }
  451. /**
  452. * Constructor. Initializes WordPress hooks
  453. */
  454. private function __construct() {
  455. /*
  456. * Check for and alert any deprecated hooks
  457. */
  458. add_action( 'init', array( $this, 'deprecated_hooks' ) );
  459. /*
  460. * Enable enhanced handling of previewing sites in Calypso
  461. */
  462. if ( Jetpack::is_active() ) {
  463. require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-iframe-embed.php';
  464. add_action( 'init', array( 'Jetpack_Iframe_Embed', 'init' ), 9, 0 );
  465. }
  466. /*
  467. * Load things that should only be in Network Admin.
  468. *
  469. * For now blow away everything else until a more full
  470. * understanding of what is needed at the network level is
  471. * available
  472. */
  473. if( is_multisite() ) {
  474. Jetpack_Network::init();
  475. }
  476. // Load Gutenberg editor blocks
  477. add_action( 'init', array( $this, 'load_jetpack_gutenberg' ) );
  478. add_action( 'set_user_role', array( $this, 'maybe_clear_other_linked_admins_transient' ), 10, 3 );
  479. // Unlink user before deleting the user from .com
  480. add_action( 'deleted_user', array( $this, 'unlink_user' ), 10, 1 );
  481. add_action( 'remove_user_from_blog', array( $this, 'unlink_user' ), 10, 1 );
  482. if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST && isset( $_GET['for'] ) && 'jetpack' == $_GET['for'] ) {
  483. @ini_set( 'display_errors', false ); // Display errors can cause the XML to be not well formed.
  484. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-xmlrpc-server.php';
  485. $this->xmlrpc_server = new Jetpack_XMLRPC_Server();
  486. $this->require_jetpack_authentication();
  487. if ( Jetpack::is_active() ) {
  488. // Hack to preserve $HTTP_RAW_POST_DATA
  489. add_filter( 'xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
  490. $signed = $this->verify_xml_rpc_signature();
  491. if ( $signed && ! is_wp_error( $signed ) ) {
  492. // The actual API methods.
  493. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'xmlrpc_methods' ) );
  494. } else {
  495. // The jetpack.authorize method should be available for unauthenticated users on a site with an
  496. // active Jetpack connection, so that additional users can link their account.
  497. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'authorize_xmlrpc_methods' ) );
  498. }
  499. } else {
  500. // The bootstrap API methods.
  501. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'bootstrap_xmlrpc_methods' ) );
  502. $signed = $this->verify_xml_rpc_signature();
  503. if ( $signed && ! is_wp_error( $signed ) ) {
  504. // the jetpack Provision method is available for blog-token-signed requests
  505. add_filter( 'xmlrpc_methods', array( $this->xmlrpc_server, 'provision_xmlrpc_methods' ) );
  506. }
  507. }
  508. // Now that no one can authenticate, and we're whitelisting all XML-RPC methods, force enable_xmlrpc on.
  509. add_filter( 'pre_option_enable_xmlrpc', '__return_true' );
  510. } elseif (
  511. is_admin() &&
  512. isset( $_POST['action'] ) && (
  513. 'jetpack_upload_file' == $_POST['action'] ||
  514. 'jetpack_update_file' == $_POST['action']
  515. )
  516. ) {
  517. $this->require_jetpack_authentication();
  518. $this->add_remote_request_handlers();
  519. } else {
  520. if ( Jetpack::is_active() ) {
  521. add_action( 'login_form_jetpack_json_api_authorization', array( &$this, 'login_form_json_api_authorization' ) );
  522. add_filter( 'xmlrpc_methods', array( $this, 'public_xmlrpc_methods' ) );
  523. }
  524. }
  525. if ( Jetpack::is_active() ) {
  526. Jetpack_Heartbeat::init();
  527. if ( Jetpack::is_module_active( 'stats' ) && Jetpack::is_module_active( 'search' ) ) {
  528. require_once JETPACK__PLUGIN_DIR . '_inc/lib/class.jetpack-search-performance-logger.php';
  529. Jetpack_Search_Performance_Logger::init();
  530. }
  531. }
  532. add_filter( 'determine_current_user', array( $this, 'wp_rest_authenticate' ) );
  533. add_filter( 'rest_authentication_errors', array( $this, 'wp_rest_authentication_errors' ) );
  534. add_action( 'jetpack_clean_nonces', array( 'Jetpack', 'clean_nonces' ) );
  535. if ( ! wp_next_scheduled( 'jetpack_clean_nonces' ) ) {
  536. wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
  537. }
  538. add_filter( 'xmlrpc_blog_options', array( $this, 'xmlrpc_options' ) );
  539. add_action( 'admin_init', array( $this, 'admin_init' ) );
  540. add_action( 'admin_init', array( $this, 'dismiss_jetpack_notice' ) );
  541. add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
  542. add_action( 'wp_dashboard_setup', array( $this, 'wp_dashboard_setup' ) );
  543. // Filter the dashboard meta box order to swap the new one in in place of the old one.
  544. add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'get_user_option_meta_box_order_dashboard' ) );
  545. // returns HTTPS support status
  546. add_action( 'wp_ajax_jetpack-recheck-ssl', array( $this, 'ajax_recheck_ssl' ) );
  547. // If any module option is updated before Jump Start is dismissed, hide Jump Start.
  548. add_action( 'update_option', array( $this, 'jumpstart_has_updated_module_option' ) );
  549. // JITM AJAX callback function
  550. add_action( 'wp_ajax_jitm_ajax', array( $this, 'jetpack_jitm_ajax_callback' ) );
  551. // Universal ajax callback for all tracking events triggered via js
  552. add_action( 'wp_ajax_jetpack_tracks', array( $this, 'jetpack_admin_ajax_tracks_callback' ) );
  553. add_action( 'wp_ajax_jetpack_connection_banner', array( $this, 'jetpack_connection_banner_callback' ) );
  554. add_action( 'wp_loaded', array( $this, 'register_assets' ) );
  555. add_action( 'wp_enqueue_scripts', array( $this, 'devicepx' ) );
  556. add_action( 'customize_controls_enqueue_scripts', array( $this, 'devicepx' ) );
  557. add_action( 'admin_enqueue_scripts', array( $this, 'devicepx' ) );
  558. // gutenberg locale
  559. add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_locale' ) );
  560. add_action( 'plugins_loaded', array( $this, 'extra_oembed_providers' ), 100 );
  561. /**
  562. * These actions run checks to load additional files.
  563. * They check for external files or plugins, so they need to run as late as possible.
  564. */
  565. add_action( 'wp_head', array( $this, 'check_open_graph' ), 1 );
  566. add_action( 'plugins_loaded', array( $this, 'check_twitter_tags' ), 999 );
  567. add_action( 'plugins_loaded', array( $this, 'check_rest_api_compat' ), 1000 );
  568. add_filter( 'plugins_url', array( 'Jetpack', 'maybe_min_asset' ), 1, 3 );
  569. add_action( 'style_loader_src', array( 'Jetpack', 'set_suffix_on_min' ), 10, 2 );
  570. add_filter( 'style_loader_tag', array( 'Jetpack', 'maybe_inline_style' ), 10, 2 );
  571. add_filter( 'map_meta_cap', array( $this, 'jetpack_custom_caps' ), 1, 4 );
  572. add_filter( 'jetpack_get_default_modules', array( $this, 'filter_default_modules' ) );
  573. add_filter( 'jetpack_get_default_modules', array( $this, 'handle_deprecated_modules' ), 99 );
  574. // A filter to control all just in time messages
  575. add_filter( 'jetpack_just_in_time_msgs', '__return_true', 9 );
  576. add_filter( 'jetpack_just_in_time_msg_cache', '__return_true', 9);
  577. // If enabled, point edit post, page, and comment links to Calypso instead of WP-Admin.
  578. // We should make sure to only do this for front end links.
  579. if ( Jetpack::get_option( 'edit_links_calypso_redirect' ) && ! is_admin() ) {
  580. add_filter( 'get_edit_post_link', array( $this, 'point_edit_post_links_to_calypso' ), 1, 2 );
  581. add_filter( 'get_edit_comment_link', array( $this, 'point_edit_comment_links_to_calypso' ), 1 );
  582. //we'll override wp_notify_postauthor and wp_notify_moderator pluggable functions
  583. //so they point moderation links on emails to Calypso
  584. jetpack_require_lib( 'functions.wp-notify' );
  585. }
  586. // Update the Jetpack plan from API on heartbeats
  587. add_action( 'jetpack_heartbeat', array( $this, 'refresh_active_plan_from_wpcom' ) );
  588. /**
  589. * This is the hack to concatenate all css files into one.
  590. * For description and reasoning see the implode_frontend_css method
  591. *
  592. * Super late priority so we catch all the registered styles
  593. */
  594. if( !is_admin() ) {
  595. add_action( 'wp_print_styles', array( $this, 'implode_frontend_css' ), -1 ); // Run first
  596. add_action( 'wp_print_footer_scripts', array( $this, 'implode_frontend_css' ), -1 ); // Run first to trigger before `print_late_styles`
  597. }
  598. /**
  599. * These are sync actions that we need to keep track of for jitms
  600. */
  601. add_filter( 'jetpack_sync_before_send_updated_option', array( $this, 'jetpack_track_last_sync_callback' ), 99 );
  602. // Actually push the stats on shutdown.
  603. if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
  604. add_action( 'shutdown', array( $this, 'push_stats' ) );
  605. }
  606. }
  607. function point_edit_post_links_to_calypso( $default_url, $post_id ) {
  608. $post = get_post( $post_id );
  609. if ( empty( $post ) ) {
  610. return $default_url;
  611. }
  612. $post_type = $post->post_type;
  613. // Mapping the allowed CPTs on WordPress.com to corresponding paths in Calypso.
  614. // https://en.support.wordpress.com/custom-post-types/
  615. $allowed_post_types = array(
  616. 'post' => 'post',
  617. 'page' => 'page',
  618. 'jetpack-portfolio' => 'edit/jetpack-portfolio',
  619. 'jetpack-testimonial' => 'edit/jetpack-testimonial',
  620. );
  621. if ( ! in_array( $post_type, array_keys( $allowed_post_types ) ) ) {
  622. return $default_url;
  623. }
  624. $path_prefix = $allowed_post_types[ $post_type ];
  625. $site_slug = Jetpack::build_raw_urls( get_home_url() );
  626. return esc_url( sprintf( 'https://wordpress.com/%s/%s/%d', $path_prefix, $site_slug, $post_id ) );
  627. }
  628. function point_edit_comment_links_to_calypso( $url ) {
  629. // Take the `query` key value from the URL, and parse its parts to the $query_args. `amp;c` matches the comment ID.
  630. wp_parse_str( wp_parse_url( $url, PHP_URL_QUERY ), $query_args );
  631. return esc_url( sprintf( 'https://wordpress.com/comment/%s/%d',
  632. Jetpack::build_raw_urls( get_home_url() ),
  633. $query_args['amp;c']
  634. ) );
  635. }
  636. function jetpack_track_last_sync_callback( $params ) {
  637. /**
  638. * Filter to turn off jitm caching
  639. *
  640. * @since 5.4.0
  641. *
  642. * @param bool false Whether to cache just in time messages
  643. */
  644. if ( ! apply_filters( 'jetpack_just_in_time_msg_cache', false ) ) {
  645. return $params;
  646. }
  647. if ( is_array( $params ) && isset( $params[0] ) ) {
  648. $option = $params[0];
  649. if ( 'active_plugins' === $option ) {
  650. // use the cache if we can, but not terribly important if it gets evicted
  651. set_transient( 'jetpack_last_plugin_sync', time(), HOUR_IN_SECONDS );
  652. }
  653. }
  654. return $params;
  655. }
  656. function jetpack_connection_banner_callback() {
  657. check_ajax_referer( 'jp-connection-banner-nonce', 'nonce' );
  658. if ( isset( $_REQUEST['dismissBanner'] ) ) {
  659. Jetpack_Options::update_option( 'dismissed_connection_banner', 1 );
  660. wp_send_json_success();
  661. }
  662. wp_die();
  663. }
  664. function jetpack_admin_ajax_tracks_callback() {
  665. // Check for nonce
  666. if ( ! isset( $_REQUEST['tracksNonce'] ) || ! wp_verify_nonce( $_REQUEST['tracksNonce'], 'jp-tracks-ajax-nonce' ) ) {
  667. wp_die( 'Permissions check failed.' );
  668. }
  669. if ( ! isset( $_REQUEST['tracksEventName'] ) || ! isset( $_REQUEST['tracksEventType'] ) ) {
  670. wp_die( 'No valid event name or type.' );
  671. }
  672. $tracks_data = array();
  673. if ( 'click' === $_REQUEST['tracksEventType'] && isset( $_REQUEST['tracksEventProp'] ) ) {
  674. if ( is_array( $_REQUEST['tracksEventProp'] ) ) {
  675. $tracks_data = $_REQUEST['tracksEventProp'];
  676. } else {
  677. $tracks_data = array( 'clicked' => $_REQUEST['tracksEventProp'] );
  678. }
  679. }
  680. JetpackTracking::record_user_event( $_REQUEST['tracksEventName'], $tracks_data );
  681. wp_send_json_success();
  682. wp_die();
  683. }
  684. /**
  685. * The callback for the JITM ajax requests.
  686. */
  687. function jetpack_jitm_ajax_callback() {
  688. // Check for nonce
  689. if ( ! isset( $_REQUEST['jitmNonce'] ) || ! wp_verify_nonce( $_REQUEST['jitmNonce'], 'jetpack-jitm-nonce' ) ) {
  690. wp_die( 'Module activation failed due to lack of appropriate permissions' );
  691. }
  692. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'activate' == $_REQUEST['jitmActionToTake'] ) {
  693. $module_slug = $_REQUEST['jitmModule'];
  694. Jetpack::log( 'activate', $module_slug );
  695. Jetpack::activate_module( $module_slug, false, false );
  696. Jetpack::state( 'message', 'no_message' );
  697. //A Jetpack module is being activated through a JITM, track it
  698. $this->stat( 'jitm', $module_slug.'-activated-' . JETPACK__VERSION );
  699. $this->do_stats( 'server_side' );
  700. wp_send_json_success();
  701. }
  702. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'dismiss' == $_REQUEST['jitmActionToTake'] ) {
  703. // get the hide_jitm options array
  704. $jetpack_hide_jitm = Jetpack_Options::get_option( 'hide_jitm' );
  705. $module_slug = $_REQUEST['jitmModule'];
  706. if( ! $jetpack_hide_jitm ) {
  707. $jetpack_hide_jitm = array(
  708. $module_slug => 'hide'
  709. );
  710. } else {
  711. $jetpack_hide_jitm[$module_slug] = 'hide';
  712. }
  713. Jetpack_Options::update_option( 'hide_jitm', $jetpack_hide_jitm );
  714. //jitm is being dismissed forever, track it
  715. $this->stat( 'jitm', $module_slug.'-dismissed-' . JETPACK__VERSION );
  716. $this->do_stats( 'server_side' );
  717. wp_send_json_success();
  718. }
  719. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'launch' == $_REQUEST['jitmActionToTake'] ) {
  720. $module_slug = $_REQUEST['jitmModule'];
  721. // User went to WordPress.com, track this
  722. $this->stat( 'jitm', $module_slug.'-wordpress-tools-' . JETPACK__VERSION );
  723. $this->do_stats( 'server_side' );
  724. wp_send_json_success();
  725. }
  726. if ( isset( $_REQUEST['jitmActionToTake'] ) && 'viewed' == $_REQUEST['jitmActionToTake'] ) {
  727. $track = $_REQUEST['jitmModule'];
  728. // User is viewing JITM, track it.
  729. $this->stat( 'jitm', $track . '-viewed-' . JETPACK__VERSION );
  730. $this->do_stats( 'server_side' );
  731. wp_send_json_success();
  732. }
  733. }
  734. /**
  735. * If there are any stats that need to be pushed, but haven't been, push them now.
  736. */
  737. function push_stats() {
  738. if ( ! empty( $this->stats ) ) {
  739. $this->do_stats( 'server_side' );
  740. }
  741. }
  742. function jetpack_custom_caps( $caps, $cap, $user_id, $args ) {
  743. switch( $cap ) {
  744. case 'jetpack_connect' :
  745. case 'jetpack_reconnect' :
  746. if ( Jetpack::is_development_mode() ) {
  747. $caps = array( 'do_not_allow' );
  748. break;
  749. }
  750. /**
  751. * Pass through. If it's not development mode, these should match disconnect.
  752. * Let users disconnect if it's development mode, just in case things glitch.
  753. */
  754. case 'jetpack_disconnect' :
  755. /**
  756. * In multisite, can individual site admins manage their own connection?
  757. *
  758. * Ideally, this should be extracted out to a separate filter in the Jetpack_Network class.
  759. */
  760. if ( is_multisite() && ! is_super_admin() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
  761. if ( ! Jetpack_Network::init()->get_option( 'sub-site-connection-override' ) ) {
  762. /**
  763. * We need to update the option name -- it's terribly unclear which
  764. * direction the override goes.
  765. *
  766. * @todo: Update the option name to `sub-sites-can-manage-own-connections`
  767. */
  768. $caps = array( 'do_not_allow' );
  769. break;
  770. }
  771. }
  772. $caps = array( 'manage_options' );
  773. break;
  774. case 'jetpack_manage_modules' :
  775. case 'jetpack_activate_modules' :
  776. case 'jetpack_deactivate_modules' :
  777. $caps = array( 'manage_options' );
  778. break;
  779. case 'jetpack_configure_modules' :
  780. $caps = array( 'manage_options' );
  781. break;
  782. case 'jetpack_network_admin_page':
  783. case 'jetpack_network_settings_page':
  784. $caps = array( 'manage_network_plugins' );
  785. break;
  786. case 'jetpack_network_sites_page':
  787. $caps = array( 'manage_sites' );
  788. break;
  789. case 'jetpack_admin_page' :
  790. if ( Jetpack::is_development_mode() ) {
  791. $caps = array( 'manage_options' );
  792. break;
  793. } else {
  794. $caps = array( 'read' );
  795. }
  796. break;
  797. case 'jetpack_connect_user' :
  798. if ( Jetpack::is_development_mode() ) {
  799. $caps = array( 'do_not_allow' );
  800. break;
  801. }
  802. $caps = array( 'read' );
  803. break;
  804. }
  805. return $caps;
  806. }
  807. function require_jetpack_authentication() {
  808. // Don't let anyone authenticate
  809. $_COOKIE = array();
  810. remove_all_filters( 'authenticate' );
  811. remove_all_actions( 'wp_login_failed' );
  812. if ( Jetpack::is_active() ) {
  813. // Allow Jetpack authentication
  814. add_filter( 'authenticate', array( $this, 'authenticate_jetpack' ), 10, 3 );
  815. }
  816. }
  817. /**
  818. * Load language files
  819. * @action plugins_loaded
  820. */
  821. public static function plugin_textdomain() {
  822. // Note to self, the third argument must not be hardcoded, to account for relocated folders.
  823. load_plugin_textdomain( 'jetpack', false, dirname( plugin_basename( JETPACK__PLUGIN_FILE ) ) . '/languages/' );
  824. }
  825. /**
  826. * Register assets for use in various modules and the Jetpack admin page.
  827. *
  828. * @uses wp_script_is, wp_register_script, plugins_url
  829. * @action wp_loaded
  830. * @return null
  831. */
  832. public function register_assets() {
  833. if ( ! wp_script_is( 'spin', 'registered' ) ) {
  834. wp_register_script(
  835. 'spin',
  836. self::get_file_url_for_environment( '_inc/build/spin.min.js', '_inc/spin.js' ),
  837. false,
  838. '1.3'
  839. );
  840. }
  841. if ( ! wp_script_is( 'jquery.spin', 'registered' ) ) {
  842. wp_register_script(
  843. 'jquery.spin',
  844. self::get_file_url_for_environment( '_inc/build/jquery.spin.min.js', '_inc/jquery.spin.js' ),
  845. array( 'jquery', 'spin' ),
  846. '1.3'
  847. );
  848. }
  849. if ( ! wp_script_is( 'jetpack-gallery-settings', 'registered' ) ) {
  850. wp_register_script(
  851. 'jetpack-gallery-settings',
  852. self::get_file_url_for_environment( '_inc/build/gallery-settings.min.js', '_inc/gallery-settings.js' ),
  853. array( 'media-views' ),
  854. '20121225'
  855. );
  856. }
  857. if ( ! wp_script_is( 'jetpack-twitter-timeline', 'registered' ) ) {
  858. wp_register_script(
  859. 'jetpack-twitter-timeline',
  860. self::get_file_url_for_environment( '_inc/build/twitter-timeline.min.js', '_inc/twitter-timeline.js' ),
  861. array( 'jquery' ),
  862. '4.0.0',
  863. true
  864. );
  865. }
  866. if ( ! wp_script_is( 'jetpack-facebook-embed', 'registered' ) ) {
  867. wp_register_script(
  868. 'jetpack-facebook-embed',
  869. self::get_file_url_for_environment( '_inc/build/facebook-embed.min.js', '_inc/facebook-embed.js' ),
  870. array( 'jquery' ),
  871. null,
  872. true
  873. );
  874. /** This filter is documented in modules/sharedaddy/sharing-sources.php */
  875. $fb_app_id = apply_filters( 'jetpack_sharing_facebook_app_id', '249643311490' );
  876. if ( ! is_numeric( $fb_app_id ) ) {
  877. $fb_app_id = '';
  878. }
  879. wp_localize_script(
  880. 'jetpack-facebook-embed',
  881. 'jpfbembed',
  882. array(
  883. 'appid' => $fb_app_id,
  884. 'locale' => $this->get_locale(),
  885. )
  886. );
  887. }
  888. /**
  889. * As jetpack_register_genericons is by default fired off a hook,
  890. * the hook may have already fired by this point.
  891. * So, let's just trigger it manually.
  892. */
  893. require_once( JETPACK__PLUGIN_DIR . '_inc/genericons.php' );
  894. jetpack_register_genericons();
  895. /**
  896. * Register the social logos
  897. */
  898. require_once( JETPACK__PLUGIN_DIR . '_inc/social-logos.php' );
  899. jetpack_register_social_logos();
  900. if ( ! wp_style_is( 'jetpack-icons', 'registered' ) )
  901. wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION );
  902. }
  903. /**
  904. * Guess locale from language code.
  905. *
  906. * @param string $lang Language code.
  907. * @return string|bool
  908. */
  909. function guess_locale_from_lang( $lang ) {
  910. if ( 'en' === $lang || 'en_US' === $lang || ! $lang ) {
  911. return 'en_US';
  912. }
  913. if ( ! class_exists( 'GP_Locales' ) ) {
  914. if ( ! defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) || ! file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  915. return false;
  916. }
  917. require JETPACK__GLOTPRESS_LOCALES_PATH;
  918. }
  919. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  920. // WP.com: get_locale() returns 'it'
  921. $locale = GP_Locales::by_slug( $lang );
  922. } else {
  923. // Jetpack: get_locale() returns 'it_IT';
  924. $locale = GP_Locales::by_field( 'facebook_locale', $lang );
  925. }
  926. if ( ! $locale ) {
  927. return false;
  928. }
  929. if ( empty( $locale->facebook_locale ) ) {
  930. if ( empty( $locale->wp_locale ) ) {
  931. return false;
  932. } else {
  933. // Facebook SDK is smart enough to fall back to en_US if a
  934. // locale isn't supported. Since supported Facebook locales
  935. // can fall out of sync, we'll attempt to use the known
  936. // wp_locale value and rely on said fallback.
  937. return $locale->wp_locale;
  938. }
  939. }
  940. return $locale->facebook_locale;
  941. }
  942. /**
  943. * Get the locale.
  944. *
  945. * @return string|bool
  946. */
  947. function get_locale() {
  948. $locale = $this->guess_locale_from_lang( get_locale() );
  949. if ( ! $locale ) {
  950. $locale = 'en_US';
  951. }
  952. return $locale;
  953. }
  954. /**
  955. * Device Pixels support
  956. * This improves the resolution of gravatars and wordpress.com uploads on hi-res and zoomed browsers.
  957. */
  958. function devicepx() {
  959. if ( Jetpack::is_active() && ! Jetpack_AMP_Support::is_amp_request() ) {
  960. wp_enqueue_script( 'devicepx', 'https://s0.wp.com/wp-content/js/devicepx-jetpack.js', array(), gmdate( 'oW' ), true );
  961. }
  962. }
  963. /**
  964. * Return the network_site_url so that .com knows what network this site is a part of.
  965. * @param bool $option
  966. * @return string
  967. */
  968. public function jetpack_main_network_site_option( $option ) {
  969. return network_site_url();
  970. }
  971. /**
  972. * Network Name.
  973. */
  974. static function network_name( $option = null ) {
  975. global $current_site;
  976. return $current_site->site_name;
  977. }
  978. /**
  979. * Does the network allow new user and site registrations.
  980. * @return string
  981. */
  982. static function network_allow_new_registrations( $option = null ) {
  983. return ( in_array( get_site_option( 'registration' ), array('none', 'user', 'blog', 'all' ) ) ? get_site_option( 'registration') : 'none' );
  984. }
  985. /**
  986. * Does the network allow admins to add new users.
  987. * @return boolian
  988. */
  989. static function network_add_new_users( $option = null ) {
  990. return (bool) get_site_option( 'add_new_users' );
  991. }
  992. /**
  993. * File upload psace left per site in MB.
  994. * -1 means NO LIMIT.
  995. * @return number
  996. */
  997. static function network_site_upload_space( $option = null ) {
  998. // value in MB
  999. return ( get_site_option( 'upload_space_check_disabled' ) ? -1 : get_space_allowed() );
  1000. }
  1001. /**
  1002. * Network allowed file types.
  1003. * @return string
  1004. */
  1005. static function network_upload_file_types( $option = null ) {
  1006. return get_site_option( 'upload_filetypes', 'jpg jpeg png gif' );
  1007. }
  1008. /**
  1009. * Maximum file upload size set by the network.
  1010. * @return number
  1011. */
  1012. static function network_max_upload_file_size( $option = null ) {
  1013. // value in KB
  1014. return get_site_option( 'fileupload_maxk', 300 );
  1015. }
  1016. /**
  1017. * Lets us know if a site allows admins to manage the network.
  1018. * @return array
  1019. */
  1020. static function network_enable_administration_menus( $option = null ) {
  1021. return get_site_option( 'menu_items' );
  1022. }
  1023. /**
  1024. * If a user has been promoted to or demoted from admin, we need to clear the
  1025. * jetpack_other_linked_admins transient.
  1026. *
  1027. * @since 4.3.2
  1028. * @since 4.4.0 $old_roles is null by default and if it's not passed, the transient is cleared.
  1029. *
  1030. * @param int $user_id The user ID whose role changed.
  1031. * @param string $role The new role.
  1032. * @param array $old_roles An array of the user's previous roles.
  1033. */
  1034. function maybe_clear_other_linked_admins_transient( $user_id, $role, $old_roles = null ) {
  1035. if ( 'administrator' == $role
  1036. || ( is_array( $old_roles ) && in_array( 'administrator', $old_roles ) )
  1037. || is_null( $old_roles )
  1038. ) {
  1039. delete_transient( 'jetpack_other_linked_admins' );
  1040. }
  1041. }
  1042. /**
  1043. * Checks to see if there are any other users available to become primary
  1044. * Users must both:
  1045. * - Be linked to wpcom
  1046. * - Be an admin
  1047. *
  1048. * @return mixed False if no other users are linked, Int if there are.
  1049. */
  1050. static function get_other_linked_admins() {
  1051. $other_linked_users = get_transient( 'jetpack_other_linked_admins' );
  1052. if ( false === $other_linked_users ) {
  1053. $admins = get_users( array( 'role' => 'administrator' ) );
  1054. if ( count( $admins ) > 1 ) {
  1055. $available = array();
  1056. foreach ( $admins as $admin ) {
  1057. if ( Jetpack::is_user_connected( $admin->ID ) ) {
  1058. $available[] = $admin->ID;
  1059. }
  1060. }
  1061. $count_connected_admins = count( $available );
  1062. if ( count( $available ) > 1 ) {
  1063. $other_linked_users = $count_connected_admins;
  1064. } else {
  1065. $other_linked_users = 0;
  1066. }
  1067. } else {
  1068. $other_linked_users = 0;
  1069. }
  1070. set_transient( 'jetpack_other_linked_admins', $other_linked_users, HOUR_IN_SECONDS );
  1071. }
  1072. return ( 0 === $other_linked_users ) ? false : $other_linked_users;
  1073. }
  1074. /**
  1075. * Return whether we are dealing with a multi network setup or not.
  1076. * The reason we are type casting this is because we want to avoid the situation where
  1077. * the result is false since when is_main_network_option return false it cases
  1078. * the rest the get_option( 'jetpack_is_multi_network' ); to return the value that is set in the
  1079. * database which could be set to anything as opposed to what this function returns.
  1080. * @param bool $option
  1081. *
  1082. * @return boolean
  1083. */
  1084. public function is_main_network_option( $option ) {
  1085. // return '1' or ''
  1086. return (string) (bool) Jetpack::is_multi_network();
  1087. }
  1088. /**
  1089. * Return true if we are with multi-site or multi-network false if we are dealing with single site.
  1090. *
  1091. * @param string $option
  1092. * @return boolean
  1093. */
  1094. public function is_multisite( $option ) {
  1095. return (string) (bool) is_multisite();
  1096. }
  1097. /**
  1098. * Implemented since there is no core is multi network function
  1099. * Right now there is no way to tell if we which network is the dominant network on the system
  1100. *
  1101. * @since 3.3
  1102. * @return boolean
  1103. */
  1104. public static function is_multi_network() {
  1105. global $wpdb;
  1106. // if we don't have a multi site setup no need to do any more
  1107. if ( ! is_multisite() ) {
  1108. return false;
  1109. }
  1110. $num_sites = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->site}" );
  1111. if ( $num_sites > 1 ) {
  1112. return true;
  1113. } else {
  1114. return false;
  1115. }
  1116. }
  1117. /**
  1118. * Trigger an update to the main_network_site when we update the siteurl of a site.
  1119. * @return null
  1120. */
  1121. function update_jetpack_main_network_site_option() {
  1122. _deprecated_function( __METHOD__, 'jetpack-4.2' );
  1123. }
  1124. /**
  1125. * Triggered after a user updates the network settings via Network Settings Admin Page
  1126. *
  1127. */
  1128. function update_jetpack_network_settings() {
  1129. _deprecated_function( __METHOD__, 'jetpack-4.2' );
  1130. // Only sync this info for the main network site.
  1131. }
  1132. /**
  1133. * Get back if the current site is single user site.
  1134. *
  1135. * @return bool
  1136. */
  1137. public static function is_single_user_site() {
  1138. global $wpdb;
  1139. if ( false === ( $some_users = get_transient( 'jetpack_is_single_user' ) ) ) {
  1140. $some_users = $wpdb->get_var( "SELECT COUNT(*) FROM (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' LIMIT 2) AS someusers" );
  1141. set_transient( 'jetpack_is_single_user', (int) $some_users, 12 * HOUR_IN_SECONDS );
  1142. }
  1143. return 1 === (int) $some_users;
  1144. }
  1145. /**
  1146. * Returns true if the site has file write access false otherwise.
  1147. * @return string ( '1' | '0' )
  1148. **/
  1149. public static function file_system_write_access() {
  1150. if ( ! function_exists( 'get_filesystem_method' ) ) {
  1151. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  1152. }
  1153. require_once( ABSPATH . 'wp-admin/includes/template.php' );
  1154. $filesystem_method = get_filesystem_method();
  1155. if ( $filesystem_method === 'direct' ) {
  1156. return 1;
  1157. }
  1158. ob_start();
  1159. $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
  1160. ob_end_clean();
  1161. if ( $filesystem_credentials_are_stored ) {
  1162. return 1;
  1163. }
  1164. return 0;
  1165. }
  1166. /**
  1167. * Finds out if a site is using a version control system.
  1168. * @return string ( '1' | '0' )
  1169. **/
  1170. public static function is_version_controlled() {
  1171. _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Functions::is_version_controlled' );
  1172. return (string) (int) Jetpack_Sync_Functions::is_version_controlled();
  1173. }
  1174. /**
  1175. * Determines whether the current theme supports featured images or not.
  1176. * @return string ( '1' | '0' )
  1177. */
  1178. public static function featured_images_enabled() {
  1179. _deprecated_function( __METHOD__, 'jetpack-4.2' );
  1180. return current_theme_supports( 'post-thumbnails' ) ? '1' : '0';
  1181. }
  1182. /**
  1183. * Wrapper for core's get_avatar_url(). This one is deprecated.
  1184. *
  1185. * @deprecated 4.7 use get_avatar_url instead.
  1186. * @param int|string|object $id_or_email A user ID, email address, or comment object
  1187. * @param int $size Size of the avatar image
  1188. * @param string $default URL to a default image to use if no avatar is available
  1189. * @param bool $force_display Whether to force it to return an avatar even if show_avatars is disabled
  1190. *
  1191. * @return array
  1192. */
  1193. public static function get_avatar_url( $id_or_email, $size = 96, $default = '', $force_display = false ) {
  1194. _deprecated_function( __METHOD__, 'jetpack-4.7', 'get_avatar_url' );
  1195. return get_avatar_url( $id_or_email, array(
  1196. 'size' => $size,
  1197. 'default' => $default,
  1198. 'force_default' => $force_display,
  1199. ) );
  1200. }
  1201. /**
  1202. * jetpack_updates is saved in the following schema:
  1203. *
  1204. * array (
  1205. * 'plugins' => (int) Number of plugin updates available.
  1206. * 'themes' => (int) Number of theme updates available.
  1207. * 'wordpress' => (int) Number of WordPress core updates available.
  1208. * 'translations' => (int) Number of translation updates available.
  1209. * 'total' => (int) Total of all available updates.
  1210. * 'wp_update_version' => (string) The latest available version of WordPress, only present if a WordPress update is needed.
  1211. * )
  1212. * @return array
  1213. */
  1214. public static function get_updates() {
  1215. $update_data = wp_get_update_data();
  1216. // Stores the individual update counts as well as the total count.
  1217. if ( isset( $update_data['counts'] ) ) {
  1218. $updates = $update_data['counts'];
  1219. }
  1220. // If we need to update WordPress core, let's find the latest version number.
  1221. if ( ! empty( $updates['wordpress'] ) ) {
  1222. $cur = get_preferred_from_update_core();
  1223. if ( isset( $cur->response ) && 'upgrade' === $cur->response ) {
  1224. $updates['wp_update_version'] = $cur->current;
  1225. }
  1226. }
  1227. return isset( $updates ) ? $updates : array();
  1228. }
  1229. public static function get_update_details() {
  1230. $update_details = array(
  1231. 'update_core' => get_site_transient( 'update_core' ),
  1232. 'update_plugins' => get_site_transient( 'update_plugins' ),
  1233. 'update_themes' => get_site_transient( 'update_themes' ),
  1234. );
  1235. return $update_details;
  1236. }
  1237. public static function refresh_update_data() {
  1238. _deprecated_function( __METHOD__, 'jetpack-4.2' );
  1239. }
  1240. public static function refresh_theme_data() {
  1241. _deprecated_function( __METHOD__, 'jetpack-4.2' );
  1242. }
  1243. /**
  1244. * Is Jetpack active?
  1245. */
  1246. public static function is_active() {
  1247. return (bool) Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  1248. }
  1249. /**
  1250. * Make an API call to WordPress.com for plan status
  1251. *
  1252. * @uses Jetpack_Options::get_option()
  1253. * @uses Jetpack_Client::wpcom_json_api_request_as_blog()
  1254. * @uses update_option()
  1255. *
  1256. * @access public
  1257. * @static
  1258. *
  1259. * @return bool True if plan is updated, false if no update
  1260. */
  1261. public static function refresh_active_plan_from_wpcom() {
  1262. // Make the API request
  1263. $request = sprintf( '/sites/%d', Jetpack_Options::get_option( 'id' ) );
  1264. $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' );
  1265. // Bail if there was an error or malformed response
  1266. if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
  1267. return false;
  1268. }
  1269. // Decode the results
  1270. $results = json_decode( $response['body'], true );
  1271. // Bail if there were no results or plan details returned
  1272. if ( ! is_array( $results ) || ! isset( $results['plan'] ) ) {
  1273. return false;
  1274. }
  1275. // Store the option and return true if updated
  1276. return update_option( 'jetpack_active_plan', $results['plan'] );
  1277. }
  1278. /**
  1279. * Get the plan that this Jetpack site is currently using
  1280. *
  1281. * @uses get_option()
  1282. *
  1283. * @access public
  1284. * @static
  1285. *
  1286. * @return array Active Jetpack plan details
  1287. */
  1288. public static function get_active_plan() {
  1289. global $active_plan_cache;
  1290. // this can be expensive to compute so we cache for the duration of a request
  1291. if ( is_array( $active_plan_cache ) && ! empty( $active_plan_cache ) ) {
  1292. return $active_plan_cache;
  1293. }
  1294. $plan = get_option( 'jetpack_active_plan', array() );
  1295. // Set the default options
  1296. $plan = wp_parse_args( $plan, array(
  1297. 'product_slug' => 'jetpack_free',
  1298. 'class' => 'free',
  1299. 'features' => array(
  1300. 'active' => array()
  1301. ),
  1302. ) );
  1303. $supports = array();
  1304. // Define what paid modules are supported by personal plans
  1305. $personal_plans = array(
  1306. 'jetpack_personal',
  1307. 'jetpack_personal_monthly',
  1308. 'personal-bundle',
  1309. 'personal-bundle-2y',
  1310. );
  1311. if ( in_array( $plan['product_slug'], $personal_plans ) ) {
  1312. // special support value, not a module but a separate plugin
  1313. $supports[] = 'akismet';
  1314. $plan['class'] = 'personal';
  1315. }
  1316. // Define what paid modules are supported by premium plans
  1317. $premium_plans = array(
  1318. 'jetpack_premium',
  1319. 'jetpack_premium_monthly',
  1320. 'value_bundle',
  1321. 'value_bundle-2y',
  1322. );
  1323. if ( in_array( $plan['product_slug'], $premium_plans ) ) {
  1324. $supports[] = 'akismet';
  1325. $supports[] = 'simple-payments';
  1326. $supports[] = 'vaultpress';
  1327. $plan['class'] = 'premium';
  1328. }
  1329. // Define what paid modules are supported by professional plans
  1330. $business_plans = array(
  1331. 'jetpack_business',
  1332. 'jetpack_business_monthly',
  1333. 'business-bundle',
  1334. 'business-bundle-2y',
  1335. 'vip',
  1336. );
  1337. if ( in_array( $plan['product_slug'], $business_plans ) ) {
  1338. $supports[] = 'akismet';
  1339. $supports[] = 'simple-payments';
  1340. $supports[] = 'vaultpress';
  1341. $plan['class'] = 'business';
  1342. }
  1343. // get available features
  1344. foreach ( self::get_available_modules() as $module_slug ) {
  1345. $module = self::get_module( $module_slug );
  1346. if ( ! isset( $module ) || ! is_array( $module ) ) {
  1347. continue;
  1348. }
  1349. if ( in_array( 'free', $module['plan_classes'] ) || in_array( $plan['class'], $module['plan_classes'] ) ) {
  1350. $supports[] = $module_slug;
  1351. }
  1352. }
  1353. $plan['supports'] = $supports;
  1354. $active_plan_cache = $plan;
  1355. return $plan;
  1356. }
  1357. /**
  1358. * Determine whether the active plan supports a particular feature
  1359. *
  1360. * @uses Jetpack::get_active_plan()
  1361. *
  1362. * @access public
  1363. * @static
  1364. *
  1365. * @return bool True if plan supports feature, false if not
  1366. */
  1367. public static function active_plan_supports( $feature ) {
  1368. $plan = Jetpack::get_active_plan();
  1369. // Manually mapping WordPress.com features to Jetpack module slugs
  1370. foreach ( $plan['features']['active'] as $wpcom_feature ) {
  1371. switch ( $wpcom_feature ) {
  1372. case 'wordads-jetpack';
  1373. // WordAds are supported for this site
  1374. if ( 'wordads' === $feature ) {
  1375. return true;
  1376. }
  1377. break;
  1378. }
  1379. }
  1380. if (
  1381. in_array( $feature, $plan['supports'] )
  1382. || in_array( $feature, $plan['features']['active'] )
  1383. ) {
  1384. return true;
  1385. }
  1386. return false;
  1387. }
  1388. /**
  1389. * Is Jetpack in development (offline) mode?
  1390. */
  1391. public static function is_development_mode() {
  1392. $development_mode = false;
  1393. if ( defined( 'JETPACK_DEV_DEBUG' ) ) {
  1394. $development_mode = JETPACK_DEV_DEBUG;
  1395. } elseif ( $site_url = site_url() ) {
  1396. $development_mode = false === strpos( $site_url, '.' );
  1397. }
  1398. /**
  1399. * Filters Jetpack's development mode.
  1400. *
  1401. * @see https://jetpack.com/support/development-mode/
  1402. *
  1403. * @since 2.2.1
  1404. *
  1405. * @param bool $development_mode Is Jetpack's development mode active.
  1406. */
  1407. $development_mode = ( bool ) apply_filters( 'jetpack_development_mode', $development_mode );
  1408. return $development_mode;
  1409. }
  1410. /**
  1411. * Whether the site is currently onboarding or not.
  1412. * A site is considered as being onboarded if it currently has an onboarding token.
  1413. *
  1414. * @since 5.8
  1415. *
  1416. * @access public
  1417. * @static
  1418. *
  1419. * @return bool True if the site is currently onboarding, false otherwise
  1420. */
  1421. public static function is_onboarding() {
  1422. return Jetpack_Options::get_option( 'onboarding' ) !== false;
  1423. }
  1424. /**
  1425. * Get Jetpack development mode notice text and notice class.
  1426. *
  1427. * Mirrors the checks made in Jetpack::is_development_mode
  1428. *
  1429. */
  1430. public static function show_development_mode_notice() {
  1431. if ( Jetpack::is_development_mode() ) {
  1432. if ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) {
  1433. $notice = sprintf(
  1434. /* translators: %s is a URL */
  1435. __( 'In <a href="%s" target="_blank">Development Mode</a>, via the JETPACK_DEV_DEBUG constant being defined in wp-config.php or elsewhere.', 'jetpack' ),
  1436. 'https://jetpack.com/support/development-mode/'
  1437. );
  1438. } elseif ( site_url() && false === strpos( site_url(), '.' ) ) {
  1439. $notice = sprintf(
  1440. /* translators: %s is a URL */
  1441. __( 'In <a href="%s" target="_blank">Development Mode</a>, via site URL lacking a dot (e.g. http://localhost).', 'jetpack' ),
  1442. 'https://jetpack.com/support/development-mode/'
  1443. );
  1444. } else {
  1445. $notice = sprintf(
  1446. /* translators: %s is a URL */
  1447. __( 'In <a href="%s" target="_blank">Development Mode</a>, via the jetpack_development_mode filter.', 'jetpack' ),
  1448. 'https://jetpack.com/support/development-mode/'
  1449. );
  1450. }
  1451. echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
  1452. }
  1453. // Throw up a notice if using a development version and as for feedback.
  1454. if ( Jetpack::is_development_version() ) {
  1455. /* translators: %s is a URL */
  1456. $notice = sprintf( __( 'You are currently running a development version of Jetpack. <a href="%s" target="_blank">Submit your feedback</a>', 'jetpack' ), 'https://jetpack.com/contact-support/beta-group/' );
  1457. echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
  1458. }
  1459. // Throw up a notice if using staging mode
  1460. if ( Jetpack::is_staging_site() ) {
  1461. /* translators: %s is a URL */
  1462. $notice = sprintf( __( 'You are running Jetpack on a <a href="%s" target="_blank">staging server</a>.', 'jetpack' ), 'https://jetpack.com/support/staging-sites/' );
  1463. echo '<div class="updated" style="border-color: #f0821e;"><p>' . $notice . '</p></div>';
  1464. }
  1465. }
  1466. /**
  1467. * Whether Jetpack's version maps to a public release, or a development version.
  1468. */
  1469. public static function is_development_version() {
  1470. /**
  1471. * Allows filtering whether this is a development version of Jetpack.
  1472. *
  1473. * This filter is especially useful for tests.
  1474. *
  1475. * @since 4.3.0
  1476. *
  1477. * @param bool $development_version Is this a develoment version of Jetpack?
  1478. */
  1479. return (bool) apply_filters(
  1480. 'jetpack_development_version',
  1481. ! preg_match( '/^\d+(\.\d+)+$/', Jetpack_Constants::get_constant( 'JETPACK__VERSION' ) )
  1482. );
  1483. }
  1484. /**
  1485. * Is a given user (or the current user if none is specified) linked to a WordPress.com user?
  1486. */
  1487. public static function is_user_connected( $user_id = false ) {
  1488. $user_id = false === $user_id ? get_current_user_id() : absint( $user_id );
  1489. if ( ! $user_id ) {
  1490. return false;
  1491. }
  1492. return (bool) Jetpack_Data::get_access_token( $user_id );
  1493. }
  1494. /**
  1495. * Get the wpcom user data of the current|specified connected user.
  1496. */
  1497. public static function get_connected_user_data( $user_id = null ) {
  1498. if ( ! $user_id ) {
  1499. $user_id = get_current_user_id();
  1500. }
  1501. $transient_key = "jetpack_connected_user_data_$user_id";
  1502. if ( $cached_user_data = get_transient( $transient_key ) ) {
  1503. return $cached_user_data;
  1504. }
  1505. Jetpack::load_xml_rpc_client();
  1506. $xml = new Jetpack_IXR_Client( array(
  1507. 'user_id' => $user_id,
  1508. ) );
  1509. $xml->query( 'wpcom.getUser' );
  1510. if ( ! $xml->isError() ) {
  1511. $user_data = $xml->getResponse();
  1512. set_transient( $transient_key, $xml->getResponse(), DAY_IN_SECONDS );
  1513. return $user_data;
  1514. }
  1515. return false;
  1516. }
  1517. /**
  1518. * Get the wpcom email of the current|specified connected user.
  1519. */
  1520. public static function get_connected_user_email( $user_id = null ) {
  1521. if ( ! $user_id ) {
  1522. $user_id = get_current_user_id();
  1523. }
  1524. Jetpack::load_xml_rpc_client();
  1525. $xml = new Jetpack_IXR_Client( array(
  1526. 'user_id' => $user_id,
  1527. ) );
  1528. $xml->query( 'wpcom.getUserEmail' );
  1529. if ( ! $xml->isError() ) {
  1530. return $xml->getResponse();
  1531. }
  1532. return false;
  1533. }
  1534. /**
  1535. * Get the wpcom email of the master user.
  1536. */
  1537. public static function get_master_user_email() {
  1538. $master_user_id = Jetpack_Options::get_option( 'master_user' );
  1539. if ( $master_user_id ) {
  1540. return self::get_connected_user_email( $master_user_id );
  1541. }
  1542. return '';
  1543. }
  1544. function current_user_is_connection_owner() {
  1545. $user_token = Jetpack_Data::get_access_token( JETPACK_MASTER_USER );
  1546. return $user_token && is_object( $user_token ) && isset( $user_token->external_user_id ) && get_current_user_id() === $user_token->external_user_id;
  1547. }
  1548. /**
  1549. * Gets current user IP address.
  1550. *
  1551. * @param bool $check_all_headers Check all headers? Default is `false`.
  1552. *
  1553. * @return string Current user IP address.
  1554. */
  1555. public static function current_user_ip( $check_all_headers = false ) {
  1556. if ( $check_all_headers ) {
  1557. foreach ( array(
  1558. 'HTTP_CF_CONNECTING_IP',
  1559. 'HTTP_CLIENT_IP',
  1560. 'HTTP_X_FORWARDED_FOR',
  1561. 'HTTP_X_FORWARDED',
  1562. 'HTTP_X_CLUSTER_CLIENT_IP',
  1563. 'HTTP_FORWARDED_FOR',
  1564. 'HTTP_FORWARDED',
  1565. 'HTTP_VIA',
  1566. ) as $key ) {
  1567. if ( ! empty( $_SERVER[ $key ] ) ) {
  1568. return $_SERVER[ $key ];
  1569. }
  1570. }
  1571. }
  1572. return ! empty( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
  1573. }
  1574. /**
  1575. * Add any extra oEmbed providers that we know about and use on wpcom for feature parity.
  1576. */
  1577. function extra_oembed_providers() {
  1578. // Cloudup: https://dev.cloudup.com/#oembed
  1579. wp_oembed_add_provider( 'https://cloudup.com/*' , 'https://cloudup.com/oembed' );
  1580. wp_oembed_add_provider( 'https://me.sh/*', 'https://me.sh/oembed?format=json' );
  1581. wp_oembed_add_provider( '#https?://(www\.)?gfycat\.com/.*#i', 'https://api.gfycat.com/v1/oembed', true );
  1582. wp_oembed_add_provider( '#https?://[^.]+\.(wistia\.com|wi\.st)/(medias|embed)/.*#', 'https://fast.wistia.com/oembed', true );
  1583. wp_oembed_add_provider( '#https?://sketchfab\.com/.*#i', 'https://sketchfab.com/oembed', true );
  1584. wp_oembed_add_provider( '#https?://(www\.)?icloud\.com/keynote/.*#i', 'https://iwmb.icloud.com/iwmb/oembed', true );
  1585. }
  1586. /**
  1587. * Synchronize connected user role changes
  1588. */
  1589. function user_role_change( $user_id ) {
  1590. _deprecated_function( __METHOD__, 'jetpack-4.2', 'Jetpack_Sync_Users::user_role_change()' );
  1591. Jetpack_Sync_Users::user_role_change( $user_id );
  1592. }
  1593. /**
  1594. * Loads the currently active modules.
  1595. */
  1596. public static function load_modules() {
  1597. if (
  1598. ! self::is_active()
  1599. && ! self::is_development_mode()
  1600. && ! self::is_onboarding()
  1601. && (
  1602. ! is_multisite()
  1603. || ! get_site_option( 'jetpack_protect_active' )
  1604. )
  1605. ) {
  1606. return;
  1607. }
  1608. $version = Jetpack_Options::get_option( 'version' );
  1609. if ( ! $version ) {
  1610. $version = $old_version = JETPACK__VERSION . ':' . time();
  1611. /** This action is documented in class.jetpack.php */
  1612. do_action( 'updating_jetpack_version', $version, false );
  1613. Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
  1614. }
  1615. list( $version ) = explode( ':', $version );
  1616. $modules = array_filter( Jetpack::get_active_modules(), array( 'Jetpack', 'is_module' ) );
  1617. $modules_data = array();
  1618. // Don't load modules that have had "Major" changes since the stored version until they have been deactivated/reactivated through the lint check.
  1619. if ( version_compare( $version, JETPACK__VERSION, '<' ) ) {
  1620. $updated_modules = array();
  1621. foreach ( $modules as $module ) {
  1622. $modules_data[ $module ] = Jetpack::get_module( $module );
  1623. if ( ! isset( $modules_data[ $module ]['changed'] ) ) {
  1624. continue;
  1625. }
  1626. if ( version_compare( $modules_data[ $module ]['changed'], $version, '<=' ) ) {
  1627. continue;
  1628. }
  1629. $updated_modules[] = $module;
  1630. }
  1631. $modules = array_diff( $modules, $updated_modules );
  1632. }
  1633. $is_development_mode = Jetpack::is_development_mode();
  1634. foreach ( $modules as $index => $module ) {
  1635. // If we're in dev mode, disable modules requiring a connection
  1636. if ( $is_development_mode ) {
  1637. // Prime the pump if we need to
  1638. if ( empty( $modules_data[ $module ] ) ) {
  1639. $modules_data[ $module ] = Jetpack::get_module( $module );
  1640. }
  1641. // If the module requires a connection, but we're in local mode, don't include it.
  1642. if ( $modules_data[ $module ]['requires_connection'] ) {
  1643. continue;
  1644. }
  1645. }
  1646. if ( did_action( 'jetpack_module_loaded_' . $module ) ) {
  1647. continue;
  1648. }
  1649. if ( ! include_once( Jetpack::get_module_path( $module ) ) ) {
  1650. unset( $modules[ $index ] );
  1651. self::update_active_modules( array_values( $modules ) );
  1652. continue;
  1653. }
  1654. /**
  1655. * Fires when a specific module is loaded.
  1656. * The dynamic part of the hook, $module, is the module slug.
  1657. *
  1658. * @since 1.1.0
  1659. */
  1660. do_action( 'jetpack_module_loaded_' . $module );
  1661. }
  1662. /**
  1663. * Fires when all the modules are loaded.
  1664. *
  1665. * @since 1.1.0
  1666. */
  1667. do_action( 'jetpack_modules_loaded' );
  1668. // Load module-specific code that is needed even when a module isn't active. Loaded here because code contained therein may need actions such as setup_theme.
  1669. require_once( JETPACK__PLUGIN_DIR . 'modules/module-extras.php' );
  1670. }
  1671. /**
  1672. * Check if Jetpack's REST API compat file should be included
  1673. * @action plugins_loaded
  1674. * @return null
  1675. */
  1676. public function check_rest_api_compat() {
  1677. /**
  1678. * Filters the list of REST API compat files to be included.
  1679. *
  1680. * @since 2.2.5
  1681. *
  1682. * @param array $args Array of REST API compat files to include.
  1683. */
  1684. $_jetpack_rest_api_compat_includes = apply_filters( 'jetpack_rest_api_compat', array() );
  1685. if ( function_exists( 'bbpress' ) )
  1686. $_jetpack_rest_api_compat_includes[] = JETPACK__PLUGIN_DIR . 'class.jetpack-bbpress-json-api-compat.php';
  1687. foreach ( $_jetpack_rest_api_compat_includes as $_jetpack_rest_api_compat_include )
  1688. require_once $_jetpack_rest_api_compat_include;
  1689. }
  1690. /**
  1691. * Gets all plugins currently active in values, regardless of whether they're
  1692. * traditionally activated or network activated.
  1693. *
  1694. * @todo Store the result in core's object cache maybe?
  1695. */
  1696. public static function get_active_plugins() {
  1697. $active_plugins = (array) get_option( 'active_plugins', array() );
  1698. if ( is_multisite() ) {
  1699. // Due to legacy code, active_sitewide_plugins stores them in the keys,
  1700. // whereas active_plugins stores them in the values.
  1701. $network_plugins = array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
  1702. if ( $network_plugins ) {
  1703. $active_plugins = array_merge( $active_plugins, $network_plugins );
  1704. }
  1705. }
  1706. sort( $active_plugins );
  1707. return array_unique( $active_plugins );
  1708. }
  1709. /**
  1710. * Gets and parses additional plugin data to send with the heartbeat data
  1711. *
  1712. * @since 3.8.1
  1713. *
  1714. * @return array Array of plugin data
  1715. */
  1716. public static function get_parsed_plugin_data() {
  1717. if ( ! function_exists( 'get_plugins' ) ) {
  1718. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  1719. }
  1720. /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
  1721. $all_plugins = apply_filters( 'all_plugins', get_plugins() );
  1722. $active_plugins = Jetpack::get_active_plugins();
  1723. $plugins = array();
  1724. foreach ( $all_plugins as $path => $plugin_data ) {
  1725. $plugins[ $path ] = array(
  1726. 'is_active' => in_array( $path, $active_plugins ),
  1727. 'file' => $path,
  1728. 'name' => $plugin_data['Name'],
  1729. 'version' => $plugin_data['Version'],
  1730. 'author' => $plugin_data['Author'],
  1731. );
  1732. }
  1733. return $plugins;
  1734. }
  1735. /**
  1736. * Gets and parses theme data to send with the heartbeat data
  1737. *
  1738. * @since 3.8.1
  1739. *
  1740. * @return array Array of theme data
  1741. */
  1742. public static function get_parsed_theme_data() {
  1743. $all_themes = wp_get_themes( array( 'allowed' => true ) );
  1744. $header_keys = array( 'Name', 'Author', 'Version', 'ThemeURI', 'AuthorURI', 'Status', 'Tags' );
  1745. $themes = array();
  1746. foreach ( $all_themes as $slug => $theme_data ) {
  1747. $theme_headers = array();
  1748. foreach ( $header_keys as $header_key ) {
  1749. $theme_headers[ $header_key ] = $theme_data->get( $header_key );
  1750. }
  1751. $themes[ $slug ] = array(
  1752. 'is_active_theme' => $slug == wp_get_theme()->get_template(),
  1753. 'slug' => $slug,
  1754. 'theme_root' => $theme_data->get_theme_root_uri(),
  1755. 'parent' => $theme_data->parent(),
  1756. 'headers' => $theme_headers
  1757. );
  1758. }
  1759. return $themes;
  1760. }
  1761. /**
  1762. * Checks whether a specific plugin is active.
  1763. *
  1764. * We don't want to store these in a static variable, in case
  1765. * there are switch_to_blog() calls involved.
  1766. */
  1767. public static function is_plugin_active( $plugin = 'jetpack/jetpack.php' ) {
  1768. return in_array( $plugin, self::get_active_plugins() );
  1769. }
  1770. /**
  1771. * Check if Jetpack's Open Graph tags should be used.
  1772. * If certain plugins are active, Jetpack's og tags are suppressed.
  1773. *
  1774. * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
  1775. * @action plugins_loaded
  1776. * @return null
  1777. */
  1778. public function check_open_graph() {
  1779. if ( in_array( 'publicize', Jetpack::get_active_modules() ) || in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) {
  1780. add_filter( 'jetpack_enable_open_graph', '__return_true', 0 );
  1781. }
  1782. $active_plugins = self::get_active_plugins();
  1783. if ( ! empty( $active_plugins ) ) {
  1784. foreach ( $this->open_graph_conflicting_plugins as $plugin ) {
  1785. if ( in_array( $plugin, $active_plugins ) ) {
  1786. add_filter( 'jetpack_enable_open_graph', '__return_false', 99 );
  1787. break;
  1788. }
  1789. }
  1790. }
  1791. /**
  1792. * Allow the addition of Open Graph Meta Tags to all pages.
  1793. *
  1794. * @since 2.0.3
  1795. *
  1796. * @param bool false Should Open Graph Meta tags be added. Default to false.
  1797. */
  1798. if ( apply_filters( 'jetpack_enable_open_graph', false ) ) {
  1799. require_once JETPACK__PLUGIN_DIR . 'functions.opengraph.php';
  1800. }
  1801. }
  1802. /**
  1803. * Check if Jetpack's Twitter tags should be used.
  1804. * If certain plugins are active, Jetpack's twitter tags are suppressed.
  1805. *
  1806. * @uses Jetpack::get_active_modules, add_filter, get_option, apply_filters
  1807. * @action plugins_loaded
  1808. * @return null
  1809. */
  1810. public function check_twitter_tags() {
  1811. $active_plugins = self::get_active_plugins();
  1812. if ( ! empty( $active_plugins ) ) {
  1813. foreach ( $this->twitter_cards_conflicting_plugins as $plugin ) {
  1814. if ( in_array( $plugin, $active_plugins ) ) {
  1815. add_filter( 'jetpack_disable_twitter_cards', '__return_true', 99 );
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. /**
  1821. * Allow Twitter Card Meta tags to be disabled.
  1822. *
  1823. * @since 2.6.0
  1824. *
  1825. * @param bool true Should Twitter Card Meta tags be disabled. Default to true.
  1826. */
  1827. if ( ! apply_filters( 'jetpack_disable_twitter_cards', false ) ) {
  1828. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-twitter-cards.php';
  1829. }
  1830. }
  1831. /**
  1832. * Allows plugins to submit security reports.
  1833. *
  1834. * @param string $type Report type (login_form, backup, file_scanning, spam)
  1835. * @param string $plugin_file Plugin __FILE__, so that we can pull plugin data
  1836. * @param array $args See definitions above
  1837. */
  1838. public static function submit_security_report( $type = '', $plugin_file = '', $args = array() ) {
  1839. _deprecated_function( __FUNCTION__, 'jetpack-4.2', null );
  1840. }
  1841. /* Jetpack Options API */
  1842. public static function get_option_names( $type = 'compact' ) {
  1843. return Jetpack_Options::get_option_names( $type );
  1844. }
  1845. /**
  1846. * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate.
  1847. *
  1848. * @param string $name Option name
  1849. * @param mixed $default (optional)
  1850. */
  1851. public static function get_option( $name, $default = false ) {
  1852. return Jetpack_Options::get_option( $name, $default );
  1853. }
  1854. /**
  1855. * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate.
  1856. *
  1857. * @deprecated 3.4 use Jetpack_Options::update_option() instead.
  1858. * @param string $name Option name
  1859. * @param mixed $value Option value
  1860. */
  1861. public static function update_option( $name, $value ) {
  1862. _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_option()' );
  1863. return Jetpack_Options::update_option( $name, $value );
  1864. }
  1865. /**
  1866. * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate.
  1867. *
  1868. * @deprecated 3.4 use Jetpack_Options::update_options() instead.
  1869. * @param array $array array( option name => option value, ... )
  1870. */
  1871. public static function update_options( $array ) {
  1872. _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::update_options()' );
  1873. return Jetpack_Options::update_options( $array );
  1874. }
  1875. /**
  1876. * Deletes the given option. May be passed multiple option names as an array.
  1877. * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
  1878. *
  1879. * @deprecated 3.4 use Jetpack_Options::delete_option() instead.
  1880. * @param string|array $names
  1881. */
  1882. public static function delete_option( $names ) {
  1883. _deprecated_function( __METHOD__, 'jetpack-3.4', 'Jetpack_Options::delete_option()' );
  1884. return Jetpack_Options::delete_option( $names );
  1885. }
  1886. /**
  1887. * Enters a user token into the user_tokens option
  1888. *
  1889. * @param int $user_id
  1890. * @param string $token
  1891. * return bool
  1892. */
  1893. public static function update_user_token( $user_id, $token, $is_master_user ) {
  1894. // not designed for concurrent updates
  1895. $user_tokens = Jetpack_Options::get_option( 'user_tokens' );
  1896. if ( ! is_array( $user_tokens ) )
  1897. $user_tokens = array();
  1898. $user_tokens[$user_id] = $token;
  1899. if ( $is_master_user ) {
  1900. $master_user = $user_id;
  1901. $options = compact( 'user_tokens', 'master_user' );
  1902. } else {
  1903. $options = compact( 'user_tokens' );
  1904. }
  1905. return Jetpack_Options::update_options( $options );
  1906. }
  1907. /**
  1908. * Returns an array of all PHP files in the specified absolute path.
  1909. * Equivalent to glob( "$absolute_path/*.php" ).
  1910. *
  1911. * @param string $absolute_path The absolute path of the directory to search.
  1912. * @return array Array of absolute paths to the PHP files.
  1913. */
  1914. public static function glob_php( $absolute_path ) {
  1915. if ( function_exists( 'glob' ) ) {
  1916. return glob( "$absolute_path/*.php" );
  1917. }
  1918. $absolute_path = untrailingslashit( $absolute_path );
  1919. $files = array();
  1920. if ( ! $dir = @opendir( $absolute_path ) ) {
  1921. return $files;
  1922. }
  1923. while ( false !== $file = readdir( $dir ) ) {
  1924. if ( '.' == substr( $file, 0, 1 ) || '.php' != substr( $file, -4 ) ) {
  1925. continue;
  1926. }
  1927. $file = "$absolute_path/$file";
  1928. if ( ! is_file( $file ) ) {
  1929. continue;
  1930. }
  1931. $files[] = $file;
  1932. }
  1933. closedir( $dir );
  1934. return $files;
  1935. }
  1936. public static function activate_new_modules( $redirect = false ) {
  1937. if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
  1938. return;
  1939. }
  1940. $jetpack_old_version = Jetpack_Options::get_option( 'version' ); // [sic]
  1941. if ( ! $jetpack_old_version ) {
  1942. $jetpack_old_version = $version = $old_version = '1.1:' . time();
  1943. /** This action is documented in class.jetpack.php */
  1944. do_action( 'updating_jetpack_version', $version, false );
  1945. Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
  1946. }
  1947. list( $jetpack_version ) = explode( ':', $jetpack_old_version ); // [sic]
  1948. if ( version_compare( JETPACK__VERSION, $jetpack_version, '<=' ) ) {
  1949. return;
  1950. }
  1951. $active_modules = Jetpack::get_active_modules();
  1952. $reactivate_modules = array();
  1953. foreach ( $active_modules as $active_module ) {
  1954. $module = Jetpack::get_module( $active_module );
  1955. if ( ! isset( $module['changed'] ) ) {
  1956. continue;
  1957. }
  1958. if ( version_compare( $module['changed'], $jetpack_version, '<=' ) ) {
  1959. continue;
  1960. }
  1961. $reactivate_modules[] = $active_module;
  1962. Jetpack::deactivate_module( $active_module );
  1963. }
  1964. $new_version = JETPACK__VERSION . ':' . time();
  1965. /** This action is documented in class.jetpack.php */
  1966. do_action( 'updating_jetpack_version', $new_version, $jetpack_old_version );
  1967. Jetpack_Options::update_options(
  1968. array(
  1969. 'version' => $new_version,
  1970. 'old_version' => $jetpack_old_version,
  1971. )
  1972. );
  1973. Jetpack::state( 'message', 'modules_activated' );
  1974. Jetpack::activate_default_modules( $jetpack_version, JETPACK__VERSION, $reactivate_modules );
  1975. if ( $redirect ) {
  1976. $page = 'jetpack'; // make sure we redirect to either settings or the jetpack page
  1977. if ( isset( $_GET['page'] ) && in_array( $_GET['page'], array( 'jetpack', 'jetpack_modules' ) ) ) {
  1978. $page = $_GET['page'];
  1979. }
  1980. wp_safe_redirect( Jetpack::admin_url( 'page=' . $page ) );
  1981. exit;
  1982. }
  1983. }
  1984. /**
  1985. * List available Jetpack modules. Simply lists .php files in /modules/.
  1986. * Make sure to tuck away module "library" files in a sub-directory.
  1987. */
  1988. public static function get_available_modules( $min_version = false, $max_version = false ) {
  1989. static $modules = null;
  1990. if ( ! isset( $modules ) ) {
  1991. $available_modules_option = Jetpack_Options::get_option( 'available_modules', array() );
  1992. // Use the cache if we're on the front-end and it's available...
  1993. if ( ! is_admin() && ! empty( $available_modules_option[ JETPACK__VERSION ] ) ) {
  1994. $modules = $available_modules_option[ JETPACK__VERSION ];
  1995. } else {
  1996. $files = Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules' );
  1997. $modules = array();
  1998. foreach ( $files as $file ) {
  1999. if ( ! $headers = Jetpack::get_module( $file ) ) {
  2000. continue;
  2001. }
  2002. $modules[ Jetpack::get_module_slug( $file ) ] = $headers['introduced'];
  2003. }
  2004. Jetpack_Options::update_option( 'available_modules', array(
  2005. JETPACK__VERSION => $modules,
  2006. ) );
  2007. }
  2008. }
  2009. /**
  2010. * Filters the array of modules available to be activated.
  2011. *
  2012. * @since 2.4.0
  2013. *
  2014. * @param array $modules Array of available modules.
  2015. * @param string $min_version Minimum version number required to use modules.
  2016. * @param string $max_version Maximum version number required to use modules.
  2017. */
  2018. $mods = apply_filters( 'jetpack_get_available_modules', $modules, $min_version, $max_version );
  2019. if ( ! $min_version && ! $max_version ) {
  2020. return array_keys( $mods );
  2021. }
  2022. $r = array();
  2023. foreach ( $mods as $slug => $introduced ) {
  2024. if ( $min_version && version_compare( $min_version, $introduced, '>=' ) ) {
  2025. continue;
  2026. }
  2027. if ( $max_version && version_compare( $max_version, $introduced, '<' ) ) {
  2028. continue;
  2029. }
  2030. $r[] = $slug;
  2031. }
  2032. return $r;
  2033. }
  2034. /**
  2035. * Default modules loaded on activation.
  2036. */
  2037. public static function get_default_modules( $min_version = false, $max_version = false ) {
  2038. $return = array();
  2039. foreach ( Jetpack::get_available_modules( $min_version, $max_version ) as $module ) {
  2040. $module_data = Jetpack::get_module( $module );
  2041. switch ( strtolower( $module_data['auto_activate'] ) ) {
  2042. case 'yes' :
  2043. $return[] = $module;
  2044. break;
  2045. case 'public' :
  2046. if ( Jetpack_Options::get_option( 'public' ) ) {
  2047. $return[] = $module;
  2048. }
  2049. break;
  2050. case 'no' :
  2051. default :
  2052. break;
  2053. }
  2054. }
  2055. /**
  2056. * Filters the array of default modules.
  2057. *
  2058. * @since 2.5.0
  2059. *
  2060. * @param array $return Array of default modules.
  2061. * @param string $min_version Minimum version number required to use modules.
  2062. * @param string $max_version Maximum version number required to use modules.
  2063. */
  2064. return apply_filters( 'jetpack_get_default_modules', $return, $min_version, $max_version );
  2065. }
  2066. /**
  2067. * Checks activated modules during auto-activation to determine
  2068. * if any of those modules are being deprecated. If so, close
  2069. * them out, and add any replacement modules.
  2070. *
  2071. * Runs at priority 99 by default.
  2072. *
  2073. * This is run late, so that it can still activate a module if
  2074. * the new module is a replacement for another that the user
  2075. * currently has active, even if something at the normal priority
  2076. * would kibosh everything.
  2077. *
  2078. * @since 2.6
  2079. * @uses jetpack_get_default_modules filter
  2080. * @param array $modules
  2081. * @return array
  2082. */
  2083. function handle_deprecated_modules( $modules ) {
  2084. $deprecated_modules = array(
  2085. 'debug' => null, // Closed out and moved to ./class.jetpack-debugger.php
  2086. 'wpcc' => 'sso', // Closed out in 2.6 -- SSO provides the same functionality.
  2087. 'gplus-authorship' => null, // Closed out in 3.2 -- Google dropped support.
  2088. );
  2089. // Don't activate SSO if they never completed activating WPCC.
  2090. if ( Jetpack::is_module_active( 'wpcc' ) ) {
  2091. $wpcc_options = Jetpack_Options::get_option( 'wpcc_options' );
  2092. if ( empty( $wpcc_options ) || empty( $wpcc_options['client_id'] ) || empty( $wpcc_options['client_id'] ) ) {
  2093. $deprecated_modules['wpcc'] = null;
  2094. }
  2095. }
  2096. foreach ( $deprecated_modules as $module => $replacement ) {
  2097. if ( Jetpack::is_module_active( $module ) ) {
  2098. self::deactivate_module( $module );
  2099. if ( $replacement ) {
  2100. $modules[] = $replacement;
  2101. }
  2102. }
  2103. }
  2104. return array_unique( $modules );
  2105. }
  2106. /**
  2107. * Checks activated plugins during auto-activation to determine
  2108. * if any of those plugins are in the list with a corresponding module
  2109. * that is not compatible with the plugin. The module will not be allowed
  2110. * to auto-activate.
  2111. *
  2112. * @since 2.6
  2113. * @uses jetpack_get_default_modules filter
  2114. * @param array $modules
  2115. * @return array
  2116. */
  2117. function filter_default_modules( $modules ) {
  2118. $active_plugins = self::get_active_plugins();
  2119. if ( ! empty( $active_plugins ) ) {
  2120. // For each module we'd like to auto-activate...
  2121. foreach ( $modules as $key => $module ) {
  2122. // If there are potential conflicts for it...
  2123. if ( ! empty( $this->conflicting_plugins[ $module ] ) ) {
  2124. // For each potential conflict...
  2125. foreach ( $this->conflicting_plugins[ $module ] as $title => $plugin ) {
  2126. // If that conflicting plugin is active...
  2127. if ( in_array( $plugin, $active_plugins ) ) {
  2128. // Remove that item from being auto-activated.
  2129. unset( $modules[ $key ] );
  2130. }
  2131. }
  2132. }
  2133. }
  2134. }
  2135. return $modules;
  2136. }
  2137. /**
  2138. * Extract a module's slug from its full path.
  2139. */
  2140. public static function get_module_slug( $file ) {
  2141. return str_replace( '.php', '', basename( $file ) );
  2142. }
  2143. /**
  2144. * Generate a module's path from its slug.
  2145. */
  2146. public static function get_module_path( $slug ) {
  2147. return JETPACK__PLUGIN_DIR . "modules/$slug.php";
  2148. }
  2149. /**
  2150. * Load module data from module file. Headers differ from WordPress
  2151. * plugin headers to avoid them being identified as standalone
  2152. * plugins on the WordPress plugins page.
  2153. */
  2154. public static function get_module( $module ) {
  2155. $headers = array(
  2156. 'name' => 'Module Name',
  2157. 'description' => 'Module Description',
  2158. 'jumpstart_desc' => 'Jumpstart Description',
  2159. 'sort' => 'Sort Order',
  2160. 'recommendation_order' => 'Recommendation Order',
  2161. 'introduced' => 'First Introduced',
  2162. 'changed' => 'Major Changes In',
  2163. 'deactivate' => 'Deactivate',
  2164. 'free' => 'Free',
  2165. 'requires_connection' => 'Requires Connection',
  2166. 'auto_activate' => 'Auto Activate',
  2167. 'module_tags' => 'Module Tags',
  2168. 'feature' => 'Feature',
  2169. 'additional_search_queries' => 'Additional Search Queries',
  2170. 'plan_classes' => 'Plans',
  2171. );
  2172. $file = Jetpack::get_module_path( Jetpack::get_module_slug( $module ) );
  2173. $mod = Jetpack::get_file_data( $file, $headers );
  2174. if ( empty( $mod['name'] ) ) {
  2175. return false;
  2176. }
  2177. $mod['sort'] = empty( $mod['sort'] ) ? 10 : (int) $mod['sort'];
  2178. $mod['recommendation_order'] = empty( $mod['recommendation_order'] ) ? 20 : (int) $mod['recommendation_order'];
  2179. $mod['deactivate'] = empty( $mod['deactivate'] );
  2180. $mod['free'] = empty( $mod['free'] );
  2181. $mod['requires_connection'] = ( ! empty( $mod['requires_connection'] ) && 'No' == $mod['requires_connection'] ) ? false : true;
  2182. if ( empty( $mod['auto_activate'] ) || ! in_array( strtolower( $mod['auto_activate'] ), array( 'yes', 'no', 'public' ) ) ) {
  2183. $mod['auto_activate'] = 'No';
  2184. } else {
  2185. $mod['auto_activate'] = (string) $mod['auto_activate'];
  2186. }
  2187. if ( $mod['module_tags'] ) {
  2188. $mod['module_tags'] = explode( ',', $mod['module_tags'] );
  2189. $mod['module_tags'] = array_map( 'trim', $mod['module_tags'] );
  2190. $mod['module_tags'] = array_map( array( __CLASS__, 'translate_module_tag' ), $mod['module_tags'] );
  2191. } else {
  2192. $mod['module_tags'] = array( self::translate_module_tag( 'Other' ) );
  2193. }
  2194. if ( $mod['plan_classes'] ) {
  2195. $mod['plan_classes'] = explode( ',', $mod['plan_classes'] );
  2196. $mod['plan_classes'] = array_map( 'strtolower', array_map( 'trim', $mod['plan_classes'] ) );
  2197. } else {
  2198. $mod['plan_classes'] = array( 'free' );
  2199. }
  2200. if ( $mod['feature'] ) {
  2201. $mod['feature'] = explode( ',', $mod['feature'] );
  2202. $mod['feature'] = array_map( 'trim', $mod['feature'] );
  2203. } else {
  2204. $mod['feature'] = array( self::translate_module_tag( 'Other' ) );
  2205. }
  2206. /**
  2207. * Filters the feature array on a module.
  2208. *
  2209. * This filter allows you to control where each module is filtered: Recommended,
  2210. * Jumpstart, and the default "Other" listing.
  2211. *
  2212. * @since 3.5.0
  2213. *
  2214. * @param array $mod['feature'] The areas to feature this module:
  2215. * 'Jumpstart' adds to the "Jumpstart" option to activate many modules at once.
  2216. * 'Recommended' shows on the main Jetpack admin screen.
  2217. * 'Other' should be the default if no other value is in the array.
  2218. * @param string $module The slug of the module, e.g. sharedaddy.
  2219. * @param array $mod All the currently assembled module data.
  2220. */
  2221. $mod['feature'] = apply_filters( 'jetpack_module_feature', $mod['feature'], $module, $mod );
  2222. /**
  2223. * Filter the returned data about a module.
  2224. *
  2225. * This filter allows overriding any info about Jetpack modules. It is dangerous,
  2226. * so please be careful.
  2227. *
  2228. * @since 3.6.0
  2229. *
  2230. * @param array $mod The details of the requested module.
  2231. * @param string $module The slug of the module, e.g. sharedaddy
  2232. * @param string $file The path to the module source file.
  2233. */
  2234. return apply_filters( 'jetpack_get_module', $mod, $module, $file );
  2235. }
  2236. /**
  2237. * Like core's get_file_data implementation, but caches the result.
  2238. */
  2239. public static function get_file_data( $file, $headers ) {
  2240. //Get just the filename from $file (i.e. exclude full path) so that a consistent hash is generated
  2241. $file_name = basename( $file );
  2242. $cache_key = 'jetpack_file_data_' . JETPACK__VERSION;
  2243. $file_data_option = get_transient( $cache_key );
  2244. if ( false === $file_data_option ) {
  2245. $file_data_option = array();
  2246. }
  2247. $key = md5( $file_name . serialize( $headers ) );
  2248. $refresh_cache = is_admin() && isset( $_GET['page'] ) && 'jetpack' === substr( $_GET['page'], 0, 7 );
  2249. // If we don't need to refresh the cache, and already have the value, short-circuit!
  2250. if ( ! $refresh_cache && isset( $file_data_option[ $key ] ) ) {
  2251. return $file_data_option[ $key ];
  2252. }
  2253. $data = get_file_data( $file, $headers );
  2254. $file_data_option[ $key ] = $data;
  2255. set_transient( $cache_key, $file_data_option, 29 * DAY_IN_SECONDS );
  2256. return $data;
  2257. }
  2258. /**
  2259. * Return translated module tag.
  2260. *
  2261. * @param string $tag Tag as it appears in each module heading.
  2262. *
  2263. * @return mixed
  2264. */
  2265. public static function translate_module_tag( $tag ) {
  2266. return jetpack_get_module_i18n_tag( $tag );
  2267. }
  2268. /**
  2269. * Get i18n strings as a JSON-encoded string
  2270. *
  2271. * @return string The locale as JSON
  2272. */
  2273. public static function get_i18n_data_json() {
  2274. $i18n_json = JETPACK__PLUGIN_DIR . 'languages/json/jetpack-' . jetpack_get_user_locale() . '.json';
  2275. if ( is_file( $i18n_json ) && is_readable( $i18n_json ) ) {
  2276. $locale_data = @file_get_contents( $i18n_json );
  2277. if ( $locale_data ) {
  2278. return $locale_data;
  2279. }
  2280. }
  2281. // Return valid empty Jed locale
  2282. return json_encode( array(
  2283. '' => array(
  2284. 'domain' => 'jetpack',
  2285. 'lang' => is_admin() ? get_user_locale() : get_locale(),
  2286. ),
  2287. ) );
  2288. }
  2289. /**
  2290. * Return module name translation. Uses matching string created in modules/module-headings.php.
  2291. *
  2292. * @since 3.9.2
  2293. *
  2294. * @param array $modules
  2295. *
  2296. * @return string|void
  2297. */
  2298. public static function get_translated_modules( $modules ) {
  2299. foreach ( $modules as $index => $module ) {
  2300. $i18n_module = jetpack_get_module_i18n( $module['module'] );
  2301. if ( isset( $module['name'] ) ) {
  2302. $modules[ $index ]['name'] = $i18n_module['name'];
  2303. }
  2304. if ( isset( $module['description'] ) ) {
  2305. $modules[ $index ]['description'] = $i18n_module['description'];
  2306. $modules[ $index ]['short_description'] = $i18n_module['description'];
  2307. }
  2308. }
  2309. return $modules;
  2310. }
  2311. /**
  2312. * Get a list of activated modules as an array of module slugs.
  2313. */
  2314. public static function get_active_modules() {
  2315. $active = Jetpack_Options::get_option( 'active_modules' );
  2316. if ( ! is_array( $active ) ) {
  2317. $active = array();
  2318. }
  2319. if ( class_exists( 'VaultPress' ) || function_exists( 'vaultpress_contact_service' ) ) {
  2320. $active[] = 'vaultpress';
  2321. } else {
  2322. $active = array_diff( $active, array( 'vaultpress' ) );
  2323. }
  2324. //If protect is active on the main site of a multisite, it should be active on all sites.
  2325. if ( ! in_array( 'protect', $active ) && is_multisite() && get_site_option( 'jetpack_protect_active' ) ) {
  2326. $active[] = 'protect';
  2327. }
  2328. /**
  2329. * Allow filtering of the active modules.
  2330. *
  2331. * Gives theme and plugin developers the power to alter the modules that
  2332. * are activated on the fly.
  2333. *
  2334. * @since 5.8.0
  2335. *
  2336. * @param array $active Array of active module slugs.
  2337. */
  2338. $active = apply_filters( 'jetpack_active_modules', $active );
  2339. return array_unique( $active );
  2340. }
  2341. /**
  2342. * Check whether or not a Jetpack module is active.
  2343. *
  2344. * @param string $module The slug of a Jetpack module.
  2345. * @return bool
  2346. *
  2347. * @static
  2348. */
  2349. public static function is_module_active( $module ) {
  2350. return in_array( $module, self::get_active_modules() );
  2351. }
  2352. public static function is_module( $module ) {
  2353. return ! empty( $module ) && ! validate_file( $module, Jetpack::get_available_modules() );
  2354. }
  2355. /**
  2356. * Catches PHP errors. Must be used in conjunction with output buffering.
  2357. *
  2358. * @param bool $catch True to start catching, False to stop.
  2359. *
  2360. * @static
  2361. */
  2362. public static function catch_errors( $catch ) {
  2363. static $display_errors, $error_reporting;
  2364. if ( $catch ) {
  2365. $display_errors = @ini_set( 'display_errors', 1 );
  2366. $error_reporting = @error_reporting( E_ALL );
  2367. add_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
  2368. } else {
  2369. @ini_set( 'display_errors', $display_errors );
  2370. @error_reporting( $error_reporting );
  2371. remove_action( 'shutdown', array( 'Jetpack', 'catch_errors_on_shutdown' ), 0 );
  2372. }
  2373. }
  2374. /**
  2375. * Saves any generated PHP errors in ::state( 'php_errors', {errors} )
  2376. */
  2377. public static function catch_errors_on_shutdown() {
  2378. Jetpack::state( 'php_errors', self::alias_directories( ob_get_clean() ) );
  2379. }
  2380. /**
  2381. * Rewrite any string to make paths easier to read.
  2382. *
  2383. * Rewrites ABSPATH (eg `/home/jetpack/wordpress/`) to ABSPATH, and if WP_CONTENT_DIR
  2384. * is located outside of ABSPATH, rewrites that to WP_CONTENT_DIR.
  2385. *
  2386. * @param $string
  2387. * @return mixed
  2388. */
  2389. public static function alias_directories( $string ) {
  2390. // ABSPATH has a trailing slash.
  2391. $string = str_replace( ABSPATH, 'ABSPATH/', $string );
  2392. // WP_CONTENT_DIR does not have a trailing slash.
  2393. $string = str_replace( WP_CONTENT_DIR, 'WP_CONTENT_DIR', $string );
  2394. return $string;
  2395. }
  2396. public static function activate_default_modules(
  2397. $min_version = false,
  2398. $max_version = false,
  2399. $other_modules = array(),
  2400. $redirect = true,
  2401. $send_state_messages = true
  2402. ) {
  2403. $jetpack = Jetpack::init();
  2404. $modules = Jetpack::get_default_modules( $min_version, $max_version );
  2405. $modules = array_merge( $other_modules, $modules );
  2406. // Look for standalone plugins and disable if active.
  2407. $to_deactivate = array();
  2408. foreach ( $modules as $module ) {
  2409. if ( isset( $jetpack->plugins_to_deactivate[$module] ) ) {
  2410. $to_deactivate[$module] = $jetpack->plugins_to_deactivate[$module];
  2411. }
  2412. }
  2413. $deactivated = array();
  2414. foreach ( $to_deactivate as $module => $deactivate_me ) {
  2415. list( $probable_file, $probable_title ) = $deactivate_me;
  2416. if ( Jetpack_Client_Server::deactivate_plugin( $probable_file, $probable_title ) ) {
  2417. $deactivated[] = $module;
  2418. }
  2419. }
  2420. if ( $deactivated && $redirect ) {
  2421. Jetpack::state( 'deactivated_plugins', join( ',', $deactivated ) );
  2422. $url = add_query_arg(
  2423. array(
  2424. 'action' => 'activate_default_modules',
  2425. '_wpnonce' => wp_create_nonce( 'activate_default_modules' ),
  2426. ),
  2427. add_query_arg( compact( 'min_version', 'max_version', 'other_modules' ), Jetpack::admin_url( 'page=jetpack' ) )
  2428. );
  2429. wp_safe_redirect( $url );
  2430. exit;
  2431. }
  2432. /**
  2433. * Fires before default modules are activated.
  2434. *
  2435. * @since 1.9.0
  2436. *
  2437. * @param string $min_version Minimum version number required to use modules.
  2438. * @param string $max_version Maximum version number required to use modules.
  2439. * @param array $other_modules Array of other modules to activate alongside the default modules.
  2440. */
  2441. do_action( 'jetpack_before_activate_default_modules', $min_version, $max_version, $other_modules );
  2442. // Check each module for fatal errors, a la wp-admin/plugins.php::activate before activating
  2443. if ( $send_state_messages ) {
  2444. Jetpack::restate();
  2445. Jetpack::catch_errors( true );
  2446. }
  2447. $active = Jetpack::get_active_modules();
  2448. foreach ( $modules as $module ) {
  2449. if ( did_action( "jetpack_module_loaded_$module" ) ) {
  2450. $active[] = $module;
  2451. self::update_active_modules( $active );
  2452. continue;
  2453. }
  2454. if ( $send_state_messages && in_array( $module, $active ) ) {
  2455. $module_info = Jetpack::get_module( $module );
  2456. if ( ! $module_info['deactivate'] ) {
  2457. $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
  2458. if ( $active_state = Jetpack::state( $state ) ) {
  2459. $active_state = explode( ',', $active_state );
  2460. } else {
  2461. $active_state = array();
  2462. }
  2463. $active_state[] = $module;
  2464. Jetpack::state( $state, implode( ',', $active_state ) );
  2465. }
  2466. continue;
  2467. }
  2468. $file = Jetpack::get_module_path( $module );
  2469. if ( ! file_exists( $file ) ) {
  2470. continue;
  2471. }
  2472. // we'll override this later if the plugin can be included without fatal error
  2473. if ( $redirect ) {
  2474. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  2475. }
  2476. if ( $send_state_messages ) {
  2477. Jetpack::state( 'error', 'module_activation_failed' );
  2478. Jetpack::state( 'module', $module );
  2479. }
  2480. ob_start();
  2481. require_once $file;
  2482. $active[] = $module;
  2483. if ( $send_state_messages ) {
  2484. $state = in_array( $module, $other_modules ) ? 'reactivated_modules' : 'activated_modules';
  2485. if ( $active_state = Jetpack::state( $state ) ) {
  2486. $active_state = explode( ',', $active_state );
  2487. } else {
  2488. $active_state = array();
  2489. }
  2490. $active_state[] = $module;
  2491. Jetpack::state( $state, implode( ',', $active_state ) );
  2492. }
  2493. Jetpack::update_active_modules( $active );
  2494. ob_end_clean();
  2495. }
  2496. if ( $send_state_messages ) {
  2497. Jetpack::state( 'error', false );
  2498. Jetpack::state( 'module', false );
  2499. }
  2500. Jetpack::catch_errors( false );
  2501. /**
  2502. * Fires when default modules are activated.
  2503. *
  2504. * @since 1.9.0
  2505. *
  2506. * @param string $min_version Minimum version number required to use modules.
  2507. * @param string $max_version Maximum version number required to use modules.
  2508. * @param array $other_modules Array of other modules to activate alongside the default modules.
  2509. */
  2510. do_action( 'jetpack_activate_default_modules', $min_version, $max_version, $other_modules );
  2511. }
  2512. public static function activate_module( $module, $exit = true, $redirect = true ) {
  2513. /**
  2514. * Fires before a module is activated.
  2515. *
  2516. * @since 2.6.0
  2517. *
  2518. * @param string $module Module slug.
  2519. * @param bool $exit Should we exit after the module has been activated. Default to true.
  2520. * @param bool $redirect Should the user be redirected after module activation? Default to true.
  2521. */
  2522. do_action( 'jetpack_pre_activate_module', $module, $exit, $redirect );
  2523. $jetpack = Jetpack::init();
  2524. if ( ! strlen( $module ) )
  2525. return false;
  2526. if ( ! Jetpack::is_module( $module ) )
  2527. return false;
  2528. // If it's already active, then don't do it again
  2529. $active = Jetpack::get_active_modules();
  2530. foreach ( $active as $act ) {
  2531. if ( $act == $module )
  2532. return true;
  2533. }
  2534. $module_data = Jetpack::get_module( $module );
  2535. if ( ! Jetpack::is_active() ) {
  2536. if ( ! Jetpack::is_development_mode() && ! Jetpack::is_onboarding() )
  2537. return false;
  2538. // If we're not connected but in development mode, make sure the module doesn't require a connection
  2539. if ( Jetpack::is_development_mode() && $module_data['requires_connection'] )
  2540. return false;
  2541. }
  2542. // Check and see if the old plugin is active
  2543. if ( isset( $jetpack->plugins_to_deactivate[ $module ] ) ) {
  2544. // Deactivate the old plugin
  2545. if ( Jetpack_Client_Server::deactivate_plugin( $jetpack->plugins_to_deactivate[ $module ][0], $jetpack->plugins_to_deactivate[ $module ][1] ) ) {
  2546. // If we deactivated the old plugin, remembere that with ::state() and redirect back to this page to activate the module
  2547. // We can't activate the module on this page load since the newly deactivated old plugin is still loaded on this page load.
  2548. Jetpack::state( 'deactivated_plugins', $module );
  2549. wp_safe_redirect( add_query_arg( 'jetpack_restate', 1 ) );
  2550. exit;
  2551. }
  2552. }
  2553. // Protect won't work with mis-configured IPs
  2554. if ( 'protect' === $module ) {
  2555. include_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php';
  2556. if ( ! jetpack_protect_get_ip() ) {
  2557. Jetpack::state( 'message', 'protect_misconfigured_ip' );
  2558. return false;
  2559. }
  2560. }
  2561. if ( ! Jetpack::active_plan_supports( $module ) ) {
  2562. return false;
  2563. }
  2564. // Check the file for fatal errors, a la wp-admin/plugins.php::activate
  2565. Jetpack::state( 'module', $module );
  2566. Jetpack::state( 'error', 'module_activation_failed' ); // we'll override this later if the plugin can be included without fatal error
  2567. Jetpack::catch_errors( true );
  2568. ob_start();
  2569. require Jetpack::get_module_path( $module );
  2570. /** This action is documented in class.jetpack.php */
  2571. do_action( 'jetpack_activate_module', $module );
  2572. $active[] = $module;
  2573. Jetpack::update_active_modules( $active );
  2574. Jetpack::state( 'error', false ); // the override
  2575. ob_end_clean();
  2576. Jetpack::catch_errors( false );
  2577. // A flag for Jump Start so it's not shown again. Only set if it hasn't been yet.
  2578. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
  2579. Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' );
  2580. //Jump start is being dismissed send data to MC Stats
  2581. $jetpack->stat( 'jumpstart', 'manual,'.$module );
  2582. $jetpack->do_stats( 'server_side' );
  2583. }
  2584. if ( $redirect ) {
  2585. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  2586. }
  2587. if ( $exit ) {
  2588. exit;
  2589. }
  2590. return true;
  2591. }
  2592. function activate_module_actions( $module ) {
  2593. _deprecated_function( __METHOD__, 'jetpack-4.2' );
  2594. }
  2595. public static function deactivate_module( $module ) {
  2596. /**
  2597. * Fires when a module is deactivated.
  2598. *
  2599. * @since 1.9.0
  2600. *
  2601. * @param string $module Module slug.
  2602. */
  2603. do_action( 'jetpack_pre_deactivate_module', $module );
  2604. $jetpack = Jetpack::init();
  2605. $active = Jetpack::get_active_modules();
  2606. $new = array_filter( array_diff( $active, (array) $module ) );
  2607. // A flag for Jump Start so it's not shown again.
  2608. if ( 'new_connection' === Jetpack_Options::get_option( 'jumpstart' ) ) {
  2609. Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' );
  2610. //Jump start is being dismissed send data to MC Stats
  2611. $jetpack->stat( 'jumpstart', 'manual,deactivated-'.$module );
  2612. $jetpack->do_stats( 'server_side' );
  2613. }
  2614. return self::update_active_modules( $new );
  2615. }
  2616. public static function enable_module_configurable( $module ) {
  2617. $module = Jetpack::get_module_slug( $module );
  2618. add_filter( 'jetpack_module_configurable_' . $module, '__return_true' );
  2619. }
  2620. public static function module_configuration_url( $module ) {
  2621. $module = Jetpack::get_module_slug( $module );
  2622. return Jetpack::admin_url( array( 'page' => 'jetpack', 'configure' => $module ) );
  2623. }
  2624. public static function module_configuration_load( $module, $method ) {
  2625. $module = Jetpack::get_module_slug( $module );
  2626. add_action( 'jetpack_module_configuration_load_' . $module, $method );
  2627. }
  2628. public static function module_configuration_head( $module, $method ) {
  2629. $module = Jetpack::get_module_slug( $module );
  2630. add_action( 'jetpack_module_configuration_head_' . $module, $method );
  2631. }
  2632. public static function module_configuration_screen( $module, $method ) {
  2633. $module = Jetpack::get_module_slug( $module );
  2634. add_action( 'jetpack_module_configuration_screen_' . $module, $method );
  2635. }
  2636. public static function module_configuration_activation_screen( $module, $method ) {
  2637. $module = Jetpack::get_module_slug( $module );
  2638. add_action( 'display_activate_module_setting_' . $module, $method );
  2639. }
  2640. /* Installation */
  2641. public static function bail_on_activation( $message, $deactivate = true ) {
  2642. ?>
  2643. <!doctype html>
  2644. <html>
  2645. <head>
  2646. <meta charset="<?php bloginfo( 'charset' ); ?>">
  2647. <style>
  2648. * {
  2649. text-align: center;
  2650. margin: 0;
  2651. padding: 0;
  2652. font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
  2653. }
  2654. p {
  2655. margin-top: 1em;
  2656. font-size: 18px;
  2657. }
  2658. </style>
  2659. <body>
  2660. <p><?php echo esc_html( $message ); ?></p>
  2661. </body>
  2662. </html>
  2663. <?php
  2664. if ( $deactivate ) {
  2665. $plugins = get_option( 'active_plugins' );
  2666. $jetpack = plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' );
  2667. $update = false;
  2668. foreach ( $plugins as $i => $plugin ) {
  2669. if ( $plugin === $jetpack ) {
  2670. $plugins[$i] = false;
  2671. $update = true;
  2672. }
  2673. }
  2674. if ( $update ) {
  2675. update_option( 'active_plugins', array_filter( $plugins ) );
  2676. }
  2677. }
  2678. exit;
  2679. }
  2680. /**
  2681. * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
  2682. * @static
  2683. */
  2684. public static function plugin_activation( $network_wide ) {
  2685. Jetpack_Options::update_option( 'activated', 1 );
  2686. if ( version_compare( $GLOBALS['wp_version'], JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
  2687. Jetpack::bail_on_activation( sprintf( __( 'Jetpack requires WordPress version %s or later.', 'jetpack' ), JETPACK__MINIMUM_WP_VERSION ) );
  2688. }
  2689. if ( $network_wide )
  2690. Jetpack::state( 'network_nag', true );
  2691. // For firing one-off events (notices) immediately after activation
  2692. set_transient( 'activated_jetpack', true, .1 * MINUTE_IN_SECONDS );
  2693. update_option( 'jetpack_activation_source', self::get_activation_source( wp_get_referer() ) );
  2694. Jetpack::plugin_initialize();
  2695. }
  2696. public static function get_activation_source( $referer_url ) {
  2697. if ( defined( 'WP_CLI' ) && WP_CLI ) {
  2698. return array( 'wp-cli', null );
  2699. }
  2700. $referer = parse_url( $referer_url );
  2701. $source_type = 'unknown';
  2702. $source_query = null;
  2703. if ( ! is_array( $referer ) ) {
  2704. return array( $source_type, $source_query );
  2705. }
  2706. $plugins_path = parse_url( admin_url( 'plugins.php' ), PHP_URL_PATH );
  2707. $plugins_install_path = parse_url( admin_url( 'plugin-install.php' ), PHP_URL_PATH );// /wp-admin/plugin-install.php
  2708. if ( isset( $referer['query'] ) ) {
  2709. parse_str( $referer['query'], $query_parts );
  2710. } else {
  2711. $query_parts = array();
  2712. }
  2713. if ( $plugins_path === $referer['path'] ) {
  2714. $source_type = 'list';
  2715. } elseif ( $plugins_install_path === $referer['path'] ) {
  2716. $tab = isset( $query_parts['tab'] ) ? $query_parts['tab'] : 'featured';
  2717. switch( $tab ) {
  2718. case 'popular':
  2719. $source_type = 'popular';
  2720. break;
  2721. case 'recommended':
  2722. $source_type = 'recommended';
  2723. break;
  2724. case 'favorites':
  2725. $source_type = 'favorites';
  2726. break;
  2727. case 'search':
  2728. $source_type = 'search-' . ( isset( $query_parts['type'] ) ? $query_parts['type'] : 'term' );
  2729. $source_query = isset( $query_parts['s'] ) ? $query_parts['s'] : null;
  2730. break;
  2731. default:
  2732. $source_type = 'featured';
  2733. }
  2734. }
  2735. return array( $source_type, $source_query );
  2736. }
  2737. /**
  2738. * Runs before bumping version numbers up to a new version
  2739. * @param string $version Version:timestamp
  2740. * @param string $old_version Old Version:timestamp or false if not set yet.
  2741. * @return null [description]
  2742. */
  2743. public static function do_version_bump( $version, $old_version ) {
  2744. if ( ! $old_version ) { // For new sites
  2745. // Setting up jetpack manage
  2746. Jetpack::activate_manage();
  2747. }
  2748. }
  2749. /**
  2750. * Sets the internal version number and activation state.
  2751. * @static
  2752. */
  2753. public static function plugin_initialize() {
  2754. if ( ! Jetpack_Options::get_option( 'activated' ) ) {
  2755. Jetpack_Options::update_option( 'activated', 2 );
  2756. }
  2757. if ( ! Jetpack_Options::get_option( 'version' ) ) {
  2758. $version = $old_version = JETPACK__VERSION . ':' . time();
  2759. /** This action is documented in class.jetpack.php */
  2760. do_action( 'updating_jetpack_version', $version, false );
  2761. Jetpack_Options::update_options( compact( 'version', 'old_version' ) );
  2762. }
  2763. Jetpack::load_modules();
  2764. Jetpack_Options::delete_option( 'do_activate' );
  2765. Jetpack_Options::delete_option( 'dismissed_connection_banner' );
  2766. }
  2767. /**
  2768. * Removes all connection options
  2769. * @static
  2770. */
  2771. public static function plugin_deactivation( ) {
  2772. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  2773. if( is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) {
  2774. Jetpack_Network::init()->deactivate();
  2775. } else {
  2776. Jetpack::disconnect( false );
  2777. //Jetpack_Heartbeat::init()->deactivate();
  2778. }
  2779. }
  2780. /**
  2781. * Disconnects from the Jetpack servers.
  2782. * Forgets all connection details and tells the Jetpack servers to do the same.
  2783. * @static
  2784. */
  2785. public static function disconnect( $update_activated_state = true ) {
  2786. wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
  2787. Jetpack::clean_nonces( true );
  2788. // If the site is in an IDC because sync is not allowed,
  2789. // let's make sure to not disconnect the production site.
  2790. if ( ! self::validate_sync_error_idc_option() ) {
  2791. JetpackTracking::record_user_event( 'disconnect_site', array() );
  2792. Jetpack::load_xml_rpc_client();
  2793. $xml = new Jetpack_IXR_Client();
  2794. $xml->query( 'jetpack.deregister' );
  2795. }
  2796. Jetpack_Options::delete_option(
  2797. array(
  2798. 'blog_token',
  2799. 'user_token',
  2800. 'user_tokens',
  2801. 'master_user',
  2802. 'time_diff',
  2803. 'fallback_no_verify_ssl_certs',
  2804. )
  2805. );
  2806. Jetpack_IDC::clear_all_idc_options();
  2807. Jetpack_Options::delete_raw_option( 'jetpack_secrets' );
  2808. if ( $update_activated_state ) {
  2809. Jetpack_Options::update_option( 'activated', 4 );
  2810. }
  2811. if ( $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ) ) {
  2812. // Check then record unique disconnection if site has never been disconnected previously
  2813. if ( - 1 == $jetpack_unique_connection['disconnected'] ) {
  2814. $jetpack_unique_connection['disconnected'] = 1;
  2815. } else {
  2816. if ( 0 == $jetpack_unique_connection['disconnected'] ) {
  2817. //track unique disconnect
  2818. $jetpack = Jetpack::init();
  2819. $jetpack->stat( 'connections', 'unique-disconnect' );
  2820. $jetpack->do_stats( 'server_side' );
  2821. }
  2822. // increment number of times disconnected
  2823. $jetpack_unique_connection['disconnected'] += 1;
  2824. }
  2825. Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection );
  2826. }
  2827. // Delete cached connected user data
  2828. $transient_key = "jetpack_connected_user_data_" . get_current_user_id();
  2829. delete_transient( $transient_key );
  2830. // Delete all the sync related data. Since it could be taking up space.
  2831. require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php';
  2832. Jetpack_Sync_Sender::get_instance()->uninstall();
  2833. // Disable the Heartbeat cron
  2834. Jetpack_Heartbeat::init()->deactivate();
  2835. }
  2836. /**
  2837. * Unlinks the current user from the linked WordPress.com user
  2838. */
  2839. public static function unlink_user( $user_id = null ) {
  2840. if ( ! $tokens = Jetpack_Options::get_option( 'user_tokens' ) )
  2841. return false;
  2842. $user_id = empty( $user_id ) ? get_current_user_id() : intval( $user_id );
  2843. if ( Jetpack_Options::get_option( 'master_user' ) == $user_id )
  2844. return false;
  2845. if ( ! isset( $tokens[ $user_id ] ) )
  2846. return false;
  2847. Jetpack::load_xml_rpc_client();
  2848. $xml = new Jetpack_IXR_Client( compact( 'user_id' ) );
  2849. $xml->query( 'jetpack.unlink_user', $user_id );
  2850. unset( $tokens[ $user_id ] );
  2851. Jetpack_Options::update_option( 'user_tokens', $tokens );
  2852. /**
  2853. * Fires after the current user has been unlinked from WordPress.com.
  2854. *
  2855. * @since 4.1.0
  2856. *
  2857. * @param int $user_id The current user's ID.
  2858. */
  2859. do_action( 'jetpack_unlinked_user', $user_id );
  2860. return true;
  2861. }
  2862. /**
  2863. * Attempts Jetpack registration. If it fail, a state flag is set: @see ::admin_page_load()
  2864. */
  2865. public static function try_registration() {
  2866. // The user has agreed to the TOS at some point by now.
  2867. Jetpack_Options::update_option( 'tos_agreed', true );
  2868. // Let's get some testing in beta versions and such.
  2869. if ( self::is_development_version() && defined( 'PHP_URL_HOST' ) ) {
  2870. // Before attempting to connect, let's make sure that the domains are viable.
  2871. $domains_to_check = array_unique( array(
  2872. 'siteurl' => parse_url( get_site_url(), PHP_URL_HOST ),
  2873. 'homeurl' => parse_url( get_home_url(), PHP_URL_HOST ),
  2874. ) );
  2875. foreach ( $domains_to_check as $domain ) {
  2876. $result = Jetpack_Data::is_usable_domain( $domain );
  2877. if ( is_wp_error( $result ) ) {
  2878. return $result;
  2879. }
  2880. }
  2881. }
  2882. $result = Jetpack::register();
  2883. // If there was an error with registration and the site was not registered, record this so we can show a message.
  2884. if ( ! $result || is_wp_error( $result ) ) {
  2885. return $result;
  2886. } else {
  2887. return true;
  2888. }
  2889. }
  2890. /**
  2891. * Tracking an internal event log. Try not to put too much chaff in here.
  2892. *
  2893. * [Everyone Loves a Log!](https://www.youtube.com/watch?v=2C7mNr5WMjA)
  2894. */
  2895. public static function log( $code, $data = null ) {
  2896. // only grab the latest 200 entries
  2897. $log = array_slice( Jetpack_Options::get_option( 'log', array() ), -199, 199 );
  2898. // Append our event to the log
  2899. $log_entry = array(
  2900. 'time' => time(),
  2901. 'user_id' => get_current_user_id(),
  2902. 'blog_id' => Jetpack_Options::get_option( 'id' ),
  2903. 'code' => $code,
  2904. );
  2905. // Don't bother storing it unless we've got some.
  2906. if ( ! is_null( $data ) ) {
  2907. $log_entry['data'] = $data;
  2908. }
  2909. $log[] = $log_entry;
  2910. // Try add_option first, to make sure it's not autoloaded.
  2911. // @todo: Add an add_option method to Jetpack_Options
  2912. if ( ! add_option( 'jetpack_log', $log, null, 'no' ) ) {
  2913. Jetpack_Options::update_option( 'log', $log );
  2914. }
  2915. /**
  2916. * Fires when Jetpack logs an internal event.
  2917. *
  2918. * @since 3.0.0
  2919. *
  2920. * @param array $log_entry {
  2921. * Array of details about the log entry.
  2922. *
  2923. * @param string time Time of the event.
  2924. * @param int user_id ID of the user who trigerred the event.
  2925. * @param int blog_id Jetpack Blog ID.
  2926. * @param string code Unique name for the event.
  2927. * @param string data Data about the event.
  2928. * }
  2929. */
  2930. do_action( 'jetpack_log_entry', $log_entry );
  2931. }
  2932. /**
  2933. * Get the internal event log.
  2934. *
  2935. * @param $event (string) - only return the specific log events
  2936. * @param $num (int) - get specific number of latest results, limited to 200
  2937. *
  2938. * @return array of log events || WP_Error for invalid params
  2939. */
  2940. public static function get_log( $event = false, $num = false ) {
  2941. if ( $event && ! is_string( $event ) ) {
  2942. return new WP_Error( __( 'First param must be string or empty', 'jetpack' ) );
  2943. }
  2944. if ( $num && ! is_numeric( $num ) ) {
  2945. return new WP_Error( __( 'Second param must be numeric or empty', 'jetpack' ) );
  2946. }
  2947. $entire_log = Jetpack_Options::get_option( 'log', array() );
  2948. // If nothing set - act as it did before, otherwise let's start customizing the output
  2949. if ( ! $num && ! $event ) {
  2950. return $entire_log;
  2951. } else {
  2952. $entire_log = array_reverse( $entire_log );
  2953. }
  2954. $custom_log_output = array();
  2955. if ( $event ) {
  2956. foreach ( $entire_log as $log_event ) {
  2957. if ( $event == $log_event[ 'code' ] ) {
  2958. $custom_log_output[] = $log_event;
  2959. }
  2960. }
  2961. } else {
  2962. $custom_log_output = $entire_log;
  2963. }
  2964. if ( $num ) {
  2965. $custom_log_output = array_slice( $custom_log_output, 0, $num );
  2966. }
  2967. return $custom_log_output;
  2968. }
  2969. /**
  2970. * Log modification of important settings.
  2971. */
  2972. public static function log_settings_change( $option, $old_value, $value ) {
  2973. switch( $option ) {
  2974. case 'jetpack_sync_non_public_post_stati':
  2975. self::log( $option, $value );
  2976. break;
  2977. }
  2978. }
  2979. /**
  2980. * Return stat data for WPCOM sync
  2981. */
  2982. public static function get_stat_data( $encode = true, $extended = true ) {
  2983. $data = Jetpack_Heartbeat::generate_stats_array();
  2984. if ( $extended ) {
  2985. $additional_data = self::get_additional_stat_data();
  2986. $data = array_merge( $data, $additional_data );
  2987. }
  2988. if ( $encode ) {
  2989. return json_encode( $data );
  2990. }
  2991. return $data;
  2992. }
  2993. /**
  2994. * Get additional stat data to sync to WPCOM
  2995. */
  2996. public static function get_additional_stat_data( $prefix = '' ) {
  2997. $return["{$prefix}themes"] = Jetpack::get_parsed_theme_data();
  2998. $return["{$prefix}plugins-extra"] = Jetpack::get_parsed_plugin_data();
  2999. $return["{$prefix}users"] = (int) Jetpack::get_site_user_count();
  3000. $return["{$prefix}site-count"] = 0;
  3001. if ( function_exists( 'get_blog_count' ) ) {
  3002. $return["{$prefix}site-count"] = get_blog_count();
  3003. }
  3004. return $return;
  3005. }
  3006. private static function get_site_user_count() {
  3007. global $wpdb;
  3008. if ( function_exists( 'wp_is_large_network' ) ) {
  3009. if ( wp_is_large_network( 'users' ) ) {
  3010. return -1; // Not a real value but should tell us that we are dealing with a large network.
  3011. }
  3012. }
  3013. if ( false === ( $user_count = get_transient( 'jetpack_site_user_count' ) ) ) {
  3014. // It wasn't there, so regenerate the data and save the transient
  3015. $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities'" );
  3016. set_transient( 'jetpack_site_user_count', $user_count, DAY_IN_SECONDS );
  3017. }
  3018. return $user_count;
  3019. }
  3020. /* Admin Pages */
  3021. function admin_init() {
  3022. // If the plugin is not connected, display a connect message.
  3023. if (
  3024. // the plugin was auto-activated and needs its candy
  3025. Jetpack_Options::get_option_and_ensure_autoload( 'do_activate', '0' )
  3026. ||
  3027. // the plugin is active, but was never activated. Probably came from a site-wide network activation
  3028. ! Jetpack_Options::get_option( 'activated' )
  3029. ) {
  3030. Jetpack::plugin_initialize();
  3031. }
  3032. if ( ! Jetpack::is_active() && ! Jetpack::is_development_mode() ) {
  3033. Jetpack_Connection_Banner::init();
  3034. } elseif ( false === Jetpack_Options::get_option( 'fallback_no_verify_ssl_certs' ) ) {
  3035. // Upgrade: 1.1 -> 1.1.1
  3036. // Check and see if host can verify the Jetpack servers' SSL certificate
  3037. $args = array();
  3038. Jetpack_Client::_wp_remote_request(
  3039. Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'test' ) ),
  3040. $args,
  3041. true
  3042. );
  3043. } else if ( $this->can_display_jetpack_manage_notice() && ! Jetpack_Options::get_option( 'dismissed_manage_banner' ) ) {
  3044. // Show the notice on the Dashboard only for now
  3045. add_action( 'load-index.php', array( $this, 'prepare_manage_jetpack_notice' ) );
  3046. }
  3047. if ( current_user_can( 'manage_options' ) && 'AUTO' == JETPACK_CLIENT__HTTPS && ! self::permit_ssl() ) {
  3048. add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
  3049. }
  3050. add_action( 'load-plugins.php', array( $this, 'intercept_plugin_error_scrape_init' ) );
  3051. add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) );
  3052. add_filter( 'plugin_action_links_' . plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ), array( $this, 'plugin_action_links' ) );
  3053. if ( Jetpack::is_active() || Jetpack::is_development_mode() ) {
  3054. // Artificially throw errors in certain whitelisted cases during plugin activation
  3055. add_action( 'activate_plugin', array( $this, 'throw_error_on_activate_plugin' ) );
  3056. }
  3057. // Jetpack Manage Activation Screen from .com
  3058. Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) );
  3059. // Add custom column in wp-admin/users.php to show whether user is linked.
  3060. add_filter( 'manage_users_columns', array( $this, 'jetpack_icon_user_connected' ) );
  3061. add_action( 'manage_users_custom_column', array( $this, 'jetpack_show_user_connected_icon' ), 10, 3 );
  3062. add_action( 'admin_print_styles', array( $this, 'jetpack_user_col_style' ) );
  3063. }
  3064. function admin_body_class( $admin_body_class = '' ) {
  3065. $classes = explode( ' ', trim( $admin_body_class ) );
  3066. $classes[] = self::is_active() ? 'jetpack-connected' : 'jetpack-disconnected';
  3067. $admin_body_class = implode( ' ', array_unique( $classes ) );
  3068. return " $admin_body_class ";
  3069. }
  3070. static function add_jetpack_pagestyles( $admin_body_class = '' ) {
  3071. return $admin_body_class . ' jetpack-pagestyles ';
  3072. }
  3073. /**
  3074. * Call this function if you want the Big Jetpack Manage Notice to show up.
  3075. *
  3076. * @return null
  3077. */
  3078. function prepare_manage_jetpack_notice() {
  3079. add_action( 'admin_print_styles', array( $this, 'admin_banner_styles' ) );
  3080. add_action( 'admin_notices', array( $this, 'admin_jetpack_manage_notice' ) );
  3081. }
  3082. function manage_activate_screen() {
  3083. include ( JETPACK__PLUGIN_DIR . 'modules/manage/activate-admin.php' );
  3084. }
  3085. /**
  3086. * Sometimes a plugin can activate without causing errors, but it will cause errors on the next page load.
  3087. * This function artificially throws errors for such cases (whitelisted).
  3088. *
  3089. * @param string $plugin The activated plugin.
  3090. */
  3091. function throw_error_on_activate_plugin( $plugin ) {
  3092. $active_modules = Jetpack::get_active_modules();
  3093. // The Shortlinks module and the Stats plugin conflict, but won't cause errors on activation because of some function_exists() checks.
  3094. if ( function_exists( 'stats_get_api_key' ) && in_array( 'shortlinks', $active_modules ) ) {
  3095. $throw = false;
  3096. // Try and make sure it really was the stats plugin
  3097. if ( ! class_exists( 'ReflectionFunction' ) ) {
  3098. if ( 'stats.php' == basename( $plugin ) ) {
  3099. $throw = true;
  3100. }
  3101. } else {
  3102. $reflection = new ReflectionFunction( 'stats_get_api_key' );
  3103. if ( basename( $plugin ) == basename( $reflection->getFileName() ) ) {
  3104. $throw = true;
  3105. }
  3106. }
  3107. if ( $throw ) {
  3108. trigger_error( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), 'WordPress.com Stats' ), E_USER_ERROR );
  3109. }
  3110. }
  3111. }
  3112. function intercept_plugin_error_scrape_init() {
  3113. add_action( 'check_admin_referer', array( $this, 'intercept_plugin_error_scrape' ), 10, 2 );
  3114. }
  3115. function intercept_plugin_error_scrape( $action, $result ) {
  3116. if ( ! $result ) {
  3117. return;
  3118. }
  3119. foreach ( $this->plugins_to_deactivate as $deactivate_me ) {
  3120. if ( "plugin-activation-error_{$deactivate_me[0]}" == $action ) {
  3121. Jetpack::bail_on_activation( sprintf( __( 'Jetpack contains the most recent version of the old &#8220;%1$s&#8221; plugin.', 'jetpack' ), $deactivate_me[1] ), false );
  3122. }
  3123. }
  3124. }
  3125. function add_remote_request_handlers() {
  3126. add_action( 'wp_ajax_nopriv_jetpack_upload_file', array( $this, 'remote_request_handlers' ) );
  3127. add_action( 'wp_ajax_nopriv_jetpack_update_file', array( $this, 'remote_request_handlers' ) );
  3128. }
  3129. function remote_request_handlers() {
  3130. $action = current_filter();
  3131. switch ( current_filter() ) {
  3132. case 'wp_ajax_nopriv_jetpack_upload_file' :
  3133. $response = $this->upload_handler();
  3134. break;
  3135. case 'wp_ajax_nopriv_jetpack_update_file' :
  3136. $response = $this->upload_handler( true );
  3137. break;
  3138. default :
  3139. $response = new Jetpack_Error( 'unknown_handler', 'Unknown Handler', 400 );
  3140. break;
  3141. }
  3142. if ( ! $response ) {
  3143. $response = new Jetpack_Error( 'unknown_error', 'Unknown Error', 400 );
  3144. }
  3145. if ( is_wp_error( $response ) ) {
  3146. $status_code = $response->get_error_data();
  3147. $error = $response->get_error_code();
  3148. $error_description = $response->get_error_message();
  3149. if ( ! is_int( $status_code ) ) {
  3150. $status_code = 400;
  3151. }
  3152. status_header( $status_code );
  3153. die( json_encode( (object) compact( 'error', 'error_description' ) ) );
  3154. }
  3155. status_header( 200 );
  3156. if ( true === $response ) {
  3157. exit;
  3158. }
  3159. die( json_encode( (object) $response ) );
  3160. }
  3161. /**
  3162. * Uploads a file gotten from the global $_FILES.
  3163. * If `$update_media_item` is true and `post_id` is defined
  3164. * the attachment file of the media item (gotten through of the post_id)
  3165. * will be updated instead of add a new one.
  3166. *
  3167. * @param boolean $update_media_item - update media attachment
  3168. * @return array - An array describing the uploadind files process
  3169. */
  3170. function upload_handler( $update_media_item = false ) {
  3171. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
  3172. return new Jetpack_Error( 405, get_status_header_desc( 405 ), 405 );
  3173. }
  3174. $user = wp_authenticate( '', '' );
  3175. if ( ! $user || is_wp_error( $user ) ) {
  3176. return new Jetpack_Error( 403, get_status_header_desc( 403 ), 403 );
  3177. }
  3178. wp_set_current_user( $user->ID );
  3179. if ( ! current_user_can( 'upload_files' ) ) {
  3180. return new Jetpack_Error( 'cannot_upload_files', 'User does not have permission to upload files', 403 );
  3181. }
  3182. if ( empty( $_FILES ) ) {
  3183. return new Jetpack_Error( 'no_files_uploaded', 'No files were uploaded: nothing to process', 400 );
  3184. }
  3185. foreach ( array_keys( $_FILES ) as $files_key ) {
  3186. if ( ! isset( $_POST["_jetpack_file_hmac_{$files_key}"] ) ) {
  3187. return new Jetpack_Error( 'missing_hmac', 'An HMAC for one or more files is missing', 400 );
  3188. }
  3189. }
  3190. $media_keys = array_keys( $_FILES['media'] );
  3191. $token = Jetpack_Data::get_access_token( get_current_user_id() );
  3192. if ( ! $token || is_wp_error( $token ) ) {
  3193. return new Jetpack_Error( 'unknown_token', 'Unknown Jetpack token', 403 );
  3194. }
  3195. $uploaded_files = array();
  3196. $global_post = isset( $GLOBALS['post'] ) ? $GLOBALS['post'] : null;
  3197. unset( $GLOBALS['post'] );
  3198. foreach ( $_FILES['media']['name'] as $index => $name ) {
  3199. $file = array();
  3200. foreach ( $media_keys as $media_key ) {
  3201. $file[$media_key] = $_FILES['media'][$media_key][$index];
  3202. }
  3203. list( $hmac_provided, $salt ) = explode( ':', $_POST['_jetpack_file_hmac_media'][$index] );
  3204. $hmac_file = hash_hmac_file( 'sha1', $file['tmp_name'], $salt . $token->secret );
  3205. if ( $hmac_provided !== $hmac_file ) {
  3206. $uploaded_files[$index] = (object) array( 'error' => 'invalid_hmac', 'error_description' => 'The corresponding HMAC for this file does not match' );
  3207. continue;
  3208. }
  3209. $_FILES['.jetpack.upload.'] = $file;
  3210. $post_id = isset( $_POST['post_id'][$index] ) ? absint( $_POST['post_id'][$index] ) : 0;
  3211. if ( ! current_user_can( 'edit_post', $post_id ) ) {
  3212. $post_id = 0;
  3213. }
  3214. if ( $update_media_item ) {
  3215. if ( ! isset( $post_id ) || $post_id === 0 ) {
  3216. return new Jetpack_Error( 'invalid_input', 'Media ID must be defined.', 400 );
  3217. }
  3218. $media_array = $_FILES['media'];
  3219. $file_array['name'] = $media_array['name'][0];
  3220. $file_array['type'] = $media_array['type'][0];
  3221. $file_array['tmp_name'] = $media_array['tmp_name'][0];
  3222. $file_array['error'] = $media_array['error'][0];
  3223. $file_array['size'] = $media_array['size'][0];
  3224. $edited_media_item = Jetpack_Media::edit_media_file( $post_id, $file_array );
  3225. if ( is_wp_error( $edited_media_item ) ) {
  3226. return $edited_media_item;
  3227. }
  3228. $response = (object) array(
  3229. 'id' => (string) $post_id,
  3230. 'file' => (string) $edited_media_item->post_title,
  3231. 'url' => (string) wp_get_attachment_url( $post_id ),
  3232. 'type' => (string) $edited_media_item->post_mime_type,
  3233. 'meta' => (array) wp_get_attachment_metadata( $post_id ),
  3234. );
  3235. return (array) array( $response );
  3236. }
  3237. $attachment_id = media_handle_upload(
  3238. '.jetpack.upload.',
  3239. $post_id,
  3240. array(),
  3241. array(
  3242. 'action' => 'jetpack_upload_file',
  3243. )
  3244. );
  3245. if ( ! $attachment_id ) {
  3246. $uploaded_files[$index] = (object) array( 'error' => 'unknown', 'error_description' => 'An unknown problem occurred processing the upload on the Jetpack site' );
  3247. } elseif ( is_wp_error( $attachment_id ) ) {
  3248. $uploaded_files[$index] = (object) array( 'error' => 'attachment_' . $attachment_id->get_error_code(), 'error_description' => $attachment_id->get_error_message() );
  3249. } else {
  3250. $attachment = get_post( $attachment_id );
  3251. $uploaded_files[$index] = (object) array(
  3252. 'id' => (string) $attachment_id,
  3253. 'file' => $attachment->post_title,
  3254. 'url' => wp_get_attachment_url( $attachment_id ),
  3255. 'type' => $attachment->post_mime_type,
  3256. 'meta' => wp_get_attachment_metadata( $attachment_id ),
  3257. );
  3258. // Zip files uploads are not supported unless they are done for installation purposed
  3259. // lets delete them in case something goes wrong in this whole process
  3260. if ( 'application/zip' === $attachment->post_mime_type ) {
  3261. // Schedule a cleanup for 2 hours from now in case of failed install.
  3262. wp_schedule_single_event( time() + 2 * HOUR_IN_SECONDS, 'upgrader_scheduled_cleanup', array( $attachment_id ) );
  3263. }
  3264. }
  3265. }
  3266. if ( ! is_null( $global_post ) ) {
  3267. $GLOBALS['post'] = $global_post;
  3268. }
  3269. return $uploaded_files;
  3270. }
  3271. /**
  3272. * Add help to the Jetpack page
  3273. *
  3274. * @since Jetpack (1.2.3)
  3275. * @return false if not the Jetpack page
  3276. */
  3277. function admin_help() {
  3278. $current_screen = get_current_screen();
  3279. // Overview
  3280. $current_screen->add_help_tab(
  3281. array(
  3282. 'id' => 'home',
  3283. 'title' => __( 'Home', 'jetpack' ),
  3284. 'content' =>
  3285. '<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' .
  3286. '<p>' . __( 'Jetpack supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com.', 'jetpack' ) . '</p>' .
  3287. '<p>' . __( 'On this page, you are able to view the modules available within Jetpack, learn more about them, and activate or deactivate them as needed.', 'jetpack' ) . '</p>',
  3288. )
  3289. );
  3290. // Screen Content
  3291. if ( current_user_can( 'manage_options' ) ) {
  3292. $current_screen->add_help_tab(
  3293. array(
  3294. 'id' => 'settings',
  3295. 'title' => __( 'Settings', 'jetpack' ),
  3296. 'content' =>
  3297. '<p><strong>' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</strong></p>' .
  3298. '<p>' . __( 'You can activate or deactivate individual Jetpack modules to suit your needs.', 'jetpack' ) . '</p>' .
  3299. '<ol>' .
  3300. '<li>' . __( 'Each module has an Activate or Deactivate link so you can toggle one individually.', 'jetpack' ) . '</li>' .
  3301. '<li>' . __( 'Using the checkboxes next to each module, you can select multiple modules to toggle via the Bulk Actions menu at the top of the list.', 'jetpack' ) . '</li>' .
  3302. '</ol>' .
  3303. '<p>' . __( 'Using the tools on the right, you can search for specific modules, filter by module categories or which are active, or change the sorting order.', 'jetpack' ) . '</p>'
  3304. )
  3305. );
  3306. }
  3307. // Help Sidebar
  3308. $current_screen->set_help_sidebar(
  3309. '<p><strong>' . __( 'For more information:', 'jetpack' ) . '</strong></p>' .
  3310. '<p><a href="https://jetpack.com/faq/" target="_blank">' . __( 'Jetpack FAQ', 'jetpack' ) . '</a></p>' .
  3311. '<p><a href="https://jetpack.com/support/" target="_blank">' . __( 'Jetpack Support', 'jetpack' ) . '</a></p>' .
  3312. '<p><a href="' . Jetpack::admin_url( array( 'page' => 'jetpack-debugger' ) ) .'">' . __( 'Jetpack Debugging Center', 'jetpack' ) . '</a></p>'
  3313. );
  3314. }
  3315. function admin_menu_css() {
  3316. wp_enqueue_style( 'jetpack-icons' );
  3317. }
  3318. function admin_menu_order() {
  3319. return true;
  3320. }
  3321. function enqueue_gutenberg_locale() {
  3322. wp_add_inline_script(
  3323. 'wp-i18n',
  3324. 'wp.i18n.setLocaleData( ' . self::get_i18n_data_json() . ', \'jetpack\' );'
  3325. );
  3326. }
  3327. function jetpack_menu_order( $menu_order ) {
  3328. $jp_menu_order = array();
  3329. foreach ( $menu_order as $index => $item ) {
  3330. if ( $item != 'jetpack' ) {
  3331. $jp_menu_order[] = $item;
  3332. }
  3333. if ( $index == 0 ) {
  3334. $jp_menu_order[] = 'jetpack';
  3335. }
  3336. }
  3337. return $jp_menu_order;
  3338. }
  3339. function admin_head() {
  3340. if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) )
  3341. /** This action is documented in class.jetpack-admin-page.php */
  3342. do_action( 'jetpack_module_configuration_head_' . $_GET['configure'] );
  3343. }
  3344. function admin_banner_styles() {
  3345. $min = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
  3346. if ( ! wp_style_is( 'jetpack-dops-style' ) ) {
  3347. wp_register_style(
  3348. 'jetpack-dops-style',
  3349. plugins_url( '_inc/build/admin.dops-style.css', JETPACK__PLUGIN_FILE ),
  3350. array(),
  3351. JETPACK__VERSION
  3352. );
  3353. }
  3354. wp_enqueue_style(
  3355. 'jetpack',
  3356. plugins_url( "css/jetpack-banners{$min}.css", JETPACK__PLUGIN_FILE ),
  3357. array( 'jetpack-dops-style' ),
  3358. JETPACK__VERSION . '-20121016'
  3359. );
  3360. wp_style_add_data( 'jetpack', 'rtl', 'replace' );
  3361. wp_style_add_data( 'jetpack', 'suffix', $min );
  3362. }
  3363. function plugin_action_links( $actions ) {
  3364. $jetpack_home = array( 'jetpack-home' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack' ), __( 'Jetpack', 'jetpack' ) ) );
  3365. if( current_user_can( 'jetpack_manage_modules' ) && ( Jetpack::is_active() || Jetpack::is_development_mode() ) ) {
  3366. return array_merge(
  3367. $jetpack_home,
  3368. array( 'settings' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack#/settings' ), __( 'Settings', 'jetpack' ) ) ),
  3369. array( 'support' => sprintf( '<a href="%s">%s</a>', Jetpack::admin_url( 'page=jetpack-debugger '), __( 'Support', 'jetpack' ) ) ),
  3370. $actions
  3371. );
  3372. }
  3373. return array_merge( $jetpack_home, $actions );
  3374. }
  3375. /**
  3376. * This is the first banner
  3377. * It should be visible only to user that can update the option
  3378. * Are not connected
  3379. *
  3380. * @return null
  3381. */
  3382. function admin_jetpack_manage_notice() {
  3383. $screen = get_current_screen();
  3384. // Don't show the connect notice on the jetpack settings page.
  3385. if ( ! in_array( $screen->base, array( 'dashboard' ) ) || $screen->is_network || $screen->action ) {
  3386. return;
  3387. }
  3388. $opt_out_url = $this->opt_out_jetpack_manage_url();
  3389. $opt_in_url = $this->opt_in_jetpack_manage_url();
  3390. /**
  3391. * I think it would be great to have different wordsing depending on where you are
  3392. * for example if we show the notice on dashboard and a different one if we show it on Plugins screen
  3393. * etc..
  3394. */
  3395. ?>
  3396. <div id="message" class="updated jp-banner">
  3397. <a href="<?php echo esc_url( $opt_out_url ); ?>" class="notice-dismiss" title="<?php esc_attr_e( 'Dismiss this notice', 'jetpack' ); ?>"></a>
  3398. <div class="jp-banner__description-container">
  3399. <h2 class="jp-banner__header"><?php esc_html_e( 'Jetpack Centralized Site Management', 'jetpack' ); ?></h2>
  3400. <p class="jp-banner__description"><?php printf( __( 'Manage multiple Jetpack enabled sites from one single dashboard at wordpress.com. Allows all existing, connected Administrators to modify your site.', 'jetpack' ), 'https://jetpack.com/support/site-management' ); ?></p>
  3401. <p class="jp-banner__button-container">
  3402. <a href="<?php echo esc_url( $opt_in_url ); ?>" class="button button-primary" id="wpcom-connect"><?php _e( 'Activate Jetpack Manage', 'jetpack' ); ?></a>
  3403. <a href="https://jetpack.com/support/site-management" class="button" target="_blank" title="<?php esc_attr_e( 'Learn more about Jetpack Manage on Jetpack.com', 'jetpack' ); ?>"><?php _e( 'Learn more', 'jetpack' ); ?></a>
  3404. </p>
  3405. </div>
  3406. </div>
  3407. <?php
  3408. }
  3409. /**
  3410. * Returns the url that the user clicks to remove the notice for the big banner
  3411. * @return string
  3412. */
  3413. function opt_out_jetpack_manage_url() {
  3414. $referer = '&_wp_http_referer=' . add_query_arg( '_wp_http_referer', null );
  3415. return wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-manage-opt-out' . $referer ), 'jetpack_manage_banner_opt_out' );
  3416. }
  3417. /**
  3418. * Returns the url that the user clicks to opt in to Jetpack Manage
  3419. * @return string
  3420. */
  3421. function opt_in_jetpack_manage_url() {
  3422. return wp_nonce_url( Jetpack::admin_url( 'jetpack-notice=jetpack-manage-opt-in' ), 'jetpack_manage_banner_opt_in' );
  3423. }
  3424. function opt_in_jetpack_manage_notice() {
  3425. ?>
  3426. <div class="wrap">
  3427. <div id="message" class="jetpack-message is-opt-in">
  3428. <?php echo sprintf( __( '<p><a href="%1$s" title="Opt in to WordPress.com Site Management" >Activate Site Management</a> to manage multiple sites from our centralized dashboard at wordpress.com/sites. <a href="%2$s" target="_blank">Learn more</a>.</p><a href="%1$s" class="jp-button">Activate Now</a>', 'jetpack' ), $this->opt_in_jetpack_manage_url(), 'https://jetpack.com/support/site-management' ); ?>
  3429. </div>
  3430. </div>
  3431. <?php
  3432. }
  3433. /**
  3434. * Determines whether to show the notice of not true = display notice
  3435. * @return bool
  3436. */
  3437. function can_display_jetpack_manage_notice() {
  3438. // never display the notice to users that can't do anything about it anyways
  3439. if( ! current_user_can( 'jetpack_manage_modules' ) )
  3440. return false;
  3441. // don't display if we are in development more
  3442. if( Jetpack::is_development_mode() ) {
  3443. return false;
  3444. }
  3445. // don't display if the site is private
  3446. if( ! Jetpack_Options::get_option( 'public' ) )
  3447. return false;
  3448. /**
  3449. * Should the Jetpack Remote Site Management notice be displayed.
  3450. *
  3451. * @since 3.3.0
  3452. *
  3453. * @param bool ! self::is_module_active( 'manage' ) Is the Manage module inactive.
  3454. */
  3455. return apply_filters( 'can_display_jetpack_manage_notice', ! self::is_module_active( 'manage' ) );
  3456. }
  3457. /*
  3458. * Registration flow:
  3459. * 1 - ::admin_page_load() action=register
  3460. * 2 - ::try_registration()
  3461. * 3 - ::register()
  3462. * - Creates jetpack_register option containing two secrets and a timestamp
  3463. * - Calls https://jetpack.wordpress.com/jetpack.register/1/ with
  3464. * siteurl, home, gmt_offset, timezone_string, site_name, secret_1, secret_2, site_lang, timeout, stats_id
  3465. * - That request to jetpack.wordpress.com does not immediately respond. It first makes a request BACK to this site's
  3466. * xmlrpc.php?for=jetpack: RPC method: jetpack.verifyRegistration, Parameters: secret_1
  3467. * - The XML-RPC request verifies secret_1, deletes both secrets and responds with: secret_2
  3468. * - https://jetpack.wordpress.com/jetpack.register/1/ verifies that XML-RPC response (secret_2) then finally responds itself with
  3469. * jetpack_id, jetpack_secret, jetpack_public
  3470. * - ::register() then stores jetpack_options: id => jetpack_id, blog_token => jetpack_secret
  3471. * 4 - redirect to https://wordpress.com/start/jetpack-connect
  3472. * 5 - user logs in with WP.com account
  3473. * 6 - remote request to this site's xmlrpc.php with action remoteAuthorize, Jetpack_XMLRPC_Server->remote_authorize
  3474. * - Jetpack_Client_Server::authorize()
  3475. * - Jetpack_Client_Server::get_token()
  3476. * - GET https://jetpack.wordpress.com/jetpack.token/1/ with
  3477. * client_id, client_secret, grant_type, code, redirect_uri:action=authorize, state, scope, user_email, user_login
  3478. * - which responds with access_token, token_type, scope
  3479. * - Jetpack_Client_Server::authorize() stores jetpack_options: user_token => access_token.$user_id
  3480. * - Jetpack::activate_default_modules()
  3481. * - Deactivates deprecated plugins
  3482. * - Activates all default modules
  3483. * - Responds with either error, or 'connected' for new connection, or 'linked' for additional linked users
  3484. * 7 - For a new connection, user selects a Jetpack plan on wordpress.com
  3485. * 8 - User is redirected back to wp-admin/index.php?page=jetpack with state:message=authorized
  3486. * Done!
  3487. */
  3488. /**
  3489. * Handles the page load events for the Jetpack admin page
  3490. */
  3491. function admin_page_load() {
  3492. $error = false;
  3493. // Make sure we have the right body class to hook stylings for subpages off of.
  3494. add_filter( 'admin_body_class', array( __CLASS__, 'add_jetpack_pagestyles' ) );
  3495. if ( ! empty( $_GET['jetpack_restate'] ) ) {
  3496. // Should only be used in intermediate redirects to preserve state across redirects
  3497. Jetpack::restate();
  3498. }
  3499. if ( isset( $_GET['connect_url_redirect'] ) ) {
  3500. // User clicked in the iframe to link their accounts
  3501. if ( ! Jetpack::is_user_connected() ) {
  3502. $from = ! empty( $_GET['from'] ) ? $_GET['from'] : 'iframe';
  3503. $redirect = ! empty( $_GET['redirect_after_auth'] ) ? $_GET['redirect_after_auth'] : false;
  3504. add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) );
  3505. $connect_url = $this->build_connect_url( true, $redirect, $from );
  3506. remove_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_environments' ) );
  3507. if ( isset( $_GET['notes_iframe'] ) )
  3508. $connect_url .= '&notes_iframe';
  3509. wp_redirect( $connect_url );
  3510. exit;
  3511. } else {
  3512. if ( ! isset( $_GET['calypso_env'] ) ) {
  3513. Jetpack::state( 'message', 'already_authorized' );
  3514. wp_safe_redirect( Jetpack::admin_url() );
  3515. exit;
  3516. } else {
  3517. $connect_url = $this->build_connect_url( true, false, 'iframe' );
  3518. $connect_url .= '&already_authorized=true';
  3519. wp_redirect( $connect_url );
  3520. exit;
  3521. }
  3522. }
  3523. }
  3524. if ( isset( $_GET['action'] ) ) {
  3525. switch ( $_GET['action'] ) {
  3526. case 'authorize':
  3527. if ( Jetpack::is_active() && Jetpack::is_user_connected() ) {
  3528. Jetpack::state( 'message', 'already_authorized' );
  3529. wp_safe_redirect( Jetpack::admin_url() );
  3530. exit;
  3531. }
  3532. Jetpack::log( 'authorize' );
  3533. $client_server = new Jetpack_Client_Server;
  3534. $client_server->client_authorize();
  3535. exit;
  3536. case 'register' :
  3537. if ( ! current_user_can( 'jetpack_connect' ) ) {
  3538. $error = 'cheatin';
  3539. break;
  3540. }
  3541. check_admin_referer( 'jetpack-register' );
  3542. Jetpack::log( 'register' );
  3543. Jetpack::maybe_set_version_option();
  3544. $registered = Jetpack::try_registration();
  3545. if ( is_wp_error( $registered ) ) {
  3546. $error = $registered->get_error_code();
  3547. Jetpack::state( 'error', $error );
  3548. Jetpack::state( 'error', $registered->get_error_message() );
  3549. JetpackTracking::record_user_event( 'jpc_register_fail', array(
  3550. 'error_code' => $error,
  3551. 'error_message' => $registered->get_error_message()
  3552. ) );
  3553. break;
  3554. }
  3555. $from = isset( $_GET['from'] ) ? $_GET['from'] : false;
  3556. $redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : false;
  3557. JetpackTracking::record_user_event( 'jpc_register_success', array(
  3558. 'from' => $from
  3559. ) );
  3560. $url = $this->build_connect_url( true, $redirect, $from );
  3561. if ( ! empty( $_GET['onboarding'] ) ) {
  3562. $url = add_query_arg( 'onboarding', $_GET['onboarding'], $url );
  3563. }
  3564. if ( ! empty( $_GET['auth_approved'] ) && 'true' === $_GET['auth_approved'] ) {
  3565. $url = add_query_arg( 'auth_approved', 'true', $url );
  3566. }
  3567. wp_redirect( $url );
  3568. exit;
  3569. case 'activate' :
  3570. if ( ! current_user_can( 'jetpack_activate_modules' ) ) {
  3571. $error = 'cheatin';
  3572. break;
  3573. }
  3574. $module = stripslashes( $_GET['module'] );
  3575. check_admin_referer( "jetpack_activate-$module" );
  3576. Jetpack::log( 'activate', $module );
  3577. if ( ! Jetpack::activate_module( $module ) ) {
  3578. Jetpack::state( 'error', sprintf( __( 'Could not activate %s', 'jetpack' ), $module ) );
  3579. }
  3580. // The following two lines will rarely happen, as Jetpack::activate_module normally exits at the end.
  3581. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  3582. exit;
  3583. case 'activate_default_modules' :
  3584. check_admin_referer( 'activate_default_modules' );
  3585. Jetpack::log( 'activate_default_modules' );
  3586. Jetpack::restate();
  3587. $min_version = isset( $_GET['min_version'] ) ? $_GET['min_version'] : false;
  3588. $max_version = isset( $_GET['max_version'] ) ? $_GET['max_version'] : false;
  3589. $other_modules = isset( $_GET['other_modules'] ) && is_array( $_GET['other_modules'] ) ? $_GET['other_modules'] : array();
  3590. Jetpack::activate_default_modules( $min_version, $max_version, $other_modules );
  3591. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  3592. exit;
  3593. case 'disconnect' :
  3594. if ( ! current_user_can( 'jetpack_disconnect' ) ) {
  3595. $error = 'cheatin';
  3596. break;
  3597. }
  3598. check_admin_referer( 'jetpack-disconnect' );
  3599. Jetpack::log( 'disconnect' );
  3600. Jetpack::disconnect();
  3601. wp_safe_redirect( Jetpack::admin_url( 'disconnected=true' ) );
  3602. exit;
  3603. case 'reconnect' :
  3604. if ( ! current_user_can( 'jetpack_reconnect' ) ) {
  3605. $error = 'cheatin';
  3606. break;
  3607. }
  3608. check_admin_referer( 'jetpack-reconnect' );
  3609. Jetpack::log( 'reconnect' );
  3610. $this->disconnect();
  3611. wp_redirect( $this->build_connect_url( true, false, 'reconnect' ) );
  3612. exit;
  3613. case 'deactivate' :
  3614. if ( ! current_user_can( 'jetpack_deactivate_modules' ) ) {
  3615. $error = 'cheatin';
  3616. break;
  3617. }
  3618. $modules = stripslashes( $_GET['module'] );
  3619. check_admin_referer( "jetpack_deactivate-$modules" );
  3620. foreach ( explode( ',', $modules ) as $module ) {
  3621. Jetpack::log( 'deactivate', $module );
  3622. Jetpack::deactivate_module( $module );
  3623. Jetpack::state( 'message', 'module_deactivated' );
  3624. }
  3625. Jetpack::state( 'module', $modules );
  3626. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  3627. exit;
  3628. case 'unlink' :
  3629. $redirect = isset( $_GET['redirect'] ) ? $_GET['redirect'] : '';
  3630. check_admin_referer( 'jetpack-unlink' );
  3631. Jetpack::log( 'unlink' );
  3632. $this->unlink_user();
  3633. Jetpack::state( 'message', 'unlinked' );
  3634. if ( 'sub-unlink' == $redirect ) {
  3635. wp_safe_redirect( admin_url() );
  3636. } else {
  3637. wp_safe_redirect( Jetpack::admin_url( array( 'page' => $redirect ) ) );
  3638. }
  3639. exit;
  3640. case 'onboard' :
  3641. if ( ! current_user_can( 'manage_options' ) ) {
  3642. wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) );
  3643. } else {
  3644. Jetpack::create_onboarding_token();
  3645. $url = $this->build_connect_url( true );
  3646. if ( false !== ( $token = Jetpack_Options::get_option( 'onboarding' ) ) ) {
  3647. $url = add_query_arg( 'onboarding', $token, $url );
  3648. }
  3649. $calypso_env = ! empty( $_GET[ 'calypso_env' ] ) ? $_GET[ 'calypso_env' ] : false;
  3650. if ( $calypso_env ) {
  3651. $url = add_query_arg( 'calypso_env', $calypso_env, $url );
  3652. }
  3653. wp_redirect( $url );
  3654. exit;
  3655. }
  3656. exit;
  3657. default:
  3658. /**
  3659. * Fires when a Jetpack admin page is loaded with an unrecognized parameter.
  3660. *
  3661. * @since 2.6.0
  3662. *
  3663. * @param string sanitize_key( $_GET['action'] ) Unrecognized URL parameter.
  3664. */
  3665. do_action( 'jetpack_unrecognized_action', sanitize_key( $_GET['action'] ) );
  3666. }
  3667. }
  3668. if ( ! $error = $error ? $error : Jetpack::state( 'error' ) ) {
  3669. self::activate_new_modules( true );
  3670. }
  3671. $message_code = Jetpack::state( 'message' );
  3672. if ( Jetpack::state( 'optin-manage' ) ) {
  3673. $activated_manage = $message_code;
  3674. $message_code = 'jetpack-manage';
  3675. }
  3676. switch ( $message_code ) {
  3677. case 'jetpack-manage':
  3678. $this->message = '<strong>' . sprintf( __( 'You are all set! Your site can now be managed from <a href="%s" target="_blank">wordpress.com/sites</a>.', 'jetpack' ), 'https://wordpress.com/sites' ) . '</strong>';
  3679. if ( $activated_manage ) {
  3680. $this->message .= '<br /><strong>' . __( 'Manage has been activated for you!', 'jetpack' ) . '</strong>';
  3681. }
  3682. break;
  3683. }
  3684. $deactivated_plugins = Jetpack::state( 'deactivated_plugins' );
  3685. if ( ! empty( $deactivated_plugins ) ) {
  3686. $deactivated_plugins = explode( ',', $deactivated_plugins );
  3687. $deactivated_titles = array();
  3688. foreach ( $deactivated_plugins as $deactivated_plugin ) {
  3689. if ( ! isset( $this->plugins_to_deactivate[$deactivated_plugin] ) ) {
  3690. continue;
  3691. }
  3692. $deactivated_titles[] = '<strong>' . str_replace( ' ', '&nbsp;', $this->plugins_to_deactivate[$deactivated_plugin][1] ) . '</strong>';
  3693. }
  3694. if ( $deactivated_titles ) {
  3695. if ( $this->message ) {
  3696. $this->message .= "<br /><br />\n";
  3697. }
  3698. $this->message .= wp_sprintf(
  3699. _n(
  3700. 'Jetpack contains the most recent version of the old %l plugin.',
  3701. 'Jetpack contains the most recent versions of the old %l plugins.',
  3702. count( $deactivated_titles ),
  3703. 'jetpack'
  3704. ),
  3705. $deactivated_titles
  3706. );
  3707. $this->message .= "<br />\n";
  3708. $this->message .= _n(
  3709. 'The old version has been deactivated and can be removed from your site.',
  3710. 'The old versions have been deactivated and can be removed from your site.',
  3711. count( $deactivated_titles ),
  3712. 'jetpack'
  3713. );
  3714. }
  3715. }
  3716. $this->privacy_checks = Jetpack::state( 'privacy_checks' );
  3717. if ( $this->message || $this->error || $this->privacy_checks || $this->can_display_jetpack_manage_notice() ) {
  3718. add_action( 'jetpack_notices', array( $this, 'admin_notices' ) );
  3719. }
  3720. if ( isset( $_GET['configure'] ) && Jetpack::is_module( $_GET['configure'] ) && current_user_can( 'manage_options' ) ) {
  3721. /**
  3722. * Fires when a module configuration page is loaded.
  3723. * The dynamic part of the hook is the configure parameter from the URL.
  3724. *
  3725. * @since 1.1.0
  3726. */
  3727. do_action( 'jetpack_module_configuration_load_' . $_GET['configure'] );
  3728. }
  3729. add_filter( 'jetpack_short_module_description', 'wptexturize' );
  3730. }
  3731. function admin_notices() {
  3732. if ( $this->error ) {
  3733. ?>
  3734. <div id="message" class="jetpack-message jetpack-err">
  3735. <div class="squeezer">
  3736. <h2><?php echo wp_kses( $this->error, array( 'a' => array( 'href' => array() ), 'small' => true, 'code' => true, 'strong' => true, 'br' => true, 'b' => true ) ); ?></h2>
  3737. <?php if ( $desc = Jetpack::state( 'error_description' ) ) : ?>
  3738. <p><?php echo esc_html( stripslashes( $desc ) ); ?></p>
  3739. <?php endif; ?>
  3740. </div>
  3741. </div>
  3742. <?php
  3743. }
  3744. if ( $this->message ) {
  3745. ?>
  3746. <div id="message" class="jetpack-message">
  3747. <div class="squeezer">
  3748. <h2><?php echo wp_kses( $this->message, array( 'strong' => array(), 'a' => array( 'href' => true ), 'br' => true ) ); ?></h2>
  3749. </div>
  3750. </div>
  3751. <?php
  3752. }
  3753. if ( $this->privacy_checks ) :
  3754. $module_names = $module_slugs = array();
  3755. $privacy_checks = explode( ',', $this->privacy_checks );
  3756. $privacy_checks = array_filter( $privacy_checks, array( 'Jetpack', 'is_module' ) );
  3757. foreach ( $privacy_checks as $module_slug ) {
  3758. $module = Jetpack::get_module( $module_slug );
  3759. if ( ! $module ) {
  3760. continue;
  3761. }
  3762. $module_slugs[] = $module_slug;
  3763. $module_names[] = "<strong>{$module['name']}</strong>";
  3764. }
  3765. $module_slugs = join( ',', $module_slugs );
  3766. ?>
  3767. <div id="message" class="jetpack-message jetpack-err">
  3768. <div class="squeezer">
  3769. <h2><strong><?php esc_html_e( 'Is this site private?', 'jetpack' ); ?></strong></h2><br />
  3770. <p><?php
  3771. echo wp_kses(
  3772. wptexturize(
  3773. wp_sprintf(
  3774. _nx(
  3775. "Like your site's RSS feeds, %l allows access to your posts and other content to third parties.",
  3776. "Like your site's RSS feeds, %l allow access to your posts and other content to third parties.",
  3777. count( $privacy_checks ),
  3778. '%l = list of Jetpack module/feature names',
  3779. 'jetpack'
  3780. ),
  3781. $module_names
  3782. )
  3783. ),
  3784. array( 'strong' => true )
  3785. );
  3786. echo "\n<br />\n";
  3787. echo wp_kses(
  3788. sprintf(
  3789. _nx(
  3790. 'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating this feature</a>.',
  3791. 'If your site is not publicly accessible, consider <a href="%1$s" title="%2$s">deactivating these features</a>.',
  3792. count( $privacy_checks ),
  3793. '%1$s = deactivation URL, %2$s = "Deactivate {list of Jetpack module/feature names}',
  3794. 'jetpack'
  3795. ),
  3796. wp_nonce_url(
  3797. Jetpack::admin_url(
  3798. array(
  3799. 'page' => 'jetpack',
  3800. 'action' => 'deactivate',
  3801. 'module' => urlencode( $module_slugs ),
  3802. )
  3803. ),
  3804. "jetpack_deactivate-$module_slugs"
  3805. ),
  3806. esc_attr( wp_kses( wp_sprintf( _x( 'Deactivate %l', '%l = list of Jetpack module/feature names', 'jetpack' ), $module_names ), array() ) )
  3807. ),
  3808. array( 'a' => array( 'href' => true, 'title' => true ) )
  3809. );
  3810. ?></p>
  3811. </div>
  3812. </div>
  3813. <?php endif;
  3814. // only display the notice if the other stuff is not there
  3815. if( $this->can_display_jetpack_manage_notice() && ! $this->error && ! $this->message && ! $this->privacy_checks ) {
  3816. if( isset( $_GET['page'] ) && 'jetpack' != $_GET['page'] )
  3817. $this->opt_in_jetpack_manage_notice();
  3818. }
  3819. }
  3820. /**
  3821. * Record a stat for later output. This will only currently output in the admin_footer.
  3822. */
  3823. function stat( $group, $detail ) {
  3824. if ( ! isset( $this->stats[ $group ] ) )
  3825. $this->stats[ $group ] = array();
  3826. $this->stats[ $group ][] = $detail;
  3827. }
  3828. /**
  3829. * Load stats pixels. $group is auto-prefixed with "x_jetpack-"
  3830. */
  3831. function do_stats( $method = '' ) {
  3832. if ( is_array( $this->stats ) && count( $this->stats ) ) {
  3833. foreach ( $this->stats as $group => $stats ) {
  3834. if ( is_array( $stats ) && count( $stats ) ) {
  3835. $args = array( "x_jetpack-{$group}" => implode( ',', $stats ) );
  3836. if ( 'server_side' === $method ) {
  3837. self::do_server_side_stat( $args );
  3838. } else {
  3839. echo '<img src="' . esc_url( self::build_stats_url( $args ) ) . '" width="1" height="1" style="display:none;" />';
  3840. }
  3841. }
  3842. unset( $this->stats[ $group ] );
  3843. }
  3844. }
  3845. }
  3846. /**
  3847. * Runs stats code for a one-off, server-side.
  3848. *
  3849. * @param $args array|string The arguments to append to the URL. Should include `x_jetpack-{$group}={$stats}` or whatever we want to store.
  3850. *
  3851. * @return bool If it worked.
  3852. */
  3853. static function do_server_side_stat( $args ) {
  3854. $response = wp_remote_get( esc_url_raw( self::build_stats_url( $args ) ) );
  3855. if ( is_wp_error( $response ) )
  3856. return false;
  3857. if ( 200 !== wp_remote_retrieve_response_code( $response ) )
  3858. return false;
  3859. return true;
  3860. }
  3861. /**
  3862. * Builds the stats url.
  3863. *
  3864. * @param $args array|string The arguments to append to the URL.
  3865. *
  3866. * @return string The URL to be pinged.
  3867. */
  3868. static function build_stats_url( $args ) {
  3869. $defaults = array(
  3870. 'v' => 'wpcom2',
  3871. 'rand' => md5( mt_rand( 0, 999 ) . time() ),
  3872. );
  3873. $args = wp_parse_args( $args, $defaults );
  3874. /**
  3875. * Filter the URL used as the Stats tracking pixel.
  3876. *
  3877. * @since 2.3.2
  3878. *
  3879. * @param string $url Base URL used as the Stats tracking pixel.
  3880. */
  3881. $base_url = apply_filters(
  3882. 'jetpack_stats_base_url',
  3883. 'https://pixel.wp.com/g.gif'
  3884. );
  3885. $url = add_query_arg( $args, $base_url );
  3886. return $url;
  3887. }
  3888. static function translate_current_user_to_role() {
  3889. foreach ( self::$capability_translations as $role => $cap ) {
  3890. if ( current_user_can( $role ) || current_user_can( $cap ) ) {
  3891. return $role;
  3892. }
  3893. }
  3894. return false;
  3895. }
  3896. static function translate_user_to_role( $user ) {
  3897. foreach ( self::$capability_translations as $role => $cap ) {
  3898. if ( user_can( $user, $role ) || user_can( $user, $cap ) ) {
  3899. return $role;
  3900. }
  3901. }
  3902. return false;
  3903. }
  3904. static function translate_role_to_cap( $role ) {
  3905. if ( ! isset( self::$capability_translations[$role] ) ) {
  3906. return false;
  3907. }
  3908. return self::$capability_translations[$role];
  3909. }
  3910. static function sign_role( $role, $user_id = null ) {
  3911. if ( empty( $user_id ) ) {
  3912. $user_id = (int) get_current_user_id();
  3913. }
  3914. if ( ! $user_id ) {
  3915. return false;
  3916. }
  3917. $token = Jetpack_Data::get_access_token();
  3918. if ( ! $token || is_wp_error( $token ) ) {
  3919. return false;
  3920. }
  3921. return $role . ':' . hash_hmac( 'md5', "{$role}|{$user_id}", $token->secret );
  3922. }
  3923. /**
  3924. * Builds a URL to the Jetpack connection auth page
  3925. *
  3926. * @since 3.9.5
  3927. *
  3928. * @param bool $raw If true, URL will not be escaped.
  3929. * @param bool|string $redirect If true, will redirect back to Jetpack wp-admin landing page after connection.
  3930. * If string, will be a custom redirect.
  3931. * @param bool|string $from If not false, adds 'from=$from' param to the connect URL.
  3932. * @param bool $register If true, will generate a register URL regardless of the existing token, since 4.9.0
  3933. *
  3934. * @return string Connect URL
  3935. */
  3936. function build_connect_url( $raw = false, $redirect = false, $from = false, $register = false ) {
  3937. $site_id = Jetpack_Options::get_option( 'id' );
  3938. $token = Jetpack_Options::get_option( 'blog_token' );
  3939. if ( $register || ! $token || ! $site_id ) {
  3940. $url = Jetpack::nonce_url_no_esc( Jetpack::admin_url( 'action=register' ), 'jetpack-register' );
  3941. if ( ! empty( $redirect ) ) {
  3942. $url = add_query_arg(
  3943. 'redirect',
  3944. urlencode( wp_validate_redirect( esc_url_raw( $redirect ) ) ),
  3945. $url
  3946. );
  3947. }
  3948. if( is_network_admin() ) {
  3949. $url = add_query_arg( 'is_multisite', network_admin_url( 'admin.php?page=jetpack-settings' ), $url );
  3950. }
  3951. } else {
  3952. // Let's check the existing blog token to see if we need to re-register. We only check once per minute
  3953. // because otherwise this logic can get us in to a loop.
  3954. $last_connect_url_check = intval( Jetpack_Options::get_raw_option( 'jetpack_last_connect_url_check' ) );
  3955. if ( ! $last_connect_url_check || ( time() - $last_connect_url_check ) > MINUTE_IN_SECONDS ) {
  3956. Jetpack_Options::update_raw_option( 'jetpack_last_connect_url_check', time() );
  3957. $response = Jetpack_Client::wpcom_json_api_request_as_blog(
  3958. sprintf( '/sites/%d', $site_id ) .'?force=wpcom',
  3959. '1.1'
  3960. );
  3961. if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
  3962. // Generating a register URL instead to refresh the existing token
  3963. return $this->build_connect_url( $raw, $redirect, $from, true );
  3964. }
  3965. }
  3966. if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && include_once JETPACK__GLOTPRESS_LOCALES_PATH ) {
  3967. $gp_locale = GP_Locales::by_field( 'wp_locale', get_locale() );
  3968. }
  3969. $role = self::translate_current_user_to_role();
  3970. $signed_role = self::sign_role( $role );
  3971. $user = wp_get_current_user();
  3972. $jetpack_admin_page = esc_url_raw( admin_url( 'admin.php?page=jetpack' ) );
  3973. $redirect = $redirect
  3974. ? wp_validate_redirect( esc_url_raw( $redirect ), $jetpack_admin_page )
  3975. : $jetpack_admin_page;
  3976. if( isset( $_REQUEST['is_multisite'] ) ) {
  3977. $redirect = Jetpack_Network::init()->get_url( 'network_admin_page' );
  3978. }
  3979. $secrets = Jetpack::generate_secrets( 'authorize', false, 2 * HOUR_IN_SECONDS );
  3980. $site_icon = ( function_exists( 'has_site_icon') && has_site_icon() )
  3981. ? get_site_icon_url()
  3982. : false;
  3983. /**
  3984. * Filter the type of authorization.
  3985. * 'calypso' completes authorization on wordpress.com/jetpack/connect
  3986. * while 'jetpack' ( or any other value ) completes the authorization at jetpack.wordpress.com.
  3987. *
  3988. * @since 4.3.3
  3989. *
  3990. * @param string $auth_type Defaults to 'calypso', can also be 'jetpack'.
  3991. */
  3992. $auth_type = apply_filters( 'jetpack_auth_type', 'calypso' );
  3993. $tracks_identity = jetpack_tracks_get_identity( get_current_user_id() );
  3994. $args = urlencode_deep(
  3995. array(
  3996. 'response_type' => 'code',
  3997. 'client_id' => Jetpack_Options::get_option( 'id' ),
  3998. 'redirect_uri' => add_query_arg(
  3999. array(
  4000. 'action' => 'authorize',
  4001. '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
  4002. 'redirect' => urlencode( $redirect ),
  4003. ),
  4004. esc_url( admin_url( 'admin.php?page=jetpack' ) )
  4005. ),
  4006. 'state' => $user->ID,
  4007. 'scope' => $signed_role,
  4008. 'user_email' => $user->user_email,
  4009. 'user_login' => $user->user_login,
  4010. 'is_active' => Jetpack::is_active(),
  4011. 'jp_version' => JETPACK__VERSION,
  4012. 'auth_type' => $auth_type,
  4013. 'secret' => $secrets['secret_1'],
  4014. 'locale' => ( isset( $gp_locale ) && isset( $gp_locale->slug ) ) ? $gp_locale->slug : '',
  4015. 'blogname' => get_option( 'blogname' ),
  4016. 'site_url' => site_url(),
  4017. 'home_url' => home_url(),
  4018. 'site_icon' => $site_icon,
  4019. 'site_lang' => get_locale(),
  4020. '_ui' => $tracks_identity['_ui'],
  4021. '_ut' => $tracks_identity['_ut']
  4022. )
  4023. );
  4024. self::apply_activation_source_to_args( $args );
  4025. $url = add_query_arg( $args, Jetpack::api_url( 'authorize' ) );
  4026. }
  4027. if ( $from ) {
  4028. $url = add_query_arg( 'from', $from, $url );
  4029. }
  4030. if ( isset( $_GET['calypso_env'] ) ) {
  4031. $url = add_query_arg( 'calypso_env', sanitize_key( $_GET['calypso_env'] ), $url );
  4032. }
  4033. return $raw ? $url : esc_url( $url );
  4034. }
  4035. public static function apply_activation_source_to_args( &$args ) {
  4036. list( $activation_source_name, $activation_source_keyword ) = get_option( 'jetpack_activation_source' );
  4037. if ( $activation_source_name ) {
  4038. $args['_as'] = urlencode( $activation_source_name );
  4039. }
  4040. if ( $activation_source_keyword ) {
  4041. $args['_ak'] = urlencode( $activation_source_keyword );
  4042. }
  4043. }
  4044. function build_reconnect_url( $raw = false ) {
  4045. $url = wp_nonce_url( Jetpack::admin_url( 'action=reconnect' ), 'jetpack-reconnect' );
  4046. return $raw ? $url : esc_url( $url );
  4047. }
  4048. public static function admin_url( $args = null ) {
  4049. $args = wp_parse_args( $args, array( 'page' => 'jetpack' ) );
  4050. $url = add_query_arg( $args, admin_url( 'admin.php' ) );
  4051. return $url;
  4052. }
  4053. public static function nonce_url_no_esc( $actionurl, $action = -1, $name = '_wpnonce' ) {
  4054. $actionurl = str_replace( '&amp;', '&', $actionurl );
  4055. return add_query_arg( $name, wp_create_nonce( $action ), $actionurl );
  4056. }
  4057. function dismiss_jetpack_notice() {
  4058. if ( ! isset( $_GET['jetpack-notice'] ) ) {
  4059. return;
  4060. }
  4061. switch( $_GET['jetpack-notice'] ) {
  4062. case 'dismiss':
  4063. if ( check_admin_referer( 'jetpack-deactivate' ) && ! is_plugin_active_for_network( plugin_basename( JETPACK__PLUGIN_DIR . 'jetpack.php' ) ) ) {
  4064. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  4065. deactivate_plugins( JETPACK__PLUGIN_DIR . 'jetpack.php', false, false );
  4066. wp_safe_redirect( admin_url() . 'plugins.php?deactivate=true&plugin_status=all&paged=1&s=' );
  4067. }
  4068. break;
  4069. case 'jetpack-manage-opt-out':
  4070. if ( check_admin_referer( 'jetpack_manage_banner_opt_out' ) ) {
  4071. // Don't show the banner again
  4072. Jetpack_Options::update_option( 'dismissed_manage_banner', true );
  4073. // redirect back to the page that had the notice
  4074. if ( wp_get_referer() ) {
  4075. wp_safe_redirect( wp_get_referer() );
  4076. } else {
  4077. // Take me to Jetpack
  4078. wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) );
  4079. }
  4080. }
  4081. break;
  4082. case 'jetpack-protect-multisite-opt-out':
  4083. if ( check_admin_referer( 'jetpack_protect_multisite_banner_opt_out' ) ) {
  4084. // Don't show the banner again
  4085. update_site_option( 'jetpack_dismissed_protect_multisite_banner', true );
  4086. // redirect back to the page that had the notice
  4087. if ( wp_get_referer() ) {
  4088. wp_safe_redirect( wp_get_referer() );
  4089. } else {
  4090. // Take me to Jetpack
  4091. wp_safe_redirect( admin_url( 'admin.php?page=jetpack' ) );
  4092. }
  4093. }
  4094. break;
  4095. case 'jetpack-manage-opt-in':
  4096. if ( check_admin_referer( 'jetpack_manage_banner_opt_in' ) ) {
  4097. // This makes sure that we are redirect to jetpack home so that we can see the Success Message.
  4098. $redirection_url = Jetpack::admin_url();
  4099. remove_action( 'jetpack_pre_activate_module', array( Jetpack_Admin::init(), 'fix_redirect' ) );
  4100. // Don't redirect form the Jetpack Setting Page
  4101. $referer_parsed = parse_url ( wp_get_referer() );
  4102. // check that we do have a wp_get_referer and the query paramater is set orderwise go to the Jetpack Home
  4103. if ( isset( $referer_parsed['query'] ) && false !== strpos( $referer_parsed['query'], 'page=jetpack_modules' ) ) {
  4104. // Take the user to Jetpack home except when on the setting page
  4105. $redirection_url = wp_get_referer();
  4106. add_action( 'jetpack_pre_activate_module', array( Jetpack_Admin::init(), 'fix_redirect' ) );
  4107. }
  4108. // Also update the JSON API FULL MANAGEMENT Option
  4109. Jetpack::activate_module( 'manage', false, false );
  4110. // Special Message when option in.
  4111. Jetpack::state( 'optin-manage', 'true' );
  4112. // Activate the Module if not activated already
  4113. // Redirect properly
  4114. wp_safe_redirect( $redirection_url );
  4115. }
  4116. break;
  4117. }
  4118. }
  4119. public static function admin_screen_configure_module( $module_id ) {
  4120. // User that doesn't have 'jetpack_configure_modules' will never end up here since Jetpack Landing Page woun't let them.
  4121. if ( ! in_array( $module_id, Jetpack::get_active_modules() ) && current_user_can( 'manage_options' ) ) {
  4122. if ( has_action( 'display_activate_module_setting_' . $module_id ) ) {
  4123. /**
  4124. * Fires to diplay a custom module activation screen.
  4125. *
  4126. * To add a module actionation screen use Jetpack::module_configuration_activation_screen method.
  4127. * Example: Jetpack::module_configuration_activation_screen( 'manage', array( $this, 'manage_activate_screen' ) );
  4128. *
  4129. * @module manage
  4130. *
  4131. * @since 3.8.0
  4132. *
  4133. * @param int $module_id Module ID.
  4134. */
  4135. do_action( 'display_activate_module_setting_' . $module_id );
  4136. } else {
  4137. self::display_activate_module_link( $module_id );
  4138. }
  4139. return false;
  4140. } ?>
  4141. <div id="jp-settings-screen" style="position: relative">
  4142. <h3>
  4143. <?php
  4144. $module = Jetpack::get_module( $module_id );
  4145. echo '<a href="' . Jetpack::admin_url( 'page=jetpack_modules' ) . '">' . __( 'Jetpack by WordPress.com', 'jetpack' ) . '</a> &rarr; ';
  4146. printf( __( 'Configure %s', 'jetpack' ), $module['name'] );
  4147. ?>
  4148. </h3>
  4149. <?php
  4150. /**
  4151. * Fires within the displayed message when a feature configuation is updated.
  4152. *
  4153. * @since 3.4.0
  4154. *
  4155. * @param int $module_id Module ID.
  4156. */
  4157. do_action( 'jetpack_notices_update_settings', $module_id );
  4158. /**
  4159. * Fires when a feature configuation screen is loaded.
  4160. * The dynamic part of the hook, $module_id, is the module ID.
  4161. *
  4162. * @since 1.1.0
  4163. */
  4164. do_action( 'jetpack_module_configuration_screen_' . $module_id );
  4165. ?>
  4166. </div><?php
  4167. }
  4168. /**
  4169. * Display link to activate the module to see the settings screen.
  4170. * @param string $module_id
  4171. * @return null
  4172. */
  4173. public static function display_activate_module_link( $module_id ) {
  4174. $info = Jetpack::get_module( $module_id );
  4175. $extra = '';
  4176. $activate_url = wp_nonce_url(
  4177. Jetpack::admin_url(
  4178. array(
  4179. 'page' => 'jetpack',
  4180. 'action' => 'activate',
  4181. 'module' => $module_id,
  4182. )
  4183. ),
  4184. "jetpack_activate-$module_id"
  4185. );
  4186. ?>
  4187. <div class="wrap configure-module">
  4188. <div id="jp-settings-screen">
  4189. <?php
  4190. if ( $module_id == 'json-api' ) {
  4191. $info['name'] = esc_html__( 'Activate Site Management and JSON API', 'jetpack' );
  4192. $activate_url = Jetpack::init()->opt_in_jetpack_manage_url();
  4193. $info['description'] = sprintf( __( 'Manage your multiple Jetpack sites from our centralized dashboard at wordpress.com/sites. <a href="%s" target="_blank">Learn more</a>.', 'jetpack' ), 'https://jetpack.com/support/site-management' );
  4194. // $extra = __( 'To use Site Management, you need to first activate JSON API to allow remote management of your site. ', 'jetpack' );
  4195. } ?>
  4196. <h3><?php echo esc_html( $info['name'] ); ?></h3>
  4197. <div class="narrow">
  4198. <p><?php echo $info['description']; ?></p>
  4199. <?php if( $extra ) { ?>
  4200. <p><?php echo esc_html( $extra ); ?></p>
  4201. <?php } ?>
  4202. <p>
  4203. <?php
  4204. if( wp_get_referer() ) {
  4205. printf( __( '<a class="button-primary" href="%s">Activate Now</a> or <a href="%s" >return to previous page</a>.', 'jetpack' ) , $activate_url, wp_get_referer() );
  4206. } else {
  4207. printf( __( '<a class="button-primary" href="%s">Activate Now</a>', 'jetpack' ) , $activate_url );
  4208. } ?>
  4209. </p>
  4210. </div>
  4211. </div>
  4212. </div>
  4213. <?php
  4214. }
  4215. public static function sort_modules( $a, $b ) {
  4216. if ( $a['sort'] == $b['sort'] )
  4217. return 0;
  4218. return ( $a['sort'] < $b['sort'] ) ? -1 : 1;
  4219. }
  4220. function ajax_recheck_ssl() {
  4221. check_ajax_referer( 'recheck-ssl', 'ajax-nonce' );
  4222. $result = Jetpack::permit_ssl( true );
  4223. wp_send_json( array(
  4224. 'enabled' => $result,
  4225. 'message' => get_transient( 'jetpack_https_test_message' )
  4226. ) );
  4227. }
  4228. /* Client API */
  4229. /**
  4230. * Returns the requested Jetpack API URL
  4231. *
  4232. * @return string
  4233. */
  4234. public static function api_url( $relative_url ) {
  4235. return trailingslashit( JETPACK__API_BASE . $relative_url ) . JETPACK__API_VERSION . '/';
  4236. }
  4237. /**
  4238. * Some hosts disable the OpenSSL extension and so cannot make outgoing HTTPS requsets
  4239. */
  4240. public static function fix_url_for_bad_hosts( $url ) {
  4241. if ( 0 !== strpos( $url, 'https://' ) ) {
  4242. return $url;
  4243. }
  4244. switch ( JETPACK_CLIENT__HTTPS ) {
  4245. case 'ALWAYS' :
  4246. return $url;
  4247. case 'NEVER' :
  4248. return set_url_scheme( $url, 'http' );
  4249. // default : case 'AUTO' :
  4250. }
  4251. // we now return the unmodified SSL URL by default, as a security precaution
  4252. return $url;
  4253. }
  4254. /**
  4255. * Create a random secret for validating onboarding payload
  4256. *
  4257. * @return string Secret token
  4258. */
  4259. public static function create_onboarding_token() {
  4260. if ( false === ( $token = Jetpack_Options::get_option( 'onboarding' ) ) ) {
  4261. $token = wp_generate_password( 32, false );
  4262. Jetpack_Options::update_option( 'onboarding', $token );
  4263. }
  4264. return $token;
  4265. }
  4266. /**
  4267. * Remove the onboarding token
  4268. *
  4269. * @return bool True on success, false on failure
  4270. */
  4271. public static function invalidate_onboarding_token() {
  4272. return Jetpack_Options::delete_option( 'onboarding' );
  4273. }
  4274. /**
  4275. * Validate an onboarding token for a specific action
  4276. *
  4277. * @return boolean True if token/action pair is accepted, false if not
  4278. */
  4279. public static function validate_onboarding_token_action( $token, $action ) {
  4280. // Compare tokens, bail if tokens do not match
  4281. if ( ! hash_equals( $token, Jetpack_Options::get_option( 'onboarding' ) ) ) { // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  4282. return false;
  4283. }
  4284. // List of valid actions we can take
  4285. $valid_actions = array(
  4286. '/jetpack/v4/settings',
  4287. );
  4288. // Whitelist the action
  4289. if ( ! in_array( $action, $valid_actions ) ) {
  4290. return false;
  4291. }
  4292. return true;
  4293. }
  4294. /**
  4295. * Checks to see if the URL is using SSL to connect with Jetpack
  4296. *
  4297. * @since 2.3.3
  4298. * @return boolean
  4299. */
  4300. public static function permit_ssl( $force_recheck = false ) {
  4301. // Do some fancy tests to see if ssl is being supported
  4302. if ( $force_recheck || false === ( $ssl = get_transient( 'jetpack_https_test' ) ) ) {
  4303. $message = '';
  4304. if ( 'https' !== substr( JETPACK__API_BASE, 0, 5 ) ) {
  4305. $ssl = 0;
  4306. } else {
  4307. switch ( JETPACK_CLIENT__HTTPS ) {
  4308. case 'NEVER':
  4309. $ssl = 0;
  4310. $message = __( 'JETPACK_CLIENT__HTTPS is set to NEVER', 'jetpack' );
  4311. break;
  4312. case 'ALWAYS':
  4313. case 'AUTO':
  4314. default:
  4315. $ssl = 1;
  4316. break;
  4317. }
  4318. // If it's not 'NEVER', test to see
  4319. if ( $ssl ) {
  4320. if ( ! wp_http_supports( array( 'ssl' => true ) ) ) {
  4321. $ssl = 0;
  4322. $message = __( 'WordPress reports no SSL support', 'jetpack' );
  4323. } else {
  4324. $response = wp_remote_get( JETPACK__API_BASE . 'test/1/' );
  4325. if ( is_wp_error( $response ) ) {
  4326. $ssl = 0;
  4327. $message = __( 'WordPress reports no SSL support', 'jetpack' );
  4328. } elseif ( 'OK' !== wp_remote_retrieve_body( $response ) ) {
  4329. $ssl = 0;
  4330. $message = __( 'Response was not OK: ', 'jetpack' ) . wp_remote_retrieve_body( $response );
  4331. }
  4332. }
  4333. }
  4334. }
  4335. set_transient( 'jetpack_https_test', $ssl, DAY_IN_SECONDS );
  4336. set_transient( 'jetpack_https_test_message', $message, DAY_IN_SECONDS );
  4337. }
  4338. return (bool) $ssl;
  4339. }
  4340. /*
  4341. * Displays an admin_notice, alerting the user to their JETPACK_CLIENT__HTTPS constant being 'AUTO' but SSL isn't working.
  4342. */
  4343. public function alert_auto_ssl_fail() {
  4344. if ( ! current_user_can( 'manage_options' ) )
  4345. return;
  4346. $ajax_nonce = wp_create_nonce( 'recheck-ssl' );
  4347. ?>
  4348. <div id="jetpack-ssl-warning" class="error jp-identity-crisis">
  4349. <div class="jp-banner__content">
  4350. <h2><?php _e( 'Outbound HTTPS not working', 'jetpack' ); ?></h2>
  4351. <p><?php _e( 'Your site could not connect to WordPress.com via HTTPS. This could be due to any number of reasons, including faulty SSL certificates, misconfigured or missing SSL libraries, or network issues.', 'jetpack' ); ?></p>
  4352. <p>
  4353. <?php _e( 'Jetpack will re-test for HTTPS support once a day, but you can click here to try again immediately: ', 'jetpack' ); ?>
  4354. <a href="#" id="jetpack-recheck-ssl-button"><?php _e( 'Try again', 'jetpack' ); ?></a>
  4355. <span id="jetpack-recheck-ssl-output"><?php echo get_transient( 'jetpack_https_test_message' ); ?></span>
  4356. </p>
  4357. <p>
  4358. <?php printf( __( 'For more help, try our <a href="%1$s">connection debugger</a> or <a href="%2$s" target="_blank">troubleshooting tips</a>.', 'jetpack' ),
  4359. esc_url( Jetpack::admin_url( array( 'page' => 'jetpack-debugger' ) ) ),
  4360. esc_url( 'https://jetpack.com/support/getting-started-with-jetpack/troubleshooting-tips/' ) ); ?>
  4361. </p>
  4362. </div>
  4363. </div>
  4364. <style>
  4365. #jetpack-recheck-ssl-output { margin-left: 5px; color: red; }
  4366. </style>
  4367. <script type="text/javascript">
  4368. jQuery( document ).ready( function( $ ) {
  4369. $( '#jetpack-recheck-ssl-button' ).click( function( e ) {
  4370. var $this = $( this );
  4371. $this.html( <?php echo json_encode( __( 'Checking', 'jetpack' ) ); ?> );
  4372. $( '#jetpack-recheck-ssl-output' ).html( '' );
  4373. e.preventDefault();
  4374. var data = { action: 'jetpack-recheck-ssl', 'ajax-nonce': '<?php echo $ajax_nonce; ?>' };
  4375. $.post( ajaxurl, data )
  4376. .done( function( response ) {
  4377. if ( response.enabled ) {
  4378. $( '#jetpack-ssl-warning' ).hide();
  4379. } else {
  4380. this.html( <?php echo json_encode( __( 'Try again', 'jetpack' ) ); ?> );
  4381. $( '#jetpack-recheck-ssl-output' ).html( 'SSL Failed: ' + response.message );
  4382. }
  4383. }.bind( $this ) );
  4384. } );
  4385. } );
  4386. </script>
  4387. <?php
  4388. }
  4389. /**
  4390. * Returns the Jetpack XML-RPC API
  4391. *
  4392. * @return string
  4393. */
  4394. public static function xmlrpc_api_url() {
  4395. $base = preg_replace( '#(https?://[^?/]+)(/?.*)?$#', '\\1', JETPACK__API_BASE );
  4396. return untrailingslashit( $base ) . '/xmlrpc.php';
  4397. }
  4398. /**
  4399. * Creates two secret tokens and the end of life timestamp for them.
  4400. *
  4401. * Note these tokens are unique per call, NOT static per site for connecting.
  4402. *
  4403. * @since 2.6
  4404. * @return array
  4405. */
  4406. public static function generate_secrets( $action, $user_id = false, $exp = 600 ) {
  4407. if ( ! $user_id ) {
  4408. $user_id = get_current_user_id();
  4409. }
  4410. $secret_name = 'jetpack_' . $action . '_' . $user_id;
  4411. $secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
  4412. if (
  4413. isset( $secrets[ $secret_name ] ) &&
  4414. $secrets[ $secret_name ]['exp'] > time()
  4415. ) {
  4416. return $secrets[ $secret_name ];
  4417. }
  4418. $secret_value = array(
  4419. 'secret_1' => wp_generate_password( 32, false ),
  4420. 'secret_2' => wp_generate_password( 32, false ),
  4421. 'exp' => time() + $exp,
  4422. );
  4423. $secrets[ $secret_name ] = $secret_value;
  4424. Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets );
  4425. return $secrets[ $secret_name ];
  4426. }
  4427. public static function get_secrets( $action, $user_id ) {
  4428. $secret_name = 'jetpack_' . $action . '_' . $user_id;
  4429. $secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
  4430. if ( ! isset( $secrets[ $secret_name ] ) ) {
  4431. return new WP_Error( 'verify_secrets_missing', 'Verification secrets not found' );
  4432. }
  4433. if ( $secrets[ $secret_name ]['exp'] < time() ) {
  4434. self::delete_secrets( $action, $user_id );
  4435. return new WP_Error( 'verify_secrets_expired', 'Verification took too long' );
  4436. }
  4437. return $secrets[ $secret_name ];
  4438. }
  4439. public static function delete_secrets( $action, $user_id ) {
  4440. $secret_name = 'jetpack_' . $action . '_' . $user_id;
  4441. $secrets = Jetpack_Options::get_raw_option( 'jetpack_secrets', array() );
  4442. if ( isset( $secrets[ $secret_name ] ) ) {
  4443. unset( $secrets[ $secret_name ] );
  4444. Jetpack_Options::update_raw_option( 'jetpack_secrets', $secrets );
  4445. }
  4446. }
  4447. /**
  4448. * Builds the timeout limit for queries talking with the wpcom servers.
  4449. *
  4450. * Based on local php max_execution_time in php.ini
  4451. *
  4452. * @since 2.6
  4453. * @return int
  4454. * @deprecated
  4455. **/
  4456. public function get_remote_query_timeout_limit() {
  4457. _deprecated_function( __METHOD__, 'jetpack-5.4' );
  4458. return Jetpack::get_max_execution_time();
  4459. }
  4460. /**
  4461. * Builds the timeout limit for queries talking with the wpcom servers.
  4462. *
  4463. * Based on local php max_execution_time in php.ini
  4464. *
  4465. * @since 5.4
  4466. * @return int
  4467. **/
  4468. public static function get_max_execution_time() {
  4469. $timeout = (int) ini_get( 'max_execution_time' );
  4470. // Ensure exec time set in php.ini
  4471. if ( ! $timeout ) {
  4472. $timeout = 30;
  4473. }
  4474. return $timeout;
  4475. }
  4476. /**
  4477. * Sets a minimum request timeout, and returns the current timeout
  4478. *
  4479. * @since 5.4
  4480. **/
  4481. public static function set_min_time_limit( $min_timeout ) {
  4482. $timeout = self::get_max_execution_time();
  4483. if ( $timeout < $min_timeout ) {
  4484. $timeout = $min_timeout;
  4485. set_time_limit( $timeout );
  4486. }
  4487. return $timeout;
  4488. }
  4489. /**
  4490. * Takes the response from the Jetpack register new site endpoint and
  4491. * verifies it worked properly.
  4492. *
  4493. * @since 2.6
  4494. * @return string|Jetpack_Error A JSON object on success or Jetpack_Error on failures
  4495. **/
  4496. public function validate_remote_register_response( $response ) {
  4497. if ( is_wp_error( $response ) ) {
  4498. return new Jetpack_Error( 'register_http_request_failed', $response->get_error_message() );
  4499. }
  4500. $code = wp_remote_retrieve_response_code( $response );
  4501. $entity = wp_remote_retrieve_body( $response );
  4502. if ( $entity )
  4503. $registration_response = json_decode( $entity );
  4504. else
  4505. $registration_response = false;
  4506. $code_type = intval( $code / 100 );
  4507. if ( 5 == $code_type ) {
  4508. return new Jetpack_Error( 'wpcom_5??', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code );
  4509. } elseif ( 408 == $code ) {
  4510. return new Jetpack_Error( 'wpcom_408', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code );
  4511. } elseif ( ! empty( $registration_response->error ) ) {
  4512. if ( 'xml_rpc-32700' == $registration_response->error && ! function_exists( 'xml_parser_create' ) ) {
  4513. $error_description = __( "PHP's XML extension is not available. Jetpack requires the XML extension to communicate with WordPress.com. Please contact your hosting provider to enable PHP's XML extension.", 'jetpack' );
  4514. } else {
  4515. $error_description = isset( $registration_response->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $registration_response->error_description ) : '';
  4516. }
  4517. return new Jetpack_Error( (string) $registration_response->error, $error_description, $code );
  4518. } elseif ( 200 != $code ) {
  4519. return new Jetpack_Error( 'wpcom_bad_response', sprintf( __( 'Error Details: %s', 'jetpack' ), $code ), $code );
  4520. }
  4521. // Jetpack ID error block
  4522. if ( empty( $registration_response->jetpack_id ) ) {
  4523. return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is empty. Do not publicly post this error message! %s', 'jetpack' ), $entity ), $entity );
  4524. } elseif ( ! is_scalar( $registration_response->jetpack_id ) ) {
  4525. return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID is not a scalar. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity );
  4526. } elseif ( preg_match( '/[^0-9]/', $registration_response->jetpack_id ) ) {
  4527. return new Jetpack_Error( 'jetpack_id', sprintf( __( 'Error Details: Jetpack ID begins with a numeral. Do not publicly post this error message! %s', 'jetpack' ) , $entity ), $entity );
  4528. }
  4529. return $registration_response;
  4530. }
  4531. /**
  4532. * @return bool|WP_Error
  4533. */
  4534. public static function register() {
  4535. JetpackTracking::record_user_event( 'jpc_register_begin' );
  4536. add_action( 'pre_update_jetpack_option_register', array( 'Jetpack_Options', 'delete_option' ) );
  4537. $secrets = Jetpack::generate_secrets( 'register' );
  4538. if (
  4539. empty( $secrets['secret_1'] ) ||
  4540. empty( $secrets['secret_2'] ) ||
  4541. empty( $secrets['exp'] )
  4542. ) {
  4543. return new Jetpack_Error( 'missing_secrets' );
  4544. }
  4545. // better to try (and fail) to set a higher timeout than this system
  4546. // supports than to have register fail for more users than it should
  4547. $timeout = Jetpack::set_min_time_limit( 60 ) / 2;
  4548. $gmt_offset = get_option( 'gmt_offset' );
  4549. if ( ! $gmt_offset ) {
  4550. $gmt_offset = 0;
  4551. }
  4552. $stats_options = get_option( 'stats_options' );
  4553. $stats_id = isset($stats_options['blog_id']) ? $stats_options['blog_id'] : null;
  4554. $tracks_identity = jetpack_tracks_get_identity( get_current_user_id() );
  4555. $args = array(
  4556. 'method' => 'POST',
  4557. 'body' => array(
  4558. 'siteurl' => site_url(),
  4559. 'home' => home_url(),
  4560. 'gmt_offset' => $gmt_offset,
  4561. 'timezone_string' => (string) get_option( 'timezone_string' ),
  4562. 'site_name' => (string) get_option( 'blogname' ),
  4563. 'secret_1' => $secrets['secret_1'],
  4564. 'secret_2' => $secrets['secret_2'],
  4565. 'site_lang' => get_locale(),
  4566. 'timeout' => $timeout,
  4567. 'stats_id' => $stats_id,
  4568. 'state' => get_current_user_id(),
  4569. '_ui' => $tracks_identity['_ui'],
  4570. '_ut' => $tracks_identity['_ut'],
  4571. 'jetpack_version' => JETPACK__VERSION
  4572. ),
  4573. 'headers' => array(
  4574. 'Accept' => 'application/json',
  4575. ),
  4576. 'timeout' => $timeout,
  4577. );
  4578. self::apply_activation_source_to_args( $args['body'] );
  4579. $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'register' ) ), $args, true );
  4580. // Make sure the response is valid and does not contain any Jetpack errors
  4581. $registration_details = Jetpack::init()->validate_remote_register_response( $response );
  4582. if ( is_wp_error( $registration_details ) ) {
  4583. return $registration_details;
  4584. } elseif ( ! $registration_details ) {
  4585. return new Jetpack_Error( 'unknown_error', __( 'Unknown error registering your Jetpack site', 'jetpack' ), wp_remote_retrieve_response_code( $response ) );
  4586. }
  4587. if ( empty( $registration_details->jetpack_secret ) || ! is_string( $registration_details->jetpack_secret ) ) {
  4588. return new Jetpack_Error( 'jetpack_secret', '', wp_remote_retrieve_response_code( $response ) );
  4589. }
  4590. if ( isset( $registration_details->jetpack_public ) ) {
  4591. $jetpack_public = (int) $registration_details->jetpack_public;
  4592. } else {
  4593. $jetpack_public = false;
  4594. }
  4595. Jetpack_Options::update_options(
  4596. array(
  4597. 'id' => (int) $registration_details->jetpack_id,
  4598. 'blog_token' => (string) $registration_details->jetpack_secret,
  4599. 'public' => $jetpack_public,
  4600. )
  4601. );
  4602. /**
  4603. * Fires when a site is registered on WordPress.com.
  4604. *
  4605. * @since 3.7.0
  4606. *
  4607. * @param int $json->jetpack_id Jetpack Blog ID.
  4608. * @param string $json->jetpack_secret Jetpack Blog Token.
  4609. * @param int|bool $jetpack_public Is the site public.
  4610. */
  4611. do_action( 'jetpack_site_registered', $registration_details->jetpack_id, $registration_details->jetpack_secret, $jetpack_public );
  4612. // Initialize Jump Start for the first and only time.
  4613. if ( ! Jetpack_Options::get_option( 'jumpstart' ) ) {
  4614. Jetpack_Options::update_option( 'jumpstart', 'new_connection' );
  4615. $jetpack = Jetpack::init();
  4616. $jetpack->stat( 'jumpstart', 'unique-views' );
  4617. $jetpack->do_stats( 'server_side' );
  4618. };
  4619. return true;
  4620. }
  4621. /**
  4622. * If the db version is showing something other that what we've got now, bump it to current.
  4623. *
  4624. * @return bool: True if the option was incorrect and updated, false if nothing happened.
  4625. */
  4626. public static function maybe_set_version_option() {
  4627. list( $version ) = explode( ':', Jetpack_Options::get_option( 'version' ) );
  4628. if ( JETPACK__VERSION != $version ) {
  4629. Jetpack_Options::update_option( 'version', JETPACK__VERSION . ':' . time() );
  4630. if ( version_compare( JETPACK__VERSION, $version, '>' ) ) {
  4631. /** This action is documented in class.jetpack.php */
  4632. do_action( 'updating_jetpack_version', JETPACK__VERSION, $version );
  4633. }
  4634. return true;
  4635. }
  4636. return false;
  4637. }
  4638. /* Client Server API */
  4639. /**
  4640. * Loads the Jetpack XML-RPC client
  4641. */
  4642. public static function load_xml_rpc_client() {
  4643. require_once ABSPATH . WPINC . '/class-IXR.php';
  4644. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-ixr-client.php';
  4645. }
  4646. /**
  4647. * Resets the saved authentication state in between testing requests.
  4648. */
  4649. public function reset_saved_auth_state() {
  4650. $this->xmlrpc_verification = null;
  4651. $this->rest_authentication_status = null;
  4652. }
  4653. function verify_xml_rpc_signature() {
  4654. if ( $this->xmlrpc_verification ) {
  4655. return $this->xmlrpc_verification;
  4656. }
  4657. // It's not for us
  4658. if ( ! isset( $_GET['token'] ) || empty( $_GET['signature'] ) ) {
  4659. return false;
  4660. }
  4661. @list( $token_key, $version, $user_id ) = explode( ':', $_GET['token'] );
  4662. if (
  4663. empty( $token_key )
  4664. ||
  4665. empty( $version ) || strval( JETPACK__API_VERSION ) !== $version
  4666. ) {
  4667. return false;
  4668. }
  4669. if ( '0' === $user_id ) {
  4670. $token_type = 'blog';
  4671. $user_id = 0;
  4672. } else {
  4673. $token_type = 'user';
  4674. if ( empty( $user_id ) || ! ctype_digit( $user_id ) ) {
  4675. return false;
  4676. }
  4677. $user_id = (int) $user_id;
  4678. $user = new WP_User( $user_id );
  4679. if ( ! $user || ! $user->exists() ) {
  4680. return false;
  4681. }
  4682. }
  4683. $token = Jetpack_Data::get_access_token( $user_id );
  4684. if ( ! $token ) {
  4685. return false;
  4686. }
  4687. $token_check = "$token_key.";
  4688. if ( ! hash_equals( substr( $token->secret, 0, strlen( $token_check ) ), $token_check ) ) { // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  4689. return false;
  4690. }
  4691. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php';
  4692. $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
  4693. if ( isset( $_POST['_jetpack_is_multipart'] ) ) {
  4694. $post_data = $_POST;
  4695. $file_hashes = array();
  4696. foreach ( $post_data as $post_data_key => $post_data_value ) {
  4697. if ( 0 !== strpos( $post_data_key, '_jetpack_file_hmac_' ) ) {
  4698. continue;
  4699. }
  4700. $post_data_key = substr( $post_data_key, strlen( '_jetpack_file_hmac_' ) );
  4701. $file_hashes[$post_data_key] = $post_data_value;
  4702. }
  4703. foreach ( $file_hashes as $post_data_key => $post_data_value ) {
  4704. unset( $post_data["_jetpack_file_hmac_{$post_data_key}"] );
  4705. $post_data[$post_data_key] = $post_data_value;
  4706. }
  4707. ksort( $post_data );
  4708. $body = http_build_query( stripslashes_deep( $post_data ) );
  4709. } elseif ( is_null( $this->HTTP_RAW_POST_DATA ) ) {
  4710. $body = file_get_contents( 'php://input' );
  4711. } else {
  4712. $body = null;
  4713. }
  4714. $signature = $jetpack_signature->sign_current_request(
  4715. array( 'body' => is_null( $body ) ? $this->HTTP_RAW_POST_DATA : $body, )
  4716. );
  4717. if ( ! $signature ) {
  4718. return false;
  4719. } else if ( is_wp_error( $signature ) ) {
  4720. return $signature;
  4721. } else if ( ! hash_equals( $signature, $_GET['signature'] ) ) { // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  4722. return false;
  4723. }
  4724. $timestamp = (int) $_GET['timestamp'];
  4725. $nonce = stripslashes( (string) $_GET['nonce'] );
  4726. if ( ! $this->add_nonce( $timestamp, $nonce ) ) {
  4727. return false;
  4728. }
  4729. // Let's see if this is onboarding. In such case, use user token type and the provided user id.
  4730. if ( isset( $this->HTTP_RAW_POST_DATA ) || ! empty( $_GET['onboarding'] ) ) {
  4731. if ( ! empty( $_GET['onboarding'] ) ) {
  4732. $jpo = $_GET;
  4733. } else {
  4734. $jpo = json_decode( $this->HTTP_RAW_POST_DATA, true );
  4735. }
  4736. $jpo_token = ! empty( $jpo['onboarding']['token'] ) ? $jpo['onboarding']['token'] : null;
  4737. $jpo_user = ! empty( $jpo['onboarding']['jpUser'] ) ? $jpo['onboarding']['jpUser'] : null;
  4738. if (
  4739. isset( $jpo_user ) && isset( $jpo_token ) &&
  4740. is_email( $jpo_user ) && ctype_alnum( $jpo_token ) &&
  4741. isset( $_GET['rest_route'] ) &&
  4742. self::validate_onboarding_token_action( $jpo_token, $_GET['rest_route'] )
  4743. ) {
  4744. $jpUser = get_user_by( 'email', $jpo_user );
  4745. if ( is_a( $jpUser, 'WP_User' ) ) {
  4746. wp_set_current_user( $jpUser->ID );
  4747. $user_can = is_multisite()
  4748. ? current_user_can_for_blog( get_current_blog_id(), 'manage_options' )
  4749. : current_user_can( 'manage_options' );
  4750. if ( $user_can ) {
  4751. $token_type = 'user';
  4752. $token->external_user_id = $jpUser->ID;
  4753. }
  4754. }
  4755. }
  4756. }
  4757. $this->xmlrpc_verification = array(
  4758. 'type' => $token_type,
  4759. 'user_id' => $token->external_user_id,
  4760. );
  4761. return $this->xmlrpc_verification;
  4762. }
  4763. /**
  4764. * Authenticates XML-RPC and other requests from the Jetpack Server
  4765. */
  4766. function authenticate_jetpack( $user, $username, $password ) {
  4767. if ( is_a( $user, 'WP_User' ) ) {
  4768. return $user;
  4769. }
  4770. $token_details = $this->verify_xml_rpc_signature();
  4771. if ( ! $token_details || is_wp_error( $token_details ) ) {
  4772. return $user;
  4773. }
  4774. if ( 'user' !== $token_details['type'] ) {
  4775. return $user;
  4776. }
  4777. if ( ! $token_details['user_id'] ) {
  4778. return $user;
  4779. }
  4780. nocache_headers();
  4781. return new WP_User( $token_details['user_id'] );
  4782. }
  4783. // Authenticates requests from Jetpack server to WP REST API endpoints.
  4784. // Uses the existing XMLRPC request signing implementation.
  4785. function wp_rest_authenticate( $user ) {
  4786. if ( ! empty( $user ) ) {
  4787. // Another authentication method is in effect.
  4788. return $user;
  4789. }
  4790. if ( ! isset( $_GET['_for'] ) || $_GET['_for'] !== 'jetpack' ) {
  4791. // Nothing to do for this authentication method.
  4792. return null;
  4793. }
  4794. if ( ! isset( $_GET['token'] ) && ! isset( $_GET['signature'] ) ) {
  4795. // Nothing to do for this authentication method.
  4796. return null;
  4797. }
  4798. // Ensure that we always have the request body available. At this
  4799. // point, the WP REST API code to determine the request body has not
  4800. // run yet. That code may try to read from 'php://input' later, but
  4801. // this can only be done once per request in PHP versions prior to 5.6.
  4802. // So we will go ahead and perform this read now if needed, and save
  4803. // the request body where both the Jetpack signature verification code
  4804. // and the WP REST API code can see it.
  4805. if ( ! isset( $GLOBALS['HTTP_RAW_POST_DATA'] ) ) {
  4806. $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents( 'php://input' );
  4807. }
  4808. $this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA'];
  4809. // Only support specific request parameters that have been tested and
  4810. // are known to work with signature verification. A different method
  4811. // can be passed to the WP REST API via the '?_method=' parameter if
  4812. // needed.
  4813. if ( $_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
  4814. $this->rest_authentication_status = new WP_Error(
  4815. 'rest_invalid_request',
  4816. __( 'This request method is not supported.', 'jetpack' ),
  4817. array( 'status' => 400 )
  4818. );
  4819. return null;
  4820. }
  4821. if ( $_SERVER['REQUEST_METHOD'] !== 'POST' && ! empty( $this->HTTP_RAW_POST_DATA ) ) {
  4822. $this->rest_authentication_status = new WP_Error(
  4823. 'rest_invalid_request',
  4824. __( 'This request method does not support body parameters.', 'jetpack' ),
  4825. array( 'status' => 400 )
  4826. );
  4827. return null;
  4828. }
  4829. if ( ! empty( $_SERVER['CONTENT_TYPE'] ) ) {
  4830. $content_type = $_SERVER['CONTENT_TYPE'];
  4831. } elseif ( ! empty( $_SERVER['HTTP_CONTENT_TYPE'] ) ) {
  4832. $content_type = $_SERVER['HTTP_CONTENT_TYPE'];
  4833. }
  4834. if (
  4835. isset( $content_type ) &&
  4836. $content_type !== 'application/x-www-form-urlencoded' &&
  4837. $content_type !== 'application/json'
  4838. ) {
  4839. $this->rest_authentication_status = new WP_Error(
  4840. 'rest_invalid_request',
  4841. __( 'This Content-Type is not supported.', 'jetpack' ),
  4842. array( 'status' => 400 )
  4843. );
  4844. return null;
  4845. }
  4846. $verified = $this->verify_xml_rpc_signature();
  4847. if ( is_wp_error( $verified ) ) {
  4848. $this->rest_authentication_status = $verified;
  4849. return null;
  4850. }
  4851. if (
  4852. $verified &&
  4853. isset( $verified['type'] ) &&
  4854. 'user' === $verified['type'] &&
  4855. ! empty( $verified['user_id'] )
  4856. ) {
  4857. // Authentication successful.
  4858. $this->rest_authentication_status = true;
  4859. return $verified['user_id'];
  4860. }
  4861. // Something else went wrong. Probably a signature error.
  4862. $this->rest_authentication_status = new WP_Error(
  4863. 'rest_invalid_signature',
  4864. __( 'The request is not signed correctly.', 'jetpack' ),
  4865. array( 'status' => 400 )
  4866. );
  4867. return null;
  4868. }
  4869. /**
  4870. * Report authentication status to the WP REST API.
  4871. *
  4872. * @param WP_Error|mixed $result Error from another authentication handler, null if we should handle it, or another value if not
  4873. * @return WP_Error|boolean|null {@see WP_JSON_Server::check_authentication}
  4874. */
  4875. public function wp_rest_authentication_errors( $value ) {
  4876. if ( $value !== null ) {
  4877. return $value;
  4878. }
  4879. return $this->rest_authentication_status;
  4880. }
  4881. function add_nonce( $timestamp, $nonce ) {
  4882. global $wpdb;
  4883. static $nonces_used_this_request = array();
  4884. if ( isset( $nonces_used_this_request["$timestamp:$nonce"] ) ) {
  4885. return $nonces_used_this_request["$timestamp:$nonce"];
  4886. }
  4887. // This should always have gone through Jetpack_Signature::sign_request() first to check $timestamp an $nonce
  4888. $timestamp = (int) $timestamp;
  4889. $nonce = esc_sql( $nonce );
  4890. // Raw query so we can avoid races: add_option will also update
  4891. $show_errors = $wpdb->show_errors( false );
  4892. $old_nonce = $wpdb->get_row(
  4893. $wpdb->prepare( "SELECT * FROM `$wpdb->options` WHERE option_name = %s", "jetpack_nonce_{$timestamp}_{$nonce}" )
  4894. );
  4895. if ( is_null( $old_nonce ) ) {
  4896. $return = $wpdb->query(
  4897. $wpdb->prepare(
  4898. "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s)",
  4899. "jetpack_nonce_{$timestamp}_{$nonce}",
  4900. time(),
  4901. 'no'
  4902. )
  4903. );
  4904. } else {
  4905. $return = false;
  4906. }
  4907. $wpdb->show_errors( $show_errors );
  4908. $nonces_used_this_request["$timestamp:$nonce"] = $return;
  4909. return $return;
  4910. }
  4911. /**
  4912. * In some setups, $HTTP_RAW_POST_DATA can be emptied during some IXR_Server paths since it is passed by reference to various methods.
  4913. * Capture it here so we can verify the signature later.
  4914. */
  4915. function xmlrpc_methods( $methods ) {
  4916. $this->HTTP_RAW_POST_DATA = $GLOBALS['HTTP_RAW_POST_DATA'];
  4917. return $methods;
  4918. }
  4919. function public_xmlrpc_methods( $methods ) {
  4920. if ( array_key_exists( 'wp.getOptions', $methods ) ) {
  4921. $methods['wp.getOptions'] = array( $this, 'jetpack_getOptions' );
  4922. }
  4923. return $methods;
  4924. }
  4925. function jetpack_getOptions( $args ) {
  4926. global $wp_xmlrpc_server;
  4927. $wp_xmlrpc_server->escape( $args );
  4928. $username = $args[1];
  4929. $password = $args[2];
  4930. if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
  4931. return $wp_xmlrpc_server->error;
  4932. }
  4933. $options = array();
  4934. $user_data = $this->get_connected_user_data();
  4935. if ( is_array( $user_data ) ) {
  4936. $options['jetpack_user_id'] = array(
  4937. 'desc' => __( 'The WP.com user ID of the connected user', 'jetpack' ),
  4938. 'readonly' => true,
  4939. 'value' => $user_data['ID'],
  4940. );
  4941. $options['jetpack_user_login'] = array(
  4942. 'desc' => __( 'The WP.com username of the connected user', 'jetpack' ),
  4943. 'readonly' => true,
  4944. 'value' => $user_data['login'],
  4945. );
  4946. $options['jetpack_user_email'] = array(
  4947. 'desc' => __( 'The WP.com user email of the connected user', 'jetpack' ),
  4948. 'readonly' => true,
  4949. 'value' => $user_data['email'],
  4950. );
  4951. $options['jetpack_user_site_count'] = array(
  4952. 'desc' => __( 'The number of sites of the connected WP.com user', 'jetpack' ),
  4953. 'readonly' => true,
  4954. 'value' => $user_data['site_count'],
  4955. );
  4956. }
  4957. $wp_xmlrpc_server->blog_options = array_merge( $wp_xmlrpc_server->blog_options, $options );
  4958. $args = stripslashes_deep( $args );
  4959. return $wp_xmlrpc_server->wp_getOptions( $args );
  4960. }
  4961. function xmlrpc_options( $options ) {
  4962. $jetpack_client_id = false;
  4963. if ( self::is_active() ) {
  4964. $jetpack_client_id = Jetpack_Options::get_option( 'id' );
  4965. }
  4966. $options['jetpack_version'] = array(
  4967. 'desc' => __( 'Jetpack Plugin Version', 'jetpack' ),
  4968. 'readonly' => true,
  4969. 'value' => JETPACK__VERSION,
  4970. );
  4971. $options['jetpack_client_id'] = array(
  4972. 'desc' => __( 'The Client ID/WP.com Blog ID of this site', 'jetpack' ),
  4973. 'readonly' => true,
  4974. 'value' => $jetpack_client_id,
  4975. );
  4976. return $options;
  4977. }
  4978. public static function clean_nonces( $all = false ) {
  4979. global $wpdb;
  4980. $sql = "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE %s";
  4981. $sql_args = array( $wpdb->esc_like( 'jetpack_nonce_' ) . '%' );
  4982. if ( true !== $all ) {
  4983. $sql .= ' AND CAST( `option_value` AS UNSIGNED ) < %d';
  4984. $sql_args[] = time() - 3600;
  4985. }
  4986. $sql .= ' ORDER BY `option_id` LIMIT 100';
  4987. $sql = $wpdb->prepare( $sql, $sql_args );
  4988. for ( $i = 0; $i < 1000; $i++ ) {
  4989. if ( ! $wpdb->query( $sql ) ) {
  4990. break;
  4991. }
  4992. }
  4993. }
  4994. /**
  4995. * State is passed via cookies from one request to the next, but never to subsequent requests.
  4996. * SET: state( $key, $value );
  4997. * GET: $value = state( $key );
  4998. *
  4999. * @param string $key
  5000. * @param string $value
  5001. * @param bool $restate private
  5002. */
  5003. public static function state( $key = null, $value = null, $restate = false ) {
  5004. static $state = array();
  5005. static $path, $domain;
  5006. if ( ! isset( $path ) ) {
  5007. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  5008. $admin_url = Jetpack::admin_url();
  5009. $bits = parse_url( $admin_url );
  5010. if ( is_array( $bits ) ) {
  5011. $path = ( isset( $bits['path'] ) ) ? dirname( $bits['path'] ) : null;
  5012. $domain = ( isset( $bits['host'] ) ) ? $bits['host'] : null;
  5013. } else {
  5014. $path = $domain = null;
  5015. }
  5016. }
  5017. // Extract state from cookies and delete cookies
  5018. if ( isset( $_COOKIE[ 'jetpackState' ] ) && is_array( $_COOKIE[ 'jetpackState' ] ) ) {
  5019. $yum = $_COOKIE[ 'jetpackState' ];
  5020. unset( $_COOKIE[ 'jetpackState' ] );
  5021. foreach ( $yum as $k => $v ) {
  5022. if ( strlen( $v ) )
  5023. $state[ $k ] = $v;
  5024. setcookie( "jetpackState[$k]", false, 0, $path, $domain );
  5025. }
  5026. }
  5027. if ( $restate ) {
  5028. foreach ( $state as $k => $v ) {
  5029. setcookie( "jetpackState[$k]", $v, 0, $path, $domain );
  5030. }
  5031. return;
  5032. }
  5033. // Get a state variable
  5034. if ( isset( $key ) && ! isset( $value ) ) {
  5035. if ( array_key_exists( $key, $state ) )
  5036. return $state[ $key ];
  5037. return null;
  5038. }
  5039. // Set a state variable
  5040. if ( isset ( $key ) && isset( $value ) ) {
  5041. if( is_array( $value ) && isset( $value[0] ) ) {
  5042. $value = $value[0];
  5043. }
  5044. $state[ $key ] = $value;
  5045. setcookie( "jetpackState[$key]", $value, 0, $path, $domain );
  5046. }
  5047. }
  5048. public static function restate() {
  5049. Jetpack::state( null, null, true );
  5050. }
  5051. public static function check_privacy( $file ) {
  5052. static $is_site_publicly_accessible = null;
  5053. if ( is_null( $is_site_publicly_accessible ) ) {
  5054. $is_site_publicly_accessible = false;
  5055. Jetpack::load_xml_rpc_client();
  5056. $rpc = new Jetpack_IXR_Client();
  5057. $success = $rpc->query( 'jetpack.isSitePubliclyAccessible', home_url() );
  5058. if ( $success ) {
  5059. $response = $rpc->getResponse();
  5060. if ( $response ) {
  5061. $is_site_publicly_accessible = true;
  5062. }
  5063. }
  5064. Jetpack_Options::update_option( 'public', (int) $is_site_publicly_accessible );
  5065. }
  5066. if ( $is_site_publicly_accessible ) {
  5067. return;
  5068. }
  5069. $module_slug = self::get_module_slug( $file );
  5070. $privacy_checks = Jetpack::state( 'privacy_checks' );
  5071. if ( ! $privacy_checks ) {
  5072. $privacy_checks = $module_slug;
  5073. } else {
  5074. $privacy_checks .= ",$module_slug";
  5075. }
  5076. Jetpack::state( 'privacy_checks', $privacy_checks );
  5077. }
  5078. /**
  5079. * Helper method for multicall XMLRPC.
  5080. */
  5081. public static function xmlrpc_async_call() {
  5082. global $blog_id;
  5083. static $clients = array();
  5084. $client_blog_id = is_multisite() ? $blog_id : 0;
  5085. if ( ! isset( $clients[$client_blog_id] ) ) {
  5086. Jetpack::load_xml_rpc_client();
  5087. $clients[$client_blog_id] = new Jetpack_IXR_ClientMulticall( array( 'user_id' => JETPACK_MASTER_USER, ) );
  5088. if ( function_exists( 'ignore_user_abort' ) ) {
  5089. ignore_user_abort( true );
  5090. }
  5091. add_action( 'shutdown', array( 'Jetpack', 'xmlrpc_async_call' ) );
  5092. }
  5093. $args = func_get_args();
  5094. if ( ! empty( $args[0] ) ) {
  5095. call_user_func_array( array( $clients[$client_blog_id], 'addCall' ), $args );
  5096. } elseif ( is_multisite() ) {
  5097. foreach ( $clients as $client_blog_id => $client ) {
  5098. if ( ! $client_blog_id || empty( $client->calls ) ) {
  5099. continue;
  5100. }
  5101. $switch_success = switch_to_blog( $client_blog_id, true );
  5102. if ( ! $switch_success ) {
  5103. continue;
  5104. }
  5105. flush();
  5106. $client->query();
  5107. restore_current_blog();
  5108. }
  5109. } else {
  5110. if ( isset( $clients[0] ) && ! empty( $clients[0]->calls ) ) {
  5111. flush();
  5112. $clients[0]->query();
  5113. }
  5114. }
  5115. }
  5116. public static function staticize_subdomain( $url ) {
  5117. // Extract hostname from URL
  5118. $host = parse_url( $url, PHP_URL_HOST );
  5119. // Explode hostname on '.'
  5120. $exploded_host = explode( '.', $host );
  5121. // Retrieve the name and TLD
  5122. if ( count( $exploded_host ) > 1 ) {
  5123. $name = $exploded_host[ count( $exploded_host ) - 2 ];
  5124. $tld = $exploded_host[ count( $exploded_host ) - 1 ];
  5125. // Rebuild domain excluding subdomains
  5126. $domain = $name . '.' . $tld;
  5127. } else {
  5128. $domain = $host;
  5129. }
  5130. // Array of Automattic domains
  5131. $domain_whitelist = array( 'wordpress.com', 'wp.com' );
  5132. // Return $url if not an Automattic domain
  5133. if ( ! in_array( $domain, $domain_whitelist ) ) {
  5134. return $url;
  5135. }
  5136. if ( is_ssl() ) {
  5137. return preg_replace( '|https?://[^/]++/|', 'https://s-ssl.wordpress.com/', $url );
  5138. }
  5139. srand( crc32( basename( $url ) ) );
  5140. $static_counter = rand( 0, 2 );
  5141. srand(); // this resets everything that relies on this, like array_rand() and shuffle()
  5142. return preg_replace( '|://[^/]+?/|', "://s$static_counter.wp.com/", $url );
  5143. }
  5144. /* JSON API Authorization */
  5145. /**
  5146. * Handles the login action for Authorizing the JSON API
  5147. */
  5148. function login_form_json_api_authorization() {
  5149. $this->verify_json_api_authorization_request();
  5150. add_action( 'wp_login', array( &$this, 'store_json_api_authorization_token' ), 10, 2 );
  5151. add_action( 'login_message', array( &$this, 'login_message_json_api_authorization' ) );
  5152. add_action( 'login_form', array( &$this, 'preserve_action_in_login_form_for_json_api_authorization' ) );
  5153. add_filter( 'site_url', array( &$this, 'post_login_form_to_signed_url' ), 10, 3 );
  5154. }
  5155. // Make sure the login form is POSTed to the signed URL so we can reverify the request
  5156. function post_login_form_to_signed_url( $url, $path, $scheme ) {
  5157. if ( 'wp-login.php' !== $path || ( 'login_post' !== $scheme && 'login' !== $scheme ) ) {
  5158. return $url;
  5159. }
  5160. $parsed_url = parse_url( $url );
  5161. $url = strtok( $url, '?' );
  5162. $url = "$url?{$_SERVER['QUERY_STRING']}";
  5163. if ( ! empty( $parsed_url['query'] ) )
  5164. $url .= "&{$parsed_url['query']}";
  5165. return $url;
  5166. }
  5167. // Make sure the POSTed request is handled by the same action
  5168. function preserve_action_in_login_form_for_json_api_authorization() {
  5169. echo "<input type='hidden' name='action' value='jetpack_json_api_authorization' />\n";
  5170. echo "<input type='hidden' name='jetpack_json_api_original_query' value='" . esc_url( set_url_scheme( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) ) . "' />\n";
  5171. }
  5172. // If someone logs in to approve API access, store the Access Code in usermeta
  5173. function store_json_api_authorization_token( $user_login, $user ) {
  5174. add_filter( 'login_redirect', array( &$this, 'add_token_to_login_redirect_json_api_authorization' ), 10, 3 );
  5175. add_filter( 'allowed_redirect_hosts', array( &$this, 'allow_wpcom_public_api_domain' ) );
  5176. $token = wp_generate_password( 32, false );
  5177. update_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], $token );
  5178. }
  5179. // Add public-api.wordpress.com to the safe redirect whitelist - only added when someone allows API access
  5180. function allow_wpcom_public_api_domain( $domains ) {
  5181. $domains[] = 'public-api.wordpress.com';
  5182. return $domains;
  5183. }
  5184. // Add all wordpress.com environments to the safe redirect whitelist
  5185. function allow_wpcom_environments( $domains ) {
  5186. $domains[] = 'wordpress.com';
  5187. $domains[] = 'wpcalypso.wordpress.com';
  5188. $domains[] = 'horizon.wordpress.com';
  5189. $domains[] = 'calypso.localhost';
  5190. return $domains;
  5191. }
  5192. // Add the Access Code details to the public-api.wordpress.com redirect
  5193. function add_token_to_login_redirect_json_api_authorization( $redirect_to, $original_redirect_to, $user ) {
  5194. return add_query_arg(
  5195. urlencode_deep(
  5196. array(
  5197. 'jetpack-code' => get_user_meta( $user->ID, 'jetpack_json_api_' . $this->json_api_authorization_request['client_id'], true ),
  5198. 'jetpack-user-id' => (int) $user->ID,
  5199. 'jetpack-state' => $this->json_api_authorization_request['state'],
  5200. )
  5201. ),
  5202. $redirect_to
  5203. );
  5204. }
  5205. /**
  5206. * Verifies the request by checking the signature
  5207. *
  5208. * @since 4.6.0 Method was updated to use `$_REQUEST` instead of `$_GET` and `$_POST`. Method also updated to allow
  5209. * passing in an `$environment` argument that overrides `$_REQUEST`. This was useful for integrating with SSO.
  5210. *
  5211. * @param null|array $environment
  5212. */
  5213. function verify_json_api_authorization_request( $environment = null ) {
  5214. require_once JETPACK__PLUGIN_DIR . 'class.jetpack-signature.php';
  5215. $environment = is_null( $environment )
  5216. ? $_REQUEST
  5217. : $environment;
  5218. list( $envToken, $envVersion, $envUserId ) = explode( ':', $environment['token'] );
  5219. $token = Jetpack_Data::get_access_token( $envUserId );
  5220. if ( ! $token || empty( $token->secret ) ) {
  5221. wp_die( __( 'You must connect your Jetpack plugin to WordPress.com to use this feature.' , 'jetpack' ) );
  5222. }
  5223. $die_error = __( 'Someone may be trying to trick you into giving them access to your site. Or it could be you just encountered a bug :). Either way, please close this window.', 'jetpack' );
  5224. $jetpack_signature = new Jetpack_Signature( $token->secret, (int) Jetpack_Options::get_option( 'time_diff' ) );
  5225. if ( isset( $environment['jetpack_json_api_original_query'] ) ) {
  5226. $signature = $jetpack_signature->sign_request(
  5227. $environment['token'],
  5228. $environment['timestamp'],
  5229. $environment['nonce'],
  5230. '',
  5231. 'GET',
  5232. $environment['jetpack_json_api_original_query'],
  5233. null,
  5234. true
  5235. );
  5236. } else {
  5237. $signature = $jetpack_signature->sign_current_request( array( 'body' => null, 'method' => 'GET' ) );
  5238. }
  5239. if ( ! $signature ) {
  5240. wp_die( $die_error );
  5241. } else if ( is_wp_error( $signature ) ) {
  5242. wp_die( $die_error );
  5243. } else if ( ! hash_equals( $signature, $environment['signature'] ) ) { // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  5244. if ( is_ssl() ) {
  5245. // If we signed an HTTP request on the Jetpack Servers, but got redirected to HTTPS by the local blog, check the HTTP signature as well
  5246. $signature = $jetpack_signature->sign_current_request( array( 'scheme' => 'http', 'body' => null, 'method' => 'GET' ) );
  5247. if ( ! $signature || is_wp_error( $signature ) || ! hash_equals( $signature, $environment['signature'] ) ) { // phpcs:ignore PHPCompatibility -- skipping since `hash_equals` is part of WP core
  5248. wp_die( $die_error );
  5249. }
  5250. } else {
  5251. wp_die( $die_error );
  5252. }
  5253. }
  5254. $timestamp = (int) $environment['timestamp'];
  5255. $nonce = stripslashes( (string) $environment['nonce'] );
  5256. if ( ! $this->add_nonce( $timestamp, $nonce ) ) {
  5257. // De-nonce the nonce, at least for 5 minutes.
  5258. // We have to reuse this nonce at least once (used the first time when the initial request is made, used a second time when the login form is POSTed)
  5259. $old_nonce_time = get_option( "jetpack_nonce_{$timestamp}_{$nonce}" );
  5260. if ( $old_nonce_time < time() - 300 ) {
  5261. wp_die( __( 'The authorization process expired. Please go back and try again.' , 'jetpack' ) );
  5262. }
  5263. }
  5264. $data = json_decode( base64_decode( stripslashes( $environment['data'] ) ) );
  5265. $data_filters = array(
  5266. 'state' => 'opaque',
  5267. 'client_id' => 'int',
  5268. 'client_title' => 'string',
  5269. 'client_image' => 'url',
  5270. );
  5271. foreach ( $data_filters as $key => $sanitation ) {
  5272. if ( ! isset( $data->$key ) ) {
  5273. wp_die( $die_error );
  5274. }
  5275. switch ( $sanitation ) {
  5276. case 'int' :
  5277. $this->json_api_authorization_request[$key] = (int) $data->$key;
  5278. break;
  5279. case 'opaque' :
  5280. $this->json_api_authorization_request[$key] = (string) $data->$key;
  5281. break;
  5282. case 'string' :
  5283. $this->json_api_authorization_request[$key] = wp_kses( (string) $data->$key, array() );
  5284. break;
  5285. case 'url' :
  5286. $this->json_api_authorization_request[$key] = esc_url_raw( (string) $data->$key );
  5287. break;
  5288. }
  5289. }
  5290. if ( empty( $this->json_api_authorization_request['client_id'] ) ) {
  5291. wp_die( $die_error );
  5292. }
  5293. }
  5294. function login_message_json_api_authorization( $message ) {
  5295. return '<p class="message">' . sprintf(
  5296. esc_html__( '%s wants to access your site&#8217;s data. Log in to authorize that access.' , 'jetpack' ),
  5297. '<strong>' . esc_html( $this->json_api_authorization_request['client_title'] ) . '</strong>'
  5298. ) . '<img src="' . esc_url( $this->json_api_authorization_request['client_image'] ) . '" /></p>';
  5299. }
  5300. /**
  5301. * Get $content_width, but with a <s>twist</s> filter.
  5302. */
  5303. public static function get_content_width() {
  5304. $content_width = isset( $GLOBALS['content_width'] ) ? $GLOBALS['content_width'] : false;
  5305. /**
  5306. * Filter the Content Width value.
  5307. *
  5308. * @since 2.2.3
  5309. *
  5310. * @param string $content_width Content Width value.
  5311. */
  5312. return apply_filters( 'jetpack_content_width', $content_width );
  5313. }
  5314. /**
  5315. * Pings the WordPress.com Mirror Site for the specified options.
  5316. *
  5317. * @param string|array $option_names The option names to request from the WordPress.com Mirror Site
  5318. *
  5319. * @return array An associative array of the option values as stored in the WordPress.com Mirror Site
  5320. */
  5321. public function get_cloud_site_options( $option_names ) {
  5322. $option_names = array_filter( (array) $option_names, 'is_string' );
  5323. Jetpack::load_xml_rpc_client();
  5324. $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) );
  5325. $xml->query( 'jetpack.fetchSiteOptions', $option_names );
  5326. if ( $xml->isError() ) {
  5327. return array(
  5328. 'error_code' => $xml->getErrorCode(),
  5329. 'error_msg' => $xml->getErrorMessage(),
  5330. );
  5331. }
  5332. $cloud_site_options = $xml->getResponse();
  5333. return $cloud_site_options;
  5334. }
  5335. /**
  5336. * Checks if the site is currently in an identity crisis.
  5337. *
  5338. * @return array|bool Array of options that are in a crisis, or false if everything is OK.
  5339. */
  5340. public static function check_identity_crisis() {
  5341. if ( ! Jetpack::is_active() || Jetpack::is_development_mode() || ! self::validate_sync_error_idc_option() ) {
  5342. return false;
  5343. }
  5344. return Jetpack_Options::get_option( 'sync_error_idc' );
  5345. }
  5346. /**
  5347. * Checks whether the home and siteurl specifically are whitelisted
  5348. * Written so that we don't have re-check $key and $value params every time
  5349. * we want to check if this site is whitelisted, for example in footer.php
  5350. *
  5351. * @since 3.8.0
  5352. * @return bool True = already whitelisted False = not whitelisted
  5353. */
  5354. public static function is_staging_site() {
  5355. $is_staging = false;
  5356. $known_staging = array(
  5357. 'urls' => array(
  5358. '#\.staging\.wpengine\.com$#i', // WP Engine
  5359. '#\.staging\.kinsta\.com$#i', // Kinsta.com
  5360. ),
  5361. 'constants' => array(
  5362. 'IS_WPE_SNAPSHOT', // WP Engine
  5363. 'KINSTA_DEV_ENV', // Kinsta.com
  5364. 'WPSTAGECOACH_STAGING', // WP Stagecoach
  5365. 'JETPACK_STAGING_MODE', // Generic
  5366. )
  5367. );
  5368. /**
  5369. * Filters the flags of known staging sites.
  5370. *
  5371. * @since 3.9.0
  5372. *
  5373. * @param array $known_staging {
  5374. * An array of arrays that each are used to check if the current site is staging.
  5375. * @type array $urls URLs of staging sites in regex to check against site_url.
  5376. * @type array $constants PHP constants of known staging/developement environments.
  5377. * }
  5378. */
  5379. $known_staging = apply_filters( 'jetpack_known_staging', $known_staging );
  5380. if ( isset( $known_staging['urls'] ) ) {
  5381. foreach ( $known_staging['urls'] as $url ){
  5382. if ( preg_match( $url, site_url() ) ) {
  5383. $is_staging = true;
  5384. break;
  5385. }
  5386. }
  5387. }
  5388. if ( isset( $known_staging['constants'] ) ) {
  5389. foreach ( $known_staging['constants'] as $constant ) {
  5390. if ( defined( $constant ) && constant( $constant ) ) {
  5391. $is_staging = true;
  5392. }
  5393. }
  5394. }
  5395. // Last, let's check if sync is erroring due to an IDC. If so, set the site to staging mode.
  5396. if ( ! $is_staging && self::validate_sync_error_idc_option() ) {
  5397. $is_staging = true;
  5398. }
  5399. /**
  5400. * Filters is_staging_site check.
  5401. *
  5402. * @since 3.9.0
  5403. *
  5404. * @param bool $is_staging If the current site is a staging site.
  5405. */
  5406. return apply_filters( 'jetpack_is_staging_site', $is_staging );
  5407. }
  5408. /**
  5409. * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup.
  5410. *
  5411. * @since 4.4.0
  5412. * @since 5.4.0 Do not call get_sync_error_idc_option() unless site is in IDC
  5413. *
  5414. * @return bool
  5415. */
  5416. public static function validate_sync_error_idc_option() {
  5417. $is_valid = false;
  5418. $idc_allowed = get_transient( 'jetpack_idc_allowed' );
  5419. if ( false === $idc_allowed ) {
  5420. $response = wp_remote_get( 'https://jetpack.com/is-idc-allowed/' );
  5421. if ( 200 === (int) wp_remote_retrieve_response_code( $response ) ) {
  5422. $json = json_decode( wp_remote_retrieve_body( $response ) );
  5423. $idc_allowed = isset( $json, $json->result ) && $json->result ? '1' : '0';
  5424. $transient_duration = HOUR_IN_SECONDS;
  5425. } else {
  5426. // If the request failed for some reason, then assume IDC is allowed and set shorter transient.
  5427. $idc_allowed = '1';
  5428. $transient_duration = 5 * MINUTE_IN_SECONDS;
  5429. }
  5430. set_transient( 'jetpack_idc_allowed', $idc_allowed, $transient_duration );
  5431. }
  5432. // Is the site opted in and does the stored sync_error_idc option match what we now generate?
  5433. $sync_error = Jetpack_Options::get_option( 'sync_error_idc' );
  5434. if ( $idc_allowed && $sync_error && self::sync_idc_optin() ) {
  5435. $local_options = self::get_sync_error_idc_option();
  5436. if ( $sync_error['home'] === $local_options['home'] && $sync_error['siteurl'] === $local_options['siteurl'] ) {
  5437. $is_valid = true;
  5438. }
  5439. }
  5440. /**
  5441. * Filters whether the sync_error_idc option is valid.
  5442. *
  5443. * @since 4.4.0
  5444. *
  5445. * @param bool $is_valid If the sync_error_idc is valid or not.
  5446. */
  5447. $is_valid = (bool) apply_filters( 'jetpack_sync_error_idc_validation', $is_valid );
  5448. if ( ! $idc_allowed || ( ! $is_valid && $sync_error ) ) {
  5449. // Since the option exists, and did not validate, delete it
  5450. Jetpack_Options::delete_option( 'sync_error_idc' );
  5451. }
  5452. return $is_valid;
  5453. }
  5454. /**
  5455. * Normalizes a url by doing three things:
  5456. * - Strips protocol
  5457. * - Strips www
  5458. * - Adds a trailing slash
  5459. *
  5460. * @since 4.4.0
  5461. * @param string $url
  5462. * @return WP_Error|string
  5463. */
  5464. public static function normalize_url_protocol_agnostic( $url ) {
  5465. $parsed_url = wp_parse_url( trailingslashit( esc_url_raw( $url ) ) );
  5466. if ( ! $parsed_url || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
  5467. return new WP_Error( 'cannot_parse_url', sprintf( esc_html__( 'Cannot parse URL %s', 'jetpack' ), $url ) );
  5468. }
  5469. // Strip www and protocols
  5470. $url = preg_replace( '/^www\./i', '', $parsed_url['host'] . $parsed_url['path'] );
  5471. return $url;
  5472. }
  5473. /**
  5474. * Gets the value that is to be saved in the jetpack_sync_error_idc option.
  5475. *
  5476. * @since 4.4.0
  5477. * @since 5.4.0 Add transient since home/siteurl retrieved directly from DB
  5478. *
  5479. * @param array $response
  5480. * @return array Array of the local urls, wpcom urls, and error code
  5481. */
  5482. public static function get_sync_error_idc_option( $response = array() ) {
  5483. // Since the local options will hit the database directly, store the values
  5484. // in a transient to allow for autoloading and caching on subsequent views.
  5485. $local_options = get_transient( 'jetpack_idc_local' );
  5486. if ( false === $local_options ) {
  5487. require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php';
  5488. $local_options = array(
  5489. 'home' => Jetpack_Sync_Functions::home_url(),
  5490. 'siteurl' => Jetpack_Sync_Functions::site_url(),
  5491. );
  5492. set_transient( 'jetpack_idc_local', $local_options, MINUTE_IN_SECONDS );
  5493. }
  5494. $options = array_merge( $local_options, $response );
  5495. $returned_values = array();
  5496. foreach( $options as $key => $option ) {
  5497. if ( 'error_code' === $key ) {
  5498. $returned_values[ $key ] = $option;
  5499. continue;
  5500. }
  5501. if ( is_wp_error( $normalized_url = self::normalize_url_protocol_agnostic( $option ) ) ) {
  5502. continue;
  5503. }
  5504. $returned_values[ $key ] = $normalized_url;
  5505. }
  5506. set_transient( 'jetpack_idc_option', $returned_values, MINUTE_IN_SECONDS );
  5507. return $returned_values;
  5508. }
  5509. /**
  5510. * Returns the value of the jetpack_sync_idc_optin filter, or constant.
  5511. * If set to true, the site will be put into staging mode.
  5512. *
  5513. * @since 4.3.2
  5514. * @return bool
  5515. */
  5516. public static function sync_idc_optin() {
  5517. if ( Jetpack_Constants::is_defined( 'JETPACK_SYNC_IDC_OPTIN' ) ) {
  5518. $default = Jetpack_Constants::get_constant( 'JETPACK_SYNC_IDC_OPTIN' );
  5519. } else {
  5520. $default = ! Jetpack_Constants::is_defined( 'SUNRISE' ) && ! is_multisite();
  5521. }
  5522. /**
  5523. * Allows sites to optin to IDC mitigation which blocks the site from syncing to WordPress.com when the home
  5524. * URL or site URL do not match what WordPress.com expects. The default value is either false, or the value of
  5525. * JETPACK_SYNC_IDC_OPTIN constant if set.
  5526. *
  5527. * @since 4.3.2
  5528. *
  5529. * @param bool $default Whether the site is opted in to IDC mitigation.
  5530. */
  5531. return (bool) apply_filters( 'jetpack_sync_idc_optin', $default );
  5532. }
  5533. /**
  5534. * Maybe Use a .min.css stylesheet, maybe not.
  5535. *
  5536. * Hooks onto `plugins_url` filter at priority 1, and accepts all 3 args.
  5537. */
  5538. public static function maybe_min_asset( $url, $path, $plugin ) {
  5539. // Short out on things trying to find actual paths.
  5540. if ( ! $path || empty( $plugin ) ) {
  5541. return $url;
  5542. }
  5543. $path = ltrim( $path, '/' );
  5544. // Strip out the abspath.
  5545. $base = dirname( plugin_basename( $plugin ) );
  5546. // Short out on non-Jetpack assets.
  5547. if ( 'jetpack/' !== substr( $base, 0, 8 ) ) {
  5548. return $url;
  5549. }
  5550. // File name parsing.
  5551. $file = "{$base}/{$path}";
  5552. $full_path = JETPACK__PLUGIN_DIR . substr( $file, 8 );
  5553. $file_name = substr( $full_path, strrpos( $full_path, '/' ) + 1 );
  5554. $file_name_parts_r = array_reverse( explode( '.', $file_name ) );
  5555. $extension = array_shift( $file_name_parts_r );
  5556. if ( in_array( strtolower( $extension ), array( 'css', 'js' ) ) ) {
  5557. // Already pointing at the minified version.
  5558. if ( 'min' === $file_name_parts_r[0] ) {
  5559. return $url;
  5560. }
  5561. $min_full_path = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $full_path );
  5562. if ( file_exists( $min_full_path ) ) {
  5563. $url = preg_replace( "#\.{$extension}$#", ".min.{$extension}", $url );
  5564. // If it's a CSS file, stash it so we can set the .min suffix for rtl-ing.
  5565. if ( 'css' === $extension ) {
  5566. $key = str_replace( JETPACK__PLUGIN_DIR, 'jetpack/', $min_full_path );
  5567. self::$min_assets[ $key ] = $path;
  5568. }
  5569. }
  5570. }
  5571. return $url;
  5572. }
  5573. /**
  5574. * If the asset is minified, let's flag .min as the suffix.
  5575. *
  5576. * Attached to `style_loader_src` filter.
  5577. *
  5578. * @param string $tag The tag that would link to the external asset.
  5579. * @param string $handle The registered handle of the script in question.
  5580. * @param string $href The url of the asset in question.
  5581. */
  5582. public static function set_suffix_on_min( $src, $handle ) {
  5583. if ( false === strpos( $src, '.min.css' ) ) {
  5584. return $src;
  5585. }
  5586. if ( ! empty( self::$min_assets ) ) {
  5587. foreach ( self::$min_assets as $file => $path ) {
  5588. if ( false !== strpos( $src, $file ) ) {
  5589. wp_style_add_data( $handle, 'suffix', '.min' );
  5590. return $src;
  5591. }
  5592. }
  5593. }
  5594. return $src;
  5595. }
  5596. /**
  5597. * Maybe inlines a stylesheet.
  5598. *
  5599. * If you'd like to inline a stylesheet instead of printing a link to it,
  5600. * wp_style_add_data( 'handle', 'jetpack-inline', true );
  5601. *
  5602. * Attached to `style_loader_tag` filter.
  5603. *
  5604. * @param string $tag The tag that would link to the external asset.
  5605. * @param string $handle The registered handle of the script in question.
  5606. *
  5607. * @return string
  5608. */
  5609. public static function maybe_inline_style( $tag, $handle ) {
  5610. global $wp_styles;
  5611. $item = $wp_styles->registered[ $handle ];
  5612. if ( ! isset( $item->extra['jetpack-inline'] ) || ! $item->extra['jetpack-inline'] ) {
  5613. return $tag;
  5614. }
  5615. if ( preg_match( '# href=\'([^\']+)\' #i', $tag, $matches ) ) {
  5616. $href = $matches[1];
  5617. // Strip off query string
  5618. if ( $pos = strpos( $href, '?' ) ) {
  5619. $href = substr( $href, 0, $pos );
  5620. }
  5621. // Strip off fragment
  5622. if ( $pos = strpos( $href, '#' ) ) {
  5623. $href = substr( $href, 0, $pos );
  5624. }
  5625. } else {
  5626. return $tag;
  5627. }
  5628. $plugins_dir = plugin_dir_url( JETPACK__PLUGIN_FILE );
  5629. if ( $plugins_dir !== substr( $href, 0, strlen( $plugins_dir ) ) ) {
  5630. return $tag;
  5631. }
  5632. // If this stylesheet has a RTL version, and the RTL version replaces normal...
  5633. if ( isset( $item->extra['rtl'] ) && 'replace' === $item->extra['rtl'] && is_rtl() ) {
  5634. // And this isn't the pass that actually deals with the RTL version...
  5635. if ( false === strpos( $tag, " id='$handle-rtl-css' " ) ) {
  5636. // Short out, as the RTL version will deal with it in a moment.
  5637. return $tag;
  5638. }
  5639. }
  5640. $file = JETPACK__PLUGIN_DIR . substr( $href, strlen( $plugins_dir ) );
  5641. $css = Jetpack::absolutize_css_urls( file_get_contents( $file ), $href );
  5642. if ( $css ) {
  5643. $tag = "<!-- Inline {$item->handle} -->\r\n";
  5644. if ( empty( $item->extra['after'] ) ) {
  5645. wp_add_inline_style( $handle, $css );
  5646. } else {
  5647. array_unshift( $item->extra['after'], $css );
  5648. wp_style_add_data( $handle, 'after', $item->extra['after'] );
  5649. }
  5650. }
  5651. return $tag;
  5652. }
  5653. /**
  5654. * Loads a view file from the views
  5655. *
  5656. * Data passed in with the $data parameter will be available in the
  5657. * template file as $data['value']
  5658. *
  5659. * @param string $template - Template file to load
  5660. * @param array $data - Any data to pass along to the template
  5661. * @return boolean - If template file was found
  5662. **/
  5663. public function load_view( $template, $data = array() ) {
  5664. $views_dir = JETPACK__PLUGIN_DIR . 'views/';
  5665. if( file_exists( $views_dir . $template ) ) {
  5666. require_once( $views_dir . $template );
  5667. return true;
  5668. }
  5669. error_log( "Jetpack: Unable to find view file $views_dir$template" );
  5670. return false;
  5671. }
  5672. /**
  5673. * Throws warnings for deprecated hooks to be removed from Jetpack
  5674. */
  5675. public function deprecated_hooks() {
  5676. global $wp_filter;
  5677. /*
  5678. * Format:
  5679. * deprecated_filter_name => replacement_name
  5680. *
  5681. * If there is no replacement, use null for replacement_name
  5682. */
  5683. $deprecated_list = array(
  5684. 'jetpack_bail_on_shortcode' => 'jetpack_shortcodes_to_include',
  5685. 'wpl_sharing_2014_1' => null,
  5686. 'jetpack-tools-to-include' => 'jetpack_tools_to_include',
  5687. 'jetpack_identity_crisis_options_to_check' => null,
  5688. 'update_option_jetpack_single_user_site' => null,
  5689. 'audio_player_default_colors' => null,
  5690. 'add_option_jetpack_featured_images_enabled' => null,
  5691. 'add_option_jetpack_update_details' => null,
  5692. 'add_option_jetpack_updates' => null,
  5693. 'add_option_jetpack_network_name' => null,
  5694. 'add_option_jetpack_network_allow_new_registrations' => null,
  5695. 'add_option_jetpack_network_add_new_users' => null,
  5696. 'add_option_jetpack_network_site_upload_space' => null,
  5697. 'add_option_jetpack_network_upload_file_types' => null,
  5698. 'add_option_jetpack_network_enable_administration_menus' => null,
  5699. 'add_option_jetpack_is_multi_site' => null,
  5700. 'add_option_jetpack_is_main_network' => null,
  5701. 'add_option_jetpack_main_network_site' => null,
  5702. 'jetpack_sync_all_registered_options' => null,
  5703. 'jetpack_has_identity_crisis' => 'jetpack_sync_error_idc_validation',
  5704. 'jetpack_is_post_mailable' => null,
  5705. 'jetpack_seo_site_host' => null,
  5706. 'jetpack_installed_plugin' => 'jetpack_plugin_installed',
  5707. 'jetpack_holiday_snow_option_name' => null,
  5708. 'jetpack_holiday_chance_of_snow' => null,
  5709. 'jetpack_holiday_snow_js_url' => null,
  5710. 'jetpack_is_holiday_snow_season' => null,
  5711. 'jetpack_holiday_snow_option_updated' => null,
  5712. 'jetpack_holiday_snowing' => null,
  5713. 'jetpack_sso_auth_cookie_expirtation' => 'jetpack_sso_auth_cookie_expiration',
  5714. 'jetpack_cache_plans' => null,
  5715. 'jetpack_updated_theme' => 'jetpack_updated_themes',
  5716. 'jetpack_lazy_images_skip_image_with_atttributes' => 'jetpack_lazy_images_skip_image_with_attributes',
  5717. 'jetpack_enable_site_verification' => null,
  5718. );
  5719. // This is a silly loop depth. Better way?
  5720. foreach( $deprecated_list AS $hook => $hook_alt ) {
  5721. if ( has_action( $hook ) ) {
  5722. foreach( $wp_filter[ $hook ] AS $func => $values ) {
  5723. foreach( $values AS $hooked ) {
  5724. if ( is_callable( $hooked['function'] ) ) {
  5725. $function_name = 'an anonymous function';
  5726. } else {
  5727. $function_name = $hooked['function'];
  5728. }
  5729. _deprecated_function( $hook . ' used for ' . $function_name, null, $hook_alt );
  5730. }
  5731. }
  5732. }
  5733. }
  5734. }
  5735. /**
  5736. * Converts any url in a stylesheet, to the correct absolute url.
  5737. *
  5738. * Considerations:
  5739. * - Normal, relative URLs `feh.png`
  5740. * - Data URLs `data:image/gif;base64,eh129ehiuehjdhsa==`
  5741. * - Schema-agnostic URLs `//domain.com/feh.png`
  5742. * - Absolute URLs `http://domain.com/feh.png`
  5743. * - Domain root relative URLs `/feh.png`
  5744. *
  5745. * @param $css string: The raw CSS -- should be read in directly from the file.
  5746. * @param $css_file_url : The URL that the file can be accessed at, for calculating paths from.
  5747. *
  5748. * @return mixed|string
  5749. */
  5750. public static function absolutize_css_urls( $css, $css_file_url ) {
  5751. $pattern = '#url\((?P<path>[^)]*)\)#i';
  5752. $css_dir = dirname( $css_file_url );
  5753. $p = parse_url( $css_dir );
  5754. $domain = sprintf(
  5755. '%1$s//%2$s%3$s%4$s',
  5756. isset( $p['scheme'] ) ? "{$p['scheme']}:" : '',
  5757. isset( $p['user'], $p['pass'] ) ? "{$p['user']}:{$p['pass']}@" : '',
  5758. $p['host'],
  5759. isset( $p['port'] ) ? ":{$p['port']}" : ''
  5760. );
  5761. if ( preg_match_all( $pattern, $css, $matches, PREG_SET_ORDER ) ) {
  5762. $find = $replace = array();
  5763. foreach ( $matches as $match ) {
  5764. $url = trim( $match['path'], "'\" \t" );
  5765. // If this is a data url, we don't want to mess with it.
  5766. if ( 'data:' === substr( $url, 0, 5 ) ) {
  5767. continue;
  5768. }
  5769. // If this is an absolute or protocol-agnostic url,
  5770. // we don't want to mess with it.
  5771. if ( preg_match( '#^(https?:)?//#i', $url ) ) {
  5772. continue;
  5773. }
  5774. switch ( substr( $url, 0, 1 ) ) {
  5775. case '/':
  5776. $absolute = $domain . $url;
  5777. break;
  5778. default:
  5779. $absolute = $css_dir . '/' . $url;
  5780. }
  5781. $find[] = $match[0];
  5782. $replace[] = sprintf( 'url("%s")', $absolute );
  5783. }
  5784. $css = str_replace( $find, $replace, $css );
  5785. }
  5786. return $css;
  5787. }
  5788. /**
  5789. * This methods removes all of the registered css files on the front end
  5790. * from Jetpack in favor of using a single file. In effect "imploding"
  5791. * all the files into one file.
  5792. *
  5793. * Pros:
  5794. * - Uses only ONE css asset connection instead of 15
  5795. * - Saves a minimum of 56k
  5796. * - Reduces server load
  5797. * - Reduces time to first painted byte
  5798. *
  5799. * Cons:
  5800. * - Loads css for ALL modules. However all selectors are prefixed so it
  5801. * should not cause any issues with themes.
  5802. * - Plugins/themes dequeuing styles no longer do anything. See
  5803. * jetpack_implode_frontend_css filter for a workaround
  5804. *
  5805. * For some situations developers may wish to disable css imploding and
  5806. * instead operate in legacy mode where each file loads seperately and
  5807. * can be edited individually or dequeued. This can be accomplished with
  5808. * the following line:
  5809. *
  5810. * add_filter( 'jetpack_implode_frontend_css', '__return_false' );
  5811. *
  5812. * @since 3.2
  5813. **/
  5814. public function implode_frontend_css( $travis_test = false ) {
  5815. $do_implode = true;
  5816. if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) {
  5817. $do_implode = false;
  5818. }
  5819. /**
  5820. * Allow CSS to be concatenated into a single jetpack.css file.
  5821. *
  5822. * @since 3.2.0
  5823. *
  5824. * @param bool $do_implode Should CSS be concatenated? Default to true.
  5825. */
  5826. $do_implode = apply_filters( 'jetpack_implode_frontend_css', $do_implode );
  5827. // Do not use the imploded file when default behaviour was altered through the filter
  5828. if ( ! $do_implode ) {
  5829. return;
  5830. }
  5831. // We do not want to use the imploded file in dev mode, or if not connected
  5832. if ( Jetpack::is_development_mode() || ! self::is_active() ) {
  5833. if ( ! $travis_test ) {
  5834. return;
  5835. }
  5836. }
  5837. // Do not use the imploded file if sharing css was dequeued via the sharing settings screen
  5838. if ( get_option( 'sharedaddy_disable_resources' ) ) {
  5839. return;
  5840. }
  5841. /*
  5842. * Now we assume Jetpack is connected and able to serve the single
  5843. * file.
  5844. *
  5845. * In the future there will be a check here to serve the file locally
  5846. * or potentially from the Jetpack CDN
  5847. *
  5848. * For now:
  5849. * - Enqueue a single imploded css file
  5850. * - Zero out the style_loader_tag for the bundled ones
  5851. * - Be happy, drink scotch
  5852. */
  5853. add_filter( 'style_loader_tag', array( $this, 'concat_remove_style_loader_tag' ), 10, 2 );
  5854. $version = Jetpack::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . 'css/jetpack.css' ) : JETPACK__VERSION;
  5855. wp_enqueue_style( 'jetpack_css', plugins_url( 'css/jetpack.css', __FILE__ ), array(), $version );
  5856. wp_style_add_data( 'jetpack_css', 'rtl', 'replace' );
  5857. }
  5858. function concat_remove_style_loader_tag( $tag, $handle ) {
  5859. if ( in_array( $handle, $this->concatenated_style_handles ) ) {
  5860. $tag = '';
  5861. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  5862. $tag = "<!-- `" . esc_html( $handle ) . "` is included in the concatenated jetpack.css -->\r\n";
  5863. }
  5864. }
  5865. return $tag;
  5866. }
  5867. /*
  5868. * Check the heartbeat data
  5869. *
  5870. * Organizes the heartbeat data by severity. For example, if the site
  5871. * is in an ID crisis, it will be in the $filtered_data['bad'] array.
  5872. *
  5873. * Data will be added to "caution" array, if it either:
  5874. * - Out of date Jetpack version
  5875. * - Out of date WP version
  5876. * - Out of date PHP version
  5877. *
  5878. * $return array $filtered_data
  5879. */
  5880. public static function jetpack_check_heartbeat_data() {
  5881. $raw_data = Jetpack_Heartbeat::generate_stats_array();
  5882. $good = array();
  5883. $caution = array();
  5884. $bad = array();
  5885. foreach ( $raw_data as $stat => $value ) {
  5886. // Check jetpack version
  5887. if ( 'version' == $stat ) {
  5888. if ( version_compare( $value, JETPACK__VERSION, '<' ) ) {
  5889. $caution[ $stat ] = $value . " - min supported is " . JETPACK__VERSION;
  5890. continue;
  5891. }
  5892. }
  5893. // Check WP version
  5894. if ( 'wp-version' == $stat ) {
  5895. if ( version_compare( $value, JETPACK__MINIMUM_WP_VERSION, '<' ) ) {
  5896. $caution[ $stat ] = $value . " - min supported is " . JETPACK__MINIMUM_WP_VERSION;
  5897. continue;
  5898. }
  5899. }
  5900. // Check PHP version
  5901. if ( 'php-version' == $stat ) {
  5902. if ( version_compare( PHP_VERSION, '5.2.4', '<' ) ) {
  5903. $caution[ $stat ] = $value . " - min supported is 5.2.4";
  5904. continue;
  5905. }
  5906. }
  5907. // Check ID crisis
  5908. if ( 'identitycrisis' == $stat ) {
  5909. if ( 'yes' == $value ) {
  5910. $bad[ $stat ] = $value;
  5911. continue;
  5912. }
  5913. }
  5914. // The rest are good :)
  5915. $good[ $stat ] = $value;
  5916. }
  5917. $filtered_data = array(
  5918. 'good' => $good,
  5919. 'caution' => $caution,
  5920. 'bad' => $bad
  5921. );
  5922. return $filtered_data;
  5923. }
  5924. /*
  5925. * This method is used to organize all options that can be reset
  5926. * without disconnecting Jetpack.
  5927. *
  5928. * It is used in class.jetpack-cli.php to reset options
  5929. *
  5930. * @since 5.4.0 Logic moved to Jetpack_Options class. Method left in Jetpack class for backwards compat.
  5931. *
  5932. * @return array of options to delete.
  5933. */
  5934. public static function get_jetpack_options_for_reset() {
  5935. return Jetpack_Options::get_options_for_reset();
  5936. }
  5937. /**
  5938. * Check if an option of a Jetpack module has been updated.
  5939. *
  5940. * If any module option has been updated before Jump Start has been dismissed,
  5941. * update the 'jumpstart' option so we can hide Jump Start.
  5942. *
  5943. * @param string $option_name
  5944. *
  5945. * @return bool
  5946. */
  5947. public static function jumpstart_has_updated_module_option( $option_name = '' ) {
  5948. // Bail if Jump Start has already been dismissed
  5949. if ( 'new_connection' !== Jetpack_Options::get_option( 'jumpstart' ) ) {
  5950. return false;
  5951. }
  5952. $jetpack = Jetpack::init();
  5953. // Manual build of module options
  5954. $option_names = self::get_jetpack_options_for_reset();
  5955. if ( in_array( $option_name, $option_names['wp_options'] ) ) {
  5956. Jetpack_Options::update_option( 'jumpstart', 'jetpack_action_taken' );
  5957. //Jump start is being dismissed send data to MC Stats
  5958. $jetpack->stat( 'jumpstart', 'manual,'.$option_name );
  5959. $jetpack->do_stats( 'server_side' );
  5960. }
  5961. }
  5962. /*
  5963. * Strip http:// or https:// from a url, replaces forward slash with ::,
  5964. * so we can bring them directly to their site in calypso.
  5965. *
  5966. * @param string | url
  5967. * @return string | url without the guff
  5968. */
  5969. public static function build_raw_urls( $url ) {
  5970. $strip_http = '/.*?:\/\//i';
  5971. $url = preg_replace( $strip_http, '', $url );
  5972. $url = str_replace( '/', '::', $url );
  5973. return $url;
  5974. }
  5975. /**
  5976. * Stores and prints out domains to prefetch for page speed optimization.
  5977. *
  5978. * @param mixed $new_urls
  5979. */
  5980. public static function dns_prefetch( $new_urls = null ) {
  5981. static $prefetch_urls = array();
  5982. if ( empty( $new_urls ) && ! empty( $prefetch_urls ) ) {
  5983. echo "\r\n";
  5984. foreach ( $prefetch_urls as $this_prefetch_url ) {
  5985. printf( "<link rel='dns-prefetch' href='%s'/>\r\n", esc_attr( $this_prefetch_url ) );
  5986. }
  5987. } elseif ( ! empty( $new_urls ) ) {
  5988. if ( ! has_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) ) ) {
  5989. add_action( 'wp_head', array( __CLASS__, __FUNCTION__ ) );
  5990. }
  5991. foreach ( (array) $new_urls as $this_new_url ) {
  5992. $prefetch_urls[] = strtolower( untrailingslashit( preg_replace( '#^https?://#i', '//', $this_new_url ) ) );
  5993. }
  5994. $prefetch_urls = array_unique( $prefetch_urls );
  5995. }
  5996. }
  5997. public function wp_dashboard_setup() {
  5998. if ( self::is_active() ) {
  5999. add_action( 'jetpack_dashboard_widget', array( __CLASS__, 'dashboard_widget_footer' ), 999 );
  6000. }
  6001. if ( has_action( 'jetpack_dashboard_widget' ) ) {
  6002. wp_add_dashboard_widget(
  6003. 'jetpack_summary_widget',
  6004. esc_html__( 'Site Stats', 'jetpack' ),
  6005. array( __CLASS__, 'dashboard_widget' )
  6006. );
  6007. wp_enqueue_style( 'jetpack-dashboard-widget', plugins_url( 'css/dashboard-widget.css', JETPACK__PLUGIN_FILE ), array(), JETPACK__VERSION );
  6008. // If we're inactive and not in development mode, sort our box to the top.
  6009. if ( ! self::is_active() && ! self::is_development_mode() ) {
  6010. global $wp_meta_boxes;
  6011. $dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
  6012. $ours = array( 'jetpack_summary_widget' => $dashboard['jetpack_summary_widget'] );
  6013. $wp_meta_boxes['dashboard']['normal']['core'] = array_merge( $ours, $dashboard );
  6014. }
  6015. }
  6016. }
  6017. /**
  6018. * @param mixed $result Value for the user's option
  6019. * @return mixed
  6020. */
  6021. function get_user_option_meta_box_order_dashboard( $sorted ) {
  6022. if ( ! is_array( $sorted ) ) {
  6023. return $sorted;
  6024. }
  6025. foreach ( $sorted as $box_context => $ids ) {
  6026. if ( false === strpos( $ids, 'dashboard_stats' ) ) {
  6027. // If the old id isn't anywhere in the ids, don't bother exploding and fail out.
  6028. continue;
  6029. }
  6030. $ids_array = explode( ',', $ids );
  6031. $key = array_search( 'dashboard_stats', $ids_array );
  6032. if ( false !== $key ) {
  6033. // If we've found that exact value in the option (and not `google_dashboard_stats` for example)
  6034. $ids_array[ $key ] = 'jetpack_summary_widget';
  6035. $sorted[ $box_context ] = implode( ',', $ids_array );
  6036. // We've found it, stop searching, and just return.
  6037. break;
  6038. }
  6039. }
  6040. return $sorted;
  6041. }
  6042. public static function dashboard_widget() {
  6043. /**
  6044. * Fires when the dashboard is loaded.
  6045. *
  6046. * @since 3.4.0
  6047. */
  6048. do_action( 'jetpack_dashboard_widget' );
  6049. }
  6050. public static function dashboard_widget_footer() {
  6051. ?>
  6052. <footer>
  6053. <div class="protect">
  6054. <?php if ( Jetpack::is_module_active( 'protect' ) ) : ?>
  6055. <h3><?php echo number_format_i18n( get_site_option( 'jetpack_protect_blocked_attempts', 0 ) ); ?></h3>
  6056. <p><?php echo esc_html_x( 'Blocked malicious login attempts', '{#} Blocked malicious login attempts -- number is on a prior line, text is a caption.', 'jetpack' ); ?></p>
  6057. <?php elseif ( current_user_can( 'jetpack_activate_modules' ) && ! self::is_development_mode() ) : ?>
  6058. <a href="<?php echo esc_url( wp_nonce_url( Jetpack::admin_url( array( 'action' => 'activate', 'module' => 'protect' ) ), 'jetpack_activate-protect' ) ); ?>" class="button button-jetpack" title="<?php esc_attr_e( 'Protect helps to keep you secure from brute-force login attacks.', 'jetpack' ); ?>">
  6059. <?php esc_html_e( 'Activate Protect', 'jetpack' ); ?>
  6060. </a>
  6061. <?php else : ?>
  6062. <?php esc_html_e( 'Protect is inactive.', 'jetpack' ); ?>
  6063. <?php endif; ?>
  6064. </div>
  6065. <div class="akismet">
  6066. <?php if ( is_plugin_active( 'akismet/akismet.php' ) ) : ?>
  6067. <h3><?php echo number_format_i18n( get_option( 'akismet_spam_count', 0 ) ); ?></h3>
  6068. <p><?php echo esc_html_x( 'Spam comments blocked by Akismet.', '{#} Spam comments blocked by Akismet -- number is on a prior line, text is a caption.', 'jetpack' ); ?></p>
  6069. <?php elseif ( current_user_can( 'activate_plugins' ) && ! is_wp_error( validate_plugin( 'akismet/akismet.php' ) ) ) : ?>
  6070. <a href="<?php echo esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'activate', 'plugin' => 'akismet/akismet.php' ), admin_url( 'plugins.php' ) ), 'activate-plugin_akismet/akismet.php' ) ); ?>" class="button button-jetpack">
  6071. <?php esc_html_e( 'Activate Akismet', 'jetpack' ); ?>
  6072. </a>
  6073. <?php else : ?>
  6074. <p><a href="<?php echo esc_url( 'https://akismet.com/?utm_source=jetpack&utm_medium=link&utm_campaign=Jetpack%20Dashboard%20Widget%20Footer%20Link' ); ?>"><?php esc_html_e( 'Akismet can help to keep your blog safe from spam!', 'jetpack' ); ?></a></p>
  6075. <?php endif; ?>
  6076. </div>
  6077. </footer>
  6078. <?php
  6079. }
  6080. /**
  6081. * Return string containing the Jetpack logo.
  6082. *
  6083. * @since 3.9.0
  6084. *
  6085. * @return string
  6086. */
  6087. public static function get_jp_emblem() {
  6088. return '<svg id="jetpack-logo__icon" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 32 32"><path fill="#00BE28" d="M16,0C7.2,0,0,7.2,0,16s7.2,16,16,16c8.8,0,16-7.2,16-16S24.8,0,16,0z M15.2,18.7h-8l8-15.5V18.7z M16.8,28.8 V13.3h8L16.8,28.8z"/></svg>';
  6089. }
  6090. /*
  6091. * Adds a "blank" column in the user admin table to display indication of user connection.
  6092. */
  6093. function jetpack_icon_user_connected( $columns ) {
  6094. $columns['user_jetpack'] = '';
  6095. return $columns;
  6096. }
  6097. /*
  6098. * Show Jetpack icon if the user is linked.
  6099. */
  6100. function jetpack_show_user_connected_icon( $val, $col, $user_id ) {
  6101. if ( 'user_jetpack' == $col && Jetpack::is_user_connected( $user_id ) ) {
  6102. $emblem_html = sprintf(
  6103. '<a title="%1$s" class="jp-emblem-user-admin">%2$s</a>',
  6104. esc_attr__( 'This user is linked and ready to fly with Jetpack.', 'jetpack' ),
  6105. Jetpack::get_jp_emblem()
  6106. );
  6107. return $emblem_html;
  6108. }
  6109. return $val;
  6110. }
  6111. /*
  6112. * Style the Jetpack user column
  6113. */
  6114. function jetpack_user_col_style() {
  6115. global $current_screen;
  6116. if ( ! empty( $current_screen->base ) && 'users' == $current_screen->base ) { ?>
  6117. <style>
  6118. .fixed .column-user_jetpack {
  6119. width: 21px;
  6120. }
  6121. .jp-emblem-user-admin svg {
  6122. width: 20px;
  6123. height: 20px;
  6124. }
  6125. .jp-emblem-user-admin path {
  6126. fill: #00BE28;
  6127. }
  6128. </style>
  6129. <?php }
  6130. }
  6131. /**
  6132. * Checks if Akismet is active and working.
  6133. *
  6134. * We dropped support for Akismet 3.0 with Jetpack 6.1.1 while introducing a check for an Akismet valid key
  6135. * that implied usage of methods present since more recent version.
  6136. * See https://github.com/Automattic/jetpack/pull/9585
  6137. *
  6138. * @since 5.1.0
  6139. *
  6140. * @return bool True = Akismet available. False = Aksimet not available.
  6141. */
  6142. public static function is_akismet_active() {
  6143. if ( method_exists( 'Akismet' , 'http_post' ) ) {
  6144. $akismet_key = Akismet::get_api_key();
  6145. if ( ! $akismet_key ) {
  6146. return false;
  6147. }
  6148. $cached_key_verification = get_transient( 'jetpack_akismet_key_is_valid' );
  6149. // We cache the result of the Akismet key verification for ten minutes.
  6150. if ( in_array( $cached_key_verification, array( 'valid', 'invalid' ) ) ) {
  6151. $akismet_key_state = $cached_key_verification;
  6152. } else {
  6153. $akismet_key_state = Akismet::verify_key( $akismet_key );
  6154. if ( 'failed' === $akismet_key_state ) {
  6155. return false;
  6156. }
  6157. set_transient( 'jetpack_akismet_key_is_valid', $akismet_key_state, 10 * MINUTE_IN_SECONDS );
  6158. }
  6159. return ( 'valid' === $akismet_key_state );
  6160. }
  6161. return false;
  6162. }
  6163. /**
  6164. * Checks if one or more function names is in debug_backtrace
  6165. *
  6166. * @param $names Mixed string name of function or array of string names of functions
  6167. *
  6168. * @return bool
  6169. */
  6170. public static function is_function_in_backtrace( $names ) {
  6171. $backtrace = debug_backtrace( false ); // phpcs:ignore PHPCompatibility
  6172. if ( ! is_array( $names ) ) {
  6173. $names = array( $names );
  6174. }
  6175. $names_as_keys = array_flip( $names );
  6176. //Do check in constant O(1) time for PHP5.5+
  6177. if ( function_exists( 'array_column' ) ) {
  6178. $backtrace_functions = array_column( $backtrace, 'function' ); // phpcs:ignore PHPCompatibility
  6179. $backtrace_functions_as_keys = array_flip( $backtrace_functions );
  6180. $intersection = array_intersect_key( $backtrace_functions_as_keys, $names_as_keys );
  6181. return ! empty ( $intersection );
  6182. }
  6183. //Do check in linear O(n) time for < PHP5.5 ( using isset at least prevents O(n^2) )
  6184. foreach ( $backtrace as $call ) {
  6185. if ( isset( $names_as_keys[ $call['function'] ] ) ) {
  6186. return true;
  6187. }
  6188. }
  6189. return false;
  6190. }
  6191. /**
  6192. * Given a minified path, and a non-minified path, will return
  6193. * a minified or non-minified file URL based on whether SCRIPT_DEBUG is set and truthy.
  6194. *
  6195. * Both `$min_base` and `$non_min_base` are expected to be relative to the
  6196. * root Jetpack directory.
  6197. *
  6198. * @since 5.6.0
  6199. *
  6200. * @param string $min_path
  6201. * @param string $non_min_path
  6202. * @return string The URL to the file
  6203. */
  6204. public static function get_file_url_for_environment( $min_path, $non_min_path ) {
  6205. $path = ( Jetpack_Constants::is_defined( 'SCRIPT_DEBUG' ) && Jetpack_Constants::get_constant( 'SCRIPT_DEBUG' ) )
  6206. ? $non_min_path
  6207. : $min_path;
  6208. return plugins_url( $path, JETPACK__PLUGIN_FILE );
  6209. }
  6210. /**
  6211. * Checks for whether Jetpack Rewind is enabled.
  6212. * Will return true if the state of Rewind is anything except "unavailable".
  6213. * @return bool|int|mixed
  6214. */
  6215. public static function is_rewind_enabled() {
  6216. if ( ! Jetpack::is_active() ) {
  6217. return false;
  6218. }
  6219. $rewind_enabled = get_transient( 'jetpack_rewind_enabled' );
  6220. if ( false === $rewind_enabled ) {
  6221. jetpack_require_lib( 'class.core-rest-api-endpoints' );
  6222. $rewind_data = (array) Jetpack_Core_Json_Api_Endpoints::rewind_data();
  6223. $rewind_enabled = ( ! is_wp_error( $rewind_data )
  6224. && ! empty( $rewind_data['state'] )
  6225. && 'active' === $rewind_data['state'] )
  6226. ? 1
  6227. : 0;
  6228. set_transient( 'jetpack_rewind_enabled', $rewind_enabled, 10 * MINUTE_IN_SECONDS );
  6229. }
  6230. return $rewind_enabled;
  6231. }
  6232. /**
  6233. * Checks whether or not TOS has been agreed upon.
  6234. * Will return true if a user has clicked to register, or is already connected.
  6235. */
  6236. public static function jetpack_tos_agreed() {
  6237. return Jetpack_Options::get_option( 'tos_agreed' ) || Jetpack::is_active();
  6238. }
  6239. /**
  6240. * Handles activating default modules as well general cleanup for the new connection.
  6241. *
  6242. * @param boolean $activate_sso Whether to activate the SSO module when activating default modules.
  6243. * @param boolean $redirect_on_activation_error Whether to redirect on activation error.
  6244. * @param boolean $send_state_messages Whether to send state messages.
  6245. * @return void
  6246. */
  6247. public static function handle_post_authorization_actions(
  6248. $activate_sso = false,
  6249. $redirect_on_activation_error = false,
  6250. $send_state_messages = true
  6251. ) {
  6252. $other_modules = $activate_sso
  6253. ? array( 'sso' )
  6254. : array();
  6255. if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) {
  6256. Jetpack::delete_active_modules();
  6257. Jetpack::activate_default_modules( 999, 1, array_merge( $active_modules, $other_modules ), $redirect_on_activation_error, $send_state_messages );
  6258. } else {
  6259. Jetpack::activate_default_modules( false, false, $other_modules, $redirect_on_activation_error, $send_state_messages );
  6260. }
  6261. // Since this is a fresh connection, be sure to clear out IDC options
  6262. Jetpack_IDC::clear_all_idc_options();
  6263. Jetpack_Options::delete_raw_option( 'jetpack_last_connect_url_check' );
  6264. // Start nonce cleaner
  6265. wp_clear_scheduled_hook( 'jetpack_clean_nonces' );
  6266. wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' );
  6267. if ( $send_state_messages ) {
  6268. Jetpack::state( 'message', 'authorized' );
  6269. }
  6270. }
  6271. /**
  6272. * Check if Gutenberg editor is available
  6273. *
  6274. * @since 6.5.0
  6275. *
  6276. * @return bool
  6277. */
  6278. public static function is_gutenberg_available() {
  6279. return function_exists( 'register_block_type' );
  6280. }
  6281. /**
  6282. * Load Gutenberg editor blocks.
  6283. *
  6284. * This section meant for unstable phase of developing Jetpack's
  6285. * Gutenberg extensions. If still around after Sep. 15, 2018 then
  6286. * please file an issue to remove it; if nobody responds within one
  6287. * week then please delete the code.
  6288. *
  6289. *
  6290. * Loading blocks is disabled by default and enabled via filter:
  6291. * add_filter( 'jetpack_gutenberg', '__return_true', 10 );
  6292. *
  6293. * When enabled, blocks are loaded from CDN by default. To load locally instead:
  6294. * add_filter( 'jetpack_gutenberg_cdn', '__return_false', 10 );
  6295. *
  6296. * Note that when loaded locally, you need to build the files yourself:
  6297. * - _inc/blocks/jetpack-editor.js
  6298. * - _inc/blocks/jetpack-editor.css
  6299. *
  6300. * CDN cache is busted once a day or when Jetpack version changes. To customize it:
  6301. * add_filter( 'jetpack_gutenberg_cdn_cache_buster', function( $version ) { return time(); }, 10, 1 );
  6302. *
  6303. * @since 6.5.0
  6304. *
  6305. * @return void
  6306. */
  6307. public static function load_jetpack_gutenberg() {
  6308. /**
  6309. * Filter to turn on loading Gutenberg blocks
  6310. *
  6311. * @since 6.5.0
  6312. *
  6313. * @param bool false Whether to load Gutenberg blocks
  6314. */
  6315. if ( ! Jetpack::is_gutenberg_available() || ! apply_filters( 'jetpack_gutenberg', false ) ) {
  6316. return;
  6317. }
  6318. /**
  6319. * Filter to turn off serving blocks via CDN
  6320. *
  6321. * @since 6.5.0
  6322. *
  6323. * @param bool true Whether to load Gutenberg blocks from CDN
  6324. */
  6325. if ( apply_filters( 'jetpack_gutenberg_cdn', true ) ) {
  6326. $editor_script = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks/jetpack-editor.js';
  6327. $editor_style = 'https://s0.wp.com/wp-content/mu-plugins/jetpack/_inc/blocks/jetpack-editor.css';
  6328. /**
  6329. * Filter to modify cache busting for Gutenberg block assets loaded from CDN
  6330. *
  6331. * @since 6.5.0
  6332. *
  6333. * @param string
  6334. */
  6335. $version = apply_filters( 'jetpack_gutenberg_cdn_cache_buster', sprintf( '%s-%s', gmdate( 'd-m-Y' ), JETPACK__VERSION ) );
  6336. } else {
  6337. $editor_script = plugins_url( '_inc/blocks/jetpack-editor.js', JETPACK__PLUGIN_FILE );
  6338. $editor_style = plugins_url( '_inc/blocks/jetpack-editor.css', JETPACK__PLUGIN_FILE );
  6339. $version = Jetpack::is_development_version() ? filemtime( JETPACK__PLUGIN_DIR . '_inc/blocks/jetpack-editor.js' ) : JETPACK__VERSION;
  6340. }
  6341. wp_register_script(
  6342. 'jetpack-blocks-editor',
  6343. $editor_script,
  6344. array(
  6345. 'wp-blocks',
  6346. 'wp-components',
  6347. 'wp-compose',
  6348. 'wp-data',
  6349. 'wp-editor',
  6350. 'wp-element',
  6351. 'wp-i18n',
  6352. 'wp-plugins',
  6353. ),
  6354. $version
  6355. );
  6356. wp_register_style(
  6357. 'jetpack-blocks-editor',
  6358. $editor_style,
  6359. array(),
  6360. $version
  6361. );
  6362. register_block_type( 'jetpack/blocks', array(
  6363. 'editor_script' => 'jetpack-blocks-editor',
  6364. 'editor_style' => 'jetpack-blocks-editor',
  6365. ) );
  6366. }
  6367. /**
  6368. * Returns a boolean for whether backups UI should be displayed or not.
  6369. *
  6370. * @return bool Should backups UI be displayed?
  6371. */
  6372. public static function show_backups_ui() {
  6373. /**
  6374. * Whether UI for backups should be displayed.
  6375. *
  6376. * @since 6.5.0
  6377. *
  6378. * @param bool $show_backups Should UI for backups be displayed? True by default.
  6379. */
  6380. return Jetpack::is_plugin_active( 'vaultpress/vaultpress.php' ) || apply_filters( 'jetpack_show_backups', true );
  6381. }
  6382. }