-
Notifications
You must be signed in to change notification settings - Fork 0
/
hide-process-pages.php
executable file
·137 lines (96 loc) · 3.05 KB
/
hide-process-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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
Plugin Name: Hide Process Pages
Plugin URI: http://stomptheweb.co.uk
Description: Hide process pages from users that are crucial to a process
Version: 1.0.0
Author: Steven Jones
Author URI: http://stomptheweb.co.uk/
License: GPL2
*/
// Hide pages from non-Administrators
function hpp_hide_pages_admin( $query ) {
if ('administrator' == hpp_get_user_role()) {
return;
}
global $typenow;
if ( 'page' != $typenow) {
return;
}
$current_selection = get_option('hpp_pages');
if ( is_admin() && $query->is_main_query() ) {
$query->set( 'post__not_in', $current_selection );
}
}
add_action( 'pre_get_posts', 'hpp_hide_pages_admin' );
// Add a submenu item labelled 'Process Pages' the pages menu item.
function hpp_add_submenu_page() {
global $hide_process_pages;
$hide_process_pages = add_submenu_page( 'edit.php?post_type=page', 'Hide Process Pages', 'Process Pages', 'manage_options', 'process-pages', 'hpp_manage_pages' );
}
add_action('admin_menu', 'hpp_add_submenu_page');
// Admin page manage the process pages
function hpp_manage_pages() { ?>
<script type="text/javascript">
jQuery(document).ready(function() { jQuery("#hide-pages").select2(); });
</script>
<style>
.select2-container {
width: 50%;
min-width: 320px;
}
.select2-results {
width: 100%;
}
</style>
<h2>Hide Process Pages</h2>
<?php if ( isset( $_POST['hpp_update_pages'] ) && $_POST['hpp_update_pages'] ) { ?>
<div id="message" class="updated">
<p>Selection Updated.</p>
</div>
<br />
<?php } ?>
<p>Select the pages that should be hidden users (Non-Administrators). They will be hidden from the pages section.</p>
<form method="POST">
<?php
$pages = get_pages();
$current_selection = get_option('hpp_pages');
?>
<select name="process_pages[]" id="hide-pages" multiple>
<?php foreach ( $pages as $page ) { ?>
<option value="<?php echo $page->ID; ?>" <?php if(in_array($page->ID, $current_selection)) { echo 'selected'; } ?>>
<?php echo $page->post_title; ?>
</option>
<?php } ?>
</select>
<p class="submit">
<input class="button-primary" type="submit" name="hpp_update_pages" value="Update Selection"/>
</p>
</form>
<?php }
function hpp_update_pages() {
$process_pages = array();
if (isset($_POST['hpp_update_pages'])) {
$process_pages = $_POST['process_pages'];
update_option('hpp_pages', $process_pages);
}
}
add_action('admin_init', 'hpp_update_pages');
function hpp_enqueue_select2($hook) {
global $hide_process_pages;
if ($hook != $hide_process_pages) {
return;
}
wp_register_script( 'select2', plugin_dir_url( __FILE__ ) . 'js/select2/select2.min.js', array( 'jquery' ));
wp_register_style( 'select2', plugin_dir_url( __FILE__ ) . 'js/select2/select2.css', array( ));
wp_enqueue_script( 'select2' );
wp_enqueue_style( 'select2' );
}
add_action('admin_enqueue_scripts', 'hpp_enqueue_select2');
function hpp_get_user_role() {
global $wp_roles;
$current_user = wp_get_current_user();
$roles = $current_user->roles;
$role = array_shift($roles);
return $role;
}