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. * Constructor 
  5. */ 
  6. public function __construct() 
  7. // Register our submission custom post type. 
  8. add_action( 'init', array( $this, 'custom_post_type' ), 5 ); 
  9.  
  10. // Filter Post Row Actions 
  11. add_filter( 'post_row_actions', array( $this, 'post_row_actions' ) ); 
  12.  
  13. // Change our submission columns. 
  14. add_filter( 'manage_nf_sub_posts_columns', array( $this, 'change_columns' ) ); 
  15.  
  16. // Add the appropriate data for our custom columns. 
  17. add_action( 'manage_posts_custom_column', array( $this, 'custom_columns' ), 10, 2 ); 
  18.  
  19. // Save our metabox values 
  20. add_action( 'save_post', array( $this, 'save_nf_sub' ), 10, 2 ); 
  21.  
  22. add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ), 10, 2 ); 
  23. add_action( 'add_meta_boxes', array( $this, 'remove_meta_boxes' ) ); 
  24.  
  25.  
  26. /** 
  27. * Custom Post Type 
  28. */ 
  29. function custom_post_type() { 
  30.  
  31. $labels = array( 
  32. 'name' => _x( 'Submissions', 'Post Type General Name', 'ninja_forms' ),  
  33. 'singular_name' => _x( 'Submission', 'Post Type Singular Name', 'ninja_forms' ),  
  34. 'menu_name' => __( 'Submissions', 'ninja_forms' ),  
  35. 'name_admin_bar' => __( 'Submissions', 'ninja_forms' ),  
  36. 'parent_item_colon' => __( 'Parent Item:', 'ninja_forms' ),  
  37. 'all_items' => __( 'All Items', 'ninja_forms' ),  
  38. 'add_new_item' => __( 'Add New Item', 'ninja_forms' ),  
  39. 'add_new' => __( 'Add New', 'ninja_forms' ),  
  40. 'new_item' => __( 'New Item', 'ninja_forms' ),  
  41. 'edit_item' => __( 'Edit Item', 'ninja_forms' ),  
  42. 'update_item' => __( 'Update Item', 'ninja_forms' ),  
  43. 'view_item' => __( 'View Item', 'ninja_forms' ),  
  44. 'search_items' => __( 'Search Item', 'ninja_forms' ),  
  45. 'not_found' => $this->not_found_message(),  
  46. 'not_found_in_trash' => __( 'Not found in Trash', 'ninja_forms' ),  
  47. ); 
  48. $args = array( 
  49. 'label' => __( 'Submission', 'ninja_forms' ),  
  50. 'description' => __( 'Form Submissions', 'ninja_forms' ),  
  51. 'labels' => $labels,  
  52. 'supports' => false,  
  53. 'hierarchical' => false,  
  54. 'public' => false,  
  55. 'show_ui' => true,  
  56. 'show_in_menu' => false,  
  57. 'menu_position' => 5,  
  58. 'show_in_admin_bar' => true,  
  59. 'show_in_nav_menus' => true,  
  60. 'can_export' => true,  
  61. 'has_archive' => true,  
  62. 'exclude_from_search' => false,  
  63. 'publicly_queryable' => true,  
  64. 'capability_type' => 'page',  
  65. ); 
  66. register_post_type( $this->cpt_slug, $args ); 
  67.  
  68. public function post_row_actions( $actions ) 
  69. if( $this->cpt_slug == get_post_type() ) { 
  70. unset( $actions[ 'view' ] ); 
  71. unset( $actions[ 'inline hide-if-no-js' ] ); 
  72. return $actions; 
  73.  
  74. public function change_columns( $columns ) 
  75. if( ! isset( $_GET[ 'form_id' ] ) ) return $columns; 
  76.  
  77. $columns = array( 
  78. 'cb' => '<input type="checkbox" />',  
  79. 'id' => __( '#', 'ninja-forms' ),  
  80. ); 
  81.  
  82. $form_id = absint( $_GET[ 'form_id' ] ); 
  83.  
  84. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  85.  
  86. foreach( $fields as $field ) { 
  87.  
  88. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() ); 
  89. if( in_array( $field->get_setting( 'type' ), array_values( $hidden_field_types ) ) ) continue; 
  90.  
  91. $id = $field->get_id(); 
  92. $label = $field->get_setting( 'label' ); 
  93. $admin_label = $field->get_setting( 'admin_label' ); 
  94.  
  95. $columns[ $id ] = ( $admin_label ) ? $admin_label : $label; 
  96.  
  97. $columns['sub_date'] = __( 'Date', 'ninja-forms' ); 
  98.  
  99. return $columns; 
  100.  
  101. public function custom_columns( $column, $sub_id ) 
  102. if( 'nf_sub' != get_post_type() ) { 
  103. return; 
  104.  
  105. $sub = Ninja_Forms()->form()->get_sub( $sub_id ); 
  106.  
  107. if( 'id' == $column ) { 
  108. echo apply_filters( 'nf_sub_table_seq_num', $sub->get_seq_num(), $sub_id, $column ); 
  109.  
  110. if( is_numeric( $column ) ) { 
  111. $value = $sub->get_field_value( $column ); 
  112. $field = Ninja_Forms()->form()->get_field( $column ); 
  113. echo apply_filters( 'ninja_forms_custom_columns', $value, $field ); 
  114.  
  115.  
  116. public function save_nf_sub( $nf_sub_id, $nf_sub ) 
  117. global $pagenow; 
  118.  
  119. if ( ! isset ( $_POST['nf_edit_sub'] ) || $_POST['nf_edit_sub'] != 1 ) 
  120. return $nf_sub_id; 
  121.  
  122. // verify if this is an auto save routine. 
  123. // If it is our form has not been submitted, so we dont want to do anything 
  124. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
  125. return $nf_sub_id; 
  126.  
  127. if ( $pagenow != 'post.php' ) 
  128. return $nf_sub_id; 
  129.  
  130. if ( $nf_sub->post_type != 'nf_sub' ) 
  131. return $nf_sub_id; 
  132.  
  133. /** Get the post type object. */ 
  134. $post_type = get_post_type_object( $nf_sub->post_type ); 
  135.  
  136. /** Check if the current user has permission to edit the post. */ 
  137. if ( !current_user_can( $post_type->cap->edit_post, $nf_sub_id ) ) 
  138. return $nf_sub_id; 
  139.  
  140. $sub = Ninja_Forms()->form()->sub( $nf_sub_id )->get(); 
  141.  
  142. foreach ( $_POST['fields'] as $field_id => $user_value ) { 
  143. $user_value = apply_filters( 'nf_edit_sub_user_value', $user_value, $field_id, $nf_sub_id ); 
  144. $sub->update_field_value( $field_id, $user_value ); 
  145.  
  146. $sub->save(); 
  147.  
  148. /** 
  149. * Meta Boxes 
  150. */ 
  151. public function add_meta_boxes( $post_type, $post ) 
  152. add_meta_box( 
  153. 'nf_sub_fields',  
  154. __( 'User Submitted Values', 'ninja-forms' ),  
  155. array( $this, 'fields_meta_box' ),  
  156. 'nf_sub',  
  157. 'normal',  
  158. 'default' 
  159. ); 
  160.  
  161. add_meta_box( 
  162. 'nf_sub_info',  
  163. __( 'Submission Info', 'ninja-forms' ),  
  164. array( $this, 'info_meta_box' ),  
  165. 'nf_sub',  
  166. 'side',  
  167. 'default' 
  168. ); 
  169.  
  170. /** 
  171. * Fields Meta Box 
  172. * @param $post 
  173. */ 
  174. public function fields_meta_box( $post ) 
  175. $form_id = get_post_meta( $post->ID, '_form_id', TRUE ); 
  176.  
  177. $sub = Ninja_Forms()->form()->get_sub( $post->ID ); 
  178.  
  179. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  180.  
  181. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() ); 
  182.  
  183. Ninja_Forms::template( 'admin-metabox-sub-fields.html.php', compact( 'fields', 'sub', 'hidden_field_types' ) ); 
  184.  
  185. /** 
  186. * Info Meta Box 
  187. * @param $post 
  188. */ 
  189. public function info_meta_box( $post ) 
  190. $sub = Ninja_Forms()->form()->sub( $post->ID )->get(); 
  191.  
  192. $seq_num = $sub->get_seq_num(); 
  193.  
  194. $status = ucwords( $sub->get_status() ); 
  195.  
  196. $user = apply_filters( 'nf_edit_sub_username', $sub->get_user()->data->user_nicename, $post->post_author ); 
  197.  
  198. $form_title = $sub->get_form_title(); 
  199.  
  200. $sub_date = apply_filters( 'nf_edit_sub_date_submitted', $sub->get_sub_date( 'm/d/Y H:i' ), $post->ID ); 
  201.  
  202. $mod_date = apply_filters( 'nf_edit_sub_date_modified', $sub->get_mod_date( 'm/d/Y H:i' ), $post->ID ); 
  203.  
  204. Ninja_Forms::template( 'admin-metabox-sub-info.html.php', compact( 'post', 'seq_num', 'status', 'user', 'form_title', 'sub_date', 'mod_date' ) ); 
  205.  
  206. /** 
  207. * Remove Meta Boxes 
  208. */ 
  209. public function remove_meta_boxes() 
  210. // Remove the default Publish metabox 
  211. remove_meta_box( 'submitdiv', 'nf_sub', 'side' ); 
  212.  
  213. /** 
  214. * PRIVATE METHODS 
  215. */ 
  216.  
  217. private function not_found_message() 
  218. if ( ! isset ( $_REQUEST['form_id'] ) || empty( $_REQUEST['form_id'] ) ) { 
  219. return __( 'Please select a form to view submissions', 'ninja-forms' ); 
  220. } else { 
  221. return __( 'No Submissions Found', 'ninja-forms' ); 
  222.