Açıklama Yok

class-base.php 68KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207
  1. <?php
  2. /**
  3. * Base field template.
  4. *
  5. * @since 1.0.0
  6. */
  7. abstract class WPForms_Field {
  8. /**
  9. * Full name of the field type, eg "Paragraph Text".
  10. *
  11. * @since 1.0.0
  12. *
  13. * @var string
  14. */
  15. public $name;
  16. /**
  17. * Type of the field, eg "textarea".
  18. *
  19. * @since 1.0.0
  20. *
  21. * @var string
  22. */
  23. public $type;
  24. /**
  25. * Font Awesome Icon used for the editor button, eg "fa-list".
  26. *
  27. * @since 1.0.0
  28. *
  29. * @var mixed
  30. */
  31. public $icon = false;
  32. /**
  33. * Priority order the field button should show inside the "Add Fields" tab.
  34. *
  35. * @since 1.0.0
  36. *
  37. * @var int
  38. */
  39. public $order = 1;
  40. /**
  41. * Field group the field belongs to.
  42. *
  43. * @since 1.0.0
  44. *
  45. * @var string
  46. */
  47. public $group = 'standard';
  48. /**
  49. * Placeholder to hold default value(s) for some field types.
  50. *
  51. * @since 1.0.0
  52. *
  53. * @var mixed
  54. */
  55. public $defaults;
  56. /**
  57. * Current form ID in the admin builder.
  58. *
  59. * @since 1.1.1
  60. *
  61. * @var int|bool
  62. */
  63. public $form_id;
  64. /**
  65. * Current field ID.
  66. *
  67. * @since 1.5.6
  68. *
  69. * @var int
  70. */
  71. public $field_id;
  72. /**
  73. * Current form data.
  74. *
  75. * @since 1.1.1
  76. *
  77. * @var array
  78. */
  79. public $form_data;
  80. /**
  81. * Current field data.
  82. *
  83. * @since 1.5.6
  84. *
  85. * @var array
  86. */
  87. public $field_data;
  88. /**
  89. * Primary class constructor.
  90. *
  91. * @since 1.0.0
  92. *
  93. * @param bool $init Pass false to allow to shortcut the whole initialization, if needed.
  94. */
  95. public function __construct( $init = true ) {
  96. if ( ! $init ) {
  97. return;
  98. }
  99. // The form ID is to be accessed in the builder.
  100. $this->form_id = isset( $_GET['form_id'] ) ? (int) $_GET['form_id'] : false; // phpcs:ignore WordPress.Security.NonceVerification
  101. // Bootstrap.
  102. $this->init();
  103. // Temporary solution to get an object of the field class.
  104. add_filter(
  105. "wpforms_fields_get_field_object_{$this->type}",
  106. function () {
  107. return $this;
  108. }
  109. );
  110. // Add fields tab.
  111. add_filter( 'wpforms_builder_fields_buttons', array( $this, 'field_button' ), 15 );
  112. // Field options tab.
  113. add_action( "wpforms_builder_fields_options_{$this->type}", array( $this, 'field_options' ), 10 );
  114. // Preview fields.
  115. add_action( "wpforms_builder_fields_previews_{$this->type}", array( $this, 'field_preview' ), 10 );
  116. // AJAX Add new field.
  117. add_action( "wp_ajax_wpforms_new_field_{$this->type}", array( $this, 'field_new' ) );
  118. // Display field input elements on front-end.
  119. add_action( "wpforms_display_field_{$this->type}", array( $this, 'field_display' ), 10, 3 );
  120. // Display field on back-end.
  121. add_filter( "wpforms_pro_admin_entries_edit_is_field_displayable_{$this->type}", '__return_true', 9 );
  122. // Validation on submit.
  123. add_action( "wpforms_process_validate_{$this->type}", array( $this, 'validate' ), 10, 3 );
  124. // Format.
  125. add_action( "wpforms_process_format_{$this->type}", array( $this, 'format' ), 10, 3 );
  126. // Prefill.
  127. add_filter( 'wpforms_field_properties', array( $this, 'field_prefill_value_property' ), 10, 3 );
  128. // Change the choice's value while saving entries.
  129. add_filter( 'wpforms_process_before_form_data', [ $this, 'field_fill_empty_choices' ] );
  130. // Change field name for ajax error.
  131. add_filter( 'wpforms_process_ajax_error_field_name', [ $this, 'ajax_error_field_name' ], 10, 4 );
  132. // Add HTML line breaks before all newlines in Entry Preview.
  133. add_filter( "wpforms_pro_fields_entry_preview_get_field_value_{$this->type}_field_after", 'nl2br', 100 );
  134. }
  135. /**
  136. * All systems go. Used by subclasses. Required.
  137. *
  138. * @since 1.0.0
  139. * @since 1.5.0 Converted to abstract method, as it's required for all fields.
  140. */
  141. abstract public function init();
  142. /**
  143. * Prefill field value with either fallback or dynamic data.
  144. * This needs to be public (although internal) to be used in WordPress hooks.
  145. *
  146. * @since 1.5.0
  147. *
  148. * @param array $properties Field properties.
  149. * @param array $field Current field specific data.
  150. * @param array $form_data Prepared form data/settings.
  151. *
  152. * @return array Modified field properties.
  153. */
  154. public function field_prefill_value_property( $properties, $field, $form_data ) {
  155. // Process only for current field.
  156. if ( $this->type !== $field['type'] ) {
  157. return $properties;
  158. }
  159. // Set the form data, so we can reuse it later, even on front-end.
  160. $this->form_data = $form_data;
  161. // Dynamic data.
  162. if ( ! empty( $this->form_data['settings']['dynamic_population'] ) ) {
  163. $properties = $this->field_prefill_value_property_dynamic( $properties, $field );
  164. }
  165. // Fallback data, rewrites dynamic because user-submitted data is more important.
  166. $properties = $this->field_prefill_value_property_fallback( $properties, $field );
  167. return $properties;
  168. }
  169. /**
  170. * As we are processing user submitted data - ignore all admin-defined defaults.
  171. * Preprocess choices-related fields only.
  172. *
  173. * @since 1.5.0
  174. *
  175. * @param array $field Field data and settings.
  176. * @param array $properties Properties we are modifying.
  177. */
  178. public function field_prefill_remove_choices_defaults( $field, &$properties ) {
  179. // Skip this step on admin page.
  180. if ( is_admin() && ! wpforms_is_admin_page( 'entries', 'edit' ) ) {
  181. return;
  182. }
  183. if (
  184. ! empty( $field['dynamic_choices'] ) ||
  185. ! empty( $field['choices'] )
  186. ) {
  187. array_walk_recursive(
  188. $properties['inputs'],
  189. function ( &$value, $key ) {
  190. if ( 'default' === $key ) {
  191. $value = false;
  192. }
  193. if ( 'wpforms-selected' === $value ) {
  194. $value = '';
  195. }
  196. }
  197. );
  198. }
  199. }
  200. /**
  201. * Whether current field can be populated dynamically.
  202. *
  203. * @since 1.5.0
  204. *
  205. * @param array $properties Field properties.
  206. * @param array $field Current field specific data.
  207. *
  208. * @return bool
  209. */
  210. public function is_dynamic_population_allowed( $properties, $field ) {
  211. $allowed = true;
  212. // Allow population on front-end only.
  213. if ( is_admin() ) {
  214. $allowed = false;
  215. }
  216. // For dynamic population we require $_GET.
  217. if ( empty( $_GET ) ) { // phpcs:ignore
  218. $allowed = false;
  219. }
  220. return apply_filters( 'wpforms_field_is_dynamic_population_allowed', $allowed, $properties, $field );
  221. }
  222. /**
  223. * Prefill the field value with a dynamic value, that we get from $_GET.
  224. * The pattern is: wpf4_12_primary, where:
  225. * 4 - form_id,
  226. * 12 - field_id,
  227. * first - input key.
  228. * As 'primary' is our default input key, "wpf4_12_primary" and "wpf4_12" are the same.
  229. *
  230. * @since 1.5.0
  231. *
  232. * @param array $properties Field properties.
  233. * @param array $field Current field specific data.
  234. *
  235. * @return array Modified field properties.
  236. */
  237. protected function field_prefill_value_property_dynamic( $properties, $field ) {
  238. if ( ! $this->is_dynamic_population_allowed( $properties, $field ) ) {
  239. return $properties;
  240. }
  241. // Iterate over each GET key, parse, and scrap data from there.
  242. foreach ( $_GET as $key => $raw_value ) { // phpcs:ignore
  243. preg_match( '/wpf(\d+)_(\d+)(.*)/i', $key, $matches );
  244. if ( empty( $matches ) || ! is_array( $matches ) ) {
  245. continue;
  246. }
  247. // Required.
  248. $form_id = absint( $matches[1] );
  249. $field_id = absint( $matches[2] );
  250. $input = 'primary';
  251. // Optional.
  252. if ( ! empty( $matches[3] ) ) {
  253. $input = sanitize_key( trim( $matches[3], '_' ) );
  254. }
  255. // Both form and field IDs should be the same as current form/field.
  256. if (
  257. (int) $this->form_data['id'] !== $form_id ||
  258. (int) $field['id'] !== $field_id
  259. ) {
  260. // Go to the next GET param.
  261. continue;
  262. }
  263. if ( ! empty( $raw_value ) ) {
  264. $this->field_prefill_remove_choices_defaults( $field, $properties );
  265. }
  266. /*
  267. * Some fields (like checkboxes) support multiple selection.
  268. * We do not support nested values, so omit them.
  269. * Example: ?wpf771_19_wpforms[fields][19][address1]=test
  270. * In this case:
  271. * $input = wpforms
  272. * $raw_value = [fields=>[]]
  273. * $single_value = [19=>[]]
  274. * There is no reliable way to clean those things out.
  275. * So we will ignore the value altogether if it's an array.
  276. * We support only single value numeric arrays, like these:
  277. * ?wpf771_19[]=test1&wpf771_19[]=test2
  278. * ?wpf771_19_value[]=test1&wpf771_19_value[]=test2
  279. * ?wpf771_41_r3_c2[]=1&wpf771_41_r1_c4[]=1
  280. */
  281. if ( is_array( $raw_value ) ) {
  282. foreach ( $raw_value as $single_value ) {
  283. $properties = $this->get_field_populated_single_property_value( $single_value, $input, $properties, $field );
  284. }
  285. } else {
  286. $properties = $this->get_field_populated_single_property_value( $raw_value, $input, $properties, $field );
  287. }
  288. }
  289. return $properties;
  290. }
  291. /**
  292. * Public version of get_field_populated_single_property_value() to use by external classes.
  293. *
  294. * @since 1.6.0.1
  295. *
  296. * @param string $raw_value Value from a GET param, always a string.
  297. * @param string $input Represent a subfield inside the field. May be empty.
  298. * @param array $properties Field properties.
  299. * @param array $field Current field specific data.
  300. *
  301. * @return array Modified field properties.
  302. */
  303. public function get_field_populated_single_property_value_public( $raw_value, $input, $properties, $field ) {
  304. return $this->get_field_populated_single_property_value( $raw_value, $input, $properties, $field );
  305. }
  306. /**
  307. * Get the value, that is used to prefill via dynamic or fallback population.
  308. * Based on field data and current properties.
  309. *
  310. * @since 1.5.0
  311. *
  312. * @param string $raw_value Value from a GET param, always a string.
  313. * @param string $input Represent a subfield inside the field. May be empty.
  314. * @param array $properties Field properties.
  315. * @param array $field Current field specific data.
  316. *
  317. * @return array Modified field properties.
  318. */
  319. protected function get_field_populated_single_property_value( $raw_value, $input, $properties, $field ) {
  320. if ( ! is_string( $raw_value ) ) {
  321. return $properties;
  322. }
  323. $get_value = stripslashes( sanitize_text_field( $raw_value ) );
  324. // For fields that have dynamic choices we need to add extra logic.
  325. if ( ! empty( $field['dynamic_choices'] ) ) {
  326. $properties = $this->get_field_populated_single_property_value_dynamic_choices( $get_value, $properties );
  327. } elseif ( ! empty( $field['choices'] ) && is_array( $field['choices'] ) ) {
  328. $properties = $this->get_field_populated_single_property_value_normal_choices( $get_value, $properties, $field );
  329. } else {
  330. /*
  331. * For other types of fields we need to check that
  332. * the key is registered for the defined field in inputs array.
  333. */
  334. if (
  335. ! empty( $input ) &&
  336. isset( $properties['inputs'][ $input ] )
  337. ) {
  338. $properties['inputs'][ $input ]['attr']['value'] = $get_value;
  339. }
  340. }
  341. return $properties;
  342. }
  343. /**
  344. * Get the value, that is used to prefill via dynamic or fallback population.
  345. * Based on field data and current properties.
  346. * Dynamic choices section.
  347. *
  348. * @since 1.6.0
  349. *
  350. * @param string $get_value Value from a GET param, always a string, sanitized, stripped slashes.
  351. * @param array $properties Field properties.
  352. *
  353. * @return array Modified field properties.
  354. */
  355. protected function get_field_populated_single_property_value_dynamic_choices( $get_value, $properties ) {
  356. $default_key = null;
  357. foreach ( $properties['inputs'] as $input_key => $input_arr ) {
  358. // Dynamic choices support only integers in its values.
  359. if ( absint( $get_value ) === $input_arr['attr']['value'] ) {
  360. $default_key = $input_key;
  361. // Stop iterating over choices.
  362. break;
  363. }
  364. }
  365. // Redefine default choice only if dynamic value has changed anything.
  366. if ( null !== $default_key ) {
  367. foreach ( $properties['inputs'] as $input_key => $choice_arr ) {
  368. if ( $input_key === $default_key ) {
  369. $properties['inputs'][ $input_key ]['default'] = true;
  370. $properties['inputs'][ $input_key ]['container']['class'][] = 'wpforms-selected';
  371. // Stop iterating over choices.
  372. break;
  373. }
  374. }
  375. }
  376. return $properties;
  377. }
  378. /**
  379. * Fill choices without labels.
  380. *
  381. * @since 1.6.2
  382. *
  383. * @param array $form_data Form data.
  384. *
  385. * @return array
  386. */
  387. public function field_fill_empty_choices( $form_data ) {
  388. if ( empty( $form_data['fields'] ) ) {
  389. return $form_data;
  390. }
  391. // Set value for choices with the image only. Conditional logic doesn't work without value.
  392. foreach ( $form_data['fields'] as $field_key => $field ) {
  393. // Payment fields have their labels set up upfront.
  394. if ( empty( $field['choices'] ) || ! in_array( $field['type'], [ 'radio', 'checkbox' ], true ) ) {
  395. continue;
  396. }
  397. foreach ( $field['choices'] as $choice_id => $choice ) {
  398. if ( ( isset( $choice['value'] ) && '' !== trim( $choice['value'] ) ) || empty( $choice['image'] ) ) {
  399. continue;
  400. }
  401. /* translators: %d - choice number. */
  402. $form_data['fields'][ $field_key ]['choices'][ $choice_id ]['value'] = sprintf( esc_html__( 'Choice %d', 'wpforms-lite' ), (int) $choice_id );
  403. }
  404. }
  405. return $form_data;
  406. }
  407. /**
  408. * Get the value, that is used to prefill via dynamic or fallback population.
  409. * Based on field data and current properties.
  410. * Normal choices section.
  411. *
  412. * @since 1.6.0
  413. *
  414. * @param string $get_value Value from a GET param, always a string, sanitized.
  415. * @param array $properties Field properties.
  416. * @param array $field Current field specific data.
  417. *
  418. * @return array Modified field properties.
  419. */
  420. protected function get_field_populated_single_property_value_normal_choices( $get_value, $properties, $field ) {
  421. $default_key = null;
  422. // For fields that have normal choices we need to add extra logic.
  423. foreach ( $field['choices'] as $choice_key => $choice_arr ) {
  424. $choice_value_key = isset( $field['show_values'] ) ? 'value' : 'label';
  425. if (
  426. (
  427. isset( $choice_arr[ $choice_value_key ] ) &&
  428. strtoupper( sanitize_text_field( $choice_arr[ $choice_value_key ] ) ) === strtoupper( $get_value )
  429. ) ||
  430. (
  431. empty( $choice_arr[ $choice_value_key ] ) &&
  432. /* translators: %d - choice number. */
  433. $get_value === sprintf( esc_html__( 'Choice %d', 'wpforms-lite' ), (int) $choice_key )
  434. )
  435. ) {
  436. $default_key = $choice_key;
  437. // Stop iterating over choices.
  438. break;
  439. }
  440. }
  441. // Redefine default choice only if population value has changed anything.
  442. if ( null !== $default_key ) {
  443. foreach ( $field['choices'] as $choice_key => $choice_arr ) {
  444. if ( $choice_key === $default_key ) {
  445. $properties['inputs'][ $choice_key ]['default'] = true;
  446. $properties['inputs'][ $choice_key ]['container']['class'][] = 'wpforms-selected';
  447. break;
  448. }
  449. }
  450. }
  451. return $properties;
  452. }
  453. /**
  454. * Whether current field can be populated dynamically.
  455. *
  456. * @since 1.5.0
  457. *
  458. * @param array $properties Field properties.
  459. * @param array $field Current field specific data.
  460. *
  461. * @return bool
  462. */
  463. public function is_fallback_population_allowed( $properties, $field ) {
  464. $allowed = true;
  465. // Allow population on front-end only.
  466. if ( is_admin() ) {
  467. $allowed = false;
  468. }
  469. /*
  470. * Commented out to allow partial fail for complex multi-inputs fields.
  471. * Example: name field with first/last format and being required, filled out only first.
  472. * On submit we will preserve those sub-inputs that are not empty and display an error for an empty.
  473. */
  474. // Do not populate if there are errors for that field.
  475. /*
  476. $errors = wpforms()->process->errors;
  477. if ( ! empty( $errors[ $this->form_data['id'] ][ $field['id'] ] ) ) {
  478. $allowed = false;
  479. }
  480. */
  481. // Require form id being the same for submitted and currently rendered form.
  482. if (
  483. ! empty( $_POST['wpforms']['id'] ) && // phpcs:ignore
  484. (int) $_POST['wpforms']['id'] !== (int) $this->form_data['id'] // phpcs:ignore
  485. ) {
  486. $allowed = false;
  487. }
  488. // Require $_POST of submitted field.
  489. if ( empty( $_POST['wpforms']['fields'] ) ) { // phpcs:ignore
  490. $allowed = false;
  491. }
  492. // Require field (processed and rendered) being the same.
  493. if ( ! isset( $_POST['wpforms']['fields'][ $field['id'] ] ) ) { // phpcs:ignore
  494. $allowed = false;
  495. }
  496. return apply_filters( 'wpforms_field_is_fallback_population_allowed', $allowed, $properties, $field );
  497. }
  498. /**
  499. * Prefill the field value with a fallback value from form submission (in case of JS validation failed), that we get from $_POST.
  500. *
  501. * @since 1.5.0
  502. *
  503. * @param array $properties Field properties.
  504. * @param array $field Current field specific data.
  505. *
  506. * @return array Modified field properties.
  507. */
  508. protected function field_prefill_value_property_fallback( $properties, $field ) {
  509. if ( ! $this->is_fallback_population_allowed( $properties, $field ) ) {
  510. return $properties;
  511. }
  512. if ( empty( $_POST['wpforms']['fields'] ) || ! is_array( $_POST['wpforms']['fields'] ) ) { // phpcs:ignore
  513. return $properties;
  514. }
  515. // We got user submitted raw data (not processed, will be done later).
  516. $raw_value = $_POST['wpforms']['fields'][ $field['id'] ]; // phpcs:ignore
  517. $input = 'primary';
  518. if ( ! empty( $raw_value ) ) {
  519. $this->field_prefill_remove_choices_defaults( $field, $properties );
  520. }
  521. /*
  522. * For this particular field this value may be either array or a string.
  523. * In array - this is a complex field, like address.
  524. * The key in array will be a sub-input (address1, state), and its appropriate value.
  525. */
  526. if ( is_array( $raw_value ) ) {
  527. foreach ( $raw_value as $input => $single_value ) {
  528. $properties = $this->get_field_populated_single_property_value( $single_value, sanitize_key( $input ), $properties, $field );
  529. }
  530. } else {
  531. $properties = $this->get_field_populated_single_property_value( $raw_value, sanitize_key( $input ), $properties, $field );
  532. }
  533. return $properties;
  534. }
  535. /**
  536. * Create the button for the 'Add Fields' tab, inside the form editor.
  537. *
  538. * @since 1.0.0
  539. *
  540. * @param array $fields List of form fields with their data.
  541. *
  542. * @return array
  543. */
  544. public function field_button( $fields ) {
  545. // Add field information to fields array.
  546. $fields[ $this->group ]['fields'][] = array(
  547. 'order' => $this->order,
  548. 'name' => $this->name,
  549. 'type' => $this->type,
  550. 'icon' => $this->icon,
  551. );
  552. // Wipe hands clean.
  553. return $fields;
  554. }
  555. /**
  556. * Create the field options panel. Used by subclasses.
  557. *
  558. * @since 1.0.0
  559. * @since 1.5.0 Converted to abstract method, as it's required for all fields.
  560. *
  561. * @param array $field Field data and settings.
  562. */
  563. abstract public function field_options( $field );
  564. /**
  565. * Create the field preview. Used by subclasses.
  566. *
  567. * @since 1.0.0
  568. * @since 1.5.0 Converted to abstract method, as it's required for all fields.
  569. *
  570. * @param array $field Field data and settings.
  571. */
  572. abstract public function field_preview( $field );
  573. /**
  574. * Helper function to create field option elements.
  575. *
  576. * Field option elements are pieces that help create a field option.
  577. * They are used to quickly build field options.
  578. *
  579. * @since 1.0.0
  580. *
  581. * @param string $option Field option to render.
  582. * @param array $field Field data and settings.
  583. * @param array $args Field preview arguments.
  584. * @param bool $echo Print or return the value. Print by default.
  585. *
  586. * @return mixed echo or return string
  587. */
  588. public function field_element( $option, $field, $args = array(), $echo = true ) {
  589. $id = (int) $field['id'];
  590. $class = ! empty( $args['class'] ) ? wpforms_sanitize_classes( (array) $args['class'], true ) : '';
  591. $slug = ! empty( $args['slug'] ) ? sanitize_title( $args['slug'] ) : '';
  592. $attrs = '';
  593. $output = '';
  594. if ( ! empty( $args['data'] ) ) {
  595. foreach ( $args['data'] as $arg_key => $val ) {
  596. if ( is_array( $val ) ) {
  597. $val = wp_json_encode( $val );
  598. }
  599. $attrs .= ' data-' . $arg_key . '=\'' . $val . '\'';
  600. }
  601. }
  602. if ( ! empty( $args['attrs'] ) ) {
  603. foreach ( $args['attrs'] as $arg_key => $val ) {
  604. if ( is_array( $val ) ) {
  605. $val = wp_json_encode( $val );
  606. }
  607. $attrs .= $arg_key . '=\'' . $val . '\'';
  608. }
  609. }
  610. switch ( $option ) {
  611. // Row.
  612. case 'row':
  613. $output = sprintf(
  614. '<div class="wpforms-field-option-row wpforms-field-option-row-%s %s" id="wpforms-field-option-row-%d-%s" data-field-id="%d" %s>%s</div>',
  615. $slug,
  616. $class,
  617. $id,
  618. $slug,
  619. $id,
  620. $attrs,
  621. $args['content']
  622. );
  623. break;
  624. // Label.
  625. case 'label':
  626. $class = ! empty( $class ) ? ' class="' . $class . '"' : '';
  627. $output = sprintf( '<label for="wpforms-field-option-%d-%s"%s>%s', $id, $slug, $class, esc_html( $args['value'] ) );
  628. if ( isset( $args['tooltip'] ) && ! empty( $args['tooltip'] ) ) {
  629. $output .= sprintf( '<i class="fa fa-question-circle-o wpforms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) );
  630. }
  631. if ( isset( $args['after_tooltip'] ) && ! empty( $args['after_tooltip'] ) ) {
  632. $output .= $args['after_tooltip'];
  633. }
  634. $output .= '</label>';
  635. break;
  636. // Text input.
  637. case 'text':
  638. $type = ! empty( $args['type'] ) ? esc_attr( $args['type'] ) : 'text';
  639. $placeholder = ! empty( $args['placeholder'] ) ? esc_attr( $args['placeholder'] ) : '';
  640. $before = ! empty( $args['before'] ) ? '<span class="before-input">' . esc_html( $args['before'] ) . '</span>' : '';
  641. if ( ! empty( $before ) ) {
  642. $class .= ' has-before';
  643. }
  644. $output = sprintf( '%s<input type="%s" class="%s" id="wpforms-field-option-%d-%s" name="fields[%d][%s]" value="%s" placeholder="%s" %s>', $before, $type, $class, $id, $slug, $id, $slug, esc_attr( $args['value'] ), $placeholder, $attrs );
  645. break;
  646. // Textarea.
  647. case 'textarea':
  648. $rows = ! empty( $args['rows'] ) ? (int) $args['rows'] : '3';
  649. $output = sprintf( '<textarea class="%s" id="wpforms-field-option-%d-%s" name="fields[%d][%s]" rows="%d" %s>%s</textarea>', $class, $id, $slug, $id, $slug, $rows, $attrs, $args['value'] );
  650. break;
  651. // Checkbox.
  652. case 'checkbox':
  653. $checked = checked( '1', $args['value'], false );
  654. $output = sprintf( '<input type="checkbox" class="%s" id="wpforms-field-option-%d-%s" name="fields[%d][%s]" value="1" %s %s>', $class, $id, $slug, $id, $slug, $checked, $attrs );
  655. $output .= empty( $args['nodesc'] ) ? sprintf( '<label for="wpforms-field-option-%d-%s" class="inline">%s', $id, $slug, $args['desc'] ) : '';
  656. if ( isset( $args['tooltip'] ) && ! empty( $args['tooltip'] ) ) {
  657. $output .= sprintf( '<i class="fa fa-question-circle-o wpforms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) );
  658. }
  659. $output .= empty( $args['nodesc'] ) ? '</label>' : '';
  660. break;
  661. // Toggle.
  662. case 'toggle':
  663. $output = $this->field_element_toggle( $args, $id, $slug, $attrs, $class );
  664. break;
  665. // Select.
  666. case 'select':
  667. $options = $args['options'];
  668. $value = isset( $args['value'] ) ? $args['value'] : '';
  669. $output = sprintf( '<select class="%s" id="wpforms-field-option-%d-%s" name="fields[%d][%s]" %s>', $class, $id, $slug, $id, $slug, $attrs );
  670. foreach ( $options as $arg_key => $arg_option ) {
  671. $output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $arg_key ), selected( $arg_key, $value, false ), $arg_option );
  672. }
  673. $output .= '</select>';
  674. break;
  675. }
  676. if ( ! $echo ) {
  677. return $output;
  678. }
  679. // @todo Ideally, we should late-escape here. All data above seems to be escaped or trusted, but we should consider refactoring this method.
  680. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  681. echo $output;
  682. }
  683. /**
  684. * Create field option toggle element.
  685. *
  686. * @since 1.6.8
  687. *
  688. * @param array $args Arguments.
  689. * @param integer $id Field ID.
  690. * @param string $slug Field slug.
  691. * @param string $attrs Attributes.
  692. * @param string $class Class.
  693. *
  694. * @return string
  695. */
  696. private function field_element_toggle( $args, $id, $slug, $attrs, $class ) {
  697. $input_id = sprintf(
  698. 'wpforms-field-option-%d-%s',
  699. esc_attr( $id ),
  700. esc_attr( $slug )
  701. );
  702. $field_name = sprintf(
  703. 'fields[%d][%s]',
  704. esc_attr( $id ),
  705. esc_attr( $slug )
  706. );
  707. $label = ! empty( $args['desc'] ) ? $args['desc'] : '';
  708. $value = ! empty( $args['value'] ) ? $args['value'] : '';
  709. // Compatibility with the `checkbox` element.
  710. $args['label-hide'] = ! empty( $args['nodesc'] ) ? $args['nodesc'] : false;
  711. $args['input-class'] = $class;
  712. return wpforms_panel_field_toggle_control( $args, $input_id, $field_name, $label, $value, $attrs );
  713. }
  714. /**
  715. * Helper function to create common field options that are used frequently.
  716. *
  717. * @since 1.0.0
  718. *
  719. * @param string $option Field option to render.
  720. * @param array $field Field data and settings.
  721. * @param array $args Field preview arguments.
  722. * @param bool $echo Print or return the value. Print by default.
  723. *
  724. * @return mixed echo or return string
  725. */
  726. public function field_option( $option, $field, $args = [], $echo = true ) { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded, Generic.Metrics.NestingLevel.MaxExceeded
  727. $output = '';
  728. $markup = '';
  729. switch ( $option ) {
  730. /**
  731. * Basic Fields.
  732. */
  733. /*
  734. * Basic Options markup.
  735. */
  736. case 'basic-options':
  737. $markup = ! empty( $args['markup'] ) ? $args['markup'] : 'open';
  738. $class = ! empty( $args['class'] ) ? esc_html( $args['class'] ) : '';
  739. if ( $markup === 'open' ) {
  740. $output = sprintf(
  741. '<div class="wpforms-field-option-field-title">%3$s <span>(ID #%1$d)</span></div>
  742. <div class="wpforms-field-option-group wpforms-field-option-group-basic active" id="wpforms-field-option-basic-%1$d">
  743. <a href="#" class="wpforms-field-option-group-toggle">%2$s</a>
  744. <div class="wpforms-field-option-group-inner %4$s">
  745. ',
  746. absint( $field['id'] ),
  747. esc_html__( 'General', 'wpforms-lite' ),
  748. esc_html( $this->name ),
  749. esc_attr( $class )
  750. );
  751. } else {
  752. $output = '</div></div>';
  753. }
  754. break;
  755. /*
  756. * Field Label.
  757. */
  758. case 'label':
  759. $value = ! empty( $field['label'] ) ? esc_html( $field['label'] ) : '';
  760. $tooltip = esc_html__( 'Enter text for the form field label. Field labels are recommended and can be hidden in the Advanced Settings.', 'wpforms-lite' );
  761. $output = $this->field_element( 'label', $field, array( 'slug' => 'label', 'value' => esc_html__( 'Label', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  762. $output .= $this->field_element( 'text', $field, array( 'slug' => 'label', 'value' => $value ), false );
  763. $output = $this->field_element( 'row', $field, array( 'slug' => 'label', 'content' => $output ), false );
  764. break;
  765. /*
  766. * Field Description.
  767. */
  768. case 'description':
  769. $value = ! empty( $field['description'] ) ? esc_html( $field['description'] ) : '';
  770. $tooltip = esc_html__( 'Enter text for the form field description.', 'wpforms-lite' );
  771. $output = $this->field_element( 'label', $field, array( 'slug' => 'description', 'value' => esc_html__( 'Description', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  772. $output .= $this->field_element( 'textarea', $field, array( 'slug' => 'description', 'value' => $value ), false );
  773. $output = $this->field_element( 'row', $field, array( 'slug' => 'description', 'content' => $output ), false );
  774. break;
  775. /*
  776. * Field Required toggle.
  777. */
  778. case 'required':
  779. $default = ! empty( $args['default'] ) ? $args['default'] : '0';
  780. $value = isset( $field['required'] ) ? esc_attr( $field['required'] ) : esc_attr( $default );
  781. $tooltip = esc_html__( 'Check this option to mark the field required. A form will not submit unless all required fields are provided.', 'wpforms-lite' );
  782. $output = $this->field_element(
  783. 'toggle',
  784. $field,
  785. [
  786. 'slug' => 'required',
  787. 'value' => $value,
  788. 'desc' => esc_html__( 'Required', 'wpforms-lite' ),
  789. 'tooltip' => $tooltip,
  790. ],
  791. false
  792. );
  793. $output = $this->field_element(
  794. 'row',
  795. $field,
  796. [
  797. 'slug' => 'required',
  798. 'content' => $output,
  799. ],
  800. false
  801. );
  802. break;
  803. /*
  804. * Field Meta (field type and ID).
  805. */
  806. case 'meta':
  807. _deprecated_argument( __CLASS__ . '::' . __METHOD__ . '( [ \'slug\' => \'meta\' ] )', '1.7.1' );
  808. $output = sprintf( '<label>%s</label>', esc_html__( 'Type', 'wpforms-lite' ) );
  809. $output .= sprintf(
  810. '<p class="meta">%s <span class="id">(ID #%d)</span></p>',
  811. esc_attr( $this->name ),
  812. absint( $field['id'] )
  813. );
  814. $output = $this->field_element(
  815. 'row',
  816. $field,
  817. [
  818. 'slug' => 'meta',
  819. 'content' => $output,
  820. ],
  821. false
  822. );
  823. break;
  824. /*
  825. * Code Block.
  826. */
  827. case 'code':
  828. $value = ! empty( $field['code'] ) ? esc_textarea( $field['code'] ) : '';
  829. $tooltip = esc_html__( 'Enter code for the form field.', 'wpforms-lite' );
  830. $output = $this->field_element( 'label', $field, array( 'slug' => 'code', 'value' => esc_html__( 'Code', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  831. $output .= $this->field_element( 'textarea', $field, array( 'slug' => 'code', 'value' => $value ), false );
  832. $output = $this->field_element( 'row', $field, array( 'slug' => 'code', 'content' => $output ), false );
  833. break;
  834. /*
  835. * Choices.
  836. */
  837. case 'choices':
  838. $values = ! empty( $field['choices'] ) ? $field['choices'] : $this->defaults;
  839. $label = ! empty( $args['label'] ) ? esc_html( $args['label'] ) : esc_html__( 'Choices', 'wpforms-lite' );
  840. $class = array();
  841. $field_type = $this->type;
  842. if ( ! empty( $field['multiple'] ) ) {
  843. $field_type = 'checkbox';
  844. }
  845. if ( ! empty( $field['show_values'] ) ) {
  846. $class[] = 'show-values';
  847. }
  848. if ( ! empty( $field['dynamic_choices'] ) ) {
  849. $class[] = 'wpforms-hidden';
  850. }
  851. if ( ! empty( $field['choices_images'] ) ) {
  852. $class[] = 'show-images';
  853. }
  854. // Field label.
  855. $lbl = $this->field_element(
  856. 'label',
  857. $field,
  858. array(
  859. 'slug' => 'choices',
  860. 'value' => $label,
  861. 'tooltip' => esc_html__( 'Add choices for the form field.', 'wpforms-lite' ),
  862. 'after_tooltip' => '<a href="#" class="toggle-bulk-add-display toggle-unfoldable-cont"><i class="fa fa-download"></i><span>' . esc_html__( 'Bulk Add', 'wpforms-lite' ) . '</span></a>',
  863. ),
  864. false
  865. );
  866. // Field contents.
  867. $fld = sprintf(
  868. '<ul data-next-id="%s" class="choices-list %s" data-field-id="%d" data-field-type="%s">',
  869. max( array_keys( $values ) ) + 1,
  870. wpforms_sanitize_classes( $class, true ),
  871. absint( $field['id'] ),
  872. esc_attr( $this->type )
  873. );
  874. foreach ( $values as $key => $value ) {
  875. $default = ! empty( $value['default'] ) ? $value['default'] : '';
  876. $base = sprintf( 'fields[%s][choices][%s]', absint( $field['id'] ), sanitize_key( $key ) );
  877. $image = ! empty( $value['image'] ) ? $value['image'] : '';
  878. $hide_image_btn = false;
  879. $fld .= '<li data-key="' . absint( $key ) . '">';
  880. $fld .= sprintf(
  881. '<input type="%s" name="%s[default]" class="default" value="1" %s>',
  882. $field_type === 'checkbox' ? 'checkbox' : 'radio',
  883. esc_attr( $base ),
  884. checked( '1', $default, false )
  885. );
  886. $fld .= '<span class="move"><i class="fa fa-bars"></i></span>';
  887. $fld .= sprintf(
  888. '<input type="text" name="%s[label]" value="%s" class="label">',
  889. esc_attr( $base ),
  890. esc_attr( $value['label'] )
  891. );
  892. $fld .= '<a class="add" href="#"><i class="fa fa-plus-circle"></i></a><a class="remove" href="#"><i class="fa fa-minus-circle"></i></a>';
  893. $fld .= sprintf(
  894. '<input type="text" name="%s[value]" value="%s" class="value">',
  895. esc_attr( $base ),
  896. esc_attr( ! isset( $value['value'] ) ? '' : $value['value'] )
  897. );
  898. $fld .= '<div class="wpforms-image-upload">';
  899. $fld .= '<div class="preview">';
  900. if ( ! empty( $image ) ) {
  901. $fld .= sprintf(
  902. '<img src="%s"><a href="#" title="%s" class="wpforms-image-upload-remove"><i class="fa fa-trash-o"></i></a>',
  903. esc_url_raw( $image ),
  904. esc_attr__( 'Remove Image', 'wpforms-lite' )
  905. );
  906. $hide_image_btn = true;
  907. }
  908. $fld .= '</div>';
  909. $fld .= sprintf(
  910. '<button class="wpforms-btn wpforms-btn-sm wpforms-btn-blue wpforms-btn-block wpforms-image-upload-add" data-after-upload="hide"%s>%s</button>',
  911. $hide_image_btn ? ' style="display:none;"' : '',
  912. esc_html__( 'Upload Image', 'wpforms-lite' )
  913. );
  914. $fld .= sprintf(
  915. '<input type="hidden" name="%s[image]" value="%s" class="source">',
  916. esc_attr( $base ),
  917. esc_url_raw( $image )
  918. );
  919. $fld .= '</div>';
  920. $fld .= '</li>';
  921. }
  922. $fld .= '</ul>';
  923. // Field note: dynamic status.
  924. $source = '';
  925. $type = '';
  926. $dynamic = ! empty( $field['dynamic_choices'] ) ? esc_html( $field['dynamic_choices'] ) : '';
  927. if ( 'post_type' === $dynamic && ! empty( $field[ 'dynamic_' . $dynamic ] ) ) {
  928. $type = esc_html__( 'post type', 'wpforms-lite' );
  929. $pt = get_post_type_object( $field[ 'dynamic_' . $dynamic ] );
  930. $source = '';
  931. if ( null !== $pt ) {
  932. $source = $pt->labels->name;
  933. }
  934. } elseif ( 'taxonomy' === $dynamic && ! empty( $field[ 'dynamic_' . $dynamic ] ) ) {
  935. $type = esc_html__( 'taxonomy', 'wpforms-lite' );
  936. $tax = get_taxonomy( $field[ 'dynamic_' . $dynamic ] );
  937. $source = '';
  938. if ( false !== $tax ) {
  939. $source = $tax->labels->name;
  940. }
  941. }
  942. $note = sprintf(
  943. '<div class="wpforms-alert-warning wpforms-alert %s">',
  944. ! empty( $dynamic ) && ! empty( $field[ 'dynamic_' . $dynamic ] ) ? '' : 'wpforms-hidden'
  945. );
  946. $note .= '<h4>' . esc_html__( 'Dynamic Choices Active', 'wpforms-lite' ) . '</h4>';
  947. $note .= sprintf(
  948. /* translators: %1$s - source name; %2$s - type name. */
  949. '<p>' . esc_html__( 'Choices are dynamically populated from the %1$s %2$s. Go to the Advanced tab to change this.', 'wpforms-lite' ) . '</p>',
  950. '<span class="dynamic-name">' . esc_html( $source ) . '</span>',
  951. '<span class="dynamic-type">' . esc_html( $type ) . '</span>'
  952. );
  953. $note .= '</div>';
  954. // Final field output.
  955. $output = $this->field_element(
  956. 'row',
  957. $field,
  958. array(
  959. 'slug' => 'choices',
  960. 'content' => $lbl . $fld . $note,
  961. ),
  962. false
  963. );
  964. break;
  965. /*
  966. * Choices for payments.
  967. */
  968. case 'choices_payments':
  969. $values = ! empty( $field['choices'] ) ? $field['choices'] : $this->defaults;
  970. $class = array();
  971. $input_type = in_array( $field['type'], array( 'payment-multiple', 'payment-select' ), true ) ? 'radio' : 'checkbox';
  972. if ( ! empty( $field['choices_images'] ) ) {
  973. $class[] = 'show-images';
  974. }
  975. // Field label.
  976. $lbl = $this->field_element(
  977. 'label',
  978. $field,
  979. array(
  980. 'slug' => 'choices',
  981. 'value' => esc_html__( 'Items', 'wpforms-lite' ),
  982. 'tooltip' => esc_html__( 'Add choices for the form field.', 'wpforms-lite' ),
  983. ),
  984. false
  985. );
  986. // Field contents.
  987. $fld = sprintf(
  988. '<ul data-next-id="%s" class="choices-list %s" data-field-id="%d" data-field-type="%s">',
  989. max( array_keys( $values ) ) + 1,
  990. wpforms_sanitize_classes( $class, true ),
  991. absint( $field['id'] ),
  992. esc_attr( $this->type )
  993. );
  994. foreach ( $values as $key => $value ) {
  995. $default = ! empty( $value['default'] ) ? $value['default'] : '';
  996. $base = sprintf( 'fields[%s][choices][%s]', absint( $field['id'] ), esc_attr( $key ) );
  997. $image = ! empty( $value['image'] ) ? $value['image'] : '';
  998. $hide_image_btn = false;
  999. $fld .= '<li data-key="' . absint( $key ) . '">';
  1000. $fld .= sprintf(
  1001. '<input type="%s" name="%s[default]" class="default" value="1" %s>',
  1002. esc_attr( $input_type ),
  1003. esc_attr( $base ),
  1004. checked( '1', $default, false )
  1005. );
  1006. $fld .= '<span class="move"><i class="fa fa-bars"></i></span>';
  1007. $fld .= sprintf(
  1008. '<input type="text" name="%s[label]" value="%s" class="label">',
  1009. esc_attr( $base ),
  1010. esc_attr( $value['label'] )
  1011. );
  1012. $fld .= sprintf(
  1013. '<input type="text" name="%s[value]" value="%s" class="value wpforms-money-input" placeholder="%s">',
  1014. esc_attr( $base ),
  1015. esc_attr( $value['value'] ),
  1016. wpforms_format_amount( 0 )
  1017. );
  1018. $fld .= '<a class="add" href="#"><i class="fa fa-plus-circle"></i></a><a class="remove" href="#"><i class="fa fa-minus-circle"></i></a>';
  1019. $fld .= '<div class="wpforms-image-upload">';
  1020. $fld .= '<div class="preview">';
  1021. if ( ! empty( $image ) ) {
  1022. $fld .= sprintf(
  1023. '<img src="%s"><a href="#" title="%s" class="wpforms-image-upload-remove"><i class="fa fa-trash-o"></i></a>',
  1024. esc_url_raw( $image ),
  1025. esc_attr__( 'Remove Image', 'wpforms-lite' )
  1026. );
  1027. $hide_image_btn = true;
  1028. }
  1029. $fld .= '</div>';
  1030. $fld .= sprintf(
  1031. '<button class="wpforms-btn wpforms-btn-sm wpforms-btn-blue wpforms-btn-block wpforms-image-upload-add" data-after-upload="hide"%s>%s</button>',
  1032. $hide_image_btn ? ' style="display:none;"' : '',
  1033. esc_html__( 'Upload Image', 'wpforms-lite' )
  1034. );
  1035. $fld .= sprintf(
  1036. '<input type="hidden" name="%s[image]" value="%s" class="source">',
  1037. $base,
  1038. esc_url_raw( $image )
  1039. );
  1040. $fld .= '</div>';
  1041. $fld .= '</li>';
  1042. }
  1043. $fld .= '</ul>';
  1044. // Final field output.
  1045. $output = $this->field_element(
  1046. 'row',
  1047. $field,
  1048. [
  1049. 'slug' => 'choices',
  1050. 'content' => $lbl . $fld,
  1051. ],
  1052. false
  1053. );
  1054. break;
  1055. /*
  1056. * Choices Images.
  1057. */
  1058. case 'choices_images':
  1059. // Field note: Image tips.
  1060. $note = sprintf(
  1061. '<div class="wpforms-alert-warning wpforms-alert %s">',
  1062. ! empty( $field['choices_images'] ) ? '' : 'wpforms-hidden'
  1063. );
  1064. $note .= wp_kses( /* translators: %s - URL to the ActiveCampaign Getting started page. */
  1065. __( '<h4>Images are not cropped or resized.</h4><p>For best results, they should be the same size and 250x250 pixels or smaller.</p>', 'wpforms-lite' ),
  1066. [
  1067. 'h4' => [],
  1068. 'p' => [],
  1069. ]
  1070. );
  1071. $note .= '</div>';
  1072. // Field contents.
  1073. $fld = $this->field_element(
  1074. 'toggle',
  1075. $field,
  1076. [
  1077. 'slug' => 'choices_images',
  1078. 'value' => isset( $field['choices_images'] ) ? '1' : '0',
  1079. 'desc' => esc_html__( 'Use image choices', 'wpforms-lite' ),
  1080. 'tooltip' => esc_html__( 'Check this option to enable using images with the choices.', 'wpforms-lite' ),
  1081. ],
  1082. false
  1083. );
  1084. // Final field output.
  1085. $output = $this->field_element(
  1086. 'row',
  1087. $field,
  1088. [
  1089. 'slug' => 'choices_images',
  1090. 'class' => ! empty( $field['dynamic_choices'] ) ? 'wpforms-hidden' : '',
  1091. 'content' => $note . $fld,
  1092. ],
  1093. false
  1094. );
  1095. break;
  1096. /*
  1097. * Choices Images Style.
  1098. */
  1099. case 'choices_images_style':
  1100. // Field label.
  1101. $lbl = $this->field_element(
  1102. 'label',
  1103. $field,
  1104. array(
  1105. 'slug' => 'choices_images_style',
  1106. 'value' => esc_html__( 'Image Choice Style', 'wpforms-lite' ),
  1107. 'tooltip' => esc_html__( 'Select the style for the image choices.', 'wpforms-lite' ),
  1108. ),
  1109. false
  1110. );
  1111. // Field contents.
  1112. $fld = $this->field_element(
  1113. 'select',
  1114. $field,
  1115. array(
  1116. 'slug' => 'choices_images_style',
  1117. 'value' => ! empty( $field['choices_images_style'] ) ? esc_attr( $field['choices_images_style'] ) : 'modern',
  1118. 'options' => array(
  1119. 'modern' => esc_html__( 'Modern', 'wpforms-lite' ),
  1120. 'classic' => esc_html__( 'Classic', 'wpforms-lite' ),
  1121. 'none' => esc_html__( 'None', 'wpforms-lite' ),
  1122. ),
  1123. ),
  1124. false
  1125. );
  1126. // Final field output.
  1127. $output = $this->field_element(
  1128. 'row',
  1129. $field,
  1130. array(
  1131. 'slug' => 'choices_images_style',
  1132. 'content' => $lbl . $fld,
  1133. 'class' => ! empty( $field['choices_images'] ) ? '' : 'wpforms-hidden',
  1134. ),
  1135. false
  1136. );
  1137. break;
  1138. /**
  1139. * Advanced Fields.
  1140. */
  1141. /*
  1142. * Default value.
  1143. */
  1144. case 'default_value':
  1145. $value = ! empty( $field['default_value'] ) || ( isset( $field['default_value'] ) && '0' === (string) $field['default_value'] ) ? esc_attr( $field['default_value'] ) : '';
  1146. $tooltip = esc_html__( 'Enter text for the default form field value.', 'wpforms-lite' );
  1147. $toggle = '<a href="#" class="toggle-smart-tag-display toggle-unfoldable-cont" data-type="other"><i class="fa fa-tags"></i><span>' . esc_html__( 'Show Smart Tags', 'wpforms-lite' ) . '</span></a>';
  1148. $output = $this->field_element( 'label', $field, array( 'slug' => 'default_value', 'value' => esc_html__( 'Default Value', 'wpforms-lite' ), 'tooltip' => $tooltip, 'after_tooltip' => $toggle ), false );
  1149. $output .= $this->field_element( 'text', $field, array( 'slug' => 'default_value', 'value' => $value ), false );
  1150. $output = $this->field_element( 'row', $field, array( 'slug' => 'default_value', 'content' => $output ), false );
  1151. break;
  1152. /*
  1153. * Size.
  1154. */
  1155. case 'size':
  1156. $value = ! empty( $field['size'] ) ? esc_attr( $field['size'] ) : 'medium';
  1157. $class = ! empty( $args['class'] ) ? esc_html( $args['class'] ) : '';
  1158. $tooltip = esc_html__( 'Select the default form field size.', 'wpforms-lite' );
  1159. $options = array(
  1160. 'small' => esc_html__( 'Small', 'wpforms-lite' ),
  1161. 'medium' => esc_html__( 'Medium', 'wpforms-lite' ),
  1162. 'large' => esc_html__( 'Large', 'wpforms-lite' ),
  1163. );
  1164. $output = $this->field_element( 'label', $field, array( 'slug' => 'size', 'value' => esc_html__( 'Field Size', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  1165. $output .= $this->field_element( 'select', $field, array( 'slug' => 'size', 'value' => $value, 'options' => $options ), false );
  1166. $output = $this->field_element( 'row', $field, array( 'slug' => 'size', 'content' => $output, 'class' => $class ), false );
  1167. break;
  1168. /*
  1169. * Advanced Options markup.
  1170. */
  1171. case 'advanced-options':
  1172. $markup = ! empty( $args['markup'] ) ? $args['markup'] : 'open';
  1173. if ( $markup === 'open' ) {
  1174. $override = apply_filters( 'wpforms_advanced_options_override', false );
  1175. $override = ! empty( $override ) ? 'style="display:' . $override . ';"' : '';
  1176. $output = sprintf( '<div class="wpforms-field-option-group wpforms-field-option-group-advanced" id="wpforms-field-option-advanced-%d" %s>', absint( $field['id'] ), $override );
  1177. $output .= sprintf( '<a href="#" class="wpforms-field-option-group-toggle">%s</a>', esc_html__( 'Advanced', 'wpforms-lite' ) );
  1178. $output .= '<div class="wpforms-field-option-group-inner">';
  1179. } else {
  1180. $output = '</div></div>';
  1181. }
  1182. break;
  1183. /*
  1184. * Placeholder.
  1185. */
  1186. case 'placeholder':
  1187. $value = ! empty( $field['placeholder'] ) ? esc_attr( $field['placeholder'] ) : '';
  1188. $tooltip = esc_html__( 'Enter text for the form field placeholder.', 'wpforms-lite' );
  1189. $output = $this->field_element( 'label', $field, array( 'slug' => 'placeholder', 'value' => esc_html__( 'Placeholder Text', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  1190. $output .= $this->field_element( 'text', $field, array( 'slug' => 'placeholder', 'value' => $value ), false );
  1191. $output = $this->field_element( 'row', $field, array( 'slug' => 'placeholder', 'content' => $output ), false );
  1192. break;
  1193. /*
  1194. * CSS classes.
  1195. */
  1196. case 'css':
  1197. $toggle = '';
  1198. $value = ! empty( $field['css'] ) ? esc_attr( $field['css'] ) : '';
  1199. $tooltip = esc_html__( 'Enter CSS class names for the form field container. Class names should be separated with spaces.', 'wpforms-lite' );
  1200. if ( 'pagebreak' !== $field['type'] ) {
  1201. $toggle = '<a href="#" class="toggle-layout-selector-display toggle-unfoldable-cont"><i class="fa fa-th-large"></i><span>' . esc_html__( 'Show Layouts', 'wpforms-lite' ) . '</span></a>';
  1202. }
  1203. // Build output.
  1204. $output = $this->field_element( 'label', $field, array( 'slug' => 'css', 'value' => esc_html__( 'CSS Classes', 'wpforms-lite' ), 'tooltip' => $tooltip, 'after_tooltip' => $toggle ), false );
  1205. $output .= $this->field_element( 'text', $field, array( 'slug' => 'css', 'value' => $value ), false );
  1206. $output = $this->field_element( 'row', $field, array( 'slug' => 'css', 'content' => $output ), false );
  1207. break;
  1208. /*
  1209. * Hide Label.
  1210. */
  1211. case 'label_hide':
  1212. $value = isset( $field['label_hide'] ) ? $field['label_hide'] : '0';
  1213. $tooltip = esc_html__( 'Check this option to hide the form field label.', 'wpforms-lite' );
  1214. // Build output.
  1215. $output = $this->field_element(
  1216. 'toggle',
  1217. $field,
  1218. [
  1219. 'slug' => 'label_hide',
  1220. 'value' => $value,
  1221. 'desc' => esc_html__( 'Hide Label', 'wpforms-lite' ),
  1222. 'tooltip' => $tooltip,
  1223. ],
  1224. false
  1225. );
  1226. $output = $this->field_element(
  1227. 'row',
  1228. $field,
  1229. [
  1230. 'slug' => 'label_hide',
  1231. 'content' => $output,
  1232. ],
  1233. false
  1234. );
  1235. break;
  1236. /*
  1237. * Hide sublabels.
  1238. */
  1239. case 'sublabel_hide':
  1240. $value = isset( $field['sublabel_hide'] ) ? $field['sublabel_hide'] : '0';
  1241. $tooltip = esc_html__( 'Check this option to hide the form field sublabel.', 'wpforms-lite' );
  1242. // Build output.
  1243. $output = $this->field_element(
  1244. 'toggle',
  1245. $field,
  1246. [
  1247. 'slug' => 'sublabel_hide',
  1248. 'value' => $value,
  1249. 'desc' => esc_html__( 'Hide Sublabels', 'wpforms-lite' ),
  1250. 'tooltip' => $tooltip,
  1251. ],
  1252. false
  1253. );
  1254. $output = $this->field_element(
  1255. 'row',
  1256. $field,
  1257. [
  1258. 'slug' => 'sublabel_hide',
  1259. 'content' => $output,
  1260. ],
  1261. false
  1262. );
  1263. break;
  1264. /*
  1265. * Input Columns.
  1266. */
  1267. case 'input_columns':
  1268. $value = ! empty( $field['input_columns'] ) ? esc_attr( $field['input_columns'] ) : '';
  1269. $tooltip = esc_html__( 'Select the layout for displaying field choices.', 'wpforms-lite' );
  1270. $options = array(
  1271. '' => esc_html__( 'One Column', 'wpforms-lite' ),
  1272. '2' => esc_html__( 'Two Columns', 'wpforms-lite' ),
  1273. '3' => esc_html__( 'Three Columns', 'wpforms-lite' ),
  1274. 'inline' => esc_html__( 'Inline', 'wpforms-lite' ),
  1275. );
  1276. $output = $this->field_element( 'label', $field, array( 'slug' => 'input_columns', 'value' => esc_html__( 'Choice Layout', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  1277. $output .= $this->field_element( 'select', $field, array( 'slug' => 'input_columns', 'value' => $value, 'options' => $options ), false );
  1278. $output = $this->field_element( 'row', $field, array( 'slug' => 'input_columns', 'content' => $output ), false );
  1279. break;
  1280. /*
  1281. * Dynamic Choices.
  1282. */
  1283. case 'dynamic_choices':
  1284. $value = ! empty( $field['dynamic_choices'] ) ? esc_attr( $field['dynamic_choices'] ) : '';
  1285. $tooltip = esc_html__( 'Select auto-populate method to use.', 'wpforms-lite' );
  1286. $options = array(
  1287. '' => esc_html__( 'Off', 'wpforms-lite' ),
  1288. 'post_type' => esc_html__( 'Post Type', 'wpforms-lite' ),
  1289. 'taxonomy' => esc_html__( 'Taxonomy', 'wpforms-lite' ),
  1290. );
  1291. $output = $this->field_element( 'label', $field, array( 'slug' => 'dynamic_choices', 'value' => esc_html__( 'Dynamic Choices', 'wpforms-lite' ), 'tooltip' => $tooltip ), false );
  1292. $output .= $this->field_element( 'select', $field, array( 'slug' => 'dynamic_choices', 'value' => $value, 'options' => $options ), false );
  1293. $output = $this->field_element( 'row', $field, array( 'slug' => 'dynamic_choices', 'content' => $output ), false );
  1294. break;
  1295. /*
  1296. * Dynamic Choices Source.
  1297. */
  1298. case 'dynamic_choices_source':
  1299. $output = '';
  1300. $type = ! empty( $field['dynamic_choices'] ) ? esc_attr( $field['dynamic_choices'] ) : '';
  1301. if ( ! empty( $type ) ) {
  1302. $type_name = '';
  1303. $items = array();
  1304. if ( 'post_type' === $type ) {
  1305. $type_name = esc_html__( 'Post Type', 'wpforms-lite' );
  1306. $items = get_post_types(
  1307. array(
  1308. 'public' => true,
  1309. ),
  1310. 'objects'
  1311. );
  1312. unset( $items['attachment'] );
  1313. } elseif ( 'taxonomy' === $type ) {
  1314. $type_name = esc_html__( 'Taxonomy', 'wpforms-lite' );
  1315. $items = get_taxonomies(
  1316. array(
  1317. 'public' => true,
  1318. ),
  1319. 'objects'
  1320. );
  1321. unset( $items['post_format'] );
  1322. }
  1323. /* translators: %s - dynamic source type name. */
  1324. $tooltip = sprintf( esc_html__( 'Select %s to use for auto-populating field choices.', 'wpforms-lite' ), esc_html( $type_name ) );
  1325. /* translators: %s - dynamic source type name. */
  1326. $label = sprintf( esc_html__( 'Dynamic %s Source', 'wpforms-lite' ), esc_html( $type_name ) );
  1327. $options = array();
  1328. $source = ! empty( $field[ 'dynamic_' . $type ] ) ? esc_attr( $field[ 'dynamic_' . $type ] ) : '';
  1329. foreach ( $items as $key => $item ) {
  1330. $options[ $key ] = esc_html( $item->labels->name );
  1331. }
  1332. // Field option label.
  1333. $option_label = $this->field_element(
  1334. 'label',
  1335. $field,
  1336. array(
  1337. 'slug' => 'dynamic_' . $type,
  1338. 'value' => $label,
  1339. 'tooltip' => $tooltip,
  1340. ),
  1341. false
  1342. );
  1343. // Field option select input.
  1344. $option_input = $this->field_element(
  1345. 'select',
  1346. $field,
  1347. array(
  1348. 'slug' => 'dynamic_' . $type,
  1349. 'options' => $options,
  1350. 'value' => $source,
  1351. ),
  1352. false
  1353. );
  1354. // Field option row (markup) including label and input.
  1355. $output = $this->field_element(
  1356. 'row',
  1357. $field,
  1358. array(
  1359. 'slug' => 'dynamic_' . $type,
  1360. 'content' => $option_label . $option_input,
  1361. ),
  1362. false
  1363. );
  1364. } // End if().
  1365. break;
  1366. }
  1367. if ( ! $echo ) {
  1368. return $output;
  1369. }
  1370. if ( ! in_array( $option, [ 'basic-options', 'advanced-options' ], true ) ) {
  1371. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1372. echo $output;
  1373. return;
  1374. }
  1375. if ( $markup === 'open' ) {
  1376. do_action( "wpforms_field_options_before_{$option}", $field, $this );
  1377. }
  1378. if ( $markup === 'close' ) {
  1379. do_action( "wpforms_field_options_bottom_{$option}", $field, $this );
  1380. }
  1381. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1382. echo $output;
  1383. if ( $markup === 'open' ) {
  1384. do_action( "wpforms_field_options_top_{$option}", $field, $this );
  1385. }
  1386. if ( $markup === 'close' ) {
  1387. do_action( "wpforms_field_options_after_{$option}", $field, $this );
  1388. }
  1389. }
  1390. /**
  1391. * Helper function to create common field options that are used frequently
  1392. * in the field preview.
  1393. *
  1394. * @since 1.0.0
  1395. * @since 1.5.0 Added support for <select> HTML tag for choices.
  1396. * @since 1.6.1 Added multiple select support.
  1397. *
  1398. * @param string $option Field option to render.
  1399. * @param array $field Field data and settings.
  1400. * @param array $args Field preview arguments.
  1401. * @param bool $echo Print or return the value. Print by default.
  1402. *
  1403. * @return mixed Print or return a string.
  1404. */
  1405. public function field_preview_option( $option, $field, $args = array(), $echo = true ) {
  1406. $output = '';
  1407. $class = ! empty( $args['class'] ) ? wpforms_sanitize_classes( $args['class'] ) : '';
  1408. $allowed_tags = wpforms_builder_preview_get_allowed_tags();
  1409. switch ( $option ) {
  1410. case 'label':
  1411. $label = isset( $field['label'] ) && ! empty( $field['label'] ) ? esc_html( $field['label'] ) : esc_html__( 'Empty Label', 'wpforms-lite' );
  1412. $label_hidden = esc_html__( 'Label Hidden', 'wpforms-lite' );
  1413. $label_empty = esc_html__( 'To ensure your form is accessible, every field should have a descriptive label. If you\'d like to hide the label, you can do so by enabling Hide Label in the Advanced Field Options tab.', 'wpforms-lite' );
  1414. $output = sprintf(
  1415. '<label class="label-title %s"><span class="hidden_text" title="%s"><i class="fa fa-eye-slash"></i></span><span class="empty_text" title="%s"><i class="fa fa-exclamation-triangle"></i></span><span class="text">%s</span><span class="required">*</span></label>',
  1416. $class,
  1417. $label_hidden,
  1418. $label_empty,
  1419. $label
  1420. );
  1421. break;
  1422. case 'description':
  1423. $description = isset( $field['description'] ) && ! empty( $field['description'] ) ? wp_kses( $field['description'], $allowed_tags ) : '';
  1424. $description = strpos( $class, 'nl2br' ) !== false ? nl2br( $description ) : $description;
  1425. $output = sprintf( '<div class="description %s">%s</div>', $class, $description );
  1426. break;
  1427. case 'choices':
  1428. $fields_w_choices = [ 'checkbox', 'gdpr-checkbox', 'select', 'payment-select', 'radio', 'payment-multiple', 'payment-checkbox' ];
  1429. $values = ! empty( $field['choices'] ) ? $field['choices'] : $this->defaults;
  1430. $dynamic = ! empty( $field['dynamic_choices'] ) ? $field['dynamic_choices'] : false;
  1431. $total = count( $values );
  1432. $values = array_slice( $values, 0, 20 );
  1433. /*
  1434. * Check to see if this field is configured for Dynamic Choices,
  1435. * either auto populating from a post type or a taxonomy.
  1436. */
  1437. if ( ! empty( $field['dynamic_post_type'] ) || ! empty( $field['dynamic_taxonomy'] ) ) {
  1438. switch ( $dynamic ) {
  1439. case 'post_type':
  1440. // Post type dynamic populating.
  1441. $total_obj = wp_count_posts( $field['dynamic_post_type'] );
  1442. $total = isset( $total_obj->publish ) ? (int) $total_obj->publish : 0;
  1443. $values = [];
  1444. $posts = wpforms_get_hierarchical_object(
  1445. apply_filters(
  1446. 'wpforms_dynamic_choice_post_type_args',
  1447. [
  1448. 'post_type' => $field['dynamic_post_type'],
  1449. 'posts_per_page' => 20,
  1450. 'orderby' => 'title',
  1451. 'order' => 'ASC',
  1452. ],
  1453. $field,
  1454. $this->form_id
  1455. ),
  1456. true
  1457. );
  1458. foreach ( $posts as $post ) {
  1459. $values[] = [
  1460. 'label' => $post->post_title,
  1461. ];
  1462. }
  1463. break;
  1464. case 'taxonomy':
  1465. // Taxonomy dynamic populating.
  1466. $total = (int) wp_count_terms( $field['dynamic_taxonomy'] );
  1467. $values = [];
  1468. $terms = wpforms_get_hierarchical_object(
  1469. apply_filters(
  1470. 'wpforms_dynamic_choice_taxonomy_args',
  1471. [
  1472. 'taxonomy' => $field['dynamic_taxonomy'],
  1473. 'hide_empty' => false,
  1474. 'number' => 20,
  1475. ],
  1476. $field,
  1477. $this->form_id
  1478. ),
  1479. true
  1480. );
  1481. foreach ( $terms as $term ) {
  1482. $values[] = [
  1483. 'label' => $term->name,
  1484. ];
  1485. }
  1486. break;
  1487. }
  1488. }
  1489. // Notify if dynamic choices source is currently empty.
  1490. if ( empty( $values ) ) {
  1491. $values = [
  1492. [
  1493. 'label' => esc_html__( '(empty)', 'wpforms-lite' ),
  1494. ],
  1495. ];
  1496. }
  1497. // Build output.
  1498. if ( ! in_array( $field['type'], $fields_w_choices, true ) ) {
  1499. break;
  1500. }
  1501. switch ( $field['type'] ) {
  1502. case 'checkbox':
  1503. case 'gdpr-checkbox':
  1504. case 'payment-checkbox':
  1505. $type = 'checkbox';
  1506. break;
  1507. case 'select':
  1508. case 'payment-select':
  1509. $type = 'select';
  1510. break;
  1511. default:
  1512. $type = 'radio';
  1513. break;
  1514. }
  1515. $list_class = [ 'primary-input' ];
  1516. $with_images = empty( $field['dynamic_choices'] ) && ! empty( $field['choices_images'] );
  1517. if ( $with_images ) {
  1518. $list_class[] = 'wpforms-image-choices';
  1519. $list_class[] = 'wpforms-image-choices-' . sanitize_html_class( $field['choices_images_style'] );
  1520. }
  1521. if ( ! empty( $class ) ) {
  1522. $list_class[] = $class;
  1523. }
  1524. // Special rules for <select>-based fields.
  1525. if ( $type === 'select' ) {
  1526. $multiple = ! empty( $field['multiple'] ) ? ' multiple' : '';
  1527. $placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : '';
  1528. $output = sprintf(
  1529. '<select class="%s"%s readonly>',
  1530. wpforms_sanitize_classes( $list_class, true ),
  1531. $multiple
  1532. );
  1533. // Optional placeholder.
  1534. if ( ! empty( $placeholder ) ) {
  1535. $output .= sprintf(
  1536. '<option value="" class="placeholder">%s</option>',
  1537. esc_html( $placeholder )
  1538. );
  1539. }
  1540. // Build the select options.
  1541. foreach ( $values as $key => $value ) {
  1542. $default = isset( $value['default'] ) ? (bool) $value['default'] : false;
  1543. $selected = ! empty( $placeholder ) && empty( $multiple ) ? '' : selected( true, $default, false );
  1544. $label = isset( $value['label'] ) ? trim( $value['label'] ) : '';
  1545. /* translators: %d - Choice item number. */
  1546. $label = $label !== '' ? $label : sprintf( esc_html__( 'Choice %d', 'wpforms-lite' ), (int) $key );
  1547. $label .= ! empty( $field['show_price_after_labels'] ) && isset( $value['value'] ) ? ' - ' . wpforms_format_amount( wpforms_sanitize_amount( $value['value'] ), true ) : '';
  1548. $output .= sprintf(
  1549. '<option value="%2$s" %1$s>%2$s</option>',
  1550. $selected,
  1551. esc_html( $label )
  1552. );
  1553. }
  1554. $output .= '</select>';
  1555. } else {
  1556. // Normal checkbox/radio-based fields.
  1557. $output = sprintf(
  1558. '<ul class="%s">',
  1559. wpforms_sanitize_classes( $list_class, true )
  1560. );
  1561. foreach ( $values as $key => $value ) {
  1562. $default = isset( $value['default'] ) ? $value['default'] : '';
  1563. $selected = checked( '1', $default, false );
  1564. $input_class = [];
  1565. $item_class = [];
  1566. if ( ! empty( $value['default'] ) ) {
  1567. $item_class[] = 'wpforms-selected';
  1568. }
  1569. if ( $with_images ) {
  1570. $item_class[] = 'wpforms-image-choices-item';
  1571. }
  1572. $output .= sprintf(
  1573. '<li class="%s">',
  1574. wpforms_sanitize_classes( $item_class, true )
  1575. );
  1576. $label = isset( $value['label'] ) ? trim( $value['label'] ) : '';
  1577. /* translators: %d - Choice item number. */
  1578. $label = $label !== '' ? $label : sprintf( esc_html__( 'Choice %d', 'wpforms-lite' ), (int) $key );
  1579. $label .= ! empty( $field['show_price_after_labels'] ) && isset( $value['value'] ) ? ' - ' . wpforms_format_amount( wpforms_sanitize_amount( $value['value'] ), true ) : '';
  1580. if ( $with_images ) {
  1581. if ( in_array( $field['choices_images_style'], [ 'modern', 'classic' ], true ) ) {
  1582. $input_class[] = 'wpforms-screen-reader-element';
  1583. }
  1584. $output .= '<label>';
  1585. $output .= sprintf(
  1586. '<span class="wpforms-image-choices-image"><img src="%s" alt="%s"%s></span>',
  1587. ! empty( $value['image'] ) ? esc_url( $value['image'] ) : WPFORMS_PLUGIN_URL . 'assets/images/builder/placeholder-200x125.svg',
  1588. esc_attr( $value['label'] ),
  1589. ! empty( $value['label'] ) ? ' title="' . esc_attr( $value['label'] ) . '"' : ''
  1590. );
  1591. if ( $field['choices_images_style'] === 'none' ) {
  1592. $output .= '<br>';
  1593. }
  1594. $output .= sprintf(
  1595. '<input type="%s" class="%s" %s readonly>',
  1596. $type,
  1597. wpforms_sanitize_classes( $input_class, true ),
  1598. $selected
  1599. );
  1600. $output .= '<span class="wpforms-image-choices-label">' . wp_kses( $label, $allowed_tags ) . '</span>';
  1601. $output .= '</label>';
  1602. } else {
  1603. $output .= sprintf(
  1604. '<input type="%s" %s readonly> %s',
  1605. $type,
  1606. $selected,
  1607. wp_kses( $label, $allowed_tags )
  1608. );
  1609. }
  1610. $output .= '</li>';
  1611. }
  1612. $output .= '</ul>';
  1613. /*
  1614. * Contains more than 20 items, include a note about a limited subset of results displayed.
  1615. */
  1616. if ( $total > 20 ) {
  1617. $output .= '<div class="wpforms-alert-dynamic wpforms-alert wpforms-alert-warning">';
  1618. $output .= sprintf(
  1619. wp_kses( /* translators: %s - total amount of choices. */
  1620. __( 'Showing the first 20 choices.<br> All %s choices will be displayed when viewing the form.', 'wpforms-lite' ),
  1621. [
  1622. 'br' => [],
  1623. ]
  1624. ),
  1625. $total
  1626. );
  1627. $output .= '</div>';
  1628. }
  1629. }
  1630. break;
  1631. }
  1632. if ( ! $echo ) {
  1633. return $output;
  1634. }
  1635. echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  1636. }
  1637. /**
  1638. * Create a new field in the admin AJAX editor.
  1639. *
  1640. * @since 1.0.0
  1641. */
  1642. public function field_new() {
  1643. // Run a security check.
  1644. check_ajax_referer( 'wpforms-builder', 'nonce' );
  1645. // Check for permissions.
  1646. if ( ! wpforms_current_user_can( 'edit_forms' ) ) {
  1647. die( esc_html__( 'You do not have permission.', 'wpforms-lite' ) );
  1648. }
  1649. // Check for form ID.
  1650. if ( ! isset( $_POST['id'] ) || empty( $_POST['id'] ) ) {
  1651. die( esc_html__( 'No form ID found', 'wpforms-lite' ) );
  1652. }
  1653. // Check for field type to add.
  1654. if ( ! isset( $_POST['type'] ) || empty( $_POST['type'] ) ) {
  1655. die( esc_html__( 'No field type found', 'wpforms-lite' ) );
  1656. }
  1657. // Grab field data.
  1658. $field_args = ! empty( $_POST['defaults'] ) && is_array( $_POST['defaults'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['defaults'] ) ) : [];
  1659. $field_type = sanitize_key( $_POST['type'] );
  1660. $field_id = wpforms()->get( 'form' )->next_field_id( absint( $_POST['id'] ) );
  1661. $field = [
  1662. 'id' => $field_id,
  1663. 'type' => $field_type,
  1664. 'label' => $this->name,
  1665. 'description' => '',
  1666. ];
  1667. $field = wp_parse_args( $field_args, $field );
  1668. $field = apply_filters( 'wpforms_field_new_default', $field );
  1669. $field_required = apply_filters( 'wpforms_field_new_required', '', $field );
  1670. $field_class = apply_filters( 'wpforms_field_new_class', '', $field );
  1671. $field_helper_hide = ! empty( $_COOKIE['wpforms_field_helper_hide'] );
  1672. // Field types that default to required.
  1673. if ( ! empty( $field_required ) ) {
  1674. $field_required = 'required';
  1675. $field['required'] = '1';
  1676. }
  1677. // Build Preview.
  1678. ob_start();
  1679. $this->field_preview( $field );
  1680. $prev = ob_get_clean();
  1681. $preview = sprintf(
  1682. '<div class="wpforms-field wpforms-field-%1$s %2$s %3$s" id="wpforms-field-%4$d" data-field-id="%4$d" data-field-type="%5$s">',
  1683. esc_attr( $field_type ),
  1684. esc_attr( $field_required ),
  1685. esc_attr( $field_class ),
  1686. absint( $field['id'] ),
  1687. esc_attr( $field_type )
  1688. );
  1689. if ( apply_filters( 'wpforms_field_new_display_duplicate_button', true, $field ) ) {
  1690. $preview .= sprintf( '<a href="#" class="wpforms-field-duplicate" title="%s"><i class="fa fa-files-o" aria-hidden="true"></i></a>', esc_attr__( 'Duplicate Field', 'wpforms-lite' ) );
  1691. }
  1692. $preview .= sprintf( '<a href="#" class="wpforms-field-delete" title="%s"><i class="fa fa-trash-o"></i></a>', esc_attr__( 'Delete Field', 'wpforms-lite' ) );
  1693. if ( ! $field_helper_hide ) {
  1694. $preview .= sprintf(
  1695. // language=HTML PhpStorm.
  1696. '<div class="wpforms-field-helper">
  1697. <span class="wpforms-field-helper-edit">%s</span>
  1698. <span class="wpforms-field-helper-drag">%s</span>
  1699. <span class="wpforms-field-helper-hide" title="%s">
  1700. <i class="fa fa-times-circle" aria-hidden="true"></i>
  1701. </span>
  1702. </div>',
  1703. esc_html__( 'Click to Edit', 'wpforms-lite' ),
  1704. esc_html__( 'Drag to Reorder', 'wpforms-lite' ),
  1705. esc_html__( 'Hide Helper', 'wpforms-lite' )
  1706. );
  1707. }
  1708. $preview .= $prev;
  1709. $preview .= '</div>';
  1710. // Build Options.
  1711. $class = apply_filters( 'wpforms_builder_field_option_class', '', $field );
  1712. $options = sprintf(
  1713. '<div class="wpforms-field-option wpforms-field-option-%1$s %2$s" id="wpforms-field-option-%3$d" data-field-id="%3$d">',
  1714. sanitize_html_class( $field['type'] ),
  1715. wpforms_sanitize_classes( $class ),
  1716. absint( $field['id'] )
  1717. );
  1718. $options .= sprintf(
  1719. '<input type="hidden" name="fields[%1$d][id]" value="%1$d" class="wpforms-field-option-hidden-id">',
  1720. absint( $field['id'] )
  1721. );
  1722. $options .= sprintf(
  1723. '<input type="hidden" name="fields[%d][type]" value="%s" class="wpforms-field-option-hidden-type">',
  1724. absint( $field['id'] ),
  1725. esc_attr( $field['type'] )
  1726. );
  1727. ob_start();
  1728. $this->field_options( $field );
  1729. $options .= ob_get_clean();
  1730. $options .= '</div>';
  1731. // Prepare to return compiled results.
  1732. wp_send_json_success(
  1733. [
  1734. 'form_id' => absint( $_POST['id'] ),
  1735. 'field' => $field,
  1736. 'preview' => $preview,
  1737. 'options' => $options,
  1738. ]
  1739. );
  1740. }
  1741. /**
  1742. * Display the field input elements on the frontend.
  1743. *
  1744. * @since 1.0.0
  1745. * @since 1.5.0 Converted to abstract method, as it's required for all fields.
  1746. *
  1747. * @param array $field Field data and settings.
  1748. * @param array $field_atts Field attributes.
  1749. * @param array $form_data Form data and settings.
  1750. */
  1751. abstract public function field_display( $field, $field_atts, $form_data );
  1752. /**
  1753. * Display field input errors if present.
  1754. *
  1755. * @since 1.3.7
  1756. *
  1757. * @param string $key Input key.
  1758. * @param array $field Field data and settings.
  1759. */
  1760. public function field_display_error( $key, $field ) {
  1761. // Need an error.
  1762. if ( empty( $field['properties']['error']['value'][ $key ] ) ) {
  1763. return;
  1764. }
  1765. printf(
  1766. '<label class="wpforms-error" for="%s">%s</label>',
  1767. esc_attr( $field['properties']['inputs'][ $key ]['id'] ),
  1768. esc_html( $field['properties']['error']['value'][ $key ] )
  1769. );
  1770. }
  1771. /**
  1772. * Display field input sublabel if present.
  1773. *
  1774. * @since 1.3.7
  1775. *
  1776. * @param string $key Input key.
  1777. * @param string $position Sublabel position.
  1778. * @param array $field Field data and settings.
  1779. */
  1780. public function field_display_sublabel( $key, $position, $field ) {
  1781. // Need a sublabel value.
  1782. if ( empty( $field['properties']['inputs'][ $key ]['sublabel']['value'] ) ) {
  1783. return;
  1784. }
  1785. $pos = ! empty( $field['properties']['inputs'][ $key ]['sublabel']['position'] ) ? $field['properties']['inputs'][ $key ]['sublabel']['position'] : 'after';
  1786. $hidden = ! empty( $field['properties']['inputs'][ $key ]['sublabel']['hidden'] ) ? 'wpforms-sublabel-hide' : '';
  1787. if ( $pos !== $position ) {
  1788. return;
  1789. }
  1790. printf(
  1791. '<label for="%s" class="wpforms-field-sublabel %s %s">%s</label>',
  1792. esc_attr( $field['properties']['inputs'][ $key ]['id'] ),
  1793. sanitize_html_class( $pos ),
  1794. $hidden,
  1795. $field['properties']['inputs'][ $key ]['sublabel']['value']
  1796. );
  1797. }
  1798. /**
  1799. * Validate field on form submit.
  1800. *
  1801. * @since 1.0.0
  1802. *
  1803. * @param int $field_id Field ID.
  1804. * @param mixed $field_submit Field value that was submitted.
  1805. * @param array $form_data Form data and settings.
  1806. */
  1807. public function validate( $field_id, $field_submit, $form_data ) {
  1808. // Basic required check - If field is marked as required, check for entry data.
  1809. if ( ! empty( $form_data['fields'][ $field_id ]['required'] ) && empty( $field_submit ) && '0' !== (string) $field_submit ) {
  1810. wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = wpforms_get_required_label();
  1811. }
  1812. }
  1813. /**
  1814. * Format and sanitize field.
  1815. *
  1816. * @since 1.0.0
  1817. *
  1818. * @param int $field_id Field ID.
  1819. * @param mixed $field_submit Field value that was submitted.
  1820. * @param array $form_data Form data and settings.
  1821. */
  1822. public function format( $field_id, $field_submit, $form_data ) {
  1823. if ( is_array( $field_submit ) ) {
  1824. $field_submit = array_filter( $field_submit );
  1825. $field_submit = implode( "\r\n", $field_submit );
  1826. }
  1827. $name = ! empty( $form_data['fields'][ $field_id ]['label'] ) ? sanitize_text_field( $form_data['fields'][ $field_id ]['label'] ) : '';
  1828. // Sanitize but keep line breaks.
  1829. $value = wpforms_sanitize_textarea_field( $field_submit );
  1830. wpforms()->process->fields[ $field_id ] = array(
  1831. 'name' => $name,
  1832. 'value' => $value,
  1833. 'id' => absint( $field_id ),
  1834. 'type' => $this->type,
  1835. );
  1836. }
  1837. /**
  1838. * Get field name for ajax error message.
  1839. *
  1840. * @since 1.6.3
  1841. *
  1842. * @param string $name Field name for error triggered.
  1843. * @param array $field Field settings.
  1844. * @param array $props List of properties.
  1845. * @param string $error Error message.
  1846. *
  1847. * @return string
  1848. */
  1849. public function ajax_error_field_name( $name, $field, $props, $error ) {
  1850. if ( $name ) {
  1851. return $name;
  1852. }
  1853. $input = isset( $props['inputs']['primary'] ) ? $props['inputs']['primary'] : end( $props['inputs'] );
  1854. return (string) isset( $input['attr']['name'] ) ? $input['attr']['name'] : '';
  1855. }
  1856. /**
  1857. * Enqueue Choicesjs script and config.
  1858. *
  1859. * @param array $forms Forms on the current page.
  1860. *
  1861. * @since 1.6.3
  1862. */
  1863. protected function enqueue_choicesjs_once( $forms ) {
  1864. if ( wpforms()->frontend->is_choicesjs_enqueued ) {
  1865. return;
  1866. }
  1867. wp_enqueue_script(
  1868. 'wpforms-choicesjs',
  1869. WPFORMS_PLUGIN_URL . 'assets/js/choices.min.js',
  1870. array(),
  1871. '9.0.1',
  1872. true
  1873. );
  1874. $config = [
  1875. 'removeItemButton' => true,
  1876. 'shouldSort' => false,
  1877. 'loadingText' => esc_html__( 'Loading...', 'wpforms-lite' ),
  1878. 'noResultsText' => esc_html__( 'No results found.', 'wpforms-lite' ),
  1879. 'noChoicesText' => esc_html__( 'No choices to choose from.', 'wpforms-lite' ),
  1880. 'itemSelectText' => esc_attr__( 'Press to select.', 'wpforms-lite' ),
  1881. 'uniqueItemText' => esc_html__( 'Only unique values can be added.', 'wpforms-lite' ),
  1882. 'customAddItemText' => esc_html__( 'Only values matching specific conditions can be added.', 'wpforms-lite' ),
  1883. ];
  1884. // Allow theme/plugin developers to modify the provided or add own Choices.js settings.
  1885. $config = apply_filters( 'wpforms_field_select_choicesjs_config', $config, $forms, $this );
  1886. wp_localize_script(
  1887. 'wpforms-choicesjs',
  1888. 'wpforms_choicesjs_config',
  1889. $config
  1890. );
  1891. wpforms()->frontend->is_choicesjs_enqueued = true;
  1892. }
  1893. /**
  1894. * Whether a Choicesjs search area should be shown.
  1895. *
  1896. * @since 1.6.4
  1897. *
  1898. * @param int $choices_count Choices amount.
  1899. *
  1900. * @return bool
  1901. */
  1902. protected function is_choicesjs_search_enabled( $choices_count ) {
  1903. // We should auto hide/remove search, if less than 8 choices.
  1904. return $choices_count >= (int) apply_filters( 'wpforms_field_choicesjs_search_enabled_items_min', 8 );
  1905. }
  1906. }