| 399 | | return compact( 'readme', 'stable_tag', 'tmp_dir', 'plugin_headers', 'assets', 'tagged_versions' ); |
| | 407 | // Find blocks |
| | 408 | $blocks = array(); |
| | 409 | foreach ( Filesystem::list_files( "$tmp_dir/export/", false, '!\.(?:php|js|jsx|json)$!i' ) as $filename ) { |
| | 410 | $file_blocks = $this->find_blocks_in_file( $filename ); |
| | 411 | if ( $file_blocks ) { |
| | 412 | foreach ( $file_blocks as $block ) { |
| | 413 | // If the info came from a block.json file with more metadata (like description) then we want it to override less detailed info scraped from php/js. |
| | 414 | if ( empty( $blocks[ $block->name ]->title ) || isset( $block->description ) ) { |
| | 415 | $blocks[ $block->name ] = $block; |
| | 416 | } |
| | 417 | } |
| | 418 | } |
| | 419 | } |
| | 420 | |
| | 421 | return compact( 'readme', 'stable_tag', 'tmp_dir', 'plugin_headers', 'assets', 'tagged_versions', 'blocks' ); |
| | 483 | |
| | 484 | /** |
| | 485 | * Look for Gutenberg blocks registered within a single file. |
| | 486 | * |
| | 487 | * @param string $filename Pathname of the file. |
| | 488 | * |
| | 489 | * @return array An array of objects representing blocks, corresponding to the block.json format where possible. |
| | 490 | */ |
| | 491 | protected function find_blocks_in_file( $filename ) { |
| | 492 | |
| | 493 | $ext = strtolower( pathinfo($filename, PATHINFO_EXTENSION) ); |
| | 494 | |
| | 495 | $blocks = array(); |
| | 496 | |
| | 497 | if ( 'js' === $ext || 'jsx' === $ext ) { |
| | 498 | // Parse a js-style registerBlockType() call. |
| | 499 | // Note that this only works with literal strings for the block name and title, and assumes that order. |
| | 500 | $contents = file_get_contents( $filename ); |
| | 501 | if ( $contents && preg_match_all( "#registerBlockType\s*[(]\s*'([-\w]+/[-\w]+)'\s*,\s*[{]\s*title\s*:[\s_(]*'([^']*)'#ms", $contents, $matches, PREG_SET_ORDER ) ) { |
| | 502 | foreach ( $matches as $match ) { |
| | 503 | $blocks[] = (object) [ |
| | 504 | 'name' => $match[1], |
| | 505 | 'title' => $match[2], |
| | 506 | ]; |
| | 507 | } |
| | 508 | } |
| | 509 | } |
| | 510 | if ( 'php' === $ext ) { |
| | 511 | // Parse a php-style register_block_type() call. |
| | 512 | // Again this assumes literal strings, and only parses the name and title. |
| | 513 | $contents = file_get_contents( $filename ); |
| | 514 | if ( $contents && preg_match_all( "#register_block_type\s*[(]\s*['\"]([-\w]+/[-\w]+)['\"]#ms", $contents, $matches, PREG_SET_ORDER ) ) { |
| | 515 | foreach ( $matches as $match ) { |
| | 516 | $blocks[] = (object) [ |
| | 517 | 'name' => $match[1], |
| | 518 | 'title' => null, |
| | 519 | ]; |
| | 520 | } |
| | 521 | } |
| | 522 | } |
| | 523 | if ( 'block.json' === basename( $filename ) ) { |
| | 524 | // A block.json file has everything we want. |
| | 525 | $blockinfo = json_decode( file_get_contents( $filename ) ); |
| | 526 | if ( isset( $blockinfo->name ) && isset( $blockinfo->title ) ) { |
| | 527 | $blocks[] = $blockinfo; |
| | 528 | } |
| | 529 | } |
| | 530 | |
| | 531 | return $blocks; |
| | 532 | } |