PluginProbe
The GDPR Framework By Data443 / trunk
The GDPR Framework By Data443 vtrunk
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Components / WordpressComments / WordpressComments.php

WordpressComments.php in The GDPR Framework By Data443 trunk, at src/Components/WordpressComments/WordpressComments.php

216 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codelight\GDPR\Components\WordpressComments;
4
5 if ( ! defined( 'ABSPATH' ) ) exit;
6
7 use Codelight\GDPR\DataSubject\DataSubject;
8 use Codelight\GDPR\DataSubject\DataSubjectManager;
9
10 class WordpressComments {
11
12 /* @var DataSubjectManager */
13 protected $dataSubjectManager;
14
15 public function __construct( DataSubjectManager $dataSubjectManager ) {
16 $this->dataSubjectManager = $dataSubjectManager;
17
18 $this->setup();
19 }
20
21 public function setup() {
22 if ( gdpr( 'options' )->get( 'policy_page' ) ) {
23 $gdpr_check = gdpr( 'options' )->get( 'policy_page' );
24 } else {
25 $gdpr_check = get_option( 'gdpr_policy_page' );
26 }
27 if ( ! gdpr( 'options' )->get( 'comment_checkbox' ) ) {
28 if ( $gdpr_check ) {
29 add_action( 'comment_form_after_fields', array( $this, 'maybeAddCommentFormCheckbox' ) );
30 add_action( 'comment_form_logged_in_after', array( $this, 'maybeAddCommentFormCheckbox' ) );
31 add_filter( 'preprocess_comment', array( $this, 'validate' ) );
32 }
33 }
34
35 add_filter( 'gdpr/data-subject/data', array( $this, 'getExportData' ), 1, 2 );
36 add_action( 'gdpr/data-subject/delete', array( $this, 'deleteComments' ) );
37 add_action( 'gdpr/data-subject/anonymize', array( $this, 'deleteComments' ) );
38
39 }
40
41 /**
42 * Check if consent is needed
43 *
44 * @return bool
45 */
46 public function needsConsent( $email = null ) {
47 if ( $email ) {
48 $dataSubject = $this->dataSubjectManager->getByEmail( $email );
49 } else {
50 $dataSubject = $this->dataSubjectManager->getByLoggedInUser();
51 }
52
53 return ! ( $dataSubject && $dataSubject->hasConsented( 'privacy-policy' ) );
54 }
55
56 /**
57 * If consent is needed, render the checkbox
58 *
59 * @param $fields
60 */
61 public function maybeAddCommentFormCheckbox() {
62 $email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : null;
63
64 if ( ! $this->needsConsent( $email ) ) {
65 return;
66 }
67
68 $privacyPolicyUrl = get_permalink( gdpr( 'options' )->get( 'policy_page' ) );
69 add_filter( 'gdpr_custom_policy_link', 'gdprfPrivacyPolicyurl' );
70 $privacyPolicyUrl = apply_filters( 'gdpr_custom_policy_link', $privacyPolicyUrl );
71 $termsPage = gdpr( 'options' )->get( 'terms_page' );
72 if ( $termsPage ) {
73 $termsUrl = get_permalink( $termsPage );
74 } else {
75 $termsUrl = false;
76 }
77
78 echo gdpr( 'view' )->render(
79 'modules/wordpress-comments/terms-checkbox',
80 compact( 'termsUrl', 'privacyPolicyUrl' )
81 );
82 }
83
84 /**
85 * If consent is needed, validate it
86 */
87 public function validate( $commentData ) {
88 if ( is_user_logged_in() && is_admin() ) {
89 $allowedRoles = apply_filters( 'gdpr/roles/comments', array( 'administrator', 'editor', 'shop_manager' ) );
90
91 foreach ( wp_get_current_user()->roles as $userRole ) {
92 if ( in_array( $userRole, $allowedRoles ) ) {
93 return $commentData;
94 }
95 }
96 }
97
98 $email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : null;
99
100 if ( is_user_logged_in() ) {
101 if ( ! $this->needsConsent( $email ) ) {
102 return $commentData;
103 }
104 }
105
106 include_once ABSPATH . 'wp-admin/includes/plugin.php';
107 if ( ! is_plugin_active( 'jetpack/jetpack.php' ) && ! is_plugin_active( 'wpdiscuz/class.WpdiscuzCore.php' ) ) {
108 if ( ! isset( $_POST['gdpr_terms'] ) || ! $_POST['gdpr_terms'] ) {
109
110 $gdpr_error_message = '%sERROR:%s You need to accept the privacy policy to post a comment.';
111 add_filter( 'gdpr_custom_policy_error', 'gdpr_privacy_accpetance' );
112 $privacyPolicyerror = apply_filters( 'gdpr_custom_policy_error', $gdpr_error_message );
113 wp_die(
114 sprintf(
115 __( $privacyPolicyerror, 'gdpr-framework' ),
116 '<strong>',
117 '</strong>'
118 )
119 );
120 } else {
121 if ( is_user_logged_in() ) {
122 $dataSubject = $this->dataSubjectManager->getByLoggedInUser();
123 } else {
124 $dataSubject = $this->dataSubjectManager->getByEmail( $email );
125 }
126 if( isset( $_POST['gdpr-consent-until'] ) ){
127 $valid_until = esc_attr( wp_unslash( $_POST['gdpr-consent-until'] ) );
128 $dataSubject->giveConsent( 'privacy-policy', $valid_until );
129 }else{
130 $dataSubject->giveConsent( 'privacy-policy');
131 }
132
133 }
134 }
135
136 return $commentData;
137 }
138
139 /**
140 * Add comments as well as comment meta to export data
141 *
142 * @param $data
143 * @param $email
144 * @param $dataSubject
145 * @return mixed
146 */
147 public function getExportData( $data, $email ) {
148 $comments = $this->getCommentsByEmail( $email );
149
150 if ( count( $comments ) ) {
151
152 foreach ( $comments as $comment ) {
153 /* @var $comment \WP_Comment */
154
155 $commentData = array(
156 'comment_author' => $comment->comment_author,
157 'comment_author_email' => $comment->comment_author_email,
158 'comment_author_url' => $comment->comment_author_url,
159 'comment_author_IP' => $comment->comment_author_IP,
160 'comment_date' => $comment->comment_date,
161 'comment_date_gmt' => $comment->comment_date_gmt,
162 'comment_content' => $comment->comment_content,
163 'comment_approved' => $comment->comment_approved,
164 'comment_agent' => $comment->comment_agent,
165 'comment_consent' => 'privacy-policy',
166 );
167 if ( isset( $comment->comment_ID ) ) {
168 $commentMeta = get_comment_meta( $comment->comment_ID );
169 if ( ! empty( $commentMeta ) ) {
170 $commentData['comment_meta'] = $commentMeta;
171 }
172
173 $data['comments'][] = $commentData;
174 }
175 }
176 }
177
178 return $data;
179 }
180
181 public function deleteComments( $email ) {
182 $comments = $this->getCommentsByEmail( $email );
183
184 if ( ! count( $comments ) ) {
185 return;
186 }
187
188 foreach ( $comments as $comment ) {
189 /* @var $comment \WP_Comment */
190 wp_delete_comment( $comment->comment_ID, true );
191 }
192 }
193
194 /**
195 * @param $email
196 * @return array|int
197 */
198 public function getCommentsByEmail( $email ) {
199 if ( ! $email || ! is_email( $email ) ) {
200 return array();
201 }
202
203 $query = new \WP_Comment_Query();
204
205 $comments = $query->query(
206 array(
207 'author_email' => $email,
208 'include_unapproved' => true,
209 'status' => 'all',
210 )
211 );
212
213 return $comments;
214 }
215 }
216