NF_Admin_CPT_Submission

Class NF_Admin_CPT_Submission.

Defined (1)

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

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