C:\xampp\htdocs\landing\wp-content\plugins\totalpoll\modules\extensions\RestAPI\PollRestAPI.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php

namespace TotalPoll\Modules\Extensions\RestAPI;
defined'ABSPATH' ) && exit();



use 
TotalPoll\Contracts\Poll\Model;
use 
TotalPoll\Contracts\Poll\Repository;
use 
TotalPollVendors\TotalCore\Contracts\Http\Request;
use 
WP_Error;

/**
 * Poll Rest API.
 * @package TotalPoll\Poll
 */
class PollRestAPI {
    
/**
     * @var Request $request
     */
    
protected $request;
    
/**
     * @var Repository $repository
     */
    
protected $repository;
    
/**
     * @var array $env
     */
    
protected $env;
    
/**
     * @var Model $poll
     */
    
protected $poll;

    
/**
     * Rest constructor.
     *
     * @param Request    $request
     * @param Repository $repository
     * @param array      $env
     */
    
public function __constructRequest $requestRepository $repository$env ) {
        
$this->request    $request;
        
$this->repository $repository;
        
$this->env        $env;
    }

    
/**
     * Register routes.
     */
    
public function registerRoutes() {
        
register_rest_route(
            
$this->env['rest-namespace'], '/poll/(?P<id>\d+)',
            [
                
'methods'  => 'GET',
                
'callback' => [ $this'getPoll' ],
            ]
        );

        
register_rest_route(
            
$this->env['rest-namespace'], '/poll/(?P<id>\d+)/vote',
            [
                
'methods'  => 'POST',
                
'callback' => [ $this'postVote' ],
            ]
        );
    }

    
/**
     * Load poll.
     *
     * @param $pollId
     *
     * @throws \ErrorException
     */
    
public function loadPoll$pollId ) {
        
$status get_post_status( (int) $pollId );
        if ( 
$status === 'publish' ):
            
$this->poll $this->repository->getById( (int) $pollId );
        else:
            throw new \
ErrorException'Invalid Poll ID'404 );
        endif;
    }

    
/**
     * Get.
     *
     * @param \WP_REST_Request $restRequest
     *
     * @return array|WP_Error
     */
    
public function getPoll( \WP_REST_Request $restRequest ) {
        try {
            
$this->loadPoll( (int) $restRequest['id'] );
            
$pollArray $this->adjustPollArray$this->poll->toArray(), $this->poll->isResultsHidden(), true );

            
$pollArray['acceptingVotes'] = $this->poll->isAcceptingVotes();
            
$pollArray['hasVoted']       = $this->poll->hasVoted();

            return [
                
'code'    => 200,
                
'message' => '',
                
'data'    => [
                    
'poll'   => $pollArray,
                    
'status' => 200,
                ],
            ];

        } catch ( \
Exception $exception ) {
            return new 
WP_Error$exception->getCode(), $exception->getMessage(), [ 'status' => 404 ] );
        }
    }

    
/**
     * Vote.
     *
     * @param \WP_REST_Request $restRequest
     *
     * @return array|WP_Error
     */
    
public function postVote( \WP_REST_Request $restRequest ) {
        
$this->request['totalpoll'] = $restRequest->get_param'totalpoll' );

        try {
            
$this->loadPoll( (int) $restRequest['id'] );

            
$this->poll->setScreen'vote' );

            if ( 
$this->poll->isAcceptingVotes() ):
                if ( 
$this->poll->getForm()->validate() ):
                    
$countVote TotalPoll'polls.commands.vote.count', [ $this->poll ] )->execute();
                    
TotalPoll'polls.commands.vote.log', [ $this->poll ] )->execute();

                    if ( 
is_wp_error$countVote ) ):
                        throw new \
ErrorException$countVote->get_error_message(), 404 );
                    else:
                        
TotalPoll'polls.commands.vote.entry', [ $this->poll ] )->execute();
                        
TotalPoll'polls.commands.vote.notify', [ $this->poll ] )->execute();
                        
$this->poll->getRestrictions()->apply();
                        
$this->poll->setScreen'thankyou' );

                        return [
                            
'code'    => 200,
                            
'message' => 'Your vote has been casted successfully.',
                            
'data'    => [
                                
'poll'   => $this->adjustPollArray$this->poll->toArray(), $this->poll->isResultsHidden() ),
                                
'status' => 200,
                            ],
                        ];
                    endif;
                else:
                    throw new \
ErrorException'Fields validation failed.'403 );
                endif;
            else:
                throw new \
ErrorException'This poll does not accept new votes.'403 );
            endif;

        } catch ( \
Exception $exception ) {
            return new 
WP_Error$exception->getCode(), $exception->getMessage(), [
                
'status'     => $exception->getCode(),
                
'pollError'  => $this->poll->getError(),
                
'formErrors' => $this->poll->getForm()->errors(),
            ] );
        }
    }

    
/**
     * Adjust poll array for public rest response.
     *
     * @param array $pollArray
     * @param bool  $hideVotes
     * @param bool  $hideReceivedVotes
     *
     * @return array
     */
    
protected function adjustPollArray$pollArray$hideVotes false$hideReceivedVotes false ) {
        foreach ( 
$pollArray['questions'] as $questionUid => $question ):
            foreach ( 
$question['choices'] as $choiceUid => $choice ):
                if ( ! 
$choice['visibility'] ):
                    unset( 
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ] );
                    continue;
                endif;
                unset( 
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ]['votesOverride'] );
                unset( 
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ]['collapsed'] );
                unset( 
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ]['visibility'] );

                if ( 
$hideReceivedVotes ):
                    unset( 
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ]['receivedVotes'] );
                endif;

                if ( 
$hideVotes ):
                    
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ]['votes']       = 0;
                    
$pollArray['questions'][ $questionUid ]['choices'][ $choiceUid ]['votesHidden'] = true;
                endif;
            endforeach;

            if ( 
$hideVotes ):
                unset( 
$pollArray['questions'][ $questionUid ]['votes'] );
                
$pollArray['questions'][ $questionUid ]['votes']       = 0;
                
$pollArray['questions'][ $questionUid ]['votesHidden'] = true;
            endif;

            unset( 
$pollArray['questions'][ $questionUid ]['votesOverride'] );

            if ( 
$hideReceivedVotes ):
                unset( 
$pollArray['questions'][ $questionUid ]['receivedVotes'] );
            endif;
        endforeach;

        unset( 
$pollArray['receivedVotes'] );

        return 
$pollArray;
    }

}
x

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