register_meta(); add_filter( 'wp_prepare_attachment_for_js', [ $this, 'wp_prepare_attachment_for_js' ] ); } /** * Register meta for attachment post type. * * @since 1.12.0 */ public function register_meta(): void { register_meta( 'post', self::TRIM_POST_META_KEY, [ 'type' => 'object', 'description' => __( 'Video trim data.', 'web-stories' ), 'show_in_rest' => [ 'schema' => [ 'properties' => [ 'original' => [ 'description' => __( 'Original attachment id', 'web-stories' ), 'type' => 'integer', ], 'start' => [ 'description' => __( 'Start time.', 'web-stories' ), 'type' => 'string', ], 'end' => [ 'description' => __( 'End time.', 'web-stories' ), 'type' => 'string', ], ], ], ], 'default' => [ 'original' => 0, ], 'single' => true, 'object_subtype' => 'attachment', ] ); } /** * Filters the attachment data prepared for JavaScript. * * @since 1.12.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'] ) { /** * Post ID. * * @var int $post_id */ $post_id = $response['id']; $response[ self::TRIM_DATA_KEY ] = get_post_meta( $post_id, self::TRIM_POST_META_KEY, true ); } return $response; } /** * Act on plugin uninstall. * * @since 1.26.0 */ public function on_plugin_uninstall(): void { delete_post_meta_by_key( self::TRIM_POST_META_KEY ); } }