WordPress.org

Making WordPress.org

Changeset 5198


Ignore:
Timestamp:
03/29/2017 07:11:15 PM (2 years ago)
Author:
obenland
Message:

Plugin Directory: No text domain check for plugins requiring 4.6+

Props ocean90 for initial commit.
See [dotorg12617].
Fixes #2586.

File:
1 edited

Legend:

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

    r4223 r5198  
    22namespace WordPressdotorg\Plugin_Directory\CLI\I18N;
    33
     4use WordPressdotorg\Plugin_Directory\Readme\Parser;
    45use WordPressdotorg\Plugin_Directory\Tools\SVN;
    56use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
     
    9394     */
    9495    private function is_plugin_valid( $export_directory ) {
    95         $error = new WP_Error();
    96         $esc_export_directory = escapeshellarg( $export_directory );
     96        $error  = new WP_Error();
     97        $readme = new Parser( $this->find_readme_file( $export_directory ) );
    9798
    98         // Check for a plugin text domain declaration and loading, grep recursively, not necessarily in plugin.php.
    99         $has_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:" ' . $esc_export_directory );
    100         $has_load_plugin_textdomain = shell_exec( 'grep -r --include "*.php" "\bload_plugin_textdomain\b" ' . $esc_export_directory );
     99        // Whether plugin files should be checked for valid text domains.
     100        if ( empty( $readme->requires ) || version_compare( $readme->requires, '4.6', '<' ) ) {
     101            $esc_export_directory = escapeshellarg( $export_directory );
    101102
    102         if ( $has_textdomain_header ) {
    103             $has_slug_as_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:[[:blank:]]*' . trim( escapeshellarg( $this->plugin ), '\'' ) . '" ' . $esc_export_directory );
    104             if ( ! $has_slug_as_textdomain_header ) {
    105                 $error->add( 'wrong_textdomain', 'Wrong text domain in header.' );
     103            // Check for a plugin text domain declaration and loading, grep recursively, not necessarily in plugin.php.
     104            $has_textdomain_header      = shell_exec( 'grep -r -i --include "*.php" "Text Domain:" ' . $esc_export_directory );
     105            $has_load_plugin_textdomain = shell_exec( 'grep -r --include "*.php" "\bload_plugin_textdomain\b" ' . $esc_export_directory );
     106
     107            if ( $has_textdomain_header ) {
     108                $has_slug_as_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:[[:blank:]]*' . trim( escapeshellarg( $this->plugin ), '\'' ) . '" ' . $esc_export_directory );
     109
     110                if ( ! $has_slug_as_textdomain_header ) {
     111                    $error->add( 'wrong_textdomain', 'Wrong text domain in header.' );
     112                }
     113            } else {
     114                $error->add( 'missing_textdomain', 'Missing text domain in header.' );
    106115            }
    107         } else {
    108             $error->add( 'missing_textdomain', 'Missing text domain in header.' );
    109         }
    110116
    111         if ( ! $has_load_plugin_textdomain ) {
    112             $error->add( 'missing_load_plugin_textdomain', 'Missing load_plugin_textdomain().' );
    113         }
     117            if ( ! $has_load_plugin_textdomain ) {
     118                $error->add( 'missing_load_plugin_textdomain', 'Missing load_plugin_textdomain().' );
     119            }
    114120
    115         if ( $error->get_error_codes() ) {
    116             return $error;
     121            if ( $error->get_error_codes() ) {
     122                return $error;
     123            }
     124
    117125        }
    118126
    119127        return true;
    120128    }
     129
     130    /**
     131     * Find the plugin readme file.
     132     *
     133     * Looks for either a readme.txt or readme.md file, prioritizing readme.txt.
     134     *
     135     * @param string $directory
     136     * @return string The plugin readme.txt or readme.md filename.
     137     */
     138    private function find_readme_file( $directory ) {
     139        $files = Filesystem::list_files( $directory, false /* non-recursive */, '!^readme\.(txt|md)$!i' );
     140
     141        // Prioritize readme.txt
     142        foreach ( $files as $file ) {
     143            if ( '.txt' === strtolower( substr( $file, -4 ) ) ) {
     144                return $file;
     145            }
     146        }
     147
     148        return reset( $files );
     149    }
    121150}
Note: See TracChangeset for help on using the changeset viewer.