Skip to content

Check the video before the analytics and engagement routes answer for it - #2249

Merged
rudrakshi-gupta merged 4 commits into
developfrom
fix/analytics-engagement-scope
Nov 3, 2026
Merged

rudrakshi-gupta merged 4 commits into
developfrom
fix/analytics-engagement-scope

Conversation

@subodhr258

@subodhr258 subodhr258 commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

What changes

Analytics (inc/classes/rest-api/class-analytics.php): the fetch, history and layer-analytics routes still need upload_files, and now also need the requested video to be an attachment of this site that the user can edit (can_read_video_analytics()), which is what the Media Editor asks before it opens a video. Editors and Administrators can edit every attachment, so nothing changes for them; an Author reads the analytics of their own videos.

Engagement (inc/classes/rest-api/class-engagement.php): the video the player sends is turned into a GoDAM Central job ID only through an attachment of this site, never taken from the request. A numeric ID is an attachment ID; cmmid_<job> stands for the attachment that carries that job (rtgodam_transcoding_job_id or _godam_original_id).

  • Reading likes, comments and views, and recording a like or comment, work only for a video the user may see under rtgodam_can_view_attachment(), the same rule as GoDAM's other public routes (get_visible_transcoder_job_id()). A video uploaded while drafting one post and used on a published page counts as public, so its likes and comments still show.
  • Reading any other video returns what a video nobody has engaged with returns, before Central or the analytics service is called, so the player's other requests aren't affected. Liking or commenting on it gets 403 video_not_available and nothing is sent.
  • Editing and deleting a comment find the job the same way, and stay limited to the signed-in user's own comments.

Before merging

Done: the switch to rtgodam_can_view_attachment() (from #2245, now in develop) is in 4dbd3d1. That helper brackets its own attachment lookup, so it runs after the job lookup closes and the lookup hooks are never nested. A new test, test_video_used_on_a_public_page_is_answered, fails on the earlier check and passes now. The Playground results below were run before the switch; the unit tests cover the change.

Testing

Throwaway WordPress (Playground; PHP 7.4 + WordPress 6.8 and PHP 8.5 + WordPress 7.1, same results) with the 2.5.0 release plus these files; Central and the analytics service stubbed.

Check Role Result
Analytics of any existing video Admin, Editor 200
Analytics of own video Author 200; page renders with data
Analytics of another author's or the admin's video Author 403
Analytics for a missing or non-attachment ID any 403
Likes, comments, views of a public video (numeric and cmmid_) visitor same counts as before
Engagement of a private or draft-attached video visitor, Subscriber, Contributor, Author empty, nothing sent to Central
Same Admin, Editor as before
A job that isn't on this site, a page ID, a missing ID any empty on read; 403 on like or comment, nothing sent
Like and comment on a public video through the real UI Subscriber work
Delete own comment / someone else's signed in 200 / 403, as before
  • PHPUnit: EngagementVideoScopeTest (39 cases) plus updates to AnalyticsRoutePermissionTest, EngagementCommentPrivacyTest and SiteOriginTest; full suite OK (658 tests).
  • Jest: AnalyticsForbiddenNotice.test.js (the 403 check and the notice text).
  • phpcs clean; bin/hook-check checks pass.

Before and after

An Author opens Single Video Analytics for their own video (left) and for a video an Administrator uploaded (right), on godam-dev.local in headless Chrome. This site's analytics service is not running, so the left pane is a real outage: it must keep the "temporarily unavailable" notice. Each step's check is in the caption bar.

▶ Video (8 s): before the fix, at 4dbd3d1. The refused video (403) shows the same "Analytics is temporarily unavailable" notice as the outage.

Video: before the fix, both panes show Analytics is temporarily unavailable

▶ Play the video

▶ Video (11 s): after the fix, at d81682f. Same steps. The outage still shows "temporarily unavailable"; the refused video (403) shows "You can't view analytics for this video."

Video: after the fix, the refused video shows You can't view analytics for this video

▶ Play the video

The refused video, before and after (step 3 of each recording; the route answered 403 rest_forbidden both times):

Before: Analytics is temporarily unavailable on both panes

After: You can't view analytics for this video on the right pane

Not covered here: an Author's own video showing real analytics data, because this site's analytics service is not running. The route side (200 for the Author's own video, 403 for another user's) is in AnalyticsRoutePermissionTest.

Changelog

  • Tweak: Authors see analytics only for the videos they can edit.

Worth knowing

  • Authors no longer see analytics of videos they can't edit. The Analytics page tells them so ("You can't view analytics for this video"), and keeps the "temporarily unavailable" notice for real outages.
  • A cmmid_ video with no local attachment (an embed that never went through the GoDAM tab) shows no engagement and refuses likes and comments.
  • An attachment with no transcoding job shows no engagement.
  • The player doesn't handle a 403 on like: the count bumps locally and nothing is saved. That only happens for videos the user can't see.
  • The engagement bar is behind rtgodam_enable_engagement_feature (off by default), but the routes are registered on every connected site.
  • Central should also limit its likes and comments endpoints to the key's own jobs; that is a separate change on the Central side.

QA:

  1. As Admin and as an Editor, open the Analytics page of several videos: data shows.
  2. As an Author, open the Analytics page of your own video: data shows.
  3. With the engagement bar enabled, like and comment on a public video as a signed-in user, and view the counts logged out.
  4. As an Author, open the Analytics page of a video uploaded by another user: the page shows "You can't view analytics for this video", not the "temporarily unavailable" notice.

Issue: rtCamp/godam-plugin-wp#77

@subodhr258 subodhr258 self-assigned this Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

🔍 WordPress Plugin Check Report

⚠️ Status: Passed with warnings

📊 Report

🎯 Total Issues ❌ Errors ⚠️ Warnings
12 0 12

⚠️ Warnings (12)

📁 readme.txt (1 warning)
📍 Line 🔖 Check 💬 Message
0 mismatched_plugin_name Plugin name "GoDAM - Video Hosting for LMS & Online Courses" is different from the name declared in plugin header "GoDAM".
📁 assets/build/css/main.css (1 warning)
📍 Line 🔖 Check 💬 Message
0 EnqueuedStylesScope This style is being loaded in all contexts.
📁 assets/src/libs/analytics.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/11/03/hello-world/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle analytics-library) is loaded in the footer. Consider a defer or async script loading strategy instead.
📁 assets/build/js/main.min.js (5 warnings)
📍 Line 🔖 Check 💬 Message
0 EnqueuedScriptsScope This script is being loaded in all frontend contexts.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880 (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/2026/11/03/hello-world/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/sample-page/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.
0 NonBlockingScripts.NoStrategy This script on http://localhost:8880/demo-attachment-post/ (with handle rtgodam-script) is loaded in the footer. Consider a defer or async script loading strategy instead.

🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

@subodhr258 subodhr258 added the risk: high Full human QA and sign-off (security, data, Woo revenue, Central API) label Oct 30, 2026
Analytics: the fetch, history and layer-analytics routes still need the
`upload_files` capability and now also need the video to be an attachment of
this site that the user can edit, which is what the Media Editor asks before it
opens a video. Editors and administrators can edit every attachment, so for
them nothing changes; an author reads the analytics of their own videos.

Engagement: the video ID the player sends is turned into a GoDAM Central job ID
only through an attachment of this site, never taken from the request. A
numeric ID is an attachment ID, and `cmmid_<job>` stands for the attachment
that carries that job ID.
- Reading a video's likes, comments and views, and recording a like or a
  comment, work only for an attachment that is publicly viewable or that the
  current user may read.
- Reading any other video gives what a video nobody has engaged with gives, and
  neither Central nor the analytics service is called. Liking or commenting on
  it is answered with a 403 and nothing is sent.
- Deleting or editing a comment finds its job the same way, and still only for
  the signed-in user's own comments.

Tests cover the route permissions per capability and per video, the video
lookup, and each engagement route for a public video, a video the user cannot
read, another site's job ID, and IDs that are not attachments.
@subodhr258
subodhr258 force-pushed the fix/analytics-engagement-scope branch from b1d6e87 to 4519faf Compare October 30, 2026 05:08
@subodhr258

Copy link
Copy Markdown
Collaborator Author

Rebased onto develop (it had a conflict in tests/bootstrap.php). The only change beyond the rebase is in the current_user_can() test stub: this PR and the other Garuda route PR both extend it, and each read object_caps in its own shape. The stub now accepts both shapes, and the text is the same in both PRs, so they also merge cleanly together.

  • Full PHP suite: the same 30 errors as develop alone (PHP 8.5 deprecation warnings in older tests), no new ones.
  • With both route PRs merged onto develop together: no conflict, and their 4 test files pass (121 tests).
  • PHPCS clean on tests/bootstrap.php.

Likes, comments and views now follow the same rule as GoDAM's other public
routes. A video uploaded while drafting one post and used on a published page
counts as public, so its likes and comments show to visitors.

The check runs after the job lookup closes, because the helper brackets its
own attachment lookup; the hooks stay in pairs and are never nested.
@rudrakshi-gupta

Copy link
Copy Markdown
Member

Code review

Resolving the job ID only through an attachment of this site (never trusting the cmmid_ value directly) and gating on rtgodam_can_view_attachment() is the right shape, and the before/after lookup hooks stay balanced. The test matrix is thorough.

A few things to check:

1. Cost of the lookup on a public, unauthenticated route (find_transcoder_job_id())
For cmmid_ IDs, every anonymous GET /engagement/activities (once per video on the page) runs a get_posts() with an OR meta_query on two unindexed meta_values, then rtgodam_can_view_attachment(), which may search for posts using the video. On a large media library that is a postmeta scan per request, and anyone can trigger it. Suggest caching the job ID to attachment ID mapping (e.g. wp_cache_get/set keyed by job ID, cleared when the job meta changes) and passing 'update_post_meta_cache' => false, 'update_post_term_cache' => false to the query.

2. Non-numeric video_id on the analytics routes (can_read_video_analytics())
absint( $video_id ) means any cmmid_… or other non-numeric ID now gets a 403. Please confirm the Media Editor and Analytics page always send the attachment ID for virtual or Central media. Also, absint( '41abc' ) is accepted here (41) while Engagement uses ctype_digit(); might be worth aligning the two.

3. Behaviour change for authors
Previously any upload_files user could see per-video analytics for every video; now authors only see videos they can edit. Probably intended, but worth a changelog line, and a check that the Analytics page handles the 403 gracefully if it lists videos the author cannot open.

4. Shape of the “no engagement” response (get_activities())
The early return omits views_count. Fine if the player treats it as optional; just confirm it doesn't render undefined / NaN for those videos.

5. Nit
With posts_per_page => 10 and first-visible-wins, adding 'orderby' => 'ID', 'order' => 'ASC' would make the chosen attachment deterministic when several share a job ID.


Review drafted with Claude.

The analytics routes now refuse a video the user cannot edit (403), and the
Analytics page showed that as "Analytics is temporarily unavailable". It now
shows that analytics is only shown for videos you can edit, through the new
AnalyticsForbiddenNotice, and keeps the unavailable notice for real outages.

Addresses review: #2249 (comment)
@subodhr258

Copy link
Copy Markdown
Collaborator Author

@rudrakshi-gupta on your comment:

1. Checked, and core already does both. With 'fields' => 'ids', WP_Query returns the IDs before priming meta or term caches, and it caches the IDs it found in the object cache (post-queries group) under a key salted with the posts last_changed, which added_post_meta, updated_post_meta and deleted_post_meta bump. So on a site with a persistent object cache, a repeat request for the same job is answered from the cache, and a change to either job meta key clears it. For an unknown ID nothing is sent to Central or the analytics service. No change needed.

2. Checked: every route behind check_analytics_read_permission() already declares video_id as an integer with is_numeric() validation and absint (class-analytics.php:268), and core validates and sanitises params before it calls the permission callback. So 41abc and cmmid_ IDs get a 400 before this check, as they did before this PR. The Analytics page sends the attachment ID (pages/analytics/index.js:54). No change needed.

3. Agreed, fixed in d81682f. When the analytics route refuses the video (403), the page now says "You can't view analytics for this video. Analytics is shown only for videos you can edit." instead of the temporarily-unavailable notice, which stays for real outages. AnalyticsForbiddenNotice.test.js covers the 403 check and the text. The PR description now has the changelog line and a QA step for an Author opening someone else's video.

4. Checked: the player reads it as parseInt( views[ id ] ) || 0 (engagement.js:367) and getViews()[ id ] || 0 (:1150), so a missing views_count shows 0. The normal path already leaves it out when the analytics service returns nothing (class-engagement.php:307). No change needed.

5. Checked, and the order can't change the answer. For a cmmid_ ID every candidate's job ID is that same job ($job_id = $named_job), so the lookup returns the same value whichever visible attachment comes first, and a numeric ID has only one candidate. No change needed.

@rudrakshi-gupta rudrakshi-gupta left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed: the replies check out (core validates video_id as numeric before the permission callback runs, so the absint concern doesn't apply), and d81682f's forbidden notice looks good. LGTM.

Optional nit, non-blocking: isAnalyticsForbidden() matches any 403, but WordPress also returns 403 for an expired REST nonce (rest_cookie_invalid_nonce). After a long-idle tab an Editor would see "You can't view analytics for this video" instead of a reload prompt. Checking error?.data?.code === 'rest_forbidden' would keep the two apart.

Review drafted with Claude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high Full human QA and sign-off (security, data, Woo revenue, Central API)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants