| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmPersonalData { |
| 7 |
|
| 8 |
private $limit = 200; |
| 9 |
|
| 10 |
private $page = 1; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_data_eraser' ) ); |
| 14 |
add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporter' ) ); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Add options to the WordPress personal data exporter |
| 19 |
* |
| 20 |
* @since 3.02 |
| 21 |
* |
| 22 |
* @param array $exporters |
| 23 |
* |
| 24 |
* @return array |
| 25 |
*/ |
| 26 |
public function register_exporter( $exporters ) { |
| 27 |
$exporters['formidable-exporter'] = array( |
| 28 |
'exporter_friendly_name' => __( 'Formidable Forms Exporter', 'formidable' ), |
| 29 |
'callback' => array( $this, 'export_data' ), |
| 30 |
); |
| 31 |
|
| 32 |
return $exporters; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Add options to the WordPress personal data eraser |
| 37 |
* |
| 38 |
* @since 3.02 |
| 39 |
* |
| 40 |
* @param array $erasers |
| 41 |
* |
| 42 |
* @return array |
| 43 |
*/ |
| 44 |
public function register_data_eraser( $erasers ) { |
| 45 |
$erasers['formidable-eraser'] = array( |
| 46 |
'eraser_friendly_name' => __( 'Formidable Forms entries', 'formidable' ), |
| 47 |
'callback' => array( $this, 'erase_data' ), |
| 48 |
); |
| 49 |
|
| 50 |
return $erasers; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function export_data( $email, $page = 1 ) { |
| 57 |
$this->page = absint( $page ); |
| 58 |
|
| 59 |
$data_to_export = array( |
| 60 |
'data' => array(), |
| 61 |
'done' => true, |
| 62 |
); |
| 63 |
|
| 64 |
$entries = $this->get_user_entries( $email ); |
| 65 |
if ( empty( $entries ) ) { |
| 66 |
return $data_to_export; |
| 67 |
} |
| 68 |
|
| 69 |
foreach ( (array) $entries as $entry ) { |
| 70 |
$data_to_export['data'][] = array( |
| 71 |
'group_id' => 'formidable', |
| 72 |
'group_label' => __( 'Form Submissions', 'formidable' ), |
| 73 |
'item_id' => esc_attr( 'entry-' . $entry ), |
| 74 |
'data' => $this->prepare_entry_data( $entry ), |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
$data_to_export['done'] = count( $entries ) < $this->limit; |
| 79 |
|
| 80 |
return $data_to_export; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function erase_data( $email, $page = 1 ) { |
| 87 |
$data_removed = array( |
| 88 |
'items_removed' => false, |
| 89 |
'items_retained' => false, |
| 90 |
'messages' => array(), |
| 91 |
'done' => true, |
| 92 |
); |
| 93 |
|
| 94 |
if ( empty( $email ) ) { |
| 95 |
return $data_removed; |
| 96 |
} |
| 97 |
|
| 98 |
$this->page = absint( $page ); |
| 99 |
$entries = $this->get_user_entries( $email ); |
| 100 |
if ( empty( $entries ) ) { |
| 101 |
return $data_removed; |
| 102 |
} |
| 103 |
|
| 104 |
// TODO: Add an option to anonymize the entries with wp_privacy_anonymize_data( 'email', 'e@e.com' ); |
| 105 |
|
| 106 |
foreach ( (array) $entries as $entry ) { |
| 107 |
$removed = FrmEntry::destroy( $entry ); |
| 108 |
|
| 109 |
if ( $removed ) { |
| 110 |
$data_removed['items_removed'] = true; |
| 111 |
} else { |
| 112 |
$data_removed['items_retained'] = true; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$data_removed['done'] = count( $entries ) < $this->limit; |
| 117 |
|
| 118 |
return $data_removed; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Get all entries that were submitted by this user while logged in, |
| 123 |
* or with a field value that matches the email address |
| 124 |
* |
| 125 |
* @param string $email |
| 126 |
* |
| 127 |
* @return array of entry ids |
| 128 |
*/ |
| 129 |
private function get_user_entries( $email ) { |
| 130 |
$query_args = array( |
| 131 |
'order_by' => 'item_id ASC', |
| 132 |
'limit' => $this->get_current_page(), |
| 133 |
); |
| 134 |
|
| 135 |
$user = get_user_by( 'email', $email ); |
| 136 |
|
| 137 |
$entries_by_email = FrmDb::get_col( 'frm_item_metas', array( 'meta_value' => $email ), 'item_id', $query_args ); |
| 138 |
|
| 139 |
if ( empty( $user ) ) { |
| 140 |
// no matching user, so return the entry ids we have |
| 141 |
return $entries_by_email; |
| 142 |
} |
| 143 |
|
| 144 |
$query_args['order_by'] = 'id ASC'; |
| 145 |
|
| 146 |
$entries_by_user = FrmDb::get_col( 'frm_items', array( 'user_id' => $user->ID ), 'id', $query_args ); |
| 147 |
|
| 148 |
$entry_ids = array_merge( (array) $entries_by_user, (array) $entries_by_email ); |
| 149 |
$entry_ids = array_unique( array_filter( $entry_ids ) ); |
| 150 |
|
| 151 |
return $entry_ids; |
| 152 |
} |
| 153 |
|
| 154 |
private function get_current_page() { |
| 155 |
$start = ( $this->page - 1 ) * $this->limit; |
| 156 |
|
| 157 |
return FrmDb::esc_limit( $start . ',' . $this->limit ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* @param int $entry |
| 162 |
* |
| 163 |
* @return array |
| 164 |
*/ |
| 165 |
private function prepare_entry_data( $entry ) { |
| 166 |
$entry = FrmEntry::getOne( $entry, true ); |
| 167 |
|
| 168 |
$entry_data = array(); |
| 169 |
foreach ( $entry->metas as $field_id => $meta ) { |
| 170 |
$field = FrmField::getOne( $field_id ); |
| 171 |
|
| 172 |
$entry_data[] = array( |
| 173 |
'name' => $field->name, |
| 174 |
'value' => FrmFieldsHelper::get_unfiltered_display_value( |
| 175 |
array( |
| 176 |
'field' => $field, |
| 177 |
'value' => $meta, |
| 178 |
) |
| 179 |
), |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
return $entry_data; |
| 184 |
} |
| 185 |
} |
| 186 |
|