textdomain ); ?>
get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $this->_column_headers = array( $columns, $hidden, $sortable ); $this->items = $this->get_content(); $current_page = $this->get_pagenum(); $this->set_pagination_args( array( 'total_items' => count( $this->items ), 'per_page' => 20, ) ); } /** * Function to show message if empty list * @return void */ function no_items() { $label = isset( $_REQUEST['s'] ) ? __( 'Nothing found', $this->textdomain ) : __( 'No IP in whitelist', $this->textdomain ); ?> '', 'ip' => __( 'IP address', $this->textdomain ), 'ip_from' => __( 'Diapason from', $this->textdomain ), 'ip_to' => __( 'Diapason till', $this->textdomain ), ); return $columns; } /** * Get a list of sortable columns. * @return array list of sortable columns */ function get_sortable_columns() { $sortable_columns = array( 'ip' => array( 'ip', true ), 'ip_from' => array( 'ip_from', false ), 'ip_to' => array( 'ip_to', false ) ); return $sortable_columns; } /** * Fires when the default column output is displayed for a single row. * @param string $column_name The custom column's name. * @param array $item The cuurrent letter data. * @return void */ function column_default( $item, $column_name ) { switch ( $column_name ) { case 'ip': case 'ip_from': case 'ip_to': return $item[ $column_name ]; default: /* Show whole array for bugfix */ return print_r( $item, true ); } } /** * Function to manafe content of column with checboxes * @param array $item The cuurrent letter data. * @return string with html-structure of */ function column_cb( $item ) { /* customize displaying cb collumn */ return sprintf( '', $item['id'] ); } /** * Function to manage content of column with IP-adresses * @param array $item The cuurrent letter data. * @return string */ function column_ip( $item ) { $actions = array( 'remove' => '' . __( 'Remove from whitelist', $this->textdomain ) . '' ); return sprintf('%1$s %2$s', $item['ip'], $this->row_actions( $actions ) ); } /** * List with bulk action for IP * @return array $actions */ function get_bulk_actions() { /* adding bulk action */ $actions = array( 'cptch_remove' => __( 'Remove from whitelist', $this->textdomain ), ); return $actions; } /** * Get content for table * @return array */ function get_content() { global $wpdb; $per_page = 20; $paged = ( isset( $_REQUEST['paged'] ) && 1 < intval( $_REQUEST['paged'] ) ) ? $per_page * ( absint( intval( $_REQUEST['paged'] ) - 1 ) ) : 0; $order = isset( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'ASC'; if ( isset( $_GET['orderby'] ) && in_array( $_GET['orderby'], array_keys( $this->get_sortable_columns() ) ) && 'ip' != $_GET['orderby'] ) { switch ( $_GET['orderby'] ) { case 'ip_from': $order_by = 'ip_from_int'; break; case 'ip_to': $order_by = 'ip_to_int'; break; default: $order_by = $_GET['orderby']; break; } } else { $order_by = 'ip_from_int'; } $sql_query = "SELECT * FROM `" . $wpdb->prefix . "cptch_whitelist` "; if ( isset( $_REQUEST['s'] ) ) { $ip = stripslashes( esc_html( trim( $_REQUEST['s'] ) ) ); if ( '' != $ip ) { $ip_int = sprintf( '%u', ip2long( $ip ) ); $query_where = 0 == $ip_int ? "`ip` LIKE '%" . $ip . "%' OR `ip_to` LIKE '%" . $ip . "%' OR `ip_from` LIKE '%" . $ip . "%'" : "( `ip_from_int` <= " . $ip_int . " AND `ip_to_int` >= " . $ip_int . " )"; $sql_query .= "WHERE " . $query_where; } } $sql_query .= " ORDER BY " . $order_by . " " . $order . " LIMIT " . $per_page . " OFFSET " . $paged . ";"; return $wpdb->get_results( $sql_query, ARRAY_A ); } /** * Handle necessary reqquests and display notices * @return void */ function display_notices() { global $wpdb; $error = $message = ''; $bulk_action = isset( $_REQUEST['action'] ) && 'cptch_remove' == $_REQUEST['action'] ? true : false; if ( ! $bulk_action ) $bulk_action = isset( $_REQUEST['action2'] ) && 'cptch_remove' == $_REQUEST['action2'] ? true : false; /* Add IP in to database */ if ( isset( $_POST['cptch_add_to_whitelist'] ) && ( ! empty( $_POST['cptch_add_to_whitelist'] ) ) && check_admin_referer( $this->basename, 'captcha_nonce_name' ) ) { $valid_ip = filter_var( stripslashes( esc_html( trim( $_REQUEST['cptch_add_to_whitelist'] ) ) ), FILTER_VALIDATE_IP ); if ( $valid_ip ) { $ip_int = sprintf( '%u', ip2long( $valid_ip ) ); $id = $wpdb->get_var( "SELECT `id` FROM " . $wpdb->prefix . "cptch_whitelist WHERE ( `ip_from_int` <= " . $ip_int . " AND `ip_to_int` >= " . $ip_int . " ) OR `ip` LIKE '" . $valid_ip . "' LIMIT 1;" ); /* check if IP already in database */ if ( is_null( $id ) ) { $wpdb->insert( $wpdb->prefix . "cptch_whitelist", array( 'ip' => $valid_ip, 'ip_from' => $valid_ip, 'ip_to' => $valid_ip, 'ip_from_int' => $ip_int, 'ip_to_int' => $ip_int ) ); if ( ! $wpdb->last_error ) $message = __( 'IP added in database successfully', $this->textdomain ); else $error = __( 'Some errors occured', $this->textdomain ); } else { $error = __( 'IP is allready in database', $this->textdomain ); } } else { $error = __( 'Invalid IP. See allowed formats', $this->textdomain ); } /* Remove IP from database */ } elseif ( $bulk_action && check_admin_referer( $this->basename, 'captcha_nonce_name' ) ) { if ( ! empty( $_REQUEST['id'] ) ) { $list = implode( ',', $_REQUEST['id'] ); $result = $wpdb->query( "DELETE FROM `" . $wpdb->prefix . "cptch_whitelist` WHERE `id` IN (" . $list . ");" ); if ( ! $wpdb->last_error ) $message = sprintf( _n( "One IP was deleted successfully", "%s IPs were deleted successfully", $result, $this->textdomain ), $result ); else $error = __( 'Some errors occured', $this->textdomain ); } } elseif ( isset( $_GET['cptch_remove'] ) && check_admin_referer( 'cptch_nonce_remove_' . $_GET['cptch_remove'] ) ) { $result = $wpdb->query( "DELETE FROM `" . $wpdb->prefix . "cptch_whitelist` WHERE `id` = ". $_GET['cptch_remove'] . ";" ); if ( ! $wpdb->last_error ) $message = __( "One IP was deleted successfully", $this->textdomain ); else $error = __( 'Some errors occured', $this->textdomain ); } elseif ( isset( $_POST['cptch_add_to_whitelist'] ) && empty( $_POST['cptch_add_to_whitelist'] ) ) { $error = __( 'You have not entered any value', $this->textdomain ); } elseif ( isset( $_REQUEST['s'] ) ) { if ( '' == $_REQUEST['s'] ) { $error = __( 'You have not enterd any value in to the search form', $this->textdomain ); } else { $message = __( 'Search results for', $this->textdomain ) . ' : ' . $_REQUEST['s']; } } if ( ! empty( $message ) ) { ?>.
.