From 6b67205e43d519d6be47831cc49f0247612be16b Mon Sep 17 00:00:00 2001 From: fumikito Date: Thu, 4 Jun 2020 15:34:36 +0900 Subject: [PATCH] Add filter for registering classses. --- src/Kunoichi/ThemeCustomizer.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Kunoichi/ThemeCustomizer.php b/src/Kunoichi/ThemeCustomizer.php index 8154369..35f43ae 100644 --- a/src/Kunoichi/ThemeCustomizer.php +++ b/src/Kunoichi/ThemeCustomizer.php @@ -25,6 +25,7 @@ final private function __construct() {} * @return bool|\WP_Error */ public static function register( $name_space, $base_dir = '' ) { + static $initialized = false; self::load_locale( get_locale() ); if ( ! $base_dir ) { $base_dir = get_template_directory() . '/src'; @@ -37,14 +38,27 @@ public static function register( $name_space, $base_dir = '' ) { $base_class_name = CustomizerSetting::class; $finder = new Finder(); $found = 0; + $class_names = []; foreach ( $finder->in( $dir_name )->name( '*.php' )->files() as $file ) { - $path = $file->getPathname(); - $path = str_replace( $base_dir, '', $path ); - $path = str_replace( '.php', '', $path ); + $path = $file->getPathname(); + $path = str_replace( $base_dir, '', $path ); + $path = str_replace( '.php', '', $path ); $class_name = str_replace( '/', '\\', $path ); if ( ! class_exists( $class_name ) ) { continue; } + $class_names[] = $class_name; + } + /** + * Add filter for registered class names. + * + * @param string[] $class_names + * @param string $name_space + * @param string $base_class_name + * @param bool $initialized + */ + $class_names = apply_filters( 'theme_customizer_class_name', $class_names, $name_space, $base_class_name, $initialized ); + foreach ( $class_names as $class_name ) { try { $reflection = new \ReflectionClass( $class_name ); if ( ! $reflection->isSubclassOf( $base_class_name ) ) { @@ -56,6 +70,7 @@ public static function register( $name_space, $base_dir = '' ) { // Do nothing. } } + $initialized = true; return $found ? true : new \WP_Error( 'no_class_found', __( 'No items found.' ) ); }