<?php
/**
 * Download Magnet
 *
 * Main file for the plugin.
 *
 * @category Plugins
 * @package  WordPress
 * @author   Peachy Sofware LLC
 */

/**
 * PSDM_DownloadMagnet
 *
 * Class for the plugin.
 */
class PSDM_DownloadMagnet {

	/**
	 * PSDM_DownloadMagnet class variables.
	 *
	 * @var $psdm_att
	 */
	private $psdm_att;
	/**
	 * PSDM_DownloadMagnet class variables.
	 *
	 * @var array
	 */
	private array $psdm_buttons = array();
	/**
	 * PSDM_DownloadMagnet constructor.
	 */
	public function __construct() {
		if ( ! class_exists( 'PSDM_List_Table' ) ) {
			include_once plugin_dir_path( __FILE__ ) . 'class-psdm-list-table.php';
		}

		add_action( 'init', array( $this, 'psdm_register_post' ) );
		add_action( 'add_meta_boxes', array( $this, 'psdm_add_metabox' ) );
		add_filter( 'gettext', array( $this, 'psdm_title_change' ) );
		add_action( 'save_post_psdmcontent', array( $this, 'psdm_save_post' ) );
		add_action( 'admin_menu', array( $this, 'psdm_options_page' ) );
		add_action( 'admin_head', array( $this, 'psdm_remove_submenu' ) );
		add_shortcode( 'downloadmagnet', array( $this, 'psdm_show' ) );
		add_action( 'admin_init', array( $this, 'psdm_register_settings' ) );
		add_action( 'admin_init', array( $this, 'psdm_process_export' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'psdm_load_extra_cssjs' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'psdm_register_modal' ), 20 );
		add_action( 'admin_head', array( $this, 'psdm_table_widths' ) );
		add_action( 'wp_ajax_nopriv_psdm_download_file', array( $this, 'psdm_filedownload' ) );
		add_action( 'wp_ajax_psdm_download_file', array( $this, 'psdm_filedownload' ) );
		add_filter( 'manage_psdmcontent_posts_columns', array( $this, 'psdm_table_head' ) );
		add_filter( 'views_edit-psdmcontent', array( $this, 'psdm_load_message' ) );
		add_filter( 'post_row_actions', array( $this, 'psdm_remove_quick_edit' ), 10, 2 );
		add_action(
			'manage_psdmcontent_posts_custom_column',
			array(
				$this,
				'psdm_event_table_content',
			),
			10,
			2
		);
		$this->psdm_buttons = array();
	}//end __construct()

	/**
	 * Register the files needed for the modal dialog.
	 *
	 * @param string $actions The actions.
	 * @param string $post The current post.
	 */
	public function psdm_remove_quick_edit( $actions, $post ) {
		if ( is_admin() && 'psdmcontent' === get_current_screen()->post_type ) {
			unset( $actions['inline hide-if-no-js'] );
		}
		return $actions;
	}//end psdm_remove_quick_edit()


	/**
	 * Register the files needed for the modal dialog.
	 */
	public function psdm_register_modal() {
		wp_register_style(
			'psdownloadcss',
			plugin_dir_url( __FILE__ ) . 'css/psdm_modal.css',
			'2.0.2',
			'all'
		);
		wp_enqueue_style( 'psdownloadcss' );
		wp_register_script(
			'psdownloadmagnetfile',
			plugin_dir_url( __FILE__ ) . 'js/psdm_public.js',
			array( 'jquery' ),
			'2.0.2',
			true
		);
		wp_enqueue_script( 'psdownloadmagnetfile' );
	}//end psdm_register_modal()


	/**
	 * Register the post type.
	 */
	public function psdm_register_post() {
		$psdm_welcome_message = '<b>Want to add your first download? Click the "Add New Download Magnet" button above. For a video tutorial, <a href="https://peachysoftware.com/docs/video-tutorial/" target="_blank">click here.</a></b>';
		register_post_type(
			'psdmcontent',
			array(
				'labels'              => array(
					'name'               => 'Download Magnet',
					'edit_item'          => 'Edit Download Magnet',
					'add_new_item'       => 'Add New Download Magnet',
					'add_new'            => 'Add New Download Magnetxs',
					'not_found'          => $psdm_welcome_message,
					'not_found_in_trash' => 'No Download Magnet\'s found in trash',
				),
				'public'              => false,
				'supports'            => array( 'title' ),
				'publicly_queryable'  => false,
				'show_ui'             => true,
				'show_in_rest'        => true,
				'exclude_from_search' => true,
				'show_in_nav_menus'   => false,
				'has_archive'         => false,
				'rewrite'             => false,
				'menu_position'       => 20,
				'menu_icon'           => 'dashicons-download',
			)
		);
		$psdm_blockpath = realpath( __DIR__ . '/..' );
		register_block_type( $psdm_blockpath . '/build' );
	}//end psdm_register_post()


	/**
	 * Add content to the edit posts page.
	 *
	 * @param array $views views.
	 */
	public function psdm_load_message( $views ) {
		$psdm_logo     = plugin_dir_url( __FILE__ ) . 'images/dmlogo150.png';
		$psdm_help     = plugin_dir_url( __FILE__ ) . 'images/dmv2welcome.png';
		$psdm_feedback = plugin_dir_url( __FILE__ ) . 'images/feedback150.png';
		$psdm_propak   = plugin_dir_url( __FILE__ ) . 'images/email_propak.png';
		?>
		<div class="notice notice-info">
			<table>
				<tr>
					<td class="psdm_td">
						<a href="https://peachysoftware.com/docs-category/downloadmagnet/" target="_blank">
							<img src="<?php echo esc_url( $psdm_help ); ?>" alt="Image showing need help" width="461px" height="150px">
						</a>
					</td>
					<td class="psdm_td">
					</td>
				</tr>
			</table>
			<p>Not sure where to start? Follow our video tutorial <a href="https://peachysoftware.com/docs/video-tutorial/" target="_blank">here</a></p>
		</div>
		<?php
			return $views;
	}//end psdm_load_message()


	/**
	 * Add the options menus.
	 */
	public function psdm_options_page() {
		add_submenu_page(
			'edit.php?post_type=psdmcontent',
			'Downloaded Files',
			'Downloads',
			'manage_options',
			'psdownload_downloaded_files',
			array(
				$this,
				'psdm_downloaded_page_html',
			)
		);
		add_submenu_page(
			'edit.php?post_type=psdmcontent',
			'Export Lists',
			'Export',
			'manage_options',
			'psdownload_export_list',
			array(
				$this,
				'psdm_export_page_html',
			)
		);
		add_submenu_page(
			'edit.php?post_type=psdmcontent',
			'Download Magnet Settings',
			'Settings',
			'manage_options',
			'psdm_options',
			array(
				$this,
				'psdm_options_page_html',
			)
		);
		add_submenu_page(
			'edit.php?post_type=psdmcontent',
			'Download Magnet Welcome',
			'Welcome',
			'manage_options',
			'psdm_welcome',
			array(
				$this,
				'psdm_welcome_html',
			)
		);
	}//end psdm_options_page()


	/**
	 * Remove the welcome page for friendlier onboarding.
	 */
	public function psdm_remove_submenu() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		remove_submenu_page( 'edit.php?post_type=psdmcontent', 'psdm_welcome' );
	}

	/**
	 * Add the metaboxes.
	 */
	public function psdm_add_metabox() {
		add_meta_box(
			'ps_dm',
			'Download Magnet Entry',
			array(
				$this,
				'psdm_add_field',
			),
			'psdmcontent'
		);
	}//end psdm_add_metabox()


	/**
	 * Add the fields to the metabox.
	 *
	 * @param array $post The post object.
	 */
	public function psdm_add_options( $post ) {
		$input = json_decode( get_post_meta( $post->ID, 'psdmcontent', true ), true );
		if ( isset( $input['psdm_permission'] ) ) {
			if ( 'yes' !== $input['psdm_permission'] ) {
				$input['psdm_permission'] = 'no';
			}
		} else {
			$input['psdm_permission'] = 'no';
		}
		$permission_checked = '';
		if ( 'yes' === $input['psdm_permission'] ) {
			$permission_checked = 'checked';
		}

		if ( isset( $input['psdm_name_required'] ) ) {
			if ( 'yes' !== $input['psdm_name_required'] ) {
				$input['psdm_name_required'] = 'no';
			}
		} else {
			$input['psdm_name_required'] = 'no';
		}

		if ( isset( $input['psdm_backcolor'] ) ) {
			$psdm_bgcolor = $input['psdm_backcolor'];
		} else {
			$psdm_bgcolor = '#0000cd';
		}

		if ( isset( $input['psdm_forecolor'] ) ) {
			$psdm_color = $input['psdm_forecolor'];
		} else {
			$psdm_color = '#ffffff';
		}

		if ( isset( $input['psdm_bordercolor'] ) ) {
			$psdm_bordercolor = $input['psdm_bordercolor'];
		} else {
			$psdm_bordercolor = '#000000';
		}

		if ( isset( $input['psdm_borderwidth'] ) ) {
			$psdm_borderwidth = $input['psdm_borderwidth'];
		} else {
			$psdm_borderwidth = 2;
		}

		if ( isset( $input['psdm_borderradius'] ) ) {
			$psdm_borderradius = $input['psdm_borderradius'];
		} else {
			$psdm_borderradius = 10;
		}

		$name_checked = '';
		if ( 'yes' === $input['psdm_name_required'] ) {
			$name_checked = 'checked';
		}

		if ( isset( $input['psdm_bypass'] ) ) {
			if ( 'yes' !== $input['psdm_bypass'] ) {
				$input['psdm_bypass'] = 'no';
			}
		} else {
			$input['psdm_bypass'] = 'no';
		}

		$bypass_checked = '';
		if ( 'yes' === $input['psdm_bypass'] ) {
			$bypass_checked = 'checked';
		}

		if ( isset( $input['psdm_permission_text'] ) ) {
			$input['psdm_permission_text'] = $input['psdm_permission_text'];
		} else {
			$input['psdm_permission_text'] = 'By downloading you give permission to receive emails from us.';
		}

		if ( isset( $input['psdm_download_text'] ) ) {
			$input['psdm_download_text'] = $input['psdm_download_text'];
		} else {
			$input['psdm_download_text'] = 'Download';
		}

		$psdm_permission_text = $input['psdm_permission_text'];
		if ( '' === $psdm_permission_text ) {
			$psdm_permission_text = 'By downloading you give permission to receive emails from us.';
		}

		$psdm_download_text = $input['psdm_download_text'];
		if ( '' === $psdm_download_text ) {
			$psdm_download_text = 'Download';
		}

		$psdm_do_not_use_wp_footer = '';
		if ( isset( $input['psdm_footer_no'] ) ) {
			if ( 'yes' === $input['psdm_footer_no'] ) {
				$psdm_do_not_use_wp_footer = 'checked';
			}
		}

		$psdm_uuid = get_post_meta( $post->ID, '_psdm_uuid', true );
		if ( '' === $psdm_uuid || is_null( $psdm_uuid ) ) {
			$psdm_uuid = wp_generate_uuid4();
		}
		?>
		<br/>
		<input type="hidden" name="psdm_uuid" value="<?php echo esc_attr( $psdm_uuid ); ?>" />

		<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
			<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
				<input
					type="checkbox"
					class="psdmtw-peer psdmtw-sr-only"
					id="psdm_name_required"
					name="psdm_name_required"
					value="yes"
					<?php
					echo esc_attr( $name_checked );
					?>
				>
				<span  id="psdm_name_required_text" class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
				<span class="psdmtw-font-medium">
								<span class="psdmtw-font-medium">Require name as well as an email address.</span>
								<span class="dashicons dashicons-editor-help psdm_tooltip">
									<div class="psdm_right">
										<p>This will require the user to enter their name as well as their email address.</p>
									</div>
								</span>
							</span>
			</label>
		</div>

		<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
			<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
				<input
					type="checkbox"
					class="psdmtw-peer psdmtw-sr-only"
					id="psdm_permission"
					name="psdm_permission"
					value="yes"
					<?php
					echo esc_attr( $permission_checked );
					?>
				>
				<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
				<span class="psdmtw-font-medium">
								<span class="psdmtw-font-medium">Require opt-in user consent before download.</span>
								<span class="dashicons dashicons-editor-help psdm_tooltip">
									<div class="psdm_right">
										<p>This will require the user to physically agree by checking the box on the download form.</p>
									</div>
								</span>
							</span>
			</label>
		</div>

		<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
			<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
				<input
					type="checkbox"
					class="psdmtw-peer psdmtw-sr-only"
					id="psdm_bypass"
					name="psdm_bypass"
					value="yes"
					<?php
					echo esc_attr( $bypass_checked );
					?>
				>
				<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
				<span class="psdmtw-font-medium">
								<span class="psdmtw-font-medium">Allow download without email address.</span>
								<span class="dashicons dashicons-editor-help psdm_tooltip">
									<div class="psdm_right">
										<p>Will give an option to allow users to download without entering an email address.</p>
									</div>
								</span>
							</span>
			</label>
		</div>

		<!-- Form Switches: With Labels Small -->
		<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
			<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
				<input
						type="checkbox"
						class="psdmtw-peer psdmtw-sr-only"
						id="psdm_footer_no"
						name="psdm_footer_no"
						value="yes"
					<?php
					echo esc_attr( $psdm_do_not_use_wp_footer );
					?>
				>
				<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
				<span class="psdmtw-font-medium">
								<span class="psdmtw-font-medium"><strong>(Advanced)</strong> Do NOT user wp_footer() to create the Download Magnet dialog form (Popup or Button only)</span>
								<span class="dashicons dashicons-editor-help psdm_tooltip">
									<div class="psdm_right">
										<p>Most themes have a wp_footer hook. If the dialog will not appear, try checking this. There may be some z-index issues!</p>
									</div>
								</span>
							</span>
			</label>
		</div>




		<div class="psdmtw-space-y-1">
			<label for="psdm_permission_text" class="psdmtw-font-bold">Permission Text
				<span class="dashicons dashicons-editor-help psdm_tooltip">
				<div class="psdm_right">
					<p>The text shown for the user to agree to.</p>
				</div>
			</span>
			</label>
			<input
					type="text"
					id="psdm_permission_text"
					name="psdm_permission_text"
					placeholder="Title to display in download window"
					value="<?php echo esc_attr( $psdm_permission_text ); ?>"
					class="psdmtw-block psdmtw-w-3/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-px-3 psdmtw-py-2 psdmtw-leading-6 psdmtw-placeholder-gray-500 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
			/>
		</div>

		<div class="psdmtw-space-y-1">
			<label for="psdm_permission_text" class="psdmtw-font-bold">Permission Text
				<span class="dashicons dashicons-editor-help psdm_tooltip">
				<div class="psdm_right">
					<p>he text shown on the Download button. It is required</p>
				</div>
			</span>
			</label>
			<input
					type="text"
					id="psdm_download_text"
					name="psdm_download_text"
					placeholder="Download Button Text"
					maxlength="25"
					value="<?php echo esc_attr( $psdm_download_text ); ?>"
					class="psdmtw-block psdmtw-w-1/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-px-3 psdmtw-py-2 psdmtw-leading-6 psdmtw-placeholder-gray-500 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
					required
			/>
		</div>


		<table>
			<tr>
				<td style="width: 275px">
					<span class="psdmtw-font-normal">Download Button Background Color
						<span class="dashicons dashicons-editor-help psdm_tooltip">
							<div class="psdm_right">
								<p>Choose a new background color for the Download button. (Default is blue).</p>
							</div>
						</span>
					</span>
				</td>
				<td>
					<input type="text" name="psdm_backcolor" value="<?php echo esc_attr( $psdm_bgcolor ); ?>" class="psdm_bgcolorpicker" data-default-color="#0000cd" />
				</td>
			</tr>
			<tr>
				<td>
					<span class="psdmtw-font-normal">Download Button Text Color
						<span class="dashicons dashicons-editor-help psdm_tooltip">
							<div class="psdm_right">
								<p>Choose a new text color for the Download button. (Default is white).</p>
							</div>
						</span>
					</span>
				</td>
				<td>
					<input type="text" name="psdm_forecolor" value="<?php echo esc_attr( $psdm_color ); ?>" class="psdm_colorpicker" data-default-color="#ffffff" />
				</td>
			</tr>
			<tr>
				<td></td>
				<td>
					<span class="psdmtw-font-medium">
						The settings below only affect the default dialog box.
					</span>
				</td>
			</tr>
			<tr>
				<td>
					<span class="psdmtw-font-normal">Dialog Box Border Color
						<span class="dashicons dashicons-editor-help psdm_tooltip">
							<div class="psdm_right">
								<p>Only on default dialog - choose a new color for the Dialog Box border. (Default is black).</p>
							</div>
						</span>
					</span>
				</td>
				<td>
					<input type="text" name="psdm_bordercolor" value="<?php echo esc_attr( $psdm_bordercolor ); ?>" class="psdm_colorpicker" data-default-color="#000000" />
				</td>
			</tr>
			<tr>
				<td>
					<span class="psdmtw-font-normal">Dialog Box Border Width
						<span class="dashicons dashicons-editor-help psdm_tooltip">
							<div class="psdm_right">
								<p>Only on default dialog - choose a width for the Dialog Box border. (Default is 2px).</p>
							</div>
						</span>
					</span>
				</td>
				<td>
					<input type="range" id="psdm_borderwidth" name="psdm_borderwidth"  value="<?php echo esc_attr( $psdm_borderwidth ); ?>" min="0" max="10" />
					<span id="psdm-border-width-value" name="psdm-border-width-value"> <?php echo esc_attr( $psdm_borderwidth ); ?>px</span>
				</td>
			</tr>
			<tr>
				<td>
					<span class="psdmtw-font-normal">Dialog Box Border Radius
						<span class="dashicons dashicons-editor-help psdm_tooltip">
							<div class="psdm_right">
								<p>Only on default dialog - choose a radius setting for the Dialog Box border. (Default is 10px).</p>
							</div>
						</span>
					</span>
				</td>
				<td>
					<input type="range" id="psdm_borderradius" name="psdm_borderradius"  value="<?php echo esc_attr( $psdm_borderradius ); ?>" min="0" max="20" />
					<span id="psdm-border-radius-value" name="psdm-border-radius-value"> <?php echo esc_attr( $psdm_borderradius ); ?>px</span>
				</td>
			</tr>
		</table>

		<?php
	}//end psdm_add_options()


	/**
	 * Add the meta box for the download form.
	 *
	 * @param array $post The post object.
	 *
	 * @return void
	 */
	public function psdm_addons( $post ) {
		$input = json_decode( get_post_meta( $post->ID, 'psdmcontent', true ), true );
		if ( ! isset( $input['psdm_mailing_list'] ) ) {
			$input['psdm_mailing_list'] = '';
		}

		if ( ! isset( $input['psdm_tags'] ) ) {
			$input['psdm_tags'] = '';
		}

		for ( $psdm_custom_index = 1; $psdm_custom_index <= 3; $psdm_custom_index++ ) {
			$psdm_custom_key = 'psdm_custom_' . $psdm_custom_index;
			if ( ! isset( $input[ $psdm_custom_key ] ) ) {
				$input[ $psdm_custom_key ] = '';
			}
		}

		$options = get_option( 'psdm_options' );
		if ( false === $options ) {
			$options = array();
		}
		if ( ! defined( 'PSDM_EMAIL_ADDON' ) ) {
			?>
			<!-- CTA Section: Featured Image Alternate -->
			<div
					class="psdmtw-overflow-hidden psdmtw-bg-gradient-to-tr psdmtw-from-indigo-100 psdmtw-via-purple-50 psdmtw-to-teal-100"
			>
				<div class="container psdmtw-mx-auto psdmtw-px-4 psdmtw-py-16 lg:psdmtw-px-8 lg:psdmtw-py-32 xl:psdmtw-max-w-7xl">
					<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-16 lg:psdmtw-flex-row lg:psdmtw-items-center lg:psdmtw-gap-28">
						<!-- Image -->
						<div class="psdmtw-relative">
							<div
									class="bg-tranparent psdmtw-absolute psdmtw-top-0 psdmtw-right-0 psdmtw--m-6 size-40 psdmtw-rounded-full psdmtw-border psdmtw-border-teal-200 lg:size-96"
							></div>
							<div
									class="bg-tranparent psdmtw-absolute psdmtw-bottom-0 psdmtw-left-0 psdmtw--m-6 size-40 psdmtw-rounded-full psdmtw-border psdmtw-border-indigo-200 lg:size-96"
							></div>
							<div
									class="psdmtw-absolute psdmtw-top-0 psdmtw-left-0 psdmtw--mt-10 psdmtw--ml-10 size-36 psdmtw-rounded-full psdmtw-bg-gradient-to-br psdmtw-from-purple-200"
							></div>
							<div
									class="psdmtw-absolute psdmtw-right-0 psdmtw-bottom-0 psdmtw--mr-8 psdmtw--mb-8 size-24 psdmtw-rotate-12 psdmtw-rounded-3xl psdmtw-bg-gradient-to-tl from-amber-200"
							></div>
							<div
									class="psdmtw-absolute psdmtw-top-0 psdmtw-right-0 psdmtw--mt-10 psdmtw--mr-8 psdmtw-h-40 psdmtw-w-32 psdmtw--rotate-6 psdmtw-rounded-3xl psdmtw-bg-gradient-to-bl psdmtw-from-teal-200"
							></div>
							<div
									class="psdmtw-absolute psdmtw-bottom-0 psdmtw-left-0 psdmtw--mb-8 psdmtw--ml-8 psdmtw-h-40 psdmtw-w-32 psdmtw-rotate-12 psdmtw-rounded-3xl psdmtw-bg-gradient-to-tr psdmtw-from-indigo-200"
							></div>
							<img
									src="<?php echo esc_url( plugin_dir_url( __FILE__ ) ) . 'images/emails.jpg'; ?>"
									class="psdmtw-relative psdmtw-mx-auto psdmtw-rounded-lg psdmtw-shadow-lg"
									alt="Image of Emails"
							/>
						</div>
						<!-- END Image -->

						<div class="psdmtw-relative psdmtw-space-y-8">
							<!-- Heading -->
							<div>
								<div
										class="psdmtw-mb-1 psdmtw-text-lg psdmtw-font-bold psdmtw-tracking-wider psdmtw-text-blue-600 psdmtw-uppercase"
								>
									Email Addons Available
								</div>
								<p
										class="psdmtw-text-xl psdmtw-leading-relaxed psdmtw-font-medium psdmtw-text-gray-700"
								>
									Download Magnet offers a variety of email addons to help you automate the process and add the new user to
									your email marketing provider as well as using webhooks (Zapier/Make etc.) Connect with your audience. <br/><br/>
								</p>
								<p
										class="psdmtw-text-xl psdmtw-leading-relaxed psdmtw-font-medium psdmtw-text-gray-700"
								>
									Mailchimp, Kit, ActiveCampaign and others are available now. For advanced automation users, a WebHook addon is also available.<br/>
								</p>

							</div>
							<!-- END Heading -->

							<!-- Action -->
							<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-4 lg:psdmtw-flex-row lg:psdmtw-items-center lg:psdmtw-gap-3">
								<a
										href="https://peachysoftware.com/downloadmagnet/addons/"
										target="_blank"
										class="psdmtw-inline-flex psdmtw-items-center psdmtw-justify-center psdmtw-gap-2 psdmtw-rounded-lg psdmtw-border psdmtw-border-blue-700 psdmtw-bg-blue-700 psdmtw-px-6 psdmtw-py-3 psdmtw-leading-6 psdmtw-font-semibold psdmtw-text-white hover:psdmtw-border-blue-600 hover:psdmtw-bg-blue-600 hover:psdmtw-text-white focus:psdmtw-ring focus:ring-blue-400/50 active:psdmtw-border-blue-700 active:psdmtw-bg-blue-700"
								>
									<span>Find Out More</span>
								</a>
							</div>
							<!-- END Action -->
						</div>
					</div>
				</div>
			</div>
			<!-- END CTA Section: Featured Image Alternate -->

			<?php
		}

		do_action(
			'psdm_addtoform',
			$input['psdm_mailing_list'],
			$input['psdm_tags'],
			$input['psdm_custom_1'],
			$input['psdm_custom_2'],
			$input['psdm_custom_3']
		);
	}//end psdm_addons()


	/**
	 * Add the meta box for the download form.
	 *
	 * @param string $post current post.
	 *
	 * @return void
	 */
	public function psdm_add_field( $post ) {
		wp_nonce_field( basename( __FILE__ ), 'psdm_nonce' );
		$input = json_decode( get_post_meta( $post->ID, 'psdmcontent', true ), true );
		if ( isset( $input['psdm_style'] ) ) {
			$input['psdm_style'] = esc_attr( $input['psdm_style'] );
		} else {
			$input['psdm_style'] = 'popup';
		}

		if ( isset( $input['psdm_template'] ) ) {
			$input['psdm_template'] = esc_attr( $input['psdm_template'] );
		} else {
			$input['psdm_template'] = 'default';
		}

		if ( isset( $input['psdm_exit_intent'] ) ) {
			$input['psdm_exit_intent'] = esc_attr( $input['psdm_exit_intent'] );
		} else {
			$input['psdm_exit_intent'] = 'false';
		}

		if ( isset( $input['psdm_delay_seconds'] ) ) {
			$input['psdm_delay_seconds'] = absint( $input['psdm_delay_seconds'] );
		} else {
			$input['psdm_delay_seconds'] = 0;
		}

		$psdm_exit_intent_checked = '';
		if ( 'yes' === $input['psdm_exit_intent'] ) {
			$psdm_exit_intent_checked = 'checked';
		}

		if ( isset( $input['psdm_filename'] ) ) {
			$input['psdm_filename'] = esc_attr( $input['psdm_filename'] );
		} else {
			$input['psdm_filename'] = '';
		}

		if ( isset( $input['psdm_title'] ) ) {
			$input['psdm_title'] = esc_attr( $input['psdm_title'] );
		} else {
			$input['psdm_title'] = '';
		}

		if ( isset( $input['psdm_description'] ) ) {
			$input['psdm_description'] = $input['psdm_description'];
		} else {
			$input['psdm_description'] = '';
		}

		if ( isset( $input['psdm_download_title'] ) ) {
			$input['psdm_download_title'] = esc_attr( $input['psdm_download_title'] );
		} else {
			$input['psdm_download_title'] = '';
		}
		if ( 'button' === $input['psdm_style'] ) {
			$psdm_post_id = '[downloadmagnet id=' . $post->ID . ' button=insert button id here]';
		} else {
			$psdm_post_id = '[downloadmagnet id=' . $post->ID . ']';
		}
		?>

		<div id="smartwizard">
			<ul class="nav">
				<li class="nav-item">
					<a class="nav-link" href="#step-1">
						<div class="num">1</div>
						Download Magnet File
					</a>
				</li>
				<li class="nav-item">
					<a class="nav-link" id="psdm-step-2" href="#step-2">
						<span class="num">2</span>
						Options
					</a>
				</li>
				<li class="nav-item">
					<a class="nav-link" href="#step-3">
						<span class="num">3</span>
						Email Addon
					</a>
				</li>
			</ul>

			<div class="tab-content">
				<div id="step_1" class="tab-pane psdmtw-space-y-6" role="tabpanel" aria-labelledby="step-1">
					<input type="hidden" id="psdm_shortcodebase" value="[downloadmagnet id=<?php echo esc_attr( $post->ID ); ?>]" />
					<input type="hidden" id="psdm_shortcodebuttonbase" value="[downloadmagnet id=<?php echo esc_attr( $post->ID ); ?> button=insert button id here]" />

					<!-- Alerts: Info -->
					<div
							class="psdmtw-rounded psdmtw-bg-blue-100 psdmtw-p-4 psdmtw-text-blue-700 md:psdmtw-p-5"
					>
						<div class="psdmtw-mb-2 psdmtw-flex items-center">
							<svg
									class="psdmtw-hi-solid psdmtw-hi-information-circle psdmtw-mr-3 psdmtw-inline-block psdmtw-size-5 psdmtw-flex-none psdmtw-text-blue-500"
									fill="currentColor"
									viewBox="0 0 20 20"
									xmlns="http://www.w3.org/2000/svg"
							>
								<path
										fill-rule="evenodd"
										d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z"
										clip-rule="evenodd"
								/>
							</svg>
							<h3 class="psdmtw-font-semibold">Shortcode will be <?php echo esc_attr( $psdm_post_id ); ?></h3>
						</div>
						<div class="psdmtw-ml-8 psdmtw-mt-3 psdmtw-space-x-1">
							<button id="psdm_copybutton" class="psdm_shortcode_button" onclick="psdm_copyToClipboard('<?php echo esc_attr( $psdm_post_id ); ?>', event)";>Copy ShortCode</button>
							<span>This style can be overridden by using <i>type=button</i> or <i>type=inline</i> or <i>type=popup</i> in the shortcode.</span>
						</div>
					</div>
					<!-- END Alerts: Info -->
					<div class="psdmtw-space-y-1">
						<label for="psdm_style" class="psdmtw-font-bold">Download Magnet Style
							<span class="dashicons dashicons-editor-help psdm_tooltip">
								<div class="psdm_right">
									<p>You can choose between a popup window, show the form inline or attach to an existing button to the page to capture the email address.</p>
								</div>
							</span>
						</label>
						<select
								id="psdm_style"
								name="psdm_style"
								class="psdmtw-block psdmtw-w-1/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-py-2 psdmtw-pl-3 psdmtw-pr-10 psdmtw-text-sm psdmtw-leading-5 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
						>
							<option value="popup" <?php selected( $input['psdm_style'], 'popup' ); ?>>Popup</option>
							<option value="inline" <?php selected( $input['psdm_style'], 'inline' ); ?>>Inline</option>
							<option value="button" <?php selected( $input['psdm_style'], 'button' ); ?>>Attach to Button</option>
						</select>
					</div>

					<div id="psdm_exit_intent_option" class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
						<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
							<input
									type="checkbox"
									class="psdmtw-peer psdmtw-sr-only"
									id="psdm_exit_intent"
									name="psdm_exit_intent"
									value="yes"
								<?php
								echo esc_attr( $psdm_exit_intent_checked );
								?>
							>
							<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
							<span class="psdmtw-font-medium">
								<span class="psdmtw-font-medium">Show popup on exit intent.</span>
								<span class="dashicons dashicons-editor-help psdm_tooltip">
									<div class="psdm_right">
										<p>Shows the popup when the visitor moves their cursor toward the top of the browser window.</p>
									</div>
								</span>
							</span>
						</label>
					</div>

					<div id="psdm_delay_seconds_option" class="psdmtw-space-y-1">
						<label for="psdm_delay_seconds" class="psdmtw-font-bold">Delayed Popup Seconds
							<span class="dashicons dashicons-editor-help psdm_tooltip">
								<div class="psdm_right">
									<p>Enter the number of seconds to wait before showing the popup. Use 0 to disable the delayed popup.</p>
								</div>
							</span>
						</label>
						<input
								type="number"
								id="psdm_delay_seconds"
								name="psdm_delay_seconds"
								min="0"
								step="1"
								value="<?php echo esc_attr( $input['psdm_delay_seconds'] ); ?>"
								class="psdmtw-block psdmtw-w-1/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-px-3 psdmtw-py-2 psdmtw-leading-6 psdmtw-placeholder-gray-500 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
						/>
					</div>

					<div class="psdmtw-space-y-1">
						<label for="psdm_template" class="psdmtw-font-bold">Modal Template
							<span class="dashicons dashicons-editor-help psdm_tooltip">
								<div class="psdm_right">
									<p>Choose the visual design template for your popup or button modal. This setting only applies to popup and button styles.</p>
								</div>
							</span>
						</label>
						<select
								id="psdm_template"
								name="psdm_template"
								class="psdmtw-block psdmtw-w-1/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-py-2 psdmtw-pl-3 psdmtw-pr-10 psdmtw-text-sm psdmtw-leading-5 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
						>
							<option value="default" <?php selected( $input['psdm_template'], 'default' ); ?>>Default</option>
							<option value="modern" <?php selected( $input['psdm_template'], 'modern' ); ?>>Modern</option>
							<option value="minimal" <?php selected( $input['psdm_template'], 'minimal' ); ?>>Minimal</option>
						</select>
					</div>

					<div class="psdmtw-space-y-1">
						<label for="psdm_filename" class="psdmtw-font-bold">File to download
							<span class="dashicons dashicons-editor-help psdm_tooltip">
								<div class="psdm_right">
									<p>Enter the URL of the Download Magnet file or click on the button below to either upload or select the file. (Required)</p>
								</div>
							</span>
						</label>
						<input
								type="text"
								id="psdm_filename"
								name="psdm_filename"
								placeholder="URL of file to be downloaded"
								value="<?php echo esc_attr( $input['psdm_filename'] ); ?>"
								class="psdm_filename psdmtw-block psdmtw-w-3/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-px-3 psdmtw-py-2 psdmtw-leading-6 psdmtw-placeholder-gray-500 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
								required
						/>
						<a class="upload_image_button" id="psdm_media_select" href="#">
							<button class="psdm_shortcode_button">
								<?php
								esc_attr_e( 'Select or Upload file' );
								?>
							</button>
						</a>
					</div>

					<div class="psdmtw-space-y-1">
						<label for="psdm_download_title" class="psdmtw-font-bold">Download Title
							<span class="dashicons dashicons-editor-help psdm_tooltip">
								<div class="psdm_right">
									<p>This appears at the top of the Download Magnet dialog box for the user to download. (Required)</p>
								</div>
							</span>
						</label>
						<input
								type="text"
								id="psdm_download_title"
								name="psdm_download_title"
								placeholder="Title to display in download window"
								value="<?php echo esc_attr( $input['psdm_download_title'] ); ?>"
								class="psdmtw-block psdmtw-w-3/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-px-3 psdmtw-py-2 psdmtw-leading-6 psdmtw-placeholder-gray-500 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
								required
						/>
					</div>

					<div class="psdmtw-space-y-1">
						<label for="psdm_description" class="psdmtw-font-bold">Download Window Description
							<span class="dashicons dashicons-editor-help psdm_tooltip">
								<div class="psdm_right">
									<p>Add a description for more details for the user to read. You can use simple HTML formatting such as bold or italic tags.</p>
								</div>
							</span>
						</label>
						<textarea
								id="psdm_description"
								name="psdm_description"
								rows="8"
								placeholder="Enter a description for the download magnet"
								class="psdmtw-block psdmtw-w-3/4 psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-500 psdmtw-px-3 psdmtw-py-2 psdmtw-leading-6 psdmtw-placeholder-gray-500 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50"
						><?php echo esc_attr( $input['psdm_description'] ); ?></textarea>
					</div>
				</div>
				<div id="step-2" class="tab-pane psdmtw-space-y-6" role="tabpanel" aria-labelledby="step-2">
					<?php
						$this->psdm_add_options( $post );
					?>
				</div>
				<div id="step-3" class="tab-pane psdmtw-space-y-6" role="tabpanel" aria-labelledby="step-3">
					<?php
						$this->psdm_addons( $post );
					?>
				</div>
			</div>

		</div>

		<?php
	}//end psdm_add_field()


	/**
	 * Change the title placeholder text.
	 *
	 * @param string $input title placeholder text.
	 *
	 * @return mixed|string
	 */
	public function psdm_title_change( $input ) {
		global  $post_type;
		if ( is_admin() && 'Add title' === $input && 'psdmcontent' === $post_type ) {
			return 'Enter Download Name';
		}

		return $input;
	}//end psdm_title_change()


	/**
	 * Export the data in csv formal.
	 */
	public function psdm_process_export() {
		if ( isset( $_POST['psdm_export_nonce'] ) ) {
			$result = wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['psdm_export_nonce'] ) ), 'psdm_export' );
			if ( ! $result ) {
				wp_die( '0' );
			}
		}

		if ( ! empty( $_POST['psdm_export'] ) ) {
			global  $wpdb;
			$export_id            = ( isset( $_POST['psdm_export_list'] ) ? absint( wp_unslash( $_POST['psdm_export_list'] ) ) : 0 );
			$psdm_filter_by_date  = ! empty( $_POST['psdm_export_date_enabled'] );
			$psdm_export_from_raw = ( isset( $_POST['psdm_export_from_date'] ) ? sanitize_text_field( wp_unslash( $_POST['psdm_export_from_date'] ) ) : '' );
			$psdm_export_from     = '';

			if ( $psdm_filter_by_date && preg_match( '/^\d{4}-\d{2}-\d{2}$/', $psdm_export_from_raw ) ) {
				$psdm_export_from = $psdm_export_from_raw . ' 00:00:00';
			}

			if ( $psdm_export_from ) {
				$result = $wpdb->get_results(
					$wpdb->prepare(
						"SELECT name,email_address,time,comments AS email FROM {$wpdb->prefix}psdm_downloads WHERE download_id=%d AND time >= %s",
						$export_id,
						$psdm_export_from
					),
					'ARRAY_A'
				);
			} else {
				$result = $wpdb->get_results(
					$wpdb->prepare(
						"SELECT name,email_address,time,comments AS email FROM {$wpdb->prefix}psdm_downloads WHERE download_id=%d",
						$export_id
					),
					'ARRAY_A'
				);
			}
			$file     = 'downloadmagnet_export';
			$filename = $file . '_' . gmdate( 'Y-m-d_H-i', time() ) . '.csv';
			$fh       = @fopen( 'php://output', 'w' );
			header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
			header( 'Content-Description: File Transfer' );
			header( "Content-Disposition: attachment; filename={$filename}" );
			header( 'Expires: 0' );
			header( 'Pragma: public' );
			fprintf( $fh, chr( 0xef ) . chr( 0xbb ) . chr( 0xbf ) );
			$csv_header = array(
				'name',
				'email',
				'time',
				'comments',
			);
			fputcsv( $fh, $csv_header );
			foreach ( $result as $data_row ) {
				fputcsv( $fh, $data_row );
			}

			fclose( $fh );
			exit;
		}//end if
	}//end psdm_process_export()


		/**
		 * Export page html.
		 */
	public function psdm_export_page_html() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$psdm_args                 = array( 'post_type' => 'psdmcontent' );
		$psdm_posts                = get_posts( $psdm_args );
		$psdm_select               = '';
		$psdm_selected_export_id   = ( isset( $_POST['psdm_export_list'] ) ? absint( wp_unslash( $_POST['psdm_export_list'] ) ) : 0 );
		$psdm_filter_by_date       = ! empty( $_POST['psdm_export_date_enabled'] );
		$psdm_export_from_date_raw = ( isset( $_POST['psdm_export_from_date'] ) ? sanitize_text_field( wp_unslash( $_POST['psdm_export_from_date'] ) ) : '' );
		$psdm_export_from_date     = preg_match( '/^\d{4}-\d{2}-\d{2}$/', $psdm_export_from_date_raw ) ? $psdm_export_from_date_raw : '';
		if ( $psdm_posts ) {
			foreach ( $psdm_posts as $psdm_post ) {
				$psdm_post_id    = $psdm_post->ID;
				$psdm_post_title = $psdm_post->post_title;
				$psdm_selected   = selected( $psdm_selected_export_id, $psdm_post_id, false );
				$psdm_select    .= '<option value="' . esc_attr( $psdm_post_id ) . '"' . $psdm_selected . '>' . esc_html( $psdm_post_title ) . '</option>';
			}
		}
		?>
		<div class="wrap">
			<h1>
				<?php
				echo esc_html( get_admin_page_title() );
				?>
			</h1>
			<br/>
			<script type="application/javascript">
				jQuery('body').removeClass('wp-core-ui');
				jQuery(function($) {
					const toggleExportDate = function() {
						const showDate = $('#psdm_export_date_enabled').is(':checked');
						$('#psdm_export_date_wrap').toggleClass('psdmtw-hidden', !showDate);
						$('#psdm_export_from_date').prop('disabled', !showDate);
					};

					$('#psdm_export_date_enabled').on('change', toggleExportDate);
					toggleExportDate();
				});
			</script>

			<form class="space-y-6 dark:text-gray-100" action="#" method="post">
				<input type="hidden" name="psdm_export" value="export">
				<?php
				wp_nonce_field( 'psdm_export', 'psdm_nonce' );
				?>
				<div class="psdmtw-space-y-1 psdmtw-w-1/5">
								<label for="psdm_export_list" class="psdmtw-font-bold">Please select the Download Magnet item to export the user list.</label>
					<select
						name="psdm_export_list"
						id="psdm_export_list"
						class="psdmtw-block psdmtw-w-full psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-py-2 psdmtw-pl-3 psdmtw-pr-10 psdmtw-leading-6 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50 dark:psdmtw-border-gray-600 dark:psdmtw-bg-gray-800 dark:focus:psdmtw-border-blue-500"
					>
						<?php
						echo wp_kses(
							$psdm_select,
							array(
								'option' => array(
									'value' => array(),
								),
							)
						);
						?>
					</select>
				</div>
				<br/>
				<div class="psdmtw-space-y-3">
					<label for="psdm_export_date_enabled" class="psdmtw-inline-flex psdmtw-items-center psdmtw-gap-2">
						<input
							type="checkbox"
							name="psdm_export_date_enabled"
							id="psdm_export_date_enabled"
							value="1"
							<?php checked( $psdm_filter_by_date ); ?>
						/>
						<span>Export downloads from a specific date</span>
					</label>
					<div
						id="psdm_export_date_wrap"
						class="psdmtw-space-y-1 psdmtw-w-1/5 <?php echo $psdm_filter_by_date ? '' : 'psdmtw-hidden'; ?>"
					>
						<label for="psdm_export_from_date" class="psdmtw-font-bold">Export downloads from</label>
						<input
							type="date"
							name="psdm_export_from_date"
							id="psdm_export_from_date"
							value="<?php echo esc_attr( $psdm_export_from_date ); ?>"
							class="psdmtw-block psdmtw-w-full psdmtw-rounded-lg psdmtw-border psdmtw-border-gray-200 psdmtw-px-3 psdmtw-py-2 focus:psdmtw-border-blue-500 focus:psdmtw-ring focus:psdmtw-ring-blue-500/50 dark:psdmtw-border-gray-600 dark:psdmtw-bg-gray-800 dark:focus:psdmtw-border-blue-500"
							<?php disabled( ! $psdm_filter_by_date ); ?>
						/>
					</div>
				</div>
				<br/>
				<button
					type="submit"
					class="psdmtw-inline-flex psdmtw-items-center psdmtw-justify-center psdmtw-gap-2 psdmtw-rounded-lg psdmtw-border psdmtw-border-blue-700 psdmtw-bg-blue-700 psdmtw-px-3 psdmtw-py-2 psdmtw-text-sm psdmtw-font-semibold psdmtw-leading-5 psdmtw-text-white hover:psdmtw-border-blue-600 hover:psdmtw-bg-blue-600 hover:psdmtw-text-white focus:psdmtw-ring focus:psdmtw-ring-blue-400/50 active:psdmtw-border-blue-700 active:psdmtw-bg-blue-700 dark:focus:psdmtw-ring-blue-400/90"
				>Export</button>
			</form>
			<!-- Cards: Simple -->
		</div>
		<?php
	}//end psdm_export_page_html()


	/**
	 * Options page html.
	 */
	public function psdm_options_page_html() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		?>
		<div class="wrap">
			<h1>
				<?php
				echo esc_html( get_admin_page_title() );
				?>
			</h1>
			<form action="options.php" method="post">
				<?php
				$active_tab = ( isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'psdm' );
				?>
				<h2 class="nav-tab-wrapper">
					<a href="?post_type=psdmcontent&page=psdm_options&tab=psdm"
						class="nav-tab
						<?php
						echo 'psdm' === $active_tab ? 'nav-tab-active' : '';
						?>
		">Main Options</a>
					<a href="?post_type=psdmcontent&page=psdm_options&tab=email"
						class="nav-tab
						<?php
						echo 'email' === $active_tab ? 'nav-tab-active' : '';
						?>
		">Email Settings</a>
				</h2>
				<div class="postbox">
					<?php
					switch ( $active_tab ) {
						case 'psdm':
							settings_fields( 'psdm_options' );
							$psdm_options = get_option( 'psdm_options' );
							if ( false === $psdm_options ) {
									$psdm_options = array();
							}

							if ( ! isset( $psdm_options['email_address'] ) ) {
								$psdm_options['email_address'] = '';
							}

							if ( isset( $psdm_options['throwaway'] ) ) {
								$psdm_donotuse_disposable = true;
							} else {
								$psdm_donotuse_disposable = false;
							}

							if ( isset( $psdm_options['download_notify'] ) ) {
								$psdm_download_notify = true;
							} else {
								$psdm_download_notify = false;
							}

							if ( isset( $psdm_options['error_notify'] ) ) {
								$psdm_error_notify = true;
							} else {
								$psdm_error_notify = false;
							}

							if ( isset( $psdm_options['plain_email'] ) ) {
								$psdm_plain_email = true;
							} else {
								$psdm_plain_email = false;
							}

							if ( isset( $psdm_options['ipaddress'] ) ) {
								$psdm_donotuse_ipaddress = true;
							} else {
								$psdm_donotuse_ipaddress = false;
							}

							if ( isset( $psdm_options['honeypot'] ) ) {
								$psdm_use_honeypot = true;
							} else {
								$psdm_use_honeypot = false;
							}

							do_settings_sections( 'psdm_options' );

							?>
							<table class="form-table">
								<tr>
									<td class="psdm-settings-width" style="width: 250px; vertical-align: top"; id="psdm_email_form"><b>Email Notifications</b><br/>Enter the email address you would notifications to go to.</td>
									<td>
										<input type="email" size="50" aria-labelledby="psdm_email_form" placeholder="Enter email address here for notifications" name="psdm_options[email_address]" value=" <?php echo esc_attr( $psdm_options['email_address'] ); ?>" /><br/><br/>
										<!-- Form Switches: With Labels Small -->
										<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
											<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
												<input
														type="checkbox"
														class="psdmtw-peer psdmtw-sr-only"
														id="psdm_options[download_notify]"
														name="psdm_options[download_notify]"
														aria-labelledby="psdm_send_email_form"
														value="true"
													<?php echo ( $psdm_download_notify ) ? 'checked' : ''; ?>                                                 >
												<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
												<span class="psdmtw-font-normal">Enable sending email notifications when a magnet is downloaded.</span>
											</label>
										</div>
										<br/>
										<!-- END Form Switches: With Labels Small -->
										<?php if ( defined( 'PSDM_EMAIL_ADDON' ) ) { ?>
											<!-- Form Switches: With Labels Small -->
											<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
												<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
													<input
															type="checkbox"
															class="psdmtw-peer psdmtw-sr-only"
															id="psdm_options[error_notify]"
															name="psdm_options[error_notify]"
															aria-labelledby="psdm_send_error_form"
															value="true"
														<?php echo ( $psdm_error_notify ) ? 'checked' : ''; ?>                                                 >
													<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
													<span class="psdmtw-font-normal">Enable sending error notifications on a failed subscriber add.</span>
												</label>
											</div>
											<br/>
											<!-- END Form Switches: With Labels Small -->
										<?php } ?>
										<!-- Form Switches: With Labels Small -->
										<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
											<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
												<input
														type="checkbox"
														class="psdmtw-peer psdmtw-sr-only"
														id="psdm_options[plain_email]"
														name="psdm_options[plain_email]"
														aria-labelledby="send_plain_form"
														value="true"
													<?php echo ( $psdm_plain_email ) ? 'checked' : ''; ?>                                                 >
												<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
												<span class="psdmtw-font-normal">Send plain text emails (HTML by default).</span>
											</label>
										</div>
										<!-- END Form Switches: With Labels Small -->
									</td>
								</tr>
								<tr>
									<td style="vertical-align: top"><b>Use Honeypot on Form</b><br/>Add a hidden honeypot</br>to try and prevent bots/spammers.
									</td>
									<td style="vertical-align: top">
										<!-- Form Switches: With Labels Small -->
										<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
											<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
												<input
														type="checkbox"
														class="psdmtw-peer psdmtw-sr-only"
														id="psdm_options[honeypot]"
														name="psdm_options[honeypot]"
														aria-labelledby="psdm_honeypot_form"
														value="true"
														<?php echo ( $psdm_use_honeypot ) ? 'checked' : ''; ?>                                                 >
												<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
												<span class="psdmtw-font-normal">Use honeypot on download form.</span>
											</label>
										</div>
										<!-- END Form Switches: With Labels Small -->
									</td>
								</tr>
								<tr>
									<td style="vertical-align: top"><b>Disposable Email Addresses</b><br/>Whether to process emails </br>determined to be disposable.
									</td>
									<td style="vertical-align: top">
										<!-- Form Switches: With Labels Small -->
										<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
											<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
												<input
														type="checkbox"
														class="psdmtw-peer psdmtw-sr-only"
														id="psdm_options[throwaway]"
														name="psdm_options[throwaway]"
														aria-labelledby="psdm_ignore_form"
														value="true"
													<?php echo ( $psdm_donotuse_disposable ) ? 'checked' : ''; ?>                                                 >
												<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
												<span class="psdmtw-font-normal">Ignore disposable emails.</span>
											</label>
										</div>
										<!-- END Form Switches: With Labels Small -->
									</td>
								</tr>
								<tr>
									<td style="vertical-align: top"><b>Ignore users IP Address</b><br/>If checked, ignore the</br>users IP address. (GDPR etc.)
									</td>
									<td style="vertical-align: top">
										<!-- Form Switches: With Labels Small -->
										<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-6">
											<label class="psdmtw-group psdmtw-relative psdmtw-inline-flex psdmtw-items-center psdmtw-gap-3">
												<input
														type="checkbox"
														class="psdmtw-peer psdmtw-sr-only"
														id="psdm_options[ipaddress]"
														name="psdm_options[ipaddress]"
														aria-labelledby="psdm_gdpr_form"
														value="true"
													<?php echo ( $psdm_donotuse_ipaddress ) ? 'checked' : ''; ?>                                                 >
												<span class="psdmtw-relative psdmtw-h-6 psdmtw-w-10 psdmtw-flex-none psdmtw-rounded-full psdmtw-bg-gray-300 psdmtw-transition-all psdmtw-duration-150 psdmtw-ease-out before:psdmtw-absolute before:psdmtw-left-1 before:psdmtw-top-1 before:psdmtw-size-4 before:psdmtw-rounded-full before:psdmtw-bg-white before:psdmtw-transition-transform before:psdmtw-duration-150 before:psdmtw-ease-out before:psdmtw-content-[''] peer-checked:psdmtw-bg-blue-500 peer-checked:before:psdmtw-translate-x-full peer-focus:psdmtw-ring peer-focus:psdmtw-ring-blue-500/50 peer-focus:psdmtw-ring-offset-2 peer-focus:psdmtw-ring-offset-white peer-disabled:psdmtw-cursor-not-allowed peer-disabled:psdmtw-opacity-75"></span>
												<span class="psdmtw-font-normal">Ignore and do not store the users IP address (GDPR etc.).</span>
											</label>
										</div>
										<!-- END Form Switches: With Labels Small -->
									</td>
								</tr>
									<tr>
										<td style="vertical-align: top"><b>Installed Addons</b></td>
										<td>
											<div class="psdm_container">
												<?php
												if ( defined( 'PSDM_EMAIL_ADDON' ) ) {
													do_action( 'psdm_addons_name' );
													if ( isset( $psdm_options['email_provider'] ) && 'none' !== $psdm_options['email_provider'] ) {
														?>
														<div class="psdmtw-mt-5">
															<a href="<?php echo esc_url( get_admin_url() ); ?>edit.php?post_type=psdmcontent&page=psdm_options&tab=email">
																<button
																		type="button"
																		class="psdmtw-inline-flex psdmtw-items-center psdmtw-justify-center psdmtw-gap-2 psdmtw-rounded-lg psdmtw-border psdmtw-border-blue-200 psdmtw-bg-blue-100 psdmtw-px-3 psdmtw-py-2 psdmtw-text-sm psdmtw-font-semibold psdmtw-leading-5 psdmtw-text-blue-800 hover:psdmtw-border-blue-300 hover:psdmtw-text-blue-900 hover:psdmtw-shadow-sm focus:psdmtw-ring focus:psdmtw-ring-blue-300/25 active:psdmtw-border-blue-200 active:psdmtw-shadow-none dark:psdmtw-border-blue-200 dark:psdmtw-bg-blue-200 dark:psdmtw-hover:border-blue-300 dark:hover:psdmtw-bg-blue-300 dark:focus:psdmtw-ring-blue-500/50 dark:active:psdmtw-border-blue-200 dark:active:psdmtw-bg-blue-200"
																>Email Settings
																</button>
															</a>
														</div>
														<?php
													}
												} else {
													echo '<p><b>Addons are available for purchase</b></p>';
													echo '<p>Connect to many email marketing providers or Webhooks (Zapier/Make etc.). <a href="' . esc_url( get_admin_url() ) . 'edit.php?post_type=psdmcontent&page=downloadmagnet-addons" target="_blank">View Addon Store.</a></p>';
													echo '<p><a href="https://peachysoftware.com/downloadmagnet/addons" target="_blank">or visit our website for more details. Download Magnet Addons</a></p><br/>';
												}
												?>
											</div>
										</td>
									</tr>
							</table>
								<?php
							break;

						case 'email':
							if ( defined( 'PSDM_EMAIL_ADDON' ) ) {
								do_action( 'psdm_settings_email' );
							} else {
								?>
								<!-- CTA Section: Featured Image Alternate -->
								<div
										class="psdmtw-overflow-hidden psdmtw-bg-gradient-to-tr psdmtw-from-indigo-100 psdmtw-via-purple-50 psdmtw-to-teal-100"
								>
									<div class="container psdmtw-mx-auto psdmtw-px-4 psdmtw-py-16 lg:psdmtw-px-8 lg:psdmtw-py-32 xl:psdmtw-max-w-7xl">
										<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-16 lg:psdmtw-flex-row lg:psdmtw-items-center lg:psdmtw-gap-28">
											<!-- Image -->
											<div class="psdmtw-relative">
												<div
														class="bg-tranparent psdmtw-absolute psdmtw-top-0 psdmtw-right-0 psdmtw--m-6 size-40 psdmtw-rounded-full psdmtw-border psdmtw-border-teal-200 lg:size-96"
												></div>
												<div
														class="bg-tranparent psdmtw-absolute psdmtw-bottom-0 psdmtw-left-0 psdmtw--m-6 size-40 psdmtw-rounded-full psdmtw-border psdmtw-border-indigo-200 lg:size-96"
												></div>
												<div
														class="psdmtw-absolute psdmtw-top-0 psdmtw-left-0 psdmtw--mt-10 psdmtw--ml-10 size-36 psdmtw-rounded-full psdmtw-bg-gradient-to-br psdmtw-from-purple-200"
												></div>
												<div
														class="psdmtw-absolute psdmtw-right-0 psdmtw-bottom-0 psdmtw--mr-8 psdmtw--mb-8 size-24 psdmtw-rotate-12 psdmtw-rounded-3xl psdmtw-bg-gradient-to-tl from-amber-200"
												></div>
												<div
														class="psdmtw-absolute psdmtw-top-0 psdmtw-right-0 psdmtw--mt-10 psdmtw--mr-8 psdmtw-h-40 psdmtw-w-32 psdmtw--rotate-6 psdmtw-rounded-3xl psdmtw-bg-gradient-to-bl psdmtw-from-teal-200"
												></div>
												<div
														class="psdmtw-absolute psdmtw-bottom-0 psdmtw-left-0 psdmtw--mb-8 psdmtw--ml-8 psdmtw-h-40 psdmtw-w-32 psdmtw-rotate-12 psdmtw-rounded-3xl psdmtw-bg-gradient-to-tr psdmtw-from-indigo-200"
												></div>
												<img
														src="<?php echo esc_url( plugin_dir_url( __FILE__ ) ) . 'images/emails.jpg'; ?>"
														class="psdmtw-relative psdmtw-mx-auto psdmtw-rounded-lg psdmtw-shadow-lg"
														alt="Image of Emails"
												/>
											</div>
											<!-- END Image -->

											<div class="psdmtw-relative psdmtw-space-y-8">
												<!-- Heading -->
												<div>
													<div
															class="psdmtw-mb-1 psdmtw-text-lg psdmtw-font-bold psdmtw-tracking-wider psdmtw-text-blue-600 psdmtw-uppercase"
													>
														Email Addons Available
													</div>
													<p
															class="psdmtw-text-xl psdmtw-leading-relaxed psdmtw-font-medium psdmtw-text-gray-700"
													>
														Download Magnet offers a variety of email addons to help you automate the funnel and add the new user to
														your email marketing provider as well as using webhooks (Zapier/Make etc.) Connect with your audience. <br/><br/>
													</p>
													<p
															class="psdmtw-text-xl psdmtw-leading-relaxed psdmtw-font-medium psdmtw-text-gray-700"
													>
														Mailchimp, Kit, ActiveCampaign and others are available now. For advanced automation users, a WebHook addon is also available.<br/>
													</p>
												</div>
												<!-- END Heading -->

												<!-- Action -->
												<div class="psdmtw-flex psdmtw-flex-col psdmtw-gap-4 lg:psdmtw-flex-row lg:psdmtw-items-center lg:psdmtw-gap-3">
													<a
															href="https://peachysoftware.com/downloadmagnet/addons/"
															target="_blank"
															class="psdmtw-inline-flex psdmtw-items-center psdmtw-justify-center psdmtw-gap-2 psdmtw-rounded-lg psdmtw-border psdmtw-border-blue-700 psdmtw-bg-blue-700 psdmtw-px-6 psdmtw-py-3 psdmtw-leading-6 psdmtw-font-semibold psdmtw-text-white hover:psdmtw-border-blue-600 hover:psdmtw-bg-blue-600 hover:psdmtw-text-white focus:psdmtw-ring focus:ring-blue-400/50 active:psdmtw-border-blue-700 active:psdmtw-bg-blue-700"
													>
														<span>Find Out More</span>
													</a>
												</div>
												<!-- END Action -->
											</div>
										</div>
									</div>
								</div>
								<!-- END CTA Section: Featured Image Alternate -->

								<?php

							}
							break;
					}//end switch
					?>
				</div>
				<?php
					submit_button( 'Save Settings' );
				?>
			</form>
		</div>
		<?php
	}//end psdm_options_page_html()


	/**
	 * Display the downloaded files page
	 */
	public function psdm_downloaded_page_html() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! class_exists( 'PSDM_List_Table' ) ) {
			include_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
		}

		$wp_list_table = new PSDM_List_Table();
		$wp_list_table->prepare_items();
		?>
		<div class="wrap">
			<h1>
				<?php
				echo esc_html( get_admin_page_title() );
				?>
			</h1>
			<form id="table-filter" method="post">
				<input type="hidden" name="page" value="psdownload_downloaded_files"/>
				<?php
				$wp_list_table->display();
				?>
			</form>
		</div>
		<?php
	}//end psdm_downloaded_page_html()

	/**
	 * Display the welcome page
	 */
	public function psdm_welcome_html() {
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		?>
		<div class="wrap">
			<h1>
				<?php
				echo esc_html( get_admin_page_title() );
				?>
			</h1>
			<p>Welcome to Download Magnet</p>
		</div>
		<?php
	}//end psdm_welcome_html()


	/**
	 * Save the settings.
	 */
	public function psdm_register_settings() {
		register_setting( 'psdm_options', 'psdm_options', array( $this, 'psdm_sanitize' ) );
	}//end psdm_register_settings()


		/**
		 * Sanitize the settings.
		 *
		 * @param array $input The settings.
		 */
	public function psdm_sanitize( $input ) {
		$input['email_address'] = wp_kses_post( $input['email_address'] );
		return $input;
	}//end psdm_sanitize()

	/**
	 * Save the settings.
	 *
	 * @param int  $post_id The post ID.
	 * @param bool $psdm_post The post.
	 */
	public function psdm_save_post( $post_id, $psdm_post = false ) {
		$is_autosave    = wp_is_post_autosave( $post_id );
		$is_revision    = wp_is_post_revision( $post_id );
		$is_valid_nonce = ( isset( $_POST['psdm_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['psdm_nonce'] ) ), basename( __FILE__ ) ) ? 'true' : 'false' );
		if ( $is_autosave || $is_revision || ! $is_valid_nonce ) {
			return;
		}

		if ( isset( $_POST['psdm_permission_text'] ) ) {
			$permission_text = sanitize_text_field( wp_unslash( $_POST['psdm_permission_text'] ) );
			if ( '' === $permission_text ) {
				$permission_text = 'By downloading you give permission to receive emails from us.';
			}
		} else {
			$permission_text = 'By downloading you give permission to receive emails from us.';
		}

		if ( isset( $_POST['psdm_download_text'] ) ) {
			$download_text = sanitize_text_field( wp_unslash( $_POST['psdm_download_text'] ) );
			if ( '' === $download_text ) {
				$download_text = 'Download';
			}
		} else {
			$download_text = 'Download';
		}

		$psdm_posted_data = wp_unslash( $_POST );

		if ( 0 !== count( $psdm_posted_data ) ) {
			$input['psdm_style']           = wp_kses_post( $psdm_posted_data['psdm_style'] );
			$input['psdm_template']        = isset( $psdm_posted_data['psdm_template'] ) ? sanitize_text_field( $psdm_posted_data['psdm_template'] ) : 'default';
			$input['psdm_exit_intent']     = ( isset( $psdm_posted_data['psdm_exit_intent'] ) && 'yes' === sanitize_text_field( $psdm_posted_data['psdm_exit_intent'] ) ? 'yes' : 'false' );
			$input['psdm_delay_seconds']   = ( isset( $psdm_posted_data['psdm_delay_seconds'] ) ? absint( $psdm_posted_data['psdm_delay_seconds'] ) : 0 );
			$input['psdm_filename']        = wp_kses_post( $psdm_posted_data['psdm_filename'] );
			$input['psdm_download_title']  = wp_kses_post( $psdm_posted_data['psdm_download_title'] );
			$input['psdm_description']     = wp_kses_post( $psdm_posted_data['psdm_description'] );
			$input['psdm_name_required']   = ( isset( $psdm_posted_data['psdm_name_required'] ) ? $psdm_posted_data['psdm_name_required'] : 'false' );
			$input['psdm_permission']      = ( isset( $psdm_posted_data['psdm_permission'] ) ? $psdm_posted_data['psdm_permission'] : 'false' );
			$input['psdm_footer_no']       = ( isset( $psdm_posted_data['psdm_footer_no'] ) ? $psdm_posted_data['psdm_footer_no'] : 'false' );
			$input['psdm_permission_text'] = wp_kses_post( $permission_text );
			$input['psdm_download_text']   = wp_kses_post( $download_text );
			$input['psdm_bypass']          = ( isset( $psdm_posted_data['psdm_bypass'] ) ? $psdm_posted_data['psdm_bypass'] : 'false' );
			$input['psdm_backcolor']       = $psdm_posted_data['psdm_backcolor'];
			$input['psdm_forecolor']       = $psdm_posted_data['psdm_forecolor'];
			$input['psdm_bordercolor']     = $psdm_posted_data['psdm_bordercolor'];
			$input['psdm_borderwidth']     = $psdm_posted_data['psdm_borderwidth'];
			$input['psdm_borderradius']    = $psdm_posted_data['psdm_borderradius'];
			// 2.0.1 Bugfix, corrected validation error.
			$input['psdm_mailing_list'] = ( isset( $psdm_posted_data['psdm_mailing_list'] ) ? wp_kses_post( $psdm_posted_data['psdm_mailing_list'] ) : '' );
			$input['psdm_tags']         = ( isset( $psdm_posted_data['psdm_tags'] ) ? wp_kses_post( $psdm_posted_data['psdm_tags'] ) : '' );
			$input['psdm_custom_1']     = ( isset( $psdm_posted_data['psdm_custom_1'] ) ? wp_kses_post( $psdm_posted_data['psdm_custom_1'] ) : '' );
			$input['psdm_custom_2']     = ( isset( $psdm_posted_data['psdm_custom_2'] ) ? wp_kses_post( $psdm_posted_data['psdm_custom_2'] ) : '' );
			$input['psdm_custom_3']     = ( isset( $psdm_posted_data['psdm_custom_3'] ) ? wp_kses_post( $psdm_posted_data['psdm_custom_3'] ) : '' );
			update_post_meta( $post_id, 'psdmcontent', wp_json_encode( $input ) );
			update_post_meta( $post_id, '_psdm_uuid', wp_kses_data( $psdm_posted_data['psdm_uuid'] ) );
		}
	}//end psdm_save_post()


	/**
	 * Load the adminextra css and js.
	 *
	 * @param int $psdm_hook_suffix The hook suffix.
	 */
	public function psdm_load_extra_cssjs( $psdm_hook_suffix ) {
		$psdm_type = get_current_screen()->post_type;
		$psdm_base = get_current_screen()->base;
		if ( 'psdmcontent' === $psdm_type && 'post' === $psdm_base ) {
			wp_enqueue_media();
			wp_enqueue_script(
				'psdownloadmagnet',
				plugin_dir_url( __FILE__ ) . 'js/psdm.js',
				array( 'jquery' ),
				'2.0.2',
				true
			);
			wp_enqueue_script(
				'psdm_color_picker',
				plugin_dir_url( __FILE__ ) . 'js/psdm_cp.js',
				array( 'wp-color-picker' ),
				'2.0.1',
				true
			);
			wp_enqueue_style(
				'psdownloadmagnetcss',
				plugin_dir_url( __FILE__ ) . 'css/downloadmagnet.css',
				array(),
				'1.0.7',
				'all'
			);
			wp_enqueue_script(
				'psdownloadmagnetwizardjs',
				plugin_dir_url( __FILE__ ) . 'js/jquery.smartWizard.min.js',
				array(),
				'6.0.6',
				true
			);
			wp_enqueue_style(
				'psdownloadmagnetwizardcss',
				plugin_dir_url( __FILE__ ) . 'css/smart_wizard_all.min.css',
				array(),
				'1.0.7',
				'all'
			);
			wp_enqueue_style(
				'psadmincss',
				plugin_dir_url( __FILE__ ) . 'css/psdm_admin.css',
				array(),
				'1.0.1',
				'all'
			);

			wp_enqueue_style( 'wp-color-picker' );
		}//end if

		if ( 'psdmcontent' === $psdm_type && 'edit' === $psdm_base ) {

			wp_enqueue_script(
				'psdownloadmagnet',
				plugin_dir_url( __FILE__ ) . 'js/psdm_copy.js',
				array(),
				'1.0.6',
				true
			);
			wp_enqueue_style(
				'psdownloadmagnetcss',
				plugin_dir_url( __FILE__ ) . 'css/downloadmagnet.css',
				array(),
				'1.0.7',
				'all'
			);
			wp_enqueue_style(
				'psadmincss',
				plugin_dir_url( __FILE__ ) . 'css/psdm_admin.css',
				array(),
				'1.0.1',
				'all'
			);

		}

		if ( 'psdmcontent' === $psdm_type && 'psdmcontent_page_psdm_options' === $psdm_base ) {
			wp_enqueue_style(
				'psdownloadmagnetcss',
				plugin_dir_url( __FILE__ ) . 'css/downloadmagnet.css',
				array(),
				'1.0.7',
				'all'
			);
		}

		if ( 'psdmcontent' === $psdm_type && 'psdmcontent_page_psdownload_export_list' === $psdm_base ) {
			wp_enqueue_style(
				'psdownloadmagnetcss',
				plugin_dir_url( __FILE__ ) . 'css/downloadmagnet.css',
				array(),
				'1.0.7',
				'all'
			);

		}
		switch ( $psdm_hook_suffix ) {
			case 'psdmcontent_page_psdm_options':
			case 'psdmcontent_page_psdownload_export_list':
				wp_enqueue_style(
					'psadmincss',
					plugin_dir_url( __FILE__ ) . 'css/psdm_admin.css',
					array(),
					'1.0.1',
					'all'
				);
				break;
		}
	}//end psdm_load_extra_cssjs()


	/**
	 * Process the shortcode.
	 *
	 * @param array $psdm_attributes The attributes.
	 */
	public function psdm_show( $psdm_attributes ) {
		$psdm_attributes    = array_change_key_case( (array) $psdm_attributes, CASE_LOWER );
		$id                 = null;
		$psdm_guid          = wp_rand( 12000, 99999 );
		$psdm_bgcolor       = '#0000cd';
		$psdm_color         = '#ffffff';
		$psdm_download_text = 'Download';
		if ( isset( $psdm_attributes['id'] ) ) {
			$id = $psdm_attributes['id'];
		}
		if ( is_null( $id ) ) {
			return 'DownloadMagnet shortcode not found.';
		}
		if ( isset( $psdm_attributes['button'] ) ) {
			$psdm_button = $psdm_attributes['button'];
		}
		if ( isset( $psdm_attributes['type'] ) ) {
			switch ( $psdm_attributes['type'] ) {
				case 'button':
				case 'popup':
				case 'inline':
					$psdm_style = $psdm_attributes['type'];
					break;
				default:
					$psdm_style = 'popup';
			}
		} else {
			$psdm_style = 'popup';
			$psdm_input = json_decode( get_post_meta( $id, 'psdmcontent', true ), true );
			if ( isset( $psdm_input['psdm_style'] ) ) {
				$psdm_style = $psdm_input['psdm_style'];
			}
		}
		$psdm_do_not_use_wp_footer = '';
		if ( isset( $psdm_input['psdm_footer_no'] ) ) {
			$psdm_do_not_use_wp_footer = $psdm_input['psdm_footer_no'];
		}

		switch ( $psdm_style ) {
			case 'button':
				$psdm_attributes['type']   = 'button';
				$psdm_attributes['uuid']   = $psdm_guid;
				$psdm_attributes['footer'] = true;
				$this->psdm_att            = $psdm_attributes;
				$this->psdm_buttons[]      = $psdm_attributes;
				if ( 'yes' === $psdm_do_not_use_wp_footer ) {
					return $this->psdm_show_modal( $psdm_attributes );
				} else {
					add_action( 'wp_footer', array( $this, 'psdm_footer' ) );
				}
				break;
			case 'inline':
				$psdm_attributes['type']   = 'inline';
				$psdm_attributes['uuid']   = $psdm_guid;
				$psdm_attributes['footer'] = false;
				$this->psdm_att            = $psdm_attributes;
				return $this->psdm_show_modal( $psdm_attributes );
			default:
				$psdm_attributes['type']   = 'popup';
				$psdm_attributes['uuid']   = $psdm_guid;
				$psdm_attributes['footer'] = true;
				$this->psdm_att            = $psdm_attributes;
				$this->psdm_buttons[]      = $psdm_attributes;
				if ( isset( $psdm_input['psdm_download_text'] ) ) {
					$psdm_download_text = $psdm_input['psdm_download_text'];
				}

				if ( isset( $psdm_input['psdm_backcolor'] ) ) {
					$psdm_bgcolor = $psdm_input['psdm_backcolor'];
				}
				if ( isset( $psdm_input['psdm_forecolor'] ) ) {
					$psdm_color = $psdm_input['psdm_forecolor'];
				}
				if ( isset( $psdm_input['psdm_bordercolor'] ) ) {
					$psdm_bordercolor = $psdm_input['psdm_bordercolor'];
				}
				if ( 'yes' === $psdm_do_not_use_wp_footer ) {
					return $this->psdm_show_modal( $psdm_attributes );
				} else {
					add_action( 'wp_footer', array( $this, 'psdm_footer' ) );
				}
				return '<div><button value="' . $psdm_guid . '" onclick="psdmShowModal(this)" class="psdm-button" style="background:' . $psdm_bgcolor . ';color:' . $psdm_color . ';">' . $psdm_download_text . '</button></div>';

		}
	}//end psdm_show()


	/**
	 * Ajax function to get file for download.
	 */
	public function psdm_filedownload() {
		if ( ! isset( $_POST['psdm_submitted'] ) ) {
			wp_die( '0' );
		}

		if ( ! isset( $_POST['psdm_use_nonce'] ) ) {
			wp_die( '0' );
		}

		$result = wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['psdm_use_nonce'] ) ), basename( __FILE__ ) );
		if ( ! $result ) {
			wp_die( '0' );
		}

		$psdm_options = get_option( 'psdm_options' );
		if ( isset( $psdm_options['throwaway'] ) ) {
			$psdm_is_throwaway = true;
		} else {
			$psdm_is_throwaway = false;
		}

		if ( isset( $psdm_options['ipaddress'] ) ) {
			$psdm_donotuse_ipaddress = true;
		} else {
			$psdm_donotuse_ipaddress = false;
		}

		$psdm_ignore_throwaway = false;
		$psdm_uuid             = ( isset( $_POST['psdm_file'] ) ? sanitize_text_field( wp_unslash( $_POST['psdm_file'] ) ) : '' );
		$psdm_name             = ( isset( $_POST['psdm_name'] ) ? sanitize_text_field( wp_unslash( $_POST['psdm_name'] ) ) : '' );
		$psdm_email            = ( isset( $_POST['psdm_email'] ) ? sanitize_email( wp_unslash( $_POST['psdm_email'] ) ) : '' );
		$psdm_honeypot         = ( isset( $_POST['psdm_hp'] ) ? true : false );
		if ( $psdm_honeypot ) {
			if ( '' !== $_POST['psdm_hp'] ) {
				wp_die( '0' );
			}
		}

		// Validate Email.
		if ( $psdm_is_throwaway ) {
			$psdm_email_throwaway = $this->psdm_is_disposable_mail( $psdm_email );
			if ( $psdm_email_throwaway ) {
				$psdm_status           = 'Email was determined DISPOSABLE';
				$psdm_ignore_throwaway = true;
			} else {
				$psdm_status = 'Email address captured';
			}
		} else {
			$psdm_email_throwaway = false;
			$psdm_status          = 'Email address captured';
		}

		$psdm_args      = array(
			'post_type'           => 'psdmcontent',
			'ignore_sticky_posts' => 1,
			'meta_key'            => '_psdm_uuid',
			'meta_value'          => $psdm_uuid,
			'meta_compare'        => '=',
		);
		$psdm_get_posts = new WP_Query( $psdm_args );
		if ( 0 === $psdm_get_posts->post_count ) {
			wp_die( '0' );
		}

		$psdm_post_id            = $psdm_get_posts->posts[0]->ID;
		$psdm_input              = ( isset( $psdm_post_id ) ? json_decode( get_post_meta( wp_kses_post( wp_unslash( $psdm_post_id ) ), 'psdmcontent', true ), true ) : null );
		$psdm_permission         = ( isset( $_POST['psdm_permission'] ) ? sanitize_text_field( wp_unslash( $_POST['psdm_permission'] ) ) : 'false' );
		$psdm_permission_granted = ( 'true' === $psdm_permission ? 1 : 0 );
		$psdm_file               = $psdm_input['psdm_filename'];
		$psdm_title              = get_the_title( $psdm_post_id );
		global  $wpdb;
		$psdm_table_name = $wpdb->prefix . 'psdm_downloads';
		$psdm_time       = current_time( 'mysql' );
		if ( $psdm_donotuse_ipaddress ) {
			$psdm_ip = 'Not Collected';
		} else {
			$psdm_ip = ( ! empty( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '' );
		}

		$wpdb->insert(
			$psdm_table_name,
			array(
				'time'               => $psdm_time,
				'download_id'        => $psdm_post_id,
				'download_name'      => $psdm_title,
				'email_address'      => $psdm_email,
				'name'               => $psdm_name,
				'ip_address'         => $psdm_ip,
				'permission_granted' => $psdm_permission_granted,
				'comments'           => $psdm_status,
			),
			array(
				'%s',
				'%d',
				'%s',
				'%s',
				'%s',
				'%s',
				'%d',
				'%s',
			)
		);
		$db_index   = $wpdb->insert_id;
		$psdm_count = get_post_meta( $psdm_post_id, '_psdm_download_count', true );
		update_post_meta( $psdm_post_id, '_psdm_download_count', ( (int) $psdm_count + 1 ) );
		echo esc_attr( $psdm_file );
		if ( false === $psdm_options ) {
			$psdm_options = array();
		}
		do_action(
			'psdm_download',
			$psdm_input['psdm_mailing_list'],
			$psdm_input['psdm_tags'],
			$psdm_email,
			$psdm_name,
			$db_index,
			isset( $psdm_input['psdm_custom_1'] ) ? $psdm_input['psdm_custom_1'] : '',
			isset( $psdm_input['psdm_custom_2'] ) ? $psdm_input['psdm_custom_2'] : '',
			isset( $psdm_input['psdm_custom_3'] ) ? $psdm_input['psdm_custom_3'] : ''
		);

		if ( ! isset( $psdm_options['download_notify'] ) ) {
			die;
		}
		if ( array_key_exists( 'email_address', $psdm_options ) ) {
			if ( isset( $psdm_options['plain_email'] ) ) {
				$psdm_sendmail = $psdm_options['email_address'];
				$psdm_message  = 'The following file, ' . $psdm_title . ' was downloaded by ' . $psdm_email . '.';
				wp_mail( $psdm_sendmail, 'Download Magnet - File Downloaded', $psdm_message );
			} else {
				$psdm_message    = '<p>The following file, ' . $psdm_title . ' was downloaded by ' . $psdm_email . '.</p>';
				$psdm_email_path = __DIR__ . '/email/notificational.html';

				$psdm_email_body = file_get_contents( $psdm_email_path );
				$psdm_output     = str_replace(
					array(
						'{{NAME}}',
						'{{BODY}}',
						'{{WEBSITE}}',
					),
					array(
						$psdm_title . ' downloaded',
						$psdm_message,
						get_bloginfo( 'name' ),
					),
					$psdm_email_body
				);
				$psdm_sendmail   = $psdm_options['email_address'];
				add_filter( 'wp_mail_content_type', array( $this, 'psdm_set_content_type' ) );
				wp_mail( $psdm_sendmail, 'Download Magnet - File Downloaded', $psdm_output );
				remove_filter( 'wp_mail_content_type', array( $this, 'psdm_set_content_type' ) );
			}
		}

		die;
	}//end psdm_filedownload()


	/**
	 * Filters for text/html on email send
	 */
	public function psdm_set_content_type() {
		return 'text/html';
	}//end psdm_set_content_type()

	/**
	 * Validate if email address is disposable.
	 *
	 * @param string $email          Mailchimp mailing list ID.
	 * @param string $blocklist_path Path to disposable email blocklist.
	 */
	private function psdm_is_disposable_mail( $email, $blocklist_path = null ) {
		if ( ! $blocklist_path ) {
			$blocklist_path = __DIR__ . '/disposable_email_blocklist.conf';
		}

		$disposable_domains = file( $blocklist_path, ( FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES ) );
		$domain             = mb_strtolower( explode( '@', trim( $email ) )[1] );
		return in_array( $domain, $disposable_domains );
	}//end psdm_is_disposable_mail()


	/**
	 * Calculate column widths to the downloads table.
	 */
	public function psdm_table_widths() {
		if ( is_admin() && 'psdmcontent_page_psdownload_downloaded_files' === get_current_screen()->id ) {
			echo '<style type="text/css">';
			echo '.column-id { width:30px !important; overflow:hidden }';
			echo '.column-download_name { width:150px !important; overflow:hidden }';
			echo '.column-name { width:120px !important; overflow:hidden }';
			echo '.column-email_address { width:200px !important; overflow:hidden }';
			echo '.column-comments { width:300px !important; overflow:hidden }';
			echo '</style>';
		} elseif ( is_admin() && 'psdmcontent' === get_current_screen()->post_type ) {
			echo '<style type="text/css">';
			echo '.column-title { width:150px !important; overflow:hidden }';
			echo '.column-downloadType { width:50px !important; overflow:hidden }';
			echo '.column-downloads { width:65px !important; overflow:hidden }';
			echo '.column-shortcode { width:300px !important; overflow:hidden }';
			echo '.column-author { width:100px !important; overflow:hidden }';
			echo '</style>';
		}
	}//end psdm_table_widths()


	/**
	 * Add head titles to the downloads table.
	 *
	 * @param array $psdm_defaults default columns.
	 */
	public function psdm_table_head( $psdm_defaults ) {
		$psdm_defaults['downloadType'] = 'Type';
		$psdm_defaults['downloads']    = 'Downloads';
		$psdm_defaults['shortcode']    = 'Shortcode';
		$psdm_defaults['author']       = 'Added By';
		return $psdm_defaults;
	}//end psdm_table_head()


	/**
	 * Add content to the downloads table.
	 *
	 * @param string  $column_name column name.
	 * @param integer $post_id     post id.
	 */
	public function psdm_event_table_content( $column_name, $post_id ) {
		if ( 'downloads' === $column_name ) {
			$psdm_count = get_post_meta( $post_id, '_psdm_download_count', true );
			echo esc_attr( $psdm_count );
		}

		if ( 'downloadType' === $column_name ) {
			$psdm_input = json_decode( get_post_meta( $post_id, 'psdmcontent', true ), true );
			if ( isset( $psdm_input['psdm_style'] ) ) {
				$psdm_input_style = esc_attr( $psdm_input['psdm_style'] );
			} else {
				$psdm_input_style = 'popup';
			}

			echo esc_attr( $psdm_input_style );
		}

		if ( 'shortcode' === $column_name ) {
			$psdm_input = json_decode( get_post_meta( $post_id, 'psdmcontent', true ), true );
			if ( isset( $psdm_input['psdm_style'] ) ) {
				$psdm_input_style = esc_attr( $psdm_input['psdm_style'] );
			} else {
				$psdm_input_style = 'popup';
			}
			switch ( $psdm_input_style ) {
				case 'button':
					$psdm_copy     = '\'[downloadmagnet id=' . $post_id . ' button=insert button id here]\'';
					$psdm_showcopy = '[downloadmagnet id=' . $post_id . ' button=insert button id here]';
					break;
				default:
					$psdm_copy     = '\'[downloadmagnet id=' . $post_id . ']\'';
					$psdm_showcopy = '[downloadmagnet id=' . $post_id . ']';
			}
			echo esc_attr( $psdm_showcopy ) . '&nbsp;<button class="psdm_shortcode_button" onclick="psdm_copyToClipboard(' . esc_attr( $psdm_copy ) . ', event)";>Copy</button>';
		}
	}//end psdm_event_table_content()

	/**
	 * Build the Modal.
	 *
	 * @param array $psdm_attributes Shortcode attributes.
	 *
	 * @return string
	 */
	private function psdm_show_modal( $psdm_attributes ) {
		$psdm_attributes = array_change_key_case( (array) $psdm_attributes, CASE_LOWER );
		$id              = null;
		$psdm_button     = '';
		$psdm_not_footer = false;
		$psdm_type       = 'button';
		if ( isset( $psdm_attributes['id'] ) ) {
			$id = $psdm_attributes['id'];
		}
		if ( isset( $psdm_attributes['button'] ) ) {
			$psdm_button = $psdm_attributes['button'];
		}
		if ( is_null( $id ) ) {
			return 'DownloadMagnet shortcode not found.';
		}
		$psdm_input = json_decode( get_post_meta( $id, 'psdmcontent', true ), true );
		if ( is_null( $psdm_input ) ) {
			return '';
		}

		if ( get_post_status( $id ) !== 'publish' ) {
			return '';
		}

		$psdm_options = get_option( 'psdm_options' );
		if ( false === $psdm_options ) {
			$psdm_options = array();
		}

		$psdm_permission = $psdm_input['psdm_permission'];
		if ( 'yes' === $psdm_permission ) {
			$psdm_permission = true;
		} else {
			$psdm_permission = false;
		}

		$psdm_name_required = $psdm_input['psdm_name_required'];
		if ( 'yes' === $psdm_name_required ) {
			$psdm_name_required = true;
		} else {
			$psdm_name_required = false;
		}

		if ( isset( $psdm_attributes['type'] ) ) {
			switch ( $psdm_attributes['type'] ) {
				case 'button':
				case 'popup':
				case 'inline':
					$psdm_style = $psdm_attributes['type'];
					break;
				default:
					$psdm_style = 'popup';
			}
		} else {
			$psdm_style = 'popup';
			$psdm_input = json_decode( get_post_meta( $id, 'psdmcontent', true ), true );
			if ( isset( $psdm_input['psdm_style'] ) ) {
				$psdm_style = $psdm_input['psdm_style'];
			}
		}

		if ( 'button' === $psdm_style && '' === $psdm_button ) {
			return 'DownloadMagnet shortcode - button class expected but not set.';
		}

		if ( isset( $psdm_input['psdm_download_text'] ) ) {
			$psdm_download_text = $psdm_input['psdm_download_text'];
		} else {
			$psdm_download_text = 'Download';
		}

		if ( isset( $psdm_input['psdm_backcolor'] ) ) {
			$psdm_bgcolor = $psdm_input['psdm_backcolor'];
		} else {
			$psdm_bgcolor = '#0000cd';
		}

		if ( isset( $psdm_input['psdm_forecolor'] ) ) {
			$psdm_color = $psdm_input['psdm_forecolor'];
		} else {
			$psdm_color = '#ffffff';
		}

		if ( isset( $psdm_input['psdm_bordercolor'] ) ) {
			$psdm_bordercolor = $psdm_input['psdm_bordercolor'];
		} else {
			$psdm_bordercolor = '#000000';
		}

		if ( isset( $psdm_input['psdm_borderwidth'] ) ) {
			$psdm_borderwidth = $psdm_input['psdm_borderwidth'];
		} else {
			$psdm_borderwidth = '2';
		}

		if ( isset( $psdm_input['psdm_borderradius'] ) ) {
			$psdm_borderradius = $psdm_input['psdm_borderradius'];
		} else {
			$psdm_borderradius = '10';
		}

		if ( isset( $psdm_input['psdm_footer_no'] ) ) {
			if ( 'yes' === $psdm_input['psdm_footer_no'] ) {
				$psdm_not_footer = true;
			}
		}

		$psdm_exit_intent   = isset( $psdm_input['psdm_exit_intent'] ) && 'yes' === $psdm_input['psdm_exit_intent'];
		$psdm_delay_seconds = isset( $psdm_input['psdm_delay_seconds'] ) ? absint( $psdm_input['psdm_delay_seconds'] ) : 0;
		$psdm_template_name = isset( $psdm_input['psdm_template'] ) && ! empty( $psdm_input['psdm_template'] ) ? $psdm_input['psdm_template'] : 'default';

		$psdm_uuid            = get_post_meta( $id, '_psdm_uuid', true );
		$psdm_permission_text = $psdm_input['psdm_permission_text'];
		$random               = $psdm_attributes['uuid'];

		if ( $psdm_permission ) {
			$sz_permission = '<p><input class="psdm_permission_' . $random . '" type="checkbox" name="psdm_permission" value="1"> ' . $psdm_permission_text . '</p>';
		} else {
			$sz_permission = '<p><input class="psdm_permission_' . $random . '" type="checkbox" name="psdm_permission" value="1"  hidden checked>' . $psdm_permission_text . '</p>';
		}

		if ( $psdm_name_required ) {
			if ( 'inline' === $psdm_style ) {
				$sz_name_required = '<div class="psdm-inline-edit"><input id="psdm_name_' . $random . '" class="psdm-input" size="40" type="text" name="psdm_name" placeholder="Name"></div>';
			} else {
				switch ( $psdm_template_name ) {
					case 'minimal':
						$sz_name_required = '<input id="psdm_name_' . $random . '" class="psdm-input psdm-minimal-input" type="text" name="psdm_name" placeholder="Name">';
						break;
					default:
						$sz_name_required = '<p class="psdm_dialog_p"><input id="psdm_name_' . $random . '" class="psdm-input" size="40" type="text" name="psdm_name" placeholder="Name"></p>';
				}
			}
			$sz_pass_name = '<input type="hidden" id="psdm_name_required_' . $random . '" value="1">';
		} else {
			$sz_name_required = '';
			$sz_pass_name     = '<input type="hidden" id="psdm_name_required_' . $random . '" value="0">';
		}

		if ( isset( $psdm_options['honeypot'] ) ) {
			$psdm_honeypot    = '<p class="psdm_no_visual"><input id="psdm_pot_' . $random . '" class="psdm-input" size="40" type="text" name="psdm_pot" placeholder="HP"></p>';
			$psdm_hp_required = '<input type="hidden" id="psdm_hp_required_' . $random . '" value="1">';
		} else {
			$psdm_honeypot    = '';
			$psdm_hp_required = '<input type="hidden" id="psdm_hp_required_' . $random . '" value="0">';
		}

		$file               = $psdm_input['psdm_filename'];
		$title              = $psdm_input['psdm_download_title'];
		$description        = $psdm_input['psdm_description'];
		$admin_url          = esc_url( admin_url( 'admin-ajax.php' ) );
		$psdm_nonce         = wp_nonce_field( basename( __FILE__ ), 'psdm_nonce', true, false );
		$psdm_bypass        = '<p class="psdm_checkbox"><a href="#" onclick="psdmjustsend(\'' . $file . '\')" rel="modal:close">All I want is the file.</a> </p>';
		$psdm_template_name = isset( $psdm_input['psdm_template'] ) && ! empty( $psdm_input['psdm_template'] ) ? $psdm_input['psdm_template'] : 'default';
		if ( 'yes' !== $psdm_input['psdm_bypass'] ) {
			$psdm_bypass = '';
		}
		$psdm_output = '';
		if ( 'popup' === $psdm_style || 'button' === $psdm_style ) {
			$dialog_path = __DIR__ . '/dialogs/' . sanitize_file_name( $psdm_template_name ) . '.html';

			if ( ! file_exists( $dialog_path ) ) {
				$dialog_path = __DIR__ . '/dialogs/default.html';
			}

			$psdm_dialog  = file_get_contents( $dialog_path );
			$psdm_output .= str_replace(
				array(
					'{{psdm_title}}',
					'{{psdm_description}}',
					'{{psdm_nonce}}',
					'{{psdm_file}}',
					'{{psdm_url}}',
					'{{psdm_hp_required}}',
					'{{psdm_honeypot}}',
					'{{sz_pass_name}}',
					'{{sz_name_required}}',
					'{{sz_permission}}',
					'{{psdm_bypass}}',
					'{{psdm_bgcolor}}',
					'{{psdm_color}}',
					'{{psdm_download_text}}',
					'{{psdm_bordercolor}}',
					'{{psdm_borderwidth}}',
					'{{psdm_borderradius}}',
					'{{random}}',
				),
				array(
					$title,
					$description,
					$psdm_nonce,
					$psdm_uuid,
					$admin_url,
					$psdm_hp_required,
					$psdm_honeypot,
					$sz_pass_name,
					$sz_name_required,
					$sz_permission,
					$psdm_bypass,
					$psdm_bgcolor,
					$psdm_color,
					$psdm_download_text,
					$psdm_bordercolor,
					$psdm_borderwidth,
					$psdm_borderradius,
					$random,
				),
				$psdm_dialog
			);
		} else {
			$psdm_output .= '<div id="' . $random . '" class="psdm-inline">
            <div class="psdm-inline-content is-layout-constrained">
                  <h3 class="psdm-inline-title">' . $title . '</h3>
                  <div class="psdm-inline-text">' . $description . '</div>
                  <div class="psdm-inline-form">
                      <form method="post">
                          ' . $psdm_nonce . '
                          <input type="hidden" id="psdm_file_' . $random . '" name="psdm_file" value="' . $psdm_uuid . '">
                          <input type="hidden" id="psdm_url_' . $random . '" value="' . $admin_url . '">
                          ' . $psdm_hp_required . '
                          ' . $psdm_honeypot . '
                          ' . $sz_pass_name . '
                          <span class="psdm_error" id="psdm_name_error_' . $random . '"></span>
                          
                          <span class="psdm_error" id="psdm_email_error_' . $random . '"></span>
                          <div class="psdm-inline-block">
                             ' . $sz_name_required . '
                             <div class="psdm-inline-edit"><input id="psdm_email_' . $random . '" class="psdm-inline-input" type="email" name="psdm_email" placeholder="Email"></div>
                             <div class="psdm-inline-right"><button type="button" class="psdm-button psdm-inline-button-download" style="background:' . $psdm_bgcolor . ';color:' . $psdm_color . ';" id="psdm_submitted_' . $random . '" name="psdm_submitted" value="Send" onclick="psdmdownload(\'' . $random . '\')">' . $psdm_download_text . '</button></div>
                          </div>
                          ' . $sz_permission . '
                          <span class="psdm_error" id="psdm_required_error_' . $random . '"></span>
                      </form>
                  </div>
                  ' . $psdm_bypass . '
                </div>
            </div>';
		}
		if ( 'button' === $psdm_style ) {
			$psdm_output .= '<script>jQuery("#' . $psdm_button . '").on("click", function() { psdmShowButtonModal(' . $random . '); });</script>';
		}
		if ( ( 'popup' === $psdm_style || 'button' === $psdm_style ) && ( $psdm_exit_intent || 0 < $psdm_delay_seconds ) ) {
			$psdm_trigger_options = array(
				'exitIntent'   => $psdm_exit_intent,
				'delaySeconds' => $psdm_delay_seconds,
			);
			$psdm_output .= '<script>window.psdmAutoTriggerQueue = window.psdmAutoTriggerQueue || []; window.psdmAutoTriggerQueue.push([' . wp_json_encode( (string) $random ) . ', ' . wp_json_encode( $psdm_trigger_options ) . ']); if (window.psdmRegisterAutoTriggers) { window.psdmRegisterAutoTriggers(' . wp_json_encode( (string) $random ) . ', ' . wp_json_encode( $psdm_trigger_options ) . '); }</script>';
		}
		return $psdm_output;
	}

	/**
	 * Add the modal to the wp_footer hook.
	 *
	 * @return void
	 */
	public function psdm_footer() {
		$psdm_permitted_html = array(
			'div'    => array(
				'id'    => array(),
				'class' => array(),
				'style' => array(),
			),
			'a'      => array(
				'href'    => array(),
				'onclick' => array(),
				'rel'     => array(),
			),
			'span'   => array(
				'id'      => array(),
				'class'   => array(),
				'onclick' => array(),
			),
			'p'      => array(
				'id'    => array(),
				'class' => array(),
			),
			'h3'     => array(
				'class' => array(),
			),
			'form'   => array(
				'method' => array(),
			),
			'input'  => array(
				'style'       => array(),
				'type'        => array(),
				'name'        => array(),
				'value'       => array(),
				'id'          => array(),
				'class'       => array(),
				'size'        => array(),
				'placeholder' => array(),
				'hidden'      => array(),
				'checked'     => array(),
			),
			'script' => array(
				'type' => array(),
			),
			'button' => array(
				'type'    => array(),
				'class'   => array(),
				'id'      => array(),
				'name'    => array(),
				'value'   => array(),
				'onclick' => array(),
				'style'   => array(
					'background' => array(),
					'color'      => array(),
				),
			),
		);
		foreach ( $this->psdm_buttons as $psdm_button ) {
			echo wp_kses( $this->psdm_show_modal( $psdm_button ), $psdm_permitted_html );
		}
	}//end psdm_footer()
}//end class
