HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1014-azure #14~24.04.1-Ubuntu SMP Fri Oct 3 20:52:11 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/dreamstour-wp/wp-content/plugins/dreams-tour/includes/cpt-tour.php
<?php
/**
 * Register Custom Post Type: Tour
 */
function dreamstour_register_cpts() {
    $types = ['tour'];
    foreach ( $types as $type ) {
        register_post_type(
            $type,
            array(
                'labels' => array(
                    'name'          => esc_html__( ucfirst( $type ) . 's', 'dreams-tour' ),
                    'singular_name' => esc_html__( ucfirst( $type ), 'dreams-tour' ),
                ),
                'public'       => true,
                'has_archive'  => true,
                'supports'     => array( 'title', 'editor', 'thumbnail' ),
                'rewrite'      => array( 'slug' => $type ),
                'show_in_rest' => true,
                'capability_type' => 'product',
                'map_meta_cap'    => true,
            )
        );
    }
}
add_action( 'init', 'dreamstour_register_cpts' );

/**
 * Register Taxonomies
 */
function dreamstour_register_taxonomies() {
    $taxonomies = array(
        'tour_category'        => esc_html__( 'Category', 'dreams-tour' ),
        'tour_duration_days'   => esc_html__( 'Duration (Days)', 'dreams-tour' ),
        'tour_duration_nights' => esc_html__( 'Duration (Nights)', 'dreams-tour' ),
        'tour_country'         => esc_html__( 'Country', 'dreams-tour' ),
        'tour_activities'      => esc_html__( 'Activities', 'dreams-tour' ),
        'tour_includes'        => esc_html__( 'Includes', 'dreams-tour' ),
        'tour_excludes'        => esc_html__( 'Excludes', 'dreams-tour' ),
        'tour_why_book'        => esc_html__( 'Why Book With Us', 'dreams-tour' ),
    );

    foreach ( $taxonomies as $taxonomy => $label ) {
        register_taxonomy(
            $taxonomy,
            'tour',
            array(
                'labels'       => array(
                    'name'          => $label,
                    'singular_name' => $label,
                ),
                'public'       => true,
                'hierarchical' => true,
                'show_in_rest' => true,
            )
        );
    }
}
add_action( 'init', 'dreamstour_register_taxonomies' );

/**
 * Add Category Image Field (Add New)
 */
function dreamstour_taxonomy_add_image_field( $taxonomy ) {
    if ( 'tour_category' !== $taxonomy ) {
        return;
    }
    ?>
    <div class="form-field term-group">
        <label for="dreamstour-category-image"><?php esc_html_e( 'Category Image', 'dreams-tour' ); ?></label>
        <input type="hidden" id="dreamstour-category-image-id" name="dreamstour-category-image-id" value="">
        <div id="dreamstour-category-image-wrapper"></div>
        <p>
            <button type="button" class="button dreamstour-image-upload"><?php esc_html_e( 'Add Image', 'dreams-tour' ); ?></button>
            <button type="button" class="button dreamstour-image-remove"><?php esc_html_e( 'Remove Image', 'dreams-tour' ); ?></button>
        </p>
    </div>
    <?php
}
add_action( 'tour_category_add_form_fields', 'dreamstour_taxonomy_add_image_field' );

/**
 * Edit Category Image Field
 */
function dreamstour_taxonomy_edit_image_field( $term, $taxonomy ) {
    if ( 'tour_category' !== $taxonomy ) {
        return;
    }

    $image_id  = get_term_meta( $term->term_id, 'dreamstour-category-image-id', true );
    $image_url = $image_id ? wp_get_attachment_image_url( $image_id, 'thumbnail' ) : '';
    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row"><label for="dreamstour-category-image"><?php esc_html_e( 'Category Image', 'dreams-tour' ); ?></label></th>
        <td>
            <input type="hidden" id="dreamstour-category-image-id" name="dreamstour-category-image-id" value="<?php echo esc_attr( $image_id ); ?>">
            <div id="dreamstour-category-image-wrapper">
                <?php if ( $image_url ) : ?>
                    <img src="<?php echo esc_url( $image_url ); ?>" style="max-width:100px;">
                <?php endif; ?>
            </div>
            <p>
                <button type="button" class="button dreamstour-image-upload"><?php esc_html_e( 'Add Image', 'dreams-tour' ); ?></button>
                <button type="button" class="button dreamstour-image-remove"><?php esc_html_e( 'Remove Image', 'dreams-tour' ); ?></button>
            </p>
        </td>
    </tr>
    <?php
}
add_action( 'tour_category_edit_form_fields', 'dreamstour_taxonomy_edit_image_field', 10, 2 );

/**
 * Save Category Image
 */
function dreamstour_save_category_image( $term_id ) {
    if ( isset( $_POST['dreamstour-category-image-id'] ) && '' !== $_POST['dreamstour-category-image-id'] ) {
        update_term_meta( $term_id, 'dreamstour-category-image-id', absint( $_POST['dreamstour-category-image-id'] ) );
    } else {
        delete_term_meta( $term_id, 'dreamstour-category-image-id' );
    }
}
add_action( 'created_tour_category', 'dreamstour_save_category_image' );
add_action( 'edited_tour_category', 'dreamstour_save_category_image' );

/**
 * Add Icon Field for Why Book With Us (Add New)
 */
function dreamstour_taxonomy_add_icon_field( $taxonomy ) {
    if ( 'tour_why_book' !== $taxonomy ) {
        return;
    }
    ?>
    <div class="form-field term-group">
        <label for="dreamstour-why-book-icon"><?php esc_html_e( 'Icon Class', 'dreams-tour' ); ?></label>
        <input type="text" id="dreamstour-why-book-icon" name="dreamstour-why-book-icon" value="" placeholder="e.g., isax isax-medal-star">
        <p class="description"><?php esc_html_e( 'Enter the icon class (e.g., isax isax-medal-star)', 'dreams-tour' ); ?></p>
    </div>
    <?php
}
add_action( 'tour_why_book_add_form_fields', 'dreamstour_taxonomy_add_icon_field' );

/**
 * Edit Icon Field for Why Book With Us
 */
function dreamstour_taxonomy_edit_icon_field( $term, $taxonomy ) {
    if ( 'tour_why_book' !== $taxonomy ) {
        return;
    }

    $icon_class = get_term_meta( $term->term_id, 'dreamstour-why-book-icon', true );
    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row"><label for="dreamstour-why-book-icon"><?php esc_html_e( 'Icon Class', 'dreams-tour' ); ?></label></th>
        <td>
            <input type="text" id="dreamstour-why-book-icon" name="dreamstour-why-book-icon" value="<?php echo esc_attr( $icon_class ); ?>" placeholder="e.g., isax isax-medal-star">
            <p class="description"><?php esc_html_e( 'Enter the icon class (e.g., isax isax-medal-star)', 'dreams-tour' ); ?></p>
        </td>
    </tr>
    <?php
}
add_action( 'tour_why_book_edit_form_fields', 'dreamstour_taxonomy_edit_icon_field', 10, 2 );

/**
 * Save Icon Field for Why Book With Us
 */
function dreamstour_save_why_book_icon( $term_id ) {
    if ( isset( $_POST['dreamstour-why-book-icon'] ) ) {
        update_term_meta( $term_id, 'dreamstour-why-book-icon', sanitize_text_field( $_POST['dreamstour-why-book-icon'] ) );
    }
}
add_action( 'created_tour_why_book', 'dreamstour_save_why_book_icon' );
add_action( 'edited_tour_why_book', 'dreamstour_save_why_book_icon' );
 

function dreamstour_taxonomy_admin_assets( $hook ) {
    // Only load on add/edit taxonomy pages
    if ( 'edit-tags.php' !== $hook && 'term.php' !== $hook ) {
        return;
    }

    if ( ! isset( $_GET['taxonomy'] ) || 'tour_category' !== $_GET['taxonomy'] ) {
        return;
    }

    wp_enqueue_media();
    wp_enqueue_script(
        'dreamstour-taxonomy-image',
        get_stylesheet_directory_uri() . '/assets/js/admin/dreamstour-taxonomy-image.js', // ✅ child theme safe
        array( 'jquery' ),
        '1.0',
        true
    );

    wp_localize_script(
        'dreamstour-taxonomy-image',
        'dreamstour_taxonomy',
        array(
            'title'  => __( 'Select or Upload Image', 'dreams-tour' ),
            'button' => __( 'Use this image', 'dreams-tour' ),
        )
    );
}
add_action( 'admin_enqueue_scripts', 'dreamstour_taxonomy_admin_assets' );


/**
 * Add Image Column to Tour Category
 */
function dreamstour_tour_category_columns( $columns ) {
    // Insert new column after checkbox
    $new_columns = array();
    foreach ( $columns as $key => $value ) {
        $new_columns[ $key ] = $value;
        if ( 'cb' === $key ) {
            $new_columns['image'] = esc_html__( 'Image', 'dreams-tour' );
        }
    }
    return $new_columns;
}
add_filter( 'manage_edit-tour_category_columns', 'dreamstour_tour_category_columns' );

/**
 * Render Image Column Content
 */
function dreamstour_tour_category_column_content( $content, $column_name, $term_id ) {
    if ( 'image' === $column_name ) {
        $image_id  = get_term_meta( $term_id, 'dreamstour-category-image-id', true );
        $image_url = $image_id ? wp_get_attachment_image_url( $image_id, 'thumbnail' ) : '';

        if ( $image_url ) {
            $content = '<img src="' . esc_url( $image_url ) . '" style="max-width:60px;height:auto;">';
        } else {
            $content = '<span style="color:#aaa;">' . esc_html__( '—', 'dreams-tour' ) . '</span>';
        }
    }
    return $content;
}
add_filter( 'manage_tour_category_custom_column', 'dreamstour_tour_category_column_content', 10, 3 );