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. } else if ( ! in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ) { 
  10. add_filter( 'widget_display_callback', array( __CLASS__, 'filter_widget' ) ); 
  11. add_filter( 'sidebars_widgets', array( __CLASS__, 'sidebars_widgets' ) ); 
  12. add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) ); 
  13.  
  14. public static function widget_admin_setup() { 
  15. if( is_rtl() ) { 
  16. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/rtl/widget-conditions-rtl.css', __FILE__ ) ); 
  17. } else { 
  18. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); 
  19. wp_enqueue_style( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.css', __FILE__ ) ); 
  20. wp_enqueue_script( 'widget-conditions', plugins_url( 'widget-conditions/widget-conditions.js', __FILE__ ), array( 'jquery', 'jquery-ui-core' ), 20140721, true ); 
  21.  
  22. // Set up a single copy of all of the data that Widget Visibility needs. 
  23. // This allows all widget conditions to reuse the same data, keeping page size down 
  24. // and eliminating the AJAX calls we used to have to use to fetch the minor rule options. 
  25. $widget_conditions_data = array(); 
  26.  
  27. $widget_conditions_data['category'] = array(); 
  28. $widget_conditions_data['category'][] = array( '', __( 'All category pages', 'jetpack' ) ); 
  29.  
  30. $categories = get_categories( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); 
  31. usort( $categories, array( __CLASS__, 'strcasecmp_name' ) ); 
  32.  
  33. foreach ( $categories as $category ) { 
  34. $widget_conditions_data['category'][] = array( (string) $category->term_id, $category->name ); 
  35.  
  36. $widget_conditions_data['loggedin'] = array(); 
  37. $widget_conditions_data['loggedin'][] = array( 'loggedin', __( 'Logged In', 'jetpack' ) ); 
  38. $widget_conditions_data['loggedin'][] = array( 'loggedout', __( 'Logged Out', 'jetpack' ) ); 
  39.  
  40. $widget_conditions_data['author'] = array(); 
  41. $widget_conditions_data['author'][] = array( '', __( 'All author pages', 'jetpack' ) ); 
  42.  
  43. // Only users with publish caps 
  44. $authors = get_users( 
  45. array( 
  46. 'orderby' => 'name',  
  47. 'who' => 'authors',  
  48. ); 
  49.  
  50. foreach ( $authors as $author ) { 
  51. $widget_conditions_data['author'][] = array( (string) $author->ID, $author->display_name ); 
  52.  
  53. $widget_conditions_data['role'] = array(); 
  54.  
  55. global $wp_roles; 
  56.  
  57. foreach ( $wp_roles->roles as $role_key => $role ) { 
  58. $widget_conditions_data['role'][] = array( (string) $role_key, $role['name'] ); 
  59.  
  60. $widget_conditions_data['tag'] = array(); 
  61. $widget_conditions_data['tag'][] = array( '', __( 'All tag pages', 'jetpack' ) ); 
  62.  
  63. $tags = get_tags( array( 'number' => 1000, 'orderby' => 'count', 'order' => 'DESC' ) ); 
  64. usort( $tags, array( __CLASS__, 'strcasecmp_name' ) ); 
  65.  
  66. foreach ( $tags as $tag ) { 
  67. $widget_conditions_data['tag'][] = array( (string) $tag->term_id, $tag->name ); 
  68.  
  69. $widget_conditions_data['date'] = array(); 
  70. $widget_conditions_data['date'][] = array( '', __( 'All date archives', 'jetpack' ) ); 
  71. $widget_conditions_data['date'][] = array( 'day', __( 'Daily archives', 'jetpack' ) ); 
  72. $widget_conditions_data['date'][] = array( 'month', __( 'Monthly archives', 'jetpack' ) ); 
  73. $widget_conditions_data['date'][] = array( 'year', __( 'Yearly archives', 'jetpack' ) ); 
  74.  
  75. $widget_conditions_data['page'] = array(); 
  76. $widget_conditions_data['page'][] = array( 'front', __( 'Front page', 'jetpack' ) ); 
  77. $widget_conditions_data['page'][] = array( 'posts', __( 'Posts page', 'jetpack' ) ); 
  78. $widget_conditions_data['page'][] = array( 'archive', __( 'Archive page', 'jetpack' ) ); 
  79. $widget_conditions_data['page'][] = array( '404', __( '404 error page', 'jetpack' ) ); 
  80. $widget_conditions_data['page'][] = array( 'search', __( 'Search results', 'jetpack' ) ); 
  81.  
  82. $post_types = get_post_types( array( 'public' => true ), 'objects' ); 
  83.  
  84. $widget_conditions_post_types = array(); 
  85. $widget_conditions_post_type_archives = array(); 
  86.  
  87. foreach ( $post_types as $post_type ) { 
  88. $widget_conditions_post_types[] = array( 'post_type-' . $post_type->name, $post_type->labels->singular_name ); 
  89. $widget_conditions_post_type_archives[] = array( 'post_type_archive-' . $post_type->name, $post_type->labels->name ); 
  90.  
  91. $widget_conditions_data['page'][] = array( __( 'Post type:', 'jetpack' ), $widget_conditions_post_types ); 
  92.  
  93. $widget_conditions_data['page'][] = array( __( 'Post type Archives:', 'jetpack' ), $widget_conditions_post_type_archives ); 
  94.  
  95. $pages_dropdown = preg_replace( '/<\/?select[^>]*?>/i', '', wp_dropdown_pages( array( 'echo' => false ) ) ); 
  96.  
  97. preg_match_all( '/value=.([0-9]+).[^>]*>([^<]+)</', $pages_dropdown, $page_ids_and_titles, PREG_SET_ORDER ); 
  98.  
  99. $static_pages = array(); 
  100.  
  101. foreach ( $page_ids_and_titles as $page_id_and_title ) { 
  102. $static_pages[] = array( (string) $page_id_and_title[1], $page_id_and_title[2] ); 
  103.  
  104. $widget_conditions_data['page'][] = array( __( 'Static page:', 'jetpack' ), $static_pages ); 
  105.  
  106. $widget_conditions_data['taxonomy'] = array(); 
  107. $widget_conditions_data['taxonomy'][] = array( '', __( 'All taxonomy pages', 'jetpack' ) ); 
  108.  
  109. $taxonomies = get_taxonomies( array( '_builtin' => false ), 'objects' ); 
  110. usort( $taxonomies, array( __CLASS__, 'strcasecmp_name' ) ); 
  111.  
  112. foreach ( $taxonomies as $taxonomy ) { 
  113. $taxonomy_terms = get_terms( array( $taxonomy->name ), array( 'number' => 250, 'hide_empty' => false ) ); 
  114.  
  115. $widget_conditions_terms = array(); 
  116. $widget_conditions_terms[] = array( $taxonomy->name, __( 'All pages', 'jetpack' ) ); 
  117.  
  118. foreach ( $taxonomy_terms as $term ) { 
  119. $widget_conditions_terms[] = array( $taxonomy->name . '_tax_' . $term->term_id, $term->name ); 
  120.  
  121. $widget_conditions_data['taxonomy'][] = array( $taxonomy->labels->name . ':', $widget_conditions_terms ); 
  122.  
  123. wp_localize_script( 'widget-conditions', 'widget_conditions_data', $widget_conditions_data ); 
  124.  
  125. // Save a list of the IDs of all pages that have children for dynamically showing the "Include children" checkbox. 
  126. $all_pages = get_pages(); 
  127. $all_parents = array(); 
  128.  
  129. foreach ( $all_pages as $page ) { 
  130. if ( $page->post_parent ) { 
  131. $all_parents[ (string) $page->post_parent ] = true; 
  132.  
  133. $front_page_id = get_option( 'page_on_front' ); 
  134.  
  135. if ( isset( $all_parents[ $front_page_id ] ) ) { 
  136. $all_parents[ 'front' ] = true; 
  137.  
  138. wp_localize_script( 'widget-conditions', 'widget_conditions_parent_pages', $all_parents ); 
  139.  
  140. /** 
  141. * Add the widget conditions to each widget in the admin. 
  142. * @param $widget unused. 
  143. * @param $return unused. 
  144. * @param array $instance The widget settings. 
  145. */ 
  146. public static function widget_conditions_admin( $widget, $return, $instance ) { 
  147. $conditions = array(); 
  148.  
  149. if ( isset( $instance['conditions'] ) ) 
  150. $conditions = $instance['conditions']; 
  151.  
  152. if ( ! isset( $conditions['action'] ) ) 
  153. $conditions['action'] = 'show'; 
  154.  
  155. if ( empty( $conditions['rules'] ) ) 
  156. $conditions['rules'][] = array( 'major' => '', 'minor' => '', 'has_children' => '' ); 
  157.  
  158. if ( empty( $conditions['match_all'] ) ) { 
  159. $conditions['match_all'] = false; 
  160.  
  161. ?> 
  162. <div 
  163. class=" 
  164. widget-conditional 
  165. <?php 
  166. if ( 
  167. empty( $_POST['widget-conditions-visible'] ) 
  168. || $_POST['widget-conditions-visible'] == '0' 
  169. ) { 
  170. ?>widget-conditional-hide<?php 
  171. ?> 
  172. <?php 
  173. if ( ! empty( $conditions['match_all'] ) && $conditions['match_all'] ) { 
  174. ?>intersection<?php 
  175. } else { 
  176. ?>conjunction<?php 
  177. ?> 
  178. "> 
  179. <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 } ?>" /> 
  180. <?php if ( ! isset( $_POST['widget-conditions-visible'] ) ) { ?><a href="#" class="button display-options"><?php _e( 'Visibility', 'jetpack' ); ?></a><?php } ?> 
  181. <div class="widget-conditional-inner"> 
  182. <div class="condition-top"> 
  183. <?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>' ); ?> 
  184. </div><!-- .condition-top --> 
  185.  
  186. <div class="conditions"> 
  187. <?php 
  188.  
  189. foreach ( $conditions['rules'] as $rule_index => $rule ) { 
  190. $rule = wp_parse_args( $rule, array( 'major' => '', 'minor' => '', 'has_children' => '' ) ); 
  191. ?> 
  192. <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'] ); ?>"> 
  193. <div class="selection alignleft"> 
  194. <select class="conditions-rule-major" name="conditions[rules_major][]"> 
  195. <option value="" <?php selected( "", $rule['major'] ); ?>><?php echo esc_html_x( '-- Select --', 'Used as the default option in a dropdown list', 'jetpack' ); ?></option> 
  196. <option value="category" <?php selected( "category", $rule['major'] ); ?>><?php esc_html_e( 'Category', 'jetpack' ); ?></option> 
  197. <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> 
  198.  
  199. <?php if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { // this doesn't work on .com because of caching ?> 
  200. <option value="loggedin" <?php selected( "loggedin", $rule['major'] ); ?>><?php echo esc_html_x( 'User', 'Noun', 'jetpack' ); ?></option> 
  201. <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> 
  202. <?php } ?> 
  203.  
  204. <option value="tag" <?php selected( "tag", $rule['major'] ); ?>><?php echo esc_html_x( 'Tag', 'Noun, as in: "This post has one tag."', 'jetpack' ); ?></option> 
  205. <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> 
  206. <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> 
  207. <?php if ( get_taxonomies( array( '_builtin' => false ) ) ) : ?> 
  208. <option value="taxonomy" <?php selected( "taxonomy", $rule['major'] ); ?>><?php echo esc_html_x( 'Taxonomy', 'Noun, as in: "This post has one taxonomy."', 'jetpack' ); ?></option> 
  209. <?php endif; ?> 
  210. </select> 
  211.  
  212. <?php _ex( 'is', 'Widget Visibility: {Rule Major [Page]} is {Rule Minor [Search results]}', 'jetpack' ); ?> 
  213.  
  214. <select class="conditions-rule-minor" name="conditions[rules_minor][]" <?php if ( ! $rule['major'] ) { ?> disabled="disabled"<?php } ?>> 
  215. <?php /** Include the currently selected value so that if the widget is saved without 
  216. expanding the Visibility section, we don't lose the minor part of the rule. 
  217. If it is opened, this list is cleared out and populated with all the values. */ ?> 
  218. <option value="<?php echo esc_attr( $rule['minor'] ); ?>" selected="selected"></option> 
  219. </select> 
  220.  
  221. <span class="conditions-rule-has-children" <?php if ( ! $rule['has_children'] ) { ?> style="display: none;"<?php } ?>> 
  222. <label> 
  223. <input type="checkbox" name="conditions[page_children][<?php echo $rule_index; ?>]" value="has" <?php checked( $rule['has_children'], true ); ?> /> 
  224. <?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' ); ?> 
  225. </label> 
  226. </span> 
  227. </div> 
  228.  
  229. <div class="condition-control"> 
  230. <span class="condition-conjunction"> 
  231. <?php echo esc_html_x( 'or', 'Shown between widget visibility conditions.', 'jetpack' ); ?> 
  232. </span> 
  233. <span class="condition-intersection"> 
  234. <?php echo esc_html_x( 'and', 'Shown between widget visibility conditions.', 'jetpack' ); ?> 
  235. </span> 
  236. <div class="actions alignright"> 
  237. <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> 
  238. </div> 
  239. </div> 
  240.  
  241. </div><!-- .condition --> 
  242. <?php 
  243.  
  244. ?> 
  245. </div><!-- .conditions --> 
  246. <div class="conditions"> 
  247. <div class="condition-top"> 
  248. <label> 
  249. <input 
  250. type="checkbox" 
  251. name="conditions[match_all]" 
  252. value="1" 
  253. class="conditions-match-all" 
  254. <?php checked( $conditions['match_all'], '1' ); ?> /> 
  255. <?php esc_html_e( 'Match all conditions', 'jetpack' ); ?> 
  256. </label> 
  257. </div><!-- .condition-top --> 
  258. </div><!-- .conditions --> 
  259. </div><!-- .widget-conditional-inner --> 
  260. </div><!-- .widget-conditional --> 
  261. <?php 
  262.  
  263. /** 
  264. * On an AJAX update of the widget settings, process the display conditions. 
  265. * @param array $new_instance New settings for this instance as input by the user. 
  266. * @param array $old_instance Old settings for this instance. 
  267. * @return array Modified settings. 
  268. */ 
  269. public static function widget_update( $instance, $new_instance, $old_instance ) { 
  270. if ( empty( $_POST['conditions'] ) ) { 
  271. return $instance; 
  272.  
  273. $conditions = array(); 
  274. $conditions['action'] = $_POST['conditions']['action']; 
  275. $conditions['match_all'] = ( isset( $_POST['conditions']['match_all'] ) ? '1' : '0' ); 
  276. $conditions['rules'] = array(); 
  277.  
  278. foreach ( $_POST['conditions']['rules_major'] as $index => $major_rule ) { 
  279. if ( ! $major_rule ) 
  280. continue; 
  281.  
  282. $conditions['rules'][] = array( 
  283. 'major' => $major_rule,  
  284. 'minor' => isset( $_POST['conditions']['rules_minor'][$index] ) ? $_POST['conditions']['rules_minor'][$index] : '',  
  285. 'has_children' => isset( $_POST['conditions']['page_children'][$index] ) ? true : false,  
  286. ); 
  287.  
  288. if ( ! empty( $conditions['rules'] ) ) 
  289. $instance['conditions'] = $conditions; 
  290. else 
  291. unset( $instance['conditions'] ); 
  292.  
  293. if ( 
  294. ( isset( $instance['conditions'] ) && ! isset( $old_instance['conditions'] ) ) 
  295. || 
  296. isset( $instance['conditions'], $old_instance['conditions'] ) 
  297. && 
  298. serialize( $instance['conditions'] ) != serialize( $old_instance['conditions'] ) 
  299. ) { 
  300.  
  301. /** 
  302. * Fires after the widget visibility conditions are saved. 
  303. * @module widget-visibility 
  304. * @since 2.4.0 
  305. */ 
  306. do_action( 'widget_conditions_save' ); 
  307. else if ( ! isset( $instance['conditions'] ) && isset( $old_instance['conditions'] ) ) { 
  308.  
  309. /** 
  310. * Fires after the widget visibility conditions are deleted. 
  311. * @module widget-visibility 
  312. * @since 2.4.0 
  313. */ 
  314. do_action( 'widget_conditions_delete' ); 
  315.  
  316. return $instance; 
  317.  
  318. /** 
  319. * Filter the list of widgets for a sidebar so that active sidebars work as expected. 
  320. * @param array $widget_areas An array of widget areas and their widgets. 
  321. * @return array The modified $widget_area array. 
  322. */ 
  323. public static function sidebars_widgets( $widget_areas ) { 
  324. $settings = array(); 
  325.  
  326. foreach ( $widget_areas as $widget_area => $widgets ) { 
  327. if ( empty( $widgets ) ) 
  328. continue; 
  329.  
  330. if ( ! is_array( $widgets ) ) 
  331. continue; 
  332.  
  333. if ( 'wp_inactive_widgets' == $widget_area ) 
  334. continue; 
  335.  
  336. foreach ( $widgets as $position => $widget_id ) { 
  337. // Find the conditions for this widget. 
  338. if ( preg_match( '/^(.+?)-(\d+)$/', $widget_id, $matches ) ) { 
  339. $id_base = $matches[1]; 
  340. $widget_number = intval( $matches[2] ); 
  341. else { 
  342. $id_base = $widget_id; 
  343. $widget_number = null; 
  344.  
  345. if ( ! isset( $settings[$id_base] ) ) { 
  346. $settings[$id_base] = get_option( 'widget_' . $id_base ); 
  347.  
  348. // New multi widget (WP_Widget) 
  349. if ( ! is_null( $widget_number ) ) { 
  350. if ( isset( $settings[$id_base][$widget_number] ) && false === self::filter_widget( $settings[$id_base][$widget_number] ) ) { 
  351. unset( $widget_areas[$widget_area][$position] ); 
  352.  
  353. // Old single widget 
  354. else if ( ! empty( $settings[ $id_base ] ) && false === self::filter_widget( $settings[$id_base] ) ) { 
  355. unset( $widget_areas[$widget_area][$position] ); 
  356.  
  357. return $widget_areas; 
  358.  
  359. public static function template_redirect() { 
  360. self::$passed_template_redirect = true; 
  361.  
  362. /** 
  363. * Generates a condition key based on the rule array 
  364. * @param array $rule 
  365. * @return string key used to retrieve the condition. 
  366. */ 
  367. static function generate_condition_key( $rule ) { 
  368. if ( isset( $rule['has_children'] ) ) { 
  369. return $rule['major'] . ":" . $rule['minor'] . ":" . $rule['has_children']; 
  370. return $rule['major'] . ":" . $rule['minor']; 
  371.  
  372. /** 
  373. * Determine whether the widget should be displayed based on conditions set by the user. 
  374. * @param array $instance The widget settings. 
  375. * @return array Settings to display or bool false to hide. 
  376. */ 
  377. public static function filter_widget( $instance ) { 
  378. global $wp_query; 
  379.  
  380. if ( empty( $instance['conditions'] ) || empty( $instance['conditions']['rules'] ) ) 
  381. return $instance; 
  382.  
  383. // Store the results of all in-page condition lookups so that multiple widgets with 
  384. // the same visibility conditions don't result in duplicate DB queries. 
  385. static $condition_result_cache = array(); 
  386.  
  387. $condition_result = false; 
  388.  
  389. foreach ( $instance['conditions']['rules'] as $rule ) { 
  390. $condition_result = false; 
  391. $condition_key = self::generate_condition_key( $rule ); 
  392.  
  393. if ( isset( $condition_result_cache[ $condition_key ] ) ) { 
  394. $condition_result = $condition_result_cache[ $condition_key ]; 
  395. else { 
  396. switch ( $rule['major'] ) { 
  397. case 'date': 
  398. switch ( $rule['minor'] ) { 
  399. case '': 
  400. $condition_result = is_date(); 
  401. break; 
  402. case 'month': 
  403. $condition_result = is_month(); 
  404. break; 
  405. case 'day': 
  406. $condition_result = is_day(); 
  407. break; 
  408. case 'year': 
  409. $condition_result = is_year(); 
  410. break; 
  411. break; 
  412. case 'page': 
  413. // Previously hardcoded post type options. 
  414. if ( 'post' == $rule['minor'] ) 
  415. $rule['minor'] = 'post_type-post'; 
  416. else if ( ! $rule['minor'] ) 
  417. $rule['minor'] = 'post_type-page'; 
  418.  
  419. switch ( $rule['minor'] ) { 
  420. case '404': 
  421. $condition_result = is_404(); 
  422. break; 
  423. case 'search': 
  424. $condition_result = is_search(); 
  425. break; 
  426. case 'archive': 
  427. $condition_result = is_archive(); 
  428. break; 
  429. case 'posts': 
  430. $condition_result = $wp_query->is_posts_page; 
  431. break; 
  432. case 'home': 
  433. $condition_result = is_home(); 
  434. break; 
  435. case 'front': 
  436. if ( current_theme_supports( 'infinite-scroll' ) ) 
  437. $condition_result = is_front_page(); 
  438. else { 
  439. $condition_result = is_front_page() && !is_paged(); 
  440. break; 
  441. default: 
  442. if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) { 
  443. $condition_result = is_singular( substr( $rule['minor'], 10 ) ); 
  444. } elseif ( substr( $rule['minor'], 0, 18 ) == 'post_type_archive-' ) { 
  445. $condition_result = is_post_type_archive( substr( $rule['minor'], 18 ) ); 
  446. } elseif ( $rule['minor'] == get_option( 'page_for_posts' ) ) { 
  447. // If $rule['minor'] is a page ID which is also the posts page 
  448. $condition_result = $wp_query->is_posts_page; 
  449. } else { 
  450. // $rule['minor'] is a page ID 
  451. $condition_result = is_page() && ( $rule['minor'] == get_the_ID() ); 
  452.  
  453. // Check if $rule['minor'] is parent of page ID 
  454. if ( ! $condition_result && isset( $rule['has_children'] ) && $rule['has_children'] ) 
  455. $condition_result = wp_get_post_parent_id( get_the_ID() ) == $rule['minor']; 
  456. break; 
  457. break; 
  458. case 'tag': 
  459. // All tag pages. 
  460. if( ! $rule['minor'] ) { 
  461. if ( is_tag() ) { 
  462. $condition_result = true; 
  463. } else if ( is_singular() ) { 
  464. if( in_array( 'post_tag', get_post_taxonomies() ) ) { 
  465. $condition_result = true; 
  466. break; 
  467.  
  468. // All pages with the specified tag term. 
  469. if ( is_tag( $rule['minor'] ) ) { 
  470. $condition_result = true; 
  471. else if ( is_singular() && has_term( $rule['minor'], 'post_tag' ) ) { 
  472. $condition_result = true; 
  473. break; 
  474. case 'category': 
  475. // All category pages. 
  476. if( ! $rule['minor'] ) { 
  477. if ( is_category() ) { 
  478. $condition_result = true; 
  479. else if ( is_singular() ) { 
  480. if( in_array( 'category', get_post_taxonomies() ) ) { 
  481. $condition_result = true; 
  482. break; 
  483.  
  484. // All pages with the specified category term. 
  485. if ( is_category( $rule['minor'] ) ) { 
  486. $condition_result = true; 
  487. else if ( is_singular() && has_term( $rule['minor'], 'category' ) ) { 
  488. $condition_result = true; 
  489. break; 
  490. case 'loggedin': 
  491. $condition_result = is_user_logged_in(); 
  492. if ( 'loggedin' !== $rule['minor'] ) { 
  493. $condition_result = ! $condition_result; 
  494. break; 
  495. case 'author': 
  496. $post = get_post(); 
  497. if ( ! $rule['minor'] && is_author() ) 
  498. $condition_result = true; 
  499. else if ( $rule['minor'] && is_author( $rule['minor'] ) ) 
  500. $condition_result = true; 
  501. else if ( is_singular() && $rule['minor'] && $rule['minor'] == $post->post_author ) 
  502. $condition_result = true; 
  503. break; 
  504. case 'role': 
  505. if( is_user_logged_in() ) { 
  506. $current_user = wp_get_current_user(); 
  507.  
  508. $user_roles = $current_user->roles; 
  509.  
  510. if( in_array( $rule['minor'], $user_roles ) ) { 
  511. $condition_result = true; 
  512. } else { 
  513. $condition_result = false; 
  514.  
  515. } else { 
  516. $condition_result = false; 
  517. break; 
  518. case 'post_type': 
  519. if ( substr( $rule['minor'], 0, 10 ) == 'post_type-' ) { 
  520. $condition_result = is_singular( substr( $rule['minor'], 10 ) ); 
  521. } elseif ( substr( $rule['minor'], 0, 18 ) == 'post_type_archive-' ) { 
  522. $condition_result = is_post_type_archive( substr( $rule['minor'], 18 ) ); 
  523. break; 
  524. case 'taxonomy': 
  525. // All taxonomy pages. 
  526. if( ! $rule['minor'] ) { 
  527. if ( is_archive() ) { 
  528. if ( is_tag() || is_category() || is_tax() ) { 
  529. $condition_result = true; 
  530. else if ( is_singular() ) { 
  531. $post_taxonomies = get_post_taxonomies(); 
  532. $condition_result = ! empty( $post_taxonomies ); 
  533. break; 
  534.  
  535. // Specified taxonomy page. 
  536. $term = explode( '_tax_', $rule['minor'] ); // $term[0] = taxonomy name; $term[1] = term id 
  537. if ( isset( $term[0] ) && isset( $term[1] ) ) { 
  538. $term[1] = self::maybe_get_split_term( $term[1], $term[0] ); 
  539.  
  540. // All pages of the specified taxonomy. 
  541. if ( ! isset( $term[1] ) || ! $term[1] ) { 
  542. if ( is_tax( $term[0] ) ) { 
  543. $condition_result = true; 
  544. else if ( is_singular() ) { 
  545. if( in_array( $term[0], get_post_taxonomies() ) ) { 
  546. $condition_result = true; 
  547. break; 
  548.  
  549. // All pages with the specified taxonomy term. 
  550. if ( is_tax( $term[0], $term[1] ) ) { 
  551. $condition_result = true; 
  552. else if ( is_singular() && has_term( $term[1], $term[0] ) ) { 
  553. $condition_result = true; 
  554. break; 
  555.  
  556. if ( $condition_result || self::$passed_template_redirect ) { 
  557. // Some of the conditions will return false when checked before the template_redirect 
  558. // action has been called, like is_page(). Only store positive lookup results, which 
  559. // won't be false positives, before template_redirect, and everything after. 
  560. $condition_result_cache[ $condition_key ] = $condition_result; 
  561.  
  562. if ( 
  563. isset( $instance['conditions']['match_all'] ) 
  564. && $instance['conditions']['match_all'] == '1' 
  565. && ! $condition_result 
  566. ) { 
  567.  
  568. // In case the match_all flag was set we quit on first failed condition 
  569. break; 
  570. } elseif ( 
  571. empty( $instance['conditions']['match_all'] ) 
  572. || $instance['conditions']['match_all'] !== '1' 
  573. && $condition_result 
  574. ) { 
  575.  
  576. // Only quit on first condition if the match_all flag was not set 
  577. break; 
  578.  
  579. if ( 
  580. 'show' == $instance['conditions']['action'] 
  581. && ! $condition_result 
  582. ) || ( 
  583. 'hide' == $instance['conditions']['action'] 
  584. && $condition_result 
  585. ) { 
  586. return false; 
  587.  
  588. return $instance; 
  589.  
  590. public static function strcasecmp_name( $a, $b ) { 
  591. return strcasecmp( $a->name, $b->name ); 
  592.  
  593. public static function maybe_get_split_term( $old_term_id = '', $taxonomy = '' ) { 
  594. $term_id = $old_term_id; 
  595.  
  596. if ( 'tag' == $taxonomy ) { 
  597. $taxonomy = 'post_tag'; 
  598.  
  599. if ( function_exists( 'wp_get_split_term' ) && $new_term_id = wp_get_split_term( $old_term_id, $taxonomy ) ) { 
  600. $term_id = $new_term_id; 
  601.  
  602. return $term_id; 
  603.  
  604. /** 
  605. * Upgrade routine to go through all widgets and move the Post Type 
  606. * setting to its newer location. 
  607. * @since 4.7.1 
  608. */ 
  609. static function migrate_post_type_rules() { 
  610. global $wp_registered_widgets; 
  611.  
  612. $sidebars_widgets = get_option( 'sidebars_widgets' ); 
  613.  
  614. // Going through all sidebars and through inactive and orphaned widgets 
  615. foreach ( $sidebars_widgets as $s => $sidebar ) { 
  616. if ( ! is_array( $sidebar ) ) { 
  617. continue; 
  618.  
  619. foreach ( $sidebar as $w => $widget ) { 
  620. // $widget is the id of the widget 
  621. if ( empty( $wp_registered_widgets[ $widget ] ) ) { 
  622. continue; 
  623.  
  624. $opts = $wp_registered_widgets[ $widget ]; 
  625. $instances = get_option( $opts['callback'][0]->option_name ); 
  626.  
  627. // Going through each instance of the widget 
  628. foreach( $instances as $number => $instance ) { 
  629. if ( 
  630. ! is_array( $instance ) || 
  631. empty( $instance['conditions'] ) || 
  632. empty( $instance['conditions']['rules'] ) 
  633. ) { 
  634. continue; 
  635.  
  636. // Going through all visibility rules 
  637. foreach( $instance['conditions']['rules'] as $index => $rule ) { 
  638.  
  639. // We only need Post Type rules 
  640. if ( 'post_type' !== $rule['major'] ) { 
  641. continue; 
  642.  
  643. $rule_type = false; 
  644.  
  645. // Post type or type archive rule 
  646. if ( 0 === strpos( $rule['minor'], 'post_type_archive' ) ) { 
  647. $rule_type = 'post_type_archive'; 
  648. } else if ( 0 === strpos( $rule['minor'], 'post_type' ) ) { 
  649. $rule_type = 'post_type'; 
  650.  
  651. if ( $rule_type ) { 
  652. $post_type = substr( $rule['minor'], strlen( $rule_type ) + 1 ); 
  653. $rule['minor'] = $rule_type . '-' . $post_type; 
  654. $rule['major'] = 'page'; 
  655.  
  656. $instances[ $number ]['conditions']['rules'][ $index ] = $rule; 
  657.  
  658. update_option( $opts['callback'][0]->option_name, $instances ); 
  659.