*/ private static $memo = array(); /** * Recursion flag. * * @var bool */ private static $in = false; /** * Decisions made this request, by widget ID. * * @var array */ private static $decisions = array(); /** * Hidden widget IDs kept in place (explain mode, Customizer preview). * * @var array */ private static $kept_hidden = array(); /** * Hook in. */ public static function register() { add_filter( 'sidebars_widgets', array( __CLASS__, 'filter' ), 10 ); add_filter( 'widget_display_callback', array( __CLASS__, 'display' ), 10, 3 ); add_filter( 'dynamic_sidebar_params', array( __CLASS__, 'preview_class' ), 10 ); add_action( 'wp_enqueue_scripts', array( __CLASS__, 'preview_style' ) ); } /** * Reset per-request state (tests, switch_to_blog). */ public static function reset() { self::$memo = array(); self::$decisions = array(); self::$kept_hidden = array(); self::$in = false; } /** * Decision for a widget made this request, if any. * * @param string $widget_id Widget ID. * @return Decision|null */ public static function decision_for( $widget_id ) { return isset( self::$decisions[ $widget_id ] ) ? self::$decisions[ $widget_id ] : null; } /** * The sidebars_widgets callback. * * @param mixed $sidebars Sidebars => widget IDs. * @return mixed */ public static function filter( $sidebars ) { if ( ! is_array( $sidebars ) || self::$in ) { return $sidebars; } $mode = Request_Guard::widgets(); if ( Request_Guard::SKIP === $mode ) { return $sidebars; } $explain = Explain_Mode::active(); $key = get_current_blog_id() . ':' . get_current_user_id() . ':' . self::query_state() . ':' . md5( (string) wp_json_encode( $sidebars ) ); $use = ! $explain && Request_Guard::EVALUATE === $mode; if ( $use && isset( self::$memo[ $key ] ) ) { // Restore the decisions of this exact state so display() agrees with the list. self::$decisions = array_merge( self::$decisions, self::$memo[ $key ][1] ); return self::$memo[ $key ][0]; } $made = array(); self::$in = true; try { $keep = $explain || ( is_customize_preview() && current_user_can( 'edit_theme_options' ) ); $fix = (bool) Settings::get( 'main_query_fix' ); $out = $sidebars; foreach ( $sidebars as $area => $ids ) { if ( 'wp_inactive_widgets' === $area || ! is_array( $ids ) ) { continue; } foreach ( $ids as $pos => $wid ) { if ( ! is_string( $wid ) ) { continue; } $d = Request_Guard::DEFER === $mode ? self::deferred( $wid ) : self::decide( $wid, $fix ); if ( null === $d ) { continue; } self::$decisions[ $wid ] = $d; $made[ $wid ] = $d; if ( ! $d->visible ) { if ( $keep ) { self::$kept_hidden[ $wid ] = true; } else { unset( $out[ $area ][ $pos ] ); } } } } } finally { self::$in = false; } if ( $use ) { self::$memo[ $key ] = array( $out, $made ); } return $out; } /** * The page state a legacy rule can read: which query object is current, * its loop position, the current post and the loop's day markers. Widget * Logic 5.10.4 decided again on every sidebars_widgets call, so a theme * that replaces the query or calls the sidebar inside the loop got answers * for that moment; the memo must not serve an answer from another moment. * * @return string */ private static function query_state() { $q = isset( $GLOBALS['wp_query'] ) && is_object( $GLOBALS['wp_query'] ) ? $GLOBALS['wp_query'] : null; $post = isset( $GLOBALS['post'] ) && is_object( $GLOBALS['post'] ) && isset( $GLOBALS['post']->ID ) ? (int) $GLOBALS['post']->ID : 0; $parts = array( $q ? spl_object_id( $q ) : 0, $q && isset( $q->current_post ) ? (int) $q->current_post : -2, $q && ! empty( $q->in_the_loop ) ? 1 : 0, $post, isset( $GLOBALS['currentday'] ) && is_scalar( $GLOBALS['currentday'] ) ? (string) $GLOBALS['currentday'] : '', isset( $GLOBALS['previousday'] ) && is_scalar( $GLOBALS['previousday'] ) ? (string) $GLOBALS['previousday'] : '', ); return md5( implode( '|', $parts ) ); } /** * Before the main query exists: any rule-carrying widget is held back. * * @param string $wid Widget ID. * @return Decision|null */ private static function deferred( $wid ) { if ( null === Widget_Store::rule( $wid ) && ! self::block_rules_present( $wid ) ) { return null; } $d = new Decision(); $d->visible = false; $d->tree_result = Decision::U; $d->fallback_applied = 'default_hide'; $d->unknowns[] = array( 'path' => 'tree', 'reason' => 'condition_unavailable', 'text' => 'main_query_not_ready', ); return $d; } /** * Decide one widget in page context. * * @param string $wid Widget ID. * @param bool $fix Main-query fix. * @return Decision|null Null when the widget carries no rule at all. */ public static function decide( $wid, $fix ) { $raw = Widget_Store::rule( $wid ); $has_block = self::block_rules_present( $wid ); if ( null === $raw && ! $has_block ) { return null; } $run = static function () use ( $raw, $wid, $has_block ) { $evaluator = new Evaluator(); $sim = Explain_Mode::simulating(); $d = $evaluator->evaluate( Rule::from_stored( $raw, 'widget' ), array( 'simulated_visitor' => $sim ) ); if ( $d->visible && $has_block && self::all_top_blocks_hidden( $wid, $evaluator, $sim ) ) { $d->visible = false; $d->fallback_applied = $d->fallback_applied ? $d->fallback_applied : null; $d->badges[] = array( 'path' => 'content', 'badge' => 'all_blocks_hidden', ); } return $d; }; list( $d, $swapped, $mismatch ) = Query_Context::run( static function () use ( $run ) { return Explain_Mode::with_visitor( $run ); }, $fix ); $d->query_swapped = $swapped; $d->query_mismatch = $mismatch; return $d; } /** * Does a block widget's content carry block rules (cheap string check). * * @param string $wid Widget ID. * @return bool */ private static function block_rules_present( $wid ) { if ( 0 !== strpos( $wid, 'block-' ) ) { return false; } $inst = Widget_Store::instance( $wid ); return is_array( $inst ) && isset( $inst['content'] ) && is_string( $inst['content'] ) && false !== strpos( $inst['content'], '"gateforgeRule"' ); } /** * Sidebar-emptiness check: every top-level block carries a rule that hides it. * * @param string $wid Widget ID. * @param Evaluator $evaluator Evaluator. * @param bool $sim Simulated visitor. * @return bool */ private static function all_top_blocks_hidden( $wid, Evaluator $evaluator, $sim ) { $inst = Widget_Store::instance( $wid ); $blocks = parse_blocks( (string) $inst['content'] ); $seen = 0; foreach ( $blocks as $b ) { if ( null === $b['blockName'] ) { if ( '' !== trim( (string) $b['innerHTML'] ) ) { return false; } continue; } ++$seen; if ( ! isset( $b['attrs']['metadata']['gateforgeRule'] ) ) { return false; } $d = $evaluator->evaluate( Rule::from_stored( $b['attrs']['metadata']['gateforgeRule'], 'block' ), array( 'simulated_visitor' => $sim ) ); if ( $d->visible ) { return false; } } return $seen > 0; } /** * Second enforcement point for widgets rendered by dynamic_sidebar (F24). * * @param array|false $instance Instance. * @param \WP_Widget $widget Widget. * @param array $args Sidebar args. * @return array|false */ public static function display( $instance, $widget, $args ) { unset( $args ); if ( false === $instance || ! $widget instanceof \WP_Widget || ! is_string( $widget->id ) ) { return $instance; } $wid = (string) $widget->id; $has = ( is_array( $instance ) && array_key_exists( Widget_Store::KEY, $instance ) ) || isset( self::$decisions[ $wid ] ); if ( ! $has ) { return $instance; } $mode = Request_Guard::widgets(); if ( Request_Guard::SKIP === $mode ) { return $instance; } $d = isset( self::$decisions[ $wid ] ) ? self::$decisions[ $wid ] : ( Request_Guard::DEFER === $mode ? self::deferred( $wid ) : self::decide( $wid, (bool) Settings::get( 'main_query_fix' ) ) ); if ( null === $d ) { return $instance; } if ( Explain_Mode::active() ) { Explain_Mode::render_widget_note( $wid, $d ); return $d->visible ? $instance : false; } if ( ! $d->visible && ! isset( self::$kept_hidden[ $wid ] ) ) { return false; } return $instance; } /** * Customizer preview: mark hidden widgets with a class (admin-only). * * @param array $params Sidebar params. * @return array */ public static function preview_class( $params ) { if ( ! is_customize_preview() || Explain_Mode::active() || ! isset( $params[0]['widget_id'], $params[0]['before_widget'] ) ) { return $params; } if ( isset( self::$kept_hidden[ $params[0]['widget_id'] ] ) ) { $params[0]['before_widget'] = preg_replace( '/class="/', 'class="gtfg-preview-hidden ', $params[0]['before_widget'], 1 ); } return $params; } /** * Customizer preview fade style, only for users who can edit widgets. * Enqueued as an inline style on a registered handle (no raw output). */ public static function preview_style() { if ( is_customize_preview() && current_user_can( 'edit_theme_options' ) ) { wp_register_style( 'gateforge-preview', false, array(), GATEFORGE_VERSION ); wp_enqueue_style( 'gateforge-preview' ); wp_add_inline_style( 'gateforge-preview', '.gtfg-preview-hidden{opacity:.2}' ); } } }