Making WordPress.org

Ticket #3918: 3918.2.patch

File 3918.2.patch, 9.7 KB (added by netweb, 6 years ago)
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-helphub/inc/helphub-manager/class-helphub-manager.php

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

     
    2222require_once( dirname( __FILE__ ) . '/inc/table-of-contents-lite/table-of-contents-lite.php' );
    2323require_once( dirname( __FILE__ ) . '/inc/helphub-front-page-blocks/helphub-front-page-blocks.php' );
    2424require_once( dirname( __FILE__ ) . '/inc/helphub-customroles/class-helphub-custom-roles.php' );
     25require_once( dirname( __FILE__ ) . '/inc/helphub-manager/class-helphub-manager.php' );