A fix to learn about

Statcomm version 1.7.66 introduced accidentally a bug that forced me to change the way I used to program the plugin.

Brent from http://bestmobilewebsitedesigner.com/ told me there was a conflict between Statcomm and another multisite plugin Empire Builder Plugin . Thanks with the help of  Anton Oliinyk and George Katsoudas I could adress the issue… at least I thought it was addressed.

Statcomm is supporting a subplugin system since version 1.7.30 . This approach allows to add functionality without core modifications. To do that, it was necessary to dig into WordPress code core and find a way to implement a subplugin system in similar fashion. Todo that I needed to inject an special class to make subplugin works. this code was resumed on wpsubpluginlisttable.php

The following example is from wpsubpluginlisttable.php

   function __construct($redirectPage) {
        global $status, $page, $current_screen;
        $status = 'all';

        if ( isset( $_REQUEST['subplugin_status'] ) && 
in_array( $_REQUEST['subplugin_status'],  array( 'active', 'inactive' ) ) )
        $status = $_REQUEST['subplugin_status'];
        $this->subplugin_page = $redirectPage;

       $current_screen= WP_Screen::get('plugins');

        parent::__construct( array('plural' => 'subplugins',
                                   'screen' => $current_screen,
        ) );
    }

This code overwrites a global variable $current_screen, which leads to undesirable side effects, preventing other multisite plugins to work properly.

The suggested patch was simply enough:

   function __construct($redirectPage) {
        global $status, $page, $current_screen;
        $status = 'all';

        if ( isset( $_REQUEST['subplugin_status'] ) 
&& in_array( $_REQUEST['subplugin_status'],  array( 'active', 'inactive' ) ) )
        $status = $_REQUEST['subplugin_status'];
        $this->subplugin_page = $redirectPage;

//        $current_screen= WP_Screen::get('plugins');

        parent::__construct( array('plural' => 'subplugins',
                                   'screen' => WP_Screen::get('plugins'),
        ) );
    }

The approach was simple,  suppress $current_screen assignment altogether. However , it didn’t work. I ended up with:

Notice: Trying to get property of non-object in …….\wp-admin\includes\class-wp-list-table.php on line 89

The problem is that $current_screen needs to be assigned. The worst part? It affected all multisite installations since this code is for multisite environments.   Talking about screwing big time…

A fix for the fix

Finally I ended up in this approach that worked:

    function __construct($redirectPage) {
        global $status, $page, $current_screen;
        $status = 'all';

        if ( isset( $_REQUEST['subplugin_status'] ) 
&& in_array( $_REQUEST['subplugin_status'],  array( 'active', 'inactive' ) ) )
        $status = $_REQUEST['subplugin_status'];
        $this->subplugin_page = $redirectPage;

        $local_screen = $current_screen;
        $current_screen= WP_Screen::get('plugins');

        parent::__construct( array('plural' => 'subplugins',
                                   'screen' => WP_Screen::get('plugins'),
        ) );

       $current_screen= $local_screen;
    }

This time I used a local $local_screen to hold the actual data and release it after use. That solved finally the multisite issue.

This problem make think I should implement a best way to test any code changes on the plugin. So after careful planning, I decided to split up the development in three parts: a site for development only, where the plugin is never activated, a multisite environment and a single one environment.

So when the plugin is ready to be tested, a batch is launched  the project is  deployed to both sites and in few seconds we are ready to test the plugin. I run all the Unit Tests and when all the lights are green, I have some degree of confidence that the plugin should improve code quality in further releases.

No Comments


Leave a Reply

Your email address will not be published Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*