From dadb9f8e94c15da637af629f0aecfe97c2a1755b Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Mon, 20 Jan 2025 15:57:25 +0000 Subject: [PATCH] Static analysis fixes for block theme --- phpstan.neon | 1 + phpstan/constants.php | 8 ++++++++ themes/10up-block-theme/functions.php | 5 ++++- themes/10up-block-theme/includes/blocks.php | 16 ++++++++++++++++ themes/10up-block-theme/includes/core.php | 13 +++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/phpstan.neon b/phpstan.neon index 8ef7f3b6..048697fb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,5 +4,6 @@ includes: parameters: paths: - themes/10up-theme + - themes/10up-block-theme - mu-plugins/10up-plugin - mu-plugins/10up-plugin-loader.php diff --git a/phpstan/constants.php b/phpstan/constants.php index 794d6459..edc498d2 100644 --- a/phpstan/constants.php +++ b/phpstan/constants.php @@ -21,3 +21,11 @@ define( 'TENUP_THEME_DIST_URL', TENUP_THEME_TEMPLATE_URL . '/dist/' ); define( 'TENUP_THEME_INC', TENUP_THEME_PATH . 'includes/' ); define( 'TENUP_THEME_BLOCK_DIR', TENUP_THEME_INC . 'blocks/' ); + +define( 'TENUP_BLOCK_THEME_VERSION', '1.0.0' ); +define( 'TENUP_BLOCK_THEME_TEMPLATE_URL', '' ); +define( 'TENUP_BLOCK_THEME_PATH', '/' ); +define( 'TENUP_BLOCK_THEME_DIST_PATH', TENUP_BLOCK_THEME_PATH . 'dist/' ); +define( 'TENUP_BLOCK_THEME_DIST_URL', TENUP_BLOCK_THEME_TEMPLATE_URL . '/dist/' ); +define( 'TENUP_BLOCK_THEME_INC', TENUP_BLOCK_THEME_PATH . 'includes/' ); +define( 'TENUP_BLOCK_THEME_BLOCK_DIST_DIR', TENUP_BLOCK_THEME_DIST_PATH . '/blocks/' ); diff --git a/themes/10up-block-theme/functions.php b/themes/10up-block-theme/functions.php index 14743aa3..77dcdade 100755 --- a/themes/10up-block-theme/functions.php +++ b/themes/10up-block-theme/functions.php @@ -25,7 +25,10 @@ if ( $is_local && file_exists( __DIR__ . '/dist/fast-refresh.php' ) ) { require_once __DIR__ . '/dist/fast-refresh.php'; - TenUpToolkit\set_dist_url_path( basename( __DIR__ ), TENUP_BLOCK_THEME_DIST_URL, TENUP_BLOCK_THEME_DIST_PATH ); + + if ( function_exists( 'TenUpToolkit\set_dist_url_path' ) ) { + TenUpToolkit\set_dist_url_path( basename( __DIR__ ), TENUP_BLOCK_THEME_DIST_URL, TENUP_BLOCK_THEME_DIST_PATH ); + } } require_once TENUP_BLOCK_THEME_INC . 'core.php'; diff --git a/themes/10up-block-theme/includes/blocks.php b/themes/10up-block-theme/includes/blocks.php index fe4f0c6b..0e845072 100644 --- a/themes/10up-block-theme/includes/blocks.php +++ b/themes/10up-block-theme/includes/blocks.php @@ -30,9 +30,18 @@ function register_theme_blocks() { $block_json_files = glob( TENUP_BLOCK_THEME_BLOCK_DIST_DIR . '*/block.json' ); $block_names = []; + if ( empty( $block_json_files ) ) { + return; + } + foreach ( $block_json_files as $filename ) { $block_folder = dirname( $filename ); $block = register_block_type_from_metadata( $block_folder ); + + if ( ! $block ) { + continue; + } + $block_names[] = $block->name; } @@ -50,9 +59,16 @@ function ( array|bool $allowed_blocks ) use ( $block_names ): array|bool { /** * Enqueue block specific styles. + * + * @return void */ function enqueue_theme_block_styles() { $stylesheets = glob( TENUP_BLOCK_THEME_DIST_PATH . '/blocks/autoenqueue/**/*.css' ); + + if ( empty( $stylesheets ) ) { + return; + } + foreach ( $stylesheets as $stylesheet_path ) { $block_type = str_replace( TENUP_BLOCK_THEME_DIST_PATH . '/blocks/autoenqueue/', '', $stylesheet_path ); $block_type = str_replace( '.css', '', $block_type ); diff --git a/themes/10up-block-theme/includes/core.php b/themes/10up-block-theme/includes/core.php index a15baf91..a8340e80 100755 --- a/themes/10up-block-theme/includes/core.php +++ b/themes/10up-block-theme/includes/core.php @@ -40,6 +40,8 @@ function i18n() { /** * Sets up theme defaults and registers support for various WordPress features. + * + * @return void */ function theme_setup() { add_theme_support( 'editor-styles' ); @@ -104,6 +106,8 @@ function editor_style_overrides() { /** * register all icons located in the dist/svg folder + * + * @return void */ function register_all_icons() { if ( ! function_exists( '\UIKitCore\Helpers\register_icons' ) ) { @@ -111,10 +115,19 @@ function register_all_icons() { } $icon_paths = glob( TENUP_BLOCK_THEME_DIST_PATH . 'svg/*.svg' ); + + if ( ! $icon_paths ) { + return; + } + $icons = array_map( function ( $icon_path ) { $icon_name = preg_replace( '#\..*$#', '', basename( $icon_path ) ); + if ( ! $icon_name || ! class_exists( '\UIKitCore\Icon' ) ) { + return false; + } + return new \UIKitCore\Icon( $icon_name, ucwords( str_replace( '-', ' ', $icon_name ) ),