coenjacobs / conductor

Enforces your WordPress plugin requirements by checking PHP versions and details of other plugins
5 stars 1 forks source link

Don't read entire file to improve performance #2

Open danieliser opened 4 years ago

danieliser commented 4 years ago

The line below loads what could be a heavy file into memory.

https://github.com/coenjacobs/conductor/blob/master/src/FileReader.php#L9

The way WP core does it is likely more ideal, so we should either patch it to use the get_plugin_data function, or mimic it's functionality.

Specifically, core only loads the first 8 bytes of the file or so via get_file_data.

Option 1

    public function getPluginVersion($slug)
    {
        $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $slug, false, false );
        return ! empty( $data['Version'] ) ? $data['Version'] : false; // Set default that you are set to handle as a false.
    }

Option 2 - A little lighter as it has none of the overhead logic.

    public function getPluginVersion($slug)
    {
        // Stripped down list of headers.
    $default_headers = [
        'Version'     => 'Version',
    ];

    $plugin_data = get_file_data( $WP_PLUGIN_DIR . '/' . $slug, $default_headers, 'plugin' );
        return ! empty( $data['Version'] ) ? $data['Version'] : false; // Set default that you are set to handle as a false.
    }
coenjacobs commented 4 years ago

Completely agree. A PR to fix this is very welcome. Since we only need the version right now, I'm leaning towards option 2, which we can always further expand upon should we need more data.

danieliser commented 4 years ago

@coenjacobs - Sweet, PR incoming as soon as I get a few minutes to test it.