functions.php 15 KB

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