<?php

namespace Simple_Giveaways;

/**
 * Class GA_GDPR
 * Register various functions and outputs for GDPR WP Tools.
 */
class GA_GDPR {

	/**
	 * GA_GDPR constructor.
	 */
	public function __construct() {
		global $wp_version;

		add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporter' ) );
		add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_erasers' ) );

		if ( version_compare( $wp_version, '5.7', '>=' ) ) {
			add_action( 'admin_init', array( $this, 'add_privacy_content' ) );
		} else {
			add_filter( 'wp_get_default_privacy_policy_content', array( $this, 'privacy_content' ), 20 );
		}
	}

	/**
	 * Add Privacy Content.
	 */
	public function add_privacy_content() {
		wp_add_privacy_policy_content( 'Simple Giveaways', $this->get_privacy_content() );
	}

	/**
	 * Adding Privacy Content.
	 */
	public function privacy_content( $content ) {
		$content .= $this->get_privacy_content();
		return $content;
	}

	private function get_privacy_content() {
		return '<h2>' . __( 'Giveaways', 'giveasap' ) . '</h2>' .
			   '<p>' . __( 'This is just a helper text and you should change it accordingly. Please be descriptive and add everything you can to it to define how your store and manipulate giveaway data.', 'giveasap' ) . '</p>' .
			   '<p>' . __( 'When running giveaways, we are collecting your email address so you can collect entries and grow your chance of winning.', 'giveasap' ) . '</p>' .
			   '<p>' . __( 'Email that are saved are also distributed to a 3rd party service: MailChimp. We are using MailChimp to keep your email in one place and contact you after winning or losing.', 'giveasap' ) . '</p>';

	}

	/**
	 * Register exporter.
	 *
	 * @param $exporters
	 *
	 * @return array
	 */
	public function register_exporter( $exporters ) {
		$exporters['simple-giveaways'] = array(
			'exporter_friendly_name' => __( 'Giveaways', 'giveasap' ),
			'callback'               => array( $this, 'exporter_cb' ),
		);
		return $exporters;
	}

	/**
	 * Register exporter.
	 *
	 * @param $exporters
	 *
	 * @return array
	 */
	public function register_erasers( $exporters ) {
		$exporters['simple-giveaways'] = array(
			'eraser_friendly_name' => __( 'Giveaways', 'giveasap' ),
			'callback'             => array( $this, 'eraser_cb' ),
		);
		return $exporters;
	}

	/**
	 * Get all subscribers by email.
	 *
	 * @param $email
	 *
	 * @return array|null|object
	 */
	private function get_subscribers_by_email( $email ) {
		global $wpdb;

		$users = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->giveasap_entries . ' WHERE email = %s', $email ) );

		return $users;
	}

	/**
	 * Get all subscribers by email.
	 *
	 * @param $email
	 *
	 * @return array|null|object
	 */
	private function delete_subscribers_by_email( $email ) {
		global $wpdb;

		$res = $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->giveasap_entries . ' WHERE email = %s', $email ) );

		return $res;
	}

	/**
	 * @param     $email_address
	 * @param int           $page
	 *
	 * @return array
	 */
	public function exporter_cb( $email_address, $page = 1 ) {
		$export_items = array();

		$entries = $this->get_subscribers_by_email( $email_address );

		if ( $entries ) {

			$group_id = 'simple-giveaways';

			$group_label = __( 'Giveaways', 'giveasap' );

			foreach ( $entries as $entry ) {
				$item_id = 'giveasap-' . $entry->id;
				$data    = array();

				$data[] = array(
					'name'  => __( 'Giveaway Name', 'giveasap' ),
					'value' => get_the_title( $entry->post_id ),
				);

				$data[] = array(
					'name'  => __( 'Giveaway URL', 'giveasap' ),
					'value' => get_permalink( $entry->post_id ),
				);

				$data[] = array(
					'name'  => __( 'Entries Earned', 'giveasap' ),
					'value' => $entry->entries,
				);

				$export_items[] = array(
					'group_id'    => $group_id,
					'group_label' => $group_label,
					'item_id'     => $item_id,
					'data'        => $data,
				);
			}
		}

		return array(
			'data' => $export_items,
			'done' => true,
		);
	}

	/**
	 * @param     $email_address
	 * @param int           $page
	 */
	public function eraser_cb( $email_address, $page = 1 ) {
		if ( empty( $email_address ) ) {
			return array(
				'items_removed'  => false,
				'items_retained' => false,
				'messages'       => array(),
				'done'           => true,
			);
		}

		$entries = $this->get_subscribers_by_email( $email_address );

		if ( ! $entries ) {
			return array(
				'items_removed'  => false,
				'items_retained' => false,
				'messages'       => array(),
				'done'           => true,
			);
		}

		// We have some, let's delete all.
		$delete         = $this->delete_subscribers_by_email( $email_address );
		$messages       = array();
		$items_removed  = false;
		$items_retained = false;
		if ( false === $delete ) {
			$messages[]     = __( 'Something went wrong with deleting the email', 'giveasap' );
			$items_retained = true;
		} else {
			$items_removed = true;
		}

		return array(
			'items_removed'  => $items_removed,
			'items_retained' => $items_retained,
			'messages'       => $messages,
			'done'           => true,
		);
	}
}

new GA_GDPR();
