'IP', 'plural' => 'IP', 'ajax' => true, ) ); $this->basename = $plugin_basename; } /** * Display content * * @return void */ public function display_content() { global $gglcptch_options; ?>

display_notices(); $this->prepare_items(); ?>
display(); wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?>
get_sortable_columns() ) ) ) { switch ( $_GET['orderby'] ) { case 'ip': $this->order_by = 'ip_from_int'; break; case 'ip_from': $this->order_by = 'ip_from_int'; break; case 'ip_to': $this->order_by = 'ip_to_int'; break; default: $this->order_by = esc_sql( sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) ); break; } } else { $this->order_by = 'add_time'; } $this->order = isset( $_REQUEST['order'] ) && in_array( strtoupper( sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) ), array( 'ASC', 'DESC' ), true ) ? sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) : ''; $this->paged = isset( $_REQUEST['paged'] ) && is_numeric( $_REQUEST['paged'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['paged'] ) ) : ''; $this->s = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; $this->per_page = $this->get_items_per_page( 'gglcptch_per_page', 20 ); $columns = $this->get_columns(); $hidden = array(); $sortable = $this->get_sortable_columns(); $primary = 'ip'; $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); $this->items = $this->get_content(); $this->set_pagination_args( array( 'total_items' => $this->get_items_number(), 'per_page' => 20, ) ); } /** * Function to show message if empty list * * @return void */ public function no_items() { $label = isset( $_REQUEST['s'] ) ? __( 'Nothing found', 'google-captcha' ) : __( 'No IP in the Allow List', 'google-captcha' ); ?>

'', 'ip' => __( 'IP Address', 'google-captcha' ), 'add_time' => __( 'Date Added', 'google-captcha' ), ); return $columns; } /** * Get a list of sortable columns. * * @return array list of sortable columns */ public function get_sortable_columns() { $sortable_columns = array( 'ip' => array( 'ip', true ), 'add_time' => array( 'add_time', 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 */ public function column_default( $item, $column_name ) { switch ( $column_name ) { case 'ip': case 'add_time': return $item[ $column_name ]; default: /* Show whole array for bugfix */ return print_r( $item, true ); } } /** * Function to manage content of column with checboxes * * @param array $item The cuurrent letter data. * @return string with html-structure of */ public 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 with html-structure of */ public function column_ip( $item ) { $order_by = empty( $this->order_by ) ? '' : "&orderby={$this->order_by}"; $order = empty( $this->order ) ? '' : "&order={$this->order}"; $paged = empty( $this->paged ) ? '' : "&paged={$this->paged}"; $s = empty( $this->s ) ? '' : "&s={$this->s}"; $url = "?page=google-captcha-allowlist.php&gglcptch_remove={$item['id']}{$order_by}{$order}{$paged}{$s}"; $actions = array( 'delete' => '' . __( 'Delete', 'google-captcha' ) . '', ); return sprintf( '%1$s %2$s', $item['ip'], $this->row_actions( $actions ) ); } /** * List with bulk action for IP * * @return array $actions */ public function get_bulk_actions() { /* adding bulk action */ return array( 'gglcptch_remove' => __( 'Delete', 'google-captcha' ) ); } /** * Get content for table * * @return array */ public function get_content() { global $wpdb; if ( empty( $this->s ) ) { $where = ''; } else { $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0; $where = 0 === $ip_int ? " WHERE `ip` LIKE '%{$this->s}%'" : " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )"; } $order_by = empty( $this->order_by ) ? '' : " ORDER BY `{$this->order_by}`"; $order = empty( $this->order ) ? '' : strtoupper( " {$this->order}" ); $offset = empty( $this->paged ) ? '' : ' OFFSET ' . ( $this->per_page * ( absint( $this->paged ) - 1 ) ); return $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}gglcptch_allowlist`{$where}{$order_by}{$order} LIMIT {$this->per_page}{$offset}", ARRAY_A ); } /** * Get number of all IPs which were added to database * * @since 1.6.9 * @param void * @return int the number of IPs */ private function get_items_number() { global $wpdb; if ( empty( $this->s ) ) { $where = ''; } else { $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0; $where = 0 === $ip_int ? " WHERE `ip` LIKE '%{$this->s}%'" : " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )"; } return absint( $wpdb->get_var( "SELECT COUNT(`id`) FROM `{$wpdb->prefix}gglcptch_allowlist`{$where}" ) ); } /** * Handle necessary reqquests and display notices * * @return void */ public function display_notices() { global $wpdb, $gglcptch_options; $error = $message = ''; $bulk_action = isset( $_REQUEST['action'] ) && 'gglcptch_remove' === $_REQUEST['action'] ? true : false; if ( ! $bulk_action ) { $bulk_action = isset( $_REQUEST['action2'] ) && 'gglcptch_remove' === $_REQUEST['action2'] ? true : false; } /* Add IP to the database */ if ( isset( $_POST['gglcptch_add_to_allowlist'] ) && ( ! empty( $_POST['gglcptch_add_to_allowlist'] ) || isset( $_POST['gglcptch_add_to_allowlist_my_ip'] ) ) && check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) ) { $add_ip = isset( $_POST['gglcptch_add_to_allowlist_my_ip'] ) ? sanitize_text_field( wp_unslash( $_POST['gglcptch_add_to_allowlist_my_ip_value'] ) ) : sanitize_text_field( wp_unslash( $_POST['gglcptch_add_to_allowlist'] ) ); $valid_ip = filter_var( stripslashes( trim( $add_ip ) ), FILTER_VALIDATE_IP ); if ( $valid_ip ) { $ip_int = sprintf( '%u', ip2long( $valid_ip ) ); $id = $wpdb->get_var( $wpdb->prepare( 'SELECT `id` FROM ' . $wpdb->prefix . 'gglcptch_allowlist WHERE ( `ip_from_int` <= %d AND `ip_to_int` >= %d ) OR `ip` LIKE %s LIMIT 1;', $ip_int, $ip_int, $valid_ip ) ); /* check if IP already in database */ if ( is_null( $id ) ) { $time = current_time( 'mysql' ); $wpdb->insert( $wpdb->prefix . 'gglcptch_allowlist', array( 'ip' => $valid_ip, 'ip_from_int' => $ip_int, 'ip_to_int' => $ip_int, 'add_time' => $time, ) ); if ( ! $wpdb->last_error ) { $message = __( 'IP added to the allow list successfully.', 'google-captcha' ); } else { $error = __( 'Some errors occurred.', 'google-captcha' ); } } else { $error = __( 'IP is already in the allow list.', 'google-captcha' ); } } else { $error = __( 'Invalid IP. See allowed formats.', 'google-captcha' ); } if ( empty( $error ) ) { $gglcptch_options['allowlist_is_empty'] = false; update_option( 'gglcptch_options', $gglcptch_options ); } /* Remove IP from database */ } elseif ( $bulk_action && check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) ) { if ( ! empty( $_REQUEST['id'] ) ) { foreach ( $_REQUEST['id'] as $key => $value ) { $_REQUEST['id'][ $key ] = absint( $value ); } $list = implode( ',', $_REQUEST['id'] ); $result = $wpdb->query( 'DELETE FROM `' . $wpdb->prefix . 'gglcptch_allowlist` WHERE `id` IN (' . $list . ');' ); if ( ! $wpdb->last_error ) { $message = sprintf( _n( '%s IP was deleted successfully.', '%s IPs were deleted successfully.', $result, 'google-captcha' ), $result ); $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false; update_option( 'gglcptch_options', $gglcptch_options ); } else { $error = __( 'Some errors occurred.', 'google-captcha' ); } } } elseif ( isset( $_GET['gglcptch_remove'] ) && check_admin_referer( 'gglcptch_nonce_remove_' . sanitize_text_field( wp_unslash( $_GET['gglcptch_remove'] ) ) ) ) { $wpdb->delete( $wpdb->prefix . 'gglcptch_allowlist', array( 'id' => absint( sanitize_text_field( wp_unslash( $_GET['gglcptch_remove'] ) ) ) ) ); if ( ! $wpdb->last_error ) { $message = __( 'One IP was deleted successfully.', 'google-captcha' ); $gglcptch_options['allowlist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_allowlist` LIMIT 1" ) ) ? true : false; update_option( 'gglcptch_options', $gglcptch_options ); } else { $error = __( 'Some errors occurred.', 'google-captcha' ); } } elseif ( isset( $_POST['gglcptch_submit_add_to_allowlist'] ) && empty( $_POST['gglcptch_add_to_allowlist'] ) ) { $error = __( 'You have not entered any IP.', 'google-captcha' ); } elseif ( isset( $_REQUEST['s'] ) ) { if ( '' === sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) ) { $error = __( 'You have not entered any IP in to the search form.', 'google-captcha' ); } else { $message = __( 'Search results for', 'google-captcha' ) . ' : ' . sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ); } } if ( ! empty( $message ) ) { ?>