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