Making WordPress.org

Changeset 12532


Ignore:
Timestamp:
04/12/2023 04:50:46 AM (3 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.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
2 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
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload-handler.php

    r12526 r12532  
    22namespace WordPressdotorg\Plugin_Directory\Shortcodes;
    33
     4use WordPressdotorg\Plugin_Directory\CLI\Import;
    45use WordPressdotorg\Plugin_Directory\Readme\Parser;
    56use WordPressdotorg\Plugin_Directory\Plugin_Directory;
     
    6465                $this->plugin_dir = Filesystem::unzip( $zip_file );
    6566
    66                 $plugin_files = Filesystem::list_files( $this->plugin_dir, true /* Recursive */, '!\.php$!i', 1 /* Depth */ );
    67                 foreach ( $plugin_files as $plugin_file ) {
    68                         $plugin_data = get_plugin_data( $plugin_file, false, false ); // No markup/translation needed.
    69                         if ( ! empty( $plugin_data['Name'] ) ) {
    70                                 $this->plugin      = $plugin_data;
    71                                 $this->plugin_root = dirname( $plugin_file );
    72                                 break;
    73                         }
     67                $plugin_data = (array) Import::find_plugin_headers( $this->plugin_dir, 1 /* Max Depth to search */ );
     68                if ( ! empty( $plugin_data['Name'] ) ) {
     69                        $this->plugin      = $plugin_data;
     70                        $this->plugin_root = dirname( $plugin_data['PluginFile'] );
    7471                }
    7572
     
    262259
    263260                // Check for a valid readme.
    264                 $readme = $this->find_readme_file();
     261                $readme = Import::find_readme_file( $this->plugin_root );
    265262                if ( empty( $readme ) ) {
    266263                        $error = __( 'Error: The plugin has no readme.', 'wporg-plugins' );
     
    381378                                        'header_textdomain'        => $this->plugin['TextDomain'],
    382379                                        'header_description'       => $this->plugin['Description'],
     380                                        'requires_plugins'         => $this->plugin['RequiresPlugins'],
    383381                                        'assets_screenshots'       => array(),
    384382                                        'assets_icons'             => array(),
     
    677675
    678676        /**
    679          * Find the plugin readme file.
    680          *
    681          * Looks for either a readme.txt or readme.md file, prioritizing readme.txt.
    682          *
    683          * @return string The plugin readme.txt or readme.md filename.
    684          */
    685         protected function find_readme_file() {
    686                 $files = Filesystem::list_files( $this->plugin_root, false /* non-recursive */, '!^readme\.(txt|md)$!i' );
    687 
    688                 // Prioritize readme.txt file.
    689                 foreach ( $files as $file ) {
    690                         if ( '.txt' === strtolower( substr( $file, -4 ) ) ) {
    691                                 return $file;
    692                         }
    693                 }
    694 
    695                 return reset( $files );
    696         }
    697 
    698         /**
    699677         * Sends a plugin through Plugin Check.
    700678         *
Note: See TracChangeset for help on using the changeset viewer.