Test Failed
Push — master ( b6268a...3d95f3 )
by Stiofan
59s queued 10s
created

GeoDir_Privacy_Erasers   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C review_data_eraser() 0 80 8
B reviews_by_author() 0 24 5
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