Нет описания

functions.php 79KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007
  1. <?php
  2. /**
  3. * Helper function to trigger displaying a form.
  4. *
  5. * @since 1.0.2
  6. *
  7. * @param mixed $form_id Form ID.
  8. * @param bool $title Form title.
  9. * @param bool $desc Form description.
  10. */
  11. function wpforms_display( $form_id = false, $title = false, $desc = false ) {
  12. wpforms()->frontend->output( $form_id, $title, $desc );
  13. }
  14. /**
  15. * Perform json_decode and unslash.
  16. *
  17. * IMPORTANT: This function decodes the result of wpforms_encode() properly only if
  18. * wp_insert_post() or wp_update_post() were used after the data is encoded.
  19. * Both wp_insert_post() and wp_update_post() remove excessive slashes added by wpforms_encode().
  20. *
  21. * Using wpforms_decode() on wpforms_encode() result directly
  22. * (without using wp_insert_post() or wp_update_post() first) always returns null or false.
  23. *
  24. * @since 1.0.0
  25. *
  26. * @param string $data Data to decode.
  27. *
  28. * @return array|false|null
  29. */
  30. function wpforms_decode( $data ) {
  31. if ( ! $data || empty( $data ) ) {
  32. return false;
  33. }
  34. return wp_unslash( json_decode( $data, true ) );
  35. }
  36. /**
  37. * Perform json_encode and wp_slash.
  38. *
  39. * IMPORTANT: This function adds excessive slashes to prevent data damage
  40. * by wp_insert_post() or wp_update_post() that use wp_unslash() on all the incoming data.
  41. *
  42. * Decoding the result of this function by wpforms_decode() directly
  43. * (without using wp_insert_post() or wp_update_post() first) always returns null or false.
  44. *
  45. * @since 1.3.1.3
  46. *
  47. * @param mixed $data Data to encode.
  48. *
  49. * @return string|false
  50. */
  51. function wpforms_encode( $data = false ) {
  52. if ( empty( $data ) ) {
  53. return false;
  54. }
  55. return wp_slash( wp_json_encode( $data ) );
  56. }
  57. /**
  58. * Check if a string is a valid URL.
  59. *
  60. * @since 1.0.0
  61. * @since 1.5.8 Changed the pattern used to validate the URL.
  62. *
  63. * @param string $url Input URL.
  64. *
  65. * @return bool
  66. */
  67. function wpforms_is_url( $url ) {
  68. // The pattern taken from https://gist.github.com/dperini/729294.
  69. // It is the best choice according to the https://mathiasbynens.be/demo/url-regex.
  70. $pattern = '%^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z0-9\x{00a1}-\x{ffff}][a-z0-9\x{00a1}-\x{ffff}_-]{0,62})?[a-z0-9\x{00a1}-\x{ffff}]\.)+(?:[a-z\x{00a1}-\x{ffff}]{2,}\.?))(?::\d{2,5})?(?:[/?#]\S*)?$%iu';
  71. if ( preg_match( $pattern, trim( $url ) ) ) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. /**
  77. * Get the current URL.
  78. *
  79. * @since 1.0.0
  80. *
  81. * @return string
  82. */
  83. function wpforms_current_url() {
  84. $url = ( ! empty( $_SERVER['HTTPS'] ) ) ? 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] : 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  85. return esc_url_raw( $url );
  86. }
  87. /**
  88. * Convert object to an array.
  89. *
  90. * @since 1.1.7
  91. *
  92. * @param object $object Object to convert.
  93. *
  94. * @return mixed
  95. */
  96. function wpforms_object_to_array( $object ) {
  97. if ( ! is_object( $object ) && ! is_array( $object ) ) {
  98. return $object;
  99. }
  100. if ( is_object( $object ) ) {
  101. $object = get_object_vars( $object );
  102. }
  103. return array_map( 'wpforms_object_to_array', $object );
  104. }
  105. /**
  106. * Get the value of a specific WPForms setting.
  107. *
  108. * @since 1.0.0
  109. *
  110. * @param string $key
  111. * @param mixed $default
  112. * @param string $option
  113. *
  114. * @return mixed
  115. */
  116. function wpforms_setting( $key, $default = false, $option = 'wpforms_settings' ) {
  117. $key = wpforms_sanitize_key( $key );
  118. $options = get_option( $option, false );
  119. $value = is_array( $options ) && ! empty( $options[ $key ] ) ? wp_unslash( $options[ $key ] ) : $default;
  120. return $value;
  121. }
  122. /**
  123. * Update plugin settings option and allow it to be filterable.
  124. *
  125. * @since 1.6.6
  126. *
  127. * @param array $settings A plugin settings array that is saved into options table.
  128. *
  129. * @return bool
  130. */
  131. function wpforms_update_settings( $settings ) {
  132. /**
  133. * Allows plugin settings to be modified before persisting in the database.
  134. *
  135. * @since 1.6.6
  136. *
  137. * @param array $settings An array of plugin settings to modify.
  138. */
  139. $settings = (array) apply_filters( 'wpforms_update_settings', $settings );
  140. $updated = update_option( 'wpforms_settings', $settings );
  141. /**
  142. * Fires after the plugin settings were persisted in the database.
  143. *
  144. * The `$updated` parameter allows to check whether the update was actually successful.
  145. *
  146. * @since 1.6.1
  147. *
  148. * @param array $settings An array of plugin settings.
  149. * @param bool $updated Whether an option was updated or not.
  150. */
  151. do_action( 'wpforms_settings_updated', $settings, $updated );
  152. return $updated;
  153. }
  154. /**
  155. * Sanitize key, primarily used for looking up options.
  156. *
  157. * @since 1.3.9
  158. *
  159. * @param string $key
  160. *
  161. * @return string
  162. */
  163. function wpforms_sanitize_key( $key = '' ) {
  164. return preg_replace( '/[^a-zA-Z0-9_\-\.\:\/]/', '', $key );
  165. }
  166. /**
  167. * Check if form provided contains the specified field type.
  168. *
  169. * @since 1.0.5
  170. *
  171. * @param array|string $type
  172. * @param array|object $form
  173. * @param bool $multiple
  174. *
  175. * @return bool
  176. */
  177. function wpforms_has_field_type( $type, $form, $multiple = false ) {
  178. $form_data = '';
  179. $field = false;
  180. $type = (array) $type;
  181. if ( $multiple ) {
  182. foreach ( $form as $single_form ) {
  183. $field = wpforms_has_field_type( $type, $single_form );
  184. if ( $field ) {
  185. break;
  186. }
  187. }
  188. return $field;
  189. }
  190. if ( is_object( $form ) && ! empty( $form->post_content ) ) {
  191. $form_data = wpforms_decode( $form->post_content );
  192. } elseif ( is_array( $form ) ) {
  193. $form_data = $form;
  194. }
  195. if ( empty( $form_data['fields'] ) ) {
  196. return false;
  197. }
  198. foreach ( $form_data['fields'] as $single_field ) {
  199. if ( in_array( $single_field['type'], $type, true ) ) {
  200. $field = true;
  201. break;
  202. }
  203. }
  204. return $field;
  205. }
  206. /**
  207. * Check if form provided contains a field which a specific setting.
  208. *
  209. * @since 1.4.5
  210. *
  211. * @param string $setting
  212. * @param object|array $form
  213. * @param bool $multiple
  214. *
  215. * @return bool
  216. */
  217. function wpforms_has_field_setting( $setting, $form, $multiple = false ) {
  218. $form_data = '';
  219. $field = false;
  220. if ( $multiple ) {
  221. foreach ( $form as $single_form ) {
  222. $field = wpforms_has_field_setting( $setting, $single_form );
  223. if ( $field ) {
  224. break;
  225. }
  226. }
  227. return $field;
  228. }
  229. if ( is_object( $form ) && ! empty( $form->post_content ) ) {
  230. $form_data = wpforms_decode( $form->post_content );
  231. } elseif ( is_array( $form ) ) {
  232. $form_data = $form;
  233. }
  234. if ( empty( $form_data['fields'] ) ) {
  235. return false;
  236. }
  237. foreach ( $form_data['fields'] as $single_field ) {
  238. if ( ! empty( $single_field[ $setting ] ) ) {
  239. $field = true;
  240. break;
  241. }
  242. }
  243. return $field;
  244. }
  245. /**
  246. * Check if form provided contains page breaks, if so give details.
  247. *
  248. * @since 1.0.0
  249. *
  250. * @param mixed $form
  251. *
  252. * @return mixed
  253. */
  254. function wpforms_has_pagebreak( $form = false ) {
  255. $form_data = '';
  256. $pagebreak = false;
  257. $pages = 1;
  258. if ( is_object( $form ) && ! empty( $form->post_content ) ) {
  259. $form_data = wpforms_decode( $form->post_content );
  260. } elseif ( is_array( $form ) ) {
  261. $form_data = $form;
  262. }
  263. if ( empty( $form_data['fields'] ) ) {
  264. return false;
  265. }
  266. $fields = $form_data['fields'];
  267. foreach ( $fields as $field ) {
  268. if ( 'pagebreak' === $field['type'] && empty( $field['position'] ) ) {
  269. $pagebreak = true;
  270. $pages ++;
  271. }
  272. }
  273. if ( $pagebreak ) {
  274. return $pages;
  275. }
  276. return false;
  277. }
  278. /**
  279. * Try to find and return a top or bottom pagebreak.
  280. *
  281. * @since 1.2.1
  282. *
  283. * @param mixed $form
  284. * @param mixed $type
  285. *
  286. * @return array|bool
  287. */
  288. function wpforms_get_pagebreak( $form = false, $type = false ) {
  289. $form_data = '';
  290. if ( is_object( $form ) && ! empty( $form->post_content ) ) {
  291. $form_data = wpforms_decode( $form->post_content );
  292. } elseif ( is_array( $form ) ) {
  293. $form_data = $form;
  294. }
  295. if ( empty( $form_data['fields'] ) ) {
  296. return false;
  297. }
  298. $fields = $form_data['fields'];
  299. $pages = array();
  300. foreach ( $fields as $field ) {
  301. if ( 'pagebreak' === $field['type'] ) {
  302. $position = ! empty( $field['position'] ) ? $field['position'] : false;
  303. if ( 'pages' === $type && 'bottom' !== $position ) {
  304. $pages[] = $field;
  305. } elseif ( $position === $type ) {
  306. return $field;
  307. }
  308. }
  309. }
  310. if ( ! empty( $pages ) ) {
  311. return $pages;
  312. }
  313. return false;
  314. }
  315. /**
  316. * Return information about pages if the form has multiple pages.
  317. *
  318. * @since 1.3.7
  319. *
  320. * @param mixed $form
  321. *
  322. * @return mixed false or an array
  323. */
  324. function wpforms_get_pagebreak_details( $form = false ) {
  325. $form_data = '';
  326. $details = array();
  327. $pages = 1;
  328. if ( is_object( $form ) && ! empty( $form->post_content ) ) {
  329. $form_data = wpforms_decode( $form->post_content );
  330. } elseif ( is_array( $form ) ) {
  331. $form_data = $form;
  332. }
  333. if ( empty( $form_data['fields'] ) ) {
  334. return false;
  335. }
  336. foreach ( $form_data['fields'] as $field ) {
  337. if ( 'pagebreak' === $field['type'] ) {
  338. if ( empty( $field['position'] ) ) {
  339. $pages ++;
  340. $details['total'] = $pages;
  341. $details['pages'][] = $field;
  342. } elseif ( 'top' === $field['position'] ) {
  343. $details['top'] = $field;
  344. } elseif ( 'bottom' === $field['position'] ) {
  345. $details['bottom'] = $field;
  346. }
  347. }
  348. }
  349. if ( ! empty( $details ) ) {
  350. if ( empty( $details['top'] ) ) {
  351. $details['top'] = array();
  352. }
  353. if ( empty( $details['bottom'] ) ) {
  354. $details['bottom'] = array();
  355. }
  356. $details['current'] = 1;
  357. return $details;
  358. }
  359. return false;
  360. }
  361. /**
  362. * Format, sanitize, and return/echo HTML element ID, classes, attributes,
  363. * and data attributes.
  364. *
  365. * @since 1.3.7
  366. *
  367. * @param string $id
  368. * @param array $class
  369. * @param array $datas
  370. * @param array $atts
  371. * @param bool $echo
  372. *
  373. * @return string
  374. */
  375. function wpforms_html_attributes( $id = '', $class = array(), $datas = array(), $atts = array(), $echo = false ) {
  376. $id = trim( $id );
  377. $parts = array();
  378. if ( ! empty( $id ) ) {
  379. $id = sanitize_html_class( $id );
  380. if ( ! empty( $id ) ) {
  381. $parts[] = 'id="' . $id . '"';
  382. }
  383. }
  384. if ( ! empty( $class ) ) {
  385. $class = wpforms_sanitize_classes( $class, true );
  386. if ( ! empty( $class ) ) {
  387. $parts[] = 'class="' . $class . '"';
  388. }
  389. }
  390. if ( ! empty( $datas ) ) {
  391. foreach ( $datas as $data => $val ) {
  392. $parts[] = 'data-' . sanitize_html_class( $data ) . '="' . esc_attr( $val ) . '"';
  393. }
  394. }
  395. if ( ! empty( $atts ) ) {
  396. foreach ( $atts as $att => $val ) {
  397. if ( '0' === (string) $val || ! empty( $val ) ) {
  398. if ( $att[0] === '[' ) {
  399. // Handle special case for bound attributes in AMP.
  400. $escaped_att = '[' . sanitize_html_class( trim( $att, '[]' ) ) . ']';
  401. } else {
  402. $escaped_att = sanitize_html_class( $att );
  403. }
  404. $parts[] = $escaped_att . '="' . esc_attr( $val ) . '"';
  405. }
  406. }
  407. }
  408. $output = implode( ' ', $parts );
  409. if ( $echo ) {
  410. echo trim( $output ); // phpcs:ignore
  411. } else {
  412. return trim( $output );
  413. }
  414. }
  415. /**
  416. * Sanitize string of CSS classes.
  417. *
  418. * @since 1.2.1
  419. *
  420. * @param array|string $classes
  421. * @param bool $convert True will convert strings to array and vice versa.
  422. *
  423. * @return string|array
  424. */
  425. function wpforms_sanitize_classes( $classes, $convert = false ) {
  426. $array = is_array( $classes );
  427. $css = array();
  428. if ( ! empty( $classes ) ) {
  429. if ( ! $array ) {
  430. $classes = explode( ' ', trim( $classes ) );
  431. }
  432. foreach ( $classes as $class ) {
  433. if ( ! empty( $class ) ) {
  434. $css[] = sanitize_html_class( $class );
  435. }
  436. }
  437. }
  438. if ( $array ) {
  439. return $convert ? implode( ' ', $css ) : $css;
  440. }
  441. return $convert ? $css : implode( ' ', $css );
  442. }
  443. /**
  444. * Convert a file size provided, such as "2M", to bytes.
  445. *
  446. * @link http://stackoverflow.com/a/22500394
  447. *
  448. * @since 1.0.0
  449. *
  450. * @param string $size
  451. *
  452. * @return int
  453. */
  454. function wpforms_size_to_bytes( $size ) {
  455. if ( is_numeric( $size ) ) {
  456. return $size;
  457. }
  458. $suffix = substr( $size, - 1 );
  459. $value = substr( $size, 0, - 1 );
  460. switch ( strtoupper( $suffix ) ) {
  461. case 'P':
  462. $value *= 1024;
  463. case 'T':
  464. $value *= 1024;
  465. case 'G':
  466. $value *= 1024;
  467. case 'M':
  468. $value *= 1024;
  469. case 'K':
  470. $value *= 1024;
  471. break;
  472. }
  473. return $value;
  474. }
  475. /**
  476. * Convert a file size provided, such as "2M", to bytes.
  477. *
  478. * @link http://stackoverflow.com/a/22500394
  479. *
  480. * @since 1.0.0
  481. *
  482. * @param bool $bytes
  483. *
  484. * @return mixed
  485. */
  486. function wpforms_max_upload( $bytes = false ) {
  487. $max = wp_max_upload_size();
  488. if ( $bytes ) {
  489. return $max;
  490. }
  491. return size_format( $max );
  492. }
  493. /**
  494. * Retrieve actual fields from a form.
  495. *
  496. * Non-posting elements such as section divider, page break, and HTML are
  497. * automatically excluded. Optionally a white list can be provided.
  498. *
  499. * @since 1.0.0
  500. *
  501. * @param mixed $form
  502. * @param array $whitelist
  503. *
  504. * @return mixed boolean or array
  505. */
  506. function wpforms_get_form_fields( $form = false, $whitelist = array() ) {
  507. // Accept form (post) object or form ID.
  508. if ( is_object( $form ) ) {
  509. $form = wpforms_decode( $form->post_content );
  510. } elseif ( is_numeric( $form ) ) {
  511. $form = wpforms()->form->get(
  512. $form,
  513. array(
  514. 'content_only' => true,
  515. )
  516. );
  517. }
  518. // White list of field types to allow.
  519. $allowed_form_fields = [
  520. 'address',
  521. 'checkbox',
  522. 'date-time',
  523. 'email',
  524. 'file-upload',
  525. 'gdpr-checkbox',
  526. 'hidden',
  527. 'likert_scale',
  528. 'name',
  529. 'net_promoter_score',
  530. 'number',
  531. 'number-slider',
  532. 'payment-checkbox',
  533. 'payment-multiple',
  534. 'payment-select',
  535. 'payment-single',
  536. 'payment-total',
  537. 'phone',
  538. 'radio',
  539. 'rating',
  540. 'richtext',
  541. 'select',
  542. 'signature',
  543. 'text',
  544. 'textarea',
  545. 'url',
  546. ];
  547. $allowed_form_fields = apply_filters( 'wpforms_get_form_fields_allowed', $allowed_form_fields );
  548. if ( ! is_array( $form ) || empty( $form['fields'] ) ) {
  549. return false;
  550. }
  551. $whitelist = ! empty( $whitelist ) ? $whitelist : $allowed_form_fields;
  552. $form_fields = $form['fields'];
  553. foreach ( $form_fields as $id => $form_field ) {
  554. if ( ! in_array( $form_field['type'], $whitelist, true ) ) {
  555. unset( $form_fields[ $id ] );
  556. }
  557. }
  558. return $form_fields;
  559. }
  560. /**
  561. * Conditional logic form fields supported.
  562. *
  563. * @since 1.5.2
  564. *
  565. * @return array
  566. */
  567. function wpforms_get_conditional_logic_form_fields_supported() {
  568. $fields_supported = [
  569. 'checkbox',
  570. 'email',
  571. 'hidden',
  572. 'net_promoter_score',
  573. 'number',
  574. 'number-slider',
  575. 'payment-checkbox',
  576. 'payment-multiple',
  577. 'payment-select',
  578. 'radio',
  579. 'rating',
  580. 'richtext',
  581. 'select',
  582. 'text',
  583. 'textarea',
  584. 'url',
  585. ];
  586. return apply_filters( 'wpforms_get_conditional_logic_form_fields_supported', $fields_supported );
  587. }
  588. /**
  589. * Get meta key value for a form field.
  590. *
  591. * @since 1.1.9
  592. *
  593. * @param int|string $id Field ID.
  594. * @param string $key Meta key.
  595. * @param mixed $form_data Form data array.
  596. *
  597. * @return string
  598. */
  599. function wpforms_get_form_field_meta( $id = '', $key = '', $form_data = '' ) {
  600. if ( empty( $id ) || empty( $key ) || empty( $form_data ) ) {
  601. return '';
  602. }
  603. if ( ! empty( $form_data['fields'][ $id ]['meta'][ $key ] ) ) {
  604. return $form_data['fields'][ $id ]['meta'][ $key ];
  605. }
  606. return '';
  607. }
  608. /**
  609. * Get meta key value for a form field.
  610. *
  611. * @since 1.3.1
  612. * @since 1.5.0 More strict parameters. Always return an array.
  613. *
  614. * @param string $key Meta key.
  615. * @param string $value Meta value to check against.
  616. * @param array $form_data Form data array.
  617. *
  618. * @return array|bool Empty array, when no data is found.
  619. */
  620. function wpforms_get_form_fields_by_meta( $key, $value, $form_data ) {
  621. $found = array();
  622. if ( empty( $key ) || empty( $value ) || empty( $form_data['fields'] ) ) {
  623. return $found;
  624. }
  625. foreach ( $form_data['fields'] as $id => $field ) {
  626. if ( ! empty( $field['meta'][ $key ] ) && $value === $field['meta'][ $key ] ) {
  627. $found[ $id ] = $field;
  628. }
  629. }
  630. return $found;
  631. }
  632. /**
  633. * US States.
  634. *
  635. * @since 1.0.0
  636. *
  637. * @return array
  638. */
  639. function wpforms_us_states() {
  640. $states = array(
  641. 'AL' => esc_html__( 'Alabama', 'wpforms-lite' ),
  642. 'AK' => esc_html__( 'Alaska', 'wpforms-lite' ),
  643. 'AZ' => esc_html__( 'Arizona', 'wpforms-lite' ),
  644. 'AR' => esc_html__( 'Arkansas', 'wpforms-lite' ),
  645. 'CA' => esc_html__( 'California', 'wpforms-lite' ),
  646. 'CO' => esc_html__( 'Colorado', 'wpforms-lite' ),
  647. 'CT' => esc_html__( 'Connecticut', 'wpforms-lite' ),
  648. 'DE' => esc_html__( 'Delaware', 'wpforms-lite' ),
  649. 'DC' => esc_html__( 'District of Columbia', 'wpforms-lite' ),
  650. 'FL' => esc_html__( 'Florida', 'wpforms-lite' ),
  651. 'GA' => esc_html_x( 'Georgia', 'US State', 'wpforms-lite' ),
  652. 'HI' => esc_html__( 'Hawaii', 'wpforms-lite' ),
  653. 'ID' => esc_html__( 'Idaho', 'wpforms-lite' ),
  654. 'IL' => esc_html__( 'Illinois', 'wpforms-lite' ),
  655. 'IN' => esc_html__( 'Indiana', 'wpforms-lite' ),
  656. 'IA' => esc_html__( 'Iowa', 'wpforms-lite' ),
  657. 'KS' => esc_html__( 'Kansas', 'wpforms-lite' ),
  658. 'KY' => esc_html__( 'Kentucky', 'wpforms-lite' ),
  659. 'LA' => esc_html__( 'Louisiana', 'wpforms-lite' ),
  660. 'ME' => esc_html__( 'Maine', 'wpforms-lite' ),
  661. 'MD' => esc_html__( 'Maryland', 'wpforms-lite' ),
  662. 'MA' => esc_html__( 'Massachusetts', 'wpforms-lite' ),
  663. 'MI' => esc_html__( 'Michigan', 'wpforms-lite' ),
  664. 'MN' => esc_html__( 'Minnesota', 'wpforms-lite' ),
  665. 'MS' => esc_html__( 'Mississippi', 'wpforms-lite' ),
  666. 'MO' => esc_html__( 'Missouri', 'wpforms-lite' ),
  667. 'MT' => esc_html__( 'Montana', 'wpforms-lite' ),
  668. 'NE' => esc_html__( 'Nebraska', 'wpforms-lite' ),
  669. 'NV' => esc_html__( 'Nevada', 'wpforms-lite' ),
  670. 'NH' => esc_html__( 'New Hampshire', 'wpforms-lite' ),
  671. 'NJ' => esc_html__( 'New Jersey', 'wpforms-lite' ),
  672. 'NM' => esc_html__( 'New Mexico', 'wpforms-lite' ),
  673. 'NY' => esc_html__( 'New York', 'wpforms-lite' ),
  674. 'NC' => esc_html__( 'North Carolina', 'wpforms-lite' ),
  675. 'ND' => esc_html__( 'North Dakota', 'wpforms-lite' ),
  676. 'OH' => esc_html__( 'Ohio', 'wpforms-lite' ),
  677. 'OK' => esc_html__( 'Oklahoma', 'wpforms-lite' ),
  678. 'OR' => esc_html__( 'Oregon', 'wpforms-lite' ),
  679. 'PA' => esc_html__( 'Pennsylvania', 'wpforms-lite' ),
  680. 'RI' => esc_html__( 'Rhode Island', 'wpforms-lite' ),
  681. 'SC' => esc_html__( 'South Carolina', 'wpforms-lite' ),
  682. 'SD' => esc_html__( 'South Dakota', 'wpforms-lite' ),
  683. 'TN' => esc_html__( 'Tennessee', 'wpforms-lite' ),
  684. 'TX' => esc_html__( 'Texas', 'wpforms-lite' ),
  685. 'UT' => esc_html__( 'Utah', 'wpforms-lite' ),
  686. 'VT' => esc_html__( 'Vermont', 'wpforms-lite' ),
  687. 'VA' => esc_html__( 'Virginia', 'wpforms-lite' ),
  688. 'WA' => esc_html__( 'Washington', 'wpforms-lite' ),
  689. 'WV' => esc_html__( 'West Virginia', 'wpforms-lite' ),
  690. 'WI' => esc_html__( 'Wisconsin', 'wpforms-lite' ),
  691. 'WY' => esc_html__( 'Wyoming', 'wpforms-lite' ),
  692. );
  693. return apply_filters( 'wpforms_us_states', $states );
  694. }
  695. /**
  696. * Countries.
  697. *
  698. * @since 1.0.0
  699. *
  700. * @return array
  701. */
  702. function wpforms_countries() {
  703. $countries = array(
  704. 'AF' => esc_html__( 'Afghanistan', 'wpforms-lite' ),
  705. 'AX' => esc_html__( 'Åland Islands', 'wpforms-lite' ),
  706. 'AL' => esc_html__( 'Albania', 'wpforms-lite' ),
  707. 'DZ' => esc_html__( 'Algeria', 'wpforms-lite' ),
  708. 'AS' => esc_html__( 'American Samoa', 'wpforms-lite' ),
  709. 'AD' => esc_html__( 'Andorra', 'wpforms-lite' ),
  710. 'AO' => esc_html__( 'Angola', 'wpforms-lite' ),
  711. 'AI' => esc_html__( 'Anguilla', 'wpforms-lite' ),
  712. 'AQ' => esc_html__( 'Antarctica', 'wpforms-lite' ),
  713. 'AG' => esc_html__( 'Antigua and Barbuda', 'wpforms-lite' ),
  714. 'AR' => esc_html__( 'Argentina', 'wpforms-lite' ),
  715. 'AM' => esc_html__( 'Armenia', 'wpforms-lite' ),
  716. 'AW' => esc_html__( 'Aruba', 'wpforms-lite' ),
  717. 'AU' => esc_html__( 'Australia', 'wpforms-lite' ),
  718. 'AT' => esc_html__( 'Austria', 'wpforms-lite' ),
  719. 'AZ' => esc_html__( 'Azerbaijan', 'wpforms-lite' ),
  720. 'BS' => esc_html__( 'Bahamas', 'wpforms-lite' ),
  721. 'BH' => esc_html__( 'Bahrain', 'wpforms-lite' ),
  722. 'BD' => esc_html__( 'Bangladesh', 'wpforms-lite' ),
  723. 'BB' => esc_html__( 'Barbados', 'wpforms-lite' ),
  724. 'BY' => esc_html__( 'Belarus', 'wpforms-lite' ),
  725. 'BE' => esc_html__( 'Belgium', 'wpforms-lite' ),
  726. 'BZ' => esc_html__( 'Belize', 'wpforms-lite' ),
  727. 'BJ' => esc_html__( 'Benin', 'wpforms-lite' ),
  728. 'BM' => esc_html__( 'Bermuda', 'wpforms-lite' ),
  729. 'BT' => esc_html__( 'Bhutan', 'wpforms-lite' ),
  730. 'BO' => esc_html__( 'Bolivia (Plurinational State of)', 'wpforms-lite' ),
  731. 'BQ' => esc_html__( 'Bonaire, Saint Eustatius and Saba', 'wpforms-lite' ),
  732. 'BA' => esc_html__( 'Bosnia and Herzegovina', 'wpforms-lite' ),
  733. 'BW' => esc_html__( 'Botswana', 'wpforms-lite' ),
  734. 'BV' => esc_html__( 'Bouvet Island', 'wpforms-lite' ),
  735. 'BR' => esc_html__( 'Brazil', 'wpforms-lite' ),
  736. 'IO' => esc_html__( 'British Indian Ocean Territory', 'wpforms-lite' ),
  737. 'BN' => esc_html__( 'Brunei Darussalam', 'wpforms-lite' ),
  738. 'BG' => esc_html__( 'Bulgaria', 'wpforms-lite' ),
  739. 'BF' => esc_html__( 'Burkina Faso', 'wpforms-lite' ),
  740. 'BI' => esc_html__( 'Burundi', 'wpforms-lite' ),
  741. 'CV' => esc_html__( 'Cabo Verde', 'wpforms-lite' ),
  742. 'KH' => esc_html__( 'Cambodia', 'wpforms-lite' ),
  743. 'CM' => esc_html__( 'Cameroon', 'wpforms-lite' ),
  744. 'CA' => esc_html__( 'Canada', 'wpforms-lite' ),
  745. 'KY' => esc_html__( 'Cayman Islands', 'wpforms-lite' ),
  746. 'CF' => esc_html__( 'Central African Republic', 'wpforms-lite' ),
  747. 'TD' => esc_html__( 'Chad', 'wpforms-lite' ),
  748. 'CL' => esc_html__( 'Chile', 'wpforms-lite' ),
  749. 'CN' => esc_html__( 'China', 'wpforms-lite' ),
  750. 'CX' => esc_html__( 'Christmas Island', 'wpforms-lite' ),
  751. 'CC' => esc_html__( 'Cocos (Keeling) Islands', 'wpforms-lite' ),
  752. 'CO' => esc_html__( 'Colombia', 'wpforms-lite' ),
  753. 'KM' => esc_html__( 'Comoros', 'wpforms-lite' ),
  754. 'CG' => esc_html__( 'Congo', 'wpforms-lite' ),
  755. 'CD' => esc_html__( 'Congo (Democratic Republic of the)', 'wpforms-lite' ),
  756. 'CK' => esc_html__( 'Cook Islands', 'wpforms-lite' ),
  757. 'CR' => esc_html__( 'Costa Rica', 'wpforms-lite' ),
  758. 'CI' => esc_html__( 'Côte d\'Ivoire', 'wpforms-lite' ),
  759. 'HR' => esc_html__( 'Croatia', 'wpforms-lite' ),
  760. 'CU' => esc_html__( 'Cuba', 'wpforms-lite' ),
  761. 'CW' => esc_html__( 'Curaçao', 'wpforms-lite' ),
  762. 'CY' => esc_html__( 'Cyprus', 'wpforms-lite' ),
  763. 'CZ' => esc_html__( 'Czech Republic', 'wpforms-lite' ),
  764. 'DK' => esc_html__( 'Denmark', 'wpforms-lite' ),
  765. 'DJ' => esc_html__( 'Djibouti', 'wpforms-lite' ),
  766. 'DM' => esc_html__( 'Dominica', 'wpforms-lite' ),
  767. 'DO' => esc_html__( 'Dominican Republic', 'wpforms-lite' ),
  768. 'EC' => esc_html__( 'Ecuador', 'wpforms-lite' ),
  769. 'EG' => esc_html__( 'Egypt', 'wpforms-lite' ),
  770. 'SV' => esc_html__( 'El Salvador', 'wpforms-lite' ),
  771. 'GQ' => esc_html__( 'Equatorial Guinea', 'wpforms-lite' ),
  772. 'ER' => esc_html__( 'Eritrea', 'wpforms-lite' ),
  773. 'EE' => esc_html__( 'Estonia', 'wpforms-lite' ),
  774. 'ET' => esc_html__( 'Ethiopia', 'wpforms-lite' ),
  775. 'FK' => esc_html__( 'Falkland Islands (Malvinas)', 'wpforms-lite' ),
  776. 'FO' => esc_html__( 'Faroe Islands', 'wpforms-lite' ),
  777. 'FJ' => esc_html__( 'Fiji', 'wpforms-lite' ),
  778. 'FI' => esc_html__( 'Finland', 'wpforms-lite' ),
  779. 'FR' => esc_html__( 'France', 'wpforms-lite' ),
  780. 'GF' => esc_html__( 'French Guiana', 'wpforms-lite' ),
  781. 'PF' => esc_html__( 'French Polynesia', 'wpforms-lite' ),
  782. 'TF' => esc_html__( 'French Southern Territories', 'wpforms-lite' ),
  783. 'GA' => esc_html__( 'Gabon', 'wpforms-lite' ),
  784. 'GM' => esc_html__( 'Gambia', 'wpforms-lite' ),
  785. 'GE' => esc_html_x( 'Georgia', 'Country', 'wpforms-lite' ),
  786. 'DE' => esc_html__( 'Germany', 'wpforms-lite' ),
  787. 'GH' => esc_html__( 'Ghana', 'wpforms-lite' ),
  788. 'GI' => esc_html__( 'Gibraltar', 'wpforms-lite' ),
  789. 'GR' => esc_html__( 'Greece', 'wpforms-lite' ),
  790. 'GL' => esc_html__( 'Greenland', 'wpforms-lite' ),
  791. 'GD' => esc_html__( 'Grenada', 'wpforms-lite' ),
  792. 'GP' => esc_html__( 'Guadeloupe', 'wpforms-lite' ),
  793. 'GU' => esc_html__( 'Guam', 'wpforms-lite' ),
  794. 'GT' => esc_html__( 'Guatemala', 'wpforms-lite' ),
  795. 'GG' => esc_html__( 'Guernsey', 'wpforms-lite' ),
  796. 'GN' => esc_html__( 'Guinea', 'wpforms-lite' ),
  797. 'GW' => esc_html__( 'Guinea-Bissau', 'wpforms-lite' ),
  798. 'GY' => esc_html__( 'Guyana', 'wpforms-lite' ),
  799. 'HT' => esc_html__( 'Haiti', 'wpforms-lite' ),
  800. 'HM' => esc_html__( 'Heard Island and McDonald Islands', 'wpforms-lite' ),
  801. 'HN' => esc_html__( 'Honduras', 'wpforms-lite' ),
  802. 'HK' => esc_html__( 'Hong Kong', 'wpforms-lite' ),
  803. 'HU' => esc_html__( 'Hungary', 'wpforms-lite' ),
  804. 'IS' => esc_html__( 'Iceland', 'wpforms-lite' ),
  805. 'IN' => esc_html__( 'India', 'wpforms-lite' ),
  806. 'ID' => esc_html__( 'Indonesia', 'wpforms-lite' ),
  807. 'IR' => esc_html__( 'Iran (Islamic Republic of)', 'wpforms-lite' ),
  808. 'IQ' => esc_html__( 'Iraq', 'wpforms-lite' ),
  809. 'IE' => esc_html__( 'Ireland (Republic of)', 'wpforms-lite' ),
  810. 'IM' => esc_html__( 'Isle of Man', 'wpforms-lite' ),
  811. 'IL' => esc_html__( 'Israel', 'wpforms-lite' ),
  812. 'IT' => esc_html__( 'Italy', 'wpforms-lite' ),
  813. 'JM' => esc_html__( 'Jamaica', 'wpforms-lite' ),
  814. 'JP' => esc_html__( 'Japan', 'wpforms-lite' ),
  815. 'JE' => esc_html__( 'Jersey', 'wpforms-lite' ),
  816. 'JO' => esc_html__( 'Jordan', 'wpforms-lite' ),
  817. 'KZ' => esc_html__( 'Kazakhstan', 'wpforms-lite' ),
  818. 'KE' => esc_html__( 'Kenya', 'wpforms-lite' ),
  819. 'KI' => esc_html__( 'Kiribati', 'wpforms-lite' ),
  820. 'KP' => esc_html__( 'Korea (Democratic People\'s Republic of)', 'wpforms-lite' ),
  821. 'KR' => esc_html__( 'Korea (Republic of)', 'wpforms-lite' ),
  822. 'XK' => esc_html__( 'Kosovo', 'wpforms-lite' ),
  823. 'KW' => esc_html__( 'Kuwait', 'wpforms-lite' ),
  824. 'KG' => esc_html__( 'Kyrgyzstan', 'wpforms-lite' ),
  825. 'LA' => esc_html__( 'Lao People\'s Democratic Republic', 'wpforms-lite' ),
  826. 'LV' => esc_html__( 'Latvia', 'wpforms-lite' ),
  827. 'LB' => esc_html__( 'Lebanon', 'wpforms-lite' ),
  828. 'LS' => esc_html__( 'Lesotho', 'wpforms-lite' ),
  829. 'LR' => esc_html__( 'Liberia', 'wpforms-lite' ),
  830. 'LY' => esc_html__( 'Libya', 'wpforms-lite' ),
  831. 'LI' => esc_html__( 'Liechtenstein', 'wpforms-lite' ),
  832. 'LT' => esc_html__( 'Lithuania', 'wpforms-lite' ),
  833. 'LU' => esc_html__( 'Luxembourg', 'wpforms-lite' ),
  834. 'MO' => esc_html__( 'Macao', 'wpforms-lite' ),
  835. 'MK' => esc_html__( 'North Macedonia (Republic of)', 'wpforms-lite' ),
  836. 'MG' => esc_html__( 'Madagascar', 'wpforms-lite' ),
  837. 'MW' => esc_html__( 'Malawi', 'wpforms-lite' ),
  838. 'MY' => esc_html__( 'Malaysia', 'wpforms-lite' ),
  839. 'MV' => esc_html__( 'Maldives', 'wpforms-lite' ),
  840. 'ML' => esc_html__( 'Mali', 'wpforms-lite' ),
  841. 'MT' => esc_html__( 'Malta', 'wpforms-lite' ),
  842. 'MH' => esc_html__( 'Marshall Islands', 'wpforms-lite' ),
  843. 'MQ' => esc_html__( 'Martinique', 'wpforms-lite' ),
  844. 'MR' => esc_html__( 'Mauritania', 'wpforms-lite' ),
  845. 'MU' => esc_html__( 'Mauritius', 'wpforms-lite' ),
  846. 'YT' => esc_html__( 'Mayotte', 'wpforms-lite' ),
  847. 'MX' => esc_html__( 'Mexico', 'wpforms-lite' ),
  848. 'FM' => esc_html__( 'Micronesia (Federated States of)', 'wpforms-lite' ),
  849. 'MD' => esc_html__( 'Moldova (Republic of)', 'wpforms-lite' ),
  850. 'MC' => esc_html__( 'Monaco', 'wpforms-lite' ),
  851. 'MN' => esc_html__( 'Mongolia', 'wpforms-lite' ),
  852. 'ME' => esc_html__( 'Montenegro', 'wpforms-lite' ),
  853. 'MS' => esc_html__( 'Montserrat', 'wpforms-lite' ),
  854. 'MA' => esc_html__( 'Morocco', 'wpforms-lite' ),
  855. 'MZ' => esc_html__( 'Mozambique', 'wpforms-lite' ),
  856. 'MM' => esc_html__( 'Myanmar', 'wpforms-lite' ),
  857. 'NA' => esc_html__( 'Namibia', 'wpforms-lite' ),
  858. 'NR' => esc_html__( 'Nauru', 'wpforms-lite' ),
  859. 'NP' => esc_html__( 'Nepal', 'wpforms-lite' ),
  860. 'NL' => esc_html__( 'Netherlands', 'wpforms-lite' ),
  861. 'NC' => esc_html__( 'New Caledonia', 'wpforms-lite' ),
  862. 'NZ' => esc_html__( 'New Zealand', 'wpforms-lite' ),
  863. 'NI' => esc_html__( 'Nicaragua', 'wpforms-lite' ),
  864. 'NE' => esc_html__( 'Niger', 'wpforms-lite' ),
  865. 'NG' => esc_html__( 'Nigeria', 'wpforms-lite' ),
  866. 'NU' => esc_html__( 'Niue', 'wpforms-lite' ),
  867. 'NF' => esc_html__( 'Norfolk Island', 'wpforms-lite' ),
  868. 'MP' => esc_html__( 'Northern Mariana Islands', 'wpforms-lite' ),
  869. 'NO' => esc_html__( 'Norway', 'wpforms-lite' ),
  870. 'OM' => esc_html__( 'Oman', 'wpforms-lite' ),
  871. 'PK' => esc_html__( 'Pakistan', 'wpforms-lite' ),
  872. 'PW' => esc_html__( 'Palau', 'wpforms-lite' ),
  873. 'PS' => esc_html__( 'Palestine (State of)', 'wpforms-lite' ),
  874. 'PA' => esc_html__( 'Panama', 'wpforms-lite' ),
  875. 'PG' => esc_html__( 'Papua New Guinea', 'wpforms-lite' ),
  876. 'PY' => esc_html__( 'Paraguay', 'wpforms-lite' ),
  877. 'PE' => esc_html__( 'Peru', 'wpforms-lite' ),
  878. 'PH' => esc_html__( 'Philippines', 'wpforms-lite' ),
  879. 'PN' => esc_html__( 'Pitcairn', 'wpforms-lite' ),
  880. 'PL' => esc_html__( 'Poland', 'wpforms-lite' ),
  881. 'PT' => esc_html__( 'Portugal', 'wpforms-lite' ),
  882. 'PR' => esc_html__( 'Puerto Rico', 'wpforms-lite' ),
  883. 'QA' => esc_html__( 'Qatar', 'wpforms-lite' ),
  884. 'RE' => esc_html__( 'Réunion', 'wpforms-lite' ),
  885. 'RO' => esc_html__( 'Romania', 'wpforms-lite' ),
  886. 'RU' => esc_html__( 'Russian Federation', 'wpforms-lite' ),
  887. 'RW' => esc_html__( 'Rwanda', 'wpforms-lite' ),
  888. 'BL' => esc_html__( 'Saint Barthélemy', 'wpforms-lite' ),
  889. 'SH' => esc_html__( 'Saint Helena, Ascension and Tristan da Cunha', 'wpforms-lite' ),
  890. 'KN' => esc_html__( 'Saint Kitts and Nevis', 'wpforms-lite' ),
  891. 'LC' => esc_html__( 'Saint Lucia', 'wpforms-lite' ),
  892. 'MF' => esc_html__( 'Saint Martin (French part)', 'wpforms-lite' ),
  893. 'PM' => esc_html__( 'Saint Pierre and Miquelon', 'wpforms-lite' ),
  894. 'VC' => esc_html__( 'Saint Vincent and the Grenadines', 'wpforms-lite' ),
  895. 'WS' => esc_html__( 'Samoa', 'wpforms-lite' ),
  896. 'SM' => esc_html__( 'San Marino', 'wpforms-lite' ),
  897. 'ST' => esc_html__( 'Sao Tome and Principe', 'wpforms-lite' ),
  898. 'SA' => esc_html__( 'Saudi Arabia', 'wpforms-lite' ),
  899. 'SN' => esc_html__( 'Senegal', 'wpforms-lite' ),
  900. 'RS' => esc_html__( 'Serbia', 'wpforms-lite' ),
  901. 'SC' => esc_html__( 'Seychelles', 'wpforms-lite' ),
  902. 'SL' => esc_html__( 'Sierra Leone', 'wpforms-lite' ),
  903. 'SG' => esc_html__( 'Singapore', 'wpforms-lite' ),
  904. 'SX' => esc_html__( 'Sint Maarten (Dutch part)', 'wpforms-lite' ),
  905. 'SK' => esc_html__( 'Slovakia', 'wpforms-lite' ),
  906. 'SI' => esc_html__( 'Slovenia', 'wpforms-lite' ),
  907. 'SB' => esc_html__( 'Solomon Islands', 'wpforms-lite' ),
  908. 'SO' => esc_html__( 'Somalia', 'wpforms-lite' ),
  909. 'ZA' => esc_html__( 'South Africa', 'wpforms-lite' ),
  910. 'GS' => esc_html__( 'South Georgia and the South Sandwich Islands', 'wpforms-lite' ),
  911. 'SS' => esc_html__( 'South Sudan', 'wpforms-lite' ),
  912. 'ES' => esc_html__( 'Spain', 'wpforms-lite' ),
  913. 'LK' => esc_html__( 'Sri Lanka', 'wpforms-lite' ),
  914. 'SD' => esc_html__( 'Sudan', 'wpforms-lite' ),
  915. 'SR' => esc_html__( 'Suriname', 'wpforms-lite' ),
  916. 'SJ' => esc_html__( 'Svalbard and Jan Mayen', 'wpforms-lite' ),
  917. 'SZ' => esc_html__( 'Eswatini (Kingdom of)', 'wpforms-lite' ),
  918. 'SE' => esc_html__( 'Sweden', 'wpforms-lite' ),
  919. 'CH' => esc_html__( 'Switzerland', 'wpforms-lite' ),
  920. 'SY' => esc_html__( 'Syrian Arab Republic', 'wpforms-lite' ),
  921. 'TW' => esc_html__( 'Taiwan, Republic of China', 'wpforms-lite' ),
  922. 'TJ' => esc_html__( 'Tajikistan', 'wpforms-lite' ),
  923. 'TZ' => esc_html__( 'Tanzania (United Republic of)', 'wpforms-lite' ),
  924. 'TH' => esc_html__( 'Thailand', 'wpforms-lite' ),
  925. 'TL' => esc_html__( 'Timor-Leste', 'wpforms-lite' ),
  926. 'TG' => esc_html__( 'Togo', 'wpforms-lite' ),
  927. 'TK' => esc_html__( 'Tokelau', 'wpforms-lite' ),
  928. 'TO' => esc_html__( 'Tonga', 'wpforms-lite' ),
  929. 'TT' => esc_html__( 'Trinidad and Tobago', 'wpforms-lite' ),
  930. 'TN' => esc_html__( 'Tunisia', 'wpforms-lite' ),
  931. 'TR' => esc_html__( 'Turkey', 'wpforms-lite' ),
  932. 'TM' => esc_html__( 'Turkmenistan', 'wpforms-lite' ),
  933. 'TC' => esc_html__( 'Turks and Caicos Islands', 'wpforms-lite' ),
  934. 'TV' => esc_html__( 'Tuvalu', 'wpforms-lite' ),
  935. 'UG' => esc_html__( 'Uganda', 'wpforms-lite' ),
  936. 'UA' => esc_html__( 'Ukraine', 'wpforms-lite' ),
  937. 'AE' => esc_html__( 'United Arab Emirates', 'wpforms-lite' ),
  938. 'GB' => esc_html__( 'United Kingdom of Great Britain and Northern Ireland', 'wpforms-lite' ),
  939. 'US' => esc_html__( 'United States of America', 'wpforms-lite' ),
  940. 'UM' => esc_html__( 'United States Minor Outlying Islands', 'wpforms-lite' ),
  941. 'UY' => esc_html__( 'Uruguay', 'wpforms-lite' ),
  942. 'UZ' => esc_html__( 'Uzbekistan', 'wpforms-lite' ),
  943. 'VU' => esc_html__( 'Vanuatu', 'wpforms-lite' ),
  944. 'VA' => esc_html__( 'Vatican City State', 'wpforms-lite' ),
  945. 'VE' => esc_html__( 'Venezuela (Bolivarian Republic of)', 'wpforms-lite' ),
  946. 'VN' => esc_html__( 'Vietnam', 'wpforms-lite' ),
  947. 'VG' => esc_html__( 'Virgin Islands (British)', 'wpforms-lite' ),
  948. 'VI' => esc_html__( 'Virgin Islands (U.S.)', 'wpforms-lite' ),
  949. 'WF' => esc_html__( 'Wallis and Futuna', 'wpforms-lite' ),
  950. 'EH' => esc_html__( 'Western Sahara', 'wpforms-lite' ),
  951. 'YE' => esc_html__( 'Yemen', 'wpforms-lite' ),
  952. 'ZM' => esc_html__( 'Zambia', 'wpforms-lite' ),
  953. 'ZW' => esc_html__( 'Zimbabwe', 'wpforms-lite' ),
  954. );
  955. return apply_filters( 'wpforms_countries', $countries );
  956. }
  957. /**
  958. * Calendar Months.
  959. *
  960. * @since 1.3.7
  961. *
  962. * @return array
  963. */
  964. function wpforms_months() {
  965. $months = array(
  966. 'Jan' => esc_html__( 'January', 'wpforms-lite' ),
  967. 'Feb' => esc_html__( 'February', 'wpforms-lite' ),
  968. 'Mar' => esc_html__( 'March', 'wpforms-lite' ),
  969. 'Apr' => esc_html__( 'April', 'wpforms-lite' ),
  970. 'May' => esc_html__( 'May', 'wpforms-lite' ),
  971. 'Jun' => esc_html__( 'June', 'wpforms-lite' ),
  972. 'Jul' => esc_html__( 'July', 'wpforms-lite' ),
  973. 'Aug' => esc_html__( 'August', 'wpforms-lite' ),
  974. 'Sep' => esc_html__( 'September', 'wpforms-lite' ),
  975. 'Oct' => esc_html__( 'October', 'wpforms-lite' ),
  976. 'Nov' => esc_html__( 'November', 'wpforms-lite' ),
  977. 'Dec' => esc_html__( 'December', 'wpforms-lite' ),
  978. );
  979. return apply_filters( 'wpforms_months', $months );
  980. }
  981. /**
  982. * Calendar Days.
  983. *
  984. * @since 1.3.7
  985. *
  986. * @return array
  987. */
  988. function wpforms_days() {
  989. $days = array(
  990. 'Sun' => esc_html__( 'Sunday', 'wpforms-lite' ),
  991. 'Mon' => esc_html__( 'Monday', 'wpforms-lite' ),
  992. 'Tue' => esc_html__( 'Tuesday', 'wpforms-lite' ),
  993. 'Wed' => esc_html__( 'Wednesday', 'wpforms-lite' ),
  994. 'Thu' => esc_html__( 'Thursday', 'wpforms-lite' ),
  995. 'Fri' => esc_html__( 'Friday', 'wpforms-lite' ),
  996. 'Sat' => esc_html__( 'Saturday', 'wpforms-lite' ),
  997. );
  998. return apply_filters( 'wpforms_days', $days );
  999. }
  1000. /**
  1001. * Lookup user IP.
  1002. *
  1003. * There are many ways to do this, but we prefer the way EDD does it.
  1004. * https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/misc-functions.php#L163
  1005. *
  1006. * @since 1.2.5
  1007. *
  1008. * @return string
  1009. */
  1010. function wpforms_get_ip() {
  1011. $ip = false;
  1012. if ( ! empty( $_SERVER['HTTP_X_REAL_IP'] ) ) {
  1013. $ip = filter_var( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ), FILTER_VALIDATE_IP );
  1014. } elseif ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
  1015. // Check ip from share internet.
  1016. $ip = filter_var( wp_unslash( $_SERVER['HTTP_CLIENT_IP'] ), FILTER_VALIDATE_IP );
  1017. } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  1018. // To check ip is pass from proxy.
  1019. // Can include more than 1 ip, first is the public one.
  1020. // WPCS: sanitization ok.
  1021. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  1022. $ips = explode( ',', wp_unslash( $_SERVER['HTTP_X_FORWARDED_FOR'] ) );
  1023. if ( is_array( $ips ) ) {
  1024. $ip = filter_var( $ips[0], FILTER_VALIDATE_IP );
  1025. }
  1026. } elseif ( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
  1027. $ip = filter_var( wp_unslash( $_SERVER['REMOTE_ADDR'] ), FILTER_VALIDATE_IP );
  1028. }
  1029. $ip = false !== $ip ? $ip : '127.0.0.1';
  1030. $ip_array = explode( ',', $ip );
  1031. $ip_array = array_map( 'trim', $ip_array );
  1032. return sanitize_text_field( apply_filters( 'wpforms_get_ip', $ip_array[0] ) );
  1033. }
  1034. /**
  1035. * Sanitize hex color.
  1036. *
  1037. * @since 1.2.1
  1038. *
  1039. * @param string $color
  1040. *
  1041. * @return string
  1042. */
  1043. function wpforms_sanitize_hex_color( $color ) {
  1044. if ( empty( $color ) ) {
  1045. return '';
  1046. }
  1047. // 3 or 6 hex digits, or the empty string.
  1048. if ( preg_match( '|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ) {
  1049. return $color;
  1050. }
  1051. return '';
  1052. }
  1053. /**
  1054. * Sanitize error message, primarily used during form frontend output.
  1055. *
  1056. * @since 1.3.7
  1057. *
  1058. * @param string $error
  1059. *
  1060. * @return string
  1061. */
  1062. function wpforms_sanitize_error( $error = '' ) {
  1063. $allow = array(
  1064. 'a' => array(
  1065. 'href' => array(),
  1066. 'title' => array(),
  1067. ),
  1068. 'br' => array(),
  1069. 'em' => array(),
  1070. 'strong' => array(),
  1071. 'p' => array(),
  1072. );
  1073. return wp_kses( $error, $allow );
  1074. }
  1075. /**
  1076. * Sanitize a string, that can be a multiline.
  1077. *
  1078. * @uses wpforms_sanitize_text_deeply()
  1079. *
  1080. * @since 1.4.1
  1081. *
  1082. * @param string $string String to deeply sanitize.
  1083. *
  1084. * @return string Sanitized string, or empty string if not a string provided.
  1085. */
  1086. function wpforms_sanitize_textarea_field( $string ) {
  1087. return wpforms_sanitize_text_deeply( $string, true );
  1088. }
  1089. /**
  1090. * Deeply sanitize the string, preserve newlines if needed.
  1091. * Prevent maliciously prepared strings from containing HTML tags.
  1092. *
  1093. * @since 1.6.0
  1094. *
  1095. * @param string $string String to deeply sanitize.
  1096. * @param bool $keep_newlines Whether to keep newlines. Default: false.
  1097. *
  1098. * @return string Sanitized string, or empty string if not a string provided.
  1099. */
  1100. function wpforms_sanitize_text_deeply( $string, $keep_newlines = false ) {
  1101. if ( is_object( $string ) || is_array( $string ) ) {
  1102. return '';
  1103. }
  1104. $string = (string) $string;
  1105. $keep_newlines = (bool) $keep_newlines;
  1106. $new_value = _sanitize_text_fields( $string, $keep_newlines );
  1107. if ( strlen( $new_value ) !== strlen( $string ) ) {
  1108. $new_value = wpforms_sanitize_text_deeply( $new_value, $keep_newlines );
  1109. }
  1110. return $new_value;
  1111. }
  1112. /**
  1113. * Sanitize an HTML string with a set of allowed HTML tags.
  1114. *
  1115. * @since 1.7.0
  1116. *
  1117. * @param string $value String to sanitize.
  1118. *
  1119. * @return string Sanitized string.
  1120. */
  1121. function wpforms_sanitize_richtext_field( $value ) {
  1122. $count = 1;
  1123. $value = convert_invalid_entities( $value );
  1124. // Remove 'script' and 'style' tags recursively.
  1125. while ( $count ) {
  1126. $value = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $value, - 1, $count );
  1127. }
  1128. // Make sure we have allowed tags only.
  1129. $value = wp_kses( $value, wpforms_get_allowed_html_tags_for_richtext_field() );
  1130. // Make sure that all tags are balanced.
  1131. return force_balance_tags( $value );
  1132. }
  1133. /**
  1134. * Escaping for Rich Text field values.
  1135. *
  1136. * @since 1.7.0
  1137. *
  1138. * @param string $value Text to escape.
  1139. *
  1140. * @return string Escaped text.
  1141. */
  1142. function wpforms_esc_richtext_field( $value ) {
  1143. return wpautop( wpforms_sanitize_richtext_field( $value ) );
  1144. }
  1145. /**
  1146. * Retrieve allowed HTML tags for Rich Text field.
  1147. *
  1148. * @since 1.7.0
  1149. *
  1150. * @return array Array of allowed tags.
  1151. */
  1152. function wpforms_get_allowed_html_tags_for_richtext_field() {
  1153. $allowed_tags = array_fill_keys(
  1154. [
  1155. 'img',
  1156. 'h1',
  1157. 'h2',
  1158. 'h3',
  1159. 'h4',
  1160. 'h5',
  1161. 'h6',
  1162. 'p',
  1163. 'a',
  1164. 'ul',
  1165. 'ol',
  1166. 'li',
  1167. 'dl',
  1168. 'dt',
  1169. 'dd',
  1170. 'hr',
  1171. 'br',
  1172. 'code',
  1173. 'pre',
  1174. 'strong',
  1175. 'b',
  1176. 'em',
  1177. 'i',
  1178. 'blockquote',
  1179. 'cite',
  1180. 'q',
  1181. 'del',
  1182. 'span',
  1183. 'small',
  1184. 'table',
  1185. 'th',
  1186. 'tr',
  1187. 'td',
  1188. 'abbr',
  1189. 'address',
  1190. 'sub',
  1191. 'sup',
  1192. 'ins',
  1193. 'figure',
  1194. 'figcaption',
  1195. 'div',
  1196. ],
  1197. array_fill_keys(
  1198. [ 'align', 'class', 'id', 'style', 'src', 'rel', 'href', 'target', 'width', 'height', 'title', 'cite', 'start', 'reversed', 'datetime' ],
  1199. []
  1200. )
  1201. );
  1202. /**
  1203. * Allowed HTML tags for Rich Text field.
  1204. *
  1205. * @since 1.7.0
  1206. *
  1207. * @param array $allowed_tags Allowed HTML tags.
  1208. */
  1209. $tags = (array) apply_filters( 'wpforms_get_allowed_html_tags_for_richtext_field', $allowed_tags );
  1210. // Force unset iframes, script and style no matter when we get back
  1211. // from apply_filters, as they are a huge security risk.
  1212. unset( $tags['iframe'], $tags['script'], $tags['style'] );
  1213. return $tags;
  1214. }
  1215. /**
  1216. * Sanitize an array, that consists of values as strings.
  1217. * After that - merge all array values into multiline string.
  1218. *
  1219. * @since 1.4.1
  1220. *
  1221. * @param array $array
  1222. *
  1223. * @return mixed If not an array is passed (or empty var) - return unmodified var. Otherwise - a merged array into multiline string.
  1224. */
  1225. function wpforms_sanitize_array_combine( $array ) {
  1226. if ( empty( $array ) || ! is_array( $array ) ) {
  1227. return $array;
  1228. }
  1229. return implode( "\n", array_map( 'sanitize_text_field', $array ) );
  1230. }
  1231. /**
  1232. * Detect if we should use a light or dark color based on the color given.
  1233. *
  1234. * @since 1.2.5
  1235. * @link https://docs.woocommerce.com/wc-apidocs/source-function-wc_light_or_dark.html#608-627
  1236. *
  1237. * @param mixed $color
  1238. * @param string $dark (default: '#000000').
  1239. * @param string $light (default: '#FFFFFF').
  1240. *
  1241. * @return string
  1242. */
  1243. function wpforms_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
  1244. $hex = str_replace( '#', '', $color );
  1245. $c_r = hexdec( substr( $hex, 0, 2 ) );
  1246. $c_g = hexdec( substr( $hex, 2, 2 ) );
  1247. $c_b = hexdec( substr( $hex, 4, 2 ) );
  1248. $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000;
  1249. return $brightness > 155 ? $dark : $light;
  1250. }
  1251. /**
  1252. * Build and return either a taxonomy or post type object that is
  1253. * nested to accommodate any hierarchy.
  1254. *
  1255. * @since 1.3.9
  1256. * @since 1.5.0 Return array only. Empty array of no data.
  1257. *
  1258. * @param array $args Object arguments to pass to data retrieval function.
  1259. * @param bool $flat Preserve hierarchy or not. False by default - preserve it.
  1260. *
  1261. * @return array
  1262. */
  1263. function wpforms_get_hierarchical_object( $args = [], $flat = false ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded
  1264. if ( empty( $args['taxonomy'] ) && empty( $args['post_type'] ) ) {
  1265. return [];
  1266. }
  1267. $children = [];
  1268. $parents = [];
  1269. $ref_parent = '';
  1270. $ref_name = '';
  1271. if ( ! empty( $args['post_type'] ) ) {
  1272. $defaults = [
  1273. 'posts_per_page' => - 1,
  1274. 'orderby' => 'title',
  1275. 'order' => 'ASC',
  1276. ];
  1277. $args = wp_parse_args( $args, $defaults );
  1278. $items = get_posts( $args );
  1279. $ref_parent = 'post_parent';
  1280. $ref_id = 'ID';
  1281. $ref_name = 'post_title';
  1282. } elseif ( ! empty( $args['taxonomy'] ) ) {
  1283. $defaults = [
  1284. 'hide_empty' => false,
  1285. 'orderby' => 'name',
  1286. 'order' => 'ASC',
  1287. ];
  1288. $args = wp_parse_args( $args, $defaults );
  1289. $items = get_terms( $args );
  1290. $ref_parent = 'parent';
  1291. $ref_id = 'term_id';
  1292. $ref_name = 'name';
  1293. }
  1294. if ( empty( $items ) || is_wp_error( $items ) ) {
  1295. return [];
  1296. }
  1297. foreach ( $items as $item ) {
  1298. if ( $item->{$ref_parent} ) {
  1299. $children[ $item->{$ref_id} ] = $item;
  1300. $children[ $item->{$ref_id} ]->ID = (int) $item->{$ref_id};
  1301. } else {
  1302. $parents[ $item->{$ref_id} ] = $item;
  1303. $parents[ $item->{$ref_id} ]->ID = (int) $item->{$ref_id};
  1304. }
  1305. }
  1306. $children_count = count( $children );
  1307. while ( $children_count >= 1 ) {
  1308. foreach ( $children as $child ) {
  1309. _wpforms_get_hierarchical_object_search( $child, $parents, $children, $ref_parent );
  1310. // $children is modified by reference, so we need to recount to make sure we met the limits.
  1311. $children_count = count( $children );
  1312. }
  1313. }
  1314. // Sort nested child objects alphabetically using natural order, applies only
  1315. // to ordering by entry title or term name.
  1316. if ( in_array( $args['orderby'], [ 'title', 'name' ], true ) ) {
  1317. _wpforms_sort_hierarchical_object( $parents, $args['orderby'], $args['order'] );
  1318. }
  1319. if ( $flat ) {
  1320. $parents_flat = [];
  1321. _wpforms_get_hierarchical_object_flatten( $parents, $parents_flat, $ref_name );
  1322. return $parents_flat;
  1323. }
  1324. return $parents;
  1325. }
  1326. /**
  1327. * Sort a nested array of objects.
  1328. *
  1329. * @since 1.6.5
  1330. *
  1331. * @param array $objects An array of objects to sort.
  1332. * @param string $orderby The object field to order by.
  1333. * @param string $order Order direction.
  1334. */
  1335. function _wpforms_sort_hierarchical_object( &$objects, $orderby, $order ) {
  1336. // Map WP_Query/WP_Term_Query orderby to WP_Post/WP_Term property.
  1337. $map = [
  1338. 'title' => 'post_title',
  1339. 'name' => 'name',
  1340. ];
  1341. foreach ( $objects as $object ) {
  1342. if ( ! isset( $object->children ) ) {
  1343. continue;
  1344. }
  1345. uasort(
  1346. $object->children,
  1347. static function ( $a, $b ) use ( $map, $orderby, $order ) {
  1348. /**
  1349. * This covers most cases and works for most languages. For some – e.g. European languages
  1350. * that use extended latin charset (Polish, German etc) it will sort the objects into 2
  1351. * groups – base and extended, properly sorted within each group. Making it even more
  1352. * robust requires either additional PHP extensions to be installed on the server
  1353. * or using heavy (and slow) conversions and computations.
  1354. */
  1355. return $order === 'ASC' ?
  1356. strnatcasecmp( $a->{$map[ $orderby ]}, $b->{$map[ $orderby ]} ) :
  1357. strnatcasecmp( $b->{$map[ $orderby ]}, $a->{$map[ $orderby ]} );
  1358. }
  1359. );
  1360. _wpforms_sort_hierarchical_object( $object->children, $orderby, $order );
  1361. }
  1362. }
  1363. /**
  1364. * Search a given array and find the parent of the provided object.
  1365. *
  1366. * @since 1.3.9
  1367. *
  1368. * @param object $child Current child.
  1369. * @param array $parents Parents list.
  1370. * @param array $children Children list.
  1371. * @param string $ref_parent Parent reference.
  1372. */
  1373. function _wpforms_get_hierarchical_object_search( $child, &$parents, &$children, $ref_parent ) {
  1374. foreach ( $parents as $id => $parent ) {
  1375. if ( $parent->ID === $child->{$ref_parent} ) {
  1376. if ( empty( $parent->children ) ) {
  1377. $parents[ $id ]->children = array(
  1378. $child->ID => $child,
  1379. );
  1380. } else {
  1381. $parents[ $id ]->children[ $child->ID ] = $child;
  1382. }
  1383. unset( $children[ $child->ID ] );
  1384. } elseif ( ! empty( $parent->children ) && is_array( $parent->children ) ) {
  1385. _wpforms_get_hierarchical_object_search( $child, $parent->children, $children, $ref_parent );
  1386. }
  1387. }
  1388. }
  1389. /**
  1390. * Flatten a hierarchical object.
  1391. *
  1392. * @since 1.3.9
  1393. *
  1394. * @param array $array Array to process.
  1395. * @param array $output Processed output.
  1396. * @param string $ref_name Name reference.
  1397. * @param int $level Nesting level.
  1398. */
  1399. function _wpforms_get_hierarchical_object_flatten( $array, &$output, $ref_name = 'name', $level = 0 ) {
  1400. foreach ( $array as $key => $item ) {
  1401. $indicator = apply_filters( 'wpforms_hierarchical_object_indicator', '&mdash;' );
  1402. $item->{$ref_name} = str_repeat( $indicator, $level ) . ' ' . $item->{$ref_name};
  1403. $item->depth = $level + 1;
  1404. $output[ $item->ID ] = $item;
  1405. if ( ! empty( $item->children ) ) {
  1406. _wpforms_get_hierarchical_object_flatten( $item->children, $output, $ref_name, $level + 1 );
  1407. unset( $output[ $item->ID ]->children );
  1408. }
  1409. }
  1410. }
  1411. /**
  1412. * Return field choice properties for field configured with dynamic choices.
  1413. *
  1414. * @since 1.4.5
  1415. *
  1416. * @param array $field Field settings.
  1417. * @param int $form_id Form ID.
  1418. * @param array $form_data Form data and settings.
  1419. *
  1420. * @return false|array
  1421. */
  1422. function wpforms_get_field_dynamic_choices( $field, $form_id, $form_data = array() ) {
  1423. if ( empty( $field['dynamic_choices'] ) ) {
  1424. return false;
  1425. }
  1426. $choices = array();
  1427. if ( 'post_type' === $field['dynamic_choices'] ) {
  1428. if ( empty( $field['dynamic_post_type'] ) ) {
  1429. return false;
  1430. }
  1431. $posts = wpforms_get_hierarchical_object(
  1432. apply_filters(
  1433. 'wpforms_dynamic_choice_post_type_args',
  1434. array(
  1435. 'post_type' => $field['dynamic_post_type'],
  1436. 'posts_per_page' => -1,
  1437. 'orderby' => 'title',
  1438. 'order' => 'ASC',
  1439. ),
  1440. $field,
  1441. $form_id
  1442. ),
  1443. true
  1444. );
  1445. foreach ( $posts as $post ) {
  1446. $choices[] = array(
  1447. 'value' => $post->ID,
  1448. 'label' => $post->post_title,
  1449. 'depth' => isset( $post->depth ) ? absint( $post->depth ) : 1,
  1450. );
  1451. }
  1452. } elseif ( 'taxonomy' === $field['dynamic_choices'] ) {
  1453. if ( empty( $field['dynamic_taxonomy'] ) ) {
  1454. return false;
  1455. }
  1456. $terms = wpforms_get_hierarchical_object(
  1457. apply_filters(
  1458. 'wpforms_dynamic_choice_taxonomy_args',
  1459. array(
  1460. 'taxonomy' => $field['dynamic_taxonomy'],
  1461. 'hide_empty' => false,
  1462. ),
  1463. $field,
  1464. $form_data
  1465. ),
  1466. true
  1467. );
  1468. foreach ( $terms as $term ) {
  1469. $choices[] = array(
  1470. 'value' => $term->term_id,
  1471. 'label' => $term->name,
  1472. 'depth' => isset( $term->depth ) ? absint( $term->depth ) : 1,
  1473. );
  1474. }
  1475. }
  1476. return $choices;
  1477. }
  1478. /**
  1479. * Insert an array into another array before/after a certain key.
  1480. *
  1481. * @link https://gist.github.com/scribu/588429
  1482. *
  1483. * @since 1.3.9
  1484. *
  1485. * @param array $array The initial array.
  1486. * @param array $pairs The array to insert.
  1487. * @param string $key The certain key.
  1488. * @param string $position Where to insert the array - before or after the key.
  1489. *
  1490. * @return array
  1491. */
  1492. function wpforms_array_insert( $array, $pairs, $key, $position = 'after' ) {
  1493. $key_pos = array_search( $key, array_keys( $array ), true );
  1494. if ( 'after' === $position ) {
  1495. $key_pos ++;
  1496. }
  1497. if ( false !== $key_pos ) {
  1498. $result = array_slice( $array, 0, $key_pos );
  1499. $result = array_merge( $result, $pairs );
  1500. $result = array_merge( $result, array_slice( $array, $key_pos ) );
  1501. } else {
  1502. $result = array_merge( $array, $pairs );
  1503. }
  1504. return $result;
  1505. }
  1506. /**
  1507. * Recursively remove empty strings from an array.
  1508. *
  1509. * @since 1.3.9.1
  1510. *
  1511. * @param array $data
  1512. *
  1513. * @return array
  1514. */
  1515. function wpforms_array_remove_empty_strings( $data ) {
  1516. foreach ( $data as $key => $value ) {
  1517. if ( is_array( $value ) ) {
  1518. $data[ $key ] = wpforms_array_remove_empty_strings( $data[ $key ] );
  1519. }
  1520. if ( '' === $data[ $key ] ) {
  1521. unset( $data[ $key ] );
  1522. }
  1523. }
  1524. return $data;
  1525. }
  1526. /**
  1527. * Check whether plugin works in a debug mode.
  1528. *
  1529. * @since 1.2.3
  1530. *
  1531. * @return bool
  1532. */
  1533. function wpforms_debug() {
  1534. $debug = false;
  1535. if ( ( defined( 'WPFORMS_DEBUG' ) && true === WPFORMS_DEBUG ) && is_super_admin() ) {
  1536. $debug = true;
  1537. }
  1538. return apply_filters( 'wpforms_debug', $debug );
  1539. }
  1540. /**
  1541. * Helper function to display debug data.
  1542. *
  1543. * @since 1.0.0
  1544. *
  1545. * @param mixed $data What to dump, can be any type.
  1546. * @param bool $echo Whether to print or return. Default is to print.
  1547. *
  1548. * @return string
  1549. */
  1550. function wpforms_debug_data( $data, $echo = true ) {
  1551. /**
  1552. * Allow developers to determine whether the debug data should be displayed.
  1553. * Works only in debug mode (`WPFORMS_DEBUG` constant is `true`).
  1554. *
  1555. * @since 1.6.8
  1556. *
  1557. * @param bool $allow_display True by default.
  1558. */
  1559. $allow_display = apply_filters( 'wpforms_debug_data_allow_display', true );
  1560. if ( wpforms_debug() ) {
  1561. $output = '<div><textarea style="background:#fff;margin: 20px 0;width:100%;height:500px;font-size:12px;font-family: Consolas,Monaco,monospace;direction: ltr;unicode-bidi: embed;line-height: 1.4;padding: 4px 6px 1px;" readonly>';
  1562. $output .= "=================== WPFORMS DEBUG ===================\n\n";
  1563. if ( is_array( $data ) || is_object( $data ) ) {
  1564. $output .= print_r( $data, true ); // phpcs:ignore
  1565. } else {
  1566. $output .= $data;
  1567. }
  1568. $output .= '</textarea></div>';
  1569. if ( $echo && $allow_display ) {
  1570. echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1571. } else {
  1572. return $output;
  1573. }
  1574. }
  1575. }
  1576. /**
  1577. * Log helper.
  1578. *
  1579. * @since 1.0.0
  1580. *
  1581. * @param string $title Title of a log message.
  1582. * @param mixed $message Content of a log message.
  1583. * @param array $args Expected keys: form_id, meta, parent.
  1584. */
  1585. function wpforms_log( $title = '', $message = '', $args = array() ) {
  1586. // Skip if logs disabled in Tools -> Logs.
  1587. if ( ! wpforms_setting( 'logs-enable', false ) ) {
  1588. return;
  1589. }
  1590. // Require log title.
  1591. if ( empty( $title ) ) {
  1592. return;
  1593. }
  1594. /**
  1595. * Compare error levels to determine if we should log.
  1596. * Current supported levels:
  1597. * - Conditional Logic (conditional_logic)
  1598. * - Entries (entry)
  1599. * - Errors (error)
  1600. * - Payments (payment)
  1601. * - Providers (provider)
  1602. * - Security (security)
  1603. * - Spam (spam)
  1604. */
  1605. $types = ! empty( $args['type'] ) ? (array) $args['type'] : [ 'error' ];
  1606. // Skip invalid logs types.
  1607. $log_types = \WPForms\Logger\Log::get_log_types();
  1608. foreach ( $types as $key => $type ) {
  1609. if ( ! isset( $log_types[ $type ] ) ) {
  1610. unset( $types[ $key ] );
  1611. }
  1612. }
  1613. if ( empty( $types ) ) {
  1614. return;
  1615. }
  1616. // Make arrays and objects look nice.
  1617. if ( is_array( $message ) || is_object( $message ) ) {
  1618. $message = '<pre>' . print_r( $message, true ) . '</pre>'; // phpcs:ignore
  1619. }
  1620. // Filter logs types from Tools -> Logs page.
  1621. $logs_types = wpforms_setting( 'logs-types', false );
  1622. if ( $logs_types && empty( array_intersect( $logs_types, $types ) ) ) {
  1623. return;
  1624. }
  1625. // Filter user roles from Tools -> Logs page.
  1626. global $current_user;
  1627. $logs_user_roles = wpforms_setting( 'logs-user-roles', false );
  1628. if ( $logs_user_roles && empty( array_intersect( $logs_user_roles, $current_user->roles ) ) ) {
  1629. return;
  1630. }
  1631. // Filter logs users from Tools -> Logs page.
  1632. $logs_users = wpforms_setting( 'logs-users', false );
  1633. if ( $logs_users && ! in_array( $current_user->ID, $logs_users, true ) ) {
  1634. return;
  1635. }
  1636. $log = wpforms()->get( 'log' );
  1637. if ( ! method_exists( $log, 'add' ) ) {
  1638. return;
  1639. }
  1640. // Create log entry.
  1641. $log->add(
  1642. $title,
  1643. $message,
  1644. $types,
  1645. isset( $args['form_id'] ) ? absint( $args['form_id'] ) : 0,
  1646. isset( $args['parent'] ) ? absint( $args['parent'] ) : 0,
  1647. $current_user->ID
  1648. );
  1649. }
  1650. /**
  1651. * Check whether the current page is in AMP mode or not.
  1652. * We need to check for specific functions, as there is no special AMP header.
  1653. *
  1654. * @since 1.4.1
  1655. *
  1656. * @param bool $check_theme_support Whether theme support should be checked. Defaults to true.
  1657. *
  1658. * @return bool
  1659. */
  1660. function wpforms_is_amp( $check_theme_support = true ) {
  1661. $is_amp = false;
  1662. if (
  1663. // AMP by Automattic; ampforwp.
  1664. ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ||
  1665. // Better AMP.
  1666. ( function_exists( 'is_better_amp' ) && is_better_amp() )
  1667. ) {
  1668. $is_amp = true;
  1669. }
  1670. if ( $is_amp && $check_theme_support ) {
  1671. $is_amp = current_theme_supports( 'amp' );
  1672. }
  1673. return apply_filters( 'wpforms_is_amp', $is_amp );
  1674. }
  1675. /**
  1676. * Decode special characters, both alpha- (<) and numeric-based (').
  1677. * Sanitize recursively, preserve new lines.
  1678. * Handle all the possible mixed variations of < and `&lt;` that can be processed into tags.
  1679. *
  1680. * @since 1.4.1
  1681. * @since 1.6.0 Sanitize recursively, preserve new lines.
  1682. *
  1683. * @param string $string Raw string to decode.
  1684. *
  1685. * @return string
  1686. */
  1687. function wpforms_decode_string( $string ) {
  1688. if ( ! is_string( $string ) ) {
  1689. return $string;
  1690. }
  1691. /*
  1692. * Sanitization should be done first, so tags are stripped and < is converted to &lt; etc.
  1693. * This iteration may do nothing when the string already comes with &lt; and &gt; only.
  1694. */
  1695. $string = wpforms_sanitize_text_deeply( $string, true );
  1696. // Now we need to convert the string without tags: &lt; back to < (same for quotes).
  1697. $string = wp_kses_decode_entities( html_entity_decode( $string, ENT_QUOTES ) );
  1698. // And now we need to sanitize AGAIN, to avoid unwanted tags that appeared after decoding.
  1699. return wpforms_sanitize_text_deeply( $string, true );
  1700. }
  1701. /**
  1702. * Get a suffix for assets, `.min` if debug is disabled.
  1703. *
  1704. * @since 1.4.1
  1705. *
  1706. * @return string
  1707. */
  1708. function wpforms_get_min_suffix() {
  1709. return wpforms_debug() ? '' : '.min';
  1710. }
  1711. /**
  1712. * Get the required label text, with a filter.
  1713. *
  1714. * @since 1.4.4
  1715. *
  1716. * @return string
  1717. */
  1718. function wpforms_get_required_label() {
  1719. return apply_filters( 'wpforms_required_label', esc_html__( 'This field is required.', 'wpforms-lite' ) );
  1720. }
  1721. /**
  1722. * Get the required field label HTML, with a filter.
  1723. *
  1724. * @since 1.4.8
  1725. *
  1726. * @return string
  1727. */
  1728. function wpforms_get_field_required_label() {
  1729. $label_html = apply_filters_deprecated(
  1730. 'wpforms_field_required_label',
  1731. array( ' <span class="wpforms-required-label">*</span>' ),
  1732. '1.4.8 of WPForms plugin',
  1733. 'wpforms_get_field_required_label'
  1734. );
  1735. return apply_filters( 'wpforms_get_field_required_label', $label_html );
  1736. }
  1737. /**
  1738. * Get the default capability to manage everything for WPForms.
  1739. *
  1740. * @since 1.4.4
  1741. *
  1742. * @return string
  1743. */
  1744. function wpforms_get_capability_manage_options() {
  1745. return apply_filters( 'wpforms_manage_cap', 'manage_options' );
  1746. }
  1747. /**
  1748. * Check WPForms permissions for currently logged in user.
  1749. * Both short (e.g. 'view_own_forms') or long (e.g. 'wpforms_view_own_forms') capability name can be used.
  1750. * Only WPForms capabilities get processed.
  1751. *
  1752. * @since 1.4.4
  1753. *
  1754. * @param array|string $caps Capability name(s).
  1755. * @param int $id ID of the specific object to check against if `$capability` is a "meta" cap.
  1756. * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
  1757. * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
  1758. * 'edit_others_posts', etc. Accessed via func_get_args() and passed to WP_User::has_cap(),
  1759. * then map_meta_cap().
  1760. *
  1761. * @return bool
  1762. */
  1763. function wpforms_current_user_can( $caps = [], $id = 0 ) {
  1764. $access = wpforms()->get( 'access' );
  1765. if ( ! method_exists( $access, 'current_user_can' ) ) {
  1766. return false;
  1767. }
  1768. $user_can = $access->current_user_can( $caps , $id );
  1769. return apply_filters( 'wpforms_current_user_can', $user_can, $caps, $id );
  1770. }
  1771. /**
  1772. * Return date and time formatted as expected.
  1773. *
  1774. * @since 1.6.3
  1775. *
  1776. * @param string|int $date Date to format.
  1777. * @param string $format Optional. Format for the date and time.
  1778. * @param bool $gmt_offset Optional. GTM offset.
  1779. *
  1780. * @return string
  1781. */
  1782. function wpforms_datetime_format( $date, $format = '', $gmt_offset = false ) {
  1783. if ( '' === $format ) {
  1784. $format = sprintf( '%s %s', get_option( 'date_format' ), get_option( 'time_format' ) );
  1785. }
  1786. if ( is_string( $date ) ) {
  1787. $date = strtotime( $date );
  1788. }
  1789. if ( $gmt_offset ) {
  1790. $date += (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
  1791. }
  1792. return date_i18n( $format, $date );
  1793. }
  1794. /**
  1795. * Return date formatted as expected.
  1796. *
  1797. * @since 1.6.3
  1798. *
  1799. * @param string|int $date Date to format.
  1800. * @param string $format Optional. Format for the date.
  1801. * @param bool $gmt_offset Optional. GTM offset.
  1802. *
  1803. * @return string
  1804. */
  1805. function wpforms_date_format( $date, $format = '', $gmt_offset = false ) {
  1806. if ( '' === $format ) {
  1807. $format = get_option( 'date_format' );
  1808. }
  1809. return wpforms_datetime_format( $date, $format, $gmt_offset );
  1810. }
  1811. /**
  1812. * Get the certain date of a specified day in a specified format.
  1813. *
  1814. * @since 1.4.4
  1815. * @since 1.6.3 Added $use_gmt_offset parameter.
  1816. *
  1817. * @param string $period Supported values: start, end.
  1818. * @param string $timestamp Default is the current timestamp, if left empty.
  1819. * @param string $format Default is a MySQL format.
  1820. * @param bool $use_gmt_offset Use GTM offset.
  1821. *
  1822. * @return string
  1823. */
  1824. function wpforms_get_day_period_date( $period, $timestamp = '', $format = 'Y-m-d H:i:s', $use_gmt_offset = false ) {
  1825. $date = '';
  1826. if ( empty( $timestamp ) ) {
  1827. $timestamp = time();
  1828. }
  1829. $offset_sec = $use_gmt_offset ? get_option( 'gmt_offset' ) * 3600 : 0;
  1830. switch ( $period ) {
  1831. case 'start_of_day':
  1832. $date = gmdate( $format, strtotime( 'today', $timestamp ) - $offset_sec );
  1833. break;
  1834. case 'end_of_day':
  1835. $date = gmdate( $format, strtotime( 'tomorrow', $timestamp ) - 1 - $offset_sec );
  1836. break;
  1837. }
  1838. return $date;
  1839. }
  1840. /**
  1841. * Get an array of all possible provider addons.
  1842. *
  1843. * @since 1.5.5
  1844. *
  1845. * @return array
  1846. */
  1847. function wpforms_get_providers_all() {
  1848. return [
  1849. [
  1850. 'name' => 'ActiveCampaign',
  1851. 'slug' => 'activecampaign',
  1852. 'img' => 'addon-icon-activecampaign.png',
  1853. 'plugin' => 'wpforms-activecampaign/wpforms-activecampaign.php',
  1854. 'plugin_slug' => 'wpforms-activecampaign',
  1855. 'license' => 'elite',
  1856. ],
  1857. [
  1858. 'name' => 'AWeber',
  1859. 'slug' => 'aweber',
  1860. 'img' => 'addon-icon-aweber.png',
  1861. 'plugin' => 'wpforms-aweber/wpforms-aweber.php',
  1862. 'plugin_slug' => 'wpforms-aweber',
  1863. 'license' => 'pro',
  1864. ],
  1865. [
  1866. 'name' => 'Campaign Monitor',
  1867. 'slug' => 'campaign-monitor',
  1868. 'img' => 'addon-icon-campaign-monitor.png',
  1869. 'plugin' => 'wpforms-campaign-monitor/wpforms-campaign-monitor.php',
  1870. 'plugin_slug' => 'wpforms-campaign-monitor',
  1871. 'license' => 'pro',
  1872. ],
  1873. [
  1874. 'name' => 'Drip',
  1875. 'slug' => 'drip',
  1876. 'img' => 'addon-icon-drip.png',
  1877. 'plugin' => 'wpforms-drip/wpforms-drip.php',
  1878. 'plugin_slug' => 'wpforms-drip',
  1879. 'license' => 'pro',
  1880. ],
  1881. [
  1882. 'name' => 'GetResponse',
  1883. 'slug' => 'getresponse',
  1884. 'img' => 'addon-icon-getresponse.png',
  1885. 'plugin' => 'wpforms-getresponse/wpforms-getresponse.php',
  1886. 'plugin_slug' => 'wpforms-getresponse',
  1887. 'license' => 'pro',
  1888. ],
  1889. [
  1890. 'name' => 'Mailchimp',
  1891. 'slug' => 'mailchimp',
  1892. 'img' => 'addon-icon-mailchimp.png',
  1893. 'plugin' => 'wpforms-mailchimp/wpforms-mailchimp.php',
  1894. 'plugin_slug' => 'wpforms-mailchimp',
  1895. 'license' => 'pro',
  1896. ],
  1897. [
  1898. 'name' => 'Salesforce',
  1899. 'slug' => 'salesforce',
  1900. 'img' => 'addon-icon-salesforce.png',
  1901. 'plugin' => 'wpforms-salesforce/wpforms-salesforce.php',
  1902. 'plugin_slug' => 'wpforms-salesforce',
  1903. 'license' => 'elite',
  1904. ],
  1905. [
  1906. 'name' => 'Sendinblue',
  1907. 'slug' => 'sendinblue',
  1908. 'img' => 'addon-icon-sendinblue.png',
  1909. 'plugin' => 'wpforms-sendinblue/wpforms-sendinblue.php',
  1910. 'plugin_slug' => 'wpforms-sendinblue',
  1911. 'license' => 'pro',
  1912. ],
  1913. [
  1914. 'name' => 'Zapier',
  1915. 'slug' => 'zapier',
  1916. 'img' => 'addon-icon-zapier.png',
  1917. 'plugin' => 'wpforms-zapier/wpforms-zapier.php',
  1918. 'plugin_slug' => 'wpforms-zapier',
  1919. 'license' => 'pro',
  1920. ],
  1921. ];
  1922. }
  1923. /**
  1924. * Get an array of all the active provider addons.
  1925. *
  1926. * @since 1.4.7
  1927. *
  1928. * @return array
  1929. */
  1930. function wpforms_get_providers_available() {
  1931. return (array) apply_filters( 'wpforms_providers_available', array() );
  1932. }
  1933. /**
  1934. * Get options for all providers.
  1935. *
  1936. * @since 1.4.7
  1937. *
  1938. * @param string $provider Define a single provider to get options for this one only.
  1939. *
  1940. * @return array
  1941. */
  1942. function wpforms_get_providers_options( $provider = '' ) {
  1943. $options = get_option( 'wpforms_providers', [] );
  1944. $provider = sanitize_key( $provider );
  1945. $data = $options;
  1946. if ( ! empty( $provider ) && isset( $options[ $provider ] ) ) {
  1947. $data = $options[ $provider ];
  1948. }
  1949. return (array) apply_filters( 'wpforms_get_providers_options', $data, $provider );
  1950. }
  1951. /**
  1952. * Update options for all providers.
  1953. *
  1954. * @since 1.4.7
  1955. *
  1956. * @param string $provider Provider slug.
  1957. * @param array|false $options If false is passed - provider will be removed. Otherwise saved.
  1958. * @param string $key Optional key to identify which connection to update. If empty - generate a new one.
  1959. */
  1960. function wpforms_update_providers_options( $provider, $options, $key = '' ) {
  1961. $providers = wpforms_get_providers_options();
  1962. $id = ! empty( $key ) ? $key : uniqid();
  1963. $provider = sanitize_key( $provider );
  1964. if ( $options ) {
  1965. $providers[ $provider ][ $id ] = (array) $options;
  1966. } else {
  1967. unset( $providers[ $provider ] );
  1968. }
  1969. update_option( 'wpforms_providers', $providers );
  1970. }
  1971. /**
  1972. * Helper function to determine if loading an WPForms related admin page.
  1973. *
  1974. * Here we determine if the current administration page is owned/created by
  1975. * WPForms. This is done in compliance with WordPress best practices for
  1976. * development, so that we only load required WPForms CSS and JS files on pages
  1977. * we create. As a result we do not load our assets admin wide, where they might
  1978. * conflict with other plugins needlessly, also leading to a better, faster user
  1979. * experience for our users.
  1980. *
  1981. * @since 1.3.9
  1982. *
  1983. * @param string $slug Slug identifier for a specific WPForms admin page.
  1984. * @param string $view Slug identifier for a specific WPForms admin page view ("subpage").
  1985. *
  1986. * @return bool
  1987. */
  1988. function wpforms_is_admin_page( $slug = '', $view = '' ) {
  1989. // phpcs:disable WordPress.Security.NonceVerification.Recommended
  1990. // Check against basic requirements.
  1991. if (
  1992. ! is_admin() ||
  1993. empty( $_REQUEST['page'] ) ||
  1994. strpos( $_REQUEST['page'], 'wpforms' ) === false
  1995. ) {
  1996. return false;
  1997. }
  1998. // Check against page slug identifier.
  1999. if (
  2000. ( ! empty( $slug ) && 'wpforms-' . $slug !== $_REQUEST['page'] ) ||
  2001. ( empty( $slug ) && 'wpforms-builder' === $_REQUEST['page'] )
  2002. ) {
  2003. return false;
  2004. }
  2005. // Check against sub-level page view.
  2006. if (
  2007. ! empty( $view ) &&
  2008. ( empty( $_REQUEST['view'] ) || $view !== $_REQUEST['view'] )
  2009. ) {
  2010. return false;
  2011. }
  2012. // phpcs:enable
  2013. return true;
  2014. }
  2015. /**
  2016. * Get the ISO 639-2 Language Code from user/site locale.
  2017. *
  2018. * @see http://www.loc.gov/standards/iso639-2/php/code_list.php
  2019. *
  2020. * @since 1.5.0
  2021. *
  2022. * @return string
  2023. */
  2024. function wpforms_get_language_code() {
  2025. $default_lang = 'en';
  2026. $locale = get_user_locale();
  2027. if ( ! empty( $locale ) ) {
  2028. $lang = explode( '_', $locale );
  2029. if ( ! empty( $lang ) && is_array( $lang ) ) {
  2030. $default_lang = strtolower( $lang[0] );
  2031. }
  2032. }
  2033. return $default_lang;
  2034. }
  2035. /**
  2036. * Determine if we should show the "Show Values" toggle for checkbox, radio, or
  2037. * select fields in form builder. Legacy.
  2038. *
  2039. * @since 1.5.0
  2040. *
  2041. * @return bool
  2042. */
  2043. function wpforms_show_fields_options_setting() {
  2044. return apply_filters( 'wpforms_fields_show_options_setting', false );
  2045. }
  2046. /**
  2047. * Check if a string is empty.
  2048. *
  2049. * @since 1.5.0
  2050. *
  2051. * @param string $string String to test.
  2052. *
  2053. * @return bool
  2054. */
  2055. function wpforms_is_empty_string( $string ) {
  2056. return is_string( $string ) && '' === $string;
  2057. }
  2058. /**
  2059. * Return URL to form preview page.
  2060. *
  2061. * @since 1.5.1
  2062. *
  2063. * @param int $form_id Form ID.
  2064. * @param bool $new_window New window flag.
  2065. *
  2066. * @return string
  2067. */
  2068. function wpforms_get_form_preview_url( $form_id, $new_window = false ) {
  2069. $url = add_query_arg(
  2070. array(
  2071. 'wpforms_form_preview' => absint( $form_id ),
  2072. ),
  2073. home_url()
  2074. );
  2075. if ( $new_window ) {
  2076. $url = add_query_arg(
  2077. array(
  2078. 'new_window' => 1,
  2079. ),
  2080. $url
  2081. );
  2082. }
  2083. return $url;
  2084. }
  2085. /**
  2086. * Include a template - alias to \WPForms\Helpers\Template::get_html.
  2087. * Use 'require' if $args are passed or 'load_template' if not.
  2088. *
  2089. * @since 1.5.6
  2090. *
  2091. * @param string $template_name Template name.
  2092. * @param array $args Arguments.
  2093. * @param bool $extract Extract arguments.
  2094. *
  2095. * @throws \RuntimeException If extract() tries to modify the scope.
  2096. *
  2097. * @return string Compiled HTML.
  2098. */
  2099. function wpforms_render( $template_name, $args = array(), $extract = false ) {
  2100. return \WPForms\Helpers\Templates::get_html( $template_name, $args, $extract );
  2101. }
  2102. /**
  2103. * Chain monad, useful for chaining certain array or string related functions.
  2104. *
  2105. * @since 1.5.6
  2106. *
  2107. * @param mixed $value Any data.
  2108. *
  2109. * @return \WPForms\Helpers\Chain
  2110. */
  2111. function wpforms_chain( $value ) {
  2112. return \WPForms\Helpers\Chain::of( $value );
  2113. }
  2114. /**
  2115. * Get the current installation license type (always lowercase).
  2116. *
  2117. * @since 1.5.6
  2118. *
  2119. * @return string|false
  2120. */
  2121. function wpforms_get_license_type() {
  2122. $type = \wpforms_setting( 'type', '', 'wpforms_license' );
  2123. if ( empty( $type ) || ! \wpforms()->pro ) {
  2124. return false;
  2125. }
  2126. return strtolower( $type );
  2127. }
  2128. /**
  2129. * Get the current installation license key.
  2130. *
  2131. * @since 1.6.2.3
  2132. *
  2133. * @return string
  2134. */
  2135. function wpforms_get_license_key() {
  2136. // Check for license key.
  2137. $key = wpforms_setting( 'key', '', 'wpforms_license' );
  2138. // Allow wp-config constant to pass key.
  2139. if ( empty( $key ) && defined( 'WPFORMS_LICENSE_KEY' ) ) {
  2140. $key = WPFORMS_LICENSE_KEY;
  2141. }
  2142. return $key;
  2143. }
  2144. /**
  2145. * Get when WPForms was first installed.
  2146. *
  2147. * @since 1.6.0
  2148. *
  2149. * @param string $type Specific install type to check for.
  2150. *
  2151. * @return int|false
  2152. */
  2153. function wpforms_get_activated_timestamp( $type = '' ) {
  2154. $activated = get_option( 'wpforms_activated', [] );
  2155. $types = ! empty( $type ) ? [ $type ] : [ 'lite', 'pro' ];
  2156. foreach ( $types as $type ) {
  2157. if ( ! empty( $activated[ $type ] ) ) {
  2158. return absint( $activated[ $type ] );
  2159. }
  2160. }
  2161. return false;
  2162. }
  2163. /**
  2164. * Detect if AJAX frontend form submit is being processed.
  2165. *
  2166. * @since 1.5.8.2
  2167. * @since 1.6.5 Added filterable frontend ajax actions list as a fallback to missing referer cases.
  2168. * @since 1.6.7.1 Removed a requirement for an AJAX action to be a WPForms action if referer is not missing.
  2169. *
  2170. * @return bool
  2171. */
  2172. function wpforms_is_frontend_ajax() {
  2173. if ( ! wp_doing_ajax() ) {
  2174. return false;
  2175. }
  2176. // Additional check to make sure the request targets admin-ajax.php.
  2177. if ( isset( $_SERVER['SCRIPT_FILENAME'] ) && basename( sanitize_text_field( wp_unslash( $_SERVER['SCRIPT_FILENAME'] ) ) ) !== 'admin-ajax.php' ) {
  2178. return false;
  2179. }
  2180. $ref = wp_get_raw_referer();
  2181. if ( ! $ref ) {
  2182. // Try to detect a frontend AJAX call indirectly by comparing the current action
  2183. // with a known frontend actions list in case there's no HTTP referer.
  2184. $frontend_actions = [
  2185. 'wpforms_submit',
  2186. 'wpforms_file_upload_speed_test',
  2187. 'wpforms_upload_chunk_init',
  2188. 'wpforms_upload_chunk',
  2189. 'wpforms_file_chunks_uploaded',
  2190. 'wpforms_remove_file',
  2191. 'wpforms_restricted_email',
  2192. 'wpforms_form_locker_unique_answer',
  2193. 'wpforms_form_abandonment',
  2194. ];
  2195. $action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  2196. // This filter may be running as early as "plugins_loaded" hook. Please mind the hooks order when using it.
  2197. $frontend_actions = (array) apply_filters( 'wpforms_is_frontend_ajax_frontend_actions', $frontend_actions );
  2198. return in_array( $action, $frontend_actions, true );
  2199. }
  2200. $path = wp_parse_url( $ref, PHP_URL_PATH );
  2201. $admin_path = wp_parse_url( admin_url(), PHP_URL_PATH );
  2202. // It's a frontend AJAX call if HTTP referer doesn't contain an admin path.
  2203. return strpos( $path, $admin_path ) === false;
  2204. }
  2205. /**
  2206. * Dequeue enqueues by URI list.
  2207. * Parts of URI (e.g. filename) is also supported.
  2208. *
  2209. * @since 1.6.1
  2210. *
  2211. * @param array|string $uris List of URIs or individual URI to dequeue.
  2212. * @param \WP_Scripts|\WP_Styles $enqueues Enqueues list to dequeue from.
  2213. */
  2214. function wpforms_dequeue_by_uri( $uris, $enqueues ) {
  2215. if ( empty( $enqueues->queue ) ) {
  2216. return;
  2217. }
  2218. foreach ( $enqueues->queue as $handle ) {
  2219. if ( empty( $enqueues->registered[ $handle ]->src ) ) {
  2220. continue;
  2221. }
  2222. $src = wp_make_link_relative( $enqueues->registered[ $handle ]->src );
  2223. // Support full URLs.
  2224. $src = site_url( $src );
  2225. foreach ( (array) $uris as $uri ) {
  2226. if ( strpos( $src, $uri ) !== false ) {
  2227. wp_dequeue_script( $handle );
  2228. break;
  2229. }
  2230. }
  2231. }
  2232. }
  2233. /**
  2234. * Dequeue scripts by URI list.
  2235. * Parts of URI (e.g. filename) is also supported.
  2236. *
  2237. * @since 1.6.1
  2238. *
  2239. * @param array|string $uris List of URIs or individual URI to dequeue.
  2240. */
  2241. function wpforms_dequeue_scripts_by_uri( $uris ) {
  2242. wpforms_dequeue_by_uri( $uris, wp_scripts() );
  2243. }
  2244. /**
  2245. * Dequeue styles by URI list.
  2246. * Parts of URI (e.g. filename) is also supported.
  2247. *
  2248. * @since 1.6.1
  2249. *
  2250. * @param array|string $uris List of URIs or individual URI to dequeue.
  2251. */
  2252. function wpforms_dequeue_styles_by_uri( $uris ) {
  2253. wpforms_dequeue_by_uri( $uris, wp_styles() );
  2254. }
  2255. /**
  2256. * Count words in the string.
  2257. *
  2258. * @since 1.6.2
  2259. *
  2260. * @param string $string String value.
  2261. *
  2262. * @return integer Words count.
  2263. */
  2264. function wpforms_count_words( $string ) {
  2265. if ( ! is_string( $string ) ) {
  2266. return 0;
  2267. }
  2268. $patterns = [
  2269. '/([A-Z]+),([A-Z]+)/i',
  2270. '/([0-9]+),([A-Z]+)/i',
  2271. '/([A-Z]+),([0-9]+)/i',
  2272. ];
  2273. foreach ( $patterns as $pattern ) {
  2274. $string = preg_replace_callback(
  2275. $pattern,
  2276. function( $matches ) {
  2277. return $matches[1] . ', ' . $matches[2];
  2278. },
  2279. $string
  2280. );
  2281. }
  2282. $words = preg_split( '/[\s]+/', $string );
  2283. return is_array( $words ) ? count( $words ) : 0;
  2284. }
  2285. /**
  2286. * Get WPForms upload root path (e.g. /wp-content/uploads/wpforms).
  2287. *
  2288. * As of 1.7.0, you can pass in your own value that matches the output of wp_upload_dir()
  2289. * in order to use this function inside of a filter without infinite looping.
  2290. *
  2291. * @since 1.6.1
  2292. *
  2293. * @return array WPForms upload root path (no trailing slash).
  2294. */
  2295. function wpforms_upload_dir() {
  2296. $upload_dir = wp_upload_dir();
  2297. if ( ! empty( $upload_dir['error'] ) ) {
  2298. return [ 'error' => $upload_dir['error'] ];
  2299. }
  2300. $wpforms_upload_root = trailingslashit( realpath( $upload_dir['basedir'] ) ) . 'wpforms';
  2301. // Add filter to allow redefine store directory.
  2302. $custom_uploads_root = apply_filters( 'wpforms_upload_root', $wpforms_upload_root );
  2303. if ( wp_is_writable( $custom_uploads_root ) ) {
  2304. $wpforms_upload_root = $custom_uploads_root;
  2305. }
  2306. return [
  2307. 'path' => $wpforms_upload_root,
  2308. 'url' => trailingslashit( $upload_dir['baseurl'] ) . 'wpforms',
  2309. 'error' => false,
  2310. ];
  2311. }
  2312. /**
  2313. * Create index.html file in the specified directory if it doesn't exist.
  2314. *
  2315. * @since 1.6.1
  2316. *
  2317. * @param string $path Path to the directory.
  2318. *
  2319. * @return int|false Number of bytes that were written to the file, or false on failure.
  2320. */
  2321. function wpforms_create_index_html_file( $path ) {
  2322. if ( ! is_dir( $path ) || is_link( $path ) ) {
  2323. return false;
  2324. }
  2325. $index_file = wp_normalize_path( trailingslashit( $path ) . 'index.html' );
  2326. // Do nothing if index.html exists in the directory.
  2327. if ( file_exists( $index_file ) ) {
  2328. return false;
  2329. }
  2330. // Create empty index.html.
  2331. return file_put_contents( $index_file, '' ); // phpcs:ignore WordPress.WP.AlternativeFunctions
  2332. }
  2333. /**
  2334. * Create .htaccess file in the WPForms upload directory.
  2335. *
  2336. * @since 1.6.1
  2337. *
  2338. * @return bool True when the .htaccess file exists, false on failure.
  2339. */
  2340. function wpforms_create_upload_dir_htaccess_file() {
  2341. if ( ! apply_filters( 'wpforms_create_upload_dir_htaccess_file', true ) ) {
  2342. return false;
  2343. }
  2344. $upload_dir = wpforms_upload_dir();
  2345. if ( ! empty( $upload_dir['error'] ) ) {
  2346. return false;
  2347. }
  2348. $htaccess_file = wp_normalize_path( trailingslashit( $upload_dir['path'] ) . '.htaccess' );
  2349. $cache_key = 'wpforms_htaccess_file';
  2350. if ( is_file( $htaccess_file ) ) {
  2351. $cached_stat = get_transient( $cache_key );
  2352. $stat = array_intersect_key(
  2353. stat( $htaccess_file ),
  2354. [
  2355. 'size' => 0,
  2356. 'mtime' => 0,
  2357. 'ctime' => 0,
  2358. ]
  2359. );
  2360. if ( $cached_stat === $stat ) {
  2361. return true;
  2362. }
  2363. @unlink( $htaccess_file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
  2364. }
  2365. if ( ! function_exists( 'insert_with_markers' ) ) {
  2366. require_once ABSPATH . 'wp-admin/includes/misc.php';
  2367. }
  2368. $contents = apply_filters(
  2369. 'wpforms_create_upload_dir_htaccess_file_content',
  2370. '# Disable PHP and Python scripts parsing.
  2371. <Files *>
  2372. SetHandler none
  2373. SetHandler default-handler
  2374. RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
  2375. RemoveType .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
  2376. </Files>
  2377. <IfModule mod_php5.c>
  2378. php_flag engine off
  2379. </IfModule>
  2380. <IfModule mod_php7.c>
  2381. php_flag engine off
  2382. </IfModule>
  2383. <IfModule mod_php8.c>
  2384. php_flag engine off
  2385. </IfModule>
  2386. <IfModule headers_module>
  2387. Header set X-Robots-Tag "noindex"
  2388. </IfModule>'
  2389. );
  2390. $created = insert_with_markers( $htaccess_file, 'WPForms', $contents );
  2391. if ( $created ) {
  2392. clearstatcache( true, $htaccess_file );
  2393. $stat = array_intersect_key(
  2394. stat( $htaccess_file ),
  2395. [
  2396. 'size' => 0,
  2397. 'mtime' => 0,
  2398. 'ctime' => 0,
  2399. ]
  2400. );
  2401. set_transient( $cache_key, $stat );
  2402. }
  2403. return $created;
  2404. }
  2405. /**
  2406. * Check if Gutenberg is active.
  2407. *
  2408. * @since 1.6.2
  2409. *
  2410. * @return bool True if Gutenberg is active.
  2411. */
  2412. function wpforms_is_gutenberg_active() {
  2413. $gutenberg = false;
  2414. $block_editor = false;
  2415. if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
  2416. // Gutenberg is installed and activated.
  2417. $gutenberg = true;
  2418. }
  2419. if ( version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ) ) {
  2420. // Block editor.
  2421. $block_editor = true;
  2422. }
  2423. if ( ! $gutenberg && ! $block_editor ) {
  2424. return false;
  2425. }
  2426. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  2427. if ( is_plugin_active( 'disable-gutenberg/disable-gutenberg.php' ) ) {
  2428. return ! disable_gutenberg();
  2429. }
  2430. if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
  2431. return get_option( 'classic-editor-replace' ) === 'block';
  2432. }
  2433. return true;
  2434. }
  2435. /**
  2436. * Determine if the plugin/addon installations are allowed.
  2437. *
  2438. * @since 1.6.2.3
  2439. *
  2440. * @param string $type Should be `plugin` or `addon`.
  2441. *
  2442. * @return bool
  2443. */
  2444. function wpforms_can_install( $type ) {
  2445. if ( ! in_array( $type, [ 'plugin', 'addon' ], true ) ) {
  2446. return false;
  2447. }
  2448. if ( ! current_user_can( 'install_plugins' ) ) {
  2449. return false;
  2450. }
  2451. // Determine whether file modifications are allowed.
  2452. if ( ! wp_is_file_mod_allowed( 'wpforms_can_install' ) ) {
  2453. return false;
  2454. }
  2455. // All plugin checks are done.
  2456. if ( 'plugin' === $type ) {
  2457. return true;
  2458. }
  2459. // Addons require additional license checks.
  2460. $license = get_option( 'wpforms_license', [] );
  2461. // Allow addons installation if license is not expired, enabled and valid.
  2462. return empty( $license['is_expired'] ) && empty( $license['is_disabled'] ) && empty( $license['is_invalid'] );
  2463. }
  2464. /**
  2465. * Retrieve the full config for CAPTCHA.
  2466. *
  2467. * @since 1.6.4
  2468. *
  2469. * @return array
  2470. */
  2471. function wpforms_get_captcha_settings() {
  2472. $allowed_captcha_list = [ 'hcaptcha', 'recaptcha' ];
  2473. $captcha_provider = wpforms_setting( 'captcha-provider', 'recaptcha' );
  2474. if ( ! in_array( $captcha_provider, $allowed_captcha_list, true ) ) {
  2475. return [
  2476. 'provider' => 'none',
  2477. ];
  2478. }
  2479. return [
  2480. 'provider' => $captcha_provider,
  2481. 'site_key' => sanitize_text_field( wpforms_setting( "{$captcha_provider}-site-key", '' ) ),
  2482. 'secret_key' => sanitize_text_field( wpforms_setting( "{$captcha_provider}-secret-key", '' ) ),
  2483. 'recaptcha_type' => wpforms_setting( 'recaptcha-type', 'v2' ),
  2484. ];
  2485. }
  2486. /**
  2487. * Wrapper for set_time_limit to see if it is enabled.
  2488. *
  2489. * @since 1.6.4
  2490. *
  2491. * @param int $limit Time limit.
  2492. */
  2493. function wpforms_set_time_limit( $limit = 0 ) {
  2494. if ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved
  2495. @set_time_limit( $limit ); // @codingStandardsIgnoreLine
  2496. }
  2497. }
  2498. /**
  2499. * Determine if collecting user's IP is allowed by GDPR setting (globally or per form).
  2500. * Majority of our users have GDPR disabled.
  2501. * So we remove this data from the request only when it's not needed:
  2502. * 1) when GDPR is enabled AND globally disabled user details storage;
  2503. * 2) when GDPR is enabled AND IP address processing is disabled on per form basis.
  2504. *
  2505. * @since 1.6.6
  2506. *
  2507. * @param array $form_data Form settings.
  2508. *
  2509. * @return bool
  2510. */
  2511. function wpforms_is_collecting_ip_allowed( $form_data = [] ) {
  2512. if (
  2513. wpforms_setting( 'gdpr', false ) &&
  2514. (
  2515. wpforms_setting( 'gdpr-disable-details', false ) ||
  2516. ( ! empty( $form_data ) && ! empty( $form_data['settings']['disable_ip'] ) )
  2517. )
  2518. ) {
  2519. return false;
  2520. }
  2521. return true;
  2522. }
  2523. /**
  2524. * Retrieve a timezone from the site settings as a `DateTimeZone` object.
  2525. *
  2526. * Timezone can be based on a PHP timezone string or a ±HH:MM offset.
  2527. *
  2528. * @since 1.6.6
  2529. *
  2530. * @return DateTimeZone Timezone object.
  2531. */
  2532. function wpforms_get_timezone() {
  2533. if ( function_exists( 'wp_timezone' ) ) {
  2534. return wp_timezone();
  2535. }
  2536. // Fallback for WordPress version < 5.3.
  2537. $timezone_string = get_option( 'timezone_string' );
  2538. if ( ! $timezone_string ) {
  2539. $offset = (float) get_option( 'gmt_offset' );
  2540. $hours = (int) $offset;
  2541. $minutes = ( $offset - $hours );
  2542. $sign = ( $offset < 0 ) ? '-' : '+';
  2543. $abs_hour = abs( $hours );
  2544. $abs_mins = abs( $minutes * 60 );
  2545. $timezone_string = sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
  2546. }
  2547. return timezone_open( $timezone_string );
  2548. }
  2549. /**
  2550. * Alias for default readonly function.
  2551. *
  2552. * @since 1.6.9
  2553. *
  2554. * @param mixed $readonly One of the values to compare.
  2555. * @param mixed $current The other value to compare if not just true.
  2556. * @param bool $echo Whether to echo or just return the string.
  2557. *
  2558. * @return string HTML attribute or empty string.
  2559. */
  2560. function wpforms_readonly( $readonly, $current = true, $echo = true ) {
  2561. if ( function_exists( 'wp_readonly' ) ) {
  2562. return wp_readonly( $readonly, $current, $echo );
  2563. }
  2564. return __checked_selected_helper( $readonly, $current, $echo, 'readonly' );
  2565. }
  2566. /**
  2567. * Process smart tags.
  2568. *
  2569. * @since 1.7.1
  2570. *
  2571. * @param string $content Content.
  2572. * @param array $form_data Form data.
  2573. * @param array $fields List of fields.
  2574. * @param string $entry_id Entry ID.
  2575. *
  2576. * @return string
  2577. */
  2578. function wpforms_process_smart_tags( $content, $form_data, $fields = [], $entry_id = '' ) {
  2579. // Skip it if variables have invalid format.
  2580. if ( ! is_string( $content ) || ! is_array( $form_data ) || ! is_array( $fields ) ) {
  2581. return $content;
  2582. }
  2583. /**
  2584. * Process smart tags.
  2585. *
  2586. * @since 1.4.0
  2587. *
  2588. * @param string $content Content.
  2589. * @param array $form_data Form data.
  2590. * @param array $fields List of fields.
  2591. * @param string $entry_id Entry ID.
  2592. *
  2593. * @return string
  2594. */
  2595. return apply_filters( 'wpforms_process_smart_tags', $content, $form_data, $fields, $entry_id );
  2596. }