class-fl-builder-model.php 186 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535
  1. <?php
  2. /**
  3. * Builder data handling class that deals
  4. * with all database operations.
  5. *
  6. * @since 1.0
  7. */
  8. final class FLBuilderModel {
  9. /**
  10. * An array that contains the sizes for columns
  11. * in each row layout.
  12. *
  13. * @since 1.0
  14. * @var array $row_layouts
  15. */
  16. static public $row_layouts = array(
  17. '1-col' => array( 100 ),
  18. '2-cols' => array( 50, 50 ),
  19. '3-cols' => array( 33.33, 33.33, 33.33 ),
  20. '4-cols' => array( 25, 25, 25, 25 ),
  21. '5-cols' => array( 20, 20, 20, 20, 20 ),
  22. '6-cols' => array( 16.65, 16.65, 16.65, 16.65, 16.65, 16.65 ),
  23. 'left-sidebar' => array( 33.33, 66.66 ),
  24. 'right-sidebar' => array( 66.66, 33.33 ),
  25. 'left-right-sidebar' => array( 25, 50, 25 ),
  26. );
  27. /**
  28. * An array that contains data for each registered settings form.
  29. *
  30. * @since 1.0
  31. * @var array $settings_forms
  32. */
  33. static public $settings_forms = array();
  34. /**
  35. * An array used to cache default values for settings forms.
  36. *
  37. * @since 1.0
  38. * @var array $settings_form_defaults
  39. */
  40. static public $settings_form_defaults = array();
  41. /**
  42. * An array that instances for each registered module.
  43. *
  44. * @since 1.0
  45. * @var array $modules
  46. */
  47. static public $modules = array();
  48. /**
  49. * An array of module aliases with their own names,
  50. * categories and default settings.
  51. *
  52. * @since 1.10
  53. * @var array $module_aliases
  54. */
  55. static public $module_aliases = array();
  56. /**
  57. * Whether the builder is active or not.
  58. *
  59. * @since 1.10
  60. * @access private
  61. * @var bool $active
  62. */
  63. static private $active = null;
  64. /**
  65. * Cached global settings.
  66. *
  67. * @access private
  68. * @var array $global_settings
  69. */
  70. static private $global_settings;
  71. /**
  72. * The last node id that was generated by the builder.
  73. * This is saved to ensure the next node id is unique.
  74. *
  75. * @since 1.0
  76. * @access private
  77. * @var string $last_generated_node_id
  78. */
  79. static private $last_generated_node_id = null;
  80. /**
  81. * Cached post data from either the $_POST array
  82. * or from the fl_builder_data post variable.
  83. *
  84. * @since 1.0
  85. * @access private
  86. * @var array $post_data
  87. */
  88. static private $post_data = null;
  89. /**
  90. * An array of post IDs the builder will be forced to use instead
  91. * of a post ID set in the internal $post_data array or the global
  92. * $post->ID when calling the FLBuilderModel::get_post_id method.
  93. *
  94. * The first post ID in the array is always returned for the
  95. * FLBuilderModel::get_post_id method. To get a previously set
  96. * post ID, first call the FLBuilderModel::reset_post_id method.
  97. *
  98. * @since 1.10
  99. * @access private
  100. * @var array $post_id
  101. */
  102. static private $post_id = array();
  103. /**
  104. * An array of cached published layout data by post_id.
  105. *
  106. * @since 1.0
  107. * @access private
  108. * @var array $published_layout_data
  109. */
  110. static private $published_layout_data = array();
  111. /**
  112. * An array of cached draft layout data by post_id.
  113. *
  114. * @since 1.0
  115. * @access private
  116. * @var array $draft_layout_data
  117. */
  118. static private $draft_layout_data = array();
  119. /**
  120. * An array of paths to template data files.
  121. *
  122. * @since 1.8
  123. * @access private
  124. * @var array $templates
  125. */
  126. static private $templates = array();
  127. /**
  128. * An array of cached template data that has been
  129. * loaded from .dat files.
  130. *
  131. * @since 1.10
  132. * @access private
  133. * @var array $template_data
  134. */
  135. static private $template_data = null;
  136. /**
  137. * An array of cached post IDs for node templates.
  138. *
  139. * @since 1.7.6
  140. * @access private
  141. * @var array $node_template_post_ids
  142. */
  143. static private $node_template_post_ids = array();
  144. /**
  145. * An array of cached types for user and node templates.
  146. *
  147. * @since 1.7.9
  148. * @access private
  149. * @var array $node_template_types
  150. */
  151. static private $node_template_types = array();
  152. /**
  153. * Initialize hooks.
  154. *
  155. * @since 1.8
  156. * @return void
  157. */
  158. static public function init() {
  159. /* Admin AJAX */
  160. add_action( 'wp_ajax_fl_builder_disable', __CLASS__ . '::disable' );
  161. add_action( 'wp_ajax_fl_builder_duplicate_wpml_layout', __CLASS__ . '::duplicate_wpml_layout' );
  162. /* Actions */
  163. add_action( 'init', __CLASS__ . '::load_settings', 1 );
  164. add_action( 'init', __CLASS__ . '::load_modules', 2 );
  165. add_action( 'before_delete_post', __CLASS__ . '::delete_post' );
  166. add_action( 'save_post', __CLASS__ . '::save_revision' );
  167. add_action( 'save_post', __CLASS__ . '::set_node_template_default_type', 10, 3 );
  168. add_action( 'wp_restore_post_revision', __CLASS__ . '::restore_revision', 10, 2 );
  169. /* Filters */
  170. add_filter( 'heartbeat_received', __CLASS__ . '::lock_post', 10, 2 );
  171. add_filter( 'fl_builder_register_settings_form', __CLASS__ . '::filter_row_settings_for_resize', 10, 2 );
  172. /* Core Templates */
  173. self::register_core_templates();
  174. }
  175. /**
  176. * Returns a builder edit URL for a post.
  177. *
  178. * @since 1.0
  179. * @param int $post_id The post id to get an edit url for.
  180. * @return string
  181. */
  182. static public function get_edit_url( $post_id = false ) {
  183. if ( false === $post_id ) {
  184. global $post;
  185. } else {
  186. $post = get_post( $post_id );
  187. }
  188. preg_match( '/(https?)/', get_bloginfo( 'url' ), $matches );
  189. $scheme = ( isset( $matches[1] ) ) ? $matches[1] : false;
  190. return set_url_scheme( add_query_arg( 'fl_builder', '', get_permalink( $post->ID ) ), $scheme );
  191. }
  192. /**
  193. * Returns the URL to upgrade the builder to the premium version.
  194. * Can be overridden by theme developers to use their affiliate
  195. * link using the fl_builder_upgrade_url filter.
  196. *
  197. * @since 1.0
  198. * @param array $params An array of key/value params to add to the query string.
  199. * @return string
  200. */
  201. static public function get_upgrade_url( $params = array() ) {
  202. /**
  203. * Use this filter to modify the upgrade URL in Beaver Builder Lite.
  204. * This can be used to add an affiliate ID.
  205. * @see fl_builder_upgrade_url
  206. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  207. */
  208. return apply_filters( 'fl_builder_upgrade_url', self::get_store_url( '', $params ) );
  209. }
  210. /**
  211. * Returns a URL that points to the Beaver Builder store.
  212. *
  213. * @since 1.8.6
  214. * @param string $path A URL path to append to the store URL.
  215. * @param array $params An array of key/value params to add to the query string.
  216. * @return string
  217. */
  218. static public function get_store_url( $path = '', $params = array() ) {
  219. $url = trailingslashit( FL_BUILDER_STORE_URL . $path ) . '?' . http_build_query( $params, '', '&' );
  220. return apply_filters( 'fl_builder_store_url', $url, $path );
  221. }
  222. /**
  223. * Returns an array of post data from either $_POST['fl_builder_data']
  224. * or $_POST if that is not set.
  225. *
  226. * @since 1.0
  227. * @return array
  228. */
  229. static public function get_post_data() {
  230. if ( ! self::$post_data ) {
  231. self::$post_data = array();
  232. if ( isset( $_POST['fl_builder_data'] ) ) {
  233. // Decode settings if our ModSecurity fix is enabled.
  234. if ( isset( $_POST['fl_builder_data']['settings'] ) ) {
  235. $_POST['fl_builder_data']['settings'] = FLBuilderUtils::modsec_fix_decode( $_POST['fl_builder_data']['settings'] );
  236. }
  237. if ( isset( $_POST['fl_builder_data']['node_settings'] ) ) {
  238. $_POST['fl_builder_data']['node_settings'] = FLBuilderUtils::modsec_fix_decode( $_POST['fl_builder_data']['node_settings'] );
  239. }
  240. $data = FLBuilderUtils::json_decode_deep( wp_unslash( $_POST['fl_builder_data'] ) );
  241. foreach ( $data as $key => $val ) {
  242. self::$post_data[ $key ] = $val;
  243. }
  244. } elseif ( isset( $_POST ) ) {
  245. foreach ( $_POST as $key => $val ) {
  246. self::$post_data[ $key ] = $val;
  247. }
  248. }
  249. }
  250. return self::$post_data;
  251. }
  252. /**
  253. * Update a value in the $post_data array.
  254. *
  255. * @since 1.0
  256. * @param string $key The post data key.
  257. * @param mixed $value The value to update.
  258. * @return void
  259. */
  260. static public function update_post_data( $key, $value ) {
  261. $post_data = self::get_post_data();
  262. $post_data[ $key ] = $value;
  263. self::$post_data = $post_data;
  264. }
  265. /**
  266. * Return an array of post types that the builder
  267. * is enabled to work with.
  268. *
  269. * @since 1.0
  270. * @return array
  271. */
  272. static public function get_post_types() {
  273. $value = self::get_admin_settings_option( '_fl_builder_post_types', true );
  274. if ( ! $value ) {
  275. $value = array( 'page', 'fl-builder-template' );
  276. } else {
  277. $value[] = 'fl-builder-template';
  278. }
  279. /**
  280. * Use this filter to modify the post types that the builder works with.
  281. * @see fl_builder_post_types
  282. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  283. */
  284. return apply_filters( 'fl_builder_post_types', $value );
  285. }
  286. /**
  287. * Return an array of post ids that should have their
  288. * builder assets loaded globally.
  289. *
  290. * @since 1.0
  291. * @return array
  292. */
  293. static public function get_global_posts() {
  294. /**
  295. * Use this filter to specify a post or posts whose CSS and JavaScript assets should be loaded globally.
  296. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  297. * @see fl_builder_global_posts
  298. * @since 1.0
  299. */
  300. return apply_filters( 'fl_builder_global_posts', array() );
  301. }
  302. /**
  303. * Adds the given post ID to the beginning of the internal $post_id
  304. * array so the builder will be forced to use that instead of
  305. * a post ID set in the internal $post_data array or the global $post->ID.
  306. *
  307. * @since 1.10
  308. * @param int $post_id
  309. * @return void
  310. */
  311. static public function set_post_id( $post_id ) {
  312. array_unshift( self::$post_id, $post_id );
  313. }
  314. /**
  315. * Removes the first item from the internal $post_id array so the
  316. * last set post ID is used. If the internal $post_id array is
  317. * empty, a post ID set in the internal $post_data array or the global
  318. * $post->ID will be used when calling FLBuilderModel::get_post_id.
  319. *
  320. * @since 1.10
  321. * @return void
  322. */
  323. static public function reset_post_id() {
  324. array_shift( self::$post_id );
  325. }
  326. /**
  327. * Returns the post id for the current post that
  328. * is being displayed or worked on.
  329. *
  330. * @since 1.0
  331. * @param bool $force_globals Force the use of WP globals instead of checking our internal post ID.
  332. * @return int|bool The post id or false.
  333. */
  334. static public function get_post_id( $force_globals = false ) {
  335. // Check our internal post IDs first if we're not forced to use WP globals.
  336. if ( ! $force_globals ) {
  337. $post_data = self::get_post_data();
  338. if ( ! empty( self::$post_id ) ) {
  339. // Get a post ID from the internal $post_id array if not empty.
  340. return self::$post_id[0];
  341. } elseif ( isset( $post_data['post_id'] ) ) {
  342. // Get a post ID from an AJAX request.
  343. return $post_data['post_id'];
  344. }
  345. }
  346. // Check WP globals.
  347. global $wp_the_query;
  348. global $post;
  349. if ( in_the_loop() && is_main_query() && isset( $wp_the_query->post ) ) {
  350. // Get a post ID from the main query.
  351. return $wp_the_query->post->ID;
  352. } elseif ( isset( $post ) ) {
  353. // Get a post ID in a query outside of the main loop.
  354. return $post->ID;
  355. }
  356. // No post ID found.
  357. return false;
  358. }
  359. /**
  360. * Returns the post object for the current post that
  361. * is being worked on.
  362. *
  363. * @since 1.6.3
  364. * @return object
  365. */
  366. static public function get_post() {
  367. return get_post( self::get_post_id() );
  368. }
  369. /**
  370. * Checks to see if the site has SSL enabled or not.
  371. *
  372. * @since 1.0
  373. * @return bool
  374. */
  375. static public function is_ssl() {
  376. if ( is_ssl() ) {
  377. return true;
  378. } elseif ( 0 === stripos( get_option( 'siteurl' ), 'https://' ) ) {
  379. return true;
  380. } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' == $_SERVER['HTTP_X_FORWARDED_PROTO'] ) {
  381. return true;
  382. }
  383. return false;
  384. }
  385. /**
  386. * Checks to see if the builder can be enabled for
  387. * the current post in the main query.
  388. *
  389. * @since 1.0
  390. * @return bool
  391. */
  392. static public function is_post_editable() {
  393. global $wp_the_query;
  394. $editable = false;
  395. if ( is_singular() && isset( $wp_the_query->post ) ) {
  396. $post = $wp_the_query->post;
  397. $post_types = self::get_post_types();
  398. $user_can = current_user_can( 'edit_post', $post->ID );
  399. $user_access = FLBuilderUserAccess::current_user_can( 'builder_access' );
  400. if ( in_array( $post->post_type, $post_types ) && $user_can && $user_access ) {
  401. $editable = true;
  402. }
  403. }
  404. return (bool) apply_filters( 'fl_builder_is_post_editable', $editable );
  405. }
  406. /**
  407. * Called by the heartbeat API. Lock the current post
  408. * so only the current user can edit it.
  409. *
  410. * @since 1.0
  411. * @return void
  412. */
  413. static public function lock_post( $response, $data ) {
  414. if ( isset( $data['fl_builder_post_lock'] ) ) {
  415. require_once ABSPATH . 'wp-admin/includes/post.php';
  416. wp_set_post_lock( $data['fl_builder_post_lock']['post_id'] );
  417. }
  418. }
  419. /**
  420. * Checks to see if the builder layout is enabled
  421. * for the current post.
  422. *
  423. * @since 1.0
  424. * @param int $post_id A post ID to check otherwise, self::get_post_id will be used.
  425. * @return bool
  426. */
  427. static public function is_builder_enabled( $post_id = null ) {
  428. global $wp_the_query;
  429. // If in iframe preview return true as the post might not be a draft yet.
  430. if ( self::is_builder_draft_preview() ) {
  431. return true;
  432. }
  433. $query_id = ( isset( $wp_the_query->post->ID ) ) ? $wp_the_query->post->ID : false;
  434. $post_id = $post_id ? $post_id : self::get_post_id();
  435. if ( ! is_admin() && post_password_required( $post_id ) ) {
  436. return false;
  437. } elseif ( self::is_builder_active() && $query_id === $post_id ) {
  438. return true;
  439. } else {
  440. $post_types = self::get_post_types();
  441. $post = get_post( $post_id );
  442. if ( $post && in_array( $post->post_type, $post_types ) ) {
  443. return get_post_meta( $post->ID, '_fl_builder_enabled', true );
  444. }
  445. }
  446. return false;
  447. }
  448. /**
  449. * Checks to see if the builder UI is active for
  450. * the current post in the main query.
  451. *
  452. * @since 1.0
  453. * @return bool
  454. */
  455. static public function is_builder_active() {
  456. global $wp_the_query;
  457. global $post;
  458. $query_id = ( isset( $wp_the_query->post->ID ) ) ? $wp_the_query->post->ID : false;
  459. $post_id = ( isset( $post->ID ) ) ? $post->ID : false;
  460. if ( null !== self::$active ) {
  461. return self::$active;
  462. } elseif ( ! is_admin() && is_singular() && $query_id != $post_id ) {
  463. self::$active = false;
  464. } elseif ( is_customize_preview() ) {
  465. self::$active = false;
  466. } elseif ( self::is_post_editable() && ! is_admin() && ! post_password_required() ) {
  467. $post_data = self::get_post_data();
  468. self::$active = isset( $_GET['fl_builder'] ) || isset( $post_data['fl_builder'] );
  469. }
  470. return self::$active;
  471. }
  472. /**
  473. * Returns if this is a draft layout preview or not.
  474. *
  475. * @since 2.1
  476. * @return bool
  477. */
  478. static public function is_builder_draft_preview() {
  479. return is_user_logged_in() && isset( $_GET['fl_builder_preview'] );
  480. }
  481. /**
  482. * Checks to see if this is the first time
  483. * a user has launched the builder.
  484. *
  485. * @since 1.4.9
  486. * @return bool
  487. */
  488. static public function is_new_user() {
  489. if ( self::is_builder_active() ) {
  490. $current_user = wp_get_current_user();
  491. $launched = get_user_meta( $current_user->ID, '_fl_builder_launched', true );
  492. if ( empty( $launched ) ) {
  493. update_user_meta( $current_user->ID, '_fl_builder_launched', 1 );
  494. return true;
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * Gets the status to use for working with nodes in
  501. * the database. Returns draft if the builder is active,
  502. * otherwise it returns published.
  503. *
  504. * @since 1.0
  505. * @return string
  506. */
  507. static public function get_node_status() {
  508. $status = self::is_builder_active() ? 'draft' : 'published';
  509. return apply_filters( 'fl_builder_node_status', $status );
  510. }
  511. /**
  512. * Enable the builder layout for the current post.
  513. *
  514. * @since 1.0
  515. * @return void
  516. */
  517. static public function enable() {
  518. update_post_meta( self::get_post_id(), '_fl_builder_enabled', true );
  519. }
  520. /**
  521. * Disable the builder layout for the current post.
  522. *
  523. * @since 1.0
  524. * @return void
  525. */
  526. static public function disable() {
  527. update_post_meta( self::get_post_id(), '_fl_builder_enabled', false );
  528. }
  529. /**
  530. * Enable the builder editor for the main post in the query.
  531. *
  532. * @since 1.0
  533. * @return void
  534. */
  535. static public function enable_editing() {
  536. global $wp_the_query;
  537. if ( self::is_post_editable() && is_object( $wp_the_query->post ) ) {
  538. $post = $wp_the_query->post;
  539. $published = self::get_layout_data( 'published' );
  540. $draft = self::get_layout_data( 'draft' );
  541. $content = apply_filters( 'fl_builder_migrated_post_content', $post->post_content );
  542. // Migrate existing post content to the builder?
  543. if ( empty( $published ) && empty( $draft ) && ! empty( $content ) ) {
  544. $row = self::add_row();
  545. $cols = self::get_nodes( 'column' );
  546. $col = array_shift( $cols );
  547. $settings = self::get_module_defaults( 'rich-text' );
  548. $settings->text = $content;
  549. self::add_module( 'rich-text', $settings, $col->node );
  550. } elseif ( empty( $draft ) ) {
  551. self::update_layout_data( $published, 'draft', $post->ID );
  552. self::update_layout_settings( self::get_layout_settings( 'published' ), 'draft', $post->ID );
  553. }
  554. // Delete old draft asset cache.
  555. self::delete_asset_cache();
  556. // Lock the post.
  557. require_once ABSPATH . 'wp-admin/includes/post.php';
  558. wp_set_post_lock( $post->ID );
  559. // Allow devs to hook into when editing is enabled.
  560. do_action( 'fl_builder_editing_enabled' );
  561. }
  562. }
  563. /**
  564. * Returns an array of paths for the upload directory
  565. * of the current site.
  566. *
  567. * @since 1.0
  568. * @return array
  569. */
  570. static public function get_upload_dir() {
  571. $wp_info = wp_upload_dir( null, false );
  572. $dir_name = basename( FL_BUILDER_DIR );
  573. // We use bb-plugin for the lite version as well.
  574. if ( 'beaver-builder-lite-version' == $dir_name ) {
  575. $dir_name = 'bb-plugin';
  576. }
  577. // SSL workaround.
  578. if ( self::is_ssl() ) {
  579. $wp_info['baseurl'] = str_ireplace( 'http://', 'https://', $wp_info['baseurl'] );
  580. }
  581. // Build the paths.
  582. $dir_info = array(
  583. 'path' => $wp_info['basedir'] . '/' . $dir_name . '/',
  584. 'url' => $wp_info['baseurl'] . '/' . $dir_name . '/',
  585. );
  586. // Create the upload dir if it doesn't exist.
  587. if ( ! fl_builder_filesystem()->file_exists( $dir_info['path'] ) ) {
  588. // Create the directory.
  589. fl_builder_filesystem()->mkdir( $dir_info['path'] );
  590. // Add an index file for security.
  591. fl_builder_filesystem()->file_put_contents( $dir_info['path'] . 'index.html', '' );
  592. }
  593. /**
  594. * Use this filter to modify the upload directory path and URL that the builder uses to store things like the cache and custom icons.
  595. * @see fl_builder_get_upload_dir
  596. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  597. */
  598. return apply_filters( 'fl_builder_get_upload_dir', $dir_info );
  599. }
  600. /**
  601. * Returns an array of paths for the cache directory
  602. * of the current site.
  603. *
  604. * @since 1.0
  605. * @param string $name The name of the cache directory to get paths for.
  606. * @return array
  607. */
  608. static public function get_cache_dir( $name = 'cache' ) {
  609. $upload_info = self::get_upload_dir();
  610. $allowed = array( 'cache', 'icons' );
  611. // Make sure the dir name is allowed.
  612. if ( ! in_array( $name, $allowed ) ) {
  613. return false;
  614. }
  615. // Build the paths.
  616. $dir_info = array(
  617. 'path' => $upload_info['path'] . $name . '/',
  618. 'url' => $upload_info['url'] . $name . '/',
  619. );
  620. // Create the cache dir if it doesn't exist.
  621. if ( ! fl_builder_filesystem()->file_exists( $dir_info['path'] ) ) {
  622. // Create the directory.
  623. fl_builder_filesystem()->mkdir( $dir_info['path'] );
  624. // Add an index file for security.
  625. fl_builder_filesystem()->file_put_contents( $dir_info['path'] . 'index.html', '' );
  626. }
  627. /**
  628. * Use this filter to modify the cache directory path and URL that the builder uses to store cached images, JavaScript, and CSS files.
  629. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  630. * @see fl_builder_get_cache_dir
  631. */
  632. return apply_filters( 'fl_builder_get_cache_dir', $dir_info );
  633. }
  634. /**
  635. * Returns the version number to be applied to the query string
  636. * of a CSS or JS asset. If the builder is active a random hash
  637. * is returned to prevent caching, otherwise a hash of the post
  638. * update time is returned.
  639. *
  640. * @since 1.0
  641. * @return string
  642. */
  643. static public function get_asset_version() {
  644. $post_id = self::get_post_id();
  645. $active = self::is_builder_active();
  646. $preview = self::is_builder_draft_preview();
  647. if ( $active || $preview ) {
  648. return md5( uniqid() );
  649. } else {
  650. return md5( get_post_modified_time( 'U', false, $post_id ) );
  651. }
  652. }
  653. /**
  654. * Returns an array of paths for the CSS and JS assets
  655. * of the current post.
  656. *
  657. * @since 1.0
  658. * @return array
  659. */
  660. static public function get_asset_info() {
  661. $post_data = self::get_post_data();
  662. $post_id = self::get_post_id();
  663. $cache_dir = self::get_cache_dir();
  664. $active = self::is_builder_active();
  665. $preview = self::is_builder_draft_preview();
  666. if ( isset( $post_data['node_preview'] ) ) {
  667. $suffix = '-layout-preview';
  668. } elseif ( $active || $preview ) {
  669. $suffix = '-layout-draft';
  670. } else {
  671. $suffix = '-layout';
  672. }
  673. $info = array(
  674. 'css' => $cache_dir['path'] . $post_id . $suffix . '.css',
  675. 'css_url' => $cache_dir['url'] . $post_id . $suffix . '.css',
  676. 'css_partial' => $cache_dir['path'] . $post_id . $suffix . '-partial.css',
  677. 'css_partial_url' => $cache_dir['url'] . $post_id . $suffix . '-partial.css',
  678. 'js' => $cache_dir['path'] . $post_id . $suffix . '.js',
  679. 'js_url' => $cache_dir['url'] . $post_id . $suffix . '.js',
  680. 'js_partial' => $cache_dir['path'] . $post_id . $suffix . '-partial.js',
  681. 'js_partial_url' => $cache_dir['url'] . $post_id . $suffix . '-partial.js',
  682. );
  683. return $info;
  684. }
  685. /**
  686. * Deletes either the preview, draft or live CSS and/or JS asset cache
  687. * for the current post based on the data returned from get_asset_info.
  688. * Both the CSS and JS asset cache will be delete if a type is not specified.
  689. *
  690. * @since 1.0
  691. * @param string $type The type of cache to delete. Either css or js.
  692. * @return void
  693. */
  694. static public function delete_asset_cache( $type = false ) {
  695. $info = self::get_asset_info();
  696. $types = $type ? array( $type ) : array( 'css', 'css_partial', 'js', 'js_partial' );
  697. foreach ( $types as $type ) {
  698. if ( isset( $info[ $type ] ) && fl_builder_filesystem()->file_exists( $info[ $type ] ) ) {
  699. fl_builder_filesystem()->unlink( $info[ $type ] );
  700. }
  701. }
  702. }
  703. /**
  704. * Deletes preview, draft and live CSS/JS asset cache for the current
  705. * post. If a post ID is supplied, the asset cache will be deleted for
  706. * that post instead.
  707. *
  708. * @since 1.0
  709. * @param int $post_id
  710. * @return void
  711. */
  712. static public function delete_all_asset_cache( $post_id = false ) {
  713. $post_id = $post_id ? $post_id : self::get_post_id();
  714. $cache_dir = self::get_cache_dir();
  715. if ( $post_id ) {
  716. $paths = array(
  717. $cache_dir['path'] . $post_id . '-layout.css',
  718. $cache_dir['path'] . $post_id . '-layout-draft.css',
  719. $cache_dir['path'] . $post_id . '-layout-preview.css',
  720. $cache_dir['path'] . $post_id . '-layout-partial.css',
  721. $cache_dir['path'] . $post_id . '-layout-draft-partial.css',
  722. $cache_dir['path'] . $post_id . '-layout-preview-partial.css',
  723. $cache_dir['path'] . $post_id . '-layout.js',
  724. $cache_dir['path'] . $post_id . '-layout-draft.js',
  725. $cache_dir['path'] . $post_id . '-layout-preview.js',
  726. $cache_dir['path'] . $post_id . '-layout-partial.js',
  727. $cache_dir['path'] . $post_id . '-layout-draft-partial.js',
  728. $cache_dir['path'] . $post_id . '-layout-preview-partial.js',
  729. );
  730. foreach ( $paths as $path ) {
  731. if ( fl_builder_filesystem()->file_exists( $path ) ) {
  732. fl_builder_filesystem()->unlink( $path );
  733. }
  734. }
  735. }
  736. }
  737. /**
  738. * Deletes the asset cache for all posts that contain the node
  739. * template with the supplied post ID.
  740. *
  741. * @since 1.6.3
  742. * @param int $post_id
  743. * @return void
  744. */
  745. static public function delete_node_template_asset_cache( $post_id = false ) {
  746. $posts = self::get_posts_with_global_node_template( $post_id );
  747. if ( ! empty( $posts ) ) {
  748. foreach ( $posts as $post ) {
  749. self::delete_all_asset_cache( $post->ID );
  750. }
  751. }
  752. }
  753. /**
  754. * Deletes preview, draft and live CSS/JS asset cache for all posts.
  755. *
  756. * @since 1.6.3
  757. * @return void
  758. */
  759. static public function delete_asset_cache_for_all_posts() {
  760. $cache_dir = self::get_cache_dir();
  761. $css = glob( $cache_dir['path'] . '*.css' );
  762. $js = glob( $cache_dir['path'] . '*.js' );
  763. if ( is_array( $css ) ) {
  764. array_map( array( fl_builder_filesystem(), 'unlink' ), $css );
  765. }
  766. if ( is_array( $js ) ) {
  767. array_map( array( fl_builder_filesystem(), 'unlink' ), $js );
  768. }
  769. }
  770. /**
  771. * Generates a unique id for a builder node such as a
  772. * row, column or module.
  773. *
  774. * @since 1.0
  775. * @return string
  776. */
  777. static public function generate_node_id() {
  778. $node_id = uniqid();
  779. if ( $node_id == self::$last_generated_node_id ) {
  780. return self::generate_node_id();
  781. }
  782. self::$last_generated_node_id = $node_id;
  783. return $node_id;
  784. }
  785. /**
  786. * Generates new node ids for an array of nodes.
  787. *
  788. * @since 1.0
  789. * @param array $data An array of node data.
  790. * @return array
  791. */
  792. static public function generate_new_node_ids( $data ) {
  793. $map = array();
  794. $nodes = array();
  795. // Map the new node ids to the old.
  796. foreach ( $data as $node_id => $node ) {
  797. $map[ $node_id ] = self::generate_node_id();
  798. }
  799. // Replace the old node ids.
  800. foreach ( $data as $node_id => $node ) {
  801. $nodes[ $map[ $node_id ] ] = $node;
  802. $nodes[ $map[ $node_id ] ]->node = $map[ $node_id ];
  803. if ( ! empty( $node->parent ) && isset( $map[ $node->parent ] ) ) {
  804. $nodes[ $map[ $node_id ] ]->parent = $map[ $node->parent ];
  805. }
  806. }
  807. return $nodes;
  808. }
  809. /**
  810. * Returns a single node.
  811. *
  812. * @since 1.0
  813. * @param string|object $node_id Either a node id or node object.
  814. * @param string $status The node status. Either draft or published.
  815. * @return object
  816. */
  817. static public function get_node( $node_id = null, $status = null ) {
  818. if ( is_object( $node_id ) ) {
  819. $node = $node_id;
  820. } else {
  821. $data = self::get_layout_data( $status );
  822. $node = isset( $data[ $node_id ] ) ? $data[ $node_id ] : null;
  823. }
  824. if ( $node && ! empty( $node->settings ) ) {
  825. $node->settings = self::get_node_settings( $node );
  826. }
  827. return $node;
  828. }
  829. /**
  830. * Returns an array of nodes.
  831. *
  832. * @since 1.0
  833. * @param string $type The type of nodes to return.
  834. * @param string|object $parent_id Either the parent node id or parent node object.
  835. * @param string $status The node status. Either draft or published.
  836. * @return array
  837. */
  838. static public function get_nodes( $type = null, $parent_id = null, $status = null ) {
  839. $parent = is_object( $parent_id ) ? $parent_id : self::get_node( $parent_id );
  840. $nodes = array();
  841. // Get the layout data.
  842. if ( ! $parent ) {
  843. $data = self::get_layout_data( $status );
  844. } else {
  845. $data = self::get_child_nodes( $parent, $status );
  846. }
  847. // Return all nodes?
  848. if ( ! $type ) {
  849. $nodes = $data;
  850. } else {
  851. foreach ( $data as $node_id => $node ) {
  852. if ( $node->type == $type ) {
  853. $nodes[ $node_id ] = $node;
  854. }
  855. }
  856. }
  857. // Sort the nodes by position.
  858. uasort( $nodes, array( 'FLBuilderModel', 'order_nodes' ) );
  859. // Merge default settings.
  860. foreach ( $nodes as $node_id => $node ) {
  861. if ( ! empty( $node->settings ) ) {
  862. $nodes[ $node_id ]->settings = self::get_node_settings( $nodes[ $node_id ] );
  863. }
  864. }
  865. // Return the nodes.
  866. return $nodes;
  867. }
  868. /**
  869. * Returns the direct parent object for a single node.
  870. *
  871. * @since 1.9
  872. * @param string|object $node_id Either a node id or node object.
  873. * @param string $status The node status. Either draft or published.
  874. * @return object
  875. */
  876. static public function get_node_parent( $node_id = null, $status = null ) {
  877. $parent = null;
  878. if ( is_object( $node_id ) ) {
  879. $node = $node_id;
  880. } else {
  881. $node = self::get_node( $node_id, $status );
  882. }
  883. if ( $node ) {
  884. $template_post_id = self::is_node_global( $node );
  885. $post_id = $template_post_id ? $template_post_id : self::get_post_id();
  886. $data = self::get_layout_data( $status, $post_id );
  887. if ( isset( $data[ $node->parent ] ) ) {
  888. return $data[ $node->parent ];
  889. }
  890. }
  891. return $parent;
  892. }
  893. /**
  894. * Returns a node's parent node of the specified type.
  895. *
  896. * @since 1.8.3
  897. * @param string|object $node The node ID. Can also be a node object.
  898. * @param string $type The type of parent to return. Either "column", "column-group" or "row".
  899. * @return object The parent node.
  900. */
  901. static public function get_node_parent_by_type( $node, $type = '' ) {
  902. // Get node object if node ID set
  903. if ( ! is_object( $node ) ) {
  904. $node = self::get_node( $node );
  905. }
  906. // Return early if no node object found or node has no parent
  907. if ( empty( $node ) || empty( $node->parent ) ) {
  908. return;
  909. }
  910. // Helper array of parent types and their categories for each node type
  911. $parent_types = array(
  912. 'module' => array(
  913. 'type' => 'column',
  914. 'category' => 'columns',
  915. ),
  916. 'column' => array(
  917. 'type' => 'column-group',
  918. 'category' => 'groups',
  919. ),
  920. 'column-group' => array(
  921. 'type' => 'row',
  922. 'category' => 'rows',
  923. ),
  924. );
  925. // Helper array of node type hierarchies
  926. $hierarchy = array(
  927. 'module' => 10,
  928. 'column' => 20,
  929. 'column-group' => 30,
  930. 'row' => 40,
  931. );
  932. // Set immediate parent type of the node when:
  933. // - type is not of allowed types
  934. // - type is the same as node type
  935. // - type is lower in hierarchy than the node type
  936. if ( ! in_array( $type, array_keys( $hierarchy ) ) || $type == $node->type || $hierarchy[ $parent_types[ $node->type ]['type'] ] > $hierarchy[ $type ] ) {
  937. $type = $parent_types[ $node->type ]['type'];
  938. }
  939. // Get all layout nodes, categorized
  940. $nodes = array_filter( self::get_categorized_nodes() );
  941. // Null out the output initially
  942. $output = '';
  943. // Parse layout nodes to get the correct output
  944. if ( ! empty( $nodes ) ) {
  945. while ( empty( $output ) ) {
  946. if ( ! empty( $node->parent ) && isset( $nodes[ $parent_types[ $node->type ]['category'] ] ) ) {
  947. $break_while = true;
  948. foreach ( $nodes[ $parent_types[ $node->type ]['category'] ] as $parent ) {
  949. if ( $parent->node == $node->parent ) {
  950. $break_while = false;
  951. if ( $parent_types[ $node->type ]['type'] == $type ) {
  952. // We have got the type we wanted! Set the output and break from while and foreach loops.
  953. $output = $parent;
  954. break; // From foreach
  955. }
  956. // We now need node parents to crawl the tree
  957. $node = $parent;
  958. break; // From foreach
  959. }
  960. }
  961. // If we get this far without changing $break_while, something is wrong
  962. if ( $break_while ) {
  963. break; // From while
  964. }
  965. } else {
  966. break; // From while
  967. }
  968. }
  969. }
  970. return $output;
  971. }
  972. /**
  973. * Returns an array of child nodes for a parent.
  974. *
  975. * @since 1.0
  976. * @param string|object $parent_id Either the parent node id or parent node object.
  977. * @param string $status The node status. Either draft or published.
  978. * @return array
  979. */
  980. static public function get_child_nodes( $parent_id, $status = null ) {
  981. $parent = is_object( $parent_id ) ? $parent_id : self::get_node( $parent_id );
  982. $template_post_id = self::is_node_global( $parent );
  983. $template_node_id = null;
  984. $status = $template_post_id && ! self::is_post_node_template() ? 'published' : $status;
  985. $data = self::get_layout_data( $status, $template_post_id );
  986. $nodes = array();
  987. if ( $template_post_id ) {
  988. $template_node_id = apply_filters( 'fl_builder_parent_template_node_id', $parent->template_node_id, $parent, $data );
  989. }
  990. if ( is_object( $parent ) ) {
  991. foreach ( $data as $node_id => $node ) {
  992. if ( ( isset( $node->parent ) && $node->parent == $parent->node )
  993. || ( $template_node_id && $template_node_id == $node->parent ) ) {
  994. $nodes[ $node_id ] = $node;
  995. }
  996. }
  997. }
  998. return $nodes;
  999. }
  1000. /**
  1001. * Returns all child nodes and children of those children
  1002. * for a single node.
  1003. *
  1004. * @since 1.6.3
  1005. * @param string $parent_id The parent node id.
  1006. * @return array
  1007. */
  1008. static public function get_nested_nodes( $parent_id ) {
  1009. $children = self::get_child_nodes( $parent_id );
  1010. foreach ( $children as $child_id => $child ) {
  1011. $grand_children = self::get_child_nodes( $child_id );
  1012. if ( count( $grand_children ) > 0 ) {
  1013. $children = array_merge( $children, $grand_children );
  1014. foreach ( $grand_children as $grand_child_id => $grand_child ) {
  1015. $nested = self::get_nested_nodes( $grand_child_id );
  1016. if ( count( $nested ) > 0 ) {
  1017. $children = array_merge( $children, $nested );
  1018. }
  1019. }
  1020. }
  1021. }
  1022. return $children;
  1023. }
  1024. /**
  1025. * Returns an array of all nodes for a layout, categorized by type.
  1026. *
  1027. * @since 1.6.3
  1028. * @return array
  1029. */
  1030. static public function get_categorized_nodes() {
  1031. $nodes = array(
  1032. 'rows' => array(),
  1033. 'groups' => array(),
  1034. 'columns' => array(),
  1035. 'modules' => array(),
  1036. );
  1037. if ( self::is_post_user_template( 'module' ) ) {
  1038. $nodes['modules'] = self::get_all_modules();
  1039. } elseif ( self::is_post_user_template( 'column' ) ) {
  1040. $root_col = self::get_node_template_root( 'column' );
  1041. $nodes['columns'][ $root_col->node ] = $root_col;
  1042. $col_children = self::get_nodes( null, $root_col );
  1043. foreach ( $col_children as $col_child ) {
  1044. if ( 'module' == $col_child->type ) {
  1045. $module = self::get_module( $col_child );
  1046. if ( $module ) {
  1047. $nodes['modules'][ $col_child->node ] = $module;
  1048. }
  1049. } elseif ( 'column-group' == $col_child->type ) {
  1050. $nodes['groups'][ $col_child->node ] = $col_child;
  1051. $group_cols = self::get_nodes( 'column', $col_child );
  1052. foreach ( $group_cols as $group_col ) {
  1053. $nodes['columns'][ $group_col->node ] = $group_col;
  1054. $modules = self::get_modules( $group_col );
  1055. foreach ( $modules as $module ) {
  1056. $nodes['modules'][ $module->node ] = $module;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. } else {
  1062. $rows = self::get_nodes( 'row' );
  1063. foreach ( $rows as $row ) {
  1064. $nodes['rows'][ $row->node ] = $row;
  1065. $groups = self::get_nodes( 'column-group', $row );
  1066. foreach ( $groups as $group ) {
  1067. $nodes['groups'][ $group->node ] = $group;
  1068. $cols = self::get_nodes( 'column', $group );
  1069. foreach ( $cols as $col ) {
  1070. $nodes['columns'][ $col->node ] = $col;
  1071. $col_children = self::get_nodes( null, $col );
  1072. foreach ( $col_children as $col_child ) {
  1073. if ( 'module' == $col_child->type ) {
  1074. $module = self::get_module( $col_child );
  1075. if ( $module ) {
  1076. $nodes['modules'][ $col_child->node ] = $module;
  1077. }
  1078. } elseif ( 'column-group' == $col_child->type ) {
  1079. $nodes['groups'][ $col_child->node ] = $col_child;
  1080. $group_cols = self::get_nodes( 'column', $col_child );
  1081. foreach ( $group_cols as $group_col ) {
  1082. $nodes['columns'][ $group_col->node ] = $group_col;
  1083. $modules = self::get_modules( $group_col );
  1084. foreach ( $modules as $module ) {
  1085. $nodes['modules'][ $module->node ] = $module;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. return $nodes;
  1095. }
  1096. /**
  1097. * Returns node settings that are merged with the
  1098. * default or preview settings.
  1099. *
  1100. * @since 1.0
  1101. * @param object|string $node A node object or node ID.
  1102. * @param bool $filter Whether to filter the settings or not.
  1103. * @return object
  1104. */
  1105. static public function get_node_settings( $node, $filter = true ) {
  1106. $node = is_object( $node ) ? $node : self::get_node( $node );
  1107. $post_data = self::get_post_data();
  1108. // Get the node settings for a node template's root node?
  1109. if ( self::is_node_template_root( $node ) && ! self::is_post_node_template() ) {
  1110. $template_post_id = self::get_node_template_post_id( $node->template_id );
  1111. $template_data = self::get_layout_data( 'published', $template_post_id );
  1112. // Fallback to draft data if we don't have published data.
  1113. if ( ! isset( $template_data[ $node->template_node_id ] ) ) {
  1114. $template_data = self::get_layout_data( 'draft', $template_post_id );
  1115. }
  1116. // Set the node settings to the template node settings.
  1117. if ( isset( $template_data[ $node->template_node_id ] ) ) {
  1118. $template_node = $template_data[ $node->template_node_id ];
  1119. $template_settings = clone $template_node->settings;
  1120. if ( 'column' == $node->type ) {
  1121. $template_settings->size = $node->settings->size;
  1122. }
  1123. $node->settings = $template_settings;
  1124. }
  1125. }
  1126. // Get either the preview settings or saved node settings merged with the defaults.
  1127. if ( isset( $post_data['node_preview'] ) && isset( $post_data['node_id'] ) && $post_data['node_id'] == $node->node ) {
  1128. if ( ! isset( $post_data['node_preview_processed_settings'] ) ) {
  1129. $settings = $post_data['node_preview'];
  1130. $settings = (object) array_merge( (array) $node->settings, (array) $settings );
  1131. $settings = self::process_node_settings( $node, $settings );
  1132. self::update_post_data( 'node_preview_processed_settings', $settings );
  1133. } else {
  1134. $settings = $post_data['node_preview_processed_settings'];
  1135. }
  1136. } else {
  1137. $defaults = self::get_node_defaults( $node );
  1138. $settings = (object) array_merge( (array) $defaults, (array) $node->settings );
  1139. if ( 'module' == $node->type ) {
  1140. $settings = self::merge_nested_module_defaults( $node->settings->type, $settings );
  1141. } elseif ( 'column' == $node->type ) {
  1142. $settings = self::merge_nested_form_defaults( 'general', 'col', $settings );
  1143. } elseif ( 'row' == $node->type ) {
  1144. $settings = self::merge_nested_form_defaults( 'general', 'row', $settings );
  1145. }
  1146. }
  1147. return ! $filter ? $settings : apply_filters( 'fl_builder_node_settings', $settings, $node );
  1148. }
  1149. /**
  1150. * Returns node settings that have been processed with
  1151. * specific logic based on the type of node.
  1152. *
  1153. * @since 1.0
  1154. * @param object $node A node object.
  1155. * @param object $new_settings The new node settings.
  1156. * @return object
  1157. */
  1158. static public function process_node_settings( $node, $new_settings ) {
  1159. if ( 'row' == $node->type ) {
  1160. $new_settings = self::process_row_settings( $node, $new_settings );
  1161. $new_settings = self::sanitize_settings( $new_settings, 'row', 'general' );
  1162. }
  1163. if ( 'column' == $node->type ) {
  1164. $new_settings = self::process_col_settings( $node, $new_settings );
  1165. $new_settings = self::sanitize_settings( $new_settings, 'col', 'general' );
  1166. }
  1167. if ( 'module' == $node->type ) {
  1168. $new_settings = self::process_module_settings( $node, $new_settings );
  1169. $new_settings = self::sanitize_settings( $new_settings, $node->settings->type, 'module' );
  1170. }
  1171. return $new_settings;
  1172. }
  1173. /**
  1174. * Returns the default settings for a node.
  1175. *
  1176. * @since 1.0
  1177. * @param object $node A node object.
  1178. * @return object
  1179. */
  1180. static public function get_node_defaults( $node ) {
  1181. $defaults = array();
  1182. if ( 'row' == $node->type ) {
  1183. $defaults = self::get_row_defaults();
  1184. } elseif ( 'column' == $node->type ) {
  1185. $defaults = self::get_col_defaults();
  1186. } elseif ( 'module' == $node->type ) {
  1187. $defaults = self::get_module_defaults( $node->settings->type );
  1188. }
  1189. return $defaults;
  1190. }
  1191. /**
  1192. * Callback for the uasort function.
  1193. *
  1194. * @since 1.0
  1195. * @param int $a The first position.
  1196. * @param int $b The second position.
  1197. * @return int
  1198. */
  1199. static public function order_nodes( $a, $b ) {
  1200. return (int) $a->position - (int) $b->position;
  1201. }
  1202. /**
  1203. * Counts the number of nodes in a parent.
  1204. *
  1205. * @since 1.0
  1206. * @param string $type The type of nodes to count.
  1207. * @param string $parent_id The parent node id.
  1208. * @return int
  1209. */
  1210. static public function count_nodes( $type = 'row', $parent_id = null ) {
  1211. return count( self::get_nodes( $type, $parent_id ) );
  1212. }
  1213. /**
  1214. * Returns the index of the next available
  1215. * position in a parent node.
  1216. *
  1217. * @since 1.0
  1218. * @param string $type The type of nodes to count.
  1219. * @param string $parent_id The parent node id.
  1220. * @return int
  1221. */
  1222. static public function next_node_position( $type = 'row', $parent_id = null ) {
  1223. $nodes = self::get_nodes( $type, $parent_id );
  1224. $last = array_pop( $nodes );
  1225. return $last ? $last->position + 1 : 0;
  1226. }
  1227. /**
  1228. * Deletes a node.
  1229. *
  1230. * @since 1.0
  1231. * @param string $node_id The ID of the node to delete.
  1232. * @return void
  1233. */
  1234. static public function delete_node( $node_id = null ) {
  1235. // Get the layout data.
  1236. $data = self::get_layout_data();
  1237. // Return if the node doesn't exist.
  1238. if ( ! isset( $data[ $node_id ] ) ) {
  1239. return;
  1240. }
  1241. // Get the node.
  1242. $node = $data[ $node_id ];
  1243. // Call the delete method if we're deleting a module.
  1244. self::call_module_delete( $node );
  1245. // Delete the node.
  1246. unset( $data[ $node_id ] );
  1247. // Get the sibling nodes.
  1248. if ( 'row' === $node->type ) {
  1249. $siblings = self::get_nodes( 'row' );
  1250. } else {
  1251. $siblings = self::get_nodes( null, $node->parent );
  1252. }
  1253. // Reorder sibling nodes.
  1254. $position = 0;
  1255. foreach ( $siblings as $sibling_id => $sibling ) {
  1256. if ( isset( $data[ $sibling_id ] ) ) {
  1257. $data[ $sibling_id ]->position = $position;
  1258. $position++;
  1259. }
  1260. }
  1261. // Delete the node's children.
  1262. self::delete_child_nodes_from_data( $node, $data );
  1263. // Update the layout data.
  1264. self::update_layout_data( $data );
  1265. }
  1266. /**
  1267. * Deletes all child nodes for a parent.
  1268. *
  1269. * @since 1.0
  1270. * @param object $parent The parent node object.
  1271. * @param object $data The data array to delete from.
  1272. * @return void
  1273. */
  1274. static public function delete_child_nodes_from_data( $parent = null, &$data ) {
  1275. $children = self::get_nodes( null, $parent );
  1276. foreach ( $children as $child_id => $child ) {
  1277. // Call the delete method if we're deleting a module.
  1278. self::call_module_delete( $child );
  1279. // Delete the node.
  1280. unset( $data[ $child_id ] );
  1281. // Delete the node's children.
  1282. self::delete_child_nodes_from_data( $child, $data );
  1283. }
  1284. }
  1285. /**
  1286. * Calls the delete method for a node
  1287. * that is a module.
  1288. *
  1289. * @since 1.0
  1290. * @param object $node A module node.
  1291. * @return void
  1292. */
  1293. static public function call_module_delete( $node ) {
  1294. if ( 'module' == $node->type && isset( self::$modules[ $node->settings->type ] ) ) {
  1295. $class = get_class( self::$modules[ $node->settings->type ] );
  1296. $instance = new $class();
  1297. $instance->node = $node->node;
  1298. $instance->parent = $node->parent;
  1299. $instance->settings = $node->settings;
  1300. $instance->delete();
  1301. $instance->remove();
  1302. }
  1303. }
  1304. /**
  1305. * Repositions a node within a parent.
  1306. *
  1307. * @since 1.0
  1308. * @param string $node_id A node ID.
  1309. * @param int $position The new position.
  1310. * @param string $type The type of node to order.
  1311. * @return void
  1312. */
  1313. static public function reorder_node( $node_id = null, $position = 0 ) {
  1314. $data = self::get_layout_data();
  1315. $node = $data[ $node_id ];
  1316. $type = ! $node->parent ? $node->type : null;
  1317. $nodes = self::get_nodes( $type, $node->parent );
  1318. $new_pos = 0;
  1319. // Make sure node positions start at zero.
  1320. foreach ( $nodes as $node ) {
  1321. $data[ $node->node ]->position = $new_pos;
  1322. $new_pos++;
  1323. }
  1324. // Get the node and remove it from the array.
  1325. $node = $data[ $node_id ];
  1326. $removed = array_splice( $nodes, $node->position, 1 );
  1327. $new_pos = 0;
  1328. // Reposition it in the array.
  1329. array_splice( $nodes, $position, 0, $removed );
  1330. // Update the position data.
  1331. foreach ( $nodes as $node ) {
  1332. $data[ $node->node ]->position = $new_pos;
  1333. $new_pos++;
  1334. }
  1335. // Update the layout data.
  1336. self::update_layout_data( $data );
  1337. }
  1338. /**
  1339. * Moves a node to another parent.
  1340. *
  1341. * @since 1.0
  1342. * @param string $node_id ID of the node to move.
  1343. * @param int $new_parent_id ID of the new parent.
  1344. * @param int $position The position in the new parent.
  1345. * @return void
  1346. */
  1347. static public function move_node( $node_id = null, $new_parent_id = null, $position = 0 ) {
  1348. $data = self::get_layout_data();
  1349. $new_parent = self::get_node( $new_parent_id );
  1350. $node = self::get_node( $node_id );
  1351. $siblings = self::get_nodes( null, $node->parent );
  1352. $sibling_pos = 0;
  1353. // Set the node's new parent.
  1354. $data[ $node_id ]->parent = $new_parent->node;
  1355. // Remove the node from the $siblings array.
  1356. unset( $siblings[ $node_id ] );
  1357. // Reorder old siblings.
  1358. foreach ( $siblings as $sibling ) {
  1359. $data[ $sibling->node ]->position = $sibling_pos;
  1360. $sibling_pos++;
  1361. }
  1362. // Update the layout data.
  1363. self::update_layout_data( $data );
  1364. // Set the node's new order.
  1365. self::reorder_node( $node_id, $position );
  1366. }
  1367. /**
  1368. * Adds a row to the current layout.
  1369. *
  1370. * @since 1.0
  1371. * @param string $cols The type of column layout to use.
  1372. * @param int $position The position of the new row.
  1373. * @return object The new row object.
  1374. */
  1375. static public function add_row( $cols = '1-col', $position = false ) {
  1376. $data = self::get_layout_data();
  1377. $settings = self::get_row_defaults();
  1378. $row_node_id = self::generate_node_id();
  1379. // Add the row.
  1380. $data[ $row_node_id ] = new StdClass();
  1381. $data[ $row_node_id ]->node = $row_node_id;
  1382. $data[ $row_node_id ]->type = 'row';
  1383. $data[ $row_node_id ]->parent = null;
  1384. $data[ $row_node_id ]->position = self::next_node_position( 'row' );
  1385. $data[ $row_node_id ]->settings = $settings;
  1386. // Update the layout data.
  1387. self::update_layout_data( $data );
  1388. // Position the row.
  1389. if ( false !== $position ) {
  1390. self::reorder_node( $row_node_id, $position );
  1391. }
  1392. // Add a column group.
  1393. self::add_col_group( $row_node_id, $cols, 0 );
  1394. // Return the updated row.
  1395. return self::get_node( $row_node_id );
  1396. }
  1397. /**
  1398. * Copys a row and adds it to the current layout.
  1399. *
  1400. * @since 1.0
  1401. * @param string $node_id Node ID of the row to copy.
  1402. * @param object $settings These settings will be used for the copy if present.
  1403. * @param string $settings_id The ID of the node who's settings were passed.
  1404. * @return void
  1405. */
  1406. static public function copy_row( $node_id = null, $settings = null, $settings_id = null ) {
  1407. $layout_data = self::get_layout_data();
  1408. $row = self::get_node( $node_id );
  1409. $new_row_id = self::generate_node_id();
  1410. $col_groups = self::get_nodes( 'column-group', $row );
  1411. $new_nodes = array();
  1412. $template_cols = array();
  1413. // Add the new row.
  1414. $layout_data[ $new_row_id ] = clone $row;
  1415. $layout_data[ $new_row_id ]->settings = clone $row->settings;
  1416. $layout_data[ $new_row_id ]->node = $new_row_id;
  1417. // Unset row template data.
  1418. if ( isset( $layout_data[ $new_row_id ]->template_id ) ) {
  1419. unset( $layout_data[ $new_row_id ]->template_id );
  1420. unset( $layout_data[ $new_row_id ]->template_node_id );
  1421. unset( $layout_data[ $new_row_id ]->template_root_node );
  1422. }
  1423. // Get the new child nodes.
  1424. foreach ( $col_groups as $col_group ) {
  1425. $new_nodes[ $col_group->node ] = clone $col_group;
  1426. $cols = self::get_nodes( 'column', $col_group );
  1427. foreach ( $cols as $col ) {
  1428. $new_nodes[ $col->node ] = clone $col;
  1429. $new_nodes[ $col->node ]->settings = clone $col->settings;
  1430. $nodes = self::get_nodes( null, $col );
  1431. foreach ( $nodes as $node ) {
  1432. $new_nodes[ $node->node ] = clone $node;
  1433. if ( 'module' == $node->type ) {
  1434. $new_nodes[ $node->node ]->settings = self::clone_module_settings( $node->settings );
  1435. } elseif ( 'column-group' == $node->type ) {
  1436. $nested_cols = self::get_nodes( 'column', $node );
  1437. foreach ( $nested_cols as $nested_col ) {
  1438. $new_nodes[ $nested_col->node ] = clone $nested_col;
  1439. $new_nodes[ $nested_col->node ]->settings = clone $nested_col->settings;
  1440. $modules = self::get_nodes( 'module', $nested_col );
  1441. foreach ( $modules as $module ) {
  1442. $new_nodes[ $module->node ] = clone $module;
  1443. $new_nodes[ $module->node ]->settings = self::clone_module_settings( $module->settings );
  1444. }
  1445. }
  1446. }
  1447. }
  1448. }
  1449. }
  1450. // Apply settings that were passed if we have them.
  1451. if ( $settings && $settings_id ) {
  1452. if ( $settings_id === $row->node ) {
  1453. $layout_data[ $new_row_id ]->settings = (object) array_merge( (array) $row->settings, (array) $settings );
  1454. } else {
  1455. $new_nodes[ $settings_id ]->settings = (object) array_merge( (array) $new_nodes[ $settings_id ]->settings, (array) $settings );
  1456. }
  1457. }
  1458. // Generate new child ids.
  1459. $new_nodes = self::generate_new_node_ids( $new_nodes );
  1460. // Set col group parent ids to the new row id and unset template data.
  1461. foreach ( $new_nodes as $child_node_id => $child ) {
  1462. // Check for column template's new node id.
  1463. if ( isset( $child->template_node_id ) ) {
  1464. $template_cols[ $child->template_node_id ] = $child_node_id;
  1465. }
  1466. if ( 'column-group' == $child->type ) {
  1467. if ( $child->parent == $row->node || ( isset( $row->template_node_id ) && $child->parent == $row->template_node_id ) ) {
  1468. $new_nodes[ $child_node_id ]->parent = $new_row_id;
  1469. }
  1470. } elseif ( 'module' == $child->type ) {
  1471. if ( isset( $template_cols[ $child->parent ] ) ) {
  1472. $new_nodes[ $child_node_id ]->parent = $template_cols[ $child->parent ];
  1473. }
  1474. }
  1475. if ( isset( $new_nodes[ $child_node_id ]->template_id ) ) {
  1476. unset( $new_nodes[ $child_node_id ]->template_id );
  1477. unset( $new_nodes[ $child_node_id ]->template_node_id );
  1478. }
  1479. }
  1480. // Merge the child data.
  1481. $layout_data = array_merge( $layout_data, $new_nodes );
  1482. // Update the layout data.
  1483. self::update_layout_data( $layout_data );
  1484. // Position the new row.
  1485. self::reorder_node( $new_row_id, $row->position + 1 );
  1486. // Return the new row.
  1487. return self::get_node( $new_row_id );
  1488. }
  1489. /**
  1490. * Returns the default settings for row nodes.
  1491. *
  1492. * @since 1.0
  1493. * @return object
  1494. */
  1495. static public function get_row_defaults() {
  1496. $settings = self::get_settings_form_defaults( 'row' );
  1497. $settings = self::merge_nested_form_defaults( 'general', 'row', $settings );
  1498. return $settings;
  1499. }
  1500. /**
  1501. * Returns an array of spacing placeholders for row
  1502. * margins and padding.
  1503. *
  1504. * @since 1.9
  1505. * @return array
  1506. */
  1507. static public function get_row_spacing_placeholders() {
  1508. $settings = FLBuilderModel::get_global_settings();
  1509. $placeholders = array();
  1510. // Default.
  1511. $placeholders['row_margins'] = $settings->row_margins;
  1512. $placeholders['row_padding'] = $settings->row_padding;
  1513. // Medium.
  1514. $placeholders['row_margins_medium'] = ( '' != $settings->row_margins_medium ) ? $settings->row_margins_medium : $settings->row_margins;
  1515. $placeholders['row_padding_medium'] = ( '' != $settings->row_padding_medium ) ? $settings->row_padding_medium : $settings->row_padding;
  1516. // Responsive row margins.
  1517. if ( '' != $settings->row_margins_responsive ) {
  1518. $placeholders['row_margins_responsive'] = $settings->row_margins_responsive;
  1519. } elseif ( $settings->auto_spacing ) {
  1520. $placeholders['row_margins_responsive'] = 0;
  1521. } else {
  1522. $placeholders['row_margins_responsive'] = $placeholders['row_margins_medium'];
  1523. }
  1524. // Responsive row padding.
  1525. if ( '' != $settings->row_padding_responsive ) {
  1526. $placeholders['row_padding_tb_responsive'] = $settings->row_padding_responsive;
  1527. $placeholders['row_padding_lr_responsive'] = $settings->row_padding_responsive;
  1528. } elseif ( $settings->auto_spacing ) {
  1529. $placeholders['row_padding_tb_responsive'] = $placeholders['row_padding_medium'];
  1530. $placeholders['row_padding_lr_responsive'] = 0;
  1531. } else {
  1532. $placeholders['row_padding_tb_responsive'] = $placeholders['row_padding_medium'];
  1533. $placeholders['row_padding_lr_responsive'] = $placeholders['row_padding_medium'];
  1534. }
  1535. return $placeholders;
  1536. }
  1537. /**
  1538. * Runs row specific logic on new row settings.
  1539. *
  1540. * @since 1.0
  1541. * @param object $row A row node.
  1542. * @param object $new_settings The new settings object.
  1543. * @return object
  1544. */
  1545. static public function process_row_settings( $row, $new_settings ) {
  1546. // Cache background video data.
  1547. if ( 'video' == $new_settings->bg_type ) {
  1548. // Video Fallback Photo
  1549. if ( ! empty( $new_settings->bg_video_fallback_src ) ) {
  1550. $fallback = $new_settings->bg_video_fallback_src;
  1551. } else {
  1552. $fallback = '';
  1553. }
  1554. if ( 'wordpress' == $new_settings->bg_video_source ) {
  1555. // Video MP4
  1556. $mp4 = FLBuilderPhoto::get_attachment_data( $new_settings->bg_video );
  1557. if ( $mp4 ) {
  1558. $parts = explode( '.', $mp4->filename );
  1559. $mp4->extension = array_pop( $parts );
  1560. $new_settings->bg_video_data = $mp4;
  1561. $new_settings->bg_video_data->fallback = $fallback;
  1562. }
  1563. // Video WebM
  1564. $webm = FLBuilderPhoto::get_attachment_data( $new_settings->bg_video_webm );
  1565. if ( $webm ) {
  1566. $parts = explode( '.', $webm->filename );
  1567. $webm->extension = array_pop( $parts );
  1568. $new_settings->bg_video_webm_data = $webm;
  1569. $new_settings->bg_video_webm_data->fallback = $fallback;
  1570. }
  1571. }
  1572. }
  1573. // Cache background slideshow data.
  1574. if ( 'slideshow' == $new_settings->bg_type && 'wordpress' == $new_settings->ss_source ) {
  1575. // Make sure we have a photo data object.
  1576. if ( ! isset( $row->settings->ss_photo_data ) ) {
  1577. $row->settings->ss_photo_data = new StdClass();
  1578. }
  1579. // Hijack the slideshow module to get WordPress photo data.
  1580. $ss = new FLSlideshowModule();
  1581. $ss->settings = new StdClass();
  1582. $ss->settings->photos = $new_settings->ss_photos;
  1583. $ss->settings->photo_data = $row->settings->ss_photo_data;
  1584. $new_settings->ss_photo_data = $ss->get_wordpress_photos();
  1585. }
  1586. return $new_settings;
  1587. }
  1588. /**
  1589. * Returns background data for a row.
  1590. *
  1591. * @since 1.0
  1592. * @param object $row A row node.
  1593. * @return object
  1594. */
  1595. static public function get_row_bg_data( $row ) {
  1596. $data = null;
  1597. // Background Video
  1598. if ( 'video' == $row->settings->bg_type ) {
  1599. if ( isset( $row->settings->bg_video_data ) ) {
  1600. $data = array();
  1601. $data['mp4'] = $row->settings->bg_video_data;
  1602. }
  1603. if ( isset( $row->settings->bg_video_webm_data ) ) {
  1604. if ( ! $data ) {
  1605. $data = array();
  1606. }
  1607. $data['webm'] = $row->settings->bg_video_webm_data;
  1608. }
  1609. } elseif ( 'slideshow' == $row->settings->bg_type && isset( $row->settings->ss_photo_data ) ) {
  1610. $data = $row->settings->ss_photo_data;
  1611. }
  1612. return $data;
  1613. }
  1614. /**
  1615. * Returns the source for a row background slideshow.
  1616. *
  1617. * @since 1.0
  1618. * @param object $row A row node.
  1619. * @return string
  1620. */
  1621. static public function get_row_slideshow_source( $row ) {
  1622. // Make sure we have a photo data object.
  1623. if ( ! isset( $row->settings->ss_photo_data ) ) {
  1624. $row->settings->ss_photo_data = new StdClass();
  1625. }
  1626. // This class does not exist in Lite version.
  1627. if ( ! class_exists( 'FLSlideshowModule' ) ) {
  1628. return false;
  1629. }
  1630. // Hijack the slideshow module to get the source.
  1631. $ss = new FLSlideshowModule();
  1632. $ss->settings = new StdClass();
  1633. $ss->settings->source = $row->settings->ss_source;
  1634. $ss->settings->photos = $row->settings->ss_photos;
  1635. $ss->settings->feed_url = $row->settings->ss_feed_url;
  1636. $ss->settings->photo_data = $row->settings->ss_photo_data;
  1637. // Return the slideshow source.
  1638. return $ss->get_source();
  1639. }
  1640. /**
  1641. * Set the max-width of a specific row.
  1642. *
  1643. * @since 2.0
  1644. * @param int Row node id
  1645. * @param int Width
  1646. * @return void
  1647. */
  1648. static public function resize_row_content( $node_id, $width ) {
  1649. $data = self::get_layout_data();
  1650. $row = self::get_node( $node_id );
  1651. $row->settings->max_content_width = $width;
  1652. $data[ $node_id ] = $row;
  1653. self::update_layout_data( $data );
  1654. }
  1655. /**
  1656. * Adds a column group to a row in the current layout.
  1657. *
  1658. * @since 1.0
  1659. * @param string $node_id A row node ID.
  1660. * @param string $cols The type of column group layout or the ID of an existing column to add.
  1661. * @param int $position The position of the new column group.
  1662. * @return object The new column group object.
  1663. */
  1664. static public function add_col_group( $node_id = null, $cols = '1-col', $position = false ) {
  1665. $data = self::get_layout_data();
  1666. $group_node_id = self::generate_node_id();
  1667. $parent = self::get_node( $node_id );
  1668. $old_group = null;
  1669. // Add the column group.
  1670. $data[ $group_node_id ] = new StdClass();
  1671. $data[ $group_node_id ]->node = $group_node_id;
  1672. $data[ $group_node_id ]->type = 'column-group';
  1673. $data[ $group_node_id ]->parent = $node_id;
  1674. $data[ $group_node_id ]->position = self::next_node_position( null, $node_id );
  1675. $data[ $group_node_id ]->settings = '';
  1676. // Add node template data.
  1677. if ( self::is_node_global( $parent ) ) {
  1678. $data[ $group_node_id ]->template_id = $parent->template_id;
  1679. $data[ $group_node_id ]->template_node_id = $group_node_id;
  1680. }
  1681. // Add new columns?
  1682. if ( isset( self::$row_layouts[ $cols ] ) ) {
  1683. for ( $i = 0; $i < count( self::$row_layouts[ $cols ] ); $i++ ) {
  1684. $col_node_id = self::generate_node_id();
  1685. $data[ $col_node_id ] = new StdClass();
  1686. $data[ $col_node_id ]->node = $col_node_id;
  1687. $data[ $col_node_id ]->type = 'column';
  1688. $data[ $col_node_id ]->parent = $group_node_id;
  1689. $data[ $col_node_id ]->position = $i;
  1690. $data[ $col_node_id ]->settings = new StdClass();
  1691. $data[ $col_node_id ]->settings->size = self::$row_layouts[ $cols ][ $i ];
  1692. if ( self::is_node_global( $parent ) ) {
  1693. $data[ $col_node_id ]->template_id = $parent->template_id;
  1694. $data[ $col_node_id ]->template_node_id = $col_node_id;
  1695. }
  1696. }
  1697. } elseif ( isset( $data[ $cols ] ) ) {
  1698. $old_group = $data[ $cols ]->parent;
  1699. $siblings = self::get_nodes( 'column', $old_group );
  1700. $sibling_pos = 0;
  1701. // Add the column to the group.
  1702. $data[ $cols ]->parent = $group_node_id;
  1703. $data[ $cols ]->position = 0;
  1704. $data[ $cols ]->settings->size = 100;
  1705. if ( self::is_node_global( $parent ) ) {
  1706. $data[ $cols ]->template_id = $parent->template_id;
  1707. $data[ $cols ]->template_node_id = $data[ $cols ]->node;
  1708. }
  1709. // Remove the column from the $siblings array.
  1710. unset( $siblings[ $cols ] );
  1711. // Reorder old siblings.
  1712. foreach ( $siblings as $sibling ) {
  1713. $data[ $sibling->node ]->position = $sibling_pos;
  1714. $sibling_pos++;
  1715. }
  1716. }
  1717. // Update the layout data.
  1718. self::update_layout_data( $data );
  1719. // Delete an existing column's old group if empty or resize it.
  1720. if ( $old_group ) {
  1721. if ( 0 === count( self::get_nodes( 'column', $old_group ) ) ) {
  1722. self::delete_node( $old_group );
  1723. } else {
  1724. self::reset_col_widths( $old_group );
  1725. }
  1726. }
  1727. // Position the column group.
  1728. if ( false !== $position ) {
  1729. self::reorder_node( $group_node_id, $position );
  1730. }
  1731. // Return the column group.
  1732. return self::get_node( $group_node_id );
  1733. }
  1734. /**
  1735. * Runs column specific logic on new column settings.
  1736. *
  1737. * @since 1.0
  1738. * @param object $col A column node.
  1739. * @param object $new_settings The new settings object.
  1740. * @return object
  1741. */
  1742. static public function process_col_settings( $col, $new_settings ) {
  1743. $post_data = self::get_post_data();
  1744. // Don't process for preview nodes.
  1745. if ( isset( $post_data['node_preview'] ) ) {
  1746. return $new_settings;
  1747. }
  1748. // Resize sibling cols if needed.
  1749. $new_settings->size = self::resize_col( $col->node, $new_settings->size );
  1750. // Update other sibling vars as needed.
  1751. $equal_height = false;
  1752. $content_alignment = false;
  1753. $responsive_order = false;
  1754. // Adjust sibling equal height?
  1755. if ( $col->settings->equal_height != $new_settings->equal_height ) {
  1756. $equal_height = $new_settings->equal_height;
  1757. }
  1758. // Adjust sibling content alignment?
  1759. if ( $col->settings->content_alignment != $new_settings->content_alignment ) {
  1760. $content_alignment = $new_settings->content_alignment;
  1761. }
  1762. // Adjust sibling responsive order?
  1763. if ( $col->settings->responsive_order != $new_settings->responsive_order ) {
  1764. $responsive_order = $new_settings->responsive_order;
  1765. }
  1766. // Update the siblings?
  1767. if ( false !== $equal_height || false !== $content_alignment || false !== $responsive_order ) {
  1768. $data = self::get_layout_data();
  1769. $cols = self::get_nodes( 'column', $col->parent );
  1770. foreach ( $cols as $node_id => $node ) {
  1771. if ( false !== $equal_height ) {
  1772. $data[ $node_id ]->settings->equal_height = $equal_height;
  1773. }
  1774. if ( false !== $content_alignment ) {
  1775. $data[ $node_id ]->settings->content_alignment = $content_alignment;
  1776. }
  1777. if ( false !== $responsive_order ) {
  1778. $data[ $node_id ]->settings->responsive_order = $responsive_order;
  1779. }
  1780. }
  1781. self::update_layout_data( $data );
  1782. }
  1783. return $new_settings;
  1784. }
  1785. /**
  1786. * Deletes a column.
  1787. *
  1788. * @since 1.0
  1789. * @param string $node_id Node ID of the column to delete (can also be a group).
  1790. * @param int $new_width New width of the remaining columns.
  1791. * @return void
  1792. */
  1793. static public function delete_col( $node_id = null, $new_width = 100 ) {
  1794. $col = self::get_node( $node_id );
  1795. // Delete the column.
  1796. self::delete_node( $node_id );
  1797. // Return if the node we just deleted was a group.
  1798. if ( 'column-group' == $col->type ) {
  1799. return;
  1800. }
  1801. // Get the group
  1802. $group = self::get_node( $col->parent );
  1803. // Get the group children.
  1804. $cols = self::get_nodes( 'column', $group->node );
  1805. // Delete the group if empty.
  1806. if ( count( $cols ) === 0 ) {
  1807. self::delete_node( $group->node );
  1808. } else {
  1809. // Get the layout data.
  1810. $data = self::get_layout_data();
  1811. // Loop through the columns.
  1812. foreach ( $cols as $col_id => $col ) {
  1813. // Set the new size.
  1814. $data[ $col_id ]->settings->size = round( $new_width, 3 );
  1815. }
  1816. // Update the layout data.
  1817. self::update_layout_data( $data );
  1818. }
  1819. }
  1820. /**
  1821. * Moves a column within a group.
  1822. *
  1823. * @since 1.9
  1824. * @param string $node_id
  1825. * @param int $position
  1826. * @return void
  1827. */
  1828. static public function reorder_col( $node_id, $position = 0 ) {
  1829. $col = self::get_node( $node_id );
  1830. self::reorder_node( $node_id, $position );
  1831. self::reset_col_widths( $col->parent );
  1832. }
  1833. /**
  1834. * Moves a column from one group to another.
  1835. *
  1836. * @since 1.9
  1837. * @param string $col_id
  1838. * @param string $group_id
  1839. * @param int $position
  1840. * @param array $resize
  1841. * @return void
  1842. */
  1843. static public function move_col( $col_id, $group_id, $position, $resize = array() ) {
  1844. $col = self::get_node( $col_id );
  1845. $old_group = self::get_node( $col->parent );
  1846. self::move_node( $col_id, $group_id, $position );
  1847. if ( 0 === count( self::get_nodes( 'column', $old_group ) ) ) {
  1848. self::delete_node( $old_group->node );
  1849. self::reset_col_widths( $group_id );
  1850. } else {
  1851. self::reset_col_widths( $resize );
  1852. }
  1853. }
  1854. /**
  1855. * Resizes a column.
  1856. *
  1857. * @since 1.0
  1858. * @param string $node_id Node ID of the column to resize.
  1859. * @param int $new_width New width of the column.
  1860. * @return int The new width
  1861. */
  1862. static public function resize_col( $node_id = null, $new_width = 100 ) {
  1863. $data = self::get_layout_data();
  1864. $col = $data[ $node_id ];
  1865. $group = $data[ $col->parent ];
  1866. $cols = array_values( self::get_nodes( 'column', $group->node ) );
  1867. $pos = $col->position;
  1868. $siblings = array();
  1869. $siblings_width = 0;
  1870. $num_cols = count( $cols );
  1871. $min_width = 8;
  1872. $max_width = 100 - $min_width;
  1873. // Don't resize if only one column or width isn't a number.
  1874. if ( 1 == $num_cols || ! is_numeric( $new_width ) ) {
  1875. return $col->settings->size;
  1876. }
  1877. // Find the sibling column to absorb this resize.
  1878. for ( $i = 0; $i < count( $cols ); $i++ ) {
  1879. if ( $col->node == $cols[ $i ]->node ) {
  1880. if ( isset( $cols[ $i + 1 ] ) ) {
  1881. $sibling = $cols[ $i + 1 ];
  1882. } else {
  1883. $sibling = $cols[ $i - 1 ];
  1884. }
  1885. break;
  1886. }
  1887. }
  1888. // Find other siblings.
  1889. foreach ( $cols as $c ) {
  1890. if ( $col->node == $c->node ) {
  1891. continue;
  1892. }
  1893. if ( $sibling->node == $c->node ) {
  1894. continue;
  1895. }
  1896. $siblings[] = $c;
  1897. $max_width -= $c->settings->size;
  1898. $siblings_width += $c->settings->size;
  1899. }
  1900. // Make sure the new width isn't too small.
  1901. if ( $new_width < $min_width ) {
  1902. $new_width = $min_width;
  1903. }
  1904. // Make sure the new width isn't too big.
  1905. if ( $new_width > $max_width ) {
  1906. $new_width = $max_width;
  1907. }
  1908. // Save new sibling size.
  1909. $data[ $sibling->node ]->settings->size = round( 100 - $siblings_width - $new_width, 3 );
  1910. // Save new column size.
  1911. $data[ $col->node ]->settings->size = $new_width;
  1912. // Update the layout data.
  1913. self::update_layout_data( $data );
  1914. // Return the new size.
  1915. return $new_width;
  1916. }
  1917. /**
  1918. * Resizes a column and its sibling using the provided widths.
  1919. *
  1920. * @since 1.6.4
  1921. * @param string $col_id Node ID of the column to resize.
  1922. * @param int $col_width New width of the column.
  1923. * @param string $sibling_id Node ID of the sibling to resize.
  1924. * @param int $sibling_width New width of the sibling.
  1925. * @return void
  1926. */
  1927. static public function resize_cols( $col_id = null, $col_width = null, $sibling_id = null, $sibling_width = null ) {
  1928. $data = self::get_layout_data();
  1929. // Save the column width.
  1930. $data[ $col_id ]->settings->size = $col_width;
  1931. // Save the sibling width.
  1932. $data[ $sibling_id ]->settings->size = $sibling_width;
  1933. // Update the layout data.
  1934. self::update_layout_data( $data );
  1935. }
  1936. /**
  1937. * Resets the widths of all columns in a group.
  1938. *
  1939. * @since 1.6.4
  1940. * @param string|array $group_id Node ID of the group whose columns to reset or an array of group IDs.
  1941. * @return void
  1942. */
  1943. static public function reset_col_widths( $group_id = null ) {
  1944. if ( 'array' == gettype( $group_id ) ) {
  1945. foreach ( $group_id as $id ) {
  1946. self::reset_col_widths( $id );
  1947. }
  1948. return;
  1949. }
  1950. $data = self::get_layout_data();
  1951. $post_data = self::get_post_data();
  1952. $cols = self::get_nodes( 'column', $group_id );
  1953. $width = round( 100 / count( $cols ), 3 );
  1954. foreach ( $cols as $col_id => $col ) {
  1955. $data[ $col_id ]->settings->size = $width;
  1956. }
  1957. self::update_layout_data( $data );
  1958. }
  1959. /**
  1960. * Adds a column to a column group in the current layout.
  1961. *
  1962. * @since 1.9
  1963. * @param string $node_id A column group node ID.
  1964. * @param int $position The position of the new column.
  1965. * @return object The new column object.
  1966. */
  1967. static public function add_col( $node_id = null, $position = false ) {
  1968. $group = self::get_node( $node_id );
  1969. $cols = self::get_nodes( 'column', $group );
  1970. $num_cols = count( $cols );
  1971. $i = 0;
  1972. $sibling = false;
  1973. $insert = 'before';
  1974. foreach ( $cols as $col ) {
  1975. if ( $i == $position ) {
  1976. $sibling = $col;
  1977. break;
  1978. }
  1979. $i++;
  1980. }
  1981. if ( ! $sibling ) {
  1982. $sibling = $col;
  1983. $insert = 'after';
  1984. }
  1985. self::add_cols( $sibling->node, $insert );
  1986. $cols = self::get_nodes( 'column', $group );
  1987. $col_ids = array_keys( $cols );
  1988. return $cols[ $col_ids[ $position ] ];
  1989. }
  1990. /**
  1991. * Inserts a column (or columns) before or after another column.
  1992. *
  1993. * @since 1.6.4
  1994. * @param string $node_id Node ID of the column to insert before or after.
  1995. * @param string $insert Either before or after.
  1996. * @param string $type The type of column(s) to insert.
  1997. * @param boolean $nested Whether these columns are nested or not.
  1998. * @return object
  1999. */
  2000. static public function add_cols( $col_id, $insert = 'before', $type = '1-col', $nested = false ) {
  2001. $data = self::get_layout_data();
  2002. $col = self::get_node( $col_id );
  2003. $parent = self::get_node( $col->parent );
  2004. $cols = self::get_nodes( 'column', $col->parent );
  2005. $global = self::is_node_global( $parent );
  2006. $num_new_cols = count( self::$row_layouts[ $type ] );
  2007. $num_cols = count( $cols );
  2008. $max_cols = $nested ? 4 : 12;
  2009. $reposition = false;
  2010. $position = 0;
  2011. // Make sure we have 12 columns or less.
  2012. if ( $num_cols + $num_new_cols > $max_cols ) {
  2013. $num_new_cols = $num_new_cols - ( $num_cols + $num_new_cols - $max_cols );
  2014. $num_cols = $max_cols;
  2015. } else {
  2016. $num_cols += $num_new_cols;
  2017. }
  2018. // Get the new width.
  2019. if ( 6 === $num_cols ) {
  2020. $new_width = 16.65;
  2021. } elseif ( 7 === $num_cols ) {
  2022. $new_width = 14.28;
  2023. } else {
  2024. $new_width = round( 100 / $num_cols, 3 );
  2025. }
  2026. // Get the new column position.
  2027. if ( 'before' == $insert ) {
  2028. $new_col_position = $col->position - 1 < 0 ? 0 : $col->position;
  2029. } else {
  2030. $new_col_position = $col->position + 1;
  2031. }
  2032. // Add the new columns.
  2033. for ( $i = 0; $i < $num_new_cols; $i++ ) {
  2034. $new_col_id = self::generate_node_id();
  2035. $data[ $new_col_id ] = new StdClass();
  2036. $data[ $new_col_id ]->node = $new_col_id;
  2037. $data[ $new_col_id ]->type = 'column';
  2038. $data[ $new_col_id ]->parent = $parent->node;
  2039. $data[ $new_col_id ]->position = $new_col_position;
  2040. $data[ $new_col_id ]->settings = new StdClass();
  2041. $data[ $new_col_id ]->settings->size = $new_width;
  2042. // Add node template data.
  2043. if ( $global ) {
  2044. $data[ $new_col_id ]->template_id = $parent->template_id;
  2045. $data[ $new_col_id ]->template_node_id = $new_col_id;
  2046. }
  2047. $new_col_position++;
  2048. }
  2049. // Resize sibling columns and set their new position.
  2050. foreach ( $cols as $sibling_col_id => $sibling_col ) {
  2051. $data[ $sibling_col_id ]->settings->size = $new_width;
  2052. if ( $sibling_col_id == $col_id ) {
  2053. $reposition = true;
  2054. if ( 'before' == $insert ) {
  2055. $data[ $sibling_col_id ]->position = $new_col_position;
  2056. $new_col_position++;
  2057. }
  2058. } elseif ( $reposition ) {
  2059. $data[ $sibling_col_id ]->position = $new_col_position;
  2060. $new_col_position++;
  2061. } else {
  2062. $data[ $sibling_col_id ]->position = $position;
  2063. $position++;
  2064. }
  2065. }
  2066. // Update the layout data.
  2067. self::update_layout_data( $data );
  2068. // Return the column group.
  2069. return $parent;
  2070. }
  2071. /**
  2072. * Adds a parent node for a column if a parent with the supplied
  2073. * parent ID doesn't exist.
  2074. *
  2075. * @since 2.1
  2076. * @param string $parent_id The node ID of the parent to look for.
  2077. * @param int $position The position of the parent.
  2078. * @return string|null The new parent ID or null if none exists.
  2079. */
  2080. static public function add_col_parent( $parent_id = null, $position = null ) {
  2081. $data = self::get_layout_data();
  2082. $parent = ! $parent_id ? null : self::get_node( $parent_id );
  2083. if ( ! $parent ) {
  2084. // Add a new row if we don't have a parent, but don't add column.
  2085. $row = self::add_row( null, $position );
  2086. $col_groups = self::get_nodes( 'column-group', $row->node );
  2087. $col_group = array_shift( $col_groups );
  2088. $parent_id = $col_group->node;
  2089. } elseif ( 'row' == $parent->type ) {
  2090. // Add a new column group if the parent is a row, but don't add column.
  2091. $col_group = self::add_col_group( $parent->node, null, $position );
  2092. $parent_id = $col_group->node;
  2093. }
  2094. return $parent_id;
  2095. }
  2096. /**
  2097. * Returns a column's parent node of the specified type.
  2098. *
  2099. * @since 2.1
  2100. * @param string $type The type of parent to return.
  2101. * @param string|object $column_id The columns's node ID. Can also be a column object.
  2102. * @return object The parent node.
  2103. */
  2104. static public function get_col_parent( $type, $column_id ) {
  2105. $column = is_object( $column_id ) ? $column_id : self::get_node( $column_id );
  2106. $nodes = self::get_categorized_nodes();
  2107. foreach ( $nodes['groups'] as $group ) {
  2108. if ( $group->node == $column->parent ) {
  2109. if ( 'column-group' == $type ) {
  2110. return $group;
  2111. }
  2112. foreach ( $nodes['rows'] as $row ) {
  2113. if ( $row->node == $group->parent ) {
  2114. return $row;
  2115. }
  2116. }
  2117. }
  2118. }
  2119. return null;
  2120. }
  2121. /**
  2122. * Copys a column and adds it to the current layout.
  2123. *
  2124. * @since 2.0
  2125. * @param string $node_id Node ID of the column to copy.
  2126. * @param object $settings These settings will be used for the copy if present.
  2127. * @param string $settings_id The ID of the node who's settings were passed.
  2128. * @return void
  2129. */
  2130. static public function copy_col( $node_id = null, $settings = null, $settings_id = null ) {
  2131. $layout_data = self::get_layout_data();
  2132. $col = self::get_node( $node_id );
  2133. $new_col_id = self::generate_node_id();
  2134. $nodes = self::get_nodes( null, $col );
  2135. $parent = self::get_node_parent( $node_id );
  2136. $new_nodes = array();
  2137. // Add the new column.
  2138. $layout_data[ $new_col_id ] = clone $col;
  2139. $layout_data[ $new_col_id ]->settings = clone $col->settings;
  2140. $layout_data[ $new_col_id ]->node = $new_col_id;
  2141. // Unset column template data.
  2142. if ( isset( $layout_data[ $new_col_id ]->template_id ) ) {
  2143. // Get the column root parent on a page.
  2144. if ( isset( $layout_data[ $new_col_id ]->template_root_node ) ) {
  2145. $parent = self::get_node( $layout_data[ $new_col_id ]->parent );
  2146. }
  2147. // Check if parent is a global node.
  2148. if ( self::is_node_global( $parent ) ) {
  2149. $layout_data[ $new_col_id ]->template_id = $parent->template_id;
  2150. $layout_data[ $new_col_id ]->template_node_id = $new_col_id;
  2151. } else {
  2152. unset( $layout_data[ $new_col_id ]->template_id );
  2153. unset( $layout_data[ $new_col_id ]->template_node_id );
  2154. }
  2155. unset( $layout_data[ $new_col_id ]->template_root_node );
  2156. }
  2157. // Get the new child nodes.
  2158. foreach ( $nodes as $node ) {
  2159. $new_nodes[ $node->node ] = clone $node;
  2160. if ( 'module' == $node->type ) {
  2161. $new_nodes[ $node->node ]->settings = self::clone_module_settings( $node->settings );
  2162. } elseif ( 'column-group' == $node->type ) {
  2163. $nested_cols = self::get_nodes( 'column', $node );
  2164. foreach ( $nested_cols as $nested_col ) {
  2165. $new_nodes[ $nested_col->node ] = clone $nested_col;
  2166. $new_nodes[ $nested_col->node ]->settings = clone $nested_col->settings;
  2167. $modules = self::get_nodes( 'module', $nested_col );
  2168. foreach ( $modules as $module ) {
  2169. $new_nodes[ $module->node ] = clone $module;
  2170. $new_nodes[ $module->node ]->settings = self::clone_module_settings( $module->settings );
  2171. }
  2172. }
  2173. }
  2174. }
  2175. // Apply settings that were passed if we have them.
  2176. if ( $settings && $settings_id ) {
  2177. if ( $settings_id === $col->node ) {
  2178. $layout_data[ $new_col_id ]->settings = (object) array_merge( (array) $col->settings, (array) $settings );
  2179. } else {
  2180. $new_nodes[ $settings_id ]->settings = (object) array_merge( (array) $new_nodes[ $settings_id ]->settings, (array) $settings );
  2181. }
  2182. }
  2183. // Generate new child ids.
  2184. $new_nodes = self::generate_new_node_ids( $new_nodes );
  2185. // Set child parent ids to the new column id and unset template data.
  2186. foreach ( $new_nodes as $child_node_id => $child ) {
  2187. if ( $child->parent == $col->node || ( isset( $col->template_node_id ) && $child->parent == $col->template_node_id ) ) {
  2188. $new_nodes[ $child_node_id ]->parent = $new_col_id;
  2189. }
  2190. if ( isset( $new_nodes[ $child_node_id ]->template_id ) ) {
  2191. // Check if the column is global.
  2192. if ( isset( $layout_data[ $new_col_id ]->template_node_id ) ) {
  2193. $new_nodes[ $child_node_id ]->template_id = $parent->template_id;
  2194. $new_nodes[ $child_node_id ]->template_node_id = $child_node_id;
  2195. } else {
  2196. unset( $new_nodes[ $child_node_id ]->template_id );
  2197. unset( $new_nodes[ $child_node_id ]->template_node_id );
  2198. }
  2199. }
  2200. }
  2201. // Merge the child data.
  2202. $layout_data = array_merge( $layout_data, $new_nodes );
  2203. // Update the layout data.
  2204. self::update_layout_data( $layout_data );
  2205. // Position the new column.
  2206. self::reorder_node( $new_col_id, $col->position + 1 );
  2207. // Reset the column widths.
  2208. self::reset_col_widths( $col->parent );
  2209. // Return the new column.
  2210. return self::get_node( $new_col_id );
  2211. }
  2212. /**
  2213. * Returns the default settings for column nodes.
  2214. *
  2215. * @since 1.0
  2216. * @return object
  2217. */
  2218. static public function get_col_defaults() {
  2219. $settings = self::get_settings_form_defaults( 'col' );
  2220. $settings = self::merge_nested_form_defaults( 'general', 'col', $settings );
  2221. return $settings;
  2222. }
  2223. /**
  2224. * Loads the classes for core builder modules.
  2225. *
  2226. * @since 1.0
  2227. * @return void
  2228. */
  2229. static public function load_modules() {
  2230. $paths = glob( FL_BUILDER_DIR . 'modules/*' );
  2231. $module_path = '';
  2232. // Make sure we have an array.
  2233. if ( ! is_array( $paths ) ) {
  2234. return;
  2235. }
  2236. // Load all found modules.
  2237. foreach ( $paths as $path ) {
  2238. // Make sure we have a directory.
  2239. if ( ! is_dir( $path ) ) {
  2240. continue;
  2241. }
  2242. // Get the module slug.
  2243. $slug = basename( $path );
  2244. // Paths to check.
  2245. $module_path = $slug . '/' . $slug . '.php';
  2246. $child_path = get_stylesheet_directory() . '/fl-builder/modules/' . $module_path;
  2247. $theme_path = get_template_directory() . '/fl-builder/modules/' . $module_path;
  2248. $builder_path = FL_BUILDER_DIR . 'modules/' . $module_path;
  2249. // Check for the module class in a child theme.
  2250. if ( is_child_theme() && file_exists( $child_path ) ) {
  2251. require_once $child_path;
  2252. } elseif ( file_exists( $theme_path ) ) {
  2253. require_once $theme_path;
  2254. } elseif ( file_exists( $builder_path ) ) {
  2255. require_once $builder_path;
  2256. }
  2257. }
  2258. do_action( 'fl_builder_register_extensions' );
  2259. }
  2260. /**
  2261. * Registers a module with the builder.
  2262. *
  2263. * @since 1.0
  2264. * @param string $class The module's PHP class name.
  2265. * @param array $form The module's settings form.
  2266. * @return void
  2267. */
  2268. static public function register_module( $class, $form ) {
  2269. if ( class_exists( $class ) ) {
  2270. // Create a new instance of the module.
  2271. $instance = new $class();
  2272. // Log an error if a module with this slug already exists.
  2273. if ( isset( self::$modules[ $instance->slug ] ) ) {
  2274. error_log( sprintf( _x( 'A module with the filename %s.php already exists! Please namespace your module filenames to ensure compatibility with Beaver Builder.', '%s stands for the module filename', 'fl-builder' ), $instance->slug ) );
  2275. return;
  2276. }
  2277. /**
  2278. * Use this filter to override the modules that are enabled in the builder.
  2279. * @see fl_builder_register_module
  2280. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  2281. */
  2282. $instance->enabled = apply_filters( 'fl_builder_register_module', $instance->enabled, $instance );
  2283. // Save the instance in the modules array.
  2284. self::$modules[ $instance->slug ] = $instance;
  2285. /**
  2286. * Use this filter to modify the config array for a settings form when it is registered.
  2287. * @see fl_builder_register_settings_form
  2288. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  2289. */
  2290. self::$modules[ $instance->slug ]->form = apply_filters( 'fl_builder_register_settings_form', $form, $instance->slug );
  2291. self::$modules[ $instance->slug ]->form['advanced'] = self::$settings_forms['module_advanced'];
  2292. }
  2293. }
  2294. /**
  2295. * Registers an alias to a module with its own name,
  2296. * category and default settings.
  2297. *
  2298. * @since 1.10
  2299. * @param string $alias The alias key.
  2300. * @param array $config The alias config.
  2301. * @return void
  2302. */
  2303. static public function register_module_alias( $alias, $config ) {
  2304. if ( isset( self::$module_aliases[ $alias ] ) ) {
  2305. _doing_it_wrong( __CLASS__ . '::register_module_alias', sprintf( _x( 'The module alias %s already exists! Please namespace your module aliases to ensure compatibility with Beaver Builder.', '%s stands for the module alias key', 'fl-builder' ), $alias ), '1.10' );
  2306. return;
  2307. }
  2308. $instance = new stdClass;
  2309. $instance->alias = $alias;
  2310. $instance->slug = isset( $config['module'] ) ? $config['module'] : null;
  2311. $instance->name = isset( $config['name'] ) ? $config['name'] : $slug;
  2312. $instance->description = isset( $config['description'] ) ? $config['description'] : '';
  2313. $instance->category = isset( $config['category'] ) ? $config['category'] : null;
  2314. $instance->group = isset( $config['group'] ) ? $config['group'] : null;
  2315. $instance->settings = isset( $config['settings'] ) ? $config['settings'] : array();
  2316. $instance->enabled = isset( $config['enabled'] ) ? $config['enabled'] : true;
  2317. $instance->icon = isset( $config['icon'] ) ? $config['icon'] : FLBuilderModule::get_default_icon();
  2318. self::$module_aliases[ $alias ] = $instance;
  2319. }
  2320. /**
  2321. * Returns the default settings for a module alias.
  2322. *
  2323. * @since 1.10
  2324. * @param string $alias The alias key.
  2325. * @return array|null
  2326. */
  2327. static public function get_module_alias_settings( $alias ) {
  2328. if ( isset( self::$module_aliases[ $alias ] ) ) {
  2329. return self::$module_aliases[ $alias ]->settings;
  2330. }
  2331. return null;
  2332. }
  2333. /**
  2334. * Checks to see if a module of a certain type has
  2335. * been registered.
  2336. *
  2337. * @since 1.9
  2338. * @param array $type The module's type slug.
  2339. * @return void
  2340. */
  2341. static public function is_module_registered( $type ) {
  2342. return isset( self::$modules[ $type ] );
  2343. }
  2344. /**
  2345. * Returns an array of modules that are enabled by default.
  2346. *
  2347. * @since 2.1
  2348. * @return array
  2349. */
  2350. static public function get_default_enabled_modules() {
  2351. $default = array_keys( self::$modules );
  2352. // These modules are deprecated and disabled by default.
  2353. $deprecated = array(
  2354. 'social-buttons',
  2355. );
  2356. // Remove deprecated modules from the defaults.
  2357. foreach ( $default as $key => $slug ) {
  2358. if ( in_array( $slug, $deprecated ) ) {
  2359. unset( $default[ $key ] );
  2360. }
  2361. }
  2362. return array_values( $default );
  2363. }
  2364. /**
  2365. * Returns an array of all modules that are enabled.
  2366. *
  2367. * @since 1.0
  2368. * @return array
  2369. */
  2370. static public function get_enabled_modules() {
  2371. $setting = self::get_admin_settings_option( '_fl_builder_enabled_modules', true );
  2372. if ( ! $setting ) {
  2373. // Fallback to the defaults if no saved setting.
  2374. $setting = self::get_default_enabled_modules();
  2375. } elseif ( in_array( 'all', $setting ) ) {
  2376. // Redefine $setting in case new modules have been installed since the last save.
  2377. $setting = array_keys( self::$modules );
  2378. $setting[] = 'all';
  2379. }
  2380. foreach ( self::$modules as $module_slug => $module ) {
  2381. if ( ! $module->enabled && in_array( $module_slug, $setting ) ) {
  2382. $key = array_search( $module_slug, $setting );
  2383. unset( $setting[ $key ] );
  2384. }
  2385. }
  2386. return apply_filters( 'fl_builder_enabled_modules', $setting );
  2387. }
  2388. /**
  2389. * Returns an array of module group slugs and names.
  2390. *
  2391. * @since 2.0
  2392. * @return array
  2393. */
  2394. static public function get_module_groups() {
  2395. $groups = array();
  2396. $templates = FLBuilderModel::get_module_templates_data();
  2397. // Add module groups.
  2398. foreach ( self::$modules as $module ) {
  2399. if ( ! $module->group || ! $module->enabled ) {
  2400. continue;
  2401. }
  2402. // Check if widgets are enabled
  2403. if ( 'widget' == $module->slug && ! in_array( 'widget', self::get_enabled_modules() ) ) {
  2404. continue;
  2405. }
  2406. $slug = sanitize_key( $module->group );
  2407. if ( ! isset( $groups[ $slug ] ) ) {
  2408. $groups[ $slug ] = $module->group;
  2409. }
  2410. }
  2411. // Add module template groups.
  2412. if ( isset( $templates['groups'] ) ) {
  2413. foreach ( $templates['groups'] as $slug => $data ) {
  2414. if ( ! isset( $groups[ $slug ] ) ) {
  2415. $groups[ $slug ] = $data['name'];
  2416. }
  2417. }
  2418. }
  2419. return $groups;
  2420. }
  2421. /**
  2422. * Returns an array of module category slugs => names
  2423. *
  2424. * @since 2.0
  2425. * @return array
  2426. */
  2427. static public function get_module_categories() {
  2428. $categories = array();
  2429. /**
  2430. * Use this filter to add custom module categories that will show up before the default module categories in the builder’s UI.
  2431. * @see fl_builder_module_categories
  2432. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  2433. */
  2434. foreach ( apply_filters( 'fl_builder_module_categories', array() ) as $custom_category ) {
  2435. $categories[ $custom_category ] = array();
  2436. }
  2437. // Build the default category arrays.
  2438. $categories[ __( 'Basic', 'fl-builder' ) ] = array();
  2439. $categories[ __( 'Media', 'fl-builder' ) ] = array();
  2440. $categories[ __( 'Actions', 'fl-builder' ) ] = array();
  2441. $categories[ __( 'Layout', 'fl-builder' ) ] = array();
  2442. $categories[ __( 'Info', 'fl-builder' ) ] = array();
  2443. $categories[ __( 'Posts', 'fl-builder' ) ] = array();
  2444. $categories[ __( 'Advanced', 'fl-builder' ) ] = array();
  2445. $categories[ __( 'Other', 'fl-builder' ) ] = array();
  2446. return $categories;
  2447. }
  2448. /**
  2449. * Returns an array of categorized modules.
  2450. *
  2451. * @since 1.0
  2452. * @param bool $show_disabled Whether to include disabled modules in the result.
  2453. * @return array
  2454. */
  2455. static public function get_categorized_modules( $show_disabled = false ) {
  2456. $enabled_modules = self::get_enabled_modules();
  2457. $widgets = null;
  2458. $categories = self::get_module_categories();
  2459. $other_key = __( 'Other', 'fl-builder' );
  2460. $widgets_key = __( 'WordPress Widgets', 'fl-builder' );
  2461. // Build the categories array.
  2462. foreach ( self::$modules as $module ) {
  2463. if ( ! $module->enabled ) {
  2464. continue;
  2465. } elseif ( ! in_array( $module->slug, $enabled_modules ) && ! $show_disabled ) {
  2466. continue;
  2467. } elseif ( 'widget' == $module->slug ) {
  2468. $widgets = self::get_wp_widgets();
  2469. } elseif ( isset( $module->category ) ) {
  2470. if ( ! isset( $categories[ $module->category ] ) ) {
  2471. $categories[ $module->category ] = array();
  2472. }
  2473. $categories[ $module->category ][ $module->name ] = $module;
  2474. } else {
  2475. $categories[ $other_key ][ $module->name ] = $module;
  2476. }
  2477. }
  2478. // Add module aliases.
  2479. foreach ( self::$module_aliases as $alias => $config ) {
  2480. if ( ! $config->enabled || ! $config->slug || ! $config->category ) {
  2481. continue;
  2482. }
  2483. if ( ! isset( $categories[ $config->category ] ) ) {
  2484. $categories[ $config->category ] = array();
  2485. }
  2486. $categories[ $config->category ][ $config->name ] = $config;
  2487. }
  2488. // Add widgets if we have them.
  2489. if ( $widgets ) {
  2490. $categories[ $widgets_key ] = $widgets;
  2491. }
  2492. // Sort the modules.
  2493. foreach ( $categories as $title => $modules ) {
  2494. if ( count( $categories[ $title ] ) == 0 ) {
  2495. unset( $categories[ $title ] );
  2496. } else {
  2497. ksort( $categories[ $title ] );
  2498. }
  2499. }
  2500. // Return sorted categories.
  2501. return $categories;
  2502. }
  2503. /**
  2504. * Similar to get_categorized_modules() but creates a flat list.
  2505. *
  2506. * @since 2.0
  2507. * @param bool $show_disabled Should show disabled?
  2508. * @return array
  2509. */
  2510. static public function get_uncategorized_modules( $show_disabled = false ) {
  2511. $enabled_modules = self::get_enabled_modules();
  2512. $modules = array();
  2513. $aliases = self::$module_aliases;
  2514. $widgets = FLBuilderModel::get_wp_widgets();
  2515. foreach ( self::$modules as $module ) {
  2516. if ( ! $module->enabled ) {
  2517. continue;
  2518. } elseif ( ! in_array( $module->slug, $enabled_modules ) && ! $show_disabled ) {
  2519. continue;
  2520. } elseif ( 'widget' === $module->slug ) {
  2521. continue;
  2522. }
  2523. $module = clone (object) $module;
  2524. $module->kind = 'module';
  2525. $module->isWidget = false; // @codingStandardsIgnoreLine
  2526. $module->isAlias = false; // @codingStandardsIgnoreLine
  2527. $module->group = $module->group ? array( sanitize_key( $module->group ) ) : array( 'standard' );
  2528. if ( ! isset( $module->icon ) || '' == $module->icon ) {
  2529. $module->icon = FLBuilderModule::get_default_icon();
  2530. }
  2531. // Remove backend-only & instance properties.
  2532. unset( $module->css );
  2533. unset( $module->js );
  2534. unset( $module->editor_export );
  2535. unset( $module->node );
  2536. unset( $module->parent );
  2537. unset( $module->partial_refresh );
  2538. unset( $module->position );
  2539. unset( $module->settings );
  2540. unset( $module->form );
  2541. unset( $module->dir );
  2542. $modules[] = $module;
  2543. }
  2544. // Add module aliases.
  2545. foreach ( $aliases as $alias => $config ) {
  2546. if ( ! $config->enabled || ! $config->slug || ! $config->category ) {
  2547. continue;
  2548. }
  2549. if ( ! isset( $categories[ $config->category ] ) ) {
  2550. $categories[ $config->category ] = array();
  2551. }
  2552. $config->kind = 'module';
  2553. $config->isWidget = false; // @codingStandardsIgnoreLine
  2554. $config->isAlias = true; // @codingStandardsIgnoreLine
  2555. $config->group = $config->group ? array( sanitize_key( $config->group ) ) : array( 'standard' );
  2556. $modules[] = $config;
  2557. }
  2558. // Add WordPress widgets.
  2559. if ( in_array( 'widget', $enabled_modules ) ) {
  2560. foreach ( $widgets as $widget ) {
  2561. $data = new stdClass;
  2562. $widget = (object) $widget;
  2563. $data->id = $widget->id;
  2564. $data->name = $widget->name;
  2565. $data->class = $widget->class;
  2566. $data->category = $widget->category;
  2567. $data->kind = 'module';
  2568. $data->isWidget = true; // @codingStandardsIgnoreLine
  2569. $data->isAlias = false; // @codingStandardsIgnoreLine
  2570. $data->description = isset( $widget->widget_options['description'] ) ? $widget->widget_options['description'] : '';
  2571. $data->group = array( sanitize_key( __( 'WordPress Widgets', 'fl-builder' ) ) );
  2572. if ( ! isset( $widget->icon ) ) {
  2573. $data->icon = FLBuilderModule::get_widget_icon();
  2574. }
  2575. $modules[] = $data;
  2576. }
  2577. }
  2578. return $modules;
  2579. }
  2580. /**
  2581. * Returns an instance of a module.
  2582. *
  2583. * @since 1.0
  2584. * @param string|object $node_id A module node ID or object.
  2585. * @return object|bool The module or false if it doesn't exist.
  2586. */
  2587. static public function get_module( $node_id ) {
  2588. $module = is_object( $node_id ) ? $node_id : self::get_node( $node_id );
  2589. if ( self::is_module_registered( $module->settings->type ) ) {
  2590. $class = get_class( self::$modules[ $module->settings->type ] );
  2591. $instance = new $class();
  2592. $instance->node = $module->node;
  2593. $instance->parent = $module->parent;
  2594. $instance->position = $module->position;
  2595. $instance->settings = $module->settings;
  2596. $instance->type = 'module';
  2597. $instance->form = self::$modules[ $module->settings->type ]->form;
  2598. $instance->icon = isset( $module->icon ) ? $module->icon : FLBuilderModule::get_default_icon();
  2599. if ( isset( $module->template_id ) ) {
  2600. $instance->template_id = $module->template_id;
  2601. $instance->template_node_id = $module->template_node_id;
  2602. }
  2603. if ( isset( $module->template_root_node ) ) {
  2604. $instance->template_root_node = true;
  2605. }
  2606. return $instance;
  2607. }
  2608. return false;
  2609. }
  2610. /**
  2611. * Returns an array of all modules in the current layout
  2612. * or in a column if a column id or object is supplied.
  2613. *
  2614. * @since 1.0
  2615. * @param string|object $col_id A column ID or object.
  2616. * @return array
  2617. */
  2618. static public function get_modules( $col_id = null ) {
  2619. $col = is_object( $col_id ) ? $col_id : self::get_node( $col_id );
  2620. $modules = self::get_nodes( 'module', $col );
  2621. $instances = array();
  2622. $i = 0;
  2623. foreach ( $modules as $module ) {
  2624. if ( self::is_module_registered( $module->settings->type ) ) {
  2625. $class = get_class( self::$modules[ $module->settings->type ] );
  2626. $instances[ $i ] = new $class();
  2627. $instances[ $i ]->node = $module->node;
  2628. $instances[ $i ]->parent = $module->parent;
  2629. $instances[ $i ]->position = $module->position;
  2630. $instances[ $i ]->settings = $module->settings;
  2631. $instances[ $i ]->type = 'module';
  2632. $instances[ $i ]->icon = isset( $module->icon ) ? $module->icon : FLBuilderModule::get_default_icon();
  2633. $instances[ $i ]->form = self::$modules[ $module->settings->type ]->form;
  2634. if ( isset( $module->template_id ) ) {
  2635. $instances[ $i ]->template_id = $module->template_id;
  2636. $instances[ $i ]->template_node_id = $module->template_node_id;
  2637. }
  2638. if ( isset( $module->template_root_node ) ) {
  2639. $instances[ $i ]->template_root_node = true;
  2640. }
  2641. $i++;
  2642. }
  2643. }
  2644. return $instances;
  2645. }
  2646. /**
  2647. * Returns an array of all modules in the current layout.
  2648. *
  2649. * @since 1.0
  2650. * @return array
  2651. */
  2652. static public function get_all_modules() {
  2653. return self::get_modules();
  2654. }
  2655. /**
  2656. * Add a new module to a column in the current layout.
  2657. *
  2658. * @since 1.0
  2659. * @param string $type The type of module to add.
  2660. * @param array $settings The new module's settings.
  2661. * @param string $parent_id The new module's parent node ID.
  2662. * @param int $position The new module's position.
  2663. * @return object The new module object.
  2664. */
  2665. static public function add_module( $type = null, $settings = array(), $parent_id = null, $position = false ) {
  2666. $data = self::get_layout_data();
  2667. $parent = self::get_node( $parent_id );
  2668. $module_node_id = self::generate_node_id();
  2669. $settings->type = $type;
  2670. // Run module update method.
  2671. $class = get_class( self::$modules[ $type ] );
  2672. $instance = new $class();
  2673. $instance->node = $module_node_id;
  2674. $instance->settings = $settings;
  2675. $settings = $instance->update( $settings );
  2676. // Save the module.
  2677. $data[ $module_node_id ] = new StdClass();
  2678. $data[ $module_node_id ]->node = $module_node_id;
  2679. $data[ $module_node_id ]->type = 'module';
  2680. $data[ $module_node_id ]->parent = $parent_id;
  2681. $data[ $module_node_id ]->position = self::next_node_position( 'module', $parent_id );
  2682. $data[ $module_node_id ]->settings = $settings;
  2683. // Add node template data.
  2684. if ( self::is_node_global( $parent ) ) {
  2685. $data[ $module_node_id ]->template_id = $parent->template_id;
  2686. $data[ $module_node_id ]->template_node_id = $module_node_id;
  2687. }
  2688. // Update the layout data.
  2689. self::update_layout_data( $data );
  2690. // Position the module.
  2691. if ( false !== $position ) {
  2692. self::reorder_node( $module_node_id, $position );
  2693. }
  2694. // Send back the inserted module.
  2695. return self::get_module( $module_node_id );
  2696. }
  2697. /**
  2698. * Adds a parent node for a module if a parent with the supplied
  2699. * parent ID doesn't exist.
  2700. *
  2701. * @since 1.6.3
  2702. * @param string $parent_id The node ID of the parent to look for.
  2703. * @param int $position The position of the parent.
  2704. * @return string|null The new parent ID or null if none exists.
  2705. */
  2706. static public function add_module_parent( $parent_id = null, $position = null ) {
  2707. $parent = ! $parent_id ? null : self::get_node( $parent_id );
  2708. if ( ! $parent ) {
  2709. // Add a new row if we don't have a parent.
  2710. $row = self::add_row( '1-col', $position );
  2711. $col_groups = self::get_nodes( 'column-group', $row->node );
  2712. $col_group = array_shift( $col_groups );
  2713. $cols = self::get_nodes( 'column', $col_group->node );
  2714. $parent = array_shift( $cols );
  2715. $parent_id = $parent->node;
  2716. } elseif ( 'row' == $parent->type ) {
  2717. // Add a new column group if the parent is a row.
  2718. $col_group = self::add_col_group( $parent->node, '1-col', $position );
  2719. $cols = self::get_nodes( 'column', $col_group->node );
  2720. $parent = array_shift( $cols );
  2721. $parent_id = $parent->node;
  2722. } elseif ( 'column-group' == $parent->type ) {
  2723. // Add a new column if the parent is a column group.
  2724. $parent = self::add_col( $parent->node, $position );
  2725. $parent_id = $parent->node;
  2726. }
  2727. return $parent_id;
  2728. }
  2729. /**
  2730. * Returns a module's parent node of the specified type.
  2731. *
  2732. * @since 1.7
  2733. * @param string $type The type of parent to return.
  2734. * @param string|object $module_id The module's node ID. Can also be a module object.
  2735. * @return object The parent node.
  2736. */
  2737. static public function get_module_parent( $type, $module_id ) {
  2738. $module = is_object( $module_id ) ? $module_id : self::get_module( $module_id );
  2739. $nodes = self::get_categorized_nodes();
  2740. foreach ( $nodes['columns'] as $column ) {
  2741. if ( $column->node == $module->parent ) {
  2742. if ( 'column' == $type ) {
  2743. return $column;
  2744. }
  2745. foreach ( $nodes['groups'] as $group ) {
  2746. if ( $group->node == $column->parent ) {
  2747. if ( 'column-group' == $type ) {
  2748. return $group;
  2749. }
  2750. foreach ( $nodes['rows'] as $row ) {
  2751. if ( $row->node == $group->parent ) {
  2752. return $row;
  2753. }
  2754. }
  2755. }
  2756. }
  2757. }
  2758. }
  2759. return null;
  2760. }
  2761. /**
  2762. * Add a new module with default settings to a column
  2763. * in the current layout.
  2764. *
  2765. * @since 1.0
  2766. * @param string $parent_id The new module's parent node ID.
  2767. * @param string $type The type of module to add.
  2768. * @param int $position The new module's position.
  2769. * @return object The new module object.
  2770. * @return array $defaults Default settings for the module.
  2771. */
  2772. static public function add_default_module( $parent_id = null, $type = null, $position = null, $defaults = null ) {
  2773. $parent = ( 0 == $parent_id ) ? null : self::get_node( $parent_id );
  2774. $settings = self::get_module_defaults( $type );
  2775. $module_node_id = self::generate_node_id();
  2776. // Add a new parent if one is needed.
  2777. if ( ! $parent || 'row' == $parent->type || 'column-group' == $parent->type ) {
  2778. $parent_id = self::add_module_parent( $parent_id, $position );
  2779. $parent = self::get_node( $parent_id );
  2780. $position = null;
  2781. }
  2782. // Merge default settings if present.
  2783. if ( $defaults ) {
  2784. $settings = (object) array_merge( (array) $settings, $defaults );
  2785. }
  2786. // Run module update method.
  2787. $class = get_class( self::$modules[ $type ] );
  2788. $instance = new $class();
  2789. $instance->node = $module_node_id;
  2790. $instance->settings = $settings;
  2791. $settings = $instance->update( $settings );
  2792. // Save the module.
  2793. $data = self::get_layout_data();
  2794. $data[ $module_node_id ] = new StdClass();
  2795. $data[ $module_node_id ]->node = $module_node_id;
  2796. $data[ $module_node_id ]->type = 'module';
  2797. $data[ $module_node_id ]->parent = $parent_id;
  2798. $data[ $module_node_id ]->position = self::next_node_position( 'module', $parent_id );
  2799. $data[ $module_node_id ]->settings = $settings;
  2800. // Add node template data.
  2801. if ( self::is_node_global( $parent ) ) {
  2802. $data[ $module_node_id ]->template_id = $parent->template_id;
  2803. $data[ $module_node_id ]->template_node_id = $module_node_id;
  2804. }
  2805. // Update the layout data.
  2806. self::update_layout_data( $data );
  2807. // Position the module.
  2808. if ( null !== $position ) {
  2809. self::reorder_node( $module_node_id, $position );
  2810. }
  2811. // Send back the inserted module.
  2812. return self::get_module( $module_node_id );
  2813. }
  2814. /**
  2815. * Make a copy of a module.
  2816. *
  2817. * @since 1.0
  2818. * @param string $node_id Node ID of the module to copy.
  2819. * @param object $settings These settings will be used for the copy if present.
  2820. * @return object The new module object.
  2821. */
  2822. static public function copy_module( $node_id = null, $settings = null ) {
  2823. $module = self::get_module( $node_id );
  2824. if ( $settings ) {
  2825. $module->settings = (object) array_merge( (array) $module->settings, (array) $settings );
  2826. }
  2827. return self::add_module( $module->settings->type, $module->settings, $module->parent, $module->position + 1 );
  2828. }
  2829. /**
  2830. * Run module specific logic on new node settings.
  2831. *
  2832. * @since 1.0
  2833. * @param object $module A module node object.
  2834. * @param object $new_settings The new settings.
  2835. * @return object
  2836. */
  2837. static public function process_module_settings( $module, $new_settings ) {
  2838. // Get a new node instance to work with.
  2839. $class = get_class( self::$modules[ $module->settings->type ] );
  2840. $instance = new $class();
  2841. $instance->node = $module->node;
  2842. $instance->parent = $module->parent;
  2843. $instance->settings = $module->settings;
  2844. // Run node delete to clear any cache.
  2845. $instance->delete();
  2846. // Run node update.
  2847. $instance->settings = $new_settings;
  2848. $new_settings = $instance->update( $new_settings );
  2849. return $new_settings;
  2850. }
  2851. /**
  2852. * Returns a cloned settings object for a module.
  2853. *
  2854. * @since 1.9
  2855. * @param object $settings
  2856. * @return object
  2857. */
  2858. static public function clone_module_settings( $settings ) {
  2859. $new_settings = new stdClass;
  2860. foreach ( $settings as $key => $val ) {
  2861. $new_settings->$key = $val;
  2862. }
  2863. return $new_settings;
  2864. }
  2865. /**
  2866. * Returns the default settings for a module or
  2867. * all modules if type is null.
  2868. *
  2869. * @since 1.0
  2870. * @param string $type The type of module.
  2871. * @return object|array
  2872. */
  2873. static public function get_module_defaults( $type = null ) {
  2874. if ( $type ) {
  2875. $defaults = new StdClass();
  2876. if ( isset( self::$modules[ $type ]->form ) ) {
  2877. $defaults = self::get_settings_form_defaults( $type );
  2878. $defaults = self::merge_nested_module_defaults( $type, $defaults );
  2879. $defaults->type = $type;
  2880. }
  2881. } else {
  2882. $defaults = array();
  2883. foreach ( self::$modules as $module ) {
  2884. $defaults[ $module->slug ] = self::get_module_defaults( $module->slug );
  2885. }
  2886. }
  2887. return $defaults;
  2888. }
  2889. /**
  2890. * Merges the default settings for nested forms in a module.
  2891. *
  2892. * @since 1.7
  2893. * @param string $type The type of module.
  2894. * @param object $settings The module settings object.
  2895. * @return object
  2896. */
  2897. static public function merge_nested_module_defaults( $type, $settings ) {
  2898. return self::merge_nested_form_defaults( 'module', $type, $settings );
  2899. }
  2900. /**
  2901. * Returns an array of data for each core WordPress widget.
  2902. *
  2903. * @since 1.0
  2904. * @return array
  2905. */
  2906. static public function get_wp_widgets() {
  2907. global $wp_widget_factory;
  2908. $widgets = array();
  2909. // These are known widgets that won't work in the builder.
  2910. $exclude = apply_filters( 'fl_get_wp_widgets_exclude', array(
  2911. 'WP_Widget_Media_Audio',
  2912. 'WP_Widget_Media_Image',
  2913. 'WP_Widget_Media_Video',
  2914. 'WP_Widget_Media_Gallery',
  2915. 'WP_Widget_Text',
  2916. 'WP_Widget_Custom_HTML',
  2917. ) );
  2918. foreach ( $wp_widget_factory->widgets as $class => $widget ) {
  2919. if ( in_array( $class, $exclude ) ) {
  2920. continue;
  2921. }
  2922. $widget->class = $class;
  2923. $widget->isWidget = true; // @codingStandardsIgnoreLine
  2924. $widget->category = __( 'WordPress Widgets', 'fl-builder' );
  2925. $widgets[ $widget->name ] = $widget;
  2926. }
  2927. ksort( $widgets );
  2928. return $widgets;
  2929. }
  2930. /**
  2931. * Returns an array of data for all registered sidebars.
  2932. *
  2933. * @since 1.0
  2934. * @return array
  2935. */
  2936. static public function get_wp_sidebars() {
  2937. global $wp_registered_sidebars;
  2938. $sidebars = array();
  2939. foreach ( $wp_registered_sidebars as $sidebar ) {
  2940. $sidebars[ $sidebar['name'] ] = $sidebar;
  2941. }
  2942. ksort( $sidebars );
  2943. return $sidebars;
  2944. }
  2945. /**
  2946. * Returns an array of column group data.
  2947. *
  2948. * @since 2.0
  2949. * @return array
  2950. */
  2951. static public function get_column_groups() {
  2952. $cols = array(
  2953. array(
  2954. 'name' => __( '1 Column', 'fl-builder' ),
  2955. 'id' => '1-col',
  2956. 'count' => 1,
  2957. ),
  2958. array(
  2959. 'name' => __( '2 Columns', 'fl-builder' ),
  2960. 'id' => '2-cols',
  2961. 'count' => 2,
  2962. ),
  2963. array(
  2964. 'name' => __( '3 Columns', 'fl-builder' ),
  2965. 'id' => '3-cols',
  2966. 'count' => 3,
  2967. ),
  2968. array(
  2969. 'name' => __( '4 Columns', 'fl-builder' ),
  2970. 'id' => '4-cols',
  2971. 'count' => 4,
  2972. ),
  2973. array(
  2974. 'name' => __( '5 Columns', 'fl-builder' ),
  2975. 'id' => '5-cols',
  2976. 'count' => 5,
  2977. ),
  2978. array(
  2979. 'name' => __( '6 Columns', 'fl-builder' ),
  2980. 'id' => '6-cols',
  2981. 'count' => 6,
  2982. ),
  2983. array(
  2984. 'name' => __( 'Left Sidebar', 'fl-builder' ),
  2985. 'id' => 'left-sidebar',
  2986. 'count' => 2,
  2987. ),
  2988. array(
  2989. 'name' => __( 'Right Sidebar', 'fl-builder' ),
  2990. 'id' => 'right-sidebar',
  2991. 'count' => 2,
  2992. ),
  2993. array(
  2994. 'name' => __( 'Left & Right Sidebar', 'fl-builder' ),
  2995. 'id' => 'left-right-sidebar',
  2996. 'count' => 3,
  2997. ),
  2998. );
  2999. return $cols;
  3000. }
  3001. /**
  3002. * Loads the files for all core builder settings.
  3003. *
  3004. * @since 1.0
  3005. * @return void
  3006. */
  3007. static public function load_settings() {
  3008. require_once FL_BUILDER_DIR . 'includes/global-settings.php';
  3009. require_once FL_BUILDER_DIR . 'includes/layout-settings.php';
  3010. require_once FL_BUILDER_DIR . 'includes/row-settings.php';
  3011. require_once FL_BUILDER_DIR . 'includes/column-settings.php';
  3012. require_once FL_BUILDER_DIR . 'includes/module-settings.php';
  3013. }
  3014. /**
  3015. * Register a settings form with the builder.
  3016. *
  3017. * @since 1.0
  3018. * @param string $id The form id.
  3019. * @param array $form The form data.
  3020. * @return void
  3021. */
  3022. static public function register_settings_form( $id, $form ) {
  3023. /**
  3024. * Use this filter to modify the config array for a settings form when it is registered.
  3025. * @see fl_builder_register_settings_form
  3026. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  3027. */
  3028. self::$settings_forms[ $id ] = apply_filters( 'fl_builder_register_settings_form', $form, $id );
  3029. // Since 2.0 we need to store the form ID on each tab to ensure that
  3030. // it's always available for rendering forms in JS on the frontend.
  3031. if ( isset( self::$settings_forms[ $id ]['tabs'] ) ) {
  3032. foreach ( self::$settings_forms[ $id ]['tabs'] as $tab_id => $tab ) {
  3033. self::$settings_forms[ $id ]['tabs'][ $tab_id ]['form_id'] = $id;
  3034. }
  3035. } else {
  3036. self::$settings_forms[ $id ]['form_id'] = $id;
  3037. }
  3038. }
  3039. /**
  3040. * Returns the data for a settings form.
  3041. *
  3042. * @since 1.0
  3043. * @param string $id The form id.
  3044. * @return array
  3045. */
  3046. static public function get_settings_form( $id ) {
  3047. return isset( self::$settings_forms[ $id ] ) ? self::$settings_forms[ $id ] : false;
  3048. }
  3049. /**
  3050. * Returns an array of fields in a settings form.
  3051. *
  3052. * @since 1.0
  3053. * @param array|string $form The form data array or the form key. If key, group must be set as well.
  3054. * @param string The form group. Either general or module.
  3055. * @return array
  3056. */
  3057. static public function get_settings_form_fields( $form, $group = null ) {
  3058. $fields = array();
  3059. if ( 'string' === gettype( $form ) ) {
  3060. if ( 'general' === $group ) {
  3061. $form = FLBuilderModel::$settings_forms[ $form ]['tabs'];
  3062. } elseif ( 'module' === $group ) {
  3063. $form = FLBuilderModel::$modules[ $form ]->form;
  3064. } else {
  3065. return $fields;
  3066. }
  3067. }
  3068. foreach ( (array) $form as $tab ) {
  3069. if ( isset( $tab['sections'] ) ) {
  3070. foreach ( $tab['sections'] as $section ) {
  3071. if ( isset( $section['fields'] ) ) {
  3072. foreach ( $section['fields'] as $name => $field ) {
  3073. $fields[ $name ] = $field;
  3074. }
  3075. }
  3076. }
  3077. }
  3078. }
  3079. return $fields;
  3080. }
  3081. /**
  3082. * Returns a settings object with the defaults for a form.
  3083. *
  3084. * @since 1.0
  3085. * @param string $type The type of form.
  3086. * @return object
  3087. */
  3088. static public function get_settings_form_defaults( $type ) {
  3089. // Check to see if the defaults are cached first.
  3090. if ( isset( self::$settings_form_defaults[ $type ] ) ) {
  3091. return self::$settings_form_defaults[ $type ];
  3092. }
  3093. // They aren't cached, let's get them.
  3094. $defaults = new StdClass();
  3095. // Check the registered forms first.
  3096. if ( isset( self::$settings_forms[ $type ] ) ) {
  3097. $form_type = $type;
  3098. $tabs = self::$settings_forms[ $type ]['tabs'];
  3099. } elseif ( isset( self::$modules[ $type ] ) ) {
  3100. $form_type = $type . '-module';
  3101. $tabs = self::$modules[ $type ]->form;
  3102. } else {
  3103. return $defaults;
  3104. }
  3105. // Get the fields.
  3106. $fields = self::get_settings_form_fields( $tabs );
  3107. // Handle dimension fields. We have to do it this way for backwards compat
  3108. // with old margin, padding, and border fields as the settings expect margin_top
  3109. // or margin_bottom to exist instead of just the margin key.
  3110. foreach ( $fields as $name => $field ) {
  3111. if ( 'dimension' == $field['type'] ) {
  3112. foreach ( array( 'top', 'right', 'bottom', 'left' ) as $position ) {
  3113. $fields[ $name . '_' . $position ] = $field;
  3114. }
  3115. unset( $fields[ $name ] );
  3116. }
  3117. }
  3118. // Loop through the fields and get the defaults.
  3119. foreach ( $fields as $name => $field ) {
  3120. $default = isset( $field['default'] ) ? $field['default'] : '';
  3121. $is_multiple = isset( $field['multiple'] ) && true === $field['multiple'];
  3122. $supports_multiple = 'editor' != $field['type'] && 'photo' != $field['type'];
  3123. $responsive = isset( $field['responsive'] ) && $field['responsive'] ? $field['responsive'] : false;
  3124. $responsive_name = '';
  3125. if ( $is_multiple && $supports_multiple ) {
  3126. $defaults->$name = is_array( $default ) ? $default : array( $default );
  3127. } elseif ( $responsive ) {
  3128. foreach ( array( 'default', 'medium', 'responsive' ) as $device ) {
  3129. $responsive_name = $name . ( 'default' == $device ? '' : '_' . $device );
  3130. if ( is_array( $responsive ) && isset( $responsive['default'] ) && isset( $responsive['default'][ $device ] ) ) {
  3131. $defaults->{ $responsive_name } = $responsive['default'][ $device ];
  3132. } elseif ( 'default' == $device ) {
  3133. $defaults->$name = $default;
  3134. } else {
  3135. $defaults->{ $responsive_name } = '';
  3136. }
  3137. }
  3138. } else {
  3139. $defaults->$name = $default;
  3140. }
  3141. }
  3142. /**
  3143. * Use this filter to change the defaults for any of the settings forms in the builder including global, row, column and module settings.
  3144. * @see fl_builder_settings_form_defaults
  3145. * @link https://kb.wpbeaverbuilder.com/article/117-plugin-filter-reference
  3146. */
  3147. self::$settings_form_defaults[ $type ] = apply_filters( 'fl_builder_settings_form_defaults', $defaults, $form_type );
  3148. return self::$settings_form_defaults[ $type ];
  3149. }
  3150. /**
  3151. * Merges the default settings for nested forms.
  3152. *
  3153. * @since 1.10.8
  3154. * @param string $type The type of form. Either general or module.
  3155. * @param string $form The form ID.
  3156. * @param object $settings The module settings object.
  3157. * @return object
  3158. */
  3159. static public function merge_nested_form_defaults( $type, $form, $settings ) {
  3160. // Get the fields.
  3161. if ( 'module' === $type && isset( self::$modules[ $form ] ) ) {
  3162. $fields = self::get_settings_form_fields( self::$modules[ $form ]->form );
  3163. } elseif ( isset( self::$settings_forms[ $form ] ) ) {
  3164. $fields = self::get_settings_form_fields( self::$settings_forms[ $form ]['tabs'] );
  3165. } else {
  3166. return $settings;
  3167. }
  3168. // Loop through the settings.
  3169. foreach ( $settings as $key => $val ) {
  3170. // Make sure this field is a nested form.
  3171. if ( ! isset( $fields[ $key ]['form'] ) ) {
  3172. continue;
  3173. }
  3174. // Get the nested form defaults.
  3175. $nested_defaults = self::get_settings_form_defaults( $fields[ $key ]['form'] );
  3176. // Merge the defaults.
  3177. if ( is_array( $val ) ) {
  3178. foreach ( $val as $nested_key => $nested_val ) {
  3179. $settings->{ $key }[ $nested_key ] = (object) array_merge( (array) $nested_defaults, (array) $nested_val );
  3180. }
  3181. } elseif ( ! empty( $settings->{ $key } ) ) {
  3182. $settings->{ $key } = (object) array_merge( (array) $nested_defaults, (array) $settings->{ $key } );
  3183. } else {
  3184. $settings->{ $key } = (object) $nested_defaults;
  3185. }
  3186. }
  3187. return $settings;
  3188. }
  3189. /**
  3190. * Save the settings for a node.
  3191. *
  3192. * @since 1.0
  3193. * @param string $node_id The node ID.
  3194. * @param object $settings The settings to save.
  3195. * @return void
  3196. */
  3197. static public function save_settings( $node_id = null, $settings = null ) {
  3198. $node = self::get_node( $node_id );
  3199. $new_settings = (object) array_merge( (array) $node->settings, (array) $settings );
  3200. $template_post_id = self::is_node_global( $node );
  3201. // Process the settings.
  3202. $new_settings = self::process_node_settings( $node, $new_settings );
  3203. // Save the settings to the node.
  3204. $data = self::get_layout_data();
  3205. $data[ $node_id ]->settings = $new_settings;
  3206. // Update the layout data.
  3207. self::update_layout_data( $data );
  3208. // Save settings for a global node template?
  3209. if ( $template_post_id && ! self::is_post_node_template() ) {
  3210. // Get the template data.
  3211. $template_data = self::get_layout_data( 'published', $template_post_id );
  3212. // Update the template node settings.
  3213. $template_data[ $node->template_node_id ]->settings = $new_settings;
  3214. // Save the template data.
  3215. self::update_layout_data( $template_data, 'published', $template_post_id );
  3216. self::update_layout_data( $template_data, 'draft', $template_post_id );
  3217. // Delete the template asset cache.
  3218. self::delete_all_asset_cache( $template_post_id );
  3219. self::delete_node_template_asset_cache( $template_post_id );
  3220. }
  3221. // Return the processed settings and new layout.
  3222. return array(
  3223. 'node_id' => $node->node,
  3224. 'settings' => $new_settings,
  3225. 'layout' => FLBuilderAJAXLayout::render(),
  3226. );
  3227. }
  3228. /**
  3229. * Sanitizes settings for a form.
  3230. *
  3231. * @since 2.0
  3232. * @param string $form
  3233. * @param string $group
  3234. * @param object $settings
  3235. * @return object
  3236. */
  3237. static public function sanitize_settings( $settings, $form, $group ) {
  3238. $fields = FLBuilderModel::get_settings_form_fields( $form, $group );
  3239. foreach ( $settings as $name => $value ) {
  3240. if ( ! isset( $fields[ $name ] ) ) {
  3241. continue;
  3242. } elseif ( isset( $fields[ $name ]['sanitize'] ) ) {
  3243. $settings->$name = call_user_func_array( $fields[ $name ]['sanitize'], array( $value ) );
  3244. }
  3245. }
  3246. return $settings;
  3247. }
  3248. /**
  3249. * Adds slashes to settings going into the database as WordPress
  3250. * removes them when we save using update_metadata. This is done
  3251. * to ensure slashes in user input aren't removed.
  3252. *
  3253. * @since 1.5.6
  3254. * @param mixed $data The data to slash.
  3255. * @return mixed The slashed data.
  3256. */
  3257. static public function slash_settings( $data ) {
  3258. if ( is_array( $data ) ) {
  3259. foreach ( $data as $key => $val ) {
  3260. $data[ $key ] = self::slash_settings( $val );
  3261. }
  3262. } elseif ( is_object( $data ) ) {
  3263. foreach ( $data as $key => $val ) {
  3264. $data->$key = self::slash_settings( $val );
  3265. }
  3266. } elseif ( is_string( $data ) ) {
  3267. $data = wp_slash( $data );
  3268. }
  3269. return $data;
  3270. }
  3271. /**
  3272. * Merge defaults into a settings object.
  3273. *
  3274. * @since 1.0
  3275. * @param object $settings Reference to a settings object.
  3276. * @param array $defaults The defaults to merge in.
  3277. * @return void
  3278. */
  3279. static public function default_settings( &$settings, $defaults ) {
  3280. foreach ( $defaults as $name => $value ) {
  3281. if ( ! isset( $settings->$name ) ) {
  3282. $settings->$name = $value;
  3283. }
  3284. }
  3285. }
  3286. /**
  3287. * Get the global builder settings.
  3288. *
  3289. * @since 1.0
  3290. * @return object
  3291. */
  3292. static public function get_global_settings() {
  3293. if ( null === self::$global_settings ) {
  3294. $settings = get_option( '_fl_builder_settings' );
  3295. $defaults = self::get_settings_form_defaults( 'global' );
  3296. if ( ! $settings ) {
  3297. $settings = new StdClass();
  3298. }
  3299. // Merge in defaults and cache settings
  3300. self::$global_settings = (object) array_merge( (array) $defaults, (array) $settings );
  3301. self::$global_settings = self::merge_nested_form_defaults( 'general', 'global', self::$global_settings );
  3302. }
  3303. return self::$global_settings;
  3304. }
  3305. /**
  3306. * Save the global builder settings.
  3307. *
  3308. * @since 1.0
  3309. * @param array $settings The new global settings.
  3310. * @return object
  3311. */
  3312. static public function save_global_settings( $settings = array() ) {
  3313. $old_settings = self::get_global_settings();
  3314. $new_settings = (object) array_merge( (array) $old_settings, (array) $settings );
  3315. self::delete_asset_cache_for_all_posts();
  3316. self::$global_settings = null;
  3317. update_option( '_fl_builder_settings', $new_settings );
  3318. return self::get_global_settings();
  3319. }
  3320. /**
  3321. * Duplicate the current post.
  3322. *
  3323. * @since 1.0
  3324. * @return int The new post ID.
  3325. */
  3326. static public function duplicate_post() {
  3327. global $wpdb;
  3328. $post_id = self::get_post_id();
  3329. $post = get_post( $post_id );
  3330. $current_user = wp_get_current_user();
  3331. $template_id = false;
  3332. // Duplicate the post.
  3333. $data = array(
  3334. 'comment_status' => $post->comment_status,
  3335. 'ping_status' => $post->ping_status,
  3336. 'post_author' => $current_user->ID,
  3337. 'post_content' => $post->post_content,
  3338. 'post_excerpt' => $post->post_excerpt,
  3339. 'post_name' => $post->post_name . '-copy',
  3340. 'post_parent' => $post->post_parent,
  3341. 'post_password' => $post->post_password,
  3342. 'post_status' => 'draft',
  3343. 'post_title' => sprintf( _x( 'Copy of %s', '%s stands for post/page title.', 'fl-builder' ), $post->post_title ),
  3344. 'post_type' => $post->post_type,
  3345. 'to_ping' => $post->to_ping,
  3346. 'menu_order' => $post->menu_order,
  3347. );
  3348. // Get the new post id.
  3349. $new_post_id = wp_insert_post( $data );
  3350. // Duplicate post meta.
  3351. $post_meta = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $post_id ) );
  3352. if ( count( $post_meta ) !== 0 ) {
  3353. $sql = "INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) ";
  3354. foreach ( $post_meta as $meta_info ) {
  3355. $meta_key = $meta_info->meta_key;
  3356. if ( '_fl_builder_template_id' == $meta_key ) {
  3357. $meta_value = self::generate_node_id();
  3358. } else {
  3359. $meta_value = addslashes( $meta_info->meta_value );
  3360. }
  3361. $sql_select[] = "SELECT {$new_post_id}, '{$meta_key}', '{$meta_value}'";
  3362. }
  3363. $sql .= implode( ' UNION ALL ', $sql_select );
  3364. // @codingStandardsIgnoreStart
  3365. $wpdb->query($sql);
  3366. // @codingStandardsIgnoreEnd
  3367. }
  3368. // Duplicate post terms.
  3369. $taxonomies = get_object_taxonomies( $post->post_type );
  3370. foreach ( $taxonomies as $taxonomy ) {
  3371. $post_terms = wp_get_object_terms( $post_id, $taxonomy );
  3372. for ( $i = 0; $i < count( $post_terms ); $i++ ) {
  3373. wp_set_object_terms( $new_post_id, $post_terms[ $i ]->slug, $taxonomy, true );
  3374. }
  3375. }
  3376. // Get the duplicated layout data.
  3377. $data = self::get_layout_data( 'published', $new_post_id );
  3378. // Generate new node ids.
  3379. $data = self::generate_new_node_ids( $data );
  3380. // Update template ID and template node ID
  3381. $template_id = get_post_meta( $new_post_id, '_fl_builder_template_id', true );
  3382. if ( $template_id ) {
  3383. foreach ( $data as $node_id => $node ) {
  3384. $data[ $node_id ]->template_id = $template_id;
  3385. $data[ $node_id ]->template_node_id = $node_id;
  3386. }
  3387. }
  3388. // Save the duplicated layout data.
  3389. self::update_layout_data( $data, 'published', $new_post_id );
  3390. // Also update draft data
  3391. self::update_layout_data( $data, 'draft', $new_post_id );
  3392. // Return the new post id.
  3393. return $new_post_id;
  3394. }
  3395. /**
  3396. * Deletes all layout data and asset cache for a post.
  3397. *
  3398. * @since 1.0
  3399. * @param int $post_id The post ID to delete data and cache for.
  3400. * @return void
  3401. */
  3402. static public function delete_post( $post_id ) {
  3403. // If this is a global template, unlink it from other posts.
  3404. self::unlink_global_node_template_from_all_posts( $post_id );
  3405. // Delete all published and draft data.
  3406. self::delete_layout_data( 'published', $post_id );
  3407. self::delete_layout_data( 'draft', $post_id );
  3408. // Delete all css and js.
  3409. self::delete_all_asset_cache( $post_id );
  3410. }
  3411. /**
  3412. * Save a revision of a builder layout.
  3413. *
  3414. * @since 1.0
  3415. * @param int $post_id
  3416. * @return void
  3417. */
  3418. static public function save_revision( $post_id ) {
  3419. $parent_id = wp_is_post_revision( $post_id );
  3420. if ( $parent_id ) {
  3421. $parent = get_post( $parent_id );
  3422. $data = self::get_layout_data( 'published', $parent->ID );
  3423. $settings = self::get_layout_settings( 'published', $parent->ID );
  3424. if ( ! empty( $data ) ) {
  3425. self::update_layout_data( $data, 'published', $post_id );
  3426. self::update_layout_settings( $settings, 'published', $post_id );
  3427. }
  3428. }
  3429. }
  3430. /**
  3431. * Restore a revision of a builder layout.
  3432. *
  3433. * @since 1.0
  3434. * @param int $post_id
  3435. * @param int $revision_id
  3436. * @return void
  3437. */
  3438. static public function restore_revision( $post_id, $revision_id ) {
  3439. $post = get_post( $post_id );
  3440. $revision = get_post( $revision_id );
  3441. if ( $revision ) {
  3442. $data = self::get_layout_data( 'published', $revision->ID );
  3443. $settings = self::get_layout_settings( 'published', $revision->ID );
  3444. if ( ! empty( $data ) ) {
  3445. self::update_layout_data( $data, 'published', $post_id );
  3446. self::update_layout_data( $data, 'draft', $post_id );
  3447. self::update_layout_settings( $settings, 'published', $post_id );
  3448. self::update_layout_settings( $settings, 'draft', $post_id );
  3449. } else {
  3450. self::delete_layout_data( 'published', $post_id );
  3451. self::delete_layout_data( 'draft', $post_id );
  3452. self::delete_layout_settings( 'published', $post_id );
  3453. self::delete_layout_settings( 'draft', $post_id );
  3454. }
  3455. self::delete_all_asset_cache( $post_id );
  3456. }
  3457. }
  3458. /**
  3459. * Get all of the layout data for a post. We use get_metadata
  3460. * here instead of get_post_meta to ensure revisions are queried accordingly.
  3461. *
  3462. * @since 1.0
  3463. * @param string $status Either published or draft.
  3464. * @param int $post_id The ID of the post to get data for.
  3465. * @return array
  3466. */
  3467. static public function get_layout_data( $status = null, $post_id = null ) {
  3468. $post_id = ! $post_id ? self::get_post_id() : $post_id;
  3469. $status = ! $status ? self::get_node_status() : $status;
  3470. // Get published data?
  3471. if ( 'published' == $status || 'revision' == get_post_type( $post_id ) ) {
  3472. if ( isset( self::$published_layout_data[ $post_id ] ) ) {
  3473. $data = self::$published_layout_data[ $post_id ];
  3474. } else {
  3475. $data = get_metadata( 'post', $post_id, '_fl_builder_data', true );
  3476. $data = self::clean_layout_data( $data );
  3477. self::$published_layout_data[ $post_id ] = apply_filters( 'fl_builder_get_layout_metadata', $data, $status, $post_id );
  3478. }
  3479. } elseif ( 'draft' == $status ) {
  3480. if ( isset( self::$draft_layout_data[ $post_id ] ) ) {
  3481. $data = self::$draft_layout_data[ $post_id ];
  3482. } else {
  3483. $data = get_metadata( 'post', $post_id, '_fl_builder_draft', true );
  3484. $data = self::clean_layout_data( $data );
  3485. self::$draft_layout_data[ $post_id ] = apply_filters( 'fl_builder_get_layout_metadata', $data, $status, $post_id );
  3486. }
  3487. }
  3488. // Make sure we have an array.
  3489. if ( empty( $data ) ) {
  3490. $data = array();
  3491. }
  3492. // Clone the layout data to ensure the cache remains intact.
  3493. foreach ( $data as $node_id => $node ) {
  3494. if ( is_object( $node ) ) {
  3495. $data[ $node_id ] = clone $node;
  3496. }
  3497. }
  3498. // Return the data.
  3499. return apply_filters( 'fl_builder_layout_data', $data, $status, $post_id );
  3500. }
  3501. /**
  3502. * Update the layout data for a post. We use update_metadata
  3503. * here instead of update_post_meta to ensure revisions are updated accordingly.
  3504. *
  3505. * @since 1.0
  3506. * @param array $data The layout data to update.
  3507. * @param string $status Either published or draft.
  3508. * @param int $post_id The ID of the post to update.
  3509. * @return void
  3510. */
  3511. static public function update_layout_data( $data, $status = null, $post_id = null ) {
  3512. $post_id = ! $post_id ? self::get_post_id() : $post_id;
  3513. $status = ! $status ? self::get_node_status() : $status;
  3514. $key = 'published' == $status ? '_fl_builder_data' : '_fl_builder_draft';
  3515. $raw_data = get_metadata( 'post', $post_id, $key );
  3516. $data = self::slash_settings( self::clean_layout_data( $data ) );
  3517. // Update the data.
  3518. if ( 0 === count( $raw_data ) ) {
  3519. add_metadata( 'post', $post_id, $key, $data );
  3520. } else {
  3521. update_metadata( 'post', $post_id, $key, $data );
  3522. }
  3523. // Cache the data.
  3524. if ( 'published' == $status ) {
  3525. self::$published_layout_data[ $post_id ] = $data;
  3526. } elseif ( 'draft' == $status ) {
  3527. self::$draft_layout_data[ $post_id ] = $data;
  3528. }
  3529. }
  3530. /**
  3531. * Delete the layout data for a post.
  3532. *
  3533. * @since 1.0
  3534. * @param string $status Either published or draft.
  3535. * @param int $post_id The ID of the post to delete data.
  3536. * @return void
  3537. */
  3538. static public function delete_layout_data( $status = null, $post_id = null ) {
  3539. // Make sure we have a status to delete.
  3540. if ( ! $status ) {
  3541. return;
  3542. }
  3543. // Get the post id.
  3544. $post_id = ! $post_id ? self::get_post_id() : $post_id;
  3545. // Get the data to delete.
  3546. $data = self::get_layout_data( $status, $post_id );
  3547. // Delete the nodes.
  3548. foreach ( $data as $node ) {
  3549. self::call_module_delete( $node );
  3550. }
  3551. // Update the layout data.
  3552. self::update_layout_data( array(), $status, $post_id );
  3553. }
  3554. /**
  3555. * Ensures the integrity of layout data key/value pairs.
  3556. *
  3557. * Also makes sure we're not serializing any FLBuilderModule
  3558. * instances because those are too big and bloat the data array.
  3559. *
  3560. * @since 1.0
  3561. * @param array $data An array of layout data.
  3562. * @return array
  3563. */
  3564. static public function clean_layout_data( $data = array() ) {
  3565. $cleaned = array();
  3566. if ( is_array( $data ) ) {
  3567. foreach ( $data as $node ) {
  3568. if ( is_object( $node ) && isset( $node->node ) ) {
  3569. if ( is_a( $node, 'FLBuilderModule' ) ) {
  3570. $cleaned[ $node->node ] = new StdClass();
  3571. $cleaned[ $node->node ]->node = $node->node;
  3572. $cleaned[ $node->node ]->type = $node->type;
  3573. $cleaned[ $node->node ]->parent = $node->parent;
  3574. $cleaned[ $node->node ]->position = $node->position;
  3575. $cleaned[ $node->node ]->settings = $node->settings;
  3576. } else {
  3577. $cleaned[ $node->node ] = $node;
  3578. }
  3579. }
  3580. }
  3581. }
  3582. return $cleaned;
  3583. }
  3584. /**
  3585. * Detect if the current layout has previously drafted changes.
  3586. *
  3587. * @since 2.0
  3588. * @return bool
  3589. */
  3590. static public function layout_has_drafted_changes() {
  3591. $post_id = FLBuilderModel::get_post_id();
  3592. $published = serialize( self::get_layout_data( 'published', $post_id ) );
  3593. $draft = serialize( self::get_layout_data( 'draft', $post_id ) );
  3594. if ( $published != $draft ) {
  3595. return true;
  3596. }
  3597. return false;
  3598. }
  3599. /**
  3600. * Get the builder settings for a layout.
  3601. *
  3602. * @since 1.7
  3603. * @param string $status Either published or draft.
  3604. * @param int $post_id The ID of the post to get settings for.
  3605. * @return object
  3606. */
  3607. static public function get_layout_settings( $status = null, $post_id = null ) {
  3608. $status = ! $status ? self::get_node_status() : $status;
  3609. $post_id = ! $post_id ? self::get_post_id() : $post_id;
  3610. $key = 'published' == $status ? '_fl_builder_data_settings' : '_fl_builder_draft_settings';
  3611. $settings = get_metadata( 'post', $post_id, $key, true );
  3612. $defaults = self::get_settings_form_defaults( 'layout' );
  3613. if ( ! $settings ) {
  3614. $settings = new StdClass();
  3615. }
  3616. $settings = (object) array_merge( (array) $defaults, (array) $settings );
  3617. return apply_filters( 'fl_builder_layout_settings', $settings, $status, $post_id );
  3618. }
  3619. /**
  3620. * Updates the layout settings for a post.
  3621. *
  3622. * @since 1.7
  3623. * @param array $settings The new layout settings.
  3624. * @param string $status Either published or draft.
  3625. * @param int $post_id The ID of the post to update.
  3626. * @return object
  3627. */
  3628. static public function update_layout_settings( $settings = array(), $status = null, $post_id = null ) {
  3629. $status = ! $status ? self::get_node_status() : $status;
  3630. $post_id = ! $post_id ? self::get_post_id() : $post_id;
  3631. $key = 'published' == $status ? '_fl_builder_data_settings' : '_fl_builder_draft_settings';
  3632. $raw_settings = get_metadata( 'post', $post_id, $key );
  3633. $old_settings = self::get_layout_settings( $status, $post_id );
  3634. $new_settings = (object) array_merge( (array) $old_settings, (array) $settings );
  3635. if ( 0 === count( $raw_settings ) ) {
  3636. add_metadata( 'post', $post_id, $key, self::slash_settings( $new_settings ) );
  3637. } else {
  3638. update_metadata( 'post', $post_id, $key, self::slash_settings( $new_settings ) );
  3639. }
  3640. return $new_settings;
  3641. }
  3642. /**
  3643. * Called via AJAX to save the layout settings.
  3644. *
  3645. * @since 1.7
  3646. * @param array $settings The new layout settings.
  3647. * @param string $status Either published or draft.
  3648. * @param int $post_id The ID of the post to update.
  3649. * @return object
  3650. */
  3651. static public function save_layout_settings( $settings = array(), $status = null, $post_id = null ) {
  3652. return self::update_layout_settings( $settings, $status, $post_id );
  3653. }
  3654. /**
  3655. * Delete the layout settings for a post.
  3656. *
  3657. * @since 1.7
  3658. * @param string $status Either published or draft.
  3659. * @param int $post_id The ID of a post whose settings to delete.
  3660. * @return void
  3661. */
  3662. static public function delete_layout_settings( $status = null, $post_id = null ) {
  3663. $status = ! $status ? self::get_node_status() : $status;
  3664. $post_id = ! $post_id ? self::get_post_id() : $post_id;
  3665. $key = 'published' == $status ? '_fl_builder_data_settings' : '_fl_builder_draft_settings';
  3666. update_metadata( 'post', $post_id, $key, array() );
  3667. }
  3668. /**
  3669. * Merge two sets of layout settings together.
  3670. *
  3671. * @since 1.7
  3672. * @param object $settings The layout settings to merge into.
  3673. * @param object $merge_settings The layout settings to merge.
  3674. * @return object
  3675. */
  3676. static public function merge_layout_settings( $settings, $merge_settings ) {
  3677. $keys = array( 'css', 'js' );
  3678. foreach ( $keys as $key ) {
  3679. if ( empty( $merge_settings->{$key} ) ) {
  3680. continue;
  3681. } elseif ( strstr( $settings->{$key}, $merge_settings->{$key} ) ) {
  3682. continue;
  3683. } else {
  3684. if ( ! empty( $settings->{$key} ) ) {
  3685. $settings->{$key} .= "\n";
  3686. }
  3687. $settings->{$key} .= $merge_settings->{$key};
  3688. }
  3689. }
  3690. return $settings;
  3691. }
  3692. /**
  3693. * Clears a draft layout and saves a new draft using
  3694. * the currently published layout data.
  3695. *
  3696. * @since 1.0
  3697. * @return void
  3698. */
  3699. static public function clear_draft_layout() {
  3700. $post_id = self::get_post_id();
  3701. $data = self::get_layout_data( 'published', $post_id );
  3702. $settings = self::get_layout_settings( 'published', $post_id );
  3703. // Delete the old draft layout.
  3704. self::delete_layout_data( 'draft' );
  3705. // Save the new draft layout.
  3706. self::update_layout_data( $data, 'draft', $post_id );
  3707. // Save the new draft layout settings.
  3708. self::update_layout_settings( $settings, 'draft', $post_id );
  3709. // Clear the asset cache.
  3710. self::delete_all_asset_cache( $post_id );
  3711. }
  3712. /**
  3713. * Saves layout data when a user chooses to publish.
  3714. *
  3715. * @since 1.0
  3716. * @param bool $publish Whether to publish the parent post or not.
  3717. * @return void
  3718. */
  3719. static public function save_layout( $publish = true ) {
  3720. $editor_content = FLBuilder::render_editor_content();
  3721. $post_id = self::get_post_id();
  3722. $data = self::get_layout_data( 'draft', $post_id );
  3723. $settings = self::get_layout_settings( 'draft', $post_id );
  3724. /**
  3725. * This action allows you to hook into before the data is saved for a layout.
  3726. * @see fl_builder_before_save_layout
  3727. * @link https://kb.wpbeaverbuilder.com/article/116-plugin-action-reference
  3728. */
  3729. do_action( 'fl_builder_before_save_layout', $post_id, $publish, $data, $settings );
  3730. // Delete the old published layout.
  3731. self::delete_layout_data( 'published', $post_id );
  3732. self::delete_layout_settings( 'published', $post_id );
  3733. // Save the new published layout.
  3734. self::update_layout_data( $data, 'published', $post_id );
  3735. self::update_layout_settings( $settings, 'published', $post_id );
  3736. // Clear the asset cache.
  3737. self::delete_all_asset_cache( $post_id );
  3738. self::delete_node_template_asset_cache( $post_id );
  3739. // Enable the builder to take over the post content.
  3740. self::enable();
  3741. // Get the post status.
  3742. $post_status = get_post_status( $post_id );
  3743. // Publish the post?
  3744. if ( $publish ) {
  3745. $is_draft = strstr( $post_status, 'draft' );
  3746. $is_pending = strstr( $post_status, 'pending' );
  3747. if ( current_user_can( 'publish_posts' ) ) {
  3748. $post_status = $is_draft || $is_pending ? 'publish' : $post_status;
  3749. } elseif ( $is_draft ) {
  3750. $post_status = 'pending';
  3751. }
  3752. }
  3753. // Update the post with stripped down content.
  3754. wp_update_post(array(
  3755. 'ID' => self::get_post_id(),
  3756. 'post_status' => $post_status,
  3757. 'post_content' => $editor_content,
  3758. ));
  3759. // Rerender the assets for this layout.
  3760. FLBuilder::render_assets();
  3761. /**
  3762. * This action allows you to hook into after the data is saved for a layout.
  3763. * @see fl_builder_after_save_layout
  3764. * @link https://kb.wpbeaverbuilder.com/article/116-plugin-action-reference
  3765. */
  3766. do_action( 'fl_builder_after_save_layout', $post_id, $publish, $data, $settings );
  3767. }
  3768. /**
  3769. * Publishes the current builder layout only if the parent post
  3770. * is still a draft. The layout will be published but the parent
  3771. * post will remain a draft so the post can be scheduled and the
  3772. * layout can be viewed while the builder is not active. If the
  3773. * parent post is already published, nothing happens.
  3774. *
  3775. * @since 1.6.1
  3776. * @return void
  3777. */
  3778. static public function save_draft() {
  3779. $post_id = self::get_post_id();
  3780. $post_status = get_post_status( $post_id );
  3781. if ( strstr( $post_status, 'draft' ) ) {
  3782. self::save_layout( false );
  3783. }
  3784. do_action( 'fl_builder_after_save_draft', $post_id, $post_status );
  3785. }
  3786. /**
  3787. * Duplicates a layout for WPML when the copy from original
  3788. * button has been clicked.
  3789. *
  3790. * @since 1.1.7
  3791. * @param int $original_post_id
  3792. * @param int $new_post_id
  3793. * @return array
  3794. */
  3795. static public function duplicate_wpml_layout( $original_post_id = null, $new_post_id = null ) {
  3796. $post_data = self::get_post_data();
  3797. $original_post_id = isset( $post_data['original_post_id'] ) ? $post_data['original_post_id'] : $original_post_id;
  3798. $new_post_id = isset( $post_data['post_id'] ) ? $post_data['post_id'] : $new_post_id;
  3799. $enabled = get_post_meta( $original_post_id, '_fl_builder_enabled', true );
  3800. $published = self::get_layout_data( 'published', $original_post_id );
  3801. $draft = self::get_layout_data( 'draft', $original_post_id );
  3802. $response = array(
  3803. 'enabled' => false,
  3804. 'has_layout' => false,
  3805. );
  3806. if ( ! empty( $enabled ) ) {
  3807. update_post_meta( $new_post_id, '_fl_builder_enabled', true );
  3808. $response['enabled'] = true;
  3809. }
  3810. if ( ! empty( $published ) ) {
  3811. self::update_layout_data( $published, 'published', $new_post_id );
  3812. $response['has_layout'] = true;
  3813. }
  3814. if ( ! empty( $draft ) ) {
  3815. self::update_layout_data( $draft, 'draft', $new_post_id );
  3816. $response['has_layout'] = true;
  3817. }
  3818. return $response;
  3819. }
  3820. /**
  3821. * Returns the type of templates that are enabled.
  3822. *
  3823. * @since 1.1.3
  3824. * @return string
  3825. */
  3826. static public function get_enabled_templates() {
  3827. $value = self::get_admin_settings_option( '_fl_builder_enabled_templates', true );
  3828. return ! $value ? 'enabled' : $value;
  3829. }
  3830. /**
  3831. * Checks to see if the current post is a user template.
  3832. *
  3833. * @since 1.6.3
  3834. * @param string $type The type of user template to check for.
  3835. * @return bool
  3836. */
  3837. static public function is_post_user_template( $type = null ) {
  3838. $post = FLBuilderModel::get_post();
  3839. if ( ! $post ) {
  3840. return false;
  3841. } elseif ( 'fl-builder-template' == $post->post_type ) {
  3842. if ( null === $type ) {
  3843. return true;
  3844. } else {
  3845. $saved_type = self::get_user_template_type( $post->ID );
  3846. if ( $saved_type == $type ) {
  3847. return true;
  3848. }
  3849. }
  3850. }
  3851. return false;
  3852. }
  3853. /**
  3854. * Saves a user defined template via AJAX.
  3855. *
  3856. * @since 1.1.3
  3857. * @return void
  3858. */
  3859. static public function save_user_template( $settings = array() ) {
  3860. // Save the user template post.
  3861. $post_id = wp_insert_post(array(
  3862. 'post_title' => $settings['name'],
  3863. 'post_type' => 'fl-builder-template',
  3864. 'post_status' => 'publish',
  3865. 'ping_status' => 'closed',
  3866. 'comment_status' => 'closed',
  3867. ));
  3868. // Set the template type.
  3869. wp_set_post_terms( $post_id, 'layout', 'fl-builder-template-type' );
  3870. // Add category
  3871. $cat = isset( $settings['category'] ) ? $settings['category'] : '';
  3872. $cat_added = '';
  3873. if ( __( 'Uncategorized', 'fl-builder' ) !== $cat && 'uncategorized' !== $cat ) {
  3874. $cat_added = wp_set_object_terms( $post_id, $cat, 'fl-builder-template-category' );
  3875. }
  3876. // Get the layout data and settings to copy.
  3877. $data = self::get_layout_data();
  3878. $layout_settings = self::get_layout_settings();
  3879. // Generate new node ids.
  3880. $data = self::generate_new_node_ids( $data );
  3881. // Save the template layout data and settings.
  3882. self::update_layout_data( $data, 'published', $post_id );
  3883. self::update_layout_settings( $layout_settings, 'published', $post_id );
  3884. // Enable the builder for this template.
  3885. update_post_meta( $post_id, '_fl_builder_enabled', true );
  3886. // Allow extensions to hook into saving a user template.
  3887. do_action( 'fl_builder_after_save_user_template', $post_id );
  3888. $response = array(
  3889. 'name' => $settings['name'],
  3890. 'id' => get_post_meta( $post_id, '_fl_builder_template_id', true ),
  3891. 'postId' => $post_id,
  3892. 'image' => FL_BUILDER_URL . 'img/templates/blank.jpg',
  3893. 'kind' => 'template',
  3894. 'content' => 'layout',
  3895. 'type' => 'user',
  3896. 'isGlobal' => false,
  3897. 'link' => add_query_arg( 'fl_builder', '', get_permalink( $post_id ) ),
  3898. 'category' => array(),
  3899. );
  3900. if ( is_array( $cat_added ) && ! empty( $cat_added ) ) {
  3901. $term = get_term( $cat_added[0] );
  3902. $response['category'][ $term->slug ] = $term->name;
  3903. } else {
  3904. $response['category']['uncategorized'] = __( 'Uncategorized', 'fl-builder' );
  3905. }
  3906. return $response;
  3907. }
  3908. /**
  3909. * Returns data for all user defined templates.
  3910. *
  3911. * @since 1.1.3
  3912. * @since 1.5.7 Added support for template categories.
  3913. * @param string $type The type of user template to return.
  3914. * @return array
  3915. */
  3916. static public function get_user_templates( $type = 'layout' ) {
  3917. $categorized = array(
  3918. 'uncategorized' => array(
  3919. 'name' => _x( 'Uncategorized', 'Default user template category.', 'fl-builder' ),
  3920. 'templates' => array(),
  3921. ),
  3922. );
  3923. $posts = get_posts( array(
  3924. 'post_type' => 'fl-builder-template',
  3925. 'orderby' => 'menu_order title',
  3926. 'order' => 'ASC',
  3927. 'posts_per_page' => '-1',
  3928. 'tax_query' => array(
  3929. array(
  3930. 'taxonomy' => 'fl-builder-template-type',
  3931. 'field' => 'slug',
  3932. 'terms' => $type,
  3933. ),
  3934. ),
  3935. ) );
  3936. $templates = array();
  3937. // Loop through templates posts and build the templates array.
  3938. foreach ( $posts as $post ) {
  3939. if ( has_post_thumbnail( $post->ID ) ) {
  3940. $image_data = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium_large' );
  3941. $image = $image_data[0];
  3942. } else {
  3943. $image = FL_BUILDER_URL . 'img/templates/blank.jpg';
  3944. }
  3945. $templates[] = array(
  3946. 'id' => get_post_meta( $post->ID, '_fl_builder_template_id', true ),
  3947. 'postId' => $post->ID,
  3948. 'name' => $post->post_title,
  3949. 'image' => $image,
  3950. 'kind' => 'template',
  3951. 'type' => 'user',
  3952. 'content' => FLBuilderModel::get_user_template_type( $post->ID ),
  3953. 'isGlobal' => FLBuilderModel::is_post_global_node_template( $post->ID ),
  3954. 'link' => add_query_arg( 'fl_builder', '', get_permalink( $post->ID ) ),
  3955. 'category' => array(),
  3956. );
  3957. }
  3958. // Loop through templates and build the categorized array.
  3959. foreach ( $templates as $i => $template ) {
  3960. $cats = wp_get_post_terms( $template['postId'], 'fl-builder-template-category' );
  3961. if ( 0 === count( $cats ) || is_wp_error( $cats ) ) {
  3962. $template['category'] = array(
  3963. 'uncategorized' => __( 'Uncategorized', 'fl-builder' ),
  3964. );
  3965. $categorized['uncategorized']['templates'][] = $template;
  3966. } else {
  3967. foreach ( $cats as $cat ) {
  3968. $template['category'][ $cat->slug ] = $cat->name;
  3969. }
  3970. foreach ( $cats as $cat ) {
  3971. if ( ! isset( $categorized[ $cat->slug ] ) ) {
  3972. $categorized[ $cat->slug ] = array(
  3973. 'name' => $cat->name,
  3974. 'templates' => array(),
  3975. );
  3976. }
  3977. $categorized[ $cat->slug ]['templates'][] = $template;
  3978. }
  3979. }
  3980. $templates[ $i ] = $template;
  3981. }
  3982. // Unset the uncategorized cat if no templates.
  3983. if ( 0 === count( $categorized['uncategorized']['templates'] ) ) {
  3984. unset( $categorized['uncategorized'] );
  3985. }
  3986. // sort the categories.
  3987. asort( $categorized );
  3988. return array(
  3989. 'templates' => $templates,
  3990. 'categorized' => $categorized,
  3991. );
  3992. }
  3993. /**
  3994. * Returns the template type for a user template.
  3995. *
  3996. * @since 1.6.3
  3997. * @param int $template_id The post ID of the template.
  3998. * @return string
  3999. */
  4000. static public function get_user_template_type( $template_id = null ) {
  4001. if ( $template_id && isset( self::$node_template_types[ $template_id ] ) ) {
  4002. return self::$node_template_types[ $template_id ];
  4003. }
  4004. $post = $template_id ? get_post( $template_id ) : FLBuilderModel::get_post();
  4005. if ( ! is_object( $post ) || 'fl-builder-template' != $post->post_type ) {
  4006. return '';
  4007. } else {
  4008. $terms = wp_get_post_terms( $post->ID, 'fl-builder-template-type' );
  4009. $type = ( 0 === count( $terms ) ) ? 'layout' : $terms[0]->slug;
  4010. self::$node_template_types[ $template_id ] = $type;
  4011. return $type;
  4012. }
  4013. }
  4014. /**
  4015. * Delete a user defined template.
  4016. *
  4017. * @since 1.1.3
  4018. * @param int $template_id The post ID of the template to delete.
  4019. * @return void
  4020. */
  4021. static public function delete_user_template( $template_id = null ) {
  4022. if ( isset( $template_id ) ) {
  4023. wp_delete_post( $template_id, true );
  4024. }
  4025. }
  4026. /**
  4027. * Apply a user defined template to the current layout.
  4028. *
  4029. * @since 1.1.3
  4030. * @param int|object $template The post ID of the template to apply or a template data object.
  4031. * @param bool $append Whether to append the new template or replacing the existing layout.
  4032. * @return array
  4033. */
  4034. static public function apply_user_template( $template = null, $append = false ) {
  4035. if ( $template ) {
  4036. // Delete existing nodes and settings?
  4037. if ( ! $append ) {
  4038. self::delete_layout_data( 'draft' );
  4039. self::delete_layout_settings( 'draft' );
  4040. }
  4041. // Insert new nodes if this is not a blank template.
  4042. if ( 'blank' != $template ) {
  4043. // Get the template data if $template is not an object.
  4044. if ( ! is_object( $template ) ) {
  4045. $template_id = $template;
  4046. $template = new StdClass();
  4047. $template->nodes = self::get_layout_data( 'published', $template_id );
  4048. $template->settings = self::get_layout_settings( 'published', $template_id );
  4049. }
  4050. // Get new ids for the template nodes.
  4051. $template->nodes = self::generate_new_node_ids( $template->nodes );
  4052. // Get the existing layout data and settings.
  4053. $layout_data = self::get_layout_data();
  4054. $layout_settings = self::get_layout_settings();
  4055. // Reposition rows if we are appending.
  4056. if ( $append ) {
  4057. $row_position = self::next_node_position( 'row' );
  4058. foreach ( $template->nodes as $node_id => $node ) {
  4059. if ( 'row' == $node->type ) {
  4060. $template->nodes[ $node_id ]->position += $row_position;
  4061. }
  4062. }
  4063. }
  4064. // Merge the layout data and settings.
  4065. $data = array_merge( $layout_data, $template->nodes );
  4066. $settings = self::merge_layout_settings( $layout_settings, $template->settings );
  4067. // Update the layout data and settings.
  4068. self::update_layout_data( $data );
  4069. self::update_layout_settings( $settings );
  4070. // Delete old asset cache.
  4071. self::delete_asset_cache();
  4072. }
  4073. }
  4074. // Return the layout.
  4075. return array(
  4076. 'layout_css' => isset( $settings ) ? $settings->css : null,
  4077. 'layout' => FLBuilderAJAXLayout::render(),
  4078. 'config' => FLBuilderUISettingsForms::get_node_js_config(),
  4079. );
  4080. }
  4081. /**
  4082. * Returns true if the node templates UI is enabled, false if not.
  4083. *
  4084. * @since 1.6.3
  4085. * @return bool
  4086. */
  4087. static public function node_templates_enabled() {
  4088. $enabled_templates = self::get_enabled_templates();
  4089. if ( true === FL_BUILDER_LITE ) {
  4090. return false;
  4091. }
  4092. if ( 'core' == $enabled_templates || 'disabled' == $enabled_templates ) {
  4093. return false;
  4094. }
  4095. return true;
  4096. }
  4097. /**
  4098. * Checks to see if the current post is a node template.
  4099. *
  4100. * @since 1.6.3
  4101. * @param int $post_id If supplied, this post will be checked instead.
  4102. * @return bool
  4103. */
  4104. static public function is_post_node_template( $post_id = false ) {
  4105. $post_id = $post_id ? $post_id : self::get_post_id();
  4106. $post = get_post( $post_id );
  4107. if ( ! $post ) {
  4108. return false;
  4109. } elseif ( 'fl-builder-template' == $post->post_type ) {
  4110. $saved_type = self::get_user_template_type( $post->ID );
  4111. if ( in_array( $saved_type, array( 'row', 'column', 'module' ) ) ) {
  4112. return true;
  4113. }
  4114. }
  4115. return false;
  4116. }
  4117. /**
  4118. * Checks to see if the current post is a global node template.
  4119. *
  4120. * @since 1.6.3
  4121. * @param int $post_id If supplied, this post will be checked instead.
  4122. * @return bool
  4123. */
  4124. static public function is_post_global_node_template( $post_id = false ) {
  4125. $post_id = $post_id ? $post_id : self::get_post_id();
  4126. if ( ! self::is_post_node_template( $post_id ) ) {
  4127. return false;
  4128. }
  4129. $global = get_post_meta( $post_id, '_fl_builder_template_global', true );
  4130. if ( ! $global ) {
  4131. return false;
  4132. }
  4133. return true;
  4134. }
  4135. /**
  4136. * Checks to see if a node is a global node.
  4137. *
  4138. * @since 1.6.3
  4139. * @param object $node The node object to check.
  4140. * @return bool|int
  4141. */
  4142. static public function is_node_global( $node ) {
  4143. if ( ! isset( $node->template_id ) ) {
  4144. return false;
  4145. }
  4146. return self::get_node_template_post_id( $node->template_id );
  4147. }
  4148. /**
  4149. * Check the visibility settings that has been sets from any type of node (rows/columns/modules)
  4150. * This will be applied ONLY when the builder is not active.
  4151. *
  4152. * @param object $node The type of object to check
  4153. * @return bool
  4154. */
  4155. static public function is_node_visible( $node ) {
  4156. global $wp_the_query;
  4157. $is_visible = true;
  4158. if ( isset( $node->settings->visibility_display ) && ('' != $node->settings->visibility_display) ) {
  4159. // For logged out users
  4160. if ( 'logged_out' == $node->settings->visibility_display && ! is_user_logged_in() ) {
  4161. $is_visible = true;
  4162. } elseif ( 'logged_in' == $node->settings->visibility_display && is_user_logged_in() ) {
  4163. $is_visible = true;
  4164. // User capability setting
  4165. if ( isset( $node->settings->visibility_user_capability ) && ! empty( $node->settings->visibility_user_capability ) ) {
  4166. if ( self::current_user_has_capability( trim( $node->settings->visibility_user_capability ) ) ) {
  4167. $is_visible = true;
  4168. } else {
  4169. $is_visible = false;
  4170. }
  4171. }
  4172. } elseif ( 0 == $node->settings->visibility_display ) {
  4173. $is_visible = false;
  4174. } else {
  4175. $is_visible = false;
  4176. }
  4177. }
  4178. return apply_filters( 'fl_builder_is_node_visible', $is_visible, $node );
  4179. }
  4180. /**
  4181. * Checks if a node has visibility rules or not.
  4182. *
  4183. * @param object $node
  4184. * @return bool
  4185. */
  4186. static public function node_has_visibility_rules( $node ) {
  4187. return isset( $node->settings->visibility_display ) && ( '' !== $node->settings->visibility_display );
  4188. }
  4189. /**
  4190. * Checks to see if a node is the root node of a global template.
  4191. *
  4192. * @since 1.6.3
  4193. * @param object $node The node object to check.
  4194. * @return bool|int
  4195. */
  4196. static public function is_node_template_root( $node ) {
  4197. return self::is_node_global( $node ) && isset( $node->template_root_node );
  4198. }
  4199. /**
  4200. * Get an array of node template info.
  4201. *
  4202. * @since 1.6.3
  4203. * @param string $type The type of node template to get.
  4204. * @return array
  4205. */
  4206. static public function get_node_templates( $type = '' ) {
  4207. $posts = get_posts( array(
  4208. 'post_type' => 'fl-builder-template',
  4209. 'orderby' => 'title',
  4210. 'order' => 'ASC',
  4211. 'posts_per_page' => '-1',
  4212. 'tax_query' => array(
  4213. array(
  4214. 'taxonomy' => 'fl-builder-template-type',
  4215. 'field' => 'slug',
  4216. 'terms' => $type,
  4217. ),
  4218. ),
  4219. ) );
  4220. $templates = array();
  4221. foreach ( $posts as $post ) {
  4222. $templates[] = array(
  4223. 'id' => get_post_meta( $post->ID, '_fl_builder_template_id', true ),
  4224. 'global' => get_post_meta( $post->ID, '_fl_builder_template_global', true ),
  4225. 'link' => add_query_arg( 'fl_builder', '', get_permalink( $post->ID ) ),
  4226. 'name' => $post->post_title,
  4227. );
  4228. }
  4229. return $templates;
  4230. }
  4231. /**
  4232. * Returns the root node for a node template.
  4233. *
  4234. * @since 1.6.3
  4235. * @param string $type The type of node template.
  4236. * @param array $nodes The node template data.
  4237. * @return object
  4238. */
  4239. static public function get_node_template_root( $type = '', $nodes = array() ) {
  4240. if ( '' != $type ) {
  4241. $nodes = count( $nodes ) > 0 ? $nodes : self::get_nodes( $type );
  4242. }
  4243. foreach ( $nodes as $node ) {
  4244. if ( $type == $node->type ) {
  4245. // Root parent for column template should be null.
  4246. if ( 'column' == $type && $node->parent ) {
  4247. continue;
  4248. }
  4249. return $node;
  4250. }
  4251. }
  4252. return false;
  4253. }
  4254. /**
  4255. * Uses a node template ID to retrieve its post ID.
  4256. *
  4257. * @since 1.6.3
  4258. * @param string $template_id The node template ID as stored in the template's post meta.
  4259. * @return int
  4260. */
  4261. static public function get_node_template_post_id( $template_id ) {
  4262. if ( isset( self::$node_template_post_ids[ $template_id ] ) ) {
  4263. return self::$node_template_post_ids[ $template_id ];
  4264. } else {
  4265. $posts = get_posts( array(
  4266. 'post_type' => 'fl-builder-template',
  4267. 'post_status' => array( 'any', 'trash' ),
  4268. 'posts_per_page' => '-1',
  4269. 'post_status' => 'any',
  4270. 'meta_key' => '_fl_builder_template_id',
  4271. 'meta_value' => $template_id,
  4272. ) );
  4273. if ( 0 === count( $posts ) ) {
  4274. return false;
  4275. }
  4276. $post_id = apply_filters( 'fl_builder_node_template_post_id', $posts[0]->ID );
  4277. self::$node_template_post_ids[ $template_id ] = $post_id;
  4278. return $post_id;
  4279. }
  4280. }
  4281. /**
  4282. * Returns the edit URL for a node template.
  4283. *
  4284. * @since 1.6.3
  4285. * @param string $template_id The node template ID as stored in the template's post meta.
  4286. * @return string
  4287. */
  4288. static public function get_node_template_edit_url( $template_id ) {
  4289. return self::get_edit_url( self::get_node_template_post_id( $template_id ) );
  4290. }
  4291. /**
  4292. * Returns an array of posts that have the global node template
  4293. * with the specified ID.
  4294. *
  4295. * @since 1.6.3
  4296. * @param int $post_id The post ID of the global node template.
  4297. * @return array
  4298. */
  4299. static public function get_posts_with_global_node_template( $post_id = false ) {
  4300. $posts = array();
  4301. if ( self::is_post_global_node_template( $post_id ) ) {
  4302. $template_id = get_post_meta( $post_id, '_fl_builder_template_id', true );
  4303. $query = new WP_Query( array(
  4304. 'meta_query' => array(
  4305. 'relation' => 'OR',
  4306. array(
  4307. 'key' => '_fl_builder_data',
  4308. 'value' => $template_id,
  4309. 'compare' => 'LIKE',
  4310. ),
  4311. array(
  4312. 'key' => '_fl_builder_draft',
  4313. 'value' => $template_id,
  4314. 'compare' => 'LIKE',
  4315. )
  4316. ),
  4317. 'post_type' => 'any',
  4318. 'post_status' => 'any',
  4319. 'post__not_in' => array( $post_id ),
  4320. ) );
  4321. $posts = $query->posts;
  4322. }
  4323. return $posts;
  4324. }
  4325. /**
  4326. * Saves a node template.
  4327. *
  4328. * @since 1.6.3
  4329. * @param string $template_node_id The ID of the node to save as a template.
  4330. * @param string $settings The settings for this template.
  4331. * @return void
  4332. */
  4333. static public function save_node_template( $template_node_id, $settings ) {
  4334. $root_node = self::get_node( $template_node_id );
  4335. $nodes = self::get_nested_nodes( $template_node_id );
  4336. $template_id = self::generate_node_id();
  4337. $original_parent = $root_node->parent;
  4338. $original_position = $root_node->position;
  4339. // Save the node template post.
  4340. $post_id = wp_insert_post( array(
  4341. 'post_title' => $settings['name'],
  4342. 'post_type' => 'fl-builder-template',
  4343. 'post_status' => 'publish',
  4344. 'ping_status' => 'closed',
  4345. 'comment_status' => 'closed',
  4346. ) );
  4347. // Set the template type.
  4348. wp_set_post_terms( $post_id, $root_node->type, 'fl-builder-template-type' );
  4349. // Reset the root node's position.
  4350. $root_node->position = 0;
  4351. // Remove root parent for column template.
  4352. if ( 'column' == $root_node->type ) {
  4353. $root_node->parent = null;
  4354. $root_node->settings->size = 100;
  4355. }
  4356. // Add the root node to the nodes array.
  4357. $nodes[ $root_node->node ] = $root_node;
  4358. // Generate new node ids.
  4359. $nodes = self::generate_new_node_ids( $nodes );
  4360. // Get the root node from the template data since its ID changed.
  4361. $root_node = self::get_node_template_root( $root_node->type, $nodes );
  4362. // Add the template ID and template node ID for global templates.
  4363. if ( $settings['global'] ) {
  4364. foreach ( $nodes as $node_id => $node ) {
  4365. $nodes[ $node_id ]->template_id = $template_id;
  4366. $nodes[ $node_id ]->template_node_id = $node_id;
  4367. if ( $node_id == $root_node->node ) {
  4368. $nodes[ $node_id ]->template_root_node = true;
  4369. } elseif ( isset( $nodes[ $node_id ]->template_root_node ) ) {
  4370. unset( $nodes[ $node_id ]->template_root_node );
  4371. }
  4372. }
  4373. } else {
  4374. foreach ( $nodes as $node_id => $node ) {
  4375. if ( isset( $nodes[ $node_id ]->template_id ) ) {
  4376. unset( $nodes[ $node_id ]->template_id );
  4377. }
  4378. if ( isset( $nodes[ $node_id ]->template_node_id ) ) {
  4379. unset( $nodes[ $node_id ]->template_node_id );
  4380. }
  4381. if ( isset( $nodes[ $node_id ]->template_root_node ) ) {
  4382. unset( $nodes[ $node_id ]->template_root_node );
  4383. }
  4384. }
  4385. }
  4386. // Save the template layout data.
  4387. self::update_layout_data( $nodes, 'published', $post_id );
  4388. self::update_layout_data( $nodes, 'draft', $post_id );
  4389. // Enable the builder for this template.
  4390. update_post_meta( $post_id, '_fl_builder_enabled', true );
  4391. // Add the template ID post meta. We use a custom ID for node
  4392. // templates in case templates are imported since their WordPress
  4393. // IDs will change, breaking global templates.
  4394. update_post_meta( $post_id, '_fl_builder_template_id', $template_id );
  4395. // Add the template global flag post meta.
  4396. update_post_meta( $post_id, '_fl_builder_template_global', $settings['global'] );
  4397. // Delete the existing node and apply the template for global templates.
  4398. if ( $settings['global'] ) {
  4399. // Delete the existing node.
  4400. self::delete_node( $template_node_id );
  4401. // Apply the global template.
  4402. $root_node = self::apply_node_template( $template_id, $original_parent, $original_position );
  4403. }
  4404. // Return an array of template settings.
  4405. return array(
  4406. 'id' => $template_id,
  4407. 'global' => $settings['global'] ? true : false,
  4408. 'link' => add_query_arg( 'fl_builder', '', get_permalink( $post_id ) ),
  4409. 'name' => $settings['name'],
  4410. 'type' => $root_node->type,
  4411. 'layout' => $settings['global'] ? FLBuilderAJAXLayout::render( $root_node->node, $template_node_id ) : null,
  4412. 'config' => $settings['global'] ? FLBuilderUISettingsForms::get_node_js_config() : null,
  4413. 'postID' => $post_id,
  4414. );
  4415. }
  4416. /**
  4417. * Sets the default type for a node template when created in wp-admin.
  4418. *
  4419. * @since 1.6.3
  4420. * @param int $post_ID The post ID for the template.
  4421. * @param object $post The post object for the template.
  4422. * @param bool $update Whether this is a new post or an update.
  4423. * @return void
  4424. */
  4425. static public function set_node_template_default_type( $post_id, $post, $update ) {
  4426. global $pagenow;
  4427. if ( 'admin.php' == $pagenow && isset( $_GET['import'] ) ) {
  4428. return;
  4429. }
  4430. $post_data = self::get_post_data();
  4431. if ( $update || 'fl-builder-template' != $post->post_type ) {
  4432. return;
  4433. }
  4434. if ( isset( $post_data['fl_action'] ) && 'duplicate_post' == $post_data['fl_action'] ) {
  4435. return;
  4436. }
  4437. $type = wp_get_post_terms( $post_id, 'fl-builder-template-type' );
  4438. if ( 0 === count( $type ) ) {
  4439. wp_set_post_terms( $post_id, 'layout', 'fl-builder-template-type' );
  4440. }
  4441. }
  4442. /**
  4443. * Deletes a node template via AJAX.
  4444. *
  4445. * @since 1.6.3
  4446. * @param string $template_id The ID of node template to delete.
  4447. * @return void
  4448. */
  4449. static public function delete_node_template( $template_id ) {
  4450. // Make sure we have a template ID.
  4451. if ( ! isset( $template_id ) ) {
  4452. return;
  4453. }
  4454. // Get the post ID for the template.
  4455. $template_post_id = self::get_node_template_post_id( $template_id );
  4456. // Bail if we don't have a post ID.
  4457. if ( ! $template_post_id ) {
  4458. return;
  4459. }
  4460. // Unlink if this is a global template.
  4461. self::unlink_global_node_template_from_all_posts( $template_post_id );
  4462. // Delete the template post.
  4463. wp_delete_post( $template_post_id, true );
  4464. }
  4465. /**
  4466. * Unlinks all instances of a global node template in all posts.
  4467. *
  4468. * @since 1.6.3
  4469. * @param int $template_post_id The post ID of the template to unlink.
  4470. * @return void
  4471. */
  4472. static public function unlink_global_node_template_from_all_posts( $template_post_id ) {
  4473. if ( self::is_post_global_node_template( $template_post_id ) ) {
  4474. $posts = self::get_posts_with_global_node_template( $template_post_id );
  4475. $template_id = get_post_meta( $template_post_id, '_fl_builder_template_id', true );
  4476. foreach ( $posts as $post ) {
  4477. self::unlink_global_node_template_from_post( 'published', $post->ID, $template_post_id, $template_id );
  4478. self::unlink_global_node_template_from_post( 'draft', $post->ID, $template_post_id, $template_id );
  4479. self::delete_all_asset_cache( $post->ID );
  4480. }
  4481. }
  4482. }
  4483. /**
  4484. * Unlinks all instances of a global node template from a post's
  4485. * layout data with the specified status. Since only the root node
  4486. * of a global template is saved to a posts layout data, the child
  4487. * nodes will be saved to the post when the global template is unlinked.
  4488. *
  4489. * @since 1.6.3
  4490. * @param string $status The status of the layout data. Either draft or published.
  4491. * @param int $post_id The ID of the post to unlink from.
  4492. * @param string $template_post_id The post ID of the template to unlink from the layout data.
  4493. * @param string $template_id The ID of the template to unlink from the layout data.
  4494. * @return void
  4495. */
  4496. static public function unlink_global_node_template_from_post( $status, $post_id, $template_post_id, $template_id ) {
  4497. $template_data = self::get_layout_data( $status, $template_post_id );
  4498. $layout_data = self::get_layout_data( $status, $post_id );
  4499. $update = false;
  4500. // Loop through the layout data.
  4501. foreach ( $layout_data as $node_id => $node ) {
  4502. // Check to see if this is the global template node to unlink.
  4503. if ( isset( $node->template_id ) && $node->template_id == $template_id ) {
  4504. // Generate new node ids for the template data.
  4505. $new_data = self::generate_new_node_ids( $template_data );
  4506. // Get the root node from the template data.
  4507. $root_node = self::get_node_template_root( $node->type, $new_data );
  4508. // Remove the root node from the template data since it's already in the layout.
  4509. unset( $new_data[ $root_node->node ] );
  4510. // Update the settings for the root node in this layout.
  4511. $layout_data[ $node_id ]->settings = $root_node->settings;
  4512. // Update children with the new parent node ID.
  4513. foreach ( $new_data as $i => $n ) {
  4514. if ( $n->parent == $root_node->node ) {
  4515. $new_data[ $i ]->parent = $node->node;
  4516. }
  4517. }
  4518. // Add the template data to the layout data.
  4519. $layout_data = array_merge( $layout_data, $new_data );
  4520. // Set the update flag.
  4521. $update = true;
  4522. }
  4523. }
  4524. // Only update if we need to.
  4525. if ( $update ) {
  4526. // Remove template info from the layout data.
  4527. foreach ( $layout_data as $node_id => $node ) {
  4528. unset( $layout_data[ $node_id ]->template_id );
  4529. unset( $layout_data[ $node_id ]->template_node_id );
  4530. unset( $layout_data[ $node_id ]->template_root_node );
  4531. }
  4532. // Update the layout data.
  4533. self::update_layout_data( $layout_data, $status, $post_id );
  4534. }
  4535. }
  4536. /**
  4537. * Deletes all instances of a global node template from all posts.
  4538. *
  4539. * @since 1.6.3
  4540. * @param int $template_post_id The post ID of the template to delete.
  4541. * @return void
  4542. */
  4543. static public function delete_global_node_template_from_all_posts( $template_post_id ) {
  4544. if ( self::is_post_global_node_template( $template_post_id ) ) {
  4545. $posts = self::get_posts_with_global_node_template( $template_post_id );
  4546. $template_id = get_post_meta( $template_post_id, '_fl_builder_template_id', true );
  4547. foreach ( $posts as $post ) {
  4548. self::delete_global_node_template_from_post( 'published', $post->ID, $template_id );
  4549. self::delete_global_node_template_from_post( 'draft', $post->ID, $template_id );
  4550. self::delete_all_asset_cache( $post->ID );
  4551. }
  4552. }
  4553. }
  4554. /**
  4555. * Deletes all instances of a global node template from a post's
  4556. * layout data with the specified status.
  4557. *
  4558. * @since 1.6.3
  4559. * @param string $status The status of the layout data. Either draft or published.
  4560. * @param int $post_id The ID of the post to delete from.
  4561. * @param string $template_id The ID of the template to delete from the layout data.
  4562. * @return void
  4563. */
  4564. static public function delete_global_node_template_from_post( $status, $post_id, $template_id ) {
  4565. $layout_data = self::get_layout_data( $status, $post_id );
  4566. $update = false;
  4567. // Loop through the nodes.
  4568. foreach ( $layout_data as $node_id => $node ) {
  4569. $siblings = array();
  4570. $position = 0;
  4571. // Check to see if this is the global template node to delete.
  4572. if ( isset( $node->template_id ) && $node->template_id == $template_id ) {
  4573. // Unset this node in the layout data.
  4574. unset( $layout_data[ $node_id ] );
  4575. // Find sibiling nodes to update their position.
  4576. foreach ( $layout_data as $i => $n ) {
  4577. if ( $n->parent == $node->parent ) {
  4578. $siblings[ $i ] = $n;
  4579. }
  4580. }
  4581. // Sort the sibiling nodes by position.
  4582. uasort( $siblings, array( 'FLBuilderModel', 'order_nodes' ) );
  4583. // Update sibiling node positions.
  4584. foreach ( $siblings as $i => $n ) {
  4585. $layout_data[ $i ]->position = $position;
  4586. $position++;
  4587. }
  4588. // Set the update flag.
  4589. $update = true;
  4590. }
  4591. }
  4592. // Only update if we need to.
  4593. if ( $update ) {
  4594. self::update_layout_data( $layout_data, $status, $post_id );
  4595. }
  4596. }
  4597. /**
  4598. * Applies a node template to the current layout.
  4599. *
  4600. * @since 1.6.3
  4601. * @param int $template_id The node template ID.
  4602. * @param string $parent_id The new parent node ID for the template.
  4603. * @param int $position The position of the template within the layout.
  4604. * @param object $template Optional. Template data to use instead of pulling it with the template ID.
  4605. * @return void
  4606. */
  4607. static public function apply_node_template( $template_id = null, $parent_id = null, $position = 0, $template = null ) {
  4608. $parent = ( 0 == $parent_id ) ? null : self::get_node( $parent_id );
  4609. $template_post_id = self::get_node_template_post_id( $template_id );
  4610. $is_col_template = false;
  4611. // Allow extensions to hook into applying a node template.
  4612. $override = apply_filters( 'fl_builder_override_apply_node_template', false, array(
  4613. 'template_id' => $template_id,
  4614. 'parent_id' => $parent_id,
  4615. 'position' => $position,
  4616. 'template' => $template,
  4617. 'template_post_id' => $template_post_id,
  4618. ) );
  4619. // Return if we got an override from the filter.
  4620. if ( $override ) {
  4621. return $override;
  4622. }
  4623. // Get the template data from $template if we have it.
  4624. if ( is_object( $template ) ) {
  4625. $template_data = $template->nodes;
  4626. $template_settings = $template->settings;
  4627. $type = $template->type;
  4628. $global = $template->global;
  4629. } else {
  4630. $template_data = self::get_layout_data( 'published', $template_post_id );
  4631. $template_settings = self::get_layout_settings( 'published', $template_post_id );
  4632. $type = self::get_user_template_type( $template_post_id );
  4633. $global = get_post_meta( $template_post_id, '_fl_builder_template_global', true );
  4634. }
  4635. // Generate new node ids.
  4636. $template_data = self::generate_new_node_ids( $template_data );
  4637. // Get the root node from the template data.
  4638. $root_node = self::get_node_template_root( $type, $template_data );
  4639. // Handle module and column templates.
  4640. if ( 'module' == $root_node->type || 'column' == $root_node->type ) {
  4641. // Add a new parent for module or column node templates if needed.
  4642. if ( ! $parent || 'row' == $parent->type || 'column-group' == $parent->type ) {
  4643. if ( 'module' == $root_node->type ) {
  4644. $parent_id = self::add_module_parent( $parent_id, $position );
  4645. $position = null;
  4646. } elseif ( 'column' == $root_node->type ) {
  4647. $parent_id = self::add_col_parent( $parent_id, $position );
  4648. $is_col_template = self::is_node_global( $root_node );
  4649. }
  4650. $parent = self::get_node( $parent_id );
  4651. }
  4652. // Set the node's template data if the parent is a global node.
  4653. if ( self::is_node_global( $parent ) && ! $is_col_template ) {
  4654. $template_data[ $root_node->node ]->template_id = $parent->template_id;
  4655. $template_data[ $root_node->node ]->template_node_id = $root_node->node;
  4656. unset( $template_data[ $root_node->node ]->template_root_node );
  4657. $global = true;
  4658. }
  4659. }
  4660. // Update the root node's parent.
  4661. $template_data[ $root_node->node ]->parent = ! $parent_id ? null : $parent_id;
  4662. // Get the layout data and settings.
  4663. $layout_data = self::get_layout_data( 'draft' );
  4664. $layout_settings = self::get_layout_settings( 'draft' );
  4665. // Only merge the root node for global templates.
  4666. if ( $global ) {
  4667. $layout_data[ $root_node->node ] = $template_data[ $root_node->node ];
  4668. } else {
  4669. // Merge template data.
  4670. foreach ( $template_data as $node_id => $node ) {
  4671. unset( $template_data[ $node_id ]->template_id );
  4672. unset( $template_data[ $node_id ]->template_node_id );
  4673. unset( $template_data[ $node_id ]->template_root_node );
  4674. }
  4675. $layout_data = array_merge( $layout_data, $template_data );
  4676. // Merge template settings.
  4677. $layout_settings = self::merge_layout_settings( $layout_settings, $template_settings );
  4678. }
  4679. // Update the layout data and settings.
  4680. self::update_layout_data( $layout_data );
  4681. self::update_layout_settings( $layout_settings );
  4682. // Reorder the main template node.
  4683. if ( null !== $position ) {
  4684. self::reorder_node( $root_node->node, $position );
  4685. }
  4686. // Re-size column widths
  4687. if ( 'column' == $root_node->type && 'column-group' == $parent->type ) {
  4688. self::reset_col_widths( $parent_id );
  4689. }
  4690. // Delete old asset cache.
  4691. self::delete_asset_cache();
  4692. // Return the root node.
  4693. if ( 'module' == $root_node->type ) {
  4694. return self::get_module( $root_node->node );
  4695. } else {
  4696. return $root_node;
  4697. }
  4698. }
  4699. /**
  4700. * Registers a template data file with the builder.
  4701. *
  4702. * @since 1.8
  4703. * @param string|array $path The directory path to the template data file.
  4704. * @param array $meta The collection information for this template file.
  4705. * @return void
  4706. */
  4707. static public function register_templates( $path = false, $args = array() ) {
  4708. // Check if the file exists if path is a string.
  4709. if ( is_string( $path ) && ! file_exists( $path ) ) {
  4710. return;
  4711. }
  4712. // Make sure one file exists if path is an array.
  4713. if ( is_array( $path ) ) {
  4714. $exists = false;
  4715. foreach ( $path as $file ) {
  4716. if ( file_exists( $file ) ) {
  4717. $exists = true;
  4718. }
  4719. }
  4720. if ( ! $exists ) {
  4721. return;
  4722. }
  4723. }
  4724. // Store the template data.
  4725. self::$templates[] = array(
  4726. 'group' => isset( $args['group'] ) ? $args['group'] : false,
  4727. 'path' => is_string( $path ) ? array( $path ) : $path,
  4728. );
  4729. }
  4730. /**
  4731. * Registers the core templates with the builder.
  4732. *
  4733. * @since 1.10.3
  4734. * @return void
  4735. */
  4736. static private function register_core_templates() {
  4737. $templates = glob( FL_BUILDER_DIR . 'data/*' );
  4738. $paths = array();
  4739. // glob() will return false on error so cast as an array() just in case.
  4740. foreach ( (array) $templates as $template ) {
  4741. if ( 'templates.dat' == basename( $template ) ) {
  4742. continue;
  4743. }
  4744. $paths[] = $template;
  4745. }
  4746. self::register_templates( $paths );
  4747. }
  4748. /**
  4749. * Applies a core template and can be overridden by extensions to
  4750. * apply something else that is being shown in the selector.
  4751. *
  4752. * @since 1.0
  4753. * @since 1.5.7. Added logic for overriding core templates.
  4754. * @param int $index The index of the template to apply.
  4755. * @param bool $append Whether to append the new template or replacing the existing layout.
  4756. * @param string $type The type of template to apply.
  4757. * @return void
  4758. */
  4759. static public function apply_template( $index = 0, $append = false, $type = 'layout' ) {
  4760. // Allow extensions to hook into applying a template.
  4761. $override = apply_filters( 'fl_builder_override_apply_template', false, array(
  4762. 'index' => $index,
  4763. 'append' => $append,
  4764. 'type' => $type,
  4765. ) );
  4766. // Return if we have an override from the filter.
  4767. if ( $override ) {
  4768. return $override;
  4769. }
  4770. // Apply a core template.
  4771. return self::apply_core_template( $index, $append, $type );
  4772. }
  4773. /**
  4774. * Applies a core template and cannot be overridden by extensions.
  4775. *
  4776. * @since 1.10
  4777. * @param int $index The index of the template to apply.
  4778. * @param bool $append Whether to append the new template or replacing the existing layout.
  4779. * @param string $type The type of template to apply.
  4780. * @return array
  4781. */
  4782. static public function apply_core_template( $index = 0, $append = false, $type = 'layout' ) {
  4783. $template = self::get_template( $index, $type );
  4784. $row_position = self::next_node_position( 'row' );
  4785. // Delete existing nodes and settings?
  4786. if ( ! $append ) {
  4787. self::delete_layout_data( 'draft' );
  4788. self::delete_layout_settings( 'draft' );
  4789. }
  4790. // Only move forward if we have template nodes.
  4791. if ( isset( $template->nodes ) ) {
  4792. // Get new ids for the template nodes.
  4793. $template->nodes = self::generate_new_node_ids( $template->nodes );
  4794. // Get the existing layout data and settings.
  4795. $layout_data = self::get_layout_data();
  4796. $layout_settings = self::get_layout_settings();
  4797. // Reposition rows?
  4798. if ( $append ) {
  4799. foreach ( $template->nodes as $node_id => $node ) {
  4800. if ( 'row' == $node->type ) {
  4801. $template->nodes[ $node_id ]->position += $row_position;
  4802. }
  4803. }
  4804. }
  4805. // Merge and update the layout data.
  4806. $data = array_merge( $layout_data, $template->nodes );
  4807. self::update_layout_data( $data );
  4808. // Merge and update the layout settings.
  4809. if ( isset( $template->settings ) ) {
  4810. $settings = self::merge_layout_settings( $layout_settings, $template->settings );
  4811. self::update_layout_settings( $settings );
  4812. }
  4813. }
  4814. // Delete old asset cache.
  4815. self::delete_asset_cache();
  4816. // Return the layout.
  4817. return array(
  4818. 'layout' => FLBuilderAJAXLayout::render(),
  4819. 'config' => FLBuilderUISettingsForms::get_node_js_config(),
  4820. );
  4821. }
  4822. /**
  4823. * Returns data for a core template.
  4824. *
  4825. * @since 1.0
  4826. * @param int $index The index of the template.
  4827. * @param string $type The type of template to get. Currently either layout, row or module.
  4828. * @return object
  4829. */
  4830. static public function get_template( $index, $type = 'layout' ) {
  4831. $templates = self::get_templates( $type );
  4832. $template = isset( $templates[ $index ] ) ? $templates[ $index ] : false;
  4833. if ( $template && isset( $template->nodes ) ) {
  4834. $template->nodes = maybe_unserialize( $template->nodes );
  4835. }
  4836. return $template;
  4837. }
  4838. /**
  4839. * Returns data for all core or third party templates.
  4840. *
  4841. * @since 1.0
  4842. * @param string $type Either layout, row or module
  4843. * @param bool $cached
  4844. * @return array
  4845. */
  4846. static public function get_templates( $type = 'layout', $cached = true ) {
  4847. // Pull from dat files if cached is false or we don't have saved data.
  4848. if ( ! $cached || ! self::$template_data ) {
  4849. self::$template_data = array();
  4850. foreach ( self::$templates as $args ) {
  4851. foreach ( $args['path'] as $path ) {
  4852. // Make sure the template file exists.
  4853. if ( ! file_exists( $path ) ) {
  4854. continue;
  4855. }
  4856. // Get the unserialized template data.
  4857. if ( stristr( $path, '.php' ) ) {
  4858. ob_start();
  4859. include $path;
  4860. $unserialized = unserialize( ob_get_clean() );
  4861. } else {
  4862. $unserialized = fl_maybe_fix_unserialize( file_get_contents( $path ) );
  4863. }
  4864. // Make sure we have an unserialized array.
  4865. if ( ! is_array( $unserialized ) ) {
  4866. continue;
  4867. }
  4868. // Group and cache the template data.
  4869. foreach ( $unserialized as $template_type => $template_data ) {
  4870. if ( ! isset( self::$template_data[ $template_type ] ) ) {
  4871. self::$template_data[ $template_type ] = array();
  4872. }
  4873. foreach ( $template_data as $key => $template ) {
  4874. // Add the main group to each template.
  4875. $template_data[ $key ]->group = $args['group'];
  4876. // Reserialize the node data as it's expensive to store in memory.
  4877. if ( isset( $template->nodes ) ) {
  4878. $template_data[ $key ]->nodes = serialize( $template_data[ $key ]->nodes );
  4879. }
  4880. }
  4881. self::$template_data[ $template_type ] = array_merge( self::$template_data[ $template_type ], $template_data );
  4882. }
  4883. }
  4884. }
  4885. }
  4886. $templates = isset( self::$template_data[ $type ] ) ? self::$template_data[ $type ] : array();
  4887. return apply_filters( 'fl_builder_get_templates', $templates, $type );
  4888. }
  4889. /**
  4890. * Checks to see if any templates exist.
  4891. *
  4892. * @since 1.8
  4893. * @return bool
  4894. */
  4895. static public function has_templates() {
  4896. return apply_filters( 'fl_builder_has_templates', ( count( self::get_templates() ) > 0 ) );
  4897. }
  4898. /**
  4899. * Returns template data needed for the template selector.
  4900. * Can also return data for row, column and module templates if
  4901. * a template type is passed.
  4902. *
  4903. * @since 1.5.7
  4904. * @param string $type Either layout, row or module
  4905. * @return array
  4906. */
  4907. static public function get_template_selector_data( $type = 'layout' ) {
  4908. $type = apply_filters( 'fl_builder_template_selector_data_type', $type );
  4909. $categorized = array();
  4910. $templates = array();
  4911. $core_categories = array(
  4912. 'general' => __( 'General', 'fl-builder' ),
  4913. 'landing' => __( 'Landing Pages', 'fl-builder' ),
  4914. 'company' => __( 'Content Pages', 'fl-builder' ),
  4915. );
  4916. $groups = array();
  4917. // Build the the templates array.
  4918. foreach ( self::get_templates( $type ) as $key => $template ) {
  4919. if ( 'module' == $type && isset( $template->nodes ) ) {
  4920. $nodes = maybe_unserialize( $template->nodes );
  4921. $node = array_shift( $nodes );
  4922. if ( ! isset( $node->settings ) || ! isset( self::$modules[ $node->settings->type ] ) ) {
  4923. continue;
  4924. }
  4925. }
  4926. if ( strstr( $template->image, '://' ) || strstr( $template->image, ';base64,' ) ) {
  4927. $image = $template->image;
  4928. } else {
  4929. $image = FL_BUILDER_URL . 'img/templates/' . ( empty( $template->image ) ? 'blank.jpg' : $template->image );
  4930. }
  4931. $templates[] = apply_filters( 'fl_builder_template_details', array(
  4932. 'id' => $key,
  4933. 'name' => $template->name,
  4934. 'image' => $image,
  4935. 'author' => '',
  4936. 'category' => isset( $template->category ) ? $template->category : $template->categories,
  4937. 'tags' => array(),
  4938. 'group' => $template->group,
  4939. 'type' => 'core',
  4940. 'kind' => 'template',
  4941. 'content' => ! in_array( $type, array( 'row', 'column', 'module' ) ) ? 'layout' : $type,
  4942. ), $template );
  4943. }
  4944. // Build the categorized templates array and groups array.
  4945. foreach ( $templates as $i => $template ) {
  4946. // Make sure we have a template category and it's an array.
  4947. if ( ! isset( $template['category'] ) ) {
  4948. $template['category'] = array(
  4949. 'uncategorized' => __( 'Uncategorized', 'fl-builder' ),
  4950. );
  4951. } elseif ( is_string( $template['category'] ) ) {
  4952. $template['category'] = array(
  4953. $template['category'] => $core_categories[ $template['category'] ],
  4954. );
  4955. }
  4956. // Get template group data.
  4957. $template_groups = array();
  4958. if ( ! $template['group'] ) {
  4959. // If we don't have a group, use categories as groups.
  4960. foreach ( $template['category'] as $cat_name ) {
  4961. $template_groups[] = $cat_name;
  4962. }
  4963. // Clear the categories since we're using groups instead.
  4964. $template['category'] = array(
  4965. 'none' => '',
  4966. );
  4967. } elseif ( is_string( $template['group'] ) ) {
  4968. // Make sure template group is an array.
  4969. $template_groups = array( $template['group'] );
  4970. } else {
  4971. $template_groups = $template['group'];
  4972. }
  4973. // Add to the groups array.
  4974. $template['group'] = array();
  4975. foreach ( $template_groups as $group_name ) {
  4976. $group_key = sanitize_key( $group_name );
  4977. if ( ! isset( $groups[ $group_key ] ) ) {
  4978. $groups[ $group_key ] = array(
  4979. 'name' => $group_name,
  4980. 'categories' => array(),
  4981. );
  4982. }
  4983. foreach ( $template['category'] as $cat_key => $cat_name ) {
  4984. if ( ! isset( $groups[ $group_key ]['categories'][ $cat_key ] ) ) {
  4985. $groups[ $group_key ]['categories'][ $cat_key ] = array(
  4986. 'name' => $cat_name,
  4987. );
  4988. }
  4989. }
  4990. $template['group'][] = $group_key;
  4991. }
  4992. // Add to the categorized array.
  4993. foreach ( $template['category'] as $cat_key => $cat_name ) {
  4994. // Add the category if we don't have it yet.
  4995. if ( ! isset( $categorized[ $cat_key ] ) ) {
  4996. $categorized[ $cat_key ] = array(
  4997. 'name' => $cat_name,
  4998. 'templates' => array(),
  4999. );
  5000. }
  5001. $categorized[ $cat_key ]['templates'][] = $template;
  5002. }
  5003. $templates[ $i ] = $template;
  5004. }
  5005. // Return both the templates and categorized templates array.
  5006. return apply_filters( 'fl_builder_template_selector_data', array(
  5007. 'templates' => $templates,
  5008. 'categorized' => $categorized,
  5009. 'groups' => $groups,
  5010. ), $type );
  5011. }
  5012. /**
  5013. * Returns data for row templates to be shown in the UI panel.
  5014. *
  5015. * @since 1.8
  5016. * @return array
  5017. */
  5018. static public function get_row_templates_data() {
  5019. return apply_filters( 'fl_builder_row_templates_data', self::get_template_selector_data( 'row' ) );
  5020. }
  5021. /**
  5022. * Returns data for column templates to be shown in the UI panel.
  5023. *
  5024. * @since 2.1
  5025. * @return array
  5026. */
  5027. static public function get_column_templates_data() {
  5028. return apply_filters( 'fl_builder_column_templates_data', self::get_template_selector_data( 'column' ) );
  5029. }
  5030. /**
  5031. * Returns data for module templates to be shown in the UI panel.
  5032. *
  5033. * @since 1.8
  5034. * @return array
  5035. */
  5036. static public function get_module_templates_data() {
  5037. return apply_filters( 'fl_builder_module_templates_data', self::get_template_selector_data( 'module' ) );
  5038. }
  5039. /**
  5040. * Get color presets.
  5041. *
  5042. * @since 1.6.4
  5043. * @return object
  5044. */
  5045. static public function get_color_presets() {
  5046. $settings = get_option( '_fl_builder_color_presets', array() );
  5047. return apply_filters( 'fl_builder_color_presets', $settings );
  5048. }
  5049. /**
  5050. * Save color presets.
  5051. *
  5052. * @since 1.6.4
  5053. * @param array $presets The new color presets collection.
  5054. * @return object
  5055. */
  5056. static public function save_color_presets( $presets = array() ) {
  5057. return update_option( '_fl_builder_color_presets', $presets );
  5058. }
  5059. /**
  5060. * Returns whether the UI has been white labeled or not.
  5061. *
  5062. * @since 2.1
  5063. * @return bool
  5064. */
  5065. static public function is_white_labeled() {
  5066. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  5067. return FLBuilderWhiteLabel::is_white_labeled();
  5068. }
  5069. return false;
  5070. }
  5071. /**
  5072. * Returns whether the inline editing is enabled.
  5073. *
  5074. * @since 2.1
  5075. * @return bool
  5076. */
  5077. static public function is_inline_enabled() {
  5078. return apply_filters( 'fl_inline_editing_enabled', true );
  5079. }
  5080. /**
  5081. * Returns whether the Ace Editor error checking is enabled.
  5082. *
  5083. * @since 2.1
  5084. * @return bool
  5085. */
  5086. static public function is_codechecking_enabled() {
  5087. return apply_filters( 'fl_code_checking_enabled', true );
  5088. }
  5089. /**
  5090. * Returns Ace Editor defaults as an array.
  5091. *
  5092. * @since 2.1
  5093. * @return array
  5094. */
  5095. static public function ace_editor_settings() {
  5096. $defaults = array(
  5097. 'enableBasicAutocompletion' => true,
  5098. 'enableLiveAutocompletion' => true,
  5099. 'enableSnippets' => false,
  5100. 'showLineNumbers' => false,
  5101. 'showFoldWidgets' => false,
  5102. );
  5103. return apply_filters( 'fl_ace_editor_settings', $defaults );
  5104. }
  5105. /**
  5106. * Returns the custom branding string.
  5107. *
  5108. * @since 1.3.1
  5109. * @return string
  5110. */
  5111. static public function get_branding() {
  5112. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  5113. return FLBuilderWhiteLabel::get_branding();
  5114. }
  5115. return __( 'Beaver Builder', 'fl-builder' );
  5116. }
  5117. /**
  5118. * Returns the custom branding icon URL.
  5119. *
  5120. * @since 1.3.7
  5121. * @return string
  5122. */
  5123. static public function get_branding_icon() {
  5124. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  5125. return FLBuilderWhiteLabel::get_branding_icon();
  5126. }
  5127. return FL_BUILDER_URL . 'img/beaver.png';
  5128. }
  5129. /**
  5130. * Returns an array of slugs for all enabled icon sets.
  5131. *
  5132. * @since 1.4.6
  5133. * @return array
  5134. */
  5135. static public function get_enabled_icons() {
  5136. $value = self::get_admin_settings_option( '_fl_builder_enabled_icons', true );
  5137. return ! $value ? array( 'font-awesome-5-regular', 'font-awesome-5-solid', 'font-awesome-5-brands', 'foundation-icons', 'dashicons' ) : $value;
  5138. }
  5139. /**
  5140. * Check if the current user has the specific capabilities
  5141. *
  5142. * @param string $cap The capability to evaluate if it's single or multiple (comma separated) value
  5143. * @return bool
  5144. */
  5145. static public function current_user_has_capability( $cap ) {
  5146. if ( strstr( $cap, ',' ) ) {
  5147. $parts = explode( ',', $cap );
  5148. foreach ( $parts as $part ) {
  5149. if ( current_user_can( trim( $part ) ) ) {
  5150. return true;
  5151. }
  5152. }
  5153. return false;
  5154. } else {
  5155. return current_user_can( $cap );
  5156. }
  5157. }
  5158. /**
  5159. * Returns the default settings for the builder's help button.
  5160. *
  5161. * @since 1.4.9
  5162. * @return array
  5163. */
  5164. static public function get_help_button_defaults() {
  5165. $defaults = array(
  5166. 'enabled' => true,
  5167. 'tour' => true,
  5168. 'video' => true,
  5169. 'video_embed' => '<iframe src="https://player.vimeo.com/video/240550556?autoplay=1" width="420" height="315" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>',
  5170. 'knowledge_base' => true,
  5171. 'knowledge_base_url' => self::get_store_url( 'knowledge-base', array(
  5172. 'utm_medium' => ( true === FL_BUILDER_LITE ? 'bb-lite' : 'bb-pro' ),
  5173. 'utm_source' => 'builder-ui',
  5174. 'utm_campaign' => 'kb-help-button',
  5175. ) ),
  5176. 'forums' => true,
  5177. 'forums_url' => self::get_store_url( 'knowledge-base', array(
  5178. 'utm_medium' => ( true === FL_BUILDER_LITE ? 'bb-lite' : 'bb-pro' ),
  5179. 'utm_source' => 'builder-ui',
  5180. 'utm_campaign' => 'forums-help-button',
  5181. ) ),
  5182. );
  5183. return $defaults;
  5184. }
  5185. /**
  5186. * Returns the settings for the builder's help button.
  5187. *
  5188. * @since 1.4.9
  5189. * @return array
  5190. */
  5191. static public function get_help_button_settings() {
  5192. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  5193. return FLBuilderWhiteLabel::get_help_button_settings();
  5194. }
  5195. return self::get_help_button_defaults();
  5196. }
  5197. /**
  5198. * Get row resize settings
  5199. *
  5200. * @since 2.0
  5201. * @return array
  5202. */
  5203. static public function get_row_resize_settings() {
  5204. $defaults = array(
  5205. 'userCanResizeRows' => true,
  5206. 'minAllowedWidth' => 300,
  5207. 'maxAllowedWidth' => false,
  5208. );
  5209. $settings = apply_filters( 'fl_row_resize_settings', $defaults );
  5210. // Ensure everything is still defined after filter
  5211. $settings = wp_parse_args( $settings, $defaults );
  5212. // Min width can't go lower than 100px
  5213. if ( false == $settings['minAllowedWidth'] || $settings['minAllowedWidth'] < 100 ) {
  5214. $settings['minAllowedWidth'] = 100;
  5215. }
  5216. // Convert string numbers to int
  5217. if ( is_string( $settings['minAllowedWidth'] ) ) {
  5218. $settings['minAllowedWidth'] = intval( $settings['minAllowedWidth'] );
  5219. }
  5220. if ( is_string( $settings['maxAllowedWidth'] ) ) {
  5221. $settings['maxAllowedWidth'] = intval( $settings['maxAllowedWidth'] );
  5222. }
  5223. // Check user capability
  5224. if ( ! FLBuilderUserAccess::current_user_can( 'unrestricted_editing' ) ) {
  5225. $settings['userCanResizeRows'] = false;
  5226. }
  5227. return $settings;
  5228. }
  5229. /**
  5230. * Filter the row settings to remove max width field
  5231. *
  5232. * @since 2.0
  5233. * @return array
  5234. */
  5235. static public function filter_row_settings_for_resize( $form, $id ) {
  5236. if ( 'row' == $id && ! FLBuilderModel::user_can_resize_rows() ) {
  5237. unset( $form['tabs']['style']['sections']['general']['fields']['max_content_width'] );
  5238. }
  5239. return $form;
  5240. }
  5241. /**
  5242. * Check if user has the ability to resize rows
  5243. *
  5244. * @since 2.0
  5245. * @return bool
  5246. */
  5247. static public function user_can_resize_rows() {
  5248. $args = self::get_row_resize_settings();
  5249. return $args['userCanResizeRows'];
  5250. }
  5251. /**
  5252. * Returns an array of account data for all integrated services.
  5253. *
  5254. * @since 1.5.4
  5255. * @return array
  5256. */
  5257. static public function get_services() {
  5258. return get_option( '_fl_builder_services', array() );
  5259. }
  5260. /**
  5261. * Updates the account data for an integrated service.
  5262. *
  5263. * @since 1.5.4
  5264. * @param string $service The service id.
  5265. * @param string $account The account name.
  5266. * @param array $data The account data.
  5267. * @return void
  5268. */
  5269. static public function update_services( $service, $account, $data ) {
  5270. $services = self::get_services();
  5271. $account = sanitize_text_field( $account );
  5272. if ( ! isset( $services[ $service ] ) ) {
  5273. $services[ $service ] = array();
  5274. }
  5275. $services[ $service ][ $account ] = $data;
  5276. update_option( '_fl_builder_services', $services );
  5277. }
  5278. /**
  5279. * Deletes an account for an integrated service.
  5280. *
  5281. * @since 1.5.4
  5282. * @param string $service The service id.
  5283. * @param string $account The account name.
  5284. * @return void
  5285. */
  5286. static public function delete_service_account( $service, $account ) {
  5287. $services = self::get_services();
  5288. if ( isset( $services[ $service ][ $account ] ) ) {
  5289. unset( $services[ $service ][ $account ] );
  5290. }
  5291. if ( 0 === count( $services[ $service ] ) ) {
  5292. unset( $services[ $service ] );
  5293. }
  5294. update_option( '_fl_builder_services', $services );
  5295. }
  5296. /**
  5297. * Returns an option from the database for
  5298. * the admin settings page.
  5299. *
  5300. * @since 1.5.7
  5301. * @param string $key The option key.
  5302. * @param bool $network_override Whether to allow the network admin setting to be overridden on subsites.
  5303. * @return mixed
  5304. */
  5305. static public function get_admin_settings_option( $key, $network_override = true ) {
  5306. if ( is_network_admin() ) {
  5307. // Get the site-wide option if we're in the network admin.
  5308. $value = get_site_option( $key );
  5309. } elseif ( ! $network_override && class_exists( 'FLBuilderMultisiteSettings' ) ) {
  5310. // Get the site-wide option if there's no network override.
  5311. $value = get_site_option( $key );
  5312. } elseif ( class_exists( 'FLBuilderMultisiteSettings' ) ) {
  5313. // Network overrides are allowed. Return the subsite option if it exists.
  5314. $value = get_option( $key );
  5315. $value = false === $value ? get_site_option( $key ) : $value;
  5316. } else {
  5317. // This must be a single site install. Get the single site option.
  5318. $value = get_option( $key );
  5319. }
  5320. return $value;
  5321. }
  5322. /**
  5323. * Updates an option from the admin settings page.
  5324. *
  5325. * @since 1.5.7
  5326. * @param string $key The option key.
  5327. * @param mixed $value The value to update.
  5328. * @param bool $network_override Whether to allow the network admin setting to be overridden on subsites.
  5329. * @return mixed
  5330. */
  5331. static public function update_admin_settings_option( $key, $value, $network_override = true ) {
  5332. if ( is_network_admin() ) {
  5333. // Update the site-wide option since we're in the network admin.
  5334. update_site_option( $key, $value );
  5335. } elseif ( $network_override && FLBuilderAdminSettings::multisite_support() && ! isset( $_POST['fl-override-ms'] ) ) {
  5336. // Delete the option if we don't have a network override.
  5337. delete_option( $key );
  5338. } else {
  5339. // Update the option for single install or subsite.
  5340. update_option( $key, $value );
  5341. }
  5342. }
  5343. /**
  5344. * Returns the plugin basename for Beaver Builder.
  5345. *
  5346. * @since 1.0
  5347. * @return string
  5348. */
  5349. static public function plugin_basename() {
  5350. return plugin_basename( FL_BUILDER_DIR . 'fl-builder.php' );
  5351. }
  5352. /**
  5353. * Deletes almost all database data and asset cache for the builder.
  5354. * We don't delete _fl_builder_enabled, _fl_builder_data and _fl_builder_draft
  5355. * so layouts can be recovered should the plugin be installed again.
  5356. *
  5357. * @since 1.0
  5358. * @return void
  5359. */
  5360. static public function uninstall_database() {
  5361. if ( current_user_can( 'delete_plugins' ) ) {
  5362. // Delete builder options.
  5363. delete_option( '_fl_builder_settings' );
  5364. delete_option( '_fl_builder_enabled_modules' );
  5365. delete_option( '_fl_builder_enabled_templates' );
  5366. delete_option( '_fl_builder_templates_override' );
  5367. delete_option( '_fl_builder_templates_override_rows' );
  5368. delete_option( '_fl_builder_templates_override_columns' );
  5369. delete_option( '_fl_builder_templates_override_modules' );
  5370. delete_option( '_fl_builder_post_types' );
  5371. delete_option( '_fl_builder_enabled_icons' );
  5372. delete_option( '_fl_builder_branding' );
  5373. delete_option( '_fl_builder_branding_icon' );
  5374. delete_option( '_fl_builder_theme_branding' );
  5375. delete_option( '_fl_builder_user_access' );
  5376. delete_option( '_fl_builder_help_button' );
  5377. delete_option( '_fl_builder_color_presets' );
  5378. // Delete builder user meta.
  5379. delete_metadata( 'user', 0, '_fl_builder_launched', 1, true );
  5380. // Delete uploaded files and folders.
  5381. $upload_dir = self::get_upload_dir();
  5382. fl_builder_filesystem()->rmdir( $upload_dir['path'], true );
  5383. // Deactivate and delete the plugin.
  5384. if ( ! function_exists( 'deactivate_plugins' ) ) {
  5385. require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  5386. }
  5387. deactivate_plugins( array( self::plugin_basename() ), false, is_network_admin() );
  5388. delete_plugins( array( self::plugin_basename() ) );
  5389. // Redirect to the plugins page.
  5390. wp_redirect( admin_url( 'plugins.php?deleted=true&plugin_status=all&paged=1&s=' ) );
  5391. exit;
  5392. }
  5393. }
  5394. /**
  5395. * @since 1.6.4.3
  5396. * @deprecated 1.8
  5397. */
  5398. static public function get_theme_branding() {
  5399. _deprecated_function( __METHOD__, '1.8', 'FLBuilderWhiteLabel::get_theme_branding()' );
  5400. if ( class_exists( 'FLBuilderWhiteLabel' ) ) {
  5401. return FLBuilderWhiteLabel::get_theme_branding();
  5402. }
  5403. }
  5404. /**
  5405. * @since 1.0
  5406. * @deprecated 1.8
  5407. */
  5408. static public function save_templates( $templates = array() ) {
  5409. _deprecated_function( __METHOD__, '1.8', 'FLBuilderCoreTemplatesAdmin::save_templates()' );
  5410. if ( class_exists( 'FLBuilderCoreTemplatesAdmin' ) ) {
  5411. FLBuilderCoreTemplatesAdmin::save_templates( $templates );
  5412. }
  5413. }
  5414. /**
  5415. * @since 1.0
  5416. * @deprecated 1.8
  5417. */
  5418. static public function save_template( $settings ) {
  5419. _deprecated_function( __METHOD__, '1.8', 'FLBuilderCoreTemplatesAdmin::save_template()' );
  5420. if ( class_exists( 'FLBuilderCoreTemplatesAdmin' ) ) {
  5421. FLBuilderCoreTemplatesAdmin::save_template( $settings );
  5422. }
  5423. }
  5424. /**
  5425. * @since 1.0
  5426. * @deprecated 1.8
  5427. */
  5428. static public function update_template( $old_index, $settings ) {
  5429. _deprecated_function( __METHOD__, '1.8', 'FLBuilderCoreTemplatesAdmin::update_template()' );
  5430. if ( class_exists( 'FLBuilderCoreTemplatesAdmin' ) ) {
  5431. FLBuilderCoreTemplatesAdmin::update_template( $old_index, $settings );
  5432. }
  5433. }
  5434. /**
  5435. * @since 1.0
  5436. * @deprecated 1.8
  5437. */
  5438. static public function delete_template( $index ) {
  5439. _deprecated_function( __METHOD__, '1.8', 'FLBuilderCoreTemplatesAdmin::delete_template()' );
  5440. if ( class_exists( 'FLBuilderCoreTemplatesAdmin' ) ) {
  5441. FLBuilderCoreTemplatesAdmin::delete_template( $index );
  5442. }
  5443. }
  5444. /**
  5445. * @since 1.3.9
  5446. * @deprecated 1.10
  5447. */
  5448. static public function get_editing_capability() {
  5449. _deprecated_function( __METHOD__, '1.10' );
  5450. return 'edit_posts';
  5451. }
  5452. /**
  5453. * @since 1.7
  5454. * @deprecated 1.10
  5455. */
  5456. static public function current_user_has_editing_capability() {
  5457. _deprecated_function( __METHOD__, '1.10', 'FLBuilderUserAccess::current_user_can()' );
  5458. return FLBuilderUserAccess::current_user_can( 'unrestricted_editing' );
  5459. }
  5460. /**
  5461. * @since 1.6.3
  5462. * @deprecated 1.10
  5463. */
  5464. static public function get_global_templates_editing_capability() {
  5465. _deprecated_function( __METHOD__, '1.10', 'FLBuilderUserAccess::current_user_can' );
  5466. return 'edit_posts';
  5467. }
  5468. /**
  5469. * @since 1.5.7
  5470. * @deprecated 1.10
  5471. */
  5472. static public function user_templates_admin_enabled() {
  5473. _deprecated_function( __METHOD__, '1.10', 'FLBuilderUserAccess::current_user_can( "builder_admin" )' );
  5474. return FLBuilderUserAccess::current_user_can( 'builder_admin' );
  5475. }
  5476. /**
  5477. * @since 1.0
  5478. * @deprecated 2.0
  5479. */
  5480. static public function get_module_category_slug( $name ) {
  5481. _deprecated_function( __METHOD__, '2.0' );
  5482. return sanitize_html_class( $name );
  5483. }
  5484. /**
  5485. * @since 1.8
  5486. * @deprecated 2.0
  5487. */
  5488. static public function get_template_selector_filter_data() {
  5489. _deprecated_function( __METHOD__, '2.0' );
  5490. return array();
  5491. }
  5492. }
  5493. FLBuilderModel::init();