Making WordPress.org

Changeset 9934


Ignore:
Timestamp:
05/27/2020 04:56:53 AM (4 years ago)
Author:
tellyworth
Message:

Plugin dir: improve block asset detection during import.

This refactors the code and does a smarter job of finding asset files. It successfully works with most blocks currently in the directory.

Props coreymckrill.
See #5207

File:
1 edited

Legend:

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

    r9892 r9934  
    444444        }
    445445
    446         // Find blocks
    447         $blocks = array();
    448         $files_with_blocks = array();
    449         foreach ( Filesystem::list_files( "$tmp_dir/export/", true /* recursive */, '!\.(?:php|js|jsx|json)$!i' ) as $filename ) {
    450             $file_blocks = $this->find_blocks_in_file( $filename );
    451             if ( $file_blocks ) {
    452                 $files_with_blocks[] = str_replace( "$tmp_dir/export/", '', $filename );
    453                 foreach ( $file_blocks as $block ) {
    454                     // 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.
    455                     if ( empty( $blocks[ $block->name ]->title ) || isset( $block->description ) ) {
    456                         $blocks[ $block->name ] = $block;
    457                     }
    458                 }
    459             }
    460         }
    461 
    462         foreach ( $blocks as $block_name => $block ) {
    463             if ( empty( $block->title ) )
    464                 $blocks[ $block_name ]->title = $trunk_readme->name;
    465         }
    466 
    467446        if ( 'trunk' === $stable_tag ) {
    468447            $stable_path = $stable_tag;
    469448        } else {
    470             $stable_path = "tags/$stable_tag";
    471         }
    472 
    473         // Find blocks dist/build JS files
     449            $stable_path  = 'tags/';
     450            $stable_path .= $_stable_tag ?? $stable_tag;
     451        }
     452
     453        // Find registered blocks and their files.
     454        $blocks = array();
    474455        $block_files = array();
    475         $dist_files = SVN::ls( 'https://plugins.svn.wordpress.org' . "/{$plugin_slug}/{$stable_path}/dist" ) ?: array();
    476         $build_files = SVN::ls( 'https://plugins.svn.wordpress.org' . "/{$plugin_slug}/{$stable_path}/build" ) ?: array();
    477 
    478         foreach ( $dist_files as $file ) {
    479             $block_files[] = "/{$stable_path}/dist/" . $file;
    480         }
    481 
    482         foreach ( $build_files as $file ) {
    483             $block_files[] = "/{$stable_path}/build/" . $file;
    484         }
    485 
     456        $potential_block_directories = array();
     457        $base_dir = "$tmp_dir/export";
     458
     459        $block_json_files = Filesystem::list_files( $base_dir, true, '!^block\.json$!i' );
     460        if ( ! empty( $block_json_files ) ) {
     461            foreach ( $block_json_files as $filename ) {
     462                $blocks_in_file = $this->find_blocks_in_file( $filename );
     463                $relative_filename = str_replace( "$base_dir/", '', $filename );
     464                $potential_block_directories[] = dirname( $relative_filename );
     465                foreach ( $blocks_in_file as $block ) {
     466                    $blocks[ $block->name ] = $block;
     467
     468                    $extracted_files = $this->extract_file_paths_from_block_json( $block, dirname( $relative_filename ) );
     469                    if ( ! empty( $extracted_files ) ) {
     470                        $block_files = array_merge(
     471                            $block_files,
     472                            array_map(
     473                                function( $file ) use ( $stable_path ) {
     474                                    return "/$stable_path/" . ltrim( $file, '\\' );
     475                                },
     476                                $extracted_files
     477                            )
     478                        );
     479                    }
     480                }
     481            }
     482        } else {
     483            foreach ( Filesystem::list_files( $base_dir, true, '!\.(?:php|js|jsx)$!i' ) as $filename ) {
     484                $blocks_in_file = $this->find_blocks_in_file( $filename );
     485                if ( ! empty( $blocks_in_file ) ) {
     486                    $relative_filename = str_replace( "$base_dir/", '', $filename );
     487                    $potential_block_directories[] = dirname( $relative_filename );
     488                    foreach ( $blocks_in_file as $block ) {
     489                        $blocks[ $block->name ] = $block;
     490                    }
     491                }
     492            }
     493        }
     494
     495        foreach ( $blocks as $block_name => $block ) {
     496            if ( empty( $block->title ) ) {
     497                $blocks[ $block_name ]->title = $trunk_readme->name;
     498            }
     499        }
     500
     501        // Only search for block files if none were found in a block.json.
    486502        if ( empty( $block_files ) ) {
    487             foreach ( $files_with_blocks as $file ) {
    488                 $block_files[] = "/{$stable_path}/" . $file;
     503            $build_files = array();
     504
     505            if ( ! empty( $potential_block_directories ) ) {
     506                $potential_block_directories = array_unique( $potential_block_directories );
     507
     508                foreach ( $potential_block_directories as $block_dir ) {
     509                    // dirname() returns . when there is no directory separator present.
     510                    if ( '.' === $block_dir ) {
     511                        $block_dir = '';
     512                    }
     513
     514                    // First look for a dedicated "build" or "dist" directory.
     515                    foreach ( array( 'build', 'dist' ) as $dirname ) {
     516                        if ( is_dir( "$base_dir/$block_dir/$dirname" ) ) {
     517                            $build_files += Filesystem::list_files( "$base_dir/$block_dir/$dirname", true, '!\.(?:js|jsx|css)$!i' );
     518                        }
     519                    }
     520
     521                    // There must be at least on JS file, so if only css was found, keep looking.
     522                    if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     523                        // Then check for files in the current directory with "build" or "min" in the filename.
     524                        $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '![_\-\.]+(?:build|dist|min)[_\-\.]+!i' );
     525                    }
     526
     527                    if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     528                        // Finally, just grab whatever js/css files there are in the current directory.
     529                        $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '!\.(?:js|jsx|css)$!i' );
     530                    }
     531                }
     532            }
     533
     534            if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     535                // Nothing in the potential block directories. Check if we somehow missed build/dist directories in the root.
     536                foreach ( array( 'build', 'dist' ) as $dirname ) {
     537                    if ( is_dir( "$base_dir/$dirname" ) ) {
     538                        $build_files += Filesystem::list_files( "$base_dir/$dirname", true, '!\.(?:js|jsx|css)$!i' );
     539                    }
     540                }
     541            }
     542
     543            if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     544                // Still nothing. Take on last wild swing.
     545                $build_files += Filesystem::list_files( $base_dir, false, '!\.(?:js|jsx|css)$!i' );
     546            }
     547
     548            foreach ( $build_files as $file ) {
     549                $block_files[] = "/$stable_path/" . str_replace( "$base_dir/", '', $file );
    489550            }
    490551        }
     
    607668        return $blocks;
    608669    }
     670
     671    /**
     672     * Get script and style file paths from an imported block.json.
     673     *
     674     * @param object $parsed_json
     675     * @param string $block_json_path
     676     *
     677     * @return array
     678     */
     679    protected function extract_file_paths_from_block_json( $parsed_json, $block_json_path = '' ) {
     680        $files = array();
     681
     682        $props = array( 'editorScript', 'script', 'editorStyle', 'style' );
     683
     684        foreach ( $props as $prop ) {
     685            if ( isset( $parsed_json->$prop ) ) {
     686                $files[] = trailingslashit( $block_json_path ) . $parsed_json->$prop;
     687            }
     688        }
     689
     690        return $files;
     691    }
    609692}
Note: See TracChangeset for help on using the changeset viewer.