-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.php
More file actions
240 lines (177 loc) · 5.51 KB
/
Setup.php
File metadata and controls
240 lines (177 loc) · 5.51 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Setup Hide Admin Bar.
*
* @package Hide_Admin_Bar
*/
namespace Mapsteps\HideAdminBar;
use Mapsteps\HideAdminBar\Ajax\SaveSettingsAction;
/**
* Setup Hide Admin Bar.
*/
class Setup {
/**
* Whether to remove admin bar for current user.
*
* @var bool
*/
public static $remove_admin_bar = false;
/**
* The class instance.
*
* @var object
*/
public static $instance;
/**
* Get instance of the class.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Init the class setup.
*/
public static function init() {
self::$instance = new self();
add_action( 'plugins_loaded', array( self::$instance, 'setup' ) );
}
/**
* Check if we're on the BAB settings page.
*
* @return bool
*/
private function is_settings_page() {
$current_screen = get_current_screen();
return ( 'settings_page_hide-admin-bar' === $current_screen->id ? true : false );
}
/**
* Setup action & filters.
*/
public function setup() {
add_action( 'init', array( $this, 'setup_text_domain' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ), 999 );
add_action( 'admin_menu', array( $this, 'add_submenu_page' ) );
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ), 20 );
add_action( 'wp', array( $this, 'remove_admin_bar' ) );
add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 4 );
// Ajax.
add_action( 'wp_ajax_hide_admin_bar_save_settings', array( new SaveSettingsAction(), 'ajax' ) );
}
/**
* Setup textdomain.
*/
public function setup_text_domain() {
load_plugin_textdomain( 'hide-admin-bar', false, HIDE_ADMIN_BAR_PLUGIN_DIR . '/languages' );
}
/**
* Enqueue admin styles & scripts.
*/
public function admin_scripts() {
if ( ! $this->is_settings_page() ) {
return;
}
// Select2.
wp_enqueue_style( 'select2', HIDE_ADMIN_BAR_PLUGIN_URL . '/assets/css/select2.min.css', array(), HIDE_ADMIN_BAR_PLUGIN_VERSION );
// Settings page styling.
wp_enqueue_style( 'heatbox', HIDE_ADMIN_BAR_PLUGIN_URL . '/assets/css/heatbox.css', array(), HIDE_ADMIN_BAR_PLUGIN_VERSION );
// Hide Admin Bar admin styling.
wp_enqueue_style( 'hide-admin-bar-settings', HIDE_ADMIN_BAR_PLUGIN_URL . '/assets/css/settings-page.css', array(), HIDE_ADMIN_BAR_PLUGIN_VERSION );
// Select2.
wp_enqueue_script( 'select2', HIDE_ADMIN_BAR_PLUGIN_URL . '/assets/js/select2.min.js', array( 'jquery' ), HIDE_ADMIN_BAR_PLUGIN_VERSION, true );
wp_enqueue_script( 'hide-admin-bar-settings', HIDE_ADMIN_BAR_PLUGIN_URL . '/assets/js/settings-page.js', array( 'jquery' ), HIDE_ADMIN_BAR_PLUGIN_VERSION, true );
wp_localize_script(
'hide-admin-bar-settings',
'HideAdminBar',
array(
'labels' => array(
'edit' => __( 'Edit', 'hide-admin-bar' ),
'save' => __( 'Save', 'hide-admin-bar' ),
),
)
);
}
/**
* Add settings link to plugin list page.
*
* @param array $actions An array of plugin action links.
* @param string $plugin_file Path to the plugin file relative to the plugins directory.
* @param array $plugin_data An array of plugin data. See `get_plugin_data()`.
* @param string $context The plugin context. By default this can include 'all', 'active', 'inactive',
* 'recently_activated', 'upgrade', 'mustuse', 'dropins', and 'search'.
*
* @return array The modified plugin action links.
*/
public function add_settings_link( $actions, $plugin_file, $plugin_data, $context ) {
if ( HIDE_ADMIN_BAR_PLUGIN_BASENAME === $plugin_file ) {
$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=hide-admin-bar' ) ) . '">' . __( 'Settings', 'hide-admin-bar' ) . '</a>';
array_unshift( $actions, $settings_link );
}
return $actions;
}
/**
* Add submenu under "Settings" menu item.
*/
public function add_submenu_page() {
add_options_page(
__( 'Hide Admin Bar', 'hide-admin-bar' ),
__( 'Hide Admin Bar', 'hide-admin-bar' ),
'delete_others_posts',
'hide-admin-bar',
array(
$this,
'settings_page_output',
)
);
}
/**
* Hide Admin Bar settings page output.
*/
public function settings_page_output() {
require __DIR__ . '/templates/settings-page.php';
}
/**
* Modify admin body class.
*
* @param string $classes The class names.
*/
public function admin_body_class( $classes ) {
$current_user = wp_get_current_user();
$classes .= ' hide-admin-bar-user-' . $current_user->user_nicename;
$roles = $current_user->roles;
$roles = $roles ?: array();
foreach ( $roles as $role ) {
$classes .= ' hide-admin-bar-role-' . $role;
}
if ( ! $this->is_settings_page() ) {
return $classes;
}
$classes .= ' heatbox-admin has-header';
return $classes;
}
/**
* Remove admin bar on the frontend for certain roles.
*/
public function remove_admin_bar() {
$admin_bar_settings = hide_admin_bar_get_admin_bar_settings();
if ( empty( $admin_bar_settings['remove_by_roles'] ) ) {
return;
}
$selected_roles = $admin_bar_settings['remove_by_roles'];
if ( in_array( 'all', $admin_bar_settings['remove_by_roles'], true ) ) {
self::$remove_admin_bar = true;
add_filter( 'show_admin_bar', '__return_false' );
return;
}
$current_user = wp_get_current_user();
foreach ( $current_user->roles as $role ) {
if ( in_array( $role, $selected_roles, true ) ) {
self::$remove_admin_bar = true;
add_filter( 'show_admin_bar', '__return_false' );
break;
}
}
}
}