Making WordPress.org

Changeset 10085


Ignore:
Timestamp:
07/20/2020 08:40:31 AM (4 years ago)
Author:
tellyworth
Message:

Plugin directory: better asset detection in block checker.

This should make the block checker use identical guesswork to the importer when looking for block assets.

See #5303.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-block-plugin-checker.php

    r10071 r10085  
    3737    protected $asset_php_files = null;
    3838    protected $block_json_validation = array();
     39    protected $block_assets = array();
     40    protected $block_scripts = array();
    3941
    4042    /**
     
    233235
    234236        $this->asset_php_files = $this->find_asset_php_files( $this->path_to_plugin );
     237
     238        $this->block_assets = $this->find_block_assets( $this->path_to_plugin );
     239        $this->block_scripts = $this->find_block_scripts( $this->path_to_plugin );
    235240    }
    236241
     
    274279    }
    275280
    276     public function find_block_scripts() {
     281    public function find_block_assets( $base_dir ) {
     282        $assets = Import::find_possible_block_assets( $base_dir );
     283        $assets = array_map( array( $this, 'relative_filename' ), $assets );
     284
     285        $block_assets = array(
     286            'js' => array(),
     287            'css' => array(),
     288        );
     289
     290        foreach ( $assets as $file ) {
     291            $ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
     292            if ( array_key_exists( $ext, $block_assets ) ) {
     293                $block_assets[ $ext ][] = $file;
     294            }
     295        }
     296
     297        return $block_assets;
     298    }
     299
     300    public function find_block_scripts( $base_dir ) {
     301
    277302        $block_scripts = \array_map(
    278303            function( $block ) {
     
    283308                    if ( isset( $block->$prop ) ) {
    284309                        $assets[ $prop ] = $block->$prop;
     310                    }
     311                }
     312
     313                // If no block.json data was found, try to guess.
     314                if ( empty( $assets ) ) {
     315                    // If js or css assets were found, assume we can use the first one.
     316                    if ( !empty( $this->block_assets[ 'js' ] ) ) {
     317                        $assets[ 'script' ] = reset( $this->block_assets[ 'js' ] );
     318                    }
     319
     320                    if ( !empty( $this->block_assets[ 'css' ] ) ) {
     321                        $assets[ 'style' ] = reset( $this->block_assets[ 'css' ] );
    285322                    }
    286323                }
     
    570607    function check_for_block_script_files() {
    571608        $seen_scripts = array();
    572         foreach ( $this->find_block_scripts() as $block_name => $scripts ) {
     609        foreach ( $this->block_scripts as $block_name => $scripts ) {
    573610            foreach ( $scripts as $kind => $script ) {
    574611                // No need to warn multiple times for the same value.
Note: See TracChangeset for help on using the changeset viewer.