From 92ed4d37820eca5cf45d679a5b9caf60ad2f2fdb Mon Sep 17 00:00:00 2001 From: Giuseppe Foti Date: Wed, 10 Jan 2024 19:00:52 +0100 Subject: [PATCH] fix add filters, for tests Signed-off-by: Giuseppe Foti --- settings.php | 58 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/settings.php b/settings.php index 0f7ecfb..5e0bbb8 100644 --- a/settings.php +++ b/settings.php @@ -107,7 +107,7 @@ function gcmi_upgrade() { $new_ver = GCMI_VERSION; if ( $old_ver === $new_ver ) { - return; + return; } if ( version_compare( $old_ver, '2.2.0', '<' ) ) { @@ -133,18 +133,54 @@ function gcmi_upgrade() { */ function gcmi_add_index_on_tables(): void { global $wpdb; - $wpdb->query( - $wpdb->prepare( - 'ALTER TABLE `%1$s` ADD INDEX(`i_cod_comune`);', - GCMI_TABLE_PREFIX . 'comuni_attuali' - ) - ); - $wpdb->query( + if ( false === gcmi_index_exist( GCMI_TABLE_PREFIX . 'comuni_attuali', 'i_cod_comune' ) ) { + $wpdb->query( + $wpdb->prepare( + 'ALTER TABLE `%1$s` ADD INDEX(`i_cod_comune`);', + GCMI_TABLE_PREFIX . 'comuni_attuali' + ) + ); + } + if ( false === gcmi_index_exist( GCMI_TABLE_PREFIX . 'comuni_soppressi', 'i_cod_comune' ) ) { + $wpdb->query( + $wpdb->prepare( + 'ALTER TABLE `%1$s` ADD INDEX(`i_cod_comune`);', + GCMI_TABLE_PREFIX . 'comuni_soppressi' + ) + ); + } +} + +/** + * Controlla se su un campo di una tabella è presente già un indice + * + * @param string $table_name + * @param string $field_name + * @since 2.2.0 + * @return bool + */ +function gcmi_index_exist( $table_name, $field_name ) { + global $wpdb; + if ( function_exists( 'str_starts_with' ) && false === str_starts_with( $table_name, GCMI_TABLE_PREFIX ) ) { + $table_name = GCMI_TABLE_PREFIX . $table_name; + } + if ( ! function_exists( 'str_starts_with' ) && 0 === strpos( $table_name, GCMI_TABLE_PREFIX ) ) { + $table_name = GCMI_TABLE_PREFIX . $table_name; + } + + $index_fields = $wpdb->get_col( $wpdb->prepare( - 'ALTER TABLE `%1$s` ADD INDEX(`i_cod_comune`);', - GCMI_TABLE_PREFIX . 'comuni_soppressi' - ) + 'SHOW INDEX FROM `%1$s`', + $table_name + ), + 4 // Column_name . ); + $unique = array_unique( $index_fields ); + + if ( in_array( $field_name, $unique ) ) { + return true; + } + return false; } /**