<?php
	// Header and Footer
add_action( 'wp_head', 'wpfresh5_header_stuff' );
add_action( 'wp_footer', 'wpfresh5_footer_stuff', 45);

function wpfresh5_header_stuff() {
	if( is_admin() )
		return;
	$fb_pixel = trim( wpfresh5_get_settings( array( 'others', 'fheader', 'fb_pixel' ) ) );
	$analytics_code = wpfresh5_get_settings( array( 'others', 'fheader', 'analytics_code' ) );
	$webmaster_code = wpfresh5_get_settings( array( 'others', 'fheader', 'webmaster_code' ) );
	$header_code = wpfresh5_get_settings( array( 'others', 'fheader', 'header_code' ) );
	if( !empty( $fb_pixel ) )
		echo stripslashes( $fb_pixel )."\n";
	if( !empty( $analytics_code ) )
		echo stripslashes( $analytics_code )."\n";
	if( !empty( $webmaster_code ) )
		echo stripslashes( $webmaster_code )."\n";
	if( !empty( $header_code ) )
		echo stripslashes( $header_code )."\n";

	$css_code = wpfresh5_get_settings( array( 'others', 'customcode', 'css_code' ) );
	$css_code = trim( $css_code );
	if( !empty( $css_code ) )
		echo '<style type="text/css">'.$css_code."</style>\n";
}

function wpfresh5_footer_stuff() {
	if( is_admin() )
		return;
	$footer_code = wpfresh5_get_settings( array( 'others', 'fheader', 'footer_code' ) );
	if( !empty( $footer_code ) )
		echo $footer_code."\n";
	$js_code = wpfresh5_get_settings( array( 'others', 'customcode', 'js_code' ) );
	$js_code = trim( $js_code );
	if( !empty( $js_code ) )
		echo '<script>'.stripslashes( $js_code )."</script>\n";
}

// Mail From

// Name and email filter
add_filter( 'wp_mail_from_name', 'wpfresh5_wp_mail_from_name', 100 );
add_filter( 'wp_mail_from', 'wpfresh5_wp_mail_from', 100 );

/**
 * Filter: wp_mail_from_name
 *
 * @since   1.1
 *
 * @param   string  $name  Default name.
 * @return  string         WP Mail From name.
 */
function wpfresh5_wp_mail_from_name( $name ) {
	$mailfrom_name = wpfresh5_get_settings( array( 'others', 'mail', 'mail_from_name' ) );
	if ( ! empty( $mailfrom_name ) /*&& ! $this->is_default_from_name( $mailfrom_name )*/ ) {
		return trim( $mailfrom_name );
	}
	return $name;
}

/**
 * Filter: wp_mail_from
 *
 * @since   1.1
 *
 * @param   string $name  Default email.
 * @return  string        WP Mail From email.
 */
function wpfresh5_wp_mail_from( $email ) {
	// $wp_mailfrom_ii_email = get_option( 'wp_mailfrom_ii_email', '' );
	$mailfrom_email = wpfresh5_get_settings( array( 'others', 'mail', 'mail_from_mail' ) );
	if ( ! empty( $mailfrom_email ) && is_email( $mailfrom_email ) ) {
		// $override_default = get_option( 'wp_mailfrom_ii_override_default', 0 );
		// $override_admin = get_option( 'wp_mailfrom_ii_override_admin', 0 );
			return trim( $mailfrom_email );

		// if ( $override_default == 1 && $this->is_default_from( $email ) ) {
		// 	return $mailfrom_email;
		// }
		// if ( $override_admin == 1 && $this->is_admin_from( $email ) ) {
		// 	return $mailfrom_email;
		// }

	}
	return $email;
}

// Visual

add_action( 'wp_footer', 'wpfresh5_footer_txt', 8 );

function wpfresh5_footer_txt() {
	$footertxt = wpfresh5_get_settings( array( 'others', 'visual', 'footertxt' ) );
	if( $footertxt ) {
		$site_name = get_bloginfo('name');
		echo '<p><a href="'.site_url().'">'.$site_name.'</a> '.date( 'Y' ).'</p>';
	}

	// $scrolltop = wpfresh5_get_settings( array( 'others', 'visual', 'scrolltop' ) );
	// if( $scrolltop ) {
	// 	echo '<p><a href="'.site_url().'">'.$site_name.'</a> '.date( 'Y' ).'</p>';
	// }
}


/*  Main plugin code */
function wpfresh5_footer_scrolltop()
{
	$scrolltop = wpfresh5_get_settings( array( 'others', 'visual', 'scrolltop' ) );
	if( $scrolltop ) {
		$scrolltop_color = wpfresh5_get_settings( array( 'others', 'visual', 'scrolltop_color' ) );
		if( empty( $scrolltop_color ) )
			$scrolltop_color = '#49a0ff';
	?>
		<div id="wpf5-move-to-top" style="background-color:<?php echo $scrolltop_color;?>"><i class="fa fa-chevron-up"></i></div>

	    <script>
	    var wpfresh5_el_vis = false;
	    jQuery(document).ready(function($) {
			jQuery("#wpf5-move-to-top").click(function() {
				jQuery("html, body").animate({ scrollTop: 0}, 2000);
			});

			jQuery(document).scroll(function() {
			  var y = $(this).scrollTop();
			  var el = jQuery('#wpf5-move-to-top')
			  if ( y > 200 ) {
			  	if ( !wpfresh5_el_vis )
			    	wpfresh5_el_vis = 1;
				    el.fadeIn( "slow", function() {
				        wpfresh5_el_vis = 1;
				      });
			  } else {
			  	if ( wpfresh5_el_vis )
			  		  el.fadeOut( "slow", function() {
				        wpfresh5_el_vis = 0;
				  	  });
			  }
			});
	    });
	    </script>
	<?php }
	$gtranslate = wpfresh5_get_settings( array( 'others', 'visual', 'gtranslate' ) );
	if( $gtranslate ) {
		?>
		<div id="wpfresh5-translate">
		    <div id="google_translate_element"></div><script type="text/javascript">
		    function googleTranslateElementInit() {
		    	if ( jQuery(window).width() > 650 ) {
		      new google.translate.TranslateElement({pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE}, 'google_translate_element');
		  		}
		    }
		    </script><script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
	    </div>
    <?php
	}
} //function ended

add_action('wp_footer','wpfresh5_footer_scrolltop');

// Related Posts

function wpfresh5_get_related_posts( $post_id, $related_count, $args = array() ) {
		$args = wp_parse_args( (array) $args, array(
			'orderby' => 'rand',
			'return'  => 'query', // Valid values are: 'query' (WP_Query object), 'array' (the arguments array)
		) );

		$related_args = array(
			'post_type'      => get_post_type( $post_id ),
			'posts_per_page' => $related_count,
			'post_status'    => 'publish',
			'post__not_in'   => array( $post_id ),
			'orderby'        => $args['orderby'],
			'tax_query'      => array()
		);

		$post       = get_post( $post_id );
		$taxonomies = get_object_taxonomies( $post, 'names' );

		foreach ( $taxonomies as $taxonomy ) {
			$terms = get_the_terms( $post_id, $taxonomy );
			if ( empty( $terms ) ) {
				continue;
			}
			$term_list                   = wp_list_pluck( $terms, 'slug' );
			$related_args['tax_query'][] = array(
				'taxonomy' => $taxonomy,
				'field'    => 'slug',
				'terms'    => $term_list
			);
		}

		if ( count( $related_args['tax_query'] ) > 1 ) {
			$related_args['tax_query']['relation'] = 'OR';
		}

		if ( $args['return'] == 'query' ) {
			return new WP_Query( $related_args );
		} else {
			return $related_args;
		}
	}
// hide meta

add_filter( 'the_content', 'wpfresh5_related_posts' );

function wpfresh5_related_posts( $content ) {
	$show_related = wpfresh5_get_settings( array( 'others', 'visual', 'show_related' ) );
	if( !$show_related )
		return $content;
	if( !is_singular( 'post' ) )
		return $content;
	$related = wpfresh5_get_related_posts( get_the_ID(), 3 );
	$str = '';
	if ( $related->have_posts() ):
		$str .= '<div class="related-posts">
			<h3>Related posts</h3>
			<ul>';
				while ( $related->have_posts() ): $related->the_post();
					$str .= '<li>
						<a href="'.get_permalink( get_the_id() ).'">'.get_the_title().'</a>
					</li>';
				endwhile;
			$str .='</ul>
		</div>';
	endif;
	wp_reset_postdata();
	return $content.$str;
}

add_action('loop_start', 'wpfresh5_hide_meta' );

function wpfresh5_hide_meta() {
	$hide_date = wpfresh5_get_settings( array( 'others', 'visual', 'hide_date' ) );
	$hide_author = wpfresh5_get_settings( array( 'others', 'visual', 'hide_author' ) );

	if( $hide_date ) {
		add_filter('the_date', '__return_false');
		add_filter('the_time', '__return_false');
		add_filter('the_modified_date', '__return_false');
		add_filter('get_the_date', '__return_false');
		add_filter('get_the_time', '__return_false');
		add_filter('get_the_modified_date', '__return_false');
	}
	if( $hide_author ) {
		add_filter('the_author', '__return_false');
		add_filter('get_the_author', '__return_false');
		add_filter('get_the_title', '__return_false');
	}

}

// Minify
// $minify = wpfresh5_get_settings( array( 'others', 'visual', 'hide_date' ) );
if( wpfresh5_is_pro() )
	include WPFRESH5_DIR.'/lib/minify.php';

add_action('init', 'wpfresh5_check_google_cdn' );

function wpfresh5_check_google_cdn() {
	$google_libraries = wpfresh5_get_settings( array( 'others', 'optimize', 'google_libraries' ) );
	if( !$google_libraries )
		return;
	if (is_admin()) {
		return;
	}

	global $wp_scripts;
	if (isset($wp_scripts->registered['jquery']->ver)) {
		$ver = $wp_scripts->registered['jquery']->ver;
	} else {
		$ver = '1.12.4';
	}

	wp_deregister_script('jquery');
	wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/$ver/jquery.min.js", false, $ver);
}


// Cleanup

function wpfresh5_cleanup_page(){
?>
<p><span class="proonly">*</span>Delete unneccessary elements from your database (Warning: we recommend doing a full database backup first)</p>
<h2>Options:</h2>
<?php
function wpfresh5_cleanup($type){
	global $wpdb;
	switch($type){
		case "revision":
			$wcu_sql = "DELETE FROM $wpdb->posts WHERE post_type = 'revision'";
			$wpdb->query($wcu_sql);
			break;
		case "draft":
			$wcu_sql = "DELETE FROM $wpdb->posts WHERE post_status = 'draft'";
			$wpdb->query($wcu_sql);
			break;
		case "autodraft":
			$wcu_sql = "DELETE FROM $wpdb->posts WHERE post_status = 'auto-draft'";
			$wpdb->query($wcu_sql);
			break;
		case "moderated":
			$wcu_sql = "DELETE FROM $wpdb->comments WHERE comment_approved = '0'";
			$wpdb->query($wcu_sql);
			break;
		case "spam":
			$wcu_sql = "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam'";
			$wpdb->query($wcu_sql);
			break;
		case "trash":
			$wcu_sql = "DELETE FROM $wpdb->comments WHERE comment_approved = 'trash'";
			$wpdb->query($wcu_sql);
			break;
		case "postmeta":
			$wcu_sql = "DELETE pm FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL";
			//$wcu_sql = "DELETE FROM $wpdb->postmeta WHERE NOT EXISTS ( SELECT * FROM $wpdb->posts WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID )";
			$wpdb->query($wcu_sql);
			break;
		case "commentmeta":
			$wcu_sql = "DELETE FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM $wpdb->comments)";
			$wpdb->query($wcu_sql);
			break;
		case "relationships":
			$wcu_sql = "DELETE FROM $wpdb->term_relationships WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM $wpdb->posts)";
			$wpdb->query($wcu_sql);
			break;
		case "feed":
			$wcu_sql = "DELETE FROM $wpdb->options WHERE option_name LIKE '_site_transient_browser_%' OR option_name LIKE '_site_transient_timeout_browser_%' OR option_name LIKE '_transient_feed_%' OR option_name LIKE '_transient_timeout_feed_%'";
			$wpdb->query($wcu_sql);
			break;
	}
}

function wpfresh5_cleanup_count($type){
	global $wpdb;
	switch($type){
		case "revision":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'revision'";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "draft":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft'";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "autodraft":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'auto-draft'";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "moderated":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "spam":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'spam'";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "trash":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'trash'";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "postmeta":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->postmeta pm LEFT JOIN $wpdb->posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL";
			//$wcu_sql = "SELECT COUNT(*) FROM $wpdb->postmeta WHERE NOT EXISTS ( SELECT * FROM $wpdb->posts WHERE $wpdb->postmeta.post_id = $wpdb->posts.ID )";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "commentmeta":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE comment_id NOT IN (SELECT comment_id FROM $wpdb->comments)";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "relationships":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id=1 AND object_id NOT IN (SELECT id FROM $wpdb->posts)";
			$count = $wpdb->get_var($wcu_sql);
			break;
		case "feed":
			$wcu_sql = "SELECT COUNT(*) FROM $wpdb->options WHERE option_name LIKE '_site_transient_browser_%' OR option_name LIKE '_site_transient_timeout_browser_%' OR option_name LIKE '_transient_feed_%' OR option_name LIKE '_transient_timeout_feed_%'";
			$count = $wpdb->get_var($wcu_sql);
			break;
	}
	return $count;
}

function wpfresh5_cleanup_optimize(){
	global $wpdb;
	$wcu_sql = 'SHOW TABLE STATUS FROM `'.DB_NAME.'`';
	$result = $wpdb->get_results($wcu_sql);
	foreach($result as $row){
		$wcu_sql = 'OPTIMIZE TABLE '.$row->Name;
		$wpdb->query($wcu_sql);
	}
}

	$wcu_message = array();

	if(isset($_POST['wpfresh5_cleanup_revision'])){
		wpfresh5_cleanup('revision');
		$wcu_message[] = __("All revisions deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_draft'])){
		wpfresh5_cleanup('draft');
		$wcu_message[] = __("All drafts deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_autodraft'])){
		wpfresh5_cleanup('autodraft');
		$wcu_message[] = __("All autodrafts deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_moderated'])){
		wpfresh5_cleanup('moderated');
		$wcu_message[] = __("All moderated comments deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_spam'])){
		wpfresh5_cleanup('spam');
		$wcu_message[] = __("All spam comments deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_trash'])){
		wpfresh5_cleanup('trash');
		$wcu_message[] = __("All trash comments deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_postmeta'])){
		wpfresh5_cleanup('postmeta');
		$wcu_message[] = __("All orphan postmeta deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_commentmeta'])){
		wpfresh5_cleanup('commentmeta');
		$wcu_message[] = __("All orphan commentmeta deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_relationships'])){
		wpfresh5_cleanup('relationships');
		$wcu_message[] = __("All orphan relationships deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_feed'])){
		wpfresh5_cleanup('feed');
		$wcu_message[] = __("All dashboard transient feed deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_all'])){
		wpfresh5_cleanup('revision');
		wpfresh5_cleanup('draft');
		wpfresh5_cleanup('autodraft');
		wpfresh5_cleanup('moderated');
		wpfresh5_cleanup('spam');
		wpfresh5_cleanup('trash');
		wpfresh5_cleanup('postmeta');
		wpfresh5_cleanup('commentmeta');
		wpfresh5_cleanup('relationships');
		wpfresh5_cleanup('feed');
		$wcu_message[] = __("All redundant data deleted!","WP-Clean-Up");
	}

	if(isset($_POST['wpfresh5_cleanup_optimize'])){
		wpfresh5_cleanup_optimize();
		$wcu_message[] = __("Database Optimized!","WP-Clean-Up");
	}

	if( !empty( $wcu_message ) ){
		foreach( $wcu_message as $message )
			echo '<div class="notice notice-info fade is-dismissible"><p><strong>' . $message . '</strong></p></div>';
	}
?>

<p>
<table class="widefat" style="width:419px;">
	<thead>
		<tr>
			<th scope="col"><?php _e('Type','WP-Clean-Up'); ?></th>
			<th scope="col" class="centered-col"><?php _e('Count','WP-Clean-Up'); ?></th>
			<th scope="col" class="centered-col"><?php _e('Delete','WP-Clean-Up'); ?></th>
		</tr>
	</thead>
	<tbody id="the-list">
		<tr class="alternate">
			<td class="column-name">
				<b><?php _e('Revision','WP-Clean-Up'); ?></b>
			</td>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('revision'); ?>
			</td>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_revision" value="revision" />
					<!-- <input type="hidden" name="wpfresh5_cleanup_revision" value="revision" /> -->
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('revision')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr>
			<td class="column-name">
				<?php _e('Draft','WP-Clean-Up'); ?>
			</td>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('draft'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_draft" value="draft" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('draft')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr class="alternate">
			<td class="column-name">
				<?php _e('Auto Draft','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('autodraft'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_autodraft" value="autodraft" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('autodraft')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr>
			<td class="column-name">
				<?php _e('Moderated Comments','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('moderated'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_moderated" value="moderated" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('moderated')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr class="alternate">
			<td class="column-name">
				<?php _e('Spam Comments','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('spam'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_spam" value="spam" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('spam')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr>
			<td class="column-name">
				<?php _e('Trash Comments','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('trash'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_trash" value="trash" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('trash')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr class="alternate">
			<td class="column-name">
				<?php _e('Orphan Postmeta','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('postmeta'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_postmeta" value="postmeta" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('postmeta')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr>
			<td class="column-name">
				<?php _e('Orphan Commentmeta','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('commentmeta'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_commentmeta" value="commentmeta" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('commentmeta')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr class="alternate">
			<td class="column-name">
				<?php _e('Orphan Relationships','WP-Clean-Up'); ?>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('relationships'); ?>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_relationships" value="relationships" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('relationships')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
		<tr>
			<td class="column-name">
				<?php _e('Dashboard Transient Feed','WP-Clean-Up'); ?>
			</td>
			<td class="column-name centered-col">
				<?php echo wpfresh5_cleanup_count('feed'); ?>
			</td>
			<td class="column-name centered-col">
				<!-- <form action="" method="post"> -->
					<input type="checkbox" name="wpfresh5_cleanup_feed" value="feed" />
					<!-- <input type="submit" class="<?php if(wpfresh5_cleanup_count('feed')>0){echo 'button-primary';}else{echo 'button';} ?>" value="<?php _e('Delete','WP-Clean-Up'); ?>" /> -->
				<!-- </form> -->
			</td>
		</tr>
	</tbody>
</table>
</p>
<p>
<!-- <form action="" method="post"> -->
	<!-- <input type="hidden" name="wpfresh5_cleanup_all" value="all" /> -->
	<!-- <button type="submit" class="button-primary"><i class="fa fa-remove"></i> Delete All</button> -->
	<button type="button" id="wpfresh5-show-database" class="button button-secondary"><i class="fa fa-database"></i> Show Database Tables</button>
<!-- </form> -->
</p>
<br/>
<div id="wpfresh5-database-tables" style="display: none">
<h2>Database Tables:</h2>

<table class="widefat" style="width:auto">
	<thead>
		<tr>
			<th scope="col"><?php _e('Table','WP-Clean-Up'); ?></th>
			<th scope="col"><?php _e('Size','WP-Clean-Up'); ?></th>
		</tr>
	</thead>
	<tbody id="the-list">
	<?php
		global $wpdb;
		$total_size = 0;
		$alternate = " class='alternate'";
		$wcu_sql = 'SHOW TABLE STATUS FROM `'.DB_NAME.'`';
		$result = $wpdb->get_results($wcu_sql);

		foreach($result as $row){

			$table_size = $row->Data_length + $row->Index_length;
			$table_size = $table_size / 1024;
			$table_size = sprintf("%0.3f",$table_size);

			$every_size = $row->Data_length + $row->Index_length;
			$every_size = $every_size / 1024;
			$total_size += $every_size;

			echo "<tr". $alternate .">
					<td class='column-name'>". $row->Name ."</td>
					<td class='column-name'>". $table_size ." KB"."</td>
				</tr>\n";
			$alternate = (empty($alternate)) ? " class='alternate'" : "";
		}
	?>
	</tbody>
	<tfoot>
		<tr>
			<th scope="col"><?php _e('Total','WP-Clean-Up'); ?></th>
			<th scope="col" style="font-family:Tahoma;"><?php echo sprintf("%0.3f",$total_size).' KB'; ?></th>
		</tr>
	</tfoot>
</table>
<p>
<!-- <form action="" method="post"> -->
	<label>
		<input type="checkbox" name="wpfresh5_cleanup_optimize" value="optimize" />
		Optimize Database Tables
	</label>
	<!-- <button type="submit" class="button-primary"><i class="fa fa-check"></i> Optimize Database Tables</button> -->
<!-- </form> -->
</p>
<br />
</div>
<?php
}
?>