Skip to content

Commit

Permalink
Add filter for registering classses.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Jun 4, 2020
1 parent f367ddc commit 6b67205
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Kunoichi/ThemeCustomizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 ) ) {
Expand All @@ -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.' ) );
}

Expand Down

0 comments on commit 6b67205

Please sign in to comment.