Changeset 59488 for trunk/src/wp-includes/functions.php
- Timestamp:
- 12/05/2024 12:11:27 PM (18 months ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/functions.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r59479 r59488 6932 6932 6933 6933 /** 6934 * Parses the plugin contents to retrieve plugin's metadata.6935 *6936 * All plugin headers must be on their own line. Plugin description must not have6937 * any newlines, otherwise only parts of the description will be displayed.6938 * The below is formatted for printing.6939 *6940 * /*6941 * Plugin Name: Name of the plugin.6942 * Plugin URI: The home page of the plugin.6943 * Description: Plugin description.6944 * Author: Plugin author's name.6945 * Author URI: Link to the author's website.6946 * Version: Plugin version.6947 * Text Domain: Optional. Unique identifier, should be same as the one used in6948 * load_plugin_textdomain().6949 * Domain Path: Optional. Only useful if the translations are located in a6950 * folder above the plugin's base path. For example, if .mo files are6951 * located in the locale folder then Domain Path will be "/locale/" and6952 * must have the first slash. Defaults to the base folder the plugin is6953 * located in.6954 * Network: Optional. Specify "Network: true" to require that a plugin is activated6955 * across all sites in an installation. This will prevent a plugin from being6956 * activated on a single site when Multisite is enabled.6957 * Requires at least: Optional. Specify the minimum required WordPress version.6958 * Requires PHP: Optional. Specify the minimum required PHP version.6959 * * / # Remove the space to close comment.6960 *6961 * The first 8 KB of the file will be pulled in and if the plugin data is not6962 * within that first 8 KB, then the plugin author should correct their plugin6963 * and move the plugin data headers to the top.6964 *6965 * The plugin file is assumed to have permissions to allow for scripts to read6966 * the file. This is not checked however and the file is only opened for6967 * reading.6968 *6969 * @since 1.5.06970 * @since 5.3.0 Added support for `Requires at least` and `Requires PHP` headers.6971 * @since 5.8.0 Added support for `Update URI` header.6972 * @since 6.5.0 Added support for `Requires Plugins` header.6973 *6974 * @param string $plugin_file Absolute path to the main plugin file.6975 * @param bool $markup Optional. If the returned data should have HTML markup applied.6976 * Default true.6977 * @param bool $translate Optional. If the returned data should be translated. Default true.6978 * @return array {6979 * Plugin data. Values will be empty if not supplied by the plugin.6980 *6981 * @type string $Name Name of the plugin. Should be unique.6982 * @type string $PluginURI Plugin URI.6983 * @type string $Version Plugin version.6984 * @type string $Description Plugin description.6985 * @type string $Author Plugin author's name.6986 * @type string $AuthorURI Plugin author's website address (if set).6987 * @type string $TextDomain Plugin textdomain.6988 * @type string $DomainPath Plugin's relative directory path to .mo files.6989 * @type bool $Network Whether the plugin can only be activated network-wide.6990 * @type string $RequiresWP Minimum required version of WordPress.6991 * @type string $RequiresPHP Minimum required version of PHP.6992 * @type string $UpdateURI ID of the plugin for update purposes, should be a URI.6993 * @type string $RequiresPlugins Comma separated list of dot org plugin slugs.6994 * @type string $Title Title of the plugin and link to the plugin's site (if set).6995 * @type string $AuthorName Plugin author's name.6996 * }6997 */6998 function get_plugin_data( $plugin_file, $markup = true, $translate = true ) {6999 7000 $default_headers = array(7001 'Name' => 'Plugin Name',7002 'PluginURI' => 'Plugin URI',7003 'Version' => 'Version',7004 'Description' => 'Description',7005 'Author' => 'Author',7006 'AuthorURI' => 'Author URI',7007 'TextDomain' => 'Text Domain',7008 'DomainPath' => 'Domain Path',7009 'Network' => 'Network',7010 'RequiresWP' => 'Requires at least',7011 'RequiresPHP' => 'Requires PHP',7012 'UpdateURI' => 'Update URI',7013 'RequiresPlugins' => 'Requires Plugins',7014 // Site Wide Only is deprecated in favor of Network.7015 '_sitewide' => 'Site Wide Only',7016 );7017 7018 $plugin_data = get_file_data( $plugin_file, $default_headers, 'plugin' );7019 7020 // Site Wide Only is the old header for Network.7021 if ( ! $plugin_data['Network'] && $plugin_data['_sitewide'] ) {7022 /* translators: 1: Site Wide Only: true, 2: Network: true */7023 _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>' ) );7024 $plugin_data['Network'] = $plugin_data['_sitewide'];7025 }7026 $plugin_data['Network'] = ( 'true' === strtolower( $plugin_data['Network'] ) );7027 unset( $plugin_data['_sitewide'] );7028 7029 // If no text domain is defined fall back to the plugin slug.7030 if ( ! $plugin_data['TextDomain'] ) {7031 $plugin_slug = dirname( plugin_basename( $plugin_file ) );7032 if ( '.' !== $plugin_slug && ! str_contains( $plugin_slug, '/' ) ) {7033 $plugin_data['TextDomain'] = $plugin_slug;7034 }7035 }7036 7037 if ( $markup || $translate ) {7038 $plugin_data = _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup, $translate );7039 } else {7040 $plugin_data['Title'] = $plugin_data['Name'];7041 $plugin_data['AuthorName'] = $plugin_data['Author'];7042 }7043 7044 return $plugin_data;7045 }7046 7047 /**7048 * Sanitizes plugin data, optionally adds markup, optionally translates.7049 *7050 * @since 2.7.07051 *7052 * @see get_plugin_data()7053 *7054 * @access private7055 *7056 * @param string $plugin_file Path to the main plugin file.7057 * @param array $plugin_data An array of plugin data. See get_plugin_data().7058 * @param bool $markup Optional. If the returned data should have HTML markup applied.7059 * Default true.7060 * @param bool $translate Optional. If the returned data should be translated. Default true.7061 * @return array Plugin data. Values will be empty if not supplied by the plugin.7062 * See get_plugin_data() for the list of possible values.7063 */7064 function _get_plugin_data_markup_translate( $plugin_file, $plugin_data, $markup = true, $translate = true ) {7065 7066 // Sanitize the plugin filename to a WP_PLUGIN_DIR relative path.7067 $plugin_file = plugin_basename( $plugin_file );7068 7069 // Translate fields.7070 if ( $translate ) {7071 $textdomain = $plugin_data['TextDomain'];7072 if ( $textdomain ) {7073 if ( ! is_textdomain_loaded( $textdomain ) ) {7074 if ( $plugin_data['DomainPath'] ) {7075 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) . $plugin_data['DomainPath'] );7076 } else {7077 load_plugin_textdomain( $textdomain, false, dirname( $plugin_file ) );7078 }7079 }7080 } elseif ( 'hello.php' === basename( $plugin_file ) ) {7081 $textdomain = 'default';7082 }7083 if ( $textdomain ) {7084 foreach ( array( 'Name', 'PluginURI', 'Description', 'Author', 'AuthorURI', 'Version' ) as $field ) {7085 if ( ! empty( $plugin_data[ $field ] ) ) {7086 // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText,WordPress.WP.I18n.NonSingularStringLiteralDomain7087 $plugin_data[ $field ] = translate( $plugin_data[ $field ], $textdomain );7088 }7089 }7090 }7091 }7092 7093 // Sanitize fields.7094 $allowed_tags_in_links = array(7095 'abbr' => array( 'title' => true ),7096 'acronym' => array( 'title' => true ),7097 'code' => true,7098 'em' => true,7099 'strong' => true,7100 );7101 7102 $allowed_tags = $allowed_tags_in_links;7103 $allowed_tags['a'] = array(7104 'href' => true,7105 'title' => true,7106 );7107 7108 /*7109 * Name is marked up inside <a> tags. Don't allow these.7110 * Author is too, but some plugins have used <a> here (omitting Author URI).7111 */7112 $plugin_data['Name'] = wp_kses( $plugin_data['Name'], $allowed_tags_in_links );7113 $plugin_data['Author'] = wp_kses( $plugin_data['Author'], $allowed_tags );7114 7115 $plugin_data['Description'] = wp_kses( $plugin_data['Description'], $allowed_tags );7116 $plugin_data['Version'] = wp_kses( $plugin_data['Version'], $allowed_tags );7117 7118 $plugin_data['PluginURI'] = esc_url( $plugin_data['PluginURI'] );7119 $plugin_data['AuthorURI'] = esc_url( $plugin_data['AuthorURI'] );7120 7121 $plugin_data['Title'] = $plugin_data['Name'];7122 $plugin_data['AuthorName'] = $plugin_data['Author'];7123 7124 // Apply markup.7125 if ( $markup ) {7126 if ( $plugin_data['PluginURI'] && $plugin_data['Name'] ) {7127 $plugin_data['Title'] = '<a href="' . $plugin_data['PluginURI'] . '">' . $plugin_data['Name'] . '</a>';7128 }7129 7130 if ( $plugin_data['AuthorURI'] && $plugin_data['Author'] ) {7131 $plugin_data['Author'] = '<a href="' . $plugin_data['AuthorURI'] . '">' . $plugin_data['Author'] . '</a>';7132 }7133 7134 $plugin_data['Description'] = wptexturize( $plugin_data['Description'] );7135 7136 if ( $plugin_data['Author'] ) {7137 $plugin_data['Description'] .= sprintf(7138 /* translators: %s: Plugin author. */7139 ' <cite>' . __( 'By %s.' ) . '</cite>',7140 $plugin_data['Author']7141 );7142 }7143 }7144 7145 return $plugin_data;7146 }7147 7148 /**7149 * Determines whether a plugin is active.7150 *7151 * Only plugins installed in the plugins/ folder can be active.7152 *7153 * Plugins in the mu-plugins/ folder can't be "activated," so this function will7154 * return false for those plugins.7155 *7156 * For more information on this and similar theme functions, check out7157 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/7158 * Conditional Tags} article in the Theme Developer Handbook.7159 *7160 * @since 2.5.07161 *7162 * @param string $plugin Path to the plugin file relative to the plugins directory.7163 * @return bool True, if in the active plugins list. False, not in the list.7164 */7165 function is_plugin_active( $plugin ) {7166 return in_array( $plugin, (array) get_option( 'active_plugins', array() ), true ) || is_plugin_active_for_network( $plugin );7167 }7168 7169 /**7170 * Determines whether the plugin is inactive.7171 *7172 * Reverse of is_plugin_active(). Used as a callback.7173 *7174 * For more information on this and similar theme functions, check out7175 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/7176 * Conditional Tags} article in the Theme Developer Handbook.7177 *7178 * @since 3.1.07179 *7180 * @see is_plugin_active()7181 *7182 * @param string $plugin Path to the plugin file relative to the plugins directory.7183 * @return bool True if inactive. False if active.7184 */7185 function is_plugin_inactive( $plugin ) {7186 return ! is_plugin_active( $plugin );7187 }7188 7189 /**7190 * Determines whether the plugin is active for the entire network.7191 *7192 * Only plugins installed in the plugins/ folder can be active.7193 *7194 * Plugins in the mu-plugins/ folder can't be "activated," so this function will7195 * return false for those plugins.7196 *7197 * For more information on this and similar theme functions, check out7198 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/7199 * Conditional Tags} article in the Theme Developer Handbook.7200 *7201 * @since 3.0.07202 *7203 * @param string $plugin Path to the plugin file relative to the plugins directory.7204 * @return bool True if active for the network, otherwise false.7205 */7206 function is_plugin_active_for_network( $plugin ) {7207 if ( ! is_multisite() ) {7208 return false;7209 }7210 7211 $plugins = get_site_option( 'active_sitewide_plugins' );7212 if ( isset( $plugins[ $plugin ] ) ) {7213 return true;7214 }7215 7216 return false;7217 }7218 7219 /**7220 * Checks for "Network: true" in the plugin header to see if this should7221 * be activated only as a network wide plugin. The plugin would also work7222 * when Multisite is not enabled.7223 *7224 * Checks for "Site Wide Only: true" for backward compatibility.7225 *7226 * @since 3.0.07227 *7228 * @param string $plugin Path to the plugin file relative to the plugins directory.7229 * @return bool True if plugin is network only, false otherwise.7230 */7231 function is_network_only_plugin( $plugin ) {7232 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );7233 if ( $plugin_data ) {7234 return $plugin_data['Network'];7235 }7236 return false;7237 }7238 7239 /**7240 6934 * Returns true. 7241 6935 *
Note: See TracChangeset
for help on using the changeset viewer.