How to check if WordPress is updating plugins?

I have a simple dependency check at the start of some plugins – which check if another plugin is installed and active (using class_exists()).

This works well in the normal WordPress flow – as the plugin that includes the required class is set to instantiate earlier and I also re-order the available plugins so this plugin is loaded first – like this:

Read More
    /**
     * Force this plugin to load first
     * 
     * @since   0.1
     * @return  void
     */
    public function load_this_plugin_first() 
    {

        // grab plugin directory + file name ##
        $path = plugin_basename(dirname(__FILE__)).'/'.basename(__FILE__);

        // grab the list of active plugins ##
        if ( $plugins = get_option( 'active_plugins' ) ) {

            // check if out plugin is in there ##
            if ( $key = array_search( $path, $plugins ) ) {

                // push it to the top of the list ##
                array_splice( $plugins, $key, 1 );
                array_unshift( $plugins, $path );
                update_option( 'active_plugins', $plugins );

            }

        }

    }

This all works well until I need to update a plugin – then the other plugins are all deactivated – as all the class_exists() checks return false.

So, I’d like to know if there is a constant – such as DOING_AUTOSAVE for plugin updates – something like DOING_PLUGIN_UPDATE?

Related posts

2 comments

  1. Instead of re-ordering your plugins to load in a specific order, have each of them that requires some other plugin to be loaded to defer their initialization until the “plugins_loaded” action hook.

    In other words, when a plugin with a dependency is included and ran, it shouldn’t do anything directly. Instead, it should have functions, and then add_action calls to hook those functions to plugins_loaded. That function that is called later can do the class_exists check and act accordingly.

  2. How about using WP_INSTALLING? – this check is placed above the main plugin Class…

    if ( ! class_exists( 'REQUIRED_CLASS_NAME' ) && ! defined( 'WP_INSTALLING' ) )  { 
    
        // give some notice to the user ##
        add_action('admin_notices', function(){
            $plugin = get_plugin_data( __FILE__ );
            echo "<div id='message' class='error'><p><code>".$plugin["Name"]."</code> has been deactivated because it requires the <a href='#'>PLUGIN NAME</a>.</p></div>";
            if ( isset( $_GET['activate'] ) ) {
                unset( $_GET['activate'] ); // remove the activation notice ##
            }
        });
    
       // turn this plugin off ##
       add_action('admin_notices', function(){
          deactivate_plugins( plugin_basename( __FILE__ ) );
       });
    
       // stop this plugin from running ##
       return;
    }
    

    Then the actual plugin class is below:

    if ( ! class_eixsts( "Plugin_Class" ) ) 
    { 
        Plugin_Class() 
        { 
          ... 
        } 
    }
    

Comments are closed.