Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Use (new Sample_Video_Updater())->process( $data ) to update the video file.
- */
- class Sample_Video_Updater {
- /**
- * Runs after processing the video file.
- *
- * @param array $data Processed video details {
- * @type string $source_type Source type.
- * @type int $as3cf_item_id Offload media item id.
- * @type string $new_file_name New file name.
- * @type string $acf_object_key ACF object key.
- * @type int $width Width.
- * @type int $height Height.
- * @type int $duration Duration.
- * @type string $acf_object_key ACF object key.
- * }.
- */
- public function process( array $data ) {
- $source_type = empty( $data['source_type'] ) ? '' : $data['source_type'];
- $as3cf_item_id = empty( $data['as3cf_item_id'] ) ? '' : $data['as3cf_item_id'];
- $as3cf_item = $this->get_item( $as3cf_item_id, $source_type );
- // If attachment id not found or id is not video return.
- if ( ! $as3cf_item || empty( $data['new_file_name'] ) ) {
- return;
- }
- // update s3 row + meta
- // update attachment mime type+guid
- // update attachment metadata.
- $existing_basename = wp_basename( $as3cf_item->path() );
- $new_filename = wp_basename( $data['new_file_name'] );
- $path = str_replace( $existing_basename, $new_filename, $as3cf_item->path() );
- $source_path = str_replace( $existing_basename, $new_filename, $as3cf_item->source_path() );
- $original_path = str_replace( $existing_basename, $new_filename, $as3cf_item->original_path() );
- $original_source_path = str_replace( $existing_basename, $new_filename, $as3cf_item->original_source_path() );
- $as3cf_item->set_path( $path );
- $as3cf_item->set_source_path( $source_path );
- $as3cf_item->set_original_path( $original_path );
- $as3cf_item->set_original_source_path( $original_source_path );
- /**
- * @todo: avoid shallow override of object instead of deep override
- */
- $extra_info = array_merge(
- $as3cf_item->extra_info(),
- array(
- 'objects' => array(
- $as3cf_item::primary_object_key() => array(
- 'source_file' => $new_filename,
- ),
- ),
- )
- );
- $as3cf_item->set_extra_info( $extra_info );
- $as3cf_item->set_is_private( $as3cf_item->is_private() );
- $save = $as3cf_item->save();
- if ( is_wp_error( $save ) ) {
- error_log( 'Message: ' . $save->get_error_message() );
- return;
- }
- $type = wp_check_filetype( $source_path );
- $mime_type = isset( $type['type'] ) ? $type['type'] : '';
- $source_id = $as3cf_item->source_id();
- update_post_meta( $source_id, '_wp_attached_file', $source_path );
- delete_post_meta( $source_id, '__aws_transcoding_state' );
- // Raw access, avoid wp_get_attachment_metadata().
- $meta_data = array(
- 'width' => $data['width'],
- 'height' => $data['height'],
- 'length' => $data['duration'],
- );
- if ( $mime_type ) {
- $meta_data['mime_type'] = $mime_type;
- }
- update_post_meta(
- $source_id,
- '_wp_attachment_metadata',
- $meta_data
- );
- // when the file format changes in transcoding.
- if ( $mime_type && get_post_mime_type( $source_id ) !== $mime_type ) {
- // update post table columns.
- $post = get_post( $source_id );
- $guid = str_replace( wp_basename( $post->guid ), $new_filename, $post->guid );
- wp_update_post(
- array(
- 'ID' => $source_id,
- 'post_mime_type' => $mime_type,
- 'guid' => $guid,
- )
- );
- }
- $region = $as3cf_item->region();
- $bucket = $as3cf_item->bucket();
- global $as3cf;
- // Delete original file.
- $as3cf->get_provider_client( $region )->delete_objects(
- array(
- 'Bucket' => $bucket,
- 'Delete' => array(
- 'Objects' => array(
- array( 'Key' => $data['acf_object_key'] )
- ),
- ),
- )
- );
- }
- /**
- * Get item from source type and item id.
- *
- * @param int $as3cf_item_id Item id.
- * @param string $as3cf_source_type Source type.
- *
- * @return false|Item
- */
- private function get_item( int $as3cf_item_id, string $as3cf_source_type ) {
- /* @var Amazon_S3_And_CloudFront_Pro */
- global $as3cf;
- $class = $as3cf->get_source_type_class( $as3cf_source_type );
- if ( ! $class ) {
- return false;
- }
- return $class::get_by_id( $as3cf_item_id );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement