Test Failed
Pull Request — master (#460)
by Kiran
18:13
created

GeoDir_Privacy_Erasers::review_data_eraser()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 80
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 42
nc 7
nop 2
dl 0
loc 80
rs 6.0132
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Personal data erasers.
4
 *
5
 * @since 1.6.26
6
 * @package GeoDirectory
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * GeoDir_Privacy_Erasers Class.
13
 */
14
class GeoDir_Privacy_Erasers {
15
16
	/**
17
	 * Erases personal data associated with an email address from the reviews table.
18
	 *
19
	 * @since 1.6.26
20
	 *
21
	 * @param  string $email_address The review author email address.
22
	 * @param  int    $page          Review page.
23
	 * @return array
24
	 */
25
	public static function review_data_eraser( $email_address, $page ) {
26
		global $wpdb;
27
28
		$response = array(
29
			'items_removed'  => false,
30
			'items_retained' => false,
31
			'messages'       => array(),
32
			'done'           => true,
33
		);
34
35
		if ( empty( $email_address ) ) {
36
			return $response;
37
		}
38
39
		$page           = (int) $page;
40
		$items_removed  = false;
41
		$items_retained = false;
42
43
		$reviews = self::reviews_by_author( $email_address, $page );
44
45
		if ( empty( $reviews ) ) {
46
			return $response;
47
		}
48
49
		$messages    = array();
50
51
		foreach ( $reviews as $review ) {
52
			$anonymized_review                         		= array();
53
			$anonymized_review['user_id']               	= 0;
54
			$anonymized_review['rating_ip']    				= wp_privacy_anonymize_data( 'ip', $review->rating_ip );
55
56
			$review_id = (int) $review->id;
57
58
			/**
59
			 * Filters whether to anonymize the review.
60
			 *
61
			 * @since 1.6.26
62
			 *
63
			 * @param bool|string                    Whether to apply the review anonymization (bool).
64
			 *                                       Custom prevention message (string). Default true.
65
			 * @param object 	 $review             Review object.
66
			 * @param array      $anonymized_review  Anonymized review data.
67
			 */
68
			$anon_message = apply_filters( 'geodir_anonymize_post_review', true, $review, $anonymized_review );
69
70
			if ( true !== $anon_message ) {
71
				if ( $anon_message && is_string( $anon_message ) ) {
72
					$messages[] = esc_html( $anon_message );
73
				} else {
74
					/* translators: %d: Review ID */
75
					$messages[] = sprintf( __( 'Review %d contains personal data but could not be anonymized.', 'geodirectory' ), $review_id );
76
				}
77
78
				$items_retained = true;
79
80
				continue;
81
			}
82
83
			$args = array(
84
				'id' => $review_id,
85
			);
86
87
			$updated = $wpdb->update( GEODIR_REVIEW_TABLE, $anonymized_review, $args );
88
89
			if ( $updated ) {
90
				$items_removed = true;
91
			} else {
92
				$items_retained = true;
93
			}
94
		}
95
96
		$done = count( $reviews ) < $number;
0 ignored issues
show
Bug introduced by
The variable $number does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
97
98
		return array(
99
			'items_removed'  => $items_removed,
100
			'items_retained' => $items_retained,
101
			'messages'       => $messages,
102
			'done'           => $done,
103
		);
104
	}
105
106
	public static function reviews_by_author( $email_address, $page, $posts_per_page = 10 ) {
107
		global $wpdb;
108
109
		if ( empty( $email_address ) || empty( $page ) ) {
110
			return array();
111
		}
112
113
		$user = get_user_by( 'email', $email_address );
114
		if ( empty( $user ) ) {
115
			return array();
116
		}
117
118
		if ( absint( $page ) < 1 ) {
119
			$page = 1;
120
		}
121
122
		$limit = absint( ( $page - 1 ) * $posts_per_page ) . ", " . $posts_per_page;
123
			
124
		$query = $wpdb->prepare( "SELECT * FROM " . GEODIR_REVIEW_TABLE . " WHERE user_id = %d ORDER BY comment_id ASC LIMIT " . $limit, array( $user->ID ) );
125
126
		$reviews = $wpdb->get_results( $query );
127
128
		return apply_filters( 'geodir_privacy_review_data_eraser_reviews', $reviews, $email_address, $user, $page );
129
	}
130
}
131