Submission.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Admin_CPT_Submission
  4. */
  5. class NF_Admin_CPT_Submission
  6. {
  7. protected $cpt_slug = 'nf_sub';
  8. public $screen_options;
  9. /**
  10. * Constructor
  11. */
  12. public function __construct()
  13. {
  14. // Register our submission custom post type.
  15. add_action( 'init', array( $this, 'custom_post_type' ), 5 );
  16. add_action( 'admin_print_styles', array( $this, 'enqueue_scripts' ) );
  17. // Filter Post Row Actions
  18. add_filter( 'post_row_actions', array( $this, 'post_row_actions' ), 10, 2 );
  19. // Change our submission columns.
  20. add_filter( 'manage_nf_sub_posts_columns', array( $this, 'change_columns' ) );
  21. // Add the appropriate data for our custom columns.
  22. add_action( 'manage_posts_custom_column', array( $this, 'custom_columns' ), 10, 2 );
  23. // Save our metabox values
  24. add_action( 'save_post', array( $this, 'save_nf_sub' ), 10, 2 );
  25. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 1 );
  26. add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ) );
  27. // Filter our submission capabilities
  28. add_filter( 'user_has_cap', array( $this, 'cap_filter' ), 10, 3 );
  29. // Filter our hidden columns by form ID.
  30. add_action( 'wp', array( $this, 'filter_hidden_columns' ) );
  31. // Save our hidden columns by form id.
  32. add_action( 'wp_ajax_nf_hide_columns', array( $this, 'hide_columns' ) );
  33. add_action( 'trashed_post', array( $this, 'nf_trash_sub' ) );
  34. }
  35. /**
  36. * Custom Post Type
  37. */
  38. function custom_post_type() {
  39. $labels = array(
  40. 'name' => _x( 'Submissions', 'Post Type General Name', 'ninja-forms' ),
  41. 'singular_name' => _x( 'Submission', 'Post Type Singular Name', 'ninja-forms' ),
  42. 'menu_name' => __( 'Submissions', 'ninja-forms' ),
  43. 'name_admin_bar' => __( 'Submissions', 'ninja-forms' ),
  44. 'parent_item_colon' => __( 'Parent Item:', 'ninja-forms' ),
  45. 'all_items' => __( 'All Items', 'ninja-forms' ),
  46. 'add_new_item' => __( 'Add New Item', 'ninja-forms' ),
  47. 'add_new' => __( 'Add New', 'ninja-forms' ),
  48. 'new_item' => __( 'New Item', 'ninja-forms' ),
  49. 'edit_item' => __( 'Edit Item', 'ninja-forms' ),
  50. 'update_item' => __( 'Update Item', 'ninja-forms' ),
  51. 'view_item' => __( 'View Item', 'ninja-forms' ),
  52. 'search_items' => __( 'Search Item', 'ninja-forms' ),
  53. 'not_found' => $this->not_found_message(),
  54. 'not_found_in_trash' => __( 'Not found in Trash', 'ninja-forms' ),
  55. );
  56. $args = array(
  57. 'label' => __( 'Submission', 'ninja-forms' ),
  58. 'description' => __( 'Form Submissions', 'ninja-forms' ),
  59. 'labels' => $labels,
  60. 'supports' => false,
  61. 'hierarchical' => false,
  62. 'public' => false,
  63. 'show_ui' => true,
  64. 'show_in_menu' => false,
  65. 'menu_position' => 5,
  66. 'show_in_admin_bar' => false,
  67. 'show_in_nav_menus' => false,
  68. 'can_export' => true,
  69. 'has_archive' => false,
  70. 'exclude_from_search' => true,
  71. 'publicly_queryable' => false,
  72. // 'rewrite' => false,
  73. 'capability_type' => 'nf_sub',
  74. 'capabilities' => array(
  75. 'publish_posts' => 'nf_sub',
  76. 'edit_posts' => 'nf_sub',
  77. 'edit_others_posts' => 'nf_sub',
  78. 'delete_posts' => 'nf_sub',
  79. 'delete_others_posts' => 'nf_sub',
  80. 'read_private_posts' => 'nf_sub',
  81. 'edit_post' => 'nf_sub',
  82. 'delete_post' => 'nf_sub',
  83. 'read_post' => 'nf_sub',
  84. ),
  85. );
  86. register_post_type( $this->cpt_slug, $args );
  87. }
  88. public function nf_trash_sub( $post_id )
  89. {
  90. // If this isn't a submission...
  91. if ( 'nf_sub' != get_post_type( $post_id ) ) {
  92. // Exit early.
  93. return false;
  94. }
  95. global $pagenow;
  96. // If we were on the post.php page...
  97. if ( 'post.php' == $pagenow ) {
  98. // Redirect the user to the submissions page for the form that submission belonged to.
  99. wp_safe_redirect( admin_url( 'edit.php?post_status=all&post_type=nf_sub&form_id=' . get_post_meta( $post_id, '_form_id', true ) ) );
  100. exit;
  101. }
  102. }
  103. public function enqueue_scripts()
  104. {
  105. global $pagenow, $typenow;
  106. // Bail if we aren't on the edit.php page or we aren't editing our custom post type.
  107. if ( ( $pagenow != 'edit.php' && $pagenow != 'post.php' ) || $typenow != 'nf_sub' )
  108. return false;
  109. $form_id = isset ( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : '';
  110. wp_enqueue_script( 'subs-cpt',
  111. Ninja_Forms::$url . 'deprecated/assets/js/min/subs-cpt.min.js',
  112. array( 'jquery', 'jquery-ui-datepicker' ) );
  113. wp_localize_script( 'subs-cpt', 'nf_sub', array( 'form_id' => $form_id ) );
  114. }
  115. public function post_row_actions( $actions, $sub )
  116. {
  117. if ( $this->cpt_slug == get_post_type() ){
  118. unset( $actions[ 'view' ] );
  119. unset( $actions[ 'inline hide-if-no-js' ] );
  120. $export_url = add_query_arg( array( 'action' => 'export', 'post[]' => $sub->ID ) );
  121. $actions[ 'export' ] = sprintf( '<a href="%s">%s</a>', $export_url, __( 'Export', 'ninja-forms' ) );
  122. }
  123. return $actions;
  124. }
  125. public function change_columns( $columns )
  126. {
  127. if( ! isset( $_GET[ 'form_id' ] ) ) return $columns;
  128. $form_id = absint( $_GET[ 'form_id' ] );
  129. static $columns;
  130. if( $columns ) return $columns;
  131. $columns = array(
  132. 'cb' => '<input type="checkbox" />',
  133. 'id' => __( '#', 'ninja-forms' ),
  134. );
  135. $form_cache = WPN_Helper::get_nf_cache( $form_id );
  136. $form_fields = Ninja_Forms()->form( $form_id )->get_fields();
  137. foreach( $form_fields as $field ) {
  138. if( is_object( $field ) ) {
  139. $field = array(
  140. 'id' => $field->get_id(),
  141. 'settings' => $field->get_settings()
  142. );
  143. }
  144. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() );
  145. if( in_array( $field[ 'settings' ][ 'type' ], array_values( $hidden_field_types ) ) ) continue;
  146. $id = $field[ 'id' ];
  147. $label = $field[ 'settings' ][ 'label' ];
  148. $columns[ $id ] = ( isset( $field[ 'settings' ][ 'admin_label' ] ) && $field[ 'settings' ][ 'admin_label' ] ) ? $field[ 'settings' ][ 'admin_label' ] : $label;
  149. }
  150. $columns['sub_date'] = __( 'Date', 'ninja-forms' );
  151. return $columns;
  152. }
  153. public function custom_columns( $column, $sub_id )
  154. {
  155. if( 'nf_sub' != get_post_type() ) {
  156. return;
  157. }
  158. $sub = Ninja_Forms()->form()->get_sub( $sub_id );
  159. if( 'id' == $column ) {
  160. echo apply_filters( 'nf_sub_table_seq_num', $sub->get_seq_num(), $sub_id, $column );
  161. }
  162. $form_id = absint( $_GET[ 'form_id' ] );
  163. if( is_numeric( $column ) ){
  164. $value = $sub->get_field_value( $column );
  165. static $fields;
  166. if( ! isset( $fields[ $column ] ) ) {
  167. $fields[$column] = Ninja_Forms()->form( $form_id )->get_field( $column );
  168. }
  169. $field = $fields[$column];
  170. echo apply_filters( 'ninja_forms_custom_columns', $value, $field, $sub_id );
  171. }
  172. }
  173. public function save_nf_sub( $nf_sub_id, $nf_sub )
  174. {
  175. global $pagenow;
  176. if ( ! isset ( $_POST['nf_edit_sub'] ) || $_POST['nf_edit_sub'] != 1 )
  177. return $nf_sub_id;
  178. // verify if this is an auto save routine.
  179. // If it is our form has not been submitted, so we dont want to do anything
  180. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  181. return $nf_sub_id;
  182. if ( $pagenow != 'post.php' )
  183. return $nf_sub_id;
  184. if ( $nf_sub->post_type != 'nf_sub' )
  185. return $nf_sub_id;
  186. /* Get the post type object. */
  187. $post_type = get_post_type_object( $nf_sub->post_type );
  188. /* Check if the current user has permission to edit the post. */
  189. if ( !current_user_can( $post_type->cap->edit_post, $nf_sub_id ) )
  190. return $nf_sub_id;
  191. $sub = Ninja_Forms()->form()->sub( $nf_sub_id )->get();
  192. foreach ( $_POST['fields'] as $field_id => $user_value ) {
  193. $user_value = apply_filters( 'nf_edit_sub_user_value', $user_value, $field_id, $nf_sub_id );
  194. $sub->update_field_value( $field_id, $user_value );
  195. }
  196. $sub->save();
  197. }
  198. /**
  199. * Meta Boxes
  200. */
  201. public function add_meta_boxes( $post_type )
  202. {
  203. add_meta_box(
  204. 'nf_sub_fields',
  205. __( 'User Submitted Values', 'ninja-forms' ),
  206. array( $this, 'fields_meta_box' ),
  207. 'nf_sub',
  208. 'normal',
  209. 'default'
  210. );
  211. add_meta_box(
  212. 'nf_sub_info',
  213. __( 'Submission Info', 'ninja-forms' ),
  214. array( $this, 'info_meta_box' ),
  215. 'nf_sub',
  216. 'side',
  217. 'default'
  218. );
  219. }
  220. /**
  221. * Fields Meta Box
  222. *
  223. * @param $post
  224. */
  225. public function fields_meta_box( $post )
  226. {
  227. $form_id = get_post_meta( $post->ID, '_form_id', TRUE );
  228. $sub = Ninja_Forms()->form()->get_sub( $post->ID );
  229. $fields = Ninja_Forms()->form( $form_id )->get_fields();
  230. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() );
  231. Ninja_Forms::template( 'admin-metabox-sub-fields.html.php', compact( 'fields', 'sub', 'hidden_field_types' ) );
  232. }
  233. public static function sort_fields( $a, $b )
  234. {
  235. if ( $a->get_setting( 'order' ) == $b->get_setting( 'order' ) ) {
  236. return 0;
  237. }
  238. return ( $a->get_setting( 'order' ) < $b->get_setting( 'order' ) ) ? -1 : 1;
  239. }
  240. /**
  241. * Info Meta Box
  242. *
  243. * @param $post
  244. */
  245. public function info_meta_box( $post )
  246. {
  247. $sub = Ninja_Forms()->form()->sub( $post->ID )->get();
  248. $seq_num = $sub->get_seq_num();
  249. $status = ucwords( $sub->get_status() );
  250. if ($sub->get_user()) {
  251. $user = apply_filters('nf_edit_sub_username', $sub->get_user()->data->user_nicename, $post->post_author);
  252. } else {
  253. $user = __( 'Anonymous', 'ninja-forms' );
  254. }
  255. $form_title = $sub->get_form_title();
  256. $sub_date = apply_filters( 'nf_edit_sub_date_submitted', $sub->get_sub_date( 'm/d/Y H:i' ), $post->ID );
  257. $mod_date = apply_filters( 'nf_edit_sub_date_modified', $sub->get_mod_date( 'm/d/Y H:i' ), $post->ID );
  258. Ninja_Forms::template( 'admin-metabox-sub-info.html.php', compact( 'post', 'seq_num', 'status', 'user', 'form_title', 'sub_date', 'mod_date' ) );
  259. }
  260. /**
  261. * Remove Meta Boxes
  262. */
  263. public function remove_meta_boxes()
  264. {
  265. // Remove the default Publish metabox
  266. remove_meta_box( 'submitdiv', 'nf_sub', 'side' );
  267. }
  268. public function cap_filter( $allcaps, $cap, $args )
  269. {
  270. $sub_cap = apply_filters('ninja_forms_admin_submissions_capabilities', 'manage_options');
  271. if (!empty($allcaps[$sub_cap])) {
  272. $allcaps['nf_sub'] = true;
  273. }
  274. return $allcaps;
  275. }
  276. /**
  277. * Filter our hidden columns so that they are handled on a per-form basis.
  278. *
  279. * @access public
  280. * @since 2.7
  281. * @return void
  282. */
  283. public function filter_hidden_columns() {
  284. global $pagenow;
  285. // Bail if we aren't on the edit.php page, we aren't editing our custom post type, or we don't have a form_id set.
  286. if ( $pagenow != 'edit.php' || ! isset ( $_REQUEST['post_type'] ) || $_REQUEST['post_type'] != 'nf_sub' || ! isset ( $_REQUEST['form_id'] ) )
  287. return false;
  288. // Grab our current user.
  289. $user = wp_get_current_user();
  290. // Grab our form id.
  291. $form_id = absint( $_REQUEST['form_id'] );
  292. // Get the columns that should be hidden for this form ID.
  293. $hidden_columns = get_user_option( 'manageedit-nf_subcolumnshidden-form-' . $form_id );
  294. // Checks to see if hidden columns are in the format expected for 2.9.x and converts formatting.
  295. if( ! empty( $hidden_columns ) && strpos( $hidden_columns[ 0 ], 'form_' ) !== false ) {
  296. $hidden_columns = $this->convert_hidden_columns( $form_id, $hidden_columns );
  297. }
  298. if ( $hidden_columns === false ) {
  299. // If we don't have custom hidden columns set up for this form, then only show the first five columns.
  300. // Get our column headers
  301. $columns = get_column_headers( 'edit-nf_sub' );
  302. $hidden_columns = array();
  303. $x = 0;
  304. foreach ( $columns as $slug => $name ) {
  305. if ( $x > 5 ) {
  306. if ( $slug != 'sub_date' )
  307. $hidden_columns[] = $slug;
  308. }
  309. $x++;
  310. }
  311. }
  312. update_user_option( $user->ID, 'manageedit-nf_subcolumnshidden', $hidden_columns, true );
  313. }
  314. /**
  315. * Convert Hidden Columns
  316. * Looks for 2.9.x hidden columns formatting and converts it to the formatting 3.0 expects.
  317. * @param $form_id
  318. * @param $hidden_columns
  319. * @return mixed
  320. */
  321. private function convert_hidden_columns( $form_id, $hidden_columns )
  322. {
  323. $id = 'form_' . $form_id . '_field_';
  324. if( 'sub_date' !== $hidden_columns ) {
  325. $hidden_columns = str_replace( $id, '', $hidden_columns );
  326. }
  327. return $hidden_columns;
  328. }
  329. /**
  330. * Save our hidden columns per form id.
  331. *
  332. * @access public
  333. * @since 2.7
  334. * @return void
  335. */
  336. public function hide_columns() {
  337. // Grab our current user.
  338. $user = wp_get_current_user();
  339. // Grab our form id.
  340. $form_id = absint( $_REQUEST['form_id'] );
  341. $hidden = isset( $_POST['hidden'] ) ? explode( ',', esc_html( $_POST['hidden'] ) ) : array();
  342. $hidden = array_filter( $hidden );
  343. update_user_option( $user->ID, 'manageedit-nf_subcolumnshidden-form-' . $form_id, $hidden, true );
  344. die();
  345. }
  346. /*
  347. * PRIVATE METHODS
  348. */
  349. private function not_found_message()
  350. {
  351. if ( ! isset ( $_REQUEST['form_id'] ) || empty( $_REQUEST['form_id'] ) ) {
  352. return __( 'Please select a form to view submissions', 'ninja-forms' );
  353. } else {
  354. return __( 'No Submissions Found', 'ninja-forms' );
  355. }
  356. }
  357. }