Widget Context - Version 0.8.1

Version Description

(1) Revert to a legacy method for attaching widget control settings in order to make it work with old plugins. (2) Fix the word count context logic.

Download this release

Release Info

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

Code changes from version 0.8 to 0.8.1

Files changed (2) hide show
  1. readme.txt +9 -1
  2. widget-context.php +63 -26
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=kaspa
4
  Tags: widget, widget context, context, logic, widget logic, cms
5
  Requires at least: 3.0
6
  Tested up to: 3.5.1
7
- Stable tag: 0.8
8
  License: GPLv2 or later
9
 
10
  Show or hide widgets on specific posts, pages or sections of your site.
@@ -30,6 +30,11 @@ Widget Context allows you to show or hide widgets on certain sections of your si
30
 
31
  == Changelog ==
32
 
 
 
 
 
 
33
  **0.8**
34
 
35
  * Major code rewrite and refactoring to improve performance and usability.
@@ -70,6 +75,9 @@ Widget Context allows you to show or hide widgets on certain sections of your si
70
 
71
  == Upgrade Notice ==
72
 
 
 
 
73
  = 0.8 =
74
  Major code rewrite and refactoring to improve plugin performance and usability.
75
 
4
  Tags: widget, widget context, context, logic, widget logic, cms
5
  Requires at least: 3.0
6
  Tested up to: 3.5.1
7
+ Stable tag: 0.8.1
8
  License: GPLv2 or later
9
 
10
  Show or hide widgets on specific posts, pages or sections of your site.
30
 
31
  == Changelog ==
32
 
33
+ **0.8.1**
34
+
35
+ * Revert back to changing callback function in `$wp_registered_widgets` for attaching widget context setting controls.
36
+ * Fix the word count logic.
37
+
38
  **0.8**
39
 
40
  * Major code rewrite and refactoring to improve performance and usability.
75
 
76
  == Upgrade Notice ==
77
 
78
+ = 0.8.1 =
79
+ (1) Revert to a legacy method for attaching widget control settings in order to make it work with old plugins. (2) Fix the word count context logic.
80
+
81
  = 0.8 =
82
  Major code rewrite and refactoring to improve plugin performance and usability.
83
 
widget-context.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Widget Context
4
  Plugin URI: http://wordpress.org/extend/plugins/widget-context/
5
  Description: Display widgets in context.
6
- Version: 0.8
7
  Author: Kaspars Dambis
8
  Author URI: http://konstruktors.com/
9
 
@@ -39,7 +39,7 @@ class widget_context {
39
  // Load plugin settings
40
  add_action( 'init', array( $this, 'load_plugin_settings' ) );
41
  // Amend widget controls with Widget Context controls
42
- add_action( 'in_widget_form', array( $this, 'display_widget_context' ), 10, 3 );
43
  // Hide the widget if necessary
44
  add_filter( 'widget_display_callback', array( $this, 'maybe_hide_widget' ), 10, 3 );
45
  // Add admin menu for config
@@ -57,13 +57,13 @@ class widget_context {
57
  if ( ! is_array( $this->context_options ) || empty( $this->context_options ) )
58
  $this->context_options = array();
59
  }
 
60
 
61
-
62
  function admin_scripts() {
63
  wp_enqueue_style( 'widget-context-admin', WP_CONTENT_URL . '/plugins/'. basename(__DIR__) . '/admin-style.css' );
64
  }
65
 
66
-
67
  function save_widget_context_settings() {
68
  if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['sidebar'] ) || empty( $_POST['sidebar'] ) )
69
  return;
@@ -77,6 +77,19 @@ class widget_context {
77
 
78
  update_option( $this->options_name, $this->context_options );
79
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
 
82
  function maybe_hide_widget( $instance, $widget_object, $args ) {
@@ -85,6 +98,28 @@ class widget_context {
85
 
86
  return $instance;
87
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
 
90
  function get_current_url() {
@@ -181,6 +216,8 @@ class widget_context {
181
 
182
  if ( $this->words_on_page > $word_count_to_check && $check_type == 'more' )
183
  $do_show_by_word_count = true;
 
 
184
  else
185
  $do_show_by_word_count = false;
186
  }
@@ -206,44 +243,44 @@ class widget_context {
206
 
207
  return $do_show;
208
  }
209
-
210
-
211
- function display_widget_context( $form, $return, $instance ) {
212
-
213
- echo '<div class="widget-context"><div class="widget-context-inside">'
214
  . '<p class="wl-visibility">'
215
- . $this->make_simple_dropdown( array( $form->id, 'incexc' ), array( 'show' => __('Show everywhere'), 'selected' => __('Show on selected'), 'notselected' => __('Hide on selected'), 'hide' => __('Hide everywhere') ), sprintf( '<strong>%s</strong>', __( 'Widget Context' ) ) )
216
  . '</p>'
217
 
218
  . '<div class="wl-columns">'
219
  . '<div class="wl-column-2-1"><p>'
220
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_front_page' ), __('Front Page') )
221
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_home' ), __('Blog Index') )
222
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_single' ), __('All Posts') )
223
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_page' ), __('All Pages') )
224
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_attachment' ), __('All Attachments') )
225
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_search' ), __('Search') )
226
  . '</p></div>'
227
  . '<div class="wl-column-2-2"><p>'
228
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_archive' ), __('All Archives') )
229
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_category' ), __('Category Archive') )
230
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_tag' ), __('Tag Archive') )
231
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_author' ), __('Author Archive') )
232
- . $this->make_simple_checkbox( array( $form->id, 'location', 'is_404' ), __('404 Error') )
233
  . '</p></div>'
234
 
235
  . '<div class="wl-word-count"><p>'
236
- . $this->make_simple_checkbox( array( $form->id, 'location', 'check_wordcount' ), __('Has') )
237
- . $this->make_simple_dropdown( array( $form->id, 'location', 'check_wordcount_type' ), array('less' => __('less'), 'more' => __('more')), '', __('than') )
238
- . $this->make_simple_textfield( array( $form->id, 'location', 'word_count' ), __('words') )
239
  . '</p></div>'
240
  . '</div>'
241
 
242
  . '<div class="wl-options">'
243
- . $this->make_simple_textarea( array( $form->id, 'url', 'urls' ), __('or target by URL'), __('Enter one location fragment per line. Use <code>*</code> character as a wildcard. Don\'t include the domain name.'))
244
  . '</div>'
245
 
246
- . $this->make_simple_textarea( array( $form->id, 'general', 'notes' ), __('Notes (invisible to public)'))
247
  . '</div></div>';
248
 
249
  }
3
  Plugin Name: Widget Context
4
  Plugin URI: http://wordpress.org/extend/plugins/widget-context/
5
  Description: Display widgets in context.
6
+ Version: 0.8.1
7
  Author: Kaspars Dambis
8
  Author URI: http://konstruktors.com/
9
 
39
  // Load plugin settings
40
  add_action( 'init', array( $this, 'load_plugin_settings' ) );
41
  // Amend widget controls with Widget Context controls
42
+ add_action( 'sidebar_admin_setup', array( $this, 'attach_widget_context_controls' ) );
43
  // Hide the widget if necessary
44
  add_filter( 'widget_display_callback', array( $this, 'maybe_hide_widget' ), 10, 3 );
45
  // Add admin menu for config
57
  if ( ! is_array( $this->context_options ) || empty( $this->context_options ) )
58
  $this->context_options = array();
59
  }
60
+
61
 
 
62
  function admin_scripts() {
63
  wp_enqueue_style( 'widget-context-admin', WP_CONTENT_URL . '/plugins/'. basename(__DIR__) . '/admin-style.css' );
64
  }
65
 
66
+
67
  function save_widget_context_settings() {
68
  if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['sidebar'] ) || empty( $_POST['sidebar'] ) )
69
  return;
77
 
78
  update_option( $this->options_name, $this->context_options );
79
  }
80
+
81
+
82
+ function attach_widget_context_controls() {
83
+ global $wp_registered_widget_controls, $wp_registered_widgets;
84
+
85
+ foreach ($wp_registered_widgets as $widget_id => $widget_data) {
86
+ // Pass widget id as param, so that we can later call the original callback function
87
+ $wp_registered_widget_controls[$widget_id]['params'][]['widget_id'] = $widget_id;
88
+ // Store the original callback functions and replace them with Widget Context
89
+ $wp_registered_widget_controls[$widget_id]['callback_original_wc'] = $wp_registered_widget_controls[$widget_id]['callback'];
90
+ $wp_registered_widget_controls[$widget_id]['callback'] = array($this, 'replace_widget_control_callback');
91
+ }
92
+ }
93
 
94
 
95
  function maybe_hide_widget( $instance, $widget_object, $args ) {
98
 
99
  return $instance;
100
  }
101
+
102
+
103
+ function replace_widget_control_callback() {
104
+ global $wp_registered_widget_controls;
105
+
106
+ $all_params = func_get_args();
107
+ if (is_array($all_params[1]))
108
+ $widget_id = $all_params[1]['widget_id'];
109
+ else
110
+ $widget_id = $all_params[0]['widget_id'];
111
+
112
+ $original_callback = $wp_registered_widget_controls[$widget_id]['callback_original_wc'];
113
+
114
+ // Display the original callback
115
+ if (isset($original_callback) && is_callable($original_callback)) {
116
+ call_user_func_array($original_callback, $all_params);
117
+ } else {
118
+ print '<!-- widget context [controls]: could not call the original callback function -->';
119
+ }
120
+
121
+ print $this->display_widget_context( $widget_id );
122
+ }
123
 
124
 
125
  function get_current_url() {
216
 
217
  if ( $this->words_on_page > $word_count_to_check && $check_type == 'more' )
218
  $do_show_by_word_count = true;
219
+ elseif ( $this->words_on_page < $word_count_to_check && $check_type == 'less' )
220
+ $do_show_by_word_count = true;
221
  else
222
  $do_show_by_word_count = false;
223
  }
243
 
244
  return $do_show;
245
  }
246
+
247
+
248
+ function display_widget_context( $wid = null ) {
249
+
250
+ return '<div class="widget-context"><div class="widget-context-inside">'
251
  . '<p class="wl-visibility">'
252
+ . $this->make_simple_dropdown( array( $wid, 'incexc' ), array( 'show' => __('Show everywhere'), 'selected' => __('Show on selected'), 'notselected' => __('Hide on selected'), 'hide' => __('Hide everywhere') ), sprintf( '<strong>%s</strong>', __( 'Widget Context' ) ) )
253
  . '</p>'
254
 
255
  . '<div class="wl-columns">'
256
  . '<div class="wl-column-2-1"><p>'
257
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_front_page' ), __('Front Page') )
258
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_home' ), __('Blog Index') )
259
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_single' ), __('All Posts') )
260
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_page' ), __('All Pages') )
261
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_attachment' ), __('All Attachments') )
262
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_search' ), __('Search') )
263
  . '</p></div>'
264
  . '<div class="wl-column-2-2"><p>'
265
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_archive' ), __('All Archives') )
266
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_category' ), __('Category Archive') )
267
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_tag' ), __('Tag Archive') )
268
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_author' ), __('Author Archive') )
269
+ . $this->make_simple_checkbox( array( $wid, 'location', 'is_404' ), __('404 Error') )
270
  . '</p></div>'
271
 
272
  . '<div class="wl-word-count"><p>'
273
+ . $this->make_simple_checkbox( array( $wid, 'location', 'check_wordcount' ), __('Has') )
274
+ . $this->make_simple_dropdown( array( $wid, 'location', 'check_wordcount_type' ), array('less' => __('less'), 'more' => __('more')), '', __('than') )
275
+ . $this->make_simple_textfield( array( $wid, 'location', 'word_count' ), __('words') )
276
  . '</p></div>'
277
  . '</div>'
278
 
279
  . '<div class="wl-options">'
280
+ . $this->make_simple_textarea( array( $wid, 'url', 'urls' ), __('or target by URL'), __('Enter one location fragment per line. Use <strong>*</strong> character as a wildcard. Example: <code>category/peace/*</code> to target all posts in category <em>peace</em>.') )
281
  . '</div>'
282
 
283
+ . $this->make_simple_textarea( array( $wid, 'general', 'notes' ), __('Notes (invisible to public)'))
284
  . '</div></div>';
285
 
286
  }