PluginProbe
Widget Context / 0.8
Widget Context v0.8
1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 trunk 0.4.1 0.4.2 0.4.3 0.4.4 0.4.5 0.6 0.7 0.7.1 0.7.2 0.8 0.8.1 All 30 releases
widget-context / widget-context.php

widget-context.php in Widget Context 0.8, at widget-context.php

369 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
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
10 For changelog see readme.txt
11 ----------------------------
12
13 Copyright 2009 Kaspars Dambis (email: kaspars@konstruktors.com)
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation; either version 2 of the License, or
18 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27
28 // Go!
29 new widget_context();
30
31 class widget_context {
32
33 var $options_name = 'widget_logic_options'; // Widget context settings (visibility, etc)
34 var $context_options = array();
35 var $words_on_page = 0;
36
37
38 function 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
46 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
47 // Save widget context settings, when in admin area
48 add_filter( 'admin_init', array( $this, 'save_widget_context_settings' ) );
49 // Check the number of words on page
50 add_action( 'wp', array( $this, 'count_words_on_page' ) );
51 }
52
53
54 function load_plugin_settings() {
55 $this->context_options = get_option( $this->options_name );
56
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;
70
71 // Delete
72 if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] )
73 unset( $this->context_options[ $_POST['widget-id'] ] );
74
75 // Add / Update
76 $this->context_options = array_merge( $this->context_options, $_POST['wl'] );
77
78 update_option( $this->options_name, $this->context_options );
79 }
80
81
82 function maybe_hide_widget( $instance, $widget_object, $args ) {
83 if ( ! $this->check_widget_visibility( $args['widget_id'] ) )
84 return false;
85
86 return $instance;
87 }
88
89
90 function get_current_url() {
91 if ($_SERVER['REQUEST_URI'] == '')
92 $uri = $_SERVER['REDIRECT_URL'];
93 else
94 $uri = $_SERVER['REQUEST_URI'];
95
96 return (!empty($_SERVER['HTTPS']))
97 ? "https://".$_SERVER['SERVER_NAME'].$uri
98 : "http://".$_SERVER['SERVER_NAME'].$uri;
99 }
100
101
102 // Thanks to Drupal: http://api.drupal.org/api/function/drupal_match_path/6
103 function match_path( $path, $patterns ) {
104 $patterns_safe = array();
105
106 // Strip home url and check only the REQUEST_URI part
107 $path = trim( str_replace( trailingslashit( get_bloginfo('url') ), '', $path ), '/' );
108
109 foreach ( explode( "\n", $patterns ) as $pattern )
110 $patterns_safe[] = trim( trim( $pattern ), '/' );
111
112 $regexps = '/^('. preg_replace( array( '/(\r\n|\n| )+/', '/\\\\\*/' ), array( '|', '.*' ), preg_quote( implode( "\n", array_filter( $patterns_safe, 'trim' ) ), '/' ) ) .')$/';
113
114 // Debug
115 //echo $regexps;
116 //print_r(array_filter( $patterns_safe, 'trim' ));
117
118 return preg_match( $regexps, $path );
119 }
120
121
122 function count_words_on_page() {
123 global $wp_query;
124
125 if ( empty( $wp_query->posts ) || is_admin() )
126 return;
127
128 foreach ( $wp_query->posts as $post_data )
129 $this->words_on_page += str_word_count( strip_tags( strip_shortcodes( $post_data->post_content ) ) );
130 }
131
132
133 function check_widget_visibility( $widget_id ) {
134 // Show widget because no context settings found
135 if ( ! isset( $this->context_options[ $widget_id ] ) )
136 return true;
137
138 $vis_settings = $this->context_options[ $widget_id ];
139
140 // Hide/show if forced
141 if ( $vis_settings['incexc'] == 'hide' )
142 return false;
143 elseif ( $vis_settings['incexc'] == 'show' )
144 return true;
145
146 $do_show = true;
147 $do_show_by_select = false;
148 $do_show_by_url = false;
149 $do_show_by_word_count = false;
150
151 // Check by current URL
152 if ( ! empty( $vis_settings['url']['urls'] ) )
153 if ( $this->match_path( $this->get_current_url(), $vis_settings['url']['urls'] ) )
154 $do_show_by_url = true;
155
156 // Check by tag settings
157 if ( ! empty( $vis_settings['location'] ) ) {
158 $currently = array();
159
160 if ( is_front_page() && ! is_paged() ) $currently['is_front_page'] = true;
161 if ( is_home() && ! is_paged() ) $currently['is_home'] = true;
162 if ( is_page() && ! is_attachment() ) $currently['is_page'] = true;
163 if ( is_single() && ! is_attachment() ) $currently['is_single'] = true;
164 if ( is_archive() ) $currently['is_archive'] = true;
165 if ( is_category() ) $currently['is_category'] = true;
166 if ( is_tag() ) $currently['is_tag'] = true;
167 if ( is_author() ) $currently['is_author'] = true;
168 if ( is_search() ) $currently['is_search'] = true;
169 if ( is_404() ) $currently['is_404'] = true;
170 if ( is_attachment() ) $currently['is_attachment'] = true;
171
172 // Check for selected pages/sections
173 if ( array_intersect_key( $currently, $vis_settings['location'] ) )
174 $do_show_by_select = true;
175
176 // Word count
177 if ( isset( $vis_settings['location']['check_wordcount'] ) ) {
178 // Check for word count
179 $word_count_to_check = intval( $vis_settings['location']['word_count'] );
180 $check_type = $vis_settings['location']['check_wordcount_type'];
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 }
187 }
188
189 // Combine all context checks
190 if ($do_show_by_word_count || $do_show_by_url || $do_show_by_select)
191 $one_is_true = true;
192 elseif (!$do_show_by_word_count || !$do_show_by_url || !$do_show_by_select)
193 $one_is_true = false;
194
195 if (($vis_settings['incexc'] == 'selected') && $one_is_true) {
196 // Show on selected
197 $do_show = true;
198 } elseif (($vis_settings['incexc'] == 'notselected') && !$one_is_true) {
199 // Hide on selected
200 $do_show = true;
201 } elseif (!empty($vis_settings['incexc'])) {
202 $do_show = false;
203 } else {
204 $do_show = true;
205 }
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 }
250
251
252 /*
253
254 Interface constructors
255
256 */
257
258
259 function make_simple_checkbox( $name, $label ) {
260 return sprintf(
261 '<label class="wl-%s"><input type="checkbox" value="1" name="wl%s" %s />&nbsp;%s</label>',
262 $this->get_field_classname( $name ),
263 $this->get_field_name( $name ),
264 checked( (bool) $this->get_field_value( $name ), 1, false ),
265 $label
266 );
267 }
268
269
270 function make_simple_textarea( $name, $label, $tip = null ) {
271 if ( $tip )
272 $tip = sprintf( '<p class="wl-tip">%s</p>', $tip );
273
274 return sprintf(
275 '<div class="wl-%s">
276 <label>
277 <strong>%s</strong>
278 <textarea name="wl%s">%s</textarea>
279 </label>
280 %s
281 </div>',
282 $this->get_field_classname( $name ),
283 $label,
284 $this->get_field_name( $name ),
285 esc_textarea( $this->get_field_value( $name ) ),
286 $tip
287 );
288 }
289
290
291 function make_simple_textfield( $name, $label_before = null, $label_after = null) {
292 return sprintf(
293 '<label class="wl-%s">%s <input type="text" name="wl%s" value="%s" /> %s</label>',
294 $this->get_field_classname( $name ),
295 $label_before,
296 $this->get_field_name( $name ),
297 esc_attr( $this->get_field_value( $name ) ),
298 $label_after
299 );
300 }
301
302
303 function make_simple_dropdown( $name, $selection = array(), $label_before = null, $label_after = null ) {
304 $value = esc_attr( $this->get_field_value( $name ) );
305 $options = array();
306
307 if ( empty( $selection ) )
308 $options[] = sprintf( '<option value="">%s</option>', __('No options given') );
309
310 foreach ( $selection as $sid => $svalue )
311 $options[] = sprintf( '<option value="%s" %s>%s</option>', $sid, selected( $value, $sid, false ), $svalue );
312
313 return sprintf(
314 '<label class="wl-%s">
315 %s
316 <select name="wl%s">
317 %s
318 </select>
319 %s
320 </label>',
321 $this->get_field_classname( $name ),
322 $label_before,
323 $this->get_field_name( $name ),
324 implode( '', $options ),
325 $label_after
326 );
327 }
328
329 /**
330 * Returns [part1][part2][partN] from array( 'part1', 'part2', 'part3' )
331 * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
332 * @return string i.e. [part1][part2][partN]
333 */
334 function get_field_name( $parts ) {
335 return esc_attr( sprintf( '[%s]', implode( '][', $parts ) ) );
336 }
337
338 function get_field_classname( $parts ) {
339 return sanitize_html_class( str_replace( '_', '-', end( $parts ) ) );
340 }
341
342
343 /**
344 * Given option keys return its value
345 * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
346 * @param array $options i.e. array( 'part1' => array( 'part2' => array( 'part3' => 'VALUE' ) ) )
347 * @return string Returns option value
348 */
349 function get_field_value( $parts, $options = array() ) {
350 if ( empty( $options ) )
351 $options = $this->context_options;
352
353 if ( ! empty( $parts ) )
354 $part = array_shift( $parts );
355
356 if ( isset( $part ) && isset( $options[ $part ] ) && is_array( $options[ $part ] ) )
357 $value = $this->get_field_value( $parts, $options[ $part ] );
358 elseif ( isset( $options[ $part ] ) )
359 $value = $options[ $part ];
360 else
361 $value = '';
362
363 return trim( $value );
364 }
365
366
367 }
368
369