dbname; $key = "Tables_in_{$db}"; $plTables = array( $wpdb->prefix . self::$tableDimension, $wpdb->prefix . self::$tableLabelSheets, $wpdb->prefix . self::$tablePaperFormats, $wpdb->prefix . self::$tableProfiles, $wpdb->prefix . self::$tableTemplates, $wpdb->prefix . self::$tableUserSettings, $wpdb->prefix . self::$tableGeneratedLabelsData, $wpdb->prefix . self::$tableProfileMeta, $wpdb->prefix . self::$tableProfilesPermissions, ); $result = $wpdb->get_results("SHOW TABLES"); $tables = array(); foreach ($result as $value) { $tables[] = $value->$key; } if (count(array_diff($plTables, $tables)) > 0) { self::createTables(); } } catch (\Throwable $th) { } } public static function setupTables($network_wide) { global $wpdb; if (is_multisite() && $network_wide) { $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); foreach ($blog_ids as $blog_id) { switch_to_blog($blog_id); self::createTables(); restore_current_blog(); } } else { self::createTables(); } self::createTables(); } public static function createTables() { self::setupFormatsTables(); self::setupGeneratedLabelDataTable(); self::setupProfilesTable(); self::setupProfileMetaTable(); self::setupProfilesPermissionsTable(); self::setDefaultValues(); } protected static function setupFormatsTables() { global $wpdb; $tblPaperFormats = $wpdb->prefix . self::$tablePaperFormats; $sql = "CREATE TABLE `{$tblPaperFormats}` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `width` decimal(12,4) DEFAULT '0.0000', `height` decimal(12,4) DEFAULT '0.0000', `default` tinyint(1) NOT NULL DEFAULT '0', `units` varchar(10) DEFAULT NULL, `landscape` tinyint(1) DEFAULT 0 NOT NULL, `is_roll` tinyint(1) DEFAULT 0 NOT NULL, `is_editable` tinyint(1) DEFAULT 1 NOT NULL, `is_removed` tinyint(1) DEFAULT 0 NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); $tblSheetFormat = $wpdb->prefix . self::$tableLabelSheets; $sql = "CREATE TABLE `{$tblSheetFormat}` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `userId` BIGINT(20) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `width` decimal(12,4) DEFAULT NULL, `height` decimal(12,4) DEFAULT NULL, `around` decimal(12,4) DEFAULT NULL, `across` decimal(12,4) DEFAULT NULL, `marginLeft` decimal(12,4) DEFAULT NULL, `marginRight` decimal(12,4) DEFAULT NULL, `marginTop` decimal(12,4) DEFAULT NULL, `marginBottom` decimal(12,4) DEFAULT NULL, `aroundCount` int(11) DEFAULT NULL, `acrossCount` int(11) DEFAULT NULL, `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `paperId` int(11) unsigned DEFAULT '1' COMMENT 'linked to install', `default` tinyint(1) NOT NULL DEFAULT '0', `is_removed` tinyint(1) DEFAULT 0 NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); $tblUserSettings = $wpdb->prefix . self::$tableUserSettings; $sql = "CREATE TABLE `{$tblUserSettings}` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `userId` BIGINT(20) DEFAULT NULL, `param` varchar(255) DEFAULT NULL, `value` LONGTEXT DEFAULT NULL, `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); $tblDimension = $wpdb->prefix . self::$tableDimension; $sql = "CREATE TABLE `{$tblDimension}` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `label` varchar(255) DEFAULT NULL, `is_default` tinyint(1) DEFAULT NULL, `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); $dataDimension = require __DIR__ . '/../config/dimension.php'; $dataLabelsFormats = require __DIR__ . '/../config/labels.php'; $dataPaperFormats = require __DIR__ . '/../config/papers.php'; for ($i = 0; $i < count($dataDimension); ++$i) { $shortcode = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `{$tblDimension}` WHERE (`id` = %s);", array($dataDimension[$i]['id']) ) ); if (!$shortcode) { $wpdb->insert($tblDimension, $dataDimension[$i]); } } for ($i = 0; $i < count($dataPaperFormats); ++$i) { $paperFormat = $wpdb->get_row( $wpdb->prepare( " SELECT * FROM `{$tblPaperFormats}` WHERE (`id` = %s) ", array($dataPaperFormats[$i]['id']) ) ); if (!$paperFormat) { $wpdb->insert($tblPaperFormats, $dataPaperFormats[$i]); $insertId = $wpdb->insert_id; if (count($dataLabelsFormats) > 0) { foreach ($dataLabelsFormats as $label) { $label['userId'] = get_current_user_id(); if ($i + 1 == $label['paperId']) { $label['paperId'] = $insertId; $wpdb->insert($tblSheetFormat, $label); } } } } else { if ('1' === $paperFormat->default && '4' !== $paperFormat->id) { $wpdb->update( $tblPaperFormats, array( 'name' => $dataPaperFormats[$i]['name'], 'width' => $dataPaperFormats[$i]['width'], 'height' => $dataPaperFormats[$i]['height'], 'default' => $dataPaperFormats[$i]['default'], 'is_editable' => $dataPaperFormats[$i]['is_editable'], ), array('id' => $dataPaperFormats[$i]['id']) ); } elseif (4 === $dataPaperFormats[$i]['id']) { $wpdb->update( $tblPaperFormats, array( 'default' => $dataPaperFormats[$i]['default'], 'is_editable' => $dataPaperFormats[$i]['is_editable'], ), array('id' => $dataPaperFormats[$i]['id']) ); } } } } protected static function setupGeneratedLabelDataTable() { global $wpdb; $tbl = $wpdb->prefix . self::$tableGeneratedLabelsData; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE `{$tbl}` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) unsigned NOT NULL, `template_id` int(11) unsigned NOT NULL, `item_id` int(11) unsigned NOT NULL, `item_parent_id` int(11) unsigned NOT NULL, `order_item_id` int(11) unsigned NOT NULL, `profile_id` int(11) unsigned NOT NULL, `maker_class` varchar(1023) DEFAULT NULL, `barcode_type` varchar(255) DEFAULT NULL, `replacements` longtext, `barcode_image_xml` longtext, `item_title` varchar(255) DEFAULT '', `order_number` int(11) unsigned DEFAULT 0, PRIMARY KEY (`id`), KEY user_id_idx (`user_id`) ) $charset_collate"; dbDelta($sql); } protected static function setupProfilesTable() { global $wpdb; $tblProfiles = $wpdb->prefix . self::$tableProfiles; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE `{$tblProfiles}` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `templateId` int(10) DEFAULT NULL, `isDefault` tinyint(1) DEFAULT 0, `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `author_id` BIGINT(20) DEFAULT NULL, `parent_id` BIGINT(20) DEFAULT NULL, PRIMARY KEY (`id`) ) $charset_collate"; dbDelta($sql); } protected static function setupProfileMetaTable() { global $wpdb; $tbl = $wpdb->prefix . self::$tableProfileMeta; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE `{$tbl}` ( `meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `barcode_v4_profile_id` bigint(20) unsigned NOT NULL, `meta_key` varchar(255) DEFAULT NULL, `meta_value` longtext, PRIMARY KEY (`meta_id`) ) $charset_collate"; dbDelta($sql); } protected static function setupProfilesPermissionsTable() { global $wpdb; $tbl = $wpdb->prefix . self::$tableProfilesPermissions; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE `{$tbl}` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `profile_id` bigint(20) unsigned NOT NULL, `role` varchar(255) DEFAULT NULL, `button_slug` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) $charset_collate"; dbDelta($sql); } protected static function setDefaultValues() { (new \UkrSolution\WpLabelsPrinting4\Models\Profiles())->createUserDefaultProfileIfNotExists(); } }