Skip to content

🎉 Add WordPress Multisite support and modularize database management - #3267

Open
harunollyo wants to merge 39 commits into
4.3.0from
feat/multisite
Open

harunollyo wants to merge 39 commits into
4.3.0from
feat/multisite

Conversation

@harunollyo

Copy link
Copy Markdown
Collaborator

ClickUp: https://app.clickup.com/t/9018365849/z8zyy1rk1c

Summary

Adds WordPress Multisite support and modernizes Tutor LMS database management with a modular, extensible architecture.

Key Improvements

  • Multisite: Network-wide activation and automatic table provisioning for new subsites.
  • Centralized Management: Single manager for table creation, registration, and cleanup.
  • Less Duplication: Shared logic reduces repeated code and improves consistency.
  • Extensibility: Hooks let Pro features and add-ons register their own tables.
  • Compatibility: Single-site behavior and public entry points are preserved.

Design Patterns & Approaches

  • Template Method: Standardizes table operations while each table defines only its schema.
  • Registry: One manager handles table registration and lifecycle.
  • Hook-Based Extensibility: Addons integrate custom tables without core changes.
  • Safe Context Management: The original site context is always restored, even on errors.

Backward Compatibility

Single-site behavior and public entry points are preserved, but some legacy database methods were replaced. Tutor Pro and third-party addons may need to adopt the new table registration approach.

Testing

  • Tested
  • Not tested

Use of AI Tool

Used: Yes

Used for: Analyzing dependencies, ensuring backward compatibility, and implementing code according to a planned architecture.

@harunollyo
harunollyo requested a review from shewa12 November 30, 2026 06:15
@harunollyo harunollyo self-assigned this Nov 30, 2026
@harunollyo harunollyo added the 4.3.0 Tutor v4.3.0 label Nov 30, 2026

@shewa12 shewa12 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice direction: one Table class per schema, a central TableManager, and per-blog FK names via fk_name() (needed because FK constraint names must be unique across the whole database, which made the old fixed names collide on multisite). I regenerated all 17 schemas from the new classes and diffed them against the previous CREATE TABLE SQL. They match, apart from these intentional or harmless differences:

  • answer_explanation is DEFAULT NULL instead of '', which also avoids the strict-mode "TEXT can't have a default" error.
  • item_details / item_type are now part of the base tutor_cart_items schema.
  • The column COMMENTs on tutor_orders are dropped (nit inline).

CI: PHPUnit, E2E and PHP compat pass. WPCS fails on new files.

Requesting changes, mainly for backward compatibility with Tutor Pro and the multisite lifecycle:

  1. 🔴 Free still reads $wpdb->tutor_gradebooks and $wpdb->tutor_email_queue, but no longer registers them, and the gradebook/email-queue installers were removed. Without a matching Pro release this breaks the Gradebook and email queue.
  2. 🔴 wp_initialize_site provisions Tutor (tables, options, roles and pages) on every new subsite, even when Tutor isn't network-activated.
  3. 🟠 Network or new-site provisioning skips the per-site activation steps (daily cron, permalink flush, first-activation time).
  4. 🟠 Uninstall on multisite only cleans the current site, and now also drops the e-commerce tables.
  5. 🟠 Upgrader still creates tutor_order_itemmeta with the old fixed FK name, so it collides on the second subsite.

Details are inline.

Comment thread classes/Upgrader.php Resolved
Comment thread classes/Multisite.php Resolved
Comment thread classes/Multisite.php Resolved
Comment thread classes/Multisite.php Resolved
Comment thread classes/Multisite.php Resolved
Comment thread database/Tables/Table.php Resolved
Comment thread classes/Tutor.php Resolved
Comment thread classes/Tutor.php Resolved
Comment thread database/Tables/OrdersTable.php Resolved
Comment thread database/TableManager.php Resolved
Comment thread database/Tables/CartItemsTable.php Resolved
Comment thread database/Tables/Table.php Resolved

@shewa12 shewa12 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up after a deeper pass over all 17 table classes and the code paths that create them. This adds new findings on top of my earlier review.

How I verified this:

  • I extracted WordPress core's real dbDelta() (current trunk) and ran each new get_schema() through it against two states: a fresh install (no tables), and an existing site whose tables were built from the previous Tutor::create_database() / Upgrader SQL. I then diffed the queries dbDelta() issues for the old schema vs the new one.
  • I diffed every new class column by column against the old SQL. All columns, types, defaults, keys and table-creation/FK ordering match, apart from the intentional changes noted earlier.
  • I resolved Table::get_name() under different $table_prefix values.
  • I checked tutor-pro (local 4.2.3, plus the origin/4.3.0 and origin/multisite branches) for the $wpdb table properties this PR stops registering.

Fresh installs are fine: dbDelta() issues exactly one valid CREATE TABLE per table, in a valid FK order.

Existing sites and some prefixes are not:

  1. 🔴 Putting the CONSTRAINT on the same line as the last KEY breaks dbDelta()'s index parser for 5 tables. Every reactivation of an existing site raises 20 PHP warnings ("Undefined array key index_type/index_name/index_columns/column_name") and runs 5 invalid ALTER TABLE … ADD `` (``) queries. Warnings during activation also cause the "plugin generated unexpected output" notice.
  2. 🔴 Table::get_name() goes through the new prefix-stripping prepare_table_name(). On sites whose $table_prefix is a leading part of tutor_ (t, tu, tut, tutor, tutor_ …), every Tutor table resolves to the wrong name (tutor_orders instead of tutor_tutor_orders). Existing data "disappears" and empty tables get created.
  3. 🔴 Confirmed backward-compatibility break with Pro: no Pro branch I checked registers $wpdb->tutor_gradebooks, tutor_gradebooks_results, tutor_email_queue or the tutor_subscription* properties, or creates the gradebook/email-queue tables. Pro uses $wpdb->tutor_gradebooks* 68 times. With this PR and the current Pro, Gradebook and the email queue break. Please link the companion Pro PR, or keep the fallbacks (see the earlier comment on Tutor.php:468).
  4. 🟠 Upgrader now uses DB::has_table()/has_column(), which reject table names starting with a digit. QueryHelper::table_exists() didn't, so on prefixes like 2fa_ every column migration is silently skipped.
  5. 🟡 JSON in the base tutor_cart_items schema makes the whole CREATE TABLE fail on MySQL < 5.7.8 / MariaDB < 10.2.7. CREATE TABLE IF NOT EXISTS on earnings/withdraws stops dbDelta() from ever updating those tables.

Details are inline.

Comment thread database/Tables/Table.php Resolved
Comment thread database/Tables/OrdermetaTable.php Resolved
Comment thread database/Tables/Table.php Resolved
Comment thread classes/Upgrader.php Resolved
Comment thread database/Tables/CartItemsTable.php Resolved
Comment thread database/Tables/EarningsTable.php Resolved
@shewa12

shewa12 commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Correction to point 3 of my review above. I analysed d86e9cc, and the review was posted after the newer commits up to 7187636 landed.

  • ✅ Resolved: Tutor.php again registers $wpdb->tutor_gradebooks, tutor_gradebooks_results, tutor_email_queue and the tutor_subscription* properties, so Pro's 68 $wpdb->tutor_gradebooks* references resolve again.
  • 🔴 Still open: table creation for Pro addons. Upgrader::__construct() no longer hooks install_gradebook / install_tutor_email_queue on tutor_addon_before_enable_tutor-pro/addons/gradebook/... and .../tutor-email/..., and init_email_table_deployment() now only calls \TutorPro\Database\Tables\EmailQueueTable::create() when that class exists. No current tutor-pro branch I checked (4.2.3, origin/4.3.0, origin/multisite) has that class or creates the tutor_gradebooks / tutor_gradebooks_results / tutor_email_queue tables itself. So with this PR and the current Pro, enabling Gradebook or Email on a site that doesn't already have those tables leaves them missing, and queries fail. Please keep the old installers until the companion Pro PR ships the table classes, or link that Pro PR here.

Everything else in the review (points 1, 2, 4 and 5, and all 6 inline comments) still applies to 7187636; the referenced lines are unchanged.

Comment thread database/Tables/EarningsTable.php Resolved
Comment thread database/Tables/QuizQuestionsTable.php Resolved
Comment thread database/Tables/SchedulerTable.php Resolved
Comment thread database/Tables/WithdrawsTable.php Resolved
Comment thread classes/Multisite.php Resolved
@harunollyo
harunollyo requested a review from shewa12 November 30, 2026 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.3.0 Tutor v4.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants