NF_Form

Handles the output of our form, as well as interacting with its settings.

Defined (1)

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

/deprecated/classes/form.php  
  1. class NF_Form { 
  2.  
  3. /** 
  4. * @var form_id 
  5. * @since 2.7 
  6. */ 
  7. var $form_id; 
  8.  
  9. /** 
  10. * @var settings - Form Settings 
  11. * @since 2.7 
  12. */ 
  13. var $settings = array(); 
  14.  
  15. /** 
  16. * @var fields - Form Fields 
  17. * @since 2.7 
  18. */ 
  19. var $fields = array(); 
  20.  
  21. /** 
  22. * @var fields - Fields List 
  23. * @since 2.7 
  24. */ 
  25. var $field_keys = array(); 
  26.  
  27. /** 
  28. * @var errors - Form errors 
  29. * @since 2.7 
  30. */ 
  31. var $errors = array(); 
  32.  
  33. /** 
  34. * Get things started 
  35. *  
  36. * @access public 
  37. * @since 2.7 
  38. * @return void 
  39. */ 
  40. public function __construct( $form_id = '' ) { 
  41. if ( ! empty ( $form_id ) ) { // We've been passed a form id. 
  42. // Set our current form id. 
  43. $this->form_id = $form_id; 
  44. $this->update_fields(); 
  45. $this->settings = nf_get_form_settings( $form_id ); 
  46.  
  47. /** 
  48. * Add a form 
  49. *  
  50. * @access public 
  51. * @since 2.9 
  52. * @return int $form_id 
  53. */ 
  54. public function create( $defaults = array() ) { 
  55. $form_id = nf_insert_object( 'form' ); 
  56. $date_updated = date( 'Y-m-d', current_time( 'timestamp' ) ); 
  57. nf_update_object_meta( $form_id, 'date_updated', $date_updated ); 
  58.  
  59. foreach( $defaults as $meta_key => $meta_value ) { 
  60. nf_update_object_meta( $form_id, $meta_key, $meta_value ); 
  61.  
  62. // Add a single event hook that will check to see if this is an orphaned function. 
  63. $timestamp = strtotime( '+24 hours', time() ); 
  64. $args = array( 
  65. 'form_id' => $form_id 
  66. ); 
  67. wp_schedule_single_event( $timestamp, 'nf_maybe_delete_form', $args ); 
  68. return $form_id; 
  69.  
  70. /** 
  71. * Insert a field into our form 
  72. *  
  73. * @access public 
  74. * @since 2.9 
  75. * @return bool() 
  76. */ 
  77. public function insert_field( $field_id ) { 
  78. return nf_add_relationship( $field_id, 'field', $this->form_id, 'form' ); 
  79.  
  80. /** 
  81. * Update our fields 
  82. *  
  83. * @access public 
  84. * @since 2.9 
  85. * @return void 
  86. */ 
  87. public function update_fields() { 
  88. $this->fields = nf_get_fields_by_form_id( $this->form_id ); 
  89.  
  90. /** 
  91. * Get one of our form settings. 
  92. *  
  93. * @access public 
  94. * @since 2.7 
  95. * @return string $setting 
  96. */ 
  97. public function get_setting( $setting, $bypass_cache = false ) { 
  98. if ( $bypass_cache ) { 
  99. return nf_get_object_meta_value( $this->form_id, 'last_sub' ); 
  100. if ( isset ( $this->settings[ $setting ] ) ) { 
  101. return $this->settings[ $setting ]; 
  102. } else { 
  103. return false; 
  104.  
  105. /** 
  106. * Update a form setting (this doesn't update anything in the database) 
  107. * Changes are only applied to this object. 
  108. *  
  109. * @access public 
  110. * @since 2.8 
  111. * @param string $setting 
  112. * @param mixed $value 
  113. * @return bool 
  114. */ 
  115. public function update_setting( $setting, $value ) { 
  116. $this->settings[ $setting ] = $value; 
  117. nf_update_object_meta( $this->form_id, $setting, $value ); 
  118. $this->dump_cache(); 
  119. return true; 
  120.  
  121. /** 
  122. * Get all of our settings 
  123. *  
  124. * @access public 
  125. * @since 2.9 
  126. * @return array $settings 
  127. */ 
  128. public function get_all_settings() { 
  129. return $this->settings; 
  130.  
  131. /** 
  132. * Get all the submissions for this form 
  133. *  
  134. * @access public 
  135. * @since 2.7 
  136. * @return array $sub_ids 
  137. */ 
  138. public function get_subs( $args = array() ) { 
  139. $args['form_id'] = $this->form_id; 
  140. return Ninja_Forms()->subs()->get( $args ); 
  141.  
  142. /** 
  143. * Return a count of the submissions this form has had 
  144. *  
  145. * @access public 
  146. * @param array $args 
  147. * @since 2.7 
  148. * @return int $count 
  149. */ 
  150. public function sub_count( $args = array() ) { 
  151. return count( $this->get_subs( $args ) ); 
  152.  
  153. /** 
  154. * Delete this form 
  155. *  
  156. * @access public 
  157. * @since 2.9 
  158. */ 
  159. public function delete() { 
  160. global $wpdb; 
  161. // Delete this object. 
  162. nf_delete_object( $this->form_id ); 
  163. // Delete any fields on this form. 
  164. $wpdb->query($wpdb->prepare( "DELETE FROM ".NINJA_FORMS_FIELDS_TABLE_NAME." WHERE form_id = %d", $this->form_id ) ); 
  165.  
  166. /** 
  167. * Delete the cached form object (transient) 
  168. * @access public 
  169. * @since 2.9.17 
  170. */ 
  171. public function dump_cache() 
  172. delete_transient( 'nf_form_' . $this->form_id ); 
  173.  
  174. /** 
  175. * Deprecated wrapper for dump_cache() 
  176. * @access public 
  177. * @since 2.9.12 
  178. */ 
  179. public function dumpCache() 
  180. $this->dump_cache(); 
  181.