Site icon Hip-Hop Website Design and Development

Wp_insert_term doesn’t work from separate file with custom taxonomy

I have code that works good in main plugin file:

if ( ! class_exists( 'Id_CRM_Contacts_User_Cpt' ) ) {

class Id_CRM_Contacts_User_Cpt {

    function __construct() {
        register_activation_hook( __FILE__, array( $this, 'custom_post_type' ) );
        register_activation_hook( __FILE__, array( $this, 'create_first_contact_status' ) );
        add_action( 'init', array( $this, 'custom_post_type' ) );
    }

    public function custom_post_type() {

            $labels = array(
                'name'              => esc_html_x( 'Statuses', 'taxonomy general name', 'idcrm_contacts' ),
                'singular_name'     => esc_html_x( 'Status', 'taxonomy singular name', 'idcrm_contacts' ),
                'search_items'      => esc_html__( 'Search Statuses', 'idcrm_contacts' ),
                'all_items'         => esc_html__( 'All Statuses', 'idcrm_contacts' ),
                'parent_item'       => esc_html__( 'Parent Status', 'idcrm_contacts' ),
                'parent_item_colon' => esc_html__( 'Parent Status:', 'idcrm_contacts' ),
                'edit_item'         => esc_html__( 'Edit Status', 'idcrm_contacts' ),
                'update_item'       => esc_html__( 'Update Status', 'idcrm_contacts' ),
                'add_new_item'      => esc_html__( 'Add New Status', 'idcrm_contacts' ),
                'new_item_name'     => esc_html__( 'New Status Name', 'idcrm_contacts' ),
                'menu_name'         => esc_html__( 'Statuses', 'idcrm_contacts' ),
            );

            $args = array(
                'hierarchical'      => true,
                'show_ui'           => true,
                'show_admin_column' => true,
                'query_var'         => true,
                'rewrite'           => array(
                    'slug'  => 'contacts/status',
                    'feeds' => false,
                    'feed'  => false,
                ),
                'labels'            => $labels,
                'sort'              => true,
                'capabilities     ' => array(
                    'manage_terms' => 'edit_user_status',
                    'edit_terms'   => 'edit_user_status',
                    'delete_terms' => 'edit_user_status',
                    'assign_terms' => 'edit_user_status',
                ),
            );

            register_taxonomy( 'user_status', 'user_contact', $args );

            
            register_post_type(
                'user_contact',
                array(
                    'public'          => true,
                    'has_archive'     => true,
                    'rewrite'         => array(
                        'slug'  => 'contacts',
                        'feeds' => false,
                        'feed'  => false,
                    ),
                    'label'           => esc_html__( 'Contacts', 'idcrm_contacts' ),
                    'supports'        => array( 'title', 'editor', 'comments', 'revisions', 'author', 'excerpt', 'custom-fields', 'thumbnail' ),
                    'taxonomies'      => array( 'user_status', 'user_source ' ),
                    'show_ui'         => true,
                    'show_in_menu'    => 'idcrm-contacts',
                    'capability_type' => array( 'user_contact', 'user_contacts' ),
                    'map_meta_cap'    => true,
                )
            );
    }
    /** Create first contact status */
    public function create_first_contact_status() {
        $term_check = term_exists( esc_html__( 'Leads', 'idcrm_contacts' ) );

        if ( empty( $term_check ) ) {
            wp_insert_term( esc_html__( 'Leads', 'idcrm_contacts' ), 'user_status' );
        }
     }
   }
 }
 if ( class_exists( 'Id_CRM_Contacts_User_Cpt' ) ) {
    $id_crm_contacts_user_cpt = new Id_CRM_Contacts_User_Cpt();
    $id_crm_contacts_user_cpt->register();
 }

But if I move it to separate file it doesn’t work. I try to fire it from main file like this:

if ( ! class_exists( 'Id_CRM_Contacts_User_Cpt' ) ) {
 require IDCRMCONTACTS_PATH . 'includes/class-id-crm-contacts-user.php';
}

I don’t want to use ‘default_term’ because it creates every time then user change it. And don’t want to use ‘init’ the same. It should creates at plugin activation only. Is it possible to insert term to custom taxonomy from other file?