Widget Context - Version 1.1.1

Version Description

= 1.0.2 = Load available custom post types and taxonomies right before visibility checks to avoid PHP warnings. Run visibility checks only after the main post query has run. Fixes issues with WooCommerce.

= 1.0.1 =

Fix PHP warning in custom post type and taxonomy module.

= 1.0 =

New modular architecture and settings page. Please make sure you test this version before deploying to a production website.

= 0.8.1 =

(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.

= 0.8 =

Major code rewrite and refactoring to improve plugin performance and usability.

= 0.7.2 =

Fix PHP warnings/notices.

= 0.7.1 =

Confirm that plugin works with the latest version of WordPress.

= 0.7 =

Bug fix: check for active sidebars only after $paged has been set.

= 0.6 =

Performance improvements - don't check if sidebar has any widgets on every widget load.

Download this release

Release Info

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

Code changes from version 1.1.0 to 1.1.1

class/modules/word-count/module.php DELETED
@@ -1,112 +0,0 @@
1
- <?php
2
-
3
- // Check for Widget Context plugin
4
- if ( ! class_exists( 'widget_context' ) )
5
- die;
6
-
7
-
8
- // Go!
9
- WidgetContextWordCount::instance();
10
-
11
-
12
- class WidgetContextWordCount {
13
-
14
- private static $instance;
15
- private $wc;
16
-
17
- var $words_on_page = 0;
18
-
19
-
20
- static function instance() {
21
-
22
- if ( ! self::$instance )
23
- self::$instance = new self();
24
-
25
- return self::$instance;
26
-
27
- }
28
-
29
-
30
- private function __construct() {
31
-
32
- $this->wc = widget_context::instance();
33
-
34
- // Check the number of words on page
35
- add_action( 'wp', array( $this, 'count_words_on_page' ) );
36
-
37
- // Define our context
38
- add_filter( 'widget_contexts', array( $this, 'add_word_count_context' ) );
39
-
40
- add_filter( 'widget_context_control-word_count', array( $this, 'control_word_count' ), 10, 2 );
41
- add_filter( 'widget_context_check-word_count', array( $this, 'context_check_word_count' ), 10, 2 );
42
-
43
- }
44
-
45
-
46
- function add_word_count_context( $contexts ) {
47
-
48
- $contexts[ 'word_count' ] = array(
49
- 'label' => __( 'Word Count', 'widget-context' ),
50
- 'description' => __( 'Context based on word count on the page.', 'widget-context' ),
51
- 'weight' => 15
52
- );
53
-
54
- return $contexts;
55
-
56
- }
57
-
58
-
59
- function count_words_on_page() {
60
-
61
- global $wp_query;
62
-
63
- if ( empty( $wp_query->posts ) || is_admin() )
64
- return;
65
-
66
- foreach ( $wp_query->posts as $post_data )
67
- $this->words_on_page += str_word_count( strip_tags( strip_shortcodes( $post_data->post_content ) ) );
68
-
69
- }
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,
77
- 'check_wordcount_type' => null
78
- ) );
79
-
80
- // Make sure this context check was enabled
81
- if ( ! $settings['check_wordcount'] )
82
- return $check;
83
-
84
- $word_count = (int) $settings['word_count'];
85
-
86
- // No word count specified, bail out
87
- if ( ! $word_count )
88
- return $check;
89
-
90
- if ( $settings['check_wordcount_type'] == 'less' && $this->words_on_page < $word_count )
91
- return true;
92
- elseif ( $settings['check_wordcount_type'] == 'more' && $this->words_on_page > $word_count )
93
- return true;
94
-
95
- return $check;
96
-
97
- }
98
-
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') ),
106
- $this->wc->make_simple_textfield( $control_args, 'word_count', null, __('words', 'widget-context') )
107
- );
108
-
109
- }
110
-
111
-
112
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
debug/debug-bar.php DELETED
@@ -1,109 +0,0 @@
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
- }
10
-
11
- function prerender() {
12
-
13
- $this->set_visible( ! is_admin() );
14
-
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
-
30
- foreach ( $sidebars_widgets as $widget_area => $widgets ) {
31
-
32
- if ( 'wp_inactive_widgets' == $widget_area )
33
- continue;
34
-
35
- foreach ( $widgets as $widget_i => $widget_id ) {
36
-
37
- $a = array(); // sorry
38
- $context_options = $wc->get_context_options( $widget_id );
39
-
40
- foreach ( $wc->get_contexts() as $context_id => $context ) {
41
-
42
- if ( isset( $context_options[ $context_id ] ) )
43
- $widget_context_args = $context_options[ $context_id ];
44
- else
45
- $widget_context_args = array();
46
-
47
- if ( $context_id == 'incexc' ) {
48
- if ( isset( $widget_context_args['condition'] ) && isset( $map[ $widget_context_args['condition'] ] ) )
49
- $set = $map[ $widget_context_args['condition'] ];
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
- );
70
-
71
- }
72
-
73
- if ( $wc->check_widget_visibility( $widget_id ) )
74
- $status = sprintf( __( 'Showing <strong>%s</strong> in "%s"' ), esc_html( $widget_id ), esc_html( $widget_area ) );
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>
82
- <th>Context</th>
83
- <th>Match</th>
84
- <th>Settings</th>
85
- </tr>
86
- %s
87
- </table>',
88
- $widget_i,
89
- __( 'Toggle', 'widget-context' ),
90
- esc_html( $set ),
91
- $status,
92
- $widget_i,
93
- implode( '', $a )
94
- );
95
-
96
- }
97
- }
98
-
99
- printf(
100
- '%s
101
- <h3>Registered Contexts:</h3>
102
- <pre>%s</pre>',
103
- implode( '', $out ),
104
- esc_html( print_r( $wc->get_contexts(), true ) )
105
- );
106
-
107
- }
108
-
109
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
debug/debug.js DELETED
@@ -1,8 +0,0 @@
1
- jQuery(document).ready(function($) {
2
-
3
- $('#debug-menu-target-Debug_Widget_Context .toggle').on('click', function(e){
4
- e.preventDefault();
5
- $( $(this).attr('href') ).toggle();
6
- });
7
-
8
- });
 
 
 
 
 
 
 
 
phpcs.xml.dist ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <ruleset name="WordPress Coding Standards for Plugins">
3
+ <config name="installed_paths" value="vendor/wp-coding-standards/wpcs" />
4
+ <config name="ignore_warnings_on_exit" value="1" />
5
+
6
+ <arg name="extensions" value="php" />
7
+ <arg name="colors" />
8
+ <arg value="s" /><!-- Show sniff codes in all reports. -->
9
+
10
+ <rule ref="WordPress-Core">
11
+ <exclude name="WordPress.Files.FileName" /><!-- We'll be switching to PSR-4 naming soon. -->
12
+ <exclude name="Generic.Formatting.MultipleStatementAlignment" />
13
+ <exclude name="WordPress.Arrays.MultipleStatementAlignment" />
14
+ </rule>
15
+
16
+ <!--
17
+ <rule ref="WordPress-Docs">
18
+ <exclude name="Squiz.Commenting.FileComment" />
19
+ </rule>
20
+ -->
21
+
22
+ <exclude-pattern>/node_modules</exclude-pattern>
23
+ <exclude-pattern>/vendor</exclude-pattern>
24
+ </ruleset>
readme.txt CHANGED
@@ -1,30 +1,53 @@
1
  === Widget Context ===
2
 
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.9.6
7
- Stable tag: 1.1.0
8
- License: GPLv2 or later
9
 
10
  Show or hide widgets on specific posts, pages or sections of your site.
11
 
12
 
13
  == Description ==
14
 
15
- Widget Context allows you to show or hide widgets on certain sections of your site — front page, posts, pages, archives, search, etc. It also features section targeting by URLs (with wildcard support) for maximum flexibility.
16
 
 
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
 
23
 
24
  == Installation ==
25
 
26
- - Install the plugin through **Add New Plugin** feature in your WordPress dashboard -- search for Widget Context.
27
- - Widget Context settings will appear automatically under each widget under Design > Widgets.
 
28
 
29
 
30
  == Changelog ==
1
  === Widget Context ===
2
 
3
+ Contributors: kasparsd, jamescollins
4
+ Tags: widget, widget context, context, logic, widget logic, cms
5
+ Requires at least: 3.0
6
+ Tested up to: 5.2
7
+ Stable tag: 1.1.1
8
+ License: GPLv2 or later
9
 
10
  Show or hide widgets on specific posts, pages or sections of your site.
11
 
12
 
13
  == Description ==
14
 
15
+ [Widget Context](https://widgetcontext.com) will show or hide widgets on certain sections of your site — front page, posts, pages, archives, search, etc. It also features section targeting by URLs (with wildcard support) for maximum flexibility.
16
 
17
+ = Contribute =
18
 
19
+ - Suggest code improvements [on GitHub](https://github.com/kasparsd/widget-context-wporg).
20
+ - Report bugs and suggestions on [WordPress.org forums](http://wordpress.org/support/plugin/widget-context).
21
+ - [Help translate](https://translate.wordpress.org/projects/wp-plugins/widget-context) to your language.
22
 
23
+
24
+ = Documentation =
25
+
26
+ = Target by URL =
27
+
28
+ The “Target by URL” is a very powerful feature with a lot of flexibility for targeting sections of your website based on the request URLs. It was inspired by a similar feature in the [Drupal CMS](https://www.drupal.org).
29
+
30
+ - Use relative URLs such as `page/sub-page` instead of absolute URLs `https://example.com/page/sub-page`.
31
+
32
+ - Relative are URLs more flexible and make the logic portable between different domains and server environments.
33
+
34
+
35
+ = Wildcards =
36
+
37
+ Use the wildcard symbol `*` for matching dynamic parts of the URL. For example:
38
+
39
+ - `topic/widgets/*` to match all posts in the widgets category, if your permalink structure is set to `/topic/%category%/%postname%`.
40
+
41
+ - `page-slug/*` to match all child pages of the page-slug parent page.
42
+
43
+ - Use a trailing `?*` to capture URL with all query arguments such as `utm_source`, etc. For example, for every `blog/post-slug` also include `blog/post-slug?*`.
44
 
45
 
46
  == Installation ==
47
 
48
+ - Search for **Widget Context** under "Plugins → Add New" in your WordPress dashboard.
49
+
50
+ - Widget Context settings will appear automatically under **each widget** under "Appearance → Widgets".
51
 
52
 
53
  == Changelog ==
class/class-widget-context.php → src/WidgetContext.php RENAMED
@@ -3,7 +3,7 @@
3
  /**
4
  * Widget Context plugin core.
5
  */
6
- class widget_context {
7
 
8
  private $asset_version = '1.0.4';
9
  private $sidebars_widgets;
@@ -11,46 +11,44 @@ class widget_context {
11
  private $settings_name = 'widget_context_settings'; // Widget Context global settings
12
  private $sidebars_widgets_copy;
13
 
14
- private $core_modules = array(
15
- 'word-count',
16
- 'custom-post-types-taxonomies'
17
- );
18
-
19
  private $context_options = array(); // Store visibility settings
20
  private $context_settings = array(); // Store admin settings
21
  private $contexts = array();
22
  private $plugin_path;
23
 
 
 
 
 
 
 
 
 
24
 
 
 
 
 
 
25
  static function instance() {
26
-
27
- static $instance;
28
-
29
- if ( ! $instance )
30
- $instance = new self();
31
-
32
- return $instance;
33
-
34
  }
35
 
36
  /**
37
- * Set path to the plugin directory.
38
  *
39
- * @param string $path Path to the plugin directory.
 
 
40
  */
41
- public function set_path( $path ) {
42
- $this->plugin_path = $path;
43
  }
44
 
45
  /**
46
  * Hook into WP.
47
  */
48
  public function init() {
49
- // Ensure that path to this plugin is defined first.
50
- if ( empty( $this->plugin_path ) ) {
51
- return;
52
- }
53
-
54
  // Define available widget contexts
55
  add_action( 'init', array( $this, 'define_widget_contexts' ), 5 );
56
 
@@ -75,34 +73,21 @@ class widget_context {
75
 
76
  // Register admin settings
77
  add_action( 'admin_init', array( $this, 'widget_context_settings_init' ) );
78
-
79
- // Register our own debug bar panel
80
- add_filter( 'debug_bar_panels', array( $this, 'widget_context_debug_bar_init' ) );
81
- add_action( 'debug_bar_enqueue_scripts', array( $this, 'widget_context_debug_bar_scripts' ) );
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
- foreach ( $this->core_modules as $module ) {
100
- $module_file = sprintf( '%s/class/modules/%s/module.php', $this->plugin_path, $module );
101
-
102
- if ( file_exists( $module_file ) ) {
103
- include $module_file;
104
- }
105
- }
106
 
107
  // Default context
108
  $default_contexts = array(
@@ -115,26 +100,24 @@ class widget_context {
115
  'location' => array(
116
  'label' => __( 'Global Sections', 'widget-context' ),
117
  'description' => __( 'Based on standard WordPress template tags.', 'widget-context' ),
118
- 'weight' => 10
119
  ),
120
  'url' => array(
121
  'label' => __( 'Target by URL', 'widget-context' ),
122
  'description' => __( 'Based on URL patterns.', 'widget-context' ),
123
- 'weight' => 20
124
  ),
125
  'admin_notes' => array(
126
  'label' => __( 'Notes (invisible to public)', 'widget-context' ),
127
- 'description' => __( 'Enables private notes on widget context settings.', 'widget-context'),
128
- 'weight' => 90
129
- )
130
  );
131
 
132
  // Add default context controls and checks
133
  foreach ( $default_contexts as $context_name => $context_desc ) {
134
-
135
  add_filter( 'widget_context_control-' . $context_name, array( $this, 'control_' . $context_name ), 10, 2 );
136
  add_filter( 'widget_context_check-' . $context_name, array( $this, 'context_check_' . $context_name ), 10, 2 );
137
-
138
  }
139
 
140
  // Enable other plugins and themes to specify their own contexts
@@ -142,66 +125,60 @@ class widget_context {
142
 
143
  // Sort contexts by their weight
144
  uasort( $this->contexts, array( $this, 'sort_context_by_weight' ) );
145
-
146
  }
147
 
148
 
149
  public function get_context_options( $widget_id = null ) {
150
-
151
- if ( ! $widget_id )
152
  return $this->context_options;
 
153
 
154
- if ( isset( $this->context_options[ $widget_id ] ) )
155
  return $this->context_options[ $widget_id ];
156
- else
157
- return null;
158
 
 
159
  }
160
 
161
 
162
  public function get_context_settings( $widget_id = null ) {
163
-
164
- if ( ! $widget_id )
165
  return $this->context_settings;
 
166
 
167
- if ( isset( $this->context_settings[ $widget_id ] ) )
168
  return $this->context_settings[ $widget_id ];
169
- else
170
- return null;
171
 
 
172
  }
173
 
174
 
175
  public function get_contexts() {
176
-
177
  return $this->contexts;
178
-
179
  }
180
 
181
 
182
  function sort_context_by_weight( $a, $b ) {
183
-
184
- if ( ! isset( $a['weight'] ) )
185
  $a['weight'] = 10;
 
186
 
187
- if ( ! isset( $b['weight'] ) )
188
  $b['weight'] = 10;
 
189
 
190
  return ( $a['weight'] < $b['weight'] ) ? -1 : 1;
191
-
192
  }
193
 
194
 
195
  function set_widget_contexts_frontend() {
196
-
197
  // Hide/show widgets for is_active_sidebar() to work
198
  add_filter( 'sidebars_widgets', array( $this, 'maybe_unset_widgets_by_context' ), 10 );
199
-
200
  }
201
 
202
 
203
  function admin_scripts( $page ) {
204
-
205
  // Enqueue only on widgets and customizer view
206
  if ( ! in_array( $page, array( 'widgets.php', 'settings_page_widget_context_settings' ), true ) ) {
207
  return;
@@ -220,25 +197,23 @@ class widget_context {
220
  array( 'jquery' ),
221
  $this->asset_version
222
  );
223
-
224
  }
225
 
226
 
227
  function widget_context_controls( $object, $return, $instance ) {
228
-
229
  echo $this->display_widget_context( $object->id );
230
-
231
  }
232
 
233
 
234
  function save_widget_context_settings() {
235
-
236
- if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['wl'] ) )
237
  return;
 
238
 
239
  // Delete a widget
240
- if ( isset( $_POST['delete_widget'] ) && isset( $_POST['the-widget-id'] ) )
241
  unset( $this->context_options[ $_POST['the-widget-id'] ] );
 
242
 
243
  // Add / Update
244
  $this->context_options = array_merge( $this->context_options, $_POST['wl'] );
@@ -247,105 +222,110 @@ class widget_context {
247
  $all_widget_ids = array();
248
 
249
  // Get a lits of all widget IDs
250
- foreach ( $sidebars_widgets as $widget_area => $widgets )
251
- foreach ( $widgets as $widget_order => $widget_id )
252
  $all_widget_ids[] = $widget_id;
 
 
253
 
254
  // Remove non-existant widget contexts from the settings
255
- foreach ( $this->context_options as $widget_id => $widget_context )
256
- if ( ! in_array( $widget_id, $all_widget_ids ) )
257
  unset( $this->context_options[ $widget_id ] );
 
 
258
 
259
  update_option( $this->options_name, $this->context_options );
260
-
261
  }
262
 
263
 
264
  function maybe_unset_widgets_by_context( $sidebars_widgets ) {
265
-
266
  // Don't run this at the backend or before
267
  // post query has been run
268
- if ( is_admin() )
269
  return $sidebars_widgets;
 
270
 
271
  // Return from cache if we have done the context checks already
272
- if ( ! empty( $this->sidebars_widgets ) )
273
  return $this->sidebars_widgets;
 
274
 
275
  // Store a local copy of the original widget location
276
  $this->sidebars_widgets_copy = $sidebars_widgets;
277
 
278
  foreach ( $sidebars_widgets as $widget_area => $widget_list ) {
279
 
280
- if ( $widget_area == 'wp_inactive_widgets' || empty( $widget_list ) )
281
  continue;
 
282
 
283
  foreach ( $widget_list as $pos => $widget_id ) {
284
 
285
- if ( ! $this->check_widget_visibility( $widget_id ) )
286
  unset( $sidebars_widgets[ $widget_area ][ $pos ] );
287
-
288
  }
289
-
290
  }
291
 
292
  // Store in class cache
293
  $this->sidebars_widgets = $sidebars_widgets;
294
 
295
  return $sidebars_widgets;
296
-
297
  }
298
 
299
 
300
  function check_widget_visibility( $widget_id ) {
301
-
302
  // Check if this widget even has context set
303
- if ( ! isset( $this->context_options[ $widget_id ] ) )
304
  return true;
 
305
 
306
  $matches = array();
307
 
308
  foreach ( $this->get_contexts() as $context_id => $context_settings ) {
309
-
310
  // This context check has been disabled in the plugin settings
311
- if ( isset( $this->context_settings['contexts'][ $context_id ] ) && ! $this->context_settings['contexts'][ $context_id ] )
312
  continue;
 
313
 
314
  // Make sure that context settings for this widget are defined
315
- if ( ! isset( $this->context_options[ $widget_id ][ $context_id ] ) )
316
  $widget_context_args = array();
317
- else
318
  $widget_context_args = $this->context_options[ $widget_id ][ $context_id ];
 
319
 
320
  $matches[ $context_id ] = apply_filters(
321
- 'widget_context_check-' . $context_id,
322
- null,
323
- $widget_context_args
324
- );
325
-
326
  }
327
 
328
  // Get the match rule for this widget (show/hide/selected/notselected)
329
- $match_rule = $this->context_options[ $widget_id ][ 'incexc' ][ 'condition' ];
330
 
331
  // Force show or hide the widget!
332
- if ( $match_rule == 'show' )
333
  return true;
334
- elseif ( $match_rule == 'hide' )
335
  return false;
 
336
 
337
- if ( $match_rule == 'selected' )
 
 
338
  $inc = true;
339
- else
340
- $inc = false;
341
 
342
- if ( $inc && in_array( true, $matches ) )
343
  return true;
344
- elseif ( ! $inc && ! in_array( true, $matches ) )
345
  return true;
346
- else
347
- return false;
348
 
 
349
  }
350
 
351
 
@@ -354,40 +334,37 @@ class widget_context {
354
  */
355
 
356
  function context_check_incexc( $check, $settings ) {
357
-
358
  return $check;
359
-
360
  }
361
 
362
 
363
  function context_check_location( $check, $settings ) {
364
-
365
  $status = array(
366
- 'is_front_page' => is_front_page(),
367
- 'is_home' => is_home(),
368
- 'is_singular' => is_singular(),
369
- 'is_single' => is_singular( 'post' ),
370
- 'is_page' => ( is_page() && ! is_front_page() ),
371
- 'is_attachment' => is_attachment(),
372
- 'is_search' => is_search(),
373
- 'is_404' => is_404(),
374
- 'is_archive' => is_archive(),
375
- 'is_date' => is_date(),
376
- 'is_day' => is_day(),
377
- 'is_month' => is_month(),
378
- 'is_year' => is_year(),
379
- 'is_category' => is_category(),
380
- 'is_tag' => is_tag(),
381
- 'is_author' => is_author()
382
- );
383
 
384
  $matched = array_intersect_assoc( $settings, $status );
385
 
386
- if ( ! empty( $matched ) )
387
  return true;
 
388
 
389
  return $check;
390
-
391
  }
392
 
393
 
@@ -403,11 +380,11 @@ class widget_context {
403
  static $path;
404
 
405
  $settings = wp_parse_args(
406
- $settings,
407
- array(
408
- 'urls' => null
409
- )
410
- );
411
 
412
  $urls = trim( $settings['urls'] );
413
 
@@ -523,7 +500,6 @@ class widget_context {
523
  */
524
 
525
  function display_widget_context( $widget_id = null ) {
526
-
527
  $controls = array();
528
  $controls_disabled = array();
529
  $controls_core = array();
@@ -531,9 +507,9 @@ class widget_context {
531
  foreach ( $this->contexts as $context_name => $context_settings ) {
532
 
533
  $context_classes = array(
534
- 'context-group',
535
- sprintf( 'context-group-%s', esc_attr( $context_name ) )
536
- );
537
 
538
  // Hide this context from the admin UX. We can't remove them
539
  // because settings will get lost if this page is submitted.
@@ -548,35 +524,30 @@ class widget_context {
548
  }
549
 
550
  $control_args = array(
551
- 'name' => $context_name,
552
- 'input_prefix' => 'wl' . $this->get_field_name( array( $widget_id, $context_name ) ),
553
- 'settings' => $this->get_field_value( array( $widget_id, $context_name ) ),
554
- 'widget_id' => $widget_id
555
- );
556
 
557
  $context_controls = apply_filters( 'widget_context_control-' . $context_name, $control_args );
558
  $context_classes = apply_filters( 'widget_context_classes-' . $context_name, $context_classes, $control_args );
559
 
560
  if ( ! empty( $context_controls ) && is_string( $context_controls ) ) {
561
-
562
  $controls[ $context_name ] = sprintf(
563
- '<div class="%s">
564
- <h4 class="context-toggle">%s</h4>
565
- <div class="context-group-wrap">
566
- %s
567
- </div>
568
- </div>',
569
- esc_attr( implode( ' ', $context_classes ) ),
570
- esc_html( $context_settings['label'] ),
571
- $context_controls
572
- );
573
-
574
  }
575
-
576
  }
577
 
578
-
579
-
580
  // Non-core controls that should be visible if enabled
581
  $controls_not_core = array_diff( array_keys( $controls ), $controls_core );
582
 
@@ -586,123 +557,113 @@ class widget_context {
586
  if ( empty( $controls ) || empty( $has_controls ) ) {
587
 
588
  if ( current_user_can( 'edit_theme_options' ) ) {
589
-
590
- $controls = array( sprintf(
591
  '<p class="error">%s</p>',
592
  sprintf(
593
  __( 'No widget controls enabled. You can enable them in <a href="%s">Widget Context settings</a>.', 'widget-context' ),
594
  admin_url( 'options-general.php?page=widget_context_settings' )
595
  )
596
- ) );
597
-
598
  } else {
599
-
600
- $controls = array( sprintf(
601
  '<p class="error">%s</p>',
602
  __( 'No widget controls enabled.', 'widget-context' )
603
- ) );
604
-
605
  }
606
-
607
  }
608
 
609
  $settings_link = '';
610
 
611
  if ( current_user_can( 'edit_theme_options' ) ) {
612
-
613
  $settings_link = sprintf(
614
- '<a href="%s" class="widget-context-settings-link" title="%s">%s</a>',
615
- admin_url( 'options-general.php?page=widget_context_settings' ),
616
- esc_attr__( 'Widget Context Settings', 'widget-context' ),
617
- esc_html__( 'Settings', 'widget-context' )
618
- );
619
-
620
  }
621
 
622
  return sprintf(
623
- '<div class="widget-context">
624
- <div class="widget-context-header">
625
- <h3>%s</h3>
626
- %s
627
- </div>
628
- <div class="widget-context-inside" id="widget-context-%s" data-widget-id="%s">
629
- %s
630
- </div>
631
- </div>',
632
- __( 'Widget Context', 'widget-context' ),
633
- $settings_link,
634
- // Inslide classes
635
- esc_attr( $widget_id ),
636
- esc_attr( $widget_id ),
637
- // Controls
638
- implode( '', $controls )
639
- );
640
 
641
  }
642
 
643
 
644
  function control_incexc( $control_args ) {
645
-
646
  $options = array(
647
- 'show' => __( 'Show widget everywhere', 'widget-context' ),
648
- 'selected' => __( 'Show widget on selected', 'widget-context' ),
649
- 'notselected' => __( 'Hide widget on selected', 'widget-context' ),
650
- 'hide' => __( 'Hide widget everywhere', 'widget-context' )
651
- );
652
 
653
  return $this->make_simple_dropdown( $control_args, 'condition', $options );
654
-
655
  }
656
 
657
 
658
  function control_location( $control_args ) {
659
-
660
  $options = array(
661
- 'is_front_page' => __( 'Front page', 'widget-context' ),
662
- 'is_home' => __( 'Blog page', 'widget-context' ),
663
- 'is_singular' => __( 'All posts, pages and custom post types', 'widget-context' ),
664
- 'is_single' => __( 'All posts', 'widget-context' ),
665
- 'is_page' => __( 'All pages', 'widget-context' ),
666
- 'is_attachment' => __( 'All attachments', 'widget-context' ),
667
- 'is_search' => __( 'Search results', 'widget-context' ),
668
- 'is_404' => __( '404 error page', 'widget-context' ),
669
- 'is_archive' => __( 'All archives', 'widget-context' ),
670
- 'is_date' => __( 'All date archives', 'widget-context' ),
671
- 'is_day' => __( 'Daily archives', 'widget-context' ),
672
- 'is_month' => __( 'Monthly archives', 'widget-context' ),
673
- 'is_year' => __( 'Yearly archives', 'widget-context' ),
674
- 'is_category' => __( 'All category archives', 'widget-context' ),
675
- 'is_tag' => __( 'All tag archives', 'widget-context' ),
676
- 'is_author' => __( 'All author archives', 'widget-context' )
677
- );
678
 
679
- foreach ( $options as $option => $label )
680
  $out[] = $this->make_simple_checkbox( $control_args, $option, $label );
 
681
 
682
  return implode( '', $out );
683
-
684
  }
685
 
686
 
687
  function control_url( $control_args ) {
688
-
689
  return sprintf(
690
- '<div>%s</div>
691
- <p class="help">%s</p>',
692
- $this->make_simple_textarea( $control_args, 'urls' ),
693
- __( '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' )
694
- );
695
-
696
  }
697
 
698
 
699
  function control_admin_notes( $control_args ) {
700
-
701
  return sprintf(
702
- '<div>%s</div>',
703
- $this->make_simple_textarea( $control_args, 'notes' )
704
- );
705
-
706
  }
707
 
708
 
@@ -714,194 +675,195 @@ class widget_context {
714
 
715
  function make_simple_checkbox( $control_args, $option, $label ) {
716
 
717
- if ( isset( $control_args['settings'][ $option ] ) && $control_args['settings'][ $option ] )
718
  $value = true;
719
- else
720
  $value = false;
 
721
 
722
  return sprintf(
723
- '<label class="wc-field-checkbox-%s" data-widget-id="%s">
724
- <input type="hidden" value="0" name="%s[%s]" />
725
- <input type="checkbox" value="1" name="%s[%s]" %s />&nbsp;%s
726
- </label>',
727
- $this->get_field_classname( $option ),
728
- esc_attr( $control_args['widget_id'] ),
729
- // Input hidden
730
- $control_args['input_prefix'],
731
- esc_attr( $option ),
732
- // Input value
733
- $control_args['input_prefix'],
734
- esc_attr( $option ),
735
- checked( $value, true, false ),
736
- // Label
737
- esc_html( $label )
738
- );
739
 
740
  }
741
 
742
 
743
  function make_simple_textarea( $control_args, $option, $label = null ) {
744
-
745
- if ( isset( $control_args['settings'][ $option ] ) )
746
  $value = esc_textarea( $control_args['settings'][ $option ] );
747
- else
748
  $value = '';
 
749
 
750
  return sprintf(
751
- '<label class="wc-field-textarea-%s" data-widget-id="%s">
752
- <strong>%s</strong>
753
- <textarea name="%s[%s]">%s</textarea>
754
- </label>',
755
- $this->get_field_classname( $option ),
756
- esc_attr( $control_args['widget_id'] ),
757
- // Label
758
- esc_html( $label ),
759
- // Input
760
- $control_args['input_prefix'],
761
- $option,
762
- $value
763
- );
764
-
765
  }
766
 
767
 
768
- function make_simple_textfield( $control_args, $option, $label_before = null, $label_after = null) {
769
-
770
- if ( isset( $control_args['settings'][ $option ] ) )
771
  $value = esc_attr( $control_args['settings'][ $option ] );
772
- else
773
  $value = false;
 
774
 
775
  return sprintf(
776
- '<label class="wc-field-text-%s" data-widget-id="%s">
777
- %s
778
- <input type="text" name="%s[%s]" value="%s" />
779
- %s
780
- </label>',
781
- $this->get_field_classname( $option ),
782
- esc_attr( $control_args['widget_id'] ),
783
- // Before
784
- $label_before,
785
- // Input
786
- $control_args['input_prefix'],
787
- $option,
788
- esc_attr( $value ),
789
- // After
790
- esc_html( $label_after )
791
- );
792
-
793
  }
794
 
795
 
796
  function make_simple_dropdown( $control_args, $option, $selection = array(), $label_before = null, $label_after = null ) {
797
-
798
  $options = array();
799
 
800
- if ( isset( $control_args['settings'][ $option ] ) )
801
  $value = $control_args['settings'][ $option ];
802
- else
803
  $value = false;
 
804
 
805
- if ( empty( $selection ) )
806
  $options[] = sprintf(
807
- '<option value="">%s</option>',
808
- esc_html__( 'No options available', 'widget-context' )
809
- );
 
810
 
811
- foreach ( $selection as $sid => $svalue )
812
  $options[] = sprintf(
813
- '<option value="%s" %s>%s</option>',
814
- esc_attr( $sid ),
815
- selected( $value, $sid, false ),
816
- esc_html( $svalue )
817
- );
 
818
 
819
  return sprintf(
820
- '<label class="wc-field-select-%s" data-widget-id="%s">
 
 
821
  %s
822
- <select name="%s[%s]">
823
- %s
824
- </select>
825
- %s
826
- </label>',
827
- $this->get_field_classname( $option ),
828
- esc_attr( $control_args['widget_id'] ),
829
- // Before
830
- $label_before,
831
- // Input
832
- $control_args['input_prefix'],
833
- $option,
834
- implode( '', $options ),
835
- // After
836
- $label_after
837
- );
838
-
839
  }
840
 
841
 
842
  /**
843
  * Returns [part1][part2][partN] from array( 'part1', 'part2', 'part3' )
844
- * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
 
845
  * @return string i.e. [part1][part2][partN]
846
  */
847
  function get_field_name( $parts ) {
848
-
849
  return esc_attr( sprintf( '[%s]', implode( '][', $parts ) ) );
850
-
851
  }
852
 
853
  function get_field_classname( $name ) {
854
-
855
- if ( is_array( $name ) )
856
  $name = end( $name );
 
857
 
858
  return sanitize_html_class( str_replace( '_', '-', $name ) );
859
-
860
  }
861
 
862
 
863
  /**
864
  * Given option keys return its value
865
- * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
866
- * @param array $options i.e. array( 'part1' => array( 'part2' => array( 'part3' => 'VALUE' ) ) )
 
867
  * @return string Returns option value
868
  */
869
  function get_field_value( $parts, $options = null ) {
870
-
871
- if ( $options == null )
872
  $options = $this->context_options;
 
873
 
874
  $value = false;
875
 
876
- if ( empty( $parts ) || ! is_array( $parts ) )
877
  return false;
 
878
 
879
  $part = array_shift( $parts );
880
 
881
- if ( ! empty( $parts ) && isset( $options[ $part ] ) && is_array( $options[ $part ] ) )
882
  $value = $this->get_field_value( $parts, $options[ $part ] );
883
- elseif ( isset( $options[ $part ] ) )
884
  return $options[ $part ];
 
885
 
886
  return $value;
887
-
888
  }
889
 
890
 
891
  function fix_legacy_options( $options ) {
892
-
893
- if ( empty( $options ) || ! is_array( $options ) )
894
  return $options;
 
895
 
896
  foreach ( $options as $widget_id => $option ) {
897
-
898
  // This doesn't have an include/exclude rule defined
899
- if ( ! isset( $option['incexc'] ) )
900
  unset( $options[ $widget_id ] );
 
901
 
902
  // We moved from [incexc] = 1/0 to [incexc][condition] = 1/0
903
- if ( isset( $option['incexc'] ) && ! is_array( $option['incexc'] ) )
904
  $options[ $widget_id ]['incexc'] = array( 'condition' => $option['incexc'] );
 
905
 
906
  // Move notes from "general" group to "admin_notes"
907
  if ( isset( $option['general']['notes'] ) ) {
@@ -910,17 +872,16 @@ class widget_context {
910
  }
911
 
912
  // We moved word count out of location context group
913
- if ( isset( $option['location']['check_wordcount'] ) )
914
  $options[ $widget_id ]['word_count'] = array(
915
- 'check_wordcount' => true,
916
- 'check_wordcount_type' => $option['location']['check_wordcount_type'],
917
- 'word_count' => $option['location']['word_count']
918
- );
919
-
920
  }
921
 
922
  return $options;
923
-
924
  }
925
 
926
 
@@ -931,7 +892,6 @@ class widget_context {
931
 
932
 
933
  function widget_context_settings_menu() {
934
-
935
  add_options_page(
936
  __( 'Widget Context Settings', 'widget-context' ),
937
  __( 'Widget Context', 'widget-context' ),
@@ -939,57 +899,54 @@ class widget_context {
939
  $this->settings_name,
940
  array( $this, 'widget_context_admin_view' )
941
  );
942
-
943
  }
944
 
945
 
946
  function widget_context_settings_init() {
947
-
948
  register_setting( $this->settings_name, $this->settings_name );
949
-
950
  }
951
 
952
 
953
  function widget_context_admin_view() {
954
-
955
  $context_controls = array();
956
 
957
  foreach ( $this->get_contexts() as $context_id => $context_args ) {
958
-
959
  // Hide core modules from being disabled
960
- if ( isset( $context_args['type'] ) && $context_args['type'] == 'core' )
961
  continue;
 
962
 
963
- if ( ! empty( $context_args['description'] ) )
964
  $context_description = sprintf(
965
  '<p class="context-desc">%s</p>',
966
  esc_html( $context_args['description'] )
967
  );
968
- else
969
  $context_description = null;
 
970
 
971
  // Enable new modules by default
972
- if ( ! isset( $this->context_settings['contexts'][ $context_id ] ) )
973
  $this->context_settings['contexts'][ $context_id ] = 1;
 
974
 
975
  $context_controls[] = sprintf(
976
- '<li class="context-%s">
977
- <label>
978
- <input type="hidden" name="%s[contexts][%s]" value="0" />
979
- <input type="checkbox" name="%s[contexts][%s]" value="1" %s /> %s
980
- </label>
981
- %s
982
- </li>',
983
- esc_attr( $context_id ),
984
- $this->settings_name,
985
- esc_attr( $context_id ),
986
- $this->settings_name,
987
- esc_attr( $context_id ),
988
- checked( $this->context_settings['contexts'][ $context_id ], 1, false ),
989
- esc_html( $context_args['label'] ),
990
- $context_description
991
- );
992
-
993
  }
994
 
995
  ?>
@@ -1043,7 +1000,7 @@ class widget_context {
1043
  <p><?php esc_html_e( 'Subscribe to receive news & updates about the plugin.', 'widget-context' ); ?></p>
1044
  <form action="//osc.us2.list-manage.com/subscribe/post?u=e8d173fc54c0fc4286a2b52e8&amp;id=8afe96c5a3" method="post" target="_blank">
1045
  <?php $user = wp_get_current_user(); ?>
1046
- <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>
1047
  <p><label><?php _e( 'Your Email', 'widget-context' ); ?>: <input type="text" name="EMAIL" value="<?php echo esc_attr( $user->user_email ); ?>" /></label></p>
1048
  <p><input class="button" name="subscribe" type="submit" value="<?php _e( 'Subscribe', 'widget-context' ); ?>" /></p>
1049
  </form>
@@ -1074,32 +1031,7 @@ class widget_context {
1074
 
1075
 
1076
  public function get_sidebars_widgets_copy() {
1077
-
1078
  return $this->sidebars_widgets_copy;
1079
-
1080
- }
1081
-
1082
-
1083
- function widget_context_debug_bar_init( $panels ) {
1084
-
1085
- include $this->plugin_path . '/debug/debug-bar.php';
1086
-
1087
- if ( class_exists( 'Debug_Widget_Context' ) )
1088
- $panels[] = new Debug_Widget_Context();
1089
-
1090
- return $panels;
1091
-
1092
- }
1093
-
1094
-
1095
- function widget_context_debug_bar_scripts() {
1096
-
1097
- wp_enqueue_script(
1098
- 'widget-context-debug-js',
1099
- $this->asset_url( 'debug/debug.js' ),
1100
- array( 'jquery' )
1101
- );
1102
-
1103
  }
1104
 
1105
  /**
3
  /**
4
  * Widget Context plugin core.
5
  */
6
+ class WidgetContext {
7
 
8
  private $asset_version = '1.0.4';
9
  private $sidebars_widgets;
11
  private $settings_name = 'widget_context_settings'; // Widget Context global settings
12
  private $sidebars_widgets_copy;
13
 
 
 
 
 
 
14
  private $context_options = array(); // Store visibility settings
15
  private $context_settings = array(); // Store admin settings
16
  private $contexts = array();
17
  private $plugin_path;
18
 
19
+ /**
20
+ * Start the plugin.
21
+ *
22
+ * @param string $path Absolute path to the plugin.
23
+ */
24
+ public function __construct( $path ) {
25
+ $this->plugin_path = $path;
26
+ }
27
 
28
+ /**
29
+ * Legacy singleton instance getter.
30
+ *
31
+ * @return WidgetContext
32
+ */
33
  static function instance() {
34
+ return self;
 
 
 
 
 
 
 
35
  }
36
 
37
  /**
38
+ * Interface for registering modules.
39
  *
40
+ * @param mixed $module Instance of the module.
41
+ *
42
+ * @return void
43
  */
44
+ public function register_module( $module ) {
45
+ $module->init();
46
  }
47
 
48
  /**
49
  * Hook into WP.
50
  */
51
  public function init() {
 
 
 
 
 
52
  // Define available widget contexts
53
  add_action( 'init', array( $this, 'define_widget_contexts' ), 5 );
54
 
73
 
74
  // Register admin settings
75
  add_action( 'admin_init', array( $this, 'widget_context_settings_init' ) );
 
 
 
 
76
  }
77
 
78
 
79
  function define_widget_contexts() {
 
80
  $this->context_options = apply_filters(
81
+ 'widget_context_options',
82
+ (array) get_option( $this->options_name, array() )
83
+ );
84
 
85
  $this->context_settings = wp_parse_args(
86
+ (array) get_option( $this->settings_name, array() ),
87
+ array(
88
+ 'contexts' => array(),
89
+ )
90
+ );
 
 
 
 
 
 
 
 
91
 
92
  // Default context
93
  $default_contexts = array(
100
  'location' => array(
101
  'label' => __( 'Global Sections', 'widget-context' ),
102
  'description' => __( 'Based on standard WordPress template tags.', 'widget-context' ),
103
+ 'weight' => 10,
104
  ),
105
  'url' => array(
106
  'label' => __( 'Target by URL', 'widget-context' ),
107
  'description' => __( 'Based on URL patterns.', 'widget-context' ),
108
+ 'weight' => 20,
109
  ),
110
  'admin_notes' => array(
111
  'label' => __( 'Notes (invisible to public)', 'widget-context' ),
112
+ 'description' => __( 'Enables private notes on widget context settings.', 'widget-context' ),
113
+ 'weight' => 90,
114
+ ),
115
  );
116
 
117
  // Add default context controls and checks
118
  foreach ( $default_contexts as $context_name => $context_desc ) {
 
119
  add_filter( 'widget_context_control-' . $context_name, array( $this, 'control_' . $context_name ), 10, 2 );
120
  add_filter( 'widget_context_check-' . $context_name, array( $this, 'context_check_' . $context_name ), 10, 2 );
 
121
  }
122
 
123
  // Enable other plugins and themes to specify their own contexts
125
 
126
  // Sort contexts by their weight
127
  uasort( $this->contexts, array( $this, 'sort_context_by_weight' ) );
 
128
  }
129
 
130
 
131
  public function get_context_options( $widget_id = null ) {
132
+ if ( ! $widget_id ) {
 
133
  return $this->context_options;
134
+ }
135
 
136
+ if ( isset( $this->context_options[ $widget_id ] ) ) {
137
  return $this->context_options[ $widget_id ];
138
+ }
 
139
 
140
+ return null;
141
  }
142
 
143
 
144
  public function get_context_settings( $widget_id = null ) {
145
+ if ( ! $widget_id ) {
 
146
  return $this->context_settings;
147
+ }
148
 
149
+ if ( isset( $this->context_settings[ $widget_id ] ) ) {
150
  return $this->context_settings[ $widget_id ];
151
+ }
 
152
 
153
+ return null;
154
  }
155
 
156
 
157
  public function get_contexts() {
 
158
  return $this->contexts;
 
159
  }
160
 
161
 
162
  function sort_context_by_weight( $a, $b ) {
163
+ if ( ! isset( $a['weight'] ) ) {
 
164
  $a['weight'] = 10;
165
+ }
166
 
167
+ if ( ! isset( $b['weight'] ) ) {
168
  $b['weight'] = 10;
169
+ }
170
 
171
  return ( $a['weight'] < $b['weight'] ) ? -1 : 1;
 
172
  }
173
 
174
 
175
  function set_widget_contexts_frontend() {
 
176
  // Hide/show widgets for is_active_sidebar() to work
177
  add_filter( 'sidebars_widgets', array( $this, 'maybe_unset_widgets_by_context' ), 10 );
 
178
  }
179
 
180
 
181
  function admin_scripts( $page ) {
 
182
  // Enqueue only on widgets and customizer view
183
  if ( ! in_array( $page, array( 'widgets.php', 'settings_page_widget_context_settings' ), true ) ) {
184
  return;
197
  array( 'jquery' ),
198
  $this->asset_version
199
  );
 
200
  }
201
 
202
 
203
  function widget_context_controls( $object, $return, $instance ) {
 
204
  echo $this->display_widget_context( $object->id );
 
205
  }
206
 
207
 
208
  function save_widget_context_settings() {
209
+ if ( ! current_user_can( 'edit_theme_options' ) || empty( $_POST ) || ! isset( $_POST['wl'] ) ) {
 
210
  return;
211
+ }
212
 
213
  // Delete a widget
214
+ if ( isset( $_POST['delete_widget'] ) && isset( $_POST['the-widget-id'] ) ) {
215
  unset( $this->context_options[ $_POST['the-widget-id'] ] );
216
+ }
217
 
218
  // Add / Update
219
  $this->context_options = array_merge( $this->context_options, $_POST['wl'] );
222
  $all_widget_ids = array();
223
 
224
  // Get a lits of all widget IDs
225
+ foreach ( $sidebars_widgets as $widget_area => $widgets ) {
226
+ foreach ( $widgets as $widget_order => $widget_id ) {
227
  $all_widget_ids[] = $widget_id;
228
+ }
229
+ }
230
 
231
  // Remove non-existant widget contexts from the settings
232
+ foreach ( $this->context_options as $widget_id => $widget_context ) {
233
+ if ( ! in_array( $widget_id, $all_widget_ids ) ) {
234
  unset( $this->context_options[ $widget_id ] );
235
+ }
236
+ }
237
 
238
  update_option( $this->options_name, $this->context_options );
 
239
  }
240
 
241
 
242
  function maybe_unset_widgets_by_context( $sidebars_widgets ) {
 
243
  // Don't run this at the backend or before
244
  // post query has been run
245
+ if ( is_admin() ) {
246
  return $sidebars_widgets;
247
+ }
248
 
249
  // Return from cache if we have done the context checks already
250
+ if ( ! empty( $this->sidebars_widgets ) ) {
251
  return $this->sidebars_widgets;
252
+ }
253
 
254
  // Store a local copy of the original widget location
255
  $this->sidebars_widgets_copy = $sidebars_widgets;
256
 
257
  foreach ( $sidebars_widgets as $widget_area => $widget_list ) {
258
 
259
+ if ( 'wp_inactive_widgets' === $widget_area || empty( $widget_list ) ) {
260
  continue;
261
+ }
262
 
263
  foreach ( $widget_list as $pos => $widget_id ) {
264
 
265
+ if ( ! $this->check_widget_visibility( $widget_id ) ) {
266
  unset( $sidebars_widgets[ $widget_area ][ $pos ] );
267
+ }
268
  }
 
269
  }
270
 
271
  // Store in class cache
272
  $this->sidebars_widgets = $sidebars_widgets;
273
 
274
  return $sidebars_widgets;
 
275
  }
276
 
277
 
278
  function check_widget_visibility( $widget_id ) {
 
279
  // Check if this widget even has context set
280
+ if ( ! isset( $this->context_options[ $widget_id ] ) ) {
281
  return true;
282
+ }
283
 
284
  $matches = array();
285
 
286
  foreach ( $this->get_contexts() as $context_id => $context_settings ) {
 
287
  // This context check has been disabled in the plugin settings
288
+ if ( isset( $this->context_settings['contexts'][ $context_id ] ) && ! $this->context_settings['contexts'][ $context_id ] ) {
289
  continue;
290
+ }
291
 
292
  // Make sure that context settings for this widget are defined
293
+ if ( ! isset( $this->context_options[ $widget_id ][ $context_id ] ) ) {
294
  $widget_context_args = array();
295
+ } else {
296
  $widget_context_args = $this->context_options[ $widget_id ][ $context_id ];
297
+ }
298
 
299
  $matches[ $context_id ] = apply_filters(
300
+ 'widget_context_check-' . $context_id,
301
+ null,
302
+ $widget_context_args
303
+ );
 
304
  }
305
 
306
  // Get the match rule for this widget (show/hide/selected/notselected)
307
+ $match_rule = $this->context_options[ $widget_id ]['incexc']['condition'];
308
 
309
  // Force show or hide the widget!
310
+ if ( 'show' === $match_rule ) {
311
  return true;
312
+ } elseif ( 'hide' === $match_rule ) {
313
  return false;
314
+ }
315
 
316
+ $inc = false;
317
+
318
+ if ( 'selected' === $match_rule ) {
319
  $inc = true;
320
+ }
 
321
 
322
+ if ( $inc && in_array( true, $matches ) ) {
323
  return true;
324
+ } elseif ( ! $inc && ! in_array( true, $matches ) ) {
325
  return true;
326
+ }
 
327
 
328
+ return false;
329
  }
330
 
331
 
334
  */
335
 
336
  function context_check_incexc( $check, $settings ) {
 
337
  return $check;
 
338
  }
339
 
340
 
341
  function context_check_location( $check, $settings ) {
 
342
  $status = array(
343
+ 'is_front_page' => is_front_page(),
344
+ 'is_home' => is_home(),
345
+ 'is_singular' => is_singular(),
346
+ 'is_single' => is_singular( 'post' ),
347
+ 'is_page' => ( is_page() && ! is_front_page() ),
348
+ 'is_attachment' => is_attachment(),
349
+ 'is_search' => is_search(),
350
+ 'is_404' => is_404(),
351
+ 'is_archive' => is_archive(),
352
+ 'is_date' => is_date(),
353
+ 'is_day' => is_day(),
354
+ 'is_month' => is_month(),
355
+ 'is_year' => is_year(),
356
+ 'is_category' => is_category(),
357
+ 'is_tag' => is_tag(),
358
+ 'is_author' => is_author(),
359
+ );
360
 
361
  $matched = array_intersect_assoc( $settings, $status );
362
 
363
+ if ( ! empty( $matched ) ) {
364
  return true;
365
+ }
366
 
367
  return $check;
 
368
  }
369
 
370
 
380
  static $path;
381
 
382
  $settings = wp_parse_args(
383
+ $settings,
384
+ array(
385
+ 'urls' => null,
386
+ )
387
+ );
388
 
389
  $urls = trim( $settings['urls'] );
390
 
500
  */
501
 
502
  function display_widget_context( $widget_id = null ) {
 
503
  $controls = array();
504
  $controls_disabled = array();
505
  $controls_core = array();
507
  foreach ( $this->contexts as $context_name => $context_settings ) {
508
 
509
  $context_classes = array(
510
+ 'context-group',
511
+ sprintf( 'context-group-%s', esc_attr( $context_name ) ),
512
+ );
513
 
514
  // Hide this context from the admin UX. We can't remove them
515
  // because settings will get lost if this page is submitted.
524
  }
525
 
526
  $control_args = array(
527
+ 'name' => $context_name,
528
+ 'input_prefix' => 'wl' . $this->get_field_name( array( $widget_id, $context_name ) ),
529
+ 'settings' => $this->get_field_value( array( $widget_id, $context_name ) ),
530
+ 'widget_id' => $widget_id,
531
+ );
532
 
533
  $context_controls = apply_filters( 'widget_context_control-' . $context_name, $control_args );
534
  $context_classes = apply_filters( 'widget_context_classes-' . $context_name, $context_classes, $control_args );
535
 
536
  if ( ! empty( $context_controls ) && is_string( $context_controls ) ) {
 
537
  $controls[ $context_name ] = sprintf(
538
+ '<div class="%s">
539
+ <h4 class="context-toggle">%s</h4>
540
+ <div class="context-group-wrap">
541
+ %s
542
+ </div>
543
+ </div>',
544
+ esc_attr( implode( ' ', $context_classes ) ),
545
+ esc_html( $context_settings['label'] ),
546
+ $context_controls
547
+ );
 
548
  }
 
549
  }
550
 
 
 
551
  // Non-core controls that should be visible if enabled
552
  $controls_not_core = array_diff( array_keys( $controls ), $controls_core );
553
 
557
  if ( empty( $controls ) || empty( $has_controls ) ) {
558
 
559
  if ( current_user_can( 'edit_theme_options' ) ) {
560
+ $controls = array(
561
+ sprintf(
562
  '<p class="error">%s</p>',
563
  sprintf(
564
  __( 'No widget controls enabled. You can enable them in <a href="%s">Widget Context settings</a>.', 'widget-context' ),
565
  admin_url( 'options-general.php?page=widget_context_settings' )
566
  )
567
+ ),
568
+ );
569
  } else {
570
+ $controls = array(
571
+ sprintf(
572
  '<p class="error">%s</p>',
573
  __( 'No widget controls enabled.', 'widget-context' )
574
+ ),
575
+ );
576
  }
 
577
  }
578
 
579
  $settings_link = '';
580
 
581
  if ( current_user_can( 'edit_theme_options' ) ) {
 
582
  $settings_link = sprintf(
583
+ '<a href="%s" class="widget-context-settings-link" title="%s">%s</a>',
584
+ admin_url( 'options-general.php?page=widget_context_settings' ),
585
+ esc_attr__( 'Widget Context Settings', 'widget-context' ),
586
+ esc_html__( 'Settings', 'widget-context' )
587
+ );
 
588
  }
589
 
590
  return sprintf(
591
+ '<div class="widget-context">
592
+ <div class="widget-context-header">
593
+ <h3>%s</h3>
594
+ %s
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
+ $settings_link,
602
+ // Inslide classes
603
+ esc_attr( $widget_id ),
604
+ esc_attr( $widget_id ),
605
+ // Controls
606
+ implode( '', $controls )
607
+ );
608
 
609
  }
610
 
611
 
612
  function control_incexc( $control_args ) {
 
613
  $options = array(
614
+ 'show' => __( 'Show widget everywhere', 'widget-context' ),
615
+ 'selected' => __( 'Show widget on selected', 'widget-context' ),
616
+ 'notselected' => __( 'Hide widget on selected', 'widget-context' ),
617
+ 'hide' => __( 'Hide widget everywhere', 'widget-context' ),
618
+ );
619
 
620
  return $this->make_simple_dropdown( $control_args, 'condition', $options );
 
621
  }
622
 
623
 
624
  function control_location( $control_args ) {
 
625
  $options = array(
626
+ 'is_front_page' => __( 'Front page', 'widget-context' ),
627
+ 'is_home' => __( 'Blog page', 'widget-context' ),
628
+ 'is_singular' => __( 'All posts, pages and custom post types', 'widget-context' ),
629
+ 'is_single' => __( 'All posts', 'widget-context' ),
630
+ 'is_page' => __( 'All pages', 'widget-context' ),
631
+ 'is_attachment' => __( 'All attachments', 'widget-context' ),
632
+ 'is_search' => __( 'Search results', 'widget-context' ),
633
+ 'is_404' => __( '404 error page', 'widget-context' ),
634
+ 'is_archive' => __( 'All archives', 'widget-context' ),
635
+ 'is_date' => __( 'All date archives', 'widget-context' ),
636
+ 'is_day' => __( 'Daily archives', 'widget-context' ),
637
+ 'is_month' => __( 'Monthly archives', 'widget-context' ),
638
+ 'is_year' => __( 'Yearly archives', 'widget-context' ),
639
+ 'is_category' => __( 'All category archives', 'widget-context' ),
640
+ 'is_tag' => __( 'All tag archives', 'widget-context' ),
641
+ 'is_author' => __( 'All author archives', 'widget-context' ),
642
+ );
643
 
644
+ foreach ( $options as $option => $label ) {
645
  $out[] = $this->make_simple_checkbox( $control_args, $option, $label );
646
+ }
647
 
648
  return implode( '', $out );
 
649
  }
650
 
651
 
652
  function control_url( $control_args ) {
 
653
  return sprintf(
654
+ '<div>%s</div>
655
+ <p class="help">%s</p>',
656
+ $this->make_simple_textarea( $control_args, 'urls' ),
657
+ __( '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' )
658
+ );
 
659
  }
660
 
661
 
662
  function control_admin_notes( $control_args ) {
 
663
  return sprintf(
664
+ '<div>%s</div>',
665
+ $this->make_simple_textarea( $control_args, 'notes' )
666
+ );
 
667
  }
668
 
669
 
675
 
676
  function make_simple_checkbox( $control_args, $option, $label ) {
677
 
678
+ if ( isset( $control_args['settings'][ $option ] ) && $control_args['settings'][ $option ] ) {
679
  $value = true;
680
+ } else {
681
  $value = false;
682
+ }
683
 
684
  return sprintf(
685
+ '<label class="wc-field-checkbox-%s" data-widget-id="%s">
686
+ <input type="hidden" value="0" name="%s[%s]" />
687
+ <input type="checkbox" value="1" name="%s[%s]" %s />&nbsp;%s
688
+ </label>',
689
+ $this->get_field_classname( $option ),
690
+ esc_attr( $control_args['widget_id'] ),
691
+ // Input hidden
692
+ $control_args['input_prefix'],
693
+ esc_attr( $option ),
694
+ // Input value
695
+ $control_args['input_prefix'],
696
+ esc_attr( $option ),
697
+ checked( $value, true, false ),
698
+ // Label
699
+ esc_html( $label )
700
+ );
701
 
702
  }
703
 
704
 
705
  function make_simple_textarea( $control_args, $option, $label = null ) {
706
+ if ( isset( $control_args['settings'][ $option ] ) ) {
 
707
  $value = esc_textarea( $control_args['settings'][ $option ] );
708
+ } else {
709
  $value = '';
710
+ }
711
 
712
  return sprintf(
713
+ '<label class="wc-field-textarea-%s" data-widget-id="%s">
714
+ <strong>%s</strong>
715
+ <textarea name="%s[%s]">%s</textarea>
716
+ </label>',
717
+ $this->get_field_classname( $option ),
718
+ esc_attr( $control_args['widget_id'] ),
719
+ // Label
720
+ esc_html( $label ),
721
+ // Input
722
+ $control_args['input_prefix'],
723
+ $option,
724
+ $value
725
+ );
 
726
  }
727
 
728
 
729
+ function make_simple_textfield( $control_args, $option, $label_before = null, $label_after = null ) {
730
+ if ( isset( $control_args['settings'][ $option ] ) ) {
 
731
  $value = esc_attr( $control_args['settings'][ $option ] );
732
+ } else {
733
  $value = false;
734
+ }
735
 
736
  return sprintf(
737
+ '<label class="wc-field-text-%s" data-widget-id="%s">
738
+ %s
739
+ <input type="text" name="%s[%s]" value="%s" />
740
+ %s
741
+ </label>',
742
+ $this->get_field_classname( $option ),
743
+ esc_attr( $control_args['widget_id'] ),
744
+ // Before
745
+ $label_before,
746
+ // Input
747
+ $control_args['input_prefix'],
748
+ $option,
749
+ esc_attr( $value ),
750
+ // After
751
+ esc_html( $label_after )
752
+ );
 
753
  }
754
 
755
 
756
  function make_simple_dropdown( $control_args, $option, $selection = array(), $label_before = null, $label_after = null ) {
 
757
  $options = array();
758
 
759
+ if ( isset( $control_args['settings'][ $option ] ) ) {
760
  $value = $control_args['settings'][ $option ];
761
+ } else {
762
  $value = false;
763
+ }
764
 
765
+ if ( empty( $selection ) ) {
766
  $options[] = sprintf(
767
+ '<option value="">%s</option>',
768
+ esc_html__( 'No options available', 'widget-context' )
769
+ );
770
+ }
771
 
772
+ foreach ( $selection as $sid => $svalue ) {
773
  $options[] = sprintf(
774
+ '<option value="%s" %s>%s</option>',
775
+ esc_attr( $sid ),
776
+ selected( $value, $sid, false ),
777
+ esc_html( $svalue )
778
+ );
779
+ }
780
 
781
  return sprintf(
782
+ '<label class="wc-field-select-%s" data-widget-id="%s">
783
+ %s
784
+ <select name="%s[%s]">
785
  %s
786
+ </select>
787
+ %s
788
+ </label>',
789
+ $this->get_field_classname( $option ),
790
+ esc_attr( $control_args['widget_id'] ),
791
+ // Before
792
+ $label_before,
793
+ // Input
794
+ $control_args['input_prefix'],
795
+ $option,
796
+ implode( '', $options ),
797
+ // After
798
+ $label_after
799
+ );
 
 
 
800
  }
801
 
802
 
803
  /**
804
  * Returns [part1][part2][partN] from array( 'part1', 'part2', 'part3' )
805
+ *
806
+ * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
807
  * @return string i.e. [part1][part2][partN]
808
  */
809
  function get_field_name( $parts ) {
 
810
  return esc_attr( sprintf( '[%s]', implode( '][', $parts ) ) );
 
811
  }
812
 
813
  function get_field_classname( $name ) {
814
+ if ( is_array( $name ) ) {
 
815
  $name = end( $name );
816
+ }
817
 
818
  return sanitize_html_class( str_replace( '_', '-', $name ) );
 
819
  }
820
 
821
 
822
  /**
823
  * Given option keys return its value
824
+ *
825
+ * @param array $parts i.e. array( 'part1', 'part2', 'part3' )
826
+ * @param array $options i.e. array( 'part1' => array( 'part2' => array( 'part3' => 'VALUE' ) ) )
827
  * @return string Returns option value
828
  */
829
  function get_field_value( $parts, $options = null ) {
830
+ if ( null === $options ) {
 
831
  $options = $this->context_options;
832
+ }
833
 
834
  $value = false;
835
 
836
+ if ( empty( $parts ) || ! is_array( $parts ) ) {
837
  return false;
838
+ }
839
 
840
  $part = array_shift( $parts );
841
 
842
+ if ( ! empty( $parts ) && isset( $options[ $part ] ) && is_array( $options[ $part ] ) ) {
843
  $value = $this->get_field_value( $parts, $options[ $part ] );
844
+ } elseif ( isset( $options[ $part ] ) ) {
845
  return $options[ $part ];
846
+ }
847
 
848
  return $value;
 
849
  }
850
 
851
 
852
  function fix_legacy_options( $options ) {
853
+ if ( empty( $options ) || ! is_array( $options ) ) {
 
854
  return $options;
855
+ }
856
 
857
  foreach ( $options as $widget_id => $option ) {
 
858
  // This doesn't have an include/exclude rule defined
859
+ if ( ! isset( $option['incexc'] ) ) {
860
  unset( $options[ $widget_id ] );
861
+ }
862
 
863
  // We moved from [incexc] = 1/0 to [incexc][condition] = 1/0
864
+ if ( isset( $option['incexc'] ) && ! is_array( $option['incexc'] ) ) {
865
  $options[ $widget_id ]['incexc'] = array( 'condition' => $option['incexc'] );
866
+ }
867
 
868
  // Move notes from "general" group to "admin_notes"
869
  if ( isset( $option['general']['notes'] ) ) {
872
  }
873
 
874
  // We moved word count out of location context group
875
+ if ( isset( $option['location']['check_wordcount'] ) ) {
876
  $options[ $widget_id ]['word_count'] = array(
877
+ 'check_wordcount' => true,
878
+ 'check_wordcount_type' => $option['location']['check_wordcount_type'],
879
+ 'word_count' => $option['location']['word_count'],
880
+ );
881
+ }
882
  }
883
 
884
  return $options;
 
885
  }
886
 
887
 
892
 
893
 
894
  function widget_context_settings_menu() {
 
895
  add_options_page(
896
  __( 'Widget Context Settings', 'widget-context' ),
897
  __( 'Widget Context', 'widget-context' ),
899
  $this->settings_name,
900
  array( $this, 'widget_context_admin_view' )
901
  );
 
902
  }
903
 
904
 
905
  function widget_context_settings_init() {
 
906
  register_setting( $this->settings_name, $this->settings_name );
 
907
  }
908
 
909
 
910
  function widget_context_admin_view() {
 
911
  $context_controls = array();
912
 
913
  foreach ( $this->get_contexts() as $context_id => $context_args ) {
 
914
  // Hide core modules from being disabled
915
+ if ( isset( $context_args['type'] ) && 'core' === $context_args['type'] ) {
916
  continue;
917
+ }
918
 
919
+ if ( ! empty( $context_args['description'] ) ) {
920
  $context_description = sprintf(
921
  '<p class="context-desc">%s</p>',
922
  esc_html( $context_args['description'] )
923
  );
924
+ } else {
925
  $context_description = null;
926
+ }
927
 
928
  // Enable new modules by default
929
+ if ( ! isset( $this->context_settings['contexts'][ $context_id ] ) ) {
930
  $this->context_settings['contexts'][ $context_id ] = 1;
931
+ }
932
 
933
  $context_controls[] = sprintf(
934
+ '<li class="context-%s">
935
+ <label>
936
+ <input type="hidden" name="%s[contexts][%s]" value="0" />
937
+ <input type="checkbox" name="%s[contexts][%s]" value="1" %s /> %s
938
+ </label>
939
+ %s
940
+ </li>',
941
+ esc_attr( $context_id ),
942
+ $this->settings_name,
943
+ esc_attr( $context_id ),
944
+ $this->settings_name,
945
+ esc_attr( $context_id ),
946
+ checked( $this->context_settings['contexts'][ $context_id ], 1, false ),
947
+ esc_html( $context_args['label'] ),
948
+ $context_description
949
+ );
 
950
  }
951
 
952
  ?>
1000
  <p><?php esc_html_e( 'Subscribe to receive news & updates about the plugin.', 'widget-context' ); ?></p>
1001
  <form action="//osc.us2.list-manage.com/subscribe/post?u=e8d173fc54c0fc4286a2b52e8&amp;id=8afe96c5a3" method="post" target="_blank">
1002
  <?php $user = wp_get_current_user(); ?>
1003
+ <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>
1004
  <p><label><?php _e( 'Your Email', 'widget-context' ); ?>: <input type="text" name="EMAIL" value="<?php echo esc_attr( $user->user_email ); ?>" /></label></p>
1005
  <p><input class="button" name="subscribe" type="submit" value="<?php _e( 'Subscribe', 'widget-context' ); ?>" /></p>
1006
  </form>
1031
 
1032
 
1033
  public function get_sidebars_widgets_copy() {
 
1034
  return $this->sidebars_widgets_copy;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1035
  }
1036
 
1037
  /**
{class → src}/modules/custom-post-types-taxonomies/module.php RENAMED
@@ -1,161 +1,137 @@
1
  <?php
2
 
3
- // Check for Widget Context plugin
4
- if ( ! class_exists( 'widget_context' ) )
5
- die;
6
 
7
-
8
- // Go!
9
- WidgetContextCustomCPTTax::instance();
10
-
11
-
12
- class WidgetContextCustomCPTTax {
13
 
14
  private static $instance;
15
  private $wc;
16
  public $post_types;
17
  public $taxonomies;
18
 
19
-
20
- static function instance() {
21
-
22
- if ( ! self::$instance )
23
- self::$instance = new self();
24
-
25
- return self::$instance;
26
-
27
  }
28
 
29
-
30
- private function __construct() {
31
-
32
- $this->wc = widget_context::instance();
33
-
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
  }
41
 
42
-
43
  function set_objects() {
44
-
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
- );
64
-
65
  }
66
 
67
 
68
  function add_context( $contexts ) {
69
-
70
- $contexts[ 'custom_post_types_taxonomies' ] = array(
71
- 'label' => __( 'Custom Post Types and Taxonomies', 'widget-context' ),
72
- 'description' => __( 'Context based custom post types and taxonomies', 'widget-context' ),
73
- 'weight' => 10
74
- );
75
 
76
  return $contexts;
77
-
78
  }
79
 
80
 
81
  function context_check( $check, $settings ) {
82
-
83
- if ( empty( $settings ) )
84
  return $check;
 
85
 
86
  $status = array();
87
 
88
- if ( ! is_array( $this->post_types ) )
89
  $this->set_objects();
 
90
 
91
  foreach ( $this->post_types as $post_type => $post_type_settings ) {
92
 
93
- if ( isset( $settings[ 'is_singular-' . $post_type ] ) && $settings[ 'is_singular-' . $post_type ] )
94
  $status[ 'is_singular-' . $post_type ] = is_singular( $post_type );
 
95
 
96
- if ( isset( $settings[ 'is_archive-' . $post_type ] ) && $settings[ 'is_archive-' . $post_type ] )
97
  $status[ 'is_archive-' . $post_type ] = is_post_type_archive( $post_type );
98
-
99
  }
100
 
101
  foreach ( $this->taxonomies as $taxonomy => $tax_settings ) {
102
 
103
- if ( isset( $settings[ 'is_tax-' . $taxonomy ] ) && $settings[ 'is_tax-' . $taxonomy ] )
104
  $status[ 'is_tax-' . $taxonomy ] = is_tax( $taxonomy );
105
-
106
  }
107
 
108
  $matched = array_intersect_assoc( $settings, $status );
109
 
110
- if ( ! empty( $matched ) )
111
  return true;
 
112
 
113
  return $check;
114
-
115
  }
116
 
117
 
118
  function context_controls( $control_args ) {
119
-
120
  $options = array();
121
  $out = array();
122
 
123
- if ( ! is_array( $this->post_types ) )
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
  }
149
 
150
- foreach ( $options as $option => $label )
151
  $out[] = $this->wc->make_simple_checkbox( $control_args, $option, $label );
 
152
 
153
- if ( ! empty( $out ) )
154
  return implode( '', $out );
 
155
 
156
  return sprintf( '%s', esc_html__( 'None.', 'widget-context' ) );
157
-
158
  }
159
 
160
-
161
  }
1
  <?php
2
 
 
 
 
3
 
4
+ class WidgetContextCustomCptTax {
 
 
 
 
 
5
 
6
  private static $instance;
7
  private $wc;
8
  public $post_types;
9
  public $taxonomies;
10
 
11
+ public function __construct( $plugin ) {
12
+ $this->wc = $plugin;
 
 
 
 
 
 
13
  }
14
 
15
+ public function init() {
 
 
 
 
16
  add_filter( 'widget_contexts', array( $this, 'add_context' ) );
17
 
18
  add_filter( 'widget_context_control-custom_post_types_taxonomies', array( $this, 'context_controls' ), 10, 2 );
19
 
20
  add_filter( 'widget_context_check-custom_post_types_taxonomies', array( $this, 'context_check' ), 10, 2 );
 
21
  }
22
 
 
23
  function set_objects() {
24
+ if ( is_array( $this->post_types ) ) {
 
25
  return;
26
+ }
27
 
28
  $this->post_types = get_post_types(
29
+ array(
30
+ 'public' => true,
31
+ '_builtin' => false,
32
+ 'publicly_queryable' => true,
33
+ ),
34
+ 'objects'
35
+ );
36
 
37
  $this->taxonomies = get_taxonomies(
38
+ array(
39
+ 'public' => true,
40
+ '_builtin' => false,
41
+ ),
42
+ 'objects'
43
+ );
 
44
  }
45
 
46
 
47
  function add_context( $contexts ) {
48
+ $contexts['custom_post_types_taxonomies'] = array(
49
+ 'label' => __( 'Custom Post Types and Taxonomies', 'widget-context' ),
50
+ 'description' => __( 'Context based custom post types and taxonomies', 'widget-context' ),
51
+ 'weight' => 10,
52
+ );
 
53
 
54
  return $contexts;
 
55
  }
56
 
57
 
58
  function context_check( $check, $settings ) {
59
+ if ( empty( $settings ) ) {
 
60
  return $check;
61
+ }
62
 
63
  $status = array();
64
 
65
+ if ( ! is_array( $this->post_types ) ) {
66
  $this->set_objects();
67
+ }
68
 
69
  foreach ( $this->post_types as $post_type => $post_type_settings ) {
70
 
71
+ if ( isset( $settings[ 'is_singular-' . $post_type ] ) && $settings[ 'is_singular-' . $post_type ] ) {
72
  $status[ 'is_singular-' . $post_type ] = is_singular( $post_type );
73
+ }
74
 
75
+ if ( isset( $settings[ 'is_archive-' . $post_type ] ) && $settings[ 'is_archive-' . $post_type ] ) {
76
  $status[ 'is_archive-' . $post_type ] = is_post_type_archive( $post_type );
77
+ }
78
  }
79
 
80
  foreach ( $this->taxonomies as $taxonomy => $tax_settings ) {
81
 
82
+ if ( isset( $settings[ 'is_tax-' . $taxonomy ] ) && $settings[ 'is_tax-' . $taxonomy ] ) {
83
  $status[ 'is_tax-' . $taxonomy ] = is_tax( $taxonomy );
84
+ }
85
  }
86
 
87
  $matched = array_intersect_assoc( $settings, $status );
88
 
89
+ if ( ! empty( $matched ) ) {
90
  return true;
91
+ }
92
 
93
  return $check;
 
94
  }
95
 
96
 
97
  function context_controls( $control_args ) {
 
98
  $options = array();
99
  $out = array();
100
 
101
+ if ( ! is_array( $this->post_types ) ) {
102
  $this->set_objects();
103
+ }
104
 
105
  foreach ( $this->post_types as $post_type => $post_type_settings ) {
 
106
  $options[ 'is_singular-' . $post_type ] = sprintf(
107
+ __( 'All "%s" posts', 'widget-context' ),
108
+ $post_type_settings->label
109
+ );
110
 
111
+ if ( $post_type_settings->has_archive ) {
112
  $options[ 'is_archive-' . $post_type ] = sprintf(
113
  __( 'Archive of "%s" posts', 'widget-context' ),
114
  $post_type_settings->label
115
  );
116
+ }
117
  }
118
 
119
  foreach ( $this->taxonomies as $taxonomy => $tax_settings ) {
 
120
  $options[ 'is_tax-' . $taxonomy ] = sprintf(
121
+ __( 'All "%s" taxonomy archives', 'widget-context' ),
122
+ $tax_settings->label
123
+ );
 
124
  }
125
 
126
+ foreach ( $options as $option => $label ) {
127
  $out[] = $this->wc->make_simple_checkbox( $control_args, $option, $label );
128
+ }
129
 
130
+ if ( ! empty( $out ) ) {
131
  return implode( '', $out );
132
+ }
133
 
134
  return sprintf( '%s', esc_html__( 'None.', 'widget-context' ) );
 
135
  }
136
 
 
137
  }
src/modules/word-count/module.php ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class WidgetContextWordCount {
4
+
5
+ private static $instance;
6
+ private $wc;
7
+
8
+ var $words_on_page = 0;
9
+
10
+ public function __construct( $plugin ) {
11
+ $this->wc = $plugin;
12
+ }
13
+
14
+ public function init() {
15
+ // Check the number of words on page
16
+ add_action( 'wp', array( $this, 'count_words_on_page' ) );
17
+
18
+ // Define our context
19
+ add_filter( 'widget_contexts', array( $this, 'add_word_count_context' ) );
20
+
21
+ add_filter( 'widget_context_control-word_count', array( $this, 'control_word_count' ), 10, 2 );
22
+ add_filter( 'widget_context_check-word_count', array( $this, 'context_check_word_count' ), 10, 2 );
23
+ }
24
+
25
+ function add_word_count_context( $contexts ) {
26
+ $contexts['word_count'] = array(
27
+ 'label' => __( 'Word Count', 'widget-context' ),
28
+ 'description' => __( 'Context based on word count on the page.', 'widget-context' ),
29
+ 'weight' => 15,
30
+ );
31
+
32
+ return $contexts;
33
+ }
34
+
35
+
36
+ function count_words_on_page() {
37
+ global $wp_query;
38
+
39
+ if ( empty( $wp_query->posts ) || is_admin() ) {
40
+ return;
41
+ }
42
+
43
+ foreach ( $wp_query->posts as $post_data ) {
44
+ $this->words_on_page += str_word_count( strip_tags( strip_shortcodes( $post_data->post_content ) ) );
45
+ }
46
+ }
47
+
48
+
49
+ function context_check_word_count( $check, $settings ) {
50
+ $settings = wp_parse_args(
51
+ $settings,
52
+ array(
53
+ 'check_wordcount' => false,
54
+ 'word_count' => null,
55
+ 'check_wordcount_type' => null,
56
+ )
57
+ );
58
+
59
+ // Make sure this context check was enabled
60
+ if ( ! $settings['check_wordcount'] ) {
61
+ return $check;
62
+ }
63
+
64
+ $word_count = (int) $settings['word_count'];
65
+
66
+ // No word count specified, bail out
67
+ if ( ! $word_count ) {
68
+ return $check;
69
+ }
70
+
71
+ if ( 'less' === $settings['check_wordcount_type'] && $this->words_on_page < $word_count ) {
72
+ return true;
73
+ } elseif ( 'more' === $settings['check_wordcount_type'] && $this->words_on_page > $word_count ) {
74
+ return true;
75
+ }
76
+
77
+ return $check;
78
+ }
79
+
80
+
81
+ function control_word_count( $control_args ) {
82
+ return sprintf(
83
+ '<p>%s %s %s</p>',
84
+ $this->wc->make_simple_checkbox( $control_args, 'check_wordcount', __( 'Has', 'widget-context' ) ),
85
+ $this->wc->make_simple_dropdown(
86
+ $control_args,
87
+ 'check_wordcount_type',
88
+ array(
89
+ 'less' => __( 'less', 'widget-context' ),
90
+ 'more' => __(
91
+ 'more',
92
+ 'widget-context'
93
+ ),
94
+ ),
95
+ null,
96
+ __( 'than', 'widget-context' )
97
+ ),
98
+ $this->wc->make_simple_textfield( $control_args, 'word_count', null, __( 'words', 'widget-context' ) )
99
+ );
100
+ }
101
+
102
+ }
widget-context.php CHANGED
@@ -3,15 +3,20 @@
3
  * Plugin Name: Widget Context
4
  * Plugin URI: https://widgetcontext.com
5
  * Description: Show or hide widgets depending on the section of the site that is being viewed.
6
- * Version: 1.1.0
7
- * Author: Kaspars Dambis
8
- * Author URI: https://kaspars.net
9
  * Text Domain: widget-context
10
  */
11
 
12
- require_once dirname( __FILE__ ) . '/class/class-widget-context.php';
 
 
 
 
 
 
 
 
13
 
14
- // Go!
15
- $plugin = widget_context::instance();
16
- $plugin->set_path( dirname( __FILE__ ) );
17
  $plugin->init();
3
  * Plugin Name: Widget Context
4
  * Plugin URI: https://widgetcontext.com
5
  * Description: Show or hide widgets depending on the section of the site that is being viewed.
6
+ * Version: 1.1.1
7
+ * Author: Preseto
8
+ * Author URI: https://preseto.com
9
  * Text Domain: widget-context
10
  */
11
 
12
+ // TODO Switch to proper autoloading.
13
+ require_once dirname( __FILE__ ) . '/src/WidgetContext.php';
14
+ require_once dirname( __FILE__ ) . '/src/modules/custom-post-types-taxonomies/module.php';
15
+ require_once dirname( __FILE__ ) . '/src/modules/word-count/module.php';
16
+
17
+ $plugin = new WidgetContext( dirname( __FILE__ ) );
18
+
19
+ $plugin->register_module( new WidgetContextCustomCptTax( $plugin ) );
20
+ $plugin->register_module( new WidgetContextWordCount( $plugin ) );
21
 
 
 
 
22
  $plugin->init();
wp-cli.yaml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ path: /home/vagrant/code
2
+
3
+ config create:
4
+ dbname: homestead
5
+ dbuser: homestead
6
+ dbpass: secret