PluginProbe
Media Cloud Sync / 1.2.11
Media Cloud Sync v1.2.11
1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / db.php

db.php in Media Cloud Sync 1.2.11, at includes/base/db.php

142 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Dudlewebs\WPMCS;
3
4 defined('ABSPATH') || exit;
5
6 class Db {
7 private static $instance = null;
8 private string $assets_url;
9 private string $version;
10 private string $token;
11
12
13 /**
14 * Admin constructor.
15 * @since 1.0.0
16 */
17 public function __construct() {
18 $this->assets_url = WPMCS_ASSETS_URL;
19 $this->version = WPMCS_VERSION;
20 $this->token = WPMCS_TOKEN;
21 }
22
23 /**
24 * Get table name with correct DB prefix (multisite-aware)
25 *
26 * @param int|null $blog_id Optional. Blog ID for multisite.
27 * @return string
28 */
29 public static function get_table_name( $blog_id = null ) {
30 global $wpdb;
31 $prefix = is_multisite() ? $wpdb->get_blog_prefix( $blog_id ?? get_current_blog_id() ) : $wpdb->prefix;
32 return $prefix . WPMCS_ITEM_TABLE;
33 }
34
35 /**
36 * Create Database Table
37 *
38 */
39 public function create_table() {
40 global $wpdb;
41
42 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
43
44 $table_name = self::get_table_name();
45 $queries = array();
46 $charset_collate = $wpdb->get_charset_collate();
47 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
48 $queries[] = "
49 CREATE TABLE {$table_name} (
50 id BIGINT(20) NOT NULL AUTO_INCREMENT,
51 provider varchar(18) NOT NULL,
52 region varchar(255) NOT NULL,
53 storage varchar(255) NOT NULL,
54 source_id bigint(20) NOT NULL,
55 source_path varchar(1024) NOT NULL,
56 source_type varchar(18) NOT NULL,
57 url varchar(1024) NOT NULL,
58 `key` varchar(1024) NOT NULL,
59 is_private tinyint(1) NOT NULL DEFAULT 0,
60 extra longtext DEFAULT NULL,
61 PRIMARY KEY (id),
62 UNIQUE KEY uidx_url (url(190), id),
63 UNIQUE KEY uidx_key (`key`(190), id),
64 UNIQUE KEY uidx_source_path (source_path(190), id)
65 ) $charset_collate;";
66 }
67 dbDelta( $queries );
68 }
69
70 /**
71 * Create tables across all sites (on activation)
72 */
73 public static function handle_tables($network_wide = false) {
74 // If network-wide activation, create tables for all sites in multisite
75 if (is_multisite() && $network_wide) {
76 $sites = get_sites(['number' => 1000, 'fields' => 'ids']); // Adjust number as needed
77 // If no sites found, return early
78 if (is_wp_error($sites) || !is_array($sites) || empty($sites)) {
79 return;
80 }
81
82 // Create table for each site in multisite
83 foreach ($sites as $site_id) {
84 switch_to_blog($site_id);
85 self::instance()->create_table();
86 restore_current_blog();
87 }
88 } else {
89 self::instance()->create_table();
90 }
91 }
92
93 /**
94 * Database Upgrade
95 * @since 1.0.0
96 */
97 public function do_database_upgrade() {
98 global $wpdb;
99 $table_name = self::get_table_name();
100 $current_version = get_option( $this->token.'_db_version' );
101
102 /**
103 * If DB version below 1.0.1
104 */
105 if (version_compare($current_version, '1.0.1', '<')) {
106 $credentials = Utils::get_credentials();
107 if (isset($credentials['service']) && $credentials['service'] === 'gcloud') {
108 $config = $credentials['config'] ?? null;
109 if($config) {
110 $config_json_path = $config['config_json']['path'] ?? null;
111 if (!empty($config_json_path) && file_exists($config_json_path)) {
112 $config_json = file_get_contents($config_json_path);
113 if (!empty($config_json)) {
114 $credentials['config']['config_json'] = $config_json;
115 Utils::update_credentials($credentials);
116 $updated = Utils::update_option('credentials', $credentials, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
117 if ($updated) {
118 unlink($config_json_path);
119 }
120 }
121 }
122 }
123 }
124 }
125
126 update_option( $this->token.'_db_version', WPMCS_DB_VERSION, true );
127 }
128
129 /**
130 * Ensures only one instance of Class is loaded or can be loaded.
131 *
132 * @return Db Class instance
133 * @since 1.0.0
134 * @static
135 */
136 public static function instance(){
137 if (is_null(self::$instance)) {
138 self::$instance = new self();
139 }
140 return self::$instance;
141 }
142 }