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
|
<?php
namespace TotalPoll\Migrations\Polls\WPPolls; ! defined( 'ABSPATH' ) && exit();
use TotalPoll\Contracts\Migrations\Poll\Extract as ExtractContract; use TotalPoll\Contracts\Migrations\Poll\Template\Poll;
/** * Extract Polls. * @package TotalPoll\Migrations\Polls\WPPolls */ class Extract implements ExtractContract { /** * @var \wpdb $database */ protected $database; /** * @var array */ private $tables;
/** * Extract constructor. */ public function __construct() { $this->database = TotalPoll( 'database' ); $this->tables = [ 'questions' => "{$this->database->prefix}pollsq", 'answers' => "{$this->database->prefix}pollsa", 'log' => "{$this->database->prefix}pollsip", ]; }
/** * Count polls. * * @return int */ public function getCount() { $polls = $this->getPollsIds();
return ! empty( $polls ) ? count( array_diff( $polls, $this->getMigratedPollsIds() ) ) : 0; }
/** * @return array */ public function getMigratedPollsIds() { return (array) get_option( 'wp-polls_poll_migrated', [] ); }
/** * Get polls. * * @return array */ public function getPolls() { $pollsIds = array_slice( $this->getPollsIds(), 0, 5 ); $extractedPolls = [];
if ( ! empty( $pollsIds ) ):
$options = [ 'poll_bar' => get_option( 'poll_bar', false ), 'poll_ans_sortby' => get_option( 'poll_ans_sortby', false ), 'poll_ans_sortorder' => get_option( 'poll_ans_sortorder', false ), 'poll_ans_result_sortby' => get_option( 'poll_ans_result_sortby', false ), 'poll_ans_result_sortorder' => get_option( 'poll_ans_result_sortorder', false ), 'poll_allowtovote' => get_option( 'poll_allowtovote', false ), 'poll_cookielog_expiry' => get_option( 'poll_cookielog_expiry', false ), 'poll_logging_method' => get_option( 'poll_logging_method', false ), 'poll_bar_bg' => get_option( 'poll_bar_bg', false ), 'poll_bar_border' => get_option( 'poll_bar_border', false ), ];
foreach ( $pollsIds as $pollId ): $query = $this->database->prepare( "SELECT pollq_question AS question, pollq_timestamp AS start_date, pollq_expiry AS end_date, pollq_multiple AS multiple FROM {$this->tables['questions']} WHERE pollq_id = %d", $pollId ); $poll = $this->database->get_row( $query, ARRAY_A );
if ( ! $poll ): continue; endif;
$query = $this->database->prepare( "SELECT polla_answers AS label, polla_votes AS votes FROM {$this->tables['answers']} WHERE polla_qid = %d", $pollId ); $poll['choices'] = $this->database->get_results( $query, ARRAY_A ); $poll['id'] = $pollId; $poll['options'] = $options;
$extractedPolls[] = $poll; endforeach;
endif;
return $extractedPolls; }
/** * Get options. */ public function getOptions() { return []; }
/** * Get log entries. * * @param Poll $poll * * @return array */ public function getLogEntries( Poll $poll ) { $pollsIds = $this->getPollsIds(); $extractedLogEntries = []; $choicesMap = [];
foreach ( $poll['choices'] as $choice ): $choicesMap[ $choice['uid'] ] = $choice['label']; endforeach;
if ( ! empty( $pollsIds ) ):
foreach ( $pollsIds as $pollId ): $query = $this->database->prepare( "SELECT polla_answers AS choices, FROM_UNIXTIME(pollip_timestamp) AS date, pollip_userid AS user_id, pollip_ip AS ip FROM {$this->tables['log']} INNER JOIN {$this->tables['answers']} ON polla_aid = pollip_aid WHERE pollip_qid = %d", $pollId ); $logEntry = $this->database->get_row( $query, ARRAY_A );
if ( ! $logEntry ): continue; endif;
$choices = []; foreach ( (array) $logEntry['choices'] as $choice ): $choiceUid = array_search( $choice, $choicesMap ); if ( $choiceUid ): $choices[] = $choiceUid; endif; endforeach;
$extractedLogEntries[] = [ 'ip' => $logEntry['ip'], 'useragent' => '', 'user_id' => empty( $logEntry['user_id'] ) ? 0 : (int) $logEntry['user_id'], 'poll_id' => $poll->getNewId(), 'choices' => $choices, 'action' => 'vote', 'status' => 'accepted', 'details' => [ 'choices' => (array) $logEntry['choices'], ], 'date' => TotalPoll( 'datetime', [ $logEntry['date'] ] ), ];
endforeach;
endif;
return $extractedLogEntries; }
/** * @param Poll $poll * * @return array */ public function getSubmissions( Poll $poll ) { return []; }
/** * Get polls ids array. * * @return array */ private function getPollsIds() { $tableName = "{$this->database->prefix}pollsq";
if ( $this->database->get_var( "SHOW TABLES LIKE '{$this->tables['questions']}'" ) == $tableName ) : $ids = $this->database->get_col( "SELECT pollq_id FROM {$this->tables['questions']}" );
return array_diff( $ids, $this->getMigratedPollsIds() ); endif;
return []; } }
|