Making WordPress.org

Changeset 10058


Ignore:
Timestamp:
07/10/2020 09:28:14 PM (4 years ago)
Author:
ryelle
Message:

Plugin Directory: Update block.json file checks in block checker

Remove the json file check, since the same items are checked for in check_block_json_is_valid. Improve messages in check_for_block_script_files.

See #5303

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
2 edited

Legend:

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

    r10040 r10058  
    275275
    276276    public function find_block_scripts() {
    277         $block_scripts = array();
    278         foreach ( $this->blocks as $block ) {
    279             $scripts = Import::extract_file_paths_from_block_json( $block );
    280             if ( isset( $block_scripts[ $block->name ] ) ) {
    281                 $block_scripts[ $block->name ] = array_merge( $block_scripts[ $block->name ], $scripts );
    282             } else {
    283                 $block_scripts[ $block->name ] = $scripts;
    284             }
    285         }
     277        $block_scripts = \array_map(
     278            function( $block ) {
     279                $assets = array();
     280                $props = array( 'editorScript', 'script', 'editorStyle', 'style' );
     281
     282                foreach ( $props as $prop ) {
     283                    if ( isset( $block->$prop ) ) {
     284                        $assets[ $prop ] = $block->$prop;
     285                    }
     286                }
     287                return $assets;
     288            },
     289            $this->blocks
     290        );
    286291
    287292        return $block_scripts;
     
    479484
    480485    /**
    481      * Do the blocks all have available script assets, either discoverable or in the block.json file?
    482      */
    483     function check_for_block_scripts() {
    484         $block_scripts = $this->find_block_scripts();
    485 
    486         foreach ( $this->blocks as $block_name => $block_info ) {
    487             if ( ! empty( $block_scripts[ $block_name ] ) ) {
    488                 $this->record_result(
    489                     __FUNCTION__,
    490                     'info',
    491                     // translators: %s is the block name.
    492                     sprintf( __( 'Scripts found for block %s.', 'wporg-plugins' ), '<code>' . $block_name . '</code>' ),
    493                     $block_scripts[ $block_name ]
    494                 );
    495             } else {
    496                 $this->record_result(
    497                     __FUNCTION__,
    498                     'warning',
    499                     // translators: %s is the block name.
    500                     sprintf( __( 'No scripts found for block %s.', 'wporg-plugins' ), '<code>' . $block_name . '</code>' ),
    501                     $block_name
    502                 );
    503             }
    504         }
    505     }
    506 
    507     /**
    508486     * Check for a single parent block
    509487     */
     
    544522     */
    545523    function check_for_block_script_files() {
     524        $seen_scripts = array();
    546525        foreach ( $this->find_block_scripts() as $block_name => $scripts ) {
    547             foreach ( $scripts as $script ) {
    548                 if ( file_exists( $this->path_to_plugin . $script ) ) {
     526            foreach ( $scripts as $kind => $script ) {
     527                // No need to warn multiple times for the same value.
     528                if ( in_array( $script, $seen_scripts ) ) {
     529                    continue;
     530                }
     531                $seen_scripts[] = $script;
     532                $file_path = trailingslashit( $this->path_to_plugin ) . str_replace( 'file:', '', $script );
     533                if ( file_exists( $file_path ) ) {
    549534                    $this->record_result(
    550535                        __FUNCTION__,
    551536                        'info',
    552537                        // translators: %s is the block name.
    553                         sprintf( __( 'Script file exists for block %s.', 'wporg-plugins' ), '<code>' . $block_name . '</code>' ),
    554                         $script
     538                        sprintf( __( 'Found file %s.', 'wporg-plugins' ), '<code>' . $script . '</code>' ),
     539                        compact( $kind, $script )
    555540                    );
    556541                } else {
     542                    // translators: %s is the file name.
     543                    $message = __( 'Expected %s to be a valid file path.', 'wporg-plugins' );
     544                    if ( in_array( $kind, array( 'editorScript', 'script' ) ) ) {
     545                        // translators: %s is the file name.
     546                        $message = __( 'Expected %s to be a valid JavaScript file path.', 'wporg-plugins' );
     547                    } else if ( in_array( $kind, array( 'editorStyle', 'style' ) ) ) {
     548                        // translators: %s is the file name.
     549                        $message = __( 'Expected %s to be a valid CSS file path.', 'wporg-plugins' );
     550                    }
    557551                    $this->record_result(
    558552                        __FUNCTION__,
    559                         'warning',
    560                         // translators: %s is the block name.
    561                         sprintf( __( 'Missing script file for block %s.', 'wporg-plugins' ), '<code>' . $block_name . '</code>' ),
    562                         $script
     553                        'error',
     554                        sprintf( $message, '<code>' . $script . '</code>' ),
     555                        compact( $kind, $script )
    563556                    );
    564557                }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-block-validator.php

    r10057 r10058  
    240240                return false;
    241241            case 'check_block_tag':
    242                 return __( 'The readme.txt file must contain the tag "block" for this to be added to the block directory.', 'wporg-plugins' );
     242                return __( 'The readme.txt file must contain the tag "block" (singular) for this to be added to the block directory.', 'wporg-plugins' );
    243243            case 'check_for_duplicate_block_name':
    244244                return [
     
    254254            case 'check_for_block_json':
    255255                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' );
    256             case 'check_for_block_scripts':
    257                 return 'TODO';
    258256            case 'check_for_block_script_files':
    259                 return 'TODO';
     257                return [
     258                    __( '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' ),
     259                    __( 'Unlike regular blocks, plugins in the block directory cannot use a script handle for these values.', 'wporg-plugins' ),
     260                ];
    260261            case 'check_for_register_block_type':
    261262                return __( 'At least one of your JavaScript files must explicitly call registerBlockType(). Without that call, your block will not work in the editor.', 'wporg-plugins' );
     
    263264                return __( 'This block.json file is invalid. The Block Directory needs to be able to read this file.', 'wporg-plugins' );
    264265            case 'check_asset_php_file':
    265                 return 'TODO'; // Is this really an issue?
     266                return __( '', 'wporg-plugins' ); // @todo
    266267            case 'check_php_size':
    267268                return __( 'Block plugins should keep the PHP code to a mimmum. If you need a lot of PHP code, your plugin probably belongs in the main Plugin Directory rather than the Block Directory.', 'wporg-plugins' );
Note: See TracChangeset for help on using the changeset viewer.