Skip to content

Instantly share code, notes, and snippets.

@whyisjake
Created October 30, 2017 21:38
Show Gist options
  • Save whyisjake/d8088a945170b523d5d13f5edf814eea to your computer and use it in GitHub Desktop.
Save whyisjake/d8088a945170b523d5d13f5edf814eea to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Comment Moderator
* Plugin URI: https://99robots.com/products/
* Description: Add a new user role, Comment Moderator, that allows a new user to only manage comments.
* Version: 1.3.2
* Author: 99 Robots
* Author URI: https://99robots.com
* License: GPL2
* Text Domain: wpsite-comment-moderator
*/
/**
* Global Definitions
*/
// Plugin Name
if ( ! defined( 'WPSITE_COMMENT_MODERATOR_PLUGIN_NAME' ) ) {
define( 'WPSITE_COMMENT_MODERATOR_PLUGIN_NAME', trim( dirname( plugin_basename( __FILE__ ) ), '/' ) );
}
// Plugin directory
if ( ! defined( 'WPSITE_COMMENT_MODERATOR_PLUGIN_DIR' ) ) {
define( 'WPSITE_COMMENT_MODERATOR_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . WPSITE_COMMENT_MODERATOR_PLUGIN_NAME );
}
// Plugin url
if ( ! defined( 'WPSITE_COMMENT_MODERATOR_PLUGIN_URL' ) ) {
define( 'WPSITE_COMMENT_MODERATOR_PLUGIN_URL', WP_PLUGIN_URL . '/' . WPSITE_COMMENT_MODERATOR_PLUGIN_NAME );
}
// Plugin verison
if ( ! defined( 'WPSITE_COMMENT_MODERATOR_VERSION_NUM' ) ) {
define( 'WPSITE_COMMENT_MODERATOR_VERSION_NUM', '1.3.2' );
}
/**
* Activatation / Deactivation
*/
register_activation_hook( __FILE__, array( 'WPsiteCommentModerator', 'register_activation' ) );
/**
* WPsiteCommentModerator
*
* @since 1.0.0
*/
class WPsiteCommentModerator {
/**
* The Constructor
*
* @since 1.3.2
*/
public function __construct() {
add_action( 'init', array( $this, 'load_textdomain' ) );
add_action( 'admin_menu', array( $this, 'remove_menu' ) );
// each time we update roles, replace this conditional to check for the latest role or capability. Conditional makes sure it only runs once.
$comment_moderator = get_role( 'comment_moderator' );
if ( $comment_moderator->has_cap( 'edit_others_posts' ) ) {
add_action( 'init', array( $this, 'initial_role_handler' ), 10 );
}
}
/**
* Load the text domain
*
* @since 1.0.0
*/
public function load_textdomain() {
load_plugin_textdomain( 'wpsite-comment-moderator', false, WPSITE_COMMENT_MODERATOR_PLUGIN_DIR . '/languages' );
}
/**
* Remove menu according to role
*/
public function remove_menu() {
$user = wp_get_current_user();
if ( ! empty( $user ) && in_array( 'comment_moderator', (array) $user->roles ) ) {
remove_menu_page( 'edit.php' );
remove_menu_page( 'tools.php' );
$post_types = get_post_types( '', 'names' );
// This doesn't cut the snuff...
foreach ( $post_types as $post_type ) {
remove_menu_page( 'edit.php?post_type=' . $post_type );
}
}
}
/**
* Hooks to 'register_activation_hook'
*
* @since 1.0.0
*/
static function register_activation() {
// Check if multisite, if so then save as site option
if ( is_multisite() ) {
add_site_option( 'wpsite_comment_moderator_version', WPSITE_COMMENT_MODERATOR_VERSION_NUM );
} else {
add_option( 'wpsite_comment_moderator_version', WPSITE_COMMENT_MODERATOR_VERSION_NUM );
}
remove_role( 'comment_moderator' );
add_role(
'comment_moderator',
esc_html__( 'Comment Moderator', 'wpsite-comment-moderator' ),
array(
'read' => true,
'moderate_comments' => true,
'edit_comment' => true,
'edit_others_posts' => false,
'edit_published_posts' => false,
'edit_posts' => false,
'edit_others_pages' => false,
'edit_published_pages' => false,
'edit_pages' => false,
)
);
}
/**
* Redefine the moderator role
*/
public function initial_role_handler() {
$moderator = get_role( 'comment_moderator' );
$moderator->remove_cap( 'edit_others_pages' );
$moderator->remove_cap( 'edit_others_posts' );
$moderator->remove_cap( 'edit_posts' );
$moderator->remove_cap( 'edit_pages' );
$moderator->remove_cap( 'edit_published_pages' );
$moderator->remove_cap( 'edit_published_posts' );
}
}
/**
* Start the plugin
*/
new WPsiteCommentModerator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment