<?php


/**
 * Add submenu to DragBlock Main Menu
 */
add_action('admin_menu', 'dragblock_admin_menu_form_entries');
function dragblock_admin_menu_form_entries()
{
	add_submenu_page(
		DRAGBLOCK_ADMIN_MENU_SLUG,
		__('DragBlock - Form Entries', 'dragblock'),
		__('Form Entries', 'dragblock'),
		'manage_options',
		'edit.php?post_type=' . DRAGBLOCK_FORM_ENTRY,
		''
	);
}


/**
 * customize the table's columns of our custom post type
 */
add_filter('manage_' . DRAGBLOCK_FORM_ENTRY . '_posts_columns', 'dragblock_manage_posts_columns_form_entries', 1, 1);
function dragblock_manage_posts_columns_form_entries($columns)
{
	unset($columns['date']);
	$columns['content'] = __('Content', 'dragblock');
	$columns['referrer'] = __('Referrer', 'dragblock');
	$columns['email'] = __('Emailed', 'dragblock');
	$columns['date'] = __('Date', 'dragblock');
	return $columns;
}


add_action('manage_' . DRAGBLOCK_FORM_ENTRY . '_posts_custom_column', 'dragblock_manage_posts_custom_column_form_entries', 10, 2);
function dragblock_manage_posts_custom_column_form_entries($column, $post_id)
{

	if ('content' === $column) {
		echo wp_kses(
			get_the_content($post_id),
			array(
				'strong' => array(),
				'p' => array()
			)
		);
		return;
	}

	if ('referrer' === $column) {
		$referrer = get_post_meta($post_id, DRAGBLOCK_FORM_ENTRY . '--_wp_http_referer', true);
		$referrer_html = '<a target="_blank" href="' . esc_url($referrer) . '">' . esc_html($referrer) . '</a>';

		echo wp_kses(
			$referrer_html,
			array(
				'a' => array(
					'target' => array(),
					'href' => array(),
				),
			)
		);

		return;
	}

	if ('email' === $column) {
		$failedEmail = get_post_meta($post_id, DRAGBLOCK_FORM_ENTRY . '-failed-email', true);
		if ('PASSED' === $failedEmail) {
			echo wp_kses(
				'<span class="dragblock-form-emailed-successful">' . __('SENT', 'dragblock') . '</span>',
				array(
					'span' => array(
						'class' => array(),
					),
				)
			);
		} elseif (!$failedEmail) {
			
			echo wp_kses(
				'<strong class="dragblock-form-emailed-local">' . __('LOCAL', 'dragblock') . '</strong>',
				array(
					'strong' => array(
						'class' => array(),
					),
				)
			);			
		} else {
			echo wp_kses(
				'<strong class="dragblock-form-emailed-failed">' . __('FAILED', 'dragblock') . '</strong>',
				array(
					'strong' => array(
						'class' => array(),
					),
				)
			);
		}
		return;
	}
}