1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
<?php
namespace TotalPoll\Restrictions; ! defined( 'ABSPATH' ) && exit();
/** * IP Restriction. * @package TotalPoll\Restrictions */ class IPAddress extends Restriction { use \TotalPollVendors\TotalCore\Traits\Cookies;
/** * @return bool|\WP_Error */ public function check() {
// If a specific cookie exists then we can waive DB check $cookieName = $this->getCookieName( 'ip' ); $cookieValue = absint( $this->getCookie( $cookieName ) ); $result = ! ( $cookieValue >= $this->getVotesPerIP() );
if ( $this->isFullCheck() || $result ): $conditions = [ 'poll_id' => $this->getPollId(), 'action' => $this->getAction(), 'status' => 'accepted', 'ip' => (string) TotalPoll( 'http.request' )->ip(), 'date' => [], ];
$timeout = $this->getTimeout(); if ( $timeout !== 0 ): $date = TotalPoll( 'datetime', [ "-{$timeout} minutes" ] ); $conditions['date'][] = [ 'operator' => '>', 'value' => $date->format( 'Y/m/d H:i:s' ) ]; endif;
$count = TotalPoll( 'log.repository' )->count( [ 'conditions' => $conditions ] ); if ( $count >= $this->getVotesPerIP() ): $this->setCookie( $cookieName, (int) $this->getVotesPerIP(), $timeout ); $result = false; endif; endif;
return $result ?: new \WP_Error( 'ip', $this->getMessage() ?: __( 'You cannot vote again.', 'totalpoll' ) ); }
/** * Apply restriction. */ public function apply() { $cookieTimeout = $this->getTimeout(); $cookieName = $this->getCookieName( 'ip' ); $cookieValue = absint( $this->getCookie( $cookieName, 0 ) ); $this->setCookie( $cookieName, $cookieValue + 1, $cookieTimeout ); } }
|