<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

function fbbttm_cpt_tm_fooditems(){

	
	$fooditems_type_title          =  'Fooditems' ;
	$fooditems_type_title_singular =  'Fooditems' ;
	$fooditems_type_slug           =  'fooditems' ;
	$fooditems_cat_title           =   'Fooditems Categories' ;
	$fooditems_cat_title_singular =  'Fooditems Category' ;
	$fooditems_cat_slug           =  'fooditems-category' ;
	
	
	/*
	 *  Custom Post Type
	 */
	$labels = array(
		'name'               => esc_attr_x( 'fooditems', 'post type general name', 'foodboxbooker' ),
		'singular_name'      => esc_attr_x( 'fooditems', 'post type singular name', 'foodboxbooker' ),
		'menu_name'          => esc_attr_x( 'Fooditems', 'admin menu', 'foodboxbooker' ),
		'name_admin_bar'     => esc_attr_x( 'fooditems', 'add new on admin bar', 'foodboxbooker' ),
		'add_new'            => esc_attr_x( 'Add New', 'fooditems', 'foodboxbooker' ),
		'add_new_item'       => esc_attr__( 'Add New fooditems', 'foodboxbooker' ),
		'new_item'           => esc_attr__( 'New fooditems', 'foodboxbooker' ),
		'edit_item'          => esc_attr__( 'Edit fooditems', 'foodboxbooker' ),
		'view_item'          => esc_attr__( 'View fooditems', 'foodboxbooker' ),
		'all_items'          => esc_attr__( 'All fooditems', 'foodboxbooker' ),
		'search_items'       => esc_attr__( 'Search fooditems', 'foodboxbooker' ),
		'parent_item_colon'  => esc_attr__( 'Parent fooditems:', 'foodboxbooker' ),
		'not_found'          => esc_attr__( 'No fooditems found.', 'foodboxbooker' ),
		'not_found_in_trash' => esc_attr__( 'No fooditems found in Trash.', 'foodboxbooker' )
	);
	
	
	
	
	
	
	$args = array(
		'labels'             => $labels,
		'menu_icon'          => 'dashicons-screenoptions',
		'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		//'show_in_menu'       => true,
		'query_var'          => true,
		'rewrite'            => array( 'with_front' => false, 'slug' => $fooditems_type_slug ),
		'capability_type'    => 'post',
		'has_archive'        => false,
		'hierarchical'       => false,
		'menu_position'      => null,
		'show_in_menu' => false, 
		'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields')
	);

	register_post_type( 'fbbttm_fooditems', $args );
	
	


	
	//Registaring Taxonomy for Post Type Fooditems
	
	$labels = 	array(
		'name'              => esc_attr__('Fooditems Category', 'foodboxbooker'),
		'singular_name'     => esc_attr__('Fooditems Category', 'foodboxbooker'),
		'search_items'      => esc_attr__('Search fooditems Category', 'foodboxbooker'),
		'all_items'         => esc_attr__('All fooditems Category', 'foodboxbooker'), 
		'parent_item'       => esc_attr__('Parent fooditems Category', 'foodboxbooker'),
		'parent_item_colon' => esc_attr__('Parent fooditems Category:', 'foodboxbooker'), 
		'edit_item'         => esc_attr__('Edit fooditems Category', 'foodboxbooker'),
		'update_item'       => esc_attr__('Update fooditems Category', 'foodboxbooker'),
		'add_new_item'      => esc_attr__('Add New fooditems Category', 'foodboxbooker'),
		'new_item_name'     => esc_attr__('New fooditems Category Name', 'foodboxbooker'),
		'menu_name'         => esc_attr__('Fooditems Category', 'foodboxbooker'),
	);
	
	

	
	
	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_admin_column' => true,
		'query_var'         => true,
		'rewrite'           => array( 'slug' => $fooditems_cat_slug ),

	);
	
	register_taxonomy( 'fbbttm_fooditems_category', 'fbbttm_fooditems', $args  );
	
	
}

add_action( 'init', 'fbbttm_cpt_tm_fooditems', 8 );




// Show Featured image in the admin section
add_filter( 'manage_fbbttm_fooditems_posts_columns', 'fbbttm_foodboxbooker_fooditems_set_featured_image_column' );
add_action( 'manage_fbbttm_fooditems_posts_custom_column' , 'fbbttm_foodboxbooker_fooditems_set_featured_image_column_content', 10, 2 );


function fbbttm_foodboxbooker_fooditems_set_featured_image_column($columns) {
	$new_columns = array();
	foreach( $columns as $key=>$val ){
		$new_columns[$key] = $val;
		if( $key=='title' ){
			$new_columns['fbbttm_foodboxbooker_featured_image'] = esc_attr__( 'Featured Image', 'foodboxbooker' );
		}
	}
	return $new_columns;
}



function fbbttm_foodboxbooker_fooditems_set_featured_image_column_content( $column, $post_id ) {
	if( $column == 'fbbttm_foodboxbooker_featured_image' ){
		echo '<a href="'. esc_url(get_permalink($post_id)) .'">';
		if ( has_post_thumbnail($post_id) ) {
			the_post_thumbnail('thumbnail');
		} else {
			echo '<img src="' . esc_url(FBBTTM_URL) . '/assets/images/admin-no-image.png" />';
		}
		echo '</a>';
	}
	
}




 $fbbttm_cpt_public_slug = FBBTTM_CPT;

add_action( 'add_meta_boxes_' . $fbbttm_cpt_public_slug, 'fbbttm_adding_custom_meta_boxes' );
add_action( 'save_post', 'fbbttm_save_metabox' , 10, 2 );
add_action( 'publish_'.$fbbttm_cpt_public_slug, 'fbbttm_save_metabox' , 10, 2 );

function fbbttm_adding_custom_meta_boxes(){

	global $fbbttm_cpt_public_slug;
    add_meta_box('itemdetails', 'Item Details', 'fbbttm_cpt_form_site_Render', $fbbttm_cpt_public_slug, 'normal', 'high');
}

function fbbttm_cpt_form_site_Render(){

    global $post;
    $post_meta = get_post_meta($post->ID); 
    // put here input or textarea 
    $price =  (!empty($post_meta['fbbttmprice'][0]))? $post_meta['fbbttmprice'][0] : '';
    $fbbttmdetails =  (!empty($post_meta['fbbttmdetails'][0]))? $post_meta['fbbttmdetails'][0] : '';

    ?>
    <div class="">
		<label><?php echo esc_html__('Add Price', 'foodboxbooker');?></label>
		<input type="number" name="fbbttmprice" value="<?php echo esc_html($price); ?>" required/>
	</div>

    <?php 
	}

function fbbttm_save_metabox($post_id, $post){
    global $post;
// when first publish and then save
    // phpcs:disable WordPress.Security.NonceVerification.Missing
    if( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) {

        // you will need to use a $_POST param and validate before saving
        $meta_val = isset( $_POST['fbbttmprice'] ) ? sanitize_text_field( wp_unslash($_POST['fbbttmprice'] )) : '';
    
        // the $meta_val would be a $_POST param from inner meta box form
        update_post_meta($post_id, 'fbbttmprice', $meta_val);
       // update_post_meta($post_id, 'fbbttmdetails', $meta_val_textarea);
    }

}





   function fbbttm_migrate_old_post_type_to_new() {
    global $wpdb;

    $old_post_type = 'tm_fooditems';
    $new_post_type = 'fbbttm_fooditems';

    // Get all post IDs of the old post type
    //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
    $post_ids = $wpdb->get_col(
        $wpdb->prepare(
            "SELECT ID FROM $wpdb->posts WHERE post_type = %s",
            $old_post_type
        )
    );

    // Update the post type in the database
    //phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
	//phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
    $wpdb->update(
        $wpdb->posts,
        array( 'post_type' => $new_post_type ), // New post type
        array( 'post_type' => $old_post_type )  // Old post type
    );

    // Loop through each post ID and clear its cache
    foreach ( $post_ids as $post_id ) {
        clean_post_cache( $post_id );
    }
}



add_action( 'admin_init', 'fbbttm_migrate_old_post_type_to_new' );



function fbbttm_migrate_terms_to_new_taxonomy() {
    global $wpdb;

    $old_taxonomy = 'tm_fooditems_category';
    $new_taxonomy = 'fbbttm_fooditems_category';

    // Update the term taxonomy in the database
    $wpdb->update(
        $wpdb->term_taxonomy,
        array( 'taxonomy' => $new_taxonomy ), // New taxonomy
        array( 'taxonomy' => $old_taxonomy )  // Old taxonomy
    );

    // Clear the cache to ensure changes are reflected
    clean_term_cache(0, $old_taxonomy);
    clean_term_cache(0, $new_taxonomy);
}
add_action( 'admin_init', 'fbbttm_migrate_terms_to_new_taxonomy' );
?>