-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-only-pages.php
71 lines (57 loc) · 1.9 KB
/
admin-only-pages.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Plugin Name: Admin only pages
* Description: The plugin allows you to select pages that can only be edited by administrator.
* Version: 1.0.0
* Author: Daria Levchenko
* Author URI: https://github.com/levenyatko
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Add actions with settings
*/
if ( is_multisite() ) {
require_once plugin_dir_path( __FILE__ ) . 'aop-multisite.php';
} else {
require_once plugin_dir_path( __FILE__ ) . 'aop-single-site.php';
}
/**
* Add statuses to pages and check user capabilities
*/
add_action( 'init', 'aop_maybe_add_user_caps_check' );
function aop_maybe_add_user_caps_check()
{
global $aopPagesSettings;
$aopPagesSettings = get_option('closed-pages-list');
if ( empty($aopPagesSettings) ) {
$aopPagesSettings = [];
}
if ( ! current_user_can('manage_options') && current_user_can('edit_pages') ) {
add_filter( 'display_post_states', 'aop_add_admin_only_state', 10, 2 );
add_filter( 'user_has_cap', 'aop_maybe_grant_pages_capability_cap', 100, 4 );
}
}
function aop_add_admin_only_state( $post_states, $post )
{
global $aopPagesSettings;
if ( ! empty($aopPagesSettings) && in_array($post->ID, $aopPagesSettings) ) {
$post_states['admin-only'] = '<span style="color: #b32d2e;">' . __('Admins only', 'aop') . '</span>';
}
return $post_states;
}
function aop_maybe_grant_pages_capability_cap( $allcaps, $caps, $args, $user )
{
global $aopPagesSettings;
if ( ! empty($args[2]) ) {
if ( ! empty($aopPagesSettings) && in_array($args[0], ['edit_post', 'delete_post']) ) {
if ( in_array($args[2], $aopPagesSettings) ) {
$allcaps['edit_published_pages'] = false;
$allcaps['delete_pages'] = false;
$allcaps['delete_published_pages'] = false;
}
}
}
return $allcaps;
}