nor'], 'post_tag' ) ) { $condition_result = true; } break; case 'category': // All category pages. if ( ! $rule['minor'] ) { if ( is_category() ) { $condition_result = true; } elseif ( is_singular() ) { if ( in_array( 'category', get_post_taxonomies(), true ) ) { $condition_result = true; } } break; } // All pages with the specified category term. if ( is_category( $rule['minor'] ) ) { $condition_result = true; } elseif ( is_singular() && has_term( $rule['minor'], 'category' ) ) { $condition_result = true; } break; case 'loggedin': $condition_result = is_user_logged_in(); if ( 'loggedin' !== $rule['minor'] ) { $condition_result = ! $condition_result; } break; case 'author': if ( ! $rule['minor'] && is_author() ) { $condition_result = true; } elseif ( $rule['minor'] && is_author( $rule['minor'] ) ) { $condition_result = true; } elseif ( is_singular() && $rule['minor'] ) { $post = get_post(); if ( $post && $rule['minor'] === $post->post_author ) { $condition_result = true; } } break; case 'role': if ( is_user_logged_in() ) { $current_user = wp_get_current_user(); $user_roles = $current_user->roles; if ( in_array( $rule['minor'], $user_roles, true ) ) { $condition_result = true; } else { $condition_result = false; } } else { $condition_result = false; } break; case 'post_type': if ( str_starts_with( $rule['minor'], 'post_type-' ) ) { $condition_result = is_singular( substr( $rule['minor'], 10 ) ); } elseif ( str_starts_with( $rule['minor'], 'post_type_archive-' ) ) { $condition_result = is_post_type_archive( substr( $rule['minor'], 18 ) ); } break; case 'taxonomy': // All taxonomy pages. if ( ! $rule['minor'] ) { if ( is_archive() ) { if ( is_tag() || is_category() || is_tax() ) { $condition_result = true; } } elseif ( is_singular() ) { $post_taxonomies = get_post_taxonomies(); $condition_result = ! empty( $post_taxonomies ); } break; } // Specified taxonomy page. $term = explode( '_tax_', $rule['minor'] ); // $term[0] is taxonomy name; $term[1] is term id. if ( isset( $term[0] ) && isset( $term[1] ) ) { $term[1] = self::maybe_get_split_term( $term[1], $term[0] ); } // All pages of the specified taxonomy. if ( ! isset( $term[1] ) || ! $term[1] ) { if ( is_tax( $term[0] ) ) { $condition_result = true; } elseif ( is_singular() ) { if ( in_array( $term[0], get_post_taxonomies(), true ) ) { $condition_result = true; } } break; } // All pages with the specified taxonomy term. if ( is_tax( $term[0], $term[1] ) ) { $condition_result = true; } elseif ( is_singular() && has_term( $term[1], $term[0] ) ) { $condition_result = true; } break; } if ( $condition_result || self::$passed_template_redirect ) { // Some of the conditions will return false when checked before the template_redirect // action has been called, like is_page(). Only store positive lookup results, which // won't be false positives, before template_redirect, and everything after. $condition_result_cache[ $condition_key ] = $condition_result; } } if ( isset( $conditions['match_all'] ) && '1' === $conditions['match_all'] && ! $condition_result ) { // In case the match_all flag was set we quit on first failed condition. break; } elseif ( ( empty( $conditions['match_all'] ) || '1' !== $conditions['match_all'] ) && $condition_result ) { // Only quit on first condition if the match_all flag was not set. break; } } if ( ( 'show' === $conditions['action'] && ! $condition_result ) || ( 'hide' === $conditions['action'] && $condition_result ) ) { return false; } return true; } /** * Helper function wrapping strcasecmp to compare term names. * * @param string $a str1. * @param string $b str2. */ public static function strcasecmp_name( $a, $b ) { return strcasecmp( $a->name, $b->name ); } /** * Determine if provided term has been split. * * @param int $old_term_id Old term id to test. * @param string $taxonomy Taxonmy that $old_term_id belongs to. */ public static function maybe_get_split_term( $old_term_id = '', $taxonomy = '' ) { $term_id = $old_term_id; if ( 'tag' === $taxonomy ) { $taxonomy = 'post_tag'; } $new_term_id = wp_get_split_term( $old_term_id, $taxonomy ); if ( $new_term_id ) { $term_id = $new_term_id; } return $term_id; } /** * Upgrade routine to go through all widgets and move the Post Type * setting to its newer location. * * @since 4.7.1 */ public static function migrate_post_type_rules() { global $wp_registered_widgets; $sidebars_widgets = get_option( 'sidebars_widgets' ); // Going through all sidebars and through inactive and orphaned widgets. foreach ( $sidebars_widgets as $sidebar ) { if ( ! is_array( $sidebar ) ) { continue; } foreach ( $sidebar as $widget ) { // $widget is the id of the widget if ( empty( $wp_registered_widgets[ $widget ] ) ) { continue; } $opts = $wp_registered_widgets[ $widget ]; $instances = get_option( $opts['callback'][0]->option_name ); if ( ! is_array( $instances ) || empty( $instances ) ) { continue; } // Going through each instance of the widget. foreach ( $instances as $number => $instance ) { if ( ! is_array( $instance ) || empty( $instance['conditions'] ) || empty( $instance['conditions']['rules'] ) ) { continue; } // Going through all visibility rules. foreach ( $instance['conditions']['rules'] as $index => $rule ) { // We only need Post Type rules. if ( 'post_type' !== $rule['major'] ) { continue; } $rule_type = false; // Post type or type archive rule. if ( str_starts_with( $rule['minor'], 'post_type_archive' ) ) { $rule_type = 'post_type_archive'; } elseif ( str_starts_with( $rule['minor'], 'post_type' ) ) { $rule_type = 'post_type'; } if ( $rule_type ) { $post_type = substr( $rule['minor'], strlen( $rule_type ) + 1 ); $rule['minor'] = $rule_type . '-' . $post_type; $rule['major'] = 'page'; $instances[ $number ]['conditions']['rules'][ $index ] = $rule; } } } update_option( $opts['callback'][0]->option_name, $instances ); } } } } add_action( 'init', array( 'Jetpack_Widget_Conditions', 'init' ) ); // Add the 'conditions' attribute to server side rendered blocks // 'init' happens too late to hook on block registration. global $pagenow; $current_url = ! empty( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; if ( is_customize_preview() || 'widgets.php' === $pagenow || str_contains( $current_url, '/wp-json/wp/v2/block-renderer' ) || 1 === preg_match( '~^/wp/v2/sites/\d+/block-renderer~', $current_url ) ) { Jetpack_Widget_Conditions::add_block_attributes_filter(); }