From d4fac1b87e2d9b66826ece4f11d129ddc30c5025 Mon Sep 17 00:00:00 2001 From: danieliser Date: Sat, 4 Jul 2020 19:14:21 -0400 Subject: [PATCH 1/2] Reduce overhead by only loading file header Rewrites the getPluginVersion method to no longer load the entire plugin file but rather only the first 8kb header, mimicing WordPress core. This should be much better for memory usage and overall package performance. Closes #2 --- src/FileReader.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/FileReader.php b/src/FileReader.php index c29774e..c17a177 100644 --- a/src/FileReader.php +++ b/src/FileReader.php @@ -6,9 +6,12 @@ class FileReader { public function getPluginVersion($slug) { - $contents = file_get_contents(WP_PLUGIN_DIR .'/' . $slug); - $matches = []; - preg_match('/(?:.*)(?:Version:)\s*(.*)?/', $contents, $matches); - return $matches[1]; + // Stripped down list of headers. + $default_headers = [ + 'Version' => 'Version', + ]; + + $plugin_data = get_file_data( WP_PLUGIN_DIR . '/' . $slug, $default_headers, 'plugin' ); + return ! empty( $plugin_data['Version'] ) ? $plugin_data['Version'] : false; // Set default that you are set to handle as a false. } } From eca595fd2bbd39fb5667b42dab5e635c2234fb9d Mon Sep 17 00:00:00 2001 From: danieliser Date: Sat, 4 Jul 2020 19:15:11 -0400 Subject: [PATCH 2/2] Add proper docblocks --- src/FileReader.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/FileReader.php b/src/FileReader.php index c17a177..e98f212 100644 --- a/src/FileReader.php +++ b/src/FileReader.php @@ -2,9 +2,21 @@ namespace CoenJacobs\Conductor; +/** + * Class FileReader + * + * @package CoenJacobs\Conductor + */ class FileReader { - public function getPluginVersion($slug) + /** + * Get the current version of an installed plugin. + * + * @param string $slug + * + * @return false|string + */ + public function getPluginVersion($slug) { // Stripped down list of headers. $default_headers = [