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

// Register Portfolio Custom Post Type
function aump_register_portfolio_post_type() {
    $labels = array(
        'name'               => esc_html__('Portfolio Items', 'advanced-universal-modern-portfolio-and-gallery'),
        'singular_name'      => esc_html__('Portfolio Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'menu_name'          => esc_html__('Aump Portfolio', 'advanced-universal-modern-portfolio-and-gallery'),
        'name_admin_bar'     => esc_html__('Portfolio Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'add_new'            => esc_html__('Add New Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'add_new_item'       => esc_html__('Add New Portfolio Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'new_item'           => esc_html__('New Portfolio Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'edit_item'          => esc_html__('Edit Portfolio Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'view_item'          => esc_html__('View Portfolio Item', 'advanced-universal-modern-portfolio-and-gallery'),
        'all_items'          => esc_html__('All Portfolio Items', 'advanced-universal-modern-portfolio-and-gallery'),
        'search_items'       => esc_html__('Search Portfolio Items', 'advanced-universal-modern-portfolio-and-gallery'),
        'not_found'          => esc_html__('No portfolio items found.', 'advanced-universal-modern-portfolio-and-gallery'),
        'not_found_in_trash' => esc_html__('No portfolio items found in Trash.', 'advanced-universal-modern-portfolio-and-gallery'),
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array('slug' => 'portfolio-item'),
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => 5,
        'supports'           => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
        'show_in_rest'       => true,
    );

    register_post_type('aump_portfolio', $args);
}
add_action('init', 'aump_register_portfolio_post_type');

// Register Portfolio Categories Taxonomy
function aump_register_portfolio_taxonomy() {
    $labels = array(
        'name'              => esc_html__('Portfolio Categories', 'advanced-universal-modern-portfolio-and-gallery'),
        'singular_name'     => esc_html__('Portfolio Category', 'advanced-universal-modern-portfolio-and-gallery'),
        'search_items'      => esc_html__('Search Portfolio Categories', 'advanced-universal-modern-portfolio-and-gallery'),
        'all_items'         => esc_html__('All Portfolio Categories', 'advanced-universal-modern-portfolio-and-gallery'),
        'parent_item'       => esc_html__('Parent Category', 'advanced-universal-modern-portfolio-and-gallery'),
        'parent_item_colon' => esc_html__('Parent Category:', 'advanced-universal-modern-portfolio-and-gallery'),
        'edit_item'         => esc_html__('Edit Portfolio Category', 'advanced-universal-modern-portfolio-and-gallery'),
        'update_item'       => esc_html__('Update Portfolio Category', 'advanced-universal-modern-portfolio-and-gallery'),
        'add_new_item'      => esc_html__('Add New Portfolio Category', 'advanced-universal-modern-portfolio-and-gallery'),
        'new_item_name'     => esc_html__('New Portfolio Category Name', 'advanced-universal-modern-portfolio-and-gallery'),
        'menu_name'         => esc_html__('Portfolio Categories', 'advanced-universal-modern-portfolio-and-gallery'),
    );

    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'portfolio-category'),
        'show_in_rest'      => true,
    );

    register_taxonomy('aump_portfolio_category', array('aump_portfolio'), $args);
}
add_action('init', 'aump_register_portfolio_taxonomy');

function aump_remove_add_new_submenu() {
    remove_submenu_page('edit.php?post_type=aump_portfolio', 'post-new.php?post_type=aump_portfolio');
}
add_action('admin_menu', 'aump_remove_add_new_submenu', 999);


// Add Duplicate link to action row
function aump_add_duplicate_link($actions, $post) {
    if ($post->post_type === 'aump_portfolio') {
        $url = wp_nonce_url(
    admin_url('admin.php?action=aump_duplicate_portfolio_item&post=' . $post->ID),
    'aump_duplicate_portfolio_item_' . $post->ID,
    'aump_nonce'
);
        $actions['duplicate'] = '<a href="' . esc_url($url) . '" title="' . esc_attr__('Duplicate this item', 'advanced-universal-modern-portfolio-and-gallery') . '">' . esc_html__('Duplicate', 'advanced-universal-modern-portfolio-and-gallery') . '</a>';
    }
    return $actions;
}
add_filter('post_row_actions', 'aump_add_duplicate_link', 10, 2);

// Function to duplicate the portfolio item
function aump_duplicate_portfolio_item() {
if (
    !isset($_GET['post'], $_GET['aump_nonce']) ||
    !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['aump_nonce'])), 'aump_duplicate_portfolio_item_' . absint($_GET['post'])) ||
    !current_user_can('edit_post', absint($_GET['post']))
) {
    wp_die(esc_html__('You do not have permission to duplicate this item.', 'advanced-universal-modern-portfolio-and-gallery'));
}

    $post_id = absint($_GET['post']);
    $post = get_post($post_id);

    if (!$post || $post->post_type !== 'aump_portfolio') {
        wp_die(esc_html__('Invalid portfolio item.', 'advanced-universal-modern-portfolio-and-gallery'));
    }

    $new_post = array(
        'post_title'    => $post->post_title . ' (Copy)',
        'post_content'  => $post->post_content,
        'post_status'   => 'draft',
        'post_type'     => $post->post_type,
        'post_author'   => get_current_user_id(),
    );

    $new_post_id = wp_insert_post($new_post);

    $meta_data = get_post_meta($post_id);
    foreach ($meta_data as $key => $values) {
        foreach ($values as $value) {
            add_post_meta($new_post_id, $key, maybe_unserialize($value));
        }
    }

    $taxonomies = get_object_taxonomies($post->post_type);
    foreach ($taxonomies as $taxonomy) {
        $terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'ids'));
        wp_set_object_terms($new_post_id, $terms, $taxonomy);
    }

    $thumbnail_id = get_post_thumbnail_id($post_id);
    if ($thumbnail_id) {
        set_post_thumbnail($new_post_id, $thumbnail_id);
    }

    wp_redirect(admin_url('post.php?post=' . $new_post_id . '&action=edit'));
    exit;
}
add_action('admin_action_aump_duplicate_portfolio_item', 'aump_duplicate_portfolio_item');