NF_Sub

Submission.

Defined (1)

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

/deprecated/classes/sub.php  
  1. class NF_Sub { 
  2.  
  3. /** 
  4. * @var $sub_id store our sub id 
  5. */ 
  6. var $sub_id; 
  7.  
  8. /** 
  9. * @var $seq_num store our sequential number 
  10. */ 
  11. var $seq_num; 
  12.  
  13. /** 
  14. * @var $form_id store our form_id 
  15. */ 
  16. var $form_id; 
  17.  
  18. /** 
  19. * @var $fields store our user values 
  20. */ 
  21. var $fields; 
  22.  
  23. /** 
  24. * @var $action store the action that created this sub 
  25. */ 
  26. var $action; 
  27.  
  28. /** 
  29. * @var $user_id store the user ID for this submission 
  30. */ 
  31. var $user_id; 
  32.  
  33. /** 
  34. * @var $meta store our non-field meta 
  35. */ 
  36. var $meta; 
  37.  
  38. /** 
  39. * @var $date_submitted store our submitted date 
  40. */ 
  41. var $date_submitted; 
  42.  
  43. /** 
  44. * @var $date_modified store our modified date 
  45. */ 
  46. var $date_modified; 
  47.  
  48. /** 
  49. * Get things started 
  50. * @access public 
  51. * @since 2.7 
  52. * @return void/ 
  53. */ 
  54. public function __construct( $sub_id ) { 
  55. global $ninja_forms_fields; 
  56.  
  57. // Bail if the sub doesn't exist. 
  58. $sub = get_post( $sub_id ); 
  59. if ( ! is_object( $sub ) ) 
  60. return false; 
  61.  
  62. // Set our sub id 
  63. $this->sub_id = $sub_id; 
  64.  
  65. // Populate our fields 
  66. $this->fields = array(); 
  67. $this->retrieve_fields(); 
  68.  
  69. // Setup our form id var 
  70. $this->form_id = $this->get_meta( '_form_id' ); 
  71. // Setup our action var 
  72. $this->action = $this->get_meta( '_action' ); 
  73. // Setup our sequential id 
  74. $this->seq_num = $this->get_meta( '_seq_num' ); 
  75. // Setup our user_id var 
  76. $this->user_id = $sub->post_author; 
  77. // Setup our date submitted var 
  78. $this->date_submitted = get_the_time( 'Y-m-d G:i:s', $sub_id ); 
  79. // Setup our date modified var 
  80. $this->date_modified = get_post_modified_time( 'Y-m-d G:i:s', false, $sub_id ); 
  81.  
  82.  
  83. private function retrieve_fields() { 
  84. global $ninja_forms_fields; 
  85.  
  86. // Setup our fields and meta vars. 
  87. $post_meta = get_post_custom( $this->sub_id ); 
  88.  
  89. foreach ( $post_meta as $key => $array ) { 
  90. if ( is_serialized( $array[0] ) ) { 
  91. $meta_value = unserialize( $array[0] ); 
  92. } else { 
  93. $meta_value = $array[0]; 
  94.  
  95. if ( strpos( $key, '_field_' ) !== false ) { 
  96. $field_id = str_replace( '_field_', '', $key ); 
  97. $field = ninja_forms_get_field_by_id( $field_id ); 
  98. $field_type = $field['type']; 
  99.  
  100. // Check to see if our field type has been set as a "process_field". 
  101. if ( isset ( $ninja_forms_fields[ $field_type ] ) ) { 
  102. $reg_field = $ninja_forms_fields[ $field_type ]; 
  103. $process_field = $reg_field['process_field']; 
  104. } else { 
  105. $process_field = false; 
  106.  
  107. if ( $process_field ) { 
  108. $this->fields[ $field_id ] = $meta_value; 
  109.  
  110. } else if ( $key != '_form_id' && $key != '_action' ) { 
  111. $this->meta[ $key ] = $meta_value; 
  112.  
  113. /** 
  114. * Update our form id 
  115. * @access public 
  116. * @since 2.7 
  117. * @return bool 
  118. */ 
  119. public function update_form_id( $form_id ) { 
  120. if ( update_post_meta( $this->sub_id, '_form_id', $form_id ) ) { 
  121. $this->form_id = $form_id; 
  122. return true; 
  123. } else { 
  124. return false; 
  125.  
  126. /** 
  127. * Update our action 
  128. * @access public 
  129. * @since 2.7 
  130. * @return bool 
  131. */ 
  132. public function update_action( $action ) { 
  133. if ( update_post_meta( $this->sub_id, '_action', $action ) ) { 
  134. $this->action = $action; 
  135. return true; 
  136. } else { 
  137. return false; 
  138.  
  139. /** 
  140. * Update our sequential id 
  141. * @access public 
  142. * @since 2.7 
  143. * @return bool 
  144. */ 
  145. public function update_seq_num( $seq_num ) { 
  146. if ( update_post_meta( $this->sub_id, '_seq_num', $seq_num ) ) { 
  147. $this->seq_num = $seq_num; 
  148. return true; 
  149. } else { 
  150. return false; 
  151.  
  152. /** 
  153. * Update our user id 
  154. * @access public 
  155. * @since 2.7 
  156. * @return bool 
  157. */ 
  158. public function update_user_id( $user_id ) { 
  159. $args = array( 
  160. 'ID' => $this->sub_id,  
  161. 'post_author' => $user_id,  
  162. ); 
  163.  
  164. // Update the post into the database 
  165. if ( wp_update_post( $args ) ) { 
  166. $this->user_id = $user_id; 
  167. return true; 
  168. } else { 
  169. return false; 
  170.  
  171. /** 
  172. * Update our date submitted 
  173. * @access public 
  174. * @since 2.7 
  175. * @return bool 
  176. */ 
  177. public function update_date_submitted( $date ) { 
  178. $args = array( 
  179. 'ID' => $this->sub_id,  
  180. 'post_date' => $date,  
  181. 'post_date_gmt' => get_gmt_from_date( $date ),  
  182. ); 
  183.  
  184. // Update the post into the database 
  185. if ( wp_update_post( $args ) ) { 
  186. $this->date_submitted = $date; 
  187. return true; 
  188. } else { 
  189. return false; 
  190.  
  191. /** 
  192. * Update our date modified 
  193. * @access public 
  194. * @since 2.7 
  195. * @return bool 
  196. */ 
  197. public function update_date_modified( $date ) { 
  198. $args = array( 
  199. 'ID' => $this->sub_id,  
  200. 'post_modified' => $date,  
  201. 'post_modified_gmt' => get_gmt_from_date( $date ),  
  202. ); 
  203.  
  204. // Update the post into the database 
  205. if ( wp_update_post( $args ) ) { 
  206. $this->date_modified = $date; 
  207. return true; 
  208. } else { 
  209. return false; 
  210.  
  211. /** 
  212. * Add a meta value to our submission. 
  213. * @access public 
  214. * @since 2.7 
  215. * @return bool 
  216. */ 
  217. public function add_meta( $meta_key, $value ) { 
  218. if ( update_post_meta( $this->sub_id, $meta_key, $value ) ) { 
  219. $this->meta[ $meta_key ] = $value; 
  220. return true; 
  221. } else { 
  222. return false; 
  223.  
  224. /** 
  225. * Update a meta value. 
  226. * Wrapper for add_field(). 
  227. * @access public 
  228. * @since 2.7 
  229. * @return bool 
  230. */ 
  231. public function update_meta( $meta_key, $value ) { 
  232. return $this->add_meta( $meta_key, $value ); 
  233.  
  234. /** 
  235. * Delete a meta value. 
  236. * @access public 
  237. * @since 2.9 
  238. * @return bool 
  239. */ 
  240. public function delete_meta( $meta_key, $value = '' ) { 
  241. if ( empty( $value ) ) { 
  242. return delete_post_meta( $this->sub_id, $meta_key ); 
  243. } else { 
  244. return delete_post_meta( $this->sub_id, $meta_key, $value ); 
  245.  
  246.  
  247. /** 
  248. * Add a field value to our submission. 
  249. * @access public 
  250. * @since 2.7 
  251. * @return bool 
  252. */ 
  253. public function add_field( $field_id, $value ) { 
  254. $meta_key = '_field_' . $field_id; 
  255. if ( update_post_meta( $this->sub_id, $meta_key, $value ) ) { 
  256. $this->field[ $field_id ] = $value; 
  257. return true; 
  258. } else { 
  259. return false; 
  260.  
  261. /** 
  262. * Update a field value 
  263. * @access public 
  264. * @since 2.7 
  265. * @return bool 
  266. */ 
  267. public function update_field( $field_id, $value ) { 
  268. return $this->add_field( $field_id, $value ); 
  269.  
  270. /** 
  271. * Get a meta value from our submission by meta key 
  272. * @access public 
  273. * @since 2.7 
  274. * @return array|bool 
  275. */ 
  276. public function get_meta( $meta_key ) { 
  277. if ( ! isset ( $this->meta[ $meta_key ] ) ) { 
  278. $this->meta[ $meta_key ] = get_post_meta( $this->sub_id, $meta_key, true ); 
  279.  
  280. return $this->meta[ $meta_key ]; 
  281.  
  282. /** 
  283. * Get a field value from our submission by field id 
  284. * @access public 
  285. * @since 2.7 
  286. * @return array|bool 
  287. */ 
  288. public function get_field( $field_id ) { 
  289. if ( isset ( $this->fields[ $field_id ] ) ) { 
  290. return $this->fields[ $field_id ]; 
  291. } else { 
  292. return get_post_meta( $this->sub_id, '_field_' . $field_id, true ); 
  293.  
  294. /** 
  295. * Get a submission from the database, returning all the field data. 
  296. * @access public 
  297. * @since 2.7 
  298. * @return array $sub 
  299. */ 
  300. public function get_all_fields() { 
  301. if ( empty ( $this->fields ) ) { 
  302. $this->retrieve_fields(); 
  303. return $this->fields; 
  304.  
  305. /** 
  306. * Get a submission sequential ID by the post ID. 
  307. * This function puts together the prefix, sequential number, and postfix 
  308. * @access public 
  309. * @since 2.7 
  310. * @return string $seq_num 
  311. */ 
  312. public function get_seq_num() { 
  313. return apply_filters( 'nf_subs_seq_num', $this->seq_num, $this->sub_id ); 
  314.  
  315. /** 
  316. * Export our current submission. 
  317. * @access public 
  318. * @param array $sub_ids 
  319. * @param bool @return 
  320. * @since 2.7 
  321. * @return void 
  322. */ 
  323. public function export( $return = false ) { 
  324. if ( $return ) { 
  325. return Ninja_Forms()->subs()->export( $this->sub_id, $return ); 
  326. } else { 
  327. Ninja_Forms()->subs()->export( $this->sub_id, $return ); 
  328.  
  329. /** 
  330. * Delete this submission 
  331. * @access public 
  332. * @since 2.7 
  333. * @return void 
  334. */ 
  335. public function delete() { 
  336. wp_delete_post( $this->sub_id, true ); 
  337.