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
64
65
66
67
|
<?php
namespace TotalPoll\Restrictions; ! defined( 'ABSPATH' ) && exit();
/** * Authenticated User Restriction. * @package TotalPoll\Restrictions */ class LoggedInUser 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( 'user' ); $cookieValue = absint( $this->getCookie( $cookieName ) ); $result = ! ( $cookieValue >= $this->getVotesPerUser() );
if ( ( $this->isFullCheck() || $result ) && is_user_logged_in() ):
$timeout = (int) $this->getTimeout(); $conditions = [ 'poll_id' => $this->getPollId(), 'action' => $this->getAction(), 'status' => 'accepted', 'user_id' => get_current_user_id(), 'date' => [], ];
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->getVotesPerUser() ): $result = false; endif;
endif;
return $result ?: new \WP_Error( 'loggedin_user', empty( $this->args['message'] ) ? __( 'You cannot vote again.', 'totalpoll' ) : (string) $this->args['message'] ); }
/** * Apply Restriction. */ public function apply() { $cookieTimeout = $this->getTimeout(); $cookieName = $this->getCookieName( 'user' ); $cookieValue = absint( $this->getCookie( $cookieName, 0 ) ); $this->setCookie( $cookieName, $cookieValue + 1, $cookieTimeout ); } }
|