<?php /**
 * Plugin Name: Export Users To PDF
 * Description: This Plugin allows you to export users data and metadata into PDF file.
 * Author: Narola Infotech Solutions LLP
 * Version: 1.0
 * Requires at least: 5.2
 * Tested up to: 5.9
 * Requires PHP: 7.2.29
 * License:GPLv2
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) { die; }
use Dompdf\Dompdf;
// Add setting link into plugin listing page
add_filter('plugin_action_links_'.plugin_basename(__FILE__), 'eutp_users_page_link_settings');
function eutp_users_page_link_settings($links){
	$links[] = '<a href="'.admin_url('users.php').'">'.__('Export Users').'</a>';
	return $links;
}
// Add Button into admin side user list
function eutp_export_users(){
	$screen = get_current_screen();
	//Only add to users.php page
	if ( $screen->id != "users" )
		
	return;
	?>
	<script type="text/javascript">
		jQuery(document).ready( function($) {			
			jQuery('.tablenav.top .clear, .tablenav.bottom .clear').before('<form action="#" method="POST">&nbsp;<input class="button button-primary user_export_button" name="forpdf" type="submit" value="Export Users as PDF" /></form>');		
		});
	</script>
	<?php 
}
add_action('admin_footer','eutp_export_users');

function Export_Users_To_PDF() {

	//For PDF
	if (isset($_POST['forpdf']) == true) {	
		if (current_user_can('manage_options')) {
			 // reference the Dompdf namespace
			 include("dompdf/autoload.inc.php");
			 $dompdf = new Dompdf();

			$args = array (
				'order' => 'ASC',
				'orderby' => 'display_name',
				'fields' => 'all',
			);
			// The User Query
			$wp_users = get_users( $args );

			//Write HTML code
			$html="
			<style type='text/css'>	
				table,th,td{
					border: 1px solid;
				}
				table{
					border-collapse: collapse;
				}
				th {
				    text-align: left;
				}
				td, th {
				    border-bottom: 1px solid;
				    padding: 7px;
				}
				table tbody tr:last-child td {
				    border-bottom: none;
				} 
				caption{
					font-size:20px;
					padding-bottom:10px;
				}
			</style>
			<table >
				<caption ><strong>All Users Details</strong></caption>
				<thead >
					<tr>
						<th>User ID</th>
						<th>User Name</th>
						<th>First Name</th>
						<th>Last Name </th>
						<th> Email ID </th>
						<th> Nick Name</th>
						<th> User Role</th>
						<th> Registered Date </th>
					</tr>
				</thead>
				<tbody>
				";
			$count_user=count($wp_users);
			foreach ( $wp_users as $user ) {
				$user_id = $user->ID;
				$user_name = $user->user_login;
				$reg_date = $user->user_registered;
				$meta = get_user_meta($user_id);
				$role = $user->roles;
				$email = $user->user_email;

				$first_name = ( isset($meta['first_name'][0]) && $meta['first_name'][0] != '' ) ? $meta['first_name'][0] : 'NULL' ;
				$last_name  = ( isset($meta['last_name'][0]) && $meta['last_name'][0] != '' ) ? $meta['last_name'][0] : 'NULL' ;
				$nickname = ( isset($meta['nickname'][0]) && $meta['nickname'][0] != '' ) ? $meta['nickname'][0] : 'NULL' ;

				 $html  .=('<tr><td> '.$user_id.'</td> <td> '.$user_name.' </td> <td> '.$first_name.' </td> <td> '.$last_name.' </td> <td> '.$email.'</td> <td>'.$nickname.'</td> <td>'.ucfirst($role[0]).'</td> <td> '.$reg_date.' </td> </tr> ');
			}
			$html.="</tbody>
			</table>" ;
			$dompdf->loadHtml($html);
			
			// Render the HTML as PDF
			$dompdf->render();

			// Output the generated PDF to Browser
			$dompdf->stream('Export_Users_To_PDF');
		}
	}
}
add_action('admin_init','Export_Users_To_PDF');
//you can use admin_init as well
?>

