Instantly share code, notes, and snippets.

Embed
What would you like to do?
Personal data exporter and eraser for Gravity Forms.
<?php
/**
* Personal data exporter and eraser for Gravity Forms.
*
* @package BJ\GravityForms\PrivacyActions
* @author bjornjohansen
* @version 0.1.1
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License version 2 (GPLv2)
*/
/**
* Register the Gravity Forms exporter.
*
* @param array $exporters Current array of data exporters.
* @return array The exporters.
*/
function bj_register_gravityforms_exporter( $exporters ) {
// Let us stay out of the way if an exporter already exists for Gravity Forms.
if ( ! array_key_exists( 'gravityforms', $exporters ) && class_exists( 'GFAPI' ) ) {
$exporters['gravityforms'] = array(
'exporter_friendly_name' => __( 'Gravity Forms', 'gravityforms' ),
'callback' => 'bj_gravityforms_exporter',
);
}
return $exporters;
}
add_filter( 'wp_privacy_personal_data_exporters', 'bj_register_gravityforms_exporter', 10, 1 );
/**
* Personal data exporter for Gravity Forms callback.
*
* @param string $email_address The email address to the individual.
* @param integer $page The exporter page.
* @return array The return array with the data and the “done” status.
*/
function bj_gravityforms_exporter( $email_address, $page = 1 ) {
$export_items = [];
$forms = GFAPI::get_forms();
$form = $forms[ $page - 1 ];
$search_criteria['field_filters'][] = [
'value' => $email_address,
];
$entries = GFAPI::get_entries( $form['id'], $search_criteria );
foreach ( $entries as $entry ) {
$data = [
[
'name' => __( 'Form', 'gravityforms' ),
'value' => esc_html( $form['title'] ),
],
];
if ( isset( $entry['source_url'] ) && strlen( $entry['source_url'] ) ) {
$data[] = [
'name' => __( 'Embed Url', 'gravityforms' ),
'value' => esc_url( $entry['source_url'] ),
];
}
if ( isset( $entry['date_created'] ) && strlen( $entry['date_created'] ) ) {
$data[] = [
'name' => __( 'Submitted on', 'gravityforms' ),
'value' => esc_html( $entry['date_created'] ),
];
}
if ( isset( $entry['ip'] ) && strlen( $entry['ip'] ) ) {
$data[] = [
'name' => __( 'User IP', 'gravityforms' ),
'value' => esc_html( $entry['ip'] ),
];
}
if ( isset( $entry['user_agent'] ) && strlen( $entry['user_agent'] ) ) {
$data[] = [
'name' => __( 'User Agent', 'gravityforms' ),
'value' => esc_html( $entry['user_agent'] ),
];
}
foreach ( $form['fields'] as $field ) {
$data[] = [
'name' => $field->label,
'value' => esc_html( $field->get_value_export( $entry ) ),
];
}
$export_items[] = [
'group_id' => 'gravityforms',
'group_label' => __( 'Forms', 'gravityforms' ),
'item_id' => 'gravityforms-' . $entry['id'],
'data' => $data,
];
}
return [
'data' => $export_items,
'done' => count( $forms ) <= $page,
];
}
/**
* Register the Gravity Forms eraser.
*
* @param array $erasers Current array of data erasers.
* @return array The erasers.
*/
function bj_register_gravityforms_eraser( $erasers ) {
// Let us stay out of the way if an eraser already exists for Gravity Forms.
if ( ! array_key_exists( 'gravityforms', $erasers ) && class_exists( 'GFAPI' ) ) {
$erasers['gravityforms'] = [
'eraser_friendly_name' => __( 'Gravity Forms', 'gravityforms' ),
'callback' => 'bj_gravityforms_eraser',
];
}
return $erasers;
}
add_filter( 'wp_privacy_personal_data_erasers', 'bj_register_gravityforms_eraser', 10, 1 );
/**
* Personal data eraser for Gravity Forms callback.
*
* @param string $email_address The email address to the individual.
* @param integer $page The eraser page.
* @return array The return status array.
*/
function bj_gravityforms_eraser( $email_address, $page = 1 ) {
$items_removed = false;
$items_retained = false;
$messages = [];
$forms = GFAPI::get_forms();
$form = $forms[ $page - 1 ];
$search_criteria['field_filters'][] = [
'value' => $email_address,
];
$entries = GFAPI::get_entries( $form['id'], $search_criteria );
foreach ( $entries as $entry ) {
$delete_res = GFAPI::delete_entry( $entry['id'] );
if ( true === $delete_res ) {
$items_removed = true;
} elseif ( is_wp_error( $delete_res ) ) {
$items_retained = true;
$messages[] = $delete_res->get_error_message();
} else {
$items_retained = true;
}
}
return [
'items_removed' => $items_removed,
'items_retained' => $items_retained,
'messages' => $messages,
'done' => count( $forms ) <= $page,
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment