Test Failed
Pull Request — master (#456)
by Kiran
22:05
created

GeoDir_Privacy   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 244
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 35 4
B get_privacy_message() 0 91 1
A queue_cleanup_personal_data() 0 5 1
B anonymize_custom_data_types() 0 16 6
A trash_pending_posts() 0 3 1
A anonymize_published_posts() 0 3 1
A trash_posts_query() 0 12 3
A anonymize_posts_query() 0 13 3
1
<?php
2
/**
3
 * Privacy/GDPR related functionality which ties into WordPress functionality.
4
 *
5
 * @since 1.6.26
6
 * @package GeoDirectory
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * GeoDir_Privacy Class.
13
 */
14
class GeoDir_Privacy extends GeoDir_Abstract_Privacy {
15
16
	/**
17
	 * Background process to clean up orders.
18
	 *
19
	 * @var GeoDir_Privacy_Background_Process
20
	 */
21
	protected static $background_process;
22
23
	/**
24
	 * Init - hook into events.
25
	 */
26
	public function __construct() {
27
		parent::__construct( __( 'GeoDirectory', 'geodirectory' ) );
28
29
		if ( ! self::$background_process ) {
30
			self::$background_process = new GeoDir_Privacy_Background_Process();
31
		}
32
33
		// Include supporting classes.
34
		include_once( 'class-geodir-privacy-erasers.php' );
35
		include_once( 'class-geodir-privacy-exporters.php' );
36
37
		$gd_post_types = geodir_get_posttypes( 'object' );
38
39
		if ( ! empty( $gd_post_types ) ) {
40
			foreach ( $gd_post_types as $post_type => $info ) {
0 ignored issues
show
Bug introduced by
The expression $gd_post_types of type array|object|string is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
41
				$name = $info->labels->name;
42
				$slug = $info->has_archive;
43
44
				// This hook registers GeoDirectory data exporters.
45
				$this->add_exporter( 'geodirectory-' . $slug, wp_sprintf( __( 'User %s', 'geodirectory' ), $name ), array( 'GeoDir_Privacy_Exporters', 'post_data_exporter' ) );
0 ignored issues
show
Documentation introduced by
array('GeoDir_Privacy_Ex..., 'post_data_exporter') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47
				// This hook registers GeoDirectory data erasers.
48
				$this->add_eraser( 'geodirectory-' . $slug, wp_sprintf( __( 'User %s', 'geodirectory' ), $name ), array( 'GeoDir_Privacy_Erasers', 'post_data_eraser' ) );
0 ignored issues
show
Documentation introduced by
array('GeoDir_Privacy_Er...s', 'post_data_eraser') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
49
			}
50
		}
51
52
		// Cleanup orders daily - this is a callback on a daily cron event.
53
		add_action( 'geodir_cleanup_personal_data', array( $this, 'queue_cleanup_personal_data' ) );
54
55
		// Handles custom anonomization types not included in core.
56
		add_filter( 'wp_privacy_anonymize_data', array( $this, 'anonymize_custom_data_types' ), 10, 3 );
57
58
		// When this is fired, data is removed in a given order. Called from bulk actions.
59
		add_action( 'geodir_remove_post_personal_data', array( 'GeoDir_Privacy_Erasers', 'remove_post_personal_data' ) );
60
	}
61
62
	/**
63
	 * Add privacy policy content for the privacy policy page.
64
	 *
65
	 * @since 1.6.26
66
	 *
67
	 * @return string The default policy content.
68
	 */
69
	public function get_privacy_message() {
70
		$content = '';
71
72
		// Start of the suggested privacy policy text.
73
		$content .=
74
			'<div class="geodir-privacy-text">';
75
		$content .=
76
			'<h2>' . __( 'Who we are' ) . '</h2>';
77
		$content .=
78
			'<p>' . __( 'GeoDirectory is the only WordPress directory plugin on the market that can scale to millions of listings and withstand the battering of traffic that comes along with that.' ) . '</p>';
79
80
			'<h2>' . __( 'What personal data we collect and why we collect it' ) . '</h2>';
81
		$content .=
82
			'<p>' . __( 'In this section you should note what personal data you collect from users and site visitors. This may include personal data, such as name, email address, personal account preferences; transactional data, such as purchase information; and technical data, such as information about cookies.' ) . '</p>' .
83
			'<p>' . __( 'You should also note any collection and retention of sensitive personal data, such as data concerning health.' ) . '</p>' .
84
			'<p>' . __( 'In addition to listing what personal data you collect, you need to note why you collect it. These explanations must note either the legal basis for your data collection and retention or the active consent the user has given.' ) . '</p>' .
85
			'<p>' . __( 'Personal data is not just created by a user&#8217;s interactions with your site. Personal data is also generated from technical processes such as contact forms, comments, cookies, analytics, and third party embeds.' ) . '</p>' .
86
			'<p>' . __( 'By default WordPress does not collect any personal data about visitors, and only collects the data shown on the User Profile screen from registered users. However some of your plugins may collect personal data. You should add the relevant information below.' ) . '</p>';
87
88
		$content .=
89
			'<h3>' . __( 'Posts' ) . '</h3>';
90
		$content .=
91
			'<p>' . __( 'In this subsection you should note what information is captured through posts. We have noted the data which WordPress collects by default.' ) . '</p>' . 
92
			'<p>' . __( 'When users add posts on the site we collect the data shown in the posts form, and also the visitor&#8217;s IP address and browser user agent string to help spam detection.' ) . '</p>' .
93
			'<p>' . __( 'An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your post, your profile picture is visible to the public in the context of your post.' ) . '</p>';
94
95
		$content .=
96
			'<h3>' . __( 'Reviews' ) . '</h3>';
97
		$content .=
98
			'<p>' . __( 'In this subsection you should note what information is captured through reviews. We have noted the data which WordPress collects by default.' ) . '</p>' . 
99
			'<p>' . __( 'When visitors leave reviews on the site we collect the data shown in the reviews form, and also the visitor&#8217;s IP address and browser user agent string to help spam detection.' ) . '</p>' .
100
			'<p>' . __( 'An anonymized string created from your email address (also called a hash) may be provided to the Gravatar service to see if you are using it. The Gravatar service privacy policy is available here: https://automattic.com/privacy/. After approval of your review, your profile picture is visible to the public in the context of your review.' ) . '</p>';
101
102
		$content .=
103
			'<h3>' . __( 'Media' ) . '</h3>';
104
		$content .=
105
			'<p>' . __( 'In this subsection you should note what information may be disclosed by users who can upload media files. All uploaded files are usually publicly accessible.' ) . '</p>' . 
106
			'<p>' . __( 'If you upload images to the website, you should avoid uploading images with embedded location data (EXIF GPS) included. Visitors to the website can download and extract any location data from images on the website.' ) . '</p>';
107
108
		$content .=
109
			'<h2>' . __( 'How long we retain your data' ) . '</h2>';
110
		$content .=
111
			'<p>' . __( 'In this section you should explain how long you retain personal data collected or processed by the web site. While it is your responsibility to come up with the schedule of how long you keep each dataset for and why you keep it, that information does need to be listed here. For example, you may want to say that you keep contact form entries for six months, analytics records for a year, and customer purchase records for ten years.' ) . '</p>' . 
112
			'<p>' . __( 'If you leave a comment, the comment and its metadata are retained indefinitely. This is so we can recognize and approve any follow-up comments automatically instead of holding them in a moderation queue.' ) . '</p>' .
113
			'<p>' . __( 'For users that register on our website (if any), we also store the personal information they provide in their user profile. All users can see, edit, or delete their personal information at any time (except they cannot change their username). Website administrators can also see and edit that information.' ) . '</p>' .
114
115
			'<h2>' . __( 'What rights you have over your data' ) . '</h2>';
116
		$content .=
117
			'<p>' . __( 'In this section you should explain what rights your users have over their data and how they can invoke those rights.' ) . '</p>' . 
118
			'<p>' . __( 'If you have an account on this site, or have left comments, you can request to receive an exported file of the personal data we hold about you, including any data you have provided to us. You can also request that we erase any personal data we hold about you. This does not include any data we are obliged to keep for administrative, legal, or security purposes.' ) . '</p>';
119
120
		$content .=
121
			'<h2>' . __( 'Where we send your data' ) . '</h2>';
122
		$content .=
123
			'<p>' . __( 'In this section you should list all transfers of your site data outside the European Union and describe the means by which that data is safeguarded to European data protection standards. This could include your web hosting, cloud storage, or other third party services.' ) . '</p>' .
124
			'<p>' . __( 'European data protection law requires data about European residents which is transferred outside the European Union to be safeguarded to the same standards as if the data was in Europe. So in addition to listing where data goes, you should describe how you ensure that these standards are met either by yourself or by your third party providers, whether that is through an agreement such as Privacy Shield, model clauses in your contracts, or binding corporate rules.' ) . '</p>' . 
125
			'<p>' . __( 'Visitor comments may be checked through an automated spam detection service.' ) . '</p>';
126
127
		$content .=
128
			'<h2>' . __( 'Additional information' ) . '</h2>';
129
		$content .=
130
			'<p>' . __( 'If you use your site for commercial purposes and you engage in more complex collection or processing of personal data, you should note the following information in your privacy policy in addition to the information we have already discussed.' ) . '</p>';
131
132
		$content .=
133
			'<h3>' . __( 'How we protect your data' ) . '</h3>';
134
		$content .=
135
			'<p>' . __( 'In this section you should explain what measures you have taken to protect your users&#8217; data. This could include technical measures such as encryption; security measures such as two factor authentication; and measures such as staff training in data protection. If you have carried out a Privacy Impact Assessment, you can mention it here too.' ) . '</p>';
136
137
		$content .=
138
			'<h3>' . __( 'What data breach procedures we have in place' ) . '</h3>';
139
		$content .=
140
			'<p>' . __( 'In this section you should explain what procedures you have in place to deal with data breaches, either potential or real, such as internal reporting systems, contact mechanisms, or bug bounties.' ) . '</p>';
141
142
		$content .=
143
			'<h3>' . __( 'What third parties we receive data from' ) . '</h3>';
144
		$content .=
145
			'<p>' . __( 'If your web site receives data about users from third parties, including advertisers, this information must be included within the section of your privacy policy dealing with third party data.' ) . '</p>';
146
147
		$content .=
148
			'<h3>' . __( 'What automated decision making and/or profiling we do with user data' ) . '</h3>';
149
		$content .=
150
			'<p>' . __( 'If your web site provides a service which includes automated decision making - for example, allowing customers to apply for credit, or aggregating their data into an advertising profile - you must note that this is taking place, and include information about how that information is used, what decisions are made with that aggregated data, and what rights users have over decisions made without human intervention.' ) . '</p>';
151
152
		$content .=
153
			'<h3>' . __( 'Industry regulatory disclosure requirements' ) . '</h3>';
154
		$content .=
155
			'<p>' . __( 'If you are a member of a regulated industry, or if you are subject to additional privacy laws, you may be required to disclose that information here.' ) . '</p>' .
156
			'</div>';
157
158
		return apply_filters( 'geodir_privacy_policy_content', $content );
159
	}
160
161
	/**
162
	 * Spawn events for order cleanup.
163
	 */
164
	public function queue_cleanup_personal_data() {
165
		self::$background_process->push_to_queue( array( 'task' => 'trash_pending_posts' ) );
166
		self::$background_process->push_to_queue( array( 'task' => 'anonymize_published_posts' ) );
167
		self::$background_process->save()->dispatch();
168
	}
169
170
	/**
171
	 * Handle some custom types of data and anonymize them.
172
	 *
173
	 * @param string $anonymous Anonymized string.
174
	 * @param string $type Type of data.
175
	 * @param string $data The data being anonymized.
176
	 * @return string Anonymized string.
177
	 */
178
	public function anonymize_custom_data_types( $anonymous, $type, $data ) {
179
		switch ( $type ) {
180
			case 'city':
181
			case 'region':
182
			case 'country':
183
				$anonymous = '';
184
				break;
185
			case 'phone':
186
				$anonymous = preg_replace( '/\d/u', '0', $data );
187
				break;
188
			case 'numeric_id':
189
				$anonymous = 0;
190
				break;
191
		}
192
		return $anonymous;
193
	}
194
195
	/**
196
	 * Find and trash old posts.
197
	 *
198
	 * @since 1.6.26
199
	 * @param  int $limit Limit posts to process per batch.
200
	 * @return int Number of posts processed.
201
	 */
202
	public static function trash_pending_posts( $limit = 20 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
		return 0;
204
	}
205
206
	/**
207
	 * Anonymize old published posts.
208
	 *
209
	 * @since 1.6.26
210
	 * @param  int $limit Limit posts to process per batch.
211
	 * @return int Number of posts processed.
212
	 */
213
	public static function anonymize_published_posts( $limit = 20 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $limit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
		return 0;
215
	}
216
217
	/**
218
	 * For a given query trash all matches.
219
	 *
220
	 * @since 3.4.0
221
	 * @param array $query Query array to pass to wc_get_orders().
222
	 * @return int Count of orders that were trashed.
223
	 */
224
	protected static function trash_posts_query( $query ) {
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
		$posts = array();
226
		$count  = 0;
227
228
		if ( $posts ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $posts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
229
			foreach ( $posts as $post ) {
230
				$count ++;
231
			}
232
		}
233
234
		return $count;
235
	}
236
237
	/**
238
	 * For a given query, anonymize all matches.
239
	 *
240
	 * @since 1.6.26
241
	 * @param array $query Query array.
242
	 * @return int Count of orders that were anonymized.
243
	 */
244
	protected static function anonymize_posts_query( $query ) {
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
245
		$posts = array();
246
		$count  = 0;
247
248
		if ( $posts ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $posts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
249
			foreach ( $posts as $post ) {
250
				GeoDir_Privacy_Erasers::remove_post_personal_data( $post );
251
				$count ++;
252
			}
253
		}
254
255
		return $count;
256
	}
257
}
258
259
new GeoDir_Privacy();
260