activate_plugin()

最后更新于:2021-11-25 19:07:07

activate_plugin( string$plugin, string$redirect=”, bool$network_wide=false, bool$silent=false)

Attempts activation of plugin in a “sandbox” and redirects on success.

参数

$plugin

(string) (Required) Path to the plugin file relative to the plugins directory.

$redirect

(string) (Optional) URL to redirect to.

Default value: ”

$network_wide

(bool) (Optional) Whether to enable the plugin for all sites in the network or just the current site. Multisite only.

Default value: false

$silent

(bool) (Optional) Whether to prevent calling activation hooks.

Default value: false

响应

(null|GC_Error) Null on success, GC_Error on invalid file.

$cache_plugins = gc_cache_get( 'plugins', 'plugins' );
if ( !empty( $cache_plugins ) ) {
	$new_plugin = array(
		'Name' => $plugin_name,
		'PluginURI' => $plugin_uri,
		'Version' => $plugin_version,
		'Description' => $plugin_description,
		'Author' => $author_name,
		'AuthorURI' => $author_uri,
		'TextDomain' => '',
		'DomainPath' => '',
		'Network' => '',
		'Title' => $plugin_name,
		'AuthorName' => $author_name,
	);
	$cache_plugins[''][$plugin_path] = $new_plugin;
	gc_cache_set( 'plugins', $cache_plugins, 'plugins' );
}

源文件

文件: gc-admin/includes/plugin.php

function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
	$plugin = plugin_basename( trim( $plugin ) );

	if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) {
		$network_wide        = true;
		$current             = get_site_option( 'active_sitewide_plugins', array() );
		$_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
	} else {
		$current = get_option( 'active_plugins', array() );
	}

	$valid = validate_plugin( $plugin );
	if ( is_gc_error( $valid ) ) {
		return $valid;
	}

	$requirements = validate_plugin_requirements( $plugin );
	if ( is_gc_error( $requirements ) ) {
		return $requirements;
	}

	if ( $network_wide && ! isset( $current[ $plugin ] )
		|| ! $network_wide && ! in_array( $plugin, $current, true )
	) {
		if ( ! empty( $redirect ) ) {
			// We'll override this later if the plugin can be included without fatal error.
			gc_redirect( add_query_arg( '_error_nonce', gc_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) );
		}

		ob_start();

		// Load the plugin to test whether it throws any errors.
		plugin_sandbox_scrape( $plugin );

		if ( ! $silent ) {
			/**
			 * Fires before a plugin is activated.
			 *
			 * If a plugin is silently activated (such as during an update),
			 * this hook does not fire.
			 *
			 * @since 2.9.0
			 *
			 * @param string $plugin       Path to the plugin file relative to the plugins directory.
			 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
			 *                             or just the current site. Multisite only. Default false.
			 */
			do_action( 'activate_plugin', $plugin, $network_wide );

			/**
			 * Fires as a specific plugin is being activated.
			 *
			 * This hook is the "activation" hook used internally by register_activation_hook().
			 * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
			 *
			 * If a plugin is silently activated (such as during an update), this hook does not fire.
			 *
			 * @since 2.0.0
			 *
			 * @param bool $network_wide Whether to enable the plugin for all sites in the network
			 *                           or just the current site. Multisite only. Default false.
			 */
			do_action( "activate_{$plugin}", $network_wide );
		}

		if ( $network_wide ) {
			$current            = get_site_option( 'active_sitewide_plugins', array() );
			$current[ $plugin ] = time();
			update_site_option( 'active_sitewide_plugins', $current );
		} else {
			$current   = get_option( 'active_plugins', array() );
			$current[] = $plugin;
			sort( $current );
			update_option( 'active_plugins', $current );
		}

		if ( ! $silent ) {
			/**
			 * Fires after a plugin has been activated.
			 *
			 * If a plugin is silently activated (such as during an update),
			 * this hook does not fire.
			 *
			 * @since 2.9.0
			 *
			 * @param string $plugin       Path to the plugin file relative to the plugins directory.
			 * @param bool   $network_wide Whether to enable the plugin for all sites in the network
			 *                             or just the current site. Multisite only. Default false.
			 */
			do_action( 'activated_plugin', $plugin, $network_wide );
		}

		if ( ob_get_length() > 0 ) {
			$output = ob_get_clean();
			return new GC_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
		}

		ob_end_clean();
	}

	return null;
}