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