PluginProbe
Widget Context / 1.0.3
Widget Context v1.0.3
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 1.0.3, at widget-context.php

1,060 lines 28.1 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: 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
12 // Go!
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
20 private $sidebars_widgets_copy;
21
22 private $core_modules = array(
23 'word-count',
24 'custom-post-types-taxonomies'
25 );
26
27 private $context_options = array(); // Store visibility settings
28 private $context_settings = array(); // Store admin settings
29 private $contexts = array();
30 private $plugin_path;
31
32
33 static function instance() {
34
35 static $instance;
36
37 if ( ! $instance )
38 $instance = new self();
39
40 return $instance;
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
69 // Fix legacy context option naming
70 add_filter( 'widget_context_options', array( $this, 'fix_legacy_options' ) );
71
72 // Register admin settings menu
73 add_action( 'admin_menu', array( $this, 'widget_context_settings_menu' ) );
74
75 // Register admin settings
76 add_action( 'admin_init', array( $this, 'widget_context_settings_init' ) );
77
78 // Register our own debug bar panel
79 add_filter( 'debug_bar_panels', array( $this, 'widget_context_debug_bar_init' ) );
80 add_action( 'debug_bar_enqueue_scripts', array( $this, 'widget_context_debug_bar_scripts' ) );
81
82 }
83
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 )
97 );
98
99 // Initialize core modules
100 $include_path = plugin_dir_path( __FILE__ ) . '/modules';
101
102 foreach ( $this->core_modules as $module ) {
103 include sprintf( '%s/%s/module.php', $include_path, $module );
104 }
105
106 // Default context
107 $default_contexts = array(
108 'incexc' => array(
109 'label' => __( 'Widget Context', 'widget-context' ),
110 'description' => __( 'Set the default logic to show or hide.', 'widget-context' ),
111 'weight' => -100,
112 'type' => 'core',
113 ),
114 'location' => array(
115 'label' => __( 'Global Sections', 'widget-context' ),
116 'description' => __( 'Based on standard WordPress template tags.', 'widget-context' ),
117 'weight' => 10
118 ),
119 'url' => array(
120 'label' => __( 'Target by URL', 'widget-context' ),
121 'description' => __( 'Based on URL patterns.', 'widget-context' ),
122 'weight' => 20
123 ),
124 'admin_notes' => array(
125 'label' => __( 'Notes (invisible to public)', 'widget-context' ),
126 'description' => __( 'Enables private notes on widget context settings.', 'widget-context'),
127 'weight' => 90
128 )
129 );
130
131 // Add default context controls and checks
132 foreach ( $default_contexts as $context_name => $context_desc ) {
133
134 add_filter( 'widget_context_control-' . $context_name, array( $this, 'control_' . $context_name ), 10, 2 );
135 add_filter( 'widget_context_check-' . $context_name, array( $this, 'context_check_' . $context_name ), 10, 2 );
136
137 }
138
139 // Enable other plugins and themes to specify their own contexts
140 $this->contexts = apply_filters( 'widget_contexts', $default_contexts );
141
142 // Sort contexts by their weight
143 uasort( $this->contexts, array( $this, 'sort_context_by_weight' ) );
144
145 }
146
147
148 public function get_context_options( $widget_id = null ) {
149
150 if ( ! $widget_id )
151 return $this->context_options;
152
153 if ( isset( $this->context_options[ $widget_id ] ) )
154 return $this->context_options[ $widget_id ];
155 else
156 return null;
157
158 }
159
160
161 public function get_context_settings( $widget_id = null ) {
162
163 if ( ! $widget_id )
164 return $this->context_settings;
165
166 if ( isset( $this->context_settings[ $widget_id ] ) )
167 return $this->context_settings[ $widget_id ];
168 else
169 return null;
170
171 }
172
173
174 public function get_contexts() {
175
176 return $this->contexts;
177
178 }
179
180
181 function sort_context_by_weight( $a, $b ) {
182
183 if ( ! isset( $a['weight'] ) )
184 $a['weight'] = 10;
185
186 if ( ! isset( $b['weight'] ) )
187 $b['weight'] = 10;
188
189 return ( $a['weight'] < $b['weight'] ) ? -1 : 1;
190
191 }
192
193
194 function set_widget_contexts_frontend() {
195
196 // Hide/show widgets for is_active_sidebar() to work
197 add_filter( 'sidebars_widgets', array( $this, 'maybe_unset_widgets_by_context' ), 10 );
198
199 }
200
201
202 function init_l10n() {
203
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
248
249 function widget_context_controls( $object, $return, $instance ) {
250
251 echo $this->display_widget_context( $object->id );
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
268 $sidebars_widgets = wp_get_sidebars_widgets();
269 $all_widget_ids = array();
270
271 // Get a lits of all widget IDs
272 foreach ( $sidebars_widgets as $widget_area => $widgets )
273 foreach ( $widgets as $widget_order => $widget_id )
274 $all_widget_ids[] = $widget_id;
275
276 // Remove non-existant widget contexts from the settings
277 foreach ( $this->context_options as $widget_id => $widget_context )
278 if ( ! in_array( $widget_id, $all_widget_ids ) )
279 unset( $this->context_options[ $widget_id ] );
280
281 update_option( $this->options_name, $this->context_options );
282
283 }
284
285
286 function maybe_unset_widgets_by_context( $sidebars_widgets ) {
287
288 // Don't run this at the backend or before
289 // post query has been run
290 if ( is_admin() )
291 return $sidebars_widgets;
292
293 // Return from cache if we have done the context checks already
294 if ( ! empty( $this->sidebars_widgets ) )
295 return $this->sidebars_widgets;
296
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 ] );
309
310 }
311
312 }
313
314 // Store in class cache
315 $this->sidebars_widgets = $sidebars_widgets;
316
317 return $sidebars_widgets;
318
319 }
320
321
322 function check_widget_visibility( $widget_id ) {
323
324 // Check if this widget even has context set
325 if ( ! isset( $this->context_options[ $widget_id ] ) )
326 return true;
327
328 $matches = array();
329
330 foreach ( $this->get_contexts() as $context_id => $context_settings ) {
331
332 // This context check has been disabled in the plugin settings
333 if ( isset( $this->context_settings['contexts'][ $context_id ] ) && ! $this->context_settings['contexts'][ $context_id ] )
334 continue;
335
336 // Make sure that context settings for this widget are defined
337 if ( ! isset( $this->context_options[ $widget_id ][ $context_id ] ) )
338 $widget_context_args = array();
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
348 }
349
350 // Get the match rule for this widget (show/hide/selected/notselected)
351 $match_rule = $this->context_options[ $widget_id ][ 'incexc' ][ 'condition' ];
352
353 // Force show or hide the widget!
354 if ( $match_rule == 'show' )
355 return true;
356 elseif ( $match_rule == 'hide' )
357 return false;
358
359 if ( $match_rule == 'selected' )
360 $inc = true;
361 else
362 $inc = false;
363
364 if ( $inc && in_array( true, $matches ) )
365 return true;
366 elseif ( ! $inc && ! in_array( true, $matches ) )
367 return true;
368 else
369 return false;
370
371 }
372
373
374 /**
375 * Default context checks
376 */
377
378 function context_check_incexc( $check, $settings ) {
379
380 return $check;
381
382 }
383
384
385 function context_check_location( $check, $settings ) {
386
387 $status = array(
388 'is_front_page' => is_front_page(),
389 'is_home' => is_home(),
390 'is_singular' => is_singular(),
391 'is_single' => is_singular( 'post' ),
392 'is_page' => ( is_page() && ! is_front_page() ),
393 'is_attachment' => is_attachment(),
394 'is_search' => is_search(),
395 'is_404' => is_404(),
396 'is_archive' => is_archive(),
397 'is_date' => is_date(),
398 'is_day' => is_day(),
399 'is_month' => is_month(),
400 'is_year' => is_year(),
401 'is_category' => is_category(),
402 'is_tag' => is_tag(),
403 'is_author' => is_author()
404 );
405
406 $matched = array_intersect_assoc( $settings, $status );
407
408 if ( ! empty( $matched ) )
409 return true;
410
411 return $check;
412
413 }
414
415
416 function context_check_url( $check, $settings ) {
417
418 $settings = wp_parse_args(
419 $settings,
420 array(
421 'urls' => null
422 )
423 );
424
425 $urls = trim( $settings['urls'] );
426
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
445 $url_request = $wp->request;
446
447 // Append the query string
448 if ( ! empty( $_SERVER['QUERY_STRING'] ) )
449 $url_request .= '?' . $_SERVER['QUERY_STRING'];
450
451 $rows = explode( "\n", $patterns );
452
453 foreach ( $rows as $pattern ) {
454
455 // Trim trailing, leading slashes and whitespace
456 $pattern = trim( trim( $pattern ), '/' );
457
458 // Escape regex chars
459 $pattern = preg_quote( $pattern, '/' );
460
461 // Enable wildcard checks
462 $pattern = str_replace( '\*', '.*', $pattern );
463
464 $patterns_safe[] = $pattern;
465
466 }
467
468 // Remove empty patterns
469 $patterns_safe = array_filter( $patterns_safe );
470
471 $regexps = sprintf(
472 '/^(%s)$/i',
473 implode( '|', $patterns_safe )
474 );
475
476 return preg_match( $regexps, $url_request );
477
478 }
479
480
481 // Dummy function
482 function context_check_admin_notes( $check, $widget_id ) {}
483
484
485 // Dummy function
486 function context_check_general( $check, $widget_id ) {}
487
488
489 /*
490 Widget Controls
491 */
492
493 function display_widget_context( $widget_id = null ) {
494
495 $controls = array();
496 $controls_disabled = array();
497 $controls_core = array();
498
499 foreach ( $this->contexts as $context_name => $context_settings ) {
500
501 $context_classes = array(
502 'context-group',
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';
510 $controls_disabled[] = $context_name;
511 }
512
513 // Store core controls
514 if ( isset( $context_settings['type'] ) && 'core' == $context_settings['type'] ) {
515 $controls_core[] = $context_name;
516 }
517
518 $control_args = array(
519 'name' => $context_name,
520 'input_prefix' => 'wl' . $this->get_field_name( array( $widget_id, $context_name ) ),
521 'settings' => $this->get_field_value( array( $widget_id, $context_name ) ),
522 'widget_id' => $widget_id
523 );
524
525 $context_controls = apply_filters( 'widget_context_control-' . $context_name, $control_args );
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 );
541
542 }
543
544 }
545
546
547
548 // Non-core controls that should be visible if enabled
549 $controls_not_core = array_diff( array_keys( $controls ), $controls_core );
550
551 // Check if any non-core context controls have been enabled
552 $has_controls = array_diff( $controls_not_core, $controls_disabled );
553
554 if ( empty( $controls ) || empty( $has_controls ) ) {
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' )
563 )
564 ) );
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 ),
608 // Controls
609 implode( '', $controls )
610 );
611
612 }
613
614
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
624 return $this->make_simple_dropdown( $control_args, 'condition', $options );
625
626 }
627
628
629 function control_location( $control_args ) {
630
631 $options = array(
632 'is_front_page' => __( 'Front page', 'widget-context' ),
633 'is_home' => __( 'Blog page', 'widget-context' ),
634 'is_singular' => __( 'All posts, pages and custom post types', 'widget-context' ),
635 'is_single' => __( 'All posts', 'widget-context' ),
636 'is_page' => __( 'All pages', 'widget-context' ),
637 'is_attachment' => __( 'All attachments', 'widget-context' ),
638 'is_search' => __( 'Search results', 'widget-context' ),
639 'is_404' => __( '404 error page', 'widget-context' ),
640 'is_archive' => __( 'All archives', 'widget-context' ),
641 'is_date' => __( 'All date archives', 'widget-context' ),
642 'is_day' => __( 'Daily archives', 'widget-context' ),
643 'is_month' => __( 'Monthly archives', 'widget-context' ),
644 'is_year' => __( 'Yearly archives', 'widget-context' ),
645 'is_category' => __( 'All category archives', 'widget-context' ),
646 'is_tag' => __( 'All tag archives', 'widget-context' ),
647 'is_author' => __( 'All author archives', 'widget-context' )
648 );
649
650 foreach ( $options as $option => $label )
651 $out[] = $this->make_simple_checkbox( $control_args, $option, $label );
652
653 return implode( '', $out );
654
655 }
656
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' ),
664 __( '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>.', 'widget-context' )
665 );
666
667 }
668
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 ] )
689 $value = true;
690 else
691 $value = false;
692
693 return sprintf(
694 '<label class="wc-field-checkbox-%s" data-widget-id="%s">
695 <input type="hidden" value="0" name="%s[%s]" />
696 <input type="checkbox" value="1" name="%s[%s]" %s />&nbsp;%s
697 </label>',
698 $this->get_field_classname( $option ),
699 esc_attr( $control_args['widget_id'] ),
700 // Input hidden
701 $control_args['input_prefix'],
702 esc_attr( $option ),
703 // Input value
704 $control_args['input_prefix'],
705 esc_attr( $option ),
706 checked( $value, true, false ),
707 // Label
708 esc_html( $label )
709 );
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>
725 </label>',
726 $this->get_field_classname( $option ),
727 esc_attr( $control_args['widget_id'] ),
728 // Label
729 esc_html( $label ),
730 // Input
731 $control_args['input_prefix'],
732 $option,
733 $value
734 );
735
736 }
737
738
739 function make_simple_textfield( $control_args, $option, $label_before = null, $label_after = null) {
740
741 if ( isset( $control_args['settings'][ $option ] ) )
742 $value = esc_attr( $control_args['settings'][ $option ] );
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 ),
753 esc_attr( $control_args['widget_id'] ),
754 // Before
755 $label_before,
756 // Input
757 $control_args['input_prefix'],
758 $option,
759 esc_attr( $value ),
760 // After
761 esc_html( $label_after )
762 );
763
764 }
765
766
767 function make_simple_dropdown( $control_args, $option, $selection = array(), $label_before = null, $label_after = null ) {
768
769 $options = array();
770
771 if ( isset( $control_args['settings'][ $option ] ) )
772 $value = $control_args['settings'][ $option ];
773 else
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
807 $label_after
808 );
809
810 }
811
812
813 /**
814 * Returns [part1][part2][partN] from array( 'part1', 'part2', 'part3' )
815 * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
816 * @return string i.e. [part1][part2][partN]
817 */
818 function get_field_name( $parts ) {
819
820 return esc_attr( sprintf( '[%s]', implode( '][', $parts ) ) );
821
822 }
823
824 function get_field_classname( $name ) {
825
826 if ( is_array( $name ) )
827 $name = end( $name );
828
829 return sanitize_html_class( str_replace( '_', '-', $name ) );
830
831 }
832
833
834 /**
835 * Given option keys return its value
836 * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
837 * @param array $options i.e. array( 'part1' => array( 'part2' => array( 'part3' => 'VALUE' ) ) )
838 * @return string Returns option value
839 */
840 function get_field_value( $parts, $options = null ) {
841
842 if ( $options == null )
843 $options = $this->context_options;
844
845 $value = false;
846
847 if ( empty( $parts ) || ! is_array( $parts ) )
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 ] ) )
855 return $options[ $part ];
856
857 return $value;
858
859 }
860
861
862 function fix_legacy_options( $options ) {
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
870 if ( ! isset( $option['incexc'] ) )
871 unset( $options[ $widget_id ] );
872
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'];
880 unset( $option['general']['notes'] );
881 }
882
883 // We moved word count out of location context group
884 if ( isset( $option['location']['check_wordcount'] ) )
885 $options[ $widget_id ]['word_count'] = array(
886 'check_wordcount' => true,
887 'check_wordcount_type' => $option['location']['check_wordcount_type'],
888 'word_count' => $option['location']['word_count']
889 );
890
891 }
892
893 return $options;
894
895 }
896
897
898
899 /**
900 * Admin Settings
901 */
902
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
914 }
915
916
917 function widget_context_settings_init() {
918
919 register_setting( $this->settings_name, $this->settings_name );
920
921 }
922
923
924 function widget_context_admin_view() {
925
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;
941
942 // Enable new modules by default
943 if ( ! isset( $this->context_settings['contexts'][ $context_id ] ) )
944 $this->context_settings['contexts'][ $context_id ] = 1;
945
946 $context_controls[] = sprintf(
947 '<li class="context-%s">
948 <label>
949 <input type="hidden" name="%s[contexts][%s]" value="0" />
950 <input type="checkbox" name="%s[contexts][%s]" value="1" %s /> %s
951 </label>
952 %s
953 </li>',
954 esc_attr( $context_id ),
955 $this->settings_name,
956 esc_attr( $context_id ),
957 $this->settings_name,
958 esc_attr( $context_id ),
959 checked( $this->context_settings['contexts'][ $context_id ], 1, false ),
960 esc_html( $context_args['label'] ),
961 $context_description
962 );
963
964 }
965
966 ?>
967 <div class="wrap wrap-widget-context">
968 <h2><?php esc_html_e( 'Widget Context Settings', 'widget-context' ); ?></h2>
969
970 <div class="widget-context-settings-wrap">
971
972 <div class="widget-context-form">
973 <form method="post" action="options.php">
974 <?php
975 settings_fields( $this->settings_name );
976 do_settings_sections( $this->settings_name );
977 ?>
978
979 <?php
980 printf(
981 '<div class="settings-section settings-section-modules">
982 <h3>%s</h3>
983 <ul>%s</ul>
984 </div>',
985 esc_html__( 'Enabled Context Modules', 'widget-context' ),
986 implode( '', $context_controls )
987 );
988 ?>
989
990 <?php
991 submit_button();
992 ?>
993 </form>
994 </div>
995
996 <div class="widget-context-sidebar">
997 <div class="wc-sidebar-in">
998
999 <div class="wc-sidebar-section wc-sidebar-credits">
1000 <p>
1001 <img src="http://gravatar.com/avatar/661eb21385c25c01ad64ab9e13b37331/?s=60" alt="Kaspars Dambis" width="60" height="60" />
1002 <?php printf( esc_html__( 'Widget Context is created and maintained by %s.' , 'widget-context'), '<a href="http://kaspars.net">Kaspars Dambis</a>' ); ?>
1003 </p>
1004 </div>
1005
1006 <div class="wc-sidebar-section wc-sidebar-newsletter">
1007 <h3><?php esc_html_e( 'News & Updates' , 'widget-context'); ?></h3>
1008 <p><?php esc_html_e( 'Subscribe to receive news & updates about the plugin.' , 'widget-context'); ?></p>
1009 <form action="//osc.us2.list-manage.com/subscribe/post?u=e8d173fc54c0fc4286a2b52e8&amp;id=8afe96c5a3" method="post" target="_blank">
1010 <?php $user = wp_get_current_user(); ?>
1011 <p><label><?php _e( 'Your Name', 'widget-context' ); ?>: <input type="text" name="NAME" value="<?php echo esc_attr( sprintf( '%s %s', $user->first_name, $user->last_name ) ) ?>" /></label></p>
1012 <p><label><?php _e( 'Your Email', 'widget-context' ); ?>: <input type="text" name="EMAIL" value="<?php echo esc_attr( $user->user_email ); ?>" /></label></p>
1013 <p><input class="button" name="subscribe" type="submit" value="<?php _e( 'Subscribe', 'widget-context' ); ?>" /></p>
1014 </form>
1015 </div>
1016
1017 </div>
1018 </div>
1019
1020 </div>
1021 </div>
1022 <?php
1023
1024 }
1025
1026
1027 public function get_sidebars_widgets_copy() {
1028
1029 return $this->sidebars_widgets_copy;
1030
1031 }
1032
1033
1034 function widget_context_debug_bar_init( $panels ) {
1035
1036 include plugin_dir_path( __FILE__ ) . '/debug/debug-bar.php';
1037
1038 if ( class_exists( 'Debug_Widget_Context' ) )
1039 $panels[] = new Debug_Widget_Context();
1040
1041 return $panels;
1042
1043 }
1044
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
1060