<?php
/*
Plugin Name: CRM
Description: Plugin for My Functions
Author: ITlinks Pvt Ltd
Version: 1.1
*/

function wpb_custom_logo() {
echo '
<style type="text/css">
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
background-image: url(' . home_url() . '/wp-content/uploads/2022/01/crm-logo.jpeg) !important;
background-position: 0 0;
color:rgba(0, 0, 0, 0);
}
		#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
		background-position: 0 0;
}
</style>
';
}

//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpb_custom_logo');

class mishaDateRange{

	function __construct(){

		// if you do not want to remove default "by month filter", remove/comment this line
		add_filter( 'months_dropdown_results', '__return_empty_array' );

		// include CSS/JS, in our case jQuery UI datepicker
		add_action( 'admin_enqueue_scripts', array( $this, 'jqueryui' ) );

		// HTML of the filter
		add_action( 'restrict_manage_posts', array( $this, 'form' ) );

		// the function that filters posts
		add_action( 'pre_get_posts', array( $this, 'filterquery' ) );

	}

	/*
	 * Add jQuery UI CSS and the datepicker script
	 * Everything else should be already included in /wp-admin/ like jquery, jquery-ui-core etc
	 * If you use WooCommerce, you can skip this function completely
	 */
	function jqueryui(){
		wp_enqueue_style( 'jquery-ui', '//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.min.css' );
		wp_enqueue_script( 'jquery-ui-datepicker' );
	}

	/*
	 * Two input fields with CSS/JS
	 * If you would like to move CSS and JavaScript to the external file - welcome.
	 */
	function form(){

		$from = ( isset( $_GET['mishaDateFrom'] ) && $_GET['mishaDateFrom'] ) ? $_GET['mishaDateFrom'] : '';
		$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';

		echo '
		<style>
			input[name="mishaDateFrom"], input[name="mishaDateTo"]{
			line-height: 28px;
			height: 28px;
			margin: 0;
			width:125px;
		}
		</style>

		<input type="text" name="mishaDateFrom" placeholder="Date from" value="' . esc_attr( $from ) . '" />
		<input type="text" name="mishaDateTo" placeholder="Date to" value="' . esc_attr( $to ) . '" />

		<script>
		jQuery( function($) {
			var from = $(\'input[name="mishaDateFrom"]\'),
			    to = $(\'input[name="mishaDateTo"]\');

			$( \'input[name="mishaDateFrom"], input[name="mishaDateTo"]\' ).datepicker( {dateFormat : "yy-mm-dd"} );
			// by default, the dates look like this "April 3, 2017"
    			// I decided to make it 2017-04-03 with this parameter datepicker({dateFormat : "yy-mm-dd"});


    			// the rest part of the script prevents from choosing incorrect date interval
    			from.on( \'change\', function() {
				to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
	}

	/*
	 * The main function that actually filters the posts
	 */
	function filterquery( $admin_query ){
		global $pagenow;

		if (
			is_admin()
			&& $admin_query->is_main_query()
			// by default filter will be added to all post types, you can operate with $_GET['post_type'] to restrict it for some types
			&& in_array( $pagenow, array( 'edit.php', 'upload.php' ) )
			&& ( ! empty( $_GET['mishaDateFrom'] ) || ! empty( $_GET['mishaDateTo'] ) )
		) {

			$admin_query->set(
				'date_query', // I love date_query appeared in WordPress 3.7!
				array(
					'after' => sanitize_text_field( $_GET['mishaDateFrom'] ), // any strtotime()-acceptable format!
					'before' => sanitize_text_field( $_GET['mishaDateTo'] ),
					'inclusive' => true, // include the selected days as well
					'column'    => 'post_date' // 'post_modified', 'post_date_gmt', 'post_modified_gmt'
				)
			);

		}

		return $admin_query;

	}
}
 new mishaDateRange();

function redirect_user_on_role(){
    //retrieve current user info
    global $current_user;
        get_currentuserinfo();
    //If login user role is Subscriber
    if ( in_array( 'deo', $current_user->roles ) ) {
        // wp_redirect( admin_url() ); exit;
    }
    else if ($current_user->user_level == 0)
        {
            wp_redirect( home_url() ); exit;
        }
    //If login user role is Contributor
    else if ($current_user->user_level > 1)
    {
             wp_redirect( home_url() ); exit;
        }
    //If login user role is Editor
        else if ($current_user->user_level >8)
        {
        wp_redirect( home_url() ); exit;
        }
    // For other roles
    else
        {
            $redirect_to = 'http://google.com/';
            return $redirect_to;
    }
}
// add_action('admin_init','redirect_user_on_role');

function display_none_menu_post(){
	global $current_user;
	get_currentuserinfo();
	if ( in_array( 'deo', $current_user->roles ) ) {
		echo "
		<style>
			#menu-posts {
		    display: none;
			}
		</style>
		";
	}

}
add_action('admin_init','display_none_menu_post');



function custom_login_redirect( $redirect_to, $request, $user ) {
  if ( isset( $user->roles ) && is_array( $user->roles ) ) {
    if ( in_array( 'administrator', $user->roles ) || in_array( 'editor', $user->roles ) || in_array( 'author', $user->roles ) || in_array( 'deo', $user->roles ) ) {
      $redirect_to = admin_url();
      } else if ( in_array( 'customer', $user->roles ) || in_array( 'shop_manager', $user->roles ) ) {
        $redirect_to = home_url( '/shop' );
      } else {
        $redirect_to = home_url();
      }
    }
  return $redirect_to;
}
// add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );


add_action( 'init', 'psp_register_cpt_projects');
function psp_register_cpt_projects() {
    $args = array(
   'label'               => __( 'cus_recs', 'cus_recs' ),
   'description'         => __( 'cus_rec', 'cus_rec' ),
   'supports'            => array( 'title', 'comments', 'revisions', ),
   'hierarchical'        => false,
   'public'              => true,
   'show_ui'             => true,
   'capability_type'     => array('cus_rec','cus_recs'),
   'map_meta_cap'        => true,
   );
   register_post_type( 'cus_rec', $args );
}

/**
 * Overwrite args of custom post type registered by plugin
 */
add_filter( 'register_post_type_args', 'change_capabilities_of_course_document' , 10, 2 );

function change_capabilities_of_course_document( $args, $post_type ){

 // Do not filter any other post type
 if ( 'cus_rec' !== $post_type ) {

     // Give other post_types their original arguments
     return $args;

 }

 // Change the capabilities of the "course_document" post_type
 $args['capabilities'] = array(
            'edit_post' => 'edit_cus_rec',
            'edit_posts' => 'edit_cus_recs',
            'edit_others_posts' => 'edit_other_cus_recs',
            'publish_posts' => 'publish_cus_recs',
            'read_post' => 'read_cus_rec',
            'read_private_posts' => 'read_private_cus_recs',
            'delete_post' => 'delete_cus_rec'
        );

  // Give the course_document post type it's arguments
  return $args;

}

/** add teachers capability */
// add_action('admin_init','rpt_add_role_caps',999);

function rpt_add_role_caps() {

    $role = get_role('rpt_teacher');
    $role->add_cap( 'read_cus_rec');
    $role->add_cap( 'edit_cus_rec' );
    $role->add_cap( 'edit_cus_recs' );
    $role->add_cap( 'edit_other_cus_recs' );
    $role->add_cap( 'edit_published_cus_recs' );
    $role->add_cap( 'publish_cus_recs' );
    $role->add_cap( 'read_private_cus_recs' );
    $role->add_cap( 'delete_cus_rec' );


}

/** add teachers capability */
// add_action('admin_init','admin_add_role_caps',999);
function admin_add_role_caps() {
    $role = get_role('administrator');
    $role->add_cap( 'read_cus_rec');
    $role->add_cap( 'edit_cus_rec' );
    $role->add_cap( 'edit_cus_recs' );
    $role->add_cap( 'edit_other_cus_recs' );
    $role->add_cap( 'edit_published_cus_recs' );
    $role->add_cap( 'publish_cus_recs' );
    $role->add_cap( 'read_private_cus_recs' );
    $role->add_cap( 'delete_cus_rec' );
}

remove_action('welcome_panel', 'wp_welcome_panel');
add_action( 'wp_dashboard_setup', 'remove_site_health_widget' );
function remove_site_health_widget() {
    global $wp_meta_boxes;
    unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_site_health'] );
}

function remove_dashboard_meta() {
  remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
  remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
  remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
  remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
  remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
  remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
  remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
  remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
  remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
}
add_action( 'admin_init', 'remove_dashboard_meta' );

// http://www.wptricks24.com/add-section-wordpress-admin-dashboard/
// License(s) expiring today
function expire_license_widget_content() {
  $expiry_date = gmdate("Ymd", strtotime('+ 724 hours')); // UAE +4 hours + 30 days
   display_expiry_data("license", $expiry_date);
}
function expire_license_today_add_dashboard_widgets() {
    wp_add_dashboard_widget('expire_license_widget', 'License(s) expiring in next 30 days', 'expire_license_widget_content');
}
add_action('wp_dashboard_setup', 'expire_license_today_add_dashboard_widgets' );

// Establishment cards(s) expiring today
function expire_establishment_card_widget_content() {
  $expiry_date = gmdate("Ymd", strtotime('+ 724 hours')); // UAE +4 hours + 30 days
  display_expiry_data("establishment_card", $expiry_date);
}
function expire_establishment_card_today_add_dashboard_widgets() {
    wp_add_dashboard_widget('expire_establishment_card_widget', 'Establishment cards(s) expiring in next 30 days', 'expire_establishment_card_widget_content');
}
add_action('wp_dashboard_setup', 'expire_establishment_card_today_add_dashboard_widgets');

function display_expiry_data($type, $expiry_date){
  $today_date = gmdate("Ymd", strtotime('+ 4 hours')); // UAE +4
	$args = array(
		'post_type'      => 'cus_rec', 'posts_per_page' => 10000, 'orderby' => $type.'_expiry_date', 'order' => 'ASC', 'meta_query'     => array(
			array('key'     => $type.'_expiry_date','value'   => array($today_date,$expiry_date),'compare' => 'BETWEEN','type'    => 'NUMERIC',), ),
		);
		// print_r ($args);
		// echo "<br/>";
	$query = new WP_Query($args);
  $data = '<table width="100%">
	         <tr>
	         <td><strong>Name</strong></td>
				   <td><strong>Email</strong></td>
				   <td><strong>Cell number</strong></td>
				   <td><strong>Expiry date</strong></td>
			   <tr>';
  if($query->have_posts()) {
   	while ($query->have_posts()) : $query->the_post();
      $data.='<tr>
   		       <td><a href="'. get_edit_post_link(). '" target="_new">'. get_the_title() .'</a></td>
   		       <td>'. get_field("email"). '</td>
   		       <td>'. get_field("cell_number"). '</td>
   		       <td>'. get_field($type."_expiry_date"). '</td>
            </tr>';
   	endwhile;
}else {
  $data.='<tr><td colspan="4" align="center">No Data</td></tr>';
}
  $data.='</table>';
 	echo $data;
}
add_action('admin_head', 'my_custom_css');

function my_custom_css() {
  echo '<style>
				@media only screen and (min-width: 767px){
					#dashboard-widgets .postbox-container {width: 50% !important;}
				}
				.manage-column {vertical-align: top;}
				#ul {list-style-type: disc;margin-left: 20px;}
         #expired_establishment_card_dashboard_widget h2.hndle.ui-sortable-handle { color: red;}
		     #expired_license_dashboard_widget h2.hndle.ui-sortable-handle { color: red;}
				 table, th, td {border: 1px solid grey;border-collapse: collapse;border-style: ridge;}
        </style>';
}

// Established Card(s) expired data last 90 days
function expired_establishment_card_widget_content() {

  $prev_date = gmdate("Ymd", strtotime('- 2164 hours')); // UAE +4 hours + 90 days
   display_expired_data("establishment_card", $prev_date);
}
function expired_establishment_card_dashboard_widget() {
    wp_add_dashboard_widget('expired_establishment_card_dashboard_widget', 'Establishment Card(s) expired in last 90 days', 'expired_establishment_card_widget_content');
}
add_action('wp_dashboard_setup', 'expired_establishment_card_dashboard_widget' );



// License(s) expired data last 90 days
function expired_license_widget_content() {
  $prev_date = gmdate("Ymd", strtotime('- 2164 hours')); // UAE +4 hours + 90 days
   display_expired_data("license", $prev_date);
}
function expired_license_dashboard_widget() {
    wp_add_dashboard_widget('expired_license_dashboard_widget', 'License(s) expired in last 90 days', 'expired_license_widget_content');
}
add_action('wp_dashboard_setup', 'expired_license_dashboard_widget' );


function display_expired_data($type, $prev_date){
  $today_date = gmdate("Ymd", strtotime('+ 4 hours')); // UAE +4
	$args = array(
		'post_type'      => 'cus_rec', 'posts_per_page' => 10000, 'orderby' => $type.'_expiry_date', 'order' => 'DESC', 'meta_query'     => array(
			array('key'     => $type.'_expiry_date','value'   => array($prev_date,$today_date),'compare' => 'BETWEEN','type'    => 'NUMERIC',), ),
		);
		// print_r ($args);
		// echo "<br/>";
	$query = new WP_Query($args);
  $data = '<table width="100%">
	         <tr>
	         <td><strong>Name</strong></td>
				   <td><strong>Email</strong></td>
				   <td><strong>Cell number</strong></td>
				   <td><strong>Expiry date</strong></td>
			   <tr>';
  if($query->have_posts()) {
   	while ($query->have_posts()) : $query->the_post();
      $data.='<tr>
   		       <td><a href="'. get_edit_post_link(). '" target="_new">'. get_the_title() .'</a></td>
   		       <td>'. get_field("email"). '</td>
   		       <td>'. get_field("cell_number"). '</td>
   		        <td>'. get_field($type."_expiry_date"). '</td>
              </tr>';
   	endwhile;
}else {
  $data.='<tr><td colspan="4" align="center">No Data</td></tr>';
}
  $data.='</table>';
 	echo $data;
}





function new_rec_add_dashboard_contents() {
	echo '
		<ul id="ul">
	  	<li><a href="'.admin_url().'edit.php?post_type=cus_rec">All customer records</a></li>
	  	<li><a href="'.admin_url().'post-new.php?post_type=cus_rec">Add new customer record</a></li>
	  	<li><a href="'.admin_url().'edit-tags.php?taxonomy=license_type&post_type=cus_rec">License types</a></li>
			<li><a href="'.admin_url().'edit-tags.php?taxonomy=license_authority&post_type=cus_rec">License authorities</a></li>
		</ul>
	';
}
function new_rec_add_dashboard_widgets() {
    wp_add_dashboard_widget('new_rec_add_dashboard_widget', 'Shortcuts', 'new_rec_add_dashboard_contents');
}
add_action('wp_dashboard_setup', 'new_rec_add_dashboard_widgets' );



// https://en.bainternet.info/add-taxonomy-filter-to-custom-post-type/
if (!class_exists('Tax_CTP_Filter')){
   /**
     * Tax CTP Filter Class
     * Simple class to add custom taxonomy dropdown to a custom post type admin edit list
     * @author Ohad Raz <admin@bainternet.info>
     * @version 0.1
     */
     class Tax_CTP_Filter
     {
         /**
          * __construct
          * @author Ohad Raz <admin@bainternet.info>
          * @since 0.1
          * @param array $cpt [description]
          */
         function __construct($cpt = array()){
             $this->cpt = $cpt;
             // Adding a Taxonomy Filter to Admin List for a Custom Post Type
             add_action( 'restrict_manage_posts', array($this,'my_restrict_manage_posts' ));
         }

         /**
          * my_restrict_manage_posts  add the slelect dropdown per taxonomy
          * @author Ohad Raz <admin@bainternet.info>
          * @since 0.1
          * @return void
          */
         public function my_restrict_manage_posts() {
             // only display these taxonomy filters on desired custom post_type listings
             global $typenow;
             $types = array_keys($this->cpt);
             if (in_array($typenow, $types)) {
                 // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
                 $filters = $this->cpt[$typenow];
                 foreach ($filters as $tax_slug) {
                     // retrieve the taxonomy object
                     $tax_obj = get_taxonomy($tax_slug);
                     $tax_name = $tax_obj->labels->name;

                     // output html for taxonomy dropdown filter
                     echo "<select name='".strtolower($tax_slug)."' id='".strtolower($tax_slug)."' class='postform'>";
                     echo "<option value=''>Show All $tax_name</option>";
                     $this->generate_taxonomy_options($tax_slug,0,0,(isset($_GET[strtolower($tax_slug)])? $_GET[strtolower($tax_slug)] : null));
                     echo "</select>";
                 }
             }
         }

         /**
          * generate_taxonomy_options generate dropdown
          * @author Ohad Raz <admin@bainternet.info>
          * @since 0.1
          * @param  string  $tax_slug
          * @param  string  $parent
          * @param  integer $level
          * @param  string  $selected
          * @return void
          */
         public function generate_taxonomy_options($tax_slug, $parent = '', $level = 0,$selected = null) {
             $args = array('show_empty' => 1);
             if(!is_null($parent)) {
                 $args = array('parent' => $parent);
             }
             $terms = get_terms($tax_slug,$args);
             $tab='';
             for($i=0;$i<$level;$i++){
                 $tab.='--';
             }

             foreach ($terms as $term) {
                 // output each select option line, check against the last $_GET to show the current option selected
                 echo '<option value='. $term->slug, $selected == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>';
                 $this->generate_taxonomy_options($tax_slug, $term->term_id, $level+1,$selected);
             }

         }
     }//end class
 }//end if
new Tax_CTP_Filter(array('cus_rec' => array('license_type','license_authority')));

// https://www.templatemonster.com/help/wordpress-how-to-replace-enter-title-here-placeholder-text.html
function wpb_change_title_text( $title ){
     $screen = get_current_screen();
     if  ( 'cus_rec' == $screen->post_type ) {
          $title = 'Enter customer name';
     }
     return $title;
}
add_filter( 'enter_title_here', 'wpb_change_title_text' );


//// LICENSE ISSUE DATES START ////

function license_issue_date_input() {
    $scr = get_current_screen();
    //if ( $scr->base !== 'edit' && $scr->post_type !== 'cv') return;
    if ($scr->post_type !== 'cus_rec') return;
		// LICENSE DATES START
    $license_issue_from_date = ( isset( $_GET['license_issue_from_date'] ) && $_GET['license_issue_from_date'] ) ? $_GET['license_issue_from_date'] : '';
    $license_issue_to_date = ( isset( $_GET['license_issue_to_date'] ) && $_GET['license_issue_to_date'] ) ? $_GET['license_issue_to_date'] : '';

		//$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';
    echo '<input type="text" class="datepicker" name="license_issue_from_date" placeholder="License issue date from" value="' . esc_attr( $license_issue_from_date ) . '">';
    echo '<input type="text" class="datepicker" name="license_issue_to_date" placeholder="License issue date to" value="' . esc_attr( $license_issue_to_date ) . '">';

    echo '<script>
		jQuery( function($) {

			    var from = $(\'input[name="license_issue_from_date"]\'),
			    to = $(\'input[name="license_issue_to_date"]\');

			    $( \'input[name="license_issue_from_date"], input[name="license_issue_to_date"]\' ).datepicker( {dateFormat : "dd/mm/yy"} );

    		  from.on( \'change\', function() {
				  to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
}
 add_action('restrict_manage_posts', 'license_issue_date_input');

/**
 * Filter the results based on meta data.
 */
// https://pineco.de/filter-by-acf-meta-data-in-wordpress-admin/
function license_issue_date_query_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
      $scr = get_current_screen();
      if ( $scr->base !== 'edit' && $scr->post_type !== 'cus_rec' ) return;

      if ( !empty($_GET['license_issue_from_date']) && !empty($_GET['license_issue_to_date']) ) {
        $license_issue_from_date = sanitize_text_field($_GET['license_issue_from_date']);
        $license_issue_from_date = substr($license_issue_from_date,6,4).substr($license_issue_from_date,3,2).substr($license_issue_from_date,0,2);

        $license_issue_to_date = sanitize_text_field($_GET['license_issue_to_date']);
        $license_issue_to_date = substr($license_issue_to_date,6,4).substr($license_issue_to_date,3,2).substr($license_issue_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'license_issue_date',
          'value' => array($license_issue_from_date , $license_issue_to_date ),
          'compare' => 'BETWEEN',
          'type'    => 'NUMERIC'
        ) ) );
      }
      elseif ( !empty($_GET['license_issue_from_date']) ) {

        $license_issue_from_date = sanitize_text_field($_GET['license_issue_from_date']);
        $license_issue_from_date = substr($license_issue_from_date,6,4).substr($license_issue_from_date,3,2).substr($license_issue_from_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'license_issue_date',
          'value' => $license_issue_from_date,
          'compare' => '>=',
          'type'    => 'NUMERIC'
        ) ) );
      } elseif (!empty($_GET['license_issue_to_date']) ) {

        $license_issue_to_date = sanitize_text_field($_GET['license_issue_to_date']);
        $license_issue_to_date = substr($license_issue_to_date,6,4).substr($license_issue_to_date,3,2).substr($license_issue_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'license_issue_date',
          'value' => $license_issue_to_date,
          'compare' => '<=',
          'type'    => 'NUMERIC'
        ) ) );
      }
    }
}

 add_action('pre_get_posts','license_issue_date_query_filter');

//// LICENSE ISSUE DATES END ////

/////  LICENSE EXPIRY DATE START //////////
function license_expiry_date_input() {
    $scr = get_current_screen();
    //if ( $scr->base !== 'edit' && $scr->post_type !== 'cv') return;
    if ($scr->post_type !== 'cus_rec') return;
		// LICENSE DATES START
    $license_expiry_from_date = ( isset( $_GET['license_expiry_from_date'] ) && $_GET['license_expiry_from_date'] ) ? $_GET['license_expiry_from_date'] : '';
    $license_expiry_to_date = ( isset( $_GET['license_expiry_to_date'] ) && $_GET['license_expiry_to_date'] ) ? $_GET['license_expiry_to_date'] : '';

		//$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';
    echo '<input type="text" class="datepicker" name="license_expiry_from_date" placeholder="License expiry date from" value="' . esc_attr( $license_expiry_from_date ) . '">';
    echo '<input type="text" class="datepicker" name="license_expiry_to_date" placeholder="License expiry date to" value="' . esc_attr( $license_expiry_to_date ) . '">';

    echo '<script>
		jQuery( function($) {

			    var from = $(\'input[name="license_expiry_from_date"]\'),
			    to = $(\'input[name="license_expiry_to_date"]\');

			    $( \'input[name="license_expiry_from_date"], input[name="license_expiry_to_date"]\' ).datepicker( {dateFormat : "dd/mm/yy"} );

    		  from.on( \'change\', function() {
				  to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
		// LICENSE DATES END

}

 add_action('restrict_manage_posts', 'license_expiry_date_input');

/**
 * Filter the results based on meta data.
 */
// https://pineco.de/filter-by-acf-meta-data-in-wordpress-admin/
function license_expiry_date_query_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
      $scr = get_current_screen();
      if ( $scr->base !== 'edit' && $scr->post_type !== 'cus_rec' ) return;

      if ( !empty($_GET['license_expiry_from_date']) && !empty($_GET['license_expiry_to_date']) ) {
        $license_expiry_from_date = sanitize_text_field($_GET['license_expiry_from_date']);
        $license_expiry_from_date = substr($license_expiry_from_date,6,4).substr($license_expiry_from_date,3,2).substr($license_expiry_from_date,0,2);

        $license_expiry_to_date = sanitize_text_field($_GET['license_expiry_to_date']);
        $license_expiry_to_date = substr($license_expiry_to_date,6,4).substr($license_expiry_to_date,3,2).substr($license_expiry_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'license_expiry_date',
          'value' => array($license_expiry_from_date , $license_expiry_to_date ),
          'compare' => 'BETWEEN',
          'type'    => 'NUMERIC'
        ) ) );
      }
      elseif ( !empty($_GET['license_expiry_from_date']) ) {

        $license_expiry_from_date = sanitize_text_field($_GET['license_expiry_from_date']);
        $license_expiry_from_date = substr($license_expiry_from_date,6,4).substr($license_expiry_from_date,3,2).substr($license_expiry_from_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'license_expiry_date',
          'value' => $license_expiry_from_date,
          'compare' => '>=',
          'type'    => 'NUMERIC'
        ) ) );
      } elseif (!empty($_GET['license_expiry_to_date']) ) {

        $license_expiry_to_date = sanitize_text_field($_GET['license_expiry_to_date']);
        $license_expiry_to_date = substr($license_expiry_to_date,6,4).substr($license_expiry_to_date,3,2).substr($license_expiry_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'license_expiry_date',
          'value' => $license_expiry_to_date,
          'compare' => '<=',
          'type'    => 'NUMERIC'
        ) ) );
      }
    }
}

 add_action('pre_get_posts','license_expiry_date_query_filter');

/////  LICENSE EXPIRY DATE END //////////


///// VISA ISSUE DATE START /////



function visa_issue_date_input() {
    $scr = get_current_screen();
    //if ( $scr->base !== 'edit' && $scr->post_type !== 'cv') return;
    if ($scr->post_type !== 'cus_rec') return;
		// visa DATES START
    $visa_issue_from_date = ( isset( $_GET['visa_issue_from_date'] ) && $_GET['visa_issue_from_date'] ) ? $_GET['visa_issue_from_date'] : '';
    $visa_issue_to_date = ( isset( $_GET['visa_issue_to_date'] ) && $_GET['visa_issue_to_date'] ) ? $_GET['visa_issue_to_date'] : '';

		//$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';
    echo '<input type="text" class="datepicker" name="visa_issue_from_date" placeholder="Visa issue date from" value="' . esc_attr( $visa_issue_from_date ) . '">';
    echo '<input type="text" class="datepicker" name="visa_issue_to_date" placeholder="Visa issue date to" value="' . esc_attr( $visa_issue_to_date ) . '">';

    echo '<script>
		jQuery( function($) {

			    var from = $(\'input[name="visa_issue_from_date"]\'),
			    to = $(\'input[name="visa_issue_to_date"]\');

			    $( \'input[name="visa_issue_from_date"], input[name="visa_issue_to_date"]\' ).datepicker( {dateFormat : "dd/mm/yy"} );

    		  from.on( \'change\', function() {
				  to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
}

 add_action('restrict_manage_posts', 'visa_issue_date_input');

/**
 * Filter the results based on meta data.
 */
// https://pineco.de/filter-by-acf-meta-data-in-wordpress-admin/
function visa_issue_date_query_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
      $scr = get_current_screen();
      if ( $scr->base !== 'edit' && $scr->post_type !== 'cus_rec' ) return;

      if ( !empty($_GET['visa_issue_from_date']) && !empty($_GET['visa_issue_to_date']) ) {
        $visa_issue_from_date = sanitize_text_field($_GET['visa_issue_from_date']);
        $visa_issue_from_date = substr($visa_issue_from_date,6,4).substr($visa_issue_from_date,3,2).substr($visa_issue_from_date,0,2);

        $visa_issue_to_date = sanitize_text_field($_GET['visa_issue_to_date']);
        $visa_issue_to_date = substr($visa_issue_to_date,6,4).substr($visa_issue_to_date,3,2).substr($visa_issue_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'visa_issue_date',
          'value' => array($visa_issue_from_date , $visa_issue_to_date ),
          'compare' => 'BETWEEN',
          'type'    => 'NUMERIC'
        ) ) );
      }
      elseif ( !empty($_GET['visa_issue_from_date']) ) {

        $visa_issue_from_date = sanitize_text_field($_GET['visa_issue_from_date']);
        $visa_issue_from_date = substr($visa_issue_from_date,6,4).substr($visa_issue_from_date,3,2).substr($visa_issue_from_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'visa_issue_date',
          'value' => $visa_issue_from_date,
          'compare' => '>=',
          'type'    => 'NUMERIC'
        ) ) );
      } elseif (!empty($_GET['visa_issue_to_date']) ) {

        $visa_issue_to_date = sanitize_text_field($_GET['visa_issue_to_date']);
        $visa_issue_to_date = substr($visa_issue_to_date,6,4).substr($visa_issue_to_date,3,2).substr($visa_issue_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'visa_issue_date',
          'value' => $visa_issue_to_date,
          'compare' => '<=',
          'type'    => 'NUMERIC'
        ) ) );
      }
    }
}

 add_action('pre_get_posts','visa_issue_date_query_filter');

//// VISA ISSUE DATE END ////


//// VISA EXPIRY DATE START ///

function visa_expiry_date_input() {
    $scr = get_current_screen();
    //if ( $scr->base !== 'edit' && $scr->post_type !== 'cv') return;
    if ($scr->post_type !== 'cus_rec') return;
		// visa DATES START
    $visa_expiry_from_date = ( isset( $_GET['visa_expiry_from_date'] ) && $_GET['visa_expiry_from_date'] ) ? $_GET['visa_expiry_from_date'] : '';
    $visa_expiry_to_date = ( isset( $_GET['visa_expiry_to_date'] ) && $_GET['visa_expiry_to_date'] ) ? $_GET['visa_expiry_to_date'] : '';

		//$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';
    echo '<input type="text" class="datepicker" name="visa_expiry_from_date" placeholder="Visa expiry date from" value="' . esc_attr( $visa_expiry_from_date ) . '">';
    echo '<input type="text" class="datepicker" name="visa_expiry_to_date" placeholder="Visa expiry date to" value="' . esc_attr( $visa_expiry_to_date ) . '">';

    echo '<script>
		jQuery( function($) {

			    var from = $(\'input[name="visa_expiry_from_date"]\'),
			    to = $(\'input[name="visa_expiry_to_date"]\');

			    $( \'input[name="visa_expiry_from_date"], input[name="visa_expiry_to_date"]\' ).datepicker( {dateFormat : "dd/mm/yy"} );

    		  from.on( \'change\', function() {
				  to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
}

add_action('restrict_manage_posts', 'visa_expiry_date_input');

/**
 * Filter the results based on meta data.
 */
// https://pineco.de/filter-by-acf-meta-data-in-wordpress-admin/
function visa_expiry_date_query_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
      $scr = get_current_screen();
      if ( $scr->base !== 'edit' && $scr->post_type !== 'cus_rec' ) return;

      if ( !empty($_GET['visa_expiry_from_date']) && !empty($_GET['visa_expiry_to_date']) ) {
        $visa_expiry_from_date = sanitize_text_field($_GET['visa_expiry_from_date']);
        $visa_expiry_from_date = substr($visa_expiry_from_date,6,4).substr($visa_expiry_from_date,3,2).substr($visa_expiry_from_date,0,2);

        $visa_expiry_to_date = sanitize_text_field($_GET['visa_expiry_to_date']);
        $visa_expiry_to_date = substr($visa_expiry_to_date,6,4).substr($visa_expiry_to_date,3,2).substr($visa_expiry_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'visa_expiry_date',
          'value' => array($visa_expiry_from_date , $visa_expiry_to_date ),
          'compare' => 'BETWEEN',
          'type'    => 'NUMERIC'
        ) ) );
      }
      elseif ( !empty($_GET['visa_expiry_from_date']) ) {

        $visa_expiry_from_date = sanitize_text_field($_GET['visa_expiry_from_date']);
        $visa_expiry_from_date = substr($visa_expiry_from_date,6,4).substr($visa_expiry_from_date,3,2).substr($visa_expiry_from_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'visa_expiry_date',
          'value' => $visa_expiry_from_date,
          'compare' => '>=',
          'type'    => 'NUMERIC'
        ) ) );
      } elseif (!empty($_GET['visa_expiry_to_date']) ) {

        $visa_expiry_to_date = sanitize_text_field($_GET['visa_expiry_to_date']);
        $visa_expiry_to_date = substr($visa_expiry_to_date,6,4).substr($visa_expiry_to_date,3,2).substr($visa_expiry_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'visa_expiry_date',
          'value' => $visa_expiry_to_date,
          'compare' => '<=',
          'type'    => 'NUMERIC'
        ) ) );
      }
    }
}

add_action('pre_get_posts','visa_expiry_date_query_filter');

//// VISA EXPIRY DATE END ///


//// ESTABLISHMENT ISSUE DATE START ///

function establishment_card_issue_date_input() {
    $scr = get_current_screen();
    //if ( $scr->base !== 'edit' && $scr->post_type !== 'cv') return;
    if ($scr->post_type !== 'cus_rec') return;
		// visa DATES START
    $establishment_card_issue_from_date = ( isset( $_GET['establishment_card_issue_from_date'] ) && $_GET['establishment_card_issue_from_date'] ) ? $_GET['establishment_card_issue_from_date'] : '';
    $establishment_card_issue_to_date = ( isset( $_GET['establishment_card_issue_to_date'] ) && $_GET['establishment_card_issue_to_date'] ) ? $_GET['establishment_card_issue_to_date'] : '';

		//$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';
    echo '<input size="28" type="text" class="datepicker" name="establishment_card_issue_from_date" placeholder="Establishment card issue date from" value="' . esc_attr( $establishment_card_issue_from_date ) . '">';
    echo '<input size="28" type="text" class="datepicker" name="establishment_card_issue_to_date" placeholder="Establishment card issue date to" value="' . esc_attr( $establishment_card_issue_to_date ) . '">';

    echo '<script>
		jQuery( function($) {

			    var from = $(\'input[name="establishment_card_issue_from_date"]\'),
			    to = $(\'input[name="establishment_card_issue_to_date"]\');

			    $( \'input[name="establishment_card_issue_from_date"], input[name="establishment_card_issue_to_date"]\' ).datepicker( {dateFormat : "dd/mm/yy"} );

    		  from.on( \'change\', function() {
				  to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
}

add_action('restrict_manage_posts', 'establishment_card_issue_date_input');

/**
 * Filter the results based on meta data.
 */
// https://pineco.de/filter-by-acf-meta-data-in-wordpress-admin/
function establishment_card_issue_date_query_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
      $scr = get_current_screen();
      if ( $scr->base !== 'edit' && $scr->post_type !== 'cus_rec' ) return;

      if ( !empty($_GET['establishment_card_issue_from_date']) && !empty($_GET['establishment_card_issue_to_date']) ) {
        $establishment_card_issue_from_date = sanitize_text_field($_GET['establishment_card_issue_from_date']);
        $establishment_card_issue_from_date = substr($establishment_card_issue_from_date,6,4).substr($establishment_card_issue_from_date,3,2).substr($establishment_card_issue_from_date,0,2);

        $establishment_card_issue_to_date = sanitize_text_field($_GET['establishment_card_issue_to_date']);
        $establishment_card_issue_to_date = substr($establishment_card_issue_to_date,6,4).substr($establishment_card_issue_to_date,3,2).substr($establishment_card_issue_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'establishment_card_issue_date',
          'value' => array($establishment_card_issue_from_date , $establishment_card_issue_to_date ),
          'compare' => 'BETWEEN',
          'type'    => 'NUMERIC'
        ) ) );
      }
      elseif ( !empty($_GET['establishment_card_issue_from_date']) ) {

        $establishment_card_issue_from_date = sanitize_text_field($_GET['establishment_card_issue_from_date']);
        $establishment_card_issue_from_date = substr($establishment_card_issue_from_date,6,4).substr($establishment_card_issue_from_date,3,2).substr($establishment_card_issue_from_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'establishment_card_issue_date',
          'value' => $establishment_card_issue_from_date,
          'compare' => '>=',
          'type'    => 'NUMERIC'
        ) ) );
      } elseif (!empty($_GET['establishment_card_issue_to_date']) ) {

        $establishment_card_issue_to_date = sanitize_text_field($_GET['establishment_card_issue_to_date']);
        $establishment_card_issue_to_date = substr($establishment_card_issue_to_date,6,4).substr($establishment_card_issue_to_date,3,2).substr($establishment_card_issue_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'establishment_card_issue_date',
          'value' => $establishment_card_issue_to_date,
          'compare' => '<=',
          'type'    => 'NUMERIC'
        ) ) );
      }
    }
}

add_action('pre_get_posts','establishment_card_issue_date_query_filter');

//// ESTABLISHMENT ISSUE DATE END ///


//// ESTABLISHMENT CARD EXPIRY DATE START ///

function establishment_card_expiry_date_input() {
    $scr = get_current_screen();
    //if ( $scr->base !== 'edit' && $scr->post_type !== 'cv') return;
    if ($scr->post_type !== 'cus_rec') return;
		// visa DATES START
    $establishment_card_expiry_from_date = ( isset( $_GET['establishment_card_expiry_from_date'] ) && $_GET['establishment_card_expiry_from_date'] ) ? $_GET['establishment_card_expiry_from_date'] : '';
    $establishment_card_expiry_to_date = ( isset( $_GET['establishment_card_expiry_to_date'] ) && $_GET['establishment_card_expiry_to_date'] ) ? $_GET['establishment_card_expiry_to_date'] : '';

		//$to = ( isset( $_GET['mishaDateTo'] ) && $_GET['mishaDateTo'] ) ? $_GET['mishaDateTo'] : '';
    echo '<input size="28" type="text" class="datepicker" name="establishment_card_expiry_from_date" placeholder="Establishment card expiry date from" value="' . esc_attr( $establishment_card_expiry_from_date ) . '">';
    echo '<input size="28" type="text" class="datepicker" name="establishment_card_expiry_to_date" placeholder="Establishment card expiry date to" value="' . esc_attr( $establishment_card_expiry_to_date ) . '">';

    echo '<script>
		jQuery( function($) {

			    var from = $(\'input[name="establishment_card_expiry_from_date"]\'),
			    to = $(\'input[name="establishment_card_expiry_to_date"]\');

			    $( \'input[name="establishment_card_expiry_from_date"], input[name="establishment_card_expiry_to_date"]\' ).datepicker( {dateFormat : "dd/mm/yy"} );

    		  from.on( \'change\', function() {
				  to.datepicker( \'option\', \'minDate\', from.val() );
			});

			to.on( \'change\', function() {
				from.datepicker( \'option\', \'maxDate\', to.val() );
			});

		});
		</script>';
}

add_action('restrict_manage_posts', 'establishment_card_expiry_date_input');

/**
 * Filter the results based on meta data.
 */
// https://pineco.de/filter-by-acf-meta-data-in-wordpress-admin/
function establishment_card_expiry_date_query_filter($query) {
    if ( is_admin() && $query->is_main_query() ) {
      $scr = get_current_screen();
      if ( $scr->base !== 'edit' && $scr->post_type !== 'cus_rec' ) return;

      if ( !empty($_GET['establishment_card_expiry_from_date']) && !empty($_GET['establishment_card_expiry_to_date']) ) {
        $establishment_card_expiry_from_date = sanitize_text_field($_GET['establishment_card_expiry_from_date']);
        $establishment_card_expiry_from_date = substr($establishment_card_expiry_from_date,6,4).substr($establishment_card_expiry_from_date,3,2).substr($establishment_card_expiry_from_date,0,2);

        $establishment_card_expiry_to_date = sanitize_text_field($_GET['establishment_card_expiry_to_date']);
        $establishment_card_expiry_to_date = substr($establishment_card_expiry_to_date,6,4).substr($establishment_card_expiry_to_date,3,2).substr($establishment_card_expiry_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'establishment_card_expiry_date',
          'value' => array($establishment_card_expiry_from_date , $establishment_card_expiry_to_date ),
          'compare' => 'BETWEEN',
          'type'    => 'NUMERIC'
        ) ) );
      }
      elseif ( !empty($_GET['establishment_card_expiry_from_date']) ) {

        $establishment_card_expiry_from_date = sanitize_text_field($_GET['establishment_card_expiry_from_date']);
        $establishment_card_expiry_from_date = substr($establishment_card_expiry_from_date,6,4).substr($establishment_card_expiry_from_date,3,2).substr($establishment_card_expiry_from_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'establishment_card_expiry_date',
          'value' => $establishment_card_expiry_from_date,
          'compare' => '>=',
          'type'    => 'NUMERIC'
        ) ) );
      } elseif (!empty($_GET['establishment_card_expiry_to_date']) ) {

        $establishment_card_expiry_to_date = sanitize_text_field($_GET['establishment_card_expiry_to_date']);
        $establishment_card_expiry_to_date = substr($establishment_card_expiry_to_date,6,4).substr($establishment_card_expiry_to_date,3,2).substr($establishment_card_expiry_to_date,0,2);

        $query->set('meta_query', array( array(
          'key' => 'establishment_card_expiry_date',
          'value' => $establishment_card_expiry_to_date,
          'compare' => '<=',
          'type'    => 'NUMERIC'
        ) ) );
      }
    }
}

add_action('pre_get_posts','establishment_card_expiry_date_query_filter');

//// ESTABLISHMENT EXPIRY DATE END ///



// https://css-tricks.com/snippets/wordpress/apply-custom-css-to-admin-area/
// Apply Custom CSS to Admin Area
// add_action('admin_head', 'wpadmin_css1');

function wpadmin_css1() {

}




// Make columns sortable
// https://code.tutsplus.com/articles/quick-tip-make-your-custom-column-sortable--wp-25095
//add_filter( 'manage_edit-cus_rec_sortable_columns', 'my_sortable_cus_rec_column' );
function my_sortable_cus_rec_column( $columns ) {
//	$columns['deptt'] = 'deptt';
//  $columns['vacancy'] = 'vacancy';
//  $columns['personal_info'] = 'personal_info';
//  $columns['address_info'] = 'address_info';
//  $columns['father_info'] = 'father_info';
  //$columns['cvstatus'] = 'cvstatus';

	//To make a column 'un-sortable' remove it from the array
	//unset($columns['date']);
	return $columns;
}

// Make Columns of a Custom Post Type


function cus_rec_edit_columns($columns){
    $columns = array(
        'cb' => '<input type="checkbox"/>',
        'title' => __('<strong>Full Name</strong><hr>'),
        'contact_info' => __('<strong>Email<br />Address<br/>Cell number<br/>Company</strong>'),
        'license_info' => __('<strong>Share Holders<br />License package<br />License type<br />License document<br />Issue date<br />Expiry date</strong>'),
        'visa_info' => __('<strong>No of Visa<br />Visa document</strong>'),
        'establishment_info' => __('<strong>Establishment card<br />Issue date<br />Expiry date</strong>'),
        'supporting_documents' => __('<strong>Supporting document 1<br />Supporting document 2</br>Supporting document 3<br />Supporting document 4</strong>'),
    );
    return $columns;
}
add_filter('manage_cus_rec_posts_columns', 'cus_rec_edit_columns');

function cus_rec_custom_columns_data($column){
    global $post;
    switch ($column) {
      case 'supporting_documents':
        //the_field ("supporting_document_1");
        $file = get_field('supporting_document_1');
        if( $file ){
            $url = wp_get_attachment_url( $file ); ?>
            <a target="_new" href="<?php echo esc_html($url); ?>" >Supporting document 1</a><?php
          }else{
            echo "--";
          }
        echo "<br />";
        //the_field ("supporting_document_2");
        $file = get_field('supporting_document_2');
        if( $file ){
            $url = wp_get_attachment_url( $file ); ?>
            <a target="_new" href="<?php echo esc_html($url); ?>" >Supporting document 2</a><?php
          }else{
            echo "--";
          }
					echo "<br />";
	        //the_field ("supporting_document_2");
	        $file = get_field('supporting_document_3');
	        if( $file ){
	            $url = wp_get_attachment_url( $file ); ?>
	            <a target="_new" href="<?php echo esc_html($url); ?>" >Supporting document 3</a><?php
	          }else{
	            echo "--";
	          }
						echo "<br />";
						//the_field ("supporting_document_2");
						$file = get_field('supporting_document_4');
						if( $file ){
								$url = wp_get_attachment_url( $file ); ?>
								<a target="_new" href="<?php echo esc_html($url); ?>" >Supporting document 4</a><?php
							}else{
								echo "--";
							}

      break;
      case 'establishment_info':
        // the_field ("establishment_card");
        $file = get_field('establishment_card');
        if( $file ){
            $url = wp_get_attachment_url( $file ); ?>
            <a target="_new" href="<?php echo esc_html($url); ?>" >Establishment card</a><?php
          }else{
            echo "--";
          }
        echo "<br />";
        the_field ("establishment_card_issue_date");
        echo "<br />";
        the_field ("establishment_card_expiry_date");
      break;
      case 'visa_info':
        the_field ("no_of_visa");
        echo "<br />";
        //the_field ("visa_type");
        //the_field ("visa_document");
        $file = get_field('visa_document');
        if( $file ){
            $url = wp_get_attachment_url( $file ); ?>
            <a target="_new" href="<?php echo esc_html($url); ?>" >Visa document</a><?php
          }else{
            echo "--";
          }
      break;

      case 'license_info':
				the_field ("share_holders");
				echo "<br />";
        the_field ("license_package");
        echo "<br />";
        //the_field ("license_type");
        echo $term_name = get_term( get_field("license_type") )->name;
        echo "<br />";
        //the_field ("license_document");
        $file = get_field('license_document');
        if( $file ){
            $url = wp_get_attachment_url( $file ); ?>
            <a target="_new" href="<?php echo esc_html($url); ?>" >License document</a><?php
          }else{
            echo "--";
          }
        echo "<br />";
        the_field ("license_issue_date");
        echo "<br />";
        the_field ("license_expiry_date");
      break;

      case 'contact_info':
				echo "<a href='mailto:".get_field ("email")."'>".get_field ("email")."</a>";
        echo "<br />";
        the_field ("address");
        echo "<br />";
        the_field ("cell_number");
        echo "<br />";
        the_field ("company");
      break;
    }
}
add_action('manage_posts_custom_column',  'cus_rec_custom_columns_data');





// https://wordpress.stackexchange.com/questions/11758/extending-the-search-context-in-the-admin-list-post-screen
add_filter( 'posts_join', 'cus_rec_search_join' );
function cus_rec_search_join ( $join ) {
    global $pagenow, $wpdb;

    // I want the filter only when performing a search on edit page of Custom Post Type named "segnalazioni".
    if ( is_admin() && 'edit.php' === $pagenow && 'cus_rec' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {
        $join .= 'LEFT JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
    }
    return $join;
}

add_filter( 'posts_where', 'cus_rec_search_where' );
function cus_rec_search_where( $where ) {
    global $pagenow, $wpdb;

    // I want the filter only when performing a search on edit page of Custom Post Type named "segnalazioni".
    if ( is_admin() && 'edit.php' === $pagenow && 'cus_rec' === $_GET['post_type'] && ! empty( $_GET['s'] ) ) {
        $where = preg_replace(
            "/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(" . $wpdb->posts . ".post_title LIKE $1) OR (" . $wpdb->postmeta . ".meta_value LIKE $1)", $where );
        $where.= " GROUP BY {$wpdb->posts}.id"; // Solves duplicated results
    }
    return $where;
}
function cus_rec_search_distinct( $where ){
    global $pagenow, $wpdb;

    if ( is_admin() && $pagenow=='edit.php' && $_GET['post_type']=='cus_rec' && $_GET['s'] != '') {
    return "DISTINCT";
    }
    return $where;
}
add_filter( 'posts_distinct', 'cus_rec_search_distinct' );

// https://pressidium.com/blog/wordpress-admin-search-extending-the-results/
// add_action( 'pre_get_posts', 'extend_admin_search' );
// function extend_admin_search( $query ) {
// 	$post_type = 'cus_rec';
// 	$custom_fields = array("address","email","license_type");
//     if( ! is_admin() )
//     	return;
//   	if ( $query->query['post_type'] != $post_type )
//   		return;
//     $search_term = $query->query_vars['s'];
//
//     $query->query_vars['s'] = '';
//
//     if ( $search_term != '' ) {
//         $meta_query = array( 'relation' => 'OR' );
//         foreach( $custom_fields as $custom_field ) {
//             array_push( $meta_query, array(
//                 'key' => $custom_field,
//                 'value' => $search_term,
//                 'compare' => 'LIKE'
//             ));
//         }
//         $query->set( 'meta_query', $meta_query );
//     };
// }


/*Remove WordPress menu from admin bar*/
add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
function remove_wp_logo( $wp_admin_bar ) {
	$wp_admin_bar->remove_node( 'wp-logo' );
}

if( function_exists('acf_add_options_page') ) {

	acf_add_options_page(array(
		'page_title' 	=> '60 Days alert options',
		'menu_title'	=> '60 Days alert options',
		'menu_slug' 	=> '60d-alert-options',
		'capability'	=> 'edit_posts',
		'redirect'		=> true
	));

  acf_add_options_page(array(
		'page_title' 	=> '30 Days alert options',
		'menu_title'	=> '30 Days alert options',
		'menu_slug' 	=> '30d-alert-options',
		'capability'	=> 'edit_posts',
		'redirect'		=> true
	));

  acf_add_options_page(array(
		'page_title' 	=> '15 Days alert options',
		'menu_title'	=> '15 Days alert options',
		'menu_slug' 	=> '15d-alert-options',
		'capability'	=> 'edit_posts',
		'redirect'		=> true
	));

  acf_add_options_page(array(
		'page_title' 	=> '7 Days alert options',
		'menu_title'	=> '7 Days alert options',
		'menu_slug' 	=> '7d-alert-options',
		'capability'	=> 'edit_posts',
		'redirect'		=> true
	));

	acf_add_options_page(array(
		'page_title' 	=> 'Cron job log',
		'menu_title'	=> 'Cron job log',
		'menu_slug' 	=> 'cron-job-log',
		'capability'	=> 'edit_posts',
		'redirect'		=> true
	));
}

?>
