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

namespace TotalPoll\Poll\Commands;
defined'ABSPATH' ) && exit();


use 
TotalPoll\Contracts\Poll\Model;
use 
TotalPoll\Notification\Mail;
use 
TotalPoll\Notification\Push;
use 
TotalPoll\Notification\WebHook;
use 
TotalPollVendors\TotalCore\Helpers\Strings;

/**
 * Class SendNotifications
 * @package TotalPoll\Poll\Commands
 */
class SendNotifications extends \TotalPollVendors\TotalCore\Helpers\Command {
    
/**
     * @var Model $poll
     */
    
protected $poll;
    
/**
     * @var \TotalPoll\Contracts\Log\Model $log
     */
    
protected $log;
    
/**
     * @var \TotalPoll\Contracts\Entry\Model $entry
     */
    
protected $entry;
    
/**
     * @var array $templateVars
     */
    
protected $templateVars;

    
/**
     * SendNotifications constructor.
     *
     * @param Model $poll
     */
    
public function __constructModel $poll ) {
        
$this->poll         $poll;
        
$this->log          = static::getShared'log'null );
        
$this->entry        = static::getShared'entry'null );

        
$labels = [];

        foreach(
$this->poll->getFields() as $field) {
            
$labels[$field['name']] = empty($field['label']) ? $field['name'] : $field['label'];
        }

        
$this->templateVars = [
            
'title'      => $this->poll->getTitle(),
            
'ip'         => $this->log->getIp(),
            
'browser'    => $this->log->getUseragent(),
            
'user'       => $this->log->getId() ? $this->log->getUser()->user_login 'anonymous',
            
'date'       => $this->log->getDate(),
            
'poll'       => $this->poll->toArray(),
            
'fields'     => '',
            
'choices'    => implode' & 'array_map(
                function ( 
$choice ) {
                    return 
$choice['label'];
                },
                
$this->poll->getReceivedChoices()
            ) ),
            
'log'        => $this->log->toArray(),
            
'deactivate' => esc_urladmin_url"post.php?post={$this->poll->getId()}&action=edit&tab=editor>settings>general>notifications" ) ),
        ];

        if(
$this->entry) {
            
$this->templateVars['fields'] = implodePHP_EOLarray_map(
                function ( 
$key ) use ($labels) {
                    
$label = isset($labels[$key]) ? $labels[$key] : ucfirst($key);
                    return 
$label ' : ' implode(' , ', (array) $this->entry->getField($key));
                },
                
array_keys($this->entry->getFields())
            ) );
        }

        
$this->templateVars['fieldsHTML'] = nl2br($this->templateVars['fields']);
    }

    protected function 
handle() {
        
$email   $this->poll->getSettingsItem'notifications.email', [] );
        
$push    $this->poll->getSettingsItem'notifications.push', [] );
        
$webhook $this->poll->getSettingsItem'notifications.webhook', [] );

        
/**
         * Fires before sending notifications.
         *
         * @param Model $poll     WebHook settings.
         * @param array $settings Notifications settings.
         * @param array $log      Log entry.
         *
         * @since 4.0.0
         */
        
do_action'totalpoll/actions/before/poll/command/notify'$this->poll, [
            
'email'   => $email,
            
'push'    => $push,
            
'webhook' => $webhook,
        ], 
$this->log->toArray() );

        if ( ! empty( 
$email['on']['newVote'] ) && ! empty( $email['recipient'] ) ):
            
$this->sendEmail$email['recipient'], $this->getTitle(), $this->getTemplate() );
        endif;

        if ( ! empty( 
$push['on']['newVote'] ) && ! empty( $push['appId'] ) && ! empty( $push['apiKey'] ) ):
            
$this->sendPush( [ 'All' ], $this->getTitle(), $this->getBody(), [ 'appId' => $push['appId'], 'apiKey' => $push['apiKey'] ] );
        endif;

        if ( ! empty( 
$webhook['on']['newVote'] ) && ! empty( $webhook['url'] ) ):
            
$this->sendWebhook(
                
$webhook['url'],
                
array_merge(
                    
$this->poll->toArray(), [
                    
'form'  => $this->poll->getForm()->toArray(),
                    
'log'   => $this->log $this->log->toArray() : [],
                    
'entry' => $this->entry $this->entry->toArray() : [],
                ] )
            );
        endif;

        
/**
         * Fires after sending notifications.
         *
         * @param \TotalPoll\Contracts\Poll\Model $poll Poll model object.
         *
         * @since 4.0.0
         */
        
do_action'totalpoll/actions/after/poll/command/notify'$this->poll );

        return 
true;
    }

    
/**
     * @param $recipient
     *
     * @return bool|\WP_Error
     */
    
private function sendEmail$recipient$subject$body ) {
        try {
            
$notification = new Mail();
            
$notification->setTo$recipient )
                         ->
setSubject$subject )
                         ->
setBody$body )
                         ->
send();

            return 
true;
        } catch ( \
Exception $e ) {
            return new \
WP_Error$e->getCode(), $e->getMessage() );
        }
    }

    private function 
sendPush$recipient$title$message$args = [] ) {
        try {
            
$notification = new Push();
            
$notification->setTo$recipient )
                         ->
setSubject$title )
                         ->
setBody$message )
                         ->
setArgs$args )
                         ->
send();

            return 
true;
        } catch ( \
Exception $e ) {
            return new \
WP_Error$e->getCode(), $e->getMessage() );
        }
    }

    private function 
sendWebhook$url$payload$userAgent 'TotalPoll Notification' ) {
        try {
            
$notification = new WebHook();
            
$notification->setTo$url )
                         ->
setFrom$userAgent )
                         ->
setBody$payload )
                         ->
send();

            return 
true;
        } catch ( \
Exception $e ) {
            return new \
WP_Error$e->getCode(), $e->getMessage() );
        }
    }

    
/**
     * @return string
     */
    
private function getTitle() {
        
$template TotalPoll()->option'notifications.title' ) ?: 'New vote on {{poll.title}}';

        return 
Strings::template$template$this->templateVars );
    }

    
/**
     * @return string
     */
    
private function getBody() {
        
$template TotalPoll()->option'notifications.body' ) ?: 'Someone just voted for {{choices}} on {{poll.title}}';

        return 
Strings::template$template$this->templateVars );
    }

    
/**
     * @return string
     */
    
private function getTemplate() {
        
$template TotalPoll()->option'notifications.template' ) ?: file_get_contents__DIR__ '/views/notifications/new-vote.php' );

        return 
Strings::template$template$this->templateVars );
    }
}
x

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