notifications-table.php 20 KB

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