NF_Admin_AllFormsTable

The Ninja Forms NF Admin AllFormsTable class.

Defined (1)

The class is defined in the following location(s).

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