abstract-wc-privacy.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * WooCommerce abstract privacy class.
  4. *
  5. * @since 3.4.0
  6. * @package WooCommerce/Abstracts
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Abstract class that is intended to be extended by
  11. * specific privacy class. It handles the display
  12. * of the privacy message of the privacy id to the admin,
  13. * privacy data to be exported and privacy data to be deleted.
  14. *
  15. * @version 3.4.0
  16. * @package WooCommerce/Abstracts
  17. */
  18. abstract class WC_Abstract_Privacy {
  19. /**
  20. * This is the name of this object type.
  21. *
  22. * @var string
  23. */
  24. public $name;
  25. /**
  26. * This is a list of exporters.
  27. *
  28. * @var array
  29. */
  30. protected $exporters = array();
  31. /**
  32. * This is a list of erasers.
  33. *
  34. * @var array
  35. */
  36. protected $erasers = array();
  37. /**
  38. * Constructor
  39. *
  40. * @param string $name Plugin identifier.
  41. */
  42. public function __construct( $name = '' ) {
  43. $this->name = $name;
  44. $this->init();
  45. }
  46. /**
  47. * Hook in events.
  48. */
  49. protected function init() {
  50. add_action( 'admin_init', array( $this, 'add_privacy_message' ) );
  51. // We set priority to 5 to help WooCommerce's findings appear before those from extensions in exported items.
  52. add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporters' ), 5 );
  53. add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_erasers' ) );
  54. }
  55. /**
  56. * Adds the privacy message on WC privacy page.
  57. */
  58. public function add_privacy_message() {
  59. if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
  60. $content = $this->get_privacy_message();
  61. if ( $content ) {
  62. wp_add_privacy_policy_content( $this->name, $this->get_privacy_message() );
  63. }
  64. }
  65. }
  66. /**
  67. * Gets the message of the privacy to display.
  68. * To be overloaded by the implementor.
  69. *
  70. * @return string
  71. */
  72. public function get_privacy_message() {
  73. return '';
  74. }
  75. /**
  76. * Integrate this exporter implementation within the WordPress core exporters.
  77. *
  78. * @param array $exporters List of exporter callbacks.
  79. * @return array
  80. */
  81. public function register_exporters( $exporters = array() ) {
  82. foreach ( $this->exporters as $id => $exporter ) {
  83. $exporters[ $id ] = $exporter;
  84. }
  85. return $exporters;
  86. }
  87. /**
  88. * Integrate this eraser implementation within the WordPress core erasers.
  89. *
  90. * @param array $erasers List of eraser callbacks.
  91. * @return array
  92. */
  93. public function register_erasers( $erasers = array() ) {
  94. foreach ( $this->erasers as $id => $eraser ) {
  95. $erasers[ $id ] = $eraser;
  96. }
  97. return $erasers;
  98. }
  99. /**
  100. * Add exporter to list of exporters.
  101. *
  102. * @param string $id ID of the Exporter.
  103. * @param string $name Exporter name.
  104. * @param string $callback Exporter callback.
  105. */
  106. public function add_exporter( $id, $name, $callback ) {
  107. $this->exporters[ $id ] = array(
  108. 'exporter_friendly_name' => $name,
  109. 'callback' => $callback,
  110. );
  111. return $this->exporters;
  112. }
  113. /**
  114. * Add eraser to list of erasers.
  115. *
  116. * @param string $id ID of the Eraser.
  117. * @param string $name Exporter name.
  118. * @param string $callback Exporter callback.
  119. */
  120. public function add_eraser( $id, $name, $callback ) {
  121. $this->erasers[ $id ] = array(
  122. 'eraser_friendly_name' => $name,
  123. 'callback' => $callback,
  124. );
  125. return $this->erasers;
  126. }
  127. }