No Description

abstract-wc-privacy.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * This is a priority for the wp_privacy_personal_data_exporters filter
  39. *
  40. * @var int
  41. */
  42. protected $export_priority;
  43. /**
  44. * This is a priority for the wp_privacy_personal_data_erasers filter
  45. *
  46. * @var int
  47. */
  48. protected $erase_priority;
  49. /**
  50. * WC_Abstract_Privacy Constructor.
  51. *
  52. * @param string $name Plugin identifier.
  53. * @param int $export_priority Export priority.
  54. * @param int $erase_priority Erase priority.
  55. */
  56. public function __construct( $name = '', $export_priority = 5, $erase_priority = 10 ) {
  57. $this->name = $name;
  58. $this->export_priority = $export_priority;
  59. $this->erase_priority = $erase_priority;
  60. $this->init();
  61. }
  62. /**
  63. * Hook in events.
  64. */
  65. protected function init() {
  66. add_action( 'admin_init', array( $this, 'add_privacy_message' ) );
  67. // We set priority to 5 to help WooCommerce's findings appear before those from extensions in exported items.
  68. add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporters' ), $this->export_priority );
  69. add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_erasers' ), $this->erase_priority );
  70. }
  71. /**
  72. * Adds the privacy message on WC privacy page.
  73. */
  74. public function add_privacy_message() {
  75. if ( function_exists( 'wp_add_privacy_policy_content' ) ) {
  76. $content = $this->get_privacy_message();
  77. if ( $content ) {
  78. wp_add_privacy_policy_content( $this->name, $this->get_privacy_message() );
  79. }
  80. }
  81. }
  82. /**
  83. * Gets the message of the privacy to display.
  84. * To be overloaded by the implementor.
  85. *
  86. * @return string
  87. */
  88. public function get_privacy_message() {
  89. return '';
  90. }
  91. /**
  92. * Integrate this exporter implementation within the WordPress core exporters.
  93. *
  94. * @param array $exporters List of exporter callbacks.
  95. * @return array
  96. */
  97. public function register_exporters( $exporters = array() ) {
  98. foreach ( $this->exporters as $id => $exporter ) {
  99. $exporters[ $id ] = $exporter;
  100. }
  101. return $exporters;
  102. }
  103. /**
  104. * Integrate this eraser implementation within the WordPress core erasers.
  105. *
  106. * @param array $erasers List of eraser callbacks.
  107. * @return array
  108. */
  109. public function register_erasers( $erasers = array() ) {
  110. foreach ( $this->erasers as $id => $eraser ) {
  111. $erasers[ $id ] = $eraser;
  112. }
  113. return $erasers;
  114. }
  115. /**
  116. * Add exporter to list of exporters.
  117. *
  118. * @param string $id ID of the Exporter.
  119. * @param string $name Exporter name.
  120. * @param string|array $callback Exporter callback.
  121. *
  122. * @return array
  123. */
  124. public function add_exporter( $id, $name, $callback ) {
  125. $this->exporters[ $id ] = array(
  126. 'exporter_friendly_name' => $name,
  127. 'callback' => $callback,
  128. );
  129. return $this->exporters;
  130. }
  131. /**
  132. * Add eraser to list of erasers.
  133. *
  134. * @param string $id ID of the Eraser.
  135. * @param string $name Exporter name.
  136. * @param string|array $callback Exporter callback.
  137. *
  138. * @return array
  139. */
  140. public function add_eraser( $id, $name, $callback ) {
  141. $this->erasers[ $id ] = array(
  142. 'eraser_friendly_name' => $name,
  143. 'callback' => $callback,
  144. );
  145. return $this->erasers;
  146. }
  147. }