settings = $settings; $this->ip = $ip; $this->limiter = $limiter; } /** * Registers hooks. * * @return void */ public function boot() { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_init', array( $this, 'register' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); add_filter( 'plugin_action_links_' . plugin_basename( CCLG_FILE ), array( $this, 'action_links' ) ); foreach ( array( 'unlock', 'unlock_all', 'clear_log', 'import', 'allow_me' ) as $action ) { add_action( 'admin_post_cclg_' . $action, array( $this, 'handle_' . $action ) ); } } /** * Adds the Settings submenu. * * @return void */ public function menu() { $this->hook = add_options_page( __( 'CodingCush Login Guard', 'codingcush-login-guard' ), __( 'Login Guard', 'codingcush-login-guard' ), 'manage_options', self::SLUG, array( $this, 'render' ) ); } /** * Registers the option with the Settings API. * * @return void */ public function register() { register_setting( 'cclg', CCLG_OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this->settings, 'sanitize' ), 'default' => Settings::defaults(), ) ); } /** * Adds a Settings link on the Plugins screen. * * @param string[] $links Links. * @return string[] */ public function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'codingcush-login-guard' ) . '' ); return $links; } /** * Loads CSS/JS on our screen only. * * @param string $hook Current hook suffix. * @return void */ public function assets( $hook ) { if ( $hook !== $this->hook ) { return; } wp_enqueue_style( 'cclg-admin', CCLG_URL . 'assets/css/admin.css', array( 'dashicons' ), CCLG_VERSION ); wp_enqueue_script( 'cclg-admin', CCLG_URL . 'assets/js/admin.js', array(), CCLG_VERSION, true ); } /** * Screen URL. * * @param array $args Extra query args. * @return string */ public function url( $args = array() ) { return add_query_arg( array_merge( array( 'page' => self::SLUG ), $args ), admin_url( 'options-general.php' ) ); } /** * Renders the screen. * * @return void */ public function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only list filters. $filter_event = isset( $_GET['cclg_event'] ) ? sanitize_key( wp_unslash( $_GET['cclg_event'] ) ) : ''; $filter_search = isset( $_GET['cclg_s'] ) ? sanitize_text_field( wp_unslash( $_GET['cclg_s'] ) ) : ''; $page_num = isset( $_GET['cclg_p'] ) ? max( 1, absint( $_GET['cclg_p'] ) ) : 1; $msg = isset( $_GET['cclg_msg'] ) ? sanitize_key( wp_unslash( $_GET['cclg_msg'] ) ) : ''; // phpcs:enable if ( ! array_key_exists( $filter_event, Log::events() ) ) { $filter_event = ''; } $per_page = 25; $view = array( 'settings' => $this->settings, 'values' => $this->settings->all(), 'admin' => $this, 'my_ip' => $this->ip->client_ip(), 'detected' => Ip::detected_sources(), 'my_trusted' => $this->limiter->is_trusted( $this->ip->client_ip() ), 'stats' => Log::counts_since( 24 ), 'stats_week' => Log::counts_since( 24 * 7 ), 'active' => Log::active_lockouts(), 'rows' => Log::recent( $filter_event, $filter_search, $per_page, ( $page_num - 1 ) * $per_page ), 'total' => Log::count( $filter_event, $filter_search ), 'per_page' => $per_page, 'page_num' => $page_num, 'filter_event' => $filter_event, 'filter_search' => $filter_search, 'top_users' => Log::top_usernames( 7, 5 ), 'import' => Importer::available() ? Importer::preview() : array(), 'notice' => $this->notice( $msg ), 'woo' => class_exists( 'WooCommerce' ), ); include CCLG_DIR . 'includes/admin/view-page.php'; } /** * Notice text for a result code. * * @param string $code Code. * @return string */ private function notice( $code ) { $map = array( 'unlocked' => __( 'The IP address was unlocked.', 'codingcush-login-guard' ), 'unlocked_all' => __( 'All lockouts were lifted.', 'codingcush-login-guard' ), 'cleared' => __( 'The log was cleared. Running lockouts were kept.', 'codingcush-login-guard' ), 'imported' => __( 'Settings were imported from Limit Login Attempts. Review them below and save if you change anything.', 'codingcush-login-guard' ), 'nothing' => __( 'There was nothing to import.', 'codingcush-login-guard' ), 'allowed' => __( 'Your IP address was added to the allow list.', 'codingcush-login-guard' ), ); return isset( $map[ $code ] ) ? $map[ $code ] : ''; } // admin-post handlers. /** * Stops the request unless the user can manage the site. * * @return void */ private function require_admin() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'codingcush-login-guard' ), 403 ); } } /** * Redirects back to a tab with a result code. * * @param string $msg Code. * @param string $tab Tab. * @return void */ private function back( $msg, $tab = 'overview' ) { wp_safe_redirect( $this->url( array( 'cclg_msg' => $msg ) ) . '#' . $tab ); exit; } /** * Unlocks one IP. * * @return void */ public function handle_unlock() { $this->require_admin(); if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ), 'cclg_unlock' ) ) { wp_die( esc_html__( 'The link you followed has expired. Please go back and try again.', 'codingcush-login-guard' ), 403 ); } $ip = isset( $_POST['ip'] ) ? sanitize_text_field( wp_unslash( $_POST['ip'] ) ) : ''; if ( filter_var( $ip, FILTER_VALIDATE_IP ) ) { /* translators: %s: username of the administrator. */ $this->limiter->unlock( $ip, sprintf( __( 'Unlocked by %s', 'codingcush-login-guard' ), wp_get_current_user()->user_login ) ); } $this->back( 'unlocked' ); } /** * Lifts every running lockout. * * @return void */ public function handle_unlock_all() { $this->require_admin(); check_admin_referer( 'cclg_unlock_all' ); foreach ( Log::active_lockouts() as $row ) { /* translators: %s: username of the administrator. */ $this->limiter->unlock( (string) $row['ip'], sprintf( __( 'Unlocked by %s', 'codingcush-login-guard' ), wp_get_current_user()->user_login ) ); } $this->back( 'unlocked_all', 'tools' ); } /** * Clears the log. * * @return void */ public function handle_clear_log() { $this->require_admin(); check_admin_referer( 'cclg_clear_log' ); Log::clear(); $this->back( 'cleared', 'tools' ); } /** * Imports Limit Login Attempts settings. * * @return void */ public function handle_import() { $this->require_admin(); check_admin_referer( 'cclg_import' ); $n = Importer::run( $this->settings ); $this->back( $n ? 'imported' : 'nothing', $n ? 'limits' : 'tools' ); } /** * Adds the current admin's IP to the allow list. * * @return void */ public function handle_allow_me() { $this->require_admin(); check_admin_referer( 'cclg_allow_me' ); $ip = $this->ip->client_ip(); if ( '' !== $ip ) { $values = $this->settings->all(); $values['allowlist'] = implode( "\n", Ip::parse_list( $values['allowlist'] . "\n" . $ip ) ); update_option( CCLG_OPTION, $this->settings->sanitize( $values ) ); } $this->back( 'allowed', 'ip' ); } /** * Small helper for the view: a POST button to an admin-post action. * * @param string $action Action (without the cclg_ prefix). * @param string $label Button label. * @param array $fields Hidden fields. * @param string $css_class Button class. * @param string $confirm Optional confirmation text. * @return void */ public function button( $action, $label, $fields = array(), $css_class = 'cclg-btn cclg-btn-ghost', $confirm = '' ) { echo '
'; echo ''; wp_nonce_field( 'cclg_' . $action ); foreach ( $fields as $name => $value ) { echo ''; } echo ''; echo '
'; } }