Changeset 10013
- Timestamp:
- 07/06/2020 04:20:13 AM (5 years ago)
- 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 35 35 protected $blocks = null; 36 36 protected $block_json_files = null; 37 protected $asset_php_files = null; 37 38 protected $block_json_validation = array(); 38 39 … … 44 45 public function __construct( $slug = null ) { 45 46 $this->slug = $slug; 47 48 add_filter( 'extra_plugin_headers', function( $extra_headers ) { 49 $extra_headers[ 'License' ] = 'License'; 50 return $extra_headers; 51 } ); 46 52 } 47 53 … … 204 210 $this->block_json_validation[ $block_json_file ] = $validator->validate( $block_json ); 205 211 } 212 213 $this->asset_php_files = $this->find_asset_php_files( $this->path_to_plugin ); 206 214 } 207 215 … … 261 269 262 270 /** 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 /** 263 278 * Used by check_*() functions to record info. 264 279 * … … 297 312 */ 298 313 function check_license() { 299 if ( empty( $this->readme->license ) ) {314 if ( empty( $this->readme->license ) && empty( $this->headers->License ) ) { 300 315 $this->record_result( __FUNCTION__, 301 316 '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 303 355 ); 304 356 } else { 305 357 $this->record_result( __FUNCTION__, 306 358 'info', 307 __( 'Found a licensein readme.txt.', 'wporg-plugins' ),308 $this->readme-> license359 __( 'Found a block tag in readme.txt.', 'wporg-plugins' ), 360 $this->readme->tags 309 361 ); 310 362 } … … 358 410 } else { 359 411 $this->record_result( __FUNCTION__, 360 'info',412 ( count( $this->blocks ) < 5 ? 'info' : 'warning' ), // 5+ blocks suggests it's probably not a block plugin 361 413 sprintf( _n( 'Found %d block.', 'Found %d blocks.', count( $this->blocks ), 'wporg-plugins' ), count( $this->blocks ) ), 362 414 array_keys( $this->blocks ) … … 505 557 } 506 558 } 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 } 507 615 }
Note: See TracChangeset
for help on using the changeset viewer.