Base.php
6 years ago
EasyDigitalDownloads.php
6 years ago
MemberPress.php
6 years ago
Woocommerce.php
6 years ago
Base.php
112 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 WpMatomo\Logger; |
| 13 | use WpMatomo\Settings; |
| 14 | use WpMatomo\AjaxTracker; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit; // if accessed directly |
| 18 | } |
| 19 | |
| 20 | class Base { |
| 21 | protected $key_order_tracked = 'order-tracked'; |
| 22 | |
| 23 | /** |
| 24 | * @var Logger |
| 25 | */ |
| 26 | protected $logger; |
| 27 | |
| 28 | /** |
| 29 | * @var AjaxTracker |
| 30 | */ |
| 31 | protected $tracker; |
| 32 | |
| 33 | /** |
| 34 | * We can't echo cart updates directly as we wouldn't know where in the template rendering stage we are and whether |
| 35 | * we're supposed to print or not etc. Also there might be multiple cart updates triggered during one page load so |
| 36 | * we want to make sure to print only the most recent tracking code |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $cart_update_queue = ''; |
| 41 | |
| 42 | private $ajax_tracker_calls = array(); |
| 43 | |
| 44 | public function __construct( AjaxTracker $tracker ) { |
| 45 | $this->logger = new Logger(); |
| 46 | $this->tracker = $tracker; |
| 47 | |
| 48 | // by using prefix we make sure it will be removed on unistall and make sure it's clear it belongs to us |
| 49 | $this->key_order_tracked = Settings::OPTION_PREFIX . $this->key_order_tracked; |
| 50 | } |
| 51 | |
| 52 | public function register_hooks() { |
| 53 | if ( ! is_admin() ) { |
| 54 | add_action( 'wp_footer', array( $this, 'on_print_queues' ), 99999, 0 ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public function on_print_queues() { |
| 59 | // we need to queue in case there are multiple cart updates within one page load |
| 60 | if ( ! empty( $this->cart_update_queue ) ) { |
| 61 | echo $this->cart_update_queue; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | protected function has_order_been_tracked_already( $order_id ) { |
| 66 | return get_post_meta( $order_id, $this->key_order_tracked, true ) == 1; |
| 67 | } |
| 68 | |
| 69 | protected function set_order_been_tracked( $order_id ) { |
| 70 | update_post_meta( $order_id, $this->key_order_tracked, 1 ); |
| 71 | } |
| 72 | |
| 73 | protected function is_doing_ajax() { |
| 74 | return defined( 'DOING_AJAX' ) && DOING_AJAX; |
| 75 | } |
| 76 | |
| 77 | protected function make_matomo_js_tracker_call( $params ) { |
| 78 | if ( $this->is_doing_ajax() ) { |
| 79 | $this->ajax_tracker_calls[] = $params; |
| 80 | } |
| 81 | |
| 82 | return sprintf( 'window._paq = window._paq || []; window._paq.push(%s);', wp_json_encode( $params ) ); |
| 83 | } |
| 84 | |
| 85 | protected function wrap_script( $script ) { |
| 86 | if ( $this->is_doing_ajax() ) { |
| 87 | foreach ( $this->ajax_tracker_calls as $call ) { |
| 88 | $methods = array( |
| 89 | 'addEcommerceItem' => 'addEcommerceItem', |
| 90 | 'trackEcommerceOrder' => 'doTrackEcommerceOrder', |
| 91 | 'trackEcommerceCartUpdate' => 'doTrackEcommerceCartUpdate', |
| 92 | ); |
| 93 | if ( ! empty( $call[0] ) && ! empty( $methods[ $call[0] ] ) ) { |
| 94 | $tracker_method = $methods[ $call[0] ]; |
| 95 | array_shift( $call ); |
| 96 | call_user_func_array( array( $this->tracker, $tracker_method ), $call ); |
| 97 | } |
| 98 | } |
| 99 | $this->ajax_tracker_calls = array(); |
| 100 | |
| 101 | return ''; |
| 102 | } |
| 103 | |
| 104 | if ( empty( $script ) ) { |
| 105 | return ''; |
| 106 | } |
| 107 | |
| 108 | return '<script type="text/javascript">' . $script . '</script>'; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 |