
/**
 * Allows freelancers to access the "My Bought Services" page by replacing
 * the theme's original shortcode with a custom version that has a modified permission check.
 *
 * Add this code using the "Code Snippets" plugin.
 */

// Use the 'wp_loaded' hook to ensure all theme files are loaded first.
add_action( 'wp_loaded', 'replace_my_bought_services_shortcode' );
function replace_my_bought_services_shortcode() {
    // Step 1: Remove the original shortcode from the theme.
    remove_shortcode( 'wp_freeio_my_bought_services' );

    // Step 2: Add our own custom shortcode with the same name.
    add_shortcode( 'wp_freeio_my_bought_services', 'custom_my_bought_services_shortcode' );
}

/**
 * Custom shortcode function to display the "My Bought Services" page.
 */
function custom_my_bought_services_shortcode( $atts ) {
    
    // This is a copy of the theme's function, with a modified permission check.

    if ( ! is_user_logged_in() ) {
        return WP_Freeio_Template_Loader::get_template_part( 'misc/need-login' );
    
    // --- THE FIX: Allow both Employers AND Freelancers to access the page ---
    } elseif ( WP_Freeio_User::is_employer() || WP_Freeio_User::is_freelancer() ) {
        
        if ( ! empty( $_REQUEST['action'] ) ) {
            $action = sanitize_title( $_REQUEST['action'] );
            
            if ( $action == 'view-history' ) {
                return WP_Freeio_Template_Loader::get_template_part( 'misc/my-services-employer-history' );
            }
        }
        return WP_Freeio_Template_Loader::get_template_part( 'misc/my-services-employer' );
    }

    // If the user is neither an employer nor a freelancer, show the "not allowed" message.
    return WP_Freeio_Template_Loader::get_template_part( 'misc/not-allowed' );
}
