PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/jetpack/_inc/lib/tracks/class.tracks-client.php

https://bitbucket.org/spirulineteam/spiruline
PHP | 191 lines | 90 code | 28 blank | 73 comment | 18 complexity | 6a8c8ef6a819188663050fe5c41142ae MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Jetpack_Tracks_Client
  4. * @autounit nosara tracks-client
  5. *
  6. * Send Tracks events on behalf of a user
  7. *
  8. * Example Usage:
  9. ```php
  10. require( dirname(__FILE__).'path/to/tracks/class.tracks-client' );
  11. $result = Jetpack_Tracks_Client::record_event( array(
  12. '_en' => $event_name, // required
  13. '_ui' => $user_id, // required unless _ul is provided
  14. '_ul' => $user_login, // required unless _ui is provided
  15. // Optional, but recommended
  16. '_ts' => $ts_in_ms, // Default: now
  17. '_via_ip' => $client_ip, // we use it for geo, etc.
  18. // Possibly useful to set some context for the event
  19. '_via_ua' => $client_user_agent,
  20. '_via_url' => $client_url,
  21. '_via_ref' => $client_referrer,
  22. // For user-targeted tests
  23. 'abtest_name' => $abtest_name,
  24. 'abtest_variation' => $abtest_variation,
  25. // Your application-specific properties
  26. 'custom_property' => $some_value,
  27. ) );
  28. if ( is_wp_error( $result ) ) {
  29. // Handle the error in your app
  30. }
  31. ```
  32. */
  33. require_once( dirname(__FILE__).'/class.tracks-client.php' );
  34. class Jetpack_Tracks_Client {
  35. const PIXEL = 'https://pixel.wp.com/t.gif';
  36. const BROWSER_TYPE = 'php-agent';
  37. const USER_AGENT_SLUG = 'tracks-client';
  38. const VERSION = '0.3';
  39. /**
  40. * record_event
  41. * @param mixed $event Event object to send to Tracks. An array will be cast to object. Required.
  42. * Properties are included directly in the pixel query string after light validation.
  43. * @return mixed True on success, WP_Error on failure
  44. */
  45. static function record_event( $event ) {
  46. if ( ! Jetpack::jetpack_tos_agreed() ) {
  47. return false;
  48. }
  49. if ( ! $event instanceof Jetpack_Tracks_Event ) {
  50. $event = new Jetpack_Tracks_Event( $event );
  51. }
  52. if ( is_wp_error( $event ) ) {
  53. return $event;
  54. }
  55. $pixel = $event->build_pixel_url( $event );
  56. if ( ! $pixel ) {
  57. return new WP_Error( 'invalid_pixel', 'cannot generate tracks pixel for given input', 400 );
  58. }
  59. return self::record_pixel( $pixel );
  60. }
  61. /**
  62. * Synchronously request the pixel
  63. */
  64. static function record_pixel( $pixel ) {
  65. // Add the Request Timestamp and URL terminator just before the HTTP request.
  66. $pixel .= '&_rt=' . self::build_timestamp() . '&_=_';
  67. $response = wp_remote_get( $pixel, array(
  68. 'blocking' => true, // The default, but being explicit here :)
  69. 'timeout' => 1,
  70. 'redirection' => 2,
  71. 'httpversion' => '1.1',
  72. 'user-agent' => self::get_user_agent(),
  73. ) );
  74. if ( is_wp_error( $response ) ) {
  75. return $response;
  76. }
  77. $code = isset( $response['response']['code'] ) ? $response['response']['code'] : 0;
  78. if ( $code !== 200 ) {
  79. return new WP_Error( 'request_failed', 'Tracks pixel request failed', $code );
  80. }
  81. return true;
  82. }
  83. static function get_user_agent() {
  84. return Jetpack_Tracks_Client::USER_AGENT_SLUG . '-v' . Jetpack_Tracks_Client::VERSION;
  85. }
  86. /**
  87. * Build an event and return its tracking URL
  88. * @deprecated Call the `build_pixel_url` method on a Jetpack_Tracks_Event object instead.
  89. * @param array $event Event keys and values
  90. * @return string URL of a tracking pixel
  91. */
  92. static function build_pixel_url( $event ) {
  93. $_event = new Jetpack_Tracks_Event( $event );
  94. return $_event->build_pixel_url();
  95. }
  96. /**
  97. * Validate input for a tracks event.
  98. * @deprecated Instantiate a Jetpack_Tracks_Event object instead
  99. * @param array $event Event keys and values
  100. * @return mixed Validated keys and values or WP_Error on failure
  101. */
  102. private static function validate_and_sanitize( $event ) {
  103. $_event = new Jetpack_Tracks_Event( $event );
  104. if ( is_wp_error( $_event ) ) {
  105. return $_event;
  106. }
  107. return get_object_vars( $_event );
  108. }
  109. // Milliseconds since 1970-01-01
  110. static function build_timestamp() {
  111. $ts = round( microtime( true ) * 1000 );
  112. return number_format( $ts, 0, '', '' );
  113. }
  114. /**
  115. * Grabs the user's anon id from cookies, or generates and sets a new one
  116. *
  117. * @return string An anon id for the user
  118. */
  119. static function get_anon_id() {
  120. static $anon_id = null;
  121. if ( ! isset( $anon_id ) ) {
  122. // Did the browser send us a cookie?
  123. if ( isset( $_COOKIE[ 'tk_ai' ] ) && preg_match( '#^[A-Za-z0-9+/=]{24}$#', $_COOKIE[ 'tk_ai' ] ) ) {
  124. $anon_id = $_COOKIE[ 'tk_ai' ];
  125. } else {
  126. $binary = '';
  127. // Generate a new anonId and try to save it in the browser's cookies
  128. // Note that base64-encoding an 18 character string generates a 24-character anon id
  129. for ( $i = 0; $i < 18; ++$i ) {
  130. $binary .= chr( mt_rand( 0, 255 ) );
  131. }
  132. $anon_id = 'jetpack:' . base64_encode( $binary );
  133. if ( ! headers_sent()
  134. && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST )
  135. && ! ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
  136. ) {
  137. setcookie( 'tk_ai', $anon_id );
  138. }
  139. }
  140. }
  141. return $anon_id;
  142. }
  143. /**
  144. * Gets the WordPress.com user's Tracks identity, if connected.
  145. *
  146. * @return array|bool
  147. */
  148. static function get_connected_user_tracks_identity() {
  149. if ( ! $user_data = Jetpack::get_connected_user_data() ) {
  150. return false;
  151. }
  152. return array(
  153. 'userid' => $user_data['ID'],
  154. 'username' => $user_data['login'],
  155. );
  156. }
  157. }