PluginProbe
Media Cloud Sync / 1.2.13
Media Cloud Sync v1.2.13
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.13, at includes/base/db.php

159 lines 5.5 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 * @since 1.2.11
105 *
106 * This is to handle the migration of Google Cloud credentials from file to JSON string.
107 */
108 if (version_compare($current_version, '1.0.1', '<')) {
109 $credentials = Utils::get_credentials();
110 if (isset($credentials['service']) && $credentials['service'] === 'gcloud') {
111 $config = $credentials['config'] ?? null;
112 if($config) {
113 $config_json_path = $config['config_json']['path'] ?? null;
114 if (!empty($config_json_path) && file_exists($config_json_path)) {
115 $config_json = file_get_contents($config_json_path);
116 if (!empty($config_json)) {
117 $credentials['config']['config_json'] = $config_json;
118 Utils::update_credentials($credentials);
119 $updated = Utils::update_option('credentials', $credentials, Schema::getConstant('GLOBAL_SETTINGS_KEY'));
120 if ($updated) {
121 unlink($config_json_path);
122 }
123 }
124 }
125 }
126 }
127 }
128
129 /**
130 * If DB version below 1.0.2
131 *
132 * @since 1.2.12
133 * This is to handle the migration of media library counter from the global settings to the specific media library key.
134 */
135 if (version_compare($current_version, '1.0.2', '<')) {
136 $settings_key = Schema::getConstant('COUNTER_KEY');
137 $media_library = Utils::get_option( '', [], $settings_key );
138 if (is_array($media_library) && !empty($media_library)) {
139 Utils::update_option('', ['media_library' => $media_library ?? []], $settings_key);
140 }
141 }
142
143 update_option( $this->token.'_db_version', WPMCS_DB_VERSION, true );
144 }
145
146 /**
147 * Ensures only one instance of Class is loaded or can be loaded.
148 *
149 * @return Db Class instance
150 * @since 1.0.0
151 * @static
152 */
153 public static function instance(){
154 if (is_null(self::$instance)) {
155 self::$instance = new self();
156 }
157 return self::$instance;
158 }
159 }