/deprecated/includes/functions.php

  1. <?php if ( ! defined( 'ABSPATH' ) ) exit; 
  2.  
  3. function ninja_forms_return_echo($function_name) { 
  4. $arguments = func_get_args(); 
  5. array_shift($arguments); // We need to remove the first arg ($function_name) 
  6. ob_start(); 
  7. call_user_func_array($function_name, $arguments); 
  8. $return = ob_get_clean(); 
  9. return $return; 
  10.  
  11. function ninja_forms_random_string($length = 10) { 
  12. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
  13. $random_string = ''; 
  14. for ($i = 0; $i < $length; $i++) { 
  15. $random_string .= $characters[rand(0, strlen($characters) - 1)]; 
  16. return $random_string; 
  17.  
  18. function ninja_forms_remove_from_array($arr, $key, $val, $within = FALSE) { 
  19. foreach ($arr as $i => $array) 
  20. if ($within && stripos($array[$key], $val) !== FALSE && (gettype($val) === gettype($array[$key]))) 
  21. unset($arr[$i]); 
  22. elseif ($array[$key] === $val) 
  23. unset($arr[$i]); 
  24.  
  25. return array_values($arr); 
  26.  
  27. function ninja_forms_letters_to_numbers( $size ) { 
  28. $l = substr( $size, -1 ); 
  29. $ret = substr( $size, 0, -1 ); 
  30. switch( strtoupper( $l ) ) { 
  31. case 'P': 
  32. $ret *= 1024; 
  33. case 'T': 
  34. $ret *= 1024; 
  35. case 'G': 
  36. $ret *= 1024; 
  37. case 'M': 
  38. $ret *= 1024; 
  39. case 'K': 
  40. $ret *= 1024; 
  41. return $ret; 
  42.  
  43. function ninja_forms_subval_sort( $a, $subkey ) { 
  44. $b = ''; 
  45. foreach($a as $k=>$v) { 
  46. $b[$k] = strtolower($v[$subkey]); 
  47. if ( is_array ( $b ) ) { 
  48. asort($b); 
  49. foreach($b as $key=>$val) { 
  50. $c[] = $a[$key]; 
  51. return $c; 
  52. } else { 
  53. return $a; 
  54.  
  55.  
  56. /** 
  57. * Takes a field ID and returns the admin label if it exists and the label if it does not. 
  58. * 
  59. * @since 2.8 
  60. * @param int $field_id 
  61. * @return string $label 
  62. */ 
  63. function nf_get_field_admin_label( $field_id, $form_id = '' ) { 
  64.  
  65. if ( empty ( $form_id ) ) { 
  66. $form = ninja_forms_get_form_by_field_id( $field_id ); 
  67. $form_id = $form['id']; 
  68.  
  69. $admin_label = isset( Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['admin_label'] ) ? Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['admin_label'] : ''; 
  70. $field_label = isset( Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['label'] ) ? Ninja_Forms()->form( $form_id )->fields[ $field_id ]['data']['label'] : ''; 
  71.  
  72. if ( ! empty( $admin_label ) ) { 
  73. $label = $admin_label; 
  74. } else { 
  75. $label = $field_label; 
  76.  
  77. return $label; 
  78.  
  79. /** 
  80. * Return the begin date with an added 00:00:00. 
  81. * Checks for the current date format setting and tries to respect it. 
  82. * 
  83. * @since 2.7 
  84. * @param string $begin_date 
  85. * @return string $begin_date 
  86. */ 
  87. function nf_get_begin_date( $begin_date ) { 
  88. $plugin_settings = nf_get_settings(); 
  89.  
  90. if ( isset ( $plugin_settings['date_format'] ) ) { 
  91. $date_format = $plugin_settings['date_format']; 
  92. } else { 
  93. $date_format = 'm/d/Y'; 
  94.  
  95. if ( $date_format == 'd/m/Y' ) { 
  96. $begin_date = str_replace( '/', '-', $begin_date ); 
  97. } else if ( $date_format == 'm-d-Y' ) { 
  98. $begin_date = str_replace( '-', '/', $begin_date ); 
  99. $begin_date .= '00:00:00'; 
  100. $begin_date = new DateTime( $begin_date ); 
  101.  
  102. return $begin_date; 
  103.  
  104. /** 
  105. * Return the end date with an added 23:59:59. 
  106. * Checks for the current date format setting and tries to respect it. 
  107. * 
  108. * @since 2.7 
  109. * @param string $end_date 
  110. * @return string $end_date 
  111. */ 
  112. function nf_get_end_date( $end_date ) { 
  113. $plugin_settings = nf_get_settings(); 
  114.  
  115. if ( isset ( $plugin_settings['date_format'] ) ) { 
  116. $date_format = $plugin_settings['date_format']; 
  117. } else { 
  118. $date_format = 'm/d/Y'; 
  119.  
  120. if ( $date_format == 'd/m/Y' ) { 
  121. $end_date = str_replace( '/', '-', $end_date ); 
  122. } else if ( $date_format == 'm-d-Y' ) { 
  123. $end_date = str_replace( '-', '/', $end_date ); 
  124. $end_date .= '23:59:59'; 
  125. $end_date = new DateTime( $end_date ); 
  126.  
  127. return $end_date; 
  128.  
  129. /** 
  130. * Checks whether function is disabled. 
  131. * 
  132. * @since 2.7 
  133. * 
  134. * @param string $function Name of the function. 
  135. * @return bool Whether or not function is disabled. 
  136. */ 
  137. if( ! function_exists( 'nf_is_func_disabled' ) ) { 
  138. function nf_is_func_disabled($function) 
  139. $disabled = explode(', ', ini_get('disable_functions')); 
  140.  
  141. return in_array($function, $disabled); 
  142.  
  143. /** 
  144. * Acts as a wrapper/alias for nf_get_objects_by_type that is specific to notifications. 
  145. * 
  146. * @since 2.8 
  147. * @return array $notifications 
  148. */ 
  149. function nf_get_all_notifications() { 
  150. return nf_get_objects_by_type( 'notification' ); 
  151.  
  152. /** 
  153. * Acts as a wrapper/alias for nf_get_object_children that is specific to notifications. 
  154. * 
  155. * @since 2.8 
  156. * @param string $form_id 
  157. * @return array $notifications 
  158. */ 
  159.  
  160. function nf_get_notifications_by_form_id( $form_id, $full_data = true ) { 
  161. return nf_get_object_children( $form_id, 'notification', $full_data ); 
  162.  
  163. /** 
  164. * Acts as a wrapper/alias for nf_get_object_meta 
  165. * 
  166. * @since 2.8 
  167. * @param string $id 
  168. * @return array $notification 
  169. */ 
  170.  
  171. function nf_get_notification_by_id( $notification_id ) { 
  172. return nf_get_object_meta( $notification_id ); 
  173.  
  174. /** 
  175. * Insert a notification into the database. 
  176. * 
  177. * Calls nf_insert_object() 
  178. * Calls nf_add_relationship() 
  179. * Calls nf_update_object_meta() 
  180. * 
  181. * @since 2.8 
  182. * @param int $form_id 
  183. * @return int $n_id 
  184. */ 
  185.  
  186. function nf_insert_notification( $form_id = '' ) { 
  187. if ( empty ( $form_id ) ) 
  188. return false; 
  189.  
  190. $n_id = nf_insert_object( 'notification' ); 
  191. nf_add_relationship( $n_id, 'notification', $form_id, 'form' ); 
  192. $date_updated = date( 'Y-m-d', current_time( 'timestamp' ) ); 
  193. nf_update_object_meta( $n_id, 'date_updated', $date_updated ); 
  194. return $n_id; 
  195.  
  196. /** 
  197. * Delete a notification. 
  198. * 
  199. * Acts as a wrapper/alias for nf_delete_object 
  200. * 
  201. * @since 2.8 
  202. * @param int $n_id 
  203. * @return void 
  204. */ 
  205.  
  206. function nf_delete_notification( $n_id ) { 
  207. nf_delete_object( $n_id ); 
  208.  
  209.  
  210. /** 
  211. * Function that gets a piece of object meta 
  212. * 
  213. * @since 2.8 
  214. * @param string $object_id 
  215. * @param string $meta_key 
  216. * @return var $meta_value 
  217. */ 
  218.  
  219. function nf_get_object_meta_value( $object_id, $meta_key ) { 
  220. global $wpdb; 
  221.  
  222. $meta_value = $wpdb->get_row( $wpdb->prepare( 'SELECT meta_value FROM ' . NF_OBJECT_META_TABLE_NAME . ' WHERE object_id = %d AND meta_key = %s', $object_id, $meta_key ), ARRAY_A ); 
  223. if ( is_array ( $meta_value['meta_value'] ) ) { 
  224. $meta_value['meta_value'] = unserialize( $meta_value['meta_value'] ); 
  225.  
  226. return $meta_value['meta_value']; 
  227.  
  228.  
  229. /** 
  230. * Function that gets children objects by type and parent id 
  231. * 
  232. * @since 2.8 
  233. * @param string $parent_id 
  234. * @param string $type 
  235. * @return array $children 
  236. */ 
  237.  
  238. function nf_get_object_children( $object_id, $child_type = '', $full_data = true, $include_forms = true ) { 
  239. global $wpdb; 
  240.  
  241.  
  242. if ( $include_forms ) { 
  243. if ( $child_type != '' ) { 
  244. $children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE child_type = %s AND parent_id = %d", $child_type, $object_id ), ARRAY_A); 
  245. } else { 
  246. $children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = %d", $object_id ), ARRAY_A); 
  247. } else { 
  248. if ( $child_type != '' ) { 
  249. $children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE child_type = %s AND parent_id = %d AND parent_type <> 'form'", $child_type, $object_id ), ARRAY_A); 
  250. } else { 
  251. $children = $wpdb->get_results( $wpdb->prepare( "SELECT child_id FROM " . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . " WHERE parent_id = %d AND parent_type <> 'form'", $object_id ), ARRAY_A); 
  252.  
  253. $tmp_array = array(); 
  254. if ( $full_data ) { 
  255. foreach( $children as $id ) { 
  256. $child_id = $id['child_id']; 
  257. $settings = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM " . NF_OBJECT_META_TABLE_NAME . " WHERE object_id = %d", $child_id ), ARRAY_A); 
  258. if ( ! empty( $settings ) ) { 
  259. foreach ( $settings as $s ) { 
  260. if ( is_array ( $s['meta_value'] ) ) { 
  261. $s['meta_value'] = unserialize( $s['meta_value'] ); 
  262. $tmp_array[ $child_id ][ $s['meta_key'] ] = $s['meta_value']; 
  263. } else { 
  264. $tmp_array[ $child_id ] = array(); 
  265.  
  266.  
  267. } else { 
  268. if ( is_array( $children ) ) { 
  269. foreach ( $children as $child ) { 
  270. $tmp_array[] = $child['child_id']; 
  271.  
  272. return $tmp_array; 
  273.  
  274. /** 
  275. * Function that updates a piece of object meta 
  276. * 
  277. * @since 3.0 
  278. * @param string $object_id 
  279. * @param string $meta_key 
  280. * @param string $meta_value 
  281. * @return string $meta_id 
  282. */ 
  283.  
  284. function nf_update_object_meta( $object_id, $meta_key, $meta_value ) { 
  285. global $wpdb; 
  286.  
  287. if ( is_array( $meta_value ) ) { 
  288. $meta_value = serialize( $meta_value ); 
  289.  
  290. // Check to see if this meta_key/meta_value pair exist for this object_id. 
  291. $found = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM ".NF_OBJECT_META_TABLE_NAME." WHERE object_id = %d AND meta_key = %s", $object_id, $meta_key ), ARRAY_A ); 
  292.  
  293. if ( $found ) { 
  294. $wpdb->update( NF_OBJECT_META_TABLE_NAME, array( 'meta_value' => $meta_value ), array( 'meta_key' => $meta_key, 'object_id' => $object_id ) ); 
  295. $meta_id = $found['id']; 
  296. } else { 
  297. $wpdb->insert( NF_OBJECT_META_TABLE_NAME, array( 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $meta_value ) ); 
  298. $meta_id = $wpdb->insert_id; 
  299.  
  300. return $meta_id; 
  301.  
  302. /** 
  303. * Function that gets all the meta values attached to a given object. 
  304. * 
  305. * @since 2.8 
  306. * @param string $object 
  307. * @return array $settings 
  308. */ 
  309. function nf_get_object_meta( $object_id ) { 
  310. global $wpdb; 
  311.  
  312. $tmp_array = array(); 
  313. $settings = $wpdb->get_results( $wpdb->prepare( 'SELECT meta_key, meta_value FROM ' . NF_OBJECT_META_TABLE_NAME . ' WHERE object_id = %d', $object_id ), ARRAY_A); 
  314.  
  315. if ( is_array( $settings ) ) { 
  316. foreach( $settings as $setting ) { 
  317. $tmp_array[ $setting['meta_key'] ] = $setting['meta_value'] = maybe_unserialize( $setting['meta_value'] ); 
  318.  
  319. return $tmp_array; 
  320.  
  321. /** 
  322. * Insert an object. 
  323. * 
  324. * @since 3.0 
  325. * @param string $type 
  326. * @return int $object_id 
  327. */ 
  328.  
  329. function nf_insert_object( $type, $id = NULL ) { 
  330. global $wpdb; 
  331. $wpdb->insert( NF_OBJECTS_TABLE_NAME, array( 'id' => $id, 'type' => $type ) ); 
  332. return $wpdb->insert_id; 
  333.  
  334. /** 
  335. * Delete an object. Also removes all of the objectmeta attached to the object and any references to it in the relationship table. 
  336. * 
  337. * @since 2.8 
  338. * @param int $object_id 
  339. * @return bool 
  340. */ 
  341.  
  342. function nf_delete_object( $object_id ) { 
  343. global $wpdb; 
  344.  
  345. // Check to see if we have any object children. 
  346. $children = nf_get_object_children( $object_id, '', false, false ); 
  347.  
  348. foreach ( $children as $child_id ) { 
  349. nf_delete_object( $child_id ); 
  350.  
  351. // Delete this object. 
  352. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . NF_OBJECTS_TABLE_NAME .' WHERE id = %d', $object_id ) ); 
  353.  
  354. // Delete any objectmeta attached to this object. 
  355. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . NF_OBJECT_META_TABLE_NAME .' WHERE object_id = %d', $object_id ) ); 
  356.  
  357. // Delete any references to this object in the relationship table 
  358. $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . NF_OBJECT_RELATIONSHIPS_TABLE_NAME .' WHERE child_id = %d OR parent_id = %d', $object_id, $object_id ) ); 
  359.  
  360. return true; 
  361.  
  362.  
  363. /** 
  364. * Create a relationship between two objects 
  365. * 
  366. * @since 2.8 
  367. * @param int $child_id 
  368. * @param string child_type 
  369. * @param int $parent_id 
  370. * @param string $parent_type 
  371. * @return void 
  372. */ 
  373.  
  374. function nf_add_relationship( $child_id, $child_type, $parent_id, $parent_type ) { 
  375. global $wpdb; 
  376. // Make sure that our relationship doesn't already exist. 
  377. $count = $wpdb->query( $wpdb->prepare( 'SELECT id FROM ' . NF_OBJECT_RELATIONSHIPS_TABLE_NAME .' WHERE child_id = %d AND parent_id = %d', $child_id, $parent_id ), ARRAY_A ); 
  378. if ( empty( $count ) ) { 
  379. $wpdb->insert( NF_OBJECT_RELATIONSHIPS_TABLE_NAME, array( 'child_id' => $child_id, 'child_type' => $child_type, 'parent_id' => $parent_id, 'parent_type' => $parent_type ) ); 
  380.  
  381. /** 
  382. * Get an object's parent 
  383. * 
  384. * @since 2.8 
  385. * @param int $child_id 
  386. * @return int $parent_id 
  387. */ 
  388.  
  389. function nf_get_object_parent( $child_id ) { 
  390. global $wpdb; 
  391. // Check our relationship table for where this ID appears as a child. 
  392. $parent = $wpdb->get_row( $wpdb->prepare( 'SELECT parent_id FROM ' . NF_OBJECT_RELATIONSHIPS_TABLE_NAME . ' WHERE child_id = %d', $child_id ), ARRAY_A ); 
  393. return $parent['parent_id']; 
  394.  
  395. /** 
  396. * Get an object's type 
  397. * 
  398. * @since 2.8.6 
  399. * @param $object_id 
  400. * @return string $return 
  401. */ 
  402.  
  403. function nf_get_object_type( $object_id ) { 
  404. global $wpdb; 
  405. // Get our object type 
  406. $type = $wpdb->get_row( $wpdb->prepare( 'SELECT type FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE id = %d', $object_id ), ARRAY_A ); 
  407. $return = ( isset ( $type['type'] ) ) ? $type['type'] : false; 
  408. return $return; 
  409.  
  410. /** 
  411. * Get User IP 
  412. * 
  413. * Returns the IP address of the current visitor 
  414. * 
  415. * @since 2.8 
  416. * @return string $ip User's IP address 
  417. */ 
  418. function nf_get_ip() { 
  419.  
  420. $ip = '127.0.0.1'; 
  421.  
  422. if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { 
  423. //check ip from share internet 
  424. $ip = $_SERVER['HTTP_CLIENT_IP']; 
  425. } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { 
  426. //to check ip is pass from proxy 
  427. $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
  428. } elseif( ! empty( $_SERVER['REMOTE_ADDR'] ) ) { 
  429. $ip = $_SERVER['REMOTE_ADDR']; 
  430. return apply_filters( 'nf_get_ip', $ip ); 
  431.  
  432.  
  433. /** 
  434. * Function that gets all objects of a given type. 
  435. * 
  436. * @since 2.8 
  437. * @return array $results 
  438. */ 
  439.  
  440. function nf_get_objects_by_type( $object_type ) { 
  441. global $wpdb; 
  442.  
  443. // Bail if we don't have an object type. 
  444. if ( $object_type == '' ) 
  445. return false; 
  446.  
  447. $results = $wpdb->get_results( $wpdb->prepare( 'SELECT id FROM ' . NF_OBJECTS_TABLE_NAME . ' WHERE type = %s', $object_type ), ARRAY_A ); 
  448.  
  449. return $results; 
  450.  
  451. /** 
  452. * Add filters so that users given the ability to see the "All Forms" table and the add new form page 
  453. * can add new fields and delete forms. 
  454. * 
  455. * @since 2.8.6 
  456. * @return void 
  457. */ 
  458. function nf_add_permissions_filters( $cap ) { 
  459. return apply_filters( 'ninja_forms_admin_all_forms_capabilities', $cap ); 
  460.  
  461. add_filter( 'nf_new_field_capabilities', 'nf_add_permissions_filters' ); 
  462. add_filter( 'nf_delete_field_capabilities', 'nf_add_permissions_filters' ); 
  463. add_filter( 'nf_delete_form_capabilities', 'nf_add_permissions_filters' ); 
  464.  
  465. function nf_admin_footer_text( $footer_text ) { 
  466. global $current_screen, $pagenow, $typenow; 
  467.  
  468. $current_tab = ninja_forms_get_current_tab(); 
  469.  
  470. // only display custom text on Ninja Admin Pages 
  471. if ( isset( $current_screen->id ) && strpos( $current_screen->id, 'ninja' ) !== false || ( ( $pagenow == 'edit.php' || $pagenow == 'post.php' ) && $typenow == 'nf_sub' ) ) { 
  472. $footer_text = sprintf( __( 'Please rate %sNinja Forms%s %s on %sWordPress.org%s to help us keep this plugin free. Thank you from the WP Ninjas team!', 'ninja-forms' ), '<strong>', '</strong>', '<a href="http://wordpress.org/support/view/plugin-reviews/ninja-forms?filter=5" target="_blank">★★★★★</a>', '<a href="http://wordpress.org/support/view/plugin-reviews/ninja-forms?filter=5" target="_blank">', '</a>' ); 
  473.  
  474. if ( 'builder' == $current_tab ) { 
  475. $footer_text = ''; 
  476.  
  477. return $footer_text; 
  478.  
  479. add_filter( 'admin_footer_text', 'nf_admin_footer_text' , 1, 2 ); 
.