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

namespace TotalPoll\Admin\Ajax;
defined'ABSPATH' ) && exit();


use 
TotalPoll\Contracts\Log\Repository as LogRepository;
use 
TotalPoll\Contracts\Poll\Model;
use 
TotalPoll\Contracts\Poll\Repository as PollRepository;
use 
TotalPollVendors\TotalCore\Contracts\Http\Request;

/**
 * Class Insights
 * @package TotalPoll\Admin\Ajax
 * @since   1.0.0
 */
class Insights {
    
/**
     * @var Request $request
     */
    
protected $request;
    
/**
     * @var LogRepository $log
     */
    
protected $log;
    
/**
     * @var PollRepository $poll
     */
    
protected $poll;
    
/**
     * @var array $criteria
     */
    
protected $criteria = [];

    
/**
     * Insights constructor.
     *
     * @param Request        $request
     * @param LogRepository  $log
     * @param PollRepository $poll
     */
    
public function __constructRequest $requestLogRepository $logPollRepository $poll ) {
        
$this->request $request;
        
$this->log     $log;
        
$this->poll    $poll;

        
$this->criteria = [
            
'poll'   => $this->request->request'poll'null ),
            
'from'   => $this->request->request'from'null ),
            
'to'     => $this->request->request'to'null ),
            
'format' => $this->request->request'format'null ),
        ];
    }

    
/**
     * Get metrics AJAX endpoint.
     * @action-callback wp_ajax_totalpoll_insights_metrics
     */
    
public function metrics() {
        
        
$args = [ 'conditions' => [ 'status' => 'accepted''action' => 'vote','date' => [] ] ];

        if ( 
$this->criteria['poll'] && current_user_can'edit_poll'$this->criteria['poll'] ) ):
            
$args['conditions']['poll_id'] = $this->criteria['poll'];
        endif;

        if ( 
$this->criteria['from'] && strptime$this->criteria['from'], '%Y-%m-%d 00:00:00' ) ):
            
$args['conditions']['date'][] = [ 'operator' => '>=''value' => $this->criteria['from'] ];
        endif;

        if ( 
$this->criteria['to'] && strptime$this->criteria['to'], '%Y-%m-%d 00:00:00' ) ):
            
$args['conditions']['date'][] = [ 'operator' => '<=''value' => $this->criteria['to'] ];
        endif;

        
$period $this->log->countVotesPerPeriod$args );

        
$platforms $this->log->countVotesPerUserAgent$args );

        unset( 
$args['conditions']['status'], $args['conditions']['date'], $args['conditions']['action'] );

        
$poll        $this->poll->getById$args['conditions']['poll_id'] );
        
$choicesUids array_keys$poll->getChoices() );
        if ( ! empty( 
$choicesUids ) ):
            
$args['conditions']['choice_uid'] = [ $choicesUids ];
        endif;
        
$choices $this->log->countVotesPerChoices$args );

        
$insights = [ 'period' => $period'choices' => $choices'platforms' => $platforms, ];

        
/**
         * Filters the data sent to insights browser.
         *
         * @param Model[] $polls    Array of poll models.
         * @param array   $criteria Array of criteria.
         *
         * @since 4.0.0
         * @return array
         */
        
$insights apply_filters'totalpoll/filters/admin/insights/metrics'$insights$this->criteria );

        
wp_send_json$insights );
        
    }

    
/**
     * Get polls AJAX endpoint.
     * @action-callback wp_ajax_totalpoll_insights_polls
     */
    
public function polls() {

        
$queryArgs = [
            
'status' => 'any',
            
'perPage' => -1,
        ];

        if ( ! 
current_user_can'edit_others_polls' ) ):
            
$queryArgs['wpQuery']['author'] = get_current_user_id();
        endif;

        
$polls array_map(
            function ( 
$poll ) {

                
/**
                 * Filters the poll object sent to insights.
                 *
                 * @param array $pollRepresentation The representation of a poll.
                 * @param Model $poll               Poll model object.
                 *
                 * @since 4.0.0
                 * @return array
                 */
                
return apply_filters'totalpoll/filters/admin/insights/poll', [ 'id' => $poll->getId(), 'title' => $poll->getTitle() ], $poll$this );
            },

            
TotalPoll('polls.repository')->get$queryArgs )
        );

        
/**
         * Filters the polls list sent to insights browser.
         *
         * @param Model[] $polls Array of poll models.
         *
         * @since 4.0.0
         * @return array
         */
        
$polls apply_filters'totalpoll/filters/admin/insights/polls'$polls );

        
wp_send_json$polls );
    }
}
x

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