Jetpack_Widget_Conditions

Hide or show widgets conditionally.

Defined (1)

The class is defined in the following location(s).

/modules/widget-visibility/widget-conditions.php  
  1. class Jetpack_Widget_Conditions { 
  2. static $passed_template_redirect = false; 
  3.  
  4. public static function init() { 
  5. if ( is_admin() ) { 
  6. add_action( 'sidebar_admin_setup', array( __CLASS__, 'widget_admin_setup' ) ); 
  7. add_filter( 'widget_update_callback', array( __CLASS__, 'widget_update' ), 10, 3 ); 
  8. add_action( 'in_widget_form', array( __CLASS__, 'widget_conditions_admin' ), 10, 3 ); 
  9. add_action( 'wp_ajax_widget_conditions_options', array( __CLASS__, 'widget_conditions_options' ) ); 
  10. add_action( 'wp_ajax_widget_conditions_has_children', array( __CLASS__, 'widget_conditions_has_children' ) ); 
  11. } else if ( ! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) { 
  12. add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) ); 
  13. add_filter( 'sidebars_widgets', array( __CLASS__, 'sidebars_widgets' ) ); 
  14. add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) ); 
  15.  
  16. public static function widget_admin_setup() { 
  17. if( is_rtl() ) { 
  18. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/rtl/widget-conditions-rtl.css', __FILE__ ) ); 
  19. } else { 
  20. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); 
  21. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); 
  22. wp_enqueue_script( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.js', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), 20140721, true ); 
  23.  
  24. /** 
  25. * Provided a second level of granularity for widget conditions. 
  26. */ 
  27. public static function widget_conditions_options_echo( $major = '', $minor = '' ) { 
  28. if ( in_array( $major, array( 'category', 'tag' ) ) && is_numeric( $minor ) ) { 
  29. $minor = self::maybe_get_split_term( $minor, $major ); 
  30.  
  31. switch ( $major ) { 
  32. case 'category': 
  33. ?> 
  34. <option value=""><?php _e( 'All category pages', 'jetpack' ); ?></option> 
  35. <?php 
  36.  
  37. $categories = get_categories( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); 
  38. usort( $categories, array( __CLASS__, 'strcasecmp_name' ) ); 
  39.  
  40. foreach ( $categories as $category ) { 
  41. ?> 
  42. <option value="<?php echo esc_attr( $category->term_id ); ?>" <?php selected( $category->term_id, $minor ); ?>><?php echo esc_html( $category->name ); ?></option> 
  43. <?php 
  44. break; 
  45. case 'loggedin': 
  46. ?> 
  47. <option value="loggedin" <?php selected( 'loggedin', $minor ); ?>><?php _e( 'Logged In', 'jetpack' ); ?></option> 
  48. <option value="loggedout" <?php selected( 'loggedout', $minor ); ?>><?php _e( 'Logged Out', 'jetpack' ); ?></option> 
  49. <?php 
  50. break; 
  51. case 'author': 
  52. ?> 
  53. <option value=""><?php _e( 'All author pages', 'jetpack' ); ?></option> 
  54. <?php 
  55.  
  56. foreach ( get_users( array( 'orderby' => 'name', 'exclude_admin' => true ) ) as $author ) { 
  57. ?> 
  58. <option value="<?php echo esc_attr( $author->ID ); ?>" <?php selected( $author->ID, $minor ); ?>><?php echo esc_html( $author->display_name ); ?></option> 
  59. <?php 
  60. break; 
  61. case 'role': 
  62. global $wp_roles; 
  63.  
  64. foreach ( $wp_roles->roles as $role_key => $role ) { 
  65. ?> 
  66. <option value="<?php echo esc_attr( $role_key ); ?>" <?php selected( $role_key, $minor ); ?> ><?php echo esc_html( $role['name'] ); ?></option> 
  67. <?php 
  68. break; 
  69. case 'tag': 
  70. ?> 
  71. <option value=""><?php _e( 'All tag pages', 'jetpack' ); ?></option> 
  72. <?php 
  73.  
  74. $tags = get_tags( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); 
  75. usort( $tags, array( __CLASS__, 'strcasecmp_name' ) ); 
  76.  
  77. foreach ( $tags as $tag ) { 
  78. ?> 
  79. <option value="<?php echo esc_attr($tag->term_id ); ?>" <?php selected( $tag->term_id, $minor ); ?>><?php echo esc_html( $tag->name ); ?></option> 
  80. <?php 
  81. break; 
  82. case 'date': 
  83. ?> 
  84. <option value="" <?php selected( '', $minor ); ?>><?php _e( 'All date archives', 'jetpack' ); ?></option> 
  85. <option value="day"<?php selected( 'day', $minor ); ?>><?php _e( 'Daily archives', 'jetpack' ); ?></option> 
  86. <option value="month"<?php selected( 'month', $minor ); ?>><?php _e( 'Monthly archives', 'jetpack' ); ?></option> 
  87. <option value="year"<?php selected( 'year', $minor ); ?>><?php _e( 'Yearly archives', 'jetpack' ); ?></option> 
  88. <?php 
  89. break; 
  90. case 'page': 
  91. // Previously hardcoded post type options. 
  92. if ( ! $minor ) 
  93. $minor = 'post_type-page'; 
  94. else if ( 'post' == $minor ) 
  95. $minor = 'post_type-post'; 
  96.  
  97. ?> 
  98. <option value="front" <?php selected( 'front', $minor ); ?>><?php _e( 'Front page', 'jetpack' ); ?></option> 
  99. <option value="posts" <?php selected( 'posts', $minor ); ?>><?php _e( 'Posts page', 'jetpack' ); ?></option> 
  100. <option value="archive" <?php selected( 'archive', $minor ); ?>><?php _e( 'Archive page', 'jetpack' ); ?></option> 
  101. <option value="404" <?php selected( '404', $minor ); ?>><?php _e( '404 error page', 'jetpack' ); ?></option> 
  102. <option value="search" <?php selected( 'search', $minor ); ?>><?php _e( 'Search results', 'jetpack' ); ?></option> 
  103. <optgroup label="<?php esc_attr_e( 'Post type:', 'jetpack' ); ?>"> 
  104. <?php 
  105.  
  106. $post_types = get_post_types( array( 'public' => true ), 'objects' ); 
  107.  
  108. foreach ( $post_types as $post_type ) { 
  109. ?> 
  110. <option value="<?php echo esc_attr( 'post_type-' . $post_type->name ); ?>" <?php selected( 'post_type-' . $post_type->name, $minor ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option> 
  111. <?php 
  112.  
  113. ?> 
  114. </optgroup> 
  115. <optgroup label="<?php esc_attr_e( 'Static page:', 'jetpack' ); ?>"> 
  116. <?php 
  117.  
  118. echo str_replace( ' value="' . esc_attr( $minor ) . '"', ' value="' . esc_attr( $minor ) . '" selected="selected"', preg_replace( '/<\/?select[^>]*?>/i', '', wp_dropdown_pages( array( 'echo' => false ) ) ) ); 
  119.  
  120. ?> 
  121. </optgroup> 
  122. <?php 
  123. break; 
  124. case 'taxonomy': 
  125. ?> 
  126. <option value=""><?php _e( 'All taxonomy pages', 'jetpack' ); ?></option> 
  127. <?php 
  128.  
  129. $taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' ); 
  130. usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) ); 
  131.  
  132. $parts = explode( '_tax_', $minor ); 
  133.  
  134. if ( 2 === count( $parts ) ) { 
  135. $minor_id = self::maybe_get_split_term( $parts[1], $parts[0] ); 
  136. $minor = $parts[0] . '_tax_' . $minor_id; 
  137.  
  138. foreach ( $taxonomies as $taxonomy ) { 
  139. ?> 
  140. <optgroup label="<?php esc_attr_e( $taxonomy->labels->name . ':', 'jetpack' ); ?>"> 
  141. <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $taxonomy->name, $minor ); ?>><?php echo 'All ' . esc_html( $taxonomy->name ) . ' pages'; ?></option> 
  142. <?php 
  143.  
  144. $terms = get_terms( array( $taxonomy->name ), array( 'number' => 250, 'hide_empty' => false ) ); 
  145. foreach ( $terms as $term ) { 
  146. ?> 
  147. <option value="<?php echo esc_attr( $taxonomy->name . '_tax_' . $term->term_id ); ?>" <?php selected( $taxonomy->name . '_tax_' . $term->term_id, $minor ); ?>><?php echo esc_html( $term->name ); ?></option> 
  148. <?php 
  149.  
  150. ?> 
  151. </optgroup> 
  152. <?php 
  153. break; 
  154.  
  155. /** 
  156. * This is the AJAX endpoint for the second level of conditions. 
  157. */ 
  158. public static function widget_conditions_options() { 
  159. self::widget_conditions_options_echo( $_REQUEST['major'], isset( $_REQUEST['minor'] ) ? $_REQUEST['minor'] : '' ); 
  160. die; 
  161.  
  162. /** 
  163. * Provide an option to include children of pages. 
  164. */ 
  165. public static function widget_conditions_has_children_echo( $major = '', $minor = '', $has_children = false ) { 
  166. if ( ! $major || 'page' !== $major || ! $minor ) { 
  167. return null; 
  168.  
  169. if ( 'front' == $minor ) { 
  170. $minor = get_option( 'page_on_front' ); 
  171.  
  172. if ( ! is_numeric( $minor ) ) { 
  173. return null; 
  174.  
  175. $page_children = get_pages( array( 'child_of' => (int) $minor ) ); 
  176.  
  177. if ( $page_children ) { 
  178. ?> 
  179. <label> 
  180. <input type="checkbox" id="include_children" name="conditions[page_children][]" value="has" <?php checked( $has_children, true ); ?> /> 
  181. <?php echo esc_html_x( "Include children", 'Checkbox on Widget Visibility if choosen page has children.', 'jetpack' ); ?> 
  182. </label> 
  183. <?php 
  184.  
  185. /** 
  186. * This is the AJAX endpoint for the has_children input. 
  187. */ 
  188. public static function widget_conditions_has_children() { 
  189. self::widget_conditions_has_children_echo( $_REQUEST['major'], isset( $_REQUEST['minor'] ) ? $_REQUEST['minor'] : '', isset( $_REQUEST['has_children'] ) ? $_REQUEST['has_children'] : false ); 
  190. die; 
  191.  
  192. /** 
  193. * Add the widget conditions to each widget in the admin. 
  194. * @param $widget unused. 
  195. * @param $return unused. 
  196. * @param array $instance The widget settings. 
  197. */ 
  198. public static function widget_conditions_admin( $widget, $return, $instance ) { 
  199. $conditions = array(); 
  200.  
  201. if ( isset( $instance['conditions'] ) ) 
  202. $conditions = $instance['conditions']; 
  203.  
  204. if ( ! isset( $conditions['action'] ) ) 
  205. $conditions['action'] = 'show'; 
  206.  
  207. if ( empty( $conditions['rules'] ) ) 
  208. $conditions['rules'][] = array( 'major' => '', 'minor' => '', 'has_children' => '' ); 
  209.  
  210. ?> 
  211. <div class="widget-conditional <?php if ( empty( $_POST['widget-conditions-visible'] ) || $_POST['widget-conditions-visible'] == '0' ) { ?>widget-conditional-hide<?php } ?>"> 
  212. <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 } ?>" /> 
  213. <?php if ( ! isset( $_POST['widget-conditions-visible'] ) ) { ?><a href="#" class="button display-options"><?php _e( 'Visibility', 'jetpack' ); ?></a><?php } ?> 
  214. <div class="widget-conditional-inner"> 
  215. <div class="condition-top"> 
  216. <?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>' ); ?> 
  217. </div><!-- .condition-top --> 
  218.  
  219. <div class="conditions"> 
  220. <?php 
  221.  
  222. foreach ( $conditions['rules'] as $rule ) { 
  223. ?> 
  224. <div class="condition"> 
  225. <div class="selection alignleft"> 
  226. <select class="conditions-rule-major" name="conditions[rules_major][]"> 
  227. <option value="" <?php selected( "", $rule['major'] ); ?>><?php echo esc_html_x( '-- Select --', 'Used as the default option in a dropdown list', 'jetpack' ); ?></option> 
  228. <option value="category" <?php selected( "category", $rule['major'] ); ?>><?php esc_html_e( 'Category', 'jetpack' ); ?></option> 
  229. <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> 
  230.  
  231. <?php if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { // this doesn't work on .com because of caching ?> 
  232. <option value="loggedin" <?php selected( "loggedin", $rule['major'] ); ?>><?php echo esc_html_x( 'User', 'Noun', 'jetpack' ); ?></option> 
  233. <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> 
  234. <?php } ?> 
  235.  
  236. <option value="tag" <?php selected( "tag", $rule['major'] ); ?>><?php echo esc_html_x( 'Tag', 'Noun, as in: "This post has one tag."', 'jetpack' ); ?></option> 
  237. <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> 
  238. <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> 
  239.  
  240. <?php if ( get_taxonomies( array( '_builtin' => false ) ) ) : ?> 
  241. <option value="taxonomy" <?php selected( "taxonomy", $rule['major'] ); ?>><?php echo esc_html_x( 'Taxonomy', 'Noun, as in: "This post has one taxonomy."', 'jetpack' ); ?></option> 
  242. <?php endif; ?> 
  243. </select> 
  244.  
  245. <?php _ex( 'is', 'Widget Visibility: {Rule Major [Page]} is {Rule Minor [Search results]}', 'jetpack' ); ?> 
  246.  
  247. <select class="conditions-rule-minor" name="conditions[rules_minor][]" <?php if ( ! $rule['major'] ) { ?> disabled="disabled"<?php } ?> data-loading-text="<?php esc_attr_e( 'Loading...', 'jetpack' ); ?>"> 
  248. <?php self::widget_conditions_options_echo( $rule['major'], $rule['minor'] ); ?> 
  249. </select> 
  250.  
  251. <span class="conditions-rule-has-children"> 
  252. <?php self::widget_conditions_has_children_echo( $rule['major'], $rule['minor'], $rule['has_children'] ); ?> 
  253. </span> 
  254. </div> 
  255.  
  256. <div class="condition-control"> 
  257. <span class="condition-conjunction"><?php echo esc_html_x( 'or', 'Shown between widget visibility conditions.', 'jetpack' ); ?></span> 
  258. <div class="actions alignright"> 
  259. <a href="#" class="delete-condition"><?php esc_html_e( 'Delete', 'jetpack' ); ?></a> | <a href="#" class="add-condition"><?php esc_html_e( 'Add', 'jetpack' ); ?></a> 
  260. </div> 
  261. </div> 
  262.  
  263. </div><!-- .condition --> 
  264. <?php 
  265.  
  266. ?> 
  267. </div><!-- .conditions --> 
  268. </div><!-- .widget-conditional-inner --> 
  269. </div><!-- .widget-conditional --> 
  270. <?php 
  271.  
  272. /** 
  273. * On an AJAX update of the widget settings, process the display conditions. 
  274. * @param array $new_instance New settings for this instance as input by the user. 
  275. * @param array $old_instance Old settings for this instance. 
  276. * @return array Modified settings. 
  277. */ 
  278. public static function widget_update( $instance, $new_instance, $old_instance ) { 
  279. $conditions = array(); 
  280. $conditions['action'] = $_POST['conditions']['action']; 
  281. $conditions['rules'] = array(); 
  282.  
  283. foreach ( $_POST['conditions']['rules_major'] as $index => $major_rule ) { 
  284. if ( ! $major_rule ) 
  285. continue; 
  286.  
  287. $conditions['rules'][] = array( 
  288. 'major' => $major_rule,  
  289. 'minor' => isset( $_POST['conditions']['rules_minor'][$index] ) ? $_POST['conditions']['rules_minor'][$index] : '',  
  290. 'has_children' => isset( $_POST['conditions']['page_children'][$index] ) ? true : false,  
  291. ); 
  292.  
  293. if ( ! empty( $conditions['rules'] ) ) 
  294. $instance['conditions'] = $conditions; 
  295. else 
  296. unset( $instance['conditions'] ); 
  297.  
  298. if ( 
  299. ( isset( $instance['conditions'] ) && ! isset( $old_instance['conditions'] ) ) 
  300. || 
  301. isset( $instance['conditions'], $old_instance['conditions'] ) 
  302. && 
  303. serialize( $instance['conditions'] ) != serialize( $old_instance['conditions'] ) 
  304. ) { 
  305.  
  306. /** 
  307. * Fires after the widget visibility conditions are saved. 
  308. * @module widget-visibility 
  309. * @since 2.4.0 
  310. */ 
  311. do_action( 'widget_conditions_save' ); 
  312. else if ( ! isset( $instance['conditions'] ) && isset( $old_instance['conditions'] ) ) { 
  313.  
  314. /** 
  315. * Fires after the widget visibility conditions are deleted. 
  316. * @module widget-visibility 
  317. * @since 2.4.0 
  318. */ 
  319. do_action( 'widget_conditions_delete' ); 
  320.  
  321. return $instance; 
  322.  
  323. /** 
  324. * Filter the list of widgets for a sidebar so that active sidebars work as expected. 
  325. * @param array $widget_areas An array of widget areas and their widgets. 
  326. * @return array The modified $widget_area array. 
  327. */ 
  328. public static function sidebars_widgets( $widget_areas ) { 
  329. $settings = array(); 
  330.  
  331. foreach ( $widget_areas as $widget_area => $widgets ) { 
  332. if ( empty( $widgets ) ) 
  333. continue; 
  334.  
  335. if ( 'wp_inactive_widgets' == $widget_area ) 
  336. continue; 
  337.  
  338. foreach ( $widgets as $position => $widget_id ) { 
  339. // Find the conditions for this widget. 
  340. if ( preg_match( '/^(.+?)-(\d+)$/', $widget_id, $matches ) ) { 
  341. $id_base = $matches[1]; 
  342. $widget_number = intval( $matches[2] ); 
  343. else { 
  344. $id_base = $widget_id; 
  345. $widget_number = null; 
  346.  
  347. if ( ! isset( $settings[$id_base] ) ) { 
  348. $settings[$id_base] = get_option( 'widget_' . $id_base ); 
  349.  
  350. // New multi widget (WP_Widget) 
  351. if ( ! is_null( $widget_number ) ) { 
  352. if ( isset( $settings[$id_base][$widget_number] ) && false === self::filter_widget( $settings[$id_base][$widget_number] ) ) { 
  353. unset( $widget_areas[$widget_area][$position] ); 
  354.  
  355. // Old single widget 
  356. else if ( ! empty( $settings[ $id_base ] ) && false === self::filter_widget( $settings[$id_base] ) ) { 
  357. unset( $widget_areas[$widget_area][$position] ); 
  358.  
  359. return $widget_areas; 
  360.  
  361. public static function template_redirect() { 
  362. self::$passed_template_redirect = true; 
  363.  
  364. /** 
  365. * Generates a condition key based on the rule array 
  366. * @param array $rule 
  367. * @return string key used to retrieve the condition. 
  368. */ 
  369. static function generate_condition_key( $rule ) { 
  370. if ( isset( $rule['has_children'] ) ) { 
  371. return $rule['major'] . ":" . $rule['minor'] . ":" . $rule['has_children']; 
  372. return $rule['major'] . ":" . $rule['minor']; 
  373.  
  374. /** 
  375. * Determine whether the widget should be displayed based on conditions set by the user. 
  376. * @param array $instance The widget settings. 
  377. * @return array Settings to display or bool false to hide. 
  378. */ 
  379. public static function filter_widget( $instance ) { 
  380. global $wp_query; 
  381.  
  382. if ( empty( $instance['conditions'] ) || empty( $instance['conditions']['rules'] ) ) 
  383. return $instance; 
  384.  
  385. // Store the results of all in-page condition lookups so that multiple widgets with 
  386. // the same visibility conditions don't result in duplicate DB queries. 
  387. static $condition_result_cache = array(); 
  388.  
  389. $condition_result = false; 
  390.  
  391. foreach ( $instance['conditions']['rules'] as $rule ) { 
  392. $condition_key = self::generate_condition_key( $rule ); 
  393.  
  394. if ( isset( $condition_result_cache[ $condition_key ] ) ) { 
  395. $condition_result = $condition_result_cache[ $condition_key ]; 
  396. else { 
  397. switch ( $rule['major'] ) { 
  398. case 'date': 
  399. switch ( $rule['minor'] ) { 
  400. case '': 
  401. $condition_result = is_date(); 
  402. break; 
  403. case 'month': 
  404. $condition_result = is_month(); 
  405. break; 
  406. case 'day': 
  407. $condition_result = is_day(); 
  408. break; 
  409. case 'year': 
  410. $condition_result = is_year(); 
  411. break; 
  412. break; 
  413. case 'page': 
  414. // Previously hardcoded post type options. 
  415. if ( 'post' == $rule['minor'] ) 
  416. $rule['minor'] = 'post_type-post'; 
  417. else if ( ! $rule['minor'] ) 
  418. $rule['minor'] = 'post_type-page'; 
  419.  
  420. switch ( $rule['minor'] ) { 
  421. case '404': 
  422. $condition_result = is_404(); 
  423. break; 
  424. case 'search': 
  425. $condition_result = is_search(); 
  426. break; 
  427. case 'archive': 
  428. $condition_result = is_archive(); 
  429. break; 
  430. case 'posts': 
  431. $condition_result = $wp_query->is_posts_page; 
  432. break; 
  433. case 'home': 
  434. $condition_result = is_home(); 
  435. break; 
  436. case 'front': 
  437. if ( current_theme_supports( 'infinite-scroll' ) ) 
  438. $condition_result = is_front_page(); 
  439. else { 
  440. $condition_result = is_front_page() && !is_paged(); 
  441. break; 
  442. default: 
  443. if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) { 
  444. $condition_result = is_singular( substr( $rule['minor'], 10 ) ); 
  445. } elseif ( $rule['minor'] == get_option( 'page_for_posts' ) ) { 
  446. // If $rule['minor'] is a page ID which is also the posts page 
  447. $condition_result = $wp_query->is_posts_page; 
  448. } else { 
  449. // $rule['minor'] is a page ID 
  450. $condition_result = is_page( $rule['minor'] ); 
  451. // Check if $rule['minor'] is parent of page ID 
  452. if ( ! $condition_result && isset( $rule['has_children'] ) && $rule['has_children'] ) 
  453. $condition_result = wp_get_post_parent_id( get_the_ID() ) == $rule['minor']; 
  454. break; 
  455. break; 
  456. case 'tag': 
  457. if ( ! $rule['minor'] && is_tag() ) { 
  458. $condition_result = true; 
  459. } else { 
  460. $rule['minor'] = self::maybe_get_split_term( $rule['minor'], $rule['major'] ); 
  461. if ( is_singular() && $rule['minor'] && has_tag( $rule['minor'] ) ) { 
  462. $condition_result = true; 
  463. } else { 
  464. $tag = get_tag( $rule['minor'] ); 
  465. if ( $tag && is_tag( $tag->slug ) ) { 
  466. $condition_result = true; 
  467. break; 
  468. case 'category': 
  469. if ( ! $rule['minor'] && is_category() ) { 
  470. $condition_result = true; 
  471. } else { 
  472. $rule['minor'] = self::maybe_get_split_term( $rule['minor'], $rule['major'] ); 
  473. if ( is_category( $rule['minor'] ) ) { 
  474. $condition_result = true; 
  475. } else if ( is_singular() && $rule['minor'] && in_array( 'category', get_post_taxonomies() ) && has_category( $rule['minor'] ) ) 
  476. $condition_result = true; 
  477. break; 
  478. case 'loggedin': 
  479. $condition_result = is_user_logged_in(); 
  480. if ( 'loggedin' !== $rule['minor'] ) { 
  481. $condition_result = ! $condition_result; 
  482. break; 
  483. case 'author': 
  484. $post = get_post(); 
  485. if ( ! $rule['minor'] && is_author() ) 
  486. $condition_result = true; 
  487. else if ( $rule['minor'] && is_author( $rule['minor'] ) ) 
  488. $condition_result = true; 
  489. else if ( is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author ) 
  490. $condition_result = true; 
  491. break; 
  492. case 'role': 
  493. if( is_user_logged_in() ) { 
  494. global $current_user; 
  495. get_currentuserinfo(); 
  496.  
  497. $user_roles = $current_user->roles; 
  498.  
  499. if( in_array( $rule['minor'], $user_roles ) ) { 
  500. $condition_result = true; 
  501. } else { 
  502. $condition_result = false; 
  503.  
  504. } else { 
  505. $condition_result = false; 
  506. break; 
  507. case 'taxonomy': 
  508. $term = explode( '_tax_', $rule['minor'] ); // $term[0] = taxonomy name; $term[1] = term id 
  509. if ( isset( $term[0] ) && isset( $term[1] ) ) { 
  510. $term[1] = self::maybe_get_split_term( $term[1], $term[0] ); 
  511. if ( isset( $term[1] ) && is_tax( $term[0], $term[1] ) ) 
  512. $condition_result = true; 
  513. else if ( isset( $term[1] ) && is_singular() && $term[1] && has_term( $term[1], $term[0] ) ) 
  514. $condition_result = true; 
  515. else if ( is_singular() && $post_id = get_the_ID() ) { 
  516. $terms = get_the_terms( $post_id, $rule['minor'] ); // Does post have terms in taxonomy? 
  517. if( $terms && ! is_wp_error( $terms ) ) { 
  518. $condition_result = true; 
  519. break; 
  520.  
  521. if ( $condition_result || self::$passed_template_redirect ) { 
  522. // Some of the conditions will return false when checked before the template_redirect 
  523. // action has been called, like is_page(). Only store positive lookup results, which 
  524. // won't be false positives, before template_redirect, and everything after. 
  525. $condition_result_cache[ $condition_key ] = $condition_result; 
  526.  
  527. if ( $condition_result ) 
  528. break; 
  529.  
  530. if ( ( 'show' == $instance['conditions']['action'] && ! $condition_result ) || ( 'hide' == $instance['conditions']['action'] && $condition_result ) ) 
  531. return false; 
  532.  
  533. return $instance; 
  534.  
  535. public static function strcasecmp_name( $a, $b ) { 
  536. return strcasecmp( $a->name, $b->name ); 
  537.  
  538. public static function maybe_get_split_term( $old_term_id = '', $taxonomy = '' ) { 
  539. $term_id = $old_term_id; 
  540.  
  541. if ( 'tag' == $taxonomy ) { 
  542. $taxonomy = 'post_tag'; 
  543.  
  544. if ( function_exists( 'wp_get_split_term' ) && $new_term_id = wp_get_split_term( $old_term_id, $taxonomy ) ) { 
  545. $term_id = $new_term_id; 
  546.  
  547. return $term_id;