• Hi!

    I noticed that update_option() is being called on each page load even when no changes are made. To save some database resources, I’ve slightly refactored the code to use a flag ($needs_update).

    This way, the DB is only touched if a setting is actually missing or updated. I also cleaned up the repetitive checks with a quick loop.

    Here is the suggested change for lines 201-275

    // Check if we have options in DB. Write defaults if not.
    $g_options = get_option('quicklatex');

    if ($g_options === false) {
    // Write default options to DB for the first time
    update_option('quicklatex', $def_options);
    $g_options = $def_options;
    } else {
    // This flag tracks if any changes were made during the checks
    $needs_update = false;

    // 1. AMS Packages - Check if preamble needs correction
    if (!isset($g_options['is_preamble_corrected'])) {
    $preamble = trim($g_options['preamble'] ?? '');

    if ($preamble == '') {
    // Setup default AMS packages if preamble is empty
    $preamble = "\\usepackage{amsmath}\n\\usepackage{amsfonts}\n\\usepackage{amssymb}\n";
    } else {
    // Check for individual packages and prepend if missing
    if (strpos($preamble, "amssymb") === false) $preamble = "\\usepackage{amssymb}\n" . $preamble;
    if (strpos($preamble, "amsfonts") === false) $preamble = "\\usepackage{amsfonts}\n" . $preamble;
    if (strpos($preamble, "amsmath") === false) $preamble = "\\usepackage{amsmath}\n" . $preamble;
    }

    $g_options['preamble'] = $preamble;
    $g_options['is_preamble_corrected'] = 1;
    $needs_update = true;
    }

    // 2. Default settings for missing fields
    $fields_to_check = [
    'displayed_equations_align' => 0,
    'eqno_align' => 0,
    'latex_syntax' => 0,
    'exclude_dollars' => 0,
    ];

    foreach ($fields_to_check as $key => $default_value) {
    if (!isset($g_options[$key])) {
    $g_options[$key] = $default_value;
    $needs_update = true;
    }
    }

    // 3. Image Format Logic
    if (!isset($g_options['image_format'])) {
    $g_options['image_format'] = 1; // Set default to PNG
    $needs_update = true;
    } elseif ($g_options['image_format'] == 0) {
    // Migration: Switch from GIF (0) to PNG (1)
    $g_options['image_format'] = 1;
    $needs_update = true;
    }

    // Final Step: Only update the database if a change was actually made
    if ($needs_update) {
    update_option('quicklatex', $g_options);
    }
    }

    Bests,
    Lena

You must be logged in to reply to this topic.