/var/www/html_uk/wp-content/plugins/automatewoo/includes/Log.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

use 
AutomateWoo\DataTypes\DataTypes;
use 
AutomateWoo\Workflows\Factory;

if ( ! 
defined'ABSPATH' ) ) exit;

/**
 * @class Log
 */
class Log extends Abstract_Model_With_Meta_Table {

    
/** @var string */
    
public $table_id 'logs';

    
/** @var string  */
    
public $object_type 'log';

    
/** @var Data_Layer */
    
private $data_layer;

    
/**
     * Returns the ID of the model's meta table.
     *
     * @return string
     */
    
public function get_meta_table_id() {
        return 
'log-meta';
    }

    
/**
     * @param bool|int $id
     */
    
function __construct$id false ) {
        if ( 
$id $this->get_by'id'$id );
    }


    
/**
     * @param int $workflow_id
     */
    
function set_workflow_id$workflow_id ) {
        
$this->set_prop'workflow_id'Clean::id$workflow_id ) );
    }


    
/**
     * @return int
     */
    
function get_workflow_id() {
        return 
Clean::id$this->get_prop'workflow_id' ) );
    }


    
/**
     * @param DateTime $date
     */
    
function set_date$date ) {
        
$this->set_date_column'date'$date );
    }


    
/**
     * @return DateTime|bool
     */
    
function get_date() {
        return 
$this->get_date_column'date' );
    }


    
/**
     * @param bool $has_errors
     */
    
function set_has_errors$has_errors ) {
        
$this->set_prop'has_errors'aw_bool_int$has_errors ) );
    }


    
/**
     * @return bool
     */
    
function has_errors() {
        return (bool) 
$this->get_prop'has_errors' );
    }


    
/**
     * @param bool $has_blocked_emails
     */
    
function set_has_blocked_emails$has_blocked_emails ) {
        
$this->set_prop'has_blocked_emails'absint( (bool) $has_blocked_emails ) );
    }


    
/**
     * @return bool
     */
    
function has_blocked_emails() {
        return (bool) 
$this->get_prop'has_blocked_emails' );
    }


    
/**
     * @param bool $enabled
     */
    
function set_tracking_enabled$enabled ) {
        
$this->set_prop'tracking_enabled'aw_bool_int$enabled ) );
    }


    
/**
     * @return bool
     */
    
function is_tracking_enabled() {
        return (bool) 
$this->get_prop'tracking_enabled' );
    }


    
/**
     * @param bool $enabled
     */
    
function set_conversion_tracking_enabled$enabled ) {
        
$this->set_prop'conversion_tracking_enabled'aw_bool_int$enabled ) );
    }


    
/**
     * @return bool
     */
    
function is_conversion_tracking_enabled() {
        return (bool) 
$this->get_prop'conversion_tracking_enabled' );
    }


    
/**
     * @return bool
     */
    
function is_anonymized() {
        return (bool) 
$this->get_meta'is_anonymized' );
    }


    
/**
     * Records that a trackable message from a workflow was clicked.
     *
     * @param string $url
     */
    
public function record_click$url ) {

        if ( ! 
$tracking $this->get_meta('tracking_data') ) {
            
$tracking = [];
        }

        
$tracking[] = [
            
'type' => 'click',
            
'url' => $url,
            
'date' => current_time'mysql'true )
        ];

        
// clicking requires an open so record one in case images were blocked
        
if ( ! $this->has_open_recorded() ) {
            
$tracking[] = [
                
'type' => 'open',
                
'date' => current_time'mysql'true )
            ];
        }

        
$this->update_meta'tracking_data'$tracking );

        
/**
         * Fires when a link in a trackable workflow message is clicked.
         *
         * @param Workflow $workflow The workflow that sent the message.
         * @param Log      $log      The record of the workflow running.
         * @param string   $url      The URL that was clicked
         */
        
do_action'automatewoo/workflow/record_click'$this->get_workflow(), $this$url );
    }


    
/**
     * Records that a trackable message from a workflow was opened.
     *
     * Only records an open once i.e. unique opens.
     */
    
public function record_open() {

        if ( 
$this->has_open_recorded() ) {
            return; 
// already opened
        
}

        if ( ! 
$tracking $this->get_meta('tracking_data') ) {
            
$tracking = [];
        }

        
$tracking[] = [
            
'type' => 'open',
            
'date' => current_time'mysql'true )
        ];

        
$this->update_meta'tracking_data'$tracking );

        
/**
         * Fires for unique opens of a trackable workflow message.
         *
         * @param Workflow $workflow The workflow that sent the message.
         * @param Log      $log      The record of the workflow running.
         */
        
do_action'automatewoo/workflow/record_open'$this->get_workflow(), $this );
    }


    
/**
     * @return bool
     */
    
function has_open_recorded() {

        
$tracking $this->get_meta('tracking_data');

        if ( 
is_array$tracking ) ) foreach( $tracking as $item ) {
            if ( 
$item['type'] == 'open') {
                return 
true;
            }
        }

        return 
false;
    }


    
/**
     * @return bool
     */
    
function has_click_recorded() {

        
$tracking $this->get_meta('tracking_data');

        if ( 
is_array$tracking ) ) foreach( $tracking as $item ) {
            if ( 
$item['type'] == 'click')
                return 
true;
        }

        return 
false;
    }


    
/**
     * @return DateTime|false
     */
    
function get_date_opened() {
        
$tracking $this->get_meta('tracking_data');

        if ( 
is_array$tracking ) ) foreach( $tracking as $item ) {
            if ( 
$item['type'] == 'open') {
                return new 
DateTime$item['date'] );
            }
        }

        return 
false;
    }


    
/**
     * @return DateTime|false
     */
    
function get_date_clicked() {
        
$tracking $this->get_meta('tracking_data');

        if ( 
is_array$tracking ) ) foreach( $tracking as $item ) {
            if ( 
$item['type'] == 'click') {
                return new 
DateTime$item['date'] );
            }
        }

        return 
false;
    }

    
/**
     * Add a note.
     *
     * @param string $note
     */
    
function add_note$note ) {
        
$notes $this->get_notes();
        
$notes[] = $note;
        
$this->update_notes$notes );
    }

    
/**
     * Get the log notes.
     *
     * @since 4.4.0
     *
     * @return array
     */
    
function get_notes() {
        
$notes $this->get_meta'notes' );
        if ( ! 
is_array$notes ) ) {
            return [];
        }
        else {
            return 
Clean::recursive$notes );
        }
    }

    
/**
     * Update the log notes.
     *
     * @since 4.4.0
     *
     * @param array $notes
     */
    
function update_notes$notes ) {
        
$notes Clean::recursive$notes );
        
$this->update_meta'notes'$notes );
    }


    
/**
     * Returns the workflow without a data layer
     * @return Workflow
     */
    
function get_workflow() {
        return 
Factory::get$this->get_workflow_id() );
    }


    
/**
     * @param string $output - array|object this for backwards compatibility
     * @return Data_Layer|array
     */
    
function get_data_layer$output 'array' ) {

        if ( ! isset( 
$this->data_layer ) ) {
            if ( 
$compressed $this->get_compressed_data_layer() ) {
                
$this->data_layer $this->decompress_data_layer$compressed );
            }
            else {
                
$this->data_layer = new Data_Layer();
            }
        }

        if ( 
$output == 'array' ) {
            return 
$this->data_layer->get_raw_data();
        }

        return 
$this->data_layer;
    }


    
/**
     * Fetches the data layer from log meta, but does not decompress
     * Uses the the supplied_data_items field on the workflows trigger
     *
     * @return array|false
     */
    
private function get_compressed_data_layer() {

        if ( ! 
$workflow $this->get_workflow() )
            return 
false// workflow must be set

        
if ( ! $this->exists )
            return 
false// log must be saved

        
if ( ! $trigger $workflow->get_trigger() )
            return 
false// need a trigger

        
$data_layer = [];
        
$supplied_items $trigger->get_supplied_data_items();

        
// when anonymized log is converted to guest
        
if ( $this->is_anonymized() ) {
            
$supplied_items[] = 'guest';
        }

        foreach ( 
$supplied_items as $data_type_id ) {

            
$data_item_value $this->get_compressed_data_item$data_type_id$trigger->get_supplied_data_items() );

            if ( 
$data_item_value !== false ) {
                
$data_layer$data_type_id ] = $data_item_value;
            }
        }

        return 
$data_layer;
    }


    
/**
     * @param $data_type_id
     * @param array $supplied_data_items
     * @return string|false
     */
    
private function get_compressed_data_item$data_type_id$supplied_data_items ) {
        if ( 
DataTypes::is_non_stored_data_type$data_type_id ) ) {
            return 
false// storage not required
        
}

        
// user requires special logic when related to an order
        
if ( $data_type_id === 'user' && in_array'order'$supplied_data_items ) ) {
            return 
0// get user data from the order when decompressing
        
}

        
$storage_key Logs::get_data_layer_storage_key$data_type_id );

        if ( ! 
$storage_key )
            return 
false;

        return 
Clean::string$this->get_meta$storage_key ) );
    }


    
/**
     * @param array $compressed_data_layer
     * @return Data_Layer
     */
    
private function decompress_data_layer$compressed_data_layer ) {

        
$data = [];

        if ( 
is_array$compressed_data_layer ) ) foreach ( $compressed_data_layer as $data_type_id => $compressed_item ) {
            if ( 
$data_type DataTypes::get$data_type_id ) ) {
                
$data[$data_type_id] = $data_type->decompress$compressed_item$compressed_data_layer );
            }
        }

        return new 
Data_Layer$data );
    }


    
/**
     * Stores a data layer in log meta
     * @param Data_Layer $data_layer
     */
    
function store_data_layer$data_layer ) {

        if ( ! 
$this->exists )
            return; 
// log must be saved before meta can be added

        
foreach ( $data_layer->get_raw_data() as $data_type_id => $data_item ) {
            
$this->store_data_item$data_type_id$data_item );
        }
    }


    
/**
     * @param $data_type_id
     * @param $data_item
     */
    
private function store_data_item$data_type_id$data_item ) {
        
$data_type DataTypes::get$data_type_id );

        if (
            ! 
$data_type ||
            ! 
$data_type->validate$data_item ) ||
            
DataTypes::is_non_stored_data_type$data_type_id )
        ) {
            return;
        }

        
// special logic for users who are actually guests
        
if ( $data_type_id === 'user' && $data_item->ID === ) {
            
$storage_key 'guest_email';
            
$storage_value $data_item->user_email;
        }
        else {
            
$storage_key Logs::get_data_layer_storage_key$data_type_id );
            
$storage_value Logs::get_data_layer_storage_value$data_type_id$data_item );
        }

        if ( 
$storage_key ) {
            
$this->update_meta$storage_key$storage_value );
        }
    }


    
/**
     * Delete the log and clear related conversion order meta
     */
    
function delete() {

        
// Get orders with conversion data.
        
$converted_orders wc_get_orders(
            [
                
'type'       => 'shop_order',
                
'status'     => 'any',
                
'limit'      => -1,
                
'meta_key'   => '_aw_conversion_log',
                
'meta_value' => $this->get_id(),
            ]
        );

        if ( 
$converted_orders ) {
            foreach ( 
$converted_orders as $order ) {
                
$order->delete_meta_data'_aw_conversion' );
                
$order->delete_meta_data'_aw_conversion_log' );
                
$order->save();
            }
        }

        
parent::delete();
    }

    function 
clear_cached_data() {

        if ( ! 
$this->get_workflow_id() )
            return;

        
Cache::delete_transient'times_run/workflow=' $this->get_workflow_id() );
    }


    
/**
     * Reruns the workflow skipping validation
     * @return Log|bool - the newly created log
     */
    
function rerun() {
        
$workflow $this->get_workflow();
        
$workflow->maybe_run$this->get_data_layer'object' ), truetrue );

        
$log $workflow->get_current_log();

        if ( 
$log ) {
            
$log->add_note__'This log was created from manual workflow re-run.''automatewoo' ) );
            return 
$log;
        }

        return 
false;
    }

}