NF_Notifications_List_Table

The Ninja Forms NF Notifications List Table class.

Defined (1)

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

/deprecated/classes/notifications-table.php  
  1. class NF_Notifications_List_Table extends WP_List_Table { 
  2.  
  3. /** 
  4. * @var form_id 
  5. */ 
  6. var $form_id = ''; 
  7.  
  8. /** ************************************************************************ 
  9. * REQUIRED. Set up a constructor that references the parent constructor. We 
  10. * use the parent reference to set some default configs. 
  11. ***************************************************************************/ 
  12. function __construct() { 
  13. global $status, $page; 
  14.  
  15. //Set parent defaults 
  16. parent::__construct( array( 
  17. 'singular' => 'notification', //singular name of the listed records 
  18. 'plural' => 'notifications', //plural name of the listed records 
  19. 'ajax' => false //does this table support ajax? 
  20. ) ); 
  21.  
  22. $this->form_id = isset ( $_REQUEST['form_id'] ) ? absint( $_REQUEST['form_id'] ) : ''; 
  23.  
  24.  
  25. /** ************************************************************************ 
  26. * Recommended. This method is called when the parent class can't find a method 
  27. * specifically build for a given column. Generally, it's recommended to include 
  28. * one method for each column you want to render, keeping your package class 
  29. * neat and organized. For example, if the class needs to process a column 
  30. * named 'title', it would first see if a method named $this->column_title() 
  31. * exists - if it does, that method will be used. If it doesn't, this one will 
  32. * be used. Generally, you should try to use custom column methods as much as 
  33. * possible. 
  34. * Since we have defined a column_title() method later on, this method doesn't 
  35. * need to concern itself with any column with a name of 'title'. Instead, it 
  36. * needs to handle everything else. 
  37. * For more detailed insight into how columns are handled, take a look at 
  38. * WP_List_Table::single_row_columns() 
  39. * @param array $item A singular item (one full row's worth of data) 
  40. * @param array $column_name The name/slug of the column to be processed 
  41. * @return string Text or HTML to be placed inside the column <td> 
  42. **************************************************************************/ 
  43. public function column_default($item, $column_name) { 
  44. switch($column_name) { 
  45. case 'type': 
  46. return Ninja_Forms()->notification( $item['id'] )->type_name(); 
  47. case 'date_updated': 
  48. return $item[$column_name]; 
  49. default: 
  50. return print_r($item, true); //Show the whole array for troubleshooting purposes 
  51.  
  52. /** ************************************************************************ 
  53. * Recommended. This is a custom column method and is responsible for what 
  54. * is rendered in any column with a name/slug of 'title'. Every time the class 
  55. * needs to render a column, it first looks for a method named 
  56. * column_{$column_title} - if it exists, that method is run. If it doesn't 
  57. * exist, column_default() is called instead. 
  58. * This example also illustrates how to implement rollover actions. Actions 
  59. * should be an associative array formatted as 'slug'=>'link html' - and you 
  60. * will need to generate the URLs yourself. You could even ensure the links 
  61. * @see WP_List_Table::::single_row_columns() 
  62. * @param array $item A singular item (one full row's worth of data) 
  63. * @return string Text to be placed inside the column <td> (movie title only) 
  64. **************************************************************************/ 
  65. public function column_name( $item ) { 
  66.  
  67. $base_url = esc_url_raw( remove_query_arg( array( '_wp_http_referer', '_wpnonce' ) ) ); 
  68.  
  69. $activate_text = ( Ninja_Forms()->notification( $item['id'] )->active ) ? __( 'Deactivate', 'ninja-forms' ) : __( 'Activate', 'ninja-forms' ); 
  70.  
  71. $activate_action = ( Ninja_Forms()->notification( $item['id'] )->active ) ? 'deactivate' : 'activate'; 
  72.  
  73. $activate_url = esc_url( add_query_arg( array( 'notification-action' => $activate_action, 'id' => $item['id'] ), $base_url ) ); 
  74. $edit_url = esc_url( add_query_arg( array( 'notification-action' => 'edit', 'id' => $item['id'] ), $base_url ) ); 
  75. $delete_url = esc_url( add_query_arg( array( 'action' => 'delete' ), $base_url ) ); 
  76. $duplicate_url = esc_url( add_query_arg( array( 'notification-action' => 'duplicate', 'id' => $item['id'] ), $base_url ) ); 
  77.  
  78. //Build row actions 
  79. $actions = array( 
  80. 'active' => '<a href="' . $activate_url . '" class="notification-activate" data-action="' . $activate_action . '" data-n_id="' . $item['id'] . '">' . $activate_text . '</a>',  
  81. 'edit' => '<a href="' . $edit_url . '">' . __( 'Edit', 'ninja-forms' ) . '</a>',  
  82. 'delete' => '<a href="' . $delete_url .'" class="notification-delete" data-n_id="' . $item['id'] . '">' . __( 'Delete', 'ninja-forms' ) . '</a>',  
  83. 'duplicate' => '<a href="' . $duplicate_url .'">' . __( 'Duplicate', 'ninja-forms' ) . '</a>',  
  84. ); 
  85.  
  86. //Return the title contents 
  87. return sprintf( '<a href="%1$s">%2$s</a> %3$s',  
  88. /**$1%s*/ $edit_url,  
  89. /**$2%s*/ $item['name'],  
  90. /**$3%s*/ $this->row_actions($actions) 
  91. ); 
  92.  
  93. /** ************************************************************************ 
  94. * REQUIRED if displaying checkboxes or using bulk actions! The 'cb' column 
  95. * is given special treatment when columns are processed. It ALWAYS needs to 
  96. * have it's own method. 
  97. * @see WP_List_Table::::single_row_columns() 
  98. * @param array $item A singular item (one full row's worth of data) 
  99. * @return string Text to be placed inside the column <td> (movie title only) 
  100. **************************************************************************/ 
  101. public function column_cb($item) { 
  102. return sprintf( 
  103. '<input type="checkbox" name="%1$s[]" value="%2$s" />',  
  104. /**$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie") 
  105. /**$2%s*/ $item['id'] //The value of the checkbox should be the record's id 
  106. ); 
  107.  
  108. /** ************************************************************************ 
  109. * REQUIRED! This method dictates the table's columns and titles. This should 
  110. * return an array where the key is the column slug (and class) and the value 
  111. * is the column's title text. If you need a checkbox for bulk actions, refer 
  112. * to the $columns array below. 
  113. * The 'cb' column is treated differently than the rest. If including a checkbox 
  114. * column in your table you must create a column_cb() method. If you don't need 
  115. * bulk actions or checkboxes, simply leave the 'cb' entry out of your array. 
  116. * @see WP_List_Table::::single_row_columns() 
  117. * @return array An associative array containing column information: 'slugs'=>'Visible Titles' 
  118. **************************************************************************/ 
  119. public function get_columns() { 
  120. $columns = array( 
  121. 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text 
  122. 'name' => __( 'Name', 'ninja-forms' ),  
  123. 'type' => __( 'Type', 'ninja-forms' ),  
  124. 'date_updated' => __( 'Date Updated', 'ninja-forms' ),  
  125. ); 
  126. return $columns; 
  127.  
  128. /** ************************************************************************ 
  129. * Optional. If you want one or more columns to be sortable (ASC/DESC toggle),  
  130. * you will need to register it here. This should return an array where the 
  131. * key is the column that needs to be sortable, and the value is db column to 
  132. * sort by. Often, the key and value will be the same, but this is not always 
  133. * the case (as the value is a column name from the database, not the list table). 
  134. * This method merely defines which columns should be sortable and makes them 
  135. * clickable - it does not handle the actual sorting. You still need to detect 
  136. * the ORDERBY and ORDER querystring variables within prepare_items() and sort 
  137. * your data accordingly (usually by modifying your query). 
  138. * @return array An associative array containing all the columns that should be sortable: 'slugs'=>array('data_values', bool) 
  139. **************************************************************************/ 
  140. public function get_sortable_columns() { 
  141. $sortable_columns = array( 
  142. 'name' => array( 'name', false ), //true means it's already sorted 
  143. 'type' => array( 'type', false ),  
  144. 'date_updated' => array( 'date_updated', false ) 
  145. ); 
  146. return $sortable_columns; 
  147.  
  148. /** ************************************************************************ 
  149. * Optional. If you need to include bulk actions in your list table, this is 
  150. * the place to define them. Bulk actions are an associative array in the format 
  151. * 'slug'=>'Visible Title' 
  152. * If this method returns an empty value, no bulk action will be rendered. If 
  153. * you specify any bulk actions, the bulk actions box will be rendered with 
  154. * the table automatically on display(). 
  155. * Also note that list tables are not automatically wrapped in <form> elements,  
  156. * so you will need to create those manually in order for bulk actions to function. 
  157. * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles' 
  158. **************************************************************************/ 
  159. public function get_bulk_actions() { 
  160. $actions = array( 
  161. 'activate' => __( 'Activate', 'ninja-forms' ),  
  162. 'deactivate' => __( 'Deactivate', 'ninja-forms' ),  
  163. 'delete' => __( 'Delete', 'ninja-forms' ),  
  164. ); 
  165. return $actions; 
  166.  
  167. public function extra_tablenav( $which ) { 
  168. if ( $which == 'bottom' ) 
  169. return false; 
  170.  
  171. if ( isset ( $_REQUEST['type'] ) ) { 
  172. $type = esc_html( $_REQUEST['type'] ); 
  173. } else { 
  174. $type = ''; 
  175.  
  176. ?> 
  177. <div class="alignleft actions"> 
  178. <select name="type" id="filter-type"> 
  179. <option value="" <?php selected( $type, '' ); ?>><?php _e( '- View All Types', 'ninja-forms' ); ?></option> 
  180. <?php 
  181. foreach ( Ninja_Forms()->notifications->get_types() as $slug => $nicename ) { 
  182. ?> 
  183. <option value="<?php echo $slug; ?>" <?php selected( $type, $slug ); ?>><?php echo $nicename; ?></option> 
  184. <?php 
  185. ?> 
  186. </select> 
  187. <span class="nf-more-actions"><a href="<?php echo nf_aff_link( 'https://ninjaforms.com/extensions/?display=actions&utm_medium=plugin&utm_source=actions-table&utm_campaign=Ninja+Forms+Upsell&utm_content=Ninja+Forms+Actions' ); ?>" target="_blank"><?php _e( 'Get More Types', 'ninja-forms' ); ?> <span class="dashicons dashicons-external"></span></a></span> 
  188. <span style="float:left;" class="spinner"></span> 
  189. </div> 
  190. <?php 
  191.  
  192. /** 
  193. * Generates content for a single row of the table 
  194. * @since 3.1.0 
  195. * @access protected 
  196. * @param object $item The current item 
  197. */ 
  198. function single_row( $item ) { 
  199. static $alternate = ''; 
  200.  
  201. $active = ( Ninja_Forms()->notification( $item['id'] )->active ) ? 'nf-notification-active ' : 'nf-notification-inactive'; 
  202. $alternate = ( $alternate == '' ? 'alternate' : '' ); 
  203.  
  204. echo '<tr class="' . $active . ' ' . $alternate . '" id="' . $item['id'] . '">'; 
  205. $this->single_row_columns( $item ); 
  206. echo '</tr>'; 
  207.  
  208. /** ************************************************************************ 
  209. * REQUIRED! This is where you prepare your data for display. This method will 
  210. * usually be used to query the database, sort and filter the data, and generally 
  211. * get it ready to be displayed. At a minimum, we should set $this->items and 
  212. * $this->set_pagination_args(), although the following properties and methods 
  213. * are frequently interacted with here... 
  214. * @global WPDB $wpdb 
  215. * @uses $this->_column_headers 
  216. * @uses $this->items 
  217. * @uses $this->get_columns() 
  218. * @uses $this->get_sortable_columns() 
  219. * @uses $this->get_pagenum() 
  220. * @uses $this->set_pagination_args() 
  221. **************************************************************************/ 
  222. public function prepare_items() { 
  223. global $wpdb; //This is used only if making any database queries 
  224.  
  225. /** 
  226. * First, lets decide how many records per page to show 
  227. */ 
  228. $per_page = 99999; 
  229.  
  230.  
  231. /** 
  232. * REQUIRED. Now we need to define our column headers. This includes a complete 
  233. * array of columns to be displayed (slugs & titles), a list of columns 
  234. * to keep hidden, and a list of columns that are sortable. Each of these 
  235. * can be defined in another method (as we've done here) before being 
  236. * used to build the value for our _column_headers property. 
  237. */ 
  238. $columns = $this->get_columns(); 
  239. $hidden = array(); 
  240. $sortable = $this->get_sortable_columns(); 
  241.  
  242.  
  243. /** 
  244. * REQUIRED. Finally, we build an array to be used by the class for column 
  245. * headers. The $this->_column_headers property takes an array which contains 
  246. * 3 other arrays. One for all columns, one for hidden columns, and one 
  247. * for sortable columns. 
  248. */ 
  249. $this->_column_headers = array($columns, $hidden, $sortable); 
  250.  
  251.  
  252. /** 
  253. * Optional. You can handle your bulk actions however you see fit. In this 
  254. * case, we'll handle them within our package just to keep things clean. 
  255. */ 
  256. //$this->process_bulk_action(); 
  257.  
  258.  
  259. /** 
  260. * Instead of querying a database, we're going to fetch the example data 
  261. * property we created for use in this plugin. This makes this example 
  262. * package slightly different than one you might build on your own. In 
  263. * this example, we'll be using array manipulation to sort and paginate 
  264. * our data. In a real-world implementation, you will probably want to 
  265. * use sort and pagination data to build a custom query instead, as you'll 
  266. * be able to use your precisely-queried data immediately. 
  267. */ 
  268. $notifications = nf_get_notifications_by_form_id( $this->form_id ); 
  269. $data = array(); 
  270.  
  271. if ( is_array( $notifications ) ) { 
  272. foreach ( $notifications as $id => $n ) { 
  273. if ( isset ( $_REQUEST['type'] ) && ! empty( $_REQUEST['type'] ) ) { 
  274. if ( nf_get_object_meta_value( $id, 'type' ) == esc_html( $_REQUEST['type'] ) ) { 
  275. $n['id'] = $id; 
  276. $data[] = $n; 
  277. } else { 
  278. $n['id'] = $id; 
  279. $data[] = $n; 
  280.  
  281.  
  282. /** 
  283. * This checks for sorting input and sorts the data in our array accordingly. 
  284. * In a real-world situation involving a database, you would probably want 
  285. * to handle sorting by passing the 'orderby' and 'order' values directly 
  286. * to a custom query. The returned data will be pre-sorted, and this array 
  287. * sorting technique would be unnecessary. 
  288. */ 
  289. function usort_reorder($a, $b) { 
  290. $orderby = (!empty($_REQUEST['orderby'])) ? esc_html( $_REQUEST['orderby'] ) : 'name'; //If no sort, default to title 
  291. $order = (!empty($_REQUEST['order'])) ? esc_html( $_REQUEST['order'] ) : 'asc'; //If no order, default to asc 
  292. $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order 
  293. return ($order==='asc') ? $result : -$result; //Send final sort direction to usort 
  294. usort($data, 'usort_reorder'); 
  295.  
  296.  
  297. /*********************************************************************** 
  298. * --------------------------------------------------------------------- 
  299. * vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv 
  300. * In a real-world situation, this is where you would place your query. 
  301. * For information on making queries in WordPress, see this Codex entry: 
  302. * http://codex.wordpress.org/Class_Reference/wpdb 
  303. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
  304. * --------------------------------------------------------------------- 
  305. **********************************************************************/ 
  306.  
  307.  
  308. /** 
  309. * REQUIRED for pagination. Let's figure out what page the user is currently 
  310. * looking at. We'll need this later, so you should always include it in 
  311. * your own package classes. 
  312. */ 
  313. $current_page = $this->get_pagenum(); 
  314.  
  315. /** 
  316. * REQUIRED for pagination. Let's check how many items are in our data array. 
  317. * In real-world use, this would be the total number of items in your database,  
  318. * without filtering. We'll need this later, so you should always include it 
  319. * in your own package classes. 
  320. */ 
  321. $total_items = count($data); 
  322.  
  323.  
  324. /** 
  325. * The WP_List_Table class does not handle pagination for us, so we need 
  326. * to ensure that the data is trimmed to only the current page. We can use 
  327. * array_slice() to 
  328. */ 
  329. $data = array_slice($data, (($current_page-1)*$per_page), $per_page); 
  330.  
  331.  
  332.  
  333. /** 
  334. * REQUIRED. Now we can add our *sorted* data to the items property, where 
  335. * it can be used by the rest of the class. 
  336. */ 
  337. $this->items = $data; 
  338.  
  339.  
  340. /** 
  341. * REQUIRED. We also have to register our pagination options & calculations. 
  342. */ 
  343. $this->set_pagination_args( array( 
  344. 'total_items' => $total_items, //WE have to calculate the total number of items 
  345. 'per_page' => $per_page, //WE have to determine how many items to show on a page 
  346. 'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages 
  347. ) ); 
  348.