ajax.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. add_action( 'wp_ajax_ninja_forms_save_metabox_state', 'ninja_forms_save_metabox_state' );
  3. function ninja_forms_save_metabox_state(){
  4. // Bail if we aren't in the admin
  5. if ( ! is_admin() )
  6. return false;
  7. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  8. $plugin_settings = nf_get_settings();
  9. $page = esc_html( $_REQUEST['page'] );
  10. $tab = esc_html( $_REQUEST['tab'] );
  11. $slug = esc_html( $_REQUEST['slug'] );
  12. $metabox_state = esc_html( $_REQUEST['metabox_state'] );
  13. $plugin_settings['metabox_state'][$page][$tab][$slug] = $metabox_state;
  14. update_option( 'ninja_forms_settings', $plugin_settings );
  15. die();
  16. }
  17. /**
  18. * When a field settings metabox is expanded, return a JSON element containing the field settings HTML
  19. *
  20. * @since 2.9
  21. * @return false;
  22. */
  23. function nf_output_field_settings_html() {
  24. global $nf_rte_editors;
  25. // Bail if we aren't in the admin
  26. if ( ! is_admin() )
  27. return false;
  28. // Bail if we don't have proper permissions
  29. if ( ! current_user_can( apply_filters( 'nf_new_field_capabilities', 'manage_options' ) ) )
  30. return false;
  31. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  32. $field_id = esc_html( $_REQUEST['field_id'] );
  33. $data = isset ( $_REQUEST['data'] ) ? json_decode( stripslashes( $_REQUEST['data'] ), true ) : array();
  34. $field = ninja_forms_get_field_by_id( $field_id );
  35. $field_data = $field['data'];
  36. $data = wp_parse_args( $data, $field_data );
  37. nf_output_registered_field_settings( $field_id, $data );
  38. die();
  39. }
  40. add_action( 'wp_ajax_nf_output_field_settings_html', 'nf_output_field_settings_html' );
  41. /**
  42. * Save our admin fields page.
  43. *
  44. * @since 2.9
  45. * @return false;
  46. */
  47. function nf_admin_save_builder() {
  48. global $ninja_forms_fields, $wpdb;
  49. // Bail if we aren't in the admin
  50. if ( ! is_admin() )
  51. return false;
  52. // Bail if we don't have proper permissions
  53. if ( ! current_user_can( apply_filters( 'nf_new_field_capabilities', 'manage_options' ) ) )
  54. return false;
  55. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  56. $field_data = json_decode( stripslashes( $_REQUEST['field_data'] ), true );
  57. $form_id = esc_html( $_REQUEST['form_id'] );
  58. $form_title = stripslashes( $_REQUEST['form_title'] );
  59. $field_order = json_decode( strip_tags( stripslashes( $_REQUEST['field_order'] ) ), true );
  60. if ( is_array ( $field_order ) ) {
  61. $order_array = array();
  62. $x = 0;
  63. foreach ( $field_order as $id ) {
  64. $id = str_replace( 'ninja_forms_field_', '', $id );
  65. $order_array[ $id ] = $x;
  66. $x++;
  67. }
  68. }
  69. $tmp_array = array();
  70. foreach ( $field_data as $field ) {
  71. $field_id = $field['id'];
  72. unset( $field['id'] );
  73. unset( $field['metabox_state'] );
  74. $tmp_array[ $field_id ] = $field;
  75. }
  76. $field_data = $tmp_array;
  77. if ( isset ( $ninja_forms_fields ) && is_array( $ninja_forms_fields ) ) {
  78. foreach ( $ninja_forms_fields as $slug => $field ){
  79. if ( $field['save_function'] != '') {
  80. $save_function = $field['save_function'];
  81. $arguments['form_id'] = $form_id;
  82. $arguments['data'] = $field_data;
  83. $field_data = call_user_func_array( $save_function, $arguments );
  84. }
  85. }
  86. }
  87. if( $form_id != '' && $form_id != 0 && $form_id != 'new' ){
  88. foreach ( $field_data as $field_id => $vals ) {
  89. $field_order = isset( $order_array[$field_id] ) ? $order_array[$field_id] : '';
  90. $field_row = ninja_forms_get_field_by_id( $field_id );
  91. $data = $field_row['data'];
  92. foreach( $vals as $k => $v ){
  93. $data[$k] = $v;
  94. }
  95. $data_array = array('data' => serialize( $data ), 'order' => $field_order);
  96. $wpdb->update( NINJA_FORMS_FIELDS_TABLE_NAME, $data_array, array( 'id' => $field_id ));
  97. }
  98. $date_updated = date( 'Y-m-d H:i:s', strtotime ( 'now' ) );
  99. Ninja_Forms()->form( $form_id )->update_setting( 'form_title', $form_title );
  100. Ninja_Forms()->form( $form_id )->update_setting( 'date_updated', $date_updated );
  101. Ninja_Forms()->form( $form_id )->update_setting( 'status', '' );
  102. }
  103. // Dump our current form transient.
  104. delete_transient( 'nf_form_' . $form_id );
  105. die();
  106. }
  107. add_action( 'wp_ajax_nf_admin_save_builder', 'nf_admin_save_builder' );
  108. add_action('wp_ajax_ninja_forms_new_field', 'ninja_forms_new_field');
  109. function ninja_forms_new_field(){
  110. global $wpdb, $ninja_forms_fields;
  111. // Bail if we aren't in the admin
  112. if ( ! is_admin() )
  113. return false;
  114. // Bail if we don't have proper permissions
  115. if ( ! current_user_can( apply_filters( 'nf_new_field_capabilities', 'manage_options' ) ) )
  116. return false;
  117. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  118. $type = esc_html( $_REQUEST['type'] );
  119. $form_id = absint( $_REQUEST['form_id'] );
  120. if( isset( $ninja_forms_fields[$type]['name'] ) ){
  121. $type_name = $ninja_forms_fields[$type]['name'];
  122. }else{
  123. $type_name = '';
  124. }
  125. if( isset( $ninja_forms_fields[$type]['default_label'] ) ){
  126. $default_label = $ninja_forms_fields[$type]['default_label'];
  127. }else{
  128. $default_label = '';
  129. }
  130. if( isset( $ninja_forms_fields[$type]['edit_options'] ) ){
  131. $edit_options = $ninja_forms_fields[$type]['edit_options'];
  132. }else{
  133. $edit_options = '';
  134. }
  135. if ( $default_label != '' ) {
  136. $label = $default_label;
  137. } else {
  138. $label = $type_name;
  139. }
  140. $input_limit_msg = __( 'character(s) left', 'ninja-forms' );
  141. $data = serialize( array( 'label' => $label, 'input_limit_msg' => $input_limit_msg ) );
  142. $order = 999;
  143. if($form_id != 0 && $form_id != ''){
  144. $args = array(
  145. 'type' => $type,
  146. 'data' => $data,
  147. );
  148. $new_id = ninja_forms_insert_field( $form_id, $args );
  149. $new_html = ninja_forms_return_echo('ninja_forms_edit_field', $new_id, true );
  150. header("Content-type: application/json");
  151. $array = array ('new_id' => $new_id, 'new_type' => $type_name, 'new_html' => $new_html, 'edit_options' => $edit_options, 'new_type_slug' => $type );
  152. echo json_encode($array);
  153. die();
  154. }
  155. }
  156. add_action('wp_ajax_ninja_forms_remove_field', 'ninja_forms_remove_field');
  157. function ninja_forms_remove_field(){
  158. global $wpdb;
  159. // Bail if we aren't in the admin
  160. if ( ! is_admin() )
  161. return false;
  162. // Bail if we don't have proper permissions
  163. if ( ! current_user_can( apply_filters( 'nf_delete_field_capabilities', 'manage_options' ) ) )
  164. return false;
  165. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  166. $field_id = absint( $_REQUEST['field_id'] );
  167. $form_id = absint( $_REQUEST['form_id'] );
  168. $wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE id = %d", $field_id));
  169. Ninja_Forms()->form( $form_id )->dump_cache();
  170. die();
  171. }
  172. add_action('wp_ajax_ninja_forms_add_list_option', 'ninja_forms_add_list_options');
  173. function ninja_forms_add_list_options(){
  174. global $wpdb;
  175. // Bail if we aren't in the admin
  176. if ( ! is_admin() )
  177. return false;
  178. // Bail if we don't have proper permissions
  179. if ( ! current_user_can( apply_filters( 'nf_new_field_capabilities', 'manage_options' ) ) )
  180. return false;
  181. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  182. $field_id = absint( $_REQUEST['field_id'] );
  183. $x = absint( $_REQUEST['x'] );
  184. $hidden_value = esc_html( $_REQUEST['hidden_value'] );
  185. ninja_forms_field_list_option_output($field_id, $x, '', $hidden_value);
  186. die();
  187. }
  188. function ninja_forms_insert_fav(){
  189. global $wpdb, $ninja_forms_fields;
  190. // Bail if we aren't in the admin
  191. if ( ! is_admin() )
  192. return false;
  193. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  194. $fav_id = absint( $_REQUEST['field_id'] );
  195. $form_id = absint( $_REQUEST['form_id'] );
  196. $fav_row = ninja_forms_get_fav_by_id($fav_id);
  197. $data = serialize($fav_row['data']);
  198. $type = $fav_row['type'];
  199. $type_name = $ninja_forms_fields[$type]['name'];
  200. if($form_id != 0 && $form_id != ''){
  201. $args = array(
  202. 'type' => $type,
  203. 'data' => $data,
  204. 'fav_id' => $fav_id,
  205. );
  206. $new_id = ninja_forms_insert_field( $form_id, $args );
  207. $new_html = ninja_forms_return_echo('ninja_forms_edit_field', $new_id, true );
  208. header("Content-type: application/json");
  209. $array = array ('new_id' => $new_id, 'new_type' => $type_name, 'new_html' => $new_html);
  210. echo json_encode($array);
  211. }
  212. die();
  213. }
  214. add_action('wp_ajax_ninja_forms_insert_fav', 'ninja_forms_insert_fav');
  215. function ninja_forms_insert_def(){
  216. global $wpdb, $ninja_forms_fields;
  217. // Bail if we aren't in the admin
  218. if ( ! is_admin() )
  219. return false;
  220. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  221. $def_id = absint( $_REQUEST['field_id'] );
  222. $form_id = absint( $_REQUEST['form_id'] );
  223. $def_row = ninja_forms_get_def_by_id($def_id);
  224. $data = serialize($def_row['data']);
  225. $type = $def_row['type'];
  226. $type_name = $ninja_forms_fields[$type]['name'];
  227. if($form_id != 0 && $form_id != ''){
  228. $args = array(
  229. 'type' => $type,
  230. 'data' => $data,
  231. 'def_id' => $def_id,
  232. );
  233. $new_id = ninja_forms_insert_field( $form_id, $args );
  234. $new_html = ninja_forms_return_echo('ninja_forms_edit_field', $new_id, true );
  235. header("Content-type: application/json");
  236. $array = array ('new_id' => $new_id, 'new_type' => $type_name, 'new_html' => $new_html);
  237. echo json_encode($array);
  238. }
  239. die();
  240. }
  241. add_action('wp_ajax_ninja_forms_insert_def', 'ninja_forms_insert_def');
  242. add_action('wp_ajax_ninja_forms_add_fav', 'ninja_forms_add_fav');
  243. function ninja_forms_add_fav(){
  244. global $wpdb;
  245. // Bail if we aren't in the admin
  246. if ( ! is_admin() )
  247. return false;
  248. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  249. $field_data = $_REQUEST['field_data'];
  250. $field_id = absint( $_REQUEST['field_id'] );
  251. $field_row = ninja_forms_get_field_by_id($field_id);
  252. $field_type = $field_row['type'];
  253. $form_id = 1;
  254. $data = array();
  255. foreach($field_data as $key => $val){
  256. $key = stripslashes( $key );
  257. $key = str_replace('"', '', $key);
  258. if(strpos($key, '[')){
  259. $key = str_replace(']', '', $key);
  260. $key = explode('[', $key);
  261. $multi = array();
  262. $temp =& $multi;
  263. $x = 0;
  264. $count = count($key) - 1;
  265. foreach ($key as $item){
  266. $temp[$item] = array();
  267. if($x < $count){
  268. $temp =& $temp[$item];
  269. }else{
  270. $temp[$item] = $val;
  271. }
  272. $x++;
  273. }
  274. $data = ninja_forms_array_merge_recursive($data, $multi);
  275. }else{
  276. $data[$key] = $val;
  277. }
  278. }
  279. $name = stripslashes( esc_html( $_REQUEST['fav_name'] ) );
  280. if ( !isset ( $data['label'] ) or empty ( $data['label'] ) ) {
  281. $data['label'] = $name;
  282. }
  283. $data = ninja_forms_stripslashes_deep( $data );
  284. $data = serialize($data);
  285. $wpdb->insert(NINJA_FORMS_FAV_FIELDS_TABLE_NAME, array('row_type' => 1, 'type' => $field_type, 'order' => 0, 'data' => $data, 'name' => $name));
  286. $fav_id = $wpdb->insert_id;
  287. $update_array = array('fav_id' => $fav_id);
  288. $wpdb->update( NINJA_FORMS_FIELDS_TABLE_NAME, $update_array, array( 'id' => $field_id ));
  289. $new_html = '<p class="button-controls" id="ninja_forms_insert_fav_field_'.$fav_id.'_p">
  290. <a class="button add-new-h2 ninja-forms-insert-fav-field" id="ninja_forms_insert_fav_field_'.$fav_id.'" data-field="' . $fav_id . '" data-type="fav" href="#">'.__($name, 'ninja-forms').'</a>
  291. </p>';
  292. header("Content-type: application/json");
  293. $array = array ('fav_id' => $fav_id, 'fav_name' => $name, 'link_html' => $new_html);
  294. echo json_encode($array);
  295. die();
  296. }
  297. add_action('wp_ajax_ninja_forms_add_def', 'ninja_forms_add_def');
  298. function ninja_forms_add_def(){
  299. global $wpdb;
  300. // Bail if we aren't in the admin
  301. if ( ! is_admin() )
  302. return false;
  303. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  304. $field_data = $_REQUEST['field_data'];
  305. $field_id = absint( $_REQUEST['field_id'] );
  306. $field_row = ninja_forms_get_field_by_id($field_id);
  307. $field_type = $field_row['type'];
  308. $row_type = 0;
  309. $data = array();
  310. foreach($field_data as $key => $val){
  311. $key = str_replace('"', '', $key);
  312. if(strpos($key, '[')){
  313. $key = str_replace(']', '', $key);
  314. $key = explode('[', $key);
  315. $multi = array();
  316. $temp =& $multi;
  317. $x = 0;
  318. $count = count($key) - 1;
  319. foreach ($key as $item){
  320. $temp[$item] = array();
  321. if($x < $count){
  322. $temp =& $temp[$item];
  323. }else{
  324. $temp[$item] = $val;
  325. }
  326. $x++;
  327. }
  328. $data = ninja_forms_array_merge_recursive($data, $multi);
  329. }else{
  330. $data[$key] = $val;
  331. }
  332. }
  333. $name = stripslashes( esc_html( $_REQUEST['def_name'] ) );
  334. $data['label'] = $name;
  335. $data = serialize($data);
  336. $wpdb->insert(NINJA_FORMS_FAV_FIELDS_TABLE_NAME, array('row_type' => $row_type, 'type' => $field_type, 'data' => $data, 'name' => $name));
  337. $def_id = $wpdb->insert_id;
  338. $update_array = array('def_id' => $def_id);
  339. $wpdb->update( NINJA_FORMS_FIELDS_TABLE_NAME, $update_array, array( 'id' => $field_id ));
  340. $new_html = '<p class="button-controls" id="ninja_forms_insert_def_field_'.$def_id.'_p">
  341. <a class="button add-new-h2 ninja-forms-insert-def-field" id="ninja_forms_insert_def_field_'.$def_id.'" name="" href="#">'.__($name, 'ninja-forms').'</a>
  342. </p>';
  343. header("Content-type: application/json");
  344. $array = array ('def_id' => $def_id, 'def_name' => $name, 'link_html' => $new_html);
  345. echo json_encode($array);
  346. die();
  347. }
  348. add_action('wp_ajax_ninja_forms_remove_fav', 'ninja_forms_remove_fav');
  349. function ninja_forms_remove_fav(){
  350. global $wpdb, $ninja_forms_fields;
  351. // Bail if we aren't in the admin
  352. if ( ! is_admin() )
  353. return false;
  354. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  355. $field_id = absint( $_REQUEST['field_id'] );
  356. $field_row = ninja_forms_get_field_by_id($field_id);
  357. $field_type = $field_row['type'];
  358. $fav_id = $field_row['fav_id'];
  359. $wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d", $fav_id));
  360. $wpdb->update(NINJA_FORMS_FIELDS_TABLE_NAME, array('fav_id' => '' ), array('fav_id' => $fav_id));
  361. $type_name = $ninja_forms_fields[$field_type]['name'];
  362. header("Content-type: application/json");
  363. $array = array ('fav_id' => $fav_id, 'type_name' => $type_name);
  364. echo json_encode($array);
  365. die();
  366. }
  367. add_action('wp_ajax_ninja_forms_remove_def', 'ninja_forms_remove_def');
  368. function ninja_forms_remove_def(){
  369. global $wpdb, $ninja_forms_fields;
  370. // Bail if we aren't in the admin
  371. if ( ! is_admin() )
  372. return false;
  373. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  374. $field_id = absint( $_REQUEST['field_id'] );
  375. $field_row = ninja_forms_get_field_by_id($field_id);
  376. $field_type = $field_row['type'];
  377. $def_id = $field_row['def_id'];
  378. $wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d", $def_id));
  379. $wpdb->update(NINJA_FORMS_FIELDS_TABLE_NAME, array('def_id' => '' ), array('def_id' => $def_id));
  380. $type_name = $ninja_forms_fields[$field_type]['name'];
  381. header("Content-type: application/json");
  382. $array = array ('def_id' => $def_id, 'type_name' => $type_name);
  383. echo json_encode($array);
  384. die();
  385. }
  386. add_action( 'wp_ajax_ninja_forms_side_sortable', 'ninja_forms_side_sortable' );
  387. function ninja_forms_side_sortable(){
  388. // Bail if we aren't in the admin
  389. if ( ! is_admin() )
  390. return false;
  391. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  392. $plugin_settings = nf_get_settings();
  393. $page = esc_html( $_REQUEST['page'] );
  394. $tab = esc_html( $_REQUEST['tab'] );
  395. $order = ninja_forms_esc_html_deep( $_REQUEST['order'] );
  396. $plugin_settings['sidebars'][$page][$tab] = $order;
  397. update_option( 'ninja_forms_settings', $plugin_settings );
  398. die();
  399. }
  400. add_action('wp_ajax_ninja_forms_delete_sub', 'ninja_forms_delete_sub');
  401. function ninja_forms_delete_sub($sub_id = ''){
  402. global $wpdb;
  403. // Bail if we aren't in the admin
  404. if ( ! is_admin() )
  405. return false;
  406. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  407. if($sub_id == ''){
  408. $ajax = true;
  409. $sub_id = absint( $_REQUEST['sub_id'] );
  410. }else{
  411. $ajax = false;
  412. }
  413. $wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_SUBS_TABLE_NAME." WHERE id = %d", $sub_id));
  414. if( $ajax ){
  415. die();
  416. }
  417. }
  418. function ninja_forms_array_merge_recursive() {
  419. $arrays = func_get_args();
  420. $base = array_shift($arrays);
  421. foreach ($arrays as $array) {
  422. reset($base); //important
  423. while (list($key, $value) = @each($array)) {
  424. if (is_array($value) && @is_array($base[$key])) {
  425. $base[$key] = ninja_forms_array_merge_recursive($base[$key], $value);
  426. } else {
  427. $base[$key] = $value;
  428. }
  429. }
  430. }
  431. return $base;
  432. }
  433. function ninja_forms_import_list_options(){
  434. // Bail if we aren't in the admin
  435. if ( ! is_admin() )
  436. return false;
  437. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  438. $options = $_REQUEST['options'];
  439. $field_id = absint( $_REQUEST['field_id'] );
  440. $options = str_replace('\,', '-comma-replace-placeholder-', $options );
  441. $options = ninja_forms_csv_explode( $options );
  442. if( is_array( $options ) ){
  443. $tmp_array = array();
  444. $x = 0;
  445. foreach( $options as $option ){
  446. $label = stripslashes( $option[0] );
  447. $value = stripslashes( $option[1] );
  448. $calc = stripslashes( $option[2] );
  449. $label = str_replace( "''", "", $label );
  450. $label = str_replace( "-comma-replace-placeholder-", ",", $label );
  451. $value = str_replace( "''", "", $value );
  452. $value = str_replace( "-comma-replace-placeholder-", ",", $value );
  453. $calc = str_replace( "''", "", $calc );
  454. $calc = str_replace( "-comma-replace-placeholder-", ",", $calc );
  455. $tmp_array[$x]['label'] = $label;
  456. $tmp_array[$x]['value'] = $value;
  457. $tmp_array[$x]['calc'] = $calc;
  458. $x++;
  459. }
  460. $x = 0;
  461. foreach( $tmp_array as $option ){
  462. $hidden = 0;
  463. ninja_forms_field_list_option_output($field_id, $x, $option, $hidden);
  464. $x++;
  465. }
  466. }
  467. die();
  468. }
  469. add_action( 'wp_ajax_ninja_forms_import_list_options', 'ninja_forms_import_list_options' );
  470. /*
  471. *
  472. * Function that outputs a list of terms so that the user can exclude terms from a list selector.
  473. *
  474. * @since 2.2.51
  475. * @return void
  476. */
  477. function ninja_forms_list_terms_checkboxes( $field_id = '', $tax_name = '' ){
  478. // Bail if we aren't in the admin
  479. if ( ! is_admin() )
  480. return false;
  481. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  482. if ( $field_id == '' && isset ( $_POST['field_id'] ) ) {
  483. $field_id = absint( $_POST['field_id'] );
  484. }
  485. if ( $tax_name == '' && isset ( $_POST['tax_name'] ) ) {
  486. $tax_name = esc_html( $_POST['tax_name'] );
  487. }
  488. if ( $field_id != '' && $tax_name != '' ) {
  489. $field = ninja_forms_get_field_by_id( $field_id );
  490. if ( isset ( $field['data']['exclude_terms'] ) ) {
  491. $exclude_terms = $field['data']['exclude_terms'];
  492. } else {
  493. $exclude_terms = '';
  494. }
  495. $terms = get_terms( $tax_name, array( 'hide_empty' => false ) );
  496. if ( is_array ( $terms ) && !empty ( $terms ) ) {
  497. ?>
  498. <h4><?php _e( 'Do not show these terms', 'ninja-forms' );?>:</h4>
  499. <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>[exclude_terms]" value="">
  500. <?php
  501. foreach ( $terms as $term ) {
  502. ?>
  503. <div>
  504. <label>
  505. <input type="checkbox" <?php checked( in_array ( $term->term_id, $exclude_terms ), true );?> name="ninja_forms_field_<?php echo $field_id;?>[exclude_terms][]" value="<?php echo $term->term_id;?>">
  506. <?php echo $term->name;?>
  507. </label>
  508. </div>
  509. <?php
  510. }
  511. }
  512. }
  513. if ( isset ( $_POST['from_ajax'] ) && absint( $_POST['from_ajax'] ) == 1 ) {
  514. die();
  515. }
  516. }
  517. add_action( 'wp_ajax_ninja_forms_list_terms_checkboxes', 'ninja_forms_list_terms_checkboxes' );
  518. /*
  519. *
  520. * Function that outputs a calculation row
  521. *
  522. * @since 2.2.28
  523. * @returns void
  524. */
  525. function ninja_forms_add_calc_row(){
  526. // Bail if we aren't in the admin
  527. if ( ! is_admin() )
  528. return false;
  529. check_ajax_referer( 'nf_ajax', 'nf_ajax_nonce' );
  530. $field_id = absint( $_REQUEST['field_id'] );
  531. $c = array( 'calc' => '', 'operator' => 'add', 'value' => '', 'when' => '' );
  532. $x = absint( $_REQUEST['x'] );
  533. ninja_forms_output_field_calc_row( $field_id, $c, $x );
  534. die();
  535. }
  536. add_action( 'wp_ajax_ninja_forms_add_calc_row', 'ninja_forms_add_calc_row' );
  537. /**
  538. *
  539. * Covert a multi-line CSV string into a 2d array. Follows RFC 4180, allows
  540. * "cells with ""escaped delimiters""" && multi-line enclosed cells
  541. * It assumes the CSV file is properly formatted, and doesn't check for errors
  542. * in CSV format.
  543. * @param string $str The CSV string
  544. * @param string $d The delimiter between values
  545. * @param string $e The enclosing character
  546. * @param bool $crlf Set to true if your CSV file should return carriage return
  547. * and line feed (CRLF should be returned according to RFC 4180
  548. * @return array
  549. */
  550. function ninja_forms_csv_explode( $str, $d=',', $e='"', $crlf=TRUE ) {
  551. // Convert CRLF to LF, easier to work with in regex
  552. if( $crlf ) $str = str_replace("\r\n","\n",$str);
  553. // Get rid of trailing linebreaks that RFC4180 allows
  554. $str = trim($str);
  555. // Do the dirty work
  556. if ( preg_match_all(
  557. '/(?:
  558. '.$e.'((?:[^'.$e.']|'.$e.$e.')*+)'.$e.'(?:'.$d.'|\n|$)
  559. # match enclose, then match either non-enclose or double-enclose
  560. # zero to infinity times (possesive), then match another enclose,
  561. # followed by a comma, linebreak, or string end
  562. | ####### OR #######
  563. ([^'.$d.'\n]*+)(?:['.$d.'\n]|$)
  564. # match anything thats not a comma or linebreak zero to infinity
  565. # times (possesive), then match either a comma or a linebreak or
  566. # string end
  567. )/x',
  568. $str, $ms, PREG_SET_ORDER
  569. ) === FALSE ) return FALSE;
  570. // Initialize vars, $r will hold our return data, $i will track which line we're on
  571. $r = array(); $i = 0;
  572. // Loop through results
  573. foreach( $ms as $m ) {
  574. // If the first group of matches is empty, the cell has no quotes
  575. if( empty($m[1]) )
  576. // Put the CRLF back in if needed
  577. $r[$i][] = ($crlf == TRUE) ? str_replace("\n","\r\n",$m[2]) : $m[2];
  578. else {
  579. // The cell was quoted, so we want to convert any "" back to " and
  580. // any LF back to CRLF, if needed
  581. $r[$i][] = ($crlf == TRUE) ?
  582. str_replace(
  583. array("\n",$e.$e),
  584. array("\r\n",$e),
  585. $m[1]) :
  586. str_replace($e.$e, $e, $m[1]);
  587. }
  588. // If the raw match doesn't have a delimiter, it must be the last in the
  589. // row, so we increment our line count.
  590. if( substr($m[0],-1) != $d )
  591. $i++;
  592. }
  593. // An empty array will exist due to $ being a zero-length match, so remove it
  594. array_pop( $r );
  595. return $r;
  596. }