C:\xampp\htdocs\landing\wp-content\plugins\totalpoll\src\Entry\Repository.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
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
<?php

namespace TotalPoll\Entry;
defined'ABSPATH' ) && exit();


use 
TotalPoll\Contracts\Entry\Repository as RepositoryContract;
use 
TotalPollVendors\TotalCore\Contracts\Http\Request;
use 
TotalPollVendors\TotalCore\Helpers\Arrays;
use 
TotalPollVendors\TotalCore\Helpers\Sql;

/**
 * Entry Repository.
 *
 * @package TotalPoll\Entry
 * @since   1.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         $env
     */
    
public function __constructRequest $request, \wpdb $database$env ) {
        
$this->request  $request;
        
$this->database $database;
        
$this->env      $env;
    }

    
/**
     * Get entries.
     *
     * @param $query
     *
     * @return mixed
     * @since 1.0.0
     */
    
public function get$query ) {
        
$args Arrays::parse$query, [
            
'conditions'     => [],
            
'page'           => 1,
            
'perPage'        => 10,
            
'orderBy'        => 'date',
            
'orderDirection' => 'DESC',
        ] );
        
// Models
        
$entryModels = [];

        
/**
         * Filters the list of arguments used for get entries query.
         *
         * @param array $args  Arguments.
         * @param array $query Query.
         *
         * @return array
         * @since 4.0.0
         */
        
$args apply_filters'totalpoll/filters/entries/get/query'$args$query );
        
// Where clause
        
$where Sql::generateWhereClause$args['conditions'] );
        
// Order
        
$order Sql::generateOrderClause$args['orderBy'], $args['orderDirection'] );
        
// Limit clause
        
$limit $args['perPage'] === - '' Sql::generateLimitClause$args['page'], $args['perPage'] );

        
// Finally our fancy SQL query
        
$query "SELECT * FROM `{$this->env['db']['tables']['entries']}{$where} {$order} {$limit}";

        
// Get results
        
$entries = (array) $this->database->get_results$queryARRAY_A );
        
// Iterate and convert each row to entry model
        
foreach ( $entries as $entry ):
            
$entryModels[] = new Model$entry );
        endforeach;

        
/**
         * Filters the results of get entries query.
         *
         * @param \TotalPoll\Contracts\Entry\Model[] $entryModels Entries models.
         * @param array                              $args        Arguments.
         * @param array                              $query       Query.
         *
         * @return array
         * @since 4.0.0
         */
        
$entryModels apply_filters'totalpoll/filters/entries/get/results'$entryModels$args$query );

        
// Return models
        
return $entryModels;
    }

    
/**
     * Get entry by id.
     *
     * @param $entryId
     *
     * @return \TotalPoll\Contracts\Entry\Model|null
     * @since 4.0.8
     */
    
public function getById$entryId ) {
        
$result $this->get( [ 'conditions' => [ 'id' => (int) $entryId ] ] );

        return empty( 
$result ) ? null $result[0];
    }

    
/**
     * Get entry by id.
     *
     * @param $logId
     *
     * @return \TotalPoll\Contracts\Entry\Model|null
     * @since 4.0.8
     */
    
public function getByLogId$logId ) {
        
$result $this->get( [ 'conditions' => [ 'log_id' => (int) $logId ] ] );

        return empty( 
$result ) ? null $result[0];
    }

    
/**
     * Create an entry.
     *
     * @param $attributes
     *
     * @return \TotalPoll\Contracts\Entry\Model|\WP_Error
     * @since 1.0.0
     */
    
public function create$attributes ) {

        
$attributes Arrays::parse(
            
$attributes,
            [
                
'user_id' => get_current_user_id(),
                
'date'    => TotalPoll'datetime' ),
                
'fields'  => [],
                
'details' => [],
            ]
        );

        
/**
         * Filters the attributes of an entry model used for insertion.
         *
         * @param array $attributes Entry attributes.
         *
         * @return array
         * @since 4.0.0
         */
        
$attributes apply_filters'totalpoll/filters/entries/insert/attributes'$attributes );

        if ( empty( 
$attributes['poll_id'] ) || empty( $attributes['fields'] ) ):
            return new \
WP_Error'missing_fields'__'poll_id and fields are required' ) );
        endif;

        if ( ! ( 
$attributes['date'] instanceof \DateTime ) ):
            return new \
WP_Error'missing_fields'__'date must be a DateTime object' ) );
        endif;

        
$entryModelAttributes = [
            
'date'    => $attributes['date']->format'Y-m-d H:i:s' ),
            
'user_id' => (int) $attributes['user_id'],
            
'poll_id' => (int) $attributes['poll_id'],
            
'log_id'  => $attributes['log_id'],
            
'fields'  => json_encode( (array) $attributes['fields'] ),
            
'details' => json_encode( (array) $attributes['details'] ),
        ];

        
$inserted $this->database->insert$this->env['db']['tables']['entries'], $entryModelAttributes );

        if ( ! 
$inserted ):
            return new \
WP_Error'insert_fail'__'Unable to insert the entry.''totalpoll' ) );
        endif;

        
$entryModelAttributes['id'] = $this->database->insert_id;


        
/**
         * Filters the entry model attributes after insertion.
         *
         * @param array $entryModelAttributes Entry attributes.
         * @param array $attributes           Original insertion attributes.
         *
         * @return array
         * @since 4.0.0
         */
        
$entryModelAttributes apply_filters'totalpoll/filters/entries/insert/model'$entryModelAttributes$attributes );

        return new 
Model$entryModelAttributes );
    }

    
/**
     * Delete entries.
     *
     * @param $query array
     *
     * @return bool|\WP_Error
     * @since 1.0.0
     */
    
public function delete$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']['entries']}{$where}";

        return (bool) 
$this->database->query$query );

    }

    
/**
     * Count entries.
     *
     * @param $query
     *
     * @return mixed
     * @since 1.0.0
     */
    
public function count$query ) {
        
$args Arrays::parse$query, [ 'conditions' => [], ] );

        
/**
         * Filters the list of arguments used for count entries query.
         *
         * @param array $args  Arguments.
         * @param array $query Query.
         *
         * @return array
         * @since 4.0.0
         */
        
$args apply_filters'totalpoll/filters/entries/count/query'$args$query );

        
// Where clause
        
$where Sql::generateWhereClause$args['conditions'] );
        
// Finally our fancy SQL query
        
$query "SELECT COUNT(*) FROM `{$this->env['db']['tables']['entries']}{$where}";

        
// Get count
        
return (int) $this->database->get_var$query );
    }

    
/**
     * Anonymize entries.
     *
     * @param $query
     *
     * @return mixed
     * @since 1.0.0
     */
    
public function anonymize$query ) {
        
$args Arrays::parse$query, [
            
'conditions' => [],
        ] );

        
/**
         * Filters the list of arguments used for anonymize entries query.
         *
         * @param array $args  Arguments.
         * @param array $query Query.
         *
         * @return array
         * @since 4.0.0
         */
        
$args apply_filters'totalpoll/filters/entries/anonymize/query'$args$query );

        
// Where clause
        
$where Sql::generateWhereClause$args['conditions'] );

        if ( empty( 
$where ) ):
            return new \
WP_Error'no_conditions'__'No conditions were specified''totalpoll' ) );
        endif;

        
// Finally our fancy SQL query
        
$query "UPDATE `{$this->env['db']['tables']['entries']}` SET `user_id` = 0, `log_id` = 0, `fields` = '[]', `details` = '{\"anonymized\":true}' {$where}";

        
// Get results
        
return (bool) $this->database->query$query );

    }
}
x

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