|
| 1 | +Jetonomy 1.4.1 introduced the **Public / Private community toggle** documented in [Access Control Settings](../admin-settings/07-access-control.md). On the code side that toggle is enforced through two pieces: |
| 2 | + |
| 3 | +1. A small helper class - `Jetonomy\Visibility` - that every front-end template and REST permission callback can call to decide "is this caller allowed to see community content right now?" |
| 4 | +2. A shell script - `bin/access-matrix-check.sh` - that walks every public REST route as every role in both modes and asserts the responses match the documented contract. |
| 5 | + |
| 6 | +This page covers both. |
| 7 | + |
| 8 | +**Namespace:** `Jetonomy\` |
| 9 | +**Source:** `includes/class-visibility.php`, `bin/access-matrix-check.sh` |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Why the helper exists |
| 14 | + |
| 15 | +Before 1.4.1 the public/private check was sprinkled across templates, the template loader, and individual REST controllers. Each call site had its own "is the community private and the user a guest?" expression, and any new endpoint had to remember to repeat the pattern. The fastest way to ship a leak was to forget the check. |
| 16 | + |
| 17 | +`Jetonomy\Visibility` consolidates that into one read of `jetonomy_settings.guest_read`. The front-end template loader and every public-read REST endpoint route through the same helper, so they answer the same question with the same logic. New endpoints opt into the gate with a single line. |
| 18 | + |
| 19 | +The helper deliberately does **not** look at per-resource visibility (private spaces, blocked users, restricted posts). Those remain the responsibility of individual controllers - `Visibility` only answers the global "can this caller see ANY community content right now?" question. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## API Reference |
| 24 | + |
| 25 | +### `Visibility::can_view_community()` |
| 26 | + |
| 27 | +Returns `true` if the current request should be allowed to see community content, `false` otherwise. In public mode this is always `true`. In private mode it requires the caller to be authenticated. |
| 28 | + |
| 29 | +**Returns:** `bool` |
| 30 | + |
| 31 | +**Example - gating a custom template fragment:** |
| 32 | + |
| 33 | +```php |
| 34 | +add_action( 'jetonomy_sidebar_before', function () { |
| 35 | + if ( ! \Jetonomy\Visibility::can_view_community() ) { |
| 36 | + return; |
| 37 | + } |
| 38 | + echo do_shortcode( '[my_member_only_widget]' ); |
| 39 | +} ); |
| 40 | +``` |
| 41 | + |
| 42 | +The check is global, not per-resource - you do not need to pass a user ID or a post ID. Per-resource visibility (private spaces, restricted access rules) is enforced separately inside the relevant controllers. |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +### `Visibility::get_mode()` |
| 47 | + |
| 48 | +Returns the active visibility mode as a string. Useful when you want to render a different UI in private mode (e.g. a "Members only" badge in the site header) without duplicating the option read. |
| 49 | + |
| 50 | +**Returns:** `string` - `'public'` or `'private'` |
| 51 | + |
| 52 | +**Example - tagging the body class:** |
| 53 | + |
| 54 | +```php |
| 55 | +add_filter( 'body_class', function ( array $classes ): array { |
| 56 | + $classes[] = 'jt-mode-' . \Jetonomy\Visibility::get_mode(); |
| 57 | + return $classes; |
| 58 | +} ); |
| 59 | +``` |
| 60 | + |
| 61 | +The function defaults to `'public'` on a fresh install - an unset, null, or `true` value of `guest_read` all resolve to public. Only an explicit `false` flips the community to private. |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### `Visibility::rest_check()` |
| 66 | + |
| 67 | +Designed to be used as a REST `permission_callback`. Returns `true` when the caller may proceed, or a 401 `WP_Error` with code `community_private` when the community is in private mode and the caller is not logged in. |
| 68 | + |
| 69 | +**Returns:** `true|\WP_Error` |
| 70 | + |
| 71 | +**Example - protecting your own REST route:** |
| 72 | + |
| 73 | +```php |
| 74 | +register_rest_route( 'my-plugin/v1', '/community-events', [ |
| 75 | + 'methods' => 'GET', |
| 76 | + 'callback' => 'my_plugin_list_events', |
| 77 | + 'permission_callback' => [ '\Jetonomy\Visibility', 'rest_check' ], |
| 78 | +] ); |
| 79 | +``` |
| 80 | + |
| 81 | +**Example - chaining with an existing capability check:** |
| 82 | + |
| 83 | +```php |
| 84 | +'permission_callback' => static function ( $request ) { |
| 85 | + $vis = \Jetonomy\Visibility::rest_check( $request ); |
| 86 | + if ( is_wp_error( $vis ) ) { |
| 87 | + return $vis; |
| 88 | + } |
| 89 | + return current_user_can( 'read' ); |
| 90 | +}, |
| 91 | +``` |
| 92 | + |
| 93 | +Anonymous calls in private mode return: |
| 94 | + |
| 95 | +```json |
| 96 | +{ |
| 97 | + "code": "community_private", |
| 98 | + "message": "This community is private. Please log in to view content.", |
| 99 | + "data": { "status": 401 } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Authenticated calls fall through to your route's own permission logic. |
| 104 | + |
| 105 | +> **Note:** `/auth/*` and `/admin/*` endpoints intentionally do not route through this helper. Locking `/auth/*` would lock users out forever, and admin endpoints have their own capability gates. Apply `Visibility::rest_check` to public-read endpoints only. |
| 106 | +
|
| 107 | +--- |
| 108 | + |
| 109 | +## The Access Matrix Runner |
| 110 | + |
| 111 | +`bin/access-matrix-check.sh` is the regression safety net that proves the helper actually works. It walks a representative subset of every Jetonomy REST route as every role, makes the real HTTP call, and asserts the response code matches the documented expectation. |
| 112 | + |
| 113 | +### What it covers |
| 114 | + |
| 115 | +- **78 checks** across the public REST surface |
| 116 | +- **6 roles** - anonymous, subscriber, author, editor (space-admin), moderator, administrator |
| 117 | +- **Both modes** - the runner can flip `jetonomy_settings.guest_read` to private for the duration of the run and restore it on exit (even if a check fails midway) |
| 118 | + |
| 119 | +In public mode the runner asserts that anonymous reads return `200`. In private mode it asserts the same anonymous reads return `401` from the central `Visibility::rest_check` gate. Logged-in calls are mode-independent and stay unchanged in either mode. |
| 120 | + |
| 121 | +### Running it locally |
| 122 | + |
| 123 | +```bash |
| 124 | +# From the plugin root: |
| 125 | +bin/access-matrix-check.sh # public mode (default) |
| 126 | +bin/access-matrix-check.sh --mode=private # private community gate |
| 127 | +bin/access-matrix-check.sh --quiet # only show failures |
| 128 | +``` |
| 129 | + |
| 130 | +Sample output: |
| 131 | + |
| 132 | +``` |
| 133 | +PASS GET /spaces [anon] expected=200 got=200 |
| 134 | +PASS GET /spaces [subscriber] expected=200 got=200 |
| 135 | +PASS POST /posts/123/vote [anon] expected=401 got=401 |
| 136 | +FAIL GET /posts/recent [anon] expected=401 got=200 <- regression |
| 137 | +
|
| 138 | +Summary: 77 passed, 1 failed (78 total) |
| 139 | +``` |
| 140 | + |
| 141 | +A regression is any row where the actual response code does not match the documented expectation. Exit code is `0` on a clean run, `1` if any row fails. |
| 142 | + |
| 143 | +### Release gating |
| 144 | + |
| 145 | +`bin/build-release.sh` invokes the access matrix runner before producing the release zip. If any row regresses, the build aborts and no zip is produced - the gate exists so we never ship a release that quietly opens up a previously-locked endpoint or quietly locks a previously-open one. |
| 146 | + |
| 147 | +Run the matrix locally before every commit that touches `permission_callback` wiring, the `Visibility` helper, or REST route registration. |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +## What's Next? |
| 152 | + |
| 153 | +- [Hooks Reference](./02-hooks-reference.md) - All `jetonomy_*` actions and filters |
| 154 | +- [REST API Reference](./01-rest-api.md) - Full endpoint listing with permission contracts |
| 155 | +- [Modal Toolkit](./09-modal-toolkit.md) - Front-end `jetonomyConfirm` / `jetonomyAlert` / `jetonomyPrompt` API |
0 commit comments