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