Making WordPress.org


Ignore:
Timestamp:
04/12/2023 04:50:46 AM (2 years ago)
Author:
dd32
Message:

Plugin Directory: Import the Requires Plugins header on plugin import.

At present this is not used for anything, and is for future purposes.

See #6921, https://core.trac.wordpress.org/ticket/22316.

File:
1 edited

Legend:

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

    r12463 r12532  
    4242    // Plugin headers that are stored in plugin meta
    4343    public $plugin_headers = array(
    44         // Header    => meta_key
    45         'Name'       => 'header_name',
    46         'PluginURI'  => 'header_plugin_uri',
    47         'Author'     => 'header_author',
    48         'AuthorURI'  => 'header_author_uri',
    49         'TextDomain' => 'header_textdomain',
     44        // Header         => meta_key
     45        'Name'            => 'header_name',
     46        'PluginURI'       => 'header_plugin_uri',
     47        'Author'          => 'header_author',
     48        'AuthorURI'       => 'header_author_uri',
     49        'TextDomain'      => 'header_textdomain',
     50        'RequiresPlugins' => 'requires_plugins'
    5051
    5152        // These headers are stored in these fields, but are handled separately.
     
    723724     *
    724725     * @param string $directory The directory of the plugin.
     726     * @param int    $max_depth The maximum depth to search for files. Default: current directory only.
    725727     *
    726728     * @return object The plugin headers.
    727729     */
    728     public static function find_plugin_headers( $directory ) {
    729         $files = Filesystem::list_files( $directory, false, '!\.php$!i' );
     730    public static function find_plugin_headers( $directory, $max_depth = -1 ) {
     731        $files = Filesystem::list_files( $directory, ( $max_depth > 0 ), '!\.php$!i', $max_depth );
    730732
    731733        if ( ! function_exists( 'get_plugin_data' ) ) {
     
    733735        }
    734736
     737        // Add any additional headers required.
     738        add_filter( 'extra_plugin_headers', array( __CLASS__, 'add_extra_plugin_headers' ) );
     739
    735740        /*
    736741         * Sometimes plugins have multiple files which we detect as a plugin based on the headers.
    737          * We'll return immediately if the file has a `Plugin Name:` header, otherwise
     742         * We'll break immediately if the file has a `Plugin Name:` header, otherwise
    738743         * simply return the last set of headers we come across.
    739744         */
    740         $possible_headers = false;
     745        $headers = false;
    741746        foreach ( $files as $file ) {
    742747            $data = get_plugin_data( $file, false, false );
    743748            if ( array_filter( $data ) ) {
    744                 if ( $data['Name'] ) {
    745                     return (object) $data;
    746                 } else {
    747                     $possible_headers = (object) $data;
    748                 }
    749             }
    750         }
    751 
    752         if ( $possible_headers ) {
    753             return $possible_headers;
    754         }
    755 
    756         return false;
     749                $data['PluginFile'] = $file;
     750                $headers            = $data;
     751
     752                if ( $headers['Name'] ) {
     753                    break;
     754                }
     755            }
     756        }
     757
     758        remove_filter( 'extra_plugin_headers', array( __CLASS__, 'add_extra_plugin_headers' ) );
     759
     760        if ( ! $headers ) {
     761            return false;
     762        }
     763
     764        // The extra_plugin_headers filter doesn't let you set the key.
     765        foreach ( self::add_extra_plugin_headers( [] ) as $key => $header ) {
     766            if (
     767                $key != $header &&
     768                ! isset( $headers[ $key ] ) &&
     769                isset( $headers[ $header ] )
     770            ) {
     771                $headers[ $key ] = $headers[ $header ];
     772                unset( $headers[ $header ] );
     773            }
     774        }
     775
     776        return (object) $headers;
     777    }
     778
     779    /**
     780     * Add support for additional plugin headers prior to WordPress supporting it.
     781     *
     782     * @param array $headers The headers to look for in plugins.
     783     * @return array
     784     */
     785    public static function add_extra_plugin_headers( $headers ) {
     786        // WordPress Plugin Dependencies - See https://meta.trac.wordpress.org/ticket/6921
     787        if ( ! isset( $headers['RequiresPlugins'] ) ) {
     788            $headers['RequiresPlugins'] = 'Requires Plugins';
     789        }
     790
     791        return $headers;
    757792    }
    758793
Note: See TracChangeset for help on using the changeset viewer.