get_col("SELECT blog_id FROM $wpdb->blogs"); // Create tables for each blog foreach ($blog_ids as $blog_id) { switch_to_blog($blog_id); self::createTables(); restore_current_blog(); } } else { self::createTables(); } self::createTables(); } /** * Create tables. */ public static function createTables() { self::setupFormatsTables(); self::setupTemplatesTable(); } /** * Setup formats tables. */ protected static function setupFormatsTables() { global $wpdb; $tblSheetFormat = $wpdb->prefix.'a4barcode_custom_formats'; $formatSheetTable = array('%s', '%f', '%f', '%d'); $tblPaperFormats = $wpdb->prefix.'a4barcode_paper_formats'; $formatPaperTable = array('%d', '%s', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%f', '%d', '%d', '%d', '%d'); // Check to see if the table exists already, if not, then create it $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', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); // Check to see if the table exists already, if not, then create it $sql = "CREATE TABLE `{$tblSheetFormat}` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `userId` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `width` decimal(12,4) DEFAULT NULL, `height` decimal(12,4) DEFAULT NULL, `arround` 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, `arroundCount` 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', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); $dataCustomFormat = file_get_contents(__DIR__.'/../config/formats.json'); $dataCustomFormat = json_decode($dataCustomFormat); // insert data for table page formats $dataPaperFormats = array(); $dataPaperFormats[] = array( 'name' => 'A4', 'width' => 210, 'height' => 297, 'default' => 1, ); $dataPaperFormats[] = array( 'name' => 'Letter', 'width' => 215.9, 'height' => 279.4, 'default' => 1, ); $dataPaperFormats[] = array( 'name' => 'P4', 'width' => 215.9, 'height' => 279.4, 'default' => 1, ); $dataPaperFormats[] = array( 'name' => 'Roll Label Printer', 'width' => 70, 'height' => 44, 'default' => 0, ); // Each preinstalled format for ($i = 0; $i < count($dataPaperFormats); ++$i) { $paperFormat = $wpdb->get_results( $wpdb->prepare("SELECT * FROM `{$tblPaperFormats}` WHERE `name` = %s", array($dataPaperFormats[$i]['name'])) ); // Client don't have this format if (!$paperFormat) { // Insert it in $wpdb->insert($tblPaperFormats, $dataPaperFormats[$i], $formatSheetTable); $insertId = $wpdb->insert_id; // if there is data to insert into the table if (count($dataCustomFormat) > 0) { // loop through the data and insert into the table foreach ($dataCustomFormat as $itemDataCustomFormat) { $format = [ 'userId' => get_current_user_id(), 'name' => $itemDataCustomFormat->name, 'width' => $itemDataCustomFormat->width, 'height' => $itemDataCustomFormat->height, 'arround' => $itemDataCustomFormat->arround, 'across' => $itemDataCustomFormat->across, 'marginLeft' => $itemDataCustomFormat->marginLeft, 'marginRight' => $itemDataCustomFormat->marginRight, 'marginTop' => $itemDataCustomFormat->marginTop, 'marginBottom' => $itemDataCustomFormat->marginBottom, 'arroundCount' => $itemDataCustomFormat->arroundCount, 'acrossCount' => $itemDataCustomFormat->acrossCount, 'paperId' => $itemDataCustomFormat->paperId, 'default' => 0, ]; // Find and put sheet formats for new paper format if ($i + 1 == $format['paperId']) { $format['paperId'] = $insertId; $wpdb->insert($tblSheetFormat, $format, $formatPaperTable); } } } } } } /** * Setup custom templates table. */ protected static function setupTemplatesTable() { global $wpdb; $tbl = $wpdb->prefix.'a4barcode_custom_templates'; $format = array('%s', '%s', '%s', '%d', '%d', '%d', '%d', '%d', '%s'); // Check to see if the table exists already, if not, then create it $sql = "CREATE TABLE `{$tbl}` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `slug` varchar(255) DEFAULT NULL, `template` TEXT, `is_active` tinyint(1) NOT NULL DEFAULT '0', `is_default` tinyint(1) NOT NULL DEFAULT '0', `is_base` tinyint(1) NOT NULL DEFAULT '0', `height` decimal(12,2) DEFAULT 37 NULL, `width` decimal(12,2) DEFAULT 70 NULL, `uol` varchar(16) DEFAULT 'mm' NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; dbDelta($sql); $data = require __DIR__.'/../config/templates.php'; // Insert data into table foreach ($data as $datum) { // Replace image url path $datum['template'] = preg_replace('/\[logo_img_url]/', plugin_dir_url(dirname(__FILE__)).'assets/img/amazon-200x113.jpg', $datum['template']); // Check for datum already exists. $datumExists = $wpdb->get_results( $wpdb->prepare("SELECT * FROM `{$tbl}` WHERE `slug` = %s AND `is_default` = 1", array($datum['slug'])) ); // If row already exists - update it, otherwise - insert. if ($datumExists) { // Update only tmpl content $wpdb->update( $tbl, array( 'name' => $datum['name'], 'template' => $datum['template'], 'width' => $datum['width'], 'height' => $datum['height'], 'uol' => $datum['uol'], ), array('slug' => $datum['slug'], 'is_default' => 1), // where array('%s', '%s', '%s', '%s', '%s'), array('%s', '%d') ); } else { // Insert $wpdb->insert($tbl, $datum, $format); } } } }