AllFormsTable.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. if( ! class_exists( 'WP_List_Table' ) ){
  3. if( file_exists( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ) ) {
  4. require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
  5. } else {
  6. //TODO: Load local wp-list-table-class.php
  7. }
  8. }
  9. class NF_Admin_AllFormsTable extends WP_List_Table
  10. {
  11. /** Class constructor */
  12. public function __construct() {
  13. parent::__construct( array(
  14. 'singular' => __( 'Form', 'ninja-forms' ), //singular name of the listed records
  15. 'plural' => __( 'Forms', 'ninja-forms' ), //plural name of the listed records
  16. 'ajax' => false //should this table support ajax?
  17. ) );
  18. }
  19. public function no_items() {
  20. _e( 'No forms found.', 'ninja-forms' );
  21. }
  22. /**
  23. * Prepare the items for the table to process
  24. *
  25. * @return Void
  26. */
  27. public function prepare_items()
  28. {
  29. wp_enqueue_script( 'nf-all-forms', Ninja_Forms::$url . 'assets/js/all-forms.js' );
  30. wp_localize_script( 'nf-all-forms', 'nfi18n', array(
  31. 'confirm_delete' => __( 'Really Delete This Form? This will remove all fields and submission data. Recovery is not possible.', 'ninja-forms' ),
  32. ) );
  33. $columns = $this->get_columns();
  34. $hidden = $this->get_hidden_columns();
  35. $sortable = $this->get_sortable_columns();
  36. $data = $this->table_data();
  37. usort( $data, array( &$this, 'sort_data' ) );
  38. $perPage = 20;
  39. $currentPage = $this->get_pagenum();
  40. $totalItems = count($data);
  41. $this->set_pagination_args( array(
  42. 'total_items' => $totalItems,
  43. 'per_page' => $perPage
  44. ) );
  45. $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
  46. $this->_column_headers = array($columns, $hidden, $sortable);
  47. $this->items = $data;
  48. }
  49. /**
  50. * Override the parent columns method. Defines the columns to use in your listing table
  51. *
  52. * @return Array
  53. */
  54. public function get_columns()
  55. {
  56. $columns = array(
  57. 'cb' => '<input type="checkbox" />',
  58. 'title' => __( 'Form Title', 'ninja-forms' ),
  59. 'shortcode' => __( 'Shortcode', 'ninja-forms' ),
  60. 'date' => __( 'Created', 'ninja-forms' )
  61. );
  62. return $columns;
  63. }
  64. /**
  65. * Define which columns are hidden
  66. *
  67. * @return Array
  68. */
  69. public function get_hidden_columns()
  70. {
  71. return array();
  72. }
  73. /**
  74. * Define the sortable columns
  75. *
  76. * @return Array
  77. */
  78. public function get_sortable_columns()
  79. {
  80. return array(
  81. 'title' => array( __( 'title', 'ninja-forms' ), TRUE ),
  82. 'date' => array( __( 'date', 'ninja-forms' ), TRUE ),
  83. );
  84. }
  85. /**
  86. * Get the table data
  87. *
  88. * @return Array
  89. */
  90. private function table_data()
  91. {
  92. $data = array();
  93. $forms = Ninja_Forms()->form()->get_forms();
  94. foreach( $forms as $form ){
  95. $data[] = array(
  96. 'id' => $form->get_id(),
  97. 'title' => $form->get_setting( 'title' ),
  98. 'shortcode' => apply_filters ( 'ninja_forms_form_list_shortcode','[ninja_form id=' . $form->get_id() . ']', $form->get_id() ),
  99. 'date' => $form->get_setting( 'created_at' )
  100. );
  101. }
  102. return $data;
  103. }
  104. /**
  105. * Define what data to show on each column of the table
  106. *
  107. * @param Array $item Data
  108. * @param String $column_name - Current column name
  109. *
  110. * @return Mixed
  111. */
  112. public function column_default( $item, $column_name )
  113. {
  114. switch( $column_name ) {
  115. case 'title':
  116. case 'shortcode':
  117. case 'date':
  118. return $item[ $column_name ];
  119. default:
  120. return print_r( $item, true ) ;
  121. }
  122. }
  123. /**
  124. * Allows you to sort the data by the variables set in the $_GET
  125. *
  126. * @return Mixed
  127. */
  128. private function sort_data( $a, $b )
  129. {
  130. // Set defaults
  131. $orderby = 'id';
  132. $order = 'asc';
  133. // If orderby is set, use this as the sort column
  134. if(!empty($_GET['orderby']))
  135. {
  136. $orderby = $_GET['orderby'];
  137. }
  138. // If order is set use this as the order
  139. if(!empty($_GET['order']))
  140. {
  141. $order = $_GET['order'];
  142. }
  143. $result = strnatcmp( $a[$orderby], $b[$orderby] );
  144. if($order === 'asc')
  145. {
  146. return $result;
  147. }
  148. return -$result;
  149. }
  150. function column_cb( $item )
  151. {
  152. return sprintf(
  153. '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id']
  154. );
  155. }
  156. function column_title( $item )
  157. {
  158. $title = $item[ 'title' ];
  159. $edit_url = add_query_arg( 'form_id', $item[ 'id' ], admin_url( 'admin.php?page=ninja-forms') );
  160. $delete_url = add_query_arg( array( 'action' => 'delete', 'id' => $item[ 'id' ], '_wpnonce' => wp_create_nonce( 'nf_delete_form' )));
  161. $duplicate_url = add_query_arg( array( 'action' => 'duplicate', 'id' => $item[ 'id' ], '_wpnonce' => wp_create_nonce( 'nf_duplicate_form' )));
  162. $preview_url = add_query_arg( 'nf_preview_form', $item[ 'id' ], site_url() );
  163. $submissions_url = add_query_arg( 'form_id', $item[ 'id' ], admin_url( 'edit.php?post_status=all&post_type=nf_sub') );
  164. $form = Ninja_Forms()->form( $item[ 'id' ] )->get();
  165. $locked = $form->get_setting( 'lock' );
  166. Ninja_Forms::template( 'admin-menu-all-forms-column-title.html.php', compact( 'title', 'edit_url', 'delete_url', 'duplicate_url', 'preview_url', 'submissions_url', 'locked' ) );
  167. }
  168. public function single_row( $item )
  169. {
  170. $form = Ninja_Forms()->form( $item[ 'id' ] )->get();
  171. echo '<tr>';
  172. $this->single_row_columns( $item );
  173. echo '</tr>';
  174. }
  175. /**
  176. * Returns an associative array containing the bulk action
  177. *
  178. * @return array
  179. */
  180. public function get_bulk_actions()
  181. {
  182. $actions = array(
  183. 'bulk-delete' => __( 'Delete', 'ninja-forms' )
  184. );
  185. return $actions;
  186. }
  187. public static function process_bulk_action()
  188. {
  189. if( ! isset( $_GET[ 'page' ] ) || 'ninja-forms' != $_GET[ 'page' ] ) return;
  190. if ( isset( $_REQUEST[ 'action' ] ) && 'duplicate' === $_REQUEST[ 'action' ] ) {
  191. // In our file that handles the request, verify the nonce.
  192. $nonce = esc_attr( $_REQUEST['_wpnonce'] );
  193. if ( ! wp_verify_nonce( $nonce, 'nf_duplicate_form' ) ) {
  194. die( __( 'Go get a life, script kiddies', 'ninja-forms' ) );
  195. }
  196. else {
  197. NF_Database_Models_Form::duplicate( absint( $_GET['id'] ) );
  198. }
  199. wp_redirect( admin_url( 'admin.php?page=ninja-forms' ) );
  200. exit;
  201. }
  202. if ( isset( $_REQUEST[ 'action' ] ) && 'delete' === $_REQUEST[ 'action' ] ) {
  203. // In our file that handles the request, verify the nonce.
  204. $nonce = esc_attr( $_REQUEST['_wpnonce'] );
  205. if ( ! wp_verify_nonce( $nonce, 'nf_delete_form' ) ) {
  206. die( __( 'Go get a life, script kiddies', 'ninja-forms' ) );
  207. }
  208. else {
  209. self::delete_item( absint( $_GET['id'] ) );
  210. }
  211. wp_redirect( admin_url( 'admin.php?page=ninja-forms' ) );
  212. exit;
  213. }
  214. // If the delete bulk action is triggered
  215. if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' )
  216. || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' )
  217. ) {
  218. // In our file that handles the request, verify the nonce.
  219. $nonce = esc_attr( $_REQUEST['_wpnonce'] );
  220. if ( ! wp_verify_nonce( $nonce, 'bulk-forms' ) ) {
  221. die( __( 'Go get a life, script kiddies', 'ninja-forms' ) );
  222. }
  223. if( isset( $_POST[ 'bulk-delete' ] ) ) {
  224. $delete_ids = esc_sql($_POST['bulk-delete']);
  225. // loop over the array of record IDs and delete them
  226. foreach ($delete_ids as $id) {
  227. self::delete_item(absint($id));
  228. }
  229. }
  230. wp_redirect( admin_url( 'admin.php?page=ninja-forms' ) );
  231. exit;
  232. }
  233. }
  234. public static function delete_item( $id )
  235. {
  236. $form = Ninja_Forms()->form( $id )->get();
  237. $form->delete();
  238. }
  239. } // END CLASS NF_Admin_AllFormsTable