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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
<?php
namespace TotalPoll\Poll; ! defined( 'ABSPATH' ) && exit();
use TotalPoll\Contracts\Poll\Model as ModelContract; use TotalPoll\Contracts\Poll\Repository as RepositoryContract; use TotalPollVendors\TotalCore\Helpers\Arrays; use TotalPollVendors\TotalCore\Helpers\Sql; use TotalPollVendors\TotalCore\Http\Request;
/** * Poll repository. * @package TotalPoll\Poll * @since 4.0.0 */ class Repository implements RepositoryContract { /** * @var Request $request */ protected $request; /** * @var \wpdb $database */ protected $database; /** * @var array $env */ protected $env;
/** * Repository constructor. * * @param Request $request * @param \wpdb $database * @param array $env */ public function __construct( Request $request, \wpdb $database, $env ) { $this->request = $request; $this->database = $database; $this->env = $env; }
/** * Get polls. * * @param $query * * @return ModelContract[] */ public function get( $query ) { $args = Arrays::parse( $query, [ 'page' => 1, 'perPage' => 10, 'orderBy' => 'date', 'orderDirection' => 'DESC', 'status' => 'publish', 'wpQuery' => [], ] );
// Models $pollModels = []; // Query $wpQueryArgs = Arrays::parse( [ 'post_type' => TP_POLL_CPT_NAME, 'post_status' => $args['status'], 'paged' => $args['page'], 'posts_per_page' => $args['perPage'], 'order' => $args['orderDirection'], 'orderby' => $args['orderBy'], ], $args['wpQuery'] );
/** * Filters the list of arguments used for get polls query. * * @param array $wpQueryArgs WP_Query arguments. * @param array $args Arguments. * @param array $query Query. * * @return array * @since 4.0.0 */ $wpQueryArgs = apply_filters( 'totalpoll/filters/polls/get/query', $wpQueryArgs, $args, $query );
$wpQuery = new \WP_Query( $wpQueryArgs );
// Iterate and convert each row to log model foreach ( $wpQuery->get_posts() as $pollPost ): $pollModels[] = $this->getById( $pollPost ); endforeach;
/** * Filters the results of get polls query. * * @param \TotalPoll\Contracts\Poll\Model[] $pollModels Polls models. * @param array $wpQueryArgs WP_Query arguments. * @param array $args Arguments. * @param array $query Query. * * @return array * @since 4.0.0 */ $pollModels = apply_filters( 'totalpoll/filters/polls/get/results', $pollModels, $wpQueryArgs, $args, $query );
// Return models return $pollModels; }
/** * Get poll. * * @param $pollId * * @return null|ModelContract * @since 4.0.0 */ public function getById( $pollId ) { $attributes = []; // Post if ( $pollId instanceof \WP_Post ): $attributes['post'] = $pollId; else: $attributes['post'] = get_post( $pollId ); if ( ! $attributes['post'] ): return null; endif; endif;
$attributes['id'] = $attributes['post']->ID; $attributes['action'] = $this->request->request( 'totalpoll.action' ); $attributes['ip'] = $this->request->ip(); $attributes['currentPage'] = (int) get_query_var( 'current_page', (int) get_query_var( 'paged', 1 ) ); if ( empty( $attributes['currentPage'] ) ): $attributes['currentPage'] = $this->request->request( 'totalpoll.page', 1 ); endif;
$container = TotalPoll()->container();
if ( ! $container->has( "poll.instances.{$attributes['id']}" ) ): $attributes['votes'] = $this->getVotes( $attributes['id'] );
/** * Filters the poll model attributes after retrieving. * * @param array $attributes Entry attributes. * * @return array * @since 4.0.0 */ $attributes = apply_filters( 'totalpoll/filters/polls/get/attributes', $attributes ); $pollModel = new Model( $attributes );
/** * Filters the poll model after creation and before adding it to container. * * @param \TotalPoll\Contracts\Poll\Model $model Poll model object. * @param array $attributes Poll attributes. * * @return array * @since 4.0.0 */ $pollModel = apply_filters( 'totalpoll/filters/polls/get/model', $pollModel, $attributes );
$container->share( "poll.instances.{$attributes['id']}", $pollModel ); endif;
return $container->get( "poll.instances.{$attributes['id']}" ); }
/** * Get choice(s) votes. * * @param int $pollId * @param string|null $choiceUid * * @return mixed * @since 4.0.0 */ public function getVotes( $pollId, $choiceUid = null ) { $votes = [];
// Where clause $where = [ 'poll_id' => (int) $pollId ]; if ( ! empty( $choiceUid ) ): $where['choice_uid'] = [ $choiceUid ]; endif;
/** * Filters the list of where clauses used in votes counting query. * * @param array $args Arguments. * @param array $query Query. * * @return array * @since 4.0.0 */ $where = apply_filters( 'totalpoll/filters/polls/votes/query', $where ); $where = Sql::generateWhereClause( $where );
// Query $query = "SELECT choice_uid, votes FROM `{$this->env['db']['tables']['votes']}` {$where}";
// Results $rows = $this->database->get_results( $query, ARRAY_A ); foreach ( $rows as $row ): $votes[ $row['choice_uid'] ] = (int) $row['votes']; endforeach;
/** * Filters the results of votes counting query. * * @param array $votes Array of choices [choiceUid => votes]. * @param array $args Arguments. * @param array $query Query. * * @return array * @since 4.0.0 */ $votes = apply_filters( 'totalpoll/filters/polls/votes/results', $votes );
return $votes; }
/** * Increment choice(s) votes. * * @param int $pollId * @param array $choices * * @return false|int * @since 4.0.0 */ public function incrementVotes( $pollId, $choices ) { if(empty($choices)) { return true; } // Preparing SQL statement (INSERT INTO ... ON DUPLICATE KEY UPDATE) $query = "INSERT INTO %s (choice_uid, poll_id, votes, last_vote_at) VALUES %s ON DUPLICATE KEY UPDATE votes = (votes+(CASE choice_uid %s END)), last_vote_at = CURRENT_TIME()"; $values = []; $votes = []; foreach ( (array) $choices as $choice ): $values[] = $this->database->prepare( '(%s, %d, %d, CURRENT_TIME())', (string) $choice['uid'], (int) $pollId, absint( $choice['votes'] ) ); $votes[] = $this->database->prepare( 'WHEN %s THEN %d', (string) $choice['uid'], absint( $choice['votes'] ) ); endforeach; // Replace placeholders $query = sprintf( $query, $this->env['db']['tables']['votes'], implode( ', ', $values ), implode( ' ', $votes ) );
// Execute return $this->database->query( $query ); }
/** * Set choice(s) votes. * * @param int $pollId * @param array $choicesUidsVotes * * @return false|int * @since 4.0.0 */ public function setVotes( $pollId, $choicesUidsVotes ) { if ( empty( $pollId ) || empty( $choicesUidsVotes ) ): return false; endif;
// Preparing SQL statement (INSERT INTO ... ON DUPLICATE KEY UPDATE) $query = "INSERT INTO %s (choice_uid, poll_id, votes, last_vote_at) VALUES %s ON DUPLICATE KEY UPDATE votes = (CASE choice_uid %s END), last_vote_at = CURRENT_TIME()"; $values = []; $votes = []; foreach ( (array) $choicesUidsVotes as $choiceUid => $choiceVotes ): $values[] = $this->database->prepare( '(%s, %d, %d, CURRENT_TIME())', (string) $choiceUid, (int) $pollId, (int) $choiceVotes ); $votes[] = $this->database->prepare( 'WHEN %s THEN %d', (string) $choiceUid, (int) $choiceVotes ); endforeach; // Replace placeholders $query = sprintf( $query, $this->env['db']['tables']['votes'], implode( ', ', $values ), implode( ' ', $votes ) );
// Execute return $this->database->query( $query ); }
/** * Delete votes. * * @param $query array * * @return bool|\WP_Error * @since 4.0.5 */ public function deleteVotes( $query ) { $where = Sql::generateWhereClause( $query );
if ( empty( $where ) ): return new \WP_Error( 'no_conditions', __( 'No conditions were specified', 'totalpoll' ) ); endif;
$query = "DELETE FROM `{$this->env['db']['tables']['votes']}` {$where}";
return (bool) $this->database->query( $query );
} }
|