assets_url = WPMCS_ASSETS_URL; $this->version = WPMCS_VERSION; $this->token = WPMCS_TOKEN; } /** * Get table name with correct DB prefix (multisite-aware) * * @param int|null $blog_id Optional. Blog ID for multisite. * @return string */ public static function get_table_name( $blog_id = null ) { global $wpdb; $prefix = is_multisite() ? $wpdb->get_blog_prefix( $blog_id ?? get_current_blog_id() ) : $wpdb->prefix; return $prefix . WPMCS_ITEM_TABLE; } /** * Create Database Table * */ public function create_table() { global $wpdb; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); $table_name = self::get_table_name(); $queries = array(); $charset_collate = $wpdb->get_charset_collate(); if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { $queries[] = " CREATE TABLE {$table_name} ( id BIGINT(20) NOT NULL AUTO_INCREMENT, provider varchar(18) NOT NULL, region varchar(255) NOT NULL, storage varchar(255) NOT NULL, source_id bigint(20) NOT NULL, source_path varchar(1024) NOT NULL, source_type varchar(18) NOT NULL, url varchar(1024) NOT NULL, `key` varchar(1024) NOT NULL, is_private tinyint(1) NOT NULL DEFAULT 0, extra longtext DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY uidx_url (url(190), id), UNIQUE KEY uidx_key (`key`(190), id), UNIQUE KEY uidx_source_path (source_path(190), id) ) $charset_collate;"; } dbDelta( $queries ); } /** * Create tables across all sites (on activation) */ public static function handle_tables($network_wide = false) { // If network-wide activation, create tables for all sites in multisite if (is_multisite() && $network_wide) { $sites = get_sites(['number' => 1000, 'fields' => 'ids']); // Adjust number as needed // If no sites found, return early if (is_wp_error($sites) || !is_array($sites) || empty($sites)) { return; } // Create table for each site in multisite foreach ($sites as $site_id) { switch_to_blog($site_id); self::instance()->create_table(); restore_current_blog(); } } else { self::instance()->create_table(); } } /** * Database Upgrade * @since 1.0.0 */ public function do_database_upgrade() { global $wpdb; $table_name = self::get_table_name(); $current_version = get_option( $this->token.'_db_version' ); /** * If DB version below 1.0.1 */ if (version_compare($current_version, '1.0.1', '<')) { $credentials = Utils::get_credentials(); if (isset($credentials['service']) && $credentials['service'] === 'gcloud') { $config = $credentials['config'] ?? null; if($config) { $config_json_path = $config['config_json']['path'] ?? null; if (!empty($config_json_path) && file_exists($config_json_path)) { $config_json = file_get_contents($config_json_path); if (!empty($config_json)) { $credentials['config']['config_json'] = $config_json; Utils::update_credentials($credentials); $updated = Utils::update_option('credentials', $credentials, Schema::getConstant('GLOBAL_SETTINGS_KEY')); if ($updated) { unlink($config_json_path); } } } } } } update_option( $this->token.'_db_version', WPMCS_DB_VERSION, true ); } /** * Ensures only one instance of Class is loaded or can be loaded. * * @return Db Class instance * @since 1.0.0 * @static */ public static function instance(){ if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } }