/**
 * Attempts activation of plugin in a "sandbox" and redirects on success.
 *
 * A plugin that is already activated will not attempt to be activated again.
 *
 * The way it works is by setting the redirection to the error before trying to
 * include the plugin file. If the plugin fails, then the redirection will not
 * be overwritten with the success message. Also, the options will not be
 * updated and the activation hook will not be called on plugin error.
 *
 * It should be noted that in no way the below code will actually prevent errors
 * within the file. The code should not be used elsewhere to replicate the
 * "sandbox", which uses redirection to work.
 *
 * If any errors are found or text is outputted, then it will be captured to
 * ensure that the success redirection will update the error redirection.
 *
 * @since 1.0
 *
 * @param string $plugin Plugin path to main plugin file with plugin data.
 * @param string $redirect Optional. URL to redirect to.
 * @return nxt_Error|null nxt_Error on invalid file or null on success.
 */
function bb_activate_plugin($plugin, $redirect = '')
{
    $active_plugins = (array) bb_get_option('active_plugins');
    $plugin = bb_plugin_basename(trim($plugin));
    $valid_path = bb_validate_plugin($plugin);
    if (is_nxt_error($valid_path)) {
        return $valid_path;
    }
    if (in_array($plugin, $active_plugins)) {
        return false;
    }
    if (!empty($redirect)) {
        // We'll override this later if the plugin can be included without fatal error
        nxt_redirect(add_query_arg('_scrape_nonce', bb_create_nonce('scrape-plugin_' . $plugin), $redirect));
    }
    ob_start();
    @(include $valid_path);
    // Add to the active plugins array
    $active_plugins[] = $plugin;
    ksort($active_plugins);
    bb_update_option('active_plugins', $active_plugins);
    do_action('bb_activate_plugin_' . $plugin);
    ob_end_clean();
    return $valid_path;
}
Example #2
0
            }
            // Overrides the ?message=error one above
            wp_redirect('plugins.php?plugin_request=' . $plugin_request . '&message=activate&plugin=' . urlencode($plugin));
            break;
        case 'deactivate':
            // Deactivation
            bb_check_admin_referer('deactivate-plugin_' . $plugin);
            // Remove the deactivated plugin
            bb_deactivate_plugins($plugin);
            // Redirect
            wp_redirect('plugins.php?plugin_request=' . $plugin_request . '&message=deactivate&plugin=' . urlencode($plugin));
            break;
        case 'scrape':
            // Scrape php errors from the plugin
            bb_check_admin_referer('scrape-plugin_' . $plugin);
            $valid_path = bb_validate_plugin($plugin);
            if (is_wp_error($valid_path)) {
                bb_die($valid_path);
            }
            // Pump up the errors and output them to screen
            error_reporting(E_ALL ^ E_NOTICE);
            @ini_set('display_errors', true);
            include $valid_path;
            break;
    }
    // Stop processing
    exit;
}
// Display notices
if (isset($_GET['message'])) {
    switch ($_GET['message']) {