Repository navigation
🎉 Add WordPress Multisite support and modularize database management #3267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
harunollyo
wants to merge
39
commits into
4.3.0
Choose a base branch
from
feat/multisite
base: 4.3.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 6c8babb
Merge branch '4.2.1' of https://github.com/themeum/tutor into query-b…
harunollyo 9c6f50d
refactor: use WordPress wpdb properties for postmeta and users tables
harunollyo 59df598
email table creation moved to addon
harunollyo f320800
feat: add WordPress multisite support and modular database management
harunollyo 84452d7
refactor: update table registration and core table creation methods i…
harunollyo 35ce716
refactor: refactor database schemas into modular Table classes
harunollyo ee610fc
fix: validate table class names before checking instance in TableManager
harunollyo a5a0f2a
refactor: improve code style and phpdoc formatting across database an…
harunollyo 5531778
refactor: replace QueryHelper with DB class methods in Upgrader
harunollyo 098bf93
refactor: optimize database table registration and naming in TableMan…
harunollyo d29691d
refactor: refactor multisite site provisioning and table management h…
harunollyo d1528bd
remove commented out REST API privacy filter hook in Tutor class
harunollyo 384866a
fix: prepend global namespace to WP_CLI constant check
harunollyo f4f7660
Merge branch '4.3.0' of https://github.com/themeum/tutor into feat/mu…
harunollyo 8c7a206
docs: add since 4.3.0 version tags to database tables and manager cla…
harunollyo 7caefae
refactor: migrate earnings and withdraws table creation to dedicated …
harunollyo acfd0c2
chore: update version tags to 4.3.0 for multisite table prefix support
harunollyo 5752e3e
docs: update since version description for database creation method
harunollyo f7af5fd
refactor: clean up unused properties, hooks, and test methods in Emai…
harunollyo c498fba
fix: foreign key issue on site delete
harunollyo c5b21d0
fix: set default value of answer_explanation to null in QuizQuestions…
harunollyo 9e5c782
fix: remove unused queue_table property in EmailController
harunollyo d86e9cc
Merge branch '4.3.0' of https://github.com/themeum/tutor into feat/mu…
harunollyo cd4e27c
fix: prepare table name error when it has alias
harunollyo b92bba1
revert old code for review request
harunollyo 0e7d0c5
added table schema comment
harunollyo 40b4ba8
cs fix
harunollyo 2853a76
refactor: removed unused method
harunollyo a4fd629
missing code added on provision a site
harunollyo 29daa97
update: add cap instead of add role
harunollyo 7187636
catched trowable instead of exception
harunollyo 405f0de
refactor: used table schema class instead of inline sql
harunollyo 4a6f45a
override include constraints for existing table
harunollyo 1e9bb18
refactor: remove unused Table class imports from database table classes
harunollyo 0840898
docs: remove outdated since tags from multisite hooks
harunollyo 07ba985
fix: improve database table prefixing, multisite provisioning, and JS…
harunollyo 9406f32
fix: add support for table names starting with digits in database hel…
harunollyo 95edaaf
fix: prevent duplicate multisite provisioning and check network activ…
harunollyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| * | ||
| * @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' ) ) { | ||
| 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 { | ||
|
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(); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.