/includes/Admin/Menus/Submissions.php

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