Making WordPress.org

Changeset 10049


Ignore:
Timestamp:
07/10/2020 01:20:10 AM (5 years ago)
Author:
tellyworth
Message:

Plugin directory: show expandable details for validator messages.

Props ryelle.
See #5303.

File:
1 edited

Legend:

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

    r10038 r10049  
    5959        $plugin = Plugin_Directory::get_plugin_post( $slug );
    6060
    61         return ( 
     61        return (
    6262            $plugin &&
    63             $plugin->post_name === $slug && 
     63            $plugin->post_name === $slug &&
    6464            has_term( 'block', 'plugin_section', $plugin )
    6565        );
     
    8989
    9090        $results_by_type = array();
     91        $block_json_issues = array();
    9192        foreach ( $results as $item ) {
    92             $results_by_type[ $item->type ][] = $item;
     93            if ( 'check_block_json_is_valid' === $item->check_name ) {
     94                $block_json_issues[] = $item;
     95            } else {
     96                $results_by_type[ $item->type ][] = $item;
     97            }
    9398        }
    9499
     
    102107                echo '<p>';
    103108                if ( ! empty( $results_by_type['error'] ) ) {
     109                    // translators: %s plugin title.
    104110                    printf( __( "%s can't be added to the block directory, due to errors in validation.", 'wporg-plugins' ), $plugin->post_title );
    105111                } else if ( self::plugin_is_in_block_directory( $checker->slug ) ) {
     112                    // translators: %s plugin title.
    106113                    echo '<button type="submit" name="block-directory-edit" value="remove">' . sprintf( __( 'Remove %s from Block Directory', 'wporg-plugins' ), $plugin->post_title ) . '</button>';
    107114                } else {
     115                    // translators: %s plugin title.
    108116                    echo '<button type="submit" name="block-directory-edit" value="add">' . sprintf( __( 'Add %s to Block Directory', 'wporg-plugins' ), $plugin->post_title ) . '</button>';
    109117                }
    110118                echo '</p>';
    111    
     119
    112120                echo '<ul><li><a href="' . get_edit_post_link( $plugin->ID ) . '">' . __( 'Edit plugin', 'wporg-plugins' ) . '</a></li>';
    113                 echo '<li><a href="' . esc_url( 'https://plugins.trac.wordpress.org/browser/' . $checker->slug . '/trunk' ) .'">' . __( 'Trac browser', 'wporg-plugins' ) . '</a></li></ul>';
     121                echo '<li><a href="' . esc_url( 'https://plugins.trac.wordpress.org/browser/' . $checker->slug . '/trunk' ) . '">' . __( 'Trac browser', 'wporg-plugins' ) . '</a></li></ul>';
    114122                echo '</form>';
    115123            }
     
    146154            $output .= "<h3>{$warning_label}</h3>\n";
    147155            $output .= "<div class='notice notice-{$type} notice-alt'>\n";
    148             $output .= "<ul class='{$type}'>\n";
    149156            foreach ( $results_by_type[ $type ] as $item ) {
    150                 $docs_link = '';
    151                 if ( 'check' === substr( $item->check_name, 0, 5 ) ) {
    152                     $docs_link = "<a href='help#{$item->check_name}'>" . __( 'More about this.', 'wporg-plugins' ) . '</a>';
     157                // Only get details if this is a warning or error.
     158                $details = ( 'info' === $type ) ? false : self::get_detailed_help( $item->check_name );
     159                if ( $details ) {
     160                    $details = '<p>' . implode( '</p><p>', (array) $details ) . '</p>';
     161                    $output .= "<details class='{$item->check_name}'><summary>{$item->message}</summary>{$details}</details>";
     162                } else {
     163                    $output .= "<p>{$item->message}</p>";
    153164                }
    154                 $output .= "<li class='{$item->check_name}'>{$item->message} {$docs_link}</li>\n";
    155             }
    156             $output .= "</ul>\n";
     165            }
     166            // Collapse block.json warnings into one details at the end of warnings list.
     167            if ( 'warning' === $type && ! empty( $block_json_issues ) ) {
     168                $messages = wp_list_pluck( $block_json_issues, 'message' );
     169                $details = '<p>' . implode( '</p><p>', (array) $messages ) . '</p>';
     170                $output .= sprintf(
     171                    '<details class="check_block_json_is_valid"><summary>%1$s</summary>%2$s</details>',
     172                    __( 'Issues found in block.json file.', 'wporg-plugins' ),
     173                    $details
     174                );
     175            }
    157176            $output .= "</div>\n";
    158177        }
     
    166185        echo $output;
    167186    }
     187
     188    /**
     189     * Get a more detailed help message for a given check.
     190     *
     191     * @param string $method The name of the check method.
     192     *
     193     * @return string|array More details for a given block issue. Array of strings if there should be a linebreak.
     194     */
     195    public static function get_detailed_help( $method ) {
     196        switch ( $method ) {
     197            // These don't need more details.
     198            case 'check_readme_exists':
     199            case 'check_license':
     200            case 'check_plugin_headers':
     201                return false;
     202            // This is a special case, since multiple values may be collapsed.
     203            case 'check_block_json_is_valid':
     204                return false;
     205            case 'check_block_tag':
     206                return __( 'The readme.txt file must contain the tag "block" for this to be added to the block directory.', 'wporg-plugins' );
     207            case 'check_for_duplicate_block_name':
     208                return [
     209                    __( "Block names must be unique, otherwise it can cause problems when using the block. It is recommended to use your plugin's name as the namespace.", 'wporg-plugins' ),
     210                    '<em>' . __( 'If this is a different version of your own plugin, you can ignore this warning.', 'wporg-plugins' ) . '</em>',
     211                ];
     212            case 'check_for_blocks':
     213                return [
     214                    __( 'In order to work in the Block Directory, a plugin must register a block. Generally one per plugin (multiple blocks may be permitted if those blocks are interdependent, such as a list block that contains list item blocks).', 'wporg-plugins' ),
     215                    __( 'If your plugin doesn’t register a block, it probably belongs in the main Plugin Directory rather than the Block Directory.', 'wporg-plugins' ),
     216                    sprintf( '<a href="TUTORIAL">%s</a>', __( 'Learn how to create a block.' ) ),
     217                ];
     218            case 'check_for_block_json':
     219                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' );
     220            case 'check_for_block_scripts':
     221                return 'TODO';
     222            case 'check_for_block_script_files':
     223                return 'TODO';
     224            case 'check_for_register_block_type':
     225                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' );
     226            case 'check_block_json_is_valid_json':
     227                return __( 'This block.json file is invalid. The Block Directory needs to be able to read this file.', 'wporg-plugins' );
     228            case 'check_asset_php_file':
     229                return 'TODO'; // Is this really an issue?
     230            case 'check_php_size':
     231                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' );
     232        }
     233    }
    168234}
Note: See TracChangeset for help on using the changeset viewer.