-
-
Save 2ndkauboy/8d6eb6279e6ff395031c2ce77ded9f6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Subscriber Edit Comments | |
* | |
* @package SubscribersEditComments | |
* @author Bernhard Kau | |
* @license GPLv3 | |
* | |
* @wordpress-plugin | |
* Plugin Name: Subscriber Edit Comments | |
* Description: This plugin allow users with the role "subscriber" to edit their own comments | |
* Version: 1.0.0 | |
* Author: Bernhard Kau | |
* Author URI: http://kau-boys.de | |
* Plugin URI: https://gist.github.com/2ndkauboy/8d6eb6279e6ff395031c2ce77ded9f6f | |
* License: GPLv3 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0 | |
*/ | |
add_action( 'plugins_loaded', array( Subscriber_Edit_Comments::get_instance(), 'plugin_setup' ) ); | |
/** | |
* Class Subscriber_Edit_Comments | |
*/ | |
class Subscriber_Edit_Comments { | |
/** | |
* Plugin instance. | |
* | |
* @see get_instance() | |
* @var object | |
*/ | |
protected static $instance = null; | |
/** | |
* Access this plugin’s working instance | |
* | |
* @wp-hook plugins_loaded | |
* @return object of this class | |
*/ | |
public static function get_instance() { | |
null === self::$instance and self::$instance = new self; | |
return self::$instance; | |
} | |
/** | |
* Used for regular plugin work. | |
* | |
* @wp-hook plugins_loaded | |
* @return void | |
*/ | |
public function plugin_setup() { | |
add_action( 'admin_menu', array( $this, 'edit_comments_admin_menu' ) ); | |
add_filter( 'user_has_cap', array( $this, 'edit_comment_cap' ), 10, 4 ); | |
add_filter( 'map_meta_cap', array( $this, 'edit_comment_map_meta_cap' ), 10, 4 ); | |
} | |
/** | |
* Constructor. | |
* Intentionally left empty and public. | |
* | |
* @see plugin_setup() | |
*/ | |
public function __construct() { | |
} | |
/** | |
* Overwriting the admin menu entry for the "edit_comments.php" page | |
* | |
* @todo: Remove this function when trac ticket #12104 is solved | |
* @see: https://core.trac.wordpress.org/ticket/12104 | |
*/ | |
public function edit_comments_admin_menu() { | |
global $menu, $submenu; | |
if ( current_user_can( 'moderate_comments' ) || current_user_can( 'edit_posts' ) ) { | |
return; | |
} | |
$awaiting_mod = wp_count_comments(); | |
$awaiting_mod = $awaiting_mod->moderated; | |
$menu[25] = array( | |
sprintf( __( 'Comments %s' ), '<span class="awaiting-mod count-' . absint( $awaiting_mod ) . '"><span class="pending-count">' . number_format_i18n( $awaiting_mod ) . '</span></span>' ), | |
'edit_comment', | |
'edit-comments.php', | |
'', | |
'menu-top menu-icon-comments', | |
'menu-comments', | |
'dashicons-admin-comments', | |
); | |
$submenu['edit-comments.php'][0] = array( __( 'All Comments' ), 'edit_comment', 'edit-comments.php' ); | |
} | |
/** | |
* Give users the "edit_posts" capability if they are on the "edit-comment.php" page | |
* | |
* @todo: Remove this function when trac ticket #12104 is solved | |
* @see: https://core.trac.wordpress.org/ticket/12104 | |
* | |
* @param array $allcaps An array of all the user's capabilities. | |
* @param array $caps Actual capabilities for meta capability. | |
* @param array $args Optional parameters passed to has_cap(), typically object ID. | |
* @param WP_User $user The user object. | |
* | |
* @return mixed | |
*/ | |
public function edit_comment_cap( $allcaps, $caps, $args, $user ) { | |
if ( 'edit_posts' === $args[0] ) { | |
global $pagenow; | |
if ( 'edit-comments.php' === $pagenow ) { | |
$allcaps['edit_posts'] = true; | |
} | |
} | |
if ( 'edit_comment' === $args[0] ) { | |
if ( $allcaps['moderate_comments'] ) { | |
return $allcaps; | |
} | |
$comment = get_comment( $args[2] ); | |
$current_user = wp_get_current_user(); | |
if ( $current_user->user_login === $comment->comment_author ) { | |
$allcaps['edit_comment'] = true; | |
} | |
} | |
return $allcaps; | |
} | |
/** | |
* Map the meta caps for "edit_comments" | |
* | |
* @param array $caps Returns the user's actual capabilities. | |
* @param string $cap Capability name. | |
* @param int $user_id The user ID. | |
* @param array $args Adds the context to the cap. Typically the object ID. | |
* | |
* @return array | |
*/ | |
public function edit_comment_map_meta_cap( $caps, $cap, $user_id, $args ) { | |
if ( 'edit_comment' === $cap ) { | |
$comment = get_comment( $args[2] ); | |
$current_user = wp_get_current_user(); | |
if ( $current_user->user_login === $comment->comment_author ) { | |
$caps = array( 'edit_comment' ); | |
} | |
} | |
return $caps; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment