Widget Context - Version 1.0.4

Version Description

(May 6, 2016) = * Confirm the plugin works with the latest version of WordPress. * Fix the PHP class constructor warning. * Move the widget context settings link. * Fix the initial context state in the customizer.

Download this release

Release Info

Developer kasparsd
Plugin Icon 128x128 Widget Context
Version 1.0.4
Comparing to
See all releases

Code changes from version 1.0.3 to 1.0.4

css/admin.css CHANGED
@@ -6,8 +6,8 @@
6
  .widget-context .help { line-height:1.2; color:#666; }
7
 
8
  .widget-context .widget-context-header { float:left; width:100%; clear:both; }
9
- .widget-context .widget-context-header h3 { font-size:1.1em; margin:0 0.5em 0.5em 0; padding:0; }
10
- .widget-context .widget-context-header h3:before { font-family: 'dashicons'; content: "\f489"; vertical-align:bottom; margin-right:0.2em; }
11
 
12
  .widget-context .context-group { float:left; width:100%; clear:both; margin-bottom:0.5em; }
13
  .widget-context .context-toggle { margin:0 0 0.3em 0; display:block; text-decoration:none; color:inherit; font-weight:bold; }
@@ -19,10 +19,10 @@
19
 
20
  /** Modules **/
21
 
22
- .context-group-incexc select { width:100%; border-width:2px; }
23
- .context-group-incexc .context-toggle { display:none; }
24
 
25
- .context-group-location .context-group-wrap { max-height:10em; overflow:auto; padding:0.5em; margin-bottom:0.5em; background:#fff; border:1px solid #ddd; }
26
  .context-group-location label { display:block; padding:0.25em 0; }
27
 
28
  .context-group-custom_post_types_taxonomies .context-group-wrap { max-height:10em; overflow:auto; padding:0.5em; margin-bottom:0.5em; background:#fff; border:1px solid #ddd; }
@@ -32,6 +32,8 @@
32
 
33
  .context-group-admin_notes textarea { height:4em; }
34
 
 
 
35
  /** Settings UI **/
36
 
37
  .widget-context-form th { width:auto; }
6
  .widget-context .help { line-height:1.2; color:#666; }
7
 
8
  .widget-context .widget-context-header { float:left; width:100%; clear:both; }
9
+ .widget-context .widget-context-header h3 { font-size:1.1em; margin:0 0.5em 0.5em 0; padding:0; float:left; }
10
+ .widget-context .widget-context-header a { float:right; }
11
 
12
  .widget-context .context-group { float:left; width:100%; clear:both; margin-bottom:0.5em; }
13
  .widget-context .context-toggle { margin:0 0 0.3em 0; display:block; text-decoration:none; color:inherit; font-weight:bold; }
19
 
20
  /** Modules **/
21
 
22
+ .context-group-incexc select { width:100%; }
23
+ .context-group-incexc .context-toggle { display:none; }
24
 
25
+ .context-group-location .context-group-wrap { max-height:10em; overflow:auto; padding:0.5em; margin-bottom:0.5em; background:#fff; border:1px solid #ddd; }
26
  .context-group-location label { display:block; padding:0.25em 0; }
27
 
28
  .context-group-custom_post_types_taxonomies .context-group-wrap { max-height:10em; overflow:auto; padding:0.5em; margin-bottom:0.5em; background:#fff; border:1px solid #ddd; }
32
 
33
  .context-group-admin_notes textarea { height:4em; }
34
 
35
+ .context-group-word_count select { max-width:4em; min-width:2em; }
36
+
37
  /** Settings UI **/
38
 
39
  .widget-context-form th { width:auto; }
debug/debug-bar.php CHANGED
@@ -1,10 +1,9 @@
1
-
2
  <?php
3
 
4
  class Debug_Widget_Context extends Debug_Bar_Panel {
5
 
6
  function init() {
7
-
8
  $this->title( __( 'Widget Context', 'widget-context' ) );
9
 
10
  }
@@ -16,15 +15,15 @@ class Debug_Widget_Context extends Debug_Bar_Panel {
16
  }
17
 
18
  function render() {
19
-
20
  $wc = widget_context::instance();
21
 
22
  $sidebars_widgets = $wc->get_sidebars_widgets_copy();
23
 
24
  $map = array(
25
- 'show' => __( 'Show widget everywhere', 'widget-context' ),
26
- 'selected' => __( 'Show widget on selected', 'widget-context' ),
27
- 'notselected' => __( 'Hide widget on selected', 'widget-context' ),
28
  'hide' => __( 'Hide widget everywhere', 'widget-context' )
29
  );
30
 
@@ -51,20 +50,20 @@ class Debug_Widget_Context extends Debug_Bar_Panel {
51
  else
52
  $set = __( 'Default', 'widget-context' );
53
  }
54
-
55
- $check = apply_filters(
56
- 'widget_context_check-' . $context_id,
57
- null,
58
  $widget_context_args
59
  );
60
 
61
- $a[] = sprintf(
62
  '<tr>
63
  <th><strong>%s</strong></th>
64
  <td>%s</td>
65
  <td><pre>%s</pre></td>
66
- </tr>',
67
- $context_id,
68
  $check ? __( 'Yes', 'widget-context' ) : __( 'No', 'widget-context' ),
69
  esc_html( print_r( $widget_context_args, true ) )
70
  );
@@ -76,7 +75,7 @@ class Debug_Widget_Context extends Debug_Bar_Panel {
76
  else
77
  $status = sprintf( __( 'Hiding <strong>%s</strong> in "%s"' ), esc_html( $widget_id ), esc_html( $widget_area ) );
78
 
79
- $out[] = sprintf(
80
  '<h3><a href="#widget-%d" class="toggle">%s</a> <strong>%s</strong> &mdash; %s</h3>
81
  <table width="100%%" id="widget-%d" style="display:none;">
82
  <tr>
@@ -97,7 +96,7 @@ class Debug_Widget_Context extends Debug_Bar_Panel {
97
  }
98
  }
99
 
100
- printf(
101
  '%s
102
  <h3>Registered Contexts:</h3>
103
  <pre>%s</pre>',
@@ -108,4 +107,3 @@ class Debug_Widget_Context extends Debug_Bar_Panel {
108
  }
109
 
110
  }
111
-
 
1
  <?php
2
 
3
  class Debug_Widget_Context extends Debug_Bar_Panel {
4
 
5
  function init() {
6
+
7
  $this->title( __( 'Widget Context', 'widget-context' ) );
8
 
9
  }
15
  }
16
 
17
  function render() {
18
+
19
  $wc = widget_context::instance();
20
 
21
  $sidebars_widgets = $wc->get_sidebars_widgets_copy();
22
 
23
  $map = array(
24
+ 'show' => __( 'Show widget everywhere', 'widget-context' ),
25
+ 'selected' => __( 'Show widget on selected', 'widget-context' ),
26
+ 'notselected' => __( 'Hide widget on selected', 'widget-context' ),
27
  'hide' => __( 'Hide widget everywhere', 'widget-context' )
28
  );
29
 
50
  else
51
  $set = __( 'Default', 'widget-context' );
52
  }
53
+
54
+ $check = apply_filters(
55
+ 'widget_context_check-' . $context_id,
56
+ null,
57
  $widget_context_args
58
  );
59
 
60
+ $a[] = sprintf(
61
  '<tr>
62
  <th><strong>%s</strong></th>
63
  <td>%s</td>
64
  <td><pre>%s</pre></td>
65
+ </tr>',
66
+ $context_id,
67
  $check ? __( 'Yes', 'widget-context' ) : __( 'No', 'widget-context' ),
68
  esc_html( print_r( $widget_context_args, true ) )
69
  );
75
  else
76
  $status = sprintf( __( 'Hiding <strong>%s</strong> in "%s"' ), esc_html( $widget_id ), esc_html( $widget_area ) );
77
 
78
+ $out[] = sprintf(
79
  '<h3><a href="#widget-%d" class="toggle">%s</a> <strong>%s</strong> &mdash; %s</h3>
80
  <table width="100%%" id="widget-%d" style="display:none;">
81
  <tr>
96
  }
97
  }
98
 
99
+ printf(
100
  '%s
101
  <h3>Registered Contexts:</h3>
102
  <pre>%s</pre>',
107
  }
108
 
109
  }
 
debug/debug.js CHANGED
@@ -5,4 +5,4 @@ jQuery(document).ready(function($) {
5
  $( $(this).attr('href') ).toggle();
6
  });
7
 
8
- });
5
  $( $(this).attr('href') ).toggle();
6
  });
7
 
8
+ });
js/widget-context.js CHANGED
@@ -2,13 +2,9 @@ jQuery(document).ready(function($) {
2
 
3
  function show_hide_controls( widget_id ) {
4
 
5
- var value = $( '#widget-context-' + widget_id + ' .wc-field-select-condition select' ).val();
6
 
7
- if ( value == 'show' || value == 'hide' ) {
8
- $( '#widget-context-' + widget_id ).addClass('context-global');
9
- } else {
10
- $( '#widget-context-' + widget_id ).removeClass('context-global');
11
- }
12
 
13
  }
14
 
@@ -19,16 +15,15 @@ jQuery(document).ready(function($) {
19
  });
20
 
21
  $('#widgets-right, #widgets-left, #customize-theme-controls').on( 'change', '.wc-field-select-condition select', function(){
22
-
23
  show_hide_controls( $(this).parent().data('widget-id') );
24
 
25
  });
26
 
27
- $(document).bind( 'widget-updated', function( e, widget ) {
28
-
29
  show_hide_controls( widget.find('input[name="widget-id"]').val() );
30
 
31
  });
32
 
33
-
34
- });
2
 
3
  function show_hide_controls( widget_id ) {
4
 
5
+ var condition = $( '#widget-context-' + widget_id + ' .wc-field-select-condition select' ).val();
6
 
7
+ $( '#widget-context-' + widget_id ).toggleClass( 'context-global', ( condition == 'show' || condition == 'hide' ) );
 
 
 
 
8
 
9
  }
10
 
15
  });
16
 
17
  $('#widgets-right, #widgets-left, #customize-theme-controls').on( 'change', '.wc-field-select-condition select', function(){
18
+
19
  show_hide_controls( $(this).parent().data('widget-id') );
20
 
21
  });
22
 
23
+ $( document ).on( 'widget-updated widget-added', function( e, widget ) {
24
+
25
  show_hide_controls( widget.find('input[name="widget-id"]').val() );
26
 
27
  });
28
 
29
+ });
 
modules/custom-post-types-taxonomies/module.php CHANGED
@@ -34,7 +34,7 @@ class WidgetContextCustomCPTTax {
34
  add_filter( 'widget_contexts', array( $this, 'add_context' ) );
35
 
36
  add_filter( 'widget_context_control-custom_post_types_taxonomies', array( $this, 'context_controls' ), 10, 2 );
37
-
38
  add_filter( 'widget_context_check-custom_post_types_taxonomies', array( $this, 'context_check' ), 10, 2 );
39
 
40
  }
@@ -45,19 +45,19 @@ class WidgetContextCustomCPTTax {
45
  if ( is_array( $this->post_types ) )
46
  return;
47
 
48
- $this->post_types = get_post_types(
49
- array(
50
- 'public' => true,
51
- '_builtin' => false,
52
- 'publicly_queryable' => true
53
  ),
54
  'objects'
55
  );
56
 
57
  $this->taxonomies = get_taxonomies(
58
- array(
59
- 'public' => true,
60
- '_builtin' => false
61
  ),
62
  'objects'
63
  );
@@ -124,25 +124,25 @@ class WidgetContextCustomCPTTax {
124
  $this->set_objects();
125
 
126
  foreach ( $this->post_types as $post_type => $post_type_settings ) {
127
-
128
- $options[ 'is_singular-' . $post_type ] = sprintf(
129
- __( 'All "%s" posts', 'widget-context' ),
130
- $post_type_settings->label
131
  );
132
 
133
  if ( $post_type_settings->has_archive )
134
- $options[ 'is_archive-' . $post_type ] = sprintf(
135
- __( 'Archive of "%s" posts', 'widget-context' ),
136
- $post_type_settings->label
137
  );
138
 
139
  }
140
 
141
  foreach ( $this->taxonomies as $taxonomy => $tax_settings ) {
142
 
143
- $options[ 'is_tax-' . $taxonomy ] = sprintf(
144
- __( 'All "%s" taxonomy archives', 'widget-context' ),
145
- $tax_settings->label
146
  );
147
 
148
  }
34
  add_filter( 'widget_contexts', array( $this, 'add_context' ) );
35
 
36
  add_filter( 'widget_context_control-custom_post_types_taxonomies', array( $this, 'context_controls' ), 10, 2 );
37
+
38
  add_filter( 'widget_context_check-custom_post_types_taxonomies', array( $this, 'context_check' ), 10, 2 );
39
 
40
  }
45
  if ( is_array( $this->post_types ) )
46
  return;
47
 
48
+ $this->post_types = get_post_types(
49
+ array(
50
+ 'public' => true,
51
+ '_builtin' => false,
52
+ 'publicly_queryable' => true
53
  ),
54
  'objects'
55
  );
56
 
57
  $this->taxonomies = get_taxonomies(
58
+ array(
59
+ 'public' => true,
60
+ '_builtin' => false
61
  ),
62
  'objects'
63
  );
124
  $this->set_objects();
125
 
126
  foreach ( $this->post_types as $post_type => $post_type_settings ) {
127
+
128
+ $options[ 'is_singular-' . $post_type ] = sprintf(
129
+ __( 'All "%s" posts', 'widget-context' ),
130
+ $post_type_settings->label
131
  );
132
 
133
  if ( $post_type_settings->has_archive )
134
+ $options[ 'is_archive-' . $post_type ] = sprintf(
135
+ __( 'Archive of "%s" posts', 'widget-context' ),
136
+ $post_type_settings->label
137
  );
138
 
139
  }
140
 
141
  foreach ( $this->taxonomies as $taxonomy => $tax_settings ) {
142
 
143
+ $options[ 'is_tax-' . $taxonomy ] = sprintf(
144
+ __( 'All "%s" taxonomy archives', 'widget-context' ),
145
+ $tax_settings->label
146
  );
147
 
148
  }
modules/word-count/module.php CHANGED
@@ -59,7 +59,7 @@ class WidgetContextWordCount {
59
  function count_words_on_page() {
60
 
61
  global $wp_query;
62
-
63
  if ( empty( $wp_query->posts ) || is_admin() )
64
  return;
65
 
@@ -70,7 +70,7 @@ class WidgetContextWordCount {
70
 
71
 
72
  function context_check_word_count( $check, $settings ) {
73
-
74
  $settings = wp_parse_args( $settings, array(
75
  'check_wordcount' => false,
76
  'word_count' => null,
@@ -99,7 +99,7 @@ class WidgetContextWordCount {
99
 
100
  function control_word_count( $control_args ) {
101
 
102
- return sprintf(
103
  '<p>%s %s %s</p>',
104
  $this->wc->make_simple_checkbox( $control_args, 'check_wordcount', __('Has', 'widget-context') ),
105
  $this->wc->make_simple_dropdown( $control_args, 'check_wordcount_type', array( 'less' => __('less', 'widget-context'), 'more' => __('more', 'widget-context') ), null, __('than', 'widget-context') ),
59
  function count_words_on_page() {
60
 
61
  global $wp_query;
62
+
63
  if ( empty( $wp_query->posts ) || is_admin() )
64
  return;
65
 
70
 
71
 
72
  function context_check_word_count( $check, $settings ) {
73
+
74
  $settings = wp_parse_args( $settings, array(
75
  'check_wordcount' => false,
76
  'word_count' => null,
99
 
100
  function control_word_count( $control_args ) {
101
 
102
+ return sprintf(
103
  '<p>%s %s %s</p>',
104
  $this->wc->make_simple_checkbox( $control_args, 'check_wordcount', __('Has', 'widget-context') ),
105
  $this->wc->make_simple_dropdown( $control_args, 'check_wordcount_type', array( 'less' => __('less', 'widget-context'), 'more' => __('more', 'widget-context') ), null, __('than', 'widget-context') ),
readme.txt CHANGED
@@ -3,8 +3,8 @@
3
  Contributors: kasparsd, jamescollins
4
  Tags: widget, widget context, context, logic, widget logic, cms
5
  Requires at least: 3.0
6
- Tested up to: 4.0
7
- Stable tag: 1.0.3
8
  License: GPLv2 or later
9
 
10
  Show or hide widgets on specific posts, pages or sections of your site.
@@ -17,7 +17,6 @@ Widget Context allows you to show or hide widgets on certain sections of your si
17
 
18
  = Get Involved: =
19
 
20
- * News and updates on [kaspars.net](http://kaspars.net/blog),
21
  * Development and pull requests [on GitHub](https://github.com/kasparsd/widget-context-wporg),
22
  * Bug reports and suggestions on [WordPress.org forums](http://wordpress.org/support/plugin/widget-context).
23
 
@@ -30,6 +29,12 @@ Widget Context allows you to show or hide widgets on certain sections of your si
30
 
31
  == Changelog ==
32
 
 
 
 
 
 
 
33
  = 1.0.3 =
34
  * Include Russian translation (Thanks Flector!).
35
  * Add textdomain to the remaining strings.
@@ -146,4 +151,3 @@ Performance improvements - don't check if sidebar has any widgets on every widge
146
  == Screenshots ==
147
 
148
  1. Widget Context settings at the bottom of every widget
149
-
3
  Contributors: kasparsd, jamescollins
4
  Tags: widget, widget context, context, logic, widget logic, cms
5
  Requires at least: 3.0
6
+ Tested up to: 4.5.2
7
+ Stable tag: 1.0.4
8
  License: GPLv2 or later
9
 
10
  Show or hide widgets on specific posts, pages or sections of your site.
17
 
18
  = Get Involved: =
19
 
 
20
  * Development and pull requests [on GitHub](https://github.com/kasparsd/widget-context-wporg),
21
  * Bug reports and suggestions on [WordPress.org forums](http://wordpress.org/support/plugin/widget-context).
22
 
29
 
30
  == Changelog ==
31
 
32
+ = 1.0.4 (May 6, 2016) =
33
+ * Confirm the plugin works with the latest version of WordPress.
34
+ * Fix the PHP class constructor warning.
35
+ * Move the widget context settings link.
36
+ * Fix the initial context state in the customizer.
37
+
38
  = 1.0.3 =
39
  * Include Russian translation (Thanks Flector!).
40
  * Add textdomain to the remaining strings.
151
  == Screenshots ==
152
 
153
  1. Widget Context settings at the bottom of every widget
 
widget-context.php CHANGED
@@ -3,9 +3,9 @@
3
  Plugin Name: Widget Context
4
  Plugin URI: https://wordpress.org/plugins/widget-context/
5
  Description: Show or hide widgets depending on the section of the site that is being viewed.
6
- Version: 1.0.3
7
  Author: Kaspars Dambis
8
- Author URI: http://kaspars.net
9
  Text Domain: widget-context
10
  */
11
 
@@ -13,7 +13,8 @@ Text Domain: widget-context
13
  widget_context::instance();
14
 
15
  class widget_context {
16
-
 
17
  private $sidebars_widgets;
18
  private $options_name = 'widget_logic_options'; // Context settings for widgets (visibility, etc)
19
  private $settings_name = 'widget_context_settings'; // Widget Context global settings
@@ -29,7 +30,7 @@ class widget_context {
29
  private $contexts = array();
30
  private $plugin_path;
31
 
32
-
33
  static function instance() {
34
 
35
  static $instance;
@@ -41,28 +42,25 @@ class widget_context {
41
 
42
  }
43
 
44
-
45
- private function widget_context() {
46
 
47
  // Define available widget contexts
48
  add_action( 'init', array( $this, 'define_widget_contexts' ), 5 );
49
 
50
- // Load plugin settings and show/hide widgets by altering the
51
  // $sidebars_widgets global variable
52
  add_action( 'wp', array( $this, 'set_widget_contexts_frontend' ) );
53
 
54
  // Enable localization
55
  add_action( 'plugins_loaded', array( $this, 'init_l10n' ) );
56
-
57
  // Append Widget Context settings to widget controls
58
  add_action( 'in_widget_form', array( $this, 'widget_context_controls' ), 10, 3 );
59
 
60
  // Add admin menu for config
61
  add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
62
 
63
- // Style things based on admin color scheme
64
- add_action( 'admin_footer', array( $this, 'admin_styles_inline' ) );
65
-
66
  // Save widget context settings, when in admin area
67
  add_action( 'sidebar_admin_setup', array( $this, 'save_widget_context_settings' ) );
68
 
@@ -84,13 +82,13 @@ class widget_context {
84
 
85
  function define_widget_contexts() {
86
 
87
- $this->context_options = apply_filters(
88
- 'widget_context_options',
89
- (array) get_option( $this->options_name, array() )
90
  );
91
 
92
- $this->context_settings = wp_parse_args(
93
- (array) get_option( $this->settings_name, array() ),
94
  array(
95
  'contexts' => array()
96
  )
@@ -175,7 +173,7 @@ class widget_context {
175
 
176
  return $this->contexts;
177
 
178
- }
179
 
180
 
181
  function sort_context_by_weight( $a, $b ) {
@@ -204,44 +202,27 @@ class widget_context {
204
  load_plugin_textdomain( 'widget-context', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
205
 
206
  }
207
-
208
-
209
  function admin_scripts( $page ) {
210
 
211
  // Enqueue only on widgets and customizer view
212
  if ( ! in_array( $page, array( 'widgets.php', 'settings_page_widget_context_settings' ) ) )
213
  return;
214
 
215
- wp_enqueue_style(
216
- 'widget-context-css',
217
- plugins_url( 'css/admin.css', plugin_basename( __FILE__ ) )
 
 
218
  );
219
 
220
- wp_enqueue_script(
221
- 'widget-context-js',
222
- plugins_url( 'js/widget-context.js', plugin_basename( __FILE__ ) ),
223
- array( 'jquery' )
 
224
  );
225
-
226
- }
227
-
228
-
229
- function admin_styles_inline() {
230
-
231
- global $_wp_admin_css_colors;
232
-
233
- $color_scheme = get_user_option( 'admin_color' );
234
-
235
- if ( isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
236
-
237
- printf(
238
- '<style type="text/css">
239
- .widget-context .widget-context-header h3:before { color:%1$s; }
240
- </style>',
241
- $_wp_admin_css_colors[ $color_scheme ]->colors[3]
242
- );
243
-
244
- }
245
 
246
  }
247
 
@@ -252,16 +233,16 @@ class widget_context {
252
 
253
  }
254
 
255
-
256
  function save_widget_context_settings() {
257
 
258
  if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['wl'] ) )
259
  return;
260
-
261
  // Delete a widget
262
  if ( isset( $_POST['delete_widget'] ) && isset( $_POST['the-widget-id'] ) )
263
  unset( $this->context_options[ $_POST['the-widget-id'] ] );
264
-
265
  // Add / Update
266
  $this->context_options = array_merge( $this->context_options, $_POST['wl'] );
267
 
@@ -297,12 +278,12 @@ class widget_context {
297
  // Store a local copy of the original widget location
298
  $this->sidebars_widgets_copy = $sidebars_widgets;
299
 
300
- foreach( $sidebars_widgets as $widget_area => $widget_list ) {
301
 
302
- if ( $widget_area == 'wp_inactive_widgets' || empty( $widget_list ) )
303
  continue;
304
 
305
- foreach( $widget_list as $pos => $widget_id ) {
306
 
307
  if ( ! $this->check_widget_visibility( $widget_id ) )
308
  unset( $sidebars_widgets[ $widget_area ][ $pos ] );
@@ -318,7 +299,7 @@ class widget_context {
318
 
319
  }
320
 
321
-
322
  function check_widget_visibility( $widget_id ) {
323
 
324
  // Check if this widget even has context set
@@ -339,9 +320,9 @@ class widget_context {
339
  else
340
  $widget_context_args = $this->context_options[ $widget_id ][ $context_id ];
341
 
342
- $matches[ $context_id ] = apply_filters(
343
- 'widget_context_check-' . $context_id,
344
- null,
345
  $widget_context_args
346
  );
347
 
@@ -427,18 +408,18 @@ class widget_context {
427
  if ( empty( $urls ) )
428
  return $check;
429
 
430
- if ( $this->match_path( $urls ) )
431
  return true;
432
 
433
  return $check;
434
 
435
  }
436
 
437
-
438
  function match_path( $patterns ) {
439
 
440
  global $wp;
441
-
442
  $patterns_safe = array();
443
 
444
  // Get the request URI from WP
@@ -468,7 +449,7 @@ class widget_context {
468
  // Remove empty patterns
469
  $patterns_safe = array_filter( $patterns_safe );
470
 
471
- $regexps = sprintf(
472
  '/^(%s)$/i',
473
  implode( '|', $patterns_safe )
474
  );
@@ -503,7 +484,7 @@ class widget_context {
503
  sprintf( 'context-group-%s', esc_attr( $context_name ) )
504
  );
505
 
506
- // Hide this context from the admin UX. We can't remove them
507
  // because settings will get lost if this page is submitted.
508
  if ( isset( $this->context_settings['contexts'][ $context_name ] ) && ! $this->context_settings['contexts'][ $context_name ] ) {
509
  $context_classes[] = 'context-inactive';
@@ -526,15 +507,15 @@ class widget_context {
526
  $context_classes = apply_filters( 'widget_context_classes-' . $context_name, $context_classes, $control_args );
527
 
528
  if ( ! empty( $context_controls ) && is_string( $context_controls ) ) {
529
-
530
- $controls[ $context_name ] = sprintf(
531
  '<div class="%s">
532
  <h4 class="context-toggle">%s</h4>
533
  <div class="context-group-wrap">
534
  %s
535
  </div>
536
  </div>',
537
- esc_attr( implode( ' ', $context_classes ) ),
538
  esc_html( $context_settings['label'] ),
539
  $context_controls
540
  );
@@ -555,8 +536,8 @@ class widget_context {
555
 
556
  if ( current_user_can( 'edit_theme_options' ) ) {
557
 
558
- $controls = array( sprintf(
559
- '<p class="error">%s</p>',
560
  sprintf(
561
  __( 'No widget controls enabled. You can enable them in <a href="%s">Widget Context settings</a>.', 'widget-context' ),
562
  admin_url( 'options-general.php?page=widget_context_settings' )
@@ -565,43 +546,40 @@ class widget_context {
565
 
566
  } else {
567
 
568
- $controls = array( sprintf(
569
- '<p class="error">%s</p>',
570
- __( 'No widget controls enabled.', 'widget-context' )
571
  ) );
572
-
573
  }
574
-
575
  }
576
 
 
 
577
  if ( current_user_can( 'edit_theme_options' ) ) {
578
 
579
- $controls[] = sprintf(
580
- '<p class="widget-context-settings-link"><a href="%s">%s</a></p>',
581
- admin_url( 'options-general.php?page=widget_context_settings' ),
582
- __( 'Widget Context Settings', 'widget-context' )
 
583
  );
584
 
585
  }
586
 
587
- return sprintf(
588
  '<div class="widget-context">
589
  <div class="widget-context-header">
590
  <h3>%s</h3>
591
- <!-- <a href="#widget-context-%s" class="toggle-contexts hide-if-no-js">
592
- <span class="expand">%s</span>
593
- <span class="collapse">%s</span>
594
- </a> -->
595
  </div>
596
  <div class="widget-context-inside" id="widget-context-%s" data-widget-id="%s">
597
- %s
598
  </div>
599
  </div>',
600
  __( 'Widget Context', 'widget-context' ),
601
- esc_attr( $widget_id ),
602
- // Toggle buttons
603
- __( 'Expand', 'widget-context' ),
604
- __( 'Collapse', 'widget-context' ),
605
  // Inslide classes
606
  esc_attr( $widget_id ),
607
  esc_attr( $widget_id ),
@@ -615,9 +593,9 @@ class widget_context {
615
  function control_incexc( $control_args ) {
616
 
617
  $options = array(
618
- 'show' => __( 'Show widget everywhere', 'widget-context' ),
619
- 'selected' => __( 'Show widget on selected', 'widget-context' ),
620
- 'notselected' => __( 'Hide widget on selected', 'widget-context' ),
621
  'hide' => __( 'Hide widget everywhere', 'widget-context' )
622
  );
623
 
@@ -625,7 +603,7 @@ class widget_context {
625
 
626
  }
627
 
628
-
629
  function control_location( $control_args ) {
630
 
631
  $options = array(
@@ -657,7 +635,7 @@ class widget_context {
657
 
658
  function control_url( $control_args ) {
659
 
660
- return sprintf(
661
  '<div>%s</div>
662
  <p class="help">%s</p>',
663
  $this->make_simple_textarea( $control_args, 'urls' ),
@@ -669,20 +647,20 @@ class widget_context {
669
 
670
  function control_admin_notes( $control_args ) {
671
 
672
- return sprintf(
673
  '<div>%s</div>',
674
  $this->make_simple_textarea( $control_args, 'notes' )
675
  );
676
 
677
  }
678
 
679
-
680
 
681
  /**
682
  * Widget control helpers
683
  */
684
 
685
-
686
  function make_simple_checkbox( $control_args, $option, $label ) {
687
 
688
  if ( isset( $control_args['settings'][ $option ] ) && $control_args['settings'][ $option ] )
@@ -710,15 +688,15 @@ class widget_context {
710
 
711
  }
712
 
713
-
714
  function make_simple_textarea( $control_args, $option, $label = null ) {
715
 
716
  if ( isset( $control_args['settings'][ $option ] ) )
717
  $value = esc_textarea( $control_args['settings'][ $option ] );
718
  else
719
  $value = '';
720
-
721
- return sprintf(
722
  '<label class="wc-field-textarea-%s" data-widget-id="%s">
723
  <strong>%s</strong>
724
  <textarea name="%s[%s]">%s</textarea>
@@ -743,10 +721,10 @@ class widget_context {
743
  else
744
  $value = false;
745
 
746
- return sprintf(
747
  '<label class="wc-field-text-%s" data-widget-id="%s">
748
- %s
749
- <input type="text" name="%s[%s]" value="%s" />
750
  %s
751
  </label>',
752
  $this->get_field_classname( $option ),
@@ -774,33 +752,33 @@ class widget_context {
774
  $value = false;
775
 
776
  if ( empty( $selection ) )
777
- $options[] = sprintf(
778
- '<option value="">%s</option>',
779
- esc_html__( 'No options available', 'widget-context' )
780
  );
781
 
782
  foreach ( $selection as $sid => $svalue )
783
- $options[] = sprintf(
784
- '<option value="%s" %s>%s</option>',
785
- esc_attr( $sid ),
786
- selected( $value, $sid, false ),
787
- esc_html( $svalue )
788
  );
789
 
790
- return sprintf(
791
  '<label class="wc-field-select-%s" data-widget-id="%s">
792
- %s
793
  <select name="%s[%s]">
794
  %s
795
- </select>
796
  %s
797
  </label>',
798
  $this->get_field_classname( $option ),
799
  esc_attr( $control_args['widget_id'] ),
800
  // Before
801
- $label_before,
802
  // Input
803
- $control_args['input_prefix'],
804
  $option,
805
  implode( '', $options ),
806
  // After
@@ -848,7 +826,7 @@ class widget_context {
848
  return false;
849
 
850
  $part = array_shift( $parts );
851
-
852
  if ( ! empty( $parts ) && isset( $options[ $part ] ) && is_array( $options[ $part ] ) )
853
  $value = $this->get_field_value( $parts, $options[ $part ] );
854
  elseif ( isset( $options[ $part ] ) )
@@ -863,7 +841,7 @@ class widget_context {
863
 
864
  if ( empty( $options ) || ! is_array( $options ) )
865
  return $options;
866
-
867
  foreach ( $options as $widget_id => $option ) {
868
 
869
  // This doesn't have an include/exclude rule defined
@@ -873,7 +851,7 @@ class widget_context {
873
  // We moved from [incexc] = 1/0 to [incexc][condition] = 1/0
874
  if ( isset( $option['incexc'] ) && ! is_array( $option['incexc'] ) )
875
  $options[ $widget_id ]['incexc'] = array( 'condition' => $option['incexc'] );
876
-
877
  // Move notes from "general" group to "admin_notes"
878
  if ( isset( $option['general']['notes'] ) ) {
879
  $options[ $widget_id ]['admin_notes']['notes'] = $option['general']['notes'];
@@ -887,7 +865,7 @@ class widget_context {
887
  'check_wordcount_type' => $option['location']['check_wordcount_type'],
888
  'word_count' => $option['location']['word_count']
889
  );
890
-
891
  }
892
 
893
  return $options;
@@ -903,11 +881,11 @@ class widget_context {
903
 
904
  function widget_context_settings_menu() {
905
 
906
- add_options_page(
907
- __( 'Widget Context Settings', 'widget-context' ),
908
- __( 'Widget Context', 'widget-context' ),
909
- 'manage_options',
910
- $this->settings_name,
911
  array( $this, 'widget_context_admin_view' )
912
  );
913
 
@@ -926,15 +904,15 @@ class widget_context {
926
  $context_controls = array();
927
 
928
  foreach ( $this->get_contexts() as $context_id => $context_args ) {
929
-
930
  // Hide core modules from being disabled
931
  if ( isset( $context_args['type'] ) && $context_args['type'] == 'core' )
932
  continue;
933
 
934
  if ( ! empty( $context_args['description'] ) )
935
- $context_description = sprintf(
936
- '<p class="context-desc">%s</p>',
937
- esc_html( $context_args['description'] )
938
  );
939
  else
940
  $context_description = null;
@@ -977,7 +955,7 @@ class widget_context {
977
  ?>
978
 
979
  <?php
980
- printf(
981
  '<div class="settings-section settings-section-modules">
982
  <h3>%s</h3>
983
  <ul>%s</ul>
@@ -986,7 +964,7 @@ class widget_context {
986
  implode( '', $context_controls )
987
  );
988
  ?>
989
-
990
  <?php
991
  submit_button();
992
  ?>
@@ -1025,7 +1003,7 @@ class widget_context {
1025
 
1026
 
1027
  public function get_sidebars_widgets_copy() {
1028
-
1029
  return $this->sidebars_widgets_copy;
1030
 
1031
  }
@@ -1045,15 +1023,13 @@ class widget_context {
1045
 
1046
  function widget_context_debug_bar_scripts() {
1047
 
1048
- wp_enqueue_script(
1049
- 'widget-context-debug-js',
1050
- plugins_url( 'debug/debug.js', plugin_basename( __FILE__ ) ),
1051
- array( 'jquery' )
1052
  );
1053
 
1054
  }
1055
 
1056
 
1057
  }
1058
-
1059
-
3
  Plugin Name: Widget Context
4
  Plugin URI: https://wordpress.org/plugins/widget-context/
5
  Description: Show or hide widgets depending on the section of the site that is being viewed.
6
+ Version: 1.0.4
7
  Author: Kaspars Dambis
8
+ Author URI: https://kaspars.net
9
  Text Domain: widget-context
10
  */
11
 
13
  widget_context::instance();
14
 
15
  class widget_context {
16
+
17
+ private $asset_version = '1.0.4';
18
  private $sidebars_widgets;
19
  private $options_name = 'widget_logic_options'; // Context settings for widgets (visibility, etc)
20
  private $settings_name = 'widget_context_settings'; // Widget Context global settings
30
  private $contexts = array();
31
  private $plugin_path;
32
 
33
+
34
  static function instance() {
35
 
36
  static $instance;
42
 
43
  }
44
 
45
+
46
+ private function __construct() {
47
 
48
  // Define available widget contexts
49
  add_action( 'init', array( $this, 'define_widget_contexts' ), 5 );
50
 
51
+ // Load plugin settings and show/hide widgets by altering the
52
  // $sidebars_widgets global variable
53
  add_action( 'wp', array( $this, 'set_widget_contexts_frontend' ) );
54
 
55
  // Enable localization
56
  add_action( 'plugins_loaded', array( $this, 'init_l10n' ) );
57
+
58
  // Append Widget Context settings to widget controls
59
  add_action( 'in_widget_form', array( $this, 'widget_context_controls' ), 10, 3 );
60
 
61
  // Add admin menu for config
62
  add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
63
 
 
 
 
64
  // Save widget context settings, when in admin area
65
  add_action( 'sidebar_admin_setup', array( $this, 'save_widget_context_settings' ) );
66
 
82
 
83
  function define_widget_contexts() {
84
 
85
+ $this->context_options = apply_filters(
86
+ 'widget_context_options',
87
+ (array) get_option( $this->options_name, array() )
88
  );
89
 
90
+ $this->context_settings = wp_parse_args(
91
+ (array) get_option( $this->settings_name, array() ),
92
  array(
93
  'contexts' => array()
94
  )
173
 
174
  return $this->contexts;
175
 
176
+ }
177
 
178
 
179
  function sort_context_by_weight( $a, $b ) {
202
  load_plugin_textdomain( 'widget-context', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
203
 
204
  }
205
+
206
+
207
  function admin_scripts( $page ) {
208
 
209
  // Enqueue only on widgets and customizer view
210
  if ( ! in_array( $page, array( 'widgets.php', 'settings_page_widget_context_settings' ) ) )
211
  return;
212
 
213
+ wp_enqueue_style(
214
+ 'widget-context-css',
215
+ plugins_url( 'css/admin.css', plugin_basename( __FILE__ ) ),
216
+ null,
217
+ $this->asset_version
218
  );
219
 
220
+ wp_enqueue_script(
221
+ 'widget-context-js',
222
+ plugins_url( 'js/widget-context.js', plugin_basename( __FILE__ ) ),
223
+ array( 'jquery' ),
224
+ $this->asset_version
225
  );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  }
228
 
233
 
234
  }
235
 
236
+
237
  function save_widget_context_settings() {
238
 
239
  if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['wl'] ) )
240
  return;
241
+
242
  // Delete a widget
243
  if ( isset( $_POST['delete_widget'] ) && isset( $_POST['the-widget-id'] ) )
244
  unset( $this->context_options[ $_POST['the-widget-id'] ] );
245
+
246
  // Add / Update
247
  $this->context_options = array_merge( $this->context_options, $_POST['wl'] );
248
 
278
  // Store a local copy of the original widget location
279
  $this->sidebars_widgets_copy = $sidebars_widgets;
280
 
281
+ foreach ( $sidebars_widgets as $widget_area => $widget_list ) {
282
 
283
+ if ( $widget_area == 'wp_inactive_widgets' || empty( $widget_list ) )
284
  continue;
285
 
286
+ foreach ( $widget_list as $pos => $widget_id ) {
287
 
288
  if ( ! $this->check_widget_visibility( $widget_id ) )
289
  unset( $sidebars_widgets[ $widget_area ][ $pos ] );
299
 
300
  }
301
 
302
+
303
  function check_widget_visibility( $widget_id ) {
304
 
305
  // Check if this widget even has context set
320
  else
321
  $widget_context_args = $this->context_options[ $widget_id ][ $context_id ];
322
 
323
+ $matches[ $context_id ] = apply_filters(
324
+ 'widget_context_check-' . $context_id,
325
+ null,
326
  $widget_context_args
327
  );
328
 
408
  if ( empty( $urls ) )
409
  return $check;
410
 
411
+ if ( $this->match_path( $urls ) )
412
  return true;
413
 
414
  return $check;
415
 
416
  }
417
 
418
+
419
  function match_path( $patterns ) {
420
 
421
  global $wp;
422
+
423
  $patterns_safe = array();
424
 
425
  // Get the request URI from WP
449
  // Remove empty patterns
450
  $patterns_safe = array_filter( $patterns_safe );
451
 
452
+ $regexps = sprintf(
453
  '/^(%s)$/i',
454
  implode( '|', $patterns_safe )
455
  );
484
  sprintf( 'context-group-%s', esc_attr( $context_name ) )
485
  );
486
 
487
+ // Hide this context from the admin UX. We can't remove them
488
  // because settings will get lost if this page is submitted.
489
  if ( isset( $this->context_settings['contexts'][ $context_name ] ) && ! $this->context_settings['contexts'][ $context_name ] ) {
490
  $context_classes[] = 'context-inactive';
507
  $context_classes = apply_filters( 'widget_context_classes-' . $context_name, $context_classes, $control_args );
508
 
509
  if ( ! empty( $context_controls ) && is_string( $context_controls ) ) {
510
+
511
+ $controls[ $context_name ] = sprintf(
512
  '<div class="%s">
513
  <h4 class="context-toggle">%s</h4>
514
  <div class="context-group-wrap">
515
  %s
516
  </div>
517
  </div>',
518
+ esc_attr( implode( ' ', $context_classes ) ),
519
  esc_html( $context_settings['label'] ),
520
  $context_controls
521
  );
536
 
537
  if ( current_user_can( 'edit_theme_options' ) ) {
538
 
539
+ $controls = array( sprintf(
540
+ '<p class="error">%s</p>',
541
  sprintf(
542
  __( 'No widget controls enabled. You can enable them in <a href="%s">Widget Context settings</a>.', 'widget-context' ),
543
  admin_url( 'options-general.php?page=widget_context_settings' )
546
 
547
  } else {
548
 
549
+ $controls = array( sprintf(
550
+ '<p class="error">%s</p>',
551
+ __( 'No widget controls enabled.', 'widget-context' )
552
  ) );
553
+
554
  }
555
+
556
  }
557
 
558
+ $settings_link = '';
559
+
560
  if ( current_user_can( 'edit_theme_options' ) ) {
561
 
562
+ $settings_link = sprintf(
563
+ '<a href="%s" class="widget-context-settings-link" title="%s">%s</a>',
564
+ admin_url( 'options-general.php?page=widget_context_settings' ),
565
+ esc_attr__( 'Widget Context Settings', 'widget-context' ),
566
+ esc_html__( 'Settings', 'widget-context' )
567
  );
568
 
569
  }
570
 
571
+ return sprintf(
572
  '<div class="widget-context">
573
  <div class="widget-context-header">
574
  <h3>%s</h3>
575
+ %s
 
 
 
576
  </div>
577
  <div class="widget-context-inside" id="widget-context-%s" data-widget-id="%s">
578
+ %s
579
  </div>
580
  </div>',
581
  __( 'Widget Context', 'widget-context' ),
582
+ $settings_link,
 
 
 
583
  // Inslide classes
584
  esc_attr( $widget_id ),
585
  esc_attr( $widget_id ),
593
  function control_incexc( $control_args ) {
594
 
595
  $options = array(
596
+ 'show' => __( 'Show widget everywhere', 'widget-context' ),
597
+ 'selected' => __( 'Show widget on selected', 'widget-context' ),
598
+ 'notselected' => __( 'Hide widget on selected', 'widget-context' ),
599
  'hide' => __( 'Hide widget everywhere', 'widget-context' )
600
  );
601
 
603
 
604
  }
605
 
606
+
607
  function control_location( $control_args ) {
608
 
609
  $options = array(
635
 
636
  function control_url( $control_args ) {
637
 
638
+ return sprintf(
639
  '<div>%s</div>
640
  <p class="help">%s</p>',
641
  $this->make_simple_textarea( $control_args, 'urls' ),
647
 
648
  function control_admin_notes( $control_args ) {
649
 
650
+ return sprintf(
651
  '<div>%s</div>',
652
  $this->make_simple_textarea( $control_args, 'notes' )
653
  );
654
 
655
  }
656
 
657
+
658
 
659
  /**
660
  * Widget control helpers
661
  */
662
 
663
+
664
  function make_simple_checkbox( $control_args, $option, $label ) {
665
 
666
  if ( isset( $control_args['settings'][ $option ] ) && $control_args['settings'][ $option ] )
688
 
689
  }
690
 
691
+
692
  function make_simple_textarea( $control_args, $option, $label = null ) {
693
 
694
  if ( isset( $control_args['settings'][ $option ] ) )
695
  $value = esc_textarea( $control_args['settings'][ $option ] );
696
  else
697
  $value = '';
698
+
699
+ return sprintf(
700
  '<label class="wc-field-textarea-%s" data-widget-id="%s">
701
  <strong>%s</strong>
702
  <textarea name="%s[%s]">%s</textarea>
721
  else
722
  $value = false;
723
 
724
+ return sprintf(
725
  '<label class="wc-field-text-%s" data-widget-id="%s">
726
+ %s
727
+ <input type="text" name="%s[%s]" value="%s" />
728
  %s
729
  </label>',
730
  $this->get_field_classname( $option ),
752
  $value = false;
753
 
754
  if ( empty( $selection ) )
755
+ $options[] = sprintf(
756
+ '<option value="">%s</option>',
757
+ esc_html__( 'No options available', 'widget-context' )
758
  );
759
 
760
  foreach ( $selection as $sid => $svalue )
761
+ $options[] = sprintf(
762
+ '<option value="%s" %s>%s</option>',
763
+ esc_attr( $sid ),
764
+ selected( $value, $sid, false ),
765
+ esc_html( $svalue )
766
  );
767
 
768
+ return sprintf(
769
  '<label class="wc-field-select-%s" data-widget-id="%s">
770
+ %s
771
  <select name="%s[%s]">
772
  %s
773
+ </select>
774
  %s
775
  </label>',
776
  $this->get_field_classname( $option ),
777
  esc_attr( $control_args['widget_id'] ),
778
  // Before
779
+ $label_before,
780
  // Input
781
+ $control_args['input_prefix'],
782
  $option,
783
  implode( '', $options ),
784
  // After
826
  return false;
827
 
828
  $part = array_shift( $parts );
829
+
830
  if ( ! empty( $parts ) && isset( $options[ $part ] ) && is_array( $options[ $part ] ) )
831
  $value = $this->get_field_value( $parts, $options[ $part ] );
832
  elseif ( isset( $options[ $part ] ) )
841
 
842
  if ( empty( $options ) || ! is_array( $options ) )
843
  return $options;
844
+
845
  foreach ( $options as $widget_id => $option ) {
846
 
847
  // This doesn't have an include/exclude rule defined
851
  // We moved from [incexc] = 1/0 to [incexc][condition] = 1/0
852
  if ( isset( $option['incexc'] ) && ! is_array( $option['incexc'] ) )
853
  $options[ $widget_id ]['incexc'] = array( 'condition' => $option['incexc'] );
854
+
855
  // Move notes from "general" group to "admin_notes"
856
  if ( isset( $option['general']['notes'] ) ) {
857
  $options[ $widget_id ]['admin_notes']['notes'] = $option['general']['notes'];
865
  'check_wordcount_type' => $option['location']['check_wordcount_type'],
866
  'word_count' => $option['location']['word_count']
867
  );
868
+
869
  }
870
 
871
  return $options;
881
 
882
  function widget_context_settings_menu() {
883
 
884
+ add_options_page(
885
+ __( 'Widget Context Settings', 'widget-context' ),
886
+ __( 'Widget Context', 'widget-context' ),
887
+ 'manage_options',
888
+ $this->settings_name,
889
  array( $this, 'widget_context_admin_view' )
890
  );
891
 
904
  $context_controls = array();
905
 
906
  foreach ( $this->get_contexts() as $context_id => $context_args ) {
907
+
908
  // Hide core modules from being disabled
909
  if ( isset( $context_args['type'] ) && $context_args['type'] == 'core' )
910
  continue;
911
 
912
  if ( ! empty( $context_args['description'] ) )
913
+ $context_description = sprintf(
914
+ '<p class="context-desc">%s</p>',
915
+ esc_html( $context_args['description'] )
916
  );
917
  else
918
  $context_description = null;
955
  ?>
956
 
957
  <?php
958
+ printf(
959
  '<div class="settings-section settings-section-modules">
960
  <h3>%s</h3>
961
  <ul>%s</ul>
964
  implode( '', $context_controls )
965
  );
966
  ?>
967
+
968
  <?php
969
  submit_button();
970
  ?>
1003
 
1004
 
1005
  public function get_sidebars_widgets_copy() {
1006
+
1007
  return $this->sidebars_widgets_copy;
1008
 
1009
  }
1023
 
1024
  function widget_context_debug_bar_scripts() {
1025
 
1026
+ wp_enqueue_script(
1027
+ 'widget-context-debug-js',
1028
+ plugins_url( 'debug/debug.js', plugin_basename( __FILE__ ) ),
1029
+ array( 'jquery' )
1030
  );
1031
 
1032
  }
1033
 
1034
 
1035
  }