Form.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Database_Models_Form
  4. */
  5. final class NF_Database_Models_Form extends NF_Abstracts_Model
  6. {
  7. protected $_type = 'form';
  8. protected $_table_name = 'nf3_forms';
  9. protected $_meta_table_name = 'nf3_form_meta';
  10. protected $_columns = array(
  11. 'title',
  12. 'created_at',
  13. 'form_title',
  14. 'default_label_pos',
  15. 'show_title',
  16. 'clear_complete',
  17. 'hide_complete',
  18. 'logged_in',
  19. 'seq_num'
  20. );
  21. protected $_fields;
  22. protected static $imported_form_id;
  23. public function __construct( $db, $id = '' )
  24. {
  25. add_action( 'ninja_forms_before_import_form', array( $this, 'import_form_backwards_compatibility' ) );
  26. parent::__construct( $db, $id );
  27. }
  28. public function delete()
  29. {
  30. parent::delete();
  31. $fields = Ninja_Forms()->form( $this->_id )->get_fields();
  32. foreach( $fields as $field ){
  33. $field->delete();
  34. }
  35. $actions = Ninja_Forms()->form( $this->_id )->get_actions();
  36. foreach( $actions as $action ){
  37. $action->delete();
  38. }
  39. $chunked_option_flag = 'nf_form_' . $this->_id . '_chunks';
  40. $chunked_option_value = get_option( $chunked_option_flag );
  41. // if there is nf_form_x_chunks option, we need to delete those
  42. if( $chunked_option_value ) {
  43. // if we have chunk'd it, get the list of chunks
  44. $form_chunks = explode( ',', $chunked_option_value );
  45. //get the option value of each chunk and concat them into the form
  46. foreach( $form_chunks as $chunk ){
  47. delete_option( $chunk );
  48. }
  49. delete_option( $chunked_option_flag );
  50. }
  51. $this->delete_submissions();
  52. WPN_Helper::delete_nf_cache( $this->_id );
  53. }
  54. private function delete_submissions( ) {
  55. global $wpdb;
  56. $total_subs_deleted = 0;
  57. $post_result = 0;
  58. $max_cnt = 250;
  59. // SQL for getting 250 subs at a time
  60. $sub_sql = "SELECT id FROM `" . $wpdb->prefix . "posts` AS p
  61. LEFT JOIN `" . $wpdb->prefix . "postmeta` AS m ON p.id = m.post_id
  62. WHERE p.post_type = 'nf_sub' AND m.meta_key = '_form_id'
  63. AND m.meta_value = %s LIMIT " . $max_cnt;
  64. while ($post_result <= $max_cnt ) {
  65. $subs = $wpdb->get_col( $wpdb->prepare( $sub_sql, $this->_id ),0 );
  66. // if we are out of subs, then stop
  67. if( 0 === count( $subs ) ) break;
  68. // otherwise, let's delete the postmeta as well
  69. $delete_meta_query = "DELETE FROM `" . $wpdb->prefix . "postmeta` WHERE post_id IN ( [IN] )";
  70. $delete_meta_query = $this->prepare_in( $delete_meta_query, $subs );
  71. $meta_result = $wpdb->query( $delete_meta_query );
  72. if ( $meta_result > 0 ) {
  73. // now we actually delete the posts(nf_sub)
  74. $delete_post_query = "DELETE FROM `" . $wpdb->prefix . "posts` WHERE id IN ( [IN] )";
  75. $delete_post_query = $this->prepare_in( $delete_post_query, $subs );
  76. $post_result = $wpdb->query( $delete_post_query );
  77. $total_subs_deleted = $total_subs_deleted + $post_result;
  78. }
  79. }
  80. }
  81. private function prepare_in( $sql, $vals ) {
  82. global $wpdb;
  83. $not_in_count = substr_count( $sql, '[IN]' );
  84. if ( $not_in_count > 0 ) {
  85. $args = array( str_replace( '[IN]', implode( ', ', array_fill( 0, count( $vals ), '%d' ) ), str_replace( '%', '%%', $sql ) ) );
  86. // This will populate ALL the [IN]'s with the $vals, assuming you have more than one [IN] in the sql
  87. for ( $i=0; $i < substr_count( $sql, '[IN]' ); $i++ ) {
  88. $args = array_merge( $args, $vals );
  89. }
  90. $sql = call_user_func_array( array( $wpdb, 'prepare' ), array_merge( $args ) );
  91. }
  92. return $sql;
  93. }
  94. public static function get_next_sub_seq( $form_id )
  95. {
  96. global $wpdb;
  97. // TODO: Leverage form cache.
  98. $last_seq_num = $wpdb->get_var( $wpdb->prepare(
  99. 'SELECT value FROM ' . $wpdb->prefix . 'nf3_form_meta WHERE `key` = "_seq_num" AND `parent_id` = %s'
  100. , $form_id ) );
  101. if( $last_seq_num ) {
  102. $wpdb->update( $wpdb->prefix . 'nf3_form_meta', array( 'value' => $last_seq_num + 1,
  103. 'meta_value' => $last_seq_num + 1, 'meta_key' => '_seq_num' )
  104. , array( 'key' => '_seq_num', 'parent_id'
  105. => $form_id ) );
  106. $wpdb->update( $wpdb->prefix . 'nf3_forms', array( 'seq_num' => $last_seq_num + 1 ), array( 'id' => $form_id ) );
  107. } else {
  108. $last_seq_num = 1;
  109. $wpdb->insert( $wpdb->prefix . 'nf3_form_meta',
  110. array( 'key' => '_seq_num',
  111. 'value' => $last_seq_num + 1,
  112. 'parent_id' => $form_id,
  113. 'meta_key' => '_seq_num',
  114. 'meta_value' => $last_seq_num + 1
  115. ) );
  116. $wpdb->update( $wpdb->prefix . 'nf3_forms', array( 'seq_num' => $last_seq_num + 1 ), array( 'id' => $form_id ) );
  117. }
  118. return $last_seq_num;
  119. }
  120. public static function import( array $import, $id = '', $is_conversion )
  121. {
  122. $import = apply_filters( 'ninja_forms_before_import_form', $import );
  123. /*
  124. * Create Form
  125. */
  126. $form = Ninja_Forms()->form( $id )->get();
  127. $form->update_settings( $import[ 'settings' ] );
  128. if( ! $is_conversion ) {
  129. $form->update_setting( 'created_at', current_time( 'mysql' ) );
  130. }
  131. $form->save();
  132. $form_id = $form->get_id();
  133. $form_cache = array(
  134. 'id' => $form_id,
  135. 'fields' => array(),
  136. 'actions' => array(),
  137. 'settings' => $form->get_settings()
  138. );
  139. $update_process = Ninja_Forms()->background_process( 'update-fields' );
  140. foreach( $import[ 'fields' ] as $settings ){
  141. if( $is_conversion ) {
  142. $field_id = $settings[ 'id' ];
  143. $field = Ninja_Forms()->form($form_id)->field( $field_id )->get();
  144. $field->save();
  145. } else {
  146. unset( $settings[ 'id' ] );
  147. $settings[ 'created_at' ] = current_time( 'mysql' );
  148. $field = Ninja_Forms()->form($form_id)->field()->get();
  149. $field->save();
  150. }
  151. $settings[ 'parent_id' ] = $form_id;
  152. array_push( $form_cache[ 'fields' ], array(
  153. 'id' => $field->get_id(),
  154. 'settings' => $settings
  155. ));
  156. $update_process->push_to_queue(array(
  157. 'id' => $field->get_id(),
  158. 'settings' => $settings
  159. ));
  160. }
  161. $update_process->save()->dispatch();
  162. foreach( $import[ 'actions' ] as $settings ){
  163. $action = Ninja_Forms()->form($form_id)->action()->get();
  164. if( ! $is_conversion ) {
  165. $settings[ 'created_at' ] = current_time( 'mysql' );
  166. }
  167. $action->update_settings( $settings )->save();
  168. array_push( $form_cache[ 'actions' ], array(
  169. 'id' => $action->get_id(),
  170. 'settings' => $settings
  171. ));
  172. }
  173. WPN_Helper::update_nf_cache( $form_id, $form_cache );
  174. add_action( 'admin_notices', array( 'NF_Database_Models_Form', 'import_admin_notice' ) );
  175. self::$imported_form_id = $form_id;
  176. return $form_id;
  177. }
  178. public static function import_admin_notice()
  179. {
  180. Ninja_Forms()->template( 'admin-notice-form-import.html.php', array( 'form_id'=> self::$imported_form_id ) );
  181. }
  182. public static function duplicate( $form_id )
  183. {
  184. global $wpdb;
  185. // Duplicate the Form Object.
  186. $wpdb->query( $wpdb->prepare(
  187. "
  188. INSERT INTO {$wpdb->prefix}nf3_forms ( `title` )
  189. SELECT CONCAT( `title`, ' - ', %s )
  190. FROM {$wpdb->prefix}nf3_forms
  191. WHERE id = %d;
  192. ", __( 'copy', 'ninja-forms' ), $form_id
  193. ) );
  194. $new_form_id = $wpdb->insert_id;
  195. // Duplicate the Form Meta.
  196. $wpdb->query( $wpdb->prepare(
  197. "
  198. INSERT INTO {$wpdb->prefix}nf3_form_meta ( `parent_id`, `key`, `value` )
  199. SELECT %d, `key`, `value`
  200. FROM {$wpdb->prefix}nf3_form_meta
  201. WHERE parent_id = %d
  202. AND `key` != '_seq_num';
  203. ", $new_form_id, $form_id
  204. ));
  205. // Get the fields to duplicate
  206. $old_fields = $wpdb->get_results( $wpdb->prepare(
  207. "
  208. SELECT `id`
  209. FROM {$wpdb->prefix}nf3_fields
  210. WHERE parent_id = %d
  211. ", $form_id
  212. ));
  213. foreach( $old_fields as $old_field ){
  214. // Duplicate the Field Object.
  215. $wpdb->query( $wpdb->prepare(
  216. "
  217. INSERT INTO {$wpdb->prefix}nf3_fields ( `label`, `key`, `type`, `parent_id` )
  218. SELECT `label`, `key`, `type`, %d
  219. FROM {$wpdb->prefix}nf3_fields
  220. WHERE id = %d
  221. ", $new_form_id, $old_field->id
  222. ));
  223. $new_field_id = $wpdb->insert_id;
  224. // Duplicate the Field Meta.
  225. $wpdb->query( $wpdb->prepare(
  226. "
  227. INSERT INTO {$wpdb->prefix}nf3_field_meta ( `parent_id`, `key`, `value` )
  228. SELECT %d, `key`, `value`
  229. FROM {$wpdb->prefix}nf3_field_meta
  230. WHERE parent_id = %d;
  231. ", $new_field_id, $old_field->id
  232. ));
  233. }
  234. // Duplicate the Actions.
  235. // Get the actions to duplicate
  236. $old_actions = $wpdb->get_results( $wpdb->prepare(
  237. "
  238. SELECT `id`
  239. FROM {$wpdb->prefix}nf3_actions
  240. WHERE parent_id = %d
  241. ", $form_id
  242. ));
  243. foreach( $old_actions as $old_action ){
  244. // Duplicate the Action Object.
  245. $wpdb->query( $wpdb->prepare(
  246. "
  247. INSERT INTO {$wpdb->prefix}nf3_actions ( `title`, `key`, `type`, `active`, `parent_id` )
  248. SELECT `title`, `key`, `type`, `active`, %d
  249. FROM {$wpdb->prefix}nf3_actions
  250. WHERE id = %d
  251. ", $new_form_id, $old_action->id
  252. ));
  253. $new_action_id = $wpdb->insert_id;
  254. // Duplicate the Action Meta.
  255. $wpdb->query( $wpdb->prepare(
  256. "
  257. INSERT INTO {$wpdb->prefix}nf3_action_meta ( `parent_id`, `key`, `value` )
  258. SELECT %d, `key`, `value`
  259. FROM {$wpdb->prefix}nf3_action_meta
  260. WHERE parent_id = %d;
  261. ", $new_action_id, $old_action->id
  262. ));
  263. }
  264. /*
  265. * In order for our new form and form_meta fields to populate on
  266. * duplicate we need to update_settings and save
  267. */
  268. $new_form = Ninja_Forms()->form( $new_form_id )->get();
  269. $new_form->update_settings( $new_form->get_settings() );
  270. $new_form->save();
  271. return $new_form_id;
  272. }
  273. public static function export( $form_id, $return = FALSE )
  274. {
  275. //TODO: Set Date Format from Plugin Settings
  276. $date_format = 'm/d/Y';
  277. $form = Ninja_Forms()->form( $form_id )->get();
  278. $form_title = $form->get_setting( 'title' );
  279. $form_title = preg_replace( "/[^A-Za-z0-9 ]/", '', $form_title );
  280. $form_title = str_replace( ' ', '_', $form_title );
  281. $export = array(
  282. 'settings' => $form->get_settings(),
  283. 'fields' => array(),
  284. 'actions' => array()
  285. );
  286. $fields = Ninja_Forms()->form( $form_id )->get_fields();
  287. foreach( $fields as $field ){
  288. // If the field is set.
  289. if ( ! is_null( $field ) && ! empty( $field ) ) {
  290. $export['fields'][] = $field->get_settings();
  291. }
  292. }
  293. $actions = Ninja_Forms()->form( $form_id )->get_actions();
  294. foreach( $actions as $action ){
  295. // If the action is set.
  296. if ( ! is_null( $action ) && ! empty( $action ) ) {
  297. $export[ 'actions' ][] = $action->get_settings();
  298. }
  299. }
  300. if( $return ){
  301. return $export;
  302. } else {
  303. $today = date( $date_format, current_time( 'timestamp' ) );
  304. $filename = apply_filters( 'ninja_forms_form_export_filename', 'nf_form_' . $today . '_' . $form_title );
  305. $filename = $filename . ".nff";
  306. header( 'Content-type: application/json');
  307. header( 'Content-Disposition: attachment; filename="'.$filename .'"' );
  308. header( 'Pragma: no-cache');
  309. header( 'Expires: 0' );
  310. // echo apply_filters( 'ninja_forms_form_export_bom',"\xEF\xBB\xBF" ) ; // Byte Order Mark
  311. if( isset( $_REQUEST[ 'nf_export_form_turn_off_encoding' ] )
  312. && $_REQUEST[ 'nf_export_form_turn_off_encoding' ] ) {
  313. echo json_encode( $export );
  314. } else {
  315. echo json_encode( WPN_Helper::utf8_encode( $export ) );
  316. }
  317. die();
  318. }
  319. }
  320. /*
  321. |--------------------------------------------------------------------------
  322. | Backwards Compatibility
  323. |--------------------------------------------------------------------------
  324. */
  325. public function import_form_backwards_compatibility( $import )
  326. {
  327. // Rename `data` to `settings`
  328. if( isset( $import[ 'data' ] ) ){
  329. $import[ 'settings' ] = $import[ 'data' ];
  330. unset( $import[ 'data' ] );
  331. }
  332. // Rename `notifications` to `actions`
  333. if( isset( $import[ 'notifications' ] ) ){
  334. $import[ 'actions' ] = $import[ 'notifications' ];
  335. unset( $import[ 'notifications' ] );
  336. }
  337. // Rename `form_title` to `title`
  338. if( isset( $import[ 'settings' ][ 'form_title' ] ) ){
  339. $import[ 'settings' ][ 'title' ] = $import[ 'settings' ][ 'form_title' ];
  340. unset( $import[ 'settings' ][ 'form_title' ] );
  341. }
  342. // Convert `last_sub` to `_seq_num`
  343. if( isset( $import[ 'settings' ][ 'last_sub' ] ) ) {
  344. $import[ 'settings' ][ '_seq_num' ] = $import[ 'settings' ][ 'last_sub' ] + 1;
  345. }
  346. // Make sure
  347. if( ! isset( $import[ 'fields' ] ) ){
  348. $import[ 'fields' ] = array();
  349. }
  350. // `Field` to `Fields`
  351. if( isset( $import[ 'field' ] ) ){
  352. $import[ 'fields' ] = $import[ 'field' ];
  353. unset( $import[ 'field' ] );
  354. }
  355. $import = apply_filters( 'ninja_forms_upgrade_settings', $import );
  356. // Combine Field and Field Data
  357. foreach( $import[ 'fields' ] as $key => $field ){
  358. if( '_honeypot' == $field[ 'type' ] ) {
  359. unset( $import[ 'fields' ][ $key ] );
  360. continue;
  361. }
  362. if( ! $field[ 'type' ] ) {
  363. unset( $import[ 'fields'][ $key ] );
  364. continue;
  365. }
  366. // TODO: Split Credit Card field into multiple fields.
  367. $field = $this->import_field_backwards_compatibility( $field );
  368. if( isset( $field[ 'new_fields' ] ) ){
  369. foreach( $field[ 'new_fields' ] as $new_field ){
  370. $import[ 'fields' ][] = $new_field;
  371. }
  372. unset( $field[ 'new_fields' ] );
  373. }
  374. $import[ 'fields' ][ $key ] = $field;
  375. }
  376. $has_save_action = FALSE;
  377. foreach( $import[ 'actions' ] as $key => $action ){
  378. $action = $this->import_action_backwards_compatibility( $action );
  379. $import[ 'actions' ][ $key ] = $action;
  380. if( 'save' == $action[ 'type' ] ) $has_save_action = TRUE;
  381. }
  382. if( ! $has_save_action ) {
  383. $import[ 'actions' ][] = array(
  384. 'type' => 'save',
  385. 'label' => __( 'Save Form', 'ninja-forms' ),
  386. 'active' => TRUE
  387. );
  388. }
  389. $import = $this->import_merge_tags_backwards_compatibility( $import );
  390. return apply_filters( 'ninja_forms_after_upgrade_settings', $import );
  391. }
  392. public function import_merge_tags_backwards_compatibility( $import )
  393. {
  394. $field_lookup = array();
  395. foreach( $import[ 'fields' ] as $key => $field ){
  396. if( ! isset( $field[ 'id' ] ) ) continue;
  397. $field_id = $field[ 'id' ];
  398. $field_key = $field[ 'type' ] . '_' . $field_id;
  399. $field_lookup[ $field_id ] = $import[ 'fields' ][ $key ][ 'key' ] = $field_key;
  400. }
  401. foreach( $import[ 'actions' ] as $key => $action_settings ){
  402. foreach( $action_settings as $setting => $value ){
  403. foreach( $field_lookup as $field_id => $field_key ){
  404. // Convert Tokenizer
  405. $token = 'field_' . $field_id;
  406. if( ! is_array( $value ) ) {
  407. if (FALSE !== strpos($value, $token)) {
  408. $value = str_replace($token, '{field:' . $field_key . '}', $value);
  409. }
  410. }
  411. // Convert Shortcodes
  412. $shortcode = "[ninja_forms_field id=$field_id]";
  413. if( ! is_array( $value ) ) {
  414. if ( FALSE !== strpos( $value, $shortcode ) ) {
  415. $value = str_replace( $shortcode, '{field:' . $field_key . '}', $value );
  416. }
  417. }
  418. }
  419. //Checks for the nf_sub_seq_num short code and replaces it with the submission sequence merge tag
  420. $sub_seq = '[nf_sub_seq_num]';
  421. if( ! is_array( $value ) ) {
  422. if( FALSE !== strpos( $value, $sub_seq ) ){
  423. $value = str_replace( $sub_seq, '{submission:sequence}', $value );
  424. }
  425. }
  426. if( ! is_array( $value ) ) {
  427. if (FALSE !== strpos($value, '[ninja_forms_all_fields]')) {
  428. $value = str_replace('[ninja_forms_all_fields]', '{field:all_fields}', $value);
  429. }
  430. }
  431. $action_settings[ $setting ] = $value;
  432. $import[ 'actions' ][ $key ] = $action_settings;
  433. }
  434. }
  435. return $import;
  436. }
  437. public function import_action_backwards_compatibility( $action )
  438. {
  439. // Remove `_` from type
  440. if( isset( $action[ 'type' ] ) ) {
  441. $action['type'] = str_replace('_', '', $action['type']);
  442. }
  443. if( 'email' == $action[ 'type' ] ){
  444. $action[ 'to' ] = str_replace( '`', ',', $action[ 'to' ] );
  445. $action[ 'email_subject' ] = str_replace( '`', ',', $action[ 'email_subject' ] );
  446. $action[ 'cc' ] = str_replace( '`', ',', $action[ 'cc' ] );
  447. $action[ 'bcc' ] = str_replace( '`', ',', $action[ 'bcc' ] );
  448. // If our email is in plain text...
  449. if ( $action[ 'email_format' ] == 'plain' ) {
  450. // Record it as such.
  451. $action[ 'email_message_plain' ] = $action[ 'email_message' ];
  452. } // Otherwise... (It's not plain text.)
  453. else {
  454. // Record it as HTML.
  455. $action[ 'email_message' ] = nl2br( $action[ 'email_message' ] );
  456. }
  457. }
  458. // Convert `name` to `label`
  459. if( isset( $action[ 'name' ] ) ) {
  460. $action['label'] = $action['name'];
  461. unset($action['name']);
  462. }
  463. return apply_filters( 'ninja_forms_upgrade_action_' . $action[ 'type' ], $action );
  464. }
  465. public function import_field_backwards_compatibility( $field )
  466. {
  467. // Flatten field settings array
  468. if( isset( $field[ 'data' ] ) && is_array( $field[ 'data' ] ) ){
  469. $field = array_merge( $field, $field[ 'data' ] );
  470. }
  471. unset( $field[ 'data' ] );
  472. // Drop form_id in favor of parent_id, which is set by the form.
  473. if( isset( $field[ 'form_id' ] ) ){
  474. unset( $field[ 'form_id' ] );
  475. }
  476. // Remove `_` prefix from type setting
  477. $field[ 'type' ] = ltrim( $field[ 'type' ], '_' );
  478. // Type: `text` -> `textbox`
  479. if( 'text' == $field[ 'type' ] ){
  480. $field[ 'type' ] = 'textbox';
  481. }
  482. if( 'submit' == $field[ 'type' ] ){
  483. $field[ 'processing_label' ] = 'Processing';
  484. }
  485. if( isset( $field[ 'email' ] ) ){
  486. if( 'textbox' == $field[ 'type' ] && $field[ 'email' ] ) {
  487. $field['type'] = 'email';
  488. }
  489. unset( $field[ 'email' ] );
  490. }
  491. if( isset( $field[ 'class' ] ) ){
  492. $field[ 'element_class' ] = $field[ 'class' ];
  493. unset( $field[ 'class' ] );
  494. }
  495. if( isset( $field[ 'req' ] ) ){
  496. $field[ 'required' ] = $field[ 'req' ];
  497. unset( $field[ 'req' ] );
  498. }
  499. if( isset( $field[ 'default_value_type' ] ) ){
  500. /* User Data */
  501. if( '_user_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:user_id}';
  502. if( '_user_email' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:user_email}';
  503. if( '_user_lastname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:user_last_name}';
  504. if( '_user_firstname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:user_first_name}';
  505. if( '_user_display_name' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:user_display_name}';
  506. /* Post Data */
  507. if( 'post_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:post_id}';
  508. if( 'post_url' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:post_url}';
  509. if( 'post_title' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{wp:post_title}';
  510. /* System Data */
  511. if( 'today' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{other:date}';
  512. /* Miscellaneous */
  513. if( '_custom' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ){
  514. $field[ 'default' ] = $field[ 'default_value' ];
  515. }
  516. if( 'querystring' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ){
  517. $field[ 'default' ] = '{querystring:' . $field[ 'default_value' ] . '}';
  518. }
  519. unset( $field[ 'default_value' ] );
  520. unset( $field[ 'default_value_type' ] );
  521. } else if ( isset ( $field[ 'default_value' ] ) ) {
  522. $field[ 'default' ] = $field[ 'default_value' ];
  523. }
  524. if( 'list' == $field[ 'type' ] ) {
  525. if ( isset( $field[ 'list_type' ] ) ) {
  526. if ('dropdown' == $field['list_type']) {
  527. $field['type'] = 'listselect';
  528. }
  529. if ('radio' == $field['list_type']) {
  530. $field['type'] = 'listradio';
  531. }
  532. if ('checkbox' == $field['list_type']) {
  533. $field['type'] = 'listcheckbox';
  534. }
  535. if ('multi' == $field['list_type']) {
  536. $field['type'] = 'listmultiselect';
  537. }
  538. }
  539. if( isset( $field[ 'list' ][ 'options' ] ) ) {
  540. $field[ 'options' ] = array_values( $field[ 'list' ][ 'options' ] );
  541. unset( $field[ 'list' ][ 'options' ] );
  542. }
  543. foreach( $field[ 'options' ] as &$option ){
  544. if( isset( $option[ 'value' ] ) && $option[ 'value' ] ) continue;
  545. $option[ 'value' ] = $option[ 'label' ];
  546. }
  547. }
  548. if( 'country' == $field[ 'type' ] ){
  549. $field[ 'type' ] = 'listcountry';
  550. $field[ 'options' ] = array();
  551. }
  552. // Convert `textbox` to other field types
  553. foreach( array( 'fist_name', 'last_name', 'user_zip', 'user_city', 'user_phone', 'user_email', 'user_address_1', 'user_address_2', 'datepicker' ) as $item ) {
  554. if ( isset( $field[ $item ] ) && $field[ $item ] ) {
  555. $field[ 'type' ] = str_replace( array( '_', 'user', '1', '2', 'picker' ), '', $item );
  556. unset( $field[ $item ] );
  557. }
  558. }
  559. if( 'timed_submit' == $field[ 'type' ] ) {
  560. $field[ 'type' ] = 'submit';
  561. }
  562. if( 'checkbox' == $field[ 'type' ] ){
  563. if( isset( $field[ 'calc_value' ] ) ){
  564. if( isset( $field[ 'calc_value' ][ 'checked' ] ) ){
  565. $field[ 'checked_calc_value' ] = $field[ 'calc_value' ][ 'checked' ];
  566. unset( $field[ 'calc_value' ][ 'checked' ] );
  567. }
  568. if( isset( $field[ 'calc_value' ][ 'unchecked' ] ) ){
  569. $field[ 'unchecked_calc_value' ] = $field[ 'calc_value' ][ 'unchecked' ];
  570. unset( $field[ 'calc_value' ][ 'unchecked' ] );
  571. }
  572. }
  573. }
  574. if( 'rating' == $field[ 'type' ] ){
  575. $field[ 'type' ] = 'starrating';
  576. if( isset( $field[ 'rating_stars' ] ) ){
  577. $field[ 'default' ] = $field[ 'rating_stars' ];
  578. unset( $field[ 'rating_stars' ] );
  579. }
  580. }
  581. if( 'number' == $field[ 'type' ] ){
  582. if( ! isset( $field[ 'number_min' ] ) || ! $field[ 'number_min' ] ){
  583. $field[ 'num_min' ] = '';
  584. } else {
  585. $field[ 'num_min' ] = $field[ 'number_min' ];
  586. }
  587. if( ! isset( $field[ 'number_max' ] ) || ! $field[ 'number_max' ] ){
  588. $field[ 'num_max' ] = '';
  589. } else {
  590. $field[ 'num_max' ] = $field[ 'number_max' ];
  591. }
  592. if( ! isset( $field[ 'number_step' ] ) || ! $field[ 'number_step' ] ){
  593. $field[ 'num_step' ] = 1;
  594. } else {
  595. $field[ 'num_step' ] = $field[ 'number_step' ];
  596. }
  597. }
  598. if( 'profile_pass' == $field[ 'type' ] ){
  599. $field[ 'type' ] = 'password';
  600. $passwordconfirm = array_merge( $field, array(
  601. 'id' => '',
  602. 'type' => 'passwordconfirm',
  603. 'label' => $field[ 'label' ] . ' ' . __( 'Confirm', 'ninja-forms' ),
  604. 'confirm_field' => 'password_' . $field[ 'id' ]
  605. ));
  606. $field[ 'new_fields' ][] = $passwordconfirm;
  607. }
  608. if( 'desc' == $field[ 'type' ] ){
  609. $field[ 'type' ] = 'html';
  610. }
  611. if( 'credit_card' == $field[ 'type' ] ){
  612. $field[ 'type' ] = 'creditcardnumber';
  613. $field[ 'label' ] = $field[ 'cc_number_label' ];
  614. $field[ 'label_pos' ] = 'above';
  615. if( $field[ 'help_text' ] ){
  616. $field[ 'help_text' ] = '<p>' . $field[ 'help_text' ] . '</p>';
  617. }
  618. $credit_card_fields = array(
  619. 'creditcardcvc' => $field[ 'cc_cvc_label' ],
  620. 'creditcardfullname' => $field[ 'cc_name_label' ],
  621. 'creditcardexpiration' => $field[ 'cc_exp_month_label' ] . ' ' . $field[ 'cc_exp_year_label' ],
  622. 'creditcardzip' => __( 'Credit Card Zip', 'ninja-forms' ),
  623. );
  624. foreach( $credit_card_fields as $new_type => $new_label ){
  625. $field[ 'new_fields' ][] = array_merge( $field, array(
  626. 'id' => '',
  627. 'type' => $new_type,
  628. 'label' => $new_label,
  629. 'help_text' => '',
  630. 'desc_text' => ''
  631. ));
  632. }
  633. }
  634. /*
  635. * Convert inside label position over to placeholder
  636. */
  637. if ( isset ( $field[ 'label_pos' ] ) && 'inside' == $field[ 'label_pos' ] ) {
  638. if ( ! isset ( $field[ 'placeholder' ] ) || empty ( $field[ 'placeholder' ] ) ) {
  639. $field[ 'placeholder' ] = $field[ 'label' ];
  640. }
  641. $field[ 'label_pos' ] = 'hidden';
  642. }
  643. if( isset( $field[ 'desc_text' ] ) ){
  644. $field[ 'desc_text' ] = nl2br( $field[ 'desc_text' ] );
  645. }
  646. if( isset( $field[ 'help_text' ] ) ){
  647. $field[ 'help_text' ] = nl2br( $field[ 'help_text' ] );
  648. }
  649. return apply_filters( 'ninja_forms_upgrade_field', $field );
  650. }
  651. } // End NF_Database_Models_Form