database.php 13 KB

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