Skip to content

Commit acdf5df

Browse files
committed
Fix admin page check and update database schema
- Updated the method name for checking admin pages from `is_cof_admin_page` to `is_cofld_admin_page` for consistency. - Bumped the database version from 1.0.0 to 1.1.0 to reflect schema changes. - Added migration functions to rename database tables and update option keys from the old `cof_` prefix to the new `cofld_` prefix during the upgrade process. - Called the database update check in the plugin initialization hooks.
1 parent a640ff8 commit acdf5df

5 files changed

Lines changed: 66 additions & 5 deletions

File tree

admin/src/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import type { Fieldset, Field, LocationType } from '../types';
88

99
const getConfig = () => ({
10-
apiUrl: window.cofldAdmin?.restUrl || '/wp-json/openfields/v1',
10+
apiUrl: window.cofldAdmin?.restUrl || '/wp-json/codeideal-open-fields/v1',
1111
nonce: window.cofldAdmin?.nonce || '',
1212
});
1313

plugin/assets/admin/js/admin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugin/includes/class-cofld-assets.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private function __construct() {
5959
*/
6060
public function admin_scripts( $hook ) {
6161
// Only load on Codeideal Open Fields admin pages.
62-
if ( ! $this->is_cof_admin_page( $hook ) ) {
62+
if ( ! $this->is_cofld_admin_page( $hook ) ) {
6363
return;
6464
}
6565

@@ -189,7 +189,7 @@ public function meta_box_scripts( $hook ) {
189189
* @param string $hook Current admin page hook.
190190
* @return bool
191191
*/
192-
private function is_cof_admin_page( $hook ) {
192+
private function is_cofld_admin_page( $hook ) {
193193
$cofld_pages = array(
194194
'toplevel_page_codeideal-open-fields',
195195
'open-fields_page_codeideal-open-fields-settings',

plugin/includes/class-cofld-installer.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ class COFLD_Installer {
2323
/**
2424
* Database version.
2525
*
26+
* Bump this when the DB schema changes or a migration is needed.
27+
*
2628
* @var string
2729
*/
28-
const DB_VERSION = '1.0.0';
30+
const DB_VERSION = '1.1.0';
2931

3032
/**
3133
* Activate the plugin.
@@ -190,10 +192,66 @@ public static function needs_db_update() {
190192
*/
191193
public static function maybe_update_db() {
192194
if ( self::needs_db_update() ) {
195+
self::migrate_table_prefix();
196+
self::migrate_option_keys();
193197
self::create_tables();
194198
}
195199
}
196200

201+
/**
202+
* Migrate database tables from old `cof_` prefix to `cofld_`.
203+
*
204+
* Runs once during the 1.0.0 → 1.1.0 DB upgrade. If the old tables exist
205+
* and the new ones do not, this renames them in-place to preserve data.
206+
*
207+
* @since 0.3.0
208+
*/
209+
private static function migrate_table_prefix() {
210+
global $wpdb;
211+
212+
$old_tables = array(
213+
$wpdb->prefix . 'cof_fieldsets' => $wpdb->prefix . 'cofld_fieldsets',
214+
$wpdb->prefix . 'cof_fields' => $wpdb->prefix . 'cofld_fields',
215+
$wpdb->prefix . 'cof_locations' => $wpdb->prefix . 'cofld_locations',
216+
);
217+
218+
foreach ( $old_tables as $old_name => $new_name ) {
219+
// Only rename if old table exists and new one doesn't.
220+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
221+
$old_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $old_name ) );
222+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
223+
$new_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $new_name ) );
224+
225+
if ( $old_exists && ! $new_exists ) {
226+
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
227+
$wpdb->query( "RENAME TABLE `{$old_name}` TO `{$new_name}`" );
228+
}
229+
}
230+
}
231+
232+
/**
233+
* Migrate option keys from old `cof_` prefix to `cofld_`.
234+
*
235+
* @since 0.3.0
236+
*/
237+
private static function migrate_option_keys() {
238+
global $wpdb;
239+
240+
$old_options = array(
241+
'cof_settings' => 'cofld_settings',
242+
'cof_version' => 'cofld_version',
243+
'cof_db_version' => 'cofld_db_version',
244+
);
245+
246+
foreach ( $old_options as $old_key => $new_key ) {
247+
$old_value = get_option( $old_key );
248+
if ( false !== $old_value && false === get_option( $new_key ) ) {
249+
update_option( $new_key, $old_value );
250+
delete_option( $old_key );
251+
}
252+
}
253+
}
254+
197255
/**
198256
* Get table names.
199257
*

plugin/includes/class-cofld.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ private function includes() {
9292
private function init_hooks() {
9393
add_action( 'init', array( $this, 'init' ), 0 );
9494

95+
// Check for database updates (e.g. table prefix migration).
96+
COFLD_Installer::maybe_update_db();
97+
9598
// Initialize REST API early - rest_api_init fires before init on REST requests.
9699
COFLD_REST_API::instance();
97100
}

0 commit comments

Comments
 (0)