PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.0
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / database / DBMigrator.php

DBMigrator.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.0, at database/DBMigrator.php

115 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Database;
4
5 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
6
7 use FluentSupport\Database\Migrations\ActivityMigrator;
8 use FluentSupport\Database\Migrations\MailBoxMigrator;
9 use FluentSupport\Database\Migrations\MetaMigrator;
10 use FluentSupport\Database\Migrations\PersonsMigrator;
11 use FluentSupport\Database\Migrations\TicketsMigrator;
12 use FluentSupport\Database\Migrations\ConversationsMigrator;
13 use FluentSupport\Database\Migrations\AttachmentsMigrator;
14 use FluentSupport\Database\Migrations\TaggablesMigrator;
15 use FluentSupport\Database\Migrations\TagRelationsMigrator;
16 use FluentSupport\Database\Migrations\DataMetrixMigrator;
17 use FluentSupport\Database\Migrations\ProductsMigrator;
18
19 class DBMigrator
20 {
21 protected static $migrators = [
22 ProductsMigrator::class,
23 PersonsMigrator::class,
24 TicketsMigrator::class,
25 ConversationsMigrator::class,
26 AttachmentsMigrator::class,
27 TaggablesMigrator::class,
28 TagRelationsMigrator::class,
29 DataMetrixMigrator::class,
30 MetaMigrator::class,
31 MailBoxMigrator::class,
32 ActivityMigrator::class,
33 ];
34
35 public static function run($network_wide = false)
36 {
37 global $wpdb;
38
39 if ($network_wide) {
40 // Retrieve all site IDs from this network (WordPress >= 4.6 provides easy to use functions for that).
41 if (function_exists('get_sites') && function_exists('get_current_network_id')) {
42 $site_ids = get_sites(array(
43 'fields' => 'ids',
44 'network_id' => get_current_network_id()
45 ));
46 } else {
47 $site_ids = $wpdb->get_col(
48 "SELECT blog_id FROM $wpdb->blogs WHERE site_id = $wpdb->siteid;"
49 );
50 }
51 // Install the plugin for all these sites.
52 foreach ($site_ids as $site_id) {
53 switch_to_blog($site_id);
54 self::migrate();
55 restore_current_blog();
56 }
57 } else {
58 self::migrate();
59 }
60 }
61
62 public static function migrate()
63 {
64 foreach (static::$migrators as $class) {
65 $class::migrate();
66 }
67 }
68
69 // Version-gated re-run for in-place updates that never reactivate; locked so concurrent requests don't race.
70 public static function maybeMigrateDBChanges()
71 {
72 $currentDbVersion = get_option('fluent_support_db_version');
73
74 if ($currentDbVersion && !version_compare($currentDbVersion, FLUENT_SUPPORT_DB_VERSION, '<')) {
75 return;
76 }
77
78 if (!self::acquireMigrationLock()) {
79 return;
80 }
81
82 self::migrate();
83 update_option('fluent_support_db_version', FLUENT_SUPPORT_DB_VERSION, 'no');
84
85 self::releaseMigrationLock();
86 }
87
88 // MySQL advisory lock (zero-wait); fails open on NULL so a locking hiccup never blocks upgrades.
89 private static function acquireMigrationLock()
90 {
91 global $wpdb;
92
93 $acquired = $wpdb->get_var($wpdb->prepare("SELECT GET_LOCK(%s, 0)", self::getMigrationLockName()));
94
95 return $acquired === null || (string) $acquired === '1';
96 }
97
98 private static function releaseMigrationLock()
99 {
100 global $wpdb;
101
102 $wpdb->query($wpdb->prepare("SELECT RELEASE_LOCK(%s)", self::getMigrationLockName()));
103 }
104
105 private static function getMigrationLockName()
106 {
107 global $wpdb;
108
109 // GET_LOCK names are server-wide; scope to this site's DB and prefix.
110 $dbName = defined('DB_NAME') ? DB_NAME : '';
111
112 return 'fs_db_migration_' . md5($dbName . '|' . $wpdb->prefix);
113 }
114 }
115