Making WordPress.org

Changeset 10066


Ignore:
Timestamp:
07/13/2020 08:19:33 PM (4 years ago)
Author:
ryelle
Message:

Plugin Directory: Add a check for block name standards

This will flag as errors any block names that would not register in the editor (using the same regex). It also checks for some common block starter content namespaces, to ensure names are unique across the repo (as warnings).

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

    r10065 r10066  
    439439                        [ 'block_name' => $block->name, 'slug' => $post->post_name ]
    440440                    );
     441                }
     442            }
     443        }
     444    }
     445
     446    /**
     447     * Does the block name follow expected naming conventions
     448     */
     449    function check_for_standard_block_name() {
     450        foreach ( $this->blocks as $block ) {
     451            if ( ! trim( strval( $block->name ) ) ) {
     452                continue;
     453            }
     454            if ( ! preg_match( '/^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]+$/', $block->name ) ) {
     455                $this->record_result(
     456                    __FUNCTION__,
     457                    'error',
     458                    // translators: %s is the block name.
     459                    sprintf( __( 'Block name %s is invalid. Please use lowercase alphanumeric characters.', 'wporg-plugins' ), '<code>' . $block->name . '</code>' )
     460                );
     461            } else {
     462                $disallowed_ns = array( 'cgb/', 'create-block/', 'example/', 'block/', 'core/' );
     463                foreach ( $disallowed_ns as $ns ) {
     464                    if ( 0 === strpos( $block->name, $ns ) ) {
     465                        $this->record_result(
     466                            __FUNCTION__,
     467                            'warning',
     468                            sprintf(
     469                                // translators: %1$s is the block name, %2$s is the namespace.
     470                                __( 'Block %1$s uses namespace %2$s. Please use a unique namespace.', 'wporg-plugins' ),
     471                                '<code>' . $block->name . '</code>',
     472                                '<code>' . $ns . '</code>'
     473                            )
     474                        );
     475                        break;
     476                    }
    441477                }
    442478            }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-block-validator.php

    r10065 r10066  
    271271            case 'check_php_size':
    272272                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' );
     273            case 'check_for_standard_block_name':
     274                return [
     275                    __( 'Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. The namespace should be unique to your block plugin, make sure to change any defaults from block templates like "create-block/" or "cgb/".', 'wporg-plugins' ),
     276                    __( 'Example: <code>my-plugin/my-custom-block</code>', 'wporg-plugins' ),
     277                ];
    273278        }
    274279    }
Note: See TracChangeset for help on using the changeset viewer.