Making WordPress.org

Ticket #5207: 5207-subdir-check.diff

File 5207-subdir-check.diff, 5.7 KB (added by coreymckrill, 4 years ago)
  • cli/class-block-plugin-checker.php

     
    3434        protected $headers = null;
    3535        protected $blocks = null;
    3636        protected $block_json_files = null;
     37        protected $potential_block_directories = array();
    3738        protected $asset_php_files = null;
    3839        protected $block_json_validation = array();
    3940        protected $block_assets = array();
     
    7071        public function get_results( $type = null, $check = null ) {
    7172                $out = array();
    7273                foreach ( $this->results as $result ) {
    73                         if ( 
     74                        if (
    7475                                ( is_null( $type ) || $type === $result->type ) &&
    7576                                ( is_null( $check ) || $check === $result->check_name )
    7677                        ) {
     
    193194                } else {
    194195                        return $this->results;
    195196                }
    196                
     197
    197198        }
    198199
    199200        function export_plugin( $svn_url ) {
     
    284285                        foreach ( $this->block_json_files as $filename ) {
    285286                                $blocks_in_file = Import::find_blocks_in_file( $filename );
    286287                                $relative_filename = $this->relative_filename( $filename );
    287                                 $potential_block_directories[] = dirname( $relative_filename );
     288                                $this->potential_block_directories[] = dirname( $relative_filename );
    288289                                foreach ( $blocks_in_file as $block ) {
    289290                                        $blocks[ $block->name ] = $block;
    290291                                }
     
    294295                        $blocks_in_file = Import::find_blocks_in_file( $filename );
    295296                        if ( ! empty( $blocks_in_file ) ) {
    296297                                $relative_filename = $this->relative_filename( $filename );
     298                                $this->potential_block_directories[] = dirname( $relative_filename );
    297299                                foreach ( $blocks_in_file as $block ) {
    298300                                        if ( preg_match( '!\.(?:js|jsx)$!i', $relative_filename ) && empty( $block->script ) )
    299301                                                $block->script = $relative_filename;
     
    307309        }
    308310
    309311        public function find_block_assets( $base_dir ) {
    310                 $assets = Import::find_possible_block_assets( $base_dir );
     312                $assets = Import::find_possible_block_assets( $base_dir, $this->potential_block_directories );
    311313                $assets = array_map( array( $this, 'relative_filename' ), $assets );
    312314
    313315                $block_assets = array(
     
    338340         *
    339341         * @param string $file Path to a PHP file.
    340342         * @return array A list of functions found, in (function, line, file) form.
    341          */ 
     343         */
    342344        public static function find_called_functions_in_file($file) {
    343345                $source = file_get_contents($file);
    344346                $tokens = token_get_all($source, TOKEN_PARSE);
     
    363365                                $context[] = ' ';
    364366                        } else {
    365367                                $context[] = $token[0];
    366                         }       
     368                        }
    367369                }
    368370
    369371                return $function_calls;
     
    646648        }
    647649
    648650        /**
     651         * Did we find block assets?
     652         */
     653        function check_block_assets() {
     654                $js_asset_count = count( $this->block_assets['js'] );
     655                $js_asset_result_type = $js_asset_count > 0 ? 'info' : 'warning';
     656                $css_asset_count = count( $this->block_assets['css'] );
     657                $css_asset_result_type = $css_asset_count > 0 ? 'info' : 'warning';
     658
     659                $this->record_result(
     660                        __FUNCTION__,
     661                        $js_asset_result_type,
     662                        sprintf(
     663                                _n(
     664                                        'Found %d JS asset.',
     665                                        'Found %d JS assets.',
     666                                        $js_asset_count,
     667                                        'wporg-plugins'
     668                                ),
     669                                number_format_i18n( $js_asset_count )
     670                        ),
     671                        $this->block_assets['js']
     672                );
     673                $this->record_result(
     674                        __FUNCTION__,
     675                        $css_asset_result_type,
     676                        sprintf(
     677                                _n(
     678                                        'Found %d CSS asset.',
     679                                        'Found %d CSS assets.',
     680                                        $css_asset_count,
     681                                        'wporg-plugins'
     682                                ),
     683                                number_format_i18n( $css_asset_count )
     684                        ),
     685                        $this->block_assets['css']
     686                );
     687        }
     688
     689        /**
    649690         * Do the script files all exist?
    650691         */
    651692        function check_for_block_script_files() {
  • cli/class-import.php

     
    723723                        // There must be at least on JS file, so if only css was found, keep looking.
    724724                        if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
    725725                                // Then check for files in the current directory with "build" or "min" in the filename.
    726                                 $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '![_\-\.]+(?:build|dist|min)[_\-\.]+!i' );
     726                                $build_files += Filesystem::list_files( "$base_dir/$block_dir", true, '![_\-\.]+(?:build|dist|min)[_\-\.]+!i' );
    727727                        }
    728728
    729729                        if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
    730730                                // Finally, just grab whatever js/css files there are in the current directory.
    731                                 $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '#(?<!webpack\.config)\.(?:js|jsx|css)$#i' );
     731                                $build_files += Filesystem::list_files( "$base_dir/$block_dir", true, '#(?<!webpack\.config)\.(?:js|jsx|css)$#i' );
    732732                        }
    733733                }
    734734
  • shortcodes/class-block-validator.php

     
    297297                                ];
    298298                        case 'check_for_block_json':
    299299                                return __( 'Your plugin should contain at least one <code>block.json</code> file. This file contains metadata describing the block and its JavaScript and CSS assets. Make sure you include at least one <code>script</code> or <code>editorScript</code> item.', 'wporg-plugins' );
     300                        case 'check_block_assets':
     301                                if ( count( $result->data ) > 0 ) {
     302                                        return $result->data;
     303                                }
     304                                return __( 'The checker was not able to find any files for this type of asset.', 'wporg-plugins' );
    300305                        case 'check_for_block_script_files':
    301306                                return [
    302307                                        __( 'The value of <code>script</code>, <code>style</code>, <code>editorScript</code>, <code>editorStyle</code> must be a valid file path. This value was detected, but there is no file at this location in your plugin.', 'wporg-plugins' ),