This commit is contained in:
Javier Casares 2026-03-02 14:31:34 +00:00
commit 2c300652a6
9 changed files with 230 additions and 82 deletions

View file

@ -89,11 +89,15 @@ class Robotstxt_Mlp2_Mlp5_Admin_Page {
if ( ! current_user_can( self::CAPABILITY ) ) {
wp_die( esc_html__( 'You do not have permission to perform this action.', 'robotstxt-mlp2-mlp5' ) );
}
$result = Robotstxt_Mlp2_Mlp5_Table_Preparer::rename_to_backup();
$args = array( 'renamed' => count( $result['renamed'] ) );
$result = Robotstxt_Mlp2_Mlp5_Table_Preparer::rename_to_backup();
$sitemeta_renamed = Robotstxt_Mlp2_Mlp5_Table_Preparer::rename_mlp2_sitemeta();
$args = array( 'renamed' => count( $result['renamed'] ) );
if ( ! empty( $result['errors'] ) ) {
$args['rename_errors'] = count( $result['errors'] );
}
if ( $sitemeta_renamed > 0 ) {
$args['sitemeta_renamed'] = $sitemeta_renamed;
}
wp_safe_redirect( self::page_url( $args ) );
exit;
}
@ -106,11 +110,15 @@ class Robotstxt_Mlp2_Mlp5_Admin_Page {
if ( ! current_user_can( self::CAPABILITY ) ) {
wp_die( esc_html__( 'You do not have permission to perform this action.', 'robotstxt-mlp2-mlp5' ) );
}
$result = Robotstxt_Mlp2_Mlp5_Table_Preparer::restore_from_backup();
$args = array( 'restored' => count( $result['restored'] ) );
$result = Robotstxt_Mlp2_Mlp5_Table_Preparer::restore_from_backup();
$sitemeta_restored = Robotstxt_Mlp2_Mlp5_Table_Preparer::restore_mlp2_sitemeta();
$args = array( 'restored' => count( $result['restored'] ) );
if ( ! empty( $result['errors'] ) ) {
$args['restore_errors'] = count( $result['errors'] );
}
if ( $sitemeta_restored > 0 ) {
$args['sitemeta_restored'] = $sitemeta_restored;
}
wp_safe_redirect( self::page_url( $args ) );
exit;
}
@ -423,6 +431,12 @@ class Robotstxt_Mlp2_Mlp5_Admin_Page {
/* translators: %d: number of tables that could not be renamed */
printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( sprintf( _n( '%d table could not be renamed. Check database permissions.', '%d tables could not be renamed. Check database permissions.', $count, 'robotstxt-mlp2-mlp5' ), $count ) ) ); // phpcs:ignore WordPress.WP.I18n.MismatchedPlaceholders
}
if ( isset( $_GET['sitemeta_renamed'] ) ) {
printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html__( 'MLP2 sitemeta backed up. MultilingualPress 5 can now activate cleanly.', 'robotstxt-mlp2-mlp5' ) );
}
if ( isset( $_GET['sitemeta_restored'] ) ) {
printf( '<div class="notice notice-success is-dismissible"><p>%s</p></div>', esc_html__( 'MLP2 sitemeta restored to original keys.', 'robotstxt-mlp2-mlp5' ) );
}
if ( isset( $_GET['restored'] ) ) {
$count = (int) $_GET['restored'];
/* translators: %d: number of restored tables */

View file

@ -80,26 +80,36 @@ class Robotstxt_Mlp2_Mlp5_Step_Cleanup {
/**
* Delete MLP2-related entries from wp_sitemeta.
*
* Removes all sitemeta keys whose name starts with 'mlp_' (MLP2 prefix).
* Removes sitemeta keys matching 'mlp_*' (MLP2 prefix) and
* 'mlp2bak_inpsyde_multilingual*' (backed-up MLP2 sitemeta).
*
* @return int Number of keys deleted.
*/
public static function remove_mlp2_sitemeta(): int {
global $wpdb;
$like = $wpdb->esc_like( 'mlp_' ) . '%';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$keys = $wpdb->get_col(
$wpdb->prepare(
"SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s AND site_id = %d",
$like,
get_current_network_id()
)
$patterns = array(
$wpdb->esc_like( 'mlp_' ) . '%',
$wpdb->esc_like( Robotstxt_Mlp2_Mlp5_Table_Preparer::SITEMETA_BACKUP_PREFIX ) . '%',
);
$keys = array();
foreach ( $patterns as $like ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$found = $wpdb->get_col(
$wpdb->prepare(
"SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE %s AND site_id = %d",
$like,
get_current_network_id()
)
);
if ( is_array( $found ) ) {
$keys = array_merge( $keys, $found );
}
}
$deleted = 0;
foreach ( (array) $keys as $key ) {
foreach ( array_unique( $keys ) as $key ) {
if ( delete_site_option( $key ) ) {
++$deleted;
}

View file

@ -84,7 +84,8 @@ class Robotstxt_Mlp2_Mlp5_Table_Preparer {
}
/**
* Return all tables in the database with the original MLP2 prefix.
* Return all tables in the database with the original MLP2 prefix,
* excluding tables that belong to this plugin.
*
* @return string[] Array of full table names.
*/
@ -96,7 +97,21 @@ class Robotstxt_Mlp2_Mlp5_Table_Preparer {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', $like ) );
return is_array( $tables ) ? $tables : array();
if ( ! is_array( $tables ) ) {
return array();
}
$prefix = $wpdb->base_prefix . self::ORIGINAL_FRAGMENT;
return array_values(
array_filter(
$tables,
static function ( string $table ) use ( $prefix ): bool {
$suffix = substr( $table, strlen( $prefix ) );
return ! in_array( $suffix, self::PLUGIN_TABLE_SUFFIXES, true );
}
)
);
}
/**
@ -115,6 +130,73 @@ class Robotstxt_Mlp2_Mlp5_Table_Preparer {
return is_array( $tables ) ? $tables : array();
}
/**
* Sitemeta key prefix used by MLP2 that blocks MLP5 activation.
*
* @var string
*/
const SITEMETA_ORIGINAL_PREFIX = 'inpsyde_multilingual';
/**
* Prefix prepended to MLP2 sitemeta keys when backing them up.
*
* @var string
*/
const SITEMETA_BACKUP_PREFIX = 'mlp2bak_inpsyde_multilingual';
/**
* Rename MLP2 blocking sitemeta keys to backup names.
*
* MLP5's SystemChecker detects any sitemeta key starting with
* `inpsyde_multilingual` and refuses to activate. This method renames
* those keys to `mlp2bak_inpsyde_multilingual*` so MLP5 can activate
* cleanly while the original data is preserved for restore or cleanup.
*
* @return int Number of sitemeta rows renamed.
*/
public static function rename_mlp2_sitemeta(): int {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$renamed = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->sitemeta} SET meta_key = CONCAT( %s, SUBSTR( meta_key, %d ) ) WHERE meta_key LIKE %s AND site_id = %d",
'mlp2bak_',
1,
$wpdb->esc_like( self::SITEMETA_ORIGINAL_PREFIX ) . '%',
get_current_network_id()
)
);
return is_int( $renamed ) ? $renamed : 0;
}
/**
* Restore backup sitemeta keys to their original MLP2 names.
*
* Reverses rename_mlp2_sitemeta(). Only use this before MLP5 has been
* activated, as restoring these keys will block MLP5 activation again.
*
* @return int Number of sitemeta rows restored.
*/
public static function restore_mlp2_sitemeta(): int {
global $wpdb;
$prefix_length = strlen( 'mlp2bak_' ) + 1; // SUBSTR is 1-indexed.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$restored = $wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->sitemeta} SET meta_key = SUBSTR( meta_key, %d ) WHERE meta_key LIKE %s AND site_id = %d",
$prefix_length,
$wpdb->esc_like( self::SITEMETA_BACKUP_PREFIX ) . '%',
get_current_network_id()
)
);
return is_int( $restored ) ? $restored : 0;
}
/**
* Whether any original MLP2 tables exist in the database.
*