WordPress.org

Making WordPress.org

Changeset 3523


Ignore:
Timestamp:
06/22/2016 08:32:57 AM (5 years ago)
Author:
ocean90
Message:

Plugin Directory: Move plugin validation into a separate method.

See #1691.

File:
1 edited

Legend:

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

    r3515 r3523  
    55use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
    66use Exception;
     7use WP_Error;
    78
    89/**
     
    5354        }
    5455
    55         $esc_export_directory = escapeshellarg( $export_directory );
    56 
    57         // Check for a plugin text domain declaration and loading, grep recursively, not necessarily in plugin.php.
    58         $has_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:" ' . $esc_export_directory );
    59         $has_load_plugin_textdomain = shell_exec( 'grep -r --include "*.php" "\bload_plugin_textdomain\b" ' . $esc_export_directory );
    60 
    61         $has_slug_as_textdomain_header = false;
    62         if ( $has_textdomain_header ) {
    63             $has_slug_as_textdomain_header = shell_exec( 'grep -r -i --include "*.php" "Text Domain:[[:blank:]]*' . trim( escapeshellarg( $this->plugin ), '\'' ) . '" ' . $esc_export_directory );
    64             if ( ! $has_slug_as_textdomain_header ) {
    65                 echo 'Wrong text domain in header';
    66             }
    67         } else {
    68             echo 'Missing text domain in header';
    69         }
    70 
    71         if ( ! $has_load_plugin_textdomain ) {
    72             echo 'Missing load_plugin_textdomain()';
    73         }
    74 
    75         if ( ! $has_textdomain_header || ! $has_slug_as_textdomain_header || ! $has_load_plugin_textdomain ) {
     56        $valid = $this->is_plugin_valid( $export_directory );
     57        if ( is_wp_error( $valid ) ) {
    7658            throw new Exception( 'Plugin is not compatible with language packs.' );
    7759        }
     
    10183        }
    10284    }
     85
     86    /**
     87     * Checks if a plugin has the correct text domain declarations.
     88     *
     89     * @todo This can be removed/changed if WordPress 4.6 is released.
     90     *
     91     * @param string $export_directory
     92     * @return true|\WP_Error True if valid, WP_Error in case of failures.
     93     */
     94    private function is_plugin_valid( $export_directory ) {
     95        $error = new WP_Error();
     96        $esc_export_directory = escapeshellarg( $export_directory );
     97
     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 );
     101
     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.' );
     106            }
     107        } else {
     108            $error->add( 'missing_textdomain', 'Missing text domain in header.' );
     109        }
     110
     111        if ( ! $has_load_plugin_textdomain ) {
     112            $error->add( 'missing_load_plugin_textdomain', 'Missing load_plugin_textdomain().' );
     113        }
     114
     115        if ( $error->get_error_codes() ) {
     116            return $error;
     117        }
     118
     119        return true;
     120    }
    103121}
Note: See TracChangeset for help on using the changeset viewer.