Making WordPress.org

Changeset 10013


Ignore:
Timestamp:
07/06/2020 04:20:13 AM (5 years ago)
Author:
tellyworth
Message:

Plugin directory: add more validation rules.

Also improve a few existing rules.

File:
1 edited

Legend:

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

    r10012 r10013  
    3535    protected $blocks = null;
    3636    protected $block_json_files = null;
     37    protected $asset_php_files = null;
    3738    protected $block_json_validation = array();
    3839
     
    4445    public function __construct( $slug = null ) {
    4546        $this->slug = $slug;
     47
     48        add_filter( 'extra_plugin_headers', function( $extra_headers ) {
     49            $extra_headers[ 'License' ] = 'License';
     50            return $extra_headers;
     51        } );
    4652    }
    4753
     
    204210            $this->block_json_validation[ $block_json_file ] = $validator->validate( $block_json );
    205211        }
     212
     213        $this->asset_php_files = $this->find_asset_php_files( $this->path_to_plugin );
    206214    }
    207215
     
    261269
    262270    /**
     271     * Find index.asset.php files as generated by the create block scripts.
     272     */
     273    public function find_asset_php_files( $base_dir ) {
     274        return Filesystem::list_files( $base_dir, true, '!(:^|/)\w+\.asset\.php$!' );
     275    }
     276
     277    /**
    263278     * Used by check_*() functions to record info.
    264279     *
     
    297312     */
    298313    function check_license() {
    299         if ( empty( $this->readme->license ) ) {
     314        if ( empty( $this->readme->license ) && empty( $this->headers->License ) ) {
    300315            $this->record_result( __FUNCTION__,
    301316                'warning',
    302                 __( 'Missing license in readme.txt.', 'wporg-plugins' )
     317                __( 'No plugin license was found.', 'wporg-plugins' )
     318            );
     319        } elseif ( !empty( $this->readme->license ) ) {
     320            $this->record_result( __FUNCTION__,
     321                'info',
     322                sprintf( __( 'Found a license in readme.txt: %s.', 'wporg-plugins' ), $this->readme->license ),
     323                $this->readme->license
     324            );
     325        } elseif ( !empty( $this->headers->License ) ) {
     326            $this->record_result( __FUNCTION__,
     327                'info',
     328                sprintf( __( 'Found a license in plugin headers: %s.', 'wporg-plugins' ), $this->headers->License ),
     329                $this->headers->License
     330            );
     331        }
     332    }
     333
     334    /**
     335     * Must have plugin headers.
     336     */
     337    function check_plugin_headers() {
     338        if ( empty( $this->headers ) ) {
     339            $this->record_result( __FUNCTION__,
     340                'error',
     341                __( 'Missing plugin headers. Is this a WordPress plugin?', 'wporg-plugins' )
     342            );
     343        }
     344    }
     345
     346    /**
     347     * Should have the 'block' tag.
     348     */
     349    function check_block_tag() {
     350        if ( empty( $this->readme->tags ) || !in_array( 'block', array_map( 'strtolower', $this->readme_tags ) ) ) {
     351            $this->record_result( __FUNCTION__,
     352                'warning',
     353                __( 'No block tag found in readme.txt.', 'wporg-plugins' ),
     354                $this->readme->tags
    303355            );
    304356        } else {
    305357            $this->record_result( __FUNCTION__,
    306358                'info',
    307                 __( 'Found a license in readme.txt.', 'wporg-plugins' ),
    308                 $this->readme->license
     359                __( 'Found a block tag in readme.txt.', 'wporg-plugins' ),
     360                $this->readme->tags
    309361            );
    310362        }
     
    358410        } else {
    359411            $this->record_result( __FUNCTION__,
    360                 'info',
     412                ( count( $this->blocks ) < 5 ? 'info' : 'warning' ), // 5+ blocks suggests it's probably not a block plugin
    361413                sprintf( _n( 'Found %d block.', 'Found %d blocks.', count( $this->blocks ), 'wporg-plugins' ), count( $this->blocks ) ),
    362414                array_keys( $this->blocks )
     
    505557        }
    506558    }
     559
     560    /**
     561     * Does it have an index.asset.php file generate by the create block scripts?
     562     */
     563    function check_asset_php_file() {
     564        $count = 0;
     565        foreach ( $this->asset_php_files as $file ) {
     566            $contents = file_get_contents( $file );
     567            if ( false !== strpos( $contents, '<?php return array(' ) )
     568                ++ $count;
     569        }
     570
     571        if ( 1 === $count ) {
     572            $this->record_result( __FUNCTION__,
     573                'info',
     574                sprintf( __( 'PHP asset file found: %s.', 'wporg-plugins' ), $this->relative_filename( $file ) ),
     575                $this->relative_filename( $file )
     576            );
     577        } elseif ( $count > 1 ) {
     578            $this->record_result( __FUNCTION__,
     579                'warning',
     580                sprintf( __( 'Found %s PHP asset files.', 'wporg-plugins' ), $count ),
     581                array_map( array( $this, relative_filename ), $this->asset_php_files )
     582            );
     583        }
     584    }
     585
     586    /**
     587     * Does it have a lot of PHP code?
     588     */
     589    function check_php_size() {
     590        $php_files = Filesystem::list_files( $this->path_to_plugin, true, '!\.php$!i' );
     591        $total_size = 0;
     592        foreach ( $php_files as $file ) {
     593            $total_size += filesize( $file );
     594        }
     595        if ( $total_size > 50 * KB_IN_BYTES ) {
     596            $this->record_result( __FUNCTION__,
     597                'error',
     598                sprintf( __( 'Found %s of PHP code. Please submit PHP plugins to the plugin directory.', 'wporg-plugins' ), size_format( $total_size ) ),
     599                $total_size
     600            );
     601        } elseif ( $total_size > 10 * KB_IN_BYTES ) {
     602            $this->record_result( __FUNCTION__,
     603                'warning',
     604                sprintf( __( 'Found %s of PHP code. This might not be a block plugin.', 'wporg-plugins' ), size_format( $total_size ) ),
     605                $total_size
     606            );
     607        } else {
     608            $this->record_result( __FUNCTION__,
     609                'info',
     610                sprintf( __( 'Found %s of PHP code. Thanks for keeping it minimal!', 'wporg-plugins' ), size_format( $total_size ) ),
     611                $total_size
     612            );
     613        }
     614    }
    507615}
Note: See TracChangeset for help on using the changeset viewer.