-
Notifications
You must be signed in to change notification settings - Fork 3
/
capability.php
50 lines (42 loc) · 1.38 KB
/
capability.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace tangible\template_system;
/**
* Admins without `unfiltered_html` capability cannot edit template post types
*
* - On multisite installs, by default only network admins have this capability, not subsite admins
* - Show admin menu, template editor (Gutenberg, Elementor, Beaver), and edit screens to only allowed admins
*
* The plan is to implement more detailed access control settings in Template
* System Pro module.
*
* @see ./menu.php
* @see /interations/gutenberg/enqueue
* @see /interations/elementor/template-editor-widget
* @see /interations/beaver/modules/tangible-template
*/
function can_user_edit_template($id = 0) {
if ($id === 0) {
$id = get_current_user_id();
}
return user_can( $id, 'manage_options' )
&& user_can( $id, 'unfiltered_html' )
;
}
add_action('load-post.php', function() use ($plugin) {
$id = (int) ($_GET['post'] ?? $_POST['post_ID'] ?? 0);
if (in_array(
get_post_type( $id ),
$plugin->template_post_types
) && !current_user_can( 'unfiltered_html' )) {
wp_die( __( 'Sorry, you are not allowed to edit this item.' ) );
}
});
add_action('load-post-new.php', function() use ($plugin) {
$type = $_GET['post_type'] ?? '';
if (in_array(
$type,
$plugin->template_post_types
) && !current_user_can( 'unfiltered_html' )) {
wp_die( __( 'Sorry, you are not allowed to create this item.' ) );
}
});