__( 'Nodebook', 'nodebook' ), 'callback' => array( __CLASS__, 'export' ), ); return $exporters; } public static function register_eraser( $erasers ) { $erasers['nodebook'] = array( 'eraser_friendly_name' => __( 'Nodebook', 'nodebook' ), 'callback' => array( __CLASS__, 'erase' ), ); return $erasers; } const PER_PAGE = 500; // Timeline entries per export page. /** * Personal-data export, in pages: page 1 has the contact itself, then the whole timeline 500 entries * at a time, with everything stored for each entry (messages, form fields, notes, consent wording). */ public static function export( $email, $page = 1 ) { global $wpdb; $page = max( 1, (int) $page ); $c = Nodebook_Contacts::find_by_email( $email ); if ( ! $c ) { return array( 'data' => array(), 'done' => true, ); } $data = 1 === $page ? array_merge( array( self::contact_group( $c ) ), self::deals( (int) $c['id'] ) ) : array(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- the plugin's own table, read in pages. $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE contact_id = %d ORDER BY id ASC LIMIT %d OFFSET %d', Nodebook_Activities::table(), $c['id'], self::PER_PAGE, ( $page - 1 ) * self::PER_PAGE ), ARRAY_A ); $types = Nodebook_Activities::types(); foreach ( $rows as $a ) { $item = array( array( 'name' => __( 'Date (UTC)', 'nodebook' ), 'value' => $a['created_at'], ), array( 'name' => __( 'Type', 'nodebook' ), 'value' => $types[ $a['type'] ] ?? $a['type'], ), array( 'name' => __( 'Description', 'nodebook' ), 'value' => $a['title'], ), ); if ( '' !== (string) $a['url'] ) { $item[] = array( 'name' => __( 'URL', 'nodebook' ), 'value' => (string) $a['url'], ); } $details = self::details( json_decode( (string) $a['data'], true ) ); if ( '' !== $details ) { $item[] = array( 'name' => __( 'Details', 'nodebook' ), 'value' => $details, ); } $data[] = array( 'group_id' => 'nodebook-activity', 'group_label' => __( 'CRM activity', 'nodebook' ), 'item_id' => 'nodebook-activity-' . $a['id'], 'data' => $item, ); } return array( 'data' => $data, 'done' => count( $rows ) < self::PER_PAGE, ); } /** Deals of the contact: what, value, stage or outcome, dates, notes. */ private static function deals( $contact_id ) { $out = array(); foreach ( Nodebook_Deals::query( array( 'contact_id' => $contact_id, 'outcome' => 'all', 'order' => 'created', 'limit' => 500 ) ) as $d ) { $state = '' === $d['outcome'] ? Nodebook_Deals::stage_name( $d ) : ( 'won' === $d['outcome'] ? __( 'Won', 'nodebook' ) : __( 'Lost', 'nodebook' ) ); $item = array( array( 'name' => __( 'Deal', 'nodebook' ), 'value' => $d['title'], ), array( 'name' => __( 'Value', 'nodebook' ), 'value' => trim( $d['currency'] . ' ' . number_format( (float) $d['value'], 2, '.', '' ) ), ), array( 'name' => __( 'Stage', 'nodebook' ), 'value' => $state . ( '' !== $d['lost_reason'] ? ' (' . $d['lost_reason'] . ')' : '' ), ), array( 'name' => __( 'Created (UTC)', 'nodebook' ), 'value' => $d['created_at'], ), ); if ( $d['expected_close'] ) { $item[] = array( 'name' => __( 'Expected close', 'nodebook' ), 'value' => $d['expected_close'], ); } if ( '' !== (string) $d['notes'] ) { $item[] = array( 'name' => __( 'Notes', 'nodebook' ), 'value' => (string) $d['notes'], ); } foreach ( Nodebook_Props::values( 'deal', (int) $d['id'] ) as $k => $v ) { $def = Nodebook_Props::get( 'deal', $k ); if ( $def && '' !== $v ) { $item[] = array( 'name' => $def['label'], 'value' => Nodebook_Props::display( $def, $v ), ); } } $out[] = array( 'group_id' => 'nodebook-deals', 'group_label' => __( 'CRM deals', 'nodebook' ), 'item_id' => 'nodebook-deal-' . $d['id'], 'data' => $item, ); } return $out; } /** The contact record: fields, your fields, other form fields, consent, other addresses, segments. */ private static function contact_group( array $c ) { $props = array(); foreach ( Nodebook_Fields::contact() as $key => $def ) { if ( 'company' === $key ) { $co = $c['company_id'] ? Nodebook_Companies::get( $c['company_id'] ) : null; $value = $co ? $co['name'] : ''; } else { $value = isset( $c[ $key ] ) ? (string) $c[ $key ] : ''; } if ( '' !== $value ) { $props[] = array( 'name' => $def['label'], 'value' => $value, ); } } $vals = Nodebook_Props::values( 'contact', (int) $c['id'] ); foreach ( Nodebook_Props::defs( 'contact' ) as $key => $def ) { if ( isset( $vals[ $key ] ) && '' !== $vals[ $key ] ) { $props[] = array( 'name' => $def['label'], 'value' => Nodebook_Props::display( $def, $vals[ $key ] ), ); } } foreach ( $c['meta'] as $k => $v ) { $props[] = array( 'name' => $k, 'value' => $v, ); } if ( '' !== (string) ( $c['email_status'] ?? '' ) ) { $props[] = array( 'name' => __( 'Email consent', 'nodebook' ), 'value' => trim( Nodebook_Subscription::statuses()[ $c['email_status'] ] . ' · ' . $c['email_status_at'] . ' UTC · ' . $c['consent_source'], ' ·' ), ); } if ( '' !== trim( (string) ( $c['pinned'] ?? '' ) ) ) { $props[] = array( 'name' => __( 'Pinned note', 'nodebook' ), 'value' => (string) $c['pinned'], ); } $emails = Nodebook_Contacts::emails( $c['id'] ); if ( count( $emails ) > 1 ) { $props[] = array( 'name' => __( 'Other email addresses', 'nodebook' ), 'value' => implode( ', ', array_slice( $emails, 1 ) ), ); } $segs = wp_list_pluck( Nodebook_Segments::for_contact( $c['id'] ), 'name' ); if ( $segs ) { $props[] = array( 'name' => __( 'Lists', 'nodebook' ), 'value' => implode( ', ', $segs ), ); } return array( 'group_id' => 'nodebook-contact', 'group_label' => __( 'CRM contact', 'nodebook' ), 'item_id' => 'nodebook-contact-' . $c['id'], 'data' => $props, ); } /** Everything stored with a timeline entry, as readable "key: value" lines. */ private static function details( $d, $prefix = '', $depth = 0 ) { if ( ! is_array( $d ) || $depth > 3 ) { return ''; } $lines = array(); foreach ( $d as $k => $v ) { $label = '' !== $prefix ? $prefix . ( is_int( $k ) ? ' ' . ( $k + 1 ) : ' › ' . $k ) : (string) $k; if ( is_array( $v ) ) { $sub = self::details( $v, $label, $depth + 1 ); if ( '' !== $sub ) { $lines[] = $sub; } } elseif ( is_bool( $v ) ) { $lines[] = $label . ': ' . ( $v ? __( 'yes', 'nodebook' ) : __( 'no', 'nodebook' ) ); } elseif ( null !== $v && '' !== (string) $v ) { $lines[] = $label . ': ' . (string) $v; } } return implode( "\n", $lines ); } public static function erase( $email, $page = 1 ) { $c = Nodebook_Contacts::find_by_email( $email ); if ( ! $c ) { return array( 'items_removed' => false, 'items_retained' => false, 'messages' => array(), 'done' => true ); } Nodebook_Contacts::delete( array( $c['id'] ) ); return array( 'items_removed' => true, 'items_retained' => false, 'messages' => array(), 'done' => true ); } public static function policy_text() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $text = '

' . __( 'When you submit a form, register or place an order on this site, we store your contact details and the information you submit in our customer relationship database. We also remember how you first arrived on this site (for example a search engine, an ad or a newsletter link) in a first-party cookie without an identifier (nodebook_src, valid for one year), and add it to your record when you identify yourself. After that, a first-party cookie (nodebook_vid, valid for one year) lets us recognise your later visits and record which pages you view, so we can follow up on your enquiry or order. This data stays on this website and is not shared with third parties by the CRM. You can request an export or deletion of this data at any time.', 'nodebook' ) . '

'; wp_add_privacy_policy_content( __( 'Nodebook', 'nodebook' ), wp_kses_post( wpautop( $text, false ) ) ); } }