Making WordPress.org

Changeset 10065


Ignore:
Timestamp:
07/13/2020 07:40:02 PM (5 years ago)
Author:
ryelle
Message:

Plugin Directory: Update duplicate block name check

Switches the check to flag an error if we find the block name in a different repo plugin, or a warning if the found block has the same author.

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

    r10058 r10065  
    400400    /**
    401401     * Does the plugin have a block name that already exists in the DB?
    402      * Note that this isn't a blocker if we're re-running checks on a plugin that has already been uploaded, since it will match with itself.
     402     * Note that this does not flag a match if we can tell it's the same plugin already in the repo. It will only
     403     * trigger a warning if we identify the matched plugin to be the same author, and will trigger an error in all
     404     * other cases.
    403405     */
    404406    function check_for_duplicate_block_name() {
    405407        foreach ( $this->blocks as $block ) {
    406             if ( !trim( strval( $block->name ) ) )
     408            if ( ! trim( strval( $block->name ) ) ) {
    407409                continue;
     410            }
    408411
    409412            $query_args = array(
     
    413416                        'key' => 'block_name',
    414417                        'value' => $block->name,
    415                     )
    416                 )
     418                    ),
     419                ),
    417420            );
    418421
     
    420423            if ( $query->found_posts > 0 ) {
    421424                foreach ( $query->posts as $post ) {
    422                     if ( $this->slug && $this->slug === $post->post_name )
    423                         continue; // It's this very same plugin
    424 
    425                     $this->record_result( __FUNCTION__,
    426                         'info',
    427                         sprintf( __( 'Block name %s already exists in plugin %s.', 'wporg-plugins' ), $block->name, $query->posts[0]->post_name ),
     425                    if ( $this->slug && $this->slug === $post->post_name ) {
     426                        continue; // It's this very same plugin.
     427                    }
     428
     429                    $this->record_result(
     430                        __FUNCTION__,
     431                        // Check the author, since this might be the same plugin hosted on github.
     432                        ( get_current_user_id() === $post->post_author ) ? 'warning' : 'error',
     433                        sprintf(
     434                            // translators: %1$s is the block slug, %2$s is the found plugin title.
     435                            __( 'Block name %1$s already exists in the plugin "%2$s."', 'wporg-plugins' ),
     436                            '<code>' . $block->name . '</code>',
     437                            $query->posts[0]->post_title
     438                        ),
    428439                        [ 'block_name' => $block->name, 'slug' => $post->post_name ]
    429440                    );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-block-validator.php

    r10059 r10065  
    192192            foreach ( $results_by_type[ $type ] as $item ) {
    193193                // Only get details if this is a warning or error.
    194                 $details = ( 'info' === $type ) ? false : self::get_detailed_help( $item->check_name );
     194                $details = ( 'info' === $type ) ? false : self::get_detailed_help( $item->check_name, $item );
    195195                if ( $details ) {
    196196                    $details = '<p>' . implode( '</p><p>', (array) $details ) . '</p>';
     
    226226     *
    227227     * @param string $method The name of the check method.
     228     * @param array  $result The full result data.
    228229     *
    229230     * @return string|array More details for a given block issue. Array of strings if there should be a linebreak.
    230231     */
    231     public static function get_detailed_help( $method ) {
     232    public static function get_detailed_help( $method, $result ) {
    232233        switch ( $method ) {
    233234            // These don't need more details.
     
    242243                return __( 'The readme.txt file must contain the tag "block" (singular) for this to be added to the block directory.', 'wporg-plugins' );
    243244            case 'check_for_duplicate_block_name':
    244                 return [
     245                $details = [
    245246                    __( "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' ),
    246                     '<em>' . __( 'If this is a different version of your own plugin, you can ignore this warning.', 'wporg-plugins' ) . '</em>',
    247247                ];
     248                if ( 'warning' === $result->type ) {
     249                    $details[] = '<em>' . __( 'If this is a different version of your own plugin, you can ignore this warning.', 'wporg-plugins' ) . '</em>';
     250                }
     251                return $details;
    248252            case 'check_for_blocks':
    249253                return [
Note: See TracChangeset for help on using the changeset viewer.