register_meta(); add_action( 'delete_attachment', [ $this, 'delete_video' ] ); add_action( 'rest_api_init', [ $this, 'rest_api_init' ] ); add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ] ); } /** * Register meta for attachment post type. * * @since 1.10.0 */ public function register_meta(): void { register_meta( 'post', self::IS_MUTED_POST_META_KEY, [ 'type' => 'boolean', 'description' => __( 'Whether the video is muted', 'web-stories' ), 'default' => false, 'single' => true, 'object_subtype' => 'attachment', ] ); register_meta( 'post', self::MUTED_ID_POST_META_KEY, [ 'sanitize_callback' => 'absint', 'type' => 'integer', 'description' => __( 'ID of muted video.', 'web-stories' ), 'show_in_rest' => true, 'default' => 0, 'single' => true, 'object_subtype' => 'attachment', ] ); } /** * Registers additional REST API fields upon API initialization. * * @since 1.10.0 */ public function rest_api_init(): void { register_rest_field( 'attachment', self::IS_MUTED_REST_API_KEY, [ 'get_callback' => [ $this, 'get_callback_is_muted' ], 'schema' => [ 'type' => [ 'boolean', 'null' ], 'description' => __( 'Whether the video is muted', 'web-stories' ), 'default' => null, 'context' => [ 'view', 'edit', 'embed' ], 'arg_options' => [ 'sanitize_callback' => 'rest_sanitize_boolean', ], ], 'update_callback' => [ $this, 'update_callback_is_muted' ], ] ); } /** * Filters the attachment data prepared for JavaScript. * * @since 1.10.0 * * @param array|mixed $response Array of prepared attachment data. * @return array|mixed Response data. * * @template T * * @phpstan-return ($response is array ? array : mixed) */ public function wp_prepare_attachment_for_js( $response ) { if ( ! \is_array( $response ) ) { return $response; } if ( 'video' === $response['type'] ) { $response[ self::IS_MUTED_REST_API_KEY ] = $this->get_callback_is_muted( $response ); } return $response; } /** * Get the attachment's post meta. * * @since 1.10.0 * * @param array $prepared Array of data to add to. */ public function get_callback_is_muted( array $prepared ): ?bool { /** * Attachment ID. * * @var int $id */ $id = $prepared['id']; /** * Muted value. * * @var bool|null $value */ $value = get_metadata_raw( 'post', $id, self::IS_MUTED_POST_META_KEY, true ); if ( null === $value ) { return $value; } return rest_sanitize_boolean( $value ); } /** * Update the attachment's post meta. * * @since 1.10.0 * * @param mixed $value Value to updated. * @param WP_Post $post Post object to be updated. * @return true|WP_Error */ public function update_callback_is_muted( $value, WP_Post $post ) { $object_id = $post->ID; $name = self::IS_MUTED_REST_API_KEY; $meta_key = self::IS_MUTED_POST_META_KEY; if ( ! current_user_can( 'edit_post_meta', $object_id, $meta_key ) ) { return new \WP_Error( 'rest_cannot_update', /* translators: %s: Custom field key.**/ sprintf( __( 'Sorry, you are not allowed to edit the %s custom field.', 'web-stories' ), $name ), [ 'key' => $name, 'status' => rest_authorization_required_code(), ] ); } update_post_meta( $object_id, $meta_key, $value ); return true; } /** * Deletes associated meta data when a video is deleted. * * @since 1.26.0 * * @param int $attachment_id ID of the attachment to be deleted. */ public function delete_video( int $attachment_id ): void { delete_metadata( 'post', 0, self::MUTED_ID_POST_META_KEY, $attachment_id, true ); } /** * Act on plugin uninstall. * * @since 1.26.0 */ public function on_plugin_uninstall(): void { delete_post_meta_by_key( self::MUTED_ID_POST_META_KEY ); delete_post_meta_by_key( self::IS_MUTED_POST_META_KEY ); } }