PluginProbe
Themify Builder / trunk
Themify Builder vtrunk
7.8.1 7.8.0 7.7.9 7.7.8 7.7.7 7.7.6 7.7.5 7.7.4 7.7.3 7.7.2 trunk 7.6.0 7.6.1 7.6.2 7.6.3 7.6.4 7.6.5 7.6.6 7.6.7 7.6.8 7.6.9 7.7.0 7.7.1
themify-builder / themify / class-themify-access-role.php

class-themify-access-role.php in Themify Builder trunk, at themify/class-themify-access-role.php

225 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! class_exists( 'Themify_Access_Role',false ) ) :
4 class Themify_Access_Role {
5
6 public static function init(){
7 add_filter( 'themify_theme_config_setup', array( __CLASS__, 'config_setup' ), 14 );
8 add_filter( 'admin_init', array( __CLASS__, 'hide_customizer' ), 99 );
9 add_filter( 'themify_metabox/fields/themify-meta-boxes', array( __CLASS__, 'hide_custom_panel_and_backend_builder' ), 999 );
10 }
11
12 /**
13 * Renders the options for role access control
14 *
15 * @param array $data
16 * @return string
17 */
18 public static function config_view( $data = array() ){
19 global $wp_roles;
20 $roles = $wp_roles->get_names();
21 // Remove the adminitrator and subscriber user role from the array
22 unset( $roles['administrator'],$roles['subscriber'] );
23
24 // Get the unique setting name
25 $setting = $data['attr']['setting'];
26
27 // Generate prefix with the setting name
28 $prefix = 'setting-'.$setting.'-';
29
30 $show_owned_option = in_array( $setting, [ 'tbp', 'frontend', 'backend' ], true );
31
32 ob_start();
33 if ( 'custom_panel' === $setting ) :
34 ?>
35 <div class="themify-info-link"><?php _e( 'Role access allow certain user roles to have access to the tool. Only set disable if you want to disallow the tool to certain user(s), otherwise keep everything as default.', 'themify' ); ?></div>
36 <?php
37 endif;
38 ?>
39 <ul>
40 <?php foreach( $roles as $role => $slug ) {
41 $prefix_role = esc_attr($prefix.$role);
42 // Get value from the database
43 $value = themify_builder_get( $prefix_role,$prefix_role);
44
45 // Check if the user has not saved any setting till now, if so, set the 'default' as value
46 $value = ( null !== $value ) ? $value : 'default';
47
48 ?>
49 <li class="role-access-controller">
50 <!-- Set the column title -->
51 <div class="role-title">
52 <?php echo $slug; ?>
53 </div>
54
55 <!-- Set option to default -->
56 <div class="role-option role-default">
57 <input type="radio" id="default-<?php echo $prefix_role; ?>" name="<?php echo $prefix_role; ?>" value="default" <?php echo checked( $value, 'default', false ); ?>/>
58 <label for="default-<?php echo $prefix_role; ?>"><?php _e( 'Default', 'themify' ); ?></label>
59 </div>
60
61 <!-- Set option to enable -->
62 <div class="role-option role-enable">
63 <input type="radio" id="enable-<?php echo $prefix_role; ?>" name="<?php echo $prefix_role; ?>" value="enable" <?php echo checked( $value, 'enable', false ); ?>/>
64 <label for="enable-<?php echo $prefix_role; ?>"><?php _e( 'Enable', 'themify' ); ?></label>
65 </div>
66
67 <?php if ( $show_owned_option ) : ?>
68 <div class="role-option role-enableown">
69 <input type="radio" id="enableown-<?php echo $prefix_role; ?>" name="<?php echo $prefix_role; ?>" value="enableown" <?php echo checked( $value, 'enableown', false ); ?>/>
70 <label for="enableown-<?php echo $prefix_role; ?>"><?php _e( 'Enable For Owned Posts', 'themify' ); ?></label>
71 </div>
72 <?php endif; ?>
73
74 <!-- Set option to disable -->
75 <div class="role-option role-disable">
76 <input type="radio" id="disable-<?php echo $prefix_role; ?>" name="<?php echo $prefix_role; ?>" value="disable" <?php echo checked( $value, 'disable', false ); ?>/>
77 <label for="disable-<?php echo $prefix_role; ?>"><?php _e( 'Disable', 'themify' ); ?></label>
78 </div>
79 </li>
80 <?php }//end foreach ?>
81 </ul>
82 <?php
83 return ob_get_clean();
84 }
85
86 /**
87 * Role Access Control
88 * @param array $themify_theme_config
89 * @return array
90 */
91 public static function config_setup(array $themify_theme_config ):array {
92 // Add role acceess control tab on settings page
93 $themify_theme_config['panel']['settings']['tab']['role_access'] = array(
94 'title' => __('Role Access', 'themify'),
95 'id' => 'role_access',
96 'custom-module' => array(
97 array(
98 'title' => __('Themify Custom Panel (In Post/Page Edit)', 'themify'),
99 'function' => array( __CLASS__, 'config_view' ),
100 'setting' => 'custom_panel'
101 ),
102 array(
103 'title' => __('Customizer', 'themify'),
104 'function' => array( __CLASS__, 'config_view' ),
105 'setting' => 'customizer'
106 ),
107 array(
108 'title' => __('Builder Backend', 'themify'),
109 'function' => array( __CLASS__, 'config_view' ),
110 'setting' => 'backend'
111 ),
112 array(
113 'title' => __('Builder Frontend', 'themify'),
114 'function' => array( __CLASS__, 'config_view' ),
115 'setting' => 'frontend'
116 )
117 )
118 );
119
120 return $themify_theme_config;
121 }
122
123 // Hide Themify Custom Panel and Backend Builder
124 public static function hide_custom_panel_and_backend_builder(array $meta ):array {
125 if( is_user_logged_in() ){
126 $custom_panel = self::check_role_access('custom_panel');
127 $backend_builder = self::check_access_backend();
128 // Remove Page Builde if disabled from role access control
129 if( !$backend_builder || 'disable' === $custom_panel ){
130 // Check each meta box for panels
131 foreach( $meta as $key => $panel ) {
132 // if page builder id found in meta boxes, unset it
133 // Remove Custom Panel if disabled from role access control
134 if ( (!$backend_builder && 'page-builder' === $panel['id'] ) ||('disable' === $custom_panel && 'page-builder' !== $panel['id'])) {
135 unset( $meta[ $key ] );
136 }
137 }
138 }
139 }
140 return $meta;
141 }
142
143 /**
144 * Check if user has access to builder's backend editor
145 */
146 public static function check_access_backend(?int $post_id = null ):bool {
147 $has_access = is_user_logged_in() && ((empty($post_id) && current_user_can( 'edit_posts' )) || ($post_id>0 && current_user_can( 'edit_post', $post_id )));
148 if ( $has_access === true ) {
149 $has_access = 'disable' !== self::check_role_access( 'backend' );
150
151 /* check access to specific $post_id */
152 if ( $has_access === true && $post_id > 0 ) {
153 $has_access === current_user_can( 'edit_post', $post_id );
154 if ( $has_access === true && 'enableown' === self::check_role_access( 'backend' ) && ! self::is_current_user_the_author( $post_id ) ) {
155 $has_access = false;
156 }
157 }
158 }
159
160 return $has_access;
161 }
162
163 /**
164 * Check if user has access to builder's frontend editor
165 */
166 public static function check_access_frontend(?int $post_id = null):bool {
167 $has_access = is_user_logged_in() && ((empty($post_id) && current_user_can( 'edit_posts' )) || ($post_id>0 && current_user_can( 'edit_post', $post_id )));
168 if ( $has_access === true ) {
169 if ( 'enableown' === self::check_role_access( 'frontend' ) ) {
170 $has_access = self::is_current_user_the_author( $post_id );
171 }
172 else{
173 $has_access = 'disable' !== self::check_role_access( 'frontend' );
174 }
175 }
176
177 return $has_access;
178 }
179
180 private static function get_current_role(){
181 static $user = null;
182 if( $user === null ) {
183 $user = wp_get_current_user();
184 $roles = ! empty( $user->roles ) && is_array( $user->roles ) ? $user->roles : array();
185 // Get first role ( don't use key )
186 $user = array_shift( $roles );
187 }
188 return $user;
189 }
190
191 // Hide Themify Builder Customizer
192 public static function hide_customizer( $data ) {
193 if( is_user_logged_in() ){
194 $is_available = current_user_can('customize');
195 $value = self::check_role_access('customizer');
196 // get the the role object
197 $editor = get_role(self::get_current_role());
198 if ( 'enable' === $value && !$is_available) {
199 // add $cap capability to this role object
200 $editor->add_cap('edit_theme_options');
201 } elseif( 'disable' === $value && $is_available) {
202 $editor->remove_cap('edit_theme_options');
203 }
204 }
205
206 return $data;
207 }
208
209 public static function check_role_access(string $key){
210 $setting = 'setting-'.$key.'-'.self::get_current_role();
211 return themify_builder_get( $setting, $setting,true );
212 }
213
214 /**
215 * Returns true if current logged-in user is the author of $post
216 */
217 private static function is_current_user_the_author( $post ):bool {
218 $post = get_post( $post );
219 return $post && (int) $post->post_author === wp_get_current_user()->ID;
220 }
221 }
222
223 Themify_Access_Role::init();
224 endif;
225