Base.php
4 years ago
EasyDigitalDownloads.php
4 years ago
MatomoTestEcommerce.php
4 years ago
MemberPress.php
3 years ago
Woocommerce.php
4 years ago
Base.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * @package matomo |
| 8 | */ |
| 9 | |
| 10 | namespace WpMatomo\Ecommerce; |
| 11 | |
| 12 | use Exception; |
| 13 | use WpMatomo; |
| 14 | use WpMatomo\Admin\TrackingSettings; |
| 15 | use WpMatomo\AjaxTracker; |
| 16 | use WpMatomo\Logger; |
| 17 | use WpMatomo\Settings; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; // if accessed directly |
| 21 | } |
| 22 | |
| 23 | class Base { |
| 24 | protected $key_order_tracked = 'order-tracked'; |
| 25 | |
| 26 | /** |
| 27 | * @var Logger |
| 28 | */ |
| 29 | protected $logger; |
| 30 | |
| 31 | /** |
| 32 | * @var AjaxTracker |
| 33 | */ |
| 34 | protected $tracker; |
| 35 | |
| 36 | /** |
| 37 | * We can't echo cart updates directly as we wouldn't know where in the template rendering stage we are and whether |
| 38 | * we're supposed to print or not etc. Also there might be multiple cart updates triggered during one page load so |
| 39 | * we want to make sure to print only the most recent tracking code |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $cart_update_queue = ''; |
| 44 | |
| 45 | private $ajax_tracker_calls = []; |
| 46 | |
| 47 | public function __construct( AjaxTracker $tracker ) { |
| 48 | $this->logger = new Logger(); |
| 49 | $this->tracker = $tracker; |
| 50 | |
| 51 | // by using prefix we make sure it will be removed on unistall and make sure it's clear it belongs to us |
| 52 | $this->key_order_tracked = Settings::OPTION_PREFIX . $this->key_order_tracked; |
| 53 | } |
| 54 | |
| 55 | public function register_hooks() { |
| 56 | if ( ! is_admin() ) { |
| 57 | add_action( 'wp_footer', [ $this, 'on_print_queues' ], 99999, 0 ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | public function on_print_queues() { |
| 62 | // we need to queue in case there are multiple cart updates within one page load |
| 63 | if ( ! empty( $this->cart_update_queue ) ) { |
| 64 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 65 | echo $this->cart_update_queue; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | protected function has_order_been_tracked_already( $order_id ) { |
| 70 | // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison |
| 71 | return get_post_meta( $order_id, $this->key_order_tracked, true ) == 1; |
| 72 | } |
| 73 | |
| 74 | protected function set_order_been_tracked( $order_id ) { |
| 75 | update_post_meta( $order_id, $this->key_order_tracked, 1 ); |
| 76 | } |
| 77 | |
| 78 | protected function should_track_background() { |
| 79 | return ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 80 | || WpMatomo::$settings->get_global_option( 'track_mode' ) === TrackingSettings::TRACK_MODE_TAGMANAGER; |
| 81 | } |
| 82 | |
| 83 | protected function make_matomo_js_tracker_call( $params ) { |
| 84 | if ( $this->should_track_background() ) { |
| 85 | $this->ajax_tracker_calls[] = $params; |
| 86 | } |
| 87 | |
| 88 | return sprintf( 'window._paq = window._paq || []; window._paq.push(%s);', wp_json_encode( $params ) ); |
| 89 | } |
| 90 | |
| 91 | protected function wrap_script( $script ) { |
| 92 | if ( $this->should_track_background() ) { |
| 93 | foreach ( $this->ajax_tracker_calls as $call ) { |
| 94 | $methods = [ |
| 95 | 'addEcommerceItem' => 'addEcommerceItem', |
| 96 | 'trackEcommerceOrder' => 'doTrackEcommerceOrder', |
| 97 | 'trackEcommerceCartUpdate' => 'doTrackEcommerceCartUpdate', |
| 98 | ]; |
| 99 | if ( ! empty( $call[0] ) && ! empty( $methods[ $call[0] ] ) ) { |
| 100 | try { |
| 101 | $tracker_method = $methods[ $call[0] ]; |
| 102 | array_shift( $call ); |
| 103 | call_user_func_array( [ $this->tracker, $tracker_method ], $call ); |
| 104 | } catch ( Exception $e ) { |
| 105 | $this->logger->log_exception( $call[0], $e ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | $this->ajax_tracker_calls = []; |
| 110 | |
| 111 | return ''; |
| 112 | } |
| 113 | |
| 114 | if ( empty( $script ) ) { |
| 115 | return ''; |
| 116 | } |
| 117 | |
| 118 | if ( function_exists( 'wp_get_inline_script_tag' ) ) { |
| 119 | $script = wp_get_inline_script_tag( $script ); |
| 120 | } else { |
| 121 | // line feed is required to match the wp_get_inline_script_tag output |
| 122 | $script = '<script >' . PHP_EOL . $script . PHP_EOL . '</script>' . PHP_EOL; |
| 123 | } |
| 124 | |
| 125 | return $script; |
| 126 | } |
| 127 | } |
| 128 |