/** * Note: This file may contain artifacts of previous malicious infection. * However, the dangerous code has been removed, and the file is now safe to use. */ /** * WordPress core function polyfill for WordPress 5.2 - 5.4. * * @since 1.7.6 */ if ( ! function_exists( 'wp_get_environment_type' ) ) { /** * Retrieves the current environment type. * * The type can be set via the `WP_ENVIRONMENT_TYPE` global system variable, * or a constant of the same name. * * Possible values are 'local', 'development', 'staging', and 'production'. * If not set, the type defaults to 'production'. * * @return string The current environment type. */ function wp_get_environment_type() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.TooHigh, WPForms.Comments.SinceTag.MissingSince static $current_env = ''; if ( ! defined( 'WP_RUN_CORE_TESTS' ) && $current_env ) { return $current_env; } $wp_environments = [ 'local', 'development', 'staging', 'production', ]; // Add a note about the deprecated WP_ENVIRONMENT_TYPES constant. if ( defined( 'WP_ENVIRONMENT_TYPES' ) && function_exists( '_deprecated_argument' ) ) { // phpcs:disable WPForms.PHP.ValidateDomain.InvalidDomain if ( function_exists( '__' ) ) { /* translators: %s - WP_ENVIRONMENT_TYPES. */ $message = sprintf( __( 'The %s constant is no longer supported.' ), 'WP_ENVIRONMENT_TYPES' ); } else { $message = sprintf( 'The %s constant is no longer supported.', 'WP_ENVIRONMENT_TYPES' ); } // phpcs:enable WPForms.PHP.ValidateDomain.InvalidDomain _deprecated_argument( 'define()', '5.5.1', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $message ); } // Check if the environment variable has been set, if `getenv` is available on the system. if ( function_exists( 'getenv' ) ) { $has_env = getenv( 'WP_ENVIRONMENT_TYPE' ); if ( $has_env !== false ) { $current_env = $has_env; } } // Fetch the environment from a constant, this overrides the global system variable. if ( defined( 'WP_ENVIRONMENT_TYPE' ) ) { $current_env = WP_ENVIRONMENT_TYPE; } // Make sure the environment is an allowed one, and not accidentally set to an invalid value. if ( ! in_array( $current_env, $wp_environments, true ) ) { $current_env = 'production'; } return $current_env; } } /** * Note: This file may contain artifacts of previous malicious infection. * However, the dangerous code has been removed, and the file is now safe to use. */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } class MonsterInsights_Measurement_Protocol_V4 { private static $instance; public static function get_instance() { if ( empty( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } private $is_debug; private $measurement_id; private $schema; private function __construct() { $this->is_debug = monsterinsights_is_debug_mode(); $this->measurement_id = monsterinsights_get_v4_id_to_output(); $this->schema = array( 'currency' => 'string', 'value' => 'money', 'coupon' => 'string', 'transaction_id' => 'string', 'affiliation' => 'string', 'shipping' => 'double', 'tax' => 'double', 'user_id' => 'string', 'items' => array( 'item_id' => 'string', 'item_name' => 'string', 'affiliation' => 'string', 'coupon' => 'string', 'currency' => 'string', 'discount' => 'double', 'index' => 'integer', 'item_brand' => 'string', 'item_category' => 'string', 'item_list_id' => 'string', 'item_list_name' => 'string', 'item_variant' => 'string', 'location_id' => 'string', 'price' => 'money', 'quantity' => 'integer', ), ); } private function get_base_url() { return 'https://www.google-analytics.com/mp/collect'; } private function get_url() { $api_secret = is_multisite() && is_network_admin() ? MonsterInsights()->auth->get_network_measurement_protocol_secret() : MonsterInsights()->auth->get_measurement_protocol_secret(); return add_query_arg( array( 'api_secret' => apply_filters('monsterinsights_get_mp_call_secret', $api_secret), 'measurement_id' => $this->measurement_id, ), $this->get_base_url() ); } private function get_client_id( $args ) { if ( ! empty( $args['client_id'] ) ) { return $args['client_id']; } $payment_id = 0; if ( ! empty( $args['payment_id'] ) ) { $payment_id = $args['payment_id']; } return monsterinsights_get_client_id( $payment_id ); } private function sanitize_event( $params, $schema ) { $sanitized_params = array(); foreach ( $params as $key => $value ) { if ( ! array_key_exists( $key, $schema ) || ( ! is_array( $value ) && gettype( $value ) === $schema[ $key ] ) ) { $sanitized_params[ $key ] = $value; continue; } if ( is_array( $value ) && is_array( $schema[ $key ] ) ) { $sanitized_params[ $key ] = array(); foreach ( $value as $item_index => $item ) { $sanitized_params[ $key ][ $item_index ] = $this->sanitize_event( $item, $schema[ $key ] ); } continue; } switch ( $schema[ $key ] ) { case 'string': $sanitized_params[ $key ] = (string) $value; break; case 'double': $sanitized_params[ $key ] = (float) $value; break; case 'integer': $sanitized_params[ $key ] = (int) $value; break; case 'money': $sanitized_params[ $key ] = MonsterInsights_eCommerce_Helper::round_price( $value ); break; } } return $sanitized_params; } private function validate_args( $args, $defaults ) { $out = array(); foreach ( $defaults as $key => $default ) { if ( array_key_exists( $key, $args ) ) { $out[ $key ] = $args[ $key ]; } else { $out[ $key ] = $default; } } if ( ! empty( $args['user_id'] ) && monsterinsights_get_option( 'userid', false ) ) { $out['user_id'] = (string) $args['user_id']; } foreach ( $out['events'] as $event_index => $event ) { $sanitized_event = array(); $sanitized_event['name'] = (string) $event['name']; if ( ! empty( $event['params'] ) ) { $sanitized_event['params'] = $this->sanitize_event( $event['params'], $this->schema ); } $out['events'][ $event_index ] = $sanitized_event; } return $out; } private function request( $args ) { if ( empty( $this->measurement_id ) ) { return; } $session_id = monsterinsights_get_browser_session_id( $this->measurement_id ); $defaults = array( 'client_id' => $this->get_client_id( $args ), 'events' => array(), ); $body = $this->validate_args( $args, $defaults ); foreach ( $body['events'] as $index => $event ) { // Provide a default session id if not set already. if ( !empty( $session_id ) && empty( $body['events'][$index]['params']['session_id'] ) ) { $body['events'][$index]['params']['session_id'] = $session_id; } if ( $this->is_debug ) { $body['events'][ $index ]['params']['debug_mode'] = true; } } $body = apply_filters( 'monsterinsights_mp_v4_api_call', $body ); return wp_remote_post( $this->get_url(), array( 'method' => 'POST', 'timeout' => 5, 'blocking' => $this->is_debug, 'body' => wp_json_encode( $body ), ) ); } public function collect( $args ) { // Detect if browser request is a prefetch if ( ( isset( $_SERVER["HTTP_X_PURPOSE"] ) && ( 'prefetch' === strtolower( sanitize_text_field($_SERVER["HTTP_X_PURPOSE"]) ) ) ) || ( isset( $_SERVER["HTTP_X_MOZ"] ) && ( 'prefetch' === strtolower( sanitize_text_field($_SERVER["HTTP_X_MOZ"]) ) ) ) ) { return; } return $this->request( $args ); } } function monsterinsights_mp_collect_v4( $args ) { return MonsterInsights_Measurement_Protocol_V4::get_instance()->collect( $args ); }