current_version); } } /** * {@inheritdoc} */ public static function table_name($with_prefix = true) { $table_name = static::base_table_name(); if ($with_prefix) { global $wpdb; $table_name = $wpdb->prefix . $table_name; } return $table_name; } /** * {@inheritdoc} */ public static function uid_column(): string { $primary_columns = static::primary_columns(); return array_values($primary_columns)[0]; } /** * Gets the primary columns for the table. * * @since 3.0.0 * * @return array The primary columns for the table. */ public static function primary_columns(): array { return static::get_current_schema()->get_primary_key()->get_columns(); } /** * {@inheritdoc} */ public function update() { // @phpstan-ignore-next-line require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $sql = $this->get_sql(); $results = []; try { /** * Hookable action before the table schema has updated. * * @since 3.0.0 * * @param string $table_name The prefix-less table name. * @param Table $table The table object. */ do_action('stellarwp_schema_table_before_update_' . static::get_schema_slug(), static::table_name(), $this); /** * Hookable action before the table schema has updated. * * @since 3.0.0 * * @param string $table_name The prefix-less table name. * @param Table $table The table object. */ do_action('stellarwp_schema_table_before_update', static::table_name(), $this); $results = (array) $this->db::delta($sql); $this->archive_previous_version(); $this->sync_stored_version(); $results = $this->after_update($results); /** * Hookable action after the table schema has updated. * * @since 3.0.0 * * @param string $table_name The prefix-less table name. * @param array $results The results of the table schema updates. * @param Table $table The table object. */ do_action('stellarwp_schema_table_after_update_' . static::get_schema_slug(), static::table_name(), $results, $this); /** * Hookable action after the table schema has updated. * * @since 3.0.0 * * @param string $table_name The prefix-less table name. * @param array $results The results of the table schema updates. * @param Table $table The table object. */ do_action('stellarwp_schema_table_after_update', static::table_name(), $results, $this); } catch (Exception $e) { if (!has_action('stellarwp_schema_table_update_error_' . static::get_schema_slug()) && !has_action('stellarwp_schema_table_update_error')) { throw $e; } /** * Hookable action after the table schema has failed to update. * * @since 3.0.0 * * @param Exception $e The exception. * @param Table $table The table object. */ do_action('stellarwp_schema_table_update_error_' . static::get_schema_slug(), $e, $this); /** * Hookable action after the table schema has failed to update. * * @since 3.0.0 * * @param Exception $e The exception. * @param Table $table The table object. */ do_action('stellarwp_schema_table_update_error', $e, $this); } return $results; } /** * Checks if a foreign key exists on a table. * * @since 1.1.3 * * @param string $foreign_key The foreign key to check for. * @param string $table_name The table name to check. Defaults to the current table. * * @return bool Whether the foreign key exists on the table. */ public function has_foreign_key(string $foreign_key, string $table_name = ''): bool { $table_name = $table_name ?: static::table_name(); $count = $this->db::table($this->db::raw('information_schema.statistics'))->whereRaw('WHERE TABLE_SCHEMA = DATABASE()')->where('TABLE_NAME', $table_name)->where('INDEX_NAME', $foreign_key)->count(); return $count >= 1; } /** * {@inheritdoc} */ public static function transform_from_array(array $result_array) { return $result_array; } }