/var/www/html_uk/wp-content/plugins/automatewoo/admin/ajax.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

use 
AutomateWoo\Admin\JSON_Search;
use 
AutomateWoo\Exceptions\InvalidPreviewData;
use 
AutomateWoo\Workflows\Factory;
use 
WP_Error;

/**
 * @class Admin_Ajax
 */
class Admin_Ajax {

    
/**
     * Hook in methods
     */
    
static function init() {
        
$ajax_events = [
            
'fill_trigger_fields',
            
'fill_action_fields',
            
'json_search_workflows',
            
'json_search_coupons',
            
'json_search_coupons_recurring',
            
'json_search_attribute_terms',
            
'json_search_taxonomy_terms',
            
'json_search_customers',
            
'json_search_products_and_variations_not_variable',
            
'json_search_products_not_variations_not_variable',
            
'json_search_sensei_courses',
            
'json_search_sensei_lessons',
            
'json_search_sensei_quizzes',
            
'json_search_sensei_questions',
            
'json_search_sensei_groups',
            
'activate',
            
'deactivate',
            
'email_preview_iframe',
            
'test_sms',
            
'database_update',
            
'database_update_items_to_process_count',
            
'save_preview_data',
            
'send_test_email',
            
'dismiss_system_error_notice',
            
'get_rule_select_choices',
            
'toggle_workflow_status',
            
'modal_log_info',
            
'modal_queue_info',
            
'modal_variable_info',
            
'modal_cart_info',
            
'update_dynamic_action_select',
            
'update_dynamic_trigger_options_select',
        ];

        foreach ( 
$ajax_events as $ajax_event ) {
            
add_action'wp_ajax_aw_' $ajax_event, [ __CLASS__$ajax_event ] );
        }
    }


    
/**
     *
     */
    
static function fill_trigger_fields() {

        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$trigger_name Clean::stringaw_request('trigger_name') );
        
$workflow_id absintaw_request('workflow_id') );
        
$is_new_workflow aw_request('is_new_workflow');

        
$workflow false;
        
$trigger Triggers::get$trigger_name );

        if ( ! 
$trigger )
            die;

        if ( ! 
$is_new_workflow ) {
            
$workflow = new Workflow$workflow_id );
        }

        
ob_start();

        
Admin::get_view('trigger-fields', [
            
'trigger' => $trigger,
            
'workflow' => $workflow,
        ]);

        
$fields ob_get_clean();

        
wp_send_json_success([
            
'fields' => $fields,
            
'trigger' => Admin_Workflow_Edit::get_trigger_data$trigger ),
        ]);
    }


    
/**
     * Retrieve action fields and echo JSON.
     */
    
public static function fill_action_fields() {

        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$action_name   Clean::stringaw_request'action_name' ) );
        
$action_number Clean::stringaw_request'action_number' ) );

        if ( 
'' === $action_name ) {
            
wp_send_json_success(
                [
                    
'fields'      => '',
                    
'title'       => 'New Action',
                    
'description' => '',
                ]
            );
        }

        
$action Actions::get$action_name );

        
ob_start();

        
Admin::get_view(
            
'action-fields',
            [
                
'action'        => $action,
                
'action_number' => $action_number,
            ]
        );

        
$fields ob_get_clean();

        
wp_send_json_success(
            [
                
'fields'      => $fields,
                
'title'       => $action->get_titletrue ),
                
'description' => $action->get_description_html(),
            ]
        );
    }


    
/**
     * Search for workflows and echo JSON.
     */
    
public static function json_search_workflows() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
ob_start();

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::workflows$term );
    }


    
/**
     * Search customers, includes guests customers
     */
    
static function json_search_customers() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
ob_start();

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::customers$term );
    }

    
/**
     * Search for products and variations, but not variable products.
     */
    
static function json_search_products_and_variations_not_variable() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::products$termtruefalse );
    }

    
/**
     * Search for products excluding variable and variation products.
     */
    
static function json_search_products_not_variations_not_variable() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::products$termfalsefalse );
    }


    
/**
     * Search for products and echo json
     */
    
public static function json_search_attribute_terms() {

        if ( ! 
current_user_can'manage_woocommerce' ) )
            die;

        if ( empty( 
$_GET['term'] ) || empty( $_GET['sibling'] ) ) {
            die;
        }

        
$search Clean::stringstripslashes$_GET['term'] ) );
        
$sibling Clean::stringstripslashes$_GET['sibling'] ) );

        
$terms get_terms'pa_' $sibling, [
            
'hide_empty' => false,
            
'search' => $search
        
]);

        
$found = [];

        if ( ! 
$terms || is_wp_error($terms)  )
            die();

        foreach ( 
$terms as $term ) {
            
$found$term->term_id '|' $term->taxonomy  ] = rawurldecode$term->name );
        }

        
wp_send_json$found );
    }



    
/**
     * Search for products and echo json
     */
    
public static function json_search_taxonomy_terms() {
        if ( ! 
Permissions::can_manage() ) {
            die;
        }

        
ob_start();

        
$search  Clean::stringstripslashesaw_get_url_var'term' ) ) );
        
$sibling Clean::stringstripslashesaw_get_url_var'sibling' ) ) );

        if ( empty( 
$search ) || empty($sibling) ) {
            die;
        }

        
$terms get_terms(
            
$sibling,
            [
                
'hide_empty' => false,
                
'search'     => $search
            
]
        );


        
$found = [];

        if ( ! 
$terms || is_wp_error($terms)  )
            die;

        foreach ( 
$terms as $term ) {
            
$found$term->term_id '|' $term->taxonomy  ] = rawurldecode$term->name );
        }

        
wp_send_json$found );
    }

    
/**
     * Search for coupons and echo JSON.
     */
    
public static function json_search_coupons() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::coupons$termtruefalse );
    }

    
/**
     * Search for recurring coupons and echo JSON.
     */
    
public static function json_search_coupons_recurring() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::coupons$termtruetrue );
    }

    
/**
     * Search for sensei courses and echo JSON.
     *
     * @since 5.6.10
     */
    
public static function json_search_sensei_courses() {
        if ( ! 
current_user_can'manage_sensei' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::sensei_data$term'course' );
    }

    
/**
     * Search for sensei lessons and echo JSON.
     *
     * @since 5.6.10
     */
    
public static function json_search_sensei_lessons() {
        if ( ! 
current_user_can'manage_sensei' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::sensei_data$term'lesson' );
    }

    
/**
     * Search for sensei quizzes and echo JSON.
     *
     * @since 5.6.10
     */
    
public static function json_search_sensei_quizzes() {
        if ( ! 
current_user_can'manage_sensei' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::sensei_data$term'quiz' );
    }

    
/**
     * Search for sensei questions and echo JSON.
     *
     * @since 5.6.10
     */
    
public static function json_search_sensei_questions() {
        if ( ! 
current_user_can'manage_sensei' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::sensei_data$term'question' );
    }

    
/**
     * Search for sensei groups and echo JSON.
     *
     * @since 5.6.10
     */
    
public static function json_search_sensei_groups() {
        if ( ! 
current_user_can'manage_sensei' ) ) {
            die;
        }

        
$term Clean::stringwp_unslashaw_get_url_var'term' ) ) );
        
JSON_Search::sensei_data$term'group' );
    }

    static function 
email_preview_iframe() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
$type Clean::stringaw_request('type') );
        
$args Clean::recursiveaw_request('args') );

        switch ( 
$type ) {

            case 
'workflow_action':
                try {
                    
$action Preview_Data::generate_preview_action$args['workflow_id'], $args['action_number'] );
                } catch ( 
InvalidPreviewData $e ) {
                    return 
wp_die$e->getMessage() );
                }

                
do_action'automatewoo/action/before_preview'$action );

                
$action->workflow->setup();

                echo 
$action->get_preview();

                
$action->workflow->cleanup();

                
do_action'automatewoo/action/after_preview'$action );

                break;

            default:
                
do_action'automatewoo/email_preview/html'$type$args );
        }

        exit();
    }


    
/**
     * Sends a test to supplied emails
     */
    
static function send_test_email() {

        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$type Clean::stringaw_request('type') );
        
$args Clean::recursiveaw_request('args') );
        
$to Clean::stringaw_request('to_emails') );

        
// save the to field
        
update_user_metaget_current_user_id(), 'automatewoo_email_preview_test_emails'$to );

        
$to Emails::parse_multi_email_field$to );

        switch ( 
$type ) {

            case 
'workflow_action':
                try {
                    
$action Preview_Data::generate_preview_action$args['workflow_id'], $args['action_number'], 'test' );
                } catch ( 
InvalidPreviewData $e ) {
                    return 
wp_die$e->getMessage() );
                }

                
$action->workflow->setup();
                
$current_user get_current_user_id();
                
// Temporarily remove the current user since no current user is typically exists when running a workflow
                // phpcs:ignore
                
wp_set_current_user);

                
$result $action->run_test( [ 'recipients' => $to ] );

                
// Rollback to the previous user.
                // phpcs:ignore
                
wp_set_current_user$current_user );
                
$action->workflow->cleanup();

                break;

            default:
                
do_action'automatewoo/email_preview/send_test'$type$to$args );
                
$result false;
        }

        if ( 
$result instanceof WP_Error ) {
            
wp_send_json_error(
                [
                    
/* translators: %s: The error message. */
                    
'message' => sprintf__'Error: %s''automatewoo' ), $result->get_error_message() ),
                ]
            );
        }

        
wp_send_json_success([
            
'message' => sprintf(
                
/* translators: %s: Number of emails sent. */
                
_n'Success! %s email sent''Success! %s emails sent'count$to ), 'automatewoo' ),
                
count$to )
            )
        ]);
    }



    static function 
test_sms() {
        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$from Clean::stringaw_request('from') );
        
$auth_id Clean::stringaw_request('auth_id') );
        
$auth_token Clean::stringaw_request('auth_token') );
        
$test_message Clean::stringaw_request('test_message') );
        
$test_recipient Clean::stringaw_request('test_recipient') );

        
$twilio = new Integration_Twilio$from$auth_id$auth_token );

        
$twilio->log_errors false// errors will be visible

        
$request $twilio->send_sms$test_recipient$test_message$from );

        if ( 
$request->is_successful() ) {
            
wp_send_json_success( [
                
'message' => __('Message sent.','automatewoo')
            ] );
        }
        else {
            
wp_send_json_error( [
                
'message' => $twilio->get_request_error_message$request )
            ] );
        }
    }


    static function 
database_update() {

        
$verify wp_verify_nonce$_REQUEST['nonce'], 'automatewoo_database_upgrade' );
        
$plugin_slug Clean::stringaw_request('plugin_slug') );

        if ( ! 
$verify ) {
            
wp_send_json_error__'Permission error.''automatewoo' ) );
        }

        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        if ( 
$plugin_slug == AW()->plugin_slug ) {
            
// updating the primary plugin
            
$complete Installer::run_database_updates();

            
wp_send_json_success([
                
'complete' => $complete,
                
'items_processed' => Installer::$db_update_items_processed
            
]);
        }
        else {
            
// updating an addon
            
$addon Addons::get$plugin_slug );

            if ( ! 
$addon ) {
                
wp_send_json_error(__'Add-on could not be updated''automatewoo' ) );
            }

            
$addon->do_database_update();

            
wp_send_json_success([
                
'complete' => true
            
]);
        }
    }


    static function 
database_update_items_to_process_count() {
        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$plugin_slug Clean::stringaw_request('plugin_slug') );

        if ( 
$plugin_slug == AW()->plugin_slug ) {
            
$count Installer::get_database_update_items_to_process_count();
        }
        else {
            
$count 0// batch processor not yet supported for addons
        
}

        
wp_send_json_success([
            
'items_to_process' => $count
        
]);
    }


    
/**
     * To preview an action save temporarily in the options table.
     */
    
static function save_preview_data() {
        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$workflow Factory::getaw_get_post_var'workflow_id' ) );
        
$trigger_name Clean::stringaw_get_post_var'trigger_name' ) );
        
$action_fields $workflow->sanitize_action_fieldsaw_get_post_var'action_fields' ) );

        if ( ! 
$trigger_name || ! is_array$action_fields ) || ! $workflow ) {
            
wp_send_json_error();
        }

        
$preview_data = [
            
'trigger_name' => $trigger_name,
            
'action_fields' => $action_fields,
        ];

        
update_option'aw_wf_preview_data_' $workflow->get_id(), $preview_datafalse );

        
wp_send_json_success();
    }


    
/**
     *
     */
    
static function dismiss_system_error_notice() {
        if ( ! 
self::is_authorized() ) {
            die;
        }

        
delete_transient('automatewoo_background_system_check_errors');

        
wp_send_json_success();
    }


    static function 
get_rule_select_choices() {

        if ( ! 
current_user_can'manage_woocommerce' ) )
            die;

        if ( ! 
$rule_name Clean::stringaw_request('rule_name') ) )
            die;

        
$rule_object Rules::get$rule_name );

        if ( 
$rule_object->type == 'select' ) {
            
wp_send_json_success([
                
'select_choices' => $rule_object->get_select_choices()
            ]);
        }

        die;
    }


    
/**
     * Display content for log details modal
     */
    
static function modal_log_info() {

        if ( ! 
current_user_can'manage_woocommerce' ) )
            die;

        if ( 
$log Log_Factory::getabsintaw_request('log_id') ) ) ) {
            
Admin::get_view'modal-log-info', [ 'log' => $log ] );
            die;
        }

        die( 
__'No log found.''automatewoo' ) );
    }


    static function 
modal_queue_info() {

        if ( ! 
current_user_can'manage_woocommerce' ) )
            die;

        if ( 
$event Queued_Event_Factory::getabsintaw_request('queued_event_id') ) ) ) {
            
Admin::get_view'modal-queued-event-info', [ 'event' => $event ] );
            die;
        }

        die( 
__'No queued event found.''automatewoo' ) );
    }


    static function 
modal_variable_info() {
        if ( ! 
current_user_can'manage_woocommerce' ) ) {
            die;
        }

        
$variable Variables::get_variableClean::stringaw_request'variable' ) ) );

        if ( 
$variable ) {
            
Admin::get_view'modal-variable-info', [
                
'variable' => $variable
            
]);
            die;
        }

        
wp_die__'Variable not found.''automatewoo' ) );
    }


    static function 
modal_cart_info() {

        if ( ! 
current_user_can'manage_woocommerce' ) )
            die;

        if ( 
$cart Cart_Factory::getabsintaw_request('cart_id') ) ) ) {
            
Admin::get_view'modal-cart-info', [ 'cart' => $cart ] );
            die;
        }

        die( 
__'No cart found.''automatewoo' ) );
    }



    static function 
toggle_workflow_status() {

        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$workflow Factory::getaw_request'workflow_id' ) );
        
$new_state Clean::stringaw_request'new_state' ) );

        if ( ! 
$workflow || ! $new_state )
            die;

        
$workflow->update_status$new_state === 'on' 'active' 'disabled' );

        
wp_send_json_success();
    }


    static function 
update_dynamic_action_select() {

        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$action_name Clean::stringaw_request'action_name' ) );
        
$target_field_name Clean::stringaw_request'target_field_name' ) );
        
$reference_field_value Clean::stringaw_request'reference_field_value' ) );

        
$options = [];

        if ( 
$reference_field_value ) {
            
$action Actions::get$action_name );
            
$options $action->get_dynamic_field_options$target_field_name$reference_field_value );
        }

        
wp_send_json_success$options );
    }

    
/**
     * Respond dynamic field options for a given trigger field.
     *
     * @since 5.6.6
     */
    
static function update_dynamic_trigger_options_select() {
        if ( ! 
self::is_authorized() ) {
            die;
        }

        
$trigger_name          Clean::stringaw_request'trigger_name' ) );
        
$target_field_name     Clean::stringaw_request'target_field_name' ) );
        
$reference_field_value Clean::stringaw_request'reference_field_value' ) );

        
$options = [];

        if ( 
$reference_field_value ) {
            
$trigger Triggers::get$trigger_name );
            
$options $trigger->get_dynamic_field_options$target_field_name$reference_field_value );
        }

        
wp_send_json_success$options );
    }

    
/**
     * Verifies if user is authorized to perform an ajax request.
     * We do so by checking if is able to manage WooCommerce and also checking the nonce.
     *
     * @since 5.7.6
     *
     * @return bool True if is authorized, false otherwise
     */
    
static function is_authorized() {
        
$nonce Clean::stringaw_request'nonce' ) );

        if ( ! 
current_user_can'manage_woocommerce' ) || ! wp_verify_nonce$nonceaw_request'action' ) ) ) {
            return 
false;
        }

        return 
true;
    }
}