Нет описания

class-wc-settings-tracking.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * WooCommerce Settings Tracking
  4. *
  5. * @package WooCommerce\Tracks
  6. */
  7. defined( 'ABSPATH' ) || exit;
  8. /**
  9. * This class adds actions to track usage of WooCommerce Settings.
  10. */
  11. class WC_Settings_Tracking {
  12. /**
  13. * List of allowed WooCommerce settings to potentially track updates for.
  14. *
  15. * @var array
  16. */
  17. protected $allowed_options = array();
  18. /**
  19. * WooCommerce settings that have been updated (and will be tracked).
  20. *
  21. * @var array
  22. */
  23. protected $updated_options = array();
  24. /**
  25. * Init tracking.
  26. */
  27. public function init() {
  28. add_action( 'woocommerce_settings_page_init', array( $this, 'track_settings_page_view' ) );
  29. add_action( 'woocommerce_update_option', array( $this, 'add_option_to_list' ) );
  30. add_action( 'woocommerce_update_options', array( $this, 'send_settings_change_event' ) );
  31. }
  32. /**
  33. * Add a WooCommerce option name to our allowed options list and attach
  34. * the `update_option` hook. Rather than inspecting every updated
  35. * option and pattern matching for "woocommerce", just build a dynamic
  36. * list for WooCommerce options that might get updated.
  37. *
  38. * See `woocommerce_update_option` hook.
  39. *
  40. * @param array $option WooCommerce option (config) that might get updated.
  41. */
  42. public function add_option_to_list( $option ) {
  43. $this->allowed_options[] = $option['id'];
  44. // Delay attaching this action since it could get fired a lot.
  45. if ( false === has_action( 'update_option', array( $this, 'track_setting_change' ) ) ) {
  46. add_action( 'update_option', array( $this, 'track_setting_change' ), 10, 3 );
  47. }
  48. }
  49. /**
  50. * Add WooCommerce option to a list of updated options.
  51. *
  52. * @param string $option_name Option being updated.
  53. * @param mixed $old_value Old value of option.
  54. * @param mixed $new_value New value of option.
  55. */
  56. public function track_setting_change( $option_name, $old_value, $new_value ) {
  57. // Make sure this is a WooCommerce option.
  58. if ( ! in_array( $option_name, $this->allowed_options, true ) ) {
  59. return;
  60. }
  61. // Check to make sure the new value is truly different.
  62. // `woocommerce_price_num_decimals` tends to trigger this
  63. // because form values aren't coerced (e.g. '2' vs. 2).
  64. if (
  65. is_scalar( $old_value ) &&
  66. is_scalar( $new_value ) &&
  67. (string) $old_value === (string) $new_value
  68. ) {
  69. return;
  70. }
  71. $this->updated_options[] = $option_name;
  72. }
  73. /**
  74. * Send a Tracks event for WooCommerce options that changed values.
  75. */
  76. public function send_settings_change_event() {
  77. global $current_tab;
  78. if ( empty( $this->updated_options ) ) {
  79. return;
  80. }
  81. $properties = array(
  82. 'settings' => implode( ',', $this->updated_options ),
  83. );
  84. if ( isset( $current_tab ) ) {
  85. $properties['tab'] = $current_tab;
  86. }
  87. WC_Tracks::record_event( 'settings_change', $properties );
  88. }
  89. /**
  90. * Send a Tracks event for WooCommerce settings page views.
  91. */
  92. public function track_settings_page_view() {
  93. global $current_tab, $current_section;
  94. $properties = array(
  95. 'tab' => $current_tab,
  96. 'section' => empty( $current_section ) ? null : $current_section,
  97. );
  98. WC_Tracks::record_event( 'settings_view', $properties );
  99. }
  100. }