Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
1310114
update: prepare table name method now support multi-site too
harunollyo Nov 3, 2026
6c8babb
Merge branch '4.2.1' of https://github.com/themeum/tutor into query-b…
harunollyo Nov 5, 2026
9c6f50d
refactor: use WordPress wpdb properties for postmeta and users tables
harunollyo Nov 5, 2026
59df598
email table creation moved to addon
harunollyo Nov 9, 2026
f320800
feat: add WordPress multisite support and modular database management
harunollyo Nov 9, 2026
84452d7
refactor: update table registration and core table creation methods i…
harunollyo Nov 9, 2026
35ce716
refactor: refactor database schemas into modular Table classes
harunollyo Nov 10, 2026
ee610fc
fix: validate table class names before checking instance in TableManager
harunollyo Nov 10, 2026
a5a0f2a
refactor: improve code style and phpdoc formatting across database an…
harunollyo Nov 10, 2026
5531778
refactor: replace QueryHelper with DB class methods in Upgrader
harunollyo Nov 10, 2026
098bf93
refactor: optimize database table registration and naming in TableMan…
harunollyo Nov 11, 2026
d29691d
refactor: refactor multisite site provisioning and table management h…
harunollyo Nov 11, 2026
d1528bd
remove commented out REST API privacy filter hook in Tutor class
harunollyo Nov 11, 2026
384866a
fix: prepend global namespace to WP_CLI constant check
harunollyo Nov 11, 2026
f4f7660
Merge branch '4.3.0' of https://github.com/themeum/tutor into feat/mu…
harunollyo Nov 26, 2026
8c7a206
docs: add since 4.3.0 version tags to database tables and manager cla…
harunollyo Nov 26, 2026
7caefae
refactor: migrate earnings and withdraws table creation to dedicated …
harunollyo Nov 26, 2026
acfd0c2
chore: update version tags to 4.3.0 for multisite table prefix support
harunollyo Nov 26, 2026
5752e3e
docs: update since version description for database creation method
harunollyo Nov 26, 2026
f7af5fd
refactor: clean up unused properties, hooks, and test methods in Emai…
harunollyo Nov 26, 2026
c498fba
fix: foreign key issue on site delete
harunollyo Nov 26, 2026
c5b21d0
fix: set default value of answer_explanation to null in QuizQuestions…
harunollyo Nov 27, 2026
9e5c782
fix: remove unused queue_table property in EmailController
harunollyo Nov 27, 2026
d86e9cc
Merge branch '4.3.0' of https://github.com/themeum/tutor into feat/mu…
harunollyo Nov 27, 2026
cd4e27c
fix: prepare table name error when it has alias
harunollyo Nov 30, 2026
b92bba1
revert old code for review request
harunollyo Nov 30, 2026
0e7d0c5
added table schema comment
harunollyo Nov 30, 2026
40b4ba8
cs fix
harunollyo Nov 30, 2026
2853a76
refactor: removed unused method
harunollyo Nov 30, 2026
a4fd629
missing code added on provision a site
harunollyo Nov 30, 2026
29daa97
update: add cap instead of add role
harunollyo Nov 30, 2026
7187636
catched trowable instead of exception
harunollyo Nov 30, 2026
405f0de
refactor: used table schema class instead of inline sql
harunollyo Nov 30, 2026
4a6f45a
override include constraints for existing table
harunollyo Nov 30, 2026
1e9bb18
refactor: remove unused Table class imports from database table classes
harunollyo Nov 30, 2026
0840898
docs: remove outdated since tags from multisite hooks
harunollyo Nov 30, 2026
07ba985
fix: improve database table prefixing, multisite provisioning, and JS…
harunollyo Nov 30, 2026
9406f32
fix: add support for table names starting with digits in database hel…
harunollyo Nov 30, 2026
95edaaf
fix: prevent duplicate multisite provisioning and check network activ…
harunollyo Nov 30, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions classes/Multisite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php
/**
* Handle multisite support for Tutor LMS.
*
* @package TUTOR
* @author Themeum <support@themeum.com>
* @link https://themeum.com
* @since 4.3.0
*/

namespace TUTOR;

defined( 'ABSPATH' ) || exit;

use Tutor\Database\TableManager;

/**
* Class Multisite
*
* @since 4.3.0
*/
class Multisite {

/**
* Constructor.
*
* @since 4.3.0
*/
public function __construct() {
if ( ! is_multisite() ) {
return;
}

add_action( 'wp_initialize_site', array( $this, 'on_initialize_site' ), 10, 2 );
add_action( 'wp_uninitialize_site', array( $this, 'on_uninitialize_site' ), 5 );
add_action( 'init', array( $this, 'maybe_provision_current_site' ), 1 );
add_action( 'init', array( $this, 'ensure_admin_capabilities' ) );
}

/**
* Initialize Tutor on a newly created subsite.
*
* @since 4.3.0
*
* @param \WP_Site $site New site object.
* @param array $args Setup arguments.
*
* @return void
*/
public function on_initialize_site( \WP_Site $site, array $args = array() ): void {
if ( ! is_plugin_active_for_network( plugin_basename( TUTOR_FILE ) ) ) {
return;
}

$blog_id = (int) $site->blog_id;

self::run_in_site(
$blog_id,
function () use ( $blog_id, $site ) {
self::provision_site();

/**
* Action fired after a subsite is initialized with Tutor LMS.
Comment thread
harunollyo marked this conversation as resolved.
Resolved
*
* @param int $blog_id Blog ID.
* @param \WP_Site $site Site instance.
*/
do_action( 'tutor_multisite_subsite_initialized', $blog_id, $site );
}
);
}

/**
* Clean up tables on subsite deletion.
*
* @since 4.3.0
*
* @param \WP_Site $site Site being uninitialized.
*
* @return void
*/
public function on_uninitialize_site( \WP_Site $site ): void {
$blog_id = (int) $site->blog_id;

/**
* Filter whether to drop Tutor tables when a subsite is deleted.
*
* @param bool $should_drop Whether to drop tables. Default true.
* @param int $blog_id Blog ID being deleted.
*/
if ( apply_filters( 'tutor_multisite_drop_tables_on_delete', true, $blog_id ) ) {
self::run_in_site( $blog_id, array( TableManager::class, 'drop_tables' ) );
}

/**
* Action fired after subsite tables have been cleaned up.
*
* @since 4.3.0
*
* @param int $blog_id Blog ID.
* @param \WP_Site $site Site instance.
*/
do_action( 'tutor_multisite_subsite_deleted', $blog_id, $site );
}

/**
* Network activation runner.
*
* Provisions the current site immediately; remaining subsites are lazily
* provisioned on their first request via {@see maybe_provision_current_site()}
* to avoid timeouts on large networks.
*
* @since 4.3.0
*
* @return void
*/
public static function activate_network(): void {
self::provision_site();
}

/**
* Lazily provision the current subsite if Tutor has not been initialized on it yet.
*
* Covers existing subsites after network activation as well as sites created
* while the plugin was inactive.
*
* @since 4.3.0
*
* @return void
*/
public function maybe_provision_current_site(): void {
if ( wp_installing() || get_option( 'tutor_version' ) || get_transient( 'tutor_provisioning' ) ) {

Check failure on line 132 in classes/Multisite.php

View workflow job for this annotation

GitHub Actions / WPCS

Expected exactly one space before closing parenthesis; &quot; &quot; found.
return;
}

set_transient( 'tutor_provisioning', 1, MINUTE_IN_SECONDS );
try {
self::provision_site();
} finally {
delete_transient( 'tutor_provisioning' );
}
}

/**
* Ensure administrator users have required Tutor capabilities on the subsite.
*
* @since 4.3.0
*
* @return void
*/
public function ensure_admin_capabilities(): void {
if ( User::is_admin() && ! current_user_can( tutor()->instructor_role ) ) {
$user = wp_get_current_user();
if ( $user && $user->exists() ) {
$user->add_cap( 'manage_tutor' );
$user->add_role( tutor()->instructor_role );
}
}
}

/**
* Create tables and seed defaults for the current site.
*
* Tables are always created or updated so missing ones are restored.
* Default options, roles and pages are seeded only on a fresh install,
* so existing settings are never overwritten.
*
* @since 4.3.0
*
* @return void
*/
private static function provision_site(): void {
Comment thread
harunollyo marked this conversation as resolved.
Resolved
$is_installed = (bool) get_option( 'tutor_version' );

TableManager::create_core_tables( ! $is_installed );

if ( $is_installed ) {
return;
}

// Rewrite Flush.
Permalink::set_permalink_flag();

// Set scheduler.
if ( ! wp_next_scheduled( 'tutor_once_in_day_run_schedule' ) ) {
wp_schedule_event( tutor_time(), 'twicedaily', 'tutor_once_in_day_run_schedule' );
}

// Save First activation Time.
$first_activation_date = get_option( 'tutor_first_activation_time' );
if ( ! $first_activation_date ) {
update_option( 'tutor_first_activation_time', tutor_time() );
}

update_option( 'tutor_option', Tutor::default_options() );
Tutor::manage_tutor_roles_and_permissions();
Tutor::save_data();
update_option( 'tutor_version', TUTOR_VERSION );
}

/**
* Run a callback in the context of a given site, always restoring the previous one.
*
* @since 4.3.0
*
* @param int $blog_id Blog ID.
* @param callable $callback Callback to run.
*
* @return void
*/
private static function run_in_site( int $blog_id, callable $callback ): void {
switch_to_blog( $blog_id );
try {
$callback();
} catch ( \Throwable $e ) {
error_log(
sprintf( 'Tutor: site initialization failed for blog ID %d: %s', $blog_id, $e->getMessage() )
);
} finally {
restore_current_blog();
}
}
}
Loading
Loading