| 1 | <?php |
| 2 | /** |
| 3 | * This adds custom roles for the HelpHub project. |
| 4 | * |
| 5 | * @package HelpHub |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | class HelpHub_Manager { |
| 13 | |
| 14 | /** |
| 15 | * The single instance of HelpHub_Custom_Roles. |
| 16 | * |
| 17 | * @var object |
| 18 | * @access private |
| 19 | * @since 1.0.0 |
| 20 | */ |
| 21 | private static $_instance = null; |
| 22 | |
| 23 | /** |
| 24 | * Settings class object |
| 25 | * |
| 26 | * @var object |
| 27 | * @access public |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | public $settings = null; |
| 31 | |
| 32 | /** |
| 33 | * The version number. |
| 34 | * |
| 35 | * @var string |
| 36 | * @access public |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public $_version; |
| 40 | |
| 41 | /** |
| 42 | * The token. |
| 43 | * |
| 44 | * @var string |
| 45 | * @access public |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | public $_token; |
| 49 | |
| 50 | /** |
| 51 | * The main plugin file. |
| 52 | * |
| 53 | * @var string |
| 54 | * @access public |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | public $file; |
| 58 | |
| 59 | /** |
| 60 | * The main plugin directory. |
| 61 | * |
| 62 | * @var string |
| 63 | * @access public |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | public $dir; |
| 67 | |
| 68 | /** |
| 69 | * The plugin assets directory. |
| 70 | * |
| 71 | * @var string |
| 72 | * @access public |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public $assets_dir; |
| 76 | |
| 77 | /** |
| 78 | * The plugin assets URL. |
| 79 | * |
| 80 | * @var string |
| 81 | * @access public |
| 82 | * @since 1.0.0 |
| 83 | */ |
| 84 | public $assets_url; |
| 85 | |
| 86 | /** |
| 87 | * Suffix for Javascripts. |
| 88 | * |
| 89 | * @var string |
| 90 | * @access public |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | public $script_suffix; |
| 94 | |
| 95 | /** |
| 96 | * Custom roles Constructor. |
| 97 | * |
| 98 | * @param string $file filename. |
| 99 | * @param string $version version. |
| 100 | */ |
| 101 | public function __construct( $file = '', $version = '1.0.0' ) { |
| 102 | $this->_version = $version; |
| 103 | $this->_token = 'helphub_manager'; |
| 104 | |
| 105 | $this->file = $file; |
| 106 | $this->dir = dirname( $this->file ); |
| 107 | |
| 108 | $this->add_helphub_customrole(); |
| 109 | |
| 110 | add_action( 'bbp_template_after_user_profile', array( $this, 'helphub_profile_section' ) ); |
| 111 | |
| 112 | add_action( 'bbp_post_request', array( $this, 'helphub_profile_edits' ) ); |
| 113 | } // End __construct () |
| 114 | |
| 115 | /** |
| 116 | * Main HelpHub_Manager Instance |
| 117 | * |
| 118 | * Ensures only one instance of HelpHub_Manager is loaded or can be loaded. |
| 119 | * |
| 120 | * @param string $file Filename of site. |
| 121 | * @param string $version Version number. |
| 122 | * @since 1.0.0 |
| 123 | * @static |
| 124 | * @see HelpHub_Custom_Roles() |
| 125 | * @return Main HelpHub_Manager instance |
| 126 | */ |
| 127 | public static function instance( $file = '', $version = '1.0.0' ) { |
| 128 | if ( is_null( self::$_instance ) ) { |
| 129 | self::$_instance = new self( $file, $version ); |
| 130 | } |
| 131 | return self::$_instance; |
| 132 | } // End instance () |
| 133 | |
| 134 | /** |
| 135 | * Cloning is forbidden. |
| 136 | * |
| 137 | * @since 1.0.0 |
| 138 | */ |
| 139 | public function __clone() { |
| 140 | _doing_it_wrong( __FUNCTION__, esc_html( __( 'Sorry, this is not allowed.', 'wporg-forums' ) ), esc_html( $this->_version ) ); |
| 141 | } // End __clone () |
| 142 | |
| 143 | /** |
| 144 | * Unserializing instances of this class is forbidden. |
| 145 | * |
| 146 | * @since 1.0.0 |
| 147 | */ |
| 148 | public function __wakeup() { |
| 149 | _doing_it_wrong( __FUNCTION__, esc_html( __( 'Sorry, this is not allowed.', 'wporg-forums' ) ), esc_html( $this->_version ) ); |
| 150 | } // End __wakeup () |
| 151 | |
| 152 | public function get_helphub_roles() { |
| 153 | return array( |
| 154 | 'helphub_editor' => esc_html__( 'HelpHub Editor', 'wporg-forums' ), |
| 155 | 'helphub_manager' => esc_html__( 'HelpHub Manager', 'wporg-forums' ), |
| 156 | ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Output markup for various HelpHub managements in the user profile section of bbPress. |
| 161 | */ |
| 162 | public function helphub_profile_section() { |
| 163 | /* |
| 164 | * Don't process anything if the user in question is lacking the proper capabilities. |
| 165 | * |
| 166 | * For our use, that means HelpHub Managers, anyone with higher access can use the appropriate edit screens. |
| 167 | */ |
| 168 | if ( ! current_user_can( 'manage_helphub' ) ) { |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | // Also don't allow editing your own user. |
| 173 | if ( bbp_get_displayed_user_id() === get_current_user_id() ) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | $helphub_roles = $this->get_helphub_roles(); |
| 178 | |
| 179 | // Get users current blog role. |
| 180 | $user_role = bbp_get_user_blog_role( bbp_get_displayed_user_id() ); |
| 181 | |
| 182 | /* |
| 183 | * Only allow changing roles of users that are HelpHub related, or do not already hold |
| 184 | * a role within the user hierarchy as is. |
| 185 | * |
| 186 | * This is to prevent overriding users with higher capabilities altogether. |
| 187 | */ |
| 188 | if ( ! empty( $user_role ) && ! isset( $helphub_roles[ $user_role ] ) ) { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | ?> |
| 193 | |
| 194 | <div class="wporg-support-helphub"> |
| 195 | <h2 id="helphub" class="entry-title"><?php esc_html_e( 'HelpHub', 'wporg-forums' ); ?></h2> |
| 196 | <div class="bbp-user-section"> |
| 197 | <form action="" method="post"> |
| 198 | <fieldset class="bbp-form"> |
| 199 | <label for="role"><?php esc_html_e( 'HelpHub Role', 'wporg-forums' ); ?></label> |
| 200 | <select name="role" id="role"> |
| 201 | <option value=""><?php esc_html_e( '— No role for this site —', 'wporg-forums' ); ?></option> |
| 202 | |
| 203 | <?php foreach ( $helphub_roles as $role => $label ) : ?> |
| 204 | |
| 205 | <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo $label; ?></option> |
| 206 | |
| 207 | <?php endforeach; ?> |
| 208 | </select> |
| 209 | </fieldset> |
| 210 | |
| 211 | <fieldset class="submit"> |
| 212 | <legend><?php esc_html_e( 'Save Changes', 'wporg-forums' ); ?></legend> |
| 213 | <div> |
| 214 | <input type="hidden" name="action" id="helphub_post_action" value="helphub-update-user"> |
| 215 | <input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( bbp_get_displayed_user_id() ); ?>"> |
| 216 | |
| 217 | <?php wp_nonce_field( 'helphub-change-user-role-' . bbp_get_displayed_user_id(), '_helphub_manage' ); ?> |
| 218 | |
| 219 | <button type="submit" class="button submit user-submit"><?php esc_html_e( 'Update User', 'wporg-forums' ); ?></button> |
| 220 | </div> |
| 221 | </fieldset> |
| 222 | </form> |
| 223 | </div> |
| 224 | </div> |
| 225 | |
| 226 | <?php |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Capture and perform any profile edits initiated by a HelpHub Manager. |
| 231 | */ |
| 232 | public function helphub_profile_edits() { |
| 233 | // Don't process anything if the post actions are invalid. |
| 234 | if ( ! isset( $_POST['action'] ) || 'helphub-update-user' !== $_POST['action'] ) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // Get the displayed user ID. |
| 239 | $user_id = bbp_get_displayed_user_id(); |
| 240 | |
| 241 | // Ensure the proper user capabilities exist for changing user details. |
| 242 | if ( ! current_user_can( 'manage_helphub' ) ) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // Double-check that nobody is trying to edit their own user. |
| 247 | if ( get_current_user_id() === $user_id ) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | // Check that the nonce is valid. |
| 252 | if ( ! wp_verify_nonce( $_POST['_helphub_manage'], 'helphub-change-user-role-' . $user_id ) ) { |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | // Make sure the new role is a HelpHub one, or is being reset to nothing. |
| 257 | $roles = $this->get_helphub_roles(); |
| 258 | if ( ! empty( $_POST['role'] ) && ! isset( $roles[ $_POST['role'] ] ) ) { |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | $user_forum_role = bbp_get_user_role( $user_id ); |
| 263 | |
| 264 | $user = new stdClass(); |
| 265 | |
| 266 | $user->ID = (int) $user_id; |
| 267 | $user->role = $_POST['role']; |
| 268 | |
| 269 | $edit_user = wp_update_user( $user ); |
| 270 | |
| 271 | // Updating a user resets the forum role, so let's explicitly update that. |
| 272 | bbp_set_user_role( $user_id, $user_forum_role ); |
| 273 | |
| 274 | // Error(s) editng the user, so copy them into the global. |
| 275 | if ( is_wp_error( $edit_user ) ) { |
| 276 | bbpress()->errors = $edit_user; |
| 277 | |
| 278 | // Successful edit to redirect. |
| 279 | } elseif ( is_integer( $edit_user ) ) { |
| 280 | $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_url( $edit_user ) ); |
| 281 | |
| 282 | wp_safe_redirect( $redirect ); |
| 283 | exit; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Adds a HelpHub custom role. |
| 289 | */ |
| 290 | public function add_helphub_customrole() { |
| 291 | |
| 292 | // Load users library. |
| 293 | if ( ! function_exists( 'get_editable_roles' ) ) { |
| 294 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
| 295 | } |
| 296 | get_editable_roles(); |
| 297 | $role = 'helphub_manager'; |
| 298 | |
| 299 | // Check if the HelpHub Manager role is already added. |
| 300 | global $wp_roles; |
| 301 | $default_editorroles = $wp_roles->get_role( 'editor' ); |
| 302 | if ( empty( $GLOBALS['wp_roles']->is_role( $role ) ) ) { |
| 303 | $wp_roles->add_role( $role, __( 'HelpHub Manager', 'wporg-forums' ), $default_editorroles->capabilities ); |
| 304 | |
| 305 | $wp_roles->add_cap( $role, 'edit_theme_options' ); |
| 306 | $wp_roles->add_cap( $role, 'manage_helphub' ); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Returns the main instance of HelpHub_Manager to prevent the need to use globals. |
| 313 | * |
| 314 | * @since 1.0.0 |
| 315 | * @return object HelpHub_Custom_Roles |
| 316 | */ |
| 317 | function helphub_manager() { |
| 318 | $instance = HelpHub_Manager::instance( __FILE__, '1.0.0' ); |
| 319 | return $instance; |
| 320 | } |
| 321 | |
| 322 | helphub_manager(); |