sub.php 8.0 KB

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