├── .gitignore ├── CHANGELOG.MD ├── LICENSE ├── README.md ├── composer.json ├── docs └── images │ └── screenshot-admin.png ├── geniem-roles-styles.css └── plugin.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | -------------------------------------------------------------------------------- /CHANGELOG.MD: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## Unreleased 8 | 9 | ## Released 10 | 11 | ## [1.6.1] - 2024-05-20 12 | - Fix method param and return types. 13 | 14 | ## [1.6.0] - 2024-01-31 15 | - Removed role default caps false values. 16 | - Default caps has been listed in wiki page from now on. 17 | 18 | ## [1.5.2] - 2022-11-23 19 | - Fixed role renaming `rename()` (props [@jmaekki](https://github.com/jmaekki)). 20 | 21 | ## [1.5.1] - 2022-05-31 22 | - Optimized admin screenshot. 23 | 24 | ## [1.5.0] - 2021-11-11 25 | - `grant_post_edit()` hotfix to handle only post type related caps inside map_meta_cap(). 26 | - Added parameter $post_types to the function `grant_post_edit()`. 27 | 28 | ## [1.4.0] - 2021-08-31 29 | - grant_post_edit() to grant posts with the given ids and caps. 30 | - You can also define caps for the blocked posts. 31 | 32 | ## [1.3.1] - 2021-01-19 33 | - Removed hook that resets roles on wp-geniem-roles admin page. This might cause undesired situations if users reset roles by accident. Use these methods instead https://github.com/devgeniem/wp-geniem-roles#reset-roles-with-wp-geniem-roles. 34 | 35 | ## [1.3.0] - 2020-12-10 36 | - Added a method `remove_admin_bar_nodes()`. This method removes admin bar nodes by node id. 37 | 38 | ## [1.2.2] - 2020-10-02 39 | 40 | ## Fixed 41 | - Fixed functions `add_caps()` and `remove_caps()` for the default WordPress roles. 42 | 43 | ## [1.2.1] - 2019-10-04 44 | 45 | ## Modified 46 | - Added global `$menu` variable empty check when removing menu pages. 47 | 48 | ## [1.2.0] - 2019-10-04 49 | 50 | ## Modified 51 | - Adds functions which makes possible to restrict roles available templates. 52 | 53 | ## [1.1.2] - 2019-10-02 54 | 55 | ## Modified 56 | - Adds support for array of capabilities for `restrict_post_edit()`. 57 | 58 | ## [1.1.1] - 2019-09-09 59 | 60 | ## Modified 61 | - Fixes a bug with a cap `add_user` and function `restrict_user_management_by_role()`. 62 | 63 | ## [1.1.0] - 2019-08-20 64 | 65 | ### Added 66 | - Support for resetting roles on multisite with CLI command `wp eval "\Geniem\Roles::reset_roles();"` 67 | 68 | ## [1.0.3] - 2019-03-01 69 | 70 | ### Modified 71 | - Version bump. 72 | 73 | ## [1.0.2] - 2019-01-25 74 | 75 | ### Added 76 | - Reset roles on wp-geniem-roles admin page on load. 77 | 78 | ### Modified 79 | - Fixed wp-geniem-roles option page rename issue. 80 | - remove_menu_pages() removes now also admin_bar menu pages. 81 | 82 | ## [1.0.1] - 2018-10-18 83 | 84 | ### Modified 85 | - Better documentation. 86 | - Escaping strings. 87 | - Role reset function `reset_to_default_roles()` to reset roles to the WordPress standard roles. 88 | - Role reseting function for development `reset_roles()` to reset roles after changes. 89 | 90 | ## [user-management-documentation] - 2018-04-26 91 | 92 | ### Modified 93 | - Enhanced documentation for `get_default_caps()` and `README.md` heading for networks. 94 | 95 | ## [0.4.0] - 2018-03-26 96 | 97 | ### Added 98 | - Added function `restrict_user_management_by_role()`. 99 | - Added documentation for `restrict_user_management_by_role()`. 100 | - Hotfix for role rename function. 101 | - Added support for removing WordPress customizer menu page `customize.php` from the admin menu. Changes implemented in the function `remove_menu_pages()`. 102 | 103 | ## [0.3] - 2017-11-12 104 | 105 | ### Modified 106 | - Singleton is now woken in plugin.php. User doesn't need to create singleton in the theme code anymore. Singleton is initialized when Geniem Roles has been activated. 107 | - Function `load_current_roles()` runs now on `setup_theme` hook. Global `$wp_roles` isn't avalaible before `setup_theme` hook. 108 | - `remove_menu_pages()` can now remove also submenu pages. See the documentation for the details. 109 | - Added check for function `add_caps()`. If cap already exists for the role -> do not try to add the cap. 110 | - Added check for function `remove_caps()`. If cap is already removed from the role -> do not try to remove the cap. 111 | 112 | ### Removed 113 | - Removed function `init()`. 114 | - Removed function `remove_submenu_pages()`. `remove_menu_pages()` does now the same thing in more intuitive way. 115 | - Removed documentation for `remove_submenu_pages()`. 116 | 117 | ## [0.2.4] - 2017-09-25 118 | 119 | ### Added 120 | - Menu slugs page that lists all admin menu page slugs 121 | 122 | ## [0.2.3] - 2017-09-11 123 | 124 | ### Added 125 | - CHANGELOG.MD 126 | 127 | ### Changed 128 | - Documentation: How to remove plugin pages from admin menu with function `remove_menu_pages()`. 129 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Geniem Roles 2 | Wrapper classes for developers to create and manipulate WordPress roles. 3 | 4 | # Table of contents 5 | - [Geniem Roles](#geniem-roles) 6 | - [Table of contents](#table-of-contents) 7 | - [Installation](#installation) 8 | - [Composer installation](#composer-installation) 9 | - [Manual installation](#manual-installation) 10 | - [Development guidelines](#development-guidelines) 11 | - [Reset roles with wp-geniem-roles](#reset-roles-with-wp-geniem-roles) 12 | - [Reset roles to WordPress default roles](#reset-roles-to-wordpress-default-roles) 13 | - [Reset roles on multisite with WP CLI](#reset-roles-on-multisite-with-wp-cli) 14 | - [WP CLI reset roles.](#wp-cli-reset-roles) 15 | - [Examples / Features](#examples--features) 16 | - [Initializing Geniem Roles](#initializing-geniem-roles) 17 | - [Create a new role with capabilities](#create-a-new-role-with-capabilities) 18 | - [Get and manipulate a role](#get-and-manipulate-a-role) 19 | - [Add caps for a role](#add-caps-for-a-role) 20 | - [Remove caps from a role](#remove-caps-from-a-role) 21 | - [Remove a role.](#remove-a-role) 22 | - [Rename a role.](#rename-a-role) 23 | - [Remove menu pages from a role](#remove-menu-pages-from-a-role) 24 | - [Remove admin bar nodes by a role](#remove-admin-bar-nodes-by-a-role) 25 | - [Restrict post editing and deleting](#restrict-post-editing-and-deleting) 26 | - [Grant post editing](#grant-post-editing) 27 | - [Restrict user management by role](#restrict-user-management-by-role) 28 | - [Restrict user template management by role](#restrict-user-template-management-by-role) 29 | - [Grant super admin cap for a single user](#grant-super-admin-cap-for-a-single-user) 30 | - [Filters](#filters) 31 | - ['geniem/roles/add\_menu\_page\_cap'](#geniemrolesadd_menu_page_cap) 32 | - [Example usage](#example-usage) 33 | - ['geniem/roles/default\_roles'](#geniemrolesdefault_roles) 34 | - [Example usage](#example-usage-1) 35 | - [Admin page role listing](#admin-page-role-listing) 36 | - [screenshot](#screenshot) 37 | - [Admin page menu slugs](#admin-page-menu-slugs) 38 | - [Wiki](#wiki) 39 | 40 | # Installation 41 | ## Composer installation 42 | command line 43 | ``` 44 | composer require devgeniem/wp-geniem-roles 45 | ``` 46 | composer.json 47 | ```json 48 | ... 49 | "require": { 50 | "devgeniem/wp-geniem-roles": "*", 51 | ... 52 | } 53 | ... 54 | ``` 55 | 56 | ## Manual installation 57 | Move plugin to your WordPress installation `plugins/` folder. 58 | 59 | # Development guidelines 60 | User roles are stored in the database so you need to reset roles in the database after changes to the roles. This can be done with wp-geniem-roles or wp cli. 61 | 62 | ## Reset roles with wp-geniem-roles 63 | wp-geniem-roles resets WordPress standard and custom roles with one method. 64 | Note this is a slow process you don't want to run this on every page load. 65 | Helper function for debugging and controlled reseting of the roles. 66 | ```php 67 | \Geniem\Roles::reset_roles(); 68 | ``` 69 | 70 | ## Reset roles to WordPress default roles 71 | wp-geniem-roles resets all roles to WordPress default ones with one method. 72 | ```php 73 | \Geniem\Roles::reset_to_default_roles(); 74 | ``` 75 | 76 | ## Reset roles on multisite with WP CLI 77 | Reset roles on multisite with CLI command. This resets all default and custom roles. 78 | ```bash 79 | wp eval "\Geniem\Roles::reset_roles();" 80 | ``` 81 | 82 | ## WP CLI reset roles. 83 | You can also reset roles with WP CLI see the documentation from here. 84 | https://developer.wordpress.org/cli/commands/role/ 85 | 86 | # Examples / Features 87 | 88 | ## Initializing Geniem Roles 89 | Geniem roles plugin has been designed in singleton architecture. Singleton is created inside the plugin. After plugin has been activated you are able to use Geniem roles functions. 90 | It is still good practice to check that plugin has been activated. 91 | 92 | ```php 93 | // If wp-geniem-roles is active. 94 | if ( class_exists( '\Geniem\Roles' ) ) { 95 | // Run Geniem roles functions here. 96 | } 97 | ``` 98 | 99 | ## Create a new role with capabilities 100 | All new roles capabilities defaults to `false`. So we add just capabilities that we want to apply for the role. See the example code for a hands on example. 101 | 102 | ```php 103 | /** 104 | * Create a new role 105 | */ 106 | 107 | // Caps to be added to the new role 108 | // all caps default to false see the details plugin.php \Geniem\Role::get_default_caps() 109 | $new_role_caps = array( 110 | "activate_plugins" => true, 111 | "delete_others_pages" => true, 112 | "delete_others_posts" => true, 113 | ); 114 | 115 | // Create a new role "testrole" with wanted capabilities 116 | $new_role = \Geniem\Roles::create( 'new_role', __( 'New role', 'theme-text-domain' ), $new_role_caps ); 117 | 118 | // Check if role throws a WordPress error 119 | if ( is_wp_error( $new_role ) ) { 120 | error_log( $new_role->get_error_messages() ); 121 | } 122 | ``` 123 | 124 | ## Get and manipulate a role 125 | You can call existing role from WordPress by calling function `\Geniem\Roles::get( $role_slug );`. You can use a role as an object to manipulate the role. See the example from the below. 126 | 127 | ```php 128 | // Gets a role instance of \Geniem\Roles 129 | $admin = \Geniem\Roles::get( 'administrator' ); 130 | ``` 131 | 132 | ## Add caps for a role 133 | ```php 134 | // Define desired capabilities for a role 'administrator' 135 | $admin_caps = [ 136 | 'geniem_roles' 137 | ]; 138 | 139 | // add_caps takes an array off capabilities 140 | $admin->add_caps( $admin_caps ); 141 | ``` 142 | 143 | ## Remove caps from a role 144 | ```php 145 | // Define removable caps in an array 146 | $admin_removable_caps = [ 147 | 'edit_users', 148 | 'delete_users', 149 | 'create_users' 150 | ]; 151 | 152 | // Run function remove_caps for desired wp role 153 | $admin->remove_caps( $admin_removable_caps ); 154 | ``` 155 | 156 | ## Remove a role. 157 | ```php 158 | // Get a role. 159 | $author = \Geniem\Roles::get( 'author' ); 160 | 161 | // If role is removed successfully this will cause an error on the next page load. 162 | if ( $author ) { 163 | // Remove a role 164 | $author->remove(); 165 | } 166 | ``` 167 | 168 | ## Rename a role. 169 | ```php 170 | // Rename a role 171 | $author->rename( 'New name' ); 172 | ``` 173 | 174 | ## Remove menu pages from a role 175 | You can remove single admin menu page with `string` value or multiple pages with an `array` value. You can also remove submenu pages. See the example below for the details. 176 | ```php 177 | /** 178 | * Define removable admin pages array 179 | * 180 | * note: Most of the plugins can be removed by plugin page slug. 181 | * Geniem roles lists all menu slugs in the admin page [Geniem Roles -> Menu slugs]. 182 | * 183 | */ 184 | $admin_removable_admin_pages = [ 185 | 'edit.php', // Posts 186 | 'edit.php?post_type=page', // Pages 187 | 'edit.php?post_type=acf-field-group', // Advanced Custom Fields 188 | 'plugin_page_slug', // Remove plugin page by plugin page slug 189 | // You can also remove submenu pages like so. 190 | 'themes.php' => [ 191 | 'themes.php', 192 | // Note geniem roles has automated feature to remove 'customize.php' with ease. 193 | // No need for usually required query parameters. 194 | 'customize.php', 195 | ], 196 | ]; 197 | 198 | // Remove multiple menu pages remove_role_menu_pages( $role_slug, $menu_pages ) 199 | $admin->remove_menu_pages( $admin_removable_admin_pages ); 200 | ``` 201 | ## Remove admin bar nodes by a role 202 | You can remove admin bar nodes by a role with a function `remove_admin_bar_nodes()`. See the example below for the details. 203 | ```php 204 | /** 205 | * Define removable admin bar nodes. 206 | */ 207 | $admin_removable_admin_bar_nodes = [ 208 | 'new-post', // Default post type 'post' add new button. 209 | ]; 210 | 211 | $admin->remove_admin_bar_nodes( $admin_removable_admin_bar_nodes ); 212 | ``` 213 | 214 | ## Restrict post editing and deleting 215 | This function makes easy and fast to restrict editing of certain posts. Pass restricted post as an array of post ids and choose which capabilities you want to restrict for them. 216 | 217 | ```php 218 | /** 219 | * All possible restricted capabilities are listed below 220 | * https://codex.wordpress.org/Function_Reference/map_meta_cap 221 | */ 222 | 'delete_page' 223 | 'delete_post' 224 | 'delete_user' 225 | 'edit_comment' (3.1. Mapped to 'edit_post' meta capability.) 226 | 'edit_page' 227 | 'edit_post' 228 | 'edit_user' 229 | 'publish_post' (3.5) 230 | 'promote_user' 231 | 'read_post' 232 | 'read_page' 233 | 'remove_user' 234 | ``` 235 | 236 | ```php 237 | // Define restricted post IDs 238 | $frontpage_id = (int) get_option( 'page_on_front' ); 239 | 240 | $restricted_posts = [ 241 | $frontpage_id, 242 | 2 243 | ]; 244 | 245 | // Restricted capabilities for the post. 246 | $capabilities = [ 247 | 'delete_post', 248 | 'edit_post', 249 | ]; 250 | 251 | $admin->restrict_post_edit( $restricted_posts, $capabilities ); 252 | ``` 253 | 254 | ## Grant post editing 255 | This function makes easy and fast to grant posts by the given post ids and capabilities. 256 | Note you can also define caps for the other posts if not defined all caps will be blocked for the other posts. 257 | 258 | ```php 259 | /** 260 | * All possible restricted capabilities are listed below 261 | * https://codex.wordpress.org/Function_Reference/map_meta_cap 262 | */ 263 | 'delete_page' 264 | 'delete_post' 265 | 'delete_user' 266 | 'edit_comment' (3.1. Mapped to 'edit_post' meta capability.) 267 | 'edit_page' 268 | 'edit_post' 269 | 'edit_user' 270 | 'publish_post' (3.5) 271 | 'promote_user' 272 | 'read_post' 273 | 'read_page' 274 | 'remove_user' 275 | ``` 276 | 277 | ```php 278 | // Define restricted post IDs 279 | $frontpage_id = (int) get_option( 'page_on_front' ); 280 | 281 | $granted_post_ids = [ 282 | $frontpage_id, 283 | 2 284 | ]; 285 | 286 | // Allow edit_post cap for granted posts. 287 | $granted_posts_caps = [ 288 | 'edit_post', 289 | ]; 290 | 291 | // If empty all caps will be blocked. 292 | // Here we are allowing delete_post cap for other posts than $granted_post_ids. (optional) 293 | $restricted_posts_caps = [ 294 | 'delete_post', 295 | ]; 296 | 297 | // Post types to restrict. If defined other post types won't be handled. (optional) 298 | $post_types = [ 'page' ]; 299 | 300 | $admin->grant_post_edit( $granted_post_ids, $granted_posts_caps, $restricted_posts_caps, $post_types ); 301 | ``` 302 | 303 | ## Restrict user management by role 304 | Function to restrict users to manage users by the given roles and capabilities. 305 | Role will be removed from the WordPress admin user management dropdowns for example bulk action "Change role to" if the role has been restricted with the caps `edit_user` or `promote_user`. 306 | 307 | ```php 308 | /** 309 | * Associative array of removed caps by the role. 310 | * $key = User role to be restricted from the role. 311 | * $value = Array of removed caps from the role. 312 | * 313 | * All possible user editing caps has been listed below: 314 | * 'edit_user' 315 | * 'delete_user' 316 | * 'promote_user' 317 | * 'remove_user' 318 | */ 319 | $custom_role_restricted_user_roles = [ 320 | // Role 321 | 'administrator' => [ 322 | // Removed caps. 323 | 'edit_user', 324 | 'delete_user', 325 | 'remove_user', 326 | ], 327 | // Another role 328 | 'editor' => [ 329 | 'delete_user', 330 | ], 331 | ]; 332 | 333 | $custom_role->restrict_user_management_by_role( $custom_role_restricted_user_roles ); 334 | ``` 335 | 336 | ## Restrict user template management by role 337 | Function to restrict roles available templates. Add only templates that you want to enable. 338 | Array of slugs will be used ass a parameter. (You can inspect template slug from the wp-admin template dropdown.) 339 | `Note!: WordPress doesn't handle default template the same way as other templates. 340 | Default template will always be available for the users. If you want to enable only default template add string 'default' as a parameter.` 341 | 342 | ```php 343 | // Example 1 344 | // Allow only default template for the user. 345 | $role->restrict_role_templates( 'default' ); 346 | ``` 347 | 348 | ```php 349 | // Example 2 allow 350 | // page-frontpage.php and default template for the user. 351 | $allowed_templates = [ 352 | 'page-frontpage.php', 353 | ]; 354 | 355 | $role->restrict_role_templates( $allowed_templates ); 356 | ``` 357 | 358 | ## Grant super admin cap for a single user 359 | ```php 360 | \Geniem\Roles::grant_super_admin_cap( 1 ); 361 | ``` 362 | 363 | # Filters 364 | ## 'geniem/roles/add_menu_page_cap' 365 | Change wp-geniem-roles wp-admin menu capability. 366 | - Default value: `'activate_plugins'` 367 | - Parameter type: `string` 368 | 369 | ### Example usage 370 | ```php 371 | /** 372 | * Filter wp-geniem-roles menu page capability. 373 | * 374 | * @return string WordPress capability. 375 | */ 376 | function filter_geniem_roles_menu_page_cap( $menu_page_cap ) { 377 | 378 | return 'manage_options'; 379 | } 380 | 381 | \add_filter( 'geniem/roles/add_menu_page_cap', 'filter_geniem_roles_menu_page_cap' ); 382 | ``` 383 | ## 'geniem/roles/default_roles' 384 | Filter new role default capabilities. 385 | - Parameter type: `array` 386 | ### Example usage 387 | ```php 388 | /** 389 | * Filter wp-geniem-roles default capabilities for the new role. 390 | * 391 | * @return array An array of capabilities to be added to the new role automatically. 392 | */ 393 | function filter_geniem_roles_default_caps( $default_caps ) { 394 | 395 | $default_caps = [ 396 | 'edit_posts' => true, 397 | ]; 398 | 399 | return $default_caps; 400 | } 401 | 402 | \add_filter( 'geniem/roles/default_roles', 'filter_geniem_roles_default_caps' ); 403 | ``` 404 | 405 | # Admin page role listing 406 | `wp-geniem-roles` creates a admin page which lists all current active roles and their capabilities in the admin side. Admin page can be seen for roles that can `can_activate_plugins`. 407 | 408 | #### screenshot 409 | ![Admin side screenshot](docs/images/screenshot-admin.png) 410 | 411 | # Admin page menu slugs 412 | `wp-geniem-roles` creates a admin page which lists all menu slugs. This is helper page for usage of function `remove_menu_pages()`. See the function documentation for more information. 413 | 414 | # Wiki 415 | Tips and tricks https://github.com/devgeniem/wp-geniem-roles/wiki 416 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "devgeniem/wp-geniem-roles", 3 | "type": "wordpress-plugin", 4 | "license": "GPL-3.0", 5 | "description": "Wrapper class for WordPress role creation and manipulation.", 6 | "homepage": "https://github.com/devgeniem/wp-geniem-roles", 7 | "authors": [ 8 | { 9 | "name": "Timi-Artturi Mäkelä", 10 | "email": "timi.makela@geniem.com", 11 | "homepage": "https://github.com/Liblastic" 12 | } 13 | ], 14 | "keywords": [ 15 | "Geniem","wordpress","wp","roles","wrapper class", "must use" 16 | ], 17 | "require": { 18 | "php": ">=7.0", 19 | "composer/installers": ">=v1.0.12" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /docs/images/screenshot-admin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/devgeniem/wp-geniem-roles/2fc422287e662989f7e34644406fc0f67db7ddfe/docs/images/screenshot-admin.png -------------------------------------------------------------------------------- /geniem-roles-styles.css: -------------------------------------------------------------------------------- 1 | /** 2 | * Geniem roles admin styles 3 | */ 4 | .geniem-roles { 5 | 6 | } 7 | 8 | .geniem-roles__wrapper { 9 | display: flex; 10 | flex-flow: row wrap; 11 | } 12 | 13 | .geniem-roles__single-role { 14 | width: calc( 50% - 3rem ); 15 | float: left; 16 | margin: 1rem 1rem 0rem 0rem; 17 | background: white; 18 | padding: 1rem; 19 | } 20 | 21 | .geniem-roles__slugs { 22 | margin: 1rem; 23 | padding: 1rem; 24 | background-color: white; 25 | } 26 | 27 | .geniem-roles__slugs table { 28 | border-collapse: collapse; 29 | } 30 | 31 | .geniem-roles__slugs td { 32 | padding: 0.25rem; 33 | } 34 | 35 | .geniem-roles__slugs .child-menu td:first-child { 36 | padding-left: 2rem; 37 | } 38 | 39 | @media ( max-width: 768px ) { 40 | .geniem-roles__single-role { 41 | width: 100%; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /plugin.php: -------------------------------------------------------------------------------- 1 | role_objects as $role ) { 89 | 90 | $display_name = ''; 91 | 92 | // Loop through role_names table to get display name. 93 | foreach ( $wp_roles->role_names as $key => $value ) { 94 | 95 | if ( $key === $role->name ) { 96 | $display_name = $value; 97 | } 98 | } 99 | 100 | // Create Role instance. 101 | self::$roles[ $role->name ] = new Role( $role->name, $display_name ); 102 | } 103 | } 104 | 105 | /** 106 | * Returns all role instances created from active roles. 107 | * 108 | * @return array Array of role instances. 109 | */ 110 | public static function get_roles() { 111 | 112 | $roles = self::$roles; 113 | 114 | return $roles; 115 | } 116 | 117 | /** 118 | * Create new roles 119 | * 120 | * @param string $name Role name in lowercase. 121 | * @param string $display_name Role display name. 122 | * @param array $caps Capabilities to be added. 123 | * @return \Geniem\Role Role instance. 124 | */ 125 | public static function create( $name, $display_name, $caps ) { 126 | 127 | // If role already exists return it 128 | if ( self::role_exists( $name ) ) { 129 | 130 | $role = self::$roles[ $name ]; 131 | 132 | return $role; 133 | } else { 134 | 135 | // Merge capabilities. 136 | $caps = \wp_parse_args( $caps, Role::get_default_caps() ); 137 | 138 | // Add role. 139 | \add_role( $name, $display_name, $caps ); 140 | 141 | // Create new \Geniem\Role intance. 142 | $role_instance = new Role( $name, $display_name ); 143 | 144 | return $role_instance; 145 | } 146 | } 147 | 148 | /** 149 | * Check if role exists. 150 | * 151 | * @param string $name Role name. 152 | * @return bool True if role exists. 153 | */ 154 | public static function role_exists( $name ) { 155 | 156 | $role = \get_role( $name ); 157 | 158 | return $role !== null; 159 | } 160 | 161 | /** 162 | * Remove roles. 163 | * 164 | * @param string $name Role name. 165 | * @return void 166 | */ 167 | public static function remove_role( $name ) { 168 | 169 | // If role exists remove role 170 | if ( self::role_exists( $name ) ) { 171 | \remove_role( $name ); 172 | unset( self::$roles[ $name ] ); 173 | } 174 | } 175 | 176 | /** 177 | * Rename a role with new_display_name. 178 | * 179 | * @param string $name Role name. 180 | * @param string $new_display_name New display name for role. 181 | * @return void 182 | */ 183 | public static function rename( $name, $new_display_name ) { 184 | 185 | global $wp_roles; 186 | 187 | // Rename role 188 | $wp_roles->roles[ $name ]['name'] = $new_display_name; 189 | $wp_roles->role_names[ $name ] = $new_display_name; 190 | 191 | // Update also geniem roles instance name. 192 | self::$roles[ $name ]->display_name = $new_display_name; 193 | } 194 | 195 | /** 196 | * Add caps to the role. 197 | * 198 | * @param string $name Role name. 199 | * @param array $caps Role capabilities to be added. 200 | * @return void|false On fail returns false. 201 | */ 202 | public static function add_caps( $name, $caps ) { 203 | 204 | // Fail fast if role name is not set or caps are empty. 205 | if ( empty( $name ) || empty( $caps ) ) { 206 | return false; 207 | } 208 | 209 | // Get wp role 210 | $role = \get_role( $name ); 211 | 212 | // Loop through removed caps. 213 | foreach ( $caps as $cap ) { 214 | 215 | $cap_status = self::$roles[ $name ]->capabilities[ $cap ] ?? false; 216 | 217 | // If cap is not true add cap. 218 | if ( $cap_status !== true ) { 219 | 220 | $role->add_cap( $cap ); 221 | } 222 | } 223 | unset( $cap ); 224 | } 225 | 226 | /** 227 | * Remove capabilities from a role. 228 | * 229 | * @param string $name Role slug. 230 | * @param array $caps Array of capabilities to be removed. 231 | * @return void|false On fail returns false. 232 | */ 233 | public static function remove_caps( $name, $caps ) { 234 | 235 | // Fail fast if role name is not set or caps are empty 236 | if ( empty( $name ) || empty( $caps ) ) { 237 | return false; 238 | } 239 | 240 | // Get wp role 241 | $role = \get_role( $name ); 242 | 243 | // Loop through removed caps. 244 | foreach ( $caps as $cap ) { 245 | 246 | $cap_status = self::$roles[ $name ]->capabilities[ $cap ] ?? false; 247 | 248 | // If cap is true remove the cap. 249 | if ( $cap_status === true ) { 250 | 251 | $role->remove_cap( $cap ); 252 | } 253 | } 254 | unset( $cap ); 255 | } 256 | 257 | /** 258 | * If role exists return the role else returns false. 259 | * insert int or string 260 | * 261 | * @param string $name Role name. 262 | * @return \Geniem\Role|null Role instance or null if it doesn't exist. 263 | */ 264 | public static function get( $name ) { 265 | 266 | // Get from cache 267 | if ( isset( self::$roles[ $name ] ) ) { 268 | return self::$roles[ $name ]; 269 | } 270 | 271 | return null; 272 | } 273 | 274 | /** 275 | * Remove menu pages from a role. 276 | * note: All menu page slugs can be found from the admin Geniem Roles -> Menu slugs. 277 | * 278 | * @param string $name Role name. 279 | * @param string|array|null $menu_pages Menu page slugs. 280 | * @return void|false On fail returns false. 281 | */ 282 | public static function remove_menu_pages( $name = '', $menu_pages = null ) { 283 | 284 | // Run in admin_menu hook when called outside class 285 | add_action( 'admin_init', function() use ( $name, $menu_pages ) { 286 | 287 | global $menu; 288 | 289 | // In some weird cases $menu might be empty. 290 | // Bail early if $menu is empty. 291 | if ( empty( $menu ) ) { 292 | return false; 293 | } 294 | 295 | // user object 296 | $user = wp_get_current_user(); 297 | 298 | /** 299 | * Remove menu pages by role 300 | * Note: Some plugins cannot be removed in admin_menu -hook so we have to do it in admin_init. 301 | * In admin_init hook we have to check if not doing ajax to avoid errors. 302 | */ 303 | if ( in_array( $name, $user->roles, true ) && ! wp_doing_ajax() ) { 304 | 305 | if ( ! empty( $menu_pages ) ) { 306 | 307 | // If multiple menu pages in array. 308 | if ( is_array( $menu_pages ) && ! empty( $menu_pages ) ) { 309 | foreach ( $menu_pages as $main_lvl_key => $menu_page ) { 310 | 311 | // If there are submenu pages to be removed. 312 | if ( is_array( $menu_page ) ) { 313 | foreach ( $menu_page as $submenu_item ) { 314 | 315 | // If we want to hide customize.php from admin menu we need to do some extra checks. 316 | if ( $submenu_item === 'customize.php' ) { 317 | 318 | // Get and form current page url ending. 319 | $current_url = \esc_url( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ); 320 | $url_ending = substr( $current_url, strrpos( $current_url, '/wp-admin/' ) + 0 ); 321 | $url_ending = rawurlencode( $url_ending ); 322 | $current_customizer_url = 'customize.php?return=' . $url_ending; 323 | 324 | \remove_submenu_page( $main_lvl_key, $current_customizer_url ); 325 | 326 | } else { 327 | \remove_submenu_page( $main_lvl_key, $submenu_item ); 328 | } 329 | } 330 | } else { 331 | \remove_menu_page( $menu_page ); 332 | } // End if(). 333 | } // End foreach(). 334 | // If only one item to be removed. 335 | } elseif ( is_string( $menu_pages ) ) { 336 | \remove_menu_page( $menu_pages ); 337 | // Removed menu page isn't valid. 338 | } else { 339 | return false; 340 | } 341 | } else { 342 | error_log( 'remove_role_menu_pages called without valid $menu_pages' ); 343 | } 344 | } 345 | }); 346 | 347 | // Handle related wp_admin_bar items automatically. 348 | add_action( 'wp_before_admin_bar_render', function() use ( $menu_pages ) { 349 | 350 | global $wp_admin_bar; 351 | 352 | $nodes = $wp_admin_bar->get_nodes(); 353 | 354 | if ( ! empty( $nodes ) ) { 355 | foreach ( $nodes as $key => $node ) { 356 | 357 | $splitted_href = explode( '/', $node->href ); 358 | $end_of_url = end( $splitted_href ); 359 | 360 | $page_param_position = strpos( $end_of_url, '?page=' ); 361 | 362 | // If page parameter take the end of the string. 363 | if ( $page_param_position ) { 364 | $end_of_url_position = $page_param_position + strlen( '?page=' ); 365 | $end_of_url = substr( $end_of_url, $end_of_url_position ); 366 | } 367 | 368 | if ( self::in_array_r( $end_of_url, $menu_pages ) ) { 369 | $wp_admin_bar->remove_node( $node->id ); 370 | } 371 | } 372 | } 373 | }); 374 | } 375 | 376 | /** 377 | * Remove admin bar nodes 378 | * 379 | * @param string $name Role name. 380 | * @param array $removed_nodes Removed admin bar nodes. 381 | * @return void 382 | */ 383 | public static function remove_admin_bar_nodes( $name, $removed_nodes ) { 384 | 385 | add_action( 'wp_before_admin_bar_render', function() use ( $name, $removed_nodes ) { 386 | 387 | $user = wp_get_current_user(); 388 | 389 | if ( in_array( $name, $user->roles, true ) && ! wp_doing_ajax() ) { 390 | 391 | global $wp_admin_bar; 392 | 393 | $nodes = $wp_admin_bar->get_nodes(); 394 | 395 | if ( ! empty( $nodes ) ) { 396 | foreach ( $nodes as $key => $node ) { 397 | 398 | $node_id = $node->id ?? false; 399 | 400 | if ( ! empty( $node_id ) && self::in_array_r( $node_id, $removed_nodes ) ) { 401 | $wp_admin_bar->remove_node( $node_id ); 402 | } 403 | } 404 | } 405 | } 406 | }); 407 | } 408 | 409 | /** 410 | * Recursive in array function. 411 | * 412 | * @param string $needle String value to be fetched. 413 | * @param array $haystack Multidimensional array. 414 | * 415 | * @return boolean If value was found. 416 | */ 417 | public static function in_array_r( $needle, $haystack ) { 418 | 419 | foreach ( $haystack as $item ) { 420 | if ( ( $item == $needle ) || ( is_array( $item ) && self::in_array_r( $needle, $item ) ) ) { 421 | return true; 422 | } 423 | } 424 | 425 | return false; 426 | } 427 | 428 | /** 429 | * Add a user to the Super admin user list in WordPress Multisite. 430 | * 431 | * @param int $user_id User ID. 432 | * @return void 433 | */ 434 | public static function grant_super_admin_cap( $user_id ) { 435 | grant_super_admin( $user_id ); 436 | } 437 | 438 | /** 439 | * Adds WP Ga options settings. 440 | * 441 | * @return void 442 | */ 443 | public static function add_options_page() { 444 | 445 | if ( is_admin() ) { 446 | 447 | // Run in admin_menu hook when called outside class 448 | add_action( 'admin_menu', function() { 449 | 450 | $menu_page_capability = apply_filters( 'geniem/roles/add_menu_page_cap', 'activate_plugins' ); 451 | 452 | \add_menu_page( 453 | __( 'Geniem Roles', 'wp-geniem-roles' ), // page title 454 | __( 'Geniem Roles', 'wp-geniem-roles' ), // menu title 455 | $menu_page_capability, 456 | 'wp-geniem-roles', // menu slug 457 | array( __CLASS__, 'geniem_roles_html' ), // render function 458 | 'dashicons-universal-access', 459 | 80 460 | ); 461 | 462 | \add_submenu_page( 463 | 'wp-geniem-roles', // parent menu slug 464 | __( 'Geniem Roles: Menu slugs', 'wp-geniem-roles' ), // page title 465 | __( 'Menu slugs', 'wp-geniem-roles' ), // menu title 466 | $menu_page_capability, 467 | 'wp-geniem-roles-slugs', // menu slug 468 | array( __CLASS__, 'geniem_roles_slug_html' ) // render function 469 | ); 470 | }); 471 | } 472 | } 473 | 474 | /** 475 | * Geniem roles printable html. 476 | * 477 | * @return void 478 | */ 479 | public static function geniem_roles_html() { 480 | 481 | global $wp_roles; 482 | 483 | echo '
'; 484 | echo '

' . esc_html__( 'Geniem roles', 'geniem-roles' ) . '

'; 485 | echo '

' . esc_html__( 'This page lists all current roles and their enabled capabilities.', 'geniem-roles' ) . '

'; 486 | 487 | // Do not list cap if in $legacy_caps 488 | $legacy_caps = [ 489 | 'level_10', 490 | 'level_9', 491 | 'level_8', 492 | 'level_7', 493 | 'level_6', 494 | 'level_5', 495 | 'level_4', 496 | 'level_3', 497 | 'level_2', 498 | 'level_1', 499 | 'level_0', 500 | ]; 501 | 502 | if ( ! empty( $wp_roles->roles ) ) { 503 | 504 | echo '
'; 505 | 506 | // Roles 507 | foreach ( $wp_roles->roles as $roles_slug => $role ) { 508 | 509 | // Single role wrap 510 | echo '
'; 511 | 512 | // Name 513 | echo '

' . esc_html( $role['name'] ) . '

'; 514 | 515 | // Caps 516 | echo '
    '; 517 | if ( ! empty( $role['capabilities'] ) ) { 518 | foreach ( $role['capabilities'] as $key => $value ) { 519 | 520 | $formated_cap = \str_replace( '_', ' ', $key ); 521 | 522 | if ( ! in_array( $key, $legacy_caps ) && $value !== false ) { 523 | echo '
  • ' . esc_html( $formated_cap ) . '
  • '; 524 | } 525 | } 526 | } 527 | echo '
'; 528 | echo '
'; // geniem-roles__single-role 529 | 530 | } // foreach ends 531 | } 532 | echo '
'; 533 | echo '
'; // wrapper ends 534 | } 535 | 536 | /** 537 | * Geniem roles menu items slug list. 538 | * 539 | * @return void 540 | */ 541 | public static function geniem_roles_slug_html() { 542 | 543 | $menu_list = self::get_menu_list(); 544 | 545 | echo '
'; 546 | echo '

' . esc_html__( 'Geniem roles', 'geniem-roles' ) . '

'; 547 | echo '

' . esc_html__( 'This page lists all admin menu slugs.', 'geniem-roles' ) . '

'; 548 | echo '
'; 549 | echo '
'; 550 | echo ''; 551 | 552 | foreach ( $menu_list as $menu ) { 553 | echo ''; 554 | echo ''; 555 | echo ''; 556 | echo ''; 557 | 558 | foreach ( $menu->children as $child_menu ) { 559 | echo ''; 560 | echo ''; 561 | echo ''; 562 | echo ''; 563 | } 564 | } 565 | echo '
' . esc_html( $menu->label ) . '' . esc_html( $menu->path ) . '
' . esc_html( $child_menu->label ) . '' . esc_html( $child_menu->path ) . '
'; 566 | 567 | echo '
'; 568 | echo '
'; 569 | echo '
'; 570 | } 571 | 572 | /** 573 | * Get menu list. 574 | * 575 | * @return array An array of admin menu items. 576 | */ 577 | public static function get_menu_list() { 578 | global $menu, $submenu; 579 | 580 | $menu_list = []; 581 | foreach ( $menu as $i => $menu_data ) { 582 | if ( $menu_data[0] ) { 583 | $parent_menu = (object) [ 584 | 'label' => strip_tags( $menu_data[0] ), 585 | 'path' => $menu_data[2], 586 | 'children' => [], 587 | ]; 588 | 589 | $menu_list[ $parent_menu->path ] = $parent_menu; 590 | } 591 | } 592 | foreach ( $submenu as $i => $menu_data ) { 593 | if ( array_key_exists( $i, $menu_list ) ) { 594 | $sub_menus = array_map( function( $menu ) { 595 | $item = (object) [ 596 | 'label' => strip_tags( $menu[0] ), 597 | 'path' => $menu[2], 598 | ]; 599 | 600 | return $item; 601 | 602 | }, $menu_data); 603 | $menu_list[ $i ]->children = $sub_menus; 604 | } 605 | } 606 | 607 | return $menu_list; 608 | } 609 | 610 | /** 611 | * Add function to map_meta_cap which disallows certain actions for role in specifed posts. 612 | * 613 | * @param string $name WP Role name. 614 | * @param array $blocked_posts Blocked posts. 615 | * @param string|array $capabilities Capabilities which is disallowed for the user. 616 | * @return void 617 | */ 618 | public static function restrict_post_edit( $name, $blocked_posts, $capabilities ) { 619 | 620 | // Fail fast. 621 | if ( empty( $capabilities ) ) { 622 | return; 623 | } 624 | 625 | $current_user = wp_get_current_user(); 626 | 627 | // Add function to map_meta_cap which disallows certain actions for role in specifed posts. 628 | // Check if we need to restrict current user. 629 | if ( in_array( $name, $current_user->roles, true ) ) { 630 | 631 | /** 632 | * Map_meta_cap arguments. 633 | * 634 | * $caps (array) Returns the user's actual capabilities. 635 | * $cap (string) Capabilities name. 636 | * $user_id (int) The user ID. 637 | * $args (array) Adds the context to the cap. Typically the object ID. 638 | */ 639 | \add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) use ( $blocked_posts, $name, $capabilities ) { 640 | 641 | if ( is_array( $capabilities ) ) { 642 | 643 | // $args[0] is the post id. 644 | if ( in_array( $cap, $capabilities ) && in_array( $args[0], $blocked_posts, true ) ) { 645 | // This is default Wordpress way to restrict access. 646 | $caps[] = 'do_not_allow'; 647 | } 648 | } 649 | else { 650 | 651 | // $args[0] is the post id. 652 | if ( $cap === $capabilities && in_array( $args[0], $blocked_posts, true ) ) { 653 | // This is default Wordpress way to restrict access. 654 | $caps[] = 'do_not_allow'; 655 | } 656 | } 657 | 658 | return $caps; 659 | }, 10, 4 ); 660 | } 661 | } 662 | 663 | /** 664 | * Grant rights for the role by the given post ids and capabilities. 665 | * 666 | * @param string $name WP Role name. 667 | * @param array $blocked_posts Blocked posts. 668 | * @param array $granted_posts_caps Capabilities which is allowed for the user. 669 | * @param array $restricted_posts_caps (optional) Capabilities which is allowed for the restricted posts. 670 | * @param array $post_types (optional) Post types to restrict. If defined other post types won't be handled. 671 | * @return void|false On fail returns false. 672 | */ 673 | public static function grant_post_edit( $name, $granted_posts, $granted_posts_caps, $restricted_posts_caps, $post_types ) { 674 | 675 | // Fail fast. 676 | if ( empty( $granted_posts_caps ) || ! is_array( $granted_posts_caps ) ) { 677 | return false; 678 | } 679 | 680 | $current_user = wp_get_current_user(); 681 | 682 | // Add function to map_meta_cap which disallows certain actions for role in specifed posts. 683 | // Check if we need to restrict current user. 684 | if ( in_array( $name, $current_user->roles, true ) ) { 685 | 686 | /** 687 | * Map_meta_cap arguments. 688 | * 689 | * $caps (array) Returns the user's actual capabilities. 690 | * $cap (string) Capabilities name. 691 | * $user_id (int) The user ID. 692 | * $args (array) Adds the context to the cap. Typically the object ID. 693 | */ 694 | \add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) use ( $granted_posts, $name, $granted_posts_caps, $restricted_posts_caps, $post_types ) { 695 | 696 | // If we are not handling a post type capability. 697 | if ( ! in_array( $cap, self::get_all_post_type_caps() ) ) { 698 | return $caps; 699 | } 700 | 701 | // Note $args[0] is empty on post list at first. 702 | if ( empty( $args[0] ) ) { 703 | return $caps; 704 | } 705 | 706 | // Check if the $post_types parameter has been defined. 707 | if ( ! empty( $post_types ) ) { 708 | 709 | $current_post_type = \get_post( $args[0] )->post_type ?? ''; 710 | 711 | // If we are not handling a post with desired post type skip the handling. 712 | if ( ! in_array( $current_post_type, $post_types, true ) ) { 713 | 714 | return $caps; 715 | } 716 | } 717 | 718 | // Restricted posts 719 | // If post is not in the granted_posts restrict it. 720 | if ( ! in_array( $args[0], $granted_posts, true ) ) { 721 | 722 | // If restricted posts has some caps the need to match the current cap. 723 | if ( ! empty( $restricted_posts_caps ) ) { 724 | if ( ! in_array( $cap, $restricted_posts_caps ) ) { 725 | $caps[] = 'do_not_allow'; 726 | } 727 | } 728 | // If not given specific caps then we need to restrict all. 729 | else { 730 | $caps[] = 'do_not_allow'; 731 | } 732 | } 733 | // Granted posts 734 | // If cap not matching -> do_not_allow. 735 | else { 736 | 737 | if ( ! in_array( $cap, $granted_posts_caps ) ) { 738 | $caps[] = 'do_not_allow'; 739 | } 740 | } 741 | 742 | return $caps; 743 | }, 10, 4 ); 744 | } 745 | } 746 | 747 | /** 748 | * Get all post type caps. 749 | * 750 | * @return array Array of post type capabilities. 751 | */ 752 | private static function get_all_post_type_caps() { 753 | 754 | global $wp_post_types; 755 | 756 | $all_caps = []; 757 | 758 | if ( empty( $wp_post_types ) ) { 759 | return []; 760 | } 761 | 762 | foreach ( $wp_post_types as $post_type ) { 763 | 764 | $post_type_caps = array_values( (array) $post_type->cap ) ?? []; 765 | $all_caps = array_merge( $all_caps, $post_type_caps ); 766 | } 767 | 768 | return array_unique( $all_caps ); 769 | } 770 | 771 | /** 772 | * Prevents user to create and manage users by the given user roles and capabilities. 773 | * Removes role from the admin side dropdowns if 774 | * 'edit_user' or 'promote_user' has been restricted. 775 | * 776 | * Example usage: 777 | * 'administrator' => [ 778 | * 'add_user', 779 | * 'edit_user', 780 | * 'delete_user', 781 | * 'remove_user', 782 | * ], 783 | * 784 | * @param string $name Name of the role. 785 | * @param array $removed_user_caps_by_role Associative array of role specific restricted caps. 786 | * @return void 787 | */ 788 | public static function restrict_user_management_by_role( $name, $removed_user_caps_by_role ) { 789 | 790 | // Get current user. 791 | $current_user = wp_get_current_user(); 792 | $current_user_roles = $current_user->roles; 793 | 794 | // Remove restricted role from the role lists in the admin side. 795 | \add_filter( 'editable_roles', function ( $roles ) use ( $name, $removed_user_caps_by_role, $current_user_roles ) { 796 | 797 | // If current users role is the smae as the edited one. 798 | if ( in_array( $name, $current_user_roles, true ) ) { 799 | 800 | // Loop through restricted user roles. 801 | foreach ( $removed_user_caps_by_role as $role => $restricted_caps ) { 802 | 803 | $edit_user_caps = [ 804 | 'add_user', 805 | 'edit_user', 806 | 'promote_user', 807 | ]; 808 | 809 | // Loop through restricted caps. 810 | foreach ( $restricted_caps as $cap ) { 811 | if ( in_array( $cap, $edit_user_caps ) && isset( $roles[ $role ] ) ) { 812 | // Unset the role from editable roles. 813 | unset( $roles[ $role ] ); 814 | } 815 | } 816 | } // End foreach(). 817 | } // End if(). 818 | 819 | return $roles; 820 | 821 | }); // End filter editable_roles. 822 | 823 | // If current users role is the same as the edited one. 824 | if ( in_array( $name, $current_user_roles, true ) ) { 825 | 826 | // Restrict user to manage users with given $removed_user_caps_by_role. 827 | \add_filter( 'map_meta_cap', function ( $caps, $cap, $user_id, $args ) use ( $name, $removed_user_caps_by_role ) { 828 | 829 | // Loop through the roles and their caps. 830 | foreach ( $removed_user_caps_by_role as $role => $restricted_caps ) { 831 | 832 | // Check that array of caps have been assigned to the role. 833 | if ( ! empty( $restricted_caps ) && is_array( $restricted_caps ) ) { 834 | 835 | // Loop through roles restricted capabilities. 836 | foreach ( $restricted_caps as $restricted_cap ) { 837 | 838 | // Map meta cap switch case for user capabilities. 839 | if ( $cap === $restricted_cap ) { 840 | 841 | // Currently edited user. 842 | $edited_user = new \WP_User( absint( $args[0] ) ); 843 | 844 | if ( in_array( $role, $edited_user->roles ) ) { 845 | $caps[] = 'do_not_allow'; 846 | } 847 | } // End if(). 848 | } // End foreach(). 849 | } // End if(). 850 | } // End foreach(). 851 | 852 | return $caps; 853 | 854 | }, 10, 4 ); // End map_meta_cap. 855 | } // End if(). 856 | } 857 | 858 | /** 859 | * Helper function reset default WordPress roles. 860 | * 861 | * @return void 862 | */ 863 | public static function reset_to_default_roles() { 864 | require_once( ABSPATH . 'wp-admin/includes/schema.php' ); 865 | \populate_roles(); 866 | } 867 | 868 | /** 869 | * Reset roles from the database. 870 | * Run this before your role changes on your theme. 871 | * 872 | * @return void 873 | */ 874 | public static function reset_roles() { 875 | 876 | global $wp_roles; 877 | 878 | // Check if multisite and roles reset is requested from CLI 879 | // Reset roles on all sites 880 | if ( \is_multisite() && defined( 'WP_CLI' ) && WP_CLI ) { 881 | // Get the sites. Default amount is 100 and there is no flag to list all sites so we have to use 882 | // high number here. Maybe would be better to use custom SQL query later if there is going to be 883 | // huge amounts of sites. 884 | $sites_args = [ 885 | 'number' => 1000, 886 | ]; 887 | $sites = \get_sites( $sites_args ); 888 | foreach ( $sites as $site ) { 889 | \set_time_limit( 30 ); 890 | \switch_to_blog( $site->blog_id ); 891 | 892 | foreach ( $wp_roles->roles as $role_name => $role ) { 893 | \remove_role( $role_name ); 894 | } 895 | 896 | // Create and define WordPress default roles. 897 | Roles::reset_to_default_roles(); 898 | } 899 | \restore_current_blog(); 900 | 901 | } 902 | else { 903 | // Single site 904 | foreach ( $wp_roles->roles as $role_name => $role ) { 905 | \remove_role( $role_name ); 906 | } 907 | 908 | // Create and define WordPress default roles. 909 | Roles::reset_to_default_roles(); 910 | } 911 | } 912 | 913 | /** 914 | * Restrict role templates. 915 | * If called restricts other templates than added templates from the given role. 916 | * Note!: WordPress doesn't handle default template the same way as other templates. 917 | * Default template will always be available for the users. If you want to enable only default template add string 'default' as a parameter. 918 | * 919 | * @param string $name Role name. 920 | * @param array|string $allowed_templates Array of templates to be added for the role. 'default' If we want to enable only the default template for the role. 921 | * @return void|false False on failure. No return if success. 922 | */ 923 | public static function restrict_role_templates( $name, $allowed_templates ) { 924 | 925 | // Fail fast. 926 | if ( empty( $allowed_templates ) ) { 927 | return false; 928 | } 929 | 930 | $current_user = wp_get_current_user(); 931 | 932 | // Check if we need to restrict current user. 933 | if ( in_array( $name, $current_user->roles, true ) ) { 934 | 935 | /** 936 | * Filters list of page templates for a theme. 937 | * 938 | * @since 4.9.6 939 | * 940 | * @param string[] $post_templates Array of page templates. Keys are filenames, 941 | * values are translated names. 942 | */ 943 | add_filter( 'theme_templates', function( $post_templates ) use ( $allowed_templates ) { 944 | 945 | // If we want to add multiple templates for the role. 946 | if ( is_array( $allowed_templates ) ) { 947 | foreach ( $post_templates as $template_file => &$template_name ) { 948 | 949 | // If the template isn't in the allowed_templates remove it from the current user. 950 | if ( ! in_array( $template_file, $allowed_templates ) ) { 951 | 952 | unset( $post_templates[ $template_file ] ); 953 | } 954 | } 955 | } 956 | else { 957 | 958 | // WordPress doesn't handle default template the same way as other templates. 959 | // Default template will always be available for the users. 960 | // Check if we wan't to enable only default template. 961 | if ( empty( $post_templates === 'default' ) ) { 962 | 963 | // Set post templates empty here. 964 | $post_templates = []; 965 | } 966 | } 967 | 968 | return $post_templates; 969 | }); 970 | } 971 | } 972 | } 973 | 974 | /** 975 | * Class Role which handles a intance of a single editable role 976 | */ 977 | class Role { 978 | 979 | /** 980 | * Role name for role identification. 981 | * 982 | * @var string Role name. 983 | */ 984 | public $name; 985 | 986 | /** 987 | * Role display name shown for the admin user. 988 | * 989 | * @var string Role display name. 990 | */ 991 | public $display_name; 992 | 993 | /** 994 | * Role capabilities. 995 | * 996 | * @var array 997 | */ 998 | public $capabilities; 999 | 1000 | /** 1001 | * Get default caps for roles 1002 | * 1003 | * @return array Default capabilities. 1004 | */ 1005 | public static function get_default_caps() { 1006 | 1007 | // Default to empty list. 1008 | $defaults = []; 1009 | 1010 | // filter default caps 1011 | return \apply_filters( 'geniem/roles/default_roles', $defaults ); 1012 | } 1013 | 1014 | /** 1015 | * Role constructor. 1016 | * 1017 | * @param string $name Role name. 1018 | * @param string $display_name Display name for the role. 1019 | */ 1020 | public function __construct( $name, $display_name ) { 1021 | 1022 | // Get WordPress role. 1023 | $role = \get_role( $name ); 1024 | 1025 | // Get role 1026 | if ( $role ) { 1027 | // Set role properties.. 1028 | $this->capabilities = $role->capabilities; 1029 | $this->name = $role->name; 1030 | $this->display_name = $display_name; 1031 | 1032 | // Create a new role and set role properties. 1033 | } else { 1034 | $this->capabilities = self::get_default_caps(); 1035 | $this->name = $name; 1036 | $this->display_name = $display_name; 1037 | } 1038 | } 1039 | 1040 | /** 1041 | * Remove a role. 1042 | * 1043 | * @return void 1044 | */ 1045 | public function remove() { 1046 | Roles::remove_role( $this->name ); 1047 | } 1048 | 1049 | /** 1050 | * Remove menu pages. 1051 | * 1052 | * @param array $menu_pages Mixed array of removable admin menu items. 1053 | * Array value can be a string or 1054 | * assoaciative array item 'parent_slug' => [ 'submenu_item1_slug', 'submenu_item2_slug' ]. 1055 | * @return void 1056 | */ 1057 | public function remove_menu_pages( $menu_pages ) { 1058 | Roles::remove_menu_pages( $this->name, $menu_pages ); 1059 | } 1060 | 1061 | /** 1062 | * Remove admin bar nodes. 1063 | * 1064 | * @param array $nodes An array of removable nodes. 1065 | * @return void 1066 | */ 1067 | public function remove_admin_bar_nodes( $nodes ) { 1068 | Roles::remove_admin_bar_nodes( $this->name, $nodes ); 1069 | } 1070 | 1071 | /** 1072 | * Add capabilities for a role 1073 | * Makes db changes do not run everytime. 1074 | * 1075 | * @param array $caps An array of capabilities. 1076 | * @return void 1077 | */ 1078 | public function add_caps( $caps ) { 1079 | Roles::add_caps( $this->name, $caps ); 1080 | } 1081 | 1082 | /** 1083 | * Remove capabilities for a role 1084 | * Makes db changes do not run everytime. 1085 | * 1086 | * @param array $caps An array of capabilities to be removed. 1087 | */ 1088 | public function remove_caps( $caps ) { 1089 | Roles::remove_caps( $this->name, $caps ); 1090 | } 1091 | 1092 | /** 1093 | * Get all caps from a role. 1094 | * 1095 | * @return array Role capabilities. 1096 | */ 1097 | public function get_caps() { 1098 | return \get_role( $this->name )->capabilities; 1099 | } 1100 | 1101 | /** 1102 | * Rename a role. 1103 | * 1104 | * @param string $new_display_name Display name for a role. 1105 | * @return void 1106 | */ 1107 | public function rename( $new_display_name ) { 1108 | return Roles::rename( $this->name, $new_display_name ); 1109 | } 1110 | 1111 | /** 1112 | * Restrict post editing capabilities by post ids. 1113 | * 1114 | * @param array $blocked_posts An array of blocked post ids. 1115 | * @param string|array $capability Capability to restrict for the role. 1116 | * @return void 1117 | */ 1118 | public function restrict_post_edit( $blocked_posts, $capability ) { 1119 | return Roles::restrict_post_edit( $this->name, $blocked_posts, $capability ); 1120 | } 1121 | 1122 | /** 1123 | * Grant post editing capabilities by post ids. 1124 | * 1125 | * @param array $granted_posts An array of blocked post ids. 1126 | * @param array $granted_posts_caps Capability to allow for the role. 1127 | * @param array $restricted_posts_caps (optional) Capabilities which is allowed for the restricted posts. 1128 | * @param array $post_types (optional) Post types to restrict. If defined other post types won't be handled. 1129 | * @return void 1130 | */ 1131 | public function grant_post_edit( $granted_posts, $granted_posts_caps, $restricted_posts_caps = [], $post_types = [] ) { 1132 | return Roles::grant_post_edit( $this->name, $granted_posts, $granted_posts_caps, $restricted_posts_caps, $post_types ); 1133 | } 1134 | 1135 | /** 1136 | * Restrict user management capabilities for the given role object. 1137 | * 1138 | * Example usage: 1139 | * 'administrator' => [ 1140 | * 'add_user', 1141 | * 'edit_user', 1142 | * 'delete_user', 1143 | * 'remove_user', 1144 | * ], 1145 | * 1146 | * @param array $removed_user_caps_by_role Associative array of role specific restricted caps. 1147 | * @return void 1148 | */ 1149 | public function restrict_user_management_by_role( $removed_user_caps_by_role ) { 1150 | return Roles::restrict_user_management_by_role( $this->name, $removed_user_caps_by_role ); 1151 | } 1152 | 1153 | /** 1154 | * Restrict role templates. 1155 | * 1156 | * @param array|string $allowed_templates Array of templates to be added for the role. 'default' If we want to enable only the default template for the role. 1157 | * @return void|false False on failure. No return if success. 1158 | */ 1159 | public function restrict_role_templates( $allowed_templates ) { 1160 | return Roles::restrict_role_templates( $this->name, $allowed_templates ); 1161 | } 1162 | } 1163 | 1164 | /** 1165 | * Returns the Geniem Roles singleton. 1166 | * 1167 | * @return object 1168 | */ 1169 | function roles() { 1170 | return Roles::instance(); 1171 | } 1172 | 1173 | // Create Geniem role singleton. 1174 | roles(); 1175 | --------------------------------------------------------------------------------