Skip to content

Commit

Permalink
fix add filters, for tests
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Foti <foti.giuseppe@gmail.com>
  • Loading branch information
MocioF committed Jan 10, 2024
1 parent 8b47908 commit 92ed4d3
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<' ) ) {
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 92ed4d3

Please # to comment.