This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets-master.php
359 lines (318 loc) · 13.1 KB
/
widgets-master.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
/*
Plugin Name: Widgets Master
Plugin URI: http://www.jeroenvanwissen.nl/weblog/wordpress/widgets-master
Description: Control the visibility of Widgets by Categories/Taxonomies, Pages, Post types and more.
Author: Jeroen van Wissen
Author URI: http://www.jeroenvanwissen.nl
Version: 0.2
------------------------------------------------------------------------
Copyright 2012 Jeroen van Wissen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* Widgets Master class
*
* */
class Widgets_Master {
/**
* version
*
* @var string
* */
protected $version = '0.2';
/**
* constructor
*
* @since 0.1
* @version 0.2
* */
function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'in_widget_form', array( $this, 'widget_options' ), 10, 3 );
add_filter( 'widget_display_callback', array( $this, 'widget_display_callback' ), 10, 1 );
add_filter( 'widget_update_callback', array( $this, 'widget_update_callback' ), 10, 2 );
}
/**
* Load the CSS and JavaScripts needed.
*
* @since 0.1
* @version 0.1
* */
public function enqueue_scripts() {
if ( is_admin() ) {
wp_enqueue_style( 'widgets-master', plugins_url( '/css/widgets-master.css', __FILE__ ), array(), $this->version, 'all' );
wp_enqueue_script( 'widgets-master', plugins_url( '/js/widgets-master.js', __FILE__ ), array( 'jquery' ), $this->version, 'all' );
}
}
/**
* Widget update callback function
*
* @param array $instance The widget instance
* @param array $new_instance The widget instance
* @param array $old_instance The widget instance
* @return array $instance The widget instance
* @since 0.1
* @version 0.1
* */
public function widget_update_callback( $instance, $new_instance ) {
$instance = $new_instance;
// wpml compatibility
if ( function_exists( 'icl_object_id' ) && defined( 'ICL_LANGUAGE_CODE' ) ) {
$instance['language'] = ICL_LANGUAGE_CODE;
}
$instance['id_base'] = $_POST['id_base'];
// show widget on home, check with is_home()
$instance['page-home'] = false;
if ( isset( $_POST[$instance['id_base'] . '-widgets-master-home'] ) ) {
$instance['page-home'] = true;
}
// show widget on archive, check with is_archive()
$instance['page-archive'] = false;
if ( isset( $_POST[$instance['id_base'] . '-widgets-master-archive'] ) ) {
$instance['page-archive'] = true;
}
// show widget on 404, check with is_404()
$instance['page-404'] = false;
if ( isset( $_POST[$instance['id_base'] . '-widgets-master-404'] ) ) {
$instance['page-404'] = true;
}
// show widget on search, check with is_search()
$instance['page-search'] = false;
if ( isset( $_POST[$instance['id_base'] . '-widgets-master-search'] ) ) {
$instance['page-search'] = true;
}
// show widget on single, check with is_single()
$instance['page-single'] = false;
if ( isset( $_POST[$instance['id_base'] . '-widgets-master-single'] ) ) {
$instance['page-single'] = true;
}
// always show widget when nothing selected..
$instance['all'] = false;
if ( !$instance['page-home'] && !$instance['page-archive'] && !$instance['page-404'] && !$instance['page-search'] && !$instance['page-single'] ) {
$instance['all'] = true;
}
// show widget on post type
if ( isset( $_POST['posttype'] ) && is_array( $_POST['posttype'] ) ) {
foreach ( $_POST['posttype'] as $posttype ) {
$instance['type-' . $posttype] = true;
}
$instance['all'] = false;
} else {
$instance['type-all'] = true;
}
// show widget on taxonomy term
if ( isset( $_POST['term_id'] ) && is_array( $_POST['term_id'] ) ) {
foreach ( $_POST['term_id'] as $term_id ) {
$instance['cat-' . $term_id] = true;
}
// why is this line?
$instance['type-all'] = false;
$instance['all'] = false;
} else {
$instance['cat-all'] = true;
}
// show widget on page
if ( isset( $_POST['page_id'] ) && is_array( $_POST['page_id'] ) ) {
foreach ( $_POST['page_id'] as $page_id ) {
$instance['page-' . $page_id] = true;
}
$instance['all'] = false;
} else {
$instance['page-all'] = true;
}
return $instance;
}
/**
* Widget display callback function
*
* @param array $instance The widget instance
* @return mixed $instance The widget instance or false
* @since 0.1
* @version 0.1
* */
public function widget_display_callback( $instance ) {
$show = false;
// check for home / frontpage
if ( ( is_home() && isset( $instance['page-home'] ) ) || ( is_front_page() && isset( $instance['page-home'] ) ) ) {
$show = $instance['page-home'];
// check for category
} else if ( is_category() ) {
$show = isset( $instance['cat-' . get_query_var( 'cat' )] ) ? ( $instance['cat-' . get_query_var( 'cat' )] ) : false;
if ( !$show ) {
$show = isset( $instance['cat-all'] ) ? ( $instance['cat-all'] ) : false;
}
// check for taxonomy/term
} else if ( is_tax() ) {
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$show = isset( $instance['cat-' . $term->term_id] ) ? ( $instance['cat-' . $term->term_id] ) : false;
if ( !$show ) {
$show = isset( $instance['cat-all'] ) ? ( $instance['cat-all'] ) : false;
}
// check for archive
} else if ( is_archive() && isset( $instance['page-archive'] ) ) {
$show = $instance['page-archive'];
// check for single
} else if ( is_single() ) {
$type = get_post_type();
if ( $type != 'page' and $type != 'post' ) {
$show = isset( $instance['type-' . $type] ) ? ( $instance['type-' . $type] ) : false;
}
if ( !$show ) {
$show = isset( $instance['page-single'] ) ? ( $instance['page-single'] ) : false;
}
if ( !$show ) {
global $post;
$terms = wp_get_object_terms( $post->ID, get_taxonomies() );
foreach ( $terms as $term ) {
if ( $show ) {
continue;
}
if ( function_exists( 'icl_object_id' ) ) {
$term_id = icl_object_id( $term->term_id, $term->taxonomy, true, $instance['language'] );
} else {
$term_id = $term->term_id;
}
$show = isset( $instance['cat-' . $term_id] ) ? ( $instance['cat-' . $term_id] ) : false;
}
}
if ( !$show ) {
$show = isset( $instance['type-all'] ) ? ( $instance['type-all'] ) : false;
}
// check for 404
} else if ( is_404() ) {
$show = isset( $instance['page-404'] ) ? ( $instance['page-404'] ) : false;
// check for search
} else if ( is_search() ) {
$show = isset( $instance['page-search'] ) ? ( $instance['page-search'] ) : false;
// check for page
} else {
global $wp_query;
$post_id = $wp_query->get_queried_object_id();
$show = isset( $instance['page-' . $post_id] ) ? ( $instance['page-' . $post_id] ) : false;
if ( !$show ) {
$show = isset( $instance['page-all'] ) ? ( $instance['page-all'] ) : false;
}
}
// Show widget when no options set.
if ( !$show && isset( $instance['all'] ) && $instance['all'] == true ) {
return $instance;
// Show widget when options never saved. ( fist time use )
} else if ( !$show && !isset( $instance['all'] ) ) {
return $instance;
// Show widget when options say so.
} else if ( $show ) {
return $instance;
// Don't show widget
} else {
return false;
}
}
/**
* Widget Options function
*
* @param object $widget
* @param array $return
* @param array $instance
* @since 0.1
* @version 0.1
* */
public function widget_options( $widget, $return, $instance ) {
?>
<div id="<?php echo $widget->id_base; ?>-widgets-master" class="widgets-master">
<h4 class="widgets-master-heading"><?php echo __( 'Widgets Master Display Conditions', 'widgets-master' ); ?></h4>
<ul id="<?php echo $widget->id_base; ?>-widgets-master-tabs" class="widgets-master-tabs">
<li class="active <?php echo $widget->id_base; ?>-widgets-master-pages"><a class="active" href="javascript:void(null);"><?php echo __( 'Pages', 'widgets-master' ); ?></a></li>
<li class="<?php echo $widget->id_base; ?>-widgets-master-category"><a href="javascript:void(null);"><?php echo __( 'Taxonomies', 'widgets-master' ); ?></a></li>
<li class="<?php echo $widget->id_base; ?>-widgets-master-posttypes"><a href="javascript:void(null);"><?php echo __( 'Post Types', 'widgets-master' ); ?></a></li>
</ul>
<div class="<?php echo $widget->id_base; ?>-widgets-master-pages tabs-panel">
<ul>
<?php $this->widget_page_list( $widget, $instance ); ?>
</ul>
</div>
<div class="<?php echo $widget->id_base; ?>-widgets-master-category tabs-panel" style="display:none;">
<ul>
<?php $this->widget_category_list( $widget, $instance ); ?>
</ul>
</div>
<div class="<?php echo $widget->id_base; ?>-widgets-master-posttypes tabs-panel" style="display:none;">
<ul>
<?php $this->widget_posttypes_list( $widget, $instance ); ?>
</ul>
</div>
</div>
<div class="<?php echo $widget->id_base; ?>-widgets-master-extra widgets-master-box extra">
<label for="<?php echo $widget->id_base; ?>-widgets-master-home"><input type="checkbox" name="<?php echo $widget->id_base; ?>-widgets-master-home" class="widgets-master-checkbox" <?php echo isset( $instance['page-home'] ) && $instance['page-home'] == 1 ? 'checked=checked' : ''; ?>> <?php echo __( 'Homepage', 'widgets-master' ); ?></label><br />
<label for="<?php echo $widget->id_base; ?>-widgets-master-archive"><input type="checkbox" name="<?php echo $widget->id_base; ?>-widgets-master-archive" class="widgets-master-checkbox" <?php echo isset( $instance['page-archive'] ) && $instance['page-archive'] == 1 ? 'checked=checked' : ''; ?>> <?php echo __( 'Archive', 'widgets-master' ); ?></label><br />
<label for="<?php echo $widget->id_base; ?>-widgets-master-404"><input type="checkbox" name="<?php echo $widget->id_base; ?>-widgets-master-404" class="widgets-master-checkbox" <?php echo isset( $instance['page-404'] ) && $instance['page-404'] == 1 ? 'checked=checked' : ''; ?>> <?php echo __( '404 - Page not found', 'widgets-master' ); ?></label><br />
<label for="<?php echo $widget->id_base; ?>-widgets-master-search"><input type="checkbox" name="<?php echo $widget->id_base; ?>-widgets-master-search" class="widgets-master-checkbox" <?php echo isset( $instance['page-search'] ) && $instance['page-search'] == 1 ? 'checked=checked' : ''; ?>> <?php echo __( 'Search', 'widgets-master' ); ?></label><br />
<label for="<?php echo $widget->id_base; ?>-widgets-master-single"><input type="checkbox" name="<?php echo $widget->id_base; ?>-widgets-master-single" class="widgets-master-checkbox" <?php echo isset( $instance['page-single'] ) && $instance['page-single'] == 1 ? 'checked=checked' : ''; ?>> <?php echo __( 'Single', 'widgets-master' ); ?></label><br />
</div>
<?php
}
/**
* Widget Page list function
*
* @param object $widget
* @param array $instance
* @since 0.1
* @version 0.2
* */
public function widget_page_list( $widget, $instance ) {
$pages = get_pages();
foreach ( $pages as $page ) {
$checked = isset( $instance['page-' . $page->ID] ) ? 'checked=checked' : '';
echo '<li><input type="checkbox" name="page_id[]" value="' . $page->ID . '" ' . $checked . '> ' . $page->post_title . '</li>';
}
}
/**
* Widget Category list function
*
* @param object $widget
* @param array $instance
* @since 0.1
* @version 0.2
* */
public function widget_category_list( $widget, $instance ) {
$taxonomies = get_taxonomies( array( 'public' => true ), 'names', 'and' );
foreach ( $taxonomies as $taxonomy ) {
echo '<h3>' . $taxonomy . '</h3>';
$terms = get_terms( $taxonomy, array( 'hide_empty' => 0 ) );
if ( is_array( $terms ) && count( $terms ) > 0 ) {
echo '<ul>';
foreach ( $terms as $term ) {
$checked = isset( $instance['cat-' . $term->term_id] ) ? 'checked=checked' : '';
echo '<li><input type="checkbox" name="term_id[]" value="' . $term->term_id . '" ' . $checked . '> ' . $term->name . '</li>';
}
echo '</ul>';
}
}
}
/**
* Widget PostTypes list function
*
* @param object $widget
* @param array $instance
* @since 0.1
* @version 0.2
* */
public function widget_posttypes_list( $widget, $instance ) {
$posttypes = get_post_types( array( '_builtin' => false ) );
foreach ( $posttypes as $posttype ) {
$checked = isset( $instance['type-' . $posttype] ) ? 'checked=checked' : '';
echo '<li><input type="checkbox" name="posttype[]" value="' . $posttype . '" ' . $checked . ' > ' . ucfirst( $posttype ) . '</li>';
}
}
} // END class Widgets_Master
$widgets_master = new Widgets_Master();