Test Failed
Pull Request — master (#456)
by Kiran
16:45
created

GeoDir_Privacy   C

Complexity

Total Complexity 65

Size/Duplication

Total Lines 514
Duplicated Lines 43.19 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 222
loc 514
rs 5.7894
c 0
b 0
f 0
wmc 65
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 37 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
A get_personal_data_exporters() 0 23 1
D get_personal_data_exporter_key() 109 109 19
D personal_data_exporter_key() 113 113 20
B exporter_post_type() 0 19 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like GeoDir_Privacy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GeoDir_Privacy, and based on these observations, apply Extract Interface, too.

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
43
				// This hook registers GeoDirectory data exporters.
44
				$this->add_exporter( 'geodirectory-post-' . $post_type, 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...
45
46
				// This hook registers GeoDirectory data erasers.
47
				$this->add_eraser( 'geodirectory-post-' . $post_type, 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...
48
			}
49
		}
50
51
		// Cleanup orders daily - this is a callback on a daily cron event.
52
		add_action( 'geodir_cleanup_personal_data', array( $this, 'queue_cleanup_personal_data' ) );
53
54
		// Handles custom anonomization types not included in core.
55
		add_filter( 'wp_privacy_anonymize_data', array( $this, 'anonymize_custom_data_types' ), 10, 3 );
56
57
		// When this is fired, data is removed in a given order. Called from bulk actions.
58
		add_action( 'geodir_remove_post_personal_data', array( 'GeoDir_Privacy_Erasers', 'remove_post_personal_data' ) );
59
60
		// Review data
61
		add_filter( 'wp_privacy_personal_data_export_page', array( 'GeoDir_Privacy_Exporters', 'review_data_exporter' ), 10, 7 );
62
	}
63
64
	/**
65
	 * Add privacy policy content for the privacy policy page.
66
	 *
67
	 * @since 1.6.26
68
	 *
69
	 * @return string The default policy content.
70
	 */
71
	public function get_privacy_message() {
72
		$content = '';
73
74
		// Start of the suggested privacy policy text.
75
		$content .=
76
			'<div class="geodir-privacy-text">';
77
		$content .=
78
			'<h2>' . __( 'Who we are' ) . '</h2>';
79
		$content .=
80
			'<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>';
81
82
			'<h2>' . __( 'What personal data we collect and why we collect it' ) . '</h2>';
83
		$content .=
84
			'<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>' .
85
			'<p>' . __( 'You should also note any collection and retention of sensitive personal data, such as data concerning health.' ) . '</p>' .
86
			'<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>' .
87
			'<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>' .
88
			'<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>';
89
90
		$content .=
91
			'<h3>' . __( 'Posts' ) . '</h3>';
92
		$content .=
93
			'<p>' . __( 'In this subsection you should note what information is captured through posts. We have noted the data which WordPress collects by default.' ) . '</p>' . 
94
			'<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>' .
95
			'<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>';
96
97
		$content .=
98
			'<h3>' . __( 'Reviews' ) . '</h3>';
99
		$content .=
100
			'<p>' . __( 'In this subsection you should note what information is captured through reviews. We have noted the data which WordPress collects by default.' ) . '</p>' . 
101
			'<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>' .
102
			'<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>';
103
104
		$content .=
105
			'<h3>' . __( 'Media' ) . '</h3>';
106
		$content .=
107
			'<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>' . 
108
			'<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>';
109
110
		$content .=
111
			'<h2>' . __( 'How long we retain your data' ) . '</h2>';
112
		$content .=
113
			'<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>' . 
114
			'<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>' .
115
			'<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>' .
116
117
			'<h2>' . __( 'What rights you have over your data' ) . '</h2>';
118
		$content .=
119
			'<p>' . __( 'In this section you should explain what rights your users have over their data and how they can invoke those rights.' ) . '</p>' . 
120
			'<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>';
121
122
		$content .=
123
			'<h2>' . __( 'Where we send your data' ) . '</h2>';
124
		$content .=
125
			'<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>' .
126
			'<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>' . 
127
			'<p>' . __( 'Visitor comments may be checked through an automated spam detection service.' ) . '</p>';
128
129
		$content .=
130
			'<h2>' . __( 'Additional information' ) . '</h2>';
131
		$content .=
132
			'<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>';
133
134
		$content .=
135
			'<h3>' . __( 'How we protect your data' ) . '</h3>';
136
		$content .=
137
			'<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>';
138
139
		$content .=
140
			'<h3>' . __( 'What data breach procedures we have in place' ) . '</h3>';
141
		$content .=
142
			'<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>';
143
144
		$content .=
145
			'<h3>' . __( 'What third parties we receive data from' ) . '</h3>';
146
		$content .=
147
			'<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>';
148
149
		$content .=
150
			'<h3>' . __( 'What automated decision making and/or profiling we do with user data' ) . '</h3>';
151
		$content .=
152
			'<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>';
153
154
		$content .=
155
			'<h3>' . __( 'Industry regulatory disclosure requirements' ) . '</h3>';
156
		$content .=
157
			'<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>' .
158
			'</div>';
159
160
		return apply_filters( 'geodir_privacy_policy_content', $content );
161
	}
162
163
	/**
164
	 * Spawn events for order cleanup.
165
	 */
166
	public function queue_cleanup_personal_data() {
167
		self::$background_process->push_to_queue( array( 'task' => 'trash_pending_posts' ) );
168
		self::$background_process->push_to_queue( array( 'task' => 'anonymize_published_posts' ) );
169
		self::$background_process->save()->dispatch();
170
	}
171
172
	/**
173
	 * Handle some custom types of data and anonymize them.
174
	 *
175
	 * @param string $anonymous Anonymized string.
176
	 * @param string $type Type of data.
177
	 * @param string $data The data being anonymized.
178
	 * @return string Anonymized string.
179
	 */
180
	public function anonymize_custom_data_types( $anonymous, $type, $data ) {
181
		switch ( $type ) {
182
			case 'city':
183
			case 'region':
184
			case 'country':
185
				$anonymous = '';
186
				break;
187
			case 'phone':
188
				$anonymous = preg_replace( '/\d/u', '0', $data );
189
				break;
190
			case 'numeric_id':
191
				$anonymous = 0;
192
				break;
193
		}
194
		return $anonymous;
195
	}
196
197
	/**
198
	 * Find and trash old posts.
199
	 *
200
	 * @since 1.6.26
201
	 * @param  int $limit Limit posts to process per batch.
202
	 * @return int Number of posts processed.
203
	 */
204
	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...
205
		return 0;
206
	}
207
208
	/**
209
	 * Anonymize old published posts.
210
	 *
211
	 * @since 1.6.26
212
	 * @param  int $limit Limit posts to process per batch.
213
	 * @return int Number of posts processed.
214
	 */
215
	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...
216
		return 0;
217
	}
218
219
	/**
220
	 * For a given query trash all matches.
221
	 *
222
	 * @since 3.4.0
223
	 * @param array $query Query array to pass to wc_get_orders().
224
	 * @return int Count of orders that were trashed.
225
	 */
226
	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...
227
		$posts = array();
228
		$count  = 0;
229
230
		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...
231
			foreach ( $posts as $post ) {
232
				$count ++;
233
			}
234
		}
235
236
		return $count;
237
	}
238
239
	/**
240
	 * For a given query, anonymize all matches.
241
	 *
242
	 * @since 1.6.26
243
	 * @param array $query Query array.
244
	 * @return int Count of orders that were anonymized.
245
	 */
246
	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...
247
		$posts = array();
248
		$count  = 0;
249
250
		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...
251
			foreach ( $posts as $post ) {
252
				GeoDir_Privacy_Erasers::remove_post_personal_data( $post );
253
				$count ++;
254
			}
255
		}
256
257
		return $count;
258
	}
259
260
	public static function get_personal_data_exporters() {
261
		/**
262
		 * Filters the array of exporter callbacks.
263
		 *
264
		 *
265
		 * @param array $args {
266
		 *     An array of callable exporters of personal data. Default empty array.
267
		 *
268
		 *     @type array {
269
		 *         Array of personal data exporters.
270
		 *
271
		 *         @type string $callback               Callable exporter function that accepts an
272
		 *                                              email address and a page and returns an array
273
		 *                                              of name => value pairs of personal data.
274
		 *         @type string $exporter_friendly_name Translated user facing friendly name for the
275
		 *                                              exporter.
276
		 *     }
277
		 * }
278
		 */
279
		$exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
280
281
		return $exporters;
282
	}
283
284 View Code Duplication
	public static function get_personal_data_exporter_key() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
285
		if ( empty( $_POST['id'] ) ) {
286
			return false;
287
		}
288
		$request_id = (int) $_POST['id'];
289
290
		if ( $request_id < 1 ) {
291
			return false;
292
		}
293
294
		if ( ! current_user_can( 'export_others_personal_data' ) ) {
295
			return false;
296
		}
297
298
		// Get the request data.
299
		$request = wp_get_user_request_data( $request_id );
300
301
		if ( ! $request || 'export_personal_data' !== $request->action_name ) {
302
			return false;
303
		}
304
305
		$email_address = $request->email;
306
		if ( ! is_email( $email_address ) ) {
307
			return false;
308
		}
309
310
		if ( ! isset( $_POST['exporter'] ) ) {
311
			return false;
312
		}
313
		$exporter_index = (int) $_POST['exporter'];
314
315
		if ( ! isset( $_POST['page'] ) ) {
316
			return false;
317
		}
318
		$page = (int) $_POST['page'];
319
320
		$send_as_email = isset( $_POST['sendAsEmail'] ) ? ( 'true' === $_POST['sendAsEmail'] ) : false;
321
322
		/**
323
		 * Filters the array of exporter callbacks.
324
		 *
325
		 * @since 1.6.26
326
		 *
327
		 * @param array $args {
328
		 *     An array of callable exporters of personal data. Default empty array.
329
		 *
330
		 *     @type array {
331
		 *         Array of personal data exporters.
332
		 *
333
		 *         @type string $callback               Callable exporter function that accepts an
334
		 *                                              email address and a page and returns an array
335
		 *                                              of name => value pairs of personal data.
336
		 *         @type string $exporter_friendly_name Translated user facing friendly name for the
337
		 *                                              exporter.
338
		 *     }
339
		 * }
340
		 */
341
		$exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
342
343
		if ( ! is_array( $exporters ) ) {
344
			return false;
345
		}
346
347
		// Do we have any registered exporters?
348
		if ( 0 < count( $exporters ) ) {
349
			if ( $exporter_index < 1 ) {
350
				return false;
351
			}
352
353
			if ( $exporter_index > count( $exporters ) ) {
354
				return false;
355
			}
356
357
			if ( $page < 1 ) {
358
				return false;
359
			}
360
361
			$exporter_keys = array_keys( $exporters );
362
			$exporter_key  = $exporter_keys[ $exporter_index - 1 ];
363
			$exporter      = $exporters[ $exporter_key ];
364
			
365
			if ( ! is_array( $exporter ) || empty( $exporter_key ) ) {
366
				return false;
367
			}
368
			if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
369
				return false;
370
			}
371
			if ( ! array_key_exists( 'callback', $exporter ) ) {
372
				return false;
373
			}
374
		}
375
376
		/**
377
		 * Filters a page of personal data exporter.
378
		 *
379
		 * @since 1.6.26
380
		 *
381
		 * @param array  $exporter_key    The key (slug) of the exporter that provided this data.
382
		 * @param array  $exporter        The personal data for the given exporter.
383
		 * @param int    $exporter_index  The index of the exporter that provided this data.
384
		 * @param string $email_address   The email address associated with this personal data.
385
		 * @param int    $page            The page for this response.
386
		 * @param int    $request_id      The privacy request post ID associated with this request.
387
		 * @param bool   $send_as_email   Whether the final results of the export should be emailed to the user.
388
		 */
389
		$exporter_key = apply_filters( 'geodir_privacy_personal_data_exporter', $exporter_key, $exporter, $exporter_index, $email_address, $page, $request_id, $send_as_email );
0 ignored issues
show
Bug introduced by
The variable $exporter_key does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $exporter does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
390
391
		return $exporter_key;
392
	}
393
394 View Code Duplication
	public static function personal_data_exporter_key() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
395
		if ( ! wp_doing_ajax() ) {
396
			return false;
397
		}
398
399
		if ( empty( $_POST['id'] ) ) {
400
			return false;
401
		}
402
		$request_id = (int) $_POST['id'];
403
404
		if ( $request_id < 1 ) {
405
			return false;
406
		}
407
408
		if ( ! current_user_can( 'export_others_personal_data' ) ) {
409
			return false;
410
		}
411
412
		// Get the request data.
413
		$request = wp_get_user_request_data( $request_id );
414
415
		if ( ! $request || 'export_personal_data' !== $request->action_name ) {
416
			return false;
417
		}
418
419
		$email_address = $request->email;
420
		if ( ! is_email( $email_address ) ) {
421
			return false;
422
		}
423
424
		if ( ! isset( $_POST['exporter'] ) ) {
425
			return false;
426
		}
427
		$exporter_index = (int) $_POST['exporter'];
428
429
		if ( ! isset( $_POST['page'] ) ) {
430
			return false;
431
		}
432
		$page = (int) $_POST['page'];
433
434
		$send_as_email = isset( $_POST['sendAsEmail'] ) ? ( 'true' === $_POST['sendAsEmail'] ) : false;
435
436
		/**
437
		 * Filters the array of exporter callbacks.
438
		 *
439
		 * @since 1.6.26
440
		 *
441
		 * @param array $args {
442
		 *     An array of callable exporters of personal data. Default empty array.
443
		 *
444
		 *     @type array {
445
		 *         Array of personal data exporters.
446
		 *
447
		 *         @type string $callback               Callable exporter function that accepts an
448
		 *                                              email address and a page and returns an array
449
		 *                                              of name => value pairs of personal data.
450
		 *         @type string $exporter_friendly_name Translated user facing friendly name for the
451
		 *                                              exporter.
452
		 *     }
453
		 * }
454
		 */
455
		$exporters = apply_filters( 'wp_privacy_personal_data_exporters', array() );
456
457
		if ( ! is_array( $exporters ) ) {
458
			return false;
459
		}
460
461
		// Do we have any registered exporters?
462
		if ( 0 < count( $exporters ) ) {
463
			if ( $exporter_index < 1 ) {
464
				return false;
465
			}
466
467
			if ( $exporter_index > count( $exporters ) ) {
468
				return false;
469
			}
470
471
			if ( $page < 1 ) {
472
				return false;
473
			}
474
475
			$exporter_keys = array_keys( $exporters );
476
			$exporter_key  = $exporter_keys[ $exporter_index - 1 ];
477
			$exporter      = $exporters[ $exporter_key ];
478
			
479
			if ( ! is_array( $exporter ) || empty( $exporter_key ) ) {
480
				return false;
481
			}
482
			if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
483
				return false;
484
			}
485
			if ( ! array_key_exists( 'callback', $exporter ) ) {
486
				return false;
487
			}
488
		}
489
490
		/**
491
		 * Filters a page of personal data exporter.
492
		 *
493
		 * @since 1.6.26
494
		 *
495
		 * @param array  $exporter_key    The key (slug) of the exporter that provided this data.
496
		 * @param array  $exporter        The personal data for the given exporter.
497
		 * @param int    $exporter_index  The index of the exporter that provided this data.
498
		 * @param string $email_address   The email address associated with this personal data.
499
		 * @param int    $page            The page for this response.
500
		 * @param int    $request_id      The privacy request post ID associated with this request.
501
		 * @param bool   $send_as_email   Whether the final results of the export should be emailed to the user.
502
		 */
503
		$exporter_key = apply_filters( 'geodir_privacy_personal_data_exporter', $exporter_key, $exporter, $exporter_index, $email_address, $page, $request_id, $send_as_email );
0 ignored issues
show
Bug introduced by
The variable $exporter_key does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $exporter does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
504
505
		return $exporter_key;
506
	}
507
508
	public static function exporter_post_type() {
509
		$exporter_key = self::personal_data_exporter_key();
510
511
		if ( empty( $exporter_key ) ) {
512
			return false;
513
		}
514
515
		if ( strpos( $exporter_key, 'geodirectory-post-' ) !== 0 ) {
516
			return false;
517
		}
518
519
		$post_type = str_replace( 'geodirectory-post-', '', $exporter_key );
520
521
		if ( $post_type != '' && in_array( $post_type, geodir_get_posttypes() ) ) {
522
			return $post_type;
523
		}
524
525
		return false;
526
	}
527
}
528
529
new GeoDir_Privacy();
530