Plugin Directory

Changeset 3489019


Ignore:
Timestamp:
03/23/2026 12:59:45 PM (10 days ago)
Author:
urkekg
Message:

Fix update routine 10 on sites with 10k+ posts

Location:
head-footer-code
Files:
32 added
4 edited

Legend:

Unmodified
Added
Removed
  • head-footer-code/trunk/classes/techwebux/hfc/class-main.php

    r3479175 r3489019  
    106106        }
    107107
     108        // Lock initiated updating
     109        if ( get_transient( 'auhfc_updating' ) ) {
     110            return;
     111        }
     112        set_transient( 'auhfc_updating', true, 5 * MINUTE_IN_SECONDS );
     113
    108114        // Require update script and trigger update function.
    109115        require_once $this->plugin->dir . '/update.php';
    110116        auhfc_update();
     117
     118        // Remove updating lock
     119        delete_transient( 'auhfc_updating' );
    111120    }
    112121
  • head-footer-code/trunk/head-footer-code.php

    r3480015 r3489019  
    1212 * Plugin URI:  https://urosevic.net/wordpress/plugins/head-footer-code/
    1313 * Description: Easy add site-wide, category or article specific custom code before the closing <strong>&lt;/head&gt;</strong> and <strong>&lt;/body&gt;</strong> or after opening <strong>&lt;body&gt;</strong> HTML tag.
    14  * Version:     1.5.5
     14 * Version:     1.5.6
    1515 * Author:      Aleksandar Urošević
    1616 * Author URI:  https://urosevic.net/
     
    3131define( 'HFC__MIN_WP', '5.2' );
    3232
    33 define( 'HFC_VER', '1.5.5' );
     33define( 'HFC_VER', '1.5.6' );
    3434define( 'HFC_VER_DB', '11' );
    3535define( 'HFC_FILE', __FILE__ );
  • head-footer-code/trunk/readme.txt

    r3480015 r3489019  
    66Requires at least: 5.2
    77Tested up to: 7.0
    8 Stable tag: 1.5.5
     8Stable tag: 1.5.6
    99Requires PHP: 5.6
    1010License: GPLv3
     
    194194## Changelog
    195195
     196### 1.5.6 (2026-03-23)
     197* Fix: update routine 10 infinite loop on sites with huge number of posts (10k+)
     198* Test: WordPress 7.0-beta6
     199
    196200### 1.5.5 (2026-03-11)
    197201* Fix: Remove `set_time_limit` from `update.php` to prevent fatal error on servers where it's disabled
  • head-footer-code/trunk/update.php

    r3480015 r3489019  
    254254 * Migration for v. 1.5.3
    255255 * Clean up double slashes from existing meta data caused by previous double-slashing.
     256 * Processes 500 records at a time and stays on DB version 9 until
     257 * all records are cleaned to prevent timeouts and memory exhaustion.
    256258 */
    257259function auhfc_update_10() {
    258260    global $wpdb;
    259261
    260     $meta_key = '_auhfc';
     262    $meta_key   = '_auhfc';
     263    $batch_size = 500;
     264    $has_more   = false;
    261265
    262266    /**
    263      * Strip slashes from Post Metas
    264      * We use direct SQL to fetch all IDs at once to avoid N+1 query issues
    265      * and memory exhaustion on large databases.
     267     * Strip slashes from Post Metas when actually contain backslashes (\\) in meta_value.
    266268     */
    267269    $post_metas = $wpdb->get_results(
    268270        $wpdb->prepare(
    269             "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s",
    270             $meta_key
     271            "SELECT post_id, meta_value FROM $wpdb->postmeta
     272            WHERE meta_key = %s AND meta_value LIKE %s
     273            LIMIT %d",
     274            $meta_key,
     275            '%' . $wpdb->esc_like( '\\' ) . '%',
     276            $batch_size
    271277        )
    272278    );
    273279
    274280    if ( ! empty( $post_metas ) ) {
     281        $has_more = true;
    275282        foreach ( $post_metas as $meta ) {
    276283            $original_data = maybe_unserialize( $meta->meta_value );
     
    278285            if ( is_array( $original_data ) ) {
    279286                $cleaned_data = stripslashes_deep( $original_data );
    280                 update_post_meta( $meta->post_id, $meta_key, wp_slash( $cleaned_data ) );
     287                if ( $cleaned_data !== $original_data ) {
     288                    update_post_meta( $meta->post_id, $meta_key, wp_slash( $cleaned_data ) );
     289                }
    281290            }
    282291        }
     
    284293
    285294    /**
    286      * Strip slashes from Taxonomies (Categories at the moment)
     295     * Strip slashes from Taxonomies (Terms) within available batch capacity.
    287296     */
    288     $term_metas = $wpdb->get_results(
    289         $wpdb->prepare(
    290             "SELECT term_id, meta_value FROM $wpdb->termmeta WHERE meta_key = %s",
    291             $meta_key
    292         )
    293     );
    294 
    295     if ( ! empty( $term_metas ) ) {
    296         foreach ( $term_metas as $meta ) {
    297             $original_data = maybe_unserialize( $meta->meta_value );
    298 
    299             if ( is_array( $original_data ) ) {
    300                 $cleaned_data = stripslashes_deep( $original_data );
    301                 update_term_meta( $meta->term_id, $meta_key, wp_slash( $cleaned_data ) );
     297    if ( count( $post_metas ) < $batch_size ) {
     298        $term_metas = $wpdb->get_results(
     299            $wpdb->prepare(
     300                "SELECT term_id, meta_value FROM $wpdb->termmeta
     301                WHERE meta_key = %s AND meta_value LIKE %s
     302                LIMIT %d",
     303                $meta_key,
     304                '%' . $wpdb->esc_like( '\\' ) . '%',
     305                $batch_size - count( $post_metas )
     306            )
     307        );
     308
     309        if ( ! empty( $term_metas ) ) {
     310            $has_more = true;
     311            foreach ( $term_metas as $meta ) {
     312                $original_data = maybe_unserialize( $meta->meta_value );
     313
     314                if ( is_array( $original_data ) ) {
     315                    $cleaned_data = stripslashes_deep( $original_data );
     316                    if ( $cleaned_data !== $original_data ) {
     317                        update_term_meta( $meta->term_id, $meta_key, wp_slash( $cleaned_data ) );
     318                    }
     319                }
    302320            }
    303321        }
     322    }
     323
     324    /**
     325     * Revert DB versin if there is more work in batches so
     326     * Main::plugins_loaded() triggers this function agaiuntil completion.
     327     */
     328    if ( $has_more ) {
     329        update_option( 'auhfc_db_ver', 9 );
    304330    }
    305331}
Note: See TracChangeset for help on using the changeset viewer.