form.php 4.3 KB

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