Make WordPress Core


Ignore:
Timestamp:
11/26/2024 10:14:07 AM (6 months ago)
Author:
swissspidy
Message:

I18N: Load translations just-in-time for custom themes and plugins.

In #34114, just-in-time (JIT) translation loading was implemented for projects hosted on WordPress.org. This is now expanded to all other plugins/themes.

Projects with a custom Text Domain and Domain Path header no longer need to call load_plugin_textdomain() or load_theme_textdomain().

This reduces the risk of calling them too late, after some translation calls already happened, and generally makes it easier to properly internationalize a plugin or theme.

This moves the get_plugin_data() from wp-admin/includes/plugin.php to wp-includes/functions.php so it's available during the plugin loading process.

Props swissspidy.
Fixes #62244.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r59389 r59461  
    69316931
    69326932/**
     6933 * Parses the plugin contents to retrieve plugin's metadata.
     6934 *
     6935 * All plugin headers must be on their own line. Plugin description must not have
     6936 * any newlines, otherwise only parts of the description will be displayed.
     6937 * The below is formatted for printing.
     6938 *
     6939 *     /*
     6940 *     Plugin Name: Name of the plugin.
     6941 *     Plugin URI: The home page of the plugin.
     6942 *     Description: Plugin description.
     6943 *     Author: Plugin author's name.
     6944 *     Author URI: Link to the author's website.
     6945 *     Version: Plugin version.
     6946 *     Text Domain: Optional. Unique identifier, should be same as the one used in
     6947 *          load_plugin_textdomain().
     6948 *     Domain Path: Optional. Only useful if the translations are located in a
     6949 *          folder above the plugin's base path. For example, if .mo files are
     6950 *          located in the locale folder then Domain Path will be "/locale/" and
     6951 *          must have the first slash. Defaults to the base folder the plugin is
     6952 *          located in.
     6953 *     Network: Optional. Specify "Network: true" to require that a plugin is activated
     6954 *          across all sites in an installation. This will prevent a plugin from being
     6955 *          activated on a single site when Multisite is enabled.
     6956 *     Requires at least: Optional. Specify the minimum required WordPress version.
     6957 *     Requires PHP: Optional. Specify the minimum required PHP version.
     6958 *     * / # Remove the space to close comment.
     6959 *
     6960 * The first 8 KB of the file will be pulled in and if the plugin data is not
     6961 * within that first 8 KB, then the plugin author should correct their plugin
     6962 * and move the plugin data headers to the top.
     6963 *
     6964 * The plugin file is assumed to have permissions to allow for scripts to read
     6965 * the file. This is not checked however and the file is only opened for
     6966 * reading.
     6967 *
     6968 * @since 1.5.0
     6969 * @since 5.3.0 Added support for `Requires at least` and `Requires PHP` headers.
     6970 * @since 5.8.0 Added support for `Update URI` header.
     6971 * @since 6.5.0 Added support for `Requires Plugins` header.
     6972 *
     6973 * @param string $plugin_file Absolute path to the main plugin file.
     6974 * @param bool   $markup      Optional. If the returned data should have HTML markup applied.
     6975 *                            Default true.
     6976 * @param bool   $translate   Optional. If the returned data should be translated. Default true.
     6977 * @return array {
     6978 *     Plugin data. Values will be empty if not supplied by the plugin.
     6979 *
     6980 *     @type string $Name            Name of the plugin. Should be unique.
     6981 *     @type string $PluginURI       Plugin URI.
     6982 *     @type string $Version         Plugin version.
     6983 *     @type string $Description     Plugin description.
     6984 *     @type string $Author          Plugin author's name.
     6985 *     @type string $AuthorURI       Plugin author's website address (if set).
     6986 *     @type string $TextDomain      Plugin textdomain.
     6987 *     @type string $DomainPath      Plugin's relative directory path to .mo files.
     6988 *     @type bool   $Network         Whether the plugin can only be activated network-wide.
     6989 *     @type string $RequiresWP      Minimum required version of WordPress.
     6990 *     @type string $RequiresPHP     Minimum required version of PHP.
     6991 *     @type string $UpdateURI       ID of the plugin for update purposes, should be a URI.
     6992 *     @type string $RequiresPlugins Comma separated list of dot org plugin slugs.
     6993 *     @type string $Title           Title of the plugin and link to the plugin's site (if set).
     6994 *     @type string $AuthorName      Plugin author's name.
     6995 * }
     6996 */
     6997function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {
     6998
     6999    $default_headers = array(
     7000        'Name'            => 'Plugin Name',
     7001        'PluginURI'       => 'Plugin URI',
     7002        'Version'         => 'Version',
     7003        'Description'     => 'Description',
     7004        'Author'          => 'Author',
     7005        'AuthorURI'       => 'Author URI',
     7006        'TextDomain'      => 'Text Domain',
     7007        'DomainPath'      => 'Domain Path',
     7008        'Network'         => 'Network',
     7009        'RequiresWP'      => 'Requires at least',
     7010        'RequiresPHP'     => 'Requires PHP',
     7011        'UpdateURI'       => 'Update URI',
     7012        'RequiresPlugins' => 'Requires Plugins',
     7013        // Site Wide Only is deprecated in favor of Network.
     7014        '_sitewide'       => 'Site Wide Only',
     7015    );
     7016
     7017    $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );
     7018
     7019    // Site Wide Only is the old header for Network.
     7020    if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {
     7021        /* translators: 1: Site Wide Only: true, 2: Network: true */
     7022        _deprecated_argument( __FUNCTION__, '3.0.0', sprintf( __( 'The %1$s plugin header is deprecated. Use %2$s instead.' ), '<code>Site Wide Only: true</code>', '<code>Network: true</code>' ) );
     7023        $plugin_data['Network'] = $plugin_data['_sitewide'];
     7024    }
     7025    $plugin_data['Network'] = ( 'true' === strtolower( $plugin_data['Network'] ) );
     7026    unset( $plugin_data['_sitewide'] );
     7027
     7028    // If no text domain is defined fall back to the plugin slug.
     7029    if ( ! $plugin_data['TextDomain'] ) {
     7030        $plugin_slug = dirname( plugin_basename( $plugin_file ) );
     7031        if ( '.' !== $plugin_slug && ! str_contains( $plugin_slug, '/' ) ) {
     7032            $plugin_data['TextDomain'] = $plugin_slug;
     7033        }
     7034    }
     7035
     7036    if ( $markup || $translate ) {
     7037        $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );
     7038    } else {
     7039        $plugin_data['Title']      = $plugin_data['Name'];
     7040        $plugin_data['AuthorName'] = $plugin_data['Author'];
     7041    }
     7042
     7043    return $plugin_data;
     7044}
     7045
     7046/**
     7047 * Sanitizes plugin data, optionally adds markup, optionally translates.
     7048 *
     7049 * @since 2.7.0
     7050 *
     7051 * @see get_plugin_data()
     7052 *
     7053 * @access private
     7054 *
     7055 * @param string $plugin_file Path to the main plugin file.
     7056 * @param array  $plugin_data An array of plugin data. See get_plugin_data().
     7057 * @param bool   $markup      Optional. If the returned data should have HTML markup applied.
     7058 *                            Default true.
     7059 * @param bool   $translate   Optional. If the returned data should be translated. Default true.
     7060 * @return array Plugin data. Values will be empty if not supplied by the plugin.
     7061 *               See get_plugin_data() for the list of possible values.
     7062 */
     7063function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {
     7064
     7065    // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path.
     7066    $plugin_file = plugin_basename( $plugin_file );
     7067
     7068    // Translate fields.
     7069    if ( $translate ) {
     7070        $textdomain = $plugin_data['TextDomain'];
     7071        if ( $textdomain ) {
     7072            if ( ! is_textdomain_loaded( $textdomain ) ) {
     7073                if ( $plugin_data['DomainPath'] ) {
     7074                    load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );
     7075                } else {
     7076                    load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );
     7077                }
     7078            }
     7079        } elseif ( 'hello.php' === basename( $plugin_file ) ) {
     7080            $textdomain = 'default';
     7081        }
     7082        if ( $textdomain ) {
     7083            foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) {
     7084                if ( ! empty( $plugin_data[ $field ] ) ) {
     7085                    // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain
     7086                    $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );
     7087                }
     7088            }
     7089        }
     7090    }
     7091
     7092    // Sanitize fields.
     7093    $allowed_tags_in_links = array(
     7094        'abbr'    => array( 'title' => true ),
     7095        'acronym' => array( 'title' => true ),
     7096        'code'    => true,
     7097        'em'      => true,
     7098        'strong'  => true,
     7099    );
     7100
     7101    $allowed_tags      = $allowed_tags_in_links;
     7102    $allowed_tags['a'] = array(
     7103        'href'  => true,
     7104        'title' => true,
     7105    );
     7106
     7107    /*
     7108     * Name is marked up inside <a> tags. Don't allow these.
     7109     * Author is too, but some plugins have used <a> here (omitting Author URI).
     7110     */
     7111    $plugin_data['Name']   = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );
     7112    $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );
     7113
     7114    $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );
     7115    $plugin_data['Version']     = wp_kses( $plugin_data['Version'], $allowed_tags );
     7116
     7117    $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );
     7118    $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );
     7119
     7120    $plugin_data['Title']      = $plugin_data['Name'];
     7121    $plugin_data['AuthorName'] = $plugin_data['Author'];
     7122
     7123    // Apply markup.
     7124    if ( $markup ) {
     7125        if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) {
     7126            $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';
     7127        }
     7128
     7129        if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) {
     7130            $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';
     7131        }
     7132
     7133        $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );
     7134
     7135        if ( $plugin_data['Author'] ) {
     7136            $plugin_data['Description'] .= sprintf(
     7137            /* translators: %s: Plugin author. */
     7138                ' <cite>' . __( 'By %s.' ) . '</cite>',
     7139                $plugin_data['Author']
     7140            );
     7141        }
     7142    }
     7143
     7144    return $plugin_data;
     7145}
     7146
     7147/**
    69337148 * Returns true.
    69347149 *
Note: See TracChangeset for help on using the changeset viewer.