/modules/widget-visibility/widget-conditions.php

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