Plugin Directory

Changeset 3457834


Ignore:
Timestamp:
02/10/2026 09:55:45 AM (7 weeks ago)
Author:
codecanel
Message:

plugin update dependency logic mofifed

Location:
wp-before-after-image-slider
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-before-after-image-slider/tags/2.1.2/includes/features/Admin/PluginSync.php

    r3457783 r3457834  
    160160        // Only run for admin users.
    161161        if ( ! current_user_can( 'activate_plugins' ) ) {
     162            return;
     163        }
     164
     165        // Skip dependency check if sync is in progress to avoid race condition.
     166        if ( get_transient( 'bais_plugin_sync_needed' ) || get_transient( 'bais_plugin_sync_in_progress' ) ) {
    162167            return;
    163168        }
     
    302307     */
    303308    private function perform_sync( array $sync_data ): void {
     309        // Set flag to indicate sync is in progress (prevents dependency check race condition).
     310        set_transient( 'bais_plugin_sync_in_progress', true, 60 );
     311
    304312        $target_slug = $sync_data['target_slug'];
    305313        $target_type = $sync_data['target_type'];
     
    318326
    319327            // Get the source version (the version that was just updated).
    320             $source_slug    = 'free' === $target_type ? self::PRO_SLUG : self::FREE_SLUG;
     328            $source_slug    = 'free' === $target_type ? $this->get_pro_plugin_slug() : $this->get_free_plugin_slug();
    321329            $source_version = $this->get_plugin_version( $source_slug );
    322330
     
    325333                $this->update_plugin( $target_slug );
    326334            }
     335
     336            // Ensure both plugins are active after sync.
     337            $this->ensure_both_plugins_active();
    327338        } catch ( \Exception $e ) {
    328339            // Log error but don't break the admin.
    329340            error_log( 'BAIS Plugin Sync Error: ' . $e->getMessage() ); // phpcs:ignore
     341        } finally {
     342            // Clear sync in progress flag.
     343            delete_transient( 'bais_plugin_sync_in_progress' );
     344        }
     345    }
     346
     347    /**
     348     * Ensure both free and pro plugins are active after sync.
     349     *
     350     * @return void
     351     */
     352    private function ensure_both_plugins_active(): void {
     353        if ( ! function_exists( 'is_plugin_active' ) ) {
     354            require_once ABSPATH . 'wp-admin/includes/plugin.php';
     355        }
     356
     357        $free_slug = $this->get_free_plugin_slug();
     358        $pro_slug  = $this->get_pro_plugin_slug();
     359
     360        // Activate free plugin if it's installed but not active.
     361        if ( $this->is_plugin_installed( $free_slug ) && ! is_plugin_active( $free_slug ) ) {
     362            activate_plugin( $free_slug, '', false, true );
     363        }
     364
     365        // Activate pro plugin if it's installed but not active.
     366        if ( $this->is_plugin_installed( $pro_slug ) && ! is_plugin_active( $pro_slug ) ) {
     367            activate_plugin( $pro_slug, '', false, true );
    330368        }
    331369    }
     
    344382        }
    345383
     384        // Include required WordPress functions.
     385        if ( ! function_exists( 'is_plugin_active' ) ) {
     386            require_once ABSPATH . 'wp-admin/includes/plugin.php';
     387        }
     388
     389        // Remember if plugin was active before update.
     390        $was_active = is_plugin_active( $plugin_slug );
     391
    346392        try {
    347393            // Create upgrader instance.
     
    354400                error_log( 'BAIS Plugin Sync Update Error: ' . $result->get_error_message() ); // phpcs:ignore
    355401                return false;
     402            }
     403
     404            // Reactivate plugin if it was active before update.
     405            if ( $was_active ) {
     406                activate_plugin( $plugin_slug, '', false, true );
    356407            }
    357408
  • wp-before-after-image-slider/trunk/includes/features/Admin/PluginSync.php

    r3457783 r3457834  
    160160        // Only run for admin users.
    161161        if ( ! current_user_can( 'activate_plugins' ) ) {
     162            return;
     163        }
     164
     165        // Skip dependency check if sync is in progress to avoid race condition.
     166        if ( get_transient( 'bais_plugin_sync_needed' ) || get_transient( 'bais_plugin_sync_in_progress' ) ) {
    162167            return;
    163168        }
     
    302307     */
    303308    private function perform_sync( array $sync_data ): void {
     309        // Set flag to indicate sync is in progress (prevents dependency check race condition).
     310        set_transient( 'bais_plugin_sync_in_progress', true, 60 );
     311
    304312        $target_slug = $sync_data['target_slug'];
    305313        $target_type = $sync_data['target_type'];
     
    318326
    319327            // Get the source version (the version that was just updated).
    320             $source_slug    = 'free' === $target_type ? self::PRO_SLUG : self::FREE_SLUG;
     328            $source_slug    = 'free' === $target_type ? $this->get_pro_plugin_slug() : $this->get_free_plugin_slug();
    321329            $source_version = $this->get_plugin_version( $source_slug );
    322330
     
    325333                $this->update_plugin( $target_slug );
    326334            }
     335
     336            // Ensure both plugins are active after sync.
     337            $this->ensure_both_plugins_active();
    327338        } catch ( \Exception $e ) {
    328339            // Log error but don't break the admin.
    329340            error_log( 'BAIS Plugin Sync Error: ' . $e->getMessage() ); // phpcs:ignore
     341        } finally {
     342            // Clear sync in progress flag.
     343            delete_transient( 'bais_plugin_sync_in_progress' );
     344        }
     345    }
     346
     347    /**
     348     * Ensure both free and pro plugins are active after sync.
     349     *
     350     * @return void
     351     */
     352    private function ensure_both_plugins_active(): void {
     353        if ( ! function_exists( 'is_plugin_active' ) ) {
     354            require_once ABSPATH . 'wp-admin/includes/plugin.php';
     355        }
     356
     357        $free_slug = $this->get_free_plugin_slug();
     358        $pro_slug  = $this->get_pro_plugin_slug();
     359
     360        // Activate free plugin if it's installed but not active.
     361        if ( $this->is_plugin_installed( $free_slug ) && ! is_plugin_active( $free_slug ) ) {
     362            activate_plugin( $free_slug, '', false, true );
     363        }
     364
     365        // Activate pro plugin if it's installed but not active.
     366        if ( $this->is_plugin_installed( $pro_slug ) && ! is_plugin_active( $pro_slug ) ) {
     367            activate_plugin( $pro_slug, '', false, true );
    330368        }
    331369    }
     
    344382        }
    345383
     384        // Include required WordPress functions.
     385        if ( ! function_exists( 'is_plugin_active' ) ) {
     386            require_once ABSPATH . 'wp-admin/includes/plugin.php';
     387        }
     388
     389        // Remember if plugin was active before update.
     390        $was_active = is_plugin_active( $plugin_slug );
     391
    346392        try {
    347393            // Create upgrader instance.
     
    354400                error_log( 'BAIS Plugin Sync Update Error: ' . $result->get_error_message() ); // phpcs:ignore
    355401                return false;
     402            }
     403
     404            // Reactivate plugin if it was active before update.
     405            if ( $was_active ) {
     406                activate_plugin( $plugin_slug, '', false, true );
    356407            }
    357408
Note: See TracChangeset for help on using the changeset viewer.