Completed
Push — update/move-connections-to-at-... ( 841220...b7fe64 )
by
unknown
08:33
created

widget_conditions_options()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
/**
5
 * Hide or show widgets conditionally.
6
 */
7
8
class Jetpack_Widget_Conditions {
9
	static $passed_template_redirect = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $passed_template_redirect.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
10
11
	public static function init() {
12
		if ( is_admin() ) {
13
			add_action( 'sidebar_admin_setup', array( __CLASS__, 'widget_admin_setup' ) );
14
			add_filter( 'widget_update_callback', array( __CLASS__, 'widget_update' ), 10, 3 );
15
			add_action( 'in_widget_form', array( __CLASS__, 'widget_conditions_admin' ), 10, 3 );
16
		} else if ( ! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) {
17
			add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) );
18
			add_filter( 'sidebars_widgets', array( __CLASS__, 'sidebars_widgets' ) );
19
			add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) );
20
		}
21
	}
22
23
	public static function widget_admin_setup() {
24
		if( is_rtl() ) {
25
			wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/rtl/widget-conditions-rtl.css', __FILE__ ) );
26
		} else {
27
			wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) );
28
		}
29
		wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) );
30
		wp_enqueue_script( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.js', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), 20140721, true );
31
32
		// Set up a single copy of all of the data that Widget Visibility needs.
33
		// This allows all widget conditions to reuse the same data, keeping page size down
34
		// and eliminating the AJAX calls we used to have to use to fetch the minor rule options.
35
		$widget_conditions_data = array();
36
37
		$widget_conditions_data['category'] = array();
38
		$widget_conditions_data['category'][] = array( '', __( 'All category pages', 'jetpack' ) );
39
40
		$categories = get_categories( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) );
41
		usort( $categories, array( __CLASS__, 'strcasecmp_name' ) );
42
43
		foreach ( $categories as $category ) {
44
			$widget_conditions_data['category'][] = array( (string) $category->term_id, $category->name );
45
		}
46
47
		$widget_conditions_data['loggedin'] = array();
48
		$widget_conditions_data['loggedin'][] = array( 'loggedin', __( 'Logged In', 'jetpack' ) );
49
		$widget_conditions_data['loggedin'][] = array( 'loggedout', __( 'Logged Out', 'jetpack' ) );
50
51
		$widget_conditions_data['author'] = array();
52
		$widget_conditions_data['author'][] = array( '', __( 'All author pages', 'jetpack' ) );
53
54
		$authors = get_users( array( 'orderby' => 'name', 'exclude_admin' => true ) );
55
56
		foreach ( $authors as $author ) {
57
			$widget_conditions_data['author'][] = array( (string) $author->ID, $author->display_name );
58
		}
59
60
		$widget_conditions_data['role'] = array();
61
62
		global $wp_roles;
63
64
		foreach ( $wp_roles->roles as $role_key => $role ) {
65
			$widget_conditions_data['role'][] = array( (string) $role_key, $role['name'] );
66
		}
67
68
		$widget_conditions_data['tag'] = array();
69
		$widget_conditions_data['tag'][] = array( '', __( 'All tag pages', 'jetpack' ) );
70
71
		$tags = get_tags( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) );
72
		usort( $tags, array( __CLASS__, 'strcasecmp_name' ) );
73
74
		foreach ( $tags as $tag ) {
75
			$widget_conditions_data['tag'][] = array( (string) $tag->term_id, $tag->name );
76
		}
77
78
		$widget_conditions_data['date'] = array();
79
		$widget_conditions_data['date'][] = array( '', __( 'All date archives', 'jetpack' ) );
80
		$widget_conditions_data['date'][] = array( 'day', __( 'Daily archives', 'jetpack' ) );
81
		$widget_conditions_data['date'][] = array( 'month', __( 'Monthly archives', 'jetpack' ) );
82
		$widget_conditions_data['date'][] = array( 'year', __( 'Yearly archives', 'jetpack' ) );
83
84
		$widget_conditions_data['page'] = array();
85
		$widget_conditions_data['page'][] = array( 'front', __( 'Front page', 'jetpack' ) );
86
		$widget_conditions_data['page'][] = array( 'posts', __( 'Posts page', 'jetpack' ) );
87
		$widget_conditions_data['page'][] = array( 'archive', __( 'Archive page', 'jetpack' ) );
88
		$widget_conditions_data['page'][] = array( '404', __( '404 error page', 'jetpack' ) );
89
		$widget_conditions_data['page'][] = array( 'search', __( 'Search results', 'jetpack' ) );
90
91
		$post_types = get_post_types( array( 'public' => true ), 'objects' );
92
93
		$widget_conditions_post_types = array();
94
95
		foreach ( $post_types as $post_type ) {
96
			$widget_conditions_post_types[] = array( 'post_type-' . $post_type->name, $post_type->labels->singular_name );
97
		}
98
99
		$widget_conditions_data['page'][] = array( __( 'Post type:', 'jetpack' ), $widget_conditions_post_types );
100
101
		$pages_dropdown = preg_replace( '/<\/?select[^>]*?>/i', '', wp_dropdown_pages( array( 'echo' => false ) ) );
102
103
		preg_match_all( '/value=.([0-9]+).[^>]*>([^<]+)</', $pages_dropdown, $page_ids_and_titles, PREG_SET_ORDER );
104
105
		$static_pages = array();
106
107
		foreach ( $page_ids_and_titles as $page_id_and_title ) {
0 ignored issues
show
Bug introduced by
The expression $page_ids_and_titles of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
108
			$static_pages[] = array( (string) $page_id_and_title[1], $page_id_and_title[2] );
109
		}
110
111
		$widget_conditions_data['page'][] = array( __( 'Static page:', 'jetpack' ), $static_pages );
112
113
		$widget_conditions_data['taxonomy'] = array();
114
		$widget_conditions_data['taxonomy'][] = array( '', __( 'All taxonomy pages', 'jetpack' ) );
115
116
		$taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' );
117
		usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) );
118
119
		foreach ( $taxonomies as $taxonomy ) {
120
			$taxonomy_terms = get_terms( array( $taxonomy->name ), array( 'number' => 250, 'hide_empty' => false ) );
121
122
			$widget_conditions_terms = array();
123
			$widget_conditions_terms[] = array( $taxonomy->name, __( 'All pages', 'jetpack' ) );
124
125
			foreach ( $taxonomy_terms as $term ) {
126
				$widget_conditions_terms[] = array( $taxonomy->name . '_tax_' . $term->term_id, $term->name );
127
			}
128
129
			$widget_conditions_data['taxonomy'][] = array( $taxonomy->labels->name . ':', $widget_conditions_terms );
130
		}
131
132
		wp_localize_script( 'widget-conditions', 'widget_conditions_data', $widget_conditions_data );
133
134
		// Save a list of the IDs of all pages that have children for dynamically showing the "Include children" checkbox.
135
		$all_pages = get_pages();
136
		$all_parents = array();
137
138
		foreach ( $all_pages as $page ) {
139
			if ( $page->post_parent ) {
140
				$all_parents[ (string) $page->post_parent ] = true;
141
			}
142
		}
143
144
		$front_page_id = get_option( 'page_on_front' );
145
146
		if ( isset( $all_parents[ $front_page_id ] ) ) {
147
			$all_parents[ 'front' ] = true;
148
		}
149
150
		wp_localize_script( 'widget-conditions', 'widget_conditions_parent_pages', $all_parents );
151
	}
152
153
	/**
154
	 * Add the widget conditions to each widget in the admin.
155
	 *
156
	 * @param $widget unused.
157
	 * @param $return unused.
158
	 * @param array $instance The widget settings.
159
	 */
160
	public static function widget_conditions_admin( $widget, $return, $instance ) {
161
		$conditions = array();
162
163
		if ( isset( $instance['conditions'] ) )
164
			$conditions = $instance['conditions'];
165
166
		if ( ! isset( $conditions['action'] ) )
167
			$conditions['action'] = 'show';
168
169
		if ( empty( $conditions['rules'] ) )
170
			$conditions['rules'][] = array( 'major' => '', 'minor' => '', 'has_children' => '' );
171
172
		?>
173
		<div class="widget-conditional <?php if ( empty( $_POST['widget-conditions-visible'] ) || $_POST['widget-conditions-visible'] == '0' ) { ?>widget-conditional-hide<?php } ?>">
174
			<input type="hidden" name="widget-conditions-visible" value="<?php if ( isset( $_POST['widget-conditions-visible'] ) ) { echo esc_attr( $_POST['widget-conditions-visible'] ); } else { ?>0<?php } ?>" />
175
			<?php if ( ! isset( $_POST['widget-conditions-visible'] ) ) { ?><a href="#" class="button display-options"><?php _e( 'Visibility', 'jetpack' ); ?></a><?php } ?>
176
			<div class="widget-conditional-inner">
177
				<div class="condition-top">
178
					<?php printf( _x( '%s if:', 'placeholder: dropdown menu to select widget visibility; hide if or show if', 'jetpack' ), '<select name="conditions[action]"><option value="show" ' . selected( $conditions['action'], 'show', false ) . '>' . esc_html_x( 'Show', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option><option value="hide" ' . selected( $conditions['action'], 'hide', false ) . '>' . esc_html_x( 'Hide', 'Used in the "%s if:" translation for the widget visibility dropdown', 'jetpack' ) . '</option></select>' ); ?>
179
				</div><!-- .condition-top -->
180
181
				<div class="conditions">
182
					<?php
183
184
					foreach ( $conditions['rules'] as $rule_index => $rule ) {
185
						$rule = wp_parse_args( $rule, array( 'major' => '', 'minor' => '', 'has_children' => '' ) );
186
						?>
187
						<div class="condition" data-rule-major="<?php echo esc_attr( $rule['major'] ); ?>" data-rule-minor="<?php echo esc_attr( $rule['minor'] ); ?>" data-rule-has-children="<?php echo esc_attr( $rule['has_children'] ); ?>">
188
							<div class="selection alignleft">
189
								<select class="conditions-rule-major" name="conditions[rules_major][]">
190
									<option value="" <?php selected( "", $rule['major'] ); ?>><?php echo esc_html_x( '-- Select --', 'Used as the default option in a dropdown list', 'jetpack' ); ?></option>
191
									<option value="category" <?php selected( "category", $rule['major'] ); ?>><?php esc_html_e( 'Category', 'jetpack' ); ?></option>
192
									<option value="author" <?php selected( "author", $rule['major'] ); ?>><?php echo esc_html_x( 'Author', 'Noun, as in: "The author of this post is..."', 'jetpack' ); ?></option>
193
194
									<?php if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { // this doesn't work on .com because of caching ?>
195
										<option value="loggedin" <?php selected( "loggedin", $rule['major'] ); ?>><?php echo esc_html_x( 'User', 'Noun', 'jetpack' ); ?></option>
196
										<option value="role" <?php selected( "role", $rule['major'] ); ?>><?php echo esc_html_x( 'Role', 'Noun, as in: "The user role of that can access this widget is..."', 'jetpack' ); ?></option>
197
									<?php } ?>
198
199
									<option value="tag" <?php selected( "tag", $rule['major'] ); ?>><?php echo esc_html_x( 'Tag', 'Noun, as in: "This post has one tag."', 'jetpack' ); ?></option>
200
									<option value="date" <?php selected( "date", $rule['major'] ); ?>><?php echo esc_html_x( 'Date', 'Noun, as in: "This page is a date archive."', 'jetpack' ); ?></option>
201
									<option value="page" <?php selected( "page", $rule['major'] ); ?>><?php echo esc_html_x( 'Page', 'Example: The user is looking at a page, not a post.', 'jetpack' ); ?></option>
202
									<option value="post_type" <?php selected( "post_type", $rule['major'] ); ?>><?php echo esc_html_x( 'Post Type', 'Example: the user is viewing a custom post type archive.', 'jetpack' ); ?></option>
203
									<?php if ( get_taxonomies( array( '_builtin' => false ) ) ) : ?>
204
										<option value="taxonomy" <?php selected( "taxonomy", $rule['major'] ); ?>><?php echo esc_html_x( 'Taxonomy', 'Noun, as in: "This post has one taxonomy."', 'jetpack' ); ?></option>
205
									<?php endif; ?>
206
								</select>
207
208
								<?php _ex( 'is', 'Widget Visibility: {Rule Major [Page]} is {Rule Minor [Search results]}', 'jetpack' ); ?>
209
210
								<select class="conditions-rule-minor" name="conditions[rules_minor][]" <?php if ( ! $rule['major'] ) { ?> disabled="disabled"<?php } ?>>
211
									<?php /* Include the currently selected value so that if the widget is saved without
212
									         expanding the Visibility section, we don't lose the minor part of the rule.
213
									         If it is opened, this list is cleared out and populated with all the values. */ ?>
214
									<option value="<?php echo esc_attr( $rule['minor'] ); ?>" selected="selected"></option>
215
								</select>
216
217
								<span class="conditions-rule-has-children" <?php if ( ! $rule['has_children'] ) { ?> style="display: none;"<?php } ?>>
218
									<label>
219
										<input type="checkbox" name="conditions[page_children][<?php echo $rule_index; ?>]" value="has" <?php checked( $rule['has_children'], true ); ?> />
220
										<?php echo esc_html_x( "Include children", 'Checkbox on Widget Visibility if children of the selected page should be included in the visibility rule.', 'jetpack' ); ?>
221
									</label>
222
								</span>
223
							</div>
224
225
							<div class="condition-control">
226
								<span class="condition-conjunction"><?php echo esc_html_x( 'or', 'Shown between widget visibility conditions.', 'jetpack' ); ?></span>
227
								<div class="actions alignright">
228
									<a href="#" class="delete-condition dashicons dashicons-no"><?php esc_html_e( 'Delete', 'jetpack' ); ?></a><a href="#" class="add-condition dashicons dashicons-plus"><?php esc_html_e( 'Add', 'jetpack' ); ?></a>
229
								</div>
230
							</div>
231
232
						</div><!-- .condition -->
233
						<?php
234
					}
235
236
					?>
237
				</div><!-- .conditions -->
238
			</div><!-- .widget-conditional-inner -->
239
		</div><!-- .widget-conditional -->
240
		<?php
241
	}
242
243
	/**
244
	 * On an AJAX update of the widget settings, process the display conditions.
245
	 *
246
	 * @param array $new_instance New settings for this instance as input by the user.
247
	 * @param array $old_instance Old settings for this instance.
248
	 * @return array Modified settings.
249
	 */
250
	public static function widget_update( $instance, $new_instance, $old_instance ) {
251
		if ( empty( $_POST['conditions'] ) ) {
252
			return $instance;
253
		}
254
255
		$conditions = array();
256
		$conditions['action'] = $_POST['conditions']['action'];
257
		$conditions['rules'] = array();
258
259
		foreach ( $_POST['conditions']['rules_major'] as $index => $major_rule ) {
260
			if ( ! $major_rule )
261
				continue;
262
263
			$conditions['rules'][] = array(
264
				'major' => $major_rule,
265
				'minor' => isset( $_POST['conditions']['rules_minor'][$index] ) ? $_POST['conditions']['rules_minor'][$index] : '',
266
				'has_children' => isset( $_POST['conditions']['page_children'][$index] ) ? true : false,
267
			);
268
		}
269
270
		if ( ! empty( $conditions['rules'] ) )
271
			$instance['conditions'] = $conditions;
272
		else
273
			unset( $instance['conditions'] );
274
275
		if (
276
				( isset( $instance['conditions'] ) && ! isset( $old_instance['conditions'] ) )
277
				||
278
				(
279
					isset( $instance['conditions'], $old_instance['conditions'] )
280
					&&
281
					serialize( $instance['conditions'] ) != serialize( $old_instance['conditions'] )
282
				)
283
			) {
284
285
			/**
286
			 * Fires after the widget visibility conditions are saved.
287
			 *
288
			 * @module widget-visibility
289
			 *
290
			 * @since 2.4.0
291
			 */
292
			do_action( 'widget_conditions_save' );
293
		}
294
		else if ( ! isset( $instance['conditions'] ) && isset( $old_instance['conditions'] ) ) {
295
296
			/**
297
			 * Fires after the widget visibility conditions are deleted.
298
			 *
299
			 * @module widget-visibility
300
			 *
301
			 * @since 2.4.0
302
			 */
303
			do_action( 'widget_conditions_delete' );
304
		}
305
306
		return $instance;
307
	}
308
309
	/**
310
	 * Filter the list of widgets for a sidebar so that active sidebars work as expected.
311
	 *
312
	 * @param array $widget_areas An array of widget areas and their widgets.
313
	 * @return array The modified $widget_area array.
314
	 */
315
	public static function sidebars_widgets( $widget_areas ) {
316
		$settings = array();
317
318
		foreach ( $widget_areas as $widget_area => $widgets ) {
319
			if ( empty( $widgets ) )
320
				continue;
321
322
			if ( ! is_array( $widgets ) )
323
				continue;
324
325
			if ( 'wp_inactive_widgets' == $widget_area )
326
				continue;
327
328
			foreach ( $widgets as $position => $widget_id ) {
329
				// Find the conditions for this widget.
330
				if ( preg_match( '/^(.+?)-(\d+)$/', $widget_id, $matches ) ) {
331
					$id_base = $matches[1];
332
					$widget_number = intval( $matches[2] );
333
				}
334
				else {
335
					$id_base = $widget_id;
336
					$widget_number = null;
337
				}
338
339
				if ( ! isset( $settings[$id_base] ) ) {
340
					$settings[$id_base] = get_option( 'widget_' . $id_base );
341
				}
342
343
				// New multi widget (WP_Widget)
344
				if ( ! is_null( $widget_number ) ) {
345
					if ( isset( $settings[$id_base][$widget_number] ) && false === self::filter_widget( $settings[$id_base][$widget_number] ) ) {
346
						unset( $widget_areas[$widget_area][$position] );
347
					}
348
				}
349
350
				// Old single widget
351
				else if ( ! empty( $settings[ $id_base ] ) && false === self::filter_widget( $settings[$id_base] ) ) {
352
					unset( $widget_areas[$widget_area][$position] );
353
				}
354
			}
355
		}
356
357
		return $widget_areas;
358
	}
359
360
	public static function template_redirect() {
361
		self::$passed_template_redirect = true;
362
	}
363
364
	/**
365
	 * Generates a condition key based on the rule array
366
	 *
367
	 * @param array $rule
368
	 * @return string key used to retrieve the condition.
369
	 */
370
	static function generate_condition_key( $rule ) {
371
		if ( isset( $rule['has_children'] ) ) {
372
			return $rule['major'] . ":" . $rule['minor'] . ":" . $rule['has_children'];
373
		}
374
		return $rule['major'] . ":" . $rule['minor'];
375
	}
376
377
	/**
378
	 * Determine whether the widget should be displayed based on conditions set by the user.
379
	 *
380
	 * @param array $instance The widget settings.
381
	 * @return array Settings to display or bool false to hide.
382
	 */
383
	public static function filter_widget( $instance ) {
384
		global $wp_query;
385
386
		if ( empty( $instance['conditions'] ) || empty( $instance['conditions']['rules'] ) )
387
			return $instance;
388
389
		// Store the results of all in-page condition lookups so that multiple widgets with
390
		// the same visibility conditions don't result in duplicate DB queries.
391
		static $condition_result_cache = array();
392
393
		$condition_result = false;
394
395
		foreach ( $instance['conditions']['rules'] as $rule ) {
396
			$condition_key = self::generate_condition_key( $rule );
397
398
			if ( isset( $condition_result_cache[ $condition_key ] ) ) {
399
				$condition_result = $condition_result_cache[ $condition_key ];
400
			}
401
			else {
402
				switch ( $rule['major'] ) {
403
					case 'date':
404
						switch ( $rule['minor'] ) {
405
							case '':
406
								$condition_result = is_date();
407
							break;
408
							case 'month':
409
								$condition_result = is_month();
410
							break;
411
							case 'day':
412
								$condition_result = is_day();
413
							break;
414
							case 'year':
415
								$condition_result = is_year();
416
							break;
417
						}
418
					break;
419
					case 'page':
420
						// Previously hardcoded post type options.
421
						if ( 'post' == $rule['minor'] )
422
							$rule['minor'] = 'post_type-post';
423
						else if ( ! $rule['minor'] )
424
							$rule['minor'] = 'post_type-page';
425
426
						switch ( $rule['minor'] ) {
427
							case '404':
428
								$condition_result = is_404();
429
							break;
430
							case 'search':
431
								$condition_result = is_search();
432
							break;
433
							case 'archive':
434
								$condition_result = is_archive();
435
							break;
436
							case 'posts':
437
								$condition_result = $wp_query->is_posts_page;
438
							break;
439
							case 'home':
440
								$condition_result = is_home();
441
							break;
442
							case 'front':
443
								if ( current_theme_supports( 'infinite-scroll' ) )
444
									$condition_result = is_front_page();
445
								else {
446
									$condition_result = is_front_page() && !is_paged();
447
								}
448
							break;
449
							default:
450
								if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) {
451
									$condition_result = is_singular( substr( $rule['minor'], 10 ) );
452
								} elseif ( $rule['minor'] == get_option( 'page_for_posts' ) ) {
453
									// If $rule['minor'] is a page ID which is also the posts page
454
									$condition_result = $wp_query->is_posts_page;
455
								} else {
456
									// $rule['minor'] is a page ID
457
									$condition_result = is_page() && ( $rule['minor'] == get_the_ID() );
458
459
									// Check if $rule['minor'] is parent of page ID
460
									if ( ! $condition_result && isset( $rule['has_children'] ) && $rule['has_children'] )
461
										$condition_result = wp_get_post_parent_id( get_the_ID() ) == $rule['minor'];
462
								}
463
							break;
464
						}
465
					break;
466 View Code Duplication
					case 'tag':
467
						// All tag pages.
468
						if( ! $rule['minor'] ) {
469
							if ( is_tag() ) {
470
								$condition_result = true;
471
							} else if ( is_singular() ) {
472
								if( in_array( 'post_tag', get_post_taxonomies() ) ) {
473
									$condition_result = true;
474
								}
475
							}
476
							break;
477
						}
478
479
						// All pages with the specified tag term.
480
						if ( is_tag( $rule['minor'] ) ) {
481
							$condition_result = true;
482
						}
483
						else if ( is_singular() && has_term( $rule['minor'], 'post_tag' ) ) {
484
							$condition_result = true;
485
						}
486
					break;
487 View Code Duplication
					case 'category':
488
						// All category pages.
489
						if( ! $rule['minor'] ) {
490
							if ( is_category() ) {
491
								$condition_result = true;
492
							}
493
							else if ( is_singular() ) {
494
								if( in_array( 'category', get_post_taxonomies() ) ) {
495
									$condition_result = true;
496
								}
497
							}
498
							break;
499
						}
500
501
						// All pages with the specified category term.
502
						if ( is_category( $rule['minor'] ) ) {
503
							$condition_result = true;
504
						}
505
						else if ( is_singular() && has_term( $rule['minor'], 'category' ) ) {
506
							$condition_result = true;
507
						}
508
					break;
509
					case 'loggedin':
510
						$condition_result = is_user_logged_in();
511
						if ( 'loggedin' !== $rule['minor'] ) {
512
							$condition_result = ! $condition_result;
513
						}
514
					break;
515
					case 'author':
516
						$post = get_post();
517
						if ( ! $rule['minor'] && is_author() )
518
							$condition_result = true;
519
						else if ( $rule['minor'] && is_author( $rule['minor'] ) )
520
							$condition_result = true;
521
						else if ( is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author )
522
							$condition_result = true;
523
					break;
524
					case 'role':
525
						if( is_user_logged_in() ) {
526
							$current_user = wp_get_current_user();
527
528
							$user_roles = $current_user->roles;
529
530
							if( in_array( $rule['minor'], $user_roles ) ) {
531
								$condition_result = true;
532
							} else {
533
								$condition_result = false;
534
							}
535
536
						} else {
537
							$condition_result = false;
538
						}
539
					break;
540
					case 'post_type':
541
						if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) {
542
							$condition_result = is_singular( substr( $rule['minor'], 10 ) );
543
						} elseif ( substr( $rule['minor'], 0, 18 ) == 'post_type_archive-' ) {
544
							$condition_result = is_post_type_archive( substr( $rule['minor'], 18 ) );
545
						}
546
					break;
547
					case 'taxonomy':
548
						// All taxonomy pages.
549
						if( ! $rule['minor'] ) {
550
							if ( is_archive() ) {
551
								if ( is_tag() || is_category() || is_tax() ) {
552
									$condition_result = true;
553
								}
554
							}
555
							else if ( is_singular() ) {
556
								$post_taxonomies = get_post_taxonomies();
557
								$condition_result = ! empty( $post_taxonomies );
558
							}
559
							break;
560
						}
561
562
						// Specified taxonomy page.
563
						$term = explode( '_tax_', $rule['minor'] ); // $term[0] = taxonomy name; $term[1] = term id
564
						if ( isset( $term[0] ) && isset( $term[1] ) ) {
565
							$term[1] = self::maybe_get_split_term( $term[1], $term[0] );
566
						}
567
568
						// All pages of the specified taxonomy.
569
						if ( ! isset( $term[1] ) || ! $term[1] ) {
570
							if ( is_tax( $term[0] ) ) {
571
								$condition_result = true;
572
							}
573
							else if ( is_singular() ) {
574
								if( in_array( $term[0], get_post_taxonomies() ) ) {
575
									$condition_result = true;
576
								}
577
							}
578
							break;
579
						}
580
581
						// All pages with the specified taxonomy term.
582
						if ( is_tax( $term[0], $term[1] ) ) {
583
							$condition_result = true;
584
						}
585
						else if ( is_singular() && has_term( $term[1], $term[0] ) ) {
586
							$condition_result = true;
587
						}
588
					break;
589
				}
590
591
				if ( $condition_result || self::$passed_template_redirect ) {
592
					// Some of the conditions will return false when checked before the template_redirect
593
					// action has been called, like is_page(). Only store positive lookup results, which
594
					// won't be false positives, before template_redirect, and everything after.
595
					$condition_result_cache[ $condition_key ] = $condition_result;
596
				}
597
			}
598
599
			if ( $condition_result )
600
				break;
601
		}
602
603
		if ( ( 'show' == $instance['conditions']['action'] && ! $condition_result ) || ( 'hide' == $instance['conditions']['action'] && $condition_result ) )
604
			return false;
605
606
		return $instance;
607
	}
608
609
	public static function strcasecmp_name( $a, $b ) {
610
		return strcasecmp( $a->name, $b->name );
611
	}
612
613
	public static function maybe_get_split_term( $old_term_id = '', $taxonomy = '' ) {
614
		$term_id = $old_term_id;
615
616
		if ( 'tag' == $taxonomy ) {
617
			$taxonomy = 'post_tag';
618
		}
619
620
		if ( function_exists( 'wp_get_split_term' ) && $new_term_id = wp_get_split_term( $old_term_id, $taxonomy ) ) {
621
			$term_id = $new_term_id;
622
		}
623
624
		return $term_id;
625
	}
626
}
627
628
add_action( 'init', array( 'Jetpack_Widget_Conditions', 'init' ) );
629