prefix . 'akkedli_v2_outbox'; } public static function maybe_install() { if (self::ready()) { return true; } return self::install(); } public static function ready() { return (int) get_option(self::OPTION, 0) >= self::VERSION && self::schema_ready(); } public static function install() { global $wpdb; if ((int) get_option(self::OPTION, 0) > self::VERSION) { return self::schema_ready(); } if (!function_exists('dbDelta')) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; } $table = self::table(); $collation = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, event_id char(64) NOT NULL, order_key char(64) NOT NULL, operation varchar(12) NOT NULL, envelope longtext NULL, state varchar(24) NOT NULL, capture_attempts tinyint(3) unsigned NOT NULL DEFAULT 0, attempts tinyint(3) unsigned NOT NULL DEFAULT 0, next_attempt_gmt datetime NOT NULL, lease_owner char(64) NULL, lease_expires_gmt datetime NULL, last_outcome varchar(32) NULL, created_gmt datetime NOT NULL, updated_gmt datetime NOT NULL, completed_gmt datetime NULL, PRIMARY KEY (id), UNIQUE KEY event_id (event_id), KEY due (state,next_attempt_gmt,id), KEY lease (state,lease_expires_gmt,id), KEY order_fifo (order_key,id) ) {$collation};"; dbDelta($sql); if (!self::schema_ready()) { return false; } update_option(self::OPTION, self::VERSION, false); return (int) get_option(self::OPTION, 0) === self::VERSION; } private static function schema_ready() { global $wpdb; $table = self::table(); if (!self::table_exists()) { return false; } $columns = $wpdb->get_col("SHOW COLUMNS FROM {$table}", 0); $rows = $wpdb->get_results("SHOW INDEX FROM {$table}", ARRAY_A); $indexes = array(); foreach (is_array($rows) ? $rows : array() as $row) { if (!isset($row['Key_name'], $row['Non_unique'], $row['Seq_in_index'], $row['Column_name'])) { return false; } $indexes[$row['Key_name'] . ':' . $row['Non_unique'] . ':' . $row['Seq_in_index']] = $row['Column_name']; } $required_columns = array('id', 'event_id', 'order_key', 'operation', 'envelope', 'state', 'capture_attempts', 'attempts', 'next_attempt_gmt', 'lease_owner', 'lease_expires_gmt', 'last_outcome', 'created_gmt', 'updated_gmt', 'completed_gmt'); $required_indexes = array('PRIMARY:0:1' => 'id', 'event_id:0:1' => 'event_id', 'due:1:1' => 'state', 'due:1:2' => 'next_attempt_gmt', 'due:1:3' => 'id', 'lease:1:1' => 'state', 'lease:1:2' => 'lease_expires_gmt', 'lease:1:3' => 'id', 'order_fifo:1:1' => 'order_key', 'order_fifo:1:2' => 'id'); return is_array($columns) && !array_diff($required_columns, $columns) && !array_diff_assoc($required_indexes, $indexes); } private static function table_exists() { global $wpdb; $table = self::table(); return $table === $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($table))); } }