class-admin-bar.php
7.19 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WPF_Admin_Bar {
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
/**
* Gets things started
*
* @access public
* @since 1.0
* @return void
*/
public function init() {
if(!is_user_logged_in() || !current_user_can( 'manage_options' ))
return;
add_action( 'wp_before_admin_bar_render', array($this, 'render_admin_bar'), 100);
add_action( 'wp_enqueue_scripts', array($this, 'admin_bar_style') );
add_filter( 'query_vars', array($this, 'query_vars') );
add_action( 'pre_get_posts', array( $this, 'unset_query_arg' ) );
add_filter( 'wpf_user_can_access', array( $this, 'admin_bar_overrides'), 10, 3 );
}
/**
* Register admin bar scripts and styles
*
* @access public
* @return void
*/
public function admin_bar_style() {
wp_enqueue_script( 'wpf-admin-bar', WPF_DIR_URL . '/assets/js/wpf-admin-bar.js', array('jquery') );
wp_enqueue_style( 'wpf-admin-bar', WPF_DIR_URL . '/assets/css/wpf-admin-bar.css', array(), WP_FUSION_VERSION );
}
/**
* Allows for overriding content restriction via the admin bar
*
* @access public
* @return bool
*/
public function admin_bar_overrides( $can_access, $user_id, $post_id ) {
if(get_query_var( 'wpf_tag' ) == 'unlock-all')
return true;
if(get_query_var( 'wpf_tag' ) == 'lock-all')
return false;
$user_tags = wp_fusion()->user->get_tags( $user_id );
$post_restrictions = get_post_meta( $post_id, 'wpf-settings', true );
if(empty($post_restrictions) || !isset($post_restrictions['allow_tags']) || empty($post_restrictions['allow_tags']))
return true;
if($current_filter = get_query_var( 'wpf_tag' ))
$user_tags[] = $current_filter;
foreach( (array) $post_restrictions['allow_tags'] as $tag) {
if(in_array($tag, (array) $user_tags)) {
return true;
}
}
return $can_access;
}
public function unset_query_arg( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
$filter = get_query_var( 'wpf_tag' );
if ( ! empty( $filter ) ) {
$query->set( 'wpf_tag', null );
global $wp;
// unset wpf_tag var from $wp
unset( $wp->query_vars[ 'wpf_tag' ] );
// if in home (because $wp->query_vars is empty) and 'show_on_front' is page
if ( empty( $wp->query_vars ) && get_option( 'show_on_front' ) === 'page' ) {
// reset and re-parse query vars
$wp->query_vars['page_id'] = get_option( 'page_on_front' );
$query->parse_query( $wp->query_vars );
}
// Reset the query var now that the query has been modified
set_query_var( 'wpf_tag', $filter );
}
}
/**
* Register WPF query var
*
* @access public
* @return array Vars
*/
public function query_vars($vars) {
$vars[] = "wpf_tag";
return $vars;
}
/**
* Assists admin bar in sorting tags
*
* @access public
* @return void
*/
public function subval_sort($a,$subkey) {
foreach($a as $k=>$v) {
$b[$k] = strtolower($v[$subkey]);
}
asort($b);
foreach($b as $key=>$val) {
$c[$key] = $a[$key];
}
return $c;
}
/**
* Renders admin bar
*
* @access public
* @return void
*/
public function render_admin_bar() {
if( $current_filter = get_query_var( 'wpf_tag' ) ) {
$current_filter = urldecode($current_filter);
if($current_filter == 'unlock-all') {
$label = 'Viewing: Unlock All';
} elseif ($current_filter == 'lock-all') {
$label = 'Viewing: Lock All';
} else {
$label = 'Viewing: ' . wp_fusion()->user->get_tag_label( $current_filter );
}
} else {
$label = 'Preview With Tag';
}
$menu_id = 'wpf';
global $wp_admin_bar;
global $wp;
$current_url = home_url( $wp->request ) . '/';
// Add the parent admin bar container
$wp_admin_bar->add_menu(array('id' => $menu_id, 'title' => $label));
// If theres'a filter currently set, show the option to remove filters
if($current_filter = get_query_var( 'wpf_tag' ))
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => 'Remove Filters', 'id' => 'remove-filters', 'href' => $current_url ));
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => 'Unlock All', 'id' => 'unlock-all', 'href' => $current_url . '?wpf_tag=unlock-all' ));
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => 'Lock All', 'id' => 'lock-all', 'href' => $current_url . '?wpf_tag=lock-all' ));
$tag_categories = array();
$available_tags = (array) wp_fusion()->settings->get('available_tags');
if( is_array( reset($available_tags) ) ) {
foreach( (array)$available_tags as $value) {
$tag_categories[] = $value['category'];
}
$tag_categories = array_unique($tag_categories);
sort($tag_categories);
// Add the submenu headers
foreach($tag_categories as $tag_category) {
$sub_menu_id = 'wpf-cat-' . sanitize_title_with_dashes($tag_category);
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => $tag_category, 'id' => $sub_menu_id, 'meta' => array('class' => 'wpf-category-header')));
}
$without_category_id = null;
// Sort by label
$available_tags = $this->subval_sort($available_tags, 'label');
// Add the submenu links
foreach($available_tags as $tag_id => $tag_data) {
$parent_id = 'wpf-cat-' . sanitize_title_with_dashes($tag_data['category']);
// If no category specified
if(sanitize_title_with_dashes($tag_data['category']) == '0') {
if($without_category_id == null) {
$without_category_id = 'no-category';
$wp_admin_bar->add_menu(array('parent' => $menu_id, 'title' => 'Without Category', 'id' => $without_category_id, 'meta' => array('class' => 'wpf-category-header')));
}
$parent_id = $without_category_id;
}
$wp_admin_bar->add_menu(array('parent' => $parent_id, 'title' => $tag_data['label'], 'id' => 'wpf-tag-' . $tag_id, 'href' => $current_url . '?wpf_tag=' . $tag_id));
}
} else {
asort($available_tags);
foreach( $available_tags as $tag ) {
$wp_admin_bar->add_menu( array( 'parent' => $menu_id, 'title' => $tag, 'id' => $tag, 'href' => $current_url . '?wpf_tag=' . urlencode($tag) ));
}
}
}
}
new WPF_Admin_Bar;