GFAPI

API for standard Gravity Forms functionality.

Defined (1)

The class is defined in the following location(s).

/includes/api.php  
  1. class GFAPI { 
  2.  
  3. // FORMS ---------------------------------------------------- 
  4.  
  5. /** 
  6. * Returns the form object for a given Form ID 
  7. * @since 1.8 
  8. * @access public 
  9. * @static 
  10. * @param int $form_id The ID of the Form 
  11. * @return mixed The form meta array or false 
  12. */ 
  13. public static function get_form( $form_id ) { 
  14.  
  15. $form_id = absint( $form_id ); 
  16.  
  17. $form = GFFormsModel::get_form_meta( $form_id ); 
  18. if ( ! $form ) { 
  19. return false; 
  20.  
  21. //loading form columns into meta 
  22. $form_info = GFFormsModel::get_form( $form_id, true ); 
  23. $form['is_active'] = $form_info->is_active; 
  24. $form['date_created'] = $form_info->date_created; 
  25. $form['is_trash'] = $form_info->is_trash; 
  26.  
  27. return $form; 
  28.  
  29.  
  30. /** 
  31. * Returns all the form objects 
  32. * @since 1.8.11.5 
  33. * @access public 
  34. * @static 
  35. * @param bool $active 
  36. * @param bool $trash 
  37. * @return mixed The array of Forms 
  38. */ 
  39. public static function get_forms( $active = true, $trash = false ) { 
  40.  
  41. $form_ids = GFFormsModel::get_form_ids( $active, $trash ); 
  42. if ( empty( $form_ids ) ) { 
  43. return array(); 
  44.  
  45. $forms = array(); 
  46. foreach ( $form_ids as $form_id ) { 
  47. $forms[] = GFAPI::get_form( $form_id ); 
  48.  
  49. return $forms; 
  50.  
  51. /** 
  52. * Deletes the forms with the given Form IDs 
  53. * @since 1.8 
  54. * @access public 
  55. * @static 
  56. * @param array $form_ids An array of form IDs to delete 
  57. */ 
  58. public static function delete_forms( $form_ids ) { 
  59.  
  60. GFFormsModel::delete_forms( $form_ids ); 
  61.  
  62. /** 
  63. * Deletes the form with the given Form ID 
  64. * @since 1.8 
  65. * @access public 
  66. * @static 
  67. * @param int $form_id The ID of the Form to delete 
  68. * @return mixed True for success, or a WP_Error instance 
  69. */ 
  70. public static function delete_form( $form_id ) { 
  71. $form = self::get_form( $form_id ); 
  72. if ( empty( $form ) ) { 
  73. return new WP_Error( 'not_found', sprintf( __( 'Form with id: %s not found', 'gravityforms' ), $form_id ), $form_id ); 
  74. self::delete_forms( array( $form_id ) ); 
  75.  
  76. return true; 
  77.  
  78. /** 
  79. * Updates the forms with an array of form objects 
  80. * @since 1.8 
  81. * @access public 
  82. * @static 
  83. * @param array $forms The Form objects 
  84. * @return mixed True for success, or a WP_Error instance 
  85. */ 
  86. public static function update_forms( $forms ) { 
  87.  
  88. foreach ( $forms as $form ) { 
  89. $result = self::update_form( $form ); 
  90. if ( is_wp_error( $result ) ) { 
  91. return $result; 
  92.  
  93. return true; 
  94.  
  95. /** 
  96. * Updates the form with a given form object. 
  97. * @since 1.8 
  98. * @access public 
  99. * @static 
  100. * @param array $form The Form object 
  101. * @param int $form_id Optional. If specified, then the ID in the Form object will be ignored 
  102. * @return mixed True for success, or a WP_Error instance 
  103. */ 
  104. public static function update_form( $form, $form_id = null ) { 
  105. global $wpdb; 
  106.  
  107. if ( ! $form ) { 
  108. return new WP_Error( 'invalid', __( 'Invalid form object', 'gravityforms' ) ); 
  109.  
  110. $form_table_name = $wpdb->prefix . 'rg_form'; 
  111. if ( empty( $form_id ) ) { 
  112. $form_id = $form['id']; 
  113. } else { 
  114. // make sure the form object has the right form id 
  115. $form['id'] = $form_id; 
  116. if ( isset( $form['fields'] ) ) { 
  117. foreach ( $form['fields'] as &$field ) { 
  118. if ( $field instanceof GF_Field ) { 
  119. $field->formId = $form_id; 
  120. } else { 
  121. $field['formId'] = $form_id; 
  122.  
  123. if ( empty( $form_id ) ) { 
  124. return new WP_Error( 'missing_form_id', __( 'Missing form id', 'gravityforms' ) ); 
  125.  
  126. $meta_table_name = GFFormsModel::get_meta_table_name(); 
  127.  
  128. if ( intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(0) FROM {$meta_table_name} WHERE form_id=%d", $form_id ) ) ) == 0 ) { 
  129. return new WP_Error( 'not_found', __( 'Form not found', 'gravityforms' ) ); 
  130.  
  131. // Strip confirmations and notifications 
  132. $form_display_meta = $form; 
  133. unset( $form_display_meta['confirmations'] ); 
  134. unset( $form_display_meta['notifications'] ); 
  135.  
  136. $result = GFFormsModel::update_form_meta( $form_id, $form_display_meta ); 
  137. if ( false === $result ) { 
  138. return new WP_Error( 'error_updating_form', __( 'Error updating form', 'gravityforms' ), $wpdb->last_error ); 
  139.  
  140. if ( isset( $form['confirmations'] ) && is_array( $form['confirmations'] ) ) { 
  141. $result = GFFormsModel::update_form_meta( $form_id, $form['confirmations'], 'confirmations' ); 
  142. if ( false === $result ) { 
  143. return new WP_Error( 'error_updating_confirmations', __( 'Error updating form confirmations', 'gravityforms' ), $wpdb->last_error ); 
  144.  
  145. if ( isset( $form['notifications'] ) && is_array( $form['notifications'] ) ) { 
  146. $result = GFFormsModel::update_form_meta( $form_id, $form['notifications'], 'notifications' ); 
  147. if ( false === $result ) { 
  148. return new WP_Error( 'error_updating_notifications', __( 'Error updating form notifications', 'gravityforms' ), $wpdb->last_error ); 
  149.  
  150. //updating form title and is_active flag 
  151. $is_active = rgar( $form, 'is_active' ) ? '1' : '0'; 
  152. $result = $wpdb->query( $wpdb->prepare( "UPDATE {$form_table_name} SET title=%s, is_active=%s WHERE id=%d", $form['title'], $is_active, $form['id'] ) ); 
  153. if ( false === $result ) { 
  154. return new WP_Error( 'error_updating_title', __( 'Error updating title', 'gravityforms' ), $wpdb->last_error ); 
  155.  
  156. return true; 
  157.  
  158. /** 
  159. * Updates a form property - a column in the main forms table. e.g. is_trash, is_active, title 
  160. * @since 1.8.3.15 
  161. * @access public 
  162. * @static 
  163. * @param array $form_ids The IDs of the forms to update 
  164. * @param array $property_key The name of the column in the database e.g. is_trash, is_active, title 
  165. * @param array $value The new value 
  166. * @return mixed Either a WP_Error instance or the result of the query 
  167. */ 
  168. public static function update_forms_property( $form_ids, $property_key, $value ) { 
  169. global $wpdb; 
  170. $table = GFFormsModel::get_form_table_name(); 
  171.  
  172. $db_columns = GFFormsModel::get_form_db_columns(); 
  173.  
  174. if ( ! in_array( strtolower( $property_key ), $db_columns ) ) { 
  175. return new WP_Error( 'property_key_incorrect', __( 'Property key incorrect', 'gravityforms' ) ); 
  176.  
  177. $value = esc_sql( $value ); 
  178. if ( ! is_numeric( $value ) ) { 
  179. $value = sprintf( "'%s'", $value ); 
  180. $in_str_arr = array_fill( 0, count( $form_ids ), '%d' ); 
  181. $in_str = join( $in_str_arr, ', ' ); 
  182. $result = $wpdb->query( 
  183. $wpdb->prepare( 
  184. UPDATE $table 
  185. SET {$property_key} = {$value} 
  186. WHERE id IN ($in_str) 
  187. ", $form_ids 
  188. ); 
  189.  
  190. return $result; 
  191.  
  192. /** 
  193. * Updates the property of one form - columns in the main forms table. e.g. is_trash, is_active, title 
  194. * @since 1.8.3.15 
  195. * @access public 
  196. * @static 
  197. * @param array|int $form_id The ID of the forms to update 
  198. * @param string $property_key The name of the column in the database e.g. is_trash, is_active, title 
  199. * @param string $value The new value 
  200. * @return mixed Either a WP_Error instance or the result of the query 
  201. */ 
  202. public static function update_form_property( $form_id, $property_key, $value ) { 
  203. return self::update_forms_property( array( $form_id ), $property_key, $value ); 
  204.  
  205.  
  206. /** 
  207. * Adds multiple form objects. 
  208. * @since 1.8 
  209. * @access public 
  210. * @static 
  211. * @param array $forms The Form objects 
  212. * @return mixed Either an array of new form IDs or a WP_Error instance 
  213. */ 
  214. public static function add_forms( $forms ) { 
  215.  
  216. if ( ! $forms || ! is_array( $forms ) ) { 
  217. return new WP_Error( 'invalid', __( 'Invalid form objects', 'gravityforms' ) ); 
  218. $form_ids = array(); 
  219. foreach ( $forms as $form ) { 
  220. $result = self::add_form( $form ); 
  221. if ( is_wp_error( $result ) ) { 
  222. return $result; 
  223. $form_ids[] = $result; 
  224.  
  225. return $form_ids; 
  226.  
  227. /** 
  228. * Adds a new form using the given Form object. Warning, little checking is done to make sure it's a valid Form object. 
  229. * @since 1.8 
  230. * @access public 
  231. * @static 
  232. * @param array $form_meta The Form object 
  233. * @return mixed Either the new Form ID or a WP_Error instance 
  234. */ 
  235. public static function add_form( $form_meta ) { 
  236. global $wpdb; 
  237.  
  238. if ( ! $form_meta || ! is_array( $form_meta ) ) { 
  239. return new WP_Error( 'invalid', __( 'Invalid form object', 'gravityforms' ) ); 
  240.  
  241. if ( rgar( $form_meta, 'title' ) == '' ) { 
  242. return new WP_Error( 'missing_title', __( 'The form title is missing', 'gravityforms' ) ); 
  243. //Making sure title is not duplicate 
  244. $title = $form_meta['title']; 
  245. $count = 2; 
  246. while ( ! RGFormsModel::is_unique_title( $title ) ) { 
  247. $title = $form_meta['title'] . "($count)"; 
  248. $count ++; 
  249.  
  250. //inserting form 
  251. $form_id = RGFormsModel::insert_form( $title ); 
  252.  
  253. //updating form meta 
  254. $form_meta['title'] = $title; 
  255.  
  256. //updating object's id property 
  257. $form_meta['id'] = $form_id; 
  258.  
  259. if ( isset( $form_meta['confirmations'] ) ) { 
  260. $form_meta['confirmations'] = self::set_property_as_key( $form_meta['confirmations'], 'id' ); 
  261. GFFormsModel::update_form_meta( $form_id, $form_meta['confirmations'], 'confirmations' ); 
  262. unset( $form_meta['confirmations'] ); 
  263.  
  264. if ( isset( $form_meta['notifications'] ) ) { 
  265. $form_meta['notifications'] = self::set_property_as_key( $form_meta['notifications'], 'id' ); 
  266. GFFormsModel::update_form_meta( $form_id, $form_meta['notifications'], 'notifications' ); 
  267. unset( $form_meta['notifications'] ); 
  268.  
  269. //updating form meta 
  270. $result = GFFormsModel::update_form_meta( $form_id, $form_meta ); 
  271.  
  272. if ( false === $result ) { 
  273. return new WP_Error( 'insert_form_error', __( 'There was a problem while inserting the form', 'gravityforms' ), $wpdb->last_error ); 
  274.  
  275. return $form_id; 
  276.  
  277. /** 
  278. * Private. 
  279. * @since 1.8 
  280. * @access private 
  281. * @static 
  282. * @ignore 
  283. */ 
  284. private static function set_property_as_key( $array, $property ) { 
  285. $new_array = array(); 
  286. foreach ( $array as $item ) { 
  287. $new_array[ $item[ $property ] ] = $item; 
  288.  
  289. return $new_array; 
  290.  
  291. // ENTRIES ---------------------------------------------------- 
  292.  
  293. /** 
  294. * Returns an array of Entry objects for the given search criteria. The search criteria array is constructed as follows: 
  295. * Filter by status 
  296. * $search_criteria['status'] = 'active'; 
  297. * Filter by date range 
  298. * $search_criteria['start_date'] = $start_date; 
  299. * $search_criteria['end_date'] = $end_date; 
  300. * Filter by any column in the main table 
  301. * $search_criteria['field_filters'][] = array("key" => 'currency', value => 'USD'); 
  302. * $search_criteria['field_filters'][] = array("key" => 'is_read', value => true); 
  303. * Filter by Field Values 
  304. * $search_criteria['field_filters'][] = array('key' => '1', 'value' => 'gquiz159982170'); 
  305. * Filter Operators 
  306. * Supported operators for scalar values: is/=, isnot/<>, contains 
  307. * $search_criteria['field_filters'][] = array('key' => '1', 'operator' => 'contains', value' => 'Steve'); 
  308. * Supported operators for array values: in/=, not in/<>/!= 
  309. * $search_criteria['field_filters'][] = array('key' => '1', 'operator' => 'not in', value' => array( 'Alex', 'David', 'Dana' ); 
  310. * Filter by a checkbox value (not recommended) 
  311. * $search_criteria['field_filters'][] = array('key' => '2.2', 'value' => 'gquiz246fec995'); 
  312. * note: this will work for checkboxes but it won't work if the checkboxes have been re-ordered - best to use the following examples below 
  313. * Filter by a checkbox value (recommended) 
  314. * $search_criteria['field_filters'][] = array('key' => '2', 'value' => 'gquiz246fec995'); 
  315. * $search_criteria['field_filters'][] = array('key' => '2', 'operator' => 'not in', value' => array( 'First Choice', 'Third Choice' ); 
  316. * Filter by a global search of values of any form field 
  317. * $search_criteria['field_filters'][] = array('value' => $search_value); 
  318. * OR 
  319. * $search_criteria['field_filters'][] = array('key' => 0, 'value' => $search_value); 
  320. * Filter entries by Entry meta (added using the gform_entry_meta hook) 
  321. * $search_criteria['field_filters'][] = array('key' => 'gquiz_score', 'value' => '1'); 
  322. * $search_criteria['field_filters'][] = array('key' => 'gquiz_is_pass', 'value' => '1'); 
  323. * Filter by ALL / ANY of the field filters 
  324. * $search_criteria['field_filters']['mode'] = 'all'; // default 
  325. * $search_criteria['field_filters']['mode'] = 'any'; 
  326. * Sorting: column, field or entry meta 
  327. * $sorting = array('key' => $sort_field, 'direction' => 'ASC' ); 
  328. * Paging 
  329. * $paging = array('offset' => 0, 'page_size' => 20 ); 
  330. * @since 1.8 
  331. * @access public 
  332. * @static 
  333. * @param int|array $form_ids The ID of the form or an array IDs of the Forms. Zero for all forms. 
  334. * @param array $search_criteria Optional. An array containing the search criteria 
  335. * @param array $sorting Optional. An array containing the sorting criteria 
  336. * @param array $paging Optional. An array containing the paging criteria 
  337. * @param int $total_count Optional. An output parameter containing the total number of entries. Pass a non-null value to get the total count. 
  338. * @return mixed Either an array of the Entry objects or a WP_Error instance 
  339. */ 
  340. public static function get_entries( $form_ids, $search_criteria = array(), $sorting = null, $paging = null, &$total_count = null ) { 
  341.  
  342. if ( empty( $sorting ) ) { 
  343. $sorting = array( 'key' => 'id', 'direction' => 'DESC', 'is_numeric' => true ); 
  344.  
  345.  
  346. $entries = GFFormsModel::search_leads( $form_ids, $search_criteria, $sorting, $paging ); 
  347.  
  348. if ( ! is_null( $total_count ) ) { 
  349. $total_count = self::count_entries( $form_ids, $search_criteria ); 
  350.  
  351.  
  352. return $entries; 
  353.  
  354. /** 
  355. * Returns the total number of entries for the given search criteria. See get_entries() for examples of the search criteria. 
  356. * @since 1.8 
  357. * @access public 
  358. * @static 
  359. * @param int|array $form_ids The ID of the Form or an array of Form IDs 
  360. * @param array $search_criteria Optional. An array containing the search criteria 
  361. * @return int The total count 
  362. */ 
  363. public static function count_entries( $form_ids, $search_criteria = array() ) { 
  364. return GFFormsModel::count_search_leads( $form_ids, $search_criteria ); 
  365.  
  366. /** 
  367. * Returns the Entry object for a given Entry ID 
  368. * @since 1.8 
  369. * @access public 
  370. * @static 
  371. * @param int $entry_id The ID of the Entry 
  372. * @return mixed The Entry object or a WP_Error instance 
  373. */ 
  374. public static function get_entry( $entry_id ) { 
  375.  
  376. $search_criteria['field_filters'][] = array( 'key' => 'id', 'value' => $entry_id ); 
  377.  
  378. $paging = array( 'offset' => 0, 'page_size' => 1 ); 
  379. $entries = self::get_entries( 0, $search_criteria, null, $paging ); 
  380.  
  381. if ( empty( $entries ) ) { 
  382. return new WP_Error( 'not_found', sprintf( __( 'Entry with id %s not found', 'gravityforms' ), $entry_id ), $entry_id ); 
  383.  
  384. return $entries[0]; 
  385.  
  386. /** 
  387. * Adds multiple Entry objects. 
  388. * @since 1.8 
  389. * @access public 
  390. * @static 
  391. * @param array $entries The Entry objects 
  392. * @param int $form_id Optional. If specified, the form_id in the Entry objects will be ignored 
  393. * @return mixed Either an array of new Entry IDs or a WP_Error instance 
  394. */ 
  395. public static function add_entries( $entries, $form_id = null ) { 
  396.  
  397. $entry_ids = array(); 
  398. foreach ( $entries as $entry ) { 
  399. if ( $form_id ) { 
  400. $entry['form_id'] = $form_id; 
  401. $result = self::add_entry( $entry ); 
  402. if ( is_wp_error( $result ) ) { 
  403. return $result; 
  404. $entry_ids[] = $result; 
  405.  
  406. return $entry_ids; 
  407.  
  408. /** 
  409. * Updates multiple Entry objects. 
  410. * @since 1.8 
  411. * @access public 
  412. * @static 
  413. * @param array $entries The Entry objects 
  414. * @return mixed Either True for success, or a WP_Error instance 
  415. */ 
  416. public static function update_entries( $entries ) { 
  417.  
  418. foreach ( $entries as $entry ) { 
  419. $entry_id = rgar( $entry, 'id' ); 
  420. GFCommon::log_debug( 'Updating entry ' . $entry_id ); 
  421. $result = self::update_entry( $entry, $entry_id ); 
  422. if ( is_wp_error( $result ) ) { 
  423. return $result; 
  424.  
  425. return true; 
  426.  
  427. /** 
  428. * Updates a single Entry object. 
  429. * @since 1.8 
  430. * @access public 
  431. * @static 
  432. * @param array $entry The Entry object 
  433. * @param int $entry_id Optional. If specified, the ID in the Entry object will be ignored 
  434. * @return mixed Either True or a WP_Error instance 
  435. */ 
  436. public static function update_entry( $entry, $entry_id = null ) { 
  437. global $wpdb; 
  438.  
  439. if ( empty( $entry_id ) ) { 
  440. if ( rgar( $entry, 'id' ) ) { 
  441. $entry_id = absint( $entry['id'] ); 
  442. } else { 
  443. $entry['id'] = absint( $entry_id ); 
  444.  
  445. if ( empty( $entry_id ) ) { 
  446. return new WP_Error( 'missing_entry_id', __( 'Missing entry id', 'gravityforms' ) ); 
  447.  
  448. $current_entry = $original_entry = self::get_entry( $entry_id ); 
  449.  
  450. if ( ! $current_entry ) { 
  451. return new WP_Error( 'not_found', __( 'Entry not found', 'gravityforms' ), $entry_id ); 
  452.  
  453. if ( is_wp_error( $current_entry ) ) { 
  454. return $current_entry; 
  455.  
  456. // make sure the form id exists 
  457. $form_id = rgar( $entry, 'form_id' ); 
  458. if ( empty( $form_id ) ) { 
  459. $form_id = rgar( $current_entry, 'form_id' ); 
  460.  
  461. if ( false === self::form_id_exists( $form_id ) ) { 
  462. return new WP_Error( 'invalid_form_id', __( 'The form for this entry does not exist', 'gravityforms' ) ); 
  463.  
  464.  
  465. $entry = apply_filters( 'gform_entry_pre_update', $entry, $original_entry ); 
  466.  
  467. // use values in the entry object if present 
  468. $post_id = isset( $entry['post_id'] ) ? intval( $entry['post_id'] ) : 'NULL'; 
  469. $date_created = isset( $entry['date_created'] ) ? sprintf( "'%s'", esc_sql( $entry['date_created'] ) ) : 'utc_timestamp()'; 
  470. $is_starred = isset( $entry['is_starred'] ) ? $entry['is_starred'] : 0; 
  471. $is_read = isset( $entry['is_read'] ) ? $entry['is_read'] : 0; 
  472. $ip = isset( $entry['ip'] ) ? $entry['ip'] : GFFormsModel::get_ip(); 
  473. $source_url = isset( $entry['source_url'] ) ? $entry['source_url'] : GFFormsModel::get_current_page_url(); 
  474. $user_agent = isset( $entry['user_agent'] ) ? $entry['user_agent'] : 'API'; 
  475. $currency = isset( $entry['currency'] ) ? $entry['currency'] : GFCommon::get_currency(); 
  476. $payment_status = isset( $entry['payment_status'] ) ? sprintf( "'%s'", esc_sql( $entry['payment_status'] ) ) : 'NULL'; 
  477. $payment_date = strtotime( rgar( $entry, 'payment_date' ) ) ? "'" . gmdate( 'Y-m-d H:i:s', strtotime( "{$entry['payment_date']}" ) ) . "'" : 'NULL'; 
  478. $payment_amount = isset( $entry['payment_amount'] ) ? (float) $entry['payment_amount'] : 'NULL'; 
  479. $payment_method = isset( $entry['payment_method'] ) ? $entry['payment_method'] : ''; 
  480. $transaction_id = isset( $entry['transaction_id'] ) ? sprintf( "'%s'", esc_sql( $entry['transaction_id'] ) ) : 'NULL'; 
  481. $is_fulfilled = isset( $entry['is_fulfilled'] ) ? intval( $entry['is_fulfilled'] ) : 'NULL'; 
  482. $status = isset( $entry['status'] ) ? $entry['status'] : 'active'; 
  483.  
  484. global $current_user; 
  485. $user_id = isset( $entry['created_by'] ) ? absint( $entry['created_by'] ) : ''; 
  486. if ( empty( $user_id ) ) { 
  487. $user_id = $current_user && $current_user->ID ? absint( $current_user->ID ) : 'NULL'; 
  488.  
  489. $transaction_type = isset( $entry['transaction_type'] ) ? intval( $entry['transaction_type'] ) : 'NULL'; 
  490.  
  491. $lead_table = GFFormsModel::get_lead_table_name(); 
  492. $sql = $wpdb->prepare( 
  493. UPDATE $lead_table 
  494. SET 
  495. form_id = %d,  
  496. post_id = {$post_id},  
  497. date_created = {$date_created},  
  498. is_starred = %d,  
  499. is_read = %d,  
  500. ip = %s,  
  501. source_url = %s,  
  502. user_agent = %s,  
  503. currency = %s,  
  504. payment_status = {$payment_status},  
  505. payment_date = {$payment_date},  
  506. payment_amount = {$payment_amount},  
  507. transaction_id = {$transaction_id},  
  508. is_fulfilled = {$is_fulfilled},  
  509. created_by = {$user_id},  
  510. transaction_type = {$transaction_type},  
  511. status = %s,  
  512. payment_method = %s 
  513. WHERE 
  514. id = %d 
  515. ", $form_id, $is_starred, $is_read, $ip, $source_url, $user_agent, $currency, $status, $payment_method, $entry_id 
  516. ); 
  517. $result = $wpdb->query( $sql ); 
  518. if ( false === $result ) { 
  519. return new WP_Error( 'update_entry_properties_failed', __( 'There was a problem while updating the entry properties', 'gravityforms' ), $wpdb->last_error ); 
  520.  
  521. // only save field values for fields that currently exist in the form. The rest in $entry will be ignored. The rest in $current_entry will get deleted. 
  522.  
  523. $lead_detail_table = GFFormsModel::get_lead_details_table_name(); 
  524. $current_fields = $wpdb->get_results( $wpdb->prepare( "SELECT id, field_number FROM $lead_detail_table WHERE lead_id=%d", $entry_id ) ); 
  525.  
  526. $form = GFFormsModel::get_form_meta( $form_id ); 
  527.  
  528. $form = gf_apply_filters( array( 'gform_form_pre_update_entry', $form_id ), $form, $entry, $entry_id ); 
  529.  
  530. foreach ( $form['fields'] as $field ) { 
  531. /** @var GF_Field $field */ 
  532. $type = GFFormsModel::get_input_type( $field ); 
  533. if ( in_array( $type, array( 'html', 'page', 'section' ) ) ) { 
  534. continue; 
  535. $inputs = $field->get_entry_inputs(); 
  536. if ( is_array( $inputs ) ) { 
  537. foreach ( $field->inputs as $input ) { 
  538. $input_id = (string) $input['id']; 
  539. if ( isset( $entry[ $input_id ] ) ) { 
  540. if ( $entry[ $input_id ] != $current_entry[ $input_id ] ) { 
  541. $lead_detail_id = GFFormsModel::get_lead_detail_id( $current_fields, $input_id ); 
  542. $result = GFFormsModel::update_lead_field_value( $form, $entry, $field, $lead_detail_id, $input_id, $entry[ $input_id ] ); 
  543. if ( false === $result ) { 
  544. return new WP_Error( 'update_input_value_failed', __( 'There was a problem while updating one of the input values for the entry', 'gravityforms' ), $wpdb->last_error ); 
  545. unset( $current_entry[ $input_id ] ); 
  546. } else { 
  547. $field_id = $field->id; 
  548. $field_value = isset( $entry[ (string) $field_id ] ) ? $entry[ (string) $field_id ] : ''; 
  549. if ( $field_value != $current_entry[ $field_id ] ) { 
  550. $lead_detail_id = GFFormsModel::get_lead_detail_id( $current_fields, $field_id ); 
  551. $result = GFFormsModel::update_lead_field_value( $form, $entry, $field, $lead_detail_id, $field_id, $field_value ); 
  552. if ( false === $result ) { 
  553. return new WP_Error( 'update_field_values_failed', __( 'There was a problem while updating the field values', 'gravityforms' ), $wpdb->last_error ); 
  554. unset( $current_entry[ $field_id ] ); 
  555.  
  556. // save the entry meta values - only for the entry meta currently available for the form, ignore the rest 
  557. $entry_meta = GFFormsModel::get_entry_meta( $form_id ); 
  558. if ( is_array( $entry_meta ) ) { 
  559. foreach ( array_keys( $entry_meta ) as $key ) { 
  560. if ( isset( $entry[ $key ] ) ) { 
  561. if ( $entry[ $key ] != $current_entry[ $key ] ) { 
  562. gform_update_meta( $entry_id, $key, $entry[ $key ] ); 
  563. unset( $current_entry[ $key ] ); 
  564.  
  565. // now delete remaining values from the old entry 
  566.  
  567. if ( is_array( $entry_meta ) ) { 
  568. foreach ( array_keys( $entry_meta ) as $meta_key ) { 
  569. if ( isset( $current_entry[ $meta_key ] ) ) { 
  570. gform_delete_meta( $entry_id, $meta_key ); 
  571. unset( $current_entry[ $meta_key ] ); 
  572.  
  573. foreach ( $current_entry as $k => $v ) { 
  574. $lead_detail_id = GFFormsModel::get_lead_detail_id( $current_fields, $k ); 
  575. $field = GFFormsModel::get_field( $form, $k ); 
  576. $result = GFFormsModel::update_lead_field_value( $form, $entry, $field, $lead_detail_id, $k, '' ); 
  577. if ( false === $result ) { 
  578. return new WP_Error( 'update_field_values_failed', __( 'There was a problem while updating the field values', 'gravityforms' ), $wpdb->last_error ); 
  579.  
  580. /** 
  581. * Fires after the Entry is updated. 
  582. * @param array $lead The entry object after being updated. 
  583. * @param array $original_entry The entry object before being updated. 
  584. */ 
  585. gf_do_action( array( 'gform_post_update_entry', $form_id ), $entry, $original_entry ); 
  586.  
  587. return true; 
  588.  
  589. /** 
  590. * Adds a single Entry object. 
  591. * Intended to be used for importing an entry object. The usual hooks that are triggered while saving entries are not fired here. 
  592. * Checks that the form id, field ids and entry meta exist and ignores legacy values (i.e. values for fields that no longer exist). 
  593. * @since 1.8 
  594. * @access public 
  595. * @static 
  596. * @param array $entry The Entry object 
  597. * @return mixed Either the new Entry ID or a WP_Error instance 
  598. */ 
  599. public static function add_entry( $entry ) { 
  600. global $wpdb; 
  601.  
  602. if ( ! is_array( $entry ) ) { 
  603. return new WP_Error( 'invalid_entry_object', __( 'The entry object must be an array', 'gravityforms' ) ); 
  604.  
  605. // make sure the form id exists 
  606. $form_id = rgar( $entry, 'form_id' ); 
  607. if ( empty( $form_id ) ) { 
  608. return new WP_Error( 'empty_form_id', __( 'The form id must be specified', 'gravityforms' ) ); 
  609.  
  610. if ( false === self::form_id_exists( $form_id ) ) { 
  611. return new WP_Error( 'invalid_form_id', __( 'The form for this entry does not exist', 'gravityforms' ) ); 
  612.  
  613. // use values in the entry object if present 
  614. $post_id = isset( $entry['post_id'] ) ? intval( $entry['post_id'] ) : 'NULL'; 
  615. $date_created = isset( $entry['date_created'] ) && $entry['date_created'] != '' ? sprintf( "'%s'", esc_sql( $entry['date_created'] ) ) : 'utc_timestamp()'; 
  616. $is_starred = isset( $entry['is_starred'] ) ? $entry['is_starred'] : 0; 
  617. $is_read = isset( $entry['is_read'] ) ? $entry['is_read'] : 0; 
  618. $ip = isset( $entry['ip'] ) ? $entry['ip'] : GFFormsModel::get_ip(); 
  619. $source_url = isset( $entry['source_url'] ) ? $entry['source_url'] : esc_url_raw( GFFormsModel::get_current_page_url() ); 
  620. $user_agent = isset( $entry['user_agent'] ) ? $entry['user_agent'] : 'API'; 
  621. $currency = isset( $entry['currency'] ) ? $entry['currency'] : GFCommon::get_currency(); 
  622. $payment_status = isset( $entry['payment_status'] ) ? sprintf( "'%s'", esc_sql( $entry['payment_status'] ) ) : 'NULL'; 
  623. $payment_date = strtotime( rgar( $entry, 'payment_date' ) ) ? sprintf( "'%s'", gmdate( 'Y-m-d H:i:s', strtotime( "{$entry['payment_date']}" ) ) ) : 'NULL'; 
  624. $payment_amount = isset( $entry['payment_amount'] ) ? (float) $entry['payment_amount'] : 'NULL'; 
  625. $payment_method = isset( $entry['payment_method'] ) ? $entry['payment_method'] : ''; 
  626. $transaction_id = isset( $entry['transaction_id'] ) ? sprintf( "'%s'", esc_sql( $entry['transaction_id'] ) ) : 'NULL'; 
  627. $is_fulfilled = isset( $entry['is_fulfilled'] ) ? intval( $entry['is_fulfilled'] ) : 'NULL'; 
  628. $status = isset( $entry['status'] ) ? $entry['status'] : 'active'; 
  629.  
  630. global $current_user; 
  631. $user_id = isset( $entry['created_by'] ) ? absint( $entry['created_by'] ) : ''; 
  632. if ( empty( $user_id ) ) { 
  633. $user_id = $current_user && $current_user->ID ? absint( $current_user->ID ) : 'NULL'; 
  634.  
  635. $transaction_type = isset( $entry['transaction_type'] ) ? intval( $entry['transaction_type'] ) : 'NULL'; 
  636.  
  637. $lead_table = GFFormsModel::get_lead_table_name(); 
  638. $result = $wpdb->query( 
  639. $wpdb->prepare( 
  640. INSERT INTO $lead_table 
  641. (form_id, post_id, date_created, is_starred, is_read, ip, source_url, user_agent, currency, payment_status, payment_date, payment_amount, transaction_id, is_fulfilled, created_by, transaction_type, status, payment_method) 
  642. VALUES 
  643. (%d, {$post_id}, {$date_created}, %d, %d, %s, %s, %s, %s, {$payment_status}, {$payment_date}, {$payment_amount}, {$transaction_id}, {$is_fulfilled}, {$user_id}, {$transaction_type}, %s, %s) 
  644. ", $form_id, $is_starred, $is_read, $ip, $source_url, $user_agent, $currency, $status, $payment_method 
  645. ); 
  646. if ( false === $result ) { 
  647. return new WP_Error( 'insert_entry_properties_failed', __( 'There was a problem while inserting the entry properties', 'gravityforms' ), $wpdb->last_error ); 
  648. // reading newly created lead id 
  649. $entry_id = $wpdb->insert_id; 
  650. $entry['id'] = $entry_id; 
  651.  
  652. // only save field values for fields that currently exist in the form 
  653.  
  654. $form = GFFormsModel::get_form_meta( $form_id ); 
  655. foreach ( $form['fields'] as $field ) { 
  656. /** @var GF_Field $field */ 
  657. if ( in_array( $field->type, array( 'html', 'page', 'section' ) ) ) { 
  658. continue; 
  659. $inputs = $field->get_entry_inputs(); 
  660. if ( is_array( $inputs ) ) { 
  661. foreach ( $inputs as $input ) { 
  662. $input_id = (string) $input['id']; 
  663. if ( isset( $entry[ $input_id ] ) ) { 
  664. $result = GFFormsModel::update_lead_field_value( $form, $entry, $field, 0, $input_id, $entry[ $input_id ] ); 
  665. if ( false === $result ) { 
  666. return new WP_Error( 'insert_input_value_failed', __( 'There was a problem while inserting one of the input values for the entry', 'gravityforms' ), $wpdb->last_error ); 
  667. } else { 
  668. $field_id = $field->id; 
  669. $field_value = isset( $entry[ (string) $field_id ] ) ? $entry[ (string) $field_id ] : ''; 
  670. $result = GFFormsModel::update_lead_field_value( $form, $entry, $field, 0, $field_id, $field_value ); 
  671. if ( false === $result ) { 
  672. return new WP_Error( 'insert_field_values_failed', __( 'There was a problem while inserting the field values', 'gravityforms' ), $wpdb->last_error ); 
  673.  
  674. // add save the entry meta values - only for the entry meta currently available for the form, ignore the rest 
  675. $entry_meta = GFFormsModel::get_entry_meta( $form_id ); 
  676. if ( is_array( $entry_meta ) ) { 
  677. foreach ( array_keys( $entry_meta ) as $key ) { 
  678. if ( isset( $entry[ $key ] ) ) { 
  679. gform_update_meta( $entry_id, $key, $entry[ $key ], $form['id'] ); 
  680.  
  681. return $entry_id; 
  682.  
  683. /** 
  684. * Deletes a single Entry. 
  685. * @since 1.8 
  686. * @access public 
  687. * @static 
  688. * @param int $entry_id The ID of the Entry object 
  689. * @return mixed Either true for success or a WP_Error instance 
  690. */ 
  691. public static function delete_entry( $entry_id ) { 
  692.  
  693. $entry = GFFormsModel::get_lead( $entry_id ); 
  694. if ( empty( $entry ) ) { 
  695. return new WP_Error( 'invalid_entry_id', sprintf( __( 'Invalid entry id: %s', 'gravityforms' ), $entry_id ), $entry_id ); 
  696. GFFormsModel::delete_lead( $entry_id ); 
  697.  
  698. return true; 
  699.  
  700. /** 
  701. * Updates a single property of an entry. 
  702. * @since 1.8.3.1 
  703. * @access public 
  704. * @static 
  705. * @param int $entry_id The ID of the Entry object 
  706. * @param string $property The property of the Entry object to be updated 
  707. * @param mixed $value The value to which the property should be set 
  708. * @return bool Whether the entry property was updated successfully 
  709. */ 
  710. public static function update_entry_property( $entry_id, $property, $value ) { 
  711. return GFFormsModel::update_lead_property( $entry_id, $property, $value ); 
  712.  
  713. /** 
  714. * Updates a single field of an entry. 
  715. * @since 1.9 
  716. * @access public 
  717. * @static 
  718. * @param int $entry_id The ID of the Entry object 
  719. * @param string $input_id The id of the input to be updated. For single input fields such as text, paragraph, website, drop down etc... this will be the same as the field ID. 
  720. * For multi input fields such as name, address, checkboxes, etc... the input id will be in the format {FIELD_ID}.{INPUT NUMBER}. ( i.e. "1.3" ) 
  721. * The $input_id can be obtained by inspecting the key for the specified field in the $entry object. 
  722. * @param mixed $value The value to which the field should be set 
  723. * @return bool Whether the entry property was updated successfully 
  724. */ 
  725. public static function update_entry_field( $entry_id, $input_id, $value ) { 
  726. global $wpdb; 
  727.  
  728. $entry = self::get_entry( $entry_id ); 
  729. if ( is_wp_error( $entry ) ) { 
  730. return $entry; 
  731.  
  732. $form = self::get_form( $entry['form_id'] ); 
  733. if ( ! $form ) { 
  734. return false; 
  735.  
  736. $field = GFFormsModel::get_field( $form, $input_id ); 
  737.  
  738. $input_id_min = (float) $input_id - 0.0001; 
  739. $input_id_max = (float) $input_id + 0.0001; 
  740.  
  741. $lead_details_table_name = GFFormsModel::get_lead_details_table_name(); 
  742.  
  743. $lead_detail_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$lead_details_table_name} WHERE lead_id=%d AND field_number BETWEEN %s AND %s", $entry_id, $input_id_min, $input_id_max ) ); 
  744.  
  745. $result = true; 
  746. if ( ! isset( $entry[ $input_id ] ) || $entry[ $input_id ] != $value ) { 
  747. $result = GFFormsModel::update_lead_field_value( $form, $entry, $field, $lead_detail_id, $input_id, $value ); 
  748.  
  749. return $result; 
  750.  
  751. // FORM SUBMISSIONS ------------------------------------------- 
  752.  
  753. /** 
  754. * Submits a form. Use this function to send input values through the complete form submission process. 
  755. * Supports field validation, notifications, confirmations, multiple-pages and save & continue. 
  756. * Example usage: 
  757. * $input_values['input_1'] = 'Single line text'; 
  758. * $input_values['input_2_3'] = 'First name'; 
  759. * $input_values['input_2_6'] = 'Last name'; 
  760. * $input_values['input_5'] = 'A paragraph of text.'; 
  761. * //$input_values['gform_save'] = true; // support for save and continue 
  762. * $result = GFAPI::submit_form( 52, $input_values ); 
  763. * Example output for a successful submission: 
  764. * 'is_valid' => boolean true 
  765. * 'page_number' => int 0 
  766. * 'source_page_number' => int 1 
  767. * 'confirmation_message' => string 'confirmation message [snip]' 
  768. * Example output for failed validation: 
  769. * 'is_valid' => boolean false 
  770. * 'validation_messages' => 
  771. * array (size=1) 
  772. * 2 => string 'This field is required. Please enter the first and last name.' 
  773. * 'page_number' => int 1 
  774. * 'source_page_number' => int 1 
  775. * 'confirmation_message' => string '' 
  776. * Example output for save and continue: 
  777. * 'is_valid' => boolean true 
  778. * 'page_number' => int 1 
  779. * 'source_page_number' => int 1 
  780. * 'confirmation_message' => string 'Please use the following link to return to your form from any computer. [snip]' 
  781. * 'resume_token' => string '045f941cc4c04d479556bab1db6d3495' 
  782. * @param int $form_id The Form ID 
  783. * @param array $input_values An array of values. 
  784. * @param array $field_values Optional. 
  785. * @param int $target_page Optional. 
  786. * @param int $source_page Optional. 
  787. * @return array An array containing the result of the submission. 
  788. */ 
  789. public static function submit_form( $form_id, $input_values, $field_values = array(), $target_page = 0, $source_page = 1 ) { 
  790. $form_id = absint( $form_id ); 
  791. $form = GFAPI::get_form( $form_id ); 
  792.  
  793. if ( empty( $form ) || ! $form['is_active'] || $form['is_trash'] ) { 
  794. return new WP_Error( 'form_not_found', __( 'Your form could not be found', 'gravityforms' ) ); 
  795.  
  796. $input_values[ 'is_submit_' . $form_id ] = true; 
  797. $input_values['gform_submit'] = $form_id; 
  798. $input_values[ 'gform_target_page_number_' . $form_id ] = absint( $target_page ); 
  799. $input_values[ 'gform_source_page_number_' . $form_id ] = absint( $source_page ); 
  800. $input_values['gform_field_values'] = $field_values; 
  801.  
  802. require_once(GFCommon::get_base_path() . '/form_display.php'); 
  803.  
  804. if ( ! isset( $_POST ) ) { 
  805. $_POST = array(); 
  806.  
  807. $_POST = array_merge_recursive( $_POST, $input_values ); 
  808.  
  809. try { 
  810. GFFormDisplay::process_form( $form_id ); 
  811. } catch ( Exception $ex ) { 
  812. return new WP_Error( 'error_processing_form', __( 'There was an error while processing the form:', 'gravityforms' ) . ' ' . $ex->getCode() . ' ' . $ex->getMessage() ); 
  813.  
  814. if ( empty( GFFormDisplay::$submission ) ) { 
  815. return new WP_Error( 'error_processing_form', __( 'There was an error while processing the form:', 'gravityforms' ) ); 
  816.  
  817. $submissions_array = GFFormDisplay::$submission; 
  818.  
  819. $submission_details = $submissions_array[ $form_id ]; 
  820.  
  821. $result = array(); 
  822.  
  823. $result['is_valid'] = $submission_details['is_valid']; 
  824.  
  825. if ( $result['is_valid'] == false ) { 
  826. $validation_messages = array(); 
  827. foreach ( $submission_details['form']['fields'] as $field ) { 
  828. if ( $field->failed_validation ) { 
  829. $validation_messages[ $field->id ] = $field->validation_message; 
  830. $result['validation_messages'] = $validation_messages; 
  831.  
  832. $result['page_number'] = $submission_details['page_number']; 
  833. $result['source_page_number'] = $submission_details['source_page_number']; 
  834. $result['confirmation_message'] = $submission_details['confirmation_message']; 
  835.  
  836. if ( isset( $submission_details['resume_token'] ) ) { 
  837. $result['resume_token'] = $submission_details['resume_token']; 
  838.  
  839. $form = self::get_form( $form_id ); 
  840.  
  841. $result['confirmation_message'] = GFFormDisplay::replace_save_variables( $result['confirmation_message'], $form, $result['resume_token'] ); 
  842.  
  843. return $result; 
  844.  
  845. // FEEDS ------------------------------------------------------ 
  846.  
  847. /** 
  848. * Returns all the feeds for the given criteria. 
  849. * @since 1.8 
  850. * @access public 
  851. * @static 
  852. * @param mixed $feed_ids The ID of the Feed or an array of Feed IDs 
  853. * @param int $form_id The ID of the Form to which the Feeds belong 
  854. * @param string $addon_slug The slug of the add-on to which the Feeds belong 
  855. * @param bool $is_active 
  856. * @return mixed Either an array of Feed objects or a WP_Error instance 
  857. */ 
  858. public static function get_feeds( $feed_ids = null, $form_id = null, $addon_slug = null, $is_active = true ) { 
  859. global $wpdb; 
  860.  
  861. $table = $wpdb->prefix . 'gf_addon_feed'; 
  862. $where_arr = array(); 
  863. $where_arr[] = $wpdb->prepare( 'is_active=%d', $is_active ); 
  864. if ( false === empty( $form_id ) ) { 
  865. $where_arr[] = $wpdb->prepare( 'form_id=%d', $form_id ); 
  866. if ( false === empty( $addon_slug ) ) { 
  867. $where_arr[] = $wpdb->prepare( 'addon_slug=%s', $addon_slug ); 
  868. if ( false === empty( $feed_ids ) ) { 
  869. if ( ! is_array( $feed_ids ) ) { 
  870. $feed_ids = array( $feed_ids ); 
  871. $in_str_arr = array_fill( 0, count( $feed_ids ), '%d' ); 
  872. $in_str = join( $in_str_arr, ', ' ); 
  873. $where_arr[] = $wpdb->prepare( "id IN ($in_str)", $feed_ids ); 
  874.  
  875.  
  876. $where = join( ' AND ', $where_arr ); 
  877.  
  878. $sql = "SELECT id, form_id, addon_slug, meta FROM {$table} WHERE $where"; 
  879.  
  880. $results = $wpdb->get_results( $sql, ARRAY_A ); 
  881. if ( empty( $results ) ) { 
  882. return new WP_Error( 'not_found', __( 'Feed not found', 'gravityforms' ) ); 
  883.  
  884. foreach ( $results as &$result ) { 
  885. $result['meta'] = json_decode( $result['meta'], true ); 
  886.  
  887. return $results; 
  888.  
  889. /** 
  890. * Deletes a single Feed 
  891. * @since 1.8 
  892. * @access public 
  893. * @static 
  894. * @param int $feed_id The ID of the Feed to delete 
  895. * @return mixed Either an array of Feed objects or a WP_Error instance 
  896. */ 
  897. public static function delete_feed( $feed_id ) { 
  898.  
  899. global $wpdb; 
  900.  
  901. $table = $wpdb->prefix . 'gf_addon_feed'; 
  902.  
  903. $sql = $wpdb->prepare( "DELETE FROM {$table} WHERE id=%d", $feed_id ); 
  904.  
  905. $results = $wpdb->query( $sql ); 
  906. if ( false === $results ) { 
  907. return new WP_Error( 'error_deleting', sprintf( __( 'There was an an error while deleting feed id %s', 'gravityforms' ), $feed_id ), $wpdb->last_error ); 
  908.  
  909. if ( 0 === $results ) { 
  910. return new WP_Error( 'not_found', sprintf( __( 'Feed id %s not found', 'gravityforms' ), $feed_id ) ); 
  911.  
  912. return true; 
  913.  
  914. public static function update_feed( $feed_id, $feed_meta, $form_id = null ) { 
  915. global $wpdb; 
  916.  
  917. $feed_meta_json = json_encode( $feed_meta ); 
  918. $table = $wpdb->prefix . 'gf_addon_feed'; 
  919. if ( empty( $form_id ) ) { 
  920. $sql = $wpdb->prepare( "UPDATE {$table} SET meta= %s WHERE id=%d", $feed_meta_json, $feed_id ); 
  921. } else { 
  922. $sql = $wpdb->prepare( "UPDATE {$table} SET form_id = %d, meta= %s WHERE id=%d", $form_id, $feed_meta_json, $feed_id ); 
  923.  
  924. $results = $wpdb->query( $sql ); 
  925.  
  926. if ( false === $results ) { 
  927. return new WP_Error( 'error_updating', sprintf( __( 'There was an an error while updating feed id %s', 'gravityforms' ), $feed_id ), $wpdb->last_error ); 
  928.  
  929. if ( 0 === $results ) { 
  930. return new WP_Error( 'not_found', sprintf( __( 'Feed id %s not found', 'gravityforms' ), $feed_id ) ); 
  931.  
  932. return $results; 
  933.  
  934. /** 
  935. * Adds a feed with the given Feed object. 
  936. * @since 1.8 
  937. * @access public 
  938. * @static 
  939. * @param int $form_id The ID of the Form to which the Feed belongs 
  940. * @param array $feed_meta The Feed object 
  941. * @param string $addon_slug The slug of the add-on to which the Feeds belong 
  942. * @return mixed Either the ID of the newly created Feed or a WP_Error instance 
  943. */ 
  944. public static function add_feed( $form_id, $feed_meta, $addon_slug ) { 
  945. global $wpdb; 
  946.  
  947. $table = $wpdb->prefix . 'gf_addon_feed'; 
  948. $feed_meta_json = json_encode( $feed_meta ); 
  949. $sql = $wpdb->prepare( "INSERT INTO {$table} (form_id, meta, addon_slug) VALUES (%d, %s, %s)", $form_id, $feed_meta_json, $addon_slug ); 
  950.  
  951. $results = $wpdb->query( $sql ); 
  952.  
  953. if ( false === $results ) { 
  954. return new WP_Error( 'error_inserting', __( 'There was an an error while inserting a feed', 'gravityforms' ), $wpdb->last_error ); 
  955.  
  956. return $wpdb->insert_id; 
  957.  
  958. // NOTIFICATIONS ---------------------------------------------- 
  959.  
  960. /** 
  961. * Sends all active notifications for a form given an entry object and an event. 
  962. * @param $form 
  963. * @param $entry 
  964. * @param string $event Default = 'form_submission' 
  965. * @return array 
  966. */ 
  967. public static function send_notifications( $form, $entry, $event = 'form_submission' ) { 
  968.  
  969. if ( rgempty( 'notifications', $form ) || ! is_array( $form['notifications'] ) ) { 
  970. return array(); 
  971.  
  972. $entry_id = rgar( $entry, 'id' ); 
  973. GFCommon::log_debug( "GFAPI::send_notifications(): Gathering notifications for {$event} event for entry #{$entry_id}." ); 
  974.  
  975. $notifications_to_send = array(); 
  976.  
  977. //running through filters that disable form submission notifications 
  978. foreach ( $form['notifications'] as $notification ) { 
  979. if ( rgar( $notification, 'event' ) != $event ) { 
  980. continue; 
  981.  
  982. if ( $event == 'form_submission' ) { 
  983. if ( rgar( $notification, 'type' ) == 'user' && gf_apply_filters( array( 'gform_disable_user_notification', $form['id'] ), false, $form, $entry ) ) { 
  984. GFCommon::log_debug( "GFAPI::send_notifications(): Notification is disabled by gform_disable_user_notification hook, not including notification (#{$notification['id']} - {$notification['name']})." ); 
  985. //skip user notification if it has been disabled by a hook 
  986. continue; 
  987. } elseif ( rgar( $notification, 'type' ) == 'admin' && gf_apply_filters( array( 'gform_disable_admin_notification', $form['id'] ), false, $form, $entry ) ) { 
  988. GFCommon::log_debug( "GFAPI::send_notifications(): Notification is disabled by gform_disable_admin_notification hook, not including notification (#{$notification['id']} - {$notification['name']})." ); 
  989. //skip admin notification if it has been disabled by a hook 
  990. continue; 
  991.  
  992. if ( gf_apply_filters( array( 'gform_disable_notification', $form['id'] ), false, $notification, $form, $entry ) ) { 
  993. GFCommon::log_debug( "GFAPI::send_notifications(): Notification is disabled by gform_disable_notification hook, not including notification (#{$notification['id']} - {$notification['name']})." ); 
  994. //skip notifications if it has been disabled by a hook 
  995. continue; 
  996.  
  997. $notifications_to_send[] = $notification['id']; 
  998.  
  999. GFCommon::send_notifications( $notifications_to_send, $form, $entry, true, $event ); 
  1000.  
  1001.  
  1002. // PERMISSIONS ------------------------------------------------ 
  1003. /** 
  1004. * Checks the permissions for the current user. Returns true if the current user has any of the specified capabilities. 
  1005. * IMPORTANT: Call this before calling any of the other API Functions as permission checks are not performed at lower levels. 
  1006. * @since 1.8.5.10 
  1007. * @access public 
  1008. * @static 
  1009. * @param array|string $capabilities An array of capabilities, or a single capability 
  1010. * @return bool Returns true if the current user has any of the specified capabilities 
  1011. */ 
  1012. public static function current_user_can_any( $capabilities ) { 
  1013. return GFCommon::current_user_can_any( $capabilities ); 
  1014.  
  1015. // FIELDS ----------------------------------------------------- 
  1016.  
  1017. /** 
  1018. * Returns an array containing the form fields of the specified type or types. 
  1019. * @since 1.9.9.8 
  1020. * @param array $form 
  1021. * @param array|string $types 
  1022. * @param bool $use_input_type 
  1023. * @return GF_Field[] 
  1024. */ 
  1025. public static function get_fields_by_type( $form, $types, $use_input_type = false ) { 
  1026. return GFFormsModel::get_fields_by_type( $form, $types, $use_input_type ); 
  1027.  
  1028. // HELPERS ---------------------------------------------------- 
  1029.  
  1030. /** 
  1031. * Private. 
  1032. * @since 1.8 
  1033. * @access private 
  1034. * @static 
  1035. * @ignore 
  1036. */ 
  1037. public static function form_id_exists( $form_id ) { 
  1038. global $wpdb; 
  1039. $form_table_name = GFFormsModel::get_form_table_name(); 
  1040. $form_id = intval( $form_id ); 
  1041. $result = $wpdb->get_var( 
  1042. $wpdb->prepare( 
  1043. " SELECT count(id) FROM {$form_table_name} 
  1044. WHERE id=%d", $form_id 
  1045. ); 
  1046.  
  1047. $result = intval( $result ); 
  1048.  
  1049. return $result > 0; 
  1050.