state || $rule->read_only ) { return $rule; } $registry = Registry::instance(); $registry->ensure_loaded(); $prev_tree = ( $previous instanceof Rule && Rule::STATE_OK === $previous->state ) ? $previous->tree : null; $stamps = array(); if ( null !== $prev_tree ) { self::collect_stamps( $prev_tree, $stamps ); } $out = clone $rule; $out->v = Rule::SCHEMA_VERSION; $out->tree = self::stamp( $rule->tree, $registry, $stamps, (int) $user_id ); // 5.9 (a): fallback is reset to the previous stored value; only the filter may set it. $out->fallback = ( $previous instanceof Rule ) ? $previous->fallback : null; // 5.9 (e): blocks are never imported. if ( 'block' === $context ) { $out->import = null; } if ( ! is_array( $out->ext ) ) { $out->ext = array(); } unset( $out->ext['gateforge'] ); if ( function_exists( 'apply_filters' ) ) { $filtered = apply_filters( 'gtfg_rule_before_save', $out->to_array(), $previous instanceof Rule ? $previous->to_array() : null, $context ); if ( is_array( $filtered ) ) { if ( array_key_exists( 'fallback', $filtered ) && in_array( $filtered['fallback'], array( 'show', 'hide', null ), true ) ) { $out->fallback = $filtered['fallback']; } if ( isset( $filtered['ext'] ) && is_array( $filtered['ext'] ) ) { foreach ( $filtered['ext'] as $slug => $data ) { if ( 'gateforge' !== $slug && is_string( $slug ) ) { $out->ext[ $slug ] = $data; } } } } } $marker = $out->compute_marker(); $out->ext['gateforge'] = array( 'saved_hash' => $marker ); return $out; } /** * Collect previous risk stamps by condition name. * * @param mixed $node Node. * @param array $stamps Stamps (by reference). */ private static function collect_stamps( $node, array &$stamps ) { if ( ! is_array( $node ) ) { return; } if ( isset( $node['cond'] ) && is_string( $node['cond'] ) && isset( $node['risk'] ) ) { $stamps[ strtolower( $node['cond'] ) ] = 'location' === $node['risk'] ? 'location' : 'audience'; } foreach ( array( 'items' ) as $k ) { if ( isset( $node[ $k ] ) && is_array( $node[ $k ] ) ) { foreach ( $node[ $k ] as $c ) { self::collect_stamps( $c, $stamps ); } } } if ( isset( $node['item'] ) ) { self::collect_stamps( $node['item'], $stamps ); } } /** * Stamp risk and authorization on every cond node. * * @param mixed $node Node. * @param Registry $registry Registry. * @param array $stamps Previous stamps. * @param int $user_id User. * @return mixed */ private static function stamp( $node, Registry $registry, array $stamps, $user_id ) { if ( ! is_array( $node ) ) { return $node; } if ( isset( $node['cond'] ) && is_string( $node['cond'] ) ) { $name = strtolower( $node['cond'] ); $def = $registry->get( $name ); if ( $def ) { $node['risk'] = $def['risk']; if ( 'custom' === $def['source'] && '' !== $def['min_cap'] ) { $allowed = $user_id > 0 && function_exists( 'user_can' ) && user_can( $user_id, $def['min_cap'] ); if ( ! $allowed && empty( $node['denied'] ) ) { // Kept, never stripped; evaluates as UNKNOWN (unauthorized). $node['denied'] = true; } elseif ( $allowed ) { unset( $node['denied'] ); } } } elseif ( isset( $node['risk'] ) && in_array( $node['risk'], array( 'audience', 'location' ), true ) && isset( $stamps[ $name ] ) ) { // Unregistered (Pro off, snippet removed): keep the earlier stamp. $node['risk'] = $stamps[ $name ]; } else { // 5.9 (b): unregistered with no prior stamp counts as audience. $node['risk'] = 'audience'; } return $node; } if ( isset( $node['items'] ) && is_array( $node['items'] ) ) { foreach ( $node['items'] as $i => $c ) { $node['items'][ $i ] = self::stamp( $c, $registry, $stamps, $user_id ); } } if ( isset( $node['item'] ) ) { $node['item'] = self::stamp( $node['item'], $registry, $stamps, $user_id ); } return $node; } }