null|Model */ public static function is_blocklisted_ip( $ip ): ?Model { $orm = self::get_orm(); return $orm->get_repository( self::class ) ->select( 'ip,status' ) ->where( 'ip', $ip ) ->where( 'status', self::STATUS_BLOCKED ) ->first(); } /** * Maybe unblock IP? * * @param string $ip The IP address to search for. * * @return string */ public static function get_unlocked_ip_by( $ip ) { $orm = self::get_orm(); $model = $orm->get_repository( self::class ) ->where( 'ip', $ip ) ->first(); if ( is_object( $model ) ) { $model->status = self::STATUS_NORMAL; $orm->save( $model ); return $model->ip; } return ''; } /** * Retrieves bulk IPs based on the provided status, IPs, and limit. * * @param string $status The status of the IPs to retrieve. * @param array|null $ips An array of IPs to retrieve. If null, retrieves all IPs with the given status. * @param int|null $limit The maximum number of IPs to retrieve. If null, retrieves all IPs. * * @return array An array of IP models. */ public static function get_bulk( string $status, $ips = null, $limit = null ) { $orm = self::get_orm(); $builder = $orm->get_repository( self::class ); if ( null === $ips ) { $builder->where( 'status', $status ); } if ( null !== $ips ) { $builder->where( 'ip', 'in', $ips ); } if ( null !== $limit ) { $builder->limit( $limit ); } return $builder->get(); } /** * Get the access status of this IP. * * @return array */ public function get_access_status(): array { $settings = wd_di()->get( Blacklist_Lockout::class ); if ( ! in_array( $this->ip, $settings->get_list( 'blocklist' ), true ) && ! in_array( $this->ip, $settings->get_list( 'allowlist' ), true ) ) { return array( 'na' ); } $result = array(); if ( in_array( $this->ip, $settings->get_list( 'blocklist' ), true ) ) { $result[] = 'banned'; } if ( in_array( $this->ip, $settings->get_list( 'allowlist' ), true ) ) { $result[] = 'allowlist'; } return $result; } /** * Returns the text representation of the access status based on the given status code. * * @param string $status The status code to determine the access status text for. * Possible values are: 'banned', 'allowlist', 'na'. * * @return string The text representation of the access status. * Returns an empty string if the status code is not recognized. */ public function get_access_status_text( string $status ): string { switch ( $status ) { case 'banned': return esc_html__( 'Banned', 'wpdef' ); case 'allowlist': return esc_html__( 'In Allowlist', 'wpdef' ); case 'na': return esc_html__( 'Not banned or in allowlist', 'wpdef' ); default: return ''; } } /** * Get locked IPs. * * @return array */ public static function query_locked_ip(): array { $orm = self::get_orm(); $time = new DateTime( 'now', wp_timezone() ); return $orm->get_repository( self::class ) ->select( 'id,ip,status' ) ->where( 'status', self::STATUS_BLOCKED ) ->where( 'release_time', '>', $time->getTimestamp() ) ->group_by( 'ip' ) ->order_by( 'lock_time', 'desc' ) ->get_results(); } /** * Checks if the current object is locked. * * @return bool Returns false if the object is not locked, true otherwise. */ public function is_locked(): bool { if ( self::STATUS_BLOCKED === $this->status ) { $time = new DateTime( 'now', wp_timezone() ); if ( $this->release_time < $time->getTimestamp() ) { // Unlock it and clear the metadata. $this->attempt = 0; $this->meta = array( 'nf' => array(), 'login' => array(), ); $this->status = self::STATUS_NORMAL; $this->save(); return false; } else { return true; } } return false; } /** * Return remaining release time. * * @return int Remaining release time. */ public function remaining_release_time(): int { $time = new DateTime( 'now', wp_timezone() ); return $this->release_time - $time->getTimestamp(); } /** * Remove all records. * * @return bool|int * @since 3.3.0 */ public static function truncate() { $orm = self::get_orm(); return $orm->get_repository( self::class )->truncate(); } }