Advertisement
buddydev

Untitled

Nov 6th, 2024
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.27 KB | None | 0 0
  1.  
  2. /**
  3.  * Use (new Sample_Video_Updater())->process( $data ) to update the video file.
  4.  */
  5. class Sample_Video_Updater {
  6.  
  7.     /**
  8.      * Runs after processing the video file.
  9.      *
  10.      * @param array $data Processed video details {
  11.      *     @type string $source_type Source type.
  12.      *     @type int $as3cf_item_id Offload media item id.
  13.      *     @type string $new_file_name New file name.
  14.      *     @type string $acf_object_key ACF object key.
  15.      *     @type int $width Width.
  16.      *     @type int $height Height.
  17.      *     @type int $duration Duration.
  18.      *     @type string $acf_object_key ACF object key.
  19.      * }.
  20.      */
  21.     public function process( array $data ) {
  22.  
  23.         $source_type   = empty( $data['source_type'] ) ? '' : $data['source_type'];
  24.         $as3cf_item_id = empty( $data['as3cf_item_id'] ) ? '' : $data['as3cf_item_id'];
  25.  
  26.         $as3cf_item = $this->get_item( $as3cf_item_id, $source_type );
  27.  
  28.         // If attachment id not found or id is not video return.
  29.         if ( ! $as3cf_item || empty( $data['new_file_name'] ) ) {
  30.             return;
  31.         }
  32.  
  33.         // update s3 row + meta
  34.         // update attachment mime type+guid
  35.         // update attachment metadata.
  36.  
  37.         $existing_basename    = wp_basename( $as3cf_item->path() );
  38.         $new_filename         = wp_basename( $data['new_file_name'] );
  39.         $path                 = str_replace( $existing_basename, $new_filename, $as3cf_item->path() );
  40.         $source_path          = str_replace( $existing_basename, $new_filename, $as3cf_item->source_path() );
  41.         $original_path        = str_replace( $existing_basename, $new_filename, $as3cf_item->original_path() );
  42.         $original_source_path = str_replace( $existing_basename, $new_filename, $as3cf_item->original_source_path() );
  43.  
  44.         $as3cf_item->set_path( $path );
  45.         $as3cf_item->set_source_path( $source_path );
  46.         $as3cf_item->set_original_path( $original_path );
  47.         $as3cf_item->set_original_source_path( $original_source_path );
  48.  
  49.         /**
  50.          * @todo: avoid shallow override of object instead of deep override
  51.          */
  52.         $extra_info = array_merge(
  53.             $as3cf_item->extra_info(),
  54.             array(
  55.                 'objects' => array(
  56.                     $as3cf_item::primary_object_key() => array(
  57.                         'source_file' => $new_filename,
  58.                     ),
  59.                 ),
  60.             )
  61.         );
  62.  
  63.         $as3cf_item->set_extra_info( $extra_info );
  64.         $as3cf_item->set_is_private( $as3cf_item->is_private() );
  65.  
  66.         $save = $as3cf_item->save();
  67.  
  68.         if ( is_wp_error( $save ) ) {
  69.             error_log( 'Message: ' . $save->get_error_message() );
  70.             return;
  71.         }
  72.  
  73.         $type = wp_check_filetype( $source_path );
  74.  
  75.         $mime_type = isset( $type['type'] ) ? $type['type'] : '';
  76.  
  77.         $source_id = $as3cf_item->source_id();
  78.         update_post_meta( $source_id, '_wp_attached_file', $source_path );
  79.         delete_post_meta( $source_id, '__aws_transcoding_state' );
  80.         // Raw access, avoid wp_get_attachment_metadata().
  81.         $meta_data = array(
  82.             'width'  => $data['width'],
  83.             'height' => $data['height'],
  84.             'length' => $data['duration'],
  85.         );
  86.  
  87.         if ( $mime_type ) {
  88.             $meta_data['mime_type'] = $mime_type;
  89.         }
  90.  
  91.         update_post_meta(
  92.             $source_id,
  93.             '_wp_attachment_metadata',
  94.             $meta_data
  95.         );
  96.  
  97.         // when the file format changes in transcoding.
  98.         if ( $mime_type && get_post_mime_type( $source_id ) !== $mime_type ) {
  99.             // update post table columns.
  100.             $post = get_post( $source_id );
  101.             $guid = str_replace( wp_basename( $post->guid ), $new_filename, $post->guid );
  102.             wp_update_post(
  103.                 array(
  104.                     'ID'             => $source_id,
  105.                     'post_mime_type' => $mime_type,
  106.                     'guid'           => $guid,
  107.                 )
  108.             );
  109.         }
  110.  
  111.  
  112.         $region = $as3cf_item->region();
  113.         $bucket = $as3cf_item->bucket();
  114.  
  115.         global $as3cf;
  116.  
  117.         // Delete original file.
  118.         $as3cf->get_provider_client( $region )->delete_objects(
  119.             array(
  120.                 'Bucket' => $bucket,
  121.                 'Delete' => array(
  122.                     'Objects' => array(
  123.                         array( 'Key' => $data['acf_object_key'] )
  124.                     ),
  125.                 ),
  126.             )
  127.         );
  128.     }
  129.  
  130.     /**
  131.      * Get item from source type and item id.
  132.      *
  133.      * @param int   $as3cf_item_id    Item id.
  134.      * @param string $as3cf_source_type Source type.
  135.      *
  136.      * @return false|Item
  137.      */
  138.     private function get_item( int $as3cf_item_id, string $as3cf_source_type ) {
  139.         /* @var Amazon_S3_And_CloudFront_Pro */
  140.  
  141.         global $as3cf;
  142.  
  143.         $class = $as3cf->get_source_type_class( $as3cf_source_type );
  144.  
  145.         if ( ! $class ) {
  146.             return false;
  147.         }
  148.  
  149.         return $class::get_by_id( $as3cf_item_id );
  150.     }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement