= self::$buffer_level ) { ob_end_flush(); } self::$buffer_level = 0; } /** * Remove explain arguments and nonce from every tag except our own toolbar links. * * @param string $html Page HTML. * @return string */ public static function strip_links( $html ) { // Our own toolbar links keep their arguments: mark them first. $html = (string) preg_replace( '/(]+id=["\']wp-admin-bar-gateforge-explain[a-z\-]*["\'][^>]*>\s*]*>/', static function ( $m ) { $tag = $m[0]; if ( false !== strpos( $tag, 'data-gtfg-keep' ) || ( false === strpos( $tag, self::ARG ) && false === strpos( $tag, self::NONCE ) ) ) { return $tag; } return self::strip_args( $tag ); }, $html ); } /** * Strip our query arguments from a string. * * @param string $s Text. * @return string */ public static function strip_args( $s ) { $s = preg_replace( '/([?&]|&|&)(' . self::ARG . '|' . self::NONCE . ')=[^&"\'#\s<>]*/', '$1', $s ); $s = preg_replace( '/\?(&|&|&)+/', '?', $s ); $s = preg_replace( '/(&|&|&){2,}/', '$1', $s ); return (string) preg_replace( '/[?](?=["\'#\s>])/', '', $s ); } /** * Toolbar items (front end only). * * @param \WP_Admin_Bar $bar Admin bar. */ public static function toolbar( $bar ) { if ( is_admin() || ! current_user_can( 'edit_theme_options' ) ) { return; } $scheme = is_ssl() ? 'https://' : 'http://'; $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; $base = remove_query_arg( array( self::ARG, self::NONCE ), $scheme . $host . $uri ); $nonce = wp_create_nonce( self::ACTION ); $bar->add_node( array( 'id' => 'gateforge-explain', 'title' => esc_html__( 'Explain visibility', 'gateforge' ), 'href' => add_query_arg( array( self::ARG => 'me', self::NONCE => $nonce, ), $base ), 'meta' => array( 'html' => '' ), ) ); $bar->add_node( array( 'parent' => 'gateforge-explain', 'id' => 'gateforge-explain-me', 'title' => esc_html__( 'View as me', 'gateforge' ), 'href' => add_query_arg( array( self::ARG => 'me', self::NONCE => $nonce, ), $base ), ) ); $bar->add_node( array( 'parent' => 'gateforge-explain', 'id' => 'gateforge-explain-visitor', 'title' => esc_html__( 'View as logged-out visitor', 'gateforge' ), 'href' => add_query_arg( array( self::ARG => 'visitor', self::NONCE => $nonce, ), $base ), ) ); if ( self::active() ) { $bar->add_node( array( 'parent' => 'gateforge-explain', 'id' => 'gateforge-explain-off', 'title' => esc_html__( 'Leave explain mode', 'gateforge' ), 'href' => $base, ) ); } } /** * Admin-only explain CSS, only in an explain request. Enqueued as an * inline style on a registered handle (no raw output). */ public static function style() { if ( ! self::active() ) { return; } wp_register_style( 'gateforge-explain', false, array(), GATEFORGE_VERSION ); wp_enqueue_style( 'gateforge-explain' ); wp_add_inline_style( 'gateforge-explain', '.gtfg-card{border:2px dashed #888;background:#f4f4f4;color:#222;padding:10px;margin:8px 0;font:13px/1.4 sans-serif}.gtfg-card.gtfg-hidden{opacity:.85}.gtfg-tag{display:inline-block;background:#e6f4ea;color:#1e4620;font:12px sans-serif;padding:2px 6px;margin:4px 0}.gtfg-card code{white-space:pre-wrap}' ); } /** * Print the card or tag for one widget. * * @param string $wid Widget ID. * @param Decision $d Decision. */ public static function render_widget_note( $wid, Decision $d ) { echo self::card_html( $wid, $d ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- card_html escapes every part. } /** * Build the card or tag HTML. * * @param string $wid Widget ID. * @param Decision $d Decision. * @return string */ public static function card_html( $wid, Decision $d ) { $raw = Widget_Store::rule( $wid ); $inst = null === $raw ? Widget_Store::instance( $wid ) : null; if ( null === $raw && is_array( $inst ) && isset( $inst['content'] ) && is_string( $inst['content'] ) && Block_Rules::has_rules( $inst['content'] ) ) { return self::block_widget_card( $wid, $inst['content'], $d ); } $rule = Rule::from_stored( $raw, 'widget' ); return self::rule_card( $wid, $rule, $d, array(), __( 'widget', 'gateforge' ) ); } /** * Card for a block widget that has no widget rule of its own but whose * blocks carry rules (G-4). The widget-level decision comes from those block * rules: it is shown unless every top-level block is hidden by its rule. A * shown widget gets one line, because its blocks are explained where they * render; a hidden widget never renders, so its card lists each top-level * block's rule and result. * * @param string $wid Widget ID. * @param string $content Block widget content. * @param Decision $d Widget decision. * @return string */ public static function block_widget_card( $wid, $content, Decision $d ) { $blocks = parse_blocks( $content ); $all = array(); Block_Rules::collect( $blocks, $all ); $top_lines = array(); $top_count = 0; foreach ( $blocks as $b ) { if ( ! isset( $b['attrs']['metadata']['gateforgeRule'] ) ) { continue; } ++$top_count; $raw = $b['attrs']['metadata']['gateforgeRule']; $rule = Rule::from_stored( $raw, 'block' ); $bd = self::with_visitor( static function () use ( $rule ) { return ( new Evaluator() )->evaluate( $rule, array( 'simulated_visitor' => self::simulating() ) ); } ); $label = isset( $b['blockName'] ) ? (string) $b['blockName'] : 'block'; $top_lines[] = sprintf( /* translators: 1: block name, 2: plain-English rule summary, 3: shown or hidden. */ __( 'Block %1$s: %2$s Result: %3$s.', 'gateforge' ), $label, Rule::STATE_OK === $rule->state ? Summarizer::summarize( $rule->tree ) : __( 'This rule is not readable, so it fails closed.', 'gateforge' ), $bd->visible ? __( 'shown', 'gateforge' ) : __( 'hidden', 'gateforge' ) ); $decided = self::decided_line( $rule, $bd ); if ( null !== $decided ) { $top_lines[] = $decided; } } $inner = count( $all ) - $top_count; if ( $d->visible ) { return '
' . esc_html( sprintf( /* translators: 1: widget ID, 2: number of block rules inside the widget. */ _n( '%1$s has no widget rule of its own. %2$d block rule inside it decides what shows; it is explained on that block.', '%1$s has no widget rule of its own. %2$d block rules inside it decide what shows; each is explained on its block.', count( $all ), 'gateforge' ), $wid, count( $all ) ) ) . '
'; } $lines = array(); $lines[] = '' . esc_html( /* translators: %1$s: widget ID. */ sprintf( __( 'GateForge: %1$s', 'gateforge' ), $wid ) ) . ''; $lines[] = esc_html__( 'This widget has no widget rule of its own. It is hidden because every block at its top level is hidden by its own rule:', 'gateforge' ); foreach ( $top_lines as $line ) { $lines[] = esc_html( $line ); } if ( $inner > 0 ) { $lines[] = esc_html( sprintf( /* translators: %1$d: number of rules on nested blocks. */ _n( '%1$d more rule sits on a block nested inside; it did not change the result.', '%1$d more rules sit on blocks nested inside; they did not change the result.', $inner, 'gateforge' ), $inner ) ); } $lines[] = esc_html__( 'Visibility is not security: hidden items are not protected content.', 'gateforge' ); return '
' . implode( '
', $lines ) . '
'; } /** * Card or tag for one block (6.6), including the read-only note on core's * own blockVisibility setting (R7 clarification). That note never changes a * GateForge decision. * * @param array $parsed Parsed block. * @param Decision $d Decision. * @return string */ public static function block_card_html( array $parsed, Decision $d ) { $meta = isset( $parsed['attrs']['metadata'] ) && is_array( $parsed['attrs']['metadata'] ) ? $parsed['attrs']['metadata'] : array(); $rule = Rule::from_stored( isset( $meta['gateforgeRule'] ) ? $meta['gateforgeRule'] : null, 'block' ); $label = isset( $parsed['blockName'] ) ? (string) $parsed['blockName'] : 'block'; if ( isset( $meta['name'] ) && is_string( $meta['name'] ) && '' !== $meta['name'] ) { $label .= ' "' . $meta['name'] . '"'; } $extra = array(); $core = array_key_exists( 'blockVisibility', $meta ) ? $meta['blockVisibility'] : null; if ( false === $core ) { $extra[] = __( 'Note: the WordPress Hide setting on this block also hides it on every screen size. GateForge does not change that setting.', 'gateforge' ); } elseif ( is_array( $core ) && isset( $core['viewport'] ) && is_array( $core['viewport'] ) ) { $hidden_on = array(); foreach ( $core['viewport'] as $size => $on ) { if ( false === $on ) { $hidden_on[] = (string) $size; } } if ( $hidden_on ) { $extra[] = sprintf( /* translators: %1$s: list of screen sizes such as mobile, tablet. */ __( 'Note: the WordPress Hide setting on this block hides it on these screen sizes: %1$s. GateForge does not change that setting.', 'gateforge' ), implode( ', ', $hidden_on ) ); } } return self::rule_card( $label, $rule, $d, $extra, __( 'block', 'gateforge' ) ); } /** * Shared card body. * * @param string $wid Item label (widget ID or block name). * @param Rule $rule Rule. * @param Decision $d Decision. * @param array $extra Extra plain-text lines. * @param string $noun "widget" or "block". * @return string */ private static function rule_card( $wid, Rule $rule, Decision $d, array $extra, $noun ) { $summary = Rule::STATE_OK === $rule->state ? Summarizer::summarize( $rule->tree ) : __( 'This rule is not readable, so it fails closed.', 'gateforge' ); $printed = Rule::STATE_OK === $rule->state ? Printer::print_tree( $rule->tree ) : ''; if ( $d->visible && null === $d->both_outcomes ) { /* translators: 1: widget ID, 2: plain-English rule summary. */ $tag = '
' . esc_html( sprintf( __( 'Shown because the rule on %1$s is met. %2$s', 'gateforge' ), $wid, $summary ) ); $decided = self::decided_line( $rule, $d ); if ( null !== $decided ) { $tag .= '
' . esc_html( $decided ); } foreach ( $extra as $line ) { $tag .= '
' . esc_html( $line ); } return $tag . '
'; } $lines = array(); $lines[] = '' . esc_html( /* translators: %1$s: widget ID. */ sprintf( __( 'GateForge: %1$s', 'gateforge' ), $wid ) ) . ''; $lines[] = esc_html( $summary ); if ( '' !== $printed ) { $lines[] = '' . esc_html( $printed ) . ''; } if ( null !== $d->both_outcomes ) { $lines[] = esc_html( sprintf( /* translators: 1: widget or block, 2: shown or hidden if the visitor-dependent part is true, 3: same if false. */ __( 'Part of this rule depends on the visitor and cannot be judged here. If it is true the %1$s is %2$s; if it is false the %1$s is %3$s.', 'gateforge' ), $noun, $d->both_outcomes['if_true'] ? __( 'shown', 'gateforge' ) : __( 'hidden', 'gateforge' ), $d->both_outcomes['if_false'] ? __( 'shown', 'gateforge' ) : __( 'hidden', 'gateforge' ) ) ); } elseif ( null !== self::decided_line( $rule, $d ) ) { $lines[] = esc_html( (string) self::decided_line( $rule, $d ) ); } foreach ( $d->unknowns as $u ) { $lines[] = esc_html( sprintf( /* translators: 1: original text that could not be checked, 2: reason in plain English. */ __( 'Could not be checked: %1$s (%2$s).', 'gateforge' ), '' !== $u['text'] ? $u['text'] : $u['path'], self::reason_text( $u['reason'] ) ) ); } if ( null !== $d->fallback_applied ) { $lines[] = esc_html( sprintf( /* translators: %1$s: which fallback rule applied, in plain English. */ __( 'Because part of the rule could not be checked: %1$s', 'gateforge' ), self::fallback_text( $d->fallback_applied ) ) ); } if ( $d->query_mismatch ) { $lines[] = esc_html__( 'Hint: another query had replaced the main page query when this rule ran. The main-query fix setting may help.', 'gateforge' ); } if ( defined( 'WP_CACHE' ) && WP_CACHE ) { $lines[] = esc_html__( 'Hint: a page cache is active. Visitors may still be served an older copy of this page.', 'gateforge' ); } if ( self::uses_missing_condition( $rule ) ) { $lines[] = esc_html__( 'Hint: this rule uses a condition that is not active on this site (for example from an add-on that is turned off).', 'gateforge' ); } foreach ( $extra as $line ) { $lines[] = esc_html( $line ); } $lines[] = esc_html__( 'Visibility is not security: hidden items are not protected content.', 'gateforge' ); return '
' . implode( '
', $lines ) . '
'; } /** * The "Decided by" line, the same for shown and hidden items. It states the * value the deciding condition itself returned (Decision::decided_value), * never the rule's overall result: under a "not" (a Hide-when rule) the two * are opposite. * * @param Rule $rule Rule. * @param Decision $d Decision. * @return string|null */ public static function decided_line( Rule $rule, Decision $d ) { if ( null !== $d->both_outcomes || null === $d->decided_by || Rule::STATE_OK !== $rule->state ) { return null; } $cond = self::describe_path( $rule, $d->decided_by ); if ( Decision::T !== $d->decided_value && Decision::F !== $d->decided_value ) { /* translators: %1$s: the deciding part of the rule. */ return sprintf( __( 'Decided by %1$s.', 'gateforge' ), $cond ); } return sprintf( /* translators: 1: the deciding condition, 2: the value it returned (true or false). */ __( 'Decided by %1$s, which was %2$s.', 'gateforge' ), $cond, Decision::T === $d->decided_value ? __( 'true', 'gateforge' ) : __( 'false', 'gateforge' ) ); } /** * Describe a decision path using the printed condition. * * @param Rule $rule Rule. * @param string $path Path like tree.items[1].item. * @return string */ private static function describe_path( Rule $rule, $path ) { $node = $rule->tree; if ( preg_match_all( '/\.(items)\[(\d+)\]|\.(item)/', $path, $m, PREG_SET_ORDER ) ) { foreach ( $m as $step ) { if ( ! empty( $step[1] ) && isset( $node['items'][ (int) $step[2] ] ) ) { $node = $node['items'][ (int) $step[2] ]; } elseif ( ! empty( $step[3] ) && isset( $node['item'] ) ) { $node = $node['item']; } } } return Printer::print_tree( $node ); } /** * Does the rule name a condition with no registry entry. * * @param Rule $rule Rule. * @return bool */ private static function uses_missing_condition( Rule $rule ) { $found = false; $walk = static function ( $n, $depth = 0 ) use ( &$walk, &$found ) { if ( $depth > self::MAX_TREE_DEPTH || ! is_array( $n ) ) { return; } if ( isset( $n['cond'] ) && null === Registry::instance()->get( (string) $n['cond'] ) ) { $found = true; } foreach ( isset( $n['items'] ) && is_array( $n['items'] ) ? $n['items'] : array() as $c ) { $walk( $c, $depth + 1 ); } if ( isset( $n['item'] ) ) { $walk( $n['item'], $depth + 1 ); } }; $walk( $rule->tree ); return $found; } /** * Reason code to plain English. * * @param string $code Code. * @return string */ public static function reason_text( $code ) { $map = array( 'unknown_function' => __( 'uses a function GateForge does not know', 'gateforge' ), 'bad_arguments' => __( 'the values given to a condition are not plain literals GateForge can check', 'gateforge' ), 'unsupported_syntax' => __( 'uses PHP that GateForge does not run', 'gateforge' ), 'multiple_statements' => __( 'contains more than one PHP statement', 'gateforge' ), 'parse_error' => __( 'is not valid as written (it also failed in Widget Logic)', 'gateforge' ), 'too_large' => __( 'the rule is over the size limit', 'gateforge' ), 'condition_unavailable' => __( 'the condition is not available right now', 'gateforge' ), 'dependency_inactive' => __( 'the plugin this condition needs is inactive', 'gateforge' ), 'condition_error' => __( 'the condition failed with an error', 'gateforge' ), 'unsupported_node' => __( 'part of the rule uses a format this version does not support', 'gateforge' ), 'group_missing' => __( 'a reusable group it uses was deleted', 'gateforge' ), 'group_cycle' => __( 'a reusable group refers back to itself', 'gateforge' ), 'simulated_visitor' => __( 'depends on the visitor', 'gateforge' ), 'unauthorized' => __( 'it was added by a user who may not use this custom condition', 'gateforge' ), 'invalid_rule' => __( 'the stored rule is damaged', 'gateforge' ), ); return isset( $map[ $code ] ) ? $map[ $code ] : $code; } /** * Fallback row to plain English. * * @param string $row Row. * @return string */ public static function fallback_text( $row ) { $map = array( 'audience_lock' => __( 'it depends on who the visitor is, so it stays hidden (locked).', 'gateforge' ), 'override_show' => __( 'an admin chose "Show when unsure" for this widget, so it is shown.', 'gateforge' ), 'pro_flag_show' => __( 'the saved add-on setting says show.', 'gateforge' ), 'pro_flag_hide' => __( 'the saved add-on setting says hide.', 'gateforge' ), 'default_hide' => __( 'hidden by default when unsure.', 'gateforge' ), ); return isset( $map[ $row ] ) ? $map[ $row ] : $row; } }