Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
41.12% |
88 / 214 |
|
46.67% |
7 / 15 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_REST_API_V2_Attachment_VideoPress_Data | |
41.04% |
87 / 212 |
|
46.67% |
7 / 15 |
1530.38 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_fields | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| add_jetpack_videopress_custom_query_filters | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| filter_attachments_by_jetpack_videopress_fields | |
32.43% |
12 / 37 |
|
0.00% |
0 / 1 |
56.42 | |||
| filter_attachments_by_jetpack_videopress_fields_wpcom | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
56 | |||
| apply_wpcom_id_constraint | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
14 | |||
| wpcom_privacy_type_plan | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
10 | |||
| get_wpcom_videopress_post_ids | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
90 | |||
| wpcom_privacy_value_set | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
7 | |||
| get_schema | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| get_videopress_data | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
90 | |||
| is_video | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| remove_field_for_non_videos | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| video_is_private | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Extend the REST API functionality for VideoPress users. |
| 4 | * |
| 5 | * @package automattic/jetpack-videopress |
| 6 | * @since-jetpack 7.1.0 |
| 7 | * @since 0.3.1 |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\VideoPress; |
| 11 | |
| 12 | use Automattic\Jetpack\Connection\Manager as Jetpack_Connection; |
| 13 | use WP_Post; |
| 14 | use WP_REST_Request; |
| 15 | use WP_REST_Response; |
| 16 | |
| 17 | /** |
| 18 | * Add per-attachment VideoPress data. |
| 19 | * |
| 20 | * { # Attachment Object |
| 21 | * ... |
| 22 | * jetpack_videopress: (object) VideoPress data |
| 23 | * ... |
| 24 | * } |
| 25 | * |
| 26 | * @since 7.1.0 |
| 27 | * |
| 28 | * @phan-constructor-used-for-side-effects |
| 29 | */ |
| 30 | class WPCOM_REST_API_V2_Attachment_VideoPress_Data { |
| 31 | /** |
| 32 | * The REST Object Type to which the jetpack_videopress field will be added. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $object_type = 'attachment'; |
| 37 | |
| 38 | /** |
| 39 | * The name of the REST API field to add. |
| 40 | * |
| 41 | * @var string $field_name |
| 42 | */ |
| 43 | protected $field_name = 'jetpack_videopress'; |
| 44 | |
| 45 | /** |
| 46 | * Constructor. |
| 47 | */ |
| 48 | public function __construct() { |
| 49 | add_action( 'rest_api_init', array( $this, 'register_fields' ) ); |
| 50 | |
| 51 | add_action( 'rest_api_init', array( $this, 'add_jetpack_videopress_custom_query_filters' ) ); |
| 52 | |
| 53 | // do this again later to collect any CPTs that get registered later. |
| 54 | add_action( 'restapi_theme_init', array( $this, 'register_fields' ), 20 ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Registers the jetpack_videopress field and adds a filter to remove it for attachments that are not videos. |
| 59 | */ |
| 60 | public function register_fields() { |
| 61 | global $wp_rest_additional_fields; |
| 62 | |
| 63 | if ( ! empty( $wp_rest_additional_fields[ $this->object_type ][ $this->field_name ] ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | register_rest_field( |
| 68 | $this->object_type, |
| 69 | $this->field_name, |
| 70 | array( |
| 71 | 'get_callback' => array( $this, 'get' ), |
| 72 | 'update_callback' => null, |
| 73 | 'schema' => $this->get_schema(), |
| 74 | ) |
| 75 | ); |
| 76 | |
| 77 | add_filter( 'rest_prepare_attachment', array( $this, 'remove_field_for_non_videos' ), 10, 2 ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Adds the custom query filters |
| 82 | */ |
| 83 | public function add_jetpack_videopress_custom_query_filters() { |
| 84 | add_filter( 'rest_attachment_query', array( $this, 'filter_attachments_by_jetpack_videopress_fields' ), 999, 2 ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Filter request args to handle the custom VideoPress query filters |
| 89 | * |
| 90 | * Possible filters: |
| 91 | * |
| 92 | * `no_videopress`: the returned attachments should not be VideoPress videos |
| 93 | * (off-Simple: no videopress_guid meta; on Simple: not a |
| 94 | * member of wpcom's videos table) |
| 95 | * `videopress_has_guid`: (WPCOM only) restrict results to VideoPress videos |
| 96 | * (members of wpcom's videos table) |
| 97 | * `videopress_only_videos`: (WPCOM only) restrict results to video attachments |
| 98 | * `videopress_privacy_setting`: restrict by privacy (0/1/2). Off-Simple this |
| 99 | * is a comma list; on Simple it's a single |
| 100 | * DataViews `is` code, resolved via the videos table |
| 101 | * |
| 102 | * @param array $args The original list of args before the filtering. |
| 103 | * @param WP_REST_Request $request The original request data. |
| 104 | */ |
| 105 | public function filter_attachments_by_jetpack_videopress_fields( $args, $request ) { |
| 106 | |
| 107 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 108 | return $this->filter_attachments_by_jetpack_videopress_fields_wpcom( $args, $request ); |
| 109 | } |
| 110 | |
| 111 | if ( ! isset( $args['meta_query'] ) || ! is_array( $args['meta_query'] ) ) { |
| 112 | $args['meta_query'] = array(); |
| 113 | } |
| 114 | |
| 115 | /* To ignore all VideoPress videos, select only attachments without videopress_guid meta field */ |
| 116 | if ( isset( $request['no_videopress'] ) ) { |
| 117 | $args['meta_query'][] = array( |
| 118 | 'key' => 'videopress_guid', |
| 119 | 'compare' => 'NOT EXISTS', |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Hide local attachments that have already been uploaded to VideoPress. |
| 125 | * Such "zombie" locals carry a `_videopress_uploaded_id` meta pointing |
| 126 | * at their VideoPress sibling attachment; the sibling is the row the |
| 127 | * dashboard should surface. |
| 128 | */ |
| 129 | if ( isset( $request['videopress_hide_already_uploaded'] ) ) { |
| 130 | $args['meta_query'][] = array( |
| 131 | 'key' => Uploader::UPLOADED_KEY, |
| 132 | 'compare' => 'NOT EXISTS', |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | /* Filter using privacy setting meta key */ |
| 137 | if ( isset( $request['videopress_privacy_setting'] ) ) { |
| 138 | $videopress_privacy_setting = sanitize_text_field( $request['videopress_privacy_setting'] ); |
| 139 | |
| 140 | /* Allows the filtering to happens using a list of privacy settings separated by comma */ |
| 141 | $videopress_privacy_setting_list = explode( ',', $videopress_privacy_setting ); |
| 142 | |
| 143 | $site_default_is_private = Data::get_videopress_videos_private_for_site(); |
| 144 | |
| 145 | if ( $site_default_is_private ) { |
| 146 | /** |
| 147 | * If the search is looking for private videos and the site default is private, |
| 148 | * the site default setting should be included on the search. |
| 149 | */ |
| 150 | if ( in_array( strval( \VIDEOPRESS_PRIVACY::IS_PRIVATE ), $videopress_privacy_setting_list, true ) ) { |
| 151 | $videopress_privacy_setting_list[] = \VIDEOPRESS_PRIVACY::SITE_DEFAULT; |
| 152 | } |
| 153 | } else { // phpcs:ignore Universal.ControlStructures.DisallowLonelyIf.Found |
| 154 | /** |
| 155 | * If the search is looking for public videos and the site default is public, |
| 156 | * the site default setting should be included on the search. |
| 157 | */ |
| 158 | if ( in_array( strval( \VIDEOPRESS_PRIVACY::IS_PUBLIC ), $videopress_privacy_setting_list, true ) ) { |
| 159 | $videopress_privacy_setting_list[] = \VIDEOPRESS_PRIVACY::SITE_DEFAULT; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $args['meta_query'][] = array( |
| 164 | 'key' => 'videopress_privacy_setting', |
| 165 | 'value' => $videopress_privacy_setting_list, |
| 166 | 'compare' => 'IN', |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /* Filter using rating meta key */ |
| 171 | if ( isset( $request['videopress_rating'] ) ) { |
| 172 | $videopress_rating = sanitize_text_field( $request['videopress_rating'] ); |
| 173 | |
| 174 | /* Allows the filtering to happens using a list of ratings separated by comma */ |
| 175 | $videopress_rating_list = explode( ',', $videopress_rating ); |
| 176 | |
| 177 | $args['meta_query'][] = array( |
| 178 | 'key' => 'videopress_rating', |
| 179 | 'value' => $videopress_rating_list, |
| 180 | 'compare' => 'IN', |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | return $args; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * WordPress.com Simple variant of the VideoPress query filters. |
| 189 | * |
| 190 | * On Simple a VideoPress attachment keeps its ORIGINAL mime (video/mp4, …), |
| 191 | * never video/videopress, and its privacy/type data live in wpcom's global |
| 192 | * `videos`/`video_meta` tables rather than postmeta. So every filter here is |
| 193 | * expressed as a WP_Query ID-set constraint (post__in / post__not_in) resolved |
| 194 | * from those tables, which keeps found_posts — and therefore the X-WP-Total / |
| 195 | * X-WP-TotalPages headers — exact (no client-side truncation). |
| 196 | * |
| 197 | * Only reached when IS_WPCOM is defined and true, where the `videos` tables |
| 198 | * and video_is_private_wpcom_blog() exist. |
| 199 | * |
| 200 | * @param array $args The original WP_Query args. |
| 201 | * @param WP_REST_Request $request The REST request. |
| 202 | * @return array The filtered WP_Query args. |
| 203 | */ |
| 204 | private function filter_attachments_by_jetpack_videopress_fields_wpcom( $args, $request ) { |
| 205 | /* |
| 206 | * Browse default: the `media_type` param is rejected on WPCOM and |
| 207 | * `mime_type=video/*` doesn't narrow the query, so the dashboard sends |
| 208 | * `videopress_only_videos`. The bare major type makes WP_Query match |
| 209 | * `post_mime_type LIKE 'video/%'`. Always sent so the grid shows every |
| 210 | * video; the ID-set constraints below narrow it further when a type or |
| 211 | * privacy filter is active. |
| 212 | */ |
| 213 | if ( isset( $request['videopress_only_videos'] ) ) { |
| 214 | $args['post_mime_type'] = 'video'; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Normalize the privacy filter to a single code. The dashboard's DataViews |
| 219 | * control uses the `is` operator, so it sends one value: 0 = public, |
| 220 | * 1 = private, 2 = site-default. Anything else (empty, malformed, a stray |
| 221 | * comma list) means "no privacy filter". |
| 222 | */ |
| 223 | $privacy = null; |
| 224 | if ( isset( $request['videopress_privacy_setting'] ) ) { |
| 225 | $raw = trim( (string) $request['videopress_privacy_setting'] ); |
| 226 | if ( '0' === $raw || '1' === $raw || '2' === $raw ) { |
| 227 | $privacy = (int) $raw; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Resolve the (type, privacy) pair into a single WP_Query ID-set |
| 233 | * constraint. This reproduces the old client-side matchesClientSideFilters |
| 234 | * exactly — including its treatment of a "local" (non-VideoPress) video as |
| 235 | * public + site-default: locals carry no videos-table privacy row, so they |
| 236 | * surface under the Public and Site-default filters and never under |
| 237 | * Private. See wpcom_privacy_type_plan(). |
| 238 | */ |
| 239 | list( $mode, $set ) = $this->wpcom_privacy_type_plan( |
| 240 | isset( $request['videopress_has_guid'] ), |
| 241 | isset( $request['no_videopress'] ), |
| 242 | $privacy |
| 243 | ); |
| 244 | |
| 245 | $ids = in_array( $mode, array( 'in', 'not_in' ), true ) |
| 246 | ? $this->get_wpcom_videopress_post_ids( $set ) |
| 247 | : array(); |
| 248 | |
| 249 | return $this->apply_wpcom_id_constraint( $args, $mode, $ids ); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Apply a resolved (mode, ids) constraint to WP_Query args, composing with |
| 254 | * any include/exclude constraints core already mapped onto post__in / |
| 255 | * post__not_in (the REST `include`/`exclude` params) rather than clobbering |
| 256 | * them. WP_Query ignores post__not_in whenever post__in is present, so |
| 257 | * whenever this method *introduces* a post__in where none existed, a |
| 258 | * pre-existing exclusion (which WOULD have been honored) is folded into it |
| 259 | * by subtraction. A request carrying both include and exclude keeps core's |
| 260 | * own precedence (include wins, exclude is dropped), matching what the |
| 261 | * off-Simple meta_query path yields. |
| 262 | * |
| 263 | * Pure (args in, args out) so the composition rules are unit-testable |
| 264 | * off-platform; only the resolution of $ids touches wpcom. |
| 265 | * |
| 266 | * @param array $args The WP_Query args. |
| 267 | * @param string $mode How to apply the set: 'in' | 'not_in' | 'empty' | 'none'. |
| 268 | * @param int[] $ids The resolved ID set for 'in'/'not_in'. |
| 269 | * @return array The args with the constraint applied. |
| 270 | */ |
| 271 | private function apply_wpcom_id_constraint( $args, $mode, $ids ) { |
| 272 | // WP_Query normalizes these lists with absint; mirror it so the |
| 273 | // composition operates on the values that would actually be queried. |
| 274 | $existing_in = isset( $args['post__in'] ) ? array_map( 'absint', (array) $args['post__in'] ) : array(); |
| 275 | $existing_not_in = isset( $args['post__not_in'] ) ? array_map( 'absint', (array) $args['post__not_in'] ) : array(); |
| 276 | |
| 277 | switch ( $mode ) { |
| 278 | case 'in': |
| 279 | // Intersect with a pre-existing include list rather than clobber |
| 280 | // it. WP_Query silently skips an empty post__in, so an empty |
| 281 | // result must fall back to a sentinel that matches no attachment |
| 282 | // (post ID 0 never exists) — otherwise it would leak the library. |
| 283 | if ( array() !== $existing_in ) { |
| 284 | // Include + exclude together: core drops the exclude, keep |
| 285 | // that precedence and intersect the include list only. |
| 286 | $ids = array_values( array_intersect( $existing_in, $ids ) ); |
| 287 | } elseif ( array() !== $existing_not_in ) { |
| 288 | // A lone exclude WOULD have been honored by WP_Query, but the |
| 289 | // post__in set here would make it ignored — fold it in by |
| 290 | // subtraction and drop the now-dead arg. |
| 291 | $ids = array_values( array_diff( $ids, $existing_not_in ) ); |
| 292 | unset( $args['post__not_in'] ); |
| 293 | } |
| 294 | $args['post__in'] = array() === $ids ? array( 0 ) : $ids; |
| 295 | break; |
| 296 | |
| 297 | case 'not_in': |
| 298 | // An empty exclude-set excludes nothing; leave the args untouched. |
| 299 | if ( array() === $ids ) { |
| 300 | break; |
| 301 | } |
| 302 | if ( array() !== $existing_in ) { |
| 303 | // post__not_in would be ignored next to post__in — express the |
| 304 | // exclusion by subtracting from the include list instead. |
| 305 | $kept = array_values( array_diff( $existing_in, $ids ) ); |
| 306 | $args['post__in'] = array() === $kept ? array( 0 ) : $kept; |
| 307 | break; |
| 308 | } |
| 309 | $args['post__not_in'] = array_values( array_unique( array_merge( $existing_not_in, $ids ) ) ); |
| 310 | break; |
| 311 | |
| 312 | case 'empty': |
| 313 | // The requested combination can't match anything (e.g. local + |
| 314 | // private). A pre-existing include intersected with the empty set |
| 315 | // is still empty, so the match-nothing sentinel stands either way. |
| 316 | $args['post__in'] = array( 0 ); |
| 317 | break; |
| 318 | |
| 319 | case 'none': |
| 320 | default: |
| 321 | // No type or privacy narrowing — the browse default stands. |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | return $args; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Map a (type, privacy) filter pair to a single WP_Query ID-set constraint, |
| 330 | * reproducing the old client-side matchesClientSideFilters exactly. |
| 331 | * |
| 332 | * The library holds two kinds of video attachment: L = local (not a member of |
| 333 | * wpcom's `videos` table) and V = VideoPress (a member). The old client filter |
| 334 | * treated every local video as public + site-default (is_private defaulted to |
| 335 | * false, privacy to 'site-default'), so locals appeared under the Public and |
| 336 | * Site-default privacy views and never under Private. This mapping preserves |
| 337 | * that parity: privacy is resolved across the whole video library, not just V. |
| 338 | * |
| 339 | * Over the site's video/* attachments, the V sub-sets are: |
| 340 | * Vall — all (non-soft-deleted) videos-table members. |
| 341 | * Vpriv — effectively private: setting 1, or (setting 2 / absent) on a private site. |
| 342 | * Vpub — effectively public: setting 0, or (setting 2 / absent) on a public site. |
| 343 | * Vsd — stored site-default: setting 2 or absent. |
| 344 | * Vexpl — an explicit setting: setting IN (0,1). |
| 345 | * |
| 346 | * Returns [ $mode, $set ], where $mode is how to apply the set and $set names it: |
| 347 | * 'none' / null — no constraint (show everything). |
| 348 | * 'in' / <set> — post__in = <set>. |
| 349 | * 'not_in' / <set> — post__not_in = <set>. |
| 350 | * 'empty' / null — match nothing. |
| 351 | * |
| 352 | * Pure and independent of the resolved site privacy — only the *contents* of |
| 353 | * each named set depend on it (see get_wpcom_videopress_post_ids()) — so this |
| 354 | * is exhaustively unit-testable off-platform. |
| 355 | * |
| 356 | * @param bool $has_guid Type filter = VideoPress (videos-table members only). |
| 357 | * @param bool $no_vp Type filter = local (exclude videos-table members). |
| 358 | * @param int|null $privacy Single privacy code (0/1/2), or null for no privacy filter. |
| 359 | * @return array{0:string,1:string|null} [ $mode, $set ]. |
| 360 | */ |
| 361 | private function wpcom_privacy_type_plan( $has_guid, $no_vp, $privacy ) { |
| 362 | // Strict comparisons throughout: a switch would match `case 0` for a null |
| 363 | // $privacy (PHP's loose null == 0), collapsing "no privacy filter" into |
| 364 | // the Public view. |
| 365 | if ( $has_guid ) { |
| 366 | // Type = VideoPress: always restrict to videos-table members, then |
| 367 | // narrow to the effective/stored privacy set when asked. |
| 368 | if ( 1 === $privacy ) { |
| 369 | return array( 'in', 'priv' ); |
| 370 | } |
| 371 | if ( 0 === $privacy ) { |
| 372 | return array( 'in', 'pub' ); |
| 373 | } |
| 374 | if ( 2 === $privacy ) { |
| 375 | return array( 'in', 'sd' ); |
| 376 | } |
| 377 | return array( 'in', 'all' ); |
| 378 | } |
| 379 | |
| 380 | if ( $no_vp ) { |
| 381 | // Type = local: exclude every videos-table member. Locals are all |
| 382 | // public + site-default and never private, so only the Private view is |
| 383 | // empty; the rest just return L. |
| 384 | if ( 1 === $privacy ) { |
| 385 | return array( 'empty', null ); |
| 386 | } |
| 387 | return array( 'not_in', 'all' ); |
| 388 | } |
| 389 | |
| 390 | // No type filter: privacy applies across the whole video library. |
| 391 | if ( 1 === $privacy ) { |
| 392 | // Vpriv only — locals are never private. |
| 393 | return array( 'in', 'priv' ); |
| 394 | } |
| 395 | if ( 0 === $privacy ) { |
| 396 | // L ∪ Vpub — everything except the effectively-private videos. |
| 397 | return array( 'not_in', 'priv' ); |
| 398 | } |
| 399 | if ( 2 === $privacy ) { |
| 400 | // L ∪ Vsd — everything except videos with an explicit setting. |
| 401 | return array( 'not_in', 'expl' ); |
| 402 | } |
| 403 | return array( 'none', null ); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * List this blog's VideoPress attachment post IDs from wpcom's global |
| 408 | * `videos` table, optionally narrowed to a named privacy set. |
| 409 | * |
| 410 | * Membership in the `videos` table — not postmeta or the mime type — is what |
| 411 | * distinguishes a VideoPress video from a local one on Simple. Soft-deleted |
| 412 | * rows (a `video_meta` row with meta_key 'deleted_at') are always excluded, |
| 413 | * matching every canonical wpcom query over these tables |
| 414 | * (e.g. helpers.php:videopress_get_jetpack_storage_used()). |
| 415 | * |
| 416 | * The named sets mirror wpcom_privacy_type_plan(): |
| 417 | * 'all' — every member, no privacy predicate (Vall). |
| 418 | * 'priv' — effectively private (Vpriv). |
| 419 | * 'pub' — effectively public (Vpub). |
| 420 | * 'sd' — stored site-default (Vsd). |
| 421 | * 'expl' — an explicit privacy_setting IN (0,1) (Vexpl); absent meta does |
| 422 | * NOT qualify, since a missing row is stored site-default. |
| 423 | * |
| 424 | * The literal meta_key strings map to wpcom's VIDEOPRESS_META_KEYS constants: |
| 425 | * 'deleted_at' = DELETED_AT, 'privacy_setting' = PRIVACY_SETTING. The privacy |
| 426 | * integers map to VIDEOPRESS_PRIVACY: 0 = IS_PUBLIC, 1 = IS_PRIVATE, |
| 427 | * 2 = SITE_DEFAULT. They're inlined as literals rather than referencing those |
| 428 | * classes, which aren't present off-platform; this method is only ever called |
| 429 | * from the IS_WPCOM-guarded branch above. |
| 430 | * |
| 431 | * @param string $set Named privacy set: 'all'|'priv'|'pub'|'sd'|'expl'. |
| 432 | * @return int[] Attachment post IDs. |
| 433 | */ |
| 434 | private function get_wpcom_videopress_post_ids( $set = 'all' ) { |
| 435 | global $wpdb; |
| 436 | |
| 437 | $privacy_join = ''; |
| 438 | $privacy_where = ''; |
| 439 | $prepare_args = array( get_current_blog_id() ); |
| 440 | |
| 441 | if ( 'all' !== $set ) { |
| 442 | if ( 'expl' === $set ) { |
| 443 | // Videos with an explicit setting only. A missing privacy_setting |
| 444 | // row is stored site-default, so absent meta does NOT qualify. |
| 445 | $values = array( 0, 1 ); |
| 446 | $inc_null = false; |
| 447 | } else { |
| 448 | $site_is_private = Data::get_videopress_videos_private_for_site(); |
| 449 | switch ( $set ) { |
| 450 | case 'priv': |
| 451 | $code = 1; |
| 452 | break; |
| 453 | case 'pub': |
| 454 | $code = 0; |
| 455 | break; |
| 456 | case 'sd': |
| 457 | default: |
| 458 | $code = 2; |
| 459 | break; |
| 460 | } |
| 461 | list( $values, $inc_null ) = $this->wpcom_privacy_value_set( $code, $site_is_private ); |
| 462 | } |
| 463 | |
| 464 | $privacy_join = 'LEFT OUTER JOIN video_meta AS privacy ON privacy.guid = videos.guid AND privacy.meta_key = \'privacy_setting\''; |
| 465 | |
| 466 | $clauses = array(); |
| 467 | if ( array() !== $values ) { |
| 468 | $placeholders = implode( ', ', array_fill( 0, count( $values ), '%d' ) ); |
| 469 | $clauses[] = "privacy.meta_value IN ( {$placeholders} )"; |
| 470 | $prepare_args = array_merge( $prepare_args, $values ); |
| 471 | } |
| 472 | if ( $inc_null ) { |
| 473 | // No stored privacy_setting row → the video defaults to SITE_DEFAULT. |
| 474 | $clauses[] = 'ISNULL( privacy.meta_value )'; |
| 475 | } |
| 476 | $privacy_where = 'AND ( ' . implode( ' OR ', $clauses ) . ' )'; |
| 477 | } |
| 478 | |
| 479 | /* |
| 480 | * $privacy_join / $privacy_where are assembled above from literal SQL |
| 481 | * fragments and %d placeholders only — never from request input — and every |
| 482 | * value is bound through $wpdb->prepare() via $prepare_args. The |
| 483 | * InterpolatedNotPrepared sniff can't see that the interpolated pieces are |
| 484 | * constant, so disable it (with the direct-query / no-caching sniffs — this |
| 485 | * is a short-lived, per-request video-id lookup) across the multi-line query. |
| 486 | */ |
| 487 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
| 488 | $ids = $wpdb->get_col( |
| 489 | $wpdb->prepare( |
| 490 | "SELECT videos.post_id |
| 491 | FROM videos |
| 492 | LEFT OUTER JOIN video_meta AS delete_key ON delete_key.guid = videos.guid AND delete_key.meta_key = 'deleted_at' |
| 493 | {$privacy_join} |
| 494 | WHERE videos.blog_id = %d |
| 495 | AND ISNULL( delete_key.meta_value ) |
| 496 | {$privacy_where}", |
| 497 | $prepare_args |
| 498 | ) |
| 499 | ); |
| 500 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber |
| 501 | |
| 502 | return array_map( 'intval', (array) $ids ); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Translate a requested privacy code into the concrete set of stored |
| 507 | * `privacy_setting` values — plus whether an absent meta row qualifies — |
| 508 | * that reproduce, for VideoPress videos, the same effective visibility the |
| 509 | * client's old matchesClientSideFilters computed for them. |
| 510 | * |
| 511 | * Effective visibility is private when the stored setting is 1 (IS_PRIVATE), |
| 512 | * or when the setting is 2 (SITE_DEFAULT) or the meta row is absent AND the |
| 513 | * site default is private. A missing privacy_setting row defaults to |
| 514 | * SITE_DEFAULT. The 'site-default' request (2) matches the *stored* setting |
| 515 | * (2 or absent) regardless of the resolved site privacy, mirroring the |
| 516 | * client's `site-default` branch on item.privacy. |
| 517 | * |
| 518 | * This resolves the Vpriv (code 1), Vpub (code 0) and Vsd (code 2) sets used |
| 519 | * by get_wpcom_videopress_post_ids(). Local (non-videos-table) videos carry no |
| 520 | * row here; the caller folds them back into the Public and Site-default views |
| 521 | * via post__not_in, restoring the old client filter's parity. |
| 522 | * |
| 523 | * Pure (no wpcom globals) so it's unit-testable off-platform. |
| 524 | * |
| 525 | * @param int $code Requested privacy code: 0 public, 1 private, 2 site-default. |
| 526 | * @param bool $site_is_private Whether the site default resolves to private. |
| 527 | * @return array{0:int[],1:bool} [ acceptable stored privacy_setting values, whether absent meta qualifies ]. |
| 528 | */ |
| 529 | private function wpcom_privacy_value_set( $code, $site_is_private ) { |
| 530 | switch ( (int) $code ) { |
| 531 | case 1: // Private -- wpcom constant IS_PRIVATE. Site-default and |
| 532 | // absent-meta videos resolve to private on a private site. |
| 533 | return $site_is_private ? array( array( 1, 2 ), true ) : array( array( 1 ), false ); |
| 534 | case 0: // Public -- wpcom constant IS_PUBLIC. Site-default and |
| 535 | // absent-meta videos resolve to public on a public site. |
| 536 | return $site_is_private ? array( array( 0 ), false ) : array( array( 0, 2 ), true ); |
| 537 | case 2: // Site default (SITE_DEFAULT) — match the stored setting, |
| 538 | default: // not the resolved visibility. |
| 539 | return array( array( 2 ), true ); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Defines data structure and what elements are visible in which contexts |
| 545 | */ |
| 546 | public function get_schema() { |
| 547 | return array( |
| 548 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 549 | 'title' => $this->field_name, |
| 550 | 'type' => 'object', |
| 551 | 'context' => array( 'view', 'edit' ), |
| 552 | 'readonly' => true, |
| 553 | 'description' => __( 'VideoPress Data', 'jetpack-videopress-pkg' ), |
| 554 | ); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Getter: Retrieve current VideoPress data for a given attachment. |
| 559 | * |
| 560 | * @param array $attachment Response from the attachment endpoint. |
| 561 | * @param WP_REST_Request $request Request to the attachment endpoint. |
| 562 | * |
| 563 | * @return array |
| 564 | */ |
| 565 | public function get( $attachment, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 566 | if ( ! isset( $attachment['id'] ) ) { |
| 567 | return array(); |
| 568 | } |
| 569 | |
| 570 | $blog_id = Jetpack_Connection::get_site_id(); |
| 571 | if ( ! is_int( $blog_id ) ) { |
| 572 | return array(); |
| 573 | } |
| 574 | |
| 575 | $videopress = $this->get_videopress_data( (int) $attachment['id'], $blog_id ); |
| 576 | |
| 577 | if ( ! $videopress ) { |
| 578 | return array(); |
| 579 | } |
| 580 | |
| 581 | return $videopress; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * Gets the VideoPress GUID for a given attachment. |
| 586 | * |
| 587 | * This is pulled out into a separate method to support unit test mocking. |
| 588 | * |
| 589 | * @param int $attachment_id Attachment ID. |
| 590 | * @param int $blog_id Blog ID. |
| 591 | * |
| 592 | * @return array |
| 593 | */ |
| 594 | public function get_videopress_data( $attachment_id, $blog_id ) { |
| 595 | $info = video_get_info_by_blogpostid( $blog_id, $attachment_id ); |
| 596 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 597 | $title = video_get_title( $blog_id, $attachment_id ); |
| 598 | $description = video_get_description( $blog_id, $attachment_id ); |
| 599 | |
| 600 | $video_attachment = get_blog_post( $blog_id, $attachment_id ); |
| 601 | if ( null === $video_attachment ) { |
| 602 | $caption = ''; |
| 603 | } else { |
| 604 | $caption = $video_attachment->post_excerpt; |
| 605 | } |
| 606 | } else { |
| 607 | $title = $info->title; |
| 608 | $description = $info->description; |
| 609 | $caption = $info->caption; |
| 610 | } |
| 611 | |
| 612 | $video_privacy_setting = ! isset( $info->privacy_setting ) ? \VIDEOPRESS_PRIVACY::SITE_DEFAULT : intval( $info->privacy_setting ); |
| 613 | $private_enabled_for_site = Data::get_videopress_videos_private_for_site(); |
| 614 | $is_private = $this->video_is_private( $video_privacy_setting, $private_enabled_for_site ); |
| 615 | |
| 616 | // The video needs a playback token if it's private for any reason (video privacy setting or site default privacy setting) |
| 617 | $video_needs_playback_token = $is_private; |
| 618 | |
| 619 | return array( |
| 620 | 'title' => $title, |
| 621 | 'description' => $description, |
| 622 | 'caption' => $caption, |
| 623 | 'guid' => $info->guid ?? null, |
| 624 | 'rating' => $info->rating ?? null, |
| 625 | 'allow_download' => |
| 626 | isset( $info->allow_download ) && $info->allow_download ? 1 : 0, |
| 627 | 'display_embed' => |
| 628 | isset( $info->display_embed ) && $info->display_embed ? 1 : 0, |
| 629 | 'privacy_setting' => $video_privacy_setting, |
| 630 | 'needs_playback_token' => $video_needs_playback_token, |
| 631 | 'is_private' => $is_private, |
| 632 | 'private_enabled_for_site' => $private_enabled_for_site, |
| 633 | ); |
| 634 | } |
| 635 | |
| 636 | /** |
| 637 | * Checks if the given attachment is a video. |
| 638 | * |
| 639 | * @param object $attachment The attachment object. |
| 640 | * |
| 641 | * @return false|int |
| 642 | */ |
| 643 | public function is_video( $attachment ) { |
| 644 | return isset( $attachment->post_mime_type ) && wp_startswith( $attachment->post_mime_type, 'video/' ); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Removes the jetpack_videopress field from the response if the |
| 649 | * given attachment is not a video. |
| 650 | * |
| 651 | * @param WP_REST_Response $response Response from the attachment endpoint. |
| 652 | * @param WP_Post $attachment The original attachment object. |
| 653 | * |
| 654 | * @return mixed |
| 655 | */ |
| 656 | public function remove_field_for_non_videos( $response, $attachment ) { |
| 657 | if ( ! $this->is_video( $attachment ) ) { |
| 658 | unset( $response->data[ $this->field_name ] ); |
| 659 | } |
| 660 | |
| 661 | return $response; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Determines if a video is private based on the video privacy |
| 666 | * setting and the site default privacy setting. |
| 667 | * |
| 668 | * @param int $video_privacy_setting The privacy setting for the video. |
| 669 | * @param bool $private_enabled_for_site Flag stating if the default video privacy is private. |
| 670 | * |
| 671 | * @return bool |
| 672 | */ |
| 673 | private function video_is_private( $video_privacy_setting, $private_enabled_for_site ) { |
| 674 | if ( $video_privacy_setting === \VIDEOPRESS_PRIVACY::IS_PUBLIC ) { |
| 675 | return false; |
| 676 | } |
| 677 | if ( $video_privacy_setting === \VIDEOPRESS_PRIVACY::IS_PRIVATE ) { |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | return $private_enabled_for_site; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 686 | wpcom_rest_api_v2_load_plugin( 'Automattic\Jetpack\VideoPress\WPCOM_REST_API_V2_Attachment_VideoPress_Data' ); |
| 687 | } |