Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
72.22% covered (warning)
72.22%
13 / 18
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PayPal_Tracks
81.25% covered (warning)
81.25%
13 / 16
50.00% covered (danger)
50.00%
1 / 2
9.53
0.00% covered (danger)
0.00%
0 / 1
 record_event
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
6.56
 platform
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * Tracks events for PayPal Payment Buttons.
4 *
5 * @package automattic/jetpack-paypal-payments
6 * @since $$next-version$$
7 */
8
9namespace Automattic\Jetpack\PaypalPayments;
10
11use Automattic\Jetpack\Connection\Manager as Connection_Manager;
12use Automattic\Jetpack\Status\Host;
13use Throwable;
14
15if ( ! defined( 'ABSPATH' ) ) {
16    exit;
17}
18
19/**
20 * Class PayPal_Tracks
21 *
22 * Records server-side Tracks events as the current user, via wpcom's
23 * `tracks_record_event()` on Simple and Jetpack's Tracking elsewhere.
24 */
25class PayPal_Tracks {
26
27    /**
28     * Record a Tracks event, adding `blog_id` and `platform`.
29     *
30     * Outside Simple this sends a blocking pixel request, so a front-end
31     * caller should check for Simple first.
32     *
33     * @since $$next-version$$
34     *
35     * @param string $event_name Full event name, e.g. `jetpack_paypal_button_created`.
36     * @param array  $properties Event properties. Keep personal data out.
37     * @return void
38     */
39    public static function record_event( string $event_name, array $properties = array() ) {
40        try {
41            $user                   = wp_get_current_user();
42            $properties['blog_id']  = (int) Connection_Manager::get_site_id( true );
43            $properties['platform'] = self::platform();
44
45            if ( ! function_exists( 'tracks_record_event' ) && function_exists( 'require_lib' ) ) {
46                require_lib( 'tracks/client' );
47            }
48
49            if ( function_exists( 'tracks_record_event' ) ) {
50                tracks_record_event( $user, $event_name, $properties );
51                return;
52            }
53
54            if ( class_exists( '\Automattic\Jetpack\Tracking' ) ) {
55                ( new \Automattic\Jetpack\Tracking() )->tracks_record_event( $user, $event_name, $properties );
56            }
57        } catch ( Throwable $e ) {
58            // Tracks is best-effort, so swallow its errors and let the recorded action finish.
59            unset( $e );
60        }
61    }
62
63    /**
64     * The host the event came from.
65     *
66     * @since $$next-version$$
67     *
68     * @return string `simple`, `atomic` or `self_hosted`.
69     */
70    private static function platform() {
71        $host = new Host();
72
73        if ( $host->is_wpcom_simple() ) {
74            return 'simple';
75        }
76
77        return $host->is_woa_site() ? 'atomic' : 'self_hosted';
78    }
79}