/deprecated/includes/database.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2.  
  3. // Begin Form Interaction Functions 
  4.  
  5. function ninja_forms_insert_field( $form_id, $args = array() ) { 
  6. global $wpdb; 
  7. $insert_array = array(); 
  8.  
  9. $insert_array['type'] = $args['type']; 
  10. $insert_array['form_id'] = $form_id; 
  11.  
  12. if( isset( $args['data'] ) ) { 
  13. $insert_array['data'] = $args['data']; 
  14. }else{ 
  15. $insert_array['data'] = ''; 
  16.  
  17. if( isset( $args['order'] ) ) { 
  18. $insert_array['order'] = $args['order']; 
  19. }else{ 
  20. $insert_array['order'] = 999; 
  21.  
  22. if( isset( $args['fav_id'] ) ) { 
  23. $insert_array['fav_id'] = $args['fav_id']; 
  24.  
  25. if( isset( $args['def_id'] ) ) { 
  26. $insert_array['def_id'] = $args['def_id']; 
  27.  
  28. $new_field = $wpdb->insert( NINJA_FORMS_FIELDS_TABLE_NAME, $insert_array ); 
  29. $new_id = $wpdb->insert_id; 
  30. return $new_id; 
  31.  
  32. function ninja_forms_get_form_ids_by_post_id( $post_id ) { 
  33. global $wpdb; 
  34. $form_ids = array(); 
  35. if( is_page( $post_id ) ) { 
  36. $form_results = ninja_forms_get_all_forms(); 
  37. if(is_array($form_results) AND !empty($form_results)) { 
  38. foreach($form_results as $form) { 
  39. $form_data = $form['data']; 
  40. if(isset($form_data['append_page']) AND !empty($form_data['append_page'])) { 
  41. if($form_data['append_page'] == $post_id) { 
  42. $form_ids[] = $form['id']; 
  43. $form_id = get_post_meta( $post_id, 'ninja_forms_form', true ); 
  44. if( !empty( $form_id ) ) { 
  45. $form_ids[] = $form_id; 
  46. }else if( is_single( $post_id ) ) { 
  47. $form_id = get_post_meta( $post_id, 'ninja_forms_form', true ); 
  48. if( !empty( $form_id ) ) { 
  49. $form_ids[] = $form_id; 
  50.  
  51. return $form_ids; 
  52.  
  53. function ninja_forms_get_form_by_sub_id( $sub_id ) { 
  54. global $wpdb; 
  55. $form_id = Ninja_Forms()->sub( $sub_id )->form_id; 
  56. $form_row = ninja_forms_get_form_by_id( $form_id ); 
  57. return $form_row; 
  58.  
  59. // The ninja_forms_delete_form( $form_id ) function is in includes/deprecated.php 
  60.  
  61. // Begin Field Interaction Functions 
  62.  
  63. function ninja_forms_get_field_by_id($field_id) { 
  64. global $wpdb; 
  65. $field_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE id = %d", $field_id), ARRAY_A); 
  66. if( $field_row != null ) { 
  67. $field_row['data'] = unserialize($field_row['data']); 
  68. return $field_row; 
  69. }else{ 
  70. return false; 
  71.  
  72. function ninja_forms_get_fields_by_form_id($form_id, $orderby = 'ORDER BY `order` ASC') { 
  73. global $wpdb; 
  74.  
  75. $field_results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE form_id = %d ".$orderby, $form_id), ARRAY_A); 
  76. if(is_array($field_results) AND !empty($field_results)) { 
  77. $x = 0; 
  78. $count = count($field_results) - 1; 
  79. while($x <= $count) { 
  80. $field_results[$x]['data'] = unserialize($field_results[$x]['data']); 
  81. $x++; 
  82.  
  83. return $field_results; 
  84.  
  85. function ninja_forms_get_all_fields() { 
  86. global $wpdb; 
  87. $field_results = $wpdb->get_results("SELECT * FROM ".NINJA_FORMS_FIELDS_TABLE_NAME, ARRAY_A); 
  88. if(is_array($field_results) AND !empty($field_results)) { 
  89. $x = 0; 
  90. $count = count($field_results) - 1; 
  91. while($x <= $count) { 
  92. $field_results[$x]['data'] = unserialize($field_results[$x]['data']); 
  93. $x++; 
  94. return $field_results; 
  95.  
  96. function ninja_forms_update_field($args) { 
  97. global $wpdb; 
  98. $update_array = $args['update_array']; 
  99. $where = $args['where']; 
  100. $wpdb->update(NINJA_FORMS_FIELDS_TABLE_NAME, $update_array, $where); 
  101.  
  102. function ninja_forms_delete_field( $field_id ) { 
  103. global $wpdb; 
  104. $wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE id = %d", $field_id), ARRAY_A); 
  105.  
  106. // Begin Favorite Fields Interaction Functions 
  107.  
  108. function ninja_forms_get_fav_by_id($fav_id) { 
  109. global $wpdb; 
  110. $fav_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d", $fav_id), ARRAY_A); 
  111. $fav_row['data'] = unserialize($fav_row['data']); 
  112.  
  113. return $fav_row; 
  114.  
  115. function ninja_forms_delete_fav_by_id($fav_id) { 
  116. global $wpdb; 
  117. $wpdb->query($wpdb->prepare("DELETE FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d", $fav_id), ARRAY_A); 
  118.  
  119. function ninja_forms_get_all_favs() { 
  120. global $wpdb; 
  121. $fav_results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE row_type = %d ORDER BY name ASC", 1), ARRAY_A); 
  122. if(is_array($fav_results) AND !empty($fav_results)) { 
  123. $x = 0; 
  124. $count = count($fav_results) - 1; 
  125. while($x <= $count) { 
  126. $fav_results[$x]['data'] = unserialize($fav_results[$x]['data']); 
  127. $x++; 
  128. return $fav_results; 
  129.  
  130. // Begin Defined Fields Functions 
  131.  
  132. function ninja_forms_get_def_by_id($def_id) { 
  133. global $wpdb; 
  134. $def_row = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE id = %d", $def_id), ARRAY_A); 
  135. $def_row['data'] = unserialize($def_row['data']); 
  136. return $def_row; 
  137.  
  138. function ninja_forms_get_all_defs() { 
  139. global $wpdb; 
  140. $def_results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FAV_FIELDS_TABLE_NAME." WHERE row_type = %d", 0), ARRAY_A); 
  141. if(is_array($def_results) AND !empty($def_results)) { 
  142. $x = 0; 
  143. $count = count($def_results) - 1; 
  144. while($x <= $count) { 
  145. $def_results[$x]['data'] = unserialize($def_results[$x]['data']); 
  146. $x++; 
  147. return $def_results; 
  148.  
  149. function ninja_forms_addslashes_deep( $value ) { 
  150. $value = is_array($value) ? 
  151. array_map('ninja_forms_addslashes_deep', $value) : 
  152. addslashes($value); 
  153. return $value; 
  154.  
  155. function utf8_encode_recursive( $input ) { 
  156. if ( is_array( $input ) ) { 
  157. return array_map( __FUNCTION__, $input ); 
  158. }else{ 
  159. return utf8_encode( $input ); 
  160.  
  161. function ninja_forms_str_replace_deep($search, $replace, $subject) { 
  162. if( is_array( $subject ) ) { 
  163. foreach( $subject as &$oneSubject ) 
  164. $oneSubject = ninja_forms_str_replace_deep($search, $replace, $oneSubject); 
  165. unset($oneSubject); 
  166. return $subject; 
  167. } else { 
  168. return str_replace($search, $replace, $subject); 
  169.  
  170. function ninja_forms_html_entity_decode_deep( $value, $flag = ENT_COMPAT ) { 
  171. $value = is_array($value) ? 
  172. array_map('ninja_forms_html_entity_decode_deep', $value) : 
  173. html_entity_decode( $value, $flag ); 
  174. return $value; 
  175.  
  176. function ninja_forms_htmlspecialchars_deep( $value ) { 
  177. $value = is_array($value) ? 
  178. array_map('ninja_forms_htmlspecialchars_deep', $value) : 
  179. htmlspecialchars( $value ); 
  180. return $value; 
  181.  
  182. function ninja_forms_stripslashes_deep( $value ) { 
  183. $value = is_array($value) ? 
  184. array_map('ninja_forms_stripslashes_deep', $value) : 
  185. stripslashes($value); 
  186. return $value; 
  187.  
  188. function ninja_forms_esc_html_deep( $value ) { 
  189. $value = is_array($value) ? 
  190. array_map('ninja_forms_esc_html_deep', $value) : 
  191. esc_html($value); 
  192. return $value; 
  193.  
  194. function nf_wp_kses_post_deep( $value ) { 
  195. $value = is_array( $value ) ? 
  196. array_map( 'nf_wp_kses_post_deep', $value ) : 
  197. wp_kses_post($value); 
  198.  
  199. return $value; 
  200.  
  201. function ninja_forms_strip_tags_deep($value ) { 
  202. $value = is_array($value) ? 
  203. array_map('ninja_forms_strip_tags_deep', $value) : 
  204. strip_tags($value); 
  205. return $value; 
  206.  
  207. function ninja_forms_json_response() { 
  208. global $ninja_forms_processing; 
  209.  
  210. $form_id = $ninja_forms_processing->get_form_ID(); 
  211.  
  212. $errors = $ninja_forms_processing->get_all_errors(); 
  213. $success = $ninja_forms_processing->get_all_success_msgs(); 
  214. $fields = $ninja_forms_processing->get_all_fields(); 
  215. $form_settings = $ninja_forms_processing->get_all_form_settings(); 
  216. $extras = $ninja_forms_processing->get_all_extras(); 
  217.  
  218. // Success will default to false if there is not success message. 
  219. if ( ! $success && ! $errors ) $success = true; 
  220.  
  221. if( version_compare( phpversion(), '5.3', '>=' ) ) { 
  222. $json = json_encode( array( 'form_id' => $form_id, 'errors' => $errors, 'success' => $success, 'fields' => $fields, 'form_settings' => $form_settings, 'extras' => $extras ), JSON_HEX_QUOT | JSON_HEX_TAG ); 
  223. }else{ 
  224.  
  225.  
  226. $errors = ninja_forms_html_entity_decode_deep( $errors ); 
  227. $success = ninja_forms_html_entity_decode_deep( $success ); 
  228. $fields = ninja_forms_html_entity_decode_deep( $fields ); 
  229. $form_settings = ninja_forms_html_entity_decode_deep( $form_settings ); 
  230. $extras = ninja_forms_html_entity_decode_deep( $extras ); 
  231.  
  232. $errors = utf8_encode_recursive( $errors ); 
  233. $success = utf8_encode_recursive( $success ); 
  234. $fields = utf8_encode_recursive( $fields ); 
  235. $form_settings = utf8_encode_recursive( $form_settings ); 
  236. $extras = utf8_encode_recursive( $extras ); 
  237.  
  238. $errors = ninja_forms_str_replace_deep( '"', "\u0022", $errors ); 
  239. $errors = ninja_forms_str_replace_deep( "'", "\u0027", $errors ); 
  240. $errors = ninja_forms_str_replace_deep( '<', "\u003C", $errors ); 
  241. $errors = ninja_forms_str_replace_deep( '>', "\u003E", $errors ); 
  242.  
  243. $success = ninja_forms_str_replace_deep( '"', "\u0022", $success ); 
  244. $success = ninja_forms_str_replace_deep( "'", "\u0027", $success ); 
  245. $success = ninja_forms_str_replace_deep( '<', "\u003C", $success ); 
  246. $success = ninja_forms_str_replace_deep( '>', "\u003E", $success ); 
  247.  
  248. $fields = ninja_forms_str_replace_deep( '"', "\u0022", $fields ); 
  249. $fields = ninja_forms_str_replace_deep( "'", "\u0027", $fields ); 
  250. $fields = ninja_forms_str_replace_deep( '<', "\u003C", $fields ); 
  251. $fields = ninja_forms_str_replace_deep( '>', "\u003E", $fields ); 
  252.  
  253. $form_settings = ninja_forms_str_replace_deep( '"', "\u0022", $form_settings ); 
  254. $form_settings = ninja_forms_str_replace_deep( "'", "\u0027", $form_settings ); 
  255. $form_settings = ninja_forms_str_replace_deep( '<', "\u003C", $form_settings ); 
  256. $form_settings = ninja_forms_str_replace_deep( '>', "\u003E", $form_settings ); 
  257.  
  258. $extras = ninja_forms_str_replace_deep( '"', "\u0022", $extras ); 
  259. $extras = ninja_forms_str_replace_deep( "'", "\u0027", $extras ); 
  260. $extras = ninja_forms_str_replace_deep( '<', "\u003C", $extras ); 
  261. $extras = ninja_forms_str_replace_deep( '>', "\u003E", $extras ); 
  262.  
  263. $json = json_encode( array( 'form_id' => $form_id, 'errors' => $errors, 'success' => $success, 'fields' => $fields, 'form_settings' => $form_settings, 'extras' => $extras ) ); 
  264. $json = str_replace( "\\\u0022", "\\u0022", $json ); 
  265. $json = str_replace( "\\\u0027", "\\u0027", $json ); 
  266. $json = str_replace( "\\\u003C", "\\u003C", $json ); 
  267. $json = str_replace( "\\\u003E", "\\u003E", $json ); 
  268.  
  269. return $json; 
  270.  
  271. /** 
  272. * 
  273. * Function that sets up our transient variable. 
  274. * 
  275. * @since 2.2.45 
  276. * @return void 
  277. */ 
  278.  
  279. function ninja_forms_set_transient() { 
  280. global $ninja_forms_processing; 
  281.  
  282. $form_id = $ninja_forms_processing->get_form_ID(); 
  283.  
  284. // Setup our transient variable. 
  285. $cache = array(); 
  286. $cache['form_id'] = $form_id; 
  287. $cache['field_values'] = $ninja_forms_processing->get_all_fields(); 
  288. $cache['form_settings'] = $ninja_forms_processing->get_all_form_settings(); 
  289. $cache['extra_values'] = $ninja_forms_processing->get_all_extras(); 
  290. $all_fields_settings = array(); 
  291. if ( $ninja_forms_processing->get_all_fields() ) { 
  292. foreach ( $ninja_forms_processing->get_all_fields() as $field_id => $user_value ) { 
  293. $field_settings = $ninja_forms_processing->get_field_settings( $field_id ); 
  294. $all_fields_settings[$field_id] = $field_settings; 
  295.  
  296. $cache['field_settings'] = $all_fields_settings; 
  297.  
  298. // Set errors and success messages as Ninja_Forms()->session variables. 
  299. $success = $ninja_forms_processing->get_all_success_msgs(); 
  300. $errors = $ninja_forms_processing->get_all_errors(); 
  301.  
  302. $cache['success_msgs'] = $success; 
  303. $cache['error_msgs'] = $errors; 
  304.  
  305. Ninja_Forms()->session->set( 'nf_cache', $cache ); 
  306.  
  307. /** 
  308. * 
  309. * Function that deletes our cache variable 
  310. * 
  311. * @since 2.2.45 
  312. * @return void 
  313. */ 
  314.  
  315. function ninja_forms_delete_transient() { 
  316. Ninja_Forms()->session->delete(); 
  317.  
  318. /** 
  319. * Get a count of submissions for a form 
  320. * 
  321. * @since 2.7 
  322. * @param int $post_id 
  323. * @return int $count 
  324. */ 
  325. function nf_get_sub_count( $form_id, $post_status = 'publish' ) { 
  326. global $wpdb; 
  327.  
  328. $meta_key = '_form_id'; 
  329. $meta_value = $form_id; 
  330.  
  331. $sql = "SELECT count(DISTINCT pm.post_id) 
  332. FROM $wpdb->postmeta pm 
  333. JOIN $wpdb->posts p ON (p.ID = pm.post_id) 
  334. WHERE pm.meta_key = %s 
  335. AND pm.meta_value = %s 
  336. AND p.post_type = 'nf_sub' 
  337. AND p.post_status = %s"; 
  338.  
  339. $count = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value, $post_status ) ); 
  340.  
  341. return $count; 
  342.  
  343. /** 
  344. * Get an array of our fields by form ID. 
  345. * The returned array has the field_ID as the key. 
  346. * 
  347. * @since 2.7 
  348. * @param int $form_id 
  349. * @return array $tmp_array 
  350. */ 
  351. function nf_get_fields_by_form_id( $form_id, $orderby = 'ORDER BY `order` ASC' ) { 
  352. global $wpdb; 
  353.  
  354. $tmp_array = array(); 
  355. $field_results = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE form_id = %d ".$orderby, $form_id), ARRAY_A); 
  356. if ( is_array( $field_results ) && ! empty( $field_results ) ) { 
  357. foreach ( $field_results as $field ) { 
  358. $field_id = $field['id']; 
  359. $field['data'] = unserialize( $field['data'] ); 
  360. $tmp_array[ $field_id ] = $field; 
  361.  
  362. return $tmp_array; 
.