/var/www/html_uk/wp-content/plugins/automatewoo/includes/Preview_Data.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
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

use 
AutomateWoo\Actions\PreviewableInterface;
use 
AutomateWoo\Actions\TestableInterface;
use 
AutomateWoo\Exceptions\InvalidPreviewData;
use 
AutomateWoo\Workflows\Factory;
use 
WC_Order;
use 
WP_Query;

/**
 * @class Preview_Data
 * @since 2.4.6
 */
class Preview_Data {

    
/**
     * The current preview workflow.
     *
     * @since 4.6.0
     *
     * @var Workflow
     */
    
private static $preview_workflow;

    
/**
     * @param array $required_items list of specific data items to get for preview
     *
     * @return array
     * @throws InvalidPreviewData When there's an error getting the preview data.
     */
    
static function get_preview_data_layer$required_items = [] ) {
        
$data_layer = [];

        if ( 
in_array'user'$required_items ) ) {
            
$data_layer['user'] = wp_get_current_user();
        }

        if ( 
in_array'customer'$required_items ) ) {
            
$data_layer['customer'] = Customer_Factory::get_by_user_idget_current_user_id() );
        }

        
/**
         * Order and order item
         */
        
if ( in_array'order'$required_items ) || in_array'order_item'$required_items ) ) {
            
$order self::get_preview_order();
            
$order_items $order->get_items();

            if ( empty( 
$order_items ) ) {
                throw 
InvalidPreviewData::data_item_needed'order_item' );
            }

            
$data_layer['order'] = $order;
            
$data_layer['order_item'] = current$order_items );
        }

        
/**
         * Product
         */
        
if ( in_array'product'$required_items ) ) {
            
$product_ids self::get_preview_product_ids();
            
$data_layer['product'] = wc_get_product$product_ids[0] );
        }

        
/**
         * Category
         */
        
if ( in_array'category'$required_items ) ) {
            
$categories get_terms(
                [
                    
'taxonomy' => 'product_cat',
                    
'order'    => 'count',
                    
'number'   => 1
                
]
            );

            if ( empty( 
$categories ) ) {
                throw 
InvalidPreviewData::data_item_needed'category' );
            }

            
$data_layer['category'] = current$categories );
        }

        
/**
         * Cart
         */
        
if ( in_array'cart'$required_items ) ) {
            
$cart = new Cart();
            
$cart->set_id);
            
$cart->set_total100 );
            
$cart->set_user_idget_current_user_id() );
            
$cart->set_token();
            
$cart->set_date_last_modified( new DateTime() );

            
$product_ids self::get_preview_product_ids( array( 'post_status' => 'publish' ) );
            
$items          = [];

            foreach ( 
$product_ids as $product_id ) {
                
$product wc_get_product$product_id );

                
// Ensure only published products are included.
                
if ( ! is_a$product'WC_Product' ) || $product->get_status() !== 'publish' ) {
                    continue;
                }

                
$variation_id 0;
                
$variation = [];

                if ( 
$product->is_type('variable') ) {
                    
/** @var \WC_Product_Variable $product */
                    
$variations $product->get_available_variations();
                    if ( 
$variations ) {
                        
$variation_id $variations[0]['variation_id'];
                        
$variation $variations[0]['attributes'];
                    }
                }

                
$itemsuniqid() ] = [
                    
'product_id' => $product_id,
                    
'variation_id' => $variation_id,
                    
'variation' => $variation,
                    
'quantity' => 1,
                    
'line_subtotal' => (float) $product->get_price(),
                    
'line_subtotal_tax' => (float) wc_get_price_including_tax$product ) - (float) $product->get_price(),
                ];

            }

            if ( empty( 
$items ) ) {
                throw 
InvalidPreviewData::data_item_needed'product' );
            }

            
$cart->set_items$items );

            
$cart->set_coupons([
                
'10off' => [
                    
'discount_incl_tax' => '10',
                    
'discount_excl_tax' => '9',
                    
'discount_tax' => '1'
                
]
            ]);

            
$data_layer['cart'] = $cart;
        }

        
/**
         * Wishlist
         */
        
if ( in_array'wishlist'$required_items ) ) {
            
$wishlist        = new Wishlist();
            
$wishlist->items self::get_preview_product_ids();

            
$data_layer['wishlist'] = $wishlist;
        }

        
/**
         * Guest
         */
        
if ( in_array'guest'$required_items ) ) {
            
$guest = new Guest();
            
$guest->set_email'[email protected]);
            
$data_layer['guest'] = $guest;
        }

        
/**
         * Subscription
         */
        
if ( Integrations::is_subscriptions_active() && in_array'subscription'$required_items ) ) {
            
$subscriptions wcs_get_subscriptions(
                [
                    
'subscriptions_per_page' => 1
                
]
            );

            if ( empty( 
$subscriptions ) ) {
                throw 
InvalidPreviewData::data_item_needed'subscription' );
            }

            
$data_layer['subscription'] = current$subscriptions );
        }

        
/**
         * Membership
         */
        
if ( Integrations::is_memberships_enabled() && in_array'membership'$required_items ) ) {
            
$memberships get_posts(
                [
                    
'post_type'      => 'wc_user_membership',
                    
'post_status'    => 'any',
                    
'posts_per_page' => 1,
                ]
            );

            if ( empty( 
$memberships ) ) {
                throw 
InvalidPreviewData::data_item_needed'membership' );
            }

            
$data_layer['membership'] = wc_memberships_get_user_membershipcurrent$memberships ) );
        }

        
/**
         * Bookings
         */
        
if ( Integrations::is_bookings_active() && in_array'booking'$required_items ) ) {
            try {
                
$booking               AW()->bookings_proxy()->get_most_recent_booking();
                
$data_layer['booking'] = $booking;
                
$data_layer['product'] = $booking->get_product();
            } catch ( 
\Exception $e ) {
                
// rethrow as user-facing InvalidPreviewData exception
                
throw InvalidPreviewData::data_item_needed'booking' );
            }
        }

        
/**
         * Sensei Integration
         */
        
if ( Integrations::is_sensei_lms_active() ) {
            if ( 
in_array'course'$required_itemstrue ) ) {
                
$data_layer['course'] = self::get_preview_course();
            }

            if ( 
in_array'lesson'$required_itemstrue ) ) {
                
$lessons get_posts(
                    [
                        
'post_type'      => 'lesson',
                        
'posts_per_page' => 1,
                    ]
                );

                if ( empty( 
$lessons ) ) {
                    throw 
InvalidPreviewData::data_item_needed'lesson' );
                }

                
$data_layer['lesson'] = current$lessons );
            }

            if ( 
in_array'quiz'$required_itemstrue ) ) {
                
$quizzes get_posts(
                    [
                        
'post_type'      => 'quiz',
                        
'post_status'    => 'publish',
                        
'posts_per_page' => 1,
                    ]
                );

                if ( empty( 
$quizzes ) ) {
                    throw 
InvalidPreviewData::data_item_needed'quiz' );
                }

                
$data_layer['quiz'] = get_postcurrent$quizzes ) );
            }

            if ( 
in_array'teacher'$required_itemstrue ) ) {
                
$course  self::get_preview_course();
                
$teacher get_user_by'id'$course->post_author );
                if ( 
$teacher ) {
                    
$data_layer['teacher'] = $teacher;
                } else {
                    
$data_layer['teacher'] = wp_get_current_user();
                }
            }

            if ( 
in_array'group'$required_itemstrue ) ) {
                
$groups get_posts(
                    [
                        
'post_type'      => 'group',
                        
'posts_per_page' => 1,
                    ]
                );

                if ( empty( 
$groups ) ) {
                    throw 
InvalidPreviewData::data_item_needed'group' );
                }

                
$data_layer['group'] = current$groups );
            }
        }

        
/**
         * Card
         */
        
if ( in_array'card'$required_items ) ) {
            
$token = new \WC_Payment_Token_CC();
            
$token->set_user_id);
            
$token->set_card_type('visa');
            
$token->set_expiry_month04 );
            
$token->set_expiry_year2020 );
            
$token->set_last41234 );

            
$data_layer['card'] = $token;
        }

        
/**
         * Download
         */
        
if ( in_array'download'$required_itemstrue ) ) {
            
$data_store \WC_Data_Store::load'customer-download' );
            
$downloads  $data_store->get_downloads(
                array(
                    
'limit' => 1,
                    
'order' => 'DESC',
                )
            );
            if ( ! empty( 
$downloads ) ) {
                
$download               current$downloads );
                
$data_layer['download'] = new Download$download->get_download_id(), $download->get_product_id(), $download->get_order_id() );
            }
        }

        return 
apply_filters'automatewoo/preview_data_layer'$data_layer$required_items );
    }


    
/**
     * Generate a previewable or testable action object filled with preview data.
     *
     * @param int    $workflow_id
     * @param int    $action_number
     * @param string $mode test|preview
     *
     * @return Action|PreviewableInterface|TestableInterface
     *
     * TODO Remove Action return type when code-hinting isn't required for public Action props
     *
     * @throws InvalidPreviewData When there's an error with the preview data.
     */
    
static function generate_preview_action$workflow_id$action_number$mode 'preview' ) {
        
$preview_data wp_unslashget_option'aw_wf_preview_data_' $workflow_id ) );
        
$workflow Factory::get$workflow_id );
        if ( ! 
$workflow || ! $action_number || ! is_array$preview_data ) ) {
            throw 
InvalidPreviewData::generic();
        }

        
$trigger Triggers::getClean::string$preview_data['trigger_name'] ) );
        
$action_fields $workflow->format_action_fields$preview_data['action_fields'] );
        if ( ! 
$trigger || ! $action_fields ) {
            throw 
InvalidPreviewData::generic();
        }

        try {
            
$action = clone $workflow->get_action_from_action_fields$action_fields );
        } catch ( 
Exception $e ) {
            throw 
InvalidPreviewData::generic();
        }

        if ( ! 
$action instanceof PreviewableInterface && ! $action instanceof TestableInterface ) {
            throw 
InvalidPreviewData::invalid_action();
        }

        
// add the workflow in preview mode
        
if ( $mode === 'test' ) {
            
$workflow->enable_test_mode();
        } else {
            
$workflow->enable_preview_mode();
        }

        
// set the data layer from preview trigger
        
$workflow->set_data_layerPreview_Data::get_preview_data_layer$trigger->get_supplied_data_items() ), true );

        
$action->workflow $workflow;

        
// replace saved options with live preview data
        
$action->set_options$action_fields );

        return 
$action;
    }


    
/**
     * Get an order for preview.
     *
     * @param int $offset used to do multiple attempts to get a valid order
     *
     * @return WC_Order
     * @throws InvalidPreviewData When no preview order is found.
     */
    
protected static function get_preview_order$offset ) {
        if ( 
$offset ) {
            throw 
InvalidPreviewData::order_not_found();
        }

        
$orders wc_get_orders(
            [
                
'type'   => 'shop_order',
                
'limit'  => 1,
                
'offset' => $offset,
                
'return' => 'ids',
            ]
        );

        if ( ! 
$orders ) {
            throw 
InvalidPreviewData::order_not_found();
        }

        
$order wc_get_order$orders[0] );

        
// if the order has a blank email, it will cause issues
        
if ( $order && $order->get_billing_email() ) {
            return 
$order;
        }

        return 
self::get_preview_order$offset );
    }

    
/**
     * Add customer language filter.
     *
     * @since 4.6.0
     *
     * @param Action $action
     */
    
public static function add_customer_language_filter$action ) {
        
self::$preview_workflow $action->workflow;
        
add_filter'automatewoo/customer/get_language', [ __CLASS__'filter_customer_language' ], 10);
    }

    
/**
     * Filter the current customers language when previewing.
     * Make it the same as the preview workflow language.
     *
     * @since 4.6.0
     *
     * @param string $lang
     * @param Customer $customer
     *
     * @return string
     */
    
public static function filter_customer_language$lang$customer ) {
        if ( ! 
self::$preview_workflow ) {
            return 
$lang;
        }

        
$workflow_customer self::$preview_workflow->data_layer()->get_customer();

        
// Only filter the language of the customer in the preview workflow.
        
if ( $workflow_customer && $customer->get_id() === $workflow_customer->get_id() ) {
            return (string) 
self::$preview_workflow->get_language();
        }

        return 
$lang;
    }

    
/**
     * Remove customer language filter.
     *
     * @since 4.6.0
     *
     * @param Action $action
     */
    
public static function remove_customer_language_filter$action ) {
        
self::$preview_workflow null;
        
remove_filter'automatewoo/customer/get_language', [ __CLASS__'filter_customer_language' ], 10 );
    }

    
/**
     * Get preview products.
     *
     * @param array $args Optional array of arguments to pass to WP_Query.
     *
     * @return array
     *
     * @throws InvalidPreviewData When no products found.
     */
    
protected static function get_preview_product_ids$args = array() ): array {
        
// Cache for request since this may be called multiple times
        
static $products null;
        if ( 
null === $products ) {
            
$args array_merge( array(
                
'post_type'      => 'product',
                
'posts_per_page' => 4,
                
'fields'         => 'ids'
            
), $args );

            
$product_query = new WP_Query$args );

            
$products $product_query->posts;
            if ( empty( 
$products ) ) {
                throw 
InvalidPreviewData::data_item_needed'product' );
            }
        }

        return 
$products;
    }

    
/**
     * Get a course for preview.
     *
     * @return \WP_Post
     * @throws InvalidPreviewData When no preview course is found.
     */
    
protected static function get_preview_course() {
        
$courses get_posts(
            [
                
'post_type'      => 'course',
                
'posts_per_page' => 1,
            ]
        );

        if ( empty( 
$courses ) ) {
            throw 
InvalidPreviewData::data_item_needed'course' );
        }

        return 
current$courses );
    }

}