siteguard-login-history-table.php
1 month ago
siteguard-menu-admin-filter.php
4 weeks ago
siteguard-menu-author-query.php
2 weeks ago
siteguard-menu-captcha.php
4 weeks ago
siteguard-menu-dashboard.php
4 weeks ago
siteguard-menu-fail-once.php
4 weeks ago
siteguard-menu-init.php
1 month ago
siteguard-menu-login-alert.php
4 weeks ago
siteguard-menu-login-history.php
4 weeks ago
siteguard-menu-login-lock.php
4 weeks ago
siteguard-menu-protect-xmlrpc.php
4 weeks ago
siteguard-menu-rename-login.php
2 weeks ago
siteguard-menu-same-error.php
4 weeks ago
siteguard-menu-updates-notify.php
4 weeks ago
siteguard-menu-waf-tuning-support.php
4 weeks ago
siteguard-waf-exclude-rule-table.php
1 month ago
siteguard-waf-exclude-rule-table.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 4 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 5 | } |
| 6 | |
| 7 | class SiteGuard_WAF_Exclude_Rule_Table extends WP_List_Table { |
| 8 | |
| 9 | function __construct() { |
| 10 | global $status, $page; |
| 11 | |
| 12 | // Set parent defaults |
| 13 | parent::__construct( |
| 14 | array( |
| 15 | 'singular' => 'rule', // singular name of the listed records |
| 16 | 'plural' => 'rules', // plural name of the listed records |
| 17 | 'ajax' => false, // does this table support ajax? |
| 18 | ) |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | function column_default( $item, $column_name ) { |
| 23 | switch ( $column_name ) { |
| 24 | case 'filename': |
| 25 | case 'comment': |
| 26 | return esc_html( $item[ $column_name ] ); |
| 27 | default: |
| 28 | return print_r( $item, true ); // Show the whole array for troubleshooting purposes |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | function column_sig( $item ) { |
| 33 | |
| 34 | // Build row actions |
| 35 | $actions = array( |
| 36 | 'edit' => '<a href="' . esc_url( sprintf( '?page=%s&action=edit&rule=%s', esc_html( $_REQUEST['page'] ), esc_html( $item['ID'] ) ) ) . '">' . esc_html( __( 'Edit' ) ) . '</a>', |
| 37 | 'delete' => '<a href="' . esc_url( sprintf( '?page=%s&action=delete&rule=%s', esc_html( $_REQUEST['page'] ), esc_html( $item['ID'] ) ) ) . '">' . esc_html( __( 'Delete' ) ) . '</a>', |
| 38 | ); |
| 39 | |
| 40 | // Return the target contents |
| 41 | return sprintf( |
| 42 | '%1$s%2$s', |
| 43 | /*$1%s*/ esc_html( $item['sig'] ), |
| 44 | /*$2%s*/ $this->row_actions( $actions ) |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | function column_cb( $item ) { |
| 50 | return sprintf( |
| 51 | '<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 52 | /*$1%s*/ esc_attr( $this->_args['singular'] ), // Let's simply repurpose the table's singular label ("rule") |
| 53 | /*$2%s*/ esc_attr( $item['ID'] ) // The value of the checkbox should be the record's id |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | function get_columns() { |
| 59 | $columns = array( |
| 60 | 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text |
| 61 | 'sig' => esc_html__( 'Signature', 'siteguard' ), |
| 62 | 'filename' => esc_html__( 'Filename', 'siteguard' ), |
| 63 | 'comment' => esc_html__( 'Comment', 'siteguard' ), |
| 64 | ); |
| 65 | return $columns; |
| 66 | } |
| 67 | |
| 68 | function get_sortable_columns() { |
| 69 | $sortable_columns = array( |
| 70 | 'sig' => array( 'sig', false ), |
| 71 | 'filename' => array( 'filename', false ), |
| 72 | 'comment' => array( 'comment', false ), |
| 73 | ); |
| 74 | return $sortable_columns; |
| 75 | } |
| 76 | |
| 77 | function get_bulk_actions() { |
| 78 | $actions = array( |
| 79 | 'delete' => esc_html__( 'Delete' ), |
| 80 | ); |
| 81 | return $actions; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | function process_bulk_action() { |
| 86 | |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | function usort_reorder( $a, $b ) { |
| 91 | $orderby_values = array( 'sig', 'filename', 'comment' ); |
| 92 | $order_values = array( 'asc', 'desc' ); |
| 93 | $orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? ( in_array( $_REQUEST['orderby'], $orderby_values ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'sig' ) : 'sig'; // If no sort, default to filename |
| 94 | $order = ( ! empty( $_REQUEST['order'] ) ) ? ( in_array( $_REQUEST['order'], $order_values ) ? sanitize_text_field( $_REQUEST['order'] ) : 'asc' ) : 'asc'; // If no order, default to asc |
| 95 | $result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order |
| 96 | return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort |
| 97 | } |
| 98 | |
| 99 | function prepare_items() { |
| 100 | global $siteguard_waf_exclude_rule; |
| 101 | |
| 102 | $per_page = 5; |
| 103 | |
| 104 | $columns = $this->get_columns(); |
| 105 | $hidden = array(); |
| 106 | $sortable = $this->get_sortable_columns(); |
| 107 | |
| 108 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 109 | |
| 110 | $this->process_bulk_action(); |
| 111 | |
| 112 | $data = $siteguard_waf_exclude_rule->get_rules(); |
| 113 | |
| 114 | $total_items = count( $data ); |
| 115 | $current_page = $this->get_pagenum(); |
| 116 | |
| 117 | if ( $total_items > 0 ) { |
| 118 | if ( is_array( $data ) ) { |
| 119 | usort( $data, array( $this, 'usort_reorder' ) ); |
| 120 | $data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page ); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | $this->items = $data; |
| 125 | |
| 126 | $this->set_pagination_args( |
| 127 | array( |
| 128 | 'total_items' => $total_items, // WE have to calculate the total number of items |
| 129 | 'per_page' => $per_page, // WE have to determine how many items to show on a page |
| 130 | 'total_pages' => ceil( $total_items / $per_page ), // WE have to calculate the total number of pages |
| 131 | ) |
| 132 | ); |
| 133 | } |
| 134 | } |
| 135 |