Badge codes. */ public static function badges( Rule $rule ) { $out = array(); if ( Rule::STATE_INVALID === $rule->state ) { return array( 'invalid' ); } if ( Rule::STATE_OK !== $rule->state ) { return $out; } if ( is_array( $rule->import ) ) { $out[] = 'imported'; $st = isset( $rule->import['status'] ) ? $rule->import['status'] : ''; if ( 'partial' === $st ) { $out[] = 'partly_translated'; } $risk = isset( $rule->import['risk'] ) ? $rule->import['risk'] : 'audience'; $unsure = in_array( $st, array( 'partial', 'untranslated' ), true ); if ( $unsure && 'audience' === $risk ) { $out[] = 'locked_hidden'; } } $walk = self::walk( $rule->tree ); if ( $walk['pro'] ) { $out[] = 'uses_pro_condition'; } if ( $walk['missing'] ) { $out[] = 'missing_target'; } return $out; } /** * Walk a tree for Pro conditions and missing targets. * * @param mixed $node Node. * @return array{pro:bool, missing:bool} */ private static function walk( $node ) { $res = array( 'pro' => false, 'missing' => false, ); if ( ! is_array( $node ) ) { return $res; } if ( isset( $node['cond'] ) && is_string( $node['cond'] ) ) { $def = Registry::instance()->get( $node['cond'] ); if ( $def && 'pro' === $def['source'] ) { $res['pro'] = true; } if ( $def && '' !== $def['entity'] && isset( $node['args'] ) && is_array( $node['args'] ) ) { $first = isset( $node['args'][0] ) ? $node['args'][0] : null; foreach ( is_array( $first ) ? $first : array( $first ) as $id ) { if ( is_int( $id ) && ! self::entity_exists( $id, $def['entity'] ) ) { $res['missing'] = true; } } } return $res; } $kids = isset( $node['items'] ) && is_array( $node['items'] ) ? $node['items'] : array(); if ( isset( $node['item'] ) ) { $kids[] = $node['item']; } foreach ( $kids as $k ) { $r = self::walk( $k ); $res['pro'] = $res['pro'] || $r['pro']; $res['missing'] = $res['missing'] || $r['missing']; } return $res; } /** * Does an entity exist. * * @param int $id ID. * @param string $entity Entity. * @return bool */ private static function entity_exists( $id, $entity ) { if ( 0 === strpos( $entity, 'term:' ) ) { $t = get_term( $id, substr( $entity, 5 ) ); return $t && ! is_wp_error( $t ); } if ( 'user' === $entity ) { return (bool) get_userdata( $id ); } return (bool) get_post( $id ); } /** * Badge label. * * @param string $code Code. * @return string */ public static function badge_text( $code ) { switch ( $code ) { case 'imported': return __( 'Imported', 'gateforge' ); case 'partly_translated': return __( 'Partly translated', 'gateforge' ); case 'locked_hidden': return __( 'Locked hidden when unsure', 'gateforge' ); case 'uses_pro_condition': return __( 'Uses a Pro condition', 'gateforge' ); case 'missing_target': return __( 'Missing target', 'gateforge' ); case 'invalid': return __( 'Rule unreadable: hidden', 'gateforge' ); } return $code; } /** * Render the section. * * @param \WP_Widget $widget Widget. * @param mixed $ret Return value of form(). * @param array $instance Instance. */ public static function render( $widget, $ret, $instance ) { unset( $ret ); if ( ! current_user_can( 'edit_theme_options' ) || ! $widget instanceof \WP_Widget || ! is_string( $widget->id ) ) { return; } $raw = ( is_array( $instance ) && array_key_exists( Widget_Store::KEY, $instance ) ) ? $instance[ Widget_Store::KEY ] : null; $rule = Rule::from_stored( $raw ); $badges = self::badges( $rule ); $field = $widget->get_field_name( Widget_Copyover::FIELD ); $attrs = array( 'class' => 'gtfg-visibility', 'style' => 'border-top:1px solid #dcdcde;margin-top:1em;padding-top:.5em', 'data-rule' => Rule::STATE_OK === $rule->state ? (string) wp_json_encode( $rule->to_array() ) : '', 'data-locked' => in_array( 'locked_hidden', $badges, true ) ? '1' : '0', 'data-printed' => Rule::STATE_OK === $rule->state ? Printer::print_tree( $rule->tree ) : '', ); echo ' $v ) { echo ' ' . esc_attr( $k ) . '="' . esc_attr( $v ) . '"'; } echo '>'; echo '

' . esc_html__( 'Visibility', 'gateforge' ) . '

'; echo ''; if ( Rule::STATE_NONE === $rule->state ) { echo '

' . esc_html__( 'No rule: this widget is always shown.', 'gateforge' ) . '

'; echo '

'; echo '

'; return; } if ( $badges ) { echo '

'; foreach ( $badges as $b ) { echo '' . esc_html( self::badge_text( $b ) ) . ''; } echo '

'; } if ( Rule::STATE_OK === $rule->state ) { echo '

' . esc_html( Summarizer::summarize( $rule->tree ) ) . '

'; echo '
' . esc_html__( 'Show as text', 'gateforge' ) . '
' . esc_html( Printer::print_tree( $rule->tree ) ) . '
'; if ( is_array( $rule->import ) && isset( $rule->import['source'] ) ) { echo '

' . esc_html__( 'Original Widget Logic text:', 'gateforge' ) . ' ' . esc_html( (string) $rule->import['source'] ) . '

'; } } else { echo '

' . esc_html__( 'This rule cannot be read, so the widget is hidden. It is kept as written.', 'gateforge' ) . '

'; } if ( ! $rule->read_only ) { if ( Rule::STATE_OK === $rule->state ) { echo '

'; } else { echo '

'; } echo '

'; echo '

'; } else { echo '

' . esc_html__( 'This rule was saved by a newer GateForge version. It is shown read-only and never rewritten.', 'gateforge' ) . '

'; } echo ''; } }