<?php 

// Register custom redirect post type
function custom_redirects_post_type() {
    $labels = array(
        'name'               => _x( 'Redirects', 'post type general name', 'your-text-domain' ),
        'singular_name'      => _x( 'Redirect', 'post type singular name', 'your-text-domain' ),
        'menu_name'          => _x( 'Redirects', 'admin menu', 'your-text-domain' ),
        'name_admin_bar'     => _x( 'Redirect', 'add new on admin bar', 'your-text-domain' ),
        'add_new'            => _x( 'Add New', 'redirect', 'your-text-domain' ),
        'add_new_item'       => __( 'Add New Redirect', 'your-text-domain' ),
        'new_item'           => __( 'New Redirect', 'your-text-domain' ),
        'edit_item'          => __( 'Edit Redirect', 'your-text-domain' ),
        'view_item'          => __( 'View Redirect', 'your-text-domain' ),
        'all_items'          => __( 'All Redirects', 'your-text-domain' ),
        'search_items'       => __( 'Search Redirects', 'your-text-domain' ),
        'parent_item_colon'  => __( 'Parent Redirect:', 'your-text-domain' ),
        'not_found'          => __( 'No redirects found.', 'your-text-domain' ),
        'not_found_in_trash' => __( 'No redirects found in Trash.', 'your-text-domain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-text-domain' ),
        'public'             => false,
        'publicly_queryable' => false,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'redirect' ),
        'capability_type'    => 'post',
        'has_archive'        => false,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor' )
    );

    register_post_type( 'redirect', $args );
}
add_action( 'init', 'custom_redirects_post_type' );

// Add custom meta boxes for old and new URLs
function custom_redirects_meta_boxes() {
    add_meta_box(
        'custom_redirects_old_url',
        __( 'Old URL', 'your-text-domain' ),
        'custom_redirects_old_url_callback',
        'redirect'
    );

    add_meta_box(
        'custom_redirects_new_url',
        __( 'New URL', 'your-text-domain' ),
        'custom_redirects_new_url_callback',
        'redirect'
    );
}
add_action( 'add_meta_boxes', 'custom_redirects_meta_boxes' );

// Callback function to display old URL meta box
function custom_redirects_old_url_callback( $post ) {
    $old_url = get_post_meta( $post->ID, '_old_url', true );
    echo '<input type="text" name="old_url" value="' . esc_attr( $old_url ) . '" size="50" />';
}

// Callback function to display new URL meta box
function custom_redirects_new_url_callback( $post ) {
    $new_url = get_post_meta( $post->ID, '_new_url', true );
    echo '<input type="text" name="new_url" value="' . esc_attr( $new_url ) . '" size="50" />';
}

// Save custom meta box data
function custom_redirects_save_meta_box_data( $post_id ) {
    if ( array_key_exists( 'old_url', $_POST ) ) {
        update_post_meta(
            $post_id,
            '_old_url',
            sanitize_text_field( $_POST['old_url'] )
        );
    }

    if ( array_key_exists( 'new_url', $_POST ) ) {
        update_post_meta(
            $post_id,
            '_new_url',
            sanitize_text_field( $_POST['new_url'] )
        );
    }
}
add_action( 'save_post', 'custom_redirects_save_meta_box_data' );

function custom_redirects() {
    // Check if it's a singular post of type 'redirect'
    if ( is_singular( 'redirect' ) ) {
        global $post;

        // Get the old URL from post meta
        $old_url = get_post_meta( $post->ID, '_old_url', true );

        // Get the new URL from post meta
        $new_url = get_post_meta( $post->ID, '_new_url', true );

        // If both old and new URLs are set, perform the redirect
        if ( $old_url && $new_url && $old_url !== $new_url ) {
            wp_redirect( $new_url, 301 );
            exit;
        }
    }
}
add_action( 'template_redirect', 'custom_redirects' );
