PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.6.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.6.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / Roles.php

Roles.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.6.0, at includes/Core/Roles.php

183 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Core;
4
5 use WPDeveloper\BetterDocs\Utils\Base;
6 use WPDeveloper\BetterDocs\Utils\Database;
7
8 class Roles extends Base {
9 /**
10 * Summary of Database
11 * @var Database
12 */
13 public $database;
14
15 public function __construct( Database $database ) {
16 $this->database = $database;
17
18 $this->assign_admin_capabilities(); //will run when it is called
19 }
20
21 /**
22 * Ensure the administrator role always has every BetterDocs capability.
23 *
24 * Capabilities are normally granted on activation via Install::activate() ->
25 * setup(). But when BetterDocs is installed/activated silently by another
26 * plugin (e.g. Essential Addons from its Integrations screen, or BetterDocs
27 * Pro), the activation hook does not fire, so setup() never runs and the
28 * administrator never receives caps like edit_docs, edit_docs_settings or
29 * read_docs_analytics. That hides most of the BetterDocs admin menu (leaving
30 * only Quick Setup, gated by the core `delete_users` cap, and FAQ Builder)
31 * and makes the pages return "Sorry, you are not allowed to access this page."
32 *
33 * This self-heals that on every request: any missing admin cap is added back.
34 * add_cap() is only called for caps the role lacks, so once healed there are
35 * no further DB writes.
36 *
37 * @return void
38 */
39 public function assign_admin_capabilities() {
40 if ( ! current_user_can( 'administrator' ) ) {
41 return;
42 }
43
44 $capabilities = $this->defaults_capabilities();
45 $admin_caps = isset( $capabilities['administrator'] ) ? $capabilities['administrator'] : [];
46
47 if ( empty( $admin_caps ) ) {
48 return;
49 }
50
51 $administrator = get_role( 'administrator' );
52 if ( ! $administrator ) {
53 return;
54 }
55
56 foreach ( $admin_caps as $cap ) {
57 if ( ! $administrator->has_cap( $cap ) ) {
58 $administrator->add_cap( $cap );
59 }
60 }
61 }
62
63 /**
64 * Assign FAQ Builder Capability To The Admin
65 *
66 * @deprecated Use assign_admin_capabilities() which grants the full admin
67 * capability set (including read_faq_builder). Kept for backward
68 * compatibility with any external callers.
69 */
70 public function assgin_faq_builder_capability_to_admin() {
71 if( current_user_can('administrator') && ! current_user_can('read_faq_builder') ) { // if the current user is admin, and current user does not have faq menu visibility option, then assign the faq menu visibility capability
72 $current_user_role = get_role('administrator');
73 $current_user_role->add_cap('read_faq_builder');
74 }
75 }
76
77 /**
78 * Default Roles Capabilities
79 *
80 * @var array
81 */
82 public function defaults_capabilities() {
83 $default_capabilities = [
84 'administrator' => [
85 // post type related caps
86 'edit_docs',
87 'edit_others_docs',
88 'edit_private_docs',
89 'edit_published_docs',
90 'read_private_docs',
91 'publish_docs',
92 'delete_docs',
93 'delete_private_docs',
94 'delete_published_docs',
95 'delete_others_docs',
96
97 // doc_terms related caps
98 'manage_doc_terms',
99 'edit_doc_terms',
100 'delete_doc_terms',
101
102 // kb terms related caps
103 'manage_knowledge_base_terms',
104 'edit_knowledge_base_terms',
105 'delete_knowledge_base_terms',
106
107 // Settings and Analytics Related caps
108 'edit_docs_settings',
109 'read_docs_analytics',
110 'read_faq_builder'
111 ],
112 'editor' => [
113 // post type related caps
114 'edit_docs',
115 'edit_others_docs',
116 'edit_private_docs',
117 'edit_published_docs',
118 'read_private_docs',
119 'publish_docs',
120 'delete_docs',
121 'delete_private_docs',
122 'delete_published_docs',
123 'delete_others_docs',
124
125 // doc_terms related caps
126 'manage_doc_terms',
127 'edit_doc_terms',
128 'delete_doc_terms',
129
130 // kb terms related caps
131 'manage_knowledge_base_terms',
132 'edit_knowledge_base_terms',
133 'delete_knowledge_base_terms'
134 ],
135 'author' => [
136 'edit_docs',
137 'edit_published_docs',
138 'publish_docs',
139 'delete_docs',
140 'delete_published_docs'
141 ],
142 'contributor' => [
143 'edit_docs',
144 'delete_docs'
145 ],
146 'other' => [
147 // post type related caps
148 'edit_docs',
149 'delete_docs'
150 ]
151 ];
152
153 return apply_filters( 'betterdocs_default_caps', $default_capabilities );
154 }
155
156 public function setup( $remove = false ) {
157 if ( $this->database->get( '_betterdocs_caps_initialized', false ) && ! $remove ) {
158 return;
159 }
160
161 global $wp_roles;
162
163 $capabilities = $this->defaults_capabilities();
164
165 if ( $remove ) {
166 unset( $capabilities['administrator'] );
167 }
168
169 foreach ( $capabilities as $role => $caps ) {
170 foreach ( $caps as $cap ) {
171 if ( $remove ) {
172 $wp_roles->remove_cap( $role, $cap );
173 continue;
174 }
175
176 $wp_roles->add_cap( $role, $cap );
177 }
178 }
179
180 $this->database->save( '_betterdocs_caps_initialized', ! $remove );
181 }
182 }
183