features
4 years ago
roles
4 years ago
admin-load.php
4 years ago
admin.php
4 years ago
backup-handler.php
4 years ago
backup.php
4 years ago
cap-helper.php
4 years ago
filters-admin.php
4 years ago
filters-woocommerce.php
7 years ago
filters-wp_rest_workarounds.php
5 years ago
filters.php
4 years ago
functions-admin.php
4 years ago
functions.php
4 years ago
handler.php
4 years ago
inflect-cme.php
7 years ago
manager.php
4 years ago
network.php
4 years ago
pp-handler.php
4 years ago
pp-ui.php
4 years ago
publishpress-roles.php
5 years ago
settings-handler.php
4 years ago
settings.php
4 years ago
admin-load.php
311 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * PublishPress Capabilities [Free] |
| 5 | * |
| 6 | * Admin execution controller: menu registration and other filters and actions that need to be loaded for every wp-admin URL |
| 7 | * |
| 8 | * This module should not include full functions related to our own plugin screens. |
| 9 | * Instead, use these filter and action handlers to load other classes when needed. |
| 10 | * |
| 11 | */ |
| 12 | class PP_Capabilities_Admin_UI { |
| 13 | function __construct() { |
| 14 | global $pagenow; |
| 15 | |
| 16 | /** |
| 17 | * The class responsible for handling notifications |
| 18 | */ |
| 19 | require_once (dirname(CME_FILE) . '/classes/pp-capabilities-notices.php'); |
| 20 | |
| 21 | add_action('init', [$this, 'featureRestrictionsGutenberg']); |
| 22 | |
| 23 | if (is_admin()) { |
| 24 | add_action('admin_init', [$this, 'featureRestrictionsClassic']); |
| 25 | } |
| 26 | |
| 27 | add_action('admin_enqueue_scripts', [$this, 'adminScripts'], 100); |
| 28 | add_action('admin_print_scripts', [$this, 'adminPrintScripts']); |
| 29 | |
| 30 | add_action('profile_update', [$this, 'action_profile_update'], 10, 2); |
| 31 | |
| 32 | if (is_multisite()) { |
| 33 | add_action('add_user_to_blog', [$this, 'action_profile_update'], 9); |
| 34 | } else { |
| 35 | add_action('user_register', [$this, 'action_profile_update'], 9); |
| 36 | } |
| 37 | |
| 38 | if (is_admin() && (isset($_REQUEST['page']) && (in_array($_REQUEST['page'], ['pp-capabilities', 'pp-capabilities-backup', 'pp-capabilities-roles', 'pp-capabilities-admin-menus', 'pp-capabilities-editor-features', 'pp-capabilities-nav-menus', 'pp-capabilities-settings', 'pp-capabilities-admin-features'])) |
| 39 | |
| 40 | || (!empty($_REQUEST['action']) && in_array($_REQUEST['action'], ['pp-roles-add-role', 'pp-roles-delete-role', 'pp-roles-hide-role', 'pp-roles-unhide-role'])) |
| 41 | || ( ! empty($_SERVER['SCRIPT_NAME']) && strpos( $_SERVER['SCRIPT_NAME'], 'p-admin/plugins.php' ) && ! empty($_REQUEST['action'] ) ) |
| 42 | || ( isset($_GET['action']) && 'reset-defaults' == $_GET['action'] ) |
| 43 | || in_array( $pagenow, array( 'users.php', 'user-edit.php', 'profile.php', 'user-new.php' ) ) |
| 44 | ) ) { |
| 45 | global $capsman; |
| 46 | |
| 47 | // Run the plugin |
| 48 | require_once ( dirname(CME_FILE) . '/framework/lib/formating.php' ); |
| 49 | require_once ( dirname(CME_FILE) . '/framework/lib/users.php' ); |
| 50 | |
| 51 | require_once ( dirname(CME_FILE) . '/includes/manager.php' ); |
| 52 | $capsman = new CapabilityManager(); |
| 53 | } else { |
| 54 | add_action( 'admin_menu', [$this, 'cmeSubmenus'], 20 ); |
| 55 | } |
| 56 | |
| 57 | add_action('init', function() { // late execution avoids clash with autoloaders in other plugins |
| 58 | global $pagenow; |
| 59 | |
| 60 | if ((($pagenow == 'admin.php') && isset($_GET['page']) && in_array($_GET['page'], ['pp-capabilities', 'pp-capabilities-roles', 'pp-capabilities-backup'])) // @todo: CSS for button alignment in Editor Features, Admin Features |
| 61 | || (defined('DOING_AJAX') && DOING_AJAX && (false !== strpos($_REQUEST['action'], 'capability-manager-enhanced'))) |
| 62 | ) { |
| 63 | if (!class_exists('\PublishPress\WordPressReviews\ReviewsController')) { |
| 64 | include_once PUBLISHPRESS_CAPS_ABSPATH . '/vendor/publishpress/wordpress-reviews/ReviewsController.php'; |
| 65 | } |
| 66 | |
| 67 | if (class_exists('\PublishPress\WordPressReviews\ReviewsController')) { |
| 68 | $reviews = new \PublishPress\WordPressReviews\ReviewsController( |
| 69 | 'capability-manager-enhanced', |
| 70 | 'PublishPress Capabilities', |
| 71 | plugin_dir_url(CME_FILE) . 'common/img/capabilities-wp-logo.png' |
| 72 | ); |
| 73 | |
| 74 | add_filter('publishpress_wp_reviews_display_banner_capability-manager-enhanced', [$this, 'shouldDisplayBanner']); |
| 75 | |
| 76 | $reviews->init(); |
| 77 | } |
| 78 | } |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | public function shouldDisplayBanner() { |
| 83 | global $pagenow; |
| 84 | |
| 85 | return ($pagenow == 'admin.php') && isset($_GET['page']) && in_array($_GET['page'], ['pp-capabilities', 'pp-capabilities-roles', 'pp-capabilities-backup']); |
| 86 | } |
| 87 | |
| 88 | private function applyFeatureRestrictions($editor = 'gutenberg') { |
| 89 | global $pagenow; |
| 90 | |
| 91 | // Return if not a post editor request |
| 92 | if (!in_array($pagenow, ['post.php', 'post-new.php'])) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | static $def_post_types; // avoid redundant filter application |
| 97 | |
| 98 | if (!isset($def_post_types)) { |
| 99 | //$def_post_types = apply_filters('pp_capabilities_feature_post_types', get_post_types(['public' => true])); |
| 100 | $def_post_types = apply_filters('pp_capabilities_feature_post_types', ['post', 'page']); |
| 101 | } |
| 102 | |
| 103 | $post_type = pp_capabilities_get_post_type(); |
| 104 | |
| 105 | // Return if not a supported post type |
| 106 | if (!in_array($post_type, $def_post_types)) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | switch ($editor) { |
| 111 | case 'gutenberg': |
| 112 | if (_pp_capabilities_is_block_editor_active()) { |
| 113 | require_once ( dirname(CME_FILE) . '/includes/features/restrict-editor-features.php' ); |
| 114 | PP_Capabilities_Post_Features::applyRestrictions($post_type); |
| 115 | } |
| 116 | |
| 117 | break; |
| 118 | |
| 119 | case 'classic': |
| 120 | if (!_pp_capabilities_is_block_editor_active()) { |
| 121 | require_once ( dirname(CME_FILE) . '/includes/features/restrict-editor-features.php' ); |
| 122 | PP_Capabilities_Post_Features::adminInitClassic($post_type); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | function featureRestrictionsGutenberg() { |
| 128 | $this->applyFeatureRestrictions(); |
| 129 | } |
| 130 | |
| 131 | function featureRestrictionsClassic() { |
| 132 | $this->applyFeatureRestrictions('classic'); |
| 133 | } |
| 134 | |
| 135 | function adminScripts() { |
| 136 | global $publishpress; |
| 137 | |
| 138 | if (function_exists('get_current_screen') && (!defined('PUBLISHPRESS_VERSION') || empty($publishpress) || empty($publishpress->modules) || empty($publishpress->modules->roles))) { |
| 139 | $screen = get_current_screen(); |
| 140 | |
| 141 | if ('user-edit' === $screen->base || ('user' === $screen->base && 'add' === $screen->action && defined('PP_CAPABILITIES_ADD_USER_MULTI_ROLES'))) { |
| 142 | // Check if we are on the user's profile page |
| 143 | wp_enqueue_script( |
| 144 | 'pp-capabilities-chosen-js', |
| 145 | plugin_dir_url(CME_FILE) . 'common/libs/chosen-v1.8.3/chosen.jquery.js', |
| 146 | ['jquery'], |
| 147 | CAPSMAN_VERSION |
| 148 | ); |
| 149 | |
| 150 | wp_enqueue_script( |
| 151 | 'pp-capabilities-roles-profile-js', |
| 152 | plugin_dir_url(CME_FILE) . 'common/js/profile.js', |
| 153 | ['jquery', 'pp-capabilities-chosen-js'], |
| 154 | CAPSMAN_VERSION |
| 155 | ); |
| 156 | |
| 157 | wp_enqueue_style( |
| 158 | 'pp-capabilities-chosen-css', |
| 159 | plugin_dir_url(CME_FILE) . 'common/libs/chosen-v1.8.3/chosen.css', |
| 160 | false, |
| 161 | CAPSMAN_VERSION |
| 162 | ); |
| 163 | wp_enqueue_style( |
| 164 | 'pp-capabilities-roles-profile-css', |
| 165 | plugin_dir_url(CME_FILE) . 'common/css/profile.css', |
| 166 | ['pp-capabilities-chosen-css'], |
| 167 | CAPSMAN_VERSION |
| 168 | ); |
| 169 | |
| 170 | $roles = !empty($_GET['user_id']) ?$this->getUsersRoles($_GET['user_id']) : []; |
| 171 | |
| 172 | if (empty($roles)) { |
| 173 | $roles = (array) get_option('default_role'); |
| 174 | } |
| 175 | |
| 176 | wp_localize_script( |
| 177 | 'pp-capabilities-roles-profile-js', |
| 178 | 'ppCapabilitiesProfileData', |
| 179 | [ |
| 180 | 'selected_roles' => $roles |
| 181 | ] |
| 182 | ); |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | function adminPrintScripts() { |
| 188 | // Counteract overzealous menu icon styling in PublishPress <= 3.2.0 :) |
| 189 | if (defined('PUBLISHPRESS_VERSION') && version_compare(constant('PUBLISHPRESS_VERSION'), '3.2.0', '<=') && defined('PP_CAPABILITIES_FIX_ADMIN_ICON')):?> |
| 190 | <style type="text/css"> |
| 191 | #toplevel_page_pp-capabilities .dashicons-before::before, #toplevel_page_pp-capabilities .wp-has-current-submenu .dashicons-before::before { |
| 192 | background-image: inherit !important; |
| 193 | content: "\f112" !important; |
| 194 | } |
| 195 | </style> |
| 196 | <?php endif; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns a list of roles with name and display name to populate a select field. |
| 201 | * |
| 202 | * @param int $userId |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | protected function getUsersRoles($userId) |
| 207 | { |
| 208 | if (empty($userId)) { |
| 209 | return []; |
| 210 | } |
| 211 | |
| 212 | $user = get_user_by('id', $userId); |
| 213 | |
| 214 | if (empty($user)) { |
| 215 | return []; |
| 216 | } |
| 217 | |
| 218 | return $user->roles; |
| 219 | } |
| 220 | |
| 221 | public function action_profile_update($userId, $oldUserData = []) |
| 222 | { |
| 223 | // Check if we need to update the user's roles, allowing to set multiple roles. |
| 224 | if (isset($_POST['pp_roles']) && current_user_can('promote_users')) { |
| 225 | // Remove the user's roles |
| 226 | $user = get_user_by('ID', $userId); |
| 227 | |
| 228 | $newRoles = $_POST['pp_roles']; |
| 229 | $currentRoles = $user->roles; |
| 230 | |
| 231 | if (empty($newRoles) || !is_array($newRoles)) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | // Remove unselected roles |
| 236 | foreach ($currentRoles as $role) { |
| 237 | // Check if it is a bbPress rule. If so, don't remove it. |
| 238 | $isBBPressRole = preg_match('/^bbp_/', $role); |
| 239 | |
| 240 | if (!in_array($role, $newRoles) && !$isBBPressRole) { |
| 241 | $user->remove_role($role); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Add new roles |
| 246 | foreach ($newRoles as $role) { |
| 247 | if (!in_array($role, $currentRoles)) { |
| 248 | $user->add_role($role); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | |
| 255 | // perf enhancement: display submenu links without loading framework and plugin code |
| 256 | function cmeSubmenus() { |
| 257 | // First we check if user is administrator and can 'manage_capabilities'. |
| 258 | if (current_user_can('administrator') && ! current_user_can('manage_capabilities')) { |
| 259 | if ($admin = get_role('administrator')) { |
| 260 | $admin->add_cap('manage_capabilities'); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | $cap_name = (is_multisite() && is_super_admin()) ? 'read' : 'manage_capabilities'; |
| 265 | |
| 266 | $permissions_title = __('Capabilities', 'capsman-enhanced'); |
| 267 | |
| 268 | $menu_order = 72; |
| 269 | |
| 270 | if (defined('PUBLISHPRESS_PERMISSIONS_MENU_GROUPING')) { |
| 271 | foreach ((array)get_option('active_plugins') as $plugin_file) { |
| 272 | if ( false !== strpos($plugin_file, 'publishpress.php') ) { |
| 273 | $menu_order = 27; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | add_menu_page( |
| 279 | $permissions_title, |
| 280 | $permissions_title, |
| 281 | $cap_name, |
| 282 | 'pp-capabilities', |
| 283 | 'cme_fakefunc', |
| 284 | 'dashicons-admin-network', |
| 285 | $menu_order |
| 286 | ); |
| 287 | |
| 288 | add_submenu_page('pp-capabilities', __('Roles', 'capsman-enhanced'), __('Roles', 'capsman-enhanced'), $cap_name, 'pp-capabilities-roles', 'cme_fakefunc'); |
| 289 | add_submenu_page('pp-capabilities', __('Editor Features', 'capsman-enhanced'), __('Editor Features', 'capsman-enhanced'), $cap_name, 'pp-capabilities-editor-features', 'cme_fakefunc'); |
| 290 | add_submenu_page('pp-capabilities', __('Admin Features', 'capsman-enhanced'), __('Admin Features', 'capsman-enhanced'), $cap_name, 'pp-capabilities-admin-features', 'cme_fakefunc'); |
| 291 | add_submenu_page('pp-capabilities', __('Admin Menus', 'capsman-enhanced'), __('Admin Menus', 'capsman-enhanced'), $cap_name, 'pp-capabilities-admin-menus', 'cme_fakefunc'); |
| 292 | add_submenu_page('pp-capabilities', __('Nav Menus', 'capsman-enhanced'), __('Nav Menus', 'capsman-enhanced'), $cap_name, 'pp-capabilities-nav-menus', 'cme_fakefunc'); |
| 293 | add_submenu_page('pp-capabilities', __('Backup', 'capsman-enhanced'), __('Backup', 'capsman-enhanced'), $cap_name, 'pp-capabilities-backup', 'cme_fakefunc'); |
| 294 | |
| 295 | if (defined('PUBLISHPRESS_CAPS_PRO_VERSION')) { |
| 296 | add_submenu_page('pp-capabilities', __('Settings', 'capsman-enhanced'), __('Settings', 'capsman-enhanced'), $cap_name, 'pp-capabilities-settings', 'cme_fakefunc'); |
| 297 | } |
| 298 | |
| 299 | if (!defined('PUBLISHPRESS_CAPS_PRO_VERSION')) { |
| 300 | add_submenu_page( |
| 301 | 'pp-capabilities', |
| 302 | __('Upgrade to Pro', 'capsman-enhanced'), |
| 303 | __('Upgrade to Pro', 'capsman-enhanced'), |
| 304 | 'manage_capabilities', |
| 305 | 'capsman-enhanced', |
| 306 | 'cme_fakefunc' |
| 307 | ); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 |