Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active October 15, 2025 19:34
Show Gist options
  • Save wpmudev-sls/ce742b2268f9231e1dcfca116581ee6b to your computer and use it in GitHub Desktop.
Save wpmudev-sls/ce742b2268f9231e1dcfca116581ee6b to your computer and use it in GitHub Desktop.
[SmartCrawl Pro] Fix corrupted redirects
<?php
/**
* Plugin Name: [SmartCrawl Pro] Fix corrupted redirects
* Description: Fixes corrupted SmartCrawl redirect data in the database.
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-7307
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*
* @package WPMUDEV_SmartCrawl_FixCorruptedRedirects
*/
// phpcs:disable WordPress.DB.DirectDatabaseQuery
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
add_action(
'admin_init',
function () {
global $wpdb;
if ( ! defined( 'SMARTCRAWL_VERSION' ) ) {
return;
}
$page = filter_input( INPUT_GET, 'page' );
if ( 'wds-advanced' !== $page ) {
return;
}
$cooldown = get_transient( 'smartcrawl_redirects_cooldown' );
// Check if table exists.
if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s;', $wpdb->prefix . 'smartcrawl_redirects' ) ) ) {
return array();
}
$redirects = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}smartcrawl_redirects", OBJECT_K );
// No redirects? Abort.
if ( empty( $redirects ) || ! is_array( $redirects ) ) {
return;
}
$to_update = array();
foreach ( $redirects as &$r ) {
if ( ! isset( $r->rules ) ) {
continue;
}
$rules = json_decode( $r->rules, true );
if ( false === $rules || is_array( $rules ) ) {
continue;
}
// Remove non-array value.
$to_update[] = $r->id;
}
$count = count( $to_update );
if ( 0 === $count ) {
add_action(
'admin_notices',
function () use ( $cooldown ) {
$remaining_minutes = round( ( $cooldown - time() ) / 60 );
?>
<div class="notice notice-info">
<p><strong>SmartCrawl redirects</strong>: No corrupted data were found. You can remove the snippet from <code><?php echo esc_html( __FILE__ ); ?></code></p>
</div>
<?php
}
);
return;
}
if ( $cooldown ) {
add_action(
'admin_notices',
function () use ( $cooldown ) {
$remaining_minutes = round( ( $cooldown - time() ) / 60 );
?>
<div class="notice notice-info">
<p><strong>SmartCrawl redirects</strong>: Please wait <?php echo intval( $remaining_minutes ); ?> minute(s) and refresh the page.</p>
</div>
<?php
}
);
return;
}
$ids = implode( ',', array_map( 'intval', $to_update ) );
try {
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->prefix}smartcrawl_redirects SET rules = %s WHERE id IN ($ids)",
'[]'
)
);
set_transient( 'smartcrawl_redirects_cooldown', time() + 5 * MINUTE_IN_SECONDS, 5 * MINUTE_IN_SECONDS );
} catch ( Exception $e ) {
// Error notice.
add_action(
'admin_notices',
function () use ( $e ) {
?>
<div class="notice notice-error">
<p><strong>SmartCrawl redirects</strong>: There was an error when trying to fix the database. Please contact support and provide the following error message: <?php echo esc_html( $e->getMessage() ); ?></p>
</div>
<?php
}
);
return;
}
// Success notice.
add_action(
'admin_notices',
function () use ( $count ) {
?>
<div class="notice notice-success">
<p><strong>SmartCrawl redirects</strong>: <?php echo intval( $count ); ?> redirect(s) were repaired. You can remove the snippet now.</p>
</div>
<?php
}
);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment