File: /mnt/data/dreamsrent-wp/wp-content/plugins/dreamsrent-booking/inc/register_custom_post.php
<?php
function dreams_register_booking_post_type() {
$labels = array(
'name' => _x( 'Car Rental', 'post type general name', 'bookingcore' ),
'singular_name' => _x( 'Car Rental', 'post type singular name', 'bookingcore' ),
'menu_name' => _x( 'Car Rental', 'admin menu', 'bookingcore' ),
'name_admin_bar' => _x( 'Car Rental', 'add new on admin bar', 'bookingcore' ),
'add_new' => _x( 'Add New', 'Car Rental', 'bookingcore' ),
'add_new_item' => __( 'Add New Car Rental', 'bookingcore' ),
'new_item' => __( 'New Car Rental', 'bookingcore' ),
'edit_item' => __( 'Edit Car Rental', 'bookingcore' ),
'view_item' => __( 'View Car Rental', 'bookingcore' ),
'all_items' => __( 'All Car Rental', 'bookingcore' ),
'search_items' => __( 'Search Car Rental', 'bookingcore' ),
'parent_item_colon' => __( 'Parent Car Rental:', 'bookingcore' ),
'not_found' => __( 'No Car Rental found.', 'bookingcore' ),
'not_found_in_trash' => __( 'No Car Rental found in Trash.', 'bookingcore' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'bookingcore' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'rental' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'menu_icon' => 'dashicons-car',
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'rental', $args );
}
add_action( 'init', 'dreams_register_booking_post_type' );
add_action('admin_menu', 'custom_redirect_rental_add_new_menu', 999);
function custom_redirect_rental_add_new_menu() {
// Remove default "Add New"
remove_submenu_page('edit.php?post_type=rental', 'post-new.php?post_type=rental');
// Add custom "Add New" submenu
add_submenu_page(
'edit.php?post_type=rental',
'Add New Car Rental',
'Add New Car',
'edit_posts',
'redirect_rental_add_new',
'redirect_rental_add_new_callback' // callback can redirect
);
}
// Redirect to a custom page when "Add New" is clicked
function redirect_rental_add_new_callback() {
$av_all_cars = dreamsrent_fl_framework_getoptions('av_all_cars');
if (!empty($av_all_cars)) {
$redirect_url = get_permalink($av_all_cars); // Get the actual page URL
wp_redirect($redirect_url);
} else {
wp_redirect(home_url('/cars')); // change to your desired URL
}
exit;
}
// Reorder submenu items for 'rental'
add_action('admin_menu', 'custom_reorder_rental_submenu_order', 1000);
function custom_reorder_rental_submenu_order() {
global $submenu;
if (!isset($submenu['edit.php?post_type=rental'])) {
return;
}
$menu = $submenu['edit.php?post_type=rental'];
$add_new = [];
$others = [];
foreach ($menu as $item) {
if ($item[2] === 'redirect_rental_add_new') {
$add_new[] = $item; // Save our custom "Add New"
} else {
$others[] = $item;
}
}
// Rebuild submenu: put custom "Add New" right after the first item (All Items)
$submenu['edit.php?post_type=rental'] = array_merge(
[ $others[0] ], // "All Car Rental"
$add_new, // Custom "Add New"
array_slice($others, 1) // Remaining items
);
}
// Hook into the page load to perform redirect
add_action('load-toplevel_page_redirect_rental_add_new', 'custom_redirect_add_new_to_frontend');
add_action('load-rental_page_redirect_rental_add_new', 'custom_redirect_add_new_to_frontend'); // fallback
function custom_redirect_add_new_to_frontend() {
$av_all_cars = dreamsrent_fl_framework_getoptions('av_all_cars');
if (!empty($av_all_cars)) {
$redirect_url = get_permalink($av_all_cars); // Get the actual page URL
wp_redirect($redirect_url);
} else {
wp_redirect(home_url('/cars')); // change to your desired URL
}
exit;
}
add_action('admin_head', 'remove_add_new_button_for_rental');
function remove_add_new_button_for_rental() {
global $pagenow;
// Only target the rental post type list screen
if ($pagenow === 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'rental' ) {
echo '<style>
.page-title-action { display: none!important; }
</style>';
}
}
function dreams_register_rental_product_type() {
class WC_Product_Rental extends WC_Product {
// Define the property explicitly
protected $product_type;
public function __construct( $product ) {
$this->product_type = 'carrental'; // Set the product type
parent::__construct( $product );
}
// Getter for the product type
public function get_product_type() {
return $this->product_type;
}
}
// Map the product type to the custom class
add_filter( 'woocommerce_product_class', function( $classname, $product_type, $post_type, $product_id ) {
if ( 'carrental' === $product_type ) {
$classname = 'WC_Product_Rental';
}
return $classname;
}, 10, 4 );
// Add the custom product type to the selector
add_filter( 'product_type_selector', function( $types ) {
$types['carrental'] = __( 'Dreams Rental New', 'bookingcore' );
return $types;
});
}
add_action( 'woocommerce_init', 'dreams_register_rental_product_type' );
/**
* Register DreamsRent Package WooCommerce product type
*/
function dreams_register_package_product_type() {
// Define product class
if ( ! class_exists( 'WC_Product_Dreamsrent_Package' ) ) {
class WC_Product_Dreamsrent_Package extends WC_Product {
protected $product_type;
public function __construct( $product ) {
$this->product_type = 'dreamsrent_package';
parent::__construct( $product );
}
public function get_product_type() {
return $this->product_type;
}
}
}
// Map product type to class
add_filter( 'woocommerce_product_class', function( $classname, $product_type, $post_type, $product_id ) {
if ( 'dreamsrent_package' === $product_type ) {
$classname = 'WC_Product_Dreamsrent_Package';
}
return $classname;
}, 10, 4 );
// Add to product type selector
add_filter( 'product_type_selector', function( $types ) {
$types['dreamsrent_package'] = __( 'DreamsRent Package', 'bookingcore' );
return $types;
} );
// Add custom tab
add_filter( 'woocommerce_product_data_tabs', function( $tabs ) {
global $post;
$tabs['dr_pkg'] = array(
'label' => __( 'Package', 'bookingcore' ),
'target' => 'dr_pkg_options',
'class' => array( 'show_if_dreamsrent_package' ),
'priority' => 60,
);
// Ensure General tab (pricing) is visible for package products
if ( isset($tabs['general']) ) {
if (!isset($tabs['general']['class']) || !is_array($tabs['general']['class'])) {
$tabs['general']['class'] = array();
}
$tabs['general']['class'][] = 'show_if_dreamsrent_package';
}
return $tabs;
} );
// Add fields panel
add_action( 'woocommerce_product_data_panels', function() {
?>
<div id="dr_pkg_options" class="panel woocommerce_options_panel hidden">
<div class="options_group">
<?php
// Car limit
woocommerce_wp_text_input( array(
'id' => '_dr_pkg_car_limit',
'label' => __( 'Max cars vendor can add', 'bookingcore' ),
'type' => 'number',
'desc_tip' => true,
'description' => __( 'Total number of cars this plan allows a vendor to add.', 'bookingcore' ),
'custom_attributes' => array( 'min' => '0', 'step' => '1' ),
) );
// Duration days
woocommerce_wp_text_input( array(
'id' => '_dr_pkg_duration_days',
'label' => __( 'Duration (days)', 'bookingcore' ),
'type' => 'number',
'desc_tip' => true,
'description' => __( 'How long the package is valid after purchase. Leave empty for no expiry.', 'bookingcore' ),
'custom_attributes' => array( 'min' => '0', 'step' => '1' ),
) );
?>
</div>
</div>
<?php
} );
// Save fields
add_action( 'woocommerce_process_product_meta', function( $post_id ) {
$product = wc_get_product( $post_id );
if ( ! $product || $product->get_type() !== 'dreamsrent_package' ) {
return;
}
$car_limit = isset( $_POST['_dr_pkg_car_limit'] ) ? absint( $_POST['_dr_pkg_car_limit'] ) : '';
$duration = isset( $_POST['_dr_pkg_duration_days'] ) ? absint( $_POST['_dr_pkg_duration_days'] ) : '';
update_post_meta( $post_id, '_dr_pkg_car_limit', $car_limit );
update_post_meta( $post_id, '_dr_pkg_duration_days', $duration );
} );
// Admin JS: also show pricing fields for package type
add_action('admin_print_footer_scripts', function(){
$screen = get_current_screen();
if (!$screen || $screen->id !== 'product') return;
?>
<script>
jQuery(function($){
function dr_pkg_toggle_pricing(){
var type = $("#product-type").val();
if(type === 'dreamsrent_package'){
$('.show_if_simple').addClass('show_if_dreamsrent_package');
$('.pricing, .general_tab').show();
}
}
dr_pkg_toggle_pricing();
$(document.body).on('woocommerce-product-type-change', dr_pkg_toggle_pricing);
});
</script>
<?php
});
}
add_action( 'woocommerce_init', 'dreams_register_package_product_type' );
function dreams_save_booking_post_with_product_vendor( $post_id ) {
if ( ! wp_is_post_revision( $post_id ) && ! wp_is_post_autosave( $post_id ) && get_post_type( $post_id ) == 'rental' ) {
$post = get_post( $post_id );
$product_title = get_the_title( $post_id );
$product_content = get_post_field('post_content', $post_id);
$existing_product_id = get_post_meta( $post_id, '_booking_product_id', true );
// Check if the existing product ID is not empty
if ( ! empty( $existing_product_id ) ) {
// Check if the product exists
$existing_product = get_post( $existing_product_id );
// If the product exists, update it
if ( $existing_product ) {
$product_data = array(
'ID' => $existing_product_id,
'post_title' => $product_title,
'post_content' => $product_content,
);
wp_update_post( $product_data );
} else {
// If the product does not exist, create a new product
$product_data = array(
'post_title' => $product_title,
'post_content' => $product_content,
'post_status' => 'publish',
'post_type' => 'product',
);
$product_id = wp_insert_post( $product_data );
if ( ! is_wp_error( $product_id ) && $product_id > 0 ) {
wp_set_post_terms( $product_id, 'carrental', 'product_type', false );
update_post_meta( $post_id, '_booking_product_id', $product_id );
}
}
} else {
// If there is no existing product ID, create a new product
$product_data = array(
'post_title' => $product_title,
'post_content' => $product_content,
'post_status' => 'publish',
'post_type' => 'product',
);
$product_id = wp_insert_post( $product_data );
if ( ! is_wp_error( $product_id ) && $product_id > 0 ) {
wp_set_post_terms( $product_id, 'carrental', 'product_type', false );
update_post_meta( $post_id, '_booking_product_id', $product_id );
}
}
}
}
function dreams_save_booking_post_with_product( $post_id ) {
if ( ! isset( $_POST['dreams_save_post_booking_nonce'] ) || ! wp_verify_nonce( $_POST['dreams_save_post_booking_nonce'], 'dreams_save_post_booking' ) ) {
return;
}
if ( ! wp_is_post_revision( $post_id ) && ! wp_is_post_autosave( $post_id ) && get_post_type( $post_id ) == 'rental' ) {
$post = get_post( $post_id );
$product_title = get_the_title( $post_id );
$product_content = get_post_field('post_content', $post_id);
$existing_product_id = get_post_meta( $post_id, '_booking_product_id', true );
// Check if the existing product ID is not empty
if ( ! empty( $existing_product_id ) ) {
// Check if the product exists
$existing_product = get_post( $existing_product_id );
// If the product exists, update it
if ( $existing_product ) {
$product_data = array(
'ID' => $existing_product_id,
'post_title' => $product_title,
'post_content' => $product_content,
);
wp_update_post( $product_data );
} else {
// If the product does not exist, create a new product
$product_data = array(
'post_title' => $product_title,
'post_content' => $product_content,
'post_status' => 'publish',
'post_type' => 'product',
);
$product_id = wp_insert_post( $product_data );
if ( ! is_wp_error( $product_id ) && $product_id > 0 ) {
wp_set_post_terms( $product_id, 'carrental', 'product_type', false );
update_post_meta( $post_id, '_booking_product_id', $product_id );
}
}
} else {
// If there is no existing product ID, create a new product
$product_data = array(
'post_title' => $product_title,
'post_content' => $product_content,
'post_status' => 'publish',
'post_type' => 'product',
);
$product_id = wp_insert_post( $product_data );
if ( ! is_wp_error( $product_id ) && $product_id > 0 ) {
wp_set_post_terms( $product_id, 'carrental', 'product_type', false );
update_post_meta( $post_id, '_booking_product_id', $product_id );
}
}
}
}
add_action( 'save_post', 'dreams_save_booking_post_with_product', 10, 2);
add_action('transition_post_status', function($new_status, $old_status, $post){
if (empty($post) || $post->post_type !== 'rental') return;
if ($new_status === 'publish' && $old_status !== 'publish') {
$author_id = intval($post->post_author);
if (!$author_id) return;
$user = get_user_by('id', $author_id);
if (!$user || !in_array('dreamsrent_vendor', (array)$user->roles, true)) return;
if (function_exists('dreamsrent_fl_framework_getoptions') && dreamsrent_fl_framework_getoptions('enable_vendor_packages') && dreamsrent_fl_framework_getoptions('enable_vendor')) {
if (function_exists('dr_vendor_can_add_car') && !dr_vendor_can_add_car($author_id)) {
remove_action('transition_post_status', __FUNCTION__, 10);
wp_update_post(array('ID' => $post->ID, 'post_status' => 'draft'));
add_action('transition_post_status', __FUNCTION__, 10, 3);
update_post_meta($post->ID, '_dr_pkg_restricted', 1);
return;
}
}
}
}, 10, 3);
// function dreams_save_booking_post_with_product( $post_id ) {
// if ( ! isset( $_POST['dreams_save_post_booking_nonce'] ) || ! wp_verify_nonce( $_POST['dreams_save_post_booking_nonce'], 'dreams_save_post_booking' ) ) {
// return;
// }
// if ( ! wp_is_post_revision( $post_id ) && ! wp_is_post_autosave( $post_id ) && get_post_type( $post_id ) == 'rental' ) {
// $post = get_post( $post_id );
// $product_title = get_the_title( $post_id );
// $product_content = get_post_field('post_content', $post_id);
// $existing_product_id = get_post_meta( $post_id, '_booking_product_id', true );
// if ( ! empty( $existing_product_id ) ) {
// $product_data = array(
// 'ID' => $existing_product_id,
// 'post_title' => $product_title,
// 'post_content' => $product_content,
// );
// wp_update_post( $product_data );
// } else {
// $product_data = array(
// 'post_title' => $product_title,
// 'post_content' => $product_content,
// 'post_status' => 'publish',
// 'post_type' => 'product',
// );
// $product_id = wp_insert_post( $product_data );
// if ( ! is_wp_error( $product_id ) && $product_id > 0 ) {
// wp_set_post_terms( $product_id, 'carrental', 'product_type', false );
// update_post_meta( $post_id, '_booking_product_id', $product_id );
// }
// }
// }
// }
// add_action( 'save_post', 'dreams_save_booking_post_with_product', 10, 2);
function dreams_add_booking_nonce_field() {
wp_nonce_field( 'dreams_save_post_booking', 'dreams_save_post_booking_nonce' );
}
add_action( 'post_submitbox_misc_actions', 'dreams_add_booking_nonce_field' );
add_action('woocommerce_product_query', 'dreams_exclude_product_types_from_shop');
function dreams_exclude_product_types_from_shop($query) {
// Check if it's the main shop page
if (is_admin() || !is_shop()) {
return;
}
// Define an array of product types to exclude
$excluded_product_types = array(
'carrental',
'product_type2',
// Add more product types as needed
);
// Modify the product query to exclude the specified product types
$query->set('post_type', 'product');
$query->set('tax_query', array(
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $excluded_product_types,
'operator' => 'NOT IN',
),
));
}
// function dreams_custom_post_type_rewrite_rules() {
// global $wp_post_types;
// $wp_post_types['rental']->rewrite['slug'] = 'new-slug'; // Change 'new-slug' to your desired slug
// }
// add_action( 'init', 'dreams_custom_post_type_rewrite_rules', 100 );
// Add Taxonomy 'Brand'
function dreams_register_brand_taxonomy() {
register_taxonomy(
'brand', // changed from 'cartype'
'rental',
array(
'label' => __( 'Brand' ),
'rewrite' => array( 'slug' => 'brand' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('brand') );
$labels->name = _x( 'Brands', 'taxonomy general name' );
$labels->singular_name = _x( 'Brand', 'taxonomy singular name' );
$labels->search_items = __( 'Search Brands' );
$labels->all_items = __( 'All Brands' );
$labels->edit_item = __( 'Edit Brand' );
$labels->update_item = __( 'Update Brand' );
$labels->add_new_item = __( 'Add New Brand' );
$labels->new_item_name = __( 'New Brand Name' );
$labels->menu_name = __( 'Brands' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['brand']->labels = $labels;
}
add_action( 'init', 'dreams_register_brand_taxonomy', 0 );
function dreams_add_brand_status_field() {
?>
<div class="form-field">
<label for="brand_status"><?php _e('Status'); ?></label>
<select name="brand_status" id="brand_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
</div>
<?php
}
add_action('brand_add_form_fields', 'dreams_add_brand_status_field', 20);
function dreams_edit_brand_status_field($term) {
$status = get_term_meta($term->term_id, 'brand_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="brand_status"><?php _e('Status'); ?></label></th>
<td>
<select name="brand_status" id="brand_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
</td>
</tr>
<?php
}
add_action('brand_edit_form_fields', 'dreams_edit_brand_status_field', 20);
function dreams_save_brand_status_meta($term_id) {
if (isset($_POST['brand_status'])) {
update_term_meta($term_id, 'brand_status', sanitize_text_field($_POST['brand_status']));
}
}
add_action('created_brand', 'dreams_save_brand_status_meta', 10, 1);
add_action('edited_brand', 'dreams_save_brand_status_meta', 10, 1);
function dreams_add_brand_status_column($columns) {
$columns['brand_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-brand_columns', 'dreams_add_brand_status_column');
function dreams_show_brand_status_column($content, $column_name, $term_id) {
if ($column_name === 'brand_status') {
$status = get_term_meta($term_id, 'brand_status', true);
if ($status === 'active') {
$content = '<span>Active</span>';
} elseif ($status === 'inactive') {
$content = '<span>Inactive</span>';
} else {
$content = '<span>-</span>';
}
}
return $content;
}
add_filter('manage_brand_custom_column', 'dreams_show_brand_status_column', 10, 3);
function dreams_add_brand_image_field() {
?>
<div class="form-field">
<label for="brand_image"><?php _e('Brand Icon'); ?></label>
<input type="button" class="button button-secondary brand-image-upload-button" value="<?php esc_attr_e('Upload Image'); ?>" />
<input type="hidden" name="brand_image" id="brand_image" value="" />
<div id="brand_image_preview" style="margin-top:10px;"></div>
</div>
<?php
}
add_action('brand_add_form_fields', 'dreams_add_brand_image_field', 10);
function dreams_edit_brand_image_field($term) {
$image_id = get_term_meta($term->term_id, 'brand_image', true);
$image_url = $image_id ? wp_get_attachment_image_url($image_id, 'thumbnail') : '';
?>
<tr class="form-field">
<th scope="row"><label for="brand_image"><?php _e('Brand Icon'); ?></label></th>
<td>
<input type="button" class="button button-secondary brand-image-upload-button" value="<?php esc_attr_e('Upload Image'); ?>" />
<input type="hidden" name="brand_image" id="brand_image" value="<?php echo esc_attr($image_id); ?>" />
<div id="brand_image_preview" style="margin-top:10px;">
<?php if ($image_url): ?>
<img src="<?php echo esc_url($image_url); ?>" style="max-width:100px;" />
<?php endif; ?>
</div>
</td>
</tr>
<?php
}
add_action('brand_edit_form_fields', 'dreams_edit_brand_image_field', 10);
function dreams_save_brand_image_meta($term_id) {
if (isset($_POST['brand_image'])) {
update_term_meta($term_id, 'brand_image', sanitize_text_field($_POST['brand_image']));
}
}
add_action('created_brand', 'dreams_save_brand_image_meta', 10, 1);
add_action('edited_brand', 'dreams_save_brand_image_meta', 10, 1);
function dreams_add_brand_image_column($columns) {
$columns['brand_image'] = __('Icon');
return $columns;
}
add_filter('manage_edit-brand_columns', 'dreams_add_brand_image_column');
function dreams_modify_brand_columns($columns) {
$new_columns = [];
// Preserve checkbox column first
if (isset($columns['cb'])) {
$new_columns['cb'] = $columns['cb'];
}
// Name column second
if (isset($columns['name'])) {
$new_columns['name'] = $columns['name'];
}
// Add custom 'Icon' column as 3rd
$new_columns['brand_image'] = __('Icon');
// Add the rest, skipping the original brand_image if it already exists
foreach ($columns as $key => $value) {
if (!in_array($key, ['cb', 'name', 'brand_image'])) {
$new_columns[$key] = $value;
}
}
return $new_columns;
}
add_filter('manage_edit-brand_columns', 'dreams_modify_brand_columns');
function dreams_show_brand_image_column($content, $column_name, $term_id) {
if ($column_name === 'brand_image') {
$image_id = get_term_meta($term_id, 'brand_image', true);
if ($image_id) {
$image_url = wp_get_attachment_image_url($image_id, 'thumbnail');
if ($image_url) {
$content = '<img src="' . esc_url($image_url) . '" style="max-width:50px; height:auto;" />';
}
}
}
return $content;
}
add_filter('manage_brand_custom_column', 'dreams_show_brand_image_column', 10, 3);
// Add Taxonomy 'model'
function dreams_register_model_taxonomy() {
register_taxonomy(
'model',
'rental',
array(
'label' => __( 'Model' ),
'rewrite' => array( 'slug' => 'model' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('model') );
$labels->name = _x( 'Models', 'taxonomy general name' );
$labels->singular_name = _x( 'Model', 'taxonomy singular name' );
$labels->search_items = __( 'Search Models' );
$labels->all_items = __( 'All Models' );
$labels->edit_item = __( 'Edit Model' );
$labels->update_item = __( 'Update Model' );
$labels->add_new_item = __( 'Add New Model' );
$labels->new_item_name = __( 'New Model Name' );
$labels->menu_name = __( 'Models' );
global $wp_taxonomies;
$wp_taxonomies['model']->labels = $labels;
}
add_action( 'init', 'dreams_register_model_taxonomy', 0 );
// Add Brand selector to Add Model form
function dreams_add_brand_dropdown_to_model_form() {
$brands = get_terms(array(
'taxonomy' => 'brand',
'hide_empty' => false,
));
?>
<div class="form-field">
<label for="model_brand"><?php _e('Select Brand'); ?></label>
<select name="model_brand" id="model_brand">
<option value=""><?php _e('Choose Brand'); ?></option>
<?php foreach ($brands as $brand): ?>
<option value="<?php echo esc_attr($brand->term_id); ?>"><?php echo esc_html($brand->name); ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e('Assign a Brand to this Model.'); ?></p>
</div>
<?php
}
add_action('model_add_form_fields', 'dreams_add_brand_dropdown_to_model_form');
// Edit form
function dreams_edit_brand_dropdown_in_model_form($term) {
$selected_brand = get_term_meta($term->term_id, 'model_brand', true);
$brands = get_terms(array(
'taxonomy' => 'brand',
'hide_empty' => false,
));
?>
<tr class="form-field">
<th scope="row"><label for="model_brand"><?php _e('Select Brand'); ?></label></th>
<td>
<select name="model_brand" id="model_brand">
<option value=""><?php _e('Choose Brand'); ?></option>
<?php foreach ($brands as $brand): ?>
<option value="<?php echo esc_attr($brand->term_id); ?>" <?php selected($selected_brand, $brand->term_id); ?>>
<?php echo esc_html($brand->name); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php _e('Assign a Brand to this Model.'); ?></p>
</td>
</tr>
<?php
}
add_action('model_edit_form_fields', 'dreams_edit_brand_dropdown_in_model_form');
// Save brand assigned to model (on create and edit)
function dreams_save_model_brand_meta($term_id) {
if (isset($_POST['model_brand'])) {
update_term_meta($term_id, 'model_brand', intval($_POST['model_brand']));
}
}
add_action('created_model', 'dreams_save_model_brand_meta', 10, 2);
add_action('edited_model', 'dreams_save_model_brand_meta', 10, 2);
// Add column
function dreams_add_model_columns($columns) {
$columns['model_brand'] = __('Brands Display');
return $columns;
}
add_filter('manage_edit-model_columns', 'dreams_add_model_columns');
// Column content
function dreams_show_model_column_content($content, $column_name, $term_id) {
if ($column_name === 'model_brand') {
$brand_id = get_term_meta($term_id, 'model_brand', true);
if ($brand_id) {
$brand = get_term($brand_id, 'brand');
if ($brand && !is_wp_error($brand)) {
echo esc_html($brand->name);
return;
}
}
echo '—';
}
}
add_action('manage_model_custom_column', 'dreams_show_model_column_content', 10, 3);
function dreams_add_status_dropdown_to_model_form() {
?>
<div class="form-field">
<label for="model_status"><?php _e('Status'); ?></label>
<select name="model_status" id="model_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Set the status of this Model.'); ?></p>
</div>
<?php
}
add_action('model_add_form_fields', 'dreams_add_status_dropdown_to_model_form');
function dreams_edit_status_dropdown_in_model_form($term) {
$selected_status = get_term_meta($term->term_id, 'model_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="model_status"><?php _e('Status'); ?></label></th>
<td>
<select name="model_status" id="model_status">
<option value="active" <?php selected($selected_status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($selected_status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Set the status of this Model.'); ?></p>
</td>
</tr>
<?php
}
add_action('model_edit_form_fields', 'dreams_edit_status_dropdown_in_model_form');
function dreams_save_model_status_meta($term_id) {
if (isset($_POST['model_status'])) {
update_term_meta($term_id, 'model_status', sanitize_text_field($_POST['model_status']));
}
}
add_action('created_model', 'dreams_save_model_status_meta', 10, 2);
add_action('edited_model', 'dreams_save_model_status_meta', 10, 2);
function dreams_add_model_status_column($columns) {
$columns['model_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-model_columns', 'dreams_add_model_status_column');
function dreams_show_model_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'model_status') {
$status = get_term_meta($term_id, 'model_status', true);
if ($status === 'active') {
$content = '<span>Active</span>';
} elseif ($status === 'inactive') {
$content = '<span>Inactive</span>';
} else {
$content = '<span>-</span>';
}
}
return $content;
}
add_action('manage_model_custom_column', 'dreams_show_model_status_column_content', 10, 3);
// Add Taxonomy 'color'
function dreams_register_color_taxonomy() {
register_taxonomy(
'color',
'rental',
array(
'label' => __( 'Color' ),
'rewrite' => array( 'slug' => 'color' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels(get_taxonomy('color'));
$labels->name = _x( 'Colors', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Color', 'taxonomy singular name' );
$labels->search_items = __( 'Search Colors' );
$labels->all_items = __( 'All Colors' );
$labels->edit_item = __( 'Edit Color' );
$labels->update_item = __( 'Update Color' );
$labels->add_new_item = __( 'Add New Color' );
$labels->new_item_name = __( 'New Color Name' );
$labels->menu_name = __( 'Colors' );
global $wp_taxonomies;
$wp_taxonomies['color']->labels = $labels;
}
add_action('init', 'dreams_register_color_taxonomy', 0 );
function dreams_add_color_picker_field() {
?>
<div class="form-field">
<label for="color_code"><?php _e('Color Code'); ?></label>
<input type="color" name="color_code" id="color_code" value="#000000" />
<p class="description"><?php _e('Select a color for this term.'); ?></p>
</div>
<?php
}
add_action('color_add_form_fields', 'dreams_add_color_picker_field');
function dreams_edit_color_picker_field($term) {
$color = get_term_meta($term->term_id, 'color_code', true);
if (empty($color)) $color = '#000000';
?>
<tr class="form-field">
<th scope="row"><label for="color_code"><?php _e('Color Code'); ?></label></th>
<td>
<input type="color" name="color_code" id="color_code" value="<?php echo esc_attr($color); ?>" />
<p class="description"><?php _e('Update the color for this term.'); ?></p>
</td>
</tr>
<?php
}
add_action('color_edit_form_fields', 'dreams_edit_color_picker_field');
function dreams_save_color_code_meta($term_id) {
if (isset($_POST['color_code'])) {
update_term_meta($term_id, 'color_code', sanitize_hex_color($_POST['color_code']));
}
}
add_action('created_color', 'dreams_save_color_code_meta', 10, 2);
add_action('edited_color', 'dreams_save_color_code_meta', 10, 2);
function dreams_add_color_column($columns) {
$columns['color_code'] = __('Color');
return $columns;
}
add_filter('manage_edit-color_columns', 'dreams_add_color_column');
function dreams_show_color_column_content($content, $column_name, $term_id) {
if ($column_name === 'color_code') {
$color = get_term_meta($term_id, 'color_code', true);
if ($color) {
echo '<span style="display:inline-block;width:20px;height:20px;background:' . esc_attr($color) . ';border-radius:3px;"></span>';
} else {
echo '—';
}
}
}
add_action('manage_color_custom_column', 'dreams_show_color_column_content', 10, 3);
function dreams_add_status_field_to_color_form() {
?>
<div class="form-field">
<label for="color_status"><?php _e('Status'); ?></label>
<select name="color_status" id="color_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Select the status of this color.'); ?></p>
</div>
<?php
}
add_action('color_add_form_fields', 'dreams_add_status_field_to_color_form');
function dreams_edit_status_field_in_color_form($term) {
$status = get_term_meta($term->term_id, 'color_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="color_status"><?php _e('Status'); ?></label></th>
<td>
<select name="color_status" id="color_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Update the status of this color.'); ?></p>
</td>
</tr>
<?php
}
add_action('color_edit_form_fields', 'dreams_edit_status_field_in_color_form');
function dreams_save_color_status_meta($term_id) {
if (isset($_POST['color_status'])) {
update_term_meta($term_id, 'color_status', sanitize_text_field($_POST['color_status']));
}
}
add_action('created_color', 'dreams_save_color_status_meta', 10, 2);
add_action('edited_color', 'dreams_save_color_status_meta', 10, 2);
function dreams_add_color_status_column($columns) {
$columns['color_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-color_columns', 'dreams_add_color_status_column');
function dreams_show_color_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'color_status') {
$status = get_term_meta($term_id, 'color_status', true);
echo esc_html(ucfirst($status) ?: '—');
}
}
add_action('manage_color_custom_column', 'dreams_show_color_status_column_content', 10, 3);
// Add Taxonomy 'seats'
function dreams_register_seats_taxonomy() {
register_taxonomy(
'seats',
'rental',
array(
'label' => __( 'Seats' ),
'rewrite' => array( 'slug' => 'seats' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels(get_taxonomy('seats'));
$labels->name = _x( 'Seats', 'taxonomy general name' );
$labels->singular_name = _x( 'Seat Option', 'taxonomy singular name' );
$labels->search_items = __( 'Search Seats' );
$labels->all_items = __( 'All Seats' );
$labels->edit_item = __( 'Edit Seat Option' );
$labels->update_item = __( 'Update Seat Option' );
$labels->add_new_item = __( 'Add New Seat Option' );
$labels->new_item_name = __( 'New Seat Option Name' );
$labels->menu_name = __( 'Seats' );
global $wp_taxonomies;
$wp_taxonomies['seats']->labels = $labels;
}
add_action('init', 'dreams_register_seats_taxonomy', 0);
// Add Status Field to 'Add New Seat' form
function dreams_add_status_field_to_seats_form() {
?>
<div class="form-field">
<label for="seats_status"><?php _e('Status'); ?></label>
<select name="seats_status" id="seats_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Select the status of this seat option.'); ?></p>
</div>
<?php
}
add_action('seats_add_form_fields', 'dreams_add_status_field_to_seats_form');
// Add Status Field to Edit Form
function dreams_edit_status_field_in_seats_form($term) {
$status = get_term_meta($term->term_id, 'seats_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="seats_status"><?php _e('Status'); ?></label></th>
<td>
<select name="seats_status" id="seats_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Update the status of this seat option.'); ?></p>
</td>
</tr>
<?php
}
add_action('seats_edit_form_fields', 'dreams_edit_status_field_in_seats_form');
// Save Status Meta
function dreams_save_seats_status_meta($term_id) {
if (isset($_POST['seats_status'])) {
update_term_meta($term_id, 'seats_status', sanitize_text_field($_POST['seats_status']));
}
}
add_action('created_seats', 'dreams_save_seats_status_meta', 10, 2);
add_action('edited_seats', 'dreams_save_seats_status_meta', 10, 2);
// Add Status Column to Seats Admin Table
function dreams_add_seats_status_column($columns) {
$columns['seats_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-seats_columns', 'dreams_add_seats_status_column');
// Show Status Column Content
function dreams_show_seats_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'seats_status') {
$status = get_term_meta($term_id, 'seats_status', true);
echo esc_html(ucfirst($status) ?: '—');
}
}
add_action('manage_seats_custom_column', 'dreams_show_seats_status_column_content', 10, 3);
// Add Taxonomy 'cylinders'
function dreams_register_cylinders_taxonomy() {
register_taxonomy(
'cylinders',
'rental',
array(
'label' => __( 'Cylinders' ),
'rewrite' => array( 'slug' => 'cylinders' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels(get_taxonomy('cylinders'));
$labels->name = _x( 'Cylinders', 'taxonomy general name' );
$labels->singular_name = _x( 'Cylinder Option', 'taxonomy singular name' );
$labels->search_items = __( 'Search Cylinders' );
$labels->all_items = __( 'All Cylinders' );
$labels->edit_item = __( 'Edit Cylinder Option' );
$labels->update_item = __( 'Update Cylinder Option' );
$labels->add_new_item = __( 'Add New Cylinder Option' );
$labels->new_item_name = __( 'New Cylinder Option Name' );
$labels->menu_name = __( 'Cylinders' );
global $wp_taxonomies;
$wp_taxonomies['cylinders']->labels = $labels;
}
add_action('init', 'dreams_register_cylinders_taxonomy', 0);
function dreams_add_status_field_to_cylinders_form() {
?>
<div class="form-field">
<label for="cylinders_status"><?php _e('Status'); ?></label>
<select name="cylinders_status" id="cylinders_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Select the status of this cylinder option.'); ?></p>
</div>
<?php
}
add_action('cylinders_add_form_fields', 'dreams_add_status_field_to_cylinders_form');
function dreams_edit_status_field_in_cylinders_form($term) {
$status = get_term_meta($term->term_id, 'cylinders_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="cylinders_status"><?php _e('Status'); ?></label></th>
<td>
<select name="cylinders_status" id="cylinders_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Update the status of this cylinder option.'); ?></p>
</td>
</tr>
<?php
}
add_action('cylinders_edit_form_fields', 'dreams_edit_status_field_in_cylinders_form');
function dreams_save_cylinders_status_meta($term_id) {
if (isset($_POST['cylinders_status'])) {
update_term_meta($term_id, 'cylinders_status', sanitize_text_field($_POST['cylinders_status']));
}
}
add_action('created_cylinders', 'dreams_save_cylinders_status_meta', 10, 2);
add_action('edited_cylinders', 'dreams_save_cylinders_status_meta', 10, 2);
function dreams_add_cylinders_status_column($columns) {
$columns['cylinders_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-cylinders_columns', 'dreams_add_cylinders_status_column');
function dreams_show_cylinders_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'cylinders_status') {
$status = get_term_meta($term_id, 'cylinders_status', true);
echo esc_html(ucfirst($status) ?: '—');
}
}
add_action('manage_cylinders_custom_column', 'dreams_show_cylinders_status_column_content', 10, 3);
//Add Taxonomy 'doors'
// Register 'doors' taxonomy for 'rental' post type
function dreams_register_doors_taxonomy() {
register_taxonomy(
'doors',
'rental',
array(
'label' => __( 'Doors' ),
'rewrite' => array( 'slug' => 'doors' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels(get_taxonomy('doors'));
$labels->name = _x( 'Doors', 'taxonomy general name' );
$labels->singular_name = _x( 'Door Option', 'taxonomy singular name' );
$labels->search_items = __( 'Search Doors' );
$labels->all_items = __( 'All Doors' );
$labels->edit_item = __( 'Edit Door Option' );
$labels->update_item = __( 'Update Door Option' );
$labels->add_new_item = __( 'Add New Door Option' );
$labels->new_item_name = __( 'New Door Option Name' );
$labels->menu_name = __( 'Doors' );
global $wp_taxonomies;
$wp_taxonomies['doors']->labels = $labels;
}
add_action('init', 'dreams_register_doors_taxonomy', 0);
// Add status field to 'doors' taxonomy add form
function dreams_add_status_field_to_doors_form() {
?>
<div class="form-field">
<label for="doors_status"><?php _e('Status'); ?></label>
<select name="doors_status" id="doors_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Select the status of this door option.'); ?></p>
</div>
<?php
}
add_action('doors_add_form_fields', 'dreams_add_status_field_to_doors_form');
// Edit status field in 'doors' taxonomy edit form
function dreams_edit_status_field_in_doors_form($term) {
$status = get_term_meta($term->term_id, 'doors_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="doors_status"><?php _e('Status'); ?></label></th>
<td>
<select name="doors_status" id="doors_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Update the status of this door option.'); ?></p>
</td>
</tr>
<?php
}
add_action('doors_edit_form_fields', 'dreams_edit_status_field_in_doors_form');
// Save the custom status field when creating or editing a door term
function dreams_save_doors_status_meta($term_id) {
if (isset($_POST['doors_status'])) {
update_term_meta($term_id, 'doors_status', sanitize_text_field($_POST['doors_status']));
}
}
add_action('created_doors', 'dreams_save_doors_status_meta', 10, 2);
add_action('edited_doors', 'dreams_save_doors_status_meta', 10, 2);
// Add custom column for 'status' in doors list table
function dreams_add_doors_status_column($columns) {
$columns['doors_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-doors_columns', 'dreams_add_doors_status_column');
// Display status column content for 'doors'
function dreams_show_doors_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'doors_status') {
$status = get_term_meta($term_id, 'doors_status', true);
echo esc_html(ucfirst($status) ?: '—');
}
}
add_action('manage_doors_custom_column', 'dreams_show_doors_status_column_content', 10, 3);
//Add Taxonomy 'features'
// Register 'features' taxonomy for 'rental' post type
function dreams_register_features_taxonomy() {
register_taxonomy(
'features',
'rental',
array(
'label' => __( 'Features' ),
'rewrite' => array( 'slug' => 'features' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels(get_taxonomy('features'));
$labels->name = _x( 'Features', 'taxonomy general name' );
$labels->singular_name = _x( 'Feature Option', 'taxonomy singular name' );
$labels->search_items = __( 'Search Features' );
$labels->all_items = __( 'All Features' );
$labels->edit_item = __( 'Edit Feature Option' );
$labels->update_item = __( 'Update Feature Option' );
$labels->add_new_item = __( 'Add New Feature Option' );
$labels->new_item_name = __( 'New Feature Option Name' );
$labels->menu_name = __( 'Features' );
global $wp_taxonomies;
$wp_taxonomies['features']->labels = $labels;
}
add_action('init', 'dreams_register_features_taxonomy', 0);
// Add status field to 'features' taxonomy add form
function dreams_add_status_field_to_features_form() {
?>
<div class="form-field">
<label for="features_status"><?php _e('Status'); ?></label>
<select name="features_status" id="features_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Select the status of this feature option.'); ?></p>
</div>
<?php
}
add_action('features_add_form_fields', 'dreams_add_status_field_to_features_form');
// Edit status field in 'features' taxonomy edit form
function dreams_edit_status_field_in_features_form($term) {
$status = get_term_meta($term->term_id, 'features_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="features_status"><?php _e('Status'); ?></label></th>
<td>
<select name="features_status" id="features_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Update the status of this feature option.'); ?></p>
</td>
</tr>
<?php
}
add_action('features_edit_form_fields', 'dreams_edit_status_field_in_features_form');
// Save the custom status field when creating or editing a feature term
function dreams_save_features_status_meta($term_id) {
if (isset($_POST['features_status'])) {
update_term_meta($term_id, 'features_status', sanitize_text_field($_POST['features_status']));
}
}
add_action('created_features', 'dreams_save_features_status_meta', 10, 2);
add_action('edited_features', 'dreams_save_features_status_meta', 10, 2);
// Add custom column for 'status' in features list table
function dreams_add_features_status_column($columns) {
$columns['features_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-features_columns', 'dreams_add_features_status_column');
// Display status column content for 'features'
function dreams_show_features_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'features_status') {
$status = get_term_meta($term_id, 'features_status', true);
echo esc_html(ucfirst($status) ?: '—');
}
}
add_action('manage_features_custom_column', 'dreams_show_features_status_column_content', 10, 3);
//Add Taxonomy 'safety_features'
// Register 'safety_features' taxonomy for 'rental' post type
function dreams_register_safety_features_taxonomy() {
register_taxonomy(
'safety_features',
'rental',
array(
'label' => __( 'Safety Features' ),
'rewrite' => array( 'slug' => 'safety-features' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels(get_taxonomy('safety_features'));
$labels->name = _x( 'Safety Features', 'taxonomy general name' );
$labels->singular_name = _x( 'Safety Feature Option', 'taxonomy singular name' );
$labels->search_items = __( 'Search Safety Features' );
$labels->all_items = __( 'All Safety Features' );
$labels->edit_item = __( 'Edit Safety Feature Option' );
$labels->update_item = __( 'Update Safety Feature Option' );
$labels->add_new_item = __( 'Add New Safety Feature Option' );
$labels->new_item_name = __( 'New Safety Feature Option Name' );
$labels->menu_name = __( 'Safety Features' );
global $wp_taxonomies;
$wp_taxonomies['safety_features']->labels = $labels;
}
add_action('init', 'dreams_register_safety_features_taxonomy', 0);
// Add status field to add form
function dreams_add_status_field_to_safety_features_form() {
?>
<div class="form-field">
<label for="safety_features_status"><?php _e('Status'); ?></label>
<select name="safety_features_status" id="safety_features_status">
<option value="active"><?php _e('Active'); ?></option>
<option value="inactive"><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Select the status of this safety feature option.'); ?></p>
</div>
<?php
}
add_action('safety_features_add_form_fields', 'dreams_add_status_field_to_safety_features_form');
// Edit status field in edit form
function dreams_edit_status_field_in_safety_features_form($term) {
$status = get_term_meta($term->term_id, 'safety_features_status', true);
?>
<tr class="form-field">
<th scope="row"><label for="safety_features_status"><?php _e('Status'); ?></label></th>
<td>
<select name="safety_features_status" id="safety_features_status">
<option value="active" <?php selected($status, 'active'); ?>><?php _e('Active'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php _e('Inactive'); ?></option>
</select>
<p class="description"><?php _e('Update the status of this safety feature option.'); ?></p>
</td>
</tr>
<?php
}
add_action('safety_features_edit_form_fields', 'dreams_edit_status_field_in_safety_features_form');
// Save status meta
function dreams_save_safety_features_status_meta($term_id) {
if (isset($_POST['safety_features_status'])) {
update_term_meta($term_id, 'safety_features_status', sanitize_text_field($_POST['safety_features_status']));
}
}
add_action('created_safety_features', 'dreams_save_safety_features_status_meta', 10, 2);
add_action('edited_safety_features', 'dreams_save_safety_features_status_meta', 10, 2);
// Add status column
function dreams_add_safety_features_status_column($columns) {
$columns['safety_features_status'] = __('Status');
return $columns;
}
add_filter('manage_edit-safety_features_columns', 'dreams_add_safety_features_status_column');
// Show status column content
function dreams_show_safety_features_status_column_content($content, $column_name, $term_id) {
if ($column_name === 'safety_features_status') {
$status = get_term_meta($term_id, 'safety_features_status', true);
echo esc_html(ucfirst($status) ?: '—');
}
}
add_action('manage_safety_features_custom_column', 'dreams_show_safety_features_status_column_content', 10, 3);
// //Add Taxonomy 'location'
// function dreams_register_location_taxonomy() {
// register_taxonomy(
// 'location',
// 'rental',
// array(
// 'label' => __( 'Location' ),
// 'rewrite' => array( 'slug' => 'location' ),
// 'hierarchical' => true,
// )
// );
// $labels = get_taxonomy_labels( get_taxonomy('location') );
// $labels->name = _x( 'Location', 'taxonomy general name' );
// $labels->singular_name = _x( 'Location', 'taxonomy singular name' );
// $labels->search_items = __( 'Search Locations' );
// $labels->all_items = __( 'All Locations' );
// $labels->edit_item = __( 'Edit Location' );
// $labels->update_item = __( 'Update Location' );
// $labels->add_new_item = __( 'Add New Location' );
// $labels->new_item_name = __( 'New Location Name' );
// $labels->menu_name = __( 'Locations' );
// // Update the taxonomy labels
// global $wp_taxonomies;
// $wp_taxonomies['location']->labels = $labels;
// }
// add_action( 'init', 'dreams_register_location_taxonomy', 6 );
//Add Taxonomy 'Vehicle ID'
// function dreams_register_vehicleid_taxonomy() {
// register_taxonomy(
// 'vehicleid',
// 'rental',
// array(
// 'label' => __( 'Vehicle ID' ),
// 'rewrite' => array( 'slug' => 'vehicleid' ),
// 'hierarchical' => true,
// )
// );
// $labels = get_taxonomy_labels( get_taxonomy('vehicleid') );
// $labels->name = _x( 'Vehicle ID', 'taxonomy general name' );
// $labels->singular_name = _x( 'Vehicle ID', 'taxonomy singular name' );
// $labels->search_items = __( 'Search Vehicle IDs' );
// $labels->all_items = __( 'All Vehicle IDs' );
// $labels->edit_item = __( 'Edit Vehicle ID' );
// $labels->update_item = __( 'Update Vehicle ID' );
// $labels->add_new_item = __( 'Add New Vehicle ID' );
// $labels->new_item_name = __( 'New Vehicle ID Name' );
// $labels->menu_name = __( 'Vehicle IDs' );
// // Update the taxonomy labels
// global $wp_taxonomies;
// $wp_taxonomies['vehicleid']->labels = $labels;
// }
// add_action( 'init', 'dreams_register_vehicleid_taxonomy', 1 );
// Add Taxonomy 'Category'
// function dreams_register_category_taxonomy() {
// register_taxonomy(
// 'rentalcategory',
// 'rental',
// array(
// 'label' => __( 'Category' ),
// 'rewrite' => array( 'slug' => 'rentalcategory' ),
// 'hierarchical' => true,
// )
// );
// }
// add_action( 'init', 'dreams_register_category_taxonomy', 8 );
// Add Taxonomy 'Car Category'
function dreams_register_carcategory_taxonomy() {
register_taxonomy(
'carcategory',
'rental',
array(
'label' => __( 'Car Category' ),
'rewrite' => array( 'slug' => 'carcategory' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('carcategory') );
$labels->name = _x( 'Car Category', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Category', 'taxonomy singular name' );
$labels->search_items = __( 'Search Car Category' );
$labels->all_items = __( 'All Car Category' );
$labels->edit_item = __( 'Edit Car Category' );
$labels->update_item = __( 'Update Car Category' );
$labels->add_new_item = __( 'Add New Car Category' );
$labels->new_item_name = __( 'New Car Category Name' );
$labels->menu_name = __( 'Car Category' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['carcategory']->labels = $labels;
}
add_action( 'init', 'dreams_register_carcategory_taxonomy', 2 );
// Add Taxonomy 'Car Type'
function dreams_register_cartype_taxonomy() {
register_taxonomy(
'cartype',
'rental',
array(
'label' => __( 'Car Type' ),
'rewrite' => array( 'slug' => 'cartype' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('cartype') );
$labels->name = _x( 'Car Type', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Type', 'taxonomy singular name' );
$labels->search_items = __( 'Search Car Types' );
$labels->all_items = __( 'All Car Types' );
$labels->edit_item = __( 'Edit Car Type' );
$labels->update_item = __( 'Update Car Type' );
$labels->add_new_item = __( 'Add New Car Type' );
$labels->new_item_name = __( 'New Car Type Name' );
$labels->menu_name = __( 'Car Types' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['cartype']->labels = $labels;
}
add_action( 'init', 'dreams_register_cartype_taxonomy', 3 );
// Add Taxonomy 'Car Capacity'
function dreams_register_carcapacity_taxonomy() {
register_taxonomy(
'carcapacity',
'rental',
array(
'label' => __( 'Car Capacity' ),
'rewrite' => array( 'slug' => 'carcapacity' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('carcapacity') );
$labels->name = _x( 'Car Capacity', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Capacity', 'taxonomy singular name' );
$labels->search_items = __( 'Search Car Capacitys' );
$labels->all_items = __( 'All Car Capacitys' );
$labels->edit_item = __( 'Edit Car Capacity' );
$labels->update_item = __( 'Update Car Capacity' );
$labels->add_new_item = __( 'Add New Car Capacity' );
$labels->new_item_name = __( 'New Car Capacity Name' );
$labels->menu_name = __( 'Car Capacity' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['carcapacity']->labels = $labels;
}
add_action( 'init', 'dreams_register_carcapacity_taxonomy', 4 );
// Add Taxonomy 'Car Manufacturer'
// function dreams_register_carmanufacturer_taxonomy() {
// register_taxonomy(
// 'carmanufacturer',
// 'rental',
// array(
// 'label' => __( 'Car Manufacturer' ),
// 'rewrite' => array( 'slug' => 'carmanufacturer' ),
// 'hierarchical' => true,
// )
// );
// $labels = get_taxonomy_labels( get_taxonomy('carmanufacturer') );
// $labels->name = _x( 'Car Manufacturer', 'taxonomy general name' );
// $labels->singular_name = _x( 'Car Manufacturer', 'taxonomy singular name' );
// $labels->search_items = __( 'Search Car Manufacturers' );
// $labels->all_items = __( 'All Car Manufacturers' );
// $labels->edit_item = __( 'Edit Car Manufacturer' );
// $labels->update_item = __( 'Update Car Manufacturer' );
// $labels->add_new_item = __( 'Add New Car Manufacturer' );
// $labels->new_item_name = __( 'New Car Manufacturer Name' );
// $labels->menu_name = __( 'Car Manufacturer' );
// // Update the taxonomy labels
// global $wp_taxonomies;
// $wp_taxonomies['carmanufacturer']->labels = $labels;
// }
// add_action( 'init', 'dreams_register_carmanufacturer_taxonomy', 6 );
// Add Taxonomy 'Car Steering'
function dreams_register_carsteering_taxonomy() {
register_taxonomy(
'carsteering',
'rental',
array(
'label' => __( 'Car Steering' ),
'rewrite' => array( 'slug' => 'carsteering' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('carsteering') );
$labels->name = _x( 'Car Steering', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Steering', 'taxonomy singular name' );
$labels->search_items = __( 'Search Car Steerings' );
$labels->all_items = __( 'All Car Steerings' );
$labels->edit_item = __( 'Edit Car Steering' );
$labels->update_item = __( 'Update Car Steering' );
$labels->add_new_item = __( 'Add New Car Steering' );
$labels->new_item_name = __( 'New Car Steering Name' );
$labels->menu_name = __( 'Car Steering' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['carsteering']->labels = $labels;
}
add_action( 'init', 'dreams_register_carsteering_taxonomy', 5 );
// Add Taxonomy 'Car Specification'
// function dreams_register_carspecification_taxonomy() {
// register_taxonomy(
// 'carspecification',
// 'rental',
// array(
// 'label' => __( 'Car Specification' ),
// 'rewrite' => array( 'slug' => 'carspecification' ),
// 'hierarchical' => true,
// )
// );
// $labels = get_taxonomy_labels( get_taxonomy('carspecification') );
// $labels->name = _x( 'Car Specification', 'taxonomy general name' );
// $labels->singular_name = _x( 'Car Specification', 'taxonomy singular name' );
// $labels->search_items = __( 'Search Car Specifications' );
// $labels->all_items = __( 'All Car Specifications' );
// $labels->edit_item = __( 'Edit Car Specification' );
// $labels->update_item = __( 'Update Car Specification' );
// $labels->add_new_item = __( 'Add New Car Specification' );
// $labels->new_item_name = __( 'New Car Specification Name' );
// $labels->menu_name = __( 'Car Specification' );
// // Update the taxonomy labels
// global $wp_taxonomies;
// $wp_taxonomies['carspecification']->labels = $labels;
// }
// add_action( 'init', 'dreams_register_carspecification_taxonomy', 7 );
// Add Taxonomy 'Car Fuel Type'
function dreams_register_carfueltype_taxonomy() {
register_taxonomy(
'carfueltype',
'rental',
array(
'label' => __( 'Car Fuel Type' ),
'rewrite' => array( 'slug' => 'carfueltype' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('carfueltype') );
$labels->name = _x( 'Car Fuel Type', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Fuel Type', 'taxonomy singular name' );
$labels->search_items = __( 'Search Car Fuel Type' );
$labels->all_items = __( 'All Car Fuel Type' );
$labels->edit_item = __( 'Edit Car Fuel Type' );
$labels->update_item = __( 'Update Car Fuel Type' );
$labels->add_new_item = __( 'Add New Car Fuel Type' );
$labels->new_item_name = __( 'New Car Fuel Type Name' );
$labels->menu_name = __( 'Car Fuel Type' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['carfueltype']->labels = $labels;
}
add_action( 'init', 'dreams_register_carfueltype_taxonomy', 6 );
// Add Taxonomy 'Car Transmission'
function dreams_register_cartransmission_taxonomy() {
register_taxonomy(
'cartransmission',
'rental',
array(
'label' => __( 'Car Transmission' ),
'rewrite' => array( 'slug' => 'cartransmission' ),
'hierarchical' => true,
)
);
$labels = get_taxonomy_labels( get_taxonomy('cartransmission') );
$labels->name = _x( 'Car Transmission', 'taxonomy general name' );
$labels->singular_name = _x( 'Car Transmission', 'taxonomy singular name' );
$labels->search_items = __( 'Search Car Transmission' );
$labels->all_items = __( 'All Car Transmission' );
$labels->edit_item = __( 'Edit Car Transmission' );
$labels->update_item = __( 'Update Car Transmission' );
$labels->add_new_item = __( 'Add New Car Transmission' );
$labels->new_item_name = __( 'New Car Transmission Name' );
$labels->menu_name = __( 'Car Transmission' );
// Update the taxonomy labels
global $wp_taxonomies;
$wp_taxonomies['cartransmission']->labels = $labels;
}
add_action( 'init', 'dreams_register_cartransmission_taxonomy', 7 );
//For Client
// Enqueue scripts and localize for AJAX
function dreams_enqueue_scripts() {
wp_enqueue_script('dreams-ajax-script', DSBRENT_PLUGIN_URI.'js/dreams-ajax.js', array('jquery'),null,true);
// Localize the script with new data
wp_localize_script('dreams-ajax-script', 'dreams_ajax_obj', array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('dreams_ajax_nonce')
));
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_script('timepicker', 'https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.js', array('jquery'), null, true);
wp_enqueue_style('jquery-ui', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css');
wp_enqueue_style('timepicker-css', 'https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.min.css');
}
add_action('admin_enqueue_scripts', 'dreams_enqueue_scripts');
// Add the submenu page
// function dreams_add_car_rental_submenu() {
// add_submenu_page(
// 'edit.php?post_type=rental',
// __('Book a Vehicles', 'bookingcore'), // Title with translation support
// __('Book a Vehicles', 'bookingcore'), // Title with translation support
// 'manage_options',
// 'manage_vehicles',
// 'dreams_manage_vehicles_page'
// );
// }
// add_action('admin_menu', 'dreams_add_car_rental_submenu');
// Display the Manage Vehicles page
function dreams_manage_vehicles_page() {
$vehicles = get_posts(array(
'post_type' => 'rental',
'posts_per_page' => -1,
'post_status' => 'publish',
));
?>
<style>
/* General form styling to mimic WordPress default */
.validation_error {
color: red;
margin:10px 0!important;
}
.wp-default-form {
padding: 20px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #fff;
}
/* Paragraph styling within forms */
.wp-default-form p {
margin: 0 0 15px;
}
.wp-default-form .reserv_notice {
width: auto!important;
padding: 8px!important;
margin: 10px 0;
}
.wp-default-form .reserv_notice p {
margin: 0;
}
/* Label styling */
.wp-default-form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
font-size: 14px;
}
/* Input field styling */
.wp-input {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
font-size: 14px;
}
/* Textarea styling */
.wp-textarea {
width: 100%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 3px;
font-size: 14px;
height: 100px;
resize: vertical;
}
.rental_type_button p{
display: flex;
}
.dsrent-type-deposit {
margin-bottom: 15px;
margin-top: 10px;
}
.wp-default-form .rental_type_button p label, .dsrent-type-deposit label {
margin-right: 15px;
}
.customer-fields {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
.customer-fields .form-column {
display: flex;
flex-direction: column;
}
.wp-default-form .wp-input {
width: 100%;
}
@media (max-width: 768px) {
.customer-fields {
grid-template-columns: 1fr;
}
}
.vehicle-form {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.vehicle-form .form-group {
display: flex;
flex-direction: column;
}
.wp-input {
width: 100%;
}
@media (max-width: 768px) {
.vehicle-form {
grid-template-columns: 1fr;
}
}
.wp-core-ui .manage-vehicles select {
max-width: 100%;
}
.dreamsrent_extra_service {
margin-bottom: 15px;
}
.vehicle-form {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.vehicle-form .form-group {
display: flex;
flex-direction: column;
}
.wp-input {
width: 100%;
}
@media (max-width: 768px) {
.vehicle-form {
grid-template-columns: 1fr;
}
}
.vehicle-form-new {
margin: 15px 0 0;
}
.vehicle-form-new input{
width: 100%;
}
.extra_services_total_class .form-group{
margin-top: 15px;
}
.extra_services_total_class select{
width: 100%;
max-width: 100%;
}
</style>
<div class="wrap">
<h1><?php esc_html_e('Book a Vehicles', 'bookingcore'); ?></h1>
<h2><?php esc_html_e('Here you can manage all vehicles associated with the Car Rental.', 'bookingcore'); ?></h2>
<div class="manage-vehicles-error"></div>
<div class=" wp-default-form manage-vehicles">
<?php if (!empty($vehicles)) : ?>
<p>
<label for="vehicle_dropdown"><?php esc_html_e('Select a Vehicle:', 'bookingcore'); ?></label>
<select id="vehicle_dropdown" name="vehicle_dropdown" class="wp-input">
<option value=""><?php esc_html_e('Select a vehicle', 'bookingcore'); ?></option>
<?php foreach ($vehicles as $vehicle) : ?>
<option value="<?php echo esc_attr($vehicle->ID); ?>" >
<?php echo esc_html($vehicle->post_title); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<?php else : ?>
<p><?php esc_html_e('No vehicles found.', 'bookingcore'); ?></p>
<?php endif; ?>
<div id="post_meta"></div>
<div class="vehicle-form">
<div class="form-group">
<p> <label for="start_date"><?php esc_html_e('Start Date:', 'bookingcore'); ?></label>
<input type="text" id="start_date" name="start_date" class="datepicker wp-input" required="" autocomplete="off"></p>
</div>
<div class="form-group">
<p> <label for="start_time"><?php esc_html_e('Start Time:', 'bookingcore'); ?></label>
<input type="text" id="start_time" name="start_time" class="timepicker wp-input" required="" autocomplete="off"></p>
</div>
<div class="form-group">
<p> <label for="end_date"><?php esc_html_e('End Date:', 'bookingcore'); ?></label>
<input type="text" id="end_date" name="end_date" class="datepicker wp-input" required="" autocomplete="off"></p>
</div>
<div class="form-group">
<p><label for="end_time"><?php esc_html_e('End Time:', 'bookingcore'); ?></label>
<input type="text" id="end_time" name="end_time" class="timepicker wp-input" required="" autocomplete="off"></p>
</div>
</div>
<div >
<a href="javascript:void(0);" style="display: none;" id="view_price" class="button button-primary button-large">
<?php esc_html_e('View Cost', 'bookingcore'); ?>
</a>
</div>
<div class=" vehicle-form extra_services_total_class" >
<div id="extra_services_total" >
</div>
<div class="form-group " id="payment_status" style="display: none;">
<p><label for="end_date"><?php esc_html_e('Status:', 'bookingcore'); ?></label>
<select name="status_order" id="status_order">
<option value="pending" selected="">Pending payment</option>
<option value="completed" >Completed</option>
<option value="processing">Processing</option>
<option value="on-hold">On hold</option>
<option value="cancelled">Cancelled</option>
<option value="refunded">Refunded</option>
<option value="failed">Failed</option>
</select>
</p>
</div>
</div>
</div>
<h2 ><?php esc_html_e('Customer Information', 'bookingcore'); ?></h2>
<div class="wp-default-form manage-vehicles ">
<div class="customer-fields">
<div class="form-column">
<p>
<label for="first_name"><?php esc_html_e('First Name:', 'bookingcore'); ?></label>
<input type="text" id="first_name" name="first_name" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="last_name"><?php esc_html_e('Last Name:', 'bookingcore'); ?></label>
<input type="text" id="last_name" name="last_name" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="country"><?php esc_html_e('Country:', 'bookingcore'); ?></label>
<input type="text" id="country" name="country" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="street_address"><?php esc_html_e('Street Address:', 'bookingcore'); ?></label>
<input type="text" id="street_address" name="street_address" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="city"><?php esc_html_e('Town/City:', 'bookingcore'); ?></label>
<input type="text" id="city" name="city" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="state"><?php esc_html_e('State:', 'bookingcore'); ?></label>
<input type="text" id="state" name="state" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="postcode"><?php esc_html_e('ZIP Code:', 'bookingcore'); ?></label>
<input type="text" id="postcode" name="postcode" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="phone"><?php esc_html_e('Phone:', 'bookingcore'); ?></label>
<input type="tel" id="phone" name="phone" class="wp-input">
</p>
</div>
<div class="form-column">
<p>
<label for="email"><?php esc_html_e('Email Address:', 'bookingcore'); ?></label>
<input type="email" id="email" name="email" class="wp-input">
</p>
</div>
</div>
<button type="button" style="display: none;" id="create_order" class="button button-primary button-large">
<?php esc_html_e('Create Order', 'bookingcore'); ?>
</button>
<div class="erro_message"></div>
</div>
</div>
<?php
}
// AJAX handler for fetching vehicle meta
function dreams_extra_price_get() {
check_ajax_referer('dreams_ajax_nonce', 'nonce');
$vehicle_id = intval($_POST['vehicle_id']);
$timeoption = sanitize_text_field($_POST['timeoption']);
$dsrent_pickup_loc = sanitize_text_field($_POST['dsrent_pickup_loc']);
$dsrent_pickoff_loc = sanitize_text_field($_POST['dsrent_pickoff_loc']);
$start_date = sanitize_text_field($_POST['start_date']);
$start_time = sanitize_text_field($_POST['start_time']);
$end_date = sanitize_text_field($_POST['end_date']);
$end_time = sanitize_text_field($_POST['end_time']);
$extra_price_get = sanitize_text_field($_POST['extra_price_get']);
// Customize the output based on the retrieved post meta
ob_start();
?>
<?php
if ($start_time && $end_time && $start_date && $end_date) {
// Combine date and time into DateTime objects
$start_datetime = new DateTime($start_date . ' ' . $start_time);
$end_datetime = new DateTime($end_date . ' ' . $end_time);
// Calculate the time difference
$time_difference = $start_datetime->diff($end_datetime);
// Get total hours including minutes
$total_minutes = ($time_difference->days * 24 * 60) + ($time_difference->h * 60) + $time_difference->i;
$total_hours = $total_minutes / 60;
// Output the total hours
// Use ceil() to round up if you want to count incomplete hours as a full hour
$time_difference_minutes = ceil($total_hours);
}
?>
<?php
if($timeoption === 'day'){
if (isset($_POST['start_date']) && isset($_POST['end_date'])) {
$pickup_date = new DateTime(sanitize_text_field($_POST['start_date']));
$pickoff_date = new DateTime(sanitize_text_field($_POST['end_date']));
if ($pickup_date && $pickoff_date) {
$date_difference = $pickup_date->diff($pickoff_date)->days;
}
}
if ($date_difference == '1') {
$rental_price = get_post_meta($vehicle_id, 'dreams_booking_meta_rdprice', true);
} else {
// Get the serialized meta data
$serialized_data = get_post_meta($vehicle_id, '_day_days', true);
if ($serialized_data) {
// Unserialize the data
$data = unserialize($serialized_data);
// Extract the day_days and day_price values
$days_prices = [];
foreach ($data as $entry) {
$days_prices[] = [
'day_days' => (int)$entry['day_days'],
'day_price' => (float)$entry['day_price']
];
}
// Sort the array based on day_days in ascending order
usort($days_prices, function($a, $b) {
return $a['day_days'] - $b['day_days'];
});
// Find the correct day_price based on date_difference
$rental_price = null;
foreach ($days_prices as $index => $entry) {
if ($date_difference == $entry['day_days']) {
$rental_price = $entry['day_price'];
break;
} elseif ($date_difference < $entry['day_days']) {
if ($index == 0) {
// If date_difference is less than the smallest day_days value
$rental_price = get_post_meta($vehicle_id, 'dreams_booking_meta_rdprice', true);
} else {
// Use the previous entry's day_price
$rental_price = $days_prices[$index - 1]['day_price'];
}
break;
}
}
// If date_difference is greater than the largest day_days value, use the last entry's day_price
if ($rental_price === null && !empty($days_prices)) {
$rental_price = end($days_prices)['day_price'];
}
} else {
// If no serialized data exists, fallback to default price
$rental_price = get_post_meta($vehicle_id, 'dreams_booking_meta_rdprice', true);
}
}
}
else if($timeoption == 'hour'){
if ($time_difference_minutes == '1') {
$hour_price_get = get_post_meta($vehicle_id, 'dreams_booking_meta_rhprice', true);
$rental_price = $hour_price_get * $time_difference_minutes;
}
else {
// Get the serialized meta data
$serialized_data = get_post_meta($vehicle_id, '_hour_days', true);
if ($serialized_data) {
// Unserialize the data
$data = unserialize($serialized_data);
// Extract the day_days and day_price values
$hour_prices = [];
foreach ($data as $entry) {
$hour_prices[] = [
'hour_days' => (int)$entry['hour_days'],
'hour_price' => (float)$entry['hour_price']
];
}
// Sort the array based on day_days in ascending order
usort($hour_prices, function($a, $b) {
return $a['hour_days'] - $b['hour_days'];
});
// Find the correct day_price based on date_difference
$rental_price = null;
foreach ($hour_prices as $index => $entry) {
if ($time_difference_minutes == $entry['hour_days']) {
$rental_price = $time_difference_minutes * $entry['hour_price'];
break;
} elseif ($time_difference_minutes < $entry['hour_days']) {
if ($index == 0) {
// If date_difference is less than the smallest day_days value
$rental_price = $time_difference_minutes * get_post_meta($vehicle_id, 'dreams_booking_meta_rhprice', true) ;
} else {
// Use the previous entry's day_price
$rental_price = $time_difference_minutes * $hour_prices[$index - 1]['hour_price'];
}
break;
}
}
// If date_difference is greater than the largest day_days value, use the last entry's day_price
if ($rental_price === null && !empty($hour_prices)) {
$rental_price = end($hour_prices)['hour_price'] * $time_difference_minutes;
}
} else {
// If no serialized data exists, fallback to default price
$rental_price = floatval($time_difference_minutes) * floatval(get_post_meta($vehicle_id, 'dreams_booking_meta_rhprice', true));
}
}
}
?>
<?php if($date_difference!='') { ?>
<?php $rental_price_total = $rental_price*$date_difference; ?>
<?php } else { ?>
<?php $rental_price_total = $rental_price; ?>
<?php } ?>
<?php $dsrent_insurance = get_post_meta($vehicle_id, 'dreams_booking_meta_amount_insurance', true); ?>
<?php $rental_price_total_all = $rental_price_total+$extra_price_get+$dsrent_insurance; ?>
<div class="form-group">
<p> <label for="end_date"><?php esc_html_e('Price:', 'bookingcore'); ?></label>
<input type="text" class="wp-input" value="<?php echo $rental_price_total_all; ?>" id="rental_price_total" name="rental_price_total">
</p>
</div>
<?php
$output = ob_get_clean();
wp_send_json_success($output);
}
add_action('wp_ajax_extra_price_get', 'dreams_extra_price_get');
// AJAX handler for fetching vehicle meta
function dreams_get_vehicle_meta() {
check_ajax_referer('dreams_ajax_nonce', 'nonce');
if (!isset($_POST['vehicle_id']) || empty($_POST['vehicle_id'])) {
wp_send_json_error(__('Invalid vehicle ID.', 'bookingcore'));
}
$vehicle_id = intval($_POST['vehicle_id']);
$booking_product_id = get_post_meta($vehicle_id, '_booking_product_id', true);
$day_booking = get_post_meta( $vehicle_id, 'dreams_booking_meta_rentaltype_day', true );
$hour_booking = get_post_meta( $vehicle_id, 'dreams_booking_meta_rentaltype_hour', true );
if (!$booking_product_id) {
wp_send_json_error(__('No booking product ID found for this vehicle.', 'bookingcore'));
}
// Customize the output based on the retrieved post meta
ob_start();
?>
<input type="hidden" name="product_id" id="" value="<?php echo esc_html($booking_product_id); ?>">
<div class="rental_type_button">
<label for="start_date"><?php esc_html_e('Rantal Type:', 'bookingcore'); ?></label>
<p>
<?php if($day_booking == 'day' && !empty($day_booking)) { ?>
<label class="radio_rental"><?php echo esc_html__('Day', 'bookingcore' ) ?>
<input type="radio" id="time-day" name="timeoption" checked value="day" />
<span class="checkmark"></span>
</label>
<?php } ?>
<?php if($hour_booking == 'hour' && !empty($hour_booking)) { ?>
<label class="radio_rental"><?php echo esc_html__('Hour', 'bookingcore' ) ?>
<input type="radio" id="time-hour" name="timeoption" value="hour" >
<span class="checkmark"></span>
</label>
<?php } ?>
</p>
</div>
<div class="vehicle-form">
<div class="form-group">
<p> <label for="start_date"><?php esc_html_e('Pickup Location:', 'bookingcore'); ?></label>
<?php
$plocations = get_post_meta($vehicle_id, '_rent_locations', true);
if ($plocations) {
$locations_data = maybe_unserialize( $plocations, true);
?>
<select id="dsrent_pickup_loc" class="select wp-input form-control dsrent_pickup_loc" required="">
<!-- <select class="select form-control dsrent_pickup_loc"> -->
<option value=""><?php echo esc_html__('Select Location', 'bookingcore'); ?></option>
<?php
foreach ($locations_data as $plocation) {
if (isset($plocation['plocation'])) {
$plocation_value = $plocation['plocation'];
$plocation_name = get_term($plocation_value)->name;
?>
<option value="<?php echo esc_attr($plocation_value); ?>">
<?php echo esc_html($plocation_name); ?>
</option>
<?php
}
}
?>
</select>
<div id="pickup_loc_error" class="text-danger"></div>
<?php
} else { ?>
<select class="select wp-input form-control">
<option value="<?php echo esc_attr("No Location Found"); ?>">
<?php echo esc_html__('No Location Found', 'bookingcore'); ?></option>
</select>
</p>
<?php
}
?>
<div id="dsrent_pickoff_loc_error">
</div>
</div>
<div class="form-group">
<p> <label for="start_time"><?php esc_html_e('Dropoff Location:', 'bookingcore'); ?></label>
<?php
$dlocations = get_post_meta($vehicle_id, '_rent_locations', true);
if ($dlocations) {
$locations_data = maybe_unserialize( $dlocations, true);
?>
<select id="dsrent_pickoff_loc" class="select wp-input form-control dsrent_pickoff_loc" required="">
<!-- <select class="select form-control dsrent_pickoff_loc"> -->
<option value=""><?php echo esc_html__('Select Location', 'bookingcore'); ?></option>
<?php
foreach ($locations_data as $dlocation) {
if (isset($dlocation['dlocation'])) {
$dlocation_value = $dlocation['dlocation'];
$dlocation_name = get_term($dlocation_value)->name;
?>
<option value="<?php echo esc_attr($dlocation_value); ?>">
<?php echo esc_html($dlocation_name); ?></option>
<?php
}
} ?>
</select>
</p>
<div id="dropoff_loc_error" class="text-danger"></div>
<?php
} else { ?>
<p>
<select class="select form-control wp-input">
<option value="<?php echo esc_attr("No Location Found"); ?>">
<?php echo esc_html__('No Location Found', 'bookingcore'); ?></option>
</select>
</p>
<?php
} ?>
<div id="dsrent_pickoff_loc_error">
</div>
</div>
<div class="dreamsrent_extra_service">
<?php $dsrent_extra_services = maybe_unserialize(get_post_meta( $vehicle_id, 'dreams_booking_meta_extra_services', true ));
if ( !empty($dsrent_extra_services ) ){ ?>
<label style="margin-top: 0;"><?php esc_html_e('Extra Service', 'bookingcore'); ?></label>
<div class="dsrent_resource">
<?php foreach ($dsrent_extra_services as $dsrent_resource_name_dis) { ?>
<?php if (isset($dsrent_resource_name_dis['dreams_booking_meta_es_uid']) && $dsrent_resource_name_dis['dreams_booking_meta_es_uid'] != '') { ?>
<p> <label class="custom_check">
<input type="checkbox" name="selected_extra_services[]"
value="<?php echo esc_attr($dsrent_resource_name_dis['dreams_booking_meta_es_uid']); ?>"
data-price="<?php echo esc_attr($dsrent_resource_name_dis['dreams_booking_meta_es_price']); ?>" />
<?php echo esc_attr($dsrent_resource_name_dis['dreams_booking_meta_es_name']); ?> -
<?php echo get_woocommerce_currency_symbol();?><?php echo $dsrent_resource_name_dis['dreams_booking_meta_es_price']; ?>
<span class="checkmark"> </span>
</label> </p>
<?php }
} ?>
</div>
<?php } ?>
</div>
</div>
<?php
$deposit_enable = get_post_meta ( $vehicle_id, 'dreams_booking_meta_enable_deposit', true );
if ( $deposit_enable === 'yes') {
?>
<div class=" modalnew">
<div class="booking-info pay-amount">
<label><?php echo esc_html__('Deposit Option 1', 'bookingcore' ) ?></label>
<div class="dsrent-deposit">
<div class="title-deposite">
<span class=""></span>
<?php
$deposit_type_deposit = get_post_meta ( $vehicle_id, 'dreams_booking_meta_type_deposit', true );
$value_deposit = get_post_meta ( $vehicle_id, 'dreams_booking_meta_amount_deposit', true );
if ($deposit_type_deposit === 'percent') {
?>
<span class=""><?php echo esc_html($value_deposit) ?>%</span>
<?php
} elseif ($deposit_type_deposit === 'value') {
?>
<span class=""><?php echo wc_price($value_deposit) ?></span>
<?php
}
?>
<span class=""><?php echo esc_html__('Per item', 'bookingcore' ) ?></span>
</div>
<div class="dsrent-type-deposit">
<div class="radio radio-btn">
<label class="dsrent-pay-deposit"
for="dsrent-pay-deposit"><?php echo esc_html__('Pay Deposit', 'bookingcore' ) ?>
<input type="radio" id="dsrent-pay-deposit" class="dsrent-pay-deposit" name="dsr_type_deposit"
value="deposit" checked onclick="handlePaymentType('deposit')" /></label>
</div>
<div class="radio">
<label class="dsrent-pay-full" for="dsrent-pay-full"><?php echo esc_html__('Full Amount', 'bookingcore' ) ?>
<input type="radio" id="dsrent-pay-full" class="dsrent-pay-full" name="dsr_type_deposit"
value="full" onclick="handlePaymentType('full')" /></label>
</div>
</div>
</div>
</div>
</div><?php
}
$output = ob_get_clean();
wp_send_json_success($output);
}
add_action('wp_ajax_get_vehicle_meta', 'dreams_get_vehicle_meta');
function check_availability($booking_product_id, $time_option_default, $start_datetime, $end_datetime) {
global $wpdb;
$table_name = $wpdb->prefix . 'dreamsrent_booking';
// Parse the start and end dates
$start_date_obj = DateTime::createFromFormat('Y-m-d H:i', $start_datetime);
$end_date_obj = DateTime::createFromFormat('Y-m-d H:i', $end_datetime);
// Check if parsing was successful
// if (!$start_date_obj || !$end_date_obj) {
// error_log("Failed to parse start or end datetime. Check the format.");
// wp_send_json_error(__('Invalid date format.', 'bookingcore'));
// return false;
// }
// Format the dates and times
$start_date = $start_date_obj->format('Y-m-d');
$end_date = $end_date_obj->format('Y-m-d');
$start_time = $start_date_obj->format('H:i');
$end_time = $end_date_obj->format('H:i');
$query = $wpdb->prepare(
"SELECT * FROM $table_name WHERE product_id = %d AND status != %s",
$booking_product_id, 'cancel'
);
$availability_query = $wpdb->get_results($query);
$all_dates_passed = true;
if ($availability_query) {
foreach ($availability_query as $result) {
$pickup_date = date('Y-m-d', strtotime($result->pickup_date));
$pickup_time = date('H:i', strtotime($result->pickup_date));
$dropoff_date = date('Y-m-d', strtotime($result->dropoff_date));
$dropoff_time = date('H:i', strtotime($result->dropoff_date));
if ($time_option_default == 'day') {
if (($start_date <= $dropoff_date && $end_date >= $pickup_date)) {
$all_dates_passed = false;
break;
}
} else if ($time_option_default == 'hour') {
if (($start_date <= $dropoff_date && $end_date >= $pickup_date)) {
if ($start_date == $dropoff_date && $start_time > $dropoff_time ) {
$all_dates_passed = true;
} else if ($end_date == $pickup_date && $end_time < $pickup_time) {
$all_dates_passed = true;
} else {
$all_dates_passed = false;
}
}
}
}
}
// Debugging output
// if ($all_dates_passed) {
// error_log("Availability check passed.");
// } else {
// error_log("Availability check failed.");
// }
return $all_dates_passed;
}
// Hook into before a post (order) is permanently deleted
add_action('before_delete_post', 'dreamsrent_delete_booking_on_order_permanent_delete');
function dreamsrent_delete_booking_on_order_permanent_delete($post_id) {
global $wpdb;
// Check if the post being deleted is an order
if (get_post_type($post_id) === 'shop_order') {
// Define the table name
$table_name = $wpdb->prefix . 'dreamsrent_booking';
// Delete the record from the custom table
$wpdb->delete($table_name, array('order_id' => $post_id), array('%d'));
}
}
/*custom code 08-05-25*/
function dreams_register_seasonal_pricing_cpt() {
$labels = array(
'name' => 'Seasonal Pricings',
'singular_name' => 'Seasonal Pricing',
'add_new' => 'Add New',
'add_new_item' => 'Add New Seasonal Pricing',
'edit_item' => 'Edit Seasonal Pricing',
'new_item' => 'New Seasonal Pricing',
'view_item' => 'View Seasonal Pricing',
'search_items' => 'Search Seasonal Pricings',
'not_found' => 'No Seasonal Pricings found',
'menu_name' => 'Seasonal Pricings',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=rental', // 👈 attaches it under "Car Rental"
'menu_position' => null,
'menu_icon' => 'dashicons-calendar-alt',
'supports' => array('title'),
'has_archive' => false,
);
register_post_type('seasonal_pricing', $args);
}
add_action('init', 'dreams_register_seasonal_pricing_cpt',0);
function dreams_add_seasonal_pricing_meta_boxes() {
add_meta_box(
'seasonal_pricing_details',
'Seasonal Pricing Details',
'dreams_render_seasonal_pricing_fields',
'seasonal_pricing',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'dreams_add_seasonal_pricing_meta_boxes');
function dreams_render_seasonal_pricing_fields($post) {
$start_date = get_post_meta($post->ID, 'seasonal_pricing_start_date', true);
$end_date = get_post_meta($post->ID, 'seasonal_pricing_end_date', true);
$status = get_post_meta($post->ID, 'seasonal_pricing_status', true);
$seasonal_daily_rate = get_post_meta($post->ID, 'seasonal_daily_rate', true);
$seasonal_weekly_rate = get_post_meta($post->ID, 'seasonal_weekly_rate', true);
$seasonal_monthly_rate = get_post_meta($post->ID, 'seasonal_monthly_rate', true);
$seasonal_late_fee = get_post_meta($post->ID, 'seasonal_late_fee', true);
wp_nonce_field(basename(__FILE__), 'seasonal_pricing_nonce');
?>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="seasonal_pricing_start_date"><?php echo esc_html__('Start Date','bookingcore'); ?>:</label><br/>
<input type="text" id="seasonal_pricing_start_date" name="seasonal_pricing_start_date" value="<?php echo esc_attr($start_date); ?>" class="datepicker" />
</p>
</div>
<div class="dreams-col">
<p>
<label for="seasonal_pricing_end_date"><?php echo esc_html__('End Date','bookingcore'); ?>:</label><br/>
<input type="text" id="seasonal_pricing_end_date" name="seasonal_pricing_end_date" value="<?php echo esc_attr($end_date); ?>" class="datepicker" />
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="seasonal_daily_rate"><?php echo esc_html__('Daily Rate','bookingcore'); ?>:</label><br/>
<input type="text" id="seasonal_daily_rate" name="seasonal_daily_rate" value="<?php echo esc_attr($seasonal_daily_rate); ?>" />
</p>
</div>
<div class="dreams-col">
<p>
<label for="seasonal_weekly_rate"><?php echo esc_html__('Weekly Rate','bookingcore'); ?>Weekly Rate:</label><br/>
<input type="text" id="seasonal_weekly_rate" name="seasonal_weekly_rate" value="<?php echo esc_attr($seasonal_weekly_rate); ?>" />
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="seasonal_monthly_rate"><?php echo esc_html__('Monthly Rate','bookingcore'); ?>:</label><br/>
<input type="text" id="seasonal_monthly_rate" name="seasonal_monthly_rate" value="<?php echo esc_attr($seasonal_monthly_rate); ?>" />
</p>
</div>
<div class="dreams-col">
<p>
<label for="seasonal_late_fee"><?php echo esc_html__('Late Fees','bookingcore'); ?>:</label><br/>
<input type="text" id="seasonal_late_fee" name="seasonal_late_fee" value="<?php echo esc_attr($seasonal_late_fee); ?>" />
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="seasonal_pricing_status"><?php echo esc_html__('Status','bookingcore'); ?>:</label><br/>
<select name="seasonal_pricing_status" id="seasonal_pricing_status">
<option value="active" <?php selected($status, 'active'); ?>><?php echo esc_html__('Active','bookingcore'); ?></option>
<option value="inactive" <?php selected($status, 'inactive'); ?>><?php echo esc_html__('Inactive','bookingcore'); ?></option>
</select>
</p>
</div>
</div>
<?php
}
// Filter the query for the posts (list) and the count (found_posts)
add_action('pre_get_posts', function($query) {
if (
is_admin() &&
$query->is_main_query() &&
$query->get('post_type') === 'seasonal_pricing'
) {
// Modify the query to include the meta query for posts
$meta_query = array(
array(
'key' => '_author_role',
'value' => 'administrator',
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
}
});
// Modify the SQL to properly adjust for the count query
add_filter('posts_clauses', function($clauses, $wp_query) {
// Only apply the filter to the query for seasonal_pricing posts
if ($wp_query->get('post_type') === 'seasonal_pricing') {
global $wpdb;
// Modify the FROM and WHERE clauses to include the _author_role meta filter
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS pm ON {$wpdb->posts}.ID = pm.post_id AND pm.meta_key = '_author_role' ";
$clauses['where'] .= " AND pm.meta_value = 'administrator' ";
}
return $clauses;
}, 10, 2);
add_filter('views_edit-seasonal_pricing', 'filter_seasonal_pricing_counts_by_author_role');
function filter_seasonal_pricing_counts_by_author_role($views) {
global $wpdb;
$role = 'administrator';
// Get count of published posts where _author_role = administrator
$count = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(p.ID)
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = %s
AND p.post_status = 'publish'
AND pm.meta_key = '_author_role'
AND pm.meta_value = %s
", 'seasonal_pricing', $role));
// Replace the count in the 'All' view (this part may vary if you want to also adjust 'Published', 'Draft', etc.)
if (isset($views['all'])) {
$views['all'] = preg_replace('/\(\d+\)/', '(' . intval($count) . ')', $views['all']);
}
if (isset($views['publish'])) {
$views['publish'] = preg_replace('/\(\d+\)/', '(' . intval($count) . ')', $views['publish']);
}
return $views;
}
// Handle saving custom metadata when saving a post
function dreams_save_seasonal_pricing_meta($post_id) {
// Nonce check for security
if (!isset($_POST['seasonal_pricing_nonce']) || !wp_verify_nonce($_POST['seasonal_pricing_nonce'], basename(__FILE__))) {
return;
}
// Auto save prevention
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// Check the post type and user permissions
if ('seasonal_pricing' !== $_POST['post_type'] || !current_user_can('edit_post', $post_id)) return;
// Get the post object
$post = get_post($post_id);
if (!$post) return;
// Get the post author and their roles
$author_id = $post->post_author;
$user = get_userdata($author_id);
// If the user is valid, update the postmeta for the author role
if ($user && !empty($user->roles)) {
update_post_meta($post_id, '_author_role', $user->roles[0]);
}
// Update other custom post meta fields
if (isset($_POST['seasonal_pricing_start_date'])) {
$formatted_sdate = date('d-m-Y', strtotime($_POST['seasonal_pricing_start_date']));
update_post_meta($post_id, 'seasonal_pricing_start_date', sanitize_text_field($formatted_sdate));
}
if (isset($_POST['seasonal_pricing_end_date'])) {
$formatted_sdate = date('d-m-Y', strtotime($_POST['seasonal_pricing_end_date']));
update_post_meta($post_id, 'seasonal_pricing_end_date', sanitize_text_field($formatted_sdate));
}
if (isset($_POST['seasonal_daily_rate'])) {
update_post_meta($post_id, 'seasonal_daily_rate', sanitize_text_field($_POST['seasonal_daily_rate']));
}
if (isset($_POST['seasonal_weekly_rate'])) {
update_post_meta($post_id, 'seasonal_weekly_rate', sanitize_text_field($_POST['seasonal_weekly_rate']));
}
if (isset($_POST['seasonal_monthly_rate'])) {
update_post_meta($post_id, 'seasonal_monthly_rate', sanitize_text_field($_POST['seasonal_monthly_rate']));
}
if (isset($_POST['seasonal_late_fee'])) {
update_post_meta($post_id, 'seasonal_late_fee', sanitize_text_field($_POST['seasonal_late_fee']));
}
if (isset($_POST['seasonal_pricing_status'])) {
update_post_meta($post_id, 'seasonal_pricing_status', sanitize_text_field($_POST['seasonal_pricing_status']));
}
}
add_action('save_post', 'dreams_save_seasonal_pricing_meta');
/*Insurance CPT */
function dreams_register_insurance_cpt() {
$labels = array(
'name' => 'Insurances',
'singular_name' => 'Insurance',
'add_new' => 'Add New',
'add_new_item' => 'Add New Insurance',
'edit_item' => 'Edit Insurance',
'new_item' => 'New Insurance',
'view_item' => 'View Insurance',
'search_items' => 'Search Insurances',
'not_found' => 'No Insurances found',
'menu_name' => 'Insurances',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=rental', // Attaches it under "Car Rental"
'menu_position' => null,
'menu_icon' => 'dashicons-shield-alt',
'supports' => array('title'),
'has_archive' => false,
);
register_post_type('insurance', $args);
}
add_action('init', 'dreams_register_insurance_cpt', 0);
function dreams_add_insurance_meta_boxes() {
add_meta_box(
'insurance_details',
'Insurance Details',
'dreams_render_insurance_fields',
'insurance',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'dreams_add_insurance_meta_boxes');
function dreams_render_insurance_fields($post) {
$insurance_name = get_post_meta($post->ID, 'insurance_name', true);
$price_type = get_post_meta($post->ID, 'insurance_price_type', true);
$insurance_price = get_post_meta($post->ID, 'insurance_price', true);
$insurance_percentages = get_post_meta($post->ID, 'insurance_percentages', true);
if (!is_array($insurance_percentages)) {
$insurance_percentages = [];
}
wp_nonce_field(basename(__FILE__), 'insurance_fields_nonce');
?>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="insurance_name"><?php esc_html_e('Insurance Name', 'bookingcore'); ?>:</label><br/>
<input type="text" name="insurance_name" id="insurance_name" value="<?php echo esc_attr($insurance_name); ?>" />
</p>
</div>
<div class="dreams-col">
<p>
<label><?php esc_html_e('Price Type', 'bookingcore'); ?>:</label><br/>
<label class="inline-radio"><input type="radio" name="insurance_price_type" value="daily" <?php checked($price_type, 'daily'); ?> /> <?php esc_html_e('Daily', 'bookingcore'); ?></label>
<label class="inline-radio"><input type="radio" name="insurance_price_type" value="fixed" <?php checked($price_type, 'fixed'); ?> /> <?php esc_html_e('Fixed', 'bookingcore'); ?></label>
<!-- <label class="inline-radio"><input type="radio" name="insurance_price_type" value="percentage" <?php checked($price_type, 'percentage'); ?> /> <?php esc_html_e('Percentage', 'bookingcore'); ?></label> -->
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="insurance_price"><?php esc_html_e('Price', 'bookingcore'); ?>:</label><br/>
<input type="text" name="insurance_price" id="insurance_price" value="<?php echo esc_attr($insurance_price); ?>" />
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label><?php esc_html_e('Benefit ', 'bookingcore'); ?>:</label>
<div id="percentage-wrapper">
<?php if (!empty($insurance_percentages)) : ?>
<?php foreach ($insurance_percentages as $percent) : ?>
<div class="percentage-field">
<input type="text" name="benefit[]" value="<?php echo esc_attr($percent); ?>" />
<span class="remove-field"><i class="dashicons-before dashicons-remove"></i></span>
</div>
<?php endforeach; ?>
<?php else : ?>
<div class="percentage-field">
<input type="text" name="benefit[]" value="" />
<span class="remove-field"><i class="dashicons-before dashicons-remove"></i></span>
</div>
<?php endif; ?>
</div>
<button type="button" id="add-percentage" class="button button-primary button-large"><?php esc_html_e('Add Percentage', 'bookingcore'); ?></button>
</p>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const wrapper = document.getElementById('percentage-wrapper');
const addBtn = document.getElementById('add-percentage');
addBtn.addEventListener('click', function() {
const div = document.createElement('div');
div.className = 'percentage-field';
div.innerHTML = '<input type="text" name="benefit[]" value="" /> <span class="remove-field"> <i class="dashicons-before dashicons-remove"></i></span>';
wrapper.appendChild(div);
});
wrapper.addEventListener('click', function(e) {
let target = e.target;
if (target.classList.contains('remove-field') || target.closest('.remove-field')) {
const field = target.closest('.percentage-field');
if (field) field.remove();
}
});
});
</script>
<?php
}
// Filter the query for the posts (list) and the count (found_posts)
add_action('pre_get_posts', function($query) {
if (
is_admin() &&
$query->is_main_query() &&
$query->get('post_type') === 'insurance' // Replace 'insurance' with your actual post type
) {
// Modify the query to include the meta query for _author_role
$meta_query = array(
array(
'key' => '_author_role', // Meta field key for author role
'value' => 'administrator', // Role to filter by
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
}
});
// Modify the SQL to properly adjust for the count query
add_filter('posts_clauses', function($clauses, $wp_query) {
// Only apply the filter to the query for 'insurance' posts
if ($wp_query->get('post_type') === 'insurance') { // Replace 'insurance' with your actual post type
global $wpdb;
// Modify the FROM and WHERE clauses to include the _author_role filter
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS pm ON {$wpdb->posts}.ID = pm.post_id AND pm.meta_key = '_author_role' ";
$clauses['where'] .= " AND pm.meta_value = 'administrator' "; // Adjust the role as needed
}
return $clauses;
}, 10, 2);
add_filter('views_edit-insurance', 'filter_insurance_counts_by_author_role');
function filter_insurance_counts_by_author_role($views) {
global $wpdb;
$role = 'administrator'; // The role you want to filter by
// Get the count of published posts for the 'insurance' post type where _author_role is 'administrator'
$count = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(p.ID)
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = %s
AND p.post_status = 'publish'
AND pm.meta_key = '_author_role'
AND pm.meta_value = %s
", 'insurance', $role));
// Replace the count in the 'All' view (this part may vary if you want to also adjust 'Published', 'Draft', etc.)
if (isset($views['all'])) {
$views['all'] = preg_replace('/\(\d+\)/', '(' . intval($count) . ')', $views['all']);
}
// You can also adjust the 'Published' count similarly
if (isset($views['publish'])) {
$views['publish'] = preg_replace('/\(\d+\)/', '(' . intval($count) . ')', $views['publish']);
}
return $views;
}
// Handle saving custom metadata when saving a post
function dreams_save_insurance_meta($post_id) {
// Nonce check for security
if (!isset($_POST['insurance_fields_nonce']) || !wp_verify_nonce($_POST['insurance_fields_nonce'], basename(__FILE__))) {
return;
}
// Auto save prevention
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// Check the post type and user permissions
if ('insurance' !== $_POST['post_type'] || !current_user_can('edit_post', $post_id)) return;
// Get the post object
$post = get_post($post_id);
if (!$post) return;
// Get the post author and their roles
$author_id = $post->post_author;
$user = get_userdata($author_id);
// If the user is valid, update the postmeta for the author role
if ($user && !empty($user->roles)) {
update_post_meta($post_id, '_author_role', $user->roles[0]);
}
// Update the custom post meta fields
if (isset($_POST['insurance_name'])) {
update_post_meta($post_id, 'insurance_name', sanitize_text_field($_POST['insurance_name']));
}
if (isset($_POST['insurance_price_type'])) {
update_post_meta($post_id, 'insurance_price_type', sanitize_text_field($_POST['insurance_price_type']));
}
if (isset($_POST['insurance_price'])) {
update_post_meta($post_id, 'insurance_price', sanitize_text_field($_POST['insurance_price']));
}
if (isset($_POST['benefit']) && is_array($_POST['benefit'])) {
// Save the percentages as an array
update_post_meta($post_id, 'insurance_benefit', array_map('sanitize_text_field', $_POST['benefit']));
}
}
add_action('save_post', 'dreams_save_insurance_meta');
/*extra_service CPT */
function dreams_register_extra_service_cpt() {
$labels = array(
'name' => 'Extra Services',
'singular_name' => 'Extra Service',
'add_new' => 'Add New',
'add_new_item' => 'Add New Extra Service',
'edit_item' => 'Edit Extra Service',
'new_item' => 'New Extra Service',
'view_item' => 'View Extra Service',
'search_items' => 'Search Extra Services',
'not_found' => 'No Extra Services found',
'menu_name' => 'Extra Services',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=rental', // Attaches it under "Car Rental"
'menu_position' => null,
'menu_icon' => 'dashicons-cart', // Or any other suitable icon
'supports' => array('title'),
'has_archive' => false,
);
register_post_type('extra_service', $args);
}
add_action('init', 'dreams_register_extra_service_cpt', 0);
function dreams_add_extra_service_meta_boxes() {
add_meta_box(
'extra_service_details',
'Extra Service Details',
'dreams_render_extra_service_fields',
'extra_service',
'normal',
'default'
);
}
add_action('add_meta_boxes', 'dreams_add_extra_service_meta_boxes');
function dreams_render_extra_service_fields($post) {
$service_name = get_post_meta($post->ID, 'service_name', true);
$quantity = get_post_meta($post->ID, 'service_quantity', true);
$price = get_post_meta($post->ID, 'service_price', true);
$price_type = get_post_meta($post->ID, 'service_price_type', true);
$description = get_post_meta($post->ID, 'service_description', true);
wp_nonce_field(basename(__FILE__), 'extra_service_fields_nonce');
?>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="service_quantity"><?php esc_html_e('Quantity', 'bookingcore'); ?>:</label><br/>
<input type="text" name="service_quantity" id="service_quantity" value="<?php echo esc_attr($quantity); ?>" />
</p>
</div>
<div class="dreams-col">
<p>
<label for="service_price"><?php esc_html_e('Price', 'bookingcore'); ?>:</label><br/>
<input type="text" name="service_price" id="service_price" value="<?php echo esc_attr($price); ?>" />
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label><?php esc_html_e('Price Type', 'bookingcore'); ?>:</label><br/>
<select name="service_price_type" id="service_price_type">
<option value="one_time" <?php selected($price_type, 'one_time'); ?>><?php esc_html_e('One Time', 'bookingcore'); ?></option>
<option value="per_day" <?php selected($price_type, 'per_day'); ?>><?php esc_html_e('Per Day', 'bookingcore'); ?></option>
</select>
</p>
</div>
</div>
<div class="dreams-row">
<div class="dreams-col">
<p>
<label for="service_description"><?php esc_html_e('Description', 'bookingcore'); ?>:</label><br/>
<textarea name="service_description" id="service_description"><?php echo esc_textarea($description); ?></textarea>
</p>
</div>
</div>
<?php
}
// Filter the query for the posts (list) and the count (found_posts)
add_action('pre_get_posts', function($query) {
if (
is_admin() &&
$query->is_main_query() &&
$query->get('post_type') === 'extra_service' // Replace 'extra_service' with your actual post type
) {
// Modify the query to include the meta query for _author_role
$meta_query = array(
array(
'key' => '_author_role', // Meta field key for author role
'value' => 'administrator', // Role to filter by
'compare' => '='
)
);
$query->set('meta_query', $meta_query);
}
});
// Modify the SQL to properly adjust for the count query
add_filter('posts_clauses', function($clauses, $wp_query) {
// Only apply the filter to the query for 'extra_service' posts
if ($wp_query->get('post_type') === 'extra_service') { // Replace 'extra_service' with your actual post type
global $wpdb;
// Modify the FROM and WHERE clauses to include the _author_role filter
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS pm ON {$wpdb->posts}.ID = pm.post_id AND pm.meta_key = '_author_role' ";
$clauses['where'] .= " AND pm.meta_value = 'administrator' "; // Adjust the role as needed
}
return $clauses;
}, 10, 2);
add_filter('views_edit-extra_service', 'filter_extra_service_counts_by_author_role');
function filter_extra_service_counts_by_author_role($views) {
global $wpdb;
$role = 'administrator'; // The role you want to filter by
// Get the count of published posts for the 'extra_service' post type where _author_role is 'administrator'
$count = $wpdb->get_var($wpdb->prepare("
SELECT COUNT(p.ID)
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id
WHERE p.post_type = %s
AND p.post_status = 'publish'
AND pm.meta_key = '_author_role'
AND pm.meta_value = %s
", 'extra_service', $role));
// Replace the count in the 'All' view (this part may vary if you want to also adjust 'Published', 'Draft', etc.)
if (isset($views['all'])) {
$views['all'] = preg_replace('/\(\d+\)/', '(' . intval($count) . ')', $views['all']);
}
// You can also adjust the 'Published' count similarly
if (isset($views['publish'])) {
$views['publish'] = preg_replace('/\(\d+\)/', '(' . intval($count) . ')', $views['publish']);
}
return $views;
}
function dreams_save_extra_service_meta($post_id) {
// Nonce check for security
if (!isset($_POST['extra_service_fields_nonce']) || !wp_verify_nonce($_POST['extra_service_fields_nonce'], basename(__FILE__))) {
return;
}
// Auto save prevention
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// Check the post type and user permissions
if ('extra_service' !== $_POST['post_type'] || !current_user_can('edit_post', $post_id)) return;
// Get the post object
$post = get_post($post_id);
if (!$post) return;
// Get the post author and their roles
$author_id = $post->post_author;
$user = get_userdata($author_id);
// If the user is valid, update the postmeta for the author role
if ($user && !empty($user->roles)) {
update_post_meta($post_id, '_author_role', $user->roles[0]);
}
if (isset($_POST['service_quantity'])) {
update_post_meta($post_id, 'service_quantity', sanitize_text_field($_POST['service_quantity']));
}
if (isset($_POST['service_price'])) {
update_post_meta($post_id, 'service_price', sanitize_text_field($_POST['service_price']));
}
if (isset($_POST['service_price_type'])) {
update_post_meta($post_id, 'service_price_type', sanitize_text_field($_POST['service_price_type']));
}
if (isset($_POST['service_description'])) {
update_post_meta($post_id, 'service_description', sanitize_textarea_field($_POST['service_description']));
}
}
add_action('save_post', 'dreams_save_extra_service_meta');
if ( ! defined( 'DREMSRENT_URI' ) ) {
define('DREMSRENT_URI', get_template_directory_uri());
}
// Enqueue Tabler Icons CSS and custom styles
function enqueue_tabler_icons() {
wp_enqueue_style('dreamsrent_vendor-tabler-icons', DREMSRENT_URI . '/assets/plugins/tabler-icons/tabler-icons.min.css', array(), null);
wp_enqueue_script('ti-icon-selector-script', DSBRENT_PLUGIN_URI.'js/ti-icon-script.js', array('jquery'),null,true);
}
add_action('admin_enqueue_scripts', 'enqueue_tabler_icons');
// Add meta box to CPT
function add_icon_meta_box() {
add_meta_box(
'icon_meta_box',
'Select Tabler Icon',
'render_icon_meta_box',
'extra_service',
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_icon_meta_box');
// Render meta box with modal trigger
function render_icon_meta_box($post) {
wp_nonce_field('icon_meta_box_nonce', 'icon_meta_box_nonce');
$saved_icon = get_post_meta($post->ID, '_tabler_icon', true);
echo '<div id="selected-icon-preview">';
if ($saved_icon) {
echo '<i class="' . esc_attr($saved_icon) . '" style="font-size: 24px;"></i>';
} else {
echo '<em>No icon selected</em>';
}
echo '</div>';
echo '<input type="hidden" id="tabler_icon_input" name="tabler_icon" value="' . esc_attr($saved_icon) . '" />';
echo '<button type="button" class="button" id="open-icon-modal">Choose Icon</button>';
// Modal HTML
echo '<div id="ti-icon-modal" class="ti-icon-modal">
<div class="ti-icon-modal-content">
<span class="ti-icon-close">×</span>
<div class="ti-icon-grid">';
$icons = [
'ti ti-gps' => 'GPS',
'ti ti-wifi-2' => 'WiFi',
'ti ti-baby-carriage' => 'Carriage',
'ti ti-user-star' => 'User Star',
'ti ti-satellite' => 'Satellite',
'ti ti-usb' => 'USB',
'ti ti-checkup-list' => 'Checkup List',
'ti ti-tallymark-2' => 'Tallymark2',
'ti ti-file-pencil' => 'File Pencil',
'ti ti-camera' => 'Camera',
'ti ti-activity' => 'Activity',
'ti ti-air-balloon' => 'Air Balloon',
'ti ti-alarm' => 'Alarm',
'ti ti-anchor' => 'Anchor',
'ti ti-archive' => 'Archive',
'ti ti-at' => 'At',
'ti ti-award' => 'Award',
'ti ti-bell' => 'Bell',
'ti ti-book' => 'Book',
'ti ti-box' => 'Box',
'ti ti-briefcase' => 'Briefcase',
'ti ti-brush' => 'Brush',
'ti ti-bug' => 'Bug',
'ti ti-building' => 'Building',
'ti ti-calendar' => 'Calendar',
'ti ti-camera' => 'Camera',
'ti ti-car' => 'Car',
'ti ti-check' => 'Check',
'ti ti-cloud' => 'Cloud',
'ti ti-code' => 'Code',
'ti ti-credit-card' => 'Credit Card',
'ti ti-crown' => 'Crown',
'ti ti-device-laptop' => 'Laptop',
'ti ti-device-mobile' => 'Mobile',
'ti ti-device-tablet' => 'Tablet',
'ti ti-download' => 'Download',
'ti ti-edit' => 'Edit',
'ti ti-eye' => 'Eye',
'ti ti-file' => 'File',
'ti ti-flag' => 'Flag',
'ti ti-flame' => 'Flame',
'ti ti-folder' => 'Folder',
'ti ti-gift' => 'Gift',
'ti ti-globe' => 'Globe',
'ti ti-heart' => 'Heart',
'ti ti-help' => 'Help',
'ti ti-home' => 'Home',
'ti ti-info-circle' => 'Info Circle',
'ti ti-key' => 'Key',
'ti ti-link' => 'Link',
'ti ti-lock' => 'Lock',
'ti ti-mail' => 'Mail',
'ti ti-map' => 'Map',
'ti ti-menu' => 'Menu',
'ti ti-message-circle' => 'Message Circle',
'ti ti-minus' => 'Minus',
'ti ti-moon' => 'Moon',
'ti ti-music' => 'Music',
'ti ti-phone' => 'Phone',
'ti ti-plus' => 'Plus',
'ti ti-power' => 'Power',
'ti ti-refresh' => 'Refresh',
'ti ti-search' => 'Search',
'ti ti-settings' => 'Settings',
'ti ti-share' => 'Share',
'ti ti-shield' => 'Shield',
'ti ti-star' => 'Star',
'ti ti-sun' => 'Sun',
'ti ti-tag' => 'Tag',
'ti ti-trash' => 'Trash',
'ti ti-upload' => 'Upload',
'ti ti-user' => 'User',
'ti ti-video' => 'Video',
'ti ti-volume' => 'Volume',
];
foreach ($icons as $class => $label) {
echo '<div class="ti-icon-option" data-icon="' . esc_attr($class) . '" title="' . esc_attr($label) . '">';
echo '<i class="' . esc_attr($class) . '"></i>';
echo '</div>';
}
echo '</div></div></div>';
}
// Save the icon on post save
function save_icon_meta_box($post_id) {
if (!isset($_POST['icon_meta_box_nonce']) || !wp_verify_nonce($_POST['icon_meta_box_nonce'], 'icon_meta_box_nonce')) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['tabler_icon'])) {
update_post_meta($post_id, '_tabler_icon', sanitize_text_field($_POST['tabler_icon']));
}
}
add_action('save_post', 'save_icon_meta_box');
// Register 'location' taxonomy for 'rental'
function dreams_register_location_taxonomy() {
$labels = array(
'name' => 'Locations',
'singular_name' => 'Location',
'search_items' => 'Search Locations',
'all_items' => 'All Locations',
'edit_item' => 'Edit Location',
'update_item' => 'Update Location',
'add_new_item' => 'Add New Location',
'new_item_name' => 'New Location Name',
'menu_name' => 'Locations',
);
register_taxonomy('location', ['rental'], array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'rewrite' => array('slug' => 'location'),
));
}
add_action('init', 'dreams_register_location_taxonomy');
function dreams_register_customers_cpt() {
$labels = array(
'name' => 'Customers',
'singular_name' => 'Customer',
'add_new' => 'Add New',
'add_new_item' => 'Add New Customer',
'edit_item' => 'Edit Customer',
'new_item' => 'New Customer',
'view_item' => 'View Customer',
'search_items' => 'Search Customers',
'not_found' => 'No Customers found',
'menu_name' => 'Customers',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=rental', // 👈 Show under Car Rental
'menu_position' => null,
'menu_icon' => 'dashicons-id-alt',
'supports' => array('title'),
'has_archive' => false,
);
register_post_type('customers', $args);
}
add_action('init', 'dreams_register_customers_cpt', 0);
function dreams_register_drivers_cpt() {
$labels = array(
'name' => 'Drivers',
'singular_name' => 'Driver',
'add_new' => 'Add New',
'add_new_item' => 'Add New Driver',
'edit_item' => 'Edit Driver',
'new_item' => 'New Driver',
'view_item' => 'View Driver',
'search_items' => 'Search Drivers',
'not_found' => 'No Drivers found',
'menu_name' => 'Drivers',
);
$args = array(
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=rental', // 👈 Appear under Car Rental menu
'menu_position' => null,
'menu_icon' => 'dashicons-id', // Optional icon if shown in its own menu
'supports' => array('title'),
'has_archive' => false,
);
register_post_type('drivers', $args);
}
add_action('init', 'dreams_register_drivers_cpt', 0);