WP_Plugin_Dependencies::check_plugin_dependencies_during_ajax()

In this article

Checks plugin dependencies after a plugin is installed via AJAX.

Source

public static function check_plugin_dependencies_during_ajax() {
	check_ajax_referer( 'updates' );

	if ( empty( $_POST['slug'] ) ) {
		wp_send_json_error(
			array(
				'slug'         => '',
				'pluginName'   => '',
				'errorCode'    => 'no_plugin_specified',
				'errorMessage' => __( 'No plugin specified.' ),
			)
		);
	}

	$slug   = sanitize_key( wp_unslash( $_POST['slug'] ) );
	$status = array( 'slug' => $slug );

	self::get_plugins();
	self::get_plugin_dirnames();

	if ( ! isset( self::$plugin_dirnames[ $slug ] ) ) {
		$status['errorCode']    = 'plugin_not_installed';
		$status['errorMessage'] = __( 'The plugin is not installed.' );
		wp_send_json_error( $status );
	}

	$plugin_file          = self::$plugin_dirnames[ $slug ];
	$status['pluginName'] = self::$plugins[ $plugin_file ]['Name'];
	$status['plugin']     = $plugin_file;

	if ( current_user_can( 'activate_plugin', $plugin_file ) && is_plugin_inactive( $plugin_file ) ) {
		$status['activateUrl'] = add_query_arg(
			array(
				'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $plugin_file ),
				'action'   => 'activate',
				'plugin'   => $plugin_file,
			),
			is_multisite() ? network_admin_url( 'plugins.php' ) : admin_url( 'plugins.php' )
		);
	}

	if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
		$status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] );
	}

	self::initialize();
	$dependencies = self::get_dependencies( $plugin_file );
	if ( empty( $dependencies ) ) {
		$status['message'] = __( 'The plugin has no required plugins.' );
		wp_send_json_success( $status );
	}

	require_once ABSPATH . '/wp-admin/includes/plugin.php';

	$inactive_dependencies = array();
	foreach ( $dependencies as $dependency ) {
		if ( false === self::$plugin_dirnames[ $dependency ] || is_plugin_inactive( self::$plugin_dirnames[ $dependency ] ) ) {
			$inactive_dependencies[] = $dependency;
		}
	}

	if ( ! empty( $inactive_dependencies ) ) {
		$inactive_dependency_names = array_map(
			function ( $dependency ) {
				if ( isset( self::$dependency_api_data[ $dependency ]['Name'] ) ) {
					$inactive_dependency_name = self::$dependency_api_data[ $dependency ]['Name'];
				} else {
					$inactive_dependency_name = $dependency;
				}
				return $inactive_dependency_name;
			},
			$inactive_dependencies
		);

		$status['errorCode']    = 'inactive_dependencies';
		$status['errorMessage'] = sprintf(
			/* translators: %s: A list of inactive dependency plugin names. */
			__( 'The following plugins must be activated first: %s.' ),
			implode( ', ', $inactive_dependency_names )
		);
		$status['errorData'] = array_combine( $inactive_dependencies, $inactive_dependency_names );

		wp_send_json_error( $status );
	}

	$status['message'] = __( 'All required plugins are installed and activated.' );
	wp_send_json_success( $status );
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.