johnbillion / query-monitor

The developer tools panel for WordPress
https://querymonitor.com
GNU General Public License v2.0
1.6k stars 210 forks source link

Add action for including files with extended output class definitions #212

Open crstauf opened 8 years ago

crstauf commented 8 years ago

I just now reorganized QMX to be as similar to QM as I can manage, which means hooking into qm/output/absolute_position filter to include the necessary files, which, while it works, is not preferred.

It would be helpful and cleaner if an action could be added into the before_output method in the HTML dispatcher that QMX could hook into to include those files.

Thoughts?

crstauf commented 8 years ago

So I actually changed it up, and it seems to work just as well, without the messiness:

add_filter('qm/outputter/html','include_outputters',0);

I think an action would still be better, but hooking onto qm/outputter/html with priority zero at least gets the panels on both front and backends.

crstauf commented 8 years ago

@johnbillion I've discovered something interesting that I'll be posting in a moment, but I'm wondering what is the recommended way for QM extensions to register and include their output files?

crstauf commented 8 years ago

Okay, so here's what I found:

When outputters are registered within the file that contains the output class (see Exhibit A), and those files are included by hooking into qm/outputter/html with priority zero, the actions are added in incorrect order (see Exhibit C).

However, when the outputters are registered during plugin load (see Exhibit D), the hooks are then in sequential order (see Exhibit E; the panels load up in the correct order), but there are errors since the function that does the actual registering does not yet exist (see Exhibit F); it works, but it feels very hacky.

Adding a filter (qm/files/outputter?) to QM/dispatchers/Html.php:209 would be sufficient (tested) to allow extensions to register their files right alongside of QM, with something like Exhibit G.

Exhibit A

qmx/output/images-sizes.php pseudo-code:

class QMX_Output_Html_ImageSizes {}
function register_qmx_output_html_imagesizes() {}
add_filter( 'qm/outputter/html', 'register_qmx_output_html_imagesizes', 153, 2 );

Exhibit B

qmx/qmx.php pseudo-code:

class QMX {
    add_filter( 'qm/outputter/html', array( __CLASS__, 'include_outputters' ), 0 );
    public static function include_outputters( $output ) { /* include all output class files */ }

Exhibit C

Dump of $wp_filter['qm/outputter/html'] when hooks added via outputter class file (Exhibit A) http://pastebin.com/mGMe3Hdz

Exhibit D

qmx/qmx.php pseudo-code:

class QMX {
    function __construct() {
        add_filter( 'qm/outputter/html', 'register_qmx_output_html_includedfiles', 119, 2 );
        add_filter( 'qm/outputter/html', 'register_qmx_output_html_constants', 150, 2 );
        add_filter( 'qm/outputter/html', 'register_qmx_output_html_paths', 151, 2 );
        add_filter( 'qm/outputter/html', 'register_qmx_output_html_multisite', 152, 2 );
        add_filter( 'qm/outputter/html', 'register_qmx_output_html_imagesizes', 153, 2 );
        add_filter( 'qm/outputter/html', 'register_qmx_output_html_vardumps', 200, 2 );
    }
}

Exhibit E

Dump of $wp_filter['qm/outputter/html when hooks registered via __construct method (Exhibit D) http://pastebin.com/gyG6tXTV

Exhibit F

screen shot 2016-08-09 at 12 29 33 am

Exhibit G

pseudo-code as example of hooking into filter (qm/files/outputter) on including outputter files.

function filter_qm_files_outputter( $QM_files ) {
    $QMX_files = array of QMX files with outputter classes;
    return array_merge( $QM_files, $QMX_files );
}