Example #1
0
/**
 * Initiate plugin.
 *
 * @return void
 */
function no_comment()
{
    /**
     * Load translations.
     */
    load_plugin_textdomain('no-comment', false, trailingslashit(dirname(plugin_basename(__FILE__))) . 'l10n/');
    /**
     * Plugin status.
     */
    $status = no_comment__status();
    // Activation has run before, stop here and keep the plugin going.
    if ('activated' === $status) {
        include_once 'inc/run.php';
        return;
    }
    /**
     * Plugin Activation:
     *
     * When first activated, the plugin doesn’t do anything.
     * Instead it will display an admin notice informing the user
     * what operations it is about to perform and prompt the user to
     * either confirm or abort.
     *
     * In case the user confirms, the plugin will run once, including the
     * performance of one-time operations (sending previously published comments
     * to moderation, setting comment and ping status on all posts to closed).
     * After its first loop, the plugin will display a success notice once and
     * then keep running (while not performing its one-time operations again).
     *
     * In case the user aborts, the plugin will display a notice that no operations
     * have been performed and the user can deactivate the plugin.
     *
     * Once aborted, the plugin needs to be deactivated and re-activated in order to show
     * the activation dialogue again.
     */
    include_once 'inc/activation.php';
    // Activation has not run yet, maybe run it now.
    if ('aborted' === $status) {
        $maybe_run = 'abort';
    } else {
        $maybe_run = isset($_GET['no-comment']) ? $_GET['no-comment'] : '';
    }
    // Activation dialogue.
    switch ($maybe_run) {
        // Run plugin and display message once.
        case 'run':
            include_once 'inc/run.php';
            add_action('admin_notices', function () {
                check_admin_referer('no_comment_activating');
                $message = no_comment__activation_success_message();
                no_comment__admin_notice($message, 'notice-success is-dismissible');
            });
            update_option('no_comment__status', 'activated');
            break;
            // Abort and display message once.
        // Abort and display message once.
        case 'abort':
            add_action('admin_notices', function () {
                check_admin_referer('no_comment_aborting');
                $message = no_comment__activation_abort_message();
                no_comment__admin_notice($message, 'notice-warning is-dismissible');
            });
            // No need to update with the same value.
            if ('aborted' !== $status) {
                update_option('no_comment__status', 'aborted');
            }
            break;
            // Display post-activation prompt message (run|abort).
        // Display post-activation prompt message (run|abort).
        default:
            if (!$status) {
                add_action('admin_notices', function () {
                    $message = no_comment__get_activation_message();
                    no_comment__admin_notice($message, 'notice-info');
                });
            }
            break;
    }
}
Example #2
0
    // Remove admin menu pages.
    remove_menu_page('edit-comments.php');
    remove_submenu_page('options-general.php', 'options-discussion.php');
    /**
     * Users can still access these pages directly via URL, so we should
     * remind them about the plugin in case they find their way here.
     */
    add_action('admin_notices', function () {
        $forbidden = array('edit-comments.php', 'options-discussion.php');
        if (!in_array($GLOBALS['pagenow'], $forbidden)) {
            return;
        }
        $message = '<p>';
        $message .= sprintf(__('This page is currently hidden from the admin menu, although you can access it directly. Anything you edit here may not work as expected.<br />If you want to use comments or pings on your site, <a href="%s">deactivate the <strong>No Comment</strong> plugin first</a>.', 'no-comment'), esc_url(add_query_arg('plugin_status', 'active', admin_url('plugins.php'))));
        $message .= '</p>';
        no_comment__admin_notice($message, 'notice-warning is-dismissible');
    });
}, no_comment__ego());
/**
 * Remove comment menu item from admin bar.
 */
add_action('wp_before_admin_bar_render', function () {
    $GLOBALS['wp_admin_bar']->remove_menu('comments');
}, no_comment__ego());
/**
 * Removes post type support for comments from all registered post types.
 * Will also remove Allow Comments/Pings checkboxes from QuickEdit.
 */
add_action('init', function () {
    $post_types = get_post_types();
    $comment_types = array('comments', 'trackbacks');