Passed
Push — master ( 169926...ba6cf3 )
by Stiofan
22s
created

WPInv_Abstract_Privacy::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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