NF_Admin_Menus_Submissions

Class NF_Admin_Menus_Submissions.

Defined (1)

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

/includes/Admin/Menus/Submissions.php  
  1. final class NF_Admin_Menus_Submissions extends NF_Abstracts_Submenu 
  2. /** 
  3. * @var string 
  4. */ 
  5. public $parent_slug = 'ninja-forms'; 
  6.  
  7. /** 
  8. * @var string 
  9. */ 
  10. public $page_title = 'Submissions'; 
  11.  
  12. /** 
  13. * @var string 
  14. */ 
  15. public $menu_slug = 'edit.php?post_type=nf_sub'; 
  16.  
  17. /** 
  18. * @var null 
  19. */ 
  20. public $function = NULL; 
  21.  
  22. /** 
  23. * Constructor 
  24. */ 
  25. public function __construct() 
  26. parent::__construct(); 
  27.  
  28. add_filter( 'manage_nf_sub_posts_columns', array( $this, 'change_columns' ) ); 
  29.  
  30. add_action( 'manage_posts_custom_column', array( $this, 'custom_columns' ), 10, 2 ); 
  31.  
  32. add_filter('months_dropdown_results', array( $this, 'remove_filter_show_all_dates' ), 9999 ); 
  33.  
  34. add_action( 'restrict_manage_posts', array( $this, 'add_filters' ) ); 
  35.  
  36. add_filter( 'parse_query', array( $this, 'table_filter' ) ); 
  37.  
  38. add_filter( 'posts_clauses', array( $this, 'search' ), 20, 1 ); 
  39.  
  40. add_filter( 'bulk_actions-edit-nf_sub', array( $this, 'remove_bulk_edit' ) ); 
  41.  
  42. add_action( 'admin_footer-edit.php', array( $this, 'bulk_admin_footer' ) ); 
  43.  
  44. add_action( 'load-edit.php', array( $this, 'export_listen' ) ); 
  45.  
  46. add_action('admin_head', array( $this, 'hide_page_title_action' ) ); 
  47.  
  48. public function get_page_title() 
  49. return __( 'Submissions', 'ninja-forms' ); 
  50.  
  51. /** 
  52. * Display 
  53. */ 
  54. public function display() 
  55. // This section intentionally left blank. 
  56.  
  57. /** 
  58. * Change Columns 
  59. * @return array 
  60. */ 
  61. public function change_columns() 
  62. $form_id = ( isset( $_GET['form_id'] ) ) ? $_GET['form_id'] : FALSE; 
  63.  
  64. if( ! $form_id ) return array(); 
  65.  
  66. static $cols; 
  67.  
  68. if( $cols ) return $cols; 
  69.  
  70. $cols = array( 
  71. 'cb' => '<input type="checkbox" />',  
  72. 'seq_num' => __( '#', 'ninja-forms' ),  
  73. ); 
  74.  
  75. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  76.  
  77. $hidden_field_types = apply_filters( 'ninja_forms_sub_hidden_field_types', array() ); 
  78.  
  79. foreach( $fields as $field ) { 
  80.  
  81. if( in_array( $field->get_setting( 'type' ), $hidden_field_types ) ) continue; 
  82.  
  83. if ( $field->get_setting( 'admin_label' ) ) { 
  84. $cols[ 'field_' . $field->get_id() ] = $field->get_setting( 'admin_label' ); 
  85. } else { 
  86. $cols[ 'field_' . $field->get_id() ] = $field->get_setting( 'label' );  
  87.  
  88.  
  89. $cols[ 'sub_date' ] = __( 'Date', 'ninja-forms' ); 
  90.  
  91. return $cols; 
  92.  
  93. /** 
  94. * Custom Columns 
  95. * @param $column 
  96. * @param $sub_id 
  97. */ 
  98. public function custom_columns( $column, $sub_id ) 
  99. $sub = Ninja_Forms()->form()->get_sub( $sub_id ); 
  100.  
  101. switch( $column ) { 
  102. case 'seq_num': 
  103. echo $this->custom_columns_seq_num( $sub ); 
  104. break; 
  105. case 'sub_date': 
  106. echo $this->custom_columns_sub_date( $sub ); 
  107. break; 
  108. default: 
  109. echo $this->custom_columns_field( $sub, $column ); 
  110.  
  111. /** 
  112. * Remove Filter: Show All Dates 
  113. * @param $months 
  114. * @return array 
  115. */ 
  116. public function remove_filter_show_all_dates( $months ) 
  117. if( ! isset( $_GET[ 'post_type' ] ) || 'nf_sub' != $_GET[ 'post_type' ] ) return $months; 
  118.  
  119. // Returning an empty array should hide the dropdown. 
  120. return array(); 
  121.  
  122. /** 
  123. * Add Filters 
  124. * @return bool 
  125. */ 
  126. public function add_filters() 
  127. global $typenow; 
  128.  
  129. // Bail if we aren't in our submission custom post type. 
  130. if ( $typenow != 'nf_sub' ) return false; 
  131.  
  132. $forms = Ninja_Forms()->form()->get_forms(); 
  133.  
  134. $form_options = array(); 
  135. foreach( $forms as $form ) { 
  136. $form_options[ $form->get_id() ] = $form->get_setting( 'title' ); 
  137. $form_options = apply_filters( 'ninja_forms_submission_filter_form_options', $form_options ); 
  138.  
  139. if( isset( $_GET[ 'form_id' ] ) ) { 
  140. $form_selected = $_GET[ 'form_id' ]; 
  141. } else { 
  142. $form_selected = 0; 
  143.  
  144. if( isset( $_GET[ 'begin_date' ] ) ) { 
  145. $begin_date = $_GET[ 'begin_date' ]; 
  146. } else { 
  147. $begin_date = ''; 
  148.  
  149. if( isset( $_GET[ 'end_date' ] ) ) { 
  150. $end_date = $_GET[ 'end_date' ]; 
  151. } else { 
  152. $end_date = ''; 
  153.  
  154. Ninja_Forms::template( 'admin-menu-subs-filter.html.php', compact( 'form_options', 'form_selected', 'begin_date', 'end_date' ) ); 
  155.  
  156. wp_enqueue_script('jquery-ui-datepicker'); 
  157. wp_enqueue_style( 'jquery-ui-datepicker', Ninja_Forms::$url .'deprecated/assets/css/jquery-ui-fresh.min.css' ); 
  158.  
  159. public function table_filter( $query ) 
  160. global $pagenow; 
  161.  
  162. if( $pagenow != 'edit.php' || ! is_admin() || ! isset( $query->query['post_type'] ) || 'nf_sub' != $query->query['post_type'] || ! is_main_query() ) return; 
  163.  
  164. $vars = &$query->query_vars; 
  165.  
  166. $form_id = ( ! empty( $_GET['form_id'] ) ) ? $_GET['form_id'] : 0; 
  167.  
  168. $vars = $this->table_filter_by_form( $vars, $form_id ); 
  169.  
  170. $vars = $this->table_filter_by_date( $vars ); 
  171.  
  172. $vars = apply_filters( 'ninja_forms_sub_table_qv', $vars, $form_id ); 
  173.  
  174. public function search( $pieces ) { 
  175. global $typenow; 
  176. // filter to select search query 
  177. if ( is_search() && is_admin() && $typenow == 'nf_sub' && isset ( $_GET['s'] ) ) { 
  178. global $wpdb; 
  179.  
  180. $keywords = explode(' ', get_query_var('s')); 
  181. $query = ""; 
  182.  
  183. foreach ($keywords as $word) { 
  184.  
  185. $query .= " (mypm1.meta_value LIKE '%{$word}%') OR "; 
  186.  
  187. if (!empty($query)) { 
  188. // add to where clause 
  189. $pieces['where'] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "( {$query} (({$wpdb->posts}.post_title LIKE '%", $pieces['where']); 
  190.  
  191. $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)"; 
  192.  
  193. return ($pieces); 
  194.  
  195. public function remove_bulk_edit( $actions ) { 
  196. unset( $actions['edit'] ); 
  197. return $actions; 
  198.  
  199. public function bulk_admin_footer() { 
  200. global $post_type; 
  201.  
  202. if ( ! is_admin() ) 
  203. return false; 
  204.  
  205. if( $post_type == 'nf_sub' && isset ( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] == 'all' ) { 
  206. ?> 
  207. <script type="text/javascript"> 
  208. jQuery(document).ready(function() { 
  209. jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']"); 
  210. jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']"); 
  211. <?php 
  212. if ( ( isset ( $_POST['action'] ) && $_POST['action'] == 'export' ) || ( isset ( $_POST['action2'] ) && $_POST['action2'] == 'export' ) ) { 
  213. ?> 
  214. setInterval(function() { 
  215. jQuery( "select[name='action'" ).val( '-1' ); 
  216. jQuery( "select[name='action2'" ).val( '-1' ); 
  217. jQuery( '#posts-filter' ).submit(); 
  218. }, 5000); 
  219. <?php 
  220.  
  221. if ( isset ( $_REQUEST['form_id'] ) && ! empty ( $_REQUEST['form_id'] ) ) { 
  222. $redirect = urlencode( remove_query_arg( array( 'download_all', 'download_file' ) ) ); 
  223. $url = admin_url( 'admin.php?page=nf-processing&action=download_all_subs&form_id=' . absint( $_REQUEST['form_id'] ) . '&redirect=' . $redirect ); 
  224. $url = esc_url( $url ); 
  225. ?> 
  226. var button = '<a href="<?php echo $url; ?>" class=<?php __( "button-secondary nf-download-all", 'ninja-forms' ) ;?> . '>' . <?php echo __( 'Download All Submissions', 'ninja-forms' ); ?></a>'; 
  227. // jQuery( '#doaction2' ).after( button ); 
  228. <?php 
  229.  
  230. if ( isset ( $_REQUEST['download_all'] ) && $_REQUEST['download_all'] != '' ) { 
  231. $redirect = esc_url_raw( add_query_arg( array( 'download_file' => esc_html( $_REQUEST['download_all'] ) ) ) ); 
  232. $redirect = remove_query_arg( array( 'download_all' ), $redirect ); 
  233. ?> 
  234. document.location.href = "<?php echo $redirect; ?>"; 
  235. <?php 
  236.  
  237. ?> 
  238. }); 
  239. </script> 
  240. <?php 
  241.  
  242. public function export_listen() 
  243. // Bail if we aren't in the admin 
  244. if (!is_admin()) 
  245. return false; 
  246.  
  247. if (!isset ($_REQUEST['form_id']) || empty ($_REQUEST['form_id'])) { 
  248. return false; 
  249.  
  250. if (isset ($_REQUEST['export_single']) && !empty($_REQUEST['export_single'])) { 
  251. Ninja_Forms()->sub(esc_html($_REQUEST['export_single']))->export(); 
  252.  
  253. if ((isset ($_REQUEST['action']) && $_REQUEST['action'] == 'export') || (isset ($_REQUEST['action2']) && $_REQUEST['action2'] == 'export')) { 
  254.  
  255. $sub_ids = array(); 
  256. if (isset($_REQUEST['post'])) { 
  257. $sub_ids = WPN_Helper::esc_html($_REQUEST['post']); 
  258.  
  259. Ninja_Forms()->form( $_REQUEST['form_id'] )->export_subs( $sub_ids ); 
  260.  
  261. if (isset ($_REQUEST['download_file']) && !empty($_REQUEST['download_file'])) { 
  262.  
  263. // Open our download all file 
  264. $filename = esc_html($_REQUEST['download_file']); 
  265.  
  266. $upload_dir = wp_upload_dir(); 
  267.  
  268. $file_path = trailingslashit($upload_dir['path']) . $filename . '.csv'; 
  269.  
  270. if (file_exists($file_path)) { 
  271. $myfile = file_get_contents($file_path); 
  272. } else { 
  273. $redirect = esc_url_raw(remove_query_arg(array('download_file', 'download_all'))); 
  274. wp_redirect($redirect); 
  275. die(); 
  276.  
  277. unlink($file_path); 
  278.  
  279. $form_name = Ninja_Forms()->form(absint($_REQUEST['form_id']))->get()->get_setting('title'); 
  280. $form_name = sanitize_title($form_name); 
  281.  
  282. $today = date('Y-m-d', current_time('timestamp')); 
  283.  
  284. $filename = apply_filters('ninja_forms_download_all_filename', $form_name . '-all-subs-' . $today); 
  285.  
  286. header('Content-type: application/csv'); 
  287. header('Content-Disposition: attachment; filename="' . $filename . '.csv"'); 
  288. header('Pragma: no-cache'); 
  289. header('Expires: 0'); 
  290.  
  291. echo $myfile; 
  292.  
  293. die(); 
  294.  
  295. public function hide_page_title_action() { 
  296.  
  297. if( 
  298. ( isset( $_GET[ 'post_type' ] ) && 'nf_sub' == $_GET[ 'post_type'] ) || 
  299. 'nf_sub' == get_post_type() 
  300. ) { 
  301. echo '<style type="text/css">.page-title-action{display: none;}</style>'; 
  302.  
  303. /** 
  304. * PRIVATE METHODS 
  305. */ 
  306.  
  307. /** 
  308. * Custom Columns: ID 
  309. * @param $sub 
  310. * @return mixed 
  311. */ 
  312. private function custom_columns_seq_num( $sub ) 
  313. return $sub->get_seq_num(); 
  314.  
  315. /** 
  316. * Custom Columns: Submission Date 
  317. * @param $sub 
  318. * @return mixed 
  319. */ 
  320. private function custom_columns_sub_date( $sub ) 
  321. return $sub->get_sub_date(); 
  322.  
  323. /** 
  324. * Custom Columns: Field 
  325. * @param $sub 
  326. * @param $column 
  327. * @return bool 
  328. */ 
  329. private function custom_columns_field( $sub, $column ) 
  330. if( FALSE === strpos( $column, 'field_' ) ) return FALSE; 
  331.  
  332. $field_id = str_replace( 'field_', '', $column ); 
  333.  
  334. return $sub->get_field_value( $field_id ); 
  335.  
  336. private function table_filter_by_form( $vars, $form_id ) 
  337. if ( ! isset ( $vars['meta_query'] ) ) { 
  338. $vars['meta_query'] = array( 
  339. array( 
  340. 'key' => '_form_id',  
  341. 'value' => $form_id,  
  342. 'compare' => '=',  
  343. ),  
  344. ); 
  345.  
  346. return $vars; 
  347.  
  348. private function table_filter_by_date( $vars ) 
  349. if( empty( $_GET[ 'begin_date' ] ) || empty( $_GET[ 'end_date' ] ) ) return $vars; 
  350.  
  351. $begin_date = $_GET[ 'begin_date' ]; 
  352. $end_date = $_GET[ 'end_date' ]; 
  353.  
  354. if( $begin_date > $end_date ) { 
  355. $temp_date = $begin_date; 
  356. $begin_date = $end_date; 
  357. $end_date = $temp_date; 
  358.  
  359. if ( ! isset ( $vars['date_query'] ) ) { 
  360.  
  361. $vars['date_query'] = array( 
  362. 'after' => $begin_date,  
  363. 'before' => $end_date 
  364. ); 
  365.  
  366. return $vars; 
  367.  
  368. public function get_capability() 
  369. return apply_filters( 'ninja_forms_admin_submissions_capabilities', $this->capability ); 
  370.