awesomemotive /
google-analytics-for-wordpress
| 1 | <?php |
||
| 2 | |||
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
||
| 4 | exit; // Exit if accessed directly |
||
| 5 | } |
||
| 6 | |||
| 7 | class MonsterInsights_Measurement_Protocol_V4 { |
||
| 8 | private static $instance; |
||
| 9 | |||
| 10 | public static function get_instance() { |
||
| 11 | if ( empty( self::$instance ) ) { |
||
| 12 | self::$instance = new self(); |
||
| 13 | } |
||
| 14 | |||
| 15 | return self::$instance; |
||
| 16 | } |
||
| 17 | |||
| 18 | private $is_debug; |
||
| 19 | |||
| 20 | private $measurement_id; |
||
| 21 | |||
| 22 | private $schema; |
||
| 23 | |||
| 24 | private function __construct() { |
||
| 25 | $this->is_debug = monsterinsights_is_debug_mode(); |
||
| 26 | $this->measurement_id = monsterinsights_get_v4_id_to_output(); |
||
| 27 | |||
| 28 | $this->schema = array( |
||
| 29 | 'currency' => 'string', |
||
| 30 | 'value' => 'money', |
||
| 31 | 'coupon' => 'string', |
||
| 32 | 'transaction_id' => 'string', |
||
| 33 | 'affiliation' => 'string', |
||
| 34 | 'shipping' => 'double', |
||
| 35 | 'tax' => 'double', |
||
| 36 | 'user_id' => 'string', |
||
| 37 | 'items' => array( |
||
| 38 | 'item_id' => 'string', |
||
| 39 | 'item_name' => 'string', |
||
| 40 | 'affiliation' => 'string', |
||
| 41 | 'coupon' => 'string', |
||
| 42 | 'currency' => 'string', |
||
| 43 | 'discount' => 'double', |
||
| 44 | 'index' => 'integer', |
||
| 45 | 'item_brand' => 'string', |
||
| 46 | 'item_category' => 'string', |
||
| 47 | 'item_list_id' => 'string', |
||
| 48 | 'item_list_name' => 'string', |
||
| 49 | 'item_variant' => 'string', |
||
| 50 | 'location_id' => 'string', |
||
| 51 | 'price' => 'money', |
||
| 52 | 'quantity' => 'integer', |
||
| 53 | ), |
||
| 54 | ); |
||
| 55 | } |
||
| 56 | |||
| 57 | private function get_base_url() { |
||
| 58 | return 'https://www.google-analytics.com/mp/collect'; |
||
| 59 | } |
||
| 60 | |||
| 61 | private function get_url() { |
||
| 62 | $api_secret = is_multisite() && is_network_admin() |
||
| 63 | ? MonsterInsights()->auth->get_network_measurement_protocol_secret() |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 64 | : MonsterInsights()->auth->get_measurement_protocol_secret(); |
||
| 65 | |||
| 66 | return add_query_arg( |
||
| 67 | array( |
||
| 68 | 'api_secret' => apply_filters('monsterinsights_get_mp_call_secret', $api_secret), |
||
| 69 | 'measurement_id' => $this->measurement_id, |
||
| 70 | ), |
||
| 71 | $this->get_base_url() |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | private function get_client_id( $args ) { |
||
| 76 | if ( ! empty( $args['client_id'] ) ) { |
||
| 77 | return $args['client_id']; |
||
| 78 | } |
||
| 79 | |||
| 80 | $payment_id = 0; |
||
| 81 | if ( ! empty( $args['payment_id'] ) ) { |
||
| 82 | $payment_id = $args['payment_id']; |
||
| 83 | } |
||
| 84 | |||
| 85 | return monsterinsights_get_client_id( $payment_id ); |
||
| 86 | } |
||
| 87 | |||
| 88 | private function sanitize_event( $params, $schema ) { |
||
| 89 | $sanitized_params = array(); |
||
| 90 | |||
| 91 | foreach ( $params as $key => $value ) { |
||
| 92 | if ( ! array_key_exists( $key, $schema ) || |
||
| 93 | ( ! is_array( $value ) && gettype( $value ) === $schema[ $key ] ) |
||
| 94 | ) { |
||
| 95 | $sanitized_params[ $key ] = $value; |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | if ( is_array( $value ) && is_array( $schema[ $key ] ) ) { |
||
| 100 | $sanitized_params[ $key ] = array(); |
||
| 101 | foreach ( $value as $item_index => $item ) { |
||
| 102 | $sanitized_params[ $key ][ $item_index ] = $this->sanitize_event( $item, $schema[ $key ] ); |
||
| 103 | } |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | switch ( $schema[ $key ] ) { |
||
| 108 | case 'string': |
||
| 109 | $sanitized_params[ $key ] = (string) $value; |
||
| 110 | break; |
||
| 111 | |||
| 112 | case 'double': |
||
| 113 | $sanitized_params[ $key ] = (float) $value; |
||
| 114 | break; |
||
| 115 | |||
| 116 | case 'integer': |
||
| 117 | $sanitized_params[ $key ] = (int) $value; |
||
| 118 | break; |
||
| 119 | |||
| 120 | case 'money': |
||
| 121 | $sanitized_params[ $key ] = MonsterInsights_eCommerce_Helper::round_price( $value ); |
||
|
0 ignored issues
–
show
The type
MonsterInsights_eCommerce_Helper was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 122 | break; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | return $sanitized_params; |
||
| 127 | } |
||
| 128 | |||
| 129 | private function validate_args( $args, $defaults ) { |
||
| 130 | $out = array(); |
||
| 131 | |||
| 132 | foreach ( $defaults as $key => $default ) { |
||
| 133 | if ( array_key_exists( $key, $args ) ) { |
||
| 134 | $out[ $key ] = $args[ $key ]; |
||
| 135 | } else { |
||
| 136 | $out[ $key ] = $default; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | if ( ! empty( $args['user_id'] ) && monsterinsights_get_option( 'userid', false ) ) { |
||
| 141 | $out['user_id'] = (string) $args['user_id']; |
||
| 142 | } |
||
| 143 | |||
| 144 | foreach ( $out['events'] as $event_index => $event ) { |
||
| 145 | $sanitized_event = array(); |
||
| 146 | $sanitized_event['name'] = (string) $event['name']; |
||
| 147 | |||
| 148 | if ( ! empty( $event['params'] ) ) { |
||
| 149 | $sanitized_event['params'] = $this->sanitize_event( $event['params'], $this->schema ); |
||
| 150 | } |
||
| 151 | |||
| 152 | $out['events'][ $event_index ] = $sanitized_event; |
||
| 153 | } |
||
| 154 | |||
| 155 | return $out; |
||
| 156 | } |
||
| 157 | |||
| 158 | private function request( $args ) { |
||
| 159 | if ( empty( $this->measurement_id ) ) { |
||
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | $session_id = monsterinsights_get_browser_session_id( $this->measurement_id ); |
||
| 164 | |||
| 165 | $defaults = array( |
||
| 166 | 'client_id' => $this->get_client_id( $args ), |
||
| 167 | 'events' => array(), |
||
| 168 | 'consent' => array( |
||
| 169 | 'ad_personalization' => 'GRANTED', |
||
| 170 | ), |
||
| 171 | ); |
||
| 172 | |||
| 173 | $body = $this->validate_args( $args, $defaults ); |
||
| 174 | |||
| 175 | foreach ( $body['events'] as $index => $event ) { |
||
| 176 | |||
| 177 | // Provide a default session id if not set already. |
||
| 178 | if ( !empty( $session_id ) && empty( $body['events'][$index]['params']['session_id'] ) ) { |
||
| 179 | $body['events'][$index]['params']['session_id'] = $session_id; |
||
| 180 | } |
||
| 181 | |||
| 182 | if ( $this->is_debug ) { |
||
| 183 | $body['events'][ $index ]['params']['debug_mode'] = true; |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | $body = apply_filters( 'monsterinsights_mp_v4_api_call', $body ); |
||
| 188 | |||
| 189 | return wp_remote_post( |
||
| 190 | $this->get_url(), |
||
| 191 | array( |
||
| 192 | 'method' => 'POST', |
||
| 193 | 'timeout' => 5, |
||
| 194 | 'blocking' => $this->is_debug, |
||
| 195 | 'body' => wp_json_encode( $body ), |
||
| 196 | ) |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function collect( $args ) { |
||
| 201 | // Detect if browser request is a prefetch |
||
| 202 | if ( ( isset( $_SERVER["HTTP_X_PURPOSE"] ) && ( 'prefetch' === strtolower( sanitize_text_field($_SERVER["HTTP_X_PURPOSE"]) ) ) ) || |
||
| 203 | ( isset( $_SERVER["HTTP_X_MOZ"] ) && ( 'prefetch' === strtolower( sanitize_text_field($_SERVER["HTTP_X_MOZ"]) ) ) ) ) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $this->request( $args ); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | function monsterinsights_mp_collect_v4( $args ) { |
||
| 212 | return MonsterInsights_Measurement_Protocol_V4::get_instance()->collect( $args ); |
||
| 213 | } |
||
| 214 |