Developer documentation for the activation and migration lifecycle of the WPResidence real estate CRM. Key files: wpestate-crm.php, libs/db-setup.php, libs/migration.php, uninstall.php.
Plugin Constants
WPESTATE_CRM_URL WPESTATE_CRM_DIR_URL WPESTATE_CRM_PATH WPESTATE_CRM_BASE WPESTATE_CRM_VERSION = '5.2.2'
Activation Routine
wpestate_crm_functionality() // lines 331-376 of wpestate-crm.php
Runs on register_activation_hook and is also invoked by wpestate_crm_check_plugin_functionality_loaded() on plugins_loaded when the stored DB version differs from WPESTATE_CRM_VERSION.
Steps
- wpestate_crm_create_tables() – create all 9 tables via dbDelta.
- wpestate_crm_seed_defaults() – seed default settings, skipping existing non-empty values.
- wpestate_crm_migrate_cpt_data() – migrate legacy CPT posts, guarded by wpestate_crm_cpt_migrated flag.
- Register manage_crm on the administrator role.
- wpestate_crm_seed_permissions() – seed the role-based matrix.
- wpestate_crm_migrate_hubspot_settings() – copy legacy Redux keys to CRM settings.
- wpestate_crm_seed_default_automations() – insert 27 rules if none exist, all paused.
- Schedule 3 cron events if not already scheduled.
- update_option(‘wpestate_crm_db_version’, WPESTATE_CRM_VERSION).
Cron Events
| Hook | Schedule |
|---|---|
| wpestate_crm_process_automations | hourly |
| wpestate_crm_daily_notifications | daily |
| wpestate_crm_weekly_digest | weekly |
Deactivation
wpestate_crm_deactivate() // lines 323-328
Clears the three scheduled cron events. Leaves tables and options intact.
Update Detection
wpestate_crm_check_plugin_functionality_loaded() hooks plugins_loaded, compares get_option(‘wpestate_crm_db_version’) against WPESTATE_CRM_VERSION, and re-runs the activation routine on mismatch. This handles the case where the plugin file is replaced via SFTP and no activation hook fires.
Schema Migrations
CPT-to-Table Migration
wpestate_crm_migrate_cpt_data() // libs/migration.php
- Guard: wpestate_crm_cpt_migrated flag.
- Inline if total posts <= 500; otherwise batched via WP-Cron at 100 posts per batch.
- Migrates wpestate_crm_contact posts into wp_wpresidence_crm_contacts, skipping duplicates by email.
- Migrates wpestate_crm_lead posts into the leads table and creates a form_submission activity per post.
- Migrates WP comments on the legacy CPT posts into note activities.
- Leaves original CPT posts in place as reference and does not delete them.
Schema v2 (Column Rename)
wpestate_crm_migrate_schema_v2() // libs/db-setup.php
- Guard: wpestate_crm_schema_v2_migrated flag.
- Copies data from legacy column names to new planned column names, such as deal_title → title, deal_stage → stage, and task_title → title.
- Copies activities with entity_type=’contact’ so they set contact_id instead of entity_id.
- Does not drop old columns. Both remain for safety.
HubSpot Settings Migration
wpestate_crm_migrate_hubspot_settings() // libs/settings.php
- Guard: wpestate_crm_hubspot_migrated flag.
- Copies wp_estate_* Redux keys into the wpestate_crm_settings array under crm_hubspot_* keys.
Viewed Listings Column Rename
In wpestate_crm_create_tables(), if the table has an old user_id column, it renames it to contact_id and drops the old index.
Page Auto-Creation
wpresidence_create_crm_helper_content() // lines 1159-1257
Hooked to wp_loaded. Compares stored setup version, currently 2.1, against the code version and creates any missing CRM pages with the correct _wp_page_template meta.
Uninstall
uninstall.php:
- DROP TABLE for all 9 custom tables.
- DELETE OPTION for CRM options: wpestate_crm_settings, wpestate_crm_db_version, wpestate_crm_cpt_migrated, wpestate_crm_schema_v2_migrated, wpestate_crm_hubspot_migrated, wpestate_crm_permissions, wpestate_crm_webhooks.
- Remove manage_crm capability from all roles.
- Delete CRM dashboard pages identified by their _wp_page_template meta value.
- Recursively delete the /uploads/wpestate-crm/ directory.
- Clear all CRM cron events.
DB Version Option
Key: wpestate_crm_db_version. Value: plugin version string, for example 5.2.2. Updated at the end of every activation run.
Development Tips
- To force a re-run of activation during development, delete the wpestate_crm_db_version option and reload any admin page.
- To rewind a migration flag, for example to re-run CPT migration for testing, delete the corresponding *_migrated option.
- Never edit table schemas directly. Edit libs/db-setup.php and let dbDelta reconcile on the next activation run.