/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. $cols = array( 
  73. 'cb' => '<input type="checkbox" />',  
  74. 'seq_num' => __( '#', 'ninja-forms' ),  
  75. ); 
  76.  
  77. $fields = Ninja_Forms()->form( $form_id )->get_fields(); 
  78.  
  79. $hidden_field_types = apply_filters( 'ninja_forms_sub_hidden_field_types', array() ); 
  80.  
  81. foreach( $fields as $field ) { 
  82.  
  83. if( in_array( $field->get_setting( 'type' ), $hidden_field_types ) ) continue; 
  84.  
  85. // TODO: Add support for 'Admin Labels' 
  86. $cols[ 'field_' . $field->get_id() ] = $field->get_setting( 'label' ); 
  87.  
  88. $cols[ 'sub_date' ] = __( 'Date', 'ninja-forms' ); 
  89.  
  90. return $cols; 
  91.  
  92. /** 
  93. * Custom Columns 
  94. * 
  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. * 
  114. * @param $months 
  115. * @return array 
  116. */ 
  117. public function remove_filter_show_all_dates( $months ) 
  118. if( ! isset( $_GET[ 'post_type' ] ) || 'nf_sub' != $_GET[ 'post_type' ] ) return $months; 
  119.  
  120. // Returning an empty array should hide the dropdown. 
  121. return array(); 
  122.  
  123. /** 
  124. * Add Filters 
  125. * 
  126. * @return bool 
  127. */ 
  128. public function add_filters() 
  129. global $typenow; 
  130.  
  131. // Bail if we aren't in our submission custom post type. 
  132. if ( $typenow != 'nf_sub' ) return false; 
  133.  
  134. $forms = Ninja_Forms()->form()->get_forms(); 
  135.  
  136. $form_options = array(); 
  137. foreach( $forms as $form ) { 
  138. $form_options[ $form->get_id() ] = $form->get_setting( 'title' ); 
  139. $form_options = apply_filters( 'ninja_forms_submission_filter_form_options', $form_options ); 
  140.  
  141. if( isset( $_GET[ 'form_id' ] ) ) { 
  142. $form_selected = $_GET[ 'form_id' ]; 
  143. } else { 
  144. $form_selected = 0; 
  145.  
  146. if( isset( $_GET[ 'begin_date' ] ) ) { 
  147. $begin_date = $_GET[ 'begin_date' ]; 
  148. } else { 
  149. $begin_date = ''; 
  150.  
  151. if( isset( $_GET[ 'end_date' ] ) ) { 
  152. $end_date = $_GET[ 'end_date' ]; 
  153. } else { 
  154. $end_date = ''; 
  155.  
  156. Ninja_Forms::template( 'admin-menu-subs-filter.html.php', compact( 'form_options', 'form_selected', 'begin_date', 'end_date' ) ); 
  157.  
  158. wp_enqueue_script('jquery-ui-datepicker'); 
  159. wp_enqueue_style( 'jquery-ui-datepicker', Ninja_Forms::$url .'deprecated/assets/css/jquery-ui-fresh.min.css' ); 
  160.  
  161. public function table_filter( $query ) 
  162. global $pagenow; 
  163.  
  164. if( $pagenow != 'edit.php' || ! is_admin() || ! isset( $query->query['post_type'] ) || 'nf_sub' != $query->query['post_type'] || ! is_main_query() ) return; 
  165.  
  166. $vars = &$query->query_vars; 
  167.  
  168. $form_id = ( ! empty( $_GET['form_id'] ) ) ? $_GET['form_id'] : 0; 
  169.  
  170. $vars = $this->table_filter_by_form( $vars, $form_id ); 
  171.  
  172. $vars = $this->table_filter_by_date( $vars ); 
  173.  
  174. $vars = apply_filters( 'ninja_forms_sub_table_qv', $vars, $form_id ); 
  175.  
  176. public function search( $pieces ) { 
  177. global $typenow; 
  178. // filter to select search query 
  179. if ( is_search() && is_admin() && $typenow == 'nf_sub' && isset ( $_GET['s'] ) ) { 
  180. global $wpdb; 
  181.  
  182. $keywords = explode(' ', get_query_var('s')); 
  183. $query = ""; 
  184.  
  185. foreach ($keywords as $word) { 
  186.  
  187. $query .= " (mypm1.meta_value LIKE '%{$word}%') OR "; 
  188.  
  189. if (!empty($query)) { 
  190. // add to where clause 
  191. $pieces['where'] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "( {$query} (({$wpdb->posts}.post_title LIKE '%", $pieces['where']); 
  192.  
  193. $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)"; 
  194.  
  195. return ($pieces); 
  196.  
  197. public function remove_bulk_edit( $actions ) { 
  198. unset( $actions['edit'] ); 
  199. return $actions; 
  200.  
  201. public function bulk_admin_footer() { 
  202. global $post_type; 
  203.  
  204. if ( ! is_admin() ) 
  205. return false; 
  206.  
  207. if( $post_type == 'nf_sub' && isset ( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] == 'all' ) { 
  208. ?> 
  209. <script type="text/javascript"> 
  210. jQuery(document).ready(function() { 
  211. jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']"); 
  212. jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']"); 
  213. <?php 
  214. if ( ( isset ( $_POST['action'] ) && $_POST['action'] == 'export' ) || ( isset ( $_POST['action2'] ) && $_POST['action2'] == 'export' ) ) { 
  215. ?> 
  216. setInterval(function() { 
  217. jQuery( "select[name='action'" ).val( '-1' ); 
  218. jQuery( "select[name='action2'" ).val( '-1' ); 
  219. jQuery( '#posts-filter' ).submit(); 
  220. }, 5000); 
  221. <?php 
  222.  
  223. if ( isset ( $_REQUEST['form_id'] ) && ! empty ( $_REQUEST['form_id'] ) ) { 
  224. $redirect = urlencode( remove_query_arg( array( 'download_all', 'download_file' ) ) ); 
  225. $url = admin_url( 'admin.php?page=nf-processing&action=download_all_subs&form_id=' . absint( $_REQUEST['form_id'] ) . '&redirect=' . $redirect ); 
  226. $url = esc_url( $url ); 
  227. ?> 
  228. var button = '<a href="<?php echo $url; ?>" class=<?php __( "button-secondary nf-download-all", 'ninja-forms' ) ;?> . '>' . <?php echo __( 'Download All Submissions', 'ninja-forms' ); ?></a>'; 
  229. jQuery( '#doaction2' ).after( button ); 
  230. <?php 
  231.  
  232. if ( isset ( $_REQUEST['download_all'] ) && $_REQUEST['download_all'] != '' ) { 
  233. $redirect = esc_url_raw( add_query_arg( array( 'download_file' => esc_html( $_REQUEST['download_all'] ) ) ) ); 
  234. $redirect = remove_query_arg( array( 'download_all' ), $redirect ); 
  235. ?> 
  236. document.location.href = "<?php echo $redirect; ?>"; 
  237. <?php 
  238.  
  239. ?> 
  240. }); 
  241. </script> 
  242. <?php 
  243.  
  244. public function export_listen() 
  245. // Bail if we aren't in the admin 
  246. if (!is_admin()) 
  247. return false; 
  248.  
  249. if (!isset ($_REQUEST['form_id']) || empty ($_REQUEST['form_id'])) { 
  250. return false; 
  251.  
  252. if (isset ($_REQUEST['export_single']) && !empty($_REQUEST['export_single'])) { 
  253. Ninja_Forms()->sub(esc_html($_REQUEST['export_single']))->export(); 
  254.  
  255. if ((isset ($_REQUEST['action']) && $_REQUEST['action'] == 'export') || (isset ($_REQUEST['action2']) && $_REQUEST['action2'] == 'export')) { 
  256.  
  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. * 
  310. * @param $sub 
  311. * @return mixed 
  312. */ 
  313. private function custom_columns_seq_num( $sub ) 
  314. return $sub->get_seq_num(); 
  315.  
  316. /** 
  317. * Custom Columns: Submission Date 
  318. * 
  319. * @param $sub 
  320. * @return mixed 
  321. */ 
  322. private function custom_columns_sub_date( $sub ) 
  323. return $sub->get_sub_date(); 
  324.  
  325. /** 
  326. * Custom Columns: Field 
  327. * 
  328. * @param $sub 
  329. * @param $column 
  330. * @return bool 
  331. */ 
  332. private function custom_columns_field( $sub, $column ) 
  333. if( FALSE === strpos( $column, 'field_' ) ) return FALSE; 
  334.  
  335. $field_id = str_replace( 'field_', '', $column ); 
  336.  
  337. return $sub->get_field_value( $field_id ); 
  338.  
  339. private function table_filter_by_form( $vars, $form_id ) 
  340. if ( ! isset ( $vars['meta_query'] ) ) { 
  341. $vars['meta_query'] = array( 
  342. array( 
  343. 'key' => '_form_id',  
  344. 'value' => $form_id,  
  345. 'compare' => '=',  
  346. ),  
  347. ); 
  348.  
  349. return $vars; 
  350.  
  351. private function table_filter_by_date( $vars ) 
  352. if( empty( $_GET[ 'begin_date' ] ) || empty( $_GET[ 'end_date' ] ) ) return $vars; 
  353.  
  354. $begin_date = $_GET[ 'begin_date' ]; 
  355. $end_date = $_GET[ 'end_date' ]; 
  356.  
  357. if( $begin_date > $end_date ) { 
  358. $temp_date = $begin_date; 
  359. $begin_date = $end_date; 
  360. $end_date = $temp_date; 
  361.  
  362. if ( ! isset ( $vars['date_query'] ) ) { 
  363.  
  364. $vars['date_query'] = array( 
  365. 'after' => $begin_date,  
  366. 'before' => $end_date 
  367. ); 
  368.  
  369. return $vars; 
  370.  
  371.  
  372.  
.