HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //var/www/innodrive/migrations/search_fixer.php
<?php

/**
 * @var $wpdb;
 *
 */

//define('APPLICATION_ENV', 'development');
define( 'SHORTINIT', true );
define( 'BASE_PATH', __DIR__ . '/../public/' );
require(BASE_PATH . 'wp-load.php' );
require(BASE_PATH . 'wp-content/plugins/custom-search/custom-search.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Product.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Brand.php');
require(BASE_PATH . 'wp-content/plugins/import/class/Category.php');

$searchResults = $wpdb->get_results('SELECT ts.* FROM top_suggestions ts 
    WHERE ts.type = "products"');

$updated = $removed = 0;

$wpdb->query('DELETE FROM top_suggestions WHERE url IS NULL OR title IS NULL');

if (count($searchResults)) {
    echo 'Current search results in products: ' . count($searchResults) . PHP_EOL;
    foreach($searchResults as $row) {
        if (!empty($row->title)) {
            $sku = mb_strpos($row->title, ' ') ? mb_substr($row->title, 0, mb_strpos($row->title, ' ')) : $row->title;
            $products = Product::search($sku, $row->lang);

            if (!empty($products[0])) {
                $wpdb->update('top_suggestions', [
                    'url' => $products[0]['url'],
                ], [
                    'id' => $row->id
                ]);
                $updated++;
            } else {
                $wpdb->delete('top_suggestions', [
                    'id' => $row->id
                ]);
                $removed++;
            }
        } else {
            $wpdb->delete('top_suggestions', [
                'id' => $row->id
            ]);
            $removed++;
        }
    }
}

echo PHP_EOL;
echo 'Updated: ' . $updated . PHP_EOL;
echo 'Removed: ' . $removed . PHP_EOL;

echo PHP_EOL;
echo 'THE END' . PHP_EOL;