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 viadbDelta.wpestate_crm_seed_defaults()— seed default settings (skip existing non-empty).wpestate_crm_migrate_cpt_data()— migrate legacy CPT posts (guarded bywpestate_crm_cpt_migratedflag).- Register
manage_crmon 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 (no activation hook fires).
Schema Migrations
CPT-to-Table Migration
wpestate_crm_migrate_cpt_data() // libs/migration.php
- Guard:
wpestate_crm_cpt_migratedflag. - Inline if total posts <= 500; else batched via WP-Cron at 100 posts per batch.
- Migrates
wpestate_crm_contactposts intowp_wpresidence_crm_contacts(skips duplicates by email). - Migrates
wpestate_crm_leadposts into 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 (not deleted).
Schema v2 (Column Rename)
wpestate_crm_migrate_schema_v2() // libs/db-setup.php
- Guard:
wpestate_crm_schema_v2_migratedflag. - Copies data from legacy column names to new planned column names (deal_title → title, deal_stage → stage, task_title → title, etc.).
- Copies activities
entity_type='contact'rows to setcontact_idinstead ofentity_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_migratedflag. - Copies
wp_estate_*Redux keys into thewpestate_crm_settingsarray undercrm_hubspot_*keys.
Viewed Listings Column Rename
In wpestate_crm_create_tables(): if the table has an old user_id column, rename it to contact_id and drop 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 code version; 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_crmcapability from all roles. - Delete CRM dashboard pages (identified by their
_wp_page_templatemeta 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 (e.g., 5.2.2). Updated at the end of every activation run.
Development Tips
- To force re-run of activation during development, delete the
wpestate_crm_db_versionoption and reload any admin page. - To rewind a migration flag (e.g., re-run CPT migration for testing), delete the corresponding
*_migratedoption. - Never edit table schemas directly; edit
libs/db-setup.phpand letdbDeltareconcile on the next activation run.