-
Notifications
You must be signed in to change notification settings - Fork 58
feat(content-gate): add per-block access control for Group, Stack, and Row blocks #4646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
87e2a3b
feat(content-gate): add Block_Visibility class skeleton
dkoo 4b2c0f2
test(content-gate): complete Block_Visibility hook coverage
dkoo bce2306
feat(content-gate): implement render_block fast path
dkoo e3920bb
test(content-gate): add is_admin fast path coverage
dkoo 4479ff3
feat(content-gate): implement registration rule evaluation
dkoo a066381
style(content-gate): remove trailing whitespace in Block_Visibility t…
dkoo c572a67
test(content-gate): add access rule evaluation and caching tests
dkoo fe6fbe5
feat(content-gate): implement block visibility render filter
dkoo 2dd68d6
feat(content-gate): register block attributes server-side
dkoo ea4eb67
feat(content-gate): enqueue block visibility editor assets
dkoo 39954f5
fix(content-gate): use edit_others_posts cap and strip callbacks from…
dkoo ee8e3d0
feat(content-gate): add block visibility JS entry and attribute regis…
dkoo b2c8ea2
feat(content-gate): implement block visibility Inspector panel
dkoo 9b6b327
feat(content-gate): add AccessRuleValueControl for block visibility p…
dkoo 28a509f
fix(content-gate): handle is_boolean rules, use config.placeholder, f…
dkoo 4b725c5
test(content-gate): add JS unit tests for block visibility attribute …
dkoo 1af1acb
style: tweak styles for Access Control block visibility panel
dkoo 1ea2dc8
refactor(content-gate): replace any with proper types in block-visibi…
dkoo 323b7aa
refactor: prettier formatting
dkoo 553a4ca
fix(content-gate): address code review issues in block visibility
dkoo 1c141f2
fix(content-gate): address Copilot review feedback on block visibility
dkoo f7f2f15
fix: allow editors to bypass access requirements
dkoo ec7005b
test(content-gate): add editor front-end bypass and restrict tests
dkoo f3dccb5
refactor: allow the blocks that can get access rules to be filtered
dkoo e822588
docs: update docblock
dkoo 2b50ec8
refactor: apply filter to all instances of $target_blocks
dkoo ff4ec4d
feat(content-gate): add gate mode to per-block access control
dkoo a0ee1ea
style: tweak UI in Access Control panel
dkoo 5292632
style: replace hardcoded px with WP grid-unit tokens
thomasguillot e844d3a
style: keep default ToggleControl order in Access Control panel
thomasguillot 04640ad
refactor(content-gate): tighten Access Control panel layout
thomasguillot 3d3d276
fix(content-gate): address human review feedback on block visibility
dkoo fdb124a
refactor(content-gate): use TokenItem objects in FormTokenField for I…
dkoo e8dff42
Revert "refactor(content-gate): use TokenItem objects in FormTokenFie…
dkoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,366 @@ | ||
| <?php | ||
| /** | ||
| * Newspack Block Access Control. | ||
| * | ||
| * Per-block visibility control based on content restriction rules. | ||
| * | ||
| * @package Newspack | ||
| */ | ||
|
|
||
| namespace Newspack; | ||
|
|
||
| defined( 'ABSPATH' ) || exit; | ||
|
|
||
| /** | ||
| * Block_Visibility class. | ||
| */ | ||
| class Block_Visibility { | ||
|
|
||
| /** | ||
| * Initialize hooks. | ||
| */ | ||
| public static function init() { | ||
| add_filter( 'render_block', [ __CLASS__, 'filter_render_block' ], 10, 2 ); | ||
| add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] ); | ||
| add_filter( 'register_block_type_args', [ __CLASS__, 'register_block_type_args' ], 10, 2 ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of blocks that can be configured for access control visibility. | ||
| * | ||
| * @return array | ||
| */ | ||
| private static function get_target_blocks() { | ||
| /** | ||
| * Filters the list of blocks that can be configured for access control visibility. | ||
| * | ||
| * @param array $target_blocks List of block names. | ||
| * @return array | ||
| */ | ||
| $target_blocks = apply_filters( 'newspack_content_gate_block_visibility_blocks', [ 'core/group', 'core/stack', 'core/row' ] ); | ||
| return $target_blocks; | ||
| } | ||
|
|
||
| /** | ||
| * Filter rendered block output based on access control attributes. | ||
| * | ||
| * @param string $block_content Rendered block HTML. | ||
| * @param array $block Block data. | ||
| * @return string | ||
| */ | ||
| public static function filter_render_block( $block_content, $block ) { | ||
| if ( ! in_array( $block['blockName'] ?? '', self::get_target_blocks(), true ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| // Bypass access control in admin screens and REST requests (block renderer, | ||
| // preview, query-loop rendering inside the editor) so blocks are never hidden | ||
| // from editors during content authoring. | ||
| if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $mode = $block['attrs']['newspackAccessControlMode'] ?? 'gate'; | ||
| $visibility = $block['attrs']['newspackAccessControlVisibility'] ?? 'visible'; | ||
|
|
||
| if ( 'gate' === $mode ) { | ||
| $gate_ids = array_filter( array_map( 'intval', $block['attrs']['newspackAccessControlGateIds'] ?? [] ) ); | ||
| if ( empty( $gate_ids ) ) { | ||
| return $block_content; // No gates selected → pass-through. | ||
| } | ||
| // If every referenced gate has been deleted or unpublished, treat as | ||
| // pass-through regardless of the visibility setting. This mirrors the | ||
| // "no gates selected" case and prevents 'hidden' mode from permanently | ||
| // hiding the block after a gate is removed. | ||
| if ( ! self::has_active_gates( $gate_ids ) ) { | ||
| return $block_content; | ||
| } | ||
| } else { | ||
| // Custom mode: check whether any rules are active before going further. | ||
| $rules = $block['attrs']['newspackAccessControlRules'] ?? []; | ||
|
|
||
| // Defensive cast: the block parser can occasionally yield a stdClass for | ||
| // object-typed attributes (e.g. after JSON round-trips). | ||
| if ( is_object( $rules ) ) { | ||
| $rules = (array) $rules; | ||
| } elseif ( ! is_array( $rules ) ) { | ||
| $rules = []; | ||
| } | ||
|
|
||
| $has_registration = ! empty( $rules['registration']['active'] ); | ||
| $has_access_rules = ! empty( $rules['custom_access']['active'] ) | ||
| && ! empty( $rules['custom_access']['access_rules'] ); | ||
|
|
||
| if ( ! $has_registration && ! $has_access_rules ) { | ||
| return $block_content; // No active rules → pass-through. | ||
| } | ||
| } | ||
|
|
||
| // Don't restrict content for users who can edit the post it's in. | ||
| $post_id = get_the_ID(); | ||
| $user_id = get_current_user_id(); | ||
| if ( ! empty( $post_id ) && user_can( $user_id, 'edit_post', $post_id ) ) { | ||
| return $block_content; | ||
| } | ||
|
|
||
| $user_matches = ( 'gate' === $mode ) | ||
| ? self::evaluate_gate_rules_for_user( $gate_ids, $user_id ) | ||
| : self::evaluate_rules_for_user( $rules, $user_id ); | ||
|
|
||
| if ( 'visible' === $visibility ) { | ||
| return $user_matches ? $block_content : ''; | ||
| } | ||
| // 'hidden' | ||
| return $user_matches ? '' : $block_content; | ||
| } | ||
|
|
||
| /** | ||
| * Register block attributes server-side for target block types. | ||
| * | ||
| * @param array $args Block type arguments. | ||
| * @param string $block_type Block type name. | ||
| * @return array | ||
| */ | ||
| public static function register_block_type_args( $args, $block_type ) { | ||
| if ( ! in_array( $block_type, self::get_target_blocks(), true ) ) { | ||
| return $args; | ||
| } | ||
|
|
||
| $args['attributes'] = array_merge( | ||
| $args['attributes'] ?? [], | ||
| [ | ||
| 'newspackAccessControlVisibility' => [ | ||
| 'type' => 'string', | ||
| 'default' => 'visible', | ||
| ], | ||
| 'newspackAccessControlMode' => [ | ||
| 'type' => 'string', | ||
| 'default' => 'gate', | ||
| ], | ||
| 'newspackAccessControlGateIds' => [ | ||
| 'type' => 'array', | ||
| 'default' => [], | ||
| 'items' => [ | ||
| 'type' => 'integer', | ||
| ], | ||
| ], | ||
| 'newspackAccessControlRules' => [ | ||
| 'type' => 'object', | ||
| 'default' => (object) [], | ||
| ], | ||
| ] | ||
| ); | ||
| return $args; | ||
| } | ||
|
|
||
| /** | ||
| * Enqueue block editor assets. | ||
| */ | ||
| public static function enqueue_block_editor_assets() { | ||
| if ( ! current_user_can( 'edit_others_posts' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $available_post_types = array_column( | ||
| Content_Restriction_Control::get_available_post_types(), | ||
| 'value' | ||
| ); | ||
| // get_post_type() returns false in the Site Editor / widget screens where | ||
| // no post is in context — in_array( false, [...], true ) is false, so the | ||
| // asset is correctly suppressed. This mirrors the guard in Content_Gate. | ||
| if ( ! in_array( get_post_type(), $available_post_types, true ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $asset_file = dirname( NEWSPACK_PLUGIN_FILE ) . '/dist/content-gate-block-visibility.asset.php'; | ||
| if ( ! file_exists( $asset_file ) ) { | ||
| return; | ||
| } | ||
| $asset = require $asset_file; | ||
|
|
||
| wp_enqueue_script( | ||
| 'newspack-content-gate-block-visibility', | ||
| Newspack::plugin_url() . '/dist/content-gate-block-visibility.js', | ||
| $asset['dependencies'], | ||
| $asset['version'], | ||
| true | ||
| ); | ||
|
|
||
| wp_localize_script( | ||
| 'newspack-content-gate-block-visibility', | ||
| 'newspackBlockVisibility', | ||
| [ | ||
| 'target_blocks' => self::get_target_blocks(), | ||
| 'available_access_rules' => array_map( | ||
| function( $rule ) { | ||
| unset( $rule['callback'] ); | ||
| return $rule; | ||
| }, | ||
| Access_Rules::get_access_rules() | ||
| ), | ||
| 'available_gates' => array_values( | ||
| array_map( | ||
| function( $gate ) { | ||
| return [ | ||
| 'id' => $gate['id'], | ||
| 'title' => $gate['title'], | ||
| ]; | ||
| }, | ||
| Content_Gate::get_gates( Content_Gate::GATE_CPT, 'publish' ) | ||
| ) | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Per-request cache: keyed by "{user_id}:{md5(rules)}" or "gate:{user_id}:{md5(gate_ids)}". | ||
| * | ||
| * @var bool[] | ||
| */ | ||
| private static $rules_match_cache = []; | ||
|
|
||
| /** | ||
| * Reset the per-request cache. Used in unit tests only. | ||
| */ | ||
| public static function reset_cache_for_tests() { | ||
| self::$rules_match_cache = []; | ||
| } | ||
|
|
||
| /** | ||
| * Public wrapper for tests. Calls evaluate_rules_for_user(). | ||
| * | ||
| * @param array $rules Rules array. | ||
| * @param int $user_id User ID. | ||
| * @return bool | ||
| */ | ||
| public static function evaluate_rules_for_user_public( $rules, $user_id ) { | ||
| return self::evaluate_rules_for_user( $rules, $user_id ); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate whether a user matches the block's custom access rules (with caching). | ||
| * | ||
| * @param array $rules Parsed newspackAccessControlRules attribute. | ||
| * @param int $user_id User ID (0 for logged-out). | ||
| * @return bool True if user matches (should be treated as "matching reader"). | ||
| */ | ||
| private static function evaluate_rules_for_user( $rules, $user_id ) { | ||
| $cache_key = $user_id . ':' . md5( wp_json_encode( $rules ) ); | ||
| if ( isset( self::$rules_match_cache[ $cache_key ] ) ) { | ||
| return self::$rules_match_cache[ $cache_key ]; | ||
| } | ||
|
|
||
| $result = self::compute_rules_match( $rules, $user_id ); | ||
| self::$rules_match_cache[ $cache_key ] = $result; | ||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Return true if at least one gate in the list is published and accessible. | ||
| * | ||
| * Used as an early-exit guard in filter_render_block() so that a block whose | ||
| * only gates have all been deleted or unpublished is treated as unrestricted, | ||
| * regardless of the block's visibility setting. | ||
| * | ||
| * @param int[] $gate_ids Array of np_content_gate post IDs. | ||
| * @return bool | ||
| */ | ||
| private static function has_active_gates( $gate_ids ) { | ||
| foreach ( $gate_ids as $gate_id ) { | ||
| $gate = Content_Gate::get_gate( $gate_id ); | ||
| if ( ! \is_wp_error( $gate ) && 'publish' === $gate['status'] ) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Evaluate whether a user matches any of the given gate's access rules (with caching). | ||
| * | ||
| * Assumes at least one gate in $gate_ids is active; call has_active_gates() first | ||
| * when a pass-through fallback is needed for fully-inactive gate lists. | ||
| * | ||
| * @param int[] $gate_ids Array of np_content_gate post IDs. | ||
| * @param int $user_id User ID (0 for logged-out). | ||
| * @return bool | ||
| */ | ||
| private static function evaluate_gate_rules_for_user( $gate_ids, $user_id ) { | ||
| $cache_key = 'gate:' . $user_id . ':' . md5( wp_json_encode( $gate_ids ) ); | ||
| if ( isset( self::$rules_match_cache[ $cache_key ] ) ) { | ||
| return self::$rules_match_cache[ $cache_key ]; | ||
| } | ||
|
|
||
| $result = self::compute_gate_rules_match( $gate_ids, $user_id ); | ||
| self::$rules_match_cache[ $cache_key ] = $result; | ||
| return $result; | ||
| } | ||
|
|
||
| /** | ||
| * Compute whether a user matches the access rules of any of the given gates (uncached). | ||
| * | ||
| * @param int[] $gate_ids Array of np_content_gate post IDs. | ||
| * @param int $user_id User ID (0 for logged-out). | ||
| * @return bool | ||
| */ | ||
| private static function compute_gate_rules_match( $gate_ids, $user_id ) { | ||
| $has_active_gate = false; | ||
|
|
||
| foreach ( $gate_ids as $gate_id ) { | ||
| $gate = Content_Gate::get_gate( $gate_id ); | ||
|
|
||
| // Deleted gate: Content_Gate::get_gate() returns WP_Error when the post | ||
| // doesn't exist. Unpublished gates have status !== 'publish'. Both are | ||
| // skipped so only currently-active gates impose restrictions. | ||
| if ( \is_wp_error( $gate ) || 'publish' !== $gate['status'] ) { | ||
| continue; | ||
| } | ||
|
|
||
| $has_active_gate = true; | ||
|
|
||
| $rules = [ | ||
| 'registration' => $gate['registration'], | ||
| 'custom_access' => $gate['custom_access'], | ||
| ]; | ||
|
|
||
| // OR logic: the user passes if they satisfy any single active gate's rules. | ||
| if ( self::compute_rules_match( $rules, $user_id ) ) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // All gates were deleted or unpublished → no active restriction → pass-through. | ||
| return ! $has_active_gate; | ||
| } | ||
|
|
||
| /** | ||
| * Compute whether a user matches the block's access rules (uncached). | ||
| * | ||
| * @param array $rules Parsed newspackAccessControlRules attribute. | ||
| * @param int $user_id User ID (0 for logged-out). | ||
| * @return bool | ||
| */ | ||
| private static function compute_rules_match( $rules, $user_id ) { | ||
| $registration = $rules['registration'] ?? []; | ||
| $custom_access = $rules['custom_access'] ?? []; | ||
|
|
||
| $registration_passes = true; | ||
| if ( ! empty( $registration['active'] ) ) { | ||
| if ( ! $user_id ) { | ||
| $registration_passes = false; | ||
| } elseif ( ! empty( $registration['require_verification'] ) ) { | ||
| $registration_passes = (bool) get_user_meta( $user_id, Reader_Activation::EMAIL_VERIFIED, true ); | ||
| } | ||
| } | ||
|
|
||
| $access_passes = true; | ||
| if ( ! empty( $custom_access['active'] ) && ! empty( $custom_access['access_rules'] ) ) { | ||
| $access_passes = Access_Rules::evaluate_rules( $custom_access['access_rules'], $user_id ); | ||
| } | ||
|
|
||
| // AND logic: both must pass when both are configured. | ||
| return $registration_passes && $access_passes; | ||
| } | ||
| } | ||
| Block_Visibility::init(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edge case: when all selected gates are deleted or unpublished,
compute_gate_rules_match()returnstrue(line 306 -- "no active restriction, pass-through"). But here,$user_matches = truecombined withvisibility = 'hidden'returns''-- the block is hidden from everyone.The intent of "all gates gone = no restriction" should mean the block always renders, regardless of visibility mode. Consider returning
$block_contentearly (before the visibility check) when the gate-mode evaluation determines there are no active gates, similar to how no-gates-selected is handled on line 69:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3d3d276 addresses this by bailing early if the block attribute value contains no active gates.