subs-cpt.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Submission CPT.
  4. * This class adds our submission CPT and handles displaying submissions in the wp-admin.
  5. *
  6. * @package Ninja Forms
  7. * @subpackage Classes/Submissions
  8. * @copyright Copyright (c) 2014, WPNINJAS
  9. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  10. * @since 2.7
  11. */
  12. class NF_Subs_CPT {
  13. var $form_id;
  14. var $screen_options;
  15. var $filename;
  16. /**
  17. * Get things started
  18. *
  19. * @access public
  20. * @since 2.7
  21. * @return void
  22. */
  23. function __construct() {
  24. // Register our submission custom post type.
  25. add_action( 'init', array( $this, 'register_cpt' ), 5 );
  26. // Populate our field settings var
  27. add_action( 'current_screen', array( $this, 'setup_fields' ) );
  28. // Filter our hidden columns by form ID.
  29. add_action( 'wp', array( $this, 'filter_hidden_columns' ) );
  30. // Add our submenu for the submissions page.
  31. add_action( 'admin_menu', array( $this, 'add_submenu' ), 10 );
  32. // Change our submission columns.
  33. add_filter( 'manage_nf_sub_posts_columns', array( $this, 'change_columns' ) );
  34. // Make our columns sortable.
  35. add_filter( 'manage_edit-nf_sub_sortable_columns', array( $this, 'sortable_columns' ) );
  36. // Actually do the sorting
  37. add_filter( 'request', array( $this, 'sort_columns' ) );
  38. // Add the appropriate data for our custom columns.
  39. add_action( 'manage_posts_custom_column', array( $this, 'custom_columns' ), 10, 2 );
  40. // Add our submission filters.
  41. add_action( 'restrict_manage_posts', array( $this, 'add_filters' ) );
  42. add_filter( 'parse_query', array( $this, 'table_filter' ) );
  43. add_filter( 'posts_clauses', array( $this, 'search' ), 20 );
  44. add_action( 'admin_footer', array( $this, 'jquery_remove_counts' ) );
  45. // Filter our post counts
  46. add_filter( 'wp_count_posts', array( $this, 'count_posts' ), 10, 3 );
  47. // Filter our bulk actions
  48. add_filter( 'bulk_actions-edit-nf_sub', array( $this, 'remove_bulk_edit' ) );
  49. add_action( 'admin_footer-edit.php', array( $this, 'bulk_admin_footer' ) );
  50. // Filter our bulk updated/trashed messages
  51. add_filter( 'bulk_post_updated_messages', array( $this, 'updated_messages_filter' ), 10, 2 );
  52. // Filter singular updated/trashed messages
  53. add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
  54. // Add our metabox for editing field values
  55. add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
  56. // Save our metabox values
  57. add_action( 'save_post', array( $this, 'save_sub' ), 10, 2 );
  58. // Save our hidden columns by form id.
  59. add_action( 'wp_ajax_nf_hide_columns', array( $this, 'hide_columns' ) );
  60. // Load any custom screen options
  61. add_filter( 'screen_settings', array( $this, 'output_screen_options' ), 10, 2 );
  62. // Listen for our exports button.
  63. add_action( 'load-edit.php', array( $this, 'export_listen' ) );
  64. // Filter our submission capabilities
  65. add_filter( 'user_has_cap', array( $this, 'cap_filter' ), 10, 3 );
  66. }
  67. /**
  68. * Register our submission CPT
  69. *
  70. * @access public
  71. * @since 2.7
  72. * @return void
  73. */
  74. public function register_cpt() {
  75. if ( ! isset ( $_REQUEST['form_id'] ) || empty( $_REQUEST['form_id'] ) ) {
  76. $not_found = __( 'Please select a form to view submissions', 'ninja-forms' );
  77. } else {
  78. $not_found = __( 'No Submissions Found', 'ninja-forms' );
  79. }
  80. $name = _x( 'Submissions', 'post type general name', 'ninja-forms' );
  81. if ( ! empty ( $_REQUEST['form_id'] ) ) {
  82. $form_title = Ninja_Forms()->form( absint( $_REQUEST['form_id'] ) )->get_setting( 'form_title' );
  83. $name =$name . ' - ' . $form_title;
  84. }
  85. $labels = array(
  86. 'name' => $name,
  87. 'singular_name' => _x( 'Submission', 'post type singular name', 'ninja-forms' ),
  88. 'add_new' => _x( 'Add New', 'nf_sub' ),
  89. 'add_new_item' => __( 'Add New Submission', 'ninja-forms' ),
  90. 'edit_item' => __( 'Edit Submission', 'ninja-forms' ),
  91. 'new_item' => __( 'New Submission', 'ninja-forms' ),
  92. 'view_item' => __( 'View Submission', 'ninja-forms' ),
  93. 'search_items' => __( 'Search Submissions', 'ninja-forms' ),
  94. 'not_found' => $not_found,
  95. 'not_found_in_trash' => __( 'No Submissions Found In The Trash', 'ninja-forms' ),
  96. 'parent_item_colon' => ''
  97. );
  98. $args = array(
  99. 'labels' => $labels,
  100. 'public' => false,
  101. 'publicly_queryable' => false,
  102. 'show_ui' => true,
  103. '_builtin' => false, // It's a custom post type, not built in!
  104. 'query_var' => true,
  105. 'has_archive' => false,
  106. 'show_in_menu' => false,
  107. 'hierarchical' => false,
  108. 'menu_events' => null,
  109. 'rewrite' => array( 'slug' => 'nf_sub' ), // Permalinks format
  110. 'supports' => array( 'custom-fields' ),
  111. 'capability_type' => 'nf_sub',
  112. 'capabilities' => array(
  113. 'publish_posts' => 'nf_sub',
  114. 'edit_posts' => 'nf_sub',
  115. 'edit_others_posts' => 'nf_sub',
  116. 'delete_posts' => 'nf_sub',
  117. 'delete_others_posts' => 'nf_sub',
  118. 'read_private_posts' => 'nf_sub',
  119. 'edit_post' => 'nf_sub',
  120. 'delete_post' => 'nf_sub',
  121. 'read_post' => 'nf_sub',
  122. ),
  123. );
  124. register_post_type( 'nf_sub',$args );
  125. }
  126. /**
  127. * Populate our fields var with all the fields. This keeps us from needing to ping the database later.
  128. *
  129. * @access public
  130. * @since 2.7
  131. */
  132. public function setup_fields() {
  133. global $pagenow, $typenow;
  134. // Bail if we aren't on the edit.php page, we aren't editing our custom post type, or we don't have a form_id set.
  135. if ( ( $pagenow != 'edit.php' && $pagenow != 'post.php' ) || $typenow != 'nf_sub' )
  136. return false;
  137. if ( isset ( $_REQUEST['form_id'] ) ) {
  138. $form_id = absint( $_REQUEST['form_id'] );
  139. } else if ( isset ( $_REQUEST['post'] ) ) {
  140. $form_id = Ninja_Forms()->sub( absint( $_REQUEST['post'] ) )->form_id;
  141. } else {
  142. $form_id = '';
  143. }
  144. $this->form_id = $form_id;
  145. Ninja_Forms()->form( $form_id );
  146. }
  147. /**
  148. * Add our submissions submenu
  149. *
  150. * @access public
  151. * @since 2.7
  152. * @return void
  153. */
  154. public function add_submenu() {
  155. // Add our submissions submenu
  156. $sub_page = add_submenu_page( 'ninja-forms', __( 'Submissions', 'ninja-forms' ), __( 'Submissions', 'ninja-forms' ), apply_filters( 'ninja_forms_admin_submissions_capabilities', 'manage_options' ), 'edit.php?post_type=nf_sub' );
  157. // Enqueue our JS on the edit page.
  158. //add_action( 'load-' . $sub_page, array( $this, 'load_js' ) );
  159. add_action( 'admin_print_styles', array( $this, 'load_js' ) );
  160. add_action( 'admin_print_styles', array( $this, 'load_css' ) );
  161. // Remove the publish box from the submission editing page.
  162. remove_meta_box( 'submitdiv', 'nf_sub', 'side' );
  163. }
  164. /**
  165. * Enqueue our submissions JS file.
  166. *
  167. * @access public
  168. * @since 2.7
  169. * @return void
  170. */
  171. public function load_js() {
  172. global $pagenow, $typenow;
  173. // Bail if we aren't on the edit.php page or we aren't editing our custom post type.
  174. if ( ( $pagenow != 'edit.php' && $pagenow != 'post.php' ) || $typenow != 'nf_sub' )
  175. return false;
  176. $form_id = isset ( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : '';
  177. if ( defined( 'NINJA_FORMS_JS_DEBUG' ) && NINJA_FORMS_JS_DEBUG ) {
  178. $suffix = '';
  179. $src = 'dev';
  180. } else {
  181. $suffix = '.min';
  182. $src = 'min';
  183. }
  184. $suffix = '';
  185. $src = 'dev';
  186. $plugin_settings = nf_get_settings();
  187. $date_format = ninja_forms_date_to_datepicker( $plugin_settings['date_format'] );
  188. $datepicker_args = array();
  189. if ( !empty( $date_format ) ) {
  190. $datepicker_args['dateFormat'] = $date_format;
  191. }
  192. wp_enqueue_script( 'subs-cpt',
  193. NF_PLUGIN_URL . 'assets/js/' . $src .'/subs-cpt' . $suffix . '.js',
  194. array('jquery', 'jquery-ui-datepicker') );
  195. wp_localize_script( 'subs-cpt', 'nf_sub', array( 'form_id' => $form_id, 'datepicker_args' => apply_filters( 'ninja_forms_admin_submissions_datepicker_args', $datepicker_args ) ) );
  196. }
  197. /**
  198. * Enqueue our submissions CSS file.
  199. *
  200. * @access public
  201. * @since 2.7
  202. * @return void
  203. */
  204. public function load_css() {
  205. global $pagenow, $typenow;
  206. // Bail if we aren't on the edit.php page or the post.php page.
  207. if ( ( $pagenow != 'edit.php' && $pagenow != 'post.php' ) || $typenow != 'nf_sub' )
  208. return false;
  209. wp_enqueue_style( 'nf-sub', NF_PLUGIN_URL .'assets/css/cpt.css' );
  210. wp_enqueue_style( 'nf-jquery-ui-freshness', NF_PLUGIN_URL .'assets/css/jquery-ui-fresh.min.css' );
  211. }
  212. /**
  213. * Modify the columns of our submissions table.
  214. *
  215. * @access public
  216. * @since 2.7
  217. * @return array $cols
  218. */
  219. public function change_columns( $cols ) {
  220. // Compatibility with old field registration system. Can be removed when the new one is in place.
  221. global $ninja_forms_fields;
  222. // End Compatibility
  223. $cols = array(
  224. 'cb' => '<input type="checkbox" />',
  225. 'id' => __( '#', 'ninja-forms' ),
  226. );
  227. // Compatibility with old field registration system. Can be removed when the new one is in place.
  228. if ( isset ( $_GET['form_id'] ) && $_GET['form_id'] != '' ) {
  229. $form_id = absint( $_GET['form_id'] );
  230. if ( is_object( Ninja_Forms()->form( $this->form_id ) ) && is_array ( Ninja_Forms()->form( $this->form_id )->fields ) ) {
  231. foreach ( Ninja_Forms()->form( $this->form_id )->fields as $field ) {
  232. $field_id = $field['id'];
  233. $field_type = $field['type'];
  234. if ( isset ( $ninja_forms_fields[ $field_type ] ) ) {
  235. $reg_field = $ninja_forms_fields[ $field_type ];
  236. $process_field = $reg_field['process_field'];
  237. } else {
  238. $process_field = false;
  239. }
  240. if ( isset ( $field['data']['admin_label'] ) && ! empty ( $field['data']['admin_label'] ) ) {
  241. $label = $field['data']['admin_label'];
  242. } else if ( isset ( $field['data']['label'] ) ) {
  243. $label = $field['data']['label'];
  244. } else {
  245. $label = '';
  246. }
  247. if ( strlen( $label ) > 140 )
  248. $label = substr( $label, 0, 140 );
  249. if ( isset ( $field['data']['label'] ) && $process_field )
  250. $cols[ 'form_' . $form_id . '_field_' . $field_id ] = $label;
  251. }
  252. }
  253. } else {
  254. $form_id = '';
  255. }
  256. // End Compatibility
  257. // Add our date column
  258. $cols['sub_date'] = __( 'Date', 'ninja-forms' );
  259. return apply_filters( 'nf_sub_table_columns', $cols, $form_id );
  260. }
  261. /**
  262. * Make our columns sortable
  263. *
  264. * @access public
  265. * @since 2.7
  266. * @return array
  267. */
  268. public function sortable_columns() {
  269. // Get a list of all of our fields.
  270. $columns = get_column_headers( 'edit-nf_sub' );
  271. $tmp_array = array();
  272. foreach ( $columns as $slug => $c ) {
  273. if ( $slug != 'cb' ) {
  274. $tmp_array[ $slug ] = $slug;
  275. }
  276. }
  277. return $tmp_array;
  278. }
  279. /**
  280. * Actually sort our columns
  281. *
  282. * @access public
  283. * @since 2.7
  284. * @return array $vars
  285. */
  286. public function sort_columns( $vars ) {
  287. global $pagenow, $typenow;
  288. if( array_key_exists( 'orderby', $vars ) ) {
  289. if( strpos( $vars['orderby'], 'form_' ) !== false ) {
  290. $args = explode( '_', $vars['orderby'] );
  291. $field_id = $args[3];
  292. if ( isset ( Ninja_Forms()->form( $this->form_id )->fields[ $field_id ]['data']['num_sort'] ) && Ninja_Forms()->form( $this->form_id )->fields[ $field_id ]['data']['num_sort'] == 1 ) {
  293. $orderby = 'meta_value_num';
  294. } else {
  295. $orderby = 'meta_value';
  296. }
  297. $vars['orderby'] = $orderby;
  298. $vars['meta_key'] = '_field_' . $field_id;
  299. } else if ( $vars['orderby'] == 'id' ) {
  300. $vars['orderby'] = 'meta_value_num';
  301. $vars['meta_key'] = '_seq_num';
  302. }
  303. } else if( is_admin() && $typenow == 'nf_sub' && $pagenow == 'edit.php' ) {
  304. $vars['orderby'] = 'meta_value_num';
  305. $vars['meta_key'] = '_seq_num';
  306. $vars['order'] = 'DESC';
  307. }
  308. return $vars;
  309. }
  310. /**
  311. * Add our custom column data
  312. *
  313. * @access public
  314. * @since 2.7
  315. * @return void
  316. */
  317. public function custom_columns( $column, $sub_id ) {
  318. if ( isset ( $_GET['form_id'] ) ) {
  319. $form_id = $_GET['form_id'];
  320. if ( $column == 'id' ) {
  321. echo apply_filters( 'nf_sub_table_seq_num', Ninja_Forms()->sub( $sub_id )->get_seq_num(), $sub_id, $column );
  322. echo '<div class="locked-info"><span class="locked-avatar"></span> <span class="locked-text"></span></div>';
  323. if ( !isset ( $_GET['post_status'] ) || $_GET['post_status'] == 'all' ) {
  324. echo '<div class="row-actions custom-row-actions">';
  325. do_action( 'nf_sub_table_before_row_actions', $sub_id, $column );
  326. echo '<span class="edit"><a href="post.php?post=' . $sub_id . '&action=edit&ref=' . urlencode( esc_url( add_query_arg( array() ) ) ) . '" title="' . __( 'Edit this item', 'ninja-forms' ) . '">' . __( 'Edit', 'ninja-forms' ) . '</a> | </span>
  327. <span class="edit"><a href="' . esc_url( add_query_arg( array( 'export_single' => $sub_id ) ) ) . '" title="' . __( 'Export this item', 'ninja-forms' ) . '">' . __( 'Export', 'ninja-forms' ) . '</a> | </span>';
  328. $row_actions = apply_filters( 'nf_sub_table_row_actions', array(), $sub_id, $form_id );
  329. if ( ! empty( $row_actions ) ) {
  330. echo implode(" | ", $row_actions);
  331. echo '| ';
  332. }
  333. echo '<span class="trash"><a class="submitdelete" title="' . __( 'Move this item to the Trash', 'ninja-forms' ) . '" href="' . get_delete_post_link( $sub_id ) . '">' . __( 'Trash', 'ninja-forms' ) . '</a> </span>';
  334. do_action( 'nf_sub_table_after_row_actions', $sub_id, $column );
  335. echo '</div>';
  336. } else {
  337. echo '<div class="row-actions custom-row-actions">';
  338. do_action( 'nf_sub_table_before_row_actions_trash', $sub_id, $column );
  339. echo '<span class="untrash"><a title="' . esc_attr( __( 'Restore this item from the Trash', 'ninja-forms' ) ) . '" href="' . wp_nonce_url( sprintf( get_edit_post_link( $sub_id ) . '&amp;action=untrash', $sub_id ) , 'untrash-post_' . $sub_id ) . '">' . __( 'Restore', 'ninja-forms' ) . '</a> | </span>
  340. <span class="delete"><a class="submitdelete" title="' . esc_attr( __( 'Delete this item permanently', 'ninja-forms' ) ) . '" href="' . get_delete_post_link( $sub_id, '', true ) . '">' . __( 'Delete Permanently', 'ninja-forms' ) . '</a></span>';
  341. do_action( 'nf_sub_table_after_row_actions_trash', $sub_id, $column );
  342. echo '</div>';
  343. }
  344. } else if ( $column == 'sub_date' ) {
  345. $post = get_post( $sub_id );
  346. if ( '0000-00-00 00:00:00' == $post->post_date ) {
  347. $t_time = $h_time = __( 'Unpublished', 'ninja-forms' );
  348. $time_diff = 0;
  349. } else {
  350. $t_time = get_the_time( 'Y/m/d g:i:s A' );
  351. $m_time = $post->post_date;
  352. $time = get_post_time( 'G', true, $post );
  353. $time_diff = time() - $time;
  354. if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS )
  355. $h_time = sprintf( __( '%s ago', 'ninja-forms' ), human_time_diff( $time ) );
  356. else
  357. $h_time = mysql2date( 'Y/m/d', $m_time );
  358. }
  359. $t_time = apply_filters( 'nf_sub_title_time', $t_time );
  360. $h_time = apply_filters( 'nf_sub_human_time', $h_time );
  361. /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
  362. echo '<abbr title="' . $t_time . '">' . $h_time . '</abbr>';
  363. echo '<br />';
  364. echo apply_filters( 'nf_sub_table_status', __( 'Submitted', 'ninja-forms' ), $sub_id );
  365. } else if ( strpos( $column, '_field_' ) !== false ) {
  366. global $ninja_forms_fields;
  367. $field_id = str_replace( 'form_' . $form_id . '_field_', '', $column );
  368. //if ( apply_filters( 'nf_add_sub_value', Ninja_Forms()->field( $field_id )->type->add_to_sub, $field_id ) ) {
  369. $field = Ninja_Forms()->form( $form_id )->fields[ $field_id ];
  370. $field_type = $field['type'];
  371. if ( isset ( $ninja_forms_fields[ $field_type ] ) ) {
  372. $reg_field = $ninja_forms_fields[ $field_type ];
  373. } else {
  374. $reg_field = array();
  375. }
  376. if ( isset ( $reg_field['sub_table_value'] ) ) {
  377. $edit_value_function = $reg_field['sub_table_value'];
  378. } else {
  379. $edit_value_function = 'nf_field_text_sub_table_value';
  380. }
  381. $user_value = Ninja_Forms()->sub( $sub_id )->get_field( $field_id );
  382. $args['field_id'] = $field_id;
  383. $args['user_value'] = ninja_forms_esc_html_deep( $user_value );
  384. $args['field'] = $field;
  385. call_user_func_array( $edit_value_function, $args );
  386. //}
  387. }
  388. }
  389. }
  390. /**
  391. * Add our submission filters
  392. *
  393. * @access public
  394. * @since 2.7
  395. * @return void
  396. */
  397. public function add_filters() {
  398. global $typenow;
  399. // Bail if we aren't in our submission custom post type.
  400. if ( $typenow != 'nf_sub' )
  401. return false;
  402. // Add our Form selection dropdown.
  403. // Get our list of forms
  404. $forms = Ninja_Forms()->forms()->get_all();
  405. $form_id = isset( $_GET['form_id'] ) ? $_GET['form_id'] : '';
  406. $begin_date = isset ( $_GET['begin_date'] ) ? $_GET['begin_date'] : '';
  407. $end_date = isset ( $_GET['end_date'] ) ? $_GET['end_date'] : '';
  408. // Add begin date and end date filter fields.
  409. $html = '<div style="float:left;">';
  410. $html .= '<span style="float:left;" class="spinner"></span>';
  411. $html .= '<select name="form_id" id="form_id" class="nf-form-jump">';
  412. $html .= '<option value="">- ' . __( 'Select a form', 'ninja-forms' ) . '</option>';
  413. if ( is_array( $forms ) ) {
  414. foreach ( $forms as $f_id ) {
  415. $form_title = Ninja_Forms()->form( $f_id )->get_setting( 'form_title' );
  416. $html .= '<option value="' . $f_id . '" ' . selected( $form_id, $f_id, false ) . '>' . $form_title . '</option>';
  417. }
  418. }
  419. $html .= '</select>';
  420. $html .= '<input name="begin_date" type="text" class="datepicker" placeholder="' . __( 'Begin Date', 'ninja-forms' ) . '" value="' . $begin_date . '" /> ';
  421. $html .= '<input name="end_date" type="text" class="datepicker" placeholder="' . __( 'End Date', 'ninja-forms' ) . '" value="' . $end_date . '" />';
  422. $html .= '</div>';
  423. echo $html;
  424. }
  425. /**
  426. * Filter our submission list by form_id
  427. *
  428. * @access public
  429. * @since 2.7
  430. * @return void
  431. */
  432. public function table_filter( $query ) {
  433. global $pagenow;
  434. if( $pagenow == 'edit.php' && is_admin() && ( isset ( $query->query['post_type'] ) && $query->query['post_type'] == 'nf_sub' ) && is_main_query() ) {
  435. $qv = &$query->query_vars;
  436. if( !empty( $_GET['form_id'] ) ) {
  437. $form_id = $_GET['form_id'];
  438. } else {
  439. $form_id = 0;
  440. }
  441. $plugin_settings = nf_get_settings();
  442. $date_format = $plugin_settings['date_format'];
  443. if ( !empty ( $_GET['begin_date'] ) ) {
  444. $begin_date = nf_get_begin_date( $_GET['begin_date'] )->format("Y-m-d G:i:s");
  445. } else {
  446. $begin_date = '';
  447. }
  448. if ( !empty ( $_GET['end_date'] ) ) {
  449. $end_date = nf_get_end_date( $_GET['end_date'] )->format("Y-m-d G:i:s");
  450. } else {
  451. $end_date = '';
  452. }
  453. if ( $begin_date > $end_date ) {
  454. $begin_date = new DateTime( $begin_date );
  455. $end_date = new DateTime( $end_date );
  456. $end_date_temp = $begin_date;
  457. $begin_date_temp = $end_date;
  458. $begin_date = $begin_date_temp;
  459. $end_date = $end_date_temp;
  460. $_GET['begin_date'] = $begin_date->format('m/d/Y');
  461. $_GET['end_date'] = $end_date->format('m/d/Y');
  462. $begin_date = $begin_date->format("Y-m-d G:i:s");
  463. $end_date = $end_date->format("Y-m-d G:i:s");
  464. }
  465. if ( ! isset ( $qv['date_query'] ) ) {
  466. $qv['date_query'] = array(
  467. 'after' => $begin_date,
  468. 'before' => $end_date,
  469. );
  470. }
  471. if ( ! isset ( $qv['meta_query'] ) ) {
  472. $qv['meta_query'] = array(
  473. array(
  474. 'key' => '_form_id',
  475. 'value' => $form_id,
  476. 'compare' => '=',
  477. ),
  478. );
  479. }
  480. $qv = apply_filters( 'nf_subs_table_qv', $qv, $form_id );
  481. }
  482. }
  483. /**
  484. * Filter our search
  485. *
  486. * @access public
  487. * @since 2.7
  488. * @return void
  489. */
  490. public function search( $pieces ) {
  491. global $typenow;
  492. // filter to select search query
  493. if ( is_search() && is_admin() && $typenow == 'nf_sub' && isset ( $_GET['s'] ) ) {
  494. global $wpdb;
  495. $keywords = explode(' ', get_query_var('s'));
  496. $query = "";
  497. foreach ($keywords as $word) {
  498. $query .= " (mypm1.meta_value LIKE '%{$word}%') OR ";
  499. }
  500. if (!empty($query)) {
  501. // add to where clause
  502. // Escape place holders for the where clause.
  503. $pieces[ 'where' ] = $wpdb->remove_placeholder_escape( $pieces[ 'where' ] );
  504. $pieces[ 'where' ] = str_replace("((({$wpdb->posts}.post_title LIKE '%", "({$query}(({$wpdb->posts}.post_title LIKE '%", $pieces[ 'where' ]);
  505. $pieces[ 'join' ] = $pieces[ 'join' ] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
  506. }
  507. }
  508. return ( $pieces );
  509. }
  510. /**
  511. * Filter our bulk updated/trashed messages so that it uses "submission" rather than "post"
  512. *
  513. * @access public
  514. * @since 2.7
  515. * @return array $bulk_messages
  516. */
  517. public function updated_messages_filter( $bulk_messages, $bulk_counts ) {
  518. $bulk_messages['nf_sub'] = array(
  519. 'updated' => _n( '%s submission updated.', '%s submissions updated.', $bulk_counts['updated'], 'ninja-forms' ),
  520. 'locked' => _n( '%s submission not updated, somebody is editing it.', '%s submissions not updated, somebody is editing them.', $bulk_counts['locked'], 'ninja-forms' ),
  521. 'deleted' => _n( '%s submission permanently deleted.', '%s submissions permanently deleted.', $bulk_counts['deleted'], 'ninja-forms' ),
  522. 'trashed' => _n( '%s submission moved to the Trash.', '%s submissions moved to the Trash.', $bulk_counts['trashed'], 'ninja-forms' ),
  523. 'untrashed' => _n( '%s submission restored from the Trash.', '%s submissions restored from the Trash.', $bulk_counts['untrashed'], 'ninja-forms' ),
  524. );
  525. return $bulk_messages;
  526. }
  527. /**
  528. * Filter our updated/trashed post messages
  529. *
  530. * @access public
  531. * @since 2.7
  532. * @return array $messages
  533. */
  534. function post_updated_messages( $messages ) {
  535. global $post, $post_ID;
  536. $post_type = 'nf_sub';
  537. $obj = get_post_type_object( $post_type );
  538. $singular = $obj->labels->singular_name;
  539. $messages[$post_type] = array(
  540. 0 => '', // Unused. Messages start at index 1.
  541. 1 => sprintf( __( '%s updated.', 'ninja-forms' ), $singular ),
  542. 2 => __( 'Custom field updated.', 'ninja-forms' ),
  543. 3 => __( 'Custom field deleted.', 'ninja-forms' ),
  544. 4 => sprintf( __( '%s updated.', 'ninja-forms' ), $singular ),
  545. /* translators: %s: date and time of the revision */
  546. 5 => isset($_GET['revision']) ? sprintf( __( '%1$s restored to revision from %2$s.', 'ninja-forms' ), $singular, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  547. 6 => sprintf( __( '%s published.', 'ninja-forms' ), $singular ),
  548. 7 => sprintf( __( '%s saved.', 'ninja-forms' ), $singular ),
  549. 8 => sprintf( __( '%1$s submitted. <a href="%2$s" target="_blank">Preview %3$s</a>', 'ninja-forms' ), $singular, esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ), $singular ),
  550. 9 => sprintf( __( '%1$s scheduled for: <strong>%2$s</strong>. <a href="%3$s" target="_blank">Preview %4$s</a>', 'ninja-forms' ), $singular, date_i18n( get_option( 'data_format' ) . ' ' . get_option( 'time_format' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID ) ), $singular ),
  551. 10 => sprintf( __( '%1$s draft updated. <a href="%2$s" target="_blank">Preview %3$s</a>', 'ninja-forms' ), $singular, esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ), $singular ),
  552. );
  553. return $messages;
  554. }
  555. /**
  556. * Remove the 'edit' bulk action
  557. *
  558. * @access public
  559. * @since 2.7
  560. * @return array $actions
  561. */
  562. public function remove_bulk_edit( $actions ) {
  563. unset( $actions['edit'] );
  564. return $actions;
  565. }
  566. /**
  567. * Add our "export" bulk action
  568. *
  569. * @access public
  570. * @since 2.7
  571. * @return void
  572. */
  573. public function bulk_admin_footer() {
  574. global $post_type;
  575. if ( ! is_admin() )
  576. return false;
  577. if( $post_type == 'nf_sub' && isset ( $_REQUEST['post_status'] ) && $_REQUEST['post_status'] == 'all' ) {
  578. ?>
  579. <script type="text/javascript">
  580. jQuery(document).ready(function() {
  581. jQuery('<option>').val('export').text('<?php _e('Export', 'ninja-forms')?>').appendTo("select[name='action']");
  582. jQuery('<option>').val('export').text('<?php _e('Export', 'ninja-forms')?>').appendTo("select[name='action2']");
  583. <?php
  584. if ( ( isset ( $_POST['action'] ) && $_POST['action'] == 'export' ) || ( isset ( $_POST['action2'] ) && $_POST['action2'] == 'export' ) ) {
  585. ?>
  586. setInterval(function(){
  587. jQuery( "select[name='action'" ).val( '-1' );
  588. jQuery( "select[name='action2'" ).val( '-1' );
  589. jQuery( '#posts-filter' ).submit();
  590. },5000);
  591. <?php
  592. }
  593. if ( isset ( $_REQUEST['form_id'] ) && ! empty ( $_REQUEST['form_id'] ) ) {
  594. $redirect = urlencode( remove_query_arg( array( 'download_all', 'download_file' ) ) );
  595. $url = admin_url( 'admin.php?page=nf-processing&action=download_all_subs&form_id=' . absint( $_REQUEST['form_id'] ) . '&redirect=' . $redirect );
  596. $url = esc_url( $url );
  597. ?>
  598. var button = '<a href="<?php echo $url; ?>" class="button-secondary nf-download-all"><?php echo __( 'Download All Submissions', 'ninja-forms' ); ?></a>';
  599. jQuery( '#doaction2' ).after( button );
  600. <?php
  601. }
  602. if ( isset ( $_REQUEST['download_all'] ) && $_REQUEST['download_all'] != '' ) {
  603. $redirect = esc_url_raw( add_query_arg( array( 'download_file' => esc_html( $_REQUEST['download_all'] ) ) ) );
  604. $redirect = remove_query_arg( array( 'download_all' ), $redirect );
  605. ?>
  606. document.location.href = "<?php echo $redirect; ?>";
  607. <?php
  608. }
  609. ?>
  610. });
  611. </script>
  612. <?php
  613. }
  614. }
  615. /**
  616. * jQuery that hides some of our post-related page items.
  617. * Also adds the active class to All and Trash links, and changes those
  618. * links to match the current filter.
  619. *
  620. * @access public
  621. * @since 2.7
  622. * @return void
  623. */
  624. public function jquery_remove_counts() {
  625. global $typenow, $pagenow;
  626. if ( $typenow == 'nf_sub' && $pagenow == 'edit.php' ) {
  627. // Remove our transient
  628. delete_transient( 'nf_sub_edit_ref' );
  629. if ( ! isset ( $_GET['post_status'] ) || $_GET['post_status'] == 'all' ) {
  630. $active = 'all';
  631. } else if ( $_GET['post_status'] == 'trash' ) {
  632. $active = 'trash';
  633. }
  634. $all_url = esc_url_raw( add_query_arg( array( 'post_status' => 'all' ) ) );
  635. $all_url = remove_query_arg( 's', $all_url );
  636. $trash_url = esc_url_raw( add_query_arg( array( 'post_status' => 'trash' ) ) );
  637. $trash_url = remove_query_arg( 's', $trash_url );
  638. if ( isset ( $_GET['form_id'] ) ) {
  639. $trashed_sub_count = nf_get_sub_count( $_GET['form_id'], 'trash' );
  640. } else {
  641. $trashed_sub_count = 0;
  642. }
  643. ?>
  644. <script type="text/javascript">
  645. jQuery(function(){
  646. jQuery( "li.all" ).find( "a" ).attr( "href", "<?php echo $all_url; ?>" );
  647. jQuery( "li.<?php echo $active; ?>" ).addClass( "current" );
  648. jQuery( "li.<?php echo $active; ?>" ).find( "a" ).addClass( "current" );
  649. jQuery( "li.trash" ).find( "a" ).attr( "href", "<?php echo $trash_url; ?>" );
  650. jQuery( ".view-switch" ).remove();
  651. <?php
  652. if ( $trashed_sub_count == 0 ) {
  653. ?>
  654. var text = jQuery( "li.all" ).prop( "innerHTML" );
  655. text = text.replace( " |", "" );
  656. jQuery( "li.all" ).prop( "innerHTML", text );
  657. <?php
  658. }
  659. ?>
  660. });
  661. </script>
  662. <style>
  663. .add-new-h2 {
  664. display:none;
  665. }
  666. li.publish {
  667. display:none;
  668. }
  669. select[name=m] {
  670. display:none;
  671. }
  672. </style>
  673. <?php
  674. } else if ( $typenow == 'nf_sub' && $pagenow == 'post.php' ) {
  675. if ( isset ( $_REQUEST['ref'] ) ) {
  676. $back_url = esc_url_raw( $_REQUEST['ref'] );
  677. } else {
  678. $back_url = get_transient( 'nf_sub_edit_ref' );
  679. }
  680. if ( $back_url ) {
  681. $back_url = urldecode( $back_url );
  682. } else {
  683. $back_url = '';
  684. }
  685. ?>
  686. <script type="text/javascript">
  687. jQuery(function(){
  688. var html = '<a href="<?php echo $back_url; ?>" class="back button-secondary"><?php _e( 'Back to list', 'ninja-forms' ); ?></a>';
  689. jQuery( 'div.wrap' ).children( 'h2:first' ).append( html );
  690. jQuery( 'li#toplevel_page_ninja-forms' ).children( 'a' ).removeClass( 'wp-not-current-submenu' );
  691. jQuery( 'li#toplevel_page_ninja-forms' ).removeClass( 'wp-not-current-submenu' );
  692. jQuery( 'li#toplevel_page_ninja-forms' ).addClass( 'wp-menu-open wp-has-current-submenu' );
  693. jQuery( 'li#toplevel_page_ninja-forms' ).children( 'a' ).addClass( 'wp-menu-open wp-has-current-submenu' );
  694. });
  695. </script>
  696. <style>
  697. .add-new-h2 {
  698. display:none;
  699. }
  700. </style>
  701. <?php
  702. }
  703. }
  704. /**
  705. * Filter our post counts for the submission listing page
  706. *
  707. * @access public
  708. * @since 2.7
  709. * @return int $count
  710. */
  711. public function count_posts( $count, $post_type, $perm ) {
  712. // Bail if we aren't working with our custom post type.
  713. if ( $post_type != 'nf_sub' )
  714. return $count;
  715. if ( isset ( $_GET['form_id'] ) ) {
  716. $sub_count = nf_get_sub_count( $_GET['form_id'] );
  717. $trashed_sub_count = nf_get_sub_count( $_GET['form_id'], 'trash' );
  718. $count->publish = $sub_count;
  719. $count->trash = $trashed_sub_count;
  720. } else {
  721. $count->publish = 0;
  722. $count->trash = 0;
  723. }
  724. return $count;
  725. }
  726. /**
  727. * Add our field editing metabox to the CPT editing page.
  728. *
  729. * @access public
  730. * @since 2.7
  731. * @return void
  732. */
  733. public function add_metaboxes() {
  734. // Remove the 'custom fields' metabox from our CPT edit page
  735. remove_meta_box( 'postcustom', 'nf_sub', 'normal' );
  736. // Remove the 'slug' metabox from our CPT edit page.
  737. remove_meta_box( 'slugdiv', 'nf_sub', 'normal' );
  738. // Add our field editing metabox.
  739. add_meta_box( 'nf_fields', __( 'User Submitted Values', 'ninja-forms' ), array( $this, 'edit_sub_metabox' ), 'nf_sub', 'normal', 'default');
  740. // Add our save field values metabox
  741. add_meta_box( 'nf_fields_save', __( 'Submission Stats', 'ninja-forms' ), array( $this, 'save_sub_metabox' ), 'nf_sub', 'side', 'default');
  742. }
  743. /**
  744. * Output our field editing metabox to the CPT editing page.
  745. *
  746. * @access public
  747. * @since 2.7
  748. * @return void
  749. */
  750. public function edit_sub_metabox( $post ) {
  751. global $ninja_forms_fields;
  752. // Get all the post meta
  753. $form_id = Ninja_Forms()->sub( $post->ID )->form_id;
  754. $fields = Ninja_Forms()->form( $this->form_id )->fields;
  755. if ( isset ( $_REQUEST['ref'] ) ) {
  756. $ref = esc_url_raw( $_REQUEST['ref'] );
  757. } else if ( get_transient( 'nf_sub_edit_ref' ) ) {
  758. $ref = get_transient( 'nf_sub_edit_ref' );
  759. } else {
  760. $ref = '';
  761. }
  762. ?>
  763. <input type="hidden" name="ref" value="<?php echo $ref; ?>" />
  764. <div id="postcustomstuff">
  765. <table id="list-table">
  766. <thead>
  767. <tr>
  768. <th class="left"><?php _e( 'Field', 'ninja-forms' ); ?></th>
  769. <th><?php _e( 'Value', 'ninja-forms' ); ?></th>
  770. </tr>
  771. </thead>
  772. <tbody id="the-list">
  773. <?php
  774. // Loop through our post meta and keep our field values
  775. foreach ( $fields as $field_id => $field ) {
  776. $user_value = Ninja_Forms()->sub( $post->ID )->get_field( $field_id );
  777. $field_type = $field['type'];
  778. if ( isset ( $field['data']['admin_label'] ) && $field['data']['admin_label'] != '' ) {
  779. $label = $field['data']['admin_label'];
  780. } else if ( isset ( $field['data']['label'] ) ) {
  781. $label = $field['data']['label'];
  782. } else {
  783. $label = '';
  784. }
  785. if ( isset ( $ninja_forms_fields[ $field_type ] ) ) {
  786. $reg_field = $ninja_forms_fields[ $field_type ];
  787. $process_field = $reg_field['process_field'];
  788. } else {
  789. $process_field = false;
  790. }
  791. if ( isset ( Ninja_Forms()->form( $this->form_id )->fields[ $field_id ] ) && $process_field ) {
  792. ?>
  793. <tr>
  794. <td class="left"><?php echo $label; ?></td>
  795. <td>
  796. <div class="nf-sub-edit-value type-<?php echo $field_type; ?>">
  797. <?php
  798. if ( isset ( $reg_field['edit_sub_value'] ) ) {
  799. $edit_value_function = $reg_field['edit_sub_value'];
  800. } else {
  801. $edit_value_function = 'nf_field_text_edit_sub_value';
  802. }
  803. $args['field_id'] = $field_id;
  804. $args['user_value'] = nf_wp_kses_post_deep( $user_value );
  805. $args['field'] = $field;
  806. $args['sub_id'] = $post->ID;
  807. call_user_func_array( $edit_value_function, $args );
  808. ?>
  809. </div>
  810. </td>
  811. </tr>
  812. <?php
  813. }
  814. }
  815. ?>
  816. </tbody>
  817. </table>
  818. </div>
  819. <?php
  820. }
  821. /**
  822. * Output our field editing metabox to the CPT editing page.
  823. *
  824. * @access public
  825. * @since 2.7
  826. * @return void
  827. */
  828. public function save_sub_metabox( $post ) {
  829. $date_submitted = apply_filters( 'nf_edit_sub_date_submitted', date( 'M j, Y @ h:i', strtotime( $post->post_date ) ), $post->ID );
  830. $date_modified = apply_filters( 'nf_edit_sub_date_modified', date( 'M j, Y @ h:i', strtotime( $post->post_modified ) ), $post->ID );
  831. if ( $post->post_author != 0 ) {
  832. $user_data = get_userdata( $post->post_author );
  833. $first_name = $user_data->first_name;
  834. $last_name = $user_data->last_name;
  835. if ( $first_name != '' && $last_name != '' ) {
  836. $name = $first_name . ' ' . $last_name;
  837. } else if ( $user_data->display_name != '' ) {
  838. $name = $user_data->display_name;
  839. } else {
  840. $name = $user_data->user_login;
  841. }
  842. $name = apply_filters( 'nf_edit_sub_username', $name, $post->post_author );
  843. }
  844. $form_id = Ninja_Forms()->sub( $post->ID )->form_id;
  845. $form_title = Ninja_Forms()->form( $form_id )->get_setting( 'form_title' );
  846. ?>
  847. <input type="hidden" name="nf_edit_sub" value="1">
  848. <div class="submitbox" id="submitpost">
  849. <div id="minor-publishing">
  850. <div id="misc-publishing-actions">
  851. <div class="misc-pub-section misc-pub-post-status">
  852. <label for="post_status"><?php _e( '#', 'ninja-forms' ); ?>:</label>
  853. <span id="sub-seq-num-display"><?php echo Ninja_Forms()->sub( $post->ID )->get_seq_num(); ?></span>
  854. </div>
  855. <div class="misc-pub-section misc-pub-post-status">
  856. <label for="post_status"><?php _e( 'Status', 'ninja-forms' ); ?>:</label>
  857. <span id="sub-status-display"><?php echo apply_filters( 'nf_sub_edit_status', __( 'Submitted', 'ninja-forms' ), $post->ID ); ?></span>
  858. <?php do_action( 'nf_sub_edit_after_status', $post ); ?>
  859. </div>
  860. <div class="misc-pub-section misc-pub-post-status">
  861. <label for="post_status"><?php _e( 'Form', 'ninja-forms' ); ?>:</label>
  862. <span id="sub-form-title-display"><?php echo $form_title; ?></span>
  863. </div>
  864. <div class="misc-pub-section curtime misc-pub-curtime">
  865. <span id="timestamp">
  866. <?php _e( 'Submitted on', 'ninja-forms' ); ?>: <b><?php echo $date_submitted; ?></b>
  867. </span>
  868. <?php do_action( 'nf_sub_edit_date_submitted', $post ); ?>
  869. </div>
  870. <div class="misc-pub-section curtime misc-pub-curtime">
  871. <span id="timestamp">
  872. <?php _e( 'Modified on', 'ninja-forms', $post ); ?>: <b><?php echo $date_modified; ?></b>
  873. </span>
  874. <?php do_action( 'nf_sub_edit_date_modified', $post ); ?>
  875. </div>
  876. <?php
  877. if ( $post->post_author != 0 ) {
  878. ?>
  879. <div class="misc-pub-section misc-pub-visibility" id="visibility">
  880. <?php _e( 'Submitted By', 'ninja-forms' ); ?>: <span id="post-visibility-display"><?php echo $name; ?></span>
  881. </div>
  882. <?php
  883. }
  884. ?>
  885. </div>
  886. </div>
  887. <div id="major-publishing-actions">
  888. <div id="delete-action">
  889. <!-- <a class="submitdelete deletion" href="<?php echo get_delete_post_link( $post->ID ); ?>">Move to Trash</a>--></div>
  890. <div id="publishing-action">
  891. <span class="spinner"></span>
  892. <input name="original_publish" type="hidden" id="original_publish" value="<?php _e( 'Update', 'ninja-forms' ); ?>">
  893. <input name="save" type="submit" class="button button-primary button-large" id="publish" accesskey="p" value="<?php _e( 'Update', 'ninja-forms' ); ?>">
  894. </div>
  895. <div class="clear"></div>
  896. </div>
  897. </div>
  898. <?php
  899. }
  900. /**
  901. * Save our submission user values
  902. *
  903. * @access public
  904. * @since 2.7
  905. * @return void
  906. */
  907. public function save_sub( $sub_id, $post ) {
  908. global $pagenow;
  909. if ( ! isset ( $_POST['nf_edit_sub'] ) || $_POST['nf_edit_sub'] != 1 )
  910. return $sub_id;
  911. // verify if this is an auto save routine.
  912. // If it is our form has not been submitted, so we dont want to do anything
  913. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  914. return $sub_id;
  915. if ( $pagenow != 'post.php' )
  916. return $sub_id;
  917. if ( $post->post_type != 'nf_sub' )
  918. return $sub_id;
  919. /* Get the post type object. */
  920. $post_type = get_post_type_object( $post->post_type );
  921. /* Check if the current user has permission to edit the post. */
  922. if ( !current_user_can( $post_type->cap->edit_post, $sub_id ) )
  923. return $sub_id;
  924. foreach ( $_POST['fields'] as $field_id => $user_value ) {
  925. $user_value = nf_wp_kses_post_deep( apply_filters( 'nf_edit_sub_user_value', $user_value, $field_id, $sub_id ) );
  926. Ninja_Forms()->sub( $sub_id )->update_field( $field_id, $user_value );
  927. }
  928. set_transient( 'nf_sub_edit_ref', esc_url_raw( $_REQUEST['ref'] ) );
  929. }
  930. /**
  931. * Filter our hidden columns so that they are handled on a per-form basis.
  932. *
  933. * @access public
  934. * @since 2.7
  935. * @return void
  936. */
  937. public function filter_hidden_columns() {
  938. global $pagenow;
  939. // Bail if we aren't on the edit.php page, we aren't editing our custom post type, or we don't have a form_id set.
  940. if ( $pagenow != 'edit.php' || ! isset ( $_REQUEST['post_type'] ) || $_REQUEST['post_type'] != 'nf_sub' || ! isset ( $_REQUEST['form_id'] ) )
  941. return false;
  942. // Grab our current user.
  943. $user = wp_get_current_user();
  944. // Grab our form id.
  945. $form_id = absint( $_REQUEST['form_id'] );
  946. // Get the columns that should be hidden for this form ID.
  947. $hidden_columns = get_user_option( 'manageedit-nf_subcolumnshidden-form-' . $form_id );
  948. if ( $hidden_columns === false ) {
  949. // If we don't have custom hidden columns set up for this form, then only show the first five columns.
  950. // Get our column headers
  951. $columns = get_column_headers( 'edit-nf_sub' );
  952. $hidden_columns = array();
  953. $x = 0;
  954. foreach ( $columns as $slug => $name ) {
  955. if ( $x > 5 ) {
  956. if ( $slug != 'sub_date' )
  957. $hidden_columns[] = $slug;
  958. }
  959. $x++;
  960. }
  961. }
  962. update_user_option( $user->ID, 'manageedit-nf_subcolumnshidden', $hidden_columns, true );
  963. }
  964. /**
  965. * Save our hidden columns per form id.
  966. *
  967. * @access public
  968. * @since 2.7
  969. * @return void
  970. */
  971. public function hide_columns() {
  972. // Grab our current user.
  973. $user = wp_get_current_user();
  974. // Grab our form id.
  975. $form_id = absint( $_REQUEST['form_id'] );
  976. $hidden = isset( $_POST['hidden'] ) ? explode( ',', esc_html( $_POST['hidden'] ) ) : array();
  977. $hidden = array_filter( $hidden );
  978. update_user_option( $user->ID, 'manageedit-nf_subcolumnshidden-form-' . $form_id, $hidden, true );
  979. die();
  980. }
  981. /**
  982. * Add custom screen options
  983. *
  984. * @access public
  985. * @since 2.7
  986. * @return void
  987. */
  988. public function output_screen_options( $status, $args ) {
  989. if ( $args->base == 'edit' && $args->post_type == 'nf_sub' ) {
  990. $status .= '<span id="nf-subs-screen-options">' . $this->screen_options . '</span>';
  991. }
  992. return $status;
  993. }
  994. /**
  995. * Listen for exporting subs
  996. *
  997. * @access public
  998. * @since 2.7.3
  999. * @return void
  1000. */
  1001. public function export_listen() {
  1002. // Bail if we aren't in the admin
  1003. if ( ! is_admin() )
  1004. return false;
  1005. if ( ! isset ( $_REQUEST['form_id'] ) || empty ( $_REQUEST['form_id'] ) )
  1006. return false;
  1007. if ( isset ( $_REQUEST['export_single'] ) && ! empty( $_REQUEST['export_single'] ) )
  1008. Ninja_Forms()->sub( esc_html( $_REQUEST['export_single'] ) )->export();
  1009. if ( ( isset ( $_REQUEST['action'] ) && $_REQUEST['action'] == 'export' ) || ( isset ( $_REQUEST['action2'] ) && $_REQUEST['action2'] == 'export' ) ) {
  1010. Ninja_Forms()->subs()->export( ninja_forms_esc_html_deep( $_REQUEST['post'] ) );
  1011. }
  1012. if ( isset ( $_REQUEST['download_file'] ) && ! empty( $_REQUEST['download_file'] ) ) {
  1013. // Open our download all file
  1014. $filename = esc_html( $_REQUEST['download_file'] );
  1015. $upload_dir = wp_upload_dir();
  1016. $file_path = trailingslashit( $upload_dir['path'] ) . $filename . '.csv';
  1017. if ( file_exists( $file_path ) ) {
  1018. $myfile = file_get_contents ( $file_path );
  1019. } else {
  1020. $redirect = esc_url_raw( remove_query_arg( array( 'download_file', 'download_all' ) ) );
  1021. wp_redirect( $redirect );
  1022. die();
  1023. }
  1024. unlink( $file_path );
  1025. $form_name = Ninja_Forms()->form( absint( $_REQUEST['form_id'] ) )->get_setting( 'form_title' );
  1026. $form_name = sanitize_title( $form_name );
  1027. $today = date( 'Y-m-d', current_time( 'timestamp' ) );
  1028. $filename = apply_filters( 'nf_download_all_filename', $form_name . '-all-subs-' . $today );
  1029. header( 'Content-type: application/csv');
  1030. header( 'Content-Disposition: attachment; filename="'.$filename .'.csv"' );
  1031. header( 'Pragma: no-cache');
  1032. header( 'Expires: 0' );
  1033. echo $myfile;
  1034. die();
  1035. }
  1036. }
  1037. /**
  1038. * Filter user capabilities
  1039. *
  1040. * @access public
  1041. * @since 2.7.7
  1042. * @return void
  1043. */
  1044. public function cap_filter( $allcaps, $cap, $args ) {
  1045. $sub_cap = apply_filters( 'ninja_forms_admin_submissions_capabilities', 'manage_options' );
  1046. if ( ! empty( $allcaps[ $sub_cap ] ) ) {
  1047. $allcaps['nf_sub'] = true;
  1048. }
  1049. return $allcaps;
  1050. }
  1051. }