Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thankyou @spoki

    I think you can close the issue,

    bye

    We have checked @dreamsnet hint, and we found out more.

    That table is constantly growing, reaching millions of rows.

    By checking the mission of that table, it seems that it only contains settings of spoki, for example:

    plugin_activated: true / false

    We found out that function set_spoki_setting_by_meta, instead of editing the meta, is constantly appending the row.

    File:

    wp-content/plugins/spoki/modules/abandoned-carts/spoki-abandoned-carts.php

    We made a fix that seems working:

    Old code (with the bug):

    	function set_spoki_setting_by_meta($meta_key, $meta_value)
    {
    global $wpdb;
    $spoki_setting_tb = $wpdb->prefix . SPOKI_SETTING_TABLE;

    // Check if the table exists before attempting to insert data.
    if ($wpdb->get_var("SHOW TABLES LIKE '$spoki_setting_tb'") !== $spoki_setting_tb) {
    error_log('Error: Table does not exist: ' . $spoki_setting_tb);
    return;
    }

    $wpdb->insert(
    $spoki_setting_tb,
    array(
    'meta_key' => $meta_key,
    'meta_value' => $meta_value
    ),
    array('%s', '%s')
    );
    }

    New code (with the bugfix):

    	function set_spoki_setting_by_meta($meta_key, $meta_value) {
    // Bugfix by Michele Capicchioni - TheSpace RSM

    global $wpdb;
    $spoki_setting_tb = $wpdb->prefix . SPOKI_SETTING_TABLE;

    // Check if the table exists before attempting to insert or update data.
    if ($wpdb->get_var("SHOW TABLES LIKE '$spoki_setting_tb'") !== $spoki_setting_tb) {
    error_log('Error: Table does not exist: ' . $spoki_setting_tb);
    return;
    }

    // Check if the meta_key already exists in the table.
    $existing_value = $wpdb->get_var($wpdb->prepare(
    "SELECT meta_value FROM $spoki_setting_tb WHERE meta_key = %s",
    $meta_key
    ));

    if ($existing_value !== null) {
    // If the meta_key exists, update the value.
    $wpdb->update(
    $spoki_setting_tb,
    array('meta_value' => $meta_value), // new value
    array('meta_key' => $meta_key), // condition
    array('%s'), // format for new value
    array('%s') // format for condition
    );
    } else {
    // If the meta_key does not exist, insert a new record.
    $wpdb->insert(
    $spoki_setting_tb,
    array(
    'meta_key' => $meta_key,
    'meta_value' => $meta_value
    ),
    array('%s', '%s')
    );
    }
    }

    As you can see, now it checks if the row already exists and updated it.

    So the table stops growing so much (causing problems of space and performance).

    I remain at your disposal in case of doubts.

    Michele Capicchioni – The Space RSM

    • This reply was modified 2 months, 4 weeks ago by thespacersm. Reason: More informations about the file
Viewing 2 replies - 1 through 2 (of 2 total)