Restrict Widgets - Version 1.0.1

Version Description

  • Fix: Widget options not saving if no options selected
Download this release

Release Info

Developer dfactory
Plugin Icon 128x128 Restrict Widgets
Version 1.0.1
Comparing to
See all releases

Code changes from version 1.0.0 to 1.0.1

Files changed (3) hide show
  1. css/chosen.css +3 -3
  2. readme.txt +8 -5
  3. restrict-widgets.php +77 -58
css/chosen.css CHANGED
@@ -160,8 +160,8 @@
160
  font-family: sans-serif;
161
  font-size: 100%;
162
  height: 15px;
163
- padding: 5px;
164
- margin: 1px 0;
165
  outline: 0;
166
  -webkit-box-shadow: none;
167
  -moz-box-shadow : none;
@@ -410,4 +410,4 @@
410
  background-size: 52px 37px !important;
411
  }
412
  }
413
- /* @end */
160
  font-family: sans-serif;
161
  font-size: 100%;
162
  height: 15px;
163
+ padding: 3px;
164
+ margin: 1px 2px;
165
  outline: 0;
166
  -webkit-box-shadow: none;
167
  -moz-box-shadow : none;
410
  background-size: 52px 37px !important;
411
  }
412
  }
413
+ /* @end */
readme.txt CHANGED
@@ -1,17 +1,17 @@
1
  === Restrict Widgets ===
2
  Contributors: dfactory
3
- Tags: widget, widgets, widget-only, cms, hide, custom, restrict, manage, management, capability, capabilities, sidebar, sidebars, user, permission, permissions
4
  Requires at least: 3.5
5
  Tested up to: 3.5.1
6
- Stable tag: 1.0
7
  License: MIT License
8
  License URI: http://opensource.org/licenses/MIT
9
 
10
- Take full control over your widgets! Select on which pages each widget will appear on, who can manage widgets and which widgets users can use.
11
 
12
  == Description ==
13
 
14
- [Restrict Widgets](http://www.dfactory.eu/plugins/restrict-widgets/) is all in one solution for widget management in WordPress. It gives every widget an extra control fields that lets you control the pages that the widget will appear on. This plugin creates a new �manage_widgets� capability, that allows you to gives access to widgets page to selected WordPress user roles. You can also set which sidebars and widgets will be available to selected users, which widget options will be available and how it will be displayed.
15
 
16
  By default, �Hide widget on selected� is enabled with no options selected, so all current widgets will continue to display on all pages.
17
 
@@ -47,9 +47,12 @@ No questions yet.
47
 
48
  == Changelog ==
49
 
 
 
 
50
  = 1.0 =
51
  Initial release
52
 
53
  == Upgrade Notice ==
54
 
55
- The current version of Restrict Widgets requires WordPress 3.5 or higher. If you use older version of WordPress, you need to upgrade WordPress first.
1
  === Restrict Widgets ===
2
  Contributors: dfactory
3
+ Tags: widget, widgets, widget-only, cms, conditional tags, conditional, widget logic, widget context, restrict, manage, management, capability, capabilities, sidebar, sidebars, user, permission, permissions
4
  Requires at least: 3.5
5
  Tested up to: 3.5.1
6
+ Stable tag: 1.0.1
7
  License: MIT License
8
  License URI: http://opensource.org/licenses/MIT
9
 
10
+ Restrict Widgets allows you to easily hide or display widgets on specified pages.
11
 
12
  == Description ==
13
 
14
+ [Restrict Widgets](http://www.dfactory.eu/plugins/restrict-widgets/) is all in one solution for widget management in WordPress. It lets you easily control the pages that each widget will appear on and avoid creating multiple sidebars and duplicating widgets. You can also set who can manage widgets, which sidebars and widgets will be available to selected users, which widget options will be available and how it will be displayed.
15
 
16
  By default, �Hide widget on selected� is enabled with no options selected, so all current widgets will continue to display on all pages.
17
 
47
 
48
  == Changelog ==
49
 
50
+ = 1.0.1 =
51
+ * Fix: Widget options not saving if no options selected
52
+
53
  = 1.0 =
54
  Initial release
55
 
56
  == Upgrade Notice ==
57
 
58
+ The current version of Restrict Widgets requires WordPress 3.5 or higher. If you use older version of WordPress, you need to upgrade WordPress first.
restrict-widgets.php CHANGED
@@ -1,8 +1,8 @@
1
  <?php
2
  /*
3
  Plugin Name: Restrict Widgets
4
- Description: Take full control over your widgets! Select on which pages each widget will appear on, who can manage widgets and which widgets users can use.
5
- Version: 1.0.0
6
  Author: dFactory
7
  Author URI: http://www.dfactory.eu/
8
  Plugin URI: http://www.dfactory.eu/plugins/restrict-widgets/
@@ -22,15 +22,15 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
22
 
23
  class RestrictWidgets
24
  {
25
- private $others = array();
26
- private $users = array();
27
  private $custom_post_types = array();
28
  private $custom_post_types_archives = array();
29
  private $categories = array();
30
  private $taxonomies = array();
31
- private $pages = array();
32
- private $options = array();
33
  private $languages = array();
 
34
 
35
 
36
  public function __construct()
@@ -44,7 +44,6 @@ class RestrictWidgets
44
  add_action('admin_init', array(&$this, 'load_dynamic_data'));
45
  add_action('widgets_init', array(&$this, 'load_other_data'), 10);
46
  add_action('widgets_init', array(&$this, 'save_restrict_options'), 10);
47
- add_action('widgets_init', array(&$this, 'init_restrict_widgets'), 10);
48
  add_action('widgets_init', array(&$this, 'init_restrict_sidebars'), 11);
49
  add_action('sidebar_admin_page', array(&$this, 'add_widgets_options_box'));
50
  add_action('in_widget_form', array(&$this, 'display_admin_widgets_options'), 10, 3);
@@ -58,7 +57,10 @@ class RestrictWidgets
58
  add_filter('dynamic_sidebar_params', array(&$this, 'restrict_sidebar_params'), 10, 3);
59
  }
60
 
61
- // Fix for Polylang - removes language switcher in widgets
 
 
 
62
  public function polylang_widgets()
63
  {
64
  if(function_exists('pll_the_languages'));
@@ -73,12 +75,18 @@ class RestrictWidgets
73
  }
74
 
75
 
 
 
 
76
  public function load_textdomain()
77
  {
78
  load_plugin_textdomain('restrict-widgets', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
79
  }
80
 
81
 
 
 
 
82
  public function restrict_sidebar_params($params)
83
  {
84
  if(!current_user_can('manage_options'))
@@ -100,6 +108,9 @@ class RestrictWidgets
100
  }
101
 
102
 
 
 
 
103
  public function activation()
104
  {
105
  $role = get_role('administrator');
@@ -121,6 +132,9 @@ class RestrictWidgets
121
  }
122
 
123
 
 
 
 
124
  public function deactivation()
125
  {
126
  $option = get_option('rw_widgets_options');
@@ -152,6 +166,9 @@ class RestrictWidgets
152
  }
153
 
154
 
 
 
 
155
  public function load_dynamic_data()
156
  {
157
  $this->taxonomies = get_taxonomies(
@@ -189,6 +206,9 @@ class RestrictWidgets
189
  }
190
 
191
 
 
 
 
192
  public function load_other_data()
193
  {
194
  $this->options = array(
@@ -249,6 +269,9 @@ class RestrictWidgets
249
  }
250
 
251
 
 
 
 
252
  public function init_restrict_sidebars()
253
  {
254
  if(!current_user_can('manage_options') && current_user_can('edit_theme_options'))
@@ -263,24 +286,9 @@ class RestrictWidgets
263
  }
264
 
265
 
266
- public function init_restrict_widgets()
267
- {
268
- /*
269
- global $wp_registered_widgets, $wp_registered_sidebars;
270
-
271
- if(!current_user_can('manage_options') && current_user_can('edit_theme_options'))
272
- {
273
- $option = get_option('rw_widgets_options');
274
-
275
- foreach(array_keys($option['available']) as $widget_class)
276
- {
277
- unregister_widget($widget_class);
278
- }
279
- }
280
- */
281
- }
282
-
283
-
284
  public function save_restrict_options()
285
  {
286
  //are we saving with administration privileges?
@@ -411,6 +419,9 @@ class RestrictWidgets
411
  }
412
 
413
 
 
 
 
414
  public function add_widgets_options_box()
415
  {
416
  if(!current_user_can('manage_options'))
@@ -539,12 +550,18 @@ class RestrictWidgets
539
  }
540
 
541
 
 
 
 
542
  private function sort_widgets_by_name($element_a, $element_b)
543
  {
544
  return strnatcasecmp($element_a['name'], $element_b['name']);
545
  }
546
 
547
 
 
 
 
548
  public function widgets_scripts_styles($page)
549
  {
550
  if($page !== 'widgets.php')
@@ -565,6 +582,7 @@ class RestrictWidgets
565
  $js_widgets = $js_class = $js_nonclass = array();
566
  $orphan_sidebar = 0;
567
 
 
568
  if(!current_user_can('manage_options'))
569
  {
570
  global $wp_registered_widgets;
@@ -582,6 +600,7 @@ class RestrictWidgets
582
  }
583
  }
584
 
 
585
  foreach($widgets as $widget)
586
  {
587
  if(isset($wp_registered_widgets[$widget]['callback'][0]))
@@ -593,12 +612,15 @@ class RestrictWidgets
593
  }
594
  }
595
 
 
596
  foreach($wp_registered_widgets as $widget)
597
  {
 
598
  if(isset($widget['callback'][0]) && is_object($widget['callback'][0]) && in_array(get_class($widget['callback'][0]), $restrict))
599
  {
600
  $js_class[] = $widget['callback'][0]->id_base;
601
  }
 
602
  elseif(in_array($widget['id'], $restrict))
603
  {
604
  $js_nonclass[] = $widget['id'];
@@ -628,6 +650,9 @@ class RestrictWidgets
628
  }
629
 
630
 
 
 
 
631
  private function getSelectionGroup($group_name, $type, $widget = '', $instance = '', $option = '')
632
  {
633
  $rw_option = get_option('rw_widgets_options');
@@ -962,6 +987,9 @@ class RestrictWidgets
962
  }
963
 
964
 
 
 
 
965
  public function display_admin_widgets_options($widget, $empty, $instance)
966
  {
967
  if(isset($instance['rw_opt']['widget_select']) === FALSE) $instance['rw_opt']['widget_select'] = FALSE;
@@ -988,14 +1016,16 @@ class RestrictWidgets
988
  }
989
 
990
 
 
 
 
991
  public function update_admin_widgets_options($instance, $new_instance)
992
  {
993
- $instance['rw_opt']['widget_select'] = ($new_instance['widget_select'] === 'yes' ? TRUE : FALSE);
994
-
995
  if(is_array($new_instance['widget_multiselect']))
996
  {
997
  $selected = $new_instance['widget_multiselect'];
998
 
 
999
  foreach($this->pages as $page)
1000
  {
1001
  if(in_array('pageid_'.$page->ID, $selected))
@@ -1008,6 +1038,7 @@ class RestrictWidgets
1008
  }
1009
  }
1010
 
 
1011
  foreach($this->custom_post_types as $cpt)
1012
  {
1013
  if(in_array('cpt_'.$cpt->name, $selected))
@@ -1020,6 +1051,7 @@ class RestrictWidgets
1020
  }
1021
  }
1022
 
 
1023
  foreach($this->custom_post_types_archives as $cpta)
1024
  {
1025
  if(in_array('cpta_'.$cpta->name, $selected))
@@ -1032,6 +1064,7 @@ class RestrictWidgets
1032
  }
1033
  }
1034
 
 
1035
  foreach($this->categories as $category)
1036
  {
1037
  if(in_array('category_'.$category->cat_ID, $selected))
@@ -1044,6 +1077,7 @@ class RestrictWidgets
1044
  }
1045
  }
1046
 
 
1047
  foreach($this->taxonomies as $taxonomy)
1048
  {
1049
  if(in_array('taxonomy_'.$taxonomy->name, $selected))
@@ -1056,6 +1090,7 @@ class RestrictWidgets
1056
  }
1057
  }
1058
 
 
1059
  foreach($this->others as $key => $value)
1060
  {
1061
  if(in_array('others_'.$key, $selected))
@@ -1068,6 +1103,7 @@ class RestrictWidgets
1068
  }
1069
  }
1070
 
 
1071
  foreach($this->users as $key => $value)
1072
  {
1073
  if(in_array('users_'.$key, $selected))
@@ -1080,6 +1116,7 @@ class RestrictWidgets
1080
  }
1081
  }
1082
 
 
1083
  if($this->languages !== FALSE)
1084
  {
1085
  foreach($this->languages as $key => $value)
@@ -1095,52 +1132,34 @@ class RestrictWidgets
1095
  }
1096
  }
1097
  }
 
 
 
 
 
1098
 
1099
  return $instance;
1100
  }
1101
 
1102
 
 
 
 
1103
  public function display_frontend_widgets($instance)
1104
  {
1105
- // var_dump($instance);
1106
-
1107
- /*
1108
- others_
1109
- + front_page
1110
- + blog_page
1111
- + single_post
1112
- + sticky_post
1113
- + author_archive
1114
- + date_archive
1115
- + 404_page
1116
- + search_page
1117
-
1118
- users_
1119
- logged_in
1120
- logged_out
1121
-
1122
- +cpt_
1123
- +cpta_
1124
- +taxonomy_
1125
- +category_
1126
- language_
1127
- +pageid_
1128
- */
1129
-
1130
  global $wp_query;
1131
-
1132
  $post_id = $wp_query->get_queried_object_id();
1133
 
1134
  if(function_exists('icl_get_languages') || function_exists('pll_the_languages'))
1135
  {
1136
-
1137
  $post_id = icl_object_id($post_id, 'page', FALSE);
1138
-
1139
  if(defined('ICL_LANGUAGE_CODE'))
1140
  {
1141
  $display = isset($instance['rw_opt']['language_'.ICL_LANGUAGE_CODE]) ? TRUE : FALSE;
1142
  }
1143
-
1144
  if(isset($instance['rw_opt']['widget_select']))
1145
  {
1146
  if (($instance['rw_opt']['widget_select'] === TRUE && $display === FALSE) || ($instance['rw_opt']['widget_select'] === FALSE && $display === TRUE))
@@ -1240,7 +1259,7 @@ class RestrictWidgets
1240
  return FALSE;
1241
  }
1242
  }
1243
-
1244
  return $instance;
1245
  }
1246
 
1
  <?php
2
  /*
3
  Plugin Name: Restrict Widgets
4
+ Description: Restrict Widgets allows you to hide or display widgets on specified pages.
5
+ Version: 1.0.1
6
  Author: dFactory
7
  Author URI: http://www.dfactory.eu/
8
  Plugin URI: http://www.dfactory.eu/plugins/restrict-widgets/
22
 
23
  class RestrictWidgets
24
  {
25
+ private $pages = array();
 
26
  private $custom_post_types = array();
27
  private $custom_post_types_archives = array();
28
  private $categories = array();
29
  private $taxonomies = array();
30
+ private $others = array();
31
+ private $users = array();
32
  private $languages = array();
33
+ private $options = array();
34
 
35
 
36
  public function __construct()
44
  add_action('admin_init', array(&$this, 'load_dynamic_data'));
45
  add_action('widgets_init', array(&$this, 'load_other_data'), 10);
46
  add_action('widgets_init', array(&$this, 'save_restrict_options'), 10);
 
47
  add_action('widgets_init', array(&$this, 'init_restrict_sidebars'), 11);
48
  add_action('sidebar_admin_page', array(&$this, 'add_widgets_options_box'));
49
  add_action('in_widget_form', array(&$this, 'display_admin_widgets_options'), 10, 3);
57
  add_filter('dynamic_sidebar_params', array(&$this, 'restrict_sidebar_params'), 10, 3);
58
  }
59
 
60
+
61
+ /**
62
+ * Fix for Polylang - removes language switcher in widgets
63
+ */
64
  public function polylang_widgets()
65
  {
66
  if(function_exists('pll_the_languages'));
75
  }
76
 
77
 
78
+ /**
79
+ * Loads text domain
80
+ */
81
  public function load_textdomain()
82
  {
83
  load_plugin_textdomain('restrict-widgets', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
84
  }
85
 
86
 
87
+ /**
88
+ * Hides widgets for users without admin privileges
89
+ */
90
  public function restrict_sidebar_params($params)
91
  {
92
  if(!current_user_can('manage_options'))
108
  }
109
 
110
 
111
+ /**
112
+ * Actives plugin
113
+ */
114
  public function activation()
115
  {
116
  $role = get_role('administrator');
132
  }
133
 
134
 
135
+ /**
136
+ * Deactivates plugin
137
+ */
138
  public function deactivation()
139
  {
140
  $option = get_option('rw_widgets_options');
166
  }
167
 
168
 
169
+ /**
170
+ * Loads dynamic data
171
+ */
172
  public function load_dynamic_data()
173
  {
174
  $this->taxonomies = get_taxonomies(
206
  }
207
 
208
 
209
+ /**
210
+ * Loads other data (dynamic data here too like get_pages() due to some WP restrictions) and languages
211
+ */
212
  public function load_other_data()
213
  {
214
  $this->options = array(
269
  }
270
 
271
 
272
+ /**
273
+ * Removes selected sidebars for users without edit_theme_options capability
274
+ */
275
  public function init_restrict_sidebars()
276
  {
277
  if(!current_user_can('manage_options') && current_user_can('edit_theme_options'))
286
  }
287
 
288
 
289
+ /**
290
+ * Saves restrict widgets options
291
+ */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  public function save_restrict_options()
293
  {
294
  //are we saving with administration privileges?
419
  }
420
 
421
 
422
+ /**
423
+ * Displays restrict widgets options box
424
+ */
425
  public function add_widgets_options_box()
426
  {
427
  if(!current_user_can('manage_options'))
550
  }
551
 
552
 
553
+ /**
554
+ * Sorts widgets by name
555
+ */
556
  private function sort_widgets_by_name($element_a, $element_b)
557
  {
558
  return strnatcasecmp($element_a['name'], $element_b['name']);
559
  }
560
 
561
 
562
+ /**
563
+ * Loads scripts and styles
564
+ */
565
  public function widgets_scripts_styles($page)
566
  {
567
  if($page !== 'widgets.php')
582
  $js_widgets = $js_class = $js_nonclass = array();
583
  $orphan_sidebar = 0;
584
 
585
+ //only for users without admin privileges
586
  if(!current_user_can('manage_options'))
587
  {
588
  global $wp_registered_widgets;
600
  }
601
  }
602
 
603
+ //which sidebars to hide
604
  foreach($widgets as $widget)
605
  {
606
  if(isset($wp_registered_widgets[$widget]['callback'][0]))
612
  }
613
  }
614
 
615
+ //which widgets to hide
616
  foreach($wp_registered_widgets as $widget)
617
  {
618
+ //standard based widget class
619
  if(isset($widget['callback'][0]) && is_object($widget['callback'][0]) && in_array(get_class($widget['callback'][0]), $restrict))
620
  {
621
  $js_class[] = $widget['callback'][0]->id_base;
622
  }
623
+ //non-standard based widget
624
  elseif(in_array($widget['id'], $restrict))
625
  {
626
  $js_nonclass[] = $widget['id'];
650
  }
651
 
652
 
653
+ /**
654
+ * Displays lists of data (pages, custom post types, categories, taxonomiex, ...) for options box and widget box
655
+ */
656
  private function getSelectionGroup($group_name, $type, $widget = '', $instance = '', $option = '')
657
  {
658
  $rw_option = get_option('rw_widgets_options');
987
  }
988
 
989
 
990
+ /**
991
+ * Displays widget box
992
+ */
993
  public function display_admin_widgets_options($widget, $empty, $instance)
994
  {
995
  if(isset($instance['rw_opt']['widget_select']) === FALSE) $instance['rw_opt']['widget_select'] = FALSE;
1016
  }
1017
 
1018
 
1019
+ /**
1020
+ * Saves widget box
1021
+ */
1022
  public function update_admin_widgets_options($instance, $new_instance)
1023
  {
 
 
1024
  if(is_array($new_instance['widget_multiselect']))
1025
  {
1026
  $selected = $new_instance['widget_multiselect'];
1027
 
1028
+ //pages
1029
  foreach($this->pages as $page)
1030
  {
1031
  if(in_array('pageid_'.$page->ID, $selected))
1038
  }
1039
  }
1040
 
1041
+ //custom post types
1042
  foreach($this->custom_post_types as $cpt)
1043
  {
1044
  if(in_array('cpt_'.$cpt->name, $selected))
1051
  }
1052
  }
1053
 
1054
+ //custom post types archives
1055
  foreach($this->custom_post_types_archives as $cpta)
1056
  {
1057
  if(in_array('cpta_'.$cpta->name, $selected))
1064
  }
1065
  }
1066
 
1067
+ //categories
1068
  foreach($this->categories as $category)
1069
  {
1070
  if(in_array('category_'.$category->cat_ID, $selected))
1077
  }
1078
  }
1079
 
1080
+ //taxonomies
1081
  foreach($this->taxonomies as $taxonomy)
1082
  {
1083
  if(in_array('taxonomy_'.$taxonomy->name, $selected))
1090
  }
1091
  }
1092
 
1093
+ //others
1094
  foreach($this->others as $key => $value)
1095
  {
1096
  if(in_array('others_'.$key, $selected))
1103
  }
1104
  }
1105
 
1106
+ //users
1107
  foreach($this->users as $key => $value)
1108
  {
1109
  if(in_array('users_'.$key, $selected))
1116
  }
1117
  }
1118
 
1119
+ //languages
1120
  if($this->languages !== FALSE)
1121
  {
1122
  foreach($this->languages as $key => $value)
1132
  }
1133
  }
1134
  }
1135
+ //clear plugin-instance
1136
+ else unset($instance['rw_opt']);
1137
+
1138
+ //widget_multiselect
1139
+ $instance['rw_opt']['widget_select'] = ($new_instance['widget_select'] === 'yes' ? TRUE : FALSE);
1140
 
1141
  return $instance;
1142
  }
1143
 
1144
 
1145
+ /**
1146
+ * Manages front-end display of widgets
1147
+ */
1148
  public function display_frontend_widgets($instance)
1149
  {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1150
  global $wp_query;
1151
+
1152
  $post_id = $wp_query->get_queried_object_id();
1153
 
1154
  if(function_exists('icl_get_languages') || function_exists('pll_the_languages'))
1155
  {
 
1156
  $post_id = icl_object_id($post_id, 'page', FALSE);
1157
+
1158
  if(defined('ICL_LANGUAGE_CODE'))
1159
  {
1160
  $display = isset($instance['rw_opt']['language_'.ICL_LANGUAGE_CODE]) ? TRUE : FALSE;
1161
  }
1162
+
1163
  if(isset($instance['rw_opt']['widget_select']))
1164
  {
1165
  if (($instance['rw_opt']['widget_select'] === TRUE && $display === FALSE) || ($instance['rw_opt']['widget_select'] === FALSE && $display === TRUE))
1259
  return FALSE;
1260
  }
1261
  }
1262
+
1263
  return $instance;
1264
  }
1265