From 474a4d2e0ff9a8c838da16b33f93f2880185318c Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Sat, 9 Jun 2018 15:52:25 +0200 Subject: [PATCH 1/6] Add filters to exclude directories and files before import --- lib/runner.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/lib/runner.php b/lib/runner.php index 90b8f82..5eabd07 100644 --- a/lib/runner.php +++ b/lib/runner.php @@ -14,11 +14,17 @@ * * @return array|\WP_Error */ -function get_wp_files( $directory ) { - $iterableFiles = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator( $directory ) - ); - $files = array(); +function get_wp_files( $root ) { + $dirIterator = new \RecursiveDirectoryIterator( $root ); + $filterIterator = filter_wp_directories( $root, $dirIterator ); + + if ( $filterIterator instanceof \RecursiveCallbackFilterIterator ) { + $iterableFiles = new \RecursiveIteratorIterator( $filterIterator ); + } else { + $iterableFiles = new \RecursiveIteratorIterator( $dirIterator ); + } + + $files = array(); try { foreach ( $iterableFiles as $file ) { @@ -26,6 +32,13 @@ function get_wp_files( $directory ) { continue; } + /** + * Whether to exclude a file for parsing. + */ + if ( ! apply_filters( 'wp_parser_pre_get_wp_file', true, $file->getPathname(), $root ) ) { + continue; + } + $files[] = $file->getPathname(); } } catch ( \UnexpectedValueException $exc ) { @@ -38,6 +51,67 @@ function get_wp_files( $directory ) { return $files; } +/** + * Fiter the directories to parse. + * + * @param string $root Root dir. + * @param object $dirIterator RecursiveDirectoryIterator. + * @return object|false RecursiveCallbackFilterIterator or false. + */ +function filter_wp_directories( $root, $dirIterator ) { + $root = trailingslashit( $root ); + + /** + * Filter directories found in the root directory. + * + * For example: 'vendor', 'tests', 'specific/directory'. + * + * @param unknown $exclude Array with directories to skip parsing. Default empty array(). + * @param unknown $root Root directory to parse. + */ + $exclude = apply_filters( 'wp_parser_exclude_directories', array(), $root ); + + /** + * Whether to exlude directories if found in a subdirectory. + * + * @param unknown $strict. Exclude subdirectories. Default false. + * @param unknown $root Root directory to parse. + */ + $strict = apply_filters( 'wp_parser_exclude_directories_strict', false, $root ); + + if ( ! $exclude ) { + return false; + } + + $filter = new \RecursiveCallbackFilterIterator( $dirIterator, function ( $current ) use ( $root, $exclude, $strict ) { + if ( $current->isFile() && ( 'php' !== $current->getExtension() ) ) { + return false; + } + + if ( ! $current->isDir() ) { + return true; + } + + // Exclude directories strict. + $dir_name = $current->getFilename(); + if ( $strict && in_array( $dir_name, $exclude ) ) { + return false; + } + + // Exclude directories in the root directory. + $current_path = $current->getPathname(); + foreach ( $exclude as $dir ) { + if ( ( $root . untrailingslashit( $dir ) ) === $current_path ) { + return false; + } + } + + return true; + } ); + + return $filter; +} + /** * @param array $files * @param string $root From 965a6f6e860e1391fde8fc416dbfaf6b118c29be Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Sat, 9 Jun 2018 15:52:25 +0200 Subject: [PATCH 2/6] Add filters to exclude directories and files before import --- lib/runner.php | 84 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/lib/runner.php b/lib/runner.php index e542787..f639ca5 100644 --- a/lib/runner.php +++ b/lib/runner.php @@ -14,11 +14,17 @@ * * @return array|\WP_Error */ -function get_wp_files( $directory ) { - $iterableFiles = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator( $directory ) - ); - $files = array(); +function get_wp_files( $root ) { + $dirIterator = new \RecursiveDirectoryIterator( $root ); + $filterIterator = filter_wp_directories( $root, $dirIterator ); + + if ( $filterIterator instanceof \RecursiveCallbackFilterIterator ) { + $iterableFiles = new \RecursiveIteratorIterator( $filterIterator ); + } else { + $iterableFiles = new \RecursiveIteratorIterator( $dirIterator ); + } + + $files = array(); try { foreach ( $iterableFiles as $file ) { @@ -26,6 +32,13 @@ function get_wp_files( $directory ) { continue; } + /** + * Whether to exclude a file for parsing. + */ + if ( ! apply_filters( 'wp_parser_pre_get_wp_file', true, $file->getPathname(), $root ) ) { + continue; + } + $files[] = $file->getPathname(); } } catch ( \UnexpectedValueException $exc ) { @@ -38,6 +51,67 @@ function get_wp_files( $directory ) { return $files; } +/** + * Fiter the directories to parse. + * + * @param string $root Root dir. + * @param object $dirIterator RecursiveDirectoryIterator. + * @return object|false RecursiveCallbackFilterIterator or false. + */ +function filter_wp_directories( $root, $dirIterator ) { + $root = trailingslashit( $root ); + + /** + * Filter directories found in the root directory. + * + * For example: 'vendor', 'tests', 'specific/directory'. + * + * @param unknown $exclude Array with directories to skip parsing. Default empty array(). + * @param unknown $root Root directory to parse. + */ + $exclude = apply_filters( 'wp_parser_exclude_directories', array(), $root ); + + /** + * Whether to exlude directories if found in a subdirectory. + * + * @param unknown $strict. Exclude subdirectories. Default false. + * @param unknown $root Root directory to parse. + */ + $strict = apply_filters( 'wp_parser_exclude_directories_strict', false, $root ); + + if ( ! $exclude ) { + return false; + } + + $filter = new \RecursiveCallbackFilterIterator( $dirIterator, function ( $current ) use ( $root, $exclude, $strict ) { + if ( $current->isFile() && ( 'php' !== $current->getExtension() ) ) { + return false; + } + + if ( ! $current->isDir() ) { + return true; + } + + // Exclude directories strict. + $dir_name = $current->getFilename(); + if ( $strict && in_array( $dir_name, $exclude ) ) { + return false; + } + + // Exclude directories in the root directory. + $current_path = $current->getPathname(); + foreach ( $exclude as $dir ) { + if ( ( $root . untrailingslashit( $dir ) ) === $current_path ) { + return false; + } + } + + return true; + } ); + + return $filter; +} + /** * @param array $files * @param string $root From fe002cd912f262c1ede1346006bc8333e614880c Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Mon, 11 Jun 2018 17:41:30 +0200 Subject: [PATCH 3/6] Update WordPress version --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 18bbdb5..435f3cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ before_install: # Setup WP_TESTS_DIR (needed to bootstrap WP PHPUnit tests). - export WP_TESTS_DIR=/tmp/wordpress/tests/phpunit/ # Clone the WordPress develop repo. - - git clone --depth=1 --branch="4.3" git://develop.git.wordpress.org/ /tmp/wordpress/ + - git clone --depth=1 --branch="4.9" git://develop.git.wordpress.org/ /tmp/wordpress/ # Setup DB. - mysql -e "CREATE DATABASE wordpress_test;" -uroot # Setup wp-config. From 693907c31a2582cdc45f2a5f8211ca7d316f0d98 Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Mon, 11 Jun 2018 17:45:20 +0200 Subject: [PATCH 4/6] Use PHPunit 4.8 --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 435f3cf..8a69b8b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,8 @@ matrix: - php: hhvm before_install: + - export PATH="$HOME/.composer/vendor/bin:$PATH" + - composer global require "phpunit/phpunit=4.8.*" # Setup WP_TESTS_DIR (needed to bootstrap WP PHPUnit tests). - export WP_TESTS_DIR=/tmp/wordpress/tests/phpunit/ # Clone the WordPress develop repo. From d0658f9bb592a5f567bd4952a2df736ab2e312af Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Tue, 29 Jan 2019 13:28:19 +0100 Subject: [PATCH 5/6] Revert changes to travis.yml This reverts commit 693907c31a2582cdc45f2a5f8211ca7d316f0d98. --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8a69b8b..435f3cf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,8 +12,6 @@ matrix: - php: hhvm before_install: - - export PATH="$HOME/.composer/vendor/bin:$PATH" - - composer global require "phpunit/phpunit=4.8.*" # Setup WP_TESTS_DIR (needed to bootstrap WP PHPUnit tests). - export WP_TESTS_DIR=/tmp/wordpress/tests/phpunit/ # Clone the WordPress develop repo. From 783f48221efd6b2811510ffac7a82a4be07eddeb Mon Sep 17 00:00:00 2001 From: Kees Meijer Date: Tue, 29 Jan 2019 13:29:29 +0100 Subject: [PATCH 6/6] Revert "Update WordPress version" This reverts commit fe002cd912f262c1ede1346006bc8333e614880c. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 435f3cf..18bbdb5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ before_install: # Setup WP_TESTS_DIR (needed to bootstrap WP PHPUnit tests). - export WP_TESTS_DIR=/tmp/wordpress/tests/phpunit/ # Clone the WordPress develop repo. - - git clone --depth=1 --branch="4.9" git://develop.git.wordpress.org/ /tmp/wordpress/ + - git clone --depth=1 --branch="4.3" git://develop.git.wordpress.org/ /tmp/wordpress/ # Setup DB. - mysql -e "CREATE DATABASE wordpress_test;" -uroot # Setup wp-config.