Nenhuma Descrição

Telemetry.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. // Exit if accessed directly.
  7. exit;
  8. }
  9. /**
  10. * Our telemetry class.
  11. *
  12. * Handles sending usage data back to our servers for those who have opted into our telemetry.
  13. *
  14. * @since 1.11.0
  15. */
  16. class PUM_Telemetry {
  17. /**
  18. * Initialization method
  19. */
  20. public static function init() {
  21. add_action( 'pum_daily_scheduled_events', array( __CLASS__, 'track_check' ) );
  22. if ( is_admin() && current_user_can( 'manage_options' ) ) {
  23. add_filter( 'pum_alert_list', array( __CLASS__, 'optin_alert' ) );
  24. add_action( 'pum_alert_dismissed', array( __CLASS__, 'optin_alert_check' ), 10, 2 );
  25. }
  26. }
  27. /**
  28. * Prepares and sends data, if it is time to do so
  29. *
  30. * @since 1.11.0
  31. */
  32. public static function track_check() {
  33. if ( self::is_time_to_send() ) {
  34. $data = self::setup_data();
  35. self::send_data( $data );
  36. set_transient( 'pum_tracking_last_send', true, 6 * DAY_IN_SECONDS );
  37. }
  38. }
  39. /**
  40. * Prepares telemetry data to be sent
  41. *
  42. * @return array
  43. * @since 1.11.0
  44. */
  45. public static function setup_data() {
  46. global $wpdb;
  47. // Retrieve current theme info.
  48. $theme_data = wp_get_theme();
  49. $theme = $theme_data->Name . ' ' . $theme_data->Version;
  50. // Retrieve current plugin information.
  51. if ( ! function_exists( 'get_plugins' ) ) {
  52. include ABSPATH . '/wp-admin/includes/plugin.php';
  53. }
  54. $plugins = array_keys( get_plugins() );
  55. $active_plugins = get_option( 'active_plugins', array() );
  56. foreach ( $plugins as $key => $plugin ) {
  57. if ( in_array( $plugin, $active_plugins ) ) {
  58. // Remove active plugins from list so we can show active and inactive separately.
  59. unset( $plugins[ $key ] );
  60. }
  61. }
  62. $popups = 0;
  63. foreach ( wp_count_posts( 'popup' ) as $status ) {
  64. $popups += $status;
  65. }
  66. $popup_themes = 0;
  67. foreach ( wp_count_posts( 'popup_theme' ) as $status ) {
  68. $popup_themes += $status;
  69. }
  70. // Aggregates important settings across all popups.
  71. $all_popups = pum_get_all_popups();
  72. $triggers = array();
  73. $cookies = array();
  74. $conditions = array();
  75. $location = array();
  76. $sizes = array();
  77. $sounds = array();
  78. // Cycle through each popup.
  79. foreach ( $all_popups as $popup ) {
  80. $settings = PUM_Admin_Popups::parse_values( $popup->get_settings() );
  81. // Cycle through each trigger to count the number of unique triggers.
  82. foreach ( $settings['triggers'] as $trigger ) {
  83. if ( isset( $triggers[ $trigger['type'] ] ) ) {
  84. $triggers[ $trigger['type'] ] += 1;
  85. } else {
  86. $triggers[ $trigger['type'] ] = 1;
  87. }
  88. }
  89. // Cycle through each cookie to count the number of unique cookie.
  90. foreach ( $settings['cookies'] as $cookie ) {
  91. if ( isset( $cookies[ $cookie['event'] ] ) ) {
  92. $cookies[ $cookie['event'] ] += 1;
  93. } else {
  94. $cookies[ $cookie['event'] ] = 1;
  95. }
  96. }
  97. // Cycle through each condition to count the number of unique condition.
  98. foreach ( $settings['conditions'] as $condition ) {
  99. foreach ( $condition as $target ) {
  100. if ( isset( $conditions[ $target['target'] ] ) ) {
  101. $conditions[ $target['target'] ] += 1;
  102. } else {
  103. $conditions[ $target['target'] ] = 1;
  104. }
  105. }
  106. }
  107. // Add locations setting.
  108. if ( isset( $location[ $settings['location'] ] ) ) {
  109. $location[ $settings['location'] ] += 1;
  110. } else {
  111. $location[ $settings['location'] ] = 1;
  112. }
  113. // Add size setting.
  114. if ( isset( $sizes[ $settings['size'] ] ) ) {
  115. $sizes[ $settings['size'] ] += 1;
  116. } else {
  117. $sizes[ $settings['size'] ] = 1;
  118. }
  119. // Add opening sound setting.
  120. if ( isset( $sounds[ $settings['open_sound'] ] ) ) {
  121. $sounds[ $settings['open_sound'] ] += 1;
  122. } else {
  123. $sounds[ $settings['open_sound'] ] = 1;
  124. }
  125. }
  126. return array(
  127. // UID.
  128. 'uid' => self::get_uuid(),
  129. // Language Info.
  130. 'language' => get_bloginfo( 'language' ),
  131. 'charset' => get_bloginfo( 'charset' ),
  132. // Server Info.
  133. 'php_version' => phpversion(),
  134. 'mysql_version' => $wpdb->db_version(),
  135. 'is_localhost' => self::is_localhost(),
  136. // WP Install Info.
  137. 'url' => get_site_url(),
  138. 'version' => Popup_Maker::$VER,
  139. 'wp_version' => get_bloginfo( 'version' ),
  140. 'theme' => $theme,
  141. 'active_plugins' => $active_plugins,
  142. 'inactive_plugins' => array_values( $plugins ),
  143. // Popup Metrics.
  144. 'popups' => $popups,
  145. 'popup_themes' => $popup_themes,
  146. 'open_count' => get_option( 'pum_total_open_count', 0 ),
  147. // Popup Maker Settings.
  148. 'block_editor_enabled' => pum_get_option( 'gutenberg_support_enabled' ),
  149. 'bypass_ad_blockers' => pum_get_option( 'bypass_adblockers' ),
  150. 'disable_taxonomies' => pum_get_option( 'disable_popup_category_tag' ),
  151. 'disable_asset_cache' => pum_get_option( 'disable_asset_caching' ),
  152. 'disable_open_tracking' => pum_get_option( 'disable_popup_open_tracking' ),
  153. 'default_email_provider' => pum_get_option( 'newsletter_default_provider', 'none' ),
  154. // Aggregate Popup Settings.
  155. 'triggers' => $triggers,
  156. 'cookies' => $cookies,
  157. 'conditions' => $conditions,
  158. 'locations' => $location,
  159. 'sizes' => $sizes,
  160. 'sounds' => $sounds,
  161. );
  162. }
  163. /**
  164. * Sends check_in data
  165. *
  166. * @param array $data Telemetry data to send.
  167. * @since 1.11.0
  168. */
  169. public static function send_data( $data = array() ) {
  170. self::api_call( 'check_in', $data );
  171. }
  172. /**
  173. * Makes HTTP request to our API endpoint
  174. *
  175. * @param string $action The specific endpoint in our API.
  176. * @param array $data Any data to send in the body.
  177. * @return array|bool False if WP Error. Otherwise, array response from wp_remote_post.
  178. * @since 1.11.0
  179. */
  180. public static function api_call( $action = '', $data = array() ) {
  181. $response = wp_remote_post(
  182. 'https://api.wppopupmaker.com/wp-json/pmapi/v2/' . $action,
  183. array(
  184. 'method' => 'POST',
  185. 'timeout' => 20,
  186. 'redirection' => 5,
  187. 'httpversion' => '1.1',
  188. 'blocking' => false,
  189. 'body' => $data,
  190. 'user-agent' => 'POPMAKE/' . Popup_Maker::$VER . '; ' . get_site_url(),
  191. )
  192. );
  193. if ( is_wp_error( $response ) ) {
  194. $error_message = $response->get_error_message();
  195. PUM_Utils_Logging::instance()->log( sprintf( 'Cannot send telemetry data. Error received was: %s', esc_html( $error_message ) ) );
  196. return false;
  197. }
  198. return $response;
  199. }
  200. /**
  201. * Adds admin notice if we haven't asked before.
  202. *
  203. * @param array $alerts The alerts currently in the alert system.
  204. * @return array Alerts for the alert system.
  205. * @since 1.11.0
  206. */
  207. public static function optin_alert( $alerts ) {
  208. if ( ! self::should_show_alert() ) {
  209. return $alerts;
  210. }
  211. $alerts[] = array(
  212. 'code' => 'pum_telemetry_notice',
  213. 'type' => 'info',
  214. 'message' => esc_html__( "We are constantly improving Popup Maker but that's difficult to do if we don't know how it's being used. Please allow data sharing so that we can receive a little information on how it is used. You can change this setting at any time on our Settings page. No user data is sent to our servers. No sensitive data is tracked.", 'popup-maker' ),
  215. 'priority' => 10,
  216. 'dismissible' => true,
  217. 'global' => false,
  218. 'actions' => array(
  219. array(
  220. 'primary' => true,
  221. 'type' => 'action',
  222. 'action' => 'pum_optin_check_allow',
  223. 'text' => __( 'Allow', 'popup-maker' ),
  224. ),
  225. array(
  226. 'primary' => false,
  227. 'type' => 'action',
  228. 'action' => 'dismiss',
  229. 'text' => __( 'Do not allow', 'popup-maker' ),
  230. ),
  231. array(
  232. 'primary' => false,
  233. 'type' => 'link',
  234. 'action' => '',
  235. 'href' => 'https://docs.wppopupmaker.com/article/528-the-data-the-popup-maker-plugin-collects',
  236. 'text' => __( 'Learn more', 'popup-maker' ),
  237. ),
  238. ),
  239. );
  240. return $alerts;
  241. }
  242. /**
  243. * Checks if any options have been clicked from admin notices.
  244. *
  245. * @param string $code The code for the alert.
  246. * @param string $action Action taken on the alert.
  247. *
  248. * @since 1.11.0
  249. */
  250. public static function optin_alert_check( $code, $action ) {
  251. if ( 'pum_telemetry_notice' === $code ) {
  252. if ( 'pum_optin_check_allow' === $action ) {
  253. pum_update_option( 'telemetry', true );
  254. }
  255. }
  256. }
  257. /**
  258. * Whether or not we should show optin alert
  259. *
  260. * @since 1.11.0
  261. * @return bool True if alert should be shown
  262. */
  263. public static function should_show_alert() {
  264. return false === self::has_opted_in() && current_user_can( 'manage_options' ) && strtotime( self::get_installed_on() . ' +15 minutes' ) < time();
  265. }
  266. /**
  267. * Determines if it is time to send telemetry data.
  268. *
  269. * @return bool True if it is time.
  270. * @since 1.11.0
  271. */
  272. public static function is_time_to_send() {
  273. // Only send if admin has opted in.
  274. if ( ! self::has_opted_in() ) {
  275. return false;
  276. }
  277. // Send a maximum of once per week.
  278. if ( get_transient( 'pum_tracking_last_send' ) ) {
  279. return false;
  280. }
  281. return true;
  282. }
  283. /**
  284. * Wrapper to check if site has opted into telemetry
  285. *
  286. * @return bool True if has opted into telemetry
  287. * @since 1.11.0
  288. */
  289. public static function has_opted_in() {
  290. return false !== pum_get_option( 'telemetry', false );
  291. }
  292. /**
  293. * Get the datetime string for when PM was installed.
  294. *
  295. * @return string
  296. * @since 1.13.0
  297. */
  298. public static function get_installed_on() {
  299. $installed_on = get_option( 'pum_installed_on', false );
  300. if ( ! $installed_on ) {
  301. $installed_on = current_time( 'mysql' );
  302. }
  303. return $installed_on;
  304. }
  305. /**
  306. * Determines if the site is in a local environment
  307. *
  308. * @return bool True for local
  309. * @since 1.11.0
  310. */
  311. public static function is_localhost() {
  312. $url = network_site_url( '/' );
  313. return stristr( $url, 'dev' ) !== false || stristr( $url, 'localhost' ) !== false || stristr( $url, ':8888' ) !== false;
  314. }
  315. /**
  316. * Generates a new UUID for this site.
  317. *
  318. * @return string
  319. * @since 1.11.0
  320. */
  321. public static function add_uuid() {
  322. $uuid = wp_generate_uuid4();
  323. update_option( 'pum_site_uuid', $uuid );
  324. return $uuid;
  325. }
  326. /**
  327. * Retrieves the site UUID
  328. *
  329. * @return string
  330. * @since 1.11.0
  331. */
  332. public static function get_uuid() {
  333. $uuid = get_option( 'pum_site_uuid', false );
  334. if ( false === $uuid || ! wp_is_uuid( $uuid ) ) {
  335. $uuid = self::add_uuid();
  336. }
  337. return $uuid;
  338. }
  339. }