self::HOST, 'events_endpoint' => '/track/?ip=0', ], $options ); $this->token = $mixpanel_token; $this->mixpanel = WPMedia_Mixpanel::getInstance( $this->token, $mixpanel_options ); } /** * Check if debug mode is enabled * * @return bool */ private function is_debug(): bool { $debug = ( defined( 'WP_DEBUG' ) ? constant( 'WP_DEBUG' ) : false ) && ( defined( 'WP_DEBUG_LOG' ) ? constant( 'WP_DEBUG_LOG' ) : false ); /** * Filters whether Mixpanel debug mode is enabled. * * @param bool $debug Debug mode value. */ return apply_filters( 'wp_media_mixpanel_debug', $debug ); } /** * Log event to error log if debug mode is enabled * * @param string $event Event name. * @param mixed[] $properties Event properties. */ private function log_event( string $event, array $properties ): void { if ( ! $this->is_debug() ) { return; } error_log( 'Mixpanel event: ' . $event . ' ' . var_export( $properties, true ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_var_export } /** * Track an event in Mixpanel * * @param string $event Event name. * @param mixed[] $properties Event properties. */ public function track( string $event, array $properties ): void { $this->log_event( $event, $properties ); $this->mixpanel->track( $event, $properties ); } /** * Identify a user in Mixpanel * * @param string $user_id User ID. * * @return void */ public function identify( string $user_id ): void { $this->mixpanel->identify( $this->hash( $user_id ) ); } /** * Set a user property in Mixpanel * * @param string $user_id User ID. * @param string $property Property name. * @param mixed $value Property value. */ public function set_user_property( string $user_id, string $property, $value ): void { $this->mixpanel->people->set( $user_id, [ $property => $value, ], '0' ); } /** * Hash a value using sha224 * * @param string $value Value to hash. * * @return string */ public function hash( string $value ): string { return hash( 'sha224', $value ); } /** * Get the WordPress version * * @return string */ public function get_wp_version(): string { $version = preg_replace( '@^(\d\.\d+).*@', '\1', get_bloginfo( 'version' ) ); if ( null === $version ) { $version = '0.0'; } return $version; } /** * Get the PHP version * * @return string */ public function get_php_version(): string { $version = preg_replace( '@^(\d\.\d+).*@', '\1', phpversion() ); if ( null === $version ) { $version = '0.0'; } return $version; } /** * Get the active theme * * @return string */ public function get_current_theme(): string { $theme = wp_get_theme(); return $theme->get( 'Name' ); } /** * Get list of active plugins names * * @return string[] */ public function get_active_plugins(): array { $plugins = []; $active_plugins = (array) get_option( 'active_plugins', [] ); $all_plugins = get_plugins(); foreach ( $active_plugins as $plugin_path ) { if ( ! is_string( $plugin_path ) ) { continue; } if ( ! isset( $all_plugins[ $plugin_path ] ) ) { continue; } $plugins[] = $all_plugins[ $plugin_path ]['Name']; } return $plugins; } /** * Get the Mixpanel token * * @return string */ public function get_token(): string { return $this->token; } /** * Add the Mixpanel script & initialize it */ public function add_script(): void { ?>