C:\xampp\htdocs\landing\wp-content\updraft\plugins-old\totalpoll\src\Poll\Commands\CountVote.php


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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

namespace TotalPoll\Poll\Commands;
defined'ABSPATH' ) && exit();


use 
TotalPoll\Contracts\Poll\Model;
use 
TotalPoll\Contracts\Poll\Repository;
use 
TotalPollVendors\TotalCore\Contracts\Http\Request;
use 
TotalPollVendors\TotalCore\Helpers\Arrays;
use 
TotalPollVendors\TotalCore\Helpers\Strings;
use 
TotalPollVendors\TotalCore\Traits\Cookies;

/**
 * Class CountVote
 * @package TotalPoll\Poll\Commands
 */
class CountVote extends \TotalPollVendors\TotalCore\Helpers\Command {
    use 
Cookies;
    
/**
     * @var Model $poll
     */
    
protected $poll;
    
/**
     * @var Request $request
     */
    
protected $request;
    
/**
     * @var Repository $repository
     */
    
protected $repository;

    
/**
     * CountVote constructor.
     *
     * @param Model      $poll
     * @param Request    $request
     * @param Repository $repository
     */
    
public function __constructModel $pollRequest $requestRepository $repository ) {
        
$this->poll       $poll;
        
$this->request    $request;
        
$this->repository $repository;
    }

    
/**
     * Count vote logic.
     *
     * @return mixed
     */
    
protected function handle() {
        try {
            
// Checking submitted choices against poll's questions. All questions must be present.
            
$questions   $this->poll->getQuestions();
            
$userChoices array_filter( (array) $this->request->request'totalpoll.choices', [] ) );
            
// Cleanup
            
foreach ( $userChoices as $questionUid => $choices ):
                
$userChoices$questionUid ] = array_filter( (array) $choices );
            endforeach;

            
$customChoices = [];
            
$choicesUids   = [];

            
/**
             * Fires before processing vote request.
             *
             * @param array                           $userChoices Submitted choices (questionUID => choiceUID[]).
             * @param \TotalPoll\Contracts\Poll\Model $poll        Poll model object.
             *
             * @since 4.0.0
             */
            
do_action'totalpoll/actions/before/poll/command/vote'$userChoices$this->poll );

            foreach ( 
$questions as $questionUid => $question ):
                
// Prepare other choice
                
if ( isset( $userChoices$questionUid ]['other'] ) ):
                    
$other trimsanitize_text_field$userChoices$questionUid ]['other'] ) );
                    unset( 
$userChoices$questionUid ]['other'] );
                endif;

                
// Are custom choices allowed
                
$allowCustomChoice Arrays::getDotNotation$question'settings.allowCustomChoice' );

                
// Start processing if there is content in other field and custom choices are allowed
                
if ( ! empty( $other ) && $allowCustomChoice ):
                    
$customChoice false;

                    
// Search for matches in choices
                    
foreach ( $question['choices'] as $choiceUid => $choice ):
                        if ( 
mb_strtolower$choice['label'] ) === mb_strtolower$other ) ):
                            
$customChoice $choice;
                            break;
                        endif;
                    endforeach;

                    
// No matches? let's insert it then
                    
if ( $customChoice ):
                        
// Set visibility to true temporarily
                        
$this->poll->setChoice$customChoice['uid'], [ 'visibility' => true ], false );
                    else:
                        
// Insert the choice.
                        
$customChoice $this->poll->addChoice(
                            [
                                
'label'      => $other,
                                
'visibility' => $allowCustomChoice !== 'hidden',
                                
'via'        => 'other-field',
                            ],
                            
$questionUid
                        
);

                        
// Set a cookie to alter visibility for the current user
                        
if ( ! $customChoice['visibility'] ):
                            
$customChoice['visibilityCookie'] = true;
                            
$this->poll->setChoice$customChoice['uid'], [ 'visibility' => true ], false );
                        endif;
                    endif;

                    
// Add it to submitted choices.
                    
$userChoices$questionUid ][]         = $customChoice['uid'];
                    
$customChoices$customChoice['uid'] ] = $customChoice;
                endif;

                
// Allow zero vote

                
$minSelected Arrays::getDotNotation$question'settings.selection.minimum');
                
$maxSelected Arrays::getDotNotation$question'settings.selection.maximum');
                
$choices     = isset($userChoices[$questionUid]) ? (array) $userChoices$questionUid ] : [];

                if ( 
count$choices ) < $minSelected ):
                    
$message _n'You must vote for at least {{minimum}} choice.''You must vote for at least {{minimum}} choices.'$minSelected'totalpoll' );
                    throw new \
ErrorExceptionStrings::template$message, [ 'minimum' => $minSelected ] ) );
                elseif ( 
count$choices ) > $maxSelected ):
                    
$message _n'You can vote for up to {{maximum}} choice.''You can vote for up to {{maximum}} choices.'$maxSelected'totalpoll' );
                    throw new \
ErrorExceptionStrings::template$message, [ 'maximum' => $maxSelected ] ) );
                endif;

                
// Check choices
                
foreach ( $choices as $choiceUid ):
                    
$choice $this->poll->getChoice$choiceUid );
                    
// Choice doesn't exists or question uid is not correct,
                    
if ( ! $choice || $choice['questionUid'] !== $questionUid || ! $choice['visibility'] ) :
                        throw new \
ErrorException__'Unknown choice. Please try again.''totalpoll' ) );
                    endif;

                    
$this->poll->incrementChoiceVotes$choiceUid );
                    
$choicesUids[] = [ 'uid' => $choiceUid'votes' => ];
                endforeach;
            endforeach;

            if ( 
$this->repository->incrementVotes$this->poll->getId(), $choicesUids ) === false ):
                throw new \
ErrorException__'Something went wrong. Please try again.''totalpoll' ) );
            endif;

            if ( ! empty( 
$customChoices ) ):
                
// Set visibility cookie
                
foreach ( $customChoices as $customChoice ):
                    if ( ! empty( 
$customChoice['visibilityCookie'] ) ):
                        
$this->setCookie$this->poll->getPrefix$customChoice['uid'] ), true);
                    endif;
                endforeach;
                
// Save the changes.
                
$this->poll->save();
            endif;

            
/**
             * Fires after processing vote request successfully.
             *
             * @param array                           $choicesUids   Submitted choices UIDs and votes (uid => choiceUID, votes => castedVotes).
             * @param array                           $customChoices Custom choices submitted through form (other field).
             * @param \TotalPoll\Contracts\Poll\Model $poll          Poll model object.
             *
             * @since 4.0.0
             */
            
do_action'totalpoll/actions/after/poll/command/vote'$choicesUids$customChoices$this->poll );

            return 
true;
        } catch ( \
Exception $exception ) {
            foreach ( 
$customChoices as $choiceUid => $choice ):
                
$this->poll->removeChoice$choiceUidfalse );
            endforeach;

            return new \
WP_Error'vote'$exception->getMessage() );
        }
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586