Make WordPress Core


Ignore:
Timestamp:
12/02/2024 05:08:19 PM (7 months ago)
Author:
swissspidy
Message:

Plugins: Make more plugin-related functions available early on.

This is a follow-up to [59461], which moved get_plugin_data() from wp-admin/includes/plugin.php to wp-includes/functions.php so it's available during the plugin loading process.

Related functions like is_plugin_active() are often used together and should therefore be moved as well, to improve backward compatibility for plugins which load wp-admin/includes/plugin.php only conditionally.

Props johnbillion, dd32, swissspidy.
See #62244.

File:
1 edited

Legend:

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

    r59471 r59479  
    71477147
    71487148/**
     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 will
     7154 * return false for those plugins.
     7155 *
     7156 * For more information on this and similar theme functions, check out
     7157 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
     7158 * Conditional Tags} article in the Theme Developer Handbook.
     7159 *
     7160 * @since 2.5.0
     7161 *
     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 */
     7165function 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 out
     7175 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
     7176 * Conditional Tags} article in the Theme Developer Handbook.
     7177 *
     7178 * @since 3.1.0
     7179 *
     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 */
     7185function 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 will
     7195 * return false for those plugins.
     7196 *
     7197 * For more information on this and similar theme functions, check out
     7198 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
     7199 * Conditional Tags} article in the Theme Developer Handbook.
     7200 *
     7201 * @since 3.0.0
     7202 *
     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 */
     7206function 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 should
     7221 * be activated only as a network wide plugin. The plugin would also work
     7222 * when Multisite is not enabled.
     7223 *
     7224 * Checks for "Site Wide Only: true" for backward compatibility.
     7225 *
     7226 * @since 3.0.0
     7227 *
     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 */
     7231function 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/**
    71497240 * Returns true.
    71507241 *
Note: See TracChangeset for help on using the changeset viewer.